Skip to content

Commit

Permalink
samples: add volumetric: hyperelasticity example with trait (#6)
Browse files Browse the repository at this point in the history
* samples: add volumetric: hyperelasticity example with trait

* Update samples/tests/traits/mod.rs

Co-authored-by: Jed Brown <jed@jedbrown.org>

* add small tolerance to test

---------

Co-authored-by: Manuel Drehwald <git@manuel.drehwald.info>
  • Loading branch information
jedbrown and ZuseZ4 authored Apr 11, 2024
1 parent 76383f5 commit cb027ff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
5 changes: 3 additions & 2 deletions samples/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#![feature(autodiff)]

mod forward;
mod neohookean;
mod reverse;
mod higher;

mod higher;
mod neohookean;
mod traits;
43 changes: 43 additions & 0 deletions samples/tests/traits/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
samples::test! {
volumetric;
/// ANCHOR: volumetric
trait Volumetric {
/// Strain energy density
fn psi(&self, j: f64) -> f64;
/// Derivative of strain energy with respect to $J$
fn d_psi(&self, j: f64, b_psi: f64) -> (f64, f64);
/// The volumetric contribution to the second Piola-Kirchhoff stress is
/// a scalar multiplied by $C^{-1}$ where $C = I + 2E$ in terms of the
/// Green-Lagrange strain $E$. The derivative of $J$ with respect to $E$
/// is $J C^{-1}$. We'll call the volumetric contribution that scalar
/// multiple of $C^{-1}$.
fn stress(&self, j: f64) -> f64 {
let (_, d_psi) = self.d_psi(j, 1.0);
d_psi * j
}
}

struct Ogden {
k: f64,
}
impl Ogden {
pub fn stress_analytic(&self, j: f64) -> f64 {
self.k * 0.5 * (j * j - 1.0)
}
}
impl Volumetric for Ogden {
#[autodiff(d_psi, Reverse, Const, Active, Active)]
fn psi(&self, j: f64) -> f64 {
self.k * 0.25 * (j * j - 1.0 - 2.0 * j.ln())
}
}

fn main() {
let j = 0.8;
let vol = Ogden { k: 1.0 };
let s = vol.stress(j);
let s_ref = vol.stress_analytic(j);
assert!((s - s_ref).abs() < 1e-15, "{}", s - s_ref);
}
// ANCHOR_END: volumetric
}

0 comments on commit cb027ff

Please sign in to comment.