Package mbdyn :: Module groups
[hide private]

Source Code for Module mbdyn.groups

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # This file is part of MBDyn sim suite. 
  5  # Copyright (C) 2007 André ESPAZE, as part of a Master thesis supervised by 
  6  # Martin O.L.Hansen (DTU) and Nicolas Chauvat (Logilab) 
  7   
  8  # MBDyn sim suite is free software; you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public License as published by 
 10  # the Free Software Foundation; either version 2 of the License, or 
 11  # (at your option) any later version. 
 12  # 
 13  # This program is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU General Public License 
 19  # along with this program; if not, write to the Free Software 
 20  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 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   
71 -class ItemGroup(ListType, BasicObject):
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
77 - def __init__(self, name):
78 ListType.__init__(self) 79 BasicObject.__init__(self, name) 80 self.labels = [] 81 self.names = [] 82 self.number = 0
83
84 - def add_item(self, item):
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
91 - def get_number(self):
92 """Return the number of items in the list""" 93 return self.number
94
95 - def set_labels(self):
96 """Save the labels on all the item contained into the list""" 97 for item in self: 98 self.labels.append(item.label)
99
100 - def get_from_label(self, label):
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
116 - def set_labels_auto(self, offset=1):
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
128 - def _is_more_than_one_time(self, name):
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
137 - def set_names_auto(self):
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
157 -class NodeGroup(ItemGroup):
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
162 - def __init__(self, mbdyn_key):
163 self.key = mbdyn_key 164 self.attr_name = ATTRIBUTES["NODE"][mbdyn_key] 165 ItemGroup.__init__(self, self.attr_name)
166
167 - def add_node(self, node):
168 """Add a node to the group, see 'add_item'""" 169 self.add_item(node)
170 171
172 -class ElementGroup(ItemGroup):
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
177 - def __init__(self, mbdyn_key):
178 self.key = mbdyn_key 179 self.attr_name = ATTRIBUTES["ELEM"][mbdyn_key] 180 ItemGroup.__init__(self, self.attr_name)
181
182 - def add_element(self, elt):
183 """Add an element to the group, see 'add_item'""" 184 self.add_item(elt)
185 186
187 -class ReferenceList(ItemGroup):
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
193 - def __init__(self):
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
204 - def write_into(self, file_to_write):
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
209 - def collect_parameters(self):
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
218 - def set_parameters(self, para):
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
229 -class CommonContainer(BasicObject):
230 """A base class for L{Nodes} and L{Elements}. The container 231 has groups of nodes and elements as attributes.""" 232
233 - def __init__(self, name):
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
242 - def add_group(self, group):
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
249 - def get_number(self):
250 """Return the number of groups""" 251 return self.number
252
253 - def init_results(self):
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
259 - def write_into(self, file_to_write):
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
267 - def __repr__(self):
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
287 - def __init__(self):
288 CommonContainer.__init__(self, "nodes") 289 self.node_paras = [] 290 self.own_para_names += ["node_paras"]
291
292 - def add(self, node):
293 """Add a node to the global list, 'all', and 294 create the node group if it does not exist.""" 295 self.all.append(node) 296 if node.group_key not in self.groups.keys(): 297 group = NodeGroup(node.group_key) 298 group.add_node(node) 299 self.add_group(group) 300 else: 301 self.groups[node.group_key].add_node(node) 302 self.number += 1
303
304 - def collect_parameters(self):
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
313 - def set_parameters(self, para):
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
326 -class Elements(CommonContainer):
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
334 - def __init__(self):
335 CommonContainer.__init__(self, "elements") 336 self.elt_paras = [] 337 self.own_para_names += ["elt_paras"]
338
339 - def add(self, elt):
340 """Add an element to the global list, C{all}, and 341 create the element group if it does not exist.""" 342 self.all.append(elt) 343 if elt.group_key not in self.groups.keys(): 344 group = ElementGroup(elt.group_key) 345 group.add_element(elt) 346 self.add_group(group) 347 else: 348 self.groups[elt.group_key].add_element(elt) 349 self.number += 1
350
351 - def collect_parameters(self):
352 """Collect the parameters to save on all the elements""" 353 self.collect_own_parameters() 354 for elt in self.all: 355 elt.collect_parameters() 356 self.elt_paras.append(elt.para) 357 self.para["elt_paras"] = self.elt_paras
358
359 - def set_parameters(self, para):
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
370 - def set_node_connectivity(self):
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