/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2010-2012 University of Bonn. 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 .
 */
/*
 * AnalysisBondsUnitTest.cpp
 *
 *  Created on: Nov 7, 2009
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
using namespace std;
#include 
#include 
#include 
#include 
#include 
#include 
#include "Analysis/analysis_bonds.hpp"
#include "Atom/atom.hpp"
#include "Bond/bond.hpp"
#include "Element/element.hpp"
#include "Graph/BondGraph.hpp"
#include "molecule.hpp"
#include "Element/periodentafel.hpp"
#include "LinearAlgebra/Vector.hpp"
#include "World.hpp"
#include "AnalysisBondsUnitTest.hpp"
#ifdef HAVE_TESTRUNNER
#include "UnitTestMain.hpp"
#endif /*HAVE_TESTRUNNER*/
/********************************************** Test classes **************************************/
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( AnalysisBondsTest );
void AnalysisBondsTest::setUp()
{
  atom *Walker = NULL;
  // get elements
  hydrogen = World::getInstance().getPeriode()->FindElement(1);
  carbon = World::getInstance().getPeriode()->FindElement(6);
  CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen");
  CPPUNIT_ASSERT(carbon != NULL && "could not find element carbon");
  // construct molecule (tetraeder of hydrogens)
  TestMolecule = World::getInstance().createMolecule();
  CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule");
  Walker = World::getInstance().createAtom();
  CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
  Walker->setType(hydrogen);
  Walker->setPosition(Vector(1.5, 0., 1.5 ));
  TestMolecule->AddAtom(Walker);
  Walker = World::getInstance().createAtom();
  CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
  Walker->setType(hydrogen);
  Walker->setPosition(Vector(0., 1.5, 1.5 ));
  TestMolecule->AddAtom(Walker);
  Walker = World::getInstance().createAtom();
  CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
  Walker->setType(hydrogen);
  Walker->setPosition(Vector(1.5, 1.5, 0. ));
  TestMolecule->AddAtom(Walker);
  Walker = World::getInstance().createAtom();
  CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
  Walker->setType(hydrogen);
  Walker->setPosition(Vector(0., 0., 0. ));
  TestMolecule->AddAtom(Walker);
  Walker = World::getInstance().createAtom();
  CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
  Walker->setType(carbon);
  Walker->setPosition(Vector(0.5, 0.5, 0.5 ));
  TestMolecule->AddAtom(Walker);
  // check that TestMolecule was correctly constructed
  CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 5 );
  // create stream with table
  std::stringstream test;
  test << ".\tH\tHe\tLi\tBe\tB\tC\n";
  test << "H\t1.\t1.\t1.\t1.\t1.\t1.2\n";
  test << "He\t1.\t1.\t1.\t1.\t1.\t1.\n";
  test << "Li\t1.\t1.\t1.\t1.\t1.\t1.\n";
  test << "Be\t1.\t1.\t1.\t1.\t1.\t1.\n";
  test << "B\t1.\t1.\t1.\t1.\t1.\t1.\n";
  test << "C\t1.2\t1.\t1.\t1.\t1.\t1.5\n";
  BG = new BondGraph(true);
  CPPUNIT_ASSERT(BG != NULL && "could not create BondGraph");
  CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(test) );
  CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
  CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,5) );
  CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(5,5) );
  World::AtomComposite Set = TestMolecule->getAtomSet();
  BG->CreateAdjacency(Set);
};
void AnalysisBondsTest::tearDown()
{
  // remove the file
  delete(BG);
  // remove molecule
  World::getInstance().destroyMolecule(TestMolecule);
  // note that all the atoms are cleaned by TestMolecule
  World::purgeInstance();
};
/** UnitTest for GetMaxMinMeanBondCount().
 */
void AnalysisBondsTest::GetMaxMinMeanBondCountTest()
{
  double Min = 20.; // check that initialization resets these arbitrary values
  double Mean = 200.;
  double Max = 1e-6;
  GetMaxMinMeanBondCount(TestMolecule, Min, Mean, Max);
  CPPUNIT_ASSERT_EQUAL( 1., Min );
  CPPUNIT_ASSERT_EQUAL( 1.6, Mean );
  CPPUNIT_ASSERT_EQUAL( 4., Max );
};
/** UnitTest for MinMaxBondDistanceBetweenElements().
 */
void AnalysisBondsTest::MinMeanMaxBondDistanceBetweenElementsTest()
{
  double Min = 20.; // check that initialization resets these arbitrary values
  double Mean = 2e+6;
  double Max = 1e-6;
  double Min2 = 20.;
  double Mean2 = 2e+6;
  double Max2 = 1e-6;
  const double maxbondlength = sqrt(1.*1. + 1.*1. + .5*.5);
  const double minbondlength = sqrt(.5*.5 + .5*.5 + .5*.5);
  const double meanbondlength = (minbondlength+3.*maxbondlength)/4.;
  // check bond lengths C-H
  MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, carbon, Min, Mean, Max);
  CPPUNIT_ASSERT( fabs( minbondlength - Min ) < MYEPSILON);
  CPPUNIT_ASSERT( fabs( meanbondlength - Mean ) < MYEPSILON);
  CPPUNIT_ASSERT( fabs( maxbondlength - Max ) < MYEPSILON);
  // check that elements are symmetric, i.e. C-H == H-C
  MinMeanMaxBondDistanceBetweenElements(TestMolecule, carbon, hydrogen, Min2, Mean2, Max2);
  CPPUNIT_ASSERT( fabs( Min - Min2 ) < MYEPSILON);
  CPPUNIT_ASSERT( fabs( Mean - Mean2 ) < MYEPSILON);
  CPPUNIT_ASSERT( fabs( Max - Max2 ) < MYEPSILON);
  // check no bond case (no bonds H-H in system!)
  MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, hydrogen, Min, Mean, Max);
  CPPUNIT_ASSERT_EQUAL( 0. , Min );
  CPPUNIT_ASSERT_EQUAL( 0. , Mean );
  CPPUNIT_ASSERT_EQUAL( 0. , Max );
};