From a162bc34bcd6dbc8ba57ed46c886bc9c539f66a3 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Wed, 25 Mar 2020 21:32:50 +0100 Subject: Move everything into main function --- boltzgas/simulation.py | 2 +- boltzgas/visual/histogram.py | 11 ++-- boltzgas/visual/view.py | 2 +- gas.py | 123 +++++++++++++++++++++++++++++-------------- 4 files changed, 91 insertions(+), 47 deletions(-) diff --git a/boltzgas/simulation.py b/boltzgas/simulation.py index a4ae4da..6af9069 100644 --- a/boltzgas/simulation.py +++ b/boltzgas/simulation.py @@ -89,7 +89,7 @@ class HardSphereSimulation: self.tick = True kernelargs = (self.cl_particle_position_b, self.cl_particle_velocity_b, self.cl_particle_position_a, self.cl_particle_velocity_a, self.cl_last_collide) - self.program.evolve(self.queue, (self.n_particles,), None, *(kernelargs)) + self.program.evolve(self.queue, (self.n_particles,), None, *(kernelargs)).wait() def gl_draw_particles(self): gl.glEnableClientState(gl.GL_VERTEX_ARRAY) diff --git a/boltzgas/visual/histogram.py b/boltzgas/visual/histogram.py index 2f90b8b..fc1e853 100644 --- a/boltzgas/visual/histogram.py +++ b/boltzgas/visual/histogram.py @@ -19,7 +19,7 @@ def get_histogram(velocities, char_u): fig = plt.figure(figsize=(8,8)) ax = fig.add_axes([0.1, 0.06, 0.88, 0.92]) - plt.ylim(0, 0.003) + plt.ylim(0, 0.004) plt.ylabel('Probability') plt.xlim(0, 1.2*char_u) @@ -55,7 +55,6 @@ class VelocityHistogram: self.tick = False self.mixing = 0.0 - def setup(self): self.vertices = np.array([ self.origin[0] , self.origin[1] , 0., 1., self.origin[0] + self.extend[0], self.origin[1] , 1., 1., @@ -85,6 +84,9 @@ class VelocityHistogram: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + def shutdown(self): + self.pool.shutdown() + def update(self): self.steps = self.steps + 1 @@ -111,9 +113,9 @@ class VelocityHistogram: def display(self, uniform): if self.tick: - self.mixing = min(self.mixing+0.1, 1.0); + self.mixing = min(self.mixing+0.05, 1.0); else: - self.mixing = max(self.mixing-0.1, 0.0); + self.mixing = max(self.mixing-0.05, 0.0); glBindTextures(self.texture_id[0], 2, self.texture_id) glUniform1iv(uniform['picture'], len(self.texture_id), self.texture_id) @@ -122,4 +124,3 @@ class VelocityHistogram: glBindVertexArray(self.vao); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4) glBindVertexArray(0) - diff --git a/boltzgas/visual/view.py b/boltzgas/visual/view.py index d6e188b..e2746e2 100644 --- a/boltzgas/visual/view.py +++ b/boltzgas/visual/view.py @@ -146,7 +146,7 @@ class View: glUniformMatrix4fv(self.particle_shader.uniform['projection'], 1, False, np.asfortranarray(self.projection)) glUniform3f(self.particle_shader.uniform['face_color'], 1., 1., 1.) glUniform3f(self.particle_shader.uniform['trace_color'], 1., 0., 0.) - glUniform1ui(self.particle_shader.uniform['trace_id'], 4) + glUniform1ui(self.particle_shader.uniform['trace_id'], -1) glEnable(GL_POINT_SPRITE) glPointSize(2*self.gas.radius*self.pixels_per_unit) self.gas.gl_draw_particles() diff --git a/gas.py b/gas.py index 9261d87..6d4f927 100644 --- a/gas.py +++ b/gas.py @@ -7,59 +7,102 @@ from boltzgas import HardSphereSetup, HardSphereSimulation from boltzgas.initial_condition import grid_of_random_velocity_particles from boltzgas.visual import View, VelocityHistogram, Tracer, ColoredBox -glutInit() -glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) -glutInitWindowPosition(0, 0) -glutCreateWindow("BoltzGas") - -grid_width = 32 -radius = 0.004 +grid_width = 40 +radius = 0.002 char_u = 1120 -position, velocity = grid_of_random_velocity_particles(grid_width, radius, char_u) -velocity[:,:] = 0 -velocity[0,0] = 10*char_u -velocity[0,1] = 4*char_u +class SimulationController: + def __init__(self, gas, instruments): + self.running = False + self.gas = gas + self.instruments = instruments + + def isRunning(self): + return self.running + + def run(self): + self.running = True + + def pause(self): + self.running = False + + def evolve(self): + if self.running: + for i in range(0,5): + self.gas.evolve() + + for instrument in self.instruments: + instrument.update() + + def shutdown(self): + self.pause() -config = HardSphereSetup(radius, char_u, position, velocity) -gas = HardSphereSimulation(config, opengl = True, t_scale = 0.1) + for instrument in self.instruments: + try: + instrument.shutdown() + except AttributeError: + return # Doesn't matter, shutdown is optional -tracer = Tracer(gas, 4) -histogram = VelocityHistogram(gas, [1.1,0], [1,1]) -histogram.setup() -view = View(gas, [ ColoredBox([0,0], [1,1], (0.2,0.2,0.2)), tracer ], [ histogram ]) -active = False +def make_display_handler(controller, view): + def on_display(): + controller.evolve() + view.display() -def on_display(): - if active: - for i in range(0,5): - gas.evolve() + return on_display - tracer.update() - histogram.update() +def make_reshape_handler(view): + def on_reshape(width, height): + view.reshape(width, height) - view.display() + return on_reshape -def on_reshape(width, height): - view.reshape(width, height) +def make_timer(): + def on_timer(t): + glutTimerFunc(t, on_timer, t) + glutPostRedisplay() -def on_timer(t): - glutTimerFunc(t, on_timer, t) - glutPostRedisplay() + return on_timer -def on_keyboard(key, x, y): - global active - active = not active +def make_keyboard_handler(controller): + def on_keyboard(key, x, y): + if controller.isRunning(): + controller.pause() + else: + controller.run() -def on_close(): - histogram.pool.shutdown() + return on_keyboard -glutDisplayFunc(on_display) -glutReshapeFunc(on_reshape) -glutTimerFunc(10, on_timer, 10) -glutKeyboardFunc(on_keyboard) -glutCloseFunc(on_close) +def make_close_handler(controller): + def on_close(): + controller.shutdown() + + return on_close if __name__ == "__main__": + glutInit() + glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) + glutInitWindowPosition(0, 0) + glutCreateWindow("BoltzGas") + + position, velocity = grid_of_random_velocity_particles(grid_width, radius, char_u) + velocity[:,:] = 0 + velocity[0,0] = 10*char_u + velocity[0,1] = 4*char_u + + config = HardSphereSetup(radius, char_u, position, velocity) + gas = HardSphereSimulation(config, opengl = True, t_scale = 0.5) + + tracer = Tracer(gas, 4) + histogram = VelocityHistogram(gas, [1.1,0], [1,1]) + view = View(gas, [ ColoredBox([0,0], [1,1], (0.2,0.2,0.2)) ], [ histogram ]) + + controller = SimulationController(gas, [ histogram ]) + + glutDisplayFunc(make_display_handler(controller, view)) + glutReshapeFunc(make_reshape_handler(view)) + glutTimerFunc(20, make_timer(), 20) + glutKeyboardFunc(make_keyboard_handler(controller)) + glutCloseFunc(make_close_handler(controller)) + glutMainLoop() -- cgit v1.2.3