← Back to team overview

dolfin team mailing list archive

Re: SubFunction

 

Quoting "Garth N. Wells" <gnw20@xxxxxxxxx>:

> 
> 
> Kristian Oelgaard wrote:
> > 
> > Hello,
> > 
> > Given the new Function interface, is it possible to get a reference to the
> part
> > of the Function vector belonging to a SubFunction?
> >
> 
> No.

OK.
 
> > GenericVector* U0 = &U[0].vector(); (this doesn't work)
> > 
> > I need to inspect the solution values in a Newton solve and set any
> negative
> > values to zero.
> > 
> 
> If you do
> 
>    Function U0 = U[0];
> 
> the Function U0 will have a copy of part of the vector that belongs to 
> U, therefore changing the vector that belongs to U0 won't affect the 
> vector that belongs to U.

Yes, this was my problem.
 
> Take a look inside
> 
>    Function::Function(const SubFunction& v)
> 
> to see how to determine which components of the vector are associated 
> with a particular subspace. This way, you can work with the Function U 
> directly.

Thanks, it works. I do:

GenericVector* U_vec = &U.vector();
Function tmp = U[1];

const dolfin::uint n = tmp.vector().size();
const dolfin::uint offset = tmp.function_space().dofmap().offset();
dolfin::uint* rows = new dolfin::uint[n];
double* values = new double[n];

for (uint i = 0; i < n; i++)
  rows[i] = offset + i;

U_vec->get(values, n, rows);

for (uint i = 0; i < n; i++)
{
  if (values[i] < 0.0)
    values[i] = 0.0;
}

U_vec->set(values, n, rows);

Kristian
 
> Garth
> 
> 
> > Kristian
> > _______________________________________________
> > DOLFIN-dev mailing list
> > DOLFIN-dev@xxxxxxxxxx
> > http://www.fenics.org/mailman/listinfo/dolfin-dev
> 
> 




References