Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set( contact_examples
common_plane_gpu.cpp
common_plane.cpp
mortar_lm_patch_test.cpp
neohookean_timing.cpp
Copy link
Member

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 )

)


Expand Down
35 changes: 35 additions & 0 deletions src/examples/neohookean_timing.cpp
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a lot of these #includes aren't needed anymore. Try taking out as many as you can


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;
Copy link
Member

Choose a reason for hiding this comment

The 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?

}
1 change: 1 addition & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ if( TRIBOL_USE_ENZYME )
tribol_enzyme_nodal_normal.cpp
tribol_enzyme_mortar_assembled.cpp
tribol_enzyme_poly_intersect.cpp
enzyme_neohookean.cpp
)

set(combined_test_depends tribol gtest)
Expand Down
133 changes: 133 additions & 0 deletions src/tests/enzyme_neohookean.cpp
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See if we can clean up some of these #includes as well.


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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try using //clang-format off and //clang-format on to keep the size 3x9. See this as an example.

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 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the for loop go to 81? Let's chat about writing a template function or using std::function to reduce this code duplication

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 ) {
Copy link
Member

Choose a reason for hiding this comment

The 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 );
}
}
}
Loading
Loading