Hi everyone,
I’m a beginner working with dolfinx, and I’m currently trying to solve a coupled ODE system using a vector-valued DG0 (discontinuous Galerkin) space. I apply Dirichlet boundary conditions at the left end of a 1D interval (x = 0), but they don’t seem to take effect when I use DG0 elements. However, when I switch to a CG1 (continuous Galerkin) space, the same boundary conditions work as expected.
The ODE system I’m solving is:
Below is a simplified version of my code. I’m applying fixed values at x = 0
using dirichletbc
on V.sub(0)
and V.sub(1)
. This works for CG1 but not for DG0 — the solution at the boundary doesn’t match the expected values.
Could someone kindly explain why this is happening? Is this behavior expected when using DG elements in dolfinx?
Any explanation or advice would be greatly appreciated. Thank you!
Here it’s my code:
import numpy as np
import matplotlib.pyplot as plt
import dolfinx
from mpi4py import MPI
from petsc4py import PETSc
from ufl import *
import basix
from dolfinx.fem import petsc
# Create mesh
T = 800.0
n = 80000
mesh = dolfinx.mesh.create_interval(MPI.COMM_WORLD, n, [0.0, T])
# DG0 vector-valued function space
vector_element = basix.ufl.element("DG", "interval", 0, shape=(2, 1))
V_vec = dolfinx.fem.functionspace(mesh, vector_element)
# Parameters
alpha = 0.1
beta1 = 0.01
beta2 = 0.01
S1 = 0.005
P1 = pi / 180
S2 = 0.3
P2 = pi
# Boundary condition at x = 0
facets = dolfinx.mesh.locate_entities_boundary(mesh, dim=0, marker=lambda x: np.isclose(x[0], 0.0))
dofs_1 = dolfinx.fem.locate_dofs_topological(V=V_vec, entity_dim=0, entities=facets)
dofs_2 = dolfinx.fem.locate_dofs_topological(V=V_vec, entity_dim=0, entities=facets)
bc_u1 = dolfinx.fem.dirichletbc(value=0.75, dofs=dofs_1, V=V_vec.sub(0))
bc_u2 = dolfinx.fem.dirichletbc(value=0.25, dofs=dofs_2, V=V_vec.sub(1))
# Define variational problem
u = TrialFunction(V_vec)
v = TestFunction(V_vec)
x = SpatialCoordinate(mesh)
A = as_matrix([[alpha + beta1, -alpha], [-alpha, alpha + beta2]])
f = as_matrix([[beta1 + S1 * sin(P1 * x[0])], [S2 * sin(P2 * x[0])]])
a = inner(dot(A, u), v) * dx + inner(jump(u), v('+')) * dS
L = inner(f, v) * dx
# Solve
problem = petsc.LinearProblem(a, L, bcs=[bc_u1, bc_u2])
uh = problem.solve()
# Extract solution
x_coords = mesh.geometry.x[:, 0]
u1 = uh.x.array[::2]
u2 = uh.x.array[1::2]
print(u1[0], u2[0]) # Values at left boundary
# Plot
plt.figure(figsize=(8, 5))
plt.plot(x_coords[:-1], u1, "r-", label="u1(x)", linewidth=2)
plt.plot(x_coords[:-1], u2, "b--", label="u2(x)", linewidth=2)
plt.xlabel("x")
plt.ylabel("Value")
plt.title("Components of Vector Function")
plt.grid(True)
plt.legend()
plt.show()