]> SALOME platform Git repositories - modules/adao.git/blob - src/daComposant/daAlgorithms/Blue.py
Salome HOME
Improvements of inversions in algorithms
[modules/adao.git] / src / daComposant / daAlgorithms / Blue.py
1 #-*-coding:iso-8859-1-*-
2 #
3 #  Copyright (C) 2008-2011  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
22 import logging
23 from daCore import BasicObjects, PlatformInfo
24 m = PlatformInfo.SystemUsage()
25
26 import numpy
27
28 # ==============================================================================
29 class ElementaryAlgorithm(BasicObjects.Algorithm):
30     def __init__(self):
31         BasicObjects.Algorithm.__init__(self)
32         self._name = "BLUE"
33         logging.debug("%s Initialisation"%self._name)
34
35     def run(self, Xb=None, Y=None, H=None, M=None, R=None, B=None, Q=None, Parameters=None):
36         """
37         Calcul de l'estimateur BLUE (ou Kalman simple, ou Interpolation Optimale)
38         """
39         logging.debug("%s Lancement"%self._name)
40         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("Mo")))
41         #
42         Hm = H["Direct"].asMatrix()
43         Ht = H["Adjoint"].asMatrix()
44         #
45         # Utilisation éventuelle d'un vecteur H(Xb) précalculé
46         # ----------------------------------------------------
47         if H["AppliedToX"] is not None and H["AppliedToX"].has_key("HXb"):
48             logging.debug("%s Utilisation de HXb"%self._name)
49             HXb = H["AppliedToX"]["HXb"]
50         else:
51             logging.debug("%s Calcul de Hm * Xb"%self._name)
52             HXb = Hm * Xb
53         HXb = numpy.asmatrix(HXb).flatten().T
54         #
55         # Précalcul des inversions de B et R
56         # ----------------------------------
57         if B is not None:
58             BI = B.I
59         elif Parameters["B_scalar"] is not None:
60             BI = 1.0 / Parameters["B_scalar"]
61             B = Parameters["B_scalar"]
62         if R is not None:
63             RI = R.I
64         elif Parameters["R_scalar"] is not None:
65             RI = 1.0 / Parameters["R_scalar"]
66             R = Parameters["R_scalar"]
67         #
68         # Calcul de l'innovation
69         # ----------------------
70         if Y.size != HXb.size:
71             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))
72         if max(Y.shape) != max(HXb.shape):
73             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))
74         d  = Y - HXb
75         logging.debug("%s Innovation d = %s"%(self._name, d))
76         #
77         # Calcul de la matrice de gain dans l'espace le plus petit et de l'analyse
78         # ------------------------------------------------------------------------
79         if Y.size <= Xb.size:
80             logging.debug("%s Calcul de K dans l'espace des observations"%self._name)
81             K  = B * Ht * (Hm * B * Ht + R).I
82         else:
83             logging.debug("%s Calcul de K dans l'espace d'ébauche"%self._name)
84             K = (Ht * RI * Hm + BI).I * Ht * RI
85         Xa = Xb + K*d
86         logging.debug("%s Analyse Xa = %s"%(self._name, Xa))
87         #
88         # Calcul de la fonction coût
89         # --------------------------
90         Jb  = 0.5 * (Xa - Xb).T * BI * (Xa - Xb)
91         Jo  = 0.5 * d.T * RI * d
92         J   = float( Jb ) + float( Jo )
93         logging.debug("%s CostFunction Jb = %s"%(self._name, Jb))
94         logging.debug("%s CostFunction Jo = %s"%(self._name, Jo))
95         logging.debug("%s CostFunction J  = %s"%(self._name, J))
96         #
97         self.StoredVariables["Analysis"].store( Xa.A1 )
98         self.StoredVariables["Innovation"].store( d.A1 )
99         self.StoredVariables["CostFunctionJb"].store( Jb )
100         self.StoredVariables["CostFunctionJo"].store( Jo )
101         self.StoredVariables["CostFunctionJ" ].store( J )
102         #
103         logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("MB")))
104         logging.debug("%s Terminé"%self._name)
105         #
106         return 0
107
108 # ==============================================================================
109 if __name__ == "__main__":
110     print '\n AUTODIAGNOSTIC \n'