Back to index

salome-kernel  6.5.0
compo_utils.py
Go to the documentation of this file.
00001 # Copyright (C) 2011-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 #
00003 # This library is free software; you can redistribute it and/or
00004 # modify it under the terms of the GNU Lesser General Public
00005 # License as published by the Free Software Foundation; either
00006 # version 2.1 of the License.
00007 #
00008 # This library is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 # Lesser General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU Lesser General Public
00014 # License along with this library; if not, write to the Free Software
00015 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 #
00017 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 #
00019 
00020 """
00021 This module provides utility functions for the computation codes intended for
00022 use in parametric studies. The computation codes must be implemented as SALOME
00023 components to use these functions. If the computation code is implemented as a
00024 Python script or function, use module
00025 :mod:`salome.kernel.parametric.pyscript_utils` instead. 
00026 """
00027 
00028 import SALOME_TYPES
00029 
00030 def create_input_dict(deterministic_dict, parametric_input):
00031     """
00032     This function returns a dictionary containing the input values to be used
00033     in the computation code.
00034     
00035     :type  deterministic_dict: dict
00036     :param deterministic_dict: dictionary containing the fixed values (i.e.
00037                                non-parametric). This dictionary can be empty
00038                                if all variables are parametric.
00039 
00040     :type  parametric_input: SALOME_TYPES/ParametricInput
00041     :param parametric_input: structure containing the description and values
00042                              of the parametric variables.
00043 
00044     :return: a dictionary containing the input values for the computation code.
00045     """
00046     # Simply get the first point from input (no time series, single observation)
00047     input_point = parametric_input.inputValues[0][0]
00048 
00049     if len(input_point) != len(parametric_input.inputVarList):
00050         raise Exception("Size mismatch between inputVarList and point to evaluate")
00051 
00052     input_dict = deterministic_dict.copy()
00053     for i in range(len(input_point)):
00054         input_dict[parametric_input.inputVarList[i]] = input_point[i][0]
00055 
00056     return input_dict
00057 
00058 def create_normal_parametric_output(output_dict, parametric_input):
00059     """
00060     This function returns a structure describing the output of the computation
00061     code in parametric studies.
00062     
00063     :type  output_dict: dict
00064     :param output_dict: dictionary containing the output values of the
00065                         computation code (the keys are the variable names, the
00066                         values are the variable values).
00067 
00068     :type  parametric_input: SALOME_TYPES/ParametricInput
00069     :param parametric_input: structure containing the description and values
00070                              of the parametric variables.
00071 
00072     :return: a structure of type SALOME_TYPES/ParametricOutput containing the
00073              output of the computation code.
00074     """
00075     output_values = [[[]]]
00076     for output_var in parametric_input.outputVarList:
00077         output_values[0][0].append([output_dict[output_var]])
00078     return SALOME_TYPES.ParametricOutput(outputValues = output_values,
00079                                          specificOutputInfos = [],
00080                                          returnCode = 0,
00081                                          errorMessage = "")
00082 
00083 def create_error_parametric_output(error_message):
00084     """
00085     This function returns a structure describing the output of the computation
00086     code in parametric studies in case of error.
00087     
00088     :type  error_message: string
00089     :param error_message: the error message.
00090 
00091     :return: a structure of type SALOME_TYPES/ParametricOutput describing the
00092              error.
00093     """
00094     return SALOME_TYPES.ParametricOutput(outputValues = [],
00095                                          specificOutputInfos = [],
00096                                          returnCode = 1,
00097                                          errorMessage = error_message)