www.mooseframework.org
PointSamplerBase.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 
15 #include "PointSamplerBase.h"
16 
17 // MOOSE includes
18 #include "MooseMesh.h"
19 #include "MooseVariable.h"
20 
21 #include "libmesh/mesh_tools.h"
22 
23 template <>
26 {
28 
29  params += validParams<SamplerBase>();
30 
31  params.addRequiredCoupledVar(
32  "variable", "The names of the variables that this VectorPostprocessor operates on");
33 
34  return params;
35 }
36 
38  : GeneralVectorPostprocessor(parameters),
40  SamplerBase(parameters, this, _communicator),
41  _mesh(_subproblem.mesh())
42 {
43  std::vector<std::string> var_names(_coupled_moose_vars.size());
44 
45  for (unsigned int i = 0; i < _coupled_moose_vars.size(); i++)
46  var_names[i] = _coupled_moose_vars[i]->name();
47 
48  // Initialize the datastructions in SamplerBase
49  SamplerBase::setupVariables(var_names);
50 }
51 
52 void
54 {
56 
57  // We do this here just in case it's been destroyed and recreated because of mesh adaptivity.
59 
60  // We may not find a requested point on a distributed mesh, and
61  // that's okay.
62  _pl->enable_out_of_mesh_mode();
63 
64  // Reset the point arrays
65  _found_points.assign(_points.size(), false);
66 
67  _point_values.resize(_points.size());
68  std::for_each(
69  _point_values.begin(), _point_values.end(), [](std::vector<Real> & vec) { vec.clear(); });
70 }
71 
72 void
74 {
75  BoundingBox bbox = _mesh.getInflatedProcessorBoundingBox();
76 
78  std::vector<Point> point_vec(1);
79 
80  for (auto i = beginIndex(_points); i < _points.size(); ++i)
81  {
82  Point & p = _points[i];
83 
84  // Do a bounding box check so we're not doing unnecessary PointLocator lookups
85  if (bbox.contains_point(p))
86  {
87  auto & values = _point_values[i];
88 
89  if (values.empty())
90  values.resize(_coupled_moose_vars.size());
91 
92  // First find the element the hit lands in
93  const Elem * elem = getLocalElemContainingPoint(p);
94 
95  if (elem)
96  {
97  // We have to pass a vector of points into reinitElemPhys
98  point_vec[0] = p;
99 
101  _subproblem.reinitElemPhys(elem, point_vec, 0); // Zero is for tid
102 
103  for (auto j = beginIndex(_coupled_moose_vars); j < _coupled_moose_vars.size(); ++j)
104  values[j] = _coupled_moose_vars[j]->sln()[0]; // The zero is for the "qp"
105 
106  _found_points[i] = true;
107  }
108  }
109  }
110 }
111 
112 void
114 {
115  // Save off for speed
116  const auto pid = processor_id();
117 
118  /*
119  * Figure out which processor is actually going "claim" each point.
120  * If multiple processors found the point and computed values what happens is that
121  * maxloc will give us the smallest PID in max_id
122  */
123  std::vector<unsigned int> max_id(_found_points.size());
124 
125  _communicator.maxloc(_found_points, max_id);
126 
127  for (auto i = beginIndex(max_id); i < max_id.size(); ++i)
128  {
129  // Only do this check on the proc zero because it's the same on every processor
130  // _found_points should contain all 1's at this point (ie every point was found by a proc)
131  if (pid == 0 && !_found_points[i])
132  mooseError("In ", name(), ", sample point not found: ", _points[i]);
133 
134  if (max_id[i] == pid)
136  }
137 
139 }
140 
141 const Elem *
143 {
144  const Elem * elem = (*_pl)(p);
145 
146  if (elem && elem->processor_id() == processor_id())
147  return elem;
148 
149  return nullptr;
150 }
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:47
Base class for VectorPostprocessors that need to do "sampling" of values in the domain.
Definition: SamplerBase.h:46
virtual void initialize()
Initialize the datastructures.
Definition: SamplerBase.C:75
std::vector< MooseVariable * > _coupled_moose_vars
Vector of coupled variables.
Definition: Coupleable.h:324
std::vector< Real > _ids
The ID to use for each point (yes, this is Real on purpose)
This class is here to combine the VectorPostprocessor interface and the base class VectorPostprocesso...
virtual void initialize()
Initialize the datastructures.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual void setCurrentSubdomainID(const Elem *elem, THREAD_ID tid)=0
std::vector< Point > _points
The points to evaluate at.
SubProblem & _subproblem
Reference to the Subproblem for this user object.
Definition: UserObject.h:146
virtual void finalize()
Finalize.
std::vector< std::vector< Real > > _point_values
Vector of values per point.
void setupVariables(const std::vector< std::string > &variable_names)
You MUST call this in the constructor of the child class and pass down the name of the variables...
Definition: SamplerBase.C:51
const Elem * getLocalElemContainingPoint(const Point &p)
Find the local element that contains the point.
std::vector< short > _found_points
Whether or not the Point was found on this processor (short because bool and char don&#39;t work with MPI...
InputParameters validParams< GeneralVectorPostprocessor >()
InputParameters validParams< PointSamplerBase >()
virtual void reinitElemPhys(const Elem *elem, std::vector< Point > phys_points_in_elem, THREAD_ID tid)=0
virtual std::unique_ptr< PointLocatorBase > getPointLocator() const
Proxy function to get a (sub)PointLocator from either the underlying libMesh mesh (default)...
Definition: MooseMesh.C:2540
virtual void execute()
Execute method.
void addRequiredCoupledVar(const std::string &name, const std::string &doc_string)
This method adds a coupled variable name pair.
PointSamplerBase(const InputParameters &parameters)
virtual void finalize()
Finalize the values.
Definition: SamplerBase.C:87
virtual void addSample(const Point &p, const Real &id, const std::vector< Real > &values)
Call this with the value of every variable at each point you want to sample at.
Definition: SamplerBase.C:61
BoundingBox getInflatedProcessorBoundingBox(Real inflation_multiplier=0.01) const
Get a (slightly inflated) processor bounding box.
Definition: MooseMesh.C:2332
Intermediate base class that ties together all the interfaces for getting MooseVariables with the Moo...
InputParameters validParams< SamplerBase >()
Definition: SamplerBase.C:26
std::unique_ptr< PointLocatorBase > _pl
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
MooseMesh & _mesh
The Mesh we&#39;re using.