Skip to content

Commit

Permalink
create a blocks to mesh converter generater
Browse files Browse the repository at this point in the history
This was primarily created to support the development of the SideSetExtrusionGenerator

 ref idaholab#20880
  • Loading branch information
RocksSavage committed Jul 8, 2022
1 parent 41a07d6 commit 648d200
Show file tree
Hide file tree
Showing 16 changed files with 278 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 @@
# BlockToMeshConverterGenerator

!syntax description /Mesh/BlockToMeshConverterGenerator

## 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 (neither are any unique id's or ids in general, regardless of what options are set). There are other mesh generators for generating the sidesets, 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 BlockToMeshConverterGenerator

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

!syntax parameters /Mesh/BlockToMeshConverterGenerator

!syntax inputs /Mesh/BlockToMeshConverterGenerator

!syntax children /Mesh/BlockToMeshConverterGenerator
31 changes: 31 additions & 0 deletions framework/include/meshgenerators/BlockToMeshConverterGenerator.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 BlockToMeshConverterGenerator : public MeshGenerator
{
public:
static InputParameters validParams();

BlockToMeshConverterGenerator(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;
};
125 changes: 125 additions & 0 deletions framework/src/meshgenerators/BlockToMeshConverterGenerator.C
@@ -0,0 +1,125 @@
//* 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 "BlockToMeshConverterGenerator.h"
#include "CastUniquePointer.h"
#include "libmesh/elem.h"
#include "MooseMeshUtils.h"

registerMooseObject("MooseApp", BlockToMeshConverterGenerator);

InputParameters
BlockToMeshConverterGenerator::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;
}

BlockToMeshConverterGenerator::BlockToMeshConverterGenerator(const InputParameters & parameters)
: MeshGenerator(parameters),
_input(getMesh("input")),
_target_blocks(getParam<std::vector<SubdomainName>>("target_blocks"))
{
}

std::unique_ptr<MeshBase>
BlockToMeshConverterGenerator::generate()
{
std::unique_ptr<MeshBase> mesh = std::move(_input);

if (!mesh->is_replicated())
mooseError("BlockToMeshConverterGenerator is not implemented for distributed meshes.");

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;

for (subdomain_id_type target_block_id : target_block_ids)
{

for (auto elem : mesh->active_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->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 = BlockToMeshConverterGenerator
input = file
target_blocks = "1 2"
[]
[]
@@ -0,0 +1,11 @@
[Mesh]
[file]
type = FileMeshGenerator
file = mixedDimMesh.e
[]
[blockToMesh]
type = BlockToMeshConverterGenerator
input = file
target_blocks = "1"
[]
[]
@@ -0,0 +1,11 @@
[Mesh]
[file]
type = FileMeshGenerator
file = 3dmultiblock.e
[]
[blockToMesh]
type = BlockToMeshConverterGenerator
input = file
target_blocks = "1 12 2 3 5 6"
[]
[]
@@ -0,0 +1,11 @@
[Mesh]
[square]
type = GeneratedMeshGenerator
dim=2
[]
[blockToMesh]
type = BlockToMeshConverterGenerator
input = square
target_blocks = "0"
[]
[]
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
53 changes: 53 additions & 0 deletions test/tests/meshgenerators/block_to_mesh_converter_generator/tests
@@ -0,0 +1,53 @@
[Tests]
design = 'meshgenerators/BlockToMeshConverterGenerator.md'
issues = '#20880'

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

[simple_two_dimensional_test]
type = 'Exodiff'
input = 'conv_simple_2dblock.i'
exodiff = 'conv_simple_2dblocks_in.e'
recover = false
cli_args = '--mesh-only'
mesh_mode = 'replicated'

detail = 'tests wheather the simplest case - a one block, single element, 2d mesh of a square - can be handled'
[]

[two_dimensional_test]
type = 'Exodiff'
input = 'conv_2dblock.i'
exodiff = 'conv_2dblock_in.e'
recover = false
cli_args = '--mesh-only'
mesh_mode = 'replicated'

detail = 'tests whether several 2D blocks may be extracted from a 2D mesh'
[]

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

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

[lower_dimensional_test]
type = 'Exodiff'
input = 'conv_lowerDblock.i'
exodiff = 'conv_lowerDblock_in.e'
recover = false
cli_args = '--mesh-only'
mesh_mode = 'replicated'

detail = 'tests whether a lower dimension block (1D) may be extracted from a 2D mesh'
[]
[]
[]

0 comments on commit 648d200

Please sign in to comment.