-#-*-coding:iso-8859-1-*-
+# -*- coding: utf-8 -*-
#
-# Copyright (C) 2008-2013 EDF R&D
+# Copyright (C) 2008-2018 EDF R&D
#
-# This library is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 2.1 of the License.
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License.
#
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# Lesser General Public License for more details.
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
#
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
-# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#
-# Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
+# Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
import logging
-from daCore import BasicObjects, PlatformInfo
-m = PlatformInfo.SystemUsage()
+from daCore import BasicObjects
import numpy
# ==============================================================================
def __init__(self):
BasicObjects.Algorithm.__init__(self, "EXTENDEDKALMANFILTER")
self.defineRequiredParameter(
- name = "StoreSupplementaryCalculations",
- default = [],
- typecast = tuple,
- message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
- listval = ["APosterioriCovariance", "BMA", "Innovation"]
+ name = "ConstrainedBy",
+ default = "EstimateProjection",
+ typecast = str,
+ message = "Prise en compte des contraintes",
+ listval = ["EstimateProjection"],
)
self.defineRequiredParameter(
name = "EstimationOf",
message = "Estimation d'etat ou de parametres",
listval = ["State", "Parameters"],
)
- self.defineRequiredParameter(
- name = "ConstrainedBy",
- default = "EstimateProjection",
- typecast = str,
- message = "Prise en compte des contraintes",
- listval = ["EstimateProjection"],
- )
self.defineRequiredParameter(
name = "StoreInternalVariables",
default = False,
typecast = bool,
- message = "Stockage des variables internes ou intermédiaires du calcul",
+ message = "Stockage des variables internes ou intermédiaires du calcul",
+ )
+ self.defineRequiredParameter(
+ name = "StoreSupplementaryCalculations",
+ default = [],
+ typecast = tuple,
+ message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
+ listval = ["APosterioriCorrelations", "APosterioriCovariance", "APosterioriStandardDeviations", "APosterioriVariances", "BMA", "CurrentState", "CostFunctionJ", "CostFunctionJb", "CostFunctionJo", "Innovation"]
+ )
+ self.defineRequiredParameter( # Pas de type
+ name = "Bounds",
+ message = "Liste des valeurs de bornes",
+ )
+ self.requireInputArguments(
+ mandatory= ("Xb", "Y", "HO", "R", "B" ),
+ optional = ("U", "EM", "CM", "Q"),
)
def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
- logging.debug("%s Lancement"%self._name)
- logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
- #
- # Paramètres de pilotage
- # ----------------------
- self.setParameters(Parameters)
+ self._pre_run(Parameters, Xb, Y, R, B, Q)
#
- if self._parameters.has_key("Bounds") and (type(self._parameters["Bounds"]) is type([]) or type(self._parameters["Bounds"]) is type(())) and (len(self._parameters["Bounds"]) > 0):
- Bounds = self._parameters["Bounds"]
- logging.debug("%s Prise en compte des bornes effectuee"%(self._name,))
- else:
- Bounds = None
if self._parameters["EstimationOf"] == "Parameters":
self._parameters["StoreInternalVariables"] = True
#
- # Opérateurs
+ # Opérateurs
# ----------
if B is None:
raise ValueError("Background error covariance matrix has to be properly defined!")
if self._parameters["EstimationOf"] == "State":
M = EM["Direct"].appliedControledFormTo
#
- if CM is not None and CM.has_key("Tangent") and U is not None:
+ if CM is not None and "Tangent" in CM and U is not None:
Cm = CM["Tangent"].asMatrix(Xb)
else:
Cm = None
#
- # Nombre de pas du Kalman identique au nombre de pas d'observations
- # -----------------------------------------------------------------
+ # Nombre de pas identique au nombre de pas d'observations
+ # -------------------------------------------------------
if hasattr(Y,"stepnumber"):
duration = Y.stepnumber()
else:
duration = 2
#
- # Précalcul des inversions de B et R
+ # Précalcul des inversions de B et R
# ----------------------------------
if self._parameters["StoreInternalVariables"]:
- if B is not None:
- BI = B.I
- elif self._parameters["B_scalar"] is not None:
- BI = 1.0 / self._parameters["B_scalar"]
- #
- if R is not None:
- RI = R.I
- elif self._parameters["R_scalar"] is not None:
- RI = 1.0 / self._parameters["R_scalar"]
+ BI = B.getI()
+ RI = R.getI()
#
# Initialisation
# --------------
#
self.StoredVariables["Analysis"].store( Xn.A1 )
if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
- self.StoredVariables["APosterioriCovariance"].store( Pn )
+ self.StoredVariables["APosterioriCovariance"].store( Pn.asfullmatrix(Xn.size) )
covarianceXa = Pn
Xa = Xn
previousJMinimum = numpy.finfo(float).max
if Cm is not None and Un is not None: # Attention : si Cm est aussi dans M, doublon !
Cm = Cm.reshape(Xn.size,Un.size) # ADAO & check shape
Xn_predicted = Xn_predicted + Cm * Un
- Pn_predicted = Mt * Pn * Ma + Q
+ Pn_predicted = Q + Mt * Pn * Ma
elif self._parameters["EstimationOf"] == "Parameters":
# --- > Par principe, M = Id, Q = 0
Xn_predicted = Xn
Pn_predicted = Pn
#
- if Bounds is not None and self._parameters["ConstrainedBy"] == "EstimateProjection":
- Xn_predicted = numpy.max(numpy.hstack((Xn_predicted,numpy.asmatrix(Bounds)[:,0])),axis=1)
- Xn_predicted = numpy.min(numpy.hstack((Xn_predicted,numpy.asmatrix(Bounds)[:,1])),axis=1)
+ if self._parameters["Bounds"] is not None and self._parameters["ConstrainedBy"] == "EstimateProjection":
+ Xn_predicted = numpy.max(numpy.hstack((Xn_predicted,numpy.asmatrix(self._parameters["Bounds"])[:,0])),axis=1)
+ Xn_predicted = numpy.min(numpy.hstack((Xn_predicted,numpy.asmatrix(self._parameters["Bounds"])[:,1])),axis=1)
#
if self._parameters["EstimationOf"] == "State":
d = Ynpu - numpy.asmatrix(numpy.ravel( H( (Xn_predicted, None) ) )).T
if Cm is not None and Un is not None: # Attention : si Cm est aussi dans H, doublon !
d = d - Cm * Un
#
- K = Pn_predicted * Ha * (Ht * Pn_predicted * Ha + R).I
- Xn = Xn_predicted + K * d
- Pn = Pn_predicted - K * Ht * Pn_predicted
+ _A = R + Ht * Pn_predicted * Ha
+ _u = numpy.linalg.solve( _A , d )
+ Xn = Xn_predicted + Pn_predicted * Ha * _u
+ Kn = Pn_predicted * Ha * (R + Ht * Pn_predicted * Ha).I
+ Pn = Pn_predicted - Kn * Ht * Pn_predicted
#
self.StoredVariables["Analysis"].store( Xn.A1 )
if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
self.StoredVariables["APosterioriCovariance"].store( Pn )
if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
self.StoredVariables["Innovation"].store( numpy.ravel( d.A1 ) )
- if self._parameters["StoreInternalVariables"]:
+ if self._parameters["StoreInternalVariables"] or "CurrentState" in self._parameters["StoreSupplementaryCalculations"]:
+ self.StoredVariables["CurrentState"].store( Xn )
+ if self._parameters["StoreInternalVariables"] \
+ or "CostFunctionJ" in self._parameters["StoreSupplementaryCalculations"] \
+ or "CostFunctionJb" in self._parameters["StoreSupplementaryCalculations"] \
+ or "CostFunctionJo" in self._parameters["StoreSupplementaryCalculations"]:
Jb = 0.5 * (Xn - Xb).T * BI * (Xn - Xb)
Jo = 0.5 * d.T * RI * d
J = float( Jb ) + float( Jo )
- self.StoredVariables["CurrentState"].store( Xn.A1 )
self.StoredVariables["CostFunctionJb"].store( Jb )
self.StoredVariables["CostFunctionJo"].store( Jo )
self.StoredVariables["CostFunctionJ" ].store( J )
if "BMA" in self._parameters["StoreSupplementaryCalculations"]:
self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
#
- logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
- logging.debug("%s Terminé"%self._name)
- #
+ self._post_run(HO)
return 0
# ==============================================================================
if __name__ == "__main__":
- print '\n AUTODIAGNOSTIC \n'
+ print('\n AUTODIAGNOSTIC \n')