Skip to content

Commit 69ad810

Browse files
committed
Update tests for molecule and atom types
1 parent 28bf363 commit 69ad810

3 files changed

Lines changed: 87 additions & 20 deletions

File tree

include/gauxc/molecule.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ extern void gauxc_molecule_delete(GauXCStatus* status, GauXCMolecule* mol );
6868
*/
6969
extern size_t gauxc_molecule_natoms(GauXCStatus* status, const GauXCMolecule mol );
7070

71+
/**
72+
* @brief Check if two Molecule instances are equal.
73+
* @param status Status object to capture any errors.
74+
* @param mol1 Handle to the first Molecule.
75+
* @param mol2 Handle to the second Molecule.
76+
* @return true if the Molecules are equal, false otherwise.
77+
*/
78+
extern bool gauxc_molecule_equal(
79+
GauXCStatus* status,
80+
const GauXCMolecule mol1,
81+
const GauXCMolecule mol2
82+
);
83+
7184
#ifdef __cplusplus
7285
} // extern "C"
7386
} // namespace GauXC::C

src/c_molecule.cxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,15 @@ size_t gauxc_molecule_natoms(GauXCStatus* status, const GauXCMolecule mol) {
6868
return detail::get_molecule_ptr(mol)->natoms();
6969
}
7070

71+
bool gauxc_molecule_equal(
72+
GauXCStatus* status,
73+
const GauXCMolecule mol_a,
74+
const GauXCMolecule mol_b
75+
) {
76+
status->code = 0;
77+
if (mol_a.ptr == nullptr || mol_b.ptr == nullptr) return false;
78+
return *detail::get_molecule_ptr(mol_a) == *detail::get_molecule_ptr(mol_b);
79+
}
80+
7181
} // extern "C"
7282
} // namespace GauXC::C

tests/moltypes_test.cxx

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*/
1212
#include "ut_common.hpp"
1313
#include "catch2/catch.hpp"
14-
#include <gauxc/molecule.h>
1514
#include <gauxc/molecule.hpp>
1615
#include <gauxc/molmeta.hpp>
1716
#include <gauxc/external/hdf5.hpp>
@@ -24,6 +23,11 @@
2423

2524
#include <gauxc/gauxc_config.hpp>
2625

26+
#ifdef GAUXC_HAS_C
27+
#include <gauxc/molecule.h>
28+
#include <gauxc/external/hdf5.h>
29+
#endif
30+
2731
#ifdef GAUXC_HAS_MPI
2832
#include <mpi.h>
2933
#endif
@@ -42,7 +46,7 @@ TEST_CASE("Atom", "[moltypes]") {
4246
CHECK( atom.y == y );
4347
CHECK( atom.z == z );
4448

45-
#if GAUXC_HAS_C
49+
#ifdef GAUXC_HAS_C
4650
SECTION("C Interop") {
4751
C::GauXCAtom c_atom = { Z, x, y, z };
4852

@@ -69,9 +73,9 @@ TEST_CASE("Molecule", "[moltypes]") {
6973
CHECK(mol.natoms() == 0);
7074
}
7175

72-
#if GAUXC_HAS_C
76+
#ifdef GAUXC_HAS_C
7377
SECTION("Default C") {
74-
C::GauXCStatus = status;
78+
C::GauXCStatus status;
7579
C::GauXCMolecule mol = C::gauxc_molecule_new(&status);
7680
CHECK(status.code == 0);
7781
CHECK(C::gauxc_molecule_natoms(&status, mol) == 0);
@@ -203,27 +207,67 @@ TEST_CASE("HDF5-MOLECULE", "[moltypes]") {
203207
if( world_rank ) return; // Only run on root rank
204208
#endif
205209

206-
210+
SECTION("HDF5 IO") {
207211
Molecule mol = make_water();
208212

209-
// Write file
210-
const std::string fname = GAUXC_REF_DATA_PATH "/test_mol.hdf5";
211-
//if( std::filesystem::exists(fname) ) std::filesystem::remove(fname);
212-
auto file_exists = [](const auto& f ) {
213-
std::ifstream file(f); return file.good();
214-
};
215-
if(file_exists(fname)) std::remove(fname.c_str());
213+
// Write file
214+
const std::string fname = GAUXC_REF_DATA_PATH "/test_mol.hdf5";
215+
//if( std::filesystem::exists(fname) ) std::filesystem::remove(fname);
216+
auto file_exists = [](const auto& f ) {
217+
std::ifstream file(f); return file.good();
218+
};
219+
if(file_exists(fname)) std::remove(fname.c_str());
220+
221+
write_hdf5_record( mol, fname , "/MOL" );
222+
223+
// Read File
224+
Molecule mol_read;
225+
read_hdf5_record( mol_read, fname, "/MOL" );
226+
227+
// Check that IO was correct
228+
CHECK( mol == mol_read );
229+
230+
//std::filesystem::remove(fname); // Delete the test file
231+
std::remove(fname.c_str());
232+
}
233+
234+
#ifdef GAUXC_HAS_C
235+
SECTION("C HDF5 IO") {
236+
C::GauXCStatus status;
237+
238+
C::GauXCMolecule mol;
239+
C::GauXCAtom atoms[3] = {
240+
{ 8, 0.000000000000000, 0.000000000000000, 0.000000000000000 },
241+
{ 1, 1.579252144093028, 2.174611055780858, 0.000000000000000 },
242+
{ 1, 1.579252144093028, -2.174611055780858, 0.000000000000000 }
243+
};
244+
mol = C::gauxc_molecule_new_from_atoms(&status, atoms, 3);
245+
CHECK(status.code == 0);
246+
247+
auto file_exists = [](const auto& f ) {
248+
std::ifstream file(f); return file.good();
249+
};
250+
// Write file
251+
const std::string fname_c = GAUXC_REF_DATA_PATH "/test_mol_c.hdf5";
252+
if(file_exists(fname_c)) std::remove(fname_c.c_str());
253+
254+
C::gauxc_molecule_write_hdf5_record(&status, mol, fname_c.c_str(), "/MOL");
255+
CHECK(status.code == 0);
216256

217-
write_hdf5_record( mol, fname , "/MOL" );
257+
C::GauXCMolecule mol_read = C::gauxc_molecule_new(&status);
258+
CHECK(status.code == 0);
218259

219-
// Read File
220-
Molecule mol_read;
221-
read_hdf5_record( mol_read, fname, "/MOL" );
260+
C::gauxc_molecule_read_hdf5_record(&status, mol_read, fname_c.c_str(), "/MOL");
261+
CHECK(status.code == 0);
222262

223-
// Check that IO was correct
224-
CHECK( mol == mol_read );
263+
CHECK(C::gauxc_molecule_equal(&status, mol, mol_read));
264+
CHECK(status.code == 0);
225265

226-
//std::filesystem::remove(fname); // Delete the test file
227-
std::remove(fname.c_str());
266+
C::gauxc_molecule_delete(&status, &mol);
267+
CHECK(status.code == 0);
268+
C::gauxc_molecule_delete(&status, &mol_read);
269+
CHECK(status.code == 0);
270+
}
271+
#endif
228272

229273
}

0 commit comments

Comments
 (0)