LCOV - code coverage report
Current view: top level - src/motion/mc - mc_ensembles.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 85.4 % 588 502
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 2 2

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief Used to run the bulk of the MC simulation, doing things like
      10              : !>      choosing move types and writing data to files
      11              : !> \author Matthew J. McGrath  (09.26.2003)
      12              : !>
      13              : !>    REVISIONS
      14              : !>      09.10.05  MJM combined the two subroutines in this module into one
      15              : ! **************************************************************************************************
      16              : MODULE mc_ensembles
      17              :    USE cell_types,                      ONLY: cell_p_type,&
      18              :                                               get_cell
      19              :    USE cp_external_control,             ONLY: external_control
      20              :    USE cp_files,                        ONLY: close_file,&
      21              :                                               open_file
      22              :    USE cp_log_handling,                 ONLY: cp_logger_get_default_io_unit
      23              :    USE cp_subsys_types,                 ONLY: cp_subsys_get,&
      24              :                                               cp_subsys_p_type,&
      25              :                                               cp_subsys_type
      26              :    USE cp_units,                        ONLY: cp_unit_from_cp2k
      27              :    USE force_env_methods,               ONLY: force_env_calc_energy_force
      28              :    USE force_env_types,                 ONLY: force_env_get,&
      29              :                                               force_env_p_type,&
      30              :                                               force_env_release
      31              :    USE global_types,                    ONLY: global_environment_type
      32              :    USE input_constants,                 ONLY: dump_xmol
      33              :    USE input_section_types,             ONLY: section_type,&
      34              :                                               section_vals_type,&
      35              :                                               section_vals_val_get
      36              :    USE kinds,                           ONLY: default_string_length,&
      37              :                                               dp
      38              :    USE machine,                         ONLY: m_flush
      39              :    USE mathconstants,                   ONLY: pi
      40              :    USE mc_control,                      ONLY: mc_create_bias_force_env,&
      41              :                                               write_mc_restart
      42              :    USE mc_coordinates,                  ONLY: check_for_overlap,&
      43              :                                               create_discrete_array,&
      44              :                                               find_mc_test_molecule,&
      45              :                                               get_center_of_mass,&
      46              :                                               mc_coordinate_fold,&
      47              :                                               rotate_molecule
      48              :    USE mc_environment_types,            ONLY: get_mc_env,&
      49              :                                               mc_environment_p_type,&
      50              :                                               set_mc_env
      51              :    USE mc_ge_moves,                     ONLY: mc_ge_swap_move,&
      52              :                                               mc_ge_volume_move,&
      53              :                                               mc_quickstep_move
      54              :    USE mc_misc,                         ONLY: final_mc_write,&
      55              :                                               mc_averages_create,&
      56              :                                               mc_averages_release
      57              :    USE mc_move_control,                 ONLY: init_mc_moves,&
      58              :                                               mc_move_update,&
      59              :                                               mc_moves_release,&
      60              :                                               write_move_stats
      61              :    USE mc_moves,                        ONLY: mc_avbmc_move,&
      62              :                                               mc_cluster_translation,&
      63              :                                               mc_conformation_change,&
      64              :                                               mc_hmc_move,&
      65              :                                               mc_molecule_rotation,&
      66              :                                               mc_molecule_translation,&
      67              :                                               mc_volume_move
      68              :    USE mc_types,                        ONLY: get_mc_molecule_info,&
      69              :                                               get_mc_par,&
      70              :                                               mc_averages_p_type,&
      71              :                                               mc_input_file_type,&
      72              :                                               mc_molecule_info_type,&
      73              :                                               mc_moves_p_type,&
      74              :                                               mc_simulation_parameters_p_type,&
      75              :                                               set_mc_par
      76              :    USE message_passing,                 ONLY: mp_comm_type,&
      77              :                                               mp_para_env_type
      78              :    USE parallel_rng_types,              ONLY: rng_stream_type
      79              :    USE particle_list_types,             ONLY: particle_list_p_type,&
      80              :                                               particle_list_type
      81              :    USE particle_methods,                ONLY: write_particle_coordinates
      82              :    USE physcon,                         ONLY: angstrom,&
      83              :                                               boltzmann,&
      84              :                                               joule,&
      85              :                                               n_avogadro
      86              : #include "../../base/base_uses.f90"
      87              : 
      88              :    IMPLICIT NONE
      89              : 
      90              :    PRIVATE
      91              : 
      92              : ! *** Global parameters ***
      93              : 
      94              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'mc_ensembles'
      95              :    LOGICAL, PARAMETER, PRIVATE :: debug_this_module = .FALSE.
      96              : 
      97              :    PUBLIC :: mc_run_ensemble, mc_compute_virial
      98              : 
      99              : CONTAINS
     100              : 
     101              : ! **************************************************************************************************
     102              : !> \brief directs the program in running one or two box MC simulations
     103              : !> \param mc_env a pointer that contains all mc_env for all the simulation
     104              : !>          boxes
     105              : !> \param para_env ...
     106              : !> \param globenv the global environment for the simulation
     107              : !> \param input_declaration ...
     108              : !> \param nboxes the number of simulation boxes
     109              : !> \param rng_stream the stream we pull random numbers from
     110              : !>
     111              : !>    Suitable for parallel.
     112              : !> \author MJM
     113              : ! **************************************************************************************************
     114           18 :    SUBROUTINE mc_run_ensemble(mc_env, para_env, globenv, input_declaration, nboxes, rng_stream)
     115              : 
     116              :       TYPE(mc_environment_p_type), DIMENSION(:), POINTER :: mc_env
     117              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     118              :       TYPE(global_environment_type), POINTER             :: globenv
     119              :       TYPE(section_type), POINTER                        :: input_declaration
     120              :       INTEGER, INTENT(IN)                                :: nboxes
     121              :       TYPE(rng_stream_type), INTENT(INOUT)               :: rng_stream
     122              : 
     123              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'mc_run_ensemble'
     124              : 
     125              :       CHARACTER(default_string_length), ALLOCATABLE, &
     126           18 :          DIMENSION(:)                                    :: atom_names_box
     127              :       CHARACTER(default_string_length), &
     128           18 :          DIMENSION(:, :), POINTER                        :: atom_names
     129              :       CHARACTER(LEN=20)                                  :: ensemble
     130              :       CHARACTER(LEN=40)                                  :: cbox, cstep, move_type, move_type_avbmc
     131           18 :       INTEGER, DIMENSION(:, :), POINTER                  :: nchains
     132           18 :       INTEGER, DIMENSION(:), POINTER                     :: avbmc_atom, mol_type, nchains_box, &
     133           18 :                                                             nunits, nunits_tot
     134           36 :       INTEGER, DIMENSION(1:nboxes)                       :: box_flag, cl, data_unit, diff, istep, &
     135           54 :                                                             move_unit, rm
     136              :       INTEGER, DIMENSION(1:3, 1:2)                       :: discrete_array
     137              :       INTEGER :: atom_number, box_number, cell_unit, com_crd, com_ene, com_mol, end_mol, handle, &
     138              :          ibox, idum, imol_type, imolecule, imove, iparticle, iprint, itype, iunit, iuptrans, &
     139              :          iupvolume, iw, jbox, jdum, molecule_type, molecule_type_swap, molecule_type_target, &
     140              :          nchain_total, nmol_types, nmoves, nnstep, nstart, nstep, source, start_atom, &
     141              :          start_atom_swap, start_atom_target, start_mol
     142              :       CHARACTER(LEN=default_string_length)               :: unit_str
     143           36 :       CHARACTER(LEN=40), DIMENSION(1:nboxes)             :: cell_file, coords_file, data_file, &
     144           36 :                                                             displacement_file, energy_file, &
     145           36 :                                                             molecules_file, moves_file
     146              :       LOGICAL                                            :: ionode, lbias, ldiscrete, lhmc, &
     147              :                                                             lnew_bias_env, loverlap, lreject, &
     148              :                                                             lstop, print_kind, should_stop
     149           18 :       REAL(dp), DIMENSION(:), POINTER                    :: pbias, pmavbmc_mol, pmclus_box, &
     150           18 :                                                             pmhmc_box, pmrot_mol, pmtraion_mol, &
     151           18 :                                                             pmtrans_mol, pmvol_box
     152           18 :       REAL(dp), DIMENSION(:, :), POINTER                 :: conf_prob, mass
     153              :       REAL(KIND=dp)                                      :: discrete_step, pmavbmc, pmcltrans, &
     154              :                                                             pmhmc, pmswap, pmtraion, pmtrans, &
     155              :                                                             pmvolume, rand, test_energy, unit_conv
     156           18 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: r_temp
     157           18 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: r_old
     158           36 :       REAL(KIND=dp), DIMENSION(1:3, 1:nboxes)            :: abc
     159           36 :       REAL(KIND=dp), DIMENSION(1:nboxes)                 :: bias_energy, energy_check, final_energy, &
     160           36 :                                                             initial_energy, last_bias_energy, &
     161           36 :                                                             old_energy
     162           18 :       TYPE(cell_p_type), DIMENSION(:), POINTER           :: cell
     163           18 :       TYPE(cp_subsys_p_type), DIMENSION(:), POINTER      :: oldsys
     164              :       TYPE(cp_subsys_type), POINTER                      :: biassys
     165           18 :       TYPE(force_env_p_type), DIMENSION(:), POINTER      :: bias_env, force_env
     166           18 :       TYPE(mc_averages_p_type), DIMENSION(:), POINTER    :: averages
     167              :       TYPE(mc_input_file_type), POINTER                  :: mc_bias_file
     168              :       TYPE(mc_molecule_info_type), POINTER               :: mc_molecule_info
     169           18 :       TYPE(mc_moves_p_type), DIMENSION(:), POINTER       :: test_moves
     170           18 :       TYPE(mc_moves_p_type), DIMENSION(:, :), POINTER    :: move_updates, moves
     171              :       TYPE(mc_simulation_parameters_p_type), &
     172           18 :          DIMENSION(:), POINTER                           :: mc_par
     173              :       TYPE(mp_comm_type)                                 :: group
     174           18 :       TYPE(particle_list_p_type), DIMENSION(:), POINTER  :: particles_old
     175              :       TYPE(particle_list_type), POINTER                  :: particles_bias
     176              :       TYPE(section_vals_type), POINTER                   :: root_section
     177              : 
     178           18 :       CALL timeset(routineN, handle)
     179              : 
     180              :       ! nullify some pointers
     181           18 :       NULLIFY (moves, move_updates, test_moves, root_section)
     182              : 
     183              :       ! allocate a whole bunch of stuff based on how many boxes we have
     184           96 :       ALLOCATE (force_env(1:nboxes))
     185           60 :       ALLOCATE (bias_env(1:nboxes))
     186           60 :       ALLOCATE (cell(1:nboxes))
     187           60 :       ALLOCATE (particles_old(1:nboxes))
     188           60 :       ALLOCATE (oldsys(1:nboxes))
     189           60 :       ALLOCATE (averages(1:nboxes))
     190           60 :       ALLOCATE (mc_par(1:nboxes))
     191           36 :       ALLOCATE (pmvol_box(1:nboxes))
     192           36 :       ALLOCATE (pmclus_box(1:nboxes))
     193           36 :       ALLOCATE (pmhmc_box(1:nboxes))
     194              : 
     195           42 :       DO ibox = 1, nboxes
     196              :          CALL get_mc_env(mc_env(ibox)%mc_env, &
     197              :                          mc_par=mc_par(ibox)%mc_par, &
     198           42 :                          force_env=force_env(ibox)%force_env)
     199              :       END DO
     200              : 
     201              :       ! Gather units of measure for output (if available)
     202           18 :       root_section => force_env(1)%force_env%root_section
     203              :       CALL section_vals_val_get(root_section, "MOTION%PRINT%TRAJECTORY%UNIT", &
     204           18 :                                 c_val=unit_str)
     205           18 :       unit_conv = cp_unit_from_cp2k(1.0_dp, TRIM(unit_str))
     206              :       CALL section_vals_val_get(root_section, "MOTION%PRINT%TRAJECTORY%PRINT_ATOM_KIND", &
     207           18 :                                 l_val=print_kind)
     208              : 
     209              :       ! get some data out of mc_par
     210              :       CALL get_mc_par(mc_par(1)%mc_par, &
     211              :                       ionode=ionode, source=source, group=group, &
     212              :                       data_file=data_file(1), moves_file=moves_file(1), &
     213              :                       cell_file=cell_file(1), coords_file=coords_file(1), &
     214              :                       energy_file=energy_file(1), displacement_file=displacement_file(1), &
     215              :                       lstop=lstop, nstep=nstep, nstart=nstart, pmvolume=pmvolume, pmhmc=pmhmc, &
     216              :                       molecules_file=molecules_file(1), pmswap=pmswap, nmoves=nmoves, &
     217              :                       pmtraion=pmtraion, pmtrans=pmtrans, pmcltrans=pmcltrans, iuptrans=iuptrans, &
     218              :                       iupvolume=iupvolume, ldiscrete=ldiscrete, pmtraion_mol=pmtraion_mol, &
     219              :                       lbias=lbias, iprint=iprint, pmavbmc_mol=pmavbmc_mol, &
     220              :                       discrete_step=discrete_step, avbmc_atom=avbmc_atom, &
     221              :                       pmavbmc=pmavbmc, pbias=pbias, mc_molecule_info=mc_molecule_info, &
     222              :                       pmrot_mol=pmrot_mol, pmtrans_mol=pmtrans_mol, pmvol_box=pmvol_box(1), &
     223           18 :                       pmclus_box=pmclus_box(1), ensemble=ensemble, pmhmc_box=pmhmc_box(1), lhmc=lhmc)
     224              : 
     225              :       ! get some data from the molecule types
     226              :       CALL get_mc_molecule_info(mc_molecule_info, conf_prob=conf_prob, &
     227              :                                 nchains=nchains, nmol_types=nmol_types, nunits_tot=nunits_tot, &
     228              :                                 mol_type=mol_type, nchain_total=nchain_total, nunits=nunits, &
     229           18 :                                 atom_names=atom_names, mass=mass)
     230              : 
     231              :       ! allocate some stuff based on the number of molecule types we have
     232          136 :       ALLOCATE (moves(1:nmol_types, 1:nboxes))
     233          118 :       ALLOCATE (move_updates(1:nmol_types, 1:nboxes))
     234              : 
     235           18 :       IF (nboxes > 1) THEN
     236           12 :          DO ibox = 2, nboxes
     237              :             CALL get_mc_par(mc_par(ibox)%mc_par, &
     238              :                             data_file=data_file(ibox), &
     239              :                             moves_file=moves_file(ibox), &
     240              :                             cell_file=cell_file(ibox), coords_file=coords_file(ibox), &
     241              :                             energy_file=energy_file(ibox), &
     242              :                             displacement_file=displacement_file(ibox), &
     243              :                             molecules_file=molecules_file(ibox), pmvol_box=pmvol_box(ibox), &
     244           12 :                             pmclus_box=pmclus_box(ibox), pmhmc_box=pmhmc_box(ibox))
     245              :          END DO
     246              :       END IF
     247              : 
     248              :       ! this is a check we can't do in the input checking
     249           18 :       IF (pmvol_box(nboxes) < 1.0E0_dp) THEN
     250            0 :          CPABORT('The last value of PMVOL_BOX needs to be 1.0')
     251              :       END IF
     252           18 :       IF (pmclus_box(nboxes) < 1.0E0_dp) THEN
     253            0 :          CPABORT('The last value of PMVOL_BOX needs to be 1.0')
     254              :       END IF
     255           18 :       IF (pmhmc_box(nboxes) < 1.0E0_dp) THEN
     256            0 :          CPABORT('The last value of PMHMC_BOX needs to be 1.0')
     257              :       END IF
     258              : 
     259              :       ! allocate the particle positions array for broadcasting
     260           96 :       ALLOCATE (r_old(3, SUM(nunits_tot), 1:nboxes))
     261              : 
     262              :       ! figure out what the default write unit is
     263           18 :       iw = cp_logger_get_default_io_unit()
     264              : 
     265           18 :       IF (iw > 0) THEN
     266            9 :          WRITE (iw, *)
     267            9 :          WRITE (iw, *)
     268            9 :          WRITE (iw, *) 'Beginning the Monte Carlo calculation.'
     269            9 :          WRITE (iw, *)
     270            9 :          WRITE (iw, *)
     271              :       END IF
     272              : 
     273              :       ! initialize running average variables
     274           42 :       energy_check(:) = 0.0E0_dp
     275           42 :       box_flag(:) = 0
     276           42 :       istep(:) = 0
     277              : 
     278           42 :       DO ibox = 1, nboxes
     279              :          ! initialize the moves array, the arrays for updating maximum move
     280              :          ! displacements, and the averages array
     281           64 :          DO itype = 1, nmol_types
     282           40 :             CALL init_mc_moves(moves(itype, ibox)%moves)
     283           64 :             CALL init_mc_moves(move_updates(itype, ibox)%moves)
     284              :          END DO
     285           24 :          CALL mc_averages_create(averages(ibox)%averages)
     286              : 
     287              :          ! find the energy of the initial configuration
     288           64 :          IF (SUM(nchains(:, ibox)) /= 0) THEN
     289              :             CALL force_env_calc_energy_force(force_env(ibox)%force_env, &
     290           24 :                                              calc_force=.FALSE.)
     291              :             CALL force_env_get(force_env(ibox)%force_env, &
     292           24 :                                potential_energy=old_energy(ibox))
     293              :          ELSE
     294            0 :             old_energy(ibox) = 0.0E0_dp
     295              :          END IF
     296           24 :          initial_energy(ibox) = old_energy(ibox)
     297              : 
     298              : ! don't care about overlaps if we're only doing HMC
     299              : 
     300           24 :          IF (.NOT. lhmc) THEN
     301              :             ! check for overlaps
     302              :             start_mol = 1
     303           28 :             DO jbox = 1, ibox - 1
     304           40 :                start_mol = start_mol + SUM(nchains(:, jbox))
     305              :             END DO
     306           60 :             end_mol = start_mol + SUM(nchains(:, ibox)) - 1
     307              :             CALL check_for_overlap(force_env(ibox)%force_env, nchains(:, ibox), &
     308           22 :                                    nunits, loverlap, mol_type(start_mol:end_mol))
     309           22 :             IF (loverlap) CPABORT("overlap in an initial configuration")
     310              :          END IF
     311              : 
     312              :          ! get the subsystems and the cell information
     313              :          CALL force_env_get(force_env(ibox)%force_env, &
     314           24 :                             subsys=oldsys(ibox)%subsys, cell=cell(ibox)%cell)
     315           24 :          CALL get_cell(cell(ibox)%cell, abc=abc(:, ibox))
     316              :          CALL cp_subsys_get(oldsys(ibox)%subsys, &
     317           24 :                             particles=particles_old(ibox)%list)
     318              :          ! record the old coordinates, in case a move is rejected
     319         2656 :          DO iparticle = 1, nunits_tot(ibox)
     320              :             r_old(1:3, iparticle, ibox) = &
     321        10552 :                particles_old(ibox)%list%els(iparticle)%r(1:3)
     322              :          END DO
     323              : 
     324              :          ! find the bias energy of the initial run
     325           24 :          IF (lbias) THEN
     326              :             ! determine the atom names of every particle
     327           42 :             ALLOCATE (atom_names_box(1:nunits_tot(ibox)))
     328              : 
     329           14 :             atom_number = 1
     330          212 :             DO imolecule = 1, SUM(nchains(:, ibox))
     331          538 :                DO iunit = 1, nunits(mol_type(imolecule + start_mol - 1))
     332              :                   atom_names_box(atom_number) = &
     333          354 :                      atom_names(iunit, mol_type(imolecule + start_mol - 1))
     334          524 :                   atom_number = atom_number + 1
     335              :                END DO
     336              :             END DO
     337              : 
     338           14 :             CALL get_mc_par(mc_par(ibox)%mc_par, mc_bias_file=mc_bias_file)
     339           14 :             nchains_box => nchains(:, ibox)
     340              :             CALL mc_create_bias_force_env(bias_env(ibox)%force_env, &
     341              :                                           r_old(:, :, ibox), atom_names_box(:), nunits_tot(ibox), &
     342              :                                           para_env, abc(:, ibox), nchains_box, input_declaration, mc_bias_file, &
     343           14 :                                           ionode)
     344           42 :             IF (SUM(nchains(:, ibox)) /= 0) THEN
     345              :                CALL force_env_calc_energy_force(bias_env(ibox)%force_env, &
     346           14 :                                                 calc_force=.FALSE.)
     347              :                CALL force_env_get(bias_env(ibox)%force_env, &
     348           14 :                                   potential_energy=last_bias_energy(ibox))
     349              : 
     350              :             ELSE
     351            0 :                last_bias_energy(ibox) = 0.0E0_dp
     352              :             END IF
     353           14 :             bias_energy(ibox) = last_bias_energy(ibox)
     354           14 :             DEALLOCATE (atom_names_box)
     355              :          END IF
     356           42 :          lnew_bias_env = .FALSE.
     357              : 
     358              :       END DO
     359              : 
     360              :       ! back to seriel for a bunch of I/O stuff
     361           18 :       IF (ionode) THEN
     362              : 
     363              :          ! record the combined energies,coordinates, and cell lengths
     364              :          CALL open_file(file_name='mc_cell_length', &
     365              :                         unit_number=cell_unit, file_position='APPEND', &
     366            9 :                         file_action='WRITE', file_status='UNKNOWN')
     367              :          CALL open_file(file_name='mc_energies', &
     368              :                         unit_number=com_ene, file_position='APPEND', &
     369            9 :                         file_action='WRITE', file_status='UNKNOWN')
     370              :          CALL open_file(file_name='mc_coordinates', &
     371              :                         unit_number=com_crd, file_position='APPEND', &
     372            9 :                         file_action='WRITE', file_status='UNKNOWN')
     373              :          CALL open_file(file_name='mc_molecules', &
     374              :                         unit_number=com_mol, file_position='APPEND', &
     375            9 :                         file_action='WRITE', file_status='UNKNOWN')
     376            9 :          WRITE (com_ene, *) 'Initial Energies:       ', &
     377           18 :             old_energy(1:nboxes)
     378           21 :          DO ibox = 1, nboxes
     379           12 :             WRITE (com_mol, *) 'Initial Molecules:       ', &
     380           53 :                nchains(:, ibox)
     381              :          END DO
     382           21 :          DO ibox = 1, nboxes
     383           12 :             WRITE (cell_unit, *) 'Initial: ', &
     384           60 :                abc(1:3, ibox)*angstrom
     385           12 :             WRITE (cbox, '(I4)') ibox
     386              :             CALL open_file(file_name='energy_differences_box'// &
     387              :                            TRIM(ADJUSTL(cbox)), &
     388              :                            unit_number=diff(ibox), file_position='APPEND', &
     389           12 :                            file_action='WRITE', file_status='UNKNOWN')
     390           32 :             IF (SUM(nchains(:, ibox)) == 0) THEN
     391            0 :                WRITE (com_crd, *) ' 0'
     392            0 :                WRITE (com_crd, *) 'INITIAL BOX '//TRIM(ADJUSTL(cbox))
     393              :             ELSE
     394              :                CALL write_particle_coordinates(particles_old(ibox)%list%els, &
     395              :                                                com_crd, dump_xmol, 'POS', 'INITIAL BOX '//TRIM(ADJUSTL(cbox)), &
     396           12 :                                                unit_conv=unit_conv, print_kind=print_kind)
     397              :             END IF
     398              :             CALL open_file(file_name=data_file(ibox), &
     399              :                            unit_number=data_unit(ibox), file_position='APPEND', &
     400           12 :                            file_action='WRITE', file_status='UNKNOWN')
     401              :             CALL open_file(file_name=moves_file(ibox), &
     402              :                            unit_number=move_unit(ibox), file_position='APPEND', &
     403           12 :                            file_action='WRITE', file_status='UNKNOWN')
     404              :             CALL open_file(file_name=displacement_file(ibox), &
     405              :                            unit_number=rm(ibox), file_position='APPEND', &
     406           12 :                            file_action='WRITE', file_status='UNKNOWN')
     407              :             CALL open_file(file_name=cell_file(ibox), &
     408              :                            unit_number=cl(ibox), file_position='APPEND', &
     409           21 :                            file_action='WRITE', file_status='UNKNOWN')
     410              : 
     411              :          END DO
     412              : 
     413              :          ! back to parallel mode
     414              :       END IF
     415              : 
     416           42 :       DO ibox = 1, nboxes
     417           24 :          CALL group%bcast(cl(ibox), source)
     418           24 :          CALL group%bcast(rm(ibox), source)
     419           24 :          CALL group%bcast(diff(ibox), source)
     420              :          ! set all the units numbers that we just opened in the respective mc_par
     421              :          CALL set_mc_par(mc_par(ibox)%mc_par, cl=cl(ibox), rm=rm(ibox), &
     422           42 :                          diff=diff(ibox))
     423              :       END DO
     424              : 
     425              :       ! if we're doing a discrete volume move, we need to set up the array
     426              :       ! that keeps track of which direction we can move in
     427           18 :       IF (ldiscrete) THEN
     428            0 :          IF (nboxes /= 1) THEN
     429            0 :             CPABORT('ldiscrete=.true. ONLY for systems with 1 box')
     430              :          END IF
     431              :          CALL create_discrete_array(abc(:, 1), discrete_array(:, :), &
     432            0 :                                     discrete_step)
     433              :       END IF
     434              : 
     435              :       ! find out how many steps we're doing...change the updates to be in cycles
     436              :       ! if the total number of steps is measured in cycles
     437           18 :       IF (.NOT. lstop) THEN
     438           10 :          nstep = nstep*nchain_total
     439           10 :          iuptrans = iuptrans*nchain_total
     440           10 :          iupvolume = iupvolume*nchain_total
     441              :       END IF
     442              : 
     443          486 :       DO nnstep = nstart + 1, nstart + nstep
     444              : 
     445          468 :          IF (MOD(nnstep, iprint) == 0 .AND. (iw > 0)) THEN
     446           15 :             WRITE (iw, *)
     447           15 :             WRITE (iw, *) "------- On Monte Carlo Step ", nnstep
     448              :          END IF
     449              : 
     450          468 :          IF (ionode) rand = rng_stream%next()
     451              :          ! broadcast the random number, to make sure we're on the same move
     452          468 :          CALL group%bcast(rand, source)
     453              : 
     454          468 :          IF (rand < pmvolume) THEN
     455              : 
     456           58 :             IF (MOD(nnstep, iprint) == 0 .AND. (iw > 0)) THEN
     457            1 :                WRITE (iw, *) "Attempting a volume move"
     458            1 :                WRITE (iw, *)
     459              :             END IF
     460              : 
     461            8 :             SELECT CASE (ensemble)
     462              :             CASE ("TRADITIONAL")
     463              :                CALL mc_volume_move(mc_par(1)%mc_par, &
     464              :                                    force_env(1)%force_env, &
     465              :                                    moves(1, 1)%moves, move_updates(1, 1)%moves, &
     466              :                                    old_energy(1), 1, &
     467              :                                    energy_check(1), r_old(:, :, 1), iw, discrete_array(:, :), &
     468            8 :                                    rng_stream)
     469              :             CASE ("GEMC_NVT")
     470              :                CALL mc_ge_volume_move(mc_par, force_env, moves, &
     471              :                                       move_updates, nnstep, old_energy, energy_check, &
     472           24 :                                       r_old, rng_stream)
     473              :             CASE ("GEMC_NPT")
     474              :                ! we need to select a box based on the probability given in the input file
     475           26 :                IF (ionode) rand = rng_stream%next()
     476           26 :                CALL group%bcast(rand, source)
     477              : 
     478           38 :                DO ibox = 1, nboxes
     479           38 :                   IF (rand <= pmvol_box(ibox)) THEN
     480           26 :                      box_number = ibox
     481           26 :                      EXIT
     482              :                   END IF
     483              :                END DO
     484              : 
     485              :                CALL mc_volume_move(mc_par(box_number)%mc_par, &
     486              :                                    force_env(box_number)%force_env, &
     487              :                                    moves(1, box_number)%moves, &
     488              :                                    move_updates(1, box_number)%moves, &
     489              :                                    old_energy(box_number), box_number, &
     490              :                                    energy_check(box_number), r_old(:, :, box_number), iw, &
     491              :                                    discrete_array(:, :), &
     492           84 :                                    rng_stream)
     493              :             END SELECT
     494              : 
     495              : ! update all the pointers here, because otherwise we may pass wrong information when we're making a bias environment
     496          166 :             DO ibox = 1, nboxes
     497              :                CALL force_env_get(force_env(ibox)%force_env, &
     498          108 :                                   subsys=oldsys(ibox)%subsys, cell=cell(ibox)%cell)
     499          108 :                CALL get_cell(cell(ibox)%cell, abc=abc(:, ibox))
     500              :                CALL cp_subsys_get(oldsys(ibox)%subsys, &
     501          166 :                                   particles=particles_old(ibox)%list)
     502              :             END DO
     503              : 
     504              :             ! we need a new biasing environment now, if we're into that sort of thing
     505           58 :             IF (lbias) THEN
     506          150 :                DO ibox = 1, nboxes
     507          100 :                   CALL force_env_release(bias_env(ibox)%force_env)
     508              :                   ! determine the atom names of every particle
     509          300 :                   ALLOCATE (atom_names_box(1:nunits_tot(ibox)))
     510          150 :                   start_mol = 1
     511          150 :                   DO jbox = 1, ibox - 1
     512          250 :                      start_mol = start_mol + SUM(nchains(:, jbox))
     513              :                   END DO
     514          300 :                   end_mol = start_mol + SUM(nchains(:, ibox)) - 1
     515          300 :                   atom_number = 1
     516         1500 :                   DO imolecule = 1, SUM(nchains(:, ibox))
     517         3800 :                      DO iunit = 1, nunits(mol_type(imolecule + start_mol - 1))
     518              :                         atom_names_box(atom_number) = &
     519         2500 :                            atom_names(iunit, mol_type(imolecule + start_mol - 1))
     520         3700 :                         atom_number = atom_number + 1
     521              :                      END DO
     522              :                   END DO
     523              : 
     524              : ! need to find out what the cell lengths are
     525              :                   CALL force_env_get(force_env(ibox)%force_env, &
     526          100 :                                      subsys=oldsys(ibox)%subsys, cell=cell(ibox)%cell)
     527          100 :                   CALL get_cell(cell(ibox)%cell, abc=abc(:, ibox))
     528              : 
     529              :                   CALL get_mc_par(mc_par(ibox)%mc_par, &
     530          100 :                                   mc_bias_file=mc_bias_file)
     531          100 :                   nchains_box => nchains(:, ibox)
     532              : 
     533              :                   CALL mc_create_bias_force_env(bias_env(ibox)%force_env, &
     534              :                                                 r_old(:, :, ibox), atom_names_box(:), nunits_tot(ibox), &
     535              :                                                 para_env, abc(:, ibox), nchains_box, input_declaration, &
     536          100 :                                                 mc_bias_file, ionode)
     537              : 
     538          300 :                   IF (SUM(nchains(:, ibox)) /= 0) THEN
     539              :                      CALL force_env_calc_energy_force( &
     540              :                         bias_env(ibox)%force_env, &
     541          100 :                         calc_force=.FALSE.)
     542              :                      CALL force_env_get(bias_env(ibox)%force_env, &
     543          100 :                                         potential_energy=last_bias_energy(ibox))
     544              :                   ELSE
     545            0 :                      last_bias_energy(ibox) = 0.0E0_dp
     546              :                   END IF
     547          100 :                   bias_energy(ibox) = last_bias_energy(ibox)
     548          150 :                   DEALLOCATE (atom_names_box)
     549              :                END DO
     550              :             END IF
     551              : 
     552          410 :          ELSE IF (rand < pmswap) THEN
     553              : 
     554              :             ! try a swap move
     555           22 :             IF (MOD(nnstep, iprint) == 0 .AND. (iw > 0)) THEN
     556            0 :                WRITE (iw, *) "Attempting a swap move"
     557            0 :                WRITE (iw, *)
     558              :             END IF
     559              : 
     560              :             CALL mc_ge_swap_move(mc_par, force_env, bias_env, moves, &
     561              :                                  energy_check(:), r_old(:, :, :), old_energy(:), input_declaration, &
     562           22 :                                  para_env, bias_energy(:), last_bias_energy(:), rng_stream)
     563              : 
     564              :             ! the number of molecules may have changed, which deallocated the whole
     565              :             ! mc_molecule_info structure
     566           22 :             CALL get_mc_par(mc_par(1)%mc_par, mc_molecule_info=mc_molecule_info)
     567              :             CALL get_mc_molecule_info(mc_molecule_info, conf_prob=conf_prob, &
     568              :                                       nchains=nchains, nmol_types=nmol_types, nunits_tot=nunits_tot, &
     569              :                                       mol_type=mol_type, nchain_total=nchain_total, nunits=nunits, &
     570           22 :                                       atom_names=atom_names, mass=mass)
     571              : 
     572          388 :          ELSE IF (rand < pmhmc) THEN
     573              : ! try hybrid Monte Carlo
     574           20 :             IF (MOD(nnstep, iprint) == 0 .AND. (iw > 0)) THEN
     575            2 :                WRITE (iw, *) "Attempting a hybrid Monte Carlo move"
     576            2 :                WRITE (iw, *)
     577              :             END IF
     578              : 
     579              : ! pick a box at random
     580           20 :             IF (ionode) rand = rng_stream%next()
     581           20 :             CALL group%bcast(rand, source)
     582              : 
     583           20 :             DO ibox = 1, nboxes
     584           20 :                IF (rand <= pmhmc_box(ibox)) THEN
     585           20 :                   box_number = ibox
     586           20 :                   EXIT
     587              :                END IF
     588              :             END DO
     589              : 
     590              :             CALL mc_hmc_move(mc_par(box_number)%mc_par, &
     591              :                              force_env(box_number)%force_env, globenv, &
     592              :                              moves(1, box_number)%moves, &
     593              :                              move_updates(1, box_number)%moves, &
     594              :                              old_energy(box_number), box_number, &
     595              :                              energy_check(box_number), r_old(:, :, box_number), &
     596           20 :                              rng_stream)
     597              : 
     598          368 :          ELSE IF (rand < pmavbmc) THEN
     599              :             ! try an AVBMC move
     600            0 :             IF (MOD(nnstep, iprint) == 0 .AND. (iw > 0)) THEN
     601            0 :                WRITE (iw, *) "Attempting an AVBMC1 move"
     602            0 :                WRITE (iw, *)
     603              :             END IF
     604              : 
     605              :             ! first, pick a box to do it for
     606            0 :             IF (ionode) rand = rng_stream%next()
     607            0 :             CALL group%bcast(rand, source)
     608              : 
     609            0 :             IF (nboxes == 2) THEN
     610            0 :                IF (rand < 0.1E0_dp) THEN
     611            0 :                   box_number = 1
     612              :                ELSE
     613            0 :                   box_number = 2
     614              :                END IF
     615              :             ELSE
     616            0 :                box_number = 1
     617              :             END IF
     618              : 
     619              :             ! now pick a molecule type to do it for
     620            0 :             IF (ionode) rand = rng_stream%next()
     621            0 :             CALL group%bcast(rand, source)
     622            0 :             molecule_type_swap = 0
     623            0 :             DO imol_type = 1, nmol_types
     624            0 :                IF (rand < pmavbmc_mol(imol_type)) THEN
     625            0 :                   molecule_type_swap = imol_type
     626            0 :                   EXIT
     627              :                END IF
     628              :             END DO
     629            0 :             IF (molecule_type_swap == 0) THEN
     630            0 :                CPABORT('Did not choose a molecule type to swap...check AVBMC input')
     631              :             END IF
     632              : 
     633              :             ! now pick a molecule, automatically rejecting the move if the
     634              :             ! box is empty or only has one molecule
     635            0 :             IF (SUM(nchains(:, box_number)) <= 1) THEN
     636              :                ! indicate that we tried a move
     637              :                moves(molecule_type_swap, box_number)%moves%empty_avbmc = &
     638            0 :                   moves(molecule_type_swap, box_number)%moves%empty_avbmc + 1
     639              :             ELSE
     640              : 
     641              :                ! pick a molecule to be swapped in the box
     642            0 :                IF (ionode) THEN
     643              :                   CALL find_mc_test_molecule(mc_molecule_info, &
     644              :                                              start_atom_swap, idum, jdum, rng_stream, &
     645            0 :                                              box=box_number, molecule_type_old=molecule_type_swap)
     646              : 
     647              :                   ! pick a molecule to act as the target in the box...we don't care what type
     648            0 :                   DO
     649              :                      CALL find_mc_test_molecule(mc_molecule_info, &
     650              :                                                 start_atom_target, idum, molecule_type_target, &
     651            0 :                                                 rng_stream, box=box_number)
     652            0 :                      IF (start_atom_swap /= start_atom_target) THEN
     653              :                         start_atom_target = start_atom_target + &
     654            0 :                                             avbmc_atom(molecule_type_target) - 1
     655              :                         EXIT
     656              :                      END IF
     657              :                   END DO
     658              : 
     659              :                   ! choose if we're swapping into the bonded region of mol_target, or
     660              :                   ! into the nonbonded region
     661            0 :                   rand = rng_stream%next()
     662              : 
     663              :                END IF
     664            0 :                CALL group%bcast(start_atom_swap, source)
     665            0 :                CALL group%bcast(box_number, source)
     666            0 :                CALL group%bcast(start_atom_target, source)
     667            0 :                CALL group%bcast(rand, source)
     668              : 
     669            0 :                IF (rand < pbias(molecule_type_swap)) THEN
     670            0 :                   move_type_avbmc = 'in'
     671              :                ELSE
     672            0 :                   move_type_avbmc = 'out'
     673              :                END IF
     674              : 
     675              :                CALL mc_avbmc_move(mc_par(box_number)%mc_par, &
     676              :                                   force_env(box_number)%force_env, &
     677              :                                   bias_env(box_number)%force_env, &
     678              :                                   moves(molecule_type_swap, box_number)%moves, &
     679              :                                   energy_check(box_number), &
     680              :                                   r_old(:, :, box_number), old_energy(box_number), &
     681              :                                   start_atom_swap, start_atom_target, molecule_type_swap, &
     682              :                                   box_number, bias_energy(box_number), &
     683              :                                   last_bias_energy(box_number), &
     684            0 :                                   move_type_avbmc, rng_stream)
     685              : 
     686              :             END IF
     687              : 
     688              :          ELSE
     689              : 
     690          368 :             IF (MOD(nnstep, iprint) == 0 .AND. (iw > 0)) THEN
     691           12 :                WRITE (iw, *) "Attempting an inner move"
     692           12 :                WRITE (iw, *)
     693              :             END IF
     694              : 
     695         2010 :             DO imove = 1, nmoves
     696              : 
     697         1642 :                IF (ionode) rand = rng_stream%next()
     698         1642 :                CALL group%bcast(rand, source)
     699         2010 :                IF (rand < pmtraion) THEN
     700              :                   ! change molecular conformation
     701              :                   ! first, pick a box to do it for
     702          506 :                   IF (ionode) rand = rng_stream%next()
     703          506 :                   CALL group%bcast(rand, source)
     704          506 :                   IF (nboxes == 2) THEN
     705            0 :                      IF (rand < 0.75E0_dp) THEN
     706            0 :                         box_number = 1
     707              :                      ELSE
     708            0 :                         box_number = 2
     709              :                      END IF
     710              :                   ELSE
     711          506 :                      box_number = 1
     712              :                   END IF
     713              : 
     714              :                   ! figure out which molecule type we're looking for
     715          506 :                   IF (ionode) rand = rng_stream%next()
     716          506 :                   CALL group%bcast(rand, source)
     717          506 :                   molecule_type = 0
     718          506 :                   DO imol_type = 1, nmol_types
     719          506 :                      IF (rand < pmtraion_mol(imol_type)) THEN
     720          506 :                         molecule_type = imol_type
     721          506 :                         EXIT
     722              :                      END IF
     723              :                   END DO
     724          506 :                   IF (molecule_type == 0) CALL cp_abort( &
     725              :                      __LOCATION__, &
     726            0 :                      'Did not choose a molecule type to conf change...PMTRAION_MOL should not be all 0.0')
     727              : 
     728              :                   ! now pick a molecule, automatically rejecting the move if the
     729              :                   ! box is empty
     730          506 :                   IF (nchains(molecule_type, box_number) == 0) THEN
     731              :                      ! indicate that we tried a move
     732              :                      moves(molecule_type, box_number)%moves%empty_conf = &
     733            0 :                         moves(molecule_type, box_number)%moves%empty_conf + 1
     734              :                   ELSE
     735              :                      ! pick a molecule in the box
     736          506 :                      IF (ionode) THEN
     737              :                         CALL find_mc_test_molecule(mc_molecule_info, &
     738              :                                                    start_atom, idum, &
     739              :                                                    jdum, rng_stream, &
     740          253 :                                                    box=box_number, molecule_type_old=molecule_type)
     741              : 
     742              :                         ! choose if we're changing a bond length or an angle
     743          253 :                         rand = rng_stream%next()
     744              :                      END IF
     745          506 :                      CALL group%bcast(rand, source)
     746          506 :                      CALL group%bcast(start_atom, source)
     747          506 :                      CALL group%bcast(box_number, source)
     748          506 :                      CALL group%bcast(molecule_type, source)
     749              : 
     750              :                      ! figure out what kind of move we're doing
     751          506 :                      IF (rand < conf_prob(1, molecule_type)) THEN
     752          312 :                         move_type = 'bond'
     753          194 :                      ELSE IF (rand < (conf_prob(1, molecule_type) + &
     754              :                                       conf_prob(2, molecule_type))) THEN
     755          194 :                         move_type = 'angle'
     756              :                      ELSE
     757            0 :                         move_type = 'dihedral'
     758              :                      END IF
     759          506 :                      box_flag(box_number) = 1
     760              :                      CALL mc_conformation_change(mc_par(box_number)%mc_par, &
     761              :                                                  force_env(box_number)%force_env, &
     762              :                                                  bias_env(box_number)%force_env, &
     763              :                                                  moves(molecule_type, box_number)%moves, &
     764              :                                                  move_updates(molecule_type, box_number)%moves, &
     765              :                                                  start_atom, molecule_type, box_number, &
     766              :                                                  bias_energy(box_number), &
     767          506 :                                                  move_type, lreject, rng_stream)
     768          506 :                      IF (lreject) EXIT
     769              :                   END IF
     770         1136 :                ELSE IF (rand < pmtrans) THEN
     771              :                   ! translate a whole molecule in the system
     772              :                   ! pick a molecule type
     773          624 :                   IF (ionode) rand = rng_stream%next()
     774          624 :                   CALL group%bcast(rand, source)
     775          624 :                   molecule_type = 0
     776          924 :                   DO imol_type = 1, nmol_types
     777          924 :                      IF (rand < pmtrans_mol(imol_type)) THEN
     778          624 :                         molecule_type = imol_type
     779          624 :                         EXIT
     780              :                      END IF
     781              :                   END DO
     782          624 :                   IF (molecule_type == 0) CALL cp_abort( &
     783              :                      __LOCATION__, &
     784            0 :                      'Did not choose a molecule type to translate...PMTRANS_MOL should not be all 0.0')
     785              : 
     786              :                   ! now pick a molecule of that type
     787          624 :                   IF (ionode) THEN
     788              :                      CALL find_mc_test_molecule(mc_molecule_info, &
     789              :                                                 start_atom, box_number, idum, rng_stream, &
     790          312 :                                                 molecule_type_old=molecule_type)
     791              :                   END IF
     792          624 :                   CALL group%bcast(start_atom, source)
     793          624 :                   CALL group%bcast(box_number, source)
     794          624 :                   box_flag(box_number) = 1
     795              :                   CALL mc_molecule_translation(mc_par(box_number)%mc_par, &
     796              :                                                force_env(box_number)%force_env, &
     797              :                                                bias_env(box_number)%force_env, &
     798              :                                                moves(molecule_type, box_number)%moves, &
     799              :                                                move_updates(molecule_type, box_number)%moves, &
     800              :                                                start_atom, box_number, bias_energy(box_number), &
     801          624 :                                                molecule_type, lreject, rng_stream)
     802          624 :                   IF (lreject) EXIT
     803          512 :                ELSE IF (rand < pmcltrans) THEN
     804              :                   ! translate a whole cluster in the system
     805              :                   ! first, pick a box to do it for
     806           10 :                   IF (ionode) rand = rng_stream%next()
     807           10 :                   CALL group%bcast(rand, source)
     808              : 
     809           10 :                   DO ibox = 1, nboxes
     810           10 :                   IF (rand <= pmclus_box(ibox)) THEN
     811           10 :                      box_number = ibox
     812           10 :                      EXIT
     813              :                   END IF
     814              :                   END DO
     815           10 :                   box_flag(box_number) = 1
     816              :                   CALL mc_cluster_translation(mc_par(box_number)%mc_par, &
     817              :                                               force_env(box_number)%force_env, &
     818              :                                               bias_env(box_number)%force_env, &
     819              :                                               moves(1, box_number)%moves, &
     820              :                                               move_updates(1, box_number)%moves, &
     821              :                                               box_number, bias_energy(box_number), &
     822           10 :                                               lreject, rng_stream)
     823           10 :                   IF (lreject) EXIT
     824              :                ELSE
     825              :                   !     rotate a whole molecule in the system
     826              :                   ! pick a molecule type
     827          502 :                   IF (ionode) rand = rng_stream%next()
     828          502 :                   CALL group%bcast(rand, source)
     829          502 :                   molecule_type = 0
     830          502 :                   DO imol_type = 1, nmol_types
     831          502 :                      IF (rand < pmrot_mol(imol_type)) THEN
     832          502 :                         molecule_type = imol_type
     833          502 :                         EXIT
     834              :                      END IF
     835              :                   END DO
     836          502 :                   IF (molecule_type == 0) CALL cp_abort( &
     837              :                      __LOCATION__, &
     838            0 :                      'Did not choose a molecule type to rotate...PMROT_MOL should not be all 0.0')
     839              : 
     840          502 :                   IF (ionode) THEN
     841              :                      CALL find_mc_test_molecule(mc_molecule_info, &
     842              :                                                 start_atom, box_number, idum, rng_stream, &
     843          251 :                                                 molecule_type_old=molecule_type)
     844              :                   END IF
     845          502 :                   CALL group%bcast(start_atom, source)
     846          502 :                   CALL group%bcast(box_number, source)
     847          502 :                   box_flag(box_number) = 1
     848              :                   CALL mc_molecule_rotation(mc_par(box_number)%mc_par, &
     849              :                                             force_env(box_number)%force_env, &
     850              :                                             bias_env(box_number)%force_env, &
     851              :                                             moves(molecule_type, box_number)%moves, &
     852              :                                             move_updates(molecule_type, box_number)%moves, &
     853              :                                             box_number, start_atom, &
     854              :                                             molecule_type, bias_energy(box_number), &
     855          502 :                                             lreject, rng_stream)
     856          502 :                   IF (lreject) EXIT
     857              :                END IF
     858              : 
     859              :             END DO
     860              : 
     861              :             ! now do a Quickstep calculation to see if we accept the sequence
     862              :             CALL mc_Quickstep_move(mc_par, force_env, bias_env, &
     863              :                                    moves, lreject, move_updates, energy_check(:), r_old(:, :, :), &
     864              :                                    nnstep, old_energy(:), bias_energy(:), last_bias_energy(:), &
     865              :                                    nboxes, box_flag(:), oldsys, particles_old, &
     866          368 :                                    rng_stream, unit_conv)
     867              : 
     868              :          END IF
     869              : 
     870              :          ! make sure the pointers are pointing correctly since the subsys may
     871              :          ! have changed
     872         1080 :          DO ibox = 1, nboxes
     873              :             CALL force_env_get(force_env(ibox)%force_env, &
     874          612 :                                subsys=oldsys(ibox)%subsys, cell=cell(ibox)%cell)
     875          612 :             CALL get_cell(cell(ibox)%cell, abc=abc(:, ibox))
     876              :             CALL cp_subsys_get(oldsys(ibox)%subsys, &
     877         1080 :                                particles=particles_old(ibox)%list)
     878              :          END DO
     879              : 
     880          468 :          IF (ionode) THEN
     881              : 
     882          234 :             IF (MOD(nnstep, iprint) == 0) THEN
     883           15 :                WRITE (com_ene, *) nnstep, old_energy(1:nboxes)
     884              : 
     885           33 :                DO ibox = 1, nboxes
     886              : 
     887              :                   ! write the molecule information
     888           47 :                   WRITE (com_mol, *) nnstep, nchains(:, ibox)
     889              : 
     890              :                   ! write the move statistics to file
     891           47 :                   DO itype = 1, nmol_types
     892              :                      CALL write_move_stats(moves(itype, ibox)%moves, &
     893           47 :                                            nnstep, move_unit(ibox))
     894              :                   END DO
     895              : 
     896              :                   ! write a restart file
     897              :                   CALL write_mc_restart(nnstep, mc_par(ibox)%mc_par, &
     898           18 :                                         nchains(:, ibox), force_env(ibox)%force_env)
     899              : 
     900              :                   ! write cell lengths
     901           72 :                   WRITE (cell_unit, *) nnstep, abc(1:3, ibox)*angstrom
     902              : 
     903              :                   ! write particle coordinates
     904           18 :                   WRITE (cbox, '(I4)') ibox
     905           18 :                   WRITE (cstep, '(I8)') nnstep
     906           62 :                   IF (SUM(nchains(:, ibox)) == 0) THEN
     907            0 :                      WRITE (com_crd, *) ' 0'
     908              :                      WRITE (com_crd, *) 'BOX '//TRIM(ADJUSTL(cbox))// &
     909            0 :                         ',  STEP '//TRIM(ADJUSTL(cstep))
     910              :                   ELSE
     911              :                      CALL write_particle_coordinates( &
     912              :                         particles_old(ibox)%list%els, &
     913              :                         com_crd, dump_xmol, 'POS', &
     914              :                         'BOX '//TRIM(ADJUSTL(cbox))// &
     915              :                         ',  STEP '//TRIM(ADJUSTL(cstep)), &
     916           18 :                         unit_conv=unit_conv)
     917              :                   END IF
     918              :                END DO
     919              :             END IF ! end the things we only do every iprint moves
     920              : 
     921          540 :             DO ibox = 1, nboxes
     922              :                ! compute some averages
     923              :                averages(ibox)%averages%ave_energy = &
     924              :                   averages(ibox)%averages%ave_energy*REAL(nnstep - &
     925              :                                                           nstart - 1, dp)/REAL(nnstep - nstart, dp) + &
     926          306 :                   old_energy(ibox)/REAL(nnstep - nstart, dp)
     927              :                averages(ibox)%averages%molecules = &
     928              :                   averages(ibox)%averages%molecules*REAL(nnstep - &
     929              :                                                          nstart - 1, dp)/REAL(nnstep - nstart, dp) + &
     930          899 :                   REAL(SUM(nchains(:, ibox)), dp)/REAL(nnstep - nstart, dp)
     931              :                averages(ibox)%averages%ave_volume = &
     932              :                   averages(ibox)%averages%ave_volume* &
     933              :                   REAL(nnstep - nstart - 1, dp)/REAL(nnstep - nstart, dp) + &
     934              :                   abc(1, ibox)*abc(2, ibox)*abc(3, ibox)/ &
     935          306 :                   REAL(nnstep - nstart, dp)
     936              : 
     937              :                ! flush the buffers to the files
     938          306 :                CALL m_flush(data_unit(ibox))
     939          306 :                CALL m_flush(diff(ibox))
     940          306 :                CALL m_flush(move_unit(ibox))
     941          306 :                CALL m_flush(cl(ibox))
     942          540 :                CALL m_flush(rm(ibox))
     943              : 
     944              :             END DO
     945              : 
     946              :             ! flush more buffers to the files
     947          234 :             CALL m_flush(cell_unit)
     948          234 :             CALL m_flush(com_ene)
     949          234 :             CALL m_flush(com_crd)
     950          234 :             CALL m_flush(com_mol)
     951              : 
     952              :          END IF
     953              : 
     954              :          ! reset the box flags
     955         1080 :          box_flag(:) = 0
     956              : 
     957              :          ! check to see if EXIT file exists...if so, end the calculation
     958          468 :          CALL external_control(should_stop, "MC", globenv=globenv)
     959          468 :          IF (should_stop) EXIT
     960              : 
     961              :          ! update the move displacements, if necessary
     962         1080 :          DO ibox = 1, nboxes
     963          612 :             IF (MOD(nnstep - nstart, iuptrans) == 0) THEN
     964            0 :                DO itype = 1, nmol_types
     965              :                   CALL mc_move_update(mc_par(ibox)%mc_par, &
     966              :                                       move_updates(itype, ibox)%moves, itype, &
     967            0 :                                       "trans", nnstep, ionode)
     968              :                END DO
     969              :             END IF
     970              : 
     971         1080 :             IF (MOD(nnstep - nstart, iupvolume) == 0) THEN
     972              :                CALL mc_move_update(mc_par(ibox)%mc_par, &
     973              :                                    move_updates(1, ibox)%moves, 1337, &
     974            0 :                                    "volume", nnstep, ionode)
     975              :             END IF
     976              :          END DO
     977              : 
     978              :          ! check to see if there are any overlaps in the boxes, and fold coordinates
     979              : ! don't care about overlaps if we're only doing HMC
     980          468 :          IF (.NOT. lhmc) THEN
     981         1040 :             DO ibox = 1, nboxes
     982         2206 :                IF (SUM(nchains(:, ibox)) /= 0) THEN
     983              :                   start_mol = 1
     984          736 :                   DO jbox = 1, ibox - 1
     985         1024 :                      start_mol = start_mol + SUM(nchains(:, jbox))
     986              :                   END DO
     987         1758 :                   end_mol = start_mol + SUM(nchains(:, ibox)) - 1
     988              :                   CALL check_for_overlap(force_env(ibox)%force_env, &
     989              :                                          nchains(:, ibox), nunits, loverlap, &
     990          592 :                                          mol_type(start_mol:end_mol))
     991          592 :                   IF (loverlap) THEN
     992            0 :                      IF (iw > 0) WRITE (iw, *) nnstep
     993            0 :                      CPABORT('coordinate overlap at the end of the above step')
     994              :                      ! now fold the coordinates...don't do this anywhere but here, because
     995              :                      ! we can get screwed up with the mc_molecule_info stuff (like in swap move)...
     996              :                      ! this is kind of ugly, with allocated and deallocating every time
     997            0 :                      ALLOCATE (r_temp(1:3, 1:nunits_tot(ibox)))
     998              : 
     999            0 :                      DO iunit = 1, nunits_tot(ibox)
    1000              :                         r_temp(1:3, iunit) = &
    1001            0 :                            particles_old(ibox)%list%els(iunit)%r(1:3)
    1002              :                      END DO
    1003              : 
    1004              :                      CALL mc_coordinate_fold(r_temp(:, :), &
    1005              :                                              SUM(nchains(:, ibox)), mol_type(start_mol:end_mol), &
    1006            0 :                                              mass, nunits, abc(1:3, ibox))
    1007              : 
    1008              :                      ! save the folded coordinates
    1009            0 :                      DO iunit = 1, nunits_tot(ibox)
    1010            0 :                         r_old(1:3, iunit, ibox) = r_temp(1:3, iunit)
    1011              :                         particles_old(ibox)%list%els(iunit)%r(1:3) = &
    1012            0 :                            r_temp(1:3, iunit)
    1013              :                      END DO
    1014              : 
    1015              :                      ! if we're biasing, we need to do the same
    1016            0 :                      IF (lbias) THEN
    1017              :                         CALL force_env_get(bias_env(ibox)%force_env, &
    1018            0 :                                            subsys=biassys)
    1019              :                         CALL cp_subsys_get(biassys, &
    1020            0 :                                            particles=particles_bias)
    1021              : 
    1022            0 :                         DO iunit = 1, nunits_tot(ibox)
    1023              :                            particles_bias%els(iunit)%r(1:3) = &
    1024            0 :                               r_temp(1:3, iunit)
    1025              :                         END DO
    1026              :                      END IF
    1027              : 
    1028            0 :                      DEALLOCATE (r_temp)
    1029              :                   END IF
    1030              :                END IF
    1031              :             END DO
    1032              :          END IF
    1033              : 
    1034              :          !debug code
    1035          486 :          IF (debug_this_module) THEN
    1036              :             DO ibox = 1, nboxes
    1037              :                IF (SUM(nchains(:, ibox)) /= 0) THEN
    1038              :                   CALL force_env_calc_energy_force(force_env(ibox)%force_env, &
    1039              :                                                    calc_force=.FALSE.)
    1040              :                   CALL force_env_get(force_env(ibox)%force_env, &
    1041              :                                      potential_energy=test_energy)
    1042              :                ELSE
    1043              :                   test_energy = 0.0E0_dp
    1044              :                END IF
    1045              : 
    1046              :                IF (ABS(initial_energy(ibox) + energy_check(ibox) - &
    1047              :                        test_energy) > 0.0000001E0_dp) THEN
    1048              :                   IF (iw > 0) THEN
    1049              :                      WRITE (iw, *) '!!!!!!! We have an energy problem. !!!!!!!!'
    1050              :                      WRITE (iw, '(A,T64,F16.10)') 'Final Energy = ', test_energy
    1051              :                      WRITE (iw, '(A,T64,F16.10)') 'Initial Energy+energy_check=', &
    1052              :                         initial_energy(ibox) + energy_check(ibox)
    1053              :                      WRITE (iw, *) 'Box ', ibox
    1054              :                      WRITE (iw, *) 'nchains ', nchains(:, ibox)
    1055              :                   END IF
    1056              :                   CPABORT('!!!!!!! We have an energy problem. !!!!!!!!')
    1057              :                END IF
    1058              :             END DO
    1059              :          END IF
    1060              :       END DO
    1061              : 
    1062              :       ! write a restart file
    1063           18 :       IF (ionode) THEN
    1064           21 :          DO ibox = 1, nboxes
    1065              :             CALL write_mc_restart(nnstep, mc_par(ibox)%mc_par, &
    1066           21 :                                   nchains(:, ibox), force_env(ibox)%force_env)
    1067              :          END DO
    1068              :       END IF
    1069              : 
    1070              :       ! calculate the final energy
    1071           42 :       DO ibox = 1, nboxes
    1072           64 :          IF (SUM(nchains(:, ibox)) /= 0) THEN
    1073              :             CALL force_env_calc_energy_force(force_env(ibox)%force_env, &
    1074           24 :                                              calc_force=.FALSE.)
    1075              :             CALL force_env_get(force_env(ibox)%force_env, &
    1076           24 :                                potential_energy=final_energy(ibox))
    1077              :          ELSE
    1078            0 :             final_energy(ibox) = 0.0E0_dp
    1079              :          END IF
    1080           42 :          IF (lbias) THEN
    1081           14 :             CALL force_env_release(bias_env(ibox)%force_env)
    1082              :          END IF
    1083              :       END DO
    1084              : 
    1085              :       ! do some stuff in serial
    1086           18 :       IF (ionode .OR. (iw > 0)) THEN
    1087              : 
    1088            9 :          WRITE (com_ene, *) 'Final Energies:                      ', &
    1089           18 :             final_energy(1:nboxes)
    1090              : 
    1091           21 :          DO ibox = 1, nboxes
    1092           12 :             WRITE (cbox, '(I4)') ibox
    1093           32 :             IF (SUM(nchains(:, ibox)) == 0) THEN
    1094            0 :                WRITE (com_crd, *) ' 0'
    1095            0 :                WRITE (com_crd, *) 'BOX '//TRIM(ADJUSTL(cbox))
    1096              :             ELSE
    1097              :                CALL write_particle_coordinates( &
    1098              :                   particles_old(ibox)%list%els, &
    1099              :                   com_crd, dump_xmol, 'POS', &
    1100           12 :                   'FINAL BOX '//TRIM(ADJUSTL(cbox)), unit_conv=unit_conv)
    1101              :             END IF
    1102              : 
    1103              :             ! write a bunch of data to the screen
    1104              :             WRITE (iw, '(A)') &
    1105           12 :                '------------------------------------------------'
    1106              :             WRITE (iw, '(A,I1,A)') &
    1107           12 :                '|                   BOX ', ibox, &
    1108           24 :                '                      |'
    1109              :             WRITE (iw, '(A)') &
    1110           12 :                '------------------------------------------------'
    1111           12 :             test_moves => moves(:, ibox)
    1112              :             CALL final_mc_write(mc_par(ibox)%mc_par, test_moves, &
    1113              :                                 iw, energy_check(ibox), &
    1114              :                                 initial_energy(ibox), final_energy(ibox), &
    1115           12 :                                 averages(ibox)%averages)
    1116              : 
    1117              :             ! close any open files
    1118           12 :             CALL close_file(unit_number=diff(ibox))
    1119           12 :             CALL close_file(unit_number=data_unit(ibox))
    1120           12 :             CALL close_file(unit_number=move_unit(ibox))
    1121           12 :             CALL close_file(unit_number=cl(ibox))
    1122           21 :             CALL close_file(unit_number=rm(ibox))
    1123              :          END DO
    1124              : 
    1125              :          ! close some more files
    1126            9 :          CALL close_file(unit_number=cell_unit)
    1127            9 :          CALL close_file(unit_number=com_ene)
    1128            9 :          CALL close_file(unit_number=com_crd)
    1129            9 :          CALL close_file(unit_number=com_mol)
    1130              :       END IF
    1131              : 
    1132           42 :       DO ibox = 1, nboxes
    1133              :          CALL set_mc_env(mc_env(ibox)%mc_env, &
    1134              :                          mc_par=mc_par(ibox)%mc_par, &
    1135           24 :                          force_env=force_env(ibox)%force_env)
    1136              : 
    1137              :          ! deallocate some stuff
    1138           64 :          DO itype = 1, nmol_types
    1139           40 :             CALL mc_moves_release(move_updates(itype, ibox)%moves)
    1140           64 :             CALL mc_moves_release(moves(itype, ibox)%moves)
    1141              :          END DO
    1142           42 :          CALL mc_averages_release(averages(ibox)%averages)
    1143              :       END DO
    1144              : 
    1145           18 :       DEALLOCATE (pmhmc_box)
    1146           18 :       DEALLOCATE (pmvol_box)
    1147           18 :       DEALLOCATE (pmclus_box)
    1148           18 :       DEALLOCATE (r_old)
    1149           18 :       DEALLOCATE (force_env)
    1150           18 :       DEALLOCATE (bias_env)
    1151           18 :       DEALLOCATE (cell)
    1152           18 :       DEALLOCATE (particles_old)
    1153           18 :       DEALLOCATE (oldsys)
    1154           18 :       DEALLOCATE (averages)
    1155           18 :       DEALLOCATE (moves)
    1156           18 :       DEALLOCATE (move_updates)
    1157           18 :       DEALLOCATE (mc_par)
    1158              : 
    1159              :       ! end the timing
    1160           18 :       CALL timestop(handle)
    1161              : 
    1162           54 :    END SUBROUTINE mc_run_ensemble
    1163              : 
    1164              : ! **************************************************************************************************
    1165              : !> \brief Computes the second virial coefficient of a molecule by using the integral form
    1166              : !>      of the second virial coefficient found in McQuarrie "Statistical Thermodynamics",
    1167              : !>      B2(T) = -2Pi Int 0toInf [ Exp[-beta*u(r)] -1] r^2 dr     Eq. 15-25
    1168              : !>      I use trapazoidal integration with various step sizes
    1169              : !>      (the integral is broken up into three parts, currently, but that's easily
    1170              : !>      changed by the first variables found below).  It generates nvirial configurations,
    1171              : !>      doing the integration for each one, and then averages all the B2(T) to produce
    1172              : !>      the final answer.
    1173              : !> \param mc_env a pointer that contains all mc_env for all the simulation
    1174              : !>          boxes
    1175              : !> \param rng_stream the stream we pull random numbers from
    1176              : !>
    1177              : !>    Suitable for parallel.
    1178              : !> \author MJM
    1179              : ! **************************************************************************************************
    1180            2 :    SUBROUTINE mc_compute_virial(mc_env, rng_stream)
    1181              : 
    1182              :       TYPE(mc_environment_p_type), DIMENSION(:), POINTER :: mc_env
    1183              :       TYPE(rng_stream_type), INTENT(INOUT)               :: rng_stream
    1184              : 
    1185              :       INTEGER :: current_division, end_atom, ibin, idivision, iparticle, iprint, itemp, iunit, &
    1186              :          ivirial, iw, nbins, nchain_total, nintegral_divisions, nmol_types, nvirial, &
    1187              :          nvirial_temps, source, start_atom
    1188            2 :       INTEGER, DIMENSION(:), POINTER                     :: mol_type, nunits, nunits_tot
    1189            2 :       INTEGER, DIMENSION(:, :), POINTER                  :: nchains
    1190              :       LOGICAL                                            :: ionode, loverlap
    1191            2 :       REAL(dp), DIMENSION(:), POINTER                    :: BETA, virial_cutoffs, virial_stepsize, &
    1192            2 :                                                             virial_temps
    1193            2 :       REAL(dp), DIMENSION(:, :), POINTER                 :: mass, mayer, r_old
    1194              :       REAL(KIND=dp) :: ave_virial, current_value, distance, exp_max_val, exp_min_val, exponent, &
    1195              :          integral, previous_value, square_value, trial_energy, triangle_value
    1196              :       REAL(KIND=dp), DIMENSION(1:3)                      :: abc, center_of_mass
    1197            2 :       TYPE(cell_p_type), DIMENSION(:), POINTER           :: cell
    1198            2 :       TYPE(cp_subsys_p_type), DIMENSION(:), POINTER      :: subsys
    1199            2 :       TYPE(force_env_p_type), DIMENSION(:), POINTER      :: force_env
    1200              :       TYPE(mc_molecule_info_type), POINTER               :: mc_molecule_info
    1201              :       TYPE(mc_simulation_parameters_p_type), &
    1202            2 :          DIMENSION(:), POINTER                           :: mc_par
    1203              :       TYPE(mp_comm_type)                                 :: group
    1204            2 :       TYPE(particle_list_p_type), DIMENSION(:), POINTER  :: particles
    1205              : 
    1206              : ! these are current magic numbers for how we compute the virial...
    1207              : ! we break it up into three parts to integrate the function so provide
    1208              : ! better statistics
    1209              : 
    1210            2 :       nintegral_divisions = 3
    1211            0 :       ALLOCATE (virial_cutoffs(1:nintegral_divisions))
    1212            2 :       ALLOCATE (virial_stepsize(1:nintegral_divisions))
    1213            2 :       virial_cutoffs(1) = 8.0_dp ! first distance, in bohr
    1214            2 :       virial_cutoffs(2) = 13.0_dp ! second distance, in bohr
    1215            2 :       virial_cutoffs(3) = 22.0_dp ! maximum distance, in bohr
    1216            2 :       virial_stepsize(1) = 0.04_dp ! stepsize from 0 to virial_cutoffs(1)
    1217            2 :       virial_stepsize(2) = 0.1_dp
    1218            2 :       virial_stepsize(3) = 0.2_dp
    1219              : 
    1220              :       nbins = CEILING(virial_cutoffs(1)/virial_stepsize(1) + (virial_cutoffs(2) - virial_cutoffs(1))/ &
    1221              :                       virial_stepsize(2) + (virial_cutoffs(3) - virial_cutoffs(2))/virial_stepsize(3))
    1222              : 
    1223              :       ! figure out what the default write unit is
    1224            2 :       iw = cp_logger_get_default_io_unit()
    1225              : 
    1226              :       ! allocate a whole bunch of stuff based on how many boxes we have
    1227            4 :       ALLOCATE (force_env(1:1))
    1228            4 :       ALLOCATE (cell(1:1))
    1229            4 :       ALLOCATE (particles(1:1))
    1230            4 :       ALLOCATE (subsys(1:1))
    1231            4 :       ALLOCATE (mc_par(1:1))
    1232              : 
    1233              :       CALL get_mc_env(mc_env(1)%mc_env, &
    1234              :                       mc_par=mc_par(1)%mc_par, &
    1235            2 :                       force_env=force_env(1)%force_env)
    1236              : 
    1237              :       ! get some data out of mc_par
    1238              :       CALL get_mc_par(mc_par(1)%mc_par, &
    1239              :                       exp_max_val=exp_max_val, &
    1240              :                       exp_min_val=exp_min_val, nvirial=nvirial, &
    1241              :                       ionode=ionode, source=source, group=group, &
    1242            2 :                       mc_molecule_info=mc_molecule_info, virial_temps=virial_temps)
    1243              : 
    1244            2 :       IF (iw > 0) THEN
    1245            1 :          WRITE (iw, *)
    1246            1 :          WRITE (iw, *)
    1247            1 :          WRITE (iw, *) 'Beginning the calculation of the second virial coefficient'
    1248            1 :          WRITE (iw, *)
    1249            1 :          WRITE (iw, *)
    1250              :       END IF
    1251              : 
    1252              :       ! get some data from the molecule types
    1253              :       CALL get_mc_molecule_info(mc_molecule_info, &
    1254              :                                 nchains=nchains, nmol_types=nmol_types, nunits_tot=nunits_tot, &
    1255              :                                 mol_type=mol_type, nchain_total=nchain_total, nunits=nunits, &
    1256            2 :                                 mass=mass)
    1257              : 
    1258            2 :       nvirial_temps = SIZE(virial_temps)
    1259            6 :       ALLOCATE (BETA(1:nvirial_temps))
    1260              : 
    1261            6 :       DO itemp = 1, nvirial_temps
    1262            6 :          BETA(itemp) = 1/virial_temps(itemp)/boltzmann*joule
    1263              :       END DO
    1264              : 
    1265              :       ! get the subsystems and the cell information
    1266              :       CALL force_env_get(force_env(1)%force_env, &
    1267            2 :                          subsys=subsys(1)%subsys, cell=cell(1)%cell)
    1268            2 :       CALL get_cell(cell(1)%cell, abc=abc(:))
    1269              :       CALL cp_subsys_get(subsys(1)%subsys, &
    1270            2 :                          particles=particles(1)%list)
    1271              : 
    1272              :       ! check and make sure the box is big enough
    1273            2 :       IF (abc(1) /= abc(2) .OR. abc(2) /= abc(3)) THEN
    1274            0 :          CPABORT('The box needs to be cubic for a virial calculation (it is easiest).')
    1275              :       END IF
    1276            2 :       IF (virial_cutoffs(nintegral_divisions) > abc(1)/2.0E0_dp) THEN
    1277            0 :          IF (iw > 0) THEN
    1278            0 :             WRITE (iw, *) "Box length ", abc(1)*angstrom, " virial cutoff ", &
    1279            0 :                virial_cutoffs(nintegral_divisions)*angstrom
    1280              :          END IF
    1281            0 :          CPABORT('You need a bigger box to deal with this virial cutoff (see above).')
    1282              :       END IF
    1283              : 
    1284              :       ! store the coordinates of the molecules in an array so we can work with it
    1285            6 :       ALLOCATE (r_old(1:3, 1:nunits_tot(1)))
    1286              : 
    1287           14 :       DO iparticle = 1, nunits_tot(1)
    1288              :          r_old(1:3, iparticle) = &
    1289           50 :             particles(1)%list%els(iparticle)%r(1:3)
    1290              :       END DO
    1291              : 
    1292              :       ! move the center of mass of molecule 1 to the origin
    1293            2 :       start_atom = 1
    1294            2 :       end_atom = nunits(mol_type(1))
    1295              :       CALL get_center_of_mass(r_old(:, start_atom:end_atom), nunits(mol_type(1)), &
    1296            2 :                               center_of_mass(:), mass(1:nunits(mol_type(1)), mol_type(1)))
    1297            8 :       DO iunit = start_atom, end_atom
    1298           26 :          r_old(:, iunit) = r_old(:, iunit) - center_of_mass(:)
    1299              :       END DO
    1300              :       ! set them in the force_env, so the first molecule is ready for the energy calc
    1301            8 :       DO iparticle = start_atom, end_atom
    1302           44 :          particles(1)%list%els(iparticle)%r(1:3) = r_old(1:3, iparticle)
    1303              :       END DO
    1304              : 
    1305              :       ! print out a notice every 1%
    1306            2 :       iprint = FLOOR(REAL(nvirial, KIND=dp)/100.0_dp)
    1307            2 :       IF (iprint == 0) iprint = 1
    1308              : 
    1309              :       ! we'll compute the average potential, and then integrate that, as opposed to
    1310              :       ! integrating every orientation and then averaging
    1311            8 :       ALLOCATE (mayer(1:nvirial_temps, 1:nbins))
    1312              : 
    1313         1772 :       mayer(:, :) = 0.0_dp
    1314              : 
    1315              :       ! loop over all nvirial random configurations
    1316           22 :       DO ivirial = 1, nvirial
    1317              : 
    1318              :          ! move molecule two back to the origin
    1319           20 :          start_atom = nunits(mol_type(1)) + 1
    1320           20 :          end_atom = nunits_tot(1)
    1321              :          CALL get_center_of_mass(r_old(:, start_atom:end_atom), nunits(mol_type(2)), &
    1322           20 :                                  center_of_mass(:), mass(1:nunits(mol_type(2)), mol_type(2)))
    1323           80 :          DO iunit = start_atom, end_atom
    1324          260 :             r_old(:, iunit) = r_old(:, iunit) - center_of_mass(:)
    1325              :          END DO
    1326              : 
    1327              :          ! now we need a random orientation for molecule 2...this routine is
    1328              :          ! only done in serial since it calls a random number
    1329           20 :          IF (ionode) THEN
    1330              :             CALL rotate_molecule(r_old(:, start_atom:end_atom), &
    1331              :                                  mass(1:nunits(mol_type(2)), mol_type(2)), &
    1332           10 :                                  nunits(mol_type(2)), rng_stream)
    1333              :          END IF
    1334          980 :          CALL group%bcast(r_old(:, :), source)
    1335              : 
    1336           20 :          distance = 0.0E0_dp
    1337           20 :          ibin = 1
    1338         5900 :          DO
    1339              :             ! find out what our stepsize is
    1340         5920 :             current_division = 0
    1341         8780 :             DO idivision = 1, nintegral_divisions
    1342         8780 :                IF (distance < virial_cutoffs(idivision) - virial_stepsize(idivision)/2.0E0_dp) THEN
    1343              :                   current_division = idivision
    1344              :                   EXIT
    1345              :                END IF
    1346              :             END DO
    1347         5920 :             IF (current_division == 0) EXIT
    1348         5900 :             distance = distance + virial_stepsize(current_division)
    1349              : 
    1350              :             ! move the second molecule only along the x direction
    1351        23600 :             DO iparticle = start_atom, end_atom
    1352        17700 :                particles(1)%list%els(iparticle)%r(1) = r_old(1, iparticle) + distance
    1353        17700 :                particles(1)%list%els(iparticle)%r(2) = r_old(2, iparticle)
    1354        23600 :                particles(1)%list%els(iparticle)%r(3) = r_old(3, iparticle)
    1355              :             END DO
    1356              : 
    1357              :             ! check for overlaps
    1358         5900 :             CALL check_for_overlap(force_env(1)%force_env, nchains(:, 1), nunits, loverlap, mol_type)
    1359              : 
    1360              :             ! compute the energy if there is no overlap
    1361              :             ! exponent is exp(-beta*energy)-1, also called the Mayer term
    1362         5900 :             IF (loverlap) THEN
    1363         2202 :                DO itemp = 1, nvirial_temps
    1364         2202 :                   mayer(itemp, ibin) = mayer(itemp, ibin) - 1.0_dp
    1365              :                END DO
    1366              :             ELSE
    1367              :                CALL force_env_calc_energy_force(force_env(1)%force_env, &
    1368         5166 :                                                 calc_force=.FALSE.)
    1369              :                CALL force_env_get(force_env(1)%force_env, &
    1370         5166 :                                   potential_energy=trial_energy)
    1371              : 
    1372        15498 :                DO itemp = 1, nvirial_temps
    1373              : 
    1374        10332 :                   exponent = -BETA(itemp)*trial_energy
    1375              : 
    1376        10332 :                   IF (exponent > exp_max_val) THEN
    1377              :                      exponent = exp_max_val
    1378        10332 :                   ELSE IF (exponent < exp_min_val) THEN
    1379              :                      exponent = exp_min_val
    1380              :                   END IF
    1381        15498 :                   mayer(itemp, ibin) = mayer(itemp, ibin) + EXP(exponent) - 1.0_dp
    1382              :                END DO
    1383              :             END IF
    1384              : 
    1385         5900 :             ibin = ibin + 1
    1386              :          END DO
    1387              :          ! write out some info that keeps track of where we are
    1388           22 :          IF (iw > 0) THEN
    1389           10 :             IF (MOD(ivirial, iprint) == 0) THEN
    1390           10 :                WRITE (iw, '(A,I6,A,I6)') ' Done with config ', ivirial, ' out of ', nvirial
    1391              :             END IF
    1392              :          END IF
    1393              :       END DO
    1394              : 
    1395              :       ! now we integrate this average potential
    1396         1772 :       mayer(:, :) = mayer(:, :)/REAL(nvirial, dp)
    1397              : 
    1398            6 :       DO itemp = 1, nvirial_temps
    1399              :          integral = 0.0_dp
    1400              :          previous_value = 0.0_dp
    1401              :          distance = 0.0E0_dp
    1402              :          ibin = 1
    1403         1180 :          DO
    1404         1184 :             current_division = 0
    1405         1756 :             DO idivision = 1, nintegral_divisions
    1406         1756 :                IF (distance < virial_cutoffs(idivision) - virial_stepsize(idivision)/2.0E0_dp) THEN
    1407              :                   current_division = idivision
    1408              :                   EXIT
    1409              :                END IF
    1410              :             END DO
    1411         1184 :             IF (current_division == 0) EXIT
    1412         1180 :             distance = distance + virial_stepsize(current_division)
    1413              : 
    1414              :             ! now we need to integrate, using the trapazoidal method
    1415              :             ! first, find the value of the square
    1416         1180 :             current_value = mayer(itemp, ibin)*distance**2
    1417         1180 :             square_value = previous_value*virial_stepsize(current_division)
    1418              :             ! now the triangle that sits on top of it, which is half the size of this square...
    1419              :             ! notice this is negative if the current value is less than the previous value
    1420         1180 :             triangle_value = 0.5E0_dp*((current_value - previous_value)*virial_stepsize(current_division))
    1421              : 
    1422         1180 :             integral = integral + square_value + triangle_value
    1423         1180 :             previous_value = current_value
    1424         1180 :             ibin = ibin + 1
    1425              :          END DO
    1426              : 
    1427              :          ! now that the integration is done, compute the second virial that results
    1428            4 :          ave_virial = -2.0E0_dp*pi*integral
    1429              : 
    1430              :          ! convert from CP2K units to something else
    1431            4 :          ave_virial = ave_virial*n_avogadro*angstrom**3/1.0E8_dp**3
    1432              : 
    1433            6 :          IF (iw > 0) THEN
    1434            2 :             WRITE (iw, *)
    1435            2 :             WRITE (iw, *) '*********************************************************************'
    1436            2 :             WRITE (iw, '(A,F12.6,A)') ' ***                Temperature = ', virial_temps(itemp), &
    1437            4 :                '                     ***'
    1438            2 :             WRITE (iw, *) '***                                                               ***'
    1439            2 :             WRITE (iw, '(A,E12.6,A)') ' ***                  B2(T) = ', ave_virial, &
    1440            4 :                ' cm**3/mol               ***'
    1441            2 :             WRITE (iw, *) '*********************************************************************'
    1442            2 :             WRITE (iw, *)
    1443              :          END IF
    1444              :       END DO
    1445              : 
    1446              :       ! deallocate some stuff
    1447            2 :       DEALLOCATE (mc_par)
    1448            2 :       DEALLOCATE (subsys)
    1449            2 :       DEALLOCATE (force_env)
    1450            2 :       DEALLOCATE (particles)
    1451            2 :       DEALLOCATE (cell)
    1452            2 :       DEALLOCATE (virial_cutoffs)
    1453            2 :       DEALLOCATE (virial_stepsize)
    1454            2 :       DEALLOCATE (r_old)
    1455            2 :       DEALLOCATE (mayer)
    1456            2 :       DEALLOCATE (BETA)
    1457              : 
    1458            6 :    END SUBROUTINE mc_compute_virial
    1459              : 
    1460              : END MODULE mc_ensembles
    1461              : 
        

Generated by: LCOV version 2.0-1