X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FGENERICSOLVER%2FGENERICSOLVER.py;h=16f48c36ca3f47aeb60e2f257818fdcca0b186bd;hb=9424ad6fd6375ff4a4c89ebd6ffeb852bf38b841;hp=cb9dcba372c041c70c9aeee72d8f71c55df82bd9;hpb=4a890ef78d8733d7a2b57ecc2012f7b0d8a63ea4;p=samples%2Fgenericsolver.git diff --git a/src/GENERICSOLVER/GENERICSOLVER.py b/src/GENERICSOLVER/GENERICSOLVER.py index cb9dcba..16f48c3 100644 --- a/src/GENERICSOLVER/GENERICSOLVER.py +++ b/src/GENERICSOLVER/GENERICSOLVER.py @@ -1,128 +1,31 @@ -# Copyright (C) 2009-2010 EDF R&D +# Copyright (C) 2009-2022 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 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 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 # -# $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("GENERICSOLVER", color = termcolor.BLUE_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 - -### -# Add some variable to the case -### -def AddDataToCase( studyId, caseEntry, varName, varValue ): - study = salome.myStudyManager.GetStudyByID( 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 ): - 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 GENERICSOLVER(GENERICSOLVER_ORB__POA.GENERICSOLVER_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 @@ -132,169 +35,6 @@ class GENERICSOLVER(GENERICSOLVER_ORB__POA.GENERICSOLVER_Gen, """ def __init__ ( self, orb, poa, contID, containerName, instanceName, interfaceName ): - logger.info("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 _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, studyId, caseEntry, wrapperDescription ): - """ - This method is an implementation for the GENERICSOLVER interface. - It sets the component with some deterministic parametrization. - """ - try: - logger.info("GENERICSOLVER.Init: " + self._containerName + ' ; ' + self._instanceName) - logger.debug("GENERICSOLVER.Init : enter") - logger.debug("GENERICSOLVER.Init : studyId = %d - caseEntry = %s - wrapperDescription = %s" % ( studyId, caseEntry, wrapperDescription )) - self.wrapperDescription = wrapperDescription - GENERICSOLVER.lock.acquire() - salome.salome_init() - GENERICSOLVER.lock.release() - - self.case = GetDataFromCase( studyId, caseEntry ) - if self.case is None: - return 1 - logger.debug("GENERICSOLVER.Init : exit") - return 0 - except: - self._raiseSalomeError() - - def Exec ( self , inPoint ): - """ - This method is an implementation for the GENERICSOLVER interface. - It runs the component with some new parameters compared with the deterministic ones. - """ - try: - logger.info("GENERICSOLVER.Exec: " + self._containerName + ' ; ' + self._instanceName) - if self.case is None : - logger.error("GENERICSOLVER.Exec : Init not run") - return 1, None - - logger.debug("GENERICSOLVER.Exec (1): inPoint = %s" % inPoint) - case = dict( self.case ) - if self.wrapperDescription != "": - import sys - logger.debug("sys.path = %s" % 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: - logger.debug("variable %s <-> index %d" % ( variable.id_, i )) - case[ variable.id_ ] = inPoint[ i ] - i += 1 - - logger.debug("Case = %s" % case) - logger.info("Evaluating case by component %s in container %s" % - (self._instanceName, self._containerName)) - outPoint = self.BeamModel( **case ) - - logger.debug("GENERICSOLVER.Exec (2): inPoint = %s" % inPoint) - logger.debug("GENERICSOLVER.Exec (2): outPoint = %s" % outPoint) - return 0, outPoint - - except: - self._raiseSalomeError() - - def Finalize ( self ): - """ - This method is an implementation for the GENERICSOLVER interface. - It cleans everything set so far. - """ - try: - logger.info("GENERICSOLVER.Finalize: " + self._containerName + ' ; ' + self._instanceName) - logger.debug("GENERICSOLVER.Finalize : enter") - logger.debug("GENERICSOLVER.Finalize : exit") - return 0 - 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=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 ) - 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()