TBTK
Need a break? Support the development by playing Polarity Puzzles
ChebyshevExpander.h
Go to the documentation of this file.
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 
69 class ChebyshevExpander : public Solver, public Communicator{
70 public:
73 
75  virtual ~ChebyshevExpander();
76 
78 // virtual void setModel(Model &model);
79 
85  void setScaleFactor(double scaleFactor);
86 
90  double getScaleFactor();
91 
96  void setNumCoefficients(int numCoefficients);
97 
102  int getNumCoefficients() const;
103 
108  void setBroadening(double broadening);
109 
114  double getBroadening() const;
115 
120  void setEnergyResolution(int energyResolution);
121 
126  int getEnergyResolution() const;
127 
132  void setLowerBound(double lowerBound);
133 
138  double getLowerBound() const;
139 
144  void setUpperBound(double upperBound);
145 
150  double getUpperBound() const;
151 
157  void setCalculateCoefficientsOnGPU(bool calculateCoefficientsOnGPU);
158 
162  bool getCalculateCoefficientsOnGPU() const;
163 
170  bool generateGreensFunctionsOnGPU
171  );
172 
176  bool getGenerateGreensFunctionsOnGPU() const;
177 
182  void setUseLookupTable(bool useLookupTable);
183 
188  bool getUseLookupTable() const ;
189 
204  std::vector<std::vector<std::complex<double>>> calculateCoefficients(
205  std::vector<Index> &to,
206  Index from
207  );
208 
220  std::vector<std::complex<double>> calculateCoefficients(
221  Index to,
222  Index from
223  );
224 
226  enum class Type{
227  Advanced,
228  Retarded,
229  Principal,
230  NonPrincipal
231  };
232 
243  std::vector<std::complex<double>> generateGreensFunction(
244  const std::vector<std::complex<double>> &coefficients,
245  Type type = Type::Retarded
246  );
247 private:
248  //These three functions are experimental and therefore not part of the
249  //public interface of released code.
250 
252  void calculateCoefficientsWithCutoff(
253  Index to,
254  Index from,
255  std::complex<double> *coefficients,
256  int numCoefficients,
257  double componentCutoff,
258  double broadening = 0.000001
259  );
260 
272  std::complex<double> getMonolopoulosABCDamping(
273  double distanceToEdge,
274  double boundarySize,
275  double e = 1.,
276  double c = 2.62
277  );
278 
283  void setDamping(std::complex<double> *damping);
284 private:
286  double scaleFactor;
287 
289  int numCoefficients;
290 
292  double broadening;
293 
296  int energyResolution;
297 
299  double lowerBound;
300 
302  double upperBound;
303 
306  bool calculateCoefficientsOnGPU;
307 
310  bool generateGreensFunctionsOnGPU;
311 
314  bool useLookupTable;
315 
317  std::complex<double> *damping;
318 
321  std::complex<double> **generatingFunctionLookupTable;
322 
324  std::complex<double> ***generatingFunctionLookupTable_device;
325 
328  int lookupTableNumCoefficients;
329 
332  int lookupTableResolution;
333 
335  double lookupTableLowerBound;
336 
338  double lookupTableUpperBound;
339 
341  void ensureLookupTableIsReady();
342 
351  void generateLookupTable(
352  int numCoefficeints,
353  int energyResolution,
354  double lowerBound = -1.,
355  double upperBound = 1.
356  );
357 
359  void destroyLookupTable();
360 
362  bool getLookupTableIsGenerated();
363 
366  void loadLookupTableGPU();
367 
370  void destroyLookupTableGPU();
371 
373  bool getLookupTableIsLoadedGPU();
374 
387  std::vector<
388  std::vector<std::complex<double>>
389  > calculateCoefficientsCPU(
390  std::vector<Index> &to,
391  Index from
392  );
393 
403  std::vector<std::complex<double>> calculateCoefficientsCPU(
404  Index to,
405  Index from
406  );
407 
419  std::vector<
420  std::vector<std::complex<double>>
421  > calculateCoefficientsGPU(
422  std::vector<Index> &to,
423  Index from
424  );
425 
434  std::vector<std::complex<double>> calculateCoefficientsGPU(
435  Index to,
436  Index from
437  );
438 
452 /* std::complex<double>* generateGreensFunctionCPU(
453  std::complex<double> *coefficients,
454  int numCoefficients,
455  int energyResolution,
456  double lowerBound = -1.,
457  double upperBound = 1.,
458  Type type = Type::Retarded
459  );*/
460 
471  std::vector<std::complex<double>> generateGreensFunctionCPU(
472  const std::vector<std::complex<double>> &coefficients,
473  Type type = Type::Retarded
474  );
475 
486  std::vector<std::complex<double>> generateGreensFunctionGPU(
487  const std::vector<std::complex<double>> &coefficients,
488  Type type = Type::Retarded
489  );
490 };
491 
492 inline void ChebyshevExpander::setScaleFactor(double scaleFactor){
493  if(generatingFunctionLookupTable != nullptr)
494  destroyLookupTable();
495  if(generatingFunctionLookupTable_device != nullptr)
496  destroyLookupTableGPU();
497 
498  this->scaleFactor = scaleFactor;
499 }
500 
502  return scaleFactor;
503 }
504 
505 inline void ChebyshevExpander::setNumCoefficients(int numCoefficients){
506  if(generatingFunctionLookupTable != nullptr)
507  destroyLookupTable();
508  if(generatingFunctionLookupTable_device != nullptr)
509  destroyLookupTableGPU();
510 
511  this->numCoefficients = numCoefficients;
512 }
513 
515  return numCoefficients;
516 }
517 
518 inline void ChebyshevExpander::setBroadening(double broadening){
519  this->broadening = broadening;
520 }
521 
522 inline double ChebyshevExpander::getBroadening() const{
523  return broadening;
524 }
525 
526 inline void ChebyshevExpander::setEnergyResolution(int energyResolution){
527  if(generatingFunctionLookupTable != nullptr)
528  destroyLookupTable();
529  if(generatingFunctionLookupTable_device != nullptr)
530  destroyLookupTableGPU();
531 
532  this->energyResolution = energyResolution;
533 }
534 
536  return energyResolution;
537 }
538 
539 inline void ChebyshevExpander::setLowerBound(double lowerBound){
540  if(generatingFunctionLookupTable != nullptr)
541  destroyLookupTable();
542  if(generatingFunctionLookupTable_device != nullptr)
543  destroyLookupTableGPU();
544 
545  this->lowerBound = lowerBound;
546 }
547 
548 inline double ChebyshevExpander::getLowerBound() const{
549  return lowerBound;
550 }
551 
552 inline void ChebyshevExpander::setUpperBound(double upperBound){
553  if(generatingFunctionLookupTable != nullptr)
554  destroyLookupTable();
555  if(generatingFunctionLookupTable_device != nullptr)
556  destroyLookupTableGPU();
557 
558  this->upperBound = upperBound;
559 }
560 
561 inline double ChebyshevExpander::getUpperBound() const{
562  return upperBound;
563 }
564 
566  bool calculateCoefficientsOnGPU
567 ){
568  this->calculateCoefficientsOnGPU = calculateCoefficientsOnGPU;
569 }
570 
572  return calculateCoefficientsOnGPU;
573 }
574 
576  bool generateGreensFunctionsOnGPU
577 ){
578  this->generateGreensFunctionsOnGPU = generateGreensFunctionsOnGPU;
579 }
580 
582  return generateGreensFunctionsOnGPU;
583 }
584 
585 inline void ChebyshevExpander::setUseLookupTable(bool useLookupTable){
586  if(!useLookupTable){
587  if(generatingFunctionLookupTable != nullptr)
588  destroyLookupTable();
589  if(generatingFunctionLookupTable_device != nullptr)
590  destroyLookupTableGPU();
591  }
592 
593  this->useLookupTable = useLookupTable;
594 }
595 
597  return useLookupTable;
598 }
599 
600 inline std::vector<
601  std::vector<std::complex<double>>
603  std::vector<Index> &to,
604  Index from
605 ){
606  if(calculateCoefficientsOnGPU){
607  return calculateCoefficientsGPU(
608  to,
609  from
610  );
611  }
612  else{
613  return calculateCoefficientsCPU(
614  to,
615  from
616  );
617  }
618 }
619 
620 inline std::vector<std::complex<double>> ChebyshevExpander::calculateCoefficients(
621  Index to,
622  Index from
623 ){
624  if(calculateCoefficientsOnGPU){
625  return calculateCoefficientsGPU(
626  to,
627  from
628  );
629  }
630  else{
631  return calculateCoefficientsCPU(
632  to,
633  from
634  );
635  }
636 }
637 
638 inline bool ChebyshevExpander::getLookupTableIsGenerated(){
639  if(generatingFunctionLookupTable != NULL)
640  return true;
641  else
642  return false;
643 }
644 
645 inline bool ChebyshevExpander::getLookupTableIsLoadedGPU(){
646  if(generatingFunctionLookupTable_device != NULL)
647  return true;
648  else
649  return false;
650 }
651 
652 inline std::vector<std::complex<double>> ChebyshevExpander::generateGreensFunction(
653  const std::vector<std::complex<double>> &coefficients,
654  Type type
655 ){
656  if(generateGreensFunctionsOnGPU){
657  return generateGreensFunctionGPU(coefficients, type);
658  }
659  else{
660  return generateGreensFunctionCPU(coefficients, type);
661  }
662 }
663 
664 inline void ChebyshevExpander::setDamping(std::complex<double> *damping){
665  this->damping = damping;
666 }
667 
668 inline void ChebyshevExpander::ensureLookupTableIsReady(){
669  if(useLookupTable){
670  if(!generatingFunctionLookupTable)
671  generateLookupTable(numCoefficients, energyResolution, lowerBound, upperBound);
672  if(generateGreensFunctionsOnGPU && !generatingFunctionLookupTable_device)
673  loadLookupTableGPU();
674  }
675  else if(generateGreensFunctionsOnGPU){
676  TBTKExit(
677  "Solver::ChebyshevSolver::ensureLookupTableIsReady()",
678  "Green's functions can only be generated on GPU using"
679  << " lookup tables.",
680  "Use Solver::ChebyshevExpander::setGenerateGreensFunctionOnGPU()"
681  << " and"
682  << " Solver::ChebyshevExpander::setUseLookupTable() to"
683  << " configure the Solver::ChebyshevExpander."
684  );
685  }
686 }
687 
688 }; //End of namespace Solver
689 }; //End of namespace TBTK
690 
691 #endif
TBTK::Solver::ChebyshevExpander::getUpperBound
double getUpperBound() const
Definition: ChebyshevExpander.h:561
TBTK::Solver::ChebyshevExpander::getUseLookupTable
bool getUseLookupTable() const
Definition: ChebyshevExpander.h:596
TBTK::Solver::ChebyshevExpander::setCalculateCoefficientsOnGPU
void setCalculateCoefficientsOnGPU(bool calculateCoefficientsOnGPU)
Definition: ChebyshevExpander.h:565
TBTK::Solver::ChebyshevExpander::generateGreensFunction
std::vector< std::complex< double > > generateGreensFunction(const std::vector< std::complex< double >> &coefficients, Type type=Type::Retarded)
Definition: ChebyshevExpander.h:652
TBTK::Solver::ChebyshevExpander::setScaleFactor
void setScaleFactor(double scaleFactor)
Definition: ChebyshevExpander.h:492
Model.h
Container of Model related information.
TBTK::Solver::ChebyshevExpander::getLowerBound
double getLowerBound() const
Definition: ChebyshevExpander.h:548
TBTK::Solver::ChebyshevExpander::Type
Type
Definition: ChebyshevExpander.h:226
TBTK::Solver::ChebyshevExpander::setBroadening
void setBroadening(double broadening)
Definition: ChebyshevExpander.h:518
TBTK::Solver::ChebyshevExpander::getGenerateGreensFunctionsOnGPU
bool getGenerateGreensFunctionsOnGPU() const
Definition: ChebyshevExpander.h:581
TBTK::Solver::ChebyshevExpander::setLowerBound
void setLowerBound(double lowerBound)
Definition: ChebyshevExpander.h:539
Solver.h
Base class for Solvers.
TBTK::Solver::ChebyshevExpander::setUseLookupTable
void setUseLookupTable(bool useLookupTable)
Definition: ChebyshevExpander.h:585
TBTK::Solver::ChebyshevExpander::setEnergyResolution
void setEnergyResolution(int energyResolution)
Definition: ChebyshevExpander.h:526
TBTK::Solver::ChebyshevExpander::getNumCoefficients
int getNumCoefficients() const
Definition: ChebyshevExpander.h:514
TBTK::Solver::ChebyshevExpander::~ChebyshevExpander
virtual ~ChebyshevExpander()
TBTK::Solver::ChebyshevExpander::getBroadening
double getBroadening() const
Definition: ChebyshevExpander.h:522
TBTK::Solver::ChebyshevExpander
Solves a Model using the Chebyshev method.
Definition: ChebyshevExpander.h:69
TBTK::Solver::ChebyshevExpander::setNumCoefficients
void setNumCoefficients(int numCoefficients)
Definition: ChebyshevExpander.h:505
TBTK::Communicator
Base class for classes that can communicate their status during execution.
Definition: Communicator.h:56
Communicator.h
Base class for classes that can communicate their status during execution.
TBTK::Solver::ChebyshevExpander::calculateCoefficients
std::vector< std::vector< std::complex< double > > > calculateCoefficients(std::vector< Index > &to, Index from)
TBTK::Solver::ChebyshevExpander::ChebyshevExpander
ChebyshevExpander()
TBTK::Solver::ChebyshevExpander::getEnergyResolution
int getEnergyResolution() const
Definition: ChebyshevExpander.h:535
TBTK::Solver::Solver
Base class for Solvers.
Definition: Solver.h:42
TBTK::Solver::ChebyshevExpander::getCalculateCoefficientsOnGPU
bool getCalculateCoefficientsOnGPU() const
Definition: ChebyshevExpander.h:571
TBTK::Index
Physical index.
Definition: Index.h:44
TBTK::Solver::ChebyshevExpander::setGenerateGreensFunctionsOnGPU
void setGenerateGreensFunctionsOnGPU(bool generateGreensFunctionsOnGPU)
Definition: ChebyshevExpander.h:575
TBTK::Solver::ChebyshevExpander::getScaleFactor
double getScaleFactor()
Definition: ChebyshevExpander.h:501
TBTK::Solver::ChebyshevExpander::setUpperBound
void setUpperBound(double upperBound)
Definition: ChebyshevExpander.h:552