Hello everyone, I am working on FeniCSX 0.6 and I have a question regarding of using a fem function (from mesh01) on mesh02. You can find more details as following:
I have defined two mesh: mesh01 = (0, 1) (0, 1) and mesh02 = (0, 1) (-1, 1). There is a common interface between these two meshes y = 0.
Now, I have got a FEM function u01 (on mesh01) after solving a PDE system, I want to set u01 as the Dirichlet boundary condition on the interface y = 0 for another FEM function u02 (on mesh02).
Is there any convenient way to reach my goal? I know we can do it by setting u01.set_allow_extrapolation(True) on old version. But I failed on fenicsx.
import ufl
import numpy as np
import dolfinx
from petsc4py import PETSc
from mpi4py import MPI
import dolfinx.fem.petsc
def f_real(x):
return np.sin(np.pi*x[0]*(1 - x[1])) + np.cos(np.pi*x[1]*(1 - x[0]))
cell_type01 = dolfinx.mesh.CellType.triangle
mesh01 = dolfinx.mesh.create_rectangle(MPI.COMM_WORLD,
[[0.0, 0.0], [1.0, 1.0]],
[10, 10],
cell_type01)
x01 = mesh01.geometry.x
tdim01 = mesh01.topology.dim
fdim01 = tdim01 - 1
def bottom01(x):
return np.isclose(x[1], 0.0)
def right01(x):
return np.isclose(x[0], 1.0)
def top01(x):
return np.isclose(x[1], 1.0)
def left01(x):
return np.isclose(x[0], 0.0)
bottom_facet01 = dolfinx.mesh.locate_entities_boundary(mesh01, fdim01, bottom01)
right_facet01 = dolfinx.mesh.locate_entities_boundary(mesh01, fdim01, right01)
top_facet01 = dolfinx.mesh.locate_entities_boundary(mesh01, fdim01, top01)
left_facet01 = dolfinx.mesh.locate_entities_boundary(mesh01, fdim01, left01)
facets01 = np.concatenate([bottom_facet01, right_facet01, top_facet01, left_facet01])
values01 = np.concatenate([
np.full_like(bottom_facet01, 1, dtype=np.int32),
np.full_like(right_facet01, 2, dtype=np.int32),
np.full_like(top_facet01, 3, dtype=np.int32),
np.full_like(left_facet01, 4, dtype=np.int32)
])
sorted_facets01 = np.argsort(facets01)
facet_tag01 = dolfinx.mesh.meshtags(mesh01, fdim01, facets01[sorted_facets01],
values01[sorted_facets01])
ds01 = ufl.Measure("ds", domain=mesh01, subdomain_data=facet_tag01)
Vh01_scal = dolfinx.fem.FunctionSpace(mesh01, ("CG", 2))
u01 = dolfinx.fem.Function(Vh01_scal)
u01.interpolate(f_real)
mesh02 = dolfinx.mesh.create_rectangle(MPI.COMM_WORLD,
[[0.0, -1.0], [1.0, 0.0]],
[50, 50],
cell_type01)
x02 = mesh02.geometry.x
tdim02 = mesh02.topology.dim
fdim02 = tdim02 - 1
def bottom02(x):
return np.isclose(x[1], -1.0)
def right02(x):
return np.isclose(x[0], 1.0)
def top02(x):
return np.isclose(x[1], 0.0)
def left02(x):
return np.isclose(x[0], 0.0)
bottom_facet02 = dolfinx.mesh.locate_entities_boundary(mesh02, fdim02, bottom02)
right_facet02 = dolfinx.mesh.locate_entities_boundary(mesh02, fdim02, right02)
top_facet02 = dolfinx.mesh.locate_entities_boundary(mesh02, fdim02, top02)
left_facet02 = dolfinx.mesh.locate_entities_boundary(mesh02, fdim02, left02)
facets02 = np.concatenate([bottom_facet02, right_facet02, top_facet02, left_facet02])
values02 = np.concatenate([
np.full_like(bottom_facet02, 1, dtype=np.int32),
np.full_like(right_facet02, 2, dtype=np.int32),
np.full_like(top_facet02, 3, dtype=np.int32),
np.full_like(left_facet02, 4, dtype=np.int32)
])
sorted_facets02 = np.argsort(facets02)
facet_tag02 = dolfinx.mesh.meshtags(mesh02, fdim02, facets02[sorted_facets02],
values02[sorted_facets02])
ds02 = ufl.Measure("ds", domain=mesh02, subdomain_data=facet_tag02)
Vh02_scal = dolfinx.fem.FunctionSpace(mesh02, ("CG", 2))
u02 = dolfinx.fem.Function(Vh02_scal)
facet_top02 = facet_tag02.find(3)
dofs_top02 = dolfinx.fem.locate_dofs_topological(Vh02_scal, fdim02, facet_top02)
bc_top02 = dolfinx.fem.dirichletbc(u01, dofs_top02, Vh02_scal)
dolfinx.fem.set_bc(u02.x.array, [bc_top02])
a01 = dolfinx.fem.assemble_scalar(dolfinx.fem.form(u02*ds02(3)))
print(f"------{a01}----------")
