LCOV - code coverage report
Current view: top level - src/motion - md_run.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:3db43b4) Lines: 93.8 % 194 182
Test Date: 2026-04-03 06:55:30 Functions: 100.0 % 2 2

            Line data    Source code
       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 Perform a molecular dynamics (MD) run using QUICKSTEP
      10              : !> \par History
      11              : !>   - Added support for Langevin regions (2014/02/05, LT)
      12              : !> \author Matthias Krack (07.11.2002)
      13              : ! **************************************************************************************************
      14              : MODULE md_run
      15              :    USE atomic_kind_list_types,          ONLY: atomic_kind_list_type
      16              :    USE averages_types,                  ONLY: average_quantities_type
      17              :    USE barostat_types,                  ONLY: barostat_type,&
      18              :                                               create_barostat_type
      19              :    USE cell_types,                      ONLY: cell_type
      20              :    USE cp_external_control,             ONLY: external_control
      21              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      22              :                                               cp_logger_type
      23              :    USE cp_output_handling,              ONLY: cp_add_iter_level,&
      24              :                                               cp_iterate,&
      25              :                                               cp_p_file,&
      26              :                                               cp_print_key_finished_output,&
      27              :                                               cp_print_key_should_output,&
      28              :                                               cp_print_key_unit_nr,&
      29              :                                               cp_rm_iter_level
      30              :    USE cp_subsys_types,                 ONLY: cp_subsys_get,&
      31              :                                               cp_subsys_type
      32              :    USE distribution_1d_types,           ONLY: distribution_1d_type
      33              :    USE force_env_methods,               ONLY: force_env_calc_energy_force
      34              :    USE force_env_types,                 ONLY: force_env_get,&
      35              :                                               force_env_type
      36              :    USE free_energy_methods,             ONLY: free_energy_evaluate
      37              :    USE free_energy_types,               ONLY: fe_env_create,&
      38              :                                               free_energy_type
      39              :    USE global_types,                    ONLY: global_environment_type
      40              :    USE input_constants,                 ONLY: &
      41              :         ehrenfest, langevin_ensemble, npe_f_ensemble, npe_i_ensemble, &
      42              :         nph_uniaxial_damped_ensemble, nph_uniaxial_ensemble, npt_f_ensemble, npt_i_ensemble, &
      43              :         npt_ia_ensemble, reftraj_ensemble
      44              :    USE input_cp2k_check,                ONLY: remove_restart_info
      45              :    USE input_section_types,             ONLY: section_vals_get_subs_vals,&
      46              :                                               section_vals_remove_values,&
      47              :                                               section_vals_type,&
      48              :                                               section_vals_val_get
      49              :    USE kinds,                           ONLY: default_string_length,&
      50              :                                               dp
      51              :    USE machine,                         ONLY: m_walltime
      52              :    USE md_ener_types,                   ONLY: create_md_ener,&
      53              :                                               md_ener_type
      54              :    USE md_energies,                     ONLY: initialize_md_ener,&
      55              :                                               md_ener_reftraj,&
      56              :                                               md_energy,&
      57              :                                               md_write_output
      58              :    USE md_environment_types,            ONLY: get_md_env,&
      59              :                                               md_env_create,&
      60              :                                               md_env_release,&
      61              :                                               md_environment_type,&
      62              :                                               need_per_atom_wiener_process,&
      63              :                                               set_md_env
      64              :    USE md_util,                         ONLY: md_output
      65              :    USE md_vel_utils,                    ONLY: angvel_control,&
      66              :                                               comvel_control,&
      67              :                                               setup_velocities,&
      68              :                                               temperature_control
      69              :    USE mdctrl_methods,                  ONLY: mdctrl_callback
      70              :    USE mdctrl_types,                    ONLY: mdctrl_type
      71              :    USE message_passing,                 ONLY: mp_para_env_type
      72              :    USE metadynamics,                    ONLY: metadyn_finalise_plumed,&
      73              :                                               metadyn_forces,&
      74              :                                               metadyn_initialise_plumed,&
      75              :                                               metadyn_write_colvar
      76              :    USE metadynamics_types,              ONLY: set_meta_env
      77              :    USE particle_list_types,             ONLY: particle_list_type
      78              :    USE qs_environment_methods,          ONLY: qs_env_time_update
      79              :    USE reftraj_types,                   ONLY: create_reftraj,&
      80              :                                               reftraj_type
      81              :    USE reftraj_util,                    ONLY: initialize_reftraj,&
      82              :                                               write_output_reftraj
      83              :    USE rt_propagation,                  ONLY: rt_prop_setup
      84              :    USE simpar_methods,                  ONLY: read_md_section
      85              :    USE simpar_types,                    ONLY: create_simpar_type,&
      86              :                                               release_simpar_type,&
      87              :                                               simpar_type
      88              :    USE thermal_region_types,            ONLY: thermal_regions_type
      89              :    USE thermal_region_utils,            ONLY: create_thermal_regions,&
      90              :                                               print_thermal_regions_langevin,&
      91              :                                               update_thermal_regions_temp_expected
      92              :    USE thermostat_methods,              ONLY: create_thermostats
      93              :    USE thermostat_types,                ONLY: thermostats_type
      94              :    USE velocity_verlet_control,         ONLY: velocity_verlet
      95              :    USE virial_methods,                  ONLY: virial_evaluate
      96              :    USE virial_types,                    ONLY: virial_type
      97              :    USE wiener_process,                  ONLY: create_wiener_process,&
      98              :                                               create_wiener_process_cv
      99              : #include "../base/base_uses.f90"
     100              : 
     101              :    IMPLICIT NONE
     102              : 
     103              :    PRIVATE
     104              : 
     105              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'md_run'
     106              : 
     107              :    PUBLIC :: qs_mol_dyn
     108              : 
     109              : CONTAINS
     110              : 
     111              : ! **************************************************************************************************
     112              : !> \brief Main driver module for Molecular Dynamics
     113              : !> \param force_env ...
     114              : !> \param globenv ...
     115              : !> \param averages ...
     116              : !> \param rm_restart_info ...
     117              : !> \param hmc_e_initial ...
     118              : !> \param hmc_e_final ...
     119              : !> \param mdctrl ...
     120              : ! **************************************************************************************************
     121         3572 :    SUBROUTINE qs_mol_dyn(force_env, globenv, averages, rm_restart_info, hmc_e_initial, hmc_e_final, mdctrl)
     122              : 
     123              :       TYPE(force_env_type), POINTER                      :: force_env
     124              :       TYPE(global_environment_type), POINTER             :: globenv
     125              :       TYPE(average_quantities_type), OPTIONAL, POINTER   :: averages
     126              :       LOGICAL, INTENT(IN), OPTIONAL                      :: rm_restart_info
     127              :       REAL(KIND=dp), OPTIONAL                            :: hmc_e_initial, hmc_e_final
     128              :       TYPE(mdctrl_type), OPTIONAL, POINTER               :: mdctrl
     129              : 
     130              :       LOGICAL                                            :: my_rm_restart_info
     131              :       TYPE(md_environment_type), POINTER                 :: md_env
     132              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     133              :       TYPE(section_vals_type), POINTER                   :: md_section, motion_section
     134              : 
     135         1786 :       my_rm_restart_info = .TRUE.
     136         1786 :       IF (PRESENT(rm_restart_info)) my_rm_restart_info = rm_restart_info
     137         1786 :       NULLIFY (md_env, para_env)
     138         1786 :       para_env => force_env%para_env
     139         1786 :       motion_section => section_vals_get_subs_vals(force_env%root_section, "MOTION")
     140         1786 :       md_section => section_vals_get_subs_vals(motion_section, "MD")
     141              : 
     142              :       ! Real call to MD driver - Low Level
     143         1786 :       ALLOCATE (md_env)
     144         1786 :       CALL md_env_create(md_env, md_section, para_env, force_env=force_env)
     145         1786 :       CALL set_md_env(md_env, averages=averages)
     146         1786 :       IF (PRESENT(hmc_e_initial) .AND. PRESENT(hmc_e_final)) THEN
     147              :          CALL qs_mol_dyn_low(md_env, md_section, motion_section, force_env, globenv, &
     148           28 :                              hmc_e_initial=hmc_e_initial, hmc_e_final=hmc_e_final)
     149              :       ELSE
     150         1758 :          CALL qs_mol_dyn_low(md_env, md_section, motion_section, force_env, globenv, mdctrl=mdctrl)
     151              :       END IF
     152         1786 :       CALL md_env_release(md_env)
     153         1786 :       DEALLOCATE (md_env)
     154              : 
     155              :       ! Clean restartable sections..
     156         1786 :       IF (my_rm_restart_info) CALL remove_restart_info(force_env%root_section)
     157         1786 :    END SUBROUTINE qs_mol_dyn
     158              : 
     159              : ! **************************************************************************************************
     160              : !> \brief Purpose: Driver routine for MD run using QUICKSTEP.
     161              : !> \param md_env ...
     162              : !> \param md_section ...
     163              : !> \param motion_section ...
     164              : !> \param force_env ...
     165              : !> \param globenv ...
     166              : !> \param hmc_e_initial ...
     167              : !> \param hmc_e_final ...
     168              : !> \param mdctrl ...
     169              : !> \par History
     170              : !>   - Cleaning (09.2007) Teodoro Laino [tlaino] - University of Zurich
     171              : !>   - Added lines to print out langevin regions (2014/02/04, LT)
     172              : !> \author Creation (07.11.2002,MK)
     173              : ! **************************************************************************************************
     174         8930 :    SUBROUTINE qs_mol_dyn_low(md_env, md_section, motion_section, force_env, globenv, hmc_e_initial, hmc_e_final, mdctrl)
     175              : 
     176              :       TYPE(md_environment_type), POINTER                 :: md_env
     177              :       TYPE(section_vals_type), POINTER                   :: md_section, motion_section
     178              :       TYPE(force_env_type), POINTER                      :: force_env
     179              :       TYPE(global_environment_type), POINTER             :: globenv
     180              :       REAL(KIND=dp), OPTIONAL                            :: hmc_e_initial, hmc_e_final
     181              :       TYPE(mdctrl_type), OPTIONAL, POINTER               :: mdctrl
     182              : 
     183              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'qs_mol_dyn_low'
     184              : 
     185              :       CHARACTER(LEN=default_string_length)               :: my_act, my_pos
     186              :       INTEGER                                            :: handle, i, istep, md_stride, run_type_id
     187              :       INTEGER, POINTER                                   :: itimes
     188              :       LOGICAL                                            :: check, ehrenfest_md, save_mem, &
     189              :                                                             should_stop, write_binary_restart_file
     190              :       REAL(KIND=dp)                                      :: dummy, time_iter_start, time_iter_stop
     191              :       REAL(KIND=dp), POINTER                             :: constant, time, used_time
     192              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
     193              :       TYPE(barostat_type), POINTER                       :: barostat
     194              :       TYPE(cell_type), POINTER                           :: cell
     195              :       TYPE(cp_logger_type), POINTER                      :: logger
     196              :       TYPE(cp_subsys_type), POINTER                      :: subsys, subsys_i
     197              :       TYPE(distribution_1d_type), POINTER                :: local_particles
     198              :       TYPE(free_energy_type), POINTER                    :: fe_env
     199              :       TYPE(md_ener_type), POINTER                        :: md_ener
     200              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     201              :       TYPE(particle_list_type), POINTER                  :: particles
     202              :       TYPE(reftraj_type), POINTER                        :: reftraj
     203              :       TYPE(section_vals_type), POINTER :: constraint_section, force_env_section, &
     204              :          free_energy_section, global_section, reftraj_section, subsys_section, work_section
     205              :       TYPE(simpar_type), POINTER                         :: simpar
     206              :       TYPE(thermal_regions_type), POINTER                :: thermal_regions
     207              :       TYPE(thermostats_type), POINTER                    :: thermostats
     208              :       TYPE(virial_type), POINTER                         :: virial
     209              : 
     210         1786 :       CALL timeset(routineN, handle)
     211         1786 :       CPASSERT(ASSOCIATED(globenv))
     212         1786 :       CPASSERT(ASSOCIATED(force_env))
     213              : 
     214         1786 :       NULLIFY (particles, cell, simpar, itimes, used_time, subsys, &
     215         1786 :                md_ener, thermostats, barostat, reftraj, force_env_section, &
     216         1786 :                reftraj_section, work_section, atomic_kinds, &
     217         1786 :                local_particles, time, fe_env, free_energy_section, &
     218         1786 :                constraint_section, thermal_regions, virial, subsys_i)
     219         1786 :       logger => cp_get_default_logger()
     220         1786 :       para_env => force_env%para_env
     221              : 
     222         1786 :       global_section => section_vals_get_subs_vals(force_env%root_section, "GLOBAL")
     223         1786 :       free_energy_section => section_vals_get_subs_vals(motion_section, "FREE_ENERGY")
     224         1786 :       constraint_section => section_vals_get_subs_vals(motion_section, "CONSTRAINT")
     225         1786 :       CALL section_vals_val_get(global_section, "SAVE_MEM", l_val=save_mem)
     226              : 
     227         1786 :       CALL section_vals_val_get(global_section, "RUN_TYPE", i_val=run_type_id)
     228         1786 :       IF (run_type_id == ehrenfest) CALL set_md_env(md_env, ehrenfest_md=.TRUE.)
     229              : 
     230         1786 :       CALL create_simpar_type(simpar)
     231         1786 :       force_env_section => force_env%force_env_section
     232         1786 :       subsys_section => section_vals_get_subs_vals(force_env_section, "SUBSYS")
     233         1786 :       CALL cp_add_iter_level(logger%iter_info, "MD")
     234         1786 :       CALL cp_iterate(logger%iter_info, iter_nr=0)
     235              :       ! Read MD section
     236         1786 :       CALL read_md_section(simpar, motion_section, md_section)
     237              :       ! Setup print_keys
     238              :       simpar%info_constraint = cp_print_key_unit_nr(logger, constraint_section, &
     239         1786 :                                                     "CONSTRAINT_INFO", extension=".shakeLog", log_filename=.FALSE.)
     240              :       simpar%lagrange_multipliers = cp_print_key_unit_nr(logger, constraint_section, &
     241         1786 :                                                          "LAGRANGE_MULTIPLIERS", extension=".LagrangeMultLog", log_filename=.FALSE.)
     242              :       simpar%dump_lm = BTEST(cp_print_key_should_output(logger%iter_info, constraint_section, &
     243         1786 :                                                         "LAGRANGE_MULTIPLIERS"), cp_p_file)
     244              : 
     245              :       ! Create the structure for the md energies
     246         7144 :       ALLOCATE (md_ener)
     247         1786 :       CALL create_md_ener(md_ener)
     248         1786 :       CALL set_md_env(md_env, md_ener=md_ener)
     249         1786 :       NULLIFY (md_ener)
     250              : 
     251              :       ! If requested setup Thermostats
     252              :       CALL create_thermostats(thermostats, md_section, force_env, simpar, para_env, &
     253         1786 :                               globenv, global_section)
     254              : 
     255              :       ! If requested setup Barostat
     256         1786 :       CALL create_barostat_type(barostat, md_section, force_env, simpar, globenv)
     257              : 
     258              :       ! If requested setup different thermal regions
     259         1786 :       CALL create_thermal_regions(thermal_regions, md_section, simpar, force_env)
     260              : 
     261              :       ! If doing langevin_ensemble, then print out langevin_regions information upon request
     262         1786 :       IF (simpar%ensemble == langevin_ensemble) THEN
     263           42 :          my_pos = "REWIND"
     264           42 :          my_act = "WRITE"
     265              :          CALL print_thermal_regions_langevin(thermal_regions, simpar, &
     266           42 :                                              pos=my_pos, act=my_act)
     267              :       END IF
     268              : 
     269         1786 :       CALL set_md_env(md_env, thermostats=thermostats, barostat=barostat, thermal_regions=thermal_regions)
     270              : 
     271         1786 :       CALL get_md_env(md_env, ehrenfest_md=ehrenfest_md)
     272              : 
     273              :       !If requested set up the REFTRAJ run
     274         1786 :       IF (simpar%ensemble == reftraj_ensemble .AND. ehrenfest_md) &
     275            0 :          CPABORT("Ehrenfest MD does not support reftraj ensemble ")
     276         1786 :       IF (simpar%ensemble == reftraj_ensemble) THEN
     277           38 :          reftraj_section => section_vals_get_subs_vals(md_section, "REFTRAJ")
     278           38 :          ALLOCATE (reftraj)
     279           38 :          CALL create_reftraj(reftraj, reftraj_section, para_env)
     280           38 :          CALL set_md_env(md_env, reftraj=reftraj)
     281              :       END IF
     282              : 
     283              :       CALL force_env_get(force_env, subsys=subsys, cell=cell, &
     284         1786 :                          force_env_section=force_env_section)
     285         1786 :       CALL cp_subsys_get(subsys, virial=virial)
     286              : 
     287              :       ! Set V0 if needed
     288         1786 :       IF (simpar%ensemble == nph_uniaxial_ensemble .OR. simpar%ensemble == nph_uniaxial_damped_ensemble) THEN
     289            6 :          IF (simpar%v0 == 0._dp) simpar%v0 = cell%deth
     290              :       END IF
     291              : 
     292              :       ! Initialize velocities possibly applying constraints at the zeroth MD step
     293              :       CALL section_vals_val_get(motion_section, "PRINT%RESTART%SPLIT_RESTART_FILE", &
     294         1786 :                                 l_val=write_binary_restart_file)
     295              :       CALL setup_velocities(force_env, simpar, globenv, md_env, md_section, constraint_section, &
     296         1786 :                             write_binary_restart_file)
     297              : 
     298              :       ! Setup Free Energy Calculation (if required)
     299         1786 :       CALL fe_env_create(fe_env, free_energy_section)
     300              : 
     301              :       CALL set_md_env(md_env=md_env, simpar=simpar, fe_env=fe_env, cell=cell, &
     302         1786 :                       force_env=force_env)
     303              : 
     304              :       ! Possibly initialize Wiener processes
     305              :       ![NB] Tested again within create_wiener_process.  Why??
     306         1786 :       IF (need_per_atom_wiener_process(md_env)) CALL create_wiener_process(md_env)
     307              : 
     308         1786 :       time_iter_start = m_walltime()
     309              : 
     310              :       CALL get_md_env(md_env, force_env=force_env, itimes=itimes, constant=constant, &
     311         1786 :                       md_ener=md_ener, t=time, used_time=used_time)
     312              : 
     313              :       ! Attach the time counter of the meta_env to the one of the MD
     314         1786 :       CALL set_meta_env(force_env%meta_env, time=time)
     315              : 
     316              :       ! Initialize the md_ener structure
     317         1786 :       CALL initialize_md_ener(md_ener, force_env, simpar)
     318              : 
     319              :       ! Check for ensembles requiring the stress tensor - takes into account the possibility for
     320              :       ! multiple force_evals
     321              :       IF ((simpar%ensemble == npt_i_ensemble) .OR. &
     322              :           (simpar%ensemble == npt_ia_ensemble) .OR. &
     323              :           (simpar%ensemble == npt_f_ensemble) .OR. &
     324              :           (simpar%ensemble == npe_f_ensemble) .OR. &
     325              :           (simpar%ensemble == npe_i_ensemble) .OR. &
     326         1786 :           (simpar%ensemble == nph_uniaxial_ensemble) .OR. &
     327              :           (simpar%ensemble == nph_uniaxial_damped_ensemble)) THEN
     328          172 :          check = virial%pv_availability
     329          172 :          IF (.NOT. check) &
     330              :             CALL cp_abort(__LOCATION__, &
     331              :                           "Virial evaluation not requested for this run in the input file!"// &
     332              :                           " You may consider to switch on the virial evaluation with the keyword: STRESS_TENSOR."// &
     333            0 :                           " Be sure the method you are using can compute the virial!")
     334          172 :          IF (ASSOCIATED(force_env%sub_force_env)) THEN
     335           26 :             DO i = 1, SIZE(force_env%sub_force_env)
     336           26 :                IF (ASSOCIATED(force_env%sub_force_env(i)%force_env)) THEN
     337           10 :                   CALL force_env_get(force_env%sub_force_env(i)%force_env, subsys=subsys_i)
     338           10 :                   CALL cp_subsys_get(subsys_i, virial=virial)
     339           10 :                   check = check .AND. virial%pv_availability
     340              :                END IF
     341              :             END DO
     342              :          END IF
     343          172 :          IF (.NOT. check) &
     344              :             CALL cp_abort(__LOCATION__, &
     345              :                           "Virial evaluation not requested for all the force_eval sections present in"// &
     346              :                           " the input file! You have to switch on the virial evaluation with the keyword: STRESS_TENSOR"// &
     347            0 :                           " in each force_eval section. Be sure the method you are using can compute the virial!")
     348              :       END IF
     349              : 
     350              :       ! Computing Forces at zero MD step
     351         1786 :       IF (simpar%ensemble /= reftraj_ensemble) THEN
     352         1748 :          CALL section_vals_val_get(md_section, "STEP_START_VAL", i_val=itimes)
     353         1748 :          CALL section_vals_val_get(md_section, "TIME_START_VAL", r_val=time)
     354         1748 :          CALL section_vals_val_get(md_section, "ECONS_START_VAL", r_val=constant)
     355         1748 :          CALL cp_iterate(logger%iter_info, iter_nr=itimes)
     356         1748 :          IF (save_mem) THEN
     357            2 :             work_section => section_vals_get_subs_vals(subsys_section, "VELOCITY")
     358            2 :             CALL section_vals_remove_values(work_section)
     359            2 :             work_section => section_vals_get_subs_vals(subsys_section, "SHELL_VELOCITY")
     360            2 :             CALL section_vals_remove_values(work_section)
     361            2 :             work_section => section_vals_get_subs_vals(subsys_section, "CORE_VELOCITY")
     362            2 :             CALL section_vals_remove_values(work_section)
     363              :          END IF
     364              : 
     365         1748 :          IF (ehrenfest_md) THEN
     366           74 :             CALL rt_prop_setup(force_env)
     367           74 :             force_env%qs_env%rtp%dt = simpar%dt
     368              :          ELSE
     369              :             ![NB] Lets let all methods, even ones without consistent energies, succeed here.
     370              :             !     They'll fail in actual integrator if needed
     371              :             ! consistent_energies=.FALSE. by default
     372         1674 :             CALL force_env_calc_energy_force(force_env, calc_force=.TRUE.)
     373              :          END IF
     374              : 
     375         1748 :          IF (ASSOCIATED(force_env%qs_env)) THEN
     376          600 :             CALL qs_env_time_update(force_env%qs_env, time, itimes)
     377              :          END IF
     378              :          ! Warm-up engines for metadynamics
     379         1748 :          IF (ASSOCIATED(force_env%meta_env)) THEN
     380              :             ! Setup stuff for plumed if needed
     381          148 :             IF (force_env%meta_env%use_plumed .EQV. .TRUE.) THEN
     382            2 :                CALL metadyn_initialise_plumed(force_env, simpar, itimes)
     383              :             ELSE
     384          146 :                IF (force_env%meta_env%langevin) THEN
     385            4 :                   CALL create_wiener_process_cv(force_env%meta_env)
     386              :                END IF
     387          146 :                IF (force_env%meta_env%well_tempered) THEN
     388            2 :                   force_env%meta_env%wttemperature = simpar%temp_ext
     389            2 :                   IF (force_env%meta_env%wtgamma > EPSILON(1._dp)) THEN
     390            0 :                      dummy = force_env%meta_env%wttemperature*(force_env%meta_env%wtgamma - 1._dp)
     391            0 :                      IF (force_env%meta_env%delta_t > EPSILON(1._dp)) THEN
     392            0 :                         check = ABS(force_env%meta_env%delta_t - dummy) < 1.E+3_dp*EPSILON(1._dp)
     393            0 :                         IF (.NOT. check) &
     394              :                            CALL cp_abort(__LOCATION__, &
     395              :                                          "Inconsistency between DELTA_T and WTGAMMA (both specified):"// &
     396            0 :                                          " please, verify that DELTA_T=(WTGAMMA-1)*TEMPERATURE")
     397              :                      ELSE
     398            0 :                         force_env%meta_env%delta_t = dummy
     399              :                      END IF
     400              :                   ELSE
     401              :                      force_env%meta_env%wtgamma = 1._dp &
     402            2 :                                                   + force_env%meta_env%delta_t/force_env%meta_env%wttemperature
     403              :                   END IF
     404            2 :                   force_env%meta_env%invdt = 1._dp/force_env%meta_env%delta_t
     405              :                END IF
     406          146 :                CALL metadyn_forces(force_env)
     407          146 :                CALL metadyn_write_colvar(force_env)
     408              :             END IF
     409              :          END IF
     410              : 
     411         1748 :          IF (simpar%do_respa) THEN
     412              :             CALL force_env_calc_energy_force(force_env%sub_force_env(1)%force_env, &
     413            6 :                                              calc_force=.TRUE.)
     414              :          END IF
     415              : 
     416         1748 :          CALL force_env_get(force_env, subsys=subsys)
     417              : 
     418              :          CALL cp_subsys_get(subsys, atomic_kinds=atomic_kinds, local_particles=local_particles, &
     419         1748 :                             particles=particles, virial=virial)
     420              : 
     421              :          CALL virial_evaluate(atomic_kinds%els, particles%els, local_particles, &
     422         1748 :                               virial, force_env%para_env)
     423              : 
     424         1748 :          CALL md_energy(md_env, md_ener)
     425         1748 :          CALL md_write_output(md_env) !inits the print env at itimes == 0 also writes trajectories
     426         1748 :          md_stride = 1
     427              :       ELSE
     428           38 :          CALL get_md_env(md_env, reftraj=reftraj)
     429           38 :          CALL initialize_reftraj(reftraj, reftraj_section, md_env)
     430           38 :          itimes = reftraj%info%first_snapshot - 1
     431           38 :          md_stride = reftraj%info%stride
     432           38 :          IF (ASSOCIATED(force_env%meta_env)) THEN
     433            4 :             IF (force_env%meta_env%use_plumed .EQV. .TRUE.) THEN
     434            0 :                CALL metadyn_initialise_plumed(force_env, simpar, itimes)
     435              :             END IF
     436              :          END IF
     437              :       END IF
     438              : 
     439              :       CALL cp_print_key_finished_output(simpar%info_constraint, logger, &
     440         1786 :                                         constraint_section, "CONSTRAINT_INFO")
     441              :       CALL cp_print_key_finished_output(simpar%lagrange_multipliers, logger, &
     442         1786 :                                         constraint_section, "LAGRANGE_MULTIPLIERS")
     443              : 
     444              : ! if we need the initial kinetic energy for Hybrid Monte Carlo
     445         1786 :       IF (PRESENT(hmc_e_initial)) hmc_e_initial = md_ener%ekin
     446              : 
     447         1786 :       IF (itimes >= simpar%max_steps) CALL cp_abort(__LOCATION__, &
     448            0 :                                                     "maximum step number smaller than initial step value")
     449              : 
     450              :       ! Real MD Loop
     451        44037 :       DO istep = 1, simpar%nsteps, md_stride
     452              :          ! Increase counters
     453        42299 :          itimes = itimes + 1
     454        42299 :          time = time + simpar%dt
     455              :          !needed when electric field fields are applied
     456        42299 :          IF (ASSOCIATED(force_env%qs_env)) THEN
     457         3222 :             CALL qs_env_time_update(force_env%qs_env, time, itimes)
     458              :          END IF
     459        42299 :          IF (ehrenfest_md) force_env%qs_env%rtp%istep = istep
     460              : 
     461        42299 :          IF (.NOT. logger%iter_info%last_iter(logger%iter_info%n_rlevel)) THEN
     462        42299 :             CALL cp_iterate(logger%iter_info, last=(istep == simpar%nsteps), iter_nr=itimes)
     463              :          ELSE
     464            0 :             CALL cp_iterate(logger%iter_info, last=.TRUE., iter_nr=itimes)
     465              :          END IF
     466              : 
     467              :          ! Open possible Shake output units
     468              :          simpar%info_constraint = cp_print_key_unit_nr(logger, constraint_section, "CONSTRAINT_INFO", &
     469        42299 :                                                        extension=".shakeLog", log_filename=.FALSE.)
     470              :          simpar%lagrange_multipliers = cp_print_key_unit_nr( &
     471              :                                        logger, constraint_section, &
     472        42299 :                                        "LAGRANGE_MULTIPLIERS", extension=".LagrangeMultLog", log_filename=.FALSE.)
     473              :          simpar%dump_lm = BTEST(cp_print_key_should_output(logger%iter_info, constraint_section, &
     474        42299 :                                                            "LAGRANGE_MULTIPLIERS"), cp_p_file)
     475              : 
     476              :          ! Velocity Verlet Integrator
     477        42299 :          CALL velocity_verlet(md_env, globenv)
     478              : 
     479              :          ! Close Shake output if requested...
     480              :          CALL cp_print_key_finished_output(simpar%info_constraint, logger, &
     481        42299 :                                            constraint_section, "CONSTRAINT_INFO")
     482              :          CALL cp_print_key_finished_output(simpar%lagrange_multipliers, logger, &
     483        42299 :                                            constraint_section, "LAGRANGE_MULTIPLIERS")
     484              : 
     485              :          ! Free Energy calculation
     486        42299 :          CALL free_energy_evaluate(md_env, should_stop, free_energy_section)
     487              : 
     488        42299 :          IF (should_stop) EXIT
     489              : 
     490              :          ! Test for <PROJECT_NAME>.EXIT_MD or for WALL_TIME to exit
     491              :          ! Default:
     492              :          ! IF so we don't overwrite the restart or append to the trajectory
     493              :          ! because the execution could in principle stop inside the SCF where energy
     494              :          ! and forces are not converged.
     495              :          ! But:
     496              :          ! You can force to print the last step (for example if the method used
     497              :          ! to compute energy and forces is not SCF based) activating the print_key
     498              :          ! MOTION%MD%PRINT%FORCE_LAST.
     499        42299 :          CALL external_control(should_stop, "MD", globenv=globenv)
     500              : 
     501              :          !check if upper bound of total steps has been reached
     502        42299 :          IF (.NOT. (istep == simpar%nsteps) .AND. logger%iter_info%last_iter(logger%iter_info%n_rlevel)) should_stop = .TRUE.
     503        42299 :          IF (itimes >= simpar%max_steps) should_stop = .TRUE.
     504              : 
     505              :          ! call external hook e.g. from global optimization
     506        42299 :          IF (PRESENT(mdctrl)) &
     507         4333 :             CALL mdctrl_callback(mdctrl, md_env, should_stop)
     508              : 
     509        42299 :          IF (should_stop) THEN
     510           48 :             CALL cp_iterate(logger%iter_info, last=.TRUE., iter_nr=itimes)
     511              :             !In Ehrenfest molecular dynamics the external control is only checked after a converged propagation
     512              :             !The restart needs to be written in order to be consistent with the mos/density matrix for the restart
     513           48 :             IF (run_type_id == ehrenfest) THEN
     514            2 :                CALL md_output(md_env, md_section, force_env%root_section, .FALSE.)
     515              :             ELSE
     516           46 :                CALL md_output(md_env, md_section, force_env%root_section, should_stop)
     517              :             END IF
     518              :             EXIT
     519              :          END IF
     520              : 
     521        42251 :          IF (simpar%ensemble /= reftraj_ensemble) THEN
     522        41963 :             CALL md_energy(md_env, md_ener)
     523        41963 :             IF (simpar%do_thermal_region) THEN ! Optionally update expected temperature for each region
     524           96 :                CALL get_md_env(md_env, thermal_regions=thermal_regions)
     525           96 :                CALL update_thermal_regions_temp_expected(itimes, thermal_regions)
     526              :             END IF
     527        41963 :             CALL temperature_control(simpar, md_env, md_ener, force_env, logger)
     528        41963 :             CALL comvel_control(md_ener, force_env, md_section, logger)
     529        41963 :             CALL angvel_control(md_ener, force_env, md_section, logger)
     530              :          ELSE
     531          288 :             CALL md_ener_reftraj(md_env, md_ener)
     532              :          END IF
     533              : 
     534        42251 :          time_iter_stop = m_walltime()
     535        42251 :          used_time = time_iter_stop - time_iter_start
     536        42251 :          time_iter_start = time_iter_stop
     537              : 
     538        42251 :          CALL md_output(md_env, md_section, force_env%root_section, should_stop)
     539       128587 :          IF (simpar%ensemble == reftraj_ensemble) THEN
     540          288 :             CALL write_output_reftraj(md_env)
     541              :          END IF
     542              :       END DO
     543              : 
     544              : ! if we need the final kinetic energy for Hybrid Monte Carlo
     545         1786 :       IF (PRESENT(hmc_e_final)) hmc_e_final = md_ener%ekin
     546              : 
     547              :       ! Remove the iteration level
     548         1786 :       CALL cp_rm_iter_level(logger%iter_info, "MD")
     549              : 
     550              :       ! Clean up PLUMED
     551         1786 :       IF (ASSOCIATED(force_env%meta_env)) THEN
     552          152 :          IF (force_env%meta_env%use_plumed .EQV. .TRUE.) THEN
     553            2 :             CALL metadyn_finalise_plumed()
     554              :          END IF
     555              :       END IF
     556              : 
     557              :       ! Deallocate Thermostats and Barostats
     558         1786 :       CALL release_simpar_type(simpar)
     559         1786 :       CALL timestop(handle)
     560              : 
     561         1786 :    END SUBROUTINE qs_mol_dyn_low
     562              : 
     563              : END MODULE md_run
        

Generated by: LCOV version 2.0-1