| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2016 Frederik Heber. All rights reserved. | 
|---|
| 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/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /* | 
|---|
| 24 | * TremoloPotentialFileParser.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Mar 9, 2016 | 
|---|
| 27 | *      Author: heber | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | // include config.h | 
|---|
| 32 | #ifdef HAVE_CONFIG_H | 
|---|
| 33 | #include <config.h> | 
|---|
| 34 | #endif | 
|---|
| 35 |  | 
|---|
| 36 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | #include "TremoloPotentialFileParser.hpp" | 
|---|
| 39 |  | 
|---|
| 40 | #include <string> | 
|---|
| 41 |  | 
|---|
| 42 | #include <boost/assign.hpp> | 
|---|
| 43 |  | 
|---|
| 44 | #include "CodePatterns/Log.hpp" | 
|---|
| 45 | #include "CodePatterns/Registry.hpp" | 
|---|
| 46 |  | 
|---|
| 47 | #include "Potentials/EmpiricalPotential.hpp" | 
|---|
| 48 | #include "Potentials/Exceptions.hpp" | 
|---|
| 49 | #include "Potentials/Particles/Particle.hpp" | 
|---|
| 50 | #include "Potentials/Particles/ParticleFactory.hpp" | 
|---|
| 51 | #include "Potentials/Particles/ParticleRegistry.hpp" | 
|---|
| 52 | #include "Potentials/PotentialFactory.hpp" | 
|---|
| 53 | #include "Potentials/PotentialRegistry.hpp" | 
|---|
| 54 | #include "Potentials/RegistryDeserializer.hpp" | 
|---|
| 55 | #include "Potentials/StreamFactory.hpp" | 
|---|
| 56 | #include "Potentials/TremoloPotentialTypes.hpp" | 
|---|
| 57 |  | 
|---|
| 58 | static TremoloPotentialFileParser::allowed_types_t potential_types = | 
|---|
| 59 | boost::assign::list_of< TremoloPotentialTypes::tokentype_t > | 
|---|
| 60 | ( TremoloPotentialTypes::bonds) | 
|---|
| 61 | ( TremoloPotentialTypes::angles) | 
|---|
| 62 | ( TremoloPotentialTypes::torsions) | 
|---|
| 63 | ( TremoloPotentialTypes::impropers) | 
|---|
| 64 | ( TremoloPotentialTypes::nonbonded_2body_potentials); | 
|---|
| 65 |  | 
|---|
| 66 | static TremoloPotentialFileParser::allowed_types_t particle_types = | 
|---|
| 67 | boost::assign::list_of< TremoloPotentialTypes::tokentype_t > | 
|---|
| 68 | ( TremoloPotentialTypes::particles); | 
|---|
| 69 |  | 
|---|
| 70 | template <class T> | 
|---|
| 71 | bool parseItems(std::istream &_serialized, | 
|---|
| 72 | StreamFactory<T> &_factory, | 
|---|
| 73 | Registry<T> &_registry, | 
|---|
| 74 | const std::string _name, | 
|---|
| 75 | const TremoloPotentialFileParser::allowed_types_t &_allowed_types) | 
|---|
| 76 | { | 
|---|
| 77 | std::string linestring; | 
|---|
| 78 | std::string token; | 
|---|
| 79 | while (_serialized.good()) { | 
|---|
| 80 | getline(_serialized, linestring); | 
|---|
| 81 | const std::string comment("#"); | 
|---|
| 82 | if (linestring.find(comment) != std::string::npos) { | 
|---|
| 83 | LOG(4, "DEBUG: Skipping comment line:"+linestring); | 
|---|
| 84 | continue; | 
|---|
| 85 | } | 
|---|
| 86 | const std::string whitespace(" \t"); | 
|---|
| 87 | const size_t strBegin = linestring.find_first_not_of(whitespace); | 
|---|
| 88 | const size_t curlyopenpos = linestring.find("{"); | 
|---|
| 89 | const size_t curlyclosedpos = linestring.find("}"); | 
|---|
| 90 | if (curlyopenpos != std::string::npos) { | 
|---|
| 91 | // begin of new type section | 
|---|
| 92 | token = linestring.substr(strBegin, curlyopenpos); | 
|---|
| 93 | const size_t tokenEnd = token.find_first_of(whitespace); | 
|---|
| 94 | token = token.substr(0, tokenEnd); | 
|---|
| 95 | LOG(1, "INFO: Token is " << token); | 
|---|
| 96 | const TremoloPotentialTypes::tokentype_t tokentype = | 
|---|
| 97 | TremoloPotentialTypes::getTokenTypeFromType(token); | 
|---|
| 98 | // get either particles {} or potentials {} | 
|---|
| 99 | if (_allowed_types.count(tokentype)) { | 
|---|
| 100 | try { | 
|---|
| 101 | RegistryDeserializer<T> deserialize( | 
|---|
| 102 | _serialized, | 
|---|
| 103 | _factory, | 
|---|
| 104 | _registry, | 
|---|
| 105 | _name); | 
|---|
| 106 | deserialize(); | 
|---|
| 107 | } catch (SerializerMissingValueException &e) { | 
|---|
| 108 | if (const std::string *key = boost::get_error_info<SerializerKey>(e)) | 
|---|
| 109 | ELOG(0, "Missing value when parsing information for particle "+*key+"."); | 
|---|
| 110 | else | 
|---|
| 111 | ELOG(0, "Missing value parsing information for particle with unknown key."); | 
|---|
| 112 | return false; | 
|---|
| 113 | } catch (SerializerIllegalKeyException &e) { | 
|---|
| 114 | if (const std::string *key = boost::get_error_info<SerializerKey>(e)) | 
|---|
| 115 | ELOG(0, "Illegal key parsing information for particle "+*key+"."); | 
|---|
| 116 | else | 
|---|
| 117 | ELOG(0, "Illegal key parsing information for particle with unknown key."); | 
|---|
| 118 | return false; | 
|---|
| 119 | } | 
|---|
| 120 | } | 
|---|
| 121 | } else if (curlyclosedpos != std::string::npos) { | 
|---|
| 122 | // section end encountered, just read next line | 
|---|
| 123 | } else { | 
|---|
| 124 | // skipping a section we do not parse | 
|---|
| 125 | } | 
|---|
| 126 | } | 
|---|
| 127 | return true; | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | bool TremoloPotentialFileParser::parseParticles( | 
|---|
| 131 | std::istream &_stream) | 
|---|
| 132 | { | 
|---|
| 133 | if (!parseItems<Particle>( | 
|---|
| 134 | _stream, | 
|---|
| 135 | ParticleFactory::getInstance(), | 
|---|
| 136 | ParticleRegistry::getInstance(), | 
|---|
| 137 | std::string("particle"), | 
|---|
| 138 | particle_types)) | 
|---|
| 139 | return false; | 
|---|
| 140 | return true; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 |  | 
|---|
| 144 | bool TremoloPotentialFileParser::parsePotentials(std::istream &_stream) | 
|---|
| 145 | { | 
|---|
| 146 | if (!parseItems<EmpiricalPotential>( | 
|---|
| 147 | _stream, | 
|---|
| 148 | PotentialFactory::getInstance(), | 
|---|
| 149 | PotentialRegistry::getInstance(), | 
|---|
| 150 | std::string("potential"), | 
|---|
| 151 | potential_types)) | 
|---|
| 152 | return false; | 
|---|
| 153 | return true; | 
|---|
| 154 | } | 
|---|