Salome HOME
Copyright update 2022
[modules/paravis.git] / bin / paravis_test.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2018-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
21 import os
22 import time
23 import unittest
24
25 class TestParavis(unittest.TestCase):
26
27     def setUp(self):
28         import salome
29         salome.salome_init()
30
31     def processGuiEvents(self):
32         import salome
33         if salome.sg.hasDesktop():
34             salome.sg.updateObjBrowser();
35             import SalomePyQt
36             SalomePyQt.SalomePyQt().processEvents()
37
38     def test_paravis(self):
39         """Quick test for Paravis module"""
40
41         import salome
42         if salome.hasDesktop(): # can test paravis in gui mode only
43
44             print()
45             print('Testing Paravis module')
46
47             # ---- initialize Paravis
48             print('... Initialize Paravis module (this can take some time)')
49             self.processGuiEvents()
50             import pvsimple as pvs
51             self.processGuiEvents()
52
53             # ---- get data dir
54             data_dir = os.getenv('DATA_DIR', '')
55             self.assertNotEqual(data_dir, '', 'DATA_DIR should be defined to load the test data')
56
57             # ---- import med File
58             print('... Load MED file')
59             file_path = os.path.join(os.getenv('DATA_DIR'), 'MedFiles', 'ResOK_0000.med')
60             med_reader = pvs.MEDReader(FileName=file_path)
61             self.assertIsNotNone(med_reader)
62             times = med_reader.TimestepValues.GetData()
63             self.assertEqual(len(times), 2)
64             self.processGuiEvents()
65
66             # ---- get view
67             print('... Get view')
68             view = pvs.GetRenderView()
69             self.assertIsNotNone(view)
70             view.ResetCamera()
71             self.processGuiEvents()
72
73             # ---- create presentations
74             print("... Display presentations")
75
76             # ---- show mesh
77             print('...... Show mesh')
78             mesh = pvs.Show(med_reader, view)
79             self.assertIsNotNone(mesh)
80             view.ResetCamera()
81             mesh.Representation = 'Surface With Edges'
82             pvs.Render(view)
83             time.sleep(1)
84             self.processGuiEvents()
85
86             # ---- show scalar map
87             print('...... Show scalar map')
88             scalar_map = mesh
89             self.assertIsNotNone(scalar_map)
90             scalar_map.Representation = 'Surface'
91             pvs.ColorBy(scalar_map, ('POINTS', 'vitesse', 'Magnitude'))
92             view.ViewTime = times[-1]
93             pvs.Render(view)
94             time.sleep(1)
95             scalar_map.Visibility = 0
96             pvs.Render(view)
97             self.processGuiEvents()
98
99             # ---- show vectors
100             print('...... Show vectors')
101             calc = pvs.Calculator(Input=med_reader)
102             self.assertIsNotNone(calc)
103             calc.ResultArrayName = 'vitesse_3c'
104             calc.Function = 'iHat * vitesse_X + jHat * vitesse_Y + kHat * 0'
105             glyph = pvs.Glyph(Input=calc, GlyphType='Arrow')
106             self.assertIsNotNone(glyph)
107             glyph.OrientationArray = ['POINTS', 'vitesse_3c']
108             glyph.ScaleArray = ['POINTS', 'No scale array']
109             glyph.ScaleFactor = 0.01
110             vectors = pvs.Show(glyph, view)
111             self.assertIsNotNone(vectors)
112             vectors.Representation = 'Surface'
113             pvs.Render(view)
114             time.sleep(1)
115             vectors.Visibility = 0
116             pvs.Render(view)
117             self.processGuiEvents()
118
119             # ---- show iso surfaces
120             print('...... Show iso surfaces')
121             merge_blocks = pvs.MergeBlocks(Input=med_reader)
122             self.assertIsNotNone(merge_blocks)
123             calc = pvs.Calculator(Input=merge_blocks)
124             self.assertIsNotNone(calc)
125             calc.ResultArrayName = 'vitesse_magnitude'
126             calc.Function = 'sqrt(vitesse_X^2+vitesse_Y^2)'
127             data_range = med_reader.GetPointDataInformation()['vitesse'].GetComponentRange(-1)
128             nb_surfaces = 10
129             surfaces = [data_range[0] + i*(data_range[1]-data_range[0])/(nb_surfaces-1) for i in range(nb_surfaces)]
130             contour = pvs.Contour(Input=calc)
131             self.assertIsNotNone(contour)
132             contour.ComputeScalars = 1
133             contour.ContourBy = ['POINTS', 'vitesse_magnitude']
134             contour.Isosurfaces = surfaces
135             iso_surfaces = pvs.Show(contour, view)
136             self.assertIsNotNone(iso_surfaces)
137             iso_surfaces.Representation = 'Surface'
138             pvs.ColorBy(iso_surfaces, ('POINTS', 'vitesse', 'Magnitude'))
139             pvs.Render(view)
140             time.sleep(1)
141             iso_surfaces.Visibility = 0
142             pvs.Render(view)
143             self.processGuiEvents()
144
145             # ---- show cut planes
146             print('...... Show cut planes')
147             slice = pvs.Slice(Input=med_reader)
148             self.assertIsNotNone(slice)
149             slice.SliceType = "Plane"
150             slice.SliceType.Normal = [1.0, 0.0, 0.0]
151             bounds = med_reader.GetDataInformation().GetBounds()
152             nb_planes = 30
153             displacement = 0.5
154             b_left = bounds[0] + (bounds[1]-bounds[0])*displacement/100
155             b_right = bounds[1] - (bounds[1]-bounds[0])*displacement/100
156             b_range = b_right - b_left
157             positions = [b_left + i*b_range/(nb_planes-1) for i in range(nb_planes)]
158             slice.SliceOffsetValues = positions
159             pvs.Hide3DWidgets(proxy=slice.SliceType)
160             cut_planes = pvs.Show(slice, view)
161             self.assertIsNotNone(cut_planes)
162             cut_planes.Representation = 'Surface'
163             pvs.ColorBy(cut_planes, ('POINTS', 'vitesse', 'Magnitude'))
164             pvs.Render(view)
165             time.sleep(1)
166             cut_planes.Visibility = 0
167             pvs.Render(view)
168             self.processGuiEvents()
169
170             # ---- show deformed shape
171             print('...... Show deformed shape')
172             merge_blocks = pvs.MergeBlocks(Input=med_reader)
173             self.assertIsNotNone(merge_blocks)
174             calc = pvs.Calculator(Input=merge_blocks)
175             self.assertIsNotNone(calc)
176             calc.ResultArrayName = 'vitesse_3c'
177             calc.Function = 'iHat * vitesse_X + jHat * vitesse_Y + kHat * 0'
178             warp = pvs.WarpByVector(Input=calc)
179             self.assertIsNotNone(warp)
180             warp.Vectors = ['POINTS', 'vitesse_3c']
181             warp.ScaleFactor = 0.5
182             deformed_shape = pvs.Show(warp, view)
183             self.assertIsNotNone(deformed_shape)
184             deformed_shape.Representation = 'Surface'
185             pvs.ColorBy(deformed_shape, ('CELLS', 'pression'))
186             pvs.Render(view)
187             time.sleep(1)
188             deformed_shape.Visibility = 0
189             pvs.Render(view)
190             self.processGuiEvents()
191
192         else: # not in gui mode, Paravis can not be tested
193             print()
194             print("PARAVIS module requires SALOME to be running in GUI mode.")
195             print()
196             print("Skipping test for PARAVIS...")
197             self.processGuiEvents()
198
199 if __name__ == '__main__':
200     unittest.main()