BCs in mixed space vector+scalar+scalar

Asked by Chris Richardson

I want to solve for a vector and two scalars, and I have specified the FunctionSpace W intuitively correctly,
but the BC complains that my dimension is incorrect (for the vector). What am I doing wrong?

mesh=UnitSquare(10,10)

V=VectorFunctionSpace(mesh,"CG",2)
Q=FunctionSpace(mesh,"CG",1)
R=FunctionSpace(mesh,"CG",1)
W=V+Q+R

def bound0(x):
    x[0]<DOLFIN_EPS

v0=Constant((0.0,0.0))

bc=DirichletBC(W.sub(0),v0,bound0)

    bc=DirichletBC(W.sub(0),v0,bound0)
  File "/usr/lib/python2.6/dist-packages/dolfin/mesh/bcs.py", line 56, in __init__
    cpp.DirichletBC.__init__(self, *args)
  File "/usr/lib/python2.6/dist-packages/dolfin/cpp.py", line 9390, in __init__
    _cpp.DirichletBC_swiginit(self,_cpp.new_DirichletBC(*args))
RuntimeError: *** Error: Unable to create boundary condition. Reason: Illegal value 2 for value dimension, should be 3.

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Chris Richardson
Solved:
Last query:
Last reply:
Revision history for this message
Johan Hake (johan-hake) said :
#1

Hello Chris!

When defining MixedFunction spaces with more than one component you should
use:

  W = MixedFunctionSpace([V,Q,R])

The add operator in FunctionSpace returns a MixedFunctionSpace of its two
components, so

  V+Q+R

would result in:

  W = MixedFunctionSpace([MixedFunctionSpace([V,Q]),R])

Johan

On Monday 15 March 2010 14:13:12 Chris Richardson wrote:
> New question #104471 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/104471
>
> I want to solve for a vector and two scalars, and I have specified the
> FunctionSpace W intuitively correctly, but the BC complains that my
> dimension is incorrect (for the vector). What am I doing wrong?
>
> mesh=UnitSquare(10,10)
>
> V=VectorFunctionSpace(mesh,"CG",2)
> Q=FunctionSpace(mesh,"CG",1)
> R=FunctionSpace(mesh,"CG",1)
> W=V+Q+R
>
> def bound0(x):
> x[0]<DOLFIN_EPS
>
> v0=Constant((0.0,0.0))
>
> bc=DirichletBC(W.sub(0),v0,bound0)
>
>
> bc=DirichletBC(W.sub(0),v0,bound0)
> File "/usr/lib/python2.6/dist-packages/dolfin/mesh/bcs.py", line 56, in
> __init__ cpp.DirichletBC.__init__(self, *args)
> File "/usr/lib/python2.6/dist-packages/dolfin/cpp.py", line 9390, in
> __init__ _cpp.DirichletBC_swiginit(self,_cpp.new_DirichletBC(*args))
> RuntimeError: *** Error: Unable to create boundary condition. Reason:
> Illegal value 2 for value dimension, should be 3.
>

Revision history for this message
Chris Richardson (chris-bpi) said :
#2

OK, that solves my problem. Thank you, Johan!

It would be better, perhaps, if the default behaviour could be:

W=V+Q+R
is equivalent to
W=MixedFunctionSpace([V,Q,R])

and
W=(V+Q)+R
could be MixedFunctionSpace([MixedFunctionSpace([V,Q]),R]), if it is needed...

or maybe the W=V+Q style syntax should be deprecated...

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

On Mon, Mar 15, 2010 at 09:33:27PM -0000, Chris Richardson wrote:
> Question #104471 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/104471
>
> Status: Answered => Solved
>
> Chris Richardson confirmed that the question is solved:
> OK, that solves my problem. Thank you, Johan!
>
> It would be better, perhaps, if the default behaviour could be:
>
> W=V+Q+R
> is equivalent to
> W=MixedFunctionSpace([V,Q,R])
>
> and
> W=(V+Q)+R
> could be MixedFunctionSpace([MixedFunctionSpace([V,Q]),R]), if it is needed...
>
> or maybe the W=V+Q style syntax should be deprecated...

Yes, that would be good but I don't know how to fix that.

The problem is if you write

  x + y + z

then Python will interpret that as (x + y) + z.

So x + y will be a mixed function space, and then that will again be
mixed with z.

This question comes up again and again so it would be good to find a
way around this.

--
Anders

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

On Monday 15 March 2010 14:33:27 Chris Richardson wrote:
> Question #104471 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/104471
>
> Status: Answered => Solved
>
> Chris Richardson confirmed that the question is solved:
> OK, that solves my problem. Thank you, Johan!
>
> It would be better, perhaps, if the default behaviour could be:
>
> W=V+Q+R
> is equivalent to
> W=MixedFunctionSpace([V,Q,R])

This is unfortunately not possible in Python (please prove me wrong!).

> and
> W=(V+Q)+R
> could be MixedFunctionSpace([MixedFunctionSpace([V,Q]),R]), if it is
> needed...
>
> or maybe the W=V+Q style syntax should be deprecated...

I tend to agree with you, as this is the number one question new users of
FEniCS have.

However it is nice to define a MixedFunctionSpace using the add operator, so
we like to think of it as a feature and not a bug. :)

Johan