Salome HOME
Improving and simplifying result variables access methods
[modules/adao.git] / src / daComposant / daAlgorithms / KalmanFilter.py
1 #-*-coding:iso-8859-1-*-
2 #
3 #  Copyright (C) 2008-2013 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
23 import logging
24 from daCore import BasicObjects, PlatformInfo
25 m = PlatformInfo.SystemUsage()
26 import numpy
27
28 # ==============================================================================
29 class ElementaryAlgorithm(BasicObjects.Algorithm):
30     def __init__(self):
31         BasicObjects.Algorithm.__init__(self, "KALMANFILTER")
32         self.defineRequiredParameter(
33             name     = "StoreSupplementaryCalculations",
34             default  = [],
35             typecast = tuple,
36             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
37             listval  = ["APosterioriCovariance", "Innovation"]
38             )
39
40     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
41         logging.debug("%s Lancement"%self._name)
42         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
43         #
44         # Paramètres de pilotage
45         # ----------------------
46         self.setParameters(Parameters)
47         #
48         # Opérateurs
49         # ----------
50         if B is None:
51             raise ValueError("Background error covariance matrix has to be properly defined!")
52         if R is None:
53             raise ValueError("Observation error covariance matrix has to be properly defined!")
54         Hm = HO["Tangent"].asMatrix(None)
55         Ha = HO["Adjoint"].asMatrix(None)
56         #
57         Mm = EM["Tangent"].asMatrix(None)
58         Mt = EM["Adjoint"].asMatrix(None)
59         #
60         if CM is not None and U is not None:
61             Cm = CM["Tangent"].asMatrix(None)
62         #
63         # Nombre de pas du Kalman identique au nombre de pas d'observations
64         # -----------------------------------------------------------------
65         duration = Y.stepnumber()
66         #
67         # Initialisation
68         # --------------
69         Xn = Xb
70         Pn = B
71         self.StoredVariables["Analysis"].store( Xn.A1 )
72         if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
73             self.StoredVariables["APosterioriCovariance"].store( Pn )
74         #
75         for step in range(duration-1):
76             if CM is not None and U is not None:
77                 if hasattr(U,"store") and len(U)>1:
78                     Xn_predicted = Mm * Xn + Cm * numpy.asmatrix(numpy.ravel( U[step] )).T
79                 elif hasattr(U,"store") and len(U)==1:
80                     Xn_predicted = Mm * Xn + Cm * numpy.asmatrix(numpy.ravel( U[0] )).T
81                 else:
82                     Xn_predicted = Mm * Xn + Cm * numpy.asmatrix(numpy.ravel( U )).T
83             else:
84                 Xn_predicted = Mm * Xn
85             Pn_predicted = Mm * Pn * Mt + Q
86             #
87             d  = numpy.asmatrix(numpy.ravel( Y[step+1] )).T - Hm * Xn_predicted
88             K  = Pn_predicted * Ha * (Hm * Pn_predicted * Ha + R).I
89             Xn = Xn_predicted + K * d
90             Pn = Pn_predicted - K * Hm * Pn_predicted
91             #
92             self.StoredVariables["Analysis"].store( Xn.A1 )
93             if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
94                 self.StoredVariables["Innovation"].store( numpy.ravel( d.A1 ) )
95             if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
96                 self.StoredVariables["APosterioriCovariance"].store( Pn )
97         #
98         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
99         logging.debug("%s Terminé"%self._name)
100         #
101         return 0
102
103 # ==============================================================================
104 if __name__ == "__main__":
105     print '\n AUTODIAGNOSTIC \n'