1 # Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
30 import GENERICSOLVER_ORB__POA
31 import SALOME_ComponentPy
32 import SALOME_DriverPy
35 from pal.logger import Logger
36 from pal import termcolor
37 logger = Logger("GENERICSOLVER", color = termcolor.BLUE_FG)
38 #logger.setLevel(logging.INFO)
43 # Retrieve data from selected case
45 def GetDataFromCase( studyId, caseEntry ):
47 study = salome.myStudyManager.GetStudyByID( studyId )
48 case = study.FindObjectID( caseEntry )
49 builder = study.NewBuilder()
50 # Get the values of the variables and make them a list
51 for name in ("E", "F", "L", "I"):
52 var = getSubSObjectByName( studyId, case, name )
54 logger.error("GENERICSOLVER.GetDataFromCase : ERROR! no variable '%s'" % name)
56 theCase[ name ] = getValueOfVariable( builder, var )
60 # Add some variable to the case
62 def AddDataToCase( studyId, caseEntry, varName, varValue ):
63 study = salome.myStudyManager.GetStudyByID( studyId )
64 case = study.FindObjectID( caseEntry )
65 builder = study.NewBuilder()
66 var = addObjectInStudy( builder, case, varName, VARIABLE_ID )
67 setValueToVariable( builder, var, varValue )
68 sg.updateObjBrowser( True )
74 def addObjectInStudy( builder, father, objname, objid ):
75 obj = getSubSObjectByName( father, objname )
77 obj = builder.NewObject( father )
78 attr = builder.FindOrCreateAttribute( obj, "AttributeName" )
79 attr.SetValue( objname )
80 attr = builder.FindOrCreateAttribute( obj, "AttributeLocalID" )
81 attr.SetValue( objid )
84 def setValueToVariable( builder, varobj, value ):
85 attr = builder.FindOrCreateAttribute( varobj, "AttributeLocalID" )
87 if (objid == VARIABLE_ID):
88 attr = builder.FindOrCreateAttribute( varobj, "AttributeReal" )
89 attr.SetValue( value )
91 attr = builder.FindOrCreateAttribute( varobj, "AttributeName" )
92 QMessageBox.information( sgPyQt.getDesktop(), 'Info', "Object '%s' isn't a variable. Can't set value." % attr.Value() )
95 def getValueOfVariable( builder, varobj ):
96 attr = builder.FindOrCreateAttribute( varobj, "AttributeLocalID" )
98 if (objid == VARIABLE_ID):
99 attr = builder.FindOrCreateAttribute( varobj, "AttributeReal" )
102 attr = builder.FindOrCreateAttribute( varobj, "AttributeName" )
103 QMessageBox.information( sgPyQt.getDesktop(), 'Info', "Object '%s' isn't a variable. Can't set value." % attr.Value() )
106 def getSubSObjectByName( studyId, sobjFather, childName ):
107 logger.debug("GENERICSOLVER.getSubSObjectByName Looking for sobjet named " + childName)
108 study = salome.myStudyManager.GetStudyByID( studyId )
109 iter = study.NewChildIterator( sobjFather )
110 #builder = study.NewBuilder()
113 logger.debug("GENERICSOLVER.getSubSObjectByName Got sobjet named " + sobj.GetName())
114 if sobj.GetName() == childName:
120 ################################################
122 class GENERICSOLVER(GENERICSOLVER_ORB__POA.GENERICSOLVER_Gen,
123 SALOME_ComponentPy.SALOME_ComponentPy_i,
124 SALOME_DriverPy.SALOME_DriverPy_i):
126 lock = threading.Lock()
129 Pour etre un composant SALOME cette classe Python
130 doit avoir le nom du composant et heriter de la
131 classe GENERICSOLVER_Gen issue de la compilation de l'idl
132 par omniidl et de la classe SALOME_ComponentPy_i
133 qui porte les services generaux d'un composant SALOME
135 def __init__ ( self, orb, poa, contID, containerName, instanceName,
137 logger.info("GENERICSOLVER.__init__: " + containerName + ' ; ' + instanceName)
138 SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa,
139 contID, containerName, instanceName, interfaceName, 0)
140 SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
141 # On stocke dans l'attribut _naming_service, une reference sur
142 # le Naming Service CORBA
143 self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
145 self.wrapperDescription = ""
147 ######################################################################
148 # This is the Wrapper part of the GENERICSOLVER module, ie
149 # the three following methods are used by generic controlling
150 # modules like OpenTURNS in order to launch a computation.
151 # The interface is declared in GENERICSOLVER_Gen.idl. The methods
152 # are free to call the legacy interface (see below).
153 ######################################################################
155 def _raiseSalomeError(self):
156 message = "Error in %s.%s" % (self.__class__.__name__, inspect.stack()[1][3])
157 logger.exception(message)
158 message += ". See logs of container %s for more details." % self._containerName
159 exc = SALOME.ExceptionStruct(SALOME.INTERNAL_ERROR, message,
160 inspect.stack()[1][1], inspect.stack()[1][2])
161 raise SALOME.SALOME_Exception(exc)
163 def Init ( self, studyId, caseEntry, wrapperDescription ):
165 This method is an implementation for the GENERICSOLVER interface.
166 It sets the component with some deterministic parametrization.
169 logger.debug("GENERICSOLVER.Init : enter")
170 logger.debug("GENERICSOLVER.Init : studyId = %d - caseEntry = %s - wrapperDescription = %s" % ( studyId, caseEntry, wrapperDescription ))
171 self.wrapperDescription = wrapperDescription
172 GENERICSOLVER.lock.acquire()
174 GENERICSOLVER.lock.release()
176 self.case = GetDataFromCase( studyId, caseEntry )
177 if self.case is None:
179 logger.debug("GENERICSOLVER.Init : exit")
182 self._raiseSalomeError()
184 def Exec ( self , inPoint ):
186 This method is an implementation for the GENERICSOLVER interface.
187 It runs the component with some new parameters compared with the deterministic ones.
190 if self.case is None :
191 logger.error("GENERICSOLVER.Exec : Init not run")
194 logger.debug("GENERICSOLVER.Exec (1): inPoint = %s" % inPoint)
195 case = dict( self.case )
196 if self.wrapperDescription != "":
198 logger.debug("sys.path = %s" % sys.path)
199 import openturns.wrapper
200 wrapper = openturns.wrapper.WrapperFile.BuildWrapperFromStream( self.wrapperDescription )
201 data = wrapper.getWrapperData()
202 variableList = data.getVariableList()
204 for idx in range( variableList.getSize() ):
205 variable = variableList[ idx ]
206 if variable.type_ == 0:
207 logger.debug("variable %s <-> index %d" % ( variable.id_, i ))
208 case[ variable.id_ ] = inPoint[ i ]
211 logger.debug("Case = %s" % case)
212 logger.info("Evaluating case by component %s in container %s" %
213 (self._instanceName, self._containerName))
214 outPoint = self.BeamModel( **case )
216 logger.debug("GENERICSOLVER.Exec (2): inPoint = %s" % inPoint)
217 logger.debug("GENERICSOLVER.Exec (2): outPoint = %s" % outPoint)
221 self._raiseSalomeError()
223 def Finalize ( self ):
225 This method is an implementation for the GENERICSOLVER interface.
226 It cleans everything set so far.
229 logger.debug("GENERICSOLVER.Finalize : enter")
230 logger.debug("GENERICSOLVER.Finalize : exit")
233 self._raiseSalomeError()
235 ######################################################################
236 # This is the computation part of the GENERICSOLVER module, ie
237 # the following method realizes what the solver is intended to do.
238 # The interface of this method (and maybe other ones) is absolutely
239 # free and depends on the module (legacy interface).
240 ######################################################################
242 def BeamModel ( self , E=1., F=0., L=0., I=1. ):
244 This method implements a beam bending model based on the following formula:
245 deviation = ( Force * Length^3 ) / ( 3 * YoungModulus * InertiaSection )
247 d = ( F * L*L*L ) / ( 3. * E * I )
248 logger.debug("GENERICSOLVER.Exec (2): BeamModel (E=%g, F=%g, L=%g, I=%g) = %g" % (E,F,L,I,d))