def dam_break_test(plot, show, save): model = sw.Model() model.plot = plot model.show_plot = show model.save_plot = save # mesh model.L_ = 1.0 model.x_N_ = 1.0 # current properties model.Fr_ = 1.19 model.beta_ = 0.0 # time stepping model.adapt_timestep = False model.adapt_initial_timestep = False model.cfl = Constant(0.5) # define stabilisation parameters (0.1,0.1,0.1,0.1) found to work well for t=10.0 model.q_b = Constant(0.0) model.h_b = Constant(0.0) model.phi_b = Constant(0.0) model.phi_d_b = Constant(0.0) # discretisation model.q_degree = 1 model.h_degree = 1 model.phi_degree = 1 model.phi_d_degree = 1 model.q_disc = "DG" model.h_disc = "DG" model.phi_disc = "DG" model.phi_d_disc = "DG" model.disc = [model.q_disc, model.h_disc, model.phi_disc, model.phi_d_disc] def getError(model): q, h, phi, phi_d, x_N_model, u_N_model = model.w[0].split() Fq = FunctionSpace(model.mesh, model.q_disc, model.q_degree + 2) Fh = FunctionSpace(model.mesh, model.h_disc, model.h_degree + 2) Fphi = FunctionSpace(model.mesh, model.phi_disc, model.phi_degree + 2) Fphi_d = FunctionSpace(model.mesh, model.phi_d_disc, model.phi_d_degree + 2) u_N = model.Fr_ / (1.0 + model.Fr_ / 2.0) h_N = (1.0 / (1.0 + model.Fr_ / 2.0)) ** 2.0 x_N = u_N * model.t x_M = (2.0 - 3.0 * h_N ** 0.5) * model.t x_L = -model.t class u_expression(Expression): def eval(self, value, x): x_gate = x[0] * x_N_model - 1.0 if x_gate <= x_L: value[0] = 0.0 elif x_gate <= x_M: value[0] = 2.0 / 3.0 * (1.0 + x_gate / model.t) else: value[0] = model.Fr_ / (1.0 + model.Fr_ / 2.0) class h_expression(Expression): def eval(self, value, x): x_gate = x[0] * x_N_model - 1.0 if x_gate <= x_L: value[0] = 1.0 elif x_gate <= x_M: value[0] = 1.0 / 9.0 * (2.0 - x_gate / model.t) ** 2.0 else: value[0] = (1.0 / (1.0 + model.Fr_ / 2.0)) ** 2.0 S_q = project(u_expression(), Fq) S_h = project(h_expression(), Fh) E_q = errornorm(q, S_q, norm_type="L2", degree_rise=2) E_h = errornorm(h, S_h, norm_type="L2", degree_rise=2) E_x_N = abs(x_N_model - x_N) E_u_N = abs(u_N_model - u_N) return E_q, E_h, E_x_N, E_u_N # long test T = 0.5 dt = [1e-1, 1e-1 / 2, 1e-1 / 4, 1e-1 / 8, 1e-1 / 16, 1e-1 / 32, 1e-1 / 64, 1e-1 / 128, 1e-1 / 256, 1e-1 / 512] dX = [1.0 / 4, 1.0 / 8, 1.0 / 16, 1.0 / 32, 1.0 / 64] dt = [1e-1 / 32] dX = [1.0 / 16, 1.0 / 32, 1.0 / 64] # quick settings dt = [1e-1 / 64] dX = [1.0 / 16, 1.0 / 32, 1.0 / 64] dX = [1.0 / 64] E = [] for dt_ in dt: E.append([]) for dX_ in dX: print dX_, dt_ model.dX_ = dX_ model.timestep = dt_ model.t = 0.0 model.initialise_function_spaces() model.setup(zero_q=True, dam_break=True) model.error_callback = getError E[-1].append(model.solve(T)) sw_io.write_array_to_file("dam_break.json", E, "w") E = E[0] print ("R = 0.00 0.00 0.00 0.00 0.00 E = %.2e %.2e %.2e %.2e %.2e" % (E[0][0], E[0][1], E[0][2], E[0][3])) for i in range(1, len(E)): rh = np.log(E[i][0] / E[i - 1][0]) / np.log(dX[i] / dX[i - 1]) rq = np.log(E[i][1] / E[i - 1][1]) / np.log(dX[i] / dX[i - 1]) rx = np.log(E[i][2] / E[i - 1][2]) / np.log(dX[i] / dX[i - 1]) ru = np.log(E[i][3] / E[i - 1][3]) / np.log(dX[i] / dX[i - 1]) print ( "R = %-5.2f %-5.2f %-5.2f %-5.2f %-5.2f E = %.2e %.2e %.2e %.2e %.2e" % (rh, rq, rx, ru, E[i][0], E[i][1], E[i][2], E[i][3]) )
def __call__(self, value): try: print "\n* * * Adjoint and optimiser time taken = {}".format(toc()) list_timings(True) except: pass #### initial condition dump hack #### phi_ic = value[0].vector().array() phi = phi_ic.copy() for i in range(len(model.mesh.cells())): j = i*2 phi[j] = phi_ic[-(j+2)] phi[j+1] = phi_ic[-(j+1)] phi_ic = phi sw_io.write_array_to_file('phi_ic_adj{}_latest.json'.format(job),phi_ic,'w') sw_io.write_array_to_file('phi_ic_adj{}.json'.format(job),phi_ic,'a') try: h_ic = value[1].vector().array() sw_io.write_array_to_file('h_ic_adj{}_latest.json'.format(job),h_ic,'w') sw_io.write_array_to_file('h_ic_adj{}.json'.format(job),h_ic,'a') except: pass try: q_a_ = value[2]((0,0)); q_pa_ = value[3]((0,0)); q_pb_ = value[4]((0,0)) sw_io.write_q_vals_to_file('q_ic_adj{}_latest.json'.format(job),q_a_,q_pa_,q_pb_,'w') sw_io.write_q_vals_to_file('q_ic_adj{}.json'.format(job),q_a_,q_pa_,q_pb_,'a') except: pass tic() print "\n* * * Computing forward model" func_value = (super(MyReducedFunctional, self)).__call__(value) # model.setup(h_ic = value[1], phi_ic = value[0], q_a = value[2], q_pa = value[3], q_pb = value[4]) # model.solve(T = options.T) # func_value = adjointer.evaluate_functional(self.functional, 0) print "* * * Forward model: time taken = {}".format(toc()) list_timings(True) # sys.exit() j = self.scale * func_value j_log.append(j) sw_io.write_array_to_file('j_log{}.json'.format(job), j_log, 'w') (fwd_var, output) = adjointer.get_forward_solution(adjointer.equation_count - 1) var = adjointer.get_variable_value(fwd_var) y, q, h, phi, phi_d, x_N, u_N = sw_io.map_to_arrays(var.data, model.y, model.mesh) sw_io.write_array_to_file('phi_d_adj{}_latest.json'.format(job),phi_d,'w') sw_io.write_array_to_file('phi_d_adj{}.json'.format(job),phi_d,'a') # from IPython import embed; embed() plotter.update_plot(phi_ic, phi_d, y, x_N, j) print "* * * J = {}".format(j) tic() return func_value
def similarity_test(plot, show, save): model = sw.Model() model.plot = plot model.show_plot = show model.save_plot = save # mesh model.dX_ = 5.0e-3 model.L_ = 1.0 # current properties model.Fr_ = 1.19 model.beta_ = 0.0 # time stepping model.timestep = 5e-4 # model.dX_/500.0 model.adapt_timestep = False model.adapt_initial_timestep = False model.cfl = Constant(0.5) # define stabilisation parameters (0.1,0.1,0.1,0.1) found to work well for t=10.0 model.q_b = Constant(0.0) model.h_b = Constant(0.0) model.phi_b = Constant(0.0) model.phi_d_b = Constant(0.0) # discretisation model.q_degree = 1 model.h_degree = 1 model.phi_degree = 1 model.phi_d_degree = 1 model.q_disc = "DG" model.h_disc = "DG" model.phi_disc = "DG" model.phi_d_disc = "DG" model.disc = [model.q_disc, model.h_disc, model.phi_disc, model.phi_d_disc] w_ic_e = [ "(2./3.)*K*pow(t,-1./3.)*x[0]*(4./9.)*pow(K,2.0)*pow(t,-2./3.)*(1./pow(Fr,2.0) - (1./4.) + (1./4.)*pow(x[0],2.0))", "(4./9.)*pow(K,2.0)*pow(t,-2./3.)*(1./pow(Fr,2.0) - (1./4.) + (1./4.)*pow(x[0],2.0))", "(4./9.)*pow(K,2.0)*pow(t,-2./3.)*(1./pow(Fr,2.0) - (1./4.) + (1./4.)*pow(x[0],2.0))", "0.0", "K*pow(t, (2./3.))", "(2./3.)*K*pow(t,-1./3.)", ] def getError(model): Fq = FunctionSpace(model.mesh, model.q_disc, model.q_degree + 2) Fh = FunctionSpace(model.mesh, model.h_disc, model.h_degree + 2) Fphi = FunctionSpace(model.mesh, model.phi_disc, model.phi_degree + 2) Fphi_d = FunctionSpace(model.mesh, model.phi_d_disc, model.phi_d_degree + 2) K = ((27.0 * model.Fr_ ** 2.0) / (12.0 - 2.0 * model.Fr_ ** 2.0)) ** (1.0 / 3.0) S_q = project(Expression(w_ic_e[0], K=K, Fr=model.Fr_, t=model.t, degree=5), Fq) S_h = project(Expression(w_ic_e[1], K=K, Fr=model.Fr_, t=model.t, degree=5), Fh) S_phi = project(Expression(w_ic_e[2], K=K, Fr=model.Fr_, t=model.t, degree=5), Fphi) q, h, phi, phi_d, x_N, u_N = model.w[0].split() E_q = errornorm(q, S_q, norm_type="L2", degree_rise=2) E_h = errornorm(h, S_h, norm_type="L2", degree_rise=2) E_phi = errornorm(phi, S_phi, norm_type="L2", degree_rise=2) E_x_N = abs(x_N - K * model.t ** (2.0 / 3.0)) E_u_N = abs(u_N - (2.0 / 3.0) * K * model.t ** (-1.0 / 3.0)) return E_q, E_h, E_phi, 0.0, E_x_N, E_u_N # long test T = 1.5 dt = [1e-1, 1e-1 / 2, 1e-1 / 4, 1e-1 / 8, 1e-1 / 16, 1e-1 / 32, 1e-1 / 64, 1e-1 / 128, 1e-1 / 256, 1e-1 / 512] dX = [1.0 / 4, 1.0 / 8, 1.0 / 16, 1.0 / 32, 1.0 / 64] dt = [1e-1 / 32] dX = [1.0 / 16, 1.0 / 32, 1.0 / 64] # quick settings T = 0.52 dt = [1e-1 / 512] dX = [1.0 / 4, 1.0 / 8, 1.0 / 16] E = [] for dt_ in dt: E.append([]) for dX_ in dX: print dX_, dt_ model.dX_ = dX_ model.timestep = dt_ model.t = 0.5 model.initialise_function_spaces() w_ic_E = Expression( (w_ic_e[0], w_ic_e[1], w_ic_e[2], w_ic_e[3], w_ic_e[4], w_ic_e[5]), K=((27.0 * model.Fr_ ** 2.0) / (12.0 - 2.0 * model.Fr_ ** 2.0)) ** (1.0 / 3.0), Fr=model.Fr_, t=model.t, element=model.W.ufl_element(), degree=5, ) w_ic = project(w_ic_E, model.W) model.setup(w_ic=w_ic, similarity=True) model.error_callback = getError E[-1].append(model.solve(T)) sw_io.write_array_to_file("similarity_convergence.json", E, "w") E = E[0] print ( "R = 0.00 0.00 0.00 0.00 0.00 E = %.2e %.2e %.2e %.2e %.2e" % (E[0][0], E[0][1], E[0][2], E[0][4], E[0][5]) ) for i in range(1, len(E)): rh = np.log(E[i][0] / E[i - 1][0]) / np.log(dX[i] / dX[i - 1]) rphi = np.log(E[i][1] / E[i - 1][1]) / np.log(dX[i] / dX[i - 1]) rq = np.log(E[i][2] / E[i - 1][2]) / np.log(dX[i] / dX[i - 1]) rx = np.log(E[i][4] / E[i - 1][4]) / np.log(dX[i] / dX[i - 1]) ru = np.log(E[i][5] / E[i - 1][5]) / np.log(dX[i] / dX[i - 1]) print ( "R = %-5.2f %-5.2f %-5.2f %-5.2f %-5.2f E = %.2e %.2e %.2e %.2e %.2e" % (rh, rphi, rq, rx, ru, E[i][0], E[i][1], E[i][2], E[i][4], E[i][5]) )
T = 75.0 if (options.T): T = options.T model.solve(T) elif job == 1: adjoint_setup(model) model.initialise_function_spaces() phi_ic = project(Expression('1.0 - 0.1*cos(pi*x[0])'), model.phi_FS) model.setup(phi_ic = phi_ic) y, q, h, phi, phi_d, x_N, u_N = sw_io.map_to_arrays(model.w[0], model.y, model.mesh) sw_io.write_array_to_file('phi_ic.json', phi, 'w') model.solve(T = options.T) y, q, h, phi, phi_d, x_N, u_N = sw_io.map_to_arrays(model.w[0], model.y, model.mesh) sw_io.write_array_to_file('deposit_data.json', phi_d, 'w') sw_io.write_array_to_file('runout_data.json', [x_N], 'w') elif job == 4: adjoint_setup(model) model.initialise_function_spaces() # phi_ic = project(Expression('1.0 - (0.8*cos(pow(x[0] +0.1,4.0)*pi))'), model.phi_FS) phi_ic = project(Expression('1.0'), model.phi_FS)