]> SALOME platform Git repositories - modules/adao.git/blob - examples/daSalome/test006_Observers_Observation_Operator.py
Salome HOME
Minor correction for raised error messages
[modules/adao.git] / examples / daSalome / test006_Observers_Observation_Operator.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 time
25 import logging
26 logging.info("ComputationFunctionNode: Begin")
27 # ==============================================================================
28 # Input data and parameters: all is in the required input variable
29 # "computation", containing for example:
30 #      {'inputValues': [[[[0.0, 0.0, 0.0]]]],
31 #       'inputVarList': ['adao_default'],
32 #       'outputVarList': ['adao_default'],
33 #       'specificParameters': [{'name': 'method', 'value': 'Direct'}]}
34 # ==============================================================================
35 #
36 # Recovering the type of computation: "Direct", "Tangent" or "Adjoint"
37 # --------------------------------------------------------------------
38 method = ""
39 for param in computation["specificParameters"]:
40     if param["name"] == "method":
41         method = param["value"]
42 logging.info("ComputationFunctionNode: Found method is \'%s\'"%method)
43 #
44 # Recovering the current control state X
45 # --------------------------------------
46 Xcurrent = computation["inputValues"][0][0][0]
47 #
48 # Building explicit calculation or requiring external ones
49 # --------------------------------------------------------
50 dimension = 3
51 H  = numpy.matrix(numpy.core.identity(dimension))
52 #
53 def FunctionH( X ):
54     time.sleep(1)
55     return H * X
56 #
57 def AdjointH( (X, Y) ):
58     return H.T * Y
59 #
60 # The possible computations
61 # -------------------------
62 if method == "Direct":
63     logging.info("ComputationFunctionNode: Direct computation")
64     data = FunctionH(numpy.matrix( Xcurrent ).T)
65 #
66 if method == "Tangent":
67     logging.info("ComputationFunctionNode: Tangent computation")
68     data = FunctionH(numpy.matrix( Xcurrent ).T)
69 #
70 if method == "Adjoint":
71     logging.info("ComputationFunctionNode: Adjoint computation")
72     Ycurrent = computation["inputValues"][0][0][1]
73     data = AdjointH((numpy.matrix( Xcurrent ).T, numpy.matrix( Ycurrent ).T))
74 #
75 # Formatting the output
76 # ---------------------
77 logging.info("ComputationFunctionNode: Formatting the output")
78 it = data.flat
79 outputValues = [[[[]]]]
80 for val in it:
81   outputValues[0][0][0].append(val)
82 #
83 result = {}
84 result["outputValues"]        = outputValues
85 result["specificOutputInfos"] = []
86 result["returnCode"]          = 0
87 result["errorMessage"]        = ""
88 #
89 logging.info("ComputationFunctionNode: End")