Ejemplo n.º 1
0
 def __init__(self):
     info('Initializing Time control')
     # watch is list [total_time, last_start, message_when_measured, count into total time]
     self.watches = {}
     self.last_measurement = 0
     self.measuring = 0
     tic()
Ejemplo n.º 2
0
 def __init__(self):
     info('Initializing Time control')
     # watch is list [total_time, last_start, message_when_measured, count into total time, count into selected time]
     self.watches = {}
     self.last_measurement = 0
     self.measuring = 0
     tic()
Ejemplo n.º 3
0
def majorant_II_nd(u, Ve, w, W, y, f, u0, error_II, gamma, T, mesh, C_FD, dim,
                   v_deg, MAJORANT_OPTIMIZE):

    tic()

    m_d, m_f, l, L, m_T = update_majorant_II_components(
        u, w, y, f, u0, mesh, dim, v_deg, W, Ve, T)
    maj_II, beta = calculate_majorant_II(m_d, m_f, l, L, m_T, C_FD, gamma)
    i_eff = sqrt(maj_II / error_II)

    majorant_reconstruction_time = toc()
    output_optimization_results(-1, maj_II, m_d, m_f, i_eff, beta, error_II)

    if MAJORANT_OPTIMIZE:
        #----------------------------------------------------------------------------#
        # Optimization algorithm
        #----------------------------------------------------------------------------#
        tic()
        S, K, L, z, g = get_matrices_of_optimization_problem_II(
            W, u, y, f, u0, T, mesh, dim, v_deg)

        w = Function(W)
        W = w.vector()

        OPT_ITER = 4
        # Execute iterative process to optimize majorant with respect to beta and flux
        for k in range(1, OPT_ITER):

            #list_linear_solver_methods()
            #list_krylov_solver_preconditioners()

            # Solve system with respect to Y
            solve((1 + beta) * S - (C_FD**2) * beta / (1 + beta) * K, W,
                  -L - (1 + beta) * z - (C_FD**2) * beta /
                  (1 + beta) * g)  # lu 3.4 - 3.5
            # Calculate majorant
            m_d, m_f, l, L, m_T = update_majorant_II_components(
                u, w, y, f, u0, mesh, dim, v_deg, W, Ve, T)
            maj_II, beta = calculate_majorant_II(m_d, m_f, l, L, m_T, C_FD,
                                                 gamma)
            i_eff = sqrt(maj_II / error_II)

            output_optimization_results(k, maj_II, m_d, m_f, i_eff, beta,
                                        error_II)

        majorant_minimization_time = toc()
    else:

        majorant_minimization_time = 0.0

    return maj_II, beta, majorant_reconstruction_time, majorant_minimization_time
Ejemplo n.º 4
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
        else:
            for jj in xrange(0, N+1):
                phi_list.append(Expression("cos(pi*i*x[0]) * cos(pi*j*x[1])", i=ii, j=jj))
    return phi_list

# Import functionality from the folder 'lib'
import os, sys

lib_path = os.path.abspath('lib')
sys.path.append(lib_path)
import postprocess

# ---------------------------------------------------------------------------------#
# Calculate reference triangle constant C_hat_G and C_hat_P

tic()
h = 1

def f1_tan(x):
    return x*cos(x)/sin(x) + 1


def f2_tan(x):
    return tan(x) + tanh(x)

zeta_1 = scipy.optimize.newton(f1_tan, DOLFIN_PI/2)
zeta_2 = scipy.optimize.newton(f2_tan, DOLFIN_PI)

sigma_1 = abs(zeta_2 * tan(zeta_2))

C_hat_P = h / zeta_1
Ejemplo n.º 6
0
def majorant_nd_t(k, m_incr_k, m0_k, md_k, mf_k, beta_k, e_incr_k, ed_k, et_k,
                  f, sq_f, phi, quadr, 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):
    tic()

    rf_k = problem.Div(y_k, dim) - (v_k1 - v_k) / tau
    rf_k1 = problem.Div(y_k1, dim) - (v_k1 - v_k) / tau

    rd_k = y_k - grad_v_k
    rd_k1 = y_k1 - grad_v_k1

    sq_F_ = quadr.f(sq_f, V_exact)
    F_t_tk_ = quadr.f_t_tk(f, V_exact)
    F_tk1_t_ = quadr.f_tk1_t(f, V_exact)

    # Components of mf = || f + div y - v_t ||
    mf_y_k = (sq_F_ + Constant(2.0) / tau * inner(F_tk1_t_, rf_k) +
              tau / Constant(3.0) * inner(rf_k, rf_k)) * dx(domain=mesh)
    mf_y_k1 = (Constant(2.0) / tau * inner(F_t_tk_, rf_k1) +
               tau / Constant(3.0) *
               (inner(rf_k, rf_k1) + inner(rf_k1, rf_k1))) * dx(domain=mesh)

    # Components of md = || y - grad v ||
    md_y_k = (tau / Constant(3.0) * inner(rd_k, rd_k)) * dx(domain=mesh)
    md_y_k1 = (tau / Constant(3.0) *
               (inner(rd_k, rd_k1) + inner(rd_k1, rd_k1))) * dx(domain=mesh)

    m0 = assemble(((phi - v_k) * (phi - v_k)) * dx(domain=mesh))

    # Assemble components of dual and balance terms of majorant
    # which stays the same in optimization process
    Mf_y_k = assemble(mf_y_k)
    Md_y_k = assemble(md_y_k)
    Mf_y_k1, Md_y_k1 = update_majorant_on_k1_time_level(mf_y_k1, md_y_k1)
    m_d, m_f = update_majorant_components(Md_y_k, Md_y_k1, Mf_y_k, Mf_y_k1)

    maj, beta = calculate_majorant(m_d, m_f, C_FD, delta)
    i_eff = sqrt(maj / e_incr_k)

    majorant_reconstruction_time[k] = toc()
    output_optimization_results(-1, maj, m_d, m_f, i_eff, beta, e_incr_k, ed_k,
                                et_k)

    #----------------------------------------------------------------------------#
    # Optimization part
    #----------------------------------------------------------------------------#
    tic()
    S, K, z, q = get_matrices_of_optimization_problem(H_div, F_t_tk_, v_k,
                                                      v_k1, grad_v_k,
                                                      grad_v_k1, mesh, tau,
                                                      dim)

    OPT_ITER = 4
    for opt_iter in range(1, OPT_ITER):

        # Solve system with respect to Y
        A = (C_FD**2) * S + beta * K
        b = -0.5 * A * y_k.vector() - C_FD**2 * 3.0 / (tau**2) * z + beta * q
        #solve(A, y_k1.vector(), b, "gmres", 'ilu')
        solve(A, y_k1.vector(), b)

        mf_y_k1 = (
            Constant(2.0) / tau * inner(F_t_tk_, rf_k1) + tau / Constant(3.0) *
            (inner(rf_k, rf_k1) + inner(rf_k1, rf_k1))) * dx(domain=mesh)
        md_y_k1 = (tau / Constant(3.0) *
                   (inner(rd_k, rd_k1) + inner(rd_k1, rd_k1))) * dx(
                       domain=mesh)
        Mf_y_k1, Md_y_k1 = update_majorant_on_k1_time_level(mf_y_k1, md_y_k1)
        m_d, m_f = update_majorant_components(Md_y_k, Md_y_k1, Mf_y_k, Mf_y_k1)

        maj, beta = calculate_majorant(m_d, m_f, C_FD, delta)
        i_eff = sqrt(maj / e_incr_k)
        output_optimization_results(opt_iter, maj, m_d, m_f, i_eff, beta,
                                    e_incr_k, ed_k, et_k)
    majorant_minimization_time[k] = toc()

    md_k[k] = m_d
    mf_k[k] = m_f
    beta_k[k] = beta
    m_incr_k[k] = maj
    m0_k[k] = m0

    rf_k = problem.Div(y_k, dim) - (v_k1 - v_k) / tau
    rf_k1 = problem.Div(y_k1, dim) - (v_k1 - v_k) / tau

    rd_k = y_k - grad_v_k
    rd_k1 = y_k1 - grad_v_k1

    md_var = tau / Constant(3.0) * (inner(rd_k, rd_k) + inner(rd_k, rd_k1) +
                                    inner(rd_k1, rd_k1))
    mf_var = 1.0 / delta * (
        (1.0 + beta) * md_var + (1.0 + 1.0 / beta) * (C_FD**2) *
        (sq_F_ + Constant(2.0) / tau *
         (inner(F_tk1_t_, rf_k) + inner(F_t_tk_, rf_k1)) +
         tau / Constant(3.0) *
         (inner(rf_k, rf_k) + inner(rf_k, rf_k1) + inner(rf_k1, rf_k1))))

    return m_incr_k, m0_k, md_k, mf_k, beta_k, y_k1, md_var, mf_var
Ejemplo n.º 7
0
def majorant_nd(u, Ve, y, H_div, f, A, invA, min_eig_A, c_H, eps, lmbd, a, u0,
                error, mesh, C_FD, dim, test_params):

    tic()

    u0_0, O_mesh = get_2d_slice_of_3d_function_on_Oz(
        mesh,
        interpolate(u0, Ve),
        0,
        dim,
        test_params["v_approx_order"],
    )
    u_0, O_mesh = get_2d_slice_of_3d_function_on_Oz(
        mesh,
        interpolate(u, Ve),
        0,
        dim,
        test_params["v_approx_order"],
    )
    r_b = abs(u0_0 - u_0)
    m_b = assemble(inner(r_b, r_b) * dx(domain=O_mesh))

    # Define residuals
    beta = 1.0
    f_bar = f - c_H * D_t(u, dim) - lmbd * u - inner(a, NablaGrad(u, dim))
    maj, m_d, m_f, beta, var_m_d, var_m_f_w_opt = calculate_majorant_bar_mu_opt(
        u, y, beta, f_bar, A, invA, min_eig_A, lmbd, mesh, dim, C_FD)

    i_eff = sqrt(maj / error)

    print " "
    print '%------------------------------------------------------------------------------------%'
    print "% Majorant before optimization"
    print '%------------------------------------------------------------------------------------%'
    print " "

    majorant_reconstruction_time = toc()
    output_optimization_results(-1, maj, m_d, m_f, i_eff, beta, error)

    if test_params['MAJORANT_OPTIMIZE']:
        #----------------------------------------------------------------------------#
        # Optimization algorithm
        #----------------------------------------------------------------------------#
        print " "
        print "%-----------------------"
        print "% optimization "
        print "%-----------------------"
        print " "

        tic()
        #S, K, z, g = get_matrices_of_optimization_problem(H_div, u, f, c_H, mesh, dim)
        S, K, z, g = get_matrices_of_optimization_problem(
            H_div, u, f_bar, invA, mesh, dim)

        y = Function(H_div)
        Y = y.vector()

        # Execute iterative process to optimize majorant with respect to beta and flux
        for k in range(1, test_params['majorant_optimization_iterations']):

            #list_linear_solver_methods()
            #list_krylov_solver_preconditioners()

            # Solve system with respect to Y
            solve((C_FD**2) / min_eig_A * S + beta * K, Y,
                  (C_FD**2) / min_eig_A * z + beta * g)  # lu 3.4 - 3.5
            #solve((C_FD ** 2) * S + beta * K, Y, (C_FD ** 2) * z + beta * g, "gmres", "jacobi") # 3.4
            #solve((C_FD ** 2) * S + beta * K, Y, (C_FD ** 2) * z + beta * g, "cg", "ilu") # 3.4
            #solve(C_FD * S + beta * K, Y, C_FD * z + beta * g, "cg", "jacobi")
            #solve(C_FD * S + beta * K, Y, C_FD * z + beta * g, "gmres", "hypre_amg") # 3.4 - 3.5

            y.vector = Y
            # Update majorant
            maj, m_d, m_f, beta, var_m_d, var_m_f_w_opt = calculate_majorant_bar_mu_opt(
                u, y, beta, f_bar, A, invA, min_eig_A, lmbd, mesh, dim, C_FD)
            i_eff = sqrt(maj / error)

            output_optimization_results(k, maj, m_d, m_f, i_eff, beta, error)

        majorant_minimization_time = toc()
    else:

        majorant_minimization_time = 0.0

    return maj, y, beta, m_d, m_f, var_m_d, var_m_f_w_opt, majorant_reconstruction_time, majorant_minimization_time
Ejemplo n.º 8
0
def majorant_nd(v, y, H_div, f, A, invA, min_eig_A, eps, a, lmbd, error, mesh,
                dim, C_FD, C_Ntr, problem_params):
    """
    :param v: approximate solution
    :param y: flux
    :param H_div: funtional space for the flux function
    :param f: right-hand side function (modified RHS)
    :param A: diffusion matrix
    :param min_eig_A: minimal eigenvalue of diffusion matrix
    :param a: convection function
    :param lmbd: reaction function
    :param error: error value
    :param mesh: mesh
    :param dim: geometrical problem dimension
    :param C_FD: Freidrichs constant of the computational domain
    :param MAJORANT_OPTIMIZE: test_parameter on weather to optimize majorant of not
    :return maj: majorant value
            m_d, m_f_one_minus_mu_opt: majorant components
            beta: optimal parameter
            var_m_d, var_m_f_w_opt: variational form of the majorant (to use them later in construction of error estimators)
            maj, y, beta, m_d, m_f, var_m_d, var_m_f_w_opt, majorant_reconstruction_time, majorant_minimization_time
    """

    tic()

    # Initialize value
    beta = 1.0
    #for i in range(2):
    f_bar = f - lmbd * v - inner(a, Grad(v, dim))
    #f_bar = f
    maj, m_d, m_f, beta, var_m_d, var_m_f_w_opt = calculate_majorant_bar_mu_opt(
        v, y, beta, f_bar, A, invA, min_eig_A, lmbd, mesh, dim, C_FD)
    i_eff = sqrt(maj / error)
    print " "
    print '%------------------------------------------------------------------------------------%'
    print "% Majorant optimization"
    print '%------------------------------------------------------------------------------------%'
    print " "

    info(parameters, verbose=True)

    print(parameters.linear_algebra_backend)
    list_linear_solver_methods()
    list_krylov_solver_preconditioners()

    solver = KrylovSolver('gmres', 'ilu')
    prm = solver.parameters
    prm.absolute_tolerance = 1e-10
    prm.relative_tolerance = 1e-6
    prm.maximum_iterations = 1000

    output_optimization_results(0, maj, m_d, m_f, i_eff, beta, error)

    majorant_reconstruction_time = toc()

    if problem_params["MAJORANT_OPTIMIZE"]:
        #----------------------------------------------------------------------------#
        # Optimization algorithm
        #----------------------------------------------------------------------------#

        tic()
        S, K, z, g = get_matrices_of_optimization_problem_bar(
            H_div, v, f_bar, invA, mesh, dim)

        y = Function(H_div)
        Y = y.vector()

        OPT_ITER = problem_params["majorant_optimization_iterations"]
        i_eff = 1e8
        k = 1
        m_d = 1.0
        m_f = 10.0

        # Execute iterative process to optimize majorant with respect to beta and flux
        while k in range(0, OPT_ITER):  # m_d * min_eig_A /m_f <= 1: #or
            # Solve system with respect to Y
            #yMatrix = (C_FD ** 2) / min_eig_A * S + beta * K
            #yRhs = sum((C_FD ** 2) / min_eig_A * z, beta * g)
            #solve(yMatrix, Y, yRhs)

            solve((C_FD**2) / min_eig_A * S + beta * K, Y,
                  (C_FD**2) / min_eig_A * z + beta * g)

            #if len(Y) <= 1e4:
            #    solve((C_FD ** 2) / min_eig_A * S + beta * K, Y, (C_FD ** 2) / min_eig_A * z + beta * g)
            #else:
            #    solver.solve((C_FD ** 2) / min_eig_A * S + beta * K, Y, (C_FD ** 2) / min_eig_A * z + beta * g,
            #      "gmres", "ilu")

            y.vector = Y
            """
            YtSY = assemble(inner(Div(y, dim), Div(y, dim)) * dx(domain=mesh))
            YtSz2 = assemble(2 * inner(Div(y, dim), f) * dx(domain=mesh))
            FF = assemble(inner(f, f) * dx(domain=mesh))

            print "Y^T*S*Y", YtSY
            print "2*Y^T*z", YtSz2
            print "FF", FF
            print "\| div y + f \|^2", YtSY + YtSz2 + FF

            YtY = assemble(inner(y, y) * dx(domain=mesh))
            YtSz2 = assemble(2 * inner(Grad(v, dim), y) * dx(domain=mesh))
            GradVGradV = assemble(inner(Grad(v, dim), Grad(v, dim)) * dx(domain=mesh))
            """

            # Calculate majorant
            maj, m_d, m_f, beta, var_m_d, var_m_f_w_opt = calculate_majorant_bar_mu_opt(
                v, y, beta, f_bar, A, invA, min_eig_A, lmbd, mesh, dim, C_FD)
            i_eff = sqrt(maj / error)

            output_optimization_results(k, maj, m_d, m_f, i_eff, beta, error)
            k = k + 1

        majorant_minimization_time = toc()

    else:

        majorant_minimization_time = 0.0

    return maj, y, beta, m_d, m_f, var_m_d, var_m_f_w_opt, majorant_reconstruction_time, majorant_minimization_time
Ejemplo n.º 9
0
def majorant_nd(v, y, H_div, V, VV, f, A, invA, lambda_1, a, lmbd, error, mesh,
                dim, C_FD, C_Ntr, problem_params):
    """
    :param v: approximate solution
    :param y: flux
    :param H_div: funtional space for the flux function
    :param f: right-hand side function (modified RHS)
    :param A: diffusion matrix
    :param lambda_1: minimal eigenvalue of diffusion matrix
    :param a: convection function
    :param lmbd: reaction function
    :param error: error value
    :param mesh: mesh
    :param dim: geometrical problem dimension
    :param C_FD: Freidrichs constant of the computational domain
    :param MAJORANT_OPTIMIZE: test_parameter on weather to optimize majorant of not
    :return maj: majorant value
            m_d, m_f_one_minus_mu_opt: majorant components
            beta: optimal parameter
            var_m_d, var_m_f_w_opt: variational form of the majorant (to use them later in construction of error estimators)
            maj, y, beta, m_d, m_f, var_m_d, var_m_f_w_opt, majorant_reconstruction_time, majorant_minimization_time
    """

    tic()

    # Initialize value
    beta = 1.0
    #for i in range(2):
    f_bar = f - lmbd * v - inner(a, Grad(v, dim))
    #f_bar = f
    maj, m_d, m_f, beta, var_m_d, var_m_f_w_opt = calculate_majorant_bar_mu_opt(
        v, y, beta, f_bar, A, invA, lambda_1, lmbd, mesh, dim, C_FD)
    i_eff = sqrt(maj / error)
    print " "
    print '%------------------------------------------------------------------------------------%'
    print "% Majorant before optimization"
    print '%------------------------------------------------------------------------------------%'
    print " "

    output_optimization_results(0, maj, m_d, m_f, i_eff, beta, error)

    majorant_reconstruction_time = toc()

    if problem_params["MAJORANT_OPTIMIZE"]:
        #----------------------------------------------------------------------------#
        # Optimization algorithm
        #----------------------------------------------------------------------------#
        print " "
        print "%-----------------------"
        print "% optimization "
        print "%-----------------------"
        print " "

        tic()
        S, K, z, g = get_matrices_of_optimization_problem_bar(
            H_div, v, f_bar, invA, mesh, dim)

        y = Function(H_div)
        Y = y.vector()

        OPT_ITER = problem_params["majorant_optimization_iterations"]
        # Execute iterative process to optimize majorant with respect to beta and flux
        for k in range(0, problem_params["majorant_optimization_iterations"]):
            # Solve system with respect to Y
            #yMatrix = (C_FD ** 2) / lambda_1 * S + beta * K
            #yRhs = sum((C_FD ** 2) / lambda_1 * z, beta * g)
            #solve(yMatrix, Y, yRhs)

            solve((C_FD**2) / lambda_1 * S + beta * K, Y,
                  (C_FD**2) / lambda_1 * z + beta * g)

            y.vector = Y
            '''

            YtSY = assemble(inner(Div(y, dim), Div(y, dim)) * dx(domain=mesh))
            YtSz2 = assemble(2 * inner(Div(y, dim), f) * dx(domain=mesh))
            FF = assemble(inner(f, f) * dx(domain=mesh))

            print "Y^T*S*Y", YtSY
            print "2*Y^T*z", YtSz2
            print "FF", FF
            print "\| div y + f \|^2", YtSY + YtSz2 + FF

            YtY = assemble(inner(y, y) * dx(domain=mesh))
            YtSz2 = assemble(2 * inner(Grad(v, dim), y) * dx(domain=mesh))
            GradVGradV = assemble(inner(Grad(v, dim), Grad(v, dim)) * dx(domain=mesh))

            print "YtY", YtY
            print "YtSz2", YtSz2
            print "GradVGradV", GradVGradV
            print "\| y - grad v \|^2", YtY - YtSz2 + GradVGradV

            print "\| v \|", (norm(v, 'L2'))**2
            print "\| grad v \|", (norm(v, 'H1'))**2
            print "\| div y \|", (norm(project(div(y), V), 'L2'))**2
            print "\| f \|", (norm(project(f, V), 'L2'))**2
            '''
            # Calculate majorant
            maj, m_d, m_f, beta, var_m_d, var_m_f_w_opt = calculate_majorant_bar_mu_opt(
                v, y, beta, f_bar, A, invA, lambda_1, lmbd, mesh, dim, C_FD)
            i_eff = sqrt(maj / error)

            output_optimization_results(k + 1, maj, m_d, m_f, i_eff, beta,
                                        error)

        majorant_minimization_time = toc()

    else:

        majorant_minimization_time = 0.0

    return maj, y, beta, m_d, m_f, var_m_d, var_m_f_w_opt, majorant_reconstruction_time, majorant_minimization_time