Chi-Tech
unpartmesh_01b_readfrompvtu.cc
Go to the documentation of this file.
2
4
5#include <vtkSmartPointer.h>
6#include <vtkUnstructuredGrid.h>
7#include <vtkXMLPUnstructuredGridReader.h>
8
9#include <vtkInformation.h>
10
11#include "chi_runtime.h"
12#include "chi_log.h"
13
14#define ErrorReadingFile(fname) \
15std::runtime_error("Failed to open file: " + options.file_name + \
16" in call to " + #fname + ".")
17
18//###################################################################
19/**Reads a VTK unstructured mesh. This reader will use the following
20 * options:
21 * - `file_name`, of course.
22 * - `material_id_fieldname`, cell data for material_id.*/
25{
26 Chi::log.Log() << "Reading PVTU file: " << options.file_name << ".";
27
28 //======================================== Attempt to open file
29 std::ifstream file;
30 file.open(options.file_name);
31 if (!file.is_open()) throw ErrorReadingFile(ReadFromVTU);
32 file.close();
33
34 //======================================== Read the file
35 mesh_options_ = options;
37 reader->SetFileName(options.file_name.c_str());
38
39 if (not reader->CanReadFile(options.file_name.c_str()))
40 throw std::logic_error("Unable to read file-type with this routine");
41 reader->UpdateInformation();
42 reader->Update();
43
44 //======================================== Get all the grid blocks
45 // For vtu files this is very simple. The
46 // output of the reader is an UnstructuredGrid.
47 auto ugrid_main = vtkUGridPtr(reader->GetOutput());
48 std::vector<vtkUGridPtrAndName> grid_blocks = {{ugrid_main,""}};
49
50 //======================================== Get the main + bndry blocks
51 const int max_dimension = chi_mesh::FindHighestDimension(grid_blocks);
52 Chi::log.Log0Verbose1() << "Maximum dimension : " << max_dimension << "\n";
53 std::vector<vtkUGridPtrAndName> domain_grid_blocks =
54 chi_mesh::GetBlocksOfDesiredDimension(grid_blocks, max_dimension);
55 std::vector<vtkUGridPtrAndName> bndry_grid_blocks =
56 chi_mesh::GetBlocksOfDesiredDimension(grid_blocks, max_dimension-1);
57
58 //======================================== Process blocks
59 auto ugrid = chi_mesh::ConsolidateGridBlocks(domain_grid_blocks);
60
61 //======================================== Copy Data
62 CopyUGridCellsAndPoints(*ugrid, options.scale, max_dimension);
63
64 //======================================== Set material ids
65 const auto material_ids = chi_mesh::BuildCellMaterialIDsFromField(
66 ugrid, options.material_id_fieldname, options.file_name);
67 SetMaterialIDsFromList(material_ids);
68
69 //======================================== Always do this
71 switch (max_dimension)
72 {
73 case 1: dimension = DIMENSION_1; break;
74 case 2: dimension = DIMENSION_2; break;
75 case 3: dimension = DIMENSION_3; break;
76 default: break;
77 }
78
79 attributes_ = dimension | UNSTRUCTURED;
80
83
84 Chi::log.Log() << "Done reading PVTU file: " << options.file_name << ".";
85}
86
static chi::ChiLog & log
Definition: chi_runtime.h:81
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
LogStream Log0Verbose1()
Definition: chi_log.h:234
vtkSmartPointer< vtkUnstructuredGrid > vtkUGridPtr
void ReadFromVTU(const Options &options)
void CopyUGridCellsAndPoints(vtkUnstructuredGrid &ugrid, double scale, int dimension_to_copy)
void ReadFromPVTU(const Options &options)
void SetMaterialIDsFromList(const std::vector< int > &material_ids)
std::vector< int > BuildCellMaterialIDsFromField(vtkUGridPtr &ugrid, const std::string &field_name, const std::string &file_name)
vtkUGridPtr ConsolidateGridBlocks(std::vector< vtkUGridPtrAndName > &ugrid_blocks, const std::string &block_id_array_name="BlockID")
MeshAttributes
Definition: chi_mesh.h:70
@ UNSTRUCTURED
Definition: chi_mesh.h:77
@ DIMENSION_1
Definition: chi_mesh.h:72
@ DIMENSION_2
Definition: chi_mesh.h:73
@ NONE
Definition: chi_mesh.h:71
@ DIMENSION_3
Definition: chi_mesh.h:74
int FindHighestDimension(std::vector< vtkUGridPtrAndName > &ugrid_blocks)
std::vector< vtkUGridPtrAndName > GetBlocksOfDesiredDimension(std::vector< vtkUGridPtrAndName > &ugrid_blocks, int desired_dimension)
#define ErrorReadingFile(fname)