Salome HOME
Updating copyright information and version change
[modules/adao.git] / examples / daSalome / test006_Observers_Observation_Operator.py
1 #-*-coding:iso-8859-1-*-
2 # Copyright (C) 2008-2015 EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
21
22 import numpy
23 import time
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 = 3
50 H  = numpy.matrix(numpy.core.identity(dimension))
51 #
52 def FunctionH( X ):
53     time.sleep(1)
54     return H * X
55 #
56 def AdjointH( (X, Y) ):
57     return H.T * Y
58 #
59 # The possible computations
60 # -------------------------
61 if method == "Direct":
62     logging.info("ComputationFunctionNode: Direct computation")
63     data = FunctionH(numpy.matrix( Xcurrent ).T)
64 #
65 if method == "Tangent":
66     logging.info("ComputationFunctionNode: Tangent computation")
67     data = FunctionH(numpy.matrix( Xcurrent ).T)
68 #
69 if method == "Adjoint":
70     logging.info("ComputationFunctionNode: Adjoint computation")
71     Ycurrent = computation["inputValues"][0][0][1]
72     data = AdjointH((numpy.matrix( Xcurrent ).T, numpy.matrix( Ycurrent ).T))
73 #
74 # Formatting the output
75 # ---------------------
76 logging.info("ComputationFunctionNode: Formatting the output")
77 it = data.flat
78 outputValues = [[[[]]]]
79 for val in it:
80   outputValues[0][0][0].append(val)
81 #
82 result = {}
83 result["outputValues"]        = outputValues
84 result["specificOutputInfos"] = []
85 result["returnCode"]          = 0
86 result["errorMessage"]        = ""
87 #
88 logging.info("ComputationFunctionNode: End")