コード例 #1
0
ファイル: test_heat_2d.py プロジェクト: tlunet/pymgrit
def test_heat_2d_step_fe():
    """
    Test step()
    """
    x_start = 0
    x_end = 1
    y_start = 3
    y_end = 4
    nx = 5
    ny = 5
    a = 1
    heat_2d = Heat2D(a=a,
                     x_start=x_start,
                     x_end=x_end,
                     y_start=y_start,
                     y_end=y_end,
                     nx=nx,
                     ny=ny,
                     method='FE',
                     rhs=lambda x, y, t: 2 * x * y,
                     t_start=0,
                     t_stop=1,
                     nt=11)

    heat_2d_res = heat_2d.step(u_start=heat_2d.vector_t_start,
                               t_start=0,
                               t_stop=0.1)

    np.testing.assert_almost_equal(
        heat_2d_res.get_values(),
        np.array([[0., 0., 0., 0., 0.], [0., 0.1625, 0.175, 0.1875, 0.],
                  [0., 0.325, 0.35, 0.375, 0.],
                  [0., 0.4875, 0.525, 0.5625, 0.], [0., 0., 0., 0., 0.]]))
コード例 #2
0
ファイル: test_heat_2d.py プロジェクト: tlunet/pymgrit
def test_heat_2d_step_be():
    """
    Test step()
    """
    x_start = 0
    x_end = 1
    y_start = 3
    y_end = 4
    nx = 5
    ny = 5
    a = 1
    heat_2d = Heat2D(a=a,
                     x_start=x_start,
                     x_end=x_end,
                     y_start=y_start,
                     y_end=y_end,
                     nx=nx,
                     ny=ny,
                     method='BE',
                     rhs=lambda x, y, t: 2 * x * y,
                     t_start=0,
                     t_stop=1,
                     nt=11)

    heat_2d_res = heat_2d.step(u_start=heat_2d.vector_t_start,
                               t_start=0,
                               t_stop=0.1)

    np.testing.assert_almost_equal(
        heat_2d_res.get_values(),
        np.array([[0., 0., 0., 0., 0.],
                  [0., 0.06659024, 0.08719337, 0.07227713, 0.],
                  [0., 0.11922399, 0.15502696, 0.12990086, 0.],
                  [0., 0.12666875, 0.16193148, 0.1391124, 0.],
                  [0., 0., 0., 0., 0.]]))
コード例 #3
0
ファイル: test_heat_2d.py プロジェクト: tlunet/pymgrit
def test_heat_2d_step_cn():
    """
    Test step()
    """
    x_start = 0
    x_end = 1
    y_start = 3
    y_end = 4
    nx = 5
    ny = 5
    a = 1
    heat_2d = Heat2D(a=a,
                     x_start=x_start,
                     x_end=x_end,
                     y_start=y_start,
                     y_end=y_end,
                     nx=nx,
                     ny=ny,
                     method='CN',
                     rhs=lambda x, y, t: 2 * x * y,
                     t_start=0,
                     t_stop=1,
                     nt=11)

    heat_2d_res = heat_2d.step(u_start=heat_2d.vector_t_start,
                               t_start=0,
                               t_stop=0.1)

    np.testing.assert_almost_equal(
        heat_2d_res.get_values(),
        np.array([[0., 0., 0., 0., 0.],
                  [0., 0.09547237, 0.12246323, 0.10480116, 0.],
                  [0., 0.17564171, 0.22390841, 0.19336787, 0.],
                  [0., 0.1964882, 0.24654636, 0.21772176, 0.],
                  [0., 0., 0., 0., 0.]]))
コード例 #4
0
ファイル: test_heat_2d.py プロジェクト: tlunet/pymgrit
def test_heat_2d_constructor_boundary():
    """
    Test constructor
    """
    x_start = 0
    x_end = 1
    y_start = 3
    y_end = 4
    nx = 5
    ny = 5
    a = 1
    Heat2D(a=a,
           x_start=x_start,
           x_end=x_end,
           y_start=y_start,
           y_end=y_end,
           nx=nx,
           ny=ny,
           bc_top=lambda y: 2,
           bc_left=lambda y: 2,
           bc_right=lambda y: 2,
           bc_bottom=lambda y: 2,
           rhs=lambda x, y, t: 2 * x * y,
           t_start=0,
           t_stop=1,
           nt=11)
コード例 #5
0
ファイル: test_heat_2d.py プロジェクト: tlunet/pymgrit
def test_heat_2d_constructor_cn():
    """
    Test constructor
    """
    x_start = 0
    x_end = 1
    y_start = 3
    y_end = 4
    nx = 5
    ny = 5
    a = 1
    heat_2d = Heat2D(a=a,
                     x_start=x_start,
                     x_end=x_end,
                     y_start=y_start,
                     y_end=y_end,
                     nx=nx,
                     ny=ny,
                     method='CN',
                     rhs=lambda x, y, t: 2 * x * y,
                     t_start=0,
                     t_stop=1,
                     nt=11)
    np.testing.assert_almost_equal(
        heat_2d.compute_rhs(u_start=4 * np.ones((5, 5)),
                            t_start=0.2,
                            t_stop=0.3),
        np.array([
            0., 0., 0., 0., 0., 0., 4.1625, 4.175, 4.1875, 0., 0., 4.325, 4.35,
            4.375, 0., 0., 4.4875, 4.525, 4.5625, 0., 0., 0., 0., 0., 0.
        ]))
コード例 #6
0
ファイル: test_heat_2d.py プロジェクト: tlunet/pymgrit
def test_heat_2d_constructor_exception_boundary_top():
    """
    Test constructor
    """
    x_start = 0
    x_end = 1
    y_start = 3
    y_end = 4
    nx = 5
    ny = 5
    a = 1
    with pytest.raises(Exception):
        Heat2D(a=a,
               x_start=x_start,
               x_end=x_end,
               y_start=y_start,
               y_end=y_end,
               nx=nx,
               ny=ny,
               bc_top='2',
               rhs=lambda x, y, t: 2 * x * y,
               t_start=0,
               t_stop=1,
               nt=11)
コード例 #7
0
ファイル: test_heat_2d.py プロジェクト: tlunet/pymgrit
def test_heat_2d_constructor():
    """
    Test constructor
    """
    x_start = 0
    x_end = 1
    y_start = 3
    y_end = 4
    nx = 5
    ny = 5
    a = 1
    heat_2d = Heat2D(a=a,
                     x_start=x_start,
                     x_end=x_end,
                     y_start=y_start,
                     y_end=y_end,
                     nx=nx,
                     ny=ny,
                     t_start=0,
                     t_stop=1,
                     nt=11)

    np.testing.assert_equal(heat_2d.x_start, x_start)
    np.testing.assert_equal(heat_2d.x_end, x_end)
    np.testing.assert_equal(heat_2d.y_start, y_start)
    np.testing.assert_equal(heat_2d.y_end, y_end)
    np.testing.assert_equal(heat_2d.nx, nx)
    np.testing.assert_equal(heat_2d.ny, ny)
    np.testing.assert_almost_equal(heat_2d.dx, 0.25)
    np.testing.assert_almost_equal(heat_2d.dy, 0.25)
    np.testing.assert_equal(heat_2d.x, np.linspace(x_start, x_end, nx))
    np.testing.assert_equal(heat_2d.y, np.linspace(y_start, y_end, ny))
    np.testing.assert_equal(heat_2d.x_2d,
                            np.linspace(x_start, x_end, nx)[:, np.newaxis])
    np.testing.assert_equal(heat_2d.y_2d,
                            np.linspace(y_start, y_end, ny)[np.newaxis, :])
    np.testing.assert_equal(heat_2d.a, a)

    np.testing.assert_equal(True,
                            isinstance(heat_2d.vector_template, VectorHeat2D))
    np.testing.assert_equal(True,
                            isinstance(heat_2d.vector_t_start, VectorHeat2D))
    np.testing.assert_equal(heat_2d.vector_t_start.get_values(),
                            np.zeros((5, 5)))

    matrix = [[
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0.
    ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., -16., 0., 0., 0., -16., 64., -16., 0., 0., 0., -16., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., -16., 0., 0., 0., -16., 64., -16., 0., 0., 0., -16.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., -16., 0., 0., 0., -16., 64., -16., 0., 0., 0.,
                  -16., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., -16., 0., 0., 0., -16., 64., -16.,
                  0., 0., 0., -16., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., -16., 0., 0., 0., -16., 64.,
                  -16., 0., 0., 0., -16., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., -16., 0., 0., 0., -16., 64.,
                  -16., 0., 0., 0., -16., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -16., 0., 0., 0.,
                  -16., 64., -16., 0., 0., 0., -16., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -16., 0., 0.,
                  0., -16., 64., -16., 0., 0., 0., -16., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -16., 0.,
                  0., 0., -16., 64., -16., 0., 0., 0., -16., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ],
              [
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
              ]]
    np.testing.assert_equal(heat_2d.space_disc.toarray(), matrix)
コード例 #8
0
ファイル: example_heat_2d.py プロジェクト: tlunet/pymgrit
def main():
    def output_fcn(self):
        # Set path to solution
        path = 'results/heat_equation_2d'
        # Create path if not existing
        pathlib.Path(path).mkdir(parents=True, exist_ok=True)
        # Save solution with corresponding time point to file
        np.save(path + '/heat_equation_2d-rank' + str(self.comm_time_rank),
                [[[self.t[0][i], self.u[0][i]] for i in self.index_local[0]]])

    # example problem parameters
    x_end = 0.75
    y_end = 1.5
    a = 3.5

    def rhs(x, y, t):
        """
        Right-hand side of 2D heat equation example problem at a given space-time point (x,y,t),
          5x(x_end - x)y(y_end - y) + 10at(y(y_end-y)) + x(x_end - x),  a = 3.5

        :param x: x-coordinate of spatial grid point
        :param y: y_coordinate of spatial grid point
        :param t: time point
        :return: right-hand side of 2D heat equation example problem at point (x,y,t)
        """

        return 5 * x * (x_end - x) * y * (y_end -
                                          y) + 10 * a * t * (y *
                                                             (y_end - y) + x *
                                                             (x_end - x))

    def exact_sol(x, y, t):
        """
        Exact solution of 2D heat equation example problem at a given space-time point (x,y,t)
        Note: Can be used for computing the error of the MGRIT approximation

        :param x: x_coordinate of spatial grid point
        :param y: y_coordinate of spatial grid point
        :param t: time point
        :return: exact solution of 2D heat equation example problem at point (x,y,t)
        """
        return 5 * t * x * (x_end - x) * y * (y_end - y)

    heat0 = Heat2D(x_start=0,
                   x_end=x_end,
                   y_start=0,
                   y_end=y_end,
                   nx=55,
                   ny=125,
                   a=a,
                   rhs=rhs,
                   t_start=0,
                   t_stop=1,
                   nt=33)
    heat1 = Heat2D(x_start=0,
                   x_end=x_end,
                   y_start=0,
                   y_end=y_end,
                   nx=55,
                   ny=125,
                   a=a,
                   rhs=rhs,
                   t_interval=heat0.t[::2])

    # Setup two-level MGRIT solver and solve the problem
    mgrit = Mgrit(problem=[heat0, heat1],
                  cycle_type='V',
                  output_fcn=output_fcn)
    info = mgrit.solve()

    if MPI.COMM_WORLD.Get_rank() == 0:
        time.sleep(2)
        sol = []
        path = 'results/heat_equation_2d/'
        for filename in os.listdir(path):
            data = np.load(path + filename, allow_pickle=True).tolist()[0]
            sol += data
        sol.sort(key=lambda tup: tup[0])

        diff = 0
        for i in range(len(sol)):
            u_e = exact_sol(x=heat0.x_2d, y=heat0.y_2d, t=heat0.t[i])
            diff += abs(sol[i][1].get_values() - u_e).max()
        print("Difference between MGRIT solution and exact solution:", diff)