Package mbdyn :: Module control_data
[hide private]

Source Code for Module mbdyn.control_data

  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 for writing the MBDyn control data. 
 23  """ 
 24  from mbdyn.common import Block 
 25   
 26   
27 -class ControlData:
28 """The control data of MBDyn, generated automatically by 29 the nodes and elements information or the user's object. 30 """
31 - def __init__(self):
32 self.objects = [] 33 self.name = "control data" 34 self.block = Block() 35 self.category = {} 36 self.obj_list = {} 37 self.extra_arguments = []
38
39 - def add(self, obj):
40 """Add an object to control data""" 41 self.objects.append(obj)
42
43 - def add_extra_argument(self, arg):
44 """Add an extra argument not yet managed by the interface, 45 it will be directly printed in the input file""" 46 self.extra_arguments.append(arg)
47
48 - def _sort_control_data(self):
49 """Create the objects lists with the control data with values 50 (for example 'beams: +1;' in MBDyn) and control data without values 51 (for example 'gravity;'). 52 53 Additionaly, the control data with values will need to gather all the 54 items that are in the same category. 55 (For example:: 56 57 beams: 58 +2 # Tower 59 +1 # Nacelle 60 ; 61 ) 62 A list for each category is here created. (Note: each category from 63 the user's model, not from all the MBDyn categories) 64 """ 65 self.category = {"with_values" : {}, "without_values" : {}} 66 self.obj_list = {"with_values" : [], "without_values" : []} 67 for obj in self.objects: 68 if hasattr(obj, "control_data_with_values"): 69 for control_data in obj.control_data_with_values: 70 category_name = control_data[0] 71 self.category["with_values"][category_name] = [] 72 self.obj_list["with_values"].append(obj) 73 if hasattr(obj, "control_data_without_values"): 74 self.obj_list["without_values"].append(obj)
75
76 - def _fill_category(self):
77 """The objects having C{control_data_with_values} fill the dictionary 78 C{category["with_values"]} according to the category name. All their 79 MBDyn value are saved into a list for the input file writing. 80 81 The objects having C{control_data_without_values} just need 82 to give their category name, they do not have any value. However the 83 user may have different objects having the same control data without 84 value, for example the C{AirProperties}. This property 85 must not be duplicated in the input file. For consistency, the 86 C{self.category["without_values"][category_name]} is set to 1 but never 87 used, just the information on the C{category_name} is important. 88 """ 89 for obj in self.obj_list["with_values"]: 90 for category_name, value in obj.control_data_with_values: 91 self.category["with_values"][category_name].append(value) 92 for obj in self.obj_list["without_values"]: 93 for category_name in obj.control_data_without_values: 94 self.category["without_values"][category_name] = 1
95
96 - def write_into(self, file_to_write):
97 """Write the control data block into the MBDyn input file""" 98 self._sort_control_data() 99 self._fill_category() 100 self.block.set_file(file_to_write) 101 self.block.set_name(self.name) 102 self.block.begin() 103 104 for category_name in self.category["with_values"].keys(): 105 self.block.write_line(category_name + ":") 106 self.block.indent() 107 for value in self.category["with_values"][category_name]: 108 self.block.write_line(value) 109 self.block.dedent() 110 self.block.write_line(";") 111 112 for category_name in self.category["without_values"].keys(): 113 self.block.write_line(category_name + ";") 114 for arg in self.extra_arguments: 115 self.block.write_line(arg + ";") 116 self.block.end()
117