]> SALOME platform Git repositories - modules/adao.git/blob - src/daComposant/daAlgorithms/FunctionTest.py
Salome HOME
Source corrections for calculation precision control
[modules/adao.git] / src / daComposant / daAlgorithms / FunctionTest.py
1 #-*-coding:iso-8859-1-*-
2 #
3 #  Copyright (C) 2008-2015 EDF R&D
4 #
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.
9 #
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.
14 #
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
18 #
19 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 #  Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22
23 import logging
24 from daCore import BasicObjects
25 import numpy, copy
26
27 # ==============================================================================
28 class ElementaryAlgorithm(BasicObjects.Algorithm):
29     def __init__(self):
30         BasicObjects.Algorithm.__init__(self, "FUNCTIONTEST")
31         self.defineRequiredParameter(
32             name     = "NumberOfPrintedDigits",
33             default  = 5,
34             typecast = int,
35             message  = "Nombre de chiffres affichés pour les impressions de réels",
36             minval   = 0,
37             )
38         self.defineRequiredParameter(
39             name     = "NumberOfRepetition",
40             default  = 1,
41             typecast = int,
42             message  = "Nombre de fois où l'exécution de la fonction est répétée",
43             minval   = 1,
44             )
45         self.defineRequiredParameter(
46             name     = "ResultTitle",
47             default  = "",
48             typecast = str,
49             message  = "Titre du tableau et de la figure",
50             )
51         self.defineRequiredParameter(
52             name     = "SetDebug",
53             default  = False,
54             typecast = bool,
55             message  = "Activation du mode debug lors de l'exécution",
56             )
57
58     def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
59         self._pre_run()
60         #
61         self.setParameters(Parameters)
62         #
63         Hm = HO["Direct"].appliedTo
64         #
65         Xn = copy.copy( Xb )
66         #
67         # ----------
68         _p = self._parameters["NumberOfPrintedDigits"]
69         if len(self._parameters["ResultTitle"]) > 0:
70             msg  = "     ====" + "="*len(self._parameters["ResultTitle"]) + "====\n"
71             msg += "        " + self._parameters["ResultTitle"] + "\n"
72             msg += "     ====" + "="*len(self._parameters["ResultTitle"]) + "====\n"
73             print("%s"%msg)
74         #
75         msg  = "===> Information before launching:\n"
76         msg += "     -----------------------------\n"
77         msg += "     Characteristics of input vector X, internally converted:\n"
78         msg += "       Type...............: %s\n"%type( Xn )
79         msg += "       Lenght of vector...: %i\n"%max(numpy.matrix( Xn ).shape)
80         msg += ("       Minimum value......: %."+str(_p)+"e\n")%numpy.min( Xn )
81         msg += ("       Maximum value......: %."+str(_p)+"e\n")%numpy.max( Xn )
82         msg += ("       Mean of vector.....: %."+str(_p)+"e\n")%numpy.mean( Xn )
83         msg += ("       Standard error.....: %."+str(_p)+"e\n")%numpy.std( Xn )
84         msg += ("       L2 norm of vector..: %."+str(_p)+"e\n")%numpy.linalg.norm( Xn )
85         print(msg)
86         #
87         if self._parameters["SetDebug"]:
88             CUR_LEVEL = logging.getLogger().getEffectiveLevel()
89             logging.getLogger().setLevel(logging.DEBUG)
90             print("===> Beginning of evaluation, activating debug\n")
91         else:
92             print("===> Beginning of evaluation, without activating debug\n")
93         #
94         # ----------
95         HO["Direct"].disableAvoidingRedundancy()
96         # ----------
97         Ys = []
98         for i in range(self._parameters["NumberOfRepetition"]):
99             print("     %s\n"%("-"*75,))
100             if self._parameters["NumberOfRepetition"] > 1:
101                 print("===> Repetition step number %i on a total of %i\n"%(i+1,self._parameters["NumberOfRepetition"]))
102             print("===> Launching direct operator evaluation\n")
103             #
104             Yn = Hm( Xn )
105             #
106             print("\n===> End of direct operator evaluation\n")
107             #
108             msg  = ("===> Information after evaluation:\n")
109             msg += ("\n     Characteristics of simulated output vector Y=H(X), to compare to others:\n")
110             msg += ("       Type...............: %s\n")%type( Yn )
111             msg += ("       Lenght of vector...: %i\n")%max(numpy.matrix( Yn ).shape)
112             msg += ("       Minimum value......: %."+str(_p)+"e\n")%numpy.min( Yn )
113             msg += ("       Maximum value......: %."+str(_p)+"e\n")%numpy.max( Yn )
114             msg += ("       Mean of vector.....: %."+str(_p)+"e\n")%numpy.mean( Yn )
115             msg += ("       Standard error.....: %."+str(_p)+"e\n")%numpy.std( Yn )
116             msg += ("       L2 norm of vector..: %."+str(_p)+"e\n")%numpy.linalg.norm( Yn )
117             print(msg)
118             #
119             Ys.append( copy.copy( numpy.ravel(
120                 Yn
121                 ) ) )
122         # ----------
123         HO["Direct"].enableAvoidingRedundancy()
124         # ----------
125         #
126         print("     %s\n"%("-"*75,))
127         if self._parameters["SetDebug"]:
128             print("===> End evaluation, deactivating debug if necessary\n")
129             logging.getLogger().setLevel(CUR_LEVEL)
130         #
131         if self._parameters["NumberOfRepetition"] > 1:
132             msg  = ("     %s\n"%("-"*75,))
133             msg += ("\n===> Statistical analysis of the outputs obtained throught repeated evaluations\n")
134             Yy = numpy.array( Ys )
135             msg += ("\n     Characteristics of the whole set of outputs Y:\n")
136             msg += ("       Number of evaluations.........................: %i\n")%len( Ys )
137             msg += ("       Minimum value of the whole set of outputs.....: %."+str(_p)+"e\n")%numpy.min( Yy )
138             msg += ("       Maximum value of the whole set of outputs.....: %."+str(_p)+"e\n")%numpy.max( Yy )
139             msg += ("       Mean of vector of the whole set of outputs....: %."+str(_p)+"e\n")%numpy.mean( Yy )
140             msg += ("       Standard error of the whole set of outputs....: %."+str(_p)+"e\n")%numpy.std( Yy )
141             Ym = numpy.mean( numpy.array( Ys ), axis=0 )
142             msg += ("\n     Characteristics of the vector Ym, mean of the outputs Y:\n")
143             msg += ("       Size of the mean of the outputs...............: %i\n")%Ym.size
144             msg += ("       Minimum value of the mean of the outputs......: %."+str(_p)+"e\n")%numpy.min( Ym )
145             msg += ("       Maximum value of the mean of the outputs......: %."+str(_p)+"e\n")%numpy.max( Ym )
146             msg += ("       Mean of the mean of the outputs...............: %."+str(_p)+"e\n")%numpy.mean( Ym )
147             msg += ("       Standard error of the mean of the outputs.....: %."+str(_p)+"e\n")%numpy.std( Ym )
148             Ye = numpy.mean( numpy.array( Ys ) - Ym, axis=0 )
149             msg += "\n     Characteristics of the mean of the differences between the outputs Y and their mean Ym:\n"
150             msg += ("       Size of the mean of the differences...........: %i\n")%Ym.size
151             msg += ("       Minimum value of the mean of the differences..: %."+str(_p)+"e\n")%numpy.min( Ye )
152             msg += ("       Maximum value of the mean of the differences..: %."+str(_p)+"e\n")%numpy.max( Ye )
153             msg += ("       Mean of the mean of the differences...........: %."+str(_p)+"e\n")%numpy.mean( Ye )
154             msg += ("       Standard error of the mean of the differences.: %."+str(_p)+"e\n")%numpy.std( Ye )
155             msg += ("\n     %s\n"%("-"*75,))
156             print(msg)
157         #
158         self._post_run(HO)
159         return 0
160
161 # ==============================================================================
162 if __name__ == "__main__":
163     print '\n AUTODIAGNOSTIC \n'