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

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(
    MPI.COMM_WORLD, extent, [Nx, Ny], mesh.CellType.quadrilateral)


# 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 * \
    inner(grad(u), grad(v))*dx + D*dt*v*dx
# 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