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, "ADJOINTTEST")
33 self.defineRequiredParameter(
34 name = "ResiduFormula",
35 default = "ScalarProduct",
37 message = "Formule de résidu utilisée",
38 listval = ["ScalarProduct"],
40 self.defineRequiredParameter(
41 name = "EpsilonMinimumExponent",
44 message = "Exposant minimal en puissance de 10 pour le multiplicateur d'incrément",
48 self.defineRequiredParameter(
49 name = "InitialDirection",
52 message = "Direction initiale de la dérivée directionnelle autour du point nominal",
54 self.defineRequiredParameter(
55 name = "AmplitudeOfInitialDirection",
58 message = "Amplitude de la direction initiale de la dérivée directionnelle autour du point nominal",
60 self.defineRequiredParameter(
62 typecast = numpy.random.seed,
63 message = "Graine fixée pour le générateur aléatoire",
65 self.defineRequiredParameter(
69 message = "Titre du tableau et de la figure",
72 def run(self, Xb=None, Y=None, H=None, M=None, R=None, B=None, Q=None, Parameters=None):
73 logging.debug("%s Lancement"%self._name)
74 logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
76 # Paramètres de pilotage
77 # ----------------------
78 self.setParameters(Parameters)
80 # Opérateur d'observation
81 # -----------------------
82 Hm = H["Direct"].appliedTo
83 Ht = H["Tangent"].appliedInXTo
84 Ha = H["Adjoint"].appliedInXTo
86 # Construction des perturbations
87 # ------------------------------
88 Perturbations = [ 10**i for i in xrange(self._parameters["EpsilonMinimumExponent"],1) ]
89 Perturbations.reverse()
91 # Calcul du point courant
92 # -----------------------
93 X = numpy.asmatrix(numpy.ravel( Xb )).T
94 NormeX = numpy.linalg.norm( X )
96 Y = numpy.asmatrix(numpy.ravel( Hm( X ) )).T
97 Y = numpy.asmatrix(numpy.ravel( Y )).T
98 NormeY = numpy.linalg.norm( Y )
100 # Fabrication de la direction de l'incrément dX
101 # ----------------------------------------------
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 # Utilisation de F(X) si aucune observation n'est donnee
115 # ------------------------------------------------------
117 # Entete des resultats
118 # --------------------
119 if self._parameters["ResiduFormula"] is "ScalarProduct":
121 On observe le residu qui est la difference de deux produits scalaires :
123 R(Alpha) = | < TangentF_X(dX) , Y > - < dX , AdjointF_X(Y) > |
125 qui doit rester constamment egal zero a la precision du calcul.
126 On prend dX0 = Normal(0,X) et dX = Alpha*dX0. F est le code de calcul.
127 Y doit etre dans l'image de F. S'il n'est pas donne, on prend Y = F(X).
132 msgs = " ====" + "="*len(self._parameters["ResultTitle"]) + "====\n"
133 msgs += " " + self._parameters["ResultTitle"] + "\n"
134 msgs += " ====" + "="*len(self._parameters["ResultTitle"]) + "====\n"
137 msg = " i Alpha ||X|| ||Y|| ||dX|| R(Alpha) "
139 msgs += "\n" + "-"*nbtirets
141 msgs += "\n" + "-"*nbtirets
145 # Boucle sur les perturbations
146 # ----------------------------
147 for i,amplitude in enumerate(Perturbations):
149 NormedX = numpy.linalg.norm( dX )
151 TangentFXdX = numpy.asmatrix( Ht( (X,dX) ) )
152 AdjointFXY = numpy.asmatrix( Ha( (X,Y) ) )
154 Residu = abs(float(numpy.dot( TangentFXdX.A1 , Y.A1 ) - numpy.dot( dX.A1 , AdjointFXY.A1 )))
156 msg = " %2i %5.0e %9.3e %9.3e %9.3e | %9.3e"%(i,amplitude,NormeX,NormeY,NormedX,Residu)
159 self.StoredVariables["CostFunctionJ"].store( Residu )
160 msgs += "\n" + "-"*nbtirets
163 # Sorties eventuelles
164 # -------------------
166 print "Results of adjoint stability check:"
169 logging.debug("%s Taille mémoire utilisée de %.1f Mo"%(self._name, m.getUsedMemory("M")))
170 logging.debug("%s Terminé"%self._name)
174 # ==============================================================================
175 if __name__ == "__main__":
176 print '\n AUTODIAGNOSTIC \n'