def plot_trajectories(q_l, q_r, ax=None, t=None, xmax=None): states, speeds, reval, wave_types = exact_riemann_solution(q_l, q_r) def reval_with_speed(xi): q = reval(xi) u = q qu = np.vstack((q, u)) return qu # density of particles for trajectories: rho_left = q_l / 3. rho_right = q_r / 3. # compute trajectories: x_traj, t_traj, xmax = riemann_tools.compute_riemann_trajectories( states, speeds, reval_with_speed, wave_types, xmax=xmax, rho_left=rho_left, rho_right=rho_right) # plot trajectories along with waves in the x-t plane: riemann_tools.plot_riemann_trajectories(x_traj, t_traj, speeds, wave_types, xmax=xmax, ax=ax, t=t) ax.set_title('Particle trajectories')
def plot_riemann_trajectories(q_l, q_r, gamma=1.4, primitive=False): if primitive: q_left = euler.primitive_to_conservative(*q_l) q_right = euler.primitive_to_conservative(*q_r) else: q_left = q_l q_right = q_r ex_states, ex_speeds, reval, wave_types = exact_riemann_solution( q_left, q_right, gamma=gamma) def reval_rho_u(x): q = reval(x) rho = q[0] u = q[1] / q[0] rho_u = np.vstack((rho, u)) return rho_u # Specify density of trajectories to left and right: rho_l = q_left[0] / 10. rho_r = q_right[0] / 10. x_traj, t_traj, xmax = riemann_tools.compute_riemann_trajectories( ex_states, ex_speeds, reval_rho_u, wave_types, i_vel=1, rho_left=rho_l, rho_right=rho_r) riemann_tools.plot_riemann_trajectories(x_traj, t_traj, ex_speeds, wave_types)
def plot_car_trajectories(q_l, q_r, D, axes=None, t=None, xmax=None): states, speeds, reval, wave_types = exact_riemann_solution(q_l, q_r, D) def reval_with_speed(xi): q = reval(xi) u = 1 - q qu = np.vstack((q, u)) return qu # density of particles for trajectories: rho_left = q_l / 2. rho_right = q_r / 2. # compute trajectories: x_traj, t_traj, xmax = riemann_tools.compute_riemann_trajectories( states, speeds, reval_with_speed, wave_types, xmax=xmax, rho_left=rho_left, rho_right=rho_right) # plot trajectories along with waves in the x-t plane: axes = riemann_tools.plot_riemann_trajectories(x_traj, t_traj, speeds, wave_types, xmax=xmax, ax=axes, t=t) axes.set_title('Vehicle trajectories') axes.set_xlabel('$x$', fontsize=15) axes.set_ylabel('$t$', fontsize=15)
def plot_car_trajectories(q_l,q_r,ax=None,t=None,xmax=None): states, speeds, reval, wave_types = riemann_traffic_exact(q_l,q_r) def reval_with_speed(xi): q = reval(xi) u = 1-q qu = np.vstack((q,u)) return qu # density of particles for trajectories: rho_left = q_l / 3. rho_right = q_r / 3. # compute trajectories: x_traj, t_traj, xmax = riemann_tools.compute_riemann_trajectories(states, speeds, reval_with_speed, wave_types, xmax=xmax,rho_left=rho_left, rho_right=rho_right) # plot trajectories along with waves in the x-t plane: riemann_tools.plot_riemann_trajectories(x_traj, t_traj, speeds, wave_types, xmax=xmax, ax=ax, t=t) ax.set_title('Vehicle trajectories')
def make_demo_plot_function(h_l=3., h_r=1., u_l=0., u_r=0, figsize=(10, 3), hlim=(0, 3.5), ulim=(-1, 1), force_waves=None, stripes=True): from matplotlib.mlab import find g = 1. q_l = primitive_to_conservative(h_l, u_l) q_r = primitive_to_conservative(h_r, u_r) x = np.linspace(-1., 1., 1000) states, speeds, reval, wave_types = \ exact_riemann_solution(q_l,q_r,g,force_waves=force_waves) # compute particle trajectories: def reval_rho_u(x): q = reval(x) rho = q[0] u = q[1] / q[0] rho_u = np.vstack((rho, u)) return rho_u if stripes: x_traj, t_traj, xmax = \ riemann_tools.compute_riemann_trajectories(states, speeds, reval_rho_u, wave_types, i_vel=1, xmax=2, rho_left=h_l/4., rho_right=h_r/4.) else: x_traj, t_traj, xmax = \ riemann_tools.compute_riemann_trajectories(states, speeds, reval_rho_u, wave_types, i_vel=1, xmax=2, num_left=3, num_right=3) num_vars = len(primitive_variables) def plot_shallow_water_demo(t=0.5, fig=0): if t == 0: q = np.zeros((2, len(x))) q[0, :] = q_l[0] * (x <= 0) + q_r[0] * (x > 0) q[1, :] = q_l[1] * (x <= 0) + q_r[1] * (x > 0) else: q = np.array(reval(x / t)) if t < 0.02: q[1] = np.where(x < 0, q_l[1], q_r[1]) primitive = conservative_to_primitive(q[0], q[1]) if fig == 0: fig = plt.figure(figsize=figsize) show_fig = True else: show_fig = False axes = [0] * num_vars for i in range(num_vars): axes[i] = fig.add_subplot(1, num_vars, i + 1) q = primitive[i] plt.plot(x, q, '-k', linewidth=3) plt.title(primitive_variables[i]) if t != 0: plt.suptitle('Solution at time $t=' + str(t) + '$', fontsize=12) else: plt.suptitle('Initial data', fontsize=12) axes[i].set_xlim(-1, 1) if i == 0 and force_waves != 'raref': # plot stripes only on depth plot # (and suppress if nonphysical solution plotted) n = find(t > t_traj) if len(n) == 0: n = 0 else: n = min(n.max(), len(t_traj) - 1) for j in range(1, x_traj.shape[1] - 1): j1 = find(x_traj[n, j] > x) if len(j1) == 0: j1 = 0 else: j1 = min(j1.max(), len(x) - 1) j2 = find(x_traj[n, j + 1] > x) if len(j2) == 0: j2 = 0 else: j2 = min(j2.max(), len(x) - 1) # set advected color for density plot: if x_traj[0, j] < 0: # shades of red for fluid starting from x<0 if np.mod(j, 2) == 0: c = 'lightblue' alpha = 1.0 else: c = 'dodgerblue' alpha = 1.0 else: # shades of blue for fluid starting from x<0 if np.mod(j, 2) == 0: c = 'cornflowerblue' alpha = 1.0 else: c = 'blue' alpha = 1.0 plt.fill_between(x[j1:j2], q[j1:j2], 0, color=c, alpha=alpha) axes[0].set_ylim(hlim) axes[1].set_ylim(ulim) if show_fig: plt.show() return plot_shallow_water_demo
def plot_with_stripes(rho_l=3., u_l=0., p_l=3., rho_r=1., u_r=0., p_r=1., gamma=1.4, t=0.4): import matplotlib.pyplot as plt import numpy as np from exact_solvers import euler from utils import riemann_tools q_l = euler.primitive_to_conservative(rho_l, u_l, p_l) q_r = euler.primitive_to_conservative(rho_r, u_r, p_r) x = np.linspace(-1., 1., 1000) states, speeds, reval, wave_types = euler.exact_riemann_solution( q_l, q_r, gamma=gamma) if t == 0: q = np.zeros((3, len(x))) q[0, :] = q_l[0] * (x <= 0) + q_r[0] * (x > 0) q[1, :] = q_l[1] * (x <= 0) + q_r[1] * (x > 0) q[1, :] = q_l[2] * (x <= 0) + q_r[2] * (x > 0) else: q = reval(x / t) primitive = euler.conservative_to_primitive(q[0], q[1], q[2]) # compute particle trajectories: def reval_rho_u(x): eps = 1.e-16 q = reval(x) rho = q[0] u = q[1] / (q[0] + eps) rho_u = np.vstack((rho, u)) return rho_u # Specify density of trajectories to left and right: num_left = 10 num_right = 10 rho_left = q_l[0] / 10. rho_right = q_r[0] / 10. x_traj, t_traj, xmax = riemann_tools.compute_riemann_trajectories( states, speeds, reval_rho_u, wave_types, i_vel=1, xmax=1, rho_left=rho_left, rho_right=rho_right) fig = plt.figure(figsize=(9, 8)) names = ['Density', 'Velocity', 'Pressure'] axes = [0] * 3 for i in range(3): axes[i] = fig.add_subplot(3, 1, i + 1) q = primitive[i] plt.plot(x, q, '-k', linewidth=3) plt.title(names[i]) qmax = max(q) qmin = min(q) qdiff = qmax - qmin if qdiff == 0: qdiff = qmin * 0.5 axes[i].set_ylim((qmin - 0.1 * qdiff, qmax + 0.1 * qdiff)) axes[i].set_xlim(-xmax, xmax) if i == 0: # plot stripes only on density plot n = np.array([j for j, v in enumerate(t > t_traj) if v]) if len(n) == 0: n = 0 else: n = min(n.max(), len(t_traj) - 1) for i in range(1, x_traj.shape[1] - 1): j1 = np.array([j for j, v in enumerate(x_traj[n, i] > x) if v]) if len(j1) == 0: j1 = 0 else: j1 = min(j1.max(), len(x) - 1) j2 = np.array( [j for j, v in enumerate(x_traj[n, i + 1] > x) if v]) if len(j2) == 0: j2 = 0 else: j2 = min(j2.max(), len(x) - 1) # set advected color for density plot: if x_traj[0, i] < 0: # shades of red for fluid starting from x<0 if np.mod(i, 2) == 0: c = [1, 0, 0] else: c = [1, 0.8, 0.8] else: # shades of blue for fluid starting from x<0 if np.mod(i, 2) == 0: c = [0, 0, 1] else: c = [0.8, 0.8, 1] plt.fill_between(x[j1:j2], q[j1:j2], 0, color=c) plt.tight_layout() plt.show()
def make_demo_plot_function(h_l=3., h_r=1., u_l=0., u_r=0, figsize=(10,3), hlim=(0,3.5), ulim=(-1,1), force_waves=None, stripes=True): from matplotlib.mlab import find g = 1. q_l = primitive_to_conservative(h_l,u_l) q_r = primitive_to_conservative(h_r,u_r) x = np.linspace(-1.,1.,1000) states, speeds, reval, wave_types = \ exact_riemann_solution(q_l,q_r,g,force_waves=force_waves) # compute particle trajectories: def reval_rho_u(x): q = reval(x) rho = q[0] u = q[1]/q[0] rho_u = np.vstack((rho,u)) return rho_u if stripes: x_traj, t_traj, xmax = \ riemann_tools.compute_riemann_trajectories(states, speeds, reval_rho_u, wave_types, i_vel=1, xmax=2, rho_left=h_l/4., rho_right=h_r/4.) else: x_traj, t_traj, xmax = \ riemann_tools.compute_riemann_trajectories(states, speeds, reval_rho_u, wave_types, i_vel=1, xmax=2, num_left=3, num_right=3) num_vars = len(primitive_variables) def plot_shallow_water_demo(t=0.5, fig=0): if t == 0: q = np.zeros((2,len(x))) q[0,:] = q_l[0]*(x<=0) + q_r[0]*(x>0) q[1,:] = q_l[1]*(x<=0) + q_r[1]*(x>0) else: q = np.array(reval(x/t)) if t<0.02: q[1] = np.where(x<0, q_l[1], q_r[1]) primitive = conservative_to_primitive(q[0],q[1]) if fig == 0: fig = plt.figure(figsize=figsize) show_fig = True else: show_fig = False axes = [0]*num_vars for i in range(num_vars): axes[i] = fig.add_subplot(1,num_vars,i+1) q = primitive[i] plt.plot(x,q,'-k',linewidth=3) plt.title(primitive_variables[i]) if t != 0: plt.suptitle('Solution at time $t='+str(t)+'$',fontsize=12) else: plt.suptitle('Initial data',fontsize=12) axes[i].set_xlim(-1,1) if i==0 and force_waves != 'raref': # plot stripes only on depth plot # (and suppress if nonphysical solution plotted) n = find(t > t_traj) if len(n)==0: n = 0 else: n = min(n.max(), len(t_traj)-1) for j in range(1, x_traj.shape[1]-1): j1 = find(x_traj[n,j] > x) if len(j1)==0: j1 = 0 else: j1 = min(j1.max(), len(x)-1) j2 = find(x_traj[n,j+1] > x) if len(j2)==0: j2 = 0 else: j2 = min(j2.max(), len(x)-1) # set advected color for density plot: if x_traj[0,j]<0: # shades of red for fluid starting from x<0 if np.mod(j,2)==0: c = 'lightblue' alpha = 1.0 else: c = 'dodgerblue' alpha = 1.0 else: # shades of blue for fluid starting from x<0 if np.mod(j,2)==0: c = 'cornflowerblue' alpha = 1.0 else: c = 'blue' alpha = 1.0 plt.fill_between(x[j1:j2],q[j1:j2],0,color=c,alpha=alpha) axes[0].set_ylim(hlim) axes[1].set_ylim(ulim) if show_fig: plt.show() return plot_shallow_water_demo
def make_demo_plot_function(h_l=3., h_r=1., u_l=0., u_r=0, figsize=(10, 3), hlim=(0, 3.5), ulim=(-1, 1), force_waves=None): from matplotlib.mlab import find import matplotlib.pyplot as plt from exact_solvers import shallow_water from utils import riemann_tools #plt.style.use('seaborn-talk') g = 1. q_l = shallow_water.primitive_to_conservative(h_l, u_l) q_r = shallow_water.primitive_to_conservative(h_r, u_r) x = np.linspace(-1., 1., 1000) states, speeds, reval, wave_types = \ shallow_water.exact_riemann_solution(q_l,q_r,g,force_waves=force_waves) # compute particle trajectories: def reval_rho_u(x): q = reval(x) rho = q[0] u = q[1] / q[0] rho_u = np.vstack((rho, u)) return rho_u x_traj, t_traj, xmax = \ riemann_tools.compute_riemann_trajectories(states, speeds, reval_rho_u, wave_types, i_vel=1, xmax=2, rho_left=h_l/4., rho_right=h_r/4.) num_vars = len(primitive_variables) def plot_shallow_water_demo(t=0.5, fig=0): if t == 0: q = np.zeros((2, len(x))) q[0, :] = q_l[0] * (x <= 0) + q_r[0] * (x > 0) q[1, :] = q_l[1] * (x <= 0) + q_r[1] * (x > 0) else: q = np.array(reval(x / t)) if t < 0.02: q[1] = np.where(x < 0, q_l[1], q_r[1]) primitive = shallow_water.conservative_to_primitive(q[0], q[1]) if fig == 0: fig = plt.figure(figsize=figsize) show_fig = True else: show_fig = False axes = [0] * num_vars for i in range(num_vars): axes[i] = fig.add_subplot(1, num_vars, i + 1) q = primitive[i] plt.plot(x, q, '-k', linewidth=3) plt.title(primitive_variables[i]) axes[i].set_xlim(-1, 1) if i == 0: # plot stripes only on depth plot n = find(t > t_traj) if len(n) == 0: n = 0 else: n = min(n.max(), len(t_traj) - 1) for i in range(1, x_traj.shape[1] - 1): j1 = find(x_traj[n, i] > x) if len(j1) == 0: j1 = 0 else: j1 = min(j1.max(), len(x) - 1) j2 = find(x_traj[n, i + 1] > x) if len(j2) == 0: j2 = 0 else: j2 = min(j2.max(), len(x) - 1) # set advected color for density plot: if x_traj[0, i] < 0: # shades of red for fluid starting from x<0 if np.mod(i, 2) == 0: c = [1, 0, 0] else: c = [1, 0.8, 0.8] else: # shades of blue for fluid starting from x<0 if np.mod(i, 2) == 0: c = [0, 0, 1] else: c = [0.8, 0.8, 1] plt.fill_between(x[j1:j2], q[j1:j2], 0, color=c) axes[0].set_ylim(hlim) axes[1].set_ylim(ulim) if show_fig: plt.show() return plot_shallow_water_demo