예제 #1
0
def main():
    parallel = True
    workers = 8
    writeCSV = True
    chunk_size = 30
    
    # mesh_sizes = np.linspace(10, 5000, num = 50)
    # mesh_sizes = np.linspace(10, 200, num = 2, dtype = int)
    # mesh_sizes = np.logspace(10, 14, base = 2, num = 40) # = [1024, 16384]

    # Output of np.geomspace(100, 15000, num = 40, dtype = int) , not available at neon
    mesh_sizes = np.array([  100,   113,   129,   147,   167,   190,   216,   245,   279,
                             317,   361,   410,   467,   531,   604,   687,   781,   888,
                             1010,  1148,  1306,  1485,  1688,  1920,  2183,  2482,  2823,
                             3210,  3650,  4150,  4719,  5366,  6102,  6939,  7890,  8972,
                             10202, 11601, 13191, 15000])

    # Resampling of the output above
    # mesh_sizes += np.array([  106.5,   138. ,   178.5,   230.5,   298. ,   385.5,   499. ,
    #                           645.5,   834.5,  1079. ,  1395.5,  1804. ,  2332.5,  3016.5,
    #                           3900. ,  5042.5,  6520.5,  8431. , 10901.5, 14095.5])

    # some more data points
    more_points = np.array([  106,   138,   178,   230,   298,   385,   499,   645,   834,
                              1079,  1395,  1804,  2332,  3016,  3900,  5042,  6520,  8431,
                              10901, 14095])

    mesh_sizes = np.concatenate([mesh_sizes, more_points])
    

    # mesh_sizes = np.array([100, 200])

    bfs = [basisfunctions.Gaussian, basisfunctions.ThinPlateSplines,
           basisfunctions.VolumeSplines, basisfunctions.MultiQuadrics,
           basisfunctions.CompactPolynomialC0, basisfunctions.CompactThinPlateSplineC2]
    RBFs = [rbf.NoneConsistent, rbf.SeparatedConsistent]
    tfs = [testfunctions.Highfreq(), testfunctions.Lowfreq(), testfunctions.Jump(), testfunctions.Constant(1)]
    ms = [4, 6, 8, 12, 16]

    print("Minimum padding needed to avoid boundary effects =", 1/np.min(mesh_sizes) * max(ms))
    
    params = []
    for mesh_size, RBF, bf, tf, m in itertools.product(mesh_sizes, RBFs, bfs, tfs, ms):
        if (not bf.has_shape_param) and (m != ms[0]):
            # skip iteration when the function has no shape parameter and it's not the first iteration in m
            continue

        if not bf.has_shape_param:
            m = 0

        params.append({"mesh_size" : mesh_size, "RBF" : RBF, "basisfunction" : bf, "testfunction" : tf, "m" : m})


    # params = itertools.chain(
        # itertools.product(mesh_sizes, RBFs,
        #                   [basisfunctions.ThinPlateSplines, basisfunctions.VolumeSplines],
        #                   tfs, [0]),
        # itertools.product(mesh_sizes, RBFs,
        #                   [basisfunctions.Gaussian],
        #                   tfs, [4, 6, 8, 10, 14]),
        # itertools.product(mesh_sizes, RBFs,
                          # [basisfunctions.MultiQuadrics],
                          # tfs, [0.1, 0.5, 1, 1.5])
    # )

    try:
        df = pd.read_csv("h_convergence.csv", index_col = "h")
        print("Read in data set of size ", len(df))
        params = filter_existing(params, df)
    except OSError:
        df = pd.DataFrame()
        print("Created new data set.")
        
    
    global runTotal
    runTotal = len(params)

    global runCounter
    runCounter = multiprocessing.Value("i", 0) # i is type id for integer
    
    chunks = [params[i:i + chunk_size] for i in range(0, len(params), chunk_size)]

    for chunk in chunks:
        results = []
        
        if parallel:
            with concurrent.futures.ProcessPoolExecutor(max_workers = workers) as executor:
                results = executor.map(unpack_args_wrapper, chunk)
        else:
            for p in chunk:
                results.append(kernel(**p))

        print("Chunk computed, writing...")

        df = df.append(pd.DataFrame(list(results)).set_index("h"))
        write(df, writeCSV)

    write(df, writeCSV) # write out, also if we filtered everything
    print(df)
            epsilon = np.sqrt(-np.log(1e-9)) / (m * s)
        else:
            epsilon = m * s
        BFs.append(bf(shape_parameter=epsilon))
        epses.append(epsilon)

    print("max h =", max(spaces), ", min h =", min(spaces), ", max eps =",
          max(epses), ", min eps =", min(epses))
    return BFs


test_mesh = np.linspace(0, 1, 5000)

ms = [4, 6, 8, 10, 12]
tf_const = testfunctions.Constant(1)
tf_hf = testfunctions.Highfreq()
BF = Gaussian

df = pd.DataFrame()

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

for order, m in itertools.product(orders, ms):
    gc_points = GaussChebyshev_1D(order=order,
                                  element_size=element_size,
                                  domain_size=1,
                                  domain_start=0)
    print("Order =", order, ", elementSize =", element_size, "mesh size =",
          len(gc_points), ", m =", m)
    spacing = mesh.spacing(gc_points)
예제 #3
0
파일: fitted.py 프로젝트: floli/PyRBF
""" Compares polynomial with increasing degree on a number of test functions on a normal sized test mesh and a slighty enlarged one. """

import numpy as np, matplotlib.pyplot as plt, pandas as pd
import basisfunctions, rbf, testfunctions
from mesh import GaussChebyshev_1D
from basisfunctions import Gaussian

norm = lambda x: np.linalg.norm(x, ord=np.inf)

BF = basisfunctions.Gaussian

tfs = [
    testfunctions.Highfreq(),
    testfunctions.Lowfreq(),
    testfunctions.Constant(1),
    testfunctions.Jump()
]

in_mesh = np.linspace(0, 1, 100)
test_mesh0 = np.linspace(0, 1, 20000)
test_mesh1 = np.linspace(-0.1, 1.1, 20000)

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

cols = [
    str(tf) + "_" + l for l in ["InfError", "InfErrorLargerMesh"] for tf in tfs
]

df = pd.DataFrame(columns=["degree"] + cols, dtype=np.float64)
df = df.set_index("degree")
예제 #4
0
def plot_rmse_cond():
    """ Plots over a range of shape parameters. """
    in_mesh = np.linspace(1, 4, 192)
    # in_mesh = GaussChebyshev(12, 0.25, 4, 1)
    tf = testfunctions.Highfreq()
    in_vals = tf(in_mesh)
    test_mesh = np.linspace(1, 4, 2000)

    ms = np.linspace(1, 20, 50)

    separated = []
    integrated = []
    no_pol = []
    separated_res = []
    no_pol_res = []
    sep_fitresc = []

    for m in ms:
        print("Working on m =", m)
        bf = Gaussian(Gaussian.shape_param_from_m(m, in_mesh))

        separated.append(SeparatedConsistent(bf, in_mesh, in_vals, False))
        integrated.append(IntegratedConsistent(bf, in_mesh, in_vals))
        no_pol.append(NoneConsistent(bf, in_mesh, in_vals, False))
        separated_res.append(SeparatedConsistent(bf, in_mesh, in_vals, True))
        no_pol_res.append(NoneConsistent(bf, in_mesh, in_vals, True))
        sep_fitresc.append(
            SeparatedConsistentFitted(bf, in_mesh, in_vals, True))

    fig, ax1 = plt.subplots()
    ax1.loglog([i.RMSE(tf, test_mesh) for i in no_pol],
               [i.condC for i in no_pol],
               label="No Polynomial")
    ax1.loglog([i.RMSE(tf, test_mesh) for i in integrated],
               [i.condC for i in integrated],
               label="Integrated Polynomial")
    ax1.loglog([i.RMSE(tf, test_mesh) for i in separated],
               [i.condC for i in separated],
               label="Separated Polynomial")

    ax1.loglog([i.RMSE(tf, test_mesh) for i in no_pol_res],
               [i.condC for i in no_pol_res],
               label="No polynomial, rescaled")
    ax1.loglog([i.RMSE(tf, test_mesh) for i in separated_res],
               [i.condC for i in separated_res],
               label="Separated polynomial, rescaled")
    ax1.loglog([i.RMSE(tf, test_mesh) for i in sep_fitresc],
               [i.condC for i in sep_fitresc],
               label="Separated, fitted, rescaled")

    ax1.set_ylabel("Condition")
    ax1.set_xlabel("RMSE")

    ax1.annotate("better",
                 weight="bold",
                 size="large",
                 xycoords="axes fraction",
                 xy=(0.05, 0.05),
                 xytext=(0.25, 0.25),
                 arrowprops={
                     "width": 5,
                     "headwidth": 10,
                     "shrink": 30
                 })

    ax1.set_xlim([5e-8, 0.1])

    ax1.legend()
    # plt.grid()
    # plt.savefig("rmse_cond.pdf")
    plt.show()
예제 #5
0
import numpy as np, matplotlib.pyplot as plt, pandas as pd
import rbf, rbf_qr, basisfunctions, testfunctions, mesh
from ipdb import set_trace

norm = lambda x: np.linalg.norm(x, ord=np.inf)


def func(x):
    return np.exp(-np.abs(x - 3)**2) + 2


# tf = testfunctions.Highfreq()
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]