2 # Copyright (C) 2007-2016 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # This file is a set of basic use case to test (from the python
22 # context) the functions developed in the MEDCALC engine and the
23 # associated MEDCALC CORBA interface (MEDDataManager and
26 # (gboulant - 16/6/2011)
29 # WARN: this scripts is a unit tests runner for testing the SALOME
30 # MEDCALC CORBA components and it should stay self-consistent. Then,
31 # it's on purpose that the script does not use the xmed python
32 # package. Conversely, some (small) parts of this code could be
33 # redundant with code from the xmed package.
36 # ===============================================================
37 # Initializing some CORBA stuff
38 # ===============================================================
41 # Remember SALOME definitions:
42 # ---------------------------
44 # componentName = Name of the component (a library lib<componentName>Engine
45 # should exist with a C function named <componentName>Engine_factory, and the
46 # component should be registered in the catalog MEDCatalog.xml).
48 # corbaModule = Name of the corba module that contains the IDL
49 # specifications of the component (name as defined in the idl file)
51 # containerType = Name of the container factory
53 componentName = "MEDFactory"
54 corbaModule = "MEDCalc"
55 containerType = "FactoryServer"
58 if salome.lcc is None:
60 __import__(corbaModule)
61 factory=salome.lcc.FindOrLoadComponent(containerType,componentName)
62 # This is not the main CORBA component of the SALOME module MED
63 # (i.e. the engine associated to the study), but the CORBA
64 # entry point for MED fields operations (i.e. a CORBA component
65 # reachable throughout the LifeCycleCORBA). This entry point is used to
66 # get the other SALOME CORBA components required for MED field
67 # operations, in particular the dataManager and the calculator
70 # ==================================================
71 # Helper functions to localize tests files and get data
72 # ==================================================
77 MED_ROOT_DIR=os.environ["MED_ROOT_DIR"]
79 raise RuntimeError("MED_ROOT_DIR should be defined to load the test data")
81 RESDIR=os.path.join(MED_ROOT_DIR,"share","salome","resources","med","medcalc_testfiles")
83 def getFilePath(filename):
85 Returns the absolute path for a given file base name. The base
86 name must match with a file contained in the test files directory.
88 filepath = os.path.join(RESDIR,filename)
89 if not os.path.exists(filepath):
90 raise RuntimeError("The file %s does not exists"%filepath)
93 testFileName = "smallmesh_varfield.med"
94 testMeshName = "My2DMesh"
95 testFieldName= "testfield2"
98 testTypeOfField = 1 # On nodes
99 testFilePath = getFilePath(testFileName)
102 # ==================================================
103 # Basic use cases of the MEDDataManager
104 # ==================================================
106 def TEST_getDataManager():
107 dataManager = factory.getDataManager()
108 if "loadDatasource" not in dir(dataManager):
112 def TEST_loadDatasource():
113 dataManager = factory.getDataManager()
114 datasource = dataManager.loadDatasource(testFilePath)
115 if datasource.name != testFileName:
116 print("ERR: datasource.name=%s (should be %s)"%(datasource.name,testFilePath))
119 # We try to load the file twice. It should not load twice and
120 # return the same datasource as previously registered (same id).
121 sourceid_ref = datasource.id
122 datasource = dataManager.loadDatasource(testFilePath)
123 if datasource.id != sourceid_ref:
124 print("ERR: datasource.id=%s (should be %s)"%(datasource.id,sourceid_ref))
129 def TEST_getFieldHandlerList():
130 dataManager = factory.getDataManager()
131 datasource = dataManager.loadDatasource(testFilePath)
132 fieldHandlerList = dataManager.getFieldHandlerList()
133 if fieldHandlerList is None or len(fieldHandlerList) == 0:
137 def TEST_getFieldRepresentation():
138 dataManager = factory.getDataManager()
139 datasource = dataManager.loadDatasource(testFilePath)
140 fieldHandlerList = dataManager.getFieldHandlerList()
141 fieldHandler0 = fieldHandlerList[0]
143 print(dataManager.getFieldRepresentation(fieldHandler0.id))
146 def TEST_updateFieldMetadata():
147 dataManager = factory.getDataManager()
148 datasource = dataManager.loadDatasource(testFilePath)
149 fieldHandlerList = dataManager.getFieldHandlerList()
150 fieldHandler0 = fieldHandlerList[0]
152 fieldid = fieldHandler0.id
153 newname = fieldHandler0.fieldname + " modified"
155 dataManager.updateFieldMetadata(fieldid, newname,
156 fieldHandler0.iteration,
158 fieldHandler0.source)
160 fieldHandlerModified = dataManager.getFieldHandler(fieldid)
161 print(fieldHandlerModified)
163 if fieldHandlerModified.fieldname != newname:
164 print("ERR: the name is %s (should be %s)"%(fieldHandlerModified.fieldname,newname))
168 def TEST_saveFields():
169 dataManager = factory.getDataManager()
170 datasource = dataManager.loadDatasource(testFilePath)
171 fieldHandlerList = dataManager.getFieldHandlerList()
172 fieldHandler0 = fieldHandlerList[0]
173 fieldIdList = [fieldHandler0.id]
174 filepath = "/tmp/test_xmed_saveFields.med"
176 print("fieldIdList = %s"%fieldIdList)
177 print("filepath = %s"%filepath)
179 dataManager.saveFields(filepath,fieldIdList)
180 # We just control that the file exists. But we should reload the
181 # contents to check the fields
183 if not os.path.exists(filepath):
184 print("ERR: the file %s does not exist"%(filepath))
189 # ==================================================
190 # Use cases of the MEDDataManager for data loading
191 # ==================================================
193 def TEST_MEDDataManager_getMeshList():
194 dataManager = factory.getDataManager()
195 datasourceHandler = dataManager.loadDatasource(testFilePath)
196 meshHandlerList = dataManager.getMeshList(datasourceHandler.id)
197 print(meshHandlerList)
199 if len(meshHandlerList) == 0:
203 def TEST_MEDDataManager_getMesh():
204 dataManager = factory.getDataManager()
205 datasourceHandler = dataManager.loadDatasource(testFilePath)
206 meshHandlerList = dataManager.getMeshList(datasourceHandler.id)
207 for mRef in meshHandlerList:
209 mRes = dataManager.getMesh(meshId)
211 if ( mRes.name != mRef.name ) or ( mRes.sourceid != mRef.sourceid):
215 def TEST_MEDDataManager_getFieldseriesListOnMesh():
216 dataManager = factory.getDataManager()
217 datasourceHandler = dataManager.loadDatasource(testFilePath)
219 meshHandlerList = dataManager.getMeshList(datasourceHandler.id)
220 # We look for the fieldseries defined on the first mesh of the list
221 meshId = meshHandlerList[0].id
222 fieldseriesList = dataManager.getFieldseriesListOnMesh(meshId)
223 print(fieldseriesList)
225 if len(fieldseriesList) == 0:
229 def TEST_MEDDataManager_getFieldListInFieldseries():
230 dataManager = factory.getDataManager()
231 testFilePath = os.path.join(RESDIR,testFileName)
233 testFilePath = getFilePath("timeseries.med")
234 datasourceHandler = dataManager.loadDatasource(testFilePath)
236 meshHandlerList = dataManager.getMeshList(datasourceHandler.id)
237 # We look for the fieldseries defined on the first mesh of the list
238 meshId = meshHandlerList[0].id
239 fieldseriesList = dataManager.getFieldseriesListOnMesh(meshId)
240 # We look for the fields defined in the first fieldseries,
241 # i.e. the time steps for this field.
242 fieldseriesId = fieldseriesList[0].id
243 fieldList = dataManager.getFieldListInFieldseries(fieldseriesId)
246 if len(fieldList) == 0:
251 # ==================================================
252 # Use cases of the MEDCalculator
253 # ==================================================
255 def TEST_Calculator_basics():
256 dataManager = factory.getDataManager()
257 datasource = dataManager.loadDatasource(testFilePath)
258 fieldHandlerList = dataManager.getFieldHandlerList()
260 # Try to operate on the two first fields
261 fieldHandler0 = fieldHandlerList[0]
262 fieldHandler1 = fieldHandlerList[1]
266 calculator = factory.getCalculator()
267 add = calculator.add(fieldHandler0, fieldHandler1)
269 sub = calculator.sub(fieldHandler0, fieldHandler1)
271 mul = calculator.mul(fieldHandler0, fieldHandler1)
273 div = calculator.div(fieldHandler0, fieldHandler1)
275 #power = calculator.pow(fieldHandler0, 2)
277 linear = calculator.lin(fieldHandler0, 3,2)
282 def TEST_Calculator_applyFunc():
283 dataManager = factory.getDataManager()
284 datasource = dataManager.loadDatasource(testFilePath)
285 fieldHandlerList = dataManager.getFieldHandlerList()
286 fieldHandler = fieldHandlerList[0]
288 # In this example, "u" stands for the whole field
289 calculator = factory.getCalculator()
291 nbResultingComponent = MEDCALC.NBCOMP_DEFAULT
292 res = calculator.fct(fieldHandler,"abs(u)",nbResultingComponent);
295 # In this example, "a" stands for the first component
296 nbResultingComponent = 1
297 res = calculator.fct(fieldHandler,"a+2",nbResultingComponent)
303 # ==================================================
304 # Use cases of the MEDDataManager that need MEDCalculator
305 # ==================================================
307 def TEST_markAsPersistent():
308 dataManager = factory.getDataManager()
309 datasource = dataManager.loadDatasource(testFilePath)
310 fieldHandlerList = dataManager.getFieldHandlerList()
311 fieldHandler0 = fieldHandlerList[0]
312 fieldHandler1 = fieldHandlerList[1]
314 calculator = factory.getCalculator()
315 add = calculator.add(fieldHandler0, fieldHandler1)
317 filepath = "/tmp/test_xmed_markAsPersistent.med"
318 dataManager.markAsPersistent(add.id, True)
319 dataManager.savePersistentFields(filepath)
321 if not os.path.exists(filepath):
322 print("ERR: the file %s does not exist"%(filepath))
327 # =============================================================
329 # =============================================================
332 from salome.kernel import pyunittester
333 class MyTestSuite(unittest.TestCase):
335 # === MEDDataManager (core functions)
336 def test_getDataManager(self):
337 self.assertTrue(TEST_getDataManager())
339 def test_loadDatasource(self):
340 self.assertTrue(TEST_loadDatasource())
342 def test_getFieldHandlerList(self):
343 self.assertTrue(TEST_getFieldHandlerList())
345 def test_getFieldRepresentation(self):
346 self.assertTrue(TEST_getFieldRepresentation())
348 def test_updateFieldMetadata(self):
349 self.assertTrue(TEST_updateFieldMetadata())
351 def test_saveFields(self):
352 self.assertTrue(TEST_saveFields())
354 # === MEDDataManager (data request functions)
355 def test_MEDDataManager_getMeshList(self):
356 self.assertTrue(TEST_MEDDataManager_getMeshList())
358 def test_MEDDataManager_getMesh(self):
359 self.assertTrue(TEST_MEDDataManager_getMesh())
361 def test_MEDDataManager_getFieldseriesListOnMesh(self):
362 self.assertTrue(TEST_MEDDataManager_getFieldseriesListOnMesh())
364 def test_MEDDataManager_getFieldListInFieldseries(self):
365 self.assertTrue(TEST_MEDDataManager_getFieldListInFieldseries())
367 # === MEDCalculator (need MEDDataManager)
368 def test_Calculator_basics(self):
369 self.assertTrue(TEST_Calculator_basics())
371 def test_Calculator_applyFunc(self):
372 self.assertTrue(TEST_Calculator_applyFunc())
374 # === MEDDataManager (need MEDCalculator)
375 def test_markAsPersistent(self):
376 self.assertTrue(TEST_markAsPersistent())
379 pyunittester.run(MyTestSuite)
381 if __name__ == "__main__":