Debian Codegraph @

examples/simulations/gray-scott/simulation/gray-scott.h

raw
1 /*
2 * SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #ifndef __GRAY_SCOTT_H__
8 #define __GRAY_SCOTT_H__
9
10 #include <random>
11 #include <vector>
12
13 #include <mpi.h>
14
15 #include "settings.h"
16
17 class GrayScott
18 {
19 public:
20 // Dimension of process grid
21 size_t npx, npy, npz;
22 // Coordinate of this rank in process grid
23 size_t px, py, pz;
24 // Dimension of local array
25 size_t size_x, size_y, size_z;
26 // Offset of local array in the global array
27 size_t offset_x, offset_y, offset_z;
28
29 GrayScott(const Settings &settings, MPI_Comm comm);
30 ~GrayScott();
31
32 void init();
33 void iterate();
34 void restart(std::vector<double> &u, std::vector<double> &v);
35
36 const std::vector<double> &u_ghost() const;
37 const std::vector<double> &v_ghost() const;
38
39 std::vector<double> u_noghost() const;
40 std::vector<double> v_noghost() const;
41
42 void u_noghost(double *u_no_ghost) const;
43 void v_noghost(double *v_no_ghost) const;
44
45 protected:
46 Settings settings;
47
48 std::vector<double> u, v, u2, v2;
49
50 int rank, procs;
51 int west, east, up, down, north, south;
52 MPI_Comm comm;
53 MPI_Comm cart_comm;
54
55 // MPI datatypes for halo exchange
56 MPI_Datatype xy_face_type;
57 MPI_Datatype xz_face_type;
58 MPI_Datatype yz_face_type;
59
60 std::random_device rand_dev;
61 std::mt19937 mt_gen;
62 std::uniform_real_distribution<double> uniform_dist;
63
64 // Setup cartesian communicator data types
65 void init_mpi();
66 // Setup initial conditions
67 void init_field();
68
69 // Progess simulation for one timestep
70 void calc(const std::vector<double> &u, const std::vector<double> &v, std::vector<double> &u2,
71 std::vector<double> &v2);
72 // Compute reaction term for U
73 double calcU(double tu, double tv) const;
74 // Compute reaction term for V
75 double calcV(double tu, double tv) const;
76 // Compute laplacian of field s at (ix, iy, iz)
77 double laplacian(int ix, int iy, int iz, const std::vector<double> &s) const;
78
79 // Exchange faces with neighbors
80 void exchange(std::vector<double> &u, std::vector<double> &v) const;
81 // Exchange XY faces with north/south
82 void exchange_xy(std::vector<double> &local_data) const;
83 // Exchange XZ faces with up/down
84 void exchange_xz(std::vector<double> &local_data) const;
85 // Exchange YZ faces with west/east
86 void exchange_yz(std::vector<double> &local_data) const;
87
88 // Return a copy of data with ghosts removed
89 std::vector<double> data_noghost(const std::vector<double> &data) const;
90
91 // pointer version
92 void data_noghost(const std::vector<double> &data, double *no_ghost) const;
93
94 // Check if point is included in my subdomain
95 inline bool is_inside(int x, int y, int z) const
96 {
97 if (x < static_cast<int>(offset_x))
98 return false;
99 if (x >= static_cast<int>(offset_x + size_x))
100 return false;
101 if (y < static_cast<int>(offset_y))
102 return false;
103 if (y >= static_cast<int>(offset_y + size_y))
104 return false;
105 if (z < static_cast<int>(offset_z))
106 return false;
107 if (z >= static_cast<int>(offset_z + size_z))
108 return false;
109
110 return true;
111 }
112 // Convert global coordinate to local index
113 inline int g2i(int gx, int gy, int gz) const
114 {
115 const int x = static_cast<int>(gx - offset_x);
116 const int y = static_cast<int>(gy - offset_y);
117 const int z = static_cast<int>(gz - offset_z);
118
119 return l2i(x + 1, y + 1, z + 1);
120 }
121 // Convert local coordinate to local index
122 inline int l2i(int x, int y, int z) const
123 {
124 return static_cast<int>(x + y * (size_x + 2) + z * (size_x + 2) * (size_y + 2));
125 }
126
127 private:
128 void data_no_ghost_common(const std::vector<double> &data, double *data_no_ghost) const;
129 };
130
131 #endif