Error with custom integration measures in complex build

Hello, I am trying to perform an integral over a line in my 2D domain. I am trying to reproduce the example from Line Integral of Vector Field - General - FEniCS Project

For the real build I am getting the correct result but for the complex one it just gives 0. Is this a bug or am I missing anything?

MWE:

from dolfinx import fem, mesh
from mpi4py import MPI
import numpy as np
import ufl
import dolfinx

domain = mesh.create_unit_square(MPI.COMM_WORLD, 100, 100, mesh.CellType.triangle)
mesh=domain

V = dolfinx.fem.FunctionSpace(domain, (“Lagrange”, 1))
u_test = dolfinx.fem.Function(V)
u_test.interpolate(lambda x: 1 + x[0]**2 + 2*x[1]**2)

#Define the measure
ds = ufl.Measure(“ds”, subdomain_data=facet_tags, subdomain_id=5 )
#Calculate the integral
integral = dolfinx.fem.form(u_test * ufl.ds)
print(fem.assemble_scalar(integral))

Thanks a lot in advance for any help.

You have missed the equation to find the proper facets.

def facet_eq(x):
    return np.isclose(x[0], 0.5)
facets = dolfinx.mesh.locate_entities(mesh, mesh.topology.dim-1, facet_eq)
facet_tags = dolfinx.mesh.meshtags(mesh, mesh.topology.dim-1, facets, np.full_like(facets, 5))

Also, you need dS instead of ds because dS integrates in the interior (actually I am not 100% sure for this). Moreover, (and this is a mistake I did a lot) you need to use the custom made ds instead of ufl.ds

from dolfinx import fem, mesh
from mpi4py import MPI
import numpy as np
import ufl
import dolfinx

domain = mesh.create_unit_square(MPI.COMM_WORLD, 100, 100, mesh.CellType.triangle)
mesh=domain

V = dolfinx.fem.FunctionSpace(domain, ("Lagrange", 1))
u_test = dolfinx.fem.Function(V)
u_test.interpolate(lambda x: 1 + x[0]**2 + 2*x[1]**2)

def facet_eq(x):
    return np.isclose(x[0], 0.5)
facets = dolfinx.mesh.locate_entities(mesh, mesh.topology.dim-1, facet_eq)
facet_tags = dolfinx.mesh.meshtags(mesh, mesh.topology.dim-1, facets, np.full_like(facets, 5))

#Define the measure
ds = ufl.Measure("dS", subdomain_data=facet_tags, subdomain_id=5 )
#Calculate the integral
integral = dolfinx.fem.form(u_test * ds)
print(fem.assemble_scalar(integral))

The ufl.ds was a typo, I actually didn’t use it in my code but I see I somehow messed up the MWE here. Same with the facet equation… Sorry for that.

But in the end, using the “dS” solved it. It is not clear to me what is the difference between “ds” and “dS”. Even in this tutorial they use only “ds”: Setting multiple Dirichlet, Neumann, and Robin conditions — FEniCSx tutorial (jsdokken.com).

But ok, main thing that it runs now. Thanks a lot!

dS is the integral measure over interior facets, these are facets that are connected to two cells.
ds is for exterior facets, those only connected to one cell.

1 Like