LCOV - code coverage report
Current view: top level - src/motion - integrator.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 83.9 % 1078 904
Test Date: 2026-11-24 01:22:08 Functions: 90.9 % 11 10

            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 Provides integrator routines (velocity verlet) for all the
      10              : !>      ensemble types
      11              : !> \par History
      12              : !>      JGH (15-Mar-2001) : Pass logical for box change to force routine
      13              : !>      Harald Forbert (Apr-2001): added path integral routine nvt_pimd
      14              : !>      CJM (15-Apr-2001) : added coef integrators and energy routines
      15              : !>      Joost VandeVondele (Juli-2003): simple version of isokinetic ensemble
      16              : !>      Teodoro Laino [tlaino] 10.2007 - University of Zurich: Generalization to
      17              : !>                                       different kind of thermostats
      18              : !>      Teodoro Laino [tlaino] 11.2007 - Metadynamics: now part of the MD modules
      19              : !>      Marcella Iannuzzi      02.2008 - Collecting common code (VV and creation of
      20              : !>                                       a temporary type)
      21              : !>      Teodoro Laino [tlaino] 02.2008 - Splitting integrator module and keeping in
      22              : !>                                       integrator only the INTEGRATORS
      23              : !>      Lianheng Tong [LT]     12.2013 - Added regions to Langevin MD
      24              : !> \author CJM
      25              : ! **************************************************************************************************
      26              : MODULE integrator
      27              :    USE atomic_kind_list_types,          ONLY: atomic_kind_list_type
      28              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      29              :                                               get_atomic_kind,&
      30              :                                               get_atomic_kind_set
      31              :    USE barostat_types,                  ONLY: barostat_type
      32              :    USE cell_methods,                    ONLY: init_cell,&
      33              :                                               read_xyz_comment
      34              :    USE cell_types,                      ONLY: cell_type,&
      35              :                                               parse_cell_line,&
      36              :                                               pbc
      37              :    USE constraint,                      ONLY: rattle_control,&
      38              :                                               shake_control,&
      39              :                                               shake_roll_control,&
      40              :                                               shake_update_targets
      41              :    USE constraint_fxd,                  ONLY: create_local_fixd_list,&
      42              :                                               fix_atom_control,&
      43              :                                               release_local_fixd_list
      44              :    USE constraint_util,                 ONLY: getold,&
      45              :                                               pv_constraint
      46              :    USE cp_control_types,                ONLY: dft_control_type
      47              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      48              :                                               cp_logger_type,&
      49              :                                               cp_to_string
      50              :    USE cp_output_handling,              ONLY: cp_iterate
      51              :    USE cp_parser_methods,               ONLY: parser_get_next_line,&
      52              :                                               parser_read_line
      53              :    USE cp_subsys_types,                 ONLY: cp_subsys_get,&
      54              :                                               cp_subsys_type
      55              :    USE cp_units,                        ONLY: cp_unit_to_cp2k
      56              :    USE distribution_1d_types,           ONLY: distribution_1d_type
      57              :    USE eigenvalueproblems,              ONLY: diagonalise
      58              :    USE extended_system_dynamics,        ONLY: shell_scale_comv
      59              :    USE extended_system_types,           ONLY: npt_info_type
      60              :    USE force_env_methods,               ONLY: force_env_calc_energy_force
      61              :    USE force_env_types,                 ONLY: force_env_get,&
      62              :                                               force_env_type
      63              :    USE global_types,                    ONLY: global_environment_type
      64              :    USE input_constants,                 ONLY: ehrenfest,&
      65              :                                               npe_f_ensemble,&
      66              :                                               npe_i_ensemble,&
      67              :                                               npt_ia_ensemble
      68              :    USE integrator_utils,                ONLY: &
      69              :         allocate_old, allocate_tmp, damp_v, damp_veps, deallocate_old, get_s_ds, &
      70              :         old_variables_type, rattle_roll_setup, set, tmp_variables_type, update_dealloc_tmp, &
      71              :         update_pv, update_veps, variable_timestep, vv_first, vv_second
      72              :    USE kinds,                           ONLY: dp,&
      73              :                                               max_line_length
      74              :    USE md_environment_types,            ONLY: get_md_env,&
      75              :                                               md_environment_type,&
      76              :                                               set_md_env
      77              :    USE message_passing,                 ONLY: mp_para_env_type
      78              :    USE metadynamics,                    ONLY: metadyn_integrator,&
      79              :                                               metadyn_velocities_colvar
      80              :    USE molecule_kind_list_types,        ONLY: molecule_kind_list_type
      81              :    USE molecule_kind_types,             ONLY: local_fixd_constraint_type,&
      82              :                                               molecule_kind_type
      83              :    USE molecule_list_types,             ONLY: molecule_list_type
      84              :    USE molecule_types,                  ONLY: global_constraint_type,&
      85              :                                               molecule_type
      86              :    USE particle_list_types,             ONLY: particle_list_type
      87              :    USE particle_types,                  ONLY: particle_type,&
      88              :                                               update_particle_set
      89              :    USE physcon,                         ONLY: femtoseconds
      90              :    USE qmmm_util,                       ONLY: apply_qmmm_walls_reflective
      91              :    USE qmmmx_update,                    ONLY: qmmmx_update_force_env
      92              :    USE qs_electrons_charge_mixing,      ONLY: charge_mixing_maybe_rebuild,&
      93              :                                               compute_mixed_workfunction,&
      94              :                                               push_lambda_to_mixed_env,&
      95              :                                               qs_electrons_charge_mixing_lambda
      96              :    USE qs_electrons_ne_electrode,       ONLY: apply_ne_core_correction
      97              :    USE qs_electrons_types,              ONLY: qs_electrons_type
      98              :    USE qs_electrons_utils,              ONLY: qs_electrons_drift,&
      99              :                                               qs_electrons_kick,&
     100              :                                               qs_electrons_thermostat_step
     101              :    USE qs_electrons_workfunction,       ONLY: qs_electrons_compute_workfunction
     102              :    USE qs_environment_types,            ONLY: get_qs_env
     103              :    USE reftraj_types,                   ONLY: REFTRAJ_EVAL_ENERGY_FORCES,&
     104              :                                               REFTRAJ_EVAL_NONE,&
     105              :                                               REFTRAJ_WRAP_CENTRAL,&
     106              :                                               REFTRAJ_WRAP_NONE,&
     107              :                                               REFTRAJ_WRAP_POSITIVE,&
     108              :                                               reftraj_type
     109              :    USE reftraj_util,                    ONLY: compute_msd_reftraj
     110              :    USE rt_propagation_methods,          ONLY: propagation_step
     111              :    USE rt_propagation_output,           ONLY: rt_prop_output
     112              :    USE rt_propagation_types,            ONLY: rt_prop_type
     113              :    USE shell_opt,                       ONLY: optimize_shell_core
     114              :    USE simpar_types,                    ONLY: simpar_type
     115              :    USE string_utilities,                ONLY: uppercase
     116              :    USE thermal_region_types,            ONLY: thermal_region_type,&
     117              :                                               thermal_regions_type
     118              :    USE thermostat_methods,              ONLY: apply_thermostat_baro,&
     119              :                                               apply_thermostat_particles,&
     120              :                                               apply_thermostat_shells
     121              :    USE thermostat_types,                ONLY: thermostat_type
     122              :    USE virial_methods,                  ONLY: virial_evaluate
     123              :    USE virial_types,                    ONLY: virial_type
     124              : #include "../base/base_uses.f90"
     125              : 
     126              :    IMPLICIT NONE
     127              : 
     128              :    PRIVATE
     129              : 
     130              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'integrator'
     131              : 
     132              :    PUBLIC :: isokin, langevin, nve, nvt, npt_i, npt_f, nve_respa
     133              :    PUBLIC :: nph_uniaxial_damped, nph_uniaxial, nvt_adiabatic, reftraj
     134              : 
     135              : CONTAINS
     136              : 
     137              : ! **************************************************************************************************
     138              : !> \brief Langevin integrator for particle positions & momenta (Brownian dynamics)
     139              : !> \param md_env ...
     140              : !> \par Literature
     141              : !>      - A. Ricci and G. Ciccotti, Mol. Phys. 101, 1927-1931 (2003)
     142              : !>      - For langevin regions:
     143              : !>        - L. Kantorovich, Phys. Rev. B 78, 094304 (2008)
     144              : !>        - L. Kantorovich and N. Rompotis, Phys. Rev. B 78, 094305 (2008)
     145              : !> \par History
     146              : !>   - Created (01.07.2005,MK)
     147              : !>   - Added support for only performing Langevin MD on a region of atoms
     148              : !>     (01.12.2013, LT)
     149              : !> \author Matthias Krack
     150              : ! **************************************************************************************************
     151          222 :    SUBROUTINE langevin(md_env)
     152              : 
     153              :       TYPE(md_environment_type), POINTER                 :: md_env
     154              : 
     155              :       INTEGER :: iparticle, iparticle_kind, iparticle_local, iparticle_reg, ireg, nparticle, &
     156              :          nparticle_kind, nparticle_local, nshell
     157              :       INTEGER, POINTER                                   :: itimes
     158          222 :       LOGICAL, ALLOCATABLE, DIMENSION(:)                 :: do_langevin
     159              :       REAL(KIND=dp)                                      :: c, c1, c2, c3, c4, dm, dt, gam, &
     160              :                                                             lambda_mix, mass, noisy_gamma_region, &
     161              :                                                             reg_temp, sigma
     162          222 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: var_w
     163              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: pos, vel, w
     164              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
     165          222 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     166              :       TYPE(atomic_kind_type), POINTER                    :: atomic_kind
     167              :       TYPE(cell_type), POINTER                           :: cell
     168              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     169              :       TYPE(distribution_1d_type), POINTER                :: local_molecules, local_particles
     170              :       TYPE(force_env_type), POINTER                      :: force_env
     171              :       TYPE(global_constraint_type), POINTER              :: gci
     172              :       TYPE(molecule_kind_list_type), POINTER             :: molecule_kinds
     173          222 :       TYPE(molecule_kind_type), DIMENSION(:), POINTER    :: molecule_kind_set
     174              :       TYPE(molecule_list_type), POINTER                  :: molecules
     175          222 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
     176              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     177              :       TYPE(particle_list_type), POINTER                  :: particles
     178          222 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     179              :       TYPE(qs_electrons_type), POINTER                   :: electrons
     180              :       TYPE(simpar_type), POINTER                         :: simpar
     181              :       TYPE(thermal_region_type), POINTER                 :: thermal_region
     182              :       TYPE(thermal_regions_type), POINTER                :: thermal_regions
     183              :       TYPE(virial_type), POINTER                         :: virial
     184              : 
     185          222 :       NULLIFY (cell, para_env, gci, force_env)
     186          222 :       NULLIFY (atomic_kinds, local_particles, subsys, local_molecules, molecule_kinds, molecules)
     187          222 :       NULLIFY (molecule_kind_set, molecule_set, particles, particle_set, simpar, virial)
     188          222 :       NULLIFY (thermal_region, thermal_regions, itimes, electrons)
     189              : 
     190              :       CALL get_md_env(md_env=md_env, simpar=simpar, force_env=force_env, &
     191              :                       para_env=para_env, thermal_regions=thermal_regions, &
     192          222 :                       itimes=itimes, electrons=electrons)
     193              : 
     194          222 :       dt = simpar%dt
     195          222 :       gam = simpar%gamma + simpar%shadow_gamma
     196              :       nshell = 0
     197              : 
     198          222 :       CALL force_env_get(force_env=force_env, subsys=subsys, cell=cell)
     199              : 
     200              :       ! Do some checks on coordinates and box
     201          222 :       CALL apply_qmmm_walls_reflective(force_env)
     202              : 
     203              :       CALL cp_subsys_get(subsys=subsys, &
     204              :                          atomic_kinds=atomic_kinds, &
     205              :                          gci=gci, &
     206              :                          local_particles=local_particles, &
     207              :                          local_molecules=local_molecules, &
     208              :                          molecules=molecules, &
     209              :                          molecule_kinds=molecule_kinds, &
     210              :                          nshell=nshell, &
     211              :                          particles=particles, &
     212          222 :                          virial=virial)
     213          222 :       IF (nshell /= 0) THEN
     214            0 :          CPABORT("Langevin dynamics is not yet implemented for core-shell models")
     215              :       END IF
     216              : 
     217          222 :       nparticle_kind = atomic_kinds%n_els
     218          222 :       atomic_kind_set => atomic_kinds%els
     219          222 :       molecule_kind_set => molecule_kinds%els
     220              : 
     221          222 :       nparticle = particles%n_els
     222          222 :       particle_set => particles%els
     223          222 :       molecule_set => molecules%els
     224              : 
     225              :       ! Setup the langevin regions information
     226          666 :       ALLOCATE (do_langevin(nparticle))
     227          222 :       IF (simpar%do_thermal_region) THEN
     228          392 :          DO iparticle = 1, nparticle
     229          392 :             do_langevin(iparticle) = thermal_regions%do_langevin(iparticle)
     230              :          END DO
     231              :       ELSE
     232        15604 :          do_langevin(1:nparticle) = .TRUE.
     233              :       END IF
     234              : 
     235              :       ! Allocate the temperature dependent variance (var_w) of the
     236              :       ! random variable for each atom. It may be different for different
     237              :       ! atoms because of the possibility of Langevin regions, and var_w
     238              :       ! for each region should depend on the temperature defined in the
     239              :       ! region
     240              :       ! RZK explains: sigma is the variance of the Wiener process associated
     241              :       ! with the stochastic term, sigma = m*var_w = m*(2*k_B*T*gamma*dt),
     242              :       ! noisy_gamma adds excessive noise that is not balanced by the damping term
     243          666 :       ALLOCATE (var_w(nparticle))
     244        15996 :       var_w(1:nparticle) = simpar%var_w
     245          222 :       IF (simpar%do_thermal_region) THEN
     246          136 :          DO ireg = 1, thermal_regions%nregions
     247           80 :             thermal_region => thermal_regions%thermal_region(ireg)
     248           80 :             noisy_gamma_region = thermal_region%noisy_gamma_region
     249          384 :             DO iparticle_reg = 1, thermal_region%npart
     250          248 :                iparticle = thermal_region%part_index(iparticle_reg)
     251          248 :                reg_temp = thermal_region%temp_expected
     252          328 :                var_w(iparticle) = 2.0_dp*reg_temp*simpar%dt*(simpar%gamma + noisy_gamma_region)
     253              :             END DO
     254              :          END DO
     255              :       END IF
     256              : 
     257              :       ! Allocate work storage
     258          666 :       ALLOCATE (pos(3, nparticle))
     259          222 :       pos(:, :) = 0.0_dp
     260              : 
     261          444 :       ALLOCATE (vel(3, nparticle))
     262          222 :       vel(:, :) = 0.0_dp
     263              : 
     264          444 :       ALLOCATE (w(3, nparticle))
     265          222 :       w(:, :) = 0.0_dp
     266              : 
     267          222 :       IF (simpar%constraint) CALL getold(gci, local_molecules, molecule_set, &
     268            4 :                                          molecule_kind_set, particle_set, cell)
     269              : 
     270              :       ! Generate random variables
     271          666 :       DO iparticle_kind = 1, nparticle_kind
     272          444 :          atomic_kind => atomic_kind_set(iparticle_kind)
     273          444 :          CALL get_atomic_kind(atomic_kind=atomic_kind, mass=mass)
     274          444 :          nparticle_local = local_particles%n_el(iparticle_kind)
     275         8553 :          DO iparticle_local = 1, nparticle_local
     276         7887 :             iparticle = local_particles%list(iparticle_kind)%array(iparticle_local)
     277         8331 :             IF (do_langevin(iparticle)) THEN
     278         7863 :                sigma = var_w(iparticle)*mass
     279              :                ASSOCIATE (rng_stream => local_particles%local_particle_set(iparticle_kind)% &
     280              :                           rng(iparticle_local))
     281        15726 :                   w(1, iparticle) = rng_stream%stream%next(variance=sigma)
     282         7863 :                   w(2, iparticle) = rng_stream%stream%next(variance=sigma)
     283        15726 :                   w(3, iparticle) = rng_stream%stream%next(variance=sigma)
     284              :                END ASSOCIATE
     285              :             END IF
     286              :          END DO
     287              :       END DO
     288              : 
     289          222 :       DEALLOCATE (var_w)
     290              : 
     291              :       ! Apply fix atom constraint
     292          222 :       CALL fix_atom_control(force_env, w)
     293              : 
     294              :       ! ---------------------------------------------------------------------
     295              :       ! Constant-potential electronic dynamics, first electronic O half-step.
     296              :       !
     297              :       ! These hooks mirror nvt() one for one.  Until they were added, langevin()
     298              :       ! propagated the atoms normally while never calling qs_electrons_kick,
     299              :       ! qs_electrons_drift, charge_mixing_maybe_rebuild or
     300              :       ! push_lambda_to_mixed_env: q(t) stayed frozen at Q_INIT, the mixing weight
     301              :       ! never updated, and the run completed and wrote entirely plausible output
     302              :       ! that meant nothing.  nve(), isokin(), nvt_adiabatic() and the npt/nph
     303              :       ! family still have this property -- see the guard in md_run.F.
     304              :       !
     305              :       ! The electronic potentiostat remains CSVR regardless of what the atoms
     306              :       ! use, independent of the atomic thermostat.  qs_electrons_thermostat_step computes
     307              :       ! taut = (tau/femtoseconds)/(0.5*dt) on the assumption that it is called
     308              :       ! TWICE per MD step, which is why there is a matching call at the end.
     309          222 :       IF (ASSOCIATED(electrons)) THEN
     310              :          ! report_only ("&ELECTRONS 0"): q is never propagated, so no O step.
     311            0 :          IF (.NOT. electrons%report_only) CALL qs_electrons_thermostat_step(electrons, 0.5_dp*dt)
     312              :       END IF
     313              :       ! ---------------------------------------------------------------------
     314              : 
     315              :       ! Velocity Verlet (first part)
     316          222 :       c = EXP(-0.25_dp*dt*gam)
     317          222 :       c2 = c*c
     318          222 :       c4 = c2*c2
     319          222 :       c1 = dt*c2
     320              : 
     321          666 :       DO iparticle_kind = 1, nparticle_kind
     322          444 :          atomic_kind => atomic_kind_set(iparticle_kind)
     323          444 :          CALL get_atomic_kind(atomic_kind=atomic_kind, mass=mass)
     324          444 :          nparticle_local = local_particles%n_el(iparticle_kind)
     325          444 :          dm = 0.5_dp*dt/mass
     326          444 :          c3 = dm/c2
     327         8553 :          DO iparticle_local = 1, nparticle_local
     328         7887 :             iparticle = local_particles%list(iparticle_kind)%array(iparticle_local)
     329         8331 :             IF (do_langevin(iparticle)) THEN
     330              :                vel(:, iparticle) = particle_set(iparticle)%v(:) + &
     331        31452 :                                    c3*particle_set(iparticle)%f(:)
     332              :                pos(:, iparticle) = particle_set(iparticle)%r(:) + &
     333              :                                    c1*particle_set(iparticle)%v(:) + &
     334              :                                    c*dm*(dt*particle_set(iparticle)%f(:) + &
     335        31452 :                                          w(:, iparticle))
     336              :             ELSE
     337              :                vel(:, iparticle) = particle_set(iparticle)%v(:) + &
     338           96 :                                    dm*particle_set(iparticle)%f(:)
     339              :                pos(:, iparticle) = particle_set(iparticle)%r(:) + &
     340              :                                    dt*particle_set(iparticle)%v(:) + &
     341           96 :                                    dm*dt*particle_set(iparticle)%f(:)
     342              :             END IF
     343              :          END DO
     344              :       END DO
     345              : 
     346          222 :       IF (simpar%constraint) THEN
     347              :          ! Possibly update the target values
     348              :          CALL shake_update_targets(gci, local_molecules, molecule_set, &
     349            4 :                                    molecule_kind_set, dt, force_env%root_section)
     350              : 
     351              :          CALL shake_control(gci, local_molecules, molecule_set, molecule_kind_set, &
     352              :                             particle_set, pos, vel, dt, simpar%shake_tol, &
     353              :                             simpar%info_constraint, simpar%lagrange_multipliers, &
     354            4 :                             simpar%dump_lm, cell, para_env, local_particles)
     355              :       END IF
     356              : 
     357              :       ! Broadcast the new particle positions
     358          222 :       CALL update_particle_set(particle_set, para_env, pos=pos)
     359              : 
     360          222 :       DEALLOCATE (pos)
     361              : 
     362              :       ! ---------------------------------------------------------------------
     363              :       ! Electronic half-kick (B) and drift (A), then re-bracket and push the new
     364              :       ! mixing weight, so that the force evaluation below sees BOTH the new
     365              :       ! positions and the new q(t) -- the same ordering as nvt().
     366          222 :       IF (ASSOCIATED(electrons)) THEN
     367              :          ! report_only ("&ELECTRONS 0"): none of this runs. q is not propagated,
     368              :          ! the endpoints are never rebuilt, and apply_ne_core_correction is NOT
     369              :          ! called -- that last one matters, since it would alter the system's
     370              :          ! charge, which in report-only mode must stay exactly as the input set
     371              :          ! it. Only the work-function readback below still runs.
     372            0 :          IF (.NOT. electrons%report_only) THEN
     373            0 :             CALL qs_electrons_kick(electrons, 0.5_dp*dt)
     374            0 :             CALL qs_electrons_drift(electrons, dt)
     375            0 :             IF (electrons%charge_mixing_enabled) THEN
     376            0 :                CALL charge_mixing_maybe_rebuild(force_env, electrons)
     377            0 :                CALL qs_electrons_charge_mixing_lambda(electrons, lambda_mix)
     378            0 :                CALL push_lambda_to_mixed_env(force_env, lambda_mix)
     379              :             ELSE
     380            0 :                CALL apply_ne_core_correction(force_env, electrons%q, electrons%neutral_electrons)
     381              :             END IF
     382              :          END IF
     383              :       END IF
     384              :       ! ---------------------------------------------------------------------
     385              : 
     386              :       ! Update forces
     387          222 :       CALL force_env_calc_energy_force(force_env)
     388              : 
     389              :       ! Refresh the mixed workfunction and Fermi level from the freshly
     390              :       ! converged density; qs_electrons_force reads current_workfunction, so this
     391              :       ! must precede the closing electronic half-kick below.
     392          222 :       IF (ASSOCIATED(electrons)) THEN
     393            0 :          IF (electrons%charge_mixing_enabled) THEN
     394              :             CALL compute_mixed_workfunction(force_env, electrons%potential_average_axis, &
     395              :                                             electrons%potential_average_min, electrons%potential_average_max, &
     396              :                                             electrons%q, electrons%charge_mixing_span, &
     397              :                                             electrons%current_fermi_level_eV, electrons%current_workfunction, &
     398            0 :                                             lambda_in=lambda_mix)
     399              :          ELSE
     400              :             CALL qs_electrons_compute_workfunction(force_env, electrons%potential_average_axis, &
     401              :                                                    electrons%potential_average_min, electrons%potential_average_max, &
     402            0 :                                                    electrons%current_fermi_level_eV, electrons%current_workfunction)
     403              :          END IF
     404              :       END IF
     405              : 
     406              :       ! Metadynamics
     407          222 :       CALL metadyn_integrator(force_env, itimes, vel)
     408              : 
     409              :       ! Update Verlet (second part)
     410          666 :       DO iparticle_kind = 1, nparticle_kind
     411          444 :          atomic_kind => atomic_kind_set(iparticle_kind)
     412          444 :          CALL get_atomic_kind(atomic_kind=atomic_kind, mass=mass)
     413          444 :          dm = 0.5_dp*dt/mass
     414          444 :          c3 = dm/c2
     415          444 :          nparticle_local = local_particles%n_el(iparticle_kind)
     416         8553 :          DO iparticle_local = 1, nparticle_local
     417         7887 :             iparticle = local_particles%list(iparticle_kind)%array(iparticle_local)
     418         8331 :             IF (do_langevin(iparticle)) THEN
     419         7863 :                vel(1, iparticle) = vel(1, iparticle) + c3*particle_set(iparticle)%f(1)
     420         7863 :                vel(2, iparticle) = vel(2, iparticle) + c3*particle_set(iparticle)%f(2)
     421         7863 :                vel(3, iparticle) = vel(3, iparticle) + c3*particle_set(iparticle)%f(3)
     422         7863 :                vel(1, iparticle) = c4*vel(1, iparticle) + c2*w(1, iparticle)/mass
     423         7863 :                vel(2, iparticle) = c4*vel(2, iparticle) + c2*w(2, iparticle)/mass
     424         7863 :                vel(3, iparticle) = c4*vel(3, iparticle) + c2*w(3, iparticle)/mass
     425              :             ELSE
     426           24 :                vel(1, iparticle) = vel(1, iparticle) + dm*particle_set(iparticle)%f(1)
     427           24 :                vel(2, iparticle) = vel(2, iparticle) + dm*particle_set(iparticle)%f(2)
     428           24 :                vel(3, iparticle) = vel(3, iparticle) + dm*particle_set(iparticle)%f(3)
     429              :             END IF
     430              :          END DO
     431              :       END DO
     432              : 
     433              :       ! Closing electronic half-kick (B), using the workfunction just recomputed
     434              :       ! from the new density.
     435          222 :       IF (ASSOCIATED(electrons)) THEN
     436              :          ! report_only ("&ELECTRONS 0"): q is never propagated, so no B step.
     437            0 :          IF (.NOT. electrons%report_only) CALL qs_electrons_kick(electrons, 0.5_dp*dt)
     438              :       END IF
     439              : 
     440          222 :       IF (simpar%temperature_annealing) THEN
     441           40 :          simpar%temp_ext = simpar%temp_ext*simpar%f_temperature_annealing
     442           40 :          simpar%var_w = simpar%var_w*simpar%f_temperature_annealing
     443              :       END IF
     444              : 
     445          222 :       IF (simpar%constraint) THEN
     446              :          CALL rattle_control(gci, local_molecules, molecule_set, molecule_kind_set, &
     447              :                              particle_set, vel, dt, simpar%shake_tol, &
     448              :                              simpar%info_constraint, simpar%lagrange_multipliers, &
     449            4 :                              simpar%dump_lm, cell, para_env, local_particles)
     450              :       END IF
     451              : 
     452              :       ! Second electronic O half-step, completing the pair assumed by
     453              :       ! qs_electrons_thermostat_step's taut = (tau/femtoseconds)/(0.5*dt).
     454          222 :       IF (ASSOCIATED(electrons)) THEN
     455              :          ! report_only ("&ELECTRONS 0"): q is never propagated, so no O step.
     456            0 :          IF (.NOT. electrons%report_only) CALL qs_electrons_thermostat_step(electrons, 0.5_dp*dt)
     457              :       END IF
     458              : 
     459              :       ! Broadcast the new particle velocities
     460          222 :       CALL update_particle_set(particle_set, para_env, vel=vel)
     461              : 
     462          222 :       DEALLOCATE (vel)
     463              : 
     464          222 :       DEALLOCATE (w)
     465              : 
     466          222 :       DEALLOCATE (do_langevin)
     467              : 
     468              :       ! Update virial
     469          222 :       IF (simpar%constraint) CALL pv_constraint(gci, local_molecules, molecule_set, &
     470            4 :                                                 molecule_kind_set, particle_set, virial, para_env)
     471              : 
     472              :       CALL virial_evaluate(atomic_kind_set, particle_set, local_particles, &
     473          222 :                            virial, para_env)
     474              : 
     475          444 :    END SUBROUTINE langevin
     476              : 
     477              : ! **************************************************************************************************
     478              : !> \brief nve integrator for particle positions & momenta
     479              : !> \param md_env ...
     480              : !> \param globenv ...
     481              : !> \par History
     482              : !>   - the local particle lists are used instead of pnode (Sep. 2003,MK)
     483              : !>   - usage of fragments retrieved from the force environment (Oct. 2003,MK)
     484              : !> \author CJM
     485              : ! **************************************************************************************************
     486        29153 :    SUBROUTINE nve(md_env, globenv)
     487              : 
     488              :       TYPE(md_environment_type), POINTER                 :: md_env
     489              :       TYPE(global_environment_type), POINTER             :: globenv
     490              : 
     491              :       INTEGER                                            :: i_iter, n_iter, nparticle, &
     492              :                                                             nparticle_kind, nshell
     493              :       INTEGER, POINTER                                   :: itimes
     494              :       LOGICAL                                            :: deallocate_vel, ehrenfest_md, &
     495              :                                                             shell_adiabatic, shell_check_distance, &
     496              :                                                             shell_present
     497              :       REAL(KIND=dp)                                      :: dt
     498        29153 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: v_old
     499              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
     500        29153 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     501              :       TYPE(cell_type), POINTER                           :: cell
     502              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     503              :       TYPE(dft_control_type), POINTER                    :: dft_control
     504              :       TYPE(distribution_1d_type), POINTER                :: local_molecules, local_particles
     505              :       TYPE(force_env_type), POINTER                      :: force_env
     506              :       TYPE(global_constraint_type), POINTER              :: gci
     507              :       TYPE(molecule_kind_list_type), POINTER             :: molecule_kinds
     508        29153 :       TYPE(molecule_kind_type), DIMENSION(:), POINTER    :: molecule_kind_set
     509              :       TYPE(molecule_list_type), POINTER                  :: molecules
     510        29153 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
     511              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     512              :       TYPE(particle_list_type), POINTER                  :: core_particles, particles, &
     513              :                                                             shell_particles
     514        29153 :       TYPE(particle_type), DIMENSION(:), POINTER         :: core_particle_set, particle_set, &
     515        29153 :                                                             shell_particle_set
     516              :       TYPE(rt_prop_type), POINTER                        :: rtp
     517              :       TYPE(simpar_type), POINTER                         :: simpar
     518              :       TYPE(thermostat_type), POINTER                     :: thermostat_coeff, thermostat_shell
     519              :       TYPE(tmp_variables_type), POINTER                  :: tmp
     520              :       TYPE(virial_type), POINTER                         :: virial
     521              : 
     522        29153 :       NULLIFY (thermostat_coeff, tmp)
     523        29153 :       NULLIFY (subsys, simpar, para_env, cell, gci, force_env, virial)
     524        29153 :       NULLIFY (atomic_kinds, local_particles, molecules, molecule_kind_set, molecule_set, particle_set)
     525        29153 :       NULLIFY (shell_particles, shell_particle_set, core_particles, &
     526        29153 :                core_particle_set, thermostat_shell, dft_control, itimes)
     527              :       CALL get_md_env(md_env=md_env, simpar=simpar, force_env=force_env, &
     528              :                       thermostat_coeff=thermostat_coeff, thermostat_shell=thermostat_shell, &
     529        29153 :                       para_env=para_env, ehrenfest_md=ehrenfest_md, itimes=itimes)
     530        29153 :       dt = simpar%dt
     531        29153 :       CALL force_env_get(force_env=force_env, subsys=subsys, cell=cell)
     532              : 
     533              :       ! Do some checks on coordinates and box
     534        29153 :       CALL apply_qmmm_walls_reflective(force_env)
     535              : 
     536              :       CALL cp_subsys_get(subsys=subsys, atomic_kinds=atomic_kinds, local_particles=local_particles, &
     537              :                          particles=particles, local_molecules=local_molecules, molecules=molecules, &
     538        29153 :                          molecule_kinds=molecule_kinds, gci=gci, virial=virial)
     539              : 
     540        29153 :       nparticle_kind = atomic_kinds%n_els
     541        29153 :       atomic_kind_set => atomic_kinds%els
     542        29153 :       molecule_kind_set => molecule_kinds%els
     543              : 
     544        29153 :       nparticle = particles%n_els
     545        29153 :       particle_set => particles%els
     546        29153 :       molecule_set => molecules%els
     547              : 
     548              :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, &
     549              :                                shell_present=shell_present, shell_adiabatic=shell_adiabatic, &
     550        29153 :                                shell_check_distance=shell_check_distance)
     551              : 
     552        29153 :       IF (shell_present) THEN
     553              :          CALL cp_subsys_get(subsys=subsys, shell_particles=shell_particles, &
     554          600 :                             core_particles=core_particles)
     555          600 :          shell_particle_set => shell_particles%els
     556          600 :          nshell = SIZE(shell_particles%els)
     557              : 
     558          600 :          IF (shell_adiabatic) THEN
     559          600 :             core_particle_set => core_particles%els
     560              :          END IF
     561              :       END IF
     562              : 
     563        29153 :       CALL allocate_tmp(md_env, tmp, nparticle, nshell, shell_adiabatic)
     564              : 
     565              :       ! Apply thermostat over the full set of shells if required
     566        29153 :       IF (shell_adiabatic) THEN
     567              :          CALL apply_thermostat_shells(thermostat_shell, atomic_kind_set, particle_set, &
     568              :                                       local_particles, para_env, shell_particle_set=shell_particle_set, &
     569          600 :                                       core_particle_set=core_particle_set)
     570              :       END IF
     571              : 
     572        29153 :       IF (simpar%constraint) CALL getold(gci, local_molecules, molecule_set, &
     573        13388 :                                          molecule_kind_set, particle_set, cell)
     574              : 
     575              :       ! Velocity Verlet (first part)
     576              :       CALL vv_first(tmp, atomic_kind_set, local_particles, particle_set, &
     577        29153 :                     core_particle_set, shell_particle_set, nparticle_kind, shell_adiabatic, dt)
     578              : 
     579        29153 :       IF (simpar%variable_dt) CALL variable_timestep(md_env, tmp, dt, simpar, para_env, atomic_kind_set, &
     580              :                                                      local_particles, particle_set, core_particle_set, shell_particle_set, &
     581          280 :                                                      nparticle_kind, shell_adiabatic)
     582              : 
     583        29153 :       IF (simpar%constraint) THEN
     584              :          ! Possibly update the target values
     585              :          CALL shake_update_targets(gci, local_molecules, molecule_set, &
     586        13388 :                                    molecule_kind_set, dt, force_env%root_section)
     587              : 
     588              :          CALL shake_control(gci, local_molecules, molecule_set, &
     589              :                             molecule_kind_set, particle_set, tmp%pos, tmp%vel, dt, simpar%shake_tol, &
     590              :                             simpar%info_constraint, simpar%lagrange_multipliers, simpar%dump_lm, &
     591        13388 :                             cell, para_env, local_particles)
     592              :       END IF
     593              : 
     594              :       ! Broadcast the new particle positions and deallocate pos part of temporary
     595              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
     596        29153 :                               core_particle_set, para_env, shell_adiabatic, pos=.TRUE.)
     597              : 
     598        29153 :       IF (shell_adiabatic .AND. shell_check_distance) THEN
     599              :          CALL optimize_shell_core(force_env, particle_set, &
     600          180 :                                   shell_particle_set, core_particle_set, globenv, tmp=tmp, check=.TRUE.)
     601              :       END IF
     602              : 
     603              :       ! Update forces
     604              :       ! In case of ehrenfest dynamics, velocities need to be iterated
     605        29153 :       IF (ehrenfest_md) THEN
     606         1056 :          ALLOCATE (v_old(3, SIZE(tmp%vel, 2)))
     607         4568 :          v_old(:, :) = tmp%vel
     608              :          CALL vv_second(tmp, atomic_kind_set, local_particles, particle_set, &
     609          352 :                         core_particle_set, shell_particle_set, nparticle_kind, shell_adiabatic, dt)
     610              :          CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
     611              :                                  core_particle_set, para_env, shell_adiabatic, vel=.TRUE., &
     612          352 :                                  should_deall_vel=.FALSE.)
     613         4568 :          tmp%vel = v_old
     614          352 :          CALL get_qs_env(force_env%qs_env, dft_control=dft_control)
     615          352 :          n_iter = dft_control%rtp_control%max_iter
     616              :       ELSE
     617              :          n_iter = 1
     618              :       END IF
     619              : 
     620        59106 :       DO i_iter = 1, n_iter
     621              : 
     622        30305 :          IF (ehrenfest_md) THEN
     623         1504 :             CALL get_qs_env(qs_env=force_env%qs_env, rtp=rtp)
     624         1504 :             rtp%iter = i_iter
     625        19640 :             tmp%vel = v_old
     626         1504 :             CALL propagation_step(force_env%qs_env, rtp, dft_control%rtp_control)
     627              :          END IF
     628              : 
     629              :          ![NB] let nve work with force mixing which does not have consistent energies and forces
     630        30305 :          CALL force_env_calc_energy_force(force_env, require_consistent_energy_force=.FALSE.)
     631              : 
     632        30305 :          IF (ehrenfest_md) THEN
     633         1504 :             CALL rt_prop_output(force_env%qs_env, ehrenfest, delta_iter=force_env%qs_env%rtp%delta_iter)
     634              :          END IF
     635              : 
     636              :          ! Metadynamics
     637        30305 :          CALL metadyn_integrator(force_env, itimes, tmp%vel)
     638              : 
     639              :          ! Velocity Verlet (second part)
     640              :          CALL vv_second(tmp, atomic_kind_set, local_particles, particle_set, &
     641        30305 :                         core_particle_set, shell_particle_set, nparticle_kind, shell_adiabatic, dt)
     642              : 
     643        30305 :          IF (simpar%constraint) CALL rattle_control(gci, local_molecules, molecule_set, &
     644              :                                                     molecule_kind_set, particle_set, tmp%vel, dt, simpar%shake_tol, &
     645              :                                                     simpar%info_constraint, simpar%lagrange_multipliers, simpar%dump_lm, &
     646        13388 :                                                     cell, para_env, local_particles)
     647              : 
     648              :          ! Apply thermostat over the full set of shell if required
     649        30305 :          IF (shell_adiabatic) THEN
     650              :             CALL apply_thermostat_shells(thermostat_shell, atomic_kind_set, particle_set, &
     651              :                                          local_particles, para_env, vel=tmp%vel, &
     652          600 :                                          shell_vel=tmp%shell_vel, core_vel=tmp%core_vel)
     653              :          END IF
     654              : 
     655        30305 :          IF (simpar%annealing) THEN
     656            0 :             tmp%vel(:, :) = tmp%vel(:, :)*simpar%f_annealing
     657            0 :             IF (shell_adiabatic) THEN
     658              :                CALL shell_scale_comv(atomic_kind_set, local_particles, particle_set, &
     659            0 :                                      tmp%vel, tmp%shell_vel, tmp%core_vel)
     660              :             END IF
     661              :          END IF
     662              : 
     663        30305 :          IF (ehrenfest_md) deallocate_vel = force_env%qs_env%rtp%converged
     664        30305 :          IF (i_iter == n_iter) deallocate_vel = .TRUE.
     665              :          ! Broadcast the new particle velocities and deallocate the full temporary
     666              :          CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
     667              :                                  core_particle_set, para_env, shell_adiabatic, vel=.TRUE., &
     668        30305 :                                  should_deall_vel=deallocate_vel)
     669        59458 :          IF (ehrenfest_md) THEN
     670         1504 :             IF (force_env%qs_env%rtp%converged) EXIT
     671              :          END IF
     672              : 
     673              :       END DO
     674              : 
     675              :       ! Update virial
     676        29153 :       IF (simpar%constraint) CALL pv_constraint(gci, local_molecules, &
     677        13388 :                                                 molecule_set, molecule_kind_set, particle_set, virial, para_env)
     678              : 
     679              :       CALL virial_evaluate(atomic_kind_set, particle_set, &
     680        29153 :                            local_particles, virial, para_env)
     681              : 
     682        58306 :    END SUBROUTINE nve
     683              : 
     684              : ! **************************************************************************************************
     685              : !> \brief simplest version of the isokinetic gaussian thermostat
     686              : !> \param md_env ...
     687              : !> \par History
     688              : !>   - Created [2004-07]
     689              : !> \author Joost VandeVondele
     690              : !> \note
     691              : !>      - time reversible, and conserves the kinetic energy to machine precision
     692              : !>      - is not yet supposed to work for e.g. constraints, our the extended version
     693              : !>        of this thermostat
     694              : !>        see:
     695              : !>         - Zhang F. , JCP 106, 6102 (1997)
     696              : !>         - Minary P. et al, JCP 118, 2510 (2003)
     697              : ! **************************************************************************************************
     698           12 :    SUBROUTINE isokin(md_env)
     699              : 
     700              :       TYPE(md_environment_type), POINTER                 :: md_env
     701              : 
     702              :       INTEGER                                            :: nparticle, nparticle_kind, nshell
     703              :       INTEGER, POINTER                                   :: itimes
     704              :       LOGICAL                                            :: shell_adiabatic, shell_present
     705              :       REAL(KIND=dp)                                      :: dt
     706              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
     707            6 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     708              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     709              :       TYPE(distribution_1d_type), POINTER                :: local_particles
     710              :       TYPE(force_env_type), POINTER                      :: force_env
     711              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     712              :       TYPE(particle_list_type), POINTER                  :: core_particles, particles, &
     713              :                                                             shell_particles
     714            6 :       TYPE(particle_type), DIMENSION(:), POINTER         :: core_particle_set, particle_set, &
     715            6 :                                                             shell_particle_set
     716              :       TYPE(simpar_type), POINTER                         :: simpar
     717              :       TYPE(tmp_variables_type), POINTER                  :: tmp
     718              : 
     719            6 :       NULLIFY (force_env, tmp, simpar, itimes)
     720            6 :       NULLIFY (atomic_kinds, para_env, subsys, local_particles)
     721            6 :       NULLIFY (core_particles, particles, shell_particles)
     722            6 :       NULLIFY (core_particle_set, particle_set, shell_particle_set)
     723              : 
     724              :       CALL get_md_env(md_env=md_env, simpar=simpar, force_env=force_env, &
     725            6 :                       para_env=para_env, itimes=itimes)
     726              : 
     727            6 :       dt = simpar%dt
     728              : 
     729            6 :       CALL force_env_get(force_env=force_env, subsys=subsys)
     730              : 
     731              :       ! Do some checks on coordinates and box
     732            6 :       CALL apply_qmmm_walls_reflective(force_env)
     733              : 
     734            6 :       IF (simpar%constraint) THEN
     735            0 :          CPABORT("Constraints not yet implemented")
     736              :       END IF
     737              : 
     738              :       CALL cp_subsys_get(subsys=subsys, atomic_kinds=atomic_kinds, &
     739              :                          local_particles=local_particles, &
     740            6 :                          particles=particles)
     741              : 
     742            6 :       nparticle_kind = atomic_kinds%n_els
     743            6 :       atomic_kind_set => atomic_kinds%els
     744            6 :       nparticle = particles%n_els
     745            6 :       particle_set => particles%els
     746              : 
     747              :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, &
     748            6 :                                shell_present=shell_present, shell_adiabatic=shell_adiabatic)
     749              : 
     750            6 :       IF (shell_present) THEN
     751              :          CALL cp_subsys_get(subsys=subsys, shell_particles=shell_particles, &
     752            0 :                             core_particles=core_particles)
     753            0 :          shell_particle_set => shell_particles%els
     754            0 :          nshell = SIZE(shell_particles%els)
     755              : 
     756            0 :          IF (shell_adiabatic) THEN
     757            0 :             core_particle_set => core_particles%els
     758              :          END IF
     759              :       END IF
     760              : 
     761            6 :       CALL allocate_tmp(md_env, tmp, nparticle, nshell, shell_adiabatic)
     762              : 
     763              :       ! compute s,ds
     764              :       CALL get_s_ds(tmp, nparticle_kind, atomic_kind_set, local_particles, particle_set, &
     765            6 :                     dt, para_env)
     766              : 
     767              :       ! Velocity Verlet (first part)
     768           24 :       tmp%scale_v(1:3) = SQRT(1.0_dp/tmp%ds)
     769           24 :       tmp%poly_v(1:3) = 2.0_dp*tmp%s/SQRT(tmp%ds)/dt
     770              :       CALL vv_first(tmp, atomic_kind_set, local_particles, particle_set, &
     771              :                     core_particle_set, shell_particle_set, nparticle_kind, &
     772            6 :                     shell_adiabatic, dt)
     773              : 
     774            6 :       IF (simpar%variable_dt) CALL variable_timestep(md_env, tmp, dt, simpar, para_env, atomic_kind_set, &
     775              :                                                      local_particles, particle_set, core_particle_set, shell_particle_set, &
     776            0 :                                                      nparticle_kind, shell_adiabatic)
     777              : 
     778              :       ! Broadcast the new particle positions and deallocate the pos components of temporary
     779              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
     780            6 :                               core_particle_set, para_env, shell_adiabatic, pos=.TRUE.)
     781              : 
     782            6 :       CALL force_env_calc_energy_force(force_env)
     783              : 
     784              :       ! Metadynamics
     785            6 :       CALL metadyn_integrator(force_env, itimes, tmp%vel)
     786              : 
     787              :       ! compute s,ds
     788              :       CALL get_s_ds(tmp, nparticle_kind, atomic_kind_set, local_particles, particle_set, &
     789            6 :                     dt, para_env, tmpv=.TRUE.)
     790              : 
     791              :       ! Velocity Verlet (second part)
     792           24 :       tmp%scale_v(1:3) = SQRT(1.0_dp/tmp%ds)
     793           24 :       tmp%poly_v(1:3) = 2.0_dp*tmp%s/SQRT(tmp%ds)/dt
     794              :       CALL vv_second(tmp, atomic_kind_set, local_particles, particle_set, &
     795              :                      core_particle_set, shell_particle_set, nparticle_kind, &
     796            6 :                      shell_adiabatic, dt)
     797              : 
     798            6 :       IF (simpar%annealing) tmp%vel(:, :) = tmp%vel(:, :)*simpar%f_annealing
     799              : 
     800              :       !  Broadcast the new particle velocities and deallocate the temporary
     801              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
     802            6 :                               core_particle_set, para_env, shell_adiabatic, vel=.TRUE.)
     803              : 
     804            6 :    END SUBROUTINE isokin
     805              : ! **************************************************************************************************
     806              : !> \brief nvt adiabatic integrator for particle positions & momenta
     807              : !> \param md_env ...
     808              : !> \param globenv ...
     809              : !> \par History
     810              : !>   - the local particle lists are used instead of pnode (Sep. 2003,MK)
     811              : !>   - usage of fragments retrieved from the force environment (Oct. 2003,MK)
     812              : !> \author CJM
     813              : ! **************************************************************************************************
     814            0 :    SUBROUTINE nvt_adiabatic(md_env, globenv)
     815              : 
     816              :       TYPE(md_environment_type), POINTER                 :: md_env
     817              :       TYPE(global_environment_type), POINTER             :: globenv
     818              : 
     819              :       INTEGER                                            :: ivar, nparticle, nparticle_kind, nshell
     820              :       INTEGER, POINTER                                   :: itimes
     821              :       LOGICAL                                            :: shell_adiabatic, shell_check_distance, &
     822              :                                                             shell_present
     823              :       REAL(KIND=dp)                                      :: dt
     824            0 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: rand
     825              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
     826            0 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     827              :       TYPE(cell_type), POINTER                           :: cell
     828              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     829              :       TYPE(distribution_1d_type), POINTER                :: local_molecules, local_particles
     830              :       TYPE(force_env_type), POINTER                      :: force_env
     831              :       TYPE(global_constraint_type), POINTER              :: gci
     832              :       TYPE(molecule_kind_list_type), POINTER             :: molecule_kinds
     833            0 :       TYPE(molecule_kind_type), DIMENSION(:), POINTER    :: molecule_kind_set
     834              :       TYPE(molecule_list_type), POINTER                  :: molecules
     835            0 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
     836              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     837              :       TYPE(particle_list_type), POINTER                  :: core_particles, particles, &
     838              :                                                             shell_particles
     839            0 :       TYPE(particle_type), DIMENSION(:), POINTER         :: core_particle_set, particle_set, &
     840            0 :                                                             shell_particle_set
     841              :       TYPE(simpar_type), POINTER                         :: simpar
     842              :       TYPE(thermostat_type), POINTER                     :: thermostat_coeff, thermostat_fast, &
     843              :                                                             thermostat_shell, thermostat_slow
     844              :       TYPE(tmp_variables_type), POINTER                  :: tmp
     845              :       TYPE(virial_type), POINTER                         :: virial
     846              : 
     847            0 :       NULLIFY (gci, force_env, thermostat_coeff, tmp, &
     848            0 :                thermostat_fast, thermostat_slow, thermostat_shell, cell, shell_particles, &
     849            0 :                shell_particle_set, core_particles, core_particle_set, rand)
     850            0 :       NULLIFY (para_env, subsys, local_molecules, local_particles, molecule_kinds, &
     851            0 :                molecules, molecule_kind_set, molecule_set, atomic_kinds, particles)
     852            0 :       NULLIFY (simpar, itimes)
     853              : 
     854              :       CALL get_md_env(md_env=md_env, simpar=simpar, force_env=force_env, &
     855              :                       thermostat_fast=thermostat_fast, thermostat_slow=thermostat_slow, &
     856              :                       thermostat_coeff=thermostat_coeff, thermostat_shell=thermostat_shell, &
     857            0 :                       para_env=para_env, itimes=itimes)
     858            0 :       dt = simpar%dt
     859              : 
     860            0 :       CALL force_env_get(force_env=force_env, subsys=subsys, cell=cell)
     861              : 
     862              :       ! Do some checks on coordinates and box
     863            0 :       CALL apply_qmmm_walls_reflective(force_env)
     864              : 
     865              :       CALL cp_subsys_get(subsys=subsys, atomic_kinds=atomic_kinds, local_particles=local_particles, &
     866              :                          particles=particles, local_molecules=local_molecules, molecules=molecules, &
     867            0 :                          molecule_kinds=molecule_kinds, gci=gci, virial=virial)
     868              : 
     869            0 :       nparticle_kind = atomic_kinds%n_els
     870            0 :       atomic_kind_set => atomic_kinds%els
     871            0 :       molecule_kind_set => molecule_kinds%els
     872              : 
     873            0 :       nparticle = particles%n_els
     874            0 :       particle_set => particles%els
     875            0 :       molecule_set => molecules%els
     876              : 
     877              :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, &
     878              :                                shell_present=shell_present, shell_adiabatic=shell_adiabatic, &
     879            0 :                                shell_check_distance=shell_check_distance)
     880              : 
     881            0 :       IF (ASSOCIATED(force_env%meta_env)) THEN
     882              :          ! Allocate random number for Langevin Thermostat acting on COLVARS
     883            0 :          IF (force_env%meta_env%langevin) THEN
     884            0 :             ALLOCATE (rand(force_env%meta_env%n_colvar))
     885            0 :             rand(:) = 0.0_dp
     886              :          END IF
     887              :       END IF
     888              : 
     889              :       !  Allocate work storage for positions and velocities
     890            0 :       IF (shell_present) THEN
     891              :          CALL cp_subsys_get(subsys=subsys, shell_particles=shell_particles, &
     892            0 :                             core_particles=core_particles)
     893            0 :          shell_particle_set => shell_particles%els
     894            0 :          nshell = SIZE(shell_particles%els)
     895              : 
     896            0 :          IF (shell_adiabatic) THEN
     897            0 :             core_particle_set => core_particles%els
     898              :          END IF
     899              :       END IF
     900              : 
     901            0 :       CALL allocate_tmp(md_env, tmp, nparticle, nshell, shell_adiabatic)
     902              : 
     903              :       ! Apply Thermostat over the full set of particles
     904            0 :       IF (shell_adiabatic) THEN
     905              : !       CALL apply_thermostat_particles(thermostat_part, molecule_kind_set, molecule_set,&
     906              : !            particle_set, local_molecules, para_env, shell_adiabatic=shell_adiabatic,&
     907              : !            shell_particle_set=shell_particle_set, core_particle_set=core_particle_set)
     908              : 
     909              :          CALL apply_thermostat_shells(thermostat_shell, atomic_kind_set, particle_set, &
     910              :                                       local_particles, para_env, shell_particle_set=shell_particle_set, &
     911            0 :                                       core_particle_set=core_particle_set)
     912              :       ELSE
     913              :          CALL apply_thermostat_particles(thermostat_fast, force_env, molecule_kind_set, molecule_set, &
     914            0 :                                          particle_set, local_molecules, local_particles, para_env)
     915              : 
     916              :          CALL apply_thermostat_particles(thermostat_slow, force_env, molecule_kind_set, molecule_set, &
     917            0 :                                          particle_set, local_molecules, local_particles, para_env)
     918              :       END IF
     919              : 
     920            0 :       IF (simpar%constraint) CALL getold(gci, local_molecules, molecule_set, &
     921            0 :                                          molecule_kind_set, particle_set, cell)
     922              : 
     923              :       !    *** Velocity Verlet for Langeving *** v(t)--> v(t+1/2)
     924            0 :       IF (ASSOCIATED(force_env%meta_env)) THEN
     925            0 :          IF (force_env%meta_env%langevin) THEN
     926            0 :             DO ivar = 1, force_env%meta_env%n_colvar
     927            0 :                rand(ivar) = force_env%meta_env%rng(ivar)%next()
     928              :             END DO
     929            0 :             CALL metadyn_velocities_colvar(force_env, rand)
     930              :          END IF
     931              :       END IF
     932              : 
     933              :       ! Velocity Verlet (first part)
     934              :       CALL vv_first(tmp, atomic_kind_set, local_particles, particle_set, &
     935            0 :                     core_particle_set, shell_particle_set, nparticle_kind, shell_adiabatic, dt)
     936              : 
     937            0 :       IF (simpar%variable_dt) CALL variable_timestep(md_env, tmp, dt, simpar, para_env, atomic_kind_set, &
     938              :                                                      local_particles, particle_set, core_particle_set, shell_particle_set, &
     939            0 :                                                      nparticle_kind, shell_adiabatic)
     940              : 
     941            0 :       IF (simpar%constraint) THEN
     942              :          ! Possibly update the target values
     943              :          CALL shake_update_targets(gci, local_molecules, molecule_set, &
     944            0 :                                    molecule_kind_set, dt, force_env%root_section)
     945              : 
     946              :          CALL shake_control(gci, local_molecules, molecule_set, &
     947              :                             molecule_kind_set, particle_set, tmp%pos, tmp%vel, dt, simpar%shake_tol, &
     948              :                             simpar%info_constraint, simpar%lagrange_multipliers, simpar%dump_lm, &
     949            0 :                             cell, para_env, local_particles)
     950              :       END IF
     951              : 
     952              :       ! Broadcast the new particle positions and deallocate pos components of temporary
     953              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
     954            0 :                               core_particle_set, para_env, shell_adiabatic, pos=.TRUE.)
     955              : 
     956            0 :       IF (shell_adiabatic .AND. shell_check_distance) THEN
     957              :          CALL optimize_shell_core(force_env, particle_set, &
     958            0 :                                   shell_particle_set, core_particle_set, globenv, tmp=tmp, check=.TRUE.)
     959              :       END IF
     960              : 
     961              :       ! Update forces
     962            0 :       CALL force_env_calc_energy_force(force_env)
     963              : 
     964              :       ! Metadynamics
     965            0 :       CALL metadyn_integrator(force_env, itimes, tmp%vel, rand=rand)
     966              : 
     967              :       ! Velocity Verlet (second part)
     968              :       CALL vv_second(tmp, atomic_kind_set, local_particles, particle_set, &
     969            0 :                      core_particle_set, shell_particle_set, nparticle_kind, shell_adiabatic, dt)
     970              : 
     971            0 :       IF (simpar%constraint) CALL rattle_control(gci, local_molecules, molecule_set, &
     972              :                                                  molecule_kind_set, particle_set, tmp%vel, dt, simpar%shake_tol, &
     973              :                                                  simpar%info_constraint, simpar%lagrange_multipliers, simpar%dump_lm, &
     974            0 :                                                  cell, para_env, local_particles)
     975              : 
     976              :       ! Apply Thermostat over the full set of particles
     977            0 :       IF (shell_adiabatic) THEN
     978              :          !  CALL apply_thermostat_particles(thermostat_part,molecule_kind_set, molecule_set, &
     979              :          !       particle_set, local_molecules, para_env, shell_adiabatic=shell_adiabatic,&
     980              :          !       vel= tmp%vel, shell_vel= tmp%shell_vel, core_vel= tmp%core_vel)
     981              : 
     982              :          CALL apply_thermostat_shells(thermostat_shell, atomic_kind_set, particle_set, &
     983              :                                       local_particles, para_env, vel=tmp%vel, shell_vel=tmp%shell_vel, &
     984            0 :                                       core_vel=tmp%core_vel)
     985              :       ELSE
     986              :          CALL apply_thermostat_particles(thermostat_slow, force_env, molecule_kind_set, molecule_set, &
     987            0 :                                          particle_set, local_molecules, local_particles, para_env, vel=tmp%vel)
     988              : 
     989              :          CALL apply_thermostat_particles(thermostat_fast, force_env, molecule_kind_set, molecule_set, &
     990            0 :                                          particle_set, local_molecules, local_particles, para_env, vel=tmp%vel)
     991              :       END IF
     992              : 
     993              :       ! Broadcast the new particle velocities and deallocate temporary
     994              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
     995            0 :                               core_particle_set, para_env, shell_adiabatic, vel=.TRUE.)
     996              : 
     997            0 :       IF (ASSOCIATED(force_env%meta_env)) THEN
     998            0 :          IF (force_env%meta_env%langevin) THEN
     999            0 :             DEALLOCATE (rand)
    1000              :          END IF
    1001              :       END IF
    1002              : 
    1003              :       ! Update constraint virial
    1004            0 :       IF (simpar%constraint) CALL pv_constraint(gci, local_molecules, &
    1005            0 :                                                 molecule_set, molecule_kind_set, particle_set, virial, para_env)
    1006              : 
    1007              :       !     **  Evaluate Virial
    1008              :       CALL virial_evaluate(atomic_kind_set, particle_set, &
    1009            0 :                            local_particles, virial, para_env)
    1010              : 
    1011            0 :    END SUBROUTINE nvt_adiabatic
    1012              : 
    1013              : ! **************************************************************************************************
    1014              : !> \brief nvt integrator for particle positions & momenta
    1015              : !> \param md_env ...
    1016              : !> \param globenv ...
    1017              : !> \par History
    1018              : !>   - the local particle lists are used instead of pnode (Sep. 2003,MK)
    1019              : !>   - usage of fragments retrieved from the force environment (Oct. 2003,MK)
    1020              : !> \author CJM
    1021              : ! **************************************************************************************************
    1022        22212 :    SUBROUTINE nvt(md_env, globenv)
    1023              : 
    1024              :       TYPE(md_environment_type), POINTER                 :: md_env
    1025              :       TYPE(global_environment_type), POINTER             :: globenv
    1026              : 
    1027              :       INTEGER                                            :: ivar, nparticle, nparticle_kind, nshell
    1028              :       INTEGER, POINTER                                   :: itimes
    1029              :       LOGICAL                                            :: shell_adiabatic, shell_check_distance, &
    1030              :                                                             shell_present
    1031              :       REAL(KIND=dp)                                      :: dt, lambda_mix
    1032         7404 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: rand
    1033              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
    1034         7404 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1035              :       TYPE(cell_type), POINTER                           :: cell
    1036              :       TYPE(cp_subsys_type), POINTER                      :: subsys
    1037              :       TYPE(distribution_1d_type), POINTER                :: local_molecules, local_particles
    1038              :       TYPE(force_env_type), POINTER                      :: force_env
    1039              :       TYPE(global_constraint_type), POINTER              :: gci
    1040              :       TYPE(molecule_kind_list_type), POINTER             :: molecule_kinds
    1041         7404 :       TYPE(molecule_kind_type), DIMENSION(:), POINTER    :: molecule_kind_set
    1042              :       TYPE(molecule_list_type), POINTER                  :: molecules
    1043         7404 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
    1044              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1045              :       TYPE(particle_list_type), POINTER                  :: core_particles, particles, &
    1046              :                                                             shell_particles
    1047         7404 :       TYPE(particle_type), DIMENSION(:), POINTER         :: core_particle_set, particle_set, &
    1048         7404 :                                                             shell_particle_set
    1049              :       TYPE(qs_electrons_type), POINTER                   :: electrons
    1050              :       TYPE(simpar_type), POINTER                         :: simpar
    1051              :       TYPE(thermostat_type), POINTER                     :: thermostat_coeff, thermostat_part, &
    1052              :                                                             thermostat_shell
    1053              :       TYPE(tmp_variables_type), POINTER                  :: tmp
    1054              :       TYPE(virial_type), POINTER                         :: virial
    1055              : 
    1056         7404 :       NULLIFY (gci, force_env, thermostat_coeff, tmp, &
    1057         7404 :                thermostat_part, thermostat_shell, cell, shell_particles, &
    1058         7404 :                shell_particle_set, core_particles, core_particle_set, rand)
    1059         7404 :       NULLIFY (para_env, subsys, local_molecules, local_particles, molecule_kinds, &
    1060         7404 :                molecules, molecule_kind_set, molecule_set, atomic_kinds, particles)
    1061         7404 :       NULLIFY (simpar, thermostat_coeff, thermostat_part, thermostat_shell, itimes, electrons)
    1062              : 
    1063              :       CALL get_md_env(md_env=md_env, simpar=simpar, force_env=force_env, &
    1064              :                       thermostat_part=thermostat_part, thermostat_coeff=thermostat_coeff, &
    1065              :                       thermostat_shell=thermostat_shell, para_env=para_env, &
    1066         7404 :                       itimes=itimes, electrons=electrons)
    1067         7404 :       dt = simpar%dt
    1068              : 
    1069         7404 :       CALL force_env_get(force_env=force_env, subsys=subsys, cell=cell)
    1070              : 
    1071              :       ! Do some checks on coordinates and box
    1072         7404 :       CALL apply_qmmm_walls_reflective(force_env)
    1073              : 
    1074              :       CALL cp_subsys_get(subsys=subsys, atomic_kinds=atomic_kinds, local_particles=local_particles, &
    1075              :                          particles=particles, local_molecules=local_molecules, molecules=molecules, &
    1076         7404 :                          molecule_kinds=molecule_kinds, gci=gci, virial=virial)
    1077              : 
    1078         7404 :       nparticle_kind = atomic_kinds%n_els
    1079         7404 :       atomic_kind_set => atomic_kinds%els
    1080         7404 :       molecule_kind_set => molecule_kinds%els
    1081              : 
    1082         7404 :       nparticle = particles%n_els
    1083         7404 :       particle_set => particles%els
    1084         7404 :       molecule_set => molecules%els
    1085              : 
    1086              :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, &
    1087              :                                shell_present=shell_present, shell_adiabatic=shell_adiabatic, &
    1088         7404 :                                shell_check_distance=shell_check_distance)
    1089              : 
    1090         7404 :       IF (ASSOCIATED(force_env%meta_env)) THEN
    1091              :          ! Allocate random number for Langevin Thermostat acting on COLVARS
    1092         1014 :          IF (force_env%meta_env%langevin) THEN
    1093          720 :             ALLOCATE (rand(force_env%meta_env%n_colvar))
    1094          720 :             rand(:) = 0.0_dp
    1095              :          END IF
    1096              :       END IF
    1097              : 
    1098              :       !  Allocate work storage for positions and velocities
    1099         7404 :       IF (shell_present) THEN
    1100              :          CALL cp_subsys_get(subsys=subsys, shell_particles=shell_particles, &
    1101          920 :                             core_particles=core_particles)
    1102          920 :          shell_particle_set => shell_particles%els
    1103          920 :          nshell = SIZE(shell_particles%els)
    1104              : 
    1105          920 :          IF (shell_adiabatic) THEN
    1106          920 :             core_particle_set => core_particles%els
    1107              :          END IF
    1108              :       END IF
    1109              : 
    1110         7404 :       CALL allocate_tmp(md_env, tmp, nparticle, nshell, shell_adiabatic)
    1111              : 
    1112              :       ! Apply Thermostat over the full set of particles
    1113         7404 :       IF (shell_adiabatic) THEN
    1114              :          CALL apply_thermostat_particles(thermostat_part, force_env, molecule_kind_set, molecule_set, &
    1115              :                                         particle_set, local_molecules, local_particles, para_env, shell_adiabatic=shell_adiabatic, &
    1116          920 :                                          shell_particle_set=shell_particle_set, core_particle_set=core_particle_set)
    1117              : 
    1118              :          CALL apply_thermostat_shells(thermostat_shell, atomic_kind_set, particle_set, &
    1119              :                                       local_particles, para_env, shell_particle_set=shell_particle_set, &
    1120          920 :                                       core_particle_set=core_particle_set)
    1121              :       ELSE
    1122              :          CALL apply_thermostat_particles(thermostat_part, force_env, molecule_kind_set, molecule_set, &
    1123         6484 :                                          particle_set, local_molecules, local_particles, para_env)
    1124              :       END IF
    1125              : 
    1126              :       ! The potentiostat (O step) fires once per MD step, in the middle of the
    1127              :       ! electronic drift below, giving a B A O A B splitting for the electronic
    1128              :       ! degree of freedom.
    1129              : 
    1130         7404 :       IF (simpar%constraint) CALL getold(gci, local_molecules, molecule_set, &
    1131         2952 :                                          molecule_kind_set, particle_set, cell)
    1132              : 
    1133              :       !    *** Velocity Verlet for Langeving *** v(t)--> v(t+1/2)
    1134         7404 :       IF (ASSOCIATED(force_env%meta_env)) THEN
    1135         1014 :          IF (force_env%meta_env%langevin) THEN
    1136          720 :             DO ivar = 1, force_env%meta_env%n_colvar
    1137          720 :                rand(ivar) = force_env%meta_env%rng(ivar)%next()
    1138              :             END DO
    1139          240 :             CALL metadyn_velocities_colvar(force_env, rand)
    1140              :          END IF
    1141              :       END IF
    1142              : 
    1143              :       ! Velocity Verlet (first part)
    1144              :       CALL vv_first(tmp, atomic_kind_set, local_particles, particle_set, &
    1145         7404 :                     core_particle_set, shell_particle_set, nparticle_kind, shell_adiabatic, dt)
    1146         7404 :       IF (ASSOCIATED(electrons)) THEN
    1147              :          ! BAOAB on the electronic coordinate: half kick, half drift, ONE full
    1148              :          ! thermostat step, half drift.  The closing half kick is after the force
    1149              :          ! evaluation below.  qs_electrons_thermostat_step takes the interval
    1150              :          ! BETWEEN calls, which is now dt (one call per step), not 0.5*dt.
    1151              :          ! report_only ("&ELECTRONS 0"): skip the whole BAOAB block. q is not
    1152              :          ! propagated, the endpoints are never rebuilt, and in particular
    1153              :          ! apply_ne_core_correction is NOT called -- it would alter the system's
    1154              :          ! charge, which in report-only mode must stay exactly as the input set
    1155              :          ! it. Only the work-function readback below still runs.
    1156            2 :          IF (.NOT. electrons%report_only) THEN
    1157            2 :             CALL qs_electrons_kick(electrons, 0.5_dp*dt)
    1158            2 :             CALL qs_electrons_drift(electrons, 0.5_dp*dt)
    1159            2 :             CALL qs_electrons_thermostat_step(electrons, dt)
    1160            2 :             CALL qs_electrons_drift(electrons, 0.5_dp*dt)
    1161            2 :             IF (electrons%charge_mixing_enabled) THEN
    1162            2 :                CALL charge_mixing_maybe_rebuild(force_env, electrons)
    1163            2 :                CALL qs_electrons_charge_mixing_lambda(electrons, lambda_mix)
    1164            2 :                CALL push_lambda_to_mixed_env(force_env, lambda_mix)
    1165              :             ELSE
    1166            0 :                CALL apply_ne_core_correction(force_env, electrons%q, electrons%neutral_electrons)
    1167              :             END IF
    1168              :          END IF
    1169              :       END IF
    1170              : 
    1171         7404 :       IF (simpar%variable_dt) CALL variable_timestep(md_env, tmp, dt, simpar, para_env, atomic_kind_set, &
    1172              :                                                      local_particles, particle_set, core_particle_set, shell_particle_set, &
    1173            0 :                                                      nparticle_kind, shell_adiabatic)
    1174              : 
    1175         7404 :       IF (simpar%constraint) THEN
    1176              :          ! Possibly update the target values
    1177              :          CALL shake_update_targets(gci, local_molecules, molecule_set, &
    1178         2952 :                                    molecule_kind_set, dt, force_env%root_section)
    1179              : 
    1180              :          CALL shake_control(gci, local_molecules, molecule_set, &
    1181              :                             molecule_kind_set, particle_set, tmp%pos, tmp%vel, dt, simpar%shake_tol, &
    1182              :                             simpar%info_constraint, simpar%lagrange_multipliers, simpar%dump_lm, &
    1183         2952 :                             cell, para_env, local_particles)
    1184              :       END IF
    1185              : 
    1186              :       ! Broadcast the new particle positions and deallocate pos components of temporary
    1187              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
    1188         7404 :                               core_particle_set, para_env, shell_adiabatic, pos=.TRUE.)
    1189              : 
    1190         7404 :       IF (shell_adiabatic .AND. shell_check_distance) THEN
    1191              :          CALL optimize_shell_core(force_env, particle_set, &
    1192          280 :                                   shell_particle_set, core_particle_set, globenv, tmp=tmp, check=.TRUE.)
    1193              :       END IF
    1194              : 
    1195              :       ![ADAPT] update input structure with new coordinates, make new labels
    1196         7404 :       CALL qmmmx_update_force_env(force_env, force_env%root_section)
    1197              : 
    1198              :       ![NB] recreate pointers changed by creation of new subsys in qmmm_update_force_mixing_env
    1199              :       ![NB] ugly hack, which is why adaptivity isn't implemented in most other ensembles
    1200              :       !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    1201         7404 :       CALL force_env_get(force_env=force_env, subsys=subsys, cell=cell)
    1202              : 
    1203              :       CALL cp_subsys_get(subsys=subsys, atomic_kinds=atomic_kinds, local_particles=local_particles, &
    1204              :                          particles=particles, local_molecules=local_molecules, molecules=molecules, &
    1205         7404 :                          molecule_kinds=molecule_kinds, gci=gci, virial=virial)
    1206              : 
    1207         7404 :       nparticle_kind = atomic_kinds%n_els
    1208         7404 :       atomic_kind_set => atomic_kinds%els
    1209         7404 :       molecule_kind_set => molecule_kinds%els
    1210              : 
    1211         7404 :       nparticle = particles%n_els
    1212         7404 :       particle_set => particles%els
    1213         7404 :       molecule_set => molecules%els
    1214              : 
    1215              :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, &
    1216              :                                shell_present=shell_present, shell_adiabatic=shell_adiabatic, &
    1217         7404 :                                shell_check_distance=shell_check_distance)
    1218              : 
    1219              :       !  Allocate work storage for positions and velocities
    1220         7404 :       IF (shell_present) THEN
    1221              :          CALL cp_subsys_get(subsys=subsys, shell_particles=shell_particles, &
    1222          920 :                             core_particles=core_particles)
    1223          920 :          shell_particle_set => shell_particles%els
    1224              :          nshell = SIZE(shell_particles%els)
    1225              : 
    1226          920 :          IF (shell_adiabatic) THEN
    1227          920 :             core_particle_set => core_particles%els
    1228              :          END IF
    1229              :       END IF
    1230              :       !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    1231              : 
    1232              :       ! Update forces
    1233              :       ![NB] let nvt work with force mixing which does not have consistent energies and forces
    1234         7404 :       CALL force_env_calc_energy_force(force_env, require_consistent_energy_force=.FALSE.)
    1235              : 
    1236         7404 :       IF (ASSOCIATED(electrons)) THEN
    1237            2 :          IF (electrons%charge_mixing_enabled) THEN
    1238              :             CALL compute_mixed_workfunction(force_env, electrons%potential_average_axis, &
    1239              :                                             electrons%potential_average_min, electrons%potential_average_max, &
    1240              :                                             electrons%q, electrons%charge_mixing_span, &
    1241              :                                             electrons%current_fermi_level_eV, electrons%current_workfunction, &
    1242            2 :                                             lambda_in=lambda_mix)
    1243              :          ELSE
    1244              :             CALL qs_electrons_compute_workfunction(force_env, electrons%potential_average_axis, &
    1245              :                                                    electrons%potential_average_min, electrons%potential_average_max, &
    1246            0 :                                                    electrons%current_fermi_level_eV, electrons%current_workfunction)
    1247              :          END IF
    1248              :       END IF
    1249              : 
    1250              :       ! Metadynamics
    1251         7404 :       CALL metadyn_integrator(force_env, itimes, tmp%vel, rand=rand)
    1252              : 
    1253              :       ! Velocity Verlet (second part)
    1254              :       CALL vv_second(tmp, atomic_kind_set, local_particles, particle_set, &
    1255         7404 :                      core_particle_set, shell_particle_set, nparticle_kind, shell_adiabatic, dt)
    1256              : 
    1257         7404 :       IF (ASSOCIATED(electrons)) THEN
    1258              :          ! report_only ("&ELECTRONS 0"): q is never propagated, so no B step.
    1259            2 :          IF (.NOT. electrons%report_only) CALL qs_electrons_kick(electrons, 0.5_dp*dt)
    1260              :       END IF
    1261              : 
    1262         7404 :       IF (simpar%constraint) CALL rattle_control(gci, local_molecules, molecule_set, &
    1263              :                                                  molecule_kind_set, particle_set, tmp%vel, dt, simpar%shake_tol, &
    1264              :                                                  simpar%info_constraint, simpar%lagrange_multipliers, simpar%dump_lm, &
    1265         2952 :                                                  cell, para_env, local_particles)
    1266              : 
    1267              :       ! Apply Thermostat over the full set of particles
    1268         7404 :       IF (shell_adiabatic) THEN
    1269              :          CALL apply_thermostat_particles(thermostat_part, force_env, molecule_kind_set, molecule_set, &
    1270              :                                         particle_set, local_molecules, local_particles, para_env, shell_adiabatic=shell_adiabatic, &
    1271          920 :                                          vel=tmp%vel, shell_vel=tmp%shell_vel, core_vel=tmp%core_vel)
    1272              : 
    1273              :          CALL apply_thermostat_shells(thermostat_shell, atomic_kind_set, particle_set, &
    1274              :                                       local_particles, para_env, vel=tmp%vel, shell_vel=tmp%shell_vel, &
    1275          920 :                                       core_vel=tmp%core_vel)
    1276              :       ELSE
    1277              :          CALL apply_thermostat_particles(thermostat_part, force_env, molecule_kind_set, molecule_set, &
    1278         6484 :                                          particle_set, local_molecules, local_particles, para_env, vel=tmp%vel)
    1279              :       END IF
    1280              : 
    1281              :       ! (no trailing electronic O: BAOAB places the single potentiostat step in
    1282              :       !  the middle of the drift, not at the step boundaries.)
    1283              : 
    1284              :       ! Broadcast the new particle velocities and deallocate temporary
    1285              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
    1286         7404 :                               core_particle_set, para_env, shell_adiabatic, vel=.TRUE.)
    1287              : 
    1288         7404 :       IF (ASSOCIATED(force_env%meta_env)) THEN
    1289         1014 :          IF (force_env%meta_env%langevin) THEN
    1290          240 :             DEALLOCATE (rand)
    1291              :          END IF
    1292              :       END IF
    1293              : 
    1294              :       ! Update constraint virial
    1295         7404 :       IF (simpar%constraint) CALL pv_constraint(gci, local_molecules, &
    1296         2952 :                                                 molecule_set, molecule_kind_set, particle_set, virial, para_env)
    1297              : 
    1298              :       !     **  Evaluate Virial
    1299              :       CALL virial_evaluate(atomic_kind_set, particle_set, &
    1300         7404 :                            local_particles, virial, para_env)
    1301              : 
    1302         7404 :    END SUBROUTINE nvt
    1303              : 
    1304              : ! **************************************************************************************************
    1305              : !> \brief npt_i integrator for particle positions & momenta
    1306              : !>      isotropic box changes
    1307              : !> \param md_env ...
    1308              : !> \param globenv ...
    1309              : !> \par History
    1310              : !>      none
    1311              : !> \author CJM
    1312              : ! **************************************************************************************************
    1313         3128 :    SUBROUTINE npt_i(md_env, globenv)
    1314              : 
    1315              :       TYPE(md_environment_type), POINTER                 :: md_env
    1316              :       TYPE(global_environment_type), POINTER             :: globenv
    1317              : 
    1318              :       REAL(KIND=dp), PARAMETER                           :: e2 = 1.0_dp/6.0_dp, e4 = e2/20.0_dp, &
    1319              :                                                             e6 = e4/42.0_dp, e8 = e6/72.0_dp
    1320              : 
    1321              :       INTEGER                                            :: iroll, ivar, nkind, nparticle, &
    1322              :                                                             nparticle_kind, nshell
    1323              :       INTEGER, POINTER                                   :: itimes
    1324              :       LOGICAL                                            :: first, first_time, shell_adiabatic, &
    1325              :                                                             shell_check_distance, shell_present
    1326              :       REAL(KIND=dp)                                      :: dt, infree, kin, roll_tol, roll_tol_thrs
    1327              :       REAL(KIND=dp), DIMENSION(3)                        :: vector_r, vector_v
    1328              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: pv_kin
    1329         1564 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: rand
    1330              :       REAL(KIND=dp), SAVE                                :: eps_0
    1331              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
    1332         1564 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1333              :       TYPE(cell_type), POINTER                           :: cell
    1334              :       TYPE(cp_subsys_type), POINTER                      :: subsys
    1335              :       TYPE(distribution_1d_type), POINTER                :: local_molecules, local_particles
    1336              :       TYPE(force_env_type), POINTER                      :: force_env
    1337              :       TYPE(global_constraint_type), POINTER              :: gci
    1338              :       TYPE(local_fixd_constraint_type), DIMENSION(:), &
    1339         1564 :          POINTER                                         :: lfixd_list
    1340              :       TYPE(molecule_kind_list_type), POINTER             :: molecule_kinds
    1341         1564 :       TYPE(molecule_kind_type), DIMENSION(:), POINTER    :: molecule_kind_set
    1342              :       TYPE(molecule_list_type), POINTER                  :: molecules
    1343         1564 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
    1344              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1345         1564 :       TYPE(npt_info_type), POINTER                       :: npt(:, :)
    1346              :       TYPE(old_variables_type), POINTER                  :: old
    1347              :       TYPE(particle_list_type), POINTER                  :: core_particles, particles, &
    1348              :                                                             shell_particles
    1349         1564 :       TYPE(particle_type), DIMENSION(:), POINTER         :: core_particle_set, particle_set, &
    1350         1564 :                                                             shell_particle_set
    1351              :       TYPE(simpar_type), POINTER                         :: simpar
    1352              :       TYPE(thermostat_type), POINTER                     :: thermostat_baro, thermostat_part, &
    1353              :                                                             thermostat_shell
    1354              :       TYPE(tmp_variables_type), POINTER                  :: tmp
    1355              :       TYPE(virial_type), POINTER                         :: virial
    1356              : 
    1357         1564 :       NULLIFY (gci, thermostat_baro, thermostat_part, thermostat_shell, force_env)
    1358         1564 :       NULLIFY (atomic_kinds, cell, para_env, subsys, local_molecules, local_particles)
    1359         1564 :       NULLIFY (molecule_kinds, molecules, molecule_kind_set, npt)
    1360         1564 :       NULLIFY (core_particles, particles, shell_particles, tmp, old)
    1361         1564 :       NULLIFY (core_particle_set, particle_set, shell_particle_set)
    1362         1564 :       NULLIFY (simpar, virial, rand, itimes, lfixd_list)
    1363              : 
    1364              :       CALL get_md_env(md_env=md_env, simpar=simpar, force_env=force_env, &
    1365              :                       thermostat_part=thermostat_part, thermostat_baro=thermostat_baro, &
    1366              :                       thermostat_shell=thermostat_shell, npt=npt, first_time=first_time, &
    1367         1564 :                       para_env=para_env, itimes=itimes)
    1368         1564 :       dt = simpar%dt
    1369         1564 :       infree = 1.0_dp/REAL(simpar%nfree, KIND=dp)
    1370              : 
    1371         1564 :       CALL force_env_get(force_env=force_env, subsys=subsys, cell=cell)
    1372              : 
    1373              :       ! Do some checks on coordinates and box
    1374         1564 :       CALL apply_qmmm_walls_reflective(force_env)
    1375              : 
    1376              :       CALL cp_subsys_get(subsys=subsys, atomic_kinds=atomic_kinds, local_particles=local_particles, &
    1377              :                          particles=particles, local_molecules=local_molecules, molecules=molecules, &
    1378         1564 :                          gci=gci, molecule_kinds=molecule_kinds, virial=virial)
    1379              : 
    1380         1564 :       nparticle_kind = atomic_kinds%n_els
    1381         1564 :       nkind = molecule_kinds%n_els
    1382         1564 :       atomic_kind_set => atomic_kinds%els
    1383         1564 :       molecule_kind_set => molecule_kinds%els
    1384              : 
    1385         1564 :       nparticle = particles%n_els
    1386         1564 :       particle_set => particles%els
    1387         1564 :       molecule_set => molecules%els
    1388              : 
    1389              :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, &
    1390              :                                shell_present=shell_present, shell_adiabatic=shell_adiabatic, &
    1391         1564 :                                shell_check_distance=shell_check_distance)
    1392              : 
    1393         1564 :       IF (first_time) THEN
    1394              :          CALL virial_evaluate(atomic_kind_set, particle_set, &
    1395          108 :                               local_particles, virial, para_env)
    1396              :       END IF
    1397              : 
    1398              :       ! Allocate work storage for positions and velocities
    1399         1564 :       CALL allocate_old(old, particle_set, npt)
    1400              : 
    1401         1564 :       IF (ASSOCIATED(force_env%meta_env)) THEN
    1402              :          ! Allocate random number for Langevin Thermostat acting on COLVARS
    1403            0 :          IF (force_env%meta_env%langevin) THEN
    1404            0 :             ALLOCATE (rand(force_env%meta_env%n_colvar))
    1405            0 :             rand(:) = 0.0_dp
    1406              :          END IF
    1407              :       END IF
    1408              : 
    1409         1564 :       IF (shell_present) THEN
    1410              :          CALL cp_subsys_get(subsys=subsys, &
    1411          120 :                             shell_particles=shell_particles, core_particles=core_particles)
    1412          120 :          shell_particle_set => shell_particles%els
    1413          120 :          nshell = SIZE(shell_particles%els)
    1414          120 :          IF (shell_adiabatic) THEN
    1415          120 :             core_particle_set => core_particles%els
    1416              :          END IF
    1417              :       END IF
    1418              : 
    1419         1564 :       CALL allocate_tmp(md_env, tmp, nparticle, nshell, shell_adiabatic)
    1420              : 
    1421              :       ! Initialize eps_0 the first time through
    1422         1564 :       IF (first_time) eps_0 = npt(1, 1)%eps
    1423              : 
    1424              :       ! Apply thermostat to  barostat
    1425         1564 :       CALL apply_thermostat_baro(thermostat_baro, npt, para_env)
    1426              : 
    1427              :       ! Apply Thermostat over the full set of particles
    1428         1564 :       IF (simpar%ensemble /= npe_i_ensemble) THEN
    1429         1524 :          IF (shell_adiabatic) THEN
    1430              :             CALL apply_thermostat_particles(thermostat_part, force_env, molecule_kind_set, molecule_set, &
    1431              :                                         particle_set, local_molecules, local_particles, para_env, shell_adiabatic=shell_adiabatic, &
    1432           80 :                                             shell_particle_set=shell_particle_set, core_particle_set=core_particle_set)
    1433              : 
    1434              :          ELSE
    1435              :             CALL apply_thermostat_particles(thermostat_part, force_env, molecule_kind_set, molecule_set, &
    1436         1444 :                                             particle_set, local_molecules, local_particles, para_env)
    1437              :          END IF
    1438              :       END IF
    1439              : 
    1440              :       ! Apply Thermostat over the core-shell motion
    1441              :       CALL apply_thermostat_shells(thermostat_shell, atomic_kind_set, particle_set, &
    1442              :                                    local_particles, para_env, shell_particle_set=shell_particle_set, &
    1443         1564 :                                    core_particle_set=core_particle_set)
    1444              : 
    1445         1564 :       IF (simpar%constraint) THEN
    1446              :          ! Possibly update the target values
    1447              :          CALL shake_update_targets(gci, local_molecules, molecule_set, &
    1448          668 :                                    molecule_kind_set, dt, force_env%root_section)
    1449              :       END IF
    1450              : 
    1451              :       ! setting up for ROLL: saving old variables
    1452         1564 :       IF (simpar%constraint) THEN
    1453          668 :          roll_tol_thrs = simpar%roll_tol
    1454          668 :          iroll = 1
    1455          668 :          CALL set(old, atomic_kind_set, particle_set, local_particles, cell, npt, 'F')
    1456              :          CALL getold(gci, local_molecules, molecule_set, &
    1457          668 :                      molecule_kind_set, particle_set, cell)
    1458              :       ELSE
    1459              :          roll_tol_thrs = EPSILON(0.0_dp)
    1460              :       END IF
    1461         1564 :       roll_tol = -roll_tol_thrs
    1462              : 
    1463              :       !    *** Velocity Verlet for Langeving *** v(t)--> v(t+1/2)
    1464         1564 :       IF (ASSOCIATED(force_env%meta_env)) THEN
    1465            0 :          IF (force_env%meta_env%langevin) THEN
    1466            0 :             DO ivar = 1, force_env%meta_env%n_colvar
    1467            0 :                rand(ivar) = force_env%meta_env%rng(ivar)%next()
    1468              :             END DO
    1469            0 :             CALL metadyn_velocities_colvar(force_env, rand)
    1470              :          END IF
    1471              :       END IF
    1472              : 
    1473         4266 :       SR: DO WHILE (ABS(roll_tol) >= roll_tol_thrs) ! SHAKE-ROLL LOOP
    1474              : 
    1475         2702 :          IF (simpar%constraint) THEN
    1476         1806 :             CALL set(old, atomic_kind_set, particle_set, local_particles, cell, npt, 'B')
    1477              :          END IF
    1478              : 
    1479              :          CALL update_pv(gci, simpar, atomic_kind_set, particle_set, &
    1480              :                         local_molecules, molecule_set, molecule_kind_set, &
    1481         2702 :                         local_particles, kin, pv_kin, virial, para_env)
    1482         2702 :          CALL update_veps(cell, npt, simpar, pv_kin, kin, virial, infree)
    1483              : 
    1484              :          tmp%arg_r(1) = (0.5_dp*npt(1, 1)%v*dt)* &
    1485         2702 :                         (0.5_dp*npt(1, 1)%v*dt)
    1486              :          tmp%poly_r(1:3) = 1.0_dp + e2*tmp%arg_r(1) + e4*tmp%arg_r(1)*tmp%arg_r(1) + &
    1487        10808 :                            e6*tmp%arg_r(1)**3 + e8*tmp%arg_r(1)**4
    1488              : 
    1489              :          tmp%arg_v(1) = (0.25_dp*npt(1, 1)%v*dt* &
    1490              :                          (1.0_dp + 3.0_dp*infree))*(0.25_dp*npt(1, 1)%v* &
    1491         2702 :                                                     dt*(1.0_dp + 3.0_dp*infree))
    1492              :          tmp%poly_v(1:3) = 1.0_dp + e2*tmp%arg_v(1) + e4*tmp%arg_v(1)*tmp%arg_v(1) + &
    1493        10808 :                            e6*tmp%arg_v(1)**3 + e8*tmp%arg_v(1)**4
    1494              : 
    1495        10808 :          tmp%scale_r(1:3) = EXP(0.5_dp*dt*npt(1, 1)%v)
    1496              :          tmp%scale_v(1:3) = EXP(-0.25_dp*dt*npt(1, 1)%v* &
    1497        10808 :                                 (1.0_dp + 3.0_dp*infree))
    1498              : 
    1499              :          ! first half of velocity verlet
    1500         2702 :          IF (simpar%ensemble == npt_ia_ensemble) THEN
    1501           20 :             CALL create_local_fixd_list(lfixd_list, nkind, molecule_kind_set, local_particles)
    1502              :             CALL vv_first(tmp, atomic_kind_set, local_particles, particle_set, &
    1503              :                           core_particle_set, shell_particle_set, nparticle_kind, &
    1504           20 :                           shell_adiabatic, dt, lfixd_list=lfixd_list)
    1505           20 :             CALL release_local_fixd_list(lfixd_list)
    1506              :          ELSE
    1507              :             CALL vv_first(tmp, atomic_kind_set, local_particles, particle_set, &
    1508              :                           core_particle_set, shell_particle_set, nparticle_kind, &
    1509         2682 :                           shell_adiabatic, dt)
    1510              :          END IF
    1511              : 
    1512         2702 :          IF (simpar%variable_dt) CALL variable_timestep(md_env, tmp, dt, simpar, para_env, &
    1513              :                                                         atomic_kind_set, local_particles, particle_set, core_particle_set, &
    1514            0 :                                                         shell_particle_set, nparticle_kind, shell_adiabatic, npt=npt)
    1515              : 
    1516         2702 :          roll_tol = 0.0_dp
    1517        10808 :          vector_r(:) = tmp%scale_r(:)*tmp%poly_r(:)
    1518        10808 :          vector_v(:) = tmp%scale_v(:)*tmp%poly_v(:)
    1519              : 
    1520         2702 :          IF (simpar%constraint) CALL shake_roll_control(gci, local_molecules, &
    1521              :                                                       molecule_set, molecule_kind_set, particle_set, tmp%pos, tmp%vel, dt, simpar, &
    1522              :                                                         roll_tol, iroll, vector_r, vector_v, para_env, cell=cell, &
    1523         3370 :                                                         local_particles=local_particles)
    1524              :       END DO SR
    1525              : 
    1526              :       ! Update eps:
    1527         4692 :       npt(:, :)%eps = npt(:, :)%eps + dt*npt(:, :)%v
    1528              : 
    1529              :       ! Update h_mat
    1530        20332 :       cell%hmat(:, :) = cell%hmat(:, :)*EXP(npt(1, 1)%eps - eps_0)
    1531              : 
    1532         1564 :       eps_0 = npt(1, 1)%eps
    1533              : 
    1534              :       ! Update the inverse
    1535         1564 :       CALL init_cell(cell)
    1536              : 
    1537              :       ! Broadcast the new particle positions and deallocate the pos components of temporary
    1538              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
    1539         1564 :                               core_particle_set, para_env, shell_adiabatic, pos=.TRUE.)
    1540              : 
    1541         1564 :       IF (shell_adiabatic .AND. shell_check_distance) THEN
    1542              :          CALL optimize_shell_core(force_env, particle_set, &
    1543            0 :                                   shell_particle_set, core_particle_set, globenv, tmp=tmp, check=.TRUE.)
    1544              :       END IF
    1545              : 
    1546              :       ! Update forces
    1547         1564 :       CALL force_env_calc_energy_force(force_env)
    1548              : 
    1549              :       ! Metadynamics
    1550         1564 :       CALL metadyn_integrator(force_env, itimes, tmp%vel, rand=rand)
    1551              : 
    1552              :       ! Velocity Verlet (second part)
    1553              :       CALL vv_second(tmp, atomic_kind_set, local_particles, particle_set, &
    1554              :                      core_particle_set, shell_particle_set, nparticle_kind, &
    1555         1564 :                      shell_adiabatic, dt)
    1556              : 
    1557         1564 :       IF (simpar%constraint) THEN
    1558          668 :          roll_tol_thrs = simpar%roll_tol
    1559          668 :          first = .TRUE.
    1560          668 :          iroll = 1
    1561          668 :          CALL set(old, atomic_kind_set, particle_set, tmp%vel, local_particles, cell, npt, 'F')
    1562              :       ELSE
    1563              :          roll_tol_thrs = EPSILON(0.0_dp)
    1564              :       END IF
    1565         1564 :       roll_tol = -roll_tol_thrs
    1566              : 
    1567         4234 :       RR: DO WHILE (ABS(roll_tol) >= roll_tol_thrs) ! RATTLE-ROLL LOOP
    1568         2670 :          roll_tol = 0.0_dp
    1569         2670 :          IF (simpar%constraint) CALL rattle_roll_setup(old, gci, atomic_kind_set, &
    1570              :                                                        particle_set, local_particles, molecule_kind_set, molecule_set, &
    1571              :                                                        local_molecules, tmp%vel, dt, cell, npt, simpar, virial, vector_v, &
    1572         1774 :                                                        roll_tol, iroll, infree, first, para_env)
    1573              : 
    1574              :          CALL update_pv(gci, simpar, atomic_kind_set, tmp%vel, particle_set, &
    1575              :                         local_molecules, molecule_set, molecule_kind_set, &
    1576         2670 :                         local_particles, kin, pv_kin, virial, para_env)
    1577         4234 :          CALL update_veps(cell, npt, simpar, pv_kin, kin, virial, infree)
    1578              :       END DO RR
    1579              : 
    1580              :       ! Apply Thermostat over the full set of particles
    1581         1564 :       IF (simpar%ensemble /= npe_i_ensemble) THEN
    1582         1524 :          IF (shell_adiabatic) THEN
    1583              :             CALL apply_thermostat_particles(thermostat_part, force_env, molecule_kind_set, molecule_set, &
    1584              :                                         particle_set, local_molecules, local_particles, para_env, shell_adiabatic=shell_adiabatic, &
    1585           80 :                                             vel=tmp%vel, shell_vel=tmp%shell_vel, core_vel=tmp%core_vel)
    1586              :          ELSE
    1587              :             CALL apply_thermostat_particles(thermostat_part, force_env, molecule_kind_set, molecule_set, &
    1588         1444 :                                             particle_set, local_molecules, local_particles, para_env, vel=tmp%vel)
    1589              :          END IF
    1590              :       END IF
    1591              : 
    1592              :       ! Apply Thermostat over the core-shell motion
    1593         1564 :       IF (ASSOCIATED(thermostat_shell)) THEN
    1594              :          CALL apply_thermostat_shells(thermostat_shell, atomic_kind_set, particle_set, &
    1595              :                                       local_particles, para_env, vel=tmp%vel, shell_vel=tmp%shell_vel, &
    1596           40 :                                       core_vel=tmp%core_vel)
    1597              :       END IF
    1598              : 
    1599              :       ! Apply Thermostat to Barostat
    1600         1564 :       CALL apply_thermostat_baro(thermostat_baro, npt, para_env)
    1601              : 
    1602              :       ! Annealing of particle velocities is only possible when no thermostat is active
    1603         1564 :       IF (simpar%ensemble == npe_i_ensemble .AND. simpar%annealing) THEN
    1604            0 :          tmp%vel(:, :) = tmp%vel(:, :)*simpar%f_annealing
    1605            0 :          IF (shell_adiabatic) THEN
    1606              :             CALL shell_scale_comv(atomic_kind_set, local_particles, particle_set, &
    1607            0 :                                   tmp%vel, tmp%shell_vel, tmp%core_vel)
    1608              :          END IF
    1609              :       END IF
    1610              :       ! Annealing of CELL velocities is only possible when no thermostat is active
    1611         1564 :       IF (simpar%ensemble == npe_i_ensemble .AND. simpar%annealing_cell) THEN
    1612            0 :          npt(1, 1)%v = npt(1, 1)%v*simpar%f_annealing_cell
    1613              :       END IF
    1614              : 
    1615              :       ! Broadcast the new particle velocities and deallocate temporary
    1616              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
    1617         1564 :                               core_particle_set, para_env, shell_adiabatic, vel=.TRUE.)
    1618              : 
    1619              :       ! Update constraint virial
    1620         1564 :       IF (simpar%constraint) CALL pv_constraint(gci, local_molecules, &
    1621          668 :                                                 molecule_set, molecule_kind_set, particle_set, virial, para_env)
    1622              : 
    1623              :       CALL virial_evaluate(atomic_kind_set, particle_set, &
    1624         1564 :                            local_particles, virial, para_env)
    1625              : 
    1626              :       ! Deallocate old variables
    1627         1564 :       CALL deallocate_old(old)
    1628              : 
    1629         1564 :       IF (ASSOCIATED(force_env%meta_env)) THEN
    1630            0 :          IF (force_env%meta_env%langevin) THEN
    1631            0 :             DEALLOCATE (rand)
    1632              :          END IF
    1633              :       END IF
    1634              : 
    1635         1564 :       IF (first_time) THEN
    1636          108 :          first_time = .FALSE.
    1637          108 :          CALL set_md_env(md_env, first_time=first_time)
    1638              :       END IF
    1639              : 
    1640         1564 :    END SUBROUTINE npt_i
    1641              : 
    1642              : ! **************************************************************************************************
    1643              : !> \brief uses coordinates in a file and generates frame after frame of these
    1644              : !> \param md_env ...
    1645              : !> \par History
    1646              : !>   - 04.2005 created [Joost VandeVondele]
    1647              : !>   - modified to make it more general [MI]
    1648              : !> \note
    1649              : !>     it can be used to compute some properties on already available trajectories
    1650              : ! **************************************************************************************************
    1651          568 :    SUBROUTINE reftraj(md_env)
    1652              :       TYPE(md_environment_type), POINTER                 :: md_env
    1653              : 
    1654              :       CHARACTER(LEN=2)                                   :: element_kind_ref0, element_symbol, &
    1655              :                                                             element_symbol_ref0
    1656              :       CHARACTER(LEN=max_line_length)                     :: errmsg
    1657              :       INTEGER                                            :: cell_itimes, i, nparticle, Nread, &
    1658              :                                                             trj_itimes
    1659              :       INTEGER, POINTER                                   :: itimes
    1660              :       LOGICAL                                            :: init, my_end, traj_has_cell_info
    1661              :       REAL(KIND=dp)                                      :: cell_time, h(3, 3), trj_epot, trj_time, &
    1662              :                                                             vol
    1663              :       REAL(KIND=dp), POINTER                             :: time
    1664              :       TYPE(cell_type), POINTER                           :: cell
    1665              :       TYPE(cp_logger_type), POINTER                      :: logger
    1666              :       TYPE(cp_subsys_type), POINTER                      :: subsys
    1667              :       TYPE(force_env_type), POINTER                      :: force_env
    1668              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1669              :       TYPE(particle_list_type), POINTER                  :: particles
    1670          284 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1671              :       TYPE(reftraj_type), POINTER                        :: reftraj_env
    1672              :       TYPE(simpar_type), POINTER                         :: simpar
    1673              : 
    1674          284 :       NULLIFY (reftraj_env, particle_set, particles, force_env, subsys, simpar, para_env, cell, logger, itimes, time)
    1675              :       CALL get_md_env(md_env=md_env, init=init, reftraj=reftraj_env, force_env=force_env, &
    1676          284 :                       para_env=para_env, simpar=simpar)
    1677          284 :       logger => cp_get_default_logger()
    1678              : 
    1679          284 :       CALL force_env_get(force_env=force_env, cell=cell, subsys=subsys)
    1680          284 :       reftraj_env%isnap = reftraj_env%isnap + reftraj_env%info%stride
    1681              : 
    1682              :       ! Do some checks on coordinates and box
    1683          284 :       CALL apply_qmmm_walls_reflective(force_env)
    1684          284 :       CALL cp_subsys_get(subsys=subsys, particles=particles)
    1685          284 :       nparticle = particles%n_els
    1686          284 :       particle_set => particles%els
    1687              : 
    1688              :       ! SnapShots read from an external file (parsers calls are buffered! please
    1689              :       ! don't put any additional MPI call!) [tlaino]
    1690          284 :       CALL parser_read_line(reftraj_env%info%traj_parser, 1)
    1691          284 :       READ (reftraj_env%info%traj_parser%input_line, FMT="(I8)") nread
    1692          284 :       CALL parser_read_line(reftraj_env%info%traj_parser, 1)
    1693              :       ! Use the same parser for FORCE_EVAL/SUBSYS/CELL which allows for extxyz
    1694              :       ! Escape values for undetected cases is HUGE(0)
    1695              :       CALL read_xyz_comment(reftraj_env%info%traj_parser%input_line, cell, &
    1696          284 :                             traj_has_cell_info, trj_itimes, trj_time, trj_epot)
    1697          284 :       IF (trj_itimes == HUGE(0)) THEN
    1698           50 :          CALL get_md_env(md_env, itimes=itimes)
    1699           50 :          trj_itimes = itimes
    1700              :       END IF
    1701          284 :       IF (trj_time == HUGE(0.0_dp)) trj_time = 0.0_dp
    1702          284 :       IF (trj_epot == HUGE(0.0_dp)) trj_epot = 0.0_dp
    1703              : 
    1704              :       ! The following parser for XYZ comment line with strict field widths from
    1705              :       ! the dumpdcd format is preserved for historical reference only
    1706              :       ! --------------------
    1707              :       !       LOGICAL                                            :: test_ok
    1708              :       !       REAL(KIND=dp), DIMENSION(3)                        :: abc, albega
    1709              :       ! abc(:) = 0.0_dp
    1710              :       ! albega(:) = 0.0_dp
    1711              :       ! test_ok = .FALSE.
    1712              :       ! IF (INDEX(reftraj_env%info%traj_parser%input_line, ", a = ") > 60) THEN
    1713              :       !    traj_has_cell_info = .TRUE.
    1714              :       !    READ (reftraj_env%info%traj_parser%input_line, &
    1715              :       !          FMT="(T6,I8,T23,F12.3,T41,F20.10,T67,F14.6,T87,F14.6,T107,F14.6,T131,F8.3,T149,F8.3,T167,F8.3)", &
    1716              :       !          ERR=999) trj_itimes, trj_time, trj_epot, abc(1:3), albega(1:3)
    1717              :       !    ! Convert cell parameters from angstrom and degree to the internal CP2K units
    1718              :       !    DO i = 1, 3
    1719              :       !       abc(i) = cp_unit_to_cp2k(abc(i), "angstrom")
    1720              :       !       albega(i) = cp_unit_to_cp2k(albega(i), "deg")
    1721              :       !    END DO
    1722              :       ! ELSE
    1723              :       !    traj_has_cell_info = .FALSE.
    1724              :       !    READ (reftraj_env%info%traj_parser%input_line, FMT="(T6,I8,T23,F12.3,T41,F20.10)", ERR=999) &
    1725              :       !       trj_itimes, trj_time, trj_epot
    1726              :       ! END IF
    1727              :       ! test_ok = .TRUE.
    1728              :       ! 999 IF (.NOT. test_ok) THEN
    1729              :       !    ! Handling properly the error when reading the title of an XYZ
    1730              :       !    CALL get_md_env(md_env, itimes=itimes)
    1731              :       !    trj_itimes = itimes
    1732              :       !    trj_time = 0.0_dp
    1733              :       !    trj_epot = 0.0_dp
    1734              :       ! END IF
    1735              :       ! --------------------
    1736              : 
    1737              :       ! Delayed print of error message until the step number is known
    1738          284 :       IF (nread /= nparticle) THEN
    1739              :          errmsg = "Number of atoms for step "//TRIM(ADJUSTL(cp_to_string(trj_itimes)))// &
    1740              :                   " in the trajectory file does not match the reference configuration: "// &
    1741            0 :                   TRIM(ADJUSTL(cp_to_string(nread)))//" != "//TRIM(ADJUSTL(cp_to_string(nparticle)))
    1742            0 :          CPABORT(errmsg)
    1743              :       END IF
    1744         9730 :       DO i = 1, nread - 1
    1745         9446 :          CALL parser_read_line(reftraj_env%info%traj_parser, 1)
    1746              :          READ (UNIT=reftraj_env%info%traj_parser%input_line(1:LEN_TRIM(reftraj_env%info%traj_parser%input_line)), FMT=*) &
    1747        37784 :             element_symbol, particle_set(i)%r
    1748         9446 :          CALL uppercase(element_symbol)
    1749         9446 :          element_symbol_ref0 = particle_set(i)%atomic_kind%element_symbol
    1750         9446 :          element_kind_ref0 = particle_set(i)%atomic_kind%name(1:2)
    1751         9446 :          CALL uppercase(element_symbol_ref0)
    1752         9446 :          CALL uppercase(element_kind_ref0)
    1753         9446 :          IF (element_symbol /= element_symbol_ref0) THEN
    1754              :             ! Make sure the label also does not match a potential kind alias.
    1755           14 :             IF (element_symbol /= element_kind_ref0) THEN
    1756              :                errmsg = "Atomic configuration from trajectory file does not match the reference configuration: "// &
    1757              :                         "Check atom "//TRIM(ADJUSTL(cp_to_string(i)))//" of step "// &
    1758              :                         TRIM(ADJUSTL(cp_to_string(trj_itimes)))//". Found trajectory label '"// &
    1759              :                         TRIM(element_symbol)//"', expected element '"//TRIM(element_symbol_ref0)// &
    1760              :                         "' or kind label '"//TRIM(element_kind_ref0)// &
    1761              :                         "'. REFTRAJ trajectories usually contain element labels; check whether the "// &
    1762            0 :                         "trajectory was modified to contain kind aliases instead."
    1763            0 :                CPABORT(errmsg)
    1764              :             END IF
    1765              :          END IF
    1766         9446 :          particle_set(i)%r(1) = cp_unit_to_cp2k(particle_set(i)%r(1), "angstrom")
    1767         9446 :          particle_set(i)%r(2) = cp_unit_to_cp2k(particle_set(i)%r(2), "angstrom")
    1768         9730 :          particle_set(i)%r(3) = cp_unit_to_cp2k(particle_set(i)%r(3), "angstrom")
    1769              :       END DO
    1770              :       ! End of file is properly addressed in the previous call..
    1771              :       ! Let's check directly (providing some info) also for the last
    1772              :       ! line of this frame..
    1773          284 :       CALL parser_read_line(reftraj_env%info%traj_parser, 1, at_end=my_end)
    1774         1136 :       READ (UNIT=reftraj_env%info%traj_parser%input_line, FMT=*) element_symbol, particle_set(i)%r
    1775          284 :       CALL uppercase(element_symbol)
    1776          284 :       element_symbol_ref0 = particle_set(i)%atomic_kind%element_symbol
    1777          284 :       element_kind_ref0 = particle_set(i)%atomic_kind%name(1:2)
    1778          284 :       CALL uppercase(element_symbol_ref0)
    1779          284 :       CALL uppercase(element_kind_ref0)
    1780          284 :       IF (element_symbol /= element_symbol_ref0) THEN
    1781              :          ! Make sure the label also does not match a potential kind alias.
    1782            2 :          IF (element_symbol /= element_kind_ref0) THEN
    1783              :             errmsg = "Atomic configuration from trajectory file does not match the reference configuration: "// &
    1784              :                      "Check atom "//TRIM(ADJUSTL(cp_to_string(i)))//" of step "// &
    1785              :                      TRIM(ADJUSTL(cp_to_string(trj_itimes)))//". Found trajectory label '"// &
    1786              :                      TRIM(element_symbol)//"', expected element '"//TRIM(element_symbol_ref0)// &
    1787              :                      "' or kind label '"//TRIM(element_kind_ref0)// &
    1788              :                      "'. REFTRAJ trajectories usually contain element labels; check whether the "// &
    1789            0 :                      "trajectory was modified to contain kind aliases instead."
    1790            0 :             CPABORT(errmsg)
    1791              :          END IF
    1792              :       END IF
    1793          284 :       particle_set(i)%r(1) = cp_unit_to_cp2k(particle_set(i)%r(1), "angstrom")
    1794          284 :       particle_set(i)%r(2) = cp_unit_to_cp2k(particle_set(i)%r(2), "angstrom")
    1795          284 :       particle_set(i)%r(3) = cp_unit_to_cp2k(particle_set(i)%r(3), "angstrom")
    1796              : 
    1797              :       ! Check if we reached the end of the file and provide some info..
    1798          284 :       IF (my_end) THEN
    1799            0 :          IF (reftraj_env%isnap /= (simpar%nsteps - 1)) THEN
    1800              :             CALL cp_abort(__LOCATION__, &
    1801              :                           "Reached the end of the Trajectory  frames in the TRAJECTORY file. Number of "// &
    1802            0 :                           "missing frames ("//cp_to_string((simpar%nsteps - 1) - reftraj_env%isnap)//").")
    1803              :          END IF
    1804              :       END IF
    1805              : 
    1806              :       ! Read cell parameters from cell file if requested and if not yet available
    1807          284 :       IF (reftraj_env%info%variable_volume .AND. (.NOT. traj_has_cell_info)) THEN
    1808           38 :          CALL parser_get_next_line(reftraj_env%info%cell_parser, 1, at_end=my_end)
    1809           38 :          CALL parse_cell_line(reftraj_env%info%cell_parser%input_line, cell_itimes, cell_time, h, vol)
    1810           38 :          CPASSERT(trj_itimes == cell_itimes)
    1811              :          ! Check if we reached the end of the file and provide some info..
    1812           38 :          IF (my_end) THEN
    1813            0 :             IF (reftraj_env%isnap /= (simpar%nsteps - 1)) THEN
    1814              :                CALL cp_abort(__LOCATION__, &
    1815              :                              "Reached the end of the cell info frames in the CELL file. Number of "// &
    1816            0 :                              "missing frames ("//cp_to_string((simpar%nsteps - 1) - reftraj_env%isnap)//").")
    1817              :             END IF
    1818              :          END IF
    1819              :       END IF
    1820              : 
    1821          284 :       IF (init) THEN
    1822           36 :          reftraj_env%time0 = trj_time
    1823           36 :          reftraj_env%epot0 = trj_epot
    1824           36 :          reftraj_env%itimes0 = trj_itimes
    1825              :       END IF
    1826              : 
    1827          284 :       IF (trj_itimes /= 0.0_dp .AND. trj_time /= 0.0_dp) simpar%dt = (trj_time/femtoseconds)/REAL(trj_itimes, KIND=dp)
    1828              : 
    1829          284 :       reftraj_env%epot = trj_epot
    1830          284 :       reftraj_env%itimes = trj_itimes
    1831          284 :       reftraj_env%time = trj_time/femtoseconds
    1832          284 :       CALL get_md_env(md_env, itimes=itimes, t=time)
    1833          284 :       itimes = reftraj_env%itimes
    1834          284 :       time = reftraj_env%time
    1835              :       CALL cp_iterate(logger%iter_info, &
    1836              :                       last=logger%iter_info%last_iter(logger%iter_info%n_rlevel), &
    1837          284 :                       iter_nr=reftraj_env%itimes)
    1838              : 
    1839          284 :       IF (traj_has_cell_info) THEN
    1840           18 :          CALL init_cell(cell)
    1841          266 :       ELSE IF (reftraj_env%info%variable_volume) THEN
    1842          494 :          cell%hmat = h
    1843           38 :          CALL init_cell(cell)
    1844              :       END IF
    1845              : 
    1846              :       ! Wrap coordinates if requested
    1847          284 :       SELECT CASE (reftraj_env%info%wrap)
    1848              :       CASE (REFTRAJ_WRAP_NONE)
    1849              :          ! Do Nothing
    1850              :       CASE (REFTRAJ_WRAP_POSITIVE)
    1851              :          ! Wrap to positive range
    1852            0 :          DO i = 1, nparticle
    1853            0 :             particle_set(i)%r(1:3) = pbc(particle_set(i)%r(1:3), cell, positive_range=.TRUE.)
    1854              :          END DO
    1855              :       CASE (REFTRAJ_WRAP_CENTRAL)
    1856              :          ! Wrap to halfway, i.e. origin is at the center
    1857            0 :          DO i = 1, nparticle
    1858            0 :             particle_set(i)%r(1:3) = pbc(particle_set(i)%r(1:3), cell)
    1859              :          END DO
    1860              :       CASE DEFAULT
    1861              :          ! Should not reach here
    1862          284 :          CPABORT("Option invalid or unavailable for reftraj_env%info%wrap")
    1863              :       END SELECT
    1864              : 
    1865              :       ![ADAPT] update input structure with new coordinates, make new labels
    1866          284 :       CALL qmmmx_update_force_env(force_env, force_env%root_section)
    1867              :       ! no pointers into force_env%subsys to update
    1868              : 
    1869              :       ! Task to perform on the reference trajectory
    1870              :       ! Compute energy and forces
    1871              :       ![NB] let reftraj work with force mixing which does not have consistent energies and forces
    1872              :       CALL force_env_calc_energy_force(force_env, &
    1873              :                                        calc_force=(reftraj_env%info%eval == REFTRAJ_EVAL_ENERGY_FORCES), &
    1874              :                                        eval_energy_forces=(reftraj_env%info%eval /= REFTRAJ_EVAL_NONE), &
    1875          284 :                                        require_consistent_energy_force=.FALSE.)
    1876              : 
    1877              :       ! Metadynamics
    1878          284 :       CALL metadyn_integrator(force_env, trj_itimes)
    1879              : 
    1880              :       ! Compute MSD with respect to a reference configuration
    1881          284 :       IF (reftraj_env%info%msd) THEN
    1882           14 :          CALL compute_msd_reftraj(reftraj_env, md_env, particle_set)
    1883              :       END IF
    1884              : 
    1885              :       ! Skip according the stride both Trajectory and Cell (if possible)
    1886          284 :       CALL parser_get_next_line(reftraj_env%info%traj_parser, (reftraj_env%info%stride - 1)*(nparticle + 2))
    1887          284 :       IF (reftraj_env%info%variable_volume) THEN
    1888           38 :          CALL parser_get_next_line(reftraj_env%info%cell_parser, (reftraj_env%info%stride - 1))
    1889              :       END IF
    1890          284 :    END SUBROUTINE reftraj
    1891              : 
    1892              : ! **************************************************************************************************
    1893              : !> \brief nph_uniaxial integrator (non-Hamiltonian version)
    1894              : !>      for particle positions & momenta undergoing
    1895              : !>      uniaxial stress ( in x-direction of orthorhombic cell)
    1896              : !>      due to a shock compression:
    1897              : !>      Reed et. al. Physical Review Letters 90, 235503 (2003).
    1898              : !> \param md_env ...
    1899              : !> \par History
    1900              : !>      none
    1901              : !> \author CJM
    1902              : ! **************************************************************************************************
    1903           80 :    SUBROUTINE nph_uniaxial(md_env)
    1904              : 
    1905              :       TYPE(md_environment_type), POINTER                 :: md_env
    1906              : 
    1907              :       REAL(dp), PARAMETER                                :: e2 = 1._dp/6._dp, e4 = e2/20._dp, &
    1908              :                                                             e6 = e4/42._dp, e8 = e6/72._dp
    1909              : 
    1910              :       INTEGER                                            :: iroll, nparticle, nparticle_kind, nshell
    1911              :       INTEGER, POINTER                                   :: itimes
    1912              :       LOGICAL                                            :: first, first_time, shell_adiabatic, &
    1913              :                                                             shell_present
    1914              :       REAL(KIND=dp)                                      :: dt, infree, kin, roll_tol, roll_tol_thrs
    1915              :       REAL(KIND=dp), DIMENSION(3)                        :: vector_r, vector_v
    1916              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: pv_kin
    1917              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
    1918           40 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1919              :       TYPE(cell_type), POINTER                           :: cell
    1920              :       TYPE(cp_subsys_type), POINTER                      :: subsys
    1921              :       TYPE(distribution_1d_type), POINTER                :: local_molecules, local_particles
    1922              :       TYPE(force_env_type), POINTER                      :: force_env
    1923              :       TYPE(global_constraint_type), POINTER              :: gci
    1924              :       TYPE(molecule_kind_list_type), POINTER             :: molecule_kinds
    1925           40 :       TYPE(molecule_kind_type), DIMENSION(:), POINTER    :: molecule_kind_set
    1926              :       TYPE(molecule_list_type), POINTER                  :: molecules
    1927           40 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
    1928              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1929           40 :       TYPE(npt_info_type), POINTER                       :: npt(:, :)
    1930              :       TYPE(old_variables_type), POINTER                  :: old
    1931              :       TYPE(particle_list_type), POINTER                  :: core_particles, particles, &
    1932              :                                                             shell_particles
    1933           40 :       TYPE(particle_type), DIMENSION(:), POINTER         :: core_particle_set, particle_set, &
    1934           40 :                                                             shell_particle_set
    1935              :       TYPE(simpar_type), POINTER                         :: simpar
    1936              :       TYPE(tmp_variables_type), POINTER                  :: tmp
    1937              :       TYPE(virial_type), POINTER                         :: virial
    1938              : 
    1939           40 :       NULLIFY (gci, force_env)
    1940           40 :       NULLIFY (atomic_kinds, cell, para_env, subsys, local_molecules, local_particles)
    1941           40 :       NULLIFY (molecule_kinds, molecules, molecule_kind_set, npt)
    1942           40 :       NULLIFY (core_particles, particles, shell_particles, tmp, old)
    1943           40 :       NULLIFY (core_particle_set, particle_set, shell_particle_set)
    1944           40 :       NULLIFY (simpar, virial, itimes)
    1945              : 
    1946              :       CALL get_md_env(md_env=md_env, simpar=simpar, force_env=force_env, npt=npt, &
    1947           40 :                       first_time=first_time, para_env=para_env, itimes=itimes)
    1948           40 :       dt = simpar%dt
    1949           40 :       infree = 1.0_dp/REAL(simpar%nfree, dp)
    1950              : 
    1951           40 :       CALL force_env_get(force_env, subsys=subsys, cell=cell)
    1952              : 
    1953              :       ! Do some checks on coordinates and box
    1954           40 :       CALL apply_qmmm_walls_reflective(force_env)
    1955              : 
    1956              :       CALL cp_subsys_get(subsys=subsys, atomic_kinds=atomic_kinds, local_particles=local_particles, &
    1957              :                          particles=particles, local_molecules=local_molecules, molecules=molecules, gci=gci, &
    1958           40 :                          molecule_kinds=molecule_kinds, virial=virial)
    1959              : 
    1960           40 :       nparticle_kind = atomic_kinds%n_els
    1961           40 :       atomic_kind_set => atomic_kinds%els
    1962           40 :       molecule_kind_set => molecule_kinds%els
    1963              : 
    1964           40 :       nparticle = particles%n_els
    1965           40 :       particle_set => particles%els
    1966           40 :       molecule_set => molecules%els
    1967              : 
    1968           40 :       IF (first_time) THEN
    1969              :          CALL virial_evaluate(atomic_kind_set, particle_set, &
    1970            4 :                               local_particles, virial, para_env)
    1971              :       END IF
    1972              : 
    1973              :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, &
    1974           40 :                                shell_present=shell_present, shell_adiabatic=shell_adiabatic)
    1975              : 
    1976              :       ! Allocate work storage for positions and velocities
    1977           40 :       CALL allocate_old(old, particle_set, npt)
    1978              : 
    1979           40 :       IF (shell_present) THEN
    1980              :          CALL cp_subsys_get(subsys=subsys, &
    1981            0 :                             shell_particles=shell_particles, core_particles=core_particles)
    1982            0 :          shell_particle_set => shell_particles%els
    1983            0 :          nshell = SIZE(shell_particles%els)
    1984            0 :          IF (shell_adiabatic) THEN
    1985            0 :             core_particle_set => core_particles%els
    1986              :          END IF
    1987              :       END IF
    1988              : 
    1989           40 :       CALL allocate_tmp(md_env, tmp, nparticle, nshell, shell_adiabatic)
    1990              : 
    1991           40 :       IF (simpar%constraint) THEN
    1992              :          ! Possibly update the target values
    1993              :          CALL shake_update_targets(gci, local_molecules, molecule_set, &
    1994            0 :                                    molecule_kind_set, dt, force_env%root_section)
    1995              :       END IF
    1996              : 
    1997              :       ! setting up for ROLL: saving old variables
    1998           40 :       IF (simpar%constraint) THEN
    1999            0 :          roll_tol_thrs = simpar%roll_tol
    2000            0 :          iroll = 1
    2001            0 :          CALL set(old, atomic_kind_set, particle_set, local_particles, cell, npt, 'F')
    2002              :          CALL getold(gci, local_molecules, molecule_set, &
    2003            0 :                      molecule_kind_set, particle_set, cell)
    2004              :       ELSE
    2005              :          roll_tol_thrs = EPSILON(0.0_dp)
    2006              :       END IF
    2007           40 :       roll_tol = -roll_tol_thrs
    2008              : 
    2009           80 :       SR: DO WHILE (ABS(roll_tol) >= roll_tol_thrs) ! SHAKE-ROLL LOOP
    2010              : 
    2011           40 :          IF (simpar%constraint) THEN
    2012            0 :             CALL set(old, atomic_kind_set, particle_set, local_particles, cell, npt, 'B')
    2013              :          END IF
    2014              :          CALL update_pv(gci, simpar, atomic_kind_set, particle_set, &
    2015              :                         local_molecules, molecule_set, molecule_kind_set, &
    2016           40 :                         local_particles, kin, pv_kin, virial, para_env)
    2017           40 :          CALL update_veps(cell, npt, simpar, pv_kin, kin, virial, infree)
    2018              : 
    2019              :          tmp%arg_r(1) = (0.5_dp*npt(1, 1)%v*dt)* &
    2020           40 :                         (0.5_dp*npt(1, 1)%v*dt)
    2021              :          tmp%poly_r(1) = 1._dp + e2*tmp%arg_r(1) + e4*tmp%arg_r(1)*tmp%arg_r(1) + &
    2022           40 :                          e6*tmp%arg_r(1)**3 + e8*tmp%arg_r(1)**4
    2023           40 :          tmp%poly_r(2) = 1.0_dp
    2024           40 :          tmp%poly_r(3) = 1.0_dp
    2025              : 
    2026              :          tmp%arg_v(1) = (0.25_dp*npt(1, 1)%v*dt* &
    2027              :                          (1._dp + infree))*(0.25_dp*npt(1, 1)%v* &
    2028           40 :                                             dt*(1._dp + infree))
    2029              :          tmp%arg_v(2) = (0.25_dp*npt(1, 1)%v*dt*infree)* &
    2030           40 :                         (0.25_dp*npt(1, 1)%v*dt*infree)
    2031              :          tmp%poly_v(1) = 1._dp + e2*tmp%arg_v(1) + e4*tmp%arg_v(1)*tmp%arg_v(1) + &
    2032           40 :                          e6*tmp%arg_v(1)**3 + e8*tmp%arg_v(1)**4
    2033              :          tmp%poly_v(2) = 1._dp + e2*tmp%arg_v(2) + e4*tmp%arg_v(2)*tmp%arg_v(2) + &
    2034           40 :                          e6*tmp%arg_v(2)**3 + e8*tmp%arg_v(2)**4
    2035              :          tmp%poly_v(3) = 1._dp + e2*tmp%arg_v(2) + e4*tmp%arg_v(2)*tmp%arg_v(2) + &
    2036           40 :                          e6*tmp%arg_v(2)**3 + e8*tmp%arg_v(2)**4
    2037              : 
    2038           40 :          tmp%scale_r(1) = EXP(0.5_dp*dt*npt(1, 1)%v)
    2039           40 :          tmp%scale_r(2) = 1.0_dp
    2040           40 :          tmp%scale_r(3) = 1.0_dp
    2041              : 
    2042              :          tmp%scale_v(1) = EXP(-0.25_dp*dt*npt(1, 1)%v* &
    2043           40 :                               (1._dp + infree))
    2044           40 :          tmp%scale_v(2) = EXP(-0.25_dp*dt*npt(1, 1)%v*infree)
    2045           40 :          tmp%scale_v(3) = EXP(-0.25_dp*dt*npt(1, 1)%v*infree)
    2046              : 
    2047              :          ! first half of velocity verlet
    2048              :          CALL vv_first(tmp, atomic_kind_set, local_particles, particle_set, &
    2049              :                        core_particle_set, shell_particle_set, nparticle_kind, &
    2050           40 :                        shell_adiabatic, dt)
    2051              : 
    2052           40 :          IF (simpar%variable_dt) CALL variable_timestep(md_env, tmp, dt, simpar, para_env, &
    2053              :                                                         atomic_kind_set, local_particles, particle_set, core_particle_set, &
    2054            0 :                                                         shell_particle_set, nparticle_kind, shell_adiabatic, npt=npt)
    2055              : 
    2056           40 :          roll_tol = 0._dp
    2057           40 :          vector_r(:) = 0._dp
    2058          160 :          vector_v(:) = tmp%scale_v(:)*tmp%poly_v(:)
    2059           40 :          vector_r(1) = tmp%scale_r(1)*tmp%poly_r(1)
    2060              : 
    2061           40 :          IF (simpar%constraint) CALL shake_roll_control(gci, local_molecules, &
    2062              :                                                       molecule_set, molecule_kind_set, particle_set, tmp%pos, tmp%vel, dt, simpar, &
    2063              :                                                         roll_tol, iroll, vector_r, vector_v, para_env, cell=cell, &
    2064           40 :                                                         local_particles=local_particles)
    2065              :       END DO SR
    2066              : 
    2067              :       ! Update h_mat
    2068           40 :       cell%hmat(1, 1) = cell%hmat(1, 1)*tmp%scale_r(1)*tmp%scale_r(1)
    2069              : 
    2070              :       ! Update the cell
    2071           40 :       CALL init_cell(cell)
    2072              : 
    2073              :       ! Broadcast the new particle positions and deallocate the pos component of temporary
    2074              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
    2075           40 :                               core_particle_set, para_env, shell_adiabatic, pos=.TRUE.)
    2076              : 
    2077              :       ! Update forces (and stress)
    2078           40 :       CALL force_env_calc_energy_force(force_env)
    2079              : 
    2080              :       ! Metadynamics
    2081           40 :       CALL metadyn_integrator(force_env, itimes, tmp%vel)
    2082              : 
    2083              :       ! Velocity Verlet (second part)
    2084              :       CALL vv_second(tmp, atomic_kind_set, local_particles, particle_set, &
    2085              :                      core_particle_set, shell_particle_set, nparticle_kind, &
    2086           40 :                      shell_adiabatic, dt)
    2087              : 
    2088           40 :       IF (simpar%constraint) THEN
    2089            0 :          roll_tol_thrs = simpar%roll_tol
    2090            0 :          first = .TRUE.
    2091            0 :          iroll = 1
    2092            0 :          CALL set(old, atomic_kind_set, particle_set, tmp%vel, local_particles, cell, npt, 'F')
    2093              :       ELSE
    2094              :          roll_tol_thrs = EPSILON(0.0_dp)
    2095              :       END IF
    2096           40 :       roll_tol = -roll_tol_thrs
    2097              : 
    2098           80 :       RR: DO WHILE (ABS(roll_tol) >= roll_tol_thrs) ! RATTLE-ROLL LOOP
    2099           40 :          roll_tol = 0._dp
    2100           40 :          IF (simpar%constraint) CALL rattle_roll_setup(old, gci, atomic_kind_set, &
    2101              :                                                        particle_set, local_particles, molecule_kind_set, molecule_set, &
    2102              :                                                        local_molecules, tmp%vel, dt, cell, npt, simpar, virial, vector_v, &
    2103            0 :                                                        roll_tol, iroll, infree, first, para_env)
    2104              : 
    2105              :          CALL update_pv(gci, simpar, atomic_kind_set, tmp%vel, particle_set, &
    2106              :                         local_molecules, molecule_set, molecule_kind_set, &
    2107           40 :                         local_particles, kin, pv_kin, virial, para_env)
    2108           80 :          CALL update_veps(cell, npt, simpar, pv_kin, kin, virial, infree)
    2109              :       END DO RR
    2110              : 
    2111           40 :       IF (simpar%annealing) tmp%vel(:, :) = tmp%vel(:, :)*simpar%f_annealing
    2112              : 
    2113              :       ! Broadcast the new particle velocities and deallocate the temporary
    2114              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
    2115           40 :                               core_particle_set, para_env, shell_adiabatic, vel=.TRUE.)
    2116              : 
    2117              :       ! Update constraint virial
    2118           40 :       IF (simpar%constraint) CALL pv_constraint(gci, local_molecules, &
    2119            0 :                                                 molecule_set, molecule_kind_set, particle_set, virial, para_env)
    2120              : 
    2121              :       CALL virial_evaluate(atomic_kind_set, particle_set, &
    2122           40 :                            local_particles, virial, para_env)
    2123              : 
    2124              :       ! Deallocate old variables
    2125           40 :       CALL deallocate_old(old)
    2126              : 
    2127           40 :       IF (first_time) THEN
    2128            4 :          first_time = .FALSE.
    2129            4 :          CALL set_md_env(md_env, first_time=first_time)
    2130              :       END IF
    2131              : 
    2132           40 :    END SUBROUTINE nph_uniaxial
    2133              : 
    2134              : ! **************************************************************************************************
    2135              : !> \brief nph_uniaxial integrator (non-Hamiltonian version)
    2136              : !>      for particle positions & momenta undergoing
    2137              : !>      uniaxial stress ( in x-direction of orthorhombic cell)
    2138              : !>      due to a shock compression:
    2139              : !>      Reed et. al. Physical Review Letters 90, 235503 (2003).
    2140              : !>      Added damping (e.g. thermostat to barostat)
    2141              : !> \param md_env ...
    2142              : !> \par History
    2143              : !>      none
    2144              : !> \author CJM
    2145              : ! **************************************************************************************************
    2146           40 :    SUBROUTINE nph_uniaxial_damped(md_env)
    2147              : 
    2148              :       TYPE(md_environment_type), POINTER                 :: md_env
    2149              : 
    2150              :       REAL(dp), PARAMETER                                :: e2 = 1._dp/6._dp, e4 = e2/20._dp, &
    2151              :                                                             e6 = e4/42._dp, e8 = e6/72._dp
    2152              : 
    2153              :       INTEGER                                            :: iroll, nparticle, nparticle_kind, nshell
    2154              :       INTEGER, POINTER                                   :: itimes
    2155              :       LOGICAL                                            :: first, first_time, shell_adiabatic, &
    2156              :                                                             shell_present
    2157              :       REAL(KIND=dp)                                      :: aa, aax, dt, gamma1, infree, kin, &
    2158              :                                                             roll_tol, roll_tol_thrs
    2159              :       REAL(KIND=dp), DIMENSION(3)                        :: vector_r, vector_v
    2160              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: pv_kin
    2161              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
    2162           20 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    2163              :       TYPE(cell_type), POINTER                           :: cell
    2164              :       TYPE(cp_subsys_type), POINTER                      :: subsys
    2165              :       TYPE(distribution_1d_type), POINTER                :: local_molecules, local_particles
    2166              :       TYPE(force_env_type), POINTER                      :: force_env
    2167              :       TYPE(global_constraint_type), POINTER              :: gci
    2168              :       TYPE(molecule_kind_list_type), POINTER             :: molecule_kinds
    2169           20 :       TYPE(molecule_kind_type), DIMENSION(:), POINTER    :: molecule_kind_set
    2170              :       TYPE(molecule_list_type), POINTER                  :: molecules
    2171           20 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
    2172              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    2173           20 :       TYPE(npt_info_type), POINTER                       :: npt(:, :)
    2174              :       TYPE(old_variables_type), POINTER                  :: old
    2175              :       TYPE(particle_list_type), POINTER                  :: core_particles, particles, &
    2176              :                                                             shell_particles
    2177           20 :       TYPE(particle_type), DIMENSION(:), POINTER         :: core_particle_set, particle_set, &
    2178           20 :                                                             shell_particle_set
    2179              :       TYPE(simpar_type), POINTER                         :: simpar
    2180              :       TYPE(tmp_variables_type), POINTER                  :: tmp
    2181              :       TYPE(virial_type), POINTER                         :: virial
    2182              : 
    2183           20 :       NULLIFY (gci, force_env)
    2184           20 :       NULLIFY (atomic_kinds, cell, para_env, subsys, local_molecules, local_particles)
    2185           20 :       NULLIFY (molecule_kinds, molecules, molecule_kind_set, npt)
    2186           20 :       NULLIFY (core_particles, particles, shell_particles, tmp, old)
    2187           20 :       NULLIFY (core_particle_set, particle_set, shell_particle_set)
    2188           20 :       NULLIFY (simpar, virial, itimes)
    2189              : 
    2190              :       CALL get_md_env(md_env=md_env, simpar=simpar, force_env=force_env, npt=npt, &
    2191           20 :                       first_time=first_time, para_env=para_env, itimes=itimes)
    2192           20 :       dt = simpar%dt
    2193           20 :       infree = 1.0_dp/REAL(simpar%nfree, dp)
    2194           20 :       gamma1 = simpar%gamma_nph
    2195              : 
    2196           20 :       CALL force_env_get(force_env, subsys=subsys, cell=cell)
    2197              : 
    2198              :       CALL cp_subsys_get(subsys=subsys, atomic_kinds=atomic_kinds, local_particles=local_particles, &
    2199              :                          particles=particles, local_molecules=local_molecules, molecules=molecules, gci=gci, &
    2200           20 :                          molecule_kinds=molecule_kinds, virial=virial)
    2201              : 
    2202           20 :       nparticle_kind = atomic_kinds%n_els
    2203           20 :       atomic_kind_set => atomic_kinds%els
    2204           20 :       molecule_kind_set => molecule_kinds%els
    2205              : 
    2206           20 :       nparticle = particles%n_els
    2207           20 :       particle_set => particles%els
    2208           20 :       molecule_set => molecules%els
    2209              : 
    2210           20 :       IF (first_time) THEN
    2211              :          CALL virial_evaluate(atomic_kind_set, particle_set, &
    2212            2 :                               local_particles, virial, para_env)
    2213              :       END IF
    2214              : 
    2215              :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, &
    2216           20 :                                shell_present=shell_present, shell_adiabatic=shell_adiabatic)
    2217              : 
    2218              :       ! Allocate work storage for positions and velocities
    2219           20 :       CALL allocate_old(old, particle_set, npt)
    2220              : 
    2221           20 :       IF (shell_present) THEN
    2222              :          CALL cp_subsys_get(subsys=subsys, &
    2223            0 :                             shell_particles=shell_particles, core_particles=core_particles)
    2224            0 :          shell_particle_set => shell_particles%els
    2225            0 :          nshell = SIZE(shell_particles%els)
    2226            0 :          IF (shell_adiabatic) THEN
    2227            0 :             core_particle_set => core_particles%els
    2228              :          END IF
    2229              :       END IF
    2230              : 
    2231           20 :       CALL allocate_tmp(md_env, tmp, nparticle, nshell, shell_adiabatic)
    2232              : 
    2233              :       ! perform damping on velocities
    2234              :       CALL damp_v(molecule_kind_set, molecule_set, particle_set, local_molecules, &
    2235           20 :                   gamma1, npt(1, 1), dt, para_env)
    2236              : 
    2237           20 :       IF (simpar%constraint) THEN
    2238              :          ! Possibly update the target values
    2239              :          CALL shake_update_targets(gci, local_molecules, molecule_set, &
    2240            0 :                                    molecule_kind_set, dt, force_env%root_section)
    2241              :       END IF
    2242              : 
    2243              :       ! setting up for ROLL: saving old variables
    2244           20 :       IF (simpar%constraint) THEN
    2245            0 :          roll_tol_thrs = simpar%roll_tol
    2246            0 :          iroll = 1
    2247            0 :          CALL set(old, atomic_kind_set, particle_set, local_particles, cell, npt, 'F')
    2248              :          CALL getold(gci, local_molecules, molecule_set, &
    2249            0 :                      molecule_kind_set, particle_set, cell)
    2250              :       ELSE
    2251              :          roll_tol_thrs = EPSILON(0.0_dp)
    2252              :       END IF
    2253           20 :       roll_tol = -roll_tol_thrs
    2254              : 
    2255           40 :       SR: DO WHILE (ABS(roll_tol) >= roll_tol_thrs) ! SHAKE-ROLL LOOP
    2256              : 
    2257              :          ! perform damping on the barostat momentum
    2258           20 :          CALL damp_veps(npt(1, 1), gamma1, dt)
    2259              : 
    2260           20 :          IF (simpar%constraint) THEN
    2261            0 :             CALL set(old, atomic_kind_set, particle_set, local_particles, cell, npt, 'B')
    2262              :          END IF
    2263              :          CALL update_pv(gci, simpar, atomic_kind_set, particle_set, &
    2264              :                         local_molecules, molecule_set, molecule_kind_set, &
    2265           20 :                         local_particles, kin, pv_kin, virial, para_env)
    2266           20 :          CALL update_veps(cell, npt, simpar, pv_kin, kin, virial, infree)
    2267              : 
    2268              :          ! perform damping on the barostat momentum
    2269           20 :          CALL damp_veps(npt(1, 1), gamma1, dt)
    2270              : 
    2271              :          tmp%arg_r(1) = (0.5_dp*npt(1, 1)%v*dt)* &
    2272           20 :                         (0.5_dp*npt(1, 1)%v*dt)
    2273              :          tmp%poly_r(1) = 1._dp + e2*tmp%arg_r(1) + e4*tmp%arg_r(1)*tmp%arg_r(1) + &
    2274           20 :                          e6*tmp%arg_r(1)**3 + e8*tmp%arg_r(1)**4
    2275              : 
    2276           20 :          aax = npt(1, 1)%v*(1.0_dp + infree)
    2277           20 :          tmp%arg_v(1) = (0.25_dp*dt*aax)*(0.25_dp*dt*aax)
    2278              :          tmp%poly_v(1) = 1._dp + e2*tmp%arg_v(1) + e4*tmp%arg_v(1)*tmp%arg_v(1) + &
    2279           20 :                          e6*tmp%arg_v(1)**3 + e8*tmp%arg_v(1)**4
    2280              : 
    2281           20 :          aa = npt(1, 1)%v*infree
    2282           20 :          tmp%arg_v(2) = (0.25_dp*dt*aa)*(0.25_dp*dt*aa)
    2283              :          tmp%poly_v(2) = 1._dp + e2*tmp%arg_v(2) + e4*tmp%arg_v(2)*tmp%arg_v(2) + &
    2284           20 :                          e6*tmp%arg_v(2)**3 + e8*tmp%arg_v(2)**4
    2285              :          tmp%poly_v(3) = 1._dp + e2*tmp%arg_v(2) + e4*tmp%arg_v(2)*tmp%arg_v(2) + &
    2286           20 :                          e6*tmp%arg_v(2)**3 + e8*tmp%arg_v(2)**4
    2287              : 
    2288           20 :          tmp%scale_r(1) = EXP(0.5_dp*dt*npt(1, 1)%v)
    2289           20 :          tmp%scale_v(1) = EXP(-0.25_dp*dt*aax)
    2290           20 :          tmp%scale_v(2) = EXP(-0.25_dp*dt*aa)
    2291           20 :          tmp%scale_v(3) = EXP(-0.25_dp*dt*aa)
    2292              : 
    2293              :          ! first half of velocity verlet
    2294              :          CALL vv_first(tmp, atomic_kind_set, local_particles, particle_set, &
    2295              :                        core_particle_set, shell_particle_set, nparticle_kind, &
    2296           20 :                        shell_adiabatic, dt)
    2297              : 
    2298           20 :          IF (simpar%variable_dt) CALL variable_timestep(md_env, tmp, dt, simpar, para_env, &
    2299              :                                                         atomic_kind_set, local_particles, particle_set, core_particle_set, &
    2300            0 :                                                         shell_particle_set, nparticle_kind, shell_adiabatic, npt=npt)
    2301              : 
    2302           20 :          roll_tol = 0._dp
    2303           20 :          vector_r(:) = 0._dp
    2304           80 :          vector_v(:) = tmp%scale_v(:)*tmp%poly_v(:)
    2305           20 :          vector_r(1) = tmp%scale_r(1)*tmp%poly_r(1)
    2306              : 
    2307           20 :          IF (simpar%constraint) CALL shake_roll_control(gci, local_molecules, &
    2308              :                                                       molecule_set, molecule_kind_set, particle_set, tmp%pos, tmp%vel, dt, simpar, &
    2309              :                                                         roll_tol, iroll, vector_r, vector_v, para_env, cell=cell, &
    2310           20 :                                                         local_particles=local_particles)
    2311              :       END DO SR
    2312              : 
    2313              :       ! Update h_mat
    2314           20 :       cell%hmat(1, 1) = cell%hmat(1, 1)*tmp%scale_r(1)*tmp%scale_r(1)
    2315              : 
    2316              :       ! Update the inverse
    2317           20 :       CALL init_cell(cell)
    2318              : 
    2319              :       ! Broadcast the new particle positions and deallocate the pos components of temporary
    2320              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
    2321           20 :                               core_particle_set, para_env, shell_adiabatic, pos=.TRUE.)
    2322              : 
    2323              :       ! Update forces
    2324           20 :       CALL force_env_calc_energy_force(force_env)
    2325              : 
    2326              :       ! Metadynamics
    2327           20 :       CALL metadyn_integrator(force_env, itimes, tmp%vel)
    2328              : 
    2329              :       ! Velocity Verlet (second part)
    2330              :       CALL vv_second(tmp, atomic_kind_set, local_particles, particle_set, &
    2331              :                      core_particle_set, shell_particle_set, nparticle_kind, &
    2332           20 :                      shell_adiabatic, dt)
    2333              : 
    2334           20 :       IF (simpar%constraint) THEN
    2335            0 :          roll_tol_thrs = simpar%roll_tol
    2336            0 :          first = .TRUE.
    2337            0 :          iroll = 1
    2338            0 :          CALL set(old, atomic_kind_set, particle_set, tmp%vel, local_particles, cell, npt, 'F')
    2339              :       ELSE
    2340              :          roll_tol_thrs = EPSILON(0.0_dp)
    2341              :       END IF
    2342           20 :       roll_tol = -roll_tol_thrs
    2343              : 
    2344           40 :       RR: DO WHILE (ABS(roll_tol) >= roll_tol_thrs) ! RATTLE-ROLL LOOP
    2345           20 :          roll_tol = 0._dp
    2346           20 :          IF (simpar%constraint) CALL rattle_roll_setup(old, gci, atomic_kind_set, &
    2347              :                                                   particle_set, local_particles, molecule_kind_set, molecule_set, local_molecules, &
    2348              :                                                  tmp%vel, dt, cell, npt, simpar, virial, vector_v, roll_tol, iroll, infree, first, &
    2349            0 :                                                        para_env)
    2350              :          ! perform damping on the barostat momentum
    2351           20 :          CALL damp_veps(npt(1, 1), gamma1, dt)
    2352              : 
    2353              :          CALL update_pv(gci, simpar, atomic_kind_set, tmp%vel, particle_set, &
    2354              :                         local_molecules, molecule_set, molecule_kind_set, &
    2355           20 :                         local_particles, kin, pv_kin, virial, para_env)
    2356           20 :          CALL update_veps(cell, npt, simpar, pv_kin, kin, virial, infree)
    2357              : 
    2358              :          ! perform damping on the barostat momentum
    2359           20 :          CALL damp_veps(npt(1, 1), gamma1, dt)
    2360              : 
    2361              :       END DO RR
    2362              : 
    2363              :       ! perform damping on velocities
    2364              :       CALL damp_v(molecule_kind_set, molecule_set, particle_set, local_molecules, &
    2365           20 :                   tmp%vel, gamma1, npt(1, 1), dt, para_env)
    2366              : 
    2367           20 :       IF (simpar%annealing) tmp%vel(:, :) = tmp%vel(:, :)*simpar%f_annealing
    2368              : 
    2369              :       ! Broadcast the new particle velocities and deallocate temporary
    2370              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
    2371           20 :                               core_particle_set, para_env, shell_adiabatic, vel=.TRUE.)
    2372              : 
    2373              :       ! Update constraint virial
    2374           20 :       IF (simpar%constraint) CALL pv_constraint(gci, local_molecules, &
    2375            0 :                                                 molecule_set, molecule_kind_set, particle_set, virial, para_env)
    2376              : 
    2377              :       CALL virial_evaluate(atomic_kind_set, particle_set, &
    2378           20 :                            local_particles, virial, para_env)
    2379              : 
    2380              :       ! Deallocate old variables
    2381           20 :       CALL deallocate_old(old)
    2382              : 
    2383           20 :       IF (first_time) THEN
    2384            2 :          first_time = .FALSE.
    2385            2 :          CALL set_md_env(md_env, first_time=first_time)
    2386              :       END IF
    2387              : 
    2388           20 :    END SUBROUTINE nph_uniaxial_damped
    2389              : 
    2390              : ! **************************************************************************************************
    2391              : !> \brief Velocity Verlet integrator for the NPT ensemble with fully flexible cell
    2392              : !> \param md_env ...
    2393              : !> \param globenv ...
    2394              : !> \par History
    2395              : !>      none
    2396              : !> \author CJM
    2397              : ! **************************************************************************************************
    2398          940 :    SUBROUTINE npt_f(md_env, globenv)
    2399              : 
    2400              :       TYPE(md_environment_type), POINTER                 :: md_env
    2401              :       TYPE(global_environment_type), POINTER             :: globenv
    2402              : 
    2403              :       REAL(KIND=dp), PARAMETER                           :: e2 = 1.0_dp/6.0_dp, e4 = e2/20.0_dp, &
    2404              :                                                             e6 = e4/42.0_dp, e8 = e6/72.0_dp
    2405              : 
    2406              :       INTEGER                                            :: i, iroll, j, nparticle, nparticle_kind, &
    2407              :                                                             nshell
    2408              :       INTEGER, POINTER                                   :: itimes
    2409              :       LOGICAL                                            :: first, first_time, shell_adiabatic, &
    2410              :                                                             shell_check_distance, shell_present
    2411              :       REAL(KIND=dp)                                      :: dt, infree, kin, roll_tol, &
    2412              :                                                             roll_tol_thrs, trvg
    2413              :       REAL(KIND=dp), DIMENSION(3)                        :: vector_r, vector_v
    2414              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: pv_kin, uh
    2415              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
    2416          940 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    2417              :       TYPE(barostat_type), POINTER                       :: barostat
    2418              :       TYPE(cell_type), POINTER                           :: cell
    2419              :       TYPE(cp_subsys_type), POINTER                      :: subsys
    2420              :       TYPE(distribution_1d_type), POINTER                :: local_molecules, local_particles
    2421              :       TYPE(force_env_type), POINTER                      :: force_env
    2422              :       TYPE(global_constraint_type), POINTER              :: gci
    2423              :       TYPE(molecule_kind_list_type), POINTER             :: molecule_kinds
    2424          940 :       TYPE(molecule_kind_type), DIMENSION(:), POINTER    :: molecule_kind_set
    2425              :       TYPE(molecule_list_type), POINTER                  :: molecules
    2426          940 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
    2427              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    2428          940 :       TYPE(npt_info_type), POINTER                       :: npt(:, :)
    2429              :       TYPE(old_variables_type), POINTER                  :: old
    2430              :       TYPE(particle_list_type), POINTER                  :: core_particles, particles, &
    2431              :                                                             shell_particles
    2432          940 :       TYPE(particle_type), DIMENSION(:), POINTER         :: core_particle_set, particle_set, &
    2433          940 :                                                             shell_particle_set
    2434              :       TYPE(simpar_type), POINTER                         :: simpar
    2435              :       TYPE(thermostat_type), POINTER                     :: thermostat_baro, thermostat_part, &
    2436              :                                                             thermostat_shell
    2437              :       TYPE(tmp_variables_type), POINTER                  :: tmp
    2438              :       TYPE(virial_type), POINTER                         :: virial
    2439              : 
    2440          940 :       NULLIFY (gci, thermostat_baro, thermostat_part, thermostat_shell, force_env)
    2441          940 :       NULLIFY (atomic_kinds, cell, para_env, subsys, local_molecules, local_particles)
    2442          940 :       NULLIFY (molecule_kinds, molecules, molecule_kind_set, npt, barostat)
    2443          940 :       NULLIFY (core_particles, particles, shell_particles, tmp, old)
    2444          940 :       NULLIFY (core_particle_set, particle_set, shell_particle_set)
    2445          940 :       NULLIFY (simpar, virial, itimes)
    2446              : 
    2447              :       CALL get_md_env(md_env=md_env, simpar=simpar, force_env=force_env, &
    2448              :                       thermostat_part=thermostat_part, thermostat_baro=thermostat_baro, &
    2449              :                       thermostat_shell=thermostat_shell, npt=npt, first_time=first_time, &
    2450          940 :                       para_env=para_env, barostat=barostat, itimes=itimes)
    2451          940 :       dt = simpar%dt
    2452          940 :       infree = 1.0_dp/REAL(simpar%nfree, KIND=dp)
    2453              : 
    2454          940 :       CALL force_env_get(force_env, subsys=subsys, cell=cell)
    2455              : 
    2456              :       ! Do some checks on coordinates and box
    2457          940 :       CALL apply_qmmm_walls_reflective(force_env)
    2458              : 
    2459              :       CALL cp_subsys_get(subsys=subsys, atomic_kinds=atomic_kinds, local_particles=local_particles, &
    2460              :                          particles=particles, local_molecules=local_molecules, molecules=molecules, &
    2461          940 :                          gci=gci, molecule_kinds=molecule_kinds, virial=virial)
    2462              : 
    2463          940 :       nparticle_kind = atomic_kinds%n_els
    2464          940 :       atomic_kind_set => atomic_kinds%els
    2465          940 :       molecule_kind_set => molecule_kinds%els
    2466              : 
    2467          940 :       nparticle = particles%n_els
    2468          940 :       particle_set => particles%els
    2469          940 :       molecule_set => molecules%els
    2470              : 
    2471              :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, &
    2472              :                                shell_present=shell_present, shell_adiabatic=shell_adiabatic, &
    2473          940 :                                shell_check_distance=shell_check_distance)
    2474              : 
    2475          940 :       IF (first_time) THEN
    2476              :          CALL virial_evaluate(atomic_kind_set, particle_set, &
    2477           64 :                               local_particles, virial, para_env)
    2478              :       END IF
    2479              : 
    2480              :       ! Allocate work storage for positions and velocities
    2481          940 :       CALL allocate_old(old, particle_set, npt)
    2482              : 
    2483          940 :       IF (shell_present) THEN
    2484              :          CALL cp_subsys_get(subsys=subsys, &
    2485          650 :                             shell_particles=shell_particles, core_particles=core_particles)
    2486          650 :          shell_particle_set => shell_particles%els
    2487          650 :          nshell = SIZE(shell_particles%els)
    2488          650 :          IF (shell_adiabatic) THEN
    2489          650 :             core_particle_set => core_particles%els
    2490              :          END IF
    2491              :       END IF
    2492              : 
    2493          940 :       CALL allocate_tmp(md_env, tmp, nparticle, nshell, shell_adiabatic)
    2494              : 
    2495              :       ! Apply Thermostat to Barostat
    2496          940 :       CALL apply_thermostat_baro(thermostat_baro, npt, para_env)
    2497              : 
    2498              :       ! Apply Thermostat over the full set of particles
    2499          940 :       IF (simpar%ensemble /= npe_f_ensemble) THEN
    2500          700 :          IF (shell_adiabatic) THEN
    2501              :             CALL apply_thermostat_particles(thermostat_part, force_env, molecule_kind_set, molecule_set, &
    2502              :                                         particle_set, local_molecules, local_particles, para_env, shell_adiabatic=shell_adiabatic, &
    2503          410 :                                             shell_particle_set=shell_particle_set, core_particle_set=core_particle_set)
    2504              :          ELSE
    2505              :             CALL apply_thermostat_particles(thermostat_part, force_env, molecule_kind_set, molecule_set, &
    2506          290 :                                             particle_set, local_molecules, local_particles, para_env)
    2507              :          END IF
    2508              :       END IF
    2509              : 
    2510              :       ! Apply Thermostat over the core-shell motion
    2511              :       CALL apply_thermostat_shells(thermostat_shell, atomic_kind_set, particle_set, &
    2512              :                                    local_particles, para_env, shell_particle_set=shell_particle_set, &
    2513          940 :                                    core_particle_set=core_particle_set)
    2514              : 
    2515          940 :       IF (simpar%constraint) THEN
    2516              :          ! Possibly update the target values
    2517              :          CALL shake_update_targets(gci, local_molecules, molecule_set, &
    2518           10 :                                    molecule_kind_set, dt, force_env%root_section)
    2519              :       END IF
    2520              : 
    2521              :       ! setting up for ROLL: saving old variables
    2522          940 :       IF (simpar%constraint) THEN
    2523           10 :          roll_tol_thrs = simpar%roll_tol
    2524           10 :          iroll = 1
    2525           10 :          CALL set(old, atomic_kind_set, particle_set, local_particles, cell, npt, 'F')
    2526              :          CALL getold(gci, local_molecules, molecule_set, &
    2527           10 :                      molecule_kind_set, particle_set, cell)
    2528              :       ELSE
    2529              :          roll_tol_thrs = EPSILON(0.0_dp)
    2530              :       END IF
    2531          940 :       roll_tol = -roll_tol_thrs
    2532              : 
    2533         1890 :       SR: DO WHILE (ABS(roll_tol) >= roll_tol_thrs) ! SHAKE-ROLL LOOP
    2534              : 
    2535          950 :          IF (simpar%constraint) THEN
    2536           20 :             CALL set(old, atomic_kind_set, particle_set, local_particles, cell, npt, 'B')
    2537              :          END IF
    2538              :          CALL update_pv(gci, simpar, atomic_kind_set, particle_set, &
    2539              :                         local_molecules, molecule_set, molecule_kind_set, &
    2540          950 :                         local_particles, kin, pv_kin, virial, para_env)
    2541              :          CALL update_veps(cell, npt, simpar, pv_kin, kin, virial, infree, &
    2542          950 :                           virial_components=barostat%virial_components)
    2543              : 
    2544          950 :          trvg = npt(1, 1)%v + npt(2, 2)%v + npt(3, 3)%v
    2545              :          !
    2546              :          ! find eigenvalues and eigenvectors of npt ( :, : )%v
    2547              :          !
    2548              : 
    2549              :          CALL diagonalise(matrix=npt(:, :)%v, mysize=3, &
    2550        12350 :                           uplo="U", eigenvalues=tmp%e_val, eigenvectors=tmp%u)
    2551              : 
    2552              :          tmp%arg_r(:) = 0.5_dp*tmp%e_val(:)*dt* &
    2553         3800 :                         0.5_dp*tmp%e_val(:)*dt
    2554              :          tmp%poly_r = 1.0_dp + e2*tmp%arg_r + e4*tmp%arg_r*tmp%arg_r + &
    2555         3800 :                       e6*tmp%arg_r**3 + e8*tmp%arg_r**4
    2556         3800 :          tmp%scale_r(:) = EXP(0.5_dp*dt*tmp%e_val(:))
    2557              : 
    2558              :          tmp%arg_v(:) = 0.25_dp*dt*(tmp%e_val(:) + trvg*infree)* &
    2559         3800 :                         0.25_dp*dt*(tmp%e_val(:) + trvg*infree)
    2560              :          tmp%poly_v = 1.0_dp + e2*tmp%arg_v + e4*tmp%arg_v*tmp%arg_v + &
    2561         3800 :                       e6*tmp%arg_v**3 + e8*tmp%arg_v**4
    2562         3800 :          tmp%scale_v(:) = EXP(-0.25_dp*dt*(tmp%e_val(:) + trvg*infree))
    2563              : 
    2564              :          CALL vv_first(tmp, atomic_kind_set, local_particles, particle_set, &
    2565              :                        core_particle_set, shell_particle_set, nparticle_kind, &
    2566          950 :                        shell_adiabatic, dt, u=tmp%u)
    2567              : 
    2568          950 :          IF (simpar%variable_dt) CALL variable_timestep(md_env, tmp, dt, simpar, para_env, &
    2569              :                                                         atomic_kind_set, local_particles, particle_set, core_particle_set, &
    2570          200 :                                                         shell_particle_set, nparticle_kind, shell_adiabatic, npt=npt)
    2571              : 
    2572          950 :          roll_tol = 0.0_dp
    2573         3800 :          vector_r = tmp%scale_r*tmp%poly_r
    2574         3800 :          vector_v = tmp%scale_v*tmp%poly_v
    2575              : 
    2576          950 :          IF (simpar%constraint) CALL shake_roll_control(gci, local_molecules, &
    2577              :                                                         molecule_set, molecule_kind_set, particle_set, tmp%pos, tmp%vel, dt, &
    2578              :                                                         simpar, roll_tol, iroll, vector_r, vector_v, &
    2579              :                                                         para_env, u=tmp%u, cell=cell, &
    2580          960 :                                                         local_particles=local_particles)
    2581              :       END DO SR
    2582              : 
    2583              :       ! Update h_mat
    2584        37600 :       uh = MATMUL(TRANSPOSE(tmp%u), cell%hmat)
    2585              : 
    2586         3760 :       DO i = 1, 3
    2587        12220 :          DO j = 1, 3
    2588        11280 :             uh(i, j) = uh(i, j)*tmp%scale_r(i)*tmp%scale_r(i)
    2589              :          END DO
    2590              :       END DO
    2591              : 
    2592        48880 :       cell%hmat = MATMUL(tmp%u, uh)
    2593              :       ! Update the inverse
    2594          940 :       CALL init_cell(cell)
    2595              : 
    2596              :       ! Broadcast the new particle positions and deallocate the pos components of temporary
    2597              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
    2598          940 :                               core_particle_set, para_env, shell_adiabatic, pos=.TRUE.)
    2599              : 
    2600          940 :       IF (shell_adiabatic .AND. shell_check_distance) THEN
    2601              :          CALL optimize_shell_core(force_env, particle_set, &
    2602          170 :                                   shell_particle_set, core_particle_set, globenv, tmp=tmp, check=.TRUE.)
    2603              :       END IF
    2604              : 
    2605              :       ! Update forces
    2606          940 :       CALL force_env_calc_energy_force(force_env)
    2607              : 
    2608              :       ! Metadynamics
    2609          940 :       CALL metadyn_integrator(force_env, itimes, tmp%vel)
    2610              : 
    2611              :       ! Velocity Verlet (second part)
    2612              :       CALL vv_second(tmp, atomic_kind_set, local_particles, particle_set, &
    2613              :                      core_particle_set, shell_particle_set, nparticle_kind, &
    2614          940 :                      shell_adiabatic, dt, tmp%u)
    2615              : 
    2616          940 :       IF (simpar%constraint) THEN
    2617           10 :          roll_tol_thrs = simpar%roll_tol
    2618           10 :          first = .TRUE.
    2619           10 :          iroll = 1
    2620           10 :          CALL set(old, atomic_kind_set, particle_set, tmp%vel, local_particles, cell, npt, 'F')
    2621              :       ELSE
    2622              :          roll_tol_thrs = EPSILON(0.0_dp)
    2623              :       END IF
    2624          940 :       roll_tol = -roll_tol_thrs
    2625              : 
    2626         1890 :       RR: DO WHILE (ABS(roll_tol) >= roll_tol_thrs) ! RATTLE-ROLL LOOP
    2627          950 :          roll_tol = 0.0_dp
    2628          950 :          IF (simpar%constraint) CALL rattle_roll_setup(old, gci, atomic_kind_set, &
    2629              :                                                        particle_set, local_particles, molecule_kind_set, molecule_set, &
    2630              :                                                        local_molecules, tmp%vel, dt, cell, npt, simpar, virial, vector_v, &
    2631           20 :                                                        roll_tol, iroll, infree, first, para_env, u=tmp%u)
    2632              : 
    2633              :          CALL update_pv(gci, simpar, atomic_kind_set, tmp%vel, particle_set, &
    2634              :                         local_molecules, molecule_set, molecule_kind_set, &
    2635          950 :                         local_particles, kin, pv_kin, virial, para_env)
    2636              :          CALL update_veps(cell, npt, simpar, pv_kin, kin, virial, infree, &
    2637         1890 :                           virial_components=barostat%virial_components)
    2638              :       END DO RR
    2639              : 
    2640              :       ! Apply Thermostat over the full set of particles
    2641          940 :       IF (simpar%ensemble /= npe_f_ensemble) THEN
    2642          700 :          IF (shell_adiabatic) THEN
    2643              :             CALL apply_thermostat_particles(thermostat_part, force_env, molecule_kind_set, molecule_set, &
    2644              :                                         particle_set, local_molecules, local_particles, para_env, shell_adiabatic=shell_adiabatic, &
    2645          410 :                                             vel=tmp%vel, shell_vel=tmp%shell_vel, core_vel=tmp%core_vel)
    2646              : 
    2647              :          ELSE
    2648              :             CALL apply_thermostat_particles(thermostat_part, force_env, molecule_kind_set, molecule_set, &
    2649          290 :                                             particle_set, local_molecules, local_particles, para_env, vel=tmp%vel)
    2650              :          END IF
    2651              :       END IF
    2652              : 
    2653              :       ! Apply Thermostat over the core-shell motion
    2654          940 :       IF (ASSOCIATED(thermostat_shell)) THEN
    2655              :          CALL apply_thermostat_shells(thermostat_shell, atomic_kind_set, particle_set, &
    2656              :                                       local_particles, para_env, vel=tmp%vel, shell_vel=tmp%shell_vel, &
    2657          320 :                                       core_vel=tmp%core_vel)
    2658              :       END IF
    2659              : 
    2660              :       ! Apply Thermostat to Barostat
    2661          940 :       CALL apply_thermostat_baro(thermostat_baro, npt, para_env)
    2662              : 
    2663              :       ! Annealing of particle velocities is only possible when no thermostat is active
    2664          940 :       IF (simpar%ensemble == npe_f_ensemble .AND. simpar%annealing) THEN
    2665        30800 :          tmp%vel(:, :) = tmp%vel(:, :)*simpar%f_annealing
    2666           80 :          IF (shell_adiabatic) THEN
    2667              :             CALL shell_scale_comv(atomic_kind_set, local_particles, particle_set, &
    2668           80 :                                   tmp%vel, tmp%shell_vel, tmp%core_vel)
    2669              :          END IF
    2670              :       END IF
    2671              :       ! Annealing of CELL velocities is only possible when no thermostat is active
    2672          940 :       IF (simpar%ensemble == npe_f_ensemble .AND. simpar%annealing_cell) THEN
    2673          520 :          npt(:, :)%v = npt(:, :)%v*simpar%f_annealing_cell
    2674              :       END IF
    2675              : 
    2676              :       ! Broadcast the new particle velocities and deallocate temporary
    2677              :       CALL update_dealloc_tmp(tmp, particle_set, shell_particle_set, &
    2678          940 :                               core_particle_set, para_env, shell_adiabatic, vel=.TRUE.)
    2679              : 
    2680              :       ! Update constraint virial
    2681          940 :       IF (simpar%constraint) THEN
    2682              :          CALL pv_constraint(gci, local_molecules, molecule_set, &
    2683           10 :                             molecule_kind_set, particle_set, virial, para_env)
    2684              :       END IF
    2685              : 
    2686              :       CALL virial_evaluate(atomic_kind_set, particle_set, &
    2687          940 :                            local_particles, virial, para_env)
    2688              : 
    2689              :       ! Deallocate old variables
    2690          940 :       CALL deallocate_old(old)
    2691              : 
    2692          940 :       IF (first_time) THEN
    2693           64 :          first_time = .FALSE.
    2694           64 :          CALL set_md_env(md_env, first_time=first_time)
    2695              :       END IF
    2696              : 
    2697         1880 :    END SUBROUTINE npt_f
    2698              : 
    2699              : ! **************************************************************************************************
    2700              : !> \brief RESPA integrator for nve ensemble for particle positions & momenta
    2701              : !> \param md_env ...
    2702              : !> \author FS
    2703              : ! **************************************************************************************************
    2704           14 :    SUBROUTINE nve_respa(md_env)
    2705              : 
    2706              :       TYPE(md_environment_type), POINTER                 :: md_env
    2707              : 
    2708              :       INTEGER                                            :: i_step, iparticle, iparticle_kind, &
    2709              :                                                             iparticle_local, n_time_steps, &
    2710              :                                                             nparticle, nparticle_kind, &
    2711              :                                                             nparticle_local
    2712              :       INTEGER, POINTER                                   :: itimes
    2713              :       REAL(KIND=dp)                                      :: constraint_dt, dm, dt, inner_dt, mass
    2714              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: pos, vel
    2715              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
    2716           14 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    2717              :       TYPE(atomic_kind_type), POINTER                    :: atomic_kind
    2718              :       TYPE(cell_type), POINTER                           :: cell
    2719              :       TYPE(cp_subsys_type), POINTER                      :: subsys, subsys_respa
    2720              :       TYPE(distribution_1d_type), POINTER                :: local_molecules, local_particles
    2721              :       TYPE(force_env_type), POINTER                      :: force_env
    2722              :       TYPE(global_constraint_type), POINTER              :: gci
    2723              :       TYPE(molecule_kind_list_type), POINTER             :: molecule_kinds
    2724           14 :       TYPE(molecule_kind_type), DIMENSION(:), POINTER    :: molecule_kind_set
    2725              :       TYPE(molecule_list_type), POINTER                  :: molecules
    2726           14 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
    2727              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    2728              :       TYPE(particle_list_type), POINTER                  :: particles, particles_respa
    2729           14 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set, particle_set_respa
    2730              :       TYPE(simpar_type), POINTER                         :: simpar
    2731              : 
    2732           14 :       NULLIFY (para_env, cell, subsys_respa, particles_respa, particle_set_respa, gci, force_env, atomic_kinds)
    2733           14 :       NULLIFY (atomic_kind_set, simpar, subsys, particles, particle_set)
    2734           14 :       NULLIFY (local_molecules, molecule_kinds, molecules, molecule_kind_set, local_particles, itimes)
    2735              :       CALL get_md_env(md_env=md_env, simpar=simpar, force_env=force_env, &
    2736           14 :                       para_env=para_env, itimes=itimes)
    2737           14 :       dt = simpar%dt
    2738              : 
    2739           14 :       n_time_steps = simpar%n_time_steps
    2740           14 :       inner_dt = dt/n_time_steps
    2741              : 
    2742           14 :       CALL force_env_get(force_env, subsys=subsys, cell=cell)
    2743           14 :       CALL force_env_get(force_env%sub_force_env(1)%force_env, subsys=subsys_respa)
    2744              : 
    2745              :       ! Do some checks on coordinates and box
    2746           14 :       CALL apply_qmmm_walls_reflective(force_env)
    2747              : 
    2748              :       CALL cp_subsys_get(subsys=subsys, atomic_kinds=atomic_kinds, local_particles=local_particles, &
    2749              :                          particles=particles, local_molecules=local_molecules, molecules=molecules, &
    2750           14 :                          gci=gci, molecule_kinds=molecule_kinds)
    2751              : 
    2752           14 :       CALL cp_subsys_get(subsys=subsys_respa, particles=particles_respa)
    2753           14 :       particle_set_respa => particles_respa%els
    2754              : 
    2755           14 :       nparticle_kind = atomic_kinds%n_els
    2756           14 :       atomic_kind_set => atomic_kinds%els
    2757           14 :       molecule_kind_set => molecule_kinds%els
    2758              : 
    2759           14 :       nparticle = particles%n_els
    2760           14 :       particle_set => particles%els
    2761           14 :       molecule_set => molecules%els
    2762              : 
    2763              :       ! Allocate work storage for positions and velocities
    2764           42 :       ALLOCATE (pos(3, nparticle))
    2765           28 :       ALLOCATE (vel(3, nparticle))
    2766           14 :       vel(:, :) = 0.0_dp
    2767              : 
    2768           14 :       constraint_dt = dt
    2769           14 :       IF (simpar%constraint) THEN
    2770            0 :          IF (gci%joint_colvar_checks) THEN
    2771            0 :             constraint_dt = inner_dt
    2772              :          ELSE
    2773              :             CALL getold(gci, local_molecules, molecule_set, &
    2774            0 :                         molecule_kind_set, particle_set, cell)
    2775              :          END IF
    2776              :       END IF
    2777              : 
    2778              :       ! Multiple time step (first part)
    2779           58 :       DO iparticle_kind = 1, nparticle_kind
    2780           44 :          atomic_kind => atomic_kind_set(iparticle_kind)
    2781           44 :          CALL get_atomic_kind(atomic_kind=atomic_kind, mass=mass)
    2782           44 :          dm = 0.5_dp*dt/mass
    2783           44 :          nparticle_local = local_particles%n_el(iparticle_kind)
    2784         2755 :          DO iparticle_local = 1, nparticle_local
    2785         2697 :             iparticle = local_particles%list(iparticle_kind)%array(iparticle_local)
    2786              :             vel(:, iparticle) = particle_set(iparticle)%v(:) + &
    2787              :                                 dm*(particle_set(iparticle)%f(:) - &
    2788        10832 :                                     particle_set_respa(iparticle)%f(:))
    2789              :          END DO
    2790              :       END DO
    2791              : 
    2792              :       ! Velocity Verlet (first part)
    2793           84 :       DO i_step = 1, n_time_steps
    2794           70 :          IF (gci%joint_colvar_checks) CALL getold(gci, local_molecules, molecule_set, &
    2795            0 :                                                   molecule_kind_set, particle_set, cell)
    2796           70 :          pos(:, :) = 0.0_dp
    2797          290 :          DO iparticle_kind = 1, nparticle_kind
    2798          220 :             atomic_kind => atomic_kind_set(iparticle_kind)
    2799          220 :             CALL get_atomic_kind(atomic_kind=atomic_kind, mass=mass)
    2800          220 :             dm = 0.5_dp*dt/(n_time_steps*mass)
    2801          220 :             nparticle_local = local_particles%n_el(iparticle_kind)
    2802        13775 :             DO iparticle_local = 1, nparticle_local
    2803        13485 :                iparticle = local_particles%list(iparticle_kind)%array(iparticle_local)
    2804              :                vel(:, iparticle) = vel(:, iparticle) + &
    2805        53940 :                                    dm*particle_set_respa(iparticle)%f(:)
    2806              :                pos(:, iparticle) = particle_set(iparticle)%r(:) + &
    2807        54160 :                                    (dt/n_time_steps)*vel(:, iparticle)
    2808              :             END DO
    2809              :          END DO
    2810              : 
    2811           70 :          IF (simpar%constraint) THEN
    2812              :             ! Possibly update the target values
    2813              :             CALL shake_update_targets(gci, local_molecules, molecule_set, &
    2814            0 :                                       molecule_kind_set, inner_dt, force_env%root_section)
    2815              : 
    2816              :             CALL shake_control(gci, local_molecules, molecule_set, &
    2817              :                                molecule_kind_set, particle_set, pos, vel, constraint_dt, simpar%shake_tol, &
    2818              :                                simpar%info_constraint, simpar%lagrange_multipliers, simpar%dump_lm, cell, &
    2819            0 :                                para_env, local_particles)
    2820              :          END IF
    2821              : 
    2822              :          ! Broadcast the new particle positions
    2823           70 :          CALL update_particle_set(particle_set, para_env, pos=pos)
    2824        27040 :          DO iparticle = 1, SIZE(particle_set)
    2825       215830 :             particle_set_respa(iparticle)%r = particle_set(iparticle)%r
    2826              :          END DO
    2827              : 
    2828              :          ! Update forces
    2829           70 :          CALL force_env_calc_energy_force(force_env%sub_force_env(1)%force_env)
    2830              : 
    2831              :          ! Metadynamics
    2832           70 :          CALL metadyn_integrator(force_env, itimes, vel)
    2833              : 
    2834              :          ! Velocity Verlet (second part)
    2835          290 :          DO iparticle_kind = 1, nparticle_kind
    2836          220 :             atomic_kind => atomic_kind_set(iparticle_kind)
    2837          220 :             CALL get_atomic_kind(atomic_kind=atomic_kind, mass=mass)
    2838          220 :             dm = 0.5_dp*dt/(n_time_steps*mass)
    2839          220 :             nparticle_local = local_particles%n_el(iparticle_kind)
    2840        13775 :             DO iparticle_local = 1, nparticle_local
    2841        13485 :                iparticle = local_particles%list(iparticle_kind)%array(iparticle_local)
    2842        13485 :                vel(1, iparticle) = vel(1, iparticle) + dm*particle_set_respa(iparticle)%f(1)
    2843        13485 :                vel(2, iparticle) = vel(2, iparticle) + dm*particle_set_respa(iparticle)%f(2)
    2844        13705 :                vel(3, iparticle) = vel(3, iparticle) + dm*particle_set_respa(iparticle)%f(3)
    2845              :             END DO
    2846              :          END DO
    2847              : 
    2848           70 :          IF (simpar%constraint) CALL rattle_control(gci, local_molecules, molecule_set, &
    2849              :                                                     molecule_kind_set, particle_set, vel, constraint_dt, simpar%shake_tol, &
    2850              :                                                     simpar%info_constraint, simpar%lagrange_multipliers, &
    2851            0 :                                                     simpar%dump_lm, cell, para_env, local_particles)
    2852              : 
    2853           84 :          IF (simpar%annealing) vel(:, :) = vel(:, :)*simpar%f_annealing
    2854              :       END DO
    2855           14 :       DEALLOCATE (pos)
    2856              : 
    2857              :       ! Multiple time step (second part)
    2858              :       ! Compute forces for respa force_env
    2859           14 :       CALL force_env_calc_energy_force(force_env)
    2860              : 
    2861              :       ! Metadynamics
    2862           14 :       CALL metadyn_integrator(force_env, itimes, vel)
    2863              : 
    2864           58 :       DO iparticle_kind = 1, nparticle_kind
    2865           44 :          atomic_kind => atomic_kind_set(iparticle_kind)
    2866           44 :          CALL get_atomic_kind(atomic_kind=atomic_kind, mass=mass)
    2867           44 :          dm = 0.5_dp*dt/mass
    2868           44 :          nparticle_local = local_particles%n_el(iparticle_kind)
    2869         2755 :          DO iparticle_local = 1, nparticle_local
    2870         2697 :             iparticle = local_particles%list(iparticle_kind)%array(iparticle_local)
    2871         2697 :             vel(1, iparticle) = vel(1, iparticle) + dm*(particle_set(iparticle)%f(1) - particle_set_respa(iparticle)%f(1))
    2872         2697 :             vel(2, iparticle) = vel(2, iparticle) + dm*(particle_set(iparticle)%f(2) - particle_set_respa(iparticle)%f(2))
    2873         2741 :             vel(3, iparticle) = vel(3, iparticle) + dm*(particle_set(iparticle)%f(3) - particle_set_respa(iparticle)%f(3))
    2874              :          END DO
    2875              :       END DO
    2876              : 
    2877           14 :       IF (gci%joint_colvar_checks) CALL rattle_control(gci, local_molecules, molecule_set, &
    2878              :                                                        molecule_kind_set, particle_set, vel, dt, simpar%shake_tol, &
    2879              :                                                        simpar%info_constraint, simpar%lagrange_multipliers, &
    2880            0 :                                                        simpar%dump_lm, cell, para_env, local_particles)
    2881              : 
    2882              :       ! Broadcast the new particle velocities
    2883           14 :       CALL update_particle_set(particle_set, para_env, vel=vel)
    2884              : 
    2885           14 :       DEALLOCATE (vel)
    2886              : 
    2887           14 :    END SUBROUTINE nve_respa
    2888              : 
    2889              : END MODULE integrator
        

Generated by: LCOV version 2.0-1