Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 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 : 14 : registerMooseObject("MooseApp", LineValueSampler); 15 : 16 : InputParameters 17 29530 : LineValueSampler::validParams() 18 : { 19 29530 : InputParameters params = SpatialUserObjectFunctor<PointVariableSamplerBase>::validParams(); 20 : 21 29530 : params.addRequiredParam<Point>("start_point", "The beginning of the line"); 22 29530 : params.addRequiredParam<Point>("end_point", "The ending of the line"); 23 : 24 29530 : params.addRequiredParam<unsigned int>("num_points", 25 : "The number of points to sample along the line"); 26 : 27 29530 : params.addClassDescription("Samples variable(s) along a specified line"); 28 : 29 29530 : return params; 30 0 : } 31 : 32 501 : LineValueSampler::LineValueSampler(const InputParameters & parameters) 33 : : SpatialUserObjectFunctor<PointVariableSamplerBase>(parameters), 34 501 : _start_point(getParam<Point>("start_point")), 35 501 : _end_point(getParam<Point>("end_point")), 36 501 : _num_points( 37 501 : declareRestartableData<unsigned int>("num_points", getParam<unsigned int>("num_points"))), 38 501 : _line_vector(_end_point - _start_point), 39 501 : _line_vector_norm(_line_vector.norm()), 40 1503 : _vpp_value(getVectorPostprocessorValueByName(_vpp_name, _variable_names[0])) 41 : { 42 501 : if (MooseUtils::absoluteFuzzyEqual(_line_vector_norm, 0.0)) 43 0 : mooseError("LineValueSampler: `start_point` and `end_point` must be different."); 44 : 45 501 : generatePointsAndIDs(_start_point, _end_point, _num_points, _points, _ids); 46 501 : } 47 : 48 : void 49 869 : LineValueSampler::generatePointsAndIDs(const Point & start_point, 50 : const Point & end_point, 51 : unsigned int num_points, 52 : std::vector<Point> & points, 53 : std::vector<Real> & ids) 54 : { 55 : 56 869 : Point difference = end_point - start_point; 57 : 58 869 : Point delta = difference / Real(num_points - 1); 59 : 60 869 : points.resize(num_points); 61 869 : ids.resize(num_points); 62 : 63 12539 : for (unsigned int i = 0; i < num_points - 1; 64 : i++) // -1 so that we can manually put in the end point to get it perfect 65 : { 66 11670 : Point p = start_point + (i * delta); 67 : 68 11670 : points[i] = p; 69 11670 : ids[i] = (p - start_point).norm(); // The ID is the distance along the line 70 : } 71 : 72 : // Add the end point explicitly 73 869 : points[num_points - 1] = end_point; 74 869 : ids[num_points - 1] = (end_point - start_point).norm(); 75 869 : } 76 : 77 : Real 78 0 : LineValueSampler::getValue(const Point & p) const 79 : { 80 0 : if (_values.size() != 1) 81 0 : mooseError("LineValueSampler: When calling getValue() on LineValueSampler, " 82 : "only one variable can be provided as input to LineValueSampler."); 83 : 84 : // Check if vectors are sorted by id 85 0 : if (_sort_by != 3) 86 0 : mooseError("LineValueSampler: When calling getValue() on LineValueSampler, " 87 : "`sort_by` should be set to `id`."); 88 : 89 0 : Real value = std::numeric_limits<Real>::infinity(); 90 : 91 : // Project point onto the line segment and normalize by length of line segment 92 : Real position = 93 0 : (p - _points[0]) * (_points.back() - _points[0]) / Utility::pow<2>(_line_vector_norm); 94 : 95 0 : if (position >= 0.0 and position <= 1.0) 96 : { 97 : unsigned int vec_pos = 98 0 : std::lower_bound(_id.begin(), _id.end(), position * _line_vector_norm) - _id.begin(); 99 : 100 0 : if (MooseUtils::absoluteFuzzyEqual(_id[vec_pos], position * _line_vector_norm)) 101 0 : value = _vpp_value[vec_pos]; 102 : else 103 : { 104 0 : mooseWarning("Value requested outside of sampled points"); 105 0 : value = (_vpp_value[vec_pos - 1] + _vpp_value[vec_pos]) * 0.5; 106 : } 107 : } 108 : 109 0 : return value; 110 : }