2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2016 CEA/DEN, EDF R&D
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 # This file is a set of basic use case to test (from the python
23 # context) the functions developped in the MEDCALC engine and the
24 # associated MEDCALC CORBA interface (MEDDataManager and
27 # (gboulant - 16/6/2011)
30 # WARN: this scripts is a unit tests runner for testing the SALOME
31 # MEDCALC CORBA components and it should stay self-consistent. Then,
32 # it's on purpose that the script does not use the xmed python
33 # package. Conversely, some (small) parts of this code could be
34 # redundant with code from the xmed package.
37 # ===============================================================
38 # Initializing some CORBA stuff
39 # ===============================================================
42 # Remember SALOME definitions:
43 # ---------------------------
45 # componentName = Name of the component (a library lib<componentName>Engine
46 # should exist with a C function named <componentName>Engine_factory, and the
47 # component should be registered in the catalog MEDCatalog.xml).
49 # corbaModule = Name of the corba module that contains the IDL
50 # specifications of the component (name as defined in the idl file)
52 # containerType = Name of the container factory
54 componentName = "MEDFactory"
55 corbaModule = "MEDCalc"
56 containerType = "FactoryServer"
59 if salome.lcc is None:
61 __import__(corbaModule)
62 factory=salome.lcc.FindOrLoadComponent(containerType,componentName)
63 # This is not the main CORBA component of the SALOME module MED
64 # (i.e. the engine associated to the active study), but the CORBA
65 # entry point for MED fields operations (i.e. a CORBA component
66 # reachable throught the LifeCycleCORBA). This entry point is used to
67 # get the other SALOME CORBA components required for MED field
68 # operations, in particular the dataManager and the calculator
71 # ==================================================
72 # Helper functions to localize tests files and get data
73 # ==================================================
78 MED_ROOT_DIR=os.environ["MED_ROOT_DIR"]
80 raise RuntimeError("MED_ROOT_DIR should be defined to load the test data")
82 RESDIR=os.path.join(MED_ROOT_DIR,"share","salome","resources","med","medcalc_testfiles")
84 def getFilePath(filename):
86 Returns the absolute path for a given file base name. The base
87 name must match with a file contained in the test files directory.
89 filepath = os.path.join(RESDIR,filename)
90 if not os.path.exists(filepath):
91 raise RuntimeError("The file %s does not exists"%filepath)
94 testFileName = "smallmesh_varfield.med"
95 testMeshName = "My2DMesh"
96 testFieldName= "testfield2"
99 testTypeOfField = 1 # On nodes
100 testFilePath = getFilePath(testFileName)
103 # ==================================================
104 # Basic use cases of the MEDDataManager
105 # ==================================================
107 def TEST_getDataManager():
108 dataManager = factory.getDataManager()
109 if "loadDatasource" not in dir(dataManager):
113 def TEST_loadDatasource():
114 dataManager = factory.getDataManager()
115 datasource = dataManager.loadDatasource(testFilePath)
116 if datasource.name != testFileName:
117 print "ERR: datasource.name=%s (should be %s)"%(datasource.name,testFilePath)
120 # We try to load the file twice. It should not load twice and
121 # return the same datasource as previously registered (same id).
122 sourceid_ref = datasource.id
123 datasource = dataManager.loadDatasource(testFilePath)
124 if datasource.id != sourceid_ref:
125 print "ERR: datasource.id=%s (should be %s)"%(datasource.id,sourceid_ref)
130 def TEST_getFieldHandlerList():
131 dataManager = factory.getDataManager()
132 datasource = dataManager.loadDatasource(testFilePath)
133 fieldHandlerList = dataManager.getFieldHandlerList()
134 if fieldHandlerList is None or len(fieldHandlerList) == 0:
138 def TEST_getFieldRepresentation():
139 dataManager = factory.getDataManager()
140 datasource = dataManager.loadDatasource(testFilePath)
141 fieldHandlerList = dataManager.getFieldHandlerList()
142 fieldHandler0 = fieldHandlerList[0]
144 print dataManager.getFieldRepresentation(fieldHandler0.id)
147 def TEST_updateFieldMetadata():
148 dataManager = factory.getDataManager()
149 datasource = dataManager.loadDatasource(testFilePath)
150 fieldHandlerList = dataManager.getFieldHandlerList()
151 fieldHandler0 = fieldHandlerList[0]
153 fieldid = fieldHandler0.id
154 newname = fieldHandler0.fieldname + " modified"
156 dataManager.updateFieldMetadata(fieldid, newname,
157 fieldHandler0.iteration,
159 fieldHandler0.source)
161 fieldHandlerModified = dataManager.getFieldHandler(fieldid)
162 print fieldHandlerModified
164 if fieldHandlerModified.fieldname != newname:
165 print "ERR: the name is %s (should be %s)"%(fieldHandlerModified.fieldname,newname)
169 def TEST_saveFields():
170 dataManager = factory.getDataManager()
171 datasource = dataManager.loadDatasource(testFilePath)
172 fieldHandlerList = dataManager.getFieldHandlerList()
173 fieldHandler0 = fieldHandlerList[0]
174 fieldIdList = [fieldHandler0.id]
175 filepath = "/tmp/test_xmed_saveFields.med"
177 print "fieldIdList = %s"%fieldIdList
178 print "filepath = %s"%filepath
180 dataManager.saveFields(filepath,fieldIdList)
181 # We just control that the file exists. But we should reload the
182 # contents to check the fields
184 if not os.path.exists(filepath):
185 print "ERR: the file %s does not exist"%(filepath)
190 # ==================================================
191 # Use cases of the MEDDataManager for data loading
192 # ==================================================
194 def TEST_MEDDataManager_getMeshList():
195 dataManager = factory.getDataManager()
196 datasourceHandler = dataManager.loadDatasource(testFilePath)
197 meshHandlerList = dataManager.getMeshList(datasourceHandler.id)
198 print meshHandlerList
200 if len(meshHandlerList) == 0:
204 def TEST_MEDDataManager_getMesh():
205 dataManager = factory.getDataManager()
206 datasourceHandler = dataManager.loadDatasource(testFilePath)
207 meshHandlerList = dataManager.getMeshList(datasourceHandler.id)
208 for mRef in meshHandlerList:
210 mRes = dataManager.getMesh(meshId)
212 if ( mRes.name != mRef.name ) or ( mRes.sourceid != mRef.sourceid):
216 def TEST_MEDDataManager_getFieldseriesListOnMesh():
217 dataManager = factory.getDataManager()
218 datasourceHandler = dataManager.loadDatasource(testFilePath)
220 meshHandlerList = dataManager.getMeshList(datasourceHandler.id)
221 # We look for the fieldseries defined on the first mesh of the list
222 meshId = meshHandlerList[0].id
223 fieldseriesList = dataManager.getFieldseriesListOnMesh(meshId)
224 print fieldseriesList
226 if len(fieldseriesList) == 0:
230 def TEST_MEDDataManager_getFieldListInFieldseries():
231 dataManager = factory.getDataManager()
232 testFilePath = os.path.join(RESDIR,testFileName)
234 testFilePath = getFilePath("timeseries.med")
235 datasourceHandler = dataManager.loadDatasource(testFilePath)
237 meshHandlerList = dataManager.getMeshList(datasourceHandler.id)
238 # We look for the fieldseries defined on the first mesh of the list
239 meshId = meshHandlerList[0].id
240 fieldseriesList = dataManager.getFieldseriesListOnMesh(meshId)
241 # We look for the fields defined in the first fieldseries,
242 # i.e. the time steps for this field.
243 fieldseriesId = fieldseriesList[0].id
244 fieldList = dataManager.getFieldListInFieldseries(fieldseriesId)
247 if len(fieldList) == 0:
252 # ==================================================
253 # Use cases of the MEDCalculator
254 # ==================================================
256 def TEST_Calculator_basics():
257 dataManager = factory.getDataManager()
258 datasource = dataManager.loadDatasource(testFilePath)
259 fieldHandlerList = dataManager.getFieldHandlerList()
261 # Try to operate on the two first fields
262 fieldHandler0 = fieldHandlerList[0]
263 fieldHandler1 = fieldHandlerList[1]
267 calculator = factory.getCalculator()
268 add = calculator.add(fieldHandler0, fieldHandler1)
270 sub = calculator.sub(fieldHandler0, fieldHandler1)
272 mul = calculator.mul(fieldHandler0, fieldHandler1)
274 div = calculator.div(fieldHandler0, fieldHandler1)
276 #power = calculator.pow(fieldHandler0, 2)
278 linear = calculator.lin(fieldHandler0, 3,2)
283 def TEST_Calculator_applyFunc():
284 dataManager = factory.getDataManager()
285 datasource = dataManager.loadDatasource(testFilePath)
286 fieldHandlerList = dataManager.getFieldHandlerList()
287 fieldHandler = fieldHandlerList[0]
289 # In this example, "u" stands for the whole field
290 calculator = factory.getCalculator()
292 nbResultingComponent = MEDCALC.NBCOMP_DEFAULT
293 res = calculator.fct(fieldHandler,"abs(u)",nbResultingComponent);
296 # In this example, "a" stands for the first component
297 nbResultingComponent = 1
298 res = calculator.fct(fieldHandler,"a+2",nbResultingComponent)
304 # ==================================================
305 # Use cases of the MEDDataManager that need MEDCalculator
306 # ==================================================
308 def TEST_markAsPersistent():
309 dataManager = factory.getDataManager()
310 datasource = dataManager.loadDatasource(testFilePath)
311 fieldHandlerList = dataManager.getFieldHandlerList()
312 fieldHandler0 = fieldHandlerList[0]
313 fieldHandler1 = fieldHandlerList[1]
315 calculator = factory.getCalculator()
316 add = calculator.add(fieldHandler0, fieldHandler1)
318 filepath = "/tmp/test_xmed_markAsPersistent.med"
319 dataManager.markAsPersistent(add.id, True)
320 dataManager.savePersistentFields(filepath)
322 if not os.path.exists(filepath):
323 print "ERR: the file %s does not exist"%(filepath)
328 # =============================================================
330 # =============================================================
333 from salome.kernel import pyunittester
334 class MyTestSuite(unittest.TestCase):
336 # === MEDDataManager (core functions)
337 def test_getDataManager(self):
338 self.assertTrue(TEST_getDataManager())
340 def test_loadDatasource(self):
341 self.assertTrue(TEST_loadDatasource())
343 def test_getFieldHandlerList(self):
344 self.assertTrue(TEST_getFieldHandlerList())
346 def test_getFieldRepresentation(self):
347 self.assertTrue(TEST_getFieldRepresentation())
349 def test_updateFieldMetadata(self):
350 self.assertTrue(TEST_updateFieldMetadata())
352 def test_saveFields(self):
353 self.assertTrue(TEST_saveFields())
355 # === MEDDataManager (data request functions)
356 def test_MEDDataManager_getMeshList(self):
357 self.assertTrue(TEST_MEDDataManager_getMeshList())
359 def test_MEDDataManager_getMesh(self):
360 self.assertTrue(TEST_MEDDataManager_getMesh())
362 def test_MEDDataManager_getFieldseriesListOnMesh(self):
363 self.assertTrue(TEST_MEDDataManager_getFieldseriesListOnMesh())
365 def test_MEDDataManager_getFieldListInFieldseries(self):
366 self.assertTrue(TEST_MEDDataManager_getFieldListInFieldseries())
368 # === MEDCalculator (need MEDDataManager)
369 def test_Calculator_basics(self):
370 self.assertTrue(TEST_Calculator_basics())
372 def test_Calculator_applyFunc(self):
373 self.assertTrue(TEST_Calculator_applyFunc())
375 # === MEDDataManager (need MEDCalculator)
376 def test_markAsPersistent(self):
377 self.assertTrue(TEST_markAsPersistent())
380 pyunittester.run(MyTestSuite)
382 if __name__ == "__main__":