TBTK
Need a break? Support the development by playing Polarity Puzzles
SusceptibilityCalculator.h
1 /* Copyright 2017 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
17 
24 #ifndef COM_DAFER45_TBTK_SUSCEPTIBILITY_CALCULATOR
25 #define COM_DAFER45_TBTK_SUSCEPTIBILITY_CALCULATOR
26 
27 #include "TBTK/RPA/DualIndex.h"
28 #include "TBTK/IndexedDataTree.h"
29 #include "TBTK/InteractionAmplitude.h"
30 #include "TBTK/RPA/MomentumSpaceContext.h"
31 #include "TBTK/Resource.h"
32 #include "TBTK/SerializableVector.h"
33 #include "TBTK/UnitHandler.h"
34 
35 #include <complex>
36 #include <vector>
37 
38 //#include <omp.h>
39 
40 namespace TBTK{
41 
42 class SusceptibilityCalculator{
43 public:
50  enum Algorithm {
51  Lindhard = 0,
52  Matsubara = 1
53  };
54 
56  SusceptibilityCalculator(
57  Algorithm algorithm,
58  const RPA::MomentumSpaceContext &momentumSpaceContext
59  );
60 
62  virtual ~SusceptibilityCalculator();
63 
67  virtual SusceptibilityCalculator* createSlave() = 0;
68 
70  virtual std::complex<double> calculateSusceptibility(
71  const std::vector<double> &k,
72  const std::vector<int> &orbitalIndices,
73  std::complex<double> energy
74  ) = 0;
75 
77  virtual std::vector<std::complex<double>> calculateSusceptibility(
78  const DualIndex &kDual,
79  const std::vector<int> &orbitalIndices
80  ) = 0;
81 
83  std::vector<std::complex<double>> calculateSusceptibility(
84  const std::vector<double> &k,
85  const std::vector<int> &orbitalIndices
86  );
87 
91 // void precompute(unsigned int numWorkers = 129);
92 
93  const RPA::MomentumSpaceContext& getMomentumSpaceContext() const;
94 
98  void generateKPlusQLookupTable();
99 
102  enum class EnergyType {Real, Imaginary, Complex};
103 
105  Algorithm getAlgorithm() const;
106 
108  void setEnergyType(EnergyType energyType);
109 
111  EnergyType getEnergyType() const;
112 
115  void setEnergies(
116  const std::vector<std::complex<double>> &energies
117  );
118 
121  const std::vector<std::complex<double>>& getEnergies() const;
122 
132  void setEnergiesAreInversionSymmetric(
133  bool energiesAreInversionSymmetric
134  );
135 
138  bool getEnergiesAreInversionSymmetric() const;
139 
141  void saveSusceptibilities(const std::string &filename) const;
142 
144  void loadSusceptibilities(const std::string &filename);
145 protected:
147  SusceptibilityCalculator(
148  Algorithm algorithm,
149  const RPA::MomentumSpaceContext &momentumSpaceContext,
150  int *kPlusQLookupTable/*,
151  double *fermiDiracLookupTable*/
152  );
153 
155  bool getIsMaster() const;
156 
158  int* getKPlusQLookupTable();
159 
161  const int* getKPlusQLookupTable() const;
162 
164  Index getSusceptibilityResultIndex(
165  const Index &kIndex,
166  const std::vector<int> &orbitalIndices
167  ) const;
168 
170  const IndexedDataTree<SerializableVector<std::complex<double>>>& getSusceptibilityTree() const;
171 
173  template<bool useKPlusQLookupTable>
174  int getKPlusQLinearIndex(
175  unsigned int meshIndex,
176  const std::vector<double> &k,
177  int kLinearIndex
178  ) const;
179 
181  void cacheSusceptibility(
182  const std::vector<std::complex<double>> &result,
183  const std::vector<double> &k,
184  const std::vector<int> &orbitalIndices,
185  const Index &kIndex,
186  const Index &resultIndex
187  );
188 
190  void clearCache();
191 private:
193  IndexedDataTree<SerializableVector<std::complex<double>>> susceptibilityTree;
194 
196  Algorithm algorithm;
197 
199  EnergyType energyType;
200 
202  std::vector<std::complex<double>> energies;
203 
207  bool energiesAreInversionSymmetric;
208 
210  const RPA::MomentumSpaceContext *momentumSpaceContext;
211 
213  int *kPlusQLookupTable;
214 
218  bool isMaster;
219 };
220 
221 inline const RPA::MomentumSpaceContext& SusceptibilityCalculator::getMomentumSpaceContext(
222 ) const{
223  return *momentumSpaceContext;
224 }
225 
226 inline SusceptibilityCalculator::Algorithm SusceptibilityCalculator::getAlgorithm() const{
227  return algorithm;
228 }
229 
230 inline Index SusceptibilityCalculator::getSusceptibilityResultIndex(
231  const Index &kIndex,
232  const std::vector<int> &orbitalIndices
233 ) const{
234  return Index(
235  kIndex,
236  {
237  orbitalIndices.at(0),
238  orbitalIndices.at(1),
239  orbitalIndices.at(2),
240  orbitalIndices.at(3)
241  }
242  );
243 }
244 
245 inline void SusceptibilityCalculator::setEnergyType(
246  EnergyType energyType
247 ){
248  this->energyType = energyType;
249 }
250 
251 inline SusceptibilityCalculator::EnergyType SusceptibilityCalculator::getEnergyType(
252 ) const{
253  return energyType;
254 }
255 
256 inline void SusceptibilityCalculator::setEnergies(
257  const std::vector<std::complex<double>> &energies
258 ){
259  this->energies = energies;
260  susceptibilityTree.clear();
261 }
262 
263 inline const std::vector<std::complex<double>>& SusceptibilityCalculator::getEnergies() const{
264  return energies;
265 }
266 
267 inline void SusceptibilityCalculator::setEnergiesAreInversionSymmetric(
268  bool energiesAreInversionSymmetric
269 ){
270  this->energiesAreInversionSymmetric = energiesAreInversionSymmetric;
271 }
272 
273 inline bool SusceptibilityCalculator::getEnergiesAreInversionSymmetric(
274 ) const{
275  return energiesAreInversionSymmetric;
276 }
277 
278 inline void SusceptibilityCalculator::saveSusceptibilities(
279  const std::string &filename
280 ) const{
281  Resource resource;
282  resource.setData(
283  susceptibilityTree.serialize(Serializable::Mode::JSON)
284  );
285  resource.write(filename);
286 }
287 
288 inline void SusceptibilityCalculator::loadSusceptibilities(
289  const std::string &filename
290 ){
291  Resource resource;
292  resource.read(filename);
293  susceptibilityTree = IndexedDataTree<SerializableVector<std::complex<double>>>(
294  resource.getData(),
295  Serializable::Mode::JSON
296  );
297 }
298 
299 inline std::vector<std::complex<double>> SusceptibilityCalculator::calculateSusceptibility(
300  const std::vector<double> &k,
301  const std::vector<int> &orbitalIndices
302 ){
303  return calculateSusceptibility(
304  DualIndex(
305  momentumSpaceContext->getKIndex(k),
306  k
307  ),
308  orbitalIndices
309  );
310 }
311 
312 inline void SusceptibilityCalculator::clearCache(){
313  susceptibilityTree.clear();
314 }
315 
316 inline bool SusceptibilityCalculator::getIsMaster() const{
317  return isMaster;
318 }
319 
320 inline int* SusceptibilityCalculator::getKPlusQLookupTable(){
321  return kPlusQLookupTable;
322 }
323 
324 inline const int* SusceptibilityCalculator::getKPlusQLookupTable() const{
325  return kPlusQLookupTable;
326 }
327 
328 template<>
329 inline int SusceptibilityCalculator::getKPlusQLinearIndex<false>(
330  unsigned int meshIndex,
331  const std::vector<double> &k,
332  int kLinearIndex
333 ) const{
334  const std::vector<std::vector<double>> &mesh
335  = momentumSpaceContext->getMesh();
336 
337  Index kPlusQIndex
338  = momentumSpaceContext->getBrillouinZone().getMinorCellIndex(
339  {mesh[meshIndex][0] + k[0], mesh[meshIndex][1] + k[1]},
340  momentumSpaceContext->getNumMeshPoints()
341  );
342  return momentumSpaceContext->getModel().getHoppingAmplitudeSet().getFirstIndexInBlock(
343  kPlusQIndex
344  );
345 }
346 
347 template<>
348 inline int SusceptibilityCalculator::getKPlusQLinearIndex<true>(
349  unsigned int meshIndex,
350  const std::vector<double> &k,
351  int kLinearIndex
352 ) const{
353  return kPlusQLookupTable[
354  meshIndex*momentumSpaceContext->getMesh().size()
355  + kLinearIndex/momentumSpaceContext->getNumOrbitals()
356  ];
357 }
358 
359 inline const IndexedDataTree<SerializableVector<std::complex<double>>>& SusceptibilityCalculator::getSusceptibilityTree() const{
360  return susceptibilityTree;
361 }
362 
363 }; //End of namespace TBTK
364 
365 #endif
366 
Handles conversions between different units.
Data structure for storing data associated with an index.
Read and write string resources from file, URL, etc.
Definition: Boolean.h:32