Salome HOME
Added DEVIATION component to be used as an example for new code integrations
authorRenaud Barate <renaud.barate@edf.fr>
Fri, 2 Jul 2010 15:44:56 +0000 (15:44 +0000)
committerRenaud Barate <renaud.barate@edf.fr>
Fri, 2 Jul 2010 15:44:56 +0000 (15:44 +0000)
idl/DEVIATION_Gen.idl [new file with mode: 0644]
idl/GENERICSOLVER_Gen.idl
idl/Makefile.am
resources/GENERICSOLVERCatalog.xml.in
src/GENERICSOLVER/DEVIATION.py [new file with mode: 0644]
src/GENERICSOLVER/GENERICSOLVER.py
src/GENERICSOLVER/Makefile.am

diff --git a/idl/DEVIATION_Gen.idl b/idl/DEVIATION_Gen.idl
new file mode 100644 (file)
index 0000000..c851a00
--- /dev/null
@@ -0,0 +1,85 @@
+//  Copyright (C) 2009-2010 EDF R&D
+//
+//  This library is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU Lesser General Public
+//  License as published by the Free Software Foundation; either
+//  version 2.1 of the License.
+//
+//  This library is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+//  Lesser General Public License for more details.
+//
+//  You should have received a copy of the GNU Lesser General Public
+//  License along with this library; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+//  $Id$
+//
+
+#ifndef __DEVIATION_GEN__
+#define __DEVIATION_GEN__
+
+#include "SALOME_Component.idl"
+#include "SALOMEDS.idl"
+#include "SALOME_Exception.idl"
+
+module GENERICSOLVER_ORB
+{
+
+  typedef sequence<double> Point;
+  typedef SALOMEDS::ID ID;
+  typedef sequence<string> VarList;
+
+  interface DEVIATION_Gen : Engines::Component, SALOMEDS::Driver
+  {
+
+    /**
+     * @brief Initialize the component with the deterministic variables and the lists of
+     *        probabilistic variables.
+     *
+     * The Init method prepares the component for a series of computation with
+     * the method Exec. It extracts the deterministic data from Salome study and
+     * stores this data along with the lists of input and output variables to
+     * identify them in future calls to Exec.
+     *
+     * @param inputVarList   a list of strings containing the names of the input
+     *                       variables in the same order as in subsequent calls to Exec.
+     * @param outputVarList  a list of strings containing the names of the output
+     *                       variables in the same order as they should be returned
+     *                       in subsequent calls to Exec.
+     * @param studyID        the identifier of the study containing the deterministic data
+     * @param entry          the identifier of the deterministic case within the study
+     */
+    void Init(in VarList inputVarList, in VarList outputVarList, in long studyID, in ID entry)
+      raises (SALOME::SALOME_Exception);
+
+    /**
+     * @brief Execute a computation with a given sample of variables.
+     *
+     * The Exec method realizes the computation with some parameters (within
+     * inPoint) corresponding to the variables set previously with the method
+     * Init. The result is put in outPoint in the order specified by
+     * outputVarList of Init method.
+     *
+     * @param inPoint   a vector of floating point values to be evaluated
+     * @param outPoint  the result of the computation as a vector of floating point values
+     */
+    void Exec(in Point inPoint, out Point outPoint)
+      raises (SALOME::SALOME_Exception);
+
+    /**
+     * @brief Cleanup everything that was previously set
+     *
+     * The Finalize method is in charge of cleaning everything that what set hitherto.
+     * It may be empty.
+     */
+    void Finalize()
+      raises (SALOME::SALOME_Exception);
+
+  };
+};
+
+#endif
index 1e9f67d0b68ae51e9c8cd56ebc6227ce681a01d7..00443505eda6585e0aed0ef518023730661126c9 100644 (file)
@@ -31,8 +31,6 @@ module GENERICSOLVER_ORB
 
   typedef sequence<double> Point;
   typedef SALOMEDS::ID ID;
-  typedef sequence<string> VarList;
-  typedef Engines::fileBlock pyobj;
 
   interface GENERICSOLVER_Gen : Engines::Component, SALOMEDS::Driver
   {
@@ -78,42 +76,6 @@ module GENERICSOLVER_ORB
     long Finalize()
       raises (SALOME::SALOME_Exception);
 
-
-    /**
-     * @brief Initialize the component with the deterministic variables and the lists of
-     *        probabilistic variables.
-     *
-     * The InitWithVarList method prepares the component for a series of computation
-     * with the method ExecWithVarList. It stores the deterministic variables and the
-     * lists of input and output variables to identify them in future calls to
-     * ExecWithVarList.
-     *
-     * @param inputVarList      a list of strings containing the names of the input
-     *                          variables in the same order as in subsequent calls to
-     *                          ExecWithVarList.
-     * @param outputVarList     a list of strings containing the names of the output
-     *                          variables in the same order as they should be returned
-     *                          in subsequent calls to ExecWithVarList.
-     * @param deterministicVars a python dictionary containing the deterministic
-     *                          variables (mapping variable names to variable values)
-     */
-    void InitWithVarList(in VarList inputVarList, in VarList outputVarList, in pyobj deterministicVars)
-      raises (SALOME::SALOME_Exception);
-
-    /**
-     * @brief Execute a computation with a given sample of variables.
-     *
-     * The ExecWithVarList method realizes the computation with some parameters (within
-     * inPoint) corresponding to the variables set previously with the method
-     * InitWithVarList. The result is put in outPoint in the order specified by
-     * outputVarList of InitWithVarList method.
-     *
-     * @param inPoint   a vector of floating point values to be evaluated
-     * @param outPoint  the result of the computation as a vector of floating point values
-     */
-    void ExecWithVarList(in Point inPoint, out Point outPoint)
-      raises (SALOME::SALOME_Exception);
-
   };
 };
 
index 7f910db912c410f168b7346776f034dd912c727c..e7b6396c592657db44d6d7a35e146c1e76c41a4c 100644 (file)
@@ -23,7 +23,7 @@
 #
 include $(top_srcdir)/adm_local/unix/make_common_starter.am
 
-BASEIDL_FILES = GENERICSOLVER_Gen.idl
+BASEIDL_FILES = GENERICSOLVER_Gen.idl DEVIATION_Gen.idl
 
 # This variable defines the files to be installed
 dist_salomeidl_DATA = $(BASEIDL_FILES)
index 25d5d1b7e2ad4cb47d05de913252975e1629091d..c20d05cd14ac6aee809798c499c3693eaa78209f 100644 (file)
                                                        </outParameter>
                                                </outParameter-list>
                                        </component-service>
+                               </component-service-list>
+                       </component-interface-list>
+               </component>
+               <component>
+                       <!-- Component identification -->
+                       <component-name>DEVIATION</component-name>
+                       <component-username>DEVIATION GUI</component-username>
+                       <component-type>Data</component-type>
+                       <component-author>R. Barate</component-author>
+                       <component-version>@VERSION@</component-version>
+                       <component-comment>EDF - RD</component-comment>
+                       <component-multistudy>1</component-multistudy>
+                       <component-icone>GENERICSOLVER.png</component-icone>
+                       <constraint>'linux' ~ OS</constraint>
+                       <component-interface-list>
+                               <component-interface-name>DEVIATION</component-interface-name>
+                               <component-interface-comment></component-interface-comment>
+                               <component-service-list>
                                        <component-service>
                                                <!-- service-identification -->
-                                               <service-name>InitWithVarList</service-name>
+                                               <service-name>Init</service-name>
                                                <service-author>EDF-RD</service-author>
                                                <service-version>@VERSION@</service-version>
                                                <service-comment></service-comment>
                                                                <inParameter-type>stringvec</inParameter-type>
                                                        </inParameter>
                                                        <inParameter>
-                                                               <inParameter-name>deterministicVars</inParameter-name>
-                                                               <inParameter-type>pyobj</inParameter-type>
+                                                               <inParameter-name>studyID</inParameter-name>
+                                                               <inParameter-type>long</inParameter-type>
+                                                       </inParameter>
+                                                       <inParameter>
+                                                               <inParameter-name>entry</inParameter-name>
+                                                               <inParameter-type>string</inParameter-type>
                                                        </inParameter>
                                                </inParameter-list>
                                        </component-service>
                                        <component-service>
                                                <!-- service-identification -->
-                                               <service-name>ExecWithVarList</service-name>
+                                               <service-name>Exec</service-name>
                                                <service-author>EDF-RD</service-author>
                                                <service-version>@VERSION@</service-version>
                                                <service-comment></service-comment>
                                                        </outParameter>
                                                </outParameter-list>
                                        </component-service>
+                                       <component-service>
+                                               <!-- service-identification -->
+                                               <service-name>Finalize</service-name>
+                                               <service-author>EDF-RD</service-author>
+                                               <service-version>@VERSION@</service-version>
+                                               <service-comment></service-comment>
+                                               <service-by-default>0</service-by-default>
+                                       </component-service>
                                </component-service-list>
                        </component-interface-list>
                </component>
diff --git a/src/GENERICSOLVER/DEVIATION.py b/src/GENERICSOLVER/DEVIATION.py
new file mode 100644 (file)
index 0000000..83381be
--- /dev/null
@@ -0,0 +1,200 @@
+#  Copyright (C) 2009-2010 EDF R&D
+#
+#  This library is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU Lesser General Public
+#  License as published by the Free Software Foundation; either
+#  version 2.1 of the License.
+#
+#  This library is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+#  Lesser General Public License for more details.
+#
+#  You should have received a copy of the GNU Lesser General Public
+#  License along with this library; if not, write to the Free Software
+#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+#
+#  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
+#  $Id$
+#
+
+import logging
+import threading
+import inspect
+import traceback
+
+import salome
+import GENERICSOLVER_ORB__POA
+import SALOME_ComponentPy
+import SALOME_DriverPy
+import SALOME
+
+from pal.logger import Logger
+from pal import termcolor
+logger = Logger("DEVIATION", color = termcolor.RED_FG)
+logger.setLevel(logging.INFO)
+
+VARIABLE_ID = 1030
+
+###
+# Retrieve data from selected case
+###
+def GetDataFromCase( studyId, caseEntry ):
+    theCase = {}
+    study = salome.myStudyManager.GetStudyByID( studyId )
+    case = study.FindObjectID( caseEntry )
+    builder = study.NewBuilder()
+    # Get the values of the variables and make them a list
+    for name in ("E", "F", "L", "I"):
+        var = getSubSObjectByName( studyId, case, name )
+        if var == None:
+            logger.error("GENERICSOLVER.GetDataFromCase : ERROR! no variable '%s'" % name)
+            break
+        theCase[ name ] = getValueOfVariable( builder, var )
+    return theCase
+
+###
+# Plays with study
+###
+
+def getValueOfVariable( builder, varobj ):
+    attr = builder.FindOrCreateAttribute( varobj, "AttributeLocalID" )
+    objid = attr.Value()
+    if (objid == VARIABLE_ID):
+        attr = builder.FindOrCreateAttribute( varobj, "AttributeReal" )
+        return attr.Value()
+    else:
+        attr = builder.FindOrCreateAttribute( varobj, "AttributeName" )
+        QMessageBox.information( sgPyQt.getDesktop(), 'Info', "Object '%s' isn't a variable. Can't set value." % attr.Value() )
+    return 0.
+
+def getSubSObjectByName( studyId, sobjFather, childName ):
+    logger.debug("GENERICSOLVER.getSubSObjectByName Looking for sobjet named " + childName)
+    study = salome.myStudyManager.GetStudyByID( studyId )
+    iter = study.NewChildIterator( sobjFather )
+    #builder = study.NewBuilder()
+    while iter.More():
+        sobj = iter.Value()
+        logger.debug("GENERICSOLVER.getSubSObjectByName Got sobjet named " + sobj.GetName())
+        if sobj.GetName() == childName:
+            return sobj
+        iter.Next()
+        pass
+    return None
+
+################################################
+
+class DEVIATION(GENERICSOLVER_ORB__POA.DEVIATION_Gen,
+                SALOME_ComponentPy.SALOME_ComponentPy_i,
+                SALOME_DriverPy.SALOME_DriverPy_i):
+    
+    lock = threading.Lock()
+    
+    """
+        Pour etre un composant SALOME cette classe Python
+        doit avoir le nom du composant et heriter de la
+        classe DEVIATION_Gen issue de la compilation de l'idl
+        par omniidl et de la classe SALOME_ComponentPy_i
+        qui porte les services generaux d'un composant SALOME
+    """
+    def __init__ ( self, orb, poa, contID, containerName, instanceName, 
+                   interfaceName ):
+        logger.info("DEVIATION.__init__: " + containerName + ' ; ' + instanceName)
+        SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa,
+                    contID, containerName, instanceName, interfaceName, 0)
+        SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
+        # On stocke dans l'attribut _naming_service, une reference sur
+        # le Naming Service CORBA
+        self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
+        self.inputVarList = None
+        self.outputVarList = None
+        self.evalPoint = None
+
+######################################################################
+# This is the Wrapper part of the GENERICSOLVER module, ie
+# the three following methods are used by generic controlling
+# modules like OpenTURNS in order to launch a computation.
+# The interface is declared in GENERICSOLVER_Gen.idl. The methods
+# are free to call the legacy interface (see below).
+######################################################################
+
+    def _raiseSalomeError(self):
+        message = "Error in component %s running in container %s." % (self._instanceName, self._containerName)
+        logger.exception(message)
+        message += " " + traceback.format_exc()
+        exc = SALOME.ExceptionStruct(SALOME.INTERNAL_ERROR, message,
+                                     inspect.stack()[1][1], inspect.stack()[1][2])
+        raise SALOME.SALOME_Exception(exc)
+
+    def Init(self, inputVarList, outputVarList, studyId, caseEntry):
+        """
+        This method is an example for the initialization of a computation component for
+        use with OpenTURNS in SALOME 5.1.5 and later (for YACS integration)
+        """
+        try:
+            logger.info("DEVIATION.Init: " + self._containerName +
+                        ' ; ' + self._instanceName)
+            self.inputVarList = inputVarList
+            self.outputVarList = outputVarList
+            self.evalPoint = GetDataFromCase(studyId, caseEntry)
+            logger.debug("inputVarList: %s" % self.inputVarList)
+            logger.debug("outputVarList: %s" % self.outputVarList)
+            logger.debug("evalPoint: %s" % self.evalPoint)
+        except:
+            self._raiseSalomeError()
+
+    def Exec(self, inPoint):
+        """
+        This method is an example for the execution of a computation component for
+        use with OpenTURNS in SALOME 5.1.5 and later (for YACS integration)
+        """
+        try:
+            logger.info("DEVIATION.Exec: " + self._containerName +
+                        ' ; ' + self._instanceName)
+            if self.inputVarList is None:
+                raise Exception("Init not run")
+            if len(inPoint) != len(self.inputVarList):
+                raise Exception("Size mismatch between inputVarList and point to evaluate")
+
+            logger.debug("DEVIATION.Exec (1): inPoint  = %s" % inPoint)
+            for i in range(len(self.inputVarList)):
+                self.evalPoint[self.inputVarList[i]] = inPoint[i]
+            logger.debug("evalPoint = %s" % self.evalPoint)
+
+            resDict = {}
+            resDict["dev"] = self.BeamModel(**self.evalPoint)
+
+            outPoint = []
+            for outputVar in self.outputVarList:
+                outPoint.append(resDict[outputVar])
+            logger.debug("DEVIATION.Exec (2): outPoint = %s" % outPoint)
+            return outPoint
+        except:
+            self._raiseSalomeError()
+
+    def Finalize(self):
+        """
+        This method is an implementation for the DEVIATION interface.
+        It cleans everything set so far.
+        """
+        try:
+            logger.info("DEVIATION.Finalize: " + self._containerName + ' ; ' + self._instanceName)
+        except:
+            self._raiseSalomeError()
+        
+######################################################################
+# This is the computation part of the GENERICSOLVER module, ie
+# the following method realizes what the solver is intended to do.
+# The interface of this method (and maybe other ones) is absolutely
+# free and depends on the module (legacy interface).
+######################################################################
+
+    def BeamModel(self, E, F, L, I):
+       """
+       This method implements a beam bending model based on the following formula:
+       deviation = ( Force * Length^3 ) / ( 3 * YoungModulus * InertiaSection )
+       """
+       d = (F * L * L * L) / (3. * E * I)
+       logger.debug("DEVIATION.BeamModel (E=%g, F=%g, L=%g, I=%g) = %g" % (E, F, L, I, d))
+       return d
index cb9dcba372c041c70c9aeee72d8f71c55df82bd9..ecd5da103173e124e2e84588017ba35f207deecf 100644 (file)
@@ -249,52 +249,3 @@ class GENERICSOLVER(GENERICSOLVER_ORB__POA.GENERICSOLVER_Gen,
        logger.debug("GENERICSOLVER.BeamModel (E=%g, F=%g, L=%g, I=%g) = %g" % (E,F,L,I,d))
 
        return (d,)
-
-
-
-    def InitWithVarList(self, inputVarList, outputVarList, deterministicVars):
-        """
-        This method is an example for the initialization of a computation component for
-        use with OpenTURNS in SALOME 5.1.5 and later (for YACS integration)
-        """
-        try:
-            logger.info("GENERICSOLVER.InitWithVarList: " + self._containerName +
-                        ' ; ' + self._instanceName)
-            import cPickle
-            self.inputVarList = inputVarList
-            self.outputVarList = outputVarList
-            self.evalPoint = cPickle.loads(deterministicVars)
-            logger.debug("inputVarList: %s" % self.inputVarList)
-            logger.debug("outputVarList: %s" % self.outputVarList)
-            logger.debug("evalPoint: %s" % self.evalPoint)
-        except:
-            self._raiseSalomeError()
-
-    def ExecWithVarList(self, inPoint):
-        """
-        This method is an example for the execution of a computation component for
-        use with OpenTURNS in SALOME 5.1.5 and later (for YACS integration)
-        """
-        try:
-            logger.info("GENERICSOLVER.ExecWithVarList: " + self._containerName +
-                        ' ; ' + self._instanceName)
-            if self.inputVarList is None:
-                raise Exception("InitWithVarList not run")
-            if len(inPoint) != len(self.inputVarList):
-                raise Exception("Size mismatch between inputVarList and point to evaluate")
-
-            logger.debug("GENERICSOLVER.ExecWithVarList (1): inPoint  = %s" % inPoint)
-            for i in range(len(self.inputVarList)):
-                self.evalPoint[self.inputVarList[i]] = inPoint[i]
-            logger.debug("evalPoint = %s" % self.evalPoint)
-
-            resDict = {}
-            (resDict["dev"],) = self.BeamModel(**self.evalPoint)
-
-            outPoint = []
-            for outputVar in self.outputVarList:
-                outPoint.append(resDict[outputVar])
-            logger.debug("GENERICSOLVER.ExecWithVarList (2): outPoint = %s" % outPoint)
-            return outPoint
-        except:
-            self._raiseSalomeError()
index 7ed9a8db367fb081ac7c1dca45ec43671fcaef2c..c5aa045719eb30807d8a8adbede70ce4ea385d96 100644 (file)
@@ -21,4 +21,5 @@ include $(top_srcdir)/adm_local/unix/make_common_starter.am
 
 # Scripts to be installed
 dist_salomescript_SCRIPTS = \
-       GENERICSOLVER.py
+       GENERICSOLVER.py \
+       DEVIATION.py