Salome HOME
Documentation and reporting improvements
[modules/adao.git] / src / daComposant / daAlgorithms / ExtendedBlue.py
index f609c092956bba427ec26a9d948dc8d9e58d6466..5b1deee66f60ae0b436244e4c70ff1fc8ae2aa05 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 #
-# Copyright (C) 2008-2018 EDF R&D
+# Copyright (C) 2008-2021 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,7 +21,7 @@
 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
 
 import logging
-from daCore import BasicObjects
+from daCore import BasicObjects, NumericObjects
 import numpy
 
 # ==============================================================================
@@ -40,25 +40,32 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             typecast = tuple,
             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
             listval  = [
+                "Analysis",
                 "APosterioriCorrelations",
                 "APosterioriCovariance",
                 "APosterioriStandardDeviations",
                 "APosterioriVariances",
                 "BMA",
-                "OMA",
-                "OMB",
-                "CurrentState",
                 "CostFunctionJ",
+                "CostFunctionJAtCurrentOptimum",
                 "CostFunctionJb",
+                "CostFunctionJbAtCurrentOptimum",
                 "CostFunctionJo",
+                "CostFunctionJoAtCurrentOptimum",
+                "CurrentOptimum",
+                "CurrentState",
                 "Innovation",
+                "MahalanobisConsistency",
+                "OMA",
+                "OMB",
+                "SampledStateForQuantiles",
                 "SigmaBck2",
                 "SigmaObs2",
-                "MahalanobisConsistency",
-                "SimulationQuantiles",
                 "SimulatedObservationAtBackground",
+                "SimulatedObservationAtCurrentOptimum",
                 "SimulatedObservationAtCurrentState",
                 "SimulatedObservationAtOptimum",
+                "SimulationQuantiles",
                 ]
             )
         self.defineRequiredParameter(
@@ -85,15 +92,24 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             name     = "SimulationForQuantiles",
             default  = "Linear",
             typecast = str,
-            message  = "Type de simulation pour l'estimation des quantiles",
+            message  = "Type de simulation en estimation des quantiles",
             listval  = ["Linear", "NonLinear"]
             )
+        self.defineRequiredParameter( # Pas de type
+            name     = "StateBoundsForQuantiles",
+            message  = "Liste des paires de bornes pour les états utilisés en estimation des quantiles",
+            )
         self.requireInputArguments(
             mandatory= ("Xb", "Y", "HO", "R", "B"),
             )
+        self.setAttributes(tags=(
+            "DataAssimilation",
+            "NonLinear",
+            "Filter",
+            ))
 
     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
-        self._pre_run(Parameters, Xb, Y, R, B, Q)
+        self._pre_run(Parameters, Xb, Y, U, HO, EM, CM, R, B, Q)
         #
         Hm = HO["Tangent"].asMatrix(Xb)
         Hm = Hm.reshape(Y.size,Xb.size) # ADAO & check shape
@@ -125,29 +141,34 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         # Calcul de la matrice de gain et de l'analyse
         # --------------------------------------------
         if Y.size <= Xb.size:
-            _A = R + Hm * B * Ha
+            _A = R + numpy.dot(Hm, B * Ha)
             _u = numpy.linalg.solve( _A , d )
             Xa = Xb + B * Ha * _u
         else:
-            _A = BI + Ha * RI * Hm
-            _u = numpy.linalg.solve( _A , Ha * RI * d )
+            _A = BI + numpy.dot(Ha, RI * Hm)
+            _u = numpy.linalg.solve( _A , numpy.dot(Ha, RI * d) )
             Xa = Xb + _u
         self.StoredVariables["Analysis"].store( Xa.A1 )
         #
         # Calcul de la fonction coût
         # --------------------------
         if self._parameters["StoreInternalVariables"] or \
-            self._toStore("CostFunctionJ") or \
+            self._toStore("CostFunctionJ")  or self._toStore("CostFunctionJAtCurrentOptimum") or \
+            self._toStore("CostFunctionJb") or self._toStore("CostFunctionJbAtCurrentOptimum") or \
+            self._toStore("CostFunctionJo") or self._toStore("CostFunctionJoAtCurrentOptimum") or \
             self._toStore("OMA") or \
             self._toStore("SigmaObs2") or \
             self._toStore("MahalanobisConsistency") or \
+            self._toStore("SimulatedObservationAtCurrentOptimum") or \
             self._toStore("SimulatedObservationAtCurrentState") or \
             self._toStore("SimulatedObservationAtOptimum") or \
             self._toStore("SimulationQuantiles"):
             HXa  = numpy.matrix(numpy.ravel( H( Xa ) )).T
             oma = Y - HXa
         if self._parameters["StoreInternalVariables"] or \
-            self._toStore("CostFunctionJ") or \
+            self._toStore("CostFunctionJ")  or self._toStore("CostFunctionJAtCurrentOptimum") or \
+            self._toStore("CostFunctionJb") or self._toStore("CostFunctionJbAtCurrentOptimum") or \
+            self._toStore("CostFunctionJo") or self._toStore("CostFunctionJoAtCurrentOptimum") or \
             self._toStore("MahalanobisConsistency"):
             Jb  = float( 0.5 * (Xa - Xb).T * BI * (Xa - Xb) )
             Jo  = float( 0.5 * oma.T * RI * oma )
@@ -155,13 +176,16 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             self.StoredVariables["CostFunctionJb"].store( Jb )
             self.StoredVariables["CostFunctionJo"].store( Jo )
             self.StoredVariables["CostFunctionJ" ].store( J )
+            self.StoredVariables["CostFunctionJbAtCurrentOptimum"].store( Jb )
+            self.StoredVariables["CostFunctionJoAtCurrentOptimum"].store( Jo )
+            self.StoredVariables["CostFunctionJAtCurrentOptimum" ].store( J )
         #
         # Calcul de la covariance d'analyse
         # ---------------------------------
         if self._toStore("APosterioriCovariance") or \
             self._toStore("SimulationQuantiles"):
-            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
+            if   (Y.size <= Xb.size): K  = B * Ha * (R + numpy.dot(Hm, B * Ha)).I
+            elif (Y.size >  Xb.size): K = (BI + numpy.dot(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)))
@@ -178,6 +202,8 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         # ---------------------------------------
         if self._parameters["StoreInternalVariables"] or self._toStore("CurrentState"):
             self.StoredVariables["CurrentState"].store( numpy.ravel(Xa) )
+        if self._toStore("CurrentOptimum"):
+            self.StoredVariables["CurrentOptimum"].store( numpy.ravel(Xa) )
         if self._toStore("Innovation"):
             self.StoredVariables["Innovation"].store( numpy.ravel(d) )
         if self._toStore("BMA"):
@@ -194,34 +220,15 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         if self._toStore("MahalanobisConsistency"):
             self.StoredVariables["MahalanobisConsistency"].store( float( 2.*J/d.size ) )
         if self._toStore("SimulationQuantiles"):
-            nech = self._parameters["NumberOfSamplesForQuantiles"]
             HtM  = HO["Tangent"].asMatrix(ValueForMethodForm = Xa)
             HtM  = HtM.reshape(Y.size,Xa.size) # ADAO & check shape
-            YfQ  = None
-            for i in range(nech):
-                if self._parameters["SimulationForQuantiles"] == "Linear":
-                    dXr = numpy.matrix(numpy.random.multivariate_normal(Xa.A1,A) - Xa.A1).T
-                    dYr = numpy.matrix(numpy.ravel( HtM * dXr )).T
-                    Yr = HXa + dYr
-                elif self._parameters["SimulationForQuantiles"] == "NonLinear":
-                    Xr = numpy.matrix(numpy.random.multivariate_normal(Xa.A1,A)).T
-                    Yr = numpy.matrix(numpy.ravel( H( Xr ) )).T
-                if YfQ is None:
-                    YfQ = Yr
-                else:
-                    YfQ = numpy.hstack((YfQ,Yr))
-            YfQ.sort(axis=-1)
-            YQ = None
-            for quantile in self._parameters["Quantiles"]:
-                if not (0. <= float(quantile) <= 1.): continue
-                indice = int(nech * float(quantile) - 1./nech)
-                if YQ is None: YQ = YfQ[:,indice]
-                else:          YQ = numpy.hstack((YQ,YfQ[:,indice]))
-            self.StoredVariables["SimulationQuantiles"].store( YQ )
+            NumericObjects.QuantilesEstimations(self, A, Xa, HXa, H, HtM)
         if self._toStore("SimulatedObservationAtBackground"):
             self.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
         if self._toStore("SimulatedObservationAtCurrentState"):
             self.StoredVariables["SimulatedObservationAtCurrentState"].store( numpy.ravel(HXa) )
+        if self._toStore("SimulatedObservationAtCurrentOptimum"):
+            self.StoredVariables["SimulatedObservationAtCurrentOptimum"].store( numpy.ravel(HXa) )
         if self._toStore("SimulatedObservationAtOptimum"):
             self.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel(HXa) )
         #
@@ -230,4 +237,4 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
 
 # ==============================================================================
 if __name__ == "__main__":
-    print('\n AUTODIAGNOSTIC \n')
+    print('\n AUTODIAGNOSTIC\n')