Octopus
test_isdf.F90
Go to the documentation of this file.
1!! Copyright (C) 2024 - 2025 A Buccheri.
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17
18#include "global.h"
19
21module test_isdf_oct_m
22 use, intrinsic :: iso_fortran_env
23
26 use debug_oct_m
29 use global_oct_m
31 use io_oct_m, only: io_mkdir
33 use lcao_oct_m, only: lcao_run
34 use mesh_oct_m
36 use mpi_oct_m, only: mpi_world
38 use parser_oct_m
40 use scf_oct_m
42 use types_oct_m
44
45 ! Modules under test
49 use isdf_oct_m
51
52 implicit none
53 private
54 public :: test_isdf
55
56contains
57
60 subroutine test_isdf(namespace, serial)
61 type(namespace_t), intent(in) :: namespace
62 logical, optional, intent(in) :: serial
63
64 type(electrons_t), pointer :: sys
65 real(real64), parameter :: LMM_R_SINGLE_ATOM = 100.0_real64
66
67 ! Centroids
68 type(centroids_t) :: centroids
69 integer(int64), allocatable :: indices(:)
70 integer :: n_int
71
72 ! ISDF
73 integer :: n_ks_states
74 type(isdf_options_t) :: isdf_options
75 logical :: output_cubes, run_serial_isdf
76 real(real64), allocatable :: phi_mu(:, :), P_r_mu(:, :)
77 real(real64), allocatable :: isdf_vectors(:, :), gram_matrix(:, :)
78
79 push_sub(test_isdf)
80
81 run_serial_isdf = optional_default(serial, .false.)
82
83 call messages_write('Info: Testing ISDF')
86 call messages_info(namespace=namespace)
87
88 ! Set up a system of electrons
89 call calc_mode_par%set_parallelization(p_strategy_states, default=.false.)
90 sys => electrons_t(namespace, mpi_world)
91
92 ! Initialise a view of the wave functions
93 ! Note, this does not change the underlying data layout of the states
94 call states_elec_allocate_wfns(sys%st, sys%gr, wfs_type = type_float, packed=.true.)
95
96 ! Guess at the states using lcao
97 call lcao_run(namespace, sys%space, sys%gr, sys%ions, sys%ext_partners, &
98 sys%st, sys%ks, sys%hm, lmm_r = lmm_r_single_atom)
99
100 call parse_variable(namespace, 'ACESize', sys%st%nst, n_ks_states)
101
102 call isdf_options%init(namespace, sys%space, sys%gr, n_ks_states)
103
104 call test_isdf_get_interpolation_points(namespace, isdf_options, sys, centroids)
105
106 ! Visualisation exact and approx product functions
107 output_cubes = .false.
108
109 if (run_serial_isdf) then
110 if (sys%st%parallel_in_states .or. sys%gr%parallel_in_domains) then
111 write(message(1), '(A)') 'Reference serial ISDF test requested, but state or domain parallel'
112 write(message(2), '(A)') ' specified in the input.'
113 call messages_fatal(2, namespace=namespace)
114 endif
115
116 indices = centroids%global_mesh_indices()
117 n_int = size(indices)
118
119 call isdf_serial_interpolation_vectors(isdf_options, namespace, sys%gr, sys%st, indices, &
120 phi_mu, p_r_mu, isdf_vectors)
121 safe_deallocate_a(phi_mu)
122 safe_deallocate_a(p_r_mu)
123
124 safe_allocate(gram_matrix(1:n_int, 1:n_int))
125 call isdf_gram_matrix(sys%gr, isdf_vectors, gram_matrix)
126 call output_matrix(namespace, "isdf_gram.dat", gram_matrix)
127
128 ! This deallocates isdf_vectors to save memory
129 call quantify_error_and_visualise(isdf_options, namespace, sys%st, sys%space, sys%gr, &
130 sys%ions, indices, isdf_vectors, output_cubes)
131
132 deallocate(indices)
133
134 else
135 call messages_write('Info: Testing ISDF Parallel Implementation')
136 call messages_new_line()
137 call messages_info(namespace=namespace)
138
139 n_int = centroids%npoints_global()
140 if (mpi_world%is_root()) write(*, *) 'ISDF: Total number of centroids: ', n_int
141 write(*, *) "ISDF: Number of centroids local to domain process ", sys%gr%mpi_grp%rank, " is ", centroids%npoints()
142
143 call isdf_interpolation_vectors(isdf_options, namespace, sys%gr, sys%st, centroids, phi_mu, p_r_mu, isdf_vectors)
144 safe_deallocate_a(phi_mu)
145 safe_deallocate_a(p_r_mu)
146
147 ! Gram matrix of the interpolation vectors offers a quantity reduced over states and domains
148 ! so good for testing parallelisation on
149 safe_allocate(gram_matrix(1:n_int, 1:n_int))
150 call isdf_gram_matrix(sys%gr, isdf_vectors, gram_matrix)
151 call output_matrix(namespace, "isdf_gram.dat", gram_matrix)
152 endif
153
154 ! Tear down
155 safe_deallocate_a(gram_matrix)
156 safe_deallocate_a(isdf_vectors)
157 call states_elec_deallocate_wfns(sys%st)
158 safe_deallocate_p(sys)
159
160 pop_sub(test_isdf)
161
162 end subroutine test_isdf
163
164
166 subroutine test_isdf_get_interpolation_points(namespace, isdf_options, sys, centroids)
167 type(namespace_t), intent(in ) :: namespace
168 type(isdf_options_t), intent(inout) :: isdf_options
169 type(electrons_t), pointer, intent(in ) :: sys
170
171 type(centroids_t), intent(out) :: centroids
172
173 character(len=1), allocatable :: dummy_species(:)
174
176
177 if (debug%info) then
178 allocate(dummy_species(isdf_options%n_interp), source='A')
179 call io_mkdir(static_dir, namespace)
180 call write_standard_xyz_file(namespace, static_dir // "/initial_centroids", isdf_options%interpolation_coords, dummy_species)
181 endif
182
183 call weighted_kmeans(sys%space, sys%gr, sys%st%rho(1:sys%gr%np, 1), isdf_options%interpolation_coords)
184
185 if (debug%info) then
186 dummy_species = 'B'
187 call write_standard_xyz_file(namespace, static_dir // "/final_centroids", isdf_options%interpolation_coords, dummy_species)
188 endif
189
190 ! Convert positions to indices
191 call centroids%init(sys%gr, isdf_options%interpolation_coords, check_duplicates=.true.)
192
193 ! Output the centroid indices so they can be asserted on
194 call centroids%output_all_indices(namespace, sys%grp, "centroid_indices.dat")
195
197
199
200end module test_isdf_oct_m
201
202!! Local Variables:
203!! mode: f90
204!! coding: utf-8
205!! End:
This module handles the calculation mode.
type(calc_mode_par_t), public calc_mode_par
Singleton instance of parallel calculation mode.
integer, parameter, public p_strategy_states
parallelization in states
type(debug_t), save, public debug
Definition: debug.F90:158
character(len= *), parameter, public static_dir
Definition: global.F90:279
subroutine, public write_standard_xyz_file(namespace, fname, pos, species, header)
Write a standard xyz file with atom labels and positions (in Angstrom).
Definition: io.F90:116
subroutine, public io_mkdir(fname, namespace, parents)
Definition: io.F90:361
Interoperable Separable Density Fitting (ISDF) molecular implementation.
Definition: isdf.F90:116
subroutine, public isdf_interpolation_vectors(isdf, namespace, mesh, st, centroids, psi_mu, P_r_mu, isdf_vectors)
Top-level routine for computing ISDF vectors.
Definition: isdf.F90:200
subroutine, public isdf_gram_matrix(mesh, isdf_vectors, gram_matrix)
Compute the Gram matrix for the ISDF interpolation vectors.
Definition: isdf.F90:445
Serial prototype for benchmarking and validating ISDF implementation.
subroutine, public isdf_serial_interpolation_vectors(isdf, namespace, mesh, st, indices, phi_mu, P_r_mu, isdf_vectors)
Construct interpolative separable density fitting (ISDF) vectors and other intermediate quantities re...
subroutine, public quantify_error_and_visualise(isdf, namespace, st, space, mesh, ions, indices, isdf_vectors, output_cubes)
Wrapper for quantifying the error in the expansion of the product basis.
subroutine, public output_matrix(namespace, fname, matrix)
Helper routine to output a 2D matrix.
Definition: isdf_utils.F90:151
subroutine, public weighted_kmeans(space, mesh, weight, centroids, n_iter, centroid_tol, discretize, inertia)
Weighted K-means clustering.
subroutine, public lcao_run(namespace, space, gr, ions, ext_partners, st, ks, hm, st_start, lmm_r, known_lower_bound)
Definition: lcao.F90:769
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:120
subroutine, public messages_new_line()
Definition: messages.F90:1089
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:162
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:410
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:594
type(mpi_grp_t), public mpi_world
Definition: mpi.F90:272
subroutine, public states_elec_deallocate_wfns(st)
Deallocates the KS wavefunctions defined within a states_elec_t structure.
subroutine, public states_elec_allocate_wfns(st, mesh, wfs_type, skip, packed)
Allocates the KS wavefunctions defined within a states_elec_t structure.
Integration tests for ISDF.
Definition: test_isdf.F90:116
subroutine, public test_isdf(namespace, serial)
Set up an electron system, compute some optimal centroid positions, and use these to build a set of I...
Definition: test_isdf.F90:156
subroutine test_isdf_get_interpolation_points(namespace, isdf_options, sys, centroids)
Initialise a system and centroids for molecular ISDF application testing.
Definition: test_isdf.F90:262
type(type_t), parameter, public type_float
Definition: types.F90:135
Encapsulate centroid points and their indexing across a domain-decomposed mesh.
Definition: centroids.F90:159
Class describing the electron system.
Definition: electrons.F90:221
int true(void)