Time dependent non linear variational problem

Hi everyone,

I am facing problem in implementation of time dependent non linear pde. The following error I am getting.
Traceback (most recent call last):
File “demo_nonlinear-poisson.py”, line 79, in
J = derivative(F , u)
File “/usr/lib/python3/dist-packages/dolfin/fem/formmanipulations.py”, line 80, in derivative
raise RuntimeError(“Computing derivative of form w.r.t. ‘{}’. Supply Function as a Coefficient”.format(u))
RuntimeError: Computing derivative of form w.r.t. ‘v_1’. Supply Function as a Coefficient

Here is my full code :

import matplotlib.pyplot as plt
from fenics import *
import numpy as np
import time

T = 1.0 # final time
num_steps = 10 # number of time steps
dt = T / num_steps # time step size

if has_linear_algebra_backend(“Epetra”):
parameters[“linear_algebra_backend”] = “Epetra”

def boundary(x, on_boundary):
return on_boundary

Create mesh and define function space

mesh = UnitSquareMesh(32, 32)
File(“mesh.pvd”) << mesh

V = FunctionSpace(mesh, “CG”, 1)

u_0 = Expression(‘x[0]x[1](x[0]-1)*(x[1]-1)’, degree=1)
u_n = interpolate(u_0,V)

Define boundary condition

bc = DirichletBC(V, Constant(0) , boundary)

Define variational problem

u = TrialFunction(V)
v = TestFunction(V)
f = Expression('2x[1](1-x[1]) + 2x[1]x[1]x[1](x[1]-1)(x[1]-1)(x[1]-1)x[0](1-x[0])(5x[0]x[0]-5x[0]+1) + 2x[0](1-x[0]) +2x[0]x[0]x[0](x[0]-1)(x[0]-1)(x[0]-1)x[1](1-x[1])(5x[1]x[1]-5x[1]+1) + x[0]x[1](x[0]-1)(x[0]-1)exp(t)’, degree=2 , t=0)
F = u
v
dx + dt*inner((1 + u**2)*grad(u), grad(v))dx - (u_n + dtf)vdx

J = derivative(F , u)

Save solution in VTK format

file = File(“nonlinear_poisson.pvd”)
u = Function(V)
t = 0
for n in range(num_steps):
t += dt

Compute solution

solve(F == 0, u, bc)
file << (u,t)
plot(u)
plt.show()
u_n.assign(u)

Thank You in advance.

For non-linear problems, u has to be a Function, not a TrialFunction

Now I am getting the following one.

No Jacobian form specified for nonlinear variational problem.
Differentiating residual form F to obtain Jacobian J = F’.
Solving nonlinear variational problem.
Newton iteration 0: r (abs) = 3.311e-03 (tol = 1.000e-10) r (rel) = 1.000e+00 (tol = 1.000e-09)
Traceback (most recent call last):
File “demo_nonlinear-poisson.py”, line 45, in
solve(F == 0, u, bc)
File “/usr/lib/python3/dist-packages/dolfin/fem/solving.py”, line 220, in solve
_solve_varproblem(*args, **kwargs)
File “/usr/lib/python3/dist-packages/dolfin/fem/solving.py”, line 266, in _solve_varproblem
solver.solve()
RuntimeError:

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at


*** fenics-support@googlegroups.com


*** Remember to include the error message listed below and, if possible,
*** include a minimal running example to reproduce the error.


*** -------------------------------------------------------------------------
*** Error: Unable to successfully call PETSc function ‘MatSetValuesLocal’.
*** Reason: PETSc error code is: 63 (Argument out of range).
*** Where: This error was encountered inside /build/dolfin-LDAJTl/dolfin-2019.1.0/dolfin/la/PETScMatrix.cpp.
*** Process: 0


*** DOLFIN version: 2019.1.0
*** Git changeset: unknown
*** -------------------------------------------------------------------------

You are probably definition function u twice in your code, make sure you are using the same as the one in the variational problem for in the solve command

Thanku so much for your reply. I also want to ask you that in my code (given above) whether at each iteration f is also changing or not ? I am not able to figure it out. f is not the constant function so it is of the form f^(n) which should be changing at each iteration. But I don’t know whether it is happening here or not?

As you have not formatted you code properly (using ``` and appropriate indentation ) i cant say for certain. But as you do u_n.assign(u) it should update.

My problem is as follows:
u_t - div((1+u^2)grad(u)) = f , u=0 on boundary and u= u0 at t=0
I am taking u = x(x-1)y(y-1)e^t be the exact solution . From above pde we can solve f(x,y,t)
After applying backward euler method , the variational formulation is as follows:
F = uvdx + dt*dot((1+u**2)*grad(u) , grad(v))dx - (u_n + dt f)vdx

Here is my complete code.

from future import print_function
import matplotlib.pyplot as plt
import numpy as np
from fenics import *
import time

T = 1.0 # final time
num_steps = 10 # number of time steps
dt = T / num_steps # time step size

#if has_linear_algebra_backend(“Epetra”):

parameters[“linear_algebra_backend”] = “Epetra”

def boundary(x, on_boundary):
return on_boundary

Create mesh and define function space

mesh = UnitSquareMesh(16,16)
File(“mesh.pvd”) << mesh

V = FunctionSpace(mesh, “CG”, 1)

u_D = Expression(‘x[0]x[1](x[0]-1)*(x[1]-1)*exp(t)’, degree=2 , t=0)

Define boundary condition

bc = DirichletBC(V, u_D , boundary)
u_n = interpolate(u_D,V)

Define variational problem

u = Function(V)
v = TestFunction(V)
f = Expression(’(2x[1](1-x[1]) + 2x[1]x[1]x[1](x[1]-1)(x[1]-1)(x[1]-1)x[0](1-x[0])(5x[0]x[0]-5x[0]+1) + 2x[0](1-x[0]) +2x[0]x[0]x[0](x[0]-1)(x[0]-1)(x[0]-1)x[1](1-x[1])(5x[1]x[1]-5x[1]+1))exp(t) + x[0]x[1](x[0]-1)(x[0]-1)exp(t)’, degree=2 , t=0)
F = u
vdx + dtinner((1 + u**2)grad(u), grad(v))dx - u_nvdx - dtfv*dx
J = derivative(F , u)

Save solution in VTK format

file = File(“nonlinear_poisson.pvd”)
t = 0
for n in range(num_steps):
t += dt
u_D.t = t

Compute solution

solve(F == 0, u, bc ,solver_parameters={“newton_solver”:{“relative_tolerance”:1e-6}})
plot(u)
u_e = interpolate(u_D, V)
error = np.abs(np.array(u_e.vector()) - np.array(u.vector())).max()
print(‘t = %.2f: error = %.3g’ % (t, error))
file << (u,t)
u_n.assign(u)
plot(u)
plt.show()

With increase in mesh size error increased and also they are showing error only for t = 1 .Whether there is some mistake in my code or not? Kindly help me out. Thank you.

As I said, format your code appropriately, encapsulating it with ``` and make sure indentation is correct. One should be able to do a copy and paste to run the example without any changes, see Read before posting: How do I get my question answered?