1 #-*-coding:iso-8859-1-*-
3 # Copyright (C) 2008-2013 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
24 from daCore import BasicObjects, PlatformInfo
25 m = PlatformInfo.SystemUsage()
28 # ==============================================================================
29 class ElementaryAlgorithm(BasicObjects.Algorithm):
31 BasicObjects.Algorithm.__init__(self, "EXTENDEDKALMANFILTER")
32 self.defineRequiredParameter(
33 name = "StoreSupplementaryCalculations",
36 message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
37 listval = ["APosterioriCovariance", "Innovation"]
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")))
44 # Paramètres de pilotage
45 # ----------------------
46 self.setParameters(Parameters)
51 raise ValueError("Background error covariance matrix has to be properly defined!")
53 raise ValueError("Observation error covariance matrix has to be properly defined!")
55 H = HO["Direct"].appliedTo
57 M = EM["Direct"].appliedTo
59 # Nombre de pas du Kalman identique au nombre de pas d'observations
60 # -----------------------------------------------------------------
61 duration = Y.stepnumber()
67 self.StoredVariables["Analysis"].store( Xn.A1 )
68 if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
69 self.StoredVariables["APosterioriCovariance"].store( Pn )
71 for step in range(duration-1):
72 Ynpu = numpy.asmatrix(numpy.ravel( Y[step+1] )).T
74 Ht = HO["Tangent"].asMatrix(ValueForMethodForm = Xn)
75 Ht = Ht.reshape(Ynpu.size,Xn.size) # ADAO & check shape
76 Ha = HO["Adjoint"].asMatrix(ValueForMethodForm = Xn)
77 Ha = Ha.reshape(Xn.size,Ynpu.size) # ADAO & check shape
79 Mt = EM["Tangent"].asMatrix(ValueForMethodForm = Xn)
80 Mt = Mt.reshape(Xn.size,Xn.size) # ADAO & check shape
81 Ma = EM["Adjoint"].asMatrix(ValueForMethodForm = Xn)
82 Ma = Ma.reshape(Xn.size,Xn.size) # ADAO & check shape
85 if hasattr(U,"store") and len(U)>1:
86 Un = numpy.asmatrix(numpy.ravel( U[step] )).T
87 elif hasattr(U,"store") and len(U)==1:
88 Un = numpy.asmatrix(numpy.ravel( U[0] )).T
90 Un = numpy.asmatrix(numpy.ravel( U )).T
94 Xn_predicted = M( (Xn, Un) )
95 Pn_predicted = Mt * Pn * Ma + Q
97 d = Ynpu - H( Xn_predicted )
98 K = Pn_predicted * Ha * (Ht * Pn_predicted * Ha + R).I
99 Xn = Xn_predicted + K * d
100 Pn = Pn_predicted - K * Ht * Pn_predicted
102 self.StoredVariables["Analysis"].store( Xn.A1 )
103 if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
104 self.StoredVariables["Innovation"].store( numpy.ravel( d.A1 ) )
105 if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
106 self.StoredVariables["APosterioriCovariance"].store( Pn )
108 logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
109 logging.debug("%s Terminé"%self._name)
113 # ==============================================================================
114 if __name__ == "__main__":
115 print '\n AUTODIAGNOSTIC \n'