Package mbdyn :: Module references
[hide private]

Source Code for Module mbdyn.references

  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  """Module interfacing the MBDyn reference frames. The main object 
 23  defined is L{ReferenceFrame}. The others object used are 
 24  C{GLOBAL_REF} and C{NONE_REF} (an abstract referential that does 
 25  not write anything in the MBDyn input file -useful 
 26  for some of the quantities, like the mass of a body-). 
 27  """ 
 28  from mbdyn.general import NULL, EYE 
 29  from mbdyn.common import BasicObject, MANAGER 
 30   
 31   
32 -class BasicReference(BasicObject):
33 """The most basic reference frame object. 34 """ 35
36 - def __init__(self, name):
37 BasicObject.__init__(self, name) 38 self.mbdyn_representation = "reference, global, "
39
40 - def to_mbdyn(self):
41 """Return the representation, still not used""" 42 return self.mbdyn_representation
43 44 45 GLOBAL_REF = BasicReference("global") 46 NODE_REF = BasicReference("node") 47 48
49 -class ReferenceFrame(BasicReference):
50 """The reference frame interface. When describing a simulation, 51 the first instance is usually a ReferenceFrame because this is 52 a construction object. 53 54 In MBDyn, the reference frames are used to describe the initial 55 conditions of the problem. They are not going to be used for 56 the resolution. So this object exists only at the beginning of 57 the simulation and its values are saved when L{Simulation} calls 58 the method L{store_mbdyn_data}. 59 60 The reference frames are usually linked. A new reference frame is 61 described relative to another one by the L{set_relative_from} method. 62 The properties of one reference frame is inherited to all the reference 63 frames that are linked to. This feature is for example very useful 64 to give an initial rotational speed to many other reference frames, 65 and thus to the nodes attached to them. The drawback is that a mistake 66 done on a base reference frame is propagated to all the other ones. 67 A common mistake is a wrong orientation matrix. 68 69 By default a reference is frame is relative to the absolute referential, 70 with a null position, a identity rotation matrix, a null velocity and 71 a null angular velocity. However, it a relative reference frame is used, 72 it is important to call L{set_relative_from} just after the instance 73 creation (otherwise the default values will be applied relative to the 74 global reference frame - a default to fix). 75 76 Example of use:: 77 78 import numpy as N 79 80 angle = N.pi/3. 81 ref1 = ReferenceFrame() 82 ref1.set_orientation_matrix(one=(N.cos(angle), 83 N.sin(angle), 84 0.), 85 three=(0., 0., 1.)) 86 87 ref2 = ReferenceFrame() 88 ref2.set_relative_from(ref1) 89 ref2.set_position(10., 0., 0.) 90 91 ref3 = ReferenceFrame() 92 ref3.set_relative_from(ref2) 93 ref3.set_angular_velocity(0., 0., 2.2) 94 95 The reference frame C{ref1} is turn of a M{S{pi}/3} angle along the M{z} axis. 96 The reference frame C{ref2} follows this orientation and is placed at M{10 m} 97 from the origin. However, but this feature is provided by MBDyn, 98 its value in the absolute reference frame is: 99 M{(10. cos(S{pi}/3), 10. sin(S{pi}/3), 0.)}. C{ref3} gets a angular velocity 100 of M{2.2 rad/s} along the M{z} axis and is at 101 the position of C{ref2}. This kinematic property is going to be transfered to 102 any node attached to this reference frame. The reference frame can also be 103 used to enter element property, but the kinematic properties are in that 104 case not relevant as only the nodes introduced kinematic degrees 105 of freedom. 106 107 At the end, the MBDyn results are always output in the absolute 108 reference frame, from which the integration is performed. However, 109 transformations in a particular reference frame can be easily done by 110 using L{node<mbdyn.nodes.StructuralNode>} 111 rotation matrix that are updated at each iteration. 112 """ 113
114 - def __init__(self, name="reference"):
115 BasicReference.__init__(self, name) 116 #self.class_name = "reference" 117 self.mbdyn_type = "reference" 118 self.ref = GLOBAL_REF 119 120 self.own_para_names += ["label"] 121 122 self.arg_names = ["label", "rel_position", "rel_orientation_matrix", 123 "rel_velocity", "rel_angular_velocity"] 124 125 # Those attributes are in the absolute reference frame 126 self.position = None 127 self.orientation_matrix = None 128 self.velocity = None 129 self.angular_velocity = None 130 131 self.will_save("position", 132 "orientation_matrix") 133 self.set_default()
134
135 - def set_default(self):
136 """Set the reference frame default. It means a null position, 137 a identity rotation matrix, a null velocity and and null 138 angular velocity relative to its reference frame""" 139 self.set_position(NULL) 140 self.set_orientation_matrix(EYE) 141 self.set_velocity(NULL) 142 self.set_angular_velocity(NULL)
143
144 - def set_relative_from(self, ref):
145 """Set the relative reference frame. All the values entered 146 will be relative to the given reference frame.""" 147 self.ref = ref 148 self.set_default()
149
150 - def set_to_none(self):
151 """No referential and no prefix for the MBDyn input file""" 152 self.mbdyn_representation = "" 153 self.name = "None"
154
155 - def set_position(self, *args, **kargs):
156 """Set the position relative to its reference frame.""" 157 MANAGER.add_vector(self, "rel_position", args, kargs)
158
159 - def set_orientation_matrix(self, *args, **kargs):
160 """Set the orientation matrix relative to its reference frame""" 161 MANAGER.add_orientation_matrix(self, 162 "rel_orientation_matrix", args, kargs)
163
164 - def set_velocity(self, *args, **kargs):
165 """Set the velocity relative to its reference frame""" 166 MANAGER.add_vector(self, "rel_velocity", args, kargs)
167
168 - def set_angular_velocity(self, *args, **kargs):
169 """Set the initial angular velocity relative to its reference frame""" 170 MANAGER.add_vector(self, "rel_angular_velocity", args, kargs)
171
172 - def get_mbdyn_instance(self):
173 """Get the MBDyn instance created by WraptMBDyn. """ 174 mbdyn_frames = self.simulation.wrapt_mbdyn.frames 175 self.mbdyn_inst = mbdyn_frames.get_from_label(self.label)
176
177 - def _init_results(self):
178 """Initialize the results and set the callbacks for 179 getting MBDyn values.""" 180 for attr in self.results.names: 181 self.saving_actions[attr] = getattr(self.mbdyn_inst, 182 "get_" + attr) 183 self.has_saving_actions = True 184 self.common_init_results()
185
186 - def store_mbdyn_data(self):
187 """Saved the initial values converted in the absolute reference frame 188 by MBDyn. By default, only the absolute position and absolute 189 orientation matrix are saved. The absolute velocity and 190 absolute angular velocity can be added by:: 191 192 will_save("velocity", 193 "angular_velocity") 194 195 """ 196 self._init_results() 197 self.save_results()
198 199 200 # Definition of the referential without prefix for the MBDyn input file 201 NONE_REF = ReferenceFrame() 202 NONE_REF.set_to_none() 203