Skip to content

Commit

Permalink
Factor out piecewise linear base class (idaholab#14220)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen authored and loganharbour committed Jan 16, 2020
1 parent 4814670 commit 6ac5968
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 63 deletions.
22 changes: 22 additions & 0 deletions framework/doc/content/source/functions/CoarsendPiecewiseLinear.md
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
26 changes: 26 additions & 0 deletions framework/include/functions/CoarsendPiecewiseLinear.h
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);
};
27 changes: 2 additions & 25 deletions framework/include/functions/PiecewiseLinear.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#pragma once

#include "PiecewiseBase.h"
#include "PiecewiseLinearBase.h"

// Forward declarations
class PiecewiseLinear;
Expand All @@ -21,33 +21,10 @@ InputParameters validParams<PiecewiseLinear>();
* Function which provides a piecewise continuous linear interpolation
* of a provided (x,y) point data set.
*/
class PiecewiseLinear : public PiecewiseBase
class PiecewiseLinear : public PiecewiseLinearBase
{
public:
static InputParameters validParams();

PiecewiseLinear(const InputParameters & parameters);

/// helper object to perform the linear interpolation of the function data
std::unique_ptr<LinearInterpolation> _linear_interp;

/**
* 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;
};
57 changes: 57 additions & 0 deletions framework/include/functions/PiecewiseLinearBase.h
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;
};
28 changes: 28 additions & 0 deletions framework/src/functions/CoarsendPiecewiseLinear.C
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();
}
42 changes: 4 additions & 38 deletions framework/src/functions/PiecewiseLinear.C
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,13 @@ defineLegacyParams(PiecewiseLinear);
InputParameters
PiecewiseLinear::validParams()
{
InputParameters params = PiecewiseBase::validParams();
InputParameters params = PiecewiseLinearBase::validParams();
params.addClassDescription("Linearly interpolates between pairs of x-y data");
return params;
}

PiecewiseLinear::PiecewiseLinear(const InputParameters & parameters) : PiecewiseBase(parameters)
PiecewiseLinear::PiecewiseLinear(const InputParameters & parameters)
: PiecewiseLinearBase(parameters)
{
// try building a linear interpolation object
try
{
_linear_interp = libmesh_make_unique<LinearInterpolation>(_raw_x, _raw_y);
}
catch (std::domain_error & e)
{
mooseError("In PiecewiseLinear ", _name, ": ", e.what());
}
}

Real
PiecewiseLinear::value(Real t, const Point & p) const
{
const Real x = _has_axis ? p(_axis) : t;
return _scale_factor * _linear_interp->sample(x);
}

Real
PiecewiseLinear::timeDerivative(Real t, const Point & p) const
{
const Real x = _has_axis ? p(_axis) : t;
return _scale_factor * _linear_interp->sampleDerivative(x);
}

Real
PiecewiseLinear::integral() const
{
return _scale_factor * _linear_interp->integrate();
}

Real
PiecewiseLinear::average() const
{
return integral() /
(_linear_interp->domain(_linear_interp->getSampleSize() - 1) - _linear_interp->domain(0));
buildInterpolation();
}
72 changes: 72 additions & 0 deletions framework/src/functions/PiecewiseLinearBase.C
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));
}

0 comments on commit 6ac5968

Please sign in to comment.