Skip to content

Commit

Permalink
added reinitBounds in kohnshameigensolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Avirup Sircar committed Jun 8, 2024
1 parent b509805 commit 114b143
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/ksdft/KohnShamDFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ namespace dftefe
d_feBMWaveFn;
std::vector<RealType> d_kohnShamEnergies;
linearAlgebra::MultiVector<ValueType, memorySpace>
d_kohnShamWaveFunctions;
* d_kohnShamWaveFunctions;
utils::ConditionalOStream d_rootCout;
size_type d_mixingHistory;
double d_mixingParameter;
Expand Down
25 changes: 15 additions & 10 deletions src/ksdft/KohnShamDFT.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,6 @@ namespace dftefe
, d_mixingScheme(d_mpiCommDomain)
, d_numWantedEigenvalues(numWantedEigenvalues)
, d_linAlgOpContext(linAlgOpContext)
, d_kohnShamWaveFunctions(feBMWaveFn->getMPIPatternP2P(),
linAlgOpContext,
d_numWantedEigenvalues)
, d_kohnShamEnergies(numWantedEigenvalues, 0.0)
, d_waveFunctionBatchSize(waveFunctionBatchSize)
, d_SCFTol(scfDensityResidualNormTolerance)
Expand All @@ -225,6 +222,7 @@ namespace dftefe
numWantedEigenvalues,
0.0,
1.0)
, d_kohnShamWaveFunctions(&d_waveFunctionSubspaceGuess)
, d_lanczosGuess(feBMWaveFn->getMPIPatternP2P(),
linAlgOpContext,
0.0,
Expand Down Expand Up @@ -424,9 +422,6 @@ namespace dftefe
, d_mixingScheme(d_mpiCommDomain)
, d_numWantedEigenvalues(numWantedEigenvalues)
, d_linAlgOpContext(linAlgOpContext)
, d_kohnShamWaveFunctions(feBMWaveFn->getMPIPatternP2P(),
linAlgOpContext,
d_numWantedEigenvalues)
, d_kohnShamEnergies(numWantedEigenvalues, 0.0)
, d_waveFunctionBatchSize(waveFunctionBatchSize)
, d_SCFTol(scfDensityResidualNormTolerance)
Expand All @@ -436,6 +431,7 @@ namespace dftefe
numWantedEigenvalues,
0.0,
1.0)
, d_kohnShamWaveFunctions(&d_waveFunctionSubspaceGuess)
, d_lanczosGuess(feBMWaveFn->getMPIPatternP2P(),
linAlgOpContext,
0.0,
Expand Down Expand Up @@ -657,11 +653,20 @@ namespace dftefe
hamiltonianComponentsVec);
}

// reinit the chfsi bounds
if (scfIter > 0)
{
d_ksEigSolve->reinitBounds(
d_kohnShamEnergies[0],
d_numElectrons == 1 ? d_kohnShamEnergies[d_numElectrons] :
d_kohnShamEnergies[d_numElectrons - 1]);
}

// Linear Eigen Solve
linearAlgebra::EigenSolverError err =
d_ksEigSolve->solve(*d_hamitonianOperator,
d_kohnShamEnergies,
d_kohnShamWaveFunctions,
*d_kohnShamWaveFunctions,
true,
*d_MContext,
*d_MInvContext);
Expand All @@ -677,7 +682,7 @@ namespace dftefe

// compute output rho
d_densCalc->computeRho(d_occupation,
d_kohnShamWaveFunctions,
*d_kohnShamWaveFunctions,
d_densityOutQuadValues);

RealType totalDensityInQuad =
Expand All @@ -698,7 +703,7 @@ namespace dftefe
d_hamitonianElec->reinitField(d_densityOutQuadValues);
d_hamitonianKin->evalEnergy(d_occupation,
*d_feBMWaveFn,
d_kohnShamWaveFunctions,
*d_kohnShamWaveFunctions,
d_waveFunctionBatchSize);
RealType kinEnergy = d_hamitonianKin->getEnergy();
d_rootCout << "Kinetic energy: " << kinEnergy << "\n";
Expand Down Expand Up @@ -735,7 +740,7 @@ namespace dftefe
d_hamitonianElec->reinitField(d_densityOutQuadValues);
d_hamitonianKin->evalEnergy(d_occupation,
*d_feBMWaveFn,
d_kohnShamWaveFunctions,
*d_kohnShamWaveFunctions,
d_waveFunctionBatchSize);
RealType kinEnergy = d_hamitonianKin->getEnergy();
d_rootCout << "Kinetic energy: " << kinEnergy << "\n";
Expand Down
32 changes: 21 additions & 11 deletions src/ksdft/KohnShamEigenSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <linearAlgebra/OperatorContext.h>
#include <linearAlgebra/HermitianIterativeEigenSolver.h>
#include <memory>
#include <utils/ConditionalOStream.h>

namespace dftefe
{
Expand Down Expand Up @@ -111,17 +112,22 @@ namespace dftefe
~KohnShamEigenSolver() = default;

void
reinit(linearAlgebra::MultiVector<ValueTypeOperand, memorySpace>
&waveFunctionSubspaceGuess,
linearAlgebra::Vector<ValueTypeOperand, memorySpace> &lanczosGuess,
const OpContext & MLanczos =
linearAlgebra::IdentityOperatorContext<ValueTypeOperator,
ValueTypeOperand,
memorySpace>(),
const OpContext &MInvLanczos =
linearAlgebra::IdentityOperatorContext<ValueTypeOperator,
ValueTypeOperand,
memorySpace>());
reinitBasis(
linearAlgebra::MultiVector<ValueTypeOperand, memorySpace>
&waveFunctionSubspaceGuess,
linearAlgebra::Vector<ValueTypeOperand, memorySpace> &lanczosGuess,
const OpContext & MLanczos =
linearAlgebra::IdentityOperatorContext<ValueTypeOperator,
ValueTypeOperand,
memorySpace>(),
const OpContext &MInvLanczos =
linearAlgebra::IdentityOperatorContext<ValueTypeOperator,
ValueTypeOperand,
memorySpace>());

void
reinitBounds(double wantedSpectrumLowerBound,
double wantedSpectrumUpperBound);

RealType
getFermiEnergy();
Expand Down Expand Up @@ -162,6 +168,10 @@ namespace dftefe
RealType d_fermiEnergy;
bool d_isSolved;
const size_type d_numElectrons;
utils::ConditionalOStream d_rootCout;
double d_wantedSpectrumLowerBound;
double d_wantedSpectrumUpperBound;
bool d_isBoundKnown;

}; // end of class KohnShamEigenSolver
} // namespace ksdft
Expand Down
67 changes: 50 additions & 17 deletions src/ksdft/KohnShamEigenSolver.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,50 @@ namespace dftefe
, d_smearingTemperature(smearingTemperature)
, d_fracOccupancy(d_numWantedEigenvalues)
, d_numElectrons(numElectrons)
, d_rootCout(std::cout)
{
reinit(waveFunctionSubspaceGuess, lanczosGuess, MLanczos, MInvLanczos);
reinitBasis(waveFunctionSubspaceGuess,
lanczosGuess,
MLanczos,
MInvLanczos);
}

template <typename ValueTypeOperator,
typename ValueTypeOperand,
utils::MemorySpace memorySpace>
void
KohnShamEigenSolver<ValueTypeOperator, ValueTypeOperand, memorySpace>::
reinit(linearAlgebra::MultiVector<ValueTypeOperand, memorySpace>
&waveFunctionSubspaceGuess,
linearAlgebra::Vector<ValueTypeOperand, memorySpace> &lanczosGuess,
const OpContext & MLanczos,
const OpContext & MInvLanczos)
reinitBasis(
linearAlgebra::MultiVector<ValueTypeOperand, memorySpace>
&waveFunctionSubspaceGuess,
linearAlgebra::Vector<ValueTypeOperand, memorySpace> &lanczosGuess,
const OpContext & MLanczos,
const OpContext & MInvLanczos)
{
d_isSolved = false;
d_isBoundKnown = false;
d_waveFunctionSubspaceGuess = &waveFunctionSubspaceGuess;
d_lanczosGuess = &lanczosGuess;
d_MLanczos = &MLanczos;
d_MInvLanczos = &MInvLanczos;
int rank;
utils::mpi::MPICommRank(
lanczosGuess.getMPIPatternP2P()->mpiCommunicator(), &rank);
d_rootCout.setCondition(rank == 0);
}

template <typename ValueTypeOperator,
typename ValueTypeOperand,
utils::MemorySpace memorySpace>
void
KohnShamEigenSolver<ValueTypeOperator, ValueTypeOperand, memorySpace>::
reinitBounds(double wantedSpectrumLowerBound,
double wantedSpectrumUpperBound)
{
d_isSolved = false;
d_isBoundKnown = true;
d_wantedSpectrumLowerBound = wantedSpectrumLowerBound;
d_wantedSpectrumUpperBound = wantedSpectrumUpperBound;
}

template <typename ValueTypeOperator,
Expand All @@ -98,8 +122,6 @@ namespace dftefe
global_size_type globalSize = kohnShamWaveFunctions.globalSize();
std::shared_ptr<linearAlgebra::LinAlgOpContext<memorySpace>>
linAlgOpContext = kohnShamWaveFunctions.getLinAlgOpContext();
std::shared_ptr<const utils::mpi::MPIPatternP2P<memorySpace>>
mpiPatternP2P = kohnShamWaveFunctions.getMPIPatternP2P();
std::vector<double> eigenSolveResidual;
utils::MemoryTransfer<memorySpace, utils::MemorySpace::HOST>
memoryTransfer;
Expand Down Expand Up @@ -142,16 +164,27 @@ namespace dftefe
if (lanczosErr.isSuccess)
{
//--------------------CHANGE THIS ------------------------------
double wantedSpectrumUpperBound = 0;
// (eigenValuesLanczos[1] - eigenValuesLanczos[0]) *
// ((double)(d_numWantedEigenvalues) / globalSize) +
// eigenValuesLanczos[0];
if (!d_isBoundKnown)
{
d_wantedSpectrumLowerBound = eigenValuesLanczos[0];
d_wantedSpectrumUpperBound = 0;
// (eigenValuesLanczos[1] - eigenValuesLanczos[0]) *
// ((double)(d_numWantedEigenvalues) / globalSize) +
// eigenValuesLanczos[0];
}

d_rootCout << "wantedSpectrumLowerBound: "
<< d_wantedSpectrumLowerBound << "\n";
d_rootCout << "wantedSpectrumUpperBound: "
<< d_wantedSpectrumUpperBound << "\n";
d_rootCout << "unWantedSpectrumUpperBound: "
<< eigenValuesLanczos[1] + residual << "\n";

linearAlgebra::ChebyshevFilteredEigenSolver<ValueTypeOperator,
ValueTypeOperand,
memorySpace>
chfsi(eigenValuesLanczos[0],
wantedSpectrumUpperBound,
chfsi(d_wantedSpectrumLowerBound,
d_wantedSpectrumUpperBound,
eigenValuesLanczos[1] + residual,
d_chebyshevPolynomialDegree,
ksdft::LinearEigenSolverDefaults::ILL_COND_TOL,
Expand Down Expand Up @@ -251,9 +284,9 @@ namespace dftefe
break;
else
{
*d_waveFunctionSubspaceGuess = kohnShamWaveFunctions;
chfsi.reinit(eigenValuesLanczos[0],
wantedSpectrumUpperBound,
d_waveFunctionSubspaceGuess = &kohnShamWaveFunctions;
chfsi.reinit(d_wantedSpectrumLowerBound,
d_wantedSpectrumUpperBound,
eigenValuesLanczos[1] + residual,
d_chebyshevPolynomialDegree,
ksdft::LinearEigenSolverDefaults::ILL_COND_TOL,
Expand Down
2 changes: 1 addition & 1 deletion test/ksdft/src/TestKSAllElectronEigenSolveOrthoEFE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ int main()
double xmax = 24.0;
double ymax = 24.0;
double zmax = 24.0;
double rc = 0.5;
double rc = 0.6;
double hMin = 1e6;
size_type maxIter = 2e7;
double absoluteTol = 1e-10;
Expand Down

0 comments on commit 114b143

Please sign in to comment.