1 # Copyright (C) 2016-2021 CEA/DEN, EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #### import the simple module from the paraview
21 from paraview.simple import *
22 import MEDLoader as ml
24 from math import pi,sqrt
27 #### disable automatic camera reset on 'Show'
28 paraview.simple._DisableFirstRenderCameraReset()
29 with tempfile.TemporaryDirectory(prefix="MEDWriter_") as tmpdir:
31 fname_int64 = os.path.join(tmpdir, 'testMEDWriter_int64.med')
32 fname_int64_exported = os.path.join(tmpdir, 'testMEDWriter_int64_exported.med')
34 ### test with int array field
36 fieldNameOnCells = "field_on_cells"
37 fieldNameOnNodes = "field_on_nodes"
39 c=ml.DataArrayDouble([0.,0., 1.,0., 1.,1., 0.,1.],4,2)
40 c.setInfoOnComponents(["X","Y"])
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])
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"])
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]))
65 mm1.write(fname_int64,2)
67 # field of int64 on cells
68 f1ts12=ml.MEDFileInt64Field1TS()
69 f1=ml.MEDCouplingFieldInt64(ml.ON_CELLS)
70 f1.setName(fieldNameOnCells)
72 f1.setArray(ml.DataArrayInt64([0,1]))
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])
84 f1ts12n.setFieldNoProfileSBT(f1n)
85 f1ts12n.write(fname_int64,0)
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]
93 assert(c.isEqualWithoutConsideringStr(c1[:,:2],1e-12))
94 fs2=ml.MEDFileFields(fname_int64_exported)
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))
104 # check field on cells
105 f2_0=mfd2.getFields()[fieldNameOnCells][0].getFieldOnMeshAtLevel(ml.ON_CELLS,0,mm2)
107 #assert(f1ts12.getFieldOnMeshAtLevel(ml.ON_CELLS,0,mm1).isEqual(f2_0,1e-12,0))
108 assert(f1.isEqual(f2_0,1e-12,0))
110 # check field on nodes
111 f2_1=mfd2.getFields()[fieldNameOnNodes][0].getFieldOnMeshAtLevel(ml.ON_NODES,0,mm2)
113 #assert(f1ts12n.getFieldOnMeshAtLevel(ml.ON_NODES,0,mm1).isEqual(f2_1,1e-12,0))
114 assert(f1n.isEqual(f2_1,1e-12,0))
116 # check field of cells numbers (set by setRenumFieldArr at level 0)
118 f3.setName("NumIdCell")
119 f3_0=mfd2.getFields()["NumIdCell"][0].getFieldOnMeshAtLevel(ml.ON_CELLS,0,mm2)
121 assert(f3.isEqual(f3_0,1e-12,0))
123 # check field of nodes numbers (set by setRenumFieldArr at level 1)
125 f4.setName("NumIdNode")
126 f4_1=mfd2.getFields()["NumIdNode"][0].getFieldOnMeshAtLevel(ml.ON_NODES,0,mm2)
128 assert(f4.isEqual(f4_1,1e-12,0))