test_parallel_io/
test_parallel_io.rs

1use mpi::{collective::CommunicatorCollectives, environment::Universe, traits::Communicator};
2use ndelement::{ciarlet::CiarletElement, map::IdentityMap, types::ReferenceCellType};
3use ndgrid::{
4    grid::ParallelGridImpl,
5    traits::{Builder, Grid, ParallelBuilder, RONExportParallel, RONImportParallel},
6    types::GraphPartitioner,
7    SingleElementGrid, SingleElementGridBuilder,
8};
9
10fn create_single_element_grid_data(b: &mut SingleElementGridBuilder<f64>, n: usize) {
11    for y in 0..n {
12        for x in 0..n {
13            b.add_point(
14                y * n + x,
15                &[x as f64 / (n - 1) as f64, y as f64 / (n - 1) as f64, 0.0],
16            );
17        }
18    }
19
20    for i in 0..n - 1 {
21        for j in 0..n - 1 {
22            b.add_cell(
23                i * (n - 1) + j,
24                &[j * n + i, j * n + i + 1, j * n + i + n, j * n + i + n + 1],
25            );
26        }
27    }
28}
29
30fn example_single_element_grid<C: Communicator>(
31    comm: &C,
32    n: usize,
33) -> ParallelGridImpl<'_, C, SingleElementGrid<f64, CiarletElement<f64, IdentityMap>>> {
34    let rank = comm.rank();
35
36    let mut b = SingleElementGridBuilder::<f64>::new(3, (ReferenceCellType::Quadrilateral, 1));
37
38    if rank == 0 {
39        create_single_element_grid_data(&mut b, n);
40        b.create_parallel_grid_root(comm, GraphPartitioner::None)
41    } else {
42        b.create_parallel_grid(comm, 0)
43    }
44}
45
46/// Test that grids can be exported as RON in parallel
47fn test_parallel_export<C: Communicator>(comm: &C) {
48    let size = comm.size();
49
50    let n = 10;
51    let grid = example_single_element_grid(comm, n);
52    let filename = format!("_examples_parallel_io_{size}ranks.ron");
53    grid.export_as_ron(&filename);
54}
55
56/// Test that grids can be imported from RON in parallel
57fn test_parallel_import<C: Communicator>(comm: &C) {
58    use ndgrid::traits::ParallelGrid;
59
60    let size = comm.size();
61
62    let filename = format!("_examples_parallel_io_{size}ranks.ron");
63    let grid =
64        ParallelGridImpl::<'_, C, SingleElementGrid<f64, CiarletElement<f64, IdentityMap>>>::import_from_ron(
65            comm, &filename,
66        );
67
68    let n = 10;
69    let grid2 = example_single_element_grid(comm, n);
70
71    assert_eq!(
72        grid.local_grid().entity_count(ReferenceCellType::Point),
73        grid2.local_grid().entity_count(ReferenceCellType::Point)
74    );
75}
76
77/// Run tests
78fn main() {
79    let universe: Universe = mpi::initialize().unwrap();
80    let world = universe.world();
81    let rank = world.rank();
82
83    if rank == 0 {
84        println!("Testing parallel grid export");
85    }
86    test_parallel_export(&world);
87
88    world.barrier();
89
90    if rank == 0 {
91        println!("Testing parallel grid import");
92    }
93    test_parallel_import(&world);
94}