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.
Add point reduction algorithm (idaholab#14220)
- Loading branch information
Showing
7 changed files
with
275 additions
and
5 deletions.
There are no files selected for viewing
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
14 changes: 14 additions & 0 deletions
14
framework/doc/content/source/vectorpostprocessors/PiecewiseFunctionTabulate.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,14 @@ | ||
# PiecewiseFunctionTabulate | ||
|
||
!syntax description /VectorPostprocessors/PiecewiseFunctionTabulate | ||
|
||
This object can be used to check the function generated by a | ||
[`CoarsendPicewiseLinear`](/CoarsendPicewiseLinear.md) function. | ||
|
||
!syntax parameters /VectorPostprocessors/PiecewiseFunctionTabulate | ||
|
||
!syntax inputs /VectorPostprocessors/PiecewiseFunctionTabulate | ||
|
||
!syntax children /VectorPostprocessors/PiecewiseFunctionTabulate | ||
|
||
!bibtex bibliography |
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,29 @@ | ||
//* 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 "Moose.h" | ||
#include <vector> | ||
|
||
namespace PointReduction | ||
{ | ||
|
||
typedef std::pair<Real, Real> FunctionNode; | ||
typedef std::vector<FunctionNode> FunctionNodeList; | ||
|
||
/// compute the perpendicular distance of a point from a line defined by begin and end points | ||
Real perpendicularDistance(const FunctionNode & point, | ||
const FunctionNode & begin, | ||
const FunctionNode & end); | ||
|
||
/// return a pruned function node list using the Ramer-Douglas-Peucker algorithm | ||
FunctionNodeList douglasPeucker(const FunctionNodeList &, Real epsilon); | ||
|
||
} // namespace PointReduction |
37 changes: 37 additions & 0 deletions
37
framework/include/vectorpostprocessors/PiecewiseFunctionTabulate.h
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,37 @@ | ||
//* 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 "GeneralVectorPostprocessor.h" | ||
|
||
class PiecewiseFunctionTabulate; | ||
class PiecewiseBase; | ||
|
||
template <> | ||
InputParameters validParams<PiecewiseFunctionTabulate>(); | ||
|
||
/** | ||
* Tabulate the function nodes of a piecewise function, such as PiecewiseLinear or | ||
* PiecewiseConstant | ||
*/ | ||
class PiecewiseFunctionTabulate : public GeneralVectorPostprocessor | ||
{ | ||
public: | ||
PiecewiseFunctionTabulate(const InputParameters & parameters); | ||
|
||
virtual void initialize() override {} | ||
virtual void execute() override; | ||
virtual void finalize() override {} | ||
|
||
protected: | ||
const PiecewiseBase * _piecewise_function; | ||
VectorPostprocessorValue & _x_col; | ||
VectorPostprocessorValue & _y_col; | ||
}; |
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,98 @@ | ||
//* 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 "PointReduction.h" | ||
#include "MooseError.h" | ||
|
||
#include <algorithm> | ||
#include <cmath> | ||
|
||
namespace PointReduction | ||
{ | ||
|
||
Real | ||
sqr(Real a) | ||
{ | ||
return a * a; | ||
} | ||
|
||
Real | ||
perpendicularDistance(const FunctionNode & node, | ||
const FunctionNode & begin, | ||
const FunctionNode & end) | ||
{ | ||
const Real x0 = node.first; | ||
const Real y0 = node.second; | ||
const Real x1 = begin.first; | ||
const Real y1 = begin.second; | ||
const Real x2 = end.first; | ||
const Real y2 = end.second; | ||
|
||
const Real denom = std::sqrt(sqr(y2 - y1) + sqr(x2 - x1)); | ||
mooseAssert(denom > 0, "Line begin and end points bust not be the same"); | ||
|
||
return std::abs((y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1) / denom; | ||
} | ||
|
||
void | ||
douglasPeuckerRecurse(const FunctionNodeList & list, | ||
const Real epsilon, | ||
std::vector<bool> & keep, | ||
std::size_t begin, | ||
std::size_t end) | ||
{ | ||
// Find the point with the maximum distance | ||
Real dmax = 0.0; | ||
std::size_t index = 0; | ||
|
||
for (std::size_t i = begin; i <= end; ++i) | ||
if (keep[i]) | ||
{ | ||
const Real d = perpendicularDistance(list[i], list[begin], list[end]); | ||
if (d > dmax) | ||
{ | ||
index = i; | ||
dmax = d; | ||
} | ||
} | ||
|
||
// If max distance is greater than epsilon, recursively simplify | ||
if (dmax > epsilon) | ||
{ | ||
// Recursive call | ||
douglasPeuckerRecurse(list, epsilon, keep, begin, index); | ||
douglasPeuckerRecurse(list, epsilon, keep, index, end); | ||
} | ||
else | ||
{ | ||
// remove all points between begin and end | ||
for (std::size_t i = begin + 1; i < end; ++i) | ||
keep[i] = false; | ||
} | ||
} | ||
|
||
FunctionNodeList | ||
douglasPeucker(const FunctionNodeList & list, const Real epsilon) | ||
{ | ||
// set up keep list for function nodes | ||
std::vector<bool> keep(list.size(), true); | ||
douglasPeuckerRecurse(list, epsilon, keep, 0, list.size() - 1); | ||
|
||
FunctionNodeList result; | ||
result.reserve(std::count_if(keep.begin(), keep.end(), [](bool k) { return k; })); | ||
|
||
/// filter result | ||
for (std::size_t i = 0; i < list.size(); ++i) | ||
if (keep[i]) | ||
result.push_back(list[i]); | ||
|
||
return result; | ||
} | ||
|
||
} // namespace PointReduction |
53 changes: 53 additions & 0 deletions
53
framework/src/vectorpostprocessors/PiecewiseFunctionTabulate.C
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,53 @@ | ||
//* 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 "PiecewiseFunctionTabulate.h" | ||
#include "PiecewiseBase.h" | ||
|
||
registerMooseObject("MooseApp", PiecewiseFunctionTabulate); | ||
|
||
template <> | ||
InputParameters | ||
validParams<PiecewiseFunctionTabulate>() | ||
{ | ||
InputParameters params = validParams<GeneralVectorPostprocessor>(); | ||
params.addClassDescription("Tabulate the function nodes of a piecewise function, such as " | ||
"PiecewiseLinear or PiecewiseConstant"); | ||
params.addRequiredParam<FunctionName>( | ||
"function", "Name of the piecewise function object to extract the time steps from"); | ||
return params; | ||
} | ||
|
||
PiecewiseFunctionTabulate::PiecewiseFunctionTabulate(const InputParameters & parameters) | ||
: GeneralVectorPostprocessor(parameters), | ||
_piecewise_function(dynamic_cast<PiecewiseBase *>( | ||
&_fe_problem.getFunction(getParam<FunctionName>("function"), | ||
isParamValid("_tid") ? getParam<THREAD_ID>("_tid") : 0))), | ||
_x_col(declareVector("x")), | ||
_y_col(declareVector("y")) | ||
{ | ||
if (!_piecewise_function) | ||
paramError("function", | ||
"The supplied function must be derived from PiecewiseBase (e.g. PiecewiseLinear or " | ||
"PiecewiseConstant)"); | ||
} | ||
|
||
void | ||
PiecewiseFunctionTabulate::execute() | ||
{ | ||
auto size = _piecewise_function->functionSize(); | ||
_x_col.resize(size); | ||
_y_col.resize(size); | ||
|
||
for (std::size_t i = 0; i < size; ++i) | ||
{ | ||
_x_col[i] = _piecewise_function->domain(i); | ||
_y_col[i] = _piecewise_function->range(i); | ||
} | ||
} |