(git:e7e05ae)
pwdft_environment.F
Go to the documentation of this file.
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 Methods and functions on the PWDFT environment
10 !> \par History
11 !> 07.2018 initial create
12 !> \author JHU
13 ! **************************************************************************************************
15  USE atomic_kind_types, ONLY: atomic_kind_type
16  USE cell_types, ONLY: cell_type
19  cp_logger_type
20  USE cp_symmetry, ONLY: write_symmetry
22  distribution_1d_type
24  USE header, ONLY: sirius_header
26  section_vals_type
27  USE kinds, ONLY: dp
28  USE machine, ONLY: m_flush
29  USE message_passing, ONLY: mp_para_env_type
30  USE molecule_kind_types, ONLY: molecule_kind_type
31  USE molecule_types, ONLY: molecule_type
35  USE particle_types, ONLY: particle_type
38  pwdft_environment_type
40  qs_energy_type
41  USE qs_kind_types, ONLY: qs_kind_type,&
44  USE qs_subsys_types, ONLY: qs_subsys_get,&
46  qs_subsys_type
50  USE virial_types, ONLY: virial_type
51 #include "./base/base_uses.f90"
52 
53  IMPLICIT NONE
54 
55  PRIVATE
56 
57 ! *** Global parameters ***
58 
59  CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'pwdft_environment'
60 
61 ! *** Public subroutines ***
62 
64 
65 CONTAINS
66 
67 ! **************************************************************************************************
68 !> \brief Initialize the pwdft environment
69 !> \param pwdft_env The pwdft environment to retain
70 !> \param root_section ...
71 !> \param para_env ...
72 !> \param force_env_section ...
73 !> \param subsys_section ...
74 !> \param use_motion_section ...
75 !> \par History
76 !> 03.2018 initial create
77 !> \author JHU
78 ! **************************************************************************************************
79  SUBROUTINE pwdft_init(pwdft_env, root_section, para_env, force_env_section, subsys_section, &
80  use_motion_section)
81  TYPE(pwdft_environment_type), POINTER :: pwdft_env
82  TYPE(section_vals_type), POINTER :: root_section
83  TYPE(mp_para_env_type), POINTER :: para_env
84  TYPE(section_vals_type), POINTER :: force_env_section, subsys_section
85  LOGICAL, INTENT(IN) :: use_motion_section
86 
87  CHARACTER(len=*), PARAMETER :: routinen = 'pwdft_init'
88 
89  INTEGER :: handle, iw, natom
90  LOGICAL :: use_ref_cell
91  TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
92  TYPE(cell_type), POINTER :: my_cell, my_cell_ref
93  TYPE(cp_logger_type), POINTER :: logger
94  TYPE(distribution_1d_type), POINTER :: local_molecules, local_particles
95  TYPE(molecule_kind_type), DIMENSION(:), POINTER :: molecule_kind_set
96  TYPE(molecule_type), DIMENSION(:), POINTER :: molecule_set
97  TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
98  TYPE(qs_energy_type), POINTER :: energy
99  TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
100  TYPE(qs_subsys_type), POINTER :: qs_subsys
101  TYPE(section_vals_type), POINTER :: pwdft_section, xc_section
102 
103  CALL timeset(routinen, handle)
104 
105  cpassert(ASSOCIATED(pwdft_env))
106  cpassert(ASSOCIATED(force_env_section))
107 
108  IF (.NOT. ASSOCIATED(subsys_section)) THEN
109  subsys_section => section_vals_get_subs_vals(force_env_section, "SUBSYS")
110  END IF
111  pwdft_section => section_vals_get_subs_vals(force_env_section, "PW_DFT")
112 
113  ! retrieve the functionals parameters
114  xc_section => section_vals_get_subs_vals(force_env_section, "DFT%XC%XC_FUNCTIONAL")
115 
116  CALL pwdft_env_set(pwdft_env=pwdft_env, pwdft_input=pwdft_section, &
117  force_env_input=force_env_section, xc_input=xc_section)
118 
119  ! parallel environment
120  CALL pwdft_env_set(pwdft_env=pwdft_env, para_env=para_env)
121 
122  NULLIFY (qs_subsys)
123  ALLOCATE (qs_subsys)
124  CALL qs_subsys_create(qs_subsys, para_env, &
125  force_env_section=force_env_section, &
126  subsys_section=subsys_section, &
127  use_motion_section=use_motion_section, &
128  root_section=root_section)
129 
130  ! Distribute molecules and atoms
131  NULLIFY (local_molecules)
132  NULLIFY (local_particles)
133  CALL qs_subsys_get(qs_subsys, particle_set=particle_set, &
134  atomic_kind_set=atomic_kind_set, &
135  molecule_set=molecule_set, &
136  molecule_kind_set=molecule_kind_set)
137 
138  CALL distribute_molecules_1d(atomic_kind_set=atomic_kind_set, &
139  particle_set=particle_set, &
140  local_particles=local_particles, &
141  molecule_kind_set=molecule_kind_set, &
142  molecule_set=molecule_set, &
143  local_molecules=local_molecules, &
144  force_env_section=force_env_section)
145 
146  CALL qs_subsys_set(qs_subsys, local_molecules=local_molecules, &
147  local_particles=local_particles)
148  CALL distribution_1d_release(local_particles)
149  CALL distribution_1d_release(local_molecules)
150 
151  CALL pwdft_env_set(pwdft_env=pwdft_env, qs_subsys=qs_subsys)
152 
153  CALL qs_subsys_get(qs_subsys, cell=my_cell, cell_ref=my_cell_ref, use_ref_cell=use_ref_cell)
154 
155  NULLIFY (logger)
156  logger => cp_get_default_logger()
157  iw = cp_logger_get_default_io_unit(logger)
158  CALL sirius_header(iw)
159 
160  NULLIFY (energy)
161  CALL allocate_qs_energy(energy)
162  CALL qs_subsys_set(qs_subsys, energy=energy)
163 
164  CALL qs_subsys_get(qs_subsys, particle_set=particle_set, &
165  qs_kind_set=qs_kind_set, &
166  atomic_kind_set=atomic_kind_set, &
167  molecule_set=molecule_set, &
168  molecule_kind_set=molecule_kind_set)
169 
170  ! init energy, force, stress arrays
171  CALL qs_subsys_get(qs_subsys, natom=natom)
172  ALLOCATE (pwdft_env%energy)
173  ALLOCATE (pwdft_env%forces(natom, 3))
174  pwdft_env%forces(1:natom, 1:3) = 0.0_dp
175  pwdft_env%stress(1:3, 1:3) = 0.0_dp
176 
177  ! Print the atomic kind set
178  CALL write_qs_kind_set(qs_kind_set, subsys_section)
179  ! Print the atomic coordinates
180  CALL write_qs_particle_coordinates(particle_set, qs_kind_set, subsys_section, label="PWDFT/SIRIUS")
181  ! Print the interatomic distances
182  CALL write_particle_distances(particle_set, my_cell, subsys_section)
183  ! Print the requested structure data
184  CALL write_structure_data(particle_set, my_cell, subsys_section)
185  ! Print symmetry information
186  CALL write_symmetry(particle_set, my_cell, subsys_section)
187 
188  ! Sirius initialization
189  CALL cp_sirius_create_env(pwdft_env)
190 
191  IF (iw > 0) THEN
192  WRITE (iw, '(A)') "SIRIUS| INIT: FINISHED"
193  END IF
194  IF (iw > 0) CALL m_flush(iw)
195 
196  CALL timestop(handle)
197 
198  END SUBROUTINE pwdft_init
199 
200 ! **************************************************************************************************
201 !> \brief Calculate energy and forces within the PWDFT/SIRIUS code
202 !> \param pwdft_env The pwdft environment to retain
203 !> \param calculate_forces ...
204 !> \param calculate_stress ...
205 !> \par History
206 !> 03.2018 initial create
207 !> \author JHU
208 ! **************************************************************************************************
209  SUBROUTINE pwdft_calc_energy_force(pwdft_env, calculate_forces, calculate_stress)
210  TYPE(pwdft_environment_type), POINTER :: pwdft_env
211  LOGICAL, INTENT(IN) :: calculate_forces, calculate_stress
212 
213  CHARACTER(len=*), PARAMETER :: routinen = 'pwdft_calc_energy_force'
214 
215  INTEGER :: handle, iatom, iw, natom
216  REAL(kind=dp), DIMENSION(1:3, 1:3) :: stress
217  REAL(kind=dp), DIMENSION(:, :), POINTER :: force
218  TYPE(cell_type), POINTER :: my_cell
219  TYPE(cp_logger_type), POINTER :: logger
220  TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
221  TYPE(qs_subsys_type), POINTER :: qs_subsys
222  TYPE(virial_type), POINTER :: virial
223 
224  CALL timeset(routinen, handle)
225 
226  cpassert(ASSOCIATED(pwdft_env))
227 
228  NULLIFY (logger)
229  logger => cp_get_default_logger()
230  iw = cp_logger_get_default_io_unit(logger)
231 
232  ! update context for new positions/cell
233  ! Sirius initialization
234  CALL cp_sirius_update_context(pwdft_env)
235  IF (iw > 0) THEN
236  WRITE (iw, '(A)') "SIRIUS| UPDATE CONTEXT : FINISHED"
237  END IF
238  IF (iw > 0) CALL m_flush(iw)
239 
240  ! calculate energy and forces/stress
241  CALL cp_sirius_energy_force(pwdft_env, calculate_forces, calculate_stress)
242 
243  IF (calculate_forces) THEN
244  CALL pwdft_env_get(pwdft_env=pwdft_env, qs_subsys=qs_subsys)
245  CALL pwdft_env_get(pwdft_env=pwdft_env, forces=force)
246  CALL qs_subsys_get(qs_subsys, particle_set=particle_set, natom=natom)
247  DO iatom = 1, natom
248  particle_set(iatom)%f(1:3) = -force(iatom, 1:3)
249  END DO
250  END IF
251 
252  IF (calculate_stress) THEN
253  ! i need to retrieve the volume of the unit cell for the stress tensor
254  CALL qs_subsys_get(qs_subsys, cell=my_cell, virial=virial)
255  CALL pwdft_env_get(pwdft_env=pwdft_env, stress=stress)
256  virial%pv_virial(1:3, 1:3) = -stress(1:3, 1:3)*my_cell%deth
257  END IF
258 
259  CALL timestop(handle)
260 
261  END SUBROUTINE pwdft_calc_energy_force
262 END MODULE pwdft_environment
Define the atomic kind types and their sub types.
Handles all functions related to the CELL.
Definition: cell_types.F:15
various routines to log and control the output. The idea is that decisions about where to log should ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
Work with symmetry.
Definition: cp_symmetry.F:13
subroutine, public write_symmetry(particle_set, cell, input_section)
Write symmetry information to output.
Definition: cp_symmetry.F:61
stores a lists of integer that are local to a processor. The idea is that these integers represent ob...
subroutine, public distribution_1d_release(distribution_1d)
releases the given distribution_1d
Distribution methods for atoms, particles, or molecules.
subroutine, public distribute_molecules_1d(atomic_kind_set, particle_set, local_particles, molecule_kind_set, molecule_set, local_molecules, force_env_section, prev_molecule_kind_set, prev_local_molecules)
Distribute molecules and particles.
Definition: header.F:13
subroutine, public sirius_header(iw)
...
Definition: header.F:400
objects that represent the structure of input sections and the data contained in an input section
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
Defines the basic variable types.
Definition: kinds.F:23
integer, parameter, public dp
Definition: kinds.F:34
Machine interface based on Fortran 2003 and POSIX.
Definition: machine.F:17
subroutine, public m_flush(lunit)
flushes units if the &GLOBAL flag is set accordingly
Definition: machine.F:106
Interface to the message passing library MPI.
Define the molecule kind structure types and the corresponding functionality.
Define the data structure for the molecule information.
Define methods related to particle_type.
subroutine, public write_qs_particle_coordinates(particle_set, qs_kind_set, subsys_section, label)
Write the atomic coordinates to the output unit.
subroutine, public write_structure_data(particle_set, cell, input_section)
Write structure data requested by a separate structure data input section to the output unit....
subroutine, public write_particle_distances(particle_set, cell, subsys_section)
Write the matrix of the particle distances to the output unit.
Define the data structure for the particle information.
The type definitions for the PWDFT environment.
subroutine, public pwdft_env_get(pwdft_env, pwdft_input, force_env_input, xc_input, cp_subsys, qs_subsys, para_env, energy, forces, stress, sctx, gs_handler, ks_handler)
Returns various attributes of the pwdft environment.
subroutine, public pwdft_env_set(pwdft_env, pwdft_input, force_env_input, xc_input, qs_subsys, cp_subsys, para_env, energy, forces, stress, sctx, gs_handler, ks_handler)
Sets various attributes of the pwdft environment.
Methods and functions on the PWDFT environment.
subroutine, public pwdft_calc_energy_force(pwdft_env, calculate_forces, calculate_stress)
Calculate energy and forces within the PWDFT/SIRIUS code.
subroutine, public pwdft_init(pwdft_env, root_section, para_env, force_env_section, subsys_section, use_motion_section)
Initialize the pwdft environment.
subroutine, public allocate_qs_energy(qs_energy)
Allocate and/or initialise a Quickstep energy data structure.
Define the quickstep kind type and their sub types.
Definition: qs_kind_types.F:23
subroutine, public write_qs_kind_set(qs_kind_set, subsys_section)
Write an atomic kind set data set to the output unit.
Routines that work on qs_subsys_type.
subroutine, public qs_subsys_create(subsys, para_env, root_section, force_env_section, subsys_section, use_motion_section, cp_subsys, cell, cell_ref, elkind)
Creates a qs_subsys. Optionally an existsing cp_subsys is used.
types that represent a quickstep subsys
subroutine, public qs_subsys_get(subsys, atomic_kinds, atomic_kind_set, particles, particle_set, local_particles, molecules, molecule_set, molecule_kinds, molecule_kind_set, local_molecules, para_env, colvar_p, shell_particles, core_particles, gci, multipoles, natom, nparticle, ncore, nshell, nkind, atprop, virial, results, cell, cell_ref, use_ref_cell, energy, force, qs_kind_set, cp_subsys, nelectron_total, nelectron_spin)
...
subroutine, public qs_subsys_set(subsys, cp_subsys, local_particles, local_molecules, cell, cell_ref, use_ref_cell, energy, force, qs_kind_set, nelectron_total, nelectron_spin)
...
Interface to the SIRIUS Library.
subroutine, public cp_sirius_update_context(pwdft_env)
Empty implementation in case SIRIUS is not compiled in.
subroutine, public cp_sirius_energy_force(pwdft_env, calculate_forces, calculate_stress)
Empty implementation in case SIRIUS is not compiled in.
subroutine, public cp_sirius_create_env(pwdft_env)
Empty implementation in case SIRIUS is not compiled in.