def test_gradient_with_shepard(self): # Given pa = self._make_2d_grid() # When. ip = Interpolator([pa], domain_manager=self._domain, method='shepard') x, y = np.random.random((2, 5, 5)) ip.set_interpolation_points(x=x, y=y) # Then. self.assertRaises(RuntimeError, ip.interpolate, 'p', 1)
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_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_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_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_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_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 _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)
class LEDCube(Application): def initialize(self): self.interpolator = None def create_particles(self): nb = 2 nx = 1.0 / dx sl = slice(-nb * dx, 1 + nb * dx, (nx + 2 * nb) * 1j) x, y, z = np.mgrid[sl, sl, sl] x, y, z = (np.ravel(t) for t in (x, y, z)) inside = (x > 0) & (x < 1) & (y > 0) & (y < 1) & (z > 0) & (z < 1) outside = ~inside water = inside & (z < water_height) # the fluid. h0 = hdx * dx m = dx * dx * dx * rho one_f = np.ones_like(x[water]) fluid = get_particle_array_wcsph(name='fluid', x=x[water], y=y[water], z=z[water], rho=rho * one_f, h=h0 * one_f, m=m * one_f) one_s = np.ones_like(x[outside]) solid = get_particle_array_wcsph(name='solid', x=x[outside], y=y[outside], z=z[outside], rho=rho * one_s, h=h0 * one_s, m=m * one_s) led = get_particle_array_wcsph(name='led', x=x[inside], y=y[inside], z=z[inside]) return [fluid, solid, led] def create_scheme(self): s = WCSPHScheme(['fluid'], ['solid'], dim=3, rho0=1000, c0=c0, h0=hdx * dx, hdx=hdx, gz=-9.81, alpha=0.25, beta=0.0, gamma=7, hg_correction=True, tensile_correction=False) extra_steppers = dict(solid=OneStageRigidBodyStep(), led=OneStageRigidBodyStep()) s.configure_solver(extra_steppers=extra_steppers, tf=10.0, dt=dt) return s def create_equations(self): eqs = self.scheme.get_equations() eqs[0].equations.append(CubeMotion(dest='solid', sources=['solid'])) eqs[0].equations.append(CubeMotion(dest='led', sources=['led'])) return eqs def post_step(self, solver): if solver.count % 5 == 0: self._create_interpolator() rho = self.interpolator.interpolate('rho') print(rho) def _create_interpolator(self): led = self.particles[2] if self.interpolator is None: from pysph.tools.interpolator import Interpolator # This is as per the order returned in create_particles. self.interpolator = Interpolator(self.particles, x=led.x, y=led.y, z=led.z) else: self.interpolator.update_particle_arrays(self.particles) self.interpolator.set_interpolation_points(x=led.x, y=led.y, z=led.z)
def _plot_depth(self): from matplotlib import pyplot as plt x_loc_to_interpolate = [4.521, 4.521, 4.521] y_loc_to_interpolate = [1.196, 1.696, 2.196] # Vacondio simulation results for this case fname_vacondio_results = [ 'tsu_sensor1_vacondio.csv', 'tsu_sensor2_vacondio.csv', 'tsu_sensor3_vacondio.csv' ] output_compare_files_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'files_for_output_comparison') fname_exp_results = os.path.join(output_compare_files_dir, 'tsu_experimental.csv') # Experimental values of relative depth at 3 sensor locations # corresponding to the coordinates (4.521 m, 1.196 m), (4.521 m, 1.696 # m), and (4.521 m, 2.196 m) # Note: The unit of relative depth in the file is in cm t_exp, dw_rel_sens1_exp, dw_rel_sens2_exp, dw_rel_sens3_exp = loadtxt( fname_exp_results, delimiter=',', unpack=True) dw_relative_sensors_exp = [ dw_rel_sens1_exp, dw_rel_sens2_exp, dw_rel_sens3_exp ] kernel = CubicSpline(dim=2) for n, (x_loc, y_loc) in enumerate( zip(x_loc_to_interpolate, y_loc_to_interpolate)): t_arr = [] dw_arr = [] 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, y_loc) dw_interp = interp.interpolate('dw') dw_arr.append(dw_interp) dw_arr = array(dw_arr) t_arr = array(t_arr) plt.clf() if (self.dw0 == 0.13535 and self.le == 5.448 and self.w == 3.402 and self.n == 0.025 and self.Vb == 1.96e-4): fname_vacondio = os.path.join(output_compare_files_dir, fname_vacondio_results[n]) t_vac, dw_relative_vac = loadtxt(fname_vacondio, delimiter=',', unpack=True) # Converting the unit of relative depth from cm to m dw_relative_exp = dw_relative_sensors_exp[n] / 100.0 plt.plot(t_vac, dw_relative_vac, 'g.', label='Vacondio et. al') plt.plot(t_exp, dw_relative_exp, 'k-', label='Experimental data') fname_res = os.path.join(self.output_dir, 'results_sensor%d.npz' % (n + 1)) savez(fname_res, x_sensor=x_loc_to_interpolate[n], y_sensor=y_loc_to_interpolate[n], t_numerical=t_arr, dw_relative_numerical=dw_arr - dw_arr[0], t_vacondio=t_vac, dw_relative_vacondio=dw_relative_vac, t_experimental=t_exp, dw_relative_experimental=dw_relative_exp) plt.plot(t_arr, dw_arr - dw_arr[0], 'r.', label='SWESPH') plt.legend() plt.ylim(-0.015, 0.05) plt.xlim(0, max(t_arr)) plt.xlabel('t (s)') plt.ylabel('$dw-dw_0$') plt.title('Relative water depth at the location (%.3f m, %.3f m)' % (x_loc, y_loc)) fig = os.path.join(self.output_dir, "relative_depth_sensor" + str(n + 1)) plt.savefig(fig, dpi=300)