def _plot_velocity(self): from pysph.tools.interpolator import Interpolator from pysph.solver.utils import load # Find the u profile for comparison. y = np.linspace(0.0, H, 100) x = np.ones_like(y) * L / 2 fname = self.output_files[-1] data = load(fname) dm = self.create_domain() interp = Interpolator(list(data['arrays'].values()), x=x, y=y, domain_manager=dm) ui_lby2 = interp.interpolate('u') x = np.ones_like(y) * L interp.set_interpolation_points(x=x, y=y) ui_l = interp.interpolate('u') import matplotlib matplotlib.use('Agg') from matplotlib import pyplot as plt y /= H y -= 0.5 f = plt.figure() plt.plot(y, ui_lby2, 'k-', label='x=L/2') plt.plot(y, ui_l, 'k-', label='x=L') plt.xlabel('y/H') plt.ylabel('u') plt.legend() fig = os.path.join(self.output_dir, 'u_profile.png') plt.savefig(fig, dpi=300) plt.close() # Plot the contours of vmag. xx, yy = np.mgrid[0:L:100j, 0:H:100j] interp.set_interpolation_points(x=xx, y=yy) u = interp.interpolate('u') v = interp.interpolate('v') xx /= L yy /= H vmag = np.sqrt(u * u + v * v) f = plt.figure() plt.contourf(xx, yy, vmag) plt.xlabel('x/L') plt.ylabel('y/H') plt.colorbar() fig = os.path.join(self.output_dir, 'vmag_contour.png') plt.savefig(fig, dpi=300) plt.close() return y, ui_lby2, ui_l, xx, yy, vmag
def test_should_correctly_update_domain(self): # Given pa = self._make_2d_grid() ip = Interpolator([pa], num_points=1000) p = ip.interpolate('p') # When. ip.set_domain((0.0, 1.0, 0.0, 1.0, 0.0, 0.0), (11, 11, 1)) p = ip.interpolate('p') # Then. expect = np.ones_like(p)*2.0 self.assertTrue(np.allclose(p, expect))
def test_should_correctly_update_domain(self): # Given pa = self._make_2d_grid() ip = Interpolator([pa], num_points=1000) p = ip.interpolate('p') # When. ip.set_domain((0.0, 1.0, 0.0, 1.0, 0.0, 0.0), (11, 11, 1)) p = ip.interpolate('p') # Then. expect = np.ones_like(p) * 2.0 self.assertTrue(np.allclose(p, expect))
def test_should_work_with_changed_data(self): # Given pa = self._make_2d_grid() ip = Interpolator([pa], num_points=1000) p = ip.interpolate('p') # When. pa.p *= 2.0 p = ip.interpolate('p') # Then. expect = np.ones_like(p) * 4.0 self.assertTrue(np.allclose(p, expect))
def test_should_work_with_changed_data(self): # Given pa = self._make_2d_grid() ip = Interpolator([pa], num_points=1000) p = ip.interpolate('p') # When. pa.p *= 2.0 p = ip.interpolate('p') # Then. expect = np.ones_like(p)*4.0 self.assertTrue(np.allclose(p, expect))
def test_should_correctly_update_domain(self): # Given pa = self._make_2d_grid() ip = Interpolator([pa], num_points=1000, domain_manager=self._domain) p = ip.interpolate('p') # When. ip.set_domain((0.1, 1.0, 0.1, 1.0, 0.0, 0.0), (11, 11, 1)) p = ip.interpolate('p') # Then. expect = np.sin(ip.x * np.pi) print(p - expect) np.testing.assert_allclose(p, expect, atol=5e-3)
def test_should_work_with_changed_data(self): # Given pa = self._make_2d_grid() ip = Interpolator([pa], num_points=1000, domain_manager=self._domain) p = ip.interpolate('p') # When. pa.p *= 2.0 ip.update() p = ip.interpolate('p') # Then. expect = np.sin(ip.x * np.pi) * 2.0 np.testing.assert_allclose(p, expect, rtol=5e-2)
def test_should_be_able_to_update_particle_arrays(self): # Given pa = self._make_2d_grid() pa_new = self._make_2d_grid() pa_new.p[:] = np.cos(pa_new.x * np.pi) ip = Interpolator([pa], num_points=1000, domain_manager=self._domain) p = ip.interpolate('p') # When. ip.update_particle_arrays([pa_new]) p = ip.interpolate('p') # Then. expect = np.cos(ip.x * np.pi) np.testing.assert_allclose(p, expect, rtol=5e-3)
def test_should_work_without_shepard(self): # Given pa = self._make_2d_grid() dx = 0.2 pa.rho[:] = pa.m / (dx * dx) x0 = 1 + dx / 2 domain = DomainManager(xmin=-x0, xmax=x0, ymin=-x0, ymax=x0, periodic_in_x=True, periodic_in_y=True) # When. ip = Interpolator([pa], num_points=1000, domain_manager=domain, use_shepard=False) p = ip.interpolate('p') u = ip.interpolate('u') # Then. expect = np.ones_like(p) * 2.0 np.testing.assert_allclose(p, expect, rtol=1e-3) expect = np.ones_like(u) * 0.1 np.testing.assert_allclose(u, expect, rtol=1e-3)
def test_should_be_able_to_update_particle_arrays(self): # Given pa = self._make_2d_grid() pa_new = self._make_2d_grid() pa_new.p[:] = 10.0 ip = Interpolator([pa], num_points=1000) p = ip.interpolate('p') # When. ip.update_particle_arrays([pa_new]) p = ip.interpolate('p') # Then. expect = np.ones_like(p) * 10.0 self.assertTrue(np.allclose(p, expect))
def test_should_be_able_to_update_particle_arrays(self): # Given pa = self._make_2d_grid() pa_new = self._make_2d_grid() pa_new.p[:] = 10.0 ip = Interpolator([pa], num_points=1000) p = ip.interpolate('p') # When. ip.update_particle_arrays([pa_new]) p = ip.interpolate('p') # Then. expect = np.ones_like(p)*10.0 self.assertTrue(np.allclose(p, expect))
def post_process(self, info_fname): self.read_info(info_fname) if len(self.output_files) == 0: return from pysph.solver.utils import iter_output import matplotlib.pyplot as plt from pysph.examples import db_exp_data as dbd from pysph.tools.interpolator import Interpolator import os H = self.geom.fluid_column_height factor_y = 1 / (ro * 9.81 * H) factor_x = np.sqrt(9.81 / H) t1, t3, data_p1, data_p3 = dbd.get_kleefsman_data() files = self.output_files t = [] p0 = [] x_probe = self.geom.obstacle_center_x - self.geom.obstacle_length * 0.5 p_x = np.repeat(x_probe, 2) p_y = np.repeat(0, 2) p_z = np.array([0.021, 0.101]) for sd, arrays1, arrays2, arrays3 in iter_output( files, "fluid", "obstacle", "boundary"): t.append(sd["t"] * factor_x) interp = Interpolator([arrays1, arrays2, arrays3], x=p_x, y=p_y, z=p_z, method="shepard") p0.append(interp.interpolate('p') * factor_y) fname = os.path.join(self.output_dir, 'results.npz') t, p0 = list(map(np.asarray, (t, p0))) np.savez(fname, t=t, p0=p0) p1 = p0[:, 0] p3 = p0[:, 1] fig1 = plt.figure() plt.plot(t, p1, label="p1 computed", figure=fig1) plt.plot(t1, data_p1, label="Kleefsman et al.", figure=fig1) plt.legend() plt.ylabel(r"$\frac{P}{\rho gH}$") plt.xlabel(r"$t \sqrt{\frac{g}{H}} $") plt.title("P1") plt.savefig(os.path.join(self.output_dir, 'p1_vs_t.png')) fig2 = plt.figure() plt.plot(t, p3, label="p3 computed", figure=fig2) plt.plot(t3, data_p3, label="Kleefsman et al.", figure=fig2) plt.legend() plt.ylabel(r"$\frac{P}{\rho gH}$") plt.xlabel(r"$t \sqrt{\frac{g}{H}} $") plt.title("P3") plt.savefig(os.path.join(self.output_dir, 'p3_vs_t.png'))
class SimpleRemesher(Tool): """A simple tool to periodically remesh a given array of particles onto an initial set of points. """ def __init__(self, app, array_name, props, freq=100, xi=None, yi=None, zi=None, kernel=None): """Constructor. Parameters ---------- app : pysph.solver.application.Application The application instance. array_name: str Name of the particle array that needs to be remeshed. props : list(str) List of properties to interpolate. freq : int Frequency of remeshing operation. xi, yi, zi : ndarray Positions to remesh the properties onto. If not specified they are taken from the particle arrays at the time of construction. kernel: any kernel from pysph.base.kernels """ from pysph.solver.utils import get_array_by_name self.app = app self.particles = app.particles self.array = get_array_by_name(self.particles, array_name) self.props = props if xi is None: xi = self.array.x if yi is None: yi = self.array.y if zi is None: zi = self.array.z self.xi, self.yi, self.zi = xi.copy(), yi.copy(), zi.copy() self.freq = freq from pysph.tools.interpolator import Interpolator if kernel is None: kernel = app.solver.kernel self.interp = Interpolator( self.particles, x=self.xi, y=self.yi, z=self.zi, kernel=kernel, domain_manager=app.create_domain() ) def post_step(self, solver): if solver.count % self.freq == 0 and solver.count > 0: self.interp.nnps.update() data = dict(x=self.xi, y=self.yi, z=self.zi) for prop in self.props: data[prop] = self.interp.interpolate(prop) self.array.set(**data)
def post_process(self, info_fname): self.read_info(info_fname) if len(self.output_files) == 0: return from pysph.solver.utils import iter_output import matplotlib.pyplot as plt from pysph.examples import db_exp_data as dbd from pysph.tools.interpolator import Interpolator import os exp_vt, exp_v, exp_ft, exp_f = dbd.get_yeh_petroff_data() files = self.output_files t = [] u = [] # Velocity probe location vp_x = 0.814 vp_y = 0.0 vp_z = 0.026 for sd, arrays in iter_output(files, "fluid"): t.append(sd["t"]) interp = Interpolator([arrays], x=vp_x, y=vp_y, z=vp_z, method="shepard") u.append(interp.interpolate('u')) u = np.array(u) fname = os.path.join(self.output_dir, 'results.npz') t, u = list(map(np.asarray, (t, u))) np.savez(fname, t=t, u=u) t = np.array(t) - 0.238 figure_1 = plt.figure() plt.plot(t, u, label="Computed", figure=figure_1) plt.scatter(exp_vt, exp_v, label="Experiment, Yeh and Petroff", marker="^", figure=figure_1, color=(0, 0, 0)) plt.legend() plt.ylabel("Horizontal Velocity (m/s)") plt.xlabel("Time (s)") left, right = plt.xlim() plt.xlim(left, 1.4) plt.savefig(os.path.join(self.output_dir, 'v_vs_t.png')) plt.show()
def post_process(self, info_fname): self.read_info(info_fname) if len(self.output_files) == 0: return from pysph.tools.interpolator import Interpolator from pysph.solver.utils import iter_output files = self.output_files y = np.linspace(0, 0.9, 20) x = np.ones_like(y) interp = None t, p, p_ex = [], [], [] for sd, arrays in iter_output(files): fluid, solid = arrays['fluid'], arrays['solid'] if interp is None: interp = Interpolator([fluid, solid], x=x, y=y) else: interp.update_particle_arrays([fluid, solid]) t.append(sd['t']) p.append(interp.interpolate('p')) g = 1.0 * damping_factor(t[-1], tdamp) p_ex.append(abs(rho0 * H * g)) t, p, p_ex = list(map(np.asarray, (t, p, p_ex))) res = os.path.join(self.output_dir, 'results.npz') np.savez(res, t=t, p=p, p_ex=p_ex, y=y) import matplotlib matplotlib.use('Agg') pmax = abs(0.9 * rho0 * gy) from matplotlib import pyplot as plt plt.plot(t, p[:, 0] / pmax, 'o-') plt.xlabel(r'$t$') plt.ylabel(r'$p$') fig = os.path.join(self.output_dir, 'p_bottom.png') plt.savefig(fig, dpi=300) plt.clf() output_at = np.arange(0.25, 2.1, 0.25) count = 0 for i in range(len(t)): if abs(t[i] - output_at[count]) < 1e-8: plt.plot(y, p[i] / pmax, 'o', label='t=%.2f' % t[i]) plt.plot(y, p_ex[i] * (H - y) / (H * pmax), 'k-') count += 1 plt.xlabel('$y$') plt.ylabel('$p$') plt.legend() fig = os.path.join(self.output_dir, 'p_vs_y.png') plt.savefig(fig, dpi=300)
def test_should_work_with_method_sph(self): # Given pa = self._make_2d_grid() # When. ip = Interpolator([pa], domain_manager=self._domain, method='sph') x, y = np.random.random((2, 5, 5)) ip.set_interpolation_points(x=x, y=y) p = ip.interpolate('p') # Then. expect = np.sin(ip.x * np.pi) np.testing.assert_allclose(p, expect, rtol=5e-3)
def test_gradient_calculation_2d(self): # Given pa = self._make_2d_grid() # When. ip = Interpolator([pa], domain_manager=self._domain, method='order1') x, y = np.random.random((2, 5, 5)) ip.set_interpolation_points(x=x, y=y) p = ip.interpolate('p', 1) # Then. expect = np.pi * np.cos(ip.x * np.pi) np.testing.assert_allclose(p, expect, rtol=1e-2)
def test_should_work_with_explicit_points_without_z(self): # Given pa = self._make_2d_grid() x, y = np.random.random((2, 5, 5)) # When. ip = Interpolator([pa], x=x, y=y) p = ip.interpolate('p') # Then. self.assertEqual(p.shape, x.shape) expect = np.ones_like(x)*2.0 self.assertTrue(np.allclose(p, expect))
def test_should_work_when_arrays_have_different_props(self): # Given pa1 = self._make_2d_grid() pa1.add_property('junk', default=2.0) pa2 = self._make_2d_grid('solid') # When. ip = Interpolator([pa1, pa2], num_points=1000) junk = ip.interpolate('junk') # Then. expect = np.ones_like(junk)*1.0 self.assertTrue(np.allclose(junk, expect))
def test_should_work_with_explicit_points_without_z(self): # Given pa = self._make_2d_grid() x, y = np.random.random((2, 5, 5)) # When. ip = Interpolator([pa], x=x, y=y, domain_manager=self._domain) p = ip.interpolate('p') # Then. self.assertEqual(p.shape, x.shape) expect = np.sin(ip.x * np.pi) np.testing.assert_allclose(p, expect, rtol=5e-3)
def test_should_work_with_explicit_points_without_z(self): # Given pa = self._make_2d_grid() x, y = np.random.random((2, 5, 5)) # When. ip = Interpolator([pa], x=x, y=y) p = ip.interpolate('p') # Then. self.assertEqual(p.shape, x.shape) expect = np.ones_like(x) * 2.0 self.assertTrue(np.allclose(p, expect))
def test_should_work_when_arrays_have_different_props(self): # Given pa1 = self._make_2d_grid() pa1.add_property('junk', default=2.0) pa2 = self._make_2d_grid('solid') # When. ip = Interpolator([pa1, pa2], num_points=1000) junk = ip.interpolate('junk') # Then. expect = np.ones_like(junk) * 1.0 self.assertTrue(np.allclose(junk, expect))
def test_that_set_interpolation_points_works(self): # Given pa = self._make_2d_grid() ip = Interpolator([pa], num_points=1000) # When. x, y = np.random.random((2, 5, 5)) ip.set_interpolation_points(x=x, y=y) p = ip.interpolate('p') # Then. self.assertEqual(p.shape, x.shape) expect = np.ones_like(x)*2.0 self.assertTrue(np.allclose(p, expect))
def test_should_work_on_2d_data(self): # Given pa = self._make_2d_grid() # When. ip = Interpolator([pa], num_points=1000, domain_manager=self._domain) p = ip.interpolate('p') u = ip.interpolate('u') # Then. expect = np.sin(ip.x * np.pi) np.testing.assert_allclose(p, expect, rtol=5e-3) expect = np.cos(ip.x * np.pi) np.testing.assert_allclose(u, expect, rtol=5e-3)
def test_should_work_on_2d_data(self): # Given pa = self._make_2d_grid() # When. ip = Interpolator([pa], num_points=1000) p = ip.interpolate('p') u = ip.interpolate('u') # Then. expect = np.ones_like(p) * 2.0 self.assertTrue(np.allclose(p, expect)) expect = np.ones_like(u) * 0.1 self.assertTrue(np.allclose(u, expect))
def test_that_set_interpolation_points_works(self): # Given pa = self._make_2d_grid() ip = Interpolator([pa], num_points=1000) # When. x, y = np.random.random((2, 5, 5)) ip.set_interpolation_points(x=x, y=y) p = ip.interpolate('p') # Then. self.assertEqual(p.shape, x.shape) expect = np.ones_like(x) * 2.0 self.assertTrue(np.allclose(p, expect))
def test_should_work_on_2d_data(self): # Given pa = self._make_2d_grid() # When. ip = Interpolator([pa], num_points=1000) p = ip.interpolate('p') u = ip.interpolate('u') # Then. expect = np.ones_like(p)*2.0 self.assertTrue(np.allclose(p, expect)) expect = np.ones_like(u)*0.1 self.assertTrue(np.allclose(u, expect))
def test_that_set_interpolation_points_works(self): # Given pa = self._make_2d_grid() ip = Interpolator([pa], num_points=1000, domain_manager=self._domain) # When. x, y = np.random.random((2, 5, 5)) ip.set_interpolation_points(x=x, y=y) p = ip.interpolate('p') # Then. self.assertEqual(p.shape, x.shape) expect = np.sin(ip.x * np.pi) np.testing.assert_allclose(p, expect, rtol=5e-3)
def test_should_work_with_multiple_arrays(self): # Given pa1 = self._make_2d_grid() pa2 = self._make_2d_grid('solid') pa2.p[:] = 4.0 pa2.u[:] = 0.2 # When. ip = Interpolator([pa1, pa2], num_points=1000) p = ip.interpolate('p') u = ip.interpolate('u') # Then. expect = np.ones_like(p) * 3.0 self.assertTrue(np.allclose(p, expect)) expect = np.ones_like(u) * 0.15 self.assertTrue(np.allclose(u, expect))
def test_should_work_with_multiple_arrays(self): # Given pa1 = self._make_2d_grid() pa2 = self._make_2d_grid('solid') pa2.p[:] = 4.0 pa2.u[:] = 0.2 # When. ip = Interpolator([pa1, pa2], num_points=1000) p = ip.interpolate('p') u = ip.interpolate('u') # Then. expect = np.ones_like(p)*3.0 self.assertTrue(np.allclose(p, expect)) expect = np.ones_like(u)*0.15 self.assertTrue(np.allclose(u, expect))
def test_should_work_with_ghost_particles(self): # Given pa = self._make_2d_grid() # Make half the particles ghosts. n = pa.get_number_of_particles() pa.tag[n/2:] = 1 pa.align_particles() # When. ip = Interpolator([pa], num_points=1000) p = ip.interpolate('p') u = ip.interpolate('u') # Then. expect = np.ones_like(p)*2.0 self.assertTrue(np.allclose(p, expect)) expect = np.ones_like(u)*0.1 self.assertTrue(np.allclose(u, expect))
def test_should_work_with_ghost_particles(self): # Given pa = self._make_2d_grid() # Make half the particles ghosts. n = pa.get_number_of_particles() pa.tag[int(n // 2):] = 1 pa.align_particles() # When. ip = Interpolator([pa], num_points=1000, domain_manager=self._domain) p = ip.interpolate('p') u = ip.interpolate('u') # Then. expect = np.sin(ip.x * np.pi) np.testing.assert_allclose(p, expect, rtol=5e-3) expect = np.cos(ip.x * np.pi) np.testing.assert_allclose(u, expect, rtol=5e-3)
def test_should_work_with_ghost_particles(self): # Given pa = self._make_2d_grid() # Make half the particles ghosts. n = pa.get_number_of_particles() pa.tag[int(n // 2):] = 1 pa.align_particles() # When. ip = Interpolator([pa], num_points=1000) p = ip.interpolate('p') u = ip.interpolate('u') # Then. expect = np.ones_like(p) * 2.0 self.assertTrue(np.allclose(p, expect)) expect = np.ones_like(u) * 0.1 self.assertTrue(np.allclose(u, expect))
def test_should_work_with_multiple_arrays(self): # Given pa1 = self._make_2d_grid() pa2 = self._make_2d_grid('solid') pa2.p[:] = 4.0 pa2.u[:] = 0.2 # When. ip = Interpolator([pa1, pa2], num_points=1000, domain_manager=self._domain) p = ip.interpolate('p') u = ip.interpolate('u') # Then. expect = (np.sin(ip.x * np.pi) + 4.0) * 0.5 np.testing.assert_allclose(p, expect, rtol=5e-2) expect = (np.cos(ip.x * np.pi) + 0.2) * 0.5 np.testing.assert_allclose(u, expect, rtol=5e-2)
def post_process(self, info_fname): self.read_info(info_fname) if len(self.output_files) == 0: return from pysph.solver.utils import iter_output import matplotlib.pyplot as plt from pysph.examples import db_exp_data as dbd from pysph.tools.interpolator import Interpolator H = 1.0 factor_y = 1 / (ro * g * H) factor_x = np.sqrt(g / H) data_t, data_p0 = dbd.get_buchner_data() files = self.output_files t = [] p0 = [] for sd, arrays1, arrays2 in iter_output(files, "fluid", "boundary"): t.append(sd["t"] * factor_x) interp = Interpolator([arrays1, arrays2], x=[container_width], y=[H * 0.2], method=self.interp_method) p0.append(interp.interpolate('p') * factor_y) plt.plot(t, p0, label="Computed") fname = os.path.join(self.output_dir, 'results.npz') t, p0 = list(map(np.asarray, (t, p0))) np.savez(fname, t=t, p0=p0) plt.scatter(data_t, data_p0, color=(0, 0, 0), label="Experiment (Buchner, 2002)") plt.legend() plt.ylabel(r"$\frac{P}{\rho gH}$") plt.xlabel(r"$t \sqrt{\frac{g}{H}}$") plt.savefig(os.path.join(self.output_dir, 'p_vs_t.png'))
def get_data(): error_u = {} error_v = {} for kernel in ['cs', 'g', 'qs', 'wq']: for nx in [50, 100, 200]: for hdx in [1.0, 1.5, 2.0]: sub_dir = kernel + '_' + str(hdx) + '_' + str(nx) files = [] for fil in os.listdir(cwd + '/' + sub_dir): if fil.endswith(".npz") and fil != 'results.npz': files.append(fil) f_num = [int(f.split('_')[1].split('.')[0]) for f in files] zipped = zip(f_num, files) last_file = sorted(zipped)[-1][1] data = load(sub_dir + '/' + last_file) _x = np.linspace(0, 1, nx) xx, yy = np.meshgrid(_x, _x) interp = Interpolator(list(data['arrays'].values()), x=xx, y=yy) ui = np.zeros_like(xx) vi = np.zeros_like(xx) interp.update_particle_arrays(list(data['arrays'].values())) _u = interp.interpolate('u') _v = interp.interpolate('v') _u.shape = nx, nx _v.shape = nx, nx ui += _u vi += _v particle = data['arrays'] fluid = particle['fluid'] u = fluid.u v = fluid.v ui = np.ravel(ui) vi = np.ravel(vi) error_u[sub_dir] = l2_error(ui, u) error_v[sub_dir] = l2_error(vi, v) return error_u, error_v
def post_process(self, info_fname): self.read_info(info_fname) if len(self.output_files) == 0: return from pysph.solver.utils import iter_output import matplotlib.pyplot as plt from pysph.examples import st_exp_data as st from pysph.tools.interpolator import Interpolator files = self.output_files t = [] p0 = [] xc0 = 0.0 for sd, arrays1, arrays2 in iter_output(files, "fluid", "boundary"): t.append(sd["t"]) xc = arrays2.x.mean() if sd["t"] == 0: xc0 = xc # Point 1: Bottom right corner of the tank # Point 2: Top right corner of the tank # Point 3: Bottom left corner of the tank if xc > xc0: z1 = arrays2.z.min() x2 = arrays2.x.max() x1 = arrays2.x[np.where(arrays2.z == z1)[0][0]] z2 = arrays2.x[np.where(arrays2.x == x2)[0][0]] angle = np.arctan((z2 - z1) / (x2 - x1)) x3 = arrays2.x.min() z3 = arrays2.z[np.where(arrays2.x == x3)[0][0]] if xc <= xc0: z2 = arrays2.z.max() x1 = arrays2.x.max() x2 = arrays2.x[np.where(arrays2.z == z2)[0][0]] z1 = arrays2.z[np.where(arrays2.x == x1)[0][0]] angle = np.arctan((z2 - z1) / (x2 - x1)) + pi z3 = arrays2.z.min() x3 = arrays2.x[np.where(arrays2.z == z3)[0][0]] if x3 - x1 == 0: vec = np.array([-1, 0]) else: vec = np.array([x3 - x1, z3 - z1]) vec = vec / np.linalg.norm(vec) dx = self.dx # Probes at 0.17 m above Point 1 and 0.45 m above Point 3 x_probe = [ x1 + 0.17 * cos(angle) + 3.25 * dx * vec[0], x3 + 0.45 * cos(angle) - 3.25 * dx * vec[0] ] z_probe = [ z1 + 0.17 * sin(angle) + 3.25 * dx * vec[1], z3 + 0.45 * sin(angle) - 3.25 * dx * vec[1] ] y_probe = [0, 0] interp = Interpolator([arrays1, arrays2], x=x_probe, y=y_probe, z=z_probe, method=self.interp_method) p0.append(interp.interpolate('p')) p0 = np.array(p0) p2 = p0.T[0] / 1000 p8 = p0.T[1] / 1000 p2_avg, p8_avg = p2, p8 fname = os.path.join(self.output_dir, 'results.npz') t, p0 = list(map(np.asarray, (t, p0))) np.savez(fname, t=t, p0=p0) exp_t2, exp_p2, exp_t8, exp_p8 = st.get_au_pitch_data() figure_1 = plt.figure() # Probe 2 plt.plot(t[:-10], p2_avg[:-10], label="Computed", figure=figure_1) plt.scatter(exp_t2, exp_p2, color=(0, 0, 0), label="Experiment (Akyildiz and Unal, 2006)", figure=figure_1) plt.title("P2 v/s t") plt.legend() plt.ylabel("Pressue [KPa]") plt.xlabel("Time [s]") plt.xlim(3, 10) plt.savefig(os.path.join(self.output_dir, 'p2_vs_t.png')) plt.show() figure_2 = plt.figure() # Probe 8 plt.plot(t, p8_avg, label="Computed", figure=figure_2) plt.scatter(exp_t8, exp_p8, color=(0, 0, 0), label="Experiment (Akyildiz and Unal, 2006)", figure=figure_2) plt.title("P8 v/s t") plt.legend() plt.ylabel("Pressue [KPa]") plt.xlabel("Time [s]") plt.xlim(3, 10) plt.savefig(os.path.join(self.output_dir, 'p8_vs_t.png')) plt.show()
def _plot_velocity(self): from pysph.tools.interpolator import Interpolator from pysph.solver.utils import load from pysph.examples.ghia_cavity_data import get_u_vs_y, get_v_vs_x # interpolated velocities _x = np.linspace(0, 1, 101) xx, yy = np.meshgrid(_x, _x) # take the last solution data fname = self.output_files[-1] data = load(fname) tf = data['solver_data']['t'] interp = Interpolator(list(data['arrays'].values()), x=xx, y=yy) ui = np.zeros_like(xx) vi = np.zeros_like(xx) # Average out the velocities over the last n_avg timesteps for fname in self.output_files[-self.n_avg:]: data = load(fname) tf = data['solver_data']['t'] interp.update_particle_arrays(list(data['arrays'].values())) _u = interp.interpolate('u') _v = interp.interpolate('v') _u.shape = 101, 101 _v.shape = 101, 101 ui += _u vi += _v ui /= self.n_avg vi /= self.n_avg # velocity magnitude self.vmag = vmag = np.sqrt(ui**2 + vi**2) import matplotlib.pyplot as plt f = plt.figure() plt.streamplot( xx, yy, ui, vi, density=(2, 2), #linewidth=5*vmag/vmag.max(), color=vmag) plt.xlim(0, 1) plt.ylim(0, 1) plt.colorbar() plt.xlabel('$x$') plt.ylabel('$y$') plt.title('Streamlines at %s seconds' % tf) fig = os.path.join(self.output_dir, 'streamplot.png') plt.savefig(fig, dpi=300) f = plt.figure() ui_c = ui[:, 50] vi_c = vi[50] s1 = plt.subplot(211) s1.plot(ui_c, _x, label='Computed') y, data = get_u_vs_y() if self.re in data: s1.plot(data[self.re], y, 'o', fillstyle='none', label='Ghia et al.') s1.set_xlabel(r'$v_x$') s1.set_ylabel(r'$y$') s1.legend() s2 = plt.subplot(212) s2.plot(_x, vi_c, label='Computed') x, data = get_v_vs_x() if self.re in data: s2.plot(x, data[self.re], 'o', fillstyle='none', label='Ghia et al.') s2.set_xlabel(r'$x$') s2.set_ylabel(r'$v_y$') s2.legend() fig = os.path.join(self.output_dir, 'centerline.png') plt.savefig(fig, dpi=300) return _x, ui, vi, ui_c, vi_c
def _plot_depth_and_vel(self): from matplotlib import pyplot as plt t_arr = [] u_arr = [] dw_arr = [] v_arr = [] # Properties of the fluid at this location are found at various times # Note: This location is wetted at all times by the fluid x_loc_to_interpolate = 0.0 y_loc_to_interpolate = 0.0 kernel = CubicSpline(dim=2) for fname in self.output_files: data = load(fname) fluid = data['arrays']['fluid'] t_arr.append(data['solver_data']['t']) interp = Interpolator([fluid], kernel=kernel) interp.set_interpolation_points(x_loc_to_interpolate, y_loc_to_interpolate) u_interp = interp.interpolate('u') v_interp = interp.interpolate('v') dw_interp = interp.interpolate('dw') u_arr.append(u_interp.item()) v_arr.append(v_interp.item()) dw_arr.append(dw_interp.item()) t = array(t_arr) x = zeros_like(t) y = zeros_like(t) fluid_rad = self.r zo = self.zo zeta = self.x_cen_fluid - 0.0 omega = self.omega def actual_fluid_surf_hei(x, y, t): fluid_surf_hei = zo + (2*zeta*(zo/fluid_rad) * ((x/fluid_rad)*cos(omega*t) - (y/fluid_rad)*sin(omega*t) - (zeta/(2.0*fluid_rad)))) return fluid_surf_hei def fluid_bottom_hei(x, y): return zo * ((x**2+y**2)/fluid_rad**2) def u_actual_func(t): return -zeta*omega*sin(omega*t) def v_actual_func(t): return -zeta*omega*cos(omega*t) dw_actual = actual_fluid_surf_hei(x, y, t) - fluid_bottom_hei(x, y) u_actual = u_actual_func(t) v_actual = v_actual_func(t) # Time period T = (2*pi) / omega if len(dw_arr) == 0 or len(u_arr) == 0 or len(v_arr) == 0: return dw_min = min(dw_arr) dw_max = max(dw_arr) u_min = min(u_arr) u_max = max(u_arr) v_min = min(v_arr) v_max = max(v_arr) b = fluid_bottom_hei(x, y) fname_res = os.path.join(self.output_dir, 'results.npz') savez( fname_res, t=t_arr, dw_num=dw_arr, dw_actual=dw_actual, u_num=u_arr, u_actual=u_actual, v_num=v_arr, v_actual=v_actual, x=x, y=y, b=b ) plt.clf() plt.plot(t_arr/T, dw_actual, 'r-', label='Analytical Solution') plt.plot(t_arr/T, dw_arr, 'bo', markersize=2, label='SWE-SPH') plt.legend() plt.ylim(0.8*dw_min, 1.5*dw_max) plt.xlim(0, 1) plt.xlabel('t/T') plt.ylabel('dw (m)') fig = os.path.join(self.output_dir, "depth") plt.savefig(fig, dpi=300) plt.clf() plt.plot(t_arr/T, u_actual, 'ro', markersize=2, label='Analytical Solution') plt.plot(t_arr/T, u_arr, 'bo', markersize=2, label='SWE-SPH') plt.legend() plt.ylim(1.5*u_min, 1.5*u_max) plt.xlim(0, 1) plt.xlabel('t/T') plt.ylabel('u (m/s)') fig = os.path.join(self.output_dir, "velocity1") plt.savefig(fig, dpi=300) plt.clf() plt.plot(t_arr/T, v_actual, 'ro', markersize=2, label='Analytical Solution') plt.plot(t_arr/T, v_arr, 'bo', markersize=2, label='SWE-SPH') plt.legend() plt.ylim(1.5*v_min, 1.5*v_max) plt.xlim(0, 1) plt.xlabel('t/T') plt.ylabel('v (m/s)') fig = os.path.join(self.output_dir, "velocity2") plt.savefig(fig, dpi=300)