problem with compiling cpp code of an example problem from FEniCS book

Asked by Xiaoxian Liu

I typed the example in Chapter 33 of the FEniCS book. On page 620, it has a demonstration of applying SUPG scheme in dolfin.
I typed this Python code on this page to a file and run it from terminal, but it said the code didn't compile.

The content of the python code is copied below:
------
from dolfin import *
import numpy as np

cppcode = """
class Stab : public Expression {
public:
  double sigma; boost::shared_ptr<GenericFunction> field;
  boost::shared_ptr<Mesh> mesh;
  Stab(): Expression(3), sigma(1.0e5){};
  void eval(Array<double>& v, const Array<double>& x, const ufc:cell& c) const{
    Cell cell(*mesh, c.index);
    double field_norm = 0.0; double tau = 0.0
    double h = cell.diameter();
    field->eval(v,x,c);
    for (uint i=0; i<x.size(); ++i)
      field_norm += v[i]*v[i];
    field_norm = sqrt(field_norm);
    double PE = 0.5*field_norm * h/sigma;
    if (PE > DOLFIN_EPS)
      tau = 1/tanh(PE) - 1/PE;
    for (uint i=0; i<x.size(); ++i)
      v[i] *= 0.5*h*tau/field_norm;}};
"""
s = Expression(cppcode)
------ (end of code)

And the error message reads,
-----------------------------------
...
Traceback (most recent call last):
  File "compile_cpp.py", line 25, in <module>
    s = Expression(cppcode)
  File "/usr/lib/python2.7/dist-packages/dolfin/functions/expression.py", line 478, in __new__
    cpp_base, members = compile_expressions([cppcode])
  File "/usr/lib/python2.7/dist-packages/dolfin/compilemodules/expressions.py", line 188, in compile_expressions
    code_snippets), classnames)
  File "/usr/lib/python2.7/dist-packages/dolfin/compilemodules/expressions.py", line 126, in compile_expression_code
    dolfin_module_import=["function", "mesh", "common"])
  File "/usr/lib/python2.7/dist-packages/dolfin/compilemodules/jit.py", line 66, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/dolfin/compilemodules/compilemodule.py", line 470, in compile_extension_module
    **instant_kwargs)
  File "/usr/lib/python2.7/dist-packages/instant/build.py", line 492, in build_module
    recompile(modulename, module_path, setup_name, new_compilation_checksum)
  File "/usr/lib/python2.7/dist-packages/instant/build.py", line 115, in recompile
    "compile, see '%s'" % compile_log_filename_dest)
  File "/usr/lib/python2.7/dist-packages/instant/output.py", line 49, in instant_error
    raise RuntimeError(text)
RuntimeError: In instant.recompile: The module did not compile...
------------------------

I was trying to learn to write cpp code for complex Expression, but it really confused me that even an example from the book didn't run... Did I make any stupid mistake? Please help me. Thanks a lot!

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Jan Blechta
Solved:
Last query:
Last reply:
Revision history for this message
Best Jan Blechta (blechta) said :
#1

On Fri, 05 Apr 2013 22:56:03 -0000
Xiaoxian Liu <email address hidden> wrote:
> New question #226021 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/226021
>
> I typed the example in Chapter 33 of the FEniCS book. On page 620, it
> has a demonstration of applying SUPG scheme in dolfin. I typed this
> Python code on this page to a file and run it from terminal, but it
> said the code didn't compile.
>
> The content of the python code is copied below:
> ------
> from dolfin import *
> import numpy as np
>
> cppcode = """
> class Stab : public Expression {
> public:
> double sigma; boost::shared_ptr<GenericFunction> field;
> boost::shared_ptr<Mesh> mesh;
> Stab(): Expression(3), sigma(1.0e5){};
> void eval(Array<double>& v, const Array<double>& x, const ufc:cell&
> c) const{ Cell cell(*mesh, c.index);
> double field_norm = 0.0; double tau = 0.0
> double h = cell.diameter();
> field->eval(v,x,c);
> for (uint i=0; i<x.size(); ++i)
> field_norm += v[i]*v[i];
> field_norm = sqrt(field_norm);
> double PE = 0.5*field_norm * h/sigma;
> if (PE > DOLFIN_EPS)
> tau = 1/tanh(PE) - 1/PE;
> for (uint i=0; i<x.size(); ++i)
> v[i] *= 0.5*h*tau/field_norm;}};
> """
> s = Expression(cppcode)
> ------ (end of code)
>
> And the error message reads,
> -----------------------------------
> ...
> Traceback (most recent call last):
> File "compile_cpp.py", line 25, in <module>
> s = Expression(cppcode)
> File
> "/usr/lib/python2.7/dist-packages/dolfin/functions/expression.py",
> line 478, in __new__ cpp_base, members =
> compile_expressions([cppcode]) File
> "/usr/lib/python2.7/dist-packages/dolfin/compilemodules/expressions.py",
> line 188, in compile_expressions code_snippets), classnames) File
> "/usr/lib/python2.7/dist-packages/dolfin/compilemodules/expressions.py",
> line 126, in compile_expression_code
> dolfin_module_import=["function", "mesh", "common"]) File
> "/usr/lib/python2.7/dist-packages/dolfin/compilemodules/jit.py", line
> 66, in mpi_jit return local_jit(*args, **kwargs) File
> "/usr/lib/python2.7/dist-packages/dolfin/compilemodules/compilemodule.py",
> line 470, in compile_extension_module **instant_kwargs) File
> "/usr/lib/python2.7/dist-packages/instant/build.py", line 492, in
> build_module recompile(modulename, module_path, setup_name,
> new_compilation_checksum) File
> "/usr/lib/python2.7/dist-packages/instant/build.py", line 115, in
> recompile "compile, see '%s'" % compile_log_filename_dest) File
> "/usr/lib/python2.7/dist-packages/instant/output.py", line 49, in
> instant_error raise RuntimeError(text) RuntimeError: In
> instant.recompile: The module did not compile...
> ------------------------
>
> I was trying to learn to write cpp code for complex Expression, but
> it really confused me that even an example from the book didn't
> run... Did I make any stupid mistake? Please help me. Thanks a lot!
>
>
>
>

There are two syntax errors:
1. missing double colon in "const ufc::cell& c"
2. missing semicolon after "double tau = 0.0;"

Next time you need to look into ~/.instant/error/foo/compile.log if you
can't jit compile c++ code.

Jan

Revision history for this message
Xiaoxian Liu (liuxiaox) said :
#2

Thanks Jan Blechta, that solved my question.