Salome HOME
Updating copyright date information
[modules/adao.git] / test / test6901 / Verification_des_Assimilation_Algorithms.py
1 #-*-coding:iso-8859-1-*-
2 #
3 # Copyright (C) 2008-2017 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 de la disponibilite de l'ensemble des algorithmes"
23
24 # ==============================================================================
25 import adaoBuilder, numpy
26 def test1():
27     """Verification de la disponibilite de l'ensemble des algorithmes"""
28     Xa = {}
29     for algo in ("3DVAR", "Blue", "ExtendedBlue", "LinearLeastSquares", "NonLinearLeastSquares", ):
30         print
31         msg = "Algorithme en test : %s"%algo
32         print msg+"\n"+"="*len(msg)
33         #
34         adaopy = adaoBuilder.New()
35         adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"EpsilonMinimumExponent":-10, })
36         adaopy.setBackground         (Vector = [0,1,2])
37         adaopy.setBackgroundError    (ScalarSparseMatrix = 1.)
38         adaopy.setObservation        (Vector = [0.5,1.5,2.5])
39         adaopy.setObservationError   (DiagonalSparseMatrix = "1 1 1")
40         adaopy.setObservationOperator(Matrix = "1 0 0;0 2 0;0 0 3")
41         adaopy.setObserver("Analysis",Template="ValuePrinter")
42         adaopy.execute()
43         Xa[algo] = adaopy.get("Analysis")[-1]
44         del adaopy
45     #
46     for algo in ("ExtendedKalmanFilter", "KalmanFilter", "UnscentedKalmanFilter", "4DVAR"):
47         print
48         msg = "Algorithme en test : %s"%algo
49         print msg+"\n"+"="*len(msg)
50         #
51         adaopy = adaoBuilder.New()
52         adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"EpsilonMinimumExponent":-10, })
53         adaopy.setBackground         (Vector = [0,1,2])
54         adaopy.setBackgroundError    (ScalarSparseMatrix = 1.)
55         adaopy.setObservation        (Vector = [0.5,1.5,2.5])
56         adaopy.setObservationError   (DiagonalSparseMatrix = "1 1 1")
57         adaopy.setObservationOperator(Matrix = "1 0 0;0 1 0;0 0 1")
58         adaopy.setEvolutionModel     (Matrix = "1 0 0;0 1 0;0 0 1")
59         adaopy.setEvolutionError     (ScalarSparseMatrix = 1.)
60         adaopy.setObserver("Analysis",Template="ValuePrinter")
61         adaopy.execute()
62         Xa[algo] = adaopy.get("Analysis")[-1]
63         del adaopy
64     #
65     for algo in ("ParticleSwarmOptimization", "QuantileRegression", ):
66         print
67         msg = "Algorithme en test : %s"%algo
68         print msg+"\n"+"="*len(msg)
69         #
70         adaopy = adaoBuilder.New()
71         adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"BoxBounds":3*[[-1,3]], "SetSeed":1000, })
72         adaopy.setBackground         (Vector = [0,1,2])
73         adaopy.setBackgroundError    (ScalarSparseMatrix = 1.)
74         adaopy.setObservation        (Vector = [0.5,1.5,2.5])
75         adaopy.setObservationError   (DiagonalSparseMatrix = "1 1 1")
76         adaopy.setObservationOperator(Matrix = "1 0 0;0 1 0;0 0 1")
77         adaopy.setEvolutionModel     (Matrix = "1 0 0;0 1 0;0 0 1")
78         adaopy.setEvolutionError     (ScalarSparseMatrix = 1.)
79         adaopy.setObserver("Analysis",Template="ValuePrinter")
80         adaopy.execute()
81         Xa[algo] = adaopy.get("Analysis")[-1]
82         del adaopy
83     #
84     for algo in ("EnsembleBlue", ):
85         print
86         msg = "Algorithme en test : %s"%algo
87         print msg+"\n"+"="*len(msg)
88         #
89         adaopy = adaoBuilder.New()
90         adaopy.setAlgorithmParameters(Algorithm=algo, Parameters={"SetSeed":1000, })
91         adaopy.setBackground         (VectorSerie = 100*[[0,1,2]])
92         adaopy.setBackgroundError    (ScalarSparseMatrix = 1.)
93         adaopy.setObservation        (Vector = [0.5,1.5,2.5])
94         adaopy.setObservationError   (DiagonalSparseMatrix = "1 1 1")
95         adaopy.setObservationOperator(Matrix = "1 0 0;0 1 0;0 0 1")
96         adaopy.setEvolutionModel     (Matrix = "1 0 0;0 1 0;0 0 1")
97         adaopy.setEvolutionError     (ScalarSparseMatrix = 1.)
98         adaopy.setObserver("Analysis",Template="ValuePrinter")
99         adaopy.execute()
100         del adaopy
101     #
102     print
103     msg = "Tests des ecarts"
104     print msg+"\n"+"="*len(msg)
105     verify_similarity_of_algo_results(("3DVAR", "Blue", "ExtendedBlue"), Xa)
106     verify_similarity_of_algo_results(("LinearLeastSquares", "NonLinearLeastSquares"), Xa)
107     verify_similarity_of_algo_results(("ExtendedKalmanFilter", "KalmanFilter", "UnscentedKalmanFilter"), Xa)
108     print
109     #
110     return 0
111
112 def almost_equal_vectors(v1, v2, precision = 1.e-15, msg = ""):
113     """Comparaison de deux vecteurs"""
114     print "  Difference maximale %s: %.2e"%(msg, max(abs(v2 - v1)))
115     return max(abs(v2 - v1)) < precision
116
117 def verify_similarity_of_algo_results(serie = [], Xa = {}):
118     print "Camparaisons :"
119     for algo1 in serie:
120         for algo2 in serie:
121             if algo1 is algo2: break
122             assert almost_equal_vectors( Xa[algo1], Xa[algo2], 1.e-5, "entre %s et %s "%(algo1, algo2) )
123     print "Algorithmes dont les resultats sont similaires : %s\n"%(serie,)
124
125 #===============================================================================
126 if __name__ == "__main__":
127     print '\n AUTODIAGNOSTIC \n'
128     test1()