Salome HOME
MEDWriter tests write in tmp directory
[modules/paravis.git] / src / Plugins / MEDWriter / plugin / Test / TestMEDWriter1.py
1 # Copyright (C) 2016-2021  CEA/DES, EDF R&D
2 #
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, or (at your option) any later version.
7 #
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.
12 #
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
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 #### import the simple module from the paraview
21 from paraview.simple import *
22 import MEDLoader as ml
23 import os
24 from math import pi,sqrt
25 import tempfile
26
27 #### disable automatic camera reset on 'Show'
28 paraview.simple._DisableFirstRenderCameraReset()
29
30 tmpdir = tempfile.TemporaryDirectory(prefix="MEDWriter_")
31
32 fname_int64 = os.path.join(tmpdir.name, 'testMEDWriter_int64.med')
33 fname_int64_exported = os.path.join(tmpdir.name, 'testMEDWriter_int64_exported.med')
34
35 ### test with int array field
36
37 fieldNameOnCells = "field_on_cells"
38 fieldNameOnNodes = "field_on_nodes"
39
40 c=ml.DataArrayDouble([0.,0., 1.,0., 1.,1., 0.,1.],4,2)
41 c.setInfoOnComponents(["X","Y"])
42 #
43 mName="mesh"
44 m1=ml.MEDCouplingUMesh(mName,2) ; m1.allocateCells() ; m1.setCoords(c)
45 m1.insertNextCell(ml.NORM_TRI3,[0,1,2])
46 m1.insertNextCell(ml.NORM_TRI3,[0,2,3])
47 mm1=ml.MEDFileUMesh()
48 mm1[0]=m1
49 # family on cell
50 mm1.setFamilyFieldArr(0,ml.DataArrayInt([1, 2]))
51 mm1.setFamilyId("Fam1",1)
52 mm1.setFamilyId("Fam2",2)
53 mm1.setFamiliesOnGroup("tri1",["Fam1"])
54 mm1.setFamiliesOnGroup("tri2",["Fam2"])
55 # family on nodes
56 mm1.setFamilyFieldArr(1,ml.DataArrayInt([-1, -1, -2, -2]))
57 mm1.setFamilyId("Fam_1",-1)
58 mm1.setFamilyId("Fam_2",-2)
59 mm1.setFamiliesOnGroup("bottom",["Fam_1"])
60 mm1.setFamiliesOnGroup("top",["Fam_2"])
61 # ids of cells (write the cells ids as it is done is SMESH)
62 mm1.setRenumFieldArr(0, ml.DataArrayInt([0,1]))
63 # ids of nodes (write the nodes ids as it is done is SMESH)
64 mm1.setRenumFieldArr(1, ml.DataArrayInt([0,1,2,3]))
65 # write mesh
66 mm1.write(fname_int64,2)
67
68 # field of int64 on cells
69 f1ts12=ml.MEDFileInt64Field1TS()
70 f1=ml.MEDCouplingFieldInt64(ml.ON_CELLS)
71 f1.setName(fieldNameOnCells)
72 f1.setMesh(m1)
73 f1.setArray(ml.DataArrayInt64([0,1]))
74 f1.setTime(0,0,0)
75 f1ts12.setFieldNoProfileSBT(f1)
76 f1ts12.write(fname_int64,0)
77 # field of int64 on nodes
78 f1ts12n=ml.MEDFileInt64Field1TS()
79 f1n=ml.MEDCouplingFieldInt64(ml.ON_NODES)
80 f1n.setName(fieldNameOnNodes)
81 arr=ml.DataArrayInt64([0,1,2,3])
82 f1n.setMesh(m1)
83 f1n.setArray(arr)
84 f1n.setTime(0,0,0)
85 f1ts12n.setFieldNoProfileSBT(f1n)
86 f1ts12n.write(fname_int64,0)
87
88 test12=MEDReader(FileName=fname_int64)
89 SaveData(fname_int64_exported, WriteAllTimeSteps=1)
90 ### test content of fname_int64_exported
91 mfd2=ml.MEDFileData(fname_int64_exported)
92 mm2=mfd2.getMeshes()[0]
93 c1=mm2.getCoords()
94 assert(c.isEqualWithoutConsideringStr(c1[:,:2],1e-12))
95 fs2=ml.MEDFileFields(fname_int64_exported)
96
97 # check mesh
98 assert(mm2.getSpaceDimension()==3)
99 assert(mm2.getCoords()[:,2].isUniform(0.,0.))
100 m2_0=mm2[0].deepCopy()
101 m2_0.changeSpaceDimension(2,0.)
102 m2_0.getCoords().setInfoOnComponents(mm1[0].getCoords().getInfoOnComponents())
103 assert(m2_0.isEqual(mm1[0],1e-12))
104
105 # check field on cells
106 f2_0=mfd2.getFields()[fieldNameOnCells][0].getFieldOnMeshAtLevel(ml.ON_CELLS,0,mm2)
107 f2_0.setMesh(m2_0)
108 #assert(f1ts12.getFieldOnMeshAtLevel(ml.ON_CELLS,0,mm1).isEqual(f2_0,1e-12,0))
109 assert(f1.isEqual(f2_0,1e-12,0))
110
111 # check field on nodes
112 f2_1=mfd2.getFields()[fieldNameOnNodes][0].getFieldOnMeshAtLevel(ml.ON_NODES,0,mm2)
113 f2_1.setMesh(m2_0)
114 #assert(f1ts12n.getFieldOnMeshAtLevel(ml.ON_NODES,0,mm1).isEqual(f2_1,1e-12,0))
115 assert(f1n.isEqual(f2_1,1e-12,0))
116
117 # check field of cells numbers (set by setRenumFieldArr at level 0)
118 f3=f1.deepCopy()
119 f3.setName("NumIdCell")
120 f3_0=mfd2.getFields()["NumIdCell"][0].getFieldOnMeshAtLevel(ml.ON_CELLS,0,mm2)
121 f3_0.setMesh(m2_0)
122 assert(f3.isEqual(f3_0,1e-12,0))
123
124 # check field of nodes numbers (set by setRenumFieldArr at level 1)
125 f4=f1n.deepCopy()
126 f4.setName("NumIdNode")
127 f4_1=mfd2.getFields()["NumIdNode"][0].getFieldOnMeshAtLevel(ml.ON_NODES,0,mm2)
128 f4_1.setMesh(m2_0)
129 assert(f4.isEqual(f4_1,1e-12,0))