]> SALOME platform Git repositories - modules/adao.git/blob - src/daComposant/daAlgorithms/EnsembleBlue.py
Salome HOME
6cc0cf2abf1dcbfb67502801be5ee9d732232b8e
[modules/adao.git] / src / daComposant / daAlgorithms / EnsembleBlue.py
1 #-*-coding:iso-8859-1-*-
2 #
3 # Copyright (C) 2008-2017 EDF R&D
4 #
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.
9 #
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.
14 #
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
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22
23 import logging
24 from daCore import BasicObjects
25 import numpy
26
27 # ==============================================================================
28 class ElementaryAlgorithm(BasicObjects.Algorithm):
29     def __init__(self):
30         BasicObjects.Algorithm.__init__(self, "ENSEMBLEBLUE")
31         self.defineRequiredParameter(
32             name     = "StoreInternalVariables",
33             default  = False,
34             typecast = bool,
35             message  = "Stockage des variables internes ou intermédiaires du calcul",
36             )
37         self.defineRequiredParameter(
38             name     = "StoreSupplementaryCalculations",
39             default  = [],
40             typecast = tuple,
41             message  = "Liste de calculs supplémentaires à stocker et/ou effectuer",
42             listval  = ["CurrentState", "Innovation", "SimulatedObservationAtBackground", "SimulatedObservationAtCurrentState", "SimulatedObservationAtOptimum"]
43             )
44         self.defineRequiredParameter(
45             name     = "SetSeed",
46             typecast = numpy.random.seed,
47             message  = "Graine fixée pour le générateur aléatoire",
48             )
49
50     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
51         self._pre_run(Parameters)
52         #
53         # Précalcul des inversions de B et R
54         # ----------------------------------
55         BI = B.getI()
56         RI = R.getI()
57         #
58         # Nombre d'ensemble pour l'ébauche
59         # --------------------------------
60         nb_ens = Xb.stepnumber()
61         #
62         # Construction de l'ensemble des observations, par génération a partir
63         # de la diagonale de R
64         # --------------------------------------------------------------------
65         DiagonaleR = R.diag(Y.size)
66         EnsembleY = numpy.zeros([Y.size,nb_ens])
67         for npar in range(DiagonaleR.size):
68             bruit = numpy.random.normal(0,DiagonaleR[npar],nb_ens)
69             EnsembleY[npar,:] = Y[npar] + bruit
70         EnsembleY = numpy.matrix(EnsembleY)
71         #
72         # Initialisation des opérateurs d'observation et de la matrice gain
73         # -----------------------------------------------------------------
74         Hm = HO["Tangent"].asMatrix(None)
75         Hm = Hm.reshape(Y.size,Xb[0].size) # ADAO & check shape
76         Ha = HO["Adjoint"].asMatrix(None)
77         Ha = Ha.reshape(Xb[0].size,Y.size) # ADAO & check shape
78         #
79         # Calcul de la matrice de gain dans l'espace le plus petit et de l'analyse
80         # ------------------------------------------------------------------------
81         if Y.size <= Xb[0].size:
82             K  = B * Ha * (R + Hm * B * Ha).I
83         else:
84             K = (BI + Ha * RI * Hm).I * Ha * RI
85         #
86         # Calcul du BLUE pour chaque membre de l'ensemble
87         # -----------------------------------------------
88         for iens in range(nb_ens):
89             HXb = Hm * Xb[iens]
90             if "SimulatedObservationAtBackground" in self._parameters["StoreSupplementaryCalculations"]:
91                 self.StoredVariables["SimulatedObservationAtBackground"].store( numpy.ravel(HXb) )
92             d  = EnsembleY[:,iens] - HXb
93             if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
94                 self.StoredVariables["Innovation"].store( numpy.ravel(d) )
95             Xa = Xb[iens] + K*d
96             self.StoredVariables["CurrentState"].store( Xa )
97             if "SimulatedObservationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"]:
98                 self.StoredVariables["SimulatedObservationAtCurrentState"].store( Hm * Xa )
99         #
100         # Fabrication de l'analyse
101         # ------------------------
102         Members = self.StoredVariables["CurrentState"][-nb_ens:]
103         Xa = numpy.matrix( Members ).mean(axis=0)
104         self.StoredVariables["Analysis"].store( Xa.A1 )
105         if "SimulatedObservationAtOptimum" in self._parameters["StoreSupplementaryCalculations"]:
106             self.StoredVariables["SimulatedObservationAtOptimum"].store( numpy.ravel( Hm * Xa ) )
107         #
108         self._post_run(HO)
109         return 0
110
111 # ==============================================================================
112 if __name__ == "__main__":
113     print('\n AUTODIAGNOSTIC \n')