-# Copyright (C) 2009-2010 EDF R&D
+# Copyright (C) 2009-2015 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
# $Id$
#
-import logging
-import threading
-import inspect
-import traceback
-
-import salome
import GENERICSOLVER_ORB__POA
import SALOME_ComponentPy
import SALOME_DriverPy
-import SALOME
-
-from salome.kernel.logger import Logger
-from salome.kernel 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
"""
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)
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,)