www.mooseframework.org
PointSamplerBase.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 
10 #include "PointSamplerBase.h"
11 
12 // MOOSE includes
13 #include "MooseMesh.h"
14 #include "MooseVariableFE.h"
15 
16 #include "libmesh/mesh_tools.h"
17 
18 template <>
21 {
23 
24  params += validParams<SamplerBase>();
25 
26  params.addRequiredCoupledVar(
27  "variable", "The names of the variables that this VectorPostprocessor operates on");
28  params.addParam<PostprocessorName>(
29  "scaling", 1.0, "The postprocessor that the variables are multiplied with");
30 
31  return params;
32 }
33 
35  : GeneralVectorPostprocessor(parameters),
37  MooseVariableInterface<Real>(this,
38  false,
39  "variable",
42  SamplerBase(parameters, this, _communicator),
43  _mesh(_subproblem.mesh()),
44  _pp_value(getPostprocessorValue("scaling"))
45 {
47 
48  std::vector<std::string> var_names(_coupled_moose_vars.size());
49 
50  for (unsigned int i = 0; i < _coupled_moose_vars.size(); i++)
51  var_names[i] = _coupled_moose_vars[i]->name();
52 
53  // Initialize the datastructions in SamplerBase
54  SamplerBase::setupVariables(var_names);
55 }
56 
57 void
59 {
61 
62  // We do this here just in case it's been destroyed and recreated because of mesh adaptivity.
64 
65  // We may not find a requested point on a distributed mesh, and
66  // that's okay.
67  _pl->enable_out_of_mesh_mode();
68 
69  // Reset the point arrays
70  _found_points.assign(_points.size(), false);
71 
72  _point_values.resize(_points.size());
73  std::for_each(
74  _point_values.begin(), _point_values.end(), [](std::vector<Real> & vec) { vec.clear(); });
75 }
76 
77 void
79 {
80  BoundingBox bbox = _mesh.getInflatedProcessorBoundingBox();
81 
83  std::vector<Point> point_vec(1);
84 
85  for (MooseIndex(_points) i = 0; i < _points.size(); ++i)
86  {
87  Point & p = _points[i];
88 
89  // Do a bounding box check so we're not doing unnecessary PointLocator lookups
90  if (bbox.contains_point(p))
91  {
92  auto & values = _point_values[i];
93 
94  if (values.empty())
95  values.resize(_coupled_moose_vars.size());
96 
97  // First find the element the hit lands in
98  const Elem * elem = getLocalElemContainingPoint(p);
99 
100  if (elem)
101  {
102  // We have to pass a vector of points into reinitElemPhys
103  point_vec[0] = p;
104 
106  _subproblem.reinitElemPhys(elem, point_vec, 0); // Zero is for tid
107 
108  for (MooseIndex(_coupled_moose_vars) j = 0; j < _coupled_moose_vars.size(); ++j)
109  values[j] = (dynamic_cast<MooseVariable *>(_coupled_moose_vars[j]))->sln()[0] *
110  _pp_value; // The zero is for the "qp"
111 
112  _found_points[i] = true;
113  }
114  }
115  }
116 }
117 
118 void
120 {
121  // Save off for speed
122  const auto pid = processor_id();
123 
124  /*
125  * Figure out which processor is actually going "claim" each point.
126  * If multiple processors found the point and computed values what happens is that
127  * maxloc will give us the smallest PID in max_id
128  */
129  std::vector<unsigned int> max_id(_found_points.size());
130 
131  _communicator.maxloc(_found_points, max_id);
132 
133  for (MooseIndex(max_id) i = 0; i < max_id.size(); ++i)
134  {
135  // Only do this check on the proc zero because it's the same on every processor
136  // _found_points should contain all 1's at this point (ie every point was found by a proc)
137  if (pid == 0 && !_found_points[i])
138  mooseError("In ", name(), ", sample point not found: ", _points[i]);
139 
140  if (max_id[i] == pid)
142  }
143 
145 }
146 
147 void
148 PointSamplerBase::setPointsVector(const std::vector<Point> & points)
149 {
150  _points = points;
151 }
152 
153 void
154 PointSamplerBase::transferPointsVector(std::vector<Point> && points)
155 {
156  _points = std::move(points);
157 }
158 
159 const Elem *
161 {
162  const Elem * elem = (*_pl)(p);
163 
164  if (elem && elem->processor_id() == processor_id())
165  return elem;
166 
167  return nullptr;
168 }
Base class for VectorPostprocessors that need to do "sampling" of values in the domain.
Definition: SamplerBase.h:40
VarFieldType
Definition: MooseTypes.h:488
virtual void initialize()
Initialize the datastructures.
Definition: SamplerBase.C:76
std::vector< Real > _ids
The ID to use for each point (yes, this is Real on purpose)
void setPointsVector(const std::vector< Point > &points)
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.
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
void addMooseVariableDependency(MooseVariableFEBase *var)
Call this function to add the passed in MooseVariableFEBase as a variable that this object depends on...
SubProblem & _subproblem
Reference to the Subproblem for this user object.
Definition: UserObject.h:138
virtual void finalize()
Finalize.
std::vector< std::vector< Real > > _point_values
Vector of values per point.
MooseVariableFE< Real > * mooseVariable() const
Get the variable that this object is using.
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:52
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 >()
VarKindType
Framework-wide stuff.
Definition: MooseTypes.h:481
InputParameters validParams< PointSamplerBase >()
const Real & _pp_value
Postprocessor multiplying the variables.
virtual void execute()
Execute method.
void transferPointsVector(std::vector< Point > &&points)
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:92
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:62
std::vector< MooseVariableFEBase * > _coupled_moose_vars
Vector of all coupled variables.
Definition: Coupleable.h:740
Intermediate base class that ties together all the interfaces for getting MooseVariableFEBases with t...
InputParameters validParams< SamplerBase >()
Definition: SamplerBase.C:23
Interface for objects that need to get values of MooseVariables.
BoundingBox getInflatedProcessorBoundingBox(Real inflation_multiplier=0.01) const
Get a (slightly inflated) processor bounding box.
Definition: MooseMesh.C:2544
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
std::unique_ptr< PointLocatorBase > _pl
MooseMesh & _mesh
The Mesh we&#39;re using.
Definition: Moose.h:112
virtual std::unique_ptr< PointLocatorBase > getPointLocator() const
Proxy function to get a (sub)PointLocator from either the underlying libMesh mesh (default)...
Definition: MooseMesh.C:2730
virtual void reinitElemPhys(const Elem *elem, const std::vector< Point > &phys_points_in_elem, THREAD_ID tid, bool suppress_displaced_init=false)=0