1 # Copyright (C) 2016-2021 CEA/DES, 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
26 #### disable automatic camera reset on 'Show'
27 paraview.simple._DisableFirstRenderCameraReset()
29 #fname_int32='testMEDWriter_int32.med'
30 fname_int64 = 'testMEDWriter_int64.med'
31 fname_int64_exported = 'testMEDWriter_int64_exported.med'
33 ### test with int array field
35 fieldNameOnCells = "field_on_cells"
36 fieldNameOnNodes = "field_on_nodes"
38 c=ml.DataArrayDouble([0.,0., 1.,0., 1.,1., 0.,1.],4,2)
39 c.setInfoOnComponents(["X","Y"])
42 m1=ml.MEDCouplingUMesh(mName,2) ; m1.allocateCells() ; m1.setCoords(c)
43 m1.insertNextCell(ml.NORM_TRI3,[0,1,2])
44 m1.insertNextCell(ml.NORM_TRI3,[0,2,3])
48 mm1.setFamilyFieldArr(0,ml.DataArrayInt([1, 2]))
49 mm1.setFamilyId("Fam1",1)
50 mm1.setFamilyId("Fam2",2)
51 mm1.setFamiliesOnGroup("tri1",["Fam1"])
52 mm1.setFamiliesOnGroup("tri2",["Fam2"])
54 mm1.setFamilyFieldArr(1,ml.DataArrayInt([-1, -1, -2, -2]))
55 mm1.setFamilyId("Fam_1",-1)
56 mm1.setFamilyId("Fam_2",-2)
57 mm1.setFamiliesOnGroup("bottom",["Fam_1"])
58 mm1.setFamiliesOnGroup("top",["Fam_2"])
59 # ids of cells (write the cells ids as it is done is SMESH)
60 mm1.setRenumFieldArr(0, ml.DataArrayInt([0,1]))
61 # ids of nodes (write the nodes ids as it is done is SMESH)
62 mm1.setRenumFieldArr(1, ml.DataArrayInt([0,1,2,3]))
64 mm1.write(fname_int64,2)
66 # field of int64 on cells
67 f1ts12=ml.MEDFileInt64Field1TS()
68 f1=ml.MEDCouplingFieldInt64(ml.ON_CELLS)
69 f1.setName(fieldNameOnCells)
71 f1.setArray(ml.DataArrayInt64([0,1]))
73 f1ts12.setFieldNoProfileSBT(f1)
74 f1ts12.write(fname_int64,0)
75 # field of int64 on nodes
76 f1ts12n=ml.MEDFileInt64Field1TS()
77 f1n=ml.MEDCouplingFieldInt64(ml.ON_NODES)
78 f1n.setName(fieldNameOnNodes)
79 arr=ml.DataArrayInt64([0,1,2,3])
83 f1ts12n.setFieldNoProfileSBT(f1n)
84 f1ts12n.write(fname_int64,0)
86 test12=MEDReader(FileName=fname_int64)
87 #test12.AllArrays=['TS0/%s/ComSup0/%s@@][@@P0'%(mName,fieldNameOnCells),'TS0/%s/ComSup0/%s@@][@@P1'%(mName,fieldNameOnNodes)]
88 #test12.AllTimeSteps = ['0000']
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]
94 assert(c.isEqualWithoutConsideringStr(c1[:,:2],1e-12))
95 fs2=ml.MEDFileFields(fname_int64_exported)
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))
105 # check field on cells
106 f2_0=mfd2.getFields()[fieldNameOnCells][0].getFieldOnMeshAtLevel(ml.ON_CELLS,0,mm2)
108 #assert(f1ts12.getFieldOnMeshAtLevel(ml.ON_CELLS,0,mm1).isEqual(f2_0,1e-12,0))
109 assert(f1.isEqual(f2_0,1e-12,0))
111 # check field on nodes
112 f2_1=mfd2.getFields()[fieldNameOnNodes][0].getFieldOnMeshAtLevel(ml.ON_NODES,0,mm2)
114 #assert(f1ts12n.getFieldOnMeshAtLevel(ml.ON_NODES,0,mm1).isEqual(f2_1,1e-12,0))
115 assert(f1n.isEqual(f2_1,1e-12,0))
117 # check field of cells numbers (set by setRenumFieldArr at level 0)
119 f3.setName("NumIdCell")
120 f3_0=mfd2.getFields()["NumIdCell"][0].getFieldOnMeshAtLevel(ml.ON_CELLS,0,mm2)
122 assert(f3.isEqual(f3_0,1e-12,0))
124 # check field of nodes numbers (set by setRenumFieldArr at level 1)
126 f4.setName("NumIdNode")
127 f4_1=mfd2.getFields()["NumIdNode"][0].getFieldOnMeshAtLevel(ml.ON_NODES,0,mm2)
129 assert(f4.isEqual(f4_1,1e-12,0))