| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: Apache-2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "gray-scott.h" |
| 8 |
|
| 9 |
#include <mpi.h> |
| 10 |
#include <random> |
| 11 |
#include <stdexcept> // runtime_error |
| 12 |
#include <vector> |
| 13 |
|
| 14 |
GrayScott::GrayScott(const Settings &settings, MPI_Comm comm) |
| 15 |
: settings(settings), comm(comm), rand_dev(), mt_gen(rand_dev()), uniform_dist(-1.0, 1.0) |
| 16 |
{ |
| 17 |
} |
| 18 |
|
| 19 |
GrayScott::~GrayScott() |
| 20 |
{ |
| 21 |
MPI_Comm_free(&cart_comm); |
| 22 |
MPI_Type_free(&xy_face_type); |
| 23 |
MPI_Type_free(&xz_face_type); |
| 24 |
MPI_Type_free(&yz_face_type); |
| 25 |
} |
| 26 |
|
| 27 |
void GrayScott::init() |
| 28 |
{ |
| 29 |
init_mpi(); |
| 30 |
init_field(); |
| 31 |
} |
| 32 |
|
| 33 |
void GrayScott::iterate() |
| 34 |
{ |
| 35 |
exchange(u, v); |
| 36 |
calc(u, v, u2, v2); |
| 37 |
|
| 38 |
u.swap(u2); |
| 39 |
v.swap(v2); |
| 40 |
} |
| 41 |
|
| 42 |
void GrayScott::restart(std::vector<double> &u_in, std::vector<double> &v_in) |
| 43 |
{ |
| 44 |
auto expected_len = (size_x + 2) * (size_y + 2) * (size_z + 2); |
| 45 |
if (u_in.size() == expected_len) |
| 46 |
{ |
| 47 |
u = u_in; |
| 48 |
v = v_in; |
| 49 |
} |
| 50 |
else |
| 51 |
{ |
| 52 |
throw std::runtime_error("Restart with incompatible array size, expected " + |
| 53 |
std::to_string(expected_len) + " got " + |
| 54 |
std::to_string(u_in.size()) + " elements"); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
const std::vector<double> &GrayScott::u_ghost() const { return u; } |
| 59 |
|
| 60 |
const std::vector<double> &GrayScott::v_ghost() const { return v; } |
| 61 |
|
| 62 |
std::vector<double> GrayScott::u_noghost() const { return data_noghost(u); } |
| 63 |
|
| 64 |
std::vector<double> GrayScott::v_noghost() const { return data_noghost(v); } |
| 65 |
|
| 66 |
void GrayScott::u_noghost(double *u_no_ghost) const { data_noghost(u, u_no_ghost); } |
| 67 |
|
| 68 |
void GrayScott::v_noghost(double *v_no_ghost) const { data_noghost(v, v_no_ghost); } |
| 69 |
|
| 70 |
std::vector<double> GrayScott::data_noghost(const std::vector<double> &data) const |
| 71 |
{ |
| 72 |
std::vector<double> buf(size_x * size_y * size_z); |
| 73 |
data_no_ghost_common(data, buf.data()); |
| 74 |
return buf; |
| 75 |
} |
| 76 |
|
| 77 |
void GrayScott::data_noghost(const std::vector<double> &data, double *data_no_ghost) const |
| 78 |
{ |
| 79 |
data_no_ghost_common(data, data_no_ghost); |
| 80 |
} |
| 81 |
|
| 82 |
void GrayScott::init_field() |
| 83 |
{ |
| 84 |
const int V = static_cast<int>((size_x + 2) * (size_y + 2) * (size_z + 2)); |
| 85 |
u.resize(V, 1.0); |
| 86 |
v.resize(V, 0.0); |
| 87 |
u2.resize(V, 0.0); |
| 88 |
v2.resize(V, 0.0); |
| 89 |
|
| 90 |
const int d = 6; |
| 91 |
const auto settingsL = static_cast<int>(settings.L); |
| 92 |
for (int z = settingsL / 2 - d; z < settingsL / 2 + d; z++) |
| 93 |
{ |
| 94 |
for (int y = settingsL / 2 - d; y < settingsL / 2 + d; y++) |
| 95 |
{ |
| 96 |
for (int x = settingsL / 2 - d; x < settingsL / 2 + d; x++) |
| 97 |
{ |
| 98 |
if (!is_inside(x, y, z)) |
| 99 |
continue; |
| 100 |
int i = g2i(x, y, z); |
| 101 |
u[i] = 0.25; |
| 102 |
v[i] = 0.33; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
double GrayScott::calcU(double tu, double tv) const |
| 109 |
{ |
| 110 |
return -tu * tv * tv + settings.F * (1.0 - tu); |
| 111 |
} |
| 112 |
|
| 113 |
double GrayScott::calcV(double tu, double tv) const |
| 114 |
{ |
| 115 |
return tu * tv * tv - (settings.F + settings.k) * tv; |
| 116 |
} |
| 117 |
|
| 118 |
double GrayScott::laplacian(int x, int y, int z, const std::vector<double> &s) const |
| 119 |
{ |
| 120 |
double ts = 0.0; |
| 121 |
ts += s[l2i(x - 1, y, z)]; |
| 122 |
ts += s[l2i(x + 1, y, z)]; |
| 123 |
ts += s[l2i(x, y - 1, z)]; |
| 124 |
ts += s[l2i(x, y + 1, z)]; |
| 125 |
ts += s[l2i(x, y, z - 1)]; |
| 126 |
ts += s[l2i(x, y, z + 1)]; |
| 127 |
ts += -6.0 * s[l2i(x, y, z)]; |
| 128 |
|
| 129 |
return ts / 6.0; |
| 130 |
} |
| 131 |
|
| 132 |
void GrayScott::calc(const std::vector<double> &u, const std::vector<double> &v, |
| 133 |
std::vector<double> &u2, std::vector<double> &v2) |
| 134 |
{ |
| 135 |
for (int z = 1, sizeZ = static_cast<int>(size_z); z < sizeZ + 1; z++) |
| 136 |
{ |
| 137 |
for (int y = 1, sizeY = static_cast<int>(size_y); y < sizeY + 1; y++) |
| 138 |
{ |
| 139 |
for (int x = 1, sizeX = static_cast<int>(size_x); x < sizeX + 1; x++) |
| 140 |
{ |
| 141 |
const int i = l2i(x, y, z); |
| 142 |
double du = 0.0; |
| 143 |
double dv = 0.0; |
| 144 |
du = settings.Du * laplacian(x, y, z, u); |
| 145 |
dv = settings.Dv * laplacian(x, y, z, v); |
| 146 |
du += calcU(u[i], v[i]); |
| 147 |
dv += calcV(u[i], v[i]); |
| 148 |
du += settings.noise * uniform_dist(mt_gen); |
| 149 |
u2[i] = u[i] + du * settings.dt; |
| 150 |
v2[i] = v[i] + dv * settings.dt; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
void GrayScott::init_mpi() |
| 157 |
{ |
| 158 |
int dims[3] = {}; |
| 159 |
const int periods[3] = {1, 1, 1}; |
| 160 |
int coords[3] = {}; |
| 161 |
|
| 162 |
MPI_Comm_rank(comm, &rank); |
| 163 |
MPI_Comm_size(comm, &procs); |
| 164 |
|
| 165 |
MPI_Dims_create(procs, 3, dims); |
| 166 |
npx = dims[0]; |
| 167 |
npy = dims[1]; |
| 168 |
npz = dims[2]; |
| 169 |
|
| 170 |
MPI_Cart_create(comm, 3, dims, periods, 0, &cart_comm); |
| 171 |
MPI_Cart_coords(cart_comm, rank, 3, coords); |
| 172 |
px = coords[0]; |
| 173 |
py = coords[1]; |
| 174 |
pz = coords[2]; |
| 175 |
|
| 176 |
size_x = settings.L / npx; |
| 177 |
size_y = settings.L / npy; |
| 178 |
size_z = settings.L / npz; |
| 179 |
|
| 180 |
if (px < settings.L % npx) |
| 181 |
{ |
| 182 |
size_x++; |
| 183 |
} |
| 184 |
if (py < settings.L % npy) |
| 185 |
{ |
| 186 |
size_y++; |
| 187 |
} |
| 188 |
if (pz < settings.L % npz) |
| 189 |
{ |
| 190 |
size_z++; |
| 191 |
} |
| 192 |
|
| 193 |
offset_x = (settings.L / npx * px) + std::min(settings.L % npx, px); |
| 194 |
offset_y = (settings.L / npy * py) + std::min(settings.L % npy, py); |
| 195 |
offset_z = (settings.L / npz * pz) + std::min(settings.L % npz, pz); |
| 196 |
|
| 197 |
MPI_Cart_shift(cart_comm, 0, 1, &west, &east); |
| 198 |
MPI_Cart_shift(cart_comm, 1, 1, &down, &up); |
| 199 |
MPI_Cart_shift(cart_comm, 2, 1, &south, &north); |
| 200 |
|
| 201 |
// XY faces: size_x * (size_y + 2) |
| 202 |
MPI_Type_vector(static_cast<int>(size_y) + 2, static_cast<int>(size_x), |
| 203 |
static_cast<int>(size_x + 2), MPI_DOUBLE, &xy_face_type); |
| 204 |
MPI_Type_commit(&xy_face_type); |
| 205 |
|
| 206 |
// XZ faces: size_x * size_z |
| 207 |
MPI_Type_vector(static_cast<int>(size_z), static_cast<int>(size_x), |
| 208 |
static_cast<int>((size_x + 2) * (size_y + 2)), MPI_DOUBLE, &xz_face_type); |
| 209 |
MPI_Type_commit(&xz_face_type); |
| 210 |
|
| 211 |
// YZ faces: (size_y + 2) * (size_z + 2) |
| 212 |
MPI_Type_vector(static_cast<int>((size_y + 2) * (size_z + 2)), 1, static_cast<int>(size_x + 2), |
| 213 |
MPI_DOUBLE, &yz_face_type); |
| 214 |
MPI_Type_commit(&yz_face_type); |
| 215 |
} |
| 216 |
|
| 217 |
void GrayScott::exchange_xy(std::vector<double> &local_data) const |
| 218 |
{ |
| 219 |
MPI_Status st; |
| 220 |
|
| 221 |
// Send XY face z=size_z to north and receive z=0 from south |
| 222 |
MPI_Sendrecv(&local_data[l2i(1, 0, static_cast<int>(size_z))], 1, xy_face_type, north, 1, |
| 223 |
&local_data[l2i(1, 0, 0)], 1, xy_face_type, south, 1, cart_comm, &st); |
| 224 |
// Send XY face z=1 to south and receive z=size_z+1 from north |
| 225 |
MPI_Sendrecv(&local_data[l2i(1, 0, 1)], 1, xy_face_type, south, 1, |
| 226 |
&local_data[l2i(1, 0, static_cast<int>(size_z + 1))], 1, xy_face_type, north, 1, |
| 227 |
cart_comm, &st); |
| 228 |
} |
| 229 |
|
| 230 |
void GrayScott::exchange_xz(std::vector<double> &local_data) const |
| 231 |
{ |
| 232 |
MPI_Status st; |
| 233 |
|
| 234 |
// Send XZ face y=size_y to up and receive y=0 from down |
| 235 |
MPI_Sendrecv(&local_data[l2i(1, static_cast<int>(size_y), 1)], 1, xz_face_type, up, 2, |
| 236 |
&local_data[l2i(1, 0, 1)], 1, xz_face_type, down, 2, cart_comm, &st); |
| 237 |
// Send XZ face y=1 to down and receive y=size_y+1 from up |
| 238 |
MPI_Sendrecv(&local_data[l2i(1, 1, 1)], 1, xz_face_type, down, 2, |
| 239 |
&local_data[l2i(1, static_cast<int>(size_y + 1), 1)], 1, xz_face_type, up, 2, |
| 240 |
cart_comm, &st); |
| 241 |
} |
| 242 |
|
| 243 |
void GrayScott::exchange_yz(std::vector<double> &local_data) const |
| 244 |
{ |
| 245 |
MPI_Status st; |
| 246 |
|
| 247 |
// Send YZ face x=size_x to east and receive x=0 from west |
| 248 |
MPI_Sendrecv(&local_data[l2i(static_cast<int>(size_x), 0, 0)], 1, yz_face_type, east, 3, |
| 249 |
&local_data[l2i(0, 0, 0)], 1, yz_face_type, west, 3, cart_comm, &st); |
| 250 |
// Send YZ face x=1 to west and receive x=size_x+1 from east |
| 251 |
MPI_Sendrecv(&local_data[l2i(1, 0, 0)], 1, yz_face_type, west, 3, |
| 252 |
&local_data[l2i(static_cast<int>(size_x + 1), 0, 0)], 1, yz_face_type, east, 3, |
| 253 |
cart_comm, &st); |
| 254 |
} |
| 255 |
|
| 256 |
void GrayScott::exchange(std::vector<double> &u, std::vector<double> &v) const |
| 257 |
{ |
| 258 |
exchange_xy(u); |
| 259 |
exchange_xz(u); |
| 260 |
exchange_yz(u); |
| 261 |
|
| 262 |
exchange_xy(v); |
| 263 |
exchange_xz(v); |
| 264 |
exchange_yz(v); |
| 265 |
} |
| 266 |
|
| 267 |
void GrayScott::data_no_ghost_common(const std::vector<double> &data, double *data_no_ghost) const |
| 268 |
{ |
| 269 |
for (int z = 1, sizeZ = static_cast<int>(size_z); z < sizeZ + 1; z++) |
| 270 |
{ |
| 271 |
for (int y = 1, sizeY = static_cast<int>(size_y); y < sizeY + 1; y++) |
| 272 |
{ |
| 273 |
for (int x = 1, sizeX = static_cast<int>(size_x); x < sizeX + 1; x++) |
| 274 |
{ |
| 275 |
data_no_ghost[(x - 1) + (y - 1) * size_x + (z - 1) * size_x * size_y] = |
| 276 |
data[l2i(x, y, z)]; |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
} |