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 String {
34    fn from(error: ConstitutiveError) -> Self {
35        match error {
36            ConstitutiveError::Custom(message, constitutive_model) => format!(
37                "\x1b[1;91m{message}\x1b[0;91m\n\
38                        In constitutive model: {constitutive_model}."
39            ),
40            ConstitutiveError::InvalidJacobian(jacobian, constitutive_model) => format!(
41                "\x1b[1;91mInvalid Jacobian: {jacobian:.6e}.\x1b[0;91m\n\
42                        In constitutive model: {constitutive_model}."
43            ),
44            ConstitutiveError::Upstream(error, constitutive_model) => format!(
45                "{error}\x1b[0;91m\n\
46                    In constitutive model: {constitutive_model}."
47            ),
48        }
49    }
50}
51
52impl From<ConstitutiveError> for TestError {
53    fn from(error: ConstitutiveError) -> Self {
54        Self {
55            message: error.to_string(),
56        }
57    }
58}
59
60impl From<TensorError> for ConstitutiveError {
61    fn from(error: TensorError) -> Self {
62        ConstitutiveError::Custom(
63            error.to_string(),
64            "unknown (temporary error handling)".to_string(),
65        )
66    }
67}
68
69impl Debug for ConstitutiveError {
70    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
71        let error = match self {
72            Self::Custom(message, constitutive_model) => format!(
73                "\x1b[1;91m{message}\x1b[0;91m\n\
74                 In constitutive model: {constitutive_model}."
75            ),
76            Self::InvalidJacobian(jacobian, constitutive_model) => {
77                format!(
78                    "\x1b[1;91mInvalid Jacobian: {jacobian:.6e}.\x1b[0;91m\n\
79                    In constitutive model: {constitutive_model}."
80                )
81            }
82            Self::Upstream(error, constitutive_model) => {
83                format!(
84                    "{error}\x1b[0;91m\n\
85                    In constitutive model: {constitutive_model}."
86                )
87            }
88        };
89        write!(f, "\n{}\n\x1b[0;2;31m{}\x1b[0m\n", error, defeat_message())
90    }
91}
92
93impl Display for ConstitutiveError {
94    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
95        let error = match self {
96            Self::Custom(message, constitutive_model) => format!(
97                "\x1b[1;91m{message}\x1b[0;91m\n\
98                 In constitutive model: {constitutive_model}."
99            ),
100            Self::InvalidJacobian(jacobian, constitutive_model) => {
101                format!(
102                    "\x1b[1;91mInvalid Jacobian: {jacobian:.6e}.\x1b[0;91m\n\
103                    In constitutive model: {constitutive_model}."
104                )
105            }
106            Self::Upstream(error, constitutive_model) => {
107                format!(
108                    "{error}\x1b[0;91m\n\
109                    In constitutive model: {constitutive_model}."
110                )
111            }
112        };
113        write!(f, "{error}\x1b[0m")
114    }
115}
116
117impl PartialEq for ConstitutiveError {
118    fn eq(&self, other: &Self) -> bool {
119        match self {
120            Self::Custom(a, b) => match other {
121                Self::Custom(c, d) => a == c && b == d,
122                _ => false,
123            },
124            Self::InvalidJacobian(a, b) => match other {
125                Self::InvalidJacobian(c, d) => a == c && b == d,
126                _ => false,
127            },
128            Self::Upstream(a, b) => match other {
129                Self::Upstream(c, d) => a == c && b == d,
130                _ => false,
131            },
132        }
133    }
134}