Package mbdyn :: Module quantity
[hide private]

Source Code for Module mbdyn.quantity

  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 module for saving attributes during the simulation. The L{Results} 
 23  class is then used by the C{Record} object, in L{mbdyn.record}. The 
 24  L{Results} instance can be accessed by the C{results} or C{res} 
 25  attribute of a MBDyn object from which the results of the 
 26  simulation can be accessed. 
 27  """ 
 28  from types import ListType 
 29   
30 -class CommonList(ListType):
31 """A list that can be cleaned. 32 An abstract class used by L{Result} and L{Results}. 33 """ 34
35 - def clean(self):
36 """Clean the list""" 37 for idx in range(len(self)): 38 item = self.pop() 39 del item
40 41
42 -class Result(CommonList):
43 """A list of results for a particular object attribute. 44 45 For example: C{node.results.position} is a C{Result} instance, 46 that contains the position at each time step. 47 """ 48
49 - def __init__(self, attr_name):
50 CommonList.__init__(self) 51 self.attr_name = attr_name
52 53
54 -class Results(CommonList):
55 """All the possible results that will be saved during 56 the simulation. This class is attribute of almost all the MDByn objects. 57 58 This is an interface to the user that decides what he 59 would like to save for every object. The instance of this class is for 60 example accessed by: C{node.results}""" 61
62 - def __init__(self):
63 CommonList.__init__(self) 64 self.names = []
65
66 - def add(self, attr_name):
67 """Add the attribute, as a string, that needs to be saved 68 during the simulation. It will create a new C{Result} instance""" 69 if attr_name not in self.names: 70 result = Result(attr_name) 71 self.append(result) 72 self.names.append(attr_name)
73
74 - def init(self):
75 """Initialize the quantities and set them as attributes""" 76 for result in self: 77 result.clean() 78 setattr(self, result.attr_name, result)
79
80 - def __repr__(self):
81 """Return the representation of all the C{Result} attributes""" 82 string = "[" 83 if len(self) > 0: 84 for name in self.names[:-1]: 85 string += name + ",\n" 86 string += self.names[-1] 87 string += "]" 88 return string
89 90 91
92 -def get_label_name(name):
93 """A function used for plotting (likely to change because 94 not used any more)""" 95 return name[0].capitalize() + name[1:]
96 97 98 EULER_DESC = "Euler angle number %(key)s [%(unit)s]" 99 # This list contains all the MBDYN quantities for describing the objects 100 QUANTITIES_DESCRIPTION = {\ 101 # Name, unit, plotting keys, plotting labels 102 # (the default is: "%(name)s along %(key)s [%(unit)s]" 103 "position" : ["m", ("x", "y", "z")], 104 "euler" : ["rad", ("1", "2", "3"), EULER_DESC], 105 "velocity" : ["m/s", ("x", "y", "z")], 106 "angular_velocity" : ["rad/s", ("x", "y", "z")], 107 "value" : ["N", ("x", "y", "z")] 108 } 109 110
111 -class Quantity3D:
112 """A physical quantity having values along 3 axis. 113 This class is currently not used. 114 """ 115
116 - def __init__(self, name):
117 self.name = name 118 desc = QUANTITIES_DESCRIPTION[name] 119 self.unit = desc[0] 120 self.direction_labels = [label for label in desc[1]] 121 self.direction_list = [[], [], []] 122 if len(desc) == 3: 123 self.label_pattern = desc[2] 124 else: 125 self.label_pattern = "%(name)s along %(key)s [%(unit)s]" 126 self.label_dic = {"name": get_label_name(name), "unit": self.unit}
127
128 - def __getitem__(self, direction_label):
129 indice = self.direction_labels.index( direction_label ) 130 return self.direction_list[indice]
131
132 - def get_label(self, direction_label):
133 """Return the label of the quantity. Used for plotting. 134 """ 135 self.label_dic["key"] = direction_label 136 return self.label_pattern % self.label_dic
137