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