Salome HOME
Minor correction for raised error messages
[modules/adao.git] / examples / daSalome / test005_ADAO_scripts_for_JDC.py
1 #-*-coding:iso-8859-1-*-
2 #
3 # Copyright (C) 2008-2017 EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22
23 import numpy
24 import logging
25 logging.info("ComputationFunctionNode: Begin")
26 # ==============================================================================
27 # Input data and parameters: all is in the required input variable
28 # "computation", containing for example:
29 #      {'inputValues': [[[[0.0, 0.0, 0.0]]]],
30 #       'inputVarList': ['adao_default'],
31 #       'outputVarList': ['adao_default'],
32 #       'specificParameters': [{'name': 'method', 'value': 'Direct'}]}
33 # ==============================================================================
34 #
35 # Recovering the type of computation: "Direct", "Tangent" or "Adjoint"
36 # --------------------------------------------------------------------
37 method = ""
38 for param in computation["specificParameters"]:
39     if param["name"] == "method":
40         method = param["value"]
41 logging.info("ComputationFunctionNode: Found method is \'%s\'"%method)
42 #
43 # Recovering the current control state X
44 # --------------------------------------
45 Xcurrent = computation["inputValues"][0][0][0]
46 #
47 # Building explicit calculation or requiring external ones
48 # --------------------------------------------------------
49 dimension = len( Xcurrent )
50 H  = numpy.matrix(numpy.core.identity(dimension))
51 #
52 def FunctionH( X ):
53     return H * X
54 #
55 def AdjointH( (X, Y) ):
56     return H.T * Y
57 #
58 # The possible computations
59 # -------------------------
60 if method == "Direct":
61     logging.info("ComputationFunctionNode: Direct computation")
62     data = FunctionH(numpy.matrix( Xcurrent ).T)
63 #
64 if method == "Tangent":
65     logging.info("ComputationFunctionNode: Tangent computation")
66     data = FunctionH(numpy.matrix( Xcurrent ).T)
67 #
68 if method == "Adjoint":
69     logging.info("ComputationFunctionNode: Adjoint computation")
70     Ycurrent = computation["inputValues"][0][0][1]
71     data = AdjointH((numpy.matrix( Xcurrent ).T, numpy.matrix( Ycurrent ).T))
72 #
73 # Formatting the output
74 # ---------------------
75 logging.info("ComputationFunctionNode: Formatting the output")
76 it = data.flat
77 outputValues = [[[[]]]]
78 for val in it:
79   outputValues[0][0][0].append(val)
80 #
81 result = {}
82 result["outputValues"]        = outputValues
83 result["specificOutputInfos"] = []
84 result["returnCode"]          = 0
85 result["errorMessage"]        = ""
86 #
87 logging.info("ComputationFunctionNode: End")