-
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 all 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,103 @@ | ||
| #include <iostream> | ||
| #include "../tests/neohookean_common.hpp" | ||
| #include <cstdlib> | ||
| #include <cstdio> | ||
| #include "axom/core.hpp" | ||
| #include "axom/slic/interface/slic.hpp" | ||
| #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 |
||
|
|
||
| template <typename TangentFn> | ||
| void do_dWdE_timing( double* E, double mu, double lambda, int N, TangentFn&& tangent_fn) | ||
| { | ||
| double S[9] = {0.0}; | ||
| axom::utilities::Timer timer{ false }; | ||
| tangent_fn(E, mu, lambda, S); | ||
|
|
||
|
|
||
| timer.start(); | ||
| for(int i = 0; i < N; ++i) { | ||
| tangent_fn(E, mu, lambda, S); | ||
| // for(int j = 0; j < 9; ++j) { | ||
| // Dw[j] += S[j]; | ||
| // } | ||
| } | ||
| timer.stop(); | ||
| std::cout << axom::fmt::format( "{:f}ms", timer.elapsedTimeInMilliSec() ) << std::endl; | ||
| } | ||
|
|
||
| template <typename TangentFn> | ||
| void do_dSdE_timing( double* E, double mu, double lambda, int N, TangentFn&& tangent_fn) | ||
| { | ||
| double S[81] = {0.0}; | ||
| axom::utilities::Timer timer{ false }; | ||
| tangent_fn(E, mu, lambda, S); | ||
|
|
||
|
|
||
| timer.start(); | ||
| for(int i = 0; i < N; ++i) { | ||
| tangent_fn(E, mu, lambda, S); | ||
| // for(int j = 0; j < 9; ++j) { | ||
| // Dw[j] += S[j]; | ||
| // } | ||
| } | ||
| timer.stop(); | ||
| std::cout << axom::fmt::format( "{:f}ms", timer.elapsedTimeInMilliSec() ) << std::endl; | ||
| } | ||
|
|
||
|
|
||
| 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; | ||
| std::cout << "Time to calc stress with fwddiff: "; | ||
|
|
||
| do_dWdE_timing(E, mu, lambda, N, [](double * E, double mu, double lambda, double* S) { | ||
| stress(E, mu, lambda, S); | ||
| }); | ||
|
|
||
| std::cout << "Time to calc stress with autodiff: "; | ||
| do_dWdE_timing(E, mu, lambda, N, [](double * E, double mu, double lambda, double* S) { | ||
| stress_reverse(E, mu, lambda, S); | ||
| }); | ||
|
|
||
| std::cout << "Time to calc stress with hand coded deriv: "; | ||
| do_dWdE_timing(E, mu, lambda, N, [](double * E, double mu, double lambda, double* S) { | ||
| hand_code_deriv(E, mu, lambda, S); | ||
| }); | ||
|
|
||
| std::cout << "Time to calc 2nd deriv with fwd_fwd: "; | ||
| do_dSdE_timing(E, mu, lambda, N, [](double * E, double mu, double lambda, double* S) { | ||
| second_deriv_fwd_fwd(E, mu, lambda, S); | ||
| }); | ||
|
|
||
| std::cout << "Time to calc 2nd deriv with rev_fwd: "; | ||
| do_dSdE_timing(E, mu, lambda, N, [](double * E, double mu, double lambda, double* S) { | ||
| second_deriv_rev_fwd(E, mu, lambda, S); | ||
| }); | ||
|
|
||
| std::cout << "Time to calc 2nd deriv with fwd_rev: "; | ||
| do_dSdE_timing(E, mu, lambda, N, [](double * E, double mu, double lambda, double* S) { | ||
| second_deriv_fwd_rev(E, mu, lambda, S); | ||
| }); | ||
|
|
||
| std::cout << "Time to calc 2nd deriv with rev_rev: "; | ||
| do_dSdE_timing(E, mu, lambda, N, [](double * E, double mu, double lambda, double* S) { | ||
| second_deriv_rev_rev(E, mu, lambda, S); | ||
| }); | ||
|
|
||
| std::cout << "Time to calc 2nd deriv with fwd_FD: "; | ||
| do_dSdE_timing(E, mu, lambda, N, [](double * E, double mu, double lambda, double* S) { | ||
| second_deriv_fwd_FD(E, mu, lambda, S); | ||
| }); | ||
|
|
||
| std::cout << "Time to calc 2nd deriv with hand_fwd: "; | ||
| do_dSdE_timing(E, mu, lambda, N, [](double * E, double mu, double lambda, double* S) { | ||
| second_deriv_hand_fwd(E, mu, lambda, S); | ||
| }); | ||
|
|
||
| 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,122 @@ | ||
| #include <iostream> | ||
| #include <gtest/gtest.h> | ||
| #include "neohookean_common.hpp" | ||
| #include <cstdlib> | ||
| #include <cstdio> | ||
|
|
||
|
|
||
| class EnzymeNeohookeanTest : public testing::Test { | ||
| protected: | ||
| //clang-format off | ||
| 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 }; | ||
| //clang-format on | ||
| 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 ); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| template <typename StressFn> | ||
| void test_stress( double* E_all, double mu, double lambda, double tol, StressFn&& stress_test_fn) | ||
| { | ||
| for ( int i = 0; i < 3; ++i ) { | ||
| double S_FD[9] = { 0.0 }; | ||
| double S_test[9] = { 0.0 }; | ||
| stress_FD( E_all + i * 9, mu, lambda, S_FD ); | ||
| stress_test_fn( E_all + i * 9, mu, lambda, S_test ); | ||
| for ( int j = 0; j < 9; ++j ) { | ||
| EXPECT_NEAR( S_FD[j], S_test[j], tol ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| template <typename StressFn> | ||
| void test_stress_2nd_deriv( double* E_all, double mu, double lambda, double tol, StressFn&& deriv2_test_fn) | ||
| { | ||
| for ( int i = 0; i < 3; ++i ) { | ||
| double dW2_FD[81] = { 0.0 }; | ||
| double dW2_test[81] = { 0.0 }; | ||
| second_deriv_fwd_FD( E_all + i * 9, mu, lambda, dW2_FD ); | ||
| deriv2_test_fn( E_all + i * 9, mu, lambda, dW2_test ); | ||
| for ( int j = 0; j < 81; ++j ) { | ||
| EXPECT_NEAR( dW2_FD[j], dW2_test[j], tol ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| TEST_F( EnzymeNeohookeanTest, finite_difference_vs_exact ) | ||
| { | ||
| test_stress(E_all, mu, lambda, tol, [](double* E, double mu, double lambda, double* S) { | ||
| hand_code_deriv(E, mu, lambda, S); | ||
| }); | ||
| } | ||
|
|
||
| TEST_F( EnzymeNeohookeanTest, finite_difference_vs_fwd_diff ) | ||
| { | ||
| test_stress(E_all, mu, lambda, tol, [](double* E, double mu, double lambda, double* S) { | ||
| stress(E, mu, lambda, S); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| TEST_F( EnzymeNeohookeanTest, finite_difference_vs_autodiff ) | ||
| { | ||
| test_stress(E_all, mu, lambda, tol, [](double* E, double mu, double lambda, double* S) { | ||
| stress_reverse(E, mu, lambda, S); | ||
| }); | ||
| } | ||
|
|
||
| TEST_F( EnzymeNeohookeanTest, 2nd_deriv_finite_difference_vs_fwd_fwd ) | ||
| { | ||
| test_stress_2nd_deriv(E_all, mu, lambda, tol, [](double* E, double mu, double lambda, double* S) { | ||
| second_deriv_fwd_fwd(E, mu, lambda, S); | ||
| }); | ||
| } | ||
|
|
||
| TEST_F( EnzymeNeohookeanTest, 2nd_deriv_finite_difference_vs_fwd_rev ) | ||
| { | ||
| test_stress_2nd_deriv(E_all, mu, lambda, tol, [](double* E, double mu, double lambda, double* S) { | ||
| second_deriv_fwd_rev(E, mu, lambda, S); | ||
| }); | ||
| } | ||
|
|
||
| TEST_F( EnzymeNeohookeanTest, 2nd_deriv_finite_difference_vs_rev_rev ) | ||
| { | ||
| test_stress_2nd_deriv(E_all, mu, lambda, tol, [](double* E, double mu, double lambda, double* S) { | ||
| second_deriv_rev_rev(E, mu, lambda, S); | ||
| }); | ||
| } | ||
|
|
||
| TEST_F( EnzymeNeohookeanTest, 2nd_deriv_finite_difference_vs_rev_fwd ) | ||
| { | ||
| test_stress_2nd_deriv(E_all, mu, lambda, tol, [](double* E, double mu, double lambda, double* S) { | ||
| second_deriv_rev_fwd(E, mu, lambda, S); | ||
| }); | ||
| } | ||
|
|
||
| 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 )