Skip to content

Commit

Permalink
Core: minor, nonintrusive improvements
Browse files Browse the repository at this point in the history
 - remove some unnecessary semicolons
 - added missing `Exception` copy and move constructors and operators
   definitions, add `noexcept` specifiers
 - fixed unnecessary copying in `Geometry` constructor
  • Loading branch information
GPMueller committed Apr 25, 2022
1 parent a4b4517 commit f49c114
Show file tree
Hide file tree
Showing 18 changed files with 94 additions and 112 deletions.
5 changes: 3 additions & 2 deletions core/include/data/Geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ class Geometry
// ---------- Constructor
// Build a regular lattice from a defined basis cell and translations
Geometry(
std::vector<Vector3> bravais_vectors, intfield n_cells, std::vector<Vector3> cell_atoms,
Basis_Cell_Composition cell_composition, scalar lattice_constant, Pinning pinning, Defects defects );
const std::vector<Vector3> & bravais_vectors, intfield n_cells, const std::vector<Vector3> & cell_atoms,
const Basis_Cell_Composition & cell_composition, scalar lattice_constant, const Pinning & pinning,
const Defects & defects );

// ---------- Convenience functions
// Retrieve triangulation, if 2D
Expand Down
2 changes: 1 addition & 1 deletion core/include/engine/Hamiltonian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Hamiltonian
virtual std::size_t Number_of_Interactions();

// Hamiltonian name as string
virtual const std::string & Name();
virtual const std::string & Name() const = 0;

// Boundary conditions
intfield boundary_conditions; // [3] (a, b, c)
Expand Down
2 changes: 1 addition & 1 deletion core/include/engine/Hamiltonian_Gaussian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Hamiltonian_Gaussian : public Hamiltonian
scalar Energy_Single_Spin( int ispin, const vectorfield & spins ) override;

// Hamiltonian name as string
const std::string & Name() override;
const std::string & Name() const override;

// Parameters of the energy landscape
int n_gaussians;
Expand Down
2 changes: 1 addition & 1 deletion core/include/engine/Hamiltonian_Heisenberg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Hamiltonian_Heisenberg : public Hamiltonian
scalar Energy_Single_Spin( int ispin, const vectorfield & spins ) override;

// Hamiltonian name as string
const std::string & Name() override;
const std::string & Name() const override;

// ------------ Single Spin Interactions ------------
// External magnetic field across the sample
Expand Down
8 changes: 4 additions & 4 deletions core/include/engine/Solver_Depondt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ inline void Method_Solver<Solver::Depondt>::Initialize()
configurations_predictor[i] = std::shared_ptr<vectorfield>( new vectorfield( this->nos, { 0, 0, 0 } ) );

this->temp1 = vectorfield( this->nos, { 0, 0, 0 } );
};
}

/*
Template instantiation of the Simulation class for use with the Depondt Solver.
Expand Down Expand Up @@ -74,16 +74,16 @@ inline void Method_Solver<Solver::Depondt>::Iteration()
// Get new spin conf n_new = R( (H+H')/2 ) * n
Vectormath::rotate( conf, temp1, angle, conf );
}
};
}

template<>
inline std::string Method_Solver<Solver::Depondt>::SolverName()
{
return "Depondt";
};
}

template<>
inline std::string Method_Solver<Solver::Depondt>::SolverFullName()
{
return "Depondt";
};
}
8 changes: 4 additions & 4 deletions core/include/engine/Solver_Heun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ inline void Method_Solver<Solver::Heun>::Initialize()
configurations_predictor[i] = std::shared_ptr<vectorfield>( new vectorfield( this->nos ) );

this->temp1 = vectorfield( this->nos, { 0, 0, 0 } );
};
}

/*
Template instantiation of the Simulation class for use with the Heun Solver.
Expand Down Expand Up @@ -77,16 +77,16 @@ inline void Method_Solver<Solver::Heun>::Iteration()
// Copy out
conf = conf_temp;
}
};
}

template<>
inline std::string Method_Solver<Solver::Heun>::SolverName()
{
return "Heun";
};
}

template<>
inline std::string Method_Solver<Solver::Heun>::SolverFullName()
{
return "Heun";
};
}
1 change: 1 addition & 0 deletions core/include/engine/Solver_Kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Engine
{
namespace Solver_Kernels
{

// SIB
void sib_transform( const vectorfield & spins, const vectorfield & force, vectorfield & out );

Expand Down
8 changes: 4 additions & 4 deletions core/include/engine/Solver_RK4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ inline void Method_Solver<Solver::RungeKutta4>::Initialize()
this->configurations_k4[i] = std::shared_ptr<vectorfield>( new vectorfield( this->nos ) );

this->temp1 = vectorfield( this->nos, { 0, 0, 0 } );
};
}

/*
Template instantiation of the Simulation class for use with the 4th order Runge Kutta Solver.
Expand Down Expand Up @@ -144,16 +144,16 @@ inline void Method_Solver<Solver::RungeKutta4>::Iteration()
// Copy out
conf = conf_temp;
}
};
}

template<>
inline std::string Method_Solver<Solver::RungeKutta4>::SolverName()
{
return "RK4";
};
}

template<>
inline std::string Method_Solver<Solver::RungeKutta4>::SolverFullName()
{
return "Runge Kutta (4th order)";
};
}
8 changes: 4 additions & 4 deletions core/include/engine/Solver_SIB.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ inline void Method_Solver<Solver::SIB>::Initialize()
this->configurations_predictor = std::vector<std::shared_ptr<vectorfield>>( this->noi );
for( int i = 0; i < this->noi; i++ )
configurations_predictor[i] = std::shared_ptr<vectorfield>( new vectorfield( this->nos ) );
};
}

/*
Template instantiation of the Simulation class for use with the SIB Solver.
Expand Down Expand Up @@ -47,16 +47,16 @@ inline void Method_Solver<Solver::SIB>::Iteration()

Solver_Kernels::sib_transform( image, forces_virtual_predictor[i], image );
}
};
}

template<>
inline std::string Method_Solver<Solver::SIB>::SolverName()
{
return "SIB";
};
}

template<>
inline std::string Method_Solver<Solver::SIB>::SolverFullName()
{
return "Semi-implicit B";
};
}
8 changes: 4 additions & 4 deletions core/include/engine/Solver_VP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ inline void Method_Solver<Solver::VP>::Initialize()
this->forces_previous = velocities; // [noi][nos]
this->projection = std::vector<scalar>( this->noi, 0 ); // [noi]
this->force_norm2 = std::vector<scalar>( this->noi, 0 ); // [noi]
};
}

/*
Template instantiation of the Simulation class for use with the VP Solver.
Expand Down Expand Up @@ -111,16 +111,16 @@ inline void Method_Solver<Solver::VP>::Iteration()
conf[idx] = conf_temp[idx].normalized();
} );
}
};
}

template<>
inline std::string Method_Solver<Solver::VP>::SolverName()
{
return "VP";
};
}

template<>
inline std::string Method_Solver<Solver::VP>::SolverFullName()
{
return "Velocity Projection";
};
}
6 changes: 3 additions & 3 deletions core/include/engine/Solver_VP_OSO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ inline void Method_Solver<Solver::VP_OSO>::Initialize()
this->projection = std::vector<scalar>( this->noi, 0 ); // [noi]
this->force_norm2 = std::vector<scalar>( this->noi, 0 ); // [noi]
this->searchdir = std::vector<vectorfield>( this->noi, vectorfield( this->nos, { 0, 0, 0 } ) );
};
}

/*
Template instantiation of the Simulation class for use with the VP Solver.
Expand Down Expand Up @@ -117,10 +117,10 @@ template<>
inline std::string Method_Solver<Solver::VP_OSO>::SolverName()
{
return "VP_OSO";
};
}

template<>
inline std::string Method_Solver<Solver::VP_OSO>::SolverFullName()
{
return "Velocity Projection using exponential transforms";
};
}
51 changes: 38 additions & 13 deletions core/include/utility/Exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include <fmt/format.h>

#include <exception>
#include <type_traits>

namespace Utility
{

Expand All @@ -29,18 +32,17 @@ enum class Exception_Classifier
};

/*
* Spirit library exception class:
* Derived from std::runtime_error.
* Adds file, line and function information to the exception message.
* Contains an exception classifier and a level so that the handler
* can decide if execution should be stopped or can continue.
* Spirit library exception class, derived from std::runtime_error.
*
* Adds file, line and function information to the exception message. Contains an exception classifier and a level so
* that the handler can decide if execution should be stopped or can continue.
*/
class Exception : public std::runtime_error
{
public:
Exception(
Exception_Classifier classifier, Log_Level level, const std::string & message, const char * file,
unsigned int line, const char * function )
unsigned int line, const char * function ) noexcept( false )
: std::runtime_error(
fmt::format( "{}:{} in function \'{}\':\n{:>49}{}", file, line, function, " ", message ) ),
classifier( classifier ),
Expand All @@ -51,7 +53,17 @@ class Exception : public std::runtime_error
{
}

~Exception() throw() override = default;
// Copy-constructor
Exception( const Exception & ) noexcept = default;
// Copy-assignment operator
Exception & operator=( const Exception & ) = delete;

// Move-constructor
Exception( Exception && ) noexcept = default;
// Move-assignment constructor
Exception & operator=( Exception && ) = delete;

~Exception() noexcept override = default;

const Exception_Classifier classifier;
const Log_Level level;
Expand All @@ -60,25 +72,38 @@ class Exception : public std::runtime_error
const char * function;
};

// To make sure that the noexcept-specifications are correct
static_assert(
std::is_nothrow_copy_constructible<Exception>::value,
"Utility::Exception is expected to be nothrow copy-constructible" );
static_assert(
std::is_nothrow_move_constructible<Exception>::value,
"Utility::Exception is expected to be nothrow move-constructible" );
static_assert(
std::is_nothrow_destructible<Exception>::value, "Utility::Exception is expected to be nothrow destructible" );

/*
* Rethrow (creating a std::nested_exception) an exception using the Exception class
* to add file and line info
*/
void rethrow( const std::string & message, const char * file, unsigned int line, const char * function );
void rethrow( const std::string & message, const char * file, const unsigned int line, const char * function ) noexcept(
false );

/*
* Handle_Exception_API finalizes what should be done when an exception is encountered at the API layer.
* This function should only be used inside API functions, since that is the top level at which an
* exception is caught.
* This function catches any exceptions and should only be used in the catch-statements of API functions, since that is
* the top level at which an exception is caught.
*/
void Handle_Exception_API(
const char * file, unsigned int line, const char * function = "", int idx_image = -1, int idx_chain = -1 );
const char * file, const unsigned int line, const char * function = "", const int idx_image = -1,
const int idx_chain = -1 ) noexcept;

/*
* Handle_Exception_Core finalizes what should be done when an exception is encountered inside the core.
* Handle_Exception_Core finalizes what should be done when an exception is encountered inside the core library.
* This function should only be used inside the core, below the API layer.
*/
void Handle_Exception_Core( const std::string & message, const char * file, unsigned int line, const char * function );
void Handle_Exception_Core(
const std::string & message, const char * file, unsigned int line, const char * function ) noexcept( false );

// Shorthand for throwing a Spirit library exception with file and line info
#define spirit_throw( classifier, level, message ) \
Expand Down
5 changes: 3 additions & 2 deletions core/src/data/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ namespace Data
{

Geometry::Geometry(
std::vector<Vector3> bravais_vectors, intfield n_cells, std::vector<Vector3> cell_atoms,
Basis_Cell_Composition cell_composition, scalar lattice_constant, Pinning pinning, Defects defects )
const std::vector<Vector3> & bravais_vectors, intfield n_cells, const std::vector<Vector3> & cell_atoms,
const Basis_Cell_Composition & cell_composition, scalar lattice_constant, const Pinning & pinning,
const Defects & defects )
: bravais_vectors( bravais_vectors ),
n_cells( n_cells ),
n_cell_atoms( cell_atoms.size() ),
Expand Down
8 changes: 0 additions & 8 deletions core/src/engine/Hamiltonian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,4 @@ scalar Hamiltonian::Energy_Single_Spin( int ispin, const vectorfield & spins )
"Tried to use Hamiltonian::Energy_Single_Spin() of the Hamiltonian base class!" );
}

static const std::string name = "--";
const std::string & Hamiltonian::Name()
{
spirit_throw(
Exception_Classifier::Not_Implemented, Log_Level::Error,
"Tried to use Hamiltonian::Name() of the Hamiltonian base class!" );
}

} // namespace Engine
2 changes: 1 addition & 1 deletion core/src/engine/Hamiltonian_Gaussian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ scalar Hamiltonian_Gaussian::Energy_Single_Spin( int ispin, const vectorfield &

// Hamiltonian name as string
static const std::string name = "Gaussian";
const std::string & Hamiltonian_Gaussian::Name()
const std::string & Hamiltonian_Gaussian::Name() const
{
return name;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/engine/Hamiltonian_Heisenberg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ void Hamiltonian_Heisenberg::Clean_DDI()

// Hamiltonian name as string
static const std::string name = "Heisenberg";
const std::string & Hamiltonian_Heisenberg::Name()
const std::string & Hamiltonian_Heisenberg::Name() const
{
return name;
}
Expand Down
5 changes: 4 additions & 1 deletion core/src/engine/Hamiltonian_Heisenberg.cu
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,10 @@ namespace Engine

// Hamiltonian name as string
static const std::string name = "Heisenberg";
const std::string& Hamiltonian_Heisenberg::Name() { return name; }
const std::string & Hamiltonian_Heisenberg::Name() const
{
return name;
}
}

#endif
Loading

0 comments on commit f49c114

Please sign in to comment.