1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2017 EDF R&D
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
24 from daCore import BasicObjects
27 # ==============================================================================
28 class ElementaryAlgorithm(BasicObjects.Algorithm):
30 BasicObjects.Algorithm.__init__(self, "ENSEMBLEBLUE")
31 self.defineRequiredParameter(
32 name = "StoreInternalVariables",
35 message = "Stockage des variables internes ou intermédiaires du calcul",
37 self.defineRequiredParameter(
38 name = "StoreSupplementaryCalculations",
41 message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
42 listval = ["CurrentState", "Innovation", "SimulatedObservationAtBackground", "SimulatedObservationAtCurrentState", "SimulatedObservationAtOptimum"]
44 self.defineRequiredParameter(
46 typecast = numpy.random.seed,
47 message = "Graine fixée pour le générateur aléatoire",
49 self.requireInputArguments(
50 mandatory= ("Xb", "Y", "HO", "R", "B" ),
53 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
54 self._pre_run(Parameters, Xb, Y, R, B, Q)
56 # Précalcul des inversions de B et R
57 # ----------------------------------
61 # Nombre d'ensemble pour l'ébauche
62 # --------------------------------
63 nb_ens = Xb.stepnumber()
65 # Construction de l'ensemble des observations, par génération a partir
66 # de la diagonale de R
67 # --------------------------------------------------------------------
68 DiagonaleR = R.diag(Y.size)
69 EnsembleY = numpy.zeros([Y.size,nb_ens])
70 for npar in range(DiagonaleR.size):
71 bruit = numpy.random.normal(0,DiagonaleR[npar],nb_ens)
72 EnsembleY[npar,:] = Y[npar] + bruit
73 EnsembleY = numpy.matrix(EnsembleY)
75 # Initialisation des opérateurs d'observation et de la matrice gain
76 # -----------------------------------------------------------------
77 Hm = HO["Tangent"].asMatrix(None)
78 Hm = Hm.reshape(Y.size,Xb[0].size) # ADAO & check shape
79 Ha = HO["Adjoint"].asMatrix(None)
80 Ha = Ha.reshape(Xb[0].size,Y.size) # ADAO & check shape
82 # Calcul de la matrice de gain dans l'espace le plus petit et de l'analyse
83 # ------------------------------------------------------------------------
84 if Y.size <= Xb[0].size:
85 K = B * Ha * (R + Hm * B * Ha).I
87 K = (BI + Ha * RI * Hm).I * Ha * RI
89 # Calcul du BLUE pour chaque membre de l'ensemble
90 # -----------------------------------------------
91 for iens in range(nb_ens):
93 if "SimulatedObservationAtBackground" in self._parameters["StoreSupplementaryCalculations"]:
94 self.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
95 d = EnsembleY[:,iens] - HXb
96 if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
97 self.StoredVariables["Innovation"].store( numpy.ravel(d) )
99 self.StoredVariables["CurrentState"].store( Xa )
100 if "SimulatedObservationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"]:
101 self.StoredVariables["SimulatedObservationAtCurrentState"].store( Hm * Xa )
103 # Fabrication de l'analyse
104 # ------------------------
105 Members = self.StoredVariables["CurrentState"][-nb_ens:]
106 Xa = numpy.matrix( Members ).mean(axis=0)
107 self.StoredVariables["Analysis"].store( Xa.A1 )
108 if "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
109 self.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel( Hm * Xa ) )
114 # ==============================================================================
115 if __name__ == "__main__":
116 print('\n AUTODIAGNOSTIC \n')