Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion Applications/ApplicationsLib/ProjectData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "BaseLib/FileTools.h"
#include "BaseLib/uniqueInsert.h"

#include "MathLib/InterpolationAlgorithms/PiecewiseLinearInterpolation.h"
#include "MeshLib/Mesh.h"

#include "NumLib/ODESolver/TimeDiscretizationBuilder.h"
Expand Down Expand Up @@ -72,8 +73,10 @@ ProjectData::ProjectData(BaseLib::ConfigTree const& project_config,
}
_mesh_vec.push_back(mesh);

// process variables
// curves
parseCurves(project_config.getConfSubtreeOptional("curves"));

// process variables
parseProcessVariables(project_config.getConfSubtree("process_variables"));

// parameters
Expand Down Expand Up @@ -352,3 +355,42 @@ void ProjectData::parseNonlinearSolvers(BaseLib::ConfigTree const& config)
"The nonlinear solver name is not unique");
}
}

static std::unique_ptr<MathLib::PiecewiseLinearInterpolation>
createPiecewiseLinearInterpolation(BaseLib::ConfigTree const& config)
{
auto coords = config.getConfParam<std::vector<double>>("coords");
auto values = config.getConfParam<std::vector<double>>("values");
if (coords.empty() || values.empty())
{
ERR("The given co-ordinates or values vector is empty.");
std::abort();
}
if (coords.size() != values.size())
{
ERR("The given co-ordinates and values vector sizes are different.");
std::abort();
}

return std::unique_ptr<MathLib::PiecewiseLinearInterpolation>{
new MathLib::PiecewiseLinearInterpolation{std::move(coords),
std::move(values)}};
}

void ProjectData::parseCurves(
boost::optional<BaseLib::ConfigTree> const& config)
{
if (!config) return;

DBUG("Reading curves configuration.");

for (auto conf : config->getConfSubtreeList("curve"))
{
auto const name = conf.getConfParam<std::string>("name");
BaseLib::insertIfKeyUniqueElseError(
_curves,
name,
createPiecewiseLinearInterpolation(conf),
"The curve name is not unique.");
}
}
10 changes: 10 additions & 0 deletions Applications/ApplicationsLib/ProjectData.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
#ifndef PROJECTDATA_H_
#define PROJECTDATA_H_

#include <map>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting that we didn't have that include before...
Same for string below.

#include <memory>
#include <string>
#include <boost/optional/optional.hpp>
Copy link
Copy Markdown
Collaborator

@chleh chleh Apr 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that already comes in via ConfigTree. (probably also both string and map)


#include "BaseLib/ConfigTree.h"

Expand All @@ -26,6 +29,9 @@

#include "NumLib/ODESolver/Types.h"

namespace MathLib {
class PiecewiseLinearInterpolation;
}
namespace MeshLib {
class Mesh;
}
Expand Down Expand Up @@ -193,6 +199,8 @@ class ProjectData final

void parseNonlinearSolvers(BaseLib::ConfigTree const& config);

void parseCurves(boost::optional<BaseLib::ConfigTree> const& config);

private:
GeoLib::GEOObjects *_geoObjects = new GeoLib::GEOObjects();
std::vector<MeshLib::Mesh*> _mesh_vec;
Expand All @@ -216,6 +224,8 @@ class ProjectData final

using NonlinearSolver = NumLib::NonlinearSolverBase<GlobalMatrix, GlobalVector>;
std::map<std::string, std::unique_ptr<NonlinearSolver> > _nonlinear_solvers;
std::map<std::string,
std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> _curves;
};

#endif //PROJECTDATA_H_
33 changes: 7 additions & 26 deletions MathLib/InterpolationAlgorithms/PiecewiseLinearInterpolation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,27 @@
*/
#include <cmath>
#include <limits>
#include <utility>

#include "PiecewiseLinearInterpolation.h"

#include "BaseLib/quicksort.h"

namespace MathLib
{
PiecewiseLinearInterpolation::PiecewiseLinearInterpolation(const std::vector<double>& supporting_points,
const std::vector<double>& values_at_supp_pnts,
bool supp_pnts_sorted) :
_supp_pnts(supporting_points), _values_at_supp_pnts(values_at_supp_pnts)
PiecewiseLinearInterpolation::PiecewiseLinearInterpolation(
std::vector<double>&& supporting_points,
std::vector<double>&& values_at_supp_pnts,
bool supp_pnts_sorted)
: _supp_pnts(std::move(supporting_points)),
_values_at_supp_pnts(std::move(values_at_supp_pnts))
{
if (!supp_pnts_sorted) {
BaseLib::quicksort<double, double>(_supp_pnts, static_cast<std::size_t> (0),
_supp_pnts.size(), _values_at_supp_pnts);
}
}

PiecewiseLinearInterpolation::PiecewiseLinearInterpolation(const std::vector<double>& supporting_points,
const std::vector<double>& values_at_supp_pnts,
const std::vector<double>& points_to_interpolate,
std::vector<double>& values_at_interpol_pnts,
bool supp_pnts_sorted) :
_supp_pnts(supporting_points), _values_at_supp_pnts(values_at_supp_pnts)
{
if (!supp_pnts_sorted) {
BaseLib::quicksort<double, double>(_supp_pnts, static_cast<std::size_t> (0),
_supp_pnts.size(),
_values_at_supp_pnts);
}

values_at_interpol_pnts.resize(points_to_interpolate.size());
std::transform(points_to_interpolate.begin(), points_to_interpolate.end(),
values_at_interpol_pnts.begin(),
[&](double const& p) { return this->getValue(p); } );
}

PiecewiseLinearInterpolation::~PiecewiseLinearInterpolation()
{}

double PiecewiseLinearInterpolation::getValue(double pnt_to_interpolate) const
{
// search interval that has the point inside
Expand Down
25 changes: 3 additions & 22 deletions MathLib/InterpolationAlgorithms/PiecewiseLinearInterpolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace MathLib
/**
* This class implements a one dimensional piecewise linear interpolation algorithm.
*/
class PiecewiseLinearInterpolation
class PiecewiseLinearInterpolation final
{
public:
/**
Expand All @@ -44,28 +44,9 @@ class PiecewiseLinearInterpolation
* @param supp_pnts_sorted false (default), if it is sure the supporting points are sorted
* one can set the switch to true
*/
PiecewiseLinearInterpolation(const std::vector<double>& supporting_points,
const std::vector<double>& values_at_supp_pnts,
PiecewiseLinearInterpolation(std::vector<double>&& supporting_points,
std::vector<double>&& values_at_supp_pnts,
bool supp_pnts_sorted = false);
/**
* The same requirements on the input vectors supporting_points and values_at_supp_pnts
* have to apply as for the previous constructor.
* @param supporting_points vector of supporting points
* @param values_at_supp_pnts vector of values at the supporting points
* @param points_to_interpolate The points should be located within the range
* \f$[x_{\min}, x_{\max}]\f$, where \f$x_{\min} = \min_{1 \le j \le n} x_j\f$ and
* \f$x_{\max} = \max_{1 \le j \le n} x_j\f$. Points outside of this interval are extrapolated.
* @param values_at_interpol_pnts The interpolated values corresponding to the entries
* of the vector points_to_interpolate.
* @param supp_pnts_sorted Is set to false per default. If it is sure that the supporting
* points are sorted one can set the switch to true.
*/
PiecewiseLinearInterpolation(const std::vector<double>& supporting_points,
const std::vector<double>& values_at_supp_pnts,
const std::vector<double>& points_to_interpolate,
std::vector<double>& values_at_interpol_pnts,
bool supp_pnts_sorted = false);
virtual ~PiecewiseLinearInterpolation();

/**
* \brief Calculates the interpolation value.
Expand Down
3 changes: 2 additions & 1 deletion NumLib/Function/LinearInterpolationAlongPolyline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ MathLib::PiecewiseLinearInterpolation LinearInterpolationAlongPolyline::createIn
}
}

return MathLib::PiecewiseLinearInterpolation(vec_known_dist, vec_known_values);
return MathLib::PiecewiseLinearInterpolation{std::move(vec_known_dist),
std::move(vec_known_values)};
}

double LinearInterpolationAlongPolyline::operator()(const MathLib::Point3d& pnt) const
Expand Down
6 changes: 4 additions & 2 deletions Tests/MathLib/TestPiecewiseLinearInterpolation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ TEST(MathLibInterpolationAlgorithms, PiecewiseLinearInterpolation)
}
}

MathLib::PiecewiseLinearInterpolation interpolation(supp_pnts, values);
MathLib::PiecewiseLinearInterpolation interpolation{std::move(supp_pnts),
std::move(values)};
// Interpolation
for (std::size_t k(0); k<size-1; ++k) {
ASSERT_NEAR(0.5, interpolation.getValue(k+0.5), std::numeric_limits<double>::epsilon());
Expand Down Expand Up @@ -67,7 +68,8 @@ TEST(MathLibInterpolationAlgorithms, PiecewiseLinearInterpolationSupportPntsInRe
}
}

MathLib::PiecewiseLinearInterpolation interpolation(supp_pnts, values);
MathLib::PiecewiseLinearInterpolation interpolation{std::move(supp_pnts),
std::move(values)};
// Interpolation
for (std::size_t k(0); k<size-1; ++k) {
ASSERT_NEAR(0.5, interpolation.getValue(k+0.5), std::numeric_limits<double>::epsilon());
Expand Down