← Back to team overview

dolfin team mailing list archive

Re: Vector conclusions

 

2008/4/15, Dag Lindbo <dag@xxxxxxxxxx>:
> Hello!
>
>  A lot has happened on the Vector/Function/<backend>Vector interface the
>  last weeks. For clarity, I would like to have a little "summing up"
>  thread. That is, I  think it would be beneficial to explicitly show how
>  the new interface is intended to be used. Sounds good? Here are some
>  situations, to which I propose solutions. Please correct if these are
>  not in line with the intended interface, or if there are better ones.
>
>  How to:
>  *) Get reference to Vector from a (discrete) Function u
>  Vector& v = dynamic_cast<Vector&>(u.vector());

Why would you want that?
All dolfin library code will now use GenericVector references.
This way it will also work with f.ex. uBlasVector objects directly.
So:

GenericVector& v = u.vector();


>  *) Get reference to uBlasVector from Vector v:
>  uBlasVector& uv = dynamic_cast<uBlasVector&>(*v.instance());

uBlasVector& uv = v.down_cast<uBlasVector>();


>  *) Get reference to PETScVector from Vector:
>  ???

PETScVector& pv = v.down_cast<PETScVector>();


>  *) Get reference to uBlasVector from (discrete) Function u
>  uBlasVector& uv = dynamic_cast<uBlasVector&>(*u.vector().instance());

uBlasVector& uv = u.vector().down_cast<uBlasVector>();


>  *) Get reference to PETScVector from (discrete) Function u
>  ???

PETScVector& uv = u.vector().down_cast<PETScVector>();


>  Please add more situations and suggestions. Thanks!

In general, this works with v being any subclass of GenericVector
including Vector:

Foo & fv = v.down_cast<Foo>();

It will trigger a dolfin error on failure.
You can check the backend in use like:

bool b = v.has_type<Foo>();

with Foo being uBlasVector, PETScVector, etc.

The point is that v.down_cast<MyVec> and v.has_type<MyVec> works with
v being an instance of MyVec or an instance of Vector with MyVec as
the actual backend.

--
Martin


Follow ups

References