Debian Codegraph @

src/mbd_c_api.F90

raw
1 ! This Source Code Form is subject to the terms of the Mozilla Public
2 ! License, v. 2.0. If a copy of the MPL was not distributed with this
3 ! file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 module mbd_c_api
5 !! Implementation of C API.
6
7 use iso_c_binding, only: c_int, c_double, c_bool, c_char, c_ptr, c_double_complex, &
8 c_null_char, c_loc, c_f_pointer, c_associated
9 use mbd_constants
10 use mbd_version
11 use mbd_coulomb, only: dipole_energy, coulomb_energy
12 use mbd_damping, only: damping_t
13 use mbd_dipole, only: dipole_matrix
14 use mbd_density, only: eval_mbd_nonint_density, eval_mbd_int_density
15 use mbd_geom, only: geom_t
16 use mbd_gradients, only: grad_t, grad_request_t
17 use mbd_matrix, only: matrix_re_t, matrix_cplx_t
18 use mbd_methods, only: get_mbd_energy, get_mbd_scs_energy
19 use mbd_ts, only: get_ts_energy
20 use mbd_utils, only: result_t
21
22 implicit none
23
24 private
25 public :: cmbd_with_scalapack, cmbd_with_mpi, cmbd_version_major, &
26 cmbd_version_minor, cmbd_version_patch, cmbd_version_suffix
27 public :: cmbd_init_geom, cmbd_destroy_geom, cmbd_init_damping, &
28 cmbd_destroy_damping, cmbd_get_exception, cmbd_update_coords, cmbd_update_lattice, &
29 cmbd_get_results, cmbd_destroy_result, cmbd_print_timing
30 public :: cmbd_ts_energy, cmbd_mbd_energy, cmbd_mbd_scs_energy, &
31 cmbd_dipole_matrix, cmbd_coulomb_energy, cmbd_dipole_energy
32
33 #ifdef WITH_MPI
34 logical(c_bool), bind(c) :: cmbd_with_mpi = .true.
35 #else
36 logical(c_bool), bind(c) :: cmbd_with_mpi = .false.
37 #endif
38 #ifdef WITH_SCALAPACK
39 logical(c_bool), bind(c) :: cmbd_with_scalapack = .true.
40 #else
41 logical(c_bool), bind(c) :: cmbd_with_scalapack = .false.
42 #endif
43
44 integer(c_int), bind(c) :: cmbd_version_major = mbd_version_major
45 integer(c_int), bind(c) :: cmbd_version_minor = mbd_version_minor
46 integer(c_int), bind(c) :: cmbd_version_patch = mbd_version_patch
47 #ifdef __NVCOMPILER
48 ! nvfortran (NVIDIA HPC SDK) ICEs on the array-constructor initializer used in
49 ! the standard branch below (see issue #64); an equivalent DATA statement
50 ! compiles cleanly.
51 character(c_char), bind(c) :: cmbd_version_suffix(30)
52 data cmbd_version_suffix/ &
53 mbd_version_suffix(1:1), mbd_version_suffix(2:2), mbd_version_suffix(3:3), mbd_version_suffix(4:4), &
54 mbd_version_suffix(5:5), mbd_version_suffix(6:6), mbd_version_suffix(7:7), mbd_version_suffix(8:8), &
55 mbd_version_suffix(9:9), mbd_version_suffix(10:10), mbd_version_suffix(11:11), mbd_version_suffix(12:12), &
56 mbd_version_suffix(13:13), mbd_version_suffix(14:14), mbd_version_suffix(15:15), mbd_version_suffix(16:16), &
57 mbd_version_suffix(17:17), mbd_version_suffix(18:18), mbd_version_suffix(19:19), mbd_version_suffix(20:20), &
58 mbd_version_suffix(21:21), mbd_version_suffix(22:22), mbd_version_suffix(23:23), mbd_version_suffix(24:24), &
59 mbd_version_suffix(25:25), mbd_version_suffix(26:26), mbd_version_suffix(27:27), mbd_version_suffix(28:28), &
60 mbd_version_suffix(29:29), mbd_version_suffix(30:30) &
61 /
62 #else
63 integer :: i
64 character(c_char), bind(c) :: cmbd_version_suffix(30) = [(mbd_version_suffix(i:i), i=1, 30)]
65 #endif
66
67 contains
68
69 type(c_ptr) function cmbd_init_geom( &
70 n_atoms, coords, lattice, k_grid, n_kpts, custom_k_pts, &
71 n_freq, do_rpa, get_spectrum, get_rpa_orders, rpa_rescale_eigs, &
72 max_atoms_per_block, ewald_cutoff_scaling &
73 ) bind(c)
74 integer(c_int), value, intent(in) :: n_atoms
75 real(c_double), intent(in) :: coords(3, n_atoms)
76 real(c_double), optional, intent(in) :: lattice(3, 3)
77 integer(c_int), optional, intent(in) :: k_grid(3)
78 integer(c_int), value, intent(in) :: n_kpts
79 real(c_double), optional, intent(in) :: custom_k_pts(3, n_kpts)
80 integer(c_int), value, intent(in) :: n_freq
81 logical(c_bool), value, intent(in) :: do_rpa
82 logical(c_bool), value, intent(in) :: get_spectrum
83 logical(c_bool), value, intent(in) :: get_rpa_orders
84 logical(c_bool), value, intent(in) :: rpa_rescale_eigs
85 integer(c_int), value, intent(in) :: max_atoms_per_block
86 real(c_double), intent(in) :: ewald_cutoff_scaling(2)
87
88 type(geom_t), pointer :: geom
89
90 allocate (geom)
91 geom%coords = coords
92 if (present(lattice)) geom%lattice = lattice
93 if (present(k_grid)) geom%k_grid = k_grid
94 if (present(custom_k_pts)) geom%custom_k_pts = custom_k_pts
95 if (n_freq > 0) geom%param%n_freq = n_freq
96 #ifdef WITH_SCALAPACK
97 if (max_atoms_per_block > 0) geom%max_atoms_per_block = max_atoms_per_block
98 #endif
99 geom%do_rpa = do_rpa
100 geom%get_eigs = get_spectrum
101 geom%get_modes = get_spectrum
102 geom%get_rpa_orders = get_rpa_orders
103 geom%param%rpa_rescale_eigs = rpa_rescale_eigs
104 geom%param%ewald_real_cutoff_scaling = ewald_cutoff_scaling(1)
105 geom%param%ewald_rec_cutoff_scaling = ewald_cutoff_scaling(2)
106 call geom%init()
107 cmbd_init_geom = c_loc(geom)
108 end function
109
110 subroutine cmbd_update_coords(geom_c, coords_c) bind(c)
111 type(c_ptr), value, intent(in) :: geom_c
112 type(c_ptr), value, intent(in) :: coords_c
113
114 type(geom_t), pointer :: geom
115 real(c_double), pointer :: coords(:, :)
116
117 call c_f_pointer(geom_c, geom)
118 call c_f_pointer(coords_c, coords, [3, geom%siz()])
119 geom%coords = coords
120 end subroutine
121
122 subroutine cmbd_update_lattice(geom_c, lattice) bind(c)
123 type(c_ptr), value, intent(in) :: geom_c
124 real(c_double), intent(in) :: lattice(3, 3)
125
126 type(geom_t), pointer :: geom
127
128 call c_f_pointer(geom_c, geom)
129 geom%lattice = lattice
130 end subroutine
131
132 subroutine cmbd_destroy_geom(geom_c) bind(c)
133 type(c_ptr), value, intent(in) :: geom_c
134
135 type(geom_t), pointer :: geom
136
137 call c_f_pointer(geom_c, geom)
138 call geom%destroy()
139 deallocate (geom)
140 end subroutine
141
142 subroutine cmbd_get_exception(geom_c, code, origin, msg) bind(c)
143 type(c_ptr), value, intent(in) :: geom_c
144 integer(c_int), intent(out) :: code
145 character(kind=c_char), intent(out) :: origin(50), msg(150)
146
147 type(geom_t), pointer :: geom
148
149 call c_f_pointer(geom_c, geom)
150 code = geom%exc%code
151 call f_c_string(geom%exc%origin, origin)
152 call f_c_string(geom%exc%msg, msg)
153 geom%exc%code = 0
154 geom%exc%origin = ''
155 geom%exc%msg = ''
156 end subroutine
157
158 type(c_ptr) function cmbd_init_damping(n_atoms, version_c, r_vdw, sigma, beta, a) bind(c)
159 integer(c_int), value, intent(in) :: n_atoms
160 character(kind=c_char), intent(in) :: version_c(*)
161 real(c_double), optional, intent(in) :: r_vdw(n_atoms)
162 real(c_double), optional, intent(in) :: sigma(n_atoms)
163 real(c_double), value, intent(in) :: beta
164 real(c_double), value, intent(in) :: a
165
166 type(damping_t), pointer :: damping
167
168 allocate (damping)
169 damping%version = f_string(version_c)
170 if (present(r_vdw)) damping%r_vdw = r_vdw
171 if (present(sigma)) damping%sigma = sigma
172 damping%beta = beta
173 damping%a = a
174 damping%ts_sr = beta
175 damping%ts_d = a
176 cmbd_init_damping = c_loc(damping)
177 end function
178
179 subroutine cmbd_destroy_damping(damping_c) bind(c)
180 type(c_ptr), value, intent(in) :: damping_c
181
182 type(damping_t), pointer :: damping
183
184 call c_f_pointer(damping_c, damping)
185 deallocate (damping)
186 end subroutine
187
188 type(c_ptr) function cmbd_ts_energy(geom_c, alpha_0_c, C6_c, damping_c, grad) bind(c)
189 type(c_ptr), value, intent(in) :: geom_c
190 type(c_ptr), value, intent(in) :: alpha_0_c
191 type(c_ptr), value, intent(in) :: C6_c
192 type(c_ptr), value, intent(in) :: damping_c
193 logical(c_bool), value, intent(in) :: grad
194
195 type(geom_t), pointer :: geom
196 real(c_double), pointer :: alpha_0(:)
197 real(c_double), pointer :: C6(:)
198 type(damping_t), pointer :: damping
199 type(result_t), pointer :: res
200
201 call c_f_pointer(geom_c, geom)
202 call c_f_pointer(alpha_0_c, alpha_0, [geom%siz()])
203 call c_f_pointer(C6_c, C6, [geom%siz()])
204 call c_f_pointer(damping_c, damping)
205 allocate (res)
206 res = get_ts_energy( &
207 geom, alpha_0, C6, damping, grad_request_t( &
208 dcoords=grad, dlattice=grad .and. allocated(geom%lattice) &
209 ) &
210 )
211 cmbd_ts_energy = c_loc(res)
212 end function
213
214 type(c_ptr) function cmbd_mbd_energy(geom_c, alpha_0_c, C6_c, damping_c, grad) bind(c)
215 type(c_ptr), value, intent(in) :: geom_c
216 type(c_ptr), value, intent(in) :: alpha_0_c
217 type(c_ptr), value, intent(in) :: C6_c
218 type(c_ptr), value, intent(in) :: damping_c
219 logical(c_bool), value, intent(in) :: grad
220
221 type(geom_t), pointer :: geom
222 real(c_double), pointer :: alpha_0(:)
223 real(c_double), pointer :: C6(:)
224 type(damping_t), pointer :: damping
225 type(result_t), pointer :: res
226
227 call c_f_pointer(geom_c, geom)
228 call c_f_pointer(alpha_0_c, alpha_0, [geom%siz()])
229 call c_f_pointer(C6_c, C6, [geom%siz()])
230 call c_f_pointer(damping_c, damping)
231 allocate (res)
232 res = get_mbd_energy( &
233 geom, alpha_0, C6, damping, grad_request_t( &
234 dcoords=grad, dlattice=grad .and. allocated(geom%lattice) &
235 ) &
236 )
237 cmbd_mbd_energy = c_loc(res)
238 end function
239
240 type(c_ptr) function cmbd_mbd_scs_energy( &
241 geom_c, variant_c, alpha_0_c, C6_c, damping_c, grad) bind(c)
242 type(c_ptr), value, intent(in) :: geom_c
243 character(kind=c_char), intent(in) :: variant_c(*)
244 type(c_ptr), value, intent(in) :: alpha_0_c
245 type(c_ptr), value, intent(in) :: C6_c
246 type(c_ptr), value, intent(in) :: damping_c
247 logical(c_bool), value, intent(in) :: grad
248
249 type(geom_t), pointer :: geom
250 character(len=20) :: variant
251 real(c_double), pointer :: alpha_0(:)
252 real(c_double), pointer :: C6(:)
253 type(damping_t), pointer :: damping
254 type(result_t), pointer :: res
255
256 call c_f_pointer(geom_c, geom)
257 variant = f_string(variant_c)
258 call c_f_pointer(alpha_0_c, alpha_0, [geom%siz()])
259 call c_f_pointer(C6_c, C6, [geom%siz()])
260 call c_f_pointer(damping_c, damping)
261 allocate (res)
262 res = get_mbd_scs_energy( &
263 geom, variant, alpha_0, C6, damping, grad_request_t( &
264 dcoords=grad, dlattice=grad .and. allocated(geom%lattice) &
265 ) &
266 )
267 cmbd_mbd_scs_energy = c_loc(res)
268 end function
269
270 subroutine cmbd_get_results( &
271 res_c, energy, gradients_c, lattice_gradients_c, eigvals_c, eigvecs_c, rpa_orders_c, &
272 eigvals_k_c, eigvecs_k_c, alpha_0_c, C6_c &
273 ) bind(c)
274 type(c_ptr), value, intent(in) :: res_c
275 real(c_double), intent(out) :: energy
276 type(c_ptr), value, intent(in) :: gradients_c
277 type(c_ptr), value, intent(in) :: lattice_gradients_c
278 type(c_ptr), value, intent(in) :: eigvals_c
279 type(c_ptr), value, intent(in) :: eigvecs_c
280 type(c_ptr), value, intent(in) :: rpa_orders_c
281 type(c_ptr), value, intent(in) :: eigvals_k_c
282 type(c_ptr), value, intent(in) :: eigvecs_k_c
283 type(c_ptr), value, intent(in) :: alpha_0_c
284 type(c_ptr), value, intent(in) :: C6_c
285
286 type(result_t), pointer :: res
287 real(c_double), pointer :: gradients(:, :)
288 real(c_double), pointer :: lattice_gradients(:, :)
289 real(c_double), pointer :: eigvals(:)
290 real(c_double), pointer :: eigvecs(:, :)
291 real(c_double), pointer :: rpa_orders(:)
292 real(c_double), pointer :: eigvals_k(:, :)
293 complex(c_double_complex), pointer :: eigvecs_k(:, :, :)
294 real(c_double), pointer :: alpha_0(:)
295 real(c_double), pointer :: C6(:)
296
297 call c_f_pointer(res_c, res)
298 energy = res%energy
299 if (c_associated(gradients_c) .and. allocated(res%dE%dcoords)) then
300 call c_f_pointer(gradients_c, gradients, [3, size(res%dE%dcoords, 1)])
301 gradients = transpose(res%dE%dcoords)
302 end if
303 if (c_associated(lattice_gradients_c) .and. allocated(res%dE%dlattice)) then
304 call c_f_pointer(lattice_gradients_c, lattice_gradients, [3, 3])
305 lattice_gradients = transpose(res%dE%dlattice)
306 end if
307 if (c_associated(eigvals_c) .and. allocated(res%mode_eigs)) then
308 call c_f_pointer(eigvals_c, eigvals, [size(res%mode_eigs)])
309 eigvals = res%mode_eigs
310 end if
311 if (c_associated(eigvecs_c) .and. allocated(res%modes)) then
312 call c_f_pointer(eigvecs_c, eigvecs, [size(res%modes, 1), size(res%modes, 2)])
313 eigvecs = res%modes
314 end if
315 if (c_associated(rpa_orders_c) .and. allocated(res%rpa_orders)) then
316 call c_f_pointer(rpa_orders_c, rpa_orders, [size(res%rpa_orders)])
317 rpa_orders = res%rpa_orders
318 end if
319 if (c_associated(eigvals_k_c) .and. allocated(res%mode_eigs_k)) then
320 call c_f_pointer( &
321 eigvals_k_c, eigvals_k, &
322 [size(res%mode_eigs_k, 1), size(res%mode_eigs_k, 2)] &
323 )
324 eigvals_k = res%mode_eigs_k
325 end if
326 if (c_associated(eigvecs_k_c) .and. allocated(res%modes_k)) then
327 call c_f_pointer( &
328 eigvecs_k_c, eigvecs_k, &
329 [size(res%modes_k, 1), size(res%modes_k, 2), size(res%modes_k, 3)] &
330 )
331 eigvecs_k = res%modes_k
332 end if
333 if (c_associated(alpha_0_c) .and. allocated(res%alpha_0)) then
334 call c_f_pointer(alpha_0_c, alpha_0, [size(res%alpha_0)])
335 alpha_0 = res%alpha_0
336 end if
337 if (c_associated(C6_c) .and. allocated(res%C6)) then
338 call c_f_pointer(C6_c, C6, [size(res%C6)])
339 C6 = res%C6
340 end if
341 end subroutine
342
343 subroutine cmbd_destroy_result(res_c) bind(c)
344 type(c_ptr), value, intent(in) :: res_c
345
346 type(result_t), pointer :: res
347
348 call c_f_pointer(res_c, res)
349 deallocate (res)
350 end subroutine
351
352 subroutine cmbd_print_timing(geom_c) bind(c)
353 type(c_ptr), value, intent(in) :: geom_c
354
355 type(geom_t), pointer :: geom
356
357 call c_f_pointer(geom_c, geom)
358 call geom%timer%print()
359 end subroutine
360
361 subroutine cmbd_dipole_matrix(geom_c, damping_c, q_point, dipmat_c) bind(c)
362 type(c_ptr), value, intent(in) :: geom_c
363 type(c_ptr), value, intent(in) :: damping_c
364 real(c_double), optional, intent(in) :: q_point(3)
365 type(c_ptr), value, intent(in) :: dipmat_c
366
367 type(geom_t), pointer :: geom
368 type(damping_t), pointer :: damp
369 type(matrix_re_t) :: dipmat_re
370 type(matrix_cplx_t) :: dipmat_cplx
371 real(dp), pointer :: dipmat_re_p(:, :)
372 complex(dp), pointer :: dipmat_cplx_p(:, :)
373 integer :: n_atoms
374
375 call c_f_pointer(geom_c, geom)
376 n_atoms = size(geom%coords, 2)
377 call c_f_pointer(damping_c, damp)
378 if (present(q_point)) then
379 dipmat_cplx = dipole_matrix(geom, damp, q=q_point)
380 call c_f_pointer(dipmat_c, dipmat_cplx_p, [3 * n_atoms, 3 * n_atoms])
381 dipmat_cplx_p = transpose(dipmat_cplx%val)
382 else
383 dipmat_re = dipole_matrix(geom, damp)
384 call c_f_pointer(dipmat_c, dipmat_re_p, [3 * n_atoms, 3 * n_atoms])
385 dipmat_re_p = transpose(dipmat_re%val)
386 end if
387 end subroutine
388
389 real(c_double) function cmbd_coulomb_energy( &
390 geom_c, n_atoms, q, m, w_t, version, r_vdw, beta, a, C) bind(c)
391 type(c_ptr), value :: geom_c
392 integer(c_int), value, intent(in) :: n_atoms
393 real(c_double), value, intent(in) :: a, beta
394 real(c_double), intent(in) :: C(3 * n_atoms, 3 * n_atoms), &
395 w_t(3 * n_atoms), q(n_atoms), m(n_atoms), r_vdw(n_atoms)
396 character(c_char), intent(in) :: version(20)
397
398 type(geom_t), pointer :: geom
399 type(damping_t) :: damp
400
401 damp%version = f_string(version)
402 damp%r_vdw = r_vdw
403 damp%ts_d = a
404 damp%ts_sr = beta
405 call c_f_pointer(geom_c, geom)
406 cmbd_coulomb_energy = coulomb_energy(geom, q, m, w_t, C, damp)
407 end function
408
409 real(c_double) function cmbd_dipole_energy( &
410 geom_c, n_atoms, a0, w, w_t, version, r_vdw, beta, a, C) bind(c)
411 type(c_ptr), value :: geom_c
412 integer(c_int), value, intent(in) :: n_atoms
413 real(c_double), intent(in) :: C(3 * n_atoms, 3 * n_atoms), &
414 w_t(3 * n_atoms), w(n_atoms), a0(n_atoms), r_vdw(n_atoms)
415 real(c_double), value, intent(in) :: a, beta
416 character(c_char), intent(in) :: version(20)
417
418 type(geom_t), pointer :: geom
419 type(damping_t) :: damp
420
421 damp%version = f_string(version)
422 damp%r_vdw = r_vdw
423 damp%beta = beta
424 damp%a = a
425 call c_f_pointer(geom_c, geom)
426 cmbd_dipole_energy = dipole_energy(geom, a0, w, w_t, C, damp)
427 end function
428
429 subroutine cmbd_nonint_density(geom_c, n_atoms, n_pts, pts, charges, masses, omegas, rho) bind(c)
430 type(c_ptr), value :: geom_c
431 integer(c_int), value, intent(in) :: n_atoms, n_pts
432 real(c_double), intent(in) :: pts(3, n_pts), charges(n_atoms), masses(n_atoms), omegas(n_atoms)
433 real(c_double), intent(out) :: rho(n_pts)
434
435 type(geom_t), pointer :: geom
436
437 call c_f_pointer(geom_c, geom)
438 rho = eval_mbd_nonint_density(geom, pts, charges, masses, omegas)
439 end subroutine
440
441 subroutine cmbd_int_density(geom_c, n_atoms, n_pts, pts, charges, masses, omegas, modes, rho) bind(c)
442 type(c_ptr), value :: geom_c
443 integer(c_int), value, intent(in) :: n_atoms, n_pts
444 real(c_double), intent(in) :: &
445 pts(3, n_pts), charges(n_atoms), masses(n_atoms), &
446 omegas(3 * n_atoms), modes(3 * n_atoms, 3 * n_atoms)
447 real(c_double), intent(out) :: rho(n_pts)
448
449 type(geom_t), pointer :: geom
450
451 call c_f_pointer(geom_c, geom)
452 rho = eval_mbd_int_density(geom, pts, charges, masses, omegas, modes)
453 end subroutine
454
455 function f_string(str_c) result(str_f)
456 character(kind=c_char), intent(in) :: str_c(*)
457 character(len=:), allocatable :: str_f
458
459 integer :: i
460
461 i = 0
462 do
463 if (str_c(i + 1) == c_null_char) exit
464 i = i + 1
465 end do
466 allocate (character(len=i) :: str_f)
467 str_f = transfer(str_c(1:i), str_f)
468 end function
469
470 subroutine f_c_string(str_f, str_c)
471 character(len=*), intent(in) :: str_f
472 character(kind=c_char), intent(out) :: str_c(:)
473
474 integer :: i
475
476 do i = 1, min(len(trim(str_f)), size(str_c) - 1)
477 str_c(i) = str_f(i:i)
478 end do
479 str_c(i) = c_null_char
480 end subroutine
481
482 end module