Skip to content

Commit

Permalink
Modified mixing class
Browse files Browse the repository at this point in the history
  • Loading branch information
Avirup Sircar committed Jun 6, 2024
1 parent 8a4968b commit 114c3c8
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/ksdft/KohnShamDFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ namespace dftefe
/* Mixing related info */
const size_type mixingHistory,
const double mixingParameter,
const double adaptAndersonMixingParameter,
const bool isAdaptiveAndersonMixingParameter,
/* Electron density related info */
const quadrature::QuadratureValuesContainer<RealType, memorySpace>
&electronChargeDensityInput,
Expand Down Expand Up @@ -199,7 +199,7 @@ namespace dftefe
utils::ConditionalOStream d_rootCout;
size_type d_mixingHistory;
double d_mixingParameter;
double d_adaptAndersonMixingParameter;
bool d_isAdaptiveAndersonMixingParameter;
bool d_evaluateEnergyEverySCF;
quadrature::QuadratureValuesContainer<RealType, memorySpace>
d_densityInQuadValues, d_densityOutQuadValues,
Expand Down
6 changes: 3 additions & 3 deletions src/ksdft/KohnShamDFT.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ namespace dftefe
const bool evaluateEnergyEverySCF,
const size_type mixingHistory,
const double mixingParameter,
const double adaptAndersonMixingParameter,
const bool isAdaptiveAndersonMixingParameter,
const quadrature::QuadratureValuesContainer<RealType, memorySpace>
&electronChargeDensityInput,
std::shared_ptr<
Expand Down Expand Up @@ -207,7 +207,7 @@ namespace dftefe
const OpContext &MInvContext)
: d_mixingHistory(mixingHistory)
, d_mixingParameter(mixingParameter)
, d_adaptAndersonMixingParameter(adaptAndersonMixingParameter)
, d_isAdaptiveAndersonMixingParameter(isAdaptiveAndersonMixingParameter)
, d_feBMWaveFn(feBMWaveFn)
, d_evaluateEnergyEverySCF(evaluateEnergyEverySCF)
, d_densityInQuadValues(electronChargeDensityInput)
Expand Down Expand Up @@ -371,7 +371,7 @@ namespace dftefe
d_jxwDataHost,
true, // call MPI REDUCE while computing dot products
d_mixingParameter,
d_adaptAndersonMixingParameter);
d_isAdaptiveAndersonMixingParameter);

//
// Begin SCF iteration
Expand Down
5 changes: 3 additions & 2 deletions src/ksdft/KohnShamEigenSolver.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ namespace dftefe
linearAlgebra::EigenSolverErrorMsg::isSuccessAndMsg(err);
returnValue.msg += nrErr.msg;
}
else if (iPass == d_maxChebyshevFilterPass)
else if (iPass >= d_maxChebyshevFilterPass && chfsiErr.isSuccess &&
nrErr.isSuccess)
{
err = linearAlgebra::EigenSolverErrorCode::KS_MAX_PASS_ERROR;
returnValue =
Expand All @@ -289,7 +290,7 @@ namespace dftefe
returnValue =
linearAlgebra::EigenSolverErrorMsg::isSuccessAndMsg(err);
returnValue.msg += "Number of CHFSI passes required are " +
std::to_string(iPass) + ".";
std::to_string(iPass + 1) + ".";
}
else
{
Expand Down
6 changes: 4 additions & 2 deletions src/ksdft/MixingScheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <utils/MemorySpaceType.h>
#include <linearAlgebra/MultiVector.h>
#include <linearAlgebra/BlasLapack.h>
#include <utils/ConditionalOStream.h>

namespace dftefe
{
Expand Down Expand Up @@ -115,7 +116,7 @@ namespace dftefe
& weightDotProducts,
const bool performMPIReduce,
const double mixingValue,
const bool adaptMixingValue);
const bool isAdaptiveMixing);

/**
* @brief Adds to the input history
Expand Down Expand Up @@ -179,13 +180,14 @@ namespace dftefe
const MPI_Comm &d_mpiComm;

std::map<mixingVariable, double> d_mixingParameter;
std::map<mixingVariable, bool> d_adaptMixingParameter;
std::map<mixingVariable, bool> d_isAdaptiveMixing;
bool d_anyMixingParameterAdaptive;
bool d_adaptiveMixingParameterDecLastIteration;
bool d_adaptiveMixingParameterDecAllIterations;
bool d_adaptiveMixingParameterIncAllIterations;
size_type d_mixingHistory;
std::map<mixingVariable, bool> d_performMixing;
utils::ConditionalOStream d_rootCout;
};
} // namespace ksdft
} // end of namespace dftefe
Expand Down
25 changes: 15 additions & 10 deletions src/ksdft/MixingScheme.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ namespace dftefe
const utils::mpi::MPIComm &mpiComm)
: d_mpiComm(mpiComm)
, d_anyMixingParameterAdaptive(false)
{}
, d_rootCout(std::cout)
{
int rank;
utils::mpi::MPICommRank(mpiComm, &rank);
d_rootCout.setCondition(rank == 0);
}

template <typename ValueTypeMixingVariable, typename ValueTypeWeights>
void
Expand All @@ -41,19 +46,19 @@ namespace dftefe
& weightDotProducts,
const bool performMPIReduce,
const double mixingValue, /*param for linear mixing (a's)*/
const bool adaptMixingValue)
const bool isAdaptiveMixing)
{
d_variableHistoryIn[mixingVariableList] =
std::deque<std::vector<ValueTypeMixingVariable>>();
d_variableHistoryResidual[mixingVariableList] =
std::deque<std::vector<ValueTypeMixingVariable>>();
d_vectorDotProductWeights[mixingVariableList] = weightDotProducts;

d_performMPIReduce[mixingVariableList] = performMPIReduce;
d_mixingParameter[mixingVariableList] = mixingValue;
d_adaptMixingParameter[mixingVariableList] = adaptMixingValue;
d_performMPIReduce[mixingVariableList] = performMPIReduce;
d_mixingParameter[mixingVariableList] = mixingValue;
d_isAdaptiveMixing[mixingVariableList] = isAdaptiveMixing;
d_anyMixingParameterAdaptive =
adaptMixingValue || d_anyMixingParameterAdaptive;
isAdaptiveMixing || d_anyMixingParameterAdaptive;
d_adaptiveMixingParameterDecLastIteration = false;
d_adaptiveMixingParameterDecAllIterations = true;
d_adaptiveMixingParameterIncAllIterations = true;
Expand Down Expand Up @@ -293,13 +298,13 @@ namespace dftefe
"MPI Error:" + mpiIsSuccessAndMsg.second);

for (const auto &[key, value] : d_variableHistoryIn)
if (d_adaptMixingParameter[key])
if (d_isAdaptiveMixing[key])
{
d_mixingParameter[key] *= ci;
}
if (d_adaptMixingParameter[mixingVariable::rho])
std::cout << "Adaptive Anderson mixing parameter for Rho: "
<< d_mixingParameter[mixingVariable::rho] << std::endl;
if (d_isAdaptiveMixing[mixingVariable::rho])
d_rootCout << "Adaptive Anderson mixing parameter for Rho: "
<< d_mixingParameter[mixingVariable::rho] << "\n";
}

// Fucntions to add to the history
Expand Down
13 changes: 7 additions & 6 deletions test/ksdft/src/TestKSDFTClassical.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ int main()


double scfDensityResidualNormTolerance = 1e-3;
size_type maxSCFIter = 4;
size_type mixingHistory = 4;
size_type mixingParameter = 0.2;
size_type adaptAndersonMixingParameter = 0.5;
size_type maxSCFIter = 40;
size_type mixingHistory = 10;
double mixingParameter = 0.2;
bool isAdaptiveAndersonMixingParameter = true;
bool evaluateEnergyEverySCF = true;

// Set up Triangulation
std::shared_ptr<basis::TriangulationBase> triangulationBase =
Expand Down Expand Up @@ -414,10 +415,10 @@ std::shared_ptr<linearAlgebra::OperatorContext<double,
chebyshevPolynomialDegree,
maxChebyshevFilterPass,
maxSCFIter,
true,
evaluateEnergyEverySCF,
mixingHistory,
mixingParameter,
adaptAndersonMixingParameter,
isAdaptiveAndersonMixingParameter,
electronChargeDensity,
basisManagerTotalPot,
basisManagerWaveFn,
Expand Down

0 comments on commit 114c3c8

Please sign in to comment.