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::{
14    defeat_message,
15    math::{Scalar, TensorError, TestError},
16};
17use std::fmt::{self, Debug, Display, Formatter};
18
19/// Required methods for constitutive models.
20pub trait Constitutive
21where
22    Self: Clone + Debug,
23{
24}
25
26/// Possible errors encountered in constitutive models.
27pub enum ConstitutiveError {
28    Custom(String, String),
29    InvalidJacobian(Scalar, String),
30    Upstream(String, String),
31}
32
33impl From<ConstitutiveError> for TestError {
34    fn from(error: ConstitutiveError) -> Self {
35        Self {
36            message: error.to_string(),
37        }
38    }
39}
40
41impl From<TensorError> for ConstitutiveError {
42    fn from(error: TensorError) -> Self {
43        ConstitutiveError::Custom(
44            error.to_string(),
45            "unknown (temporary error handling)".to_string(),
46        )
47    }
48}
49
50impl From<ConstitutiveError> for String {
51    fn from(error: ConstitutiveError) -> Self {
52        Self::from(&error)
53    }
54}
55
56impl From<&ConstitutiveError> for String {
57    fn from(error: &ConstitutiveError) -> Self {
58        match error {
59            ConstitutiveError::Custom(message, constitutive_model) => format!(
60                "\x1b[1;91m{message}\x1b[0;91m\n\
61                        In constitutive model: {constitutive_model}."
62            ),
63            ConstitutiveError::InvalidJacobian(jacobian, constitutive_model) => format!(
64                "\x1b[1;91mInvalid Jacobian: {jacobian:.6e}.\x1b[0;91m\n\
65                        In constitutive model: {constitutive_model}."
66            ),
67            ConstitutiveError::Upstream(error, constitutive_model) => format!(
68                "{error}\x1b[0;91m\n\
69                    In constitutive model: {constitutive_model}."
70            ),
71        }
72    }
73}
74
75impl Debug for ConstitutiveError {
76    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
77        write!(
78            f,
79            "\n{}\n\x1b[0;2;31m{}\x1b[0m\n",
80            String::from(self),
81            defeat_message()
82        )
83    }
84}
85
86impl Display for ConstitutiveError {
87    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
88        write!(f, "{}\x1b[0m", String::from(self))
89    }
90}
91
92impl PartialEq for ConstitutiveError {
93    fn eq(&self, other: &Self) -> bool {
94        match self {
95            Self::Custom(a, b) => match other {
96                Self::Custom(c, d) => a == c && b == d,
97                _ => false,
98            },
99            Self::InvalidJacobian(a, b) => match other {
100                Self::InvalidJacobian(c, d) => a == c && b == d,
101                _ => false,
102            },
103            Self::Upstream(a, b) => match other {
104                Self::Upstream(c, d) => a == c && b == d,
105                _ => false,
106            },
107        }
108    }
109}