Salome HOME
Avoid 'make test' failure on invalid DISPLAY
[modules/paravis.git] / src / Plugins / MEDReader / Test / testMEDReader14.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 (EDF R&D)
21
22 import os
23 import sys
24
25 from MEDLoader import *
26
27 """ This test reproduces precisely the bug EDF8655 (PAL 22677) by cheking that multi loc per geo type in a field is correctly managed.
28 """
29
30 fname="testMEDReader14.med"
31 outImgName="testMEDReader14.png"
32
33 arr0=DataArrayDouble(7) ; arr0.iota()
34 arr1=DataArrayDouble(5) ; arr1.iota()
35 c=MEDCouplingCMesh()
36 c.setCoords(arr0,arr1)
37 m=c.build1SGTUnstructured()
38 pfl=DataArrayInt([5,10,11,16,17,23])
39 m0=m[pfl.buildComplement(24)]
40 m1=m[pfl] ; m1.simplexize(0)
41 m=MEDCouplingUMesh.MergeUMeshesOnSameCoords(m1.buildUnstructured(),m0.buildUnstructured())
42 m.setName("Mesh")
43 m1=MEDCoupling1SGTUMesh(m.getName(),NORM_SEG2)
44 m1.setCoords(m.getCoords())
45 m1.allocateCells()
46 for i in [[28,21],[21,14],[14,7],[7,0]]:
47     m1.insertNextCell(i)
48     pass
49 for i in range(6):
50     m1.insertNextCell([i,i+1])
51     pass
52 for i in [[6,13],[13,20],[20,27],[27,34]]:
53     m1.insertNextCell(i)
54     pass
55 for i in range(6,0,-1):
56     m1.insertNextCell([28+i,28+i-1])
57     pass
58 #
59 mm=MEDFileUMesh()
60 mm.setMeshAtLevel(0,m)
61 mm.setMeshAtLevel(-1,m1)
62 mm.write(fname,2)
63 #
64 pfl0=DataArrayInt([0,1,2,3]) ; pfl0.setName("PFL000")
65 pfl1=DataArrayInt([4,5,6,7,8,9]) ; pfl1.setName("PFL001")
66 pfl2=DataArrayInt([10,11,12,13]) ; pfl2.setName("PFL002")
67 pfl3=DataArrayInt([13,14,15,16,19,20,23,24,26,27,28,29]) ; pfl3.setName("PFL003")
68 #
69 fieldName0="zeField0"
70 fieldName1="zeField1"
71 fieldName2="zeField2"
72 fs0=MEDFileFieldMultiTS()
73 fs1=MEDFileFieldMultiTS()
74 fs2=MEDFileFieldMultiTS()
75 for i in range(5):
76     f=MEDFileField1TS()
77     zePfl0=DataArrayInt.Aggregate(DataArrayInt.Range(0,12,1),pfl3,0) ; zePfl0.setName("PFL")
78     fNode=MEDCouplingFieldDouble(ON_GAUSS_PT) ; fNode.setTime(float(i),i,0)
79     tmp=m[zePfl0]
80     fNode.setName(fieldName0) ; fNode.setMesh(tmp)
81     fNode.setGaussLocalizationOnCells(DataArrayInt.Range(0,12,1),[0.,0.,1.,0.,0.,1.],[0.1,0.1,0.8,0.1,0.1,0.8],3*[0.16666666666666666])
82     fNode.setGaussLocalizationOnCells(DataArrayInt.Range(12,24,1),[-1.,-1.,1.,-1.,1.,1.,-1.,1.],[-0.577350,-0.577350,0.577350,-0.577350,0.577350,0.577350,-0.577350,0.577350],4*[1.])
83     arr=DataArrayDouble(2*(12*3+12*4)) ; arr.iota(0+1000*i) ; arr.rearrange(2)
84     fNode.setArray(arr) ; arr.setInfoOnComponents(["Comp1_2 [m]","Com2_2 [s^2]"]) ; fNode.checkConsistencyLight()
85     fNode.setName(fieldName0)
86     f.setFieldProfile(fNode,mm,0,zePfl0)
87     #
88     zePfl1=DataArrayInt.Range(0,14,1) ; zePfl1.setName("PFL")
89     fNode=MEDCouplingFieldDouble(ON_GAUSS_PT) ; fNode.setTime(float(i),i,0)
90     tmp=m1[zePfl1]
91     #
92     fNode.setName(fieldName0) ; fNode.setMesh(tmp)
93     fNode.setGaussLocalizationOnCells(DataArrayInt.Range(0,4,1),[-1.,1.],[-0.33333333333333337,0.33333333333333337],[1.,1.])
94     fNode.setGaussLocalizationOnCells(DataArrayInt.Range(4,10,1),[-1.,1.],[-0.5,0.,0.5],[1.,1.,1.])
95     fNode.setGaussLocalizationOnCells(DataArrayInt.Range(10,14,1),[-1.,1.],[-0.6,-0.2,0.2,0.6],[1.,1.,1.,1.])
96     arr=DataArrayDouble(2*(4*2+6*3+4*4)) ; arr.iota(100+1000*i) ; arr.rearrange(2)
97     fNode.setArray(arr) ; arr.setInfoOnComponents(["Comp1_2 [m]","Com2_2 [s^2]"]) ; fNode.checkConsistencyLight()
98     fNode.setName(fieldName0)
99     f.setFieldProfile(fNode,mm,-1,zePfl1)
100     fs0.pushBackTimeStep(f)
101     #
102     f=MEDFileField1TS()
103     zePfl2=DataArrayInt.Range(10,20,1) ; zePfl2.setName("PFL2")
104     fNode=MEDCouplingFieldDouble(ON_GAUSS_PT) ; fNode.setTime(float(i),i,0)
105     tmp=m1[zePfl2]
106     fNode.setName(fieldName1) ; fNode.setMesh(tmp)
107     fNode.setGaussLocalizationOnCells(DataArrayInt.Range(0,4,1),[-1.,1.],[-0.6,-0.2,0.2,0.6],[1.,1.,1.,1.])
108     fNode.setGaussLocalizationOnCells(DataArrayInt.Range(4,10,1),[-1.,1.],[-0.6666666666666667,-0.33333333333333337,0.,0.33333333333333337,0.6666666666666667],[1.,1.,1.,1.,1.])
109     arr=DataArrayDouble(2*(4*4+6*5)) ; arr.iota(500+1000*i) ; arr.rearrange(2)
110     fNode.setArray(arr) ; arr.setInfoOnComponents(["C1 [m]","C2 [s^2]"]) ; fNode.checkConsistencyLight()
111     f.setFieldProfile(fNode,mm,-1,zePfl2)
112     fs1.pushBackTimeStep(f)
113     #
114     f=MEDFileField1TS()
115     zePfl3=DataArrayInt([6,10,11,12,13,17,18,19,20,24,25,26,27,34]) ; zePfl3.setName("PFL3")
116     fNode=MEDCouplingFieldDouble(ON_NODES) ; fNode.setTime(float(i),i,0)
117     fNode.setName(fieldName2)
118     arr=DataArrayDouble(2*14) ; arr.iota(700+1000*i) ; arr.rearrange(2)
119     fNode.setArray(arr) ; arr.setInfoOnComponents(["C3 [kg]","C4 [m^2]"])
120     f.setFieldProfile(fNode,mm,0,zePfl3)
121     fs2.pushBackTimeStep(f)
122     pass
123 fs0.write(fname,0)
124 fs1.write(fname,0)
125 fs2.write(fname,0)
126
127 #
128
129 from paraview.simple import *
130
131 paraview.simple._DisableFirstRenderCameraReset()
132 reader=MEDReader(FileName=fname)
133 ExpectedEntries=['TS0/Mesh/ComSup0/zeField0_MM0@@][@@GAUSS', 'TS0/Mesh/ComSup1/zeField0_MM1@@][@@GAUSS', 'TS0/Mesh/ComSup2/zeField0_MM2@@][@@GAUSS', 'TS0/Mesh/ComSup2/zeField1_MM0@@][@@GAUSS', 'TS0/Mesh/ComSup3/zeField1_MM1@@][@@GAUSS', 'TS0/Mesh/ComSup4/zeField2@@][@@P1', 'TS1/Mesh/ComSup0/Mesh@@][@@P0']
134 assert(reader.GetProperty("FieldsTreeInfo")[::2]==ExpectedEntries)
135
136 if '-D' not in sys.argv:
137     renderView1=GetActiveViewOrCreate('RenderView')
138     for entry in [[ExpectedEntries[0]],[ExpectedEntries[1]],[ExpectedEntries[2],ExpectedEntries[3]],[ExpectedEntries[4]]]:
139         reader=MEDReader(FileName=fname)
140         reader.AllArrays=entry
141         gaussPoints=ELGAfieldToPointGaussian(Input=reader)
142         gaussPoints.SelectSourceArray="ELGA@0"
143         Show(gaussPoints,renderView1)
144         pass
145
146     #
147
148     readerNodeField=MEDReader(FileName=fname)
149     readerNodeField.AllArrays=[ExpectedEntries[5]]
150     nodeFieldDisplay=Show(readerNodeField,renderView1)
151     ColorBy(nodeFieldDisplay,('POINTS','zeField2'))
152     nodeFieldDisplay.RescaleTransferFunctionToDataRange(True)
153     zeField2LUT=GetColorTransferFunction('zeField2')
154     zeField2LUT.RGBPoints=[990.6568528002015, 0.231373, 0.298039, 0.752941, 1009.0416245953584, 0.865003, 0.865003, 0.865003, 1027.4263963905153, 0.705882, 0.0156863, 0.14902]
155     zeField2LUT.ScalarRangeInitialized=1.
156     #
157     renderView1.ResetCamera()
158     renderView1.InteractionMode = '2D'
159     renderView1.CameraPosition = [3.0, 2.0, 10000.0]
160     renderView1.CameraFocalPoint = [3.0, 2.0, 0.0]
161     renderView1.ViewSize =[300,300]
162     renderView1.GetRenderWindow().DoubleBufferOff()
163     #
164
165     Render()
166
167     # compare with baseline image
168     try:
169       baselineIndex = sys.argv.index('-B')+1
170       baselinePath = sys.argv[baselineIndex]
171     except:
172       print("Could not get baseline directory. Test failed.")
173       exit(1)
174     baseline_file = os.path.join(baselinePath, "testMEDReader14.png")
175     import vtk.test.Testing
176     from vtk.util.misc import vtkGetTempDir
177     vtk.test.Testing.VTK_TEMP_DIR = vtk.util.misc.vtkGetTempDir()
178     vtk.test.Testing.compareImage(renderView1.GetRenderWindow(), baseline_file,
179                                                                 threshold=1)
180     vtk.test.Testing.interact()