This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Projection/interpolation between meshes

+7 votes

Will it be possible to interpolate a function of a coefficient onto another mesh some time in the future?

That is, will u4 work?

from dolfin import *
mesh1 = RectangleMesh(0,0,1,1,3,3)
mesh2 = RectangleMesh(0,0,1,1,4,4)
V1 = FunctionSpace(mesh1,'CG',1)
V2 = FunctionSpace(mesh2,'CG',1)
u1 = Function(V1)

u2 = interpolate(u1,V2)
u3 = project(u1,V2) #fails in DOLFIN 1.2.0?
#u4 = interpolate(sin(u1),V2) #fails
u5 = project(sin(u1),V2)
asked Mar 20, 2014 by KristianE FEniCS Expert (12,900 points)

2 Answers

+4 votes
 
Best answer

Not that I know of. You can easily wrap the function in an Expression though:

class sinu1(Expression):
    def eval(self, values, x):
        values[0] = sin(u1(x))
u4 = interpolate(sinu1(),V2)

If you need this in parallel you could do as Jan suggests or install fenicstools and do

from fenicstools import interpolate_nonmatching_mesh
u2 = interpolate_nonmatching_mesh(u1, V2)
u4 = project(sin(u2), V2)
answered Mar 20, 2014 by mikael-mortensen FEniCS Expert (29,340 points)
selected Mar 20, 2014 by Jan Blechta

You can also wait a bit for pull request 112 being merged instead of installing fenicstools.

+1 vote
u5 = project(sin(u1),V2)

should work in serial. Also solution from Mikael's answer is limited to serial.

In parallel, you can do the following sequence

  1. save parallel function to file (HDF5),
  2. initialize global serial mesh on one process,
  3. load saved function serially,
  4. perform projection/interpolation (using serial approach from this answer or Mikael's approach)
  5. save serial result to file,
  6. load in parallel from file.

This is done using user supplied MPI communicators introduced by Garth.

answered Mar 20, 2014 by Jan Blechta FEniCS Expert (51,420 points)

There's also this pull request or fenicstools.

Sure, the great tool, but it will not handle a sine of a function.

Oh, you're right, Mikael, it can be done in two steps. So Mikael's solution is the easiest to use. So I pick his answer as best if you don't mind.

...