/***************************************************************************
 *   Copyright (C) 2007 by Armando Navarro Vazquez   *
 *   qoajnv@usc.es   *
 *                                                                         *
 *   This program 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.                                   *
 *                                                                         *
 *   This program 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <iomanip>
#include "cpmdwriter.h"

CPMDWriter::CPMDWriter ( CMolecule& molecule ) : m_molecule ( molecule )
{}


CPMDWriter::~CPMDWriter()
{}

std::ostream& operator << ( std::ostream& s, CPMDWriter& w )
{
  std::cout << "Exporting cpmd input file";
  s << "&CPMD" << std::endl << "&END" << std::endl;
  s << "&SYSTEM" << std::endl;
  s << "ANGSTROM" << std::endl;
  s << "&END" << std::endl;
  s << "&ATOMS" << std::endl;
  //OK lets do groups
  std::vector<CAtom>::const_iterator at;
  std::vector <std::string> names;
  std::vector<std::string>::const_iterator st;
  for ( at=w.m_molecule.m_atom.begin();at!=w.m_molecule.m_atom.end();++at )
  {

    for ( st=names.begin();st!=names.end();++st )
    {
      if ( ( *st ) == at->name ) break;
    }
    if ( ! ( st != names.end() ) ) names.push_back ( at->name );
  }
  for ( st=names.begin();st!=names.end();++st )
  {
    s << "*" << ( *st ) << ".psp" << std::endl;
    s << "LMAX=" << std::endl;
    size_t counter=0;
    for ( at=w.m_molecule.m_atom.begin();at!=w.m_molecule.m_atom.end();++at )
    {
      if ( at->name == ( *st ) ) ++counter;
    }
    s << " " << counter << std::endl;
    for ( at=w.m_molecule.m_atom.begin();at!=w.m_molecule.m_atom.end();++at )
    {
      if ( at->name == ( *st ) )
      {
        s << std::setw ( 10 ) << std::setiosflags ( std::ios::right )
        << std::setiosflags ( std::ios::fixed ) << std::setprecision ( 5 )
          << at->c.x << std::setw ( 10 ) << at->c.y << std::setw ( 10 ) << at->c.z
          << std::resetiosflags ( std::ios::right ) << std::endl;
      }
    }
  }
  s << "&END" << std::endl;

}



