6#include <deal.II/base/exceptions.h>
7#include <deal.II/base/point.h>
8#include <deal.II/lac/vector.h>
12#include <vtkCellLocator.h>
13#include <vtkDataArray.h>
14#include <vtkGenericCell.h>
16#include <vtkPointData.h>
17#include <vtkUnstructuredGrid.h>
18#include <vtkUnstructuredGridReader.h>
22template <
unsigned int dim,
typename number>
41 [[nodiscard]] dealii::types::global_dof_index
47 [[nodiscard]] dealii::types::global_dof_index
59 std::vector<std::string>
65 std::vector<std::string>
73 const std::string &scalar_name)
override;
78 dealii::Vector<number>
80 const std::string &vector_name)
override;
86 vtkNew<vtkUnstructuredGridReader>
reader;
119template <
unsigned int dim,
typename number>
123 :
ReadFieldBase<dim, number>(_ic_file, _spatial_discretization)
127 reader = vtkNew<vtkUnstructuredGridReader>();
132 AssertThrow(
reader->IsFileUnstructuredGrid(),
133 dealii::ExcMessage(
"The vtk file must be an unstructured grid"));
136 auto *output =
reader->GetOutput();
138 output->IsHomogeneous(),
140 "The vtk file must have homogeneous cells of type VTK_HEXAHEDRON or VTK_QUAD"));
143 if constexpr (dim == 3)
145 AssertThrow(output->GetCellType(0) == VTK_HEXAHEDRON,
147 "For 3D meshes, the cells must be of type VTK_HEXAHEDRON "));
149 else if constexpr (dim == 2)
151 AssertThrow(output->GetCellType(0) == VTK_QUAD,
153 "For 2D meshes, the cells must be of type VTK_QUAD"));
158 dealii::ExcMessage(
"File read-in is not supported for 1D meshes"));
163 const vtkIdType n_points_vtk = output->GetNumberOfPoints();
164 const vtkIdType n_cells_vtk = output->GetNumberOfCells();
167 AssertThrow(n_points_vtk < std::numeric_limits<dealii::types::global_dof_index>::max(),
169 "The number of points being read-in from the vtk file is too large. Try "
170 "recompiling deal.II with 64-bit indices."));
171 AssertThrow(n_cells_vtk < std::numeric_limits<dealii::types::global_dof_index>::max(),
173 "The number of cells being read-in from the vtk file is too large. Try "
174 "recompiling deal.II with 64-bit indices."));
185template <
unsigned int dim,
typename number>
186inline vtkUnstructuredGrid *
189 return reader->GetOutput();
192template <
unsigned int dim,
typename number>
193inline dealii::types::global_dof_index
199template <
unsigned int dim,
typename number>
200inline dealii::types::global_dof_index
206template <
unsigned int dim,
typename number>
211 reader->GetOutput()->PrintSelf(std::cout, vtkIndent());
214template <
unsigned int dim,
typename number>
215inline std::vector<std::string>
218 std::vector<std::string> scalars_names(
n_scalars);
219 for (
unsigned int i = 0; i <
n_scalars; ++i)
221 scalars_names[i] =
reader->GetScalarsNameInFile(
static_cast<int>(i));
223 return scalars_names;
226template <
unsigned int dim,
typename number>
227inline std::vector<std::string>
230 std::vector<std::string> vectors_names(
n_vectors);
231 for (
unsigned int i = 0; i <
n_vectors; ++i)
233 vectors_names[i] =
reader->GetVectorsNameInFile(
static_cast<int>(i));
235 return vectors_names;
238template <
unsigned int dim,
typename number>
241 const std::string &scalar_name)
245 AssertThrow(std::find(scalars_names.begin(), scalars_names.end(), scalar_name) !=
248 "The provided vtk dataset does not contain a field named " +
255 reader->SetScalarsName(scalar_name.c_str());
259 vtkUnstructuredGrid *output =
reader->GetOutput();
260 vtkPointData *point_data = output->GetPointData();
263 const vtkIdType point_id = output->FindPoint(point_vector.data());
266 AssertThrow(point_id >= 0, dealii::ExcMessage(
"No matching point found in VTK grid"));
271 output->GetPoint(point_id, point_in_dataset.data());
272 bool interpolate =
false;
273 for (
unsigned int i = 0; i < dim; i++)
284 vtkDataArray *data_array = point_data->GetArray(scalar_name.c_str());
285 AssertThrow(data_array !=
nullptr,
286 dealii::ExcMessage(std::string(
"Data array not found: ") + scalar_name));
291 vtkNew<vtkCellLocator> cell_locator;
292 cell_locator->SetDataSet(output);
293 cell_locator->BuildLocator();
300 vtkGenericCell *cell = vtkGenericCell::New();
302 const vtkIdType cell_id = cell_locator->FindCell(point_vector.data(),
310 AssertThrow(cell_id >= 0,
311 dealii::ExcMessage(
"Point not inside any cell for interpolation"));
314 vtkIdList *point_ids = output->GetCell(cell_id)->GetPointIds();
315 number interpolated_value = 0.0;
316 for (vtkIdType
id = 0;
id < point_ids->GetNumberOfIds(); ++id)
318 const vtkIdType pt_id = point_ids->GetId(
id);
320 interpolated_value += weights[id] * data_array->GetComponent(pt_id, 0);
324 return interpolated_value;
328 return data_array->GetComponent(point_id, 0);
331template <
unsigned int dim,
typename number>
332inline dealii::Vector<number>
334 const std::string &vector_name)
338 AssertThrow(std::find(vectors_names.begin(), vectors_names.end(), vector_name) !=
341 "The provided vtk dataset does not contain a field named " +
348 reader->SetVectorsName(vector_name.c_str());
352 vtkUnstructuredGrid *output =
reader->GetOutput();
353 vtkPointData *point_data = output->GetPointData();
356 vtkIdType point_id = output->FindPoint(point_vector.data());
359 AssertThrow(point_id >= 0, dealii::ExcMessage(
"No matching point found in VTK grid"));
364 output->GetPoint(point_id, point_in_dataset.data());
365 bool interpolate =
false;
366 for (
unsigned int i = 0; i < dim; i++)
377 vtkDataArray *data_array = point_data->GetArray(vector_name.c_str());
378 AssertThrow(data_array !=
nullptr,
379 dealii::ExcMessage(std::string(
"Data array not found: ") + vector_name));
382 dealii::Vector<number> vector_value(dim);
384 for (
unsigned int i = 0; i < dim; i++)
388 vtkNew<vtkCellLocator> cell_locator;
389 cell_locator->SetDataSet(output);
390 cell_locator->BuildLocator();
397 vtkGenericCell *cell = vtkGenericCell::New();
399 const vtkIdType cell_id = cell_locator->FindCell(point_vector.data(),
407 AssertThrow(cell_id >= 0,
408 dealii::ExcMessage(
"Point not inside any cell for interpolation"));
411 vtkIdList *point_ids = output->GetCell(cell_id)->GetPointIds();
412 number interpolated_value = 0.0;
413 for (vtkIdType
id = 0;
id < point_ids->GetNumberOfIds(); ++id)
415 Assert(id < data_array->GetNumberOfComponents(),
416 dealii::ExcMessage(
"Index out of bounds for data array components"));
417 const vtkIdType pt_id = point_ids->GetId(
id);
419 interpolated_value +=
420 weights[id] * data_array->GetComponent(pt_id,
static_cast<int>(
id));
424 vector_value[i] = interpolated_value;
428 vector_value[i] = data_array->GetComponent(point_id, i);
434PRISMS_PF_END_NAMESPACE
const InitialConditionFile & ic_file
Initial condition file object.
Definition read_field_base.h:86
ReadFieldBase(const InitialConditionFile &_ic_file, const SpatialDiscretization< dim > &_spatial_discretization)
Constructor.
Definition read_field_base.h:90
dealii::types::global_dof_index get_n_points() const
Get the number of points.
Definition read_vtk.h:194
unsigned int n_scalars
Number of scalars in file.
Definition read_vtk.h:101
number get_scalar_value(const dealii::Point< dim > &point, const std::string &scalar_name) override
Get scalar value for a given point.
Definition read_vtk.h:240
std::vector< std::string > get_scalars_names()
Get the names of the scalars in the vtk file.
Definition read_vtk.h:216
std::vector< std::string > get_vectors_names()
Get the names of the vectors in the vtk file.
Definition read_vtk.h:228
ReadUnstructuredVTK(const InitialConditionFile &_ic_file, const SpatialDiscretization< dim > &_spatial_discretization)
Constructor.
Definition read_vtk.h:120
dealii::types::global_dof_index n_points
Number of points.
Definition read_vtk.h:91
vtkUnstructuredGrid * get_output()
Get the vtk output.
Definition read_vtk.h:187
dealii::types::global_dof_index get_n_cells() const
Get the number of cells.
Definition read_vtk.h:201
dealii::types::global_dof_index n_cells
Number of cells.
Definition read_vtk.h:96
void print_file() override
Print the vtk file for debugging.
Definition read_vtk.h:208
vtkNew< vtkUnstructuredGridReader > reader
Reader for the vtk file.
Definition read_vtk.h:86
const unsigned int n_points_per_hex_cell
Number of points in a hex cell.
Definition read_vtk.h:111
const unsigned int n_space_coordinates
Number of space coordinates in a point.
Definition read_vtk.h:116
dealii::Vector< number > get_vector_value(const dealii::Point< dim > &point, const std::string &vector_name) override
Get vector value for a given point.
Definition read_vtk.h:333
unsigned int n_vectors
Number of vectors in file.
Definition read_vtk.h:106
static const double mesh_tolerance
Default mesh tolerance.
Definition types.h:62
Definition conditional_ostreams.cc:20
@ ReadUnstructuredVTK
Definition read_field_factory.h:31
Struct that store the read-in information for a single file.
Definition load_initial_condition_parameters.h:25
Struct that holds spatial discretization parameters.
Definition spatial_discretization.h:230
DEAL_II_ALWAYS_INLINE std::vector< number > dealii_point_to_vector(const dealii::Point< dim, number > &point)
Definition utilities.h:317