| [ab4b55] | 1 | /*
 | 
|---|
 | 2 |  * XyzParser.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Mar 2, 2010
 | 
|---|
 | 5 |  *      Author: metzler
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [bf3817] | 8 | // include config.h
 | 
|---|
 | 9 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 10 | #include <config.h>
 | 
|---|
 | 11 | #endif
 | 
|---|
 | 12 | 
 | 
|---|
| [112b09] | 13 | #include "Helpers/MemDebug.hpp"
 | 
|---|
 | 14 | 
 | 
|---|
| [e97a44] | 15 | #include "Helpers/Log.hpp"
 | 
|---|
 | 16 | #include "Helpers/Verbose.hpp"
 | 
|---|
| [ab4b55] | 17 | #include "XyzParser.hpp"
 | 
|---|
 | 18 | #include "World.hpp"
 | 
|---|
 | 19 | #include "atom.hpp"
 | 
|---|
| [dddbfe] | 20 | #include "molecule.hpp"
 | 
|---|
| [ab4b55] | 21 | #include "element.hpp"
 | 
|---|
 | 22 | #include "periodentafel.hpp"
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | using namespace std;
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | /**
 | 
|---|
 | 27 |  * Constructor.
 | 
|---|
 | 28 |  */
 | 
|---|
| [97b825] | 29 | XyzParser::XyzParser() :
 | 
|---|
 | 30 |   comment("")
 | 
|---|
 | 31 | {}
 | 
|---|
| [ab4b55] | 32 | 
 | 
|---|
 | 33 | /**
 | 
|---|
 | 34 |  * Destructor.
 | 
|---|
 | 35 |  */
 | 
|---|
 | 36 | XyzParser::~XyzParser() {
 | 
|---|
 | 37 | }
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | /**
 | 
|---|
 | 40 |  * Loads an XYZ file into the World.
 | 
|---|
 | 41 |  *
 | 
|---|
 | 42 |  * \param XYZ file
 | 
|---|
 | 43 |  */
 | 
|---|
 | 44 | void XyzParser::load(istream* file) {
 | 
|---|
| [dddbfe] | 45 |   atom* newAtom = NULL;
 | 
|---|
 | 46 |   molecule* newmol = NULL;
 | 
|---|
| [ab4b55] | 47 |   int numberOfAtoms;
 | 
|---|
 | 48 |   char commentBuffer[512], type[3];
 | 
|---|
| [d74077] | 49 |   double tmp;
 | 
|---|
| [ab4b55] | 50 | 
 | 
|---|
 | 51 |   // the first line tells number of atoms, the second line is always a comment
 | 
|---|
 | 52 |   *file >> numberOfAtoms >> ws;
 | 
|---|
 | 53 |   file->getline(commentBuffer, 512);
 | 
|---|
 | 54 |   comment = commentBuffer;
 | 
|---|
 | 55 | 
 | 
|---|
| [dddbfe] | 56 |   newmol = World::getInstance().createMolecule();
 | 
|---|
 | 57 |   newmol->ActiveFlag = true;
 | 
|---|
| [bb4408] | 58 |   // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include
 | 
|---|
| [dddbfe] | 59 |   World::getInstance().getMolecules()->insert(newmol);
 | 
|---|
| [ab4b55] | 60 |   for (int i = 0; i < numberOfAtoms; i++) {
 | 
|---|
| [4415da] | 61 |     newAtom = World::getInstance().createAtom();
 | 
|---|
| [d74077] | 62 |     *file >> type;
 | 
|---|
 | 63 |     for (int j=0;j<NDIM;j++) {
 | 
|---|
 | 64 |       *file >> tmp;
 | 
|---|
 | 65 |       newAtom->set(j, tmp);
 | 
|---|
 | 66 |     }
 | 
|---|
| [4415da] | 67 |     newAtom->setType(World::getInstance().getPeriode()->FindElement(type));
 | 
|---|
| [dddbfe] | 68 |     newmol->AddAtom(newAtom);
 | 
|---|
| [ab4b55] | 69 |   }
 | 
|---|
 | 70 | }
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 | /**
 | 
|---|
 | 73 |  * Saves the current state of the World into the given XYZ file.
 | 
|---|
 | 74 |  *
 | 
|---|
 | 75 |  * \param XYZ file
 | 
|---|
 | 76 |  */
 | 
|---|
 | 77 | void XyzParser::save(ostream* file) {
 | 
|---|
| [e97a44] | 78 |   DoLog(0) && (Log() << Verbose(0) << "Saving changes to xyz." << std::endl);
 | 
|---|
| [1434c9] | 79 |   if (comment == "") {
 | 
|---|
 | 80 |     time_t now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
 | 
|---|
| [86cff86] | 81 |     comment = "Created by molecuilder on ";
 | 
|---|
| [2fd80b5] | 82 |     // ctime ends in \n\0, we have to cut away the newline
 | 
|---|
 | 83 |     std::string time(ctime(&now));
 | 
|---|
| [df481f] | 84 |     size_t pos = time.find('\n');
 | 
|---|
 | 85 |     if (pos != 0)
 | 
|---|
 | 86 |       comment += time.substr(0,pos);
 | 
|---|
 | 87 |     else
 | 
|---|
 | 88 |       comment += time;
 | 
|---|
| [1434c9] | 89 |   }
 | 
|---|
| [86cff86] | 90 |   *file << World::getInstance().numAtoms() << endl << "\t" << comment << endl;
 | 
|---|
| [ab4b55] | 91 | 
 | 
|---|
| [4415da] | 92 |   vector<atom*> atoms = World::getInstance().getAllAtoms();
 | 
|---|
| [97ebf8] | 93 |   for(vector<atom*>::iterator it = atoms.begin(); it != atoms.end(); it++) {
 | 
|---|
| [b5c53d] | 94 |     *file << noshowpoint << (*it)->getType()->getSymbol() << "\t" << (*it)->at(0) << "\t" << (*it)->at(1) << "\t" << (*it)->at(2) << endl;
 | 
|---|
| [ab4b55] | 95 |   }
 | 
|---|
 | 96 | }
 | 
|---|