Это тот тип вещей, для которого могут использоваться полиморфизм и фабрики классов, например:
class Base_pp
{
public:
virtual ~Base_pp() {}
virtual std::tuple<double, double> get_force(double &r, double m, double C) = 0;
};
class GCM_pp : public Base_pp
{
public:
std::tuple<double, double> get_force(double &r, double m, double C) override {
auto [ff, u] = MD_tools::GCM_force(r, m, C); // This is static
return std::make_tuple(ff, u);
}
};
class Exp_pp : public Base_pp
{
public:
std::tuple<double, double> get_force(double &r, double m, double C) override {
auto [ff, u] = MD_tools::Exp_force(r, m, C); // This is static
return std::make_tuple(ff, u);
}
};
...
using funcType = std::unique_ptr<Base_pp> (*)();
std::map<std::string, funcType> make_funcs = {
{"GCM", []() -> std::unique_ptr<Base_pp> { return std::make_unique<GCM_pp>(); }},
...
{"Exp", []() -> std::unique_ptr<Base_pp> { return std::make_unique<Exp_pp>(); }}
};
std::unique_ptr<Base_pp> make_pp(std::string pp_type) {
funcType func = make_funcs[pp_type];
if (!func) func = make_funcs["Exp"];
return func();
}
...
void simulate(std::string pp_type, size_t step, double POWER, double A_CST) {
std::unique_ptr<Base_pp> potential = make_pp(pp_type);
for (step = 0; step < step_max; ++step) { // typical values: step ~1,000,000
size_t i, j;
for (i = 0; i < N - 1; ++i) { // typical values: N ~10,000
for (j = i + 1; j < N; ++j) {
// stuff happening ...
double r = sqrt((x * x) + (y * y) + (z * z));
auto [ff, temp_u] = potential->get_force(r, POWER, A_CST);
// stuff happening ...
}
}
}
}
В качестве альтернативы, вам на самом деле не нужен полиморфизм в вашем примере.Поскольку ваши классы не используют данные экземпляра, вы можете попробовать что-то более похожее на это:
class GCM_pp
{
public:
static std::tuple<double, double> get_force(double &r, double m, double C) {
auto [ff, u] = MD_tools::GCM_force(r, m, C); // This is static
return std::make_tuple(ff, u);
}
};
class Exp_pp
{
public:
static std::tuple<double, double> get_force(double &r, double m, double C) {
auto [ff, u] = MD_tools::Exp_force(r, m, C); // This is static
return std::make_tuple(ff, u);
}
};
...
using funcType = std::tuple<double, double> (*)(double&, double, double);
std::map<std::string, funcType> get_force_funcs = {
{"GCM", &GCM_pp::get_force},
...
{"Exp", &Exp_pp::get_force}
};
funcType get_force_func(std::string pp_type) {
funcType func = get_force_funcs[pp_type];
if (!func) func = get_force_funcs["Exp"];
return func;
}
...
void simulate(std::string pp_type, size_t step, double POWER, double A_CST) {
funcType get_force = get_force_func(pp_type);
for (step = 0; step < step_max; ++step) { // typical values: step ~1,000,000
size_t i, j;
for (i = 0; i < N - 1; ++i) { // typical values: N ~10,000
for (j = i + 1; j < N; ++j) {
// stuff happening ...
double r = sqrt((x * x) + (y * y) + (z * z));
auto [ff, temp_u] = get_force(r, POWER, A_CST);
// stuff happening ...
}
}
}
}
Или вот это:
class GCM_pp
{
public:
static std::tuple<double, double> get_force(double &r, double m, double C) {
auto [ff, u] = MD_tools::GCM_force(r, m, C); // This is static
return std::make_tuple(ff, u);
}
};
class Exp_pp
{
public:
static std::tuple<double, double> get_force(double &r, double m, double C) {
auto [ff, u] = MD_tools::Exp_force(r, m, C); // This is static
return std::make_tuple(ff, u);
}
};
...
template<typename T>
void do_simulate(size_t step, double POWER, double A_CST) {
for (step = 0; step < step_max; ++step) { // typical values: step ~1,000,000
size_t i, j;
for (i = 0; i < N - 1; ++i) { // typical values: N ~10,000
for (j = i + 1; j < N; ++j) {
// stuff happening ...
double r = sqrt((x * x) + (y * y) + (z * z));
auto [ff, temp_u] = T::get_force(r, POWER, A_CST);
// stuff happening ...
}
}
}
}
using funcType = void (*)(size_t, double, double);
std::map<std::string, funcType> simulate_funcs = {
{"GCM", &do_simulate<GCM_pp>},
...
{"Exp", &do_simulate<Exp_pp>}
};
funcType get_simulate_func(std::string pp_type) {
funcType func = simulate_funcs[pp_type];
if (!func) func = simulate_funcs["Exp"];
return func;
}
...
void simulate(std::string pp_type, size_t step, double POWER, double A_CST) {
funcType simulate_func = get_simulate_func(pp_type);
simulate_func(step, POWER, A_CST);
}