This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

find something strange(maybe a bug) in computing convergence rate in adaptive solver

0 votes

There are two pieces of exactly same code among the comment symbol "#############".

But the first one is good, the second one results in Segmentation fault. I don't know why.

Thanks in advance.

The source code is below:

from dolfin import *

def my_adaptive_solver(u_e, f, mesh):
    V = FunctionSpace(mesh, "Lagrange", 1)

    bc = DirichletBC(V, u_e, DomainBoundary())

    u = TrialFunction(V)
    v = TestFunction(V)
    a = inner(grad(u), grad(v))*dx()
    L = f*v*dx

    # Define function for the solution
    u = Function(V)
    # Define goal functional (quantity of interest)
    M = u*dx()
    tol = 1.e-4

    problem = LinearVariationalProblem(a, L, u, bc)
    solver = AdaptiveLinearVariationalSolver(problem, M)
    solver.parameters['max_iterations'] = 20
    solver.solve(tol)
    #solver.summary()
    #data = solver.adaptive_data()

    u_h_list = []; u_h_list.append(u.root_node())
    num_dofs_list = []; num_dofs_list.append(u.root_node().function_space().dim())
    temp = u.root_node()
    while temp.has_child():
        u_h_list.append(temp.child())
        num_dofs_list.append(temp.child().function_space().dim())
        temp = temp.child()
##############################
    error = [errornorm(u_e, u_h_list[i], norm_type='H10') for i in range(len(u_h_list))]
    h = [num_dofs_list[i]**(-1.0/2) for i in range(len(num_dofs_list))]
    rate = []
    from math import log as ln
    for i in range(len(u_h_list) - 1):
        rate.append(ln(error[i]/error[i+1]) / ln(h[i]/h[i+1]))
    print rate
##############################

    return u_h_list, num_dofs_list


if __name__ == '__main__':
    import pdb
    pdb.set_trace()
    mesh = UnitSquareMesh(2, 2)
    u_e = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]', degree=2)
    f = Constant(-6.0)

    u_h_list, num_dofs_list = my_adaptive_solver(u_e, f, mesh)

##############################
    error = [errornorm(u_e, u_h_list[i], norm_type='H10') for i in range(len(u_h_list))]
    h = [num_dofs_list[i]**(-1.0/2) for i in range(len(num_dofs_list))]
    rate = []
    from math import log as ln
    for i in range(len(u_h_list) - 1):
        rate.append(ln(error[i]/error[i+1]) / ln(h[i]/h[i+1]))
    print rate
##############################
asked Jun 26, 2017 by fanronghong FEniCS User (1,680 points)
...