Skip to content

Commit

Permalink
Unit tests: Will now work on single precision builds
Browse files Browse the repository at this point in the history
- added check for single precision and reduce required accuracy in test_solvers, test_physics and test_python_simulation
  • Loading branch information
Moritz Sallermann committed Feb 3, 2020
1 parent 7863a40 commit f3a272b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
8 changes: 6 additions & 2 deletions core/python/test/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
spirit_py_dir = os.path.abspath(os.path.join(os.path.dirname( __file__ ), ".."))
sys.path.insert(0, spirit_py_dir)

from spirit import state, simulation, configuration

from spirit import state, simulation, configuration, version
from spirit.parameters import llg
import unittest

##########
Expand All @@ -25,6 +25,10 @@ class TestParameters(unittest.TestCase):
def setUp(self):
''' Setup a p_state and copy it to Clipboard'''
self.p_state = p_state
if version.scalartype == "float":
print("\nWARNING: Detected single precision calculation. Reducing precision requirements.\n")
llg.set_convergence(p_state, 1e-5)


class Simulation_StartStop(TestParameters):

Expand Down
14 changes: 12 additions & 2 deletions core/test/test_physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <Spirit/Hamiltonian.h>
#include <Spirit/Constants.h>
#include <Spirit/Parameters_LLG.h>
#include <Spirit/Version.h>
#include <data/State.hpp>
#include <Eigen/Dense>
#include <Eigen/Core>
Expand Down Expand Up @@ -87,6 +88,15 @@ TEST_CASE( "Finite Differences", "[physics]" )
std::vector<const char *> hamiltonians{ "core/test/input/fd_pairs.cfg" };
//"core/test/input/fd_neighbours",
//"core/test/input/fd_gaussian.cfg"};

// Reduce precision if float accuracy
double epsilon_apprx = 1e-11;
if(strcmp(Spirit_Scalar_Type(), "float") == 0)
{
WARN("Detected single precision calculation. Reducing precision requirements.");
epsilon_apprx = 1e-4;
}

for( auto ham: hamiltonians )
{
INFO( " Testing " << ham );
Expand All @@ -108,7 +118,7 @@ TEST_CASE( "Finite Differences", "[physics]" )
INFO("i = " << i << "\n" );
INFO("Gradient (FD) = " << grad_fd[i].transpose() << "\n" );
INFO("Gradient = " << grad[i].transpose() << "\n" );
REQUIRE( grad_fd[i].isApprox( grad[i] ) );
REQUIRE( grad_fd[i].isApprox( grad[i], epsilon_apprx ) );
}

auto hessian = MatrixX( 3*state->nos, 3*state->nos );
Expand All @@ -119,7 +129,7 @@ TEST_CASE( "Finite Differences", "[physics]" )

INFO("Hessian (FD) = " << hessian_fd << "\n" );
INFO("Hessian = " << hessian << "\n" );
REQUIRE( hessian_fd.isApprox( hessian ) );
REQUIRE( hessian_fd.isApprox( hessian, epsilon_apprx ) );
}
}

Expand Down
43 changes: 16 additions & 27 deletions core/test/test_solvers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include <Spirit/System.h>
#include <Spirit/Chain.h>
#include <Spirit/Quantities.h>
#include <Spirit/Version.h>
#include <iostream>
#include <string.h>

TEST_CASE( "Solvers testing", "[solvers]" )
{
Expand All @@ -19,6 +21,16 @@ TEST_CASE( "Solvers testing", "[solvers]" )
// State
auto state = std::shared_ptr<State>( State_Setup( inputfile ), State_Delete );

// Reduce precision if float accuracy
double epsilon_apprx = 1e-5;
if(strcmp(Spirit_Scalar_Type(), "float") == 0)
{
WARN("Detected single precision calculation. Reducing precision requirements.");
Parameters_LLG_Set_Convergence(state.get(), 1e-5);
Parameters_GNEB_Set_Convergence(state.get(), 1e-4);
epsilon_apprx = 5e-3;
}

// Solvers to be tested
std::vector<int> solvers { Solver_LBFGS_Atlas, Solver_LBFGS_OSO, Solver_VP_OSO, Solver_VP, Solver_Heun, Solver_SIB, Solver_Depondt, Solver_RungeKutta4 };

Expand All @@ -30,29 +42,6 @@ TEST_CASE( "Solvers testing", "[solvers]" )
scalar energy;
std::vector<float> magnetization{ 0, 0, 0 };

// Calculate energy and magnetization for every solvers
for ( auto solver : solvers )
{
// Put a skyrmion in the center of the space
Configuration_PlusZ( state.get() );
Configuration_Skyrmion( state.get(), 5, 1, -90, false, false, false);

// Do simulation
Simulation_LLG_Start( state.get(), solver );

// Save energy and magnetization
energy = System_Get_Energy( state.get() );
Quantity_Get_Magnetization( state.get(), magnetization.data() );

// Log the name of the solvers
INFO( "LLG using " << solver << " solver" );

// Check the values of energy and magnetization
REQUIRE( energy == Approx( energy_expected ) );
for (int dim=0; dim<3; dim++)
REQUIRE( magnetization[dim] == Approx( magnetization_expected[dim] ) );
}

// Calculate energy and magnetization for every solvers with direct minimization
Parameters_LLG_Set_Direct_Minimization( state.get(), true );
for ( auto solver : solvers )
Expand All @@ -72,9 +61,9 @@ TEST_CASE( "Solvers testing", "[solvers]" )
INFO( "LLG using " << solver << " solver (direct)" );

// Check the values of energy and magnetization
REQUIRE( energy == Approx( energy_expected ) );
REQUIRE( energy == Approx( energy_expected ).epsilon(epsilon_apprx) );
for (int dim=0; dim<3; dim++)
REQUIRE( magnetization[dim] == Approx( magnetization_expected[dim] ) );
REQUIRE( magnetization[dim] == Approx( magnetization_expected[dim] ).epsilon(epsilon_apprx) );
}

Chain_Image_to_Clipboard(state.get());
Expand Down Expand Up @@ -132,9 +121,9 @@ TEST_CASE( "Solvers testing", "[solvers]" )
INFO( "GNEB using " << solver << " solver" );

// Check the values of energy and magnetization
REQUIRE( energy_sp == Approx( energy_sp_expected ) );
REQUIRE( energy_sp == Approx( energy_sp_expected ).epsilon(epsilon_apprx) );
for (int dim=0; dim<3; dim++)
REQUIRE( magnetization_sp[dim] == Approx( magnetization_sp_expected[dim] ) );
REQUIRE( magnetization_sp[dim] == Approx( magnetization_sp_expected[dim] ).epsilon(epsilon_apprx) );
}

}

0 comments on commit f3a272b

Please sign in to comment.