Registry to make easier additions of new models
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
# Create a library target for motions
|
||||
add_library(
|
||||
RWMotion STATIC
|
||||
RWMotion OBJECT
|
||||
conewobble.cpp
|
||||
conewobble.h
|
||||
conemotion.cpp
|
||||
conemotion.h
|
||||
diffusivemotion.cpp
|
||||
diffusivemotion.h
|
||||
coordinates.cpp
|
||||
coordinates.h
|
||||
base.cpp
|
||||
|
||||
@@ -1,22 +1,9 @@
|
||||
|
||||
#include "base.h"
|
||||
#include "../utils/registry.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "coordinates.h"
|
||||
#include "bimodalangle.h"
|
||||
#include "isosmallangle.h"
|
||||
#include "random.h"
|
||||
#include "foursitejump.h"
|
||||
#include "nsiteconejump.h"
|
||||
#include "sixsitejump.h"
|
||||
#include "rjoac.h"
|
||||
#include "conewobble.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
|
||||
namespace motions {
|
||||
BaseMotion::BaseMotion(std::string name, const double delta, const double eta) : m_name(std::move(name)), m_delta(delta), m_eta(eta) {}
|
||||
|
||||
@@ -43,31 +30,7 @@ namespace motions {
|
||||
}
|
||||
|
||||
std::unique_ptr<BaseMotion> BaseMotion::createFromInput(const std::string& input) {
|
||||
if (input == "FourSiteTetrahedral")
|
||||
return std::make_unique<FourSiteTetrahedron>();
|
||||
|
||||
if (input == "SixSiteOctahedralC3")
|
||||
return std::make_unique<SixSiteOctahedronC3>();
|
||||
|
||||
if (input == "IsotropicAngle")
|
||||
return std::make_unique<SmallAngle>();
|
||||
|
||||
if (input == "RandomJump")
|
||||
return std::make_unique<RandomJump>();
|
||||
|
||||
if (input == "BimodalAngle")
|
||||
return std::make_unique<BimodalAngle>();
|
||||
|
||||
if (input == "NSiteConeJump")
|
||||
return std::make_unique<NSiteJumpOnCone>();
|
||||
|
||||
if (input == "RandomJumpOnCone")
|
||||
return std::make_unique<RandomJumpOnCone>();
|
||||
|
||||
if (input == "ConeWobble")
|
||||
return std::make_unique<WobbleCone>();
|
||||
|
||||
throw std::invalid_argument("Invalid input " + input);
|
||||
return Registry<BaseMotion>::create(input);
|
||||
}
|
||||
|
||||
void BaseMotion::setParameters(const std::unordered_map<std::string, double> ¶meters) {
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
|
||||
#include "bimodalangle.h"
|
||||
#include "base.h"
|
||||
#include "coordinates.h"
|
||||
#include "../utils/registry.h"
|
||||
|
||||
static AutoRegister<motions::BaseMotion, motions::BimodalAngle> reg("BimodalAngle");
|
||||
|
||||
namespace motions {
|
||||
BimodalAngle::BimodalAngle(const double delta, const double eta, const double angle1, const double angle2, const double prob) :
|
||||
BaseMotion(std::string("BimodalAngle"), delta, eta),
|
||||
DiffusiveMotion(std::string("BimodalAngle"), delta, eta),
|
||||
m_angle1(angle1 * M_PI / 180.0),
|
||||
m_angle2(angle2 * M_PI / 180.0),
|
||||
m_prob(prob) {}
|
||||
BimodalAngle::BimodalAngle() : BaseMotion(std::string("BimodalAngle")) {}
|
||||
|
||||
void BimodalAngle::initialize(std::mt19937_64& rng) {
|
||||
m_prev_pos = draw_position(rng);
|
||||
m_initial_omega = omega_q(m_prev_pos);
|
||||
}
|
||||
BimodalAngle::BimodalAngle() : DiffusiveMotion(std::string("BimodalAngle")) {}
|
||||
|
||||
double BimodalAngle::jump(std::mt19937_64& rng) {
|
||||
const double angle = m_uni_dist(rng) < m_prob ? m_angle1 : m_angle2;
|
||||
|
||||
@@ -2,16 +2,14 @@
|
||||
#ifndef BIMODALANGLE_H
|
||||
#define BIMODALANGLE_H
|
||||
|
||||
#include "base.h"
|
||||
#include "coordinates.h"
|
||||
#include "diffusivemotion.h"
|
||||
|
||||
namespace motions {
|
||||
class BimodalAngle final : public BaseMotion {
|
||||
class BimodalAngle final : public DiffusiveMotion {
|
||||
public:
|
||||
BimodalAngle(double, double, double, double, double);
|
||||
BimodalAngle();
|
||||
|
||||
void initialize(std::mt19937_64& rng) override;
|
||||
double jump(std::mt19937_64& rng) override;
|
||||
[[nodiscard]] std::unique_ptr<BaseMotion> clone() const override;
|
||||
void setParameters(const std::unordered_map<std::string, double> &) override;
|
||||
@@ -22,7 +20,6 @@ namespace motions {
|
||||
double m_angle1{0};
|
||||
double m_angle2{0};
|
||||
double m_prob{0};
|
||||
coordinates::SphericalPos m_prev_pos{0., 0.};
|
||||
};
|
||||
}
|
||||
#endif //BIMODALANGLE_H
|
||||
|
||||
18
src/motions/conemotion.cpp
Normal file
18
src/motions/conemotion.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "conemotion.h"
|
||||
|
||||
namespace motions {
|
||||
void ConeMotion::initialize(std::mt19937_64& rng) {
|
||||
m_axis = draw_position(rng);
|
||||
}
|
||||
|
||||
void ConeMotion::setParameters(const std::unordered_map<std::string, double> ¶meters) {
|
||||
BaseMotion::setParameters(parameters);
|
||||
m_angle = parameters.at("angle");
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, double> ConeMotion::getParameters() const {
|
||||
auto parameter = BaseMotion::getParameters();
|
||||
parameter["angle"] = m_angle;
|
||||
return parameter;
|
||||
}
|
||||
}
|
||||
23
src/motions/conemotion.h
Normal file
23
src/motions/conemotion.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef CONEMOTION_H
|
||||
#define CONEMOTION_H
|
||||
|
||||
#include "base.h"
|
||||
#include "coordinates.h"
|
||||
|
||||
namespace motions {
|
||||
class ConeMotion : public BaseMotion {
|
||||
public:
|
||||
using BaseMotion::BaseMotion;
|
||||
|
||||
void initialize(std::mt19937_64& rng) override;
|
||||
|
||||
void setParameters(const std::unordered_map<std::string, double> &) override;
|
||||
[[nodiscard]] std::unordered_map<std::string, double> getParameters() const override;
|
||||
|
||||
protected:
|
||||
double m_angle{0};
|
||||
coordinates::SphericalPos m_axis{1, 0};
|
||||
};
|
||||
}
|
||||
|
||||
#endif //CONEMOTION_H
|
||||
@@ -1,14 +1,13 @@
|
||||
|
||||
#include "conewobble.h"
|
||||
#include "coordinates.h"
|
||||
#include "../utils/registry.h"
|
||||
|
||||
static AutoRegister<motions::BaseMotion, motions::WobbleCone> reg("ConeWobble");
|
||||
|
||||
namespace motions {
|
||||
WobbleCone::WobbleCone(const double delta, const double eta, const double chi) : BaseMotion("Wobble in Cone", delta, eta), m_angle(chi) {}
|
||||
WobbleCone::WobbleCone() : BaseMotion("Wobble in Cone") {}
|
||||
|
||||
void WobbleCone::initialize(std::mt19937_64& rng) {
|
||||
m_axis = draw_position(rng);
|
||||
}
|
||||
WobbleCone::WobbleCone(const double delta, const double eta, const double chi) : ConeMotion("Wobble in Cone", delta, eta) { m_angle = chi; }
|
||||
WobbleCone::WobbleCone() : ConeMotion("Wobble in Cone") {}
|
||||
|
||||
double WobbleCone::jump(std::mt19937_64& rng) {
|
||||
const double real_angle = m_uni_dist(rng) * m_angle;
|
||||
@@ -20,18 +19,6 @@ namespace motions {
|
||||
return std::make_unique<WobbleCone>(*this);
|
||||
}
|
||||
|
||||
void WobbleCone::setParameters(const std::unordered_map<std::string, double> ¶meters) {
|
||||
BaseMotion::setParameters(parameters);
|
||||
m_angle = parameters.at("angle");
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, double> WobbleCone::getParameters() const {
|
||||
auto parameter = BaseMotion::getParameters();
|
||||
parameter["angle"] = m_angle;
|
||||
|
||||
return parameter;
|
||||
}
|
||||
|
||||
std::string WobbleCone::toString() const {
|
||||
return std::string("ConeWobble/angle=") + std::to_string(m_angle);
|
||||
}
|
||||
|
||||
@@ -1,26 +1,17 @@
|
||||
#ifndef CONEWOBBLE_H
|
||||
#define CONEWOBBLE_H
|
||||
|
||||
#include "base.h"
|
||||
#include "coordinates.h"
|
||||
#include "conemotion.h"
|
||||
|
||||
namespace motions {
|
||||
class WobbleCone final: public BaseMotion {
|
||||
class WobbleCone final: public ConeMotion {
|
||||
public:
|
||||
WobbleCone(double, double, double);
|
||||
WobbleCone();
|
||||
|
||||
void initialize(std::mt19937_64& rng) override;
|
||||
double jump(std::mt19937_64& rng) override;
|
||||
[[nodiscard]] std::unique_ptr<BaseMotion> clone() const override;
|
||||
|
||||
void setParameters(const std::unordered_map<std::string, double> &) override;
|
||||
[[nodiscard]] std::unordered_map<std::string, double> getParameters() const override;
|
||||
[[nodiscard]] std::string toString() const override;
|
||||
|
||||
private:
|
||||
double m_angle{0};
|
||||
coordinates::SphericalPos m_axis{1, 0};
|
||||
};
|
||||
}
|
||||
#endif //CONEWOBBLE_H
|
||||
|
||||
8
src/motions/diffusivemotion.cpp
Normal file
8
src/motions/diffusivemotion.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "diffusivemotion.h"
|
||||
|
||||
namespace motions {
|
||||
void DiffusiveMotion::initialize(std::mt19937_64& rng) {
|
||||
m_prev_pos = draw_position(rng);
|
||||
m_initial_omega = omega_q(m_prev_pos);
|
||||
}
|
||||
}
|
||||
19
src/motions/diffusivemotion.h
Normal file
19
src/motions/diffusivemotion.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef DIFFUSIVEMOTION_H
|
||||
#define DIFFUSIVEMOTION_H
|
||||
|
||||
#include "base.h"
|
||||
#include "coordinates.h"
|
||||
|
||||
namespace motions {
|
||||
class DiffusiveMotion : public BaseMotion {
|
||||
public:
|
||||
using BaseMotion::BaseMotion;
|
||||
|
||||
void initialize(std::mt19937_64& rng) override;
|
||||
|
||||
protected:
|
||||
coordinates::SphericalPos m_prev_pos{0., 0.};
|
||||
};
|
||||
}
|
||||
|
||||
#endif //DIFFUSIVEMOTION_H
|
||||
@@ -1,5 +1,8 @@
|
||||
#include "foursitejump.h"
|
||||
#include "coordinates.h"
|
||||
#include "../utils/registry.h"
|
||||
|
||||
static AutoRegister<motions::BaseMotion, motions::FourSiteTetrahedron> reg("FourSiteTetrahedral");
|
||||
|
||||
namespace motions {
|
||||
FourSiteTetrahedron::FourSiteTetrahedron(const double delta, const double eta) :
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
#include "isosmallangle.h"
|
||||
#include "coordinates.h"
|
||||
#include "../utils/registry.h"
|
||||
|
||||
static AutoRegister<motions::BaseMotion, motions::SmallAngle> reg("IsotropicAngle");
|
||||
|
||||
namespace motions {
|
||||
SmallAngle::SmallAngle(const double delta, const double eta, const double chi) :
|
||||
BaseMotion(std::string("IsotropicAngle"), delta, eta), m_chi(chi * M_PI / 180.0) {}
|
||||
SmallAngle::SmallAngle() : BaseMotion(std::string("IsotropicAngle")) {}
|
||||
|
||||
void SmallAngle::initialize(std::mt19937_64& rng) {
|
||||
m_prev_pos = draw_position(rng);
|
||||
m_initial_omega = omega_q(m_prev_pos);
|
||||
}
|
||||
DiffusiveMotion(std::string("IsotropicAngle"), delta, eta), m_chi(chi * M_PI / 180.0) {}
|
||||
SmallAngle::SmallAngle() : DiffusiveMotion(std::string("IsotropicAngle")) {}
|
||||
|
||||
double SmallAngle::jump(std::mt19937_64& rng) {
|
||||
const double gamma{2 * M_PI * m_uni_dist(rng)};
|
||||
|
||||
@@ -2,16 +2,14 @@
|
||||
#ifndef RWSIM_MOTIONISOSMALLANGLE_H
|
||||
#define RWSIM_MOTIONISOSMALLANGLE_H
|
||||
|
||||
#include "base.h"
|
||||
#include "coordinates.h"
|
||||
#include "diffusivemotion.h"
|
||||
|
||||
namespace motions {
|
||||
class SmallAngle final : public BaseMotion {
|
||||
class SmallAngle final : public DiffusiveMotion {
|
||||
public:
|
||||
SmallAngle(double, double, double);
|
||||
SmallAngle();
|
||||
|
||||
void initialize(std::mt19937_64& rng) override;
|
||||
double jump(std::mt19937_64& rng) override;
|
||||
[[nodiscard]] std::unique_ptr<BaseMotion> clone() const override;
|
||||
void setParameters(const std::unordered_map<std::string, double> &) override;
|
||||
@@ -20,7 +18,6 @@ namespace motions {
|
||||
|
||||
private:
|
||||
double m_chi{0};
|
||||
coordinates::SphericalPos m_prev_pos{0., 0.};
|
||||
};
|
||||
}
|
||||
#endif //RWSIM_MOTIONISOSMALLANGLE_H
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
|
||||
#include "nsiteconejump.h"
|
||||
#include "coordinates.h"
|
||||
#include "../utils/registry.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
static AutoRegister<motions::BaseMotion, motions::NSiteJumpOnCone> reg("NSiteConeJump");
|
||||
|
||||
namespace motions {
|
||||
NSiteJumpOnCone::NSiteJumpOnCone(const double delta, const double eta, const int num_sites, const double chi) :
|
||||
BaseMotion("NSiteJumpOnCone", delta, eta),
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
|
||||
#include "random.h"
|
||||
#include "../utils/registry.h"
|
||||
|
||||
static AutoRegister<motions::BaseMotion, motions::RandomJump> reg("RandomJump");
|
||||
|
||||
namespace motions {
|
||||
RandomJump::RandomJump(const double delta, const double eta) : BaseMotion(std::string("RandomJump"), delta, eta) {}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
|
||||
#include "rjoac.h"
|
||||
#include "coordinates.h"
|
||||
#include "../utils/registry.h"
|
||||
|
||||
static AutoRegister<motions::BaseMotion, motions::RandomJumpOnCone> reg("RandomJumpOnCone");
|
||||
|
||||
namespace motions {
|
||||
RandomJumpOnCone::RandomJumpOnCone(const double delta, const double eta, const double chi) : BaseMotion("RJ on a Cone", delta, eta), m_angle(chi) {}
|
||||
RandomJumpOnCone::RandomJumpOnCone() : BaseMotion("RJ on a Cone") {}
|
||||
|
||||
void RandomJumpOnCone::initialize(std::mt19937_64& rng) {
|
||||
m_axis = draw_position(rng);
|
||||
}
|
||||
RandomJumpOnCone::RandomJumpOnCone(const double delta, const double eta, const double chi) : ConeMotion("RJ on a Cone", delta, eta) { m_angle = chi; }
|
||||
RandomJumpOnCone::RandomJumpOnCone() : ConeMotion("RJ on a Cone") {}
|
||||
|
||||
double RandomJumpOnCone::jump(std::mt19937_64& rng) {
|
||||
const double phi = 2 * M_PI * m_uni_dist(rng);
|
||||
@@ -18,18 +18,6 @@ namespace motions {
|
||||
return std::make_unique<RandomJumpOnCone>(*this);
|
||||
}
|
||||
|
||||
void RandomJumpOnCone::setParameters(const std::unordered_map<std::string, double> ¶meters) {
|
||||
BaseMotion::setParameters(parameters);
|
||||
m_angle = parameters.at("angle");
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, double> RandomJumpOnCone::getParameters() const {
|
||||
auto parameter = BaseMotion::getParameters();
|
||||
parameter["angle"] = m_angle;
|
||||
|
||||
return parameter;
|
||||
}
|
||||
|
||||
std::string RandomJumpOnCone::toString() const {
|
||||
return std::string("RandomJumpOnCone/angle=") + std::to_string(m_angle);
|
||||
}
|
||||
|
||||
@@ -1,26 +1,17 @@
|
||||
#ifndef RJOAC_H
|
||||
#define RJOAC_H
|
||||
|
||||
#include "base.h"
|
||||
#include "coordinates.h"
|
||||
#include "conemotion.h"
|
||||
|
||||
namespace motions {
|
||||
class RandomJumpOnCone final: public BaseMotion {
|
||||
class RandomJumpOnCone final: public ConeMotion {
|
||||
public:
|
||||
RandomJumpOnCone(double, double, double);
|
||||
RandomJumpOnCone();
|
||||
|
||||
void initialize(std::mt19937_64& rng) override;
|
||||
double jump(std::mt19937_64& rng) override;
|
||||
[[nodiscard]] std::unique_ptr<BaseMotion> clone() const override;
|
||||
|
||||
void setParameters(const std::unordered_map<std::string, double> &) override;
|
||||
[[nodiscard]] std::unordered_map<std::string, double> getParameters() const override;
|
||||
[[nodiscard]] std::string toString() const override;
|
||||
|
||||
private:
|
||||
double m_angle{0};
|
||||
coordinates::SphericalPos m_axis{1, 0};
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "sixsitejump.h"
|
||||
|
||||
#include "coordinates.h"
|
||||
#include "../utils/registry.h"
|
||||
|
||||
static AutoRegister<motions::BaseMotion, motions::SixSiteOctahedronC3> reg("SixSiteOctahedralC3");
|
||||
|
||||
namespace motions {
|
||||
SixSiteOctahedronC3::SixSiteOctahedronC3(const double delta, const double eta, const double chi) :
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
add_library(
|
||||
RWTime STATIC
|
||||
RWTime OBJECT
|
||||
base.cpp
|
||||
base.h
|
||||
delta.cpp
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
#include "base.h"
|
||||
#include "delta.h"
|
||||
#include "lognormal.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include "../utils/registry.h"
|
||||
|
||||
namespace times {
|
||||
BaseDistribution::BaseDistribution(std::string name, const double tau) : m_name(std::move(name)), m_tau(tau), m_tau_jump(tau) {}
|
||||
@@ -25,12 +22,6 @@ namespace times {
|
||||
|
||||
|
||||
std::unique_ptr<BaseDistribution> BaseDistribution::createFromInput(const std::string& input) {
|
||||
if (input == "Delta")
|
||||
return std::make_unique<DeltaDistribution>();
|
||||
|
||||
if (input == "LogNormal")
|
||||
return std::make_unique<LogNormalDistribution>();
|
||||
|
||||
throw std::invalid_argument("Invalid input " + input);
|
||||
return Registry<BaseDistribution>::create(input);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
#include "delta.h"
|
||||
#include "../utils/registry.h"
|
||||
|
||||
static AutoRegister<times::BaseDistribution, times::DeltaDistribution> reg("Delta");
|
||||
|
||||
namespace times {
|
||||
DeltaDistribution::DeltaDistribution(const double tau) : BaseDistribution(std::string("Delta"), tau) {}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
#include "lognormal.h"
|
||||
#include "../utils/registry.h"
|
||||
#include <cmath>
|
||||
|
||||
static AutoRegister<times::BaseDistribution, times::LogNormalDistribution> reg("LogNormal");
|
||||
|
||||
namespace times {
|
||||
LogNormalDistribution::LogNormalDistribution(const double tau, const double sigma) : BaseDistribution(std::string("LogNormal"), tau), m_sigma(sigma), m_distribution(std::log(tau), sigma) {}
|
||||
LogNormalDistribution::LogNormalDistribution() : BaseDistribution(std::string("LogNormal")) {}
|
||||
|
||||
50
src/utils/registry.h
Normal file
50
src/utils/registry.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef RWSIM_REGISTRY_H
|
||||
#define RWSIM_REGISTRY_H
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
template<typename Base>
|
||||
class Registry {
|
||||
public:
|
||||
using Creator = std::function<std::unique_ptr<Base>()>;
|
||||
|
||||
static std::unordered_map<std::string, Creator>& entries() {
|
||||
static std::unordered_map<std::string, Creator> map;
|
||||
return map;
|
||||
}
|
||||
|
||||
static void add(const std::string& name, Creator creator) {
|
||||
entries()[name] = std::move(creator);
|
||||
}
|
||||
|
||||
static std::unique_ptr<Base> create(const std::string& name) {
|
||||
auto& map = entries();
|
||||
auto it = map.find(name);
|
||||
if (it == map.end()) {
|
||||
std::string msg = "Unknown model '" + name + "'. Available: ";
|
||||
for (const auto& [key, _] : map) {
|
||||
msg += key + ", ";
|
||||
}
|
||||
if (!map.empty()) {
|
||||
msg.pop_back();
|
||||
msg.pop_back();
|
||||
}
|
||||
throw std::invalid_argument(msg);
|
||||
}
|
||||
return it->second();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Base, typename Derived>
|
||||
struct AutoRegister {
|
||||
explicit AutoRegister(const std::string& name) {
|
||||
Registry<Base>::add(name, []() { return std::make_unique<Derived>(); });
|
||||
}
|
||||
};
|
||||
|
||||
#endif //RWSIM_REGISTRY_H
|
||||
Reference in New Issue
Block a user