Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test 150 #198

Merged
merged 25 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ff7c378
test.cpp is a unitTest file for getpk2cc() without GoogleTest
Feb 1, 2024
c452049
implement GTest, individually work well, not good with test.cpp
Feb 2, 2024
3dec73a
all works! next: abs path in CMakeLists, test.cpp, unitTest.xml
Feb 2, 2024
d6c5775
Merge branch 'SimVascular:main' into unit_test_150
yuecheng-yu Feb 5, 2024
a9e87c8
current_path different between compilation(GTest) and real runtime(exe)
Feb 6, 2024
c34b201
GTest: exe at build/svFSI-build/Source/svFSI, source at tests/unitTests
Feb 16, 2024
dfe18c3
cleaned CMakeLists and test.cpp
Feb 21, 2024
cae5712
Merge branch 'main' into unit_test_150
Feb 21, 2024
6c7b5f3
modified .github/workflow/test
Feb 22, 2024
279ee52
add copyright, changed file_path in test.cpp
Feb 22, 2024
768a920
update workflow, add copyright
Feb 22, 2024
1083630
revise cmakelist to disable gtest_discover_tests
Feb 23, 2024
beab321
link pthread for ubuntu20, link LAPACK for ubuntu22
Feb 23, 2024
ba6eeb9
try to link threads lib for ubuntu20
Feb 23, 2024
9a066e6
try to link pthread on ubuntu20
Feb 23, 2024
d5a8347
2nd try to link pthread on ubuntu20
Feb 26, 2024
b8138d5
last try to link pthread on ubuntu20
Feb 26, 2024
dfeb900
last after the last try to link pthread on ubuntu20
Feb 26, 2024
57014fa
use test fixture, same setup for multiple tests
Mar 1, 2024
c986d3f
At the mid of nowhere for GMock, stuck in ten_dyad_prod()
Mar 5, 2024
47c0e18
gmock works, note: initialize mat_fun_carray::ten_init(3) before
Mar 6, 2024
c912637
create a class for isotropic material unit test (only support C10, C01)
Mar 12, 2024
c5d0b31
some clean up
Mar 13, 2024
a1a6f91
add --verbose
Mar 13, 2024
2bf104c
initialize static arrays with {}
Mar 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: |
mkdir build
cd build
cmake -DENABLE_COVERAGE=ON -DENABLE_ARRAY_INDEX_CHECKING=ON ..
cmake -DENABLE_COVERAGE=ON -DENABLE_ARRAY_INDEX_CHECKING=ON -DENABLE_UNIT_TEST=ON ..
make -j2
- name: Install test dependencies
run: |
Expand All @@ -39,6 +39,10 @@ jobs:
git lfs pull
cd tests
conda run -n svfsiplus pytest -rPv --durations=0
- name: Run unit tests
run: |
cd build/svFSI-build/Source/svFSI
ctest --output-on-failure
- name: Generate code coverage
if: startsWith(matrix.os, 'ubuntu-22.04')
run: |
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ set(SV_PETSC_DIR "" CACHE STRING "Path to a local install of the PETSc linear al
set(ENABLE_COVERAGE OFF CACHE BOOL "Enable code coverage")
set(ENABLE_ARRAY_INDEX_CHECKING OFF CACHE BOOL "Enable Array index checking")
set(SV_LOCAL_VTK_PATH "" CACHE STRING "Path to a local build of VTK.")
set(ENABLE_UNIT_TEST OFF CACHE BOOL "Enable Unit Test by Google Test")

#-----------------------------------------------------------------------------
# RPATH handling
Expand Down Expand Up @@ -147,6 +148,7 @@ ExternalProject_Add(svFSI
#-DSV_USE_PETSC:BOOL=${SV_USE_PETSC}
-DSV_PETSC_DIR:STRING=${SV_PETSC_DIR}
-DENABLE_COVERAGE:BOOL=${ENABLE_COVERAGE}
-DENABLE_UNIT_TEST:BOOL=${ENABLE_UNIT_TEST}
-DENABLE_ARRAY_INDEX_CHECKING:BOOL=${ENABLE_ARRAY_INDEX_CHECKING}
-DSV_LOCAL_VTK_PATH:STRING=${SV_LOCAL_VTK_PATH}
${SV_APPLE_CMAKE_ARGS}
Expand Down
68 changes: 68 additions & 0 deletions Code/Source/svFSI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,72 @@ if(ENABLE_COVERAGE)
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
endif()

# unit tests and Google Test
if(ENABLE_UNIT_TEST)

# link pthread on ubuntu20
find_package(Threads REQUIRED)

# install Google Test
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
FetchContent_MakeAvailable(googletest)
enable_testing()
include(GoogleTest)

# add hello_test, a sample test file to check the installation of Google Test
add_executable(
hello_test
../../../tests/unitTests/hello_test.cpp
)
target_link_libraries(
hello_test
gtest
GTest::gtest_main
pthread # link pthread on ubuntu20
)
# gtest_discover_tests(hello_test)
add_test(NAME GTestSample COMMAND hello_test)
mrp089 marked this conversation as resolved.
Show resolved Hide resolved


# add test.cpp for unit test

# remove the main.cpp and add test.cpp
list(APPEND CSRCS "../../../tests/unitTests/test.cpp")
list(REMOVE_ITEM CSRCS "main.cpp")

# include source files (same as what svFSI does except for main.cpp)
add_executable(runUnitTest ${CSRCS})

# libraries
target_link_libraries(runUnitTest
${GLOBAL_LIBRARIES}
${INTELRUNTIME_LIBRARIES}
${ZLIB_LIBRARY}
${BLAS_LIBRARIES}
${LAPACK_LIBRARIES}
${METIS_SVFSI_LIBRARY_NAME}
${PARMETIS_SVFSI_LIBRARY_NAME}
${TETGEN_LIBRARY_NAME}
${TINYXML_LIBRARY_NAME}
${SV_LIB_SVFSILS_NAME}${SV_MPI_NAME_EXT}
${VTK_LIBRARIES}
)

# link Google Test
target_link_libraries(
runUnitTest
gtest
GTest::gtest_main
pthread # link pthread on ubuntu20
)
target_compile_definitions(runUnitTest PRIVATE GTEST_HAS_PTHREAD=1)
mrp089 marked this conversation as resolved.
Show resolved Hide resolved

# gtest_discover_tests(runUnitTest)
add_test(NAME UnitTest COMMAND runUnitTest)

endif()

53 changes: 53 additions & 0 deletions tests/unitTests/hello_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Copyright (c) Stanford University, The Regents of the University of California, and others.
mrp089 marked this conversation as resolved.
Show resolved Hide resolved
*
* All Rights Reserved.
*
* See Copyright-SimVascular.txt for additional details.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
* This function is adapted from Google Test's Quickstart Guide.
* Source: Google Test Quickstart for Google Test and CMake
* URL: https://google.github.io/googletest/quickstart-cmake.html
*/

#include <gtest/gtest.h>

// Demonstrate some basic assertions.
TEST(HelloTest, BasicAssertions) {
// Expect two strings not to be equal.
EXPECT_STRNE("hello", "world");
// Expect equality.
EXPECT_EQ(7 * 6, 42);
}

// Main function running the tests
int main(int argc, char **argv) {

::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();

}
3 changes: 3 additions & 0 deletions tests/unitTests/mesh/unitCube/mesh_surfaces/X0.vtp
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/unitTests/mesh/unitCube/mesh_surfaces/X1.vtp
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/unitTests/mesh/unitCube/mesh_surfaces/Y0.vtp
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/unitTests/mesh/unitCube/mesh_surfaces/Y1.vtp
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/unitTests/mesh/unitCube/mesh_surfaces/Z0.vtp
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/unitTests/mesh/unitCube/mesh_surfaces/Z1.vtp
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/unitTests/mesh/unitCube/mesh_surfaces/bot_face.vtp
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/unitTests/mesh/unitCube/mesh_surfaces/top_face.vtp
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/unitTests/mesh/unitCube/unit_cube_volume.vtu
Git LFS file not shown
186 changes: 186 additions & 0 deletions tests/unitTests/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/* Copyright (c) Stanford University, The Regents of the University of California, and others.
*
* All Rights Reserved.
*
* See Copyright-SimVascular.txt for additional details.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* unit test for get_pk2cc
* this program generates a simple svFSI simulation to test function get_pk2cc.
* it allows you to create input deformation gradients to check the output
* stress and tangent modulus. it usese Google Test for test cases.
*/

#include <stdlib.h>
#include <iomanip>
#include <iostream>
#include "gtest/gtest.h" // include GoogleTest
#include "mat_fun.h"
#include "mat_fun_carray.h"
#include "mat_models.h"
#include "mat_models_carray.h"
#include "Simulation.h"
#include "sv_struct.h"
#include "distribute.h"
#include "initialize.h"
#include "read_files.h"


// Using global variables:
// for the output: stress tensor and tangent tensor

Array<double> S_0(3,3), Dm_0(6,6); // for identity test
Array<double> S_1(3,3), Dm_1(6,6); // for non-identity test
mrp089 marked this conversation as resolved.
Show resolved Hide resolved


// Google Test test cases

TEST(ExampleTest, OneEqualsToOne) {
mrp089 marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_EQ(1, 1);
}

TEST(GetPK2ccTest, Identity) {
mrp089 marked this conversation as resolved.
Show resolved Hide resolved
// input:
mrp089 marked this conversation as resolved.
Show resolved Hide resolved
// 1 0 0
// 0 1 0
// 0 0 1
for (short i = 0; i < 3; i++){
for (short j = 0; j < 3; j++){
EXPECT_EQ(S_0(i,j), 0);
mrp089 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

TEST(GetPK2ccTest, Non_Identity) {
// input:
// 2 0 0
// 0 1 0
// 0 0 1
for (short i = 0; i < 3; i++){
for (short j = 0; j < 3; j++){
if (i == j) {
EXPECT_NE(S_1(i,j), 0);
} else {
EXPECT_EQ(S_1(i,j), 0);
}
}
}
}


void read_files(Simulation* simulation, const std::string& file_name)
{
simulation->com_mod.timer.set_time();

if (simulation->com_mod.cm.slv(simulation->cm_mod)) {
return;
}

read_files_ns::read_files(simulation, file_name);
}


// Main function running the tests
int main(int argc, char **argv) {
// define deformation gradient as input for get_pk2cc
Array<double> F_0 = mat_fun::mat_id(3);
Array<double> F_1 = mat_fun::mat_id(3);
F_1(0,0) = 2;

// Initialize MPI.
//
int mpi_rank, mpi_size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);

auto simulation = new Simulation();

std::string file_name = "../../../../tests/unitTests/unitTest.xml";
mrp089 marked this conversation as resolved.
Show resolved Hide resolved
// std::string file_name = "";
mrp089 marked this conversation as resolved.
Show resolved Hide resolved

read_files(simulation, file_name);

std::cout << "Finish reading files" << std::endl;

distribute(simulation);
Vector<double> init_time(3);
initialize(simulation, init_time);

// preparing input arguments for get_pk2cc
auto com_mod = simulation->com_mod;
auto cm_mod_ = simulation->cm_mod;
auto cm = com_mod.cm;
auto cep_mod = simulation->get_cep_mod();
const int nsd = com_mod.nsd;
auto iM = com_mod.nMsh; // nMsh = 1 from unit mesh
auto& lM = com_mod.msh[iM-1];
auto e = lM.gnEl; // global number of elements
int eNoN = lM.eNoN;
int nFn = lM.nFn;
if (nFn == 0) {nFn = 1;}
Array<double> fN(nsd,nFn);
fN = 0.0;
mrp089 marked this conversation as resolved.
Show resolved Hide resolved

if (lM.fN.size() != 0) {
mrp089 marked this conversation as resolved.
Show resolved Hide resolved
for (int iFn = 0; iFn < nFn; iFn++) {
for (int i = 0; i < nsd; i++) {
fN(i,iFn) = lM.fN(i+nsd*iFn,e);

Check warning on line 151 in tests/unitTests/test.cpp

View check run for this annotation

Codecov / codecov/patch

tests/unitTests/test.cpp#L149-L151

Added lines #L149 - L151 were not covered by tests
}
}
}

int cEq = com_mod.cEq;
auto& eq = com_mod.eq[cEq];
int cDmn = com_mod.cDmn;
auto& dmn = eq.dmn[cDmn];
double ya_g = 0.0; // ya_g or ya: a constant related to electromechanics ?

// Call get_pk2cc, which is originally called from sv_struct.cpp
mat_models::get_pk2cc(com_mod, cep_mod, dmn, F_0, nFn, fN, ya_g, S_0, Dm_0);
mat_models::get_pk2cc(com_mod, cep_mod, dmn, F_1, nFn, fN, ya_g, S_1, Dm_1);

// results printing
// std::cout <<"==================== INPUT ===================" << std::endl;
// F.print("Input Deformation ");
// std::cout <<"=================== OUTPUT ===================" << std::endl;
// S.print("Output stress tensor:");
// Dm.print("Output stiffness matrix:");

// the following is where each argument of get_pk2cc from:
// mat_models_carray::get_pk2cc<3>(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm);
// from struct_3d(...): com_mod, cep_mod, dmn <- eq <- com_mod, nFn, fN
// from construct_dsolid(...): com_mod, cep_mod, nFn <- lM, fN <- lM
// from global_eq_assem(...): com_mod, cep_mod, lM
// from iterate_simulation(simulation): com_mod <- simulation, cep_mod <- simulation,
// lM <- com_mod.msh[iM] (iM < com_mod.nMsh)
// iterate_simulation(simulation) <- run_simulation(simulation) <- main(...)
mrp089 marked this conversation as resolved.
Show resolved Hide resolved

// run Google Test
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();

}
Loading
Loading