1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2020 CEA/DEN, EDF R&D
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, or (at your option) any later version.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 # Author : Anthony Geay (EDF R&D)
24 Test of GroupsAsMultiBlocks filter in the MEDReader plugin.
27 from paraview.simple import *
28 paraview.simple._DisableFirstRenderCameraReset()
29 from vtk.util import numpy_support
31 import medcoupling as mc
32 from MEDReaderHelper import WriteInTmpDir,RetriveBaseLine
36 raise RuntimeError("Assertion failed !")
38 def generateCase(fname):
39 arr = mc.DataArrayDouble([0,1,2,3,4,5])
40 m = mc.MEDCouplingCMesh()
42 m = m.buildUnstructured()
43 m.changeSpaceDimension(3,0.)
45 mm = mc.MEDFileUMesh()
47 grp_BottomLeft = mc.DataArrayInt([0,1,5,6]) ; grp_BottomLeft.setName("BottomLeft")
48 grp_BottomRight = mc.DataArrayInt([3,4,8,9]) ; grp_BottomRight.setName("BottomRight")
49 grp_TopLeft = mc.DataArrayInt([15,16,20,21]) ; grp_TopLeft.setName("TopLeft")
50 grp_TopRight = mc.DataArrayInt([18,19,23,24]) ; grp_TopRight.setName("TopRight")
51 mm.setGroupsAtLevel(0,[grp_BottomLeft,grp_BottomRight,grp_TopLeft,grp_TopRight])
53 f = mc.MEDCouplingFieldDouble(mc.ON_CELLS)
55 f.setArray(m.computeCellCenterOfMass().magnitude())
57 mc.WriteFieldUsingAlreadyWrittenMesh(fname,f)
58 f2 = mc.MEDCouplingFieldDouble(mc.ON_NODES)
60 f2.setArray(m.getCoords().magnitude())
62 mc.WriteFieldUsingAlreadyWrittenMesh(fname,f2)
63 return f,f2,grp_BottomLeft,grp_BottomRight,grp_TopLeft,grp_TopRight
67 fname = "testMEDReader22.med"
68 f,f2,grp_BottomLeft,grp_BottomRight,grp_TopLeft,grp_TopRight = generateCase(fname)
69 reader = MEDReader(FileName=fname)
70 reader.AllArrays = ['TS0/mesh/ComSup0/field@@][@@P0','TS0/mesh/ComSup0/field2@@][@@P1','TS0/mesh/ComSup0/mesh@@][@@P0']
71 reader.AllTimeSteps = ['0000']
73 groupsAsMultiBlocks = GroupsAsMultiBlocks(Input=reader)
74 groupsAsMultiBlocks.UpdatePipeline()
75 blocks = servermanager.Fetch(groupsAsMultiBlocks)
76 MyAssert(blocks.GetNumberOfBlocks()==4)
78 ds_bl = blocks.GetBlock(0)
79 MyAssert(ds_bl.GetNumberOfCells()==4)
80 bl_ref_conn = np.array([ 1, 0, 6, 7, 2, 1, 7, 8, 7, 6, 12, 13, 8, 7, 13, 14],dtype=np.int32)
81 MyAssert(ds_bl.GetCellData().GetNumberOfArrays() == 3 )# 3 for field, mesh and FamilyIdCell
82 MyAssert(np.all( bl_ref_conn == numpy_support.vtk_to_numpy( ds_bl.GetCells().GetConnectivityArray() )) )
83 MyAssert( mc.DataArrayDouble(numpy_support.vtk_to_numpy( ds_bl.GetCellData().GetArray("field") )).isEqual(f.getArray()[grp_BottomLeft],1e-12) )
84 # test of bottom right
85 ds_br = blocks.GetBlock(1)
86 MyAssert(ds_br.GetNumberOfCells()==4)
87 br_ref_conn = np.array([ 4, 3, 9, 10, 5, 4, 10, 11, 10, 9, 15, 16, 11, 10, 16, 17],dtype=np.int32)
88 MyAssert(np.all( br_ref_conn == numpy_support.vtk_to_numpy( ds_br.GetCells().GetConnectivityArray() )) )
89 MyAssert( mc.DataArrayDouble(numpy_support.vtk_to_numpy( ds_br.GetCellData().GetArray("field") )).isEqual(f.getArray()[grp_BottomRight],1e-12) )
91 ds_tl = blocks.GetBlock(2)
92 MyAssert(ds_tl.GetNumberOfCells()==4)
93 tl_ref_conn = np.array([ 19, 18, 24, 25, 20, 19, 25, 26, 25, 24, 30, 31, 26, 25, 31, 32],dtype=np.int32)
94 MyAssert(np.all( tl_ref_conn == numpy_support.vtk_to_numpy( ds_tl.GetCells().GetConnectivityArray() )) )
95 MyAssert( mc.DataArrayDouble(numpy_support.vtk_to_numpy( ds_tl.GetCellData().GetArray("field") )).isEqual(f.getArray()[grp_TopLeft],1e-12) )
97 ds_tr = blocks.GetBlock(3)
98 MyAssert(ds_tr.GetNumberOfCells()==4)
99 tr_ref_conn = np.array([ 22, 21, 27, 28, 23, 22, 28, 29, 28, 27, 33, 34, 29, 28, 34, 35],dtype=np.int32)
100 MyAssert(np.all( tr_ref_conn == numpy_support.vtk_to_numpy( ds_tr.GetCells().GetConnectivityArray() )) )
101 MyAssert( mc.DataArrayDouble(numpy_support.vtk_to_numpy( ds_tr.GetCellData().GetArray("field") )).isEqual(f.getArray()[grp_TopRight],1e-12) )
103 for ds in [ds_bl,ds_br,ds_tl,ds_tr]:
104 MyAssert(ds.GetPointData().GetNumberOfArrays() == 1 )# 1 for field2
105 MyAssert(ds.GetNumberOfPoints()==36) # for performance reasons all blocks share the same input coordinates
106 MyAssert(mc.DataArrayDouble( numpy_support.vtk_to_numpy( ds.GetPointData().GetArray("field2") ) ).isEqual(f2.getArray(),1e-12) )
108 if __name__ == "__main__":