How to define a constant in variational formulation?

Hello, I am receiving an error relating to how I am, defining my constant D in my variational formulation. The error says: "AttributeError: ‘Constant’ object has no attribute ‘ufl_function_space’ ". It seems that the constant is not defined in the right function space. However, I am very confused about this because I am in fact defining it on my mesh using D= fem.Constant(domain, 1.0). Please find the code below. 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)

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 + sin(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 coefficient
dt = fem.Constant(domain, 0.05)

##define the variational problem
L= (un*v*ufl.dx) - (v*ufl.dx) 
a= u*dx - v*dx - vel(u)*u*v*dt*dx  - D*inner(grad(u), grad(v))*dx*dt + D*v*dx*dt
#variational problem stated as a=L
(a, L) = system(ufl.equation(a==L))```

I’ve edited your code a bit to be compatible with v0.8.0 which is on my system.

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 (SpatialCoordinate, TrialFunction, TestFunction,
                 as_vector, cos, sin, inner, div, grad, dx, pi)

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 + sin(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 coefficient
dt = fem.Constant(domain, 0.05)

##define the variational problem
L= (un*v*ufl.dx) - (v*ufl.dx)
a= u*v*dx - vel(u)*u*v*dt*dx  - D*dt*inner(grad(u), grad(v))*dx + D*dt*v*dx

The issue is that once you’ve multiplied by ufl.dx a ufl.Form is created. If you move your constant factor inside the integral, it should work as intended.

1 Like