Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief represent a group ofunctional derivatives
10 : !> \par History
11 : !> 11.2003 created [fawzi]
12 : !> \author fawzi & thomas
13 : ! **************************************************************************************************
14 : MODULE xc_derivative_set_types
15 : USE cp_linked_list_xc_deriv, ONLY: cp_sll_xc_deriv_dealloc,&
16 : cp_sll_xc_deriv_insert_el,&
17 : cp_sll_xc_deriv_next,&
18 : cp_sll_xc_deriv_type
19 : USE kinds, ONLY: dp
20 : USE pw_grid_types, ONLY: pw_grid_type
21 : USE pw_grids, ONLY: pw_grid_create,&
22 : pw_grid_release
23 : USE pw_methods, ONLY: pw_zero
24 : USE pw_pool_types, ONLY: pw_pool_create,&
25 : pw_pool_release,&
26 : pw_pool_type
27 : USE pw_types, ONLY: pw_r3d_rs_type
28 : USE xc_derivative_desc, ONLY: standardize_desc
29 : USE xc_derivative_types, ONLY: xc_derivative_create,&
30 : xc_derivative_release,&
31 : xc_derivative_type
32 : #include "../base/base_uses.f90"
33 :
34 : IMPLICIT NONE
35 : PRIVATE
36 :
37 : LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
38 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'xc_derivative_set_types'
39 :
40 : PUBLIC :: xc_derivative_set_type
41 : PUBLIC :: xc_dset_create, xc_dset_release, &
42 : xc_dset_get_derivative, xc_dset_zero_all, xc_dset_recover_pw
43 :
44 : ! **************************************************************************************************
45 : !> \brief A derivative set contains the different derivatives of a xc-functional
46 : !> in form of a linked list
47 : ! **************************************************************************************************
48 : TYPE xc_derivative_set_type
49 : TYPE(pw_pool_type), POINTER, PRIVATE :: pw_pool => NULL()
50 : TYPE(cp_sll_xc_deriv_type), POINTER :: derivs => NULL()
51 : END TYPE xc_derivative_set_type
52 :
53 : CONTAINS
54 :
55 : ! **************************************************************************************************
56 : !> \brief returns the requested xc_derivative
57 : !> \param derivative_set the set where to search for the derivative
58 : !> \param description the description of the derivative you want to have
59 : !> \param allocate_deriv if the derivative should be allocated when not present
60 : !> Defaults to false.
61 : !> \return ...
62 : ! **************************************************************************************************
63 2289228 : FUNCTION xc_dset_get_derivative(derivative_set, description, allocate_deriv) &
64 : RESULT(res)
65 :
66 : TYPE(xc_derivative_set_type), INTENT(IN) :: derivative_set
67 : INTEGER, DIMENSION(:), INTENT(in) :: description
68 : LOGICAL, INTENT(in), OPTIONAL :: allocate_deriv
69 : TYPE(xc_derivative_type), POINTER :: res
70 :
71 2289228 : INTEGER, ALLOCATABLE, DIMENSION(:) :: std_deriv_desc
72 : LOGICAL :: my_allocate_deriv
73 : REAL(kind=dp), CONTIGUOUS, DIMENSION(:, :, :), &
74 2289228 : POINTER :: r3d_ptr
75 : TYPE(cp_sll_xc_deriv_type), POINTER :: pos
76 : TYPE(xc_derivative_type), POINTER :: deriv_att
77 :
78 2289228 : NULLIFY (pos, deriv_att, r3d_ptr)
79 :
80 2289228 : my_allocate_deriv = .FALSE.
81 824997 : IF (PRESENT(allocate_deriv)) my_allocate_deriv = allocate_deriv
82 2289228 : NULLIFY (res)
83 2289228 : CALL standardize_desc(description, std_deriv_desc)
84 2289228 : pos => derivative_set%derivs
85 9288067 : DO WHILE (cp_sll_xc_deriv_next(pos, el_att=deriv_att))
86 9288067 : IF (SIZE(deriv_att%split_desc) == SIZE(std_deriv_desc)) THEN
87 6117083 : IF (ALL(deriv_att%split_desc == std_deriv_desc)) THEN
88 913327 : res => deriv_att
89 913327 : EXIT
90 : END IF
91 : END IF
92 : END DO
93 2289228 : IF (.NOT. ASSOCIATED(res) .AND. my_allocate_deriv) THEN
94 559262 : CALL derivative_set%pw_pool%create_cr3d(r3d_ptr)
95 19886769991 : r3d_ptr = 0.0_dp
96 559262 : ALLOCATE (res)
97 : CALL xc_derivative_create(res, std_deriv_desc, &
98 559262 : r3d_ptr=r3d_ptr)
99 559262 : CALL cp_sll_xc_deriv_insert_el(derivative_set%derivs, res)
100 : END IF
101 4578456 : END FUNCTION xc_dset_get_derivative
102 :
103 : ! **************************************************************************************************
104 : !> \brief creates a derivative set object
105 : !> \param derivative_set the set where to search for the derivative
106 : !> \param pw_pool pool where to get the cr3d arrays needed to store the
107 : !> derivatives
108 : !> \param local_bounds ...
109 : ! **************************************************************************************************
110 187837 : SUBROUTINE xc_dset_create(derivative_set, pw_pool, local_bounds)
111 :
112 : TYPE(xc_derivative_set_type), INTENT(OUT) :: derivative_set
113 : TYPE(pw_pool_type), OPTIONAL, POINTER :: pw_pool
114 : INTEGER, DIMENSION(2, 3), INTENT(IN), OPTIONAL :: local_bounds
115 :
116 : TYPE(pw_grid_type), POINTER :: pw_grid
117 :
118 187837 : NULLIFY (pw_grid)
119 :
120 187837 : IF (PRESENT(pw_pool)) THEN
121 128725 : derivative_set%pw_pool => pw_pool
122 128725 : CALL pw_pool%retain()
123 128725 : IF (PRESENT(local_bounds)) THEN
124 0 : IF (ANY(pw_pool%pw_grid%bounds_local /= local_bounds)) &
125 0 : CPABORT("incompatible local_bounds and pw_pool")
126 : END IF
127 : ELSE
128 : !FM ugly hack, should be replaced by a pool only for 3d arrays
129 59112 : CPASSERT(PRESENT(local_bounds))
130 59112 : CALL pw_grid_create(pw_grid, local_bounds)
131 59112 : CALL pw_pool_create(derivative_set%pw_pool, pw_grid)
132 59112 : CALL pw_grid_release(pw_grid)
133 : END IF
134 :
135 187837 : END SUBROUTINE xc_dset_create
136 :
137 : ! **************************************************************************************************
138 : !> \brief releases a derivative set
139 : !> \param derivative_set the set to release
140 : ! **************************************************************************************************
141 187837 : SUBROUTINE xc_dset_release(derivative_set)
142 :
143 : TYPE(xc_derivative_set_type) :: derivative_set
144 :
145 : TYPE(cp_sll_xc_deriv_type), POINTER :: pos
146 : TYPE(xc_derivative_type), POINTER :: deriv_att
147 :
148 187837 : NULLIFY (deriv_att, pos)
149 :
150 187837 : pos => derivative_set%derivs
151 747099 : DO WHILE (cp_sll_xc_deriv_next(pos, el_att=deriv_att))
152 559262 : CALL xc_derivative_release(deriv_att, pw_pool=derivative_set%pw_pool)
153 559262 : DEALLOCATE (deriv_att)
154 : END DO
155 187837 : CALL cp_sll_xc_deriv_dealloc(derivative_set%derivs)
156 187837 : IF (ASSOCIATED(derivative_set%pw_pool)) CALL pw_pool_release(derivative_set%pw_pool)
157 :
158 187837 : END SUBROUTINE xc_dset_release
159 :
160 : ! **************************************************************************************************
161 : !> \brief ...
162 : !> \param deriv_set ...
163 : ! **************************************************************************************************
164 71490 : SUBROUTINE xc_dset_zero_all(deriv_set)
165 :
166 : TYPE(xc_derivative_set_type), INTENT(IN) :: deriv_set
167 :
168 : TYPE(cp_sll_xc_deriv_type), POINTER :: pos
169 : TYPE(xc_derivative_type), POINTER :: deriv_att
170 :
171 71490 : NULLIFY (pos, deriv_att)
172 :
173 71490 : IF (ASSOCIATED(deriv_set%derivs)) THEN
174 27279 : pos => deriv_set%derivs
175 111750 : DO WHILE (cp_sll_xc_deriv_next(pos, el_att=deriv_att))
176 247577769 : deriv_att%deriv_data = 0.0_dp
177 : END DO
178 : END IF
179 :
180 71490 : END SUBROUTINE xc_dset_zero_all
181 :
182 : ! **************************************************************************************************
183 : !> \brief Recovers a derivative on a pw_r3d_rs_type, the caller is responsible to release the grid later
184 : !> If the derivative is not found, either creates a blank pw_r3d_rs_type from pw_pool or leaves it unassociated
185 : !> \param deriv_set ...
186 : !> \param description ...
187 : !> \param pw ...
188 : !> \param pw_grid ...
189 : !> \param pw_pool create pw from this pool if derivative not found
190 : ! **************************************************************************************************
191 244527 : SUBROUTINE xc_dset_recover_pw(deriv_set, description, pw, pw_grid, pw_pool)
192 : TYPE(xc_derivative_set_type), INTENT(IN) :: deriv_set
193 : INTEGER, DIMENSION(:), INTENT(IN) :: description
194 : TYPE(pw_r3d_rs_type), INTENT(OUT) :: pw
195 : TYPE(pw_grid_type), INTENT(IN), POINTER :: pw_grid
196 : TYPE(pw_pool_type), INTENT(IN), OPTIONAL, POINTER :: pw_pool
197 :
198 : TYPE(xc_derivative_type), POINTER :: deriv_att
199 :
200 244527 : deriv_att => xc_dset_get_derivative(deriv_set, description)
201 244527 : IF (ASSOCIATED(deriv_att)) THEN
202 243905 : CALL pw%create(pw_grid=pw_grid, array_ptr=deriv_att%deriv_data)
203 243905 : NULLIFY (deriv_att%deriv_data)
204 622 : ELSE IF (PRESENT(pw_pool)) THEN
205 622 : CALL pw_pool%create_pw(pw)
206 622 : CALL pw_zero(pw)
207 : END IF
208 :
209 244527 : END SUBROUTINE xc_dset_recover_pw
210 :
211 0 : END MODULE xc_derivative_set_types
|