1 # Copyright (C) 2009-2010 EDF R&D
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 import GENERICSOLVER_ORB__POA
21 import SALOME_ComponentPy
22 import SALOME_DriverPy
24 from omniORB import CORBA
25 from SALOME_NamingServicePy import *
26 from LifeCycleCORBA import *
28 import SALOMEDS_Attributes_idl
33 ################################################
36 orb = CORBA.ORB_init( [''], CORBA.ORB_ID )
38 # create naming service instance
39 naming_service = SALOME_NamingServicePy_i( orb )
41 # create life cycle CORBA instance
42 lcc = LifeCycleCORBA( orb )
45 obj = naming_service.Resolve( '/myStudyManager' )
46 studyManager = obj._narrow( SALOMEDS.StudyManager )
48 ################################################
52 def getStudy( studyId ):
53 #studyId = getStudyId()
54 study = studyManager.GetStudyByID( studyId )
58 # Retrieve data from selected case
60 def GetDataFromCase( studyId, caseEntry ):
62 study = getStudy( studyId )
63 case = study.FindObjectID( caseEntry )
64 builder = study.NewBuilder()
65 # Get the values of the variables and make them a list
66 for name in ("E", "F", "L", "I"):
67 var = getSubSObjectByName( studyId, case, name )
69 print "GENERICSOLVER.GetDataFromCase : ERROR! no variable '%s'" % name
71 theCase[ name ] = getValueOfVariable( builder, var )
75 # Add some variable to the case
77 def AddDataToCase( studyId, caseEntry, varName, varValue ):
78 study = getStudy( studyId )
79 case = study.FindObjectID( caseEntry )
80 builder = study.NewBuilder()
81 var = addObjectInStudy( builder, case, varName, VARIABLE_ID )
82 setValueToVariable( builder, var, varValue )
83 sg.updateObjBrowser( True )
89 def addObjectInStudy( builder, father, objname, objid ):
90 obj = getSubSObjectByName( father, objname )
92 obj = builder.NewObject( father )
93 attr = builder.FindOrCreateAttribute( obj, "AttributeName" )
94 attr.SetValue( objname )
95 attr = builder.FindOrCreateAttribute( obj, "AttributeLocalID" )
96 attr.SetValue( objid )
99 def setValueToVariable( builder, varobj, value ):
100 attr = builder.FindOrCreateAttribute( varobj, "AttributeLocalID" )
102 if (objid == VARIABLE_ID):
103 attr = builder.FindOrCreateAttribute( varobj, "AttributeReal" )
104 attr.SetValue( value )
106 attr = builder.FindOrCreateAttribute( varobj, "AttributeName" )
107 QMessageBox.information( sgPyQt.getDesktop(), 'Info', "Object '%s' isn't a variable. Can't set value." % attr.Value() )
110 def getValueOfVariable( builder, varobj ):
111 attr = builder.FindOrCreateAttribute( varobj, "AttributeLocalID" )
113 if (objid == VARIABLE_ID):
114 attr = builder.FindOrCreateAttribute( varobj, "AttributeReal" )
117 attr = builder.FindOrCreateAttribute( varobj, "AttributeName" )
118 QMessageBox.information( sgPyQt.getDesktop(), 'Info', "Object '%s' isn't a variable. Can't set value." % attr.Value() )
121 def getSubSObjectByName( studyId, sobjFather, childName ):
122 print "GENERICSOLVER.getSubSObjectByName Looking for sobjet named", childName
123 study = getStudy( studyId )
124 iter = study.NewChildIterator( sobjFather )
125 #builder = study.NewBuilder()
128 print "GENERICSOLVER.getSubSObjectByName Got sobjet named", sobj.GetName()
129 if sobj.GetName() == childName:
135 ################################################
137 class GENERICSOLVER(GENERICSOLVER_ORB__POA.GENERICSOLVER_Gen,
138 SALOME_ComponentPy.SALOME_ComponentPy_i,
139 SALOME_DriverPy.SALOME_DriverPy_i):
141 Pour etre un composant SALOME cette classe Python
142 doit avoir le nom du composant et heriter de la
143 classe GENERICSOLVER_Gen issue de la compilation de l'idl
144 par omniidl et de la classe SALOME_ComponentPy_i
145 qui porte les services generaux d'un composant SALOME
147 def __init__ ( self, orb, poa, contID, containerName, instanceName,
149 print "GENERICSOLVER.__init__: ", containerName, ';', instanceName
150 SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa,
151 contID, containerName, instanceName, interfaceName, 0)
152 SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
153 # On stocke dans l'attribut _naming_service, une reference sur
154 # le Naming Service CORBA
155 self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
157 self.wrapperDescription = ""
159 ######################################################################
160 # This is the Wrapper part of the GENERICSOLVER module, ie
161 # the three following methods are used by generic controlling
162 # modules like OpenTURNS in order to launch a computation.
163 # The interface is declared in GENERICSOLVER_Gen.idl. The methods
164 # are free to call the legacy interface (see below).
165 ######################################################################
168 def Init ( self, studyId, caseEntry, wrapperDescription ):
170 This method is an implementation for the GENERICSOLVER interface.
171 It sets the component with some deterministic parametrization.
173 print "GENERICSOLVER.Init : enter"
174 print "GENERICSOLVER.Init : studyId = %d - caseEntry = %s - wrapperDescription = %s" % ( studyId, caseEntry, wrapperDescription )
175 self.wrapperDescription = wrapperDescription
178 self.case = GetDataFromCase( studyId, caseEntry )
180 traceback.print_exc()
181 if self.case is None:
183 print "GENERICSOLVER.Init : exit"
186 def Exec ( self , inPoint, outPoint ):
188 This method is an implementation for the GENERICSOLVER interface.
189 It runs the component with some new parameters compared with the deterministic ones.
191 if self.case is None :
192 print "GENERICSOLVER.Exec : Init not run"
195 print "GENERICSOLVER.Exec (1): inPoint = ", inPoint
196 print "GENERICSOLVER.Exec (1): outPoint = ", outPoint
199 case = dict( self.case )
200 if self.wrapperDescription != "":
202 print "sys.path = ", sys.path
203 import openturns.wrapper
204 wrapper = openturns.wrapper.WrapperFile.BuildWrapperFromStream( self.wrapperDescription )
205 data = wrapper.getWrapperData()
206 variableList = data.getVariableList()
208 for idx in range( variableList.getSize() ):
209 variable = variableList[ idx ]
210 if variable.type_ == 0:
211 print "variable %s <-> index %d" % ( variable.id_, i )
212 case[ variable.id_ ] = inPoint[ i ]
215 print "Case = ", case
216 outPoint = self.BeamModel( **case )
218 traceback.print_exc()
222 print "GENERICSOLVER.Exec (2): inPoint = ", inPoint
223 print "GENERICSOLVER.Exec (2): outPoint = ", outPoint
226 def Finalize ( self ):
228 This method is an implementation for the GENERICSOLVER interface.
229 It cleans everything set so far.
231 print "GENERICSOLVER.Finalize : enter"
232 print "GENERICSOLVER.Finalize : exit"
234 ######################################################################
235 # This is the computation part of the GENERICSOLVER module, ie
236 # the following method realizes what the solver is intended to do.
237 # The interface of this method (and maybe other ones) is absolutely
238 # free and depends on the module (legacy interface).
239 ######################################################################
241 def BeamModel ( self , E=1., F=0., L=0., I=1. ):
243 This method implements a beam bending model based on the following formula:
244 deviation = ( Force * Length^3 ) / ( 3 * YoungModulus * InertiaSection )
246 d = ( F * L*L*L ) / ( 3. * E * I )
247 print "GENERICSOLVER.Exec (2): BeamModel (E=%g, F=%g, L=%g, I=%g) = %g" % (E,F,L,I,d)