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 comm_oct_m
27 use debug_oct_m
30 use global_oct_m
34 use lcao_oct_m, only: lcao_run
35 use mesh_oct_m
37 use mpi_oct_m, only: mpi_world, mpi_double_precision
39 use parser_oct_m
41 use scf_oct_m
43 use types_oct_m
46
47 ! Modules under test
51 use isdf_oct_m
53
54 implicit none
55 private
56 public :: test_isdf
57
58contains
59
62 subroutine test_isdf(namespace, serial)
63 type(namespace_t), intent(in) :: namespace
64 logical, optional, intent(in) :: serial
65
66 type(electrons_t), pointer :: sys
67 real(real64), parameter :: LMM_R_SINGLE_ATOM = 100.0_real64
68
69 ! Centroids
70 type(centroids_t) :: centroids
71 integer(int64), allocatable :: indices(:)
72 integer :: n_int
73
74 ! ISDF
75 integer :: n_ks_states
76 type(isdf_options_t) :: isdf_options
77 logical :: output_cubes, run_serial_isdf
78 real(real64), allocatable :: phi_mu(:, :), P_r_mu(:, :)
79 real(real64), allocatable :: isdf_vectors(:, :), gram_matrix(:, :)
80
81 push_sub(test_isdf)
82
83 run_serial_isdf = optional_default(serial, .false.)
84
85 call messages_write('Info: Testing ISDF')
88 call messages_info(namespace=namespace)
89
90 ! Set up a system of electrons
91 call calc_mode_par%set_parallelization(p_strategy_states, default=.false.)
92 sys => electrons_t(namespace)
93 call sys%init_parallelization(mpi_world)
94
95 ! Initialise a view of the wave functions
96 ! Note, this does not change the underlying data layout of the states
97 call states_elec_allocate_wfns(sys%st, sys%gr, wfs_type = type_float, packed=.true.)
98
99 ! Guess at the states using lcao
100 call lcao_run(namespace, sys%space, sys%gr, sys%ions, sys%ext_partners, &
101 sys%st, sys%ks, sys%hm, lmm_r = lmm_r_single_atom)
102
103 call parse_variable(namespace, 'ACESize', sys%st%nst, n_ks_states)
104
105 call isdf_options%init(namespace, sys%space, sys%gr, n_ks_states)
106
107 call test_isdf_get_interpolation_points(namespace, isdf_options, sys, centroids)
108
109 ! Visualisation exact and approx product functions
110 output_cubes = .false.
111
112 if (run_serial_isdf) then
113 if (sys%st%parallel_in_states .or. sys%gr%parallel_in_domains) then
114 write(message(1), '(A)') 'Reference serial ISDF test requested, but state or domain parallel'
115 write(message(2), '(A)') ' specified in the input.'
116 call messages_fatal(2, namespace=namespace)
117 endif
118
119 indices = centroids%global_mesh_indices()
120 n_int = size(indices)
121
122 call isdf_serial_interpolation_vectors(isdf_options, namespace, sys%gr, sys%st, indices, &
123 phi_mu, p_r_mu, isdf_vectors)
124 safe_deallocate_a(phi_mu)
125 safe_deallocate_a(p_r_mu)
126
127 safe_allocate(gram_matrix(1:n_int, 1:n_int))
128 call isdf_gram_matrix(sys%gr, isdf_vectors, gram_matrix)
129 call output_matrix(namespace, "isdf_gram.dat", gram_matrix)
130
131 ! This deallocates isdf_vectors to save memory
132 call quantify_error_and_visualise(isdf_options, namespace, sys%st, sys%space, sys%gr, &
133 sys%ions, indices, isdf_vectors, output_cubes)
134
135 deallocate(indices)
136
137 else
138 call messages_write('Info: Testing ISDF Parallel Implementation')
139 call messages_new_line()
140 call messages_info(namespace=namespace)
141
142 n_int = centroids%npoints_global()
143 if (mpi_world%is_root()) write(*, *) 'ISDF: Total number of centroids: ', n_int
144 write(*, *) "ISDF: Number of centroids local to domain process ", sys%gr%mpi_grp%rank, " is ", centroids%npoints()
145
146 call isdf_interpolation_vectors(isdf_options, namespace, sys%gr, sys%st, centroids, phi_mu, p_r_mu, isdf_vectors)
147 safe_deallocate_a(phi_mu)
148 safe_deallocate_a(p_r_mu)
149
150 ! Gram matrix of the interpolation vectors offers a quantity reduced over states and domains
151 ! so good for testing parallelisation on
152 safe_allocate(gram_matrix(1:n_int, 1:n_int))
153 call isdf_gram_matrix(sys%gr, isdf_vectors, gram_matrix)
154 call output_matrix(namespace, "isdf_gram.dat", gram_matrix)
155 endif
156
157 ! Tear down
158 safe_deallocate_a(gram_matrix)
159 safe_deallocate_a(isdf_vectors)
160 call states_elec_deallocate_wfns(sys%st)
161 safe_deallocate_p(sys)
162
163 pop_sub(test_isdf)
164
165 end subroutine test_isdf
166
167
169 subroutine test_isdf_get_interpolation_points(namespace, isdf_options, sys, centroids)
170 type(namespace_t), intent(in ) :: namespace
171 type(isdf_options_t), intent(inout) :: isdf_options
172 type(electrons_t), pointer, intent(in ) :: sys
173
174 type(centroids_t), intent(out) :: centroids
175
176 character(len=1), allocatable :: dummy_species(:)
177
179
180 if (debug%info) then
181 allocate(dummy_species(isdf_options%n_interp), source='A')
182 call io_mkdir(static_dir, namespace)
183 call write_standard_xyz_file(namespace, static_dir // "/initial_centroids", isdf_options%interpolation_coords, dummy_species)
184 endif
185
186 call weighted_kmeans(sys%space, sys%gr, sys%st%rho(1:sys%gr%np, 1), isdf_options%interpolation_coords)
187
188 if (debug%info) then
189 dummy_species = 'B'
190 call write_standard_xyz_file(namespace, static_dir // "/final_centroids", isdf_options%interpolation_coords, dummy_species)
191 endif
192
193 ! Convert positions to indices
194 call centroids%init(sys%gr, isdf_options%interpolation_coords, check_duplicates=.true.)
195
196 ! Output the centroid indices so they can be asserted on
197 call centroids%output_all_indices(namespace, sys%grp, "centroid_indices.dat")
198
200
202
203end module test_isdf_oct_m
204
205!! Local Variables:
206!! mode: f90
207!! coding: utf-8
208!! 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:266
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_close(iunit, grp)
Definition: io.F90:467
subroutine, public io_mkdir(fname, namespace, parents)
Definition: io.F90:361
integer function, public io_open(file, namespace, action, status, form, position, die, recl, grp)
Definition: io.F90:402
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:197
subroutine, public isdf_gram_matrix(mesh, isdf_vectors, gram_matrix)
Compute the Gram matrix for the ISDF interpolation vectors.
Definition: isdf.F90:442
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:148
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:770
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:120
subroutine, public messages_new_line()
Definition: messages.F90:1112
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:158
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:265
type(type_t), public type_float
Definition: types.F90:135
This module defines the unit system, used for input and output.
type(unit_t), public unit_angstrom
For XYZ files.
type(unit_t), public unit_one
some special units required for particular quantities
Encapsulate centroid points and their indexing across a domain-decomposed mesh.
Definition: centroids.F90:159
Class describing the electron system.
Definition: electrons.F90:220
int true(void)