-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulation.cpp
112 lines (101 loc) · 2.67 KB
/
Simulation.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// Created by Jack Coughlin on 2/7/18.
//
#include "Simulation.h"
#include "utils.h"
Simulation::Simulation(int nx, int ny, float dx, float dy, float dt) {
this->nx = nx;
this->ny = ny;
this->dx = dx;
this->rdx = 1.0 / dx;
this->dy = dy;
this->dt = dt;
allocateArrays();
}
void Simulation::cleanup() {
for (int i = 0; i < nx; i++) {
delete[] p[i];
delete[] u_y[i];
delete[] solid_mask[i];
delete[] air_mask[i];
delete[] water_mask[i];
delete[] divergence[i];
delete[] Adiag[i];
delete[] Aplusi[i];
delete[] Aplusj[i];
delete[] precon[i];
}
delete[] p;
delete[] u_y;
delete[] solid_mask;
delete[] air_mask;
delete[] water_mask;
delete[] divergence;
delete[] Adiag;
delete[] Aplusi;
delete[] Aplusj;
delete[] precon;
for (int i = 0; i < nx + 1; i++) {
delete[] u_x[i];
}
delete[] u_x;
}
void Simulation::allocateArrays() {
u_x = initArray<float>(nx+1, ny);
u_y = initArray<float>(nx, ny+1);
p = initArray<float>(nx, ny);
solid_mask = initArray<short>(nx, ny);
air_mask = initArray<short>(nx, ny);
water_mask = initArray<short>(nx, ny);
divergence = initArray<float>(nx, ny);
Adiag = initArray<short>(nx, ny);
Aplusi = initArray<short>(nx, ny);
Aplusj = initArray<short>(nx, ny);
precon = initArray<double>(nx, ny);
for (int i = 0; i < nx; i++) {
for (int j = 0; j < ny; j++) {
if (i == 0 || j == ny-1 || i == nx-1) {
solid_mask[i][j] = 1;
} else if (j == 0) {
air_mask[i][j] = 1;
} else {
water_mask[i][j] = 1;
}
}
}
particles = initArray<float>(nx*ny, 2);
for (int i = 0; i < nx; i++) {
for (int j = 0; j < ny; j++) {
particles[i*ny+j][0] = i * dx;
particles[i*ny+j][1] = j * dy;
}
}
}
void Simulation::step() {
applyBodyForces();
computeDivergence();
correctPressure();
advectParticles();
advectU_x();
advectU_y();
}
void Simulation::applyBodyForces() {
for (int i = 1; i < nx; i++) {
for (int j = 0; j < ny; j++) {
if (water_mask[i][j-1] == 1 && water_mask[i][j] == 1) {
u_y[i][j] -= 9.8 * dt;
}
}
}
}
void Simulation::computeDivergence() {
for (int i = 0; i < nx; i++) {
for (int j = 0; j < ny; j++) {
if (water_mask[i][j]) {
divergence[i][j] = rdx * (u_x[i + 1][j] - u_x[i][j] + u_y[i][j + 1] - u_y[i][j]);
} else {
divergence[i][j] = 0;
}
}
}
}