| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [94d5ac6] | 5 | * | 
|---|
|  | 6 | * | 
|---|
|  | 7 | *   This file is part of MoleCuilder. | 
|---|
|  | 8 | * | 
|---|
|  | 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
|  | 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
|  | 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
|  | 12 | *    (at your option) any later version. | 
|---|
|  | 13 | * | 
|---|
|  | 14 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
|  | 15 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 16 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 17 | *    GNU General Public License for more details. | 
|---|
|  | 18 | * | 
|---|
|  | 19 | *    You should have received a copy of the GNU General Public License | 
|---|
|  | 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| [bcf653] | 21 | */ | 
|---|
|  | 22 |  | 
|---|
| [ab4b55] | 23 | /* | 
|---|
|  | 24 | * XyzParser.cpp | 
|---|
|  | 25 | * | 
|---|
|  | 26 | *  Created on: Mar 2, 2010 | 
|---|
|  | 27 | *      Author: metzler | 
|---|
|  | 28 | */ | 
|---|
|  | 29 |  | 
|---|
| [bf3817] | 30 | // include config.h | 
|---|
|  | 31 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 32 | #include <config.h> | 
|---|
|  | 33 | #endif | 
|---|
|  | 34 |  | 
|---|
| [ad011c] | 35 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [112b09] | 36 |  | 
|---|
| [c2808e] | 37 | #include <limits> | 
|---|
| [637358] | 38 | #include <vector> | 
|---|
|  | 39 |  | 
|---|
| [ad011c] | 40 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 41 | #include "CodePatterns/Verbose.hpp" | 
|---|
| [42127c] | 42 |  | 
|---|
| [ab4b55] | 43 | #include "XyzParser.hpp" | 
|---|
| [42127c] | 44 |  | 
|---|
| [6f0841] | 45 | #include "Atom/atom.hpp" | 
|---|
| [3bdb6d] | 46 | #include "Element/element.hpp" | 
|---|
|  | 47 | #include "Element/periodentafel.hpp" | 
|---|
| [42127c] | 48 | #include "molecule.hpp" | 
|---|
|  | 49 | #include "MoleculeListClass.hpp" | 
|---|
|  | 50 | #include "World.hpp" | 
|---|
| [ab4b55] | 51 |  | 
|---|
|  | 52 | using namespace std; | 
|---|
|  | 53 |  | 
|---|
| [765f16] | 54 | // declare specialized static variables | 
|---|
|  | 55 | const std::string FormatParserTrait<xyz>::name = "xyz"; | 
|---|
|  | 56 | const std::string FormatParserTrait<xyz>::suffix = "xyz"; | 
|---|
|  | 57 | const ParserTypes FormatParserTrait<xyz>::type = xyz; | 
|---|
|  | 58 |  | 
|---|
| [ab4b55] | 59 | /** | 
|---|
|  | 60 | * Constructor. | 
|---|
|  | 61 | */ | 
|---|
| [765f16] | 62 | FormatParser< xyz >::FormatParser() : | 
|---|
|  | 63 | FormatParser_common(NULL), | 
|---|
| [97b825] | 64 | comment("") | 
|---|
|  | 65 | {} | 
|---|
| [ab4b55] | 66 |  | 
|---|
|  | 67 | /** | 
|---|
|  | 68 | * Destructor. | 
|---|
|  | 69 | */ | 
|---|
| [765f16] | 70 | FormatParser< xyz >::~FormatParser() | 
|---|
|  | 71 | {} | 
|---|
| [ab4b55] | 72 |  | 
|---|
|  | 73 | /** | 
|---|
|  | 74 | * Loads an XYZ file into the World. | 
|---|
|  | 75 | * | 
|---|
|  | 76 | * \param XYZ file | 
|---|
|  | 77 | */ | 
|---|
| [765f16] | 78 | void FormatParser< xyz >::load(istream* file) | 
|---|
| [0180d6] | 79 | { | 
|---|
| [dddbfe] | 80 | atom* newAtom = NULL; | 
|---|
|  | 81 | molecule* newmol = NULL; | 
|---|
| [ab4b55] | 82 | int numberOfAtoms; | 
|---|
|  | 83 | char commentBuffer[512], type[3]; | 
|---|
| [0180d6] | 84 | string number; | 
|---|
| [637358] | 85 | std::vector<atom *> AddedAtoms; | 
|---|
| [ab4b55] | 86 |  | 
|---|
| [0180d6] | 87 | // create the molecule | 
|---|
| [dddbfe] | 88 | newmol = World::getInstance().createMolecule(); | 
|---|
|  | 89 | newmol->ActiveFlag = true; | 
|---|
| [bb4408] | 90 | // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include | 
|---|
| [dddbfe] | 91 | World::getInstance().getMolecules()->insert(newmol); | 
|---|
| [0180d6] | 92 |  | 
|---|
|  | 93 | // the first line tells number of atoms, | 
|---|
|  | 94 | // where we need this construct due to skipping of empty lines below | 
|---|
| [259887] | 95 | file->getline(commentBuffer, 512); | 
|---|
| [0180d6] | 96 | unsigned int step = 0; | 
|---|
| [4fdc65] | 97 | while (file->good()) { | 
|---|
| [259887] | 98 | std::stringstream numberstream(commentBuffer); | 
|---|
| [0180d6] | 99 | numberstream >> numberOfAtoms; | 
|---|
| [637358] | 100 | if (step == 0) | 
|---|
|  | 101 | AddedAtoms.resize(numberOfAtoms); | 
|---|
| [0180d6] | 102 | // the second line is always a comment | 
|---|
|  | 103 | file->getline(commentBuffer, 512); | 
|---|
|  | 104 | comment = commentBuffer; | 
|---|
| [259887] | 105 | LOG(3, "DEBUG: comment is '" << comment << "'."); | 
|---|
| [0180d6] | 106 |  | 
|---|
|  | 107 | for (int i = 0; i < numberOfAtoms; i++) { | 
|---|
|  | 108 | // parse type and position | 
|---|
|  | 109 | *file >> type; | 
|---|
|  | 110 | Vector tempVector; | 
|---|
|  | 111 | for (int j=0;j<NDIM;j++) { | 
|---|
|  | 112 | *file >> tempVector[j]; | 
|---|
|  | 113 | } | 
|---|
|  | 114 | LOG(4, "INFO: Parsed type as " << type << " and position at " | 
|---|
|  | 115 | << tempVector << " for time step " << step); | 
|---|
|  | 116 |  | 
|---|
|  | 117 | if (step == 0) { | 
|---|
|  | 118 | newAtom = World::getInstance().createAtom(); | 
|---|
|  | 119 | newAtom->setType(World::getInstance().getPeriode()->FindElement(type)); | 
|---|
|  | 120 | newmol->AddAtom(newAtom); | 
|---|
| [637358] | 121 | AddedAtoms[i] = newAtom; | 
|---|
|  | 122 | LOG(5, "INFO: Created new atom, setting " << i << " th component of AddedAtoms."); | 
|---|
| [0180d6] | 123 | } else { | 
|---|
| [637358] | 124 | newAtom = AddedAtoms[i]; | 
|---|
|  | 125 | LOG(5, "INFO: Using present atom " << *newAtom << " from " << i << " th component of AddedAtoms."); | 
|---|
| [0180d6] | 126 | ASSERT(newAtom->getType() == World::getInstance().getPeriode()->FindElement(type), | 
|---|
| [765f16] | 127 | "FormatParser< xyz >::load() - atom has different type "+newAtom->getType()->getSymbol()+" than parsed now "+type+", mixed up order?"); | 
|---|
| [0180d6] | 128 | } | 
|---|
|  | 129 | newAtom->setPositionAtStep(step, tempVector); | 
|---|
| [d74077] | 130 | } | 
|---|
| [0180d6] | 131 | getline (*file, number);  // eat away rest of last line | 
|---|
|  | 132 | // skip empty lines | 
|---|
|  | 133 | unsigned int counter = 0; | 
|---|
|  | 134 | while (file->good()) { | 
|---|
|  | 135 | getline (*file, number); | 
|---|
|  | 136 | LOG(4, "INFO: Skipped line is '" << number << "'"); | 
|---|
|  | 137 | counter++; | 
|---|
|  | 138 | if (!number.empty()) | 
|---|
|  | 139 | break; | 
|---|
|  | 140 | } | 
|---|
|  | 141 | LOG(3, "INFO: I skipped "+toString(counter)+" empty lines."); | 
|---|
|  | 142 | ++step; | 
|---|
| [4fdc65] | 143 | } | 
|---|
| [0180d6] | 144 | BOOST_FOREACH(atom *_atom, World::getInstance().getAllAtoms()) | 
|---|
|  | 145 | LOG(3, "INFO: Atom " << _atom->getName() << " " << *dynamic_cast<AtomInfo *>(_atom) << "."); | 
|---|
| [4afa46] | 146 |  | 
|---|
|  | 147 | // refresh atom::nr and atom::name | 
|---|
|  | 148 | newmol->getAtomCount(); | 
|---|
| [ab4b55] | 149 | } | 
|---|
|  | 150 |  | 
|---|
|  | 151 | /** | 
|---|
| [73916f] | 152 | * Saves the \a atoms into as a XYZ file. | 
|---|
| [ab4b55] | 153 | * | 
|---|
| [73916f] | 154 | * \param file where to save the state | 
|---|
|  | 155 | * \param atoms atoms to store | 
|---|
| [ab4b55] | 156 | */ | 
|---|
| [765f16] | 157 | void FormatParser< xyz >::save(ostream* file, const std::vector<atom *> &atoms) { | 
|---|
| [47d041] | 158 | LOG(0, "Saving changes to xyz."); | 
|---|
| [0180d6] | 159 |  | 
|---|
|  | 160 | // get max and min trajectories | 
|---|
| [c2808e] | 161 | size_t min_trajectories = std::numeric_limits<size_t>::max(); | 
|---|
|  | 162 | size_t max_trajectories = std::numeric_limits<size_t>::min(); | 
|---|
| [0180d6] | 163 | for (std::vector<atom *>::const_iterator iter = atoms.begin(); | 
|---|
|  | 164 | iter != atoms.end(); | 
|---|
|  | 165 | ++iter) { | 
|---|
|  | 166 | if (max_trajectories < (*iter)->getTrajectorySize()) | 
|---|
|  | 167 | max_trajectories = (*iter)->getTrajectorySize(); | 
|---|
| [c2808e] | 168 | if (min_trajectories > (*iter)->getTrajectorySize()) | 
|---|
| [0180d6] | 169 | min_trajectories = (*iter)->getTrajectorySize(); | 
|---|
|  | 170 | } | 
|---|
| [c2808e] | 171 | // no atoms? Then, they all have same amount | 
|---|
|  | 172 | if (atoms.size() == 0) | 
|---|
|  | 173 | min_trajectories = max_trajectories = 1; | 
|---|
|  | 174 | ASSERT(min_trajectories == max_trajectories, | 
|---|
|  | 175 | "FormatParser< xyz >::save() - not all atoms have same number of trajectories: " | 
|---|
|  | 176 | +toString(min_trajectories)+" != "+toString(max_trajectories)+"."); | 
|---|
|  | 177 | LOG(2, "INFO: There are " << max_trajectories << " steps to save."); | 
|---|
|  | 178 |  | 
|---|
|  | 179 | // always store at least one step | 
|---|
|  | 180 | for (size_t step = 0; (step < max_trajectories) || (step == 0); ++step) { | 
|---|
| [0180d6] | 181 | if (step != 0) | 
|---|
|  | 182 | *file << "\n"; | 
|---|
|  | 183 | //if (comment == "") { | 
|---|
|  | 184 | time_t now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time' | 
|---|
|  | 185 | comment = "Created by molecuilder on "; | 
|---|
|  | 186 | // ctime ends in \n\0, we have to cut away the newline | 
|---|
|  | 187 | std::string time(ctime(&now)); | 
|---|
|  | 188 | size_t pos = time.find('\n'); | 
|---|
|  | 189 | if (pos != 0) | 
|---|
|  | 190 | comment += time.substr(0,pos); | 
|---|
|  | 191 | else | 
|---|
|  | 192 | comment += time; | 
|---|
|  | 193 | comment += ", time step "+toString(step); | 
|---|
|  | 194 | //} | 
|---|
|  | 195 | *file << atoms.size() << endl << "\t" << comment << endl; | 
|---|
|  | 196 |  | 
|---|
|  | 197 | for(vector<atom*>::const_iterator it = atoms.begin(); it != atoms.end(); it++) { | 
|---|
|  | 198 | *file << noshowpoint << (*it)->getType()->getSymbol(); | 
|---|
|  | 199 | *file << "\t" << (*it)->atStep(0, step); | 
|---|
|  | 200 | *file << "\t" << (*it)->atStep(1, step); | 
|---|
|  | 201 | *file << "\t" << (*it)->atStep(2, step); | 
|---|
|  | 202 | *file << endl; | 
|---|
|  | 203 | } | 
|---|
| [ab4b55] | 204 | } | 
|---|
|  | 205 | } | 
|---|