]> SALOME platform Git repositories - modules/adao.git/blob - src/daComposant/daAlgorithms/Atoms/eosg.py
Salome HOME
Documentation update and method improvement
[modules/adao.git] / src / daComposant / daAlgorithms / Atoms / eosg.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2023 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 __doc__ = """
24     Ensemble Of Simulations Generation
25 """
26 __author__ = "Jean-Philippe ARGAUD"
27
28 import numpy, logging, copy
29 import daCore.NumericObjects
30
31 # ==============================================================================
32 def eosg(selfA, Xb, HO, outputEOX = False, assumeNoFailure = True):
33     """
34     Ensemble Of Simulations Generation
35     """
36     #
37     sampleList = daCore.NumericObjects.BuildComplexSampleList(
38         selfA._parameters["SampleAsnUplet"],
39         selfA._parameters["SampleAsExplicitHyperCube"],
40         selfA._parameters["SampleAsMinMaxStepHyperCube"],
41         selfA._parameters["SampleAsMinMaxLatinHyperCube"],
42         selfA._parameters["SampleAsIndependantRandomVariables"],
43         Xb,
44         selfA._parameters["SetSeed"],
45         )
46     #
47     if hasattr(sampleList,"__len__") and len(sampleList) == 0:
48         if outputEOX: return numpy.array([[]]), numpy.array([[]])
49         else:         return numpy.array([[]])
50     #
51     if outputEOX or selfA._toStore("EnsembleOfStates"):
52         EOX = numpy.stack(tuple(copy.copy(sampleList)), axis=1)
53     #
54     # ----------
55     if selfA._parameters["SetDebug"]:
56         CUR_LEVEL = logging.getLogger().getEffectiveLevel()
57         logging.getLogger().setLevel(logging.DEBUG)
58         print("===> Beginning of evaluation, activating debug\n")
59         print("     %s\n"%("-"*75,))
60     #
61     Hm = HO["Direct"].appliedTo
62     if assumeNoFailure:
63         EOS = Hm(
64             sampleList,
65             argsAsSerie = True,
66             returnSerieAsArrayMatrix = True,
67             )
68     else:
69         try:
70             EOS = Hm(
71                 sampleList,
72                 argsAsSerie = True,
73                 returnSerieAsArrayMatrix = True,
74                 )
75         except: # Reprise séquentielle sur erreur de calcul
76             EOS, __s = [], 1
77             for state in sampleList:
78                 if numpy.any(numpy.isin((None, numpy.nan), state)):
79                     EOS.append( () ) # Résultat vide
80                 else:
81                     try:
82                         EOS.append( Hm(state) )
83                         __s = numpy.asarray(EOS[-1]).size
84                     except:
85                         EOS.append( () ) # Résultat vide
86             for i, resultat in enumerate(EOS):
87                 if len(resultat) == 0: # Résultat vide
88                     EOS[i] = numpy.nan*numpy.ones(__s)
89             EOS = numpy.stack(EOS, axis=1)
90     #
91     if selfA._parameters["SetDebug"]:
92         print("\n     %s\n"%("-"*75,))
93         print("===> End evaluation, deactivating debug if necessary\n")
94         logging.getLogger().setLevel(CUR_LEVEL)
95     # ----------
96     #
97     if selfA._toStore("EnsembleOfStates"):
98         if EOX.shape[1] != EOS.shape[1]:
99             raise ValueError("Numbers of states (=%i) and snapshots (=%i) has to be the same!"%(EOX.shape[1], EOS.shape[1]))
100         selfA.StoredVariables["EnsembleOfStates"].store( EOX )
101     if selfA._toStore("EnsembleOfSimulations"):
102         selfA.StoredVariables["EnsembleOfSimulations"].store( EOS )
103     #
104     if outputEOX:
105         if EOX.shape[1] != EOS.shape[1]:
106             raise ValueError("Numbers of states (=%i) and snapshots (=%i) has to be the same!"%(EOX.shape[1], EOS.shape[1]))
107         return EOX, EOS
108     else:
109         return EOS
110
111 # ==============================================================================
112 if __name__ == "__main__":
113     print('\n AUTODIAGNOSTIC\n')