-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: add volumetric: hyperelasticity example with trait (#6)
* samples: add volumetric: hyperelasticity example with trait * Update samples/tests/traits/mod.rs Co-authored-by: Jed Brown <[email protected]> * add small tolerance to test --------- Co-authored-by: Manuel Drehwald <[email protected]>
- Loading branch information
Showing
2 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |