Skip to content
Snippets Groups Projects
Commit eb03cdc8 authored by Thibault Lestang's avatar Thibault Lestang
Browse files

Merge branch 'minimal_testing' into 'master'

Add unit test for BwdTrans operator

See merge request !4
parents 650da278 c414c2cf
No related branches found
Tags v4.3.4
1 merge request!4Add unit test for BwdTrans operator
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
project(Redesign CXX) project(Redesign CXX)
enable_testing()
option(NEKTAR_USE_CUDA "Enable CUDA support" FALSE) option(NEKTAR_USE_CUDA "Enable CUDA support" FALSE)
option(NEKTAR_USE_SIMD "Enable SIMD support, if available" TRUE) option(NEKTAR_USE_SIMD "Enable SIMD support, if available" TRUE)
...@@ -49,5 +51,7 @@ target_link_libraries(main Operators) ...@@ -49,5 +51,7 @@ target_link_libraries(main Operators)
target_include_directories(main PRIVATE "${CMAKE_SOURCE_DIR}") target_include_directories(main PRIVATE "${CMAKE_SOURCE_DIR}")
target_compile_definitions(main PUBLIC ${NEKTAR++_DEFINITIONS}) target_compile_definitions(main PUBLIC ${NEKTAR++_DEFINITIONS})
add_subdirectory(tests)
find_package(Doxygen) find_package(Doxygen)
add_custom_target(doc COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_SOURCE_DIR}/Doxyfile) add_custom_target(doc COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_SOURCE_DIR}/Doxyfile)
set(test_src test_bwdtrans.cpp)
find_package(
Boost
REQUIRED
COMPONENTS unit_test_framework
)
add_executable(test_bwdtrans ${test_src})
target_link_libraries(test_bwdtrans PRIVATE Operators)
target_link_libraries(test_bwdtrans PRIVATE ${NEKTAR++_LIBRARIES})
target_link_libraries(test_bwdtrans PRIVATE Boost::unit_test_framework)
target_include_directories(test_bwdtrans PRIVATE "${CMAKE_SOURCE_DIR}")
target_include_directories(test_bwdtrans PRIVATE ${NEKTAR++_INCLUDE_DIRS})
add_test(
NAME BwdTrans
COMMAND test_bwdtrans
# Test executable is looking for input session file in the working
# directory
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/input"
)
#include <boost/test/unit_test_log.hpp>
#include <vector>
#include <string>
#include "Field.hpp"
#include <MultiRegions/ExpList.h>
#include <Operators/OperatorBwdTrans.hpp>
using namespace Nektar::Operators;
using namespace Nektar::LibUtilities;
using namespace Nektar;
/**
* @struct InitFields
*
* A test fixture responsible for making available input and output
* Field objects. The structure constructor (destrucor) is called
* before (after) each call to BOOST_FIXTURE_TEST_CASE(<test name>,
* InitFields) macro.
*
* See "Single test case fixture" on the Boost.Test documentation for more details:
* https://www.boost.org/doc/libs/1_82_0/libs/test/doc/html/boost_test/tests_organization/fixtures/case.html
*/
struct InitFields {
Field<double, FieldState::Coeff> *fixt_in;
Field<double, FieldState::Phys> *fixt_out;
MultiRegions::ExpListSharedPtr fixt_explist {nullptr};
~InitFields() {BOOST_TEST_MESSAGE("teardown fixture");}
static std::vector<BlockAttributes>
GetBlockAttributes(FieldState state,
const MultiRegions::ExpListSharedPtr explist) {
const int n = explist->GetNumElmts();
std::map<std::tuple<LibUtilities::ShapeType,unsigned int,unsigned int>,size_t> blockList;
for (int i = 0; i < explist->GetNumElmts(); ++i)
{
auto e = explist->GetExp(i);
blockList[{e->DetShapeType(),e->GetNcoeffs(),e->GetTotPoints()}]++;
}
std::vector<BlockAttributes> blockAttr;
for (auto &x : blockList)
{
auto val = state == FieldState::Phys ? std::get<2>(x.first) : std::get<1>(x.first);
blockAttr.push_back( { x.second, val } );
}
return blockAttr;
}
InitFields() {
BOOST_TEST_MESSAGE("Creating input and output fields");
// Initialise a session, graph and create an expansion list
LibUtilities::SessionReaderSharedPtr session;
SpatialDomains::MeshGraphSharedPtr graph;
// Construct a fake command-line argument array to be fed to
// Session::Reader::CreateInstance. The first element stands for
// the name of the executable which, in our case, doesn't matter.
int argc = 2;
char *argv[] = {
(char*)"exe_name", (char*)"square.xml"
};
session = LibUtilities::SessionReader::CreateInstance(argc, argv);
graph = SpatialDomains::MeshGraph::Read(session);
fixt_explist = MemoryManager<MultiRegions::ExpList>::AllocateSharedPtr
(session, graph);
// Generate a blocks definition from the expansion list for each state
auto blocks_phys = InitFields::GetBlockAttributes(FieldState::Phys, fixt_explist);
auto blocks_coeff = InitFields::GetBlockAttributes(FieldState::Coeff, fixt_explist);
// Create two Field objects with a MemoryRegionCPU backend by default
auto f_in = Field<double, FieldState::Coeff>::create(blocks_coeff);
auto f_out = Field<double, FieldState::Phys >::create(blocks_phys);
fixt_in = new Field<double, FieldState::Coeff>(std::move(f_in));
fixt_out = new Field<double, FieldState::Phys>(std::move(f_out));
}
};
#define BOOST_TEST_MODULE example
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <memory>
#include "Field.hpp"
#include "Operators/OperatorBwdTrans.hpp"
#include <LibUtilities/BasicUtils/SessionReader.h>
#include <SpatialDomains/MeshGraph.h>
#include <MultiRegions/ExpList.h>
#include "init_fields.hpp"
using namespace std;
using namespace Nektar::Operators;
using namespace Nektar::LibUtilities;
using namespace Nektar;
BOOST_FIXTURE_TEST_CASE( bwdtrans, InitFields )
{
double *x = fixt_in->GetStorage().GetCPUPtr();
// For each element, initialise first coefficient to zero and rest
// to 1.
for (auto const &block : fixt_in->GetBlocks())
{
for (size_t el = 0; el < block.num_elements; ++el)
{
for (size_t coeff = 0; coeff < block.num_pts; ++coeff)
{
if (coeff == 0) {
*(x++) = 1.0;
}
else {
*(x++) = 0.0;
}
}
}
}
BwdTrans<>::create(fixt_explist, "StdMat")->apply(*fixt_in, *fixt_out);
double *y = fixt_out->GetStorage().GetCPUPtr();
BOOST_TEST( y[0] == 1.0 );
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment