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