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