OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
SelectionSet.cpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45#include "selection/SelectionSet.hpp"
46
47#include <algorithm>
48#include <cassert>
49#include <iterator>
50#include <string>
51
52#ifdef IS_MPI
53#include <mpi.h>
54#endif
55
56namespace OpenMD {
57
58 SelectionSet::SelectionSet(std::vector<int> nbits) {
59 bitsets_.resize(N_SELECTIONTYPES);
60 for (int i = 0; i < N_SELECTIONTYPES; i++)
61 bitsets_[i] = OpenMDBitSet(nbits[i]);
62 clearAll();
63 }
64
65 std::vector<int> SelectionSet::countBits() {
66 std::vector<int> result(N_SELECTIONTYPES, 0);
67 for (int i = 0; i < N_SELECTIONTYPES; i++)
68 result[i] = bitsets_[i].countBits();
69 return result;
70 }
71
72 void SelectionSet::flip(std::vector<int> bitIndex) {
73 for (int i = 0; i < N_SELECTIONTYPES; i++)
74 bitsets_[i].flip(bitIndex[i]);
75 }
76
77 void SelectionSet::flip(std::vector<int> fromIndex,
78 std::vector<int> toIndex) {
79 for (int i = 0; i < N_SELECTIONTYPES; i++)
80 bitsets_[i].flip(fromIndex[i], toIndex[i]);
81 }
82
84 for (int i = 0; i < N_SELECTIONTYPES; i++)
85 bitsets_[i].flip();
86 }
87
88 std::vector<bool> SelectionSet::get(std::vector<int> bitIndex) {
89 std::vector<bool> result(N_SELECTIONTYPES);
90 for (int i = 0; i < N_SELECTIONTYPES; i++)
91 result[i] = bitsets_[i].get(bitIndex[i]);
92 return result;
93 }
94
95 SelectionSet SelectionSet::get(std::vector<int> fromIndex,
96 std::vector<int> toIndex) {
97 SelectionSet result;
98
99 for (int i = 0; i < N_SELECTIONTYPES; i++)
100 result.bitsets_[i] = bitsets_[i].get(fromIndex[i], toIndex[i]);
101
102 return result;
103 }
104
105 std::vector<bool> SelectionSet::any() {
106 std::vector<bool> result(N_SELECTIONTYPES);
107 for (int i = 0; i < N_SELECTIONTYPES; i++)
108 result[i] = bitsets_[i].any();
109 return result;
110 }
111
112 std::vector<bool> SelectionSet::none() {
113 std::vector<bool> result(N_SELECTIONTYPES);
114 for (int i = 0; i < N_SELECTIONTYPES; i++)
115 result[i] = bitsets_[i].none();
116 return result;
117 }
118
119 std::vector<int> SelectionSet::firstOffBit() const {
120 std::vector<int> result(N_SELECTIONTYPES);
121 for (int i = 0; i < N_SELECTIONTYPES; i++)
122 result[i] = bitsets_[i].firstOffBit();
123 return result;
124 }
125
126 std::vector<int> SelectionSet::nextOffBit(std::vector<int> fromIndex) const {
127 std::vector<int> result(N_SELECTIONTYPES);
128 for (int i = 0; i < N_SELECTIONTYPES; i++)
129 result[i] = bitsets_[i].nextOffBit(fromIndex[i]);
130 return result;
131 }
132
133 std::vector<int> SelectionSet::firstOnBit() const {
134 std::vector<int> result(N_SELECTIONTYPES);
135 for (int i = 0; i < N_SELECTIONTYPES; i++)
136 result[i] = bitsets_[i].firstOnBit();
137 return result;
138 }
139
140 std::vector<int> SelectionSet::nextOnBit(std::vector<int> fromIndex) const {
141 std::vector<int> result(N_SELECTIONTYPES);
142 for (int i = 0; i < N_SELECTIONTYPES; i++)
143 result[i] = bitsets_[i].nextOnBit(fromIndex[i]);
144 return result;
145 }
146
148 for (int i = 0; i < N_SELECTIONTYPES; i++)
149 bitsets_[i] &= ss.bitsets_[i];
150 }
151
153 for (int i = 0; i < N_SELECTIONTYPES; i++)
154 bitsets_[i] |= ss.bitsets_[i];
155 }
156
158 for (int i = 0; i < N_SELECTIONTYPES; i++)
159 bitsets_[i] ^= ss.bitsets_[i];
160 }
161
162 // void SelectionSet::setBits(std::vector<int> fromIndex,
163 // std::vector<int> toIndex, bool value) {
164 // for (int i = 0; i < N_SELECTIONTYPES; i++)
165 // bitsets_[i].setBits(fromIndex[i], toIndex[i], value);
166 //}
167
169 for (int i = 0; i < N_SELECTIONTYPES; i++)
170 bitsets_[i].clearAll();
171 }
172
173 void SelectionSet::setAll() {
174 for (int i = 0; i < N_SELECTIONTYPES; i++)
175 bitsets_[i].setAll();
176 }
177
178 std::vector<int> SelectionSet::size() const {
179 std::vector<int> result(N_SELECTIONTYPES);
180 for (int i = 0; i < N_SELECTIONTYPES; i++)
181 result[i] = bitsets_[i].size();
182 return result;
183 }
184
185 void SelectionSet::resize(std::vector<int> nbits) {
186 for (int i = 0; i < N_SELECTIONTYPES; i++)
187 bitsets_[i].resize(nbits[i]);
188 }
189
190 SelectionSet operator|(const SelectionSet& ss1, const SelectionSet& ss2) {
191 SelectionSet result(ss1);
192 for (int i = 0; i < N_SELECTIONTYPES; i++)
193 result.bitsets_[i] |= ss2.bitsets_[i];
194 return result;
195 }
196
197 SelectionSet operator&(const SelectionSet& ss1, const SelectionSet& ss2) {
198 SelectionSet result(ss1);
199 for (int i = 0; i < N_SELECTIONTYPES; i++)
200 result.bitsets_[i] &= ss2.bitsets_[i];
201 return result;
202 }
203
204 SelectionSet operator^(const SelectionSet& ss1, const SelectionSet& ss2) {
205 SelectionSet result(ss1);
206 for (int i = 0; i < N_SELECTIONTYPES; i++)
207 result.bitsets_[i] ^= ss2.bitsets_[i];
208 return result;
209 }
210
211 SelectionSet operator-(const SelectionSet& ss1, const SelectionSet& ss2) {
212 SelectionSet result(ss1);
213 for (int i = 0; i < N_SELECTIONTYPES; i++)
214 result.bitsets_[i] -= ss2.bitsets_[i];
215 return result;
216 }
217
218 bool operator==(const SelectionSet& ss1, const SelectionSet& ss2) {
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 SelectionSet result;
229 for (int i = 0; i < N_SELECTIONTYPES; i++)
230 result.bitsets_[i] = bitsets_[i].parallelReduce();
231
232 return result;
233 }
234
235 // std::istream& operator>> ( std::istream& is, const OpenMDBitSet& bs) {
236 //
237 // return is;
238 //}
239
240 std::ostream& operator<<(std::ostream& os, const SelectionSet& ss) {
241 for (int i = 0; i < N_SELECTIONTYPES; i++)
242 os << "SelectionSet[" << i << "] = " << ss.bitsets_[i] << std::endl;
243 return os;
244 }
245
246 // void SelectionSet::setBit(std::vector<int> bitIndex, bool value) {
247 // for (int i = 0; i < N_SELECTIONTYPES; i++)
248 // bitsets_[i].setBit(bitIndex[i], value);
249 //}
250
251} // namespace OpenMD
std::vector< int > size() const
Returns the number of bits of space actually in use by this SelectionSet to represent bit values.
std::vector< int > nextOffBit(std::vector< int > fromIndex) const
Returns the index of the first bit that is set to false that occurs on or after the specified startin...
std::vector< bool > get(std::vector< int > bitIndex)
Returns the value of the bit with the specified index.
void clearAll()
Sets all of the bits in this SelectionSet to false.
void xorOperator(const SelectionSet &bs)
Performs a logical XOR of this bit set with the bit set argument.
void flip()
Sets each bit to the complement of its current value.
std::vector< int > countBits()
Returns the number of bits set to true in this SelectionSet.
std::vector< bool > none()
Returns true if no bits are set to true.
std::vector< int > nextOnBit(std::vector< int > fromIndex) const
Returns the index of the first bit that is set to true that occurs on or after the specified starting...
void andOperator(const SelectionSet &bs)
Performs a logical AND of this target bit set with the argument bit set.
void resize(std::vector< int > nbits)
Changes the size of SelectionSet.
void orOperator(const SelectionSet &bs)
Performs a logical OR of this bit set with the bit set argument.
std::vector< bool > any()
Returns true if any bits are set to true.
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
DynamicRectMatrix< Real > operator-(const DynamicRectMatrix< Real > &m)
Negate the value of every element of this matrix.