From 166ede0e271cf841a4ba2b9628a48e6741ec0a72 Mon Sep 17 00:00:00 2001 From: Alan Williams Date: Fri, 14 Oct 2016 13:35:22 -0600 Subject: [PATCH 1/3] Basic unit-test infrastructure. Demonstration unit-test creates a mesh with 1 element per processor, verifies that each element is a unit cube by looking at nodal coordinates. --- CMakeLists.txt | 6 +++ unit_tests.C | 31 +++++++++++ unit_tests/UnitTest1ElemCoordCheck.C | 78 ++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 unit_tests.C create mode 100644 unit_tests/UnitTest1ElemCoordCheck.C diff --git a/CMakeLists.txt b/CMakeLists.txt index 64ae995e..d4e0a064 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,6 +98,8 @@ add_library (nalu ${SOURCE} ${HEADER}) target_link_libraries(nalu ${Trilinos_LIBRARIES}) target_link_libraries(nalu ${YAML_LIBRARY}) +file (GLOB UNIT_TESTS_SOURCES unit_tests/*.C) + set(nalu_ex_name "naluX") message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}") if (CMAKE_BUILD_TYPE STREQUAL "DEBUG") @@ -108,4 +110,8 @@ endif() add_executable(${nalu_ex_name} nalu.C) target_link_libraries(${nalu_ex_name} nalu) + +add_executable(unit_tests unit_tests.C ${UNIT_TESTS_SOURCES}) +target_link_libraries(unit_tests nalu) + MESSAGE("\nAnd CMake says...:") diff --git a/unit_tests.C b/unit_tests.C new file mode 100644 index 00000000..fc3d9100 --- /dev/null +++ b/unit_tests.C @@ -0,0 +1,31 @@ +/*------------------------------------------------------------------------*/ +/* Copyright 2014 Sandia Corporation. */ +/* This software is released under the license detailed */ +/* in the file, LICENSE, which is located in the top-level Nalu */ +/* directory structure */ +/*------------------------------------------------------------------------*/ + +#include // for InitGoogleTest, etc +#include // for MPI_Comm_rank, MPI_Finalize, etc + +#include "include/NaluEnv.h" + +int gl_argc = 0; +char** gl_argv = 0; + +int main(int argc, char **argv) +{ + MPI_Init(&argc, &argv); + //NaluEnv will call MPI_Finalize for us. + sierra::nalu::NaluEnv::self(); + + testing::InitGoogleTest(&argc, argv); + + gl_argc = argc; + gl_argv = argv; + + int returnVal = RUN_ALL_TESTS(); + + return returnVal; +} + diff --git a/unit_tests/UnitTest1ElemCoordCheck.C b/unit_tests/UnitTest1ElemCoordCheck.C new file mode 100644 index 00000000..e8c0bc11 --- /dev/null +++ b/unit_tests/UnitTest1ElemCoordCheck.C @@ -0,0 +1,78 @@ +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void fill_mesh_1_elem_per_proc_hex8(stk::mesh::BulkData& bulk) +{ + int nprocs = bulk.parallel_size(); + std::string meshSpec = "generated:1x1x"+std::to_string(nprocs); + + stk::io::StkMeshIoBroker io(bulk.parallel()); + io.set_bulk_data(bulk); + io.add_mesh_database(meshSpec, stk::io::READ_MESH); + io.create_input_mesh(); + io.populate_bulk_data(); +} + +unsigned count_locally_owned_elems(const stk::mesh::BulkData& bulk) +{ + return stk::mesh::count_selected_entities(bulk.mesh_meta_data().locally_owned_part(), + bulk.buckets(stk::topology::ELEM_RANK)); +} + +void verify_elems_are_unit_cubes(const stk::mesh::BulkData& bulk) +{ + typedef stk::mesh::Field CoordFieldType; + CoordFieldType* coordField = bulk.mesh_meta_data().get_field(stk::topology::NODE_RANK, "coordinates"); + EXPECT_TRUE(coordField != nullptr); + + stk::mesh::EntityVector elems; + stk::mesh::get_entities(bulk, stk::topology::ELEM_RANK, elems); + + const double tolerance = std::numeric_limits::epsilon(); + + for(stk::mesh::Entity elem : elems) { + double minX = 999.99, minY = 999.99, minZ = 999.99; + double maxX = 0, maxY = 0, maxZ = 0; + const stk::mesh::Entity* nodes = bulk.begin_nodes(elem); + unsigned numNodes = bulk.num_nodes(elem); + for(unsigned i=0; i Date: Fri, 14 Oct 2016 13:53:38 -0600 Subject: [PATCH 2/3] Minor update so that unit-test executable doesn't have same name as source subdirectory. --- CMakeLists.txt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d4e0a064..056fef10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -111,7 +111,12 @@ endif() add_executable(${nalu_ex_name} nalu.C) target_link_libraries(${nalu_ex_name} nalu) -add_executable(unit_tests unit_tests.C ${UNIT_TESTS_SOURCES}) -target_link_libraries(unit_tests nalu) +set(utest_ex_name "unittestX") +if (CMAKE_BUILD_TYPE STREQUAL "DEBUG") + set(utest_ex_name "unittestXd") +endif() + +add_executable(${utest_ex_name} unit_tests.C ${UNIT_TESTS_SOURCES}) +target_link_libraries(${utest_ex_name} nalu) MESSAGE("\nAnd CMake says...:") From 2571a9c502060dd68da5feb9df6d69a1d0ec3865 Mon Sep 17 00:00:00 2001 From: Alan Williams Date: Fri, 14 Oct 2016 15:00:11 -0600 Subject: [PATCH 3/3] Added small unit-test of HexSCV::determinant. --- unit_tests/UnitTest1ElemCoordCheck.C | 19 ++----- unit_tests/UnitTestHexSCVDeterminant.C | 76 ++++++++++++++++++++++++++ unit_tests/UnitTestUtils.C | 21 +++++++ unit_tests/UnitTestUtils.h | 9 +++ 4 files changed, 112 insertions(+), 13 deletions(-) create mode 100644 unit_tests/UnitTestHexSCVDeterminant.C create mode 100644 unit_tests/UnitTestUtils.C create mode 100644 unit_tests/UnitTestUtils.h diff --git a/unit_tests/UnitTest1ElemCoordCheck.C b/unit_tests/UnitTest1ElemCoordCheck.C index e8c0bc11..55652dc1 100644 --- a/unit_tests/UnitTest1ElemCoordCheck.C +++ b/unit_tests/UnitTest1ElemCoordCheck.C @@ -9,19 +9,10 @@ #include #include #include -#include -void fill_mesh_1_elem_per_proc_hex8(stk::mesh::BulkData& bulk) -{ - int nprocs = bulk.parallel_size(); - std::string meshSpec = "generated:1x1x"+std::to_string(nprocs); - - stk::io::StkMeshIoBroker io(bulk.parallel()); - io.set_bulk_data(bulk); - io.add_mesh_database(meshSpec, stk::io::READ_MESH); - io.create_input_mesh(); - io.populate_bulk_data(); -} +#include "UnitTestUtils.h" + +namespace { unsigned count_locally_owned_elems(const stk::mesh::BulkData& bulk) { @@ -61,6 +52,8 @@ void verify_elems_are_unit_cubes(const stk::mesh::BulkData& bulk) } } +}//namespace + TEST(Basic, CheckCoords1Elem) { stk::ParallelMachine comm = MPI_COMM_WORLD; @@ -69,7 +62,7 @@ TEST(Basic, CheckCoords1Elem) stk::mesh::MetaData meta(spatialDimension); stk::mesh::BulkData bulk(meta, comm); - fill_mesh_1_elem_per_proc_hex8(bulk); + unit_test_utils::fill_mesh_1_elem_per_proc_hex8(bulk); EXPECT_EQ(1u, count_locally_owned_elems(bulk)); diff --git a/unit_tests/UnitTestHexSCVDeterminant.C b/unit_tests/UnitTestHexSCVDeterminant.C new file mode 100644 index 00000000..b2828f87 --- /dev/null +++ b/unit_tests/UnitTestHexSCVDeterminant.C @@ -0,0 +1,76 @@ +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "UnitTestUtils.h" + +namespace { + +void check_HexSCV_determinant(const stk::mesh::BulkData& bulk) +{ + typedef stk::mesh::Field CoordFieldType; + CoordFieldType* coordField = bulk.mesh_meta_data().get_field(stk::topology::NODE_RANK, "coordinates"); + EXPECT_TRUE(coordField != nullptr); + + stk::mesh::EntityVector elems; + stk::mesh::get_entities(bulk, stk::topology::ELEM_RANK, elems); + + const double tolerance = std::numeric_limits::epsilon(); + + stk::topology hex8 = stk::topology::HEX_8; + + const unsigned spatialDim = bulk.mesh_meta_data().spatial_dimension(); + std::vector hex8_node_coords(hex8.num_nodes()*spatialDim, 0.0); + std::vector hex8_scvolumes(hex8.num_nodes(), 0.0); + + sierra::nalu::HexSCV hexSCV; + double error[1] = {0}; + for(stk::mesh::Entity elem : elems) { + EXPECT_EQ(hex8, bulk.bucket(elem).topology()); + + const stk::mesh::Entity* nodes = bulk.begin_nodes(elem); + unsigned numNodes = bulk.num_nodes(elem); + unsigned counter = 0; + for(unsigned i=0; i + +#include +#include + +namespace unit_test_utils { + +void fill_mesh_1_elem_per_proc_hex8(stk::mesh::BulkData& bulk) +{ + int nprocs = bulk.parallel_size(); + std::string meshSpec = "generated:1x1x"+std::to_string(nprocs); + + stk::io::StkMeshIoBroker io(bulk.parallel()); + io.set_bulk_data(bulk); + io.add_mesh_database(meshSpec, stk::io::READ_MESH); + io.create_input_mesh(); + io.populate_bulk_data(); +} + +} + diff --git a/unit_tests/UnitTestUtils.h b/unit_tests/UnitTestUtils.h new file mode 100644 index 00000000..845bd24a --- /dev/null +++ b/unit_tests/UnitTestUtils.h @@ -0,0 +1,9 @@ + +#include + +namespace unit_test_utils { + +void fill_mesh_1_elem_per_proc_hex8(stk::mesh::BulkData& bulk); + +} +