Skip to main content

conspire/constitutive/
mod.rs

1//! Constitutive model library.
2
3#[cfg(test)]
4pub mod test;
5
6pub mod cohesive;
7pub mod fluid;
8pub mod hybrid;
9pub mod multiphysics;
10pub mod solid;
11pub mod thermal;
12
13use crate::math::{Scalar, Style, StyledError, TensorError, assert::AssertionError, styled_error};
14use std::fmt::Debug;
15
16/// Required methods for constitutive models.
17pub trait Constitutive
18where
19    Self: Clone + Debug,
20{
21}
22
23/// Possible errors encountered in constitutive models.
24pub enum ConstitutiveError {
25    Custom(String, String),
26    InvalidJacobian(Scalar, String),
27    Upstream(String, String),
28}
29
30impl From<ConstitutiveError> for AssertionError {
31    fn from(error: ConstitutiveError) -> Self {
32        Self {
33            message: error.to_string(),
34        }
35    }
36}
37
38impl From<TensorError> for ConstitutiveError {
39    fn from(error: TensorError) -> Self {
40        ConstitutiveError::Custom(
41            error.to_string(),
42            "unknown (temporary error handling)".to_string(),
43        )
44    }
45}
46
47impl From<ConstitutiveError> for String {
48    fn from(error: ConstitutiveError) -> Self {
49        error.message(&Style::detect())
50    }
51}
52
53impl StyledError for ConstitutiveError {
54    fn message(&self, style: &Style) -> String {
55        let (h, c) = (style.headline, style.frame);
56        match self {
57            Self::Custom(message, constitutive_model) => format!(
58                "{h}{message}{c}\n\
59                In constitutive model: {constitutive_model}."
60            ),
61            Self::InvalidJacobian(jacobian, constitutive_model) => format!(
62                "{h}Invalid Jacobian: {jacobian:.6e}.{c}\n\
63                In constitutive model: {constitutive_model}."
64            ),
65            Self::Upstream(error, constitutive_model) => format!(
66                "{error}{c}\n\
67                In constitutive model: {constitutive_model}."
68            ),
69        }
70    }
71}
72
73styled_error!(ConstitutiveError);
74
75impl PartialEq for ConstitutiveError {
76    fn eq(&self, other: &Self) -> bool {
77        match self {
78            Self::Custom(a, b) => match other {
79                Self::Custom(c, d) => a == c && b == d,
80                _ => false,
81            },
82            Self::InvalidJacobian(a, b) => match other {
83                Self::InvalidJacobian(c, d) => a == c && b == d,
84                _ => false,
85            },
86            Self::Upstream(a, b) => match other {
87                Self::Upstream(c, d) => a == c && b == d,
88                _ => false,
89            },
90        }
91    }
92}