math-fem 0.3.5

Multigrid FEM solver for the Helmholtz equation
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
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
//! Mesh generators for common domains
//!
//! Provides functions to create structured meshes for rectangles, boxes, and circles.

use super::types::{BoundaryType, ElementType, Mesh, Point};
use std::collections::HashMap;

/// Generate a rectangular mesh with triangular elements
pub fn rectangular_mesh_triangles(
    x_min: f64,
    x_max: f64,
    y_min: f64,
    y_max: f64,
    nx: usize,
    ny: usize,
) -> Mesh {
    let mut mesh = Mesh::new(2);

    let dx = (x_max - x_min) / nx as f64;
    let dy = (y_max - y_min) / ny as f64;

    // Create nodes
    for j in 0..=ny {
        for i in 0..=nx {
            let x = x_min + i as f64 * dx;
            let y = y_min + j as f64 * dy;
            mesh.add_node(Point::new_2d(x, y));
        }
    }

    // Create triangular elements (2 triangles per cell)
    for j in 0..ny {
        for i in 0..nx {
            let n00 = j * (nx + 1) + i;
            let n10 = n00 + 1;
            let n01 = n00 + (nx + 1);
            let n11 = n01 + 1;

            // Two triangles per cell
            mesh.add_element(ElementType::Triangle, vec![n00, n10, n11]);
            mesh.add_element(ElementType::Triangle, vec![n00, n11, n01]);
        }
    }

    // Detect boundaries
    mesh.detect_boundaries();

    // Set boundary conditions based on position
    let tol = 1e-10;
    mesh.set_boundary_condition(BoundaryType::Dirichlet, 1, |points| {
        points.iter().all(|p| (p.x - x_min).abs() < tol)
    });
    mesh.set_boundary_condition(BoundaryType::Dirichlet, 2, |points| {
        points.iter().all(|p| (p.x - x_max).abs() < tol)
    });
    mesh.set_boundary_condition(BoundaryType::Neumann, 3, |points| {
        points.iter().all(|p| (p.y - y_min).abs() < tol)
    });
    mesh.set_boundary_condition(BoundaryType::Neumann, 4, |points| {
        points.iter().all(|p| (p.y - y_max).abs() < tol)
    });

    mesh
}

/// Generate a rectangular mesh with quadrilateral elements
pub fn rectangular_mesh_quads(
    x_min: f64,
    x_max: f64,
    y_min: f64,
    y_max: f64,
    nx: usize,
    ny: usize,
) -> Mesh {
    let mut mesh = Mesh::new(2);

    let dx = (x_max - x_min) / nx as f64;
    let dy = (y_max - y_min) / ny as f64;

    // Create nodes
    for j in 0..=ny {
        for i in 0..=nx {
            let x = x_min + i as f64 * dx;
            let y = y_min + j as f64 * dy;
            mesh.add_node(Point::new_2d(x, y));
        }
    }

    // Create quadrilateral elements
    for j in 0..ny {
        for i in 0..nx {
            let n00 = j * (nx + 1) + i;
            let n10 = n00 + 1;
            let n01 = n00 + (nx + 1);
            let n11 = n01 + 1;

            mesh.add_element(ElementType::Quadrilateral, vec![n00, n10, n11, n01]);
        }
    }

    mesh.detect_boundaries();
    mesh
}

/// Generate a box mesh with tetrahedral elements
#[allow(clippy::too_many_arguments)]
pub fn box_mesh_tetrahedra(
    x_min: f64,
    x_max: f64,
    y_min: f64,
    y_max: f64,
    z_min: f64,
    z_max: f64,
    nx: usize,
    ny: usize,
    nz: usize,
) -> Mesh {
    let mut mesh = Mesh::new(3);

    let dx = (x_max - x_min) / nx as f64;
    let dy = (y_max - y_min) / ny as f64;
    let dz = (z_max - z_min) / nz as f64;

    // Create nodes
    for k in 0..=nz {
        for j in 0..=ny {
            for i in 0..=nx {
                let x = x_min + i as f64 * dx;
                let y = y_min + j as f64 * dy;
                let z = z_min + k as f64 * dz;
                mesh.add_node(Point::new_3d(x, y, z));
            }
        }
    }

    // Node indexing function
    let node_idx =
        |i: usize, j: usize, k: usize| -> usize { k * (ny + 1) * (nx + 1) + j * (nx + 1) + i };

    // Create tetrahedral elements (6 tetrahedra per cube)
    for k in 0..nz {
        for j in 0..ny {
            for i in 0..nx {
                // 8 corners of the cube
                let n000 = node_idx(i, j, k);
                let n100 = node_idx(i + 1, j, k);
                let n010 = node_idx(i, j + 1, k);
                let n110 = node_idx(i + 1, j + 1, k);
                let n001 = node_idx(i, j, k + 1);
                let n101 = node_idx(i + 1, j, k + 1);
                let n011 = node_idx(i, j + 1, k + 1);
                let n111 = node_idx(i + 1, j + 1, k + 1);

                // Divide cube into 6 tetrahedra (Kuhn triangulation)
                mesh.add_element(ElementType::Tetrahedron, vec![n000, n100, n110, n111]);
                mesh.add_element(ElementType::Tetrahedron, vec![n000, n110, n010, n111]);
                mesh.add_element(ElementType::Tetrahedron, vec![n000, n010, n011, n111]);
                mesh.add_element(ElementType::Tetrahedron, vec![n000, n011, n001, n111]);
                mesh.add_element(ElementType::Tetrahedron, vec![n000, n001, n101, n111]);
                mesh.add_element(ElementType::Tetrahedron, vec![n000, n101, n100, n111]);
            }
        }
    }

    mesh.detect_boundaries();
    mesh
}

/// Generate a box mesh with hexahedral elements
#[allow(clippy::too_many_arguments)]
pub fn box_mesh_hexahedra(
    x_min: f64,
    x_max: f64,
    y_min: f64,
    y_max: f64,
    z_min: f64,
    z_max: f64,
    nx: usize,
    ny: usize,
    nz: usize,
) -> Mesh {
    let mut mesh = Mesh::new(3);

    let dx = (x_max - x_min) / nx as f64;
    let dy = (y_max - y_min) / ny as f64;
    let dz = (z_max - z_min) / nz as f64;

    // Create nodes
    for k in 0..=nz {
        for j in 0..=ny {
            for i in 0..=nx {
                let x = x_min + i as f64 * dx;
                let y = y_min + j as f64 * dy;
                let z = z_min + k as f64 * dz;
                mesh.add_node(Point::new_3d(x, y, z));
            }
        }
    }

    // Node indexing function
    let node_idx =
        |i: usize, j: usize, k: usize| -> usize { k * (ny + 1) * (nx + 1) + j * (nx + 1) + i };

    // Create hexahedral elements
    for k in 0..nz {
        for j in 0..ny {
            for i in 0..nx {
                let n000 = node_idx(i, j, k);
                let n100 = node_idx(i + 1, j, k);
                let n010 = node_idx(i, j + 1, k);
                let n110 = node_idx(i + 1, j + 1, k);
                let n001 = node_idx(i, j, k + 1);
                let n101 = node_idx(i + 1, j, k + 1);
                let n011 = node_idx(i, j + 1, k + 1);
                let n111 = node_idx(i + 1, j + 1, k + 1);

                mesh.add_element(
                    ElementType::Hexahedron,
                    vec![n000, n100, n110, n010, n001, n101, n111, n011],
                );
            }
        }
    }

    mesh.detect_boundaries();
    mesh
}

/// Generate a circular mesh with triangular elements
pub fn circular_mesh_triangles(
    center_x: f64,
    center_y: f64,
    radius: f64,
    n_radial: usize,
    n_angular: usize,
) -> Mesh {
    let mut mesh = Mesh::new(2);

    // Center node
    mesh.add_node(Point::new_2d(center_x, center_y));

    // Radial layers of nodes
    for r in 1..=n_radial {
        let rad = radius * (r as f64) / (n_radial as f64);
        for a in 0..n_angular {
            let theta = 2.0 * std::f64::consts::PI * (a as f64) / (n_angular as f64);
            let x = center_x + rad * theta.cos();
            let y = center_y + rad * theta.sin();
            mesh.add_node(Point::new_2d(x, y));
        }
    }

    // Inner ring (triangles from center)
    for a in 0..n_angular {
        let n1 = 1 + a;
        let n2 = 1 + (a + 1) % n_angular;
        mesh.add_element(ElementType::Triangle, vec![0, n1, n2]);
    }

    // Outer rings (quadrilaterals split into triangles)
    for r in 1..n_radial {
        let offset_inner = 1 + (r - 1) * n_angular;
        let offset_outer = 1 + r * n_angular;
        for a in 0..n_angular {
            let n00 = offset_inner + a;
            let n10 = offset_inner + (a + 1) % n_angular;
            let n01 = offset_outer + a;
            let n11 = offset_outer + (a + 1) % n_angular;

            mesh.add_element(ElementType::Triangle, vec![n00, n10, n11]);
            mesh.add_element(ElementType::Triangle, vec![n00, n11, n01]);
        }
    }

    mesh.detect_boundaries();

    // Set outer boundary
    let tol = radius * 0.001;
    mesh.set_boundary_condition(BoundaryType::Dirichlet, 1, |points| {
        let avg_r = points
            .iter()
            .map(|p| ((p.x - center_x).powi(2) + (p.y - center_y).powi(2)).sqrt())
            .sum::<f64>()
            / points.len() as f64;
        (avg_r - radius).abs() < tol
    });

    mesh
}

/// Generate an annular (ring) mesh with triangular elements
pub fn annular_mesh_triangles(
    center_x: f64,
    center_y: f64,
    inner_radius: f64,
    outer_radius: f64,
    n_radial: usize,
    n_angular: usize,
) -> Mesh {
    let mut mesh = Mesh::new(2);

    // Create nodes in radial layers from inner to outer radius
    let dr = (outer_radius - inner_radius) / (n_radial as f64);

    for r in 0..=n_radial {
        let rad = inner_radius + r as f64 * dr;
        for a in 0..n_angular {
            let theta = 2.0 * std::f64::consts::PI * (a as f64) / (n_angular as f64);
            let x = center_x + rad * theta.cos();
            let y = center_y + rad * theta.sin();
            mesh.add_node(Point::new_2d(x, y));
        }
    }

    // Create triangular elements (2 per quad cell)
    for r in 0..n_radial {
        let offset_inner = r * n_angular;
        let offset_outer = (r + 1) * n_angular;
        for a in 0..n_angular {
            let n00 = offset_inner + a;
            let n10 = offset_inner + (a + 1) % n_angular;
            let n01 = offset_outer + a;
            let n11 = offset_outer + (a + 1) % n_angular;

            mesh.add_element(ElementType::Triangle, vec![n00, n10, n11]);
            mesh.add_element(ElementType::Triangle, vec![n00, n11, n01]);
        }
    }

    mesh.detect_boundaries();

    // Set inner boundary (obstacle) - tag 1
    let tol = inner_radius * 0.01;
    mesh.set_boundary_condition(BoundaryType::Dirichlet, 1, |points| {
        let avg_r = points
            .iter()
            .map(|p| ((p.x - center_x).powi(2) + (p.y - center_y).powi(2)).sqrt())
            .sum::<f64>()
            / points.len() as f64;
        (avg_r - inner_radius).abs() < tol
    });

    // Set outer boundary (far-field) - tag 2
    let tol_outer = outer_radius * 0.01;
    mesh.set_boundary_condition(BoundaryType::Dirichlet, 2, |points| {
        let avg_r = points
            .iter()
            .map(|p| ((p.x - center_x).powi(2) + (p.y - center_y).powi(2)).sqrt())
            .sum::<f64>()
            / points.len() as f64;
        (avg_r - outer_radius).abs() < tol_outer
    });

    mesh
}

/// Generate a spherical shell mesh using extruded icosphere (Tetrahedra)
///
/// Creates a 3D volume mesh between two concentric spheres.
/// Based on subdivisions of an icosahedron, extruded radially.
pub fn spherical_shell_mesh_tetrahedra(
    center_x: f64,
    center_y: f64,
    center_z: f64,
    inner_radius: f64,
    outer_radius: f64,
    subdivisions: usize,
    n_layers: usize,
) -> Mesh {
    let mut mesh = Mesh::new(3);

    // 1. Generate unit icosphere surface (nodes and triangles)
    let (base_nodes, base_tris) = generate_icosphere(subdivisions);
    let num_surface_nodes = base_nodes.len();

    // 2. Create nodes for all layers
    let dr = (outer_radius - inner_radius) / n_layers as f64;
    for l in 0..=n_layers {
        let r = inner_radius + l as f64 * dr;
        for p in &base_nodes {
            mesh.add_node(Point::new_3d(
                center_x + p.x * r,
                center_y + p.y * r,
                center_z + p.z * r,
            ));
        }
    }

    // 3. Create elements connecting layers
    for l in 0..n_layers {
        let offset_lower = l * num_surface_nodes;
        let offset_upper = (l + 1) * num_surface_nodes;

        for tri in &base_tris {
            let u0 = offset_lower + tri[0];
            let u1 = offset_lower + tri[1];
            let u2 = offset_lower + tri[2];

            let v0 = offset_upper + tri[0];
            let v1 = offset_upper + tri[1];
            let v2 = offset_upper + tri[2];

            // Split prism (u0,u1,u2)-(v0,v1,v2) into 3 tetrahedra
            // We use a consistent split based on sorting the base indices
            split_prism_consistent(&mut mesh, [u0, u1, u2], [v0, v1, v2]);
        }
    }

    mesh.detect_boundaries();

    // Set BCs
    let tol = inner_radius * 0.01;
    mesh.set_boundary_condition(BoundaryType::Dirichlet, 1, |pts| {
        // Inner
        pts[0].distance(&Point::new_3d(center_x, center_y, center_z)) < inner_radius + tol
    });
    mesh.set_boundary_condition(BoundaryType::Dirichlet, 2, |pts| {
        // Outer
        pts[0].distance(&Point::new_3d(center_x, center_y, center_z)) > outer_radius - tol
    });

    mesh
}

/// Helper: split a prism into 3 tetrahedra consistently
fn split_prism_consistent(mesh: &mut Mesh, lower: [usize; 3], upper: [usize; 3]) {
    // Sort base indices to define a local canonical order
    let mut perm = [0, 1, 2];
    perm.sort_by_key(|&i| lower[i]);

    let p0 = perm[0];
    let p1 = perm[1];
    let p2 = perm[2];

    // Canonical vertices
    let v0 = lower[p0];
    let v1 = lower[p1];
    let v2 = lower[p2];
    let v3 = upper[p0];
    let v4 = upper[p1];
    let v5 = upper[p2];

    // Standard split for 0<1<2
    // T1: 0-1-2-5 (base tri + top-max)
    mesh.add_element(ElementType::Tetrahedron, vec![v0, v1, v2, v5]);
    // T2: 0-1-5-4
    mesh.add_element(ElementType::Tetrahedron, vec![v0, v1, v5, v4]);
    // T3: 0-4-5-3
    mesh.add_element(ElementType::Tetrahedron, vec![v0, v4, v5, v3]);
}

/// Helper: generate unit icosphere
fn generate_icosphere(subdivisions: usize) -> (Vec<Point>, Vec<[usize; 3]>) {
    // Basic Icosahedron
    let t = (1.0 + 5.0f64.sqrt()) / 2.0;
    let mut nodes = vec![
        Point::new_3d(-1.0, t, 0.0),
        Point::new_3d(1.0, t, 0.0),
        Point::new_3d(-1.0, -t, 0.0),
        Point::new_3d(1.0, -t, 0.0),
        Point::new_3d(0.0, -1.0, t),
        Point::new_3d(0.0, 1.0, t),
        Point::new_3d(0.0, -1.0, -t),
        Point::new_3d(0.0, 1.0, -t),
        Point::new_3d(t, 0.0, -1.0),
        Point::new_3d(t, 0.0, 1.0),
        Point::new_3d(-t, 0.0, -1.0),
        Point::new_3d(-t, 0.0, 1.0),
    ];

    // Normalize
    for p in &mut nodes {
        let len = (p.x * p.x + p.y * p.y + p.z * p.z).sqrt();
        p.x /= len;
        p.y /= len;
        p.z /= len;
    }

    let mut tris = vec![
        [0, 11, 5],
        [0, 5, 1],
        [0, 1, 7],
        [0, 7, 10],
        [0, 10, 11],
        [1, 5, 9],
        [5, 11, 4],
        [11, 10, 2],
        [10, 7, 6],
        [7, 1, 8],
        [3, 9, 4],
        [3, 4, 2],
        [3, 2, 6],
        [3, 6, 8],
        [3, 8, 9],
        [4, 9, 5],
        [2, 4, 11],
        [6, 2, 10],
        [8, 6, 7],
        [9, 8, 1],
    ];

    // Subdivide
    for _ in 0..subdivisions {
        let mut new_tris = Vec::new();
        let mut mid_cache = HashMap::new();

        for tri in tris {
            let v0 = tri[0];
            let v1 = tri[1];
            let v2 = tri[2];

            // Get midpoints (and add to nodes if new)
            // Note: can't use closure easily with mutable borrows, using helper function pattern
            // But we need to define helper inside or outside.
            // Since this is a standalone function, we can put logic in loop.

            let a = get_middle_point(v0, v1, &mut nodes, &mut mid_cache);
            let b = get_middle_point(v1, v2, &mut nodes, &mut mid_cache);
            let c = get_middle_point(v2, v0, &mut nodes, &mut mid_cache);

            new_tris.push([v0, a, c]);
            new_tris.push([v1, b, a]);
            new_tris.push([v2, c, b]);
            new_tris.push([a, b, c]);
        }
        tris = new_tris;
    }

    (nodes, tris)
}

fn get_middle_point(
    p1: usize,
    p2: usize,
    nodes: &mut Vec<Point>,
    cache: &mut HashMap<(usize, usize), usize>,
) -> usize {
    let key = if p1 < p2 { (p1, p2) } else { (p2, p1) };
    if let Some(&idx) = cache.get(&key) {
        return idx;
    }

    let pt1 = nodes[p1];
    let pt2 = nodes[p2];
    let mut middle = Point::new_3d(
        (pt1.x + pt2.x) / 2.0,
        (pt1.y + pt2.y) / 2.0,
        (pt1.z + pt2.z) / 2.0,
    );

    let len = (middle.x * middle.x + middle.y * middle.y + middle.z * middle.z).sqrt();
    middle.x /= len;
    middle.y /= len;
    middle.z /= len;

    let idx = nodes.len();
    nodes.push(middle);
    cache.insert(key, idx);
    idx
}

/// Generate a unit square mesh with triangles
pub fn unit_square_triangles(n: usize) -> Mesh {
    rectangular_mesh_triangles(0.0, 1.0, 0.0, 1.0, n, n)
}

/// Generate a unit square mesh with quadrilaterals
pub fn unit_square_quads(n: usize) -> Mesh {
    rectangular_mesh_quads(0.0, 1.0, 0.0, 1.0, n, n)
}

/// Generate a unit cube mesh with tetrahedra
pub fn unit_cube_tetrahedra(n: usize) -> Mesh {
    box_mesh_tetrahedra(0.0, 1.0, 0.0, 1.0, 0.0, 1.0, n, n, n)
}

/// Generate a unit cube mesh with hexahedra
pub fn unit_cube_hexahedra(n: usize) -> Mesh {
    box_mesh_hexahedra(0.0, 1.0, 0.0, 1.0, 0.0, 1.0, n, n, n)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rectangular_mesh_triangles() {
        let mesh = rectangular_mesh_triangles(0.0, 1.0, 0.0, 1.0, 2, 2);

        // 3x3 = 9 nodes
        assert_eq!(mesh.num_nodes(), 9);
        // 2x2 cells x 2 triangles = 8 triangles
        assert_eq!(mesh.num_elements(), 8);
    }

    #[test]
    fn test_rectangular_mesh_quads() {
        let mesh = rectangular_mesh_quads(0.0, 1.0, 0.0, 1.0, 3, 3);

        assert_eq!(mesh.num_nodes(), 16);
        assert_eq!(mesh.num_elements(), 9);
    }

    #[test]
    fn test_box_mesh_tetrahedra() {
        let mesh = box_mesh_tetrahedra(0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 2, 2, 2);

        // 3x3x3 = 27 nodes
        assert_eq!(mesh.num_nodes(), 27);
        // 2x2x2 cubes x 6 tetrahedra = 48 tetrahedra
        assert_eq!(mesh.num_elements(), 48);
    }

    #[test]
    fn test_circular_mesh() {
        let mesh = circular_mesh_triangles(0.0, 0.0, 1.0, 3, 8);

        // Center + 3 rings of 8 nodes = 1 + 24 = 25 nodes
        assert_eq!(mesh.num_nodes(), 25);
    }

    #[test]
    fn test_annular_mesh() {
        let mesh = annular_mesh_triangles(0.0, 0.0, 0.5, 2.0, 4, 16);

        // (n_radial + 1) rings of n_angular nodes = 5 * 16 = 80 nodes
        assert_eq!(mesh.num_nodes(), 80);

        // n_radial * n_angular * 2 triangles = 4 * 16 * 2 = 128 elements
        assert_eq!(mesh.num_elements(), 128);

        // Should have boundaries on both inner and outer circles
        assert!(!mesh.boundaries.is_empty());
    }

    #[test]
    fn test_unit_square() {
        let mesh = unit_square_triangles(4);
        assert_eq!(mesh.num_nodes(), 25);
        assert_eq!(mesh.num_elements(), 32);
    }

    #[test]
    fn test_boundary_detection() {
        let mesh = rectangular_mesh_triangles(0.0, 1.0, 0.0, 1.0, 2, 2);

        // Should have 8 boundary edges (2 per side of square)
        assert_eq!(mesh.boundaries.len(), 8);
    }

    #[test]
    fn test_spherical_shell_mesh() {
        // Icosphere subdiv 0 = 12 nodes, 20 tris
        // 2 layers
        // Total nodes = 12 * 3 = 36 nodes
        // Total elems = 2 layers * 20 tris * 3 tets = 120 tets
        let mesh = spherical_shell_mesh_tetrahedra(0.0, 0.0, 0.0, 1.0, 2.0, 0, 2);

        assert_eq!(mesh.num_nodes(), 36);
        assert_eq!(mesh.num_elements(), 120);
        assert_eq!(mesh.dimension, 3);

        // Check boundaries
        // Outer + Inner = 20 + 20 = 40 boundary faces
        assert_eq!(mesh.boundaries.len(), 40);
    }
}