cfsem 11.1.0

Quasi-steady electromagnetics including filamentized approximations, Biot-Savart, and Grad-Shafranov.
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! One-shot hierarchical field-solver convenience functions.
//!
//! These helpers intentionally rebuild the source tree every call. For the
//! operating range where the hierarchical solve wins over the dense direct
//! methods, tree construction is usually a small part of total runtime, so this
//! API favors direct-method ergonomics.

use crate::mesh::triangle3d::TriangleMeshView;
use crate::physics::boundary_element::QuadratureKind;
use std::time::Instant;

use super::kernels::{
    BoundaryElementFluxDensityKernel, BoundaryElementNodalValues, BoundaryElementTriangles,
    BoundaryElementVectorPotentialKernel, DipoleFluxDensityKernel, DipoleMoments, DipoleSources,
    DipoleTargets, DipoleVectorPotentialKernel, LinearFilamentFluxDensityKernel,
    LinearFilamentSources, LinearFilamentVectorPotentialKernel,
};
use super::{
    BuildMethod, ClusterTree, EvaluationScratch, HierarchicalError, HierarchicalKernel, Scalar,
    SourceCollection, SourceMomentCollection, SourceNodeSummaries, TargetCollection, eval,
    eval_par, scratch_len, scratch_len_par, update_summaries,
};

/// Diagnostic information returned by stateless hierarchical solves.
pub struct Diagnostics<K: HierarchicalKernel> {
    /// Source tree built for this solve.
    pub source_tree: ClusterTree<K::Scalar>,
    /// Wall-clock construction time in seconds.
    pub construction_seconds: f64,
    /// Wall-clock evaluation time in seconds.
    pub evaluation_seconds: f64,
    /// Number of sources in the solve.
    pub source_count: usize,
    /// Number of targets in the solve.
    pub target_count: usize,
}

impl<K: HierarchicalKernel> Diagnostics<K> {
    /// Borrow the source tree built for this solve.
    #[inline]
    pub fn source_tree(&self) -> &ClusterTree<K::Scalar> {
        &self.source_tree
    }
}

/// Hierarchical magnetic flux density of dipole sources at Cartesian targets.
///
/// This one-shot helper mirrors the direct dipole API while rebuilding the
/// source tree internally.
///
/// This is an approximate method, and no particular accuracy level is guaranteed.
/// Truncated methods like this one may average entire local loop structures out of
/// existence; as a result, maximum relative error is unbounded. This method must be
/// tuned to a given use-case in order to be useful, and should not be used to calculate
/// safety-related field limits.
///
/// Args:
///     loc: Dipole source coordinates.
///     moment: Dipole magnetic moment components.
///     obs: Observation point coordinates.
///     outer_radius: Radius for the magnetized-sphere near-field treatment.
///     construction_method: Source-tree construction method.
///     theta: Barnes-Hut acceptance angle. Smaller values are more accurate and slower.
///     par: Whether to evaluate target batches in parallel.
///     out: Output component slices to fill.
///
/// Returns:
///     Source-tree diagnostics and construction/evaluation timing on success.
///
/// Errors:
///     Returns [`HierarchicalError`] when input lengths are inconsistent, tree construction fails,
///     scratch storage is too small, or a kernel reports an error.
pub fn flux_density_dipole_hierarchical<T: Scalar>(
    loc: (&[T], &[T], &[T]),
    moment: (&[T], &[T], &[T]),
    obs: (&[T], &[T], &[T]),
    outer_radius: &[T],
    construction_method: BuildMethod,
    theta: T,
    par: bool,
    out: (&mut [T], &mut [T], &mut [T]),
) -> Result<Diagnostics<DipoleFluxDensityKernel<T>>, HierarchicalError> {
    let sources = DipoleSources::new(loc.0, loc.1, loc.2, outer_radius);
    let moments = DipoleMoments::new(moment.0, moment.1, moment.2);
    let targets = DipoleTargets::new(obs.0, obs.1, obs.2);
    one_shot_vec3(
        DipoleFluxDensityKernel::<T>::new(),
        sources,
        moments,
        targets,
        construction_method,
        theta,
        par,
        out,
    )
}

/// Hierarchical magnetic vector potential of dipole sources at Cartesian targets.
///
/// This one-shot helper mirrors the direct dipole API while rebuilding the
/// source tree internally.
///
/// This is an approximate method, and no particular accuracy level is guaranteed.
/// Truncated methods like this one may average entire local loop structures out of
/// existence; as a result, maximum relative error is unbounded. This method must be
/// tuned to a given use-case in order to be useful, and should not be used to calculate
/// safety-related field limits.
///
/// Args:
///     loc: Dipole source coordinates.
///     moment: Dipole magnetic moment components.
///     obs: Observation point coordinates.
///     outer_radius: Radius for the magnetized-sphere near-field treatment.
///     construction_method: Source-tree construction method.
///     theta: Barnes-Hut acceptance angle. Smaller values are more accurate and slower.
///     par: Whether to evaluate target batches in parallel.
///     out: Output component slices to fill.
///
/// Returns:
///     Source-tree diagnostics and construction/evaluation timing on success.
///
/// Errors:
///     Returns [`HierarchicalError`] when input lengths are inconsistent, tree construction fails,
///     scratch storage is too small, or a kernel reports an error.
pub fn vector_potential_dipole_hierarchical<T: Scalar>(
    loc: (&[T], &[T], &[T]),
    moment: (&[T], &[T], &[T]),
    obs: (&[T], &[T], &[T]),
    outer_radius: &[T],
    construction_method: BuildMethod,
    theta: T,
    par: bool,
    out: (&mut [T], &mut [T], &mut [T]),
) -> Result<Diagnostics<DipoleVectorPotentialKernel<T>>, HierarchicalError> {
    let sources = DipoleSources::new(loc.0, loc.1, loc.2, outer_radius);
    let moments = DipoleMoments::new(moment.0, moment.1, moment.2);
    let targets = DipoleTargets::new(obs.0, obs.1, obs.2);
    one_shot_vec3(
        DipoleVectorPotentialKernel::<T>::new(),
        sources,
        moments,
        targets,
        construction_method,
        theta,
        par,
        out,
    )
}

/// Hierarchical magnetic flux density of linear filament segments.
///
/// This one-shot helper mirrors [`crate::physics::linear_filament::flux_density_linear_filament`]
/// while rebuilding the source tree internally.
///
/// This is an approximate method, and no particular accuracy level is guaranteed.
/// Truncated methods like this one may average entire local loop structures out of
/// existence; as a result, maximum relative error is unbounded. This method must be
/// tuned to a given use-case in order to be useful, and should not be used to calculate
/// safety-related field limits.
///
/// Args:
///     xyzp: Observation point coordinates.
///     xyzfil: Filament segment start coordinates.
///     dlxyzfil: Filament segment start-to-end displacement components.
///     ifil: Current in each filament segment.
///     wire_radius: Wire radius for each filament segment.
///     construction_method: Source-tree construction method.
///     theta: Barnes-Hut acceptance angle. Smaller values are more accurate and slower.
///     par: Whether to evaluate target batches in parallel.
///     out: Output component slices to fill.
///
/// Returns:
///     Source-tree diagnostics and construction/evaluation timing on success.
///
/// Errors:
///     Returns [`HierarchicalError`] when input lengths are inconsistent, tree construction fails,
///     scratch storage is too small, or a kernel reports an error.
pub fn flux_density_linear_filament_hierarchical<T: Scalar>(
    xyzp: (&[T], &[T], &[T]),
    xyzfil: (&[T], &[T], &[T]),
    dlxyzfil: (&[T], &[T], &[T]),
    ifil: &[T],
    wire_radius: &[T],
    construction_method: BuildMethod,
    theta: T,
    par: bool,
    out: (&mut [T], &mut [T], &mut [T]),
) -> Result<Diagnostics<LinearFilamentFluxDensityKernel<T>>, HierarchicalError> {
    let sources = LinearFilamentSources::new(xyzfil, dlxyzfil, wire_radius);
    let targets = DipoleTargets::new(xyzp.0, xyzp.1, xyzp.2);
    one_shot_vec3(
        LinearFilamentFluxDensityKernel::<T>::new(),
        sources,
        ifil,
        targets,
        construction_method,
        theta,
        par,
        out,
    )
}

/// Hierarchical magnetic vector potential of linear filament segments.
///
/// This one-shot helper mirrors [`crate::physics::linear_filament::vector_potential_linear_filament`]
/// while rebuilding the source tree internally.
///
/// This is an approximate method, and no particular accuracy level is guaranteed.
/// Truncated methods like this one may average entire local loop structures out of
/// existence; as a result, maximum relative error is unbounded. This method must be
/// tuned to a given use-case in order to be useful, and should not be used to calculate
/// safety-related field limits.
///
/// Args:
///     xyzp: Observation point coordinates.
///     xyzfil: Filament segment start coordinates.
///     dlxyzfil: Filament segment start-to-end displacement components.
///     ifil: Current in each filament segment.
///     wire_radius: Wire radius for each filament segment.
///     construction_method: Source-tree construction method.
///     theta: Barnes-Hut acceptance angle. Smaller values are more accurate and slower.
///     par: Whether to evaluate target batches in parallel.
///     out: Output component slices to fill.
///
/// Returns:
///     Source-tree diagnostics and construction/evaluation timing on success.
///
/// Errors:
///     Returns [`HierarchicalError`] when input lengths are inconsistent, tree construction fails,
///     scratch storage is too small, or a kernel reports an error.
pub fn vector_potential_linear_filament_hierarchical<T: Scalar>(
    xyzp: (&[T], &[T], &[T]),
    xyzfil: (&[T], &[T], &[T]),
    dlxyzfil: (&[T], &[T], &[T]),
    ifil: &[T],
    wire_radius: &[T],
    construction_method: BuildMethod,
    theta: T,
    par: bool,
    out: (&mut [T], &mut [T], &mut [T]),
) -> Result<Diagnostics<LinearFilamentVectorPotentialKernel<T>>, HierarchicalError> {
    let sources = LinearFilamentSources::new(xyzfil, dlxyzfil, wire_radius);
    let targets = DipoleTargets::new(xyzp.0, xyzp.1, xyzp.2);
    one_shot_vec3(
        LinearFilamentVectorPotentialKernel::<T>::new(),
        sources,
        ifil,
        targets,
        construction_method,
        theta,
        par,
        out,
    )
}

/// Hierarchical magnetic flux density from a triangle mesh with nodal stream-function values.
///
/// This one-shot helper mirrors [`crate::physics::boundary_element::flux_density_triangle_mesh`]
/// while rebuilding the source tree internally.
///
/// This is an approximate method, and no particular accuracy level is guaranteed.
/// Truncated methods like this one may average entire local loop structures out of
/// existence; as a result, maximum relative error is unbounded. This method must be
/// tuned to a given use-case in order to be useful, and should not be used to calculate
/// safety-related field limits.
///
/// Args:
///     obs: Observation point coordinates.
///     mesh: Triangle mesh source geometry.
///     s: Nodal stream-function values.
///     quad_kind: Triangle quadrature rule.
///     construction_method: Source-tree construction method.
///     theta: Barnes-Hut acceptance angle. Smaller values are more accurate and slower.
///     par: Whether to evaluate target batches in parallel.
///     out: Output component slices to fill.
///
/// Returns:
///     Source-tree diagnostics and construction/evaluation timing on success.
///
/// Errors:
///     Returns [`HierarchicalError`] when input lengths are inconsistent, mesh conversion fails,
///     tree construction fails, scratch storage is too small, or a kernel reports an error.
pub fn flux_density_triangle_mesh_hierarchical(
    obs: (&[f64], &[f64], &[f64]),
    mesh: &TriangleMeshView<'_>,
    s: &[f64],
    quad_kind: QuadratureKind,
    construction_method: BuildMethod,
    theta: f64,
    par: bool,
    out: (&mut [f64], &mut [f64], &mut [f64]),
) -> Result<Diagnostics<BoundaryElementFluxDensityKernel<f64>>, HierarchicalError> {
    mesh.validate_nodal_values(s)
        .map_err(|_| HierarchicalError::LengthMismatch)?;
    let sources = BoundaryElementTriangles::new(mesh);
    let moments = BoundaryElementNodalValues::new(sources, s);
    let targets = DipoleTargets::new(obs.0, obs.1, obs.2);
    one_shot_vec3(
        BoundaryElementFluxDensityKernel::<f64>::new(quad_kind),
        sources,
        moments,
        targets,
        construction_method,
        theta,
        par,
        out,
    )
}

/// Hierarchical magnetic vector potential from a triangle mesh with nodal stream-function values.
///
/// This one-shot helper mirrors [`crate::physics::boundary_element::vector_potential_triangle_mesh`]
/// while rebuilding the source tree internally.
///
/// This is an approximate method, and no particular accuracy level is guaranteed.
/// Truncated methods like this one may average entire local loop structures out of
/// existence; as a result, maximum relative error is unbounded. This method must be
/// tuned to a given use-case in order to be useful, and should not be used to calculate
/// safety-related field limits.
///
/// Args:
///     obs: Observation point coordinates.
///     mesh: Triangle mesh source geometry.
///     s: Nodal stream-function values.
///     quad_kind: Triangle quadrature rule.
///     construction_method: Source-tree construction method.
///     theta: Barnes-Hut acceptance angle. Smaller values are more accurate and slower.
///     par: Whether to evaluate target batches in parallel.
///     out: Output component slices to fill.
///
/// Returns:
///     Source-tree diagnostics and construction/evaluation timing on success.
///
/// Errors:
///     Returns [`HierarchicalError`] when input lengths are inconsistent, mesh conversion fails,
///     tree construction fails, scratch storage is too small, or a kernel reports an error.
pub fn vector_potential_triangle_mesh_hierarchical(
    obs: (&[f64], &[f64], &[f64]),
    mesh: &TriangleMeshView<'_>,
    s: &[f64],
    quad_kind: QuadratureKind,
    construction_method: BuildMethod,
    theta: f64,
    par: bool,
    out: (&mut [f64], &mut [f64], &mut [f64]),
) -> Result<Diagnostics<BoundaryElementVectorPotentialKernel<f64>>, HierarchicalError> {
    mesh.validate_nodal_values(s)
        .map_err(|_| HierarchicalError::LengthMismatch)?;
    let sources = BoundaryElementTriangles::new(mesh);
    let moments = BoundaryElementNodalValues::new(sources, s);
    let targets = DipoleTargets::new(obs.0, obs.1, obs.2);
    one_shot_vec3(
        BoundaryElementVectorPotentialKernel::<f64>::new(quad_kind),
        sources,
        moments,
        targets,
        construction_method,
        theta,
        par,
        out,
    )
}

/// Build trees, evaluate a vector-valued hierarchical solve, and return diagnostics.
pub(crate) fn one_shot_vec3<K, T, S, M, C>(
    kernel: K,
    sources: S,
    moments: M,
    targets: C,
    construction_method: BuildMethod,
    theta: T,
    par: bool,
    out: (&mut [T], &mut [T], &mut [T]),
) -> Result<Diagnostics<K>, HierarchicalError>
where
    K: HierarchicalKernel<Scalar = T, Output = [T; 3]> + Sync,
    T: Scalar,
    S: SourceCollection<K>,
    M: SourceMomentCollection<K>,
    K::TargetGeometry: Copy,
    C: TargetCollection<K>,
{
    if out.0.len() != targets.len()
        || out.1.len() != targets.len()
        || out.2.len() != targets.len()
        || !targets.valid_lengths()
        || sources.len() != moments.len()
        || !sources.valid_lengths()
        || !moments.valid_lengths()
    {
        return Err(HierarchicalError::LengthMismatch);
    }

    let construction_start = Instant::now();
    let source_tree = match construction_method {
        BuildMethod::LongestAxis => ClusterTree::build(sources)?,
        BuildMethod::MortonLbvh => ClusterTree::build_morton_lbvh(sources)?,
    };
    let mut source_summaries = SourceNodeSummaries::<K>::new(source_tree.as_view());
    let mut err = update_summaries(
        &kernel,
        source_tree.as_view(),
        sources,
        moments,
        &mut source_summaries.node_summaries,
    );
    if err != HierarchicalError::Ok {
        return Err(err);
    }
    let construction_seconds = construction_start.elapsed().as_secs_f64();

    let evaluation_start = Instant::now();
    let scratch_len = match par {
        true => scratch_len_par(targets.len()),
        false => scratch_len(),
    };
    let mut scratch_values = vec![[T::ZERO; 3]; scratch_len];
    let mut scratch = EvaluationScratch {
        contribution: &mut scratch_values,
    };
    let out_components = [out.0, out.1, out.2];
    err = match par {
        true => eval_par(
            &kernel,
            source_tree.as_view(),
            &source_summaries.node_summaries,
            sources,
            targets,
            moments,
            theta,
            out_components,
            &mut scratch,
        ),
        false => eval(
            &kernel,
            source_tree.as_view(),
            &source_summaries.node_summaries,
            sources,
            targets,
            moments,
            theta,
            out_components,
            &mut scratch,
        ),
    };
    if err != HierarchicalError::Ok {
        return Err(err);
    }
    let evaluation_seconds = evaluation_start.elapsed().as_secs_f64();

    Ok(Diagnostics {
        source_tree,
        construction_seconds,
        evaluation_seconds,
        source_count: sources.len(),
        target_count: targets.len(),
    })
}