@@ -194,4 +194,174 @@ function movie_paraview(; name="Movie", pvd=nothing, Finalize::Bool=false, Initi
194
194
end
195
195
196
196
197
-
197
+ """
198
+ write_paraview(DataSet::Q1Data, filename="test"; directory=nothing, pvd=nothing, time=nothing, verbose=true)
199
+ Writes a `Q1Data` dataset to disk, which has cell and vertex field
200
+ """
201
+ function write_paraview (DataSet:: Q1Data , filename= " test" ; directory= nothing , pvd= nothing , time= nothing , verbose= true )
202
+
203
+ # Error checking
204
+ if ! (length (size (DataSet. x))== length (size (DataSet. y))== length (size (DataSet. z)))
205
+ error (" The X/Y/Z should be 3 dimensional" )
206
+ end
207
+
208
+ # Create directory if required
209
+ if ! isnothing (directory)
210
+ mkpath (directory)
211
+ filename = joinpath (directory, filename); # add directory name to pathname
212
+ end
213
+
214
+ # Create VT* file
215
+ vtkfile = vtk_grid (filename, ustrip .(DataSet. x. val), ustrip .(DataSet. y. val), ustrip .(DataSet. z. val))
216
+
217
+ # Add vertex data fields to VT* file
218
+ names = String .(collect (keys (DataSet. fields))); # this is how to retrieve the names of the data fields
219
+ for (index, name) in enumerate (names)
220
+
221
+ if typeof (DataSet. fields[index])<: Tuple
222
+ # if we do a tuple of velocities, it appears difficult to deal with units
223
+ # This will require some more work
224
+ unit_name = " "
225
+ Data = DataSet. fields[index]
226
+ if unit (Data[1 ][1 ])!= NoUnits
227
+ error (" potential error as vector data fields have units; please save them with no units!" )
228
+ end
229
+ else
230
+ unit_name = unit (DataSet. fields[index][1 ])
231
+ Data = ustrip .(DataSet. fields[index])
232
+ end
233
+
234
+ name_with_units = join ([name," [$(unit_name) ]" ]); # add units to the name of the field
235
+ # if PointsData
236
+ # vtkfile[name_with_units, VTKPointData()] = Data[:];
237
+ # else
238
+ vtkfile[name_with_units] = Data;
239
+ # end
240
+ end
241
+
242
+ # Add cell data fields to VT* file
243
+ names = String .(collect (keys (DataSet. cellfields))); # this is how to retrieve the names of the data fields
244
+ for (index, name) in enumerate (names)
245
+
246
+ if typeof (DataSet. cellfields[index])<: Tuple
247
+ # if we do a tuple of velocities, it appears difficult to deal with units
248
+ # This will require some more work
249
+ unit_name = " "
250
+ Data = DataSet. cellfields[index]
251
+ if unit (Data[1 ][1 ])!= NoUnits
252
+ error (" potential error as vector data fields have units; please save them with no units!" )
253
+ end
254
+ else
255
+ unit_name = unit (DataSet. cellfields[index][1 ])
256
+ Data = ustrip .(DataSet. cellfields[index])
257
+ end
258
+
259
+ name_with_units = join ([name," [$(unit_name) ]" ]); # add units to the name of the field
260
+ vtkfile[name_with_units, VTKCellData ()] = Data[:];
261
+
262
+ end
263
+
264
+
265
+ outfiles = vtk_save (vtkfile);
266
+ if verbose
267
+ println (" Saved file: $(outfiles[1 ]) " )
268
+ end
269
+ if ! isnothing (pvd)
270
+ # Write movie
271
+ pvd[time] = vtkfile
272
+ end
273
+
274
+ return pvd
275
+ end
276
+
277
+
278
+
279
+
280
+ """
281
+ write_paraview(DataSet::FEData, filename="test"; directory=nothing, pvd=nothing, time=nothing, verbose=true)
282
+ Writes a `FEData` dataset (general finite element) to disk, which has cell and vertex field
283
+ """
284
+ function write_paraview (DataSet:: FEData , filename= " test" ; directory= nothing , pvd= nothing , time= nothing , verbose= true )
285
+
286
+ # Create directory if required
287
+ if ! isnothing (directory)
288
+ mkpath (directory)
289
+ filename = joinpath (directory, filename); # add directory name to pathname
290
+ end
291
+
292
+ connectivity = DataSet. connectivity
293
+ if size (DataSet. connectivity,1 ) == 4
294
+ celltype = VTKCellTypes. VTK_TETRA
295
+
296
+ elseif size (DataSet. connectivity,1 ) == 8
297
+ celltype = VTKCellTypes. VTK_HEXAHEDRON
298
+
299
+ # we need to reorder this as pTatin uses a different ordering than VTK
300
+ id_reorder = [1 ,2 ,4 ,3 ,5 ,6 ,8 ,7 ]
301
+ connectivity = connectivity[id_reorder,:]
302
+ else
303
+ error (" This element is not yet implemented" )
304
+ end
305
+
306
+ # Create VTU file
307
+ points = DataSet. vertices
308
+ cells = MeshCell[];
309
+ for i = 1 : size (connectivity,2 )
310
+ push! (cells, MeshCell (celltype, connectivity[:,i]))
311
+ end
312
+
313
+ vtkfile = vtk_grid (filename, points, cells)
314
+
315
+ # Add vertex data fields to VT* file
316
+ names = String .(collect (keys (DataSet. fields))); # this is how to retrieve the names of the data fields
317
+ for (index, name) in enumerate (names)
318
+
319
+ if typeof (DataSet. fields[index])<: Tuple
320
+ # if we do a tuple of velocities, it appears difficult to deal with units
321
+ # This will require some more work
322
+ unit_name = " "
323
+ Data = DataSet. fields[index]
324
+ if unit (Data[1 ][1 ])!= NoUnits
325
+ error (" potential error as vector data fields have units; please save them with no units!" )
326
+ end
327
+ else
328
+ unit_name = unit (DataSet. fields[index][1 ])
329
+ Data = ustrip .(DataSet. fields[index])
330
+ end
331
+
332
+ name_with_units = join ([name," [$(unit_name) ]" ]); # add units to the name of the field
333
+ vtkfile[name_with_units] = Data;
334
+ end
335
+
336
+ # Add cell data fields to VT* file
337
+ names = String .(collect (keys (DataSet. cellfields))); # this is how to retrieve the names of the data fields
338
+ for (index, name) in enumerate (names)
339
+
340
+ if typeof (DataSet. cellfields[index])<: Tuple
341
+ # if we do a tuple of velocities, it appears difficult to deal with units
342
+ # This will require some more work
343
+ unit_name = " "
344
+ Data = DataSet. cellfields[index]
345
+ if unit (Data[1 ][1 ])!= NoUnits
346
+ error (" potential error as vector data fields have units; please save them with no units!" )
347
+ end
348
+ else
349
+ unit_name = unit (DataSet. cellfields[index][1 ])
350
+ Data = ustrip .(DataSet. cellfields[index])
351
+ end
352
+
353
+ name_with_units = join ([name," [$(unit_name) ]" ]); # add units to the name of the field
354
+ vtkfile[name_with_units, VTKCellData ()] = Data[:];
355
+ end
356
+
357
+ outfiles = vtk_save (vtkfile);
358
+ if verbose
359
+ println (" Saved file: $(outfiles[1 ]) " )
360
+ end
361
+ if ! isnothing (pvd)
362
+ # Write movie
363
+ pvd[time] = vtkfile
364
+ end
365
+
366
+ return pvd
367
+ end
0 commit comments