1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2024 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 from daCore.PlatformInfo import vfloat
26 from daAlgorithms.Atoms import ecweim
27 mpr = PlatformInfo.PlatformInfo().MachinePrecision()
28 mfp = PlatformInfo.PlatformInfo().MaximumPrecision()
30 # ==============================================================================
31 class ElementaryAlgorithm(BasicObjects.Algorithm):
34 BasicObjects.Algorithm.__init__(self, "INTERPOLATIONBYREDUCEDMODELTEST")
35 self.defineRequiredParameter(
36 name = "ReducedBasis",
38 typecast = numpy.array,
39 message = "Base réduite, 1 vecteur par colonne",
41 self.defineRequiredParameter(
42 name = "MeasurementLocations",
45 message = "Liste des indices ou noms de positions optimales de mesure selon l'ordre interne d'un vecteur de base", # noqa: E501
47 self.defineRequiredParameter(
48 name = "EnsembleOfSnapshots",
50 typecast = numpy.array,
51 message = "Ensemble de vecteurs d'état physique (snapshots), 1 état par colonne (Test Set)",
53 self.defineRequiredParameter(
57 message = "Norme d'erreur utilisée pour le critère d'optimalité des positions",
58 listval = ["L2", "Linf"]
60 self.defineRequiredParameter(
61 name = "ShowElementarySummary",
64 message = "Calcule et affiche un résumé à chaque évaluation élémentaire",
66 self.defineRequiredParameter(
67 name = "NumberOfPrintedDigits",
70 message = "Nombre de chiffres affichés pour les impressions de réels",
73 self.defineRequiredParameter(
77 message = "Titre du tableau et de la figure",
79 self.requireInputArguments(
93 def run(self, Xb=None, Y=None, U=None, HO=None, EM=None, CM=None, R=None, B=None, Q=None, Parameters=None):
94 self._pre_run(Parameters, Xb, Y, U, HO, EM, CM, R, B, Q)
96 __rb = self._parameters["ReducedBasis"]
97 __ip = self._parameters["MeasurementLocations"]
98 __eos = self._parameters["EnsembleOfSnapshots"]
99 __rdim, __nrb = __rb.shape
100 __fdim, __nsn = __eos.shape
103 raise ValueError("The dimension of each snapshot (%i) has to be equal to the dimension of each reduced basis vector (%i)."%(__fdim, __rdim)) # noqa: E501
104 if __fdim < len(__ip):
105 raise ValueError("The dimension of each snapshot (%i) has to be greater or equal to the number of optimal measurement locations (%i)."%(__fdim, len(__ip))) # noqa: E501
107 # --------------------------
108 __s = self._parameters["ShowElementarySummary"]
109 __p = self._parameters["NumberOfPrintedDigits"]
113 __flech = 3 * "=" + "> "
114 __ordre = int(math.log10(__nsn)) + 1
116 if len(self._parameters["ResultTitle"]) > 0:
117 __rt = str(self._parameters["ResultTitle"])
118 msgs += (__marge + "====" + "=" * len(__rt) + "====\n")
119 msgs += (__marge + " " + __rt + "\n")
120 msgs += (__marge + "====" + "=" * len(__rt) + "====\n")
122 msgs += (__marge + "%s\n"%self._name)
123 msgs += (__marge + "%s\n"%("=" * len(self._name),))
126 msgs += (__marge + "This test allows to analyze the quality of the interpolation of states,\n")
127 msgs += (__marge + "using a reduced basis and measurements at specified points.\n")
129 msgs += (__marge + "The output shows simple statistics related to normalized errors of the\n")
130 msgs += (__marge + "interpolation with reduced basis using pseudo-measures coming from each\n")
131 msgs += (__marge + "snapshot included in the given test set.\n")
133 msgs += (__marge + "Warning: in order to be coherent, this test has to use the same norm\n")
134 msgs += (__marge + "than the one used to build the reduced basis. The user chosen norm in\n")
135 msgs += (__marge + "this test is presently \"%s\". Check the RB building one.\n"%(self._parameters["ErrorNorm"],)) # noqa: E501
137 msgs += (__flech + "Information before launching:\n")
138 msgs += (__marge + "-----------------------------\n")
140 msgs += (__marge + "Characteristics of input data:\n")
141 msgs += (__marge + " State dimension................: %i\n")%__fdim
142 msgs += (__marge + " Dimension of RB................: %i\n")%__nrb
143 msgs += (__marge + " Number of measures locations...: %i\n")%len(__ip)
144 msgs += (__marge + " Number of snapshots to test....: %i\n")%__nsn
146 msgs += (__marge + "%s\n\n"%("-" * 75,))
148 st = "Normalized interpolation error test using \"%s\" norm for all given states:"%self._parameters["ErrorNorm"]
149 msgs += (__flech + "%s\n"%st)
150 msgs += (__marge + "%s\n"%("-" * len(st),))
153 for ns in range(__nsn):
154 # __rm = __eos[__ip, ns]
155 __im = ecweim.EIM_online(self, __rb, __eos[__ip, ns], __ip)
157 if self._parameters["ErrorNorm"] == "L2":
158 __norms = numpy.linalg.norm( __eos[:, ns] )
159 __ecart = vfloat(numpy.linalg.norm( __eos[:, ns] - __im ) / __norms )
161 __norms = numpy.linalg.norm( __eos[:, ns], ord=numpy.inf )
162 __ecart = vfloat(numpy.linalg.norm( __eos[:, ns] - __im, ord=numpy.inf ) / __norms )
166 msgs += (__marge + "State %0" + str(__ordre) + "i: error of %." + str(__p) + "e for a state norm of %." + str(__p) + "e (= %3i%s)\n")%(ns, __ecart, __norms, 100 * __ecart / __norms, "%") # noqa: E501
168 msgs += (__marge + "%s\n"%("-" * 75,))
172 msgs += (__flech + "Launching statistical summary calculation for %i states\n"%__r)
174 msgs += (__marge + "Statistical analysis of the errors Es obtained over the collection of states\n")
175 msgs += (__marge + "(Remark: numbers that are (about) under %.0e represent 0 to machine precision)\n"%mpr)
177 Yy = numpy.array( Es )
178 msgs += (__marge + "Number of evaluations...........................: %i\n")%len( Es )
180 msgs += (__marge + "Characteristics of the whole set of error outputs Es:\n")
181 msgs += (__marge + " Minimum value of the whole set of outputs.....: %." + str(__p) + "e\n")%numpy.min( Yy ) # noqa: E501
182 msgs += (__marge + " Maximum value of the whole set of outputs.....: %." + str(__p) + "e\n")%numpy.max( Yy ) # noqa: E501
183 msgs += (__marge + " Mean of vector of the whole set of outputs....: %." + str(__p) + "e\n")%numpy.mean( Yy, dtype=mfp ) # noqa: E501
184 msgs += (__marge + " Standard error of the whole set of outputs....: %." + str(__p) + "e\n")%numpy.std( Yy, dtype=mfp ) # noqa: E501
186 msgs += (__marge + "%s\n"%("-" * 75,))
189 msgs += (__marge + "End of the \"%s\" verification\n\n"%self._name)
190 msgs += (__marge + "%s\n"%("-" * 75,))
193 self._post_run(HO, EM)
196 # ==============================================================================
197 if __name__ == "__main__":
198 print("\n AUTODIAGNOSTIC\n")