| [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 | /* | 
|---|
| [765f16] | 24 | * FormatParser_common_common.cpp | 
|---|
| [ab4b55] | 25 | * | 
|---|
|  | 26 | *  Created on: Mar 1, 2010 | 
|---|
|  | 27 | *      Author: metzler | 
|---|
|  | 28 | */ | 
|---|
|  | 29 |  | 
|---|
| [bf3817] | 30 | // include config.h | 
|---|
|  | 31 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 32 | #include <config.h> | 
|---|
|  | 33 | #endif | 
|---|
|  | 34 |  | 
|---|
| [9eb71b3] | 35 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| [112b09] | 36 |  | 
|---|
| [9131f3] | 37 | #include <iostream> | 
|---|
| [0e894a] | 38 | #include <limits> | 
|---|
| [ab4b55] | 39 |  | 
|---|
| [0e894a] | 40 | #include "CodePatterns/Assert.hpp" | 
|---|
| [02ce36] | 41 | #include "CodePatterns/Observer/Notification.hpp" | 
|---|
| [765f16] | 42 | #include "World.hpp" | 
|---|
|  | 43 | #include "ChangeTracker.hpp" | 
|---|
|  | 44 | #include "FormatParser_common.hpp" | 
|---|
|  | 45 |  | 
|---|
| [ab4b55] | 46 | using namespace std; | 
|---|
|  | 47 |  | 
|---|
|  | 48 | /** | 
|---|
|  | 49 | * Constructor. | 
|---|
|  | 50 | */ | 
|---|
| [765f16] | 51 | FormatParser_common::FormatParser_common(FormatParser_Parameters *_parameters) : | 
|---|
| [091838] | 52 | Observer("FormatParser_common"), | 
|---|
|  | 53 | saveStream(NULL) | 
|---|
| [cd5047] | 54 | { | 
|---|
| [765f16] | 55 | parameters = _parameters; | 
|---|
| [2f40c0e] | 56 | ChangeTracker::getInstance().signOn(this); | 
|---|
| [02ce36] | 57 | World::getInstance().signOn(this, World::AtomInserted); | 
|---|
|  | 58 | World::getInstance().signOn(this, World::AtomRemoved); | 
|---|
| [ab4b55] | 59 | } | 
|---|
|  | 60 |  | 
|---|
|  | 61 | /** | 
|---|
|  | 62 | * Destructor. | 
|---|
|  | 63 | */ | 
|---|
| [765f16] | 64 | FormatParser_common::~FormatParser_common() | 
|---|
|  | 65 | { | 
|---|
| [2f40c0e] | 66 | ChangeTracker::getInstance().signOff(this); | 
|---|
| [02ce36] | 67 | World::getInstance().signOff(this, World::AtomInserted); | 
|---|
|  | 68 | World::getInstance().signOff(this, World::AtomRemoved); | 
|---|
| [765f16] | 69 | if (parameters != NULL) | 
|---|
|  | 70 | delete parameters; | 
|---|
| [c0e28c] | 71 | // clear id translation maps | 
|---|
|  | 72 | LocaltoGobalIdMap.clear(); | 
|---|
|  | 73 | GlobaltoLocalIdMap.clear(); | 
|---|
| [3b75c2] | 74 |  | 
|---|
|  | 75 | // flush stream properly (is maintained in FormatParserStorage) | 
|---|
|  | 76 | if (saveStream != NULL) { | 
|---|
|  | 77 | saveStream->flush(); | 
|---|
|  | 78 | saveStream = NULL; | 
|---|
|  | 79 | } | 
|---|
| [ab4b55] | 80 | } | 
|---|
|  | 81 |  | 
|---|
|  | 82 | /** | 
|---|
|  | 83 | * Update operation which can be invoked by the observable (which should be the | 
|---|
|  | 84 | * change tracker here). | 
|---|
|  | 85 | */ | 
|---|
| [765f16] | 86 | void FormatParser_common::update(Observable *publisher) { | 
|---|
| [091838] | 87 | if (saveStream != NULL) { // only store when a saveStream is given | 
|---|
| [fac58f] | 88 | std::vector<const atom *> atoms = const_cast<const World &>(World::getInstance()). | 
|---|
|  | 89 | getAllAtoms(); | 
|---|
| [091838] | 90 | save(saveStream, atoms); | 
|---|
| [ab4b55] | 91 | } | 
|---|
|  | 92 | } | 
|---|
|  | 93 |  | 
|---|
| [38f991] | 94 | /** | 
|---|
|  | 95 | * With this, each format parser is informed about specific changes in the World. | 
|---|
|  | 96 | */ | 
|---|
| [765f16] | 97 | void FormatParser_common::recieveNotification(Observable *publisher, Notification_ptr notification) { | 
|---|
| [38f991] | 98 | switch (notification->getChannelNo()) { | 
|---|
|  | 99 | case World::AtomInserted: | 
|---|
| [54bdaa] | 100 | AtomInserted(World::getInstance().lastChangedAtomId()); | 
|---|
| [38f991] | 101 | break; | 
|---|
|  | 102 | case World::AtomRemoved: | 
|---|
| [54bdaa] | 103 | AtomRemoved(World::getInstance().lastChangedAtomId()); | 
|---|
| [38f991] | 104 | break; | 
|---|
|  | 105 | default: | 
|---|
|  | 106 | ASSERT(0, | 
|---|
| [765f16] | 107 | "FormatParser_common::recieveNotification() - unknown notification " | 
|---|
| [38f991] | 108 | +toString(notification->getChannelNo())+" received."); | 
|---|
|  | 109 | break; | 
|---|
|  | 110 | } | 
|---|
|  | 111 | } | 
|---|
|  | 112 |  | 
|---|
| [ab4b55] | 113 | /** | 
|---|
|  | 114 | * The observable can tell when it dies. | 
|---|
|  | 115 | */ | 
|---|
| [765f16] | 116 | void FormatParser_common::subjectKilled(Observable *publisher) {} | 
|---|
| [ab4b55] | 117 |  | 
|---|
|  | 118 | /** | 
|---|
|  | 119 | * Sets the output stream for save, so the save() method can be invoked on update | 
|---|
|  | 120 | * automatically. | 
|---|
|  | 121 | * | 
|---|
|  | 122 | * \param ostream where to save the World's state | 
|---|
|  | 123 | */ | 
|---|
| [c0e28c] | 124 | void FormatParser_common::setOstream(ostream* output) | 
|---|
|  | 125 | { | 
|---|
| [ab4b55] | 126 | saveStream = output; | 
|---|
|  | 127 | } | 
|---|
| [c0e28c] | 128 |  | 
|---|
|  | 129 | /** Function to be called when beginning to parse a new file. | 
|---|
|  | 130 | * | 
|---|
|  | 131 | * Resets internal translation maps. | 
|---|
|  | 132 | * | 
|---|
|  | 133 | */ | 
|---|
|  | 134 | void FormatParser_common::resetIdAssociations() | 
|---|
|  | 135 | { | 
|---|
|  | 136 | LocaltoGobalIdMap.clear(); | 
|---|
|  | 137 | GlobaltoLocalIdMap.clear(); | 
|---|
|  | 138 | } | 
|---|
|  | 139 |  | 
|---|
|  | 140 | /** Installs an association between a local id from a parsed file and the | 
|---|
|  | 141 | * global, unique one. | 
|---|
|  | 142 | * | 
|---|
|  | 143 | * @param local local atom id | 
|---|
|  | 144 | * @param global global atom id | 
|---|
|  | 145 | */ | 
|---|
|  | 146 | void FormatParser_common::associateLocaltoGlobalId(const int local, const int global) | 
|---|
|  | 147 | { | 
|---|
|  | 148 | ASSERT(LocaltoGobalIdMap.count(local) == 0, | 
|---|
|  | 149 | "FormatParser_common::associateLocaltoGlobalId() - local id " | 
|---|
|  | 150 | +toString(local)+" is already contained."); | 
|---|
|  | 151 | ASSERT(GlobaltoLocalIdMap.count(global) == 0, | 
|---|
|  | 152 | "FormatParser_common::associateLocaltoGlobalId() - global id " | 
|---|
|  | 153 | +toString(global)+" is already contained."); | 
|---|
|  | 154 | LocaltoGobalIdMap[local] = global; | 
|---|
|  | 155 | GlobaltoLocalIdMap[global] = local; | 
|---|
|  | 156 | } | 
|---|
|  | 157 |  | 
|---|
|  | 158 | /** Getter for the global id to a given \a local one. | 
|---|
|  | 159 | * | 
|---|
|  | 160 | * @param local local atom id | 
|---|
|  | 161 | * @return global atom id, -1 if unknown | 
|---|
|  | 162 | */ | 
|---|
|  | 163 | int FormatParser_common::getGlobalId(const int local) const | 
|---|
|  | 164 | { | 
|---|
|  | 165 | IdtoIdMap::const_iterator iter = LocaltoGobalIdMap.find(local); | 
|---|
|  | 166 | if(iter == LocaltoGobalIdMap.end()) | 
|---|
|  | 167 | return -1; | 
|---|
|  | 168 | return iter->second; | 
|---|
|  | 169 | } | 
|---|
|  | 170 |  | 
|---|
|  | 171 | /** Getter for the local id to a given \a global one. | 
|---|
|  | 172 | * | 
|---|
|  | 173 | * @param global global atom id | 
|---|
|  | 174 | * @return local atom id, -1 if unknown | 
|---|
|  | 175 | */ | 
|---|
|  | 176 | int FormatParser_common::getLocalId(const int global) const | 
|---|
|  | 177 | { | 
|---|
|  | 178 | IdtoIdMap::const_iterator iter = GlobaltoLocalIdMap.find(global); | 
|---|
|  | 179 | if(iter == GlobaltoLocalIdMap.end()) | 
|---|
|  | 180 | return -1; | 
|---|
|  | 181 | return iter->second; | 
|---|
|  | 182 | } | 
|---|
| [0e894a] | 183 |  | 
|---|
|  | 184 | std::pair<size_t, size_t> FormatParser_common::getMinMaxTrajectories( | 
|---|
|  | 185 | const std::vector<const atom *> &_atoms) const | 
|---|
|  | 186 | { | 
|---|
|  | 187 | // get max and min trajectories | 
|---|
|  | 188 | size_t min_trajectories = std::numeric_limits<size_t>::max(); | 
|---|
|  | 189 | size_t max_trajectories = std::numeric_limits<size_t>::min(); | 
|---|
|  | 190 | for (std::vector<const atom *>::const_iterator iter = _atoms.begin(); | 
|---|
|  | 191 | iter != _atoms.end(); | 
|---|
|  | 192 | ++iter) { | 
|---|
|  | 193 | if (max_trajectories < (*iter)->getTrajectorySize()) | 
|---|
|  | 194 | max_trajectories = (*iter)->getTrajectorySize(); | 
|---|
|  | 195 | if (min_trajectories > (*iter)->getTrajectorySize()) | 
|---|
|  | 196 | min_trajectories = (*iter)->getTrajectorySize(); | 
|---|
|  | 197 | } | 
|---|
|  | 198 | // no atoms? Then, they all have same amount | 
|---|
|  | 199 | if (_atoms.size() == 0) | 
|---|
|  | 200 | min_trajectories = max_trajectories = 1; | 
|---|
|  | 201 | ASSERT(min_trajectories == max_trajectories, | 
|---|
|  | 202 | "FormatParser_common::getMinMaxTrajectories() - not all atoms have same number of trajectories: " | 
|---|
|  | 203 | +toString(min_trajectories)+" != "+toString(max_trajectories)+"."); | 
|---|
|  | 204 |  | 
|---|
|  | 205 | return std::make_pair(min_trajectories, max_trajectories); | 
|---|
|  | 206 | } | 
|---|