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
Open
zhangchenyu-USTC
wants to merge
21
commits into
deepmodeling:develop
Choose a base branch
from
zhangchenyu-USTC:constant-potential-socket
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0770459
Add constant-potential electron control to the i-PI socket driver
zhangchenyu-USTC 8bfcec6
Merge verified upstream develop at 870ce158
zhangchenyu-USTC 6238fde
Reject energy-only Makov-Payne control and test potential averaging
zhangchenyu-USTC 671dc26
Merge upstream develop through f264b402
zhangchenyu-USTC e557b4f
Follow upstream DFT+U occupation initialization naming
zhangchenyu-USTC bf00b36
Move electronic control types and occupation checks to estate
zhangchenyu-USTC 80403dc
Separate electronic request workflow from solver physics
zhangchenyu-USTC 271342c
Pass electron and spin state explicitly to estate helpers
zhangchenyu-USTC 5c51640
Reuse socket control helpers with explicit parameter views
zhangchenyu-USTC be19229
Remove unused exception header from PW solver
zhangchenyu-USTC a382da5
Merge develop at 8e201758 and adapt shared SCCS potential
zhangchenyu-USTC 8539ebb
Merge develop and refine constant-potential electron control
zhangchenyu-USTC 99e89e7
Merge develop and resolve CMake formatting conflicts
zhangchenyu-USTC c36ae05
Fix retained Python driver call and clarify socket exit validation
zhangchenyu-USTC 977f6b8
Merge develop output controls into constant-potential socket branch
zhangchenyu-USTC e6b3805
Pass electronic step data without extending ESolver interfaces
zhangchenyu-USTC da4ebf2
Fix MPI lifecycle in socket driver unit tests
zhangchenyu-USTC b0e6776
Keep per-frame electron control in the first-principles solver
zhangchenyu-USTC 9952701
Merge develop and preserve the constant-potential LCAO EXX convergenc…
zhangchenyu-USTC 7476ceb
Exercise first-principles and ordinary solvers through the public soc…
zhangchenyu-USTC d48cea4
Merge develop and isolate constant-potential socket adapters
zhangchenyu-USTC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| #include "electronic_control.h" | ||
|
|
||
| #include "elecstate.h" | ||
| #include "electronic_occupations.h" | ||
| #include "module_pot/elpot.h" | ||
| #include "param_update.h" | ||
| #include "source_io/module_parameter/input_parameter.h" | ||
|
|
||
| #include <cmath> | ||
| #include <stdexcept> | ||
|
|
||
| namespace elecstate | ||
| { | ||
| void prepare_electronic_step( | ||
| const UnitCell &cell, | ||
| ElecState *state, | ||
| ElectronicStep &step, | ||
| const Input_para &input) | ||
| { | ||
| if (!state || !state->charge || !state->pot) | ||
| { | ||
| throw std::runtime_error("electronic state is not initialized for electron control"); | ||
| } | ||
| if (!step.parameters) | ||
| { | ||
| throw std::runtime_error("electronic request requires parameter storage"); | ||
| } | ||
| if (step.request_applied || step.result_ready) | ||
| { | ||
| throw std::runtime_error("electronic request has already been applied"); | ||
| } | ||
|
|
||
| if (step.window.axis >= 0) | ||
| { | ||
| if (!state->charge->rhopw) | ||
| { | ||
| throw std::runtime_error("electrostatic window requires the density FFT grid"); | ||
| } | ||
| potential_window_planes( | ||
| cell, *state->charge->rhopw, step.window.axis, step.window.minimum_A, step.window.maximum_A); | ||
| } | ||
|
|
||
| ParamUpdater::update_electron_count(step.control.nelect, *step.parameters); | ||
| update_electron_count(*state, step.control.nelect, input.nspin, input.nupdown, step.two_fermi); | ||
| step.request_applied = true; | ||
| } | ||
|
|
||
| void complete_electronic_step( | ||
| const UnitCell &cell, | ||
| ElecState *state, | ||
| ModulePW::PW_Basis *basis, | ||
| const surchem &solvent, | ||
| const Input_para &input, | ||
| bool converged, | ||
| ElectronicStep &step) | ||
| { | ||
| if (!step.request_applied || !converged || !state || !state->charge || !state->pot || !basis) | ||
| { | ||
| throw std::runtime_error("electronic observables require an applied request and a converged SCF state"); | ||
| } | ||
| if (step.result_ready) | ||
| { | ||
| throw std::runtime_error("electronic observables have already been collected"); | ||
| } | ||
|
|
||
| ElectronicObservables result; | ||
| const int npool = input.kpar * input.bndpar; | ||
| result.nelect = checked_occupation_electron_count(state->wg, step.control.nelect, npool, basis->poolnproc); | ||
| result.fermi_ry = electronic_chemical_potential(state->eferm, step.two_fermi); | ||
| if (step.window.axis >= 0) | ||
| { | ||
| result.reference_ry = | ||
| electrostatic_reference(cell, *basis, *state->charge, *state->pot, solvent, input, step.window); | ||
| } | ||
| step.result = result; | ||
| step.result_ready = true; | ||
| } | ||
|
|
||
| void update_electron_count( | ||
| ElecState &state, | ||
| double nelect, | ||
| int nspin, | ||
| double nupdown, | ||
| bool two_fermi) | ||
| { | ||
| state.eferm.two_efermi = two_fermi; | ||
| state.init_nelec_spin(nspin, nelect, nupdown); | ||
| } | ||
|
|
||
| double electronic_chemical_potential(const Efermi &fermi, bool two_fermi) | ||
| { | ||
| double chemical_potential = fermi.ef; | ||
| if (two_fermi) | ||
| { | ||
| // Total-N derivative at fixed N_up-N_down. | ||
| chemical_potential = 0.5 * (fermi.ef_up + fermi.ef_dw); | ||
| } | ||
| if (!std::isfinite(chemical_potential)) | ||
| { | ||
| throw std::runtime_error("non-finite SCF chemical potential"); | ||
| } | ||
| return chemical_potential; | ||
| } | ||
| } // namespace elecstate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| #ifndef ABACUS_ELECTRONIC_CONTROL_H | ||
| #define ABACUS_ELECTRONIC_CONTROL_H | ||
|
|
||
| class Parameter; | ||
| class UnitCell; | ||
| class surchem; | ||
| struct Input_para; | ||
| namespace ModulePW | ||
| { | ||
| class PW_Basis; | ||
| } | ||
|
|
||
| namespace elecstate | ||
| { | ||
| class ElecState; | ||
| struct Efermi; | ||
|
|
||
| // 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; | ||
| }; | ||
|
|
||
| // Owned by the caller for one evaluation; all referenced objects outlive it. | ||
| // Each MPI rank has its own step. Only a completed result may be published. | ||
| struct ElectronicStep | ||
| { | ||
| Parameter *parameters = nullptr; | ||
| ElectronicControl control; | ||
| PotentialWindow window; | ||
| bool two_fermi = false; | ||
| ElectronicObservables result; | ||
| bool request_applied = false; | ||
| bool result_ready = false; | ||
| }; | ||
|
|
||
| void prepare_electronic_step( | ||
| const UnitCell &cell, | ||
| ElecState *state, | ||
| ElectronicStep &step, | ||
| const Input_para &input); | ||
| // Call after nuclear forces: constructing the reference updates electric-field state. | ||
| void complete_electronic_step( | ||
| const UnitCell &cell, | ||
| ElecState *state, | ||
| ModulePW::PW_Basis *basis, | ||
| const surchem &solvent, | ||
| const Input_para &input, | ||
| bool converged, | ||
| ElectronicStep &step); | ||
|
|
||
| void update_electron_count( | ||
| ElecState &state, | ||
| double nelect, | ||
| int nspin, | ||
| double nupdown, | ||
| bool two_fermi); | ||
| double electronic_chemical_potential(const Efermi &fermi, bool two_fermi); | ||
| } // namespace elecstate | ||
|
|
||
| #endif |
|
mohanchen marked this conversation as resolved.
Resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #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; | ||
| } | ||
| } // namespace elecstate |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.