[c74fdb] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2012 University of Bonn. All rights reserved.
|
---|
[5aaa43] | 5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
[c74fdb] | 6 | *
|
---|
| 7 | *
|
---|
| 8 | * This file is part of MoleCuilder.
|
---|
| 9 | *
|
---|
| 10 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 11 | * it under the terms of the GNU General Public License as published by
|
---|
| 12 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 13 | * (at your option) any later version.
|
---|
| 14 | *
|
---|
| 15 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | * GNU General Public License for more details.
|
---|
| 19 | *
|
---|
| 20 | * You should have received a copy of the GNU General Public License
|
---|
| 21 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | /*
|
---|
| 25 | * Fragment.cpp
|
---|
| 26 | *
|
---|
| 27 | * Created on: Aug 8, 2012
|
---|
| 28 | * Author: heber
|
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | // include config.h
|
---|
| 33 | #ifdef HAVE_CONFIG_H
|
---|
| 34 | #include <config.h>
|
---|
| 35 | #endif
|
---|
| 36 |
|
---|
[a576eb] | 37 | // include headers that implement a archive in simple text format
|
---|
| 38 | // otherwise BOOST_CLASS_EXPORT_IMPLEMENT has no effect
|
---|
| 39 | #include <boost/archive/text_oarchive.hpp>
|
---|
| 40 | #include <boost/archive/text_iarchive.hpp>
|
---|
| 41 |
|
---|
[c74fdb] | 42 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 43 |
|
---|
| 44 | #include "Fragment.hpp"
|
---|
| 45 |
|
---|
[f3bc5f] | 46 | #include <algorithm>
|
---|
| 47 | #include <cmath>
|
---|
[184943] | 48 | #include <iostream>
|
---|
[f3bc5f] | 49 | #include <limits>
|
---|
[184943] | 50 |
|
---|
[c74fdb] | 51 | #include "CodePatterns/Assert.hpp"
|
---|
| 52 |
|
---|
| 53 | Fragment::Fragment()
|
---|
| 54 | {}
|
---|
| 55 |
|
---|
[184943] | 56 | Fragment::Fragment(const positions_t &_positions, const charges_t &_charges)
|
---|
[c74fdb] | 57 | {
|
---|
| 58 | ASSERT( _positions.size() == _charges.size(),
|
---|
| 59 | "Fragment::Fragment() - given charges and positions don't match in size.");
|
---|
| 60 | positions_t::const_iterator piter = _positions.begin();
|
---|
| 61 | charges_t::const_iterator citer = _charges.begin();
|
---|
| 62 | for (;piter != _positions.end(); ++piter, ++citer)
|
---|
| 63 | nuclei.push_back( make_pair( *piter, *citer) );
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | Fragment& Fragment::operator+=(const Fragment &other)
|
---|
| 68 | {
|
---|
| 69 | for (nuclei_t::const_iterator iter = other.nuclei.begin();
|
---|
| 70 | iter != other.nuclei.end(); ++iter) {
|
---|
| 71 | if (!containsNuclei(*iter)) {
|
---|
| 72 | nuclei.push_back(*iter);
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | return *this;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | Fragment& Fragment::operator=(const Fragment &other)
|
---|
| 79 | {
|
---|
[f3bc5f] | 80 | if (this != &other) {
|
---|
| 81 | nuclei.clear();
|
---|
| 82 | nuclei = other.nuclei;
|
---|
| 83 | }
|
---|
[c74fdb] | 84 | return *this;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | Fragment& Fragment::operator-=(const Fragment &other)
|
---|
| 88 | {
|
---|
| 89 | for (nuclei_t::const_iterator iter = other.nuclei.begin();
|
---|
| 90 | iter != other.nuclei.end(); ++iter)
|
---|
| 91 | removeNuclei(*iter);
|
---|
| 92 | return *this;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | bool Fragment::containsNuclei(const nucleus_t &n) const
|
---|
| 96 | {
|
---|
| 97 | for (nuclei_t::const_iterator iter = nuclei.begin();
|
---|
[f3bc5f] | 98 | iter != nuclei.end(); ++iter)
|
---|
| 99 | if (Fragment::isPositionEqual(iter->first, n.first))
|
---|
[c74fdb] | 100 | return true;
|
---|
| 101 | return false;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | void Fragment::removeNuclei(const nucleus_t &n)
|
---|
| 105 | {
|
---|
| 106 | for (nuclei_t::iterator iter = nuclei.begin();
|
---|
[f3bc5f] | 107 | iter != nuclei.end(); ++iter)
|
---|
| 108 | if (Fragment::isPositionEqual(iter->first, n.first)) {
|
---|
[c74fdb] | 109 | nuclei.erase(iter);
|
---|
| 110 | break;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[184943] | 114 | Fragment::positions_t Fragment::getPositions() const
|
---|
| 115 | {
|
---|
| 116 | positions_t positions;
|
---|
| 117 | for (nuclei_t::const_iterator iter = nuclei.begin();
|
---|
| 118 | iter != nuclei.end(); ++iter)
|
---|
| 119 | positions.push_back(iter->first);
|
---|
| 120 | return positions;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | Fragment::charges_t Fragment::getCharges() const
|
---|
| 124 | {
|
---|
| 125 | charges_t charges;
|
---|
| 126 | for (nuclei_t::const_iterator iter = nuclei.begin();
|
---|
| 127 | iter != nuclei.end(); ++iter)
|
---|
| 128 | charges.push_back(iter->second);
|
---|
| 129 | return charges;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[f3bc5f] | 132 | Fragment::nucleus_t Fragment::createNucleus(const position_t &position, const double charge)
|
---|
| 133 | {
|
---|
| 134 | return nucleus_t( make_pair( position, charge ) );
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | bool Fragment::isPositionEqual(const position_t &a, const position_t &b)
|
---|
| 138 | {
|
---|
| 139 | bool status = true;
|
---|
| 140 | for (size_t i=0;i<3;++i)
|
---|
| 141 | status &= fabs(a[i] - b[i]) < std::numeric_limits<double>::epsilon();
|
---|
| 142 | return status;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | bool Fragment::operator==(const Fragment& other) const
|
---|
| 146 | {
|
---|
| 147 | bool status = true;
|
---|
| 148 | // compare sizes
|
---|
| 149 | if (nuclei.size() != other.nuclei.size()) {
|
---|
| 150 | return false;
|
---|
| 151 | } else {
|
---|
| 152 | // if equal, sort, and compare each nuclei
|
---|
| 153 | Fragment::nuclei_t thisnuclei(nuclei);
|
---|
| 154 | Fragment::nuclei_t othernuclei(other.nuclei);
|
---|
| 155 | std::sort(thisnuclei.begin(), thisnuclei.end());
|
---|
| 156 | std::sort(othernuclei.begin(), othernuclei.end());
|
---|
| 157 | Fragment::nuclei_t::const_iterator iter = thisnuclei.begin();
|
---|
| 158 | Fragment::nuclei_t::const_iterator oiter = othernuclei.begin();
|
---|
| 159 | for (; iter != thisnuclei.end(); ++iter,++oiter)
|
---|
| 160 | status &= (*iter == *oiter);
|
---|
| 161 | }
|
---|
| 162 | return status;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[184943] | 165 | std::ostream & operator<<(std::ostream &ost, const Fragment &f)
|
---|
| 166 | {
|
---|
| 167 | size_t NucleiCounter = 1;
|
---|
| 168 | for (Fragment::nuclei_t::const_iterator iter = f.nuclei.begin();
|
---|
| 169 | iter != f.nuclei.end(); ++iter)
|
---|
[f3bc5f] | 170 | ost << "[" << NucleiCounter++ << "]:" << *iter << ", ";
|
---|
[184943] | 171 | return ost;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[f3bc5f] | 174 | std::ostream & operator<<(std::ostream &ost, const Fragment::nucleus_t &n)
|
---|
| 175 | {
|
---|
| 176 | ost << n.first << " with charge " << n.second;
|
---|
| 177 | return ost;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | bool operator==(const Fragment::nucleus_t &a, const Fragment::nucleus_t &b)
|
---|
| 181 | {
|
---|
| 182 | bool status = true;
|
---|
| 183 | // check positions
|
---|
| 184 | status &= Fragment::isPositionEqual(a.first, b.first);
|
---|
| 185 | status &= fabs(a.second - b.second) < std::numeric_limits<double>::epsilon();
|
---|
| 186 | return status;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[c74fdb] | 189 | template<> Fragment ZeroInstance<Fragment>()
|
---|
| 190 | {
|
---|
| 191 | Fragment::positions_t positions;
|
---|
| 192 | Fragment::charges_t charges;
|
---|
| 193 | Fragment returnvalue(positions, charges);
|
---|
| 194 | return returnvalue;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[a2a2f7] | 197 | // below is not needed as we have above static ZeroInstace instantiating the code
|
---|
[a576eb] | 198 |
|
---|
[a2a2f7] | 199 | //// we need to explicitly instantiate the serialization functions
|
---|
| 200 | //BOOST_CLASS_EXPORT_IMPLEMENT(Fragment)
|
---|