Segfault when using NonlinearVariationalSolver in c++

Asked by Ennio Barbaro

Hi,
I am trying to setup a NonlinearVariationalSolver in c++

My code:

 NonlinearVariationalProblem bl_problem(F, u, bcs, J);
 NonlinearVariationalSolver solver(bl_problem);
 solver.solve();

Unfortunatly i get a segfault when calling solver.solve().
If instead i use

 solve( F == 0,u, bcs,J);

Everything works fine.

I am using dolfin 1.2.x from launchpad, compiled with gcc-4.8.
I don't believe this is a bug in dolfin, am I just missing something? (Spent last two days recompiling everything fenics-releated to make sure i used the same versions/compilers).
Thanks in advance for help!

Backtrace:
0x00007ffff7409950 in dolfin::DirichletBC::compute_bc_topological (this=this@entry=0x1138a10, boundary_values=..., data=...) at /home/ennio/projects/fenics/dolfin/src/build/dolfin/fem/DirichletBC.cpp:802
802 _g->restrict(&data.w[0], *_function_space->element(), cell, ufc_cell);
(gdb) bt
#0 0x00007ffff7409950 in dolfin::DirichletBC::compute_bc_topological (this=this@entry=0x1138a10, boundary_values=..., data=...) at /home/ennio/projects/fenics/dolfin/src/build/dolfin/fem/DirichletBC.cpp:802
#1 0x00007ffff740c1a9 in dolfin::DirichletBC::compute_bc (this=this@entry=0x1138a10, boundary_values=..., data=..., method="topological") at /home/ennio/projects/fenics/dolfin/src/build/dolfin/fem/DirichletBC.cpp:750
#2 0x00007ffff740d0cf in dolfin::DirichletBC::apply (this=0x1138a10, A=0x0, b=0x1124210, x=0x1139910) at /home/ennio/projects/fenics/dolfin/src/build/dolfin/fem/DirichletBC.cpp:498
#3 0x00007ffff744b931 in dolfin::NonlinearVariationalSolver::NonlinearDiscreteProblem::F (this=0x1d19830, b=..., x=...) at /home/ennio/projects/fenics/dolfin/src/build/dolfin/fem/NonlinearVariationalSolver.cpp:158
#4 0x00007ffff794a429 in dolfin::NewtonSolver::solve (this=0x1d1bdd0, nonlinear_problem=..., x=...) at /home/ennio/projects/fenics/dolfin/src/build/dolfin/nls/NewtonSolver.cpp:103
#5 0x00007ffff744d7d2 in dolfin::NonlinearVariationalSolver::solve (this=0x7fffffffccb0) at /home/ennio/projects/fenics/dolfin/src/build/dolfin/fem/NonlinearVariationalSolver.cpp:94
#6 0x0000000000419951 in main () at /home/ennio/projects/tesi/main.cpp:118

Valgrind says:
==3718== Invalid read of size 8
==3718== at 0x5336950: dolfin::DirichletBC::compute_bc_topological(boost::unordered::unordered_map<unsigned long, double, boost::hash<unsigned long>, std::equal_to<unsigned long>, std::allocator<std::pair<unsigned long const, double> > >&, dolfin::BoundaryCondition::LocalData&) const (DirichletBC.cpp:802)
==3718== by 0x53391A8: dolfin::DirichletBC::compute_bc(boost::unordered::unordered_map<unsigned long, double, boost::hash<unsigned long>, std::equal_to<unsigned long>, std::allocator<std::pair<unsigned long const, double> > >&, dolfin::BoundaryCondition::LocalData&, std::string) const (DirichletBC.cpp:750)
==3718== by 0x533A0CE: dolfin::DirichletBC::apply(dolfin::GenericMatrix*, dolfin::GenericVector*, dolfin::GenericVector const*) const (DirichletBC.cpp:498)
==3718== by 0x5378930: dolfin::NonlinearVariationalSolver::NonlinearDiscreteProblem::F(dolfin::GenericVector&, dolfin::GenericVector const&) (NonlinearVariationalSolver.cpp:158)
==3718== by 0x5877428: dolfin::NewtonSolver::solve(dolfin::NonlinearProblem&, dolfin::GenericVector&) (NewtonSolver.cpp:103)
==3718== by 0x537A7D1: dolfin::NonlinearVariationalSolver::solve() (NonlinearVariationalSolver.cpp:94)
==3718== by 0x418C00: main (main.cpp:120)
==3718== Address 0x38 is not stack'd, malloc'd or (recently) free'd

cmake optional packages used when building dolfin

-- The following optional packages were found:
-- -------------------------------------------
-- (OK) OPENMP
-- (OK) MPI
-- (OK) PETSC
-- (OK) TRILINOS
-- (OK) UMFPACK
-- (OK) CHOLMOD
-- (OK) SCOTCH
-- (OK) PARMETIS
-- (OK) CGAL
-- (OK) ZLIB
-- (OK) PYTHON
-- (OK) SPHINX
-- (OK) VTK
-- (OK) QT
--
-- The following optional packages were not be found:
-- --------------------------------------------------
-- (**) SLEPC
-- (**) PASTIX
-- (**) HDF5

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Ennio Barbaro
Solved:
Last query:
Last reply:
Revision history for this message
Ennio Barbaro (e-barbaro) said :
#1

I restricted the source of the error:

 SubSpace VUx (V,0);

 //Setup boundary conditions
 std::vector<const BoundaryCondition *> bcs;

 //Inflow condition
 InflowBoundary inflow_boundary;
 InputV u0;
 bcs.push_back( new DirichletBC( V, u0, inflow_boundary)); //works fine

 //Freestream condition
 FreestreamBoundary freestream_boundary;
 Constant u_freestream(1.0);
 Constant w_freestream(0.0);

 bcs.push_back( new DirichletBC( VUx, u_freestream, freestream_boundary) ); //crash
 bcs.push_back( new DirichletBC( VUz, w_freestream, freestream_boundary) ); //crash too

The problem seems releated to the interaction between SubSpace and DirichletBC

Source code:
http://pastebin.com/K9cKQM1V

Ufl:
http://pastebin.com/FnjJ9WqM

Revision history for this message
Ennio Barbaro (e-barbaro) said :
#2

Turns out that while libdolfin was compiled with the HAS_PETSC flag defined, my program was not.
This breaks the ODR in the definition of NonlinearVariationalSolver.

So this bring the question, is there a recommended way to setup compile flags? Right now I am using:
add_definitions(${DOLFIN_CXX_DEFINITIONS})