11import os
22
33import numpy as np
4- from tvtk .api import tvtk , write_data
54
65import sharpy .utils .cout_utils as cout
76from sharpy .utils .solver_interface import solver , BaseSolver
87import sharpy .utils .settings as settings_utils
98import sharpy .utils .algebra as algebra
109
1110
11+ import vtk
12+ from vtk .numpy_interface import algorithms as algs
13+ from vtk .numpy_interface import dataset_adapter as dsa
14+
15+
1216@solver
1317class BeamPlot (BaseSolver ):
1418 """
@@ -118,9 +122,7 @@ def plot(self, online):
118122 self .write_for (it )
119123
120124 def write_beam (self , it ):
121- it_filename = (self .filename +
122- ('%06u' % it ) +
123- '.vtu' )
125+ it_filename = f"{ self .filename } { it :06d} .vtp"
124126 num_nodes = self .data .structure .num_node
125127 num_elem = self .data .structure .num_elem
126128
@@ -132,7 +134,6 @@ def write_beam(self, it):
132134 local_y = np .zeros ((num_nodes , 3 ))
133135 local_z = np .zeros ((num_nodes , 3 ))
134136 coords_a = np .zeros ((num_nodes , 3 ))
135-
136137 app_forces = np .zeros ((num_nodes , 3 ))
137138 app_moment = np .zeros ((num_nodes , 3 ))
138139
@@ -150,14 +151,9 @@ def write_beam(self, it):
150151 if tstep .mb_dict is None :
151152 pass
152153 else :
153- #TODO: fix for lack of g frame description in nonlineardynamicmultibody.py
154154 for i_node in range (tstep .num_node ):
155- #TODO: uncomment for dynamic trim
156- # try:
157- # c = algebra.euler2rot([0, self.data.trimmed_values[0], 0])
158- # except AttributeError:
159155 c = self .data .structure .timestep_info [0 ].cga ()
160- coords [i_node , :] += np . dot ( c , tstep .for_pos [0 :3 ])
156+ coords [i_node , :] += c @ tstep .for_pos [:3 ]
161157
162158 # check if I can output gravity forces
163159 with_gravity = False
@@ -260,115 +256,128 @@ def write_beam(self, it):
260256 conn [i_elem , :] = self .data .structure .elements [i_elem ].reordered_global_connectivities
261257 elem_id [i_elem ] = i_elem
262258
263- ug = tvtk .UnstructuredGrid (points = coords )
264- ug .set_cells (tvtk .Line ().cell_type , conn )
265- ug .cell_data .scalars = elem_id
266- ug .cell_data .scalars .name = 'elem_id'
267- counter = 1
259+ ug = vtk .vtkPolyData (points = coords )
260+ cells = vtk .vtkCellArray ()
261+ for _conn in conn :
262+ line = vtk .vtkPolyLine ()
263+ line .GetPointIds ().SetNumberOfIds (3 )
264+ line .GetPointIds ().SetId (0 , _conn [0 ])
265+ line .GetPointIds ().SetId (1 , _conn [1 ])
266+ line .GetPointIds ().SetId (2 , _conn [2 ])
267+ cells .InsertNextCell (line )
268+ ug .SetLines (cells )
269+
270+ ug .GetCellData ().AddArray (dsa .numpyTovtkDataArray (np .array (elem_id ), name = 'elem_id' ))
271+ ug .GetCellData ().AddArray (dsa .numpyTovtkDataArray (coords_a_cell , name = "coords_a_elem" ))
272+ ug .GetPointData ().AddArray (
273+ dsa .numpyTovtkDataArray (node_id , name = "node_id" )
274+ )
275+ ug .GetPointData ().AddArray (dsa .numpyTovtkDataArray (local_x , name = "local_x" ))
276+ ug .GetPointData ().AddArray (dsa .numpyTovtkDataArray (local_y , name = "local_y" ))
277+ ug .GetPointData ().AddArray (dsa .numpyTovtkDataArray (local_z , name = "local_z" ))
278+ ug .GetPointData ().AddArray (dsa .numpyTovtkDataArray (coords_a , name = "coords_a" ))
279+
268280 if with_postproc_cell :
269281 for k in postproc_cell_vector :
270- ug .cell_data . add_array ( tstep . postproc_cell [ k ])
271- ug . cell_data . get_array ( counter ). name = k + ' _cell'
272- counter += 1
282+ ug .GetCellData (). AddArray (
283+ dsa . numpyTovtkDataArray ( tstep . postproc_cell [ k ], name = f" { k } _cell" )
284+ )
273285 for k in postproc_cell_6vector :
274- for i in range (0 , 2 ):
275- ug .cell_data .add_array (tstep .postproc_cell [k ][:, 3 * i :3 * (i + 1 )])
276- ug .cell_data .get_array (counter ).name = k + '_' + str (i ) + '_cell'
277- counter += 1
278- ug .cell_data .add_array (coords_a_cell )
279- ug .cell_data .get_array (counter ).name = 'coords_a_elem'
280- counter += 1
281-
282- ug .point_data .scalars = node_id
283- ug .point_data .scalars .name = 'node_id'
284- point_vector_counter = 1
285- ug .point_data .add_array (local_x , 'vector' )
286- ug .point_data .get_array (point_vector_counter ).name = 'local_x'
287- point_vector_counter += 1
288- ug .point_data .add_array (local_y , 'vector' )
289- ug .point_data .get_array (point_vector_counter ).name = 'local_y'
290- point_vector_counter += 1
291- ug .point_data .add_array (local_z , 'vector' )
292- ug .point_data .get_array (point_vector_counter ).name = 'local_z'
293- point_vector_counter += 1
294- ug .point_data .add_array (coords_a , 'vector' )
295- ug .point_data .get_array (point_vector_counter ).name = 'coords_a'
286+ for i in range (2 ):
287+ ug .GetCellData ().AddArray (
288+ dsa .numpyTovtkDataArray (
289+ tstep .postproc_cell [k ][:, 3 * i :3 * (i + 1 )], name = f"{ k } _{ i } _cell"
290+ )
291+ )
292+
296293 if self .settings ['include_applied_forces' ]:
297- point_vector_counter += 1
298- ug .point_data .add_array (app_forces , 'vector' )
299- ug .point_data .get_array (point_vector_counter ).name = 'app_forces'
300- point_vector_counter += 1
301- ug .point_data .add_array (forces_constraints_nodes , 'vector' )
302- ug .point_data .get_array (point_vector_counter ).name = 'forces_constraints_nodes'
294+ ug .GetPointData ().AddArray (dsa .numpyTovtkDataArray (app_forces , name = "app_forces" ))
295+ ug .GetPointData ().AddArray (
296+ dsa .numpyTovtkDataArray (forces_constraints_nodes , name = "forces_constraints_node" )
297+ )
303298 if with_gravity :
304- point_vector_counter += 1
305- ug .point_data .add_array (gravity_forces_g [:, 0 :3 ], 'vector' )
306- ug .point_data .get_array (point_vector_counter ).name = 'gravity_forces'
299+ ug .GetPointData ().AddArray (
300+ dsa .numpyTovtkDataArray (
301+ gravity_forces_g [:, :3 ], name = "gravity_forces"
302+ )
303+ )
307304
308305 if self .settings ['include_applied_moments' ]:
309- point_vector_counter += 1
310- ug . point_data . add_array (app_moment , 'vector' )
311- ug . point_data . get_array ( point_vector_counter ). name = 'app_moments'
312- point_vector_counter += 1
313- ug . point_data . add_array (moments_constraints_nodes , 'vector' )
314- ug . point_data . get_array ( point_vector_counter ). name = 'moments_constraints_nodes'
306+ ug . GetPointData (). AddArray (
307+ dsa . numpyTovtkDataArray (app_moment , name = "app_moments" )
308+ )
309+ ug . GetPointData (). AddArray (
310+ dsa . numpyTovtkDataArray (moments_constraints_nodes , name = "moments_constraints_nodes" )
311+ )
315312 if with_gravity :
316- point_vector_counter += 1
317- ug .point_data .add_array (gravity_forces_g [:, 3 :6 ], 'vector' )
318- ug .point_data .get_array (point_vector_counter ).name = 'gravity_moments'
313+ ug .GetPointData ().AddArray (dsa .numpyTovtkDataArray (gravity_forces_g [:, 3 :6 ], name = "gravity_moments" ))
314+
319315 if with_postproc_node :
320316 for k in postproc_node_vector :
321- point_vector_counter += 1
322- ug .point_data .add_array (tstep .postproc_node [k ])
323- ug .point_data .get_array (point_vector_counter ).name = k + '_point'
317+ ug .GetPointData ().AddArray (
318+ dsa .numpyTovtkDataArray (
319+ tstep .postproc_node [k ], name = f"{ k } _point"
320+ )
321+ )
324322 for k in postproc_node_6vector :
325- for i in range (0 , 2 ):
326- point_vector_counter += 1
327- ug .point_data .add_array (tstep .postproc_node [k ][:, 3 * i :3 * (i + 1 )])
328- ug .point_data .get_array (point_vector_counter ).name = k + '_' + str (i ) + '_point'
323+ for i in range (2 ):
324+ ug .GetPointData ().AddArray (
325+ dsa .numpyTovtkDataArray (
326+ tstep .postproc_node [k ][:, 3 * i :3 * (i + 1 )], name = f"{ k } _{ i } _point"
327+ )
328+ )
329329
330330 for k in postproc_node_scalar :
331- point_vector_counter += 1
332- ug .point_data .add_array (tstep .postproc_node [k ])
333- ug .point_data .get_array (point_vector_counter ).name = k
334-
335- write_data (ug , it_filename )
331+ ug .GetPointData ().AddArray (
332+ dsa .numpyTovtkDataArray (
333+ tstep .postproc_node [k ],
334+ name = str (k ),
335+ )
336+ )
337+
338+ writer = vtk .vtkXMLPolyDataWriter ()
339+ writer .SetFileName (it_filename )
340+ writer .SetInputData (ug )
341+ writer .Write ()
336342
337343 def write_for (self , it ):
338- it_filename = (self .filename_for +
339- '%06u' % it )
344+ it_filename = f"{ self .filename_for } { it :06d} .vtp"
340345
341- forces_constraints_FoR = np .zeros ((self .data .structure .num_bodies , 3 ))
342- moments_constraints_FoR = np .zeros ((self .data .structure .num_bodies , 3 ))
343- # TODO: what should I do with the forces of the quaternion?
346+ forces_constraints_for = np .zeros ((self .data .structure .num_bodies , 3 ))
347+ moments_constraints_for = np .zeros ((self .data .structure .num_bodies , 3 ))
344348
345349 # aero2inertial rotation
346350 aero2inertial = self .data .structure .timestep_info [it ].cga ()
347351
348352 # coordinates of corners
349- FoR_coords = np .zeros ((self .data .structure .num_bodies , 3 ))
353+ for_coords = np .zeros ((self .data .structure .num_bodies , 3 ))
350354 if self .settings ['include_rbm' ]:
351355 offset = np .zeros ((3 ,))
352356 else :
353357 offset = self .data .structure .timestep_info [it ].mb_FoR_pos [0 , 0 :3 ]
354- for ibody in range (self .data .structure .num_bodies ):
355- FoR_coords [ibody , :] = self .data .structure .timestep_info [it ].mb_FoR_pos [ibody , 0 :3 ] - offset
356358
357359 for ibody in range (self .data .structure .num_bodies ):
358- forces_constraints_FoR [ibody , :] = np .dot (aero2inertial ,
359- self .data .structure .timestep_info [it ].forces_constraints_FoR [ibody , 0 :3 ])
360- moments_constraints_FoR [ibody , :] = np .dot (aero2inertial ,
361- self .data .structure .timestep_info [it ].forces_constraints_FoR [ibody , 3 :6 ])
362-
363- FoRmesh = tvtk .PolyData ()
364- FoRmesh .points = FoR_coords
365- for_vector_counter = - 1
366- for_vector_counter += 1
367- FoRmesh .point_data .add_array (forces_constraints_FoR , 'vector' )
368- FoRmesh .point_data .get_array (for_vector_counter ).name = 'forces_constraints_FoR'
369-
370- for_vector_counter += 1
371- FoRmesh .point_data .add_array (moments_constraints_FoR , 'vector' )
372- FoRmesh .point_data .get_array (for_vector_counter ).name = 'moments_constraints_FoR'
373-
374- write_data (FoRmesh , it_filename )
360+ for_coords [ibody , :] = self .data .structure .timestep_info [it ].mb_FoR_pos [ibody , 0 :3 ] - offset
361+ forces_constraints_for [ibody , :] = aero2inertial @ self .data .structure .timestep_info [it ].forces_constraints_FoR [ibody , :3 ]
362+ moments_constraints_for [ibody , :] = aero2inertial @ self .data .structure .timestep_info [it ].forces_constraints_FoR [ibody , 3 :6 ]
363+
364+ for_mesh = vtk .vtkPolyData (points = for_coords )
365+
366+ for_mesh .GetPointData ().AddArray (
367+ dsa .numpyTovtkDataArray (
368+ forces_constraints_for ,
369+ 'forces_constraints_FoR' ,
370+ )
371+ )
372+
373+ for_mesh .GetPointData ().AddArray (
374+ dsa .numpyTovtkDataArray (
375+ moments_constraints_for ,
376+ "moments_constraints_FoR" ,
377+ )
378+ )
379+
380+ writer = vtk .vtkXMLPolyDataWriter ()
381+ writer .SetFileName (it_filename )
382+ writer .SetInputData (for_mesh )
383+ writer .Write ()
0 commit comments