cannot convert 'dolfin::real' to 'double' for argument '1' to 'double cos(double)

Asked by Murtazo Nazarov

In example in /demo/ode/stiff/cpp/Timedependent1.h I tried to change function f(u,t) into

  void f(const real* u, real t, real* y)
  {
    y[0] = -1000.0 * u[0] + cos(t);
  }

But I get a compilation error:

In file included from main.cpp:12:
TestProblem1.h: In member function 'virtual void TestProblem1::f(const dolfin::real*, dolfin::real, dolfin::real*)':
TestProblem1.h:27: error: cannot convert 'dolfin::real' to 'double' for argument '1' to 'double cos(double)'
scons: *** [main.o] Error 1

It seems dolfin::real is not double by default for the latest dolfin. Is it bug or I can fix it easily in my code?
Any suggestion?

Thanks,
Murtazo

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Anders Logg
Solved:
Last query:
Last reply:
Revision history for this message
Garth Wells (garth-wells) said :
#1

Murtazo Nazarov wrote:
> New question #97399 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/97399
>
>
> In example in /demo/ode/stiff/cpp/Timedependent1.h I tried to change function f(u,t) into
>
>
> void f(const real* u, real t, real* y)
> {
> y[0] = -1000.0 * u[0] + cos(t);
> }
>
> But I get a compilation error:
>
> In file included from main.cpp:12:
> TestProblem1.h: In member function 'virtual void TestProblem1::f(const dolfin::real*, dolfin::real, dolfin::real*)':
> TestProblem1.h:27: error: cannot convert 'dolfin::real' to 'double' for argument '1' to 'double cos(double)'
> scons: *** [main.o] Error 1
>
> It seems dolfin::real is not double by default for the latest dolfin. Is it bug or I can fix it easily in my code?
> Any suggestion?
>

Use 'double'.

Garth

> Thanks,
> Murtazo
>
>

Revision history for this message
Murtazo Nazarov (murtazo) said :
#2

Garth Wells wrote:
> Question #97399 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/97399
>
> Status: Open => Answered
>
> Garth Wells proposed the following answer:
>
> Murtazo Nazarov wrote:
>
>> New question #97399 on DOLFIN:
>> https://answers.launchpad.net/dolfin/+question/97399
>>
>>
>> In example in /demo/ode/stiff/cpp/Timedependent1.h I tried to change function f(u,t) into
>>
>>
>> void f(const real* u, real t, real* y)
>> {
>> y[0] = -1000.0 * u[0] + cos(t);
>> }
>>
>> But I get a compilation error:
>>
>> In file included from main.cpp:12:
>> TestProblem1.h: In member function 'virtual void TestProblem1::f(const dolfin::real*, dolfin::real, dolfin::real*)':
>> TestProblem1.h:27: error: cannot convert 'dolfin::real' to 'double' for argument '1' to 'double cos(double)'
>> scons: *** [main.o] Error 1
>>
>> It seems dolfin::real is not double by default for the latest dolfin. Is it bug or I can fix it easily in my code?
>> Any suggestion?
>>
>>
>
> Use 'double'.
>
> Garth
>

I tried with 'double'.

  void u0(double* u)
  {
    u[0] = 1.0;
  }

  void f(const double* u, double t, double* y)
  {
    y[0] = -1000.0 * u[0] + cos(t);
  }

Then I got similar error

main.cpp:64: error: cannot declare variable 'test_problem' to be of
abstract type 'TestProblem1'
TestProblem1.h:12: note: because the following virtual functions are
pure within 'TestProblem1':
/afs/nada.kth.se/home/x/u18gh7kx/data/Work/fenics/build/include/dolfin/ode/ODE.h:83:
note: virtual void dolfin::ODE::u0(dolfin::real*)

The I tried like this:

  void u0(real* u)
  {
    u[0] = 1.0;
  }

  void f(const real* u, double t, double* y)
  {
    y[0] = -1000.0 * u[0] + cos(t);
  }

Still error:

In file included from main.cpp:12:
TestProblem1.h: In member function 'void TestProblem1::f(const
dolfin::real*, double, double*)':
TestProblem1.h:27: error: cannot convert '__gmp_expr<__mpf_struct [1],
__gmp_binary_expr<__gmp_expr<__mpf_struct [1], __gmp_binary_expr<double,
__gmp_expr<__mpf_struct [1], __mpf_struct [1]>, __gmp_binary_multiplies>
 >, double, __gmp_binary_plus> >' to 'double' in assignment
scons: *** [main.o] Error 1

>
>> Thanks,
>> Murtazo
>>
>>
>>
>
>

Revision history for this message
Best Anders Logg (logg) said :
#3

On Thu, Jan 14, 2010 at 05:42:17PM -0000, Murtazo Nazarov wrote:
> Question #97399 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/97399
>
> Status: Answered => Open
>
> Murtazo Nazarov is still having a problem:
> Garth Wells wrote:
> > Question #97399 on DOLFIN changed:
> > https://answers.launchpad.net/dolfin/+question/97399
> >
> > Status: Open => Answered
> >
> > Garth Wells proposed the following answer:
> >
> > Murtazo Nazarov wrote:
> >
> >> New question #97399 on DOLFIN:
> >> https://answers.launchpad.net/dolfin/+question/97399
> >>
> >>
> >> In example in /demo/ode/stiff/cpp/Timedependent1.h I tried to change function f(u,t) into
> >>
> >>
> >> void f(const real* u, real t, real* y)
> >> {
> >> y[0] = -1000.0 * u[0] + cos(t);
> >> }
> >>
> >> But I get a compilation error:
> >>
> >> In file included from main.cpp:12:
> >> TestProblem1.h: In member function 'virtual void TestProblem1::f(const dolfin::real*, dolfin::real, dolfin::real*)':
> >> TestProblem1.h:27: error: cannot convert 'dolfin::real' to 'double' for argument '1' to 'double cos(double)'
> >> scons: *** [main.o] Error 1
> >>
> >> It seems dolfin::real is not double by default for the latest dolfin. Is it bug or I can fix it easily in my code?
> >> Any suggestion?
> >>
> >>
> >
> > Use 'double'.
> >
> > Garth
> >
>
> I tried with 'double'.
>
> void u0(double* u)
> {
> u[0] = 1.0;
> }
>
> void f(const double* u, double t, double* y)
> {
> y[0] = -1000.0 * u[0] + cos(t);
> }
>
> Then I got similar error
>
> main.cpp:64: error: cannot declare variable 'test_problem' to be of
> abstract type 'TestProblem1'
> TestProblem1.h:12: note: because the following virtual functions are
> pure within 'TestProblem1':
> /afs/nada.kth.se/home/x/u18gh7kx/data/Work/fenics/build/include/dolfin/ode/ODE.h:83:
> note: virtual void dolfin::ODE::u0(dolfin::real*)
>
> The I tried like this:
>
> void u0(real* u)
> {
> u[0] = 1.0;
> }
>
> void f(const real* u, double t, double* y)
> {
> y[0] = -1000.0 * u[0] + cos(t);
> }
>
> Still error:
>
> In file included from main.cpp:12:
> TestProblem1.h: In member function 'void TestProblem1::f(const
> dolfin::real*, double, double*)':
> TestProblem1.h:27: error: cannot convert '__gmp_expr<__mpf_struct [1],
> __gmp_binary_expr<__gmp_expr<__mpf_struct [1], __gmp_binary_expr<double,
> __gmp_expr<__mpf_struct [1], __mpf_struct [1]>, __gmp_binary_multiplies>
> >, double, __gmp_binary_plus> >' to 'double' in assignment
> scons: *** [main.o] Error 1

Looks like you compiled DOLFIN with GMP (GNU multiprecision) and then
you need to use real (not double) in the ODE solvers.

What happens above is that cos() is not defined for reals (only for
doubles). So you need to either compile DOLFIN without GMP, or
use

  y[0] = -1000.0 * u[0] + cos(to_double(t));

Which option you want depends on if you need extended precision. My
guess is you don't so just add enableGmp=no when compiling.

--
Anders

Revision history for this message
Murtazo Nazarov (murtazo) said :
#4

Thanks Anders Logg, that solved my question.