Пример #1
0
    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)
Пример #2
0
    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'))
Пример #3
0
    def __init__(self,
                 app,
                 array_name,
                 props,
                 freq=100,
                 xi=None,
                 yi=None,
                 zi=None,
                 kernel=None,
                 equations=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

        equations: list or None
            Equations to use for the interpolation, passed to the interpolator.

        """
        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(),
                                   equations=equations)
Пример #4
0
    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)
Пример #5
0
    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()
Пример #6
0
    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)
Пример #7
0
    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))
Пример #8
0
    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))
Пример #9
0
    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)
Пример #10
0
    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))
Пример #11
0
    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))
Пример #12
0
    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))
Пример #13
0
    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))
Пример #14
0
    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))
Пример #15
0
    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)
Пример #16
0
    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)
Пример #17
0
    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)
Пример #18
0
    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))
Пример #19
0
 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)
Пример #20
0
    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)
Пример #21
0
    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))
Пример #22
0
    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))
Пример #23
0
    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))
Пример #24
0
    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
Пример #25
0
    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))
Пример #26
0
    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))
Пример #27
0
    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))
Пример #28
0
    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))
Пример #29
0
    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))
Пример #30
0
    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)
Пример #31
0
 def _setup_interpolator(self):
     if self.interpolator is None:
         interpolator = Interpolator(self.particle_arrays,
                                     num_points=self.num_points)
         self.bounds = interpolator.bounds
         self.interpolator = interpolator
     else:
         if self._arrays_changed:
             self.interpolator.update_particle_arrays(self.particle_arrays)
             self._arrays_changed = False
Пример #32
0
    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)
Пример #33
0
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)
Пример #34
0
    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'))
Пример #35
0
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
Пример #36
0
    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))
Пример #37
0
    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)
Пример #38
0
    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)
Пример #39
0
    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)