LCOV - code coverage report
Current view: top level - src - input_cp2k_pwdft.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:ccc2433) Lines: 105 117 89.7 %
Date: 2024-04-25 07:09:54 Functions: 5 5 100.0 %

          Line data    Source code
       1             : !--------------------------------------------------------------------------------------------------!
       2             : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3             : !   Copyright 2000-2024 CP2K developers group <https://cp2k.org>                                   !
       4             : !                                                                                                  !
       5             : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6             : !--------------------------------------------------------------------------------------------------!
       7             : !
       8             : ! **************************************************************************************************
       9             : ! > \brief Creates the PW section of the input
      10             : ! > \par History
      11             : ! >      07.2018 created
      12             : ! > \author JHU
      13             : ! **************************************************************************************************
      14             : 
      15             : MODULE input_cp2k_pwdft
      16             : #if defined(__SIRIUS)
      17             :    USE ISO_C_BINDING, ONLY: C_LOC
      18             :    USE SIRIUS, ONLY: &
      19             :       sirius_option_get, &
      20             :       sirius_option_get_section_length, sirius_option_get_info, &
      21             :       SIRIUS_INTEGER_TYPE, SIRIUS_NUMBER_TYPE, SIRIUS_STRING_TYPE, &
      22             :       SIRIUS_LOGICAL_TYPE, SIRIUS_ARRAY_TYPE, SIRIUS_INTEGER_ARRAY_TYPE, SIRIUS_LOGICAL_ARRAY_TYPE, &
      23             :       SIRIUS_NUMBER_ARRAY_TYPE, SIRIUS_STRING_ARRAY_TYPE, string_f2c
      24             : #endif
      25             :    USE input_keyword_types, ONLY: keyword_create, &
      26             :                                   keyword_release, &
      27             :                                   keyword_type
      28             :    USE input_section_types, ONLY: section_add_keyword, &
      29             :                                   section_add_subsection, &
      30             :                                   section_create, &
      31             :                                   section_release, &
      32             :                                   section_type
      33             :    USE input_val_types, ONLY: char_t, &
      34             :                               integer_t, &
      35             :                               lchar_t, &
      36             :                               logical_t, &
      37             :                               real_t
      38             :    USE cp_output_handling, ONLY: add_last_numeric, &
      39             :                                  cp_print_key_section_create, &
      40             :                                  debug_print_level, &
      41             :                                  high_print_level, &
      42             :                                  low_print_level, &
      43             :                                  medium_print_level, &
      44             :                                  silent_print_level
      45             :    USE kinds, ONLY: dp
      46             :    USE string_utilities, ONLY: s2a
      47             : #include "./base/base_uses.f90"
      48             : 
      49             :    IMPLICIT NONE
      50             :    PRIVATE
      51             : 
      52             :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
      53             :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'input_cp2k_pwdft'
      54             : 
      55             :    PUBLIC :: create_pwdft_section
      56             : 
      57             :    INTEGER, PARAMETER, PUBLIC :: SIRIUS_NO_VDW = -1
      58             :    INTEGER, PARAMETER, PUBLIC :: SIRIUS_FUNC_VDWDF = 1
      59             :    INTEGER, PARAMETER, PUBLIC :: SIRIUS_FUNC_VDWDF2 = 2
      60             :    INTEGER, PARAMETER, PUBLIC :: SIRIUS_FUNC_VDWDFCX = 3
      61             : 
      62             : CONTAINS
      63             : 
      64             : #if defined(__SIRIUS)
      65             : ! **************************************************************************************************
      66             : !> \brief Create the input section for PW calculations using SIRIUS
      67             : !> \param section the section to create
      68             : !> \par History
      69             : !>      07.2018 created
      70             : !> \author JHU
      71             : ! **************************************************************************************************
      72        8392 :    SUBROUTINE create_pwdft_section(section)
      73             :       TYPE(section_type), POINTER                        :: section
      74             : 
      75             :       TYPE(section_type), POINTER                        :: subsection
      76             : 
      77             : !     ------------------------------------------------------------------------
      78             : 
      79        8392 :       CPASSERT(.NOT. ASSOCIATED(section))
      80             :       CALL section_create(section, __LOCATION__, name="PW_DFT", &
      81             :                           description="DFT calculation using plane waves basis can be set in this section. "// &
      82             :                           "The backend called SIRIUS, computes the basic properties of the system, "// &
      83             :                           "such as ground state, forces and stresses tensors which can be used by "// &
      84             :                           "cp2k afterwards. The engine has all these features build-in, support of "// &
      85             :                           "pseudo-potentials and full-potentials, spin-orbit coupling, collinear and "// &
      86             :                           "non collinear magnetism, Hubbard correction, all exchange functionals "// &
      87        8392 :                           "supported by libxc and Van der Waals corrections (libvdwxc).")
      88             : 
      89        8392 :       NULLIFY (subsection)
      90        8392 :       CALL create_sirius_section(subsection, 'control')
      91        8392 :       CALL section_add_subsection(section, subsection)
      92        8392 :       CALL section_release(subsection)
      93             : 
      94        8392 :       CALL create_sirius_section(subsection, 'parameters')
      95        8392 :       CALL section_add_subsection(section, subsection)
      96        8392 :       CALL section_release(subsection)
      97             : 
      98        8392 :       CALL create_sirius_section(subsection, 'settings')
      99        8392 :       CALL section_add_subsection(section, subsection)
     100        8392 :       CALL section_release(subsection)
     101             : 
     102        8392 :       CALL create_sirius_section(subsection, 'mixer')
     103        8392 :       CALL section_add_subsection(section, subsection)
     104        8392 :       CALL section_release(subsection)
     105             : 
     106        8392 :       CALL create_sirius_section(subsection, 'iterative_solver')
     107        8392 :       CALL section_add_subsection(section, subsection)
     108        8392 :       CALL section_release(subsection)
     109             : 
     110             :       !
     111             :       ! uncomment these lines when nlcg is officialy supported in cp2k
     112             :       !
     113             : 
     114             :       !      CALL create_sirius_section(subsection, 'nlcg')
     115             :       !      CALL section_add_subsection(section, subsection)
     116             :       !      CALL section_release(subsection)
     117             : 
     118        8392 :       CALL create_print_section(subsection)
     119        8392 :       CALL section_add_subsection(section, subsection)
     120        8392 :       CALL section_release(subsection)
     121             : 
     122        8392 :    END SUBROUTINE create_pwdft_section
     123             : 
     124             : ! **************************************************************************************************
     125             : !> \brief input section for PWDFT control
     126             : !> \param section will contain the CONTROL section
     127             : !> \param section_name ...
     128             : !> \author JHU
     129             : ! **************************************************************************************************
     130       41960 :    SUBROUTINE create_sirius_section(section, section_name)
     131             :       TYPE(section_type), POINTER                        :: section
     132             :       CHARACTER(len=*), INTENT(in)                       :: section_name
     133             : 
     134             :       INTEGER                                            :: length
     135             : 
     136           0 :       CPASSERT(.NOT. ASSOCIATED(section))
     137       41960 :       CALL sirius_option_get_section_length(TRIM(ADJUSTL(section_name)), length)
     138             : 
     139             :       CALL section_create(section, __LOCATION__, &
     140             :                           name=TRIM(ADJUSTL(section_name)), &
     141             :                           description=TRIM(section_name)//" section", &
     142             :                           n_subsections=0, &
     143             :                           n_keywords=length, &
     144       41960 :                           repeats=.FALSE.)
     145             : 
     146       41960 :       CALL fill_in_section(section, TRIM(ADJUSTL(section_name)))
     147       41960 :    END SUBROUTINE create_sirius_section
     148             : 
     149             : ! **************************************************************************************************
     150             : !> \brief ...
     151             : !> \param section ...
     152             : !> \param section_name ...
     153             : ! **************************************************************************************************
     154       41960 :    SUBROUTINE fill_in_section(section, section_name)
     155             :       TYPE(section_type), POINTER                        :: section
     156             :       CHARACTER(len=*), INTENT(in)                       :: section_name
     157             : 
     158             :       CHARACTER(len=128)                                 :: name
     159             :       CHARACTER(len=128), TARGET                         :: possible_values(1:16)
     160             :       CHARACTER(len=4096)                                :: description, usage
     161             :       INTEGER                                            :: ctype, enum_i_val(1:16), enum_length, i, &
     162             :                                                             j, length, num_possible_values
     163       41960 :       INTEGER, ALLOCATABLE, DIMENSION(:), TARGET         :: ivec
     164             :       INTEGER, TARGET                                    :: dummy_i
     165             :       LOGICAL                                            :: lvecl(1:16)
     166       41960 :       LOGICAL(4), ALLOCATABLE, DIMENSION(:), TARGET      :: lvec
     167             :       LOGICAL(4), TARGET                                 :: dummy_l
     168       41960 :       REAL(kind=dp), ALLOCATABLE, DIMENSION(:), TARGET   :: rvec
     169             :       REAL(kind=dp), TARGET                              :: dummy_r
     170             :       TYPE(keyword_type), POINTER                        :: keyword
     171             : 
     172       41960 :       ALLOCATE (ivec(1:16))
     173       41960 :       ALLOCATE (rvec(1:16))
     174       41960 :       ALLOCATE (lvec(1:16))
     175             : 
     176             : #ifdef __LIBVDWXC
     177       41960 :       IF (section_name == "parameters") THEN
     178        8392 :          NULLIFY (keyword)
     179             :          CALL keyword_create(keyword, __LOCATION__, name="VDW_FUNCTIONAL", &
     180             :                              description="Select the Van der Walls functionals corrections type", &
     181             :                              default_i_val=SIRIUS_NO_VDW, &
     182             :                              enum_i_vals=(/SIRIUS_NO_VDW, SIRIUS_FUNC_VDWDF, SIRIUS_FUNC_VDWDF2, SIRIUS_FUNC_VDWDFCX/), &
     183             :                              enum_c_vals=s2a("NONE", "FUNC_VDWDF", "FUNC_VDWDF2", "FUNC_VDWDFCX"), &
     184             :                              enum_desc=s2a("No VdW correction", &
     185             :                                            "FUNC_VDWDF", &
     186             :                                            "FUNC_VDWDF2", &
     187        8392 :                                            "FUNC_VDWDFCX"))
     188        8392 :          CALL section_add_keyword(section, keyword)
     189        8392 :          CALL keyword_release(keyword)
     190             :       END IF
     191             : #endif
     192             : 
     193       41960 :       CALL sirius_option_get_section_length(section_name, length)
     194             : 
     195      839200 :       DO i = 1, length
     196      797240 :          NULLIFY (keyword)
     197      797240 :          name = ''
     198      797240 :          description = ''
     199      797240 :          usage = ''
     200             :          CALL sirius_option_get_info(section_name, &
     201             :                                      i, &
     202             :                                      name, &
     203             :                                      128, &
     204             :                                      ctype, &
     205             :                                      num_possible_values, &
     206             :                                      enum_length, &
     207             :                                      description, &
     208             :                                      4096, &
     209             :                                      usage, &
     210      797240 :                                      4096)
     211             :          ! description and usage are ignored here
     212             :          ! it is a minor inconvenience from the api.
     213             : 
     214      797240 :          name = TRIM(ADJUSTL(name))
     215             :          ! I exclude these three keywords because one of them is for debugging
     216             :          ! purpose the other are replaced by a dedicated call in cp2k
     217             :          !
     218             :          ! Moreover xc_functionals would need a special treatment.
     219             : 
     220     1636440 :          IF ((name /= 'xc_functionals') .AND. (name /= 'memory_usage') .AND. (name /= 'vk')) THEN
     221             :             !     we need to null char since SIRIUS interface is basically C
     222      209800 :             SELECT CASE (ctype)
     223             :             CASE (SIRIUS_INTEGER_TYPE)
     224      209800 :                CALL sirius_option_get(section_name, name, ctype, C_LOC(dummy_i))
     225             :                CALL keyword_create(keyword, __LOCATION__, &
     226             :                                    name=TRIM(name), &
     227             :                                    description=TRIM(ADJUSTL(description)), &
     228             :                                    !                                   usage=TRIM(ADJUSTL(usage)), &
     229             :                                    type_of_var=integer_t, &
     230             :                                    repeats=.FALSE., &
     231      209800 :                                    default_i_val=dummy_i)
     232      209800 :                CALL section_add_keyword(section, keyword)
     233      209800 :                CALL keyword_release(keyword)
     234             :             CASE (SIRIUS_NUMBER_TYPE)
     235      226584 :                CALL sirius_option_get(section_name, name, ctype, C_LOC(dummy_r))
     236             :                CALL keyword_create(keyword, __LOCATION__, &
     237             :                                    name=name, &
     238             :                                    description=TRIM(ADJUSTL(description)), &
     239             :                                    !                                   usage=TRIM(ADJUSTL(usage)), &
     240             :                                    type_of_var=real_t, &
     241             :                                    repeats=.FALSE., &
     242      226584 :                                    default_r_val=dummy_r)
     243      226584 :                CALL section_add_keyword(section, keyword)
     244      226584 :                CALL keyword_release(keyword)
     245             :             CASE (SIRIUS_LOGICAL_TYPE)
     246      167840 :                dummy_l = .FALSE.
     247      167840 :                CALL sirius_option_get(section_name, name, ctype, C_LOC(dummy_l))
     248      167840 :                IF (dummy_l) THEN
     249             :                   CALL keyword_create(keyword, __LOCATION__, &
     250             :                                       name=name, &
     251             :                                       description=TRIM(ADJUSTL(description)), &
     252             :                                       !                                      usage=TRIM(ADJUSTL(usage)), &
     253             :                                       type_of_var=logical_t, &
     254             :                                       repeats=.FALSE., &
     255             :                                       default_l_val=.TRUE., &
     256       67136 :                                       lone_keyword_l_val=.TRUE.)
     257             :                ELSE
     258             :                   CALL keyword_create(keyword, __LOCATION__, &
     259             :                                       name=name, &
     260             :                                       description=TRIM(ADJUSTL(description)), &
     261             :                                       !                                      usage=TRIM(ADJUSTL(usage)), &
     262             :                                       type_of_var=logical_t, &
     263             :                                       repeats=.FALSE., &
     264             :                                       default_l_val=.FALSE., &
     265      100704 :                                       lone_keyword_l_val=.TRUE.)
     266             :                END IF
     267      167840 :                CALL section_add_keyword(section, keyword)
     268      167840 :                CALL keyword_release(keyword)
     269             :             CASE (SIRIUS_STRING_TYPE)
     270      134272 :                IF (enum_length >= 1) THEN
     271      553872 :                   DO j = 1, enum_length
     272      436384 :                      possible_values(j) = ''
     273      436384 :                      CALL sirius_option_get(section_name, name, ctype, C_LOC(possible_values(j)), max_length=128, enum_idx=j)
     274      436384 :                      enum_i_val(j) = j
     275      553872 :                      possible_values(j) = TRIM(ADJUSTL(possible_values(j)))
     276             :                   END DO
     277             : 
     278      117488 :                   IF (enum_length > 1) THEN
     279             :                      CALL keyword_create(keyword, __LOCATION__, &
     280             :                                          name=name, &
     281             :                                          description=TRIM(ADJUSTL(description)), &
     282             :                                          !                                      usage=TRIM(ADJUSTL(usage)), &
     283             :                                          repeats=.FALSE., &
     284             :                                          enum_i_vals=enum_i_val(1:enum_length), &
     285             :                                          enum_c_vals=possible_values(1:enum_length), &
     286      117488 :                                          default_i_val=1)
     287             :                   ELSE
     288             :                      CALL keyword_create(keyword, __LOCATION__, &
     289             :                                          name=name, &
     290             :                                          description=TRIM(ADJUSTL(description)), &
     291             :                                          !                                      usage=TRIM(ADJUSTL(usage)), &
     292             :                                          type_of_var=char_t, &
     293             :                                          default_c_val=possible_values(1), &
     294           0 :                                          repeats=.FALSE.)
     295             :                   END IF
     296             :                ELSE
     297             :                   CALL keyword_create(keyword, __LOCATION__, &
     298             :                                       name=name, &
     299             :                                       description=TRIM(ADJUSTL(description)), &
     300             :                                       !                                      usage=TRIM(ADJUSTL(usage)), &
     301             :                                       type_of_var=char_t, &
     302             :                                       default_c_val='', &
     303       16784 :                                       repeats=.FALSE.)
     304             :                END IF
     305      134272 :                CALL section_add_keyword(section, keyword)
     306      134272 :                CALL keyword_release(keyword)
     307             :             CASE (SIRIUS_INTEGER_ARRAY_TYPE)
     308       33568 :                CALL sirius_option_get(section_name, name, ctype, C_LOC(ivec(1)), max_length=16)
     309             : 
     310       33568 :                IF (num_possible_values .EQ. 0) THEN
     311             :                   CALL keyword_create(keyword, __LOCATION__, &
     312             :                                       name=name, &
     313             :                                       description=TRIM(ADJUSTL(description)), &
     314             :                                       type_of_var=integer_t, &
     315             :                                       n_var=-1, &
     316           0 :                                       repeats=.FALSE.)
     317             :                ELSE
     318             :                   CALL keyword_create(keyword, __LOCATION__, &
     319             :                                       name=name, &
     320             :                                       description=TRIM(ADJUSTL(description)), &
     321             :                                       type_of_var=integer_t, &
     322             :                                       repeats=.FALSE., &
     323             :                                       n_var=num_possible_values, &
     324       33568 :                                       default_i_vals=ivec(1:num_possible_values))
     325             :                END IF
     326       33568 :                CALL section_add_keyword(section, keyword)
     327       33568 :                CALL keyword_release(keyword)
     328             :             CASE (SIRIUS_LOGICAL_ARRAY_TYPE)
     329           0 :                CALL sirius_option_get(section_name, name, ctype, C_LOC(lvec(1)), max_length=16)
     330           0 :                DO j = 1, num_possible_values
     331           0 :                   lvecl(j) = lvec(j)
     332             :                END DO
     333           0 :                IF (num_possible_values > 0) THEN
     334             :                   CALL keyword_create(keyword, __LOCATION__, &
     335             :                                       name=name, &
     336             :                                       description=TRIM(ADJUSTL(description)), &
     337             :                                       !usage=TRIM(ADJUSTL(usage)), &
     338             :                                       type_of_var=logical_t, &
     339             :                                       repeats=.FALSE., &
     340             :                                       n_var=num_possible_values, &
     341           0 :                                       default_l_vals=lvecl(1:num_possible_values))
     342             :                ELSE
     343             :                   CALL keyword_create(keyword, __LOCATION__, &
     344             :                                       name=name, &
     345             :                                       description=TRIM(ADJUSTL(description)), &
     346             :                                       !usage=TRIM(ADJUSTL(usage)), &
     347             :                                       type_of_var=logical_t, &
     348             :                                       repeats=.FALSE., &
     349           0 :                                       n_var=-1)
     350             :                END IF
     351           0 :                CALL section_add_keyword(section, keyword)
     352           0 :                CALL keyword_release(keyword)
     353             :             CASE (SIRIUS_NUMBER_ARRAY_TYPE)
     354        8392 :                CALL sirius_option_get(section_name, name, ctype, C_LOC(rvec(1)), max_length=16)
     355             : 
     356        8392 :                IF (num_possible_values .EQ. 0) THEN
     357             :                   CALL keyword_create(keyword, __LOCATION__, &
     358             :                                       name=name, &
     359             :                                       description=TRIM(ADJUSTL(description)), &
     360             :                                       !                                   usage=TRIM(ADJUSTL(usage)), &
     361             :                                       type_of_var=real_t, &
     362             :                                       repeats=.FALSE., &
     363           0 :                                       n_var=-1)
     364             :                ELSE
     365             :                   CALL keyword_create(keyword, __LOCATION__, &
     366             :                                       name=name, &
     367             :                                       description=TRIM(ADJUSTL(description)), &
     368             :                                       !     usage=TRIM(ADJUSTL(usage)), &
     369             :                                       type_of_var=real_t, &
     370             :                                       repeats=.FALSE., &
     371             :                                       n_var=num_possible_values, &
     372        8392 :                                       default_r_vals=rvec(1:num_possible_values))
     373             :                END IF
     374        8392 :                CALL section_add_keyword(section, keyword)
     375      788848 :                CALL keyword_release(keyword)
     376             :             CASE default
     377             :             END SELECT
     378             :          END IF
     379             :       END DO
     380       83920 :    END SUBROUTINE fill_in_section
     381             : 
     382             : ! **************************************************************************************************
     383             : !> \brief Create the print section for sirius
     384             : !> \param section the section to create
     385             : !> \author jgh
     386             : ! **************************************************************************************************
     387        8392 :    SUBROUTINE create_print_section(section)
     388             :       TYPE(section_type), POINTER                        :: section
     389             : 
     390             :       TYPE(section_type), POINTER                        :: print_key
     391             : 
     392        8392 :       CPASSERT(.NOT. ASSOCIATED(section))
     393             :       CALL section_create(section, __LOCATION__, name="PRINT", &
     394             :                           description="Section of possible print options in PW_DFT code.", &
     395        8392 :                           n_keywords=0, n_subsections=1, repeats=.FALSE.)
     396             : 
     397        8392 :       NULLIFY (print_key)
     398        8392 :       CALL create_dos_section(print_key)
     399        8392 :       CALL section_add_subsection(section, print_key)
     400        8392 :       CALL section_release(print_key)
     401             : 
     402        8392 :    END SUBROUTINE create_print_section
     403             : 
     404             : ! **************************************************************************************************
     405             : !> \brief ...
     406             : !> \param print_key ...
     407             : ! **************************************************************************************************
     408        8392 :    SUBROUTINE create_dos_section(print_key)
     409             : 
     410             :       TYPE(section_type), POINTER                        :: print_key
     411             : 
     412             :       TYPE(keyword_type), POINTER                        :: keyword
     413             : 
     414        8392 :       NULLIFY (keyword)
     415             : 
     416             :       CALL cp_print_key_section_create(print_key, __LOCATION__, "DOS", &
     417             :                                        description="Print Density of States (DOS) (only available states from SCF)", &
     418        8392 :                                        print_level=debug_print_level, common_iter_levels=1, filename="")
     419             : 
     420             :       CALL keyword_create(keyword, __LOCATION__, name="APPEND", &
     421             :                           description="Append the DOS obtained at different iterations to the output file. "// &
     422             :                           "By default the file is overwritten", &
     423             :                           usage="APPEND", default_l_val=.FALSE., &
     424        8392 :                           lone_keyword_l_val=.TRUE.)
     425        8392 :       CALL section_add_keyword(print_key, keyword)
     426        8392 :       CALL keyword_release(keyword)
     427             : 
     428             :       CALL keyword_create(keyword, __LOCATION__, name="DELTA_E", &
     429             :                           description="Histogramm energy spacing.", &
     430        8392 :                           usage="DELTA_E 0.0005", type_of_var=real_t, default_r_val=0.001_dp)
     431        8392 :       CALL section_add_keyword(print_key, keyword)
     432        8392 :       CALL keyword_release(keyword)
     433             : 
     434        8392 :    END SUBROUTINE create_dos_section
     435             : 
     436             : #else
     437             : ! **************************************************************************************************
     438             : !> \brief ...
     439             : !> \param section ...
     440             : ! **************************************************************************************************
     441             :    SUBROUTINE create_pwdft_section(section)
     442             :       TYPE(section_type), POINTER                        :: section
     443             : 
     444             :       CPASSERT(.NOT. ASSOCIATED(section))
     445             : 
     446             :       CALL section_create(section, __LOCATION__, name="PW_DFT", &
     447             :                           description="This section contains all information to run an "// &
     448             :                           "SIRIUS PW calculation.", &
     449             :                           n_subsections=0, &
     450             :                           repeats=.FALSE.)
     451             : 
     452             :    END SUBROUTINE create_pwdft_section
     453             : 
     454             : #endif
     455             : 
     456             : END MODULE input_cp2k_pwdft

Generated by: LCOV version 1.15