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