Salome HOME
b017aadcf58875ca42ce3ddf25508951a60fcf7a
[tools/medcoupling.git] / src / MEDLoader / Swig / MEDLoaderTest4.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013  CEA/DEN, EDF R&D
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author : Anthony Geay (CEA/DEN)
21
22 from MEDLoader import *
23 import unittest
24 from math import pi,e,sqrt
25
26 class MEDLoaderTest4(unittest.TestCase):
27     """
28     Test series to emulate the future MEDReader plugin for PARAVIS.
29     """
30     def test1(self):
31         """
32         This test is the most simple one. One time serie of one field with only cell fields with no profiles.
33         The only "difficulty" is that the cell field is lying on different levels (2D and 1D) to maximize the compatibility with ParaVIS.
34         """
35         fname="ForMEDReader1.med"
36         # building a mesh containing 4 tri3 + 5 quad4
37         tri=MEDCouplingUMesh("tri",2)
38         tri.allocateCells() ; tri.insertNextCell(NORM_TRI3,[0,1,2])
39         tri.setCoords(DataArrayDouble([(0.,0.),(0.,1.),(1.,0.)]))
40         tris=[tri.deepCpy() for i in xrange(4)]
41         for i,elt in enumerate(tris): elt.translate([i,0])
42         tris=MEDCouplingUMesh.MergeUMeshes(tris)
43         quad=MEDCouplingUMesh("quad",2)
44         quad.allocateCells() ; quad.insertNextCell(NORM_QUAD4,[0,1,2,3])
45         quad.setCoords(DataArrayDouble([(0.,0.),(0.,1.),(1.,1.),(1.,0.)]))
46         quads=[quad.deepCpy() for i in xrange(5)]
47         for i,elt in enumerate(quads): elt.translate([5+i,0])
48         quads=MEDCouplingUMesh.MergeUMeshes(quads)
49         m=MEDCouplingUMesh.MergeUMeshes(tris,quads)
50         m.setName("mesh") ; m.getCoords().setInfoOnComponents(["XX [m]","YYY [km]"])
51         m1=m.buildDescendingConnectivity()[0]
52         mm=MEDFileUMesh() ; mm.setMeshes([m,m1])
53         #
54         fieldName="zeField"
55         fs=MEDFileFieldMultiTS()
56         ##### Time step 0
57         i=0
58         f=MEDFileField1TS()
59         fCell0=MEDCouplingFieldDouble(ON_CELLS) ; fCell0.setTime(float(i),i,0)
60         fCell0.setName(fieldName) ; fCell0.setMesh(m)
61         arr=DataArrayDouble(2*m.getNumberOfCells()) ; arr.iota(100) ; arr.rearrange(2)
62         fCell0.setArray(arr) ; arr.setInfoOnComponents(["Comp1 [m]","Com2 [s^2]"])
63         fCell0.checkCoherency()
64         f.setFieldNoProfileSBT(fCell0)
65         fCell1=MEDCouplingFieldDouble(ON_CELLS) ; fCell1.setTime(float(i),i,0)
66         fCell1.setName(fieldName) ; fCell1.setMesh(m1)
67         arr=DataArrayDouble(2*m1.getNumberOfCells()) ; arr.iota(200) ; arr.rearrange(2)
68         fCell1.setArray(arr) ; arr.setInfoOnComponents(["Comp1 [m]","Com2 [s^2]"])
69         fCell1.checkCoherency()
70         f.setFieldNoProfileSBT(fCell1)
71         fs.pushBackTimeStep(f)
72         ##### Time step 1
73         i=1
74         f=MEDFileField1TS()
75         fCell0=MEDCouplingFieldDouble(ON_CELLS) ; fCell0.setTime(float(i),i,0)
76         fCell0.setName(fieldName) ; fCell0.setMesh(m)
77         arr=DataArrayDouble(2*m.getNumberOfCells()) ; arr.iota(1100) ; arr.rearrange(2)
78         fCell0.setArray(arr) ; arr.setInfoOnComponents(["Comp1 [m]","Com2 [s^2]"])
79         fCell0.checkCoherency()
80         f.setFieldNoProfileSBT(fCell0)
81         #
82         fCell1=MEDCouplingFieldDouble(ON_CELLS) ; fCell1.setTime(float(i),i,0)
83         fCell1.setName(fieldName) ; fCell1.setMesh(m1)
84         arr=DataArrayDouble(2*m1.getNumberOfCells()) ; arr.iota(1200) ; arr.rearrange(2)
85         fCell1.setArray(arr) ; arr.setInfoOnComponents(["Comp1 [m]","Com2 [s^2]"])
86         fCell1.checkCoherency()
87         f.setFieldNoProfileSBT(fCell1)
88         fs.pushBackTimeStep(f)
89         ##### Time step 2
90         i=2
91         f=MEDFileField1TS()
92         fCell0=MEDCouplingFieldDouble(ON_CELLS) ; fCell0.setTime(float(i),i,0)
93         fCell0.setName(fieldName) ; fCell0.setMesh(m)
94         arr=DataArrayDouble(2*m.getNumberOfCells()) ; arr.iota(2100) ; arr.rearrange(2)
95         fCell0.setArray(arr) ; arr.setInfoOnComponents(["Comp1 [m]","Com2 [s^2]"])
96         fCell0.checkCoherency()
97         f.setFieldNoProfileSBT(fCell0)
98         #
99         fCell1=MEDCouplingFieldDouble(ON_CELLS) ; fCell1.setTime(float(i),i,0)
100         fCell1.setName(fieldName) ; fCell1.setMesh(m1)
101         arr=DataArrayDouble(2*m1.getNumberOfCells()) ; arr.iota(2200) ; arr.rearrange(2)
102         fCell1.setArray(arr) ; arr.setInfoOnComponents(["Comp1 [m]","Com2 [s^2]"])
103         fCell1.checkCoherency()
104         f.setFieldNoProfileSBT(fCell1)
105         fs.pushBackTimeStep(f)
106         ##### Time step 3
107         i=3
108         f=MEDFileField1TS()
109         #
110         fCell0=MEDCouplingFieldDouble(ON_CELLS) ; fCell0.setTime(float(i),i,0)
111         fCell0.setName(fieldName) ; fCell0.setMesh(m)
112         arr=DataArrayDouble(2*m.getNumberOfCells()) ; arr.iota(3100) ; arr.rearrange(2)
113         fCell0.setArray(arr) ; arr.setInfoOnComponents(["Comp1 [m]","Com2 [s^2]"])
114         fCell0.checkCoherency()
115         f.setFieldNoProfileSBT(fCell0)
116         #
117         fCell1=MEDCouplingFieldDouble(ON_CELLS) ; fCell1.setTime(float(i),i,0)
118         fCell1.setName(fieldName) ; fCell1.setMesh(m1)
119         arr=DataArrayDouble(2*m1.getNumberOfCells()) ; arr.iota(3200) ; arr.rearrange(2)
120         fCell1.setArray(arr) ; arr.setInfoOnComponents(["Comp1 [m]","Com2 [s^2]"])
121         fCell1.checkCoherency()
122         f.setFieldNoProfileSBT(fCell1)
123         #
124         fs.pushBackTimeStep(f)
125         ##### Time step 4
126         i=4
127         f=MEDFileField1TS()
128         #
129         fCell0=MEDCouplingFieldDouble(ON_CELLS) ; fCell0.setTime(float(i),i,0)
130         fCell0.setName(fieldName) ; fCell0.setMesh(m)
131         arr=DataArrayDouble(2*m.getNumberOfCells()) ; arr.iota(4100) ; arr.rearrange(2)
132         fCell0.setArray(arr) ; arr.setInfoOnComponents(["Comp1 [m]","Com2 [s^2]"])
133         fCell0.checkCoherency()
134         f.setFieldNoProfileSBT(fCell0)
135         #
136         fCell1=MEDCouplingFieldDouble(ON_CELLS) ; fCell1.setTime(float(i),i,0)
137         fCell1.setName(fieldName) ; fCell1.setMesh(m1)
138         arr=DataArrayDouble(2*m1.getNumberOfCells()) ; arr.iota(4200) ; arr.rearrange(2)
139         fCell1.setArray(arr) ; arr.setInfoOnComponents(["Comp1 [m]","Com2 [s^2]"])
140         fCell1.checkCoherency()
141         f.setFieldNoProfileSBT(fCell1)
142         fs.pushBackTimeStep(f)
143         mm.write(fname,2)
144         fs.write(fname,0)
145         a0Exp=mm.getCoords().deepCpy()
146         del m,m1,mm,fs,f,fCell0,fCell1
147         ########## GO for reading in MEDReader, by not loading all. Mesh is fully loaded but not fields values
148         ms=MEDFileMeshes(fname)
149         fields=MEDFileFields(fname,False) # False is important to not read the values
150         fields_per_mesh=[fields.partOfThisLyingOnSpecifiedMeshName(meshName) for meshName in ms.getMeshesNames()]
151         allFMTSLeavesToDisplay=[]
152         for fields in fields_per_mesh:
153             allFMTSLeavesToDisplay2=[]
154             for fmts in fields:
155                 allFMTSLeavesToDisplay2+=fmts.splitDiscretizations()
156                 pass
157             allFMTSLeavesToDisplay.append(allFMTSLeavesToDisplay2)
158             pass
159         self.assertEqual(len(allFMTSLeavesToDisplay),1)
160         self.assertEqual(len(allFMTSLeavesToDisplay[0]),1)
161         for fmts in allFMTSLeavesToDisplay[0]:
162             self.assertEqual(fmts.getTimeSteps(),[(0,0,0.),(1,0,1.),(2,0,2.),(3,0,3.),(4,0,4.)]) # All discretizations have the same time series
163             pass
164         allFMTSLeavesPerTimeSeries=MEDFileAnyTypeFieldMultiTS.SplitIntoCommonTimeSeries(sum(allFMTSLeavesToDisplay,[]))
165         self.assertEqual(len(allFMTSLeavesPerTimeSeries),1)
166         self.assertEqual(len(allFMTSLeavesPerTimeSeries[0]),1)
167         allFMTSLeavesPerCommonSupport=MEDFileAnyTypeFieldMultiTS.SplitPerCommonSupport(allFMTSLeavesPerTimeSeries[0],ms[ms.getMeshesNames()[0]])
168         #
169         mst=MEDFileMeshStruct.New(ms[0])
170         fcscp=MEDFileFastCellSupportComparator.New(mst,allFMTSLeavesPerCommonSupport[0][0])
171         mml=fcscp.buildFromScratchDataSetSupport(0,fields)
172         self.assertTrue(isinstance(mml,MEDUMeshMultiLev))
173         for i in xrange(1,5):
174             self.assertTrue(fcscp.isDataSetSupportEqualToThePreviousOne(i,fields))
175             pass
176         a0,a1,a2,a3,a4,a5=mml.buildVTUArrays()
177         self.assertTrue(a0.isEqual(a0Exp,1e-12))
178         self.assertTrue(a1.isEqual(DataArrayByte([3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,5,5,5,5,9,9,9,9,9])))
179         self.assertTrue(a2.isEqual(DataArrayInt([2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,67,70,73,76,80,84,88,92,96])))
180         self.assertTrue(a3.isEqual(DataArrayInt([2,0,1,2,1,2,2,2,0,2,3,4,2,4,5,2,5,3,2,6,7,2,7,8,2,8,6,2,9,10,2,10,11,2,11,9,2,12,13,2,13,14,2,14,15,2,15,12,2,16,17,2,17,18,2,18,19,2,19,16,2,20,21,2,21,22,2,22,23,2,23,20,2,24,25,2,25,26,2,26,27,2,27,24,2,28,29,2,29,30,2,30,31,2,31,28,3,0,1,2,3,3,4,5,3,6,7,8,3,9,10,11,4,12,13,14,15,4,16,17,18,19,4,20,21,22,23,4,24,25,26,27,4,28,29,30,31])))
181         self.assertTrue(a4 is None)
182         self.assertTrue(a5 is None)
183         for i in xrange(5):
184             fsst=MEDFileField1TSStructItem.BuildItemFrom(fields[0][i],mst)
185             fields[0][i].loadArraysIfNecessary()
186             v=mml.buildDataArray(fsst,fields,fields[0][i].getUndergroundDataArray())
187             self.assertEqual(v.getHiddenCppPointer(),fields[0][i].getUndergroundDataArray().getHiddenCppPointer())
188             vExp=DataArrayDouble([200.,201.,202.,203.,204.,205.,206.,207.,208.,209.,210.,211.,212.,213.,214.,215.,216.,217.,218.,219.,220.,221.,222.,223.,224.,225.,226.,227.,228.,229.,230.,231.,232.,233.,234.,235.,236.,237.,238.,239.,240.,241.,242.,243.,244.,245.,246.,247.,248.,249.,250.,251.,252.,253.,254.,255.,256.,257.,258.,259.,260.,261.,262.,263.,100.,101.,102.,103.,104.,105.,106.,107.,108.,109.,110.,111.,112.,113.,114.,115.,116.,117.],41,2) ; vExp.setInfoOnComponents(['Comp1 [m]','Com2 [s^2]']) ; vExp+=i*1000
189             self.assertTrue(v.isEqual(vExp,1e-12))
190             pass
191         pass
192     pass
193
194 unittest.main()