forked from idaholab/moose
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out piecewise linear base class (idaholab#14220)
- Loading branch information
Showing
7 changed files
with
211 additions
and
63 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
framework/doc/content/source/functions/CoarsendPiecewiseLinear.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# PiecewiseLinear | ||
|
||
!syntax description /Functions/CoarsenedPiecewiseLinear | ||
|
||
## Description | ||
|
||
The `CoarsenedPiecewiseLinear` performs preprocessing and linear interpolation | ||
on an x/y data set. The object acts like | ||
[`PiecewiseLinear`](/PiecewiseLinear.md) except that it reduces the number of | ||
function point at the start of the simulation. It uses the | ||
[Ramer-Douglas-Peucker algorithm](https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm) | ||
for data reduction. | ||
|
||
## Example Input Syntax | ||
|
||
!listing test/tests/misc/check_error/function_file_test1.i block=Functions | ||
|
||
!syntax parameters /Functions/CoarsenedPiecewiseLinear | ||
|
||
!syntax inputs /Functions/CoarsenedPiecewiseLinear | ||
|
||
!syntax children /Functions/CoarsenedPiecewiseLinear |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//* This file is part of the MOOSE framework | ||
//* https://www.mooseframework.org | ||
//* | ||
//* All rights reserved, see COPYRIGHT for full restrictions | ||
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT | ||
//* | ||
//* Licensed under LGPL 2.1, please see LICENSE for details | ||
//* https://www.gnu.org/licenses/lgpl-2.1.html | ||
|
||
#pragma once | ||
|
||
#include "PiecewiseLinearBase.h" | ||
|
||
class CoarsendPiecewiseLinear; | ||
|
||
template <> | ||
InputParameters validParams<CoarsendPiecewiseLinear>(); | ||
|
||
/** | ||
* Perform a point reduction of the tabulated data upon initialization. | ||
*/ | ||
class CoarsendPiecewiseLinear : public PiecewiseLinearBase | ||
{ | ||
public: | ||
CoarsendPiecewiseLinear(const InputParameters & parameters); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//* This file is part of the MOOSE framework | ||
//* https://www.mooseframework.org | ||
//* | ||
//* All rights reserved, see COPYRIGHT for full restrictions | ||
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT | ||
//* | ||
//* Licensed under LGPL 2.1, please see LICENSE for details | ||
//* https://www.gnu.org/licenses/lgpl-2.1.html | ||
|
||
#pragma once | ||
|
||
#include "PiecewiseBase.h" | ||
|
||
// Forward declarations | ||
class PiecewiseLinearBase; | ||
|
||
template <> | ||
InputParameters validParams<PiecewiseLinearBase>(); | ||
|
||
/** | ||
* Function which provides a piecewise continuous linear interpolation | ||
* of a provided (x,y) point data set. | ||
*/ | ||
class PiecewiseLinearBase : public PiecewiseBase | ||
{ | ||
public: | ||
PiecewiseLinearBase(const InputParameters & parameters); | ||
|
||
virtual void initialSetup() override; | ||
|
||
/** | ||
* Get the value of the function (based on time only) | ||
* \param t The time | ||
* \param pt The point in space (x,y,z) (unused) | ||
* \return The value of the function at the specified time | ||
*/ | ||
virtual Real value(Real t, const Point & pt) const override; | ||
|
||
/** | ||
* Get the time derivative of the function (based on time only) | ||
* \param t The time | ||
* \param pt The point in space (x,y,z) (unused) | ||
* \return The time derivative of the function at the specified time | ||
*/ | ||
virtual Real timeDerivative(Real t, const Point & pt) const override; | ||
|
||
virtual Real integral() const override; | ||
|
||
virtual Real average() const override; | ||
|
||
protected: | ||
/// build the linear interpolation object from the x/y data | ||
void buildInterpolation(); | ||
|
||
/// helper object to perform the linear interpolation of the function data | ||
std::unique_ptr<LinearInterpolation> _linear_interp; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//* This file is part of the MOOSE framework | ||
//* https://www.mooseframework.org | ||
//* | ||
//* All rights reserved, see COPYRIGHT for full restrictions | ||
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT | ||
//* | ||
//* Licensed under LGPL 2.1, please see LICENSE for details | ||
//* https://www.gnu.org/licenses/lgpl-2.1.html | ||
|
||
#include "CoarsendPiecewiseLinear.h" | ||
|
||
registerMooseObject("MooseApp", CoarsendPiecewiseLinear); | ||
|
||
template <> | ||
InputParameters | ||
validParams<CoarsendPiecewiseLinear>() | ||
{ | ||
InputParameters params = validParams<PiecewiseLinearBase>(); | ||
params.addClassDescription("Perform a point reduction of the tabulated data upon initialization, " | ||
"then evaluate using a linear interpolation."); | ||
return params; | ||
} | ||
|
||
CoarsendPiecewiseLinear::CoarsendPiecewiseLinear(const InputParameters & parameters) | ||
: PiecewiseLinearBase(parameters) | ||
{ | ||
buildInterpolation(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//* This file is part of the MOOSE framework | ||
//* https://www.mooseframework.org | ||
//* | ||
//* All rights reserved, see COPYRIGHT for full restrictions | ||
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT | ||
//* | ||
//* Licensed under LGPL 2.1, please see LICENSE for details | ||
//* https://www.gnu.org/licenses/lgpl-2.1.html | ||
|
||
#include "PiecewiseLinearBase.h" | ||
|
||
template <> | ||
InputParameters | ||
validParams<PiecewiseLinearBase>() | ||
{ | ||
InputParameters params = validParams<PiecewiseBase>(); | ||
params.addClassDescription("Linearly interpolates between pairs of x-y data"); | ||
return params; | ||
} | ||
|
||
PiecewiseLinearBase::PiecewiseLinearBase(const InputParameters & parameters) | ||
: PiecewiseBase(parameters), _linear_interp(nullptr) | ||
{ | ||
} | ||
|
||
void | ||
PiecewiseLinearBase::initialSetup() | ||
{ | ||
if (!_linear_interp) | ||
mooseError("Classes derived from PiecewiseLinearBase need to call buildInterpolation()"); | ||
} | ||
|
||
void | ||
PiecewiseLinearBase::buildInterpolation() | ||
{ | ||
// try building a linear interpolation object | ||
try | ||
{ | ||
_linear_interp = libmesh_make_unique<LinearInterpolation>(_raw_x, _raw_y); | ||
} | ||
catch (std::domain_error & e) | ||
{ | ||
mooseError("In PiecewiseLinearBase ", _name, ": ", e.what()); | ||
} | ||
} | ||
|
||
Real | ||
PiecewiseLinearBase::value(Real t, const Point & p) const | ||
{ | ||
const Real x = _has_axis ? p(_axis) : t; | ||
return _scale_factor * _linear_interp->sample(x); | ||
} | ||
|
||
Real | ||
PiecewiseLinearBase::timeDerivative(Real t, const Point & p) const | ||
{ | ||
const Real x = _has_axis ? p(_axis) : t; | ||
return _scale_factor * _linear_interp->sampleDerivative(x); | ||
} | ||
|
||
Real | ||
PiecewiseLinearBase::integral() const | ||
{ | ||
return _scale_factor * _linear_interp->integrate(); | ||
} | ||
|
||
Real | ||
PiecewiseLinearBase::average() const | ||
{ | ||
return integral() / | ||
(_linear_interp->domain(_linear_interp->getSampleSize() - 1) - _linear_interp->domain(0)); | ||
} |