| [c74fdb] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2012 University of Bonn. 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 | * Fragment.cpp | 
|---|
|  | 25 | * | 
|---|
|  | 26 | *  Created on: Aug 8, 2012 | 
|---|
|  | 27 | *      Author: heber | 
|---|
|  | 28 | */ | 
|---|
|  | 29 |  | 
|---|
|  | 30 |  | 
|---|
|  | 31 | // include config.h | 
|---|
|  | 32 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 33 | #include <config.h> | 
|---|
|  | 34 | #endif | 
|---|
|  | 35 |  | 
|---|
| [a576eb] | 36 | // include headers that implement a archive in simple text format | 
|---|
|  | 37 | // otherwise BOOST_CLASS_EXPORT_IMPLEMENT has no effect | 
|---|
|  | 38 | #include <boost/archive/text_oarchive.hpp> | 
|---|
|  | 39 | #include <boost/archive/text_iarchive.hpp> | 
|---|
|  | 40 |  | 
|---|
| [c74fdb] | 41 | #include "CodePatterns/MemDebug.hpp" | 
|---|
|  | 42 |  | 
|---|
|  | 43 | #include "Fragment.hpp" | 
|---|
|  | 44 |  | 
|---|
| [f3bc5f] | 45 | #include <algorithm> | 
|---|
|  | 46 | #include <cmath> | 
|---|
| [184943] | 47 | #include <iostream> | 
|---|
| [f3bc5f] | 48 | #include <limits> | 
|---|
| [184943] | 49 |  | 
|---|
| [c74fdb] | 50 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 51 |  | 
|---|
|  | 52 | Fragment::Fragment() | 
|---|
|  | 53 | {} | 
|---|
|  | 54 |  | 
|---|
| [184943] | 55 | Fragment::Fragment(const positions_t &_positions, const charges_t &_charges) | 
|---|
| [c74fdb] | 56 | { | 
|---|
|  | 57 | ASSERT( _positions.size() == _charges.size(), | 
|---|
|  | 58 | "Fragment::Fragment() - given charges and positions don't match in size."); | 
|---|
|  | 59 | positions_t::const_iterator piter = _positions.begin(); | 
|---|
|  | 60 | charges_t::const_iterator citer = _charges.begin(); | 
|---|
|  | 61 | for (;piter != _positions.end(); ++piter, ++citer) | 
|---|
|  | 62 | nuclei.push_back( make_pair( *piter, *citer) ); | 
|---|
|  | 63 | } | 
|---|
|  | 64 |  | 
|---|
|  | 65 |  | 
|---|
|  | 66 | Fragment& Fragment::operator+=(const Fragment &other) | 
|---|
|  | 67 | { | 
|---|
|  | 68 | for (nuclei_t::const_iterator iter = other.nuclei.begin(); | 
|---|
|  | 69 | iter != other.nuclei.end(); ++iter) { | 
|---|
|  | 70 | if (!containsNuclei(*iter)) { | 
|---|
|  | 71 | nuclei.push_back(*iter); | 
|---|
|  | 72 | } | 
|---|
|  | 73 | } | 
|---|
|  | 74 | return *this; | 
|---|
|  | 75 | } | 
|---|
|  | 76 |  | 
|---|
|  | 77 | Fragment& Fragment::operator=(const Fragment &other) | 
|---|
|  | 78 | { | 
|---|
| [f3bc5f] | 79 | if (this != &other) { | 
|---|
|  | 80 | nuclei.clear(); | 
|---|
|  | 81 | nuclei = other.nuclei; | 
|---|
|  | 82 | } | 
|---|
| [c74fdb] | 83 | return *this; | 
|---|
|  | 84 | } | 
|---|
|  | 85 |  | 
|---|
|  | 86 | Fragment& Fragment::operator-=(const Fragment &other) | 
|---|
|  | 87 | { | 
|---|
|  | 88 | for (nuclei_t::const_iterator iter = other.nuclei.begin(); | 
|---|
|  | 89 | iter != other.nuclei.end(); ++iter) | 
|---|
|  | 90 | removeNuclei(*iter); | 
|---|
|  | 91 | return *this; | 
|---|
|  | 92 | } | 
|---|
|  | 93 |  | 
|---|
|  | 94 | bool Fragment::containsNuclei(const nucleus_t &n) const | 
|---|
|  | 95 | { | 
|---|
|  | 96 | for (nuclei_t::const_iterator iter = nuclei.begin(); | 
|---|
| [f3bc5f] | 97 | iter != nuclei.end(); ++iter) | 
|---|
|  | 98 | if (Fragment::isPositionEqual(iter->first, n.first)) | 
|---|
| [c74fdb] | 99 | return true; | 
|---|
|  | 100 | return false; | 
|---|
|  | 101 | } | 
|---|
|  | 102 |  | 
|---|
|  | 103 | void Fragment::removeNuclei(const nucleus_t &n) | 
|---|
|  | 104 | { | 
|---|
|  | 105 | for (nuclei_t::iterator iter = nuclei.begin(); | 
|---|
| [f3bc5f] | 106 | iter != nuclei.end(); ++iter) | 
|---|
|  | 107 | if (Fragment::isPositionEqual(iter->first, n.first)) { | 
|---|
| [c74fdb] | 108 | nuclei.erase(iter); | 
|---|
|  | 109 | break; | 
|---|
|  | 110 | } | 
|---|
|  | 111 | } | 
|---|
|  | 112 |  | 
|---|
| [184943] | 113 | Fragment::positions_t Fragment::getPositions() const | 
|---|
|  | 114 | { | 
|---|
|  | 115 | positions_t positions; | 
|---|
|  | 116 | for (nuclei_t::const_iterator iter = nuclei.begin(); | 
|---|
|  | 117 | iter != nuclei.end(); ++iter) | 
|---|
|  | 118 | positions.push_back(iter->first); | 
|---|
|  | 119 | return positions; | 
|---|
|  | 120 | } | 
|---|
|  | 121 |  | 
|---|
|  | 122 | Fragment::charges_t Fragment::getCharges() const | 
|---|
|  | 123 | { | 
|---|
|  | 124 | charges_t charges; | 
|---|
|  | 125 | for (nuclei_t::const_iterator iter = nuclei.begin(); | 
|---|
|  | 126 | iter != nuclei.end(); ++iter) | 
|---|
|  | 127 | charges.push_back(iter->second); | 
|---|
|  | 128 | return charges; | 
|---|
|  | 129 | } | 
|---|
|  | 130 |  | 
|---|
| [f3bc5f] | 131 | Fragment::nucleus_t Fragment::createNucleus(const position_t &position, const double charge) | 
|---|
|  | 132 | { | 
|---|
|  | 133 | return nucleus_t( make_pair( position, charge ) ); | 
|---|
|  | 134 | } | 
|---|
|  | 135 |  | 
|---|
|  | 136 | bool Fragment::isPositionEqual(const position_t &a, const position_t &b) | 
|---|
|  | 137 | { | 
|---|
|  | 138 | bool status = true; | 
|---|
|  | 139 | for (size_t i=0;i<3;++i) | 
|---|
|  | 140 | status &= fabs(a[i] - b[i]) < std::numeric_limits<double>::epsilon(); | 
|---|
|  | 141 | return status; | 
|---|
|  | 142 | } | 
|---|
|  | 143 |  | 
|---|
|  | 144 | bool Fragment::operator==(const Fragment& other) const | 
|---|
|  | 145 | { | 
|---|
|  | 146 | bool status = true; | 
|---|
|  | 147 | // compare sizes | 
|---|
|  | 148 | if (nuclei.size() != other.nuclei.size()) { | 
|---|
|  | 149 | return false; | 
|---|
|  | 150 | } else { | 
|---|
|  | 151 | // if equal, sort, and compare each nuclei | 
|---|
|  | 152 | Fragment::nuclei_t thisnuclei(nuclei); | 
|---|
|  | 153 | Fragment::nuclei_t othernuclei(other.nuclei); | 
|---|
|  | 154 | std::sort(thisnuclei.begin(), thisnuclei.end()); | 
|---|
|  | 155 | std::sort(othernuclei.begin(), othernuclei.end()); | 
|---|
|  | 156 | Fragment::nuclei_t::const_iterator iter = thisnuclei.begin(); | 
|---|
|  | 157 | Fragment::nuclei_t::const_iterator oiter = othernuclei.begin(); | 
|---|
|  | 158 | for (; iter != thisnuclei.end(); ++iter,++oiter) | 
|---|
|  | 159 | status &= (*iter == *oiter); | 
|---|
|  | 160 | } | 
|---|
|  | 161 | return status; | 
|---|
|  | 162 | } | 
|---|
|  | 163 |  | 
|---|
| [184943] | 164 | std::ostream & operator<<(std::ostream &ost, const Fragment &f) | 
|---|
|  | 165 | { | 
|---|
|  | 166 | size_t NucleiCounter = 1; | 
|---|
|  | 167 | for (Fragment::nuclei_t::const_iterator iter = f.nuclei.begin(); | 
|---|
|  | 168 | iter != f.nuclei.end(); ++iter) | 
|---|
| [f3bc5f] | 169 | ost << "[" << NucleiCounter++ << "]:" << *iter << ", "; | 
|---|
| [184943] | 170 | return ost; | 
|---|
|  | 171 | } | 
|---|
|  | 172 |  | 
|---|
| [f3bc5f] | 173 | std::ostream & operator<<(std::ostream &ost, const Fragment::nucleus_t &n) | 
|---|
|  | 174 | { | 
|---|
|  | 175 | ost << n.first << " with charge " << n.second; | 
|---|
|  | 176 | return ost; | 
|---|
|  | 177 | } | 
|---|
|  | 178 |  | 
|---|
|  | 179 | bool operator==(const Fragment::nucleus_t &a, const Fragment::nucleus_t &b) | 
|---|
|  | 180 | { | 
|---|
|  | 181 | bool status = true; | 
|---|
|  | 182 | // check positions | 
|---|
|  | 183 | status &= Fragment::isPositionEqual(a.first, b.first); | 
|---|
|  | 184 | status &= fabs(a.second - b.second) < std::numeric_limits<double>::epsilon(); | 
|---|
|  | 185 | return status; | 
|---|
|  | 186 | } | 
|---|
|  | 187 |  | 
|---|
| [c74fdb] | 188 | template<> Fragment ZeroInstance<Fragment>() | 
|---|
|  | 189 | { | 
|---|
|  | 190 | Fragment::positions_t positions; | 
|---|
|  | 191 | Fragment::charges_t charges; | 
|---|
|  | 192 | Fragment returnvalue(positions, charges); | 
|---|
|  | 193 | return returnvalue; | 
|---|
|  | 194 | } | 
|---|
|  | 195 |  | 
|---|
| [a576eb] | 196 |  | 
|---|
|  | 197 | // we need to explicitly instantiate the serialization functions | 
|---|
|  | 198 | BOOST_CLASS_EXPORT_IMPLEMENT(Fragment) | 
|---|