(git:107f6cb)
Loading...
Searching...
No Matches
pexsi_types.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!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief Environment storing all data that is needed in order to call the DFT
10!> driver of the PEXSI library with data from the linear scaling quickstep
11!> SCF environment, mainly parameters and intermediate data for the matrix
12!> conversion between DBCSR and CSR format.
13!> \par History
14!> 2014.11 created [Patrick Seewald]
15!> \author Patrick Seewald
16! **************************************************************************************************
17
19 USE iso_c_binding, ONLY: c_intptr_t
20 USE bibliography, ONLY: lin2009,&
21 lin2013,&
22 cite_reference
23 USE cp_dbcsr_api, ONLY: dbcsr_p_type,&
30 USE kinds, ONLY: dp
31 USE message_passing, ONLY: mp_comm_type,&
38#include "./base/base_uses.f90"
39
40 IMPLICIT NONE
41
42 PRIVATE
43
44 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'pexsi_types'
45
46 LOGICAL, PARAMETER, PRIVATE :: careful_mod = .false.
47
48 INTEGER, PARAMETER, PUBLIC :: cp2k_to_pexsi = 1, pexsi_to_cp2k = 2
49
52
53! **************************************************************************************************
54!> \brief All PEXSI related data
55!> \param options PEXSI options
56!> \param plan PEXSI plan
57!> \param mp_group message-passing group ID
58!> \param mp_dims dimensions of the MPI cartesian grid used
59!> for PEXSI
60!> \param num_ranks_per_pole number of MPI ranks per pole in PEXSI
61!> \param kTS entropic energy contribution
62!> \param matrix_w energy-weighted density matrix as needed
63!> for the forces
64!> \param csr_mat intermediate matrices in CSR format
65!> \param dbcsr_template_matrix_sym Symmetric template matrix fixing DBCSR
66!> sparsity pattern
67!> \param dbcsr_template_matrix_nonsym Nonsymmetric template matrix fixing
68!> DBCSR sparsity pattern
69!> \param csr_sparsity DBCSR matrix defining CSR sparsity
70!> \param csr_screening whether distance screening should be
71!> applied to CSR matrices
72!> \param max_ev_vector eigenvector corresponding to the largest
73!> energy eigenvalue,
74!> returned by the Arnoldi method used to
75!> determine the spectral radius deltaE
76!> \param nspin number of spins
77!> \param do_adaptive_tol_nel Whether or not to use adaptive threshold
78!> for PEXSI convergence
79!> \param adaptive_nel_alpha constants for adaptive thresholding
80!> \param adaptive_nel_beta ...
81!> \param tol_nel_initial Initial convergence threshold (in number of electrons)
82!> \param tol_nel_target Target convergence threshold (in number of electrons)
83!> \par History
84!> 11.2014 created [Patrick Seewald]
85!> \author Patrick Seewald
86! **************************************************************************************************
88 TYPE(dbcsr_type) :: dbcsr_template_matrix_sym = dbcsr_type(), &
89 dbcsr_template_matrix_nonsym = dbcsr_type()
90 TYPE(dbcsr_csr_type) :: csr_mat_p = dbcsr_csr_type(), csr_mat_ks = dbcsr_csr_type(), &
91 csr_mat_s = dbcsr_csr_type(), csr_mat_e = dbcsr_csr_type(), &
92 csr_mat_f = dbcsr_csr_type()
94 REAL(kind=dp), DIMENSION(:), POINTER :: kts => null()
95 TYPE(dbcsr_p_type), DIMENSION(:), &
96 POINTER :: matrix_w => null()
97 INTEGER(KIND=C_INTPTR_T) :: plan = 0_c_intptr_t
98 INTEGER :: nspin = -1, num_ranks_per_pole = -1
99 TYPE(mp_comm_type) :: mp_group = mp_comm_type()
100 TYPE(dbcsr_type), DIMENSION(:), &
101 POINTER :: max_ev_vector => null()
102 TYPE(dbcsr_type) :: csr_sparsity = dbcsr_type()
103 INTEGER, DIMENSION(2) :: mp_dims = -1
104
105 LOGICAL :: csr_screening = .false., do_adaptive_tol_nel = .false.
106 REAL(kind=dp) :: adaptive_nel_alpha = -1.0_dp, adaptive_nel_beta = -1.0_dp, &
107 tol_nel_initial = -1.0_dp, tol_nel_target = -1.0_dp
108 END TYPE lib_pexsi_env
109
110CONTAINS
111
112! **************************************************************************************************
113!> \brief Initialize PEXSI
114!> \param pexsi_env All data needed by PEXSI
115!> \param mp_group message-passing group ID
116!> \param nspin number of spins
117!> \par History
118!> 11.2014 created [Patrick Seewald]
119!> \author Patrick Seewald
120! **************************************************************************************************
121 SUBROUTINE lib_pexsi_init(pexsi_env, mp_group, nspin)
122 TYPE(lib_pexsi_env), INTENT(INOUT) :: pexsi_env
123
124 CLASS(mp_comm_type), INTENT(IN) :: mp_group
125 INTEGER, INTENT(IN) :: nspin
126
127 CHARACTER(len=*), PARAMETER :: routinen = 'lib_pexsi_init'
128
129 INTEGER :: handle, ispin, npsymbfact, &
130 unit_nr
131 TYPE(cp_logger_type), POINTER :: logger
132
133 logger => cp_get_default_logger()
134 IF (logger%para_env%is_source()) THEN
135 unit_nr = cp_logger_get_default_unit_nr(logger, local=.true.)
136 ELSE
137 unit_nr = -1
138 END IF
139
140 CALL timeset(routinen, handle)
141
142 CALL cite_reference(lin2009)
143 CALL cite_reference(lin2013)
144
145 pexsi_env%mp_group = mp_group
146 associate(numnodes => pexsi_env%mp_group%num_pe)
147
148 ! Use all nodes available per pole by default or if the user tries to use
149 ! more nodes than MPI size
150 IF ((pexsi_env%num_ranks_per_pole > numnodes) &
151 .OR. (pexsi_env%num_ranks_per_pole == 0)) THEN
152 pexsi_env%num_ranks_per_pole = numnodes
153 END IF
154
155 ! Find num_ranks_per_pole from user input MIN_RANKS_PER_POLE s.t. num_ranks_per_pole
156 ! is the smallest number greater or equal to min_ranks_per_pole that divides
157 ! MPI size without remainder.
158 DO WHILE (mod(numnodes, pexsi_env%num_ranks_per_pole) /= 0)
159 pexsi_env%num_ranks_per_pole = pexsi_env%num_ranks_per_pole + 1
160 END DO
161
162 CALL cp_pexsi_get_options(pexsi_env%options, npsymbfact=npsymbfact)
163 IF ((npsymbfact > pexsi_env%num_ranks_per_pole) .OR. (npsymbfact == 0)) THEN
164 ! Use maximum possible number of ranks for symbolic factorization
165 CALL cp_pexsi_set_options(pexsi_env%options, npsymbfact=pexsi_env%num_ranks_per_pole)
166 END IF
167
168 ! Create dimensions for MPI cartesian grid for PEXSI
169 pexsi_env%mp_dims = 0
170 CALL mp_dims_create(pexsi_env%num_ranks_per_pole, pexsi_env%mp_dims)
171
172 ! allocations with nspin
173 pexsi_env%nspin = nspin
174 ALLOCATE (pexsi_env%kTS(nspin))
175 pexsi_env%kTS(:) = 0.0_dp
176 ALLOCATE (pexsi_env%max_ev_vector(nspin))
177 ALLOCATE (pexsi_env%matrix_w(nspin))
178 DO ispin = 1, pexsi_env%nspin
179 ALLOCATE (pexsi_env%matrix_w(ispin)%matrix)
180 END DO
181
182 ! Initialize PEXSI
183 pexsi_env%plan = cp_pexsi_plan_initialize(pexsi_env%mp_group, pexsi_env%mp_dims(1), &
184 pexsi_env%mp_dims(2), -1)
185 END associate
186
187 pexsi_env%do_adaptive_tol_nel = .false.
188
189 ! Print PEXSI infos
190 IF (unit_nr > 0) CALL print_pexsi_info(pexsi_env, unit_nr)
191
192 CALL timestop(handle)
193 END SUBROUTINE lib_pexsi_init
194
195! **************************************************************************************************
196!> \brief Release all PEXSI data
197!> \param pexsi_env ...
198!> \par History
199!> 11.2014 created [Patrick Seewald]
200!> \author Patrick Seewald
201! **************************************************************************************************
202 SUBROUTINE lib_pexsi_finalize(pexsi_env)
203 TYPE(lib_pexsi_env), INTENT(INOUT) :: pexsi_env
204
205 CHARACTER(len=*), PARAMETER :: routinen = 'lib_pexsi_finalize'
206
207 INTEGER :: handle, ispin
208
209 CALL timeset(routinen, handle)
210 CALL cp_pexsi_plan_finalize(pexsi_env%plan)
211 DEALLOCATE (pexsi_env%kTS)
212 DEALLOCATE (pexsi_env%max_ev_vector)
213 DO ispin = 1, pexsi_env%nspin
214 DEALLOCATE (pexsi_env%matrix_w(ispin)%matrix)
215 END DO
216 DEALLOCATE (pexsi_env%matrix_w)
217 CALL timestop(handle)
218 END SUBROUTINE lib_pexsi_finalize
219
220! **************************************************************************************************
221!> \brief Scale various quantities with factors of 2. This converts spin restricted
222!> DFT calculations (PEXSI) to the unrestricted case (as is the case where
223!> the density matrix method is called in the linear scaling code).
224!> \param[in] direction ...
225!> \param[in,out] numElectron ...
226!> \param matrix_p ...
227!> \param matrix_w ...
228!> \param kTS ...
229!> \par History
230!> 01.2015 created [Patrick Seewald]
231!> \author Patrick Seewald
232! **************************************************************************************************
233 SUBROUTINE convert_nspin_cp2k_pexsi(direction, numElectron, matrix_p, matrix_w, kTS)
234 INTEGER, INTENT(IN) :: direction
235 REAL(kind=dp), INTENT(INOUT), OPTIONAL :: numelectron
236 TYPE(dbcsr_type), INTENT(INOUT), OPTIONAL :: matrix_p
237 TYPE(dbcsr_p_type), INTENT(INOUT), OPTIONAL :: matrix_w
238 REAL(kind=dp), INTENT(INOUT), OPTIONAL :: kts
239
240 CHARACTER(len=*), PARAMETER :: routinen = 'convert_nspin_cp2k_pexsi'
241
242 INTEGER :: handle
243 REAL(kind=dp) :: scaling
244
245 CALL timeset(routinen, handle)
246
247 SELECT CASE (direction)
248 CASE (cp2k_to_pexsi)
249 scaling = 2.0_dp
250 CASE (pexsi_to_cp2k)
251 scaling = 0.5_dp
252 END SELECT
253
254 IF (PRESENT(numelectron)) numelectron = scaling*numelectron
255 IF (PRESENT(matrix_p)) CALL dbcsr_scale(matrix_p, scaling)
256 IF (PRESENT(matrix_w)) CALL dbcsr_scale(matrix_w%matrix, scaling)
257 IF (PRESENT(kts)) kts = scaling*kts
258
259 CALL timestop(handle)
260 END SUBROUTINE convert_nspin_cp2k_pexsi
261
262! **************************************************************************************************
263!> \brief Print relevant options of PEXSI
264!> \param pexsi_env ...
265!> \param unit_nr ...
266! **************************************************************************************************
267 SUBROUTINE print_pexsi_info(pexsi_env, unit_nr)
268 TYPE(lib_pexsi_env), INTENT(IN) :: pexsi_env
269 INTEGER, INTENT(IN) :: unit_nr
270
271 INTEGER :: npsymbfact, numnodes, numpole, ordering, &
272 rowordering
273 REAL(kind=dp) :: gap, muinertiaexpansion, &
274 muinertiatolerance, mumax0, mumin0, &
275 mupexsisafeguard, temperature
276
277 numnodes = pexsi_env%mp_group%num_pe
278
279 CALL cp_pexsi_get_options(pexsi_env%options, temperature=temperature, gap=gap, &
280 numpole=numpole, mumin0=mumin0, mumax0=mumax0, muinertiatolerance= &
281 muinertiatolerance, muinertiaexpansion=muinertiaexpansion, &
282 mupexsisafeguard=mupexsisafeguard, ordering=ordering, rowordering=rowordering, &
283 npsymbfact=npsymbfact)
284
285 WRITE (unit_nr, '(/A)') " PEXSI| Initialized with parameters"
286 WRITE (unit_nr, '(A,T61,E20.3)') " PEXSI| Electronic temperature", temperature
287 WRITE (unit_nr, '(A,T61,E20.3)') " PEXSI| Spectral gap", gap
288 WRITE (unit_nr, '(A,T61,I20)') " PEXSI| Number of poles", numpole
289 WRITE (unit_nr, '(A,T61,E20.3)') " PEXSI| Target tolerance in number of electrons", &
290 pexsi_env%tol_nel_target
291 WRITE (unit_nr, '(A,T61,E20.3)') " PEXSI| Convergence tolerance for inertia counting in mu", &
292 muinertiatolerance
293 WRITE (unit_nr, '(A,T61,E20.3)') " PEXSI| Restart tolerance for inertia counting in mu", &
294 mupexsisafeguard
295 WRITE (unit_nr, '(A,T61,E20.3)') " PEXSI| Expansion of mu interval for inertia counting", &
296 muinertiaexpansion
297
298 WRITE (unit_nr, '(/A)') " PEXSI| Parallelization scheme"
299 WRITE (unit_nr, '(A,T61,I20)') " PEXSI| Number of poles processed in parallel", &
300 numnodes/pexsi_env%num_ranks_per_pole
301 WRITE (unit_nr, '(A,T61,I20)') " PEXSI| Number of processors per pole", &
302 pexsi_env%num_ranks_per_pole
303 WRITE (unit_nr, '(A,T71,I5,T76,I5)') " PEXSI| Process grid dimensions", &
304 pexsi_env%mp_dims(1), pexsi_env%mp_dims(2)
305 SELECT CASE (ordering)
306 CASE (0)
307 WRITE (unit_nr, '(A,T61,A20)') " PEXSI| Ordering strategy", "parallel"
308 CASE (1)
309 WRITE (unit_nr, '(A,T61,A20)') " PEXSI| Ordering strategy", "sequential"
310 CASE (2)
311 WRITE (unit_nr, '(A,T61,A20)') " PEXSI| Ordering strategy", "multiple minimum degree"
312 END SELECT
313 SELECT CASE (rowordering)
314 CASE (0)
315 WRITE (unit_nr, '(A,T61,A20)') " PEXSI| Row permutation strategy", "no row permutation"
316 CASE (1)
317 WRITE (unit_nr, '(A,T61,A20)') " PEXSI| Row permutation strategy", "make diagonal entry larger than off diagonal"
318 END SELECT
319 IF (ordering == 0) WRITE (unit_nr, '(A,T61,I20/)') &
320 " PEXSI| Number of processors for symbolic factorization", npsymbfact
321
322 END SUBROUTINE print_pexsi_info
323END MODULE pexsi_types
collects all references to literature in CP2K as new algorithms / method are included from literature...
integer, save, public lin2009
integer, save, public lin2013
subroutine, public dbcsr_scale(matrix, alpha_scalar)
...
CSR matrix type, DBCSR-CSR conversions, and CSR file I/O built on cp_dbcsr_api.
various routines to log and control the output. The idea is that decisions about where to log should ...
recursive integer function, public cp_logger_get_default_unit_nr(logger, local, skip_not_ionode)
asks the default unit number of the given logger. try to use cp_logger_get_unit_nr
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Interface to the message passing library MPI.
subroutine, public mp_dims_create(nodes, dims)
wrapper to MPI_Dims_create
Interface to the PEXSI library, providing wrappers for all PEXSI routines that are called inside CP2K...
subroutine, public cp_pexsi_set_options(pexsi_options, temperature, gap, deltae, numpole, isinertiacount, maxpexsiiter, mumin0, mumax0, mu0, muinertiatolerance, muinertiaexpansion, mupexsisafeguard, numelectronpexsitolerance, matrixtype, issymbolicfactorize, ordering, rowordering, npsymbfact, verbosity)
Set PEXSI internal options.
subroutine, public cp_pexsi_get_options(pexsi_options, temperature, gap, deltae, numpole, isinertiacount, maxpexsiiter, mumin0, mumax0, mu0, muinertiatolerance, muinertiaexpansion, mupexsisafeguard, numelectronpexsitolerance, matrixtype, issymbolicfactorize, ordering, rowordering, npsymbfact, verbosity)
Access PEXSI internal options.
subroutine, public cp_pexsi_plan_finalize(plan)
...
integer(kind=c_intptr_t) function, public cp_pexsi_plan_initialize(comm, numprocrow, numproccol, outputfileindex)
...
Environment storing all data that is needed in order to call the DFT driver of the PEXSI library with...
Definition pexsi_types.F:18
subroutine, public convert_nspin_cp2k_pexsi(direction, numelectron, matrix_p, matrix_w, kts)
Scale various quantities with factors of 2. This converts spin restricted DFT calculations (PEXSI) to...
integer, parameter, public cp2k_to_pexsi
Definition pexsi_types.F:48
subroutine, public lib_pexsi_init(pexsi_env, mp_group, nspin)
Initialize PEXSI.
integer, parameter, public pexsi_to_cp2k
Definition pexsi_types.F:48
subroutine, public lib_pexsi_finalize(pexsi_env)
Release all PEXSI data.
type of a logger, at the moment it contains just a print level starting at which level it should be l...
All PEXSI related data.
Definition pexsi_types.F:87