Repository navigation
Add constant-potential electron control to the i-PI socket driver #8136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
0770459
8bfcec6
6238fde
671dc26
e557b4f
bf00b36
80403dc
271342c
5c51640
be19229
a382da5
8539ebb
99e89e7
c36ae05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #ifndef ABACUS_ELECTRONIC_CONTROL_H | ||
| #define ABACUS_ELECTRONIC_CONTROL_H | ||
|
|
||
| namespace elecstate | ||
| { | ||
| // Physical requests and results; no socket or serialization dependencies. | ||
| struct ElectronicControl | ||
| { | ||
| double nelect = 0.0; | ||
| }; | ||
|
|
||
| struct PotentialWindow | ||
| { | ||
| int axis = -1; | ||
| double minimum_A = 0.0; | ||
| double maximum_A = 0.0; | ||
| }; | ||
|
|
||
| struct ElectronicObservables | ||
| { | ||
| double nelect = 0.0; | ||
| double fermi_ry = 0.0; | ||
| double reference_ry = 0.0; | ||
| }; | ||
| } | ||
|
|
||
| #endif |
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest moving this file to source_estate
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved the occupation sum, MPI reduction, and electron-count check to |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #include "electronic_occupations.h" | ||
| #include "source_base/matrix.h" | ||
| #include "source_base/parallel_reduce.h" | ||
|
|
||
| #include <cmath> | ||
| #include <iomanip> | ||
| #include <sstream> | ||
| #include <stdexcept> | ||
|
|
||
| namespace elecstate | ||
| { | ||
| double checked_occupation_electron_count(const ModuleBase::matrix& weights, | ||
| double requested_nelect, | ||
| int npool, | ||
| int ranks_per_pool) | ||
| { | ||
| int invalid = !std::isfinite(requested_nelect) || requested_nelect <= 0.0 | ||
| || npool <= 0 || ranks_per_pool <= 0; | ||
| double occupied = 0.0; | ||
| double compensation = 0.0; | ||
| for (int ik = 0; ik < weights.nr; ++ik) | ||
| { | ||
| for (int ib = 0; ib < weights.nc; ++ib) | ||
| { | ||
| const double weight = weights(ik, ib); | ||
| if (!std::isfinite(weight)) | ||
| { | ||
| invalid = 1; | ||
| continue; | ||
| } | ||
| // Compensated sum of the local k/spin and band shard. Generalized | ||
| // smearing weights may be negative; only their total is constrained. | ||
| const double corrected = weight - compensation; | ||
| const double next = occupied + corrected; | ||
| compensation = (next - occupied) - corrected; | ||
| occupied = next; | ||
| } | ||
| } | ||
| if (!std::isfinite(occupied)) { invalid = 1; } | ||
| // Synchronize failures before a rank can enter a different collective path. | ||
| Parallel_Reduce::reduce_all(invalid); | ||
| if (invalid) | ||
| { | ||
| throw std::runtime_error("constant-potential electron count requires finite occupations " | ||
| "and a valid target/occupation partition"); | ||
| } | ||
|
|
||
| // Same distribution as the native band-energy sum: remove the replicated | ||
| // copies inside each pool before combining KPAR and BNDPAR contributions. | ||
| Parallel_Reduce::reduce_double_allpool(npool, ranks_per_pool, occupied); | ||
| const double tolerance = 1.0e-8; | ||
| int mismatch = !std::isfinite(occupied) || occupied <= 0.0 | ||
| || std::abs(occupied - requested_nelect) > tolerance; | ||
| Parallel_Reduce::reduce_all(mismatch); | ||
| if (mismatch) | ||
| { | ||
| std::ostringstream message; | ||
| message << std::setprecision(17) | ||
| << "constant-potential occupation electron count mismatch: requested=" << requested_nelect | ||
| << ", occupied=" << occupied << ", absolute tolerance=" << tolerance; | ||
| throw std::runtime_error(message.str()); | ||
| } | ||
| return occupied; | ||
| } | ||
| } |
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make this a free function
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The check is a free function, |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #ifndef ABACUS_ELECTRONIC_OCCUPATIONS_H | ||
| #define ABACUS_ELECTRONIC_OCCUPATIONS_H | ||
|
|
||
| namespace ModuleBase | ||
| { | ||
| class matrix; | ||
| } | ||
|
|
||
| namespace elecstate | ||
| { | ||
| // weights already include k-point weights and spin degeneracy. Rows/bands are | ||
| // partitioned across npool groups and replicated on ranks_per_pool ranks. | ||
| // All ranks must participate; invalid occupations or a mismatch fail together. | ||
| double checked_occupation_electron_count(const ModuleBase::matrix& weights, | ||
| double requested_nelect, | ||
| int npool, | ||
| int ranks_per_pool); | ||
| } | ||
|
|
||
| #endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest moving this file to source_estate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the electronic request, window, and result types to
source_estate/electronic_control.hunder theelecstatenamespace. Updated the users and build wiring in 8539ebb63a7a1ad3bdfaaa51a7e1602fb0bb99de.