1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! 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,
}
}
}