1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """The interface to the groups from L{mbdyn.bindings.groups}.
23 The motivation is to have groups for the Python interface, independent from
24 the L{WraptMBDyn} that gets labels from the input file. The main work of those
25 groups is to keep track of the object references when they are added
26 from the L{Simulation<mbdyn.main.Simulation>}.
27 They also label the items before the input file writing.
28 """
29 from types import ListType
30 import numpy as N
31
32 from mbdyn.common import BasicObject, Block
33 from mbdyn.nodes import NODE_CLASS
34 from mbdyn.elements import ELEM_CLASS
35 from mbdyn.references import ReferenceFrame
36
37 ATTRIBUTES = {}
38
39 ATTRIBUTES["NODE"] = {
40 "ABSTRACT" : "abstracts",
41 "STRUCTURAL" : "structurals",
42 "ELECTRIC" : "electrics",
43 "PARAMETER" : "parameters",
44 "HYDRAULIC" : "hydraulics"
45 }
46
47 ATTRIBUTES["ELEM"] = {
48 "ROTOR" : "rotors",
49 "AUTOMATICSTRUCTURAL" : "automatic_structurals",
50 "GRAVITY" : "gravities",
51 "BODY" : "bodies",
52 "JOINT" : "joints",
53 "BEAM" : "beams",
54 "PLATE" : "plates",
55 "FORCE" : "forces",
56 "ELECTRICBULK" : "electric_bulks",
57 "ELECTRIC" : "electrics",
58 "HYDRAULIC" : "hydraulics",
59 "BULK" : "bulks",
60 "LOADABLE" : "loadables",
61 "DRIVEN" : "drivens",
62 "EXTERNAL" : "externals",
63 "AIRPROPERTIES" : "air_properties",
64 "AEROMODAL" : "aeromodals",
65 "AERODYNAMIC" : "aerodynamics",
66 "GENEL" : "genels",
67 "SOCKETSTREAM_OUTPUT" : "socketstream_outputs"
68 }
69
70
72 """A general interface for a Node group or an Elem group.
73 The C{mbdyn_key} is the same key as used in MBDyn, this is
74 the name of the group in capital letters (FORCE, BEAM and so on).
75 """
76
83
85 """Add an item to the list and keep track of its name"""
86 self.append(item)
87 self.labels.append(item.label)
88 self.names.append(item.name)
89 self.number += 1
90
92 """Return the number of items in the list"""
93 return self.number
94
96 """Save the labels on all the item contained into the list"""
97 for item in self:
98 self.labels.append(item.label)
99
101 """Return the node according to its label"""
102 try:
103 return self[self.labels.index(label)]
104 except ValueError:
105 print "'%s' not found in list" % label
106 return None
107
108 - def get(self, name):
109 """Return a node from its name"""
110 try:
111 return self[self.names.index(name)]
112 except ValueError:
113 print "'%s' not found in list" % name
114 return None
115
117 """Set automatically the labels for MBDyn.
118 This function is only used when the user is writing
119 the MBDyn input file from Python.
120 """
121 self.labels = []
122 counter = offset
123 for item in self:
124 item.set_label(counter)
125 self.labels.append(counter)
126 counter += 1
127
129 """Function only used for naming
130 (or renaming) the nodes and elements"""
131 array_result = N.where(self.array_names == name)
132 if len(array_result) == 1:
133 return False
134 else:
135 return True
136
138 """Set automatically the names. It
139 may change the ones of the user if the name
140 is present more than one time in the list.
141 This function is not safe, the name could be renamed
142 to an existing one but the names are not important
143 for the package work.
144 """
145 counter = 1
146 self.array_names = N.array(self.names)
147 for item in self:
148 if self._is_more_than_one_time(item.name):
149 new_name = item.name + str(counter)
150 nid = self.names.index(item.name)
151 self.names.remove(item.name)
152 self.names.insert(nid, new_name)
153 item.set_name(new_name)
154 counter += 1
155
156
158 """A group (or list) of nodes. It will be set on the L{Nodes} container,
159 attribute of the Simulation, according to its C{attr_name},
160 the attribute name defined at the beginning of the module."""
161
166
168 """Add a node to the group, see 'add_item'"""
169 self.add_item(node)
170
171
173 """A group (or list) of elements. The same description applied
174 as L{NodeGroup}, the instance will belong to the L{Elements}
175 container."""
176
181
183 """Add an element to the group, see 'add_item'"""
184 self.add_item(elt)
185
186
188 """A group (or list) of reference frames. As the reference frames
189 just have a single class definition, their container is also
190 simpler.
191 """
192
194 ItemGroup.__init__(self, "references")
195 self.block = Block()
196 self.ref_paras = []
197 self.own_para_names += ["ref_paras"]
198
199 - def add(self, reference):
200 """Add a reference frame to the list of reference,
201 see C{add_item} of L{ItemGroup}."""
202 self.add_item(reference)
203
205 """Write the reference block into the MBDyn input file"""
206 self.block.set_file(file_to_write)
207 self.block.write_mbdyn_objects(self)
208
210 """Collect the parameters of C{own_para_names} that need to be
211 saved (or pickled) in the result file"""
212 self.collect_own_parameters()
213 for ref in self:
214 ref.collect_parameters()
215 self.ref_paras.append(ref.para)
216 self.para["ref_paras"] = self.ref_paras
217
219 """Use the values from para to retrieved the reference frames
220 when the simulation is loaded from a file.
221 """
222 self.set_own_parameters(para)
223 for ref_para in self.ref_paras:
224 ref = ReferenceFrame()
225 ref.set_parameters(ref_para)
226 self.add(ref)
227
228
230 """A base class for L{Nodes} and L{Elements}. The container
231 has groups of nodes and elements as attributes."""
232
234 BasicObject.__init__(self, name)
235 self.block = Block()
236 self.all = []
237 self.groups = {}
238 self.groups_paras = []
239 self.number = 0
240 self.own_para_names += ["groups_paras", "number"]
241
243 """Add a group of node or element. This group
244 will set as attribute."""
245 setattr(self, group.attr_name, group)
246 self.groups_paras.append(group.para)
247 self.groups[group.key] = group
248
250 """Return the number of groups"""
251 return self.number
252
254 """Initialize the results of all the items contained
255 in all the groups."""
256 for item in self.all:
257 item.init_results()
258
260 """Write the node or element block into the MBDyn input file"""
261 self.block.set_file(file_to_write)
262 self.block.set_name(self.name)
263 self.block.begin()
264 self.block.write_mbdyn_objects(self.all)
265 self.block.end()
266
268 """Return the representation of the container,
269 with all the groups"""
270 string = "["
271 groups = self.groups.values()
272 if len(groups) > 0:
273 for group in groups[:-1]:
274 string += group.attr_name + ",\n"
275 string += groups[-1].attr_name
276 string += "]"
277 return string
278
279
280 -class Nodes(CommonContainer):
281 """The nodes of the simulation. The instance of that class
282 will be the C{nodes} attribute of the
283 L{Simulation<mbdyn.main.Simulation>} and will
284 gather all the node groups.
285 """
286
291
292 - def add(self, node):
303
305 """Collect all the parameters of the nodes before
306 to save them."""
307 self.collect_own_parameters()
308 for node in self.all:
309 node.collect_parameters()
310 self.node_paras.append(node.para)
311 self.para["node_paras"] = self.node_paras
312
314 """Set all the parameters according to the dictionary para received.
315 The nodes are created from their 'group_key' and 'class_key',
316 in the NODE_CLASS dictionary."""
317 self.set_own_parameters(para)
318 for node_para in self.node_paras:
319 NodeClass = NODE_CLASS[node_para["group_key"]]\
320 [node_para["class_key"]]
321 node = NodeClass()
322 node.set_parameters(node_para)
323 self.add(node)
324
325
327 """The elements of the Simulation. The instance of that class
328 will be the C{elts} attribute of the
329 L{Simulation<mbdyn.main.Simulation>}. All the elements
330 are contained there in the C{all} attribute
331 and this class gather all the element groups.
332 """
333
338
339 - def add(self, elt):
350
358
360 """Set the parameters according to the dictionary para received.
361 The elements are created there from the C{ELEM_CLASS} dictionary."""
362 self.set_own_parameters(para)
363 for elt_para in self.elt_paras:
364 ElemClass = ELEM_CLASS[elt_para["group_key"]]\
365 [elt_para["class_key"]]
366 elt = ElemClass()
367 elt.set_parameters(elt_para)
368 self.add(elt)
369
371 """Set the node label (or index) to the element attributes."""
372 for group in self.groups.values():
373 for elt in group:
374 elt.set_node_label()
375