Interpolating/Projecting from a Functionspace to a VectorFunctionSpace

Asked by Pietro Maximoff

Hello

I have scalar function from scalar a FunctionSpace on a reasonably fine mesh. On a coarser mesh, I have a VectorFunctionSpace. I'd like to project/interpolate the values from the Functionspace onto the VectorFunctionSpace with each scalar value making up the first component of the vector in the VectorFunctionspace. For example, in 3D: (u_scalar_value, 0, 0) .

Is this possible?

Thanks

P.

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Anders Logg
Solved:
Last query:
Last reply:

This question was reopened

Revision history for this message
Marie Rognes (meg-simula) said :
#1

Yes, for projection try this:

from dolfin import *

coarse = UnitCube(2, 2, 2)
fine = refine(coarse)

F = FunctionSpace(fine, "CG", 1)
C = VectorFunctionSpace(coarse, "CG", 1)

f = Function(F)
f.vector()[:] = 1.0

# Define projection problem
u = TrialFunction(C)
v = TestFunction(C)
a = dot(u, v)*dx
L = dot(as_vector((f, 0, 0)), v)*dx
c = Function(C)

# Project it
solve(a == L, c)

#plot(f)
#plot(c, interactive=True)

Revision history for this message
Anders Logg (logg) said :
#2

On Tue, Jan 31, 2012 at 03:30:48PM -0000, Marie Rognes wrote:
> Question #186413 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/186413
>
> Status: Open => Answered
>
> Marie Rognes proposed the following answer:
>
> Yes, for projection try this:
>
> from dolfin import *
>
> coarse = UnitCube(2, 2, 2)
> fine = refine(coarse)
>
> F = FunctionSpace(fine, "CG", 1)
> C = VectorFunctionSpace(coarse, "CG", 1)
>
> f = Function(F)
> f.vector()[:] = 1.0
>
> # Define projection problem
> u = TrialFunction(C)
> v = TestFunction(C)
> a = dot(u, v)*dx
> L = dot(as_vector((f, 0, 0)), v)*dx
> c = Function(C)
>
> # Project it
> solve(a == L, c)
>
> #plot(f)
> #plot(c, interactive=True)

Does it not work with a simpler approach like this (untested)?

C = VectorFunctionSpace(coarse, "CG", 1)
c = project(as_vector((f, 0, 0)), C)

--
Anders

Revision history for this message
Pietro Maximoff (segment-x) said :
#3

Thanks Marie. This works .

Revision history for this message
Pietro Maximoff (segment-x) said :
#4

Hi Anders

What would be the C++ equivalent of:

c = project(as_vector((f, 0, 0)), C)

P.

Revision history for this message
Best Anders Logg (logg) said :
#5

B1;2802;0cOn Tue, Jan 31, 2012 at 09:55:45PM -0000, Pietro Maximoff wrote:
> Question #186413 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/186413
>
> Status: Solved => Open
>
> Pietro Maximoff is still having a problem:
> Hi Anders
>
> What would be the C++ equivalent of:
>
> c = project(as_vector((f, 0, 0)), C)

There is no automatic projection in C++, since it relies on specific
code being generated for the function space C. So you need to define
the projection yourself in a UFL file. Name it Projection.ufl or
similar and then just import the forms and solve the linear system.

--
Anders

Revision history for this message
Pietro Maximoff (segment-x) said :
#6

Thanks Anders Logg, that solved my question.