1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2022 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 "Verification d'un exemple de la documentation"
27 # ==============================================================================
29 # Construction artificielle d'un exemple de donnees utilisateur
30 # -------------------------------------------------------------
35 alphamin, alphamax = 0., 10.
36 betamin, betamax = 3, 13
37 gammamin, gammamax = 1.5, 15.5
40 "Fonction de simulation H pour effectuer Y=H(X)"
43 __H = numpy.array([[1,0,0],[0,2,0],[0,0,3],[1,2,3]])
46 def multisimulation( xserie ):
49 yserie.append( simulation( x ) )
52 # Observations obtenues par simulation
53 # ------------------------------------
54 observations = simulation((2, 3, 4))
56 # ==============================================================================
57 class Test_Adao(unittest.TestCase):
60 print("""Exemple de la doc :
62 Exploitation independante des resultats d'un cas de calcul
63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
65 #---------------------------------------------------------------------------
67 from adao import adaoBuilder
69 # Mise en forme des entrees
70 # -------------------------
71 Xb = (alpha, beta, gamma)
79 case = adaoBuilder.New()
81 'AlgorithmParameters',
85 "MaximumNumberOfIterations":100,
86 "StoreSupplementaryCalculations":[
89 "SimulatedObservationAtOptimum",
93 case.set( 'Background', Vector = numpy.array(Xb), Stored = True )
94 case.set( 'Observation', Vector = numpy.array(observations) )
95 case.set( 'BackgroundError', ScalarSparseMatrix = 1.0e10 )
96 case.set( 'ObservationError', ScalarSparseMatrix = 1.0 )
98 'ObservationOperator',
99 OneFunction = multisimulation,
100 Parameters = {"DifferentialIncrement":0.0001},
101 InputFunctionAsMulti = True,
103 case.set( 'Observer', Variable="CurrentState", Template="ValuePrinter" )
106 # Exploitation independante
107 # -------------------------
108 Xbackground = case.get("Background")
109 Xoptimum = case.get("Analysis")[-1]
110 FX_at_optimum = case.get("SimulatedObservationAtOptimum")[-1]
111 J_values = case.get("CostFunctionJ")[:]
113 print("Number of internal iterations...: %i"%len(J_values))
114 print("Initial state...................: %s"%(numpy.ravel(Xbackground),))
115 print("Optimal state...................: %s"%(numpy.ravel(Xoptimum),))
116 print("Simulation at optimal state.....: %s"%(numpy.ravel(FX_at_optimum),))
119 #---------------------------------------------------------------------------
120 xa = case.get("Analysis")[-1]
121 ecart = assertAlmostEqualArrays(xa, [ 2., 3., 4.])
123 print(" L'écart absolu maximal obtenu lors du test est de %.2e."%ecart)
124 print(" Les résultats obtenus sont corrects.")
129 # ==============================================================================
130 def assertAlmostEqualArrays(first, second, places=7, msg=None, delta=None):
131 "Compare two vectors, like unittest.assertAlmostEqual"
135 if delta is not None:
136 if ( numpy.abs(numpy.asarray(first) - numpy.asarray(second)) > float(delta) ).any():
137 raise AssertionError("%s != %s within %s places"%(first,second,delta))
139 if ( numpy.abs(numpy.asarray(first) - numpy.asarray(second)) > 10**(-int(places)) ).any():
140 raise AssertionError("%s != %s within %i places"%(first,second,places))
141 return max(abs(numpy.asarray(first) - numpy.asarray(second)))
143 # ==============================================================================
144 if __name__ == "__main__":
145 print('\nAUTODIAGNOSTIC\n')
146 sys.stderr = sys.stdout
147 unittest.main(verbosity=2)