LCOV - code coverage report
Current view: top level - src/motion - input_cp2k_restarts.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 90.7 % 1351 1225
Test Date: 2026-11-24 01:22:08 Functions: 90.5 % 21 19

            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 Set of routines to dump the restart file of CP2K
      10              : !> \par History
      11              : !>      01.2006 [created] Teodoro Laino
      12              : ! **************************************************************************************************
      13              : MODULE input_cp2k_restarts
      14              : 
      15              :    USE al_system_types,                 ONLY: al_system_type
      16              :    USE atomic_kind_list_types,          ONLY: atomic_kind_list_type
      17              :    USE averages_types,                  ONLY: average_quantities_type
      18              :    USE cp2k_info,                       ONLY: write_restart_header
      19              :    USE cp_linked_list_input,            ONLY: cp_sll_val_create,&
      20              :                                               cp_sll_val_get_length,&
      21              :                                               cp_sll_val_type
      22              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      23              :                                               cp_logger_get_default_io_unit,&
      24              :                                               cp_logger_type,&
      25              :                                               cp_to_string
      26              :    USE cp_output_handling,              ONLY: cp_p_file,&
      27              :                                               cp_print_key_finished_output,&
      28              :                                               cp_print_key_should_output,&
      29              :                                               cp_print_key_unit_nr
      30              :    USE cp_subsys_types,                 ONLY: cp_subsys_get,&
      31              :                                               cp_subsys_type
      32              :    USE csvr_system_types,               ONLY: csvr_system_type
      33              :    USE extended_system_types,           ONLY: lnhc_parameters_type,&
      34              :                                               map_info_type,&
      35              :                                               npt_info_type
      36              :    USE force_env_types,                 ONLY: force_env_get,&
      37              :                                               force_env_type,&
      38              :                                               multiple_fe_list
      39              :    USE gle_system_types,                ONLY: gle_type
      40              :    USE helium_types,                    ONLY: helium_solvent_p_type
      41              :    USE input_constants,                 ONLY: &
      42              :         do_band_collective, do_thermo_al, do_thermo_csvr, do_thermo_gle, &
      43              :         do_thermo_no_communication, do_thermo_nose, mol_dyn_run, mon_car_run, pint_run
      44              :    USE input_restart_force_eval,        ONLY: update_force_eval
      45              :    USE input_restart_rng,               ONLY: section_rng_val_set
      46              :    USE input_section_types,             ONLY: &
      47              :         section_get_keyword_index, section_type, section_vals_add_values, section_vals_get, &
      48              :         section_vals_get_subs_vals, section_vals_get_subs_vals3, section_vals_remove_values, &
      49              :         section_vals_type, section_vals_val_get, section_vals_val_set, section_vals_val_unset, &
      50              :         section_vals_write
      51              :    USE input_val_types,                 ONLY: val_create,&
      52              :                                               val_release,&
      53              :                                               val_type
      54              :    USE kinds,                           ONLY: default_path_length,&
      55              :                                               default_string_length,&
      56              :                                               dp,&
      57              :                                               dp_size,&
      58              :                                               int_size
      59              :    USE md_environment_types,            ONLY: get_md_env,&
      60              :                                               md_environment_type
      61              :    USE memory_utilities,                ONLY: reallocate
      62              :    USE message_passing,                 ONLY: mp_para_env_type
      63              :    USE metadynamics_types,              ONLY: meta_env_type
      64              :    USE molecule_kind_list_types,        ONLY: molecule_kind_list_type
      65              :    USE molecule_list_types,             ONLY: molecule_list_type
      66              :    USE neb_types,                       ONLY: neb_var_type
      67              :    USE parallel_rng_types,              ONLY: rng_record_length
      68              :    USE particle_list_types,             ONLY: particle_list_type
      69              :    USE particle_types,                  ONLY: get_particle_pos_or_vel,&
      70              :                                               particle_type
      71              :    USE physcon,                         ONLY: angstrom
      72              :    USE pint_transformations,            ONLY: pint_u2x
      73              :    USE pint_types,                      ONLY: pint_env_type,&
      74              :                                               thermostat_gle,&
      75              :                                               thermostat_nose,&
      76              :                                               thermostat_piglet,&
      77              :                                               thermostat_pile,&
      78              :                                               thermostat_qtb
      79              :    USE simpar_types,                    ONLY: simpar_type
      80              :    USE string_utilities,                ONLY: string_to_ascii
      81              :    USE thermostat_types,                ONLY: thermostat_type
      82              :    USE thermostat_utils,                ONLY: communication_thermo_low2,&
      83              :                                               get_kin_energies
      84              : #include "../base/base_uses.f90"
      85              : 
      86              :    IMPLICIT NONE
      87              : 
      88              :    PRIVATE
      89              : 
      90              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'input_cp2k_restarts'
      91              : 
      92              :    PUBLIC :: write_restart
      93              : 
      94              : CONTAINS
      95              : 
      96              : ! **************************************************************************************************
      97              : !> \brief checks if a restart needs to be written and does so, updating all necessary fields
      98              : !>      in the input file. This is a relatively simple wrapper routine.
      99              : !> \param md_env ...
     100              : !> \param force_env ...
     101              : !> \param root_section ...
     102              : !> \param coords ...
     103              : !> \param vels ...
     104              : !> \param pint_env ...
     105              : !> \param helium_env ...
     106              : !> \par History
     107              : !>      03.2006 created [Joost VandeVondele]
     108              : !> \author Joost VandeVondele
     109              : ! **************************************************************************************************
     110       103406 :    SUBROUTINE write_restart(md_env, force_env, root_section, &
     111              :                             coords, vels, pint_env, helium_env)
     112              :       TYPE(md_environment_type), OPTIONAL, POINTER       :: md_env
     113              :       TYPE(force_env_type), OPTIONAL, POINTER            :: force_env
     114              :       TYPE(section_vals_type), POINTER                   :: root_section
     115              :       TYPE(neb_var_type), OPTIONAL, POINTER              :: coords, vels
     116              :       TYPE(pint_env_type), INTENT(IN), OPTIONAL          :: pint_env
     117              :       TYPE(helium_solvent_p_type), DIMENSION(:), &
     118              :          OPTIONAL, POINTER                               :: helium_env
     119              : 
     120              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'write_restart'
     121              :       CHARACTER(LEN=30), DIMENSION(2), PARAMETER :: &
     122              :          keys = ["PRINT%RESTART_HISTORY", "PRINT%RESTART        "]
     123              : 
     124              :       INTEGER                                            :: handle, ikey, ires, log_unit, nforce_eval
     125              :       LOGICAL                                            :: save_mem, write_binary_restart_file
     126              :       TYPE(cp_logger_type), POINTER                      :: logger
     127              :       TYPE(section_vals_type), POINTER                   :: global_section, motion_section, sections
     128              : 
     129        51703 :       CALL timeset(routineN, handle)
     130              : 
     131        51703 :       logger => cp_get_default_logger()
     132        51703 :       motion_section => section_vals_get_subs_vals(root_section, "MOTION")
     133              : 
     134        51703 :       NULLIFY (global_section)
     135        51703 :       global_section => section_vals_get_subs_vals(root_section, "GLOBAL")
     136        51703 :       CALL section_vals_val_get(global_section, "SAVE_MEM", l_val=save_mem)
     137              : 
     138              :       IF (BTEST(cp_print_key_should_output(logger%iter_info, &
     139        51703 :                                            motion_section, keys(1)), cp_p_file) .OR. &
     140              :           BTEST(cp_print_key_should_output(logger%iter_info, &
     141              :                                            motion_section, keys(2)), cp_p_file)) THEN
     142              : 
     143        15065 :          sections => section_vals_get_subs_vals(root_section, "FORCE_EVAL")
     144        15065 :          CALL section_vals_get(sections, n_repetition=nforce_eval)
     145              :          CALL section_vals_val_get(motion_section, "PRINT%RESTART%SPLIT_RESTART_FILE", &
     146        15065 :                                    l_val=write_binary_restart_file)
     147              : 
     148        15065 :          IF (write_binary_restart_file) THEN
     149          136 :             CALL update_subsys_release(md_env, force_env, root_section)
     150          136 :             CALL update_motion_release(motion_section)
     151          408 :             DO ikey = 1, SIZE(keys)
     152          272 :                log_unit = cp_logger_get_default_io_unit(logger)
     153          272 :                IF (BTEST(cp_print_key_should_output(logger%iter_info, &
     154          136 :                                                     motion_section, keys(ikey)), cp_p_file)) THEN
     155              :                   ires = cp_print_key_unit_nr(logger, motion_section, TRIM(keys(ikey)), &
     156              :                                               extension=".restart.bin", &
     157              :                                               file_action="READWRITE", &
     158              :                                               file_form="UNFORMATTED", &
     159              :                                               file_position="REWIND", &
     160              :                                               file_status="UNKNOWN", &
     161          272 :                                               do_backup=(ikey == 2))
     162          272 :                   CALL write_binary_restart(ires, log_unit, root_section, md_env, force_env)
     163              :                   CALL cp_print_key_finished_output(ires, logger, motion_section, &
     164          272 :                                                     TRIM(keys(ikey)))
     165              :                END IF
     166              :             END DO
     167              :          END IF
     168              : 
     169              :          CALL update_input(md_env, force_env, root_section, coords, vels, pint_env, helium_env, &
     170              :                            save_mem=save_mem, &
     171        15065 :                            write_binary_restart_file=write_binary_restart_file)
     172              : 
     173        45195 :          DO ikey = 1, SIZE(keys)
     174        30130 :             IF (BTEST(cp_print_key_should_output(logger%iter_info, &
     175        15065 :                                                  motion_section, keys(ikey)), cp_p_file)) THEN
     176              :                ires = cp_print_key_unit_nr(logger, motion_section, TRIM(keys(ikey)), &
     177              :                                            extension=".restart", &
     178              :                                            file_position="REWIND", &
     179        16605 :                                            do_backup=(ikey == 2))
     180        16605 :                IF (ires > 0) THEN
     181         8660 :                   CALL write_restart_header(ires)
     182         8660 :                   CALL section_vals_write(root_section, unit_nr=ires, hide_root=.TRUE.)
     183              :                END IF
     184        16605 :                CALL cp_print_key_finished_output(ires, logger, motion_section, TRIM(keys(ikey)))
     185              :             END IF
     186              :          END DO
     187              : 
     188        15065 :          IF (save_mem) THEN
     189           82 :             CALL update_subsys_release(md_env, force_env, root_section)
     190           82 :             CALL update_motion_release(motion_section)
     191              :          END IF
     192              : 
     193              :       END IF
     194              : 
     195        51703 :       CALL timestop(handle)
     196              : 
     197        51703 :    END SUBROUTINE write_restart
     198              : 
     199              : ! **************************************************************************************************
     200              : !> \brief deallocate some sub_sections of the section subsys to save some memory
     201              : !> \param md_env ...
     202              : !> \param force_env ...
     203              : !> \param root_section ...
     204              : !> \par History
     205              : !>      06.2007 created [MI]
     206              : !> \author MI
     207              : ! **************************************************************************************************
     208          218 :    SUBROUTINE update_subsys_release(md_env, force_env, root_section)
     209              : 
     210              :       TYPE(md_environment_type), OPTIONAL, POINTER       :: md_env
     211              :       TYPE(force_env_type), OPTIONAL, POINTER            :: force_env
     212              :       TYPE(section_vals_type), POINTER                   :: root_section
     213              : 
     214              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'update_subsys_release'
     215              : 
     216              :       CHARACTER(LEN=default_string_length)               :: unit_str
     217              :       INTEGER                                            :: handle, iforce_eval, myid, nforce_eval
     218          218 :       INTEGER, DIMENSION(:), POINTER                     :: i_force_eval
     219              :       LOGICAL                                            :: explicit, scale, skip_vel_section
     220              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     221              :       TYPE(force_env_type), POINTER                      :: my_force_b, my_force_env
     222              :       TYPE(particle_list_type), POINTER                  :: core_particles, particles, &
     223              :                                                             shell_particles
     224              :       TYPE(section_vals_type), POINTER                   :: force_env_sections, subsys_section, &
     225              :                                                             work_section
     226              : 
     227          218 :       CALL timeset(routineN, handle)
     228              : 
     229          218 :       NULLIFY (core_particles, my_force_env, my_force_b, particles, &
     230          218 :                shell_particles, subsys, work_section)
     231              : 
     232          218 :       IF (PRESENT(md_env)) THEN
     233          148 :          CALL get_md_env(md_env=md_env, force_env=my_force_env)
     234           70 :       ELSE IF (PRESENT(force_env)) THEN
     235           70 :          my_force_env => force_env
     236              :       END IF
     237              : 
     238          218 :       IF (ASSOCIATED(my_force_env)) THEN
     239          218 :          NULLIFY (subsys_section)
     240          218 :          CALL section_vals_val_get(root_section, "GLOBAL%RUN_TYPE", i_val=myid)
     241              :          skip_vel_section = ( &
     242              :                             (myid /= mol_dyn_run) .AND. &
     243              :                             (myid /= mon_car_run) .AND. &
     244          218 :                             (myid /= pint_run))
     245              : 
     246          218 :          force_env_sections => section_vals_get_subs_vals(root_section, "FORCE_EVAL")
     247          218 :          CALL multiple_fe_list(force_env_sections, root_section, i_force_eval, nforce_eval)
     248              : 
     249          436 :          DO iforce_eval = 1, nforce_eval
     250              :             subsys_section => section_vals_get_subs_vals3(force_env_sections, "SUBSYS", &
     251          218 :                                                           i_rep_section=i_force_eval(iforce_eval))
     252          218 :             CALL section_vals_get(subsys_section, explicit=explicit)
     253          218 :             IF (.NOT. explicit) CYCLE ! Nothing to update...
     254              : 
     255          218 :             my_force_b => my_force_env
     256          218 :             IF (iforce_eval > 1) my_force_b => my_force_env%sub_force_env(iforce_eval - 1)%force_env
     257              : 
     258          218 :             CALL force_env_get(my_force_b, subsys=subsys)
     259              : 
     260              :             CALL cp_subsys_get(subsys, particles=particles, shell_particles=shell_particles, &
     261          218 :                                core_particles=core_particles)
     262              : 
     263          218 :             work_section => section_vals_get_subs_vals(subsys_section, "COORD")
     264          218 :             CALL section_vals_get(work_section, explicit=explicit)
     265          218 :             IF (explicit) THEN
     266          218 :                CALL section_vals_val_get(work_section, "UNIT", c_val=unit_str)
     267          218 :                CALL section_vals_val_get(work_section, "SCALED", l_val=scale)
     268              :             END IF
     269          218 :             CALL section_vals_remove_values(work_section)
     270          218 :             IF (explicit) THEN
     271          218 :                CALL section_vals_val_set(work_section, "UNIT", c_val=unit_str)
     272          218 :                CALL section_vals_val_set(work_section, "SCALED", l_val=scale)
     273              :             END IF
     274              : 
     275          218 :             work_section => section_vals_get_subs_vals(subsys_section, "VELOCITY")
     276          218 :             IF (.NOT. skip_vel_section) THEN
     277          148 :                CALL section_vals_remove_values(work_section)
     278              :             END IF
     279              : 
     280          218 :             IF (ASSOCIATED(shell_particles)) THEN
     281           68 :                work_section => section_vals_get_subs_vals(subsys_section, "SHELL_COORD")
     282           68 :                CALL section_vals_get(work_section, explicit=explicit)
     283           68 :                IF (explicit) THEN
     284           20 :                   CALL section_vals_val_get(work_section, "UNIT", c_val=unit_str)
     285           20 :                   CALL section_vals_val_get(work_section, "SCALED", l_val=scale)
     286              :                END IF
     287           68 :                CALL section_vals_remove_values(work_section)
     288           68 :                IF (explicit) THEN
     289           20 :                   CALL section_vals_val_set(work_section, "UNIT", c_val=unit_str)
     290           20 :                   CALL section_vals_val_set(work_section, "SCALED", l_val=scale)
     291              :                END IF
     292              : 
     293           68 :                work_section => section_vals_get_subs_vals(subsys_section, "SHELL_VELOCITY")
     294           68 :                IF (.NOT. skip_vel_section) THEN
     295           68 :                   CALL section_vals_remove_values(work_section)
     296              :                END IF
     297              :             END IF
     298              : 
     299          872 :             IF (ASSOCIATED(core_particles)) THEN
     300           68 :                work_section => section_vals_get_subs_vals(subsys_section, "CORE_COORD")
     301           68 :                CALL section_vals_get(work_section, explicit=explicit)
     302           68 :                IF (explicit) THEN
     303           20 :                   CALL section_vals_val_get(work_section, "UNIT", c_val=unit_str)
     304           20 :                   CALL section_vals_val_get(work_section, "SCALED", l_val=scale)
     305              :                END IF
     306           68 :                CALL section_vals_remove_values(work_section)
     307           68 :                IF (explicit) THEN
     308           20 :                   CALL section_vals_val_set(work_section, "UNIT", c_val=unit_str)
     309           20 :                   CALL section_vals_val_set(work_section, "SCALED", l_val=scale)
     310              :                END IF
     311              : 
     312           68 :                work_section => section_vals_get_subs_vals(subsys_section, "CORE_VELOCITY")
     313           68 :                IF (.NOT. skip_vel_section) THEN
     314           68 :                   CALL section_vals_remove_values(work_section)
     315              :                END IF
     316              :             END IF
     317              : 
     318              :          END DO
     319              : 
     320          218 :          DEALLOCATE (i_force_eval)
     321              : 
     322              :       END IF
     323              : 
     324          218 :       CALL timestop(handle)
     325              : 
     326          218 :    END SUBROUTINE update_subsys_release
     327              : 
     328              : ! **************************************************************************************************
     329              : !> \brief deallocate the nose subsections (coord, vel, force, mass) in the md section
     330              : !> \param motion_section ...
     331              : !> \par History
     332              : !>      08.2007 created [MI]
     333              : !> \author MI
     334              : ! **************************************************************************************************
     335          218 :    SUBROUTINE update_motion_release(motion_section)
     336              : 
     337              :       TYPE(section_vals_type), POINTER                   :: motion_section
     338              : 
     339              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'update_motion_release'
     340              : 
     341              :       INTEGER                                            :: handle
     342              :       TYPE(section_vals_type), POINTER                   :: work_section
     343              : 
     344          218 :       CALL timeset(routineN, handle)
     345              : 
     346          218 :       NULLIFY (work_section)
     347              : 
     348          218 :       work_section => section_vals_get_subs_vals(motion_section, "MD%AVERAGES%RESTART_AVERAGES")
     349          218 :       CALL section_vals_remove_values(work_section)
     350              : 
     351          218 :       work_section => section_vals_get_subs_vals(motion_section, "MD%THERMOSTAT%NOSE%COORD")
     352          218 :       CALL section_vals_remove_values(work_section)
     353          218 :       work_section => section_vals_get_subs_vals(motion_section, "MD%THERMOSTAT%NOSE%VELOCITY")
     354          218 :       CALL section_vals_remove_values(work_section)
     355          218 :       work_section => section_vals_get_subs_vals(motion_section, "MD%THERMOSTAT%NOSE%MASS")
     356          218 :       CALL section_vals_remove_values(work_section)
     357          218 :       work_section => section_vals_get_subs_vals(motion_section, "MD%THERMOSTAT%NOSE%FORCE")
     358          218 :       CALL section_vals_remove_values(work_section)
     359              : 
     360          218 :       work_section => section_vals_get_subs_vals(motion_section, "MD%BAROSTAT%THERMOSTAT%NOSE%COORD")
     361          218 :       CALL section_vals_remove_values(work_section)
     362          218 :       work_section => section_vals_get_subs_vals(motion_section, "MD%BAROSTAT%THERMOSTAT%NOSE%VELOCITY")
     363          218 :       CALL section_vals_remove_values(work_section)
     364          218 :       work_section => section_vals_get_subs_vals(motion_section, "MD%BAROSTAT%THERMOSTAT%NOSE%MASS")
     365          218 :       CALL section_vals_remove_values(work_section)
     366          218 :       work_section => section_vals_get_subs_vals(motion_section, "MD%BAROSTAT%THERMOSTAT%NOSE%FORCE")
     367          218 :       CALL section_vals_remove_values(work_section)
     368              : 
     369          218 :       work_section => section_vals_get_subs_vals(motion_section, "MD%SHELL%THERMOSTAT%NOSE%COORD")
     370          218 :       CALL section_vals_remove_values(work_section)
     371          218 :       work_section => section_vals_get_subs_vals(motion_section, "MD%SHELL%THERMOSTAT%NOSE%VELOCITY")
     372          218 :       CALL section_vals_remove_values(work_section)
     373          218 :       work_section => section_vals_get_subs_vals(motion_section, "MD%SHELL%THERMOSTAT%NOSE%MASS")
     374          218 :       CALL section_vals_remove_values(work_section)
     375          218 :       work_section => section_vals_get_subs_vals(motion_section, "MD%SHELL%THERMOSTAT%NOSE%FORCE")
     376          218 :       CALL section_vals_remove_values(work_section)
     377              : 
     378          218 :       CALL timestop(handle)
     379              : 
     380          218 :    END SUBROUTINE update_motion_release
     381              : 
     382              : ! **************************************************************************************************
     383              : !> \brief Updates the whole input file for the restart
     384              : !> \param md_env ...
     385              : !> \param force_env ...
     386              : !> \param root_section ...
     387              : !> \param coords ...
     388              : !> \param vels ...
     389              : !> \param pint_env ...
     390              : !> \param helium_env ...
     391              : !> \param save_mem ...
     392              : !> \param write_binary_restart_file ...
     393              : !> \par History
     394              : !>      01.2006 created [teo]
     395              : !>      2016-07-14 Modified to work with independent helium_env [cschran]
     396              : !> \author Teodoro Laino
     397              : ! **************************************************************************************************
     398        15065 :    SUBROUTINE update_input(md_env, force_env, root_section, coords, vels, pint_env, &
     399              :                            helium_env, save_mem, write_binary_restart_file)
     400              : 
     401              :       TYPE(md_environment_type), OPTIONAL, POINTER       :: md_env
     402              :       TYPE(force_env_type), OPTIONAL, POINTER            :: force_env
     403              :       TYPE(section_vals_type), POINTER                   :: root_section
     404              :       TYPE(neb_var_type), OPTIONAL, POINTER              :: coords, vels
     405              :       TYPE(pint_env_type), INTENT(IN), OPTIONAL          :: pint_env
     406              :       TYPE(helium_solvent_p_type), DIMENSION(:), &
     407              :          OPTIONAL, POINTER                               :: helium_env
     408              :       LOGICAL, INTENT(IN), OPTIONAL                      :: save_mem, write_binary_restart_file
     409              : 
     410              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'update_input'
     411              : 
     412              :       INTEGER                                            :: handle
     413              :       LOGICAL                                            :: do_respa, lcond, my_save_mem, &
     414              :                                                             my_write_binary_restart_file
     415              :       TYPE(cp_logger_type), POINTER                      :: logger
     416              :       TYPE(force_env_type), POINTER                      :: my_force_env
     417              :       TYPE(section_vals_type), POINTER                   :: motion_section
     418              :       TYPE(simpar_type), POINTER                         :: simpar
     419              : 
     420        15065 :       CALL timeset(routineN, handle)
     421              : 
     422        15065 :       NULLIFY (logger, motion_section, my_force_env)
     423              : 
     424              :       IF (PRESENT(save_mem)) THEN
     425              :          my_save_mem = save_mem
     426              :       ELSE
     427              :          my_save_mem = .FALSE.
     428              :       END IF
     429              : 
     430        15065 :       IF (PRESENT(write_binary_restart_file)) THEN
     431        15065 :          my_write_binary_restart_file = write_binary_restart_file
     432              :       ELSE
     433            0 :          my_write_binary_restart_file = .FALSE.
     434              :       END IF
     435              : 
     436        15065 :       logger => cp_get_default_logger()
     437              : 
     438              :       ! Can handle md_env or force_env
     439        15065 :       lcond = PRESENT(md_env) .OR. PRESENT(force_env) .OR. PRESENT(pint_env) .OR. PRESENT(helium_env)
     440              :       IF (lcond) THEN
     441        14919 :          IF (PRESENT(md_env)) THEN
     442         5566 :             CALL get_md_env(md_env=md_env, force_env=my_force_env)
     443         9353 :          ELSE IF (PRESENT(force_env)) THEN
     444         8715 :             my_force_env => force_env
     445              :          END IF
     446              :          ! The real restart setting...
     447        14919 :          motion_section => section_vals_get_subs_vals(root_section, "MOTION")
     448              :          CALL update_motion(motion_section, &
     449              :                             md_env=md_env, &
     450              :                             force_env=my_force_env, &
     451              :                             logger=logger, &
     452              :                             coords=coords, &
     453              :                             vels=vels, &
     454              :                             pint_env=pint_env, &
     455              :                             helium_env=helium_env, &
     456              :                             save_mem=my_save_mem, &
     457        14919 :                             write_binary_restart_file=my_write_binary_restart_file)
     458              :          ! Update one force_env_section per time..
     459        14919 :          IF (ASSOCIATED(my_force_env)) THEN
     460        14281 :             do_respa = .FALSE.
     461              :             ! Do respa only in case of RESPA MD
     462        14281 :             IF (PRESENT(md_env)) THEN
     463         5566 :                CALL get_md_env(md_env=md_env, simpar=simpar)
     464         5566 :                IF (simpar%do_respa) THEN
     465            6 :                   do_respa = .TRUE.
     466              :                END IF
     467              :             END IF
     468              : 
     469              :             CALL update_force_eval(force_env=my_force_env, &
     470              :                                    root_section=root_section, &
     471              :                                    write_binary_restart_file=my_write_binary_restart_file, &
     472        14281 :                                    respa=do_respa)
     473              : 
     474              :          END IF
     475              :       END IF
     476              : 
     477        15065 :       CALL timestop(handle)
     478              : 
     479        15065 :    END SUBROUTINE update_input
     480              : 
     481              : ! **************************************************************************************************
     482              : !> \brief Updates the motion section of the input file
     483              : !> \param motion_section ...
     484              : !> \param md_env ...
     485              : !> \param force_env ...
     486              : !> \param logger ...
     487              : !> \param coords ...
     488              : !> \param vels ...
     489              : !> \param pint_env ...
     490              : !> \param helium_env ...
     491              : !> \param save_mem ...
     492              : !> \param write_binary_restart_file ...
     493              : !> \par History
     494              : !>      01.2006 created [teo]
     495              : !>      2016-07-14 Modified to work with independent helium_env [cschran]
     496              : !> \author Teodoro Laino
     497              : ! **************************************************************************************************
     498       134271 :    SUBROUTINE update_motion(motion_section, md_env, force_env, logger, &
     499              :                             coords, vels, pint_env, helium_env, save_mem, &
     500              :                             write_binary_restart_file)
     501              : 
     502              :       TYPE(section_vals_type), POINTER                   :: motion_section
     503              :       TYPE(md_environment_type), OPTIONAL, POINTER       :: md_env
     504              :       TYPE(force_env_type), POINTER                      :: force_env
     505              :       TYPE(cp_logger_type), POINTER                      :: logger
     506              :       TYPE(neb_var_type), OPTIONAL, POINTER              :: coords, vels
     507              :       TYPE(pint_env_type), INTENT(IN), OPTIONAL          :: pint_env
     508              :       TYPE(helium_solvent_p_type), DIMENSION(:), &
     509              :          OPTIONAL, POINTER                               :: helium_env
     510              :       LOGICAL, INTENT(IN), OPTIONAL                      :: save_mem, write_binary_restart_file
     511              : 
     512              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'update_motion'
     513              : 
     514              :       INTEGER                                            :: counter, handle, handle2, i, irep, isec, &
     515              :                                                             j, nhc_len, tot_nhcneed
     516        14919 :       INTEGER, DIMENSION(:), POINTER                     :: walkers_status
     517              :       INTEGER, POINTER                                   :: itimes
     518              :       LOGICAL                                            :: my_save_mem, my_write_binary_restart_file
     519        14919 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: buffer, eta, fnhc, mnhc, veta, wrk
     520              :       REAL(KIND=dp), POINTER                             :: constant, t
     521              :       TYPE(average_quantities_type), POINTER             :: averages
     522              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     523              :       TYPE(lnhc_parameters_type), POINTER                :: nhc
     524              :       TYPE(meta_env_type), POINTER                       :: meta_env
     525              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     526        14919 :       TYPE(npt_info_type), POINTER                       :: npt(:, :)
     527              :       TYPE(particle_list_type), POINTER                  :: particles
     528              :       TYPE(section_vals_type), POINTER                   :: replica_section, work_section
     529              :       TYPE(simpar_type), POINTER                         :: simpar
     530              :       TYPE(thermostat_type), POINTER                     :: thermostat_baro, thermostat_part, &
     531              :                                                             thermostat_shell
     532              : 
     533        14919 :       CALL timeset(routineN, handle)
     534        14919 :       NULLIFY (logger, thermostat_part, thermostat_baro, npt, para_env, nhc, &
     535        14919 :                work_section, thermostat_shell, t, averages, constant, &
     536        14919 :                walkers_status, itimes, meta_env, simpar)
     537        14919 :       NULLIFY (particles)
     538        14919 :       NULLIFY (subsys)
     539        14919 :       IF (PRESENT(md_env)) THEN
     540              :          CALL get_md_env(md_env=md_env, &
     541              :                          thermostat_part=thermostat_part, &
     542              :                          thermostat_baro=thermostat_baro, &
     543              :                          thermostat_shell=thermostat_shell, &
     544              :                          npt=npt, &
     545              :                          t=t, &
     546              :                          constant=constant, &
     547              :                          itimes=itimes, &
     548              :                          simpar=simpar, &
     549              :                          averages=averages, &
     550         5566 :                          para_env=para_env)
     551              :       ELSE
     552         9353 :          IF (ASSOCIATED(force_env)) THEN
     553         8715 :             para_env => force_env%para_env
     554          638 :          ELSE IF (PRESENT(pint_env)) THEN
     555          600 :             para_env => pint_env%logger%para_env
     556           38 :          ELSE IF (PRESENT(helium_env)) THEN
     557              :             ! Only needed in case that pure helium is simulated
     558              :             ! In this case write_restart is called only by processors
     559              :             ! with associated helium_env
     560           38 :             para_env => helium_env(1)%helium%logger%para_env
     561              :          ELSE
     562            0 :             CPABORT("No valid para_env present")
     563              :          END IF
     564              :       END IF
     565              : 
     566        14919 :       IF (ASSOCIATED(force_env)) THEN
     567        14281 :          meta_env => force_env%meta_env
     568              :       END IF
     569              : 
     570              :       IF (PRESENT(save_mem)) THEN
     571        14919 :          my_save_mem = save_mem
     572              :       ELSE
     573        14919 :          my_save_mem = .FALSE.
     574              :       END IF
     575              : 
     576        14919 :       IF (PRESENT(write_binary_restart_file)) THEN
     577        14919 :          my_write_binary_restart_file = write_binary_restart_file
     578              :       ELSE
     579              :          my_write_binary_restart_file = .FALSE.
     580              :       END IF
     581              : 
     582        14919 :       CALL timeset(routineN//"_COUNTERS", handle2)
     583        14919 :       IF (ASSOCIATED(itimes)) THEN
     584         5566 :          IF (itimes >= 0) THEN
     585         5566 :             CALL section_vals_val_set(motion_section, "MD%STEP_START_VAL", i_val=itimes)
     586         5566 :             CPASSERT(ASSOCIATED(t))
     587         5566 :             CALL section_vals_val_set(motion_section, "MD%TIME_START_VAL", r_val=t)
     588              :          END IF
     589              :       END IF
     590        14919 :       IF (ASSOCIATED(constant)) THEN
     591         5566 :          CALL section_vals_val_set(motion_section, "MD%ECONS_START_VAL", r_val=constant)
     592              :       END IF
     593        14919 :       CALL timestop(handle2)
     594              :       ! AVERAGES
     595        14919 :       CALL timeset(routineN//"_AVERAGES", handle2)
     596        14919 :       IF (ASSOCIATED(averages)) THEN
     597         5566 :          IF ((averages%do_averages) .AND. (averages%itimes_start /= -1)) THEN
     598         5542 :             work_section => section_vals_get_subs_vals(motion_section, "MD%AVERAGES")
     599         5542 :             CALL section_vals_val_set(work_section, "_SECTION_PARAMETERS_", l_val=averages%do_averages)
     600         5542 :             work_section => section_vals_get_subs_vals(motion_section, "MD%AVERAGES%RESTART_AVERAGES")
     601         5542 :             CALL section_vals_val_set(work_section, "ITIMES_START", i_val=averages%itimes_start)
     602         5542 :             CALL section_vals_val_set(work_section, "AVECPU", r_val=averages%avecpu)
     603         5542 :             CALL section_vals_val_set(work_section, "AVEHUGONIOT", r_val=averages%avehugoniot)
     604         5542 :             CALL section_vals_val_set(work_section, "AVETEMP_BARO", r_val=averages%avetemp_baro)
     605         5542 :             CALL section_vals_val_set(work_section, "AVEPOT", r_val=averages%avepot)
     606         5542 :             CALL section_vals_val_set(work_section, "AVEKIN", r_val=averages%avekin)
     607         5542 :             CALL section_vals_val_set(work_section, "AVETEMP", r_val=averages%avetemp)
     608         5542 :             CALL section_vals_val_set(work_section, "AVEKIN_QM", r_val=averages%avekin_qm)
     609         5542 :             CALL section_vals_val_set(work_section, "AVETEMP_QM", r_val=averages%avetemp_qm)
     610         5542 :             CALL section_vals_val_set(work_section, "AVEVOL", r_val=averages%avevol)
     611         5542 :             CALL section_vals_val_set(work_section, "AVECELL_A", r_val=averages%aveca)
     612         5542 :             CALL section_vals_val_set(work_section, "AVECELL_B", r_val=averages%avecb)
     613         5542 :             CALL section_vals_val_set(work_section, "AVECELL_C", r_val=averages%avecc)
     614         5542 :             CALL section_vals_val_set(work_section, "AVEALPHA", r_val=averages%aveal)
     615         5542 :             CALL section_vals_val_set(work_section, "AVEBETA", r_val=averages%avebe)
     616         5542 :             CALL section_vals_val_set(work_section, "AVEGAMMA", r_val=averages%avega)
     617         5542 :             CALL section_vals_val_set(work_section, "AVE_ECONS", r_val=averages%econs)
     618         5542 :             CALL section_vals_val_set(work_section, "AVE_PRESS", r_val=averages%avepress)
     619         5542 :             CALL section_vals_val_set(work_section, "AVE_PXX", r_val=averages%avepxx)
     620              :             ! Virial averages
     621         5542 :             IF (ASSOCIATED(averages%virial)) THEN
     622            0 :                ALLOCATE (buffer(9))
     623            0 :                buffer = RESHAPE(averages%virial%pv_total, [9])
     624            0 :                CALL section_vals_val_set(work_section, "AVE_PV_TOT", r_vals_ptr=buffer)
     625              : 
     626            0 :                ALLOCATE (buffer(9))
     627            0 :                buffer = RESHAPE(averages%virial%pv_virial, [9])
     628            0 :                CALL section_vals_val_set(work_section, "AVE_PV_VIR", r_vals_ptr=buffer)
     629              : 
     630            0 :                ALLOCATE (buffer(9))
     631            0 :                buffer = RESHAPE(averages%virial%pv_kinetic, [9])
     632            0 :                CALL section_vals_val_set(work_section, "AVE_PV_KIN", r_vals_ptr=buffer)
     633              : 
     634            0 :                ALLOCATE (buffer(9))
     635            0 :                buffer = RESHAPE(averages%virial%pv_constraint, [9])
     636            0 :                CALL section_vals_val_set(work_section, "AVE_PV_CNSTR", r_vals_ptr=buffer)
     637              : 
     638            0 :                ALLOCATE (buffer(9))
     639            0 :                buffer = RESHAPE(averages%virial%pv_xc, [9])
     640            0 :                CALL section_vals_val_set(work_section, "AVE_PV_XC", r_vals_ptr=buffer)
     641              : 
     642            0 :                ALLOCATE (buffer(9))
     643            0 :                buffer = RESHAPE(averages%virial%pv_fock_4c, [9])
     644            0 :                CALL section_vals_val_set(work_section, "AVE_PV_FOCK_4C", r_vals_ptr=buffer)
     645              :             END IF
     646              :             ! Colvars averages
     647         5542 :             IF (SIZE(averages%avecolvar) > 0) THEN
     648            6 :                ALLOCATE (buffer(SIZE(averages%avecolvar)))
     649          196 :                buffer = averages%avecolvar
     650            2 :                CALL section_vals_val_set(work_section, "AVE_COLVARS", r_vals_ptr=buffer)
     651              :             END IF
     652         5542 :             IF (SIZE(averages%aveMmatrix) > 0) THEN
     653            6 :                ALLOCATE (buffer(SIZE(averages%aveMmatrix)))
     654         9220 :                buffer = averages%aveMmatrix
     655            2 :                CALL section_vals_val_set(work_section, "AVE_MMATRIX", r_vals_ptr=buffer)
     656              :             END IF
     657              :          END IF
     658              :       END IF
     659        14919 :       CALL timestop(handle2)
     660              : 
     661              :       ! SAVE THERMOSTAT target TEMPERATURE when doing TEMPERATURE_ANNEALING
     662        14919 :       IF (PRESENT(md_env)) THEN
     663         5566 :          IF (ASSOCIATED(simpar)) THEN
     664         5566 :             IF (simpar%temperature_annealing .AND. ABS(1._dp - simpar%f_temperature_annealing) > 1.E-10_dp) THEN
     665            4 :                CALL section_vals_val_set(motion_section, "MD%TEMPERATURE", r_val=simpar%temp_ext)
     666              :             END IF
     667              :          END IF
     668              :       END IF
     669              : 
     670              :       ! PARTICLE THERMOSTAT
     671        14919 :       CALL timeset(routineN//"_THERMOSTAT_PARTICLES", handle2)
     672        14919 :       IF (ASSOCIATED(thermostat_part)) THEN
     673         1056 :          IF (thermostat_part%type_of_thermostat == do_thermo_nose) THEN
     674              :             ! Restart of Nose-Hoover Thermostat for Particles
     675          726 :             IF (.NOT. my_write_binary_restart_file) THEN
     676          638 :                nhc => thermostat_part%nhc
     677          638 :                CALL collect_nose_restart_info(nhc, para_env, eta, veta, fnhc, mnhc)
     678          638 :                work_section => section_vals_get_subs_vals(motion_section, "MD%THERMOSTAT%NOSE")
     679          638 :                CALL set_template_restart(work_section, eta, veta, fnhc, mnhc)
     680              :             END IF
     681          330 :          ELSE IF (thermostat_part%type_of_thermostat == do_thermo_csvr) THEN
     682              :             ! Restart of CSVR Thermostat for Particles
     683          310 :             work_section => section_vals_get_subs_vals(motion_section, "MD%THERMOSTAT%CSVR")
     684          310 :             CALL dump_csvr_restart_info(thermostat_part%csvr, para_env, work_section)
     685           20 :          ELSE IF (thermostat_part%type_of_thermostat == do_thermo_al) THEN
     686              :             ! Restart of AD_LANGEVIN Thermostat for Particles
     687            0 :             work_section => section_vals_get_subs_vals(motion_section, "MD%THERMOSTAT%AD_LANGEVIN")
     688            0 :             CALL dump_al_restart_info(thermostat_part%al, para_env, work_section)
     689           20 :          ELSE IF (thermostat_part%type_of_thermostat == do_thermo_gle) THEN
     690              :             ! Restart of GLE Thermostat for Particles
     691           20 :             work_section => section_vals_get_subs_vals(motion_section, "MD%THERMOSTAT%GLE")
     692           20 :             CALL dump_gle_restart_info(thermostat_part%gle, para_env, work_section)
     693              :          END IF
     694              :       END IF
     695        14919 :       CALL timestop(handle2)
     696              : 
     697              :       ! BAROSTAT - THERMOSTAT
     698        14919 :       CALL timeset(routineN//"_BAROSTAT", handle2)
     699        14919 :       IF (ASSOCIATED(thermostat_baro)) THEN
     700          340 :          IF (thermostat_baro%type_of_thermostat == do_thermo_nose) THEN
     701              :             ! Restart of Nose-Hoover Thermostat for Barostat
     702          252 :             nhc => thermostat_baro%nhc
     703          252 :             nhc_len = SIZE(nhc%nvt, 1)
     704          252 :             tot_nhcneed = nhc%glob_num_nhc
     705          756 :             ALLOCATE (eta(tot_nhcneed*nhc_len))
     706          504 :             ALLOCATE (veta(tot_nhcneed*nhc_len))
     707          504 :             ALLOCATE (fnhc(tot_nhcneed*nhc_len))
     708          504 :             ALLOCATE (mnhc(tot_nhcneed*nhc_len))
     709          252 :             counter = 0
     710         1148 :             DO i = 1, SIZE(nhc%nvt, 1)
     711         2044 :                DO j = 1, SIZE(nhc%nvt, 2)
     712          896 :                   counter = counter + 1
     713          896 :                   eta(counter) = nhc%nvt(i, j)%eta
     714          896 :                   veta(counter) = nhc%nvt(i, j)%v
     715          896 :                   fnhc(counter) = nhc%nvt(i, j)%f
     716         1792 :                   mnhc(counter) = nhc%nvt(i, j)%mass
     717              :                END DO
     718              :             END DO
     719          252 :             work_section => section_vals_get_subs_vals(motion_section, "MD%BAROSTAT%THERMOSTAT%NOSE")
     720          252 :             CALL set_template_restart(work_section, eta, veta, fnhc, mnhc)
     721           88 :          ELSE IF (thermostat_baro%type_of_thermostat == do_thermo_csvr) THEN
     722              :             ! Restart of CSVR Thermostat for Barostat
     723           88 :             work_section => section_vals_get_subs_vals(motion_section, "MD%BAROSTAT%THERMOSTAT%CSVR")
     724           88 :             CALL dump_csvr_restart_info(thermostat_baro%csvr, para_env, work_section)
     725              :          END IF
     726              :       END IF
     727        14919 :       CALL timestop(handle2)
     728              : 
     729              :       ! BAROSTAT
     730        14919 :       CALL timeset(routineN//"_NPT", handle2)
     731        14919 :       IF (ASSOCIATED(npt)) THEN
     732         1206 :          ALLOCATE (veta(SIZE(npt, 1)*SIZE(npt, 2)))
     733          804 :          ALLOCATE (mnhc(SIZE(npt, 1)*SIZE(npt, 2)))
     734          402 :          counter = 0
     735         1140 :          DO i = 1, SIZE(npt, 1)
     736         2886 :             DO j = 1, SIZE(npt, 2)
     737         1746 :                counter = counter + 1
     738         1746 :                veta(counter) = npt(i, j)%v
     739         2484 :                mnhc(counter) = npt(i, j)%mass
     740              :             END DO
     741              :          END DO
     742          402 :          work_section => section_vals_get_subs_vals(motion_section, "MD%BAROSTAT")
     743          402 :          CALL set_template_restart(work_section, veta=veta, mnhc=mnhc)
     744              :       END IF
     745        14919 :       CALL timestop(handle2)
     746              : 
     747              :       ! SHELL THERMOSTAT
     748        14919 :       CALL timeset(routineN//"_THERMOSTAT_SHELL", handle2)
     749        14919 :       IF (ASSOCIATED(thermostat_shell)) THEN
     750          160 :          IF (thermostat_shell%type_of_thermostat == do_thermo_nose) THEN
     751              :             ! Restart of Nose-Hoover Thermostat for Shell Particles
     752          136 :             IF (.NOT. my_write_binary_restart_file) THEN
     753          124 :                nhc => thermostat_shell%nhc
     754          124 :                CALL collect_nose_restart_info(nhc, para_env, eta, veta, fnhc, mnhc)
     755          124 :                work_section => section_vals_get_subs_vals(motion_section, "MD%SHELL%THERMOSTAT%NOSE")
     756          124 :                CALL set_template_restart(work_section, eta, veta, fnhc, mnhc)
     757              :             END IF
     758           24 :          ELSE IF (thermostat_shell%type_of_thermostat == do_thermo_csvr) THEN
     759           24 :             work_section => section_vals_get_subs_vals(motion_section, "MD%SHELL%THERMOSTAT%CSVR")
     760              :             ! Restart of CSVR Thermostat for Shell Particles
     761           24 :             CALL dump_csvr_restart_info(thermostat_shell%csvr, para_env, work_section)
     762              :          END IF
     763              :       END IF
     764        14919 :       CALL timestop(handle2)
     765              : 
     766        14919 :       CALL timeset(routineN//"_META", handle2)
     767        14919 :       IF (ASSOCIATED(meta_env)) THEN
     768              :          CALL section_vals_val_set(meta_env%metadyn_section, "STEP_START_VAL", &
     769          994 :                                    i_val=meta_env%n_steps)
     770              :          CALL section_vals_val_set(meta_env%metadyn_section, "NHILLS_START_VAL", &
     771          994 :                                    i_val=meta_env%hills_env%n_hills)
     772              :          !RG Adaptive hills
     773              :          CALL section_vals_val_set(meta_env%metadyn_section, "MIN_DISP", &
     774          994 :                                    r_val=meta_env%hills_env%min_disp)
     775              :          CALL section_vals_val_set(meta_env%metadyn_section, "OLD_HILL_NUMBER", &
     776          994 :                                    i_val=meta_env%hills_env%old_hill_number)
     777              :          CALL section_vals_val_set(meta_env%metadyn_section, "OLD_HILL_STEP", &
     778          994 :                                    i_val=meta_env%hills_env%old_hill_step)
     779              :          !RG Adaptive hills
     780          994 :          IF (meta_env%do_hills .AND. meta_env%hills_env%n_hills /= 0) THEN
     781          784 :             work_section => section_vals_get_subs_vals(meta_env%metadyn_section, "SPAWNED_HILLS_POS")
     782          784 :             CALL meta_hills_val_set_ss(work_section, meta_env)
     783          784 :             work_section => section_vals_get_subs_vals(meta_env%metadyn_section, "SPAWNED_HILLS_SCALE")
     784          784 :             CALL meta_hills_val_set_ds(work_section, meta_env)
     785          784 :             work_section => section_vals_get_subs_vals(meta_env%metadyn_section, "SPAWNED_HILLS_HEIGHT")
     786          784 :             CALL meta_hills_val_set_ww(work_section, meta_env)
     787          784 :             IF (meta_env%well_tempered) THEN
     788            2 :                work_section => section_vals_get_subs_vals(meta_env%metadyn_section, "SPAWNED_HILLS_INVDT")
     789            2 :                CALL meta_hills_val_set_dt(work_section, meta_env)
     790              :             END IF
     791              :          END IF
     792          994 :          IF (meta_env%extended_lagrange) THEN
     793              :             CALL section_vals_val_set(meta_env%metadyn_section, "COLVAR_AVG_TEMPERATURE_RESTART", &
     794          130 :                                       r_val=meta_env%avg_temp)
     795          130 :             work_section => section_vals_get_subs_vals(meta_env%metadyn_section, "EXT_LAGRANGE_SS0")
     796          290 :             DO irep = 1, meta_env%n_colvar
     797              :                CALL section_vals_val_set(work_section, "_DEFAULT_KEYWORD_", r_val=meta_env%metavar(irep)%ss0, &
     798          290 :                                          i_rep_val=irep)
     799              :             END DO
     800          130 :             work_section => section_vals_get_subs_vals(meta_env%metadyn_section, "EXT_LAGRANGE_VVP")
     801          290 :             DO irep = 1, meta_env%n_colvar
     802              :                CALL section_vals_val_set(work_section, "_DEFAULT_KEYWORD_", r_val=meta_env%metavar(irep)%vvp, &
     803          290 :                                          i_rep_val=irep)
     804              :             END DO
     805              : 
     806          130 :             work_section => section_vals_get_subs_vals(meta_env%metadyn_section, "EXT_LAGRANGE_SS")
     807          290 :             DO irep = 1, meta_env%n_colvar
     808              :                CALL section_vals_val_set(work_section, "_DEFAULT_KEYWORD_", r_val=meta_env%metavar(irep)%ss, &
     809          290 :                                          i_rep_val=irep)
     810              :             END DO
     811          130 :             work_section => section_vals_get_subs_vals(meta_env%metadyn_section, "EXT_LAGRANGE_FS")
     812          290 :             DO irep = 1, meta_env%n_colvar
     813              :                CALL section_vals_val_set(work_section, "_DEFAULT_KEYWORD_", r_val=meta_env%metavar(irep)%ff_s, &
     814          290 :                                          i_rep_val=irep)
     815              :             END DO
     816              : 
     817              :          END IF
     818              :          ! Multiple Walkers
     819          994 :          IF (meta_env%do_multiple_walkers) THEN
     820          636 :             ALLOCATE (walkers_status(meta_env%multiple_walkers%walkers_tot_nr))
     821         1272 :             walkers_status = meta_env%multiple_walkers%walkers_status
     822          212 :             work_section => section_vals_get_subs_vals(meta_env%metadyn_section, "MULTIPLE_WALKERS")
     823          212 :             CALL section_vals_val_set(work_section, "WALKERS_STATUS", i_vals_ptr=walkers_status)
     824              :          END IF
     825              :       END IF
     826        14919 :       CALL timestop(handle2)
     827        14919 :       CALL timeset(routineN//"_NEB", handle2)
     828        14919 :       IF (PRESENT(coords) .OR. (PRESENT(vels))) THEN
     829              :          ! Update NEB section
     830          592 :          replica_section => section_vals_get_subs_vals(motion_section, "BAND%REPLICA")
     831          592 :          CALL force_env_get(force_env, subsys=subsys)
     832          592 :          CALL cp_subsys_get(subsys, particles=particles)
     833          592 :          IF (PRESENT(coords)) THEN
     834              :             ! Allocate possible missing sections
     835           68 :             DO
     836          660 :                IF (coords%size_wrk(2) <= SIZE(replica_section%values, 2)) EXIT
     837           68 :                CALL section_vals_add_values(replica_section)
     838              :             END DO
     839              :             ! Write Values
     840         4228 :             DO isec = 1, coords%size_wrk(2)
     841         3636 :                CALL section_vals_val_unset(replica_section, "COORD_FILE_NAME", i_rep_section=isec)
     842         3636 :                work_section => section_vals_get_subs_vals3(replica_section, "COORD", i_rep_section=isec)
     843              :                CALL section_neb_coord_val_set(work_section, coords%xyz(:, isec), SIZE(coords%xyz, 1), 3*SIZE(particles%els), &
     844         3636 :                                               3, particles%els, angstrom)
     845         3636 :                IF (ALLOCATED(coords%cells)) THEN
     846           18 :                   ALLOCATE (wrk(9))
     847          180 :                   wrk(:) = RESHAPE(coords%cells(:, :, isec), [9])
     848           18 :                   CALL section_vals_val_set(replica_section, "CELL_MATRIX", r_vals_ptr=wrk, i_rep_section=isec)
     849              :                END IF
     850              :                ! Update Collective Variables
     851         4228 :                IF (coords%in_use == do_band_collective) THEN
     852          360 :                   ALLOCATE (wrk(coords%size_wrk(1)))
     853          480 :                   wrk = coords%wrk(:, isec)
     854              :                   CALL section_vals_val_set(replica_section, "COLLECTIVE", r_vals_ptr=wrk, &
     855          120 :                                             i_rep_section=isec)
     856              :                END IF
     857              :             END DO
     858              :          END IF
     859          592 :          IF (PRESENT(vels)) THEN
     860          592 :             CALL force_env_get(force_env, subsys=subsys)
     861          592 :             CALL cp_subsys_get(subsys, particles=particles)
     862              :             ! Allocate possible missing sections
     863            0 :             DO
     864          592 :                IF (vels%size_wrk(2) <= SIZE(replica_section%values, 2)) EXIT
     865            0 :                CALL section_vals_add_values(replica_section)
     866              :             END DO
     867              :             ! Write Values
     868         4228 :             DO isec = 1, vels%size_wrk(2)
     869         3636 :                work_section => section_vals_get_subs_vals3(replica_section, "VELOCITY", i_rep_section=isec)
     870         4228 :                IF (vels%in_use == do_band_collective) THEN
     871              :                   CALL section_neb_coord_val_set(work_section, vels%wrk(:, isec), SIZE(vels%wrk, 1), SIZE(vels%wrk, 1), &
     872          120 :                                                  1, particles%els, 1.0_dp)
     873              :                ELSE
     874              :                   CALL section_neb_coord_val_set(work_section, vels%wrk(:, isec), SIZE(vels%wrk, 1), 3*SIZE(particles%els), &
     875         3516 :                                                  3, particles%els, 1.0_dp)
     876              :                END IF
     877              :             END DO
     878              :          END IF
     879              :       END IF
     880        14919 :       CALL timestop(handle2)
     881              : 
     882        14919 :       IF (PRESENT(pint_env)) THEN
     883              :          ! Update PINT section
     884          600 :          CALL update_motion_pint(motion_section, pint_env)
     885              :       END IF
     886              : 
     887        14919 :       IF (PRESENT(helium_env)) THEN
     888              :          ! Update HELIUM section
     889          110 :          CALL update_motion_helium(helium_env)
     890              :       END IF
     891              : 
     892        14919 :       CALL timestop(handle)
     893              : 
     894        14919 :    END SUBROUTINE update_motion
     895              : 
     896              : ! ***************************************************************************
     897              : !> \brief  Update PINT section in the input structure
     898              : !> \param motion_section ...
     899              : !> \param pint_env ...
     900              : !> \date   2010-10-13
     901              : !> \author Lukasz Walewski <Lukasz.Walewski@ruhr-uni-bochum.de>
     902              : ! **************************************************************************************************
     903          600 :    SUBROUTINE update_motion_pint(motion_section, pint_env)
     904              : 
     905              :       TYPE(section_vals_type), POINTER                   :: motion_section
     906              :       TYPE(pint_env_type), INTENT(IN)                    :: pint_env
     907              : 
     908              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'update_motion_pint'
     909              : 
     910              :       CHARACTER(LEN=rng_record_length)                   :: rng_record
     911              :       INTEGER                                            :: handle, i, iatom, ibead, inos, isp
     912              :       INTEGER, DIMENSION(rng_record_length, 1)           :: ascii
     913              :       LOGICAL                                            :: explicit
     914          600 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: r_vals
     915              :       TYPE(section_vals_type), POINTER                   :: pint_section, tmpsec
     916              : 
     917          600 :       CALL timeset(routineN, handle)
     918              : 
     919          600 :       pint_section => section_vals_get_subs_vals(motion_section, "PINT")
     920          600 :       CALL section_vals_val_set(pint_section, "ITERATION", i_val=pint_env%iter)
     921              : 
     922              :       ! allocate memory for COORDs and VELOCITYs if the BEADS section was not
     923              :       ! explicitly given in the input (this is actually done only once since
     924              :       ! after section_vals_add_values section becomes explicit)
     925          600 :       NULLIFY (tmpsec)
     926          600 :       tmpsec => section_vals_get_subs_vals(pint_section, "BEADS")
     927          600 :       CALL section_vals_get(tmpsec, explicit=explicit)
     928          600 :       IF (.NOT. explicit) THEN
     929           46 :          CALL section_vals_add_values(tmpsec)
     930              :       END IF
     931              : 
     932              :       ! update bead coordinates in the global input structure
     933          600 :       NULLIFY (r_vals)
     934         1800 :       ALLOCATE (r_vals(pint_env%p*pint_env%ndim))
     935              : 
     936          600 :       i = 1
     937          600 :       CALL pint_u2x(pint_env)
     938        98160 :       DO iatom = 1, pint_env%ndim
     939       501072 :          DO ibead = 1, pint_env%p
     940       402912 :             r_vals(i) = pint_env%x(ibead, iatom)
     941       500472 :             i = i + 1
     942              :          END DO
     943              :       END DO
     944              :       CALL section_vals_val_set(pint_section, "BEADS%COORD%_DEFAULT_KEYWORD_", &
     945          600 :                                 r_vals_ptr=r_vals)
     946              : 
     947              :       ! update bead velocities in the global input structure
     948          600 :       NULLIFY (r_vals)
     949         1800 :       ALLOCATE (r_vals(pint_env%p*pint_env%ndim))
     950          600 :       i = 1
     951          600 :       CALL pint_u2x(pint_env, ux=pint_env%uv, x=pint_env%v)
     952        98160 :       DO iatom = 1, pint_env%ndim
     953       501072 :          DO ibead = 1, pint_env%p
     954       402912 :             r_vals(i) = pint_env%v(ibead, iatom)
     955       500472 :             i = i + 1
     956              :          END DO
     957              :       END DO
     958              :       CALL section_vals_val_set(pint_section, "BEADS%VELOCITY%_DEFAULT_KEYWORD_", &
     959          600 :                                 r_vals_ptr=r_vals)
     960              : 
     961          600 :       IF (pint_env%pimd_thermostat == thermostat_nose) THEN
     962              : 
     963              :          ! allocate memory for COORDs and VELOCITYs if the NOSE section was not
     964              :          ! explicitly given in the input (this is actually done only once since
     965              :          ! after section_vals_add_values section becomes explicit)
     966          226 :          NULLIFY (tmpsec)
     967          226 :          tmpsec => section_vals_get_subs_vals(pint_section, "NOSE")
     968          226 :          CALL section_vals_get(tmpsec, explicit=explicit)
     969          226 :          IF (.NOT. explicit) THEN
     970            0 :             CALL section_vals_add_values(tmpsec)
     971              :          END IF
     972              : 
     973              :          ! update thermostat coordinates in the global input structure
     974          226 :          NULLIFY (r_vals)
     975          678 :          ALLOCATE (r_vals(pint_env%p*pint_env%ndim*pint_env%nnos))
     976          226 :          i = 1
     977         2440 :          DO iatom = 1, pint_env%ndim
     978        17056 :             DO ibead = 1, pint_env%p
     979        63558 :                DO inos = 1, pint_env%nnos
     980        46728 :                   r_vals(i) = pint_env%tx(inos, ibead, iatom)
     981        61344 :                   i = i + 1
     982              :                END DO
     983              :             END DO
     984              :          END DO
     985              :          CALL section_vals_val_set(pint_section, "NOSE%COORD%_DEFAULT_KEYWORD_", &
     986          226 :                                    r_vals_ptr=r_vals)
     987              : 
     988              :          ! update thermostat velocities in the global input structure
     989          226 :          NULLIFY (r_vals)
     990          678 :          ALLOCATE (r_vals(pint_env%p*pint_env%ndim*pint_env%nnos))
     991          226 :          i = 1
     992         2440 :          DO iatom = 1, pint_env%ndim
     993        17056 :             DO ibead = 1, pint_env%p
     994        63558 :                DO inos = 1, pint_env%nnos
     995        46728 :                   r_vals(i) = pint_env%tv(inos, ibead, iatom)
     996        61344 :                   i = i + 1
     997              :                END DO
     998              :             END DO
     999              :          END DO
    1000              :          CALL section_vals_val_set(pint_section, "NOSE%VELOCITY%_DEFAULT_KEYWORD_", &
    1001          452 :                                    r_vals_ptr=r_vals)
    1002              : 
    1003              :       ELSE IF (pint_env%pimd_thermostat == thermostat_gle) THEN
    1004              : 
    1005            0 :          NULLIFY (tmpsec)
    1006            0 :          tmpsec => section_vals_get_subs_vals(pint_section, "GLE")
    1007            0 :          CALL dump_gle_restart_info(pint_env%gle, pint_env%replicas%para_env, tmpsec)
    1008              : 
    1009              :       ELSE IF (pint_env%pimd_thermostat == thermostat_pile) THEN
    1010              :          tmpsec => section_vals_get_subs_vals(pint_section, &
    1011          302 :                                               "PILE%RNG_INIT")
    1012          302 :          CALL pint_env%pile_therm%gaussian_rng_stream%dump(rng_record)
    1013          302 :          CALL string_to_ascii(rng_record, ascii(:, 1))
    1014              :          CALL section_rng_val_set(rng_section=tmpsec, nsize=1, &
    1015          302 :                                   ascii=ascii)
    1016          302 :          tmpsec => section_vals_get_subs_vals(pint_section, "PILE")
    1017              :          CALL section_vals_val_set(tmpsec, "THERMOSTAT_ENERGY", &
    1018          302 :                                    r_val=pint_env%e_pile)
    1019              :       ELSE IF (pint_env%pimd_thermostat == thermostat_qtb) THEN
    1020              :          tmpsec => section_vals_get_subs_vals(pint_section, &
    1021           20 :                                               "QTB%RNG_INIT")
    1022              :          CALL string_to_ascii(pint_env%qtb_therm%rng_status(1), &
    1023           20 :                               ascii(:, 1))
    1024              :          CALL section_rng_val_set(rng_section=tmpsec, nsize=1, &
    1025           20 :                                   ascii=ascii)
    1026           20 :          tmpsec => section_vals_get_subs_vals(pint_section, "QTB")
    1027              :          CALL section_vals_val_set(tmpsec, "THERMOSTAT_ENERGY", &
    1028           20 :                                    r_val=pint_env%e_qtb)
    1029              :       ELSE IF (pint_env%pimd_thermostat == thermostat_piglet) THEN
    1030              :          tmpsec => section_vals_get_subs_vals(pint_section, &
    1031            0 :                                               "PIGLET%RNG_INIT")
    1032            0 :          CALL pint_env%piglet_therm%gaussian_rng_stream%dump(rng_record)
    1033            0 :          CALL string_to_ascii(rng_record, ascii(:, 1))
    1034              :          CALL section_rng_val_set(rng_section=tmpsec, nsize=1, &
    1035            0 :                                   ascii=ascii)
    1036            0 :          tmpsec => section_vals_get_subs_vals(pint_section, "PIGLET")
    1037              :          CALL section_vals_val_set(tmpsec, "THERMOSTAT_ENERGY", &
    1038            0 :                                    r_val=pint_env%e_piglet)
    1039              :          ! update thermostat velocities in the global input structure
    1040            0 :          NULLIFY (r_vals)
    1041              :          ALLOCATE (r_vals((pint_env%piglet_therm%nsp1 - 1)* &
    1042              :                           pint_env%piglet_therm%ndim* &
    1043            0 :                           pint_env%piglet_therm%p))
    1044            0 :          i = 1
    1045            0 :          DO isp = 2, pint_env%piglet_therm%nsp1
    1046            0 :             DO ibead = 1, pint_env%piglet_therm%p*pint_env%piglet_therm%ndim
    1047            0 :                r_vals(i) = pint_env%piglet_therm%smalls(isp, ibead)
    1048            0 :                i = i + 1
    1049              :             END DO
    1050              :          END DO
    1051              :          CALL section_vals_val_set(pint_section, "PIGLET%EXTRA_DOF%_DEFAULT_KEYWORD_", &
    1052            0 :                                    r_vals_ptr=r_vals)
    1053              :       END IF
    1054              : 
    1055          600 :       CALL timestop(handle)
    1056              : 
    1057         1200 :    END SUBROUTINE update_motion_pint
    1058              : 
    1059              : ! ***************************************************************************
    1060              : !> \brief  Update HELIUM section in the input structure.
    1061              : !> \param helium_env ...
    1062              : !> \date   2009-11-12
    1063              : !> \parm   History
    1064              : !>         2016-07-14 Modified to work with independent helium_env [cschran]
    1065              : !> \author Lukasz Walewski <Lukasz.Walewski@ruhr-uni-bochum.de>
    1066              : !> \note Transfer the current helium state from the runtime environment
    1067              : !>         to the input structure, so that it can be used for I/O, etc.
    1068              : !> \note   Moved from the helium_io module directly, might be done better way
    1069              : ! **************************************************************************************************
    1070          110 :    SUBROUTINE update_motion_helium(helium_env)
    1071              : 
    1072              :       TYPE(helium_solvent_p_type), DIMENSION(:), POINTER :: helium_env
    1073              : 
    1074              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'update_motion_helium'
    1075              : 
    1076              :       CHARACTER(LEN=default_string_length)               :: err_str, stmp
    1077              :       INTEGER                                            :: handle, i, itmp, iweight, msglen, &
    1078              :                                                             nsteps, off, offset, reqlen
    1079          110 :       INTEGER, DIMENSION(:), POINTER                     :: int_msg_gather
    1080              :       LOGICAL                                            :: lbf
    1081              :       REAL(kind=dp)                                      :: bf, bu, invproc
    1082              :       REAL(kind=dp), DIMENSION(3, 2)                     :: bg, cg, ig
    1083          110 :       REAL(kind=dp), DIMENSION(:), POINTER               :: real_msg, real_msg_gather
    1084              :       TYPE(cp_logger_type), POINTER                      :: logger
    1085              : 
    1086          110 :       CALL timeset(routineN, handle)
    1087              : 
    1088              :       !CPASSERT(ASSOCIATED(helium_env))
    1089              : 
    1090          110 :       NULLIFY (logger)
    1091          110 :       logger => cp_get_default_logger()
    1092              : 
    1093          110 :       IF (ASSOCIATED(helium_env)) THEN
    1094              :          ! determine offset for arrays
    1095          105 :          offset = 0
    1096          155 :          DO i = 1, logger%para_env%mepos
    1097          155 :             offset = offset + helium_env(1)%env_all(i)
    1098              :          END DO
    1099              : 
    1100          105 :          IF (.NOT. helium_env(1)%helium%solute_present) THEN
    1101              :             ! update iteration number
    1102           38 :             itmp = logger%iter_info%iteration(2)
    1103              :             CALL section_vals_val_set( &
    1104              :                helium_env(1)%helium%input, &
    1105              :                "MOTION%PINT%ITERATION", &
    1106           38 :                i_val=itmp)
    1107              :             ! else - PINT will do that
    1108              :          END IF
    1109              : 
    1110              :          !
    1111              :          ! save coordinates
    1112              :          !
    1113              :          ! allocate the buffer to be passed and fill it with local coords at each
    1114              :          ! proc
    1115          105 :          NULLIFY (real_msg)
    1116          105 :          NULLIFY (real_msg_gather)
    1117          420 :          msglen = SIZE(helium_env(1)%helium%pos(:, :, 1:helium_env(1)%helium%beads))
    1118          315 :          ALLOCATE (real_msg(msglen*helium_env(1)%helium%num_env))
    1119          315 :          ALLOCATE (real_msg_gather(msglen*helium_env(1)%helium%num_env))
    1120       340329 :          real_msg(:) = 0.0_dp
    1121          240 :          DO i = 1, SIZE(helium_env)
    1122       172752 :       real_msg((offset+i-1)*msglen+1:(offset+i)*msglen) = PACK(helium_env(i)%helium%pos(:, :, 1:helium_env(i)%helium%beads), .TRUE.)
    1123              :          END DO
    1124              : 
    1125              :          ! pass the message from all processors to logger%para_env%source
    1126       680553 :          CALL helium_env(1)%comm%sum(real_msg)
    1127       680658 :          real_msg_gather(:) = real_msg(:)
    1128              : 
    1129              :          ! update coordinates in the global input structure, only in
    1130              :          ! helium_env(1)
    1131              :          CALL section_vals_val_set(helium_env(1)%helium%input, &
    1132              :                                    "MOTION%PINT%HELIUM%COORD%_DEFAULT_KEYWORD_", &
    1133          105 :                                    r_vals_ptr=real_msg_gather)
    1134              : 
    1135              :          ! NULLIFY, but do not DEALLOCATE! - a new pointer to this array is silently
    1136              :          ! assigned in section_vals_val_set - this memory will be used later on!
    1137              :          ! "The val becomes the owner of the array" - from section_vals_val_set docu
    1138          105 :          NULLIFY (real_msg_gather)
    1139              : 
    1140              :          ! DEALLOCATE since this array is only used locally
    1141          105 :          DEALLOCATE (real_msg)
    1142              : 
    1143              :          !
    1144              :          ! save permutation state
    1145              :          !
    1146              :          ! allocate the buffer for message passing
    1147          105 :          NULLIFY (int_msg_gather)
    1148          105 :          msglen = SIZE(helium_env(1)%helium%permutation)
    1149          315 :          ALLOCATE (int_msg_gather(msglen*helium_env(1)%helium%num_env))
    1150              : 
    1151              :          ! pass the message from all processors to logger%para_env%source
    1152         5825 :          int_msg_gather(:) = 0
    1153          240 :          DO i = 1, SIZE(helium_env)
    1154         3150 :             int_msg_gather((offset + i - 1)*msglen + 1:(offset + i)*msglen) = helium_env(i)%helium%permutation
    1155              :          END DO
    1156              : 
    1157        11545 :          CALL helium_env(1)%comm%sum(int_msg_gather)
    1158              : 
    1159              :          ! update permutation state in the global input structure
    1160              :          CALL section_vals_val_set(helium_env(1)%helium%input, &
    1161              :                                    "MOTION%PINT%HELIUM%PERM%_DEFAULT_KEYWORD_", &
    1162          105 :                                    i_vals_ptr=int_msg_gather)
    1163              : 
    1164              :          ! NULLIFY, but do not DEALLOCATE! - a new pointer to this array is silently
    1165              :          ! assigned in section_vals_val_set - this memory will be used later on!
    1166              :          ! "The val becomes the owner of the array" - from section_vals_val_set docu
    1167          105 :          NULLIFY (int_msg_gather)
    1168              : 
    1169              :          !
    1170              :          ! save averages
    1171              :          !
    1172              :          ! update the weighting factor
    1173          105 :          itmp = helium_env(1)%helium%averages_iweight
    1174          105 :          IF (itmp < 0) THEN
    1175            0 :             itmp = helium_env(1)%helium%current_step - helium_env(1)%helium%first_step
    1176              :          ELSE
    1177          105 :             itmp = itmp + helium_env(1)%helium%current_step - helium_env(1)%helium%first_step
    1178              :          END IF
    1179          240 :          DO i = 1, SIZE(helium_env)
    1180              :             CALL section_vals_val_set(helium_env(i)%helium%input, &
    1181              :                                       "MOTION%PINT%HELIUM%AVERAGES%IWEIGHT", &
    1182          240 :                                       i_val=itmp)
    1183              :          END DO
    1184              : 
    1185              :          ! allocate the buffer for message passing
    1186          105 :          NULLIFY (real_msg_gather)
    1187          105 :          msglen = 3
    1188          315 :          ALLOCATE (real_msg_gather(msglen*helium_env(1)%helium%num_env))
    1189              : 
    1190          900 :          real_msg_gather(:) = 0.0_dp
    1191              :          ! gather projected area from all processors
    1192          240 :          DO i = 1, SIZE(helium_env)
    1193          645 :             real_msg_gather((i - 1 + offset)*msglen + 1:(i + offset)*msglen) = helium_env(i)%helium%proarea%ravr(:)
    1194              :          END DO
    1195         1695 :          CALL helium_env(1)%comm%sum(real_msg_gather)
    1196              : 
    1197              :          ! update it in the global input structure
    1198              :          CALL section_vals_val_set(helium_env(1)%helium%input, &
    1199              :                                    "MOTION%PINT%HELIUM%AVERAGES%PROJECTED_AREA", &
    1200          105 :                                    r_vals_ptr=real_msg_gather)
    1201              : 
    1202              :          ! allocate the buffer for message passing
    1203          105 :          NULLIFY (real_msg_gather)
    1204          105 :          msglen = 3
    1205          315 :          ALLOCATE (real_msg_gather(msglen*helium_env(1)%helium%num_env))
    1206              : 
    1207          900 :          real_msg_gather(:) = 0.0_dp
    1208              :          ! gather projected area squared from all processors
    1209          240 :          DO i = 1, SIZE(helium_env)
    1210          645 :             real_msg_gather((i - 1 + offset)*msglen + 1:(i + offset)*msglen) = helium_env(i)%helium%prarea2%ravr(:)
    1211              :          END DO
    1212         1695 :          CALL helium_env(1)%comm%sum(real_msg_gather)
    1213              : 
    1214              :          ! update it in the global input structure
    1215              :          CALL section_vals_val_set(helium_env(1)%helium%input, &
    1216              :                                    "MOTION%PINT%HELIUM%AVERAGES%PROJECTED_AREA_2", &
    1217          105 :                                    r_vals_ptr=real_msg_gather)
    1218              : 
    1219              :          ! allocate the buffer for message passing
    1220          105 :          NULLIFY (real_msg_gather)
    1221          105 :          msglen = 3
    1222          315 :          ALLOCATE (real_msg_gather(msglen*helium_env(1)%helium%num_env))
    1223              : 
    1224          900 :          real_msg_gather(:) = 0.0_dp
    1225              :          ! gather winding number squared from all processors
    1226          240 :          DO i = 1, SIZE(helium_env)
    1227          645 :             real_msg_gather((i - 1 + offset)*msglen + 1:(i + offset)*msglen) = helium_env(i)%helium%wnmber2%ravr(:)
    1228              :          END DO
    1229         1695 :          CALL helium_env(1)%comm%sum(real_msg_gather)
    1230              : 
    1231              :          ! update it in the global input structure
    1232              :          CALL section_vals_val_set(helium_env(1)%helium%input, &
    1233              :                                    "MOTION%PINT%HELIUM%AVERAGES%WINDING_NUMBER_2", &
    1234          105 :                                    r_vals_ptr=real_msg_gather)
    1235              : 
    1236              :          ! allocate the buffer for message passing
    1237          105 :          NULLIFY (real_msg_gather)
    1238          105 :          msglen = 3
    1239          315 :          ALLOCATE (real_msg_gather(msglen*helium_env(1)%helium%num_env))
    1240              : 
    1241          900 :          real_msg_gather(:) = 0.0_dp
    1242              :          ! gather moment of inertia from all processors
    1243          240 :          DO i = 1, SIZE(helium_env)
    1244          645 :             real_msg_gather((i - 1 + offset)*msglen + 1:(i + offset)*msglen) = helium_env(i)%helium%mominer%ravr(:)
    1245              :          END DO
    1246         1695 :          CALL helium_env(1)%comm%sum(real_msg_gather)
    1247              : 
    1248              :          ! update it in the global input structure
    1249              :          CALL section_vals_val_set(helium_env(1)%helium%input, &
    1250              :                                    "MOTION%PINT%HELIUM%AVERAGES%MOMENT_OF_INERTIA", &
    1251          105 :                                    r_vals_ptr=real_msg_gather)
    1252              : 
    1253              :          ! NULLIFY, but do not DEALLOCATE! - a new pointer to this array is silently
    1254              :          ! assigned in section_vals_val_set - this memory will be used later on!
    1255              :          ! "The val becomes the owner of the array" - from section_vals_val_set docu
    1256          105 :          NULLIFY (real_msg_gather)
    1257              : 
    1258              :          !
    1259              :          ! save RNG state
    1260              :          !
    1261              :          ! pack RNG state on each processor to the local array and save in
    1262              :          ! gather with offset determined earlier
    1263              :          NULLIFY (real_msg)
    1264          105 :          msglen = 40
    1265          105 :          ALLOCATE (real_msg(msglen))
    1266              :          NULLIFY (real_msg_gather)
    1267          315 :          ALLOCATE (real_msg_gather(msglen*helium_env(1)%helium%num_env))
    1268        10705 :          real_msg_gather(:) = 0.0_dp
    1269              : 
    1270          240 :          DO i = 1, SIZE(helium_env)
    1271              :             CALL helium_env(i)%helium%rng_stream_uniform%get(bg=bg, cg=cg, ig=ig, &
    1272          135 :                                                              buffer=bu, buffer_filled=lbf)
    1273          135 :             off = 0
    1274          135 :             real_msg(off + 1:off + 6) = PACK(bg, .TRUE.)
    1275          135 :             real_msg(off + 7:off + 12) = PACK(cg, .TRUE.)
    1276          135 :             real_msg(off + 13:off + 18) = PACK(ig, .TRUE.)
    1277          135 :             IF (lbf) THEN
    1278              :                bf = 1.0_dp
    1279              :             ELSE
    1280          135 :                bf = -1.0_dp
    1281              :             END IF
    1282          135 :             real_msg(off + 19) = bf
    1283          135 :             real_msg(off + 20) = bu
    1284              :             CALL helium_env(i)%helium%rng_stream_gaussian%get(bg=bg, cg=cg, ig=ig, &
    1285          135 :                                                               buffer=bu, buffer_filled=lbf)
    1286          135 :             off = 20
    1287          135 :             real_msg(off + 1:off + 6) = PACK(bg, .TRUE.)
    1288          135 :             real_msg(off + 7:off + 12) = PACK(cg, .TRUE.)
    1289          135 :             real_msg(off + 13:off + 18) = PACK(ig, .TRUE.)
    1290          135 :             IF (lbf) THEN
    1291              :                bf = 1.0_dp
    1292              :             ELSE
    1293           73 :                bf = -1.0_dp
    1294              :             END IF
    1295          135 :             real_msg(off + 19) = bf
    1296          135 :             real_msg(off + 20) = bu
    1297              : 
    1298        11175 :             real_msg_gather((offset + i - 1)*msglen + 1:(offset + i)*msglen) = real_msg(:)
    1299              :          END DO
    1300              : 
    1301              :          ! Gather RNG state (in real_msg_gather vector) from all processors at
    1302              :          ! logger%para_env%source
    1303        21305 :          CALL helium_env(1)%comm%sum(real_msg_gather)
    1304              : 
    1305              :          ! update the RNG state in the global input structure
    1306              :          CALL section_vals_val_set(helium_env(1)%helium%input, &
    1307              :                                    "MOTION%PINT%HELIUM%RNG_STATE%_DEFAULT_KEYWORD_", &
    1308          105 :                                    r_vals_ptr=real_msg_gather)
    1309              : 
    1310              :          ! NULLIFY, but do not DEALLOCATE! - a new pointer to this array is silently
    1311              :          ! assigned in section_vals_val_set - this memeory will be used later on!
    1312              :          ! "The val becomes the owner of the array" - from section_vals_val_set docu
    1313          105 :          NULLIFY (real_msg_gather)
    1314              : 
    1315              :          ! DEALLOCATE since this array is only used locally
    1316          105 :          DEALLOCATE (real_msg)
    1317              : 
    1318          105 :          IF (helium_env(1)%helium%solute_present) THEN
    1319              :             !
    1320              :             ! save forces on the solute
    1321              :             !
    1322              :             ! check that the number of values match the current runtime
    1323           67 :             reqlen = helium_env(1)%helium%solute_atoms*helium_env(1)%helium%solute_beads*3
    1324          201 :             msglen = SIZE(helium_env(1)%helium%force_avrg)
    1325           67 :             err_str = "Invalid size of HELIUM%FORCE: received '"
    1326           67 :             stmp = ""
    1327           67 :             WRITE (stmp, *) msglen
    1328              :             err_str = TRIM(ADJUSTL(err_str))// &
    1329           67 :                       TRIM(ADJUSTL(stmp))//"' but expected '"
    1330           67 :             stmp = ""
    1331           67 :             WRITE (stmp, *) reqlen
    1332              :             err_str = TRIM(ADJUSTL(err_str))// &
    1333           67 :                       TRIM(ADJUSTL(stmp))//"'."
    1334           67 :             IF (msgLEN /= reqlen) THEN
    1335            0 :                CPABORT(err_str)
    1336              :             END IF
    1337              : 
    1338              :             ! allocate the buffer to be saved and fill it with forces
    1339              :             ! forces should be the same on all processors, but we don't check that here
    1340           67 :             NULLIFY (real_msg_gather)
    1341          201 :             ALLOCATE (real_msg_gather(msglen))
    1342         4531 :             real_msg_gather(:) = PACK(helium_env(1)%helium%force_avrg, .TRUE.)
    1343              : 
    1344              :             ! update forces in the global input structure
    1345              :             CALL section_vals_val_set(helium_env(1)%helium%input, &
    1346              :                                       "MOTION%PINT%HELIUM%FORCE%_DEFAULT_KEYWORD_", &
    1347           67 :                                       r_vals_ptr=real_msg_gather)
    1348              : 
    1349              :             ! NULLIFY, but do not DEALLOCATE! - a new pointer to this array is silently
    1350              :             ! assigned in section_vals_val_set - this memeory will be used later on!
    1351              :             ! "The val becomes the owner of the array" - from section_vals_val_set docu
    1352           67 :             NULLIFY (real_msg_gather)
    1353              :          END IF
    1354              : 
    1355              :          !
    1356              :          ! save the RDFs
    1357              :          !
    1358          105 :          IF (helium_env(1)%helium%rdf_present) THEN
    1359              : 
    1360              :             ! work on the temporary array so that accumulated data remains intact
    1361         5010 :             helium_env(1)%helium%rdf_inst(:, :) = 0.0_dp
    1362           20 :             DO i = 1, SIZE(helium_env)
    1363              :                helium_env(1)%helium%rdf_inst(:, :) = helium_env(1)%helium%rdf_inst(:, :) + &
    1364         5020 :                                                      helium_env(i)%helium%rdf_accu(:, :)
    1365              :             END DO
    1366              : 
    1367              :             ! average over processors / helium environments
    1368        10010 :             CALL helium_env(1)%comm%sum(helium_env(1)%helium%rdf_inst)
    1369           10 :             itmp = helium_env(1)%helium%num_env
    1370           10 :             invproc = 1.0_dp/REAL(itmp, dp)
    1371         5010 :             helium_env(1)%helium%rdf_inst(:, :) = helium_env(1)%helium%rdf_inst(:, :)*invproc
    1372              : 
    1373           10 :             nsteps = helium_env(1)%helium%current_step - helium_env(1)%helium%first_step
    1374         5010 :             helium_env(1)%helium%rdf_inst(:, :) = helium_env(1)%helium%rdf_inst(:, :)/REAL(nsteps, dp)
    1375           10 :             iweight = helium_env(1)%helium%rdf_iweight
    1376              :             ! average over the old and the current density (observe the weights!)
    1377              :             helium_env(1)%helium%rdf_inst(:, :) = nsteps*helium_env(1)%helium%rdf_inst(:, :) + &
    1378         5010 :                                                   iweight*helium_env(1)%helium%rdf_rstr(:, :)
    1379         5010 :             helium_env(1)%helium%rdf_inst(:, :) = helium_env(1)%helium%rdf_inst(:, :)/REAL(nsteps + iweight, dp)
    1380              :             ! update in the global input structure
    1381           10 :             NULLIFY (real_msg)
    1382           30 :             msglen = SIZE(helium_env(1)%helium%rdf_inst)
    1383           30 :             ALLOCATE (real_msg(msglen))
    1384         2510 :             real_msg(:) = PACK(helium_env(1)%helium%rdf_inst, .TRUE.)
    1385              :             CALL section_vals_val_set( &
    1386              :                helium_env(1)%helium%input, &
    1387              :                "MOTION%PINT%HELIUM%AVERAGES%RDF", &
    1388           10 :                r_vals_ptr=real_msg)
    1389           10 :             NULLIFY (real_msg)
    1390              : 
    1391              :          END IF
    1392              : 
    1393              :          !
    1394              :          ! save the densities
    1395              :          !
    1396          105 :          IF (helium_env(1)%helium%rho_present) THEN
    1397              : 
    1398              :             ! work on the temporary array so that accumulated data remains intact
    1399        21110 :             helium_env(1)%helium%rho_inst(:, :, :, :) = 0.0_dp
    1400           20 :             DO i = 1, SIZE(helium_env)
    1401              :                helium_env(1)%helium%rho_inst(:, :, :, :) = helium_env(1)%helium%rho_inst(:, :, :, :) + &
    1402        21120 :                                                            helium_env(i)%helium%rho_accu(:, :, :, :)
    1403              :             END DO
    1404              : 
    1405              :             ! average over processors / helium environments
    1406        42210 :             CALL helium_env(1)%comm%sum(helium_env(1)%helium%rho_inst)
    1407           10 :             itmp = helium_env(1)%helium%num_env
    1408           10 :             invproc = 1.0_dp/REAL(itmp, dp)
    1409        21110 :             helium_env(1)%helium%rho_inst(:, :, :, :) = helium_env(1)%helium%rho_inst(:, :, :, :)*invproc
    1410              : 
    1411           10 :             nsteps = helium_env(1)%helium%current_step - helium_env(1)%helium%first_step
    1412        21110 :             helium_env(1)%helium%rho_inst(:, :, :, :) = helium_env(1)%helium%rho_inst(:, :, :, :)/REAL(nsteps, dp)
    1413           10 :             iweight = helium_env(1)%helium%averages_iweight
    1414              :             ! average over the old and the current density (observe the weights!)
    1415              :             helium_env(1)%helium%rho_inst(:, :, :, :) = nsteps*helium_env(1)%helium%rho_inst(:, :, :, :) + &
    1416        21110 :                                                         iweight*helium_env(1)%helium%rho_rstr(:, :, :, :)
    1417        21110 :             helium_env(1)%helium%rho_inst(:, :, :, :) = helium_env(1)%helium%rho_inst(:, :, :, :)/REAL(nsteps + iweight, dp)
    1418              : 
    1419              :             ! update the densities in the global input structure
    1420           10 :             NULLIFY (real_msg)
    1421           50 :             msglen = SIZE(helium_env(1)%helium%rho_inst)
    1422           30 :             ALLOCATE (real_msg(msglen))
    1423        10010 :             real_msg(:) = PACK(helium_env(1)%helium%rho_inst, .TRUE.)
    1424              :             CALL section_vals_val_set( &
    1425              :                helium_env(1)%helium%input, &
    1426              :                "MOTION%PINT%HELIUM%AVERAGES%RHO", &
    1427           10 :                r_vals_ptr=real_msg)
    1428           10 :             NULLIFY (real_msg)
    1429              : 
    1430              :          END IF
    1431              : 
    1432              :       END IF ! ASSOCIATED(helium_env)
    1433              : 
    1434          110 :       CALL timestop(handle)
    1435              : 
    1436          110 :    END SUBROUTINE update_motion_helium
    1437              : 
    1438              : ! **************************************************************************************************
    1439              : !> \brief routine to dump thermostat CSVR energies
    1440              : !> \param thermostat_energy ...
    1441              : !> \param nsize ...
    1442              : !> \param work_section ...
    1443              : !> \par History
    1444              : !>      10.2007 created [teo]
    1445              : !> \author Teodoro Laino - University of Zurich
    1446              : ! **************************************************************************************************
    1447          442 :    SUBROUTINE dump_csvr_energy_info(thermostat_energy, nsize, work_section)
    1448              : 
    1449              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: thermostat_energy
    1450              :       INTEGER, INTENT(IN)                                :: nsize
    1451              :       TYPE(section_vals_type), POINTER                   :: work_section
    1452              : 
    1453              :       INTEGER                                            :: ik, irk, Nlist
    1454              :       TYPE(cp_sll_val_type), POINTER                     :: new_pos, vals
    1455              :       TYPE(section_type), POINTER                        :: section
    1456              :       TYPE(val_type), POINTER                            :: my_val, old_val
    1457              : 
    1458          442 :       CPASSERT(ASSOCIATED(work_section))
    1459          442 :       CPASSERT(work_section%ref_count > 0)
    1460              : 
    1461          442 :       NULLIFY (my_val, old_val, section, vals)
    1462              : 
    1463          442 :       section => work_section%section
    1464              : 
    1465          442 :       ik = section_get_keyword_index(section, "_DEFAULT_KEYWORD_")
    1466              : 
    1467          442 :       IF (ik == -2) THEN
    1468              :          CALL cp_abort(__LOCATION__, "section "//TRIM(section%name)//" does not contain keyword "// &
    1469            0 :                        "_DEFAULT_KEYWORD_")
    1470              :       END IF
    1471              : 
    1472          156 :       DO
    1473          598 :          IF (SIZE(work_section%values, 2) == 1) EXIT
    1474          156 :          CALL section_vals_add_values(work_section)
    1475              :       END DO
    1476              : 
    1477          442 :       vals => work_section%values(ik, 1)%list
    1478          442 :       Nlist = 0
    1479              : 
    1480          442 :       IF (ASSOCIATED(vals)) THEN
    1481          286 :          Nlist = cp_sll_val_get_length(vals)
    1482              :       END IF
    1483              : 
    1484        22838 :       DO irk = 1, nsize
    1485        22396 :          CALL val_create(val=my_val, r_val=thermostat_energy(irk))
    1486        22396 :          IF (Nlist /= 0) THEN
    1487        19024 :             IF (irk == 1) THEN
    1488          286 :                new_pos => vals
    1489              :             ELSE
    1490        18738 :                new_pos => new_pos%rest
    1491              :             END IF
    1492        19024 :             old_val => new_pos%first_el
    1493        19024 :             CALL val_release(old_val)
    1494        19024 :             new_pos%first_el => my_val
    1495              :          ELSE
    1496         3372 :             IF (irk == 1) THEN
    1497          156 :                NULLIFY (new_pos)
    1498          156 :                CALL cp_sll_val_create(new_pos, first_el=my_val)
    1499          156 :                vals => new_pos
    1500              :             ELSE
    1501         3216 :                NULLIFY (new_pos%rest)
    1502         3216 :                CALL cp_sll_val_create(new_pos%rest, first_el=my_val)
    1503         3216 :                new_pos => new_pos%rest
    1504              :             END IF
    1505              :          END IF
    1506        22838 :          NULLIFY (my_val)
    1507              :       END DO
    1508          442 :       work_section%values(ik, 1)%list => vals
    1509              : 
    1510          442 :    END SUBROUTINE dump_csvr_energy_info
    1511              : 
    1512              : ! **************************************************************************************************
    1513              : !> \brief Collect all information needed to dump the restart for CSVR
    1514              : !>      thermostat
    1515              : !> \param csvr ...
    1516              : !> \param para_env ...
    1517              : !> \param csvr_section ...
    1518              : !> \par History
    1519              : !>      10.2007 created [tlaino] - University of Zurich
    1520              : !> \author Teodoro Laino
    1521              : ! **************************************************************************************************
    1522          422 :    SUBROUTINE dump_csvr_restart_info(csvr, para_env, csvr_section)
    1523              : 
    1524              :       TYPE(csvr_system_type), POINTER                    :: csvr
    1525              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1526              :       TYPE(section_vals_type), POINTER                   :: csvr_section
    1527              : 
    1528              :       CHARACTER(LEN=rng_record_length)                   :: rng_record
    1529              :       INTEGER                                            :: i, my_index
    1530              :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: dwork
    1531              :       REAL(KIND=dp)                                      :: dum
    1532              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: thermo_energy
    1533              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: work
    1534              :       TYPE(section_vals_type), POINTER                   :: work_section
    1535              : 
    1536              : ! Thermostat Energies
    1537              : 
    1538         1266 :       ALLOCATE (work(csvr%glob_num_csvr))
    1539              : 
    1540         1266 :       ALLOCATE (thermo_energy(csvr%loc_num_csvr))
    1541         8544 :       DO i = 1, csvr%loc_num_csvr
    1542         8544 :          thermo_energy(i) = csvr%nvt(i)%thermostat_energy
    1543              :       END DO
    1544              :       CALL get_kin_energies(csvr%map_info, csvr%loc_num_csvr, &
    1545              :                             csvr%glob_num_csvr, thermo_energy, &
    1546          422 :                             dum, para_env, array_kin=work)
    1547          422 :       DEALLOCATE (thermo_energy)
    1548              : 
    1549              :       ! If check passes then let's dump the info on the restart file
    1550          422 :       work_section => section_vals_get_subs_vals(csvr_section, "THERMOSTAT_ENERGY")
    1551          422 :       CALL dump_csvr_energy_info(work, csvr%glob_num_csvr, work_section)
    1552          422 :       DEALLOCATE (work)
    1553              : 
    1554              :       ! Thermostat Random Number info for restart
    1555          422 :       work_section => section_vals_get_subs_vals(csvr_section, "RNG_INIT")
    1556         1266 :       ALLOCATE (dwork(rng_record_length, csvr%glob_num_csvr))
    1557          422 :       dwork = 0
    1558         8544 :       DO i = 1, csvr%loc_num_csvr
    1559         8122 :          my_index = csvr%map_info%index(i)
    1560         8122 :          CALL csvr%nvt(i)%gaussian_rng_stream%dump(rng_record)
    1561         8544 :          CALL string_to_ascii(rng_record, dwork(:, my_index))
    1562              :       END DO
    1563              : 
    1564              :       !  Collect data if there was no communication in this thermostat
    1565          422 :       IF (csvr%map_info%dis_type == do_thermo_no_communication) THEN
    1566              :          ! Collect data if there was no communication in this thermostat
    1567          148 :          CALL para_env%sum(dwork)
    1568              :       ELSE
    1569              :          ! Perform some check and collect data in case of communicating thermostats
    1570          274 :          CALL communication_thermo_low2(dwork, rng_record_length, csvr%glob_num_csvr, para_env)
    1571              :       END IF
    1572          422 :       CALL section_rng_val_set(rng_section=work_section, nsize=csvr%glob_num_csvr, ascii=dwork)
    1573          422 :       DEALLOCATE (dwork)
    1574              : 
    1575          844 :    END SUBROUTINE dump_csvr_restart_info
    1576              : 
    1577              : ! **************************************************************************************************
    1578              : !> \brief Collect all information needed to dump the restart for AD_LANGEVIN
    1579              : !>      thermostat
    1580              : !> \param al ...
    1581              : !> \param para_env ...
    1582              : !> \param al_section ...
    1583              : !> \par History
    1584              : !>      10.2007 created [tlaino] - University of Zurich
    1585              : !> \author Teodoro Laino
    1586              : ! **************************************************************************************************
    1587            0 :    SUBROUTINE dump_al_restart_info(al, para_env, al_section)
    1588              : 
    1589              :       TYPE(al_system_type), POINTER                      :: al
    1590              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1591              :       TYPE(section_vals_type), POINTER                   :: al_section
    1592              : 
    1593              :       INTEGER                                            :: i
    1594              :       REAL(KIND=dp)                                      :: dum
    1595              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: t_array, work
    1596              :       TYPE(section_vals_type), POINTER                   :: work_section
    1597              : 
    1598              : ! chi and mass
    1599              : 
    1600            0 :       ALLOCATE (work(al%glob_num_al))
    1601            0 :       ALLOCATE (t_array(al%loc_num_al))
    1602              : 
    1603              :       ! copy chi into temporary
    1604            0 :       DO i = 1, al%loc_num_al
    1605            0 :          t_array(i) = al%nvt(i)%chi
    1606              :       END DO
    1607              :       ! consolidate into work
    1608              :       CALL get_kin_energies(al%map_info, al%loc_num_al, &
    1609              :                             al%glob_num_al, t_array, &
    1610            0 :                             dum, para_env, array_kin=work)
    1611              : 
    1612              :       ! If check passes then let's dump the info on the restart file
    1613            0 :       work_section => section_vals_get_subs_vals(al_section, "CHI")
    1614            0 :       CALL dump_csvr_energy_info(work, al%glob_num_al, work_section)
    1615              : 
    1616              :       ! copy mass into temporary
    1617            0 :       DO i = 1, al%loc_num_al
    1618            0 :          t_array(i) = al%nvt(i)%mass
    1619              :       END DO
    1620              :       ! consolidate into work
    1621              :       CALL get_kin_energies(al%map_info, al%loc_num_al, &
    1622              :                             al%glob_num_al, t_array, &
    1623            0 :                             dum, para_env, array_kin=work)
    1624              : 
    1625              :       ! If check passes then let's dump the info on the restart file
    1626            0 :       work_section => section_vals_get_subs_vals(al_section, "MASS")
    1627            0 :       CALL dump_csvr_energy_info(work, al%glob_num_al, work_section)
    1628              : 
    1629            0 :       DEALLOCATE (t_array)
    1630            0 :       DEALLOCATE (work)
    1631              : 
    1632            0 :    END SUBROUTINE dump_al_restart_info
    1633              : 
    1634              : ! **************************************************************************************************
    1635              : !> \brief Collect all information needed to dump the restart for GLE
    1636              : !>      thermostat
    1637              : !> \param gle ...
    1638              : !> \param para_env ...
    1639              : !> \param gle_section ...
    1640              : !> \author MI
    1641              : ! **************************************************************************************************
    1642           20 :    SUBROUTINE dump_gle_restart_info(gle, para_env, gle_section)
    1643              : 
    1644              :       TYPE(gle_type), POINTER                            :: gle
    1645              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1646              :       TYPE(section_vals_type), POINTER                   :: gle_section
    1647              : 
    1648              :       CHARACTER(LEN=rng_record_length)                   :: rng_record
    1649              :       INTEGER                                            :: counter, glob_num, i, iproc, j, loc_num
    1650              :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: dwork
    1651           20 :       INTEGER, DIMENSION(:), POINTER                     :: gle_per_proc, index
    1652              :       REAL(dp)                                           :: dum
    1653           20 :       REAL(dp), DIMENSION(:), POINTER                    :: s_tmp
    1654              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: thermo_energy
    1655              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: work
    1656              :       TYPE(section_vals_type), POINTER                   :: work_section
    1657              : 
    1658              : ! Thermostat Energies
    1659              : 
    1660           60 :       ALLOCATE (work(gle%glob_num_gle))
    1661           60 :       ALLOCATE (thermo_energy(gle%loc_num_gle))
    1662         3260 :       DO i = 1, gle%loc_num_gle
    1663         3260 :          thermo_energy(i) = gle%nvt(i)%thermostat_energy
    1664              :       END DO
    1665              :       CALL get_kin_energies(gle%map_info, gle%loc_num_gle, &
    1666              :                             gle%glob_num_gle, thermo_energy, &
    1667           20 :                             dum, para_env, array_kin=work)
    1668           20 :       DEALLOCATE (thermo_energy)
    1669              : 
    1670              :       ! If check passes then let's dump the info on the restart file
    1671           20 :       work_section => section_vals_get_subs_vals(gle_section, "THERMOSTAT_ENERGY")
    1672           20 :       CALL dump_csvr_energy_info(work, gle%glob_num_gle, work_section)
    1673           20 :       DEALLOCATE (work)
    1674              : 
    1675              :       ! Thermostat Random Number info for restart
    1676           20 :       work_section => section_vals_get_subs_vals(gle_section, "RNG_INIT")
    1677           20 :       glob_num = gle%glob_num_gle
    1678           20 :       loc_num = gle%loc_num_gle
    1679           60 :       ALLOCATE (dwork(rng_record_length, glob_num))
    1680           20 :       dwork = 0
    1681         3260 :       DO i = 1, loc_num
    1682         3240 :          j = gle%map_info%index(i)
    1683         3240 :          CALL gle%nvt(i)%gaussian_rng_stream%dump(rng_record)
    1684         3260 :          CALL string_to_ascii(rng_record, dwork(:, j))
    1685              :       END DO
    1686              : 
    1687              :       !  Collect data if there was no communication in this thermostat
    1688           20 :       IF (gle%map_info%dis_type == do_thermo_no_communication) THEN
    1689              :          ! Collect data if there was no communication in this thermostat
    1690           20 :          CALL para_env%sum(dwork)
    1691              :       ELSE
    1692              :          ! Perform some check and collect data in case of communicating thermostats
    1693            0 :          CALL communication_thermo_low2(dwork, rng_record_length, glob_num, para_env)
    1694              :       END IF
    1695           20 :       CALL section_rng_val_set(rng_section=work_section, nsize=glob_num, ascii=dwork)
    1696           20 :       DEALLOCATE (dwork)
    1697              : 
    1698           60 :       ALLOCATE (gle_per_proc(para_env%num_pe))
    1699           60 :       gle_per_proc(:) = 0
    1700           60 :       CALL para_env%allgather(gle%loc_num_gle, gle_per_proc)
    1701              : 
    1702              :       ! Thermostat S variable info for restart
    1703           20 :       NULLIFY (s_tmp)
    1704           60 :       ALLOCATE (s_tmp((gle%ndim)*gle%glob_num_gle))
    1705        32420 :       s_tmp = 0.0_dp
    1706              : 
    1707           20 :       NULLIFY (work, index)
    1708           60 :       DO iproc = 1, para_env%num_pe
    1709           40 :          CALL reallocate(work, 1, gle_per_proc(iproc)*(gle%ndim))
    1710           40 :          CALL reallocate(index, 1, gle_per_proc(iproc))
    1711           40 :          IF (para_env%mepos == (iproc - 1)) THEN
    1712         3260 :             INDEX(:) = 0
    1713           20 :             counter = 0
    1714          120 :             DO i = 1, gle%ndim
    1715        16320 :                DO j = 1, gle%loc_num_gle
    1716        16200 :                   counter = counter + 1
    1717        16200 :                   work(counter) = gle%nvt(j)%s(i)
    1718        16300 :                   INDEX(j) = gle%map_info%index(j)
    1719              :                END DO
    1720              :             END DO
    1721              :          ELSE
    1722        16220 :             work(:) = 0.0_dp
    1723              :          END IF
    1724        64840 :          CALL para_env%bcast(work, iproc - 1)
    1725        13000 :          CALL para_env%bcast(index, iproc - 1)
    1726           40 :          counter = 0
    1727          260 :          DO i = 1, gle%ndim
    1728        32640 :             DO j = 1, gle_per_proc(iproc)
    1729        32400 :                counter = counter + 1
    1730        32600 :                s_tmp((INDEX(j) - 1)*(gle%ndim) + i) = work(counter)
    1731              :             END DO
    1732              :          END DO
    1733              :       END DO
    1734              : 
    1735           20 :       IF (SIZE(s_tmp) > 0) THEN
    1736           20 :          work_section => section_vals_get_subs_vals(gle_section, "S")
    1737           20 :          CALL section_vals_val_set(work_section, "_DEFAULT_KEYWORD_", r_vals_ptr=s_tmp)
    1738              :       ELSE
    1739            0 :          DEALLOCATE (s_tmp)
    1740              :       END IF
    1741              : 
    1742           20 :       DEALLOCATE (gle_per_proc)
    1743           20 :       DEALLOCATE (work)
    1744           20 :       DEALLOCATE (index)
    1745              : 
    1746           40 :    END SUBROUTINE dump_gle_restart_info
    1747              : 
    1748              : ! **************************************************************************************************
    1749              : !> \brief Collect all information needed to dump the restart for Nose-Hoover
    1750              : !>      thermostat
    1751              : !> \param nhc ...
    1752              : !> \param para_env ...
    1753              : !> \param eta ...
    1754              : !> \param veta ...
    1755              : !> \param fnhc ...
    1756              : !> \param mnhc ...
    1757              : !> \par History
    1758              : !>      10.2007 created [tlaino] - University of Zurich
    1759              : !> \author Teodoro Laino
    1760              : ! **************************************************************************************************
    1761          962 :    SUBROUTINE collect_nose_restart_info(nhc, para_env, eta, veta, fnhc, mnhc)
    1762              : 
    1763              :       TYPE(lnhc_parameters_type), POINTER                :: nhc
    1764              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1765              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: eta, veta, fnhc, mnhc
    1766              : 
    1767              :       INTEGER                                            :: counter, i, iproc, j, nhc_len, num_nhc, &
    1768              :                                                             numneed, tot_nhcneed
    1769          962 :       INTEGER, DIMENSION(:), POINTER                     :: index, nhc_per_proc
    1770          962 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: work
    1771              :       TYPE(map_info_type), POINTER                       :: map_info
    1772              : 
    1773          962 :       nhc_len = SIZE(nhc%nvt, 1)
    1774          962 :       num_nhc = nhc%loc_num_nhc
    1775          962 :       numneed = num_nhc
    1776          962 :       map_info => nhc%map_info
    1777         2886 :       ALLOCATE (nhc_per_proc(para_env%num_pe))
    1778         2886 :       nhc_per_proc(:) = 0
    1779              : 
    1780         2886 :       CALL para_env%allgather(numneed, nhc_per_proc)
    1781          962 :       tot_nhcneed = nhc%glob_num_nhc
    1782              : 
    1783          962 :       NULLIFY (work, index)
    1784              :       !-----------------------------------------------------------------------------
    1785              :       !-----------------------------------------------------------------------------
    1786              :       ! nhc%eta
    1787              :       !-----------------------------------------------------------------------------
    1788         2886 :       ALLOCATE (eta(tot_nhcneed*nhc_len))
    1789         2886 :       DO iproc = 1, para_env%num_pe
    1790         1924 :          CALL reallocate(work, 1, nhc_per_proc(iproc)*nhc_len)
    1791         1924 :          CALL reallocate(index, 1, nhc_per_proc(iproc))
    1792         1924 :          IF (para_env%mepos == (iproc - 1)) THEN
    1793        68909 :             INDEX(:) = 0
    1794              :             counter = 0
    1795         4662 :             DO i = 1, nhc_len
    1796       257091 :                DO j = 1, num_nhc
    1797       252429 :                   counter = counter + 1
    1798       252429 :                   work(counter) = nhc%nvt(i, j)%eta
    1799       256129 :                   INDEX(j) = map_info%index(j)
    1800              :                END DO
    1801              :             END DO
    1802              :          ELSE
    1803       253391 :             work(:) = 0.0_dp
    1804              :          END IF
    1805      1011640 :          CALL para_env%bcast(work, iproc - 1)
    1806       273712 :          CALL para_env%bcast(index, iproc - 1)
    1807         1924 :          counter = 0
    1808        10286 :          DO i = 1, nhc_len
    1809       514182 :             DO j = 1, nhc_per_proc(iproc)
    1810       504858 :                counter = counter + 1
    1811       512258 :                eta((INDEX(j) - 1)*nhc_len + i) = work(counter)
    1812              :             END DO
    1813              :          END DO
    1814              :       END DO
    1815              :       !-----------------------------------------------------------------------------
    1816              :       !-----------------------------------------------------------------------------
    1817              :       ! nhc%veta
    1818              :       !-----------------------------------------------------------------------------
    1819         1924 :       ALLOCATE (veta(tot_nhcneed*nhc_len))
    1820         2886 :       DO iproc = 1, para_env%num_pe
    1821         1924 :          CALL reallocate(work, 1, nhc_per_proc(iproc)*nhc_len)
    1822         1924 :          CALL reallocate(index, 1, nhc_per_proc(iproc))
    1823         1924 :          IF (para_env%mepos == (iproc - 1)) THEN
    1824        68909 :             INDEX(:) = 0
    1825              :             counter = 0
    1826         4662 :             DO i = 1, nhc_len
    1827       257091 :                DO j = 1, num_nhc
    1828       252429 :                   counter = counter + 1
    1829       252429 :                   work(counter) = nhc%nvt(i, j)%v
    1830       256129 :                   INDEX(j) = map_info%index(j)
    1831              :                END DO
    1832              :             END DO
    1833              :          ELSE
    1834       253391 :             work(:) = 0.0_dp
    1835              :          END IF
    1836      1011640 :          CALL para_env%bcast(work, iproc - 1)
    1837       273712 :          CALL para_env%bcast(index, iproc - 1)
    1838         1924 :          counter = 0
    1839        10286 :          DO i = 1, nhc_len
    1840       514182 :             DO j = 1, nhc_per_proc(iproc)
    1841       504858 :                counter = counter + 1
    1842       512258 :                veta((INDEX(j) - 1)*nhc_len + i) = work(counter)
    1843              :             END DO
    1844              :          END DO
    1845              :       END DO
    1846              :       !-----------------------------------------------------------------------------
    1847              :       !-----------------------------------------------------------------------------
    1848              :       ! nhc%force
    1849              :       !-----------------------------------------------------------------------------
    1850         1924 :       ALLOCATE (fnhc(tot_nhcneed*nhc_len))
    1851         2886 :       DO iproc = 1, para_env%num_pe
    1852         1924 :          CALL reallocate(work, 1, nhc_per_proc(iproc)*nhc_len)
    1853         1924 :          CALL reallocate(index, 1, nhc_per_proc(iproc))
    1854         1924 :          IF (para_env%mepos == (iproc - 1)) THEN
    1855        68909 :             INDEX(:) = 0
    1856              :             counter = 0
    1857         4662 :             DO i = 1, nhc_len
    1858       257091 :                DO j = 1, num_nhc
    1859       252429 :                   counter = counter + 1
    1860       252429 :                   work(counter) = nhc%nvt(i, j)%f
    1861       256129 :                   INDEX(j) = map_info%index(j)
    1862              :                END DO
    1863              :             END DO
    1864              :          ELSE
    1865       253391 :             work(:) = 0.0_dp
    1866              :          END IF
    1867      1011640 :          CALL para_env%bcast(work, iproc - 1)
    1868       273712 :          CALL para_env%bcast(index, iproc - 1)
    1869         1924 :          counter = 0
    1870        10286 :          DO i = 1, nhc_len
    1871       514182 :             DO j = 1, nhc_per_proc(iproc)
    1872       504858 :                counter = counter + 1
    1873       512258 :                fnhc((INDEX(j) - 1)*nhc_len + i) = work(counter)
    1874              :             END DO
    1875              :          END DO
    1876              :       END DO
    1877              :       !-----------------------------------------------------------------------------
    1878              :       !-----------------------------------------------------------------------------
    1879              :       ! nhc%mass
    1880              :       !-----------------------------------------------------------------------------
    1881         1924 :       ALLOCATE (mnhc(tot_nhcneed*nhc_len))
    1882         2886 :       DO iproc = 1, para_env%num_pe
    1883         1924 :          CALL reallocate(work, 1, nhc_per_proc(iproc)*nhc_len)
    1884         1924 :          CALL reallocate(index, 1, nhc_per_proc(iproc))
    1885         1924 :          IF (para_env%mepos == (iproc - 1)) THEN
    1886        68909 :             INDEX(:) = 0
    1887              :             counter = 0
    1888         4662 :             DO i = 1, nhc_len
    1889       257091 :                DO j = 1, num_nhc
    1890       252429 :                   counter = counter + 1
    1891       252429 :                   work(counter) = nhc%nvt(i, j)%mass
    1892       256129 :                   INDEX(j) = map_info%index(j)
    1893              :                END DO
    1894              :             END DO
    1895              :          ELSE
    1896       253391 :             work(:) = 0.0_dp
    1897              :          END IF
    1898      1011640 :          CALL para_env%bcast(work, iproc - 1)
    1899       273712 :          CALL para_env%bcast(index, iproc - 1)
    1900         1924 :          counter = 0
    1901        10286 :          DO i = 1, nhc_len
    1902       514182 :             DO j = 1, nhc_per_proc(iproc)
    1903       504858 :                counter = counter + 1
    1904       512258 :                mnhc((INDEX(j) - 1)*nhc_len + i) = work(counter)
    1905              :             END DO
    1906              :          END DO
    1907              :       END DO
    1908              : 
    1909          962 :       DEALLOCATE (work)
    1910          962 :       DEALLOCATE (index)
    1911          962 :       DEALLOCATE (nhc_per_proc)
    1912              : 
    1913          962 :    END SUBROUTINE collect_nose_restart_info
    1914              : 
    1915              : ! **************************************************************************************************
    1916              : !> \brief routine to dump NEB coordinates and velocities section.. fast implementation
    1917              : !> \param coord_section ...
    1918              : !> \param array ...
    1919              : !> \param narray ...
    1920              : !> \param nsize ...
    1921              : !> \param nfield ...
    1922              : !> \param particle_set ...
    1923              : !> \param conv_factor ...
    1924              : !> \par History
    1925              : !>      12.2006 created [teo]
    1926              : !> \author Teodoro Laino
    1927              : ! **************************************************************************************************
    1928         7272 :    SUBROUTINE section_neb_coord_val_set(coord_section, array, narray, nsize, nfield, &
    1929              :                                         particle_set, conv_factor)
    1930              : 
    1931              :       TYPE(section_vals_type), POINTER                   :: coord_section
    1932              :       REAL(KIND=dp), DIMENSION(*)                        :: array
    1933              :       INTEGER, INTENT(IN)                                :: narray, nsize, nfield
    1934              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1935              :       REAL(KIND=dp)                                      :: conv_factor
    1936              : 
    1937              :       INTEGER                                            :: ik, irk, Nlist
    1938         7272 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: my_c
    1939              :       TYPE(cp_sll_val_type), POINTER                     :: new_pos, vals
    1940              :       TYPE(section_type), POINTER                        :: section
    1941              :       TYPE(val_type), POINTER                            :: my_val, old_val
    1942              : 
    1943         7272 :       NULLIFY (my_val, old_val, section, vals)
    1944            0 :       CPASSERT(ASSOCIATED(coord_section))
    1945         7272 :       CPASSERT(coord_section%ref_count > 0)
    1946         7272 :       section => coord_section%section
    1947         7272 :       ik = section_get_keyword_index(section, "_DEFAULT_KEYWORD_")
    1948         7272 :       IF (ik == -2) THEN
    1949              :          CALL cp_abort(__LOCATION__, "section "//TRIM(section%name)//" does not contain keyword "// &
    1950            0 :                        "_DEFAULT_KEYWORD_")
    1951              :       END IF
    1952          408 :       DO
    1953         7680 :          IF (SIZE(coord_section%values, 2) == 1) EXIT
    1954          408 :          CALL section_vals_add_values(coord_section)
    1955              :       END DO
    1956         7272 :       vals => coord_section%values(ik, 1)%list
    1957         7272 :       Nlist = 0
    1958         7272 :       IF (ASSOCIATED(vals)) THEN
    1959         6864 :          Nlist = cp_sll_val_get_length(vals)
    1960              :       END IF
    1961       270564 :       DO irk = 1, nsize/nfield
    1962       789876 :          ALLOCATE (my_c(nfield))
    1963       263292 :          IF (nfield == 3) THEN
    1964      1052688 :             my_c(1:3) = get_particle_pos_or_vel(irk, particle_set, array(1:narray))
    1965      1052688 :             my_c(1:3) = my_c(1:3)*conv_factor
    1966              :          ELSE
    1967          120 :             my_c(1) = array(irk)
    1968              :          END IF
    1969       263292 :          CALL val_create(my_val, r_vals_ptr=my_c)
    1970              : 
    1971       263292 :          IF (Nlist /= 0) THEN
    1972       241300 :             IF (irk == 1) THEN
    1973         6864 :                new_pos => vals
    1974              :             ELSE
    1975       234436 :                new_pos => new_pos%rest
    1976              :             END IF
    1977       241300 :             old_val => new_pos%first_el
    1978       241300 :             CALL val_release(old_val)
    1979       241300 :             new_pos%first_el => my_val
    1980              :          ELSE
    1981        21992 :             IF (irk == 1) THEN
    1982          408 :                NULLIFY (new_pos)
    1983          408 :                CALL cp_sll_val_create(new_pos, first_el=my_val)
    1984          408 :                vals => new_pos
    1985              :             ELSE
    1986        21584 :                NULLIFY (new_pos%rest)
    1987        21584 :                CALL cp_sll_val_create(new_pos%rest, first_el=my_val)
    1988        21584 :                new_pos => new_pos%rest
    1989              :             END IF
    1990              :          END IF
    1991       270564 :          NULLIFY (my_val)
    1992              :       END DO
    1993              : 
    1994         7272 :       coord_section%values(ik, 1)%list => vals
    1995              : 
    1996         7272 :    END SUBROUTINE section_neb_coord_val_set
    1997              : 
    1998              : ! **************************************************************************************************
    1999              : !> \brief Set the nose structure like restart
    2000              : !> \param work_section ...
    2001              : !> \param eta ...
    2002              : !> \param veta ...
    2003              : !> \param fnhc ...
    2004              : !> \param mnhc ...
    2005              : !> \par History
    2006              : !>      01.2006 created [teo]
    2007              : !> \author Teodoro Laino
    2008              : ! **************************************************************************************************
    2009         1416 :    SUBROUTINE set_template_restart(work_section, eta, veta, fnhc, mnhc)
    2010              : 
    2011              :       TYPE(section_vals_type), POINTER                   :: work_section
    2012              :       REAL(KIND=dp), DIMENSION(:), OPTIONAL, POINTER     :: eta, veta, fnhc, mnhc
    2013              : 
    2014              :       TYPE(section_vals_type), POINTER                   :: coord, force, mass, velocity
    2015              : 
    2016         1416 :       NULLIFY (coord, force, velocity, mass)
    2017         1416 :       IF (PRESENT(eta)) THEN
    2018         1014 :          IF (SIZE(eta) > 0) THEN
    2019         1014 :             coord => section_vals_get_subs_vals(work_section, "COORD")
    2020         1014 :             CALL section_vals_val_set(coord, "_DEFAULT_KEYWORD_", r_vals_ptr=eta)
    2021              :          ELSE
    2022            0 :             DEALLOCATE (eta)
    2023              :          END IF
    2024              :       END IF
    2025         1416 :       IF (PRESENT(veta)) THEN
    2026         1416 :          IF (SIZE(veta) > 0) THEN
    2027         1416 :             velocity => section_vals_get_subs_vals(work_section, "VELOCITY")
    2028         1416 :             CALL section_vals_val_set(velocity, "_DEFAULT_KEYWORD_", r_vals_ptr=veta)
    2029              :          ELSE
    2030            0 :             DEALLOCATE (veta)
    2031              :          END IF
    2032              :       END IF
    2033         1416 :       IF (PRESENT(fnhc)) THEN
    2034         1014 :          IF (SIZE(fnhc) > 0) THEN
    2035         1014 :             force => section_vals_get_subs_vals(work_section, "FORCE")
    2036         1014 :             CALL section_vals_val_set(force, "_DEFAULT_KEYWORD_", r_vals_ptr=fnhc)
    2037              :          ELSE
    2038            0 :             DEALLOCATE (fnhc)
    2039              :          END IF
    2040              :       END IF
    2041         1416 :       IF (PRESENT(mnhc)) THEN
    2042         1416 :          IF (SIZE(mnhc) > 0) THEN
    2043         1416 :             mass => section_vals_get_subs_vals(work_section, "MASS")
    2044         1416 :             CALL section_vals_val_set(mass, "_DEFAULT_KEYWORD_", r_vals_ptr=mnhc)
    2045              :          ELSE
    2046            0 :             DEALLOCATE (mnhc)
    2047              :          END IF
    2048              :       END IF
    2049              : 
    2050         1416 :    END SUBROUTINE set_template_restart
    2051              : 
    2052              : ! **************************************************************************************************
    2053              : !> \brief routine to dump hills information during metadynamics run
    2054              : !> \param ss_section ...
    2055              : !> \param meta_env ...
    2056              : !> \par History
    2057              : !>      02.2006 created [teo]
    2058              : !> \author Teodoro Laino
    2059              : ! **************************************************************************************************
    2060          784 :    SUBROUTINE meta_hills_val_set_ss(ss_section, meta_env)
    2061              : 
    2062              :       TYPE(section_vals_type), POINTER                   :: ss_section
    2063              :       TYPE(meta_env_type), POINTER                       :: meta_env
    2064              : 
    2065              :       INTEGER                                            :: ik, irk, lsize, Nlist
    2066          784 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: ss_val
    2067              :       TYPE(cp_sll_val_type), POINTER                     :: new_pos, vals
    2068              :       TYPE(section_type), POINTER                        :: section
    2069              :       TYPE(val_type), POINTER                            :: my_val, old_val
    2070              : 
    2071          784 :       NULLIFY (my_val, old_val, section, vals)
    2072            0 :       CPASSERT(ASSOCIATED(ss_section))
    2073          784 :       CPASSERT(ss_section%ref_count > 0)
    2074          784 :       section => ss_section%section
    2075          784 :       ik = section_get_keyword_index(section, "_DEFAULT_KEYWORD_")
    2076          784 :       IF (ik == -2) THEN
    2077              :          CALL cp_abort(__LOCATION__, "section "//TRIM(section%name)//" does not contain keyword "// &
    2078            0 :                        "_DEFAULT_KEYWORD_")
    2079              :       END IF
    2080          100 :       DO
    2081          884 :          IF (SIZE(ss_section%values, 2) == 1) EXIT
    2082          100 :          CALL section_vals_add_values(ss_section)
    2083              :       END DO
    2084          784 :       vals => ss_section%values(ik, 1)%list
    2085          784 :       Nlist = 0
    2086          784 :       IF (ASSOCIATED(vals)) THEN
    2087          684 :          Nlist = cp_sll_val_get_length(vals)
    2088              :       END IF
    2089          784 :       lsize = SIZE(meta_env%hills_env%ss_history, 1)
    2090        12938 :       DO irk = 1, meta_env%hills_env%n_hills
    2091        36462 :          ALLOCATE (ss_val(lsize))
    2092              :          ! Always stored in A.U.
    2093        49256 :          ss_val = meta_env%hills_env%ss_history(:, irk)
    2094        12154 :          CALL val_create(my_val, r_vals_ptr=ss_val)
    2095              : 
    2096        12154 :          IF (irk <= Nlist) THEN
    2097        10980 :             IF (irk == 1) THEN
    2098          684 :                new_pos => vals
    2099              :             ELSE
    2100        10296 :                new_pos => new_pos%rest
    2101              :             END IF
    2102        10980 :             old_val => new_pos%first_el
    2103        10980 :             CALL val_release(old_val)
    2104        10980 :             new_pos%first_el => my_val
    2105              :          ELSE
    2106         1174 :             IF (irk == 1) THEN
    2107          100 :                NULLIFY (new_pos)
    2108          100 :                CALL cp_sll_val_create(new_pos, first_el=my_val)
    2109          100 :                vals => new_pos
    2110              :             ELSE
    2111         1074 :                NULLIFY (new_pos%rest)
    2112         1074 :                CALL cp_sll_val_create(new_pos%rest, first_el=my_val)
    2113         1074 :                new_pos => new_pos%rest
    2114              :             END IF
    2115              :          END IF
    2116        12938 :          NULLIFY (my_val)
    2117              :       END DO
    2118              : 
    2119          784 :       ss_section%values(ik, 1)%list => vals
    2120              : 
    2121          784 :    END SUBROUTINE meta_hills_val_set_ss
    2122              : 
    2123              : ! **************************************************************************************************
    2124              : !> \brief routine to dump hills information during metadynamics run
    2125              : !> \param ds_section ...
    2126              : !> \param meta_env ...
    2127              : !> \par History
    2128              : !>      02.2006 created [teo]
    2129              : !> \author Teodoro Laino
    2130              : ! **************************************************************************************************
    2131          784 :    SUBROUTINE meta_hills_val_set_ds(ds_section, meta_env)
    2132              : 
    2133              :       TYPE(section_vals_type), POINTER                   :: ds_section
    2134              :       TYPE(meta_env_type), POINTER                       :: meta_env
    2135              : 
    2136              :       INTEGER                                            :: ik, irk, lsize, Nlist
    2137          784 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: ds_val
    2138              :       TYPE(cp_sll_val_type), POINTER                     :: new_pos, vals
    2139              :       TYPE(section_type), POINTER                        :: section
    2140              :       TYPE(val_type), POINTER                            :: my_val, old_val
    2141              : 
    2142          784 :       NULLIFY (my_val, old_val, section, vals)
    2143            0 :       CPASSERT(ASSOCIATED(ds_section))
    2144          784 :       CPASSERT(ds_section%ref_count > 0)
    2145          784 :       section => ds_section%section
    2146          784 :       ik = section_get_keyword_index(section, "_DEFAULT_KEYWORD_")
    2147          784 :       IF (ik == -2) THEN
    2148              :          CALL cp_abort(__LOCATION__, "section "//TRIM(section%name)//" does not contain keyword "// &
    2149            0 :                        "_DEFAULT_KEYWORD_")
    2150              :       END IF
    2151          100 :       DO
    2152          884 :          IF (SIZE(ds_section%values, 2) == 1) EXIT
    2153          100 :          CALL section_vals_add_values(ds_section)
    2154              :       END DO
    2155          784 :       vals => ds_section%values(ik, 1)%list
    2156          784 :       Nlist = 0
    2157          784 :       IF (ASSOCIATED(vals)) THEN
    2158          684 :          Nlist = cp_sll_val_get_length(vals)
    2159              :       END IF
    2160          784 :       lsize = SIZE(meta_env%hills_env%delta_s_history, 1)
    2161        12938 :       DO irk = 1, meta_env%hills_env%n_hills
    2162        36462 :          ALLOCATE (ds_val(lsize))
    2163              :          ! Always stored in A.U.
    2164        49256 :          ds_val = meta_env%hills_env%delta_s_history(:, irk)
    2165        12154 :          CALL val_create(my_val, r_vals_ptr=ds_val)
    2166              : 
    2167        12154 :          IF (irk <= Nlist) THEN
    2168        10980 :             IF (irk == 1) THEN
    2169          684 :                new_pos => vals
    2170              :             ELSE
    2171        10296 :                new_pos => new_pos%rest
    2172              :             END IF
    2173        10980 :             old_val => new_pos%first_el
    2174        10980 :             CALL val_release(old_val)
    2175        10980 :             new_pos%first_el => my_val
    2176              :          ELSE
    2177         1174 :             IF (irk == 1) THEN
    2178          100 :                NULLIFY (new_pos)
    2179          100 :                CALL cp_sll_val_create(new_pos, first_el=my_val)
    2180          100 :                vals => new_pos
    2181              :             ELSE
    2182         1074 :                NULLIFY (new_pos%rest)
    2183         1074 :                CALL cp_sll_val_create(new_pos%rest, first_el=my_val)
    2184         1074 :                new_pos => new_pos%rest
    2185              :             END IF
    2186              :          END IF
    2187        12938 :          NULLIFY (my_val)
    2188              :       END DO
    2189              : 
    2190          784 :       ds_section%values(ik, 1)%list => vals
    2191              : 
    2192          784 :    END SUBROUTINE meta_hills_val_set_ds
    2193              : 
    2194              : ! **************************************************************************************************
    2195              : !> \brief routine to dump hills information during metadynamics run
    2196              : !> \param ww_section ...
    2197              : !> \param meta_env ...
    2198              : !> \par History
    2199              : !>      02.2006 created [teo]
    2200              : !> \author Teodoro Laino
    2201              : ! **************************************************************************************************
    2202          784 :    SUBROUTINE meta_hills_val_set_ww(ww_section, meta_env)
    2203              : 
    2204              :       TYPE(section_vals_type), POINTER                   :: ww_section
    2205              :       TYPE(meta_env_type), POINTER                       :: meta_env
    2206              : 
    2207              :       INTEGER                                            :: ik, irk, lsize, Nlist
    2208              :       TYPE(cp_sll_val_type), POINTER                     :: new_pos, vals
    2209              :       TYPE(section_type), POINTER                        :: section
    2210              :       TYPE(val_type), POINTER                            :: my_val, old_val
    2211              : 
    2212          784 :       NULLIFY (my_val, old_val, section, vals)
    2213          784 :       CPASSERT(ASSOCIATED(ww_section))
    2214          784 :       CPASSERT(ww_section%ref_count > 0)
    2215          784 :       section => ww_section%section
    2216          784 :       ik = section_get_keyword_index(section, "_DEFAULT_KEYWORD_")
    2217          784 :       IF (ik == -2) THEN
    2218              :          CALL cp_abort(__LOCATION__, "section "//TRIM(section%name)//" does not contain keyword "// &
    2219            0 :                        "_DEFAULT_KEYWORD_")
    2220              :       END IF
    2221          100 :       DO
    2222          884 :          IF (SIZE(ww_section%values, 2) == 1) EXIT
    2223          100 :          CALL section_vals_add_values(ww_section)
    2224              :       END DO
    2225          784 :       vals => ww_section%values(ik, 1)%list
    2226          784 :       Nlist = 0
    2227          784 :       IF (ASSOCIATED(vals)) THEN
    2228          684 :          Nlist = cp_sll_val_get_length(vals)
    2229              :       END IF
    2230          784 :       lsize = meta_env%hills_env%n_hills
    2231        12938 :       DO irk = 1, lsize
    2232        12154 :          CALL val_create(my_val, r_val=meta_env%hills_env%ww_history(irk))
    2233              : 
    2234        12154 :          IF (irk <= Nlist) THEN
    2235        10980 :             IF (irk == 1) THEN
    2236          684 :                new_pos => vals
    2237              :             ELSE
    2238        10296 :                new_pos => new_pos%rest
    2239              :             END IF
    2240        10980 :             old_val => new_pos%first_el
    2241        10980 :             CALL val_release(old_val)
    2242        10980 :             new_pos%first_el => my_val
    2243              :          ELSE
    2244         1174 :             IF (irk == 1) THEN
    2245          100 :                NULLIFY (new_pos)
    2246          100 :                CALL cp_sll_val_create(new_pos, first_el=my_val)
    2247          100 :                vals => new_pos
    2248              :             ELSE
    2249         1074 :                NULLIFY (new_pos%rest)
    2250         1074 :                CALL cp_sll_val_create(new_pos%rest, first_el=my_val)
    2251         1074 :                new_pos => new_pos%rest
    2252              :             END IF
    2253              :          END IF
    2254        12938 :          NULLIFY (my_val)
    2255              :       END DO
    2256              : 
    2257          784 :       ww_section%values(ik, 1)%list => vals
    2258              : 
    2259          784 :    END SUBROUTINE meta_hills_val_set_ww
    2260              : 
    2261              : ! **************************************************************************************************
    2262              : !> \brief routine to dump hills information during metadynamics run
    2263              : !> \param invdt_section ...
    2264              : !> \param meta_env ...
    2265              : !> \par History
    2266              : !>      12.2009 created [seb]
    2267              : !> \author SC
    2268              : ! **************************************************************************************************
    2269            2 :    SUBROUTINE meta_hills_val_set_dt(invdt_section, meta_env)
    2270              : 
    2271              :       TYPE(section_vals_type), POINTER                   :: invdt_section
    2272              :       TYPE(meta_env_type), POINTER                       :: meta_env
    2273              : 
    2274              :       INTEGER                                            :: ik, irk, lsize, Nlist
    2275              :       TYPE(cp_sll_val_type), POINTER                     :: new_pos, vals
    2276              :       TYPE(section_type), POINTER                        :: section
    2277              :       TYPE(val_type), POINTER                            :: my_val, old_val
    2278              : 
    2279            2 :       NULLIFY (my_val, old_val, section, vals)
    2280            2 :       CPASSERT(ASSOCIATED(invdt_section))
    2281            2 :       CPASSERT(invdt_section%ref_count > 0)
    2282            2 :       section => invdt_section%section
    2283            2 :       ik = section_get_keyword_index(section, "_DEFAULT_KEYWORD_")
    2284            2 :       IF (ik == -2) THEN
    2285              :          CALL cp_abort(__LOCATION__, "section "//TRIM(section%name)//" does not contain keyword "// &
    2286            0 :                        "_DEFAULT_KEYWORD_")
    2287              :       END IF
    2288            2 :       DO
    2289            4 :          IF (SIZE(invdt_section%values, 2) == 1) EXIT
    2290            2 :          CALL section_vals_add_values(invdt_section)
    2291              :       END DO
    2292            2 :       vals => invdt_section%values(ik, 1)%list
    2293            2 :       Nlist = 0
    2294            2 :       IF (ASSOCIATED(vals)) THEN
    2295            0 :          Nlist = cp_sll_val_get_length(vals)
    2296              :       END IF
    2297            2 :       lsize = meta_env%hills_env%n_hills
    2298            6 :       DO irk = 1, lsize
    2299            4 :          CALL val_create(my_val, r_val=meta_env%hills_env%invdt_history(irk))
    2300              : 
    2301            4 :          IF (irk <= Nlist) THEN
    2302            0 :             IF (irk == 1) THEN
    2303            0 :                new_pos => vals
    2304              :             ELSE
    2305            0 :                new_pos => new_pos%rest
    2306              :             END IF
    2307            0 :             old_val => new_pos%first_el
    2308            0 :             CALL val_release(old_val)
    2309            0 :             new_pos%first_el => my_val
    2310              :          ELSE
    2311            4 :             IF (irk == 1) THEN
    2312            2 :                NULLIFY (new_pos)
    2313            2 :                CALL cp_sll_val_create(new_pos, first_el=my_val)
    2314            2 :                vals => new_pos
    2315              :             ELSE
    2316            2 :                NULLIFY (new_pos%rest)
    2317            2 :                CALL cp_sll_val_create(new_pos%rest, first_el=my_val)
    2318            2 :                new_pos => new_pos%rest
    2319              :             END IF
    2320              :          END IF
    2321            6 :          NULLIFY (my_val)
    2322              :       END DO
    2323            2 :       invdt_section%values(ik, 1)%list => vals
    2324            2 :    END SUBROUTINE meta_hills_val_set_dt
    2325              : 
    2326              : ! **************************************************************************************************
    2327              : !> \brief   Write all input sections scaling in size with the number of atoms
    2328              : !>          in the system to an external file in binary format
    2329              : !> \param output_unit binary file to write to
    2330              : !> \param log_unit unit for logging debug information
    2331              : !> \param root_section ...
    2332              : !> \param md_env ...
    2333              : !> \param force_env ...
    2334              : !> \par History
    2335              : !>      - Creation (10.02.2011,MK)
    2336              : !> \author  Matthias Krack (MK)
    2337              : !> \version 1.0
    2338              : ! **************************************************************************************************
    2339          272 :    SUBROUTINE write_binary_restart(output_unit, log_unit, root_section, md_env, force_env)
    2340              : 
    2341              :       INTEGER, INTENT(IN)                                :: output_unit, log_unit
    2342              :       TYPE(section_vals_type), POINTER                   :: root_section
    2343              :       TYPE(md_environment_type), OPTIONAL, POINTER       :: md_env
    2344              :       TYPE(force_env_type), OPTIONAL, POINTER            :: force_env
    2345              : 
    2346              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'write_binary_restart'
    2347              : 
    2348              :       CHARACTER(LEN=default_path_length)                 :: binary_restart_file_name
    2349              :       CHARACTER(LEN=default_string_length)               :: section_label
    2350              :       INTEGER :: handle, iatom, icore, ikind, imolecule, ishell, istat, n_char_size, n_dp_size, &
    2351              :          n_int_size, natom, natomkind, ncore, nhc_size, nmolecule, nmoleculekind, nshell, &
    2352              :          print_level, run_type
    2353          272 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: ibuf, imol
    2354              :       LOGICAL                                            :: print_info, write_velocities
    2355          272 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: rbuf
    2356              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
    2357              :       TYPE(cp_subsys_type), POINTER                      :: subsys
    2358              :       TYPE(force_env_type), POINTER                      :: my_force_env
    2359              :       TYPE(lnhc_parameters_type), POINTER                :: nhc
    2360              :       TYPE(molecule_kind_list_type), POINTER             :: molecule_kinds
    2361              :       TYPE(molecule_list_type), POINTER                  :: molecules
    2362              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    2363              :       TYPE(particle_list_type), POINTER                  :: core_particles, particles, &
    2364              :                                                             shell_particles
    2365              :       TYPE(thermostat_type), POINTER                     :: thermostat_part, thermostat_shell
    2366              : 
    2367          272 :       CALL timeset(routineN, handle)
    2368              : 
    2369          272 :       NULLIFY (atomic_kinds)
    2370          272 :       NULLIFY (core_particles)
    2371          272 :       NULLIFY (molecule_kinds)
    2372          272 :       NULLIFY (molecules)
    2373          272 :       NULLIFY (my_force_env)
    2374          272 :       NULLIFY (para_env)
    2375          272 :       NULLIFY (particles)
    2376          272 :       NULLIFY (shell_particles)
    2377          272 :       NULLIFY (subsys)
    2378          272 :       NULLIFY (thermostat_part)
    2379          272 :       NULLIFY (thermostat_shell)
    2380              : 
    2381          272 :       IF (PRESENT(md_env)) THEN
    2382              :          CALL get_md_env(md_env=md_env, &
    2383              :                          force_env=my_force_env, &
    2384              :                          thermostat_part=thermostat_part, &
    2385          272 :                          thermostat_shell=thermostat_shell)
    2386            0 :       ELSE IF (PRESENT(force_env)) THEN
    2387            0 :          my_force_env => force_env
    2388              :       END IF
    2389              : 
    2390          272 :       IF (.NOT. ASSOCIATED(my_force_env)) THEN
    2391            0 :          CALL timestop(handle)
    2392            0 :          RETURN
    2393              :       END IF
    2394              : 
    2395          272 :       CALL section_vals_val_get(root_section, "GLOBAL%PRINT_LEVEL", i_val=print_level)
    2396              : 
    2397          272 :       IF (print_level > 1) THEN
    2398          272 :          print_info = .TRUE.
    2399              :       ELSE
    2400            0 :          print_info = .FALSE.
    2401              :       END IF
    2402              : 
    2403          272 :       CALL section_vals_val_get(root_section, "GLOBAL%RUN_TYPE", i_val=run_type)
    2404              :       write_velocities = ((run_type == mol_dyn_run) .OR. &
    2405              :                           (run_type == mon_car_run) .OR. &
    2406          272 :                           (run_type == pint_run))
    2407              : 
    2408              :       CALL force_env_get(force_env=my_force_env, &
    2409              :                          para_env=para_env, &
    2410          272 :                          subsys=subsys)
    2411              :       CALL cp_subsys_get(subsys, &
    2412              :                          atomic_kinds=atomic_kinds, &
    2413              :                          particles=particles, &
    2414              :                          natom=natom, &
    2415              :                          core_particles=core_particles, &
    2416              :                          ncore=ncore, &
    2417              :                          shell_particles=shell_particles, &
    2418              :                          nshell=nshell, &
    2419              :                          molecule_kinds=molecule_kinds, &
    2420          272 :                          molecules=molecules)
    2421              : 
    2422          272 :       natomkind = atomic_kinds%n_els
    2423          272 :       IF (ASSOCIATED(molecule_kinds)) THEN
    2424          272 :          nmoleculekind = molecule_kinds%n_els
    2425              :       ELSE
    2426            0 :          nmoleculekind = 0
    2427              :       END IF
    2428              : 
    2429          272 :       IF (ASSOCIATED(molecules)) THEN
    2430          272 :          nmolecule = molecules%n_els
    2431              :       ELSE
    2432            0 :          nmolecule = 0
    2433              :       END IF
    2434              : 
    2435          272 :       n_char_size = 0 ! init
    2436          272 :       n_int_size = 0 ! init
    2437          272 :       n_dp_size = 0 ! init
    2438              : 
    2439          272 :       IF (output_unit > 0) THEN ! only ionode
    2440              : 
    2441          136 :          IF (print_info) THEN
    2442          136 :             INQUIRE (UNIT=output_unit, NAME=binary_restart_file_name, IOSTAT=istat)
    2443          136 :             IF (istat /= 0) THEN
    2444              :                CALL cp_abort(__LOCATION__, &
    2445              :                              "An error occurred inquiring logical unit <"// &
    2446              :                              TRIM(ADJUSTL(cp_to_string(output_unit)))// &
    2447            0 :                              "> which should be linked to the binary restart file")
    2448              :             END IF
    2449          136 :             IF (log_unit > 0) THEN
    2450              :                WRITE (UNIT=log_unit, FMT="(T2,A,/,/,(T3,A,T71,I10))") &
    2451          136 :                   "Writing binary restart file "//TRIM(ADJUSTL(binary_restart_file_name)), &
    2452          136 :                   "Number of atomic kinds:", natomkind, &
    2453          136 :                   "Number of atoms:", natom, &
    2454          136 :                   "Number of cores (only core-shell model):", ncore, &
    2455          136 :                   "Number of shells (only core-shell model):", nshell, &
    2456          136 :                   "Number of molecule kinds:", nmoleculekind, &
    2457          272 :                   "Number of molecules", nmolecule
    2458              :             END IF
    2459              : 
    2460          136 :             n_int_size = n_int_size + 6
    2461              :          END IF
    2462              : 
    2463              :          WRITE (UNIT=output_unit, IOSTAT=istat) &
    2464          136 :             natomkind, natom, ncore, nshell, nmoleculekind, nmolecule
    2465          136 :          IF (istat /= 0) THEN
    2466              :             CALL stop_write("natomkind,natom,ncore,nshell,nmoleculekind,nmolecule "// &
    2467              :                             "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2468            0 :                             output_unit)
    2469              :          END IF
    2470              : 
    2471              :          ! Write atomic kind names
    2472          408 :          DO ikind = 1, natomkind
    2473          272 :             WRITE (UNIT=output_unit, IOSTAT=istat) atomic_kinds%els(ikind)%name
    2474          272 :             IF (istat /= 0) CALL stop_write("atomic_kinds%els(ikind)%name "// &
    2475              :                                             "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2476            0 :                                             output_unit)
    2477          408 :             n_char_size = n_char_size + LEN(atomic_kinds%els(ikind)%name)
    2478              :          END DO
    2479              : 
    2480              :          ! Write atomic kind numbers of all atoms
    2481          408 :          ALLOCATE (ibuf(natom))
    2482        13192 :          DO iatom = 1, natom
    2483        13192 :             ibuf(iatom) = particles%els(iatom)%atomic_kind%kind_number
    2484              :          END DO
    2485          136 :          WRITE (UNIT=output_unit, IOSTAT=istat) ibuf(1:natom)
    2486          136 :          IF (istat /= 0) CALL stop_write("ibuf(1:natom) -> atomic kind numbers "// &
    2487              :                                          "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2488            0 :                                          output_unit)
    2489          136 :          n_int_size = n_int_size + natom
    2490              :          ! Write atomic coordinates
    2491          408 :          ALLOCATE (rbuf(3, natom))
    2492        13192 :          DO iatom = 1, natom
    2493        52360 :             rbuf(1:3, iatom) = particles%els(iatom)%r(1:3)
    2494              :          END DO
    2495          136 :          WRITE (UNIT=output_unit, IOSTAT=istat) rbuf(1:3, 1:natom)
    2496          136 :          IF (istat /= 0) CALL stop_write("rbuf(1:3,1:natom) -> atomic coordinates "// &
    2497              :                                          "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2498            0 :                                          output_unit)
    2499          136 :          n_dp_size = n_dp_size + 3*natom
    2500          136 :          DEALLOCATE (rbuf)
    2501              : 
    2502              :          ! Write molecule information if available
    2503          136 :          IF (nmolecule > 0) THEN
    2504              :             ! Write molecule kind names
    2505          272 :             DO ikind = 1, nmoleculekind
    2506          136 :                WRITE (UNIT=output_unit, IOSTAT=istat) molecule_kinds%els(ikind)%name
    2507          136 :                IF (istat /= 0) CALL stop_write("molecule_kinds%els(ikind)%name "// &
    2508              :                                                "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2509            0 :                                                output_unit)
    2510          272 :                n_char_size = n_char_size + LEN(molecule_kinds%els(ikind)%name)
    2511              :             END DO
    2512              :             ! Write molecule (kind) index numbers for all atoms
    2513          136 :             ibuf(:) = 0
    2514          272 :             ALLOCATE (imol(natom))
    2515          136 :             imol(:) = 0
    2516         1224 :             DO imolecule = 1, nmolecule
    2517         1088 :                ikind = molecules%els(imolecule)%molecule_kind%kind_number
    2518        14144 :                DO iatom = molecules%els(imolecule)%first_atom, &
    2519         1224 :                   molecules%els(imolecule)%last_atom
    2520        13056 :                   ibuf(iatom) = ikind
    2521        14144 :                   imol(iatom) = imolecule
    2522              :                END DO
    2523              :             END DO
    2524              :             ! Write molecule kind index number for each atom
    2525          136 :             WRITE (UNIT=output_unit, IOSTAT=istat) ibuf(1:natom)
    2526          136 :             IF (istat /= 0) CALL stop_write("ibuf(1:natom) -> molecule kind index numbers "// &
    2527              :                                             "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2528            0 :                                             output_unit)
    2529          136 :             n_int_size = n_int_size + natom
    2530              :             ! Write molecule index number for each atom
    2531          136 :             WRITE (UNIT=output_unit, IOSTAT=istat) imol(1:natom)
    2532          136 :             IF (istat /= 0) CALL stop_write("imol(1:natom) -> molecule index numbers "// &
    2533              :                                             "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2534            0 :                                             output_unit)
    2535          136 :             n_int_size = n_int_size + natom
    2536          136 :             DEALLOCATE (imol)
    2537              :          END IF ! molecules
    2538              : 
    2539          136 :          DEALLOCATE (ibuf)
    2540              : 
    2541              :          ! Core-shell model only
    2542          136 :          section_label = "SHELL COORDINATES"
    2543          136 :          WRITE (UNIT=output_unit, IOSTAT=istat) section_label, nshell
    2544          136 :          IF (istat /= 0) CALL stop_write("section_label, nshell "// &
    2545              :                                          "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2546            0 :                                          output_unit)
    2547          136 :          n_char_size = n_char_size + LEN(section_label)
    2548          136 :          n_int_size = n_int_size + 1
    2549          136 :          IF (nshell > 0) THEN
    2550              :             ! Write shell coordinates
    2551          168 :             ALLOCATE (rbuf(3, nshell))
    2552         5432 :             DO ishell = 1, nshell
    2553        21560 :                rbuf(1:3, ishell) = shell_particles%els(ishell)%r(1:3)
    2554              :             END DO
    2555           56 :             WRITE (UNIT=output_unit, IOSTAT=istat) rbuf(1:3, 1:nshell)
    2556           56 :             IF (istat /= 0) CALL stop_write("rbuf(1:3,1:nshell) -> shell coordinates "// &
    2557              :                                             "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2558            0 :                                             output_unit)
    2559           56 :             n_dp_size = n_dp_size + 3*nshell
    2560           56 :             DEALLOCATE (rbuf)
    2561              :             ! Write atomic indices, i.e. number of the atom the shell belongs to
    2562          168 :             ALLOCATE (ibuf(nshell))
    2563         5432 :             DO ishell = 1, nshell
    2564         5432 :                ibuf(ishell) = shell_particles%els(ishell)%atom_index
    2565              :             END DO
    2566           56 :             WRITE (UNIT=output_unit, IOSTAT=istat) ibuf(1:nshell)
    2567           56 :             IF (istat /= 0) CALL stop_write("ibuf(1:nshell) -> atomic indices "// &
    2568              :                                             "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2569            0 :                                             output_unit)
    2570           56 :             n_int_size = n_int_size + nshell
    2571           56 :             DEALLOCATE (ibuf)
    2572              :          END IF
    2573              : 
    2574          136 :          section_label = "CORE COORDINATES"
    2575          136 :          WRITE (UNIT=output_unit, IOSTAT=istat) section_label, ncore
    2576          136 :          IF (istat /= 0) CALL stop_write("section_label, ncore "// &
    2577              :                                          "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2578            0 :                                          output_unit)
    2579          136 :          n_char_size = n_char_size + LEN(section_label)
    2580          136 :          n_int_size = n_int_size + 1
    2581          136 :          IF (ncore > 0) THEN
    2582              :             ! Write core coordinates
    2583          168 :             ALLOCATE (rbuf(3, ncore))
    2584         5432 :             DO icore = 1, ncore
    2585        21560 :                rbuf(1:3, icore) = core_particles%els(icore)%r(1:3)
    2586              :             END DO
    2587           56 :             WRITE (UNIT=output_unit, IOSTAT=istat) rbuf(1:3, 1:ncore)
    2588           56 :             IF (istat /= 0) CALL stop_write("rbuf(1:3,1:ncore) -> core coordinates "// &
    2589              :                                             "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2590            0 :                                             output_unit)
    2591           56 :             n_dp_size = n_dp_size + 3*ncore
    2592           56 :             DEALLOCATE (rbuf)
    2593              :             ! Write atomic indices, i.e. number of the atom the core belongs to
    2594          168 :             ALLOCATE (ibuf(ncore))
    2595         5432 :             DO icore = 1, ncore
    2596         5432 :                ibuf(icore) = core_particles%els(icore)%atom_index
    2597              :             END DO
    2598           56 :             WRITE (UNIT=output_unit, IOSTAT=istat) ibuf(1:ncore)
    2599           56 :             IF (istat /= 0) CALL stop_write("ibuf(1:ncore) -> atomic indices "// &
    2600              :                                             "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2601            0 :                                             output_unit)
    2602           56 :             n_int_size = n_int_size + ncore
    2603           56 :             DEALLOCATE (ibuf)
    2604              :          END IF
    2605              :       END IF ! ionode only
    2606              : 
    2607              :       ! Thermostat information
    2608              : 
    2609              :       ! Particle thermostats
    2610          272 :       section_label = "PARTICLE THERMOSTATS"
    2611          272 :       IF (ASSOCIATED(thermostat_part)) THEN
    2612              :          ! Nose-Hoover thermostats
    2613          176 :          IF (thermostat_part%type_of_thermostat == do_thermo_nose) THEN
    2614          176 :             nhc => thermostat_part%nhc
    2615              :             CALL write_binary_thermostats_nose(nhc, output_unit, log_unit, section_label, &
    2616              :                                                n_char_size, n_dp_size, n_int_size, &
    2617          176 :                                                print_info, para_env)
    2618              :          END IF
    2619              :       ELSE
    2620           96 :          nhc_size = 0
    2621           96 :          IF (output_unit > 0) THEN
    2622           48 :             WRITE (UNIT=output_unit, IOSTAT=istat) section_label, nhc_size
    2623           48 :             IF (istat /= 0) CALL stop_write(TRIM(section_label)//", nhc_size "// &
    2624              :                                             "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2625            0 :                                             output_unit)
    2626              :          END IF
    2627           96 :          n_char_size = n_char_size + LEN(section_label)
    2628           96 :          n_int_size = n_int_size + 1
    2629           96 :          IF (output_unit > 0 .AND. log_unit > 0) THEN ! only ionode
    2630           48 :             IF (print_info) THEN
    2631              :                WRITE (UNIT=log_unit, FMT="(T3,A,T71,I10)") &
    2632           48 :                   "NHC size ("//TRIM(ADJUSTL(section_label))//")", nhc_size
    2633              :             END IF
    2634              :          END IF
    2635              :       END IF
    2636              : 
    2637              :       ! Shell thermostats (only for core-shell models)
    2638          272 :       section_label = "SHELL THERMOSTATS"
    2639          272 :       IF (ASSOCIATED(thermostat_shell)) THEN
    2640              :          ! Nose-Hoover thermostats
    2641           24 :          IF (thermostat_shell%type_of_thermostat == do_thermo_nose) THEN
    2642           24 :             nhc => thermostat_shell%nhc
    2643              :             CALL write_binary_thermostats_nose(nhc, output_unit, log_unit, section_label, &
    2644              :                                                n_char_size, n_dp_size, n_int_size, &
    2645           24 :                                                print_info, para_env)
    2646              :          END IF
    2647              :       ELSE
    2648          248 :          nhc_size = 0
    2649          248 :          IF (output_unit > 0) THEN
    2650          124 :             WRITE (UNIT=output_unit, IOSTAT=istat) section_label, nhc_size
    2651          124 :             IF (istat /= 0) CALL stop_write("nhc_size "// &
    2652              :                                             "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2653            0 :                                             output_unit)
    2654              :          END IF
    2655          248 :          n_char_size = n_char_size + LEN(section_label)
    2656          248 :          n_int_size = n_int_size + 1
    2657          248 :          IF (output_unit > 0 .AND. log_unit > 0) THEN ! only ionode
    2658          124 :             IF (print_info) THEN
    2659              :                WRITE (UNIT=log_unit, FMT="(T3,A,T71,I10)") &
    2660          124 :                   "NHC size ("//TRIM(ADJUSTL(section_label))//")", nhc_size
    2661              :             END IF
    2662              :          END IF
    2663              :       END IF
    2664              : 
    2665              :       ! Particle velocities
    2666              : 
    2667          272 :       IF (output_unit > 0) THEN ! only ionode
    2668              :          ! Write particle velocities if needed
    2669          136 :          section_label = "VELOCITIES"
    2670              :          IF (output_unit > 0) THEN
    2671          136 :             WRITE (UNIT=output_unit, IOSTAT=istat) section_label, MERGE(natom, 0, write_velocities)
    2672          136 :             IF (istat /= 0) CALL stop_write(TRIM(section_label)//", write_velocities "// &
    2673              :                                             "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2674            0 :                                             output_unit)
    2675              :          END IF
    2676          136 :          n_char_size = n_char_size + LEN(section_label)
    2677          136 :          n_int_size = n_int_size + 1
    2678          136 :          IF (print_info .AND. log_unit > 0) THEN
    2679              :             WRITE (UNIT=log_unit, FMT="(T3,A,T78,A3)") &
    2680          136 :                "Write "//TRIM(ADJUSTL(section_label))//" section", MERGE("YES", " NO", write_velocities)
    2681              :          END IF
    2682          136 :          IF (write_velocities) THEN
    2683          408 :             ALLOCATE (rbuf(3, natom))
    2684              :             ! Write atomic velocities
    2685        13192 :             DO iatom = 1, natom
    2686        52360 :                rbuf(1:3, iatom) = particles%els(iatom)%v(1:3)
    2687              :             END DO
    2688          136 :             WRITE (UNIT=output_unit, IOSTAT=istat) rbuf(1:3, 1:natom)
    2689          136 :             IF (istat /= 0) CALL stop_write("rbuf(1:3,1:natom) -> atomic velocities "// &
    2690              :                                             "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2691            0 :                                             output_unit)
    2692          136 :             n_dp_size = n_dp_size + 3*natom
    2693          136 :             DEALLOCATE (rbuf)
    2694              :          END IF
    2695              :          ! Write shell velocities
    2696          136 :          section_label = "SHELL VELOCITIES"
    2697          136 :          WRITE (UNIT=output_unit, IOSTAT=istat) section_label, MERGE(nshell, 0, write_velocities)
    2698          136 :          IF (istat /= 0) CALL stop_write(TRIM(section_label)//", write_velocities "// &
    2699              :                                          "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2700            0 :                                          output_unit)
    2701              :          n_char_size = n_char_size + LEN(section_label)
    2702              :          n_int_size = n_int_size + 1
    2703          136 :          IF (print_info .AND. log_unit > 0) THEN
    2704              :             WRITE (UNIT=log_unit, FMT="(T3,A,T78,A3)") &
    2705          136 :                "Write "//TRIM(ADJUSTL(section_label))//" section", MERGE("YES", " NO", write_velocities)
    2706              :          END IF
    2707          136 :          IF (nshell > 0) THEN
    2708           56 :             IF (write_velocities) THEN
    2709          168 :                ALLOCATE (rbuf(3, nshell))
    2710         5432 :                DO ishell = 1, nshell
    2711        21560 :                   rbuf(1:3, ishell) = shell_particles%els(ishell)%v(1:3)
    2712              :                END DO
    2713           56 :                WRITE (UNIT=output_unit, IOSTAT=istat) rbuf(1:3, 1:nshell)
    2714           56 :                IF (istat /= 0) CALL stop_write("rbuf(1:3,1:nshell) -> shell velocities "// &
    2715              :                                                "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2716            0 :                                                output_unit)
    2717           56 :                n_dp_size = n_dp_size + 3*nshell
    2718           56 :                DEALLOCATE (rbuf)
    2719              :             END IF
    2720              :          END IF
    2721              :          ! Write core velocities
    2722          136 :          section_label = "CORE VELOCITIES"
    2723          136 :          WRITE (UNIT=output_unit, IOSTAT=istat) section_label, MERGE(ncore, 0, write_velocities)
    2724          136 :          IF (istat /= 0) CALL stop_write(TRIM(section_label)//", write_velocities "// &
    2725              :                                          "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2726            0 :                                          output_unit)
    2727          136 :          n_char_size = n_char_size + LEN(section_label)
    2728          136 :          n_int_size = n_int_size + 1
    2729          136 :          IF (print_info .AND. log_unit > 0) THEN
    2730              :             WRITE (UNIT=log_unit, FMT="(T3,A,T78,A3)") &
    2731          136 :                "Write "//TRIM(ADJUSTL(section_label))//" section", MERGE("YES", " NO", write_velocities)
    2732              :          END IF
    2733          136 :          IF (ncore > 0) THEN
    2734           56 :             IF (write_velocities) THEN
    2735          168 :                ALLOCATE (rbuf(3, ncore))
    2736         5432 :                DO icore = 1, ncore
    2737        21560 :                   rbuf(1:3, icore) = core_particles%els(icore)%v(1:3)
    2738              :                END DO
    2739           56 :                WRITE (UNIT=output_unit, IOSTAT=istat) rbuf(1:3, 1:ncore)
    2740           56 :                IF (istat /= 0) CALL stop_write("rbuf(1:3,1:ncore) -> core velocities "// &
    2741              :                                                "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2742            0 :                                                output_unit)
    2743           56 :                n_dp_size = n_dp_size + 3*ncore
    2744           56 :                DEALLOCATE (rbuf)
    2745              :             END IF
    2746              :          END IF
    2747              :       END IF ! ionode only
    2748              : 
    2749              :       ! Optionally, print a small I/O statistics
    2750          272 :       IF (output_unit > 0) THEN ! only ionode
    2751          136 :          IF (print_info .AND. log_unit > 0) THEN
    2752              :             WRITE (UNIT=log_unit, FMT="(/,(T2,I10,1X,I0,A,T68,I10,A))") &
    2753          136 :                n_char_size, int_size, "-byte characters written", n_char_size*int_size/1024, " KB", &
    2754          136 :                n_dp_size, dp_size, "-byte floating point numbers written", n_dp_size*dp_size/1024, " KB", &
    2755          272 :                n_int_size, int_size, "-byte integer numbers written", n_int_size*int_size/1024, " KB"
    2756              :             WRITE (UNIT=log_unit, FMT="(/,T2,A)") &
    2757          136 :                "Binary restart file "//TRIM(ADJUSTL(binary_restart_file_name))//" written"
    2758              :          END IF
    2759              :       END IF ! ionode only
    2760              : 
    2761          272 :       CALL timestop(handle)
    2762              : 
    2763              :    END SUBROUTINE write_binary_restart
    2764              : 
    2765              : ! **************************************************************************************************
    2766              : !> \brief   Write an input section for Nose thermostats to an external file in
    2767              : !>          binary format
    2768              : !> \param nhc ...
    2769              : !> \param output_unit binary file to write to
    2770              : !> \param log_unit unit for logging debug information
    2771              : !> \param section_label ...
    2772              : !> \param n_char_size ...
    2773              : !> \param n_dp_size ...
    2774              : !> \param n_int_size ...
    2775              : !> \param print_info ...
    2776              : !> \param para_env ...
    2777              : !> \par History
    2778              : !>      - Creation (23.03.2011,MK)
    2779              : !> \author  Matthias Krack (MK)
    2780              : !> \version 1.0
    2781              : ! **************************************************************************************************
    2782          200 :    SUBROUTINE write_binary_thermostats_nose(nhc, output_unit, log_unit, section_label, &
    2783              :                                             n_char_size, n_dp_size, n_int_size, &
    2784              :                                             print_info, para_env)
    2785              : 
    2786              :       TYPE(lnhc_parameters_type), POINTER                :: nhc
    2787              :       INTEGER, INTENT(IN)                                :: output_unit, log_unit
    2788              :       CHARACTER(LEN=default_string_length), INTENT(IN)   :: section_label
    2789              :       INTEGER, INTENT(INOUT)                             :: n_char_size, n_dp_size, n_int_size
    2790              :       LOGICAL, INTENT(IN)                                :: print_info
    2791              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    2792              : 
    2793              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'write_binary_thermostats_nose'
    2794              : 
    2795              :       INTEGER                                            :: handle, istat, nhc_size
    2796          200 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: eta, fnhc, mnhc, veta
    2797              : 
    2798          200 :       CALL timeset(routineN, handle)
    2799              : 
    2800          200 :       NULLIFY (eta)
    2801          200 :       NULLIFY (fnhc)
    2802          200 :       NULLIFY (mnhc)
    2803          200 :       NULLIFY (veta)
    2804              : 
    2805          200 :       CALL collect_nose_restart_info(nhc, para_env, eta, veta, fnhc, mnhc)
    2806              : 
    2807          200 :       nhc_size = SIZE(eta)
    2808              : 
    2809          200 :       IF (output_unit > 0) THEN ! only ionode
    2810          100 :          WRITE (UNIT=output_unit, IOSTAT=istat) section_label, nhc_size
    2811          100 :          IF (istat /= 0) CALL stop_write("nhc_size "// &
    2812              :                                          "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2813            0 :                                          output_unit)
    2814          100 :          n_char_size = n_char_size + LEN(section_label)
    2815          100 :          n_int_size = n_int_size + 1
    2816          100 :          IF (print_info .AND. log_unit > 0) THEN
    2817              :             WRITE (UNIT=log_unit, FMT="(T3,A,T71,I10)") &
    2818          100 :                "NHC size ("//TRIM(ADJUSTL(section_label))//")", nhc_size
    2819              :          END IF
    2820              :          ! eta
    2821        86500 :          WRITE (UNIT=output_unit, IOSTAT=istat) eta(1:nhc_size)
    2822          100 :          IF (istat /= 0) CALL stop_write("eta(1:nhc_size) "// &
    2823              :                                          "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2824            0 :                                          output_unit)
    2825          100 :          n_dp_size = n_dp_size + nhc_size
    2826              :       END IF ! ionode only
    2827              : 
    2828          200 :       DEALLOCATE (eta)
    2829              : 
    2830              :       ! veta
    2831          200 :       IF (output_unit > 0) THEN ! only ionode
    2832        86500 :          WRITE (UNIT=output_unit, IOSTAT=istat) veta(1:nhc_size)
    2833          100 :          IF (istat /= 0) CALL stop_write("veta(1:nhc_size) "// &
    2834              :                                          "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2835            0 :                                          output_unit)
    2836          100 :          n_dp_size = n_dp_size + nhc_size
    2837              :       END IF ! ionode only
    2838              : 
    2839          200 :       DEALLOCATE (veta)
    2840              : 
    2841              :       ! mnhc
    2842          200 :       IF (output_unit > 0) THEN ! only ionode
    2843        86500 :          WRITE (UNIT=output_unit, IOSTAT=istat) mnhc(1:nhc_size)
    2844          100 :          IF (istat /= 0) CALL stop_write("mnhc(1:nhc_size) "// &
    2845              :                                          "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2846            0 :                                          output_unit)
    2847          100 :          n_dp_size = n_dp_size + nhc_size
    2848              :       END IF ! ionode only
    2849              : 
    2850          200 :       DEALLOCATE (mnhc)
    2851              : 
    2852              :       ! fnhc
    2853          200 :       IF (output_unit > 0) THEN ! only ionode
    2854        86500 :          WRITE (UNIT=output_unit, IOSTAT=istat) fnhc(1:nhc_size)
    2855          100 :          IF (istat /= 0) CALL stop_write("fnhc(1:nhc_size) "// &
    2856              :                                          "(IOSTAT = "//TRIM(ADJUSTL(cp_to_string(istat)))//")", &
    2857            0 :                                          output_unit)
    2858          100 :          n_dp_size = n_dp_size + nhc_size
    2859              :       END IF ! ionode only
    2860              : 
    2861          200 :       DEALLOCATE (fnhc)
    2862              : 
    2863          200 :       CALL timestop(handle)
    2864              : 
    2865          200 :    END SUBROUTINE write_binary_thermostats_nose
    2866              : 
    2867              : ! **************************************************************************************************
    2868              : !> \brief Print an error message and stop the program execution in case of a
    2869              : !>        read error.
    2870              : !> \param object ...
    2871              : !> \param unit_number ...
    2872              : !> \par History
    2873              : !>      - Creation (15.02.2011,MK)
    2874              : !> \author Matthias Krack (MK)
    2875              : !> \note
    2876              : !>      object     : Name of the data object for which I/O operation failed
    2877              : !>      unit_number: Logical unit number of the file written to
    2878              : ! **************************************************************************************************
    2879            0 :    SUBROUTINE stop_write(object, unit_number)
    2880              : 
    2881              :       CHARACTER(LEN=*), INTENT(IN)                       :: object
    2882              :       INTEGER, INTENT(IN)                                :: unit_number
    2883              : 
    2884              :       CHARACTER(LEN=2*default_path_length)               :: message
    2885              :       CHARACTER(LEN=default_path_length)                 :: file_name
    2886              :       LOGICAL                                            :: file_exists
    2887              : 
    2888            0 :       IF (unit_number >= 0) THEN
    2889            0 :          INQUIRE (UNIT=unit_number, EXIST=file_exists)
    2890              :       ELSE
    2891            0 :          file_exists = .FALSE.
    2892              :       END IF
    2893            0 :       IF (file_exists) THEN
    2894            0 :          INQUIRE (UNIT=unit_number, NAME=file_name)
    2895              :          WRITE (UNIT=message, FMT="(A)") &
    2896              :             "An error occurred writing data object <"//TRIM(ADJUSTL(object))// &
    2897            0 :             "> to file <"//TRIM(ADJUSTL(file_name))//">"
    2898              :       ELSE
    2899              :          WRITE (UNIT=message, FMT="(A,I0,A)") &
    2900              :             "Could not write data object <"//TRIM(ADJUSTL(object))// &
    2901            0 :             "> to logical unit ", unit_number, ". The I/O unit does not exist."
    2902              :       END IF
    2903              : 
    2904            0 :       CPABORT(message)
    2905              : 
    2906            0 :    END SUBROUTINE stop_write
    2907              : 
    2908              : END MODULE input_cp2k_restarts
        

Generated by: LCOV version 2.0-1