LCOV - code coverage report
Current view: top level - src/motion - gopt_f_methods.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 97.8 % 581 568
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 27 27

            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 contains a functional that calculates the energy and its derivatives
      10              : !>      for the geometry optimizer
      11              : !> \par History
      12              : !>      none
      13              : ! **************************************************************************************************
      14              : MODULE gopt_f_methods
      15              : 
      16              :    USE atomic_kind_list_types,          ONLY: atomic_kind_list_type
      17              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      18              :                                               get_atomic_kind_set
      19              :    USE bibliography,                    ONLY: Henkelman1999,&
      20              :                                               cite_reference
      21              :    USE cell_manifold_utils,             ONLY: &
      22              :         cell_manifold_chart_eval, cell_manifold_chart_inv, cell_manifold_chart_pullback_grad, &
      23              :         cell_manifold_pack_hmat, cell_manifold_proj_pressure, cell_manifold_status_label, &
      24              :         cell_manifold_unpack_hmat, info_ok, n_slot
      25              :    USE cell_methods,                    ONLY: canonicalize_cell_matrix,&
      26              :                                               cell_create,&
      27              :                                               init_cell,&
      28              :                                               write_cell
      29              :    USE cell_opt_types,                  ONLY: cell_opt_fold_symmetry_rows,&
      30              :                                               cell_opt_setup_manifold
      31              :    USE cell_opt_utils,                  ONLY: cell_opt_constraint_label,&
      32              :                                               get_dg_dh,&
      33              :                                               spgr_point_group_symmetry_id
      34              :    USE cell_types,                      ONLY: cell_copy,&
      35              :                                               cell_release,&
      36              :                                               cell_type,&
      37              :                                               real_to_scaled,&
      38              :                                               scaled_to_real
      39              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      40              :                                               cp_logger_type,&
      41              :                                               cp_to_string
      42              :    USE cp_output_handling,              ONLY: cp_print_key_finished_output,&
      43              :                                               cp_print_key_unit_nr
      44              :    USE cp_subsys_types,                 ONLY: cp_subsys_get,&
      45              :                                               cp_subsys_set,&
      46              :                                               cp_subsys_type,&
      47              :                                               pack_subsys_particles,&
      48              :                                               unpack_subsys_particles
      49              :    USE cp_units,                        ONLY: cp_unit_from_cp2k
      50              :    USE dimer_methods,                   ONLY: cp_eval_at_ts,&
      51              :                                               dimer_apply_solid_state_geometry
      52              :    USE dimer_types,                     ONLY: dimer_env_type
      53              :    USE dimer_utils,                     ONLY: update_dimer_vec
      54              :    USE distribution_1d_types,           ONLY: distribution_1d_type
      55              :    USE force_env_methods,               ONLY: force_env_calc_energy_force
      56              :    USE force_env_types,                 ONLY: force_env_get,&
      57              :                                               force_env_get_natom,&
      58              :                                               force_env_get_nparticle,&
      59              :                                               force_env_type,&
      60              :                                               use_qmmm,&
      61              :                                               use_qmmmx
      62              :    USE gopt_f_types,                    ONLY: gopt_f_type
      63              :    USE gopt_param_types,                ONLY: gopt_param_type
      64              :    USE input_constants,                 ONLY: default_cell_method_id,&
      65              :                                               default_minimization_method_id,&
      66              :                                               default_shellcore_method_id,&
      67              :                                               default_ts_method_id
      68              :    USE input_cp2k_restarts,             ONLY: write_restart
      69              :    USE input_section_types,             ONLY: section_vals_type,&
      70              :                                               section_vals_val_get
      71              :    USE kinds,                           ONLY: default_string_length,&
      72              :                                               dp,&
      73              :                                               int_8
      74              :    USE machine,                         ONLY: m_flush
      75              :    USE md_energies,                     ONLY: sample_memory
      76              :    USE message_passing,                 ONLY: mp_para_env_type
      77              :    USE motion_utils,                    ONLY: write_simulation_cell,&
      78              :                                               write_stress_tensor_to_file,&
      79              :                                               write_trajectory
      80              :    USE particle_list_types,             ONLY: particle_list_type
      81              :    USE particle_methods,                ONLY: write_final_structure,&
      82              :                                               write_structure_data
      83              :    USE particle_types,                  ONLY: particle_type
      84              :    USE qmmm_util,                       ONLY: apply_qmmm_translate
      85              :    USE qmmmx_util,                      ONLY: apply_qmmmx_translate
      86              :    USE solid_state_coordinates,         ONLY: solid_state_pack,&
      87              :                                               ss_cell_dof
      88              :    USE space_groups,                    ONLY: identify_space_group,&
      89              :                                               print_spgr,&
      90              :                                               spgr_apply_rotations_coord,&
      91              :                                               spgr_apply_rotations_force,&
      92              :                                               spgr_apply_rotations_stress,&
      93              :                                               spgr_project_cell_metric,&
      94              :                                               spgr_write_stress_tensor
      95              :    USE space_groups_types,              ONLY: spgr_type
      96              :    USE virial_methods,                  ONLY: virial_evaluate,&
      97              :                                               virial_update
      98              :    USE virial_types,                    ONLY: virial_type
      99              : #include "../base/base_uses.f90"
     100              : 
     101              :    IMPLICIT NONE
     102              :    PRIVATE
     103              : 
     104              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gopt_f_methods'
     105              :    INTEGER(KIND=int_8), PARAMETER, PRIVATE :: bytes_per_mib = 1024*1024
     106              :    REAL(KIND=dp), PARAMETER, PRIVATE :: restart_manifold_rel_tol = 1.0E-6_dp
     107              :    REAL(KIND=dp), PARAMETER, PRIVATE :: spgr_project_warn_tol = 1.0E-6_dp
     108              : 
     109              :    PUBLIC :: cp_eval_at, &
     110              :              gopt_f_create_x0, &
     111              :              gopt_f_project_coord, gopt_f_project_vector, &
     112              :              gopt_f_restore_cell_manifold, gopt_f_set_h_ref, gopt_f_space_group_show, &
     113              :              print_geo_opt_header, print_geo_opt_nc, &
     114              :              gopt_f_io_init, gopt_f_io, gopt_f_io_finalize, gopt_f_ii, &
     115              :              apply_cell_change
     116              : 
     117              : CONTAINS
     118              : 
     119              : ! **************************************************************************************************
     120              : !> \brief returns the value of the parameters for the actual configuration
     121              : !> \param gopt_env the geometry optimization environment you want the info about
     122              : !>      x0: the parameter vector (is allocated by this routine)
     123              : !> \param x0 ...
     124              : !> \par History
     125              : !>      - Cell optimization revised (06.11.2012,MK)
     126              : ! **************************************************************************************************
     127         1137 :    SUBROUTINE gopt_f_create_x0(gopt_env, x0)
     128              : 
     129              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     130              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: x0
     131              : 
     132              :       INTEGER                                            :: ndof_cell, nparticle, output_unit
     133         1137 :       REAL(KIND=dp), ALLOCATABLE                         :: cartesian(:)
     134              :       TYPE(cell_type), POINTER                           :: cell
     135              :       TYPE(cp_logger_type), POINTER                      :: logger
     136              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     137              : 
     138         1137 :       NULLIFY (cell)
     139         1137 :       NULLIFY (logger)
     140         1137 :       NULLIFY (subsys)
     141              : 
     142         2028 :       SELECT CASE (gopt_env%type_id)
     143              :       CASE (default_minimization_method_id, default_ts_method_id)
     144          891 :          CALL force_env_get(gopt_env%force_env, subsys=subsys)
     145              :          ! before starting we handle the case of translating coordinates (QM/MM)
     146          891 :          CALL apply_qmmm_translation(gopt_env)
     147          891 :          nparticle = force_env_get_nparticle(gopt_env%force_env)
     148          891 :          IF (gopt_env%solid_state_dimer) THEN
     149            4 :             CALL force_env_get(gopt_env%force_env, cell=cell)
     150           24 :             ALLOCATE (x0(3*nparticle + ss_cell_dof), cartesian(3*nparticle))
     151            4 :             CALL pack_subsys_particles(subsys=subsys, r=cartesian)
     152            4 :             CALL solid_state_pack(gopt_env%dimer_env%frame, cell%hmat, cartesian, x0)
     153              :          ELSE
     154         2661 :             ALLOCATE (x0(3*nparticle))
     155          887 :             CALL pack_subsys_particles(subsys=subsys, r=x0)
     156              :          END IF
     157          891 :          IF (gopt_env%spgr%keep_space_group) THEN
     158            4 :             logger => cp_get_default_logger()
     159              :             output_unit = cp_print_key_unit_nr(logger, gopt_env%geo_section, &
     160            4 :                                                "PRINT%PROGRAM_RUN_INFO", extension=".geoLog")
     161            4 :             CALL identify_space_group(subsys, gopt_env%geo_section, gopt_env, output_unit)
     162            4 :             CALL spgr_apply_rotations_coord(gopt_env%spgr, x0)
     163            4 :             CALL print_spgr(gopt_env%spgr)
     164              :             CALL cp_print_key_finished_output(output_unit, logger, gopt_env%geo_section, &
     165            4 :                                               "PRINT%PROGRAM_RUN_INFO")
     166              :          END IF
     167              :       CASE (default_cell_method_id)
     168          246 :          CALL force_env_get(gopt_env%force_env, subsys=subsys, cell=cell)
     169              :          ! identify_space_group builds spgr%cell_ref from gopt_env%h_ref.
     170         6396 :          gopt_env%h_ref = cell%hmat
     171              :          ! before starting we handle the case of translating coordinates (QM/MM)
     172          246 :          CALL apply_qmmm_translation(gopt_env)
     173          246 :          nparticle = force_env_get_nparticle(gopt_env%force_env)
     174              : 
     175          246 :          IF (gopt_env%spgr%keep_space_group) THEN
     176           16 :             logger => cp_get_default_logger()
     177              :             output_unit = cp_print_key_unit_nr(logger, gopt_env%geo_section, &
     178           16 :                                                "PRINT%PROGRAM_RUN_INFO", extension=".geoLog")
     179           16 :             CALL identify_space_group(subsys, gopt_env%geo_section, gopt_env, output_unit)
     180           16 :             CALL print_spgr(gopt_env%spgr)
     181              :             CALL cp_print_key_finished_output(output_unit, logger, gopt_env%geo_section, &
     182           16 :                                               "PRINT%PROGRAM_RUN_INFO")
     183              :          END IF
     184          246 :          CALL build_cell_opt_manifold(gopt_env, emit_warnings=.TRUE.)
     185              : 
     186          246 :          ndof_cell = gopt_env%cell_env%manifold%ndof
     187          246 :          IF (ndof_cell == 0) THEN
     188              :             CALL cp_warn(__LOCATION__, &
     189              :                          "CELL_OPT: Constraints fix all 6 cell degrees of freedom. "// &
     190            2 :                          "The cell remains fixed during optimization.")
     191              :          END IF
     192          738 :          ALLOCATE (x0(3*nparticle + ndof_cell))
     193          246 :          CALL pack_subsys_particles(subsys=subsys, r=x0)
     194              :          CALL cell_manifold_chart_inv(gopt_env%cell_env%manifold, &
     195              :                                       gopt_env%cell_env%manifold%q0, &
     196          246 :                                       x0(3*nparticle + 1:3*nparticle + ndof_cell))
     197          246 :          IF (gopt_env%spgr%keep_space_group) CALL spgr_apply_rotations_coord(gopt_env%spgr, x0)
     198              :       CASE DEFAULT
     199         1137 :          CPABORT("Invalid or not yet implemented type of optimization")
     200              :       END SELECT
     201              : 
     202         1137 :    END SUBROUTINE gopt_f_create_x0
     203              : 
     204              : ! **************************************************************************************************
     205              : !> \brief Apply the QM/MM and QM/MM/X coordinate translations before packing x0
     206              : !> \param gopt_env ...
     207              : ! **************************************************************************************************
     208         1137 :    SUBROUTINE apply_qmmm_translation(gopt_env)
     209              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     210              : 
     211         1137 :       IF (gopt_env%force_env%in_use == use_qmmm) THEN
     212           36 :          CALL apply_qmmm_translate(gopt_env%force_env%qmmm_env)
     213              :       END IF
     214         1137 :       IF (gopt_env%force_env%in_use == use_qmmmx) THEN
     215            0 :          CALL apply_qmmmx_translate(gopt_env%force_env%qmmmx_env)
     216              :       END IF
     217         1137 :    END SUBROUTINE apply_qmmm_translation
     218              : 
     219              : ! **************************************************************************************************
     220              : !> \brief evaluete the potential energy and its gradients using an array
     221              : !>      with same dimension as the particle_set
     222              : !> \param gopt_env the geometry optimization environment
     223              : !> \param x the position where the function should be evaluated
     224              : !> \param f the function value
     225              : !> \param gradient the value of its gradient
     226              : !> \param master ...
     227              : !> \param para_env ...
     228              : !> \par History
     229              : !>       CELL OPTIMIZATION:  Teodoro Laino [tlaino] - University of Zurich - 03.2008
     230              : !>       07.2020 Pierre Cazade [pcazade] Space Group Symmetry
     231              : !> \author Teodoro Laino [tlaino] - University of Zurich - 01.2008
     232              : ! **************************************************************************************************
     233        18875 :    SUBROUTINE cp_eval_at(gopt_env, x, f, gradient, master, &
     234              :                          para_env)
     235              : 
     236              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     237              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: x
     238              :       REAL(KIND=dp), INTENT(OUT), OPTIONAL               :: f
     239              :       REAL(KIND=dp), DIMENSION(:), OPTIONAL, POINTER     :: gradient
     240              :       INTEGER, INTENT(IN)                                :: master
     241              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     242              : 
     243              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'cp_eval_at'
     244              : 
     245              :       INTEGER                                            :: handle, idg, idir, info, ip, ndof_cell, &
     246              :                                                             nparticle, nsize, shell_index
     247              :       LOGICAL                                            :: pressure_projected, unit_is_open
     248              :       REAL(KIND=dp)                                      :: f_ts, pdiff
     249              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: av_ptens, av_ptens_opt
     250        18875 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: gradient_ts
     251              :       REAL(KIND=dp), DIMENSION(n_slot)                   :: q_now, raw_cell_grad
     252              :       TYPE(cell_type), POINTER                           :: cell
     253              :       TYPE(cp_logger_type), POINTER                      :: logger
     254              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     255              :       TYPE(particle_list_type), POINTER                  :: core_particles, particles, &
     256              :                                                             shell_particles
     257              :       TYPE(spgr_type), POINTER                           :: spgr
     258              :       TYPE(virial_type), POINTER                         :: virial
     259              : 
     260        18875 :       NULLIFY (cell)
     261        18875 :       NULLIFY (core_particles)
     262        18875 :       NULLIFY (gradient_ts)
     263        18875 :       NULLIFY (logger)
     264        18875 :       NULLIFY (particles)
     265        18875 :       NULLIFY (shell_particles)
     266        18875 :       NULLIFY (subsys)
     267        18875 :       NULLIFY (virial)
     268        18875 :       NULLIFY (spgr)
     269              : 
     270        18875 :       CALL timeset(routineN, handle)
     271              : 
     272        18875 :       CALL force_env_get(gopt_env%force_env, subsys=subsys, cell=cell)
     273              :       CALL cp_subsys_get(subsys, &
     274              :                          core_particles=core_particles, &
     275              :                          particles=particles, &
     276              :                          shell_particles=shell_particles, &
     277        18875 :                          virial=virial)
     278              : 
     279        18875 :       spgr => gopt_env%spgr
     280              : 
     281        27442 :       SELECT CASE (gopt_env%type_id)
     282              :       CASE (default_minimization_method_id, default_ts_method_id)
     283         8567 :          IF (gopt_env%solid_state_dimer) THEN
     284           52 :             CALL dimer_apply_solid_state_geometry(gopt_env, x)
     285              :          ELSE
     286         8515 :             CALL unpack_subsys_particles(subsys=subsys, r=x)
     287              :          END IF
     288         8567 :          CALL write_structure_data(particles%els, cell, gopt_env%motion_section)
     289        15294 :          SELECT CASE (gopt_env%type_id)
     290              :          CASE (default_minimization_method_id)
     291              :             ! Geometry Minimization
     292              :             CALL force_env_calc_energy_force(gopt_env%force_env, &
     293              :                                              calc_force=PRESENT(gradient), &
     294         6727 :                                              require_consistent_energy_force=gopt_env%require_consistent_energy_force)
     295              :             ! Possibly take the potential energy
     296         6727 :             IF (PRESENT(f)) THEN
     297         6727 :                CALL force_env_get(gopt_env%force_env, potential_energy=f)
     298              :             END IF
     299              :             ! Possibly take the gradients
     300         6727 :             IF (PRESENT(gradient)) THEN
     301         6187 :                IF (master == para_env%mepos) THEN ! we are on the master
     302         6036 :                   CALL pack_subsys_particles(subsys=subsys, f=gradient, fscale=-1.0_dp)
     303         6036 :                   IF (spgr%keep_space_group) THEN
     304            8 :                      CALL spgr_apply_rotations_force(spgr, gradient)
     305            8 :                      CALL unpack_subsys_particles(subsys=subsys, f=gradient, fscale=-1.0_dp)
     306              :                   END IF
     307              :                END IF
     308              :             END IF
     309              :          CASE (default_ts_method_id)
     310              :             ! Transition State Optimization
     311         1840 :             IF (gopt_env%solid_state_dimer) THEN
     312          156 :                ALLOCATE (gradient_ts(SIZE(x)))
     313              :             ELSE
     314         5364 :                ALLOCATE (gradient_ts(3*particles%n_els))
     315              :             END IF
     316              :             ! Real calculation of energy and forces for transition state optimization:
     317              :             ! When doing dimer methods forces have to be always computed since the function
     318              :             ! to minimize is not the energy but the effective force
     319         1840 :             CALL cp_eval_at_ts(gopt_env, x, f_ts, gradient_ts, calc_force=.TRUE.)
     320         1840 :             CALL cite_reference(Henkelman1999)
     321              :             ! Possibly take the potential energy
     322         1840 :             IF (PRESENT(f)) f = f_ts
     323              :             ! Possibly take the gradients
     324         1840 :             IF (PRESENT(gradient)) THEN
     325          872 :                IF (master == para_env%mepos) THEN ! we are on the master
     326          872 :                   CPASSERT(ASSOCIATED(gradient))
     327        30148 :                   gradient = gradient_ts
     328              :                END IF
     329              :             END IF
     330        10407 :             DEALLOCATE (gradient_ts)
     331              :          END SELECT
     332              :          ! This call is necessary for QM/MM if a Translation is applied
     333              :          ! this makes the geometry optimizer consistent
     334         8567 :          IF (gopt_env%solid_state_dimer) THEN
     335           52 :             CALL dimer_apply_solid_state_geometry(gopt_env, x)
     336           52 :             IF (.NOT. gopt_env%dimer_rotation) THEN
     337           30 :                CALL unpack_subsys_particles(subsys, f=gopt_env%dimer_env%centre_force)
     338           30 :                virial = gopt_env%dimer_env%centre_virial
     339              :             END IF
     340              :          ELSE
     341         8515 :             CALL unpack_subsys_particles(subsys=subsys, r=x)
     342              :          END IF
     343              :       CASE (default_cell_method_id)
     344              :          ! Check for VIRIAL
     345         9988 :          IF (.NOT. virial%pv_availability) THEN
     346              :             CALL cp_abort(__LOCATION__, &
     347              :                           "For the CELL_OPT task, the FORCE_EVAL/STRESS_TENSOR "// &
     348              :                           "keyword MUST be defined in the input file for the "// &
     349            0 :                           "evaluation of the stress tensor, but none is found!")
     350              :          END IF
     351              : 
     352         9988 :          CALL apply_cell_change(gopt_env, cell, x, update_forces=.FALSE.)
     353              :          ! Possibly output the new cell used for the next calculation
     354         9988 :          CALL write_cell(cell, gopt_env%geo_section)
     355              :          ! Compute the pressure tensor
     356         9988 :          BLOCK
     357              :             TYPE(virial_type) :: virial_avg
     358              :             CALL force_env_calc_energy_force(gopt_env%force_env, &
     359              :                                              calc_force=PRESENT(gradient), &
     360         9988 :                                              require_consistent_energy_force=gopt_env%require_consistent_energy_force)
     361              :             ! Possibly take the potential energy
     362         9988 :             virial_avg = virial
     363         9988 :             CALL virial_update(virial_avg, subsys, para_env)
     364         9988 :             IF (PRESENT(f)) THEN
     365         9988 :                CALL force_env_get(gopt_env%force_env, potential_energy=f)
     366              :             END IF
     367              :             ! Possibly take the gradients
     368      2477024 :             IF (PRESENT(gradient)) THEN
     369         7768 :                CPASSERT(ANY(virial_avg%pv_total /= 0))
     370              :                ! Convert the average ptens
     371       100984 :                av_ptens(:, :) = virial_avg%pv_total(:, :)/cell%deth
     372         7768 :                IF (master == para_env%mepos) THEN ! we are on the master
     373         5985 :                   CPASSERT(ASSOCIATED(gradient))
     374         5985 :                   nparticle = force_env_get_nparticle(gopt_env%force_env)
     375         5985 :                   nsize = 3*nparticle
     376         5985 :                   ndof_cell = gopt_env%cell_env%manifold%ndof
     377         5985 :                   CPASSERT((SIZE(gradient) == nsize + ndof_cell))
     378         5985 :                   CALL pack_subsys_particles(subsys=subsys, f=gradient(1:nsize), fscale=-1.0_dp)
     379         5985 :                   CALL apply_cell_change(gopt_env, cell, gradient, update_forces=.TRUE.)
     380         5985 :                   IF (spgr%keep_space_group) THEN
     381          404 :                      CALL spgr_apply_rotations_force(spgr, gradient)
     382          404 :                      CALL spgr_apply_rotations_stress(spgr, cell, av_ptens)
     383              :                      ! Non-IO ranks have iunit <= 0. On IO ranks, CP2K closes file print-key
     384              :                      ! units between steps, so check OPENED first.
     385          404 :                      IF (spgr%iunit > 0) THEN
     386          203 :                         INQUIRE (UNIT=spgr%iunit, OPENED=unit_is_open)
     387          203 :                         IF (unit_is_open) THEN
     388          203 :                            CALL spgr_write_stress_tensor(av_ptens, spgr)
     389              :                         ELSE
     390            0 :                            logger => cp_get_default_logger()
     391              :                            spgr%iunit = cp_print_key_unit_nr(logger, gopt_env%geo_section, &
     392              :                                                              "PRINT%PROGRAM_RUN_INFO", &
     393            0 :                                                              extension=".geoLog")
     394            0 :                            CALL spgr_write_stress_tensor(av_ptens, spgr)
     395              :                            CALL cp_print_key_finished_output(spgr%iunit, logger, &
     396              :                                                              gopt_env%geo_section, &
     397            0 :                                                              "PRINT%PROGRAM_RUN_INFO")
     398              :                         END IF
     399              :                      END IF
     400              :                   END IF
     401              :                   av_ptens_opt = MATMUL(gopt_env%cell_env%input_to_opt, &
     402       472815 :                                         MATMUL(av_ptens, gopt_env%cell_env%opt_to_input))
     403              :                   CALL get_dg_dh(raw_cell_grad, av_ptens_opt, gopt_env%cell_env%pres_ext, &
     404              :                                  gopt_env%cell_env%opt_cell, gopt_env%cell_env%mtrx, &
     405         5985 :                                  gopt_env%cell_env%pres_int)
     406              :                   ! Evaluate projected pressure and reduced gradient at current cell q_now.
     407         5985 :                   q_now = cell_manifold_pack_hmat(gopt_env%cell_env%opt_cell%hmat)
     408              :                   CALL cell_manifold_proj_pressure(gopt_env%cell_env%manifold, q_now, &
     409         5985 :                                                    raw_cell_grad, pdiff, pressure_projected, info)
     410         5985 :                   IF (info /= info_ok) THEN
     411              :                      CALL cp_abort(__LOCATION__, &
     412              :                                    "CELL_OPT: cannot compute projected pressure ("// &
     413            0 :                                    TRIM(cell_manifold_status_label(info))//").")
     414              :                   END IF
     415         5985 :                   IF (pressure_projected) THEN
     416         3963 :                      gopt_env%cell_env%pres_constr = gopt_env%cell_env%pres_ext + pdiff
     417              :                   ELSE
     418         2022 :                      gopt_env%cell_env%pres_constr = gopt_env%cell_env%pres_ext
     419              :                   END IF
     420              :                   CALL cell_manifold_chart_pullback_grad(gopt_env%cell_env%manifold, q_now, &
     421              :                                                          raw_cell_grad, &
     422              :                                                          gradient(nsize + 1:nsize + ndof_cell), &
     423         5985 :                                                          info)
     424        11970 :                   IF (info /= info_ok) THEN
     425              :                      CALL cp_abort(__LOCATION__, &
     426              :                                    "CELL_OPT: cannot compute constrained cell gradient ("// &
     427            0 :                                    TRIM(cell_manifold_status_label(info))//").")
     428              :                   END IF
     429              :                END IF
     430              :                ! some callers expect pres_int to be available on all ranks. Also, here master is not necessarily a single rank.
     431              :                ! Assume at least master==0
     432         7768 :                CALL para_env%bcast(gopt_env%cell_env%pres_int, 0)
     433         7768 :                IF (gopt_env%cell_env%manifold%k > 0) THEN
     434         5394 :                   CALL para_env%bcast(gopt_env%cell_env%pres_constr, 0)
     435              :                END IF
     436              :             END IF
     437              :          END BLOCK
     438              :       CASE (default_shellcore_method_id)
     439          320 :          idg = 0
     440        31040 :          DO ip = 1, particles%n_els
     441        30720 :             shell_index = particles%els(ip)%shell_index
     442        31040 :             IF (shell_index /= 0) THEN
     443       122880 :                DO idir = 1, 3
     444        92160 :                   idg = 3*(shell_index - 1) + idir
     445       122880 :                   shell_particles%els(shell_index)%r(idir) = core_particles%els(ip)%r(idir) - x(idg)
     446              :                END DO
     447              :             END IF
     448              :          END DO
     449          320 :          CALL write_structure_data(particles%els, cell, gopt_env%motion_section)
     450              : 
     451              :          ! Shell-core optimization
     452              :          CALL force_env_calc_energy_force(gopt_env%force_env, &
     453              :                                           calc_force=PRESENT(gradient), &
     454          320 :                                           require_consistent_energy_force=gopt_env%require_consistent_energy_force)
     455              : 
     456              :          ! Possibly take the potential energy
     457          320 :          IF (PRESENT(f)) THEN
     458          320 :             CALL force_env_get(gopt_env%force_env, potential_energy=f)
     459              :          END IF
     460              : 
     461              :          ! Possibly take the gradients
     462          320 :          IF (PRESENT(gradient)) THEN
     463          320 :             IF (master == para_env%mepos) THEN ! we are on the master
     464          320 :                CPASSERT(ASSOCIATED(gradient))
     465          320 :                idg = 0
     466        31040 :                DO ip = 1, shell_particles%n_els
     467       123200 :                   DO idir = 1, 3
     468        92160 :                      idg = idg + 1
     469       122880 :                      gradient(idg) = -(core_particles%els(ip)%f(idir) - shell_particles%els(ip)%f(idir))
     470              :                   END DO
     471              :                END DO
     472              :             END IF
     473              :          END IF
     474              :       CASE DEFAULT
     475        18875 :          CPABORT("Invalid or not yet implemented type of optimization")
     476              :       END SELECT
     477              : 
     478        18875 :       CALL timestop(handle)
     479              : 
     480        18875 :    END SUBROUTINE cp_eval_at
     481              : 
     482              : ! **************************************************************************************************
     483              : !> \brief Prints iteration step of the optimization procedure on screen
     484              : !> \param its ...
     485              : !> \param output_unit ...
     486              : !> \author Teodoro Laino [tlaino] - University of Zurich - 03.2008
     487              : ! **************************************************************************************************
     488        10006 :    SUBROUTINE gopt_f_ii(its, output_unit)
     489              : 
     490              :       INTEGER, INTENT(IN)                                :: its, output_unit
     491              : 
     492        10006 :       IF (output_unit > 0) THEN
     493         5162 :          WRITE (UNIT=output_unit, FMT="(/,T2,26('-'))")
     494         5162 :          WRITE (UNIT=output_unit, FMT="(T2,A,I6)") "OPTIMIZATION STEP: ", its
     495         5162 :          WRITE (UNIT=output_unit, FMT="(T2,26('-'))")
     496         5162 :          CALL m_flush(output_unit)
     497              :       END IF
     498              : 
     499        10006 :    END SUBROUTINE gopt_f_ii
     500              : 
     501              : ! **************************************************************************************************
     502              : !> \brief Retrieves the reporting metadata used during an optimization step.
     503              : !> \param gopt_env Optimization environment supplying print configuration and units
     504              : !> \param force_env Force environment used for memory sampling
     505              : !> \param max_memory Sampled memory, or zero if memory reporting is disabled
     506              : !> \param energy_unit Energy reporting unit
     507              : !> \param stress_unit Stress reporting unit
     508              : ! **************************************************************************************************
     509        11197 :    SUBROUTINE get_reporting_metadata(gopt_env, force_env, max_memory, energy_unit, stress_unit)
     510              : 
     511              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     512              :       TYPE(force_env_type), POINTER                      :: force_env
     513              :       INTEGER(KIND=int_8), INTENT(OUT)                   :: max_memory
     514              :       CHARACTER(LEN=default_string_length), INTENT(OUT)  :: energy_unit, stress_unit
     515              : 
     516              :       LOGICAL                                            :: print_memory
     517              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     518              : 
     519        11197 :       NULLIFY (para_env)
     520        11197 :       CALL section_vals_val_get(gopt_env%motion_section, "PRINT%MEMORY_INFO", l_val=print_memory)
     521        11197 :       max_memory = 0
     522        11197 :       IF (print_memory) THEN
     523        11197 :          CALL force_env_get(force_env, para_env=para_env)
     524        11197 :          max_memory = sample_memory(para_env)
     525              :       END IF
     526              : 
     527              :       CALL section_vals_val_get(gopt_env%force_env%force_env_section, &
     528              :                                 "PRINT%PROGRAM_RUN_INFO%ENERGY_UNIT", &
     529        11197 :                                 c_val=energy_unit)
     530              :       CALL section_vals_val_get(gopt_env%force_env%force_env_section, &
     531              :                                 "PRINT%STRESS_TENSOR%STRESS_UNIT", &
     532        11197 :                                 c_val=stress_unit)
     533              : 
     534        11197 :    END SUBROUTINE get_reporting_metadata
     535              : 
     536              : ! **************************************************************************************************
     537              : !> \brief Handles the Output during an optimization run
     538              : !> \param gopt_env ...
     539              : !> \param output_unit ...
     540              : !> \param opt_energy ...
     541              : !> \param wildcard ...
     542              : !> \param its ...
     543              : !> \param used_time ...
     544              : !> \author Teodoro Laino [tlaino] - University of Zurich - 03.2008
     545              : ! **************************************************************************************************
     546         1193 :    SUBROUTINE gopt_f_io_init(gopt_env, output_unit, opt_energy, wildcard, its, used_time)
     547              : 
     548              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     549              :       INTEGER, INTENT(IN)                                :: output_unit
     550              :       REAL(KIND=dp)                                      :: opt_energy
     551              :       CHARACTER(LEN=5)                                   :: wildcard
     552              :       INTEGER, INTENT(IN)                                :: its
     553              :       REAL(KIND=dp)                                      :: used_time
     554              : 
     555              :       CHARACTER(LEN=default_string_length)               :: energy_unit, stress_unit
     556              :       INTEGER(KIND=int_8)                                :: max_memory
     557              :       REAL(KIND=dp)                                      :: pres_int
     558              : 
     559         1193 :       CALL get_reporting_metadata(gopt_env, gopt_env%force_env, max_memory, energy_unit, stress_unit)
     560              : 
     561         2180 :       SELECT CASE (gopt_env%type_id)
     562              :       CASE (default_ts_method_id, default_minimization_method_id)
     563              :          ! Geometry Optimization (Minimization and Transition State Search)
     564          987 :          IF (.NOT. gopt_env%dimer_rotation) THEN
     565              :             CALL write_cycle_infos(output_unit, &
     566              :                                    it=its, &
     567              :                                    etot=opt_energy, &
     568              :                                    wildcard=wildcard, &
     569              :                                    used_time=used_time, &
     570              :                                    max_memory=max_memory, &
     571              :                                    energy_unit=energy_unit, &
     572          845 :                                    stress_unit=stress_unit)
     573              :          ELSE
     574              :             CALL write_rot_cycle_infos(output_unit, &
     575              :                                        it=its, &
     576              :                                        etot=opt_energy, &
     577              :                                        dimer_env=gopt_env%dimer_env, &
     578              :                                        wildcard=wildcard, &
     579              :                                        used_time=used_time, &
     580          142 :                                        max_memory=max_memory)
     581              :          END IF
     582              :       CASE (default_cell_method_id)
     583              :          ! Cell Optimization
     584          186 :          pres_int = gopt_env%cell_env%pres_int
     585              :          CALL write_cycle_infos(output_unit, &
     586              :                                 it=its, &
     587              :                                 etot=opt_energy, &
     588              :                                 pres_int=pres_int, &
     589              :                                 wildcard=wildcard, &
     590              :                                 used_time=used_time, &
     591              :                                 max_memory=max_memory, &
     592              :                                 energy_unit=energy_unit, &
     593          186 :                                 stress_unit=stress_unit)
     594              :       CASE (default_shellcore_method_id)
     595              :          CALL write_cycle_infos(output_unit, &
     596              :                                 it=its, &
     597              :                                 etot=opt_energy, &
     598              :                                 wildcard=wildcard, &
     599              :                                 used_time=used_time, &
     600              :                                 max_memory=max_memory, &
     601              :                                 energy_unit=energy_unit, &
     602         1193 :                                 stress_unit=stress_unit)
     603              :       END SELECT
     604              : 
     605         1193 :    END SUBROUTINE gopt_f_io_init
     606              : 
     607              : ! **************************************************************************************************
     608              : !> \brief Handles the Output during an optimization run
     609              : !> \param gopt_env ...
     610              : !> \param force_env ...
     611              : !> \param root_section ...
     612              : !> \param its ...
     613              : !> \param opt_energy ...
     614              : !> \param output_unit ...
     615              : !> \param eold ...
     616              : !> \param emin ...
     617              : !> \param wildcard ...
     618              : !> \param gopt_param ...
     619              : !> \param ndf ...
     620              : !> \param dx ...
     621              : !> \param xi ...
     622              : !> \param conv ...
     623              : !> \param pred ...
     624              : !> \param rat ...
     625              : !> \param step ...
     626              : !> \param rad ...
     627              : !> \param used_time ...
     628              : !> \author Teodoro Laino [tlaino] - University of Zurich - 03.2008
     629              : ! **************************************************************************************************
     630        20008 :    SUBROUTINE gopt_f_io(gopt_env, force_env, root_section, its, opt_energy, &
     631        10004 :                         output_unit, eold, emin, wildcard, gopt_param, ndf, dx, xi, conv, pred, rat, &
     632              :                         step, rad, used_time)
     633              : 
     634              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     635              :       TYPE(force_env_type), POINTER                      :: force_env
     636              :       TYPE(section_vals_type), POINTER                   :: root_section
     637              :       INTEGER, INTENT(IN)                                :: its
     638              :       REAL(KIND=dp), INTENT(IN)                          :: opt_energy
     639              :       INTEGER, INTENT(IN)                                :: output_unit
     640              :       REAL(KIND=dp)                                      :: eold, emin
     641              :       CHARACTER(LEN=5)                                   :: wildcard
     642              :       TYPE(gopt_param_type), POINTER                     :: gopt_param
     643              :       INTEGER, INTENT(IN), OPTIONAL                      :: ndf
     644              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN), OPTIONAL  :: dx
     645              :       REAL(KIND=dp), DIMENSION(:), OPTIONAL, POINTER     :: xi
     646              :       LOGICAL, OPTIONAL                                  :: conv
     647              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: pred, rat, step, rad
     648              :       REAL(KIND=dp)                                      :: used_time
     649              : 
     650              :       CHARACTER(LEN=default_string_length)               :: energy_unit, stress_unit
     651              :       INTEGER(KIND=int_8)                                :: max_memory
     652              :       REAL(KIND=dp)                                      :: pres_diff, pres_diff_constr, pres_int, &
     653              :                                                             pres_tol
     654              : 
     655        10004 :       CALL get_reporting_metadata(gopt_env, force_env, max_memory, energy_unit, stress_unit)
     656              : 
     657        15214 :       SELECT CASE (gopt_env%type_id)
     658              :       CASE (default_ts_method_id, default_minimization_method_id)
     659              :          ! Geometry Optimization (Minimization and Transition State Search)
     660         5210 :          IF (.NOT. gopt_env%dimer_rotation) THEN
     661         4472 :             IF (gopt_env%solid_state_dimer) THEN
     662              :                CALL geo_opt_io(force_env=force_env, root_section=root_section, motion_section=gopt_env%motion_section, &
     663            8 :                                its=its, opt_energy=gopt_env%dimer_env%enthalpy)
     664              :             ELSE
     665              :                CALL geo_opt_io(force_env=force_env, root_section=root_section, &
     666         4464 :                                motion_section=gopt_env%motion_section, its=its, opt_energy=opt_energy)
     667              :             END IF
     668              :             CALL write_cycle_infos(output_unit, &
     669              :                                    it=its, &
     670              :                                    etot=opt_energy, &
     671              :                                    ediff=(opt_energy - eold), &
     672              :                                    pred=pred, &
     673              :                                    rat=rat, &
     674              :                                    step=step, &
     675              :                                    rad=rad, &
     676              :                                    emin=emin, &
     677              :                                    wildcard=wildcard, &
     678              :                                    used_time=used_time, &
     679              :                                    max_memory=max_memory, &
     680              :                                    energy_unit=energy_unit, &
     681         4472 :                                    stress_unit=stress_unit)
     682              :             ! Possibly check convergence
     683         4472 :             IF (PRESENT(conv)) THEN
     684         4472 :                CPASSERT(PRESENT(ndf))
     685         4472 :                CPASSERT(PRESENT(dx))
     686         4472 :                CPASSERT(PRESENT(xi))
     687         4472 :                IF (gopt_env%solid_state_dimer) THEN
     688              :                   CALL check_and_report_convergence(output_unit, gopt_param, ndf, dx, xi, conv, &
     689            8 :                                                     max_memory, stress_unit, curvature=gopt_env%dimer_env%rot%curvature)
     690              :                ELSE
     691              :                   CALL check_and_report_convergence(output_unit, gopt_param, ndf, dx, xi, conv, &
     692         4464 :                                                     max_memory, stress_unit)
     693              :                END IF
     694              :             END IF
     695              :          ELSE
     696          738 :             CALL update_dimer_vec(gopt_env%dimer_env, gopt_env%motion_section)
     697          738 :             CALL write_restart(force_env=force_env, root_section=root_section)
     698              :             CALL write_rot_cycle_infos(output_unit, its, opt_energy, opt_energy - eold, emin, gopt_env%dimer_env, &
     699          738 :                                        wildcard=wildcard, used_time=used_time, max_memory=max_memory)
     700              :             ! Possibly check convergence
     701          738 :             IF (PRESENT(conv)) THEN
     702          738 :                CPASSERT(ASSOCIATED(gopt_env%dimer_env))
     703          738 :                CALL check_rot_conv(gopt_env%dimer_env, output_unit, conv)
     704              :             END IF
     705              :          END IF
     706              :       CASE (default_cell_method_id)
     707              :          ! Cell Optimization
     708         4644 :          pres_diff = gopt_env%cell_env%pres_int - gopt_env%cell_env%pres_ext
     709         4644 :          pres_int = gopt_env%cell_env%pres_int
     710         4644 :          pres_tol = gopt_env%cell_env%pres_tol
     711              :          CALL geo_opt_io(force_env=force_env, root_section=root_section, &
     712         4644 :                          motion_section=gopt_env%motion_section, its=its, opt_energy=opt_energy)
     713              :          CALL write_cycle_infos(output_unit, &
     714              :                                 it=its, &
     715              :                                 etot=opt_energy, &
     716              :                                 ediff=(opt_energy - eold), &
     717              :                                 pred=pred, &
     718              :                                 rat=rat, &
     719              :                                 step=step, &
     720              :                                 rad=rad, &
     721              :                                 emin=emin, &
     722              :                                 pres_int=pres_int, &
     723              :                                 wildcard=wildcard, &
     724              :                                 used_time=used_time, &
     725              :                                 max_memory=max_memory, &
     726              :                                 energy_unit=energy_unit, &
     727         4644 :                                 stress_unit=stress_unit)
     728              :          ! Possibly check convergence
     729         4644 :          IF (PRESENT(conv)) THEN
     730         4644 :             CPASSERT(PRESENT(ndf))
     731         4644 :             CPASSERT(PRESENT(dx))
     732         4644 :             CPASSERT(PRESENT(xi))
     733         4644 :             IF (gopt_env%cell_env%manifold%k == 0) THEN
     734              :                CALL check_and_report_convergence(output_unit, gopt_param, ndf, dx, xi, conv, &
     735         1354 :                                                  max_memory, stress_unit, pres_diff, pres_tol)
     736              :             ELSE
     737         3290 :                pres_diff_constr = gopt_env%cell_env%pres_constr - gopt_env%cell_env%pres_ext
     738              :                CALL check_and_report_convergence(output_unit, gopt_param, ndf, dx, xi, conv, &
     739              :                                                  max_memory, stress_unit, pres_diff, pres_tol, &
     740         3290 :                                                  pres_diff_constr)
     741              :             END IF
     742              :          END IF
     743              :       CASE (default_shellcore_method_id)
     744              :          CALL write_cycle_infos(output_unit, &
     745              :                                 it=its, &
     746              :                                 etot=opt_energy, &
     747              :                                 ediff=(opt_energy - eold), &
     748              :                                 pred=pred, &
     749              :                                 rat=rat, &
     750              :                                 step=step, &
     751              :                                 rad=rad, &
     752              :                                 emin=emin, &
     753              :                                 wildcard=wildcard, &
     754              :                                 used_time=used_time, &
     755              :                                 max_memory=max_memory, &
     756              :                                 energy_unit=energy_unit, &
     757          150 :                                 stress_unit=stress_unit)
     758              :          ! Possibly check convergence
     759        10154 :          IF (PRESENT(conv)) THEN
     760          150 :             CPASSERT(PRESENT(ndf))
     761          150 :             CPASSERT(PRESENT(dx))
     762          150 :             CPASSERT(PRESENT(xi))
     763              :             CALL check_and_report_convergence(output_unit, gopt_param, ndf, dx, xi, conv, &
     764          150 :                                               max_memory, stress_unit)
     765              :          END IF
     766              :       END SELECT
     767              : 
     768        10004 :    END SUBROUTINE gopt_f_io
     769              : 
     770              : ! **************************************************************************************************
     771              : !> \brief Handles the Output at the end of an optimization run
     772              : !> \param gopt_env ...
     773              : !> \param force_env ...
     774              : !> \param x0 ...
     775              : !> \param conv ...
     776              : !> \param its ...
     777              : !> \param root_section ...
     778              : !> \param para_env ...
     779              : !> \param master ...
     780              : !> \param output_unit ...
     781              : !> \author Teodoro Laino [tlaino] - University of Zurich - 03.2008
     782              : ! **************************************************************************************************
     783         1299 :    RECURSIVE SUBROUTINE gopt_f_io_finalize(gopt_env, force_env, x0, conv, its, root_section, &
     784              :                                            para_env, master, output_unit)
     785              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
     786              :       TYPE(force_env_type), POINTER                      :: force_env
     787              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: x0
     788              :       LOGICAL                                            :: conv
     789              :       INTEGER                                            :: its
     790              :       TYPE(section_vals_type), POINTER                   :: root_section
     791              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     792              :       INTEGER, INTENT(IN)                                :: master, output_unit
     793              : 
     794         1299 :       IF (gopt_env%eval_opt_geo) THEN
     795         1279 :          IF (.NOT. gopt_env%dimer_rotation) THEN
     796              :             CALL write_final_info(output_unit, conv, its, gopt_env, x0, master, &
     797         1137 :                                   para_env, force_env, gopt_env%motion_section, root_section)
     798              :          ELSE
     799          142 :             CALL update_dimer_vec(gopt_env%dimer_env, gopt_env%motion_section)
     800          142 :             CALL write_restart(force_env=force_env, root_section=root_section)
     801              :          END IF
     802              :       END IF
     803              : 
     804         1299 :    END SUBROUTINE gopt_f_io_finalize
     805              : 
     806              : ! **************************************************************************************************
     807              : !> \brief ...
     808              : !> \param output_unit ...
     809              : !> \param it ...
     810              : !> \param etot ...
     811              : !> \param ediff ...
     812              : !> \param pred ...
     813              : !> \param rat ...
     814              : !> \param step ...
     815              : !> \param rad ...
     816              : !> \param emin ...
     817              : !> \param pres_int ...
     818              : !> \param wildcard ...
     819              : !> \param used_time ...
     820              : !> \param max_memory ...
     821              : !> \param energy_unit ...
     822              : !> \param stress_unit ...
     823              : ! **************************************************************************************************
     824        10317 :    SUBROUTINE write_cycle_infos(output_unit, it, etot, ediff, pred, rat, step, rad, emin, &
     825              :                                 pres_int, wildcard, used_time, max_memory, energy_unit, stress_unit)
     826              : 
     827              :       INTEGER, INTENT(IN)                                :: output_unit, it
     828              :       REAL(KIND=dp), INTENT(IN)                          :: etot
     829              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: ediff, pred, rat, step, rad, emin, &
     830              :                                                             pres_int
     831              :       CHARACTER(LEN=5), INTENT(IN)                       :: wildcard
     832              :       REAL(KIND=dp), INTENT(IN)                          :: used_time
     833              :       INTEGER(KIND=int_8), INTENT(IN)                    :: max_memory
     834              :       CHARACTER(LEN=default_string_length), INTENT(IN)   :: energy_unit, stress_unit
     835              : 
     836              :       CHARACTER(LEN=5)                                   :: tag
     837              : 
     838        10317 :       IF (output_unit > 0) THEN
     839         5341 :          tag = "OPT| "
     840         5341 :          WRITE (UNIT=output_unit, FMT="(/,T2,A)") tag//REPEAT("*", 74)
     841              :          WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,I25)") &
     842         5341 :             tag//"Step number", it
     843              :          WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,A25)") &
     844         5341 :             tag//"Optimization method", wildcard
     845              :          WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
     846         5341 :             tag//"Total energy ["//TRIM(ADJUSTL(energy_unit))//"]", &
     847        10682 :             cp_unit_from_cp2k(etot, TRIM(energy_unit))
     848         5341 :          IF (PRESENT(pres_int)) THEN
     849              :             WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
     850         2415 :                tag//"Internal pressure ["//TRIM(ADJUSTL(stress_unit))//"]", &
     851         4830 :                cp_unit_from_cp2k(pres_int, TRIM(stress_unit))
     852              :          END IF
     853         5341 :          IF (PRESENT(ediff)) THEN
     854              :             WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
     855         4792 :                tag//"Effective energy change ["//TRIM(ADJUSTL(energy_unit))//"]", &
     856         9584 :                cp_unit_from_cp2k(ediff, TRIM(energy_unit))
     857              :          END IF
     858         5341 :          IF (PRESENT(pred)) THEN
     859              :             WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
     860         2454 :                tag//"Predicted energy change ["//TRIM(ADJUSTL(energy_unit))//"]", &
     861         4908 :                cp_unit_from_cp2k(pred, TRIM(energy_unit))
     862              :          END IF
     863         5341 :          IF (PRESENT(rat)) THEN
     864              :             WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
     865         2456 :                tag//"Scaling factor", rat
     866              :          END IF
     867         5341 :          IF (PRESENT(step)) THEN
     868              :             WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
     869         2456 :                tag//"Step size", step
     870              :          END IF
     871         5341 :          IF (PRESENT(rad)) THEN
     872              :             WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
     873         2456 :                tag//"Trust radius", rad
     874              :          END IF
     875         5341 :          IF (PRESENT(emin)) THEN
     876         4792 :             IF (etot < emin) THEN
     877              :                WRITE (UNIT=output_unit, FMT="(T2,A,T77,A4)") &
     878         4376 :                   tag//"Decrease in energy", " YES"
     879              :             ELSE
     880              :                WRITE (UNIT=output_unit, FMT="(T2,A,T77,A4)") &
     881          416 :                   tag//"Decrease in energy", "  NO"
     882              :             END IF
     883              :          END IF
     884              :          WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.3)") &
     885         5341 :             tag//"Used time [s]", used_time
     886         5341 :          IF (it == 0) THEN
     887          543 :             WRITE (UNIT=output_unit, FMT="(T2,A)") tag//REPEAT("*", 74)
     888          543 :             IF (max_memory /= 0) THEN
     889              :                WRITE (UNIT=output_unit, FMT="(T2,A,T60,1X,I20)") &
     890          543 :                   tag//"Estimated peak process memory [MiB]", &
     891         1086 :                   (max_memory + bytes_per_mib - 1)/bytes_per_mib
     892              :             END IF
     893              :          END IF
     894              :       END IF
     895              : 
     896        10317 :    END SUBROUTINE write_cycle_infos
     897              : 
     898              : ! **************************************************************************************************
     899              : !> \brief ...
     900              : !> \param output_unit ...
     901              : !> \param it ...
     902              : !> \param etot ...
     903              : !> \param ediff ...
     904              : !> \param emin ...
     905              : !> \param dimer_env ...
     906              : !> \param used_time ...
     907              : !> \param wildcard ...
     908              : !> \param max_memory ...
     909              : !> \date  01.2008
     910              : !> \author Luca Bellucci and Teodoro Laino - created [tlaino]
     911              : ! **************************************************************************************************
     912          880 :    SUBROUTINE write_rot_cycle_infos(output_unit, it, etot, ediff, emin, dimer_env, used_time, &
     913              :                                     wildcard, max_memory)
     914              : 
     915              :       INTEGER, INTENT(IN)                                :: output_unit, it
     916              :       REAL(KIND=dp), INTENT(IN)                          :: etot
     917              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: ediff, emin
     918              :       TYPE(dimer_env_type), POINTER                      :: dimer_env
     919              :       REAL(KIND=dp), INTENT(IN)                          :: used_time
     920              :       CHARACTER(LEN=5), INTENT(IN)                       :: wildcard
     921              :       INTEGER(KIND=int_8), INTENT(IN)                    :: max_memory
     922              : 
     923              :       CHARACTER(LEN=5)                                   :: tag
     924              : 
     925          880 :       IF (output_unit > 0) THEN
     926          440 :          tag = "OPT| "
     927          440 :          WRITE (UNIT=output_unit, FMT="(/,T2,A)") tag//REPEAT("*", 74)
     928              :          WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,I25)") &
     929          440 :             tag//"Rotational step number", it
     930              :          WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,A25)") &
     931          440 :             tag//"Optimization method", wildcard
     932              :          WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
     933          440 :             tag//"Local curvature", dimer_env%rot%curvature, &
     934          880 :             tag//"Total rotational force", etot
     935          440 :          IF (PRESENT(ediff)) THEN
     936              :             WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
     937          369 :                tag//"Rotational force change", ediff
     938              :          END IF
     939          440 :          IF (PRESENT(emin)) THEN
     940          369 :             IF (etot < emin) THEN
     941              :                WRITE (UNIT=output_unit, FMT="(T2,A,T77,A4)") &
     942          162 :                   tag//"Decrease in rotational force", " YES"
     943              :             ELSE
     944              :                WRITE (UNIT=output_unit, FMT="(T2,A,T77,A4)") &
     945          207 :                   tag//"Decrease in rotational force", "  NO"
     946              :             END IF
     947              :          END IF
     948              :          WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.3)") &
     949          440 :             tag//"Used time [s]", used_time
     950          440 :          IF (it == 0) THEN
     951           71 :             WRITE (UNIT=output_unit, FMT="(T2,A)") tag//REPEAT("*", 74)
     952           71 :             IF (max_memory /= 0) THEN
     953              :                WRITE (UNIT=output_unit, FMT="(T2,A,T60,1X,I20)") &
     954           71 :                   tag//"Estimated peak process memory [MiB]", &
     955          142 :                   (max_memory + bytes_per_mib - 1)/bytes_per_mib
     956              :             END IF
     957              :          END IF
     958              :       END IF
     959              : 
     960          880 :    END SUBROUTINE write_rot_cycle_infos
     961              : 
     962              : ! **************************************************************************************************
     963              : !> \brief Evaluate the geometry/cell optimization convergence criteria (pure decision, no I/O)
     964              : !> \param ndf ...
     965              : !> \param dr ...
     966              : !> \param g ...
     967              : !> \param gopt_param ...
     968              : !> \param conv ...
     969              : !> \param conv_dx ...
     970              : !> \param conv_rdx ...
     971              : !> \param conv_g ...
     972              : !> \param conv_rg ...
     973              : !> \param conv_p ...
     974              : !> \param max_dr ...
     975              : !> \param rms_dr ...
     976              : !> \param max_grad ...
     977              : !> \param rms_grad ...
     978              : !> \param pres_diff ...
     979              : !> \param pres_tol ...
     980              : !> \param pres_diff_constr ...
     981              : ! **************************************************************************************************
     982         9266 :    SUBROUTINE check_converg(ndf, dr, g, gopt_param, conv, conv_dx, conv_rdx, conv_g, conv_rg, conv_p, &
     983              :                             max_dr, rms_dr, max_grad, rms_grad, pres_diff, pres_tol, pres_diff_constr)
     984              : 
     985              :       INTEGER, INTENT(IN)                                :: ndf
     986              :       REAL(KIND=dp), INTENT(IN)                          :: dr(ndf), g(ndf)
     987              :       TYPE(gopt_param_type), POINTER                     :: gopt_param
     988              :       LOGICAL, INTENT(OUT)                               :: conv, conv_dx, conv_rdx, conv_g, &
     989              :                                                             conv_rg, conv_p
     990              :       REAL(KIND=dp), INTENT(OUT)                         :: max_dr, rms_dr, max_grad, rms_grad
     991              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: pres_diff, pres_tol, pres_diff_constr
     992              : 
     993              :       INTEGER                                            :: indf
     994              :       REAL(KIND=dp)                                      :: dumm, dxcon, gcon, rmsgcon, rmsxcon
     995              : 
     996         9266 :       dxcon = gopt_param%max_dr
     997         9266 :       gcon = gopt_param%max_force
     998         9266 :       rmsgcon = gopt_param%rms_force
     999         9266 :       rmsxcon = gopt_param%rms_dr
    1000              : 
    1001         9266 :       conv = .FALSE.
    1002         9266 :       conv_dx = .TRUE.
    1003         9266 :       conv_rdx = .TRUE.
    1004         9266 :       conv_g = .TRUE.
    1005         9266 :       conv_rg = .TRUE.
    1006         9266 :       conv_p = .TRUE.
    1007              : 
    1008         9266 :       dumm = 0.0_dp
    1009      2735256 :       DO indf = 1, ndf
    1010      2725990 :          IF (indf == 1) max_dr = ABS(dr(indf))
    1011      2725990 :          dumm = dumm + dr(indf)**2
    1012      2725990 :          IF (ABS(dr(indf)) > dxcon) conv_dx = .FALSE.
    1013      2735256 :          IF (ABS(dr(indf)) > max_dr) max_dr = ABS(dr(indf))
    1014              :       END DO
    1015              :       ! SQRT(dumm/ndf) > rmsxcon
    1016         9266 :       IF (dumm > (rmsxcon*rmsxcon*ndf)) conv_rdx = .FALSE.
    1017         9266 :       rms_dr = SQRT(dumm/ndf)
    1018              : 
    1019         9266 :       dumm = 0.0_dp
    1020      2735256 :       DO indf = 1, ndf
    1021      2725990 :          IF (indf == 1) max_grad = ABS(g(indf))
    1022      2725990 :          dumm = dumm + g(indf)**2
    1023      2725990 :          IF (ABS(g(indf)) > gcon) conv_g = .FALSE.
    1024      2735256 :          IF (ABS(g(indf)) > max_grad) max_grad = ABS(g(indf))
    1025              :       END DO
    1026              :       ! SQRT(dumm/ndf) > rmsgcon
    1027         9266 :       IF (dumm > (rmsgcon*rmsgcon*ndf)) conv_rg = .FALSE.
    1028         9266 :       rms_grad = SQRT(dumm/ndf)
    1029              : 
    1030         9266 :       IF (PRESENT(pres_diff_constr) .AND. PRESENT(pres_tol)) THEN
    1031         3290 :          conv_p = ABS(pres_diff_constr) < ABS(pres_tol)
    1032         5976 :       ELSE IF (PRESENT(pres_diff) .AND. PRESENT(pres_tol)) THEN
    1033         1354 :          conv_p = ABS(pres_diff) < ABS(pres_tol)
    1034              :       END IF
    1035              : 
    1036         9266 :       IF (conv_dx .AND. conv_rdx .AND. conv_g .AND. conv_rg .AND. conv_p) conv = .TRUE.
    1037              : 
    1038         9266 :    END SUBROUTINE check_converg
    1039              : 
    1040              : ! **************************************************************************************************
    1041              : !> \brief Print one YES/NO convergence flag line
    1042              : !> \param output_unit ...
    1043              : !> \param tag ...
    1044              : !> \param label ...
    1045              : !> \param flag ...
    1046              : ! **************************************************************************************************
    1047        21859 :    SUBROUTINE write_conv_flag(output_unit, tag, label, flag)
    1048              :       INTEGER, INTENT(IN)                                :: output_unit
    1049              :       CHARACTER(LEN=*), INTENT(IN)                       :: tag, label
    1050              :       LOGICAL, INTENT(IN)                                :: flag
    1051              : 
    1052        21859 :       IF (flag) THEN
    1053         3957 :          WRITE (UNIT=output_unit, FMT="(T2,A,T77,A4)") tag//label, " YES"
    1054              :       ELSE
    1055        17902 :          WRITE (UNIT=output_unit, FMT="(T2,A,T77,A4)") tag//label, "  NO"
    1056              :       END IF
    1057        21859 :    END SUBROUTINE write_conv_flag
    1058              : 
    1059              : ! **************************************************************************************************
    1060              : !> \brief Print the per-cycle convergence report (body moved verbatim from check_converg)
    1061              : !> \param output_unit ...
    1062              : !> \param gopt_param ...
    1063              : !> \param conv ...
    1064              : !> \param conv_dx ...
    1065              : !> \param conv_rdx ...
    1066              : !> \param conv_g ...
    1067              : !> \param conv_rg ...
    1068              : !> \param conv_p ...
    1069              : !> \param max_dr ...
    1070              : !> \param rms_dr ...
    1071              : !> \param max_grad ...
    1072              : !> \param rms_grad ...
    1073              : !> \param max_memory ...
    1074              : !> \param stress_unit ...
    1075              : !> \param pres_diff ...
    1076              : !> \param pres_tol ...
    1077              : !> \param pres_diff_constr ...
    1078              : ! **************************************************************************************************
    1079         9266 :    SUBROUTINE write_convergence_infos(output_unit, gopt_param, conv, conv_dx, conv_rdx, conv_g, &
    1080              :                                       conv_rg, conv_p, max_dr, rms_dr, max_grad, rms_grad, &
    1081              :                                       max_memory, stress_unit, pres_diff, pres_tol, pres_diff_constr)
    1082              : 
    1083              :       INTEGER, INTENT(IN)                                :: output_unit
    1084              :       TYPE(gopt_param_type), POINTER                     :: gopt_param
    1085              :       LOGICAL, INTENT(IN)                                :: conv, conv_dx, conv_rdx, conv_g, &
    1086              :                                                             conv_rg, conv_p
    1087              :       REAL(KIND=dp), INTENT(IN)                          :: max_dr, rms_dr, max_grad, rms_grad
    1088              :       INTEGER(KIND=int_8), INTENT(IN)                    :: max_memory
    1089              :       CHARACTER(LEN=default_string_length), INTENT(IN)   :: stress_unit
    1090              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: pres_diff, pres_tol, pres_diff_constr
    1091              : 
    1092              :       CHARACTER(LEN=5)                                   :: tag
    1093              :       REAL(KIND=dp)                                      :: dxcon, gcon, rmsgcon, rmsxcon
    1094              : 
    1095         9266 :       dxcon = gopt_param%max_dr
    1096         9266 :       gcon = gopt_param%max_force
    1097         9266 :       rmsgcon = gopt_param%rms_force
    1098         9266 :       rmsxcon = gopt_param%rms_dr
    1099              : 
    1100         9266 :       IF (output_unit > 0) THEN
    1101              : 
    1102         4792 :          tag = "OPT| "
    1103              : 
    1104         4792 :          WRITE (UNIT=output_unit, FMT="(T2,A)") TRIM(tag)
    1105              :          WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
    1106         4792 :             tag//"Maximum step size", max_dr, &
    1107         9584 :             tag//"Convergence limit for maximum step size", dxcon
    1108              :          CALL write_conv_flag(output_unit, tag, "Maximum step size is converged", &
    1109         4792 :                               conv_dx)
    1110              : 
    1111         4792 :          WRITE (UNIT=output_unit, FMT="(T2,A)") TRIM(tag)
    1112              :          WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
    1113         4792 :             tag//"RMS step size", rms_dr, &
    1114         9584 :             tag//"Convergence limit for RMS step size", rmsxcon
    1115              :          CALL write_conv_flag(output_unit, tag, "RMS step size is converged", &
    1116         4792 :                               conv_rdx)
    1117              : 
    1118         4792 :          WRITE (UNIT=output_unit, FMT="(T2,A)") TRIM(tag)
    1119              :          WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
    1120         4792 :             tag//"Maximum gradient", max_grad, &
    1121         9584 :             tag//"Convergence limit for maximum gradient", gcon
    1122              :          CALL write_conv_flag(output_unit, tag, "Maximum gradient is converged", &
    1123         4792 :                               conv_g)
    1124              : 
    1125         4792 :          WRITE (UNIT=output_unit, FMT="(T2,A)") TRIM(tag)
    1126              :          WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
    1127         4792 :             tag//"RMS gradient", rms_grad, &
    1128         9584 :             tag//"Convergence limit for RMS gradient", rmsgcon
    1129              :          CALL write_conv_flag(output_unit, tag, "RMS gradient is converged", &
    1130         4792 :                               conv_rg)
    1131              : 
    1132         4792 :          IF (PRESENT(pres_diff) .AND. PRESENT(pres_tol)) THEN
    1133         2322 :             WRITE (UNIT=output_unit, FMT="(T2,A)") TRIM(tag)
    1134         2322 :             IF (PRESENT(pres_diff_constr)) THEN
    1135              :                WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
    1136              :                   tag//"Pressure deviation without constraint ["// &
    1137         1645 :                   TRIM(ADJUSTL(stress_unit))//"]", &
    1138         3290 :                   cp_unit_from_cp2k(pres_diff, TRIM(stress_unit))
    1139              :                WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
    1140              :                   tag//"Pressure deviation with constraint ["// &
    1141         1645 :                   TRIM(ADJUSTL(stress_unit))//"]", &
    1142         3290 :                   cp_unit_from_cp2k(pres_diff_constr, TRIM(stress_unit))
    1143              :             ELSE
    1144              :                WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
    1145          677 :                   tag//"Pressure deviation ["//TRIM(ADJUSTL(stress_unit))//"]", &
    1146         1354 :                   cp_unit_from_cp2k(pres_diff, TRIM(stress_unit))
    1147              :             END IF
    1148              :             WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
    1149         2322 :                tag//"Pressure tolerance ["//TRIM(ADJUSTL(stress_unit))//"]", &
    1150         4644 :                cp_unit_from_cp2k(pres_tol, TRIM(stress_unit))
    1151         2322 :             CALL write_conv_flag(output_unit, tag, "Pressure is converged", conv_p)
    1152              :          END IF
    1153              : 
    1154         4792 :          WRITE (UNIT=output_unit, FMT="(T2,A)") tag//REPEAT("*", 74)
    1155              : 
    1156         4792 :          IF (max_memory /= 0) THEN
    1157              :             WRITE (UNIT=output_unit, FMT="(T2,A,T60,1X,I20)") &
    1158         4792 :                tag//"Estimated peak process memory after this step [MiB]", &
    1159         9584 :                (max_memory + bytes_per_mib - 1)/bytes_per_mib
    1160              :          END IF
    1161              : 
    1162              :       END IF
    1163              : 
    1164         9266 :       IF ((conv) .AND. (output_unit > 0)) THEN
    1165          228 :          WRITE (UNIT=output_unit, FMT="(/,T2,A)") REPEAT("*", 79)
    1166              :          WRITE (UNIT=output_unit, FMT="(T2,A,T25,A,T78,A)") &
    1167          228 :             "***", "GEOMETRY OPTIMIZATION COMPLETED", "***"
    1168          228 :          WRITE (UNIT=output_unit, FMT="(T2,A)") REPEAT("*", 79)
    1169              :       END IF
    1170              : 
    1171         9266 :    END SUBROUTINE write_convergence_infos
    1172              : 
    1173              : ! **************************************************************************************************
    1174              : !> \brief Check convergence and print the per-cycle report (gopt_f_io call sites)
    1175              : !> \param output_unit ...
    1176              : !> \param gopt_param ...
    1177              : !> \param ndf ...
    1178              : !> \param dr ...
    1179              : !> \param g ...
    1180              : !> \param conv ...
    1181              : !> \param max_memory ...
    1182              : !> \param stress_unit ...
    1183              : !> \param pres_diff ...
    1184              : !> \param pres_tol ...
    1185              : !> \param pres_diff_constr ...
    1186              : !> \param curvature if present, require a negative dimer curvature
    1187              : ! **************************************************************************************************
    1188         9266 :    SUBROUTINE check_and_report_convergence(output_unit, gopt_param, ndf, dr, g, conv, &
    1189              :                                            max_memory, stress_unit, pres_diff, pres_tol, pres_diff_constr, curvature)
    1190              : 
    1191              :       INTEGER, INTENT(IN)                                :: output_unit
    1192              :       TYPE(gopt_param_type), POINTER                     :: gopt_param
    1193              :       INTEGER, INTENT(IN)                                :: ndf
    1194              :       REAL(KIND=dp), INTENT(IN)                          :: dr(ndf), g(ndf)
    1195              :       LOGICAL, INTENT(INOUT)                             :: conv
    1196              :       INTEGER(KIND=int_8), INTENT(IN)                    :: max_memory
    1197              :       CHARACTER(LEN=default_string_length), INTENT(IN)   :: stress_unit
    1198              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: pres_diff, pres_tol, pres_diff_constr, &
    1199              :                                                             curvature
    1200              : 
    1201              :       LOGICAL                                            :: conv_dx, conv_g, conv_p, conv_rdx, &
    1202              :                                                             conv_rg
    1203              :       REAL(KIND=dp)                                      :: max_dr, max_grad, rms_dr, rms_grad
    1204              : 
    1205              :       CALL check_converg(ndf, dr, g, gopt_param, conv, conv_dx, conv_rdx, conv_g, conv_rg, &
    1206              :                          conv_p, max_dr, rms_dr, max_grad, rms_grad, pres_diff, pres_tol, &
    1207         9266 :                          pres_diff_constr)
    1208         9266 :       IF (PRESENT(curvature)) THEN
    1209            8 :          conv = conv .AND. curvature < 0.0_dp
    1210            8 :          IF (output_unit > 0) WRITE (output_unit, '(T2,A,1X,L1)') "SS-DIMER| Negative curvature:", curvature < 0.0_dp
    1211              :       END IF
    1212              :       CALL write_convergence_infos(output_unit, gopt_param, conv, conv_dx, conv_rdx, conv_g, &
    1213              :                                    conv_rg, conv_p, max_dr, rms_dr, max_grad, rms_grad, &
    1214         9266 :                                    max_memory, stress_unit, pres_diff, pres_tol, pres_diff_constr)
    1215         9266 :    END SUBROUTINE check_and_report_convergence
    1216              : 
    1217              : ! **************************************************************************************************
    1218              : !> \brief ...
    1219              : !> \param dimer_env ...
    1220              : !> \param output_unit ...
    1221              : !> \param conv ...
    1222              : !> \date  01.2008
    1223              : !> \author Luca Bellucci and Teodoro Laino - created [tlaino]
    1224              : ! **************************************************************************************************
    1225          738 :    SUBROUTINE check_rot_conv(dimer_env, output_unit, conv)
    1226              : 
    1227              :       TYPE(dimer_env_type), POINTER                      :: dimer_env
    1228              :       INTEGER, INTENT(IN)                                :: output_unit
    1229              :       LOGICAL, INTENT(OUT)                               :: conv
    1230              : 
    1231              :       CHARACTER(LEN=5)                                   :: tag
    1232              : 
    1233          738 :       conv = (ABS(dimer_env%rot%angle2) < dimer_env%rot%angle_tol)
    1234              : 
    1235          738 :       IF (output_unit > 0) THEN
    1236          369 :          tag = "OPT| "
    1237          369 :          WRITE (UNIT=output_unit, FMT="(T2,A)") TRIM(tag)
    1238              :          WRITE (UNIT=output_unit, FMT="(T2,A,T55,1X,F25.10)") &
    1239          369 :             tag//"Predicted angle step size", dimer_env%rot%angle1, &
    1240          369 :             tag//"Effective angle step size", dimer_env%rot%angle2, &
    1241          738 :             tag//"Convergence limit for angle step size", dimer_env%rot%angle_tol
    1242          369 :          CALL write_conv_flag(output_unit, tag, "Angle step size is converged", conv)
    1243          369 :          WRITE (UNIT=output_unit, FMT="(T2,A)") tag//REPEAT("*", 74)
    1244              :       END IF
    1245              : 
    1246          738 :       IF ((conv) .AND. (output_unit > 0)) THEN
    1247           64 :          WRITE (UNIT=output_unit, FMT="(/,T2,A)") REPEAT("*", 79)
    1248              :          WRITE (UNIT=output_unit, FMT="(T2,A,T25,A,T78,A)") &
    1249           64 :             "***", "ROTATION OPTIMIZATION COMPLETED", "***"
    1250           64 :          WRITE (UNIT=output_unit, FMT="(T2,A)") REPEAT("*", 79)
    1251              :       END IF
    1252              : 
    1253          738 :    END SUBROUTINE check_rot_conv
    1254              : 
    1255              : ! **************************************************************************************************
    1256              : !> \brief ...
    1257              : !> \param output_unit ...
    1258              : !> \param conv ...
    1259              : !> \param it ...
    1260              : !> \param gopt_env ...
    1261              : !> \param x0 ...
    1262              : !> \param master ...
    1263              : !> \param para_env ...
    1264              : !> \param force_env ...
    1265              : !> \param motion_section ...
    1266              : !> \param root_section ...
    1267              : !> \date  11.2007
    1268              : !> \author Teodoro Laino [tlaino] - University of Zurich
    1269              : ! **************************************************************************************************
    1270         1137 :    RECURSIVE SUBROUTINE write_final_info(output_unit, conv, it, gopt_env, x0, master, para_env, force_env, &
    1271              :                                          motion_section, root_section)
    1272              :       INTEGER, INTENT(IN)                                :: output_unit
    1273              :       LOGICAL, INTENT(IN)                                :: conv
    1274              :       INTEGER, INTENT(INOUT)                             :: it
    1275              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
    1276              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: x0
    1277              :       INTEGER, INTENT(IN)                                :: master
    1278              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1279              :       TYPE(force_env_type), POINTER                      :: force_env
    1280              :       TYPE(section_vals_type), POINTER                   :: motion_section, root_section
    1281              : 
    1282              :       CHARACTER(LEN=default_string_length)               :: constraint_label
    1283              :       LOGICAL                                            :: keep_angles, keep_symmetry, keep_volume
    1284              :       REAL(KIND=dp)                                      :: etot
    1285              :       TYPE(cell_type), POINTER                           :: cell
    1286              :       TYPE(cp_subsys_type), POINTER                      :: subsys
    1287              :       TYPE(particle_list_type), POINTER                  :: particles
    1288         1137 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1289              : 
    1290         1137 :       CALL force_env_get(force_env, cell=cell, subsys=subsys)
    1291         1137 :       CALL cp_subsys_get(subsys=subsys, particles=particles)
    1292         1137 :       particle_set => particles%els
    1293              : 
    1294              :       ! Passing gopt_f_type pointer gopt_env to particle_methods where
    1295              :       ! write_final_structure is defined causes a circular dependency, so it
    1296              :       ! is necessary to get some flags by preprocessing...
    1297         1137 :       keep_angles = .TRUE.
    1298         1137 :       keep_symmetry = .TRUE.
    1299         1137 :       keep_volume = .TRUE.
    1300         1137 :       constraint_label = "NONE"
    1301         1137 :       IF (gopt_env%type_id == default_cell_method_id) THEN
    1302          246 :          keep_angles = gopt_env%cell_env%keep_angles
    1303          246 :          keep_symmetry = gopt_env%cell_env%keep_symmetry
    1304          246 :          keep_volume = gopt_env%cell_env%keep_volume
    1305          246 :          constraint_label = cell_opt_constraint_label(gopt_env%cell_env%constraint_id)
    1306              :       END IF
    1307              :       CALL write_final_structure(particle_set, cell, motion_section, conv, &
    1308              :                                  keep_angles, keep_symmetry, keep_volume, &
    1309         1137 :                                  gopt_env%label, constraint_label)
    1310              : 
    1311         1137 :       IF (conv) THEN
    1312          392 :          it = it + 1
    1313          392 :          CALL write_structure_data(particle_set, cell, motion_section)
    1314          392 :          CALL write_restart(force_env=force_env, root_section=root_section)
    1315              : 
    1316          392 :          IF (output_unit > 0) THEN
    1317          219 :             WRITE (UNIT=output_unit, FMT="(/,T20,' Reevaluating energy at the minimum')")
    1318              :          END IF
    1319              : 
    1320              :          CALL cp_eval_at(gopt_env, x0, f=etot, master=master, &
    1321          392 :                          para_env=para_env)
    1322          392 :          IF (gopt_env%solid_state_dimer) etot = gopt_env%dimer_env%enthalpy
    1323          392 :          CALL write_geo_traj(force_env, root_section, it, etot)
    1324          392 :          CALL write_simulation_cell(cell, motion_section, it, 0.0_dp)
    1325              :       END IF
    1326              : 
    1327         1137 :    END SUBROUTINE write_final_info
    1328              : 
    1329              : ! **************************************************************************************************
    1330              : !> \brief  Specific driver for dumping trajectory during a GEO_OPT
    1331              : !> \param force_env ...
    1332              : !> \param root_section ...
    1333              : !> \param it ...
    1334              : !> \param etot ...
    1335              : !> \date   11.2007
    1336              : !> \par    History
    1337              : !>         09.2010: Output of core and shell positions and forces (MK)
    1338              : !> \author Teodoro Laino [tlaino] - University of Zurich
    1339              : ! **************************************************************************************************
    1340        19016 :    SUBROUTINE write_geo_traj(force_env, root_section, it, etot)
    1341              : 
    1342              :       TYPE(force_env_type), POINTER                      :: force_env
    1343              :       TYPE(section_vals_type), POINTER                   :: root_section
    1344              :       INTEGER, INTENT(IN)                                :: it
    1345              :       REAL(KIND=dp), INTENT(IN)                          :: etot
    1346              : 
    1347              :       LOGICAL                                            :: shell_adiabatic, shell_present
    1348              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
    1349         9508 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1350              :       TYPE(cp_subsys_type), POINTER                      :: subsys
    1351              :       TYPE(particle_list_type), POINTER                  :: core_particles, shell_particles
    1352              : 
    1353         9508 :       NULLIFY (atomic_kinds)
    1354         9508 :       NULLIFY (atomic_kind_set)
    1355         9508 :       NULLIFY (core_particles)
    1356         9508 :       NULLIFY (shell_particles)
    1357         9508 :       NULLIFY (subsys)
    1358              : 
    1359         9508 :       CALL write_trajectory(force_env, root_section, it, 0.0_dp, 0.0_dp, etot)
    1360              :       ! Print Force
    1361         9508 :       CALL write_trajectory(force_env, root_section, it, 0.0_dp, 0.0_dp, etot, "FORCES", middle_name="frc")
    1362         9508 :       CALL force_env_get(force_env, subsys=subsys)
    1363         9508 :       CALL cp_subsys_get(subsys, atomic_kinds=atomic_kinds)
    1364         9508 :       atomic_kind_set => atomic_kinds%els
    1365              :       CALL get_atomic_kind_set(atomic_kind_set, &
    1366              :                                shell_present=shell_present, &
    1367         9508 :                                shell_adiabatic=shell_adiabatic)
    1368         9508 :       IF (shell_present) THEN
    1369              :          CALL cp_subsys_get(subsys, &
    1370              :                             core_particles=core_particles, &
    1371         3916 :                             shell_particles=shell_particles)
    1372              :          CALL write_trajectory(force_env, root_section, it=it, time=0.0_dp, dtime=0.0_dp, &
    1373              :                                etot=etot, pk_name="SHELL_TRAJECTORY", middle_name="shpos", &
    1374         3916 :                                particles=shell_particles)
    1375         3916 :          IF (shell_adiabatic) THEN
    1376              :             CALL write_trajectory(force_env, root_section, it=it, time=0.0_dp, dtime=0.0_dp, &
    1377              :                                   etot=etot, pk_name="SHELL_FORCES", middle_name="shfrc", &
    1378         3916 :                                   particles=shell_particles)
    1379              :             CALL write_trajectory(force_env, root_section, it=it, time=0.0_dp, dtime=0.0_dp, &
    1380              :                                   etot=etot, pk_name="CORE_TRAJECTORY", middle_name="copos", &
    1381         3916 :                                   particles=core_particles)
    1382              :             CALL write_trajectory(force_env, root_section, it=it, time=0.0_dp, dtime=0.0_dp, &
    1383              :                                   etot=etot, pk_name="CORE_FORCES", middle_name="cofrc", &
    1384         3916 :                                   particles=core_particles)
    1385              :          END IF
    1386              :       END IF
    1387              : 
    1388         9508 :    END SUBROUTINE write_geo_traj
    1389              : 
    1390              : ! **************************************************************************************************
    1391              : !> \brief ...
    1392              : !> \param gopt_env ...
    1393              : !> \param output_unit ...
    1394              : !> \param label ...
    1395              : !> \date  01.2008
    1396              : !> \author Teodoro Laino [tlaino] - University of Zurich
    1397              : ! **************************************************************************************************
    1398         1299 :    SUBROUTINE print_geo_opt_header(gopt_env, output_unit, label)
    1399              : 
    1400              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
    1401              :       INTEGER, INTENT(IN)                                :: output_unit
    1402              :       CHARACTER(LEN=*), INTENT(IN)                       :: label
    1403              : 
    1404              :       CHARACTER(LEN=default_string_length)               :: my_format, my_label
    1405              :       INTEGER                                            :: ix
    1406              : 
    1407         1299 :       IF (output_unit > 0) THEN
    1408          673 :          WRITE (UNIT=output_unit, FMT="(/,T2,A)") REPEAT("*", 79)
    1409          673 :          IF (gopt_env%dimer_rotation) THEN
    1410           71 :             my_label = "OPTIMIZING DIMER ROTATION"
    1411              :          ELSE
    1412          602 :             my_label = "STARTING "//gopt_env%tag(1:8)//" OPTIMIZATION"
    1413              :          END IF
    1414              : 
    1415          673 :          ix = (80 - 7 - LEN_TRIM(my_label))/2
    1416          673 :          ix = ix + 5
    1417          673 :          my_format = "(T2,A,T"//cp_to_string(ix)//",A,T78,A)"
    1418          673 :          WRITE (UNIT=output_unit, FMT=TRIM(my_format)) "***", TRIM(my_label), "***"
    1419              : 
    1420          673 :          ix = (80 - 7 - LEN_TRIM(label))/2
    1421          673 :          ix = ix + 5
    1422          673 :          my_format = "(T2,A,T"//cp_to_string(ix)//",A,T78,A)"
    1423          673 :          WRITE (UNIT=output_unit, FMT=TRIM(my_format)) "***", TRIM(label), "***"
    1424              : 
    1425          673 :          WRITE (UNIT=output_unit, FMT="(T2,A)") REPEAT("*", 79)
    1426          673 :          CALL m_flush(output_unit)
    1427              :       END IF
    1428         1299 :    END SUBROUTINE print_geo_opt_header
    1429              : 
    1430              : ! **************************************************************************************************
    1431              : !> \brief ...
    1432              : !> \param gopt_env ...
    1433              : !> \param output_unit ...
    1434              : !> \date  01.2008
    1435              : !> \author Teodoro Laino [tlaino] - University of Zurich
    1436              : ! **************************************************************************************************
    1437          747 :    SUBROUTINE print_geo_opt_nc(gopt_env, output_unit)
    1438              : 
    1439              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
    1440              :       INTEGER, INTENT(IN)                                :: output_unit
    1441              : 
    1442          747 :       IF (output_unit > 0) THEN
    1443              :          WRITE (UNIT=output_unit, FMT="(/,T2,A)") &
    1444          374 :             "*** MAXIMUM NUMBER OF OPTIMIZATION STEPS REACHED ***"
    1445          374 :          IF (.NOT. gopt_env%dimer_rotation) THEN
    1446              :             WRITE (UNIT=output_unit, FMT="(T2,A)") &
    1447          367 :                "***        EXITING GEOMETRY OPTIMIZATION         ***"
    1448              :          ELSE
    1449              :             WRITE (UNIT=output_unit, FMT="(T2,A)") &
    1450            7 :                "***        EXITING ROTATION OPTIMIZATION         ***"
    1451              :          END IF
    1452          374 :          CALL m_flush(output_unit)
    1453              :       END IF
    1454              : 
    1455          747 :    END SUBROUTINE print_geo_opt_nc
    1456              : 
    1457              : ! **************************************************************************************************
    1458              : !> \brief   Prints information during GEO_OPT common to all optimizers
    1459              : !> \param force_env ...
    1460              : !> \param root_section ...
    1461              : !> \param motion_section ...
    1462              : !> \param its ...
    1463              : !> \param opt_energy ...
    1464              : !> \date    02.2008
    1465              : !> \author  Teodoro Laino [tlaino] - University of Zurich
    1466              : !> \version 1.0
    1467              : ! **************************************************************************************************
    1468         9116 :    SUBROUTINE geo_opt_io(force_env, root_section, motion_section, its, opt_energy)
    1469              : 
    1470              :       TYPE(force_env_type), POINTER                      :: force_env
    1471              :       TYPE(section_vals_type), POINTER                   :: root_section, motion_section
    1472              :       INTEGER, INTENT(IN)                                :: its
    1473              :       REAL(KIND=dp), INTENT(IN)                          :: opt_energy
    1474              : 
    1475              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
    1476         9116 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1477              :       TYPE(cell_type), POINTER                           :: cell
    1478              :       TYPE(cp_subsys_type), POINTER                      :: subsys
    1479              :       TYPE(distribution_1d_type), POINTER                :: local_particles
    1480              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1481              :       TYPE(particle_list_type), POINTER                  :: particles
    1482         9116 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1483              :       TYPE(virial_type), POINTER                         :: virial
    1484              : 
    1485         9116 :       NULLIFY (para_env, atomic_kind_set, subsys, particle_set, &
    1486         9116 :                local_particles, atomic_kinds, particles)
    1487              : 
    1488              :       ! Write Restart File
    1489         9116 :       CALL write_restart(force_env=force_env, root_section=root_section)
    1490              : 
    1491              :       ! Write Trajectory
    1492         9116 :       CALL write_geo_traj(force_env, root_section, its, opt_energy)
    1493              : 
    1494              :       ! Write the stress Tensor
    1495              :       CALL force_env_get(force_env, cell=cell, para_env=para_env, &
    1496         9116 :                          subsys=subsys)
    1497              :       CALL cp_subsys_get(subsys=subsys, atomic_kinds=atomic_kinds, local_particles=local_particles, &
    1498         9116 :                          particles=particles, virial=virial)
    1499         9116 :       atomic_kind_set => atomic_kinds%els
    1500         9116 :       particle_set => particles%els
    1501              :       CALL virial_evaluate(atomic_kind_set, particle_set, local_particles, &
    1502         9116 :                            virial, para_env)
    1503         9116 :       CALL write_stress_tensor_to_file(virial, cell, motion_section, its, 0.0_dp)
    1504              : 
    1505              :       ! Write the cell
    1506         9116 :       CALL write_simulation_cell(cell, motion_section, its, 0.0_dp)
    1507              : 
    1508         9116 :    END SUBROUTINE geo_opt_io
    1509              : 
    1510              : ! **************************************************************************************************
    1511              : !> \brief   Apply coordinate transformations after cell (shape) change
    1512              : !> \param gopt_env ...
    1513              : !> \param cell ...
    1514              : !> \param x ...
    1515              : !> \param update_forces ...
    1516              : !> \date    05.11.2012 (revised version of unbiase_coordinates moved here, MK)
    1517              : !> \author  Matthias Krack
    1518              : !> \version 1.0
    1519              : ! **************************************************************************************************
    1520        15973 :    SUBROUTINE apply_cell_change(gopt_env, cell, x, update_forces)
    1521              : 
    1522              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
    1523              :       TYPE(cell_type), POINTER                           :: cell
    1524              :       REAL(KIND=dp), DIMENSION(:), POINTER               :: x
    1525              :       LOGICAL, INTENT(IN)                                :: update_forces
    1526              : 
    1527              :       INTEGER                                            :: i, iatom, idg, info, natom, ndof_cell, &
    1528              :                                                             nparticle, shell_index
    1529              :       REAL(KIND=dp)                                      :: fc, fs, mass
    1530              :       REAL(KIND=dp), DIMENSION(3)                        :: s
    1531              :       REAL(KIND=dp), DIMENSION(n_slot)                   :: q_man
    1532              :       TYPE(cell_type), POINTER                           :: cell_ref
    1533              :       TYPE(cp_subsys_type), POINTER                      :: subsys
    1534              :       TYPE(particle_list_type), POINTER                  :: core_particles, particles, &
    1535              :                                                             shell_particles
    1536              : 
    1537        15973 :       NULLIFY (cell_ref)
    1538        15973 :       NULLIFY (core_particles)
    1539        15973 :       NULLIFY (particles)
    1540        15973 :       NULLIFY (shell_particles)
    1541        15973 :       NULLIFY (subsys)
    1542              : 
    1543        15973 :       natom = force_env_get_natom(gopt_env%force_env)
    1544        15973 :       nparticle = force_env_get_nparticle(gopt_env%force_env)
    1545              :       CALL force_env_get(gopt_env%force_env, &
    1546        15973 :                          subsys=subsys)
    1547              :       CALL cp_subsys_get(subsys=subsys, &
    1548              :                          core_particles=core_particles, &
    1549              :                          particles=particles, &
    1550        15973 :                          shell_particles=shell_particles)
    1551              : 
    1552              :       ! Retrieve the reference cell
    1553        15973 :       CALL cell_create(cell_ref)
    1554        15973 :       CALL cell_copy(cell, cell_ref, tag="CELL_OPT_REF")
    1555              : 
    1556              :       ! Load the updated cell information
    1557        15973 :       idg = 3*nparticle
    1558        15973 :       ndof_cell = gopt_env%cell_env%manifold%ndof
    1559        15973 :       CALL init_cell(cell_ref, hmat=gopt_env%h_ref)
    1560        15973 :       CPASSERT((SIZE(x) == idg + ndof_cell))
    1561              : 
    1562        15973 :       IF (update_forces) THEN
    1563              : 
    1564              :          ! Transform particle forces back to reference cell
    1565              :          idg = 1
    1566       364395 :          DO iatom = 1, natom
    1567       358410 :             CALL real_to_scaled(s, x(idg:idg + 2), cell)
    1568       358410 :             CALL scaled_to_real(x(idg:idg + 2), s, cell_ref)
    1569       364395 :             idg = idg + 3
    1570              :          END DO
    1571              : 
    1572              :       ELSE
    1573              : 
    1574              :          CALL cell_manifold_chart_eval(gopt_env%cell_env%manifold, &
    1575         9988 :                                        x(idg + 1:idg + ndof_cell), q_man, info)
    1576         9988 :          IF (info /= info_ok) THEN
    1577              :             CALL cp_abort(__LOCATION__, &
    1578              :                           "CELL_OPT: trial step violates cell constraints ("// &
    1579            0 :                           TRIM(cell_manifold_status_label(info))//").")
    1580              :          END IF
    1581         9988 :          CALL cell_manifold_unpack_hmat(q_man, gopt_env%cell_env%opt_cell%hmat)
    1582         9988 :          CALL init_cell(gopt_env%cell_env%opt_cell)
    1583         9988 :          IF (gopt_env%spgr%keep_space_group) THEN
    1584          656 :             CALL spgr_project_cell_metric(gopt_env%spgr, gopt_env%cell_env%opt_cell)
    1585              :             ! Sync reduced cell coordinates x with the symmetrized cell.
    1586          656 :             q_man = cell_manifold_pack_hmat(gopt_env%cell_env%opt_cell%hmat)
    1587              :             CALL cell_manifold_chart_inv(gopt_env%cell_env%manifold, q_man, &
    1588          656 :                                          x(idg + 1:idg + ndof_cell))
    1589              :          END IF
    1590              : 
    1591              :          ! Reconstruct the physical cell in the fixed orientation of the input cell.
    1592              :          cell%hmat = MATMUL(gopt_env%cell_env%opt_to_input, &
    1593       779064 :                             gopt_env%cell_env%opt_cell%hmat)
    1594         9988 :          CALL init_cell(cell)
    1595         9988 :          CALL cp_subsys_set(subsys, cell=cell)
    1596              : 
    1597              :          ! Retrieve particle coordinates for the current cell
    1598         9988 :          idg = 1
    1599       683308 :          DO iatom = 1, natom
    1600       673320 :             CALL real_to_scaled(s, x(idg:idg + 2), cell_ref)
    1601       673320 :             shell_index = particles%els(iatom)%shell_index
    1602       673320 :             IF (shell_index == 0) THEN
    1603       310092 :                CALL scaled_to_real(particles%els(iatom)%r, s, cell)
    1604              :             ELSE
    1605       363228 :                CALL scaled_to_real(core_particles%els(shell_index)%r, s, cell)
    1606       363228 :                i = 3*(natom + shell_index - 1) + 1
    1607       363228 :                CALL real_to_scaled(s, x(i:i + 2), cell_ref)
    1608       363228 :                CALL scaled_to_real(shell_particles%els(shell_index)%r, s, cell)
    1609              :                ! Update atomic position due to core and shell motion
    1610       363228 :                mass = particles%els(iatom)%atomic_kind%mass
    1611       363228 :                fc = core_particles%els(shell_index)%atomic_kind%shell%mass_core/mass
    1612       363228 :                fs = shell_particles%els(shell_index)%atomic_kind%shell%mass_shell/mass
    1613              :                particles%els(iatom)%r(1:3) = fc*core_particles%els(shell_index)%r(1:3) + &
    1614      2905824 :                                              fs*shell_particles%els(shell_index)%r(1:3)
    1615              :             END IF
    1616       683308 :             idg = idg + 3
    1617              :          END DO
    1618              :       END IF
    1619              : 
    1620        15973 :       CALL cell_release(cell_ref)
    1621              : 
    1622        15973 :    END SUBROUTINE apply_cell_change
    1623              : 
    1624              : ! **************************************************************************************************
    1625              : !> \brief Identify and print the space group of the current structure (SHOW_SPACE_GROUP).
    1626              : !> \param gopt_env geometry optimization environment
    1627              : !> \param geo_section GEO_OPT or CELL_OPT input section
    1628              : !> \param output_unit print-key output unit
    1629              : ! **************************************************************************************************
    1630            6 :    SUBROUTINE gopt_f_space_group_show(gopt_env, geo_section, output_unit)
    1631              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
    1632              :       TYPE(section_vals_type), POINTER                   :: geo_section
    1633              :       INTEGER, INTENT(IN)                                :: output_unit
    1634              : 
    1635              :       TYPE(cp_subsys_type), POINTER                      :: subsys
    1636              : 
    1637            6 :       NULLIFY (subsys)
    1638              : 
    1639            6 :       IF (.NOT. gopt_env%spgr%show_space_group) RETURN
    1640            6 :       CALL force_env_get(gopt_env%force_env, subsys=subsys)
    1641            6 :       CALL identify_space_group(subsys, geo_section, gopt_env, output_unit)
    1642            6 :       CALL print_spgr(gopt_env%spgr)
    1643              :    END SUBROUTINE gopt_f_space_group_show
    1644              : 
    1645              : ! **************************************************************************************************
    1646              : !> \brief Update the reference cell in gopt_env and spgr%cell_ref.
    1647              : !> \param gopt_env geometry optimization environment
    1648              : !> \param h_ref new 3x3 reference cell matrix
    1649              : ! **************************************************************************************************
    1650            4 :    SUBROUTINE gopt_f_set_h_ref(gopt_env, h_ref)
    1651              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
    1652              :       REAL(KIND=dp), DIMENSION(3, 3), INTENT(IN)         :: h_ref
    1653              : 
    1654           52 :       gopt_env%h_ref = h_ref
    1655              :       ! space_groups maps coordinates and forces through spgr%cell_ref,
    1656              :       ! so BFGS restarts must update spgr%cell_ref alongside h_ref.
    1657            4 :       IF (ASSOCIATED(gopt_env%spgr%cell_ref)) THEN
    1658            0 :          CALL init_cell(gopt_env%spgr%cell_ref, hmat=h_ref)
    1659              :       END IF
    1660            4 :    END SUBROUTINE gopt_f_set_h_ref
    1661              : 
    1662              : ! **************************************************************************************************
    1663              : !> \brief Re-anchor the cell constraint manifold at reference_cell and remap x0.
    1664              : !> \param gopt_env geometry optimization environment
    1665              : !> \param reference_cell initial cell matrix from the BFGS restart header
    1666              : !> \param x0 optimization coordinate vector. This routine updates the cell block of x0 in place.
    1667              : ! **************************************************************************************************
    1668            4 :    SUBROUTINE gopt_f_restore_cell_manifold(gopt_env, reference_cell, x0)
    1669              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
    1670              :       REAL(KIND=dp), DIMENSION(3, 3), INTENT(IN)         :: reference_cell
    1671              :       REAL(KIND=dp), DIMENSION(:), INTENT(INOUT)         :: x0
    1672              : 
    1673              :       INTEGER                                            :: info, n_coord, ndof_cur
    1674              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: hmat_cur
    1675              :       REAL(KIND=dp), DIMENSION(n_slot)                   :: q_check, q_cur
    1676              : 
    1677            4 :       ndof_cur = gopt_env%cell_env%manifold%ndof
    1678            4 :       n_coord = 3*force_env_get_nparticle(gopt_env%force_env)
    1679            4 :       CPASSERT(SIZE(x0) == n_coord + ndof_cur)
    1680              : 
    1681           52 :       hmat_cur = gopt_env%cell_env%opt_cell%hmat
    1682            4 :       q_cur = cell_manifold_pack_hmat(hmat_cur)
    1683              : 
    1684           52 :       gopt_env%cell_env%opt_cell%hmat = reference_cell
    1685            4 :       CALL canonicalize_cell_matrix(gopt_env%cell_env%opt_cell)
    1686            4 :       CALL build_cell_opt_manifold(gopt_env, emit_warnings=.FALSE.)
    1687            4 :       CALL init_cell(gopt_env%cell_env%opt_cell, hmat=hmat_cur)
    1688              : 
    1689            4 :       IF (gopt_env%cell_env%manifold%ndof /= ndof_cur) THEN
    1690              :          CALL cp_abort(__LOCATION__, &
    1691              :                        "CELL_OPT: The cell degrees of freedom on BFGS restart differ "// &
    1692            0 :                        "from the reference cell. Set RESTART_HESSIAN F.")
    1693              :       END IF
    1694              : 
    1695              :       CALL cell_manifold_chart_inv(gopt_env%cell_env%manifold, q_cur, &
    1696            4 :                                    x0(n_coord + 1:n_coord + ndof_cur))
    1697              :       CALL cell_manifold_chart_eval(gopt_env%cell_env%manifold, &
    1698            4 :                                     x0(n_coord + 1:n_coord + ndof_cur), q_check, info)
    1699           56 :       IF (info /= info_ok .OR. &
    1700              :           MAXVAL(ABS(q_check - q_cur)) > &
    1701              :           restart_manifold_rel_tol*MAX(1.0_dp, MAXVAL(ABS(q_cur)))) THEN
    1702              :          CALL cp_abort(__LOCATION__, &
    1703              :                        "CELL_OPT: The restarted cell does not lie on the reference "// &
    1704            0 :                        "constraint manifold. Set RESTART_HESSIAN F.")
    1705              :       END IF
    1706            4 :    END SUBROUTINE gopt_f_restore_cell_manifold
    1707              : 
    1708              : ! **************************************************************************************************
    1709              : !> \brief Build cell_env%manifold from the current canonical opt_cell.
    1710              : !> \param gopt_env geometry optimization environment
    1711              : !> \param emit_warnings .TRUE. to emit setup and symmetry warnings
    1712              : ! **************************************************************************************************
    1713          250 :    SUBROUTINE build_cell_opt_manifold(gopt_env, emit_warnings)
    1714              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
    1715              :       LOGICAL, INTENT(IN)                                :: emit_warnings
    1716              : 
    1717              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: hmat_before
    1718              : 
    1719          250 :       IF (gopt_env%spgr%keep_space_group) THEN
    1720          208 :          hmat_before = gopt_env%cell_env%opt_cell%hmat
    1721              :          ! Symmetrize opt_cell before cell_opt_setup_manifold records q0 and s0.
    1722           16 :          CALL spgr_project_cell_metric(gopt_env%spgr, gopt_env%cell_env%opt_cell)
    1723          416 :          IF (emit_warnings .AND. &
    1724              :              MAXVAL(ABS(hmat_before - gopt_env%cell_env%opt_cell%hmat)) > &
    1725              :              spgr_project_warn_tol*MAX(1.0_dp, MAXVAL(ABS(hmat_before)))) THEN
    1726              :             CALL cp_warn(__LOCATION__, &
    1727              :                          "CELL_OPT: KEEP_SPACE_GROUP modified the initial cell matrix "// &
    1728            0 :                          "to match the detected point group.")
    1729              :          END IF
    1730              :       END IF
    1731          250 :       CALL cell_opt_setup_manifold(gopt_env%cell_env, emit_warnings=emit_warnings)
    1732          250 :       IF (gopt_env%spgr%keep_space_group) THEN
    1733              :          CALL cell_opt_fold_symmetry_rows( &
    1734              :             gopt_env%cell_env, &
    1735              :             spgr_point_group_symmetry_id(gopt_env%spgr, gopt_env%cell_env%opt_cell), &
    1736           16 :             emit_warnings=emit_warnings)
    1737              :       END IF
    1738          250 :    END SUBROUTINE build_cell_opt_manifold
    1739              : 
    1740              : ! **************************************************************************************************
    1741              : !> \brief Project optimizer coordinates onto the space-group-invariant subspace.
    1742              : !> \param gopt_env geometry optimization environment
    1743              : !> \param x coordinate vector to symmetrize in place
    1744              : ! **************************************************************************************************
    1745        30591 :    SUBROUTINE gopt_f_project_coord(gopt_env, x)
    1746              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
    1747              :       REAL(KIND=dp), DIMENSION(:), INTENT(INOUT)         :: x
    1748              : 
    1749        30591 :       IF (gopt_env%spgr%keep_space_group) CALL spgr_apply_rotations_coord(gopt_env%spgr, x)
    1750        30591 :    END SUBROUTINE gopt_f_project_coord
    1751              : 
    1752              : ! **************************************************************************************************
    1753              : !> \brief Project a gradient, step, or search direction onto the space-group subspace.
    1754              : !> \param gopt_env geometry optimization environment
    1755              : !> \param v vector to symmetrize in place (rotations only, no translations)
    1756              : ! **************************************************************************************************
    1757        44557 :    SUBROUTINE gopt_f_project_vector(gopt_env, v)
    1758              :       TYPE(gopt_f_type), POINTER                         :: gopt_env
    1759              :       REAL(KIND=dp), DIMENSION(:), INTENT(INOUT)         :: v
    1760              : 
    1761        44557 :       IF (gopt_env%spgr%keep_space_group) CALL spgr_apply_rotations_force(gopt_env%spgr, v)
    1762        44557 :    END SUBROUTINE gopt_f_project_vector
    1763              : 
    1764              : END MODULE gopt_f_methods
        

Generated by: LCOV version 2.0-1