Line data Source code
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 Shared real-space resolution-of-identity grid and matrix data.
10 : ! **************************************************************************************************
11 : MODULE ri_rs_types
12 : USE atomic_kind_types, ONLY: atomic_kind_type
13 : USE cell_types, ONLY: cell_type
14 : USE cp_dbcsr_api, ONLY: dbcsr_release,&
15 : dbcsr_type
16 : USE kinds, ONLY: dp
17 : USE particle_types, ONLY: particle_type
18 : #include "./base/base_uses.f90"
19 :
20 : IMPLICIT NONE
21 : PRIVATE
22 : PUBLIC :: ri_rs_env, ri_rs_grid_opt_type, rirs_grid_type, ri_rs_release
23 :
24 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'ri_rs_types'
25 :
26 : ! Atom-relative RI-RS grids
27 : TYPE rirs_grid_type
28 : INTEGER :: npts = 0
29 : REAL(KIND=dp), ALLOCATABLE :: raw_points(:, :)
30 : END TYPE rirs_grid_type
31 :
32 : ! User input for the in-memory RI-RS grid optimization.
33 : TYPE ri_rs_grid_opt_type
34 : LOGICAL :: enabled = .FALSE.
35 : REAL(KIND=dp) :: cutoff_atomic_cluster = -1.0_dp
36 : REAL(KIND=dp) :: rs_ao_ratio = -1.0_dp
37 : INTEGER :: max_iter = 20
38 : END TYPE ri_rs_grid_opt_type
39 :
40 : TYPE ri_rs_env
41 :
42 : ! Input parameters for RI-RS
43 : INTEGER :: grid_select = 1
44 : REAL(KIND=dp) :: tikhonov = 1.0E-08_dp
45 : REAL(KIND=dp) :: cutoff_radius_ri_ao = -1.0_dp
46 : REAL(KIND=dp) :: cutoff_radius_v_w = -1.0_dp
47 : TYPE(ri_rs_grid_opt_type) :: grid_opt = ri_rs_grid_opt_type()
48 :
49 : ! Geometry references used by RI-RS grid setup and optimization.
50 : ! They remain owned by qs_env and must never be deallocated here.
51 : TYPE(cell_type), POINTER :: cell => NULL()
52 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set => NULL()
53 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set => NULL()
54 :
55 : ! Data types for cutoffs based DBCSR matrices
56 : REAL(KIND=dp), ALLOCATABLE :: chunk_centroids(:, :)
57 : INTEGER, ALLOCATABLE :: grid_atom_boundaries(:)
58 : INTEGER, ALLOCATABLE :: pan_first(:), pan_last(:)
59 : ! Panel plans share a fixed grid, process layout and message limit until release.
60 : REAL(KIND=dp) :: panel_plan_cutoff = 0.0_dp
61 :
62 : ! Atom-specific grids used directly by the grid assembler
63 : TYPE(rirs_grid_type), ALLOCATABLE :: atomic_grids(:)
64 :
65 : ! Data types for storing RI-RS matrices
66 : INTEGER :: n_grid_points = 0
67 : TYPE(dbcsr_type) :: mat_phi_mu_l = dbcsr_type()
68 : TYPE(dbcsr_type) :: mat_Z_lP = dbcsr_type()
69 : REAL(KIND=dp), ALLOCATABLE :: grid_points(:, :)
70 : LOGICAL :: Z_lP_exists = .FALSE.
71 :
72 : ! Dense Gram + phi + threaded RHS memory model, collected during fitting setup.
73 : INTEGER :: fit_max_grid = 0
74 : REAL(KIND=dp) :: fit_peak_bytes = 0.0_dp
75 :
76 : ! Spatial extent of most diffuse Gaussian primitive of AO/RI basis for each atom
77 : REAL(KIND=dp), ALLOCATABLE :: radius_ao_per_atom(:)
78 : REAL(KIND=dp), ALLOCATABLE :: radius_ri_per_atom(:)
79 :
80 : ! Precomputed grid-basis kernels for RT-BSE
81 : ! mat_V_aux_rtbse(P, Q) = truncated-Coulomb V_PQ with M^-1 sandwich (RI x RI);
82 : ! the Hartree grid kernel Z V Z^T is applied factorized, never materialized
83 : ! mat_W0_grid_rtbse(l, l') = sum_PQ Z_lP W^{w=0}_PQ Z_l'Q
84 : TYPE(dbcsr_type) :: mat_V_aux_rtbse = dbcsr_type()
85 : TYPE(dbcsr_type) :: mat_W0_grid_rtbse = dbcsr_type()
86 :
87 : ! RTBSE%EPS_FILTER_RHO: cuts Delta-rho_AO in compute_sigma_ri_rs, also cuts SEX and Hartree
88 : REAL(KIND=dp) :: eps_filter_rho = -1.0_dp
89 :
90 : ! RTBSE%CUTOFF_RADIUS_W0 (bohr): drops grid block pairs of W0 beyond this distance.
91 : ! BSE-only: the GW quasiparticle energies use their own &GW CUTOFF_RADIUS_* family.
92 : REAL(KIND=dp) :: cutoff_radius_w0 = -1.0_dp
93 :
94 : ! Set TRUE once mat_phi_mu_l and mat_Z_lP have been populated in memory (by GW RI-RS
95 : ! or on-demand by the RT-BSE RI-RS kernel path).
96 : LOGICAL :: grid_built = .FALSE.
97 :
98 : ! Independent toggles for auxiliary Coulomb / grid W0 availability
99 : LOGICAL :: V_grid_built = .FALSE.
100 : LOGICAL :: W0_grid_built = .FALSE.
101 :
102 : END TYPE ri_rs_env
103 :
104 : CONTAINS
105 :
106 : ! **************************************************************************************************
107 : !> \brief Release owned RI-RS matrices and grid data, then clear construction state.
108 : !> Input settings and restart-file availability are retained. Geometry references are
109 : !> borrowed from qs_env and are only nullified. Repeated calls are safe.
110 : !> \param ri_rs RI-RS input, owned grid/matrix data and borrowed geometry references
111 : ! **************************************************************************************************
112 158 : SUBROUTINE ri_rs_release(ri_rs)
113 : TYPE(ri_rs_env), INTENT(INOUT) :: ri_rs
114 :
115 : CHARACTER(LEN=*), PARAMETER :: routineN = 'ri_rs_release'
116 :
117 : INTEGER :: handle
118 :
119 158 : CALL timeset(routineN, handle)
120 :
121 158 : IF (ri_rs%W0_grid_built) CALL dbcsr_release(ri_rs%mat_W0_grid_rtbse)
122 158 : IF (ri_rs%V_grid_built) CALL dbcsr_release(ri_rs%mat_V_aux_rtbse)
123 158 : IF (ri_rs%grid_built) THEN
124 50 : CALL dbcsr_release(ri_rs%mat_phi_mu_l)
125 50 : CALL dbcsr_release(ri_rs%mat_Z_lP)
126 : END IF
127 158 : IF (ALLOCATED(ri_rs%grid_points)) DEALLOCATE (ri_rs%grid_points)
128 158 : IF (ALLOCATED(ri_rs%atomic_grids)) DEALLOCATE (ri_rs%atomic_grids)
129 158 : IF (ALLOCATED(ri_rs%radius_ao_per_atom)) DEALLOCATE (ri_rs%radius_ao_per_atom)
130 158 : IF (ALLOCATED(ri_rs%radius_ri_per_atom)) DEALLOCATE (ri_rs%radius_ri_per_atom)
131 158 : IF (ALLOCATED(ri_rs%chunk_centroids)) DEALLOCATE (ri_rs%chunk_centroids)
132 158 : IF (ALLOCATED(ri_rs%grid_atom_boundaries)) DEALLOCATE (ri_rs%grid_atom_boundaries)
133 158 : IF (ALLOCATED(ri_rs%pan_first)) DEALLOCATE (ri_rs%pan_first)
134 158 : IF (ALLOCATED(ri_rs%pan_last)) DEALLOCATE (ri_rs%pan_last)
135 158 : ri_rs%panel_plan_cutoff = 0.0_dp
136 158 : NULLIFY (ri_rs%atomic_kind_set, ri_rs%cell, ri_rs%particle_set)
137 158 : ri_rs%fit_max_grid = 0
138 158 : ri_rs%fit_peak_bytes = 0.0_dp
139 158 : ri_rs%n_grid_points = 0
140 158 : ri_rs%grid_built = .FALSE.
141 158 : ri_rs%V_grid_built = .FALSE.
142 158 : ri_rs%W0_grid_built = .FALSE.
143 :
144 158 : CALL timestop(handle)
145 :
146 158 : END SUBROUTINE ri_rs_release
147 :
148 0 : END MODULE ri_rs_types
|