def test_has_spatial_order_of_accuracy():
    expected_order = 2

    k = 4
    ntests = 6

    ndofs = []
    err_list = []
    for i in range(ntests):
        ndofs.append(2**(i + 4) - 1)

        prob = Poisson1D(ndofs[-1])

        xvalues = np.array([(i + 1) * prob.dx for i in range(prob.ndofs)])
        uinit = np.sin(np.pi * k * xvalues)
        uexact = (np.pi * k)**2 * uinit
        ucomp = prob.A.dot(uinit)

        err_list.append(
            np.linalg.norm(uexact - ucomp, np.inf) /
            np.linalg.norm(uexact, np.inf))

    order = []
    for i in range(1, len(err_list)):
        order.append(
            np.log(err_list[i - 1] / err_list[i]) /
            np.log(ndofs[i] / ndofs[i - 1]))

    order = np.array(order)

    assert (order > expected_order * 0.9).all() and (order < expected_order * 1.1).all(), \
        'Order of accuracy of the spatial discretization is not ' + str(expected_order)
def exact_is_fixpoint(smoo_class):
    ndofs = 127
    prob = Poisson1D(ndofs)
    smoo = smoo_class(prob.A, 2.0 / 3.0)
    k = 6

    xvalues = np.array([(i + 1) * prob.dx for i in range(prob.ndofs)])
    rhs = np.sin(np.pi * k * xvalues)
    uex = spLA.spsolve(smoo.A, rhs)

    u = uex
    for i in range(10):
        u = smoo.smooth(rhs, u)

    err = np.linalg.norm(u - uex, np.inf)

    assert err <= 1E-14, 'Exact solution is not a fixpoint of the iteration!'
    def setUp(self):
        ndofs = 31
        nlevels = int(np.log2(ndofs + 1))
        self.prob = Poisson1D(ndofs=ndofs)

        self.mymg = MyMultigrid(ndofs, nlevels=nlevels)
        self.mymg.attach_transfer(LinearTransfer)
        self.mymg.attach_smoother(WeightedJacobi, self.prob.A, omega=2.0 / 3.0)

        k = 6
        xvalues = np.array([(i + 1) * self.prob.dx
                            for i in range(self.prob.ndofs)])
        self.u = np.sin(np.pi * k * xvalues)

        self.nu0 = 1
        self.nu1 = 2
        self.nu2 = 2
Beispiel #4
0
def attach_smoother_works(mult):
    prob = Poisson1D(ndofs=ndofs)
    mult.attach_transfer(LinearTransfer)
    mult.attach_smoother(WeightedJacobi, prob.A, omega=2.0 / 3.0)
    assert isinstance(mult.Acoarse, sp.csc_matrix), type(mult.Acoarse)
import numpy as np
import scipy.linalg as LA
from matplotlib import rc

from project.gauss_seidel import GaussSeidel
from project.poisson1d import Poisson1D
from project.weighted_jacobi import WeightedJacobi

if __name__ == "__main__":

    rc('font', family='sans-serif', size=32)
    rc('legend', fontsize='small')
    rc('xtick', labelsize='small')
    rc('ytick', labelsize='small')

    prob = Poisson1D(ndofs=63)
    urand = np.random.rand(prob.ndofs)

    niter = 100

    smoother_list = [(WeightedJacobi(prob=prob,
                                     omega=2.0 / 3.0), 'Jacobi_w23'),
                     (GaussSeidel(prob=prob), 'GaussSeidel')]

    for smoother, fname in smoother_list:

        u = urand
        uex = prob.u_exact
        err = [LA.norm(u - uex, np.inf)]

        for i in range(niter):
def setup():
    global list, ndofs, prob
    list = get_derived_from_in_package(SmootherBase, 'project')
    ndofs = 10
    prob = Poisson1D(ndofs)
Beispiel #7
0
if __name__ == "__main__":
    # build SimplePFASSTCollocationProblem
    num_nodes = 3
    num_subintervals = 2
    num_space = 16
    k = 1
    dt = 0.01
    GRC = CollGaussRadau_Right(num_nodes, 0.0, 1.0)
    Q = GRC.Qmat[1:, 1:]
    QD = GRC.QDmat
    # matrix_plot(QD)
    # print QD.shape, Q.shape
    nodes = GRC.nodes
    CTSB = CollocationTimeStepBase(0.0, dt, Q, nodes)
    CTSB_delta = CollocationTimeStepBase(0.0, dt, QD, nodes)
    SpaceProblem = Poisson1D(num_space)
    omega_h = np.linspace(1 / (num_space + 1), 1.0, num_space)
    u_init = np.sin(2 * np.pi * np.linspace(1 /
                                            (num_space + 1), 1.0, num_space))
    u_init_gen = lambda x: np.sin(2 * np.pi * x)
    # show_call_order(SimplePFASSTCollocationProblem, '__init__')
    PFASSTCollocProb = SimplePFASSTCollocationProblem(num_subintervals, CTSB,
                                                      SpaceProblem, u_init_gen)
    PFASSTPrecondProb = SimplePFASSTCollocationProblem(num_subintervals,
                                                       CTSB_delta,
                                                       SpaceProblem,
                                                       u_init_gen)

    sol = splinalg.spsolve(PFASSTCollocProb.A, PFASSTCollocProb.rhs)
    sol_precond = splinalg.spsolve(PFASSTPrecondProb.A, PFASSTPrecondProb.rhs)
    from project.poisson1d import Poisson1D
    from project.weighted_jacobi import WeightedJacobi
    # from project.gauss_seidel import GaussSeidel
    from project.linear_transfer import LinearTransfer
    from project.mymultigrid import MyMultigrid
    from pymg.problem_base import ProblemBase
    from project.solversmoother import SolverSmoother

    norm_type = 2
    ntests = 1
    ndofs = 15
    iter_max = 10
    # nlevels = int(np.log2(ndofs + 1))
    nlevels = 2
    prob = Poisson1D(ndofs=ndofs)
    mymg = MyMultigrid(ndofs=ndofs, nlevels=nlevels)
    mymg.attach_transfer(LinearTransfer)
    mymg.attach_smoother(WeightedJacobi, prob.A, omega=2.0 / 3.0)
    k = 6
    xvalues = np.array([(i + 1) * prob.dx for i in range(prob.ndofs)])
    prob.rhs = (np.pi * k)**2 * np.sin(np.pi * k * xvalues)
    uex = spla.spsolve(prob.A, prob.rhs)
    res = 1
    err = []
    err_so = []  # Smoother only error
    u = np.zeros(uex.size)
    u_so = np.zeros(uex.size)
    w_jac = mymg.smoo[0]
    # do the multigrid iterationssteps along side with the
    # smoother only steps