project an expression onto a subspace

Asked by mwelland

Hi all,
Can anyone help me project a particular expression onto a subspace of a mixed function space? I can get the pointer from the split() function, but I can't figure out how to use it to apply my initial guess.

Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
FEniCS Project Edit question
Assignee:
No assignee Edit question
Solved by:
mwelland
Solved:
Last query:
Last reply:
Revision history for this message
mwelland (akfypznt56ld2kjywwchehm8-mike-nw2wga2s5w6wnovqwws3u9v1) said :
#1

Example:

~~~~~~~~~~
from dolfin import *

mesh = Rectangle(0,0,1,1,10,10)

V1 = FunctionSpace(mesh, "Lagrange", 1) # Order 1 function space
V = MixedFunctionSpace([V1, V1])

u = Function(V)
u1,u2 = u.split()

u.interpolate(Expression(("1","2")))
u2.interpolate(Expression("3"))
~~~~~~~~~~~~~

*** Error: Unable to initialize vector of degrees of freedom for function.
*** Reason: Cannot be created from subspace. Consider collapsing the function space.
*** Where: This error was encountered inside Function.cpp.

Revision history for this message
Johan Hake (johan-hake) said :
#2

The GenericVector::operator+=(double) is there in the C++ interface. It
has just not been wrapped to Python. A fix for that is on its way to trunk.

Johan

On 10/17/2012 09:05 PM, mwelland wrote:
> Question #211497 on FEniCS Project changed:
> https://answers.launchpad.net/fenics/+question/211497
>
> mwelland gave more information on the question:
> Example:
>
> ~~~~~~~~~~
> from dolfin import *
>
> mesh = Rectangle(0,0,1,1,10,10)
>
> V1 = FunctionSpace(mesh, "Lagrange", 1) # Order 1 function space
> V = MixedFunctionSpace([V1, V1])
>
> u = Function(V)
> u1,u2 = u.split()
>
> u.interpolate(Expression(("1","2")))
> u2.interpolate(Expression("3"))
> ~~~~~~~~~~~~~
>
> *** Error: Unable to initialize vector of degrees of freedom for function.
> *** Reason: Cannot be created from subspace. Consider collapsing the function space.
> *** Where: This error was encountered inside Function.cpp.
>

Revision history for this message
Patrick Farrell (pefarrell) said :
#3

Johan: was that in answer to https://answers.launchpad.net/dolfin/+question/211408 , not this one?

Revision history for this message
Johan Hake (johan-hake) said :
#4

Yes, sorry for the confusion...

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

On Wed, Oct 17, 2012 at 07:05:57PM -0000, mwelland wrote:
> Question #211497 on FEniCS Project changed:
> https://answers.launchpad.net/fenics/+question/211497
>
> mwelland gave more information on the question:
> Example:
>
> ~~~~~~~~~~
> from dolfin import *
>
> mesh = Rectangle(0,0,1,1,10,10)
>
> V1 = FunctionSpace(mesh, "Lagrange", 1) # Order 1 function space
> V = MixedFunctionSpace([V1, V1])
>
> u = Function(V)
> u1,u2 = u.split()
>
> u.interpolate(Expression(("1","2")))
> u2.interpolate(Expression("3"))
> ~~~~~~~~~~~~~
>
> *** Error: Unable to initialize vector of degrees of freedom for function.
> *** Reason: Cannot be created from subspace. Consider collapsing the function space.
> *** Where: This error was encountered inside Function.cpp.

Assignment to subfunctions is a particular area that (for technical
reasons related to the coupling between the form language UFL and the
C++ representation in DOLFIN) is not as developed as one could hope.

My suggestion would be to project your expressions to the big
space. Something like this (untested code):

  V = FunctionSpace(mesh, "Lagrange", 1) # Order 1 function space
  W = MixedFunctionSpace([V, V])

  e0 = Expression("...")
  e1 = Expression("...")

  v0, v1 = TestFunctions(W)
  w = TestFunction(W)

  a = inner(u, w)*dx
  L = inner(e0, v0)*dx + e1*v1*dx

  u = Function(W)
  solve(a == L, u)

You should also be able to do this more directly using a combination
of project() and as_tensor(), something like

  u = project(as_tensor(<something involving e0 and e1>, W))

but I can't test it right now since my system is broken due to some
experimental work...

--
Anders

Revision history for this message
mwelland (akfypznt56ld2kjywwchehm8-mike-nw2wga2s5w6wnovqwws3u9v1) said :
#6

Thanks all, I found a mathematical workaround to my problem.