Ejemplo n.º 1
0
    def test_majorant(self, problem_params):

        # Define the project path
        # --------------------------------------------------------------------------------------------------------------#
        project_path = postprocess.get_project_path()

        # Check if it does exist and create folder for the results
        # --------------------------------------------------------------------------------------------------------------#
        results_folder_name = postprocess.construct_result_folder_name(
            self.dim, test_num, problem_params)
        postprocess.create_results_folder(results_folder_name)

        # --------------------------------------------------------------------------------------------------------------#
        # Define problem data
        # --------------------------------------------------------------------------------------------------------------#

        # Define the mesh based on the domain geometry
        # --------------------------------------------------------------------------------------------------------------#
        # Function to generate the mesh
        mesh = self.construct_mesh(test_params["nx0"], test_params["nx1"],
                                   test_params["nx2"], test_params["res"],
                                   project_path)
        """
        # Plot and save the initial mesh
        if self.dim == 2:
            postprocess.plot_mesh(mesh, project_path + results_folder_name + 'initial-mesh')
        elif self.dim == 3:
            postprocess.plot_mesh_3d(mesh, project_path + results_folder_name + 'initial-mesh')
        """

        # Get boundary facets function
        facet_funcs = FacetFunction('size_t', mesh)
        facet_funcs.set_all(0)

        # Calculate the estimate for Freidrichs constant based on the domain
        C_FD = problem.calculate_CF_of_domain(domain, self.dim)

        # Define functional spaces based on the mesh
        # --------------------------------------------------------------------------------------------------------------#
        V, VV, V_exact, VV_exact, H_div = problem.functional_spaces(
            mesh, problem_params, self.dim)

        # Define problem data functions
        # --------------------------------------------------------------------------------------------------------------#
        f, sq_f, phi, grad_phi, u_e, grad_u_e, sq_grad_u_e = self.convert_problem_data_to_expressions(
        )
        #f, A, invA, adjA, lmbd, a, uD, u_e, grad_u_e = self.convert_problem_data_to_expressions(mesh, test_params)

        # Majorant parameter
        delta = 1.0

        # Output to console the problem data
        problem.output_problem_characteristics(
            self.test_num, self.u_expr, self.grad_u_expr, self.f_expr,
            self.A_expr, self.lambda_expr, self.a_expr,
            self.uD_expr, self.domain, self.T, self.dim, mesh.num_cells(),
            mesh.num_vertices(), test_params["nt"],
            test_params["v_approx_order"], test_params["flux_approx_order"],
            delta)

        # Run the solver with a posteriori error control
        # --------------------------------------------------------------------------------------------------------------#
        t0_problem = time.clock()
        self.solve_problem_nd_t(
            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_e,
            self.u0_boundary,  # Dirichlet boundary condition
            self.dim,
            self.T,
            self.domain,
            C_FD,
            delta,
            self.test_num,
            test_params,
            project_path,
            results_folder_name)

        t1_problem = time.clock()
        print "time = ", t1_problem - t0_problem
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    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])
Ejemplo n.º 4
0
    def test_estimates(self, test_params):

        # Define the project path
        # --------------------------------------------------------------------------------------------------------------#
        project_path = postprocess.get_project_path()

        # Check if it does exist and create folder for the results
        # --------------------------------------------------------------------------------------------------------------#
        results_folder_name = postprocess.construct_result_folder_name(
            self.dim, test_num, test_params)
        postprocess.create_results_folder(results_folder_name, test_params)

        # --------------------------------------------------------------------------------------------------------------#
        # Define problem data
        # --------------------------------------------------------------------------------------------------------------#

        # Define the mesh based on the domain geometry
        # --------------------------------------------------------------------------------------------------------------#
        mesh = self.construct_mesh(test_params["nx0"], test_params["nx1"],
                                   test_params["nx2"], test_params["nt"],
                                   test_params["res"], project_path)

        if test_params["PLOT"] == True:
            # Plot and save the initial mesh
            file_name = project_path + results_folder_name + 'initial-mesh'
            if self.dim == 2: postprocess.plot_mesh(mesh, file_name)
            elif self.dim == 3: postprocess.plot_mesh_3d(mesh, file_name)

        boundary_facets, ds, dS = self.construct_boundary_faces(mesh)

        # Calculate the estimate for Freidrichs constant based on the domain
        C_FD = problem.calculate_CF_of_domain(domain, self.dim - 1)
        C_Ntr = problem.calculate_trace_constant(self.test_num)

        # Define functional spaces based on the mesh
        #--------------------------------------------------------------------------------------------------------------#
        V, VV, V_exact, VV_exact, H_div = \
            problem.functional_spaces(mesh, test_params, self.dim)

        # Define problem data functions
        #--------------------------------------------------------------------------------------------------------------#
        f, A, invA, adjA, lmbd, a, uD, uN, u0, u_e, grad_u_e = self.convert_problem_data_to_expressions(
            mesh, test_params)

        # Majorant parameters
        delta = 1
        # gamma = 1.5
        # gamma = 2
        gamma = 1

        # Output to console the problem data
        problem.output_problem_characteristics(self.test_num, self.u_expr,
                                               self.grad_u_expr, self.f_expr,
                                               self.domain, self.dim,
                                               mesh.num_cells(),
                                               mesh.num_vertices(),
                                               test_params["v_approx_order"])

        t0_problem = time.clock()
        self.solve_problem_nd_t(mesh, boundary_facets, ds, dS, V, VV, V_exact,
                                VV_exact, H_div, u_e, grad_u_e, f, A, invA,
                                adjA, self.min_eig_A, lmbd, a, u0, uD, uN,
                                C_FD, C_Ntr, delta, gamma, project_path,
                                results_folder_name, test_params)
        t1_problem = time.clock()

        print("time = %d s" % (t1_problem - t0_problem))
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    def test_estimates(self, test_params):

        # Define the project path
        # --------------------------------------------------------------------------------------------------------------#
        project_path = postprocess.get_project_path()

        # Check if it does exist and create folder for the results
        # --------------------------------------------------------------------------------------------------------------#
        results_folder_name = postprocess.construct_result_folder_name(
            self.dim, test_num, test_params)
        postprocess.create_results_folder(results_folder_name, test_params)

        # --------------------------------------------------------------------------------------------------------------#
        # Define problem data
        # --------------------------------------------------------------------------------------------------------------#

        # Define the mesh based on the domain geometry
        # --------------------------------------------------------------------------------------------------------------#
        mesh = self.construct_mesh(test_params["nx0"], test_params["nx1"],
                                   test_params["nx2"], test_params["res"],
                                   project_path)

        if test_params["PLOT"] == True:
            # Plot and save the initial mesh
            if self.dim == 2:
                postprocess.plot_mesh(
                    mesh, project_path + results_folder_name + 'initial-mesh')
            elif self.dim == 3:
                postprocess.plot_mesh_3d(
                    mesh, project_path + results_folder_name + 'initial-mesh')

        boundary_facets, ds, dS = self.construct_boundary_faces(mesh)

        # Calculate the estimate for Freidrichs constant based on the domain
        C_FD = problem.calculate_CF_of_domain(domain, self.dim)
        C_Ntr = self.calculate_trace_constant()

        # Define functional spaces based on the mesh
        #--------------------------------------------------------------------------------------------------------------#
        V, VV, V_exact, VV_exact, H_div = \
            problem.functional_spaces(mesh, test_params, self.dim)

        # Define problem data functions
        eps = 1e0  # initialize the eps
        #--------------------------------------------------------------------------------------------------------------#
        f, A, invA, adjA, min_eig_A, lmbd, a, uD, uN, u_e, grad_u_e = self.convert_problem_data_to_expressions(
            mesh, test_params, eps)

        # Output to console the problem data
        problem.output_problem_characteristics(
            self.test_num, self.u_expr, self.grad_u_expr, self.f_expr,
            self.A_expr, self.lambda_expr, self.a_expr,
            self.uD_expr, self.domain, self.dim, mesh.num_cells(),
            mesh.num_vertices(), test_params["v_approx_order"],
            test_params["flux_approx_order"])

        # Run the solver with a posteriori error control
        # --------------------------------------------------------------------------------------------------------------#
        t0_problem = time.clock()

        while eps > self.eps:

            mesh, V, VV, V_exact, VV_exact, H_div, boundary_facets, ds, dS \
                = self.solve_problem_nd_t(mesh, boundary_facets, ds, dS,
                                    V, V_exact, VV_exact, H_div,
                                    u_e, f, A, invA, adjA, min_eig_A, lmbd, a, eps,
                                    uD, uN,
                                    C_FD, C_Ntr,
                                    project_path, results_folder_name, test_params)

            test_params['number_of_refinements'] = test_params[
                'number_of_refinements'] + 2
            #test_params['percentage_value'] = test_params['percentage_value'] + 0.1
            eps = 1e-1 * eps
            f, A, invA, adjA, min_eig_A, lmbd, a, uD, uN, u_e, grad_u_e = self.convert_problem_data_to_expressions(
                mesh, test_params, eps)
            print "\n\nmin_eig_A = ", min_eig_A
            print "\n\nupdated eps = ", eps
            print "\n\n"

        t1_problem = time.clock()
        print("time = %d" % (t1_problem - t0_problem))