Salome HOME
Ajout d'un nouvel exemple
[modules/adao.git] / examples / daSalome / test005_ADAO_scripts_for_JDC.py
1 #-*-coding:iso-8859-1-*-
2 import numpy
3 import logging
4 logging.info("ComputationFunctionNode: Begin")
5 # ==============================================================================
6 # Input data and parameters: all is in the required input variable
7 # "computation", containing for example:
8 #      {'inputValues': [[[[0.0, 0.0, 0.0]]]],
9 #       'inputVarList': ['adao_default'],
10 #       'outputVarList': ['adao_default'],
11 #       'specificParameters': [{'name': 'method', 'value': 'Direct'}]}
12 # ==============================================================================
13 #
14 # Recovering the type of computation: "Direct", "Tangent" or "Adjoint"
15 # --------------------------------------------------------------------
16 method = ""
17 for param in computation["specificParameters"]:
18     if param["name"] == "method":
19         method = param["value"]
20 logging.info("ComputationFunctionNode: Found method is \'%s\'"%method)
21 #
22 # Recovering the current control state X
23 # --------------------------------------
24 Xcurrent = computation["inputValues"][0][0][0]
25 #
26 # Building explicit calculation or requiring external ones
27 # --------------------------------------------------------
28 dimension = len( Xcurrent )
29 H  = numpy.matrix(numpy.core.identity(dimension))
30 #
31 def FunctionH( X ):
32     return H * X
33 #
34 def AdjointH( (X, Y) ):
35     return H.T * Y
36 #
37 # The possible computations
38 # -------------------------
39 if method == "Direct":
40     logging.info("ComputationFunctionNode: Direct computation")
41     data = FunctionH(numpy.matrix( Xcurrent ).T)
42 #
43 if method == "Tangent":
44     logging.info("ComputationFunctionNode: Tangent computation")
45     data = FunctionH(numpy.matrix( Xcurrent ).T)
46 #
47 if method == "Adjoint":
48     logging.info("ComputationFunctionNode: Adjoint computation")
49     Ycurrent = computation["inputValues"][0][0][1]
50     data = AdjointH((numpy.matrix( Xcurrent ).T, numpy.matrix( Ycurrent ).T))
51 #
52 # Formatting the output
53 # ---------------------
54 logging.info("ComputationFunctionNode: Formatting the output")
55 it = data.flat
56 outputValues = [[[[]]]]
57 for val in it:
58   outputValues[0][0][0].append(val)
59 #
60 result = {}
61 result["outputValues"]        = outputValues
62 result["specificOutputInfos"] = []
63 result["returnCode"]          = 0
64 result["errorMessage"]        = ""
65 #
66 logging.info("ComputationFunctionNode: End")