Commit 52b3046e authored by Lukas Riedel's avatar Lukas Riedel

Add logging to interpolators and scaling adapters

* default logger for scaling is Richards
* pass logger to scaling adapters
* default logger for interpolators is base
* only InterpolatorFactory uses logger
parent d99eacd4
......@@ -12,6 +12,8 @@
#include <dune/common/fvector.hh>
#include <dune/common/exceptions.hh>
#include <dune/dorie/common/logging.hh>
namespace Dune {
namespace Dorie {
......@@ -174,23 +176,44 @@ struct InterpolatorFactory
{
/// Create the interpolator.
/** \param type The type of interpolator to use
* \param args Arguments passed to the interpolator constructor
/** Use perfect forwarding for the data.
* \param type The type of interpolator to use
* \param data The data array for the interpolator
* \param shape The shape of the original dataset
* \param extensions The physical extensions to map the data to
* \param offset The offset of the field with respect to the origin
* \param log The logger for the created object, defaults to
* Dune::Dorie::log_base
* \return Shared pointer to the interpolator
*/
template<typename DataType, typename... Args>
static auto create (const std::string& type, Args&&... args)
-> std::shared_ptr<Interpolator<DataType, Traits>>
template<typename Data, typename Shape, typename Domain>
static auto create (
const std::string& type,
Data&& data,
Shape&& shape,
Domain&& extensions,
Domain&& offset,
const std::shared_ptr<spdlog::logger> log=get_logger(log_base)
)
-> std::shared_ptr<Interpolator<
typename std::remove_reference_t<Data>::value_type,
Traits>>
{
using NNInterp = NearestNeighborInterpolator<DataType, Traits>;
log->debug("Creating interpolator of type: {}", type);
using data_t = typename std::remove_reference_t<Data>::value_type;
using NNInterp = NearestNeighborInterpolator<data_t, Traits>;
if (type == NNInterp::type) {
return std::make_shared<NNInterp>(
std::forward<Args>(args)...
std::forward<Data>(data),
std::forward<Shape>(shape),
std::forward<Domain>(extensions),
std::forward<Domain>(offset)
);
}
else {
DUNE_THROW(Dune::NotImplemented, "Interpolator " + type + "is not "
"available!");
log->error("Unknown interpolator type: {}", type);
DUNE_THROW(Dune::NotImplemented, "Unknown interpolator type");
}
}
......
......@@ -9,6 +9,7 @@
#include <yaml-cpp/yaml.h>
#include <dune/dorie/common/logging.hh>
#include <dune/dorie/common/interpolator.hh>
#include <dune/dorie/common/h5file.hh>
......@@ -53,13 +54,20 @@ protected:
//! Interpolator storage
std::vector<std::shared_ptr<Interpolator<RF, Traits>>> _interpolators;
/// Logger of this instance
const std::shared_ptr<spdlog::logger> _log;
public:
/// Create the adapter and store config data
/** Take a YAML node as unnamed parameter to enforce derived classes to
* take one as argument, too.
* \log The logger for this instance (defaults to the Richards logger)
*/
explicit ScalingAdapter (const YAML::Node&)
explicit ScalingAdapter (
const YAML::Node&,
const std::shared_ptr<spdlog::logger> log
):
_log(log)
{ }
/// Evaluate this adapter.
......@@ -76,10 +84,13 @@ protected:
*/
void add_interpolator (const YAML::Node& cfg, const std::string node_name)
{
_log->trace("Adding interpolator from data node: {}", node_name);
const auto dim = Traits::dim;
// open the H5 file
const auto filename = cfg["file"].as<std::string>();
const auto dataset = cfg["dataset"].as<std::string>();
H5File h5file(filename);
H5File h5file(filename, _log);
// read the dataset
std::vector<RF> data;
......@@ -89,13 +100,23 @@ protected:
// read extensions and offset
const auto ext = cfg["extensions"].as<std::vector<RF>>();
const auto off = cfg["offset"].as<std::vector<RF>>();
if (ext.size() != Traits::dim) {
DUNE_THROW(Dune::IOError, "Extensions of " + node_name +
" dataset have wrong dimensions!");
if (shape.size() != dim) {
_log->error("Expected {}-dimensional dataset. File: {}, "
"Dataset: {}, Dimensions: {}",
dim, filename, dataset, shape.size());
DUNE_THROW(Dune::IOError, "Invalid dataset for interpolator");
}
if (ext.size() != dim) {
_log->error("Expected {}-dimensional sequence as 'extensions' "
"at scaling section node: {}",
dim, node_name);
DUNE_THROW(Dune::IOError, "Invalid interpolator input");
}
if (off.size() != Traits::dim) {
DUNE_THROW(Dune::IOError, "Offset of " + node_name +
" dataset has wrong dimensions!");
if (off.size() != dim) {
_log->error("Expected {}-dimensional sequence as 'offset' "
"at scaling section node: {}",
dim, node_name);
DUNE_THROW(Dune::IOError, "Invalid interpolator input");
}
// copy into correct data structure
......@@ -106,12 +127,13 @@ protected:
// build interpolator
this->_interpolators.emplace_back(
InterpolatorFactory<Traits>::template create<RF>(
InterpolatorFactory<Traits>::create(
cfg["interpolation"].template as<std::string>(),
data,
shape,
extensions,
offset
offset,
_log
)
);
}
......@@ -153,8 +175,11 @@ private:
public:
/// Create this adapter and the internal interpolator.
explicit MillerScalingAdapter (const YAML::Node& scale_data_cfg) :
Base(scale_data_cfg)
explicit MillerScalingAdapter (
const YAML::Node& scale_data_cfg,
const std::shared_ptr<spdlog::logger> log
):
Base(scale_data_cfg, log)
{
this->add_interpolator(scale_data_cfg["scale_miller"], "scale_miller");
}
......@@ -192,8 +217,11 @@ private:
public:
/// Create this adapter. Don't initialize members.
explicit DummyScalingAdapter (const YAML::Node& scale_data_cfg) :
Base(scale_data_cfg)
explicit DummyScalingAdapter (
const YAML::Node& scale_data_cfg,
const std::shared_ptr<spdlog::logger> log
):
Base(scale_data_cfg, log)
{ }
/// Export type of this scaling adapter
......@@ -241,22 +269,27 @@ public:
/// Create a scaling adapter using a YAML config node
/** \param type Type of the scaling adapter to be created
* \param config YAML config node specifying the scale adapter settings
* \log The logger to use in the created object. Defaults to
* Dune::Dorie::log_richards.
* \return Shared pointer to the scaling adapter
*/
static std::shared_ptr<ScalingAdapter<Traits>> create (
const std::string& type,
const YAML::Node& config
const YAML::Node& config,
const std::shared_ptr<spdlog::logger> log=get_logger(log_richards)
)
{
log->debug("Creating scaling adapter of type: {}", type);
if (type == MillerScalingAdapter<Traits>::type) {
return std::make_shared<MillerScalingAdapter<Traits>>(config);
return std::make_shared<MillerScalingAdapter<Traits>>(config, log);
}
else if (to_lower(type) == DummyScalingAdapter<Traits>::type) {
return std::make_shared<DummyScalingAdapter<Traits>>(config);
return std::make_shared<DummyScalingAdapter<Traits>>(config, log);
}
else {
DUNE_THROW(Dune::NotImplemented, "Scaling type " + type +
" not available!");
log->error("Unknown scaling type: {}", type);
DUNE_THROW(Dune::NotImplemented, "Unknown scaling type");
}
}
};
......
......@@ -46,11 +46,11 @@ void test_nearest_neighbor ()
// build interpolator
auto interp = Dune::Dorie::InterpolatorFactory<InterpolatorTraits<dim>>
::template create<double>("nearest",
data,
shape,
extensions,
offset);
::create("nearest",
data,
shape,
extensions,
offset);
// check without offset
using Dune::FloatCmp::eq; // floating-point comparison
......@@ -89,11 +89,11 @@ void test_nearest_neighbor ()
// check with offset
std::fill(begin(offset), end(offset), -1.0);
interp = Dune::Dorie::InterpolatorFactory<InterpolatorTraits<dim>>
::template create<double>("nearest",
data,
shape,
extensions,
offset);
::create("nearest",
data,
shape,
extensions,
offset);
if constexpr (dim == 2) {
corners.resize(4);
......@@ -131,7 +131,8 @@ int main (int argc, char** argv)
{
try{
// initialize MPI if needed
Dune::MPIHelper::instance(argc, argv);
auto& helper = Dune::MPIHelper::instance(argc, argv);
Dune::Dorie::create_base_logger(helper);
// test the NearestNeighbor interpolator
test_nearest_neighbor<2>();
......
......@@ -49,10 +49,12 @@ struct Traits
template<int dim>
void test_scaling_setting (const YAML::Node& cfg)
{
const auto log = Dune::Dorie::get_logger(Dune::Dorie::log_base);
// create the scaling
const auto type = cfg["type"].as<std::string>();
using SAF = Dune::Dorie::ScalingAdapterFactory<Traits<dim>>;
auto scaling = SAF::create(type, cfg["data"]);
auto scaling = SAF::create(type, cfg["data"], log);
// initialize RNG
std::mt19937 rng(1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment