| 1 | /* | 
| 2 | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | 
| 3 | * | 
| 4 | * The University of Notre Dame grants you ("Licensee") a | 
| 5 | * non-exclusive, royalty free, license to use, modify and | 
| 6 | * redistribute this software in source and binary code form, provided | 
| 7 | * that the following conditions are met: | 
| 8 | * | 
| 9 | * 1. Redistributions of source code must retain the above copyright | 
| 10 | *    notice, this list of conditions and the following disclaimer. | 
| 11 | * | 
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 | *    notice, this list of conditions and the following disclaimer in the | 
| 14 | *    documentation and/or other materials provided with the | 
| 15 | *    distribution. | 
| 16 | * | 
| 17 | * This software is provided "AS IS," without a warranty of any | 
| 18 | * kind. All express or implied conditions, representations and | 
| 19 | * warranties, including any implied warranty of merchantability, | 
| 20 | * fitness for a particular purpose or non-infringement, are hereby | 
| 21 | * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 | * be liable for any damages suffered by licensee as a result of | 
| 23 | * using, modifying or distributing the software or its | 
| 24 | * derivatives. In no event will the University of Notre Dame or its | 
| 25 | * licensors be liable for any lost revenue, profit or data, or for | 
| 26 | * direct, indirect, special, consequential, incidental or punitive | 
| 27 | * damages, however caused and regardless of the theory of liability, | 
| 28 | * arising out of the use of or inability to use software, even if the | 
| 29 | * University of Notre Dame has been advised of the possibility of | 
| 30 | * such damages. | 
| 31 | * | 
| 32 | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 | * research, please cite the appropriate papers when you publish your | 
| 34 | * work.  Good starting points are: | 
| 35 | * | 
| 36 | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 37 | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 38 | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). | 
| 39 | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 | */ | 
| 42 |  | 
| 43 | #ifdef IS_MPI | 
| 44 | #include <mpi.h> | 
| 45 | #endif | 
| 46 |  | 
| 47 | #include <algorithm> | 
| 48 | #include <cassert> | 
| 49 | #include <string> | 
| 50 | #include <iterator> | 
| 51 |  | 
| 52 | #include "selection/SelectionSet.hpp" | 
| 53 | #include "utils/Algorithm.hpp" | 
| 54 |  | 
| 55 | namespace OpenMD { | 
| 56 |  | 
| 57 | SelectionSet::SelectionSet(std::vector<int> nbits) { | 
| 58 | bitsets_.resize(N_SELECTIONTYPES); | 
| 59 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 60 | bitsets_[i] = OpenMDBitSet(nbits[i]); | 
| 61 | clearAll(); | 
| 62 | } | 
| 63 |  | 
| 64 | std::vector<int> SelectionSet::countBits() { | 
| 65 | std::vector<int> result(N_SELECTIONTYPES, 0); | 
| 66 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 67 | result[i] = bitsets_[i].countBits(); | 
| 68 | return result; | 
| 69 | } | 
| 70 |  | 
| 71 | void SelectionSet::flip(std::vector<int> bitIndex) { | 
| 72 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 73 | bitsets_[i].flip(bitIndex[i]); | 
| 74 | } | 
| 75 |  | 
| 76 | void SelectionSet::flip(std::vector<int> fromIndex, | 
| 77 | std::vector<int> toIndex) { | 
| 78 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 79 | bitsets_[i].flip(fromIndex[i], toIndex[i]); | 
| 80 | } | 
| 81 |  | 
| 82 | void SelectionSet::flip() { | 
| 83 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 84 | bitsets_[i].flip(); | 
| 85 | } | 
| 86 |  | 
| 87 | std::vector<bool> SelectionSet::get(std::vector<int> bitIndex) { | 
| 88 | std::vector<bool> result(N_SELECTIONTYPES); | 
| 89 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 90 | result[i] = bitsets_[i].get(bitIndex[i]); | 
| 91 | return result; | 
| 92 | } | 
| 93 |  | 
| 94 | SelectionSet SelectionSet::get(std::vector<int> fromIndex, | 
| 95 | std::vector<int> toIndex) { | 
| 96 | SelectionSet result; | 
| 97 |  | 
| 98 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 99 | result.bitsets_[i] = bitsets_[i].get(fromIndex[i], toIndex[i]); | 
| 100 |  | 
| 101 | return result; | 
| 102 | } | 
| 103 |  | 
| 104 | std::vector<bool> SelectionSet::any() { | 
| 105 | std::vector<bool> result(N_SELECTIONTYPES); | 
| 106 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 107 | result[i] = bitsets_[i].any(); | 
| 108 | return result; | 
| 109 | } | 
| 110 |  | 
| 111 | std::vector<bool> SelectionSet::none() { | 
| 112 | std::vector<bool> result(N_SELECTIONTYPES); | 
| 113 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 114 | result[i] = bitsets_[i].none(); | 
| 115 | return result; | 
| 116 | } | 
| 117 |  | 
| 118 | std::vector<int> SelectionSet::firstOffBit() const { | 
| 119 | std::vector<int> result(N_SELECTIONTYPES); | 
| 120 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 121 | result[i] = bitsets_[i].firstOffBit(); | 
| 122 | return result; | 
| 123 | } | 
| 124 |  | 
| 125 | std::vector<int> SelectionSet::nextOffBit(std::vector<int> fromIndex) const { | 
| 126 | std::vector<int> result(N_SELECTIONTYPES); | 
| 127 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 128 | result[i] = bitsets_[i].nextOffBit(fromIndex[i]); | 
| 129 | return result; | 
| 130 | } | 
| 131 |  | 
| 132 | std::vector<int> SelectionSet::firstOnBit() const { | 
| 133 | std::vector<int> result(N_SELECTIONTYPES); | 
| 134 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 135 | result[i] = bitsets_[i].firstOnBit(); | 
| 136 | return result; | 
| 137 | } | 
| 138 |  | 
| 139 | std::vector<int> SelectionSet::nextOnBit(std::vector<int> fromIndex) const { | 
| 140 | std::vector<int> result(N_SELECTIONTYPES); | 
| 141 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 142 | result[i] = bitsets_[i].nextOnBit(fromIndex[i]); | 
| 143 | return result; | 
| 144 | } | 
| 145 |  | 
| 146 | void SelectionSet::andOperator (const SelectionSet& ss) { | 
| 147 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 148 | bitsets_[i] &= ss.bitsets_[i]; | 
| 149 | } | 
| 150 |  | 
| 151 | void SelectionSet::orOperator (const SelectionSet& ss) { | 
| 152 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 153 | bitsets_[i] |= ss.bitsets_[i]; | 
| 154 | } | 
| 155 |  | 
| 156 | void SelectionSet::xorOperator (const SelectionSet& ss) { | 
| 157 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 158 | bitsets_[i] ^= ss.bitsets_[i]; | 
| 159 | } | 
| 160 |  | 
| 161 | //void SelectionSet::setBits(std::vector<int> fromIndex, | 
| 162 | //                           std::vector<int> toIndex, bool value) { | 
| 163 | //  for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 164 | //    bitsets_[i].setBits(fromIndex[i], toIndex[i], value); | 
| 165 | //} | 
| 166 |  | 
| 167 | void SelectionSet::clearAll() { | 
| 168 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 169 | bitsets_[i].clearAll(); | 
| 170 | } | 
| 171 |  | 
| 172 | void SelectionSet::setAll() { | 
| 173 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 174 | bitsets_[i].setAll(); | 
| 175 | } | 
| 176 |  | 
| 177 | std::vector<int> SelectionSet::size() const { | 
| 178 | std::vector<int> result(N_SELECTIONTYPES); | 
| 179 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 180 | result[i] = bitsets_[i].size(); | 
| 181 | return result; | 
| 182 | } | 
| 183 |  | 
| 184 | void SelectionSet::resize(std::vector<int> nbits) { | 
| 185 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 186 | bitsets_[i].resize(nbits[i]); | 
| 187 | } | 
| 188 |  | 
| 189 | SelectionSet operator| (const SelectionSet& ss1, const SelectionSet& ss2) { | 
| 190 | SelectionSet result(ss1); | 
| 191 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 192 | result.bitsets_[i] |= ss2.bitsets_[i]; | 
| 193 | return result; | 
| 194 | } | 
| 195 |  | 
| 196 | SelectionSet operator& (const SelectionSet& ss1, const SelectionSet& ss2) { | 
| 197 | SelectionSet result(ss1); | 
| 198 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 199 | result.bitsets_[i] &= ss2.bitsets_[i]; | 
| 200 | return result; | 
| 201 | } | 
| 202 |  | 
| 203 | SelectionSet operator^ (const SelectionSet& ss1, const SelectionSet& ss2) { | 
| 204 | SelectionSet result(ss1); | 
| 205 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 206 | result.bitsets_[i] ^= ss2.bitsets_[i]; | 
| 207 | return result; | 
| 208 | } | 
| 209 |  | 
| 210 | SelectionSet operator- (const SelectionSet& ss1, const SelectionSet& ss2) { | 
| 211 | SelectionSet result(ss1); | 
| 212 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 213 | result.bitsets_[i] -= ss2.bitsets_[i]; | 
| 214 | return result; | 
| 215 | } | 
| 216 |  | 
| 217 | bool operator== (const SelectionSet & ss1, const SelectionSet &ss2) { | 
| 218 |  | 
| 219 | for (int i = 0; i < N_SELECTIONTYPES; i++) { | 
| 220 | assert(ss1.bitsets_[i].size() == ss2.bitsets_[i].size()); | 
| 221 | if (!(ss1.bitsets_[i] == ss2.bitsets_[i])) return false; | 
| 222 | } | 
| 223 |  | 
| 224 | return true; | 
| 225 | } | 
| 226 |  | 
| 227 | SelectionSet SelectionSet::parallelReduce() { | 
| 228 |  | 
| 229 | SelectionSet result; | 
| 230 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 231 | result.bitsets_[i] = bitsets_[i].parallelReduce(); | 
| 232 |  | 
| 233 | return result; | 
| 234 | } | 
| 235 |  | 
| 236 |  | 
| 237 |  | 
| 238 | //std::istream& operator>> ( std::istream& is, const OpenMDBitSet& bs) { | 
| 239 | // | 
| 240 | //    return is; | 
| 241 | //} | 
| 242 |  | 
| 243 | std::ostream& operator<< ( std::ostream& os, const SelectionSet& ss) { | 
| 244 | for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 245 | os << "SelectionSet[" << i << "] = " << ss.bitsets_[i] << std::endl; | 
| 246 | return os; | 
| 247 | } | 
| 248 |  | 
| 249 | //void SelectionSet::setBit(std::vector<int> bitIndex, bool value) { | 
| 250 | //  for (int i = 0; i < N_SELECTIONTYPES; i++) | 
| 251 | //    bitsets_[i].setBit(bitIndex[i], value); | 
| 252 | //} | 
| 253 |  | 
| 254 | } |