Skip to content

Commit 553849b

Browse files
Split up unit tests (SimVascular#413)
* distributed code from test.h and test.cpp to separate files, appears to be working for neohookean * Refactor mooney rivlin tests, make helper function inline to fix linker error * Refactoring holzapfel ogden and holapfel ogden MA tests * Refactoring CANN NH and HO tests. Fix typo in holzapfel_ogden header guard * Refactoring the 3 volumetric penalty model tests * Removing original test.h and test.cpp, since all code from these files has been moved to separate new files * Moved all material model tests into subfolder, modified CMakeLists.txt to automatically find all .cpp files under unitTests/, add README.md explaining unit tests * Add copyright notices to all material model test files * Fix typo in test_material_holzapfel_ogden_MA.h Co-authored-by: Copilot <[email protected]> * cosmetic changes to two helper functions * Remove extraneous comments from unit test files, following comments by @ktbolt * Refactor fiber and sheet direction handling in Holzapfel-Ogden material tests to use Vector class for improved clarity and performance. Refactor convertToArray() to use const reference. Per comment by @ktbolt * Refactor deformation gradient lists to contain Array<double> instead of std:Arrays, avoiding conversion between the two. Also, adjusted the tolerances on some tests to get them to pass. Also, use const references when accessing elements of F_small_list, for example * Adjust convergence order tolerances slightly for HO-ma tests so they pass on ubuntu Github machine * Replacing 1e-6 tolerance for f.s check with std::numeric_limits<double>::epsilon() * Use static constexpr and capital case for material test tolerances etc. * Minor changes to CANN material testing files * Remove unnecessary nullptr assignments in TearDown methods across various material model test files for cleaner code. --------- Co-authored-by: Copilot <[email protected]>
1 parent 6a68d14 commit 553849b

23 files changed

+4287
-3986
lines changed

Code/Source/solver/CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,17 @@ if(ENABLE_UNIT_TEST)
315315

316316
# add test.cpp for unit test
317317

318-
# remove the main.cpp and add test.cpp
319-
set(TEST_SOURCES "../../../tests/unitTests/test.cpp")
318+
# Automatically find all test files under unitTests directory
319+
file(GLOB_RECURSE TEST_SOURCES
320+
"../../../tests/unitTests/*.cpp"
321+
)
322+
list(LENGTH TEST_SOURCES TEST_SOURCES_COUNT)
323+
message(STATUS "Found ${TEST_SOURCES_COUNT} test files")
324+
foreach(TEST_SOURCE ${TEST_SOURCES})
325+
# Print the file being copied for debugging
326+
message(STATUS "TEST_SOURCE: ${TEST_SOURCE}")
327+
endforeach()
328+
320329
list(REMOVE_ITEM CSRCS "main.cpp")
321330
list(APPEND CSRCS ${TEST_SOURCES})
322331

tests/unitTests/README.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# Unit Tests for svMultiPhysics
2+
3+
This directory contains unit tests for svMultiPhysics
4+
5+
## Building and Running Tests
6+
7+
### Prerequisites
8+
- svMultiPhysics must be built with unit tests enabled:
9+
```bash
10+
cmake -DENABLE_UNIT_TEST=ON ..
11+
```
12+
13+
### Building Tests
14+
1. Navigate to the build directory:
15+
```bash
16+
cd <svMultiPhysics_root_directory>/build/svMultiPhysics-build/Source/solver
17+
```
18+
19+
2. Build the tests:
20+
```bash
21+
make
22+
```
23+
24+
### Running Tests
25+
Run all unit tests:
26+
```bash
27+
ctest --verbose
28+
```
29+
30+
Or run the test executable directly:
31+
```bash
32+
./run_all_unit_tests
33+
```
34+
35+
## Common Files
36+
### `test_common.h`
37+
- Contains mock objects for svMultiPhysics components
38+
- Provides `TestBase` class with common test infrastructure
39+
- Includes Google Test framework
40+
41+
42+
43+
## Material Model Testing
44+
45+
Each material model follows this pattern:
46+
47+
1. **Parameter Class** (e.g., `NeoHookeanParams`)
48+
- Inherits from `MatParams`
49+
- Contains material-specific parameters
50+
51+
2. **Test Material Class** (e.g., `TestNeoHookean`)
52+
- Inherits from `TestMaterialModel`
53+
- Implements required virtual methods:
54+
- `printMaterialParameters()`
55+
- `computeStrainEnergy()`
56+
- Sets up material parameters for svMultiPhysics
57+
58+
3. **Test Fixture Class** (e.g., `NeoHookeanTest`)
59+
- Inherits from `MaterialTestFixture`
60+
- Sets up test parameters and objects
61+
62+
4. **Test Cases**
63+
- Use Google Test macros (`TEST_F`)
64+
- Test the material model second Piola-Kirchoff stress and material elasticity tensor in a variety of ways
65+
66+
### Material Models Currently Tested
67+
68+
#### Hyperelastic Models
69+
- **Neo-Hookean**: Basic hyperelastic model
70+
- **Mooney-Rivlin**: Two-parameter hyperelastic model
71+
- **Holzapfel-Ogden**: Anisotropic hyperelastic model for soft tissues
72+
- **Holzapfel-Ogden Modified Anisotropy**: Enhanced version with modified anisotropy
73+
74+
#### Artificial Neural Network Models
75+
- **CANN Neo-Hookean**: Neural network approximation of Neo-Hookean behavior
76+
- **CANN Holzapfel-Ogden**: Neural network approximation of Holzapfel-Ogden behavior
77+
78+
#### Volumetric Penalty Models
79+
- **Quadratic**: Simple quadratic volumetric penalty
80+
- **Simo-Taylor 91**: Simo-Taylor volumetric penalty model
81+
- **Miehe 94**: Miehe volumetric penalty model
82+
83+
84+
### Adding a New Material Model
85+
86+
To add a new material model for testing:
87+
88+
#### 1. Create Header File (`test_material_newmodel.h`)
89+
```cpp
90+
#ifndef TEST_MATERIAL_NEWMODEL_H
91+
#define TEST_MATERIAL_NEWMODEL_H
92+
93+
#include "test_material_common.h"
94+
95+
// Parameter class
96+
class NewModelParams : public MatParams {
97+
public:
98+
double param1, param2;
99+
// Constructors...
100+
};
101+
102+
// Test material class
103+
class TestNewModel : public TestMaterialModel {
104+
public:
105+
NewModelParams params;
106+
107+
TestNewModel(const NewModelParams &params_) :
108+
TestMaterialModel(consts::ConstitutiveModelType::stIso_NewModel,
109+
consts::ConstitutiveModelType::stVol_ST91),
110+
params(params_) {
111+
// Set parameters for svMultiPhysics
112+
}
113+
114+
void printMaterialParameters() override {
115+
// Print parameters
116+
}
117+
118+
double computeStrainEnergy(const Array<double> &F) override {
119+
// Compute strain energy density
120+
}
121+
};
122+
123+
#endif
124+
```
125+
126+
#### 2. Create Source File (`test_material_newmodel.cpp`)
127+
```cpp
128+
#include "test_material_newmodel.h"
129+
130+
class NewModelTest : public MaterialTestFixture {
131+
protected:
132+
NewModelParams params;
133+
TestNewModel* TestNM;
134+
135+
void SetUp() override {
136+
MaterialTestFixture::SetUp();
137+
// Set up parameters
138+
TestNM = new TestNewModel(params);
139+
}
140+
141+
void TearDown() override {
142+
delete TestNM;
143+
}
144+
};
145+
146+
// Test cases
147+
TEST_F(NewModelTest, TestPK2StressIdentityF) {
148+
// Test implementation
149+
}
150+
```
151+
152+
153+
#### 3. Automatic Discovery of Test Files
154+
The CMakeLists.txt automatically finds all `.cpp` files under `unitTests/`, so no build configuration changes are needed.
155+

0 commit comments

Comments
 (0)