rustfront 0.1.0

Evaluate MFront material behaviours (constitutive laws) at a material point, in pure Rust, without MGIS or any C++ build dependency
Documentation
//! The errors of this crate.

use core::fmt;
use std::path::PathBuf;

use crate::Hypothesis;

/// Everything that can go wrong when loading or integrating a behaviour.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// The shared library could not be opened.
    Open {
        /// The path given to [`crate::Behaviour::load`].
        library: PathBuf,
        /// Why the system refused it.
        source: libloading::Error,
    },
    /// A symbol the behaviour must export is missing from the library: the
    /// behaviour name is wrong, or the library was not generated by MFront.
    MissingSymbol {
        /// The symbol searched for.
        symbol: String,
    },
    /// A string exported by the library is not valid UTF-8.
    InvalidString {
        /// The symbol holding the string.
        symbol: String,
    },
    /// The behaviour was generated for another interface than `generic`.
    NotGeneric {
        /// The behaviour.
        behaviour: String,
        /// The interface it was generated for.
        interface: String,
    },
    /// The version of the `generic` interface is not 1, the only one this
    /// crate knows.
    UnsupportedApiVersion {
        /// The behaviour.
        behaviour: String,
        /// The version found.
        version: u16,
    },
    /// The library was generated by a TFEL version older than 5.1.0.
    UnsupportedTfelVersion {
        /// The behaviour.
        behaviour: String,
        /// The version found.
        version: String,
    },
    /// The behaviour is not a small strain one: finite strain behaviours,
    /// cohesive zone models and general behaviours are not supported yet.
    UnsupportedBehaviourType {
        /// The behaviour.
        behaviour: String,
        /// The `_BehaviourType` symbol: 0 general, 2 finite strain, 3 cohesive
        /// zone model.
        code: u16,
    },
    /// The behaviour was not generated for the requested hypothesis.
    UnsupportedHypothesis {
        /// The behaviour.
        behaviour: String,
        /// The requested hypothesis.
        hypothesis: Hypothesis,
        /// The hypotheses the library provides.
        supported: Vec<String>,
    },
    /// A variable has a type this crate does not handle yet (arrays,
    /// dimensioned types).
    UnsupportedVariableType {
        /// The variable.
        variable: String,
        /// Its type code in MFront's encoding.
        code: i32,
    },
    /// A block of the tangent operator names a variable the behaviour does not
    /// declare.
    InvalidTangentOperatorBlock {
        /// The unknown variable.
        variable: String,
    },
    /// The string is not the name of a modelling hypothesis.
    UnknownHypothesis(String),
    /// An array given to [`crate::Behaviour::integrate`] does not have the
    /// length the behaviour expects.
    WrongLength {
        /// The array, such as `s1.internal_state_variables`.
        array: &'static str,
        /// The length the behaviour expects.
        expected: usize,
        /// The length given.
        found: usize,
    },
    /// The behaviour failed to integrate. The state at the end of the time
    /// step is meaningless.
    IntegrationFailed {
        /// The message the behaviour left, possibly empty.
        message: String,
        /// The factor by which the behaviour proposes to scale the time step
        /// before trying again.
        time_step_scaling_factor: f64,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Open { library, source } => {
                write!(f, "cannot open `{}`: {source}", library.display())
            }
            Error::MissingSymbol { symbol } => {
                write!(f, "the library does not export the symbol `{symbol}`")
            }
            Error::InvalidString { symbol } => {
                write!(f, "the symbol `{symbol}` is not a valid UTF-8 string")
            }
            Error::NotGeneric {
                behaviour,
                interface,
            } => write!(
                f,
                "`{behaviour}` was generated for the `{interface}` interface, \
                 not for the `generic` one"
            ),
            Error::UnsupportedApiVersion { behaviour, version } => write!(
                f,
                "`{behaviour}` uses version {version} of the `generic` interface, \
                 only version 1 is supported"
            ),
            Error::UnsupportedTfelVersion { behaviour, version } => write!(
                f,
                "`{behaviour}` was generated by TFEL {version}, \
                 TFEL 5.1.0 or later is required"
            ),
            Error::UnsupportedBehaviourType { behaviour, code } => write!(
                f,
                "`{behaviour}` is not a small strain behaviour (behaviour type {code}), \
                 the only kind supported yet"
            ),
            Error::UnsupportedHypothesis {
                behaviour,
                hypothesis,
                supported,
            } => write!(
                f,
                "`{behaviour}` was not generated for the `{hypothesis}` hypothesis \
                 (available: {})",
                supported.join(", ")
            ),
            Error::UnsupportedVariableType { variable, code } => write!(
                f,
                "the variable `{variable}` has a type (code {code}) \
                 that is not supported yet"
            ),
            Error::InvalidTangentOperatorBlock { variable } => write!(
                f,
                "the tangent operator names `{variable}`, which the behaviour does not declare"
            ),
            Error::UnknownHypothesis(name) => {
                write!(f, "`{name}` is not a modelling hypothesis")
            }
            Error::WrongLength {
                array,
                expected,
                found,
            } => write!(
                f,
                "`{array}` has {found} values, the behaviour expects {expected}"
            ),
            Error::IntegrationFailed {
                message,
                time_step_scaling_factor,
            } => write!(
                f,
                "integration failed (proposed time step scaling factor: \
                 {time_step_scaling_factor}): {message}"
            ),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Open { source, .. } => Some(source),
            _ => None,
        }
    }
}