Ejemplo n.º 1
0
def godunov(rho, u, p, dt, dx, gamma=1.4):
    # Boundary values are the same.
    rho_new = rho.copy()
    rho_new[1:-1] = 0.0
    u_new = u.copy()
    u_new[1:-1] = 0.0
    p_new = p.copy()
    p_new[1:-1] = 0.0

    for i in range(1, len(rho) - 1):
        # Left flux
        W_i = np.array([rho[i], u[i], p[i]])
        W_l = np.array([rho[i - 1], u[i - 1], p[i - 1]])
        W_r = W_i
        riemann = Riemann(W_l, W_r, gamma, x0=0)
        W = riemann.evaluate_single_state(0, 1)  # dx/dt = 0
        F_left = W2F(W, gamma=gamma)
        # Right flux
        W_l = W_i
        W_r = np.array([rho[i + 1], u[i + 1], p[i + 1]])
        riemann = Riemann(W_l, W_r, gamma, x0=0)
        W = riemann.evaluate_single_state(0, 1)
        F_right = W2F(W, gamma=gamma)
        # Compute at next time step
        U_i = W2U(W_i, gamma=gamma)
        U_new = U_i - dt/dx*(F_right - F_left)
        rho_new[i], u_new[i], p_new[i] = U2W(U_new)

    return rho_new, u_new, p_new
Ejemplo n.º 2
0
def _test_sample_multipleeval(test_file, W_l, W_r, gamma, t, x0=0):
    target_results = np.genfromtxt(test_file, skip_header=1)
    riemann = Riemann(W_l, W_r, gamma, x0)
    x = target_results[:, 0]
    W_target = target_results[:, 1:]
    W = riemann.evaluate_states(x, t)
    assert_allclose(W, W_target, rtol=0.002)
Ejemplo n.º 3
0
def _test_sample_multipleeval(test_file, W_l, W_r, gamma, t, x0=0):
    target_results = np.genfromtxt(test_file, skip_header=1)
    riemann = Riemann(W_l, W_r, gamma,  x0)
    x = target_results[:, 0]
    W_target = target_results[:, 1:]
    W = riemann.evaluate_states(x, t)
    assert_allclose(W, W_target, rtol=0.002)
Ejemplo n.º 4
0
def test_p_star():
    gamma = 1.4
    W_l = np.array([1, 6, 4])
    W_r = np.array([2, 6, 2])
    c_l, c_r = compute_c_from_primitives(W_l, W_r, gamma)
    pstar_waves, ustar_waves = compute_star(W_l, c_l, W_r, c_r, gamma)
    riemann = Riemann(W_l, W_r, gamma)
    riemann.update()
    assert_equal(pstar_waves, riemann.p_star)
    assert_equal(ustar_waves, riemann.u_star)
Ejemplo n.º 5
0
def test_almost_vacuum():
    W_l = np.array([1.0, -2.0, 0.4])
    W_r = np.array([1.0, 2.0, 0.4])
    gamma = 1.4
    riemann = Riemann(W_l, W_r, gamma)
    riemann.update()
    assert(riemann.p_star > 0.0)
    assert_equal(riemann.u_star, 0.0)
    assert_equal(np.array(riemann.left_wave.speed),
                 -np.array(riemann.right_wave.speed))
Ejemplo n.º 6
0
def test_p_star():
    gamma = 1.4
    W_l = np.array([1, 6, 4])
    W_r = np.array([2, 6, 2])
    c_l, c_r = compute_c_from_primitives(W_l, W_r, gamma)
    pstar_waves, ustar_waves = compute_star(W_l, c_l, W_r, c_r, gamma)
    riemann = Riemann(W_l, W_r, gamma)
    riemann.update()
    assert_equal(pstar_waves, riemann.p_star)
    assert_equal(ustar_waves, riemann.u_star)
Ejemplo n.º 7
0
def test_almost_vacuum():
    W_l = np.array([1.0, -2.0, 0.4])
    W_r = np.array([1.0, 2.0, 0.4])
    gamma = 1.4
    riemann = Riemann(W_l, W_r, gamma)
    riemann.update()
    assert (riemann.p_star > 0.0)
    assert_equal(riemann.u_star, 0.0)
    assert_equal(np.array(riemann.left_wave.speed),
                 -np.array(riemann.right_wave.speed))
Ejemplo n.º 8
0
def test_vacuum():
    x0 = 0.5
    gamma = 1.4
    W_l = np.array([1.0, -20, 1.0])
    W_r = np.array([1.0, 20, 1.0])
    riemann = Riemann(W_l, W_r, gamma, x0)
    try:
        riemann.update()
    except waves.VacuumCondition:
        return
    raise AssertionError
Ejemplo n.º 9
0
def test_vacuum():
    x0 = 0.5
    gamma = 1.4
    W_l = np.array([1.0, -20, 1.0])
    W_r = np.array([1.0, 20, 1.0])
    riemann = Riemann(W_l, W_r, gamma, x0)
    try:
        riemann.update()
    except waves.VacuumCondition:
        return
    raise AssertionError
Ejemplo n.º 10
0
def MUSCL(rho, u, p, dt, dx, gamma=1.4, limiter='zero'):
    # Boundary values are the same.
    rho_new = rho.copy()
    rho_new[1:-1] = 0.0
    u_new = u.copy()
    u_new[1:-1] = 0.0
    p_new = p.copy()
    p_new[1:-1] = 0.0
    # Update fluxes and conservative
    W = np.empty((len(rho), 3))
    U = np.empty_like(W)
    for i in range(len(U)):
        W[i] = np.array([rho[i], u[i], p[i]])
        U[i] = W2U(W[i], gamma=gamma)

    ##################
    # Predictor Step #
    ##################
    # Compute limited slope
    delta_W = limiters.limiter(W, name=limiter)
    F_l = np.empty_like(delta_W)
    F_r = F_l.copy()
    U_tilde = F_l.copy()
    W_tilde = F_l.copy()
    for i in range(len(delta_W)):
        F_l[i] = W2F(W[i] + 0.5 * delta_W[i])
        F_r[i] = W2F(W[i] - 0.5 * delta_W[i])
        U_tilde[i] = U[i] - dt / dx * (F_l[i] - F_r[i])
        W_tilde[i] = U2W(U_tilde[i])

    ####################
    ## Corrector Step ##
    ####################
    # Godunov-like
    W_l = 0.5 * (W + W_tilde + delta_W)
    W_r = 0.5 * (W + W_tilde - delta_W)
    W_rs = np.zeros_like(W_l)
    F_rs = W_rs.copy()
    for i in range(0, len(W_l) - 1):
        # Left flux
        riemann = Riemann(W_l[i], W_r[i + 1], gamma, x0=0)
        W_rs[i] = riemann.evaluate_single_state(0, 1)
        F_rs[i] = W2F(W_rs[i])

    U_new = U.copy()
    U_new[1:-1] = U[1:-1] - dt / dx * (F_rs[1:-1] - F_rs[0:-2])

    for i in range(len(U_new)):
        rho_new[i], u_new[i], p_new[i] = U2W(U_new[i])

    # pdb.set_trace()
    return rho_new, u_new, p_new
Ejemplo n.º 11
0
def MUSCL(rho, u, p, dt, dx, gamma=1.4, limiter='zero'):
    # Boundary values are the same.
    rho_new = rho.copy()
    rho_new[1:-1] = 0.0
    u_new = u.copy()
    u_new[1:-1] = 0.0
    p_new = p.copy()
    p_new[1:-1] = 0.0
    # Update fluxes and conservative
    W = np.empty((len(rho), 3))
    U = np.empty_like(W)
    for i in range(len(U)):
        W[i] = np.array([rho[i], u[i], p[i]])
        U[i] = W2U(W[i], gamma=gamma)

    ##################
    # Predictor Step #
    ##################
    # Compute limited slope
    delta_W = limiters.limiter(W, name=limiter)
    F_l = np.empty_like(delta_W)
    F_r = F_l.copy()
    U_tilde = F_l.copy()
    W_tilde = F_l.copy()
    for i in range(len(delta_W)):
        F_l[i] = W2F(W[i] + 0.5*delta_W[i])
        F_r[i] = W2F(W[i] - 0.5*delta_W[i])
        U_tilde[i] = U[i] - dt/dx*(F_l[i] - F_r[i])
        W_tilde[i] = U2W(U_tilde[i])

    ####################
    ## Corrector Step ##
    ####################
    # Godunov-like
    W_l = 0.5*(W + W_tilde + delta_W)
    W_r = 0.5*(W + W_tilde - delta_W)
    W_rs = np.zeros_like(W_l)
    F_rs = W_rs.copy()
    for i in range(0, len(W_l) - 1):
        # Left flux
        riemann = Riemann(W_l[i], W_r[i+1], gamma, x0=0)
        W_rs[i] = riemann.evaluate_single_state(0, 1)
        F_rs[i] = W2F(W_rs[i])

    U_new = U.copy()
    U_new[1:-1] = U[1:-1] - dt/dx*(F_rs[1:-1] - F_rs[0:-2])

    for i in range(len(U_new)):
        rho_new[i], u_new[i], p_new[i] = U2W(U_new[i])

    # pdb.set_trace()
    return rho_new, u_new, p_new
Ejemplo n.º 12
0
def _test_sample_singleeval(test_file, W_l, W_r, gamma, t, x0=0):
    target_results = np.genfromtxt(test_file, skip_header=1)
    riemann = Riemann(W_l, W_r, gamma,  x0)
    for x, rho, u, p in target_results:
        W_actual = riemann.evaluate_single_state(x, t)
        W_target = np.array([rho, u, p])
        try:
            compare(W_actual, W_target, 0.002)
        except AssertionError:
            print("x:", x)
            print("t:", t)
            print("S:", x / t)
            raise
Ejemplo n.º 13
0
def _test_sample_singleeval(test_file, W_l, W_r, gamma, t, x0=0):
    target_results = np.genfromtxt(test_file, skip_header=1)
    riemann = Riemann(W_l, W_r, gamma, x0)
    for x, rho, u, p in target_results:
        W_actual = riemann.evaluate_single_state(x, t)
        W_target = np.array([rho, u, p])
        try:
            compare(W_actual, W_target, 0.002)
        except AssertionError:
            print("x:", x)
            print("t:", t)
            print("S:", x / t)
            raise
Ejemplo n.º 14
0
def godunov(rho, u, p, dt, dx, gamma=1.4):
    # Boundary values are the same.
    rho_new = rho.copy()
    rho_new[1:-1] = 0.0
    u_new = u.copy()
    u_new[1:-1] = 0.0
    p_new = p.copy()
    p_new[1:-1] = 0.0

    for i in range(1, len(rho) - 1):
        # Left flux
        W_i = np.array([rho[i], u[i], p[i]])
        W_l = np.array([rho[i - 1], u[i - 1], p[i - 1]])
        W_r = W_i
        riemann = Riemann(W_l, W_r, gamma, x0=0)
        W = riemann.evaluate_single_state(0, 1)  # dx/dt = 0
        F_left = W2F(W, gamma=gamma)
        # Right flux
        W_l = W_i
        W_r = np.array([rho[i + 1], u[i + 1], p[i + 1]])
        riemann = Riemann(W_l, W_r, gamma, x0=0)
        W = riemann.evaluate_single_state(0, 1)
        F_right = W2F(W, gamma=gamma)
        # Compute at next time step
        U_i = W2U(W_i, gamma=gamma)
        U_new = U_i - dt / dx * (F_right - F_left)
        rho_new[i], u_new[i], p_new[i] = U2W(U_new)

    return rho_new, u_new, p_new
Ejemplo n.º 15
0
def test_constructor():
    W_L = np.array([1, 1, 1])
    W_R = np.array([1, 1, 1])
    riemann = Riemann(W_L, W_R, 1.4)
    assert riemann