Files
karl/src/generators/function_generator.cpp
T

101 lines
4.0 KiB
C++
Executable File

#include "generators/function_generator.h"
#include <string>
#include "IO/hdf5_reader.h"
#include "functions/functions.h"
#include "generators/vector_field_generator.h"
#include "simulation/errors.h"
#include "simulation/jansson_additionals.h"
namespace generator {
std::function<double(double)> function1D(json_t* object) {
if (json_object_by_path(object, {"Constant"})) {
DEBUG_LEVEL_2("Creating function1d: constant.");
double constant =
json_double(json_object_by_path(object, {"Constant", "constant"}));
DEBUG_LEVEL_3("with constant: " + std::to_string(constant));
return D1ToD1::Constant(constant);
} else if (json_object_by_path(object, {"PiecewiseLinear"})) {
DEBUG_LEVEL_2("Creating function1d: PiecewiseLinear.");
std::vector<double> x_values = json_vector(
json_object_by_path(object, {"PiecewiseLinear", {"xValues"}}));
std::vector<double> y_values = json_vector(
json_object_by_path(object, {"PiecewiseLinear", {"yValues"}}));
double prefactor = json_double(
json_object_by_path(object, {"PiecewiseLinear", {"prefactor"}}));
for (int i = 0; i < y_values.size(); ++i) {
y_values[i] = y_values[i] * prefactor;
}
return D1ToD1::PiecewiseLinear(x_values, y_values);
} else if (json_object_by_path(object, {"PiecewiseLinearFromFile"})) {
DEBUG_LEVEL_2("Creating function1d: PiecewiseLinear from file.");
std::string absolute_filename = readAbsoluteFilename(
json_object_by_path(object, {"PiecewiseLinearFromFile", "PathToH5"}));
DEBUG_LEVEL_3("with filename: " + absolute_filename);
Hdf5Reader reader(absolute_filename);
std::string x_values_key = json_string(
json_object_by_path(object, {"PiecewiseLinearFromFile", "xValuesKey"}));
blitz::Array<double, 1> x_values_array = reader.readD1Double(x_values_key);
// DEBUG_LEVEL_3("with x values: " + x_values_array);
std::string y_values_key = json_string(
json_object_by_path(object, {"PiecewiseLinearFromFile", "yValuesKey"}));
blitz::Array<double, 1> y_values_array = reader.readD1Double(y_values_key);
// DEBUG_LEVEL_3("with y values: " + y_values_array);
std::vector<double> x_values(x_values_array.begin(), x_values_array.end());
std::vector<double> y_values(y_values_array.begin(), y_values_array.end());
double prefactor = json_double(json_object_by_path(
object, {"PiecewiseLinearFromFile", {"prefactor"}}));
DEBUG_LEVEL_3("with prefactor: " + std::to_string(prefactor));
for (int i = 0; i < y_values.size(); ++i) {
y_values[i] = y_values[i] * prefactor;
}
return D1ToD1::PiecewiseLinear(x_values, y_values);
} else if (json_object_by_path(object, {"Product"})) {
DEBUG_LEVEL_2("Creating function1d: Product.");
json_t* function_array = json_object_by_path(object, {"Product"});
std::vector<std::function<double(double)>> functions;
size_t index;
json_t* value;
json_array_foreach(function_array, index, value) {
functions.push_back(function1D(value));
}
return D1ToD1::Product(functions);
} else {
ERROR("This 1d to 1d function is not implemented.");
return nullptr;
}
}
std::function<double(double, double, double)> function3D(json_t* object) {
if (json_object_by_path(object, {"Constant3D"})) {
DEBUG_LEVEL_2("Creating function3d: Constant3D.");
double constant =
json_double(json_object_by_path(object, {"Constant3D", "constant"}));
return D3ToD1::Constant(constant);
} else if (json_object_by_path(object, {"Product"})) {
DEBUG_LEVEL_2("Creating function3d: Product.");
std::function<double(double)> fi =
function1D(json_object_by_path(object, {"Product", "fi"}));
std::function<double(double)> fj =
function1D(json_object_by_path(object, {"Product", "fj"}));
std::function<double(double)> fk =
function1D(json_object_by_path(object, {"Product", "fk"}));
return D3ToD1::Product(fi, fj, fk);
} else {
ERROR("This 3d to 1d function is not implemented.");
return nullptr;
}
}
}; // namespace generator