Salome HOME
Avoid 'make test' failure on invalid DISPLAY
[modules/paravis.git] / src / Plugins / MEDReader / Test / testMEDReader10.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2017  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 MEDLoader import *
26
27 """ This test focuses on GenerateVector part.
28 """
29
30 fname="testMEDReader10.med"
31 outImgName="testMEDReader10.png"
32 ##############
33 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.)])
34 m=MEDCouplingUMesh("myMesh",3) ; m.setCoords(coo)
35 m.allocateCells()
36 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])
37 f0=MEDCouplingFieldDouble(ON_NODES) ; f0.setName("f0NbComp1") ; f0.setMesh(m) ;
38 arr=DataArrayDouble(8) ; arr.iota() ; arr=DataArrayDouble.Aggregate(arr,arr+8) ; arr.setInfoOnComponents(["1st"])
39 f0.setArray(arr)
40 f1=MEDCouplingFieldDouble(ON_NODES) ; f1.setName("f1NbComp2") ; f1.setMesh(m) ;
41 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"])
42 f1.setArray(arr)
43 f2=MEDCouplingFieldDouble(ON_NODES) ; f2.setName("f2NbComp3") ; f2.setMesh(m) ;
44 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.
45 arr=DataArrayDouble.Aggregate(arr,arr2) ; arr.setInfoOnComponents(["1st","2nd","3rd"])
46 f2.setArray(arr)
47 f3=MEDCouplingFieldDouble(ON_NODES) ; f3.setName("f3NbComp4") ; f3.setMesh(m) ;
48 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.
49 arr=DataArrayDouble.Aggregate(arr,arr2) ; arr3=DataArrayDouble(16) ; arr3.iota() ; arr=DataArrayDouble.Meld([arr,arr3])
50 arr.setInfoOnComponents(["1st","2nd","3rd","4th"])
51 f3.setArray(arr)
52 #
53 WriteMesh(fname,m,True)
54 WriteFieldUsingAlreadyWrittenMesh(fname,f0)
55 WriteFieldUsingAlreadyWrittenMesh(fname,f1)
56 WriteFieldUsingAlreadyWrittenMesh(fname,f2)
57 WriteFieldUsingAlreadyWrittenMesh(fname,f3)
58 ################### MED write is done -> Go to MEDReader
59 from paraview.simple import *
60
61 myMedReader=MEDReader(FileName=fname)
62 keys=myMedReader.GetProperty("FieldsTreeInfo")[::2]
63 # list all the names of arrays that can be seen (including their spatial discretization)
64 arr_name_with_dis=[elt.split("/")[-1] for elt in keys]
65 # list all the names of arrays (Equal to those in the MED File)
66 arr_name=[elt.split(myMedReader.GetProperty("Separator").GetData())[0] for elt in arr_name_with_dis]
67 myMedReader.AllArrays=keys
68 myMedReader.GenerateVectors=1
69
70 if '-D' not in sys.argv:
71   RenderView1=GetRenderView()
72   DataRepresentation1 = Show()
73   DataRepresentation1.EdgeColor = [0.0, 0.0, 0.5]
74   DataRepresentation1.SelectionPointFieldDataArrayName='f0NbComp1'
75   DataRepresentation1.ScalarOpacityUnitDistance = 2.299619191183727
76   #DataRepresentation1.ExtractedBlockIndex = 1
77   DataRepresentation1.ScaleFactor = 0.3
78   RenderView1.CenterOfRotation = [1.5, 0.5, 0.5]
79   RenderView1.CameraPosition = [1.5, 0.5, 6.907227082229696]
80   RenderView1.CameraFocalPoint = [1.5, 0.5, 0.5]
81   RenderView1.CameraParallelScale = 1.6583123951777
82   DataRepresentation1.Representation = 'Wireframe'
83   #
84   wbv=WarpByVector(Input=myMedReader)
85   wbv.ScaleFactor=0.1
86   wbv.Vectors=['POINTS','f3NbComp4_Vector']
87   assert(list(wbv.PointData.keys())==['f0NbComp1','f1NbComp2','f1NbComp2_Vector','f2NbComp3','f3NbComp4','f3NbComp4_Vector'])
88   #
89   DataRepresentation2 = Show()
90   DataRepresentation2.EdgeColor = [0.0, 0.0, 0.5000076295109483]
91   DataRepresentation2.SelectionPointFieldDataArrayName = 'f0NbComp1'
92   DataRepresentation2.SelectionCellFieldDataArrayName = 'FamilyIdCell'
93   DataRepresentation2.ScalarOpacityUnitDistance = 4.546673931685981
94   #DataRepresentation2.ExtractedBlockIndex = 1
95   DataRepresentation2.ScaleFactor = 0.5
96   DataRepresentation1.Visibility = 0
97   DataRepresentation1.ColorArrayName = ('POINT_DATA', '')
98   DataRepresentation1.Visibility = 1
99   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 )
100   a4_f3NbComp4_PiecewiseFunction = CreatePiecewiseFunction( Points=[1.118033988749895, 0.0, 0.5, 0.0, 15.0996688705415, 1.0, 0.5, 0.0] )
101   DataRepresentation1.ScalarOpacityFunction = a4_f3NbComp4_PiecewiseFunction
102   DataRepresentation1.LookupTable = a4_f3NbComp4_PVLookupTable
103   a4_f3NbComp4_PVLookupTable.ScalarOpacityFunction = a4_f3NbComp4_PiecewiseFunction
104   DataRepresentation2.ScalarOpacityFunction = a4_f3NbComp4_PiecewiseFunction
105   DataRepresentation2.ColorArrayName = ('POINT_DATA', 'f3NbComp4')
106   DataRepresentation2.LookupTable = a4_f3NbComp4_PVLookupTable
107   RenderView1.CameraViewUp = [0.32310237626907823, -0.28721735329781684, -0.901726702728644]
108   RenderView1.CameraPosition = [0.17151044563787257, 6.31903262423349, -1.8294921560963984]
109   RenderView1.CameraFocalPoint = [1.4999999999999996, 0.49999999999999944, 0.4999999999999996]
110   #
111   RenderView1.ViewSize =[300,300]
112   Render()
113
114   # compare with baseline image
115   try:
116     baselineIndex = sys.argv.index('-B')+1
117     baselinePath = sys.argv[baselineIndex]
118   except:
119     print("Could not get baseline directory. Test failed.")
120     exit(1)
121   baseline_file = os.path.join(baselinePath, "testMEDReader10.png")
122   import vtk.test.Testing
123   from vtk.util.misc import vtkGetTempDir
124   vtk.test.Testing.VTK_TEMP_DIR = vtk.util.misc.vtkGetTempDir()
125   vtk.test.Testing.compareImage(GetActiveView().GetRenderWindow(), baseline_file,
126                                                               threshold=1)
127   vtk.test.Testing.interact()