← Back to team overview

dolfin team mailing list archive

Re: sub-functions

 

There are actually two copies when you pick a sub function, first when
you do u[0] and then when you do v = u[0].

First:

    Function Function::operator[] (const uint i)
    {
      // Create copy
      Function f(*this);

      // Restrict copy to sub function or component
      f.f->sub(i);

      return f;
    }

Then:

    const Function& Function::operator= (const Function& f)
    {
      switch ( f._type )
      {
      ...
      case discrete:
        // Don't delete data if not necessary (don't want to recreate vector)
        if ( _type == discrete )
        {
          ((DiscreteFunction *) this->f)->copy(*((DiscreteFunction *) f.f));
        }
        else
        {
          delete this->f;
          this->f = new DiscreteFunction(*((DiscreteFunction *) f.f));
        }
        break;
        ...
       }
       ...
    }

When I first implemented the new Function classes, no copies where
made so all memory was shared:

    v = u; // v and u share the same x, mesh and element

I changed the behaviour when I implemented the new PDE class with the
solve Function, since we then need to create the Function inside
PDE::solve() and return it:

    Function u = pde.solve();

Then u must have a copy of everything (except the mesh, which is
user-supplied anyway).

I don't see an easy way to change this (if we don't do smart
pointers/reference counting), in particular for sub functions. Take
for example the Stokes problem. We then want to do

    Function u;
    Function p;
    stokes.solve(u, p);

Then u and p are sub functions of some w that is the system
solution. The Function w is hidden. It's created inside solve() and is
then destroyed so u and p must be copies. This is not optimal since u
and p will both have a copy of the same data (the entire w).

I'm sure there's a better way to do it. We just need to figure out
how.

In the meantime, the current approach with copying of values is more
robust and the overhead is probably small: copying a vector with PETSc
compared to writing all the values to file or solving the linear system.

/Anders

On Fri, Mar 10, 2006 at 05:14:07PM +0100, Garth N. Wells wrote:
> Would it be possible to define sub-functions that don't make a copy of
> the vector associated with the function, rather just have a pointer to
> the original vector? I use sub-functions primarily for post-processing,
> so it would be useful not to have 
> 
>   v = u[0];
> 
> every time the result is written to a file.  
> 
> Garth
> 
> 
> _______________________________________________
> DOLFIN-dev mailing list
> DOLFIN-dev@xxxxxxxxxx
> http://www.fenics.org/cgi-bin/mailman/listinfo/dolfin-dev
> 

-- 
Anders Logg
Research Assistant Professor
Toyota Technological Institute at Chicago
http://www.tti-c.org/logg/



References