| 15 | } |
| 16 | |
| 17 | static void swe(bool console) { |
| 18 | // Grid length, number and spacing |
| 19 | const unsigned Lx = 1600, nx = Lx + 1; |
| 20 | const unsigned Ly = 1600, ny = Ly + 1; |
| 21 | const float dx = Lx / (nx - 1); |
| 22 | const float dy = Ly / (ny - 1); |
| 23 | |
| 24 | array ZERO = constant(0, nx, ny); |
| 25 | array um = ZERO, vm = ZERO; |
| 26 | unsigned io = (unsigned)floor(Lx / 6.0f), jo = (unsigned)floor(Ly / 6.0f), |
| 27 | k = 15; |
| 28 | array x = tile(range(nx), 1, ny); |
| 29 | array y = tile(range(dim4(1, ny), 1), nx, 1); |
| 30 | |
| 31 | // initial condition |
| 32 | array etam = |
| 33 | 0.01f * exp((-((x - io) * (x - io) + (y - jo) * (y - jo))) / (k * k)); |
| 34 | float m_eta = max<float>(etam); |
| 35 | array eta = etam; |
| 36 | float dt = 0.5; |
| 37 | |
| 38 | // conv kernels |
| 39 | float h_diff_kernel[] = {9.81f * (dt / dx), 0, -9.81f * (dt / dx)}; |
| 40 | float h_lap_kernel[] = {0, 1, 0, 1, -4, 1, 0, 1, 0}; |
| 41 | |
| 42 | array h_diff_kernel_arr(3, h_diff_kernel); |
| 43 | array h_lap_kernel_arr(3, 3, h_lap_kernel); |
| 44 | |
| 45 | if (!console) { |
| 46 | win = new Window(1536, 768, "Shallow Water Equations"); |
| 47 | win->grid(2, 2); |
| 48 | } |
| 49 | |
| 50 | unsigned iter = 0; |
| 51 | unsigned random_interval = 30; |
| 52 | |
| 53 | while (!win->close()) { |
| 54 | if (iter > 2000) { |
| 55 | // Initial condition |
| 56 | etam = 0.01f * exp((-((x - io) * (x - io) + (y - jo) * (y - jo))) / |
| 57 | (k * k)); |
| 58 | m_eta = max<float>(etam); |
| 59 | eta = etam; |
| 60 | iter = 0; |
| 61 | } |
| 62 | |
| 63 | // raindrops |
| 64 | if (iter % 100 == 0 || iter % 130 == 0 || iter % random_interval == 0) { |
| 65 | unsigned io = (unsigned)floor(rand() % Lx), |
| 66 | jo = (unsigned)floor(rand() % Ly); |
| 67 | random_interval = rand() % 200 + 1; |
| 68 | eta += 0.01f * exp((-((x - io) * (x - io) + (y - jo) * (y - jo))) / |
| 69 | (k * k)); |
| 70 | } |
| 71 | |
| 72 | // compute |
| 73 | array up = um + convolve(eta, h_diff_kernel_arr); |
| 74 | array vp = um + convolve(eta, h_diff_kernel_arr.T()); |
no test coverage detected