From ff7c3783d78d81943042b9064427ee3b9d972a54 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Thu, 1 Feb 2024 11:18:08 -0800 Subject: [PATCH 01/23] test.cpp is a unitTest file for getpk2cc() without GoogleTest --- Code/Source/svFSI/CMakeLists.txt | 103 +++++++++++ .../METISLib/temp/metis.h | 6 +- rebuild.sh | 12 ++ tests/unitTests/CMakeLists.txt | 25 +++ tests/unitTests/load.dat | 3 + .../mesh/unitCube/mesh_surfaces/X0.vtp | 3 + .../mesh/unitCube/mesh_surfaces/X1.vtp | 3 + .../mesh/unitCube/mesh_surfaces/Y0.vtp | 3 + .../mesh/unitCube/mesh_surfaces/Y1.vtp | 3 + .../mesh/unitCube/mesh_surfaces/Z0.vtp | 3 + .../mesh/unitCube/mesh_surfaces/Z1.vtp | 3 + .../mesh/unitCube/mesh_surfaces/bot_face.vtp | 3 + .../mesh/unitCube/mesh_surfaces/top_face.vtp | 3 + .../mesh/unitCube/unit_cube_volume.vtu | 3 + tests/unitTests/test.cpp | 164 ++++++++++++++++++ tests/unitTests/unitTest.xml | 120 +++++++++++++ 16 files changed, 457 insertions(+), 3 deletions(-) create mode 100755 rebuild.sh create mode 100644 tests/unitTests/CMakeLists.txt create mode 100755 tests/unitTests/load.dat create mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/X0.vtp create mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/X1.vtp create mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/Y0.vtp create mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/Y1.vtp create mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/Z0.vtp create mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/Z1.vtp create mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/bot_face.vtp create mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/top_face.vtp create mode 100644 tests/unitTests/mesh/unitCube/unit_cube_volume.vtu create mode 100644 tests/unitTests/test.cpp create mode 100644 tests/unitTests/unitTest.xml diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index 0c1bb341..8296d148 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -215,3 +215,106 @@ if(ENABLE_COVERAGE) endif() +# add_executable(runUnitTest ../../../tests/unitTests/test.cpp) +# target_include_directories(runUnitTest PRIVATE ${SV_INCLUDE_DIRS}) + +# replace main.cpp +set(CSRCS_unitTest +../../../tests/unitTests/test.cpp + + Array3.h Array3.cpp + Array.h Array.cpp + Tensor4.h Tensor4.cpp + Vector.h Vector.cpp + + lapack_defs.h + + DebugMsg.h + Parameters.h Parameters.cpp + Simulation.h Simulation.cpp + SimulationLogger.h + VtkData.h VtkData.cpp + + all_fun.h all_fun.cpp + baf_ini.h baf_ini.cpp + bf.h bf.cpp + cep.h cep.cpp + cep_ion.h cep_ion.cpp + cmm.h cmm.cpp + consts.h consts.cpp + contact.h contact.cpp + distribute.h distribute.cpp + eq_assem.h eq_assem.cpp + fluid.h fluid.cpp + fsi.h fsi.cpp + fs.h fs.cpp + fft.h fft.cpp + heatf.h heatf.cpp + heats.h heats.cpp + initialize.h initialize.cpp + l_elas.h l_elas.cpp + lhsa.h lhsa.cpp + ls.h ls.cpp + + mat_fun.h mat_fun.cpp + mat_fun_carray.h mat_fun_carray.cpp + mat_models.h mat_models.cpp + mesh.h mesh.cpp + nn.h nn.cpp + output.h output.cpp + load_msh.h load_msh.cpp + pic.h pic.cpp + post.h post.cpp + read_files.h read_files.cpp + read_msh.h read_msh.cpp + remesh.h remesh.cpp + remeshTet.cpp + set_bc.h set_bc.cpp + shells.h shells.cpp + stokes.h stokes.cpp + sv_struct.h sv_struct.cpp + txt.h txt.cpp + utils.h utils.cpp + ustruct.h ustruct.cpp + vtk_xml.h vtk_xml.cpp + vtk_xml_parser.h vtk_xml_parser.cpp + + CepMod.h CepMod.cpp + CepModAp.h CepModAp.cpp + CepModBo.h CepModBo.cpp + CepModFn.h CepModFn.cpp + CepModTtp.h CepModTtp.cpp + ChnlMod.h ChnlMod.cpp + CmMod.h CmMod.cpp + ComMod.h ComMod.cpp + Timer.h + + SPLIT.c +) + +simvascular_add_executable(runUnitTest + SRCS ${CSRCS_unitTest} + # DEV_SCRIPT_NAME "hhhhhmysvfsi" + # INSTALL_SCRIPT_NAME "hhhhhcsvfsi" + INSTALL_COMP CoreExecutables + INSTALL_DESTINATION ${SV_INSTALL_RUNTIME_DIR}) + + +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} + ) + +if(SV_MPI_EXTRA_LIBRARY) + target_link_libraries(runUnitTest ${SV_MPI_EXTRA_LIBRARY}) +endif() + diff --git a/Code/ThirdParty/metis_svfsi/simvascular_metis_svfsi/METISLib/temp/metis.h b/Code/ThirdParty/metis_svfsi/simvascular_metis_svfsi/METISLib/temp/metis.h index 4d421193..c8cd6cde 100644 --- a/Code/ThirdParty/metis_svfsi/simvascular_metis_svfsi/METISLib/temp/metis.h +++ b/Code/ThirdParty/metis_svfsi/simvascular_metis_svfsi/METISLib/temp/metis.h @@ -30,6 +30,7 @@ GCC does provides these definitions in stdint.h, but it may require some modifications on other architectures. --------------------------------------------------------------------------*/ +// IDXTYPEWIDTH must be set to 32, setting it to 64 causes a failure. #define IDXTYPEWIDTH 32 /*-------------------------------------------------------------------------- @@ -39,9 +40,8 @@ 32 : single precision floating point (float) 64 : double precision floating point (double) --------------------------------------------------------------------------*/ -//#define REALTYPEWIDTH 32 -#define REALTYPEWIDTH 64 - +// REALTYPEWIDTH must be set to 32, setting it to 64 causes a failure. +#define REALTYPEWIDTH 32 /**************************************************************************** * In principle, nothing needs to be changed beyond this point, unless the diff --git a/rebuild.sh b/rebuild.sh new file mode 100755 index 00000000..0ed949b5 --- /dev/null +++ b/rebuild.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +rm -rf build + +mkdir build + +cd build + +cmake .. + +make -j10 + diff --git a/tests/unitTests/CMakeLists.txt b/tests/unitTests/CMakeLists.txt new file mode 100644 index 00000000..0f6faf22 --- /dev/null +++ b/tests/unitTests/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.10) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +# Set the project name +project(UnitTests) + +# Set the relative path to svFSIplus include directory +set(SVFSI_INCLUDE_DIR ../../Code/Source/svFSI) +# set(SVFSI_THIRDPARTY ../../Code/ThirdParty/msmpi/Include) + +# Include the directory +include_directories(${SVFSI_INCLUDE_DIR}) +# include_directories(${SVFSI_THIRDPARTY}) + +# Define test executable +add_executable(runUnitTests test.cpp) + +# Link against Google Test and svFSIplus libraries +# target_link_libraries(runUnitTests gtest gtest_main) + +# Add the test so it can be run with `make test` +# add_test(NAME that_test COMMAND runUnitTests) + diff --git a/tests/unitTests/load.dat b/tests/unitTests/load.dat new file mode 100755 index 00000000..01ab643c --- /dev/null +++ b/tests/unitTests/load.dat @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4e1e526c76451ab4f63d75950722b8e808791a83bbaa706dd1002816d8e202ac +size 62 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/X0.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/X0.vtp new file mode 100644 index 00000000..8ab28a46 --- /dev/null +++ b/tests/unitTests/mesh/unitCube/mesh_surfaces/X0.vtp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:33ebdfa2169105655dc4fbc0486eb6accee067487a986d6e0d288a54f11a3459 +size 6001 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/X1.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/X1.vtp new file mode 100644 index 00000000..b51534a8 --- /dev/null +++ b/tests/unitTests/mesh/unitCube/mesh_surfaces/X1.vtp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fdb6efe7dd00b3fa098c9292c41decb568a99997255d847b2b4f257281555978 +size 6005 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/Y0.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/Y0.vtp new file mode 100644 index 00000000..0c330423 --- /dev/null +++ b/tests/unitTests/mesh/unitCube/mesh_surfaces/Y0.vtp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0269e2a9d7772bd0b078af4e320e020ab39aaaec0eff014b7220abdd14e1a3a5 +size 5997 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/Y1.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/Y1.vtp new file mode 100644 index 00000000..0ae4e7e3 --- /dev/null +++ b/tests/unitTests/mesh/unitCube/mesh_surfaces/Y1.vtp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8553d38c10d49b06653615c7e8ecbaa5bc999fa94c67b98fdc791ab7ee75737a +size 6005 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/Z0.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/Z0.vtp new file mode 100644 index 00000000..da1aef5e --- /dev/null +++ b/tests/unitTests/mesh/unitCube/mesh_surfaces/Z0.vtp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8a109c4f205f4bdf4517a8f9fd21214732163996e512591efbbd90840d51830f +size 6001 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/Z1.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/Z1.vtp new file mode 100644 index 00000000..55dc9986 --- /dev/null +++ b/tests/unitTests/mesh/unitCube/mesh_surfaces/Z1.vtp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ff245d63f5f21b6fdcc388b5aecea18865b32833b0c4a014841c69d8fbd3bba8 +size 6001 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/bot_face.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/bot_face.vtp new file mode 100644 index 00000000..1c2c6b9d --- /dev/null +++ b/tests/unitTests/mesh/unitCube/mesh_surfaces/bot_face.vtp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:35a04c93417bc8edc792dabe290d2fdc2cd4c9d22fc008feb1bec035623aae96 +size 2109 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/top_face.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/top_face.vtp new file mode 100644 index 00000000..1c2c6b9d --- /dev/null +++ b/tests/unitTests/mesh/unitCube/mesh_surfaces/top_face.vtp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:35a04c93417bc8edc792dabe290d2fdc2cd4c9d22fc008feb1bec035623aae96 +size 2109 diff --git a/tests/unitTests/mesh/unitCube/unit_cube_volume.vtu b/tests/unitTests/mesh/unitCube/unit_cube_volume.vtu new file mode 100644 index 00000000..d04eef5d --- /dev/null +++ b/tests/unitTests/mesh/unitCube/unit_cube_volume.vtu @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ed473845550ea1347d62290da2b9819d9620a8f6aaf520c799d70f7bceec0285 +size 1807 diff --git a/tests/unitTests/test.cpp b/tests/unitTests/test.cpp new file mode 100644 index 00000000..3e8fc3bb --- /dev/null +++ b/tests/unitTests/test.cpp @@ -0,0 +1,164 @@ +/* unit test for get_pk2cc +* +* +*/ + + +// include header files from svFSIplus +#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" + +// from main: +#include "all_fun.h" +#include "bf.h" +#include "contact.h" +#include "distribute.h" +#include "eq_assem.h" +#include "fs.h" +#include "initialize.h" +#include "ls.h" +#include "output.h" +#include "pic.h" +#include "read_files.h" +#include "read_msh.h" +#include "remesh.h" +#include "set_bc.h" +#include "txt.h" +#include "ustruct.h" +#include "vtk_xml.h" + + +// include libraries +#include +#include +#include + + +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); + +/* + try { + read_files_ns::read_files(simulation, file_name); + + } catch (const std::exception& exception) { + std::cout << "[svFSIplus] ERROR The svFSIplus program has failed." << std::endl; + std::cout << "[svFSIplus] ERROR " << exception.what() << std::endl; + exit(1); + } +*/ + +} + +int main(int argc, char *argv[]) { + + // 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); + + // Original: initiate in main(): + + auto simulation = new Simulation(); + + // filename for the unit element .xml: (To be complete !!) + // std::string file_name = "unitTest.xml"; + + std::string file_name(argv[1]); + read_files(simulation, file_name); + + std::cout << "Finish reading files" << std::endl; + + distribute(simulation); + Vector init_time(3); + initialize(simulation, init_time); + + // Original: define from struct_3d/struct_3d_carray(...) <- construct_dsolid(...) + + // ya_g or ya: a constant related to electromechanics ?? + double ya_g = 0.0; + // S, Dm: target variables + Array S(3,3), Dm(6,6); + + 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; + + std::cout << "The number of meshes: " << com_mod.nMsh << std::endl; + + 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 + + std::cout << "The number of elements: " << e << std::endl; + + int eNoN = lM.eNoN; + int nFn = lM.nFn; + if (nFn == 0) { + nFn = 1; + } + std::cout << "The variable eNoN is : " << eNoN << std::endl; + std::cout << "The variable nFn is : " << nFn << std::endl; + + Array fN(nsd,nFn); + fN = 0.0; + if (lM.fN.size() != 0) { + for (int iFn = 0; iFn < nFn; iFn++) { + for (int i = 0; i < nsd; i++) { + fN(i,iFn) = lM.fN(i+nsd*iFn,e); + } + } + } + + + int cEq = com_mod.cEq; + auto& eq = com_mod.eq[cEq]; + int cDmn = com_mod.cDmn; + auto& dmn = eq.dmn[cDmn]; + + std::cout << "Before calling get_pk2cc: input argument F" << std::endl; + std::cout << "=================== INPUT ===================" << std::endl; + // F: deformation gradient + Array F(3,3); + F = mat_fun::mat_id(3); + // F(0, 0) = 2.0; + const auto& stM = dmn.stM; + std::cout << "Material type: " << stM.isoType << std::endl; + F.print("Input Deformation "); + + // Original: call in sv_struct.cpp + mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); + + std::cout <<"=================== OUTPUT ===================" << std::endl; + std::cout << "After calling get_pk2cc: output S and Dm" << std::endl; + S.print("Output stress tensor:"); + + std::cout << "After calling get_pk2cc: output S and Dm" << std::endl; + Dm.print("Output stiffness matrix:"); + + // 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(...) + + std::cout << "This is svFSIplus/tests/unitTests/test.cpp now!" << std::endl; + +} diff --git a/tests/unitTests/unitTest.xml b/tests/unitTests/unitTest.xml new file mode 100644 index 00000000..8530e380 --- /dev/null +++ b/tests/unitTests/unitTest.xml @@ -0,0 +1,120 @@ + + + + + 0 + 3 + 20 + 0.01 + 0.50 + STOP_SIM + + 1 + result + 1 + 1 + + 2 + 0 + + 1 + 0 + 0 + + + + + + + mesh/unitCube/unit_cube_volume.vtu + + + mesh/unitCube/mesh_surfaces/Z0.vtp + + + + mesh/unitCube/mesh_surfaces/Z1.vtp + + + + mesh/unitCube/mesh_surfaces/Y0.vtp + + + + mesh/unitCube/mesh_surfaces/Y1.vtp + + + + mesh/unitCube/mesh_surfaces/X0.vtp + + + + mesh/unitCube/mesh_surfaces/X1.vtp + + + 0.001 + + + + + + + true + 3 + 10 + 1e-6 + + + 0.0 + 240.56596E6 + 0.5 + + ST91 + 4.0E9 + + + true + true + true + true + true + true + true + true + + + + FSILS + 1e-6 + 1000 + + + + Dir + 0.0 + (0, 0, 1) + + + + Dir + 0.0 + (0, 1, 0) + + + + Dir + 0.0 + (1, 0, 0) + + + + Neu + Unsteady + load.dat + + + + + + + From c452049dc0c433fd0d1d133c4770df6648c0f237 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Thu, 1 Feb 2024 22:00:12 -0800 Subject: [PATCH 02/23] implement GTest, individually work well, not good with test.cpp --- Code/CMakeLists.txt | 9 +++++ Code/Source/svFSI/CMakeLists.txt | 63 +++++++++++++++++++++++++++--- tests/unitTests/hello_test.cpp | 17 ++++++++ tests/unitTests/test.cpp | 67 +++++++++++++++++++++++--------- 4 files changed, 132 insertions(+), 24 deletions(-) create mode 100644 tests/unitTests/hello_test.cpp diff --git a/Code/CMakeLists.txt b/Code/CMakeLists.txt index c541d1de..82fbd7b1 100644 --- a/Code/CMakeLists.txt +++ b/Code/CMakeLists.txt @@ -164,3 +164,12 @@ endif() include(SimVascularInstallLibs) add_subdirectory(Scripts) #----------------------------------------------------------------------------- + + +# Add unit test +# add_subdirectory( +# "${CMAKE_CURRENT_SOURCE_DIR}/../tests/unitTests" # Source directory, relative path from current CMakeLists.txt +# "${CMAKE_CURRENT_BINARY_DIR}/tests/unitTests" # Binary directory, where the built files will go +# ) + +# add_executable(runUnitTest ../tests/unitTests/test.cpp) \ No newline at end of file diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index 8296d148..166b8349 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -215,8 +215,50 @@ if(ENABLE_COVERAGE) endif() -# add_executable(runUnitTest ../../../tests/unitTests/test.cpp) -# target_include_directories(runUnitTest PRIVATE ${SV_INCLUDE_DIRS}) +# add Google Test: + +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip +) +# For Windows: Prevent overriding the parent project's compiler/linker settings +# set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + +FetchContent_MakeAvailable(googletest) + +enable_testing() + +add_executable( + hello_test + ../../../tests/unitTests/hello_test.cpp +) +target_link_libraries( + hello_test + GTest::gtest_main +) + +set_target_properties( + hello_test + PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../../../tests/unitTests/build" +) + +include(GoogleTest) +gtest_discover_tests(hello_test) + + + + + + + + + + + + +# add test.cpp: # replace main.cpp set(CSRCS_unitTest @@ -299,6 +341,12 @@ simvascular_add_executable(runUnitTest INSTALL_COMP CoreExecutables INSTALL_DESTINATION ${SV_INSTALL_RUNTIME_DIR}) +# add_executabdle(runUnitTest ${CSRCS_unitTest}) + +# target_link_libraries( +# runUnitTest +# GTest::gtest_main +# ) target_link_libraries(runUnitTest ${GLOBAL_LIBRARIES} @@ -312,9 +360,12 @@ target_link_libraries(runUnitTest ${TINYXML_LIBRARY_NAME} ${SV_LIB_SVFSILS_NAME}${SV_MPI_NAME_EXT} ${VTK_LIBRARIES} - ) +) -if(SV_MPI_EXTRA_LIBRARY) - target_link_libraries(runUnitTest ${SV_MPI_EXTRA_LIBRARY}) -endif() +set_target_properties( + runUnitTest + PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../../../tests/unitTests/build" +) +# gtest_discover_tests(runUnitTest) diff --git a/tests/unitTests/hello_test.cpp b/tests/unitTests/hello_test.cpp new file mode 100644 index 00000000..5f062148 --- /dev/null +++ b/tests/unitTests/hello_test.cpp @@ -0,0 +1,17 @@ +#include + +// 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(); + +} diff --git a/tests/unitTests/test.cpp b/tests/unitTests/test.cpp index 3e8fc3bb..4bd30d83 100644 --- a/tests/unitTests/test.cpp +++ b/tests/unitTests/test.cpp @@ -3,6 +3,8 @@ * */ +// include GoogleTest +// #include "gtest/gtest.h" // include header files from svFSIplus #include "mat_fun.h" @@ -37,6 +39,19 @@ #include #include +// Using global variables +Simulation *simulation; +ComMod com_mod; +CmMod cm_mod_; +cmType cm; +CepMod cep_mod; + +// S, Dm: target variables +Array S(3,3), Dm(6,6); +// I: identity matrix +Array I = mat_fun::mat_id(3); + + void read_files(Simulation* simulation, const std::string& file_name) { @@ -61,23 +76,31 @@ void read_files(Simulation* simulation, const std::string& file_name) } -int main(int argc, char *argv[]) { +// TEST(Dummy, Dummylollll) { +// EXPECT_EQ(1, 0); +// } +// TEST(GetPK2ccTest, Identity) { +// EXPECT_EQ(S(0,0), I(0,0)); // I: Identity 3x3 matrix +// } + +// Main function running the tests +int main(int argc, char **argv) { // Initialize MPI. // int mpi_rank, mpi_size; + // MPI_Init(&argc, &argv); MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); MPI_Comm_size(MPI_COMM_WORLD, &mpi_size); - // Original: initiate in main(): - auto simulation = new Simulation(); + simulation = new Simulation(); // filename for the unit element .xml: (To be complete !!) - // std::string file_name = "unitTest.xml"; + std::string file_name = "/Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/unitTest.xml"; - std::string file_name(argv[1]); + // std::string file_name(argv[1]); read_files(simulation, file_name); std::cout << "Finish reading files" << std::endl; @@ -87,16 +110,11 @@ int main(int argc, char *argv[]) { initialize(simulation, init_time); // Original: define from struct_3d/struct_3d_carray(...) <- construct_dsolid(...) - - // ya_g or ya: a constant related to electromechanics ?? - double ya_g = 0.0; - // S, Dm: target variables - Array S(3,3), Dm(6,6); - auto& com_mod = simulation->com_mod; - auto& cm_mod = simulation->cm_mod; - auto& cm = com_mod.cm; - auto& cep_mod = simulation->get_cep_mod(); + com_mod = simulation->com_mod; + cm_mod_ = simulation->cm_mod; + cm = com_mod.cm; + cep_mod = simulation->get_cep_mod(); const int nsd = com_mod.nsd; @@ -125,21 +143,27 @@ int main(int argc, char *argv[]) { } } } - int cEq = com_mod.cEq; auto& eq = com_mod.eq[cEq]; int cDmn = com_mod.cDmn; auto& dmn = eq.dmn[cDmn]; + + + // ya_g or ya: a constant related to electromechanics ?? + double ya_g = 0.0; + + + std::cout << "Before calling get_pk2cc: input argument F" << std::endl; std::cout << "=================== INPUT ===================" << std::endl; // F: deformation gradient Array F(3,3); - F = mat_fun::mat_id(3); + F = I; // F(0, 0) = 2.0; - const auto& stM = dmn.stM; - std::cout << "Material type: " << stM.isoType << std::endl; + // const auto& stM = dmn.stM; + // std::cout << "Material type: " << stM.isoType << std::endl; F.print("Input Deformation "); // Original: call in sv_struct.cpp @@ -161,4 +185,11 @@ int main(int argc, char *argv[]) { std::cout << "This is svFSIplus/tests/unitTests/test.cpp now!" << std::endl; + std::cout << "Let's try EXPECT_EQ(S, actual_value)" << std::endl; + + + + // ::testing::InitGoogleTest(&argc, argv); + // return RUN_ALL_TESTS(); + } From 3dec73a8a8dd9287085e7d8af9f3cca6df5dbebf Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Thu, 1 Feb 2024 23:12:23 -0800 Subject: [PATCH 03/23] all works! next: abs path in CMakeLists, test.cpp, unitTest.xml --- Code/Source/svFSI/CMakeLists.txt | 39 ++++++++++++++++++++++---------- tests/unitTests/test.cpp | 24 ++++++++++++-------- tests/unitTests/unitTest.xml | 16 ++++++------- 3 files changed, 50 insertions(+), 29 deletions(-) diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index 166b8349..7a59603e 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -334,19 +334,25 @@ set(CSRCS_unitTest SPLIT.c ) -simvascular_add_executable(runUnitTest - SRCS ${CSRCS_unitTest} - # DEV_SCRIPT_NAME "hhhhhmysvfsi" - # INSTALL_SCRIPT_NAME "hhhhhcsvfsi" - INSTALL_COMP CoreExecutables - INSTALL_DESTINATION ${SV_INSTALL_RUNTIME_DIR}) +set(UNIT_TEST_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../tests/unitTests/build") +message(UNIT_TEST_DIR = "${UNIT_TEST_DIR}") + +# seems only the first line affects, IDN what other lines for...... +# simvascular_add_executable(runUnitTest +# SRCS ${CSRCS_unitTest} +# # DEV_SCRIPT_NAME "hhhhhmysvfsi" +# # INSTALL_SCRIPT_NAME "hhhhhcsvfsi" +# # INSTALL_COMP CoreExecutables +# # INSTALL_DESTINATION ${SV_INSTALL_RUNTIME_DIR} +# # INSTALL_DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}/../../../tests/unitTests/build" +# # INSTALL_DESTINATION ${UNIT_TEST_DIR} +# ) + + +# the following line works to build in svFSI-build/bin/runUnitTest +add_executable(runUnitTest ${CSRCS_unitTest}) -# add_executabdle(runUnitTest ${CSRCS_unitTest}) -# target_link_libraries( -# runUnitTest -# GTest::gtest_main -# ) target_link_libraries(runUnitTest ${GLOBAL_LIBRARIES} @@ -362,10 +368,19 @@ target_link_libraries(runUnitTest ${VTK_LIBRARIES} ) +# PROBLEM: the following operations doesn't work for svFSIplus to read mesh file!!!! +# SOLUTION: change all path in .xml file to absolute path +# for example: /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/and go on... set_target_properties( runUnitTest PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../../../tests/unitTests/build" ) -# gtest_discover_tests(runUnitTest) +# PROBLEM: the following linking to GTest will cause failure to read mesh file!!!!! +target_link_libraries( + runUnitTest + GTest::gtest_main +) + +gtest_discover_tests(runUnitTest) diff --git a/tests/unitTests/test.cpp b/tests/unitTests/test.cpp index 4bd30d83..84215e58 100644 --- a/tests/unitTests/test.cpp +++ b/tests/unitTests/test.cpp @@ -4,7 +4,7 @@ */ // include GoogleTest -// #include "gtest/gtest.h" +#include "gtest/gtest.h" // include header files from svFSIplus #include "mat_fun.h" @@ -50,6 +50,8 @@ CepMod cep_mod; Array S(3,3), Dm(6,6); // I: identity matrix Array I = mat_fun::mat_id(3); +// Z: zero matrix +Array Z(3,3); @@ -76,13 +78,17 @@ void read_files(Simulation* simulation, const std::string& file_name) } -// TEST(Dummy, Dummylollll) { -// EXPECT_EQ(1, 0); -// } +TEST(Dummy, OneEqualsToOne) { + EXPECT_EQ(1, 1); +} -// TEST(GetPK2ccTest, Identity) { -// EXPECT_EQ(S(0,0), I(0,0)); // I: Identity 3x3 matrix -// } +TEST(GetPK2ccTest, Identity) { + for (short i = 0; i < 3; i++){ + for (short j = 0; j < 3; j++){ + EXPECT_EQ(S(i,j), Z(i,j)); + } + } +} // Main function running the tests int main(int argc, char **argv) { @@ -189,7 +195,7 @@ int main(int argc, char **argv) { - // ::testing::InitGoogleTest(&argc, argv); - // return RUN_ALL_TESTS(); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); } diff --git a/tests/unitTests/unitTest.xml b/tests/unitTests/unitTest.xml index 8530e380..0ae7fb5b 100644 --- a/tests/unitTests/unitTest.xml +++ b/tests/unitTests/unitTest.xml @@ -26,30 +26,30 @@ - mesh/unitCube/unit_cube_volume.vtu + /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/mesh/unitCube/unit_cube_volume.vtu - mesh/unitCube/mesh_surfaces/Z0.vtp + /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/mesh/unitCube/mesh_surfaces/Z0.vtp - mesh/unitCube/mesh_surfaces/Z1.vtp + /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/mesh/unitCube/mesh_surfaces/Z1.vtp - mesh/unitCube/mesh_surfaces/Y0.vtp + /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/mesh/unitCube/mesh_surfaces/Y0.vtp - mesh/unitCube/mesh_surfaces/Y1.vtp + /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/mesh/unitCube/mesh_surfaces/Y1.vtp - mesh/unitCube/mesh_surfaces/X0.vtp + /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/mesh/unitCube/mesh_surfaces/X0.vtp - mesh/unitCube/mesh_surfaces/X1.vtp + /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/mesh/unitCube/mesh_surfaces/X1.vtp 0.001 @@ -110,7 +110,7 @@ Neu Unsteady - load.dat + /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/load.dat From a9e87c83d87937bb912fce759b4867d10e1963a8 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Mon, 5 Feb 2024 22:45:07 -0800 Subject: [PATCH 04/23] current_path different between compilation(GTest) and real runtime(exe) --- tests/unitTests/test.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/unitTests/test.cpp b/tests/unitTests/test.cpp index 84215e58..2800ba3b 100644 --- a/tests/unitTests/test.cpp +++ b/tests/unitTests/test.cpp @@ -92,6 +92,9 @@ TEST(GetPK2ccTest, Identity) { // Main function running the tests int main(int argc, char **argv) { + // std::cout << "Current working directory: " + // << std::filesystem::current_path() << std::endl; + // Initialize MPI. // int mpi_rank, mpi_size; @@ -104,9 +107,12 @@ int main(int argc, char **argv) { simulation = new Simulation(); // filename for the unit element .xml: (To be complete !!) - std::string file_name = "/Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/unitTest.xml"; - + // current_path: /path_to_svFSIplus/svFSIplus/build/svFSI-build/Source/svFSI + std::string file_name = std::filesystem::current_path().string() + "/../../../../tests/unitTests/unitTest.xml"; + // std::string file_name = "/Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/unitTest.xml"; // std::string file_name(argv[1]); + + read_files(simulation, file_name); std::cout << "Finish reading files" << std::endl; @@ -194,7 +200,6 @@ int main(int argc, char **argv) { std::cout << "Let's try EXPECT_EQ(S, actual_value)" << std::endl; - ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); From c34b2015a333d4af9391e18b48ed3df381018241 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Thu, 15 Feb 2024 23:03:09 -0800 Subject: [PATCH 05/23] GTest: exe at build/svFSI-build/Source/svFSI, source at tests/unitTests --- CMakeLists.txt | 2 + Code/CMakeLists.txt | 9 - Code/Source/svFSI/CMakeLists.txt | 299 +++++++++++++------------------ tests/unitTests/CMakeLists.txt | 25 --- tests/unitTests/load.dat | 3 - tests/unitTests/test.cpp | 35 +--- tests/unitTests/unitTest.xml | 20 +-- 7 files changed, 138 insertions(+), 255 deletions(-) delete mode 100644 tests/unitTests/CMakeLists.txt delete mode 100755 tests/unitTests/load.dat diff --git a/CMakeLists.txt b/CMakeLists.txt index 26770c36..b03a56a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,7 @@ set(SV_USE_TRILINOS OFF CACHE BOOL "Use Trilinos Library with svFSI") 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 ON CACHE BOOL "Enable Unit Test by Google Test") #----------------------------------------------------------------------------- # RPATH handling @@ -143,6 +144,7 @@ ExternalProject_Add(svFSI -DSV_EXTERNALS_TOPLEVEL_DIR:PATH=${SV_EXTERNALS_TOPLEVEL_DIR} -DSV_USE_TRILINOS:BOOL=${SV_USE_TRILINOS} -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} diff --git a/Code/CMakeLists.txt b/Code/CMakeLists.txt index 82fbd7b1..c541d1de 100644 --- a/Code/CMakeLists.txt +++ b/Code/CMakeLists.txt @@ -164,12 +164,3 @@ endif() include(SimVascularInstallLibs) add_subdirectory(Scripts) #----------------------------------------------------------------------------- - - -# Add unit test -# add_subdirectory( -# "${CMAKE_CURRENT_SOURCE_DIR}/../tests/unitTests" # Source directory, relative path from current CMakeLists.txt -# "${CMAKE_CURRENT_BINARY_DIR}/tests/unitTests" # Binary directory, where the built files will go -# ) - -# add_executable(runUnitTest ../tests/unitTests/test.cpp) \ No newline at end of file diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index 03330377..f1aa6c8f 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -71,15 +71,11 @@ endif() find_package(BLAS REQUIRED) find_package(LAPACK REQUIRED) -# Include VTK either from a local build using SV_LOCAL_VTK_PATH -# or from a default installed version. +# Including VTK produces warnings when compiling Fortran code. # -if("${SV_LOCAL_VTK_PATH}" STREQUAL "") - find_package(VTK REQUIRED ) -else() - find_package(VTK PATHS ${SV_LOCAL_VTK_PATH} NO_DEFAULT_PATH REQUIRED) -endif() - +# -iframe is valid for C/C++/ObjC/ObjC++ but not for Fortran +# +find_package(VTK REQUIRED) include(${VTK_USE_FILE}) set(lib ${SV_LIB_SVFSI_NAME}) @@ -172,6 +168,9 @@ simvascular_add_executable(${SV_SVFSI_EXE} INSTALL_COMP CoreExecutables INSTALL_DESTINATION ${SV_INSTALL_RUNTIME_DIR}) +message("##### TINY ${TINYXML_LIBRARY_NAME}") +message("##### TET ${TETGEN_LIBRARY_NAME}") + target_link_libraries(${SV_SVFSI_EXE} ${GLOBAL_LIBRARIES} ${INTELRUNTIME_LIBRARIES} @@ -215,173 +214,125 @@ if(ENABLE_COVERAGE) WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) endif() +# unit tests and Google Test +if(ENABLE_UNIT_TEST) + # 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_main + ) + gtest_discover_tests(hello_test) + + + # add test.cpp for unit test + # replace main.cpp + set(CSRCS_unitTest + ../../../tests/unitTests/test.cpp + + Array3.h Array3.cpp + Array.h Array.cpp + Tensor4.h Tensor4.cpp + Vector.h Vector.cpp + + lapack_defs.h + + DebugMsg.h + Parameters.h Parameters.cpp + Simulation.h Simulation.cpp + SimulationLogger.h + VtkData.h VtkData.cpp + + all_fun.h all_fun.cpp + baf_ini.h baf_ini.cpp + bf.h bf.cpp + cep.h cep.cpp + cep_ion.h cep_ion.cpp + cmm.h cmm.cpp + consts.h consts.cpp + contact.h contact.cpp + distribute.h distribute.cpp + eq_assem.h eq_assem.cpp + fluid.h fluid.cpp + fsi.h fsi.cpp + fs.h fs.cpp + fft.h fft.cpp + heatf.h heatf.cpp + heats.h heats.cpp + initialize.h initialize.cpp + l_elas.h l_elas.cpp + lhsa.h lhsa.cpp + ls.h ls.cpp + + mat_fun.h mat_fun.cpp + mat_fun_carray.h mat_fun_carray.cpp + mat_models.h mat_models.cpp + mesh.h mesh.cpp + nn.h nn.cpp + output.h output.cpp + load_msh.h load_msh.cpp + pic.h pic.cpp + post.h post.cpp + read_files.h read_files.cpp + read_msh.h read_msh.cpp + remesh.h remesh.cpp + remeshTet.cpp + set_bc.h set_bc.cpp + shells.h shells.cpp + stokes.h stokes.cpp + sv_struct.h sv_struct.cpp + txt.h txt.cpp + utils.h utils.cpp + ustruct.h ustruct.cpp + vtk_xml.h vtk_xml.cpp + vtk_xml_parser.h vtk_xml_parser.cpp + + CepMod.h CepMod.cpp + CepModAp.h CepModAp.cpp + CepModBo.h CepModBo.cpp + CepModFn.h CepModFn.cpp + CepModTtp.h CepModTtp.cpp + ChnlMod.h ChnlMod.cpp + CmMod.h CmMod.cpp + ComMod.h ComMod.cpp + Timer.h + + SPLIT.c + ) + + # include source files (same as what svFSI does except for main.cpp) + add_executable(runUnitTest ${CSRCS_unitTest}) + + # libraries + target_link_libraries(runUnitTest + ${METIS_SVFSI_LIBRARY_NAME} + ${PARMETIS_SVFSI_LIBRARY_NAME} + ${TETGEN_LIBRARY_NAME} + ${TINYXML_LIBRARY_NAME} + ${SV_LIB_SVFSILS_NAME}${SV_MPI_NAME_EXT} + ${VTK_LIBRARIES} + ) -# add Google Test: - -include(FetchContent) -FetchContent_Declare( - googletest - URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip -) -# For Windows: Prevent overriding the parent project's compiler/linker settings -# set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) - -FetchContent_MakeAvailable(googletest) - -enable_testing() - -add_executable( - hello_test - ../../../tests/unitTests/hello_test.cpp -) -target_link_libraries( - hello_test - GTest::gtest_main -) - -set_target_properties( - hello_test - PROPERTIES - RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../../../tests/unitTests/build" -) - -include(GoogleTest) -gtest_discover_tests(hello_test) - - - - - - - - - - - - -# add test.cpp: - -# replace main.cpp -set(CSRCS_unitTest -../../../tests/unitTests/test.cpp - - Array3.h Array3.cpp - Array.h Array.cpp - Tensor4.h Tensor4.cpp - Vector.h Vector.cpp - - lapack_defs.h - - DebugMsg.h - Parameters.h Parameters.cpp - Simulation.h Simulation.cpp - SimulationLogger.h - VtkData.h VtkData.cpp - - all_fun.h all_fun.cpp - baf_ini.h baf_ini.cpp - bf.h bf.cpp - cep.h cep.cpp - cep_ion.h cep_ion.cpp - cmm.h cmm.cpp - consts.h consts.cpp - contact.h contact.cpp - distribute.h distribute.cpp - eq_assem.h eq_assem.cpp - fluid.h fluid.cpp - fsi.h fsi.cpp - fs.h fs.cpp - fft.h fft.cpp - heatf.h heatf.cpp - heats.h heats.cpp - initialize.h initialize.cpp - l_elas.h l_elas.cpp - lhsa.h lhsa.cpp - ls.h ls.cpp - - mat_fun.h mat_fun.cpp - mat_fun_carray.h mat_fun_carray.cpp - mat_models.h mat_models.cpp - mesh.h mesh.cpp - nn.h nn.cpp - output.h output.cpp - load_msh.h load_msh.cpp - pic.h pic.cpp - post.h post.cpp - read_files.h read_files.cpp - read_msh.h read_msh.cpp - remesh.h remesh.cpp - remeshTet.cpp - set_bc.h set_bc.cpp - shells.h shells.cpp - stokes.h stokes.cpp - sv_struct.h sv_struct.cpp - txt.h txt.cpp - utils.h utils.cpp - ustruct.h ustruct.cpp - vtk_xml.h vtk_xml.cpp - vtk_xml_parser.h vtk_xml_parser.cpp - - CepMod.h CepMod.cpp - CepModAp.h CepModAp.cpp - CepModBo.h CepModBo.cpp - CepModFn.h CepModFn.cpp - CepModTtp.h CepModTtp.cpp - ChnlMod.h ChnlMod.cpp - CmMod.h CmMod.cpp - ComMod.h ComMod.cpp - Timer.h - - SPLIT.c -) - -set(UNIT_TEST_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../tests/unitTests/build") -message(UNIT_TEST_DIR = "${UNIT_TEST_DIR}") - -# seems only the first line affects, IDN what other lines for...... -# simvascular_add_executable(runUnitTest -# SRCS ${CSRCS_unitTest} -# # DEV_SCRIPT_NAME "hhhhhmysvfsi" -# # INSTALL_SCRIPT_NAME "hhhhhcsvfsi" -# # INSTALL_COMP CoreExecutables -# # INSTALL_DESTINATION ${SV_INSTALL_RUNTIME_DIR} -# # INSTALL_DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}/../../../tests/unitTests/build" -# # INSTALL_DESTINATION ${UNIT_TEST_DIR} -# ) - - -# the following line works to build in svFSI-build/bin/runUnitTest -add_executable(runUnitTest ${CSRCS_unitTest}) - - - -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_main + ) + gtest_discover_tests(runUnitTest) -# PROBLEM: the following operations doesn't work for svFSIplus to read mesh file!!!! -# SOLUTION: change all path in .xml file to absolute path -# for example: /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/and go on... -set_target_properties( - runUnitTest - PROPERTIES - RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../../../tests/unitTests/build" -) -# PROBLEM: the following linking to GTest will cause failure to read mesh file!!!!! -target_link_libraries( - runUnitTest - GTest::gtest_main -) +endif() -gtest_discover_tests(runUnitTest) diff --git a/tests/unitTests/CMakeLists.txt b/tests/unitTests/CMakeLists.txt deleted file mode 100644 index 0f6faf22..00000000 --- a/tests/unitTests/CMakeLists.txt +++ /dev/null @@ -1,25 +0,0 @@ -cmake_minimum_required(VERSION 3.10) -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_EXTENSIONS OFF) - -# Set the project name -project(UnitTests) - -# Set the relative path to svFSIplus include directory -set(SVFSI_INCLUDE_DIR ../../Code/Source/svFSI) -# set(SVFSI_THIRDPARTY ../../Code/ThirdParty/msmpi/Include) - -# Include the directory -include_directories(${SVFSI_INCLUDE_DIR}) -# include_directories(${SVFSI_THIRDPARTY}) - -# Define test executable -add_executable(runUnitTests test.cpp) - -# Link against Google Test and svFSIplus libraries -# target_link_libraries(runUnitTests gtest gtest_main) - -# Add the test so it can be run with `make test` -# add_test(NAME that_test COMMAND runUnitTests) - diff --git a/tests/unitTests/load.dat b/tests/unitTests/load.dat deleted file mode 100755 index 01ab643c..00000000 --- a/tests/unitTests/load.dat +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:4e1e526c76451ab4f63d75950722b8e808791a83bbaa706dd1002816d8e202ac -size 62 diff --git a/tests/unitTests/test.cpp b/tests/unitTests/test.cpp index 2800ba3b..f17936ca 100644 --- a/tests/unitTests/test.cpp +++ b/tests/unitTests/test.cpp @@ -13,25 +13,9 @@ #include "mat_models_carray.h" #include "Simulation.h" #include "sv_struct.h" - -// from main: -#include "all_fun.h" -#include "bf.h" -#include "contact.h" #include "distribute.h" -#include "eq_assem.h" -#include "fs.h" #include "initialize.h" -#include "ls.h" -#include "output.h" -#include "pic.h" #include "read_files.h" -#include "read_msh.h" -#include "remesh.h" -#include "set_bc.h" -#include "txt.h" -#include "ustruct.h" -#include "vtk_xml.h" // include libraries @@ -64,18 +48,6 @@ void read_files(Simulation* simulation, const std::string& file_name) } read_files_ns::read_files(simulation, file_name); - -/* - try { - read_files_ns::read_files(simulation, file_name); - - } catch (const std::exception& exception) { - std::cout << "[svFSIplus] ERROR The svFSIplus program has failed." << std::endl; - std::cout << "[svFSIplus] ERROR " << exception.what() << std::endl; - exit(1); - } -*/ - } TEST(Dummy, OneEqualsToOne) { @@ -107,9 +79,10 @@ int main(int argc, char **argv) { simulation = new Simulation(); // filename for the unit element .xml: (To be complete !!) - // current_path: /path_to_svFSIplus/svFSIplus/build/svFSI-build/Source/svFSI - std::string file_name = std::filesystem::current_path().string() + "/../../../../tests/unitTests/unitTest.xml"; - // std::string file_name = "/Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/unitTest.xml"; + // current_path at compiling: /path_to_svFSIplus/svFSIplus/build/svFSI-build/Source/svFSI + // current_path at real run time: /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/build/ + // std::string file_name = std::filesystem::current_path().string() + "/../../../../tests/unitTests/unitTest.xml"; + std::string file_name = std::filesystem::current_path().string() + "/../../../../tests/unitTests/unitTest.xml"; // std::string file_name(argv[1]); diff --git a/tests/unitTests/unitTest.xml b/tests/unitTests/unitTest.xml index 0ae7fb5b..afb008a2 100644 --- a/tests/unitTests/unitTest.xml +++ b/tests/unitTests/unitTest.xml @@ -26,30 +26,30 @@ - /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/mesh/unitCube/unit_cube_volume.vtu + ../../../../tests/unitTests/mesh/unitCube/unit_cube_volume.vtu - /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/mesh/unitCube/mesh_surfaces/Z0.vtp + ../../../../tests/unitTests/mesh/unitCube/mesh_surfaces/Z0.vtp - /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/mesh/unitCube/mesh_surfaces/Z1.vtp + ../../../../tests/unitTests/mesh/unitCube/mesh_surfaces/Z1.vtp - /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/mesh/unitCube/mesh_surfaces/Y0.vtp + ../../../../tests/unitTests/mesh/unitCube/mesh_surfaces/Y0.vtp - /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/mesh/unitCube/mesh_surfaces/Y1.vtp + ../../../../tests/unitTests/mesh/unitCube/mesh_surfaces/Y1.vtp - /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/mesh/unitCube/mesh_surfaces/X0.vtp + ../../../../tests/unitTests/mesh/unitCube/mesh_surfaces/X0.vtp - /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/mesh/unitCube/mesh_surfaces/X1.vtp + ../../../../tests/unitTests/mesh/unitCube/mesh_surfaces/X1.vtp 0.001 @@ -107,12 +107,6 @@ (1, 0, 0) - - Neu - Unsteady - /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/load.dat - - From dfe18c353bc5e62a457dc5822a16cc16b2e524c3 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Wed, 21 Feb 2024 15:34:07 -0800 Subject: [PATCH 06/23] cleaned CMakeLists and test.cpp --- Code/Source/svFSI/CMakeLists.txt | 80 +-------------- rebuild.sh | 12 --- tests/unitTests/test.cpp | 171 +++++++++++++------------------ 3 files changed, 79 insertions(+), 184 deletions(-) delete mode 100755 rebuild.sh diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index f1aa6c8f..0bba3543 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -239,82 +239,13 @@ if(ENABLE_UNIT_TEST) # add test.cpp for unit test - # replace main.cpp - set(CSRCS_unitTest - ../../../tests/unitTests/test.cpp - - Array3.h Array3.cpp - Array.h Array.cpp - Tensor4.h Tensor4.cpp - Vector.h Vector.cpp - - lapack_defs.h - - DebugMsg.h - Parameters.h Parameters.cpp - Simulation.h Simulation.cpp - SimulationLogger.h - VtkData.h VtkData.cpp - - all_fun.h all_fun.cpp - baf_ini.h baf_ini.cpp - bf.h bf.cpp - cep.h cep.cpp - cep_ion.h cep_ion.cpp - cmm.h cmm.cpp - consts.h consts.cpp - contact.h contact.cpp - distribute.h distribute.cpp - eq_assem.h eq_assem.cpp - fluid.h fluid.cpp - fsi.h fsi.cpp - fs.h fs.cpp - fft.h fft.cpp - heatf.h heatf.cpp - heats.h heats.cpp - initialize.h initialize.cpp - l_elas.h l_elas.cpp - lhsa.h lhsa.cpp - ls.h ls.cpp - - mat_fun.h mat_fun.cpp - mat_fun_carray.h mat_fun_carray.cpp - mat_models.h mat_models.cpp - mesh.h mesh.cpp - nn.h nn.cpp - output.h output.cpp - load_msh.h load_msh.cpp - pic.h pic.cpp - post.h post.cpp - read_files.h read_files.cpp - read_msh.h read_msh.cpp - remesh.h remesh.cpp - remeshTet.cpp - set_bc.h set_bc.cpp - shells.h shells.cpp - stokes.h stokes.cpp - sv_struct.h sv_struct.cpp - txt.h txt.cpp - utils.h utils.cpp - ustruct.h ustruct.cpp - vtk_xml.h vtk_xml.cpp - vtk_xml_parser.h vtk_xml_parser.cpp - - CepMod.h CepMod.cpp - CepModAp.h CepModAp.cpp - CepModBo.h CepModBo.cpp - CepModFn.h CepModFn.cpp - CepModTtp.h CepModTtp.cpp - ChnlMod.h ChnlMod.cpp - CmMod.h CmMod.cpp - ComMod.h ComMod.cpp - Timer.h - - SPLIT.c - ) + + # 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_unitTest}) + add_executable(runUnitTest ${CSRCS}) # libraries target_link_libraries(runUnitTest @@ -333,6 +264,5 @@ if(ENABLE_UNIT_TEST) ) gtest_discover_tests(runUnitTest) - endif() diff --git a/rebuild.sh b/rebuild.sh deleted file mode 100755 index 0ed949b5..00000000 --- a/rebuild.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -rm -rf build - -mkdir build - -cd build - -cmake .. - -make -j10 - diff --git a/tests/unitTests/test.cpp b/tests/unitTests/test.cpp index f17936ca..2e99a112 100644 --- a/tests/unitTests/test.cpp +++ b/tests/unitTests/test.cpp @@ -1,12 +1,13 @@ /* 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 GoogleTest -#include "gtest/gtest.h" - -// include header files from svFSIplus +#include +#include +#include +#include "gtest/gtest.h" // include GoogleTest #include "mat_fun.h" #include "mat_fun_carray.h" #include "mat_models.h" @@ -18,25 +19,46 @@ #include "read_files.h" -// include libraries -#include -#include -#include +// Using global variables: +// for the output: stress tensor and tangent tensor -// Using global variables -Simulation *simulation; -ComMod com_mod; -CmMod cm_mod_; -cmType cm; -CepMod cep_mod; +Array S_0(3,3), Dm_0(6,6); // for identity test +Array S_1(3,3), Dm_1(6,6); // for non-identity test -// S, Dm: target variables -Array S(3,3), Dm(6,6); -// I: identity matrix -Array I = mat_fun::mat_id(3); -// Z: zero matrix -Array Z(3,3); +// Google Test test cases + +TEST(ExampleTest, OneEqualsToOne) { + EXPECT_EQ(1, 1); +} + +TEST(GetPK2ccTest, Identity) { + // input: + // 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); + } + } +} + +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) @@ -50,41 +72,25 @@ void read_files(Simulation* simulation, const std::string& file_name) read_files_ns::read_files(simulation, file_name); } -TEST(Dummy, OneEqualsToOne) { - EXPECT_EQ(1, 1); -} - -TEST(GetPK2ccTest, Identity) { - for (short i = 0; i < 3; i++){ - for (short j = 0; j < 3; j++){ - EXPECT_EQ(S(i,j), Z(i,j)); - } - } -} // Main function running the tests int main(int argc, char **argv) { - // std::cout << "Current working directory: " - // << std::filesystem::current_path() << std::endl; + // define deformation gradient as input for get_pk2cc + Array F_0 = mat_fun::mat_id(3); + Array F_1 = mat_fun::mat_id(3); + F_1(0,0) = 2; // Initialize MPI. // int mpi_rank, mpi_size; - // MPI_Init(&argc, &argv); MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); MPI_Comm_size(MPI_COMM_WORLD, &mpi_size); - // Original: initiate in main(): - - simulation = new Simulation(); - // filename for the unit element .xml: (To be complete !!) - // current_path at compiling: /path_to_svFSIplus/svFSIplus/build/svFSI-build/Source/svFSI - // current_path at real run time: /Users/yuechengyu/Work/Cardiac/svFSIplus_unitTest/svFSIplus/tests/unitTests/build/ - // std::string file_name = std::filesystem::current_path().string() + "/../../../../tests/unitTests/unitTest.xml"; - std::string file_name = std::filesystem::current_path().string() + "/../../../../tests/unitTests/unitTest.xml"; - // std::string file_name(argv[1]); + auto simulation = new Simulation(); + std::string file_path = "/../../../../tests/unitTests/unitTest.xml"; + std::string file_name = std::filesystem::current_path().string() + file_path; read_files(simulation, file_name); @@ -94,33 +100,21 @@ int main(int argc, char **argv) { Vector init_time(3); initialize(simulation, init_time); - // Original: define from struct_3d/struct_3d_carray(...) <- construct_dsolid(...) - - com_mod = simulation->com_mod; - cm_mod_ = simulation->cm_mod; - cm = com_mod.cm; - cep_mod = simulation->get_cep_mod(); - + // 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; - - std::cout << "The number of meshes: " << com_mod.nMsh << std::endl; - 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 - - std::cout << "The number of elements: " << e << std::endl; - int eNoN = lM.eNoN; int nFn = lM.nFn; - if (nFn == 0) { - nFn = 1; - } - std::cout << "The variable eNoN is : " << eNoN << std::endl; - std::cout << "The variable nFn is : " << nFn << std::endl; - + if (nFn == 0) {nFn = 1;} Array fN(nsd,nFn); fN = 0.0; + if (lM.fN.size() != 0) { for (int iFn = 0; iFn < nFn; iFn++) { for (int i = 0; i < nsd; i++) { @@ -133,46 +127,29 @@ int main(int argc, char **argv) { auto& eq = com_mod.eq[cEq]; int cDmn = com_mod.cDmn; auto& dmn = eq.dmn[cDmn]; - - - - // ya_g or ya: a constant related to electromechanics ?? - double ya_g = 0.0; + double ya_g = 0.0; // ya_g or ya: a constant related to electromechanics ? - - - std::cout << "Before calling get_pk2cc: input argument F" << std::endl; - std::cout << "=================== INPUT ===================" << std::endl; - // F: deformation gradient - Array F(3,3); - F = I; - // F(0, 0) = 2.0; - // const auto& stM = dmn.stM; - // std::cout << "Material type: " << stM.isoType << std::endl; - F.print("Input Deformation "); - - // Original: call in sv_struct.cpp - mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); - - std::cout <<"=================== OUTPUT ===================" << std::endl; - std::cout << "After calling get_pk2cc: output S and Dm" << std::endl; - S.print("Output stress tensor:"); - - std::cout << "After calling get_pk2cc: output S and Dm" << std::endl; - Dm.print("Output stiffness matrix:"); - + // 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) + // 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(...) - std::cout << "This is svFSIplus/tests/unitTests/test.cpp now!" << std::endl; - - std::cout << "Let's try EXPECT_EQ(S, actual_value)" << std::endl; - - + // run Google Test ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); From 6c7b5f3fc789752966ec0ebd709b33e45aa2f30e Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Wed, 21 Feb 2024 16:06:53 -0800 Subject: [PATCH 07/23] modified .github/workflow/test --- .github/workflows/test.yml | 4 ++++ Code/Source/svFSI/CMakeLists.txt | 15 ++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 02726edb..e118f430 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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: | diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index d08b9ccf..9f838f5f 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -107,11 +107,15 @@ endif() find_package(BLAS REQUIRED) find_package(LAPACK REQUIRED) -# Including VTK produces warnings when compiling Fortran code. +# Include VTK either from a local build using SV_LOCAL_VTK_PATH +# or from a default installed version. # -# -iframe is valid for C/C++/ObjC/ObjC++ but not for Fortran -# -find_package(VTK REQUIRED) +if("${SV_LOCAL_VTK_PATH}" STREQUAL "") + find_package(VTK REQUIRED ) +else() + find_package(VTK PATHS ${SV_LOCAL_VTK_PATH} NO_DEFAULT_PATH REQUIRED) +endif() + include(${VTK_USE_FILE}) set(lib ${SV_LIB_SVFSI_NAME}) @@ -209,9 +213,6 @@ simvascular_add_executable(${SV_SVFSI_EXE} INSTALL_COMP CoreExecutables INSTALL_DESTINATION ${SV_INSTALL_RUNTIME_DIR}) -message("##### TINY ${TINYXML_LIBRARY_NAME}") -message("##### TET ${TETGEN_LIBRARY_NAME}") - target_link_libraries(${SV_SVFSI_EXE} ${GLOBAL_LIBRARIES} ${INTELRUNTIME_LIBRARIES} From 279ee523d8445a1f72039008736e5b5b98c7f9f9 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Thu, 22 Feb 2024 14:41:00 -0800 Subject: [PATCH 08/23] add copyright, changed file_path in test.cpp --- .github/workflows/test.yml | 2 +- tests/unitTests/hello_test.cpp | 6 ++++++ tests/unitTests/test.cpp | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e118f430..58a1bb1f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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: | diff --git a/tests/unitTests/hello_test.cpp b/tests/unitTests/hello_test.cpp index 5f062148..a7aa9f27 100644 --- a/tests/unitTests/hello_test.cpp +++ b/tests/unitTests/hello_test.cpp @@ -1,3 +1,9 @@ +/** + * 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 // Demonstrate some basic assertions. diff --git a/tests/unitTests/test.cpp b/tests/unitTests/test.cpp index 2e99a112..f866c9c8 100644 --- a/tests/unitTests/test.cpp +++ b/tests/unitTests/test.cpp @@ -1,3 +1,33 @@ +/* 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 @@ -89,8 +119,7 @@ int main(int argc, char **argv) { auto simulation = new Simulation(); - std::string file_path = "/../../../../tests/unitTests/unitTest.xml"; - std::string file_name = std::filesystem::current_path().string() + file_path; + std::string file_name = "../../../../tests/unitTests/unitTest.xml"; read_files(simulation, file_name); From 768a920eae1dac435945719cd9beae6178b21562 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Thu, 22 Feb 2024 15:05:00 -0800 Subject: [PATCH 09/23] update workflow, add copyright --- .github/workflows/test.yml | 2 +- CMakeLists.txt | 2 +- tests/unitTests/hello_test.cpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 58a1bb1f..1855c326 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -28,7 +28,7 @@ jobs: run: | mkdir build cd build - cmake -DENABLE_COVERAGE=ON -DENABLE_ARRAY_INDEX_CHECKING=ON -DENABLE_UNIT_TEST=ON.. + cmake -DENABLE_COVERAGE=ON -DENABLE_ARRAY_INDEX_CHECKING=ON -DENABLE_UNIT_TEST=ON .. make -j2 - name: Install test dependencies run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index d54cff6d..20105156 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,7 +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 ON CACHE BOOL "Enable Unit Test by Google Test") +set(ENABLE_UNIT_TEST OFF CACHE BOOL "Enable Unit Test by Google Test") #----------------------------------------------------------------------------- # RPATH handling diff --git a/tests/unitTests/hello_test.cpp b/tests/unitTests/hello_test.cpp index a7aa9f27..929b24f9 100644 --- a/tests/unitTests/hello_test.cpp +++ b/tests/unitTests/hello_test.cpp @@ -1,3 +1,33 @@ +/* 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. + */ + /** * This function is adapted from Google Test's Quickstart Guide. * Source: Google Test Quickstart for Google Test and CMake From 10836306c046008c8e1eed5364d43c87612be19c Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Thu, 22 Feb 2024 16:42:56 -0800 Subject: [PATCH 10/23] revise cmakelist to disable gtest_discover_tests --- Code/Source/svFSI/CMakeLists.txt | 6 ++++-- tests/unitTests/test.cpp | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index 9f838f5f..ae6c3ef5 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -281,7 +281,8 @@ if(ENABLE_UNIT_TEST) hello_test GTest::gtest_main ) - gtest_discover_tests(hello_test) + # gtest_discover_tests(hello_test) + add_test(NAME GTestSample COMMAND hello_test) # add test.cpp for unit test @@ -308,7 +309,8 @@ if(ENABLE_UNIT_TEST) runUnitTest GTest::gtest_main ) - gtest_discover_tests(runUnitTest) + # gtest_discover_tests(runUnitTest) + add_test(NAME UnitTest COMMAND runUnitTest) endif() diff --git a/tests/unitTests/test.cpp b/tests/unitTests/test.cpp index f866c9c8..7cb898b3 100644 --- a/tests/unitTests/test.cpp +++ b/tests/unitTests/test.cpp @@ -120,6 +120,7 @@ int main(int argc, char **argv) { auto simulation = new Simulation(); std::string file_name = "../../../../tests/unitTests/unitTest.xml"; + // std::string file_name = ""; read_files(simulation, file_name); From beab321675c41052e49f1353184079f89cc34355 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Thu, 22 Feb 2024 17:15:32 -0800 Subject: [PATCH 11/23] link pthread for ubuntu20, link LAPACK for ubuntu22 --- Code/Source/svFSI/CMakeLists.txt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index ae6c3ef5..728b4c1e 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -280,6 +280,7 @@ if(ENABLE_UNIT_TEST) target_link_libraries( hello_test GTest::gtest_main + pthread ) # gtest_discover_tests(hello_test) add_test(NAME GTestSample COMMAND hello_test) @@ -296,6 +297,11 @@ if(ENABLE_UNIT_TEST) # 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} @@ -308,6 +314,7 @@ if(ENABLE_UNIT_TEST) target_link_libraries( runUnitTest GTest::gtest_main + pthread ) # gtest_discover_tests(runUnitTest) add_test(NAME UnitTest COMMAND runUnitTest) From ba6eeb962f327a2b33939a88798f49bc4f9c19fe Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Thu, 22 Feb 2024 20:49:16 -0800 Subject: [PATCH 12/23] try to link threads lib for ubuntu20 --- Code/Source/svFSI/CMakeLists.txt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index 728b4c1e..50f46aeb 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -262,6 +262,12 @@ endif() # unit tests and Google Test if(ENABLE_UNIT_TEST) + # Force Google Test to use pthreads (for ubuntu20) + add_definitions(-DGTEST_HAS_PTHREAD=1) + + # Find pthreads package + find_package(Threads REQUIRED) + # install Google Test include(FetchContent) FetchContent_Declare( @@ -280,7 +286,7 @@ if(ENABLE_UNIT_TEST) target_link_libraries( hello_test GTest::gtest_main - pthread + Threads::Threads # link threads library ) # gtest_discover_tests(hello_test) add_test(NAME GTestSample COMMAND hello_test) @@ -314,7 +320,7 @@ if(ENABLE_UNIT_TEST) target_link_libraries( runUnitTest GTest::gtest_main - pthread + Threads::Threads # link threads library ) # gtest_discover_tests(runUnitTest) add_test(NAME UnitTest COMMAND runUnitTest) From 9a066e607de000bc8ecc408a29604bd8cda58a73 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Thu, 22 Feb 2024 21:49:47 -0800 Subject: [PATCH 13/23] try to link pthread on ubuntu20 --- Code/Source/svFSI/CMakeLists.txt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index 50f46aeb..71637ca0 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -263,7 +263,8 @@ endif() # unit tests and Google Test if(ENABLE_UNIT_TEST) # Force Google Test to use pthreads (for ubuntu20) - add_definitions(-DGTEST_HAS_PTHREAD=1) + # add_definitions(-DGTEST_HAS_PTHREAD=1) + add_compile_definitions(GTEST_HAS_PTHREAD=1) # Find pthreads package find_package(Threads REQUIRED) @@ -283,10 +284,12 @@ if(ENABLE_UNIT_TEST) hello_test ../../../tests/unitTests/hello_test.cpp ) + target_compile_definitions(hello_test PRIVATE GTEST_HAS_PTHREAD=1) target_link_libraries( hello_test GTest::gtest_main Threads::Threads # link threads library + pthread ) # gtest_discover_tests(hello_test) add_test(NAME GTestSample COMMAND hello_test) @@ -317,11 +320,15 @@ if(ENABLE_UNIT_TEST) ) # link Google Test + target_compile_definitions(runUnitTest PRIVATE GTEST_HAS_PTHREAD=1) target_link_libraries( runUnitTest GTest::gtest_main Threads::Threads # link threads library + pthread ) + target_compile_definitions(runUnitTest PRIVATE GTEST_HAS_PTHREAD=1) + # gtest_discover_tests(runUnitTest) add_test(NAME UnitTest COMMAND runUnitTest) From d5a8347ea3973c0c6a9a55aca081ce7a0da6b9a5 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Mon, 26 Feb 2024 11:00:13 -0800 Subject: [PATCH 14/23] 2nd try to link pthread on ubuntu20 --- Code/Source/svFSI/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index 71637ca0..7cd421de 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -290,6 +290,7 @@ if(ENABLE_UNIT_TEST) GTest::gtest_main Threads::Threads # link threads library pthread + ${CMAKE_THREAD_LIBS_INIT} ) # gtest_discover_tests(hello_test) add_test(NAME GTestSample COMMAND hello_test) @@ -326,6 +327,7 @@ if(ENABLE_UNIT_TEST) GTest::gtest_main Threads::Threads # link threads library pthread + ${CMAKE_THREAD_LIBS_INIT} ) target_compile_definitions(runUnitTest PRIVATE GTEST_HAS_PTHREAD=1) From b8138d59aac63a9070377a5fc39657d416a624fa Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Mon, 26 Feb 2024 12:53:12 -0800 Subject: [PATCH 15/23] last try to link pthread on ubuntu20 --- Code/Source/svFSI/CMakeLists.txt | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index 7cd421de..3885d270 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -262,11 +262,8 @@ endif() # unit tests and Google Test if(ENABLE_UNIT_TEST) - # Force Google Test to use pthreads (for ubuntu20) - # add_definitions(-DGTEST_HAS_PTHREAD=1) - add_compile_definitions(GTEST_HAS_PTHREAD=1) - # Find pthreads package + # link pthread on ubuntu20 find_package(Threads REQUIRED) # install Google Test @@ -284,13 +281,10 @@ if(ENABLE_UNIT_TEST) hello_test ../../../tests/unitTests/hello_test.cpp ) - target_compile_definitions(hello_test PRIVATE GTEST_HAS_PTHREAD=1) target_link_libraries( hello_test GTest::gtest_main - Threads::Threads # link threads library - pthread - ${CMAKE_THREAD_LIBS_INIT} + pthread # link pthread on ubuntu20 ) # gtest_discover_tests(hello_test) add_test(NAME GTestSample COMMAND hello_test) @@ -321,13 +315,10 @@ if(ENABLE_UNIT_TEST) ) # link Google Test - target_compile_definitions(runUnitTest PRIVATE GTEST_HAS_PTHREAD=1) target_link_libraries( runUnitTest - GTest::gtest_main - Threads::Threads # link threads library - pthread - ${CMAKE_THREAD_LIBS_INIT} + GTest::gtest_main + pthread # link pthread on ubuntu20 ) target_compile_definitions(runUnitTest PRIVATE GTEST_HAS_PTHREAD=1) From dfeb900dbbc09a89d7b835d41ccd4025f83214d3 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Mon, 26 Feb 2024 13:05:03 -0800 Subject: [PATCH 16/23] last after the last try to link pthread on ubuntu20 --- Code/Source/svFSI/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index 3885d270..73ded7a6 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -283,6 +283,7 @@ if(ENABLE_UNIT_TEST) ) target_link_libraries( hello_test + gtest GTest::gtest_main pthread # link pthread on ubuntu20 ) @@ -317,6 +318,7 @@ if(ENABLE_UNIT_TEST) # link Google Test target_link_libraries( runUnitTest + gtest GTest::gtest_main pthread # link pthread on ubuntu20 ) From 57014facc81f0ea29c5fef654cd660e5c27f85e8 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Fri, 1 Mar 2024 00:07:58 -0800 Subject: [PATCH 17/23] use test fixture, same setup for multiple tests --- Code/Source/svFSI/CMakeLists.txt | 24 +- tests/unitTests/hello_test.cpp | 53 ---- .../mesh/unitCube/mesh_surfaces/X0.vtp | 3 - .../mesh/unitCube/mesh_surfaces/X1.vtp | 3 - .../mesh/unitCube/mesh_surfaces/Y0.vtp | 3 - .../mesh/unitCube/mesh_surfaces/Y1.vtp | 3 - .../mesh/unitCube/mesh_surfaces/Z0.vtp | 3 - .../mesh/unitCube/mesh_surfaces/Z1.vtp | 3 - .../mesh/unitCube/mesh_surfaces/bot_face.vtp | 3 - .../mesh/unitCube/mesh_surfaces/top_face.vtp | 3 - .../mesh/{unitCube => }/unit_cube_volume.vtu | 0 tests/unitTests/test.cpp | 247 +++++++++++------- tests/unitTests/unitTest.xml | 63 +---- 13 files changed, 161 insertions(+), 250 deletions(-) delete mode 100644 tests/unitTests/hello_test.cpp delete mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/X0.vtp delete mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/X1.vtp delete mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/Y0.vtp delete mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/Y1.vtp delete mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/Z0.vtp delete mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/Z1.vtp delete mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/bot_face.vtp delete mode 100644 tests/unitTests/mesh/unitCube/mesh_surfaces/top_face.vtp rename tests/unitTests/mesh/{unitCube => }/unit_cube_volume.vtu (100%) diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index 73ded7a6..d22f5453 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -276,21 +276,6 @@ if(ENABLE_UNIT_TEST) 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) - - # add test.cpp for unit test # remove the main.cpp and add test.cpp @@ -298,10 +283,10 @@ if(ENABLE_UNIT_TEST) list(REMOVE_ITEM CSRCS "main.cpp") # include source files (same as what svFSI does except for main.cpp) - add_executable(runUnitTest ${CSRCS}) + add_executable(run_all_unit_tests ${CSRCS}) # libraries - target_link_libraries(runUnitTest + target_link_libraries(run_all_unit_tests ${GLOBAL_LIBRARIES} ${INTELRUNTIME_LIBRARIES} ${ZLIB_LIBRARY} @@ -317,15 +302,14 @@ if(ENABLE_UNIT_TEST) # link Google Test target_link_libraries( - runUnitTest + run_all_unit_tests gtest GTest::gtest_main pthread # link pthread on ubuntu20 ) - target_compile_definitions(runUnitTest PRIVATE GTEST_HAS_PTHREAD=1) # gtest_discover_tests(runUnitTest) - add_test(NAME UnitTest COMMAND runUnitTest) + add_test(NAME UnitTest COMMAND run_all_unit_tests) endif() diff --git a/tests/unitTests/hello_test.cpp b/tests/unitTests/hello_test.cpp deleted file mode 100644 index 929b24f9..00000000 --- a/tests/unitTests/hello_test.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/* 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. - */ - -/** - * 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 - -// 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(); - -} diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/X0.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/X0.vtp deleted file mode 100644 index 8ab28a46..00000000 --- a/tests/unitTests/mesh/unitCube/mesh_surfaces/X0.vtp +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:33ebdfa2169105655dc4fbc0486eb6accee067487a986d6e0d288a54f11a3459 -size 6001 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/X1.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/X1.vtp deleted file mode 100644 index b51534a8..00000000 --- a/tests/unitTests/mesh/unitCube/mesh_surfaces/X1.vtp +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:fdb6efe7dd00b3fa098c9292c41decb568a99997255d847b2b4f257281555978 -size 6005 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/Y0.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/Y0.vtp deleted file mode 100644 index 0c330423..00000000 --- a/tests/unitTests/mesh/unitCube/mesh_surfaces/Y0.vtp +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:0269e2a9d7772bd0b078af4e320e020ab39aaaec0eff014b7220abdd14e1a3a5 -size 5997 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/Y1.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/Y1.vtp deleted file mode 100644 index 0ae4e7e3..00000000 --- a/tests/unitTests/mesh/unitCube/mesh_surfaces/Y1.vtp +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:8553d38c10d49b06653615c7e8ecbaa5bc999fa94c67b98fdc791ab7ee75737a -size 6005 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/Z0.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/Z0.vtp deleted file mode 100644 index da1aef5e..00000000 --- a/tests/unitTests/mesh/unitCube/mesh_surfaces/Z0.vtp +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:8a109c4f205f4bdf4517a8f9fd21214732163996e512591efbbd90840d51830f -size 6001 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/Z1.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/Z1.vtp deleted file mode 100644 index 55dc9986..00000000 --- a/tests/unitTests/mesh/unitCube/mesh_surfaces/Z1.vtp +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:ff245d63f5f21b6fdcc388b5aecea18865b32833b0c4a014841c69d8fbd3bba8 -size 6001 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/bot_face.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/bot_face.vtp deleted file mode 100644 index 1c2c6b9d..00000000 --- a/tests/unitTests/mesh/unitCube/mesh_surfaces/bot_face.vtp +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:35a04c93417bc8edc792dabe290d2fdc2cd4c9d22fc008feb1bec035623aae96 -size 2109 diff --git a/tests/unitTests/mesh/unitCube/mesh_surfaces/top_face.vtp b/tests/unitTests/mesh/unitCube/mesh_surfaces/top_face.vtp deleted file mode 100644 index 1c2c6b9d..00000000 --- a/tests/unitTests/mesh/unitCube/mesh_surfaces/top_face.vtp +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:35a04c93417bc8edc792dabe290d2fdc2cd4c9d22fc008feb1bec035623aae96 -size 2109 diff --git a/tests/unitTests/mesh/unitCube/unit_cube_volume.vtu b/tests/unitTests/mesh/unit_cube_volume.vtu similarity index 100% rename from tests/unitTests/mesh/unitCube/unit_cube_volume.vtu rename to tests/unitTests/mesh/unit_cube_volume.vtu diff --git a/tests/unitTests/test.cpp b/tests/unitTests/test.cpp index 7cb898b3..6d06b546 100644 --- a/tests/unitTests/test.cpp +++ b/tests/unitTests/test.cpp @@ -49,46 +49,8 @@ #include "read_files.h" -// Using global variables: -// for the output: stress tensor and tangent tensor - -Array S_0(3,3), Dm_0(6,6); // for identity test -Array S_1(3,3), Dm_1(6,6); // for non-identity test - - -// Google Test test cases - -TEST(ExampleTest, OneEqualsToOne) { - EXPECT_EQ(1, 1); -} - -TEST(GetPK2ccTest, Identity) { - // input: - // 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); - } - } -} - -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); - } - } - } -} +int argc; +char **argv; void read_files(Simulation* simulation, const std::string& file_name) @@ -103,84 +65,187 @@ void read_files(Simulation* simulation, const std::string& file_name) } -// Main function running the tests -int main(int argc, char **argv) { - // define deformation gradient as input for get_pk2cc - Array F_0 = mat_fun::mat_id(3); - Array F_1 = mat_fun::mat_id(3); - F_1(0,0) = 2; +class UnitTest_getpk2cc : public ::testing::Test { +protected: + // parameters for simulation setup + Simulation *simulation; + ComMod com_mod; + CepMod cep_mod; + dmnType dmn; + int nFn; + double ya_g; + int nsd; + static void SetUpTestSuite() { // 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(); + static void TearDownTestSuite() { + // Finalize MPI once after all tests are done + MPI_Finalize(); + } - std::string file_name = "../../../../tests/unitTests/unitTest.xml"; - // std::string file_name = ""; + void setUpSimulation (std::string file_name) { + // this function can be used if the user has designed input file + } + void SetUp() override { + + std::string file_name = "../../../../tests/unitTests/unitTest.xml"; + simulation = new Simulation(); + read_files(simulation, file_name); - std::cout << "Finish reading files" << std::endl; - distribute(simulation); Vector init_time(3); initialize(simulation, init_time); // preparing input arguments for get_pk2cc - auto com_mod = simulation->com_mod; + 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; + cep_mod = simulation->get_cep_mod(); + 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; + nFn = lM.nFn; if (nFn == 0) {nFn = 1;} - Array fN(nsd,nFn); - fN = 0.0; - - if (lM.fN.size() != 0) { - for (int iFn = 0; iFn < nFn; iFn++) { - for (int i = 0; i < nsd; i++) { - fN(i,iFn) = lM.fN(i+nsd*iFn,e); - } - } - } + + // for potential anisotropic material: + // Array fN(nsd,nFn); + // fN = 0.0; + // if (lM.fN.size() != 0) { + // for (int iFn = 0; iFn < nFn; iFn++) { + // for (int i = 0; i < nsd; i++) { + // fN(i,iFn) = lM.fN(i+nsd*iFn,e); + // } + // } + // } 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(...) - - // run Google Test - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); + dmn = eq.dmn[cDmn]; + ya_g = 0.0; // ya_g or ya: a constant related to electromechanics ? + } + +}; + + +TEST_F(UnitTest_getpk2cc, NeoHookean) { + // Step 1: define material properties + // set stM.isoType as NeoHookean + auto& stM = dmn.stM; + stM.isoType = consts::ConstitutiveModelType::stIso_nHook; + // set material parameters + double E = 1e6; // Elasticity_modulus + double nu = 0.5; // Poisson_ratio + double mu = 0.5 * E / (1.0 + nu); // Shear_modulus + stM.C10 = 0.5 * mu; // set_material_props.h + stM.volType = consts::ConstitutiveModelType::stVol_ST91; // Dilational_penalty_model + stM.Kpen = 4e9; // Penalty_parameter + + // Step 2: define fiber orientation (anisotropic only) + Array fN(nsd,nFn); + fN = 0.0; + + // Step 3: define the input + Array F = mat_fun::mat_id(3); + // Step 4: define the reference output + Array S_ref(3,3), Dm_ref(6,6); + S_ref = 0.0; Dm_ref = 0.0; + // Step 5: compare + Array S(3,3), Dm(6,6); + mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); + for (int i = 0; i < 3; i++){ + for (int j = 0; j < 3; j++){ + EXPECT_EQ(S(i,j), S_ref(i,j)); + } + } + + // more tests ... + F = mat_fun::mat_id(3); + F(0,0) = 2.0; + mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); + for (int i = 0; i < 3; i++){ + for (int j = 0; j < 3; j++){ + if (i == j) { + EXPECT_NE(S(i,j), S_ref(i,j)); + } else { + EXPECT_EQ(S(i,j), S_ref(i,j)); + } + } + } } + + +TEST_F(UnitTest_getpk2cc, NeoHookean_2) { + // Step 1: define material properties + // set stM.isoType as NeoHookean + auto& stM = dmn.stM; + stM.isoType = consts::ConstitutiveModelType::stIso_nHook; + // set material parameters + double E = 2e6; // Elasticity_modulus + double nu = 0.5; // Poisson_ratio + double mu = 0.5 * E / (1.0 + nu); // Shear_modulus + stM.C10 = 0.5 * mu; // set_material_props.h + stM.volType = consts::ConstitutiveModelType::stVol_ST91; // Dilational_penalty_model + stM.Kpen = 4e9; // Penalty_parameter + + // Step 2: define fiber orientation (anisotropic only) + Array fN(nsd,nFn); + fN = 0.0; + + // Step 3: define the input + Array F = mat_fun::mat_id(3); + // Step 4: define the reference output + Array S_ref(3,3), Dm_ref(6,6); + S_ref = 0.0; Dm_ref = 0.0; + // Step 5: compare + Array S(3,3), Dm(6,6); + mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); + for (int i = 0; i < 3; i++){ + for (int j = 0; j < 3; j++){ + EXPECT_EQ(S(i,j), S_ref(i,j)); + } + } + + // more tests ... + F = mat_fun::mat_id(3); + F(0,0) = 2.0; + mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); + for (int i = 0; i < 3; i++){ + for (int j = 0; j < 3; j++){ + if (i == j) { + EXPECT_NE(S(i,j), S_ref(i,j)); + } else { + EXPECT_EQ(S(i,j), S_ref(i,j)); + } + } + } +} + + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} + +/* ++++++++++++++++++++++++ why do we need an input file for unit test? ++++++++++++++++++++++++++ + 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(...) +*/ \ No newline at end of file diff --git a/tests/unitTests/unitTest.xml b/tests/unitTests/unitTest.xml index afb008a2..bb2645b9 100644 --- a/tests/unitTests/unitTest.xml +++ b/tests/unitTests/unitTest.xml @@ -26,33 +26,7 @@ - ../../../../tests/unitTests/mesh/unitCube/unit_cube_volume.vtu - - - ../../../../tests/unitTests/mesh/unitCube/mesh_surfaces/Z0.vtp - - - - ../../../../tests/unitTests/mesh/unitCube/mesh_surfaces/Z1.vtp - - - - ../../../../tests/unitTests/mesh/unitCube/mesh_surfaces/Y0.vtp - - - - ../../../../tests/unitTests/mesh/unitCube/mesh_surfaces/Y1.vtp - - - - ../../../../tests/unitTests/mesh/unitCube/mesh_surfaces/X0.vtp - - - - ../../../../tests/unitTests/mesh/unitCube/mesh_surfaces/X1.vtp - - - 0.001 + ../../../../tests/unitTests/mesh/unit_cube_volume.vtu @@ -72,41 +46,6 @@ ST91 4.0E9 - - true - true - true - true - true - true - true - true - - - - FSILS - 1e-6 - 1000 - - - - Dir - 0.0 - (0, 0, 1) - - - - Dir - 0.0 - (0, 1, 0) - - - - Dir - 0.0 - (1, 0, 0) - - From c986d3f47952a75b97cb058082cb0334b0afccb1 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Tue, 5 Mar 2024 14:30:17 -0800 Subject: [PATCH 18/23] At the mid of nowhere for GMock, stuck in ten_dyad_prod() --- Code/Source/svFSI/mat_models.cpp | 27 ++ tests/unitTests/test.cpp | 458 +++++++++++++++++++++---------- 2 files changed, 335 insertions(+), 150 deletions(-) diff --git a/Code/Source/svFSI/mat_models.cpp b/Code/Source/svFSI/mat_models.cpp index 6f28fa60..35460000 100644 --- a/Code/Source/svFSI/mat_models.cpp +++ b/Code/Source/svFSI/mat_models.cpp @@ -214,6 +214,8 @@ void get_pk2cc(const ComMod& com_mod, const CepMod& cep_mod, const dmnType& lDmn // Tensor4 CC(nsd,nsd,nsd,nsd); + std::cout << "test brreak point *1" << std::endl; + switch (stM.isoType) { case ConstitutiveModelType::stIso_lin: { double g1 = stM.C10; // mu @@ -242,12 +244,16 @@ void get_pk2cc(const ComMod& com_mod, const CepMod& cep_mod, const dmnType& lDmn // NeoHookean model case ConstitutiveModelType::stIso_nHook: { + std::cout << "test brreak point *2" << std::endl; + double g1 = 2.0 * stM.C10; auto Sb = g1*Idm; // Fiber reinforcement/active stress Sb += Tfa * mat_dyad_prod(fl.col(0), fl.col(0), nsd); + std::cout << "test brreak point *3" << std::endl; + double r1 = g1 * Inv1 / nd; for (int j = 0; j < S.ncols(); j++) { for (int i = 0; i < S.nrows(); i++) { @@ -255,10 +261,31 @@ void get_pk2cc(const ComMod& com_mod, const CepMod& cep_mod, const dmnType& lDmn } } + std::cout << "test brreak point *4" << std::endl; + std::cout << "nd, nsd: " << nd << ", "<< nsd << std::endl; + S.print("S: "); + Ci.print("Ci: "); + + + Array A(3, 3); + Array B = mat_fun::mat_id(3); + A.print("this is A: "); + B.print("this is B: "); + Tensor4 ret = ten_dyad_prod(A, B, 3); + std::cout << "test brreak point *4" << std::endl; + + + ten_dyad_prod(B, A, 3); + std::cout << "test brreak point *4" << std::endl; + CC = (-2.0/nd) * ( ten_dyad_prod(Ci, S, nsd) + ten_dyad_prod(S, Ci, nsd)); + std::cout << "test brreak point *5" << std::endl; S += p*J*Ci; + std::cout << "test brreak point *6" << std::endl; CC += 2.0*(r1 - p*J) * ten_symm_prod(Ci, Ci, nsd) + (pl*J - 2.0*r1/nd) * ten_dyad_prod(Ci, Ci, nsd); + std::cout << "test brreak point *7" << std::endl; + } break; // Mooney-Rivlin model diff --git a/tests/unitTests/test.cpp b/tests/unitTests/test.cpp index 6d06b546..5e8c19b1 100644 --- a/tests/unitTests/test.cpp +++ b/tests/unitTests/test.cpp @@ -52,70 +52,83 @@ int argc; char **argv; +class MockSimulation : public Simulation { +public: + MockSimulation() { + // initialize if needed + } + // Mock methods if needed +}; +class MockComMod : public ComMod { +public: + MockComMod() { + // initialize if needed + } + // Mock methods if needed +}; +class MockCepMod : public CepMod { +public: + MockCepMod() { + // initialize if needed + } + // Mock methods if needed +}; +class MockcmType : public cmType { +public: + MockcmType() { + // initialize if needed + } + // Mock methods if needed +}; +class MockstModelType : public stModelType { +public: + MockstModelType() { + // initialize if needed + } + // Mock methods if needed +}; +class MockdmnType : public dmnType { +public: + MockdmnType() { + // initialize if needed + } + // Mock methods if needed +}; +class MockcemModelType : public cemModelType { +public: + MockcemModelType() { + // initialize if needed + } + // Mock methods if needed +}; -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); -} - - -class UnitTest_getpk2cc : public ::testing::Test { -protected: - // parameters for simulation setup - Simulation *simulation; - ComMod com_mod; - CepMod cep_mod; - dmnType dmn; - int nFn; - double ya_g; - int nsd; - - static void SetUpTestSuite() { - // 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); - } - - static void TearDownTestSuite() { - // Finalize MPI once after all tests are done - MPI_Finalize(); - } +TEST(TestMockObject, sampleTest) { + MockSimulation *simulation; - void setUpSimulation (std::string file_name) { - // this function can be used if the user has designed input file - } - - void SetUp() override { - - std::string file_name = "../../../../tests/unitTests/unitTest.xml"; - simulation = new Simulation(); - - read_files(simulation, file_name); - - distribute(simulation); - Vector init_time(3); - initialize(simulation, init_time); + MockComMod com_mod; + MockCepMod cep_mod; + MockcmType cm; + MockstModelType stM; + MockdmnType dmn; + MockcemModelType cem; // preparing input arguments for get_pk2cc - com_mod = simulation->com_mod; - auto cm_mod_ = simulation->cm_mod; - auto cm = com_mod.cm; - cep_mod = simulation->get_cep_mod(); - 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; - nFn = lM.nFn; + // auto com_mod = simulation->com_mod; + + std::cout << "test brreak point 1" << std::endl; + + // auto cm_mod_ = simulation->cm_mod; + // auto cm = com_mod.cm; + // auto cep_mod = simulation->get_cep_mod(); + com_mod.nsd = 3; // setup by hand + int nsd = com_mod.nsd; + dmn.stM = stM; + int iM = 1; // 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; + int nFn = 0; if (nFn == 0) {nFn = 1;} // for potential anisotropic material: @@ -129,111 +142,256 @@ class UnitTest_getpk2cc : public ::testing::Test { // } // } - int cEq = com_mod.cEq; - auto& eq = com_mod.eq[cEq]; - int cDmn = com_mod.cDmn; - dmn = eq.dmn[cDmn]; - ya_g = 0.0; // ya_g or ya: a constant related to electromechanics ? - } + std::cout << "test brreak point 2" << std::endl; -}; + // 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 ? + std::cout << "test brreak point 3" << std::endl; -TEST_F(UnitTest_getpk2cc, NeoHookean) { - // Step 1: define material properties - // set stM.isoType as NeoHookean - auto& stM = dmn.stM; - stM.isoType = consts::ConstitutiveModelType::stIso_nHook; - // set material parameters - double E = 1e6; // Elasticity_modulus - double nu = 0.5; // Poisson_ratio - double mu = 0.5 * E / (1.0 + nu); // Shear_modulus - stM.C10 = 0.5 * mu; // set_material_props.h - stM.volType = consts::ConstitutiveModelType::stVol_ST91; // Dilational_penalty_model - stM.Kpen = 4e9; // Penalty_parameter - - // Step 2: define fiber orientation (anisotropic only) - Array fN(nsd,nFn); - fN = 0.0; - - // Step 3: define the input - Array F = mat_fun::mat_id(3); - // Step 4: define the reference output - Array S_ref(3,3), Dm_ref(6,6); - S_ref = 0.0; Dm_ref = 0.0; - // Step 5: compare - Array S(3,3), Dm(6,6); - mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); - for (int i = 0; i < 3; i++){ - for (int j = 0; j < 3; j++){ - EXPECT_EQ(S(i,j), S_ref(i,j)); - } - } - // more tests ... - F = mat_fun::mat_id(3); - F(0,0) = 2.0; - mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); - for (int i = 0; i < 3; i++){ + // Step 1: define material properties + // set stM.isoType as NeoHookean + // auto& stM = dmn.stM; + dmn.stM.isoType = consts::ConstitutiveModelType::stIso_nHook; + // set material parameters + double E = 1e6; // Elasticity_modulus + double nu = 0.5; // Poisson_ratio + double mu = 0.5 * E / (1.0 + nu); // Shear_modulus + dmn.stM.C10 = 0.5 * mu; // set_material_props.h + dmn.stM.volType = consts::ConstitutiveModelType::stVol_ST91; // Dilational_penalty_model + dmn.stM.Kpen = 4e9; // Penalty_parameter + + std::cout << "test brreak point 4" << std::endl; + + // Step 2: define fiber orientation (anisotropic only) + Array fN(nsd,nFn); + fN = 0.0; + + // Step 3: define the input + Array F = mat_fun::mat_id(3); + // Step 4: define the reference output + Array S_ref(3,3), Dm_ref(6,6); + S_ref = 0.0; Dm_ref = 0.0; + // Step 5: compare + Array S(3,3), Dm(6,6); + + std::cout << "test brreak point 5" << std::endl; + + cep_mod.cem = cem; + cep_mod.cem.aStress = false; + cep_mod.cem.aStrain = false; + + mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); + + std::cout << "test brreak point 6" << std::endl; + + for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ - if (i == j) { - EXPECT_NE(S(i,j), S_ref(i,j)); - } else { - EXPECT_EQ(S(i,j), S_ref(i,j)); - } + EXPECT_EQ(S(i,j), S_ref(i,j)); } - } + } + // more tests ... + F = mat_fun::mat_id(3); + F(0,0) = 2.0; + mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); + for (int i = 0; i < 3; i++){ + for (int j = 0; j < 3; j++){ + if (i == j) { + EXPECT_NE(S(i,j), S_ref(i,j)); + } else { + EXPECT_EQ(S(i,j), S_ref(i,j)); + } + } + } + } +void read_files(Simulation* simulation, const std::string& file_name) +{ + simulation->com_mod.timer.set_time(); -TEST_F(UnitTest_getpk2cc, NeoHookean_2) { - // Step 1: define material properties - // set stM.isoType as NeoHookean - auto& stM = dmn.stM; - stM.isoType = consts::ConstitutiveModelType::stIso_nHook; - // set material parameters - double E = 2e6; // Elasticity_modulus - double nu = 0.5; // Poisson_ratio - double mu = 0.5 * E / (1.0 + nu); // Shear_modulus - stM.C10 = 0.5 * mu; // set_material_props.h - stM.volType = consts::ConstitutiveModelType::stVol_ST91; // Dilational_penalty_model - stM.Kpen = 4e9; // Penalty_parameter - - // Step 2: define fiber orientation (anisotropic only) - Array fN(nsd,nFn); - fN = 0.0; - - // Step 3: define the input - Array F = mat_fun::mat_id(3); - // Step 4: define the reference output - Array S_ref(3,3), Dm_ref(6,6); - S_ref = 0.0; Dm_ref = 0.0; - // Step 5: compare - Array S(3,3), Dm(6,6); - mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); - for (int i = 0; i < 3; i++){ - for (int j = 0; j < 3; j++){ - EXPECT_EQ(S(i,j), S_ref(i,j)); - } + if (simulation->com_mod.cm.slv(simulation->cm_mod)) { + return; } - // more tests ... - F = mat_fun::mat_id(3); - F(0,0) = 2.0; - mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); - for (int i = 0; i < 3; i++){ - for (int j = 0; j < 3; j++){ - if (i == j) { - EXPECT_NE(S(i,j), S_ref(i,j)); - } else { - EXPECT_EQ(S(i,j), S_ref(i,j)); - } - } - } + read_files_ns::read_files(simulation, file_name); } +// class UnitTest_getpk2cc : public ::testing::Test { +// protected: +// // parameters for simulation setup +// Simulation *simulation; +// ComMod com_mod; +// CepMod cep_mod; +// dmnType dmn; +// int nFn; +// double ya_g; +// int nsd; + +// static void SetUpTestSuite() { +// // 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); +// } + +// static void TearDownTestSuite() { +// // Finalize MPI once after all tests are done +// MPI_Finalize(); +// } + +// void setUpSimulation (std::string file_name) { +// // this function can be used if the user has designed input file +// } + +// void SetUp() override { + +// std::string file_name = "../../../../tests/unitTests/unitTest.xml"; +// simulation = new Simulation(); + +// read_files(simulation, file_name); + +// distribute(simulation); +// Vector init_time(3); +// initialize(simulation, init_time); + +// // preparing input arguments for get_pk2cc +// com_mod = simulation->com_mod; +// auto cm_mod_ = simulation->cm_mod; +// auto cm = com_mod.cm; +// cep_mod = simulation->get_cep_mod(); +// 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; +// nFn = lM.nFn; +// if (nFn == 0) {nFn = 1;} + +// // for potential anisotropic material: +// // Array fN(nsd,nFn); +// // fN = 0.0; +// // if (lM.fN.size() != 0) { +// // for (int iFn = 0; iFn < nFn; iFn++) { +// // for (int i = 0; i < nsd; i++) { +// // fN(i,iFn) = lM.fN(i+nsd*iFn,e); +// // } +// // } +// // } + +// int cEq = com_mod.cEq; +// auto& eq = com_mod.eq[cEq]; +// int cDmn = com_mod.cDmn; +// dmn = eq.dmn[cDmn]; +// ya_g = 0.0; // ya_g or ya: a constant related to electromechanics ? +// } + +// }; + + +// TEST_F(UnitTest_getpk2cc, NeoHookean) { +// // Step 1: define material properties +// // set stM.isoType as NeoHookean +// auto& stM = dmn.stM; +// stM.isoType = consts::ConstitutiveModelType::stIso_nHook; +// // set material parameters +// double E = 1e6; // Elasticity_modulus +// double nu = 0.5; // Poisson_ratio +// double mu = 0.5 * E / (1.0 + nu); // Shear_modulus +// stM.C10 = 0.5 * mu; // set_material_props.h +// stM.volType = consts::ConstitutiveModelType::stVol_ST91; // Dilational_penalty_model +// stM.Kpen = 4e9; // Penalty_parameter + +// // Step 2: define fiber orientation (anisotropic only) +// Array fN(nsd,nFn); +// fN = 0.0; + +// // Step 3: define the input +// Array F = mat_fun::mat_id(3); +// // Step 4: define the reference output +// Array S_ref(3,3), Dm_ref(6,6); +// S_ref = 0.0; Dm_ref = 0.0; +// // Step 5: compare +// Array S(3,3), Dm(6,6); +// mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); +// for (int i = 0; i < 3; i++){ +// for (int j = 0; j < 3; j++){ +// EXPECT_EQ(S(i,j), S_ref(i,j)); +// } +// } + +// // more tests ... +// F = mat_fun::mat_id(3); +// F(0,0) = 2.0; +// mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); +// for (int i = 0; i < 3; i++){ +// for (int j = 0; j < 3; j++){ +// if (i == j) { +// EXPECT_NE(S(i,j), S_ref(i,j)); +// } else { +// EXPECT_EQ(S(i,j), S_ref(i,j)); +// } +// } +// } + +// } + + +// TEST_F(UnitTest_getpk2cc, NeoHookean_2) { +// // Step 1: define material properties +// // set stM.isoType as NeoHookean +// auto& stM = dmn.stM; +// stM.isoType = consts::ConstitutiveModelType::stIso_nHook; +// // set material parameters +// double E = 2e6; // Elasticity_modulus +// double nu = 0.5; // Poisson_ratio +// double mu = 0.5 * E / (1.0 + nu); // Shear_modulus +// stM.C10 = 0.5 * mu; // set_material_props.h +// stM.volType = consts::ConstitutiveModelType::stVol_ST91; // Dilational_penalty_model +// stM.Kpen = 4e9; // Penalty_parameter + +// // Step 2: define fiber orientation (anisotropic only) +// Array fN(nsd,nFn); +// fN = 0.0; + +// // Step 3: define the input +// Array F = mat_fun::mat_id(3); +// // Step 4: define the reference output +// Array S_ref(3,3), Dm_ref(6,6); +// S_ref = 0.0; Dm_ref = 0.0; +// // Step 5: compare +// Array S(3,3), Dm(6,6); +// mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); +// for (int i = 0; i < 3; i++){ +// for (int j = 0; j < 3; j++){ +// EXPECT_EQ(S(i,j), S_ref(i,j)); +// } +// } + +// // more tests ... +// F = mat_fun::mat_id(3); +// F(0,0) = 2.0; +// mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); +// for (int i = 0; i < 3; i++){ +// for (int j = 0; j < 3; j++){ +// if (i == j) { +// EXPECT_NE(S(i,j), S_ref(i,j)); +// } else { +// EXPECT_EQ(S(i,j), S_ref(i,j)); +// } +// } +// } +// } + + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); From 47c0e18aba1868e53e9916ff1806bf59baa1f5f4 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Wed, 6 Mar 2024 12:16:16 -0800 Subject: [PATCH 19/23] gmock works, note: initialize mat_fun_carray::ten_init(3) before calling get_pk2cc --- tests/unitTests/test.cpp | 54 +++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/tests/unitTests/test.cpp b/tests/unitTests/test.cpp index 5e8c19b1..05de7245 100644 --- a/tests/unitTests/test.cpp +++ b/tests/unitTests/test.cpp @@ -52,6 +52,8 @@ int argc; char **argv; +using namespace mat_fun; + class MockSimulation : public Simulation { public: MockSimulation() { @@ -104,7 +106,6 @@ class MockcemModelType : public cemModelType { TEST(TestMockObject, sampleTest) { MockSimulation *simulation; - MockComMod com_mod; MockCepMod cep_mod; MockcmType cm; @@ -114,9 +115,6 @@ TEST(TestMockObject, sampleTest) { // preparing input arguments for get_pk2cc // auto com_mod = simulation->com_mod; - - std::cout << "test brreak point 1" << std::endl; - // auto cm_mod_ = simulation->cm_mod; // auto cm = com_mod.cm; // auto cep_mod = simulation->get_cep_mod(); @@ -142,17 +140,12 @@ TEST(TestMockObject, sampleTest) { // } // } - std::cout << "test brreak point 2" << std::endl; - // 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 ? - std::cout << "test brreak point 3" << std::endl; - - // Step 1: define material properties // set stM.isoType as NeoHookean // auto& stM = dmn.stM; @@ -172,42 +165,45 @@ TEST(TestMockObject, sampleTest) { fN = 0.0; // Step 3: define the input - Array F = mat_fun::mat_id(3); + // Array F = mat_fun::mat_id(3); + double F[3][3]; + F[0][0] = 1.0; F[1][1] = 1.0; F[2][2] = 1.0; // Step 4: define the reference output Array S_ref(3,3), Dm_ref(6,6); S_ref = 0.0; Dm_ref = 0.0; // Step 5: compare - Array S(3,3), Dm(6,6); - - std::cout << "test brreak point 5" << std::endl; + // Array S(3,3), Dm(6,6); + double S[3][3], Dm[6][6]; cep_mod.cem = cem; cep_mod.cem.aStress = false; cep_mod.cem.aStrain = false; - mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); + mat_fun_carray::ten_init(3); + + mat_models_carray::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); - std::cout << "test brreak point 6" << std::endl; + double tol = 1e-12; // tolerance for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ - EXPECT_EQ(S(i,j), S_ref(i,j)); + EXPECT_NEAR(S[i][j], S_ref(i,j), tol); } } - // more tests ... - F = mat_fun::mat_id(3); - F(0,0) = 2.0; - mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); - for (int i = 0; i < 3; i++){ - for (int j = 0; j < 3; j++){ - if (i == j) { - EXPECT_NE(S(i,j), S_ref(i,j)); - } else { - EXPECT_EQ(S(i,j), S_ref(i,j)); - } - } - } + // // more tests ... + // F = mat_fun::mat_id(3); + // F(0,0) = 2.0; + // mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); + // for (int i = 0; i < 3; i++){ + // for (int j = 0; j < 3; j++){ + // if (i == j) { + // EXPECT_NE(S[i][j], S_ref(i,j)); + // } else { + // EXPECT_EQ(S[i][j], S_ref(i,j)); + // } + // } + // } } From c91263780e23a8e8ff5792419a7ba2e0f52c0c7f Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Tue, 12 Mar 2024 12:14:34 -0700 Subject: [PATCH 20/23] create a class for isotropic material unit test (only support C10, C01) --- tests/unitTests/test.cpp | 399 +++++---------------------------------- tests/unitTests/test.h | 129 +++++++++++++ 2 files changed, 173 insertions(+), 355 deletions(-) create mode 100644 tests/unitTests/test.h diff --git a/tests/unitTests/test.cpp b/tests/unitTests/test.cpp index 05de7245..589c9156 100644 --- a/tests/unitTests/test.cpp +++ b/tests/unitTests/test.cpp @@ -28,378 +28,67 @@ * 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 -#include -#include -#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" - - -int argc; -char **argv; +#include "test.h" using namespace mat_fun; +using namespace std; -class MockSimulation : public Simulation { -public: - MockSimulation() { - // initialize if needed - } - // Mock methods if needed -}; -class MockComMod : public ComMod { -public: - MockComMod() { - // initialize if needed - } - // Mock methods if needed -}; -class MockCepMod : public CepMod { -public: - MockCepMod() { - // initialize if needed - } - // Mock methods if needed -}; -class MockcmType : public cmType { -public: - MockcmType() { - // initialize if needed - } - // Mock methods if needed -}; -class MockstModelType : public stModelType { -public: - MockstModelType() { - // initialize if needed - } - // Mock methods if needed -}; -class MockdmnType : public dmnType { -public: - MockdmnType() { - // initialize if needed - } - // Mock methods if needed -}; -class MockcemModelType : public cemModelType { -public: - MockcemModelType() { - // initialize if needed - } - // Mock methods if needed -}; - -TEST(TestMockObject, sampleTest) { - MockSimulation *simulation; - MockComMod com_mod; - MockCepMod cep_mod; - MockcmType cm; - MockstModelType stM; - MockdmnType dmn; - MockcemModelType cem; - - // 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(); - com_mod.nsd = 3; // setup by hand - int nsd = com_mod.nsd; - dmn.stM = stM; - int iM = 1; // 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; - int nFn = 0; - if (nFn == 0) {nFn = 1;} - - // for potential anisotropic material: - // Array fN(nsd,nFn); - // fN = 0.0; - // if (lM.fN.size() != 0) { - // for (int iFn = 0; iFn < nFn; iFn++) { - // for (int i = 0; i < nsd; i++) { - // fN(i,iFn) = lM.fN(i+nsd*iFn,e); - // } - // } - // } - // 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 ? - - // Step 1: define material properties - // set stM.isoType as NeoHookean - // auto& stM = dmn.stM; - dmn.stM.isoType = consts::ConstitutiveModelType::stIso_nHook; - // set material parameters +TEST(UnitTest, nHK) { + // Step 1: define parameters + auto matType = consts::ConstitutiveModelType::stIso_nHook; // Material_model: options refer to consts.h + auto volType = consts::ConstitutiveModelType::stVol_ST91; // Dilational_penalty_model double E = 1e6; // Elasticity_modulus double nu = 0.5; // Poisson_ratio - double mu = 0.5 * E / (1.0 + nu); // Shear_modulus - dmn.stM.C10 = 0.5 * mu; // set_material_props.h - dmn.stM.volType = consts::ConstitutiveModelType::stVol_ST91; // Dilational_penalty_model - dmn.stM.Kpen = 4e9; // Penalty_parameter - - std::cout << "test brreak point 4" << std::endl; + double pen = 4e9; // Penalty_parameter + double C01; // additional parameter to C10 (optional) - // Step 2: define fiber orientation (anisotropic only) - Array fN(nsd,nFn); - fN = 0.0; + // Step 2: construct test object + UnitTestIso nHK(matType, E, nu, volType, pen, C01); // Step 3: define the input - // Array F = mat_fun::mat_id(3); - double F[3][3]; - F[0][0] = 1.0; F[1][1] = 1.0; F[2][2] = 1.0; - // Step 4: define the reference output - Array S_ref(3,3), Dm_ref(6,6); - S_ref = 0.0; Dm_ref = 0.0; - // Step 5: compare - // Array S(3,3), Dm(6,6); - double S[3][3], Dm[6][6]; - - cep_mod.cem = cem; - cep_mod.cem.aStress = false; - cep_mod.cem.aStrain = false; - - mat_fun_carray::ten_init(3); - - mat_models_carray::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); + double F[3][3]; + memset(F, 0, sizeof(F)); // initialize + F[0][0] = 1.0; F[1][1] = 1.0; F[2][2] = 1.0; // set to Identity - double tol = 1e-12; // tolerance + // Step 4: define the reference output + double S_ref[3][3], Dm_ref[6][6]; + memset(S_ref, 0, sizeof(S_ref)); + memset(Dm_ref, 0, sizeof(Dm_ref)); - for (int i = 0; i < 3; i++){ - for (int j = 0; j < 3; j++){ - EXPECT_NEAR(S[i][j], S_ref(i,j), tol); - } - } - - // // more tests ... - // F = mat_fun::mat_id(3); - // F(0,0) = 2.0; - // mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); - // for (int i = 0; i < 3; i++){ - // for (int j = 0; j < 3; j++){ - // if (i == j) { - // EXPECT_NE(S[i][j], S_ref(i,j)); - // } else { - // EXPECT_EQ(S[i][j], S_ref(i,j)); - // } - // } - // } + // Step 5: run unit test + nHK.runUnitTest(F, S_ref, Dm_ref); } -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); -} - - -// class UnitTest_getpk2cc : public ::testing::Test { -// protected: -// // parameters for simulation setup -// Simulation *simulation; -// ComMod com_mod; -// CepMod cep_mod; -// dmnType dmn; -// int nFn; -// double ya_g; -// int nsd; - -// static void SetUpTestSuite() { -// // 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); -// } - -// static void TearDownTestSuite() { -// // Finalize MPI once after all tests are done -// MPI_Finalize(); -// } - -// void setUpSimulation (std::string file_name) { -// // this function can be used if the user has designed input file -// } - -// void SetUp() override { - -// std::string file_name = "../../../../tests/unitTests/unitTest.xml"; -// simulation = new Simulation(); - -// read_files(simulation, file_name); - -// distribute(simulation); -// Vector init_time(3); -// initialize(simulation, init_time); - -// // preparing input arguments for get_pk2cc -// com_mod = simulation->com_mod; -// auto cm_mod_ = simulation->cm_mod; -// auto cm = com_mod.cm; -// cep_mod = simulation->get_cep_mod(); -// 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; -// nFn = lM.nFn; -// if (nFn == 0) {nFn = 1;} - -// // for potential anisotropic material: -// // Array fN(nsd,nFn); -// // fN = 0.0; -// // if (lM.fN.size() != 0) { -// // for (int iFn = 0; iFn < nFn; iFn++) { -// // for (int i = 0; i < nsd; i++) { -// // fN(i,iFn) = lM.fN(i+nsd*iFn,e); -// // } -// // } -// // } - -// int cEq = com_mod.cEq; -// auto& eq = com_mod.eq[cEq]; -// int cDmn = com_mod.cDmn; -// dmn = eq.dmn[cDmn]; -// ya_g = 0.0; // ya_g or ya: a constant related to electromechanics ? -// } - -// }; - - -// TEST_F(UnitTest_getpk2cc, NeoHookean) { -// // Step 1: define material properties -// // set stM.isoType as NeoHookean -// auto& stM = dmn.stM; -// stM.isoType = consts::ConstitutiveModelType::stIso_nHook; -// // set material parameters -// double E = 1e6; // Elasticity_modulus -// double nu = 0.5; // Poisson_ratio -// double mu = 0.5 * E / (1.0 + nu); // Shear_modulus -// stM.C10 = 0.5 * mu; // set_material_props.h -// stM.volType = consts::ConstitutiveModelType::stVol_ST91; // Dilational_penalty_model -// stM.Kpen = 4e9; // Penalty_parameter - -// // Step 2: define fiber orientation (anisotropic only) -// Array fN(nsd,nFn); -// fN = 0.0; - -// // Step 3: define the input -// Array F = mat_fun::mat_id(3); -// // Step 4: define the reference output -// Array S_ref(3,3), Dm_ref(6,6); -// S_ref = 0.0; Dm_ref = 0.0; -// // Step 5: compare -// Array S(3,3), Dm(6,6); -// mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); -// for (int i = 0; i < 3; i++){ -// for (int j = 0; j < 3; j++){ -// EXPECT_EQ(S(i,j), S_ref(i,j)); -// } -// } - -// // more tests ... -// F = mat_fun::mat_id(3); -// F(0,0) = 2.0; -// mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); -// for (int i = 0; i < 3; i++){ -// for (int j = 0; j < 3; j++){ -// if (i == j) { -// EXPECT_NE(S(i,j), S_ref(i,j)); -// } else { -// EXPECT_EQ(S(i,j), S_ref(i,j)); -// } -// } -// } - -// } - - -// TEST_F(UnitTest_getpk2cc, NeoHookean_2) { -// // Step 1: define material properties -// // set stM.isoType as NeoHookean -// auto& stM = dmn.stM; -// stM.isoType = consts::ConstitutiveModelType::stIso_nHook; -// // set material parameters -// double E = 2e6; // Elasticity_modulus -// double nu = 0.5; // Poisson_ratio -// double mu = 0.5 * E / (1.0 + nu); // Shear_modulus -// stM.C10 = 0.5 * mu; // set_material_props.h -// stM.volType = consts::ConstitutiveModelType::stVol_ST91; // Dilational_penalty_model -// stM.Kpen = 4e9; // Penalty_parameter - -// // Step 2: define fiber orientation (anisotropic only) -// Array fN(nsd,nFn); -// fN = 0.0; +TEST(UnitTest, MR) { + // Step 1: define parameters + auto matType = consts::ConstitutiveModelType::stIso_MR; // Material_model: options refer to consts.h + auto volType = consts::ConstitutiveModelType::stVol_ST91; // Dilational_penalty_model + double E = 1e6; // Elasticity_modulus + double nu = 0.495; // Poisson_ratio + double pen = 4e9; // Penalty_parameter + double C01 = 0.1; // additional parameter to C10 (optional) -// // Step 3: define the input -// Array F = mat_fun::mat_id(3); -// // Step 4: define the reference output -// Array S_ref(3,3), Dm_ref(6,6); -// S_ref = 0.0; Dm_ref = 0.0; -// // Step 5: compare -// Array S(3,3), Dm(6,6); -// mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); -// for (int i = 0; i < 3; i++){ -// for (int j = 0; j < 3; j++){ -// EXPECT_EQ(S(i,j), S_ref(i,j)); -// } -// } + // Step 2: construct test object + UnitTestIso MR(matType, E, nu, volType, pen, C01); -// // more tests ... -// F = mat_fun::mat_id(3); -// F(0,0) = 2.0; -// mat_models::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); -// for (int i = 0; i < 3; i++){ -// for (int j = 0; j < 3; j++){ -// if (i == j) { -// EXPECT_NE(S(i,j), S_ref(i,j)); -// } else { -// EXPECT_EQ(S(i,j), S_ref(i,j)); -// } -// } -// } -// } + // Step 3: define the input + double F[3][3]; + memset(F, 0, sizeof(F)); // initialize + F[0][0] = 1.0; F[1][1] = 1.0; F[2][2] = 1.0; // set to Identity + // Step 4: define the reference output + double S_ref[3][3], Dm_ref[6][6]; + memset(S_ref, 0, sizeof(S_ref)); + memset(Dm_ref, 0, sizeof(Dm_ref)); -int main(int argc, char **argv) { - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); + // Step 5: run unit test + MR.runUnitTest(F, S_ref, Dm_ref); + } -/* ++++++++++++++++++++++++ why do we need an input file for unit test? ++++++++++++++++++++++++++ - 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(...) -*/ \ No newline at end of file +// int main(int argc, char **argv) { +// ::testing::InitGoogleTest(&argc, argv); +// return RUN_ALL_TESTS(); +// } diff --git a/tests/unitTests/test.h b/tests/unitTests/test.h new file mode 100644 index 00000000..0319f707 --- /dev/null +++ b/tests/unitTests/test.h @@ -0,0 +1,129 @@ +/* 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. + */ + +#include +#include +#include "gtest/gtest.h" // include GoogleTest +#include "mat_fun.h" +#include "mat_fun_carray.h" +#include "mat_models.h" +#include "mat_models_carray.h" + + + + + +class MockCepMod : public CepMod { +public: + MockCepMod() { + // initialize if needed + } + // Mock methods if needed +}; +class MockdmnType : public dmnType { +public: + MockdmnType() { + // initialize if needed + } + // MockstModelType mockStM; + // Mock methods if needed +}; +class MockmshType : public mshType { +public: + MockmshType() { + // initialize if needed + } + // Mock methods if needed +}; +class MockeqType : public eqType { +public: + MockeqType() { + // initialize if needed + } + MockdmnType mockDmn; + // Mock methods if needed +}; +class MockComMod : public ComMod { +public: + MockComMod() { + // initialize if needed + nsd = 3; + } + MockeqType mockEq; + MockmshType mockMsh; + // Mock methods if needed +}; + + +class UnitTestIso { +public: + MockComMod com_mod; + MockCepMod cep_mod; + + // UnitTestIso(consts::ConstitutiveModelType matType, double E, double nu, + // consts::ConstitutiveModelType penType, double pen) + UnitTestIso(consts::ConstitutiveModelType matType, double E, double nu, + consts::ConstitutiveModelType penType, double pen, double C01 = 0.0) { + int nsd = com_mod.nsd; + auto &dmn = com_mod.mockEq.mockDmn; + mat_fun_carray::ten_init(nsd); // initialize tensor index pointer + dmn.stM.isoType = matType; // Mat_model + double mu = 0.5 * E / (1.0 + nu); // Shear_modulus + dmn.stM.C10 = 0.5 * mu - C01; // set_material_props.h + dmn.stM.C01 = C01; + dmn.stM.volType = penType; // Dilational_penalty_model + dmn.stM.Kpen = pen; // Penalty_parameter + } + + void runUnitTest(double F[3][3], double S_ref[3][3], double Dm_ref[6][6]) { + int nsd = com_mod.nsd; + auto &dmn = com_mod.mockEq.mockDmn; + // hard code for nHK + int nFn = 1; + Array fN(nsd, nFn); + double ya_g = 0.0; + double S[3][3], Dm[6][6]; + // memset(S, 0, sizeof(S)); + // memset(Dm, 0, sizeof(Dm)); + + mat_models_carray::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); + double tol = 1e-12; // tolerance + + for (int i = 0; i < 3; i++){ + for (int j = 0; j < 3; j++){ + EXPECT_NEAR(S[i][j], S_ref[i][j], tol); + } + } + + + } + + +}; \ No newline at end of file From c5d0b31e97b0545a7e9c2d7ef0bf215ddbbe90e7 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Tue, 12 Mar 2024 19:40:08 -0700 Subject: [PATCH 21/23] some clean up --- Code/Source/svFSI/CMakeLists.txt | 2 +- Code/Source/svFSI/mat_models.cpp | 27 ------------ tests/unitTests/mesh/unit_cube_volume.vtu | 3 -- tests/unitTests/test.cpp | 8 +--- tests/unitTests/test.h | 16 ++----- tests/unitTests/unitTest.xml | 53 ----------------------- 6 files changed, 6 insertions(+), 103 deletions(-) delete mode 100644 tests/unitTests/mesh/unit_cube_volume.vtu delete mode 100644 tests/unitTests/unitTest.xml diff --git a/Code/Source/svFSI/CMakeLists.txt b/Code/Source/svFSI/CMakeLists.txt index d22f5453..00e7ccdc 100644 --- a/Code/Source/svFSI/CMakeLists.txt +++ b/Code/Source/svFSI/CMakeLists.txt @@ -309,7 +309,7 @@ if(ENABLE_UNIT_TEST) ) # gtest_discover_tests(runUnitTest) - add_test(NAME UnitTest COMMAND run_all_unit_tests) + add_test(NAME all_unit_tests COMMAND run_all_unit_tests) endif() diff --git a/Code/Source/svFSI/mat_models.cpp b/Code/Source/svFSI/mat_models.cpp index 35460000..6f28fa60 100644 --- a/Code/Source/svFSI/mat_models.cpp +++ b/Code/Source/svFSI/mat_models.cpp @@ -214,8 +214,6 @@ void get_pk2cc(const ComMod& com_mod, const CepMod& cep_mod, const dmnType& lDmn // Tensor4 CC(nsd,nsd,nsd,nsd); - std::cout << "test brreak point *1" << std::endl; - switch (stM.isoType) { case ConstitutiveModelType::stIso_lin: { double g1 = stM.C10; // mu @@ -244,16 +242,12 @@ void get_pk2cc(const ComMod& com_mod, const CepMod& cep_mod, const dmnType& lDmn // NeoHookean model case ConstitutiveModelType::stIso_nHook: { - std::cout << "test brreak point *2" << std::endl; - double g1 = 2.0 * stM.C10; auto Sb = g1*Idm; // Fiber reinforcement/active stress Sb += Tfa * mat_dyad_prod(fl.col(0), fl.col(0), nsd); - std::cout << "test brreak point *3" << std::endl; - double r1 = g1 * Inv1 / nd; for (int j = 0; j < S.ncols(); j++) { for (int i = 0; i < S.nrows(); i++) { @@ -261,31 +255,10 @@ void get_pk2cc(const ComMod& com_mod, const CepMod& cep_mod, const dmnType& lDmn } } - std::cout << "test brreak point *4" << std::endl; - std::cout << "nd, nsd: " << nd << ", "<< nsd << std::endl; - S.print("S: "); - Ci.print("Ci: "); - - - Array A(3, 3); - Array B = mat_fun::mat_id(3); - A.print("this is A: "); - B.print("this is B: "); - Tensor4 ret = ten_dyad_prod(A, B, 3); - std::cout << "test brreak point *4" << std::endl; - - - ten_dyad_prod(B, A, 3); - std::cout << "test brreak point *4" << std::endl; - CC = (-2.0/nd) * ( ten_dyad_prod(Ci, S, nsd) + ten_dyad_prod(S, Ci, nsd)); - std::cout << "test brreak point *5" << std::endl; S += p*J*Ci; - std::cout << "test brreak point *6" << std::endl; CC += 2.0*(r1 - p*J) * ten_symm_prod(Ci, Ci, nsd) + (pl*J - 2.0*r1/nd) * ten_dyad_prod(Ci, Ci, nsd); - std::cout << "test brreak point *7" << std::endl; - } break; // Mooney-Rivlin model diff --git a/tests/unitTests/mesh/unit_cube_volume.vtu b/tests/unitTests/mesh/unit_cube_volume.vtu deleted file mode 100644 index d04eef5d..00000000 --- a/tests/unitTests/mesh/unit_cube_volume.vtu +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:ed473845550ea1347d62290da2b9819d9620a8f6aaf520c799d70f7bceec0285 -size 1807 diff --git a/tests/unitTests/test.cpp b/tests/unitTests/test.cpp index 589c9156..2ef46fb0 100644 --- a/tests/unitTests/test.cpp +++ b/tests/unitTests/test.cpp @@ -34,7 +34,7 @@ using namespace mat_fun; using namespace std; -TEST(UnitTest, nHK) { +TEST(UnitTestIso_1, nHK) { // Step 1: define parameters auto matType = consts::ConstitutiveModelType::stIso_nHook; // Material_model: options refer to consts.h auto volType = consts::ConstitutiveModelType::stVol_ST91; // Dilational_penalty_model @@ -61,7 +61,7 @@ TEST(UnitTest, nHK) { } -TEST(UnitTest, MR) { +TEST(UnitTestIso_2, MR) { // Step 1: define parameters auto matType = consts::ConstitutiveModelType::stIso_MR; // Material_model: options refer to consts.h auto volType = consts::ConstitutiveModelType::stVol_ST91; // Dilational_penalty_model @@ -88,7 +88,3 @@ TEST(UnitTest, MR) { } -// int main(int argc, char **argv) { -// ::testing::InitGoogleTest(&argc, argv); -// return RUN_ALL_TESTS(); -// } diff --git a/tests/unitTests/test.h b/tests/unitTests/test.h index 0319f707..d6aabdd6 100644 --- a/tests/unitTests/test.h +++ b/tests/unitTests/test.h @@ -36,10 +36,6 @@ #include "mat_models.h" #include "mat_models_carray.h" - - - - class MockCepMod : public CepMod { public: MockCepMod() { @@ -81,14 +77,13 @@ class MockComMod : public ComMod { // Mock methods if needed }; - +// Class for unit test of isotropic material model (currently only introduced two +// parameters C10 and C01) class UnitTestIso { public: MockComMod com_mod; MockCepMod cep_mod; - // UnitTestIso(consts::ConstitutiveModelType matType, double E, double nu, - // consts::ConstitutiveModelType penType, double pen) UnitTestIso(consts::ConstitutiveModelType matType, double E, double nu, consts::ConstitutiveModelType penType, double pen, double C01 = 0.0) { int nsd = com_mod.nsd; @@ -110,20 +105,15 @@ class UnitTestIso { Array fN(nsd, nFn); double ya_g = 0.0; double S[3][3], Dm[6][6]; - // memset(S, 0, sizeof(S)); - // memset(Dm, 0, sizeof(Dm)); mat_models_carray::get_pk2cc(com_mod, cep_mod, dmn, F, nFn, fN, ya_g, S, Dm); double tol = 1e-12; // tolerance + // Compare with reference solution for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ EXPECT_NEAR(S[i][j], S_ref[i][j], tol); } } - - } - - }; \ No newline at end of file diff --git a/tests/unitTests/unitTest.xml b/tests/unitTests/unitTest.xml deleted file mode 100644 index bb2645b9..00000000 --- a/tests/unitTests/unitTest.xml +++ /dev/null @@ -1,53 +0,0 @@ - - - - - 0 - 3 - 20 - 0.01 - 0.50 - STOP_SIM - - 1 - result - 1 - 1 - - 2 - 0 - - 1 - 0 - 0 - - - - - - - ../../../../tests/unitTests/mesh/unit_cube_volume.vtu - - - - - - - true - 3 - 10 - 1e-6 - - - 0.0 - 240.56596E6 - 0.5 - - ST91 - 4.0E9 - - - - - - From a1a6f91fc364de6ee4b5269e5f508afd6f7dc337 Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Wed, 13 Mar 2024 11:19:24 -0700 Subject: [PATCH 22/23] add --verbose --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1855c326..7a5ff747 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -42,7 +42,7 @@ jobs: - name: Run unit tests run: | cd build/svFSI-build/Source/svFSI - ctest --output-on-failure + ctest --verbose - name: Generate code coverage if: startsWith(matrix.os, 'ubuntu-22.04') run: | From 2bf104c3da132208122afa810d3668e926655dfe Mon Sep 17 00:00:00 2001 From: Yuecheng Yu Date: Wed, 13 Mar 2024 13:18:03 -0700 Subject: [PATCH 23/23] initialize static arrays with {} --- tests/unitTests/test.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tests/unitTests/test.cpp b/tests/unitTests/test.cpp index 2ef46fb0..7f24916a 100644 --- a/tests/unitTests/test.cpp +++ b/tests/unitTests/test.cpp @@ -47,14 +47,12 @@ TEST(UnitTestIso_1, nHK) { UnitTestIso nHK(matType, E, nu, volType, pen, C01); // Step 3: define the input - double F[3][3]; - memset(F, 0, sizeof(F)); // initialize + double F[3][3] = {}; F[0][0] = 1.0; F[1][1] = 1.0; F[2][2] = 1.0; // set to Identity // Step 4: define the reference output - double S_ref[3][3], Dm_ref[6][6]; - memset(S_ref, 0, sizeof(S_ref)); - memset(Dm_ref, 0, sizeof(Dm_ref)); + double S_ref[3][3] = {}; + double Dm_ref[6][6] = {}; // Step 5: run unit test nHK.runUnitTest(F, S_ref, Dm_ref); @@ -74,14 +72,12 @@ TEST(UnitTestIso_2, MR) { UnitTestIso MR(matType, E, nu, volType, pen, C01); // Step 3: define the input - double F[3][3]; - memset(F, 0, sizeof(F)); // initialize + double F[3][3] = {}; F[0][0] = 1.0; F[1][1] = 1.0; F[2][2] = 1.0; // set to Identity // Step 4: define the reference output - double S_ref[3][3], Dm_ref[6][6]; - memset(S_ref, 0, sizeof(S_ref)); - memset(Dm_ref, 0, sizeof(Dm_ref)); + double S_ref[3][3] = {}; + double Dm_ref[6][6] = {}; // Step 5: run unit test MR.runUnitTest(F, S_ref, Dm_ref);