3#include <vtkUnstructuredGrid.h>
4#include <vtkAppendFilter.h>
6#include <vtkCellData.h>
7#include <vtkPointData.h>
17 std::vector<vtkUGridPtrAndName>& ugrid_blocks)
20 for (
auto& ugrid : ugrid_blocks)
22 const int64_t num_cells = ugrid.first->GetNumberOfCells();
23 for (int64_t c = 0; c < num_cells; ++c)
25 auto cell = ugrid.first->GetCell(c);
27 max_dim = std::max(max_dim, cell->GetCellDimension());
38 std::vector<vtkUGridPtrAndName>& ugrid_blocks,
39 const std::string& block_id_array_name )
41 const std::string fname =
"chi_mesh::ConsolidateGridBlocks";
45 bool has_global_ids =
true;
46 for (
auto& ugrid_name : ugrid_blocks)
48 auto& ugrid = ugrid_name.first;
49 const bool has_cell_gids = ugrid->GetCellData()->GetGlobalIds();
50 const bool has_pnts_gids = ugrid->GetPointData()->GetGlobalIds();
51 const bool has_block_ids =
52 ugrid->GetCellData()->GetArray(block_id_array_name.c_str());
54 if ((not has_cell_gids) or (not has_pnts_gids)) has_global_ids =
false;
56 if (not has_block_ids)
57 throw std::logic_error(fname +
": Grid block " + ugrid_name.second +
58 " does not have \"" + block_id_array_name +
63 Chi::log.
Log() << fname <<
": blocks have global-id arrays";
67 for (
auto& ugrid : ugrid_blocks)
68 append->AddInputData(ugrid.first);
70 append->MergePointsOn();
74 vtkUnstructuredGrid::SafeDownCast(append->GetOutput()));
77 << consolidated_ugrid->GetNumberOfCells() <<
" "
78 << consolidated_ugrid->GetNumberOfPoints();
82 const vtkIdType num_points = consolidated_ugrid->GetNumberOfPoints();
83 vtkIdType min_id = num_points;
85 for (vtkIdType p = 0; p < num_points; ++p)
87 auto point_gids = vtkIdTypeArray::SafeDownCast(
88 consolidated_ugrid->GetPointData()->GetGlobalIds());
89 auto point_gid = point_gids->GetValue(p);
91 min_id = std::min(min_id, point_gid);
92 max_id = std::max(max_id, point_gid);
95 Chi::log.
Log() <<
"Minimum and Maximum node-ids " << min_id <<
" "
99 std::map<std::string, size_t> cell_type_count_map;
100 const int64_t num_cells = consolidated_ugrid->GetNumberOfCells();
101 for (int64_t c = 0; c < num_cells; ++c)
103 auto cell = consolidated_ugrid->GetCell(c);
106 auto cell_type_name = cell->GetClassName();
107 cell_type_count_map[cell_type_name] += 1;
112 std::stringstream outstr;
114 auto RightPad = [](std::string& entry,
size_t width)
116 const size_t pad_size = width - entry.size();
117 entry.append(std::string(pad_size,
' '));
120 outstr <<
"Block statistictics:\n";
121 for (
const auto& [type_name, count] : cell_type_count_map)
123 auto temp_name = type_name;
124 RightPad(temp_name, 20);
125 outstr <<
" " << temp_name <<
" " << count <<
"\n";
130 return consolidated_ugrid;
137 std::vector<vtkUGridPtrAndName>& ugrid_blocks,
int desired_dimension)
139 std::vector<chi_mesh::vtkUGridPtrAndName> desired_blocks;
140 for (
auto& ugrid : ugrid_blocks)
142 if (ugrid.first->GetNumberOfCells() == 0)
continue;
144 std::vector<vtkUGridPtrAndName> single_grid = {ugrid};
147 if (block_dimension == desired_dimension)
148 desired_blocks.push_back(ugrid);
151 return desired_blocks;
159 const int desired_dimension)
161 std::vector<uint64_t> block_mat_ids;
162 size_t total_cells = 0;
164 for (
auto& ugrid : ugrid_blocks)
166 uint64_t num_cells = ugrid.first->GetNumberOfCells();
168 if (num_cells == 0)
continue;
170 if (ugrid.first->GetCell(0)->GetCellDimension() == desired_dimension)
172 total_cells += num_cells;
173 block_mat_ids.push_back(total_cells);
176 return block_mat_ids;
186 for (
auto& ugrid : ugrid_blocks)
188 const vtkIdType num_cells = ugrid.first->GetNumberOfCells();
190 if (num_cells == 0)
continue;
193 block_id_list->SetName(
"BlockID");
195 for (vtkIdType c = 0; c < num_cells; ++c)
196 block_id_list->InsertNextValue(block_id);
198 auto arr = ugrid.first->GetCellData()->GetArray(
"BlockID");
199 if (not arr) ugrid.first->GetCellData()->RemoveArray(
"BlockID");
201 ugrid.first->GetCellData()->AddArray(block_id_list);
210 const std::string& field_name,
211 const std::string& file_name)
213 const size_t total_cell_count = ugrid->GetNumberOfCells();
214 std::vector<int> material_ids(total_cell_count, -1);
218 vtkDataArray* cell_id_array_ptr;
219 if (field_name.empty())
222 <<
"A user-supplied field name from which to recover material "
224 <<
"has not been found. Material-ids will be left unassigned.";
225 goto end_error_checks;
229 auto cell_data = ugrid->GetCellData();
230 const auto vtk_abstract_array_ptr =
231 cell_data->GetAbstractArray(field_name.c_str());
233 if (!vtk_abstract_array_ptr)
236 <<
"The VTU file : \"" << file_name <<
"\" "
237 <<
"does not contain a vtkCellData field of name : \"" << field_name
238 <<
"\". Material-ids will be left unassigned.";
239 goto end_error_checks;
242 cell_id_array_ptr = vtkArrayDownCast<vtkDataArray>(vtk_abstract_array_ptr);
243 if (!cell_id_array_ptr)
246 <<
"The VTU file : \"" << file_name <<
"\" "
247 <<
"with vtkCellData field of name : \"" << field_name <<
"\" "
248 <<
"cannot be downcast to vtkDataArray. Material-ids will be left "
250 goto end_error_checks;
253 const auto cell_id_n_tup = cell_id_array_ptr->GetNumberOfTuples();
254 if (cell_id_n_tup != total_cell_count)
257 <<
"The VTU file : \"" << file_name <<
"\" "
258 <<
"with vtkCellData field of name : \"" << field_name
259 <<
"\" has n. tuples : " << cell_id_n_tup
260 <<
", but differs from the value expected : " << total_cell_count
261 <<
". Material-ids will be left unassigned.";
262 goto end_error_checks;
265 const auto cell_id_n_val = cell_id_array_ptr->GetNumberOfValues();
266 if (cell_id_n_val != total_cell_count)
269 <<
"The VTU file : \"" << file_name <<
"\" "
270 <<
"with vtkCellData field of name : \"" << field_name
271 <<
"\" has n. values : " << cell_id_n_val
272 <<
", but differs from the value expected : " << total_cell_count
273 <<
". Material-ids will be left unassigned.";
274 goto end_error_checks;
279 for (
size_t c = 0; c < total_cell_count; ++c)
281 std::vector<double> cell_id_vec(1);
282 cell_id_array_ptr->GetTuple(
static_cast<vtkIdType
>(c), cell_id_vec.data());
283 const auto mat_id = (int)cell_id_vec.front();
285 material_ids[c] = mat_id;
#define ChiLogicalErrorIf(condition, message)
LogStream Log(LOG_LVL level=LOG_0)
std::vector< int > BuildCellMaterialIDsFromField(vtkUGridPtr &ugrid, const std::string &field_name, const std::string &file_name)
std::vector< uint64_t > BuildBlockCellExtents(std::vector< vtkUGridPtrAndName > &ugrid_blocks, int desired_dimension)
vtkUGridPtr ConsolidateGridBlocks(std::vector< vtkUGridPtrAndName > &ugrid_blocks, const std::string &block_id_array_name="BlockID")
void SetBlockIDArrays(std::vector< vtkUGridPtrAndName > &ugrid_blocks)
int FindHighestDimension(std::vector< vtkUGridPtrAndName > &ugrid_blocks)
std::vector< vtkUGridPtrAndName > GetBlocksOfDesiredDimension(std::vector< vtkUGridPtrAndName > &ugrid_blocks, int desired_dimension)