]> SALOME platform Git repositories - modules/kernel.git/blob - src/KERNEL_PY/kernel/parametric/pyscript_utils.py
Salome HOME
Merge from V6_main 28/02/2013
[modules/kernel.git] / src / KERNEL_PY / kernel / parametric / pyscript_utils.py
1 # Copyright (C) 2011-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """
21 This module provides utility functions for the computation codes intended for
22 use in parametric studies. The computation codes must be implemented as a
23 Python script or function to use these functions. If the computation code is
24 implemented as a SALOME component, use module
25 :mod:`salome.kernel.parametric.compo_utils` instead. 
26 """
27
28 def create_input_dict(deterministic_dict, parametric_input):
29     """
30     This function returns a dictionary containing the input values to be used
31     in the computation code.
32     
33     :type  deterministic_dict: dict
34     :param deterministic_dict: dictionary containing the fixed values (i.e.
35                                non-parametric). This dictionary can be empty
36                                if all variables are parametric.
37
38     :type  parametric_input: dict
39     :param parametric_input: dictionary containing the description and values
40                              of the parametric variables.
41
42     :return: a dictionary containing the input values for the computation code.
43     """
44     # Simply get the first point from input (no time series, single observation)
45     input_point = parametric_input["inputValues"][0][0]
46
47     if len(input_point) != len(parametric_input["inputVarList"]):
48         raise Exception("Size mismatch between inputVarList and point to evaluate")
49
50     input_dict = deterministic_dict.copy()
51     for i in range(len(input_point)):
52         input_dict[parametric_input["inputVarList"][i]] = input_point[i][0]
53
54     return input_dict
55
56 def create_normal_parametric_output(output_dict, parametric_input):
57     """
58     This function returns a dictionary describing the output of the
59     computation code in parametric studies.
60     
61     :type  output_dict: dict
62     :param output_dict: dictionary containing the output values of the
63                         computation code (the keys are the variable names, the
64                         values are the variable values).
65
66     :type  parametric_input: dict
67     :param parametric_input: dictionary containing the description and values
68                              of the parametric variables.
69
70     :return: a dictionary containing the representation of the output of the
71              computation code.
72     """
73     output_values = [[[]]]
74     for output_var in parametric_input["outputVarList"]:
75         output_values[0][0].append([output_dict[output_var]])
76     return {"outputValues" : output_values,
77             "specificOutputInfos" : [],
78             "returnCode" : 0,
79             "errorMessage" : ""}
80
81 def create_error_parametric_output(error_message):
82     """
83     This function returns a dictionary describing the output of the
84     computation code in parametric studies in case of error.
85     
86     :type  error_message: string
87     :param error_message: the error message.
88
89     :return: a dictionary describing the error.
90     """
91     return {"outputValues" : [],
92             "specificOutputInfos" : [],
93             "returnCode" : 1,
94             "errorMessage" : error_message}