deal.II version GIT relicensing-6842-g793a97d2aa 2026-10-02 14:00:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
utilities.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2023 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_vtk_utilities_h
14#define dealii_vtk_utilities_h
15
16#include <deal.II/base/config.h>
17
19#include <deal.II/base/point.h>
20
22
23#include <deal.II/fe/fe.h>
24
26#include <deal.II/grid/tria.h>
27
28#include <deal.II/lac/vector.h>
29
30#ifdef DEAL_II_WITH_VTK
31
32# include <vtkDataObject.h>
33# include <vtkDoubleArray.h>
34# include <vtkSmartPointer.h>
35# include <vtkUnstructuredGrid.h>
36
37# include <string>
38
39#endif
40
42
43#ifdef DEAL_II_WITH_VTK
57namespace VTKWrappers
58{
59 namespace internal
60 {
75 vtkSmartPointer<vtkUnstructuredGrid>
76 load_vtk_file(const std::string &vtk_filename,
77 const bool cleanup = true,
78 const double relative_tolerance = 0.0);
79
102 vtkSmartPointer<vtkUnstructuredGrid>
104 const vtkSmartPointer<vtkDataObject> &data_object);
105
128 void
129 write_vtk(const std::string &vtk_filename,
130 const vtkSmartPointer<vtkDataObject> &data_object);
131 } // namespace internal
132
140 template <int dim>
141 inline vtkSmartPointer<vtkDoubleArray>
142 dealii_point_to_vtk_array(const ::Point<dim> &p);
143
169 template <int dim, int spacedim>
170 void
172 const vtkUnstructuredGrid &unstructured_grid,
174 const std::string &material_id_field = "",
175 const std::string &boundary_id_field = "",
176 const std::string &manifold_id_field = "");
177
201 template <int dim, int spacedim>
202 vtkSmartPointer<vtkUnstructuredGrid>
205 const std::string &material_id_field = "",
206 const std::string &boundary_id_field = "",
207 const std::string &manifold_id_field = "");
208
229 template <int dim, int spacedim>
230 void
231 write_vtk(const std::string &vtk_filename,
233 const std::string &material_id_field = "",
234 const std::string &boundary_id_field = "",
235 const std::string &manifold_id_field = "");
236
263 template <int dim, int spacedim>
264 void
265 read_tria(const std::string &vtk_filename,
267 const bool cleanup = true,
268 const double relative_tolerance = 0.0,
269 const std::string &material_id_field = "",
270 const std::string &boundary_id_field = "",
271 const std::string &manifold_id_field = "");
272
290 void
291 read_cell_data(const std::string &vtk_filename,
292 const std::string &cell_data_name,
293 Vector<double> &output_vector,
294 const bool cleanup = true,
295 const double relative_tolerance = 0.0);
296
314 void
315 read_vertex_data(const std::string &vtk_filename,
316 const std::string &vertex_data_name,
317 Vector<double> &output_vector,
318 const bool cleanup = true,
319 const double relative_tolerance = 0.0);
320
346 void
347 read_all_data(const std::string &vtk_filename,
348 Vector<double> &output_vector,
349 const bool cleanup = true,
350 const double relative_tolerance = 0.0);
351
378 template <int dim, int spacedim>
379 std::pair<std::unique_ptr<FiniteElement<dim, spacedim>>,
380 std::vector<std::string>>
381 vtk_to_finite_element(const std::string &vtk_filename);
382
439 template <int dim, int spacedim, typename VectorType>
440 void
442 const Vector<double> &data,
444 VectorType &output_vector);
445
476 template <int dim, int spacedim>
477 void
478 read_vtk(const std::string &vtk_filename,
479 DoFHandler<dim, spacedim> &dof_handler,
480 Vector<double> &output_vector,
481 std::vector<std::string> &data_names,
482 const bool cleanup = true,
483 const double relative_tolerance = 0.0,
484 const std::string &material_id_field = "",
485 const std::string &boundary_id_field = "",
486 const std::string &manifold_id_field = "");
487
488# ifndef DOXYGEN
489 // Template implementations
490
491 template <int dim>
492 inline vtkSmartPointer<vtkDoubleArray>
493 dealii_point_to_vtk_array(const ::Point<dim> &p)
494 {
495 vtkSmartPointer<vtkDoubleArray> p_vtk =
496 vtkSmartPointer<vtkDoubleArray>::New();
497
498 p_vtk->SetNumberOfComponents(dim);
499 p_vtk->SetNumberOfTuples(1);
500
501 for (int d = 0; d < dim; ++d)
502 p_vtk->FillComponent(d, p[d]);
503
504 return p_vtk;
505 }
506
507
508
509 template <int dim, int spacedim, typename VectorType>
510 void
512 const Vector<double> &data,
514 VectorType &output_vector)
515 {
516 AssertDimension(dh.n_dofs(), output_vector.size());
517 const auto &fe = dh.get_fe();
518
519 const auto dist_to_serial_vertices =
521 dh.get_triangulation());
522
523 const auto &locally_owned_dofs = dh.locally_owned_dofs();
524
525 types::global_dof_index dofs_offset = 0;
526 unsigned int vertex_comp_offset = 0;
527 unsigned int cell_comp_offset = 0;
528 for (unsigned int field = 0; field < fe.n_blocks(); ++field)
529 {
530 const auto &field_fe = fe.base_element(field);
531 const unsigned int n_comps = field_fe.n_components();
532 if (field_fe.n_dofs_per_vertex() > 0)
533 {
534 // This is a vertex data field
535 const types::global_dof_index n_local_dofs =
536 n_comps * serial_tria.n_vertices();
537 for (const auto &cell : dh.active_cell_iterators())
538 if (cell->is_locally_owned())
539 for (const auto v : cell->vertex_indices())
540 {
541 const types::global_dof_index serial_vertex_index =
542 dist_to_serial_vertices[cell->vertex_index(v)];
543 if (serial_vertex_index != numbers::invalid_unsigned_int)
544 for (unsigned int c = 0; c < n_comps; ++c)
545 {
546 const types::global_dof_index dof_index =
547 cell->vertex_dof_index(v, vertex_comp_offset + c);
548 Assert(locally_owned_dofs.is_element(dof_index),
550 output_vector[dof_index] =
551 data[dofs_offset + n_comps * serial_vertex_index +
552 c];
553 }
554 }
555 dofs_offset += n_local_dofs;
556 vertex_comp_offset += n_comps;
557 }
558 else if (field_fe.template n_dofs_per_object<dim>() > 0)
559 {
560 // this is a cell data field
561 const types::global_dof_index n_local_dofs =
562 n_comps * serial_tria.n_global_active_cells();
563
564 // Assumption: serial and parallel meshes have the same ordering of
565 // cells.
566 auto serial_cell = serial_tria.begin_active();
567 auto parallel_cell = dh.begin_active();
568 for (; parallel_cell != dh.end(); ++parallel_cell)
569 if (parallel_cell->is_locally_owned())
570 {
571 // Advance serial cell until we reach the same cell index of
572 // the parallel cell
573 while (serial_cell->id() < parallel_cell->id())
574 ++serial_cell;
575 const auto serial_cell_index =
576 serial_cell->global_active_cell_index();
577 for (unsigned int c = 0; c < n_comps; ++c)
578 {
579 const types::global_dof_index dof_index =
580 parallel_cell->dof_index(cell_comp_offset + c);
581 Assert(locally_owned_dofs.is_element(dof_index),
583 output_vector[dof_index] =
584 data[dofs_offset + n_comps * serial_cell_index + c];
585 }
586 }
587 dofs_offset += n_local_dofs;
588 cell_comp_offset += n_comps;
589 }
590 }
591 }
592# endif
593
594} // namespace VTKWrappers
595
596#endif
597
599#endif
*  *  for(const auto &cell :triangulation.active_cell_iterators())
cell_iterator end() const
const FiniteElement< dim, spacedim > & get_fe(const types::fe_index index=0) const
const Triangulation< dim, spacedim > & get_triangulation() const
const IndexSet & locally_owned_dofs() const
active_cell_iterator begin_active(const unsigned int level=0) const
types::global_dof_index n_dofs() const
virtual types::global_cell_index n_global_active_cells() const
unsigned int n_vertices() const
active_cell_iterator begin_active(const unsigned int level=0) const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
unsigned int vertex_indices[2]
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcInternalError()
std::vector< index_type > data
Definition mpi.cc:734
std::vector< types::global_vertex_index > parallel_to_serial_vertex_indices(const Triangulation< dim, spacedim > &serial_tria, const Triangulation< dim, spacedim > &parallel_tria)
vtkSmartPointer< vtkUnstructuredGrid > convert_to_unstructured_grid(const vtkSmartPointer< vtkDataObject > &data_object)
Definition utilities.cc:101
void write_vtk(const std::string &vtk_filename, const vtkSmartPointer< vtkDataObject > &data_object)
Definition utilities.cc:213
vtkSmartPointer< vtkUnstructuredGrid > load_vtk_file(const std::string &vtk_filename, const bool cleanup=true, const double relative_tolerance=0.0)
Definition utilities.cc:139
void read_tria(const std::string &vtk_filename, Triangulation< dim, spacedim > &tria, const bool cleanup=true, const double relative_tolerance=0.0, const std::string &material_id_field="", const std::string &boundary_id_field="", const std::string &manifold_id_field="")
Definition utilities.cc:886
vtkSmartPointer< vtkDoubleArray > dealii_point_to_vtk_array(const ::Point< dim > &p)
void read_vtk(const std::string &vtk_filename, DoFHandler< dim, spacedim > &dof_handler, Vector< double > &output_vector, std::vector< std::string > &data_names, const bool cleanup=true, const double relative_tolerance=0.0, const std::string &material_id_field="", const std::string &boundary_id_field="", const std::string &manifold_id_field="")
void read_cell_data(const std::string &vtk_filename, const std::string &cell_data_name, Vector< double > &output_vector, const bool cleanup=true, const double relative_tolerance=0.0)
Definition utilities.cc:903
void read_vertex_data(const std::string &vtk_filename, const std::string &vertex_data_name, Vector< double > &output_vector, const bool cleanup=true, const double relative_tolerance=0.0)
Definition utilities.cc:927
vtkSmartPointer< vtkUnstructuredGrid > dealii_triangulation_to_unstructured_grid(const Triangulation< dim, spacedim > &tria, const std::string &material_id_field="", const std::string &boundary_id_field="", const std::string &manifold_id_field="")
Definition utilities.cc:685
void write_vtk(const std::string &vtk_filename, const Triangulation< dim, spacedim > &tria, const std::string &material_id_field="", const std::string &boundary_id_field="", const std::string &manifold_id_field="")
Definition utilities.cc:871
void unstructured_grid_to_dealii_triangulation(const vtkUnstructuredGrid &unstructured_grid, Triangulation< dim, spacedim > &tria, const std::string &material_id_field="", const std::string &boundary_id_field="", const std::string &manifold_id_field="")
Definition utilities.cc:256
void data_to_dealii_vector(const Triangulation< dim, spacedim > &serial_tria, const Vector< double > &data, const DoFHandler< dim, spacedim > &dh, VectorType &output_vector)
void read_all_data(const std::string &vtk_filename, Vector< double > &output_vector, const bool cleanup=true, const double relative_tolerance=0.0)
Definition utilities.cc:951
std::pair< std::unique_ptr< FiniteElement< dim, spacedim > >, std::vector< std::string > > vtk_to_finite_element(const std::string &vtk_filename)
constexpr unsigned int invalid_unsigned_int
Definition types.h:228