1 #-*-coding:iso-8859-1-*-
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, PlatformInfo
26 mpr = PlatformInfo.PlatformInfo().MachinePrecision()
28 # ==============================================================================
29 class ElementaryAlgorithm(BasicObjects.Algorithm):
31 BasicObjects.Algorithm.__init__(self, "ADJOINTTEST")
32 self.defineRequiredParameter(
33 name = "ResiduFormula",
34 default = "ScalarProduct",
36 message = "Formule de résidu utilisée",
37 listval = ["ScalarProduct"],
39 self.defineRequiredParameter(
40 name = "EpsilonMinimumExponent",
43 message = "Exposant minimal en puissance de 10 pour le multiplicateur d'incrément",
47 self.defineRequiredParameter(
48 name = "InitialDirection",
51 message = "Direction initiale de la dérivée directionnelle autour du point nominal",
53 self.defineRequiredParameter(
54 name = "AmplitudeOfInitialDirection",
57 message = "Amplitude de la direction initiale de la dérivée directionnelle autour du point nominal",
59 self.defineRequiredParameter(
61 typecast = numpy.random.seed,
62 message = "Graine fixée pour le générateur aléatoire",
64 self.defineRequiredParameter(
68 message = "Titre du tableau et de la figure",
70 self.defineRequiredParameter(
71 name = "StoreSupplementaryCalculations",
74 message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
75 listval = ["CurrentState", "Residu", "SimulatedObservationAtCurrentState"]
78 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
81 self.setParameters(Parameters)
83 Hm = HO["Direct"].appliedTo
84 Ht = HO["Tangent"].appliedInXTo
85 Ha = HO["Adjoint"].appliedInXTo
88 Perturbations = [ 10**i for i in xrange(self._parameters["EpsilonMinimumExponent"],1) ]
89 Perturbations.reverse()
91 X = numpy.asmatrix(numpy.ravel( Xb )).T
92 NormeX = numpy.linalg.norm( X )
94 Y = numpy.asmatrix(numpy.ravel( Hm( X ) )).T
95 Y = numpy.asmatrix(numpy.ravel( Y )).T
96 NormeY = numpy.linalg.norm( Y )
97 if "CurrentState" in self._parameters["StoreSupplementaryCalculations"]:
98 self.StoredVariables["CurrentState"].store( numpy.ravel(X) )
99 if "SimulatedObservationAtCurrentState" in self._parameters["StoreSupplementaryCalculations"]:
100 self.StoredVariables["SimulatedObservationAtCurrentState"].store( numpy.ravel(Y) )
102 if len(self._parameters["InitialDirection"]) == 0:
106 dX0.append( numpy.random.normal(0.,abs(v)) )
108 dX0.append( numpy.random.normal(0.,X.mean()) )
110 dX0 = numpy.asmatrix(numpy.ravel( self._parameters["InitialDirection"] ))
112 dX0 = float(self._parameters["AmplitudeOfInitialDirection"]) * numpy.matrix( dX0 ).T
114 # Entete des resultats
115 # --------------------
118 Remarque : les nombres inferieurs a %.0e (environ) representent un zero
119 a la precision machine.\n"""%mpr
120 if self._parameters["ResiduFormula"] == "ScalarProduct":
121 __entete = " i Alpha ||X|| ||Y|| ||dX|| R(Alpha) "
123 On observe le residu qui est la difference de deux produits scalaires :
125 R(Alpha) = | < TangentF_X(dX) , Y > - < dX , AdjointF_X(Y) > |
127 qui doit rester constamment egal a zero a la precision du calcul.
128 On prend dX0 = Normal(0,X) et dX = Alpha*dX0. F est le code de calcul.
129 Y doit etre dans l'image de F. S'il n'est pas donne, on prend Y = F(X).
132 if len(self._parameters["ResultTitle"]) > 0:
134 msgs += __marge + "====" + "="*len(self._parameters["ResultTitle"]) + "====\n"
135 msgs += __marge + " " + self._parameters["ResultTitle"] + "\n"
136 msgs += __marge + "====" + "="*len(self._parameters["ResultTitle"]) + "====\n"
141 __nbtirets = len(__entete)
142 msgs += "\n" + __marge + "-"*__nbtirets
143 msgs += "\n" + __marge + __entete
144 msgs += "\n" + __marge + "-"*__nbtirets
149 for i,amplitude in enumerate(Perturbations):
151 NormedX = numpy.linalg.norm( dX )
153 TangentFXdX = numpy.asmatrix( Ht( (X,dX) ) )
154 AdjointFXY = numpy.asmatrix( Ha( (X,Y) ) )
156 Residu = abs(float(numpy.dot( TangentFXdX.A1 , Y.A1 ) - numpy.dot( dX.A1 , AdjointFXY.A1 )))
158 msg = " %2i %5.0e %9.3e %9.3e %9.3e | %9.3e"%(i,amplitude,NormeX,NormeY,NormedX,Residu)
159 msgs += "\n" + __marge + msg
161 self.StoredVariables["Residu"].store( Residu )
163 msgs += "\n" + __marge + "-"*__nbtirets
166 # Sorties eventuelles
167 # -------------------
169 print "Results of adjoint check by \"%s\" formula:"%self._parameters["ResiduFormula"]
175 # ==============================================================================
176 if __name__ == "__main__":
177 print '\n AUTODIAGNOSTIC \n'