From 99bbc9e6f43597af2d5012cf4e4615d918015349 Mon Sep 17 00:00:00 2001 From: Jean-Philippe ARGAUD Date: Fri, 5 Jul 2013 15:46:43 +0200 Subject: [PATCH] Adding ExtendedBlue algorithm --- doc/en/reference.rst | 22 +++ src/daComposant/daAlgorithms/ExtendedBlue.py | 156 ++++++++++++++++++ .../daYacsSchemaCreator/infos_daComposant.py | 7 + 3 files changed, 185 insertions(+) create mode 100644 src/daComposant/daAlgorithms/ExtendedBlue.py diff --git a/doc/en/reference.rst b/doc/en/reference.rst index 9615431..ca97218 100644 --- a/doc/en/reference.rst +++ b/doc/en/reference.rst @@ -231,6 +231,7 @@ Options and required commands for calculation algorithms .. index:: single: 3DVAR .. index:: single: Blue +.. index:: single: ExtendedBlue .. index:: single: EnsembleBlue .. index:: single: KalmanFilter .. index:: single: ExtendedKalmanFilter @@ -292,6 +293,27 @@ commands and keywords for an ADAO calculation case`_. list: ["APosterioriCovariance", "BMA", "OMA", "OMB", "Innovation", "SigmaBck2", "SigmaObs2", "MahalanobisConsistency"]. +**"ExtendedBlue"** + + *Required commands* + *"Background", "BackgroundError", + "Observation", "ObservationError", + "ObservationOperator"* + + StoreInternalVariables + This boolean key allows to store default internal variables, mainly the + current state during iterative optimization process. Be careful, this can be + a numerically costly choice in certain calculation cases. The default is + "False". + + StoreSupplementaryCalculations + This list indicates the names of the supplementary variables that can be + available at the end of the algorithm. It involves potentially costly + calculations. The default is a void list, none of these variables being + calculated and stored by default. The possible names are in the following + list: ["APosterioriCovariance", "BMA", "OMA", "OMB", "Innovation", + "SigmaBck2", "SigmaObs2", "MahalanobisConsistency"]. + **"LinearLeastSquares"** *Required commands* diff --git a/src/daComposant/daAlgorithms/ExtendedBlue.py b/src/daComposant/daAlgorithms/ExtendedBlue.py new file mode 100644 index 0000000..c4085c5 --- /dev/null +++ b/src/daComposant/daAlgorithms/ExtendedBlue.py @@ -0,0 +1,156 @@ +#-*-coding:iso-8859-1-*- +# +# Copyright (C) 2008-2013 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 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 +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# +# Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D + +import logging +from daCore import BasicObjects, PlatformInfo +m = PlatformInfo.SystemUsage() + +import numpy + +# ============================================================================== +class ElementaryAlgorithm(BasicObjects.Algorithm): + def __init__(self): + BasicObjects.Algorithm.__init__(self, "EXTENDEDBLUE") + self.defineRequiredParameter( + name = "StoreInternalVariables", + default = False, + typecast = bool, + 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 = ["APosterioriCovariance", "BMA", "OMA", "OMB", "Innovation", "SigmaBck2", "SigmaObs2", "MahalanobisConsistency"] + ) + + 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) + # + # Opérateur d'observation + # ----------------------- + H = HO["Direct"].appliedTo + Hm = HO["Tangent"].asMatrix(Xb) + Hm = Hm.reshape(Y.size,Xb.size) # ADAO & check shape + Ha = HO["Adjoint"].asMatrix(Xb) + Ha = Ha.reshape(Xb.size,Y.size) # ADAO & check shape + # + # Utilisation éventuelle d'un vecteur H(Xb) précalculé + # ---------------------------------------------------- + if HO["AppliedToX"] is not None and HO["AppliedToX"].has_key("HXb"): + HXb = HO["AppliedToX"]["HXb"] + else: + HXb = H( Xb ) + HXb = numpy.asmatrix(numpy.ravel( HXb )).T + # + # Précalcul des inversions de B et R + # ---------------------------------- + BI = B.getI() + RI = R.getI() + # + # Calcul de l'innovation + # ---------------------- + if Y.size != HXb.size: + raise ValueError("The size %i of observations Y and %i of observed calculation H(X) are different, they have to be identical."%(Y.size,HXb.size)) + if max(Y.shape) != max(HXb.shape): + raise ValueError("The shapes %s of observations Y and %s of observed calculation H(X) are different, they have to be identical."%(Y.shape,HXb.shape)) + d = Y - HXb + # + # 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 + 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 + 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"]: + oma = Y - Hm * Xa + if self._parameters["StoreInternalVariables"] 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 ) + self.StoredVariables["CostFunctionJb"].store( Jb ) + self.StoredVariables["CostFunctionJo"].store( Jo ) + self.StoredVariables["CostFunctionJ" ].store( J ) + # + # Calcul de la covariance d'analyse + # --------------------------------- + if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]: + 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))) + if (numpy.diag(A) < 0).any(): + raise ValueError("The %s a posteriori covariance matrix A has at least one negative value on its diagonal. There is an error in the observation operator, please check it."%(self._name,)) + if logging.getLogger().level < logging.WARNING: # La verification n'a lieu qu'en debug + try: + L = numpy.linalg.cholesky( A ) + except: + raise ValueError("The %s a posteriori covariance matrix A is not symmetric positive-definite. Please check your a priori covariances and your observation operator."%(self._name,)) + self.StoredVariables["APosterioriCovariance"].store( A ) + # + # Calculs et/ou stockages supplémentaires + # --------------------------------------- + if "Innovation" in self._parameters["StoreSupplementaryCalculations"]: + self.StoredVariables["Innovation"].store( numpy.ravel(d) ) + if "BMA" in self._parameters["StoreSupplementaryCalculations"]: + self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) ) + if "OMA" in self._parameters["StoreSupplementaryCalculations"]: + self.StoredVariables["OMA"].store( numpy.ravel(oma) ) + if "OMB" in self._parameters["StoreSupplementaryCalculations"]: + self.StoredVariables["OMB"].store( numpy.ravel(d) ) + if "SigmaObs2" in self._parameters["StoreSupplementaryCalculations"]: + TraceR = R.trace(Y.size) + self.StoredVariables["SigmaObs2"].store( float( (d.T * (numpy.asmatrix(numpy.ravel(oma)).T)) ) / TraceR ) + if "SigmaBck2" in self._parameters["StoreSupplementaryCalculations"]: + self.StoredVariables["SigmaBck2"].store( float( (d.T * Hm * (Xa - Xb))/(Hm * B * Hm.T).trace() ) ) + if "MahalanobisConsistency" in self._parameters["StoreSupplementaryCalculations"]: + self.StoredVariables["MahalanobisConsistency"].store( float( 2.*J/d.size ) ) + # + logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M"))) + logging.debug("%s Terminé"%self._name) + # + return 0 + +# ============================================================================== +if __name__ == "__main__": + print '\n AUTODIAGNOSTIC \n' diff --git a/src/daSalome/daYacsSchemaCreator/infos_daComposant.py b/src/daSalome/daYacsSchemaCreator/infos_daComposant.py index 8b6d787..2b901f8 100644 --- a/src/daSalome/daYacsSchemaCreator/infos_daComposant.py +++ b/src/daSalome/daYacsSchemaCreator/infos_daComposant.py @@ -60,6 +60,7 @@ FromNumpyList["Dict"] = ["Script"] AssimAlgos = [ "3DVAR", "Blue", + "ExtendedBlue", "EnsembleBlue", "KalmanFilter", "ExtendedKalmanFilter", @@ -86,6 +87,11 @@ AlgoDataRequirements["Blue"] = [ "Observation", "ObservationError", "ObservationOperator", ] +AlgoDataRequirements["ExtendedBlue"] = [ + "Background", "BackgroundError", + "Observation", "ObservationError", + "ObservationOperator", + ] AlgoDataRequirements["EnsembleBlue"] = [ "Background", "BackgroundError", "Observation", "ObservationError", @@ -144,6 +150,7 @@ AlgoDataRequirements["AdjointTest"] = [ AlgoType = {} AlgoType["3DVAR"] = "Optim" AlgoType["Blue"] = "Optim" +AlgoType["ExtendedBlue"] = "Optim" AlgoType["EnsembleBlue"] = "Optim" AlgoType["KalmanFilter"] = "Optim" AlgoType["ExtendedKalmanFilter"] = "Optim" -- 2.39.2