conspire/constitutive/
mod.rs

1//! Constitutive model library.
2
3#[cfg(test)]
4pub mod test;
5
6pub mod fluid;
7pub mod hybrid;
8pub mod multiphysics;
9pub mod solid;
10pub mod thermal;
11
12use crate::{
13    defeat_message,
14    math::{Scalar, TensorError, TestError},
15};
16use std::fmt::{self, Debug, Display, Formatter};
17
18/// Required methods for constitutive models.
19pub trait Constitutive
20where
21    Self: Clone + Debug,
22{
23}
24
25/// Possible errors encountered in constitutive models.
26pub enum ConstitutiveError {
27    Custom(String, String),
28    InvalidJacobian(Scalar, String),
29    Upstream(String, String),
30}
31
32impl From<ConstitutiveError> for String {
33    fn from(error: ConstitutiveError) -> Self {
34        match error {
35            ConstitutiveError::Custom(message, constitutive_model) => format!(
36                "\x1b[1;91m{message}\x1b[0;91m\n\
37                        In constitutive model: {constitutive_model}."
38            ),
39            ConstitutiveError::InvalidJacobian(jacobian, constitutive_model) => format!(
40                "\x1b[1;91mInvalid Jacobian: {jacobian:.6e}.\x1b[0;91m\n\
41                        In constitutive model: {constitutive_model}."
42            ),
43            ConstitutiveError::Upstream(error, constitutive_model) => format!(
44                "{error}\x1b[0;91m\n\
45                    In constitutive model: {constitutive_model}."
46            ),
47        }
48    }
49}
50
51impl From<ConstitutiveError> for TestError {
52    fn from(error: ConstitutiveError) -> Self {
53        Self {
54            message: error.to_string(),
55        }
56    }
57}
58
59impl From<TensorError> for ConstitutiveError {
60    fn from(error: TensorError) -> Self {
61        ConstitutiveError::Custom(
62            error.to_string(),
63            "unknown (temporary error handling)".to_string(),
64        )
65    }
66}
67
68impl Debug for ConstitutiveError {
69    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
70        let error = match self {
71            Self::Custom(message, constitutive_model) => format!(
72                "\x1b[1;91m{message}\x1b[0;91m\n\
73                 In constitutive model: {constitutive_model}."
74            ),
75            Self::InvalidJacobian(jacobian, constitutive_model) => {
76                format!(
77                    "\x1b[1;91mInvalid Jacobian: {jacobian:.6e}.\x1b[0;91m\n\
78                    In constitutive model: {constitutive_model}."
79                )
80            }
81            Self::Upstream(error, constitutive_model) => {
82                format!(
83                    "{error}\x1b[0;91m\n\
84                    In constitutive model: {constitutive_model}."
85                )
86            }
87        };
88        write!(f, "\n{}\n\x1b[0;2;31m{}\x1b[0m\n", error, defeat_message())
89    }
90}
91
92impl Display for ConstitutiveError {
93    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
94        let error = match self {
95            Self::Custom(message, constitutive_model) => format!(
96                "\x1b[1;91m{message}\x1b[0;91m\n\
97                 In constitutive model: {constitutive_model}."
98            ),
99            Self::InvalidJacobian(jacobian, constitutive_model) => {
100                format!(
101                    "\x1b[1;91mInvalid Jacobian: {jacobian:.6e}.\x1b[0;91m\n\
102                    In constitutive model: {constitutive_model}."
103                )
104            }
105            Self::Upstream(error, constitutive_model) => {
106                format!(
107                    "{error}\x1b[0;91m\n\
108                    In constitutive model: {constitutive_model}."
109                )
110            }
111        };
112        write!(f, "{error}\x1b[0m")
113    }
114}
115
116impl PartialEq for ConstitutiveError {
117    fn eq(&self, other: &Self) -> bool {
118        match self {
119            Self::Custom(a, b) => match other {
120                Self::Custom(c, d) => a == c && b == d,
121                _ => false,
122            },
123            Self::InvalidJacobian(a, b) => match other {
124                Self::InvalidJacobian(c, d) => a == c && b == d,
125                _ => false,
126            },
127            Self::Upstream(a, b) => match other {
128                Self::Upstream(c, d) => a == c && b == d,
129                _ => false,
130            },
131        }
132    }
133}