1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """All the common objects used all over the MBDyn package.
23 They are mainly used for writing the MBDyn input file.
24 """
25
26 import os
27 import numpy as N
28 from mbdyn.record import Record
29 from mbdyn.general import EYE_NAME, VECTOR_NAMES, MATRIX_NAMES
30
31
32
33
34
35
37 """This function returns the MBDyn attribute name.
38 This attribute will be the connection between the Python world
39 and the MBDyn input file"""
40 return "mbdyn" + "_" + name
41
43 """MBDyn supports spaces, this function does the conversion from
44 an argument given in a dictionary to MBDyn"""
45 return name.replace(" ", "_")
46
48 """The length of the list is supposed to be a multiple of 3.
49 """
50 result_list = []
51 indices = N.arange(0, len(raw_list), 3)
52 for i in indices:
53 result_list.append([val for val in raw_list[i:i+3]])
54 return result_list
55
56
58 """An argument used for the MBDyn input file,
59 characterized only by a value without reference.
60 """
61
63 self.value = string
64 self.has_comment = False
65 self.comment = None
66
68 """Set the value of the common argument"""
69 self.value = string
70
75
77 """Return the line to write in the input file."""
78 if self.has_comment:
79 return "%s%s\t# %s" % (self.value, separator, self.comment)
80 else:
81 return "%s%s" % (self.value, separator)
82
85
86
88 """A geometric argument is whether a vector or a matrix.
89 This is a particular case because those entites can be set regarding to
90 a referential, which does not exist on a 'CommonArgument'.
91 It will be written in the MBDyn input file.
92 """
93
97
99 """Return the line to write in the input file."""
100 if self.ref_obj.name == "None":
101 rep = ""
102 elif self.ref_obj.name == "global":
103 rep = "reference, global, "
104 elif self.ref_obj.name == "node":
105 rep = "reference, node, "
106 else:
107 rep = "reference, %s, " % str(self.ref_obj.label)
108 if self.has_comment:
109 return "%s%s%s # %s" % (rep, self.value, separator, self.comment)
110 else:
111 return "%s%s%s" % (rep, self.value, separator)
112
114 rep = "{Value: %s, Reference: %s}" % (self.value, self.ref_obj.name)
115 return rep
116
117
119 """An object that will collect the values of the parameters to save.
120 The name of those parameters are in the 'own_para_names' attribute
121 and will be set specifically for each object.
122 """
123
125 Record.__init__(self)
126 self.own_para_names += ["name"]
127 self.set_name(name)
128
129 self.has_comment = False
130 self.comment = None
131
132 self.label = 1
133 self.mbdyn_label = "1"
134
135 self.mbdyn_inst = None
136
137
138
139 self.arg_names = []
140
141 self.simulation = None
142
144 """Set the name of the object."""
145 self.name = name
146 self.mbdyn_name = keyarg_to_mbdyn(name)
147
149 """Set the label of the object, used by the MBDyn input file."""
150 self.label = label
151 self.mbdyn_label = CommonArgument(str(label))
152 self.mbdyn_label.set_comment(self.name)
153
159
161 """Return all the lines of the argument into a list,
162 used for writing the MBDyn input file."""
163 lines = []
164 for arg_name in self.arg_names[:-1]:
165 mbdyn_arg_name = get_mbdyn_name(arg_name)
166 mbdyn_arg = getattr(self, mbdyn_arg_name)
167 lines.append(mbdyn_arg.get_line())
168
169
170 mbdyn_arg_name = get_mbdyn_name(self.arg_names[-1])
171 mbdyn_arg = getattr(self, mbdyn_arg_name)
172 lines.append( mbdyn_arg.get_line(separator="") )
173 return lines
174
176 """Keep a reference of the simulation"""
177 self.simulation = simu
178
180 """Set a reference to the MBDyn instance, from the
181 L{mbdyn.bindings} under the C{mbdyn_inst} attribute.
182 However a general object can not get a MBDyn
183 instance by default. This step will be done according
184 to each group, labelling each object differently."""
185 pass
186
187
189 """This object will add a vector or a matrix attributes
190 to others MBDyn objects (reference, node, element).
191 The idea is to simplify the tedious work
192 of adding a vector or a matrix to each of the object.
193 For the attribute name given and the users parameters Python value
194 will be created as well as a MBDyn value.
195 """
196
198 self.mbdyn_equivalent = {"one": "1", "two": "2", "three": "3"}
199 self.diagonal_instances = [tuple, list, N.ndarray]
200
201 - def add_argument(self, obj, name, value, com=None, mbdyn_value=None):
202 """Add a common argument to the object"""
203 mbdyn_name = get_mbdyn_name(name)
204 setattr(obj, name, value)
205 if mbdyn_value != None:
206 setattr(obj, mbdyn_name, CommonArgument(mbdyn_value))
207 else:
208 setattr(obj, mbdyn_name, CommonArgument(value))
209 if com != None:
210 obj.__dict__[mbdyn_name].set_comment(com)
211
212 - def __set_attr(self, obj, mbdyn_obj, name, mbdyn_name, reference):
213 """The object is in that case a MBDyn object.
214 As a result, the attribute names follow the convention:
215 - 'value' for the value of that object in the Python world
216 - 'name' for the name of that object in the MBDyn input file
217 """
218 setattr(obj, name, mbdyn_obj.value)
219 geo_arg = GeometricArgument(mbdyn_obj.name, reference)
220 setattr(obj, mbdyn_name, geo_arg)
221
223 """Adding a vector to a MBDyn object
224 """
225
226
227 if kargs.has_key("ref"):
228 reference = kargs["ref"]
229 else:
230 reference = obj.ref
231 mbdyn_name = get_mbdyn_name(name)
232 if len(args)==3:
233 value = N.array([arg for arg in args])
234 setattr(obj, name, value)
235 mbdyn_value = ", ".join([str(arg) for arg in args])
236 vector = GeometricArgument(mbdyn_value, reference)
237 setattr(obj, mbdyn_name, vector)
238 elif len(args)==1:
239 mbdyn_obj = args[0]
240 mbdyn_obj_name = mbdyn_obj.__class__.__name__
241 if mbdyn_obj_name in VECTOR_NAMES:
242 self.__set_attr(obj, mbdyn_obj, name, mbdyn_name, reference)
243 if kargs.has_key("com"):
244 obj.__dict__[mbdyn_name].set_comment(kargs["com"])
245
247 """Adding a matrix to a MBDyn object
248 """
249 if kargs.has_key("ref"):
250 reference = kargs["ref"]
251 else:
252 reference = obj.ref
253 mbdyn_name = get_mbdyn_name(name)
254 if len(args)==9:
255 value = N.array([ [arg for arg in args[0:3]],
256 [arg for arg in args[3:6]],
257 [arg for arg in args[6:9]] ])
258 setattr(obj, name, value)
259 mbdyn_value = ", ".join([str(arg) for arg in args])
260 matrix = GeometricArgument(mbdyn_value, reference)
261 setattr(obj, mbdyn_name, matrix)
262 elif len(args)==1:
263 mbdyn_obj = args[0]
264 mbdyn_obj_name = mbdyn_obj.__class__.__name__
265 if mbdyn_obj_name in MATRIX_NAMES:
266 self.__set_attr(obj, mbdyn_obj, name, mbdyn_name, reference)
267 if kargs.has_key("diag"):
268 self._add_diagonal_matrix(obj, name, mbdyn_name,
269 kargs["diag"], reference)
270 if kargs.has_key("com"):
271 obj.__dict__[mbdyn_name].set_comment(kargs["com"])
272
274 """Add a diagonal matrix to the object"""
275 for reference_instance in self.diagonal_instances:
276 if isinstance(diagonal, reference_instance):
277 array = N.diag(diagonal)
278
279 setattr(obj, name, array)
280
281 string_args = []
282 for idx in range(array.shape[0]):
283 string_args.append(str(array[idx][idx]))
284 mbdyn_value = "diag, " + ", ".join(string_args)
285
286 mbdyn_arg = GeometricArgument(mbdyn_value, reference)
287 setattr(obj, mbdyn_name, mbdyn_arg)
288
290 """Method used for setting the orientation matrix
291 as a MBDyn input format.
292 """
293 mbdyn_value += "%s, " % self.mbdyn_equivalent[key]
294 if kargs[key] == "guess":
295 mbdyn_value += "guess"
296 elif type(kargs[key]) == tuple:
297 mbdyn_value += ", ".join([str(val) for val in kargs[key]])
298 else:
299 print "Unknow value '%s' for the key %s." % (str(kargs[key]), key)
300 return mbdyn_value
301
303 """Add an orientation matrix to the object.
304 """
305
306
307 if kargs.has_key("ref"):
308 reference = kargs["ref"]
309 else:
310 reference = obj.ref
311 mbdyn_name = get_mbdyn_name(name)
312
313 mbdyn_value = ""
314 if kargs.has_key("one"):
315 mbdyn_value = self._scan_orientation_key("one", kargs, mbdyn_value)
316 if kargs.has_key("two"):
317 if mbdyn_value != "":
318 mbdyn_value += ", "
319 mbdyn_value = self._scan_orientation_key("two", kargs, mbdyn_value)
320 if kargs.has_key("three"):
321 if mbdyn_value != "":
322 mbdyn_value += ", "
323 mbdyn_value = self._scan_orientation_key("three", kargs,
324 mbdyn_value)
325 if mbdyn_value != "":
326 mbdyn_arg = GeometricArgument(mbdyn_value, reference)
327 setattr(obj, name, mbdyn_arg.get_line())
328 setattr(obj, mbdyn_name, mbdyn_arg)
329
330 if len(args)==9:
331 value = N.array([ [arg for arg in args[0:3]],
332 [arg for arg in args[3:6]],
333 [arg for arg in args[6:9]] ])
334 setattr(obj, name, value)
335 mbdyn_value = ", ".join([str(arg) for arg in args])
336 matrix = GeometricArgument(mbdyn_value, reference)
337 setattr(obj, mbdyn_name, matrix)
338 elif len(args)==3:
339 print "It should be the Euler angle, not so hard to do"
340 elif len(args)==1:
341 mbdyn_obj = args[0]
342 mbdyn_obj_name = mbdyn_obj.__class__.__name__
343 if mbdyn_obj_name == EYE_NAME:
344 self.__set_attr(obj, mbdyn_obj, name, mbdyn_name, reference)
345 if kargs.has_key("com"):
346 obj.__dict__[mbdyn_name].set_comment(kargs["com"])
347
348
349 MANAGER = ManagerObject()
350
352 """The object for writing a block of arguments in the MBDyn input file
353 """
354
356 import sys
357 if sys.platform == "win32":
358 self.indent_char = ""
359 else:
360 self.indent_char = " "*4
361 self.indent_nb = 0
362 self.name = None
363 self.file = None
364
366 """Set the name of the block
367 """
368 self.name = name
369
371 """Indent the cursor of one more step."""
372 self.indent_nb += 1
373
375 """Dedent the cursor of a step."""
376 self.indent_nb -= 1
377
379 """Set the file on which the block will be written."""
380 self.file = file_to_write
381
382 - def begin(self, name=None):
383 """Start the block in the MBDyn way, with 'begin:'"""
384 block_name = self.name
385 if name != None:
386 block_name = name
387 self.write_line("begin: %s;" % block_name)
388 self.indent()
389
390 - def end(self, name=None):
391 """End the MBDyn block with a 'end;'"""
392 block_name = self.name
393 if name != None:
394 block_name = name
395 self.dedent()
396 self.write_line("end: %s;" % block_name)
397
399 """Write a line in the file with the right indentation
400 and the lineseparator of the operative system"""
401
402 self.file.write(self.indent_char*self.indent_nb + line + "\n")
403
405 """Write all the objects given in the list into the file,
406 manage the indentations"""
407 for obj in obj_list:
408 if obj.has_comment:
409 self.write_line("# %s" % obj.comment)
410 self.write_line("%s:" % obj.mbdyn_type)
411 self.indent()
412 for line in obj.get_lines():
413 self.write_line(line)
414 self.dedent()
415 self.write_line(";")
416