rustfront 0.4.0

Evaluate MFront material behaviours (constitutive laws) at a material point, in pure Rust, without MGIS or any C++ build dependency
Documentation
//! The smallest use of rustfront: load a behaviour, integrate it once at one
//! material point, print what comes out.
//!
//! Compile the test behaviours first, then give the library to the example:
//!
//! ```text
//! mfront --obuild --interface=generic tests/behaviours/Elasticity.mfront
//! cargo run --example minimal -- src/libBehaviour.so
//! ```

use rustfront::voigt::{self, Shear};
use rustfront::{
    Behaviour, FinalState, Hypothesis, InitialState, Settings, TangentOperator, TimeStep,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let library = std::env::args()
        .nth(1)
        .ok_or("usage: minimal <path to the library generated by mfront>")?;

    // Loading is the only unsafe step: opening a library runs its code.
    // SAFETY: the library was generated by MFront with the `generic` interface.
    let behaviour = unsafe {
        Behaviour::load(
            &library,
            "Elasticity",
            Hypothesis::Tridimensional,
            &Settings::default(),
        )?
    };

    // The description tells what the behaviour expects, and in which order.
    let d = behaviour.description();
    println!("{} (TFEL {}, {})", d.name, d.tfel_version, d.hypothesis);
    for (kind, variables) in [
        ("gradient", &d.gradients),
        ("material property", &d.material_properties),
        ("internal state variable", &d.internal_state_variables),
        ("external state variable", &d.external_state_variables),
    ] {
        for v in &variables.list {
            println!("  {kind:<24} {:<16} {} value(s)", v.name, v.size);
        }
    }

    // The arrays of the material point belong to the caller. Symmetric
    // tensors follow TFEL's convention: xx, yy, zz, √2·xy, √2·xz, √2·yz.
    let material = [200e9, 0.3]; // YoungModulus, PoissonRatio
    let temperature = [293.15];
    let isv0 = vec![0.0; d.internal_state_variables.size];
    let mut isv1 = isv0.clone();
    let strain0 = [0.0; 6];
    let strain1 = [1e-3, 0.0, 0.0, 0.0, 0.0, 0.0];
    let stress0 = [0.0; 6];
    let mut stress1 = [0.0; 6];
    let mut tangent = vec![0.0; d.tangent_operator_size];

    let s0 = InitialState {
        gradients: &strain0,
        thermodynamic_forces: &stress0,
        material_properties: &material,
        internal_state_variables: &isv0,
        external_state_variables: &temperature,
        ..Default::default()
    };
    let mut s1 = FinalState {
        gradients: &strain1,
        thermodynamic_forces: &mut stress1,
        material_properties: &material,
        internal_state_variables: &mut isv1,
        external_state_variables: &temperature,
    };
    let operator = Some((TangentOperator::ConsistentTangent, tangent.as_mut_slice()));
    let result = behaviour.integrate(
        TimeStep {
            dt: 1.0,
            max_scaling_factor: 1.0,
        },
        &s0,
        &mut s1,
        operator,
    )?;
    println!(
        "integrated, time step scaling factor {}",
        result.time_step_scaling_factor
    );

    // Back to the standard Voigt notation: xx, yy, zz, yz, xz, xy.
    let mut stress = [0.0; 6];
    voigt::from_tfel(&stress1, Shear::Tensor, &mut stress);
    println!(
        "stress (Voigt)          {}",
        stress.map(|s| format!("{s:.4e}")).join(" ")
    );
    println!("von Mises stress        {:.4e}", isv1[0]);
    let mut stiffness = [0.0; 36];
    voigt::tangent_from_tfel(&tangent, Shear::Engineering, &mut stiffness);
    println!("tangent (Voigt, engineering shears)");
    for row in stiffness.chunks(6) {
        let row: Vec<_> = row.iter().map(|v| format!("{v:10.3e}")).collect();
        println!("  {}", row.join(" "));
    }
    Ok(())
}