From e3470528b306251a9cdf46be9111db3c96755278 Mon Sep 17 00:00:00 2001 From: mkirsz <s1351949@sms.ed.ac.uk> Date: Tue, 17 Dec 2024 13:07:35 +0000 Subject: [PATCH] Fix and cleanup of inheritance --- .../functions/basis_functions/bf_base.h | 15 +++-- .../functions/basis_functions/bf_linear.h | 8 +-- .../basis_functions/bf_polynomial2.h | 8 +-- .../tadah/models/functions/function_base.h | 56 +++++++++---------- .../models/functions/kernels/kern_base.h | 28 +++++----- .../models/functions/kernels/kern_linear.h | 16 +++--- .../tadah/models/functions/kernels/kern_lq.h | 8 +-- .../functions/kernels/kern_polynomial.h | 8 +-- .../models/functions/kernels/kern_quadratic.h | 8 +-- .../tadah/models/functions/kernels/kern_rbf.h | 8 +-- .../models/functions/kernels/kern_sigmoid.h | 8 +-- src/bf_base.cpp | 2 + src/bf_linear.cpp | 2 +- src/bf_polynomial2.cpp | 3 +- src/function_base.cpp | 15 +++++ src/kern_base.cpp | 2 + src/kern_linear.cpp | 2 +- src/kern_lq.cpp | 2 +- src/kern_polynomial.cpp | 2 +- src/kern_quadratic.cpp | 2 +- src/kern_rbf.cpp | 2 +- src/kern_sigmoid.cpp | 2 +- 22 files changed, 113 insertions(+), 94 deletions(-) diff --git a/include/tadah/models/functions/basis_functions/bf_base.h b/include/tadah/models/functions/basis_functions/bf_base.h index c68da21..19f76fb 100644 --- a/include/tadah/models/functions/basis_functions/bf_base.h +++ b/include/tadah/models/functions/basis_functions/bf_base.h @@ -8,11 +8,14 @@ /** \brief Base class to be used by all basis functions */ struct BF_Base: public virtual Function_Base { - virtual ~BF_Base(); - virtual double epredict(const t_type &, const aed_type2& ) const=0; - virtual double fpredict(const t_type &, const fd_type &, - const aed_type2& , const size_t ) const=0; - virtual force_type fpredict(const t_type &, const fd_type &, - const aed_type2& ) const=0; + BF_Base(); + BF_Base(const Config &c); + virtual ~BF_Base(); + + // virtual double epredict(const t_type &, const aed_type2& ) const=0; + // virtual double fpredict(const t_type &, const fd_type &, + // const aed_type2& , const size_t ) const=0; + // virtual force_type fpredict(const t_type &, const fd_type &, + // const aed_type2& ) const=0; }; #endif diff --git a/include/tadah/models/functions/basis_functions/bf_linear.h b/include/tadah/models/functions/basis_functions/bf_linear.h index ca36097..4595199 100644 --- a/include/tadah/models/functions/basis_functions/bf_linear.h +++ b/include/tadah/models/functions/basis_functions/bf_linear.h @@ -24,7 +24,7 @@ struct BF_Linear : public virtual BF_Base { * @brief Retrieve the label of the basis function. * @return String label of the basis function. */ - std::string get_label() const; + std::string get_label() const override; std::string label = "BF_Linear"; ///< Label identifying the basis function. @@ -34,7 +34,7 @@ struct BF_Linear : public virtual BF_Base { * @param aed Atomic descriptors. * @return Predicted energy. */ - double epredict(const t_type &weights, const aed_type2 &aed) const; + double epredict(const t_type &weights, const aed_type2 &aed) const override; /** * @brief Predicts force for a specific dimension using the linear basis function. @@ -45,7 +45,7 @@ struct BF_Linear : public virtual BF_Base { * @return Predicted force component. */ double fpredict(const t_type &weights, const fd_type &fdij, - const aed_type2 &aedi, const size_t k) const; + const aed_type2 &aedi, const size_t k) const override; /** * @brief Predicts forces using the linear basis function. @@ -55,6 +55,6 @@ struct BF_Linear : public virtual BF_Base { * @return Predicted forces. */ force_type fpredict(const t_type &weights, const fd_type &fdij, - const aed_type2 &aedi) const; + const aed_type2 &aedi) const override; }; #endif diff --git a/include/tadah/models/functions/basis_functions/bf_polynomial2.h b/include/tadah/models/functions/basis_functions/bf_polynomial2.h index f8dcd9f..59400d5 100644 --- a/include/tadah/models/functions/basis_functions/bf_polynomial2.h +++ b/include/tadah/models/functions/basis_functions/bf_polynomial2.h @@ -26,7 +26,7 @@ struct BF_Polynomial2 : public virtual BF_Base { * @brief Retrieve the label of the basis function. * @return String label of the basis function. */ - std::string get_label() const; + std::string get_label() const override; /** * @brief Predicts energy using the polynomial basis function. @@ -34,7 +34,7 @@ struct BF_Polynomial2 : public virtual BF_Base { * @param aed Atomic descriptors. * @return Predicted energy. */ - double epredict(const t_type &weights, const aed_type2 &aed) const; + double epredict(const t_type &weights, const aed_type2 &aed) const override; /** * @brief Predicts force for a specific dimension using the polynomial basis function. @@ -45,7 +45,7 @@ struct BF_Polynomial2 : public virtual BF_Base { * @return Predicted force component. */ double fpredict(const t_type &weights, const fd_type &fdij, - const aed_type2 &aedi, const size_t k) const; + const aed_type2 &aedi, const size_t k) const override; /** * @brief Predicts forces using the polynomial basis function. @@ -55,6 +55,6 @@ struct BF_Polynomial2 : public virtual BF_Base { * @return Predicted forces. */ force_type fpredict(const t_type &weights, const fd_type &fdij, - const aed_type2 &aedi) const; + const aed_type2 &aedi) const override; }; #endif diff --git a/include/tadah/models/functions/function_base.h b/include/tadah/models/functions/function_base.h index 01d73b7..d9486c8 100644 --- a/include/tadah/models/functions/function_base.h +++ b/include/tadah/models/functions/function_base.h @@ -8,35 +8,31 @@ /** Base class for Kernels and Basis Functions */ struct Function_Base { - private: - int verbose; - - public: - Function_Base() { - verbose=0; - } - Function_Base(const Config &c) { - verbose = c.get<int>("VERBOSE"); - } - void set_verbose(int v) { verbose=v; } - int get_verbose() { return verbose; } - - // Derived classes must implement Derived() and Derived(Config) - virtual ~Function_Base() {}; - - // For Kernels - virtual double operator() (const aed_type2& , const aed_type2& ) { return 0.0; }; - virtual aed_type2 derivative(const aed_type2& , const aed_type2& ) { return aed_type2(); }; - virtual double prime(const aed_type2& , const aed_type2& , - const aed_type2& ) { return 0.0; } - virtual void set_basis(const Matrix ) {}; - - /** \brief Return label of this object */ - virtual std::string get_label() const=0; - virtual double epredict(const t_type &, const aed_type2& ) const=0; - virtual double fpredict(const t_type &, const fd_type &, - const aed_type2&, const size_t ) const=0; - virtual force_type fpredict(const t_type &, const fd_type &, - const aed_type2& ) const=0; +private: + int verbose; + +public: + Function_Base(); + Function_Base(const Config &c); + void set_verbose(int v); + int get_verbose(); + + // Derived classes must implement Derived() and Derived(Config) + virtual ~Function_Base(); + + // For Kernels + virtual double operator() (const aed_type2& , const aed_type2& ) const; + virtual aed_type2 derivative(const aed_type2& , const aed_type2& ) const; + virtual double prime(const aed_type2& , const aed_type2& , + const aed_type2& ) const; + virtual void set_basis(const Matrix ); + + /** \brief Return label of this object */ + virtual std::string get_label() const=0; + virtual double epredict(const t_type &, const aed_type2& ) const=0; + virtual double fpredict(const t_type &, const fd_type &, + const aed_type2&, const size_t ) const=0; + virtual force_type fpredict(const t_type &, const fd_type &, + const aed_type2& ) const=0; }; #endif diff --git a/include/tadah/models/functions/kernels/kern_base.h b/include/tadah/models/functions/kernels/kern_base.h index 41da02e..aac4d94 100644 --- a/include/tadah/models/functions/kernels/kern_base.h +++ b/include/tadah/models/functions/kernels/kern_base.h @@ -16,22 +16,24 @@ class Kern_Base: public virtual Function_Base { public: Matrix basis; + Kern_Base(); + Kern_Base(const Config &c); virtual ~Kern_Base(); - /** - * Calculate kernel value given two vectors - * - * - b = basis vector - * - af = atomic energy descriptor - */ + // /** + // * Calculate kernel value given two vectors + // * + // * - b = basis vector + // * - af = atomic energy descriptor + // */ virtual double operator() (const aed_type2 &b, const aed_type2 &af) const=0; - - /** - * Calculate the kernel derivative wrt to the second argument - * - * - b = basis vector - * - af = atomic energy descriptor - */ + // + // /** + // * Calculate the kernel derivative wrt to the second argument + // * + // * - b = basis vector + // * - af = atomic energy descriptor + // */ virtual aed_type2 derivative(const aed_type2 &b, const aed_type2 &af) const=0; /** diff --git a/include/tadah/models/functions/kernels/kern_linear.h b/include/tadah/models/functions/kernels/kern_linear.h index 697d85f..83090cb 100644 --- a/include/tadah/models/functions/kernels/kern_linear.h +++ b/include/tadah/models/functions/kernels/kern_linear.h @@ -18,21 +18,21 @@ class Kern_Linear : public virtual Kern_Base { public: Kern_Linear (); Kern_Linear (const Config &c); - std::string get_label() const; + std::string get_label() const override; /** * Label used for this class */ std::string label = "Kern_Linear"; - double operator() (const aed_type2& b, const aed_type2& af) const; - aed_type2 derivative(const aed_type2& b, const aed_type2& ) const; - double prime(const aed_type2& b, const aed_type2& af, const aed_type2& ff) const; - void set_basis(const Matrix ) {} + double operator() (const aed_type2& b, const aed_type2& af) const override; + aed_type2 derivative(const aed_type2& b, const aed_type2& ) const override; + double prime(const aed_type2& b, const aed_type2& af, const aed_type2& ff) const override; + void set_basis(const Matrix ) override {} - double epredict(const t_type &weights, const aed_type2& aed) const; + double epredict(const t_type &weights, const aed_type2& aed) const override; double fpredict(const t_type &weights, const fd_type &fdij, - const aed_type2& aedi, const size_t k) const; + const aed_type2& aedi, const size_t k) const override; force_type fpredict(const t_type &weights, const fd_type &fdij, - const aed_type2& aedi) const; + const aed_type2& aedi) const override; }; #endif diff --git a/include/tadah/models/functions/kernels/kern_lq.h b/include/tadah/models/functions/kernels/kern_lq.h index 7dbfb7e..126da98 100644 --- a/include/tadah/models/functions/kernels/kern_lq.h +++ b/include/tadah/models/functions/kernels/kern_lq.h @@ -24,10 +24,10 @@ class Kern_LQ : public virtual Kern_Base { * Label used for this class */ std::string label = "Kern_LQ"; - double operator() (const aed_type2& b, const aed_type2& af) const; - aed_type2 derivative(const aed_type2& b, const aed_type2& af) const; - double prime(const aed_type2& b, const aed_type2& af, const aed_type2& ff) const; - std::string get_label() const; + double operator() (const aed_type2& b, const aed_type2& af) const override; + aed_type2 derivative(const aed_type2& b, const aed_type2& af) const override; + double prime(const aed_type2& b, const aed_type2& af, const aed_type2& ff) const override; + std::string get_label() const override; }; #endif diff --git a/include/tadah/models/functions/kernels/kern_polynomial.h b/include/tadah/models/functions/kernels/kern_polynomial.h index 2467249..cc105ab 100644 --- a/include/tadah/models/functions/kernels/kern_polynomial.h +++ b/include/tadah/models/functions/kernels/kern_polynomial.h @@ -27,10 +27,10 @@ class Kern_Polynomial : public virtual Kern_Base { * Label used for this class */ std::string label = "Kern_Polynomial"; - std::string get_label() const; - double operator() (const aed_type2& b, const aed_type2& af) const; - aed_type2 derivative(const aed_type2& b, const aed_type2& af) const; - double prime(const aed_type2& b, const aed_type2& af, const aed_type2& ff) const; + std::string get_label() const override; + double operator() (const aed_type2& b, const aed_type2& af) const override; + aed_type2 derivative(const aed_type2& b, const aed_type2& af) const override; + double prime(const aed_type2& b, const aed_type2& af, const aed_type2& ff) const override; }; #endif diff --git a/include/tadah/models/functions/kernels/kern_quadratic.h b/include/tadah/models/functions/kernels/kern_quadratic.h index 187c3e8..150efa1 100644 --- a/include/tadah/models/functions/kernels/kern_quadratic.h +++ b/include/tadah/models/functions/kernels/kern_quadratic.h @@ -23,10 +23,10 @@ class Kern_Quadratic : public virtual Kern_Base { * Label used for this class */ std::string label = "Kern_Quadratic"; - double operator() (const aed_type2& b, const aed_type2& af) const; - aed_type2 derivative(const aed_type2& b, const aed_type2& af) const; - double prime(const aed_type2& b, const aed_type2& af, const aed_type2& ff) const; - std::string get_label() const; + double operator() (const aed_type2& b, const aed_type2& af) const override; + aed_type2 derivative(const aed_type2& b, const aed_type2& af) const override; + double prime(const aed_type2& b, const aed_type2& af, const aed_type2& ff) const override; + std::string get_label() const override; }; #endif diff --git a/include/tadah/models/functions/kernels/kern_rbf.h b/include/tadah/models/functions/kernels/kern_rbf.h index 340d457..b746bd3 100644 --- a/include/tadah/models/functions/kernels/kern_rbf.h +++ b/include/tadah/models/functions/kernels/kern_rbf.h @@ -25,10 +25,10 @@ class Kern_RBF : public virtual Kern_Base { Kern_RBF (); Kern_RBF (const Config &c); std::string label = "Kern_RBF"; - double operator() (const aed_type2& b, const aed_type2& af) const; - aed_type2 derivative(const aed_type2& b, const aed_type2& af) const; - double prime(const aed_type2& b, const aed_type2& af, const aed_type2& ff) const; - std::string get_label() const; + double operator() (const aed_type2& b, const aed_type2& af) const override; + aed_type2 derivative(const aed_type2& b, const aed_type2& af) const override; + double prime(const aed_type2& b, const aed_type2& af, const aed_type2& ff) const override; + std::string get_label() const override; private: double gamma; diff --git a/include/tadah/models/functions/kernels/kern_sigmoid.h b/include/tadah/models/functions/kernels/kern_sigmoid.h index 7f9c0f6..072f208 100644 --- a/include/tadah/models/functions/kernels/kern_sigmoid.h +++ b/include/tadah/models/functions/kernels/kern_sigmoid.h @@ -26,10 +26,10 @@ class Kern_Sigmoid : public virtual Kern_Base { Kern_Sigmoid (); Kern_Sigmoid (const Config &c); std::string label = "Kern_Sigmoid"; - double operator() (const aed_type2& b, const aed_type2& af) const; - aed_type2 derivative(const aed_type2& b, const aed_type2& af) const; - double prime(const aed_type2& b, const aed_type2& af, const aed_type2& ff) const; - std::string get_label() const; + double operator() (const aed_type2& b, const aed_type2& af) const override; + aed_type2 derivative(const aed_type2& b, const aed_type2& af) const override; + double prime(const aed_type2& b, const aed_type2& af, const aed_type2& ff) const override; + std::string get_label() const override; }; #endif diff --git a/src/bf_base.cpp b/src/bf_base.cpp index 043999f..67b6d57 100644 --- a/src/bf_base.cpp +++ b/src/bf_base.cpp @@ -1,2 +1,4 @@ #include <tadah/models/functions/basis_functions/bf_base.h> +BF_Base::BF_Base(const Config &c): Function_Base(c) {} +BF_Base::BF_Base() {} BF_Base::~BF_Base() {} diff --git a/src/bf_linear.cpp b/src/bf_linear.cpp index 50a612b..b9e73ce 100644 --- a/src/bf_linear.cpp +++ b/src/bf_linear.cpp @@ -1,7 +1,7 @@ #include <tadah/models/functions/basis_functions/bf_linear.h> BF_Linear::BF_Linear() {} -BF_Linear::BF_Linear(const Config &c): Function_Base(c) {} +BF_Linear::BF_Linear(const Config &c): BF_Base(c) {} std::string BF_Linear::get_label() const { return label; } diff --git a/src/bf_polynomial2.cpp b/src/bf_polynomial2.cpp index 8d6e6fc..387e3f1 100644 --- a/src/bf_polynomial2.cpp +++ b/src/bf_polynomial2.cpp @@ -1,8 +1,7 @@ #include <tadah/models/functions/basis_functions/bf_polynomial2.h> - BF_Polynomial2::BF_Polynomial2() {} -BF_Polynomial2::BF_Polynomial2(const Config &c): Function_Base(c) +BF_Polynomial2::BF_Polynomial2(const Config &c): BF_Base(c) {} std::string BF_Polynomial2::get_label() const { diff --git a/src/function_base.cpp b/src/function_base.cpp index 8e80b84..748af71 100644 --- a/src/function_base.cpp +++ b/src/function_base.cpp @@ -1,2 +1,17 @@ #include <tadah/models/functions/function_base.h> +Function_Base::Function_Base(): verbose(0) { +} +Function_Base::Function_Base(const Config &c) { + verbose = (c.exist("VERBOSE")) ? c.get<int>("VERBOSE") : 0; +} +void Function_Base::set_verbose(int v) { verbose=v; } +int Function_Base::get_verbose() { return verbose; } + +Function_Base::~Function_Base() {}; + +double Function_Base::operator() (const aed_type2& , const aed_type2& ) const {std::cout << "FB" <<std::endl; return 0.0; }; +aed_type2 Function_Base::derivative(const aed_type2& , const aed_type2& ) const { return aed_type2(); }; +double Function_Base::prime(const aed_type2& , const aed_type2& , + const aed_type2& ) const { return 0.0; } +void Function_Base::set_basis(const Matrix ) {}; diff --git a/src/kern_base.cpp b/src/kern_base.cpp index 557cfd1..105de45 100644 --- a/src/kern_base.cpp +++ b/src/kern_base.cpp @@ -1,5 +1,7 @@ #include <tadah/models/functions/kernels/kern_base.h> +Kern_Base::Kern_Base() {} +Kern_Base::Kern_Base(const Config &c): Function_Base(c) {} Kern_Base::~Kern_Base() {} void Kern_Base::set_basis(const Matrix b) { diff --git a/src/kern_linear.cpp b/src/kern_linear.cpp index 52e6ee0..ef72038 100644 --- a/src/kern_linear.cpp +++ b/src/kern_linear.cpp @@ -2,7 +2,7 @@ Kern_Linear::Kern_Linear() {} Kern_Linear::Kern_Linear (const Config &c): - Function_Base(c) + Kern_Base(c) {} double Kern_Linear::operator() (const aed_type2& b, const aed_type2& af) const { diff --git a/src/kern_lq.cpp b/src/kern_lq.cpp index 4a4733d..7bb370a 100644 --- a/src/kern_lq.cpp +++ b/src/kern_lq.cpp @@ -2,7 +2,7 @@ Kern_LQ::Kern_LQ () {} Kern_LQ::Kern_LQ (const Config &c): - Function_Base(c) + Kern_Base(c) { if (get_verbose()) std::cout << std::endl << label << std::endl; read_basis_from_config(c,basis); diff --git a/src/kern_polynomial.cpp b/src/kern_polynomial.cpp index cdc3a62..84a615b 100644 --- a/src/kern_polynomial.cpp +++ b/src/kern_polynomial.cpp @@ -2,7 +2,7 @@ Kern_Polynomial::Kern_Polynomial () {} Kern_Polynomial::Kern_Polynomial (const Config &config): - Function_Base(config), + Kern_Base(config), d(config.get<int>("MPARAMS",0)), gamma(config.get<double>("MPARAMS",1)), c(config.get<double>("MPARAMS",2)) diff --git a/src/kern_quadratic.cpp b/src/kern_quadratic.cpp index 1f19615..3852a96 100644 --- a/src/kern_quadratic.cpp +++ b/src/kern_quadratic.cpp @@ -2,7 +2,7 @@ Kern_Quadratic::Kern_Quadratic () {} Kern_Quadratic::Kern_Quadratic (const Config &c): - Function_Base(c) + Kern_Base(c) { if (get_verbose()) std::cout << std::endl << label << std::endl; diff --git a/src/kern_rbf.cpp b/src/kern_rbf.cpp index f8fb513..9a374ac 100644 --- a/src/kern_rbf.cpp +++ b/src/kern_rbf.cpp @@ -2,7 +2,7 @@ Kern_RBF::Kern_RBF () {} Kern_RBF::Kern_RBF (const Config &c): - Function_Base(c), + Kern_Base(c), sigma(c.get<double>("MPARAMS")), gamma(1.0/(2.0*sigma*sigma)) { diff --git a/src/kern_sigmoid.cpp b/src/kern_sigmoid.cpp index b3fbd49..1586030 100644 --- a/src/kern_sigmoid.cpp +++ b/src/kern_sigmoid.cpp @@ -2,7 +2,7 @@ Kern_Sigmoid::Kern_Sigmoid () {} Kern_Sigmoid::Kern_Sigmoid (const Config &config): - Function_Base(config), + Kern_Base(config), gamma(config.get<double>("MPARAMS",0)), c(config.get<double>("MPARAMS",1)) { -- GitLab