From bd4539981b5e417bd2bd59064553fc635482051e Mon Sep 17 00:00:00 2001 From: mkirsz <s1351949@sms.ed.ac.uk> Date: Mon, 21 Oct 2024 20:53:58 +0100 Subject: [PATCH] Missing test, all ok --- tests/test_atom.cpp | 63 +++++++++++++++++++++ tests/test_main.cpp | 3 + tests/test_structure.cpp | 118 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 tests/test_atom.cpp create mode 100644 tests/test_main.cpp create mode 100644 tests/test_structure.cpp diff --git a/tests/test_atom.cpp b/tests/test_atom.cpp new file mode 100644 index 0000000..359a8bf --- /dev/null +++ b/tests/test_atom.cpp @@ -0,0 +1,63 @@ +#include "catch2/catch.hpp" +#include <tadah/mlip/atom.h> +#include <string> + + +char symbol1[]="Ti"; +int Z1 = 22; +Vec3d pos1(1.0,2.0,3.0); +Vec3d force1(4.0,5.0,6.0); +Element element1(symbol1,Z1); + +char symbol2[]="Nb"; +int Z2 = 41; +Vec3d pos2(-1.0,-2.0,-3.0); +Vec3d force2(-4.0,-5.0,-6.0); +Element element2(symbol2,Z2); + +TEST_CASE( "Testing Atom class constructor", "[atom]" ) { + + + // Trivial case 1/2 + Atom a(element1, 1.0, 2.0, 3.0, + 4.0, 5.0, 6.0); + + REQUIRE( pos1 == a.position ); + REQUIRE( force1 == a.force ); + REQUIRE( symbol1[0] == a.symbol[0] ); + REQUIRE( symbol1[1] == a.symbol[1] ); + REQUIRE( Z1 == a.Z ); + + // Trivial case 2/2 + Atom b; + b.position = pos1; + b.force = force1; + b.symbol[0] = symbol1[0]; + b.symbol[1] = symbol1[1]; + b.Z = Z1; + + REQUIRE( pos1 == b.position ); + REQUIRE( force1 == b.force ); + REQUIRE( symbol1[0] == b.symbol[0] ); + REQUIRE( symbol1[1] == b.symbol[1] ); + REQUIRE( Z1 == b.Z ); + + REQUIRE( a == b ); +} +TEST_CASE( "Testing Atom operator==", "[atom_operator==]" ) { + Atom a(element1, 1.0, 2.0, 3.0, + 4.0, 5.0, 6.0); + Atom b(element1, 1.0, 2.0, 3.0, + 4.0, 5.0, 6.0); + REQUIRE(a==b); + + Atom c(element2, -1.0, -2.0, -3.0, + -4.0, -5.0, -6.0); + REQUIRE(!(a==c)); +} +TEST_CASE( "Testing Atom copy", "[atom_copy]" ) { + Atom a(element1, 1.0, 2.0, 3.0, + 4.0, 5.0, 6.0); + Atom b=a; + REQUIRE(a==b); +} diff --git a/tests/test_main.cpp b/tests/test_main.cpp new file mode 100644 index 0000000..8d41722 --- /dev/null +++ b/tests/test_main.cpp @@ -0,0 +1,3 @@ +#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file +#include "catch2/catch.hpp" +// No test in this file... diff --git a/tests/test_structure.cpp b/tests/test_structure.cpp new file mode 100644 index 0000000..39c3489 --- /dev/null +++ b/tests/test_structure.cpp @@ -0,0 +1,118 @@ +#include <string> +#include <filesystem> +#include "catch2/catch.hpp" +#include <tadah/mlip/atom.h> +#include <tadah/mlip/structure.h> + +// Conversion factor from eV/A^3 to kbar; +double fac = 1602.1766208; + +TEST_CASE( "Testing Structure class volume", "[structure_volume]" ) { + //using vec=Eigen::Vector3d; + + //std::string symbol="Ti"; + //std::string name="Titanium"; + //int Z = 22; + //vec pos(1.0,2.0,3.0); + //vec force(4.0,5.0,6.0); + //Element element(symbol,name,Z); + + //Atom a(element, 1.0, 2.0, 3.0, + // 4.0, 5.0, 6.0); + + Structure st; + // Integer volume + st.cell.load(2,-1,3, + 3,2,-4, + -2,0,1); + + REQUIRE(st.get_volume() == 11 ); + + // double volume + st.cell.load(5.0147,5.0104,-5.0018, + -9.9924,-3.3395,-9.9662, + -25.6396,38.4594,12.8198); + + REQUIRE(st.get_volume() == Approx(5980.0279772134).epsilon(1e-10)); + + +} +TEST_CASE( "Testing Structure virial pressure calculations", "[structure_virial_pressures]" ) { + + Structure st; + st.cell.load(9.374769,0.0,0.0, + 0.0,9.374769,0.0, + 0.0,0.0,9.374769); + + REQUIRE(st.get_volume() == Approx(823.91)); + + st.stress.load(257.0893807653,0.0,0.0, + 0.0,257.0893807653,0.0, + 0.0,0.0,257.0893807653); + REQUIRE(fac*st.get_virial_pressure() == Approx(499.93)); + +} +TEST_CASE( "Testing Structure read and write", "[structure_read_write]" ) { + Structure st; + st.read("tests_data/structure_1.dat"); + + REQUIRE(st.get_volume() == Approx(989.521812)); + + REQUIRE(fac*st.get_virial_pressure() == Approx(26.705578)); + REQUIRE(fac*st.get_pressure(300) == Approx(28.965914)); + REQUIRE(fac*st.get_pressure(0) == Approx(26.705578)); + + std::string tempfile = std::tmpnam(nullptr); + st.save(tempfile); + + Structure st_temp; + st_temp.read(tempfile); + + REQUIRE(st_temp.get_volume() == Approx(989.521812)); + + REQUIRE(fac*st_temp.get_virial_pressure() == Approx(26.705578)); + REQUIRE(fac*st_temp.get_pressure(300) == Approx(28.965914)); + REQUIRE(fac*st_temp.get_pressure(0) == Approx(26.705578)); + + REQUIRE(st==st_temp); + + std::remove(tempfile.c_str()); + +} +TEST_CASE( "Testing Structure compare", "[structure_compare]" ) { + Structure st; + st.read("tests_data/structure_1.dat"); + std::string tempfile = std::tmpnam(nullptr); + st.save(tempfile); + + Structure st_temp; + st_temp.read(tempfile); + + SECTION("Compare unchanged") { + REQUIRE(st==st_temp); + } + SECTION("Compare symbols") { + REQUIRE(st==st_temp); + st_temp.atoms[0].symbol[0]='X'; + st_temp.atoms[0].symbol[1]='X'; + REQUIRE(!(st==st_temp)); + } + SECTION("Compare position") { + REQUIRE(st==st_temp); + st_temp.atoms[0].position(0.12,0.13,10.14); + REQUIRE(!(st==st_temp)); + } + SECTION("Compare force") { + REQUIRE(st==st_temp); + st_temp.atoms[0].force(1.12, 0.13, 0.134); + REQUIRE(!(st==st_temp)); + } + + std::remove(tempfile.c_str()); +} +TEST_CASE( "Testing Structure copy", "[structure_copy]" ) { + Structure st; + st.read("tests_data/structure_1.dat"); + Structure st2=st; + REQUIRE(st==st2); +} -- GitLab