Salome HOME
Mise à jour des copyrights
[modules/adao.git] / examples / daSalome / test005_ADAO_scripts_for_JDC.py
1 #-*-coding:iso-8859-1-*-
2 # Copyright (C) 2010-2011 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: André Ribes, andre.ribes@edf.fr, EDF R&D
21
22 import numpy
23 import logging
24 logging.info("ComputationFunctionNode: Begin")
25 # ==============================================================================
26 # Input data and parameters: all is in the required input variable
27 # "computation", containing for example:
28 #      {'inputValues': [[[[0.0, 0.0, 0.0]]]],
29 #       'inputVarList': ['adao_default'],
30 #       'outputVarList': ['adao_default'],
31 #       'specificParameters': [{'name': 'method', 'value': 'Direct'}]}
32 # ==============================================================================
33 #
34 # Recovering the type of computation: "Direct", "Tangent" or "Adjoint"
35 # --------------------------------------------------------------------
36 method = ""
37 for param in computation["specificParameters"]:
38     if param["name"] == "method":
39         method = param["value"]
40 logging.info("ComputationFunctionNode: Found method is \'%s\'"%method)
41 #
42 # Recovering the current control state X
43 # --------------------------------------
44 Xcurrent = computation["inputValues"][0][0][0]
45 #
46 # Building explicit calculation or requiring external ones
47 # --------------------------------------------------------
48 dimension = len( Xcurrent )
49 H  = numpy.matrix(numpy.core.identity(dimension))
50 #
51 def FunctionH( X ):
52     return H * X
53 #
54 def AdjointH( (X, Y) ):
55     return H.T * Y
56 #
57 # The possible computations
58 # -------------------------
59 if method == "Direct":
60     logging.info("ComputationFunctionNode: Direct computation")
61     data = FunctionH(numpy.matrix( Xcurrent ).T)
62 #
63 if method == "Tangent":
64     logging.info("ComputationFunctionNode: Tangent computation")
65     data = FunctionH(numpy.matrix( Xcurrent ).T)
66 #
67 if method == "Adjoint":
68     logging.info("ComputationFunctionNode: Adjoint computation")
69     Ycurrent = computation["inputValues"][0][0][1]
70     data = AdjointH((numpy.matrix( Xcurrent ).T, numpy.matrix( Ycurrent ).T))
71 #
72 # Formatting the output
73 # ---------------------
74 logging.info("ComputationFunctionNode: Formatting the output")
75 it = data.flat
76 outputValues = [[[[]]]]
77 for val in it:
78   outputValues[0][0][0].append(val)
79 #
80 result = {}
81 result["outputValues"]        = outputValues
82 result["specificOutputInfos"] = []
83 result["returnCode"]          = 0
84 result["errorMessage"]        = ""
85 #
86 logging.info("ComputationFunctionNode: End")