(git:e887d2c)
Loading...
Searching...
No Matches
md_util.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief Utilities for Molecular Dynamics
10!> \author Teodoro Laino [tlaino] - University of Zurich - 09.2007
11! **************************************************************************************************
12MODULE md_util
13
14 USE cp_files, ONLY: close_file,&
20 USE cp_units, ONLY: cp_unit_from_cp2k,&
22 USE fparser, ONLY: evalerrtype,&
23 evalf,&
24 finalizef,&
25 initf,&
26 parsef
31 USE kinds, ONLY: default_path_length,&
33 dp
38 USE simpar_types, ONLY: simpar_type
42#include "../base/base_uses.f90"
43
44 IMPLICIT NONE
45
46 PRIVATE
47
48! *** Global parameters ***
49
50 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'md_util'
51
52 PUBLIC :: md_output, &
55
56CONTAINS
57
58! **************************************************************************************************
59!> \brief update_expected_temperature
60!> \param md_env ...
61!> \date 03.2026
62!> \par Note on units: t_region%temperature is updated by
63!> SUBROUTINE scale_velocity_region in md_vel_utils.F
64!> which is in Kelvin, but t_region%temp_expected is
65!> in cp2k internal units. See "Note the unit conversion
66!> here" in SUBROUTINE print_thermal_regions_temperature
67!> in thermal_region_utils.F for output processing.
68!> \author HE Zilong
69! **************************************************************************************************
70 SUBROUTINE update_expected_temperature(md_env)
71 TYPE(md_environment_type), POINTER :: md_env
72
73 CHARACTER(LEN=default_string_length) :: my_itimes, my_region
74 INTEGER :: ireg
75 INTEGER, POINTER :: itimes
76 TYPE(simpar_type), POINTER :: simpar
77 TYPE(thermal_region_type), POINTER :: t_region
78 TYPE(thermal_regions_type), POINTER :: thermal_regions
79
80 NULLIFY (itimes, simpar)
81 CALL get_md_env(md_env, itimes=itimes, simpar=simpar)
82
83 ! Update thermal regions
84 IF (simpar%do_thermal_region) THEN
85 NULLIFY (thermal_regions)
86 CALL get_md_env(md_env, thermal_regions=thermal_regions)
87 DO ireg = 1, thermal_regions%nregions
88 NULLIFY (t_region)
89 t_region => thermal_regions%thermal_region(ireg)
90 IF (len_trim(t_region%temperature_function) > 0) THEN
91 CALL initf(1)
92 CALL parsef(1, trim(t_region%temperature_function), t_region%temperature_function_parameters)
93 t_region%temperature_function_values(1) = itimes*1.0_dp
94 t_region%temperature_function_values(2) = t_region%temperature
95 t_region%temp_expected = cp_unit_to_cp2k(evalf(1, t_region%temperature_function_values), "K")
96 cpassert(evalerrtype <= 0)
97 CALL finalizef()
98 IF (t_region%temp_expected < 0.0_dp) THEN
99 CALL integer_to_string(ireg, my_region)
100 CALL integer_to_string(itimes, my_itimes)
101 CALL cp_abort(__location__, &
102 "At step "//trim(my_itimes)//" , the temperature "// &
103 "evaluated for thermal region "//trim(my_region)//" is "// &
104 trim(adjustl(cp_to_string(cp_unit_from_cp2k(t_region%temp_expected, "K"))))// &
105 " K, which is negative and unphysical!")
106 END IF
107 END IF
108 END DO
109 END IF
110
111 ! Update thermostat regions: WIP
112
113 END SUBROUTINE update_expected_temperature
114
115! **************************************************************************************************
116!> \brief collects the part of the MD that, basically, does the output
117!> \param md_env ...
118!> \param md_section ...
119!> \param root_section ...
120!> \param forced_io ...
121!> \par History
122!> 03.2006 created [Joost VandeVondele]
123! **************************************************************************************************
124 SUBROUTINE md_output(md_env, md_section, root_section, forced_io)
125 TYPE(md_environment_type), POINTER :: md_env
126 TYPE(section_vals_type), POINTER :: md_section, root_section
127 LOGICAL, INTENT(IN) :: forced_io
128
129 CHARACTER(LEN=*), PARAMETER :: routinen = 'md_output'
130
131 INTEGER :: handle
132 LOGICAL :: do_print
133 TYPE(section_vals_type), POINTER :: print_section
134
135 CALL timeset(routinen, handle)
136 do_print = .true.
137 IF (forced_io) THEN
138 print_section => section_vals_get_subs_vals(md_section, "PRINT")
139 CALL section_vals_val_get(print_section, "FORCE_LAST", l_val=do_print)
140 END IF
141 IF (do_print) THEN
142 ! Dumps all files related to the MD run
143 CALL md_write_output(md_env)
144 CALL write_restart(md_env=md_env, root_section=root_section)
145 END IF
146 CALL timestop(handle)
147
148 END SUBROUTINE md_output
149
150! **************************************************************************************************
151!> \brief read eigenvalues and eigenvectors of Hessian from vibrational analysis results, for use
152!> of initialising MD simulations. Expects to read an unformatted binary file
153!> \param md_section : input section object containing MD subsections and keywords. This should
154!> provide the filename to read vib analysis eigenvalues and eigenvectors.
155!> If the filename is not explicitly specified by the user in the input, then
156!> it will use the default CARTESIAN_EIGS print key filename defined in the
157!> vibrational analysis input section as the filename.
158!> \param vib_section : input section object containing vibrational analysis subsections
159!> and keywords
160!> \param para_env : cp2k mpi environment object, needed for IO in parallel computations
161!> \param dof : outputs the total number of eigenvalues (no. degrees of freedom) read from the file
162!> \param eigenvalues : outputs the eigenvalues (Cartesian frequencies) read from the file
163!> \param eigenvectors : outputs the corresponding eigenvectors read from the file
164!> \author Lianheng Tong, lianheng.tong@kcl.ac.uk
165! **************************************************************************************************
166 SUBROUTINE read_vib_eigs_unformatted(md_section, &
167 vib_section, &
168 para_env, &
169 dof, &
170 eigenvalues, &
171 eigenvectors)
172 TYPE(section_vals_type), POINTER :: md_section, vib_section
173 TYPE(mp_para_env_type), POINTER :: para_env
174 INTEGER, INTENT(OUT) :: dof
175 REAL(kind=dp), DIMENSION(:), INTENT(OUT) :: eigenvalues
176 REAL(kind=dp), DIMENSION(:, :), INTENT(OUT) :: eigenvectors
177
178 CHARACTER(LEN=default_path_length) :: filename
179 INTEGER :: jj, n_rep_val, unit_nr
180 LOGICAL :: exist
181 TYPE(cp_logger_type), POINTER :: logger
182 TYPE(section_vals_type), POINTER :: print_key
183
184 logger => cp_get_default_logger()
185 dof = 0
186 eigenvalues = 0.0_dp
187 eigenvectors = 0.0_dp
188 ! obtain file name
189 CALL section_vals_val_get(md_section, "INITIAL_VIBRATION%VIB_EIGS_FILE_NAME", &
190 n_rep_val=n_rep_val)
191 IF (n_rep_val > 0) THEN
192 CALL section_vals_val_get(md_section, "INITIAL_VIBRATION%VIB_EIGS_FILE_NAME", c_val=filename)
193 ELSE
194 print_key => section_vals_get_subs_vals(vib_section, "PRINT%CARTESIAN_EIGS")
195 filename = cp_print_key_generate_filename(logger, print_key, extension="eig", &
196 my_local=.false.)
197 END IF
198 ! read file
199 IF (para_env%is_source()) THEN
200 INQUIRE (file=filename, exist=exist)
201 IF (.NOT. exist) THEN
202 cpabort("File "//filename//" is not found.")
203 END IF
204 CALL open_file(file_name=filename, &
205 file_action="READ", &
206 file_form="UNFORMATTED", &
207 file_status="OLD", &
208 unit_number=unit_nr)
209 ! the first record contains one integer giving degrees of freedom
210 READ (unit_nr) dof
211 IF (dof > SIZE(eigenvalues)) THEN
212 cpabort("Too many DoFs found in "//filename)
213 END IF
214 ! the second record contains the eigenvalues
215 READ (unit_nr) eigenvalues(1:dof)
216 ! the rest of the records contain the eigenvectors
217 DO jj = 1, dof
218 READ (unit_nr) eigenvectors(1:dof, jj)
219 END DO
220 END IF
221 ! broadcast to all compulational nodes. note that it is assumed
222 ! that source is the ionode
223 CALL para_env%bcast(dof)
224 CALL para_env%bcast(eigenvalues)
225 CALL para_env%bcast(eigenvectors)
226 ! close file
227 IF (para_env%is_source()) THEN
228 CALL close_file(unit_number=unit_nr)
229 END IF
230 END SUBROUTINE read_vib_eigs_unformatted
231
232END MODULE md_util
Utility routines to open and close files. Tracking of preconnections.
Definition cp_files.F:16
subroutine, public open_file(file_name, file_status, file_form, file_action, file_position, file_pad, unit_number, debug, skip_get_unit_number, file_access)
Opens the requested file using a free unit number.
Definition cp_files.F:311
subroutine, public close_file(unit_number, file_status, keep_preconnection)
Close an open file given by its logical unit number. Optionally, keep the file and unit preconnected.
Definition cp_files.F:122
various routines to log and control the output. The idea is that decisions about where to log should ...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
character(len=default_path_length) function, public cp_print_key_generate_filename(logger, print_key, middle_name, extension, my_local)
Utility function that returns a unit number to write the print key. Might open a file with a unique f...
unit conversion facility
Definition cp_units.F:30
real(kind=dp) function, public cp_unit_from_cp2k(value, unit_str, defaults, power)
converts from the internal cp2k units to the given unit
Definition cp_units.F:1178
real(kind=dp) function, public cp_unit_to_cp2k(value, unit_str, defaults, power)
converts to the internal cp2k units to the given unit
Definition cp_units.F:1149
This public domain function parser module is intended for applications where a set of mathematical ex...
Definition fparser.F:17
subroutine, public parsef(i, funcstr, var)
Parse ith function string FuncStr and compile it into bytecode.
Definition fparser.F:174
real(rn) function, public evalf(i, val)
...
Definition fparser.F:206
integer, public evalerrtype
Definition fparser.F:33
subroutine, public finalizef()
...
Definition fparser.F:127
subroutine, public initf(n)
...
Definition fparser.F:156
Set of routines to dump the restart file of CP2K.
subroutine, public write_restart(md_env, force_env, root_section, coords, vels, pint_env, helium_env)
checks if a restart needs to be written and does so, updating all necessary fields in the input file....
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
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
integer, parameter, public default_path_length
Definition kinds.F:58
prints all energy info per timestep to the screen or to user defined output files
Definition md_energies.F:16
subroutine, public md_write_output(md_env)
This routine computes the conserved quantity, temperature and things like that and prints them out.
subroutine, public get_md_env(md_env, itimes, constant, used_time, cell, simpar, npt, force_env, para_env, reftraj, t, init, first_time, fe_env, thermostats, barostat, thermostat_coeff, thermostat_part, thermostat_shell, thermostat_baro, thermostat_fast, thermostat_slow, md_ener, averages, thermal_regions, ehrenfest_md)
get components of MD environment type
Utilities for Molecular Dynamics.
Definition md_util.F:12
subroutine, public md_output(md_env, md_section, root_section, forced_io)
collects the part of the MD that, basically, does the output
Definition md_util.F:125
subroutine, public update_expected_temperature(md_env)
update_expected_temperature
Definition md_util.F:71
subroutine, public read_vib_eigs_unformatted(md_section, vib_section, para_env, dof, eigenvalues, eigenvectors)
read eigenvalues and eigenvectors of Hessian from vibrational analysis results, for use of initialisi...
Definition md_util.F:172
Interface to the message passing library MPI.
Type for storing MD parameters.
Utilities for string manipulations.
subroutine, public integer_to_string(inumber, string)
Converts an integer number to a string. The WRITE statement will return an error message,...
Thermal regions type: to initialize and control the temperature of different regions.
type of a logger, at the moment it contains just a print level starting at which level it should be l...
stores all the informations relevant to an mpi environment
Simulation parameter type for molecular dynamics.