Salome HOME
f332306d4e79479cc16b54633b28a997c9c4b49c
[modules/med.git] / src / MEDOP / cmp / test_medop_components.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2015  CEA/DEN, EDF R&D
4 #
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.
9 #
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.
14 #
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
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 # This file is a set of basic use case to test (from the python
23 # context) the functions developped in the MEDOP engine and the
24 # associated MEDOP CORBA interface (MEDDataManager and
25 # MEDCalaculator).
26 #
27 # (gboulant - 16/6/2011)
28 #
29
30 # WARN: this scripts is a unit tests runner for testing the SALOME
31 # MEDOP 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.
35
36 #
37 # ===============================================================
38 # Initializing some CORBA stuff
39 # ===============================================================
40 #
41
42 # Remember SALOME definitions:
43 # ---------------------------
44 #
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).
48 #
49 # corbaModule = Name of the corba module that contains the IDL
50 # specifications of the component (name as defined in the idl file)
51 #
52 # containerType = Name of the container factory
53 #
54 componentName = "MEDOPFactory"
55 corbaModule   = "MEDOP"
56 containerType = "FactoryServer"
57
58 import salome
59 if salome.lcc is None:
60     salome.salome_init()
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
69
70 #
71 # ==================================================
72 # Helper functions to localize tests files and get data
73 # ==================================================
74 #
75 import os
76
77 try:
78     MED_ROOT_DIR=os.environ["MED_ROOT_DIR"]
79 except KeyError, e:
80     raise RuntimeError("MED_ROOT_DIR should be defined to load the test data")
81
82 RESDIR=os.path.join(MED_ROOT_DIR,"share","salome","resources","med","medop_testfiles")
83
84 def getFilePath(filename):
85     """
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.
88     """
89     filepath = os.path.join(RESDIR,filename)
90     if not os.path.exists(filepath):
91         raise RuntimeError("The file %s does not exists"%filepath)
92     return filepath
93
94 testFileName = "smallmesh_varfield.med"
95 testMeshName = "My2DMesh"
96 testFieldName= "testfield2"
97 testFieldIt  = 1
98 testFieldDt  = 1
99 testTypeOfField = 1 # On nodes
100 testFilePath = getFilePath(testFileName)
101
102 #
103 # ==================================================
104 # Basic use cases of the MEDDataManager
105 # ==================================================
106 #
107 def TEST_getDataManager():
108     dataManager = factory.getDataManager()
109     if "loadDatasource" not in dir(dataManager):
110         return False
111     return True
112
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)
118         return False
119
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)
126         return False
127
128     return True
129
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:
135         return False
136     return True
137
138 def TEST_getFieldRepresentation():
139     dataManager = factory.getDataManager()
140     datasource = dataManager.loadDatasource(testFilePath)
141     fieldHandlerList = dataManager.getFieldHandlerList()
142     fieldHandler0 = fieldHandlerList[0]
143
144     print dataManager.getFieldRepresentation(fieldHandler0.id)
145     return True
146
147 def TEST_updateFieldMetadata():
148     dataManager = factory.getDataManager()
149     datasource = dataManager.loadDatasource(testFilePath)
150     fieldHandlerList = dataManager.getFieldHandlerList()
151     fieldHandler0 = fieldHandlerList[0]
152
153     fieldid = fieldHandler0.id
154     newname = fieldHandler0.fieldname + " modified"
155
156     dataManager.updateFieldMetadata(fieldid, newname,
157                                     fieldHandler0.iteration,
158                                     fieldHandler0.order,
159                                     fieldHandler0.source)
160
161     fieldHandlerModified = dataManager.getFieldHandler(fieldid)
162     print fieldHandlerModified
163
164     if fieldHandlerModified.fieldname != newname:
165         print "ERR: the name is %s (should be %s)"%(fieldHandlerModified.fieldname,newname)
166         return False
167     return True
168
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"
176
177     print "fieldIdList = %s"%fieldIdList
178     print "filepath = %s"%filepath
179
180     dataManager.saveFields(filepath,fieldIdList)
181     # We just control that the file exists. But we should reload the
182     # contents to check the fields
183     import os
184     if not os.path.exists(filepath):
185         print "ERR: the file %s does not exist"%(filepath)
186         return False
187     return True
188
189 #
190 # ==================================================
191 # Use cases of the MEDDataManager for data loading
192 # ==================================================
193 #
194 def TEST_MEDDataManager_getMeshList():
195     dataManager = factory.getDataManager()
196     datasourceHandler = dataManager.loadDatasource(testFilePath)
197     meshHandlerList = dataManager.getMeshList(datasourceHandler.id)
198     print meshHandlerList
199
200     if len(meshHandlerList) == 0:
201         return False
202     return True
203
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:
209         meshId = mRef.id
210         mRes = dataManager.getMesh(meshId)
211         print mRes
212         if ( mRes.name != mRef.name ) or ( mRes.sourceid != mRef.sourceid):
213             return False
214     return True
215
216 def TEST_MEDDataManager_getFieldseriesListOnMesh():
217     dataManager = factory.getDataManager()
218     datasourceHandler = dataManager.loadDatasource(testFilePath)
219
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
225
226     if len(fieldseriesList) == 0:
227         return False
228     return True
229
230 def TEST_MEDDataManager_getFieldListInFieldseries():
231     dataManager = factory.getDataManager()
232     testFilePath = os.path.join(RESDIR,testFileName)
233
234     testFilePath  = getFilePath("timeseries.med")
235     datasourceHandler = dataManager.loadDatasource(testFilePath)
236
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)
245     print fieldList
246
247     if len(fieldList) == 0:
248         return False
249     return True
250
251 #
252 # ==================================================
253 # Use cases of the MEDCalculator
254 # ==================================================
255 #
256 def TEST_Calculator_basics():
257     dataManager = factory.getDataManager()
258     datasource = dataManager.loadDatasource(testFilePath)
259     fieldHandlerList = dataManager.getFieldHandlerList()
260
261     # Try to operate on the two first fields
262     fieldHandler0 = fieldHandlerList[0]
263     fieldHandler1 = fieldHandlerList[1]
264     print fieldHandler0
265     print fieldHandler1
266
267     calculator = factory.getCalculator()
268     add = calculator.add(fieldHandler0, fieldHandler1)
269     print add
270     sub = calculator.sub(fieldHandler0, fieldHandler1)
271     print sub
272     mul = calculator.mul(fieldHandler0, fieldHandler1)
273     print mul
274     div = calculator.div(fieldHandler0, fieldHandler1)
275     print div
276     #power = calculator.pow(fieldHandler0, 2)
277     #print power
278     linear = calculator.lin(fieldHandler0, 3,2)
279     print linear
280
281     return True
282
283 def TEST_Calculator_applyFunc():
284     dataManager = factory.getDataManager()
285     datasource = dataManager.loadDatasource(testFilePath)
286     fieldHandlerList = dataManager.getFieldHandlerList()
287     fieldHandler = fieldHandlerList[0]
288
289     # In this example, "u" stands for the whole field
290     calculator = factory.getCalculator()
291     import MEDOP
292     nbResultingComponent = MEDOP.NBCOMP_DEFAULT
293     res = calculator.fct(fieldHandler,"abs(u)",nbResultingComponent);
294     print res
295
296     # In this example, "a" stands for the first component
297     nbResultingComponent = 1
298     res = calculator.fct(fieldHandler,"a+2",nbResultingComponent)
299     print res
300
301     return True
302
303 #
304 # ==================================================
305 # Use cases of the MEDDataManager that need MEDCalculator
306 # ==================================================
307 #
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]
314
315     calculator = factory.getCalculator()
316     add = calculator.add(fieldHandler0, fieldHandler1)
317
318     filepath = "/tmp/test_xmed_markAsPersistent.med"
319     dataManager.markAsPersistent(add.id, True)
320     dataManager.savePersistentFields(filepath)
321     import os
322     if not os.path.exists(filepath):
323         print "ERR: the file %s does not exist"%(filepath)
324         return False
325     return True
326
327 #
328 # =============================================================
329 # Unit tests runner
330 # =============================================================
331 #
332 import unittest
333 from salome.kernel import pyunittester
334 class MyTestSuite(unittest.TestCase):
335
336     # === MEDDataManager (core functions)
337     def test_getDataManager(self):
338         self.assertTrue(TEST_getDataManager())
339
340     def test_loadDatasource(self):
341         self.assertTrue(TEST_loadDatasource())
342
343     def test_getFieldHandlerList(self):
344         self.assertTrue(TEST_getFieldHandlerList())
345
346     def test_getFieldRepresentation(self):
347         self.assertTrue(TEST_getFieldRepresentation())
348
349     def test_updateFieldMetadata(self):
350         self.assertTrue(TEST_updateFieldMetadata())
351
352     def test_saveFields(self):
353         self.assertTrue(TEST_saveFields())
354
355     # === MEDDataManager (data request functions)
356     def test_MEDDataManager_getMeshList(self):
357         self.assertTrue(TEST_MEDDataManager_getMeshList())
358
359     def test_MEDDataManager_getMesh(self):
360         self.assertTrue(TEST_MEDDataManager_getMesh())
361
362     def test_MEDDataManager_getFieldseriesListOnMesh(self):
363         self.assertTrue(TEST_MEDDataManager_getFieldseriesListOnMesh())
364
365     def test_MEDDataManager_getFieldListInFieldseries(self):
366         self.assertTrue(TEST_MEDDataManager_getFieldListInFieldseries())
367
368     # === MEDCalculator (need MEDDataManager)
369     def test_Calculator_basics(self):
370         self.assertTrue(TEST_Calculator_basics())
371
372     def test_Calculator_applyFunc(self):
373         self.assertTrue(TEST_Calculator_applyFunc())
374
375     # === MEDDataManager (need MEDCalculator)
376     def test_markAsPersistent(self):
377         self.assertTrue(TEST_markAsPersistent())
378
379 def myunittests():
380     pyunittester.run(MyTestSuite)
381
382 if __name__ == "__main__":
383     myunittests()