Exemple #1
0
def plot_rbf_qr():
    # in_mesh = mesh.GaussChebyshev_2D(12, 1, 4, 1)
    in_mesh = mesh.GaussChebyshev_1D(12, 1, 4, 0) - 4
    in_vals = func(in_mesh)
    plot_mesh = np.linspace(np.min(in_mesh), np.max(in_mesh), 2000)  # Use a fine mesh for plotting

    shape_param = 1e-3
    print("Proposed shape parameter =", shape_param)
    rbf_qr = RBF_QR_1D(shape_param, in_mesh, in_vals)

    fig1 = plt.figure("RBF-QR Interpolation")
    ax1 = fig1.add_subplot(111)
    ax1.set_title("Interpolation on N= " + str(len(in_mesh)) + " nodes with m=" + str(shape_param))
    ax1.plot(in_mesh, in_vals, "d")
    ax1.plot(plot_mesh, func(plot_mesh), "-", label="Original Function")
    ax1.plot(plot_mesh, rbf_qr(plot_mesh), "--", label="Interpolation RBF_QR_1D, m=" + str(shape_param))
    ax1.set_ylabel("y")
    ax1.set_xlabel("x")
    ax1.legend(loc=2)
    ax1.grid()

    fig2 = plt.figure("Error")
    ax2 = fig2.add_subplot(111)
    ax2.set_title("Error of RBF_QR on N=" + str(len(in_mesh)) + " nodes with m=" + str(shape_param))
    ax2.plot(plot_mesh, np.abs(func(plot_mesh) - rbf_qr(plot_mesh)), "-", label="Error RBF_QR_1D")
    ax2.plot(plot_mesh, np.full(plot_mesh.shape, np.finfo(np.float64).eps), "-", label="Machine precision (eps)")
    ax2.set_yscale("log")
    ax2.set_ylabel("Error")
    ax2.set_xlabel("x")
    ax2.legend(loc=2)
    ax2.grid()

    plt.show()
Exemple #2
0
def combined():
    shape_parameter_space = np.logspace(0, -10, num=50)
    chebgauss_order_space = range(1, 40, 1)
    X, Y = np.meshgrid(shape_parameter_space, chebgauss_order_space)
    rmse = np.empty(X.shape)
    for i, j in tqdm(list(np.ndindex(X.shape))):
        shape = X[i, j]
        order = Y[i, j]
        in_mesh = mesh.GaussChebyshev_1D(order, 1, 4, 0)
        in_vals = func(in_mesh)
        test_mesh = np.linspace(np.min(in_mesh), np.max(in_mesh), 100)
        rbf_qr = RBF_QR_1D(shape, in_mesh, in_vals)
        rmse[i, j] = rbf_qr.RMSE(func, test_mesh)
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot_surface(np.log10(X), 4 * Y, np.log10(rmse))
    ax.set_xlabel("Shape parameter (log)")
    ax.set_ylabel("Meshsize")
    ax.set_zlabel("RMSE (log)")
    plt.show()
Exemple #3
0
def evalShapeQR():
    in_mesh = mesh.GaussChebyshev_1D(4, 1, 4, 0)
    in_vals = func(in_mesh)
    test_mesh = np.linspace(np.min(in_mesh), np.max(in_mesh), 2000)
    shape_parameter_space = np.logspace(0.8, 0, num=100)
    rmse = []
    for shape_param in shape_parameter_space:
        print("m = ", shape_param)
        rbf_qr = RBF_QR_1D(shape_param, in_mesh, in_vals)
        rmse.append(rbf_qr.RMSE(func, test_mesh))
    rmse = np.array(rmse)
    plt.plot(shape_parameter_space, rmse, "-", label="RMSE")
    plt.title("RMSE of RBF-QR for different epsilon, both axes log, mapping " + str(len(in_mesh))
              + " nodes to " + str(len(test_mesh)) + " nodes")
    plt.ylabel("RMSE")
    plt.xlabel("epsilon")
    plt.yscale("log")
    # plt.xscale("log") #mplot3d bug
    plt.legend(loc=2)
    plt.grid()
    plt.show()
Exemple #4
0
def plot_mesh_sizes():
    GC = True
    # mesh_sizes = np.arange(2, 64) if GC else np.linspace(4, 1024, 200)
    mesh_sizes = np.arange(2, 32) if GC else np.linspace(4, 496, 200)
    test_mesh = np.linspace(1, 4, 4000)
    test_vals = func(test_mesh)
    m = 15

    separated = []
    integrated = []
    no_pol = []

    for size in mesh_sizes:
        in_mesh = mesh.GaussChebyshev_1D(size, 0.25, 4,
                                         1) if GC else np.linspace(1, 4, size)
        in_vals = func(in_mesh)
        shape = rescaleBasisfunction(Gaussian, m, in_mesh)
        print("Computing with in mesh size =", int(size),
              ", resulting shape parameter =", shape)
        # bf = functools.partial(Gaussian, shape=shape)
        bf = create_BFs(Gaussian, m, in_mesh)
        separated.append(SeparatedConsistent(bf, in_mesh, in_vals))
        integrated.append(IntegratedConsistent(bf, in_mesh, in_vals))
        no_pol.append(NoneConsistent(bf, in_mesh, in_vals))

    if GC: mesh_sizes = mesh_sizes * 4 * 4  # for GC
    fig, ax1 = plt.subplots()
    print("Computing RMSE")
    ax1.semilogy(mesh_sizes, [i.RMSE(func, test_mesh) for i in separated],
                 "--",
                 label="RMSE separated Polynomial")
    ax1.semilogy(mesh_sizes, [i.RMSE(func, test_mesh) for i in integrated],
                 "--",
                 label="RMSE integrated polynomial")
    ax1.semilogy(mesh_sizes, [i.RMSE(func, test_mesh) for i in no_pol],
                 "--",
                 label="RMSE no polynomial")

    ax1.set_ylabel("RMSE")
    # ax1.legend(loc = 3)

    ax2 = ax1.twinx()
    print("Computing conditon")
    ax2.semilogy(mesh_sizes, [i.condC for i in separated],
                 label="Condition separated / no polynomial")
    ax2.semilogy(mesh_sizes, [i.condC for i in integrated],
                 label="Condition integrated polynomial")
    ax2.set_ylabel("Condition Number")
    # ax2.legend(loc = 2, framealpha = 1)

    multi_legend(ax1, ax2, loc=7)

    ax1.set_xlabel("Input Mesh Size")
    # ax1.set_xlabel("Gauss-Chebyshev Order")
    # plt.title("m = " + str(m))

    rm = [i.RMSE(func, test_mesh) for i in separated]

    plt.grid()
    # plt.show()
    plt.savefig("rc-gc-size.pdf")
Exemple #5
0
def gc_m_order():
    """ Keep number of elements constant, increase order, thus also points.
    Different plots for different m's.
    to have a comparision to equi-distant meshes """

    ms = [2, 4, 6, 8]
    gc_orders = np.arange(2, 24)

    test_mesh = np.linspace(1, 4, 5000)
    test_vals = func(test_mesh)

    f, sp = plt.subplots(2, 2, sharex='col', sharey='row')
    sp_lin = [sp[0][0], sp[0][1], sp[1][0], sp[1][1]]

    for i, m in enumerate(ms):
        print("Working on m =", m)
        separated = []
        integrated = []
        no_pol = []

        for gc_order in gc_orders:
            in_mesh = mesh.GaussChebyshev_1D(gc_order, 1, 4, 1)
            in_vals = func(in_mesh)
            # shape = rescaleBasisfunction(Gaussian, m, in_mesh)
            shape = -1
            bf = create_BFs(Gaussian, m, in_mesh)
            # bf = functools.partial(Gaussian, shape=shape)

            separated.append(SeparatedConsistent(bf, in_mesh, in_vals))
            integrated.append(IntegratedConsistent(bf, in_mesh, in_vals))
            no_pol.append(NoneConsistent(bf, in_mesh, in_vals))

        ax1 = sp_lin[i]

        ax1.semilogy(gc_orders, [i.RMSE(func, test_mesh) for i in separated],
                     "--",
                     label="RMSE separated Polynomial")
        ax1.semilogy(gc_orders, [i.RMSE(func, test_mesh) for i in integrated],
                     "--",
                     label="RMSE integrated polynomial")
        ax1.semilogy(gc_orders, [i.RMSE(func, test_mesh) for i in no_pol],
                     "--",
                     label="RMSE no polynomial")

        ax1.set_ylabel("RMSE")
        # ax1.set_ylim(10e-6, 10e0)
        ax1.legend(loc=3)

        ax2 = ax1.twinx()
        ax2.semilogy(gc_orders, [i.condC for i in separated],
                     label="Condition separated / no polynomial")
        ax2.semilogy(gc_orders, [i.condC for i in integrated],
                     label="Condition integrated polynomial")
        ax2.set_ylabel("Condition Number")
        ax2.legend()

        ax1.set_xlabel("Gauss-Chebyshev Order")
        ax1.set_title(
            "m = {} (shape parameter = {:.3f}), element size = 1, domain size = 4"
            .format(m, shape))
        ax1.grid()

    plt.show()
""" Demo for conservative error metrics. """

import csv
import numpy as np, matplotlib.pyplot as plt, pandas as pd
import mesh, basisfunctions, rbf, testfunctions

testfunction = testfunctions.Highfreq()

in_mesh = mesh.GaussChebyshev_1D(order=12,
                                 element_size=0.25,
                                 domain_size=1,
                                 domain_start=0)

# in_mesh = np.linspace(0, 1, 48)
out_mesh = np.linspace(np.min(in_mesh), np.max(in_mesh), 20)

# in_mesh = np.geomspace(1, 100, 90)
# in_mesh = in_mesh / 100
# out_mesh = np.linspace(0, 1, 80)

in_vals = testfunction(in_mesh)

m = 6
bf = basisfunctions.Gaussian(
    basisfunctions.Gaussian.shape_param_from_m(m, in_mesh))

interp = rbf.SeparatedConservative(bf, in_mesh, in_vals)
one_interp = rbf.NoneConservative(bf,
                                  in_mesh,
                                  np.ones_like(in_vals),
                                  rescale=False)
Exemple #7
0
tf = lambda x: testfunctions.Highfreq()(x * 0.5 + 0.5)
# tf = func
BF = basisfunctions.Gaussian

test_mesh = np.linspace(-0.6, 0.6, 4000)
# test_mesh = np.linspace(0.2, 0.8, 4000)
test_vals = tf(test_mesh)

orders = np.arange(4, 128, 1)
# element_size = 0.0625

res = []

for order in orders:
    in_mesh = mesh.GaussChebyshev_1D(order=order,
                                     element_size=0.25,
                                     domain_size=2,
                                     domain_start=-1)
    # in_mesh = np.polynomial.chebyshev.chebgauss(order)[0]

    in_vals = tf(in_mesh)

    shape_param = BF.shape_param_from_m(6, in_mesh)
    bf = BF(shape_param)

    interp = rbf.SeparatedConsistent(bf, in_mesh, in_vals)
    rbfqr = rbf_qr.RBF_QR_1D(1e-5, in_mesh, in_vals)

    out = interp(test_mesh)
    outq = rbfqr(test_mesh)

    res.append({