1 # Copyright (C) 2009-2011 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
31 import GENERICSOLVER_ORB__POA
32 import SALOME_ComponentPy
33 import SALOME_DriverPy
37 from salome.kernel.logger import Logger
38 from salome.kernel import termcolor
39 logger = Logger("DEVIATION", color = termcolor.RED_FG)
40 logger.setLevel(logging.INFO)
45 # Retrieve data from selected case
47 def GetDataFromCase( studyId, caseEntry ):
49 study = salome.myStudyManager.GetStudyByID( studyId )
50 case = study.FindObjectID( caseEntry )
51 builder = study.NewBuilder()
52 # Get the values of the variables and make them a list
53 for name in ("E", "F", "L", "I"):
54 var = getSubSObjectByName( studyId, case, name )
56 raise Exception('No variable "%s" was found in case "%s" (entry %s). '
57 'It is probably not a case for the code DEVIATION.' %
58 (name, case.GetName(), caseEntry))
59 theCase[ name ] = getValueOfVariable( builder, var )
66 def getValueOfVariable( builder, varobj ):
67 attr = builder.FindOrCreateAttribute( varobj, "AttributeLocalID" )
69 if (objid == VARIABLE_ID):
70 attr = builder.FindOrCreateAttribute( varobj, "AttributeReal" )
73 attr = builder.FindOrCreateAttribute( varobj, "AttributeName" )
74 QMessageBox.information( sgPyQt.getDesktop(), 'Info', "Object '%s' isn't a variable. Can't set value." % attr.Value() )
77 def getSubSObjectByName( studyId, sobjFather, childName ):
78 logger.debug("GENERICSOLVER.getSubSObjectByName Looking for sobjet named " + childName)
79 study = salome.myStudyManager.GetStudyByID( studyId )
80 iter = study.NewChildIterator( sobjFather )
81 #builder = study.NewBuilder()
84 logger.debug("GENERICSOLVER.getSubSObjectByName Got sobjet named " + sobj.GetName())
85 if sobj.GetName() == childName:
91 ################################################
93 class DEVIATION(GENERICSOLVER_ORB__POA.DEVIATION_Gen,
94 SALOME_ComponentPy.SALOME_ComponentPy_i,
95 SALOME_DriverPy.SALOME_DriverPy_i):
97 lock = threading.Lock()
100 Pour etre un composant SALOME cette classe Python
101 doit avoir le nom du composant et heriter de la
102 classe DEVIATION_Gen issue de la compilation de l'idl
103 par omniidl et de la classe SALOME_ComponentPy_i
104 qui porte les services generaux d'un composant SALOME
106 def __init__ ( self, orb, poa, contID, containerName, instanceName,
108 logger.info("__init__: " + containerName + ' ; ' + instanceName)
109 SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa,
110 contID, containerName, instanceName, interfaceName, 0)
111 SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
112 # On stocke dans l'attribut _naming_service, une reference sur
113 # le Naming Service CORBA
114 self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
115 self.deterministicValues = {}
117 ######################################################################
118 # This is the Wrapper part of the GENERICSOLVER module, ie
119 # the three following methods are used by generic controlling
120 # modules like OpenTURNS in order to launch a computation.
121 # The interface is declared in GENERICSOLVER_Gen.idl. The methods
122 # are free to call the legacy interface (see below).
123 ######################################################################
125 def _raiseSalomeError(self):
126 message = "Error in component %s running in container %s." % (self._instanceName, self._containerName)
127 logger.exception(message)
128 message += " " + traceback.format_exc()
129 exc = SALOME.ExceptionStruct(SALOME.INTERNAL_ERROR, message,
130 inspect.stack()[1][1], inspect.stack()[1][2])
131 raise SALOME.SALOME_Exception(exc)
133 def _getIdMessage(self):
134 return "%s in container %s running on %s, process %d, thread %d" % \
135 (self._instanceName, self._containerName,
136 platform.node(), os.getpid(), thread.get_ident())
138 def Init(self, studyId, detCaseEntry):
140 This method is an example for the initialization of a computation component for
141 use with OpenTURNS in SALOME 5.1.5 and later (for YACS integration)
144 logger.info("Init: " + self._getIdMessage())
145 DEVIATION.lock.acquire()
147 DEVIATION.lock.release()
149 self.deterministicValues = GetDataFromCase(studyId, detCaseEntry)
150 logger.debug("deterministic values: %s" % self.deterministicValues)
152 self._raiseSalomeError()
154 def Exec(self, paramInput):
156 This method is an example for the execution of a computation component for
157 use with OpenTURNS in SALOME 5.1.5 and later (for YACS integration)
160 logger.info("Exec: " + self._getIdMessage())
161 logger.debug("inputVarList: %s" % paramInput.inputVarList)
162 logger.debug("outputVarList: %s" % paramInput.outputVarList)
163 logger.debug("inputValues: %s" % paramInput.inputValues)
164 inputPoint = paramInput.inputValues[0][0] # No time series, single observation
165 if len(inputPoint) != len(paramInput.inputVarList):
166 raise Exception("Size mismatch between inputVarList and point to evaluate")
168 evalPoint = self.deterministicValues
169 for i in range(len(inputPoint)):
170 evalPoint[paramInput.inputVarList[i]] = inputPoint[i][0]
171 logger.debug("evalPoint = %s" % evalPoint)
173 # Test for an invalid parameter and return an error in this case
174 if evalPoint["L"] <= 0:
175 return SALOME_TYPES.ParametricOutput(
177 specificOutputInfos = [],
179 errorMessage = "Invalid value: L must be positive")
182 resDict["dev"] = self.BeamModel(**evalPoint)
184 outputValues = [[[]]]
185 for outputVar in paramInput.outputVarList:
186 outputValues[0][0].append([resDict[outputVar]])
187 logger.debug("paramOutput: %s" % outputValues)
188 return SALOME_TYPES.ParametricOutput(outputValues,
189 specificOutputInfos = [],
193 self._raiseSalomeError()
197 This method is an implementation for the DEVIATION interface.
198 It cleans everything set so far.
201 logger.info("Finalize: " + self._getIdMessage())
203 self._raiseSalomeError()
205 ######################################################################
206 # This is the computation part of the GENERICSOLVER module, ie
207 # the following method realizes what the solver is intended to do.
208 # The interface of this method (and maybe other ones) is absolutely
209 # free and depends on the module (legacy interface).
210 ######################################################################
212 def BeamModel(self, E, F, L, I):
214 This method implements a beam bending model based on the following formula:
215 deviation = ( Force * Length^3 ) / ( 3 * YoungModulus * InertiaSection )
217 d = (F * L * L * L) / (3. * E * I)
218 logger.debug("BeamModel (E=%g, F=%g, L=%g, I=%g) = %g" % (E, F, L, I, d))