← Back to team overview

dolfin team mailing list archive

Re: A minimal c++ Function test and some bugs

 

2009/2/17 Anders Logg <logg@xxxxxxxxx>:
> On Tue, Feb 17, 2009 at 04:37:20PM +0000, A Navaei wrote:
>> 2009/2/17 A Navaei <axnavaei@xxxxxxxxxxxxxx>:
>> > 2009/2/17 A Navaei <axnavaei@xxxxxxxxxxxxxx>:
>> >> 2009/2/17 Anders Logg <logg@xxxxxxxxx>:
>> >>> On Tue, Feb 17, 2009 at 04:06:50PM +0000, A Navaei wrote:
>> >>>> 2009/2/17 Anders Logg <logg@xxxxxxxxx>:
>> >>>> > On Tue, Feb 17, 2009 at 03:59:51PM +0000, A Navaei wrote:
>> >>>> >> 2009/2/17 A Navaei <axnavaei@xxxxxxxxxxxxxx>:
>> >>>> >> > The following minimal test for Function in c++ reveals some bugs. I
>> >>>> >> > guess this example can help me with dealing with the current issues of
>> >>>> >> > ImageFunction.
>> >>>> >> >
>> >>>> >> > (1) interpolate.py does not work when a Function is created in c++ and
>> >>>> >> > wrapped (see comment [2]). It seems that the bug is originated from
>> >>>> >> > the copy constructor (see comment [3])
>> >>>> >> >
>> >>>> >> > (2) In order to perform the interpolation, why is it necessary to
>> >>>> >> > create another Function and then copy it?
>> >>>> >> >
>> >>>> >> > (3) Signature checkes seem not working properly (see comment [1]). The
>> >>>> >> > signature-based assignments are error-prone anyway, why the
>> >>>> >> > object-oriented approach is not used?
>> >>>> >> >
>> >>>> >> >
>> >>>> >> I'm adding another issue:
>> >>>> >>
>> >>>> >> (4) Sub-classing Function and calling the sub-class interpolate()
>> >>>> >> function does not call eval().
>> >>>> >
>> >>>> > Which interpolate() function? There are 4 different.
>> >>>>
>> >>>> The one used in comment [4].
>> >>>
>> >>> Yes, I see that now. This is expected behavior. The reason is that the
>> >>> call to vector() will create a zero vector if there is no vector. Once
>> >>> the vector has been created, the Function will change from being a
>> >>> user-defined function to a discrete function which means that eval()
>> >>> will not be called.
>> >>>
>> >>> I don't know what the best solution is. We could either add a new
>> >>> function just called interpolate() without arguments that interpolates
>> >>> the function to its function space.
>> >>
>> >> I think this is a good idea, as calling
>> >>
>> >> v.interpolate(v.vector(), v.function_space());
>> >>
>> >> is somehow pointless.
>> >>
>> >>
>> >> -Ali
>> >>
>> >>> Or one can work around it by
>> >>>
>> >>>  Vector x;
>> >>>  v.interpolate(x, v.function_space());
>> >>>  v.vector() = x;
>> >
>> > This is what interpolate.py does. Like I explained in (1), this is
>> > buggy when Function is created in c++ and then wrapped in python.
>>
>> Also it doesn't work in pure c++ (the work around is already given in
>> the test code). Please try comment [2] in sandbox.
>
> Can you extend the sandbox code to [2] (in the simplest possible way)
> and I can take a look.

Here you go. The problem is again about the non-existance of the
vector member variable.


-Ali
>
> --
> Anders
>
>
>> -Ali
>>
>> > Also, the above answered question (2).
>> >
>> >
>> > -Ali
>> >
>> >>>
>> >>>
>> >>> -----BEGIN PGP SIGNATURE-----
>> >>> Version: GnuPG v1.4.9 (GNU/Linux)
>> >>>
>> >>> iEYEARECAAYFAkma5H0ACgkQTuwUCDsYZdFvpwCdFb7i80BdC7XDqxwbtlr28FV0
>> >>> g30AnjcDnux+6kRuTBSKdnhtKAnV7zuz
>> >>> =ri5f
>> >>> -----END PGP SIGNATURE-----
>> >>>
>> >>> _______________________________________________
>> >>> DOLFIN-dev mailing list
>> >>> DOLFIN-dev@xxxxxxxxxx
>> >>> http://www.fenics.org/mailman/listinfo/dolfin-dev
>> >>>
>> >>>
>> >>
>> >
>> _______________________________________________
>> DOLFIN-dev mailing list
>> DOLFIN-dev@xxxxxxxxxx
>> http://www.fenics.org/mailman/listinfo/dolfin-dev
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
>
> iEYEARECAAYFAkma6YMACgkQTuwUCDsYZdGxnwCfamMyFx8T2DV9smZ0RN5LX5dV
> ZfMAnRuGl6rOzfvu9sj9rw6BCg99AVTI
> =31bc
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> DOLFIN-dev mailing list
> DOLFIN-dev@xxxxxxxxxx
> http://www.fenics.org/mailman/listinfo/dolfin-dev
>
>
// Place for random tests

#include <dolfin.h>
#include "Poisson.h"

using namespace dolfin;

class MyFunction : public Function
{
public:

  MyFunction(const FunctionSpace& V) : Function(V) {};

  void eval(double* values, const double* x) const
  {
    message("Calling eval");
    double dx = x[0] - 0.5;
    double dy = x[1] - 0.5;
    values[0] = 500.0*exp(-(dx*dx + dy*dy) / 0.02);
  }
};

int main()
{  
  UnitSquare mesh(2, 2);
  PoissonFunctionSpace V(mesh);
  MyFunction f(V);
  Vector x;
  
  message("Interpolating to another vector");
  f.interpolate(x, f.function_space());
  x.disp();

  message("Interpolating to the function vector");
  f.interpolate(f.vector(), f.function_space());
  f.vector().disp();
  
  message("Interpolating using initialising by an external function");
  MyFunction f_(f);
  f.interpolate(f_.vector(), f.function_space());
  f.vector().disp();
}


Follow ups

References