Projection of Calculated Function onto part of new Mesh; Dolfinx

Hello dear fenics Community,

i am doing some research on gradient index fibers with dolfinx. I have a diffusion simulation which calculates the refractive index profile of the waveguide that i want to analyse. This happens in dolfinx. Now i wrote a electromagnetics Program which calculates the eigenmodes of the waveguide.
All of this works as expected.
Now i want to take the refractive index profile of the waveguide and add an air-layer to it, to analyze the behaviour of the eigenmodes in a more realistic environment.
I would like to project the function that i calculated in the diffusion simulation onto part of a new mesh that includes these air layers, and i want to set the refractive index of the air layers to 1.
The refractive Index which is calculated is in the CG-Space of arbitrary polynomial degree (I use 2 mose of the time) and the refractive index in my electromagnetics simulation should be of the same degree or less.
Right now i am using the build-in meshes of dolfinx and a marker-function to determine where the part of my mesh is that is the waveguide. I would like to use Cell-tags later, but for now i would be very happy about any solution to the given problem.

Thanks in Advance
Tom

You you instead use an interpolation?
Ref:

Dear dokken,
thank you for your quick answer!
I have been trying to use interpolation, but i get the following error: “RuntimeError: Interpolation on different meshes not supported (yet).”

The minimal example is:

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

def generate_activation_function(msh, corner_points):
    element = ufl.FiniteElement("CG", msh.ufl_cell(), 1)
    Vh = fem.FunctionSpace(msh, element)
    activation_function = fem.Function(Vh)
    # activation_function.vector[:] = 0

    def Omega_0(x):
        return np.logical_and(x[0] > corner_points[0][0], 
               np.logical_and(x[0] < corner_points[1][0], 
               np.logical_and(x[1] > corner_points[0][1],
                              x[1] < corner_points[1][1])))
    
    cells_0 = mesh.locate_entities(msh, msh.topology.dim, Omega_0)
    activation_function.x.array[cells_0] = np.full_like(cells_0, 1, dtype=PETSc.ScalarType)
    return activation_function, cells_0

def setup_test_mesh(width, length, depth, res):
    nondim_width = 1
    nondim_length = length / width 
    nondim_depth = depth / width
    if depth < 1e-14:
        msh = mesh.create_rectangle(MPI.COMM_WORLD,
                                    [(-nondim_width / 2, nondim_length),
                                     (nondim_width / 2, 0)], [res, res])
    else:
        msh = mesh.create_box(MPI.COMM_WORLD, 
                            [(-nondim_width / 2, -nondim_length, 0), 
                            (nondim_width / 2, 0, nondim_depth)], [res, res, 1])
    return msh

width = 1
length = 1
depth = 1
air_width = 0.1
res = 10


complete_length = length + air_width * np.sign(length)
msh = setup_test_mesh(width, complete_length, depth, res)

msh_refrac = setup_test_mesh(width, length, depth, res)

CG = ufl.FiniteElement("CG", msh.ufl_cell(), 2)
CG_refrac = ufl.FiniteElement("CG", msh_refrac.ufl_cell(), 2)

Q = fem.FunctionSpace(msh, CG)
Q_refrac = fem.FunctionSpace(msh_refrac, CG_refrac)

def f(x):
    return (1 * x[0])

activation_function, cells = generate_activation_function(msh, [[0, 0], [1, length / width]])

refrac = fem.Function(Q_refrac)
refrac.interpolate(f)
refrac.x.scatter_forward()

complete_refrac = fem.Function(Q)
complete_refrac.interpolate(refrac, cells=cells)

This means that you are using an older version of DOLFINx, as I think this was merged in before the 0.5.0 release

I am on windows, and i am using wsl2 (ubuntu) to run my scripts. I have installed it via the recommended way:

add-apt-repository ppa:fenics-packages/fenics
apt update
apt install fenicsx

And if i look up the version of fenicsx it says 0.5.0.2.

apt-show-versions fenicsx
fenicsx:all/jammy 2:0.5.0.2~ppa1~jammy1 uptodate

Is it a version issue? And if yes how do i install the current version?
Thank you in Advance for the help dokken!

Sorry, it was the 0.6.0 release that introduced checkpointing, ref: Release v0.6.0 · FEniCS/dolfinx · GitHub
ref Interpolation between different meshes by massimiliano-leoni · Pull Request #2245 · FEniCS/dolfinx · GitHub

The 0.6.0 release is not on apt (you can use conda, spack or docker to get 0.6 release).