TBTK
ChebyshevExpander.h
1 /* Copyright 2016 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 
25 #ifndef COM_DAFER45_TBTK_SOLVER_CHEBYSHEV_EXPANDER
26 #define COM_DAFER45_TBTK_SOLVER_CHEBYSHEV_EXPANDER
27 
28 #include "TBTK/Communicator.h"
29 #include "TBTK/Model.h"
30 #include "TBTK/Solver/Solver.h"
31 
32 #include <complex>
33 #ifndef __APPLE__
34 # include <omp.h>
35 #endif
36 
37 namespace TBTK{
38 namespace Solver{
39 
62 class ChebyshevExpander : public Solver, public Communicator{
63 public:
66 
68  virtual ~ChebyshevExpander();
69 
71  virtual void setModel(Model &model);
72 
78  void setScaleFactor(double scaleFactor);
79 
83  double getScaleFactor();
84 
89  void setNumCoefficients(int numCoefficients);
90 
95  int getNumCoefficients() const;
96 
101  void setBroadening(double broadening);
102 
107  double getBroadening() const;
108 
113  void setEnergyResolution(int energyResolution);
114 
119  int getEnergyResolution() const;
120 
125  void setLowerBound(double lowerBound);
126 
131  double getLowerBound() const;
132 
137  void setUpperBound(double upperBound);
138 
143  double getUpperBound() const;
144 
150  void setCalculateCoefficientsOnGPU(bool calculateCoefficientsOnGPU);
151 
155  bool getCalculateCoefficientsOnGPU() const;
156 
163  bool generateGreensFunctionsOnGPU
164  );
165 
169  bool getGenerateGreensFunctionsOnGPU() const;
170 
175  void setUseLookupTable(bool useLookupTable);
176 
181  bool getUseLookupTable() const ;
182 
197  std::vector<std::vector<std::complex<double>>> calculateCoefficients(
198  std::vector<Index> &to,
199  Index from
200  );
201 
213  std::vector<std::complex<double>> calculateCoefficients(
214  Index to,
215  Index from
216  );
217 
219  enum class Type{
220  Advanced,
221  Retarded,
222  Principal,
223  NonPrincipal
224  };
225 
236  std::vector<std::complex<double>> generateGreensFunction(
237  const std::vector<std::complex<double>> &coefficients,
238  Type type = Type::Retarded
239  );
240 private:
241  //These three functions are experimental and therefore not part of the
242  //public interface of released code.
243 
245  void calculateCoefficientsWithCutoff(
246  Index to,
247  Index from,
248  std::complex<double> *coefficients,
249  int numCoefficients,
250  double componentCutoff,
251  double broadening = 0.000001
252  );
253 
265  std::complex<double> getMonolopoulosABCDamping(
266  double distanceToEdge,
267  double boundarySize,
268  double e = 1.,
269  double c = 2.62
270  );
271 
276  void setDamping(std::complex<double> *damping);
277 private:
279  double scaleFactor;
280 
282  int numCoefficients;
283 
285  double broadening;
286 
289  int energyResolution;
290 
292  double lowerBound;
293 
295  double upperBound;
296 
299  bool calculateCoefficientsOnGPU;
300 
303  bool generateGreensFunctionsOnGPU;
304 
307  bool useLookupTable;
308 
310  std::complex<double> *damping;
311 
314  std::complex<double> **generatingFunctionLookupTable;
315 
317  std::complex<double> ***generatingFunctionLookupTable_device;
318 
321  int lookupTableNumCoefficients;
322 
325  int lookupTableResolution;
326 
328  double lookupTableLowerBound;
329 
331  double lookupTableUpperBound;
332 
334  void ensureLookupTableIsReady();
335 
344  void generateLookupTable(
345  int numCoefficeints,
346  int energyResolution,
347  double lowerBound = -1.,
348  double upperBound = 1.
349  );
350 
352  void destroyLookupTable();
353 
355  bool getLookupTableIsGenerated();
356 
359  void loadLookupTableGPU();
360 
363  void destroyLookupTableGPU();
364 
366  bool getLookupTableIsLoadedGPU();
367 
380  std::vector<
381  std::vector<std::complex<double>>
382  > calculateCoefficientsCPU(
383  std::vector<Index> &to,
384  Index from
385  );
386 
396  std::vector<std::complex<double>> calculateCoefficientsCPU(
397  Index to,
398  Index from
399  );
400 
412  std::vector<
413  std::vector<std::complex<double>>
414  > calculateCoefficientsGPU(
415  std::vector<Index> &to,
416  Index from
417  );
418 
427  std::vector<std::complex<double>> calculateCoefficientsGPU(
428  Index to,
429  Index from
430  );
431 
445 /* std::complex<double>* generateGreensFunctionCPU(
446  std::complex<double> *coefficients,
447  int numCoefficients,
448  int energyResolution,
449  double lowerBound = -1.,
450  double upperBound = 1.,
451  Type type = Type::Retarded
452  );*/
453 
464  std::vector<std::complex<double>> generateGreensFunctionCPU(
465  const std::vector<std::complex<double>> &coefficients,
466  Type type = Type::Retarded
467  );
468 
479  std::vector<std::complex<double>> generateGreensFunctionGPU(
480  const std::vector<std::complex<double>> &coefficients,
481  Type type = Type::Retarded
482  );
483 };
484 
485 inline void ChebyshevExpander::setScaleFactor(double scaleFactor){
486  if(generatingFunctionLookupTable != nullptr)
487  destroyLookupTable();
488  if(generatingFunctionLookupTable_device != nullptr)
489  destroyLookupTableGPU();
490 
491  this->scaleFactor = scaleFactor;
492 }
493 
495  return scaleFactor;
496 }
497 
498 inline void ChebyshevExpander::setNumCoefficients(int numCoefficients){
499  if(generatingFunctionLookupTable != nullptr)
500  destroyLookupTable();
501  if(generatingFunctionLookupTable_device != nullptr)
502  destroyLookupTableGPU();
503 
504  this->numCoefficients = numCoefficients;
505 }
506 
508  return numCoefficients;
509 }
510 
511 inline void ChebyshevExpander::setBroadening(double broadening){
512  this->broadening = broadening;
513 }
514 
515 inline double ChebyshevExpander::getBroadening() const{
516  return broadening;
517 }
518 
519 inline void ChebyshevExpander::setEnergyResolution(int energyResolution){
520  if(generatingFunctionLookupTable != nullptr)
521  destroyLookupTable();
522  if(generatingFunctionLookupTable_device != nullptr)
523  destroyLookupTableGPU();
524 
525  this->energyResolution = energyResolution;
526 }
527 
529  return energyResolution;
530 }
531 
532 inline void ChebyshevExpander::setLowerBound(double lowerBound){
533  if(generatingFunctionLookupTable != nullptr)
534  destroyLookupTable();
535  if(generatingFunctionLookupTable_device != nullptr)
536  destroyLookupTableGPU();
537 
538  this->lowerBound = lowerBound;
539 }
540 
541 inline double ChebyshevExpander::getLowerBound() const{
542  return lowerBound;
543 }
544 
545 inline void ChebyshevExpander::setUpperBound(double upperBound){
546  if(generatingFunctionLookupTable != nullptr)
547  destroyLookupTable();
548  if(generatingFunctionLookupTable_device != nullptr)
549  destroyLookupTableGPU();
550 
551  this->upperBound = upperBound;
552 }
553 
554 inline double ChebyshevExpander::getUpperBound() const{
555  return upperBound;
556 }
557 
559  bool calculateCoefficientsOnGPU
560 ){
561  this->calculateCoefficientsOnGPU = calculateCoefficientsOnGPU;
562 }
563 
565  return calculateCoefficientsOnGPU;
566 }
567 
569  bool generateGreensFunctionsOnGPU
570 ){
571  this->generateGreensFunctionsOnGPU = generateGreensFunctionsOnGPU;
572 }
573 
575  return generateGreensFunctionsOnGPU;
576 }
577 
578 inline void ChebyshevExpander::setUseLookupTable(bool useLookupTable){
579  if(!useLookupTable){
580  if(generatingFunctionLookupTable != nullptr)
581  destroyLookupTable();
582  if(generatingFunctionLookupTable_device != nullptr)
583  destroyLookupTableGPU();
584  }
585 
586  this->useLookupTable = useLookupTable;
587 }
588 
590  return useLookupTable;
591 }
592 
593 inline std::vector<
594  std::vector<std::complex<double>>
596  std::vector<Index> &to,
597  Index from
598 ){
599  if(calculateCoefficientsOnGPU){
600  return calculateCoefficientsGPU(
601  to,
602  from
603  );
604  }
605  else{
606  return calculateCoefficientsCPU(
607  to,
608  from
609  );
610  }
611 }
612 
613 inline std::vector<std::complex<double>> ChebyshevExpander::calculateCoefficients(
614  Index to,
615  Index from
616 ){
617  if(calculateCoefficientsOnGPU){
618  return calculateCoefficientsGPU(
619  to,
620  from
621  );
622  }
623  else{
624  return calculateCoefficientsCPU(
625  to,
626  from
627  );
628  }
629 }
630 
631 inline bool ChebyshevExpander::getLookupTableIsGenerated(){
632  if(generatingFunctionLookupTable != NULL)
633  return true;
634  else
635  return false;
636 }
637 
638 inline bool ChebyshevExpander::getLookupTableIsLoadedGPU(){
639  if(generatingFunctionLookupTable_device != NULL)
640  return true;
641  else
642  return false;
643 }
644 
645 inline std::vector<std::complex<double>> ChebyshevExpander::generateGreensFunction(
646  const std::vector<std::complex<double>> &coefficients,
647  Type type
648 ){
649  if(generateGreensFunctionsOnGPU){
650  return generateGreensFunctionGPU(coefficients, type);
651  }
652  else{
653  return generateGreensFunctionCPU(coefficients, type);
654  }
655 }
656 
657 inline void ChebyshevExpander::setDamping(std::complex<double> *damping){
658  this->damping = damping;
659 }
660 
661 inline void ChebyshevExpander::ensureLookupTableIsReady(){
662  if(useLookupTable){
663  if(!generatingFunctionLookupTable)
664  generateLookupTable(numCoefficients, energyResolution, lowerBound, upperBound);
665  if(generateGreensFunctionsOnGPU && !generatingFunctionLookupTable_device)
666  loadLookupTableGPU();
667  }
668  else if(generateGreensFunctionsOnGPU){
669  TBTKExit(
670  "Solver::ChebyshevSolver::ensureLookupTableIsReady()",
671  "Green's functions can only be generated on GPU using"
672  << " lookup tables.",
673  "Use Solver::ChebyshevExpander::setGenerateGreensFunctionOnGPU()"
674  << " and"
675  << " Solver::ChebyshevExpander::setUseLookupTable() to"
676  << " configure the Solver::ChebyshevExpander."
677  );
678  }
679 }
680 
681 }; //End of namespace Solver
682 }; //End of namespace TBTK
683 
684 #endif
std::vector< std::vector< std::complex< double > > > calculateCoefficients(std::vector< Index > &to, Index from)
Base class for Solvers.
Definition: Solver.h:32
Type
Definition: ChebyshevExpander.h:219
void setBroadening(double broadening)
Definition: ChebyshevExpander.h:511
std::vector< std::complex< double > > generateGreensFunction(const std::vector< std::complex< double >> &coefficients, Type type=Type::Retarded)
Definition: ChebyshevExpander.h:645
bool getGenerateGreensFunctionsOnGPU() const
Definition: ChebyshevExpander.h:574
Container of Model related information.
Solves a Model using the Chebyshev method.
Definition: ChebyshevExpander.h:62
virtual void setModel(Model &model)
bool getCalculateCoefficientsOnGPU() const
Definition: ChebyshevExpander.h:564
void setLowerBound(double lowerBound)
Definition: ChebyshevExpander.h:532
int getEnergyResolution() const
Definition: ChebyshevExpander.h:528
double getLowerBound() const
Definition: ChebyshevExpander.h:541
Base class for Solvers.
void setUseLookupTable(bool useLookupTable)
Definition: ChebyshevExpander.h:578
void setEnergyResolution(int energyResolution)
Definition: ChebyshevExpander.h:519
Flexible physical index.
Definition: Index.h:69
Definition: ModelFactory.h:35
bool getUseLookupTable() const
Definition: ChebyshevExpander.h:589
void setNumCoefficients(int numCoefficients)
Definition: ChebyshevExpander.h:498
Base class that communicate their current status during execution.
void setGenerateGreensFunctionsOnGPU(bool generateGreensFunctionsOnGPU)
Definition: ChebyshevExpander.h:568
int getNumCoefficients() const
Definition: ChebyshevExpander.h:507
void setCalculateCoefficientsOnGPU(bool calculateCoefficientsOnGPU)
Definition: ChebyshevExpander.h:558
Definition: Communicator.h:28
double getUpperBound() const
Definition: ChebyshevExpander.h:554
double getScaleFactor()
Definition: ChebyshevExpander.h:494
void setUpperBound(double upperBound)
Definition: ChebyshevExpander.h:545
void setScaleFactor(double scaleFactor)
Definition: ChebyshevExpander.h:485
Container of Model related information.
Definition: Model.h:52
double getBroadening() const
Definition: ChebyshevExpander.h:515