aboutsummaryrefslogtreecommitdiff
path: root/src/shader/code/collide.glsl
blob: 191204e4f40d1c6a01199645c0e8524e859b1604 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
static const std::string COLLIDE_SHADER_CODE = R"(
#version 430

layout (local_size_x = 1, local_size_y = 1) in;

layout (std430, binding=1) buffer bufferCollide  { float collideCells[];  };
layout (std430, binding=2) buffer bufferStream   { float streamCells[];   };
layout (std430, binding=3) buffer bufferFluid    { float fluidCells[];    };

uniform uint nX;
uniform uint nY;
uniform uint iT;

uniform bool show_fluid_quality;

/// LBM constants

const uint  q         = 9;
const float weight[q] = float[](
	1./36., 1./9., 1./36.,
	1./9. , 4./9., 1./9. ,
	1./36 , 1./9., 1./36.
);
const float invCs2 = 1./3.;

/// Fluid characteristics

const float physCharLength   = 1.0;
const float physCharVelocity = 1.0;
const float physViscosity    = 0.01;
const float latticeCharVelocity = 0.005;

/// Unit conversion

const float resolution    = max(nX,nY);
const float convLength    = physCharLength / resolution;
const float convTime      = latticeCharVelocity / physCharVelocity * physCharLength / resolution;
const float convVelocity  = convLength / convTime;
const float convViscosity = convLength * convLength / convTime;

const float relaxationTime = physViscosity / convViscosity * invCs2 + 0.5;
const float relaxationFrequency = 1 / relaxationTime;

/// Emergent fluid numbers

const float Re = physCharVelocity * physCharLength / physViscosity;
const float Ma = latticeCharVelocity * sqrt(invCs2);
const float Kn = Ma / Re;

/// Vector utilities

float comp(int i, int j, vec2 v) {
	return i*v.x + j*v.y;
}

float sq(float x) {
	return x*x;
}

float norm(vec2 v) {
	return sqrt(dot(v,v));
}

/// Array indexing

uint indexOfDirection(int i, int j) {
	return 3*(i+1) + (j+1);
}

uint indexOfLatticeCell(uint x, uint y) {
	return q*nX*y + q*x;
}

uint indexOfFluidVertex(uint x, uint y) {
	return 3*nX*y + 3*x;
}

/// Data access

float w(int i, int j) {
	return weight[indexOfDirection(i,j)];
}

float get(uint x, uint y, int i, int j) {
	return collideCells[indexOfLatticeCell(x,y) + indexOfDirection(i,j)];
}

void set(uint x, uint y, int i, int j, float v) {
	streamCells[indexOfLatticeCell(x,y) + indexOfDirection(i,j)] = v;
}

void setFluidVelocity(uint x, uint y, vec2 v) {
	const uint idx = indexOfFluidVertex(x, y);
	fluidCells[idx + 0] = v.x*convVelocity;
	fluidCells[idx + 1] = v.y*convVelocity;
}

void setFluidQuality(uint x, uint y, float knudsen, int quality) {
	const uint idx = indexOfFluidVertex(x, y);
	fluidCells[idx + 0] = knudsen;
	fluidCells[idx + 1] = quality;
}

int getMaterial(uint x, uint y) {
	const uint idx = indexOfFluidVertex(x, y);
	return int(fluidCells[idx + 2]);
}

/// Moments

float density(uint x, uint y) {
	const uint idx = indexOfLatticeCell(x, y);
	float d = 0.;
	for ( int i = 0; i < q; ++i ) {
		d += collideCells[idx + i];
	}
	return d;
}

vec2 velocity(uint x, uint y, float d) {
	return 1./d * vec2(
		get(x,y, 1, 0) - get(x,y,-1, 0) + get(x,y, 1, 1) - get(x,y,-1,-1) + get(x,y, 1,-1) - get(x,y,-1,1),
		get(x,y, 0, 1) - get(x,y, 0,-1) + get(x,y, 1, 1) - get(x,y,-1,-1) - get(x,y, 1,-1) + get(x,y,-1,1)
	);
}

/// Equilibrium distribution

float equilibrium(float d, vec2 v, int i, int j) {
	return w(i,j) * d * (1 + 3*comp(i,j,v) + 4.5*sq(comp(i,j,v)) - 1.5*sq(norm(v)));
}

/// Material number meaning (geometry is only changed by the interaction shader)

bool isBulkFluidCell(int material) {
	return material == 1 || material == 4 || material == 5 || material == 6;
}

bool isBounceBackCell(int material) {
	return material == 2 || material == 3;
}

bool isInflowCell(int material) {
	return material == 5;
}

bool isOutflowCell(int material) {
	return material == 6;
}

float getLocalKnudsenApproximation(uint x, uint y, float d, vec2 v) {
	float knudsen = 0.0;

	for ( int i = -1; i <= 1; ++i ) {
		for ( int j = -1; j <= 1; ++j ) {
			const float feq  = equilibrium(d,v,i,j);
			const float fneq = get(x,y,i,j) - feq;
			knudsen += abs(fneq / feq);
		}
	}

	return knudsen / q;
}

/// Actual collide&stream kernel

void main() {
	const uint x = gl_GlobalInvocationID.x;
	const uint y = gl_GlobalInvocationID.y;

	if ( !(x < nX && y < nY) ) {
		return;
	}

	const int material = getMaterial(x,y);

	float d = density(x,y);
	vec2  v = velocity(x,y,d);

	if ( isBulkFluidCell(material) ) {
		if ( isInflowCell(material) ) {
			v = vec2(min(float(iT)/5000.0*latticeCharVelocity, latticeCharVelocity), 0.0);
		}
		if ( isOutflowCell(material) ) {
			d = 1.0;
		}

		if ( show_fluid_quality ) {
			const float approxKn = getLocalKnudsenApproximation(x,y,d,v);
			setFluidQuality(x,y, approxKn, int(round(log2(approxKn / Kn))));
		} else {
			setFluidVelocity(x,y,v);
		}

		for ( int i = -1; i <= 1; ++i ) {
			for ( int j = -1; j <= 1; ++j ) {
				set(x+i,y+j,i,j,
				    get(x,y,i,j) + relaxationFrequency * (equilibrium(d,v,i,j) - get(x,y,i,j)));
			}
		}
	}

	if ( isBounceBackCell(material) ) {
		for ( int i = -1; i <= 1; ++i ) {
			for ( int j = -1; j <= 1; ++j ) {
				set(x+(-1)*i,y+(-1)*j,(-1)*i,(-1)*j,
				    get(x,y,i,j) + relaxationFrequency * (equilibrium(d,v,i,j) - get(x,y,i,j)));
			}
		}
	}
}
)";