Skip to content

Commit

Permalink
Merge pull request idaholab#26696 from YaqiWang/mat_prop_active_26675
Browse files Browse the repository at this point in the history
Add a function in material base class to check whether a property is active
  • Loading branch information
lindsayad committed Feb 7, 2024
2 parents 096ce97 + 4246721 commit bb2a44c
Show file tree
Hide file tree
Showing 23 changed files with 521 additions and 69 deletions.
8 changes: 8 additions & 0 deletions framework/doc/content/syntax/Materials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ point. Recall that "_diffusivity" is a reference to a `MaterialProperty` type. T
type is a container that stores the values of a property for each quadrature point. Therefore, this
container must be indexed by `_qp` to compute the value for a specific quadrature point.

!alert note
`ExampleMaterial` can call `isPropertyActive(_diffusivity.id())` in its `computeQpProperties` to
check whether this property is consumed during the run-time. This function provides a capability
of skipping evaluations of certain material properties within a material when such evaluations are
costly for performance optimization. MOOSE calls materials to do the evaluations when needed.
This `isPropertyActive` routine gives code developers a finer control on the material property
evaluation.

## Consuming Properties

Objects that require material properties consume them using one of two functions
Expand Down
18 changes: 18 additions & 0 deletions framework/include/materials/MaterialBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ class MaterialBase : public MooseObject,
const std::vector<std::shared_ptr<MaterialBase>> & mats,
const bool allow_stateful);

/**
* Set active properties of this material
* Note: This function is called by FEProblemBase::setActiveMaterialProperties in an element loop
* typically when switching subdomains.
*/
void setActiveProperties(const std::unordered_set<unsigned int> & needed_props);

protected:
/**
* Users must override this method.
Expand Down Expand Up @@ -277,6 +284,14 @@ class MaterialBase : public MooseObject,

virtual const QBase & qRule() const = 0;

/**
* Check whether a material property is active
*/
bool isPropertyActive(const unsigned int prop_id) const
{
return _active_prop_ids.count(prop_id) > 0;
}

SubProblem & _subproblem;

FEProblemBase & _fe_problem;
Expand Down Expand Up @@ -309,6 +324,9 @@ class MaterialBase : public MooseObject,
/// the name strings each time.
std::set<unsigned int> _supplied_prop_ids;

/// The ids of the current active supplied properties
std::unordered_set<unsigned int> _active_prop_ids;

/// If False MOOSE does not compute this property
const bool _compute;

Expand Down
8 changes: 0 additions & 8 deletions framework/include/materials/MaterialWarehouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,10 @@ class MaterialWarehouse : public MooseObjectWarehouse<MaterialBase>
std::shared_ptr<MaterialBase> face,
THREAD_ID tid = 0);

/**
* A special method unique to this class for adding Interface material objects.
*/
void addInterfaceObject(std::shared_ptr<MaterialBase> interface, THREAD_ID tid = 0);

protected:
/// Storage for neighbor material objects (Block are stored in the base class)
MooseObjectWarehouse<MaterialBase> _neighbor_materials;

/// Storage for face material objects (Block are stored in the base class)
MooseObjectWarehouse<MaterialBase> _face_materials;

/// Storage for interface material objects
MooseObjectWarehouse<MaterialBase> _interface_materials;
};
18 changes: 2 additions & 16 deletions framework/include/problems/FEProblemBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -796,13 +796,6 @@ class FEProblemBase : public SubProblem, public Restartable
const SubdomainID blk_id,
const THREAD_ID tid);

/**
* Add the MooseVariables that the current materials depend on to the dependency list.
*
* This MUST be done after the dependency list has been set for all the other objects!
*/
void prepareMaterials(const SubdomainID blk_id, const THREAD_ID tid);

void reinitMaterials(SubdomainID blk_id, const THREAD_ID tid, bool swap_stateful = true);

/**
Expand Down Expand Up @@ -869,13 +862,6 @@ class FEProblemBase : public SubProblem, public Restartable
void setActiveMaterialProperties(const std::unordered_set<unsigned int> & mat_prop_ids,
const THREAD_ID tid);

/**
* Get the material properties required by the current computing thread.
*
* @param tid The thread id
*/
const std::unordered_set<unsigned int> & getActiveMaterialProperties(const THREAD_ID tid) const;

/**
* Method to check whether or not a list of active material roperties has been set. This method
* is called by reinitMaterials to determine whether Material computeProperties methods need to be
Expand Down Expand Up @@ -2423,8 +2409,8 @@ class FEProblemBase : public SubProblem, public Restartable

std::vector<std::vector<const MooseVariableFEBase *>> _uo_jacobian_moose_vars;

/// Set of material property ids that determine whether materials get reinited
std::vector<std::unordered_set<unsigned int>> _active_material_property_ids;
/// Whether there are active material properties on each thread
std::vector<unsigned char> _has_active_material_properties;

SolverParams _solver_params;

Expand Down
3 changes: 1 addition & 2 deletions framework/src/loops/ComputeElemAuxVarsThread.C
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ ComputeElemAuxVarsThread<AuxKernelType>::subdomainChanged()
}

_fe_problem.setActiveElementalMooseVariables(needed_moose_vars, _tid);
_fe_problem.setActiveMaterialProperties(needed_mat_props, _tid);
_fe_problem.prepareMaterials(_subdomain, _tid);
_fe_problem.prepareMaterials(needed_mat_props, _subdomain, _tid);
_fe_problem.setActiveFEVariableCoupleableMatrixTags(needed_fe_var_matrix_tags, _tid);
_fe_problem.setActiveFEVariableCoupleableVectorTags(needed_fe_var_vector_tags, _tid);
}
Expand Down
3 changes: 1 addition & 2 deletions framework/src/loops/ComputeIndicatorThread.C
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ ComputeIndicatorThread::subdomainChanged()
std::unordered_set<unsigned int> needed_mat_props;
_indicator_whs.updateMatPropDependency(needed_mat_props, _tid);
_internal_side_indicators.updateMatPropDependency(needed_mat_props, _tid);
_fe_problem.setActiveMaterialProperties(needed_mat_props, _tid);

_fe_problem.prepareMaterials(_subdomain, _tid);
_fe_problem.prepareMaterials(needed_mat_props, _subdomain, _tid);
}

void
Expand Down
5 changes: 4 additions & 1 deletion framework/src/loops/ComputeMarkerThread.C
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ ComputeMarkerThread::subdomainChanged()
for (auto * var : _aux_sys._elem_vars[_tid])
var->prepareAux();

std::unordered_set<unsigned int> needed_mat_props;
_marker_whs.updateMatPropDependency(needed_mat_props, _tid);

_fe_problem.setActiveElementalMooseVariables(needed_moose_vars, _tid);
_fe_problem.prepareMaterials(_subdomain, _tid);
_fe_problem.prepareMaterials(needed_mat_props, _subdomain, _tid);
}

void
Expand Down
3 changes: 1 addition & 2 deletions framework/src/loops/ComputeUserObjectsThread.C
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ ComputeUserObjectsThread::subdomainChanged()
_subdomain, needed_fe_var_vector_tags, _tid);

_fe_problem.setActiveElementalMooseVariables(needed_moose_vars, _tid);
_fe_problem.setActiveMaterialProperties(needed_mat_props, _tid);
_fe_problem.setActiveFEVariableCoupleableVectorTags(needed_fe_var_vector_tags, _tid);
_fe_problem.prepareMaterials(_subdomain, _tid);
_fe_problem.prepareMaterials(needed_mat_props, _subdomain, _tid);

querySubdomain(Interfaces::InternalSideUserObject, _internal_side_objs);
querySubdomain(Interfaces::ElementUserObject, _element_objs);
Expand Down
3 changes: 1 addition & 2 deletions framework/src/loops/NonlinearThread.C
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ NonlinearThread::subdomainChanged()
}

_fe_problem.setActiveElementalMooseVariables(needed_moose_vars, _tid);
_fe_problem.setActiveMaterialProperties(needed_mat_props, _tid);
_fe_problem.setActiveFEVariableCoupleableVectorTags(needed_fe_var_vector_tags, _tid);
_fe_problem.prepareMaterials(_subdomain, _tid);
_fe_problem.prepareMaterials(needed_mat_props, _subdomain, _tid);
}

void
Expand Down
5 changes: 5 additions & 0 deletions framework/src/materials/Material.C
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ Material::subdomainSetup()
for (const auto & prop_id : _supplied_prop_ids)
props[prop_id].resize(nqp);

// consider all properties are active
_active_prop_ids.clear();
for (const auto & id : _supplied_prop_ids)
_active_prop_ids.insert(id);

_qp = 0;
computeQpProperties();

Expand Down
9 changes: 9 additions & 0 deletions framework/src/materials/MaterialBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ MaterialBase::registerPropName(const std::string & prop_name, bool is_get, const
_has_stateful_property = true;
}

void
MaterialBase::setActiveProperties(const std::unordered_set<unsigned int> & needed_props)
{
_active_prop_ids.clear();
for (const auto supplied_id : _supplied_prop_ids)
if (needed_props.count(supplied_id))
_active_prop_ids.insert(supplied_id);
}

std::set<OutputName>
MaterialBase::getOutputs()
{
Expand Down
19 changes: 0 additions & 19 deletions framework/src/materials/MaterialWarehouse.C
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ MaterialWarehouse::addObjects(std::shared_ptr<MaterialBase> block,
_face_materials.addObject(face, tid);
}

void
MaterialWarehouse::addInterfaceObject(std::shared_ptr<MaterialBase> interface, THREAD_ID tid /*=0*/)
{
_interface_materials.addObject(interface, tid);
}

const MooseObjectWarehouse<MaterialBase> &
MaterialWarehouse::operator[](Moose::MaterialDataType data_type) const
{
Expand All @@ -39,9 +33,6 @@ MaterialWarehouse::operator[](Moose::MaterialDataType data_type) const
case Moose::FACE_MATERIAL_DATA:
return _face_materials;

case Moose::INTERFACE_MATERIAL_DATA:
return _interface_materials;

default:
return *this;
}
Expand All @@ -53,7 +44,6 @@ MaterialWarehouse::initialSetup(THREAD_ID tid /*=0*/) const
MooseObjectWarehouse<MaterialBase>::initialSetup(tid);
_neighbor_materials.initialSetup(tid);
_face_materials.initialSetup(tid);
_interface_materials.initialSetup(tid);
}

void
Expand All @@ -62,7 +52,6 @@ MaterialWarehouse::timestepSetup(THREAD_ID tid /*=0*/) const
MooseObjectWarehouse<MaterialBase>::timestepSetup(tid);
_neighbor_materials.timestepSetup(tid);
_face_materials.timestepSetup(tid);
_interface_materials.timestepSetup(tid);
}

void
Expand Down Expand Up @@ -97,7 +86,6 @@ MaterialWarehouse::residualSetup(THREAD_ID tid /*=0*/) const
MooseObjectWarehouse<MaterialBase>::residualSetup(tid);
_neighbor_materials.residualSetup(tid);
_face_materials.residualSetup(tid);
_interface_materials.residualSetup(tid);
}

void
Expand All @@ -106,7 +94,6 @@ MaterialWarehouse::jacobianSetup(THREAD_ID tid /*=0*/) const
MooseObjectWarehouse<MaterialBase>::jacobianSetup(tid);
_neighbor_materials.jacobianSetup(tid);
_face_materials.jacobianSetup(tid);
_interface_materials.jacobianSetup(tid);
}

void
Expand All @@ -115,7 +102,6 @@ MaterialWarehouse::updateActive(THREAD_ID tid /*=0*/)
MooseObjectWarehouse<MaterialBase>::updateActive(tid);
_neighbor_materials.updateActive(tid);
_face_materials.updateActive(tid);
_interface_materials.updateActive(tid);
}

void
Expand All @@ -138,10 +124,5 @@ MaterialWarehouse::sort(THREAD_ID tid /*=0*/)
for (auto & object_pair : _face_materials._all_boundary_objects[tid])
sortHelper(object_pair.second);

for (auto & object_pair : _interface_materials._all_block_objects[tid])
sortHelper(object_pair.second);
for (auto & object_pair : _interface_materials._all_boundary_objects[tid])
sortHelper(object_pair.second);

updateActive(tid);
}
26 changes: 11 additions & 15 deletions framework/src/problems/FEProblemBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ FEProblemBase::FEProblemBase(const InputParameters & parameters)
_vector_curl_zero.resize(n_threads);
_uo_jacobian_moose_vars.resize(n_threads);

_active_material_property_ids.resize(n_threads);
_has_active_material_properties.resize(n_threads, 0);

_block_mat_side_cache.resize(n_threads);
_bnd_mat_side_cache.resize(n_threads);
Expand Down Expand Up @@ -3506,12 +3506,6 @@ FEProblemBase::prepareMaterials(const std::unordered_set<unsigned int> & consume
setActiveMaterialProperties(needed_mat_props, tid);
}

void
FEProblemBase::prepareMaterials(const SubdomainID blk_id, const THREAD_ID tid)
{
prepareMaterials(getActiveMaterialProperties(tid), blk_id, tid);
}

void
FEProblemBase::reinitMaterials(SubdomainID blk_id, const THREAD_ID tid, bool swap_stateful)
{
Expand Down Expand Up @@ -5376,25 +5370,27 @@ void
FEProblemBase::setActiveMaterialProperties(const std::unordered_set<unsigned int> & mat_prop_ids,
const THREAD_ID tid)
{
_active_material_property_ids[tid] = mat_prop_ids;
}
// mark active properties in every material
for (auto & mat : _all_materials.getObjects(tid))
mat->setActiveProperties(mat_prop_ids);
for (auto & mat : _all_materials[Moose::FACE_MATERIAL_DATA].getObjects(tid))
mat->setActiveProperties(mat_prop_ids);
for (auto & mat : _all_materials[Moose::NEIGHBOR_MATERIAL_DATA].getObjects(tid))
mat->setActiveProperties(mat_prop_ids);

const std::unordered_set<unsigned int> &
FEProblemBase::getActiveMaterialProperties(const THREAD_ID tid) const
{
return _active_material_property_ids[tid];
_has_active_material_properties[tid] = !mat_prop_ids.empty();
}

bool
FEProblemBase::hasActiveMaterialProperties(const THREAD_ID tid) const
{
return !_active_material_property_ids[tid].empty();
return _has_active_material_properties[tid];
}

void
FEProblemBase::clearActiveMaterialProperties(const THREAD_ID tid)
{
_active_material_property_ids[tid].clear();
_has_active_material_properties[tid] = 0;
}

void
Expand Down
3 changes: 1 addition & 2 deletions modules/ray_tracing/src/userobjects/RayTracingStudy.C
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,7 @@ RayTracingStudy::segmentSubdomainSetup(const SubdomainID subdomain,
var->prepareAux();

_fe_problem.setActiveElementalMooseVariables(needed_moose_vars, tid);
_fe_problem.setActiveMaterialProperties(needed_mat_props, tid);
_fe_problem.prepareMaterials(subdomain, tid);
_fe_problem.prepareMaterials(needed_mat_props, subdomain, tid);
}

void
Expand Down
27 changes: 27 additions & 0 deletions test/include/auxkernels/CheckActiveMatProp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//* 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 "AuxKernel.h"

class CheckActiveMatProp : public AuxKernel
{
public:
static InputParameters validParams();

CheckActiveMatProp(const InputParameters & parameters);

protected:
virtual Real computeValue();
const MaterialPropertyName _prop_name;
FEProblemBase & _problem;
const MaterialData & _data;
const unsigned int _prop_id;
};
26 changes: 26 additions & 0 deletions test/include/materials/ActiveGenericConstantMaterial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//* 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 "GenericConstantMaterial.h"

class ActiveGenericConstantMaterial : public GenericConstantMaterial
{
public:
static InputParameters validParams();

ActiveGenericConstantMaterial(const InputParameters & parameters);

const std::unordered_set<unsigned int> & getActivePropIDs() { return _active_prop_ids; }

protected:
virtual void computeProperties() override;
virtual void computeQpProperties() override;
};
Loading

0 comments on commit bb2a44c

Please sign in to comment.