-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmod.rs
44 lines (42 loc) · 1.38 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
samples::test! {
volumetric;
/// ANCHOR: volumetric
use std::autodiff::autodiff;
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
}