1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """The simulation work concerning the MBDyn pre-processing
23 and the writing of input files. This module defined also
24 the MultistepIntegrator."""
25 import os
26
27 from mbdyn.common import Block, BasicObject
28 from mbdyn.groups import Nodes, Elements, ReferenceList
29 from mbdyn.control_data import ControlData
30
31
33 """An argument only displayed on one line for the integrator
34 """
35
37 self.name = mbdyn_name
38
39 self.values = args
40 if type(args)==tuple:
41 self.args = list(args)
42 elif type(args)==list:
43 self.args = args
44 else:
45 self.args = [args]
46
48 """Convert the line argument to string for the MBDyn input file"""
49 values = ", ".join([str(arg) for arg in self.args])
50 return "%s: %s;" % (self.name, values)
51
52
54 """The method associated to an argument for the integrator
55 """
56
58 self.obj = obj
59 self.argument_name = argument_name
60
63
64
66 """Return the method name for the argument method"""
67 return "set_" + mbdyn_name.replace(" ", "_")
68
69
71 """The MBDyn multistep integrator.
72 """
73
75 self.name = "multistep"
76 self.block = Block()
77 self.line_args = []
78 self.line_arg_names = []
79 self.mbdyn_attributes = ["method", "non linear solver", "tolerance",
80 "initial time", "final time", "time step",
81 "min time step", "max time step",
82 "max iterations",
83 "derivatives max iterations",
84 "derivatives tolerance",
85 "derivatives coefficient"]
86 self._build_methods()
87 self.set_property(**kargs)
88
90 """Set the properties on the integrator"""
91 for arg_name, values in kargs.items():
92 method_name = get_method_name(arg_name)
93 self.__dict__[method_name](values)
94
96 """Add an argument chosen by the user to the integrator"""
97 self.line_arg_names.append(arg_name)
98 self.line_args.append(LineArgument(arg_name, value))
99
101 """Build all the possible options defined in 'mbdyn_attributes'."""
102 for mbdyn_attribute in self.mbdyn_attributes:
103 method_name = get_method_name(mbdyn_attribute)
104 method = ArgumentMethod(self, mbdyn_attribute)
105 setattr(self, method_name, method)
106
108 """Return the values of an argument entered by the user"""
109 indice = self.line_arg_names.index(arg_name)
110 values = self.line_args[indice].values
111 if len(values) == 1:
112 return values[0]
113 else:
114 return values
115
129
130
132 """The base of the Simulation. This class will mainly handle
133 the writing of the MBDyn input file, as well as cleaning the
134 directory used for the simulation. This class is always
135 abstract, a help for defining the Simulation one.
136 """
137 - def __init__(self, name, title, keep, overwrite):
138 BasicObject.__init__(self, name)
139
140 self.title = title
141 words = self.name.split()
142 capitalized_words = [word.capitalize() for word in words[1:]]
143 self.os_name = words[0].lower() + "".join(capitalized_words)
144
145 self.nodes = Nodes()
146 self.elts = Elements()
147 self.refs = ReferenceList()
148 self.control_data = ControlData()
149
150
151 self.integrator = MultistepIntegrator(initial_time = 0.,
152 final_time = 2.,
153 time_step = 1e-3,
154 max_iterations = 10,
155 tolerance = 1e-6)
156
157 self.create_dir = None
158 self.clean = None
159 self._overwrite = overwrite
160 self.keep_mbdyn_files(keep)
161
162 self.file_name = None
163 self.dir = None
164
165 self.has_aero_data = False
166 self.has_written = False
167 self.offset = 0
168
170 """An offset only use for writing the MBDyn input file and avoiding
171 integer conflict in the automatic node numbering."""
172 self.offset = int(offset)
173
182
184 """Create the temporary directory for storing MBDyn files"""
185 import tempfile
186 self.dir = tempfile.mkdtemp(suffix="_mbdyn", dir=os.getcwd())
187
189 """Create the directory where to keep the MBDyn files"""
190 if os.path.exists(self.os_name):
191 if not self._overwrite:
192 mess = "'%s' already exists" % self.os_name
193 mess += ", mbdyn will not write into it." + os.linesep
194 mess += "It is needed to delete "
195 mess += "or rename it for starting the simulation"
196 print mess
197 import sys
198 sys.exit()
199 else:
200 os.mkdir(self.os_name)
201 self.dir = self.os_name
202
204 """Will not clear the directory but indicates where are the files"""
205 print "All mbdyn's files are in the directory: '%s'." % self.dir
206
208 """Clean the temporary directory with MBDyn files on the simulation
209 is finished."""
210 if name_pattern == None:
211 name_pattern = self.os_name
212 files_ext = [".frc", ".ine", ".log", ".out", ".mov", ".jnt"]
213 files_to_remove = [self.os_name]
214 for ext in files_ext:
215 files_to_remove.append(name_pattern + ext)
216 for file_name in files_to_remove:
217 file_path = os.path.join(self.dir, file_name)
218 if os.path.exists(file_path):
219 try:
220 os.remove(file_path)
221 except WindowsError:
222 print "Impossible to remove the file" \
223 " '%s' because of Bill!" % file_path
224 return
225 try:
226 os.rmdir(self.dir)
227 except OSError:
228 print "Warning, '%s' directory could not be removed" % self.dir
229 raise
230 except WindowsError:
231 print "The '%s' directory could be removed because" \
232 " of Bill!"
233
241
242
243
244
245
246
247
249 """Write the MBDyn file"""
250
251 self._set_labels_and_names()
252 self.elts.set_node_connectivity()
253 self.create_dir()
254 self.file_name = os.path.join(self.dir, self.os_name)
255 input_file = open(self.file_name, "w")
256 input_file.write("# " + self.title + os.linesep)
257
258
259
260
261
262
263
264 input_file.write(os.linesep)
265
266 for obj in [self.integrator, self.control_data,
267 self.refs, self.nodes, self.elts]:
268 obj.write_into(input_file)
269 input_file.write(os.linesep)
270 input_file.close()
271
275