def generate(dataset_size, resolution=40): ''' Create a tensorflow dataset where the features are thermal conductivity parameters and the labels are the differences in the quantity of interest between the high fidelity model and the reduced order model (this is the ROM error) Arguments: dataset_size - number of feature-label pairs resolution - finite element mesh resolution for the high fidelity model Returns: dataset - Tensorflow dataset created from tensor slices ''' 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, dofs)) phi = np.loadtxt('data/basis.txt', delimiter=",") solver = Fin(V) errors = np.zeros((dataset_size, 1)) m = Function(V) for i in range(dataset_size): m.vector().set_local(z_s[i, :]) w, y, A, B, C = solver.forward(m) 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 dataset = tf.data.Dataset.from_tensor_slices((z_s, errors)) return dataset
class FinInput: ''' A class to create a thermal fin instance with Tensorflow input functions ''' def __init__(self, batch_size, resolution): self.resolution = resolution self.V = get_space(resolution) self.dofs = len(self.V.dofmap().dofs()) self.phi = np.loadtxt('data/basis_five_param.txt', delimiter=",") self.batch_size = batch_size self.solver = Fin(self.V) def train_input_fn(self): params = np.random.uniform(0.1, 1, (self.batch_size, self.dofs)) errors = np.zeros((self.batch_size, 1)) for i in range(self.batch_size): m = Function(self.V) m.vector().set_local(params[i, :]) w, y, A, B, C = self.solver.forward(m) psi = np.dot(A, self.phi) A_r, B_r, C_r, x_r, y_r = self.solver.reduced_forward( A, B, C, psi, self.phi) errors[i][0] = y - y_r return ({ 'x': tf.convert_to_tensor(params) }, tf.convert_to_tensor(errors)) def eval_input_fn(self): params = np.random.uniform(0.1, 1, (self.batch_size, self.dofs)) errors = np.zeros((self.batch_size, 1)) for i in range(self.batch_size): m = Function(self.V) m.vector().set_local(params[i, :]) w, y, A, B, C = self.solver.forward(m) psi = np.dot(A, self.phi) A_r, B_r, C_r, x_r, y_r = self.solver.reduced_forward( A, B, C, psi, self.phi) errors[i][0] = y - y_r return ({ 'x': tf.convert_to_tensor(params) }, tf.convert_to_tensor(errors))
def generate_and_save_dataset(dataset_size, resolution=40): V = get_space(resolution) dofs = len(V.dofmap().dofs()) z_s = np.random.uniform(0.1, 1, (dataset_size, dofs)) phi = np.loadtxt('data/basis.txt', delimiter=",") solver = Fin(V) errors = np.zeros((dataset_size, 1)) m = Function(V) for i in range(dataset_size): m.vector().set_local(z_s[i, :]) w, y, A, B, C = solver.forward(m) 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_train.txt', z_s, delimiter=",") np.savetxt('data/errors_train.txt', errors, delimiter=",")
+ Rectangle(Point(0.0, 2.75), Point(2.5, 3.0)) \ + 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) plot(mesh) plt.show() V = FunctionSpace(mesh, 'CG', 1) dofs = len(V.dofmap().dofs()) print("DOFS: {}".format(dofs)) # Pick a more interesting conductivity to see what happens # m = Function(V) # m = interpolate(Expression("2.0*exp(-(pow(x[0] - 0.5, 2) + pow(x[1]-0.5, 2)) / 0.02)", degree=2), V) m = interpolate(Expression("5- x[1]", degree=2), V) solver = Fin(V) w = solver.forward(m)[0] fig = plt.figure() p = plot(m, title="Conductivity") plt.colorbar(p) plt.show() plt.figure() p = plot(w, title="Temperature") plt.colorbar(p) plt.show()
+ Rectangle(Point(0.0, 2.75), Point(2.5, 3.0)) \ + 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.txt', delimiter=",") m = interpolate(Expression("2*x[1] + 1.0", degree=2), V) w, y, A, B, C = f.forward(m, V) 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() print("Reduced system error: {}".format(np.linalg.norm(y - y_r)))
Expression( "0.1 + exp(-(pow(x[0] - 0.5, 2) + pow(x[1], 2)) / 0.01)", degree=2), V) elif i == 1: m = interpolate(Expression("2*x[0] + 0.1", degree=2), V) elif i == 2: m = interpolate(Expression("1 + sin(x[0])* sin(x[0])", degree=2), V) elif i == 3: m = interpolate(Expression("1 + sin(x[1])* sin(x[1])", degree=2), V) elif i == 4: m = interpolate(Expression("1 + sin(x[0])* sin(x[1])", degree=2), V) else: m = Function(V) m.vector().set_local(np.random.uniform(0.1, 10.0, dofs)) w = solver.forward(m)[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
(1, len(state_obs_test))) }) if run_options.use_full_domain_data == 1 or run_options.use_bnd_data == 1: # No state prediction if the truncation layer only consists of the observations state_pred = sess.run(NN.encoded, feed_dict={ NN.parameter_input_tf: parameter_test.reshape( (1, len(parameter_test))) }) ############## # Plotting # ############## #=== Plotting test parameter and test state ===# parameter_test_dl = solver.nine_param_to_function(parameter_test.T) state_test_dl, _ = solver.forward( parameter_test_dl) # generate true state for comparison state_test = state_test_dl.vector().get_local() p_test_fig = dl.plot(parameter_test_dl) p_test_fig.ax.set_title('True Parameter', fontsize=18) plt.savefig(run_options.figures_savefile_name_parameter_test, dpi=300) print('Figure saved to ' + run_options.figures_savefile_name_parameter_test) plt.show() s_test_fig = dl.plot(state_test_dl) s_test_fig.ax.set_title('True State', fontsize=18) plt.savefig(run_options.figures_savefile_name_state_test, dpi=300) print('Figure saved to ' + run_options.figures_savefile_name_state_test) plt.show()