Salome HOME
Copyright update 2021
[samples/genericsolver.git] / src / GENERICSOLVER / GENERICSOLVER.py
index 9a95089981888ccc9787c81962b2b39993a534b5..c53c3f19935fdb43f62226b9695f97bc4228f7f4 100644 (file)
-#  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2009-2021  EDF R&D
 #
-#  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-#  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+# 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, or (at your option) any later version.
 #
-#  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.
 #
-#  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
 #
-#  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
 #
-#  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
-#
-import traceback
+
 import GENERICSOLVER_ORB__POA
 import SALOME_ComponentPy
 import SALOME_DriverPy
 
-from omniORB import CORBA
-from SALOME_NamingServicePy import *
-from LifeCycleCORBA import *
-import SALOMEDS
-import SALOMEDS_Attributes_idl
-
-
-VARIABLE_ID = 1030
-
-################################################
-
-# init ORB
-orb = CORBA.ORB_init( [''], CORBA.ORB_ID )
-
-# create naming service instance
-naming_service = SALOME_NamingServicePy_i( orb )
-
-# create life cycle CORBA instance
-lcc = LifeCycleCORBA( orb )
-
-# get study manager
-obj = naming_service.Resolve( '/myStudyManager' )
-studyManager = obj._narrow( SALOMEDS.StudyManager )
-
-################################################
-###
-# get active study
-###
-def getStudy( studyId ):
-    #studyId = getStudyId()
-    study = studyManager.GetStudyByID( studyId )
-    return study
-
-###
-# Retrieve data from selected case
-###
-def GetDataFromCase( studyId, caseEntry ):
-    theCase = {}
-    study = getStudy( 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:
-            print "GENERICSOLVER.GetDataFromCase : ERROR! no variable '%s'" % name
-            break
-        theCase[ name ] = getValueOfVariable( builder, var )
-    return theCase
-
-###
-# Add some variable to the case
-###
-def AddDataToCase( studyId, caseEntry, varName, varValue ):
-    study = getStudy( studyId )
-    case = study.FindObjectID( caseEntry )
-    builder = study.NewBuilder()
-    var = addObjectInStudy( builder, case, varName, VARIABLE_ID )
-    setValueToVariable( builder, var, varValue )
-    sg.updateObjBrowser( True )
-    pass
-
-###
-# Plays with study
-###
-def addObjectInStudy( builder, father, objname, objid ):
-    obj = getSubSObjectByName( father, objname )
-    if obj is None:
-        obj  = builder.NewObject( father )
-        attr = builder.FindOrCreateAttribute( obj, "AttributeName" )
-        attr.SetValue( objname )
-        attr = builder.FindOrCreateAttribute( obj, "AttributeLocalID" )
-        attr.SetValue( objid )
-    return obj
-
-def setValueToVariable( builder, varobj, value ):
-    attr = builder.FindOrCreateAttribute( varobj, "AttributeLocalID" )
-    objid = attr.Value()
-    if (objid == VARIABLE_ID):
-        attr = builder.FindOrCreateAttribute( varobj, "AttributeReal" )
-        attr.SetValue( value )
-    else:
-        attr = builder.FindOrCreateAttribute( varobj, "AttributeName" )
-        QMessageBox.information( sgPyQt.getDesktop(), 'Info', "Object '%s' isn't a variable. Can't set value." % attr.Value() )
-    pass
-
-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 ):
-    print "GENERICSOLVER.getSubSObjectByName Looking for sobjet named", childName
-    study = getStudy( studyId )
-    iter = study.NewChildIterator( sobjFather )
-    #builder = study.NewBuilder()
-    while iter.More():
-        sobj = iter.Value()
-        print "GENERICSOLVER.getSubSObjectByName Got sobjet named", sobj.GetName()
-        if sobj.GetName() == childName:
-            return sobj
-        iter.Next()
-        pass
-    return None
-
-################################################
 
 class GENERICSOLVER(GENERICSOLVER_ORB__POA.GENERICSOLVER_Gen,
-              SALOME_ComponentPy.SALOME_ComponentPy_i,
-              SALOME_DriverPy.SALOME_DriverPy_i):
+                    SALOME_ComponentPy.SALOME_ComponentPy_i,
+                    SALOME_DriverPy.SALOME_DriverPy_i):
+
     """
         Pour etre un composant SALOME cette classe Python
         doit avoir le nom du composant et heriter de la
@@ -149,104 +35,6 @@ class GENERICSOLVER(GENERICSOLVER_ORB__POA.GENERICSOLVER_Gen,
     """
     def __init__ ( self, orb, poa, contID, containerName, instanceName, 
                    interfaceName ):
-        print "GENERICSOLVER.__init__: ", containerName, ';', instanceName
         SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa,
-                    contID, containerName, instanceName, interfaceName, 0)
+                    contID, containerName, instanceName, interfaceName, False)
         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.case = None
-        self.wrapperDescription = ""
-
-######################################################################
-# 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 Init ( self, studyId, caseEntry, wrapperDescription ):
-        """
-        This method is an implementation for the GENERICSOLVER interface.
-        It sets the component with some deterministic parametrization.
-        """
-        print "GENERICSOLVER.Init : enter"
-        print "GENERICSOLVER.Init : studyId = %d - caseEntry = %s - wrapperDescription = %s" % ( studyId, caseEntry, wrapperDescription )
-        self.wrapperDescription = wrapperDescription
-        
-        try:
-            self.case = GetDataFromCase( studyId, caseEntry )
-        except:
-            traceback.print_exc()
-        if self.case is None:
-            return 1
-        print "GENERICSOLVER.Init : exit"
-        return 0
-
-    def Exec ( self , inPoint, outPoint ):
-        """
-        This method is an implementation for the GENERICSOLVER interface.
-        It runs the component with some new parameters compared with the deterministic ones.
-        """
-        if self.case is None :
-            print "GENERICSOLVER.Exec : Init not run"
-            return 1, outPoint
-        
-        print "GENERICSOLVER.Exec (1): inPoint  = ", inPoint
-        print "GENERICSOLVER.Exec (1): outPoint = ", outPoint
-
-        try:
-            case = dict( self.case )
-            if self.wrapperDescription != "":
-                import sys
-                print "sys.path = ", sys.path
-                import openturns.wrapper
-                wrapper = openturns.wrapper.WrapperFile.BuildWrapperFromStream( self.wrapperDescription )
-                data = wrapper.getWrapperData()
-                variableList = data.getVariableList()
-                i = 0
-                for idx in range( variableList.getSize() ):
-                    variable = variableList[ idx ]
-                    if variable.type_ == 0:
-                        print "variable %s <-> index %d" % ( variable.id_, i )
-                        case[ variable.id_ ] = inPoint[ i ]
-                        i += 1
-
-            print "Case = ", case
-            outPoint = self.BeamModel( **case )
-        except:
-            traceback.print_exc()
-            return 1, outPoint
-         
-
-        print "GENERICSOLVER.Exec (2): inPoint  = ", inPoint
-        print "GENERICSOLVER.Exec (2): outPoint = ", outPoint
-        return 0, outPoint
-
-    def Finalize ( self ):
-        """
-        This method is an implementation for the GENERICSOLVER interface.
-        It cleans everything set so far.
-        """
-        print "GENERICSOLVER.Finalize : enter"
-        print "GENERICSOLVER.Finalize : exit"
-        return 0
-######################################################################
-# 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=1., F=0., L=0., I=1. ):
-       """
-       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 )
-       print "GENERICSOLVER.Exec (2): BeamModel (E=%g, F=%g, L=%g, I=%g) = %g" % (E,F,L,I,d)
-
-       return (d,)