Back to index

salome-kernel  6.5.0
pyscript_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 a
00023 Python script or function to use these functions. If the computation code is
00024 implemented as a SALOME component, use module
00025 :mod:`salome.kernel.parametric.compo_utils` instead. 
00026 """
00027 
00028 def create_input_dict(deterministic_dict, parametric_input):
00029     """
00030     This function returns a dictionary containing the input values to be used
00031     in the computation code.
00032     
00033     :type  deterministic_dict: dict
00034     :param deterministic_dict: dictionary containing the fixed values (i.e.
00035                                non-parametric). This dictionary can be empty
00036                                if all variables are parametric.
00037 
00038     :type  parametric_input: dict
00039     :param parametric_input: dictionary containing the description and values
00040                              of the parametric variables.
00041 
00042     :return: a dictionary containing the input values for the computation code.
00043     """
00044     # Simply get the first point from input (no time series, single observation)
00045     input_point = parametric_input["inputValues"][0][0]
00046 
00047     if len(input_point) != len(parametric_input["inputVarList"]):
00048         raise Exception("Size mismatch between inputVarList and point to evaluate")
00049 
00050     input_dict = deterministic_dict.copy()
00051     for i in range(len(input_point)):
00052         input_dict[parametric_input["inputVarList"][i]] = input_point[i][0]
00053 
00054     return input_dict
00055 
00056 def create_normal_parametric_output(output_dict, parametric_input):
00057     """
00058     This function returns a dictionary describing the output of the
00059     computation code in parametric studies.
00060     
00061     :type  output_dict: dict
00062     :param output_dict: dictionary containing the output values of the
00063                         computation code (the keys are the variable names, the
00064                         values are the variable values).
00065 
00066     :type  parametric_input: dict
00067     :param parametric_input: dictionary containing the description and values
00068                              of the parametric variables.
00069 
00070     :return: a dictionary containing the representation of the output of the
00071              computation code.
00072     """
00073     output_values = [[[]]]
00074     for output_var in parametric_input["outputVarList"]:
00075         output_values[0][0].append([output_dict[output_var]])
00076     return {"outputValues" : output_values,
00077             "specificOutputInfos" : [],
00078             "returnCode" : 0,
00079             "errorMessage" : ""}
00080 
00081 def create_error_parametric_output(error_message):
00082     """
00083     This function returns a dictionary describing the output of the
00084     computation code in parametric studies in case of error.
00085     
00086     :type  error_message: string
00087     :param error_message: the error message.
00088 
00089     :return: a dictionary describing the error.
00090     """
00091     return {"outputValues" : [],
00092             "specificOutputInfos" : [],
00093             "returnCode" : 1,
00094             "errorMessage" : error_message}