aboutsummaryrefslogtreecommitdiff
path: root/trugfeuer_2d_gl_interop.py
blob: 0ae7bde8323f240bb692afba8da50e79ccb8e7f7 (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
import numpy
from string import Template

from simulation         import Lattice, Geometry
from utility.particles  import Particles
from symbolic.generator import LBM

import symbolic.D2Q9 as D2Q9

from OpenGL.GL   import *
from OpenGL.GLUT import *

from OpenGL.GL import shaders

from pyrr import matrix44

lattice_x = 512
lattice_y = 320

updates_per_frame = 40
particle_count    = 100000

inflow = 0.006
relaxation_time = 0.515

def circle(cx, cy, r):
    return lambda x, y: (x - cx)**2 + (y - cy)**2 < r*r

def get_channel_material_map(geometry):
    return [
        (lambda x, y: x > 0 and x < geometry.size_x-1 and y > 0 and y < geometry.size_y-1, 1), # bulk fluid
        (lambda x, y: y == 1,                 3), # inflow
        (lambda x, y: y == geometry.size_y-2, 4), # outflow
        (lambda x, y: x == 1,                 2), # bottom
        (lambda x, y: x == geometry.size_x-2, 2), # top
        (lambda x, y: y > geometry.size_y//20 and y < 2*geometry.size_y//20 and x < 4*geometry.size_x//9, 2),
        (lambda x, y: y > geometry.size_y//20 and y < 2*geometry.size_y//20 and x > 5*geometry.size_x//9, 2),
        (circle(geometry.size_x//2   , geometry.size_y//8, 3), 2),
        (circle(geometry.size_x//2-10, geometry.size_y//8, 3), 2),
        (circle(geometry.size_x//2+10, geometry.size_y//8, 3), 2),
        (lambda x, y: x == 0 or x == geometry.size_x-1 or y == 0 or y == geometry.size_y-1, 0) # ghost cells
    ]

boundary = Template("""
    if ( m == 2 ) {
        u_0 = 0.0;
        u_1 = 0.0;
    }
    if ( m == 3 ) {
        u_0 = 0.0;
        u_1 = min(time/10000.0 * $inflow, $inflow);
    }
    if ( m == 4 ) {
        rho = 1.0;
    }
""").substitute({
    'inflow': inflow
})

def get_projection(width, height):
    world_width = lattice_x
    world_height = world_width / width * height

    projection  = matrix44.create_orthogonal_projection(-world_width/2, world_width/2, -world_height/2, world_height/2, -1, 1)
    translation = matrix44.create_from_translation([-lattice_x/2, -lattice_y/2, 0])

    point_size = width / world_width

    return numpy.matmul(translation, projection), point_size

def glut_window(fullscreen = False):
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)

    if fullscreen:
        window = glutEnterGameMode()
    else:
        glutInitWindowSize(800, 600)
        glutInitWindowPosition(0, 0)
        window = glutCreateWindow("LBM")

    return window

lbm = LBM(D2Q9)

window = glut_window(fullscreen = False)

fragment_shader = shaders.compileShader("""
#version 430

in vec3 color;

layout(location = 0) out vec4 frag_color;

void main(){
    frag_color = vec4(color.xyz, 0.0);
}""", GL_FRAGMENT_SHADER)

particle_shader = shaders.compileShader(Template("""
#version 430

layout (location=0) in vec4 particles;

out vec3 color;

uniform mat4 projection;

vec3 fire(float x) {
    return mix(
        vec3(1.0, 1.0, 0.0),
        vec3(1.0, 0.0, 0.0),
        x
    );
}

void main() {
    gl_Position = projection * vec4(
        particles[0],
        particles[1],
        0.,
        1.
    );

    color = fire(1.0-particles[3]);
}""").substitute({
    'size_x': lattice_x,
    'size_y': lattice_y
}), GL_VERTEX_SHADER)

particle_program = shaders.compileProgram(particle_shader, fragment_shader)
projection_id = shaders.glGetUniformLocation(particle_program, 'projection')

lattice = Lattice(
    descriptor   = D2Q9,
    geometry     = Geometry(lattice_x, lattice_y),
    moments      = lbm.moments(optimize = False),
    collide      = lbm.bgk(f_eq = lbm.equilibrium(), tau = relaxation_time),
    boundary_src = boundary,
    opengl       = True
)

lattice.apply_material_map(
    get_channel_material_map(lattice.geometry))
lattice.sync_material()

particles = Particles(
    lattice,
    numpy.mgrid[
        4*lattice.geometry.size_x//9:5*lattice.geometry.size_x//9:particle_count/100j,
        lattice.geometry.size_y//20:2*lattice.geometry.size_y//20:100j,
    ].reshape(2,-1).T)

def on_display():
    for i in range(0,updates_per_frame):
        lattice.evolve()

    lattice.update_moments()

    for i in range(0,updates_per_frame):
        particles.update(aging = True)

    lattice.sync()

    glClear(GL_COLOR_BUFFER_BIT)

    shaders.glUseProgram(particle_program)
    glUniformMatrix4fv(projection_id, 1, False, numpy.ascontiguousarray(projection))
    particles.bind()
    glPointSize(point_size)
    glEnable(GL_POINT_SMOOTH)
    glDrawArrays(GL_POINTS, 0, particles.count)

    glutSwapBuffers()

def on_timer(t):
    glutTimerFunc(t, on_timer, t)
    glutPostRedisplay()

def on_reshape(width, height):
    global projection, point_size
    glViewport(0,0,width,height)
    projection, point_size = get_projection(width, height)

glutDisplayFunc(on_display)
glutReshapeFunc(on_reshape)
glutTimerFunc(10, on_timer, 10)

glutMainLoop()