Example #1
0
    def setUp(self):

        self.grid10 = libst.Grid(0, 10, 10)
        self.sol10 = libst.Solver(grid=self.grid10, nvar=1,
                                  time_increment=0.2)
        self.ce0 = self.sol10.celm(ielm=0)
        self.ce9 = self.sol10.celm(ielm=9)
Example #2
0
    def setUp(self):

        self.grid10 = libst.Grid(0, 10, 10)
        self.sol10 = libst.Solver(grid=self.grid10, nvar=1,
                                  time_increment=0.2)
        self.sol10.so0.ndarray.fill(-1)
        self.sol10.so1.ndarray.fill(-2)
Example #3
0
    def setUp(self):

        self.grid10 = libst.Grid(0, 10, 10)
        self.sol10 = libst.Solver(grid=self.grid10, nvar=1, time_increment=0.2)
        self.ce0 = self.sol10.celm(ielm=0)
        self.ce9 = self.sol10.celm(ielm=9)
        # Set trivial solution.
        self.sol10.so0.ndarray.fill(1)
        self.sol10.so1.ndarray.fill(0)
Example #4
0
    def setUp(self):

        xloc = np.array([0, 1, 3, 10])
        self.grid3 = libst.Grid(xloc=xloc)
        self.sol3 = libst.Solver(grid=self.grid3, nvar=1, time_increment=0.1)
        self.ce0 = self.sol3.celm(ielm=0)
        self.ce1 = self.sol3.celm(ielm=1)
        self.ce2 = self.sol3.celm(ielm=2)
        self.se0 = self.sol3.selm(ielm=0)
        self.se1 = self.sol3.selm(ielm=1)
        self.se2 = self.sol3.selm(ielm=2)
        self.se3 = self.sol3.selm(ielm=3)
Example #5
0
    def setUp(self):

        self.dt = 0.2
        self.grid10 = libst.Grid(0, 10, 10)
        self.sol10 = libst.Solver(grid=self.grid10, nvar=1,
                                  time_increment=self.dt)
        self.se0 = self.sol10.selm(0)
        self.se9 = self.sol10.selm(9)
        self.se10 = self.sol10.selm(10)
        # Set trivial solution.
        self.sol10.so0.ndarray.fill(1)
        self.sol10.so1.ndarray.fill(0)
Example #6
0
    def setUp(self):

        self.grid = libst.Grid(0, 10, 10)
        self.sol1 = libst.Solver(grid=self.grid, nvar=1, time_increment=0.2)
        self.sol2 = libst.Solver(grid=self.grid, nvar=1, time_increment=0.2)

        self.ce0s1 = self.sol1.celm(ielm=0)
        self.ce0s1p = self.sol1.celm(ielm=0)
        self.ce1s1 = self.sol1.celm(ielm=1)
        self.ce0s1o = self.sol1.celm(ielm=0, odd_plane=True)
        self.ce0s2 = self.sol2.celm(ielm=0)

        self.se0s1 = self.sol1.selm(ielm=0)
        self.se0s1p = self.sol1.selm(ielm=0)
        self.se1s1 = self.sol1.selm(ielm=1)
        self.se0s1o = self.sol1.selm(ielm=0, odd_plane=True)
        self.se0s2 = self.sol2.selm(ielm=0)
Example #7
0
    def _build_solver(resolution):

        # Build grid.
        xcrd = np.arange(resolution+1) / resolution
        xcrd *= 2 * np.pi
        grid = libst.Grid(xcrd)
        dx = (grid.xmax - grid.xmin) / grid.ncelm

        # Build solver.
        time_stop = 2*np.pi
        cfl_max = 1.0
        dt_max = dx * cfl_max
        nstep = int(np.ceil(time_stop / dt_max))
        dt = time_stop / nstep
        svr = LinearProxy(grid=grid, time_increment=dt, nvar=1)

        # Initialize.
        svr.set_so0(0, np.sin(xcrd))
        svr.set_so1(0, np.cos(xcrd))
        svr.setup_march()

        return nstep, xcrd, svr
Example #8
0
    def test_construction(self):

        with self.assertRaisesRegex(
            ValueError,
            "Grid::Grid\(xmin=0, xmax=10, ncelm=0\) invalid argument: "
            "ncelm smaller than 1",
        ):
            libst.Grid(0, 10, 0)

        with self.assertRaisesRegex(
            ValueError,
            "Grid::Grid\(xmin=10, xmax=10, ncelm=10\) invalid arguments: "
            "xmin >= xmax",
        ):
            libst.Grid(10, 10, 10)

        with self.assertRaisesRegex(
            ValueError,
            "Grid::Grid\(xmin=11, xmax=10, ncelm=10\) invalid arguments: "
            "xmin >= xmax",
        ):
            libst.Grid(11, 10, 10)

        # Simply test for passing.
        libst.Grid(xloc=np.arange(2) * 0.1)

        for s in [0, 1]:
            with self.assertRaisesRegex(
                ValueError,
                "Grid::init_from_array\(xloc\) invalid arguments: "
                "xloc.size\(\)=%d smaller than 2" % s
            ):
                libst.Grid(xloc=np.arange(s) * 0.1)

        with self.assertRaisesRegex(
            ValueError,
            "Grid::init_from_array\(xloc\) invalid arguments: "
            "xloc\[0\]=1 >= xloc\[1\]=0.9"
        ):
            libst.Grid(xloc=np.arange(10, -1, -1) * 0.1)
Example #9
0
def draw():

    xloc = np.array([-1, 0, 2, 3.5])
    grid = libst.Grid(xloc=xloc)
    sol = libst.Solver(grid=grid, nvar=1, time_increment=6/4)

    dx = (grid.xmax-grid.xmin)/grid.ncelm
    dt = dx
    hdt = dt / 2

    c = libst.PstCanvas(unit='2cm', padding=0.5)
    c.set(linewidth='1pt')
    c.set(linecolor='black')

    # CE grids.
    linewidth = '0.5pt'
    for se in sol.selms(odd_plane=False):
        c.line((se.x, 0), (se.x, dt), linewidth=linewidth)
    c.line((sol.selm(0).x, 0), (sol.selm(grid.ncelm).x, 0),
           linewidth=linewidth)
    c.line((sol.selm(0).x, hdt), (sol.selm(grid.ncelm).x, hdt),
           linewidth=linewidth)
    c.line((sol.selm(0).x, dt), (sol.selm(grid.ncelm).x, dt),
           linewidth=linewidth)

    # x-axis.
    sep = 0.05
    c.line((sol.selm(0).x, -hdt*1.2), (sol.selm(grid.ncelm).x, -hdt*1.2),
           linewidth=linewidth)
    c.uput(sep, 'l', (grid.xmin-sep,-hdt*1.2), r'$j$')
    c.uput(sep, 'r', (grid.xmax+sep,-hdt*1.2), r'$x$')
    for se in sol.selms(odd_plane=False):
        c.line((se.x, -hdt*1.2-sep), (se.x, -hdt*1.2+sep))
        c.uput(sep, 'd', (se.x,-hdt*1.2-sep), r'$%d$'%se.index)
    for se in sol.selms(odd_plane=True):
        c.line((se.x, -hdt*1.2-sep/2), (se.x, -hdt*1.2+sep/2))
        c.uput(sep, 'd', (se.x,-hdt*1.2-sep), r'$\frac{%d}{2}$'%(se.index*2+1))

    # t-axis.
    x = grid.xmin - dx*0.6
    c.line((x, 0), (x, dt), linewidth=linewidth)
    c.uput(sep, 'd', (x, 0-sep), r'$n$')
    c.uput(sep, 'u', (x, dt+sep), r'$t$')
    c.line((x-sep, 0), (x+sep, 0))
    c.uput(sep, 'l', (x-sep, 0), r'$0$')
    c.line((x-sep/2, hdt), (x+sep/2, hdt))
    c.uput(sep, 'l', (x-sep, hdt), r'$\frac{1}{2}$')
    c.line((x-sep, dt), (x+sep, dt))
    c.uput(sep, 'l', (x-sep, dt), r'$1$')

    # SE and solution propagation.
    sep = 0.05
    sepx = dx*0.1
    sept = dt*0.1
    for se in sol.selms(odd_plane=False):
        c.selm(se, 0,
               sep=sep, linestyle='dotted', dotsep='1pt', linecolor='red')
    for se in sol.selms(odd_plane=True):
        c.selm(se, hdt,
               sep=sep, linestyle='dotted', dotsep='1pt', linecolor='blue')

    return c
Example #10
0
def draw():

    xloc = np.array([-1, 0, 2, 3.5])
    grid = libst.Grid(xloc=xloc)
    sol = libst.Solver(grid=grid, nvar=1, time_increment=1)

    dt = sol.dt
    hdt = dt / 2
    qdt = hdt / 2
    odt = qdt / 2

    c = libst.PstCanvas(unit='5cm', padding=[0.75, 0.25, 0.7, 0.25])
    c.set(linewidth='1pt')
    c.set(linecolor='black')

    ce = sol.celm(ielm=1, odd_plane=False)
    linewidth = '0.5pt'

    # SEs.
    sexn = ce.selm_xn
    c.line((sexn.x, 0), (sexn.x, hdt), linewidth=linewidth, linecolor='red')
    c.line((sexn.x, 0), (sexn.xpos, 0), linewidth=linewidth, linecolor='red')

    c.dots((sexn.x, 0), dotstyle='o')
    c.uput(0.05, 'dl', (sexn.x, 0), r'$x_{j-\frac{1}{2}}$')
    c.dots((sexn.xctr, 0), dotstyle='triangle')
    c.uput(0.05, 'u', (sexn.xctr, 0), r'$x^s_{j-\frac{1}{2}}$')

    sexp = ce.selm_xp
    c.line((sexp.x, 0), (sexp.x, hdt), linewidth=linewidth, linecolor='blue')
    c.line((sexp.xneg, 0), (sexp.x, 0), linewidth=linewidth, linecolor='blue')

    c.dots((sexp.x, 0), dotstyle='o')
    c.uput(0.05, 'dr', (sexp.x, 0), r'$x_{j+\frac{1}{2}}$')
    c.dots((sexp.xctr, 0), dotstyle='triangle')
    c.uput(0.05, 'u', (sexp.xctr, 0), r'$x^s_{j+\frac{1}{2}}$')

    c.dots((sexn.xpos, 0), dotstyle='square')
    assert sexn.xpos == sexp.xneg
    c.uput(0.05, 'u', (sexn.xpos, 0),
           r'$x^+_{j-\frac{1}{2}} = x^-_{j+\frac{1}{2}}$')

    setp = ce.selm_tp
    c.line((setp.xneg, hdt), (setp.x, hdt),
           linewidth=linewidth,
           linecolor='orange')
    c.line((setp.x, hdt), (setp.xpos, hdt),
           linewidth=linewidth,
           linecolor='orange')

    c.dots((setp.x, hdt), dotstyle='o')
    c.dots((setp.xctr, setp.hdt), dotstyle='triangle')
    c.uput(0.05, 'd', (setp.x, hdt), r'$x_j=x^s_j$')

    # Fluxes.
    vlen = hdt / 6
    sep = 0.015
    # \Delta x^+_{j-\frac{1}{2}}
    c.line(((sexn.x + sexn.xpos) / 2, 0), ((sexn.x + sexn.xpos) / 2, -vlen),
           arrows='->',
           linecolor='red')
    c.uput(
        0, 'd', ((sexn.x + sexn.xpos) / 2, -vlen),
        r'{\color{red}$(\mathbf{h}^*)^n_{j-\frac{1}{2},+}'
        r'\cdot(0, -\Delta x^+_{j-\frac{1}{2}})$}')
    # \Delta x^-_{j+\frac{1}{2}}
    c.line(((sexp.xneg + sexp.x) / 2, 0), ((sexp.xneg + sexp.x) / 2, -vlen),
           arrows='->',
           linecolor='blue')
    c.uput(
        0, 'd', ((sexp.xneg + sexp.x) / 2, -vlen),
        r'{\color{blue}$(\mathbf{h}^*)^n_{j+\frac{1}{2},-}'
        r'\cdot(0, -\Delta x^-_{j+\frac{1}{2}})$}')
    # \Delta x_j
    c.line((setp.xctr, hdt + sep), (setp.xctr, hdt + 2 * vlen),
           arrows='->',
           linecolor='orange')
    c.uput(
        0, 'u', (setp.xctr, hdt + 2 * vlen),
        r'{\color{orange}$\mathbf{h}^{n+\frac{1}{2}}_{j}'
        r'\cdot(0, \Delta x_j)$}')
    # \Delta t^n_{j-\frac{1}{2}}
    c.line((sexn.x, qdt), (sexn.x - vlen, qdt), arrows='->', linecolor='red')
    c.uput(
        0, 'ul', (sexn.x - vlen, qdt),
        r'{\color{red}$(\mathbf{h}^*)^{n,+}_{j-\frac{1}{2}}'
        r'\cdot(-\frac{\Delta t}{2}, 0)$}')
    # \Delta t^n_{j+\frac{1}{2}}
    c.line((sexp.x, qdt), (sexp.x + vlen, qdt), arrows='->', linecolor='blue')
    c.uput(
        0, 'ur', (sexp.x + vlen, qdt),
        r'{\color{blue}$(\mathbf{h}^*)^{n,+}_{j+\frac{1}{2}}'
        r'\cdot(\frac{\Delta t}{2}, 0)$}')

    return c
Example #11
0
    def setUp(self):

        self.grid10 = libst.Grid(xmin=0.0, xmax=10.0, nelm=10)
Example #12
0
    def _build_solver(resolution):

        # Build grid.
        xcrd = np.arange(resolution+1) / resolution
        xcrd *= 2 * np.pi
        grid = libst.Grid(xcrd)
        dx = (grid.xmax - grid.xmin) / grid.ncelm

        # Build solver.
        time_stop = 2*np.pi
        cfl_max = 1.0
        dt_max = dx * cfl_max
        nstep = int(np.ceil(time_stop / dt_max))
        dt = time_stop / nstep
        svr = libst.Solver(grid=grid, time_increment=dt, nvar=1)

        # Customize to linear wave solver.
        def xn(se, iv):
            displacement = 0.5 * (se.x + se.xneg) - se.xctr
            return se.dxneg * (se.get_so0(iv) + displacement * se.get_so1(iv))
        svr.kernel.xn_calc = xn

        def xp(se, iv):
            displacement = 0.5 * (se.x + se.xpos) - se.xctr
            return se.dxpos * (se.get_so0(iv) + displacement * se.get_so1(iv))
        svr.kernel.xp_calc = xp

        def tn(se, iv):
            displacement = se.x - se.xctr
            ret = se.get_so0(iv)  # f(u)
            ret += displacement * se.get_so1(iv)  # displacement in x; f_u == 1
            ret += se.qdt * se.get_so1(iv)  # displacement in t
            return se.hdt * ret
        svr.kernel.tn_calc = tn

        def tp(se, iv):
            displacement = se.x - se.xctr
            ret = se.get_so0(iv)  # f(u)
            ret += displacement * se.get_so1(iv)  # displacement in x; f_u == 1
            ret -= se.qdt * se.get_so1(iv)  # displacement in t
            return se.hdt * ret
        svr.kernel.tp_calc = tp

        def so0p(se, iv):
            ret = se.get_so0(iv)
            ret += (se.x-se.xctr) * se.get_so1(iv)  # displacement in x
            ret -= se.hdt * se.get_so1(iv)  # displacement in t
            return ret
        svr.kernel.so0p_calc = so0p

        def cfl(se):
            hdx = min(se.dxneg, se.dxpos)
            se.set_cfl(se.hdt / hdx)
        svr.kernel.cfl_updater = cfl

        # Initialize.
        svr.set_so0(0, np.sin(xcrd))
        svr.set_so1(0, np.cos(xcrd))
        svr.setup_march()

        return nstep, xcrd, svr
Example #13
0
def draw():

    grid = libst.Grid(0, 6, 4)
    sol = libst.Solver(grid=grid, nvar=1, time_increment=6 / 4)

    dx = (grid.xmax - grid.xmin) / grid.ncelm
    dt = dx
    hdt = dt / 2

    c = libst.PstCanvas(unit='2cm', padding=0.5)
    c.set(linewidth='1pt')
    c.set(linecolor='black')

    # CE grids.
    linewidth = '0.5pt'
    for se in sol.selms(odd_plane=False):
        c.line((se.x, 0), (se.x, dt), linewidth=linewidth)
    c.line((sol.selm(0).x, 0), (sol.selm(grid.ncelm).x, 0),
           linewidth=linewidth)
    c.line((sol.selm(0).x, hdt), (sol.selm(grid.ncelm).x, hdt),
           linewidth=linewidth)
    c.line((sol.selm(0).x, dt), (sol.selm(grid.ncelm).x, dt),
           linewidth=linewidth)
    for se in sol.selms(odd_plane=False):
        c.line((se.x, dt), (se.x, dt * 1.65),
               arrows='->',
               linewidth=linewidth,
               linestyle='dashed')
    c.line((sol.selm(0).x, dt * 1.5), (sol.selm(grid.ncelm).x, dt * 1.5),
           linewidth=linewidth,
           linestyle='dashed')

    # x-axis.
    sep = 0.05
    c.line((sol.selm(0).x, -hdt * 1.2), (sol.selm(grid.ncelm).x, -hdt * 1.2),
           linewidth=linewidth)
    c.uput(sep, 'l', (grid.xmin - sep, -hdt * 1.2), r'$j$')
    c.uput(sep, 'r', (grid.xmax + sep, -hdt * 1.2), r'$x$')
    for se in sol.selms(odd_plane=False):
        c.line((se.x, -hdt * 1.2 - sep), (se.x, -hdt * 1.2 + sep))
        c.uput(sep, 'd', (se.x, -hdt * 1.2 - sep), r'$%d$' % se.index)
    for se in sol.selms(odd_plane=True):
        c.line((se.x, -hdt * 1.2 - sep / 2), (se.x, -hdt * 1.2 + sep / 2))
        c.uput(sep, 'd', (se.x, -hdt * 1.2 - sep),
               r'$\frac{%d}{2}$' % (se.index * 2 + 1))

    # t-axis.
    x = grid.xmin - dx * 0.6
    c.line((x, 0), (x, dt), linewidth=linewidth)
    c.uput(sep, 'd', (x, 0 - sep), r'$n$')
    c.uput(sep, 'u', (x, dt + sep), r'$t$')
    c.line((x - sep, 0), (x + sep, 0))
    c.uput(sep, 'l', (x - sep, 0), r'$0$')
    c.line((x - sep / 2, hdt), (x + sep / 2, hdt))
    c.uput(sep, 'l', (x - sep, hdt), r'$\frac{1}{2}$')
    c.line((x - sep, dt), (x + sep, dt))
    c.uput(sep, 'l', (x - sep, dt), r'$1$')

    # SE and solution propagation.
    sep = 0.05
    sepx = dx * 0.1
    sept = dt * 0.1
    for se in sol.selms(odd_plane=False):
        c.selm(se,
               0,
               sep=sep,
               linestyle='dotted',
               dotsep='1pt',
               linecolor='red')
        if se.index != grid.ncelm:  # left to right.
            c.line((se.x + sepx, 0 + sept), (se.xpos - sepx, hdt - sept),
                   arrows='->',
                   linecolor='red')
        if se.index != 0:  # right to left.
            c.line((se.x - sepx, 0 + sept), (se.xneg + sepx, hdt - sept),
                   arrows='->',
                   linecolor='red')
    for se in sol.selms(odd_plane=True):
        c.selm(se,
               hdt,
               sep=sep,
               linestyle='dotted',
               dotsep='1pt',
               linecolor='blue')
        if se.index != grid.ncelm - 1:  # left to right.
            c.line((se.x + sepx, hdt + sept), (se.xpos - sepx, dt - sept),
                   arrows='->',
                   linecolor='blue')
        if se.index != 0:  # right to left.
            c.line((se.x - sepx, hdt + sept), (se.xneg + sepx, dt - sept),
                   arrows='->',
                   linecolor='blue')
    for se in sol.selms(odd_plane=False):
        c.selm(se,
               dt,
               sep=sep,
               linestyle='dotted',
               dotsep='1pt',
               linecolor='orange')

    return c