From e2bb44032f785b4347e005b58f7551ef9f69d254 Mon Sep 17 00:00:00 2001 From: "G. P. Mueller" Date: Sun, 1 May 2022 22:57:46 +0200 Subject: [PATCH 1/2] Core: simplification and cleanup in `Method`s - remove all methods' pre-iteration hook - rename `Hook_Post_Iteration` to `Post_Iteration_Hook` - move `Method::Initialize` to `Method_Solver::Initialize_Solver` - moved initialization of `noi` and `nos` to `Method` constructor and moved the `Initialize_Solver` call to only occur in the `Method_s - replaced some throwing functions in `Method` with deleted (declared `= 0`), namely those which _have to_ be overridden anyways - improved some initializations - replaced some `new`s in `std::shared_ptr` constructors with `std::make_shared` - added `const` to some `auto &`s --- core/include/engine/Indexing.hpp | 34 ++++----- core/include/engine/Method.hpp | 33 +++----- core/include/engine/Method_EMA.hpp | 9 +-- core/include/engine/Method_GNEB.hpp | 4 +- core/include/engine/Method_LLG.hpp | 4 +- core/include/engine/Method_MC.hpp | 6 +- core/include/engine/Method_MMF.hpp | 4 +- core/include/engine/Method_Solver.hpp | 29 ++++--- core/include/engine/Solver_Depondt.hpp | 4 +- core/include/engine/Solver_Heun.hpp | 10 +-- core/include/engine/Solver_LBFGS_Atlas.hpp | 2 +- core/include/engine/Solver_LBFGS_OSO.hpp | 2 +- core/include/engine/Solver_RK4.hpp | 14 ++-- core/include/engine/Solver_SIB.hpp | 4 +- core/include/engine/Solver_VP.hpp | 4 +- core/include/engine/Solver_VP_OSO.hpp | 4 +- core/src/Spirit/Simulation.cpp | 4 +- core/src/engine/Method.cpp | 88 ++++++---------------- core/src/engine/Method_EMA.cpp | 17 +---- core/src/engine/Method_GNEB.cpp | 16 +--- core/src/engine/Method_LLG.cpp | 19 +---- core/src/engine/Method_MC.cpp | 10 +-- core/src/engine/Method_MMF.cpp | 14 +--- 23 files changed, 104 insertions(+), 231 deletions(-) diff --git a/core/include/engine/Indexing.hpp b/core/include/engine/Indexing.hpp index 7bde54d08..ab4ec3ee9 100644 --- a/core/include/engine/Indexing.hpp +++ b/core/include/engine/Indexing.hpp @@ -13,14 +13,14 @@ namespace Indexing inline int idx_from_translations( const intfield & n_cells, const int n_cell_atoms, const std::array & translations ) { - auto & Na = n_cells[0]; - auto & Nb = n_cells[1]; - auto & Nc = n_cells[2]; - auto & N = n_cell_atoms; + const auto & Na = n_cells[0]; + const auto & Nb = n_cells[1]; + const auto & Nc = n_cells[2]; + const auto & N = n_cell_atoms; - auto & da = translations[0]; - auto & db = translations[1]; - auto & dc = translations[2]; + const auto & da = translations[0]; + const auto & db = translations[1]; + const auto & dc = translations[2]; return da * N + db * N * Na + dc * N * Na * Nb; } @@ -61,10 +61,10 @@ inline int idx_from_translations( const intfield & n_cells, const int n_cell_atoms, const std::array & translations_i, const std::array & translations ) { - auto & Na = n_cells[0]; - auto & Nb = n_cells[1]; - auto & Nc = n_cells[2]; - auto & N = n_cell_atoms; + const auto & Na = n_cells[0]; + const auto & Nb = n_cells[1]; + const auto & Nc = n_cells[2]; + const auto & N = n_cell_atoms; int da = translations_i[0] + translations[0]; int db = translations_i[1] + translations[1]; @@ -220,9 +220,9 @@ __inline__ __device__ int cu_idx_from_pair( return -1; // Number of cells - auto & Na = n_cells[0]; - auto & Nb = n_cells[1]; - auto & Nc = n_cells[2]; + const auto & Na = n_cells[0]; + const auto & Nb = n_cells[1]; + const auto & Nc = n_cells[2]; // Invalid index if translations reach out over the lattice bounds if( std::abs( pair.translations[0] ) > Na || std::abs( pair.translations[1] ) > Nb @@ -360,9 +360,9 @@ inline int idx_from_pair( return -1; // Number of cells - auto & Na = n_cells[0]; - auto & Nb = n_cells[1]; - auto & Nc = n_cells[2]; + const auto & Na = n_cells[0]; + const auto & Nb = n_cells[1]; + const auto & Nc = n_cells[2]; // Invalid index if translations reach out over the lattice bounds if( std::abs( pair.translations[0] ) > Na || std::abs( pair.translations[1] ) > Nb diff --git a/core/include/engine/Method.hpp b/core/include/engine/Method.hpp index 1726474a4..71c5dabed 100644 --- a/core/include/engine/Method.hpp +++ b/core/include/engine/Method.hpp @@ -27,7 +27,7 @@ class Method * The constructor does not allocate any large arrays. The solvers should allocate * what they need in Solver_Initialise. */ - Method( std::shared_ptr parameters, int idx_img, int idx_chain ); + Method( const std::shared_ptr & parameters, int noi, int nos, int idx_img, int idx_chain ); virtual ~Method() = default; @@ -54,22 +54,22 @@ class Method */ virtual scalar getForceMaxAbsComponent() final; + // Maximum norm of the torque - needs to be updated at each calculation + virtual scalar getTorqueMaxNorm() final; + /* * Maximum of the absolutes of all components of the force for all images the method uses * The default is that this returns simply {getForceMaxAbsComponent()} */ virtual std::vector getForceMaxAbsComponent_All(); - // Maximum norm of the torque - needs to be updated at each calculation - virtual scalar getTorqueMaxNorm() final; - // Maximum of the norm of the torque for all images the method uses virtual std::vector getTorqueMaxNorm_All(); // ------------------------------------------------------ // Method name as string - virtual std::string Name(); + virtual std::string Name() = 0; // Solver name as string virtual std::string SolverName(); @@ -78,37 +78,28 @@ class Method // ------------------------------------------------------ // One iteration of the Method - virtual void Iteration(); + virtual void Iteration() = 0; - // Initialize arrays etc. before `Iterate` starts - virtual void Initialize(); // Finalize (e.g. output informaiton) after `Iterate` has finished - virtual void Finalize(); + virtual void Finalize() = 0; // Log messages at Start, after Steps and at End of Iterate - virtual void Message_Start(); - virtual void Message_Step(); - virtual void Message_End(); - - /* - * A hook into `Iterate` before an Iteration. - * Override this function if special actions are needed - * before each `Solver_Iteration` - */ - virtual void Hook_Pre_Iteration(); + virtual void Message_Start() = 0; + virtual void Message_Step() = 0; + virtual void Message_End() = 0; /* * A hook into `Iterate` after an Iteration. * Override this function if special actions are needed * after each `Solver_Iteration` */ - virtual void Hook_Post_Iteration(); + virtual void Post_Iteration_Hook() = 0; /* * Save current data * Override to specialize what a Method should save */ - virtual void Save_Current( std::string starttime, int iteration, bool initial = false, bool final = false ); + virtual void Save_Current( std::string starttime, int iteration, bool initial = false, bool final = false ) = 0; /* * Lock systems in order to prevent otherwise access diff --git a/core/include/engine/Method_EMA.hpp b/core/include/engine/Method_EMA.hpp index ee95c8987..b3b29c90c 100644 --- a/core/include/engine/Method_EMA.hpp +++ b/core/include/engine/Method_EMA.hpp @@ -23,9 +23,6 @@ class Method_EMA : public Method // Method name as string std::string Name() override; - // Solver name as string - std::string SolverName() override; - private: // Iteration does one time step of the oscillation // S(t) = S(0) + cos(omega * t) * direction @@ -33,13 +30,9 @@ class Method_EMA : public Method // Save the current Step's Data: spins and energy void Save_Current( std::string starttime, int iteration, bool initial = false, bool final = false ) override; - // A hook into the Method before an Iteration of the Solver - void Hook_Pre_Iteration() override; // A hook into the Method after an Iteration of the Solver - void Hook_Post_Iteration() override; + void Post_Iteration_Hook() override; - // Sets iteration_allowed to false for the corresponding method - void Initialize() override; // Sets iteration_allowed to false for the corresponding method void Finalize() override; diff --git a/core/include/engine/Method_GNEB.hpp b/core/include/engine/Method_GNEB.hpp index fae832b89..a515386e6 100644 --- a/core/include/engine/Method_GNEB.hpp +++ b/core/include/engine/Method_GNEB.hpp @@ -46,10 +46,8 @@ class Method_GNEB : public Method_Solver // Save the current Step's Data: images and images' energies and reaction coordinates void Save_Current( std::string starttime, int iteration, bool initial = false, bool final = false ) override; - // A hook into the Method before an Iteration of the Solver - void Hook_Pre_Iteration() override; // A hook into the Method after an Iteration of the Solver - void Hook_Post_Iteration() override; + void Post_Iteration_Hook() override; // A helper method that calculates the interpolated energies, split up into the different energy contributions void Calculate_Interpolated_Energy_Contributions(); diff --git a/core/include/engine/Method_LLG.hpp b/core/include/engine/Method_LLG.hpp index 95369e38e..02c00f460 100644 --- a/core/include/engine/Method_LLG.hpp +++ b/core/include/engine/Method_LLG.hpp @@ -43,9 +43,7 @@ class Method_LLG : public Method_Solver // Save the current Step's Data: spins and energy void Save_Current( std::string starttime, int iteration, bool initial = false, bool final = false ) override; // A hook into the Method before an Iteration of the Solver - void Hook_Pre_Iteration() override; - // A hook into the Method after an Iteration of the Solver - void Hook_Post_Iteration() override; + void Post_Iteration_Hook() override; // Sets iteration_allowed to false for the corresponding method void Finalize() override; diff --git a/core/include/engine/Method_MC.hpp b/core/include/engine/Method_MC.hpp index 9c51809d6..ac5f72593 100644 --- a/core/include/engine/Method_MC.hpp +++ b/core/include/engine/Method_MC.hpp @@ -32,13 +32,9 @@ class Method_MC : public Method // Save the current Step's Data: spins and energy void Save_Current( std::string starttime, int iteration, bool initial = false, bool final = false ) override; - // A hook into the Method before an Iteration of the Solver - void Hook_Pre_Iteration() override; // A hook into the Method after an Iteration of the Solver - void Hook_Post_Iteration() override; + void Post_Iteration_Hook() override; - // Sets iteration_allowed to false for the corresponding method - void Initialize() override; // Sets iteration_allowed to false for the corresponding method void Finalize() override; diff --git a/core/include/engine/Method_MMF.hpp b/core/include/engine/Method_MMF.hpp index e6d9537bf..56468af48 100644 --- a/core/include/engine/Method_MMF.hpp +++ b/core/include/engine/Method_MMF.hpp @@ -32,10 +32,8 @@ class Method_MMF : public Method_Solver // Save the current Step's Data: images and images' energies and reaction coordinates void Save_Current( std::string starttime, int iteration, bool initial = false, bool final = false ) override; - // A hook into the Method before an Iteration of the Solver - void Hook_Pre_Iteration() override; // A hook into the Method after an Iteration of the Solver - void Hook_Post_Iteration() override; + void Post_Iteration_Hook() override; // Sets iteration_allowed to false void Finalize() override; diff --git a/core/include/engine/Method_Solver.hpp b/core/include/engine/Method_Solver.hpp index bd5b6aa66..10e0b4070 100644 --- a/core/include/engine/Method_Solver.hpp +++ b/core/include/engine/Method_Solver.hpp @@ -50,12 +50,13 @@ class Method_Solver : public Method { public: // Constructor to be used in derived classes - Method_Solver( std::shared_ptr parameters, int idx_img, int idx_chain ) - : Method( parameters, idx_img, idx_chain ) + Method_Solver( std::shared_ptr parameters, int noi, int nos, int idx_img, int idx_chain ) + : Method( parameters, noi, nos, idx_img, idx_chain ) { + this->Initialize_Solver(); } - virtual ~Method_Solver() = default; + ~Method_Solver() override = default; // // `Iterate` uses the `Solver_Iteration` function to evolve given systems according to the // // `Calculate_Force` implementation of the Method-Subclass. @@ -65,11 +66,11 @@ class Method_Solver : public Method // virtual void Iterate() override; // Solver name as string - virtual std::string SolverName() override; - virtual std::string SolverFullName() override; + std::string SolverName() override; + std::string SolverFullName() override; // Iteration represents one iteration of a certain Solver - virtual void Iteration() override; + void Iteration() override; protected: // Prepare random numbers for thermal fields, if needed @@ -124,14 +125,14 @@ class Method_Solver : public Method return Method::ContinueIterating() && !this->Converged(); } - // Initialise contains the initialisations of arrays etc. for a certain solver - virtual void Initialize() override; - virtual void Finalize() override; + // Initialize arrays etc. before `Iterate` starts + void Initialize_Solver(); + void Finalize() override; // Log message blocks - virtual void Message_Start() override; - virtual void Message_Step() override; - virtual void Message_End() override; + void Message_Start() override; + void Message_Step() override; + void Message_End() override; //////////// DEPONDT //////////////////////////////////////////////////////////// // Temporaries for virtual forces @@ -238,10 +239,6 @@ bool Method_Solver::Converged() return converged; } -// Default implementation: do nothing -template -void Method_Solver::Initialize(){}; - // Default implementation: do nothing template void Method_Solver::Finalize(){}; diff --git a/core/include/engine/Solver_Depondt.hpp b/core/include/engine/Solver_Depondt.hpp index 25ed98c3e..c63a6265b 100644 --- a/core/include/engine/Solver_Depondt.hpp +++ b/core/include/engine/Solver_Depondt.hpp @@ -1,5 +1,5 @@ template<> -inline void Method_Solver::Initialize() +inline void Method_Solver::Initialize_Solver() { this->forces = std::vector( this->noi, vectorfield( this->nos, { 0, 0, 0 } ) ); this->forces_virtual = std::vector( this->noi, vectorfield( this->nos, { 0, 0, 0 } ) ); @@ -13,7 +13,7 @@ inline void Method_Solver::Initialize() this->configurations_predictor = std::vector>( this->noi ); for( int i = 0; i < this->noi; i++ ) - configurations_predictor[i] = std::shared_ptr( new vectorfield( this->nos, { 0, 0, 0 } ) ); + configurations_predictor[i] = std::make_shared( this->nos ); this->temp1 = vectorfield( this->nos, { 0, 0, 0 } ); } diff --git a/core/include/engine/Solver_Heun.hpp b/core/include/engine/Solver_Heun.hpp index 01aa0b654..09f103761 100644 --- a/core/include/engine/Solver_Heun.hpp +++ b/core/include/engine/Solver_Heun.hpp @@ -1,5 +1,5 @@ template<> -inline void Method_Solver::Initialize() +inline void Method_Solver::Initialize_Solver() { this->forces = std::vector( this->noi, vectorfield( this->nos, { 0, 0, 0 } ) ); this->forces_virtual = std::vector( this->noi, vectorfield( this->nos, { 0, 0, 0 } ) ); @@ -7,13 +7,13 @@ inline void Method_Solver::Initialize() this->forces_predictor = std::vector( this->noi, vectorfield( this->nos, { 0, 0, 0 } ) ); this->forces_virtual_predictor = std::vector( this->noi, vectorfield( this->nos, { 0, 0, 0 } ) ); - this->configurations_temp = std::vector>( this->noi ); + this->configurations_temp.resize( this->noi ); for( int i = 0; i < this->noi; i++ ) - configurations_temp[i] = std::shared_ptr( new vectorfield( this->nos ) ); + configurations_temp[i] = std::make_shared( this->nos ); - this->configurations_predictor = std::vector>( this->noi ); + this->configurations_predictor.resize( this->noi ); for( int i = 0; i < this->noi; i++ ) - configurations_predictor[i] = std::shared_ptr( new vectorfield( this->nos ) ); + configurations_predictor[i] = std::make_shared( this->nos ); this->temp1 = vectorfield( this->nos, { 0, 0, 0 } ); } diff --git a/core/include/engine/Solver_LBFGS_Atlas.hpp b/core/include/engine/Solver_LBFGS_Atlas.hpp index ca04a3fae..599dcad3b 100644 --- a/core/include/engine/Solver_LBFGS_Atlas.hpp +++ b/core/include/engine/Solver_LBFGS_Atlas.hpp @@ -11,7 +11,7 @@ using namespace Utility; template<> -inline void Method_Solver::Initialize() +inline void Method_Solver::Initialize_Solver() { this->n_lbfgs_memory = 3; // how many updates the solver tracks to estimate the hessian this->atlas_updates = std::vector>( diff --git a/core/include/engine/Solver_LBFGS_OSO.hpp b/core/include/engine/Solver_LBFGS_OSO.hpp index 1de19ccd2..a2eb65030 100644 --- a/core/include/engine/Solver_LBFGS_OSO.hpp +++ b/core/include/engine/Solver_LBFGS_OSO.hpp @@ -10,7 +10,7 @@ using namespace Utility; template<> -inline void Method_Solver::Initialize() +inline void Method_Solver::Initialize_Solver() { this->n_lbfgs_memory = 3; // how many previous iterations are stored in the memory this->delta_a = std::vector>( diff --git a/core/include/engine/Solver_RK4.hpp b/core/include/engine/Solver_RK4.hpp index ff1c48ef6..29c45865e 100644 --- a/core/include/engine/Solver_RK4.hpp +++ b/core/include/engine/Solver_RK4.hpp @@ -1,5 +1,5 @@ template<> -inline void Method_Solver::Initialize() +inline void Method_Solver::Initialize_Solver() { this->forces = std::vector( this->noi, vectorfield( this->nos, { 0, 0, 0 } ) ); this->forces_virtual = std::vector( this->noi, vectorfield( this->nos, { 0, 0, 0 } ) ); @@ -9,27 +9,27 @@ inline void Method_Solver::Initialize() this->configurations_temp = std::vector>( this->noi ); for( int i = 0; i < this->noi; i++ ) - this->configurations_temp[i] = std::shared_ptr( new vectorfield( this->nos ) ); + this->configurations_temp[i] = std::make_shared( this->nos ); this->configurations_predictor = std::vector>( this->noi ); for( int i = 0; i < this->noi; i++ ) - this->configurations_predictor[i] = std::shared_ptr( new vectorfield( this->nos ) ); + this->configurations_predictor[i] = std::make_shared( this->nos ); this->configurations_k1 = std::vector>( this->noi ); for( int i = 0; i < this->noi; i++ ) - this->configurations_k1[i] = std::shared_ptr( new vectorfield( this->nos ) ); + this->configurations_k1[i] = std::make_shared( this->nos ); this->configurations_k2 = std::vector>( this->noi ); for( int i = 0; i < this->noi; i++ ) - this->configurations_k2[i] = std::shared_ptr( new vectorfield( this->nos ) ); + this->configurations_k2[i] = std::make_shared( this->nos ); this->configurations_k3 = std::vector>( this->noi ); for( int i = 0; i < this->noi; i++ ) - this->configurations_k3[i] = std::shared_ptr( new vectorfield( this->nos ) ); + this->configurations_k3[i] = std::make_shared( this->nos ); this->configurations_k4 = std::vector>( this->noi ); for( int i = 0; i < this->noi; i++ ) - this->configurations_k4[i] = std::shared_ptr( new vectorfield( this->nos ) ); + this->configurations_k4[i] = std::make_shared( this->nos ); this->temp1 = vectorfield( this->nos, { 0, 0, 0 } ); } diff --git a/core/include/engine/Solver_SIB.hpp b/core/include/engine/Solver_SIB.hpp index f95a894aa..773722c4a 100644 --- a/core/include/engine/Solver_SIB.hpp +++ b/core/include/engine/Solver_SIB.hpp @@ -1,5 +1,5 @@ template<> -inline void Method_Solver::Initialize() +inline void Method_Solver::Initialize_Solver() { this->forces = std::vector( this->noi, vectorfield( this->nos ) ); // [noi][nos] this->forces_virtual = std::vector( this->noi, vectorfield( this->nos ) ); // [noi][nos] @@ -9,7 +9,7 @@ inline void Method_Solver::Initialize() this->configurations_predictor = std::vector>( this->noi ); for( int i = 0; i < this->noi; i++ ) - configurations_predictor[i] = std::shared_ptr( new vectorfield( this->nos ) ); + configurations_predictor[i] = std::make_shared( this->nos ); } /* diff --git a/core/include/engine/Solver_VP.hpp b/core/include/engine/Solver_VP.hpp index e7461487e..436355874 100644 --- a/core/include/engine/Solver_VP.hpp +++ b/core/include/engine/Solver_VP.hpp @@ -1,14 +1,14 @@ #include template<> -inline void Method_Solver::Initialize() +inline void Method_Solver::Initialize_Solver() { this->forces = std::vector( this->noi, vectorfield( this->nos, { 0, 0, 0 } ) ); this->forces_virtual = std::vector( this->noi, vectorfield( this->nos, { 0, 0, 0 } ) ); this->configurations_temp = std::vector>( this->noi ); for( int i = 0; i < this->noi; i++ ) - configurations_temp[i] = std::shared_ptr( new vectorfield( this->nos ) ); + configurations_temp[i] = std::make_shared( this->nos ); this->velocities = std::vector( this->noi, vectorfield( this->nos, Vector3::Zero() ) ); // [noi][nos] this->velocities_previous = velocities; // [noi][nos] diff --git a/core/include/engine/Solver_VP_OSO.hpp b/core/include/engine/Solver_VP_OSO.hpp index f6132fac5..c297bdc14 100644 --- a/core/include/engine/Solver_VP_OSO.hpp +++ b/core/include/engine/Solver_VP_OSO.hpp @@ -1,12 +1,12 @@ template<> -inline void Method_Solver::Initialize() +inline void Method_Solver::Initialize_Solver() { this->forces = std::vector( this->noi, vectorfield( this->nos, { 0, 0, 0 } ) ); this->forces_virtual = std::vector( this->noi, vectorfield( this->nos, { 0, 0, 0 } ) ); this->configurations_temp = std::vector>( this->noi ); for( int i = 0; i < this->noi; i++ ) - configurations_temp[i] = std::shared_ptr( new vectorfield( this->nos ) ); + configurations_temp[i] = std::make_shared( this->nos ); this->velocities = std::vector( this->noi, vectorfield( this->nos, Vector3::Zero() ) ); // [noi][nos] this->velocities_previous = velocities; // [noi][nos] diff --git a/core/src/Spirit/Simulation.cpp b/core/src/Spirit/Simulation.cpp index a218b08a6..b0d8d02e2 100644 --- a/core/src/Spirit/Simulation.cpp +++ b/core/src/Spirit/Simulation.cpp @@ -484,12 +484,10 @@ try method->Lock(); for( int i = 0; i < N; i++ ) { - // Pre-iteration hook - method->Hook_Pre_Iteration(); // Do one single Iteration method->Iteration(); // Post-iteration hook - method->Hook_Post_Iteration(); + method->Post_Iteration_Hook(); // Recalculate FPS method->t_iterations.pop_front(); diff --git a/core/src/engine/Method.cpp b/core/src/engine/Method.cpp index 56531dbf4..84eec89e0 100644 --- a/core/src/engine/Method.cpp +++ b/core/src/engine/Method.cpp @@ -13,17 +13,20 @@ using namespace Utility; namespace Engine { -Method::Method( std::shared_ptr parameters, int idx_img, int idx_chain ) +Method::Method( + const std::shared_ptr & parameters, int noi, int nos, int idx_img, int idx_chain ) : iteration( 0 ), step( 0 ), idx_image( idx_img ), idx_chain( idx_chain ), - parameters( parameters ), - n_iterations_amortize( parameters->n_iterations_amortize ) + noi( noi ), + nos( nos ), + n_iterations_amortize( parameters->n_iterations_amortize ), + SenderName( Log_Sender::All ), + max_torque( 0 ), + force_max_abs_component( 0 ), + parameters( parameters ) { - // Sender name for log messages - this->SenderName = Log_Sender::All; - this->history_iteration = std::vector(); this->history_max_torque = std::vector(); this->history_energy = std::vector(); @@ -35,10 +38,7 @@ Method::Method( std::shared_ptr parameters, int idx_img this->n_iterations_log = this->n_iterations; this->n_log = this->n_iterations / this->n_iterations_log; - if( n_iterations_amortize > n_iterations_log ) - { - n_iterations_amortize = n_iterations_log; - } + this->n_iterations_amortize = std::min( n_iterations_log, long( n_iterations_amortize ) ); // Setup timings for( std::uint8_t i = 0; i < 7; ++i ) @@ -77,13 +77,11 @@ void Method::Iterate() // Lock Systems this->Lock(); - // Pre-iteration hook - this->Hook_Pre_Iteration(); // Do n_iterations_amortize iterations for( int i = 0; i < n_iterations_amortize; i++ ) this->Iteration(); // Post-iteration hook - this->Hook_Post_Iteration(); + this->Post_Iteration_Hook(); // Recalculate FPS this->t_iterations.pop_front(); @@ -133,7 +131,8 @@ double Method::get_simulated_time() // Not Implemented! spirit_throw( Utility::Exception_Classifier::Not_Implemented, Utility::Log_Level::Error, - "Tried to use Method::get_simulated_time() of the Method base class!" ); + "Tried to use Method::get_simulated_time() of the Method base class, meaning it was called on a method that " + "does not have a time-scale!" ); } std::int64_t Method::getWallTime() @@ -167,14 +166,6 @@ std::vector Method::getTorqueMaxNorm_All() /////////////////////////////////////////////////////////////////////////////////////////////////// ////////////// Protected functions -void Method::Initialize() {} - -void Method::Message_Start() {} -void Method::Message_Step() {} -void Method::Message_End() {} - -void Method::Iteration() {} - bool Method::ContinueIterating() { return this->iteration < this->n_iterations && this->Iterations_Allowed() && !this->StopFile_Present(); @@ -199,38 +190,6 @@ bool Method::StopFile_Present() return f.good(); } -void Method::Save_Current( std::string starttime, int iteration, bool initial, bool final ) -{ - // Not Implemented! - spirit_throw( - Exception_Classifier::Not_Implemented, Log_Level::Error, - "Tried to use Method::Save_Current() of the Method base class!" ); -} - -void Method::Hook_Pre_Iteration() -{ - // Not Implemented! - spirit_throw( - Exception_Classifier::Not_Implemented, Log_Level::Error, - "Tried to use Method::Save_Current() of the Method base class!" ); -} - -void Method::Hook_Post_Iteration() -{ - // Not Implemented! - spirit_throw( - Exception_Classifier::Not_Implemented, Log_Level::Error, - "Tried to use Method::Save_Current() of the Method base class!" ); -} - -void Method::Finalize() -{ - // Not Implemented! - spirit_throw( - Exception_Classifier::Not_Implemented, Log_Level::Error, - "Tried to use Method::Save_Current() of the Method base class!" ); -} - void Method::Lock() { for( auto & system : this->systems ) @@ -243,29 +202,24 @@ void Method::Unlock() system->Unlock(); } -std::string Method::Name() -{ - // Not Implemented! - Log( Log_Level::Error, Log_Sender::All, std::string( "Tried to use Method::Name() of the Method base class!" ) ); - return "--"; -} - // Solver name as string std::string Method::SolverName() { // Not Implemented! - Log( Log_Level::Error, Log_Sender::All, - std::string( "Tried to use Method::SolverName() of the Method base class!" ), this->idx_image, - this->idx_chain ); + spirit_throw( + Utility::Exception_Classifier::Not_Implemented, Utility::Log_Level::Error, + "Tried to use Method::SolverName() of the Method base class, meaning it was called on a method that does not " + "use any solver!" ); return "--"; } std::string Method::SolverFullName() { // Not Implemented! - Log( Log_Level::Error, Log_Sender::All, - std::string( "Tried to use Method::SolverFullname() of the Method base class!" ), this->idx_image, - this->idx_chain ); + spirit_throw( + Utility::Exception_Classifier::Not_Implemented, Utility::Log_Level::Error, + "Tried to use Method::SolverFullname() of the Method base class, meaning it was called on a method that does " + "not use any solver!" ); return "--"; } diff --git a/core/src/engine/Method_EMA.cpp b/core/src/engine/Method_EMA.cpp index bf33e5243..7cd3af45d 100644 --- a/core/src/engine/Method_EMA.cpp +++ b/core/src/engine/Method_EMA.cpp @@ -22,15 +22,12 @@ namespace Engine { Method_EMA::Method_EMA( std::shared_ptr system, int idx_img, int idx_chain ) - : Method( system->ema_parameters, idx_img, idx_chain ) + : Method( system->ema_parameters, 1, system->nos, idx_img, idx_chain ) { this->systems = std::vector>( 1, system ); this->SenderName = Log_Sender::EMA; this->parameters_ema = system->ema_parameters; - this->noi = this->systems.size(); - this->nos = this->systems[0]->nos; - // Attributes needed for applying a mode the spins this->angle = scalarfield( this->nos ); this->angle_initial = scalarfield( this->nos ); @@ -96,11 +93,7 @@ void Method_EMA::Iteration() void Method_EMA::Save_Current( std::string starttime, int iteration, bool initial, bool final ) {} -void Method_EMA::Hook_Pre_Iteration() {} - -void Method_EMA::Hook_Post_Iteration() {} - -void Method_EMA::Initialize() {} +void Method_EMA::Post_Iteration_Hook() {} void Method_EMA::Finalize() { @@ -134,10 +127,4 @@ std::string Method_EMA::Name() return "EMA"; } -// Solver name as string -std::string Method_EMA::SolverName() -{ - return "None"; -} - } // namespace Engine diff --git a/core/src/engine/Method_GNEB.cpp b/core/src/engine/Method_GNEB.cpp index c6170f703..13994b2a5 100644 --- a/core/src/engine/Method_GNEB.cpp +++ b/core/src/engine/Method_GNEB.cpp @@ -23,14 +23,12 @@ namespace Engine template Method_GNEB::Method_GNEB( std::shared_ptr chain, int idx_chain ) - : Method_Solver( chain->gneb_parameters, -1, idx_chain ), chain( chain ) + : Method_Solver( chain->gneb_parameters, chain->noi, chain->images[0]->nos, -1, idx_chain ), + chain( chain ) { this->systems = chain->images; this->SenderName = Utility::Log_Sender::GNEB; - this->noi = chain->noi; - this->nos = chain->images[0]->nos; - this->energies = std::vector( this->noi, 0 ); this->Rx = std::vector( this->noi, 0 ); @@ -63,9 +61,6 @@ Method_GNEB::Method_GNEB( std::shared_ptr chain // History // this->history = std::map>{ { "max_torque", { this->max_torque } } }; - //---- Initialise Solver-specific variables - this->Initialize(); - // Calculate Data for the border images, which will not be updated this->chain->images[0] ->UpdateEffectiveField(); // hamiltonian->Effective_Field(image, this->chain->images[0]->effective_field); @@ -404,12 +399,7 @@ bool Method_GNEB::Iterations_Allowed() } template -void Method_GNEB::Hook_Pre_Iteration() -{ -} - -template -void Method_GNEB::Hook_Post_Iteration() +void Method_GNEB::Post_Iteration_Hook() { // --- Convergence Parameter Update this->max_torque = 0; diff --git a/core/src/engine/Method_LLG.cpp b/core/src/engine/Method_LLG.cpp index 65a443f08..a962be25d 100644 --- a/core/src/engine/Method_LLG.cpp +++ b/core/src/engine/Method_LLG.cpp @@ -20,15 +20,12 @@ namespace Engine template Method_LLG::Method_LLG( std::shared_ptr system, int idx_img, int idx_chain ) - : Method_Solver( system->llg_parameters, idx_img, idx_chain ), picoseconds_passed( 0 ) + : Method_Solver( system->llg_parameters, 1, system->nos, idx_img, idx_chain ), picoseconds_passed( 0 ) { // Currently we only support a single image being iterated at once: this->systems = std::vector>( 1, system ); this->SenderName = Utility::Log_Sender::LLG; - this->noi = this->systems.size(); - this->nos = this->systems[0]->nos; - // Forces this->forces = std::vector( this->noi, vectorfield( this->nos ) ); this->forces_virtual = std::vector( this->noi, vectorfield( this->nos ) ); @@ -51,15 +48,12 @@ Method_LLG::Method_LLG( std::shared_ptr system, int i // Allocate force array // this->force = std::vector(this->noi, vectorfield(this->nos, Vector3::Zero())); // [noi][3*nos] - //---- Initialise Solver-specific variables - this->Initialize(); - // Initial force calculation s.t. it does not seem to be already converged this->Prepare_Thermal_Field(); this->Calculate_Force( this->configurations, this->forces ); this->Calculate_Force_Virtual( this->configurations, this->forces, this->forces_virtual ); - // Post iteration hook to get forceMaxAbsComponent etc - this->Hook_Post_Iteration(); + // Call post iteration hook to get forceMaxAbsComponent etc + this->Post_Iteration_Hook(); } template @@ -239,12 +233,7 @@ bool Method_LLG::Converged() } template -void Method_LLG::Hook_Pre_Iteration() -{ -} - -template -void Method_LLG::Hook_Post_Iteration() +void Method_LLG::Post_Iteration_Hook() { // Increment the time counter (picoseconds) this->picoseconds_passed += this->systems[0]->llg_parameters->dt; diff --git a/core/src/engine/Method_MC.cpp b/core/src/engine/Method_MC.cpp index 60123c9e9..37f8716ff 100644 --- a/core/src/engine/Method_MC.cpp +++ b/core/src/engine/Method_MC.cpp @@ -20,14 +20,12 @@ namespace Engine { Method_MC::Method_MC( std::shared_ptr system, int idx_img, int idx_chain ) - : Method( system->mc_parameters, idx_img, idx_chain ) + : Method( system->mc_parameters, 1, system->nos, idx_img, idx_chain ) { // Currently we only support a single image being iterated at once: this->systems = std::vector>( 1, system ); this->SenderName = Log_Sender::MC; - this->noi = this->systems.size(); - this->nos = this->systems[0]->geometry->nos; this->nos_nonvacant = this->systems[0]->geometry->nos_nonvacant; this->xi = vectorfield( this->nos, { 0, 0, 0 } ); @@ -196,11 +194,7 @@ void Method_MC::Metropolis( const vectorfield & spins_old, vectorfield & spins_n // { // } -void Method_MC::Hook_Pre_Iteration() {} - -void Method_MC::Hook_Post_Iteration() {} - -void Method_MC::Initialize() {} +void Method_MC::Post_Iteration_Hook() {} void Method_MC::Finalize() { diff --git a/core/src/engine/Method_MMF.cpp b/core/src/engine/Method_MMF.cpp index 5b2cfb0a4..596dbd10d 100644 --- a/core/src/engine/Method_MMF.cpp +++ b/core/src/engine/Method_MMF.cpp @@ -23,12 +23,10 @@ namespace Engine template Method_MMF::Method_MMF( std::shared_ptr system, int idx_chain ) - : Method_Solver( system->mmf_parameters, -1, idx_chain ) + : Method_Solver( system->mmf_parameters, 1, system->nos, -1, idx_chain ) { this->systems = std::vector>( 1, system ); this->system = system; - this->noi = this->systems.size(); - this->nos = this->systems[0]->nos; switched1 = false; switched2 = false; @@ -59,9 +57,6 @@ Method_MMF::Method_MMF( std::shared_ptr system, int i // Create shared pointers to the method's systems' spin configurations this->configurations = std::vector>( 1 ); this->configurations[0] = this->system->spins; - - //---- Initialise Solver-specific variables - this->Initialize(); } template @@ -402,12 +397,7 @@ bool Method_MMF::Converged() } template -void Method_MMF::Hook_Pre_Iteration() -{ -} - -template -void Method_MMF::Hook_Post_Iteration() +void Method_MMF::Post_Iteration_Hook() { // --- Convergence Parameter Update this->max_torque = 0; From 49821a73d4ac4ff544d706d70c2417e149532856 Mon Sep 17 00:00:00 2001 From: "G. P. Mueller" Date: Sun, 1 May 2022 23:12:46 +0200 Subject: [PATCH 2/2] Core: minor cleanup - more `const`ifying `auto &`s - replacing `push_back`s with `emplace_back`s --- core/include/engine/HTST.hpp | 2 +- core/include/engine/Manifoldmath.hpp | 1 + core/src/engine/HTST.cpp | 16 ++++----- core/src/engine/Manifoldmath.cpp | 49 ++++++++++++++-------------- core/src/engine/Manifoldmath.cu | 10 +++--- 5 files changed, 39 insertions(+), 39 deletions(-) diff --git a/core/include/engine/HTST.hpp b/core/include/engine/HTST.hpp index 23134c4b2..c40295cad 100644 --- a/core/include/engine/HTST.hpp +++ b/core/include/engine/HTST.hpp @@ -19,7 +19,7 @@ void Calculate( Data::HTST_Info & htst_info, int n_eigenmodes_keep = 0 ); // Calculate the 'a' component of the prefactor void Calculate_Perpendicular_Velocity( const vectorfield & spins, const scalarfield & mu_s, const MatrixX & hessian, const MatrixX & basis, - const MatrixX & eigenbasis, VectorX & a ); + const MatrixX & eigenbasis, VectorX & perpendicular_velocity ); // Calculate the Velocity matrix void Calculate_Dynamical_Matrix( diff --git a/core/include/engine/Manifoldmath.hpp b/core/include/engine/Manifoldmath.hpp index c7f8d0030..fff39420c 100644 --- a/core/include/engine/Manifoldmath.hpp +++ b/core/include/engine/Manifoldmath.hpp @@ -21,6 +21,7 @@ namespace Engine // TODO: cleanly separate these two cases! namespace Manifoldmath { + // Get the norm of a vectorfield (interpreted as a 3N-vector) scalar norm( const vectorfield & vf ); // Normalize a vectorfield (interpreted as a 3N-vector) diff --git a/core/src/engine/HTST.cpp b/core/src/engine/HTST.cpp index c6c036023..44e12774d 100644 --- a/core/src/engine/HTST.cpp +++ b/core/src/engine/HTST.cpp @@ -35,8 +35,8 @@ void Calculate( Data::HTST_Info & htst_info, int n_eigenmodes_keep ) const scalar epsilon = 1e-4; const scalar epsilon_force = 1e-8; - auto & image_minimum = *htst_info.minimum->spins; - auto & image_sp = *htst_info.saddle_point->spins; + const auto & image_minimum = *htst_info.minimum->spins; + const auto & image_sp = *htst_info.saddle_point->spins; int nos = image_minimum.size(); @@ -319,12 +319,12 @@ void Calculate( Data::HTST_Info & htst_info, int n_eigenmodes_keep ) scalar Calculate_Zero_Volume( const std::shared_ptr system ) { - int nos = system->geometry->nos; - auto & n_cells = system->geometry->n_cells; - auto & spins = *system->spins; - auto & spin_positions = system->geometry->positions; - auto & geometry = *system->geometry; - auto & bravais_vectors = system->geometry->bravais_vectors; + int nos = system->geometry->nos; + const auto & n_cells = system->geometry->n_cells; + const auto & spins = *system->spins; + const auto & spin_positions = system->geometry->positions; + const auto & geometry = *system->geometry; + const auto & bravais_vectors = system->geometry->bravais_vectors; // Dimensionality of the zero mode int zero_mode_dimensionality = 0; diff --git a/core/src/engine/Manifoldmath.cpp b/core/src/engine/Manifoldmath.cpp index 62f44dd19..b9e91d3d1 100644 --- a/core/src/engine/Manifoldmath.cpp +++ b/core/src/engine/Manifoldmath.cpp @@ -17,6 +17,7 @@ namespace Engine { namespace Manifoldmath { + void project_parallel( vectorfield & vf1, const vectorfield & vf2 ) { scalar proj = Vectormath::dot( vf1, vf2 ); @@ -285,8 +286,7 @@ void tangent_basis_spherical( const vectorfield & vf, MatrixX & basis ) void sparse_tangent_basis_spherical( const vectorfield & vf, SpMatrixX & basis ) { - typedef Eigen::Triplet T; - std::vector triplet_list; + std::vector> triplet_list; triplet_list.reserve( vf.size() * 3 ); Vector3 tmp, etheta, ephi, res; @@ -297,29 +297,29 @@ void sparse_tangent_basis_spherical( const vectorfield & vf, SpMatrixX & basis ) tmp = Vector3{ 1, 0, 0 }; res = ( tmp - tmp.dot( vf[i] ) * vf[i] ).normalized(); - triplet_list.push_back( T( 3 * i, 2 * i, res[0] ) ); - triplet_list.push_back( T( 3 * i + 1, 2 * i, res[1] ) ); - triplet_list.push_back( T( 3 * i + 2, 2 * i, res[2] ) ); + triplet_list.emplace_back( 3 * i, 2 * i, res[0] ); + triplet_list.emplace_back( 3 * i + 1, 2 * i, res[1] ); + triplet_list.emplace_back( 3 * i + 2, 2 * i, res[2] ); tmp = Vector3{ 0, 1, 0 }; res = ( tmp - tmp.dot( vf[i] ) * vf[i] ).normalized(); - triplet_list.push_back( T( 3 * i, 2 * i + 1, res[0] ) ); - triplet_list.push_back( T( 3 * i + 1, 2 * i + 1, res[1] ) ); - triplet_list.push_back( T( 3 * i + 2, 2 * i + 1, res[2] ) ); + triplet_list.emplace_back( 3 * i, 2 * i + 1, res[0] ); + triplet_list.emplace_back( 3 * i + 1, 2 * i + 1, res[1] ); + triplet_list.emplace_back( 3 * i + 2, 2 * i + 1, res[2] ); } else if( vf[i][2] < -1 + 1e-8 ) { tmp = Vector3{ 1, 0, 0 }; res = ( tmp - tmp.dot( vf[i] ) * vf[i] ).normalized(); - triplet_list.push_back( T( 3 * i, 2 * i, res[0] ) ); - triplet_list.push_back( T( 3 * i + 1, 2 * i, res[1] ) ); - triplet_list.push_back( T( 3 * i + 2, 2 * i, res[2] ) ); + triplet_list.emplace_back( 3 * i, 2 * i, res[0] ); + triplet_list.emplace_back( 3 * i + 1, 2 * i, res[1] ); + triplet_list.emplace_back( 3 * i + 2, 2 * i, res[2] ); tmp = Vector3{ 0, -1, 0 }; res = ( tmp - tmp.dot( vf[i] ) * vf[i] ).normalized(); - triplet_list.push_back( T( 3 * i, 2 * i + 1, res[0] ) ); - triplet_list.push_back( T( 3 * i + 1, 2 * i + 1, res[1] ) ); - triplet_list.push_back( T( 3 * i + 2, 2 * i + 1, res[2] ) ); + triplet_list.emplace_back( 3 * i, 2 * i + 1, res[0] ); + triplet_list.emplace_back( 3 * i + 1, 2 * i + 1, res[1] ); + triplet_list.emplace_back( 3 * i + 2, 2 * i + 1, res[2] ); } else { @@ -331,13 +331,13 @@ void sparse_tangent_basis_spherical( const vectorfield & vf, SpMatrixX & basis ) ephi = Vector3{ -vf[i][1] / rxy, vf[i][0] / rxy, 0 }; res = ( etheta - etheta.dot( vf[i] ) * vf[i] ).normalized(); - triplet_list.push_back( T( 3 * i, 2 * i, res[0] ) ); - triplet_list.push_back( T( 3 * i + 1, 2 * i, res[1] ) ); - triplet_list.push_back( T( 3 * i + 2, 2 * i, res[2] ) ); + triplet_list.emplace_back( 3 * i, 2 * i, res[0] ); + triplet_list.emplace_back( 3 * i + 1, 2 * i, res[1] ); + triplet_list.emplace_back( 3 * i + 2, 2 * i, res[2] ); res = ( ephi - ephi.dot( vf[i] ) * vf[i] ).normalized(); - triplet_list.push_back( T( 3 * i, 2 * i + 1, res[0] ) ); - triplet_list.push_back( T( 3 * i + 1, 2 * i + 1, res[1] ) ); - triplet_list.push_back( T( 3 * i + 2, 2 * i + 1, res[2] ) ); + triplet_list.emplace_back( 3 * i, 2 * i + 1, res[0] ); + triplet_list.emplace_back( 3 * i + 1, 2 * i + 1, res[1] ); + triplet_list.emplace_back( 3 * i + 2, 2 * i + 1, res[2] ); } } basis.setFromTriplets( triplet_list.begin(), triplet_list.end() ); @@ -379,7 +379,7 @@ void tangent_basis_righthanded( const vectorfield & vf, MatrixX & basis ) for( int i = 0; i < size; ++i ) { - auto & axis = vf[i]; + const auto & axis = vf[i]; // Choose orthogonalisation basis for Grahm-Schmidt // We will need two vectors with which the axis always forms the @@ -514,8 +514,7 @@ void sparse_hessian_bordered_3N( lambda[i] = image[i].normalized().dot( gradient[i] ); // Construct hessian_out - typedef Eigen::Triplet T; - std::vector tripletList; + std::vector> tripletList; tripletList.reserve( hessian.nonZeros() + 3 * nos ); // Iterate over non zero entries of hesiian @@ -523,11 +522,11 @@ void sparse_hessian_bordered_3N( { for( SpMatrixX::InnerIterator it( hessian, k ); it; ++it ) { - tripletList.push_back( T( it.row(), it.col(), it.value() ) ); + tripletList.emplace_back( it.row(), it.col(), it.value() ); } int j = k % 3; int i = ( k - j ) / 3; - tripletList.push_back( T( k, k, -lambda[i] ) ); // Correction to the diagonal + tripletList.emplace_back( k, k, -lambda[i] ); // Correction to the diagonal } hessian_out.setFromTriplets( tripletList.begin(), tripletList.end() ); } diff --git a/core/src/engine/Manifoldmath.cu b/core/src/engine/Manifoldmath.cu index 11c9d0c2b..98cef47c4 100644 --- a/core/src/engine/Manifoldmath.cu +++ b/core/src/engine/Manifoldmath.cu @@ -130,27 +130,27 @@ void Tangents( for( int idx_img = 0; idx_img < noi; ++idx_img ) { - auto & image = *configurations[idx_img]; + const auto & image = *configurations[idx_img]; // First Image if( idx_img == 0 ) { - auto & image_plus = *configurations[idx_img + 1]; + const auto & image_plus = *configurations[idx_img + 1]; Vectormath::set_c_a( 1, image_plus, tangents[idx_img] ); Vectormath::add_c_a( -1, image, tangents[idx_img] ); } // Last Image else if( idx_img == noi - 1 ) { - auto & image_minus = *configurations[idx_img - 1]; + const auto & image_minus = *configurations[idx_img - 1]; Vectormath::set_c_a( 1, image, tangents[idx_img] ); Vectormath::add_c_a( -1, image_minus, tangents[idx_img] ); } // Images Inbetween else { - auto & image_plus = *configurations[idx_img + 1]; - auto & image_minus = *configurations[idx_img - 1]; + const auto & image_plus = *configurations[idx_img + 1]; + const auto & image_minus = *configurations[idx_img - 1]; // Energies scalar E_mid = 0, E_plus = 0, E_minus = 0;