Esempio n. 1
0
def gen_avg_rom_dataset(dataset_size, resolution=40):
    V = get_space(resolution)
    chol = make_cov_chol(V)
    z = Function(V)
    solver = Fin(V)
    phi = np.loadtxt('data/basis_nine_param.txt', delimiter=",")
    qoi_errors = np.zeros((dataset_size, 5))

    # TODO: Needs to be fixed for higher order functions
    z_s = np.zeros((dataset_size, V.dim()))

    for i in range(dataset_size):
        norm = np.random.randn(len(chol))
        nodal_vals = np.exp(0.5 * chol.T @ norm)
        z.vector().set_local(nodal_vals)
        z_s[i, :] = nodal_vals
        A_r, B_r, C_r, x_r, y_r = solver.averaged_forward(z, phi)
        x, y, A, B, C = solver.forward(z)
        qoi = solver.qoi_operator(x)
        qoi_r = solver.reduced_qoi_operator(x_r)
        qoi_errors[i, :] = qoi - qoi_r

    if (dataset_size > 1000):
        np.savetxt('data/z_avg_tr.txt', z_s, delimiter=",")
        np.savetxt('data/errors_avg_tr.txt', qoi_errors, delimiter=",")

    if (dataset_size < 400):
        np.savetxt('data/z_avg_eval.txt', z_s, delimiter=",")
        np.savetxt('data/errors_avg_eval.txt', qoi_errors, delimiter=",")
    return (z_s, qoi_errors)
Esempio n. 2
0
solver = Fin(V)
phi = np.loadtxt('data/basis_five_param.txt', delimiter=",")

# Obtain synthetic data
chol = make_cov_chol(V, length=0.8)
k_true = dl.Function(V)
norm = np.random.randn(len(chol))
nodal_vals = np.exp(0.5 * chol.T @ norm)
k_true.vector().set_local(nodal_vals)

w, y, A, B, C = solver.forward(k_true)
data = solver.qoi_operator(w)

# Comparing reduced and full
w, y, A, B, C = solver.forward(k_true)
A_r, B_r, C_r, w_r, y_r = solver.averaged_forward(k_true, phi)
k_true_averaged = solver.nine_param_to_function(solver.subfin_avg_op(k_true))

p = dl.plot(k_true_averaged)
plt.colorbar(p)
plt.show()

fig, (ax1, ax2) = plt.subplots(1, 2)
plt.sca(ax1)
p = dl.plot(k_true)
plt.sca(ax2)
dl.plot(k_true_averaged)
plt.colorbar(p)
plt.show()

fig, (ax1, ax2) = plt.subplots(1, 2)
#  phi = phi[:,0:10]
solver = Fin(V)
avgs_f = np.zeros((len(z_val), 5))
avgs_r = np.zeros((len(z_val), 5))
avgs_d = np.zeros((len(z_val), 5))
avgs_c = np.zeros((len(z_val), 5))
err_pred = np.zeros((len(z_val), 5))
z = Function(V)
err_pred = model.predict(z_val)
print("Average validation error in pred: {}".format(
    np.average(np.divide(np.abs(errors_val - err_pred), np.abs(errors_val)))))

for i in range(len(z_val)):
    nodal_vals = z_val[i, :]
    z.vector().set_local(nodal_vals)
    _, _, _, x_r, y_r = solver.averaged_forward(z, phi)
    x, y, A, B, C = solver.forward(z)
    avgs_f[i] = solver.qoi_operator(x)
    avgs_r[i] = solver.reduced_qoi_operator(x_r)
    avgs_d[i] = avgs_r[i] + err_pred[i]

print("Average relative error in tilde: {}".format(
    np.average(np.divide(np.abs(avgs_f - avgs_d), np.abs(avgs_f)))))

for i in range(5):
    plt.cla()
    plt.clf()
    plt.plot(errors_val[:100, i], color='#36c7d6')
    plt.plot(err_pred[:100, i], 'k--x')
    plt.xlabel("Validation Example Index", fontsize=10)
    plt.ylabel("Error in average temperature", fontsize=10)