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]
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)
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() # Posterior