How to define a Function in a submesh based on function values present in an adjoining submesh?

Dear @Sabarish_Gopi,

As this is not my field of expertise, my solution to this problem is highly specialized to CG-1 spaces.
Maybe @cdaversin has a better solution than this.

Please note that your example is far from a minimal example,as you have included solving a non-linear PDE. Let the following code be a guide on how to produce a Minimal code example in the future.

from  dolfin  import *
import numpy as np
n = 10

mesh = UnitSquareMesh(n, n)

# Divide a mesh into two parts, x[0]<0.7 and x[0]>=0.7
marker = MeshFunction("size_t",mesh ,mesh.topology().dim(),0)
for c in  cells(mesh):
    marker[c] = 0.7 < c.midpoint().x()
marker_array = marker.array()
submesh0 = MeshView.create(marker ,0)
submesh1 = MeshView.create(marker ,1)

# Initialize  function  spaces  and  basis  functions
V0 = FunctionSpace(submesh0 , "CG", 1)
V1 = FunctionSpace(submesh1 , "CG", 1)
W = MixedFunctionSpace(V0,V1)

# Create empty function, and interpolate u=x[1] onto part 0
u = Function(W)
u.sub(0).interpolate(Expression("x[1]", degree=1))

# Create mapping from submesh to parent mesh
mv0 = submesh0.topology().mapping()[mesh.id()]
vertex_map0 = np.array(mv0.vertex_map(),dtype=np.int32)
mv1 = submesh1.topology().mapping()[mesh.id()]
vertex_map1 = np.array(mv1.vertex_map(), dtype=np.int32)
# Find which vertices are in common for the two submeshes
common_vertices = np.flatnonzero(np.isin(vertex_map0, vertex_map1))

dofs0 = vertex_to_dof_map(V0)
dofs1 = vertex_to_dof_map(V1)
# Map dofs from submesh 0 to submesh 1 through their common vertex.
# To do this with other function-spaces than CG-1, mappings
# between facets has to be added
for vertex in common_vertices:
    vertex_0 = vertex_map0[vertex]
    dof0 = dofs0[vertex]
    vertex_1 = np.flatnonzero(vertex_map1 == vertex_0)[0]
    dof1 = dofs1[vertex_1]
    u.sub(1).vector().vec().setValueLocal(dof1,  u.sub(0).vector()[dof0])

File("u0.pvd") << u.sub(0)
File("u1.pvd") << u.sub(1)
1 Like