| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 42 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #include "Fragment.hpp"
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include <algorithm>
 | 
|---|
| 47 | #include <cmath>
 | 
|---|
| 48 | #include <iostream>
 | 
|---|
| 49 | #include <limits>
 | 
|---|
| 50 | 
 | 
|---|
| 51 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 52 | 
 | 
|---|
| 53 | Fragment::Fragment()
 | 
|---|
| 54 | {}
 | 
|---|
| 55 | 
 | 
|---|
| 56 | Fragment::Fragment(
 | 
|---|
| 57 |     const positions_t &_positions,
 | 
|---|
| 58 |     const atomicnumbers_t &_atomicnumbers,
 | 
|---|
| 59 |     const charges_t &_charges)
 | 
|---|
| 60 | {
 | 
|---|
| 61 |   ASSERT( _positions.size() == _charges.size(),
 | 
|---|
| 62 |       "Fragment::Fragment() - given charges and positions don't match in size.");
 | 
|---|
| 63 |   positions_t::const_iterator piter = _positions.begin();
 | 
|---|
| 64 |   atomicnumbers_t::const_iterator aiter = _atomicnumbers.begin();
 | 
|---|
| 65 |   charges_t::const_iterator citer = _charges.begin();
 | 
|---|
| 66 |   for (;piter != _positions.end(); ++piter, ++aiter, ++citer)
 | 
|---|
| 67 |     nuclei.push_back( createNucleus(*piter, *aiter, *citer) );
 | 
|---|
| 68 | }
 | 
|---|
| 69 | 
 | 
|---|
| 70 | 
 | 
|---|
| 71 | Fragment& Fragment::operator+=(const Fragment &other)
 | 
|---|
| 72 | {
 | 
|---|
| 73 |   for (nuclei_t::const_iterator iter = other.nuclei.begin();
 | 
|---|
| 74 |       iter != other.nuclei.end(); ++iter) {
 | 
|---|
| 75 |     if (!containsNuclei(*iter)) {
 | 
|---|
| 76 |       nuclei.push_back(*iter);
 | 
|---|
| 77 |     }
 | 
|---|
| 78 |   }
 | 
|---|
| 79 |   return *this;
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | Fragment& Fragment::operator=(const Fragment &other)
 | 
|---|
| 83 | {
 | 
|---|
| 84 |   if (this != &other) {
 | 
|---|
| 85 |     nuclei.clear();
 | 
|---|
| 86 |     nuclei = other.nuclei;
 | 
|---|
| 87 |   }
 | 
|---|
| 88 |   return *this;
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | Fragment& Fragment::operator-=(const Fragment &other)
 | 
|---|
| 92 | {
 | 
|---|
| 93 |   for (nuclei_t::const_iterator iter = other.nuclei.begin();
 | 
|---|
| 94 |       iter != other.nuclei.end(); ++iter)
 | 
|---|
| 95 |     removeNuclei(*iter);
 | 
|---|
| 96 |   return *this;
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | bool Fragment::containsNuclei(const nucleus_t &n) const
 | 
|---|
| 100 | {
 | 
|---|
| 101 |   for (nuclei_t::const_iterator iter = nuclei.begin();
 | 
|---|
| 102 |       iter != nuclei.end(); ++iter)
 | 
|---|
| 103 |     if (Fragment::isPositionEqual(iter->first, n.first))
 | 
|---|
| 104 |       return true;
 | 
|---|
| 105 |   return false;
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | void Fragment::removeNuclei(const nucleus_t &n)
 | 
|---|
| 109 | {
 | 
|---|
| 110 |   for (nuclei_t::iterator iter = nuclei.begin();
 | 
|---|
| 111 |       iter != nuclei.end(); ++iter)
 | 
|---|
| 112 |     if (Fragment::isPositionEqual(iter->first, n.first)) {
 | 
|---|
| 113 |       nuclei.erase(iter);
 | 
|---|
| 114 |       break;
 | 
|---|
| 115 |     }
 | 
|---|
| 116 | }
 | 
|---|
| 117 | 
 | 
|---|
| 118 | Fragment::positions_t Fragment::getPositions() const
 | 
|---|
| 119 | {
 | 
|---|
| 120 |   positions_t positions;
 | 
|---|
| 121 |   for (nuclei_t::const_iterator iter = nuclei.begin();
 | 
|---|
| 122 |       iter != nuclei.end(); ++iter)
 | 
|---|
| 123 |     positions.push_back(iter->first);
 | 
|---|
| 124 |   return positions;
 | 
|---|
| 125 | }
 | 
|---|
| 126 | 
 | 
|---|
| 127 | Fragment::atomicnumbers_t Fragment::getAtomicNumbers() const
 | 
|---|
| 128 | {
 | 
|---|
| 129 |   atomicnumbers_t positions;
 | 
|---|
| 130 |   for (nuclei_t::const_iterator iter = nuclei.begin();
 | 
|---|
| 131 |       iter != nuclei.end(); ++iter)
 | 
|---|
| 132 |     positions.push_back(iter->second.first);
 | 
|---|
| 133 |   return positions;
 | 
|---|
| 134 | }
 | 
|---|
| 135 | 
 | 
|---|
| 136 | Fragment::charges_t Fragment::getCharges() const
 | 
|---|
| 137 | {
 | 
|---|
| 138 |   charges_t charges;
 | 
|---|
| 139 |   for (nuclei_t::const_iterator iter = nuclei.begin();
 | 
|---|
| 140 |       iter != nuclei.end(); ++iter)
 | 
|---|
| 141 |     charges.push_back(iter->second.second);
 | 
|---|
| 142 |   return charges;
 | 
|---|
| 143 | }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | Fragment::nucleus_t Fragment::createNucleus(
 | 
|---|
| 146 |     const position_t &position,
 | 
|---|
| 147 |     const atomicNumber_t &atomicnumber,
 | 
|---|
| 148 |     const double charge)
 | 
|---|
| 149 | {
 | 
|---|
| 150 |   return nucleus_t( make_pair( position, std::make_pair(atomicnumber, charge) ) );
 | 
|---|
| 151 | }
 | 
|---|
| 152 | 
 | 
|---|
| 153 | bool Fragment::isPositionEqual(const position_t &a, const position_t &b)
 | 
|---|
| 154 | {
 | 
|---|
| 155 |   bool status = true;
 | 
|---|
| 156 |   for (size_t i=0;i<3;++i)
 | 
|---|
| 157 |     status &= fabs(a[i] - b[i]) < std::numeric_limits<double>::epsilon();
 | 
|---|
| 158 |   return status;
 | 
|---|
| 159 | }
 | 
|---|
| 160 | 
 | 
|---|
| 161 | bool Fragment::operator==(const Fragment& other) const
 | 
|---|
| 162 | {
 | 
|---|
| 163 |   bool status = true;
 | 
|---|
| 164 |   // compare sizes
 | 
|---|
| 165 |   if (nuclei.size() != other.nuclei.size()) {
 | 
|---|
| 166 |     return false;
 | 
|---|
| 167 |   } else {
 | 
|---|
| 168 |     // if equal, sort, and compare each nuclei
 | 
|---|
| 169 |     Fragment::nuclei_t thisnuclei(nuclei);
 | 
|---|
| 170 |     Fragment::nuclei_t othernuclei(other.nuclei);
 | 
|---|
| 171 |     std::sort(thisnuclei.begin(), thisnuclei.end());
 | 
|---|
| 172 |     std::sort(othernuclei.begin(), othernuclei.end());
 | 
|---|
| 173 |     Fragment::nuclei_t::const_iterator iter = thisnuclei.begin();
 | 
|---|
| 174 |     Fragment::nuclei_t::const_iterator oiter = othernuclei.begin();
 | 
|---|
| 175 |     for (; iter != thisnuclei.end(); ++iter,++oiter)
 | 
|---|
| 176 |       status &= (*iter == *oiter);
 | 
|---|
| 177 |   }
 | 
|---|
| 178 |   return status;
 | 
|---|
| 179 | }
 | 
|---|
| 180 | 
 | 
|---|
| 181 | std::ostream & operator<<(std::ostream &ost, const Fragment &f)
 | 
|---|
| 182 | {
 | 
|---|
| 183 |   size_t NucleiCounter = 1;
 | 
|---|
| 184 |   for (Fragment::nuclei_t::const_iterator iter = f.nuclei.begin();
 | 
|---|
| 185 |       iter != f.nuclei.end(); ++iter)
 | 
|---|
| 186 |     ost << "[" << NucleiCounter++ << "]:" << *iter << ", ";
 | 
|---|
| 187 |   return ost;
 | 
|---|
| 188 | }
 | 
|---|
| 189 | 
 | 
|---|
| 190 | std::ostream & operator<<(std::ostream &ost, const Fragment::nucleus_t &n)
 | 
|---|
| 191 | {
 | 
|---|
| 192 |   ost << n.first << " with Z of " << n.second.first << " charge " << n.second.second;
 | 
|---|
| 193 |   return ost;
 | 
|---|
| 194 | }
 | 
|---|
| 195 | 
 | 
|---|
| 196 | bool operator==(const Fragment::nucleus_t &a, const Fragment::nucleus_t &b)
 | 
|---|
| 197 | {
 | 
|---|
| 198 |   bool status = true;
 | 
|---|
| 199 |   // check positions
 | 
|---|
| 200 |   status &= Fragment::isPositionEqual(a.first, b.first);
 | 
|---|
| 201 |   status &= (a.second.first == b.second.first);
 | 
|---|
| 202 |   status &= fabs(a.second.second - b.second.second) < std::numeric_limits<double>::epsilon();
 | 
|---|
| 203 |   return status;
 | 
|---|
| 204 | }
 | 
|---|
| 205 | 
 | 
|---|
| 206 | template<> Fragment ZeroInstance<Fragment>()
 | 
|---|
| 207 | {
 | 
|---|
| 208 |   Fragment::positions_t positions;
 | 
|---|
| 209 |   Fragment::atomicnumbers_t atomicnumbers;
 | 
|---|
| 210 |   Fragment::charges_t charges;
 | 
|---|
| 211 |   Fragment returnvalue(positions, atomicnumbers, charges);
 | 
|---|
| 212 |   return returnvalue;
 | 
|---|
| 213 | }
 | 
|---|
| 214 | 
 | 
|---|
| 215 | // below is not needed as we have above static ZeroInstace instantiating the code
 | 
|---|
| 216 | 
 | 
|---|
| 217 | //// we need to explicitly instantiate the serialization functions
 | 
|---|
| 218 | //BOOST_CLASS_EXPORT_IMPLEMENT(Fragment)
 | 
|---|