Salome HOME
Documentation and source observer improvements for checking algorithms
[modules/adao.git] / src / daComposant / daAlgorithms / ExtendedBlue.py
index 44cdf15ed91b7ce108af0d21cb47e5beb2bfd047..441c6007369dc8c6ede78fc6d4bf5f9b8cf6c4ca 100644 (file)
@@ -1,6 +1,6 @@
 #-*-coding:iso-8859-1-*-
 #
-#  Copyright (C) 2008-2014 EDF R&D
+#  Copyright (C) 2008-2015 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
@@ -21,8 +21,7 @@
 #  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
 
 # ==============================================================================
@@ -40,13 +39,15 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             default  = [],
             typecast = tuple,
             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
-            listval  = ["APosterioriCovariance", "BMA", "OMA", "OMB", "Innovation", "SigmaBck2", "SigmaObs2", "MahalanobisConsistency", "SimulationQuantiles"]
+            listval  = ["APosterioriCorrelations", "APosterioriCovariance", "APosterioriStandardDeviations", "APosterioriVariances", "BMA", "OMA", "OMB", "CurrentState", "CostFunctionJ", "Innovation", "SigmaBck2", "SigmaObs2", "MahalanobisConsistency", "SimulationQuantiles", "SimulatedObservationAtBackground", "SimulatedObservationAtCurrentState", "SimulatedObservationAtOptimum"]
             )
         self.defineRequiredParameter(
             name     = "Quantiles",
             default  = [],
             typecast = tuple,
             message  = "Liste des valeurs de quantiles",
+            minval   = 0.,
+            maxval   = 1.,
             )
         self.defineRequiredParameter(
             name     = "SetSeed",
@@ -69,8 +70,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             )
 
     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")))
+        self._pre_run()
         #
         # Paramètres de pilotage
         # ----------------------
@@ -108,34 +108,30 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         # Calcul de la matrice de gain et de l'analyse
         # --------------------------------------------
         if Y.size <= Xb.size:
-            if Y.size > 100: # len(R)
-                _A = R + Hm * B * Ha
-                _u = numpy.linalg.solve( _A , d )
-                Xa = Xb + B * Ha * _u
-            else:
-                K  = B * Ha * (R + Hm * B * Ha).I
-                Xa = Xb + K*d
+            _A = R + Hm * B * Ha
+            _u = numpy.linalg.solve( _A , d )
+            Xa = Xb + B * Ha * _u
         else:
-            if Y.size > 100: # len(R)
-                _A = BI + Ha * RI * Hm
-                _u = numpy.linalg.solve( _A , Ha * RI * d )
-                Xa = Xb + _u
-            else:
-                K = (BI + Ha * RI * Hm).I * Ha * RI
-                Xa = Xb + K*d
+            _A = BI + Ha * RI * Hm
+            _u = numpy.linalg.solve( _A , Ha * RI * d )
+            Xa = Xb + _u
         self.StoredVariables["Analysis"].store( Xa.A1 )
         #
         # Calcul de la fonction coût
         # --------------------------
         if self._parameters["StoreInternalVariables"] or \
-           "OMA" in self._parameters["StoreSupplementaryCalculations"] or \
-           "SigmaObs2" in self._parameters["StoreSupplementaryCalculations"] or \
-           "MahalanobisConsistency" in self._parameters["StoreSupplementaryCalculations"] or \
-           "SimulationQuantiles" in self._parameters["StoreSupplementaryCalculations"]:
+           "CostFunctionJ"                      in self._parameters["StoreSupplementaryCalculations"] or \
+           "OMA"                                in self._parameters["StoreSupplementaryCalculations"] or \
+           "SigmaObs2"                          in self._parameters["StoreSupplementaryCalculations"] or \
+           "MahalanobisConsistency"             in self._parameters["StoreSupplementaryCalculations"] or \
+           "SimulatedObservationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"] or \
+           "SimulatedObservationAtOptimum"      in self._parameters["StoreSupplementaryCalculations"] or \
+           "SimulationQuantiles"                in self._parameters["StoreSupplementaryCalculations"]:
             HXa  = numpy.matrix(numpy.ravel( H( Xa ) )).T
             oma = Y - HXa
         if self._parameters["StoreInternalVariables"] or \
-           "MahalanobisConsistency" in self._parameters["StoreSupplementaryCalculations"]:
+           "CostFunctionJ"                 in self._parameters["StoreSupplementaryCalculations"] or \
+           "MahalanobisConsistency"        in self._parameters["StoreSupplementaryCalculations"]:
             Jb  = 0.5 * (Xa - Xb).T * BI * (Xa - Xb)
             Jo  = 0.5 * oma.T * RI * oma
             J   = float( Jb ) + float( Jo )
@@ -146,10 +142,9 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         # Calcul de la covariance d'analyse
         # ---------------------------------
         if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"] or \
-           "SimulationQuantiles" in self._parameters["StoreSupplementaryCalculations"]:
-            if   (Y.size <= Xb.size) and (Y.size > 100): K  = B * Ha * (R + Hm * B * Ha).I
-            elif (Y.size >  Xb.size) and (Y.size > 100): K = (BI + Ha * RI * Hm).I * Ha * RI
-            else:                                        pass # K deja calcule
+           "SimulationQuantiles"   in self._parameters["StoreSupplementaryCalculations"]:
+            if   (Y.size <= Xb.size): K  = B * Ha * (R + Hm * B * Ha).I
+            elif (Y.size >  Xb.size): K = (BI + Ha * RI * Hm).I * Ha * RI
             A = B - K * Hm * B
             if min(A.shape) != max(A.shape):
                 raise ValueError("The %s a posteriori covariance matrix A is of shape %s, despites it has to be a squared matrix. There is an error in the observation operator, please check it."%(self._name,str(A.shape)))
@@ -164,6 +159,8 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         #
         # Calculs et/ou stockages supplémentaires
         # ---------------------------------------
+        if self._parameters["StoreInternalVariables"] or "CurrentState" in self._parameters["StoreSupplementaryCalculations"]:
+            self.StoredVariables["CurrentState"].store( numpy.ravel(Xa) )
         if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
             self.StoredVariables["Innovation"].store( numpy.ravel(d) )
         if "BMA" in self._parameters["StoreSupplementaryCalculations"]:
@@ -180,7 +177,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         if "MahalanobisConsistency" in self._parameters["StoreSupplementaryCalculations"]:
             self.StoredVariables["MahalanobisConsistency"].store( float( 2.*J/d.size ) )
         if "SimulationQuantiles" in self._parameters["StoreSupplementaryCalculations"]:
-            Qtls = self._parameters["Quantiles"]
+            Qtls = map(float, self._parameters["Quantiles"])
             nech = self._parameters["NumberOfSamplesForQuantiles"]
             HtM  = HO["Tangent"].asMatrix(ValueForMethodForm = Xa)
             HtM  = HtM.reshape(Y.size,Xa.size) # ADAO & check shape
@@ -205,11 +202,14 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 if YQ is None: YQ = YfQ[:,indice]
                 else:          YQ = numpy.hstack((YQ,YfQ[:,indice]))
             self.StoredVariables["SimulationQuantiles"].store( YQ )
+        if "SimulatedObservationAtBackground" in self._parameters["StoreSupplementaryCalculations"]:
+            self.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
+        if "SimulatedObservationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"]:
+            self.StoredVariables["SimulatedObservationAtCurrentState"].store( numpy.ravel(HXa) )
+        if "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
+            self.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
         #
-        logging.debug("%s Nombre d'évaluation(s) de l'opérateur d'observation direct/tangent/adjoint : %i/%i/%i"%(self._name, HO["Direct"].nbcalls(0),HO["Tangent"].nbcalls(0),HO["Adjoint"].nbcalls(0)))
-        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
 
 # ==============================================================================