Chi-Tech
unpartmesh_01a_readfromvtu.cc
Go to the documentation of this file.
2
4
5#include <vtkSmartPointer.h>
6#include <vtkUnstructuredGrid.h>
7#include <vtkXMLUnstructuredGridReader.h>
8
9#include <vtkInformation.h>
10
11#include "chi_runtime.h"
12#include "chi_log.h"
13
14#define ErrorReadingFile(fname) \
15 std::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 VTU 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
60 domain_grid_blocks, mesh_options_.material_id_fieldname);
61
62 //======================================== Copy Data
63 CopyUGridCellsAndPoints(*ugrid, options.scale, max_dimension);
64
65 //======================================== Set material ids
66 const auto material_ids = chi_mesh::BuildCellMaterialIDsFromField(
67 ugrid, options.material_id_fieldname, options.file_name);
68 SetMaterialIDsFromList(material_ids);
69
70 //======================================== Always do this
72 switch (max_dimension)
73 {
74 case 1:
75 dimension = DIMENSION_1;
76 break;
77 case 2:
78 dimension = DIMENSION_2;
79 break;
80 case 3:
81 dimension = DIMENSION_3;
82 break;
83 default:
84 break;
85 }
86
87 attributes_ = dimension | UNSTRUCTURED;
88
91
92 Chi::log.Log() << "Done reading VTU file: " << options.file_name << ".";
93}
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 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)