Skip to content

Commit

Permalink
create a blocks to mesh converter to support the development of the S…
Browse files Browse the repository at this point in the history
…ideSetExtrusionGenerator ref idaholab#20880
  • Loading branch information
RocksSavage committed Jun 29, 2022
1 parent 41a07d6 commit 737d3e8
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 0 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,25 @@
# BlocksToMeshConverterGenerator

!syntax description /Mesh/BlocksToMeshConverterGenerator

## Overview

The Block to Mesh Converter is for moving one (or more) blocks from a mesh to a new mesh. This does not erase/delete the blocks from the original mesh. The new mesh has only one block (named 0).

It's important to note that no sidesets are preserved or made in the new mesh. There are other mesh generators for that, such as [SideSetsAroundSubdomainGenerator](SideSetsAroundSubdomainGenerator.md) or [AllSideSetsByNormalsGenerator](AllSideSetsByNormalsGenerator.md)

## Visual Example

### Input 2D Mesh

!media media/meshgenerators/block_to_mesh_before.png caption=A 3d object with multiple, multi-colored blocks

### Output of BlocksToMeshConverterGenerator

!media media/meshgenerators/block_to_mesh_after.png caption=A subset of blocks have been made into a new mesh

!syntax parameters /Mesh/BlocksToMeshConverterGenerator

!syntax inputs /Mesh/BlocksToMeshConverterGenerator

!syntax children /Mesh/BlocksToMeshConverterGenerator
31 changes: 31 additions & 0 deletions framework/include/meshgenerators/BlocksToMeshConverterGenerator.h
@@ -0,0 +1,31 @@
//* 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

#pragma once

#include "MeshGenerator.h"

/**
* Creates a new mesh out of one or more subdomains/blocks from another mesh
*/
class BlocksToMeshConverterGenerator : public MeshGenerator
{
public:
static InputParameters validParams();

BlocksToMeshConverterGenerator(const InputParameters & parameters);

std::unique_ptr<MeshBase> generate() override;

protected:
/// Mesh that comes from another generator
std::unique_ptr<MeshBase> & _input;

const std::vector<SubdomainName> _target_blocks;
};
123 changes: 123 additions & 0 deletions framework/src/meshgenerators/BlocksToMeshConverterGenerator.C
@@ -0,0 +1,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);
}
Binary file not shown.
@@ -0,0 +1,11 @@
[Mesh]
[file]
type = FileMeshGenerator
file = multiblock.e
[]
[blockToMesh]
type = BlocksToMeshConverterGenerator
input = file
target_blocks = "1 2"
[]
[]
@@ -0,0 +1,11 @@
[Mesh]
[file]
type = FileMeshGenerator
file = 3dmultiblock.e
[]
[blockToMesh]
type = BlocksToMeshConverterGenerator
input = file
target_blocks = "1 12 2 3 5 6"
[]
[]
Binary file not shown.
Binary file not shown.
Binary file not shown.
29 changes: 29 additions & 0 deletions test/tests/meshgenerators/block_to_mesh_converter_generator/tests
@@ -0,0 +1,29 @@
[Tests]
design = 'meshgenerators/BlocksToMeshConverterGenerator.md'
issues = '#20880'

[extract_block]
requirement = 'The system shall have the capability of creating a new mesh given a'
'one or more subdomains within an existing mesh.'

[three_dimensional_test]
type = 'Exodiff'
input = 'conv_multiblock.i'
exodiff = 'multiblock_in.e'
recover = false
cli_args = '--mesh-only'

detail = 'tests whether multiple subdomains/blocks can be incorporated into a new mesh in 3D'
[]

[lower_dimensional_block]
type = 'Exodiff'
input = 'conv_lowerDblock.i'
exodiff = 'lowerDblock_in.e'
recover = false
cli_args = '--mesh-only'

detail = 'tests whether multiple subdomains/blocks can be incorporated into a new mesh 2D'
[]
[]
[]

0 comments on commit 737d3e8

Please sign in to comment.