Commit 7c266d3e authored by Matthieu Boileau's avatar Matthieu Boileau

Add argparse to lbm_cl.py

Type ./lbm_cl.py -h to get help
parent cd081a59
......@@ -10,12 +10,15 @@ regular python implementation compared to a pyopencl version
from __future__ import absolute_import, print_function
import pyopencl as cl
import numpy as np
from matplotlib import rcParams
import matplotlib.pyplot as plt
import time
import argparse
##################" definition of default values
rcParams['figure.figsize'] = (12.8, 9.6) # figure size in inches
# number of conservative variables
_m = 9
......@@ -39,13 +42,8 @@ _Tmax = 2.
############# end of default values
def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny,
Tmax = _Tmax,
Lx = _Lx, Ly = _Ly,
animate = True,
interactive=True,
precision="single"):
def solve_ocl(m=_m, n=_n, nx=_nx, ny=_ny, Lx=_Lx, Ly=_Ly, Tmax=_Tmax,
animate=False, precision="single"):
dx = Lx / nx
dy = Ly / ny
......@@ -122,13 +120,14 @@ def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny,
# time loop
t = 0
iter = 0
elapsed = 0.;
fn_cpu = np.empty((4 * m * nx * ny, ), dtype = dtype)
elapsed = 0.
fn_cpu = np.empty((4 * m * nx * ny, ), dtype=dtype)
if animate:
fig = plt.gcf()
fig.show()
fig.canvas.draw()
plot_title = r"$n_x = {}, n_y = {}$".format(nx, ny)
print("start OpenCL computations...")
while t < Tmax:
......@@ -141,35 +140,60 @@ def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny,
elapsed += 1e-9 * (event.profile.end - event.profile.start)
# exchange buffer references for avoiding a copy
fn_gpu, fnp1_gpu = fnp1_gpu, fn_gpu
title = "iter = {}, t = {:f}, elapsed (s) = {:f}".format(iter, t, elapsed)
ite_title = "iter = {}, t = {:f}, elapsed (s) = {:f}".format(iter,
t,
elapsed)
if animate:
if iter % iterplot == 0:
cl.enqueue_copy(queue, fn_cpu, fn_gpu).wait()
wplot = np.reshape(fn_cpu, (4, m, nx, ny))
plt.clf()
#plt.imshow(np.sum(wplot, axis = 0),vmin=0, vmax=1)
fig.suptitle(title)
plt.imshow(np.sum(wplot[:, _ivplot, :, :], axis = 0))
fig.suptitle(ite_title)
plt.imshow(np.sum(wplot[:, _ivplot, :, :], axis=0))
plt.gca().invert_yaxis()
plt.colorbar()
plt.title(plot_title)
if rcParams['backend'] == "nbAgg":
fig.canvas.draw()
else:
print(title, end='\r')
plt.pause(1e-6)
else:
print(ite_title, end='\r')
iter = iter + 1
# copy OpenCL data to CPU and return the results
cl.enqueue_copy(queue, fn_cpu, fn_gpu).wait()
wplot_gpu = np.reshape(fn_cpu,(4, m, nx, ny))
wplot_gpu = np.reshape(fn_cpu, (4, m, nx, ny))
return wplot_gpu
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Solve orszag-tang using LBM'
' on PyOpenCL')
parser.add_argument('-a', '--animate', action='store_true',
help='real-time 2D plots')
parser.add_argument('-n', '--resolution', metavar='nx=ny', type=int,
default=256, help='space resolution')
parser.add_argument('-t', '--Tmax', type=float, default=2.,
help='final simulation time')
parser.add_argument('-p', '--precision', type=str, default='single',
choices=['single', 'double'],
help='floating point precision')
args = parser.parse_args()
arg_dict = vars(args)
arg_dict['nx'] = arg_dict['ny'] = arg_dict['resolution']
del(arg_dict['resolution'])
print(arg_dict)
# gpu solve
wplot_gpu = solve_ocl()
wplot_gpu = solve_ocl(**vars(args))
#print(np.sum(wplot_gpu[:, _ivplot, :, :],axis=0))
plt.clf()
#plt.imshow(np.sum(wplot_gpu[:, _ivplot, :, :],axis=0), vmin=0, vmax=1)
plt.imshow(np.sum(wplot_gpu[:, _ivplot, :, :],axis=0))
plt.gca().invert_yaxis()
......@@ -189,5 +213,3 @@ if __name__ == '__main__':
# plt.gca().invert_yaxis()
# plt.colorbar()
# plt.show()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment