Salome HOME
Simplifying test for variables to store (1)
authorJean-Philippe ARGAUD <jean-philippe.argaud@edf.fr>
Fri, 14 Dec 2018 16:10:29 +0000 (17:10 +0100)
committerJean-Philippe ARGAUD <jean-philippe.argaud@edf.fr>
Fri, 14 Dec 2018 16:10:29 +0000 (17:10 +0100)
src/daComposant/daAlgorithms/ExtendedKalmanFilter.py
src/daComposant/daAlgorithms/KalmanFilter.py
src/daComposant/daCore/BasicObjects.py

index ae83f923c14140c140e8c6b24cf7d434b6566a00..2a4cb79914ed24e14bb1d5a0a93b147e40c77709 100644 (file)
@@ -64,6 +64,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 "CostFunctionJb",
                 "CostFunctionJo",
                 "Innovation",
+                "PredictedState",
                 ]
             )
         self.defineRequiredParameter( # Pas de type
@@ -83,11 +84,6 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         #
         # Opérateurs
         # ----------
-        if B is None:
-            raise ValueError("Background error covariance matrix has to be properly defined!")
-        if R is None:
-            raise ValueError("Observation error covariance matrix has to be properly defined!")
-        #
         H = HO["Direct"].appliedControledFormTo
         #
         if self._parameters["EstimationOf"] == "State":
@@ -107,7 +103,10 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         #
         # Précalcul des inversions de B et R
         # ----------------------------------
-        if self._parameters["StoreInternalVariables"]:
+        if self._parameters["StoreInternalVariables"] \
+            or self._toStore("CostFunctionJ") \
+            or self._toStore("CostFunctionJb") \
+            or self._toStore("CostFunctionJo"):
             BI = B.getI()
             RI = R.getI()
         #
@@ -117,7 +116,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         Pn = B
         #
         self.StoredVariables["Analysis"].store( Xn.A1 )
-        if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
+        if self._toStore("APosterioriCovariance"):
             self.StoredVariables["APosterioriCovariance"].store( Pn.asfullmatrix(Xn.size) )
             covarianceXa = Pn
         Xa               = Xn
@@ -179,16 +178,20 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             Pn = Pn_predicted - Kn * Ht * Pn_predicted
             #
             self.StoredVariables["Analysis"].store( Xn.A1 )
-            if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
+            if self._toStore("APosterioriCovariance"):
                 self.StoredVariables["APosterioriCovariance"].store( Pn )
-            if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
+            if self._toStore("Innovation"):
                 self.StoredVariables["Innovation"].store( numpy.ravel( d.A1 ) )
-            if self._parameters["StoreInternalVariables"] or "CurrentState" in self._parameters["StoreSupplementaryCalculations"]:
+            if self._parameters["StoreInternalVariables"] \
+                or self._toStore("CurrentState"):
                 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"]:
+                or self._toStore("PredictedState"):
+                self.StoredVariables["PredictedState"].store( Xn_predicted )
+            if self._parameters["StoreInternalVariables"] \
+                or self._toStore("CostFunctionJ") \
+                or self._toStore("CostFunctionJb") \
+                or self._toStore("CostFunctionJo"):
                 Jb  = 0.5 * (Xn - Xb).T * BI * (Xn - Xb)
                 Jo  = 0.5 * d.T * RI * d
                 J   = float( Jb ) + float( Jo )
@@ -198,7 +201,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 if J < previousJMinimum:
                     previousJMinimum  = J
                     Xa                = Xn
-                    if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
+                    if self._toStore("APosterioriCovariance"):
                         covarianceXa  = Pn
             else:
                 Xa = Xn
@@ -208,10 +211,10 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         # ----------------------------------------------------------------
         if self._parameters["EstimationOf"] == "Parameters":
             self.StoredVariables["Analysis"].store( Xa.A1 )
-            if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
+            if self._toStore("APosterioriCovariance"):
                 self.StoredVariables["APosterioriCovariance"].store( covarianceXa )
         #
-        if "BMA" in self._parameters["StoreSupplementaryCalculations"]:
+        if self._toStore("BMA"):
             self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
         #
         self._post_run(HO)
index 58bb785e7e7aca586f3c8666ee26611ae292a2c8..9e6fe0a069bdaf1b459af0f4e78c30aa9ba46dfb 100644 (file)
@@ -57,6 +57,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 "CostFunctionJb",
                 "CostFunctionJo",
                 "Innovation",
+                "PredictedState",
                 ]
             )
         self.requireInputArguments(
@@ -94,9 +95,9 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         # Précalcul des inversions de B et R
         # ----------------------------------
         if self._parameters["StoreInternalVariables"] \
-            or "CostFunctionJ" in self._parameters["StoreSupplementaryCalculations"] \
-            or "CostFunctionJb" in self._parameters["StoreSupplementaryCalculations"] \
-            or "CostFunctionJo" in self._parameters["StoreSupplementaryCalculations"]:
+            or self._toStore("CostFunctionJ") \
+            or self._toStore("CostFunctionJb") \
+            or self._toStore("CostFunctionJo"):
             BI = B.getI()
             RI = R.getI()
         #
@@ -106,7 +107,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         Pn = B
         #
         self.StoredVariables["Analysis"].store( Xn.A1 )
-        if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
+        if self._toStore("APosterioriCovariance"):
             self.StoredVariables["APosterioriCovariance"].store( Pn.asfullmatrix(Xn.size) )
             covarianceXa = Pn
         Xa               = Xn
@@ -153,17 +154,20 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
             Pn = Pn_predicted - Kn * Ht * Pn_predicted
             #
             self.StoredVariables["Analysis"].store( Xn.A1 )
-            if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
+            if self._toStore("APosterioriCovariance"):
                 self.StoredVariables["APosterioriCovariance"].store( Pn )
-            if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
+            if self._toStore("Innovation"):
                 self.StoredVariables["Innovation"].store( numpy.ravel( d.A1 ) )
             if self._parameters["StoreInternalVariables"] \
-                or "CurrentState" in self._parameters["StoreSupplementaryCalculations"]:
+                or self._toStore("CurrentState"):
                 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"]:
+                or self._toStore("PredictedState"):
+                self.StoredVariables["PredictedState"].store( Xn_predicted )
+            if self._parameters["StoreInternalVariables"] \
+                or self._toStore("CostFunctionJ") \
+                or self._toStore("CostFunctionJb") \
+                or self._toStore("CostFunctionJo"):
                 Jb  = 0.5 * (Xn - Xb).T * BI * (Xn - Xb)
                 Jo  = 0.5 * d.T * RI * d
                 J   = float( Jb ) + float( Jo )
@@ -173,7 +177,7 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
                 if J < previousJMinimum:
                     previousJMinimum  = J
                     Xa                = Xn
-                    if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
+                    if self._toStore("APosterioriCovariance"):
                         covarianceXa  = Pn
             else:
                 Xa = Xn
@@ -183,10 +187,10 @@ class ElementaryAlgorithm(BasicObjects.Algorithm):
         # ----------------------------------------------------------------
         if self._parameters["EstimationOf"] == "Parameters":
             self.StoredVariables["Analysis"].store( Xa.A1 )
-            if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
+            if self._toStore("APosterioriCovariance"):
                 self.StoredVariables["APosterioriCovariance"].store( covarianceXa )
         #
-        if "BMA" in self._parameters["StoreSupplementaryCalculations"]:
+        if self._toStore("BMA"):
             self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
         #
         self._post_run(HO)
index a6ebe423ce36f9599b84f2e41526eae533a8761d..0ea2aab9e7c18b6a8b76e52a7a1dcd8e6ff8c96d 100644 (file)
@@ -508,6 +508,7 @@ class Algorithm(object):
         self.StoredVariables["GradientOfCostFunctionJb"]             = Persistence.OneVector(name = "GradientOfCostFunctionJb")
         self.StoredVariables["GradientOfCostFunctionJo"]             = Persistence.OneVector(name = "GradientOfCostFunctionJo")
         self.StoredVariables["CurrentState"]                         = Persistence.OneVector(name = "CurrentState")
+        self.StoredVariables["PredictedState"]                       = Persistence.OneVector(name = "PredictedState")
         self.StoredVariables["Analysis"]                             = Persistence.OneVector(name = "Analysis")
         self.StoredVariables["IndexOfOptimum"]                       = Persistence.OneIndex(name = "IndexOfOptimum")
         self.StoredVariables["CurrentOptimum"]                       = Persistence.OneVector(name = "CurrentOptimum")
@@ -539,7 +540,7 @@ class Algorithm(object):
         self.__setParameters(Parameters)
         #
         # Corrections et complements
-        def __test_vvalue( argument, variable, argname):
+        def __test_vvalue(argument, variable, argname):
             if argument is None:
                 if variable in self.__required_inputs["RequiredInputValues"]["mandatory"]:
                     raise ValueError("%s %s vector %s has to be properly defined!"%(self._name,argname,variable))
@@ -549,9 +550,11 @@ class Algorithm(object):
                     logging.debug("%s %s vector %s is not set, but is not required."%(self._name,argname,variable))
             else:
                 logging.debug("%s %s vector %s is set, and its size is %i."%(self._name,argname,variable,numpy.array(argument).size))
+            return 0
         __test_vvalue( Xb, "Xb", "Background or initial state" )
         __test_vvalue( Y,  "Y",  "Observation" )
-        def __test_cvalue( argument, variable, argname):
+        #
+        def __test_cvalue(argument, variable, argname):
             if argument is None:
                 if variable in self.__required_inputs["RequiredInputValues"]["mandatory"]:
                     raise ValueError("%s %s error covariance matrix %s has to be properly defined!"%(self._name,argname,variable))
@@ -561,6 +564,7 @@ class Algorithm(object):
                     logging.debug("%s %s error covariance matrix %s is not set, but is not required."%(self._name,argname,variable))
             else:
                 logging.debug("%s %s error covariance matrix %s is set."%(self._name,argname,variable))
+            return 0
         __test_cvalue( R, "R", "Observation" )
         __test_cvalue( B, "B", "Background" )
         __test_cvalue( Q, "Q", "Evolution" )
@@ -607,6 +611,10 @@ class Algorithm(object):
         logging.debug("%s Terminé", self._name)
         return 0
 
+    def _toStore(self, key):
+        "True if in StoreSupplementaryCalculations, else False"
+        return key in self._parameters["StoreSupplementaryCalculations"]
+
     def get(self, key=None):
         """
         Renvoie l'une des variables stockées identifiée par la clé, ou le