1 #-*-coding:iso-8859-1-*-
3 # Copyright (C) 2008-2009 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
22 Calcul de la fonction coût avec Hlin
25 __author__ = "Sophie RICCI - Octobre 2008"
27 import sys ; sys.path.insert(0, "../daCore")
31 from BasicObjects import Diagnostic
32 from AssimilationStudy import AssimilationStudy
35 # ==============================================================================
36 class ElementaryDiagnostic(Diagnostic,Persistence.OneScalar):
37 def __init__(self, name = "", unit = "", basetype = None, parameters = {}):
38 Diagnostic.__init__(self, name)
39 Persistence.OneScalar.__init__( self, name, unit, basetype = float)
40 self.__name = str( name )
42 def _formula(self, X = None, dX = None, Hlin = None, Xb=None, HXb = None, Y=None, R=None, B=None):
45 Calcul de la fonction cout
47 HX = HXb + Hlin.T * dX
48 if hasattr(HX, 'A1') :
51 Jb = 1./2. * (X - Xb).T * B.I * (X - Xb)
52 logging.info( "Partial cost function : Jb = %s"%Jb )
54 Jo = 1./2. * (Y - HX).T * R.I * (Y - HX)
55 logging.info( "Partial cost function : Jo = %s"%Jo )
58 logging.info( "Total cost function : J = Jo + Jb = %s"%J )
61 def calculate(self, x = None, dx = None, Hlin = None, xb = None, Hxb = None, yo = None, R = None, B = None , step = None):
63 Teste les arguments, active la formule de calcul et stocke le résultat
65 if (x is None) or (xb is None) or (yo is None) or (dx is None):
66 raise ValueError("Vectors x, dx, xb and yo must be given to compute J")
68 if hasattr(numpy.matrix(x), 'A1') :
69 X = numpy.matrix(x).A1
70 if hasattr(numpy.matrix(xb), 'A1') :
71 Xb = numpy.matrix(xb).A1
72 if hasattr(numpy.matrix(yo), 'A1') :
73 Y = numpy.matrix(yo).A1
77 raise ValueError("HlinT vector must be given")
79 raise ValueError("The given vector must be given")
81 if (B is None ) or (R is None ):
82 raise ValueError("The matrices B and R must be given")
84 value = self._formula(X, dX, Hlin, Xb, HXb, Y, R, B)
86 self.store( value = value, step = step )
88 #===============================================================================
89 if __name__ == "__main__":
92 D = ElementaryDiagnostic("Ma fonction cout")
94 # Vecteur de type array
95 # ---------------------
96 x = numpy.array([1., 2.])
97 dx = numpy.array([0.1, 0.2])
98 xb = numpy.array([2., 2.])
99 yo = numpy.array([5., 6.])
100 Hlin = numpy.matrix(numpy.identity(2))
104 B = numpy.matrix(numpy.identity(2))
105 R = numpy.matrix(numpy.identity(2))
107 D.calculate( x = x, dx = dx, Hlin = Hlin, xb = xb, Hxb = Hxb, yo = yo, R = R, B = B)
108 print "Le vecteur x choisi est...:", x
109 print "L ebauche xb choisie est...:", xb
110 print "Le vecteur d observation est...:", yo
113 print "La fonction cout J vaut ...: %.2e"%D.valueserie(0)
115 if (abs(D.valueserie(0) - 11.925) > 1.e-6) :
116 raise ValueError("The computation of the cost function is NOT correct")
118 print "The computation of the cost function is OK"