128 lines
4.7 KiB
C++
Executable File
128 lines
4.7 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"})) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Creating function1d: constant.");
|
|
#endif
|
|
double constant =
|
|
json_double(json_object_by_path(object, {"Constant", "constant"}));
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("with constant: " + std::to_string(constant));
|
|
#endif
|
|
return D1ToD1::Constant(constant);
|
|
|
|
} else if (json_object_by_path(object, {"PiecewiseLinear"})) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Creating function1d: PiecewiseLinear.");
|
|
#endif
|
|
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"})) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Creating function1d: PiecewiseLinear from file.");
|
|
#endif
|
|
|
|
std::string absolute_filename = readAbsoluteFilename(
|
|
json_object_by_path(object, {"PiecewiseLinearFromFile", "PathToH5"}));
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("with filename: " + absolute_filename);
|
|
#endif
|
|
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"}}));
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("with prefactor: " + std::to_string(prefactor));
|
|
#endif
|
|
|
|
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"})) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Creating function1d: Product.");
|
|
#endif
|
|
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 if (json_object_by_path(object, {"Exponential"})) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Creating function1d: Exponential.");
|
|
#endif
|
|
double prefactor = json_double(json_object_by_path(
|
|
object, {"Exponential", {"prefactor"}}));
|
|
double scale = json_double(json_object_by_path(
|
|
object, {"Exponential", {"scale"}}));
|
|
return D1ToD1::Exponential(prefactor, scale);
|
|
} 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"})) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Creating function3d: Constant3D.");
|
|
#endif
|
|
double constant =
|
|
json_double(json_object_by_path(object, {"Constant3D", "constant"}));
|
|
return D3ToD1::Constant(constant);
|
|
} else if (json_object_by_path(object, {"Product"})) {
|
|
#ifdef debug_level2
|
|
DEBUG_LEVEL_2("Creating function3d: Product.");
|
|
#endif
|
|
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
|