Salome HOME
a76649c8b2790c9deb731bde7059fcc3a903dd95
[modules/kernel.git] / src / KERNEL_PY / kernel / parametric / pyscript_utils.py
1 # Copyright (C) 2011-2024  CEA, EDF, 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, or (at your option) any later version.
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 ## \defgroup pyscript_utils pyscript_utils
21 #  \{ 
22 #  \details
23 #  This module provides utility functions for the computation codes intended for
24 #  use in parametric studies. The computation codes must be implemented as a
25 #  Python script or function to use these functions. If the computation code is
26 #  implemented as a %SALOME component, use module 
27 #  <a href="group__compo__utils.html">salome.kernel.parametric.compo_utils</a> instead. 
28 #  \}
29
30 """
31 This module provides utility functions for the computation codes intended for
32 use in parametric studies. The computation codes must be implemented as a
33 Python script or function to use these functions. If the computation code is
34 implemented as a SALOME component, use module
35 :mod:`salome.kernel.parametric.compo_utils` instead. 
36 """
37
38 ## This function returns a dictionary containing the input values to be used
39 #  in the computation code.
40 #
41 #  \param deterministic_dict (dict) dictionary containing the fixed values (i.e.
42 #  non-parametric). This dictionary can be empty if all variables are parametric.
43 #
44 #  \param parametric_input (dict) dictionary containing the description and values
45 #  of the parametric variables.
46 #
47 #  \return a dictionary containing the input values for the computation code.
48 #  \ingroup pyscript_utils
49 def create_input_dict(deterministic_dict, parametric_input):
50     """
51     This function returns a dictionary containing the input values to be used
52     in the computation code.
53     
54     :type  deterministic_dict: dict
55     :param deterministic_dict: dictionary containing the fixed values (i.e.
56                                non-parametric). This dictionary can be empty
57                                if all variables are parametric.
58
59     :type  parametric_input: dict
60     :param parametric_input: dictionary containing the description and values
61                              of the parametric variables.
62
63     :return: a dictionary containing the input values for the computation code.
64     """
65     # Simply get the first point from input (no time series, single observation)
66     input_point = parametric_input["inputValues"][0][0]
67
68     if len(input_point) != len(parametric_input["inputVarList"]):
69         raise Exception("Size mismatch between inputVarList and point to evaluate")
70
71     input_dict = deterministic_dict.copy()
72     for i in range(len(input_point)):
73         input_dict[parametric_input["inputVarList"][i]] = input_point[i][0]
74
75     return input_dict
76
77 ## This function returns a dictionary describing the output of the
78 #  computation code in parametric studies.
79 #
80 #  \param output_dict (dict) dictionary containing the output values of the
81 #  computation code (the keys are the variable names, the
82 #  values are the variable values).
83 #
84 #  \param parametric_input (dict) dictionary containing the description and values
85 #  of the parametric variables.
86 #
87 #  \return a dictionary containing the representation of the output of the
88 #  computation code.
89 #  \ingroup pyscript_utils
90 def create_normal_parametric_output(output_dict, parametric_input):
91     """
92     This function returns a dictionary describing the output of the
93     computation code in parametric studies.
94     
95     :type  output_dict: dict
96     :param output_dict: dictionary containing the output values of the
97                         computation code (the keys are the variable names, the
98                         values are the variable values).
99
100     :type  parametric_input: dict
101     :param parametric_input: dictionary containing the description and values
102                              of the parametric variables.
103
104     :return: a dictionary containing the representation of the output of the
105              computation code.
106     """
107     output_values = [[[]]]
108     for output_var in parametric_input["outputVarList"]:
109         output_values[0][0].append([output_dict[output_var]])
110     return {"outputValues" : output_values,
111             "specificOutputInfos" : [],
112             "returnCode" : 0,
113             "errorMessage" : ""}
114
115 ## This function returns a dictionary describing the output of the
116 #  computation code in parametric studies in case of error.
117 #
118 #  \param error_message (string) the error message.
119 #
120 #  \return a dictionary describing the error.
121 #  \ingroup pyscript_utils
122 def create_error_parametric_output(error_message):
123     """
124     This function returns a dictionary describing the output of the
125     computation code in parametric studies in case of error.
126     
127     :type  error_message: string
128     :param error_message: the error message.
129
130     :return: a dictionary describing the error.
131     """
132     return {"outputValues" : [],
133             "specificOutputInfos" : [],
134             "returnCode" : 1,
135             "errorMessage" : error_message}