]> SALOME platform Git repositories - modules/kernel.git/blob - src/KERNEL_PY/kernel/parametric/compo_utils.py
Salome HOME
Update copyrights 2014.
[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 """
21 This module provides utility functions for the computation codes intended for
22 use in parametric studies. The computation codes must be implemented as SALOME
23 components to use these functions. If the computation code is implemented as a
24 Python script or function, use module
25 :mod:`salome.kernel.parametric.pyscript_utils` instead. 
26 """
27
28 import SALOME_TYPES
29
30 def create_input_dict(deterministic_dict, parametric_input):
31     """
32     This function returns a dictionary containing the input values to be used
33     in the computation code.
34     
35     :type  deterministic_dict: dict
36     :param deterministic_dict: dictionary containing the fixed values (i.e.
37                                non-parametric). This dictionary can be empty
38                                if all variables are parametric.
39
40     :type  parametric_input: SALOME_TYPES/ParametricInput
41     :param parametric_input: structure containing the description and values
42                              of the parametric variables.
43
44     :return: a dictionary containing the input values for the computation code.
45     """
46     # Simply get the first point from input (no time series, single observation)
47     input_point = parametric_input.inputValues[0][0]
48
49     if len(input_point) != len(parametric_input.inputVarList):
50         raise Exception("Size mismatch between inputVarList and point to evaluate")
51
52     input_dict = deterministic_dict.copy()
53     for i in range(len(input_point)):
54         input_dict[parametric_input.inputVarList[i]] = input_point[i][0]
55
56     return input_dict
57
58 def create_normal_parametric_output(output_dict, parametric_input):
59     """
60     This function returns a structure describing the output of the computation
61     code in parametric studies.
62     
63     :type  output_dict: dict
64     :param output_dict: dictionary containing the output values of the
65                         computation code (the keys are the variable names, the
66                         values are the variable values).
67
68     :type  parametric_input: SALOME_TYPES/ParametricInput
69     :param parametric_input: structure containing the description and values
70                              of the parametric variables.
71
72     :return: a structure of type SALOME_TYPES/ParametricOutput containing the
73              output of the computation code.
74     """
75     output_values = [[[]]]
76     for output_var in parametric_input.outputVarList:
77         output_values[0][0].append([output_dict[output_var]])
78     return SALOME_TYPES.ParametricOutput(outputValues = output_values,
79                                          specificOutputInfos = [],
80                                          returnCode = 0,
81                                          errorMessage = "")
82
83 def create_error_parametric_output(error_message):
84     """
85     This function returns a structure describing the output of the computation
86     code in parametric studies in case of error.
87     
88     :type  error_message: string
89     :param error_message: the error message.
90
91     :return: a structure of type SALOME_TYPES/ParametricOutput describing the
92              error.
93     """
94     return SALOME_TYPES.ParametricOutput(outputValues = [],
95                                          specificOutputInfos = [],
96                                          returnCode = 1,
97                                          errorMessage = error_message)