DFT-FE 1.1.0-pre
Density Functional Theory With Finite-Elements
Loading...
Searching...
No Matches
OptimizedIndexSet.t.cc
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (c) 2017-2025 The Regents of the University of Michigan and DFT-FE
4// authors.
5//
6// This file is part of the DFT-FE code.
7//
8// The DFT-FE code is free software; you can use it, redistribute
9// it, and/or modify it under the terms of the GNU Lesser General
10// Public License as published by the Free Software Foundation; either
11// version 2.1 of the License, or (at your option) any later version.
12// The full text of the license can be found in the file LICENSE at
13// the top level of the DFT-FE distribution.
14//
15// ---------------------------------------------------------------------
16//
17
18/*
19 * @author Bikash Kanungo
20 */
21
22#include <iterator>
23#include <algorithm>
24#include <type_traits>
25#include <Exceptions.h>
26#include <TypeConfig.h>
27
28namespace dftfe
29{
30 namespace utils
31 {
32 //
33 // Constructor
34 //
35 template <typename T>
37 const std::set<T> &inputSet /*=std::set<T>()*/)
41 {
42 bool isValid = std::is_same<dftfe::uInt, T>::value ||
43 std::is_same<dftfe::uInt, T>::value;
45 isValid,
46 "OptimizedIndexSet expects the template parameter to be of type dftfe::uInt or unsigned long int.");
47 if (!inputSet.empty())
48 {
49 typename std::set<T>::const_iterator itLastRange = inputSet.begin();
50 typename std::set<T>::const_iterator itPrev = inputSet.begin();
51 typename std::set<T>::const_iterator it = itPrev;
52 it++;
53 for (; it != inputSet.end(); ++it)
54 {
55 bool isContiguous = ((*it - 1) == *(itPrev));
56 if (!isContiguous)
57 {
58 d_contiguousRanges.push_back(*itLastRange);
59 d_contiguousRanges.push_back(*(itPrev) + 1);
60 itLastRange = it;
61 }
62 itPrev = it;
63 }
64
65 d_contiguousRanges.push_back(*itLastRange);
66 d_contiguousRanges.push_back(*(itPrev) + 1);
67
70 dftfe::uInt cumulativeEntries = 0;
71 for (dftfe::uInt i = 0; i < d_numContiguousRanges; ++i)
72 {
73 d_numEntriesBefore[i] = cumulativeEntries;
74 cumulativeEntries +=
75 d_contiguousRanges[2 * i + 1] - d_contiguousRanges[2 * i];
76 }
77 }
78 }
79
80 template <typename T>
81 void
83 dftfe::uInt &pos,
84 bool &found) const
85 {
86 found = false;
87 /*
88 * The logic used for finding an index is as follows:
89 * 1. Find the position of the element in d_contiguousRanges
90 * which is greater than (strictly greater) the input index.
91 * Let's call this position as upPos and the value at upPos as
92 * upVal. The complexity of finding it is
93 * O(log(size of d_contiguousRanges))
94 * 2. Since d_contiguousRanges stores pairs of startId and endId
95 * (endId not inclusive) of contiguous ranges in inputSet,
96 * any index for which upPos is even (i.e., it corresponds to a
97 * startId) cannot belong to inputSet. Why? Consider two consequtive
98 * ranges [k1,k2) and [k3,k4) where k1 < k2 < k3 < k4. If upVal for index
99 * corresponds to k3 (i.e., startId of a range), then (a) index does not
100 * lie in the [k3,k4) as index < upVal (=k3). (b) index cannot lie in
101 * [k1,k2), because if index lies in [k1,k2), then upVal should be k2 (not
102 * k3)
103 * 3. If upPos is odd (i.e, it corresponds to an endId), we find the
104 * relative position of index in that range. Subsequently, we determine
105 * the global position of index in inputSet by adding the relative
106 * position to the number of entries in inputSet prior to the range where
107 * index lies
108 */
109
110 auto up = std::upper_bound(d_contiguousRanges.begin(),
111 d_contiguousRanges.end(),
112 index);
113 if (up != d_contiguousRanges.end())
114 {
115 dftfe::uInt upPos = std::distance(d_contiguousRanges.begin(), up);
116 if (upPos % 2 == 1)
117 {
118 found = true;
119 dftfe::uInt rangeId = upPos / 2;
120 pos = d_numEntriesBefore[rangeId] + index -
121 d_contiguousRanges[upPos - 1];
122 }
123 }
124 }
125
126 template <typename T>
127 bool
129 {
131 return false;
132 else
134 }
135 } // end of namespace utils
136} // end of namespace dftfe
void getPosition(const T &index, dftfe::uInt &pos, bool &found) const
Definition OptimizedIndexSet.t.cc:82
std::vector< T > d_contiguousRanges
Definition OptimizedIndexSet.h:73
std::vector< dftfe::uInt > d_numEntriesBefore
Definition OptimizedIndexSet.h:77
OptimizedIndexSet(const std::set< T > &inputSet=std::set< T >())
Constructor.
Definition OptimizedIndexSet.t.cc:36
dftfe::uInt d_numContiguousRanges
Store the number of contiguous ranges in the input set of indices.
Definition OptimizedIndexSet.h:64
Definition Cell.h:36
void throwException(bool condition, std::string msg="")
Definition pseudoPotentialToDftfeConverter.cc:34
std::uint32_t uInt
Definition TypeConfig.h:10