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, "KALMANFILTER")
32 self.defineRequiredParameter(
33 name = "StoreSupplementaryCalculations",
36 message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
37 listval = ["APosterioriCovariance", "CostFunctionJ", "Innovation"]
39 self.defineRequiredParameter(
40 name = "EstimationType",
43 message = "Estimation d'etat ou de parametres",
44 listval = ["State", "Parameters"],
47 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
48 logging.debug("%s Lancement"%self._name)
49 logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
51 # Paramètres de pilotage
52 # ----------------------
53 self.setParameters(Parameters)
58 raise ValueError("Background error covariance matrix has to be properly defined!")
60 raise ValueError("Observation error covariance matrix has to be properly defined!")
62 Ht = HO["Tangent"].asMatrix(None)
63 Ha = HO["Adjoint"].asMatrix(None)
65 Mt = EM["Tangent"].asMatrix(None)
66 Ma = EM["Adjoint"].asMatrix(None)
68 if CM is not None and CM.has_key("Tangent") and U is not None:
69 Cm = CM["Tangent"].asMatrix(None)
73 # Nombre de pas du Kalman identique au nombre de pas d'observations
74 # -----------------------------------------------------------------
75 if hasattr(Y,"stepnumber"):
76 duration = Y.stepnumber()
80 # Précalcul des inversions de B et R
81 # ----------------------------------
82 if "CostFunctionJ" in self._parameters["StoreSupplementaryCalculations"]:
85 elif self._parameters["B_scalar"] is not None:
86 BI = 1.0 / self._parameters["B_scalar"]
90 elif self._parameters["R_scalar"] is not None:
91 RI = 1.0 / self._parameters["R_scalar"]
97 self.StoredVariables["Analysis"].store( Xn.A1 )
98 if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
99 self.StoredVariables["APosterioriCovariance"].store( Pn )
101 for step in range(duration-1):
102 Ynpu = numpy.asmatrix(numpy.ravel( Y[step+1] )).T
105 if hasattr(U,"store") and len(U)>1:
106 Un = numpy.asmatrix(numpy.ravel( U[step] )).T
107 elif hasattr(U,"store") and len(U)==1:
108 Un = numpy.asmatrix(numpy.ravel( U[0] )).T
110 Un = numpy.asmatrix(numpy.ravel( U )).T
114 if self._parameters["EstimationType"] == "State" and Cm is not None and Un is not None:
115 Xn_predicted = Mt * Xn + Cm * Un
117 Xn_predicted = Mt * Xn
118 Pn_predicted = Mt * Pn * Ma + Q
120 if self._parameters["EstimationType"] == "Parameters" and Cm is not None and Un is not None:
121 d = Ynpu - Ht * Xn_predicted - Cm * Un
123 d = Ynpu - Ht * Xn_predicted
124 K = Pn_predicted * Ha * (Ht * Pn_predicted * Ha + R).I
125 Xn = Xn_predicted + K * d
126 Pn = Pn_predicted - K * Ht * Pn_predicted
128 self.StoredVariables["Analysis"].store( Xn.A1 )
129 if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
130 self.StoredVariables["Innovation"].store( numpy.ravel( d.A1 ) )
131 if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
132 self.StoredVariables["APosterioriCovariance"].store( Pn )
133 if "CostFunctionJ" in self._parameters["StoreSupplementaryCalculations"]:
134 Jb = 0.5 * (Xn - Xb).T * BI * (Xn - Xb)
135 Jo = 0.5 * d.T * RI * d
136 J = float( Jb ) + float( Jo )
137 self.StoredVariables["CostFunctionJb"].store( Jb )
138 self.StoredVariables["CostFunctionJo"].store( Jo )
139 self.StoredVariables["CostFunctionJ" ].store( J )
141 logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
142 logging.debug("%s Terminé"%self._name)
146 # ==============================================================================
147 if __name__ == "__main__":
148 print '\n AUTODIAGNOSTIC \n'