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

Is it possible to print certain values, say norm of u (displacement), while Newton Solver is running?

+2 votes

Is there any parameter that can be used to perform such a function? Or should one write his/her own newton solver?

asked Jun 5, 2017 by vivekkr1809 FEniCS Novice (510 points)

1 Answer

+2 votes
 
Best answer

Consider the following based on the nonlinear Poisson demo. Instead of plotting you could print out a functional of u:

from dolfin import *

class Problem(NonlinearProblem):
    def __init__(self, a, L, bcs):
        NonlinearProblem.__init__(self)
        self.a = a
        self.L = L
        self.bcs = bcs

    def F(self, b, x):
        assembler = SystemAssembler(self.a, self.L, self.bcs)
        assembler.assemble(b, x)

    def J(self, A, x):
        assembler = SystemAssembler(self.a, self.L, self.bcs)
        assembler.assemble(A)


class CustomSolver(NewtonSolver):
    def __init__(self, u):
        self.u = u
        NewtonSolver.__init__(self)

    def update_solution(self, x, dx, relaxation, problem, k):
        x.axpy(-1.0, dx)
        plot(self.u)
        interactive()


# Create mesh and define function space
mesh = UnitSquareMesh(32, 32)

V = FunctionSpace(mesh, "CG", 1)

# Define boundary condition
g = Constant(1.0)
bc = DirichletBC(V, g, "on_boundary")

# Define variational problem
u = Function(V)
v = TestFunction(V)
f = Constant(10.0)
F = inner((1 + u**2)*grad(u), grad(v))*dx - f*v*dx
J = derivative(F, u)
problem = Problem(J, F, [bc])
solver = CustomSolver(u)
solver.solve(problem, u.vector())

# Plot solution and solution gradient
plot(u, title="Solution")
interactive()
answered Jun 5, 2017 by nate FEniCS Expert (17,050 points)
selected Jun 6, 2017 by vivekkr1809

Hi Nate,

Maybe I am missing something but the following line in your code does not perform as expected. It does not plot anything.

def update_solution(self, x, dx, relaxation, problem, k):
    x.axpy(-1.0, dx)
    plot(self.u)
    interactive()

Any suggestions??

Are you running the latest version of dolfin?

I am currently using 2016.2.0 .

You may need to update to 2017.2.0. The above relies on recent updates.

Thanks a lot. I could, for some reason I don't know about, only update it to 2017.1.0 but it worked.

...