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