53#include "./base/base_uses.f90"
59 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'qs_electrons_charge_mixing'
80 REAL(kind=
dp),
INTENT(OUT) :: lambda
81 INTEGER,
INTENT(OUT),
OPTIONAL :: n_lo, n_hi
83 INTEGER :: my_n_hi, my_n_lo, span
85 span = max(1, electrons%charge_mixing_span)
86 my_n_lo = span*floor(electrons%q/real(span, kind=
dp))
87 my_n_hi = my_n_lo + span
88 IF (electrons%lambda_is_fixed)
THEN
93 lambda = electrons%lambda_fixed
95 lambda = (electrons%q - real(my_n_lo, kind=
dp))/real(span, kind=
dp)
98 IF (
PRESENT(n_lo)) n_lo = my_n_lo
99 IF (
PRESENT(n_hi)) n_hi = my_n_hi
125 REAL(kind=
dp),
INTENT(IN) :: lambda
129 NULLIFY (force_env_section, mixed_section)
130 CALL force_env_get(force_env, force_env_section=force_env_section)
156 fermi_level_eV, workfunction_eV, lambda_in)
158 INTEGER,
INTENT(IN) :: axis
159 REAL(kind=
dp),
INTENT(IN) :: wf_min, wf_max, q
160 INTEGER,
INTENT(IN) :: span
161 REAL(kind=
dp),
INTENT(OUT) :: fermi_level_ev, workfunction_ev
162 REAL(kind=
dp),
INTENT(IN),
OPTIONAL :: lambda_in
165 REAL(kind=
dp) :: fermi_hi, fermi_lo, lambda, wf_hi, wf_lo
172 cpassert(
ASSOCIATED(force_env%sub_force_env))
173 cpassert(
SIZE(force_env%sub_force_env) == 2)
175 IF (
PRESENT(lambda_in))
THEN
178 n_lo = span*floor(q/real(span, kind=
dp))
179 lambda = (q - real(n_lo, kind=
dp))/real(span, kind=
dp)
205 IF (
ASSOCIATED(force_env%sub_force_env(1)%force_env))
THEN
208 IF (.NOT. force_env%sub_force_env(1)%force_env%para_env%is_source())
THEN
213 IF (
ASSOCIATED(force_env%sub_force_env(2)%force_env))
THEN
216 IF (.NOT. force_env%sub_force_env(2)%force_env%para_env%is_source())
THEN
222 CALL force_env%para_env%sum(fermi_lo)
223 CALL force_env%para_env%sum(wf_lo)
224 CALL force_env%para_env%sum(fermi_hi)
225 CALL force_env%para_env%sum(wf_hi)
227 fermi_level_ev = (1.0_dp - lambda)*fermi_lo + lambda*fermi_hi
228 workfunction_ev = (1.0_dp - lambda)*wf_lo + lambda*wf_hi
247 INTEGER :: n_hi, n_lo
249 REAL(kind=
dp) :: lambda_unused
254 IF (electrons%lambda_is_fixed)
RETURN
261 moved = (n_lo /= electrons%charge_mixing_current_n_lo) .OR. &
262 (n_hi /= electrons%charge_mixing_current_n_hi)
264 IF (n_lo /= electrons%charge_mixing_current_n_lo)
THEN
265 IF (electrons%charge_mixing_inplace)
THEN
266 CALL charge_mixing_set_endpoint_charge(force_env, 1, n_lo, electrons)
268 CALL rebuild_charge_mixing_endpoint(force_env, 1, n_lo, electrons)
270 electrons%charge_mixing_current_n_lo = n_lo
272 IF (n_hi /= electrons%charge_mixing_current_n_hi)
THEN
273 IF (electrons%charge_mixing_inplace)
THEN
274 CALL charge_mixing_set_endpoint_charge(force_env, 2, n_hi, electrons)
276 CALL rebuild_charge_mixing_endpoint(force_env, 2, n_hi, electrons)
278 electrons%charge_mixing_current_n_hi = n_hi
283 CALL charge_mixing_verify_endpoints(force_env, electrons, moved)
315 INTEGER,
DIMENSION(2) :: n_have
317 IF (.NOT. electrons%charge_mixing_enabled)
RETURN
319 CALL charge_mixing_read_endpoint_counts(force_env, n_have)
320 electrons%charge_mixing_current_n_lo = n_have(1)
321 electrons%charge_mixing_current_n_hi = n_have(2)
338 SUBROUTINE charge_mixing_read_endpoint_counts(force_env, n_have)
340 INTEGER,
DIMENSION(2),
INTENT(OUT) :: n_have
347 IF (.NOT.
ASSOCIATED(force_env%sub_force_env))
RETURN
350 NULLIFY (sub, subsys)
351 IF (
idx >
SIZE(force_env%sub_force_env)) cycle
352 sub => force_env%sub_force_env(
idx)%force_env
353 IF (.NOT.
ASSOCIATED(sub)) cycle
354 IF (.NOT.
ASSOCIATED(sub%qs_env)) cycle
356 IF (.NOT.
ASSOCIATED(subsys)) cycle
360 CALL force_env%para_env%max(n_have)
362 END SUBROUTINE charge_mixing_read_endpoint_counts
379 SUBROUTINE charge_mixing_verify_endpoints(force_env, electrons, moved)
382 LOGICAL,
INTENT(IN) :: moved
384 CHARACTER(LEN=256) :: msg
386 INTEGER,
DIMENSION(2) :: n_have
388 CALL charge_mixing_read_endpoint_counts(force_env, n_have)
390 IF (n_have(1) /= electrons%charge_mixing_current_n_lo .OR. &
391 n_have(2) /= electrons%charge_mixing_current_n_hi)
THEN
392 WRITE (msg,
'(A,I0,A,I0,A,I0,A,I0,A)') &
393 "CHARGE MIXING: endpoints disagree with the requested bracket. Wanted (", &
394 electrons%charge_mixing_current_n_lo,
",", electrons%charge_mixing_current_n_hi, &
395 ") but the FORCE_EVALs hold (", n_have(1),
",", n_have(2),
")."
404 IF (n_have(2) - n_have(1) /= max(1, electrons%charge_mixing_span))
THEN
405 WRITE (msg,
'(A,I0,A,I0,A,I0,A)') &
406 "CHARGE MIXING: endpoint separation is ", n_have(2) - n_have(1), &
407 " but CHARGE_MIXING_SPAN is ", max(1, electrons%charge_mixing_span), &
408 ". The two FORCE_EVALs no longer bracket q."
415 WRITE (iw,
'(T2,A,I0,A,I0,A,F14.8)') &
416 "CHARGE MIXING| bracket now [", n_have(1),
",", n_have(2),
"] q = ", electrons%q
420 END SUBROUTINE charge_mixing_verify_endpoints
433 SUBROUTINE rebuild_charge_mixing_endpoint(force_env, idx, new_n_electron, electrons)
435 INTEGER,
INTENT(IN) ::
idx, new_n_electron
438 INTEGER :: my_group, new_charge
439 LOGICAL :: subsys_explicit
447 mixed_subsys_section, root_section, &
450 NULLIFY (new_sub, old_sub, dft_section, force_env_section, subsys_section, &
451 para_env, globenv, root_section, new_qs_env, old_subsys, sub_logger, &
452 mixed_subsys_section)
454 old_sub => force_env%sub_force_env(
idx)%force_env
455 IF (.NOT.
ASSOCIATED(old_sub))
RETURN
488 IF (
ASSOCIATED(force_env%mixed_env))
THEN
489 IF (
ASSOCIATED(force_env%mixed_env%sub_logger) .AND. &
490 ASSOCIATED(force_env%mixed_env%group_distribution))
THEN
491 my_group = force_env%mixed_env%group_distribution(force_env%para_env%mepos)
492 sub_logger => force_env%mixed_env%sub_logger(my_group + 1)%p
497 new_charge = nint(electrons%neutral_electrons) - new_n_electron
499 para_env => old_sub%para_env
500 globenv => old_sub%globenv
501 root_section => old_sub%root_section
502 force_env_section => old_sub%force_env_section
530 IF (.NOT. subsys_explicit)
THEN
531 IF (
ASSOCIATED(force_env%force_env_section))
THEN
534 IF (subsys_explicit) subsys_section => mixed_subsys_section
538 ALLOCATE (new_qs_env)
543 CALL qs_init(new_qs_env, para_env, root_section=root_section, globenv=globenv, &
544 cp_subsys=old_subsys, force_env_section=force_env_section, &
545 subsys_section=subsys_section, use_motion_section=.false.)
562 CALL force_env_create(new_sub, root_section=root_section, para_env=para_env, &
563 globenv=globenv, qs_env=new_qs_env, force_env_section=force_env_section)
593 force_env%sub_force_env(
idx)%force_env => new_sub
597 END SUBROUTINE rebuild_charge_mixing_endpoint
643 SUBROUTINE charge_mixing_set_endpoint_charge(force_env, idx, new_n_electron, electrons)
645 INTEGER,
INTENT(IN) ::
idx, new_n_electron
648 CHARACTER(LEN=256) :: msg
649 INTEGER :: input_multiplicity, ispin, multiplicity, &
651 INTEGER,
DIMENSION(2) :: nelectron_spin
652 REAL(kind=
dp) :: maxocc
660 NULLIFY (sub, dft_control, mos, subsys, scf_control, dft_section, sub_section)
662 sub => force_env%sub_force_env(
idx)%force_env
663 IF (.NOT.
ASSOCIATED(sub))
RETURN
664 IF (.NOT.
ASSOCIATED(sub%qs_env))
RETURN
666 new_charge = nint(electrons%neutral_electrons) - new_n_electron
668 CALL get_qs_env(sub%qs_env, dft_control=dft_control, mos=mos, subsys=subsys, &
669 scf_control=scf_control)
671 IF (
ASSOCIATED(scf_control))
THEN
672 IF (scf_control%use_ot)
THEN
673 msg =
"CHARGE_MIXING_INPLACE cannot change the electron count under OT; "// &
674 "use CHARGE_MIXING_INPLACE F"
679 IF (dft_control%nspins == 1)
THEN
680 multiplicity = dft_control%multiplicity
681 nelectron_spin(1) = new_n_electron
682 nelectron_spin(2) = 0
687 multiplicity = input_multiplicity
688 IF (multiplicity == 0)
THEN
689 IF (
modulo(new_n_electron, 2) == 0)
THEN
695 IF (
modulo(new_n_electron + multiplicity - 1, 2) /= 0)
THEN
696 WRITE (msg,
'(A,I0,A,I0,A)')
"CHARGE MIXING: MULTIPLICITY ", multiplicity, &
697 " is incompatible with ", new_n_electron, &
698 " electrons. Use MULTIPLICITY 0 (automatic) or an even CHARGE_MIXING_SPAN."
701 nelectron_spin(1) = (new_n_electron + multiplicity - 1)/2
702 nelectron_spin(2) = (new_n_electron - multiplicity + 1)/2
703 IF (nelectron_spin(2) < 0)
THEN
704 cpabort(
"CHARGE MIXING: too few electrons for this MULTIPLICITY")
711 IF (
ASSOCIATED(mos))
THEN
712 IF (
SIZE(mos) /= dft_control%nspins)
THEN
713 msg =
"CHARGE MIXING: mo_set count differs from nspins"
716 DO ispin = 1,
SIZE(mos)
717 CALL get_mo_set(mos(ispin), nmo=nmo, maxocc=maxocc)
718 IF (real(nelectron_spin(ispin), kind=
dp) > maxocc*real(nmo, kind=
dp) + 1.0e-8_dp)
THEN
719 WRITE (msg,
'(A,I0,A,I0,A,I0,A)')
"CHARGE MIXING: endpoint at ", new_n_electron, &
720 " electrons needs more orbitals in spin ", ispin,
" than the ", nmo, &
721 " allocated. Increase ADDED_MOS or use CHARGE_MIXING_INPLACE F."
727 dft_control%charge = new_charge
728 dft_control%multiplicity = multiplicity
731 nelectron_spin=nelectron_spin)
733 IF (
ASSOCIATED(mos))
THEN
734 DO ispin = 1,
SIZE(mos)
735 CALL set_mo_set(mos(ispin), nelectron=nelectron_spin(ispin), &
736 n_el_f=real(nelectron_spin(ispin), kind=
dp))
740 END SUBROUTINE charge_mixing_set_endpoint_charge
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
static GRID_HOST_DEVICE int idx(const orbital a)
Return coset index of given orbital angular momentum.
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
various routines to log and control the output. The idea is that decisions about where to log should ...
subroutine, public cp_rm_default_logger()
the cousin of cp_add_default_logger, decrements the stack, so that the default logger is what it has ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
subroutine, public cp_add_default_logger(logger)
adds a default logger. MUST be called before logging occours
types that represent a subsys, i.e. a part of the system
Interface for the force calculations.
subroutine, public force_env_create(force_env, root_section, para_env, globenv, fist_env, qs_env, meta_env, sub_force_env, qmmm_env, qmmmx_env, eip_env, pwdft_env, force_env_section, mixed_env, embed_env, nnp_env, ipi_env)
creates and initializes a force environment
Interface for the force calculations.
recursive subroutine, public force_env_get(force_env, in_use, fist_env, qs_env, meta_env, fp_env, subsys, para_env, potential_energy, additional_potential, kinetic_energy, harmonic_shell, kinetic_shell, cell, sub_force_env, qmmm_env, qmmmx_env, eip_env, pwdft_env, globenv, input, force_env_section, method_name_id, root_section, mixed_env, nnp_env, embed_env, ipi_env)
returns various attributes about the force environment
recursive subroutine, public force_env_release(force_env)
releases the given force env
Define type storing the global information of a run. Keep the amount of stored data small....
Defines the basic variable types.
integer, parameter, public dp
Interface to the message passing library MPI.
Dual integer-electron-endpoint charge mixing for constant-potential MD, built on top of CP2K's stock ...
subroutine, public push_lambda_to_mixed_env(force_env, lambda)
Push a dynamically computed lambda into the MIXED force_eval's input structure. force_env_methods....
subroutine, public charge_mixing_maybe_rebuild(force_env, electrons)
Check whether q(t) has drifted outside the currently-built [N_lo,N_hi] bracket and,...
subroutine, public qs_electrons_charge_mixing_lambda(electrons, lambda, n_lo, n_hi)
Given the current electronic coordinate q and the integer span S, compute the bracketing integers N_l...
subroutine, public charge_mixing_sync_endpoint_state(force_env, electrons)
Record which integer electron counts the two FORCE_EVAL blocks are ACTUALLY built for,...
subroutine, public compute_mixed_workfunction(force_env, axis, wf_min, wf_max, q, span, fermi_level_ev, workfunction_ev, lambda_in)
Fermi level / workfunction mixing across the two integer-charge endpoints. Stock MIXED only combines ...
State of the fictitious electronic degree of freedom (electron number q, its momentum,...
Work function and Fermi level readback from the converged SCF, using the planar average of the Hartre...
subroutine, public qs_electrons_compute_workfunction(force_env, axis, minimum_a, maximum_a, fermi_level_ev, workfunction_ev)
Compute the work function (vacuum Hartree level minus Fermi level, in eV) from the planar average of ...
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
subroutine, public qs_env_create(qs_env, globenv)
allocates and intitializes a qs_env
subroutine, public qs_init(qs_env, para_env, root_section, globenv, cp_subsys, kpoint_env, qmmm, qmmm_env_qm, force_env_section, subsys_section, use_motion_section, silent, multip, charge)
Read the input and the database files for the setup of the QUICKSTEP environment.
Definition and initialisation of the mo data type.
subroutine, public set_mo_set(mo_set, maxocc, homo, lfomo, nao, nelectron, n_el_f, nmo, eigenvalues, occupation_numbers, uniform_occupation, kts, mu, flexible_electron_count)
Set the components of a MO set data structure.
subroutine, public get_mo_set(mo_set, maxocc, homo, lfomo, nao, nelectron, n_el_f, nmo, eigenvalues, occupation_numbers, mo_coeff, mo_coeff_b, uniform_occupation, kts, mu, flexible_electron_count, cmo_coeff)
Get the components of a MO set data structure.
types that represent a quickstep subsys
subroutine, public qs_subsys_get(subsys, atomic_kinds, atomic_kind_set, particles, particle_set, local_particles, molecules, molecule_set, molecule_kinds, molecule_kind_set, local_molecules, para_env, colvar_p, shell_particles, core_particles, gci, multipoles, natom, nparticle, ncore, nshell, nkind, atprop, virial, results, cell, cell_ref, use_ref_cell, energy, force, qs_kind_set, cp_subsys, nelectron_total, nelectron_spin)
...
subroutine, public qs_subsys_set(subsys, cp_subsys, local_particles, local_molecules, cell, cell_ref, use_ref_cell, energy, force, qs_kind_set, nelectron_total, nelectron_spin)
...
parameters that control an scf iteration
type of a logger, at the moment it contains just a print level starting at which level it should be l...
represents a system: atoms, molecules, their pos,vel,...
wrapper to abstract the force evaluation of the various methods
contains the initially parsed file and the initial parallel environment
stores all the informations relevant to an mpi environment