def error_majorant_distribution_nd(mesh, domain, dim, u_e, grad_u_e, u, V_exact, y, f, lmbd, beta): cell_num = mesh.num_cells() #e_distr = postprocess.allocate_array(cell_num) #m_distr = postprocess.allocate_array(cell_num) ed_distr = postprocess.allocate_array(cell_num) md_distr = postprocess.allocate_array(cell_num) # Define error and residuals r_d = abs(Grad(u, dim) - y) r_f = abs(f + Div(y, dim) - lmbd * u) #C_FD = calculate_CF_of_domain(domain, dim) u_ve = interpolate(u, V_exact) u_exact_ve = interpolate(u_e, V_exact) e = u_ve - u_exact_ve #w_opt = (C_FD ** 2) * (1 + beta) / (beta + (C_FD ** 2) * (1 + beta) * lmbd) # Project UFL forms on the high-order functional space to obtain functions #ed = project(sqrt(inner(Grad(e, dim), Grad(e, dim))), V_exact) #md = project(sqrt(inner(r_d, r_d)), V_exact) ed = project(inner(Grad(e, dim), Grad(e, dim)), V_exact) md = project(inner(r_d, r_d), V_exact) #ed_test = assemble(sqrt(inner(Grad(e, dim), Grad(e, dim))) * dx) #md_test = assemble(sqrt(inner(r_d, r_d)) * dx) #e = project(sqrt(inner(Grad(e, dim), Grad(e, dim)) + lmbd * inner(e, e)), V_exact) #m = project(sqrt((1 + beta) * inner(r_d, r_d) + w_opt * inner(r_f, r_f)), V_exact) dofmap = V_exact.dofmap() scheme_order = 4 gauss = integration.SpaceIntegrator(scheme_order, dim) for c in cells(mesh): verts = dofmap.tabulate_coordinates(c) x_n = verts[0:dim + 1, :] x_n_transpose = x_n.transpose() matrix = postprocess.allocate_array_2d(dim + 1, dim + 1) matrix[0:dim, 0:dim + 1] = x_n_transpose matrix[dim, 0:dim + 1] = numpy.array([1.0 for i in range(dim + 1)]) meas = abs(numpy.linalg.det(matrix)) ed_distr[c.index()] = gauss.integrate(ed, meas, x_n_transpose) md_distr[c.index()] = gauss.integrate(md, meas, x_n_transpose) #e_distr[c.index()] = gauss.integrate(e, meas, x_n_transpose) #m_distr[c.index()] = gauss.integrate(m, meas, x_n_transpose) print 'sum of m_cells', numpy.sum(md_distr) print 'sum of e_cells', numpy.sum(ed_distr) return ed_distr, md_distr
def get_2d_slice_of_3d_function_on_Oz(mesh, u, T, dim, v_degree): # create a boundary mesh bmesh = BoundaryMesh(mesh, "exterior") cell_func = CellFunction('size_t', bmesh, 0) coordinates = bmesh.coordinates() z_indeces = postprocess.allocate_array(dim) for cell in cells(bmesh): indx = 0 for vertex in vertices(cell): z_indeces[indx] = coordinates[vertex.index()][dim - 1] indx += 1 #print "Vertex index with coordinates", vertex.index(), coordinates[vertex.index()][dim-1] if (dim == 3 and near(z_indeces[0], T) and near(z_indeces[1], T) and near(z_indeces[2], T)) or (dim == 2 and near( z_indeces[0], T) and near(z_indeces[1], T)): #print "right cell", cell.index() cell_func[cell] = 1 submesh = SubMesh(bmesh, cell_func, 1) # create a FunctionSpace on the submesh- Vs = FunctionSpace(submesh, "Lagrange", v_degree) us = interpolate(u, Vs) return us, submesh
def error_majorant_distribution_nd(mesh, dim, V_exact, var_grad_e, var_delta_e, var_m_d, var_m_f_w_opt, beta, C_FD): cell_num = mesh.num_cells() ed_distr = postprocess.allocate_array(cell_num) delta_e_distr = postprocess.allocate_array(cell_num) md_distr = postprocess.allocate_array(cell_num) maj_distr = postprocess.allocate_array(cell_num) # Project UFL forms on the high-order functional space to obtain functions ed = project((var_grad_e), V_exact) delta_e = project(var_delta_e, V_exact) md = project((var_m_d), V_exact) mf_wopt = project(var_m_f_w_opt, V_exact) scheme_order = 4 gauss = integration.SpaceIntegrator(scheme_order, dim) for c in cells(mesh): # Obtaining the coordinates of the vertices in the cell verts = c.get_vertex_coordinates().reshape(dim + 1, dim) x_n_transpose = verts.T # Calculating the area of the cell matrix = postprocess.allocate_array_2d(dim + 1, dim + 1) matrix[0:dim, 0:dim + 1] = x_n_transpose matrix[dim, 0:dim + 1] = numpy.array([1.0 for i in range(dim + 1)]) # print "matrix = ", matrix meas = abs(numpy.linalg.det(matrix)) # Integrating over the cell ed_distr[c.index()] = gauss.integrate(ed, meas, x_n_transpose) delta_e_distr[c.index()] = gauss.integrate(delta_e, meas, x_n_transpose) md_distr[c.index()] = gauss.integrate(md, meas, x_n_transpose) #maj_distr[c.index()] = gauss.integrate(mdf, meas, x_n_transpose) #print 'num md_cells', md_distr #print 'ed_cells', ed_distr #print 'delta_e_cells', delta_e_distr #print 'maj_cells', maj_distr #print 'e_cells', e_distr print '\nmd = Sum_K md_K = %8.2e' % sum(md_distr) print 'ed = Sum_K ed_K = %8.2e' % sum(ed_distr) return ed_distr, md_distr, maj_distr
def smoothness_distribution(u, mesh, dim): cell_num = mesh.num_cells() smoothness = postprocess.allocate_array(cell_num) for c in cells(mesh): # vert: num of vertices # horiz: dimension verts = c.get_vertex_coordinates().reshape(dim + 1, dim) u_inf_norm = l_inf_norm(u, verts, dim) h = c.h() denominator = h / 2 * assemble_local(inner( grad(u), grad(u)) * dx, c) + 2 / h * assemble_local(u * u * dx, c) F_c = u_inf_norm**2 / denominator smoothness[c.index()] = F_c return smoothness
def solve_problem_nd_t(self, mesh, V, VV, V_exact, VV_exact, H_div, f, sq_f, phi, grad_phi, u_e, grad_u_e, sq_grad_u_e, u_0, u0_boundary, dim, t_T, domain, C_FD, delta, test_num, problem_params, project_path, results_folder): # Define the value of the time-step nt = problem_params["nt"] tau = float(t_T / nt) eps_k = problem_params['total_accuracy'] / nt # Initialize times t_k = 0 # t_k t_kp1 = tau # t_{k + 1} t_km1 = -tau # t_{k - 1} # Initialize the time integration scheme order = 4 # time integration order 2, 3, 4 quadrature = integrators.TimeIntegrator(order, t_k, t_kp1) # Allocate the space for the arrays ed_k, et_k, e_incr_k, error_k, m0_k, md_k, mf_k, maj_incr_k, majorant_k, beta_k, i_eff_maj_k = \ estimates.allocate_space_for_error_and_majorant(nt) h_max_k = postprocess.allocate_array(nt) h_min_k = postprocess.allocate_array(nt) error_d_k = postprocess.allocate_array(nt) majorant_d_k = postprocess.allocate_array(nt) vd_k = postprocess.allocate_array(nt) vt_k = postprocess.allocate_array(nt) v_norm_incr_k = postprocess.allocate_array(nt) v_norm_k = postprocess.allocate_array(nt) rel_error_k = postprocess.allocate_array(nt) rel_majorant_k = postprocess.allocate_array(nt) primal_problem_time = postprocess.allocate_array(nt) majorant_minimization_time = postprocess.allocate_array(nt) majorant_reconstruction_time = postprocess.allocate_array(nt) # Initialize the approximation and flux on the t = t_k v_k = interpolate(phi, V) y_k = interpolate(grad_phi, H_div) v_k1 = Function(V) # Initialize the counter k = 0 error = 1e8 while k + 1 <= nt: t0_problem = tic() # Update the time integration for the quadratures print "\n-----------------------------------------" print "Time interval [%f, %f]:" % (t_k, t_kp1) print "------------------------------------------\n" quadrature.update(t_k, t_kp1) #while error > eps_k: if k == 0 or problem_params[ 'solving_strategy_tag'] == "with-refinement": # Define unknown function and test function u = TrialFunction(V) # unknown function v = TestFunction(V) # test function for the variational form # Define stiffness and mass matrices based on the functional space K, M = problem.construct_stiffness_and_mass_matrices(u, v, dim) # Define BC bc = DirichletBC(V, u_0, u0_boundary) # Update right hand side f_{k} f.t = t_k f_k = interpolate(f, V) # Update right hand side f_{k + 1} f.t = t_kp1 f_k1 = interpolate(f, V) # Update boundary condition u_0.t = t_kp1 # Update the exact solution at t_{k + 1} u_e.t = t_kp1 u_k1 = interpolate(u_e, V_exact) v_k1 = problem.get_next_step_solution( v_k, v_k1, K, tau, M, f_k, f_k1, bc, problem_params['time_discretization_tag']) # u_0.t = t_kp1 # v_k1 = interpolate(u_e, V) ''' if (problem_params['solving_strategy_tag'] == 'without-refinement' and k == 0) or \ problem_params['solving_strategy_tag'] == 'with-refinement': postprocess.plot_solution(v_k1, mesh, dim, project_path + results_folder + 'u-%d' % (k + 1)) ''' grad_v_k = problem.Grad(v_k, dim) grad_v_k1 = problem.Grad(v_k1, dim) # Define y_1 as a derivative with respect to x_i = x_0 = x[0] y_k1 = project(grad_v_k1, H_div) # Check the exact flux (test) # grad_phi.t = t_kp1 # y_k1 = project(grad_phi, H_div) v_norm_incr_k, vd_k, vt_k = estimates.v_norm_nd_t( k, v_norm_incr_k, vd_k, vt_k, v_k, v_k1, V_exact, VV_exact, mesh, dim, tau, delta) #v_norm_incr_k, vd_k, vt_k = estimates.v_norm_nd_t(k, v_norm_incr_k, vd_k, vt_k, # v_k1, grad_v_k, grad_v_k1, # V_exact, VV_exact, # mesh, dim, tau, delta) # Calculate error components e_incr_k, ed_k, et_k, ed_var = estimates.error_norm_nd_t( k, e_incr_k, ed_k, et_k, grad_u_e, sq_grad_u_e, quadrature, u_k1, v_k1, grad_v_k, grad_v_k1, V_exact, VV_exact, mesh, dim, tau, delta) # Calculate majorant components maj_incr_k, m0_k, md_k, mf_k, beta_k, y_k1, md_var, mf_var = \ estimates.majorant_nd_t(k, maj_incr_k, m0_k, md_k, mf_k, beta_k, e_incr_k[k], ed_k[k], et_k[k], f, sq_f, phi, quadrature, v_k, v_k1, grad_v_k, grad_v_k1, V_exact, y_k, y_k1, H_div, mesh, tau, delta, dim, domain, C_FD, majorant_reconstruction_time, majorant_minimization_time) e_distr, md_distr, maj_distr = estimates.error_majorant_distribution_nd( mesh, dim, ed_var, md_var, mf_var, V_exact) e_distr, md_distr, maj_distr = estimates.majorant_distribution_DG0( mesh, ed_var, md_var, mf_var) # Document indicator perpormance h_max_k[k] = mesh.hmax() h_min_k[k] = mesh.hmin() init_data_info = postprocess.construct_result_tag( test_num, problem_params["nx0"], problem_params["nx1"], problem_params["nx2"], nt, k + 1) # Update the overall error from time interval [0, t_k] error_k, majorant_k, i_eff_maj_k, rel_error_k, rel_majorant_k = \ estimates.add_increment_of_error_and_majorant(k, e_incr_k, maj_incr_k, error_k, et_k, majorant_k, i_eff_maj_k, v_norm_incr_k, v_norm_k, vt_k, rel_error_k, rel_majorant_k) postprocess.output_time_layer_result_error_and_majorant( t_k, t_kp1, rel_error_k[k], rel_majorant_k[k], i_eff_maj_k[k]) error = error_k[k] # Define the solving strategy (refine or not refine) if problem_params['solving_strategy_tag'] == "with-refinement": if dim == 2 and problem_params[ 'refinement_criteria_tag'] == "majorant": postprocess.plot_carpet_2d( mesh, project(ed_var, V_exact), project_path + results_folder + 'carpet-error' + init_data_info) postprocess.plot_carpet_2d( mesh, project(md_var, V_exact), project_path + results_folder + 'carpet-majorant' + init_data_info) # Refine mesh mesh = self.execute_refinement_strategy( problem_params, mesh, e_distr, md_distr, maj_distr, error_k, majorant_k, i_eff_maj_k, error_d_k, majorant_d_k, h_max_k, h_min_k, t_T, k, nt, project_path, results_folder, init_data_info) # num_cells = mesh.num_cells() # num_vertices = mesh.num_vertices() # results_info = postprocess.construct_result_tag(test_num, nt, nx0, nx1, nx2, k+1, num_cells, num_vertices, dim) # Update functional spaces, BC, and stiffness/mass matrices V, VV, V_exact, VV_exact, H_div = problem.functional_spaces( mesh, problem_params, dim) # Update functions #v_k1.set_allow_extrapolation(True) #y_k1.set_allow_extrapolation(True) v_k.assign(interpolate(v_k1, V)) #v_k.assign(project(v_k1, V)) #v_k.assign(v_k1) y_k.assign(interpolate(y_k1, H_div)) v_k1 = Function(V) else: # Document results postprocess.document_results_without_refinement( mesh, e_distr, md_distr, error_k, majorant_k, i_eff_maj_k, error_d_k, majorant_d_k, h_max_k, h_min_k, t_T, k, nt, project_path, results_folder, init_data_info) # Update functions v_k.assign(v_k1) y_k.assign(y_k1) # Update time layer t_kp1 += tau t_km1 += tau t_k += tau k += 1
def solve_problem_nd_t(self, mesh, boundary_facets, ds, dS, V, VV, V_exact, VV_exact, H_div, u_e, grad_ue, f, A, invA, adjA, min_eig_A, lmbd, a, u0, uD, uN, C_FD, C_Ntr, delta, gamma, project_path, results_folder, test_params): # Define the number of refinements ref_num = test_params['number_of_refinements'] ref_num_array = postprocess.allocate_array(ref_num) h_max_array = postprocess.allocate_array(ref_num) h_min_array = postprocess.allocate_array(ref_num) DOFs = postprocess.allocate_array(ref_num) numcells_array = postprocess.allocate_array(ref_num) numverts_array = postprocess.allocate_array(ref_num) e_H1_array = postprocess.allocate_array(ref_num) e_L2_array = postprocess.allocate_array(ref_num) e_total_array = postprocess.allocate_array(ref_num) maj_array = postprocess.allocate_array(ref_num) maj_II_array = postprocess.allocate_array(ref_num) min_array = postprocess.allocate_array(ref_num) i_eff_maj_array = postprocess.allocate_array(ref_num) i_eff_maj_II_array = postprocess.allocate_array(ref_num) i_eff_min_array = postprocess.allocate_array(ref_num) rel_error_k = postprocess.allocate_array(ref_num) rel_majorant_k = postprocess.allocate_array(ref_num) e_L_array = postprocess.allocate_array(ref_num) e_id_array = postprocess.allocate_array(ref_num) # Initialize the variables before the loop maj = 10e8 i = 0 while i <= ref_num - 1: print(" ") print( "%-----------------------------------------------------------------------------------------------------------%" ) print " Refinement cycle # %d : DOFs = %d" % ( i, len(V.dofmap().dofs())) print( "%-----------------------------------------------------------------------------------------------------------%" ) print(" ") # Compute approximate solution, its error and majorant u, delta_stab = problem.solve_convection_reaction_diffusion( V, c_H, eps, f, A, min_eig_A, lmbd, problem.interpolate_vector_function(a, dim, V_exact, VV_exact), u0, uD, uN, mesh, boundary_facets, self.dim, self.test_num, test_params) #u = problem.solve_parabolic_problem(V, c_H, eps, f, A, u0, uD, mesh, boundary_facets, dim, test_num) # In case of explicitely unknown exact solution, calculate it with high order order polynomials if test_params['solution_tag'] == "reference-solution": # Compute reference solution with high order polynomials u_e, delta_stab_e = problem.solve_convection_reaction_diffusion( V_exact, c_H, eps, f, A, min_eig_A, lmbd, problem.interpolate_vector_function( a, dim, V_exact, VV_exact), u0, uD, uN, mesh, boundary_facets, self.dim, self.test_num, test_params) #u_e = problem.solve_parabolic_problem(V_exact, c_H, eps, f, u0, uD, mesh, boundary_facets, dim, test_num) #''' if test_params["PLOT"] == True and ref_num <= 3: # Plot approximate solution postprocess.plot_function_3d( mesh, u, project_path + results_folder + 'u-%d' % i) #postprocess.plot_function_3d(mesh, u_e, project_path + results_folder + 'ue-%d' % i) #postprocess.plot_function(u_e, mesh, dim, project_path + results_folder + 'u-ref-%d' % i) #postprocess.plot_function_3d(mesh, f, project_path + results_folder + 'ue-ref-%d' % i) #postprocess.plot_function(u, mesh, dim, project_path + results_folder + 'u-%d' % i) #''' # Calculate error e, val_e, val_grad_e, val_delta_e, e_T, val_e_L, val_e_id, \ var_e, var_delta_e, var_grad_e = estimates.error_norm(u, u_e, A, lmbd, problem.interpolate_vector_function(a, dim, V_exact, VV_exact), f, c_H, test_params["v_approx_order"], mesh, T, dim, V, V_exact) # Define the error for the majorant error = (2 - delta) * val_grad_e + 2 * val_delta_e + c_H * e_T error_II = (2 - delta) * val_grad_e + 2 * val_delta_e + ( 1 - 1 / gamma) * c_H * e_T if test_params["error_estimates"] == True: # Calculate majorant #y_ = project(A * NablaGrad(u_e, dim), V) #y = project(A * NablaGrad(u, dim), H_div) #y = project(A * NablaGrad(interpolate(u_e, V_exact), dim), H_div) y = project(A * NablaGrad(u, dim), H_div) """ if test_params['solution_tag'] == 'predefined-solution': y = project(A * NablaGrad(u, dim), H_div) elif test_params['solution_tag'] == 'reference-solution': y = project(A * NablaGrad(interpolate(u_e, V_exact), dim), H_div) """ #print ' y - y_ = ', assemble(inner(y - y_, y - y_) * dx) #y = interpolate(problem.Grad(u, dim), H_div) MAJORANT_OPTIMIZE = 1 #y = project(problem.Grad(u_e, dim), H_div) #MAJORANT_OPTIMIZE = 1 # Calculate majorant maj, y, beta, md, mf, var_m_d, var_m_f_w_opt, \ majorant_reconstruction_time, majorant_minimization_time = \ estimates.majorant_nd(u, V_exact, y, H_div, f, A, invA, min_eig_A, c_H, eps, lmbd, a, u0, error, mesh, C_FD, dim, test_params) i_eff_maj = sqrt(maj / error) maj_array[i] = maj i_eff_maj_array[i] = i_eff_maj #w = project(u_e - interpolate(u, W), W) # Calculate majorant #maj_II, beta, majorant_reconstruction_time, majorant_minimization_time = \ # estimates.majorant_II_nd(u, V_exact, w, W, y, f, u0, error_II, gamma, T, mesh, C_FD, dim, v_deg, MAJORANT_OPTIMIZE) #i_eff_maj_II = sqrt(maj_II / error) # Construct error and majorant distribution #e_distr, m_distr, maj_distr = estimates.majorant_distribution_using_DG0(e, rd, rf, A, invA, beta, C_FD, mesh, dim) # , mf_distr, maj_distr, ed, md = \ ed_distr, md_distr, maj_distr = \ estimates.error_majorant_distribution_nd(mesh, dim, V, var_grad_e, var_delta_e, var_m_d, var_m_f_w_opt, beta, C_FD) #min, phi = estimates.minorant(u, mesh, V_exact, u_0, boundary, f, dim, error) #i_eff_min = sqrt(min / error) #estimates.output_result_minorant(min, min/error) min = 0.0 i_eff_min = 0.0 #residual_CG0_distr, e_DWR_CG0_distr, J_e_CG0_distr = estimates.get_indicators_CG0(e, sqrt(error), V, V_exact, f, u0, boundary, u, u_e, # mesh, dim) #eta_distr_, E_DWR_distr_, J_e_distr_, m_d_distr_, m_df_distr_ = \ # majorants.compare_error_indicators(mesh, V, V_exact, V_exact, f, u0, boundary, u, interpolate(u_e, V_exact), y, beta, test_num) # Update the arrays of data with respect to the refinement cycle ref_num_array[i] = i e_H1_array[i] = sqrt(val_grad_e) e_L2_array[i] = sqrt(val_e) e_total_array[i] = sqrt(error) e_L_array[i] = sqrt(val_e_L) e_id_array[i] = sqrt(val_e_id) maj_array[i] = sqrt(maj) i_eff_maj_array[i] = sqrt(maj / error) maj_array[i] = sqrt(maj) i_eff_min_array[i] = sqrt(min / error) h_max_array[i] = mesh.hmax() h_min_array[i] = mesh.hmin() DOFs[i] = len(V.dofmap().dofs()) num_cells = mesh.num_cells() num_verts = mesh.num_vertices() numcells_array[i] = num_cells numverts_array[i] = num_verts # Output the results of upper bound reconstructions estimates.output_result_error_and_majorant(sqrt(error), sqrt(maj), sqrt(maj / error)) # Construct the tag with problem information results_info = postprocess.construct_result_tag( test_num, i, test_params["nx0"], test_params["nx1"], test_params["nx2"], num_cells, num_verts) # Plot and document mesh on the current refinement iteration postprocess.document_results(mesh, test_params, project_path, results_folder, results_info, ed_distr, md_distr, maj_distr, error, maj, i_eff_maj, min, i_eff_min, h_max_array, h_min_array) # Refine mesh #mesh = self.execute_refiniment_strategy(mesh, test_params, e_distr, m_distr) mesh = self.execute_refiniment_strategy(mesh, test_params, ed_distr, md_distr) # Update functional spaces, BC, and stiffness/mass matrices V, VV, V_exact, VV_exact, H_div = problem.functional_spaces( mesh, test_params, dim) # Update boundary faces boundary_facets, ds, dS = self.construct_boundary_faces(mesh) i = i + 1 decay_result_folder = postprocess.create_decay_tag( test_params, project_path, results_folder, results_info) postprocess.document_errors_decay(float(dim), test_params, decay_result_folder, DOFs[0:i], h_min_array[0:i], e_H1_array[0:i], e_L2_array[0:i], maj_array[0:i], min_array[0:i], i_eff_maj_array[0:i], e_L_array[0:i], e_id_array[0:i])
def l_inf_norm(v, coords, dim): abs_vals = postprocess.allocate_array(dim + 1) for i in range(0, dim + 1): abs_vals[i] = abs(v(coords[i, :])) return numpy.max(abs_vals)
def error_majorant_distribution_nd(mesh, dim, V_exact, var_grad_e, var_delta_e, var_m_d, var_m_f_w_opt, beta): # Allocate arrays for error and majorant distributions cell_num = mesh.num_cells() ed_distr = postprocess.allocate_array(cell_num) delta_e_distr = postprocess.allocate_array(cell_num) md_distr = postprocess.allocate_array(cell_num) mf_distr = postprocess.allocate_array(cell_num) # Project UFL forms on the high-order functional space to obtain functions ed = project(var_grad_e, V_exact) delta_e = project(var_delta_e, V_exact) md = project(var_m_d, V_exact) mf_wopt = project(var_m_f_w_opt, V_exact) # Obtained the mapping #dofmap = V_exact #mesh_coordimates = dofmap. # Define integration scheme order and assign the integrator scheme_order = 4 gauss = integration.SpaceIntegrator(scheme_order, dim) # Iterate over cells of the mesh and obtain local value of the majorant for c in cells(mesh): # Obtaining the coordinates of the vertices in the cell verts = c.get_vertex_coordinates().reshape(dim + 1, dim) x_n_transpose = verts.T # Calculating the area of the cell matrix = postprocess.allocate_array_2d(dim + 1, dim + 1) matrix[0:dim, 0:dim + 1] = x_n_transpose matrix[dim, 0:dim + 1] = numpy.array([1.0 for i in range(dim + 1)]) #print "matrix = ", matrix meas = abs(numpy.linalg.det(matrix)) # Integrating over the cell ed_distr[c.index()] = gauss.integrate(ed, meas, x_n_transpose) delta_e_distr[c.index()] = gauss.integrate(delta_e, meas, x_n_transpose) md_distr[c.index()] = gauss.integrate(md, meas, x_n_transpose) mf_distr[c.index()] = gauss.integrate(mf_wopt, meas, x_n_transpose) # Calculate majorant based on the parameter value if sum(mf_distr) <= DOLFIN_EPS: maj_distr = md_distr else: if sum(md_distr) <= DOLFIN_EPS: maj_distr = mf_distr else: maj_distr = ( 1.0 + beta ) * md_distr + mf_distr # here (1 + 1/beta) weight is ommited since it is included into the optimal mf_wopt e_distr = ed_distr + delta_e_distr #print 'md_cells', md_distr #print 'ed_cells', ed_distr print 'sum md_cells', sum(md_distr) print 'sum maj_cells', sum(maj_distr) print 'sum e_cells', sum(e_distr) return ed_distr, delta_e_distr, e_distr, md_distr, mf_distr, maj_distr, ed, md
def solve_problem_nd_t(mesh, V, VV, V_exact, VV_exact, H_div, f, lmbd, phi, grad_phi, u_e, grad_u_e, u_0, u0_boundary, dim, domain, C_FD, nx0, nx1, nx2, test_num, results_folder, problem_params): print " " print "%---------------------" print "% Solving the problem " print "%---------------------" print " " # Initialize the project path project_path = postprocess.get_project_path() if problem_params['solving_strategy_tag'] == "with-refinement": ref_num = 5 else: ref_num = 1 ref_num_array = postprocess.allocate_array(ref_num) h_max_array = postprocess.allocate_array(ref_num) e_array = postprocess.allocate_array(ref_num) maj_array = postprocess.allocate_array(ref_num) min_array = postprocess.allocate_array(ref_num) i_eff_maj_array = postprocess.allocate_array(ref_num) i_eff_min_array = postprocess.allocate_array(ref_num) for i in range(0, ref_num): # Compute approximate solution, its error and majorant u = problem.solve_poisson(V, f, lmbd, u_0, u0_boundary, mesh) # Calculate error error = estimates.error_norm(u, u_e, lmbd, V_exact, mesh, dim) # Calculate majorant y = project(estimates.Grad(u, dim), H_div) MAJORANT_OPTIMIZE = 1 #y = project(grad_phi, H_div) #MAJORANT_OPTIMIZE = 0 # Calculate majorant maj, y, beta, md, mf, majorant_reconstruction_time, majorant_minimization_time = \ estimates.majorant_nd(u, y, H_div, f, lmbd, error, mesh, dim, domain, C_FD, MAJORANT_OPTIMIZE) i_eff_maj = sqrt(maj / error) # Construct error and majorant distribution e_distr, m_distr = estimates.error_majorant_distribution_nd( mesh, dim, u_e, u, V_exact, y) print "% -------------" print "% Save results " print "% -------------" num_cells = mesh.num_cells() num_vertices = mesh.num_vertices() # Construct the tag with problem information results_info = postprocess.construct_result_tag( test_num, i, nx0, nx1, nx2, num_cells, num_vertices) # Plot histogram with the error and majorant distribution postprocess.plot_histogram( mesh, e_distr, m_distr, project_path + results_folder + 'e-maj-distr-hist' + results_info) # Output the results of upper bound reconstructions estimates.output_result_error_and_majorant(error, maj, i_eff_maj) min, phi = estimates.minorant(u, mesh, V_exact, u_0, u0_boundary, f, lmbd, dim, error) i_eff_min = sqrt(min / error) estimates.output_result_minorant(min, min / error) # Update the arrays of data with respect to the refinement cycle ref_num_array[i] = i + 1 e_array[i] = error maj_array[i] = maj min_array[i] = min i_eff_maj_array[i] = i_eff_maj i_eff_min_array[i] = i_eff_min h_max_array[i] = mesh.hmax() # Define the refinement strategy if problem_params['refinement_tag'] == "adaptive": # Define the distribution (error or majorant) upon which the refinement is based if problem_params['refinement_criteria_tag'] == 'error': distr = e_distr elif problem_params['refinement_criteria_tag'] == 'majorant': distr = m_distr #elif test_params['refinement_criteria_tag'] == 'e-dwr': # distr = e_dwr_distr #elif test_params['refinement_criteria_tag'] == 'residual': # distr = residual_distr # Run the marking procedure theta = 0.4 # percentage of the marked cells if problem_params['marking_tag'] == 'average': marking = estimates.averaged_marking(mesh, distr) elif problem_params['marking_tag'] == 'predefined': marking = estimates.predefined_amount_of_elements_marking( mesh, distr, theta) elif problem_params['marking_tag'] == 'bulk': marking = estimates.bulk_marking(mesh, distr, theta) # Refine mesh based on the marking criteria mesh = refine(mesh, marking) refinement_tag = problem_params[ 'refinement_criteria_tag'] + '-' + problem_params[ 'marking_tag'] + '-marking' refinement_tag = refinement_tag + '-theta-%d' % (theta * 100) # Plot result mesh postprocess.plot_mesh( mesh, project_path + results_folder + 'mesh-based-' + refinement_tag + results_info) # Save the results in mat-file postprocess.save_results_to_mat_file( e_distr, m_distr, error, maj, i_eff_maj, min, i_eff_min, project_path + results_folder + 'results-on-mesh-based-' + refinement_tag + results_info) elif problem_params['refinement_tag'] == "uniform": # Plot result mesh postprocess.plot_mesh( mesh, project_path + results_folder + 'mesh-uniform' + results_info) # Save the results in mat-file postprocess.save_results_to_mat_file( e_distr, m_distr, error, maj, i_eff_maj, min, i_eff_min, project_path + results_folder + 'results-on-mesh-uniform' + results_info) # Refinement for the comparison with theta < 1.0 #cell_markers = CellFunction("bool", mesh) #cell_markers.set_all(True) #mesh = refine(mesh, cell_markers) # Refinement for the optimal convergence mesh = refine(mesh) if i == ref_num - 1: # Define the refinement_tag if problem_params['refinement_tag'] == "adaptive": refinement_tag = problem_params['refinement_criteria_tag'] + '-' + \ problem_params['marking_tag'] + '-marking' elif problem_params['refinement_tag'] == "uniform": refinement_tag = problem_params['refinement_tag'] postprocess.plot_decay_error_majorant( problem_params["v_approx_order"], h_max_array, e_array, maj_array, project_path + results_folder + "error-maj-v-P%d-" % problem_params["v_approx_order"] + refinement_tag + results_info) postprocess.save_decay_to_mat_file( h_max_array, e_array, maj_array, project_path + results_folder + "maj-decay-v-P%d" % problem_params["v_approx_order"]) postprocess.plot_decay_majorant_v_of_deg( h_max_array, maj_array, problem_params["v_approx_order"], project_path + results_folder + "maj-decay-v-P%d" % problem_params["v_approx_order"] + results_info) # Update functional spaces, BC, and stiffness/mass matrices V, VV, V_exact, VV_exact, H_div = problem.update_functional_space( mesh, problem_params["v_approx_order"], problem_params["flux_approx_order"], dim) #tag = 'E-DWR-' #tag = 'eta-' #tag = 'error-' #tag = 'm-d-' tag = 'm-df-' '''
def solve_problem_nd_t(self, mesh, boundary_facets, ds, dS, V, V_exact, VV_exact, H_div, u_e, f, A, invA, adjA, min_eig_A, lmbd, a, eps_curr, uD, uN, C_FD, C_Ntr, project_path, results_folder, test_params): # Define the number of refinements ref_num = test_params['number_of_refinements'] + 20 # Define arrays with data collected on the refinement steps ref_num_array = postprocess.allocate_array(ref_num + 1) h_max_array = postprocess.allocate_array(ref_num + 1) h_min_array = postprocess.allocate_array(ref_num + 1) dofs_array = postprocess.allocate_array(ref_num + 1) numcells_array = postprocess.allocate_array(ref_num + 1) numverts_array = postprocess.allocate_array(ref_num + 1) e_array = postprocess.allocate_array(ref_num + 1) e_l2_array = postprocess.allocate_array(ref_num + 1) e_linf_array = postprocess.allocate_array(ref_num + 1) e_min_array = postprocess.allocate_array(ref_num + 1) maj_array = postprocess.allocate_array(ref_num + 1) min_array = postprocess.allocate_array(ref_num + 1) i_eff_maj_array = postprocess.allocate_array(ref_num + 1) i_eff_min_array = postprocess.allocate_array(ref_num + 1) # Initialize the variables before the loop maj = 10e8 i = 0 h_min = 10e8 # TO DO: Calculate the reference solution on the refined mesh ''' if test_params['solution_tag'] == "reference-solution": mesh_ref = self.construct_mesh(2 ** ref_num * test_params["nx0"], 2 ** ref_num * test_params["nx1"], 2 ** ref_num * test_params["nx2"], 2 ** ref_num * test_params["res"], project_path) # Update functional spaces, BC, and stiffness/mass matrices V_ref, VV_ref, V_ref_exact, VV_ref_exact, H_ref_div = problem.functional_spaces(mesh_ref, test_params["v_approx_order"], test_params["flux_approx_order"], dim) boundary_facets_ref, ds = self.construct_boundary_faces(mesh_ref) u_e, delta = problem.solve_convection_reaction_diffusion(V_ref_exact, f, A, min_eig_A, lmbd, problem.interpolate_vector_function(a, dim, V_ref_exact, VV_ref_exact), self.boundary, uD, uN, mesh_ref, boundary_facets_ref, dim, test_num) ''' while h_min > eps_curr: # (i <= ref_num and maj > test_params['accuracy_level']) or print(" ") print( "%-----------------------------------------------------------------------------------------------------------%" ) print " Refinement cycle # %d : DOFs = %d" % ( i, len(V.dofmap().dofs())) print( "%-----------------------------------------------------------------------------------------------------------%" ) print(" ") # Compute approximate solution u and stabilization parameter (in case of the convection dominated problem) u, delta = problem.solve_convection_reaction_diffusion( V, f, A, min_eig_A, lmbd, problem.interpolate_vector_function(a, dim, V_exact, VV_exact), self.boundary, uD, uN, mesh, boundary_facets, self.dim, self.test_num, test_params) # In case of explicitely unknown exact solution, calculate it with high order order polynomials if test_params['solution_tag'] == "reference-solution": # Compute reference solution with high order polynomials u_e, delta = problem.solve_convection_reaction_diffusion( V_exact, f, A, min_eig_A, lmbd, problem.interpolate_vector_function( a, dim, V_exact, VV_exact), self.boundary, uD, uN, mesh, boundary_facets, self.dim, self.test_num, test_params) if test_params["PLOT"] == True: #plot(u, interactive=True) # Plot approximate solution postprocess.plot_function( u, mesh, dim, project_path + results_folder + 'u-%d' % i) # Calculate error grad_e, e_l2, e_linf, delta_e, lambda_e, a_e, var_grad_e, var_lmbd_diva_e, var_lmbd_e, var_a_e = \ estimates.error_norm(u, u_e, lmbd, A, invA, a, problem.interpolate_vector_function(a, dim, V_exact, VV_exact), V, V_exact, mesh, dim) # Define the error for the majorant error = grad_e + delta_e # Define the error for the minorant if test_params[ "pde_tag"] == "reaction-diffusion-pde" or test_params[ "pde_tag"] == "diffusion-pde": error_min = grad_e + delta_e elif test_params[ "pde_tag"] == "reaction-convection-diffusion-pde" or test_params[ "pde_tag"] == "convection-diffusion-pde": error_min = grad_e + lambda_e + a_e if test_params["error_estimates"] == True: # L2-projection of grad u to Hdiv space y = project(A * Grad(u, dim), H_div) # Test for the correctness of the code and the majorant for the y = A dot \grad u # y = interpolate(A * Grad(u, dim), H_div) #y = project(A * Grad(u_e, dim), H_div) # Contruct the majorant maj, y, beta, md, mf, var_m_d, var_m_f_w_opt, \ majorant_reconstruction_time, majorant_minimization_time = \ estimates.majorant_nd(u, y, H_div, f, A, invA, min_eig_A, eps, a, lmbd, error, mesh, dim, C_FD, C_Ntr, test_params) # Output the results of upper bound reconstructions estimates.output_result_error_and_majorant( sqrt(error), sqrt(maj), sqrt(maj / error)) # Construct error and majorant distribution ed_distr, delta_e_distr, e_distr, md_distr, mf_distr, maj_distr, ed, md = \ estimates.error_majorant_distribution_nd(mesh, dim, V_exact, var_grad_e, var_lmbd_diva_e, var_m_d, var_m_f_w_opt, beta) # Calculate the smoothness parameter #smoothness_distr = problem.smoothness_distribution(u, mesh, dim) #print "smoothness ", smoothness_distr #smooth_criterion = problem.SmoothnessCriterion(v=interpolate(u, V_exact), mesh=mesh, # expression_degree=test_params["expression_degree"]) #smoothness_vals = project(smooth_criterion, FunctionSpace(mesh, "DG", 0)) #print "smoothness_vals:", smoothness_vals.vector().array() # Calculate minorant min, phi = estimates.minorant(u_e, u, mesh, ds, V_exact, uN, self.boundary, f, A, lmbd, a, dim, test_params) # Output the results of lower bound reconstructions estimates.output_result_minorant(sqrt(error_min), sqrt(min), sqrt(min / error_min)) maj_array[i] = sqrt(maj) i_eff_maj_array[i] = sqrt(maj / error) min_array[i] = sqrt(min) i_eff_min_array[i] = sqrt(min / error_min) if test_params["pde_tag"] == "reaction-convection-diffusion-pde" \ or test_params["pde_tag"] == "convection-diffusion-pde": e_min_array[i] = sqrt(error_min) else: e_min_array[i] = e_array[i] ''' #tag = 'E-DWR-' #tag = 'eta-' #tag = 'error-' #tag = 'm-d-' #tag = 'm-df-' eta_distr, E_DWR_distr, J_e_distr, m_d_distr, m_df_distr = \ compare_error_indicators(mesh, V, V_star, V_e, f, u_0, boundary, u, interpolate(u_e, V_e), interpolate(grad_u_e, VV_e), y, beta, test_num, tag) cells_num = mesh.num_cells() N_array[i] = cells_num e_array[i] = sum(J_e_distr) if tag == 'eta-': distr = eta_distr elif tag == 'E-DWR-': distr = E_DWR_distr elif tag == 'error-': distr = J_e_distr elif tag == 'm-d-': distr = m_d_distr elif tag == 'm-df-': distr = m_df_distr ''' ''' md_CG0_distr, mdf_CG0_distr, e_CGO_distr = estimates.majorant_distribution_DG0(mesh, f, lmbd, A, invA, u, e_form, y, beta, C_FD, dim) residual_CG0_distr, e_DWR_CG0_distr, J_e_CG0_distr = estimates.get_indicators_CG0(mesh, V, V_exact, f, A, adjA, lmbd, u_0, boundary, u, u_e, e_form, sqrt(e_d), dim) # Plot histograms with obtained indicators postprocess.plot_histogram(mesh, J_e_CG0_distr, residual_CG0_distr, project_path + results_folder + 'je-residual-distr-hist' + results_info) postprocess.plot_histogram(mesh, J_e_CG0_distr, e_DWR_CG0_distr, project_path + results_folder + 'je-edwr-distr-hist' + results_info) # Plot histograms with obtained indicators from majorant postprocess.plot_histogram(mesh, J_e_CG0_distr, md_CG0_distr, project_path + results_folder + 'je-md-distr-hist' + results_info) postprocess.plot_histogram(mesh, J_e_CG0_distr, mdf_CG0_distr, project_path + results_folder + 'je-mdf-distr-hist' + results_info) ''' # Update the arrays of data with respect to the refinement cycle ref_num_array[i] = i + 1 e_array[i] = sqrt(error) e_l2_array[i] = e_l2 e_linf_array[i] = e_linf h_max_array[i] = mesh.hmax() h_min_array[i] = mesh.hmin() h_min = mesh.hmin() print "hmax = ", mesh.hmax() print "hmin = ", mesh.hmin() num_cells = mesh.num_cells() num_vertices = mesh.num_vertices() num_dofs = len(V.dofmap().dofs()) numcells_array[i] = num_cells numverts_array[i] = num_vertices dofs_array[i] = num_dofs # Construct the tag with problem information results_info = postprocess.construct_result_tag( test_num, i, test_params["nx0"], test_params["nx1"], test_params["nx2"], num_cells, num_vertices) # Plot histogram with the error and majorant distribution #postprocess.plot_histogram(mesh, ed_distr, md_distr, # project_path + results_folder + 'e-maj-distr-hist' + results_info) #postprocess.plot_histogram_smoothness(mesh, mf_distr, 'k', # project_path + results_folder + 'mf-distr-hist' + results_info) #postprocess.plot_histogram_smoothness(mesh, smoothness_distr, 'b', # project_path + results_folder + 'smoothness-distr-hist' + results_info) # For the refinement accept the last one change the mesh-dependent values like spaces and mesh functions if ref_num > 0 and i + 1 <= ref_num: # If the error estimates are constructed, then the mesh refinement can vary: uniform or adaptive if test_params["error_estimates"] == True: # Refine mesh mesh, mat_file_tag = self.execute_refinement_strategy( test_params, mesh, e_distr, md_distr, project_path, results_folder, results_info) # Save the results in mat-file postprocess.save_results_to_mat_file( ed_distr, md_distr, e_array, maj_array, i_eff_maj_array, e_min_array, min_array, mat_file_tag) if i + 1 == ref_num: postprocess.save_mesh_to_xml_file( mesh, project_path + results_folder, results_info) # If the refiniment strategy is adaptive, that plot the changes in the mesh, majorant, and the error if test_params['refinement_tag'] == "adaptive": if test_params["PLOT"] == True: # Plot result mesh if dim == 2: postprocess.plot_mesh( mesh, project_path + results_folder + 'mesh' + results_info) # Plot 'carpets with colored elements' dependent on the error and the majorant #postprocess.plot_carpet_2d(mesh, ed, # project_path + results_folder + 'carpet-error' + results_info) #postprocess.plot_carpet_2d(mesh, md, # project_path + results_folder + 'carpet-majorant' + results_info) elif dim == 3: postprocess.plot_mesh_3d( mesh, project_path + results_folder + 'initial-mesh') # If the error estimates aren't constructed, the mesh refinement is uniform else: mesh = refine(mesh) # Update functional spaces, BC, and stiffness/mass matrices V, VV, V_exact, VV_exact, H_div = problem.functional_spaces( mesh, test_params, dim) if test_params[ 'material_tag'] == "material-changing": # over the domain # Define A if it is changing A = problem.construct_from_mesh_functions( dim, self.A_expr, mesh) boundary_facets, ds, dS = self.construct_boundary_faces(mesh) # Define the value of the maj for the loop criteria if test_params["error_estimates"] == True: maj = maj_array[i] else: maj = e_array[i] # Update the refinement number i += 1 # Output the results decay_result_folder = postprocess.create_decay_tag( test_params, project_path, results_folder, results_info) postprocess.document_errors_decay(float(self.dim), test_params, decay_result_folder, dofs_array[0:i], h_min_array[0:i], e_array[0:i], e_l2_array[0:i], e_linf_array[0:i], e_min_array[0:i], maj_array[0:i], min_array[0:i]) return mesh, V, VV, V_exact, VV_exact, H_div, boundary_facets, ds, dS