Skip to content

Commit 2ef9a22

Browse files
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
1 parent 3dfc890 commit 2ef9a22

7 files changed

+53
-65
lines changed

tests/unitTests/material_model_tests/test_material_CANN_holzapfel_ogden.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ class HolzapfelOgdenCompareTest : public MaterialTestFixture {
7171
params_HO.f[0] = getRandomDouble(0.0, 1.0);
7272
params_HO.f[1] = getRandomDouble(0.0, 1.0);
7373
params_HO.f[2] = getRandomDouble(0.0, 1.0);
74-
double norm_f = sqrt(params_HO.f[0]*params_HO.f[0] + params_HO.f[1]*params_HO.f[1] + params_HO.f[2]*params_HO.f[2]);
75-
params_HO.f[0] /= norm_f; params_HO.f[1] /= norm_f; params_HO.f[2] /= norm_f;
74+
double norm_f = sqrt(params_HO.f.dot(params_HO.f));
75+
params_HO.f = params_HO.f / norm_f;
7676

7777
// Create s orthogonal to f
7878
if (fabs(params_HO.f[0]) < 0.9) { // Check if f[0] is not the dominant component
@@ -86,11 +86,11 @@ class HolzapfelOgdenCompareTest : public MaterialTestFixture {
8686
}
8787

8888
// Normalize s
89-
double norm_s = sqrt(params_HO.s[0]*params_HO.s[0] + params_HO.s[1]*params_HO.s[1] + params_HO.s[2]*params_HO.s[2]);
90-
params_HO.s[0] /= norm_s; params_HO.s[1] /= norm_s; params_HO.s[2] /= norm_s;
89+
double norm_s = sqrt(params_HO.s.dot(params_HO.s));
90+
params_HO.s = params_HO.s / norm_s;
9191

9292
// Check f.s = 0
93-
double dot_fs = params_HO.f[0]*params_HO.s[0] + params_HO.f[1]*params_HO.s[1] + params_HO.f[2]*params_HO.s[2];
93+
double dot_fs = params_HO.f.dot(params_HO.s);
9494
if (fabs(dot_fs) > 1e-6) {
9595
std::cout << "f.s = " << dot_fs << std::endl;
9696
std::cout << "f = [" << params_HO.f[0] << ", " << params_HO.f[1] << ", " << params_HO.f[2] << "]" << std::endl;

tests/unitTests/material_model_tests/test_material_CANN_holzapfel_ogden.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,29 @@ class CANN_HO_Params : public MatParams {
3939
public:
4040
std::vector<CANNRow> Table;
4141
// Define fiber directions
42-
double f[3]; // Fiber direction
43-
double s[3]; // Sheet direction
42+
Vector<double> f; // Fiber direction
43+
Vector<double> s; // Sheet direction
4444

4545
// Default constructor
4646
CANN_HO_Params() {
4747

4848
// Resize Table to ensure there's at least 4 elements
4949
Table.resize(4); // Ensure there's space for 4 rows
50+
51+
f.resize(3);
52+
s.resize(3);
5053
};
5154

5255
// Constructor with parameters
53-
CANN_HO_Params(std::vector<CANNRow> TableValues) {
56+
CANN_HO_Params(std::vector<CANNRow> TableValues, Vector<double> f, Vector<double> s) {
5457
for (int i = 0; i < 4; i++){
5558
this -> Table[i].invariant_index = TableValues[i].invariant_index;
5659
this -> Table[i].activation_functions = TableValues[i].activation_functions;
5760
this -> Table[i].weights = TableValues[i].weights;
58-
}
61+
}
62+
63+
this -> f = f;
64+
this -> s = s;
5965
};
6066

6167
};
@@ -118,10 +124,8 @@ class TestCANN_HO : public TestMaterialModel {
118124

119125
// Set number of fiber directions and fiber directions
120126
nFn = 2;
121-
Vector<double> f = {params.f[0], params.f[1], params.f[2]};
122-
Vector<double> s = {params.s[0], params.s[1], params.s[2]};
123-
fN.set_col(0, f);
124-
fN.set_col(1, s);
127+
fN.set_col(0, params.f);
128+
fN.set_col(1, params.s);
125129
}
126130

127131
/**
@@ -152,8 +156,8 @@ class TestCANN_HO : public TestMaterialModel {
152156
solidMechanicsTerms smTerms = calcSolidMechanicsTerms(F);
153157

154158
// Fiber and sheet directions
155-
Vector<double> f = {params.f[0], params.f[1], params.f[2]};
156-
Vector<double> s = {params.s[0], params.s[1], params.s[2]};
159+
Vector<double> f = params.f;
160+
Vector<double> s = params.s;
157161

158162
// Strain energy density for Holzapfel-Ogden material model
159163

tests/unitTests/material_model_tests/test_material_common.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,14 +1789,14 @@ class MaterialTestFixture : public ::testing::Test {
17891789
}
17901790

17911791
// Function to convert std::array to Array
1792-
void convertToArray(Array3x3 stdArray, Array<double> F) {
1793-
int N = F.ncols();
1794-
assert(F.nrows() == N);
1792+
void convertToArray(const Array3x3 &stdArray, Array<double> array) {
1793+
int N = array.ncols();
1794+
assert(array.nrows() == N);
17951795
assert(N == 3);
17961796

1797-
for (int i = 0; i < 3; ++i) {
1798-
for (int j = 0; j < 3; ++j) {
1799-
F(i,j) = stdArray[i][j];
1797+
for (int i = 0; i < N; ++i) {
1798+
for (int j = 0; j < N; ++j) {
1799+
array(i,j) = stdArray[i][j];
18001800
}
18011801
}
18021802
}

tests/unitTests/material_model_tests/test_material_holzapfel_ogden.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ class HolzapfelOgdenTest : public :: MaterialTestFixture {
7171
params.f[0] = getRandomDouble(0.0, 1.0);
7272
params.f[1] = getRandomDouble(0.0, 1.0);
7373
params.f[2] = getRandomDouble(0.0, 1.0);
74-
double norm_f = sqrt(params.f[0]*params.f[0] + params.f[1]*params.f[1] + params.f[2]*params.f[2]);
75-
params.f[0] /= norm_f; params.f[1] /= norm_f; params.f[2] /= norm_f;
74+
double norm_f = sqrt(params.f.dot(params.f));
75+
params.f = params.f / norm_f;
7676

7777
// Create s orthogonal to f
7878
if (fabs(params.f[0]) < 0.9) { // Check if f[0] is not the dominant component
@@ -86,11 +86,11 @@ class HolzapfelOgdenTest : public :: MaterialTestFixture {
8686
}
8787

8888
// Normalize s
89-
double norm_s = sqrt(params.s[0]*params.s[0] + params.s[1]*params.s[1] + params.s[2]*params.s[2]);
90-
params.s[0] /= norm_s; params.s[1] /= norm_s; params.s[2] /= norm_s;
89+
double norm_s = sqrt(params.s.dot(params.s));
90+
params.s = params.s / norm_s;
9191

9292
// Check f.s = 0
93-
double dot_fs = params.f[0]*params.s[0] + params.f[1]*params.s[1] + params.f[2]*params.s[2];
93+
double dot_fs = params.f.dot(params.s);
9494
if (fabs(dot_fs) > 1e-6) {
9595
std::cout << "f.s = " << dot_fs << std::endl;
9696
std::cout << "f = [" << params.f[0] << ", " << params.f[1] << ", " << params.f[2] << "]" << std::endl;

tests/unitTests/material_model_tests/test_material_holzapfel_ogden.h

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,19 @@ class HolzapfelOgdenParams : public MatParams {
4444
double b_s;
4545
double a_fs;
4646
double b_fs;
47-
double f[3]; // Fiber direction
48-
double s[3]; // Sheet direction
47+
Vector<double> f; // Fiber direction
48+
Vector<double> s; // Sheet direction
4949

5050
double k; // Smoothed Heaviside function parameter
5151

5252
// Default constructor
5353
HolzapfelOgdenParams() : a(0.0), b(0.0), a_f(0.0), b_f(0.0), a_s(0.0), b_s(0.0), a_fs(0.0), b_fs(0.0), k(0.0) {
54-
for (int i = 0; i < 3; i++) {
55-
f[i] = 0.0;
56-
s[i] = 0.0;
57-
}
54+
f.resize(3);
55+
s.resize(3);
5856
}
5957

6058
// Constructor with parameters
61-
HolzapfelOgdenParams(double a, double b, double a_f, double b_f, double a_s, double b_s, double a_fs, double b_fs, double k, double f[3], double s[3]) : a(a), b(b), a_f(a_f), b_f(b_f), a_s(a_s), b_s(b_s), a_fs(a_fs), b_fs(b_fs), k(k) {
62-
for (int i = 0; i < 3; i++) {
63-
this->f[i] = f[i];
64-
this->s[i] = s[i];
65-
}
59+
HolzapfelOgdenParams(double a, double b, double a_f, double b_f, double a_s, double b_s, double a_fs, double b_fs, double k, Vector<double> f, Vector<double> s) : a(a), b(b), a_f(a_f), b_f(b_f), a_s(a_s), b_s(b_s), a_fs(a_fs), b_fs(b_fs), k(k), f(f), s(s) {
6660
}
6761
};
6862

@@ -105,10 +99,8 @@ class TestHolzapfelOgden : public TestMaterialModel {
10599

106100
// Set number of fiber directions and fiber directions
107101
nFn = 2;
108-
Vector<double> f = {params.f[0], params.f[1], params.f[2]};
109-
Vector<double> s = {params.s[0], params.s[1], params.s[2]};
110-
fN.set_col(0, f);
111-
fN.set_col(1, s);
102+
fN.set_col(0, params.f);
103+
fN.set_col(1, params.s);
112104
}
113105

114106
/**
@@ -163,8 +155,8 @@ class TestHolzapfelOgden : public TestMaterialModel {
163155
double k = params.k;
164156

165157
// Fiber and sheet directions
166-
Vector<double> f = {params.f[0], params.f[1], params.f[2]};
167-
Vector<double> s = {params.s[0], params.s[1], params.s[2]};
158+
Vector<double> f = params.f;
159+
Vector<double> s = params.s;
168160

169161
// Strain energy density for Holzapfel-Ogden material model
170162

tests/unitTests/material_model_tests/test_material_holzapfel_ogden_MA.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ class HolzapfelOgdenMATest : public MaterialTestFixture {
6767
params.f[0] = getRandomDouble(0.0, 1.0);
6868
params.f[1] = getRandomDouble(0.0, 1.0);
6969
params.f[2] = getRandomDouble(0.0, 1.0);
70-
double norm_f = sqrt(params.f[0]*params.f[0] + params.f[1]*params.f[1] + params.f[2]*params.f[2]);
71-
params.f[0] /= norm_f; params.f[1] /= norm_f; params.f[2] /= norm_f;
70+
double norm_f = sqrt(params.f.dot(params.f));
71+
params.f = params.f / norm_f;
7272

7373
// Create s orthogonal to f
7474
if (fabs(params.f[0]) < 0.9) { // Check if f[0] is not the dominant component
@@ -82,11 +82,11 @@ class HolzapfelOgdenMATest : public MaterialTestFixture {
8282
}
8383

8484
// Normalize s
85-
double norm_s = sqrt(params.s[0]*params.s[0] + params.s[1]*params.s[1] + params.s[2]*params.s[2]);
86-
params.s[0] /= norm_s; params.s[1] /= norm_s; params.s[2] /= norm_s;
85+
double norm_s = sqrt(params.s.dot(params.s));
86+
params.s = params.s / norm_s;
8787

8888
// Check f.s = 0
89-
double dot_fs = params.f[0]*params.s[0] + params.f[1]*params.s[1] + params.f[2]*params.s[2];
89+
double dot_fs = params.f.dot(params.s);
9090
if (fabs(dot_fs) > 1e-6) {
9191
std::cout << "f.s = " << dot_fs << std::endl;
9292
std::cout << "f = [" << params.f[0] << ", " << params.f[1] << ", " << params.f[2] << "]" << std::endl;

tests/unitTests/material_model_tests/test_material_holzapfel_ogden_MA.h

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,19 @@ class HolzapfelOgdenMAParams : public MatParams {
4545
double b_s;
4646
double a_fs;
4747
double b_fs;
48-
double f[3]; // Fiber direction
49-
double s[3]; // Sheet direction
48+
Vector<double> f; // Fiber direction
49+
Vector<double> s; // Sheet direction
5050

5151
double k; // Smoothed Heaviside function parameter
5252

5353
// Default constructor
5454
HolzapfelOgdenMAParams() : a(0.0), b(0.0), a_f(0.0), b_f(0.0), a_s(0.0), b_s(0.0), a_fs(0.0), b_fs(0.0), k(0.0) {
55-
for (int i = 0; i < 3; i++) {
56-
f[i] = 0.0;
57-
s[i] = 0.0;
58-
}
55+
f.resize(3);
56+
s.resize(3);
5957
}
6058

6159
// Constructor with parameters
62-
HolzapfelOgdenMAParams(double a, double b, double a_f, double b_f, double a_s, double b_s, double a_fs, double b_fs, double k, double f[3], double s[3]) : a(a), b(b), a_f(a_f), b_f(b_f), a_s(a_s), b_s(b_s), a_fs(a_fs), b_fs(b_fs), k(k) {
63-
for (int i = 0; i < 3; i++) {
64-
this->f[i] = f[i];
65-
this->s[i] = s[i];
66-
}
60+
HolzapfelOgdenMAParams(double a, double b, double a_f, double b_f, double a_s, double b_s, double a_fs, double b_fs, double k, Vector<double> f, Vector<double> s) : a(a), b(b), a_f(a_f), b_f(b_f), a_s(a_s), b_s(b_s), a_fs(a_fs), b_fs(b_fs), k(k), f(f), s(s) {
6761
}
6862
};
6963

@@ -107,10 +101,8 @@ class TestHolzapfelOgdenMA : public TestMaterialModel {
107101

108102
// Set number of fiber directions and fiber directions
109103
nFn = 2;
110-
Vector<double> f = {params.f[0], params.f[1], params.f[2]};
111-
Vector<double> s = {params.s[0], params.s[1], params.s[2]};
112-
fN.set_col(0, f);
113-
fN.set_col(1, s);
104+
fN.set_col(0, params.f);
105+
fN.set_col(1, params.s);
114106
}
115107

116108
/**
@@ -165,8 +157,8 @@ class TestHolzapfelOgdenMA : public TestMaterialModel {
165157
double k = params.k;
166158

167159
// Fiber and sheet directions
168-
Vector<double> f = {params.f[0], params.f[1], params.f[2]};
169-
Vector<double> s = {params.s[0], params.s[1], params.s[2]};
160+
Vector<double> f = params.f;
161+
Vector<double> s = params.s;
170162

171163
// Strain energy density for Holzapfel-Ogden material model
172164

0 commit comments

Comments
 (0)