Skip to content
Open
Show file tree
Hide file tree
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 Oct 13, 2026
8bfcec6
Merge verified upstream develop at 870ce158
zhangchenyu-USTC Oct 13, 2026
6238fde
Reject energy-only Makov-Payne control and test potential averaging
zhangchenyu-USTC Oct 14, 2026
671dc26
Merge upstream develop through f264b402
zhangchenyu-USTC Oct 14, 2026
e557b4f
Follow upstream DFT+U occupation initialization naming
zhangchenyu-USTC Oct 14, 2026
bf00b36
Move electronic control types and occupation checks to estate
zhangchenyu-USTC Oct 20, 2026
80403dc
Separate electronic request workflow from solver physics
zhangchenyu-USTC Oct 20, 2026
271342c
Pass electron and spin state explicitly to estate helpers
zhangchenyu-USTC Oct 22, 2026
5c51640
Reuse socket control helpers with explicit parameter views
zhangchenyu-USTC Oct 22, 2026
be19229
Remove unused exception header from PW solver
zhangchenyu-USTC Oct 25, 2026
a382da5
Merge develop at 8e201758 and adapt shared SCCS potential
zhangchenyu-USTC Oct 25, 2026
8539ebb
Merge develop and refine constant-potential electron control
zhangchenyu-USTC Nov 1, 2026
99e89e7
Merge develop and resolve CMake formatting conflicts
zhangchenyu-USTC Nov 3, 2026
c36ae05
Fix retained Python driver call and clarify socket exit validation
zhangchenyu-USTC Nov 3, 2026
977f6b8
Merge develop output controls into constant-potential socket branch
zhangchenyu-USTC Nov 5, 2026
e6b3805
Pass electronic step data without extending ESolver interfaces
zhangchenyu-USTC Nov 9, 2026
da4ebf2
Fix MPI lifecycle in socket driver unit tests
zhangchenyu-USTC Nov 9, 2026
b0e6776
Keep per-frame electron control in the first-principles solver
zhangchenyu-USTC Nov 18, 2026
9952701
Merge develop and preserve the constant-potential LCAO EXX convergenc…
zhangchenyu-USTC Nov 18, 2026
7476ceb
Exercise first-principles and ordinary solvers through the public soc…
zhangchenyu-USTC Nov 18, 2026
d48cea4
Merge develop and isolate constant-potential socket adapters
zhangchenyu-USTC Nov 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions source/Makefile.Objects
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ OBJS_DEEPKS=lcao_deepks.o\


OBJS_ELECSTAT=elecstate.o\
electronic_control.o\
electronic_occupations.o\
init_scf.o\
param_update.o\
estate_e_terms.o\
Expand Down Expand Up @@ -309,6 +311,7 @@ OBJS_ELECSTAT=elecstate.o\
efield.o\
gatefield.o\
potential_new.o\
elpot.o\
potential_types.o\
pot_surchem.o\
pot_sep.o\
Expand Down Expand Up @@ -644,8 +647,10 @@ OBJS_RELAXATION=relax_data.o\
socket_frame.o\
socket_frame_utils.o\
socket_driver.o\
socket_ks.o\
socket_utils.o\
socket_handlers.o\
socket_constant_potential.o\
cg_base.o\
bfgs_basic.o\
relax_driver.o\
Expand Down
19 changes: 17 additions & 2 deletions source/source_esolver/esolver_factory.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "esolver_factory.h"

#include "esolver.h"
#include "esolver_ks_pw.h"
#include "esolver_dfpt_pw.h"
#include "esolver_sdft_pw.h"
#include "esolver_ks_pw.h"
#include "esolver_ks_pw_tddft.h"
#include "esolver_sdft_pw.h"
#include "source_base/global_variable.h"
#include "source_base/module_device/device.h"
#include "source_hamilt/module_xc/general_exx_info.h"
#include "source_io/module_parameter/parameter.h"
#include "source_relax/socket_driver.h"
#ifdef __LCAO
#include "esolver_dm2rho.h"
#include "esolver_double_xc.h"
Expand Down Expand Up @@ -158,6 +159,8 @@ ESolver* init_esolver(const Input_para& inp)
// determine type of esolver based on INPUT information
const std::string esolver_type = determine_type(inp);
const bool gamma_only = inp.gamma_only;
const bool socket_ks =
inp.socket_driver && inp.device == "cpu" && inp.precision == "double" && (inp.nspin == 1 || inp.nspin == 2);
#ifdef __LCAO
const LRRuntime lr_runtime{GlobalV::ofs_running, GlobalV::ofs_warning, GlobalV::MY_RANK};
#endif
Expand All @@ -184,6 +187,10 @@ ESolver* init_esolver(const Input_para& inp)
}
else
{
if (socket_ks)
{
return SocketCP::create_pw_socket_solver();
}
return new ESolver_KS_PW<std::complex<double>, base_device::DEVICE_CPU>();
}
}
Expand Down Expand Up @@ -298,10 +305,18 @@ ESolver* init_esolver(const Input_para& inp)
{
if (gamma_only)
{
if (socket_ks)
{
return SocketCP::create_lcao_socket_solver(true);
}
return new ESolver_KS_LCAO<double, double>();
}
else if (inp.nspin < 4)
{
if (socket_ks)
{
return SocketCP::create_lcao_socket_solver(false);
}
return new ESolver_KS_LCAO<std::complex<double>, double>();
}
else
Expand Down
3 changes: 3 additions & 0 deletions source/source_estate/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ list(
APPEND
objects
elecstate.cpp
electronic_control.cpp
electronic_occupations.cpp
init_scf.cpp
estate_e_terms.cpp
elecstate_energy.cpp
Expand All @@ -19,6 +21,7 @@ list(
module_pot/gatefield.cpp
module_pot/efield.cpp
module_pot/h_hartree_pw.cpp
module_pot/elpot.cpp
module_pot/h_tddft_pw.cpp
module_pot/pot_xc.cpp
module_pot/pot_local.cpp
Expand Down
13 changes: 6 additions & 7 deletions source/source_estate/elecstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ const double* ElecState::getRho(int spin) const
return &(this->charge->rho[spin][0]);
}

void ElecState::init_nelec_spin() { this->init_nelec_spin(PARAM.inp.nspin, PARAM.inp.nelec, PARAM.inp.nupdown); }


void ElecState::init_nelec_spin()
void ElecState::init_nelec_spin(int nspin, double nelect, double nupdown)
{
this->nelec_spin.resize(PARAM.inp.nspin);
if (PARAM.inp.nspin == 2)
this->nelec_spin.resize(nspin);
if (nspin == 2)
{
this->nelec_spin[0] = (PARAM.inp.nelec + PARAM.inp.nupdown) / 2.0;
this->nelec_spin[1] = (PARAM.inp.nelec - PARAM.inp.nupdown) / 2.0;
this->nelec_spin[0] = (nelect + nupdown) / 2.0;
this->nelec_spin[1] = (nelect - nupdown) / 2.0;
}
}


void ElecState::init_ks(Charge* chr_in, // pointer for class Charge
const K_Vectors* klist_in,
int nk_in,
Expand Down
1 change: 1 addition & 0 deletions source/source_estate/elecstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class ElecState
// if nupdown is not 0(TWO_EFERMI case),
// nelec_spin will be fixed and weights will be constrained
void init_nelec_spin();
void init_nelec_spin(int nspin, double nelect, double nupdown);
// used to record number of electrons per spin index
// for NSPIN=2, it will record number of spin up and number of spin down
// for NSPIN=4, it will record total number, magnetization for x, y, z direction
Expand Down
2 changes: 2 additions & 0 deletions source/source_estate/elecstate_tools.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "elecstate_tools.h"

#include "occupy.h"

#include "source_base/module_parallel/para_band_output.h"
#include "source_base/module_parallel/para_bridge.h"
#include "source_base/parallel_reduce.h"
#include <cmath>

#include <vector>

Expand Down
104 changes: 104 additions & 0 deletions source/source_estate/electronic_control.cpp
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
75 changes: 75 additions & 0 deletions source/source_estate/electronic_control.h
Comment thread
mohanchen marked this conversation as resolved.
Resolved
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
68 changes: 68 additions & 0 deletions source/source_estate/electronic_occupations.cpp
Comment thread
mohanchen marked this conversation as resolved.
Resolved
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
Loading
Loading