slatec 0.1.0

Safe Rust interface to selected SLATEC numerical routines
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
//! Checked axisymmetric spherical FISHPACK workflows.
//!
//! These are deliberately separate from the unit-sphere surface workflows:
//! `HWSCSP` and `HSTCSP` retain a radial coordinate and solve the
//! axisymmetric three-dimensional equation after a longitude Fourier mode has
//! been selected.  Calls use fresh owned workspaces and are process-serialized
//! because the underlying `BLKTRI` implementation has legacy saved state.

use alloc::vec::Vec;
use core::convert::TryFrom;

use slatec_sys::FortranInteger;

use crate::runtime::lock_native;

use super::fishpack_cylindrical_polar::{
    CurvilinearPdeError, CurvilinearPdeSolution, FishpackGrid2, NativeCurvilinearPdeStatus,
    RadialAxis, RadialBoundary, StaggeredRadialAxis,
};
use super::fishpack_spherical::{
    ColatitudeAxis, ColatitudeBoundary, SLATEC_PI, StaggeredColatitudeAxis,
};

/// An owned centered-grid axisymmetric spherical Helmholtz problem.
///
/// `HWSCSP` approximates
/// `(r^-2)(r^2 u_r)_r + (r^2 sin(theta))^-1 (sin(theta) u_theta)_theta +
/// lambda/(r sin(theta))^2 u = rhs` on a colatitude-by-radius grid.  There is
/// no longitude coordinate: this is not the unit-sphere surface solver.
#[derive(Clone, Debug, PartialEq)]
pub struct AxisymmetricSphericalHelmholtz2d {
    colatitude: ColatitudeAxis,
    radius: RadialAxis,
    coefficient: f32,
    rhs: FishpackGrid2,
    colatitude_boundary: ColatitudeBoundary,
    radial_boundary: RadialBoundary,
}

impl AxisymmetricSphericalHelmholtz2d {
    /// Validates and owns a centered-grid axisymmetric spherical problem.
    ///
    /// Colatitude derivatives use increasing colatitude; radial derivatives
    /// use increasing radius.  Pole and origin variants represent the
    /// source-defined regularity conditions rather than arbitrary values at a
    /// coordinate singularity.
    pub fn new(
        colatitude: ColatitudeAxis,
        radius: RadialAxis,
        coefficient: f32,
        rhs: FishpackGrid2,
        colatitude_boundary: ColatitudeBoundary,
        radial_boundary: RadialBoundary,
    ) -> Result<Self, CurvilinearPdeError> {
        validate_centered(
            colatitude,
            radius,
            coefficient,
            &rhs,
            &colatitude_boundary,
            &radial_boundary,
        )?;
        Ok(Self {
            colatitude,
            radius,
            coefficient,
            rhs,
            colatitude_boundary,
            radial_boundary,
        })
    }

    /// Solves through the reviewed single-precision `HWSCSP` driver.
    pub fn solve(self) -> Result<CurvilinearPdeSolution, CurvilinearPdeError> {
        let first_nodes = self.colatitude.nodes()?;
        let second_nodes = self.radius.nodes()?;
        let mut intl = 0;
        let mut m = as_fortran(self.colatitude.panels())?;
        let mut n = as_fortran(self.radius.panels())?;
        let mut idimf = as_fortran(first_nodes)?;
        let mut mbdcnd = self.colatitude_boundary.code();
        let mut nbdcnd = self.radial_boundary.code();
        let mut ts = self.colatitude.lower();
        let mut tf = self.colatitude.upper();
        let mut rs = self.radius.lower();
        let mut rf = self.radius.upper();
        let mut coefficient = self.coefficient;
        let mut values = self.rhs.values().to_vec();
        apply_centered_edges(
            first_nodes,
            second_nodes,
            &mut values,
            &self.colatitude_boundary,
            &self.radial_boundary,
        );
        let dummy = [0.0_f32];
        let bdts = self
            .colatitude_boundary
            .lower_derivative()
            .unwrap_or(&dummy);
        let bdtf = self
            .colatitude_boundary
            .upper_derivative()
            .unwrap_or(&dummy);
        let bdrs = self.radial_boundary.lower_derivative().unwrap_or(&dummy);
        let bdrf = self.radial_boundary.upper_derivative().unwrap_or(&dummy);
        let workspace_length = centered_workspace_len(
            self.colatitude.panels(),
            self.radius.panels(),
            self.radial_boundary.code(),
        )?;
        let mut workspace = zeroed(workspace_length)?;
        let mut perturbation = 0.0;
        let mut ierror = 0;
        let _native = lock_native();
        // SAFETY: all scalar, grid, boundary, and workspace buffers are
        // owned, non-null, source-sized, and live for this serialized
        // `HWSCSP` call.  The routine does not retain any caller pointer.
        unsafe {
            slatec_sys::pde::fishpack::hwscsp(
                &mut intl,
                &mut ts,
                &mut tf,
                &mut m,
                &mut mbdcnd,
                bdts.as_ptr().cast_mut(),
                bdtf.as_ptr().cast_mut(),
                &mut rs,
                &mut rf,
                &mut n,
                &mut nbdcnd,
                bdrs.as_ptr().cast_mut(),
                bdrf.as_ptr().cast_mut(),
                &mut coefficient,
                values.as_mut_ptr(),
                &mut idimf,
                &mut perturbation,
                &mut ierror,
                workspace.as_mut_ptr(),
            );
        }
        native_result(
            "HWSCSP",
            first_nodes,
            second_nodes,
            values,
            perturbation,
            ierror,
            workspace,
            9,
        )
    }
}

/// An owned staggered-grid axisymmetric spherical Helmholtz problem.
///
/// `HSTCSP` samples colatitude and radius at open-interval points.  Endpoint
/// data remain private owned buffers in the boundary objects; the RHS grid is
/// therefore exactly `colatitude.points() × radius.points()`.
#[derive(Clone, Debug, PartialEq)]
pub struct StaggeredAxisymmetricSphericalHelmholtz2d {
    colatitude: StaggeredColatitudeAxis,
    radius: StaggeredRadialAxis,
    coefficient: f32,
    rhs: FishpackGrid2,
    colatitude_boundary: ColatitudeBoundary,
    radial_boundary: RadialBoundary,
}

impl StaggeredAxisymmetricSphericalHelmholtz2d {
    /// Validates and owns a staggered-grid axisymmetric spherical problem.
    pub fn new(
        colatitude: StaggeredColatitudeAxis,
        radius: StaggeredRadialAxis,
        coefficient: f32,
        rhs: FishpackGrid2,
        colatitude_boundary: ColatitudeBoundary,
        radial_boundary: RadialBoundary,
    ) -> Result<Self, CurvilinearPdeError> {
        validate_staggered(
            colatitude,
            radius,
            coefficient,
            &rhs,
            &colatitude_boundary,
            &radial_boundary,
        )?;
        Ok(Self {
            colatitude,
            radius,
            coefficient,
            rhs,
            colatitude_boundary,
            radial_boundary,
        })
    }

    /// Solves through the reviewed single-precision `HSTCSP` driver.
    pub fn solve(self) -> Result<CurvilinearPdeSolution, CurvilinearPdeError> {
        let first_points = self.colatitude.points();
        let second_points = self.radius.points();
        let mut intl = 0;
        let mut m = as_fortran(first_points)?;
        let mut n = as_fortran(second_points)?;
        let mut idimf = as_fortran(first_points)?;
        let mut mbdcnd = self.colatitude_boundary.code();
        let mut nbdcnd = self.radial_boundary.code();
        let mut a = self.colatitude.lower();
        let mut b = self.colatitude.upper();
        let mut c = self.radius.lower();
        let mut d = self.radius.upper();
        let mut coefficient = self.coefficient;
        let mut values = self.rhs.values().to_vec();
        let dummy = [0.0_f32];
        let bda = self.colatitude_boundary.lower_data().unwrap_or(&dummy);
        let bdb = self.colatitude_boundary.upper_data().unwrap_or(&dummy);
        let bdc = self.radial_boundary.lower_data().unwrap_or(&dummy);
        let bdd = self.radial_boundary.upper_data().unwrap_or(&dummy);
        let workspace_length = staggered_workspace_len(first_points, second_points)?;
        let mut workspace = zeroed(workspace_length)?;
        let mut perturbation = 0.0;
        let mut ierror = 0;
        let _native = lock_native();
        // SAFETY: the owned arrays satisfy HSTCSP's `BDA(N)`, `BDB(N)`,
        // `BDC(M)`, `BDD(M)`, `F(IDIMF,N)`, and workspace contracts.  Native
        // state is serialized and no pointer escapes this synchronous call.
        unsafe {
            slatec_sys::pde::fishpack::hstcsp(
                &mut intl,
                &mut a,
                &mut b,
                &mut m,
                &mut mbdcnd,
                bda.as_ptr().cast_mut(),
                bdb.as_ptr().cast_mut(),
                &mut c,
                &mut d,
                &mut n,
                &mut nbdcnd,
                bdc.as_ptr().cast_mut(),
                bdd.as_ptr().cast_mut(),
                &mut coefficient,
                values.as_mut_ptr(),
                &mut idimf,
                &mut perturbation,
                &mut ierror,
                workspace.as_mut_ptr(),
            );
        }
        native_result(
            "HSTCSP",
            first_points,
            second_points,
            values,
            perturbation,
            ierror,
            workspace,
            10,
        )
    }
}

fn validate_centered(
    colatitude: ColatitudeAxis,
    radius: RadialAxis,
    coefficient: f32,
    rhs: &FishpackGrid2,
    colatitude_boundary: &ColatitudeBoundary,
    radial_boundary: &RadialBoundary,
) -> Result<(), CurvilinearPdeError> {
    if !coefficient.is_finite() {
        return Err(CurvilinearPdeError::NonFiniteInput {
            field: "Helmholtz coefficient",
        });
    }
    if radius.panels() < 5 {
        return Err(CurvilinearPdeError::GridTooSmall {
            panels: radius.panels(),
            minimum: 5,
        });
    }
    let first_nodes = colatitude.nodes()?;
    let second_nodes = radius.nodes()?;
    validate_grid(rhs, first_nodes, second_nodes)?;
    colatitude_boundary.validate(colatitude, second_nodes)?;
    radial_boundary.validate(first_nodes)?;
    validate_centered_combination(
        colatitude,
        radius,
        coefficient,
        colatitude_boundary,
        radial_boundary,
    )?;
    validate_corners(
        colatitude_boundary,
        radial_boundary,
        first_nodes,
        second_nodes,
    )
}

fn validate_staggered(
    colatitude: StaggeredColatitudeAxis,
    radius: StaggeredRadialAxis,
    coefficient: f32,
    rhs: &FishpackGrid2,
    colatitude_boundary: &ColatitudeBoundary,
    radial_boundary: &RadialBoundary,
) -> Result<(), CurvilinearPdeError> {
    if !coefficient.is_finite() {
        return Err(CurvilinearPdeError::NonFiniteInput {
            field: "Helmholtz coefficient",
        });
    }
    if colatitude.points() < 5 {
        return Err(CurvilinearPdeError::GridTooSmall {
            panels: colatitude.points(),
            minimum: 5,
        });
    }
    if radius.points() < 5 {
        return Err(CurvilinearPdeError::GridTooSmall {
            panels: radius.points(),
            minimum: 5,
        });
    }
    validate_grid(rhs, colatitude.points(), radius.points())?;
    colatitude_boundary.validate_staggered(colatitude, radius.points())?;
    radial_boundary.validate(colatitude.points())?;
    let colatitude_code = colatitude_boundary.code();
    if colatitude.includes_north_pole() && !matches!(colatitude_code, 5 | 6 | 9)
        || colatitude.includes_south_pole() && !matches!(colatitude_code, 7..=9)
    {
        return Err(CurvilinearPdeError::UnsupportedBoundaryCombination { routine: "HSTCSP" });
    }
    if radial_boundary.is_axis() && radius.lower() != 0.0 {
        return Err(CurvilinearPdeError::AxisBoundaryRequiresZeroRadius);
    }
    if radial_boundary.is_axis() && matches!(colatitude_code, 1 | 2 | 4 | 5 | 7) {
        return Err(CurvilinearPdeError::UnsupportedBoundaryCombination { routine: "HSTCSP" });
    }
    if radial_boundary.is_axis() && coefficient != 0.0 {
        return Err(CurvilinearPdeError::UnsupportedBoundaryCombination { routine: "HSTCSP" });
    }
    Ok(())
}

fn validate_centered_combination(
    colatitude: ColatitudeAxis,
    radius: RadialAxis,
    coefficient: f32,
    colatitude_boundary: &ColatitudeBoundary,
    radial_boundary: &RadialBoundary,
) -> Result<(), CurvilinearPdeError> {
    let colatitude_code = colatitude_boundary.code();
    if colatitude_boundary.unspecified_pole() && coefficient != 0.0 {
        return Err(CurvilinearPdeError::UnsupportedBoundaryCombination { routine: "HWSCSP" });
    }
    if radial_boundary.is_axis() && radius.lower() != 0.0 {
        return Err(CurvilinearPdeError::AxisBoundaryRequiresZeroRadius);
    }
    if radial_boundary.is_axis() && coefficient != 0.0 {
        return Err(CurvilinearPdeError::UnsupportedBoundaryCombination { routine: "HWSCSP" });
    }
    if radial_boundary.is_axis() && matches!(colatitude_code, 1 | 2 | 4 | 5 | 7) {
        return Err(CurvilinearPdeError::UnsupportedBoundaryCombination { routine: "HWSCSP" });
    }
    // The two pole tests above are already enforced by ColatitudeBoundary;
    // keep this assertion explicit because HWSCSP compares against PIMACH.
    if colatitude.includes_south_pole() && colatitude.upper() != SLATEC_PI {
        return Err(CurvilinearPdeError::InvalidAxis);
    }
    Ok(())
}

fn validate_grid(
    rhs: &FishpackGrid2,
    first_nodes: usize,
    second_nodes: usize,
) -> Result<(), CurvilinearPdeError> {
    if rhs.first_nodes() != first_nodes || rhs.second_nodes() != second_nodes {
        return Err(CurvilinearPdeError::InvalidGridShape {
            expected: first_nodes
                .checked_mul(second_nodes)
                .ok_or(CurvilinearPdeError::DimensionOverflow)?,
            actual: rhs.values().len(),
        });
    }
    if rhs.values().iter().any(|value| !value.is_finite()) {
        return Err(CurvilinearPdeError::NonFiniteInput {
            field: "right-hand side",
        });
    }
    Ok(())
}

fn validate_corners(
    colatitude: &ColatitudeBoundary,
    radial: &RadialBoundary,
    first_nodes: usize,
    second_nodes: usize,
) -> Result<(), CurvilinearPdeError> {
    let theta_lower = colatitude.lower_value();
    let theta_upper = colatitude.upper_value();
    let radius_lower = radial.lower_value();
    let radius_upper = radial.upper_value();
    if theta_lower
        .zip(radius_lower)
        .is_some_and(|(theta, radius)| theta[0] != radius[0])
        || theta_lower
            .zip(radius_upper)
            .is_some_and(|(theta, radius)| theta[second_nodes - 1] != radius[0])
        || theta_upper
            .zip(radius_lower)
            .is_some_and(|(theta, radius)| theta[0] != radius[first_nodes - 1])
        || theta_upper
            .zip(radius_upper)
            .is_some_and(|(theta, radius)| theta[second_nodes - 1] != radius[first_nodes - 1])
    {
        return Err(CurvilinearPdeError::InconsistentCornerValues);
    }
    Ok(())
}

fn apply_centered_edges(
    first_nodes: usize,
    second_nodes: usize,
    values: &mut [f32],
    colatitude: &ColatitudeBoundary,
    radial: &RadialBoundary,
) {
    if let Some(lower) = colatitude.lower_value() {
        for second in 0..second_nodes {
            values[second * first_nodes] = lower[second];
        }
    }
    if let Some(upper) = colatitude.upper_value() {
        for second in 0..second_nodes {
            values[second * first_nodes + first_nodes - 1] = upper[second];
        }
    }
    if let Some(lower) = radial.lower_value() {
        values[..first_nodes].copy_from_slice(lower);
    }
    if let Some(upper) = radial.upper_value() {
        values[(second_nodes - 1) * first_nodes..].copy_from_slice(upper);
    }
}

fn as_fortran(value: usize) -> Result<FortranInteger, CurvilinearPdeError> {
    FortranInteger::try_from(value).map_err(|_| CurvilinearPdeError::DimensionOverflow)
}

fn centered_workspace_len(
    colatitude_panels: usize,
    radial_panels: usize,
    radial_boundary: FortranInteger,
) -> Result<usize, CurvilinearPdeError> {
    let radial_unknowns = match radial_boundary {
        1 | 5 => radial_panels.checked_sub(1),
        2 | 4 | 6 => Some(radial_panels),
        3 => radial_panels.checked_add(1),
        _ => None,
    }
    .ok_or(CurvilinearPdeError::DimensionOverflow)?;
    let k = (usize::BITS - radial_unknowns.leading_zeros()) as usize;
    let l = 1_usize
        .checked_shl((k + 1) as u32)
        .ok_or(CurvilinearPdeError::DimensionOverflow)?;
    k.checked_sub(2)
        .and_then(|value| value.checked_mul(l))
        .and_then(|value| value.checked_add(k))
        .and_then(|value| {
            colatitude_panels
                .checked_add(radial_panels)
                .and_then(|sum| sum.checked_mul(5))
                .and_then(|other| value.checked_add(other))
        })
        .and_then(|value| {
            radial_panels
                .checked_mul(2)
                .zip(colatitude_panels.checked_mul(6))
                .map(|(radial, colatitude)| radial.max(colatitude))
                .and_then(|other| value.checked_add(other))
        })
        .and_then(|value| value.checked_add(23))
        .ok_or(CurvilinearPdeError::DimensionOverflow)
}

fn staggered_workspace_len(
    colatitude_points: usize,
    radial_points: usize,
) -> Result<usize, CurvilinearPdeError> {
    let k = (usize::BITS - radial_points.leading_zeros()) as usize;
    let l = 1_usize
        .checked_shl((k + 1) as u32)
        .ok_or(CurvilinearPdeError::DimensionOverflow)?;
    k.checked_sub(2)
        .and_then(|value| value.checked_mul(l))
        .and_then(|value| value.checked_add(k))
        .and_then(|value| {
            radial_points
                .checked_mul(2)
                .zip(colatitude_points.checked_mul(6))
                .map(|(radial, colatitude)| radial.max(colatitude))
                .and_then(|other| value.checked_add(other))
        })
        .and_then(|value| {
            radial_points
                .checked_add(colatitude_points)
                .and_then(|sum| sum.checked_mul(4))
                .and_then(|other| value.checked_add(other))
        })
        .and_then(|value| value.checked_add(5))
        .ok_or(CurvilinearPdeError::DimensionOverflow)
}

fn zeroed(length: usize) -> Result<Vec<f32>, CurvilinearPdeError> {
    let mut values = Vec::new();
    values
        .try_reserve_exact(length)
        .map_err(|_| CurvilinearPdeError::AllocationFailed)?;
    values.resize(length, 0.0);
    Ok(values)
}

#[allow(clippy::too_many_arguments)]
fn native_result(
    routine: &'static str,
    first_nodes: usize,
    second_nodes: usize,
    values: Vec<f32>,
    perturbation: f32,
    ierror: FortranInteger,
    workspace: Vec<f32>,
    warning_code: FortranInteger,
) -> Result<CurvilinearPdeSolution, CurvilinearPdeError> {
    if ierror != 0 && ierror != warning_code {
        return Err(CurvilinearPdeError::NativeFailure {
            routine,
            code: ierror,
        });
    }
    if !perturbation.is_finite() {
        return Err(CurvilinearPdeError::NativeFailure {
            routine,
            code: ierror,
        });
    }
    let reported = workspace[0];
    if !reported.is_finite() || reported < 1.0 || reported > workspace.len() as f32 {
        return Err(CurvilinearPdeError::InconsistentNativeWorkspace {
            reported,
            allocated: workspace.len(),
        });
    }
    Ok(CurvilinearPdeSolution::from_native(
        FishpackGrid2::new(first_nodes, second_nodes, values)?,
        perturbation,
        if ierror == 0 {
            NativeCurvilinearPdeStatus::Success
        } else {
            NativeCurvilinearPdeStatus::CoefficientWarning { code: ierror }
        },
    ))
}

#[cfg(test)]
mod tests {
    use alloc::vec;

    use super::super::fishpack_cylindrical_polar::{FishpackGrid2, RadialAxis, RadialBoundary};
    use super::super::fishpack_spherical::{ColatitudeAxis, ColatitudeBoundary};
    use super::AxisymmetricSphericalHelmholtz2d;

    #[cfg(feature = "fishpack-spherical-native-tests")]
    use super::{
        StaggeredAxisymmetricSphericalHelmholtz2d, StaggeredColatitudeAxis, StaggeredRadialAxis,
    };

    #[cfg(feature = "fishpack-spherical-native-tests")]
    #[test]
    fn native_axisymmetric_constant_solution_is_manufactured() {
        let problem = AxisymmetricSphericalHelmholtz2d::new(
            ColatitudeAxis::full_sphere(6).unwrap(),
            RadialAxis::new(1.0, 2.0, 6).unwrap(),
            0.0,
            FishpackGrid2::zeros(7, 7).unwrap(),
            ColatitudeBoundary::BothPoles,
            RadialBoundary::Dirichlet {
                lower: vec![1.0; 7],
                upper: vec![1.0; 7],
            },
        )
        .unwrap();
        let result = problem.solve().unwrap();
        assert!(
            result
                .values()
                .values()
                .iter()
                .all(|value| (*value - 1.0).abs() < 5.0e-4)
        );
    }

    #[cfg(feature = "fishpack-spherical-native-tests")]
    #[test]
    fn native_staggered_axisymmetric_constant_solution_is_manufactured() {
        let problem = StaggeredAxisymmetricSphericalHelmholtz2d::new(
            StaggeredColatitudeAxis::full_sphere(6).unwrap(),
            StaggeredRadialAxis::new(1.0, 2.0, 6).unwrap(),
            0.0,
            FishpackGrid2::zeros(6, 6).unwrap(),
            ColatitudeBoundary::BothPoles,
            RadialBoundary::Dirichlet {
                lower: vec![1.0; 6],
                upper: vec![1.0; 6],
            },
        )
        .unwrap();
        let result = problem.solve().unwrap();
        assert!(
            result
                .values()
                .values()
                .iter()
                .all(|value| (*value - 1.0).abs() < 5.0e-4)
        );
    }

    #[cfg(feature = "fishpack-spherical-native-tests")]
    #[test]
    fn native_axisymmetric_coefficient_origin_and_singular_modes_are_verified() {
        let coefficient = 0.5;
        let mut coefficient_rhs = vec![0.0; 121];
        for radial in 0..=10 {
            let radius = 1.0 + radial as f32 / 10.0;
            for colatitude in 0..=10 {
                let theta = 0.5 + 2.0 * colatitude as f32 / 10.0;
                coefficient_rhs[colatitude + 11 * radial] =
                    coefficient / (radius * theta.sin()).powi(2);
            }
        }
        let coefficient_problem = AxisymmetricSphericalHelmholtz2d::new(
            ColatitudeAxis::new(0.5, 2.5, 10).unwrap(),
            RadialAxis::new(1.0, 2.0, 10).unwrap(),
            coefficient,
            FishpackGrid2::new(11, 11, coefficient_rhs).unwrap(),
            ColatitudeBoundary::Dirichlet {
                lower: vec![1.0; 11],
                upper: vec![1.0; 11],
            },
            RadialBoundary::Dirichlet {
                lower: vec![1.0; 11],
                upper: vec![1.0; 11],
            },
        )
        .unwrap()
        .solve()
        .unwrap();
        assert!(
            coefficient_problem
                .values()
                .values()
                .iter()
                .all(|value| (*value - 1.0).abs() < 6.0e-4)
        );

        let origin_problem = AxisymmetricSphericalHelmholtz2d::new(
            ColatitudeAxis::full_sphere(10).unwrap(),
            RadialAxis::new(0.0, 2.0, 10).unwrap(),
            0.0,
            FishpackGrid2::zeros(11, 11).unwrap(),
            ColatitudeBoundary::BothPoles,
            RadialBoundary::AxisDirichlet {
                outer: vec![1.0; 11],
            },
        )
        .unwrap()
        .solve()
        .unwrap();
        assert!(
            origin_problem
                .values()
                .values()
                .iter()
                .all(|value| (*value - 1.0).abs() < 8.0e-4)
        );

        let singular = AxisymmetricSphericalHelmholtz2d::new(
            ColatitudeAxis::full_sphere(8).unwrap(),
            RadialAxis::new(1.0, 2.0, 8).unwrap(),
            0.0,
            FishpackGrid2::new(9, 9, vec![1.5; 81]).unwrap(),
            ColatitudeBoundary::BothPoles,
            RadialBoundary::Neumann {
                lower_derivative: vec![0.0; 9],
                upper_derivative: vec![0.0; 9],
            },
        )
        .unwrap()
        .solve()
        .unwrap();
        assert!((singular.perturbation() - 1.5).abs() < 4.0e-4);
    }
}