Salome HOME
Fixed copyrights and removed unused files
[samples/genericsolver.git] / src / GENERICSOLVER / GENERICSOLVER.py
1 #  Copyright (C) 2009-2010 EDF R&D
2 #
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.
7 #
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.
12 #
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
16 #
17 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19 import traceback
20 import GENERICSOLVER_ORB__POA
21 import SALOME_ComponentPy
22 import SALOME_DriverPy
23
24 from omniORB import CORBA
25 from SALOME_NamingServicePy import *
26 from LifeCycleCORBA import *
27 import SALOMEDS
28 import SALOMEDS_Attributes_idl
29
30
31 VARIABLE_ID = 1030
32
33 ################################################
34
35 # init ORB
36 orb = CORBA.ORB_init( [''], CORBA.ORB_ID )
37
38 # create naming service instance
39 naming_service = SALOME_NamingServicePy_i( orb )
40
41 # create life cycle CORBA instance
42 lcc = LifeCycleCORBA( orb )
43
44 # get study manager
45 obj = naming_service.Resolve( '/myStudyManager' )
46 studyManager = obj._narrow( SALOMEDS.StudyManager )
47
48 ################################################
49 ###
50 # get active study
51 ###
52 def getStudy( studyId ):
53     #studyId = getStudyId()
54     study = studyManager.GetStudyByID( studyId )
55     return study
56
57 ###
58 # Retrieve data from selected case
59 ###
60 def GetDataFromCase( studyId, caseEntry ):
61     theCase = {}
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 )
68         if var == None:
69             print "GENERICSOLVER.GetDataFromCase : ERROR! no variable '%s'" % name
70             break
71         theCase[ name ] = getValueOfVariable( builder, var )
72     return theCase
73
74 ###
75 # Add some variable to the case
76 ###
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 )
84     pass
85
86 ###
87 # Plays with study
88 ###
89 def addObjectInStudy( builder, father, objname, objid ):
90     obj = getSubSObjectByName( father, objname )
91     if obj is None:
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 )
97     return obj
98
99 def setValueToVariable( builder, varobj, value ):
100     attr = builder.FindOrCreateAttribute( varobj, "AttributeLocalID" )
101     objid = attr.Value()
102     if (objid == VARIABLE_ID):
103         attr = builder.FindOrCreateAttribute( varobj, "AttributeReal" )
104         attr.SetValue( value )
105     else:
106         attr = builder.FindOrCreateAttribute( varobj, "AttributeName" )
107         QMessageBox.information( sgPyQt.getDesktop(), 'Info', "Object '%s' isn't a variable. Can't set value." % attr.Value() )
108     pass
109
110 def getValueOfVariable( builder, varobj ):
111     attr = builder.FindOrCreateAttribute( varobj, "AttributeLocalID" )
112     objid = attr.Value()
113     if (objid == VARIABLE_ID):
114         attr = builder.FindOrCreateAttribute( varobj, "AttributeReal" )
115         return attr.Value()
116     else:
117         attr = builder.FindOrCreateAttribute( varobj, "AttributeName" )
118         QMessageBox.information( sgPyQt.getDesktop(), 'Info', "Object '%s' isn't a variable. Can't set value." % attr.Value() )
119     return 0.
120
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()
126     while iter.More():
127         sobj = iter.Value()
128         print "GENERICSOLVER.getSubSObjectByName Got sobjet named", sobj.GetName()
129         if sobj.GetName() == childName:
130             return sobj
131         iter.Next()
132         pass
133     return None
134
135 ################################################
136
137 class GENERICSOLVER(GENERICSOLVER_ORB__POA.GENERICSOLVER_Gen,
138               SALOME_ComponentPy.SALOME_ComponentPy_i,
139               SALOME_DriverPy.SALOME_DriverPy_i):
140     """
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
146     """
147     def __init__ ( self, orb, poa, contID, containerName, instanceName, 
148                    interfaceName ):
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 )
156         self.case = None
157         self.wrapperDescription = ""
158
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 ######################################################################
166
167
168     def Init ( self, studyId, caseEntry, wrapperDescription ):
169         """
170         This method is an implementation for the GENERICSOLVER interface.
171         It sets the component with some deterministic parametrization.
172         """
173         print "GENERICSOLVER.Init : enter"
174         print "GENERICSOLVER.Init : studyId = %d - caseEntry = %s - wrapperDescription = %s" % ( studyId, caseEntry, wrapperDescription )
175         self.wrapperDescription = wrapperDescription
176         
177         try:
178             self.case = GetDataFromCase( studyId, caseEntry )
179         except:
180             traceback.print_exc()
181         if self.case is None:
182             return 1
183         print "GENERICSOLVER.Init : exit"
184         return 0
185
186     def Exec ( self , inPoint, outPoint ):
187         """
188         This method is an implementation for the GENERICSOLVER interface.
189         It runs the component with some new parameters compared with the deterministic ones.
190         """
191         if self.case is None :
192             print "GENERICSOLVER.Exec : Init not run"
193             return 1, outPoint
194         
195         print "GENERICSOLVER.Exec (1): inPoint  = ", inPoint
196         print "GENERICSOLVER.Exec (1): outPoint = ", outPoint
197
198         try:
199             case = dict( self.case )
200             if self.wrapperDescription != "":
201                 import sys
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()
207                 i = 0
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 ]
213                         i += 1
214
215             print "Case = ", case
216             outPoint = self.BeamModel( **case )
217         except:
218             traceback.print_exc()
219             return 1, outPoint
220          
221
222         print "GENERICSOLVER.Exec (2): inPoint  = ", inPoint
223         print "GENERICSOLVER.Exec (2): outPoint = ", outPoint
224         return 0, outPoint
225
226     def Finalize ( self ):
227         """
228         This method is an implementation for the GENERICSOLVER interface.
229         It cleans everything set so far.
230         """
231         print "GENERICSOLVER.Finalize : enter"
232         print "GENERICSOLVER.Finalize : exit"
233         return 0
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 ######################################################################
240
241     def BeamModel ( self , E=1., F=0., L=0., I=1. ):
242        """
243        This method implements a beam bending model based on the following formula:
244        deviation = ( Force * Length^3 ) / ( 3 * YoungModulus * InertiaSection )
245        """
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)
248
249        return (d,)