| [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 | 
 | 
|---|
 | 36 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | #include "Fragment.hpp"
 | 
|---|
 | 39 | 
 | 
|---|
| [f3bc5f] | 40 | #include <algorithm>
 | 
|---|
 | 41 | #include <cmath>
 | 
|---|
| [184943] | 42 | #include <iostream>
 | 
|---|
| [f3bc5f] | 43 | #include <limits>
 | 
|---|
| [184943] | 44 | 
 | 
|---|
| [c74fdb] | 45 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 | Fragment::Fragment()
 | 
|---|
 | 48 | {}
 | 
|---|
 | 49 | 
 | 
|---|
| [184943] | 50 | Fragment::Fragment(const positions_t &_positions, const charges_t &_charges)
 | 
|---|
| [c74fdb] | 51 | {
 | 
|---|
 | 52 |   ASSERT( _positions.size() == _charges.size(),
 | 
|---|
 | 53 |       "Fragment::Fragment() - given charges and positions don't match in size.");
 | 
|---|
 | 54 |   positions_t::const_iterator piter = _positions.begin();
 | 
|---|
 | 55 |   charges_t::const_iterator citer = _charges.begin();
 | 
|---|
 | 56 |   for (;piter != _positions.end(); ++piter, ++citer)
 | 
|---|
 | 57 |     nuclei.push_back( make_pair( *piter, *citer) );
 | 
|---|
 | 58 | }
 | 
|---|
 | 59 | 
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 | Fragment& Fragment::operator+=(const Fragment &other)
 | 
|---|
 | 62 | {
 | 
|---|
 | 63 |   for (nuclei_t::const_iterator iter = other.nuclei.begin();
 | 
|---|
 | 64 |       iter != other.nuclei.end(); ++iter) {
 | 
|---|
 | 65 |     if (!containsNuclei(*iter)) {
 | 
|---|
 | 66 |       nuclei.push_back(*iter);
 | 
|---|
 | 67 |     }
 | 
|---|
 | 68 |   }
 | 
|---|
 | 69 |   return *this;
 | 
|---|
 | 70 | }
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 | Fragment& Fragment::operator=(const Fragment &other)
 | 
|---|
 | 73 | {
 | 
|---|
| [f3bc5f] | 74 |   if (this != &other) {
 | 
|---|
 | 75 |     nuclei.clear();
 | 
|---|
 | 76 |     nuclei = other.nuclei;
 | 
|---|
 | 77 |   }
 | 
|---|
| [c74fdb] | 78 |   return *this;
 | 
|---|
 | 79 | }
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 | Fragment& Fragment::operator-=(const Fragment &other)
 | 
|---|
 | 82 | {
 | 
|---|
 | 83 |   for (nuclei_t::const_iterator iter = other.nuclei.begin();
 | 
|---|
 | 84 |       iter != other.nuclei.end(); ++iter)
 | 
|---|
 | 85 |     removeNuclei(*iter);
 | 
|---|
 | 86 |   return *this;
 | 
|---|
 | 87 | }
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 | bool Fragment::containsNuclei(const nucleus_t &n) const
 | 
|---|
 | 90 | {
 | 
|---|
 | 91 |   for (nuclei_t::const_iterator iter = nuclei.begin();
 | 
|---|
| [f3bc5f] | 92 |       iter != nuclei.end(); ++iter)
 | 
|---|
 | 93 |     if (Fragment::isPositionEqual(iter->first, n.first))
 | 
|---|
| [c74fdb] | 94 |       return true;
 | 
|---|
 | 95 |   return false;
 | 
|---|
 | 96 | }
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 | void Fragment::removeNuclei(const nucleus_t &n)
 | 
|---|
 | 99 | {
 | 
|---|
 | 100 |   for (nuclei_t::iterator iter = nuclei.begin();
 | 
|---|
| [f3bc5f] | 101 |       iter != nuclei.end(); ++iter)
 | 
|---|
 | 102 |     if (Fragment::isPositionEqual(iter->first, n.first)) {
 | 
|---|
| [c74fdb] | 103 |       nuclei.erase(iter);
 | 
|---|
 | 104 |       break;
 | 
|---|
 | 105 |     }
 | 
|---|
 | 106 | }
 | 
|---|
 | 107 | 
 | 
|---|
| [184943] | 108 | Fragment::positions_t Fragment::getPositions() const
 | 
|---|
 | 109 | {
 | 
|---|
 | 110 |   positions_t positions;
 | 
|---|
 | 111 |   for (nuclei_t::const_iterator iter = nuclei.begin();
 | 
|---|
 | 112 |       iter != nuclei.end(); ++iter)
 | 
|---|
 | 113 |     positions.push_back(iter->first);
 | 
|---|
 | 114 |   return positions;
 | 
|---|
 | 115 | }
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 | Fragment::charges_t Fragment::getCharges() const
 | 
|---|
 | 118 | {
 | 
|---|
 | 119 |   charges_t charges;
 | 
|---|
 | 120 |   for (nuclei_t::const_iterator iter = nuclei.begin();
 | 
|---|
 | 121 |       iter != nuclei.end(); ++iter)
 | 
|---|
 | 122 |     charges.push_back(iter->second);
 | 
|---|
 | 123 |   return charges;
 | 
|---|
 | 124 | }
 | 
|---|
 | 125 | 
 | 
|---|
| [f3bc5f] | 126 | Fragment::nucleus_t Fragment::createNucleus(const position_t &position, const double charge)
 | 
|---|
 | 127 | {
 | 
|---|
 | 128 |   return nucleus_t( make_pair( position, charge ) );
 | 
|---|
 | 129 | }
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 | bool Fragment::isPositionEqual(const position_t &a, const position_t &b)
 | 
|---|
 | 132 | {
 | 
|---|
 | 133 |   bool status = true;
 | 
|---|
 | 134 |   for (size_t i=0;i<3;++i)
 | 
|---|
 | 135 |     status &= fabs(a[i] - b[i]) < std::numeric_limits<double>::epsilon();
 | 
|---|
 | 136 |   return status;
 | 
|---|
 | 137 | }
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 | bool Fragment::operator==(const Fragment& other) const
 | 
|---|
 | 140 | {
 | 
|---|
 | 141 |   bool status = true;
 | 
|---|
 | 142 |   // compare sizes
 | 
|---|
 | 143 |   if (nuclei.size() != other.nuclei.size()) {
 | 
|---|
 | 144 |     return false;
 | 
|---|
 | 145 |   } else {
 | 
|---|
 | 146 |     // if equal, sort, and compare each nuclei
 | 
|---|
 | 147 |     Fragment::nuclei_t thisnuclei(nuclei);
 | 
|---|
 | 148 |     Fragment::nuclei_t othernuclei(other.nuclei);
 | 
|---|
 | 149 |     std::sort(thisnuclei.begin(), thisnuclei.end());
 | 
|---|
 | 150 |     std::sort(othernuclei.begin(), othernuclei.end());
 | 
|---|
 | 151 |     Fragment::nuclei_t::const_iterator iter = thisnuclei.begin();
 | 
|---|
 | 152 |     Fragment::nuclei_t::const_iterator oiter = othernuclei.begin();
 | 
|---|
 | 153 |     for (; iter != thisnuclei.end(); ++iter,++oiter)
 | 
|---|
 | 154 |       status &= (*iter == *oiter);
 | 
|---|
 | 155 |   }
 | 
|---|
 | 156 |   return status;
 | 
|---|
 | 157 | }
 | 
|---|
 | 158 | 
 | 
|---|
| [184943] | 159 | std::ostream & operator<<(std::ostream &ost, const Fragment &f)
 | 
|---|
 | 160 | {
 | 
|---|
 | 161 |   size_t NucleiCounter = 1;
 | 
|---|
 | 162 |   for (Fragment::nuclei_t::const_iterator iter = f.nuclei.begin();
 | 
|---|
 | 163 |       iter != f.nuclei.end(); ++iter)
 | 
|---|
| [f3bc5f] | 164 |     ost << "[" << NucleiCounter++ << "]:" << *iter << ", ";
 | 
|---|
| [184943] | 165 |   return ost;
 | 
|---|
 | 166 | }
 | 
|---|
 | 167 | 
 | 
|---|
| [f3bc5f] | 168 | std::ostream & operator<<(std::ostream &ost, const Fragment::nucleus_t &n)
 | 
|---|
 | 169 | {
 | 
|---|
 | 170 |   ost << n.first << " with charge " << n.second;
 | 
|---|
 | 171 |   return ost;
 | 
|---|
 | 172 | }
 | 
|---|
 | 173 | 
 | 
|---|
 | 174 | bool operator==(const Fragment::nucleus_t &a, const Fragment::nucleus_t &b)
 | 
|---|
 | 175 | {
 | 
|---|
 | 176 |   bool status = true;
 | 
|---|
 | 177 |   // check positions
 | 
|---|
 | 178 |   status &= Fragment::isPositionEqual(a.first, b.first);
 | 
|---|
 | 179 |   status &= fabs(a.second - b.second) < std::numeric_limits<double>::epsilon();
 | 
|---|
 | 180 |   return status;
 | 
|---|
 | 181 | }
 | 
|---|
 | 182 | 
 | 
|---|
| [c74fdb] | 183 | template<> Fragment ZeroInstance<Fragment>()
 | 
|---|
 | 184 | {
 | 
|---|
 | 185 |   Fragment::positions_t positions;
 | 
|---|
 | 186 |   Fragment::charges_t charges;
 | 
|---|
 | 187 |   Fragment returnvalue(positions, charges);
 | 
|---|
 | 188 |   return returnvalue;
 | 
|---|
 | 189 | }
 | 
|---|
 | 190 | 
 | 
|---|