]> SALOME platform Git repositories - modules/parametric.git/blob - src/salome/parametric/study.py
Salome HOME
703f731679a083c1d2bdf5509cdf15bc2577f47d
[modules/parametric.git] / src / salome / parametric / study.py
1 # Copyright (C) 2012 EDF
2 #
3 # This file is part of SALOME PARAMETRIC module.
4 #
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.
9 #
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.
14 #
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/>.
17
18 import cPickle
19 import numpy
20
21 from salome.kernel.studyedit import getStudyEditor
22
23 # module constants
24 MODULE_NAME = "PARAMETRIC"
25
26 COMPONENT_NAME = "Parametric"
27 COMPONENT_ICON = "PARAMETRIC_small.png"
28
29 PARAM_STUDY_ICON = "param_study.png"
30 PARAM_STUDY_TYPE_ID = 1
31
32 class ParametricStudyEditor:
33   """
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.
37   """
38   def __init__(self, study_id = None):
39     self.editor = getStudyEditor(study_id)
40     self.param_comp = None
41
42   def find_or_create_param_component(self):
43     """
44     Find the component "Parametric" or create it if none is found
45     :return: the SComponent found or created.
46     """
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
50
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)
55
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)
59
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)
66
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
73     return param_study
74
75
76 class ParametricVariable:
77
78   def __init__(self, name, minval = None, maxval = None, step = None):
79     self.name = name
80     self.min = minval
81     self.max = maxval
82     self.step = step
83
84
85 class ParametricStudy:
86   
87   SALOME_COMPONENT = 0
88   PYTHON_SCRIPT = 1
89
90   def __init__(self):
91     self.input_vars = []
92     self.output_vars = []
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
97     self.name = None
98     self.nb_parallel_computations = 1
99     self.data = None
100     self.datasize = 0
101     self._value_dict = None
102     self.entry = None
103
104   def add_input_variable(self, var):
105     self.input_vars.append(var)
106
107   def add_output_variable(self, varname):
108     self.output_vars.append(varname)
109
110   def generate_data(self):
111     self.data = {}
112     self.datasize = 0
113     for var in self.input_vars:
114       self.data[var.name] = []
115     self._value_dict = {}
116     self._fill_data(self.input_vars)
117
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)
122       self.datasize += 1
123     else:
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)
129
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")
134     
135     # Header
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")
141     
142     # Data
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")
149
150     f.close()
151
152   def _format_value(self, value):
153     if value is None:
154       val = ""
155     else:
156       val = unicode(value)
157     return val.encode("utf-8")