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 ! If generate_epot is false, the code will crash in v_ks_calc
93 sys => electrons_t(namespace, generate_epot=.true.)
94 call sys%init_parallelization(mpi_world)
95
96 ! Initialise a view of the wave functions
97 ! Note, this does not change the underlying data layout of the states
98 call states_elec_allocate_wfns(sys%st, sys%gr, wfs_type = type_float, packed=.true.)
99
100 ! Guess at the states using lcao
101 call lcao_run(namespace, sys%space, sys%gr, sys%ions, sys%ext_partners, &
102 sys%st, sys%ks, sys%hm, lmm_r = lmm_r_single_atom)
103
104 call parse_variable(namespace, 'ACESize', sys%st%nst, n_ks_states)
105
106 call isdf_options%init(namespace, sys%space, sys%st, sys%gr, n_ks_states)
107
108 call test_isdf_get_interpolation_points(namespace, isdf_options, sys, centroids)
109
110 ! Visualisation exact and approx product functions
111 output_cubes = .false.
112
113 if (run_serial_isdf) then
114 if (sys%st%parallel_in_states .or. sys%gr%parallel_in_domains) then
115 write(message(1), '(A)') 'Reference serial ISDF test requested, but state or domain parallel'
116 write(message(2), '(A)') ' specified in the input.'
117 call messages_fatal(2, namespace=namespace)
118 endif
119
120 indices = centroids%global_mesh_indices()
121 n_int = size(indices)
122
123 call isdf_serial_interpolation_vectors(isdf_options, namespace, sys%gr, sys%st, indices, &
124 phi_mu, p_r_mu, isdf_vectors)
125 safe_deallocate_a(phi_mu)
126 safe_deallocate_a(p_r_mu)
127
128 safe_allocate(gram_matrix(1:n_int, 1:n_int))
129 call isdf_gram_matrix(sys%gr, isdf_vectors, gram_matrix)
130 call output_matrix(namespace, "isdf_gram.dat", gram_matrix)
131
132 ! This deallocates isdf_vectors to save memory
133 call quantify_error_and_visualise(isdf_options, namespace, sys%st, sys%space, sys%gr, &
134 sys%ions, indices, isdf_vectors, output_cubes)
135
136 deallocate(indices)
137
138 else
139 call messages_write('Info: Testing ISDF Parallel Implementation')
140 call messages_new_line()
141 call messages_info(namespace=namespace)
142
143 n_int = centroids%npoints_global()
144 if (mpi_world%is_root()) write(*, *) 'ISDF: Total number of centroids: ', n_int
145 write(*, *) "ISDF: Number of centroids local to domain process ", sys%gr%mpi_grp%rank, " is ", centroids%npoints()
146
147 call isdf_interpolation_vectors(isdf_options, namespace, sys%gr, sys%st, centroids, phi_mu, p_r_mu, isdf_vectors)
148 safe_deallocate_a(phi_mu)
149 safe_deallocate_a(p_r_mu)
150
151 ! Gram matrix of the interpolation vectors offers a quantity reduced over states and domains
152 ! so good for testing parallelisation on
153 safe_allocate(gram_matrix(1:n_int, 1:n_int))
154 call isdf_gram_matrix(sys%gr, isdf_vectors, gram_matrix)
155 call output_matrix(namespace, "isdf_gram.dat", gram_matrix)
156 endif
158 ! Tear down
159 safe_deallocate_a(gram_matrix)
160 safe_deallocate_a(isdf_vectors)
161 call states_elec_deallocate_wfns(sys%st)
162 safe_deallocate_p(sys)
163
164 pop_sub(test_isdf)
165
166 end subroutine test_isdf
167
168
170 subroutine test_isdf_get_interpolation_points(namespace, isdf_options, sys, centroids)
171 type(namespace_t), intent(in ) :: namespace
172 type(isdf_options_t), intent(inout) :: isdf_options
173 type(electrons_t), pointer, intent(in ) :: sys
174
175 type(centroids_t), intent(out) :: centroids
176
177 character(len=1), allocatable :: dummy_species(:)
178
180
181 if (debug%info) then
182 allocate(dummy_species(isdf_options%n_interp), source='A')
183 call io_mkdir(static_dir, namespace)
184 call write_standard_xyz_file(namespace, static_dir // "/initial_centroids", isdf_options%interpolation_coords, dummy_species)
185 endif
186
187 call weighted_kmeans(sys%space, sys%gr, sys%st%rho(1:sys%gr%np, 1), isdf_options%interpolation_coords)
188
189 if (debug%info) then
190 dummy_species = 'B'
191 call write_standard_xyz_file(namespace, static_dir // "/final_centroids", isdf_options%interpolation_coords, dummy_species)
192 endif
193
194 ! Convert positions to indices
195 call centroids%init(sys%gr, isdf_options%interpolation_coords, check_duplicates=.true.)
196
197 ! Output the centroid indices so they can be asserted on
198 call centroids%output_all_indices(namespace, sys%grp, "centroid_indices.dat")
199
201
203
204end module test_isdf_oct_m
205
206!! Local Variables:
207!! mode: f90
208!! coding: utf-8
209!! 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:265
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:466
subroutine, public io_mkdir(fname, namespace, parents)
Definition: io.F90:360
integer function, public io_open(file, namespace, action, status, form, position, die, recl, grp)
Definition: io.F90:401
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)
Definition: lcao.F90:768
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:120
subroutine, public messages_new_line()
Definition: messages.F90:1118
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:416
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:600
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:266
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)