rustfront 0.7.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,
//! from the directory of the crate (`rustfront/` in the repository):
//!
//! ```text
//! mfront --obuild --interface=generic tests/behaviours/Elasticity.mfront
//! cargo run --example minimal -- src/libBehaviour.so
//! ```

use rustfront::voigt::{self, Shear};
use rustfront::{
    Behaviour, BehaviourData, FinalState, Hypothesis, InitialState, IntegrationOptions, Settings,
};

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: the variables of each
    // array of the material point, in their order, and where they sit.
    let d = behaviour.description();
    println!("{} (TFEL {}, {})", d.name, d.tfel_version, d.hypothesis);
    for (kind, variables) in [
        ("gradient", &d.gradients),
        ("thermodynamic force", &d.thermodynamic_forces),
        ("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} offset {}, {} value(s)",
                v.name, v.offset, v.size
            );
        }
    }

    // The arrays of the material point belong to the caller, in the order of
    // the description.
    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();
    // Symmetric tensors follow TFEL's convention: xx, yy, zz, √2·xy, √2·xz,
    // √2·yz. The strain at the end of the step is written in Voigt notation,
    // xx, yy, zz, yz, xz, xy, with an engineering shear γxy = 2·εxy.
    let strain0 = [0.0; 6];
    let mut strain1 = [0.0; 6];
    voigt::to_tfel(
        &[1e-3, 0.0, 0.0, 0.0, 0.0, 1e-3],
        Shear::Engineering,
        &mut strain1,
    );
    let stress0 = [0.0; 6];
    let mut stress1 = [0.0; 6];
    let mut tangent = vec![0.0; d.tangent_operator_size];

    // The data of the integration, as MGIS's BehaviourDataView: the time
    // step, the factor by which the behaviour may scale it, the tangent
    // operator and the speed of sound it writes, the two states.
    let mut data = BehaviourData {
        dt: 1.0,
        rdt: 1.0,
        k: &mut tangent,
        speed_of_sound: 0.0,
        s0: InitialState {
            gradients: &strain0,
            thermodynamic_forces: &stress0,
            material_properties: &material,
            internal_state_variables: &isv0,
            external_state_variables: &temperature,
            ..Default::default()
        },
        s1: FinalState {
            gradients: &strain1,
            thermodynamic_forces: &mut stress1,
            material_properties: &material,
            internal_state_variables: &mut isv1,
            external_state_variables: &temperature,
            ..Default::default()
        },
    };
    // The integration and the consistent tangent operator, by default.
    let status = behaviour.integrate(&mut data, IntegrationOptions::default())?;
    println!(
        "integrated: {status:?}, time step scaling factor {}, stored energy {:.4e}",
        data.rdt, data.s1.stored_energy
    );

    // 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(" ")
    );
    let seq = d
        .internal_state_variables
        .list
        .iter()
        .find(|v| v.name == "VonMisesStress")
        .ok_or("no VonMisesStress")?;
    println!("von Mises stress        {:.4e}", isv1[seq.offset]);
    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(())
}