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 "Verification du fonctionnement correct d'entrees en mono ou multi-fonctions"
27 from adao import adaoBuilder
29 # ==============================================================================
31 M = numpy.matrix("1 0 0;0 2 0;0 0 3")
32 def MonoFonction( x ):
33 return M * numpy.asmatrix(numpy.ravel( x )).T
35 def MultiFonction( xserie ):
38 _mulHX.append( M * numpy.asmatrix(numpy.ravel( _subX )).T )
41 # ==============================================================================
42 class Test_Adao(unittest.TestCase):
45 Verification du fonctionnement identique pour les algorithmes temporels
46 en utilisant une fonction lineaire et carree
48 print("\n "+self.test1.__doc__.strip()+"\n")
51 for algo in ("ExtendedKalmanFilter", "KalmanFilter", "EnsembleKalmanFilter", "UnscentedKalmanFilter", "4DVAR"):
53 msg = "Algorithme en test en MonoFonction : %s"%algo
54 print(msg+"\n"+"-"*len(msg))
56 adaopy = adaoBuilder.New()
57 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"EpsilonMinimumExponent":-10, "SetSeed":1000})
58 adaopy.setBackground (Vector = [0,1,2])
59 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
60 adaopy.setObservation (Vector = [0.5,1.5,2.5])
61 adaopy.setObservationError (DiagonalSparseMatrix = "1 1 1")
62 adaopy.setObservationOperator(OneFunction = MonoFonction)
63 adaopy.setEvolutionError (ScalarSparseMatrix = 1.)
64 adaopy.setEvolutionModel (Matrix = "1 0 0;0 1 0;0 0 1")
65 adaopy.setObserver("Analysis",Template="ValuePrinter")
67 Xa["Mono/"+algo] = adaopy.get("Analysis")[-1]
70 for algo in ("ExtendedKalmanFilter", "KalmanFilter", "EnsembleKalmanFilter", "UnscentedKalmanFilter", "4DVAR"):
72 msg = "Algorithme en test en MultiFonction : %s"%algo
73 print(msg+"\n"+"-"*len(msg))
75 adaopy = adaoBuilder.New()
76 adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"EpsilonMinimumExponent":-10, "SetSeed":1000})
77 adaopy.setBackground (Vector = [0,1,2])
78 adaopy.setBackgroundError (ScalarSparseMatrix = 1.)
79 adaopy.setObservation (Vector = [0.5,1.5,2.5])
80 adaopy.setObservationError (DiagonalSparseMatrix = "1 1 1")
81 adaopy.setObservationOperator(OneFunction = MultiFonction, InputFunctionAsMulti = True)
82 adaopy.setEvolutionError (ScalarSparseMatrix = 1.)
83 adaopy.setEvolutionModel (Matrix = "1 0 0;0 1 0;0 0 1")
84 adaopy.setObserver("Analysis",Template="ValuePrinter")
86 Xa["Multi/"+algo] = adaopy.get("Analysis")[-1]
90 msg = "Tests des ecarts attendus :"
91 print(msg+"\n"+"="*len(msg))
92 for algo in ("ExtendedKalmanFilter", "KalmanFilter", "EnsembleKalmanFilter", "UnscentedKalmanFilter", "4DVAR"):
93 verify_similarity_of_algo_results(("Multi/"+algo, "Mono/"+algo), Xa, 1.e-20)
94 print(" Les resultats obtenus sont corrects.")
99 # ==============================================================================
100 def almost_equal_vectors(v1, v2, precision = 1.e-15, msg = ""):
101 """Comparaison de deux vecteurs"""
102 print(" Difference maximale %s: %.2e"%(msg, max(abs(v2 - v1))))
103 return max(abs(v2 - v1)) < precision
105 def verify_similarity_of_algo_results(serie = [], Xa = {}, precision = 1.e-15):
106 print(" Comparaisons :")
109 if algo1 is algo2: break
110 assert almost_equal_vectors( Xa[algo1], Xa[algo2], precision, "entre %s et %s "%(algo1, algo2) )
111 print(" Algorithmes dont les resultats sont similaires a %.0e : %s\n"%(precision, serie,))
114 #===============================================================================
115 if __name__ == "__main__":
116 print("\nAUTODIAGNOSTIC\n==============")
117 sys.stderr = sys.stdout
118 unittest.main(verbosity=2)