pyrucast 0.14.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
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Python wrappers for [`crate::ops::field`] — the operators polymorphic
//! over the field flavour, which give back a field of the caller's own kind.

use crate::handle::Handle;
use crate::py::element_field::{PyElementField, PySubElementField};
use crate::py::node_field::{PyNodeField, PySubNodeField};
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;

/// Extract a component-name argument that is either a single `str` or a list of
/// `str` (e.g. the result of `model.primal_vars()`) into a `Vec<String>`.
pub(crate) fn extract_names(arg: &Bound<'_, PyAny>) -> PyResult<Vec<String>> {
    if let Ok(name) = arg.extract::<String>() {
        return Ok(vec![name]);
    }
    if let Ok(names) = arg.extract::<Vec<String>>() {
        return Ok(names);
    }
    Err(PyTypeError::new_err(
        "components: expected a str or a list of str",
    ))
}

/// One entry of a field constructor's `components`: a `(name, value)` pair,
/// the component holding `value` everywhere, or a bare name, holding zero.
#[derive(FromPyObject)]
pub(crate) enum InitialComponent {
    Valued(String, f64),
    Named(String),
}

#[cfg(feature = "stub-gen")]
impl pyo3_stub_gen::PyStubType for InitialComponent {
    fn type_output() -> pyo3_stub_gen::TypeInfo {
        String::type_output() | <(String, f64)>::type_output()
    }
}

impl InitialComponent {
    /// The names of `components`, in order.
    pub(crate) fn names(components: &[Self]) -> Vec<String> {
        components
            .iter()
            .map(|c| match c {
                Self::Valued(name, _) | Self::Named(name) => name.clone(),
            })
            .collect()
    }

    /// Give each `(name, value)` entry of `components` its value on every zone
    /// of `field`, just built on them — a bare name keeps the zero it was
    /// built with.
    pub(crate) fn fill<F: crate::containers::field::Field>(field: &F, components: &[Self])
    where
        F::Sub: crate::containers::field::SubField,
    {
        use crate::containers::field::SubField;
        for c in components {
            if let Self::Valued(name, value) = c {
                for zone in field.iter() {
                    zone.write()
                        .set_uniform(name, *value)
                        .expect("every zone was built with every name");
                }
            }
        }
    }
}

/// Scalar product of two fields node by node, `pᵢ = Σ_c xᵢ,c·yᵢ,c`.
///
/// The reduction runs over the components alone: the support is kept, one
/// value per node, or per Gauss point for a field at the Gauss points. The
/// components are aligned by name (their order may differ) and only those both
/// fields carry count; a support only one field carries has no counterpart and
/// gives no zone. Each value is written once, so the result does not depend on
/// the number of threads.
///
/// # Parameters
///
/// - `x`: a `NodeField`, `SubNodeField`, `ElementField` or `SubElementField`.
/// - `y`: a field of the same type as `x`; two sub-fields must sit on the same
///   support.
///
/// # Returns
///
/// A new field of the type of `x`, one zone per support both fields carry,
/// with a single component `"psca"`.
///
/// # Raises
///
/// - `TypeError` if `y` is not of the type of `x`, or `x` none of the four.
/// - `RuntimeError` if two sub-fields sit on different supports.
///
/// # See also
///
/// `pyrucast.measure.xty`, the global scalar product: a single number over
/// the whole field.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn psca(py: Python<'_>, x: &Bound<'_, PyAny>, y: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
    use crate::containers::field::{Field, SubField};
    if let Ok(a) = x.extract::<PyRef<PyNodeField>>() {
        let b = y
            .extract::<PyRef<PyNodeField>>()
            .map_err(|_| PyTypeError::new_err("psca: both operands must be NodeFields"))?;
        let inner = a.inner.pscal_field(&b.inner)?;
        return Ok(Py::new(py, PyNodeField { inner })?.into_any());
    }
    if let Ok(a) = x.extract::<PyRef<PyElementField>>() {
        let b = y
            .extract::<PyRef<PyElementField>>()
            .map_err(|_| PyTypeError::new_err("psca: both operands must be ElementFields"))?;
        let inner = a.inner.pscal_field(&b.inner)?;
        return Ok(Py::new(py, PyElementField { inner })?.into_any());
    }
    if let Ok(a) = x.extract::<PyRef<PySubNodeField>>() {
        let b = y
            .extract::<PyRef<PySubNodeField>>()
            .map_err(|_| PyTypeError::new_err("psca: both operands must be SubNodeFields"))?;
        let out = a.handle.read().pscal(&*b.handle.read())?;
        return Ok(Py::new(
            py,
            PySubNodeField {
                handle: Handle::new(out),
            },
        )?
        .into_any());
    }
    if let Ok(a) = x.extract::<PyRef<PySubElementField>>() {
        let b = y
            .extract::<PyRef<PySubElementField>>()
            .map_err(|_| PyTypeError::new_err("psca: both operands must be SubElementFields"))?;
        let out = a.handle.read().pscal(&*b.handle.read())?;
        return Ok(Py::new(
            py,
            PySubElementField {
                handle: Handle::new(out),
            },
        )?
        .into_any());
    }
    Err(PyTypeError::new_err(
        "expected a NodeField, SubNodeField, ElementField or SubElementField",
    ))
}

// ── Element-wise unary maths (numpy-style) ──────────────────────────────────
//
// `pyrucast.cos(field)`, `pyrucast.exp(field)`, … apply a scalar function to
// every value of a field, returning a **new** field of the same type. They
// accept any of the four field flavours (`NodeField` / `SubNodeField` /
// `ElementField` / `SubElementField`) and mirror `crate::ops::field::*`.
// Results are unguarded (numpy-like): `log` of ≤ 0 → `-inf`/`nan`, etc.

/// Generate a `#[pyfunction] $name(field)` that dispatches over the four field
/// wrapper types and applies the matching `ops::field::$name`, **and** the four
/// methods that are its « sujet » face. The documentation is written once, at
/// the call site, and reaches the free function and the four methods alike.
macro_rules! define_polymorphic_pyfunction {
    ($(#[doc = $doc:literal])* $name:ident) => {
        $(#[doc = $doc])*
        #[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
        #[pyfunction]
        pub fn $name(py: Python<'_>, field: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
            use crate::ops::field::$name as op;
            if let Ok(f) = field.extract::<PyRef<PyNodeField>>() {
                return Ok(Py::new(
                    py,
                    PyNodeField {
                        inner: op(&f.inner)?,
                    },
                )?
                .into_any());
            }
            if let Ok(f) = field.extract::<PyRef<PyElementField>>() {
                return Ok(Py::new(
                    py,
                    PyElementField {
                        inner: op(&f.inner)?,
                    },
                )?
                .into_any());
            }
            if let Ok(f) = field.extract::<PyRef<PySubNodeField>>() {
                let out = op(&*f.handle.read())?;
                return Ok(Py::new(
                    py,
                    PySubNodeField {
                        handle: Handle::new(out),
                    },
                )?
                .into_any());
            }
            if let Ok(f) = field.extract::<PyRef<PySubElementField>>() {
                let out = op(&*f.handle.read())?;
                return Ok(Py::new(
                    py,
                    PySubElementField {
                        handle: Handle::new(out),
                    },
                )?
                .into_any());
            }
            Err(PyTypeError::new_err(
                "expected a NodeField, SubNodeField, ElementField or SubElementField",
            ))
        }

        $crate::impl_field_transform_pymethod! {
            $(#[doc = $doc])*
            [PyNodeField, PyElementField], $name
        }
        $crate::impl_subfield_transform_pymethod! {
            $(#[doc = $doc])*
            [PySubNodeField, PySubElementField], $name
        }
    };
}

define_polymorphic_pyfunction! {
    /// Absolute value of `field`, value by value.
    ///
    /// Every value of every component is mapped by the function, as numpy does: the
    /// result is not guarded.
    ///
    /// # Parameters
    ///
    /// - `field`: a `NodeField`, `SubNodeField`, `ElementField` or
    ///   `SubElementField`.
    ///
    /// # Returns
    ///
    /// A new field of the type of `field`, with the same structure.
    ///
    /// # Raises
    ///
    /// `TypeError` if `field` is none of the four field types.
    abs
}
define_polymorphic_pyfunction! {
    /// Square root of `field`, value by value.
    ///
    /// Every value of every component is mapped by the function, as numpy does: the
    /// result is not guarded. A negative value gives `nan`.
    ///
    /// # Parameters
    ///
    /// - `field`: a `NodeField`, `SubNodeField`, `ElementField` or
    ///   `SubElementField`.
    ///
    /// # Returns
    ///
    /// A new field of the type of `field`, with the same structure.
    ///
    /// # Raises
    ///
    /// `TypeError` if `field` is none of the four field types.
    sqrt
}
define_polymorphic_pyfunction! {
    /// Exponential `eˣ` of `field`, value by value.
    ///
    /// Every value of every component is mapped by the function, as numpy does: the
    /// result is not guarded.
    ///
    /// # Parameters
    ///
    /// - `field`: a `NodeField`, `SubNodeField`, `ElementField` or
    ///   `SubElementField`.
    ///
    /// # Returns
    ///
    /// A new field of the type of `field`, with the same structure.
    ///
    /// # Raises
    ///
    /// `TypeError` if `field` is none of the four field types.
    exp
}
define_polymorphic_pyfunction! {
    /// Natural logarithm of `field`, value by value.
    ///
    /// Every value of every component is mapped by the function, as numpy does: the
    /// result is not guarded. Zero gives `-inf`, a negative value `nan`.
    ///
    /// # Parameters
    ///
    /// - `field`: a `NodeField`, `SubNodeField`, `ElementField` or
    ///   `SubElementField`.
    ///
    /// # Returns
    ///
    /// A new field of the type of `field`, with the same structure.
    ///
    /// # Raises
    ///
    /// `TypeError` if `field` is none of the four field types.
    log
}
define_polymorphic_pyfunction! {
    /// Base-10 logarithm of `field`, value by value.
    ///
    /// Every value of every component is mapped by the function, as numpy does: the
    /// result is not guarded. Zero gives `-inf`, a negative value `nan`.
    ///
    /// # Parameters
    ///
    /// - `field`: a `NodeField`, `SubNodeField`, `ElementField` or
    ///   `SubElementField`.
    ///
    /// # Returns
    ///
    /// A new field of the type of `field`, with the same structure.
    ///
    /// # Raises
    ///
    /// `TypeError` if `field` is none of the four field types.
    log10
}
define_polymorphic_pyfunction! {
    /// Cosine of `field`, value by value, in radians.
    ///
    /// Every value of every component is mapped by the function, as numpy does: the
    /// result is not guarded.
    ///
    /// # Parameters
    ///
    /// - `field`: a `NodeField`, `SubNodeField`, `ElementField` or
    ///   `SubElementField`.
    ///
    /// # Returns
    ///
    /// A new field of the type of `field`, with the same structure.
    ///
    /// # Raises
    ///
    /// `TypeError` if `field` is none of the four field types.
    cos
}
define_polymorphic_pyfunction! {
    /// Sine of `field`, value by value, in radians.
    ///
    /// Every value of every component is mapped by the function, as numpy does: the
    /// result is not guarded.
    ///
    /// # Parameters
    ///
    /// - `field`: a `NodeField`, `SubNodeField`, `ElementField` or
    ///   `SubElementField`.
    ///
    /// # Returns
    ///
    /// A new field of the type of `field`, with the same structure.
    ///
    /// # Raises
    ///
    /// `TypeError` if `field` is none of the four field types.
    sin
}
define_polymorphic_pyfunction! {
    /// Tangent of `field`, value by value, in radians.
    ///
    /// Every value of every component is mapped by the function, as numpy does: the
    /// result is not guarded.
    ///
    /// # Parameters
    ///
    /// - `field`: a `NodeField`, `SubNodeField`, `ElementField` or
    ///   `SubElementField`.
    ///
    /// # Returns
    ///
    /// A new field of the type of `field`, with the same structure.
    ///
    /// # Raises
    ///
    /// `TypeError` if `field` is none of the four field types.
    tan
}
define_polymorphic_pyfunction! {
    /// Hyperbolic sine of `field`, value by value.
    ///
    /// Every value of every component is mapped by the function, as numpy does: the
    /// result is not guarded.
    ///
    /// # Parameters
    ///
    /// - `field`: a `NodeField`, `SubNodeField`, `ElementField` or
    ///   `SubElementField`.
    ///
    /// # Returns
    ///
    /// A new field of the type of `field`, with the same structure.
    ///
    /// # Raises
    ///
    /// `TypeError` if `field` is none of the four field types.
    sinh
}
define_polymorphic_pyfunction! {
    /// Hyperbolic cosine of `field`, value by value.
    ///
    /// Every value of every component is mapped by the function, as numpy does: the
    /// result is not guarded.
    ///
    /// # Parameters
    ///
    /// - `field`: a `NodeField`, `SubNodeField`, `ElementField` or
    ///   `SubElementField`.
    ///
    /// # Returns
    ///
    /// A new field of the type of `field`, with the same structure.
    ///
    /// # Raises
    ///
    /// `TypeError` if `field` is none of the four field types.
    cosh
}
define_polymorphic_pyfunction! {
    /// Hyperbolic tangent of `field`, value by value.
    ///
    /// Every value of every component is mapped by the function, as numpy does: the
    /// result is not guarded.
    ///
    /// # Parameters
    ///
    /// - `field`: a `NodeField`, `SubNodeField`, `ElementField` or
    ///   `SubElementField`.
    ///
    /// # Returns
    ///
    /// A new field of the type of `field`, with the same structure.
    ///
    /// # Raises
    ///
    /// `TypeError` if `field` is none of the four field types.
    tanh
}