forked from idaholab/moose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlocksToMeshConverterGenerator.C
123 lines (99 loc) · 4.34 KB
/
BlocksToMeshConverterGenerator.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html
#include "BlocksToMeshConverterGenerator.h"
#include "CastUniquePointer.h"
#include "libmesh/elem.h"
#include "MooseMeshUtils.h"
registerMooseObject("MooseApp", BlocksToMeshConverterGenerator);
InputParameters
BlocksToMeshConverterGenerator::validParams()
{
InputParameters params = MeshGenerator::validParams();
params.addClassDescription(
"Converts a block (subdomain) from a mesh into a stand-alone mesh with one block in it.");
// list params
params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
params.addRequiredParam<std::vector<SubdomainName>>(
"target_blocks",
"The (list of) blocks (or 'subdomains') we wish to have moved to a new mesh (by name, not "
"ID)");
return params;
}
BlocksToMeshConverterGenerator::BlocksToMeshConverterGenerator(const InputParameters & parameters)
: MeshGenerator(parameters),
_input(getMesh("input")),
_target_blocks(getParam<std::vector<SubdomainName>>("target_blocks"))
{
}
std::unique_ptr<MeshBase>
BlocksToMeshConverterGenerator::generate()
{
std::unique_ptr<MeshBase> mesh = std::move(_input);
auto new_mesh = buildMeshBaseObject();
std::vector<subdomain_id_type> target_block_ids =
MooseMeshUtils::getSubdomainIDs((*mesh), _target_blocks);
// Check that the block ids/names exist in the mesh
std::set<SubdomainID> mesh_blocks;
mesh->subdomain_ids(mesh_blocks);
for (std::size_t i = 0; i < target_block_ids.size(); ++i)
if (target_block_ids[i] == Moose::INVALID_BLOCK_ID || !mesh_blocks.count(target_block_ids[i]))
{
paramError("target_blocks",
"The target_block '",
getParam<std::vector<SubdomainName>>("target_blocks")[i],
"' was not found within the mesh.");
}
// know which nodes have already been inserted, by tracking the old mesh's node's ids'
std::unordered_map<dof_id_type, dof_id_type>
old_new_node_map; // keep this out fo the block loop for the mutlip
for (subdomain_id_type target_block_id : target_block_ids)
{
for (auto elem : mesh->active_local_subdomain_elements_ptr_range(target_block_id))
{
// make a deep copy so that mutiple meshs' destructors don't segfault at program termination
auto copy = elem->build(elem->type());
// index of node in the copy element must be managed manually as there is no intelligent
// insert method
dof_id_type copy_n_index = 0;
// correctly assign new copies of nodes, loop over nodes
for (dof_id_type i : elem->node_index_range())
{
auto & n = *elem->node_ptr(i);
if (old_new_node_map.count(n.id()))
{
// case where we have already inserted this particular point before
// then we need to find the already-inserted one and hook it up right
// to it's respective element
copy->set_node(copy_n_index++) = new_mesh->query_node_ptr(old_new_node_map[n.id()]);
}
else
{
// case where we've NEVER inserted this particular point before
// add them both to the element and the mesh
// Nodes' IDs are their indexes in the nodes' respective mesh
// If we set them as invalid they are automatically re-assigned
// to be the last element in the mesh's node array
std::unique_ptr<Node> node_copy = Node::build(elem->point(i), Node::invalid_id);
// Add to mesh. This method call will automatically re-assign the id; so must call this
// first
new_mesh->add_node(node_copy.get());
// Add to element copy (manually)
copy->set_node(copy_n_index++) = node_copy.get();
// remember the (old) ID
old_new_node_map[n.id()] = node_copy.release()->id();
}
}
// it is ok to release the copy element into the mesh because derived meshes class
// (ReplicatedMesh, DistributedMesh) manage their own elements, will delete them
new_mesh->add_elem(copy.release());
}
}
new_mesh->prepare_for_use();
return dynamic_pointer_cast<MeshBase>(new_mesh);
}