BilinearForm.AssembleLinearization( ... ) crashes

Hi!
I want to solve a nonlinear nonsymmetric system of equation via a newton raphson iteration.
The problem is defined as:

fesCurl_cplx = HCurl(mesh, order = 3, nograds = True, complex = True)
fes1_cplx_iron = H1(mesh, order = 3, complex = True)
fesMixed = FESpace([fesCurl_cplx, fes1_cplx_iron])
u_A, u_Psi = fesMixed.TrialFunction()
v_A, v_Psi = fesMixed.TestFunction()
sysmat_Mixed = BilinearForm(fesMixed)
sysmat_Mixed += SymbolicBFI(
mu_invcurl(u_A)curl(v_A) +
1j
omega
sigmav_Au_A +
1jomegasigmav_Agrad(u_Psi)u_Psi +
1j
omegasigmagrad(v_Psi)grad(u_Psi) +
1j
omegasigmagrad(v_Psi)u_A)
rhs_Mixed = LinearForm(fesMixed)
rhs_Mixed += SymbolicLFI(v_A
j_coil)
rhs_Mixed.Assemble()
sol_Mixed = GridFunction(fesMixed, name = “Mixed”)

The Newton Raphson iteration works like this:

lhs = sol_Mixed.vec.CreateVector()
res = sol_Mixed.vec.CreateVector()
dsol = sol_Mixed.vec.CreateVector()
for it in range(10):
# calculate residual vector
sysmat_Mixed.Apply(sol_Mixed.vec, lhs)
res.data = rhs_Mixed.vec - lhs

# create preconditioner
pre = Preconditioner(sysmat_Mixed, "bddc") # "bddc", "multigrid"

# assemble linearized system matrix
sysmat_Mixed.AssembleLinearization(sol_Mixed.vec)

# solve linear system of equations
solvers.GMRes(A = sysmat_Mixed.mat, pre = pre.mat, b = res, 
           x = dsol, printrates = True, tol = 1e-10, maxsteps = 200)

# update solution
sol_Mixed.vec.data -= dsol

# check convergence
resval = sqrt(abs(InnerProduct(dsol, res)))
print ("residual = ", resval)
if resval < 1e-8:
    break

For the first iteration loop this works fine but when assembling the linearization for the second time, the program crashes.
How can I fix that?

Thanks!
Nils

It would be easier to debug if you provide the python code.

Here is a stand-alone example.
The Newton Raphson is at the end of the file.
Thanks for the help!

https://ngsolve.org/media/kunena/attachments/1180/TEAM23_nonlinear.py

Attachment: TEAM23_nonlinear.py

I got the code running by pulling out the preconditioner line outside the loop…
not sure what’s going on internally, maybe the developers can answer that.

You are right, this was the problem. The BilinearForm keeps pointers to the preconditioners and updates them automatically if the BF is reassembled. But it doesn’t keep the Preconditioners alive and it didn’t yet unregister them on preconditioner destruction. This is what caused the segfault. I’ll push a fix for that in the next days, but keep the preconditioner out anyway, since it is more efficient (there has to be no allocation of memory,…).
Here you can see in an example that the preconditioner is updated on the call of a.Assemble, the same happens for assemblelinearization.

Best Christopher