ring.cpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------------
2 // d-SEAMS - Deferred Structural Elucidation Analysis for Molecular Simulations
3 //
4 // Copyright (c) 2018--present d-SEAMS core team
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the MIT License as published by
8 // the Open Source Initiative.
9 //
10 // A copy of the MIT License is included in the LICENSE file of this repository.
11 // You should have received a copy of the MIT License along with this program.
12 // If not, see <https://opensource.org/licenses/MIT>.
13 //-----------------------------------------------------------------------------------
14 
15 #include <ring.hpp>
16 
23  //
25 
26  rings.swap(tempEmpty);
27 
28  return 0;
29 }
30 
41  std::vector<int> *atomTypes,
42  std::vector<int> nRings) {
43  // Every value in listPrism corresponds to an index in rings.
44  // Every ring contains atom indices, corresponding to the indices (not atom
45  // IDs) in rings
46  int iring; // Index of current ring
47  int iatom; // Index of current atom
48  int ringSize; // Ring size of the current ring
49  int prevRingSize; // Ring size previously assigned to a point
50 
51  // Dummy value corresponds to a value of 1.
52  // If an atom is shared by more than one ring type, it is assigned the
53  // value 2.
54 
55  // Loop through every ring in rings
56  for (int iring = 0; iring < rings.size(); iring++) {
57  ringSize = rings[iring].size();
58  // Loop through every element in iring
59  for (int j = 0; j < ringSize; j++) {
60  iatom = rings[iring][j]; // Atom index
61  // Update the atom type
62  if ((*atomTypes)[iatom] == 1) {
63  (*atomTypes)[iatom] = ringSize;
64  } // The atom is unclassified
65  else {
66  // Only update the ring type if the number is higher
67  prevRingSize = (*atomTypes)[iatom]; // Previously assigned ring size
68  if (ringSize > prevRingSize) {
69  (*atomTypes)[iatom] = ringSize;
70  } // end of assigning the new ring size
71  } // only update if the number is higher
72  } // end of loop through every atom in iring
73  } // end of loop through every ring
74 
75  return 0;
76 } // end of function
77 
86  std::vector<int> ring2) {
87  //
88  std::vector<int> common;
89  int iatom; // Index to search for
90 
91  for (int i = 0; i < ring1.size(); i++) {
92  iatom = ring1[i];
93  // Search for iatom in ring2
94 
95  auto it = std::find(ring2.begin(), ring2.end(), iatom);
96 
97  if (it != ring2.end()) {
98  common.push_back(iatom);
99  } // iatom was found!
100  } // end of loop through every element of ring1
101 
102  return common;
103 }
104 
115  std::vector<int> ring2,
116  std::vector<int> ring3) {
118  common1; // Vector containing the common elements of the first two rings
120  common2; // Vector containing the common elements of the three rings
121 
122  // Common elements among the first two rings
123  common1 = ring::findsCommonElements(ring1, ring2);
124  if (common1.size() == 0) {
125  return false;
126  } // no common elements in ring1 and ring2
127 
128  // Common elements among all three rings
129  common2 = ring::findsCommonElements(common1, ring3);
130 
131  // If no common elements were found:
132  if (common2.size() == 0) {
133  return false;
134  } // no common elements between ring1, ring2, and ring3
135 
136  return true; // Common elements found!
137 }
138 
149  //
150  int ringSize = ring.size(); // should be 6
151  std::vector<int> ringTriplet; // triplet from the ring to be searched
152  int kIndex; // Used for making the triplet
153 
154  // Loop through every possible triplet in the ring to be searched
155  for (int i = 0; i < ringSize; i++) {
156  ringTriplet.clear(); // init
157  // Get the first element of the ring triplet
158  ringTriplet.push_back(ring[i]);
159  //
160  // Get the next two elements
161  for (int k = 1; k < 3; k++) {
162  kIndex = i + k;
163  // Wrap-around
164  if (kIndex >= ringSize) {
165  kIndex -= ringSize;
166  } // end of wrap-around
167  ringTriplet.push_back(ring[kIndex]);
168  } // next two elements of ringTriplet
169  //
170  // Obtained ringTriplet!
171  // Check equality
172  if (triplet == ringTriplet) {
173  return true;
174  } // triplet matches!
175  //
176  // Check the reversed triplet too
177  std::reverse(ringTriplet.begin(), ringTriplet.end());
178 
179  // Check for equality
180  if (triplet == ringTriplet) {
181  return true;
182  } // reversed triplet matches
183  } // first element of ringTriplet
184 
185  return false;
186 }
187 
201  //
202  std::vector<std::vector<int>> ringSingleSize; // rings of one size
203 
204  // rings contains primitive rings of all sizes
205  // Only save rings of a given size (ringSize) to the new
206  // vector of vectors, ringSingleSize
207  for (int iring = 0; iring < rings.size(); iring++) {
208  // Check the size of the current ring
209  // If it is the correct size, save it in ringSingleSize
210  if (rings[iring].size() == ringSize) {
211  ringSingleSize.push_back(rings[iring]);
212  } // End of check of the size of iring
213  } // end of loop through all rings in rings
214 
215  return ringSingleSize;
216 }
217 
227  std::vector<int> commonElements; // Vector containing common elements
228 
229  // Sort the vectors before finding common elements
230  sort(ring1.begin(), ring1.end());
231  sort(ring2.begin(), ring2.end());
232 
233  // Find intersection of sorted vectors
234  auto it =
235  std::set_intersection(ring1.begin(), ring1.end(), ring2.begin(),
236  ring2.end(), std::back_inserter(commonElements));
237 
238  // If there are no elements in common, then return false
239  if (commonElements.size() == 0) {
240  return false;
241  }
242  // If there are common elements, return true
243  else {
244  return true;
245  }
246 }
247 
259  // Sort the rings first
260  sort(ring1.begin(), ring1.end()); // Sort ring1 by ID
261  sort(ring2.begin(), ring2.end()); // Sort ring2 by ID
262  bool result;
263 
264  (ring1 == ring2) ? result = true : result = false;
265 
266  return result;
267 }
T back_inserter(T... args)
T begin(T... args)
T clear(T... args)
T end(T... args)
T find(T... args)
bool commonElementsInThreeRings(std::vector< int > ring1, std::vector< int > ring2, std::vector< int > ring3)
Common elements in 3 rings.
Definition: ring.cpp:114
bool findTripletInRing(std::vector< int > ring, std::vector< int > triplet)
Searches a particular ring for a triplet.
Definition: ring.cpp:148
int clearRingList(std::vector< std::vector< int >> &rings)
Erases memory for a vector of vectors for a list of rings.
Definition: ring.cpp:22
bool hasCommonElements(std::vector< int > ring1, std::vector< int > ring2)
Definition: ring.cpp:226
int assignPolygonType(std::vector< std::vector< int >> rings, std::vector< int > *atomTypes, std::vector< int > nRings)
Definition: ring.cpp:40
std::vector< std::vector< int > > getSingleRingSize(std::vector< std::vector< int >> rings, int ringSize)
Returns a vector of vectors of rings of a single size.
Definition: ring.cpp:200
std::vector< int > findsCommonElements(std::vector< int > ring1, std::vector< int > ring2)
Returns the common elements of two rings.
Definition: ring.cpp:85
bool compareRings(std::vector< int > ring1, std::vector< int > ring2)
Definition: ring.cpp:258
Topological network criteria functions.
Definition: ring.hpp:64
T push_back(T... args)
T reverse(T... args)
File containing common functions used by bulk and confined topological network critera.
T set_intersection(T... args)
T size(T... args)
T swap(T... args)