← Back to team overview

dolfin team mailing list archive

Re: [Bug 457262] [NEW] function and expression does not give the same result

 

logg wrote:
> On Wed, Oct 21, 2009 at 04:31:12PM -0000, Garth Wells wrote:
>> oelgaard wrote:
>>> Quoting Garth Wells <gnw20@xxxxxxxxx>:
>>>
>>>> oelgaard wrote:
>>>>> Public bug reported:
>>>>>
>>>>> The following two files will reproduce the bug.
>>>>>
>>>>> M.ufl:
>>>>>
>>>>> exact = FiniteElement("Lagrange", triangle, 6)
>>>>> approx = FiniteElement("Lagrange", triangle, 1)
>>>>>
>>>>> u = Function(exact)
>>>>> uh = Function(approx)
>>>>>
>>>>> M = (uh - u)*(uh - u)*dx
>>>>>
>>>>>
>>>>> main.cpp:
>>>>>
>>>>> #include <dolfin.h>
>>>>> #include "M.h"
>>>>>
>>>>> using namespace dolfin;
>>>>>
>>>>> class Expr : public Expression
>>>>> {
>>>>> public:
>>>>>
>>>>>   Expr() : Expression(2) {}
>>>>>
>>>>>   void eval(double* values, const double* x) const
>>>>>   {
>>>>>     values[0] = 10*x[0]*x[1];
>>>>>   }
>>>>>
>>>>> };
>>>>>
>>>>> class Expr2 : public Expression
>>>>> {
>>>>> public:
>>>>>
>>>>>   Expr2() : Expression(2) {}
>>>>>
>>>>>   void eval(double* values, const double* x) const
>>>>>   {
>>>>>     values[0] = sin(5*x[0]);
>>>>>   }
>>>>> };
>>>>>
>>>>> class Func2 : public Function
>>>>> {
>>>>> public:
>>>>>
>>>>>   Func2(FunctionSpace& v) : Function(v) {}
>>>>>
>>>>>   void eval(double* values, const double* x) const
>>>>>   {
>>>>>     values[0] = sin(5*x[0]);
>>>>>   }
>>>>> };
>>>>>
>>>> The above class shouldn't be possible with the development version of
>>>> DOLFIN. Are you using an older version?
>>> Yes:
>>>
>>> changeset:   7392:70596970a173
>>> tag:         tip
>>> user:        Anders Logg <logg@xxxxxxxxx>
>>> date:        Wed Oct 21 17:34:32 2009 +0200
>>> summary:     Attempt at bug fix in application of periodic boundary conditions
>>> on
>>>
>>>
>>> Why shouldn't it be possible to derive a class from Function?
>>>
>> In the new design, Function is strictly a finite element function, and
>> not a user-defined function. We may need to fix something to make it
>> impossible to create a Function with a user-provided eval(..).
>> Expression should be used instead.
> 
> I don't see how to do this. The eval function is virtual so there's
> nothing "wrong" with overloading it.
> 
> Unless there's some trick we can do with making it private or
> protected in Function.
> 

It should be made private.

Garth

> --
> Anders
> 
> 
>>>>>          Status: New
>>>>>
>>>> Status in DOLFIN: New
>>>>
>>>> Bug description:
>>>> The following two files will reproduce the bug.
>>>>
>>>> M.ufl:
>>>>
>>>> exact = FiniteElement("Lagrange", triangle, 6)
>>>> approx = FiniteElement("Lagrange", triangle, 1)
>>>>
>>>> u = Function(exact)
>>>> uh = Function(approx)
>>>>
>>>> M = (uh - u)*(uh - u)*dx
>>>>
>>>>
>>>> main.cpp:
>>>>
>>>> #include <dolfin.h>
>>>> #include "M.h"
>>>>
>>>> using namespace dolfin;
>>>>
>>>> class Expr : public Expression
>>>> {
>>>> public:
>>>>
>>>>   Expr() : Expression(2) {}
>>>>
>>>>   void eval(double* values, const double* x) const
>>>>   {
>>>>     values[0] = 10*x[0]*x[1];
>>>>   }
>>>>
>>>> };
>>>>
>>>> class Expr2 : public Expression
>>>> {
>>>> public:
>>>>
>>>>   Expr2() : Expression(2) {}
>>>>
>>>>   void eval(double* values, const double* x) const
>>>>   {
>>>>     values[0] = sin(5*x[0]);
>>>>   }
>>>> };
>>>>
>>>> class Func2 : public Function
>>>> {
>>>> public:
>>>>
>>>>   Func2(FunctionSpace& v) : Function(v) {}
>>>>
>>>>   void eval(double* values, const double* x) const
>>>>   {
>>>>     values[0] = sin(5*x[0]);
>>>>   }
>>>> };
>>>>
>>>> int main()
>>>> {
>>>>   // Create mesh and function space
>>>>   UnitSquare mesh(8, 8);
>>>>   M::CoefficientSpace_u Vu(mesh);
>>>>   M::CoefficientSpace_uh Vuh(mesh);
>>>>
>>>>   Expr exact;
>>>> //  Expr2 approx;      // 12.5251
>>>> //  Func2 approx(Vuh); // 11.1111
>>>>   Func2 approx(Vu);    // 11.1111
>>>>
>>>>   M::Functional m(mesh);
>>>>   m.u = exact;
>>>>   m.uh = approx;
>>>>
>>>>   double error = assemble(m);
>>>>
>>>>   std::cout << "error: " << error << std::endl;
>>>>
>>>>   return 0;
>>>> }
>>>>
>

-- 
function and expression does not give the same result
https://bugs.launchpad.net/bugs/457262
You received this bug notification because you are subscribed to DOLFIN.

Status in DOLFIN: New

Bug description:
The following two files will reproduce the bug.

M.ufl:

exact = FiniteElement("Lagrange", triangle, 6)
approx = FiniteElement("Lagrange", triangle, 1)

u = Function(exact)
uh = Function(approx)

M = (uh - u)*(uh - u)*dx


main.cpp:

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

using namespace dolfin;

class Expr : public Expression
{
public:

  Expr() : Expression(2) {}

  void eval(double* values, const double* x) const
  {
    values[0] = 10*x[0]*x[1];
  }

};

class Expr2 : public Expression
{
public:

  Expr2() : Expression(2) {}

  void eval(double* values, const double* x) const
  {
    values[0] = sin(5*x[0]);
  }
};

class Func2 : public Function
{
public:

  Func2(FunctionSpace& v) : Function(v) {}

  void eval(double* values, const double* x) const
  {
    values[0] = sin(5*x[0]);
  }
};

int main()
{
  // Create mesh and function space
  UnitSquare mesh(8, 8);
  M::CoefficientSpace_u Vu(mesh);
  M::CoefficientSpace_uh Vuh(mesh);

  Expr exact;
//  Expr2 approx;      // 12.5251
//  Func2 approx(Vuh); // 11.1111
  Func2 approx(Vu);    // 11.1111

  M::Functional m(mesh);
  m.u = exact;
  m.uh = approx;

  double error = assemble(m);

  std::cout << "error: " << error << std::endl;

  return 0;
}


Follow ups

References