Skip to content

Commit

Permalink
First steps in implementing a unit test for OASISCoupledOcean
Browse files Browse the repository at this point in the history
A skeleton of a unit test in OASISCoupledOcean_test.cpp and some changes
 in the accompanying CMakeLists.txt to make it compile. Added
 MainMPI.cpp so that the test starts. We still need a namcouple file and
  the actual ins-and-outs of the test.
  • Loading branch information
einola committed Sep 13, 2024
1 parent 9275539 commit bf7cd78
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
6 changes: 6 additions & 0 deletions physics/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ if(ENABLE_MPI)
PRIVATE USE_MPI TEST_FILES_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}\"
)
target_link_libraries(testTOPAZOcn_MPI1 PRIVATE nextsimlib doctest::doctest)
if (ENABLE_OASIS)
add_executable(testOASISCoupledOcean_MPI2 "OASISCoupledOcean_test.cpp" "MainMPI.cpp")
target_include_directories(testOASISCoupledOcean_MPI2 PRIVATE "${ModulesRoot}/OceanBoundaryModule")
target_compile_definitions(testOASISCoupledOcean_MPI2 PRIVATE TEST_FILES_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}\")
target_link_libraries(testOASISCoupledOcean_MPI2 PRIVATE nextsimlib doctest::doctest)
endif ()
else()
add_executable(testERA5Atm "ERA5Atm_test.cpp")
target_compile_definitions(testERA5Atm PRIVATE TEST_FILES_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}\")
Expand Down
30 changes: 30 additions & 0 deletions physics/test/MainMPI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*!
* @file MainMPI.cpp
*
* @date 13 Sep 2024
* @author Einar Ólason <einar.olason@nersc.no>
*
* This file is required so that the MPI enabled doc-test OASISCoupledOcean_test.cpp (and any
* others) can run.
*/

#define DOCTEST_CONFIG_IMPLEMENT

#include <doctest/extensions/doctest_mpi.h>

int main(int argc, char** argv)
{
doctest::mpi_init_thread(argc, argv, MPI_THREAD_MULTIPLE);

doctest::Context ctx;
ctx.setOption("reporters", "MpiConsoleReporter");
ctx.setOption("reporters", "MpiFileReporter");
ctx.setOption("force-colors", true);
ctx.applyCommandLine(argc, argv);

int test_result = ctx.run();

doctest::mpi_finalize();

return test_result;
}
40 changes: 40 additions & 0 deletions physics/test/OASISCoupledOcean_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*!
* @file OASISCoupledOcean_test.cpp
*
* @date 13 Sep 2024
* @author Einar Ólason <einar.olason@nersc.no>
*/

#include <doctest/extensions/doctest_mpi.h>

#include "include/OASISCoupledOcean.hpp"

namespace Nextsim {

TEST_SUITE_BEGIN("OASISCoupledOcean");
MPI_TEST_CASE("OASIS init put and get", 2)
{
MPI_Comm modelCommunicator;
int compID; // Not actually used. Only useful for debugging
const std::string compName = "nextsim"; // Not useful for any setups we have in mind
OASIS_CHECK_ERR(oasis_c_init_comp(&compID, compName.c_str(), OASIS_COUPLED));
OASIS_CHECK_ERR(oasis_c_get_localcomm(&modelCommunicator));

ModelArray::setDimensions(ModelArray::Type::H, { 1, 1 });
ModelArray::setDimensions(ModelArray::Type::Z, { 1, 1, 1 });

HField cice(ModelArray::Type::H);
cice = 1.0;
ModelComponent::getStore().registerArray(Protected::C_ICE, &cice, RO);
OASISCoupledOcean ocpl;
ModelMetadata metadata;

ocpl.setData(ModelState::DataMap());
ocpl.setMetadata(metadata);
ocpl.updateBefore(TimestepTime());
ocpl.updateAfter(TimestepTime());

OASIS_CHECK_ERR(oasis_c_terminate());
}
TEST_SUITE_END();
}

0 comments on commit bf7cd78

Please # to comment.