/* Checks if the algorithm converges regardless of where it starts */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <cassert>

#include <dune/common/shared_ptr.hh>

#include <dune/tectonic/ellipticenergy.hh>

#include "test-gradient-method-nicefunction.hh"
#include "test-gradient-method-helper.hh"

int main() {
  int const dim = 2;
  typedef Dune::EllipticEnergy<dim> Functional;
  typedef Functional::SmallMatrix SmallMatrix;
  typedef Functional::SmallVector SmallVector;

  SmallMatrix const A = { { 1, 0 }, { 0, 1 } };
  SmallVector const b = { 1, 2.5 };

  auto const f = Dune::make_shared<Dune::SampleFunction<100> const>();
  auto const phi = Dune::make_shared<Functional::NonlinearityType const>(f);
  Functional const J(A, b, phi);

  double ret1;
  {
    SmallVector start = { 0, 1 };
    ret1 = functionTester(J, start, 2);
  }

  double ret2;
  {
    // Something random
    SmallVector start = { 279, -96 };
    ret2 = functionTester(J, start, 4);
  }
  assert(std::abs(ret1 - ret2) < 1e-8);

  double ret3;
  {
    SmallVector start = { 0, 0 };
    ret3 = functionTester(J, start, 1);
  }
  assert(std::abs(ret1 - ret3) < 1e-5);
}