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

Generated by: LCOV version 2.0-1