# pylint: disable=invalid-name

U, X = sp.symbols('u, X')
LA, C, SIGMA = sp.symbols('lambda, c, sigma', constants=True)

scheme_cfg = {
    'dim':
    1,
    'scheme_velocity':
    LA,
    'schemes': [
        {
            'velocities': [1, 2],
            'conserved_moments': U,
            'polynomials': [1, X],
            'equilibrium': [U, C * U],
            'relaxation_parameters': [0, 1 / (0.5 + SIGMA)],
        },
    ],
    'parameters': {
        LA: 1.,
        C: 0.1,
        SIGMA: 1. / 1.9 - .5,
    },
}

scheme = pylbm.Scheme(scheme_cfg)
eq_pde = pylbm.EquivalentEquation(scheme)

print(eq_pde)
Example #2
0
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
Example #3
0
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
Example #4
0
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
Example #5
0
 def get_eqpde(self):
     scheme = pylbm.Scheme(self.get_dictionary())
     eqpde = pylbm.EquivalentEquation(scheme)
     return eqpde
Example #6
0
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
Example #7
0
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
    la = 1.  # velocity of the scheme
    c_0 = la / sqrt(3)  # velocity of the pressure waves
    s_rho, s_u = 2., 1.85  # relaxation parameters

    symb_s_rho = 1 / (.5 + SIGMA_RHO)  # symbolic relaxation parameter
    symb_s_u = 1 / (.5 + SIGMA_U)  # symbolic relaxation parameter

    # initial values
    rhoo = 1
    drho = rhoo / 3
    rho_left, u_left = rhoo - drho, 0  # left state
    rho_right, u_right = rhoo + drho, 0  # right state
    q_left = rho_left * u_left
    q_right = rho_right * u_right
    # fixed bounds of the graphics
    ymina, ymaxa = rhoo - 2 * drho, rhoo + 2 * drho
    yminb, ymaxb = -.25, .1

    # discontinuity position
    xmid = .5 * (xmin + xmax)

    exact_solution = exact_solver({
        'jump abscissa': xmid,
        'left state': [rho_left, u_left],
        'right state': [rho_right, u_right],
        'sound_speed': c_0,
    })

    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],
                'init': {
                    RHO: (riemann_pb, (xmid, rho_left, rho_right))
                },
            },
            {
                'velocities': [1, 2],
                'conserved_moments': Q,
                'polynomials': [1, X],
                'relaxation_parameters': [0, symb_s_u],
                'equilibrium': [Q, Q**2 / RHO + CO**2 * RHO],
                'init': {
                    Q: (riemann_pb, (xmid, q_left, q_right))
                },
            },
        ],
        'boundary_conditions': {
            0: {
                'method': {
                    0: pylbm.bc.Neumann,
                    1: pylbm.bc.Neumann,
                },
            },
        },
        'parameters': {
            LA: la,
            SIGMA_RHO: 1 / s_rho - .5,
            SIGMA_U: 1 / s_u - .5,
            CO: c_0,
        },
        '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)

    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'$\rho$')
        axe1.xaxis_set_visible(False)
        axe2 = fig[1]
        axe2.axis(xmin, xmax, yminb, ymaxb)
        axe2.set_label(r"$x$", r"$u$")

        x = sol.domain.x
        l1a = axe1.CurveScatter(
            x,
            sol.m[RHO],
            color='navy',
            label=r'$D_1Q_2$',
        )
        sole = exact_solution.evaluate(x, sol.t)
        l1e = axe1.CurveLine(
            x,
            sole[0],
            width=1,
            label='exact',
        )
        l2a = axe2.CurveScatter(
            x,
            sol.m[Q] / sol.m[RHO],
            color='orange',
            label=r'$D_1Q_2$',
        )
        l2e = axe2.CurveLine(
            x,
            sole[1],
            width=1,
            label='exact',
        )
        axe1.legend(loc='lower right')
        axe2.legend(loc='lower right')

        def update(iframe):  # pylint: disable=unused-argument
            if sol.t < final_time:
                sol.one_time_step()
                l1a.update(sol.m[RHO])
                l2a.update(sol.m[Q] / sol.m[RHO])
                sole = exact_solution.evaluate(x, sol.t)
                l1e.update(sole[0])
                l2e.update(sole[1])
                axe1.title = r'isothermal Euler at $t = {0:f}$'.format(sol.t)

        fig.animate(update)
        fig.show()
    else:
        while sol.t < final_time:
            sol.one_time_step()

    return sol