Esempio n. 1
0
class FOM_forward(mm.PyModPiece):
    """
    Solves the thermal fin steady state problem with
    a full order model
    """
    def __init__(self, resolution=40, out_type="total_avg"):
        """ 
        INPUTS:
     
        """
        V = get_space(resolution)
        dofs = len(V.dofmap().dofs())
        self.solver = Fin(V)
        self.out_type = out_type

        if out_type == "total_avg":
            out_dim = 1
        elif out_type == "subfin_avg":
            out_dim = 5
        elif out_type == "rand_pt":
            out_dim = 1
        elif out_type == "rand_pts":
            out_dim = 5
        mm.PyModPiece.__init__(self, [5], [out_dim])

    def EvaluateImpl(self, inputs):
        """
        Performs the forward solve and returns observations.
        
        """
        z = inputs[0]

        x, y, A, B, C = self.solver.forward_five_param(z)
        output = self.solver.qoi_operator(x)
        self.outputs = [output]
Esempio n. 2
0
def gen_five_param_subfin_avg(dataset_size, resolution=40):
    V = get_space(resolution)
    z_s = np.random.uniform(0.1, 1, (dataset_size, 5))
    phi = np.loadtxt('data/basis_five_param.txt', delimiter=",")
    phi = phi[:, 0:10]
    solver = Fin(V)
    errors = np.zeros((dataset_size, 5))
    avgs = np.zeros((dataset_size, 5))
    avgs_r = np.zeros((dataset_size, 5))

    for i in range(dataset_size):
        w, y, A, B, C = solver.forward_five_param(z_s[i, :])
        avgs[i] = solver.qoi_operator(w)
        psi = np.dot(A, phi)
        A_r, B_r, C_r, x_r, y_r = solver.reduced_forward(A, B, C, psi, phi)
        avgs_r[i] = solver.reduced_qoi_operator(x_r)
        errors[i] = avgs[i] - avgs_r[i]

    return (z_s, errors)
Esempio n. 3
0
def generate_five_param_np(dataset_size, resolution=40):
    V = get_space(resolution)
    z_s = np.random.uniform(0.1, 1, (dataset_size, 5))
    phi = np.loadtxt('data/basis_five_param.txt', delimiter=",")
    phi = phi[:, 0:10]
    solver = Fin(V)
    errors = np.zeros((dataset_size, 1))
    y_s = np.zeros((dataset_size, 1))
    y_r_s = np.zeros((dataset_size, 1))

    for i in range(dataset_size):
        w, y, A, B, C = solver.forward_five_param(z_s[i, :])
        y_s[i][0] = y
        psi = np.dot(A, phi)
        A_r, B_r, C_r, x_r, y_r = solver.reduced_forward(A, B, C, psi, phi)
        y_r_s[i][0] = y_r
        errors[i][0] = y - y_r

    return (z_s, errors)
Esempio n. 4
0
def generate_five_param(dataset_size, resolution=40):
    V = get_space(resolution)
    dofs = len(V.dofmap().dofs())

    # TODO: Improve this by using mass matrix covariance. Bayesian prior may work well too
    z_s = np.random.uniform(0.1, 1, (dataset_size, 5))
    phi = np.loadtxt('data/basis_five_param.txt', delimiter=",")
    phi = phi[:, 0:20]
    solver = Fin(V)
    errors = np.zeros((dataset_size, 1))

    for i in range(dataset_size):
        w, y, A, B, C = solver.forward_five_param(z_s[i, :])
        psi = np.dot(A, phi)
        A_r, B_r, C_r, x_r, y_r = solver.reduced_forward(A, B, C, psi, phi)
        errors[i][0] = y - y_r

    #  np.savetxt('data/z_s_eval.txt', z_s, delimiter=",")
    #  np.savetxt('data/errors_eval.txt', errors, delimiter=",")
    dataset = tf.data.Dataset.from_tensor_slices((z_s, errors))

    return dataset
        + Rectangle(Point(3.5, 3.75), Point(6.0, 4.0)) \

mesh = generate_mesh(geometry, 40)

V = FunctionSpace(mesh, 'CG', 1)
dofs = len(V.dofmap().dofs())
solver = Fin(V)

##########################################################3
# Basis initialization with dummy solves and POD
##########################################################3
samples = 10
Y = np.zeros((samples, dofs))
for i in range(0, samples):
    k = np.random.uniform(0.1, 1.0, 5)
    w = solver.forward_five_param(k)[0]
    Y[i, :] = w.vector()[:]

K = np.dot(Y, Y.T)

# Initial basis vectors computed using proper orthogonal decomposition
e, v = np.linalg.eig(K)

basis_size = 5
U = np.zeros((basis_size, dofs))
for i in range(basis_size):
    e_i = v[:, i].real
    U[i, :] = np.sum(np.dot(np.diag(e_i), Y), 0)

basis = U.T
Esempio n. 6
0
sys.path.insert(0,'/home/fenics/Installations/MUQ_INSTALL/lib')
import pymuqModeling as mm # Needed for Gaussian distribution
import pymuqApproximation as ma # Needed for Gaussian processes
import pymuqSamplingAlgorithms as ms # Needed for MCMC

resolution = 40
r_fwd = ROM_forward(resolution, out_type="subfin_avg")
d_fwd = DL_ROM_forward(resolution, out_type="subfin_avg")
f_fwd = FOM_forward(resolution, out_type="subfin_avg")

#z_true = np.random.uniform(0.1,1, (1,5))
z_true = np.array([[0.41126864, 0.61789679, 0.75873243, 0.96527541, 0.22348076]])

V = get_space(resolution)
full_solver = Fin(V)
w, y, A, B, C = full_solver.forward_five_param(z_true[0,:])
qoi = full_solver.qoi_operator(w)
obsData = qoi

def MCMC_sample(fwd):
    # Define prior
    logPriorMu = 0.5*np.ones(5)
    logPriorCov = 0.5*np.eye(5)

    logPrior = mm.Gaussian(logPriorMu, logPriorCov).AsDensity()

    # Likelihood
    noiseVar = 1e-4
    noiseCov = noiseVar*np.eye(obsData.size)
    likelihood = mm.Gaussian(obsData, noiseCov).AsDensity()
        + Rectangle(Point(0.0, 3.75), Point(2.5, 4.0)) \
        + Rectangle(Point(3.5, 0.75), Point(6.0, 1.0)) \
        + Rectangle(Point(3.5, 1.75), Point(6.0, 2.0)) \
        + Rectangle(Point(3.5, 2.75), Point(6.0, 3.0)) \
        + Rectangle(Point(3.5, 3.75), Point(6.0, 4.0)) \

mesh = generate_mesh(geometry, 40)

V = FunctionSpace(mesh, 'CG', 1)
dofs = len(V.dofmap().dofs())
f = Fin(V)

basis = np.loadtxt('../data/basis_five_param.txt', delimiter=",")
basis = basis[:, 0:10]
k_s = np.random.uniform(0.1, 1.0, 5)
w, y, A, B, C = f.forward_five_param(k_s)
m = f.five_param_to_function(k_s)
p = plot(m, title="Conductivity")
plt.colorbar(p)
plt.show()
p = plot(w, title="Temperature")
plt.colorbar(p)
plt.show()
A_r, B_r, C_r, x_r, y_r = f.reduced_forward(A, B, C, np.dot(A, basis), basis)
x_tilde = np.dot(basis, x_r)
x_tilde_f = Function(V)
x_tilde_f.vector().set_local(x_tilde)
p = plot(x_tilde_f, title="Temperature reduced")
plt.colorbar(p)
plt.show()