Problem with coupled boundary conditions

Hello everyone,

I am working on incorporating a time-varying boundary condition into my PDE problem using FEniCSx. The weak form of my problem is as follows:

the weak form of the my problem is as follows:

\int_{domain} w(s) \left( \mu \mathbf{\ddot{r}(s)} - \mu \mathbf{g} \right) \, ds + \int_{domain} EA \left( \frac{\|\mathbf{r}'(s)\|_2 - 1}{\|\mathbf{r}'(s)\|_2} \mathbf{r}'(s) \right) \frac{d w(s)}{d s} \, ds - w(s) \mathbf{T}(s) \Big|_0^L = 0

The boundary conditions are dirlchet at s = 0 and at s = L is differential equation which is ,

-m \ddot{\mathbf{r}(l)} - \mathbf{T}(l) + m\mathbf{(g)} = 0`

In my FEniCSx implementation, I took the finite difference of the time derivative and wrote the variational form. I replaced T(L) in the weak form with the boundary condition. Here’s the corresponding code snippet:

# Weak form 
F = (
      ufl.inner(mu * ((r - 2 * r_n + r_n1) / dt**2),w)*ufl.dx - ufl.inner(mu*g,w)* ufl.dx
    + EA * ufl.inner(((norm_r_prime - 1) / norm_r_prime) * r_prime, ufl.grad(w)) * ufl.dx
    - ufl.inner(md* ((r - 2 * r_n + r_n1) / dt**2 ) - md*g,w)*ufl.ds
)   

Could anyone advise if this is the right way to handle the time-varying boundary condition that is coupled with the weak form?

Thank you in advance for any insights or corrections!

Blockquote

What about considering the methods used in
http://jsdokken.com/FEniCS-workshop/src/form_compilation.html
or
https://jsdokken.com/dolfinx-tutorial/chapter2/ns_code2.html

Thanks for sharing.I have gone through them.Those problems are catered towards linear problem.
Is there any similar resources for nonlinear problems?i tried Newton solver to solve the variational form i have outlined in above post but it is not converging.

The boundary conditions for a non-linear problem doesn’t really change how they should be set up.

Without having a code/implementation to consider, it is hard to give any further guidance.

here is the code snippet of my problem.I have written a variational form of my problem and setup a newton solver to solve it.

from dolfinx import mesh, fem ,default_real_type
from mpi4py import MPI
import ufl
from basix.ufl import element
import numpy as np
from petsc4py import PETSc
import dolfinx
import ufl.coefficient
from dolfinx.fem import dirichletbc
from dolfinx.nls.petsc import NewtonSolver
from dolfinx import default_real_type
from dolfinx.fem.petsc import NonlinearProblem
# Create 1D mesh for s in [0, L]
L = 1.0  # Length of the domain
num_elements = 100  # Number of elements in the 1D mesh
domain = mesh.create_interval(MPI.COMM_WORLD, num_elements, [0, L])

# Define a vector function space in 3D (Lagrange element of degree 1)
#element =ufl.element("Lagrange", domain.ufl_cell(), 1, dim=3)
typeelement = element("Lagrange", domain.basix_cell(),1,shape = (3,) ,dtype=default_real_type)
V = fem.functionspace(domain, typeelement)

# Define trial and test functions
r = fem.Function(V)  # Solution function (trial function)
w = ufl.TestFunction(V)  # Test function
r_prime = ufl.grad(r) [:,0]
norm_r_prime = ufl.sqrt(ufl.inner(r_prime, r_prime))

# Define constants
mu = fem.Constant(domain, 0.1)  # Mass density, example value
md = fem.Constant(domain, 1.0)
EA = fem.Constant(domain, 10000.0 ) # Stiffness, example value
gr= np.transpose([0,-9.81,0])
g = ufl.as_vector(gr)  # Gravity vector in 3D

# Fix r at s = 0 to be zero (Dirichlet condition)
def left_boundary(x):
    return np.isclose(x[0], 0)

#u_bc_left = ufl.as_vector( np.transpose([0,0,0]))
u_bc_left = fem.Constant(domain, PETSc.ScalarType((0.0, 0.0, 0.0)))
left_bc = dirichletbc(u_bc_left, fem.locate_dofs_geometrical(V, left_boundary), V)
bcs = [left_bc]
dt = 0.01  # Time step size
t = 0.0
T_end = 2  # Final time

# Define the nonlinear term T(s)
T = EA * ((norm_r_prime - 1) / norm_r_prime) * r_prime

# Time-dependent acceleration term (assuming some finite difference approximation for r_ddot)
r_n = fem.Function(V)  # Solution at previous time step
r_n1 = fem.Function(V)
r_n.interpolate(lambda x: np.vstack((x[0], 0 * x[0], 0 * x[0])))
r_n1.x.array[:] = r_n.x.array  # Zero initial velocity
r_prime = ufl.grad(r)

# Weak form definition
F = (
      ufl.inner(mu * ((r - 2 * r_n + r_n1) / dt**2),w)*ufl.dx - ufl.inner(mu*g,w)* ufl.dx
    +EA * ufl.inner(((norm_r_prime - 1) / norm_r_prime) * r_prime, ufl.grad(w)) * ufl.dx
    + ufl.inner(md* ((r - 2 * r_n + r_n1) / dt**2 ) - md*g,w)*ufl.ds
)   


# Create nonlinear problem
problem = NonlinearProblem(F, r, bcs)

# Create solver
solver = NewtonSolver(MPI.COMM_WORLD, problem)
solver.atol = 1e-2
solver.rtol = 1e-2


# Time-stepping loop
while t < T_end:
    # Update r_ddot (acceleration term) based on finite difference
    solver.solve(r)
    
    print(t)
    # Update previous solutions
    r_n1.x.array[:], r_n.x.array[:] =r_n.x.array, r.x.array


    # Update time
    t += dt

from dolfinx.io import XDMFFile

with XDMFFile(domain.comm, "solution.xdmf", "w") as file:
    file.write_mesh(domain)
    file.write_function(r)

` ``

I tried varying the timestep and error tolerance but it didn,t worked.

Adding:

dolfinx.log.set_log_level(dolfinx.log.LogLevel.DEBUG)

prior to your loop, you will get the output:


[2024-11-21 14:12:59.423] [info] Newton iteration 0: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.424] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.424] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.424] [info] Newton iteration 1: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.424] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.424] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.424] [info] Newton iteration 2: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.424] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.424] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.424] [info] Newton iteration 3: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.424] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.424] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.424] [info] Newton iteration 4: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.424] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.424] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.424] [info] Newton iteration 5: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.424] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.424] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.424] [info] Newton iteration 6: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.424] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.425] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.425] [info] Newton iteration 7: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.425] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.425] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.425] [info] Newton iteration 8: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.425] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.425] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.425] [info] Newton iteration 9: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.425] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.425] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.425] [info] Newton iteration 10: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.425] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.425] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.425] [info] Newton iteration 11: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.425] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.425] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.425] [info] Newton iteration 12: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.425] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.425] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.425] [info] Newton iteration 13: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.425] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.425] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.425] [info] Newton iteration 14: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.425] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.425] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.425] [info] Newton iteration 15: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.426] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.426] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.426] [info] Newton iteration 16: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.426] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.426] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.426] [info] Newton iteration 17: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.426] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.426] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.426] [info] Newton iteration 18: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.426] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.426] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.426] [info] Newton iteration 19: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.426] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.426] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.426] [info] Newton iteration 20: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.426] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.426] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.426] [info] Newton iteration 21: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.426] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.426] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.426] [info] Newton iteration 22: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.426] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.426] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.426] [info] Newton iteration 23: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.426] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.426] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.426] [info] Newton iteration 24: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.427] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.427] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.427] [info] Newton iteration 25: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.427] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.427] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.427] [info] Newton iteration 26: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.427] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.427] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.427] [info] Newton iteration 27: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.427] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.427] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.427] [info] Newton iteration 28: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.427] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.427] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.427] [info] Newton iteration 29: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.427] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.427] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.427] [info] Newton iteration 30: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.427] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.427] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.427] [info] Newton iteration 31: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.427] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.427] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.427] [info] Newton iteration 32: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.427] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.427] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.428] [info] Newton iteration 33: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.428] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.428] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.428] [info] Newton iteration 34: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.428] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.428] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.428] [info] Newton iteration 35: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.428] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.428] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.428] [info] Newton iteration 36: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.428] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.428] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.428] [info] Newton iteration 37: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.428] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.428] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.428] [info] Newton iteration 38: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.428] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.428] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.428] [info] Newton iteration 39: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.428] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.428] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.428] [info] Newton iteration 40: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.428] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.428] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.429] [info] Newton iteration 41: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.429] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.429] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.429] [info] Newton iteration 42: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.429] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.429] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.429] [info] Newton iteration 43: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.429] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.429] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.429] [info] Newton iteration 44: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.429] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.429] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.429] [info] Newton iteration 45: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.429] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.429] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.429] [info] Newton iteration 46: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.429] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.429] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.429] [info] Newton iteration 47: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.429] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.429] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.429] [info] Newton iteration 48: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.429] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.429] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.429] [info] Newton iteration 49: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:12:59.429] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:12:59.429] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
[2024-11-21 14:12:59.429] [info] Newton iteration 50: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
Traceback (most recent call last):
  File "/root/shared/mwe.py", line 87, in <module>
    solver.solve(r)
  File "/usr/local/dolfinx-real/lib/python3.12/dist-packages/dolfinx/nls/petsc.py", line 51, in solve
    n, converged = super().solve(u.x.petsc_vec)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Newton solver did not converge because maximum number of iterations reached

which means that your solver don’t even converge for the first iteration.

We can then add the following options to get deeper into the problem:

solver.krylov_solver.setType("preonly")
solver.krylov_solver.getPC().setType("lu")
solver.krylov_solver.getPC().setFactorSolverType("mumps")
solver.krylov_solver.setErrorIfNotConverged(True)

yielding

[2024-11-21 14:14:45.932] [info] Newton iteration 0: r (abs) = -nan (tol = 0.01), r (rel) = -nan (tol = 0.01)
[2024-11-21 14:14:45.932] [info] PETSc Krylov solver starting to solve system.
[2024-11-21 14:14:45.934] [info] PETSc error in '/src/dolfinx/cpp/dolfinx/la/petsc.cpp', 'KSPSolve'
[2024-11-21 14:14:45.934] [info] PETSc error code '76' 'Error in external library'
[2024-11-21 14:14:45.934] [debug] Elapsed wall, usr, sys time: 0.000000, 0.000000, 0.000000 (PETSc Krylov solver)
Traceback (most recent call last):
  File "/root/shared/mwe.py", line 92, in <module>
    solver.solve(r)
  File "/usr/local/dolfinx-real/lib/python3.12/dist-packages/dolfinx/nls/petsc.py", line 51, in solve
    n, converged = super().solve(u.x.petsc_vec)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Failed to successfully call PETSc function 'KSPSolve'. PETSc error code is: 76, Error in external library

We next assemble the residual F prior to the loop:

print(dolfinx.fem.assemble_vector(dolfinx.fem.form(F)).array)

yielding

array([nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
       nan, nan, nan, nan])

which means that something in F is singular.

One thing that stands out is that you define r_prime twice:

and your initial r is zero everwhere, meaning that dividing by norm_r_prime gives you something singular.

I hope this can serve as a guide on how to debug time dependent, non-linear problems in the future.

Thank you for your insight about debugging.i just initialized r with non zero value and problem got resolved.