https://mooseframework.inl.gov
ExtraIDIntegralVectorPostprocessor.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
11 #include "MooseVariable.h"
12 #include "MooseMesh.h"
13 #include "MooseMeshUtils.h"
14 
15 #include "libmesh/mesh_base.h"
16 
18 
21 {
23  params.addParam<std::vector<MaterialPropertyName>>(
24  "mat_prop", "The names of material properties that this VectorPostprocessor operates on");
25  params.addRequiredParam<std::vector<ExtraElementIDName>>(
26  "id_name", "List of extra element ID names by which to separate integral(s).");
27  params.addParam<bool>("average", false, "Whether or not to compute volume average");
28  params.addParam<std::string>("spatial_value_name",
29  "To specify which variable or material property is to be used when "
30  "using as a spatial user object functor");
31  params.addClassDescription("Integrates or averages variables based on extra element IDs");
32  params.makeParamNotRequired("variable");
33  return params;
34 }
35 
37  const InputParameters & parameters)
39  _average(getParam<bool>("average")),
40  _nvar(isParamValid("variable") ? coupledComponents("variable") : 0),
41  _nprop(isParamValid("mat_prop") ? getParam<std::vector<MaterialPropertyName>>("mat_prop").size()
42  : 0),
43  _prop_names(isParamValid("mat_prop") ? getParam<std::vector<MaterialPropertyName>>("mat_prop")
44  : std::vector<MaterialPropertyName>()),
45  _extra_id(getParam<std::vector<ExtraElementIDName>>("id_name")),
46  _n_extra_id(_extra_id.size()),
47  _spatial_evaluation_index(std::numeric_limits<unsigned int>::max())
48 {
49  if (!_nvar && !_nprop)
50  mooseError("Neither 'variable' nor 'mat_prop' was specified.");
51 
52  // create map of element ids to parsed vpp ids
55 
56  // set up variable vector containing parsed extra ids
57  for (unsigned int i = 0; i < _n_extra_id; ++i)
58  {
59  const auto id_type = _extra_id[i];
60  auto & p = declareVector("Level-" + std::to_string(i) + "-" + id_type);
61  // collect local map of local vpp id to extra id
62  std::map<dof_id_type, dof_id_type> extra_ids;
63  for (const auto & elem : _mesh.getMesh().active_local_element_ptr_range())
64  {
65  if (!hasBlocks(elem->subdomain_id()))
66  continue;
67  auto vpp_id = _unique_vpp_ids[elem->id()];
68  if (extra_ids.find(vpp_id) == extra_ids.end())
69  extra_ids[vpp_id] = elem->get_extra_integer(getElementIDIndexByName(id_type));
70  }
71  // create global map of local vpp id to extra id
72  comm().set_union(extra_ids);
73  p.resize(extra_ids.size());
74  for (auto it : extra_ids)
75  p[it.first] = it.second;
76  _extra_ids.push_back(&p);
77  }
78 
79  std::vector<std::string> vector_names;
80 
81  // declare vectors containing variable integral values
82  for (unsigned int i = 0; i < _nvar; ++i)
83  {
84  _vars.push_back(getVar("variable", i));
85  _var_values.push_back(&coupledValue("variable", i));
86  vector_names.push_back(_vars[i]->name());
87  auto & p = declareVector(vector_names.back());
88  p.resize((*_extra_ids[0]).size());
89  _integrals.push_back(&p);
90  }
91 
92  // declare vectors containing material property integral values
93  for (auto & name : _prop_names)
94  {
95  _props.push_back(&getMaterialPropertyByName<Real>(name));
96  vector_names.push_back(name);
97  auto & p = declareVector(vector_names.back());
98  p.resize((*_extra_ids[0]).size());
99  _integrals.push_back(&p);
100  }
101 
102  if (isParamValid("spatial_value_name"))
103  {
104  const auto & name = getParam<std::string>("spatial_value_name");
105  _spatial_evaluation_index = vector_names.size();
106  for (const auto i : index_range(vector_names))
107  if (name == vector_names[i])
108  {
110  break;
111  }
112  if (_spatial_evaluation_index == vector_names.size())
113  paramError("spatial_value_name",
114  "not a variable name or material property name of this vector postprocessor");
115  }
116 }
117 
118 void
120 {
121  for (auto & integral : _integrals)
122  std::fill(integral->begin(), integral->end(), 0);
123 
124  if (_average)
125  _volumes.assign((*_extra_ids[0]).size(), 0);
126 }
127 
128 void
130 {
131  if (hasBlocks(_current_elem->subdomain_id()))
132  {
133  unsigned int i = 0;
134  auto ipos = _unique_vpp_ids[_current_elem->id()];
135  for (unsigned int ivar = 0; ivar < _nvar; ++ivar, ++i)
136  if (_vars[ivar]->hasBlocks(_current_elem->subdomain_id()))
137  for (unsigned int qp = 0; qp < _qrule->n_points(); qp++)
138  (*_integrals[i])[ipos] += _JxW[qp] * _coord[qp] * (*_var_values[ivar])[qp];
139 
140  for (unsigned int iprop = 0; iprop < _nprop; ++iprop, ++i)
141  for (unsigned int qp = 0; qp < _qrule->n_points(); qp++)
142  (*_integrals[i])[ipos] += _JxW[qp] * _coord[qp] * (*_props[iprop])[qp];
143 
144  if (_average)
145  _volumes[ipos] += _current_elem->volume();
146  }
147 }
148 
149 void
151 {
152  for (auto & integral : _integrals)
153  gatherSum(*integral);
154 
155  if (_average)
156  {
158 
159  for (auto & integral : _integrals)
160  for (unsigned int i = 0; i < integral->size(); ++i)
161  (*integral)[i] /= _volumes[i];
162  }
163 }
164 
165 void
167 {
168  const auto & sibling = static_cast<const ExtraIDIntegralVectorPostprocessor &>(s);
169 
170  for (unsigned int i = 0; i < _integrals.size(); ++i)
171  for (size_t j = 0; j < (*_integrals[i]).size(); ++j)
172  (*_integrals[i])[j] += (*sibling._integrals[i])[j];
173 
174  if (_average)
175  for (unsigned int i = 0; i < _volumes.size(); ++i)
176  _volumes[i] += sibling._volumes[i];
177 }
178 
179 Real
181  const Moose::StateArg & libmesh_dbg_var(state)) const
182 {
183  mooseAssert(state.state == 0, "We do not currently support evaluating at old states");
184  return elementValue(elem.elem);
185 }
186 
187 Real
188 ExtraIDIntegralVectorPostprocessor::evaluate(const ElemQpArg & elem,
189  const Moose::StateArg & libmesh_dbg_var(state)) const
190 {
191  mooseAssert(state.state == 0, "We do not currently support evaluating at old states");
192  return elementValue(elem.elem);
193 }
194 
195 Real
197 {
199  paramError("spatial_value_name",
200  "Must set when ExtraIDIntegralVectorPostprocessor is used as a functor");
201  const auto it = _unique_vpp_ids.find(elem->id());
202  if (it == _unique_vpp_ids.end())
203  {
204  if (!hasBlocks(elem->subdomain_id()))
205  mooseError(
206  "Failed evaluating spatial value of element ",
207  elem->id(),
208  " in subdomain ",
209  elem->subdomain_id(),
210  ". This is likely due to the object using this "
211  "ExtraIDIntegralVectorPostprocessor as a functor operates on a subdomain outside of "
212  "this ExtraIDIntegralVectorPostprocessor");
213  else
214  mooseError("Failed evaluating spatial value of element ",
215  elem->id(),
216  ". We should not hit this error. Please contact code developers for help");
217  }
218  return (*_integrals[_spatial_evaluation_index])[it->second];
219 }
const unsigned int _nprop
Number of material properties to be integrated.
std::unordered_map< dof_id_type, dof_id_type > getExtraIDUniqueCombinationMap(const MeshBase &mesh, const std::set< SubdomainID > &block_ids, std::vector< ExtraElementIDName > extra_ids)
Crate a new set of element-wise IDs by finding unique combinations of existing extra ID values...
virtual void execute() override
Execute method.
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
const MooseArray< Real > & _coord
virtual Real evaluate(const ElemArg &elem, const Moose::StateArg &state) const override
Get values for an element based on the element extra element id.
std::vector< VectorPostprocessorValue * > _integrals
Vectors holding integrals over extra IDs.
std::vector< const MooseVariable * > _vars
Coupled MOOSE variables to be integrated.
const unsigned int _n_extra_id
Number of extra IDs in use.
std::vector< const VariableValue * > _var_values
Quadrature point values of coupled MOOSE variables.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & comm() const
std::vector< VectorPostprocessorValue * > _extra_ids
Vectors holding extra IDs.
MooseVariable * getVar(const std::string &var_name, unsigned int comp)
Extract pointer to a coupled variable.
Definition: Coupleable.C:288
virtual const std::set< SubdomainID > & blockIDs() const
Return the block subdomain ids for this object Note, if this is not block restricted, this function returns all mesh subdomain ids.
const std::vector< MaterialPropertyName > _prop_names
Name of material properties.
Real elementValue(const Elem *elem) const
Get the value for an element based on the element extra element id.
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:57
ExtraIDIntegralVectorPostprocessor(const InputParameters &parameters)
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
auto max(const L &left, const R &right)
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
virtual void threadJoin(const UserObject &uo) override
Must override.
virtual const VariableValue & coupledValue(const std::string &var_name, unsigned int comp=0) const
Returns value of a coupled variable.
Definition: Coupleable.C:497
Base class VectorPostprocessors operating on elemental variables.
unsigned int _spatial_evaluation_index
The index to the values that is used in &#39;evaluate&#39; function.
void gatherSum(T &value)
Gather the parallel sum of the variable passed in.
Definition: UserObject.h:126
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:3443
const unsigned int _nvar
Number of variables to be integrated.
const bool _average
whether or not to compute volume average
const std::vector< ExtraElementIDName > _extra_id
Extra IDs in use.
registerMooseObject("MooseApp", ExtraIDIntegralVectorPostprocessor)
std::unordered_map< dof_id_type, dof_id_type > _unique_vpp_ids
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
virtual unsigned int getElementIDIndexByName(const std::string &id_name) const
Return the accessing integer for an extra element integer with its name.
void makeParamNotRequired(const std::string &name)
Changes the parameter to not be required.
std::vector< Real > _volumes
Vector holding the volume of extra IDs.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const QBase *const & _qrule
const Elem *const & _current_elem
The current element pointer (available during execute())
const MooseArray< Real > & _JxW
This ExtraIDIntegralVectorPostprocessor source code is to integrate variables based on parsed extra I...
std::vector< const MaterialProperty< Real > * > _props
Material properties to be integrated.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
Base class for creating a user object with the SpatialUserObject and Moose::Functor APIs...
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
State argument for evaluating functors.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
void ErrorVector unsigned int
auto index_range(const T &sizable)
Base class for user-specific data.
Definition: UserObject.h:40
void set_union(T &data, const unsigned int root_id) const