Salome HOME
Use module pal.logger for logs. Added a method to raise SALOME_Exception objects...
[samples/genericsolver.git] / src / GENERICSOLVER / GENERICSOLVER.py
1 #  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
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.
10 #
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.
15 #
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
19 #
20 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22 #  $Id$
23 #
24
25 import logging
26 import threading
27 import inspect
28
29 import salome
30 import GENERICSOLVER_ORB__POA
31 import SALOME_ComponentPy
32 import SALOME_DriverPy
33 import SALOME
34
35 from pal.logger import Logger
36 from pal import termcolor
37 logger = Logger("GENERICSOLVER", color = termcolor.BLUE_FG)
38 #logger.setLevel(logging.INFO)
39
40 VARIABLE_ID = 1030
41
42 ###
43 # Retrieve data from selected case
44 ###
45 def GetDataFromCase( studyId, caseEntry ):
46     theCase = {}
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 )
53         if var == None:
54             logger.error("GENERICSOLVER.GetDataFromCase : ERROR! no variable '%s'" % name)
55             break
56         theCase[ name ] = getValueOfVariable( builder, var )
57     return theCase
58
59 ###
60 # Add some variable to the case
61 ###
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 )
69     pass
70
71 ###
72 # Plays with study
73 ###
74 def addObjectInStudy( builder, father, objname, objid ):
75     obj = getSubSObjectByName( father, objname )
76     if obj is None:
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 )
82     return obj
83
84 def setValueToVariable( builder, varobj, value ):
85     attr = builder.FindOrCreateAttribute( varobj, "AttributeLocalID" )
86     objid = attr.Value()
87     if (objid == VARIABLE_ID):
88         attr = builder.FindOrCreateAttribute( varobj, "AttributeReal" )
89         attr.SetValue( value )
90     else:
91         attr = builder.FindOrCreateAttribute( varobj, "AttributeName" )
92         QMessageBox.information( sgPyQt.getDesktop(), 'Info', "Object '%s' isn't a variable. Can't set value." % attr.Value() )
93     pass
94
95 def getValueOfVariable( builder, varobj ):
96     attr = builder.FindOrCreateAttribute( varobj, "AttributeLocalID" )
97     objid = attr.Value()
98     if (objid == VARIABLE_ID):
99         attr = builder.FindOrCreateAttribute( varobj, "AttributeReal" )
100         return attr.Value()
101     else:
102         attr = builder.FindOrCreateAttribute( varobj, "AttributeName" )
103         QMessageBox.information( sgPyQt.getDesktop(), 'Info', "Object '%s' isn't a variable. Can't set value." % attr.Value() )
104     return 0.
105
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()
111     while iter.More():
112         sobj = iter.Value()
113         logger.debug("GENERICSOLVER.getSubSObjectByName Got sobjet named " + sobj.GetName())
114         if sobj.GetName() == childName:
115             return sobj
116         iter.Next()
117         pass
118     return None
119
120 ################################################
121
122 class GENERICSOLVER(GENERICSOLVER_ORB__POA.GENERICSOLVER_Gen,
123                     SALOME_ComponentPy.SALOME_ComponentPy_i,
124                     SALOME_DriverPy.SALOME_DriverPy_i):
125     
126     lock = threading.Lock()
127     
128     """
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
134     """
135     def __init__ ( self, orb, poa, contID, containerName, instanceName, 
136                    interfaceName ):
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 )
144         self.case = None
145         self.wrapperDescription = ""
146
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 ######################################################################
154
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)
162
163     def Init ( self, studyId, caseEntry, wrapperDescription ):
164         """
165         This method is an implementation for the GENERICSOLVER interface.
166         It sets the component with some deterministic parametrization.
167         """
168         try:
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()
173             salome.salome_init()
174             GENERICSOLVER.lock.release()
175         
176             self.case = GetDataFromCase( studyId, caseEntry )
177             if self.case is None:
178                 return 1
179             logger.debug("GENERICSOLVER.Init : exit")
180             return 0
181         except:
182             self._raiseSalomeError()
183
184     def Exec ( self , inPoint ):
185         """
186         This method is an implementation for the GENERICSOLVER interface.
187         It runs the component with some new parameters compared with the deterministic ones.
188         """
189         try:
190             if self.case is None :
191                 logger.error("GENERICSOLVER.Exec : Init not run")
192                 return 1, None
193         
194             logger.debug("GENERICSOLVER.Exec (1): inPoint  = %s" % inPoint)
195             case = dict( self.case )
196             if self.wrapperDescription != "":
197                 import sys
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()
203                 i = 0
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 ]
209                         i += 1
210
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 )
215
216             logger.debug("GENERICSOLVER.Exec (2): inPoint  = %s" % inPoint)
217             logger.debug("GENERICSOLVER.Exec (2): outPoint = %s" % outPoint)
218             return 0, outPoint
219
220         except:
221             self._raiseSalomeError()
222
223     def Finalize ( self ):
224         """
225         This method is an implementation for the GENERICSOLVER interface.
226         It cleans everything set so far.
227         """
228         try:
229             logger.debug("GENERICSOLVER.Finalize : enter")
230             logger.debug("GENERICSOLVER.Finalize : exit")
231             return 0
232         except:
233             self._raiseSalomeError()
234         
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 ######################################################################
241
242     def BeamModel ( self , E=1., F=0., L=0., I=1. ):
243        """
244        This method implements a beam bending model based on the following formula:
245        deviation = ( Force * Length^3 ) / ( 3 * YoungModulus * InertiaSection )
246        """
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))
249
250        return (d,)