示例#1
0
def main():
    # Create Dahlquist's test problem using implicit mid-point rule time integration
    dahlquist_lvl0 = Dahlquist(t_start=0, t_stop=5, nt=101, method='MR')
    # Create Dahlquist's test problem using backward Euler time integration
    dahlquist_lvl1 = Dahlquist(t_start=0, t_stop=5, nt=51, method='BE')

    # Setup an MGRIT solver and solve the problem
    mgrit = Mgrit(problem=[dahlquist_lvl0, dahlquist_lvl1])
    info = mgrit.solve()
示例#2
0
def test_dahlquist_step_fe():
    """
    Test step()
    """
    dahlquist = Dahlquist(method='FE', t_start=0, t_stop=1, nt=11)
    dahlquist_res = dahlquist.step(u_start=VectorDahlquist(1),
                                   t_start=0,
                                   t_stop=0.1)
    np.testing.assert_almost_equal(dahlquist_res.get_values(), 0.9)
示例#3
0
def main():
    t_interval = np.linspace(0, 5, 17)
    dahlquist_0 = Dahlquist(t_start=0, t_stop=5, nt=65)
    dahlquist_1 = Dahlquist(t_interval=dahlquist_0.t[[0,3,10,12,14,17,23,27,33,34,55,57,59,61,63,64]])
    dahlquist_2 = Dahlquist(t_interval=dahlquist_1.t[::2])
    dahlquist_3 = Dahlquist(t_interval=dahlquist_2.t[::2])
    dahlquist_4 = Dahlquist(t_interval=dahlquist_3.t[::2])
    mgrit = Mgrit(problem=[dahlquist_0, dahlquist_1, dahlquist_2, dahlquist_3,dahlquist_4], tol=1e-10, nested_iteration=False)
    #mgrit.plot_parallel_distribution(time_procs=6, save_name='test.png')
    info = mgrit.solve()
示例#4
0
def test_dahlquist_constructor():
    """
    Test constructor
    """
    dahlquist = Dahlquist(t_start=0, t_stop=1, nt=11)

    np.testing.assert_equal(
        True, isinstance(dahlquist.vector_template, VectorDahlquist))
    np.testing.assert_equal(
        True, isinstance(dahlquist.vector_t_start, VectorDahlquist))
    np.testing.assert_equal(dahlquist.vector_t_start.get_values(), 1)
    np.testing.assert_equal('BE', dahlquist.method)
示例#5
0
def main():
    # Create Dahlquist's test problem with 101 time steps in the interval [0, 5]
    dahlquist = Dahlquist(t_start=0, t_stop=5, nt=101)

    # Construct a two-level multigrid hierarchy for the test problem using a coarsening factor of 2
    dahlquist_multilevel_structure = simple_setup_problem(problem=dahlquist, level=2, coarsening=2)

    # Set up the MGRIT solver for the test problem and set the solver tolerance to 1e-10
    mgrit = Mgrit(problem=dahlquist_multilevel_structure, tol=1e-10)

    # Solve the test problem
    info = mgrit.solve()
示例#6
0
def main():
    # Option 1: Use PyMGRIT's core function simple_setup_problem()
    dahlquist_multilevel_structure_1 = simple_setup_problem(problem=Dahlquist(
        t_start=0, t_stop=5, nt=101),
                                                            level=3,
                                                            coarsening=2)
    Mgrit(problem=dahlquist_multilevel_structure_1, tol=1e-10).solve()

    # Option 2: Build each level using t_start, t_end, and nt
    dahlquist_lvl_0 = Dahlquist(t_start=0, t_stop=5, nt=101)
    dahlquist_lvl_1 = Dahlquist(t_start=0, t_stop=5, nt=51)
    dahlquist_lvl_2 = Dahlquist(t_start=0, t_stop=5, nt=26)
    dahlquist_multilevel_structure_2 = [
        dahlquist_lvl_0, dahlquist_lvl_1, dahlquist_lvl_2
    ]
    Mgrit(problem=dahlquist_multilevel_structure_2, tol=1e-10).solve()

    # Option 3: Specify time intervals for each grid level
    t_interval = np.linspace(0, 5, 101)
    dahlquist_lvl_0 = Dahlquist(t_interval=t_interval)
    dahlquist_lvl_1 = Dahlquist(
        t_interval=t_interval[::2])  # Takes every second point from t_interval
    dahlquist_lvl_2 = Dahlquist(
        t_interval=t_interval[::4])  # Takes every fourth point from t_interval
    dahlquist_multilevel_structure_3 = [
        dahlquist_lvl_0, dahlquist_lvl_1, dahlquist_lvl_2
    ]
    Mgrit(problem=dahlquist_multilevel_structure_3, tol=1e-10).solve()

    # Option 4: Mix options 2 and 3
    dahlquist_lvl_0 = Dahlquist(t_start=0, t_stop=5, nt=101)
    dahlquist_lvl_1 = Dahlquist(
        t_interval=dahlquist_lvl_0.t[::2])  # Using t from the upper level.
    dahlquist_lvl_2 = Dahlquist(t_start=0, t_stop=5, nt=26)
    dahlquist_multilevel_structure_4 = [
        dahlquist_lvl_0, dahlquist_lvl_1, dahlquist_lvl_2
    ]
    Mgrit(problem=dahlquist_multilevel_structure_4, tol=1e-10).solve()
示例#7
0
def main():

    # Create Dahlquist's test problem with 101 time steps in the interval [0, 5]
    dahlquist = Dahlquist(t_start=0, t_stop=5, nt=101)

    # Construct a two-level multigrid hierarchy for the test problem using a coarsening factor of 2
    dahlquist_multilevel_structure = simple_setup_problem(problem=dahlquist,
                                                          level=2,
                                                          coarsening=2)

    # Set up the MGRIT solver for the test problem
    mgrit = Mgrit(
        problem=dahlquist_multilevel_structure,  # Problem structure
        transfer=None,  # Spatial grid transfer. Automatically set if None.
        max_iter=10,  # Maximum number of iterations (default: 100)
        tol=1e-10,  # Stopping tolerance (default: 1e-7)
        nested_iteration=
        True,  # Use (True) or do not use (False) nested iterations
        # (default: True)
        cf_iter=1,  # Number of CF relaxations (default: 1)
        cycle_type='V',  # multigrid cycling type (default: 'V'):
        # 'V' -> V-cycles
        # 'F' -> F-cycles
        comm_time=MPI.
        COMM_WORLD,  # Time communicator (default: MPI.COMM_WORLD)
        comm_space=MPI.
        COMM_NULL,  # Space communicator (default: MPI.COMM_NULL)
        weight_c=1,  # C - relaxation weight (default: 1)
        logging_lvl=20,  # Logging level (default: 20):
        # 10: Debug -> Runtime of all components
        # 20: Info  -> Info per iteration + summary
        # 30: None  -> No information
        output_fcn=None,  # Function for saving solution values to file
        # (default: None)
        output_lvl=1,  # Output level (default: 1):
        # 0 -> output_fcn is never called
        # 1 -> output_fcn is called at the end of the simulation
        # 2 -> output_fcn is called after each MGRIT iteration
        t_norm=2,  # Temporal norm
        # 1 -> One-norm
        # 2 -> Two-norm
        # 3 -> Infinity-norm
        random_init_guess=
        False  # Use (True) or do not use (False) random initial guess
        # for all unknowns (default: False)
    )

    # Solve the test problem
    return mgrit.solve()
def main():
    # Define output function that writes the solution to a file
    def output_fcn(self):
        # Set path to solution
        path = 'results/' + 'dahlquist'
        # Create path if not existing
        pathlib.Path(path).mkdir(parents=True, exist_ok=True)

        # Save solution to file; here, we just have a single solution value at each time point.
        # Useful member variables of MGRIT solver:
        #   - self.t[0]           : local fine-grid (level 0) time interval
        #   - self.index_local[0] : indices of local fine-grid (level 0) time interval
        #   - self.u[0]           : fine-grid (level 0) solution values
        np.save(path + '/dahlquist',
                [self.u[0][i].get_values() for i in self.index_local[0]
                 ])  # Solution values at local time points

    # Create Dahlquist's test problem with 101 time steps in the interval [0, 5]
    dahlquist = Dahlquist(t_start=0, t_stop=5, nt=101)

    # Construct a two-level multigrid hierarchy for the test problem using a coarsening factor of 2
    dahlquist_multilevel_structure = simple_setup_problem(problem=dahlquist,
                                                          level=2,
                                                          coarsening=2)

    # Set up the MGRIT solver for the test problem and set the output function
    mgrit = Mgrit(problem=dahlquist_multilevel_structure,
                  output_fcn=output_fcn)

    # Solve the test problem
    info = mgrit.solve()

    # Plot the solution (Note: modifications necessary if more than one process is used for the simulation!)
    t = np.linspace(dahlquist.t_start, dahlquist.t_end, dahlquist.nt)
    sol = np.load('results/dahlquist/dahlquist.npy')
    plt.plot(t, sol)
    plt.xlabel('t')
    plt.ylabel('u(t)')
    plt.show()
示例#9
0
def test_dahlquist_constructor_exception():
    """
    Test constructor
    """
    with pytest.raises(Exception):
        Dahlquist(t_start=0, t_stop=1, nt=11, method='unknown')
示例#10
0
def test_dahlquist_constructor_tr():
    """
    Test constructor
    """
    dahlquist = Dahlquist(t_start=0, t_stop=1, nt=11, method='TR')
    np.testing.assert_equal('TR', dahlquist.method)