OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
SelectionSet.hpp
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#ifndef SELECTION_SELECTIONSET_HPP
49#define SELECTION_SELECTIONSET_HPP
50
51#include <iostream>
52#include <vector>
53
54#include "utils/OpenMDBitSet.hpp"
55
56namespace OpenMD {
57
58 /**
59 * The SelectionType enum.
60 *
61 * This is used to sort different types of selections by object type
62 */
64 STUNTDOUBLE = 0, /**< StuntDoubles (Atoms & RigidBodies) */
65 BOND = 1, /**< Bonds */
66 BEND = 2, /**< Bends */
67 TORSION = 3, /**< Torsions */
68 INVERSION = 4, /**< Inversions */
69 MOLECULE = 5, /**< Molecules */
70 N_SELECTIONTYPES = 6
71 };
72
73 class SelectionSet {
74 public:
75 /** */
76 SelectionSet() { bitsets_.resize(N_SELECTIONTYPES); }
77 /** */
78 SelectionSet(std::vector<int> nbits);
79
80 /** Returns the number of bits set to true in this SelectionSet. */
81 std::vector<int> countBits();
82
83 /** Sets the bit at the specified index to to the complement of its current
84 * value. */
85 void flip(std::vector<int> bitIndex);
86
87 /** Sets each bit from the specified fromIndex(inclusive) to the specified
88 * toIndex(exclusive) to the complement of its current value. */
89 void flip(std::vector<int> fromIndex, std::vector<int> toIndex);
90
91 /** Sets each bit to the complement of its current value. */
92 void flip();
93
94 /** Returns the value of the bit with the specified index. */
95 std::vector<bool> get(std::vector<int> bitIndex);
96
97 /** Returns a new SelectionSet composed of bits from this SelectionSet from
98 * fromIndex(inclusive) to toIndex(exclusive). */
99 SelectionSet get(std::vector<int> fromIndex, std::vector<int> toIndex);
100
101 /** Returns true if any bits are set to true */
102 std::vector<bool> any();
103
104 /** Returns true if no bits are set to true */
105 std::vector<bool> none();
106
107 std::vector<int> firstOffBit() const;
108
109 /** Returns the index of the first bit that is set to false that occurs on
110 * or after the specified starting index.*/
111 std::vector<int> nextOffBit(std::vector<int> fromIndex) const;
112
113 std::vector<int> firstOnBit() const;
114
115 /** Returns the index of the first bit that is set to true that occurs on or
116 * after the specified starting index. */
117 std::vector<int> nextOnBit(std::vector<int> fromIndex) const;
118
119 /** Performs a logical AND of this target bit set with the argument bit set.
120 */
121 void andOperator(const SelectionSet& bs);
122
123 /** Performs a logical OR of this bit set with the bit set argument. */
124 void orOperator(const SelectionSet& bs);
125
126 /** Performs a logical XOR of this bit set with the bit set argument. */
127 void xorOperator(const SelectionSet& bs);
128
129 void setBitOn(std::vector<int> bitIndex);
130
131 void setBitOff(std::vector<int> bitIndex);
132
133 // void setRangeOn(std::vector<int> fromIndex, std::vector<int> toIndex) {
134 // setBits(fromIndex, toIndex, true); }
135
136 // void setRangeOff(std::vector<int> fromIndex, std::vector<int> toIndex) {
137 // setBits(fromIndex, toIndex, false); }
138
139 /** Sets all of the bits in this SelectionSet to false. */
140 void clearAll();
141
142 void setAll();
143
144 /** Returns the number of bits of space actually in use by this SelectionSet
145 * to represent bit values. */
146 std::vector<int> size() const;
147
148 /** Changes the size of SelectionSet*/
149 void resize(std::vector<int> nbits);
150
151 SelectionSet& operator&=(const SelectionSet& ss) {
152 andOperator(ss);
153 return *this;
154 }
155 SelectionSet& operator|=(const SelectionSet& ss) {
156 orOperator(ss);
157 return *this;
158 }
159 SelectionSet& operator^=(const SelectionSet& ss) {
160 xorOperator(ss);
161 return *this;
162 }
163 SelectionSet& operator-=(const SelectionSet& ss) {
164 SelectionSet tmp = *this ^ ss;
165 *this &= tmp;
166 return *this;
167 }
168
169 SelectionSet parallelReduce();
170
171 std::vector<bool> operator[](std::vector<int> bitIndex) const {
172 std::vector<bool> result(N_SELECTIONTYPES);
173 for (int i = 0; i < N_SELECTIONTYPES; i++)
174 result[i] = bitsets_[i][bitIndex[i]];
175 return result;
176 }
177
178 friend SelectionSet operator|(const SelectionSet& bs1,
179 const SelectionSet& bs2);
180 friend SelectionSet operator&(const SelectionSet& bs1,
181 const SelectionSet& bs2);
182 friend SelectionSet operator^(const SelectionSet& bs1,
183 const SelectionSet& bs2);
184 friend SelectionSet operator-(const SelectionSet& bs1,
185 const SelectionSet& bs2);
186
187 friend bool operator==(const SelectionSet& bs1, const SelectionSet& bs2);
188
189 // friend std::istream& operator>> ( std::istream&, const SelectionSet& bs);
190 friend std::ostream& operator<<(std::ostream&, const SelectionSet& bs);
191
192 std::vector<OpenMDBitSet> bitsets_;
193
194 private:
195 /** Sets the bit at the specified index to the specified value. */
196 // void setBit(std::vector<int> bitIndex, bool value);
197
198 /** Sets the bits from the specified fromIndex(inclusive) to the specified
199 * toIndex(exclusive) to the specified value. */
200 // void setBits(std::vector<int> fromIndex, std::vector<int> toIndex, bool
201 // value);
202 };
203} // namespace OpenMD
204
205#endif
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.
SelectionType
The SelectionType enum.
@ INVERSION
Inversions.
@ STUNTDOUBLE
StuntDoubles (Atoms & RigidBodies).
@ TORSION
Torsions.
@ BEND
Bends.
@ BOND
Bonds.
@ MOLECULE
Molecules.