Skip to content

Commit

Permalink
Re-add piecewise constant by block regular material, as some users ne…
Browse files Browse the repository at this point in the history
…ed it
  • Loading branch information
GiudGiud committed Dec 3, 2021
1 parent b9a267c commit 51445a1
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 20 deletions.
32 changes: 32 additions & 0 deletions framework/include/materials/PiecewiseConstantByBlockMaterial.h
@@ -0,0 +1,32 @@
//* 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 "Material.h"

#include <unordered_map>

template <bool is_ad>
class PiecewiseConstantByBlockMaterialTempl : public Material
{
public:
PiecewiseConstantByBlockMaterialTempl(const InputParameters & parameters);
static InputParameters validParams();

protected:
virtual void computeQpProperties() override;

private:
GenericMaterialProperty<Real, is_ad> & _prop;
std::unordered_map<SubdomainID, Real> _sub_id_to_prop;
};

typedef PiecewiseConstantByBlockMaterialTempl<false> PiecewiseConstantByBlockMaterial;
typedef PiecewiseConstantByBlockMaterialTempl<true> ADPiecewiseConstantByBlockMaterial;
12 changes: 2 additions & 10 deletions framework/src/materials/PiecewiseByBlockFunctorMaterial.C
Expand Up @@ -11,14 +11,6 @@

registerMooseObject("MooseApp", PiecewiseByBlockFunctorMaterial);
registerMooseObject("MooseApp", ADPiecewiseByBlockFunctorMaterial);
registerMooseObjectRenamed("MooseApp",
PiecewiseConstantByBlockFunctorMaterial,
"06/30/2022 24:00",
PiecewiseByBlockFunctorMaterial);
registerMooseObjectRenamed("MooseApp",
ADPiecewiseConstantByBlockFunctorMaterial,
"06/30/2022 24:00",
ADPiecewiseByBlockFunctorMaterial);
registerMooseObjectRenamed("MooseApp",
FVPropValPerSubdomainMaterial,
"06/30/2022 24:00",
Expand All @@ -37,7 +29,7 @@ PiecewiseByBlockFunctorMaterialTempl<is_ad>::validParams()
// Somehow min gcc doesn't know the type of params here
params.template addRequiredParam<MaterialPropertyName>("prop_name",
"The name of the property to declare");
params.template addRequiredParam<std::map<std::string, MooseFunctorName>>(
params.template addRequiredParam<std::map<std::string, std::string>>(
"subdomain_to_prop_value", "Map from subdomain to property value. The value may be a constant"
" or any kind of functor (functions, variables, functor material properties)");
return params;
Expand All @@ -49,7 +41,7 @@ PiecewiseByBlockFunctorMaterialTempl<is_ad>::PiecewiseByBlockFunctorMaterialTemp
: FunctorMaterial(params), _prop(declareFunctorProperty<GenericReal<is_ad>>("prop_name"))
{
for (const auto & map_pr : getParam<std::map<std::string,
MooseFunctorName>>("subdomain_to_prop_value"))
std::string>>("subdomain_to_prop_value"))
{
const MooseFunctorName value = map_pr.second;
const auto & functor = &getFunctor<GenericReal<is_ad>>(value);
Expand Down
99 changes: 99 additions & 0 deletions framework/src/materials/PiecewiseConstantByBlockMaterial.C
@@ -0,0 +1,99 @@
//* 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 "PiecewiseConstantByBlockMaterial.h"

registerMooseObject("MooseApp", PiecewiseConstantByBlockMaterial);
registerMooseObject("MooseApp", ADPiecewiseConstantByBlockMaterial);

template <bool is_ad>
InputParameters
PiecewiseConstantByBlockMaterialTempl<is_ad>::validParams()
{
auto params = Material::validParams();
params.addClassDescription("Computes a property value on a per-subdomain basis");
// Somehow min gcc doesn't know the type of params here
params.template addRequiredParam<MaterialPropertyName>("prop_name",
"The name of the property to declare");
params.template addRequiredParam<std::map<std::string, Real>>(
"subdomain_to_prop_value", "Map from subdomain to property value");
return params;
}

template <bool is_ad>
PiecewiseConstantByBlockMaterialTempl<is_ad>::PiecewiseConstantByBlockMaterialTempl(
const InputParameters & params)
: Material(params), _prop(declareGenericProperty<Real, is_ad>("prop_name"))
{
for (const auto & map_pr : getParam<std::map<std::string, Real>>("subdomain_to_prop_value"))
_sub_id_to_prop.emplace(std::make_pair(_mesh.getSubdomainID(map_pr.first), map_pr.second));
}

template <bool is_ad>
void
PiecewiseConstantByBlockMaterialTempl<is_ad>::computeQpProperties()
{
if (!_bnd)
{
mooseAssert(_current_elem,
"We should be on a block which means we should definitely have a current element");
auto it = _sub_id_to_prop.find(_current_elem->subdomain_id());
mooseAssert(it != _sub_id_to_prop.end(),
"Block restriction must match the subdomain names passed in the "
"subdomain_to_prop_value parameter");
_prop[_qp] = it->second;

return;
}

if (_current_elem)
{
_face_info = _mesh.faceInfo(_current_elem, _current_side);
if (!_face_info)
{
const Elem * const neighbor = _current_elem->neighbor_ptr(_current_side);
mooseAssert(neighbor, "Should be non-null");
const auto neighbor_side = neighbor->which_neighbor_am_i(_current_elem);
_face_info = _mesh.faceInfo(neighbor, neighbor_side);
mooseAssert(_face_info, "We need to have retrieved something.");
}
mooseAssert(_current_elem->build_side_ptr(_current_side)->vertex_average() ==
_face_info->faceCentroid(),
"Making sure we're in the right place");
auto it = _sub_id_to_prop.find(_current_elem->subdomain_id());
if (it == _sub_id_to_prop.end())
{
// We may be a ghosted material
const bool current_elem_is_fi_elem = _current_elem == &_face_info->elem();
const Elem * const other_elem_to_try =
current_elem_is_fi_elem ? _face_info->neighborPtr() : &_face_info->elem();
mooseAssert(other_elem_to_try, "This should be non-null");
it = _sub_id_to_prop.find(other_elem_to_try->subdomain_id());
mooseAssert(it != _sub_id_to_prop.end(),
"Block restriction must match the subdomain names passed in the "
"subdomain_to_prop_value parameter");
}
_prop[_qp] = it->second;
return;
}

mooseAssert(_face_info,
"We must have set a face info object in order for the PiecewiseConstantByBlockMaterial "
"class to work on faces");

// We must be off the domain
auto it = _sub_id_to_prop.find(_face_info->elem().subdomain_id());
mooseAssert(it != _sub_id_to_prop.end(),
"Block restriction must match the subdomain names passed in the "
"subdomain_to_prop_value parameter");
_prop[_qp] = it->second;
}

template class PiecewiseConstantByBlockMaterialTempl<false>;
template class PiecewiseConstantByBlockMaterialTempl<true>;
Binary file not shown.
10 changes: 0 additions & 10 deletions test/tests/materials/piecewise_by_block_functor_material/tests

This file was deleted.

Binary file not shown.
Binary file not shown.
50 changes: 50 additions & 0 deletions test/tests/materials/piecewise_by_block_material/test.i
@@ -0,0 +1,50 @@
[Mesh]
[gen]
type = GeneratedMeshGenerator
dim = 1
nx = 10
xmax = 2
[]
[subdomain1]
input = gen
type = SubdomainBoundingBoxGenerator
bottom_left = '1.0 0 0'
block_id = 1
top_right = '2.0 1.0 0'
[]
[]

[AuxVariables]
[u]
type = MooseVariableFVReal
[]
[]

[AuxKernels]
[to_var]
type = ADMaterialRealAux
variable = 'u'
property = coeff
[]
[]

[Materials]
[coeff_mat]
type = ADPiecewiseConstantByBlockMaterial
prop_name = 'coeff'
subdomain_to_prop_value = '0 4
1 2'
[]
[]

[Executioner]
type = Steady
[]

[Problem]
solve = false
[]

[Outputs]
exodus = true
[]
17 changes: 17 additions & 0 deletions test/tests/materials/piecewise_by_block_material/tests
@@ -0,0 +1,17 @@
[Tests]
issues = '#16758'
[regular]
type = Exodiff
input = test.i
exodiff = test_out.e
requirement = 'The system shall provide a material object that can, for a single material property, map subdomain IDs to a possibly discontinuous property value.'
design = 'PiecewiseConstantByBlockMaterial.md'
[]
[functor]
type = Exodiff
input = test_functor.i
exodiff = test_functor_out.e
requirement = 'The system shall provide a material object that can, for a single functor material property, map subdomain IDs to different functors.'
design = 'PiecewiseByBlockFunctorMaterial.md'
[]
[]

0 comments on commit 51445a1

Please sign in to comment.