1 # Copyright (C) 2012 EDF
3 # This file is part of SALOME PARAMETRIC module.
5 # SALOME PARAMETRIC module is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # SALOME PARAMETRIC module is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with SALOME PARAMETRIC module. If not, see <http://www.gnu.org/licenses/>.
21 from salome.kernel.studyedit import getStudyEditor
24 MODULE_NAME = "PARAMETRIC"
26 COMPONENT_NAME = "Parametric"
27 COMPONENT_ICON = "PARAMETRIC_small.png"
29 PARAM_STUDY_ICON = "param_study.png"
30 PARAM_STUDY_TYPE_ID = 1
32 class ParametricStudyEditor:
34 This class provides utility methods to edit the component "Parametric" in
35 the study. The parameter `studyId` defines the ID of the study to edit. If
36 it is :const:`None`, the edited study will be the current study.
38 def __init__(self, study_id = None):
39 self.editor = getStudyEditor(study_id)
40 self.param_comp = None
42 def find_or_create_param_component(self):
44 Find the component "Parametric" or create it if none is found
45 :return: the SComponent found or created.
47 if self.param_comp is None:
48 self.param_comp = self.editor.findOrCreateComponent(MODULE_NAME, COMPONENT_NAME, COMPONENT_ICON)
49 return self.param_comp
51 def add_parametric_study(self, parametric_study):
52 self.find_or_create_param_component()
53 sobj = self.editor.createItem(self.param_comp, "__NEW_STUDY__")
54 self._set_sobj(parametric_study, sobj)
56 def set_parametric_study_at_entry(self, parametric_study, entry):
57 sobj = self.editor.study.FindObjectID(entry)
58 self._set_sobj(parametric_study, sobj)
60 def _set_sobj(self, parametric_study, sobj):
61 self.editor.setItem(sobj,
62 name = parametric_study.name,
63 comment = cPickle.dumps(parametric_study),
64 icon = PARAM_STUDY_ICON,
65 typeId = PARAM_STUDY_TYPE_ID)
67 def get_parametric_study(self, entry):
68 sobj = self.editor.study.FindObjectID(entry)
69 if sobj is None or self.editor.getTypeId(sobj) != PARAM_STUDY_TYPE_ID:
70 raise Exception("No valid parametric study at entry %s" % entry)
71 param_study = cPickle.loads(sobj.GetComment())
72 param_study.entry = entry
76 class ParametricVariable:
78 def __init__(self, name, minval = None, maxval = None, step = None):
85 class ParametricStudy:
93 self.solver_code_type = ParametricStudy.SALOME_COMPONENT
94 self.salome_component_name = None
95 self.solver_case_entry = None
96 self.python_script = None
98 self.nb_parallel_computations = 1
101 self._value_dict = None
104 def add_input_variable(self, var):
105 self.input_vars.append(var)
107 def add_output_variable(self, varname):
108 self.output_vars.append(varname)
110 def generate_data(self):
113 for var in self.input_vars:
114 self.data[var.name] = []
115 self._value_dict = {}
116 self._fill_data(self.input_vars)
118 def _fill_data(self, remaining_var_list):
119 if len(remaining_var_list) == 0:
120 for (name, value) in self._value_dict.iteritems():
121 self.data[name].append(value)
124 var = remaining_var_list[0]
125 next_var_list = remaining_var_list[1:]
126 for value in numpy.arange(var.min, var.max, var.step):
127 self._value_dict[var.name] = value
128 self._fill_data(next_var_list)
130 def export_data_to_csv_file(self, filepath, sep = ","):
131 if self.data is None:
132 raise Exception("Parametric study does not contain any data")
133 f = open(filepath, "w")
136 for invar in self.input_vars:
137 f.write(invar.name + sep)
138 for outvarname in self.output_vars:
139 f.write(outvarname + sep)
140 f.write("Error message\n")
143 for i in range(self.datasize):
144 for invar in self.input_vars:
145 f.write(self._format_value(self.data[invar.name][i]) + sep)
146 for outvarname in self.output_vars:
147 f.write(self._format_value(self.data[outvarname][i]) + sep)
148 f.write(self._format_value(self.data["__ERROR_MESSAGE__"][i]) + "\n")
152 def _format_value(self, value):
157 return val.encode("utf-8")