diff --git a/include/tadah/core/aed_type.h b/include/tadah/core/aed_type.h index 9b05b7a2e4ce9ff3fbf2d681d2bf4c3a233f7117..9b74a7123b0abbe9e83c05b4847cfe4139308928 100644 --- a/include/tadah/core/aed_type.h +++ b/include/tadah/core/aed_type.h @@ -9,6 +9,9 @@ #include <iostream> #include <chrono> +namespace tadah { +namespace core { + class aed_type { double *data_ = nullptr; size_t size_ = 0; @@ -276,5 +279,6 @@ inline std::ostream& operator<<(std::ostream& os, const aed_type& v) { // } // }; +}} #endif // AED_TYPE2_H diff --git a/include/tadah/core/aeds_type.h b/include/tadah/core/aeds_type.h index 37f5e99a8f64a1f9f3c6f29bd94be346eac9b32b..ac22d3f186fcdbfb1ee0c4a2a29a68e9447d2b91 100644 --- a/include/tadah/core/aeds_type.h +++ b/include/tadah/core/aeds_type.h @@ -7,6 +7,9 @@ #include <cstring> #include <iostream> +namespace tadah { +namespace core { + class aeds_type2 { /* Column major order * @@ -69,4 +72,5 @@ public: friend std::ostream& operator<<(std::ostream& os, const aeds_type2& v); }; +}} #endif // AEDS_TYPE2_H diff --git a/include/tadah/core/config.h b/include/tadah/core/config.h index 25eacc67264834161cbbcf26a9852a963d20bd00..778689bc33c2d93f8f06d1043cb2dcc2b549070e 100644 --- a/include/tadah/core/config.h +++ b/include/tadah/core/config.h @@ -17,6 +17,10 @@ CMRC_DECLARE(CORE); +namespace tadah { +namespace core { + + /** \brief A dictionary for key - value pairs. * * This dictionary object must be used in the workflow @@ -66,37 +70,111 @@ public: */ const std::vector<std::string>& operator()(const std::string key) const; - /** \brief Fill array with the values from the key. - * - * The array must be initialised to appropriate size. - * - * Usage example: - * - * \code{.cpp} - * # Create new config object and read config file - * Config config("config_file"); - * - * # Fill array with config values of type double - * std::vector<double> vd(10); - * c.get<std::vector<double>>("RCUT2B", vd); - * \endcode - * - * \tparam T indexable array - */ - template <typename T> - void get(const std::string key, T & value) const { - if (c.count(to_upper(key))==0) - throw std::runtime_error(knf+key); - int i=0; - try { - for (auto v : c.at(to_upper(key))) { - std::istringstream iss(v); - iss >> value[i++]; - } - } catch (...) { - throw std::runtime_error("Config: Failed getting reading/writing to an array "+key); + /** + * @brief A C++11-friendly type trait to detect if T has a .size() method. + * + * - Primary template: yields false if T does not have a .size() method. + * - Partial specialization: yields true if calling T::size() is valid. + */ + template<typename, typename = void> + struct has_size : std::false_type {}; + + // This partial specialization is chosen if (void)std::declval<U>().size() is valid. + template<typename U> + struct has_size<U, typename std::enable_if< + std::is_same< + decltype(std::declval<U>().size()), + decltype(std::declval<U>().size()) + >::value + >::type> : std::true_type {}; + + /** + * @brief Helper function that checks size at runtime (only if T has .size()). + * + * SFINAE: This overload is *only* compiled if T has a .size() method. + * + * @throw std::runtime_error if the container is too small. + */ + template<typename T> + typename std::enable_if<has_size<T>::value, void>::type + check_size_if_applicable(const T &arr, std::size_t required, const std::string &key) const + { + if (arr.size() < required) { + throw std::runtime_error("Config array too small for key: " + key); + } + } + + /** + * @brief Overload that does nothing if T *does not* have .size(). + * + * SFINAE: This is only compiled for types that have no .size() method. + */ + template<typename T> + typename std::enable_if<!has_size<T>::value, void>::type + check_size_if_applicable(const T&, std::size_t, const std::string&) const + { + // Do nothing: for types like raw pointers. + } + + /** + * @brief Fill an existing array-like type @p arr with config values under @p key. + * + * - If @p arr has .size(), a check verifies @p arr can hold all config values. + * - If @p arr does not have .size() (e.g., raw pointer), no check is performed. + * - If parsing fails or key is missing, a runtime error is thrown. + * + * ### Usage example + * \code{.cpp} + * // Assume a config with a key "MyKey" of 4 numeric values. + * Config cfg("config_file"); + * + * // 1) Use a container with .size(), e.g. std::vector<double>: + * std::vector<double> vec(5); // capacity 5 + * cfg.get("MyKey", vec); // Will check vec.size() >= 4 + * + * // 2) Use a raw pointer (no .size()): + * double arr[10]; + * cfg.get("MyKey", arr); // No size check performed. Caller must ensure arr is big enough. + * \endcode + * + * @tparam T container or pointer type that supports operator[]. + * @param key Name of the config key (case-insensitive). + * @param arr Destination where parsed values are stored. + * @throws std::runtime_error on key-not-found, array-too-small, or parse failure. + */ + template <typename T> + void get(const std::string &key, T &arr) const { + // Convert to uppercase for case-insensitive retrieval. + const auto upperKey = to_upper(key); + + // Check presence of the key in our config container. + const std::size_t numValues = c.count(upperKey); + if (numValues == 0) { + throw std::runtime_error(knf + key); + } + + // If T has .size(), do a runtime size check. If not, do nothing. + check_size_if_applicable(arr, numValues, key); + + try { + // For demonstration, assume c.at(...) returns a list of strings to be parsed. + const auto &values = c.at(upperKey); + for (std::size_t i = 0; i < values.size(); ++i) { + std::istringstream iss(values[i]); + iss >> arr[i]; // Works for both pointer indexing and container operator[] + if (iss.fail()) { + throw std::runtime_error( + "Config: Conversion failed for key: " + key + + " at value: '" + values[i] + "'" + ); + } + } + } catch (...) { + throw std::runtime_error( + "Config: Failed reading/writing to an array [" + key + "]" + ); + } } - } /** \brief Return the first value for the key. * @@ -316,6 +394,6 @@ private: const static std::string knf;//="Key not found: "; }; - - +} +} #endif diff --git a/include/tadah/core/core_types.h b/include/tadah/core/core_types.h index a98a2d2d8423cc8427f8b5e46427af6a3cc0346c..25061f0eb4a786ac9fb0938448681b03be8ef0f1 100644 --- a/include/tadah/core/core_types.h +++ b/include/tadah/core/core_types.h @@ -8,6 +8,9 @@ #include <vector> +namespace tadah { +namespace core { + typedef Vec3d force_type; typedef aed_type t_type; typedef aed_type rho_type; @@ -35,4 +38,5 @@ class mat { #endif ///////////////////////////////////////// +}} #endif diff --git a/include/tadah/core/element.h b/include/tadah/core/element.h index cc37c4efa44902f6b9fbe56daac240bb653713d4..f1e910ebc25c042e53d75a1fd124265c621bbcac 100644 --- a/include/tadah/core/element.h +++ b/include/tadah/core/element.h @@ -5,6 +5,9 @@ #include <functional> #include <string> +namespace tadah { +namespace core { + struct Element { private: void check(const std::string &symbol, const int Z); @@ -29,9 +32,11 @@ struct Element { static int toAtomicNumber(const std::string &symbol); static std::string toSymbol(int Z); }; +}} + namespace std { template <> - struct hash<Element>; + struct hash<tadah::core::Element>; } #endif diff --git a/include/tadah/core/fd_type.h b/include/tadah/core/fd_type.h index 4b665d6f4137cc7410ba3d3587eaf2723518d0fe..5211cf229213f95a6ba0b5797d21ebedea03a26d 100644 --- a/include/tadah/core/fd_type.h +++ b/include/tadah/core/fd_type.h @@ -12,6 +12,9 @@ #include <cstring> #include <limits> +namespace tadah { +namespace core { + class fd_type { size_t rows_ = 0; double* data_ = nullptr; @@ -84,4 +87,5 @@ public: bool operator==(const fd_type& v1) const; }; +}} #endif // FD_TYPE_H diff --git a/include/tadah/core/lapack.h b/include/tadah/core/lapack.h index 3a92deae426d9a7d1fdea11adc689ffd83dc8eec..99945b85c53e29e3fca5db08696f933d01a39a41 100644 --- a/include/tadah/core/lapack.h +++ b/include/tadah/core/lapack.h @@ -3,6 +3,9 @@ #include <tadah/core/maths.h> +namespace tadah { +namespace core { + extern "C" { /** @@ -213,4 +216,5 @@ double norm_X(Matrix &A, char norm='1'); aed_type solve_posv(Matrix &A, aed_type /*&*/T, char uplo='U'); double condition_number(Matrix &A, char uplo='U'); +}} #endif diff --git a/include/tadah/core/maths.h b/include/tadah/core/maths.h index eec93ed1bdda7a756741cf339127960f62117433..b890c0a23dbe88a97e321ef250ebfda3ad1f2f64 100644 --- a/include/tadah/core/maths.h +++ b/include/tadah/core/maths.h @@ -17,6 +17,9 @@ #include <array> +namespace tadah { +namespace core { + template <typename D> class Matrix_Base { // column-major order @@ -440,4 +443,5 @@ public: typedef Matrix3d_T<> Matrix3d; +}} #endif diff --git a/include/tadah/core/normaliser_core.h b/include/tadah/core/normaliser_core.h index 621a26c5173badfa6515ed969f7558bab73ad131..5232ab5ef284e02a56220babd9225bbba6bc34bb 100644 --- a/include/tadah/core/normaliser_core.h +++ b/include/tadah/core/normaliser_core.h @@ -4,6 +4,9 @@ #include <tadah/core/config.h> #include <tadah/core/core_types.h> +namespace tadah { +namespace core { + // We do calculate and store st. dev. and mean // for the first column of Phi matrix // for linear kernel, first column are 1s @@ -26,4 +29,6 @@ class Normaliser_Core { void normalise(fd_type &fd); void normalise(fd_type &fd, size_t k); }; + +}} #endif diff --git a/include/tadah/core/periodic_table.h b/include/tadah/core/periodic_table.h index acc199b070e8e728fd12a0cd484ce1445cf0fbfd..672d386036372dca7616ee4ffcabf120d7960970 100644 --- a/include/tadah/core/periodic_table.h +++ b/include/tadah/core/periodic_table.h @@ -5,6 +5,9 @@ #include <map> #include <tadah/core/element.h> +namespace tadah { +namespace core { + struct PeriodicTable { static std::map<std::string, Element> symbol; static std::map<std::string, Element> name; @@ -29,4 +32,5 @@ struct PeriodicTable { static void init_masses(); }; +}} #endif diff --git a/include/tadah/core/registry.h b/include/tadah/core/registry.h index a39e9d361047d77aa745896757b03b35d4f20daa..f0afb3cff6abea9956411d398df1c2fc2623b971 100644 --- a/include/tadah/core/registry.h +++ b/include/tadah/core/registry.h @@ -8,10 +8,12 @@ #include <sstream> #include <iostream> +namespace tadah { +namespace core { + template<typename T, typename... Args> using Factory = std::function<T*(Args&...)>; -namespace CONFIG { template<typename Base, typename... Args> struct Registry { @@ -41,6 +43,6 @@ Base* factory(const std::string& type, Args&... args) { throw std::runtime_error(oss.str()); } -} +}} #endif diff --git a/include/tadah/core/utils.h b/include/tadah/core/utils.h index 2516067565dda2ed00633f775ce528c3a27df223..213a363500ea2eb3d5d03a18c149094aa78b24c1 100644 --- a/include/tadah/core/utils.h +++ b/include/tadah/core/utils.h @@ -12,6 +12,8 @@ #include <tuple> #include <cstddef> +namespace tadah { +namespace core { /** Vector printing to streams */ template <typename T> @@ -314,4 +316,6 @@ std::vector<histogram_bin> generate_2d_histogram( const std::vector<std::pair<double,double>>& data_2d, int x_bins, int y_bins); + +}} #endif diff --git a/include/tadah/core/vec3d.h b/include/tadah/core/vec3d.h index 0a7a0a7154d9ccc0291ee3652454e9cd3859e993..2e38a4c2127481f9f675d401b4cfe0afec7ff6a8 100644 --- a/include/tadah/core/vec3d.h +++ b/include/tadah/core/vec3d.h @@ -7,6 +7,9 @@ #include <string> #include <limits> +namespace tadah { +namespace core { + class Vec3d { public: double v[3] = { }; @@ -138,4 +141,6 @@ public: // We'll declare multiply by Matrix3d in separate file "vec3d_math_inl.h" }; + +}} #endif diff --git a/include/tadah/core/vec3d_math_inl.h b/include/tadah/core/vec3d_math_inl.h index b0e70c05ef9e56124c4e22dc58e1af4aa6607c85..66a91ae49c54876c01c85578c80d47da181333c7 100644 --- a/include/tadah/core/vec3d_math_inl.h +++ b/include/tadah/core/vec3d_math_inl.h @@ -5,6 +5,9 @@ #include <tadah/core/vec3d.h> #include <tadah/core/maths.h> +namespace tadah { +namespace core { + /** * @brief Inline method: multiply in-place by a 3x3 matrix => x = M*x * @@ -24,5 +27,6 @@ inline Vec3d& transformBy(Vec3d &v, const Matrix3d &M) return v; } +}} #endif // VEC3D_MATH_INL_H diff --git a/include/tadah/core/vec3d_soa_const_view.h b/include/tadah/core/vec3d_soa_const_view.h index bb3fe2bf000da2a5d7efe422a4ff10b4b69704b8..a55d1a6169c7b79d5eb76e793e2520e29da3361a 100644 --- a/include/tadah/core/vec3d_soa_const_view.h +++ b/include/tadah/core/vec3d_soa_const_view.h @@ -8,6 +8,9 @@ #include <iostream> #include <tadah/core/vec3d.h> // for returning Vec3d in operations (e.g. cross, etc.) +namespace tadah { +namespace core { + /** * @class Vec3dSoAConstView * @brief A read-only "view" into SoA data for x,y,z. @@ -148,5 +151,7 @@ public: } }; +}} + #endif // VEC3D_SOA_CONST_VIEW_H diff --git a/include/tadah/core/vec3d_soa_view.h b/include/tadah/core/vec3d_soa_view.h index b8f530b0ef8a2e64ee3843c45988e8d12f2a7acc..4c25f6b550ef70a43775af86416354abb12386cf 100644 --- a/include/tadah/core/vec3d_soa_view.h +++ b/include/tadah/core/vec3d_soa_view.h @@ -8,6 +8,9 @@ #include <iostream> #include <tadah/core/vec3d.h> // so we can return Vec3d in operations (e.g. operator+, cross, etc.) +namespace tadah { +namespace core { + /** * @class Vec3dSoAView * @brief A non-owning "view" into SoA data for x,y,z. @@ -179,5 +182,6 @@ public: } }; +}} #endif // VEC3D_SOA_VIEW_H diff --git a/include/tadah/core/vec3d_soa_view_math_inl.h b/include/tadah/core/vec3d_soa_view_math_inl.h index f010117dd9ed33dc51790e5abc5121bd7235422b..7f1ced54381e64b69e495b2f14958563a60a1d03 100644 --- a/include/tadah/core/vec3d_soa_view_math_inl.h +++ b/include/tadah/core/vec3d_soa_view_math_inl.h @@ -5,6 +5,9 @@ #include <tadah/core/vec3d_soa_view.h> #include <tadah/core/maths.h> +namespace tadah { +namespace core { + /** * @brief Inline function to multiply Vec3dSoAView by a 3x3 matrix in place: * x => M*x @@ -26,5 +29,6 @@ inline Vec3dSoAView& transformBy(Vec3dSoAView &view, const Matrix3d &M) return view; } +}} #endif // VEC3D_SOA_VIEW_MATH_INL_H diff --git a/include/tadah/core/vec3dview.h b/include/tadah/core/vec3dview.h index 50b2c54bb513805820e91f775be8d8a24fe34620..fa057a8d92e8ed1bf3227e7b91f30ed0282b87e4 100644 --- a/include/tadah/core/vec3dview.h +++ b/include/tadah/core/vec3dview.h @@ -8,6 +8,9 @@ #include <tadah/core/vec3d.h> // so we can return Vec3d from some operations +namespace tadah { +namespace core { + /** * @class Vec3dView * @brief A non-owning "view" of 3 doubles in memory, providing @@ -194,5 +197,6 @@ public: } }; +}} #endif // VEC3DVIEW_H diff --git a/include/tadah/core/vec3dview_math_inl.h b/include/tadah/core/vec3dview_math_inl.h index 4e5fe19235ea9bd0a96ff75bd6f4b1591bb4d309..e817e29d05f508f5fa1453346c55d08d48ffbc0c 100644 --- a/include/tadah/core/vec3dview_math_inl.h +++ b/include/tadah/core/vec3dview_math_inl.h @@ -5,6 +5,9 @@ #include <tadah/core/vec3dview.h> #include <tadah/core/maths.h> +namespace tadah { +namespace core { + /** * @brief Inline multiply: x = M*x for a pointer-based Vec3dView */ @@ -21,5 +24,6 @@ inline Vec3dView& transformBy(Vec3dView &v, const Matrix3d &M) return v; } +}} #endif // VEC3DVIEW_MATH_INL_H diff --git a/src/aed_type.cpp b/src/aed_type.cpp index 7bf86d784ddf58a67ebcf88832b36bcc7ae3d366..fb843d0b249dd1cc70c7bd9c7b176f78c458899c 100644 --- a/src/aed_type.cpp +++ b/src/aed_type.cpp @@ -1,5 +1,8 @@ #include <tadah/core/aed_type.h> +namespace tadah { +namespace core { + aed_type& aed_type::operator=(const aed_type& other) { if (this != &other) { if (allocated_) { @@ -89,3 +92,5 @@ void aed_type::random(int seed, double lower, double upper) { (*this)[i] = unif(engine); } } + +}} diff --git a/src/aeds_type.cpp b/src/aeds_type.cpp index 70b91c48fabee66bf71fd4e87e724b4657e73415..6ea8c6f9ff98a02541140c401ae75faf247b7ecd 100644 --- a/src/aeds_type.cpp +++ b/src/aeds_type.cpp @@ -1,6 +1,9 @@ #include <tadah/core/aeds_type.h> #include <tadah/core/aed_type.h> +namespace tadah { +namespace core { + aeds_type2::aeds_type2() {} aeds_type2::aeds_type2(size_t dim, size_t natoms) @@ -115,4 +118,5 @@ std::ostream& operator<<(std::ostream& os, const aeds_type2& v) { } return os; } +}} diff --git a/src/config.cpp b/src/config.cpp index 345e7416c6f912ef0f4a757dde25bccb0785f785..560e1c7051cb833b899ed7012058e8509afe2b44 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,6 +1,9 @@ #include <tadah/core/config.h> #include <sstream> +namespace tadah { +namespace core { + Config::Config() { read_tadah_configuration(); @@ -343,3 +346,5 @@ void Config::deserialize(const std::vector<char>& buffer) { } +} +} diff --git a/src/element.cpp b/src/element.cpp index 47422366d02d5df4c1041fec991c33e72dfac0e7..1c6e90d5a8ff9b0e919fd75c151b029a8fc45d1e 100644 --- a/src/element.cpp +++ b/src/element.cpp @@ -1,6 +1,9 @@ #include <tadah/core/element.h> #include <tadah/core/periodic_table.h> +namespace tadah { +namespace core { + void Element::check(const std::string &symbol, const int Z) { if (symbol.size() > 2) throw std::runtime_error("Wrong symbol: "+symbol); @@ -34,18 +37,22 @@ bool Element::operator<(const Element& other) const { bool Element::operator>(const Element& other) const { return this->Z > other.Z; } +int Element::toAtomicNumber(const std::string &symbol) { + return PeriodicTable::find_by_symbol(symbol).Z; +} +std::string Element::toSymbol(int Z) { + return PeriodicTable::find_by_Z(Z).symbol; +} + +}} + namespace std { template <> - struct hash<Element> { - size_t operator()(const Element &e) const { + struct hash<tadah::core::Element> { + size_t operator()(const tadah::core::Element &e) const { return hash<int>()(e.Z); } }; } -int Element::toAtomicNumber(const std::string &symbol) { - return PeriodicTable::find_by_symbol(symbol).Z; -} -std::string Element::toSymbol(int Z) { - return PeriodicTable::find_by_Z(Z).symbol; -} + diff --git a/src/fd_type.cpp b/src/fd_type.cpp index 892ee888b20f74b30171f72a0a96178fc01bbfd5..a47826156a8025aabc59874ed23e81c0bf8a0c8a 100644 --- a/src/fd_type.cpp +++ b/src/fd_type.cpp @@ -1,6 +1,9 @@ #include <tadah/core/fd_type.h> #include <tadah/core/aed_type.h> +namespace tadah { +namespace core { + fd_type::fd_type(const fd_type& other) : rows_(other.rows_) { data_ = static_cast<double*>(malloc(3 * rows_ * sizeof(double))); @@ -127,3 +130,4 @@ bool fd_type::operator==(const fd_type& v1) const { return isApprox(v1, EPS); } +}} diff --git a/src/lapack.cpp b/src/lapack.cpp index 916256e8ff1f4706f4d3bd76165d114c2c1c26c6..0ad89dc397812a22961bfc50b6cb61e760b58022 100644 --- a/src/lapack.cpp +++ b/src/lapack.cpp @@ -1,4 +1,8 @@ #include <tadah/core/lapack.h> + +namespace tadah { +namespace core { + // Some usefull operations on vectors and matrices aed_type T_dgemv(Matrix &mat, aed_type &vec, char trans) { int m = mat.rows(); @@ -75,3 +79,5 @@ double condition_number(Matrix &A, char uplo) { return 1/rcond; } + +}} diff --git a/src/maths.cpp b/src/maths.cpp index 8026e4586bf9977166e0d096ff7bf107d3df4121..0f7681e33a9f0538b38bf1213f3beb421f4ae952 100644 --- a/src/maths.cpp +++ b/src/maths.cpp @@ -1 +1,5 @@ #include <tadah/core/maths.h> + +namespace tadah { +namespace core { +}} diff --git a/src/normaliser_core.cpp b/src/normaliser_core.cpp index f3f9b0c44940cc5d6b31832993b4a2c86c6c119a..824d54c2276fd0b752b6cdbe2c4920258982b289 100644 --- a/src/normaliser_core.cpp +++ b/src/normaliser_core.cpp @@ -1,5 +1,8 @@ #include <tadah/core/normaliser_core.h> +namespace tadah { +namespace core { + Normaliser_Core::Normaliser_Core () {} Normaliser_Core::Normaliser_Core (Config &c): bias(c.get<bool>("BIAS")), @@ -65,3 +68,5 @@ void Normaliser_Core::normalise(fd_type &fd, size_t k) { } } } + +}} diff --git a/src/periodic_table.cpp b/src/periodic_table.cpp index 048863139eb48c0e032a1294ac14b8bc4c7fdf5b..40a8f0014d7233b8acb6db9b8fe6fc5ed1cdbdee 100644 --- a/src/periodic_table.cpp +++ b/src/periodic_table.cpp @@ -1,5 +1,8 @@ #include <tadah/core/periodic_table.h> +namespace tadah { +namespace core { + // Definition of static members std::map<std::string, Element> PeriodicTable::symbol; std::map<std::string, Element> PeriodicTable::name; @@ -351,3 +354,5 @@ void PeriodicTable::init_masses() { masses[117] = 294.000; // Tennessine masses[118] = 294.000; // Oganesson } + +}} diff --git a/src/utils.cpp b/src/utils.cpp index 771432552bb25cd726b58a2e58a42e32494c34ee..c306192628b3eeab4d7f00a28422d9be2dafe950 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -8,6 +8,9 @@ #include <cmath> #include <limits> +namespace tadah { +namespace core { + v_type logspace(double start, double stop, int num, double base) { if (start==0) { throw std::runtime_error("log series cannot start at 0\n"); @@ -306,3 +309,5 @@ std::vector<histogram_bin> generate_2d_histogram( return histogram_bins; } + +}}