(git:f2099e5)
Loading...
Searching...
No Matches
fde_main.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7MODULE fde_main
9 cite_reference
12 USE cp_files, ONLY: join_paths
16 USE fde_methods, ONLY: fde_build_v_emb,&
19 fde_spawn,&
20 fde_v_es,&
22 USE kinds, ONLY: default_path_length,&
23 dp
25 USE pw_env_types, ONLY: pw_env_get,&
27 USE pw_methods, ONLY: pw_axpy,&
30 pw_scale,&
33 USE pw_types, ONLY: pw_r3d_rs_type
38#include "./base/base_uses.f90"
39
40 IMPLICIT NONE
41 PRIVATE
42
43 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'fde_main'
44
45 PUBLIC :: fde_run
46
47CONTAINS
48
49! **************************************************************************************************
50!> \brief Run the FDE self-consistency loop
51!> \param qs_env ..
52! **************************************************************************************************
53 SUBROUTINE fde_run(qs_env)
54 TYPE(qs_environment_type), POINTER :: qs_env
55
56 CHARACTER(len=*), PARAMETER :: routinen = 'fde_run'
57
58 CHARACTER(len=default_path_length) :: grid_path, input, res_path, rho_path, &
59 v_path
60 INTEGER :: handle, i_a, iounit, iter, max_iter, n_a
61 LOGICAL :: converged
62 REAL(kind=dp) :: drho, e_emb, e_env, e_es_e, e_es_int, &
63 e_es_n, e_kin_na, e_n, e_sub, &
64 e_sub_bare, e_tot, e_xc_na, n_el, &
65 n_el_pw, phi_at_atom
66 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: z
67 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: r
68 TYPE(cp_logger_type), POINTER :: logger
69 TYPE(dft_control_type), POINTER :: dft_control
70 TYPE(fde_control_type), POINTER :: fde_control
71 TYPE(mp_para_env_type), POINTER :: para_env
72 TYPE(pw_env_type), POINTER :: pw_env
73 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
74 TYPE(pw_r3d_rs_type) :: rho, rho_new, v_emb
75 TYPE(pw_r3d_rs_type), POINTER :: phi_es_ptr
76 TYPE(pw_r3d_rs_type), TARGET :: phi_es
77 TYPE(qs_energy_type), POINTER :: qs_energy
78
79 CALL timeset(routinen, handle)
80
81 CALL cite_reference(schreder2024_3)
82
83 logger => cp_get_default_logger()
84 iounit = cp_logger_get_default_io_unit(logger)
85
86 NULLIFY (dft_control, fde_control, para_env, pw_env, auxbas_pw_pool, qs_energy)
87 CALL get_qs_env(qs_env, dft_control=dft_control, para_env=para_env, pw_env=pw_env, &
88 energy=qs_energy)
89 fde_control => dft_control%fde_control
90 cpassert(ASSOCIATED(fde_control))
91
92 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
93
94 ! socket files
95 v_path = join_paths(fde_control%workdir, fde_control%pot_file)
96 grid_path = join_paths(fde_control%workdir, fde_control%grid_file)
97 rho_path = join_paths(fde_control%workdir, fde_control%rho_file)
98 res_path = join_paths(fde_control%workdir, fde_control%result_file)
99 input = fde_control%molcas_input_template
100
101 ! bare environment DFT energy
102 e_env = qs_energy%total
103
104 ! initial guess = zero
105 CALL auxbas_pw_pool%create_pw(rho)
106 CALL pw_zero(rho)
107 CALL auxbas_pw_pool%create_pw(rho_new)
108
109 max_iter = 1
110 IF (fde_control%freeze_and_thaw) max_iter = fde_control%max_ft_iter
111
112 IF (iounit > 0) THEN
113 WRITE (iounit, '(/,T2,A)') repeat("=", 78)
114 WRITE (iounit, '(T2,A)') "FDE: Frozen-density embedding"
115 WRITE (iounit, '(T2,A)') repeat("=", 78)
116 END IF
117
118 converged = .false.
119 DO iter = 1, max_iter
120 CALL fde_build_v_emb(qs_env, rho, v_emb, e_xc_na, e_kin_na)
121 CALL fde_write_pw(v_emb, trim(v_path), write_value=.true.)
122 CALL fde_write_pw(v_emb, trim(grid_path), write_value=.false.)
123
124 CALL fde_spawn(fde_control, trim(input), para_env)
125
126 CALL fde_read_pw(rho_new, trim(rho_path))
127 CALL fde_read_results(trim(res_path), para_env, e_sub, e_emb, e_n, n_el, n_a, z, r)
128
129 n_el_pw = pw_integrate_function(rho_new)
130 IF (n_el_pw > 1.0e-10_dp .AND. n_el > 0.0_dp) THEN
131 CALL pw_scale(rho_new, n_el/n_el_pw)
132 END IF
133
134 drho = fde_drho(rho, rho_new, para_env)
135
136 CALL fde_v_es(qs_env, phi_es)
137 phi_es_ptr => phi_es
138 e_es_e = pw_integral_ab(rho_new, phi_es)
139
140 e_es_n = 0.0_dp
141 DO i_a = 1, n_a
142 CALL interpolate_external_potential(r(:, i_a), phi_es_ptr, func=phi_at_atom)
143 e_es_n = e_es_n - z(i_a)*phi_at_atom
144 END DO
145 e_es_int = e_es_e + e_es_n
146 CALL auxbas_pw_pool%give_back_pw(phi_es)
147 DEALLOCATE (z, r)
148
149 e_sub_bare = e_sub - e_emb
150 e_tot = e_sub_bare + e_env + e_es_int + e_xc_na + e_kin_na
151
152 IF (iounit > 0) THEN
153 WRITE (iounit, '(/,T2,A,I0)') "FDE freeze-and-thaw iteration ", iter
154 WRITE (iounit, '(T4,A,F20.10)') "E(sub, embedded) = ", e_sub
155 WRITE (iounit, '(T4,A,F20.10)') "E(emb) = ", e_emb
156 WRITE (iounit, '(T4,A,F20.10)') "E(sub) = ", e_sub_bare
157 WRITE (iounit, '(T4,A,F20.10)') "E(env) = ", e_env
158 WRITE (iounit, '(T4,A,F20.10)') "E(es) = ", e_es_int
159 WRITE (iounit, '(T6,A,F20.10)') "E(elec) = ", e_es_e
160 WRITE (iounit, '(T6,A,F20.10)') "E(nuc) = ", e_es_n
161 WRITE (iounit, '(T4,A,F20.10)') "E(XC, nonadditive) = ", e_xc_na
162 WRITE (iounit, '(T4,A,F20.10)') "E(kin, nonadditive) = ", e_kin_na
163 WRITE (iounit, '(T4,A,F20.10)') "E(FDE) = ", e_tot
164 WRITE (iounit, '(T4,A,F14.6,A,F14.6)') "N(sub, e): grid = ", n_el_pw, &
165 " reported = ", n_el
166 WRITE (iounit, '(T4,A,ES14.6)') "int|rho(sub, new) - rho(sub, old)|dr = ", drho
167 END IF
168
169 CALL auxbas_pw_pool%give_back_pw(v_emb)
170
171 IF (fde_control%freeze_and_thaw) THEN
172 IF (drho < fde_control%eps_ft_density) THEN
173 converged = .true.
174 ELSE
175 CALL pw_scale(rho, 1.0_dp - fde_control%density_mixing)
176 CALL pw_axpy(rho_new, rho, fde_control%density_mixing)
177 END IF
178 ELSE
179 converged = .true.
180 END IF
181
182 IF (converged) EXIT
183
184 END DO
185
186 IF (iounit > 0) THEN
187 WRITE (iounit, '(/,T2,A)') repeat("=", 78)
188 IF (fde_control%freeze_and_thaw .AND. .NOT. converged) THEN
189 WRITE (iounit, '(T2,A)') "FDE WARNING: freeze-and-thaw did not converge"
190 END IF
191 WRITE (iounit, '(T2,A,F20.10)') "FDE final total energy = ", e_tot
192 WRITE (iounit, '(T2,A)') repeat("=", 78)
193 END IF
194
195 qs_energy%total = e_tot
196
197 CALL auxbas_pw_pool%give_back_pw(rho)
198 CALL auxbas_pw_pool%give_back_pw(rho_new)
199
200 CALL timestop(handle)
201
202 END SUBROUTINE fde_run
203
204! **************************************************************************************************
205!> \brief Integrated density difference: int |rho_new - rho_old| dr
206!> \param rho_a ...
207!> \param rho_b ...
208!> \param para_env ...
209!> \return ...
210! **************************************************************************************************
211 FUNCTION fde_drho(rho_a, rho_b, para_env) RESULT(drho)
212 TYPE(pw_r3d_rs_type), INTENT(IN) :: rho_a, rho_b
213 TYPE(mp_para_env_type), INTENT(IN) :: para_env
214 REAL(kind=dp) :: drho
215
216 drho = sum(abs(rho_b%array - rho_a%array))*rho_b%pw_grid%dvol
217 CALL para_env%sum(drho)
218
219 END FUNCTION fde_drho
220
221END MODULE fde_main
collects all references to literature in CP2K as new algorithms / method are included from literature...
integer, save, public schreder2024_3
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
Utility routines to open and close files. Tracking of preconnections.
Definition cp_files.F:16
character(len=default_path_length) function, public join_paths(path1, path2)
Joins two file-paths, inserting '/' as needed.
Definition cp_files.F:623
various routines to log and control the output. The idea is that decisions about where to log should ...
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...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
subroutine, public fde_run(qs_env)
Run the FDE self-consistency loop.
Definition fde_main.F:54
subroutine, public fde_read_results(filename, para_env, e_sub, e_emb, e_nuc, n_e, n_a, z_a, r_a)
Read scalar results returned by the subsystem solver, one value per line, in this order: E_sub : embe...
subroutine, public fde_read_pw(pw, filename)
Read the density in x y z weight value format.
subroutine, public fde_write_pw(pw, filename, write_value)
Write grid points to disk in the Molcas format, optionally with weights and values....
subroutine, public fde_build_v_emb(qs_env, rho, v_emb, e_xc_na, e_kin_na)
Build the embedding potential and evaluate nonadditive terms of a QS calculation.
subroutine, public fde_v_es(qs_env, v_es)
Build the electrostatic potential of a QS calculation.
Definition fde_methods.F:62
subroutine, public fde_spawn(fde_control, input_file, para_env)
Spawns the subsystem solver. Only the source rank launches the process; the other ranks wait for it o...
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_path_length
Definition kinds.F:58
Interface to the message passing library MPI.
container for various plainwaves related things
subroutine, public pw_env_get(pw_env, pw_pools, cube_info, gridlevel_info, auxbas_pw_pool, auxbas_grid, auxbas_rs_desc, auxbas_rs_grid, rs_descs, rs_grids, xc_pw_pool, vdw_pw_pool, poisson_env, interp_section)
returns the various attributes of the pw env
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
Perform a QUICKSTEP wavefunction optimization (single point).
Definition qs_energy.F:14
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.
Routines to handle an external electrostatic field The external field can be generic and is provided ...
subroutine, public interpolate_external_potential(r, grid, func, dfunc, calc_derivatives)
subroutine that interpolates the value of the external potential at position r based on the values on...
type of a logger, at the moment it contains just a print level starting at which level it should be l...
stores all the informations relevant to an mpi environment
contained for different pw related things
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...