pyrucast 0.11.0

Finite-element library in Rust, inspired by cast3m, exposed to Python
Documentation
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! Damage laws — identity, contract, and what they share.
//!
//! `<physique>/law.rs` : the enum an archive stores, the trait the physics
//! calls, and the data both need. The physics itself is one level up in
//! `damage.rs`; each law sits in its own file next door. Same shape as
//! `plasticity/law.rs`.

use crate::error::Result;
use crate::models::continuum::material::MatRead;
use crate::models::plasticity::law::MAX_INTERNAL_VARS;
use serde::{Deserialize, Serialize};
/// Which damage law a [`Damage`](super::Damage) sub-model obeys.
///
/// The same attribute pattern as [`PlasticLaw`](crate::models::plasticity::law::PlasticLaw):
/// the DOFs, the elastic operator and the incremental montage are shared, and
/// only the law that turns a strain into a degraded stress differs.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::SubElementField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::damage::{self};
/// # use pyrucast::models::continuum::material::MatRead;
/// # use pyrucast::models::damage::law::{self, DamageLaw};
/// # use pyrucast::models::continuum::elastic;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let materiau = SubElementField::from_uniform_per_component(
/// #     fes.get(0).unwrap(), vec!["E".into(), "nu".into(), "eps_d0".into(), "A_t".into(), "B_t".into(), "A_c".into(), "B_c".into()], &[30000.0, 0.2, 0.0001, 0.8, 20000.0, 1.4, 1850.0]).unwrap();
/// # let idx_mat: Vec<u32> = (0..materiau.point_values(0, 0).unwrap().len() as u32).collect();
/// # let opt_mat = [pyrucast::containers::field::ABSENT_COMPONENT; 8];
/// # let mat = MatRead::new(materiau.point_values(0, 0).unwrap(), &idx_mat, &[]);
/// // Same pattern as `PlasticLaw`: the DOFs, the elastic operator and the
/// // incremental setup are shared; only the degrading law differs
/// // la contrainte.
/// assert_eq!(DamageLaw::ALL.len(), 3);
/// assert_eq!(DamageLaw::Mazars.internal_names(), vec!["kappa".to_string()]);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum DamageLaw {
    /// Mazars — one scalar, two branches blended. Concrete.
    #[default]
    Mazars,
    /// Two damages, tension and compression apart — recovers the compressive
    /// stiffness when a crack closes.
    DamageTc,
    /// Orthotropic damage of a woven ceramic-matrix composite, one damage per
    /// weave direction.
    SicSic,
}

/// The **direct-update** family of constitutive laws: strain and previous state
/// in, new state out, with no elastic predictor in between.
///
/// The trait is named after that **integration structure**, not after a physical
/// family, because the structure is what fixes its signature. Damage laws live
/// here today; a viscoelastic law — state, `dt`, a tangent, but no return
/// mapping — would join them. Its siblings are
/// [`StatelessLawKind`](crate::models::elasticity::law::StatelessLawKind)
/// (`σ = f(ε)`, no state at all) and
/// [`ReturnMapLawKind`](crate::models::plasticity::law::ReturnMapLawKind)
/// (predictor plus projection).
///
/// Same division of labour as [`SubModelKind`] one level up: the enum
/// [`DamageLaw`] carries the **physical identity** — what an archive stores —
/// and the trait carries the **integration structure**, so a single `match`
/// (`DamageLaw::as_law`) relates the two.
///
/// Adding a law: a unit struct and its `impl` in the law's own file, plus one
/// arm in `as_law`.
pub(crate) trait DirectUpdateLawKind: Sync {
    /// The material components the law reads. `space_dim` matters for a law
    /// whose orthotropy has a different count in plane and in space.
    fn material_components(&self, space_dim: usize) -> &'static [&'static str];

    /// Advance the law by one strain increment, for one Gauss point.
    fn update(
        &self,
        eps: &[f64; 6],
        prev: &[f64],
        mat: &MatRead,
        space_dim: usize,
    ) -> Result<DamageUpdate>;

    /// The law's internal variables, beyond the reported `damage`.
    fn internal_names(&self) -> Vec<String>;
}

impl DamageLaw {
    /// The behaviour behind this identity — **the only `match` per law**.
    pub(crate) fn as_law(self) -> &'static dyn DirectUpdateLawKind {
        match self {
            Self::Mazars => &super::mazars::Mazars,
            Self::DamageTc => &super::damage_tc::DamageTc,
            Self::SicSic => &super::sic_sic::SicSic,
        }
    }

    /// The lowercase name (the inverse of
    /// [`from_name`](crate::named::Named::from_name)).
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::element_field::SubElementField;
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::damage::{self};
    /// # use pyrucast::models::continuum::material::MatRead;
    /// # use pyrucast::models::damage::law::{self, DamageLaw};
    /// # use pyrucast::models::continuum::elastic;
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let materiau = SubElementField::from_uniform_per_component(
    /// #     fes.get(0).unwrap(), vec!["E".into(), "nu".into(), "eps_d0".into(), "A_t".into(), "B_t".into(), "A_c".into(), "B_c".into()], &[30000.0, 0.2, 0.0001, 0.8, 20000.0, 1.4, 1850.0]).unwrap();
    /// # let idx_mat: Vec<u32> = (0..materiau.point_values(0, 0).unwrap().len() as u32).collect();
    /// # let opt_mat = [pyrucast::containers::field::ABSENT_COMPONENT; 8];
    /// # let mat = MatRead::new(materiau.point_values(0, 0).unwrap(), &idx_mat, &[]);
    /// # use pyrucast::named::Named;
    /// // The exact inverse of `from_name`, for the three laws.
    /// assert!(DamageLaw::ALL.iter()
    ///     .all(|l| DamageLaw::from_name(l.name()) == Some(*l)));
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn name(self) -> &'static str {
        match self {
            Self::Mazars => "mazars",
            Self::DamageTc => "damage_tc",
            Self::SicSic => "sic_sic",
        }
    }

    /// Every law, in declaration order.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::element_field::SubElementField;
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::damage::{self};
    /// # use pyrucast::models::continuum::material::MatRead;
    /// # use pyrucast::models::damage::law::{self, DamageLaw};
    /// # use pyrucast::models::continuum::elastic;
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let materiau = SubElementField::from_uniform_per_component(
    /// #     fes.get(0).unwrap(), vec!["E".into(), "nu".into(), "eps_d0".into(), "A_t".into(), "B_t".into(), "A_c".into(), "B_c".into()], &[30000.0, 0.2, 0.0001, 0.8, 20000.0, 1.4, 1850.0]).unwrap();
    /// # let idx_mat: Vec<u32> = (0..materiau.point_values(0, 0).unwrap().len() as u32).collect();
    /// # let opt_mat = [pyrucast::containers::field::ABSENT_COMPONENT; 8];
    /// # let mat = MatRead::new(materiau.point_values(0, 0).unwrap(), &idx_mat, &[]);
    /// assert_eq!(DamageLaw::ALL, [DamageLaw::Mazars, DamageLaw::DamageTc, DamageLaw::SicSic]);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub const ALL: [DamageLaw; 3] = [Self::Mazars, Self::DamageTc, Self::SicSic];

    /// The material components this law requires.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::element_field::SubElementField;
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::damage::{self};
    /// # use pyrucast::models::continuum::material::MatRead;
    /// # use pyrucast::models::damage::law::{self, DamageLaw};
    /// # use pyrucast::models::continuum::elastic;
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let materiau = SubElementField::from_uniform_per_component(
    /// #     fes.get(0).unwrap(), vec!["E".into(), "nu".into(), "eps_d0".into(), "A_t".into(), "B_t".into(), "A_c".into(), "B_c".into()], &[30000.0, 0.2, 0.0001, 0.8, 20000.0, 1.4, 1850.0]).unwrap();
    /// # let idx_mat: Vec<u32> = (0..materiau.point_values(0, 0).unwrap().len() as u32).collect();
    /// # let opt_mat = [pyrucast::containers::field::ABSENT_COMPONENT; 8];
    /// # let mat = MatRead::new(materiau.point_values(0, 0).unwrap(), &idx_mat, &[]);
    /// // Mazars: one threshold and two branches. SiC/SiC also carries the
    /// // weaving axes, hence **more components in 3-D than in 2-D**.
    /// assert!(DamageLaw::Mazars.material_components(2).contains(&"eps_d0"));
    /// assert!(DamageLaw::SicSic.material_components(3).len()
    ///         > DamageLaw::SicSic.material_components(2).len());
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn material_components(self, space_dim: usize) -> &'static [&'static str] {
        self.as_law().material_components(space_dim)
    }

    /// The law's internal variables, beyond the reported `damage`.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::element_field::SubElementField;
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::damage::{self};
    /// # use pyrucast::models::continuum::material::MatRead;
    /// # use pyrucast::models::damage::law::{self, DamageLaw};
    /// # use pyrucast::models::continuum::elastic;
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let materiau = SubElementField::from_uniform_per_component(
    /// #     fes.get(0).unwrap(), vec!["E".into(), "nu".into(), "eps_d0".into(), "A_t".into(), "B_t".into(), "A_c".into(), "B_c".into()], &[30000.0, 0.2, 0.0001, 0.8, 20000.0, 1.4, 1850.0]).unwrap();
    /// # let idx_mat: Vec<u32> = (0..materiau.point_values(0, 0).unwrap().len() as u32).collect();
    /// # let opt_mat = [pyrucast::containers::field::ABSENT_COMPONENT; 8];
    /// # let mat = MatRead::new(materiau.point_values(0, 0).unwrap(), &idx_mat, &[]);
    /// // The law's state, beyond the `damage` reported for visualization.
    /// assert_eq!(DamageLaw::Mazars.internal_names(), vec!["kappa".to_string()]);
    /// // Damage-TC en porte quatre : deux seuils et deux endommagements.
    /// assert_eq!(DamageLaw::DamageTc.internal_names().len(), 4);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn internal_names(self) -> Vec<String> {
        self.as_law().internal_names()
    }

    /// One step of the law, at a Gauss point.
    ///
    /// ```
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::element_field::SubElementField;
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::damage::{self};
    /// # use pyrucast::models::continuum::material::MatRead;
    /// # use pyrucast::models::damage::law::{self, DamageLaw};
    /// # use pyrucast::models::continuum::elastic;
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// # let materiau = SubElementField::from_uniform_per_component(
    /// #     fes.get(0).unwrap(), vec!["E".into(), "nu".into(), "eps_d0".into(), "A_t".into(), "B_t".into(), "A_c".into(), "B_c".into()], &[30000.0, 0.2, 0.0001, 0.8, 20000.0, 1.4, 1850.0]).unwrap();
    /// # let idx_mat: Vec<u32> = (0..materiau.point_values(0, 0).unwrap().len() as u32).collect();
    /// # let opt_mat = [pyrucast::containers::field::ABSENT_COMPONENT; 8];
    /// # let mat = MatRead::new(materiau.point_values(0, 0).unwrap(), &idx_mat, &[]);
    /// // Below the `eps_d0` threshold, nothing is damaged.
    /// let petit = [1e-5, 0.0, 0.0, 0.0, 0.0, 0.0];
    /// let u = DamageLaw::Mazars.update(&petit, &[0.0], &mat, 2)?;
    /// assert_eq!(u.damage, 0.0);
    ///
    /// // Beyond it, damage grows and the stress is **degraded**: it falls below
    /// // the corresponding elastic stress.
    /// let grand = [1e-3, 0.0, 0.0, 0.0, 0.0, 0.0];
    /// let u = DamageLaw::Mazars.update(&grand, &[0.0], &mat, 2)?;
    /// assert!(u.damage > 0.0 && u.damage < 1.0);
    /// let (lambda, mu) = elastic::lame(30_000.0, 0.2);
    /// assert!(u.sigma[0] < elastic::elastic_stress(&grand, lambda, mu)[0]);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn update(
        self,
        eps: &[f64; 6],
        prev: &[f64],
        mat: &MatRead,
        space_dim: usize,
    ) -> Result<DamageUpdate> {
        self.as_law().update(eps, prev, mat, space_dim)
    }
}

impl crate::named::Named for DamageLaw {
    const LABEL: &'static str = "damage law";
    const VALUES: &'static [Self] = &Self::ALL;

    fn name(self) -> &'static str {
        DamageLaw::name(self)
    }
}

impl std::fmt::Display for DamageLaw {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.name())
    }
}

/// What a damage law returns for one Gauss point.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::SubElementField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::damage::{self};
/// # use pyrucast::models::continuum::material::MatRead;
/// # use pyrucast::models::damage::law::{self, DamageLaw};
/// # use pyrucast::models::continuum::elastic;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let materiau = SubElementField::from_uniform_per_component(
/// #     fes.get(0).unwrap(), vec!["E".into(), "nu".into(), "eps_d0".into(), "A_t".into(), "B_t".into(), "A_c".into(), "B_c".into()], &[30000.0, 0.2, 0.0001, 0.8, 20000.0, 1.4, 1850.0]).unwrap();
/// # let idx_mat: Vec<u32> = (0..materiau.point_values(0, 0).unwrap().len() as u32).collect();
/// # let opt_mat = [pyrucast::containers::field::ABSENT_COMPONENT; 8];
/// # let mat = MatRead::new(materiau.point_values(0, 0).unwrap(), &idx_mat, &[]);
/// // `damage` is a **summary** for visualization; the state is `vars`.
/// // A law with several damages reports the worst one there.
/// let u = DamageLaw::Mazars.update(&[1e-3, 0.0, 0.0, 0.0, 0.0, 0.0], &[0.0], &mat, 2)?;
/// assert_eq!(u.internal().len(), DamageLaw::Mazars.internal_names().len());
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub struct DamageUpdate {
    /// The degraded stress, full 3-D Voigt.
    pub sigma: [f64; 6],
    /// A scalar summary of the damage, for visualisation. The **state** is
    /// `vars`; a law with several damages reports the worst here.
    pub damage: f64,
    /// The law's internal variables, in [`DamageLaw::internal_names`] order.
    pub vars: [f64; MAX_INTERNAL_VARS],
    /// How many of `vars` this law actually carries — the rest is padding.
    pub n_vars: usize,
}

impl DamageUpdate {
    /// An update carrying `vars` as its history.
    ///
    /// Fixed-size and on the **stack**: a damage law runs at every Gauss point
    /// of every cell, so a `Vec` here was one allocation per point for a
    /// handful of numbers.
    ///
    /// ```
    /// # use pyrucast::models::damage::law::DamageUpdate;
    /// // A history of two variables, and nothing allocated to carry it.
    /// let u = DamageUpdate::new([0.0; 6], 0.4, &[1e-4, 0.4]);
    /// assert_eq!(u.damage, 0.4);
    /// assert_eq!(u.internal(), &[1e-4, 0.4]);
    /// ```
    pub fn new(sigma: [f64; 6], damage: f64, vars: &[f64]) -> Self {
        let mut v = [0.0_f64; MAX_INTERNAL_VARS];
        v[..vars.len()].copy_from_slice(vars);
        Self {
            sigma,
            damage,
            vars: v,
            n_vars: vars.len(),
        }
    }

    /// The history variables this update carries — the live prefix of `vars`.
    ///
    /// ```
    /// # use pyrucast::models::damage::law::DamageUpdate;
    /// // A law without history returns an empty slice of it, not a `Vec`.
    /// let u = DamageUpdate::new([0.0; 6], 0.0, &[]);
    /// assert!(u.internal().is_empty());
    /// ```
    pub fn internal(&self) -> &[f64] {
        &self.vars[..self.n_vars]
    }
}

/// Positive part `⟨x⟩₊ = max(x, 0)`.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::SubElementField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::damage::{self};
/// # use pyrucast::models::continuum::material::MatRead;
/// # use pyrucast::models::damage::law::{self, DamageLaw};
/// # use pyrucast::models::continuum::elastic;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let materiau = SubElementField::from_uniform_per_component(
/// #     fes.get(0).unwrap(), vec!["E".into(), "nu".into(), "eps_d0".into(), "A_t".into(), "B_t".into(), "A_c".into(), "B_c".into()], &[30000.0, 0.2, 0.0001, 0.8, 20000.0, 1.4, 1850.0]).unwrap();
/// # let idx_mat: Vec<u32> = (0..materiau.point_values(0, 0).unwrap().len() as u32).collect();
/// # let opt_mat = [pyrucast::containers::field::ABSENT_COMPONENT; 8];
/// # let mat = MatRead::new(materiau.point_values(0, 0).unwrap(), &idx_mat, &[]);
/// // The positive part, which the laws use to separate tension
/// // et compression.
/// assert_eq!((law::pos(3.0), law::pos(-3.0)), (3.0, 0.0));
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn pos(x: f64) -> f64 {
    x.max(0.0)
}