-
Notifications
You must be signed in to change notification settings - Fork 8
test enzyme derivatives with hyperelastic neohookean potential #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #include <iostream> | ||
| #include "../tests/neohookean_common.hpp" | ||
| #include <cstdlib> | ||
| #include <cstdio> | ||
| #include <array> | ||
| #include "axom/core.hpp" | ||
| #include "axom/slic/interface/slic.hpp" | ||
| #include <cmath> | ||
| #include <limits> | ||
| #include "tribol/common/Enzyme.hpp" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like a lot of these |
||
|
|
||
| int main() | ||
| { | ||
| double E[9] = { 0.1, 0.05, 0.02, 0.05, 0.2, 0.01, 0.02, 0.01, 0.15 }; | ||
| double mu = 1.0; | ||
| double lambda = 1.0; | ||
| int N = 0; | ||
| std::cout << "Enter cycles: "; | ||
| std::cin >> N; | ||
| double dw_dE[9] = { 0.0 }; | ||
| double d2W_d2E[81] = { 0.0 }; | ||
| double h = 1e-7; | ||
|
|
||
| run_fwd_mode( E, mu, lambda, dw_dE, N ); | ||
| run_bkwd_mode( E, mu, lambda, dw_dE, N ); | ||
| run_hand_derivative( E, mu, lambda, dw_dE, N ); | ||
| run_fwd_fwd( E, mu, lambda, d2W_d2E, N ); | ||
| run_rev_fwd( E, mu, lambda, d2W_d2E, N ); | ||
| run_fwd_rev( E, mu, lambda, d2W_d2E, N ); | ||
| run_rev_rev( E, mu, lambda, d2W_d2E, N ); | ||
| run_fwd_FD( E, mu, lambda, d2W_d2E, N, h ); | ||
| run_hand_fwd( E, mu, lambda, d2W_d2E, N ); | ||
|
|
||
| return 0; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very clear and nicely written. Can you add some timing results to the PR description? |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| #include <iostream> | ||
| #include <gtest/gtest.h> | ||
| #include "neohookean_common.hpp" | ||
| #include <cstdlib> | ||
| #include <cstdio> | ||
| #include <array> | ||
| #include <cmath> | ||
| #include <limits> | ||
| #include "tribol/common/Enzyme.hpp" | ||
|
||
|
|
||
| class EnzymeNeohookeanTest : public testing::Test { | ||
| protected: | ||
| double F_all[27] = { 1.0, 0.5, 0.0, 0.0, 1.2, 0.1, 0.0, 0.0, 1.0, 1.1, 0.7, 0.1, 0.2, 1.4, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try using |
||
| 0.2, 0.02, 0.04, 1.05, 1.05, 0.2, 0.04, 0.0, 1.1, 0.14, 0.07, 0.0, 1.09 }; | ||
|
|
||
| double mu = 1.6; | ||
| double lambda = 1.2; | ||
|
|
||
| double E_all[27] = { 0.0 }; | ||
|
|
||
| double tol = 1e-6; | ||
|
|
||
| void SetUp() override | ||
| { | ||
| for ( int i = 0; i < 3; ++i ) { | ||
| calc_E_from_F( F_all + i * 9, E_all + i * 9 ); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| TEST_F( EnzymeNeohookeanTest, finite_difference_vs_exact ) | ||
| { | ||
| for ( int i = 0; i < 3; ++i ) { | ||
| double S_FD[9] = { 0.0 }; | ||
| double S_exact[9] = { 0.0 }; | ||
| stress_FD( E_all + i * 9, mu, lambda, S_FD ); | ||
| hand_code_deriv( E_all + i * 9, mu, lambda, S_exact ); | ||
| for ( int j = 0; j < 9; ++j ) { | ||
| EXPECT_NEAR( S_FD[j], S_exact[j], tol ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| TEST_F( EnzymeNeohookeanTest, finite_difference_vs_fwd_diff ) | ||
| { | ||
| for ( int i = 0; i < 3; ++i ) { | ||
| double S_FD[9] = { 0.0 }; | ||
| double S_fwddiff[9] = { 0.0 }; | ||
| stress_FD( E_all + i * 9, mu, lambda, S_FD ); | ||
| stress( E_all + i * 9, mu, lambda, S_fwddiff ); | ||
| for ( int j = 0; j < 9; ++j ) { | ||
| EXPECT_NEAR( S_FD[j], S_fwddiff[j], tol ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| TEST_F( EnzymeNeohookeanTest, finite_difference_vs_autodiff ) | ||
| { | ||
| for ( int i = 0; i < 3; ++i ) { | ||
| double S_FD[9] = { 0.0 }; | ||
| double S_autodiff[9] = { 0.0 }; | ||
| stress_FD( E_all + i * 9, mu, lambda, S_FD ); | ||
| stress_reverse( E_all + i * 9, mu, lambda, S_autodiff ); | ||
| for ( int j = 0; j < 9; ++j ) { | ||
| EXPECT_NEAR( S_FD[j], S_autodiff[j], tol ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| TEST_F( EnzymeNeohookeanTest, 2nd_deriv_finite_difference_vs_fwd_fwd ) | ||
| { | ||
| for ( int i = 0; i < 3; ++i ) { | ||
| double dW2_FD[81] = { 0.0 }; | ||
| double dW2_fwd_fwd[81] = { 0.0 }; | ||
| second_deriv_fwd_FD( E_all + i * 9, mu, lambda, dW2_FD ); | ||
| second_deriv_fwd_fwd( E_all + i * 9, mu, lambda, dW2_fwd_fwd ); | ||
| for ( int j = 0; j < 9; ++j ) { | ||
|
||
| EXPECT_NEAR( dW2_FD[j], dW2_fwd_fwd[j], tol ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| TEST_F( EnzymeNeohookeanTest, 2nd_deriv_finite_difference_vs_fwd_rev ) | ||
| { | ||
| for ( int i = 0; i < 3; ++i ) { | ||
| double dW2_FD[81] = { 0.0 }; | ||
| double dW2_fwd_rev[81] = { 0.0 }; | ||
| second_deriv_fwd_FD( E_all + i * 9, mu, lambda, dW2_FD ); | ||
| second_deriv_fwd_rev( E_all + i * 9, mu, lambda, dW2_fwd_rev ); | ||
| for ( int j = 0; j < 81; ++j ) { | ||
| EXPECT_NEAR( dW2_FD[j], dW2_fwd_rev[j], tol ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| TEST_F( EnzymeNeohookeanTest, 2nd_deriv_finite_difference_vs_rev_rev ) | ||
| { | ||
| for ( int i = 0; i < 3; ++i ) { | ||
| double dW2_FD[81] = { 0.0 }; | ||
| double dW2_rev_rev[81] = { 0.0 }; | ||
| second_deriv_fwd_FD( E_all + i * 9, mu, lambda, dW2_FD ); | ||
| second_deriv_rev_rev( E_all + i * 9, mu, lambda, dW2_rev_rev ); | ||
| for ( int j = 0; j < 81; ++j ) { | ||
| EXPECT_NEAR( dW2_FD[j], dW2_rev_rev[j], tol ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| TEST_F( EnzymeNeohookeanTest, 2nd_deriv_finite_difference_vs_rev_fwd ) | ||
| { | ||
| for ( int i = 0; i < 3; ++i ) { | ||
| double dW2_FD[81] = { 0.0 }; | ||
| double dW2_rev_fwd[81] = { 0.0 }; | ||
| second_deriv_fwd_FD( E_all + i * 9, mu, lambda, dW2_FD ); | ||
| second_deriv_rev_rev( E_all + i * 9, mu, lambda, dW2_rev_fwd ); | ||
| for ( int j = 0; j < 81; ++j ) { | ||
| EXPECT_NEAR( dW2_FD[j], dW2_rev_fwd[j], tol ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| TEST_F( EnzymeNeohookeanTest, 2nd_deriv_finite_difference_vs_hand_fwd ) | ||
| { | ||
| for ( int i = 0; i < 3; ++i ) { | ||
| double dW2_FD[81] = { 0.0 }; | ||
| double dW2_hand_fwd[81] = { 0.0 }; | ||
| second_deriv_fwd_FD( E_all + i * 9, mu, lambda, dW2_FD ); | ||
| second_deriv_hand_fwd( E_all + i * 9, mu, lambda, dW2_hand_fwd ); | ||
| for ( int j = 0; j < 9; ++j ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment why we are only looking at entries 0-8? |
||
| EXPECT_NEAR( dW2_FD[j], dW2_hand_fwd[j], tol ); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will need to turn this test off when Tribol is built without Enzyme. Try removing this from the list and adding this after:
blt_list_append(TO contact_examples ELEMENTS neohookean_timing.cpp IF TRIBOL_USE_ENZYME )