1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2021 EDF R&D
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.
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.
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
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22 "Full definition of a use case for the standard user"
28 # ==============================================================================
30 # Artificial user data example
31 # ----------------------------
36 alphamin, alphamax = 0., 10.
37 betamin, betamax = 3, 13
38 gammamin, gammamax = 1.5, 15.5
41 "Observation operator H for Y=H(X)"
44 __H = numpy.array([[1, 0, 0], [0, 2, 0], [0, 0, 3], [1, 2, 3]])
45 return numpy.dot(__H,__x)
47 def multisimulation( xserie ):
50 yserie.append( simulation( x ) )
53 # Twin experiment observations
54 # ----------------------------
55 observations = simulation((2, 3, 4))
57 # ==============================================================================
58 class Test_Adao(unittest.TestCase):
61 Full definition of a use case for the standard user
62 +++++++++++++++++++++++++++++++++++++++++++++++++++
66 from adao import adaoBuilder
68 # Mise en forme des entrees
69 # -------------------------
70 Xb = (alpha, beta, gamma)
78 case = adaoBuilder.New()
79 case.set( 'AlgorithmParameters',
80 Algorithm = 'NonLinearLeastSquares', # Mots-clé réservé
81 Parameters = { # Dictionnaire
82 "Bounds":Bounds, # Liste de paires de Real ou de None
83 "MaximumNumberOfSteps":100, # Int >= 0
84 "CostDecrementTolerance":1.e-7, # Real > 0
85 "StoreSupplementaryCalculations":[# Liste de mots-clés réservés
86 "CostFunctionJAtCurrentOptimum",
87 "CostFunctionJoAtCurrentOptimum",
89 "SimulatedObservationAtCurrentOptimum",
90 "SimulatedObservationAtOptimum",
94 case.set( 'Background',
95 Vector = Xb, # array, list, tuple, matrix
98 case.set( 'Observation',
99 Vector = observations, # array, list, tuple, matrix
100 Stored = False, # Bool
102 case.set( 'ObservationError',
103 Matrix = None, # None ou matrice carrée
104 ScalarSparseMatrix = 1.0, # None ou Real > 0
105 DiagonalSparseMatrix = None, # None ou vecteur
107 case.set( 'ObservationOperator',
108 OneFunction = multisimulation, # MultiFonction [Y] = F([X])
109 Parameters = { # Dictionnaire
110 "DifferentialIncrement":0.0001, # Real > 0
111 "CenteredFiniteDifference":False, # Bool
113 InputFunctionAsMulti = True, # Bool
115 case.set( 'Observer',
116 Variable = "CurrentState", # Mot-clé
117 Template = "ValuePrinter", # Mot-clé
118 String = None, # None ou code Python
119 Info = None, # None ou string
124 # Exploitation independante
125 # -------------------------
126 Xbackground = case.get("Background")
127 Xoptimum = case.get("Analysis")[-1]
128 FX_at_optimum = case.get("SimulatedObservationAtOptimum")[-1]
129 J_values = case.get("CostFunctionJAtCurrentOptimum")[:]
131 print("Number of internal iterations...: %i"%len(J_values))
132 print("Initial state...................: %s"%(numpy.ravel(Xbackground),))
133 print("Optimal state...................: %s"%(numpy.ravel(Xoptimum),))
134 print("Simulation at optimal state.....: %s"%(numpy.ravel(FX_at_optimum),))
139 ecart = assertAlmostEqualArrays(Xoptimum, [ 2., 3., 4.])
141 print("The maximal absolute error in the test is of %.2e."%ecart)
142 print("The results are correct.")
147 # ==============================================================================
148 def assertAlmostEqualArrays(first, second, places=7, msg=None, delta=None):
149 "Compare two vectors, like unittest.assertAlmostEqual"
153 if delta is not None:
154 if ( numpy.abs(numpy.asarray(first) - numpy.asarray(second)) > float(delta) ).any():
155 raise AssertionError("%s != %s within %s places"%(first,second,delta))
157 if ( numpy.abs(numpy.asarray(first) - numpy.asarray(second)) > 10**(-int(places)) ).any():
158 raise AssertionError("%s != %s within %i places"%(first,second,places))
159 return max(abs(numpy.asarray(first) - numpy.asarray(second)))
161 # ==============================================================================
162 if __name__ == '__main__':
163 print("\nAUTODIAGNOSTIC\n==============")
164 sys.stderr = sys.stdout
165 unittest.main(verbosity=2)