Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Plugins / MEDReader / plugin / Test / testMEDReader3.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 outImgName="testMEDReader3.png"
30
31 def GenerateCase():
32   """ This test checks that invalid double time (not in [-1e299:1e299])
33    leads MEDReader to takes iteration numbers instead of double time to discriminate timesteps each other
34    See EDF 1956.
35   """
36   fname="testMEDReader3.med"
37   #########
38   m=MEDCouplingUMesh("mesh",2)
39   coords=DataArrayDouble([(0.,0.,0.),(1.,0.,0.),(1.,1.,0.),(0.,1.,0.)],4,3)
40   m.setCoords(coords)
41   m.allocateCells()
42   m.insertNextCell(NORM_QUAD4,[0,3,2,1])
43   mm=MEDFileUMesh()
44   mm.setMeshAtLevel(0,m)
45   mm.write(fname,2)
46   fmts=MEDFileFieldMultiTS()
47   #
48   f=MEDCouplingFieldDouble(ON_NODES) ; f.setName("ANodeField")
49   f.setTime(1.7976931348623157e+308,1,1) ; f.setMesh(m)
50   arr=DataArrayDouble([0.,4.,4.,4.]) ; arr.setInfoOnComponent(0,"aaa")
51   f.setArray(arr)
52   f1ts=MEDFileField1TS()
53   f1ts.setFieldNoProfileSBT(f)
54   fmts.pushBackTimeStep(f1ts)
55   #
56   f.setTime(1.7976931348623157e+308,2,2)
57   arr=DataArrayDouble([1.,4.,4.,4.]) ; arr.setInfoOnComponent(0,"aaa")
58   f.setArray(arr)
59   f1ts=MEDFileField1TS()
60   f1ts.setFieldNoProfileSBT(f)
61   fmts.pushBackTimeStep(f1ts)
62   #
63   f.setTime(1.7976931348623157e+308,3,3)
64   arr=DataArrayDouble([2.,4.,4.,4.]) ; arr.setInfoOnComponent(0,"aaa")
65   f.setArray(arr)
66   f1ts=MEDFileField1TS()
67   f1ts.setFieldNoProfileSBT(f)
68   fmts.pushBackTimeStep(f1ts)
69   #
70   f.setTime(1.7976931348623157e+308,4,6)
71   arr=DataArrayDouble([3.,4.,4.,4.]) ; arr.setInfoOnComponent(0,"aaa")
72   f.setArray(arr)
73   f1ts=MEDFileField1TS()
74   f1ts.setFieldNoProfileSBT(f)
75   fmts.pushBackTimeStep(f1ts)
76   #
77   f.setTime(1.7976931348623157e+308,5,7)
78   arr=DataArrayDouble([4.,4.,4.,4.]) ; arr.setInfoOnComponent(0,"aaa")
79   f.setArray(arr)
80   f1ts=MEDFileField1TS()
81   f1ts.setFieldNoProfileSBT(f)
82   fmts.pushBackTimeStep(f1ts)
83   #
84   fmts.write(fname,0)
85   return fname
86
87 @WriteInTmpDir
88 def test(baseline_file):
89   fname = GenerateCase()
90   ################### MED write is done -> Go to MEDReader
91   AnimationScene1=GetAnimationScene()
92   AnimationScene1.PlayMode='Snap To TimeSteps'
93   AnimationScene1.EndTime = 2.0
94   AnimationScene1.AnimationTime = 1.0
95   AnimationScene1.StartTime = 1.0
96
97   testMEDReader3=MEDReader(FileName=fname)
98   testMEDReader3.AllArrays=['TS0/mesh/ComSup0/ANodeField@@][@@P1']
99
100   assert(list(testMEDReader3.TimestepValues)==[1.,2.,3.,4.,5.]) ## <- the test is here - double time steps are too big use dt.
101
102   if '-D' not in sys.argv:
103     RenderView1=GetRenderView()
104     RenderView1.ViewTime=3.0
105     RenderView1.CameraPosition=[0.5,0.5,5.7320508075688776]
106     RenderView1.ViewSize=[300,300]
107
108     DataRepresentation2=Show()
109     DataRepresentation2.EdgeColor=[0.0, 0.0, 0.5000076295109483]
110     DataRepresentation2.SelectionPointFieldDataArrayName='ANodeField'
111     DataRepresentation2.SelectionCellFieldDataArrayName='FamilyIdCell'
112     DataRepresentation2.ScalarOpacityUnitDistance=1.4142135623730951
113     DataRepresentation2.ExtractedBlockIndex=1
114     DataRepresentation2.ScaleFactor=0.1
115
116     a1_ANodeField_PVLookupTable=GetLookupTableForArray("ANodeField",1,RGBPoints=[0.0,0.23,0.299,0.754,4.0,0.706,0.016,0.15],VectorMode='Magnitude',NanColor=[0.25,0.0,0.0],
117                                                          ColorSpace='Diverging',ScalarRangeInitialized=1.0,AllowDuplicateScalars=1)
118     a1_ANodeField_PiecewiseFunction=CreatePiecewiseFunction(Points=[0.0,0.0,0.5,0.0,1.0,1.0,0.5,0.0])
119     a1_ANodeField_PVLookupTable.ScalarOpacityFunction=a1_ANodeField_PiecewiseFunction
120
121     DataRepresentation2.ScalarOpacityFunction=a1_ANodeField_PiecewiseFunction
122     DataRepresentation2.ColorArrayName='ANodeField'
123     DataRepresentation2.LookupTable=a1_ANodeField_PVLookupTable
124
125     # Triangulate so rendring always the same with different gpu or graphic backend.
126     extSurf = ExtractSurface(Input=testMEDReader3)
127     triangulate = Triangulate(Input=extSurf)
128     Hide(testMEDReader3, RenderView1)
129     Show(triangulate, RenderView1)
130     triangulate1Display = GetDisplayProperties(triangulate, view=RenderView1)
131     ColorBy(triangulate1Display, ('POINTS', 'ANodeField'))
132
133     Render()
134     ###
135
136     # compare with baseline image
137     import vtk.test.Testing
138     from vtk.util.misc import vtkGetTempDir
139     vtk.test.Testing.VTK_TEMP_DIR = vtk.util.misc.vtkGetTempDir()
140     vtk.test.Testing.compareImage(GetActiveView().GetRenderWindow(), baseline_file,
141                                                                 threshold=1)
142     vtk.test.Testing.interact()
143     
144 if __name__ == "__main__":
145   outImgName="testMEDReader3.png"
146   baseline_file = RetriveBaseLine(outImgName)
147   test(baseline_file)
148   pass