Salome HOME
Minor source update for OM compatibility
[modules/adao.git] / src / daComposant / daCore / BasicObjects.py
index f36c2fe8805ae25d7315b43ca42c2a4d9bbe551c..03cfe56904962bb37f57869f4d7d1b09565d93db 100644 (file)
@@ -452,21 +452,37 @@ class FullOperator(object):
         #
         __Parameters = {}
         if (asDict is not None) and isinstance(asDict, dict):
-            __Parameters.update( asDict )
-        # Priorité à EnableMultiProcessingInDerivatives=True
-        if "EnableMultiProcessing" in __Parameters and __Parameters["EnableMultiProcessing"]:
-            __Parameters["EnableMultiProcessingInDerivatives"] = True
-            __Parameters["EnableMultiProcessingInEvaluation"]  = False
-        if "EnableMultiProcessingInDerivatives" not in __Parameters:
-            __Parameters["EnableMultiProcessingInDerivatives"]  = False
-        if __Parameters["EnableMultiProcessingInDerivatives"]:
-            __Parameters["EnableMultiProcessingInEvaluation"]  = False
-        if "EnableMultiProcessingInEvaluation" not in __Parameters:
-            __Parameters["EnableMultiProcessingInEvaluation"]  = False
+            __Parameters.update( asDict )  # Copie mémoire
+        # Deprecated parameters
+        __Parameters = self.__deprecateOpt(
+            collection = __Parameters,
+            oldn = "EnableMultiProcessing",
+            newn = "EnableWiseParallelism",
+        )
+        __Parameters = self.__deprecateOpt(
+            collection = __Parameters,
+            oldn = "EnableMultiProcessingInEvaluation",
+            newn = "EnableParallelEvaluations",
+        )
+        __Parameters = self.__deprecateOpt(
+            collection = __Parameters,
+            oldn = "EnableMultiProcessingInDerivatives",
+            newn = "EnableParallelDerivatives",
+        )
+        # Priorité à EnableParallelDerivatives=True
+        if "EnableWiseParallelism" in __Parameters and __Parameters["EnableWiseParallelism"]:
+            __Parameters["EnableParallelDerivatives"] = True
+            __Parameters["EnableParallelEvaluations"]  = False
+        if "EnableParallelDerivatives" not in __Parameters:
+            __Parameters["EnableParallelDerivatives"]  = False
+        if __Parameters["EnableParallelDerivatives"]:
+            __Parameters["EnableParallelEvaluations"]  = False
+        if "EnableParallelEvaluations" not in __Parameters:
+            __Parameters["EnableParallelEvaluations"]  = False
         if "withIncrement" in __Parameters:  # Temporaire
             __Parameters["DifferentialIncrement"] = __Parameters["withIncrement"]
-        # Le défaut est équivalent à "ReducedOverallRequirements"
-        __reduceM, __avoidRC = True, True
+        #
+        __reduceM, __avoidRC = True, True  # Défaut
         if performancePrf is not None:
             if performancePrf == "ReducedAmountOfCalculation":
                 __reduceM, __avoidRC = False, True
@@ -474,6 +490,8 @@ class FullOperator(object):
                 __reduceM, __avoidRC = True, False
             elif performancePrf == "NoSavings":
                 __reduceM, __avoidRC = False, False
+            # "ReducedOverallRequirements" et tous les autres choix (y.c rien)
+            #  sont équivalents au défaut
         #
         if asScript is not None:
             __Matrix, __Function = None, None
@@ -557,7 +575,7 @@ class FullOperator(object):
                 avoidingRedundancy    = __Function["withAvoidingRedundancy"],
                 toleranceInRedundancy = __Function["withToleranceInRedundancy"],
                 lengthOfRedundancy    = __Function["withLengthOfRedundancy"],
-                mpEnabled             = __Function["EnableMultiProcessingInDerivatives"],
+                mpEnabled             = __Function["EnableParallelDerivatives"],
                 mpWorkers             = __Function["NumberOfProcesses"],
                 mfEnabled             = __Function["withmfEnabled"],
             )
@@ -568,7 +586,7 @@ class FullOperator(object):
                 avoidingRedundancy = __avoidRC,
                 inputAsMultiFunction = inputAsMF,
                 extraArguments = self.__extraArgs,
-                enableMultiProcess = __Parameters["EnableMultiProcessingInEvaluation"] )
+                enableMultiProcess = __Parameters["EnableParallelEvaluations"] )
             self.__FO["Tangent"] = Operator(
                 name = self.__name + "Tangent",
                 fromMethod = FDA.TangentOperator,
@@ -594,7 +612,7 @@ class FullOperator(object):
                 avoidingRedundancy = __avoidRC,
                 inputAsMultiFunction = inputAsMF,
                 extraArguments = self.__extraArgs,
-                enableMultiProcess = __Parameters["EnableMultiProcessingInEvaluation"] )
+                enableMultiProcess = __Parameters["EnableParallelEvaluations"] )
             self.__FO["Tangent"] = Operator(
                 name = self.__name + "Tangent",
                 fromMethod = __Function["Tangent"],
@@ -620,7 +638,7 @@ class FullOperator(object):
                 reducingMemoryUse = __reduceM,
                 avoidingRedundancy = __avoidRC,
                 inputAsMultiFunction = inputAsMF,
-                enableMultiProcess = __Parameters["EnableMultiProcessingInEvaluation"] )
+                enableMultiProcess = __Parameters["EnableParallelEvaluations"] )
             self.__FO["Tangent"] = Operator(
                 name = self.__name + "Tangent",
                 fromMatrix = __matrice,
@@ -675,6 +693,16 @@ class FullOperator(object):
         "x.__str__() <==> str(x)"
         return str(self.__FO)
 
+    def __deprecateOpt(self, collection: dict, oldn: str, newn: str):
+        if oldn in collection:
+            collection[newn] = collection[oldn]
+            del collection[oldn]
+            __msg  = "the parameter \"%s\" used in this case is"%(oldn,)
+            __msg += " deprecated and has to be replaced by \"%s\"."%(newn,)
+            __msg += " Please update your code."
+            warnings.warn(__msg, FutureWarning, stacklevel=50)
+        return collection
+
 # ==============================================================================
 class Algorithm(object):
     """
@@ -846,7 +874,7 @@ class Algorithm(object):
         #
         # Mise à jour des paramètres internes avec le contenu de Parameters, en
         # reprenant les valeurs par défauts pour toutes celles non définies
-        self.__setParameters(Parameters, reset=True)  # Copie
+        self.__setParameters(Parameters, reset=True)  # Copie mémoire
         for k, v in self.__variable_names_not_public.items():
             if k not in self._parameters:
                 self.__setParameters( {k: v} )
@@ -1484,7 +1512,8 @@ class AlgorithmAndParameters(object):
         "Renvoie la liste des attributs selon l'algorithme"
         return self.__algorithm.setAttributes()
 
-    def setObserver(self, __V, __O, __I, __S):
+    def setObserver(self, __V, __O, __I, __A, __S):
+        "Associe un observer à une variable unique"
         if self.__algorithm is None \
                 or isinstance(self.__algorithm, dict) \
                 or not hasattr(self.__algorithm, "StoredVariables"):
@@ -1492,7 +1521,24 @@ class AlgorithmAndParameters(object):
         if __V not in self.__algorithm:
             raise ValueError("An observer requires to be set on a variable named %s which does not exist."%__V)
         else:
-            self.__algorithm.StoredVariables[ __V ].setDataObserver( Scheduler      = __S, HookFunction   = __O, HookParameters = __I )
+            self.__algorithm.StoredVariables[ __V ].setDataObserver( HookFunction = __O, HookParameters = __I, Scheduler = __S )
+
+    def setCrossObserver(self, __V, __O, __I, __A, __S):
+        "Associe un observer à une collection ordonnée de variables"
+        if self.__algorithm is None \
+                or isinstance(self.__algorithm, dict) \
+                or not hasattr(self.__algorithm, "StoredVariables"):
+            raise ValueError("No observer can be build before choosing an algorithm.")
+        if not isinstance(__V, (list, tuple)):
+            raise ValueError("A cross observer requires to be set on a variable series which is not the case of %s."%__V)
+        if len(__V) != len(__I):
+            raise ValueError("The number of information fields has to be the same than the number of variables on which to set the observer.")
+        #
+        for __eV in __V:
+            if __eV not in self.__algorithm:
+                raise ValueError("An observer requires to be set on a variable named %s which does not exist."%__eV)
+            else:
+                self.__algorithm.StoredVariables[ __eV ].setDataObserver( HookFunction = __O, HookParameters = __I, Scheduler = __S, Order = __V, OSync = __A, DOVar = self.__algorithm.StoredVariables )
 
     def removeObserver(self, __V, __O, __A = False):
         if self.__algorithm is None \
@@ -1790,6 +1836,8 @@ class DataObserver(object):
                  asScript    = None,
                  asObsObject = None,
                  withInfo    = None,
+                 crossObs    = False,
+                 syncObs     = True,
                  scheduledBy = None,
                  withAlgo    = None ):
         """
@@ -1801,10 +1849,12 @@ class DataObserver(object):
         #
         if onVariable is None:
             raise ValueError("setting an observer has to be done over a variable name or a list of variable names, not over None.")
-        elif type(onVariable) in (tuple, list):
+        elif isinstance(onVariable, (tuple, list)):
             self.__V = tuple(map( str, onVariable ))
             if withInfo is None:
                 self.__I = self.__V
+            elif crossObs or isinstance(withInfo, (tuple, list)):
+                self.__I = withInfo
             else:
                 self.__I = (str(withInfo),) * len(self.__V)
         elif isinstance(onVariable, str):
@@ -1824,12 +1874,14 @@ class DataObserver(object):
             self.__O = __Function.getfunc()
         #
         for k in range(len(self.__V)):
-            ename = self.__V[k]
-            einfo = self.__I[k]
-            if ename not in withAlgo:
-                raise ValueError("An observer is asked to be set on a variable named %s which does not exist."%ename)
-            else:
-                withAlgo.setObserver(ename, self.__O, einfo, scheduledBy)
+            if self.__V[k] not in withAlgo:
+                raise ValueError("An observer is asked to be set on a variable named %s which does not exist."%self.__V[k])
+        #
+        if bool(crossObs):
+            withAlgo.setCrossObserver(self.__V, self.__O, self.__I, syncObs, scheduledBy)
+        else:
+            for k in range(len(self.__V)):
+                withAlgo.setObserver(self.__V[k], self.__O, self.__I[k], syncObs, scheduledBy)
 
     def __repr__(self):
         "x.__repr__() <==> repr(x)"
@@ -2335,7 +2387,10 @@ class Covariance(object):
 
     def __repr__(self):
         "x.__repr__() <==> repr(x)"
-        return repr(self.__C)
+        if isinstance(self.__C, numpy.float64):
+            return repr(float(self.__C))
+        else:
+            return repr(self.__C)
 
     def __str__(self):
         "x.__str__() <==> str(x)"