Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
103 changes: 103 additions & 0 deletions src/examples/neohookean_timing.cpp
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"
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


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;
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
122 changes: 122 additions & 0 deletions src/tests/enzyme_neohookean.cpp
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,
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 };
//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 ) {
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