www.mooseframework.org
MaterialVectorPostprocessor.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "Material.h"
12 #include "IndirectSort.h"
13 #include "MooseMesh.h"
14 
15 #include "libmesh/quadrature.h"
16 
17 #include <numeric>
18 
20 
21 template <>
24 {
26  params.addRequiredParam<MaterialName>("material",
27  "Material for which all properties will be recorded.");
28  params.addRequiredParam<std::vector<unsigned int>>(
29  "elem_ids", "Element IDs to print data for (others are ignored).");
30  return params;
31 }
32 
34  : ElementVectorPostprocessor(parameters),
35  _elem_filter(getParam<std::vector<unsigned int>>("elem_ids").begin(),
36  getParam<std::vector<unsigned int>>("elem_ids").end()),
37  _elem_ids(declareVector("elem_id")),
38  _qp_ids(declareVector("qp_id"))
39 {
40  auto & mat = getMaterialByName(getParam<MaterialName>("material"), true);
41  auto & prop_names = mat.getSuppliedItems();
42  if (mat.isBoundaryMaterial())
43  mooseError(name(), ": boundary materials (i.e. ", mat.name(), ") cannot be used");
44 
45  for (auto & id : _elem_filter)
46  {
47  auto el = _mesh.getMesh().query_elem_ptr(id);
48 
49  // We'd better have found the requested element on *some*
50  // processor.
51  bool found_elem = (el != nullptr);
52  this->comm().max(found_elem);
53 
54  // We might not have el on this processor in a distributed mesh,
55  // but it should be somewhere and it ought to have a material
56  // defined for its subdomain
57  if (!found_elem || (el && !mat.hasBlocks(el->subdomain_id())))
58  mooseError(name(), ": material ", mat.name(), " is not defined on element ", id);
59  }
60 
61  for (auto & prop : prop_names)
62  {
63  if (hasMaterialProperty<Real>(prop))
64  _prop_refs.push_back(&getMaterialProperty<Real>(prop));
65  else if (hasMaterialProperty<unsigned int>(prop))
66  _prop_refs.push_back(&getMaterialProperty<unsigned int>(prop));
67  else if (hasMaterialProperty<int>(prop))
68  _prop_refs.push_back(&getMaterialProperty<int>(prop));
69  else
70  {
71  mooseWarning("property " + prop +
72  " is of unsupported type and skipped by MaterialVectorPostprocessor");
73  continue;
74  }
75  _prop_vecs.push_back(&declareVector(prop));
76  _prop_names.push_back(prop);
77  }
78 }
79 
80 void
82 {
83  unsigned int elem_id = _current_elem->id();
84  if (_elem_filter.count(elem_id) == 0)
85  return;
86 
87  unsigned int nqp = _qrule->n_points();
88  for (unsigned int qp = 0; qp < nqp; qp++)
89  {
90  _elem_ids.push_back(elem_id);
91  _qp_ids.push_back(qp);
92  }
93 
94  for (unsigned int i = 0; i < _prop_names.size(); i++)
95  {
96  auto prop_name = _prop_names[i];
97  auto prop = _prop_vecs[i];
98  std::vector<Real> vals;
99  if (hasMaterialProperty<Real>(prop_name))
100  {
101  auto vals = dynamic_cast<const MaterialProperty<Real> *>(_prop_refs[i]);
102  for (unsigned int qp = 0; qp < nqp; qp++)
103  prop->push_back((*vals)[qp]);
104  }
105  else if (hasMaterialProperty<unsigned int>(prop_name))
106  {
107  auto vals = dynamic_cast<const MaterialProperty<unsigned int> *>(_prop_refs[i]);
108  for (unsigned int qp = 0; qp < nqp; qp++)
109  prop->push_back((*vals)[qp]);
110  }
111  else if (hasMaterialProperty<int>(prop_name))
112  {
113  auto vals = dynamic_cast<const MaterialProperty<int> *>(_prop_refs[i]);
114  for (unsigned int qp = 0; qp < nqp; qp++)
115  prop->push_back((*vals)[qp]);
116  }
117  }
118 }
119 
120 void
122 {
123  // collect all processor data
124  comm().gather(0, _elem_ids);
125  comm().gather(0, _qp_ids);
126  for (auto vec : _prop_vecs)
127  comm().gather(0, *vec);
128  sortVecs();
129 }
130 
131 void
133 {
134  auto & vpp = static_cast<const MaterialVectorPostprocessor &>(y);
135  _elem_ids.insert(_elem_ids.end(), vpp._elem_ids.begin(), vpp._elem_ids.end());
136  _qp_ids.insert(_qp_ids.end(), vpp._qp_ids.begin(), vpp._qp_ids.end());
137 
138  for (unsigned int i = 0; i < _prop_vecs.size(); i++)
139  {
140  auto & vec = *_prop_vecs[i];
141  auto & othervec = *vpp._prop_vecs[i];
142  vec.insert(vec.end(), othervec.begin(), othervec.end());
143  }
144  sortVecs();
145 }
146 
147 void
149 {
150  std::vector<size_t> ind;
151  ind.resize(_elem_ids.size());
152  std::iota(ind.begin(), ind.end(), 0);
153  std::sort(ind.begin(), ind.end(), [&](size_t a, size_t b) -> bool {
154  if (_elem_ids[a] == _elem_ids[b])
155  {
156  return _qp_ids[a] < _qp_ids[b];
157  }
158  return _elem_ids[a] < _elem_ids[b];
159  });
160 
163  for (auto vec : _prop_vecs)
164  Moose::applyIndices(*vec, ind);
165 }
void mooseWarning(Args &&... args) const
Definition: MooseObject.h:155
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
InputParameters validParams< MaterialVectorPostprocessor >()
std::vector< VectorPostprocessorValue * > _prop_vecs
Columns for each (scalar) property of the material.
VectorPostprocessorValue & _qp_ids
Column of quadrature point indices.
virtual void finalize() override
Finalize.
void applyIndices(T &container, const std::vector< size_t > &indices)
Uses indices created by the indirectSort function to sort the given container (which must support ran...
Definition: IndirectSort.h:108
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
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...
virtual void threadJoin(const UserObject &y) override
Must override.
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2567
Material & getMaterialByName(const std::string &name, bool no_warn=false)
std::set< unsigned int > _elem_filter
Element ids to record material properties for.
void sortVecs()
Sorts all data in the VectorPostProcessorValue objects so that output from this postprocessor is orde...
virtual void execute() override
Execute method.
std::vector< const PropertyValue * > _prop_refs
Reference to each material property - used to retrieve the actual property values at every execution ...
registerMooseObject("MooseApp", MaterialVectorPostprocessor)
VectorPostprocessorValue & declareVector(const std::string &vector_name)
Register a new vector to fill up.
std::vector< std::string > _prop_names
Names for every property in the material - used for determining if properties are scalar or not...
const QBase *const & _qrule
const Elem *const & _current_elem
The current element pointer (available during execute())
MPI_Comm comm
VectorPostprocessorValue & _elem_ids
Column of element id info.
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
This postprocessor records all scalar material properties of the specified material object on specifi...
Base class for user-specific data.
Definition: UserObject.h:37
InputParameters validParams< ElementVectorPostprocessor >()
MaterialVectorPostprocessor(const InputParameters &parameters)