Constructing one function in whole domain based on two functions in two subdomains which coincides on the interface

I have a square [0,1] x [0,1] (name: “region”) which is divided into two rectangles (“region1” and “region2”). The interface is y=0.75. I have already defined a mesh of both rectangles that coincides with the interface.

fes1 = H1(mesh, definedon=“region1”, order=order)
fes2 = H1(mesh, definedon=“region2”, order=order)
fes = H1(mesh, definedon=“region”, order=order)

now I have two functions f1 in fes1 and f2 in fes2. Moreover, f1=f2 on interface

is there any approach to construct a new function f in fes such that f = f1 in region1 and f = f2 in region2.

I have thought about the extension method, but I think it will change the value of another region since the interface value may not always be zero. Thanks a lot.

you can do

pwfunc = mesh.MaterialCF( { "region1" : f1, "region2" : f2 } )

to get a function which is f1 in region1, and f2 in region2.

Now you can, for example, interpolate this into a GridFunction on the whole domain:

gf = GridFunction(fes)
gf.Interpolate(pwfunc)

Thank you so much! it finally works!