Interpolation on different grids

Hi,guys!As the title suggests.I want interpolate on different grids.
As code shows:I want to interpolate the function u1 on a coarse grid to the function u2 on a fine grid.

from mpi4py import MPI
from petsc4py import PETSc
import numpy as np
from dolfinx import default_real_type, fem, io, mesh,default_scalar_type
from dolfinx.fem.petsc import assemble_matrix, assemble_vector
from ufl import (CellDiameter, FacetNormal, TestFunction, TrialFunction, avg,
                 conditional, div, dot, dS, ds, dx, grad, gt, inner, outer,
                  TrialFunctions,TestFunctions,curl,cross)
import ufl
from dolfinx.io import gmshio,XDMFFile
from basix.ufl import element,mixed_element 
from dolfinx.fem.petsc import (apply_lifting, assemble_matrix, assemble_vector, 
                                 create_vector, set_bc,create_matrix)
import time
import os
import dolfinx
from dolfinx.cpp.mesh import CellType

def initial(x):
    values = np.zeros((2, x.shape[1]), dtype=PETSc.ScalarType)
    values[0]=x[0]
    values[1]=x[1]
    return values

n1=5
n2=10
msh1 =mesh.create_unit_square(MPI.COMM_WORLD, n1, n1,cell_type=CellType.triangle)  #triangle  quadrilateral
msh2 =mesh.create_unit_square(MPI.COMM_WORLD, n2, n2,cell_type=CellType.triangle)  #triangle  quadrilateral

U1_element=element("DG", msh1.basix_cell(),1, shape=(msh1.geometry.dim,))
U2_element=element("DG", msh2.basix_cell(),1, shape=(msh1.geometry.dim,))

V1=fem.functionspace(msh1,U1_element)
V2=fem.functionspace(msh2,U2_element)

u1=fem.Function(V1)
u2=fem.Function(V2)

u1.interpolate(initial)
print(u1.x.array)
u2.interpolate(u1)

Thanks!!!

The api Depends on what version of DOLFINx you are using. See:
dolfinx/python/test/unit/fem/test_interpolation.py at 7bb01adaa736a325ca048e37aec0b51cf9ff1f2e · FEniCS/dolfinx · GitHub For syntax for main branch, or
dolfinx/python/test/unit/fem/test_interpolation.py at v0.8.0 · FEniCS/dolfinx · GitHub
for 0.8.0 syntax (Which has a few bugs Fixed on main branch when it comes to parallel computing)

1 Like

Thanks dokken!Are you saying that when the version of dolfinx is less than 0.8, there will be some bug when parallel computing?

No, on version 0.8 there is a bug that has been fixed on main, that resulted in a change in the API of interpolate for non-matching meshes.

If you are on version 0.7 or on the main branch it works fine.

1 Like