1 #-*-coding:iso-8859-1-*-
3 # Copyright (C) 2008-2013 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, PlatformInfo
25 m = PlatformInfo.SystemUsage()
29 # ==============================================================================
30 class ElementaryAlgorithm(BasicObjects.Algorithm):
32 BasicObjects.Algorithm.__init__(self, "BLUE")
33 self.defineRequiredParameter(
34 name = "StoreInternalVariables",
37 message = "Stockage des variables internes ou intermédiaires du calcul",
39 self.defineRequiredParameter(
40 name = "StoreSupplementaryCalculations",
43 message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
44 listval = ["APosterioriCovariance", "BMA", "OMA", "OMB", "Innovation", "SigmaBck2", "SigmaObs2", "MahalanobisConsistency"]
47 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
48 logging.debug("%s Lancement"%self._name)
49 logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
51 # Paramètres de pilotage
52 # ----------------------
53 self.setParameters(Parameters)
55 # Opérateur d'observation
56 # -----------------------
57 Hm = HO["Tangent"].asMatrix(Xb)
58 Hm = Hm.reshape(Y.size,Xb.size) # ADAO & check shape
59 Ha = HO["Adjoint"].asMatrix(Xb)
60 Ha = Ha.reshape(Xb.size,Y.size) # ADAO & check shape
62 # Utilisation éventuelle d'un vecteur H(Xb) précalculé
63 # ----------------------------------------------------
64 if HO["AppliedToX"] is not None and HO["AppliedToX"].has_key("HXb"):
65 HXb = HO["AppliedToX"]["HXb"]
68 HXb = numpy.asmatrix(numpy.ravel( HXb )).T
70 # Précalcul des inversions de B et R
71 # ----------------------------------
75 # Calcul de l'innovation
76 # ----------------------
77 if Y.size != HXb.size:
78 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))
79 if max(Y.shape) != max(HXb.shape):
80 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))
83 # Calcul de la matrice de gain et de l'analyse
84 # --------------------------------------------
86 if Y.size > 100: # len(R)
88 _u = numpy.linalg.solve( _A , d )
91 K = B * Ha * (R + Hm * B * Ha).I
94 if Y.size > 100: # len(R)
95 _A = BI + Ha * RI * Hm
96 _u = numpy.linalg.solve( _A , Ha * RI * d )
99 K = (BI + Ha * RI * Hm).I * Ha * RI
101 self.StoredVariables["Analysis"].store( Xa.A1 )
103 # Calcul de la fonction coût
104 # --------------------------
105 if self._parameters["StoreInternalVariables"] or "OMA" in self._parameters["StoreSupplementaryCalculations"] or "SigmaObs2" in self._parameters["StoreSupplementaryCalculations"] or "MahalanobisConsistency" in self._parameters["StoreSupplementaryCalculations"]:
107 if self._parameters["StoreInternalVariables"] or "MahalanobisConsistency" in self._parameters["StoreSupplementaryCalculations"]:
108 Jb = 0.5 * (Xa - Xb).T * BI * (Xa - Xb)
109 Jo = 0.5 * oma.T * RI * oma
110 J = float( Jb ) + float( Jo )
111 self.StoredVariables["CostFunctionJb"].store( Jb )
112 self.StoredVariables["CostFunctionJo"].store( Jo )
113 self.StoredVariables["CostFunctionJ" ].store( J )
115 # Calcul de la covariance d'analyse
116 # ---------------------------------
117 if "APosterioriCovariance" in self._parameters["StoreSupplementaryCalculations"]:
119 if min(A.shape) != max(A.shape):
120 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)))
121 if (numpy.diag(A) < 0).any():
122 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,))
123 if logging.getLogger().level < logging.WARNING: # La verification n'a lieu qu'en debug
125 L = numpy.linalg.cholesky( A )
127 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,))
128 self.StoredVariables["APosterioriCovariance"].store( A )
130 # Calculs et/ou stockages supplémentaires
131 # ---------------------------------------
132 if "Innovation" in self._parameters["StoreSupplementaryCalculations"]:
133 self.StoredVariables["Innovation"].store( numpy.ravel(d) )
134 if "BMA" in self._parameters["StoreSupplementaryCalculations"]:
135 self.StoredVariables["BMA"].store( numpy.ravel(Xb) - numpy.ravel(Xa) )
136 if "OMA" in self._parameters["StoreSupplementaryCalculations"]:
137 self.StoredVariables["OMA"].store( numpy.ravel(oma) )
138 if "OMB" in self._parameters["StoreSupplementaryCalculations"]:
139 self.StoredVariables["OMB"].store( numpy.ravel(d) )
140 if "SigmaObs2" in self._parameters["StoreSupplementaryCalculations"]:
141 TraceR = R.trace(Y.size)
142 self.StoredVariables["SigmaObs2"].store( float( (d.T * (numpy.asmatrix(numpy.ravel(oma)).T)) ) / TraceR )
143 if "SigmaBck2" in self._parameters["StoreSupplementaryCalculations"]:
144 self.StoredVariables["SigmaBck2"].store( float( (d.T * Hm * (Xa - Xb))/(Hm * B * Hm.T).trace() ) )
145 if "MahalanobisConsistency" in self._parameters["StoreSupplementaryCalculations"]:
146 self.StoredVariables["MahalanobisConsistency"].store( float( 2.*J/d.size ) )
148 logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
149 logging.debug("%s Terminé"%self._name)
153 # ==============================================================================
154 if __name__ == "__main__":
155 print '\n AUTODIAGNOSTIC \n'