Skip to content

Commit

Permalink
Merged in publicGithubDevelopMLXCFix (pull request #642)
Browse files Browse the repository at this point in the history
MLXC bug fix

Approved-by: Phani Motamarri
  • Loading branch information
dsambit committed Dec 15, 2024
2 parents acfdd00 + 010233a commit 163b668
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 14 deletions.
7 changes: 4 additions & 3 deletions include/NNLDA.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace dftfe
NNLDA(std::string modelFilename,
const bool isSpinPolarized = false,
const excDensityPositivityCheckTypes densityPositivityCheckType =
excDensityPositivityCheckTypes::MAKE_POSITIVE,
const double rhoTol = 1.0e-8);
excDensityPositivityCheckTypes::MAKE_POSITIVE);

~NNLDA();
void
evaluateexc(const double *rho, const unsigned int numPoints, double *exc);
Expand All @@ -25,9 +25,10 @@ namespace dftfe

private:
std::string d_modelFilename;
std::string d_ptcFilename;
torch::jit::script::Module * d_model;
const bool d_isSpinPolarized;
const double d_rhoTol;
double d_rhoTol;
const excDensityPositivityCheckTypes d_densityPositivityCheckType;
};
} // namespace dftfe
Expand Down
64 changes: 60 additions & 4 deletions src/excManager/NNLDA.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,42 @@ namespace dftfe
}
};

std::string
trim(const std::string &str, const std::string &whitespace = " \t")
{
std::size_t strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return ""; // no content
std::size_t strEnd = str.find_last_not_of(whitespace);
std::size_t strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}

std::map<std::string, std::string>
getKeyValuePairs(const std::string filename, const std::string delimiter)
{
std::map<std::string, std::string> returnValue;
std::ifstream readFile;
std::string readLine;
readFile.open(filename.c_str());
utils::throwException<utils::InvalidArgument>(readFile.is_open(),
"Unable to open file " +
filename);
while (std::getline(readFile, readLine))
{
auto pos = readLine.find_last_of(delimiter);
if (pos != std::string::npos)
{
std::string key = trim(readLine.substr(0, pos));
std::string value = trim(readLine.substr(pos + 1));
returnValue[key] = value;
}
}

readFile.close();
return returnValue;
}

void
excSpinUnpolarized(
const double * rho,
Expand Down Expand Up @@ -250,15 +286,35 @@ namespace dftfe

NNLDA::NNLDA(std::string modelFilename,
const bool isSpinPolarized /*=false*/,
const excDensityPositivityCheckTypes densityPositivityCheckType,
const double rhoTol)
const excDensityPositivityCheckTypes densityPositivityCheckType)
: d_modelFilename(modelFilename)
, d_isSpinPolarized(isSpinPolarized)
, d_densityPositivityCheckType(densityPositivityCheckType)
, d_rhoTol(rhoTol)
{
std::map<std::string, std::string> modelKeyValues =
getKeyValuePairs(d_modelFilename, "=");

std::vector<std::string> keysToFind = {"PTC_FILE", "RHO_TOL"};

// check if all required keys are found
for (unsigned int i = 0; i < keysToFind.size(); ++i)
{
bool found = false;
for (auto it = modelKeyValues.begin(); it != modelKeyValues.end(); ++it)
{
if (keysToFind[i] == it->first)
found = true;
}
utils::throwException(found,
"Unable to find the key " + keysToFind[i] +
" in file " + d_modelFilename);
}

d_ptcFilename = modelKeyValues["PTC_FILE"];
d_rhoTol = std::stod(modelKeyValues["RHO_TOL"]);

d_model = new torch::jit::script::Module;
*d_model = torch::jit::load(d_modelFilename);
*d_model = torch::jit::load(d_ptcFilename);
// Explicitly load model onto CPU, you can use kGPU if you are on Linux
// and have libtorch version with CUDA support (and a GPU)
d_model->to(torch::kCPU);
Expand Down
2 changes: 1 addition & 1 deletion src/excManager/NNLLMGGA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ namespace dftfe


d_model = new torch::jit::script::Module;
*d_model = torch::jit::load(d_modelFilename);
*d_model = torch::jit::load(d_ptcFilename);
// Explicitly load model onto CPU, you can use kGPU if you are on Linux
// and have libtorch version with CUDA support (and a GPU)
d_model->to(torch::kCPU);
Expand Down
6 changes: 3 additions & 3 deletions src/excManager/excDensityGGAClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,7 @@ namespace dftfe
if (d_NNGGAPtr != nullptr)
{
std::vector<double> excValuesFromNN(nquad, 0);
const size_t numDescriptors =
this->d_densityDescriptorAttributesList.size();
const size_t numDescriptors = 5;
std::vector<double> pdexcDescriptorValuesFromNN(numDescriptors * nquad,
0);
d_NNGGAPtr->evaluatevxc(&(densityValues[0]),
Expand All @@ -228,7 +227,8 @@ namespace dftfe
&pdexcDescriptorValuesFromNN[0]);
for (size_t i = 0; i < nquad; i++)
{
exValues[i] += excValuesFromNN[i];
exValues[i] += excValuesFromNN[i] * (densityValues[2 * i + 0] +
densityValues[2 * i + 1]);
pdexDensitySpinUpValues[i] +=
pdexcDescriptorValuesFromNN[numDescriptors * i + 0];
pdexDensitySpinDownValues[i] +=
Expand Down
6 changes: 3 additions & 3 deletions src/excManager/excDensityLDAClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ namespace dftfe
if (d_NNLDAPtr != nullptr)
{
std::vector<double> excValuesFromNN(nquad, 0);
const unsigned int numDescriptors =
this->d_densityDescriptorAttributesList.size();
const unsigned int numDescriptors = 2;
std::vector<double> pdexcDescriptorValuesFromNN(numDescriptors * nquad,
0);
d_NNLDAPtr->evaluatevxc(&(densityValues[0]),
Expand All @@ -189,7 +188,8 @@ namespace dftfe
&pdexcDescriptorValuesFromNN[0]);
for (unsigned int i = 0; i < nquad; i++)
{
exValues[i] += excValuesFromNN[i];
exValues[i] += excValuesFromNN[i] * (densityValues[2 * i + 0] +
densityValues[2 * i + 1]);
pdexDensitySpinUpValues[i] +=
pdexcDescriptorValuesFromNN[numDescriptors * i + 0];
pdexDensitySpinDownValues[i] +=
Expand Down

0 comments on commit 163b668

Please sign in to comment.