aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ldc_2d_gl_interop.py59
1 files changed, 42 insertions, 17 deletions
diff --git a/ldc_2d_gl_interop.py b/ldc_2d_gl_interop.py
index a10664d..26a8dea 100644
--- a/ldc_2d_gl_interop.py
+++ b/ldc_2d_gl_interop.py
@@ -14,6 +14,11 @@ from OpenGL.GL import shaders
screen_x = 1920
screen_y = 1200
+cells_per_pixel = 4
+updates_per_frame = 200
+
+lid_speed = 0.1
+relaxation_time = 0.515
def cavity(geometry, x, y):
if x == 1 or y == 1 or x == geometry.size_x-2:
@@ -23,19 +28,21 @@ def cavity(geometry, x, y):
else:
return 1
-boundary = """
+boundary = Template("""
if ( m == 2 ) {
u_0 = 0.0;
u_1 = 0.0;
}
if ( m == 3 ) {
- u_0 = 0.1;
+ u_0 = $lid_speed;
u_1 = 0.0;
}
-"""
+""").substitute({
+ 'lid_speed': lid_speed
+})
def get_projection():
- scale = numpy.diag([8.0/screen_x, 8.0/screen_y, 1.0, 1.0])
+ scale = numpy.diag([(2.0*cells_per_pixel)/screen_x, (2.0*cells_per_pixel)/screen_y, 1.0, 1.0])
translation = numpy.matrix(numpy.identity(4))
translation[3,0:3] = [-1.0, -1.0, 0.0]
return scale * translation;
@@ -62,43 +69,60 @@ vertex_shader = shaders.compileShader(Template("""
layout (location=0) in vec4 CellMoments;
+out vec3 color;
+
uniform mat4 projection;
+vec3 blueRedPalette(float x) {
+ return mix(
+ vec3(0.0, 0.0, 1.0),
+ vec3(1.0, 0.0, 0.0),
+ x
+ );
+}
+
vec2 fluidVertexAtIndex(uint i) {
const float y = floor(float(i) / $size_x);
- return vec2(
- i - $size_x*y,
- y
- );
+ return vec2(
+ i - $size_x*y,
+ y
+ );
}
void main() {
const vec2 idx = fluidVertexAtIndex(gl_VertexID);
gl_Position = projection * vec4(
- idx.x + 500.*CellMoments[1],
- idx.y + 500.*CellMoments[2],
+ idx.x,
+ idx.y,
0.,
1.
);
-}""").substitute({'size_x': screen_x//4}), GL_VERTEX_SHADER)
+
+ color = blueRedPalette(CellMoments[3] / $lid_speed);
+}""").substitute({
+ 'size_x' : screen_x//cells_per_pixel,
+ 'lid_speed': lid_speed
+}), GL_VERTEX_SHADER)
fragment_shader = shaders.compileShader("""
-#version 120
+#version 430
+
+in vec3 color;
+
void main(){
- gl_FragColor = vec4(1,1,1,1);
+ gl_FragColor = vec4(color.xyz, 0.0);
}""", GL_FRAGMENT_SHADER)
-
shader_program = shaders.compileProgram(vertex_shader, fragment_shader)
projection_id = shaders.glGetUniformLocation(shader_program, 'projection')
lattice = Lattice(
descriptor = D2Q9,
- geometry = Geometry(screen_x//4, screen_y//4),
+ geometry = Geometry(screen_x//cells_per_pixel, screen_y//cells_per_pixel),
moments = lbm.moments(optimize = True),
- collide = lbm.bgk(f_eq = lbm.equilibrium(), tau = 0.515),
+ collide = lbm.bgk(f_eq = lbm.equilibrium(), tau = relaxation_time),
boundary_src = boundary,
opengl = True
)
@@ -108,7 +132,7 @@ lattice.setup_geometry(cavity)
projection = get_projection()
def on_display():
- for i in range(0,60):
+ for i in range(0,updates_per_frame):
lattice.evolve()
lattice.sync_gl_moments()
@@ -123,6 +147,7 @@ def on_display():
glVertexPointer(4, GL_FLOAT, 0, lattice.gl_moments)
+ glPointSize(cells_per_pixel)
glDrawArrays(GL_POINTS, 0, lattice.geometry.volume)
glDisableClientState(GL_VERTEX_ARRAY)