OpenMD 3.2
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 following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48#include "selection/SelectionSet.hpp"
49
50#include <algorithm>
51#include <cassert>
52#include <iterator>
53#include <string>
54
55#ifdef IS_MPI
56#include <mpi.h>
57#endif
58
59namespace OpenMD {
60
61 SelectionSet::SelectionSet(std::vector<int> nbits) {
62 bitsets_.resize(N_SELECTIONTYPES);
63 for (int i = 0; i < N_SELECTIONTYPES; i++)
64 bitsets_[i] = OpenMDBitSet(nbits[i]);
65 clearAll();
66 }
67
68 std::vector<int> SelectionSet::countBits() {
69 std::vector<int> result(N_SELECTIONTYPES, 0);
70 for (int i = 0; i < N_SELECTIONTYPES; i++)
71 result[i] = bitsets_[i].countBits();
72 return result;
73 }
74
75 void SelectionSet::flip(std::vector<int> bitIndex) {
76 for (int i = 0; i < N_SELECTIONTYPES; i++)
77 bitsets_[i].flip(bitIndex[i]);
78 }
79
80 void SelectionSet::flip(std::vector<int> fromIndex,
81 std::vector<int> toIndex) {
82 for (int i = 0; i < N_SELECTIONTYPES; i++)
83 bitsets_[i].flip(fromIndex[i], toIndex[i]);
84 }
85
87 for (int i = 0; i < N_SELECTIONTYPES; i++)
88 bitsets_[i].flip();
89 }
90
91 std::vector<bool> SelectionSet::get(std::vector<int> bitIndex) {
92 std::vector<bool> result(N_SELECTIONTYPES);
93 for (int i = 0; i < N_SELECTIONTYPES; i++)
94 result[i] = bitsets_[i].get(bitIndex[i]);
95 return result;
96 }
97
98 SelectionSet SelectionSet::get(std::vector<int> fromIndex,
99 std::vector<int> toIndex) {
100 SelectionSet result;
101
102 for (int i = 0; i < N_SELECTIONTYPES; i++)
103 result.bitsets_[i] = bitsets_[i].get(fromIndex[i], toIndex[i]);
104
105 return result;
106 }
107
108 std::vector<bool> SelectionSet::any() {
109 std::vector<bool> result(N_SELECTIONTYPES);
110 for (int i = 0; i < N_SELECTIONTYPES; i++)
111 result[i] = bitsets_[i].any();
112 return result;
113 }
114
115 std::vector<bool> SelectionSet::none() {
116 std::vector<bool> result(N_SELECTIONTYPES);
117 for (int i = 0; i < N_SELECTIONTYPES; i++)
118 result[i] = bitsets_[i].none();
119 return result;
120 }
121
122 std::vector<int> SelectionSet::firstOffBit() const {
123 std::vector<int> result(N_SELECTIONTYPES);
124 for (int i = 0; i < N_SELECTIONTYPES; i++)
125 result[i] = bitsets_[i].firstOffBit();
126 return result;
127 }
128
129 std::vector<int> SelectionSet::nextOffBit(std::vector<int> fromIndex) const {
130 std::vector<int> result(N_SELECTIONTYPES);
131 for (int i = 0; i < N_SELECTIONTYPES; i++)
132 result[i] = bitsets_[i].nextOffBit(fromIndex[i]);
133 return result;
134 }
135
136 std::vector<int> SelectionSet::firstOnBit() const {
137 std::vector<int> result(N_SELECTIONTYPES);
138 for (int i = 0; i < N_SELECTIONTYPES; i++)
139 result[i] = bitsets_[i].firstOnBit();
140 return result;
141 }
142
143 std::vector<int> SelectionSet::nextOnBit(std::vector<int> fromIndex) const {
144 std::vector<int> result(N_SELECTIONTYPES);
145 for (int i = 0; i < N_SELECTIONTYPES; i++)
146 result[i] = bitsets_[i].nextOnBit(fromIndex[i]);
147 return result;
148 }
149
150 void SelectionSet::andOperator(const SelectionSet& ss) {
151 for (int i = 0; i < N_SELECTIONTYPES; i++)
152 bitsets_[i] &= ss.bitsets_[i];
153 }
154
155 void SelectionSet::orOperator(const SelectionSet& ss) {
156 for (int i = 0; i < N_SELECTIONTYPES; i++)
157 bitsets_[i] |= ss.bitsets_[i];
158 }
159
160 void SelectionSet::xorOperator(const SelectionSet& ss) {
161 for (int i = 0; i < N_SELECTIONTYPES; i++)
162 bitsets_[i] ^= ss.bitsets_[i];
163 }
164
165 // void SelectionSet::setBits(std::vector<int> fromIndex,
166 // std::vector<int> toIndex, bool value) {
167 // for (int i = 0; i < N_SELECTIONTYPES; i++)
168 // bitsets_[i].setBits(fromIndex[i], toIndex[i], value);
169 //}
170
172 for (int i = 0; i < N_SELECTIONTYPES; i++)
173 bitsets_[i].clearAll();
174 }
175
176 void SelectionSet::setAll() {
177 for (int i = 0; i < N_SELECTIONTYPES; i++)
178 bitsets_[i].setAll();
179 }
180
181 std::vector<int> SelectionSet::size() const {
182 std::vector<int> result(N_SELECTIONTYPES);
183 for (int i = 0; i < N_SELECTIONTYPES; i++)
184 result[i] = bitsets_[i].size();
185 return result;
186 }
187
188 void SelectionSet::resize(std::vector<int> nbits) {
189 for (int i = 0; i < N_SELECTIONTYPES; i++)
190 bitsets_[i].resize(nbits[i]);
191 }
192
193 SelectionSet operator|(const SelectionSet& ss1, const SelectionSet& ss2) {
194 SelectionSet result(ss1);
195 for (int i = 0; i < N_SELECTIONTYPES; i++)
196 result.bitsets_[i] |= ss2.bitsets_[i];
197 return result;
198 }
199
200 SelectionSet operator&(const SelectionSet& ss1, const SelectionSet& ss2) {
201 SelectionSet result(ss1);
202 for (int i = 0; i < N_SELECTIONTYPES; i++)
203 result.bitsets_[i] &= ss2.bitsets_[i];
204 return result;
205 }
206
207 SelectionSet operator^(const SelectionSet& ss1, const SelectionSet& ss2) {
208 SelectionSet result(ss1);
209 for (int i = 0; i < N_SELECTIONTYPES; i++)
210 result.bitsets_[i] ^= ss2.bitsets_[i];
211 return result;
212 }
213
214 SelectionSet operator-(const SelectionSet& ss1, const SelectionSet& ss2) {
215 SelectionSet result(ss1);
216 for (int i = 0; i < N_SELECTIONTYPES; i++)
217 result.bitsets_[i] -= ss2.bitsets_[i];
218 return result;
219 }
220
221 bool operator==(const SelectionSet& ss1, const SelectionSet& ss2) {
222 for (int i = 0; i < N_SELECTIONTYPES; i++) {
223 assert(ss1.bitsets_[i].size() == ss2.bitsets_[i].size());
224 if (!(ss1.bitsets_[i] == ss2.bitsets_[i])) return false;
225 }
226
227 return true;
228 }
229
230 SelectionSet SelectionSet::parallelReduce() {
231 SelectionSet result;
232 for (int i = 0; i < N_SELECTIONTYPES; i++)
233 result.bitsets_[i] = bitsets_[i].parallelReduce();
234
235 return result;
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} // 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.