Hi, if I have two functions f2D and f1D that are defined in 2D and 1D meshes respectively, and I want to interpolate f1D to f2D such that:
f2D(x,y) = f1D(x)
In the old fenics, we can simply use the Lagrange interpolator, but in the dolfinx, it seems like the Lagrange interpolator is removed
If I do:
f2D->interplolate(f1D)
,
I only get f2D(x,y) = f1D(x)
when y=0
, which means I only get a line, and the rest of the domain is emply.
Is there any functions that I can try?
BTW, in my case, the 2D mesh is [0,L] * [-v,v] and the 1D mesh is [0,L]
Thanks very much!
More details: my 1D mesh and 2D mesh will always be match, like this:
so it is more like I want to “paste” a 1D function into the 2D grid, maybe it is not that efficient to use interpolate?
dokken
January 12, 2024, 4:17pm
3
Will you only work with Lagrange elements (and of what degree)? Will they be vector elements, tensor elements, or something else?
dokken:
? Will they be vector el
Yes I only work with CG, scalar value element, but the degree can be differ (1D and 2D function spaces will always have the same degree)
dokken
January 12, 2024, 4:58pm
5
Here is a code that runs in serial.
Would have to think a bit more carefully about this to get it to work in parallel:
import dolfinx
from mpi4py import MPI
import basix.ufl
import numpy as np
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 3, 3, ghost_mode=dolfinx.mesh.GhostMode.shared_facet)
el = basix.ufl.element("Lagrange", mesh.topology.cell_name(), 2)
V = dolfinx.fem.functionspace(mesh, el)
u = dolfinx.fem.Function(V)
import numpy as np
def lower_edge(x):
return np.isclose(x[1], 0)
entities = dolfinx.mesh.locate_entities_boundary(mesh, mesh.topology.dim-1, lower_edge)
sub_mesh, _,_,_ = dolfinx.mesh.create_submesh(mesh, mesh.topology.dim-1, entities)
sub_el = basix.ufl.element("Lagrange", sub_mesh.topology.cell_name(), 2, gdim=sub_mesh.geometry.dim)
V_sub = dolfinx.fem.functionspace(sub_mesh, sub_el)
u_sub = dolfinx.fem.Function(V_sub)
u_sub.interpolate(lambda x: np.sin(x[0]+0.1))
parent_coords = V.tabulate_dof_coordinates()
sub_coords = V_sub.tabulate_dof_coordinates()
def sub_to_parent(x):
indices = np.asarray([np.flatnonzero(np.isclose(sub_coords.T[0], xi))[0] for xi in x[0]], dtype=np.int32)
return u_sub.x.array[indices]
u.interpolate(sub_to_parent)
with dolfinx.io.VTKFile(mesh.comm, "mesh.pvd", "w") as vtk:
vtk.write_mesh(mesh)
vtk.write_function(u)