www.mooseframework.org
MaterialVectorPostprocessor.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
16 #include "Material.h"
17 #include "IndirectSort.h"
18 #include "MooseMesh.h"
19 
20 #include "libmesh/quadrature.h"
21 
22 #include <numeric>
23 
24 template <>
27 {
29  params.addRequiredParam<MaterialName>("material",
30  "Material for which all properties will be recorded.");
31  params.addRequiredParam<std::vector<unsigned int>>(
32  "elem_ids", "Element IDs to print data for (others are ignored).");
33  return params;
34 }
35 
37  : ElementVectorPostprocessor(parameters),
38  _elem_filter(getParam<std::vector<unsigned int>>("elem_ids").begin(),
39  getParam<std::vector<unsigned int>>("elem_ids").end()),
40  _elem_ids(declareVector("elem_id")),
41  _qp_ids(declareVector("qp_id"))
42 {
43  auto & mat = getMaterialByName(getParam<MaterialName>("material"), true);
44  auto & prop_names = mat.getSuppliedItems();
45  if (mat.isBoundaryMaterial())
46  mooseError(name(), ": boundary materials (i.e. ", mat.name(), ") cannot be used");
47 
48  for (auto & id : _elem_filter)
49  {
50  auto el = _mesh.getMesh().query_elem_ptr(id);
51 
52  // We'd better have found the requested element on *some*
53  // processor.
54  bool found_elem = (el != nullptr);
55  this->comm().max(found_elem);
56 
57  // We might not have el on this processor in a distributed mesh,
58  // but it should be somewhere and it ought to have a material
59  // defined for its subdomain
60  if (!found_elem || (el && !mat.hasBlocks(el->subdomain_id())))
61  mooseError(name(), ": material ", mat.name(), " is not defined on element ", id);
62  }
63 
64  for (auto & prop : prop_names)
65  {
66  if (hasMaterialProperty<Real>(prop))
67  _prop_refs.push_back(&getMaterialProperty<Real>(prop));
68  else if (hasMaterialProperty<unsigned int>(prop))
69  _prop_refs.push_back(&getMaterialProperty<unsigned int>(prop));
70  else if (hasMaterialProperty<int>(prop))
71  _prop_refs.push_back(&getMaterialProperty<int>(prop));
72  else
73  {
74  mooseWarning("property " + prop +
75  " is of unsupported type and skipped by MaterialVectorPostprocessor");
76  continue;
77  }
78  _prop_vecs.push_back(&declareVector(prop));
79  _prop_names.push_back(prop);
80  }
81 }
82 
83 void
85 {
86  unsigned int elem_id = _current_elem->id();
87  if (_elem_filter.count(elem_id) == 0)
88  return;
89 
90  unsigned int nqp = _qrule->n_points();
91  for (unsigned int qp = 0; qp < nqp; qp++)
92  {
93  _elem_ids.push_back(elem_id);
94  _qp_ids.push_back(qp);
95  }
96 
97  for (unsigned int i = 0; i < _prop_names.size(); i++)
98  {
99  auto prop_name = _prop_names[i];
100  auto prop = _prop_vecs[i];
101  std::vector<Real> vals;
102  if (hasMaterialProperty<Real>(prop_name))
103  {
104  auto vals = dynamic_cast<const MaterialProperty<Real> *>(_prop_refs[i]);
105  for (unsigned int qp = 0; qp < nqp; qp++)
106  prop->push_back((*vals)[qp]);
107  }
108  else if (hasMaterialProperty<unsigned int>(prop_name))
109  {
110  auto vals = dynamic_cast<const MaterialProperty<unsigned int> *>(_prop_refs[i]);
111  for (unsigned int qp = 0; qp < nqp; qp++)
112  prop->push_back((*vals)[qp]);
113  }
114  else if (hasMaterialProperty<int>(prop_name))
115  {
116  auto vals = dynamic_cast<const MaterialProperty<int> *>(_prop_refs[i]);
117  for (unsigned int qp = 0; qp < nqp; qp++)
118  prop->push_back((*vals)[qp]);
119  }
120  }
121 }
122 
123 void
125 {
126  // collect all processor data
127  comm().gather(0, _elem_ids);
128  comm().gather(0, _qp_ids);
129  for (auto vec : _prop_vecs)
130  comm().gather(0, *vec);
131  sortVecs();
132 }
133 
134 void
136 {
137  auto & vpp = static_cast<const MaterialVectorPostprocessor &>(y);
138  _elem_ids.insert(_elem_ids.end(), vpp._elem_ids.begin(), vpp._elem_ids.end());
139  _qp_ids.insert(_qp_ids.end(), vpp._qp_ids.begin(), vpp._qp_ids.end());
140 
141  for (unsigned int i = 0; i < _prop_vecs.size(); i++)
142  {
143  auto & vec = *_prop_vecs[i];
144  auto & othervec = *vpp._prop_vecs[i];
145  vec.insert(vec.end(), othervec.begin(), othervec.end());
146  }
147  sortVecs();
148 }
149 
150 void
152 {
153  std::vector<size_t> ind;
154  ind.resize(_elem_ids.size());
155  std::iota(ind.begin(), ind.end(), 0);
156  std::sort(ind.begin(), ind.end(), [&](size_t a, size_t b) -> bool {
157  if (_elem_ids[a] == _elem_ids[b])
158  {
159  return _qp_ids[a] < _qp_ids[b];
160  }
161  return _elem_ids[a] < _elem_ids[b];
162  });
163 
166  for (auto vec : _prop_vecs)
167  Moose::applyIndices(*vec, ind);
168 }
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:47
void mooseWarning(Args &&...args) const
Definition: MooseObject.h:89
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:114
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:2355
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 ...
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 Elem *& _current_elem
The current element pointer (available during execute())
MPI_Comm comm
VectorPostprocessorValue & _elem_ids
Column of element id info.
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
This postprocessor records all scalar material properties of the specified material object on specifi...
Base class for user-specific data.
Definition: UserObject.h:42
InputParameters validParams< ElementVectorPostprocessor >()
MaterialVectorPostprocessor(const InputParameters &parameters)