www.mooseframework.org
FindValueOnLine.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 "FindValueOnLine.h"
16 
17 // MOOSE includes
18 #include "MooseMesh.h"
19 #include "MooseUtils.h"
20 #include "MooseVariable.h"
21 
22 template <>
25 {
27  params.addClassDescription("Find a specific target value along a sampling line. The variable "
28  "values along the line should change monotonically. The target value "
29  "is searched using a bisection algorithm.");
30  params.addParam<Point>("start_point", "Start point of the sampling line.");
31  params.addParam<Point>("end_point", "End point of the sampling line.");
32  params.addParam<Real>("target", "Target value to locate.");
33  params.addParam<unsigned int>("depth", 36, "Maximum number of bisections to perform.");
34  params.addParam<Real>(
35  "tol",
36  1e-10,
37  "Stop search if a value is found that is equal to the target with this tolerance applied.");
38  params.addCoupledVar("v", "Variable to inspect");
39  return params;
40 }
41 
43  : GeneralPostprocessor(parameters),
44  Coupleable(this, false),
45  _start_point(getParam<Point>("start_point")),
46  _end_point(getParam<Point>("end_point")),
47  _length((_end_point - _start_point).norm()),
48  _target(getParam<Real>("target")),
49  _depth(getParam<unsigned int>("depth")),
50  _tol(getParam<Real>("tol")),
51  _coupled_var(getVar("v", 0)),
52  _position(0.0),
53  _mesh(_subproblem.mesh()),
54  _point_vec(1)
55 {
56 }
57 
58 void
60 {
61  // We do this here just in case it's been destroyed and recreated becaue of mesh adaptivity.
63  _pl->enable_out_of_mesh_mode();
64 }
65 
66 void
68 {
69  Real s;
70  Real s_left = 0.0;
71  Real left = getValueAtPoint(_start_point);
72  Real s_right = 1.0;
73  Real right = getValueAtPoint(_end_point);
74 
79  bool left_to_right = left < right;
80  // Initial bounds check
81  if ((left_to_right && _target < left) || (!left_to_right && _target < right))
82  mooseError("Target value \"",
83  _target,
84  "\" is less than the minimum sampled value \"",
85  std::min(left, right),
86  "\"");
87  if ((left_to_right && _target > right) || (!left_to_right && _target > left))
88  mooseError("Target value \"",
89  _target,
90  "\" is greater than the maximum sampled value \"",
91  std::max(left, right),
92  "\"");
93 
94  bool found_it = false;
95  Real value = 0;
96  for (auto i = decltype(_depth)(0); i < _depth; ++i)
97  {
98  // find midpoint
99  s = (s_left + s_right) / 2.0;
100  Point p = s * (_end_point - _start_point) + _start_point;
101 
102  // sample value
103  value = getValueAtPoint(p);
104 
105  // have we hit the target value yet?
107  {
108  found_it = true;
109  break;
110  }
111 
112  // bisect
113  if ((left_to_right && _target < value) || (!left_to_right && _target > value))
114  // to the left
115  s_right = s;
116  else
117  // to the right
118  s_left = s;
119  }
120 
121  if (!found_it)
122  mooseError("Target value \"",
123  std::setprecision(10),
124  _target,
125  "\" not found on line within tolerance, last sample: ",
126  value,
127  ".");
128 
129  _position = s * _length;
130 }
131 
132 Real
134 {
135  const Elem * elem = (*_pl)(p);
136 
137  processor_id_type elem_proc_id = elem ? elem->processor_id() : DofObject::invalid_processor_id;
138  _communicator.min(elem_proc_id);
139 
140  if (elem_proc_id == DofObject::invalid_processor_id)
141  {
142  // there is no element
143  mooseError("No element found at the current search point. Please make sure the sampling line "
144  "stays inside the mesh completely.");
145  }
146 
147  Real value = 0;
148 
149  if (elem)
150  {
151  if (elem->processor_id() == processor_id())
152  {
153  // element is local
154  _point_vec[0] = p;
156  value = _coupled_var->sln()[0];
157  }
158  }
159 
160  // broadcast value
161  _communicator.broadcast(value, elem_proc_id);
162  return value;
163 }
164 
167 {
168  return _position;
169 }
const Point _start_point
line to sample along
virtual void execute() override
Execute method.
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
const Real _tol
tolerance for comparison to the target value
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual PostprocessorValue getValue() override
This will get called to actually grab the final value the postprocessor has calculated.
InputParameters validParams< GeneralPostprocessor >()
This class is here to combine the Postprocessor interface and the base class Postprocessor object alo...
const Real _length
SubProblem & _subproblem
Reference to the Subproblem for this user object.
Definition: UserObject.h:146
FindValueOnLine(const InputParameters &parameters)
Real getValueAtPoint(const Point &p)
std::unique_ptr< PointLocatorBase > _pl
helper object to locate elements containing points
Real PostprocessorValue
MOOSE typedefs.
Definition: MooseTypes.h:73
const Point _end_point
MooseMesh & _mesh
The Mesh we&#39;re using.
const Real _target
value to find along the line
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
void addCoupledVar(const std::string &name, const std::string &doc_string)
This method adds a coupled variable name pair.
MooseVariable * _coupled_var
coupled variable
Interface for objects that needs coupling capabilities.
Definition: Coupleable.h:35
InputParameters validParams< FindValueOnLine >()
const unsigned int _depth
search depth
const VariableValue & sln()
std::vector< Point > _point_vec
So we don&#39;t have to create and destroy the dummy vector.
bool absoluteFuzzyEqual(const libMesh::Real &var1, const libMesh::Real &var2, const libMesh::Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
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...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
Real _position
detected interface location