Salome HOME
Updating and extending generic tests
[modules/adao.git] / test / test6704 / Doc_TUI_Exemple_03_en_multifonction.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2019 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 "Verification d'un exemple de la documentation"
23
24 from utExtend import assertAlmostEqualArrays
25
26 # ==============================================================================
27 #
28 # Construction artificielle d'un exemple de donnees utilisateur
29 # -------------------------------------------------------------
30 alpha = 5.
31 beta = 7
32 gamma = 9.0
33 #
34 alphamin, alphamax = 0., 10.
35 betamin,  betamax  = 3, 13
36 gammamin, gammamax = 1.5, 15.5
37 #
38 def simulation(x):
39     "Fonction de simulation H pour effectuer Y=H(X)"
40     import numpy
41     __x = numpy.matrix(numpy.ravel(numpy.matrix(x))).T
42     __H = numpy.matrix("1 0 0;0 2 0;0 0 3; 1 2 3")
43     return __H * __x
44 #
45 def multisimulation( xserie ):
46     yserie = []
47     for x in xserie:
48         yserie.append( simulation( x ) )
49     return yserie
50 #
51 # Observations obtenues par simulation
52 # ------------------------------------
53 observations = simulation((2, 3, 4))
54
55 # ==============================================================================
56 def test1():
57     "Test"
58     import numpy
59     from adao import adaoBuilder
60     #
61     # Mise en forme des entrees
62     # -------------------------
63     Xb = (alpha, beta, gamma)
64     Bounds = (
65         (alphamin, alphamax),
66         (betamin,  betamax ),
67         (gammamin, gammamax))
68     #
69     # TUI ADAO
70     # --------
71     case = adaoBuilder.New()
72     case.set(
73         'AlgorithmParameters',
74         Algorithm = '3DVAR',
75         Parameters = {
76             "Bounds":Bounds,
77             "MaximumNumberOfSteps":100,
78             "StoreSupplementaryCalculations":[
79                 "CostFunctionJ",
80                 "CurrentState",
81                 "SimulatedObservationAtOptimum",
82                 ],
83             }
84         )
85     case.set( 'Background', Vector = numpy.array(Xb), Stored = True )
86     case.set( 'Observation', Vector = numpy.array(observations) )
87     case.set( 'BackgroundError', ScalarSparseMatrix = 1.0e10 )
88     case.set( 'ObservationError', ScalarSparseMatrix = 1.0 )
89     case.set(
90         'ObservationOperator',
91         OneFunction = multisimulation,
92         Parameters  = {"DifferentialIncrement":0.0001},
93         InputFunctionAsMulti = True,
94         )
95     case.set( 'Observer', Variable="CurrentState", Template="ValuePrinter" )
96     case.execute()
97     #
98     # Exploitation independante
99     # -------------------------
100     Xbackground   = case.get("Background")
101     Xoptimum      = case.get("Analysis")[-1]
102     FX_at_optimum = case.get("SimulatedObservationAtOptimum")[-1]
103     J_values      = case.get("CostFunctionJ")[:]
104     print("")
105     print("Number of internal iterations...: %i"%len(J_values))
106     print("Initial state...................: %s"%(numpy.ravel(Xbackground),))
107     print("Optimal state...................: %s"%(numpy.ravel(Xoptimum),))
108     print("Simulation at optimal state.....: %s"%(numpy.ravel(FX_at_optimum),))
109     print("")
110     #
111     return case.get("Analysis")[-1]
112
113 # ==============================================================================
114 if __name__ == "__main__":
115     print('\nAUTODIAGNOSTIC\n')
116     print("""Exemple de la doc :
117
118     Exploitation independante des resultats d'un cas de calcul
119     ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
120     """)
121     xa = test1()
122     ecart = assertAlmostEqualArrays(xa, [ 2., 3., 4.])
123     #
124     print("  L'écart absolu maximal obtenu lors du test est de %.2e."%ecart)
125     print("  Les résultats obtenus sont corrects.")
126     print("")