1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2022 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()
27 mfp = PlatformInfo.PlatformInfo().MaximumPrecision()
29 # ==============================================================================
30 class ElementaryAlgorithm(BasicObjects.Algorithm):
32 BasicObjects.Algorithm.__init__(self, "PARALLELFUNCTIONTEST")
33 self.defineRequiredParameter(
34 name = "ShowElementarySummary",
37 message = "Calcule et affiche un résumé à chaque évaluation élémentaire",
39 self.defineRequiredParameter(
40 name = "NumberOfPrintedDigits",
43 message = "Nombre de chiffres affichés pour les impressions de réels",
46 self.defineRequiredParameter(
47 name = "NumberOfRepetition",
50 message = "Nombre de fois où l'exécution de la fonction est répétée",
53 self.defineRequiredParameter(
57 message = "Titre du tableau et de la figure",
59 self.defineRequiredParameter(
63 message = "Activation du mode debug lors de l'exécution",
65 self.defineRequiredParameter(
66 name = "StoreSupplementaryCalculations",
69 message = "Liste de calculs supplémentaires à stocker et/ou effectuer",
72 "SimulatedObservationAtCurrentState",
75 self.requireInputArguments(
76 mandatory= ("Xb", "HO"),
78 self.setAttributes(tags=(
82 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
83 self._pre_run(Parameters, Xb, Y, U, HO, EM, CM, R, B, Q)
85 Hm = HO["Direct"].appliedTo
90 __s = self._parameters["ShowElementarySummary"]
92 _p = self._parameters["NumberOfPrintedDigits"]
93 if len(self._parameters["ResultTitle"]) > 0:
94 __rt = str(self._parameters["ResultTitle"])
96 msgs += __marge + "====" + "="*len(__rt) + "====\n"
97 msgs += __marge + " " + __rt + "\n"
98 msgs += __marge + "====" + "="*len(__rt) + "====\n"
101 msgs = ("===> Information before launching:\n")
102 msgs += (" -----------------------------\n")
103 msgs += (" Characteristics of input vector X, internally converted:\n")
104 msgs += (" Type...............: %s\n")%type( Xn )
105 msgs += (" Lenght of vector...: %i\n")%max(numpy.ravel( Xn ).shape)
106 msgs += (" Minimum value......: %."+str(_p)+"e\n")%numpy.min( Xn )
107 msgs += (" Maximum value......: %."+str(_p)+"e\n")%numpy.max( Xn )
108 msgs += (" Mean of vector.....: %."+str(_p)+"e\n")%numpy.mean( Xn, dtype=mfp )
109 msgs += (" Standard error.....: %."+str(_p)+"e\n")%numpy.std( Xn, dtype=mfp )
110 msgs += (" L2 norm of vector..: %."+str(_p)+"e\n")%numpy.linalg.norm( Xn )
113 print(" %s\n"%("-"*75,))
114 if self._parameters["SetDebug"]:
115 CUR_LEVEL = logging.getLogger().getEffectiveLevel()
116 logging.getLogger().setLevel(logging.DEBUG)
117 print("===> Beginning of repeated evaluation, activating debug\n")
119 print("===> Beginning of repeated evaluation, without activating debug\n")
122 HO["Direct"].disableAvoidingRedundancy()
125 print(" %s\n"%("-"*75,))
127 for i in range(self._parameters["NumberOfRepetition"]):
128 if self._toStore("CurrentState"):
129 self.StoredVariables["CurrentState"].store( numpy.ravel(Xn) )
131 print("===> Launching operator parallel evaluation for %i states\n"%self._parameters["NumberOfRepetition"])
133 Ys = Hm( Xs, argsAsSerie = True )
135 print("\n===> End of operator parallel evaluation for %i states\n"%self._parameters["NumberOfRepetition"])
138 HO["Direct"].enableAvoidingRedundancy()
141 print(" %s\n"%("-"*75,))
142 if self._parameters["SetDebug"]:
143 print("===> End of repeated evaluation, deactivating debug if necessary\n")
144 logging.getLogger().setLevel(CUR_LEVEL)
146 print("===> End of repeated evaluation, without deactivating debug\n")
148 if __s or self._toStore("SimulatedObservationAtCurrentState"):
149 for i in range(self._parameters["NumberOfRepetition"]):
151 print(" %s\n"%("-"*75,))
152 if self._parameters["NumberOfRepetition"] > 1:
153 print("===> Repetition step number %i on a total of %i\n"%(i+1,self._parameters["NumberOfRepetition"]))
157 msgs = ("===> Information after evaluation:\n")
158 msgs += ("\n Characteristics of simulated output vector Y=H(X), to compare to others:\n")
159 msgs += (" Type...............: %s\n")%type( Yn )
160 msgs += (" Lenght of vector...: %i\n")%max(numpy.ravel( Yn ).shape)
161 msgs += (" Minimum value......: %."+str(_p)+"e\n")%numpy.min( Yn )
162 msgs += (" Maximum value......: %."+str(_p)+"e\n")%numpy.max( Yn )
163 msgs += (" Mean of vector.....: %."+str(_p)+"e\n")%numpy.mean( Yn, dtype=mfp )
164 msgs += (" Standard error.....: %."+str(_p)+"e\n")%numpy.std( Yn, dtype=mfp )
165 msgs += (" L2 norm of vector..: %."+str(_p)+"e\n")%numpy.linalg.norm( Yn )
167 if self._toStore("SimulatedObservationAtCurrentState"):
168 self.StoredVariables["SimulatedObservationAtCurrentState"].store( numpy.ravel(Yn) )
170 if self._parameters["NumberOfRepetition"] > 1:
171 print(" %s\n"%("-"*75,))
172 print("===> Launching statistical summary calculation for %i states\n"%self._parameters["NumberOfRepetition"])
173 msgs = (" %s\n"%("-"*75,))
174 msgs += ("\n===> Statistical analysis of the outputs obtained through parallel repeated evaluations\n")
175 msgs += ("\n (Remark: numbers that are (about) under %.0e represent 0 to machine precision)\n"%mpr)
176 Yy = numpy.array( Ys )
177 msgs += ("\n Characteristics of the whole set of outputs Y:\n")
178 msgs += (" Number of evaluations.........................: %i\n")%len( Ys )
179 msgs += (" Minimum value of the whole set of outputs.....: %."+str(_p)+"e\n")%numpy.min( Yy )
180 msgs += (" Maximum value of the whole set of outputs.....: %."+str(_p)+"e\n")%numpy.max( Yy )
181 msgs += (" Mean of vector of the whole set of outputs....: %."+str(_p)+"e\n")%numpy.mean( Yy, dtype=mfp )
182 msgs += (" Standard error of the whole set of outputs....: %."+str(_p)+"e\n")%numpy.std( Yy, dtype=mfp )
183 Ym = numpy.mean( numpy.array( Ys ), axis=0, dtype=mfp )
184 msgs += ("\n Characteristics of the vector Ym, mean of the outputs Y:\n")
185 msgs += (" Size of the mean of the outputs...............: %i\n")%Ym.size
186 msgs += (" Minimum value of the mean of the outputs......: %."+str(_p)+"e\n")%numpy.min( Ym )
187 msgs += (" Maximum value of the mean of the outputs......: %."+str(_p)+"e\n")%numpy.max( Ym )
188 msgs += (" Mean of the mean of the outputs...............: %."+str(_p)+"e\n")%numpy.mean( Ym, dtype=mfp )
189 msgs += (" Standard error of the mean of the outputs.....: %."+str(_p)+"e\n")%numpy.std( Ym, dtype=mfp )
190 Ye = numpy.mean( numpy.array( Ys ) - Ym, axis=0, dtype=mfp )
191 msgs += "\n Characteristics of the mean of the differences between the outputs Y and their mean Ym:\n"
192 msgs += (" Size of the mean of the differences...........: %i\n")%Ym.size
193 msgs += (" Minimum value of the mean of the differences..: %."+str(_p)+"e\n")%numpy.min( Ye )
194 msgs += (" Maximum value of the mean of the differences..: %."+str(_p)+"e\n")%numpy.max( Ye )
195 msgs += (" Mean of the mean of the differences...........: %."+str(_p)+"e\n")%numpy.mean( Ye, dtype=mfp )
196 msgs += (" Standard error of the mean of the differences.: %."+str(_p)+"e\n")%numpy.std( Ye, dtype=mfp )
197 msgs += ("\n %s\n"%("-"*75,))
203 # ==============================================================================
204 if __name__ == "__main__":
205 print('\n AUTODIAGNOSTIC\n')