TBTK
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 
23 #ifndef COM_DAFER45_TBTK_SUSCEPTIBILITY_CALCULATOR
24 #define COM_DAFER45_TBTK_SUSCEPTIBILITY_CALCULATOR
25 
26 #include "TBTK/RPA/DualIndex.h"
27 #include "TBTK/IndexedDataTree.h"
28 #include "TBTK/InteractionAmplitude.h"
30 #include "TBTK/Resource.h"
32 #include "TBTK/UnitHandler.h"
33 
34 #include <complex>
35 #include <vector>
36 
37 //#include <omp.h>
38 
39 namespace TBTK{
40 
42 public:
49  enum Algorithm {
50  Lindhard = 0,
51  Matsubara = 1
52  };
53 
56  Algorithm algorithm,
57  const MomentumSpaceContext &momentumSpaceContext
58  );
59 
61  virtual ~SusceptibilityCalculator();
62 
67 
69  virtual std::complex<double> calculateSusceptibility(
70  const std::vector<double> &k,
71  const std::vector<int> &orbitalIndices,
72  std::complex<double> energy
73  ) = 0;
74 
76  virtual std::vector<std::complex<double>> calculateSusceptibility(
77  const DualIndex &kDual,
78  const std::vector<int> &orbitalIndices
79  ) = 0;
80 
82  std::vector<std::complex<double>> calculateSusceptibility(
83  const std::vector<double> &k,
84  const std::vector<int> &orbitalIndices
85  );
86 
90 // void precompute(unsigned int numWorkers = 129);
91 
93 
98 
101  enum class EnergyType {Real, Imaginary, Complex};
102 
104  Algorithm getAlgorithm() const;
105 
107  void setEnergyType(EnergyType energyType);
108 
110  EnergyType getEnergyType() const;
111 
114  void setEnergies(
115  const std::vector<std::complex<double>> &energies
116  );
117 
120  const std::vector<std::complex<double>>& getEnergies() const;
121 
132  bool energiesAreInversionSymmetric
133  );
134 
138 
140  void saveSusceptibilities(const std::string &filename) const;
141 
143  void loadSusceptibilities(const std::string &filename);
144 protected:
147  Algorithm algorithm,
148  const MomentumSpaceContext &momentumSpaceContext,
149  int *kPlusQLookupTable/*,
150  double *fermiDiracLookupTable*/
151  );
152 
154  bool getIsMaster() const;
155 
157  int* getKPlusQLookupTable();
158 
160  const int* getKPlusQLookupTable() const;
161 
164  const Index &kIndex,
165  const std::vector<int> &orbitalIndices
166  ) const;
167 
170 
172  template<bool useKPlusQLookupTable>
174  unsigned int meshIndex,
175  const std::vector<double> &k,
176  int kLinearIndex
177  ) const;
178 
180  void cacheSusceptibility(
181  const std::vector<std::complex<double>> &result,
182  const std::vector<double> &k,
183  const std::vector<int> &orbitalIndices,
184  const Index &kIndex,
185  const Index &resultIndex
186  );
187 
189  void clearCache();
190 private:
193 
195  Algorithm algorithm;
196 
198  EnergyType energyType;
199 
201  std::vector<std::complex<double>> energies;
202 
206  bool energiesAreInversionSymmetric;
207 
209  const MomentumSpaceContext *momentumSpaceContext;
210 
212  int *kPlusQLookupTable;
213 
217  bool isMaster;
218 };
219 
221 ) const{
222  return *momentumSpaceContext;
223 }
224 
226  return algorithm;
227 }
228 
230  const Index &kIndex,
231  const std::vector<int> &orbitalIndices
232 ) const{
233  return Index(
234  kIndex,
235  {
236  orbitalIndices.at(0),
237  orbitalIndices.at(1),
238  orbitalIndices.at(2),
239  orbitalIndices.at(3)
240  }
241  );
242 }
243 
245  EnergyType energyType
246 ){
247  this->energyType = energyType;
248 }
249 
251 ) const{
252  return energyType;
253 }
254 
256  const std::vector<std::complex<double>> &energies
257 ){
258  this->energies = energies;
259  susceptibilityTree.clear();
260 }
261 
262 inline const std::vector<std::complex<double>>& SusceptibilityCalculator::getEnergies() const{
263  return energies;
264 }
265 
267  bool energiesAreInversionSymmetric
268 ){
269  this->energiesAreInversionSymmetric = energiesAreInversionSymmetric;
270 }
271 
273 ) const{
274  return energiesAreInversionSymmetric;
275 }
276 
278  const std::string &filename
279 ) const{
280  Resource resource;
281  resource.setData(
282  susceptibilityTree.serialize(Serializable::Mode::JSON)
283  );
284  resource.write(filename);
285 }
286 
288  const std::string &filename
289 ){
290  Resource resource;
291  resource.read(filename);
293  resource.getData(),
294  Serializable::Mode::JSON
295  );
296 }
297 
298 inline std::vector<std::complex<double>> SusceptibilityCalculator::calculateSusceptibility(
299  const std::vector<double> &k,
300  const std::vector<int> &orbitalIndices
301 ){
303  DualIndex(
304  momentumSpaceContext->getKIndex(k),
305  k
306  ),
307  orbitalIndices
308  );
309 }
310 
312  susceptibilityTree.clear();
313 }
314 
316  return isMaster;
317 }
318 
320  return kPlusQLookupTable;
321 }
322 
324  return kPlusQLookupTable;
325 }
326 
327 template<>
328 inline int SusceptibilityCalculator::getKPlusQLinearIndex<false>(
329  unsigned int meshIndex,
330  const std::vector<double> &k,
331  int kLinearIndex
332 ) const{
333  const std::vector<std::vector<double>> &mesh
334  = momentumSpaceContext->getMesh();
335 
336  Index kPlusQIndex
337  = momentumSpaceContext->getBrillouinZone().getMinorCellIndex(
338  {mesh[meshIndex][0] + k[0], mesh[meshIndex][1] + k[1]},
339  momentumSpaceContext->getNumMeshPoints()
340  );
341  return momentumSpaceContext->getModel().getHoppingAmplitudeSet().getFirstIndexInBlock(
342  kPlusQIndex
343  );
344 }
345 
346 template<>
347 inline int SusceptibilityCalculator::getKPlusQLinearIndex<true>(
348  unsigned int meshIndex,
349  const std::vector<double> &k,
350  int kLinearIndex
351 ) const{
352  return kPlusQLookupTable[
353  meshIndex*momentumSpaceContext->getMesh().size()
354  + kLinearIndex/momentumSpaceContext->getNumOrbitals()
355  ];
356 }
357 
359  return susceptibilityTree;
360 }
361 
362 }; //End of namespace TBTK
363 
364 #endif
bool getIsMaster() const
Definition: SusceptibilityCalculator.h:315
int * getKPlusQLookupTable()
Definition: SusceptibilityCalculator.h:319
void cacheSusceptibility(const std::vector< std::complex< double >> &result, const std::vector< double > &k, const std::vector< int > &orbitalIndices, const Index &kIndex, const Index &resultIndex)
bool getEnergiesAreInversionSymmetric() const
Definition: SusceptibilityCalculator.h:272
Handles conversions between different units.
void read(const std::string &uri)
void setData(const std::string &data)
Definition: Resource.h:73
Property container for density.
Data structure for storing data associated with an index.
int getKPlusQLinearIndex(unsigned int meshIndex, const std::vector< double > &k, int kLinearIndex) const
virtual SusceptibilityCalculator * createSlave()=0
Definition: DualIndex.h:33
Algorithm
Definition: SusceptibilityCalculator.h:49
Index getSusceptibilityResultIndex(const Index &kIndex, const std::vector< int > &orbitalIndices) const
Definition: SusceptibilityCalculator.h:229
const MomentumSpaceContext & getMomentumSpaceContext() const
Definition: SusceptibilityCalculator.h:220
const std::vector< std::complex< double > > & getEnergies() const
Definition: SusceptibilityCalculator.h:262
Allows Serializable objects to be saved and loaded using URI.
Extends an Index with a continuous representation of the index.
const std::string & getData() const
Definition: Resource.h:77
void write(const std::string &uri)
Definition: SusceptibilityCalculator.h:41
Definition: IndexedDataTree.h:43
void setEnergies(const std::vector< std::complex< double >> &energies)
Definition: SusceptibilityCalculator.h:255
Definition: MomentumSpaceContext.h:32
void clearCache()
Definition: SusceptibilityCalculator.h:311
Definition: Resource.h:33
EnergyType
Definition: SusceptibilityCalculator.h:101
Flexible physical index.
Definition: Index.h:69
void saveSusceptibilities(const std::string &filename) const
Definition: SusceptibilityCalculator.h:277
Definition: ModelFactory.h:35
void setEnergiesAreInversionSymmetric(bool energiesAreInversionSymmetric)
Definition: SusceptibilityCalculator.h:266
void loadSusceptibilities(const std::string &filename)
Definition: SusceptibilityCalculator.h:287
SusceptibilityCalculator(Algorithm algorithm, const MomentumSpaceContext &momentumSpaceContext)
const IndexedDataTree< SerializableVector< std::complex< double > > > & getSusceptibilityTree() const
Definition: SusceptibilityCalculator.h:358
void setEnergyType(EnergyType energyType)
Definition: SusceptibilityCalculator.h:244
EnergyType getEnergyType() const
Definition: SusceptibilityCalculator.h:250
Serializable wrapper of std::vector.
virtual std::complex< double > calculateSusceptibility(const std::vector< double > &k, const std::vector< int > &orbitalIndices, std::complex< double > energy)=0
int & at(unsigned int n)
Definition: Index.h:351
Algorithm getAlgorithm() const
Definition: SusceptibilityCalculator.h:225