-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_2d.cpp
52 lines (45 loc) · 1.49 KB
/
main_2d.cpp
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
#include <torch/torch.h>
#include <iostream>
#include "weno5_advection.h"
#include "netcdf_output.h"
int main() {
// Check if Metal is available
torch::Device device = torch::kCPU;
if (torch::hasMPS()) {
std::cout << "MPS is available! Running on GPU." << std::endl;
device = torch::kMPS;
} else {
std::cout << "MPS is not available. Running on CPU." << std::endl;
}
// Domain and initial condition
int Cn = 10;
int nx = Cn;
int ny = Cn;
double Lx = 1.0;
double Ly = 1.0;
double dx = Lx / nx;
double dy = Ly / ny;
auto x = torch::linspace(0, Lx, nx);
auto y = torch::linspace(0, Ly, ny);
auto X = torch::meshgrid({x, y});
auto u = torch::exp(-100.0 * ((X[0] - 0.5).pow(2) + (X[1] - 0.5).pow(2)));
u.to(device);
output_to_netcdf(u, "initial_" + std::to_string(Cn) + ".nc");
// Velocity field
auto ax = torch::ones_like(u) * 1.0;
auto ay = torch::ones_like(u) * 1.0;
ax.to(device);
ay.to(device);
// Time-stepping parameters
double dt = 0.001; // Ensure CFL condition: dt < min(dx, dy) / max(a)
int num_steps = 1000; // Adjust the number of steps to control the simulation time
// Choose boundary condition
int bc = 1; // or ZERO_GRADIENT
// Time-stepping loop
for (int i = 0; i < num_steps; ++i) {
time_step_2d(u, ax, ay, dt, dx, dy, bc);
}
// Output the result
output_to_netcdf(u, "final_" + std::to_string(Cn) + ".nc");
return 0;
}