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