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