コード例 #1
0
def get_norm_jac_iteration_matrix(m, nu, omega):
    """
    Fuction to generate the norm of the iteration matrix solving the system
    
        (nu*A^2 + I)*u = A (y_d + A^{-1}*f) = A*y_d - f
        
    by using damped Jacobi

    Parameters
    ----------
    m : positive int
        size the matrix should have (for d=2 the matrix size is m**2 x m**2), 
        regarding your desired discretization
    nu : positive real number
        regularization parameter of the optimal control problem
    omega : real number between 0 and 1
        weighing parameter of the damped Jacobi iteration

    Returns
    -------
    Norm of the iteration matrix using damped Jacobi

    """
    A = fd_laplace(m, 2).toarray()
    n = m**2
    A_2 = A.dot(A)
    System = np.eye(n) + nu * A_2
    D = np.diag(np.diag(System))
    D_inv = omega * np.linalg.inv(D)
    It_matrix = D_inv.dot((1 / omega) * D - System)
    return np.linalg.norm(It_matrix, ord=2)
コード例 #2
0
def test_jacobi(nu_start, m, omega, plot="nu"):
    #y_d = 10*np.random.rand(m**2)

    #f=np.random.rand(m**2)
    u_sol = np.random.rand(m**2)
    u_guess = np.random.rand(m**2)

    maxIter = 10000

    for nu in nu_start:
        A = fd_laplace(m, 2)
        A_2 = np.dot(A, A)
        C = sparse.eye((m**2)) + nu * A_2

        f = C.dot(u_sol)

        #f  = A.dot(y_d) - f
        u_test, num_iters, res = solver_damped_jacobi(C, u_guess, omega, f,
                                                      maxIter)
        x = np.arange(0, num_iters)

        #u_test2 = sparse.linalg.spsolve(C, f)

        #print("norm error = {}".format(np.linalg.norm(u_sol - u_test)))
        if plot == "nu":
            plt.semilogy(x, res, label="nu = {}".format(nu))
        else:
            plt.semilogy(x, res, label="m = {}".format(m))
コード例 #3
0
def sparse_sol():
    nu = 0.01
    l = 4
    m = 2**l - 1
    A = fd_laplace(m, 2)
    u_sol = np.random.rand(m**2)
    A_2 = A.dot(A)
    S_jacobi = sparse.eye((m**2)) + nu * A_2
    f = S_jacobi.dot(u_sol)
    sparse.linalg.spsolve(S_jacobi, f)
コード例 #4
0
def plot_mulitgrid_jacobi():
    omega = 0.5
    plt.figure(figsize=(20, 20))
    #plt.suptitle(r"Using multigrid to solve the system $(\ nu \cdot A^2+  I )v = f$ with dampeed jacobi with damping parameter $\omega =$ {}   and  comparing this to the convergence of the smoother as a stationary method".format(omega))
    ############# important nu1 = 2 is essentially one step since we break after nu-1 iterations
    nu1 = 2
    nu2 = 2
    level = 3
    j = 1
    for i in range(0, 4):
        nu = 0.01 * 10**i
        for l in range(4, 7):

            m = 2**l - 1
            #h = 1/(m+1)
            A = fd_laplace(m, 2)

            u_sol = np.random.rand(m**2)
            u_guess = np.zeros(m**2)

            A_2 = A.dot(A)
            S_jacobi = sparse.eye((m**2)) + nu * A_2

            f = S_jacobi.dot(u_sol)

            u_jac, res_jac, k_jac = multigrid_jacobi(S_jacobi,nu, f, u_guess, m, omega, \
                                                     nu1, nu2, level)
            (u_jac_m, k_jac_m, res_jac_m) = solver_damped_jacobi(S_jacobi,
                                                                 u_guess,
                                                                 omega,
                                                                 f,
                                                                 maxIter=k_jac)

            x_jac = np.arange(0, k_jac)
            x_jac_m = np.arange(0, k_jac_m)

            plt.subplot(4, 3, j)
            plt.semilogy(x_jac,
                         res_jac,
                         "b-",
                         label="multigrid with {} levels".format(level))
            plt.semilogy(x_jac_m, res_jac_m, "r-", label="damped jacobi")
            plt.legend(loc="upper right")
            plt.ylabel("normalized residual ")
            plt.xlabel("iterations")
            plt.title(r"$m =$ {0}, $\nu =$ {1}".format(m, nu))
            print("norm differences = {}".format(np.linalg.norm(u_sol -
                                                                u_jac)))
            j = j + 1
    #plt.tight_layout()
    plt.show()
コード例 #5
0
def multi_sol():
    omega = 0.5
    nu1 = 3
    nu2 = 3
    nu = 0.01
    level = 3
    l = 4
    m = 2**l - 1
    A = fd_laplace(m, 2)
    u_sol = np.random.rand(m**2)
    u_guess = np.zeros((m**2))
    A_2 = A.dot(A)
    S_jacobi = sparse.eye((m**2)) + nu * A_2
    f = S_jacobi.dot(u_sol)
    multigrid_jacobi(nu, f, u_guess, m, omega, nu1, nu2, level)
コード例 #6
0
def test_factored(nu_start, m, plot="nu"):
    y_d = 10 * np.ones(m**2)

    u_sol = np.random.rand(m**2)

    u_guess = np.random.rand(m**2)

    A = fd_laplace(m, d=2)

    for nu in nu_start:
        f = (-1) * (nu * A.dot(A.dot(u_sol)) + u_sol - A.dot(y_d))
        u_test, info, num_iters, res = solver_poisson_factored_cg(
            u_guess, nu, y_d, f, m)
        x = np.arange(0, num_iters)
        if plot == "nu":
            plt.semilogy(x, res, label="nu = {}".format(nu))
        else:
            plt.semilogy(x, res, label="m = {}".format(m))
コード例 #7
0
def test_stationary(nu_start, m, plot="nu", toler=1e-12):
    y_d = 10 * np.ones(m**2)

    u_sol = np.random.rand(m**2)

    u_guess = np.random.rand(m**2)

    A = fd_laplace(m, d=2)

    for nu in nu_start:
        f = (-1) * (nu * A.dot(A.dot(u_sol)) + u_sol - A.dot(y_d))
        u_test, num_iters, res = solver_stationary(u_guess,
                                                   nu,
                                                   y_d,
                                                   f,
                                                   m,
                                                   tol=toler)
        x = np.arange(0, num_iters)
        if plot == "nu":
            plt.semilogy(x, res, label="nu = {}".format(nu))
        else:
            plt.semilogy(x, res, label="m = {}".format(m))
コード例 #8
0
@author: florianwolf
"""

import numpy as np
from scipy.sparse.linalg import LinearOperator
from ReducedModel import fd_laplace, condition_number_factored, condition_number_normal,\
    multigrid_jacobi, get_system, multigrid_stat,solver_stationary_fixedRight,solver_damped_jacobi,\
    fast_poisson, laplace_small_decomposition

from scipy import sparse
import matplotlib.pyplot as plt
import timeit
plt.close('all')

l = 6

m = 2**l - 1

A = fd_laplace(m, d=2)

for i in range(100):
    x_sol = np.random.rand(m**2)
    y = A.dot(x_sol)

    lam, V = laplace_small_decomposition(m)

    x_test = fast_poisson(V, V, lam, lam, y)

    print("norm of the differences = {}".format(np.linalg.norm(x_sol -
                                                               x_test)))