-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInitialize_real_time_v2.py
339 lines (260 loc) · 11.3 KB
/
Initialize_real_time_v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from paraview.simple import *
import threading
import time
paraview.simple._DisableFirstRenderCameraReset()
buildingsstl = STLReader(registrationName='buildings.stl', FileNames=['/home/boris/OpenFOAM/boris-v2206/run/Clean/Marina_Particles/constant/triSurface/buildings.stl']) # Load the STL for graphical representation
renderView1 = GetActiveViewOrCreate('RenderView')
# show data in view
buildingsstlDisplay = Show(buildingsstl, renderView1, 'GeometryRepresentation')
# get color transfer function/color map for 'STLSolidLabeling'
sTLSolidLabelingLUT = GetColorTransferFunction('STLSolidLabeling')
# trace defaults for the display properties.
buildingsstlDisplay.Representation = 'Surface'
buildingsstlDisplay.ColorArrayName = ['CELLS', 'STLSolidLabeling']
buildingsstlDisplay.LookupTable = sTLSolidLabelingLUT
buildingsstlDisplay.SelectTCoordArray = 'None'
buildingsstlDisplay.SelectNormalArray = 'None'
buildingsstlDisplay.SelectTangentArray = 'None'
buildingsstlDisplay.OSPRayScaleFunction = 'PiecewiseFunction'
buildingsstlDisplay.SelectOrientationVectors = 'None'
buildingsstlDisplay.ScaleFactor = 198.62966918945312
buildingsstlDisplay.SelectScaleArray = 'STLSolidLabeling'
buildingsstlDisplay.GlyphType = 'Arrow'
buildingsstlDisplay.GlyphTableIndexArray = 'STLSolidLabeling'
buildingsstlDisplay.GaussianRadius = 9.931483459472657
buildingsstlDisplay.SetScaleArray = [None, '']
buildingsstlDisplay.ScaleTransferFunction = 'PiecewiseFunction'
buildingsstlDisplay.OpacityArray = [None, '']
buildingsstlDisplay.OpacityTransferFunction = 'PiecewiseFunction'
buildingsstlDisplay.DataAxesGrid = 'GridAxesRepresentation'
buildingsstlDisplay.PolarAxes = 'PolarAxesRepresentation'
buildingsstlDisplay.SelectInputVectors = [None, '']
buildingsstlDisplay.WriteLog = ''
# reset view to fit data
renderView1.ResetCamera(False)
# get the material library
materialLibrary1 = GetMaterialLibrary()
# show color bar/color legend
buildingsstlDisplay.SetScalarBarVisibility(renderView1, True)
# update the view to ensure updated data information
renderView1.Update()
# get opacity transfer function/opacity map for 'STLSolidLabeling'
sTLSolidLabelingPWF = GetOpacityTransferFunction('STLSolidLabeling')
# get 2D transfer function for 'STLSolidLabeling'
sTLSolidLabelingTF2D = GetTransferFunction2D('STLSolidLabeling')
# turn off scalar coloring
ColorBy(buildingsstlDisplay, None)
# Hide the scalar bar for this color map if no visible data is colored by it.
HideScalarBarIfNotNeeded(sTLSolidLabelingLUT, renderView1)
# create a new 'Live Programmable Source'
liveProgrammableSource1 = LiveProgrammableSource(registrationName='LiveProgrammableSource1')
liveProgrammableSource1.Script = ''
liveProgrammableSource1.ScriptRequestInformation = ''
liveProgrammableSource1.PythonPath = ''
liveProgrammableSource1.ScriptCheckNeedsUpdate = ''
# Properties modified on liveProgrammableSource1
liveProgrammableSource1.OutputDataSetType = 'vtkTable'
## This script reads the existing parquet file series and display them at the correct time step
liveProgrammableSource1.Script = """import numpy as np
import pandas as pd
import time
# assuming data.csv is a CSV file with the 1st row being the names names for
# the columns
def GetUpdateTimestep(algorithm):
executive = algorithm.GetExecutive()
outInfo = executive.GetOutputInformation(0)
return outInfo.Get(executive.UPDATE_TIME_STEP()) \
if outInfo.Has(executive.UPDATE_TIME_STEP()) else None
req_time = GetUpdateTimestep(self)
data=np.empty([1,3])
data_x=data[:,0]
data_y=data[:,1]
data_z=data[:,2]
current_time_simulated_file= open("./current_time_simulated.txt","r")
simulation_time=int(current_time_simulated_file.read())
if req_time > simulation_time-10: # If the vizualisation goes too fast with respect to the simulation, wait a second
time.sleep(1)
print("Displaying faster than simulating, waiting for a second..")
for i in range(0,6):
try:
current_data=pd.read_parquet("./csv{1}/particles_positions_{0}.parquet".format(str(int(req_time)),i)).values
data_x=np.concatenate((data_x,current_data[:,0]))
data_y=np.concatenate((data_y,current_data[:,1]))
data_z=np.concatenate((data_z,current_data[:,2]))
except:
pass
current_time_file= open("./current_time.txt","w+")
current_time_file.write(str(int(req_time)))
current_time_file.close()
output.GetInformation().Set(output.DATA_TIME_STEP(), req_time)
data_x=np.delete(data_x,0)
data_y=np.delete(data_y,0)
data_z=np.delete(data_z,0)
output.RowData.append(data_x, "X")
output.RowData.append(data_y, "Y")
output.RowData.append(data_z, "Z")
coms = pd.read_csv("front_end_back_end_communication.csv")
while coms.loc[0, 'pause'] == 1:
coms = pd.read_csv("front_end_back_end_communication.csv")
continue
"""
# This scripts generate timesteps corresponding to the number of parquet file existing for paraview
liveProgrammableSource1.ScriptRequestInformation = """def setOutputTimesteps(algorithm, timesteps):
executive = algorithm.GetExecutive()
outInfo = executive.GetOutputInformation(0)
outInfo.Remove(executive.TIME_STEPS())
for timestep in timesteps:
outInfo.Append(executive.TIME_STEPS(), timestep)
outInfo.Remove(executive.TIME_RANGE())
outInfo.Append(executive.TIME_RANGE(), timesteps[0])
outInfo.Append(executive.TIME_RANGE(), timesteps[-1])
setOutputTimesteps(self, range(0,10000))
"""
liveProgrammableSource1.PythonPath = ''
# This script updates the source every second
liveProgrammableSource1.ScriptCheckNeedsUpdate = """
import time
import os
if not hasattr(self, "_my_time"):
setattr(self, "_my_time", time.time())
t = time.time()
lastTime = getattr(self, "_my_time")
# adapt here to set the time (in sec) you want to wait between updates
if t - lastTime > 2:
setattr(self, "_my_time", t)
self.SetNeedsUpdate(True)
"""
# Create a new 'SpreadSheet View'
spreadSheetView1 = CreateView('SpreadSheetView')
spreadSheetView1.ColumnToSort = ''
spreadSheetView1.BlockSize = 1024
# show data in view
liveProgrammableSource1Display = Show(liveProgrammableSource1, spreadSheetView1, 'SpreadSheetRepresentation')
# get layout
layout1 = GetLayoutByName("Layout #1")
# add view to a layout so it's visible in UI
AssignViewToLayout(view=spreadSheetView1, layout=layout1, hint=0)
# Properties modified on liveProgrammableSource1Display
liveProgrammableSource1Display.Assembly = ''
# get animation scene
animationScene1 = GetAnimationScene()
# get the time-keeper
timeKeeper1 = GetTimeKeeper()
# update animation scene based on data timesteps
animationScene1.UpdateAnimationUsingDataTimeSteps()
# update the view to ensure updated data information
renderView1.Update()
# update the view to ensure updated data information
spreadSheetView1.Update()
# destroy spreadSheetView1
Delete(spreadSheetView1)
del spreadSheetView1
# close an empty frame
layout1.Collapse(2)
# set active view
SetActiveView(renderView1)
# create a new 'Table To Points'
tableToPoints1 = TableToPoints(registrationName='TableToPoints1', Input=liveProgrammableSource1)
tableToPoints1.XColumn = 'X'
tableToPoints1.YColumn = 'X'
tableToPoints1.ZColumn = 'X'
# Properties modified on tableToPoints1
tableToPoints1.YColumn = 'Y'
tableToPoints1.ZColumn = 'Z'
# show data in view
tableToPoints1Display = Show(tableToPoints1, renderView1, 'GeometryRepresentation')
# trace defaults for the display properties.
tableToPoints1Display.Representation = 'Surface'
tableToPoints1Display.ColorArrayName = [None, '']
tableToPoints1Display.SelectTCoordArray = 'None'
tableToPoints1Display.SelectNormalArray = 'None'
tableToPoints1Display.SelectTangentArray = 'None'
tableToPoints1Display.OSPRayScaleFunction = 'PiecewiseFunction'
tableToPoints1Display.SelectOrientationVectors = 'None'
tableToPoints1Display.ScaleFactor = 1.3371082652686948
tableToPoints1Display.SelectScaleArray = 'None'
tableToPoints1Display.GlyphType = 'Arrow'
tableToPoints1Display.GlyphTableIndexArray = 'None'
tableToPoints1Display.GaussianRadius = 0.06685541326343473
tableToPoints1Display.SetScaleArray = [None, '']
tableToPoints1Display.ScaleTransferFunction = 'PiecewiseFunction'
tableToPoints1Display.OpacityArray = [None, '']
tableToPoints1Display.OpacityTransferFunction = 'PiecewiseFunction'
tableToPoints1Display.DataAxesGrid = 'GridAxesRepresentation'
tableToPoints1Display.PolarAxes = 'PolarAxesRepresentation'
tableToPoints1Display.SelectInputVectors = [None, '']
tableToPoints1Display.WriteLog = ''
# update the view to ensure updated data information
renderView1.Update()
#exec(open("./update.py").read()) # Important script for live update
#exec(open("./Initialize.py").read())
import time
import threading
def update_slice():
exec(open("paraview_vizu_script.py").read())
time.sleep(5)
t1=threading.Thread(target=update_slice)
t1.start()
#exec(open("Initialize_real_time_v2.py").read())
import time
import threading
def vizu():
try:
Delete(afoam)
Delete(slice1)
except:
pass
wind_value=5
wind_direction=75
afoam = XMLMultiBlockDataReader(registrationName='afoam', FileName=['/home/boris/OpenFOAM/boris-v2206/run/Clean/Marina_Particles/cases/val_{0}_ang_{1}.vtm'.format(wind_value,wind_direction)]) # Depending on the slider position, the simulator with select a vtm file representing the field at that given angle
slice1 = Slice(registrationName='Slice1', Input=afoam)
slice1.SliceType = 'Plane'
slice1.HyperTreeGridSlicer = 'Plane'
slice1.SliceOffsetValues = [0.0]
slice1.SliceType.Origin = [0.0, 0.0, 100.0]
slice1.SliceType.Normal = [0.0, 0.0, 1.0]
# get active view
renderView1 = GetActiveViewOrCreate('RenderView')
HideInteractiveWidgets(proxy=slice1.SliceType)
# show data in view
slice1Display = Show(slice1, renderView1, 'GeometryRepresentation')
slice1Display.Representation = 'Surface'
slice1Display.ColorArrayName = [None, '']
slice1Display.SelectTCoordArray = 'None'
slice1Display.SelectNormalArray = 'None'
slice1Display.SelectTangentArray = 'None'
slice1Display.OSPRayScaleArray = 'U'
slice1Display.OSPRayScaleFunction = 'PiecewiseFunction'
slice1Display.SelectOrientationVectors = 'U'
slice1Display.ScaleFactor = 399.99992675781255
slice1Display.SelectScaleArray = 'None'
slice1Display.GlyphType = 'Arrow'
slice1Display.GlyphTableIndexArray = 'None'
slice1Display.GaussianRadius = 19.999996337890625
slice1Display.SetScaleArray = ['POINTS', 'U']
slice1Display.ScaleTransferFunction = 'PiecewiseFunction'
slice1Display.OpacityArray = ['POINTS', 'U']
slice1Display.OpacityTransferFunction = 'PiecewiseFunction'
slice1Display.DataAxesGrid = 'GridAxesRepresentation'
slice1Display.PolarAxes = 'PolarAxesRepresentation'
slice1Display.SelectInputVectors = ['POINTS', 'U']
slice1Display.WriteLog = ''
renderView1.Update()
ColorBy(slice1Display, ('POINTS', 'U', 'Magnitude'))
slice1Display.RescaleTransferFunctionToDataRange(True, False)
# show color bar/color legend
slice1Display.SetScalarBarVisibility(renderView1, True)
# get color transfer function/color map for 'U'
uLUT = GetColorTransferFunction('U')
# get opacity transfer function/opacity map for 'U'
uPWF = GetOpacityTransferFunction('U')
# get 2D transfer function for 'U'
uTF2D = GetTransferFunction2D('U')
def update_slice():
vizu()
time.sleep(5)
t1=threading.Thread(target=update_slice)
t1.start()
#exec(open("Initialize_real_time_v2.py").read())
def lol():
print("lol")