Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Plugins / MEDReader / plugin / Test / testMEDReader10.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2022  CEA/DEN, EDF R&D
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author : Anthony Geay
21
22 import os
23 import sys
24
25 from medcoupling import *
26 from paraview.simple import *
27 from MEDReaderHelper import WriteInTmpDir,RetriveBaseLine
28
29 def GenerateCase():
30   """ This test focuses on GenerateVector part.
31   """
32   fname="testMEDReader10.med"
33   ##############
34   coo=DataArrayDouble([(0.,0.,0.),(1.,0.,0.),(2.,0.,0.),(3.,0.,0.),(0.,1.,0.),(1.,1.,0.),(2.,1.,0.),(3.,1.,0.),(0.,0.,1.),(1.,0.,1.),(2.,0.,1.),(3.,0.,1.),(0.,1.,1.),(1.,1.,1.),(2.,1.,1.),(3.,1.,1.)])
35   m=MEDCouplingUMesh("myMesh",3) ; m.setCoords(coo)
36   m.allocateCells()
37   m.insertNextCell(NORM_HEXA8,[0,4,5,1,8,12,13,9]) ; m.insertNextCell(NORM_HEXA8,[1,5,6,2,9,13,14,10]) ; m.insertNextCell(NORM_HEXA8,[2,6,7,3,10,14,15,11])
38   f0=MEDCouplingFieldDouble(ON_NODES) ; f0.setName("f0NbComp1") ; f0.setMesh(m) ;
39   arr=DataArrayDouble(8) ; arr.iota() ; arr=DataArrayDouble.Aggregate(arr,arr+8) ; arr.setInfoOnComponents(["1st"])
40   f0.setArray(arr)
41   f1=MEDCouplingFieldDouble(ON_NODES) ; f1.setName("f1NbComp2") ; f1.setMesh(m) ;
42   arr=DataArrayDouble([(-1.,-1.),(0.,0.5),(0.,0.5),(1.,-1.),(-1.,1.),(0.,-0.5),(0.,-0.5),(1.,1.)]) ; arr=DataArrayDouble.Aggregate(arr,arr) ; arr.setInfoOnComponents(["1st","2nd"])
43   f1.setArray(arr)
44   f2=MEDCouplingFieldDouble(ON_NODES) ; f2.setName("f2NbComp3") ; f2.setMesh(m) ;
45   arr=DataArrayDouble([(-1.,-1.,-1.),(0.,0.5,0.),(0.,0.5,0.),(1.,-1.,-1.),(-1.,1.,-1.),(0.,-0.5,0.),(0.,-0.5,0.),(1.,1.,-1)]) ; arr2=arr[:] ; arr2[[0,3,4,7],2]=1.
46   arr=DataArrayDouble.Aggregate(arr,arr2) ; arr.setInfoOnComponents(["1st","2nd","3rd"])
47   f2.setArray(arr)
48   f3=MEDCouplingFieldDouble(ON_NODES) ; f3.setName("f3NbComp4") ; f3.setMesh(m) ;
49   arr=DataArrayDouble([(-1.,-1.,-1.),(0.,0.5,0.),(0.,0.5,0.),(1.,-1.,-1.),(-1.,1.,-1.),(0.,-0.5,0.),(0.,-0.5,0.),(1.,1.,-1)]) ; arr2=arr[:] ; arr2[[0,3,4,7],2]=1.
50   arr=DataArrayDouble.Aggregate(arr,arr2) ; arr3=DataArrayDouble(16) ; arr3.iota() ; arr=DataArrayDouble.Meld([arr,arr3])
51   arr.setInfoOnComponents(["1st","2nd","3rd","4th"])
52   f3.setArray(arr)
53   #
54   WriteMesh(fname,m,True)
55   WriteFieldUsingAlreadyWrittenMesh(fname,f0)
56   WriteFieldUsingAlreadyWrittenMesh(fname,f1)
57   WriteFieldUsingAlreadyWrittenMesh(fname,f2)
58   WriteFieldUsingAlreadyWrittenMesh(fname,f3)
59   return fname
60
61
62 @WriteInTmpDir
63 def test(baseline_file):
64   fname = GenerateCase()
65   ################### MED write is done -> Go to MEDReader
66   myMedReader=MEDReader(FileName=fname)
67   keys=myMedReader.GetProperty("FieldsTreeInfo")[::2]
68   # list all the names of arrays that can be seen (including their spatial discretization)
69   arr_name_with_dis=[elt.split("/")[-1] for elt in keys]
70   # list all the names of arrays (Equal to those in the MED File)
71   arr_name=[elt.split(myMedReader.GetProperty("Separator").GetData())[0] for elt in arr_name_with_dis]
72   myMedReader.AllArrays=keys
73   myMedReader.GenerateVectors=1
74
75   if '-D' not in sys.argv:
76     RenderView1=GetRenderView()
77     DataRepresentation1 = Show()
78     DataRepresentation1.EdgeColor = [0.0, 0.0, 0.5]
79     DataRepresentation1.SelectionPointFieldDataArrayName='f0NbComp1'
80     DataRepresentation1.ScalarOpacityUnitDistance = 2.299619191183727
81     #DataRepresentation1.ExtractedBlockIndex = 1
82     DataRepresentation1.ScaleFactor = 0.3
83     RenderView1.CenterOfRotation = [1.5, 0.5, 0.5]
84     RenderView1.CameraPosition = [1.5, 0.5, 6.907227082229696]
85     RenderView1.CameraFocalPoint = [1.5, 0.5, 0.5]
86     RenderView1.CameraParallelScale = 1.6583123951777
87     DataRepresentation1.Representation = 'Wireframe'
88     #
89     wbv=WarpByVector(Input=myMedReader)
90     wbv.ScaleFactor=0.1
91     wbv.Vectors=['POINTS','f3NbComp4_Vector']
92     assert(list(wbv.PointData.keys())==['f0NbComp1','f1NbComp2','f1NbComp2_Vector','f2NbComp3','f3NbComp4','f3NbComp4_Vector'])
93     #
94     DataRepresentation2 = Show()
95     DataRepresentation2.EdgeColor = [0.0, 0.0, 0.5000076295109483]
96     DataRepresentation2.SelectionPointFieldDataArrayName = 'f0NbComp1'
97     DataRepresentation2.SelectionCellFieldDataArrayName = 'FamilyIdCell'
98     DataRepresentation2.ScalarOpacityUnitDistance = 4.546673931685981
99     #DataRepresentation2.ExtractedBlockIndex = 1
100     DataRepresentation2.ScaleFactor = 0.5
101     DataRepresentation1.Visibility = 0
102     DataRepresentation1.ColorArrayName = ('POINT_DATA', '')
103     DataRepresentation1.Visibility = 1
104     a4_f3NbComp4_PVLookupTable = GetLookupTableForArray( "f3NbComp4", 4, RGBPoints=[1.118033988749895, 0.23, 0.299, 0.754, 8.108851429645696, 0.865, 0.865, 0.865, 15.0996688705415, 0.706, 0.016, 0.15], VectorMode='Magnitude', NanColor=[0.25, 0.0, 0.0], ColorSpace='Diverging', ScalarRangeInitialized=1.0 )
105     a4_f3NbComp4_PiecewiseFunction = CreatePiecewiseFunction( Points=[1.118033988749895, 0.0, 0.5, 0.0, 15.0996688705415, 1.0, 0.5, 0.0] )
106     DataRepresentation1.ScalarOpacityFunction = a4_f3NbComp4_PiecewiseFunction
107     DataRepresentation1.LookupTable = a4_f3NbComp4_PVLookupTable
108     a4_f3NbComp4_PVLookupTable.ScalarOpacityFunction = a4_f3NbComp4_PiecewiseFunction
109     DataRepresentation2.ScalarOpacityFunction = a4_f3NbComp4_PiecewiseFunction
110     DataRepresentation2.ColorArrayName = ('POINT_DATA', 'f3NbComp4')
111     DataRepresentation2.LookupTable = a4_f3NbComp4_PVLookupTable
112     RenderView1.CameraViewUp = [0.32310237626907823, -0.28721735329781684, -0.901726702728644]
113     RenderView1.CameraPosition = [0.17151044563787257, 6.31903262423349, -1.8294921560963984]
114     RenderView1.CameraFocalPoint = [1.4999999999999996, 0.49999999999999944, 0.4999999999999996]
115     #
116     RenderView1.ViewSize =[300,300]
117     Render()
118
119     # compare with baseline image
120     import vtk.test.Testing
121     from vtk.util.misc import vtkGetTempDir
122     vtk.test.Testing.VTK_TEMP_DIR = vtk.util.misc.vtkGetTempDir()
123     vtk.test.Testing.compareImage(GetActiveView().GetRenderWindow(), baseline_file,
124                                                                 threshold=1)
125     vtk.test.Testing.interact()
126
127 if __name__ == "__main__":
128   outImgName="testMEDReader10.png"
129   baseline_file = RetriveBaseLine(outImgName)
130   test(baseline_file)
131   pass