www.mooseframework.org
LineValueSampler.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 "LineValueSampler.h"
11 #include <limits>
12 #include "libmesh/utility.h"
13 
15 
17 
20 {
22 
23  params.addRequiredParam<Point>("start_point", "The beginning of the line");
24  params.addRequiredParam<Point>("end_point", "The ending of the line");
25 
26  params.addRequiredParam<unsigned int>("num_points",
27  "The number of points to sample along the line");
28 
29  params.addClassDescription("Samples variable(s) along a specified line");
30 
31  return params;
32 }
33 
35  : PointSamplerBase(parameters),
36  _start_point(getParam<Point>("start_point")),
37  _end_point(getParam<Point>("end_point")),
38  _num_points(
39  declareRestartableData<unsigned int>("num_points", getParam<unsigned int>("num_points"))),
40  _line_vector(_end_point - _start_point),
41  _line_vector_norm(_line_vector.norm())
42 {
44  mooseError("LineValueSampler: `start_point` and `end_point` must be different.");
45 
47 }
48 
49 void
50 LineValueSampler::generatePointsAndIDs(const Point & start_point,
51  const Point & end_point,
52  unsigned int num_points,
53  std::vector<Point> & points,
54  std::vector<Real> & ids)
55 {
56 
57  Point difference = end_point - start_point;
58 
59  Point delta = difference / Real(num_points - 1);
60 
61  points.resize(num_points);
62  ids.resize(num_points);
63 
64  for (unsigned int i = 0; i < num_points - 1;
65  i++) // -1 so that we can manually put in the end point to get it perfect
66  {
67  Point p = start_point + (i * delta);
68 
69  points[i] = p;
70  ids[i] = (p - start_point).norm(); // The ID is the distance along the line
71  }
72 
73  // Add the end point explicitly
74  points[num_points - 1] = end_point;
75  ids[num_points - 1] = (end_point - start_point).norm();
76 }
77 
78 Real
80 {
81  if (_values.size() != 1)
82  mooseError("LineValueSampler: When calling getValue() on LineValueSampler, "
83  "only one variable can be provided as input to LineValueSampler.");
84 
85  // Check if vectors are sorted by id
86  if (_sort_by != 3)
87  mooseError("LineValueSampler: When calling getValue() on LineValueSampler, "
88  "`sort_by` should be set to `id`.");
89 
90  Real value = std::numeric_limits<Real>::infinity();
91 
92  // Project point onto the line segment and normalize by length of line segment
93  Real position =
94  (p - _points[0]) * (_points.back() - _points[0]) / Utility::pow<2>(_line_vector_norm);
95 
96  if (position >= 0.0 and position <= 1.0)
97  {
98  unsigned int vec_pos =
99  std::lower_bound(_id.begin(), _id.end(), position * _line_vector_norm) - _id.begin();
100 
101  VectorPostprocessorValue & value_vector =
103 
104  if (MooseUtils::absoluteFuzzyEqual(_id[vec_pos], position * _line_vector_norm))
105  value = value_vector[vec_pos];
106  else
107  value = (value_vector[vec_pos - 1] + value_vector[vec_pos]) * 0.5;
108  }
109 
110  return value;
111 }
LineValueSampler::_end_point
const Point _end_point
Definition: LineValueSampler.h:57
MooseVariableInterface< Real >::value
virtual const OutputTools< Real >::VariableValue & value()
The value of the variable this object is operating on.
Definition: MooseVariableInterface.C:77
PointSamplerBase::validParams
static InputParameters validParams()
Definition: PointSamplerBase.C:21
PointSamplerBase
Definition: PointSamplerBase.h:24
MooseObject::mooseError
void mooseError(Args &&... args) const
Definition: MooseObject.h:141
SamplerBase::_values
std::vector< VectorPostprocessorValue * > _values
Definition: SamplerBase.h:122
LineValueSampler::LineValueSampler
LineValueSampler(const InputParameters &parameters)
Definition: LineValueSampler.C:34
LineValueSampler::_num_points
unsigned int & _num_points
Definition: LineValueSampler.h:59
LineValueSampler::validParams
static InputParameters validParams()
Definition: LineValueSampler.C:19
LineValueSampler.h
registerMooseObject
registerMooseObject("MooseApp", LineValueSampler)
InputParameters
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
Definition: InputParameters.h:53
SamplerBase::_variable_names
std::vector< std::string > _variable_names
The variable names.
Definition: SamplerBase.h:107
LineValueSampler::generatePointsAndIDs
static void generatePointsAndIDs(const Point &start_point, const Point &end_point, unsigned int num_points, std::vector< Point > &points, std::vector< Real > &ids)
Helper function to generate the list of points along a line and a unique ID for each point.
Definition: LineValueSampler.C:50
PointSamplerBase::_ids
std::vector< Real > _ids
The ID to use for each point (yes, this is Real on purpose)
Definition: PointSamplerBase.h:61
SamplerBase::_sort_by
const unsigned int _sort_by
What to sort by.
Definition: SamplerBase.h:110
FEProblemBase::getVectorPostprocessorValue
VectorPostprocessorValue & getVectorPostprocessorValue(const VectorPostprocessorName &name, const std::string &vector_name)
DEPRECATED: Use the new version where you need to specify whether or not the vector must be broadcast...
Definition: FEProblemBase.C:3252
InputParameters::addClassDescription
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.
Definition: InputParameters.C:70
VectorPostprocessorValue
std::vector< Real > VectorPostprocessorValue
Definition: MooseTypes.h:190
PointSamplerBase::_points
std::vector< Point > _points
The points to evaluate at.
Definition: PointSamplerBase.h:58
MooseUtils::absoluteFuzzyEqual
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:234
LineValueSampler
Definition: LineValueSampler.h:21
LineValueSampler::getValue
Real getValue(Point p) const
Gets the value of the variable at a point p.
Definition: LineValueSampler.C:79
LineValueSampler::_line_vector_norm
const Real _line_vector_norm
Length of line segment.
Definition: LineValueSampler.h:68
VectorPostprocessor::_vpp_fe_problem
FEProblemBase * _vpp_fe_problem
Pointer to FEProblemBase.
Definition: VectorPostprocessor.h:69
SamplerBase::_id
VectorPostprocessorValue & _id
The node ID of each point.
Definition: SamplerBase.h:120
VectorPostprocessor::_vpp_name
std::string _vpp_name
The name of the VectorPostprocessor.
Definition: VectorPostprocessor.h:66
InputParameters::addRequiredParam
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...
Definition: InputParameters.h:1176
defineLegacyParams
defineLegacyParams(LineValueSampler)
LineValueSampler::_start_point
const Point _start_point
Definition: LineValueSampler.h:56