def reset_sol(self, test_case, lb_scheme, dx, codegen=None, codegen_dir=None, exclude=None, initialize=True, show_code=False): self.test_case = test_case self.lb_scheme = lb_scheme self.dx = dx self.simu_cfg = get_config(test_case, lb_scheme, dx, codegen, codegen_dir, exclude, show_code) self.sol = pylbm.Simulation(self.simu_cfg, initialize=initialize)
def run_simulation(args): simu_cfg, sample, duration, responses = args simu_cfg['codegen_option']['generate'] = False output = [0] * len(responses) sol = pylbm.Simulation(simu_cfg, initialize=False) sol.extra_parameters = sample sol._need_init = True solid_cells = sol.domain.in_or_out != sol.domain.valin for i, r in enumerate(responses): if isinstance(r, FromConfig): output[i] = r(simu_cfg, sample) def test_nan(): c = list(sol.scheme.consm.keys())[0] sol.m_halo[c][solid_cells] = 0 data = sol.m[c] if np.isnan(np.sum(data)) or np.any(np.abs(data) > 1e20): return True return False actions = [r for r in responses if isinstance(r, DuringSimulation)] nan_detected = False nite = 0 while sol.t <= duration and not nan_detected: sol.one_time_step() for a in actions: a(sol) if nite == 200: nite = 0 nan_detected = test_nan() nite += 1 nan_detected |= test_nan() for i, r in enumerate(responses): if isinstance(r, AfterSimulation): output[i] = r(sol) elif isinstance(r, DuringSimulation): output[i] = r.value() return [not nan_detected] + output
def run(dx, Tf, generator="numpy", sorder=None, withPlot=True): """ Parameters ---------- dx: double spatial step Tf: double final time generator: pylbm generator sorder: list storage order withPlot: boolean if True plot the solution otherwise just compute the solution """ # parameters gamma = 2. / 3. # exponent in the p-function xmin, xmax = 0., 1. # bounds of the domain la = 2. # velocity of the scheme s = 1.7 # relaxation parameter uaL, uaR, ubL, ubR = 1.50, 1.25, 1.50, 1.00 ymina, ymaxa, yminb, ymaxb = 1., 1.75, 1., 1.5 dico = { 'box': { 'x': [xmin, xmax], 'label': 0 }, 'space_step': dx, 'scheme_velocity': la, 'schemes': [ { 'velocities': [1, 2], 'conserved_moments': ua, 'polynomials': [1, LA * X], 'relaxation_parameters': [0, s], 'equilibrium': [ua, -ub], 'init': { ua: (Riemann_pb, (xmin, xmax, uaL, uaR)) }, }, { 'velocities': [1, 2], 'conserved_moments': ub, 'polynomials': [1, LA * X], 'relaxation_parameters': [0, s], 'equilibrium': [ub, ua**(-gamma)], 'init': { ub: (Riemann_pb, (xmin, xmax, ubL, ubR)) }, }, ], 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.Neumann, 1: pylbm.bc.Neumann } }, }, 'generator': generator, 'parameters': { LA: la }, } sol = pylbm.Simulation(dico, sorder=sorder) if withPlot: # create the viewer to plot the solution viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig(2, 1) ax1 = fig[0] ax1.axis(xmin, xmax, .9 * ymina, 1.1 * ymaxa) ax2 = fig[1] ax2.axis(xmin, xmax, .9 * yminb, 1.1 * ymaxb) x = sol.domain.x l1 = ax1.plot(x, sol.m[ua])[0] l2 = ax2.plot(x, sol.m[ub])[0] def update(iframe): if sol.t < Tf: sol.one_time_step() l1.set_data(x, sol.m[ua]) l2.set_data(x, sol.m[ub]) ax1.title = r'$u_a$ at $t = {0:f}$'.format(sol.t) ax2.title = r'$u_b$ at $t = {0:f}$'.format(sol.t) fig.animate(update) fig.show() else: while sol.t < Tf: sol.one_time_step() return sol
def run(space_step, final_time, generator="cython", sorder=None, with_plot=True): """ Parameters ---------- space_step: double spatial step final_time: double final time generator: string pylbm generator sorder: list storage order with_plot: boolean if True plot the solution otherwise just compute the solution Returns ------- sol <class 'pylbm.simulation.Simulation'> """ # parameters xmin, xmax = 0., 1. # bounds of the domain gamma = 2. / 3. # exponent in the p-function la = 2. # velocity of the scheme s_1, s_2 = 1.9, 1.9 # relaxation parameters symb_s_1 = 1 / (.5 + SIGMA_1) # symbolic relaxation parameters symb_s_2 = 1 / (.5 + SIGMA_2) # symbolic relaxation parameters # initial values u1_left, u1_right, u2_left, u2_right = 1.50, 0.50, 1.25, 1.50 # fixed bounds of the graphics ymina, ymaxa, yminb, ymaxb = .25, 1.75, 0.75, 1.75 # discontinuity position xmid = .5 * (xmin + xmax) exact_solution = exact_solver({ 'jump abscissa': xmid, 'left state': [u1_left, u2_left], 'right state': [u1_right, u2_right], 'gamma': gamma, }) exact_solution.diagram() simu_cfg = { 'box': { 'x': [xmin, xmax], 'label': 0 }, 'space_step': space_step, 'scheme_velocity': LA, 'schemes': [ { 'velocities': [1, 2], 'conserved_moments': U1, 'polynomials': [1, X], 'relaxation_parameters': [0, symb_s_1], 'equilibrium': [U1, -U2], 'init': { U1: (riemann_pb, (xmid, u1_left, u1_right)) }, }, { 'velocities': [1, 2], 'conserved_moments': U2, 'polynomials': [1, X], 'relaxation_parameters': [0, symb_s_2], 'equilibrium': [U2, U1**(-GAMMA)], 'init': { U2: (riemann_pb, (xmid, u2_left, u2_right)) }, }, ], 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.Neumann, 1: pylbm.bc.Neumann, }, }, }, 'generator': generator, 'parameters': { LA: la, GAMMA: gamma, SIGMA_1: 1 / s_1 - .5, SIGMA_2: 1 / s_2 - .5, }, } # build the simulation sol = pylbm.Simulation(simu_cfg, sorder=sorder) # build the equivalent PDE eq_pde = pylbm.EquivalentEquation(sol.scheme) print(eq_pde) if with_plot: # create the viewer to plot the solution viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig(2, 1) axe1 = fig[0] axe1.axis(xmin, xmax, ymina, ymaxa) axe1.set_label(None, r"$u_1$") axe1.xaxis_set_visible(False) axe2 = fig[1] axe2.axis(xmin, xmax, yminb, ymaxb) axe2.set_label(r"$x$", r"$u_2$") x = sol.domain.x l1a = axe1.CurveScatter( x, sol.m[U1], color='navy', label=r'$D_1Q_2$', ) sole = exact_solution.evaluate(x, sol.t) l1e = axe1.CurveLine( x, sole[0], width=1, color='black', label='exact', ) l2a = axe2.CurveScatter( x, sol.m[U2], color='orange', label=r'$D_1Q_2$', ) l2e = axe2.CurveLine( x, sole[1], width=1, color='black', label='exact', ) axe1.legend(loc='upper right') axe2.legend(loc='upper left') def update(iframe): # pylint: disable=unused-argument if sol.t < final_time: sol.one_time_step() l1a.update(sol.m[U1]) l2a.update(sol.m[U2]) sole = exact_solution.evaluate(x, sol.t) l1e.update(sole[0]) l2e.update(sole[1]) axe1.title = r'p-system at $t = {0:f}$'.format(sol.t) fig.animate(update) fig.show() else: while sol.t < final_time: sol.one_time_step() return sol
def run(dx, Tf, generator="cython", sorder=None, withPlot=True): """ Parameters ---------- dx: double spatial step Tf: double final time generator: pylbm generator sorder: list storage order withPlot: boolean if True plot the solution otherwise just compute the solution """ r = X**2+Y**2+Z**2 dico = { 'box': { 'x': [xmin, xmax], 'y': [ymin, ymax], 'z': [zmin, zmax], 'label': [-1, -1, 0, 1, -1, -1] }, 'space_step': dx, 'scheme_velocity': la, 'schemes': [ { 'velocities': list(range(19)), 'conserved_moments': [rho, qx, qy, qz], 'polynomials': [ 1, X, Y, Z, 19*r - 30, 2*X**2 - Y**2 - Z**2, Y**2-Z**2, X*Y, Y*Z, Z*X, X*(5*r - 9), Y*(5*r - 9), Z*(5*r - 9), X*(Y**2 - Z**2), Y*(Z**2 - X**2), Z*(X**2 - Y**2), (2*X**2 - Y**2 - Z**2)*(3*r - 5), (Y**2 - Z**2)*(3*r - 5), -sp.Rational(53, 2)*r + sp.Rational(21, 2)*r**2 + 12 ], 'relaxation_parameters': sf, 'feq': (feq_NS, (sp.Matrix([qx, qy, qz]),)), 'source_terms': {qy: beta*g*T}, }, { 'velocities': list(range(1, 7)), 'conserved_moments': T, 'polynomials': [1, X, Y, Z, X**2 - Y**2, Y**2 - Z**2, ], 'feq': (feq_T, (sp.Matrix([qx, qy, qz]),)), 'relaxation_parameters': sT, }, ], 'init': {rho: 1., qx: 0., qy: 0., qz: 0., T: init_T }, 'boundary_conditions': { 0: {'method': {0: pylbm.bc.BouzidiBounceBack, 1: pylbm.bc.BouzidiAntiBounceBack}, 'value': bc_down}, 1: {'method': {0: pylbm.bc.BouzidiBounceBack, 1: pylbm.bc.BouzidiAntiBounceBack}, 'value': bc_up}, }, 'generator': "cython", 'parameters': {LA: la}, } sol = pylbm.Simulation(dico) im = 0 compt = 0 while sol.t < Tf: sol.one_time_step() compt += 1 if compt == 128: im += 1 save(sol, im) compt = 0 return sol
def run(dx, Tf, generator="numpy", sorder=None, withPlot=True): """ Parameters ---------- dx: double spatial step Tf: double final time generator: pylbm generator sorder: list storage order withPlot: boolean if True plot the solution otherwise just compute the solution """ # parameters xmin, xmax = 0., 1. # bounds of the domain la = 2. # velocity of the scheme s = 1.5 # relaxation parameter hL, hR, qL, qR = 1., .25, 0.10, 0.10 ymina, ymaxa, yminb, ymaxb = 0., 1., 0., .5 dico = { 'box': { 'x': [xmin, xmax], 'label': 0 }, 'space_step': dx, 'scheme_velocity': la, 'schemes': [ { 'velocities': [1, 2], 'conserved_moments': h, 'polynomials': [1, LA * X], 'relaxation_parameters': [0, s], 'equilibrium': [h, q], 'init': { h: (Riemann_pb, (xmin, xmax, hL, hR)) }, }, { 'velocities': [1, 2], 'conserved_moments': q, 'polynomials': [1, LA * X], 'relaxation_parameters': [0, s], 'equilibrium': [q, q**2 / h + .5 * g * h**2], 'init': { q: (Riemann_pb, (xmin, xmax, qL, qR)) }, }, ], 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.Neumann, 1: pylbm.bc.Neumann } }, }, 'generator': generator, 'parameters': { LA: la, g: 1. }, } sol = pylbm.Simulation(dico, sorder=sorder) if withPlot: # create the viewer to plot the solution viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig(2, 1) ax1 = fig[0] ax1.axis(xmin, xmax, .9 * ymina, 1.1 * ymaxa) ax2 = fig[1] ax2.axis(xmin, xmax, .9 * yminb, 1.1 * ymaxb) x = sol.domain.x l1 = ax1.plot(x, sol.m[h], color='b')[0] l2 = ax2.plot(x, sol.m[q], color='r')[0] def update(iframe): if sol.t < Tf: sol.one_time_step() l1.set_data(x, sol.m[h]) l2.set_data(x, sol.m[q]) ax1.title = r'$h$ at $t = {0:f}$'.format(sol.t) ax2.title = r'$q$ at $t = {0:f}$'.format(sol.t) fig.animate(update) fig.show() else: while sol.t < Tf: sol.one_time_step() return sol
def run(dx, Tf, generator="cython", sorder=None, withPlot=True): """ Parameters ---------- dx: double spatial step Tf: double final time generator: pylbm generator sorder: list storage order withPlot: boolean if True plot the solution otherwise just compute the solution """ # parameters width = 1. height = .5 xmin, xmax, ymin, ymax = 0., width, -.5 * height, .5 * height zmin, zmax = -2 * dx, 2 * dx la = 1. # velocity of the scheme max_velocity = 0.1 mu = 1.e-3 zeta = 1.e-5 grad_pressure = -mu * max_velocity * 8. / height**2 cte = 10. dummy = 3.0 / (la * dx) #s1 = 1.0/(0.5+zeta*dummy) #s2 = 1.0/(0.5+mu*dummy) sigma = 1. / np.sqrt(12) s = 1. / (.5 + sigma) vs = [0., s, s, s, s, s] velocities = list(range(1, 7)) polynomes = [1, LA * X, LA * Y, LA * Z, X**2 - Y**2, X**2 - Z**2] def bc_in(f, m, x, y, z): m[p] = (x - 0.5 * width) * grad_pressure * cte m[ux] = max_velocity * (1. - 4. * y**2 / height**2) def bc_out(f, m, x, y, z): m[p] = (x - 0.5 * width) * grad_pressure * cte dico = { 'box': { 'x': [xmin, xmax], 'y': [ymin, ymax], 'z': [zmin, zmax], 'label': [1, 2, 0, 0, -1, -1] }, 'space_step': dx, 'scheme_velocity': la, 'schemes': [ { 'velocities': velocities, 'conserved_moments': p, 'polynomials': polynomes, 'relaxation_parameters': vs, 'equilibrium': [p, ux, uy, uz, 0., 0.], 'init': { p: 0. }, }, { 'velocities': velocities, 'conserved_moments': ux, 'polynomials': polynomes, 'relaxation_parameters': vs, 'equilibrium': [ux, ux**2 + p / cte, ux * uy, ux * uz, 0., 0.], 'init': { ux: 0. }, }, { 'velocities': velocities, 'conserved_moments': uy, 'polynomials': polynomes, 'relaxation_parameters': vs, 'equilibrium': [uy, uy * ux, uy**2 + p / cte, uy * uz, 0., 0.], 'init': { uy: 0. }, }, { 'velocities': velocities, 'conserved_moments': uz, 'polynomials': polynomes, 'relaxation_parameters': vs, 'equilibrium': [uz, uz * ux, uz * uy, uz**2 + p / cte, 0., 0.], 'init': { uz: 0. }, }, ], 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.BouzidiBounceBack, 1: pylbm.bc.BouzidiAntiBounceBack, 2: pylbm.bc.BouzidiAntiBounceBack, 3: pylbm.bc.BouzidiAntiBounceBack, }, }, 1: { 'method': { 0: pylbm.bc.BouzidiAntiBounceBack, 1: pylbm.bc.NeumannX, 2: pylbm.bc.NeumannX, 3: pylbm.bc.NeumannX, }, 'value': bc_out, }, 2: { 'method': { 0: pylbm.bc.BouzidiAntiBounceBack, 1: pylbm.bc.BouzidiAntiBounceBack, 2: pylbm.bc.BouzidiAntiBounceBack, 3: pylbm.bc.BouzidiAntiBounceBack, }, 'value': bc_in, }, }, 'parameters': { LA: la }, 'generator': generator, } sol = pylbm.Simulation(dico, sorder=sorder) im = 0 compt = 0 while sol.t < Tf: sol.one_time_step() compt += 1 if compt == 100 and withPlot: im += 1 save(sol, im) compt = 0 return sol
def run(space_step, final_time, generator="cython", sorder=None, with_plot=True): """ Parameters ---------- space_step: double spatial step final_time: double final time generator: string pylbm generator sorder: list storage order with_plot: boolean if True plot the solution otherwise just compute the solution Returns ------- sol <class 'pylbm.simulation.Simulation'> """ # parameters xmin, xmax = -1, 1. # bounds of the domain gnum = 1 # numerical value of g la = 2. # velocity of the scheme s_h, s_u = 1.5, 1.5 # relaxation parameters symb_s_h = 1/(.5+SIGMA_H) # symbolic relaxation parameters symb_s_q = 1/(.5+SIGMA_Q) # symbolic relaxation parameters # initial values h_left, h_right, q_left, q_right = 2, 1, .1, 0. # fixed bounds of the graphics ymina, ymaxa = 0.9, 2.1 yminb, ymaxb = -.1, .7 # discontinuity position xmid = .5*(xmin+xmax) exact_solution = exact_solver({ 'jump abscissa': xmid, 'left state': [h_left, q_left], 'right state': [h_right, q_right], 'g': gnum, }) exact_solution.diagram() simu_cfg = { 'box': {'x': [xmin, xmax], 'label': 0}, 'space_step': space_step, 'scheme_velocity': LA, 'schemes': [ { 'velocities': [1, 2], 'conserved_moments': H, 'polynomials': [1, X], 'relaxation_parameters': [0, symb_s_h], 'equilibrium': [H, Q], }, { 'velocities': [1, 2], 'conserved_moments': Q, 'polynomials': [1, X], 'relaxation_parameters': [0, symb_s_q], 'equilibrium': [Q, Q**2/H+.5*G*H**2], }, ], 'init': {H: (riemann_pb, (xmid, h_left, h_right)), Q: (riemann_pb, (xmid, q_left, q_right))}, 'boundary_conditions': { 0: {'method': { 0: pylbm.bc.Neumann, 1: pylbm.bc.Neumann, }, }, }, 'generator': generator, 'parameters': { LA: la, G: gnum, SIGMA_H: 1/s_h-.5, SIGMA_Q: 1/s_u-.5, }, } # build the simulation sol = pylbm.Simulation(simu_cfg, sorder=sorder) # build the equivalent PDE eq_pde = pylbm.EquivalentEquation(sol.scheme) print(eq_pde) if with_plot: # create the viewer to plot the solution viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig(2, 1) axe1 = fig[0] axe1.axis(xmin, xmax, ymina, ymaxa) axe1.set_label(None, "height") axe1.xaxis_set_visible(False) axe2 = fig[1] axe2.axis(xmin, xmax, yminb, ymaxb) axe2.set_label(r"$x$", "velocity") x = sol.domain.x l1a = axe1.CurveScatter( x, sol.m[H], color='navy', label=r'$D_1Q_2$', ) sole = exact_solution.evaluate(x, sol.t) l1e = axe1.CurveLine( x, sole[0], width=1, color='black', label='exact', ) l2a = axe2.CurveScatter( x, sol.m[Q]/sol.m[H], color='orange', label=r'$D_1Q_2$', ) l2e = axe2.CurveLine( x, sole[1]/sole[0], width=1, color='black', label='exact', ) axe1.legend(loc='upper right') axe2.legend(loc='upper left') def update(iframe): # pylint: disable=unused-argument if sol.t < final_time: sol.one_time_step() l1a.update(sol.m[H]) l2a.update(sol.m[Q]/sol.m[H]) sole = exact_solution.evaluate(x, sol.t) l1e.update(sole[0]) l2e.update(sole[1]/sole[0]) axe1.title = r'shallow water at $t = {0:f}$'.format(sol.t) fig.animate(update) fig.show() else: while sol.t < final_time: sol.one_time_step() return sol
def run(dx, Tf, generator="cython", sorder=None, withPlot=True): """ Parameters ---------- dx: double spatial step Tf: double final time generator: pylbm generator sorder: list storage order withPlot: boolean if True plot the solution otherwise just compute the solution """ # advective velocity ux, uy, uz = .5, .2, .1 # domain of the computation xmin, xmax, ymin, ymax, zmin, zmax = 0., 1., 0., 1., 0., 1. def u0(x, y, z): xm, ym, zm = .5 * (xmin + xmax), .5 * (ymin + ymax), .5 * (zmin + zmax) return .5*np.ones((x.size, y.size, z.size)) \ + .5*(((x-xm)**2+(y-ym)**2+(z-zm)**2)<.25**2) s = 1. la = 1. d = { 'box': { 'x': [xmin, xmax], 'y': [ymin, ymax], 'z': [zmin, zmax], 'label': -1 }, 'space_step': dx, 'scheme_velocity': la, 'schemes': [ { 'velocities': list(range(1, 7)), 'conserved_moments': [u], 'polynomials': [1, LA * X, LA * Y, LA * Z, X**2 - Y**2, X**2 - Z**2], 'equilibrium': [u, ux * u, uy * u, uz * u, 0., 0.], 'relaxation_parameters': [0., s, s, s, s, s], 'init': { u: u0 }, }, ], 'parameters': { LA: la }, 'generator': generator, } sol = pylbm.Simulation(d, sorder=sorder) im = 0 while sol.t < Tf: sol.one_time_step() if withPlot: im += 1 save(sol, im) return sol
tc_mod = importlib.import_module(data['test_case']['module']) test_case = getattr(tc_mod, data['test_case']['class'])(**data['test_case']['args']) to_exclude = [] extra = {} for k, v in data['extra_config'].items(): if k == 'la': symb = sp.symbols('lambda') else: symb = sp.symbols(k) to_exclude.append(symb) extra[symb] = v simu_cfg = pylbm_ui.simulation.get_config(test_case, lb_scheme, data['dx'], exclude=to_exclude) sol = pylbm.Simulation(simu_cfg, initialize=False) sol.extra_parameters = extra responses = [] fields = test_case.equation.get_fields() domain = pylbm.Domain(simu_cfg) time_e = test_case.duration ref = test_case.ref_solution(time_e, domain.x, field='mass') responses_list = pylbm_ui.widgets.responses.build_responses_list(test_case, lb_scheme) responses = [] for r in data['responses']: responses.append(responses_list[r])
def run(dx, Tf, generator="cython", sorder=None, withPlot=True): """ Parameters ---------- dx: double spatial step Tf: double final time generator: pylbm generator sorder: list storage order withPlot: boolean if True plot the solution otherwise just compute the solution """ rhoo = 1. U = .5 k = 80 delta = .05 Ma = .1 lamb = np.sqrt(3)/Ma mu = .0366 nu = 1e-6 sigma3 = 3*mu/(rhoo*lamb*dx) sigma4 = 3*nu/(rhoo*lamb*dx) s3 = 1./(sigma3+.5) s4 = 1./(sigma4+.5) s = [0.,0.,0.,s3,s4,s4,s3,s3,s3] kelvin_helmoltz = { 'parameters':{LA: lamb}, 'box':{'x':[0., 1.], 'y':[0., 1.], 'label':-1}, 'space_step': dx, 'scheme_velocity':LA, 'schemes':[ { 'velocities':list(range(9)), # 'polynomials':[ # 1, X, Y, # X**2 + Y**2, # X**2 - Y**2, # X*Y, # X*(X**2+Y**2), # Y*(X**2+Y**2), # (X**2+Y**2)**2 # ], # polynomials of P. Lallemand 'polynomials':[ 1, X, Y, X**2 + Y**2, X**2 - Y**2, X*Y, X*Y**2, Y*X**2, X**2*Y**2 ], # polynomials of M. Geier 'relaxation_parameters':s, #'feq': (feq, (sp.Matrix([qx/rho, qy/rho]),)), # Qian equilibrium 'equilibrium':[ rho, qx, qy, (qx**2 + qy**2 + 2*LA**2*rho**2/3)/rho, (qx**2 - qy**2)/rho, qx*qy/rho, qx*(LA**2/3+qy**2/rho**2), qy*(LA**2/3+qx**2/rho**2), rho*(LA**2/3+qx**2/rho**2)*(LA**2/3+qy**2/rho**2), ], # maxwellian equilibrium 'conserved_moments': [rho, qx, qy], 'init': {rho: 1., qx: (qx0, (U, k)), qy: (qy0, (U, delta))}, }, ], 'relative_velocity': [qx/rho, qy/rho], 'generator': generator, } sol = pylbm.Simulation(kelvin_helmoltz, sorder=sorder) if withPlot: # init viewer viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig() ax = fig[0] image = ax.image(vorticity, (sol,), cmap='jet')#, clim=[-60, 50]) def update(iframe): nrep = 128 for i in range(nrep): sol.one_time_step() image.set_data(vorticity(sol)) ax.title = "Solution t={0:f}".format(sol.t) # run the simulation fig.animate(update, interval=1) fig.show() else: while sol.t < Tf: sol.one_time_step() return sol
def run(space_step, final_time, generator="cython", sorder=None, with_plot=True): """ Parameters ---------- space_step: double spatial step final_time: double final time generator: string pylbm generator sorder: list storage order with_plot: boolean if True plot the solution otherwise just compute the solution Returns ------- sol <class 'pylbm.simulation.Simulation'> """ # parameters xmin, xmax = 0., 1. # bounds of the domain la = 1. # lattice velocity (la = dx/dt) velocity = 0.25 # velocity of the advection s = 1.9 # relaxation parameter symb_s = 1/(0.5+SIGMA) # symbolic relaxation parameter # initial values u_left, u_right = 1., 0. # discontinuity position if velocity > 0: xmid = 0.75*xmin + .25*xmax elif velocity < 0: xmid = .25*xmin + .75*xmax else: xmid = .5*xmin + .5*xmax # fixed bounds of the graphics ymin = min([u_left, u_right])-.2*abs(u_left-u_right) ymax = max([u_left, u_right])+.2*abs(u_left-u_right) exact_solution = exact_solver({ 'jump abscissa': xmid, 'left state': [u_left], 'right state': [u_right], 'velocity': velocity, }) # dictionary of the simulation simu_cfg = { 'box': {'x': [xmin, xmax], 'label': 0}, 'space_step': space_step, 'scheme_velocity': LA, 'schemes': [ { 'velocities': [1, 2], 'conserved_moments': U, 'polynomials': [1, X], 'relaxation_parameters': [0., symb_s], 'equilibrium': [U, C*U], 'init': {U: (riemann_pb, (xmid, u_left, u_right))}, }, ], 'boundary_conditions': { 0: {'method': { 0: pylbm.bc.Neumann, }, }, }, 'generator': generator, 'parameters': { LA: la, C: velocity, SIGMA: 1/s-.5 }, 'show_code': False, } # build the simulation sol = pylbm.Simulation(simu_cfg, sorder=sorder) # build the equivalent PDE eq_pde = pylbm.EquivalentEquation(sol.scheme) print(eq_pde) if with_plot: # create the viewer to plot the solution viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig() axe = fig[0] axe.axis(xmin, xmax, ymin, ymax) axe.set_label(r'$x$', r'$u$') x = sol.domain.x l1a = axe.CurveScatter( x, sol.m[U], color='navy', label=r'$D_1Q_2$', ) l1e = axe.CurveLine( x, exact_solution.evaluate(x, sol.t)[0], width=1, color='black', label='exact', ) axe.legend(loc='upper right', shadow=False, frameon=False, ) def update(iframe): # pylint: disable=unused-argument if sol.t < final_time: # time loop sol.one_time_step() # increment the solution of one time step l1a.update(sol.m[U]) l1e.update(exact_solution.evaluate(x, sol.t)[0]) axe.title = r'advection at $t = {0:f}$'.format(sol.t) fig.animate(update) fig.show() else: while sol.t < final_time: sol.one_time_step() return sol
def run(space_step, final_time, generator="cython", sorder=None, with_plot=True): """ Parameters ---------- space_step: double spatial step final_time: double final time generator: string pylbm generator sorder: list storage order with_plot: boolean if True plot the solution otherwise just compute the solution Returns ------- sol <class 'pylbm.simulation.Simulation'> """ # parameters scheme_name = 'Geier' xmin, xmax, ymin, ymax = 0., 1., 0., 1. # bounds of the domain la = 1. # velocity of the scheme rho_o = 1. # reference value of the mass driven_velocity = 0.05 # boundary value of the velocity mu = 5.e-6 # bulk viscosity zeta = 100 * mu # shear viscosity def moments_choice(scheme_name, mu, zeta): if scheme_name == 'dHumiere': dummy = 1. / rho_o QX2 = dummy * QX**2 QY2 = dummy * QY**2 Q2 = QX2 + QY2 QXY = dummy * QX * QY polynomials = [ 1, X, Y, 3 * (X**2 + Y**2) - 4 * LA**2, 0.5 * (9 * (X**2 + Y**2)**2 - 21 * (X**2 + Y**2) * LA**2 + 8 * LA**4), 3 * X * (X**2 + Y**2) - 5 * X * LA**2, 3 * Y * (X**2 + Y**2) - 5 * Y * LA**2, X**2 - Y**2, X * Y ] equilibrium = [ RHO, QX, QY, -2 * RHO * LA**2 + 3 * Q2, RHO * LA**2 - 3 * Q2, -QX * LA**2, -QY * LA**2, QX2 - QY2, QXY ] dummy = 3.0 / (la * rho_o * space_step) sigma_1 = dummy * zeta sigma_2 = dummy * mu s_1 = 1 / (.5 + sigma_1) s_2 = 1 / (.5 + sigma_2) if scheme_name == 'Geier': UX, UY = QX / RHO, QY / RHO RHOU2 = RHO * (UX**2 + UY**2) polynomials = [ 1, X, Y, X**2 + Y**2, X * Y**2, Y * X**2, X**2 * Y**2, X**2 - Y**2, X * Y, ] equilibrium = [ RHO, QX, QY, RHOU2 + 2 / 3 * RHO * LA**2, QX * (LA**2 / 3 + UY**2), QY * (LA**2 / 3 + UX**2), RHO * (LA**2 / 3 + UX**2) * (LA**2 / 3 + UY**2), RHO * (UX**2 - UY**2), RHO * UX * UY, ] dummy = 3.0 / (la * rho_o * space_step) sigma_1 = dummy * (zeta - 2 * mu / 3) sigma_2 = dummy * mu s_1 = 1 / (.5 + sigma_1) s_2 = 1 / (.5 + sigma_2) if scheme_name == 'Lallemand': dummy = 1. / rho_o QX2 = dummy * QX**2 QY2 = dummy * QY**2 Q2 = QX2 + QY2 QXY = dummy * QX * QY polynomials = [ 1, X, Y, X**2 + Y**2, X * (X**2 + Y**2), Y * (X**2 + Y**2), (X**2 + Y**2)**2, X**2 - Y**2, X * Y, ] equilibrium = [ RHO, QX, QY, Q2 + 2 / 3 * LA**2 * RHO, 4 / 3 * QX * LA**2, 4 / 3 * QY * LA**2, ((21 * Q2 + 6 * RHO * LA**2) * LA**2 - (6 * Q2 - 2 * RHO * LA**2)) / 9, QX2 - QY2, QXY, ] dummy = 3.0 / (la * rho_o * space_step) sigma_1 = dummy * zeta sigma_2 = dummy * mu s_1 = 1 / (.5 + sigma_1) s_2 = 1 / (.5 + sigma_2) s = [0., 0., 0., s_1, s_1, s_1, s_1, s_2, s_2] return polynomials, equilibrium, s polynomials, equilibrium, s = moments_choice(scheme_name, mu, zeta) simu_cfg = { 'parameters': { LA: la }, 'box': { 'x': [xmin, xmax], 'y': [ymin, ymax], 'label': [0, 0, 0, 1] }, 'space_step': space_step, 'scheme_velocity': LA, 'schemes': [ { 'velocities': list(range(9)), 'polynomials': polynomials, 'relaxation_parameters': s, 'equilibrium': equilibrium, 'conserved_moments': [RHO, QX, QY], }, ], 'init': { RHO: rho_o, QX: 0., QY: 0. }, 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.BouzidiBounceBack } }, 1: { 'method': { 0: pylbm.bc.BouzidiBounceBack }, 'value': (bc_up, (rho_o, driven_velocity)) } }, 'generator': generator, 'relative_velocity': [QX / RHO, QY / RHO], # 'show_code': True, } sol = pylbm.Simulation(simu_cfg, sorder=sorder) while sol.t < final_time: sol.one_time_step() viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig() axe = fig[0] axe.grid(visible=False) axe.xaxis_set_visible(False) axe.yaxis_set_visible(False) axe.SurfaceImage( vorticity(sol), cmap='jet', clim=[0, .1], alpha=0.25, ) lines = flow_lines(sol, 10, 2) for linek in lines: axe.CurveLine(linek[0], linek[1], alpha=1) plt.show() return sol
def run(dx, Tf, generator="cython", sorder=None, with_plot=True): """ Parameters ---------- dx: double spatial step Tf: double final time generator: pylbm generator sorder: list storage order with_plot: boolean if True plot the solution otherwise just compute the solution """ # parameters xmin, xmax, ymin, ymax = 0., 2 * np.pi, 0., 2 * np.pi gamma = 5. / 3. s0, s1, s2, s3 = [1.95] * 4 la = 10. s_rho = [0., s1, s1, s0] s_q = [0., s2, s2, s0] s_E = [0., s3, s3, s0] s_B = [0., s3, s3, s0] p = (GA - 1) * (E - (qx**2 + qy**2) / (2 * rho) - (Bx**2 + By**2) / 2) ps = p + (Bx**2 + By**2) / 2 vB = (qx * Bx + qy * By) / rho dico = { 'box': { 'x': [xmin, xmax], 'y': [ymin, ymax], 'label': -1 }, 'space_step': dx, 'scheme_velocity': la, 'schemes': [ { 'velocities': list(range(1, 5)), 'conserved_moments': rho, 'polynomials': [1, LA * X, LA * Y, X**2 - Y**2], 'relaxation_parameters': s_rho, 'equilibrium': [rho, qx, qy, 0.], }, { 'velocities': list(range(1, 5)), 'conserved_moments': qx, 'polynomials': [1, LA * X, LA * Y, X**2 - Y**2], 'relaxation_parameters': s_q, 'equilibrium': [qx, qx**2 / rho + ps - Bx**2, qx * qy / rho - Bx * By, 0.], }, { 'velocities': list(range(1, 5)), 'conserved_moments': qy, 'polynomials': [1, LA * X, LA * Y, X**2 - Y**2], 'relaxation_parameters': s_q, 'equilibrium': [qy, qx * qy / rho - Bx * By, qy**2 / rho + ps - By**2, 0.], }, { 'velocities': list(range(1, 5)), 'conserved_moments': E, 'polynomials': [1, LA * X, LA * Y, X**2 - Y**2], 'relaxation_parameters': s_E, 'equilibrium': [ E, (E + ps) * qx / rho - vB * Bx, (E + ps) * qy / rho - vB * By, 0. ], }, { 'velocities': list(range(1, 5)), 'conserved_moments': Bx, 'polynomials': [1, LA * X, LA * Y, X**2 - Y**2], 'relaxation_parameters': s_B, 'equilibrium': [Bx, 0, (qy * Bx - qx * By) / rho, 0.], }, { 'velocities': list(range(1, 5)), 'conserved_moments': By, 'polynomials': [1, LA * X, LA * Y, X**2 - Y**2], 'relaxation_parameters': s_B, 'equilibrium': [By, (qx * By - qy * Bx) / rho, 0, 0.], }, ], 'init': { rho: (init_rho, (gamma, )), qx: (init_qx, (gamma, )), qy: (init_qy, (gamma, )), E: (init_E, (gamma, )), Bx: init_Bx, By: init_By }, 'parameters': { LA: la, GA: gamma }, 'generator': generator, } sol = pylbm.Simulation(dico) if with_plot: # init viewer viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig() ax = fig[0] N, M = sol.m[rho].shape na, nb = 1, N - 1 ma, mb = 1, M - 1 im = ax.image(sol.m[rho][na:nb, ma:mb].transpose(), clim=[0.5, 7.2]) ax.title = 'solution at t = {0:f}'.format(sol.t) def update(iframe): for k in range(16): sol.one_time_step() # increment the solution of one time step im.set_data(sol.m[rho][na:nb, ma:mb].transpose()) ax.title = 'solution at t = {0:f}'.format(sol.t) # run the simulation fig.animate(update, interval=1) fig.show() else: while sol.t < Tf: sol.one_time_step() return sol
def main(): # parameters xmin, xmax, ymin, ymax = 0., 2., 0., 1. radius = 0.125 if h5_save: dx = 1. / 512 # spatial step else: dx = 1. / 128 la = 1. # velocity of the scheme rhoo = 1. uo = 0.05 mu = 5.e-5 zeta = 10 * mu dummy = 3.0 / (la * rhoo * dx) s1 = 1.0 / (0.5 + zeta * dummy) s2 = 1.0 / (0.5 + mu * dummy) s = [0., 0., 0., s1, s1, s1, s1, s2, s2] dummy = 1. / (LA**2 * rhoo) qx2 = dummy * qx**2 qy2 = dummy * qy**2 q2 = qx2 + qy2 qxy = dummy * qx * qy dico = { 'box': { 'x': [xmin, xmax], 'y': [ymin, ymax], 'label': [0, 1, 0, 0] }, 'elements': [pylbm.Circle([.3, 0.5 * (ymin + ymax) + 2 * dx], radius, label=2)], 'space_step': dx, 'scheme_velocity': LA, 'schemes': [ { 'velocities': list(range(9)), 'polynomials': [ 1, LA * X, LA * Y, 3 * (X**2 + Y**2) - 4, 0.5 * (9 * (X**2 + Y**2)**2 - 21 * (X**2 + Y**2) + 8), 3 * X * (X**2 + Y**2) - 5 * X, 3 * Y * (X**2 + Y**2) - 5 * Y, X**2 - Y**2, X * Y ], 'relaxation_parameters': s, 'equilibrium': [ rho, qx, qy, -2 * rho + 3 * q2, rho - 3 * q2, -qx / LA, -qy / LA, qx2 - qy2, qxy ], 'conserved_moments': [rho, qx, qy], }, ], 'init': { rho: rhoo, qx: rhoo * uo, qy: 0. }, 'parameters': { LA: la }, 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.BouzidiBounceBack }, 'value': (bc_rect, (rhoo, uo)) }, 1: { 'method': { 0: pylbm.bc.NeumannX } }, 2: { 'method': { 0: pylbm.bc.BouzidiBounceBack } }, }, 'generator': "cython", } sol = pylbm.Simulation(dico) Re = rhoo * uo * 2 * radius / mu print("Reynolds number {0:10.3e}".format(Re)) x, y = sol.domain.x, sol.domain.y if h5_save: Tf = 500. im = 0 l = Tf / sol.dt / 64 printProgress(im, l, prefix='Progress:', suffix='Complete', barLength=50) filename = 'Karman' path = './data_' + filename save(x, y, sol.m, im) while sol.t < Tf: for k in range(64): sol.one_time_step() im += 1 printProgress(im, l, prefix='Progress:', suffix='Complete', barLength=50) save(sol.mpi_topo, x, y, sol.m, im) else: viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig(figsize=(8, 6)) ax = fig[0] ax.ellipse([.3 / dx, 0.5 * (ymin + ymax) / dx + 2], [radius / dx, radius / dx], 'r') image = ax.image(vorticity(sol), cmap='cubehelix', clim=[0, .05]) def update(iframe): nrep = 64 for i in range(nrep): sol.one_time_step() image.set_data(vorticity(sol)) ax.title = "Solution t={0:f}".format(sol.t) # run the simulation fig.animate(update, interval=1) fig.show()
def run(space_step, final_time, generator="cython", sorder=None, with_plot=True): """ Parameters ---------- space_step: double spatial step final_time: double final time generator: string pylbm generator sorder: list storage order with_plot: boolean if True plot the solution otherwise just compute the solution Returns ------- sol <class 'pylbm.simulation.Simulation'> """ # parameters xmin, xmax, ymin, ymax = 0., 1., 0., 1. # bounds of the domain c_x, c_y = 0.2, 0.5 # velocity of the advection la = 2. # scheme velocity sigma_q = 1.e-2 # 1./np.sqrt(12) sigma_xy = 0.5 # sigma_q symb_sq = 1 / (.5 + SIGMA_0) symb_sxy = 1 / (.5 + SIGMA_1) s = [0., symb_sq, symb_sq, symb_sxy] # relaxation parameters simu_cfg = { 'box': { 'x': [xmin, xmax], 'y': [ymin, ymax], 'label': -1 }, 'space_step': space_step, 'scheme_velocity': LA, 'schemes': [ { 'velocities': list(range(1, 5)), 'conserved_moments': U, 'polynomials': [1, X, Y, X**2 - Y**2], 'relaxation_parameters': s, 'equilibrium': [U, CX * U, CY * U, 0], }, ], 'init': { U: (u_init, (xmin, xmax, ymin, ymax)) }, 'generator': generator, 'parameters': { LA: la, CX: c_x, CY: c_y, SIGMA_0: sigma_q, SIGMA_1: sigma_xy, }, 'relative_velocity': [CX, CY], } # build the simulations sol = pylbm.Simulation(simu_cfg, sorder=sorder) if with_plot: # create the viewer to plot the solution viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig(dim=3) axe = fig[0] surf = axe.SurfaceScatter(sol.domain.x, sol.domain.y, sol.m[U], size=2, sampling=4, color='navy') axe.title = 'Advection' axe.grid(visible=False) axe.set_label(r'$x$', r'$y$', r'$u$') def update(iframe): # pylint: disable=unused-argument if sol.t < final_time: for _ in range(16): sol.one_time_step() surf.update(sol.m[U]) fig.animate(update, interval=1) fig.show() else: while sol.t < final_time: sol.one_time_step() return sol
def run(dx, Tf, time_bc, generator="cython", sorder=None, with_plot=True): """ Parameters ---------- dx: double spatial step Tf: double final time time_bc: boolean set the bottom boundary condition that evolves over time generator: pylbm generator sorder: list storage order with_plot: boolean if True plot the solution otherwise just compute the solution """ # parameters Td, Tu = 0.5, -0.5 xmin, xmax, ymin, ymax = 0., 2., 0., 1. Ra = 2000 Pr = 0.71 Ma = 0.01 alpha = .005 la = 1. # velocity of the scheme rhoo = 1. g = 9.81 nu = np.sqrt(Pr*alpha*9.81*(Td-Tu)*(ymax-ymin)/Ra) kappa = nu/Pr eta = nu snu = 1./(.5+3*nu) seta = 1./(.5+3*eta) sq = 8*(2-snu)/(8-snu) se = seta sf = [0., 0., 0., seta, se, sq, sq, snu, snu] a = .5 skappa = 1./(.5+10*kappa/(4+a)) se = 1./(.5+np.sqrt(3)/3) snu = se sT = [0., skappa, skappa, se, snu] if time_bc: bc = { 0:{'method':{0: pylbm.bc.BouzidiBounceBack, 1: pylbm.bc.BouzidiAntiBounceBack}, 'value': (bc_down_with_time, (Td, Tu)), 'time_bc': True}, 1:{'method':{0: pylbm.bc.BouzidiBounceBack, 1: pylbm.bc.BouzidiAntiBounceBack}, 'value':(bc_up, (Tu,))}, } else: bc = { 0:{'method':{0: pylbm.bc.BouzidiBounceBack, 1: pylbm.bc.BouzidiAntiBounceBack}, 'value': (bc_down, (Td,))}, 1:{'method':{0: pylbm.bc.BouzidiBounceBack, 1: pylbm.bc.BouzidiAntiBounceBack}, 'value':(bc_up, (Tu,))}, } dico = { 'box':{'x':[xmin, xmax], 'y':[ymin, ymax], 'label':[-1, -1, 0, 1]}, 'space_step':dx, 'scheme_velocity':la, 'schemes':[ { 'velocities':list(range(9)), 'conserved_moments': [rho, qx, qy], 'polynomials':[ 1, X, Y, 3*(X**2+Y**2)-4, 0.5*(9*(X**2+Y**2)**2-21*(X**2+Y**2)+8), 3*X*(X**2+Y**2)-5*X, 3*Y*(X**2+Y**2)-5*Y, X**2-Y**2, X*Y ], 'relaxation_parameters':sf, 'equilibrium':[ rho, qx, qy, -2*rho + 3*(qx**2+qy**2), rho - 3*(qx**2+qy**2), -qx, -qy, qx**2 - qy**2, qx*qy ], 'source_terms':{qy: alpha*g*T}, }, { 'velocities':list(range(5)), 'conserved_moments':T, 'polynomials':[1, X, Y, 5*(X**2+Y**2) - 4, (X**2-Y**2)], 'equilibrium':[T, T*qx, T*qy, a*T, 0.], 'relaxation_parameters':sT, }, ], 'init':{rho: 1., qx: 0., qy: 0., T: (init_T, (Td, Tu, xmin, xmax, ymin, ymax))}, 'boundary_conditions': bc, 'generator': generator, } sol = pylbm.Simulation(dico) x, y = sol.domain.x, sol.domain.y if with_plot: viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig() ax = fig[0] image = ax.image(sol.m[T].T, cmap='cubehelix', clim=[Tu, Td+.25]) def update(iframe): nrep = 64 for i in range(nrep): sol.one_time_step() image.set_data(sol.m[T].T) ax.title = "Solution t={0:f}".format(sol.t) # run the simulation fig.animate(update, interval=1) fig.show() else: while sol.t < Tf: sol.one_time_step() return sol
def run(dx, Tf, generator="cython", sorder=None, withPlot=True): """ Parameters ---------- dx: double spatial step Tf: double final time generator: pylbm generator sorder: list storage order withPlot: boolean if True plot the solution otherwise just compute the solution """ # parameters gamma = 1.4 xmin, xmax = 0., 1. la = 3. # velocity of the scheme rho_L, rho_R, p_L, p_R, u_L, u_R = 1., 1. / 8., 1., 0.1, 0., 0. q_L = rho_L * u_L q_R = rho_R * u_R E_L = rho_L * u_L**2 + p_L / (gamma - 1.) E_R = rho_R * u_R**2 + p_R / (gamma - 1.) s_rho, s_q, s_E = 1.9, 1.5, 1.4 dico = { 'box': { 'x': [xmin, xmax], 'label': 0 }, 'space_step': dx, 'scheme_velocity': la, 'schemes': [ { 'velocities': [1, 2], 'conserved_moments': rho, 'polynomials': [1, LA * X], 'relaxation_parameters': [0, s_rho], 'equilibrium': [rho, q], 'init': { rho: (Riemann_pb, (xmin, xmax, rho_L, rho_R)) }, }, { 'velocities': [1, 2], 'conserved_moments': q, 'polynomials': [1, LA * X], 'relaxation_parameters': [0, s_q], 'equilibrium': [q, (gamma - 1.) * E + 0.5 * (3. - gamma) * q**2 / rho], 'init': { q: (Riemann_pb, (xmin, xmax, q_L, q_R)) }, }, { 'velocities': [1, 2], 'conserved_moments': E, 'polynomials': [1, LA * X], 'relaxation_parameters': [0, s_E], 'equilibrium': [E, gamma * E * q / rho - 0.5 * (gamma - 1.) * q**3 / rho**2], 'init': { E: (Riemann_pb, (xmin, xmax, E_L, E_R)) }, }, ], 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.Neumann, 1: pylbm.bc.Neumann, 2: pylbm.bc.Neumann }, }, }, 'parameters': { LA: la }, 'generator': generator, } sol = pylbm.Simulation(dico, sorder=sorder) while (sol.t < Tf): sol.one_time_step() if withPlot: x = sol.domain.x rho_n = sol.m[rho] q_n = sol.m[q] E_n = sol.m[E] u = q_n / rho_n p = (gamma - 1.) * (E_n - .5 * rho_n * u**2) e = E_n / rho_n - .5 * u**2 viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig(2, 3) fig[0, 0].plot(x, rho_n) fig[0, 0].title = 'mass' fig[0, 1].plot(x, u) fig[0, 1].title = 'velocity' fig[0, 2].plot(x, p) fig[0, 2].title = 'pressure' fig[1, 0].plot(x, E_n) fig[1, 0].title = 'energy' fig[1, 1].plot(x, q_n) fig[1, 1].title = 'momentum' fig[1, 2].plot(x, e) fig[1, 2].title = 'internal energy' fig.show() return sol
def run(dx, Tf, generator="numpy", sorder=None, withPlot=True): """ Parameters ---------- dx: double spatial step Tf: double final time generator: pylbm generator sorder: list storage order withPlot: boolean if True plot the solution otherwise just compute the solution """ # parameters xmin, xmax = -1., 1. # bounds of the domain uL = 0.3 # left value uR = 0.0 # right value L = 0.2 # length of the middle area la = 1. # scheme velocity (la = dx/dt) s = 1.8 # relaxation parameter # dictionary for the D1Q2 dico1 = { 'box': { 'x': [xmin, xmax], 'label': 0 }, 'space_step': dx, 'scheme_velocity': LA, 'schemes': [ { 'velocities': [1, 2], 'conserved_moments': [u], 'polynomials': [1, LA * X], 'relaxation_parameters': [0., s], 'equilibrium': [u, u**2 / 2], 'init': { u: (u0, (xmin, xmax, uL, uR)) }, }, ], 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.Neumann } }, }, 'generator': generator, 'parameters': { LA: la }, } # dictionary for the D1Q3 dico2 = { 'box': { 'x': [xmin, xmax], 'label': 0 }, 'space_step': dx, 'scheme_velocity': LA, 'schemes': [ { 'velocities': list(range(3)), 'conserved_moments': u, 'polynomials': [1, LA * X, LA**2 * X**2], 'relaxation_parameters': [0., s, s], 'equilibrium': [u, u**2 / 2, LA**2 * u / 3 + 2 * u**3 / 9], 'init': { u: (u0, (xmin, xmax, uL, uR)) }, }, ], 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.Neumann } }, }, 'generator': generator, 'parameters': { LA: la }, } # simulation sol = pylbm.Simulation(dico1, sorder=sorder) # build the simulation with D1Q2 title = 'D1Q2' # sol = pylbm.Simulation(dico2, sorder=sorder) # build the simulation with D1Q3 # title = 'D1Q3' if withPlot: # create the viewer to plot the solution viewer = pylbm.viewer.matplotlibViewer fig = viewer.Fig() ax = fig[0] ymin, ymax = min([uL, uR]) - .1 * abs(uL - uR), max( [uL, uR]) + .1 * abs(uL - uR) ax.axis(xmin, xmax, ymin, ymax) x = sol.domain.x l = ax.plot(x, sol.m[u], width=1, color='b', label=title)[0] le = ax.plot(x, solution(sol.t, x, xmin, xmax, uL, uR), width=1, color='k', label='exact')[0] def update(iframe): if sol.t < Tf: sol.one_time_step() l.set_data(x, sol.m[u]) le.set_data(x, solution(sol.t, x, xmin, xmax, uL, uR)) ax.title = 'solution at t = {0:f}'.format(sol.t) ax.legend() fig.animate(update) fig.show() else: while sol.t < Tf: sol.one_time_step() return sol
def run(space_step, final_time, generator="numpy", sorder=None, with_plot=True): """ Parameters ---------- space_step: double spatial step final_time: double final time generator: string pylbm generator sorder: list storage order with_plot: boolean if True plot the solution otherwise just compute the solution Returns ------- sol <class 'pylbm.simulation.Simulation'> """ # parameters xmin, xmax = 0., 1. # bounds of the domain la = 1. # scheme velocity (la = dx/dt) c = 0.25 # velocity of the advection mu = 1. # parameter of the source term s = 2. # relaxation parameter # dictionary of the simulation simu_cfg = { 'box': {'x': [xmin, xmax], 'label': -1}, 'space_step': space_step, 'scheme_velocity': LA, 'schemes': [ { 'velocities': [1, 2], 'conserved_moments': U, 'polynomials': [1, X], 'relaxation_parameters': [0., s], 'equilibrium': [U, C*U], 'source_terms': {U: MU*U*(1-U)}, }, ], 'init': {U: (u_init, (xmin, xmax))}, 'generator': generator, 'parameters': {LA: la, C: c, MU: mu}, } # build the simulation sol = pylbm.Simulation(simu_cfg, sorder=sorder) if with_plot: # create the viewer to plot the solution viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig() axe = fig[0] ymin, ymax = -.2, 1.2 axe.axis(xmin, xmax, ymin, ymax) x = sol.domain.x l1a = axe.CurveScatter( x, sol.m[U], color='navy', label='D1Q2' ) l1e = axe.CurveLine( x, solution(sol.t, x, xmin, xmax, c, mu), label='exact' ) axe.legend() def update(iframe): # pylint: disable=unused-argument # increment the solution of one time step if sol.t < final_time: sol.one_time_step() l1a.update(sol.m[U]) l1e.update(solution(sol.t, x, xmin, xmax, c, mu)) axe.title = 'solution at t = {0:f}'.format(sol.t) fig.animate(update) fig.show() else: while sol.t < final_time: sol.one_time_step() return sol
def run(dx, Tf, generator="cython", sorder=None, with_plot=True): """ Parameters ---------- dx: double spatial step Tf: double final time generator: pylbm generator sorder: list storage order with_plot: boolean if True plot the solution otherwise just compute the solution """ # parameters la = 1. # velocity of the scheme width = 2 height = 1 max_velocity = 0.1 rhoo = 1. mu = 0.00185 zeta = 1.e-2 xmin, xmax, ymin, ymax = 0.0, width, -0.5*height, 0.5*height grad_pressure = - max_velocity * 8.0 / (height)**2 * 3.0/(la**2*rhoo) * mu dummy = 3.0/(la*rhoo*dx) s1 = 1.0/(0.5+zeta*dummy) s2 = 1.0/(0.5+mu*dummy) s = [0.,0.,0.,s1,s1,s1,s1,s2,s2] dummy = 1./(LA**2*rhoo) qx2 = dummy*qx**2 qy2 = dummy*qy**2 q2 = qx2+qy2 qxy = dummy*qx*qy dico = { 'box':{'x':[xmin, xmax], 'y':[ymin, ymax], 'label':[2, 1, 0, 0]}, 'space_step':dx, 'scheme_velocity':la, 'schemes':[{'velocities':list(range(9)), 'polynomials':[1, LA*X, LA*Y, 3*(X**2+Y**2)-4, 0.5*(9*(X**2+Y**2)**2-21*(X**2+Y**2)+8), 3*X*(X**2+Y**2)-5*X, 3*Y*(X**2+Y**2)-5*Y, X**2-Y**2, X*Y], 'relaxation_parameters':s, 'equilibrium':[rho, qx, qy, -2*rho + 3*q2, rho - 3*q2, -qx/LA, -qy/LA, qx2 - qy2, qxy], 'conserved_moments': [rho, qx, qy], }], 'parameters':{LA:la}, 'init':{rho: 1., qx: 0., qy: 0. }, 'boundary_conditions':{ 0:{'method':{0: pylbm.bc.BouzidiBounceBack}}, 1:{'method':{0: pylbm.bc.NeumannX}}, 2:{'method':{0: pylbm.bc.BouzidiBounceBack}, 'value':(bc_in, (width, height, max_velocity, grad_pressure))} }, 'generator': generator, } sol = pylbm.Simulation(dico, sorder=sorder) if with_plot: # init viewer viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig() ax = fig[0] nt = int(sol.domain.shape_in[0]/2) y = sol.domain.y l1 = ax.plot(y, sol.m[qx][nt], color='r', marker='+', label='LBM')[0] l2 = ax.plot(y, rhoo*max_velocity * (1.-4.*y**2/height**2), color='k', label='exact') ax.title = 'Velocity at t = {0:f}'.format(sol.t) ax.legend() def update(iframe): sol.one_time_step() l1.set_data(y, sol.m[qx][nt]) ax.title = 'Velocity at t = {0:f}'.format(sol.t) # run the simulation fig.animate(update, interval=1) fig.show() else: while sol.t < Tf: sol.one_time_step() return sol
def run(dx, Tf, generator="cython", sorder=None, withPlot=True): """ Parameters ---------- dx: double spatial step Tf: double final time generator: pylbm generator sorder: list storage order withPlot: boolean if True plot the solution otherwise just compute the solution """ # parameters xmin, xmax, ymin, ymax = 0., 1., 0., 1 rayon = 0.25 * (xmax - xmin) la = 1. # velocity of the scheme rhoo = 1. uo = 0.05 mu = 2.5e-5 #0.00185 zeta = 10 * mu dummy = 3.0 / (la * rhoo * dx) s3 = 1.0 / (0.5 + zeta * dummy) s4 = s3 s5 = s4 s6 = s4 s7 = 1.0 / (0.5 + mu * dummy) s8 = s7 s = [0., 0., 0., s3, s4, s5, s6, s7, s8] dummy = 1. / (LA**2 * rhoo) qx2 = dummy * qx**2 qy2 = dummy * qy**2 q2 = qx2 + qy2 qxy = dummy * qx * qy xc = xmin + 0.75 * (xmax - xmin) yc = ymin + 0.75 * (ymax - ymin) dico = { 'box': { 'x': [xmin, xmax], 'y': [ymin, ymax], 'label': [2, 0, 1, 0] }, 'elements': [pylbm.Parallelogram((xmin, ymin), (xc, ymin), (xmin, yc), label=0)], 'scheme_velocity': la, 'space_step': dx, 'schemes': [{ 'velocities': list(range(9)), 'polynomials': [ 1, X, Y, 3 * (X**2 + Y**2) - 4, 0.5 * (9 * (X**2 + Y**2)**2 - 21 * (X**2 + Y**2) + 8), 3 * X * (X**2 + Y**2) - 5 * X, 3 * Y * (X**2 + Y**2) - 5 * Y, X**2 - Y**2, X * Y ], 'relaxation_parameters': s, 'equilibrium': [ rho, qx, qy, -2 * rho + 3 * q2, rho - 3 * q2, -qx, -qy, qx2 - qy2, qxy ], 'conserved_moments': [rho, qx, qy], 'init': { rho: rhoo, qx: 0., qy: 0. }, }], 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.BouzidiBounceBack } }, 1: { 'method': { 0: pylbm.bc.NeumannY } }, 2: { 'method': { 0: pylbm.bc.BouzidiBounceBack }, 'value': (bc_in, (rhoo, uo, ymin, ymax)) } }, 'generator': generator, 'parameters': { LA: la }, # 'show_code': True } sol = pylbm.Simulation(dico, sorder=sorder) if withPlot: # init viewer viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig() ax = fig[0] image = ax.image(norme_q, (sol, ), cmap='jet', clim=[0, uo**2]) ax.polygon([[xmin / dx, ymin / dx], [xmin / dx, yc / dx], [xc / dx, yc / dx], [xc / dx, ymin / dx]], 'k') def update(iframe): nrep = 64 for i in range(nrep): sol.one_time_step() image.set_data(norme_q(sol)) ax.title = "Solution t={0:f}".format(sol.t) # run the simulation fig.animate(update, interval=1) fig.show() else: while sol.t < Tf: sol.one_time_step() return sol
def run(space_step, final_time, generator="numpy", sorder=None, with_plot=True): """ Parameters ---------- space_step: double spatial step final_time: double final time generator: string pylbm generator sorder: list storage order with_plot: boolean if True plot the solution otherwise just compute the solution Returns ------- sol <class 'pylbm.simulation.Simulation'> """ # parameters reg = 1 # regularity of the initial condition xmin, xmax = -1., 1. # bounds of the domain la = 1. # lattice velocity (la = dx/dt) s_0 = 1.8 # relaxation parameter for the D1Q2 s_1, s_2 = 1.4, 1.0 # relaxation parameter for the D1Q3 symb_s0 = 1 / (0.5 + SIGMA_0) # symbolic relaxation parameter symb_s1 = 1 / (0.5 + SIGMA_1) # symbolic relaxation parameter symb_s2 = 1 / (0.5 + SIGMA_2) # symbolic relaxation parameter # fixed bounds of the graphics ymin, ymax = -.1, 1.1 # dictionary of the simulation for the D1Q2 simu_cfg_d1q2 = { 'box': { 'x': [xmin, xmax], 'label': 0 }, 'space_step': space_step, 'scheme_velocity': LA, 'schemes': [ { 'velocities': [1, 2], 'conserved_moments': U, 'polynomials': [1, X], 'relaxation_parameters': [0., symb_s0], 'equilibrium': [U, U**2 / 2], 'init': { U: (u_init, (xmin, xmax, reg)) }, }, ], 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.Neumann } }, }, 'generator': generator, 'parameters': { LA: la, SIGMA_0: 1 / s_0 - .5 }, 'show_code': False, } # dictionary of the simulation for the D1Q3 simu_cfg_d1q3 = { 'box': { 'x': [xmin, xmax], 'label': 0 }, 'space_step': space_step, 'scheme_velocity': LA, 'schemes': [ { 'velocities': [0, 1, 2], 'conserved_moments': U, 'polynomials': [1, X, X**2], 'relaxation_parameters': [0., symb_s1, symb_s2], 'equilibrium': [U, U**2 / 2, LA**2 * U / 3 + 2 * U**3 / 9], 'init': { U: (u_init, (xmin, xmax, reg)) }, }, ], 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.Neumann } }, }, 'generator': generator, 'parameters': { LA: la, SIGMA_1: 1 / s_1 - .5, SIGMA_2: 1 / s_2 - .5 }, 'relative_velocity': [U], 'show_code': False, } # build the simulations sol_d1q2 = pylbm.Simulation(simu_cfg_d1q2, sorder=sorder) sol_d1q3 = pylbm.Simulation(simu_cfg_d1q3, sorder=sorder) if with_plot: # create the viewer to plot the solution viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig() axe = fig[0] axe.axis(xmin, xmax, ymin, ymax) axe.set_label(r'$x$', r'$u$') x_d1q2 = sol_d1q2.domain.x l1a = axe.CurveScatter(x_d1q2, sol_d1q2.m[U], color='navy', label=r'$D_1Q_2$') x_d1q3 = sol_d1q3.domain.x l1b = axe.CurveScatter( x_d1q3, sol_d1q3.m[U], color='orange', label=r'$D_1Q_3$', ) axe.legend( loc='upper right', shadow=False, frameon=False, ) def update(iframe): # pylint: disable=unused-argument # increment the solution of one time step if sol_d1q2.t < final_time: sol_d1q2.one_time_step() l1a.update(sol_d1q2.m[U]) if sol_d1q3.t < final_time: sol_d1q3.one_time_step() l1b.update(sol_d1q3.m[U]) axe.title = r'Burgers at $t = {0:f}$'.format(sol_d1q2.t) fig.animate(update) fig.show() else: while sol_d1q2.t < final_time: sol_d1q2.one_time_step() return sol_d1q2
def run(space_step, final_time, generator="cython", sorder=None, with_plot=True): """ Parameters ---------- space_step: double spatial step final_time: double final time generator: string pylbm generator sorder: list storage order with_plot: boolean if True plot the solution otherwise just compute the solution Returns ------- sol <class 'pylbm.simulation.Simulation'> """ # parameters scheme_name = 'Geier' xmin, xmax, ymin, ymax = 0., 4., 0., 0.5 # bounds of the domain width = 0.25 # radius of the obstacle la = 1. # velocity of the scheme rho_o = 1. # reference value of the mass u_o = 0.10 # boundary value of the velocity mu = 2.5e-7 # bulk viscosity zeta = 1.e-3 # shear viscosity def moments_choice(scheme_name, mu, zeta): if scheme_name == 'dHumiere': dummy = 1. / rho_o QX2 = dummy * QX**2 QY2 = dummy * QY**2 Q2 = QX2 + QY2 QXY = dummy * QX * QY polynomials = [ 1, X, Y, 3 * (X**2 + Y**2) - 4 * LA**2, 0.5 * (9 * (X**2 + Y**2)**2 - 21 * (X**2 + Y**2) * LA**2 + 8 * LA**4), 3 * X * (X**2 + Y**2) - 5 * X * LA**2, 3 * Y * (X**2 + Y**2) - 5 * Y * LA**2, X**2 - Y**2, X * Y ] equilibrium = [ RHO, QX, QY, -2 * RHO * LA**2 + 3 * Q2, RHO * LA**2 - 3 * Q2, -QX * LA**2, -QY * LA**2, QX2 - QY2, QXY ] dummy = 3.0 / (la * rho_o * space_step) sigma_1 = dummy * zeta sigma_2 = dummy * mu s_1 = 1 / (.5 + sigma_1) s_2 = 1 / (.5 + sigma_2) if scheme_name == 'Geier': UX, UY = QX / RHO, QY / RHO RHOU2 = RHO * (UX**2 + UY**2) polynomials = [ 1, X, Y, X**2 + Y**2, X * Y**2, Y * X**2, X**2 * Y**2, X**2 - Y**2, X * Y, ] equilibrium = [ RHO, QX, QY, RHOU2 + 2 / 3 * RHO * LA**2, QX * (LA**2 / 3 + UY**2), QY * (LA**2 / 3 + UX**2), RHO * (LA**2 / 3 + UX**2) * (LA**2 / 3 + UY**2), RHO * (UX**2 - UY**2), RHO * UX * UY, ] dummy = 3.0 / (la * rho_o * space_step) sigma_1 = dummy * (zeta - 2 * mu / 3) sigma_2 = dummy * mu s_1 = 1 / (.5 + sigma_1) s_2 = 1 / (.5 + sigma_2) if scheme_name == 'Lallemand': dummy = 1. / rho_o QX2 = dummy * QX**2 QY2 = dummy * QY**2 Q2 = QX2 + QY2 QXY = dummy * QX * QY polynomials = [ 1, X, Y, X**2 + Y**2, X * (X**2 + Y**2), Y * (X**2 + Y**2), (X**2 + Y**2)**2, X**2 - Y**2, X * Y, ] equilibrium = [ RHO, QX, QY, Q2 + 2 / 3 * LA**2 * RHO, 4 / 3 * QX * LA**2, 4 / 3 * QY * LA**2, ((21 * Q2 + 6 * RHO * LA**2) * LA**2 - (6 * Q2 - 2 * RHO * LA**2)) / 9, QX2 - QY2, QXY, ] dummy = 3.0 / (la * rho_o * space_step) sigma_1 = dummy * zeta sigma_2 = dummy * mu s_1 = 1 / (.5 + sigma_1) s_2 = 1 / (.5 + sigma_2) s = [0., 0., 0., s_1, s_1, s_1, s_1, s_2, s_2] return polynomials, equilibrium, s polynomials, equilibrium, s = moments_choice(scheme_name, mu, zeta) simu_cfg = { 'box': { 'x': [xmin, xmax], 'y': [ymin, ymax], 'label': [2, 1, 0, 0] }, 'elements': [pylbm.Parallelogram((xmin, ymin), (width, 0), (0, width), label=0)], 'space_step': space_step, 'scheme_velocity': la, 'schemes': [ { 'velocities': list(range(9)), 'polynomials': polynomials, 'relaxation_parameters': s, 'equilibrium': equilibrium, 'conserved_moments': [RHO, QX, QY], }, ], 'parameters': { LA: la }, 'init': { RHO: rho_o, QX: 0., QY: 0. }, 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.BouzidiBounceBack } }, 1: { 'method': { 0: pylbm.bc.NeumannX } }, 2: { 'method': { 0: pylbm.bc.BouzidiBounceBack }, 'value': (bc_in, (rho_o, u_o, width, ymax)) }, }, 'generator': generator, 'relative_velocity': [QX / RHO, QY / RHO], # 'show_code': True } sol = pylbm.Simulation(simu_cfg, sorder=sorder) if with_plot: Re = rho_o * u_o * 2 * width / mu print("Reynolds number {0:10.3e}".format(Re)) # init viewer viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig(3, 1) ax_qx = fig[0] ax_qx.grid(visible=False) ax_qx.xaxis_set_visible(False) ax_qx.yaxis_set_visible(False) ax_qx.title = r"$u_x$ at $t={0:f}$".format(sol.t) ax_qy = fig[1] ax_qy.grid(visible=False) ax_qy.xaxis_set_visible(False) ax_qy.yaxis_set_visible(False) ax_qy.title = r"$u_y$" ax_v = fig[2] ax_v.grid(visible=False) ax_v.xaxis_set_visible(False) ax_v.yaxis_set_visible(False) ax_v.title = "vorticity" length = width / space_step ax_qx.polygon([[0, 0], [0, length], [length, length], [length, 0]], 'black') ax_qy.polygon([[0, 0], [0, length], [length, length], [length, 0]], 'black') ax_v.polygon([[0, 0], [0, length - 1], [length - 1, length - 1], [length - 1, 0]], 'black') surf_qx = ax_qx.SurfaceImage(sol.m[QX] / sol.m[RHO], cmap='jet', clim=[-u_o, u_o]) surf_qy = ax_qy.SurfaceImage(sol.m[QY] / sol.m[RHO], cmap='jet', clim=[-u_o, u_o]) surf_v = ax_v.SurfaceImage(vorticity(sol), cmap='jet', clim=[0, 0.025]) def update(iframe): # pylint: disable=unused-argument nrep = 64 for _ in range(nrep): sol.one_time_step() surf_qx.update(sol.m[QX] / sol.m[RHO]) surf_qy.update(sol.m[QY] / sol.m[RHO]) surf_v.update(vorticity(sol)) ax_qx.title = r"$u_x$ at $t={0:f}$".format(sol.t) # run the simulation fig.animate(update, interval=1) fig.show() else: while sol.t < final_time: sol.one_time_step() return sol
def run(dx, Tf, generator="numpy", sorder=None, withPlot=True): """ Parameters ---------- dx: double spatial step Tf: double final time generator: pylbm generator store: list storage order withPlot: boolean if True plot the solution otherwise just compute the solution """ # parameters xmin, xmax = 0., 1. # bounds of the domain la = 1. # scheme velocity (la = dx/dt) c = 0.25 # velocity of the advection s = 1.99 # relaxation parameter # dictionary of the simulation dico = { 'box': { 'x': [xmin, xmax], 'label': -1 }, 'space_step': dx, 'scheme_velocity': LA, 'schemes': [ { 'velocities': [1, 2], 'conserved_moments': u, 'polynomials': [1, LA * X], 'relaxation_parameters': [0., s], 'equilibrium': [u, C * u], 'init': { u: (u0, (xmin, xmax)) }, }, ], 'generator': generator, 'parameters': { LA: la, C: c }, } # simulation sol = pylbm.Simulation(dico, sorder=sorder) # build the simulation if withPlot: # create the viewer to plot the solution viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig() ax = fig[0] ymin, ymax = -.2, 1.2 ax.axis(xmin, xmax, ymin, ymax) x = sol.domain.x l1 = ax.plot(x, sol.m[u], width=2, color='b', label='D1Q2')[0] l2 = ax.plot(x, u0(x - c * sol.t, xmin, xmax), width=2, color='k', label='exact')[0] def update(iframe): if sol.t < Tf: # time loop sol.one_time_step() # increment the solution of one time step l1.set_data(x, sol.m[u]) l2.set_data(x, u0(x - c * sol.t, xmin, xmax)) ax.title = 'solution at t = {0:f}'.format(sol.t) ax.legend() fig.animate(update) fig.show() else: while sol.t < Tf: sol.one_time_step() return sol
qx: 0., qy: 0. }, }, ], 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.Bouzidi_bounce_back }, 'value': bc_in }, 1: { 'method': { 0: pylbm.bc.Bouzidi_bounce_back }, 'value': None }, 2: { 'method': { 0: pylbm.bc.Neumann_x }, 'value': None }, }, 'generator': 'cython', } sol = pylbm.Simulation(dico)
def run(space_step, final_time, generator="cython", sorder=None, with_plot=True): """ Parameters ---------- space_step: double spatial step final_time: double final time generator: string pylbm generator sorder: list storage order with_plot: boolean if True plot the solution otherwise just compute the solution Returns ------- sol <class 'pylbm.simulation.Simulation'> """ # parameters xmin, xmax, ymin, ymax = -1., 1., -1., 1. # bounds of the domain la = 4 # velocity of the scheme gravity = 1. # gravity sigma_hx = 1.e-3 sigma_hxy = 0.5 sigma_qx = 1.e-1 sigma_qxy = 0.5 symb_s_hx = 1 / (.5 + SIGMA_HX) symb_s_hxy = 1 / (.5 + SIGMA_HXY) symb_s_qx = 1 / (.5 + SIGMA_QX) symb_s_qxy = 1 / (.5 + SIGMA_QXY) s_h = [0., symb_s_hx, symb_s_hx, symb_s_hxy] s_q = [0., symb_s_qx, symb_s_qx, symb_s_qxy] vitesse = list(range(1, 5)) polynomes = [1, X, Y, X**2 - Y**2] simu_cfg = { 'parameters': { LA: la, G: gravity, SIGMA_HX: sigma_hx, SIGMA_HXY: sigma_hxy, SIGMA_QX: sigma_qx, SIGMA_QXY: sigma_qxy, }, 'box': { 'x': [xmin, xmax], 'y': [ymin, ymax], 'label': -1 }, 'space_step': space_step, 'scheme_velocity': LA, 'schemes': [ { 'velocities': vitesse, 'conserved_moments': H, 'polynomials': polynomes, 'relaxation_parameters': s_h, 'equilibrium': [H, QX, QY, 0.], 'init': { H: (h_init, (xmin, xmax, ymin, ymax)) }, }, { 'velocities': vitesse, 'conserved_moments': QX, 'polynomials': polynomes, 'relaxation_parameters': s_q, 'equilibrium': [QX, QX**2 / H + G * H**2 / 2, QX * QY / H, 0.], 'init': { QX: 0. }, }, { 'velocities': vitesse, 'conserved_moments': QY, 'polynomials': polynomes, 'relaxation_parameters': s_q, 'equilibrium': [QY, QX * QY / H, QY**2 / H + G * H**2 / 2, 0.], 'init': { QY: 0. }, }, ], 'relative_velocity': [QX / H, QY / H], 'generator': generator, } # build the simulations sol = pylbm.Simulation(simu_cfg, sorder=sorder) if with_plot: # create the viewer to plot the solution viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig(dim=3) axe = fig[0] surf = axe.SurfaceScatter(sol.domain.x, sol.domain.y, sol.m[H], size=2, sampling=4, color='navy') axe.title = 'Shallow water' axe.grid(visible=False) axe.set_label(r'$x$', r'$y$', r'$h$') def update(iframe): # pylint: disable=unused-argument if sol.t < final_time: for _ in range(16): sol.one_time_step() surf.update(sol.m[H]) fig.animate(update, interval=1) fig.show() else: while sol.t < final_time: sol.one_time_step() return sol
def run(space_step, final_time, generator="cython", sorder=None, with_plot=True): """ Parameters ---------- space_step: double spatial step final_time: double final time generator: string pylbm generator sorder: list storage order with_plot: boolean if True plot the solution otherwise just compute the solution Returns ------- sol <class 'pylbm.simulation.Simulation'> """ # parameters gamma = 1.4 # ratio of specific heats xmin, xmax = 0., 1. # bounds of the domain la = 3. # velocity of the scheme s_rho, s_u, s_p = 1.9, 1.5, 1.4 # relaxation parameters symb_s_rho = 1 / (.5 + SIGMA_RHO) # symbolic relaxation parameter symb_s_u = 1 / (.5 + SIGMA_U) # symbolic relaxation parameter symb_s_p = 1 / (.5 + SIGMA_P) # symbolic relaxation parameter # initial values rho_left, p_left, u_left = 1, 1, 0 # left state rho_right, p_right, u_right = 1 / 8, 0.1, 0 # right state q_left = rho_left * u_left q_right = rho_right * u_right rhoe_left = rho_left * u_left**2 + p_left / (gamma - 1.) rhoe_right = rho_right * u_right**2 + p_right / (gamma - 1.) # discontinuity position xmid = .5 * (xmin + xmax) exact_solution = exact_solver({ 'jump abscissa': xmid, 'left state': [rho_left, u_left, p_left], 'right state': [rho_right, u_right, p_right], 'gamma': gamma, }) exact_solution.diagram() simu_cfg = { 'box': { 'x': [xmin, xmax], 'label': 0 }, 'space_step': space_step, 'scheme_velocity': LA, 'schemes': [ { 'velocities': [1, 2], 'conserved_moments': RHO, 'polynomials': [1, X], 'relaxation_parameters': [0, symb_s_rho], 'equilibrium': [RHO, Q], }, { 'velocities': [1, 2], 'conserved_moments': Q, 'polynomials': [1, X], 'relaxation_parameters': [0, symb_s_u], 'equilibrium': [Q, (GAMMA - 1) * E + (3 - GAMMA) / 2 * Q**2 / RHO], }, { 'velocities': [1, 2], 'conserved_moments': E, 'polynomials': [1, X], 'relaxation_parameters': [0, symb_s_p], 'equilibrium': [E, GAMMA * E * Q / RHO - (GAMMA - 1) / 2 * Q**3 / RHO**2], }, ], 'init': { RHO: (riemann_pb, (xmid, rho_left, rho_right)), Q: (riemann_pb, (xmid, q_left, q_right)), E: (riemann_pb, (xmid, rhoe_left, rhoe_right)) }, 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.Neumann, 1: pylbm.bc.Neumann, 2: pylbm.bc.Neumann, }, }, }, 'parameters': { LA: la, SIGMA_RHO: 1 / s_rho - .5, SIGMA_U: 1 / s_u - .5, SIGMA_P: 1 / s_p - .5, GAMMA: gamma, }, 'generator': generator, } # build the simulation sol = pylbm.Simulation(simu_cfg, sorder=sorder) # build the equivalent PDE eq_pde = pylbm.EquivalentEquation(sol.scheme) print(eq_pde) while sol.t < final_time: sol.one_time_step() if with_plot: x = sol.domain.x rho_n = sol.m[RHO] q_n = sol.m[Q] rhoe_n = sol.m[E] u_n = q_n / rho_n p_n = (gamma - 1.) * (rhoe_n - .5 * rho_n * u_n**2) e_n = rhoe_n / rho_n - .5 * u_n**2 sole = exact_solution.evaluate(x, sol.t) viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig(2, 3) fig[0, 0].CurveScatter(x, rho_n, color='navy') fig[0, 0].CurveLine(x, sole[0], color='orange') fig[0, 0].title = 'mass' fig[0, 1].CurveScatter(x, u_n, color='navy') fig[0, 1].CurveLine(x, sole[1], color='orange') fig[0, 1].title = 'velocity' fig[0, 2].CurveScatter(x, p_n, color='navy') fig[0, 2].CurveLine(x, sole[2], color='orange') fig[0, 2].title = 'pressure' fig[1, 0].CurveScatter(x, rhoe_n, color='navy') rhoe_e = .5 * sole[0] * sole[1]**2 + sole[2] / (gamma - 1.) fig[1, 0].CurveLine(x, rhoe_e, color='orange') fig[1, 0].title = 'energy' fig[1, 1].CurveScatter(x, q_n, color='navy') fig[1, 1].CurveLine(x, sole[0] * sole[1], color='orange') fig[1, 1].title = 'momentum' fig[1, 2].CurveScatter(x, e_n, color='navy') fig[1, 2].CurveLine(x, sole[2] / sole[0] / (gamma - 1), color='orange') fig[1, 2].title = 'internal energy' fig.show() return sol
def run(dx, Tf, generator="cython", sorder=None, withPlot=True): """ Parameters ---------- dx: double spatial step Tf: double final time generator: pylbm generator sorder: list storage order withPlot: boolean if True plot the solution otherwise just compute the solution """ # parameters width = 1. height = .5 xmin, xmax, ymin, ymax = 0., width, -.5 * height, .5 * height la = 1. # velocity of the scheme max_velocity = 0.1 mu = 0.00185 zeta = 1.e-5 grad_pressure = -mu * max_velocity * 8. / height**2 cte = 3. dummy = 3.0 / (la * dx) s1 = 1.0 / (0.5 + zeta * dummy) s2 = 1.0 / (0.5 + mu * dummy) velocities = list(range(1, 5)) polynomes = [1, LA * X, LA * Y, X**2 - Y**2] dico = { 'box': { 'x': [xmin, xmax], 'y': [ymin, ymax], 'label': [2, 1, 0, 0] }, 'space_step': dx, 'scheme_velocity': la, 'schemes': [ { 'velocities': velocities, 'polynomials': polynomes, 'relaxation_parameters': [0., s1, s1, 1.], 'equilibrium': [p, ux, uy, 0.], 'conserved_moments': p, 'init': { p: 0. }, }, { 'velocities': velocities, 'polynomials': polynomes, 'relaxation_parameters': [0., s2, s2, 1.], 'equilibrium': [ux, ux**2 + p / cte, ux * uy, 0.], 'conserved_moments': ux, 'init': { ux: 0. }, }, { 'velocities': velocities, 'polynomials': polynomes, 'relaxation_parameters': [0., s2, s2, 1.], 'equilibrium': [uy, ux * uy, uy**2 + p / cte, 0.], 'conserved_moments': uy, 'init': { uy: 0. }, }, ], 'parameters': { LA: la }, 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.BouzidiBounceBack, 1: pylbm.bc.BouzidiAntiBounceBack, 2: pylbm.bc.BouzidiAntiBounceBack }, }, 1: { 'method': { 0: pylbm.bc.BouzidiAntiBounceBack, 1: pylbm.bc.NeumannX, 2: pylbm.bc.NeumannX }, 'value': (bc_out, (width, grad_pressure, cte)) }, 2: { 'method': { 0: pylbm.bc.BouzidiAntiBounceBack, 1: pylbm.bc.BouzidiAntiBounceBack, 2: pylbm.bc.BouzidiAntiBounceBack }, 'value': (bc_in, (width, height, max_velocity, grad_pressure, cte)), }, }, 'generator': generator, } sol = pylbm.Simulation(dico, sorder=sorder) while sol.t < Tf: sol.one_time_step() if withPlot: print("*" * 50) p_n = sol.m[p] ux_n = sol.m[ux] uy_n = sol.m[uy] x, y = np.meshgrid(*sol.domain.coords, sparse=True, indexing='ij') coeff = sol.domain.dx / np.sqrt(width * height) Err_p = coeff * np.linalg.norm(p_n - (x - 0.5 * width) * grad_pressure) Err_ux = coeff * np.linalg.norm(ux_n - max_velocity * (1 - 4 * y**2 / height**2)) Err_uy = coeff * np.linalg.norm(uy_n) print("Norm of the error on rho: {0:10.3e}".format(Err_p)) print("Norm of the error on qx: {0:10.3e}".format(Err_ux)) print("Norm of the error on qy: {0:10.3e}".format(Err_uy)) # init viewer viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig() ax = fig[0] ax.image(ux_n - max_velocity * (1 - 4 * y**2 / height**2)) ax.title = "Error on ux" fig.show() return sol
def run(dx, Tf, generator="cython", sorder=None, withPlot=True): """ Parameters ---------- dx: double spatial step Tf: double final time generator: pylbm generator sorder: list storage order withPlot: boolean if True plot the solution otherwise just compute the solution """ # parameters xmin, xmax, ymin, ymax = 0., 2., 0., 1. radius = 0.125 la = 1. # velocity of the scheme rhoo = 1. uo = 0.05 mu = 2.5e-5 #0.00185 zeta = 10 * mu dummy = 3.0 / (la * rhoo * dx) s1 = 1.0 / (0.5 + zeta * dummy) s2 = 1.0 / (0.5 + mu * dummy) s = [0., 0., 0., s1, s1, s1, s1, s2, s2] dummy = 1. / (LA**2 * rhoo) qx2 = dummy * qx**2 qy2 = dummy * qy**2 q2 = qx2 + qy2 qxy = dummy * qx * qy dico = { 'box': { 'x': [xmin, xmax], 'y': [ymin, ymax], 'label': [0, 1, 0, 0] }, 'elements': [pylbm.Circle([.3, 0.5 * (ymin + ymax) + 2 * dx], radius, label=2)], 'space_step': dx, 'scheme_velocity': la, 'schemes': [ { 'velocities': list(range(9)), 'polynomials': [ 1, LA * X, LA * Y, 3 * (X**2 + Y**2) - 4, 0.5 * (9 * (X**2 + Y**2)**2 - 21 * (X**2 + Y**2) + 8), 3 * X * (X**2 + Y**2) - 5 * X, 3 * Y * (X**2 + Y**2) - 5 * Y, X**2 - Y**2, X * Y ], 'relaxation_parameters': s, 'equilibrium': [ rho, qx, qy, -2 * rho + 3 * q2, rho - 3 * q2, -qx / LA, -qy / LA, qx2 - qy2, qxy ], 'conserved_moments': [rho, qx, qy], 'init': { rho: rhoo, qx: rhoo * uo, qy: 0. }, }, ], 'parameters': { LA: la }, 'boundary_conditions': { 0: { 'method': { 0: pylbm.bc.BouzidiBounceBack }, 'value': (bc_rect, (rhoo, uo)) }, 1: { 'method': { 0: pylbm.bc.NeumannX } }, 2: { 'method': { 0: pylbm.bc.BouzidiBounceBack } }, }, 'generator': generator, 'show_code': True } sol = pylbm.Simulation(dico, sorder=sorder) if withPlot: Re = rhoo * uo * 2 * radius / mu print("Reynolds number {0:10.3e}".format(Re)) # init viewer viewer = pylbm.viewer.matplotlib_viewer fig = viewer.Fig() ax = fig[0] ax.ellipse([.3 / dx, 0.5 * (ymin + ymax) / dx + 2], [radius / dx, radius / dx], 'r') image = ax.image(vorticity(sol), cmap='jet', clim=[0, .05]) def update(iframe): nrep = 100 for i in range(nrep): sol.one_time_step() image.set_data(vorticity(sol)) ax.title = "Solution t={0:f}".format(sol.t) # run the simulation fig.animate(update, interval=1) fig.show() else: while sol.t < Tf: sol.one_time_step() return sol