/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2013 Frederik Heber. All rights reserved.
 * 
 *
 *   This file is part of MoleCuilder.
 *
 *    MoleCuilder is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    MoleCuilder is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with MoleCuilder.  If not, see .
 */
/*
 * CompoundPotentialUnitTest.cpp
 *
 *  Created on: May 08, 2013
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
using namespace std;
#include 
#include 
#include 
#include "CompoundPotentialUnitTest.hpp"
#include 
#include "CodePatterns/Assert.hpp"
#include "FunctionApproximation/FunctionArgument.hpp"
#include "Potentials/CompoundPotential.hpp"
#include "Potentials/helpers.hpp"
#include "Potentials/PotentialFactory.hpp"
#include "Potentials/PotentialRegistry.hpp"
#include "Potentials/Specifics/ConstantPotential.hpp"
#include "Potentials/Specifics/PairPotential_Morse.hpp"
using namespace boost::assign;
#ifdef HAVE_TESTRUNNER
#include "UnitTestMain.hpp"
#endif /*HAVE_TESTRUNNER*/
/********************************************** Test classes **************************************/
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( CompoundPotentialTest );
void CompoundPotentialTest::setUp()
{
  // failing asserts should be thrown
  ASSERT_DO(Assert::Throw);
  // register MorsePotential with registry
  {
    PairPotential_Morse::ParticleTypes_t types =
        boost::assign::list_of
          (0)(1)
        ;
    EmpiricalPotential *morse =
        PotentialFactory::getInstance().createInstance(
            std::string("morse"), types);
    PotentialRegistry::getInstance().registerInstance(morse);
    morse = NULL;
  }
  // register ConstantPotential with registry
  {
    ConstantPotential::ParticleTypes_t types;
    EmpiricalPotential *constant =
        PotentialFactory::getInstance().createInstance(
            std::string("constant"), types);
    PotentialRegistry::getInstance().registerInstance(constant);
    constant = NULL;
  }
  // create graph (i.e. this simulates a water molecule)
  {
    // add nodes
    nodes +=
        std::make_pair(FragmentNode(0,2),1),
        std::make_pair(FragmentNode(1,1),2);
    // add edges
    edges +=
        std::make_pair(FragmentEdge(0,1),2);
    // construct graph
    graph = new HomologyGraph(nodes, edges);
  }
  // data is taken from gnuplot via set table "morse.dat" with
  // g(x)=D*(1- exp(-a*(x-c)))**2+d
  a = 0.897888;
  c = 2.92953;
  d = -78.9883;
  D = 0.196289;
  input +=
      1.89012,
      2.17632,
      2.46253,
      2.74873,
      3.03493,
      3.32114,
      3.60734,
      3.89354,
      4.17974,
      4.46595;
  output +=
      2.*0.467226+d,
      2.*0.183388+d,
      2.*0.0532649+d,
      2.*0.00609808+d,
      2.*0.00160056+d,
      2.*0.0172506+d,
      2.*0.0407952+d,
      2.*0.0658475+d,
      2.*0.0893157+d,
      2.*0.109914+d;
  CPPUNIT_ASSERT_EQUAL( input.size(), output.size() );
}
void CompoundPotentialTest::tearDown()
{
  delete graph;
  PotentialFactory::purgeInstance();
  PotentialRegistry::getInstance().cleanup();
  PotentialRegistry::purgeInstance();
}
/** UnitTest for operator()
 */
void CompoundPotentialTest::operatorTest()
{
  CompoundPotential compound(*graph);
  CompoundPotential::parameters_t params;
  params += d,a,c,D;
  compound.setParameters(params);
  for (size_t index = 0; index < input.size(); ++index) {
    argument_t firstarg(argument_t::indices_t(0,1), argument_t::types_t(0,1), input[index]);
    argument_t secondarg(argument_t::indices_t(0,2), argument_t::types_t(0,1), input[index]);
    FunctionModel::arguments_t args;
    args += firstarg,secondarg;
    const double result = compound( args )[0];
    CPPUNIT_ASSERT(
        Helpers::isEqual(
            output[index],
            result,
            1.e-4/std::numeric_limits::epsilon() // only compare four digits
        )
    );
  }
}
/** UnitTest for derivative()
 */
//void CompoundPotentialTest::derivativeTest()
//{
//  CompoundPotential compound(*graph);
//  CompoundPotential::parameters_t params;
//  params += d,a,c,D;
//  compound.setParameters(params);
//  argument_t firstarg(argument_t::indices_t(0,1), argument_t::types_t(0,1), input[index]);
//  argument_t secondarg(argument_t::indices_t(0,2), argument_t::types_t(0,1), input[index]);
//  FunctionModel::arguments_t args;
//  args += firstarg,secondarg;
//  const double result = compound.derivative( args )[0]
//  CPPUNIT_ASSERT(
//      Helpers::isEqual(
//          0.,
//          result,
//          1.e+6
//      )
//  );
//}
/** UnitTest for parameter_derivative()
 */
void CompoundPotentialTest::parameter_derivativeTest()
{
  CompoundPotential compound(*graph);
  CompoundPotential::parameters_t params;
  params += d,a,c,D;
  compound.setParameters(params);
  argument_t firstarg(argument_t::indices_t(0,1), argument_t::types_t(0,1), c);
  argument_t secondarg(argument_t::indices_t(0,2), argument_t::types_t(0,1), c);
  FunctionModel::arguments_t args;
  args += firstarg,secondarg;
  {
    const double result =
        compound.parameter_derivative(
            args,
            0)[0];
    CPPUNIT_ASSERT(
        Helpers::isEqual(
            1.,
            result,
            1.e+6
        )
    );
  }
  {
    const double result =
        compound.parameter_derivative(
            args,
            1)[0];
    CPPUNIT_ASSERT(
        Helpers::isEqual(
            0.,
            result,
            1.e+6
        )
    );
  }
  {
    const double result =
        compound.parameter_derivative(
            args,
            2)[0];
    CPPUNIT_ASSERT(
        Helpers::isEqual(
            0.,
            result,
            1.e+6
        )
    );
  }
  {
    const double result =
        compound.parameter_derivative(
            args,
            3)[0];
    CPPUNIT_ASSERT(
        Helpers::isEqual(
            0.,
            result,
            1.e+6
        )
    );
  }
}