← Back to team overview

dolfin team mailing list archive

Re: passing functions

 

On Sun, Oct 01, 2006 at 06:35:51PM +0200, Dag Lindbo wrote:
> Hi all,
> 
> I'm a bit confused about passing a Function to a function and using it to
> assemble some system. What I'm doing is passing a Function, "func", by
> reference, to a function, foo, where func goes into a form and that form
> is assembled etc.
> 
> Vector& Another_Class::foo(Mesh& mesh, Function& func)
> 	{
> 		AAA::BilineraForm a
> 		AAA::LinearForm L(func)
> 
> 		FEM:assemble(...)
> 		lu_solver.solve(...)
> 
> 		return x;
> 	}
> 
> This works fine, but when the context that called foo (e.g. main) resumes
> something has happended to "func". The next time it is referenced the
> porgram terminates with a segmentation violation. To get around this I can
> copy the data to local Vector in foo:
> 
> Vector& Another_Class::foo(Mesh& mesh, Function& func)
> 	{
> 		Vector vec = func.vector();
> 		Function func2(vec,mesh,func.element());
> 
> 		AAA::BilineraForm a
> 		AAA::LinearForm L(func2)
> 
> 		FEM:assemble(...)
> 		lu_solver.solve(...)
> 
> 		return x;
> 	}
> 
> I would dearly like to avoid this extra copy of a large vector. How should
> one do this?
> 
> Regards,
> Dag Lindbo

This should not happen so it looks like a bug. Passing a function as
argument to a form only has the effect that the pointer to the
function is placed in a list. During assembly, all functions in the
list will be interpolated to each element. The pointer is never
deleted.

See if you can produce a simplest possible example that breaks and
maybe we can find out what goes wrong. Also check with valgrind if you
have it availabe and see if you can locate the offending line.

/Anders


Follow ups

References