# ValueError: Found Argument in <Cos id=6262508512>, this is an invalid expression

Hello, I am trying to use FEniCsX to solve a time-dependent PDE with a non-linear coefficient. However, I am getting an error stating: “ValueError: Found Argument in Cos id=6262508512, this is an invalid expression.” …when I formulate my variational problem `(a,L)=system(F)`. I am wondering if this is because system() sets up a linear system to solve with a linear solver? Also can NonLinearSolver() be used to solve time-dependent PDEs? My PDE is essentially a Fokker-plank equation with a non-linear drift (or velocity) term. Please find my code below. Any help/clarification would be greatly appreciated. Thanks in advance!

``````
import numpy as np
import matplotlib

import matplotlib.pyplot as plt

from dolfinx import mesh, fem, io, plot, la
import pyvista
import ufl
import numpy as np
from dolfinx import *
import dolfinx
import numpy, sys

from mpi4py import MPI
from petsc4py import PETSc
from ufl import (VectorElement, FiniteElement,
SpatialCoordinate, TrialFunction, TestFunction,
as_vector, cos, sin, inner, div, grad, dx, pi, system)

from dolfinx import mesh, fem, io, nls, log
from dolfinx.fem.petsc import NonlinearProblem
from dolfinx.nls.petsc import NewtonSolver

##cretae a rectangular mesh with quadrilateral elements
##note: here theta=x

from mpi4py import MPI
length, height = 10, 3
Nx, Ny = 80, 60
extent = [[0., 0.], [length, height]]
domain = mesh.create_rectangle(

##define the non-linear coeff
omega=(np.pi/2)
##define the non-linear coefficient
def vel(x):
return omega + cos(x)

##setting up the variational problem
from ufl import (TestFunction, SpatialCoordinate, TrialFunction,
as_vector, dx, grad, inner, system, equation, Constant)
V = fem.FunctionSpace(domain, ("Lagrange", 1))
u = TrialFunction(V) ##time-dep c at n+1
v = TestFunction(V)
un = fem.Function(V) ##time dep C at n
D= fem.Constant(domain, 1.0) ##diffusion constant
dt = fem.Constant(domain, 0.05)

##define the variational problem
F= (un*v*ufl.dx) - (v*ufl.dx) ##note: i think it should be  L=un*v -v*dx
#variational problem stated as a=L
(a,L)=system(F)`````````

Hi

This is a non-linear problem, so considering `u=TrialFunction(V)` is wrong for `system(F)` unless you linearize and intend to implement manually a fixed-point iteration or a Newton iteration. Please see how @dokken implemented a Non-linear Poisson equation in the Dolfinx tutorial. One of the most important things is that you need to redefine `u` as `u=fem.Function(V)`. Also, be aware that using the class `NonlinearProblem` will give you an `ArityMismatch` error because you have non-linear terms involving TrialFunctions like `cos(u)*u*v*dt*dx`.

I’m unfamiliar with the type of PDE that your are solving, but the following runs with no error. I’m not sure about the expression `u*dx` (maybe is related to your previous post ?)

``````from ufl import (TestFunction, SpatialCoordinate, TrialFunction,
as_vector, dx, grad, inner, system, equation, Constant)
import numpy as np
import matplotlib

import matplotlib.pyplot as plt

from dolfinx import mesh, fem, io, plot, la
# import pyvista
import ufl
import numpy as np
from dolfinx import *
import dolfinx
import numpy
import sys

from mpi4py import MPI
from petsc4py import PETSc
from ufl import (VectorElement, FiniteElement,
SpatialCoordinate, TrialFunction, TestFunction,
as_vector, cos, sin, inner, div, grad, dx, pi, system)

from dolfinx import mesh, fem, io, nls, log
from dolfinx.fem.petsc import NonlinearProblem
from dolfinx.nls.petsc import NewtonSolver

# cretae a rectangular mesh with quadrilateral elements
# note: here theta=x

from mpi4py import MPI
length, height = 10, 3
Nx, Ny = 80, 60
extent = [[0., 0.], [length, height]]
domain = mesh.create_rectangle(

# define the non-linear coeff
omega = (np.pi/2)
# define the non-linear coefficient

def vel(x):
return omega + cos(x)

# setting up the variational problem
V = fem.FunctionSpace(domain, ("Lagrange", 1))
u = fem.Function(V)  # time-dep c at n+1
v = TestFunction(V)
un = fem.Function(V)  # time dep C at n
D = fem.Constant(domain, 1.0)  # diffusion constant
dt = fem.Constant(domain, 0.05)

# define the variational problem
# note: i think it should be  L=un*v -v*dx
F = (un*v*ufl.dx) - (v*ufl.dx)
F -= u*dx - fem.Constant(domain, 1.0)*v*dx - cos(u)*u*v*dt*dx - D*dt * \
# variational problem stated as a=L
(a, L) = system(F)

# problem = NonlinearProblem(F, u, bcs=[]) # works if taking u*v*dx instead of u*dx
``````

Hope this helps.

Cheers.

2 Likes

Hi! Thanks so much for your reply! Would a backward Euler scheme be appropriate for solving the time-dependent problem? This is a time-dependent PDE (which is different from the non-linear poisson example). Would you suggest applying a Newton Solver for the spatial discretization in combination with a backward Euler scheme for the time-dependent part? Also can you refer me to where I might find documentation on how to use NonlinearVariationalSolver() with examples? Would I want to use this function? Thank you so much!
Also, would I still want to linearize the non-linear term?

I found the page below, but it did not help clarify how the NonLinearVariationalSolver works. https://fenicsproject.org/olddocs/dolfin/1.4.0/python/programmers-reference/fem/solving/NonlinearVariationalSolver.html

Yes, backward Euler could work. It all depends on the stability of your scheme. You could also use the Newton solver, or implement a fixed-point iteration.

In the forum you could find some post on non-linear time-dependent problems:

https://fenicsproject.discourse.group/t/nonlinear-heat-equation-performance-dolfin-vs-dolfinx/11961

https://fenicsproject.discourse.group/t/how-to-check-if-the-nonlinearproblem-is-setup-properly/10218

Also, in this dolfinx-tutorial by @bleyerj you can find a thermo-elastic evolution problem.

Cheers.

1 Like