This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Nonlinear Poisson error

0 votes

I am trying to solve the nonlinear Poisson equation of the form

$\frac{d}{d x} \left[ \left( \frac{du}{dx} \right)^2 \right] = f$

where $f=2x$ and $u = \frac{1}{4}$ at $x=0$. I believe the exact solution is $u=\frac{1}{2} x^2 + \frac{1}{4}$. I have the following UFL file:

cell = interval

scalar = FiniteElement("Lagrange", cell, 2)

phi = TestFunction(scalar)
u = Coefficient(scalar) 
f = Coefficient(scalar) 

F = -(u.dx(0)**2.0)*phi.dx(0)*dx - f*phi*dx

du = TrialFunction(scalar)
J = derivative(F, u, du)

and C++ code:

class Source : public Expression { 
public:
  Source() : Expression() {}

  void eval(Array<double>& values, const Array<double>& coord) const {
    const double x = coord[0];

    values[0] = 2.0*x;
  }
};

class LeftBoundary : public SubDomain {
  bool inside(const Array<double>& coord, bool on_boundary) const {
    return abs(coord[0])<DOLFIN_EPS && on_boundary;
  }
};

int main(int argc, char *argv[]) {
// create mesh
  auto mesh = make_shared<UnitIntervalMesh>(100);

  // create function spaces
  auto V = make_shared<NonlinearPoisson::FunctionSpace>(mesh);

  // boundary condition
  auto left = make_shared<LeftBoundary>();
  auto u0 = make_shared<Constant>(0.25);
  DirichletBC leftBC(V, u0, left);

  // source
  auto u = make_shared<Function>(V); // unknown velocity
  auto f = make_shared<Source>();

  // create residual form --- (nonlinear) stress balance equations
  NonlinearPoisson::LinearForm F(V);
  F.u = u;
  F.f = f;

  // create Jacobian form J = F' (for the nonlinear solver)
  NonlinearPoisson::JacobianForm J(V, V);
  J.u = u;

  // solver parameters
  Parameters params("nonlinear_variational_solver");
  Parameters newtonParams("newton_solver");
  newtonParams.add("relative_tolerance", 1e-6);
  params.add(newtonParams);

  // solve for the initial velocities
  solve(F==0, *u, leftBC, J, params);
}

which complies fine. However, I get a runtime error:

Solving nonlinear variational problem.
  Newton iteration 0: r (abs) = nan (tol = 1.000e-10) r (rel) = nan (tol = 1.000e-06)
  Newton iteration 1: r (abs) = nan (tol = 1.000e-10) r (rel) = nan (tol = 1.000e-06)
  Newton iteration 2: r (abs) = nan (tol = 1.000e-10) r (rel) = nan (tol = 1.000e-06)
  Newton iteration 3: r (abs) = nan (tol = 1.000e-10) r (rel) = nan (tol = 1.000e-06)

Does anyone know why the solver is producing 'nan's?

asked Oct 7, 2016 by davisad FEniCS Novice (470 points)
edited Oct 7, 2016 by davisad

1 Answer

+1 vote
 
Best answer

When you create

   auto u = make_shared<Function>(V); // unknown velocity

u is set equal to 0. Then you use this value of u as initial guess for the nonlinear solver. However, the jacobian J is singular (actually equal to zero) for u = constant. You should choose a better initial guess.

answered Oct 8, 2016 by francesco.ballarin FEniCS User (4,070 points)
selected Oct 9, 2016 by davisad

Thanks! That fixed part of the problem. I added

class UGuess : public dfn::Expression { 
public:
  UGuess() : dfn::Expression() {}

  void eval(dfn::Array<double>& values, const dfn::Array<double>& coord) const {
    const double x = coord[0];

    values[0] = 0.5*x*x+0.25;
  }
};

and

  auto ug = make_shared<UGuess>();
  *u = *ug;

which got rid of the 'nan's. Now the initial guess should be the exact solution but I'm getting the error

Solving nonlinear variational problem.
  *** Warning: Using PETSc native LU solver. Consider configuring PETSc with an efficient LU solver (e.g. UMFPACK, MUMPS).
  Newton iteration 0: r (abs) = 1.000e+00 (tol = 1.000e-10) r (rel) = 1.000e+00 (tol = 1.000e-06)
  Newton iteration 1: r (abs) = 6.004e+04 (tol = 1.000e-10) r (rel) = 6.004e+04 (tol = 1.000e-06)
  Newton iteration 2: r (abs) = 4.220e+04 (tol = 1.000e-10) r (rel) = 4.220e+04 (tol = 1.000e-06)
  Newton iteration 3: r (abs) = 5.567e+04 (tol = 1.000e-10) r (rel) = 5.567e+04 (tol = 1.000e-06)
  Newton iteration 4: r (abs) = 1.400e+04 (tol = 1.000e-10) r (rel) = 1.400e+04 (tol = 1.000e-06)
  Newton iteration 5: r (abs) = 8.913e+04 (tol = 1.000e-10) r (rel) = 8.913e+04 (tol = 1.000e-06)
  Newton iteration 6: r (abs) = 2.244e+04 (tol = 1.000e-10) r (rel) = 2.244e+04 (tol = 1.000e-06)
  Newton iteration 7: r (abs) = 5.713e+03 (tol = 1.000e-10) r (rel) = 5.713e+03 (tol = 1.000e-06)
  Newton iteration 8: r (abs) = 3.762e+03 (tol = 1.000e-10) r (rel) = 3.762e+03 (tol = 1.000e-06)
  Newton iteration 9: r (abs) = 1.913e+04 (tol = 1.000e-10) r (rel) = 1.913e+04 (tol = 1.000e-06)
  Newton iteration 10: r (abs) = 1.166e+05 (tol = 1.000e-10) r (rel) = 1.166e+05 (tol = 1.000e-06)
  Newton iteration 11: r (abs) = 2.916e+04 (tol = 1.000e-10) r (rel) = 2.916e+04 (tol = 1.000e-06)
  Newton iteration 12: r (abs) = 7.297e+03 (tol = 1.000e-10) r (rel) = 7.297e+03 (tol = 1.000e-06)
  Newton iteration 13: r (abs) = 4.652e+03 (tol = 1.000e-10) r (rel) = 4.652e+03 (tol = 1.000e-06)
  Newton iteration 14: r (abs) = 3.134e+03 (tol = 1.000e-10) r (rel) = 3.134e+03 (tol = 1.000e-06)
  Newton iteration 15: r (abs) = 1.183e+05 (tol = 1.000e-10) r (rel) = 1.183e+05 (tol = 1.000e-06)
  Newton iteration 16: r (abs) = 2.962e+04 (tol = 1.000e-10) r (rel) = 2.962e+04 (tol = 1.000e-06)
  Newton iteration 17: r (abs) = 5.478e+04 (tol = 1.000e-10) r (rel) = 5.478e+04 (tol = 1.000e-06)
  Newton iteration 18: r (abs) = 1.463e+04 (tol = 1.000e-10) r (rel) = 1.463e+04 (tol = 1.000e-06)
  Newton iteration 19: r (abs) = 2.578e+04 (tol = 1.000e-10) r (rel) = 2.578e+04 (tol = 1.000e-06)
  Newton iteration 20: r (abs) = 6.819e+03 (tol = 1.000e-10) r (rel) = 6.819e+03 (tol = 1.000e-06)
  Newton iteration 21: r (abs) = 4.139e+03 (tol = 1.000e-10) r (rel) = 4.139e+03 (tol = 1.000e-06)
  Newton iteration 22: r (abs) = 1.407e+05 (tol = 1.000e-10) r (rel) = 1.407e+05 (tol = 1.000e-06)
  Newton iteration 23: r (abs) = 3.522e+04 (tol = 1.000e-10) r (rel) = 3.522e+04 (tol = 1.000e-06)
  Newton iteration 24: r (abs) = 1.915e+04 (tol = 1.000e-10) r (rel) = 1.915e+04 (tol = 1.000e-06)
  Newton iteration 25: r (abs) = 4.059e+04 (tol = 1.000e-10) r (rel) = 4.059e+04 (tol = 1.000e-06)
  Newton iteration 26: r (abs) = 1.653e+04 (tol = 1.000e-10) r (rel) = 1.653e+04 (tol = 1.000e-06)
  Newton iteration 27: r (abs) = 5.030e+05 (tol = 1.000e-10) r (rel) = 5.030e+05 (tol = 1.000e-06)
  Newton iteration 28: r (abs) = 1.258e+05 (tol = 1.000e-10) r (rel) = 1.258e+05 (tol = 1.000e-06)
  Newton iteration 29: r (abs) = 3.164e+04 (tol = 1.000e-10) r (rel) = 3.164e+04 (tol = 1.000e-06)
  Newton iteration 30: r (abs) = 7.918e+03 (tol = 1.000e-10) r (rel) = 7.918e+03 (tol = 1.000e-06)
  Newton iteration 31: r (abs) = 2.149e+03 (tol = 1.000e-10) r (rel) = 2.149e+03 (tol = 1.000e-06)
  Newton iteration 32: r (abs) = 8.087e+04 (tol = 1.000e-10) r (rel) = 8.087e+04 (tol = 1.000e-06)
  Newton iteration 33: r (abs) = 2.750e+04 (tol = 1.000e-10) r (rel) = 2.750e+04 (tol = 1.000e-06)
  Newton iteration 34: r (abs) = 8.274e+05 (tol = 1.000e-10) r (rel) = 8.274e+05 (tol = 1.000e-06)
  Newton iteration 35: r (abs) = 2.067e+05 (tol = 1.000e-10) r (rel) = 2.067e+05 (tol = 1.000e-06)
  Newton iteration 36: r (abs) = 5.382e+04 (tol = 1.000e-10) r (rel) = 5.382e+04 (tol = 1.000e-06)
  Newton iteration 37: r (abs) = 2.963e+04 (tol = 1.000e-10) r (rel) = 2.963e+04 (tol = 1.000e-06)
  Newton iteration 38: r (abs) = 1.208e+05 (tol = 1.000e-10) r (rel) = 1.208e+05 (tol = 1.000e-06)
  Newton iteration 39: r (abs) = 3.021e+04 (tol = 1.000e-10) r (rel) = 3.021e+04 (tol = 1.000e-06)
  Newton iteration 40: r (abs) = 2.607e+06 (tol = 1.000e-10) r (rel) = 2.607e+06 (tol = 1.000e-06)
  Newton iteration 41: r (abs) = 6.518e+05 (tol = 1.000e-10) r (rel) = 6.518e+05 (tol = 1.000e-06)
  Newton iteration 42: r (abs) = 1.630e+05 (tol = 1.000e-10) r (rel) = 1.630e+05 (tol = 1.000e-06)
  Newton iteration 43: r (abs) = 1.389e+05 (tol = 1.000e-10) r (rel) = 1.389e+05 (tol = 1.000e-06)
  Newton iteration 44: r (abs) = 3.472e+04 (tol = 1.000e-10) r (rel) = 3.472e+04 (tol = 1.000e-06)
  Newton iteration 45: r (abs) = 1.345e+06 (tol = 1.000e-10) r (rel) = 1.345e+06 (tol = 1.000e-06)
  Newton iteration 46: r (abs) = 3.371e+05 (tol = 1.000e-10) r (rel) = 3.371e+05 (tol = 1.000e-06)
  Newton iteration 47: r (abs) = 8.775e+04 (tol = 1.000e-10) r (rel) = 8.775e+04 (tol = 1.000e-06)
  Newton iteration 48: r (abs) = 2.076e+04 (tol = 1.000e-10) r (rel) = 2.076e+04 (tol = 1.000e-06)
  Newton iteration 49: r (abs) = 8.666e+03 (tol = 1.000e-10) r (rel) = 8.666e+03 (tol = 1.000e-06)
  Newton iteration 50: r (abs) = 3.828e+03 (tol = 1.000e-10) r (rel) = 3.828e+03 (tol = 1.000e-06)
terminate called after throwing an instance of 'std::runtime_error'
  what():  

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
***     fenics-support@googlegroups.com
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to solve nonlinear system with NewtonSolver.
*** Reason:  Newton solver did not converge because maximum number of iterations reached.
*** Where:   This error was encountered inside NewtonSolver.cpp.
*** Process: 0
*** 
*** DOLFIN version: 2016.2.0.dev0
*** Git changeset:  7de058e14dd8bc4c929083f7d8dcc75ecbefa2f9
*** -------------------------------------------------------------------------

Any thoughts?

...