LCOV - code coverage report
Current view: top level - src/input - input_parsing.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:936074a) Lines: 75.8 % 335 254
Test Date: 2025-12-04 06:27:48 Functions: 100.0 % 3 3

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2025 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief routines that parse the input
      10              : !> \par History
      11              : !>      06.2004 created
      12              : !> \author fawzi
      13              : ! **************************************************************************************************
      14              : MODULE input_parsing
      15              :    USE cp_linked_list_input,            ONLY: &
      16              :         cp_create, cp_dealloc, cp_sll_char_type, cp_sll_int_type, cp_sll_logical_type, &
      17              :         cp_sll_real_type, cp_sll_val_create, cp_sll_val_type, cp_to_array
      18              :    USE cp_log_handling,                 ONLY: cp_logger_get_default_io_unit,&
      19              :                                               cp_to_string
      20              :    USE cp_parser_methods,               ONLY: parser_get_object,&
      21              :                                               parser_location,&
      22              :                                               parser_skip_space,&
      23              :                                               parser_test_next_token
      24              :    USE cp_parser_types,                 ONLY: cp_parser_type
      25              :    USE cp_units,                        ONLY: cp_unit_compatible,&
      26              :                                               cp_unit_create,&
      27              :                                               cp_unit_desc,&
      28              :                                               cp_unit_release,&
      29              :                                               cp_unit_set_type,&
      30              :                                               cp_unit_to_cp2k1,&
      31              :                                               cp_unit_type
      32              :    USE input_enumeration_types,         ONLY: enum_c2i,&
      33              :                                               enumeration_type
      34              :    USE input_keyword_types,             ONLY: keyword_describe,&
      35              :                                               keyword_type
      36              :    USE input_section_types,             ONLY: &
      37              :         section_describe, section_get_keyword, section_get_keyword_index, &
      38              :         section_get_subsection_index, section_type, section_typo_match, section_vals_add_values, &
      39              :         section_vals_type, typo_match_section, typo_matching_line, typo_matching_rank
      40              :    USE input_val_types,                 ONLY: &
      41              :         char_t, enum_t, integer_t, lchar_t, logical_t, no_t, real_t, val_create, val_type
      42              :    USE kinds,                           ONLY: default_string_length,&
      43              :                                               dp,&
      44              :                                               max_line_length
      45              :    USE string_utilities,                ONLY: uppercase
      46              : #include "../base/base_uses.f90"
      47              : 
      48              :    IMPLICIT NONE
      49              :    PRIVATE
      50              : 
      51              :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
      52              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'input_parsing'
      53              : 
      54              :    PUBLIC :: section_vals_parse
      55              : !***
      56              : CONTAINS
      57              : 
      58              : ! **************************************************************************************************
      59              : !> \brief ...
      60              : !> \param section_vals ...
      61              : !> \param parser ...
      62              : !> \param default_units ...
      63              : !> \param root_section if the root section should be parsed (defaults to true)
      64              : !> \author fawzi
      65              : ! **************************************************************************************************
      66       236622 :    RECURSIVE SUBROUTINE section_vals_parse(section_vals, parser, default_units, root_section)
      67              :       TYPE(section_vals_type), POINTER                   :: section_vals
      68              :       TYPE(cp_parser_type), INTENT(INOUT)                :: parser
      69              :       TYPE(cp_unit_set_type), INTENT(IN)                 :: default_units
      70              :       LOGICAL, INTENT(in), OPTIONAL                      :: root_section
      71              : 
      72              :       CHARACTER(len=*), PARAMETER :: routineN = 'section_vals_parse'
      73              : 
      74              :       CHARACTER(len=max_line_length)                     :: token
      75              :       INTEGER                                            :: desc_level, handle, ik, imatch, irs, is, &
      76              :                                                             nsub, output_unit
      77              :       LOGICAL                                            :: at_end, compatible_end, root_sect, &
      78              :                                                             whole_section
      79              :       TYPE(cp_sll_val_type), POINTER                     :: last_val, new_val, previous_last, &
      80              :                                                             previous_list
      81              :       TYPE(keyword_type), POINTER                        :: keyword
      82              :       TYPE(section_type), POINTER                        :: section
      83              :       TYPE(val_type), POINTER                            :: el
      84              : 
      85       236622 :       CALL timeset(routineN, handle)
      86              : 
      87       236622 :       NULLIFY (previous_list, previous_last)
      88              : 
      89       236622 :       root_sect = .TRUE.
      90       236622 :       IF (PRESENT(root_section)) root_sect = root_section
      91              : 
      92       236622 :       CPASSERT(ASSOCIATED(section_vals))
      93       236622 :       output_unit = cp_logger_get_default_io_unit()
      94              : 
      95       236622 :       CPASSERT(section_vals%ref_count > 0)
      96       236622 :       IF (root_sect .AND. parser%icol1 > parser%icol2) &
      97              :          CALL cp_abort(__LOCATION__, &
      98              :                        "Error 1: this routine must be called just after having parsed the start of the section " &
      99            0 :                        //TRIM(parser_location(parser)))
     100       236622 :       section => section_vals%section
     101       236622 :       IF (root_sect) THEN
     102       226528 :          token = TRIM(ADJUSTL(parser%input_line(parser%icol1:parser%icol2))) ! Ignore leading or trailing blanks
     103       226528 :          CALL uppercase(token)
     104       226528 :          IF (token /= parser%section_character//section%name) &
     105              :             CALL cp_abort(__LOCATION__, &
     106              :                           "Error 2: this routine must be called just after having parsed the start of the section " &
     107            0 :                           //TRIM(parser_location(parser)))
     108              :       END IF
     109       236622 :       IF (.NOT. section%repeats .AND. SIZE(section_vals%values, 2) /= 0) &
     110              :          CALL cp_abort(__LOCATION__, "Section "//TRIM(section%name)// &
     111            0 :                        " should not repeat "//TRIM(parser_location(parser)))
     112       236622 :       CALL section_vals_add_values(section_vals)
     113       236622 :       irs = SIZE(section_vals%values, 2)
     114              : 
     115       236622 :       IF (ALLOCATED(section%deprecation_notice)) THEN
     116              :          CALL cp_warn(__LOCATION__, &
     117              :                       "The specified section '"//TRIM(section%name)// &
     118              :                       "' is deprecated and may be removed in a future version: "// &
     119            0 :                       section%deprecation_notice)
     120              :       END IF
     121              : 
     122       236622 :       IF (ASSOCIATED(section%keywords(-1)%keyword)) THEN ! reads section params
     123        50575 :          keyword => section%keywords(-1)%keyword
     124        50575 :          NULLIFY (el)
     125        50575 :          IF (keyword%type_of_var == lchar_t) CALL parser_skip_space(parser)
     126              :          CALL val_create_parsing(el, type_of_var=keyword%type_of_var, &
     127              :                                  n_var=keyword%n_var, default_value=keyword%lone_keyword_value, &
     128              :                                  enum=keyword%enum, unit=keyword%unit, &
     129              :                                  default_units=default_units, &
     130        50575 :                                  parser=parser)
     131        50575 :          NULLIFY (new_val)
     132        50575 :          CALL cp_sll_val_create(new_val, el)
     133        50575 :          section_vals%values(-1, irs)%list => new_val
     134        50575 :          NULLIFY (el)
     135              :       END IF
     136              :       DO WHILE (.TRUE.)
     137              :          CALL parser_get_object(parser, token, newline=.TRUE., &
     138      1112617 :                                 lower_to_upper=.TRUE., at_end=at_end)
     139      1112617 :          token = TRIM(ADJUSTL(token)) ! Ignore leading or trailing blanks
     140      1112617 :          IF (at_end) THEN
     141        10094 :             IF (root_sect) &
     142              :                CALL cp_abort(__LOCATION__, &
     143              :                              "unexpected end of file while parsing section "// &
     144            0 :                              TRIM(section%name)//" "//TRIM(parser_location(parser)))
     145              :             EXIT
     146              :          END IF
     147      1102523 :          IF (token(1:1) == parser%section_character) THEN
     148       452971 :             IF (token == "&END") THEN
     149              :                ! end of section
     150       226528 :                compatible_end = .TRUE.
     151       226528 :                IF (parser_test_next_token(parser) /= "EOL") THEN
     152              :                   CALL parser_get_object(parser, token, newline=.FALSE., &
     153       225042 :                                          lower_to_upper=.TRUE.)
     154       451570 :                   IF (token /= "SECTION" .AND. token /= section%name) THEN
     155            0 :                      compatible_end = .FALSE.
     156              :                   END IF
     157              :                END IF
     158       226528 :                IF (parser_test_next_token(parser) /= "EOL") THEN
     159              :                   CALL parser_get_object(parser, token, newline=.FALSE., &
     160            0 :                                          lower_to_upper=.TRUE.)
     161       226528 :                   IF (token /= section%name) THEN
     162            0 :                      PRINT *, TRIM(token), "/=", TRIM(section%name)
     163              :                      compatible_end = .FALSE.
     164              :                   END IF
     165              :                END IF
     166       226528 :                IF (.NOT. compatible_end) THEN
     167              :                   CALL cp_abort(__LOCATION__, &
     168              :                                 "non-compatible end of section "//TRIM(section%name)//" "// &
     169            0 :                                 TRIM(parser_location(parser)))
     170              :                END IF
     171              :                !              RETURN
     172              :                EXIT
     173              :             END IF
     174       226443 :             is = section_get_subsection_index(section, token(2:))
     175       226443 :             IF (is > 0) THEN
     176              :                CALL section_vals_parse(section_vals%subs_vals(is, irs)%section_vals, &
     177       226443 :                                        default_units=default_units, parser=parser)
     178              :             ELSE
     179              :                ! unknown subsection
     180            0 :                IF (output_unit > 0) THEN
     181            0 :                   WRITE (output_unit, *)
     182            0 :                   WRITE (output_unit, '(T2,A)') "Possible matches for unknown subsection "
     183            0 :                   WRITE (output_unit, *)
     184            0 :                   WRITE (output_unit, '(T2,A)') TRIM(token(2:))
     185            0 :                   WRITE (output_unit, *)
     186              :                   CALL section_typo_match(typo_match_section, TRIM(section%name), TRIM(token(2:)), "", &
     187            0 :                                           typo_matching_rank, typo_matching_line, bonus=0)
     188            0 :                   DO imatch = 1, SIZE(typo_matching_rank)
     189            0 :                      WRITE (output_unit, '(T2,A,1X,I0)') TRIM(typo_matching_line(imatch))//" score: ", typo_matching_rank(imatch)
     190              :                   END DO
     191              :                END IF
     192              :                CALL cp_abort(__LOCATION__, &
     193              :                              "unknown subsection "//TRIM(token(2:))//" of section " &
     194            0 :                              //TRIM(section%name))
     195            0 :                nSub = 1
     196            0 :                DO WHILE (nSub > 0)
     197              :                   CALL parser_get_object(parser, token, newline=.TRUE., &
     198            0 :                                          lower_to_upper=.TRUE.)
     199            0 :                   IF (token(1:1) == parser%section_character) THEN
     200            0 :                      IF (token == "&END") THEN
     201            0 :                         nSub = nSub - 1
     202              :                      ELSE
     203            0 :                         nSub = nSub + 1
     204              :                      END IF
     205              :                   END IF
     206              :                END DO
     207              :             END IF
     208              :          ELSE ! token is a keyword
     209       649552 :             IF (token == "DESCRIBE") THEN
     210            2 :                IF (output_unit > 0) WRITE (output_unit, "(/,' ****** DESCRIPTION ******',/)")
     211            2 :                desc_level = 3
     212            2 :                IF (parser_test_next_token(parser) == "INT") THEN
     213            2 :                   CALL parser_get_object(parser, desc_level)
     214              :                END IF
     215            2 :                whole_section = .TRUE.
     216            2 :                DO WHILE (parser_test_next_token(parser) == "STR")
     217            0 :                   whole_section = .FALSE.
     218              :                   CALL parser_get_object(parser, token, newline=.FALSE., &
     219            0 :                                          lower_to_upper=.TRUE.)
     220            0 :                   keyword => section_get_keyword(section, token)
     221            0 :                   IF (.NOT. ASSOCIATED(keyword)) THEN
     222              :                      CALL cp_warn(__LOCATION__, &
     223              :                                   "unknown keyword to describe "//TRIM(token)// &
     224            0 :                                   " in section "//TRIM(section%name))
     225              :                   ELSE
     226            0 :                      CALL keyword_describe(keyword, output_unit, desc_level)
     227              :                   END IF
     228              :                END DO
     229            2 :                IF (whole_section) THEN
     230            2 :                   CALL section_describe(section, output_unit, desc_level, hide_root=.NOT. root_sect)
     231              :                END IF
     232            2 :                IF (output_unit > 0) WRITE (output_unit, "(/,' ****** =========== ******',/)")
     233              : 
     234              :             ELSE ! token is a "normal" keyword
     235       649550 :                ik = section_get_keyword_index(section, token)
     236       649550 :                IF (ik < 1) THEN ! don't accept pseudo keyword names
     237       286958 :                   parser%icol = parser%icol1 - 1 ! re-read also the actual token
     238       286958 :                   ik = 0
     239       286958 :                   IF (.NOT. ASSOCIATED(section%keywords(0)%keyword)) THEN
     240            0 :                      IF (output_unit > 0) THEN
     241            0 :                         WRITE (output_unit, *)
     242            0 :                         WRITE (output_unit, '(T2,A)') "Possible matches for unknown keyword "
     243            0 :                         WRITE (output_unit, *)
     244            0 :                         WRITE (output_unit, '(T2,A)') TRIM(token)
     245            0 :                         WRITE (output_unit, *)
     246              :                         CALL section_typo_match(typo_match_section, TRIM(section%name), TRIM(token), "", &
     247            0 :                                                 typo_matching_rank, typo_matching_line, bonus=0)
     248            0 :                         DO imatch = 1, SIZE(typo_matching_rank)
     249              :                            WRITE (output_unit, '(T2,A,1X,I0)') &
     250            0 :                               TRIM(typo_matching_line(imatch))//" score: ", typo_matching_rank(imatch)
     251              :                         END DO
     252              :                      END IF
     253              :                      CALL cp_abort(__LOCATION__, &
     254              :                                    "found an unknown keyword "//TRIM(token)// &
     255            0 :                                    " in section "//TRIM(section%name))
     256              :                   END IF
     257              :                END IF
     258       649550 :                keyword => section%keywords(ik)%keyword
     259       649550 :                IF (ASSOCIATED(keyword)) THEN
     260       649550 :                   IF (keyword%removed) THEN
     261            0 :                      IF (ALLOCATED(keyword%deprecation_notice)) THEN
     262              :                         CALL cp_abort(__LOCATION__, &
     263              :                                       "The specified keyword '"//TRIM(token)//"' is not available anymore: "// &
     264            0 :                                       keyword%deprecation_notice)
     265              :                      ELSE
     266              :                         CALL cp_abort(__LOCATION__, &
     267              :                                       "The specified keyword '"//TRIM(token)// &
     268            0 :                                       "' is not available anymore, please consult the manual.")
     269              :                      END IF
     270              :                   END IF
     271              : 
     272       649550 :                   IF (ALLOCATED(keyword%deprecation_notice)) &
     273              :                      CALL cp_warn(__LOCATION__, &
     274              :                                   "The specified keyword '"//TRIM(token)// &
     275              :                                   "' is deprecated and may be removed in a future version: "// &
     276           56 :                                   keyword%deprecation_notice)
     277              : 
     278       649550 :                   NULLIFY (el)
     279       649550 :                   IF (ik /= 0 .AND. keyword%type_of_var == lchar_t) &
     280        21635 :                      CALL parser_skip_space(parser)
     281              :                   CALL val_create_parsing(el, type_of_var=keyword%type_of_var, &
     282              :                                           n_var=keyword%n_var, default_value=keyword%lone_keyword_value, &
     283              :                                           enum=keyword%enum, unit=keyword%unit, &
     284       649550 :                                           default_units=default_units, parser=parser)
     285       649550 :                   IF (ASSOCIATED(el)) THEN
     286       649550 :                      NULLIFY (new_val)
     287       649550 :                      CALL cp_sll_val_create(new_val, el)
     288       649550 :                      last_val => section_vals%values(ik, irs)%list
     289       649550 :                      IF (.NOT. ASSOCIATED(last_val)) THEN
     290       368965 :                         section_vals%values(ik, irs)%list => new_val
     291              :                      ELSE
     292       280585 :                         IF (.NOT. keyword%repeats) &
     293              :                            CALL cp_abort(__LOCATION__, &
     294              :                                          "Keyword "//TRIM(token)// &
     295            0 :                                          " in section "//TRIM(section%name)//" should not repeat.")
     296       280585 :                         IF (ASSOCIATED(last_val, previous_list)) THEN
     297       280585 :                            last_val => previous_last
     298              :                         ELSE
     299        11745 :                            previous_list => last_val
     300              :                         END IF
     301       280585 :                         DO WHILE (ASSOCIATED(last_val%rest))
     302       280585 :                            last_val => last_val%rest
     303              :                         END DO
     304       280585 :                         last_val%rest => new_val
     305       280585 :                         previous_last => new_val
     306              :                      END IF
     307              :                   END IF
     308              :                END IF
     309              :             END IF
     310              :          END IF
     311              :       END DO
     312       236622 :       CALL timestop(handle)
     313       236622 :    END SUBROUTINE section_vals_parse
     314              : 
     315              : ! **************************************************************************************************
     316              : !> \brief creates a val_type object by parsing the values
     317              : !> \param val the value that will be created
     318              : !> \param type_of_var type of the value to be created
     319              : !> \param n_var number of values to be parsed (-1: undefined)
     320              : !> \param enum ...
     321              : !> \param parser the parser from where the values should be read
     322              : !> \param unit ...
     323              : !> \param default_units ...
     324              : !> \param default_value a default value if nothing is found (can be null)
     325              : !> \author fawzi
     326              : !> \note
     327              : !>      - no_t does not create a value
     328              : ! **************************************************************************************************
     329       700125 :    SUBROUTINE val_create_parsing(val, type_of_var, n_var, enum, &
     330              :                                  parser, unit, default_units, default_value)
     331              :       TYPE(val_type), POINTER                            :: val
     332              :       INTEGER, INTENT(in)                                :: type_of_var, n_var
     333              :       TYPE(enumeration_type), POINTER                    :: enum
     334              :       TYPE(cp_parser_type), INTENT(INOUT)                :: parser
     335              :       TYPE(cp_unit_type), POINTER                        :: unit
     336              :       TYPE(cp_unit_set_type), INTENT(IN)                 :: default_units
     337              :       TYPE(val_type), OPTIONAL, POINTER                  :: default_value
     338              : 
     339              :       CHARACTER(len=*), PARAMETER :: routineN = 'val_create_parsing'
     340              : 
     341              :       CHARACTER(len=default_string_length)               :: c_val, info, location
     342              :       CHARACTER(len=default_string_length), &
     343       700125 :          DIMENSION(:), POINTER                           :: c_val_p
     344              :       INTEGER                                            :: handle, i, i_val
     345       700125 :       INTEGER, DIMENSION(:), POINTER                     :: i_val_p
     346              :       LOGICAL                                            :: check, eol, l_val, quoted
     347       700125 :       LOGICAL, DIMENSION(:), POINTER                     :: l_val_p
     348              :       REAL(kind=dp)                                      :: r_val
     349       700125 :       REAL(kind=dp), DIMENSION(:), POINTER               :: r_val_p
     350              :       TYPE(cp_sll_char_type), POINTER                    :: c_first, c_last, c_new
     351              :       TYPE(cp_sll_int_type), POINTER                     :: i_first, i_last, i_new
     352              :       TYPE(cp_sll_logical_type), POINTER                 :: l_first, l_last, l_new
     353              :       TYPE(cp_sll_real_type), POINTER                    :: r_first, r_last, r_new
     354              : 
     355       700125 :       CALL timeset(routineN, handle)
     356              : 
     357       700125 :       CPASSERT(.NOT. ASSOCIATED(val))
     358       737793 :       SELECT CASE (type_of_var)
     359              :       CASE (no_t)
     360              :       CASE (logical_t)
     361        37668 :          NULLIFY (l_val_p)
     362        75336 :          IF (parser_test_next_token(parser) == "EOL") THEN
     363        18080 :             IF (.NOT. ASSOCIATED(default_value)) THEN
     364            0 :                IF (n_var < 1) THEN
     365            0 :                   ALLOCATE (l_val_p(0))
     366            0 :                   CALL val_create(val, l_vals_ptr=l_val_p)
     367              :                ELSE
     368              :                   CALL cp_abort(__LOCATION__, &
     369              :                                 "no value was given and there is no default value"// &
     370            0 :                                 TRIM(parser_location(parser)))
     371              :                END IF
     372              :             ELSE
     373        18080 :                CPASSERT(ASSOCIATED(default_value%l_val))
     374        18080 :                CALL val_create(val, l_vals=default_value%l_val)
     375              :             END IF
     376              :          ELSE
     377        19588 :             IF (n_var < 1) THEN
     378            0 :                NULLIFY (l_last, l_first)
     379            0 :                CALL parser_get_object(parser, l_val)
     380            0 :                CALL cp_create(l_first, l_val)
     381            0 :                l_last => l_first
     382            0 :                DO WHILE (parser_test_next_token(parser) /= "EOL")
     383            0 :                   CALL parser_get_object(parser, l_val)
     384            0 :                   CALL cp_create(l_new, l_val)
     385            0 :                   l_last%rest => l_new
     386            0 :                   l_last => l_new
     387              :                END DO
     388            0 :                l_val_p => cp_to_array(l_first)
     389            0 :                CALL cp_dealloc(l_first)
     390              :             ELSE
     391        58764 :                ALLOCATE (l_val_p(n_var))
     392        39176 :                DO i = 1, n_var
     393        39176 :                   CALL parser_get_object(parser, l_val_p(i))
     394              :                END DO
     395              :             END IF
     396        57256 :             IF (ASSOCIATED(l_val_p)) THEN
     397        19588 :                CALL val_create(val, l_vals_ptr=l_val_p)
     398              :             END IF
     399              :          END IF
     400              :       CASE (integer_t)
     401        52794 :          NULLIFY (i_val_p)
     402       105588 :          IF (parser_test_next_token(parser) == "EOL") THEN
     403           14 :             IF (.NOT. ASSOCIATED(default_value)) THEN
     404            0 :                IF (n_var < 1) THEN
     405            0 :                   ALLOCATE (i_val_p(0))
     406            0 :                   CALL val_create(val, i_vals_ptr=i_val_p)
     407              :                ELSE
     408              :                   CALL cp_abort(__LOCATION__, &
     409              :                                 "no value was given and there is no default value"// &
     410            0 :                                 TRIM(parser_location(parser)))
     411              :                END IF
     412              :             ELSE
     413           14 :                check = ASSOCIATED(default_value%i_val)
     414           14 :                CPASSERT(check)
     415           14 :                CALL val_create(val, i_vals=default_value%i_val)
     416              :             END IF
     417              :          ELSE
     418        52780 :             IF (n_var < 1) THEN
     419         8269 :                NULLIFY (i_last, i_first)
     420         8269 :                CALL parser_get_object(parser, i_val)
     421         8269 :                CALL cp_create(i_first, i_val)
     422         8269 :                i_last => i_first
     423        30791 :                DO WHILE (parser_test_next_token(parser) /= "EOL")
     424        22522 :                   CALL parser_get_object(parser, i_val)
     425        22522 :                   CALL cp_create(i_new, i_val)
     426        22522 :                   i_last%rest => i_new
     427        22522 :                   i_last => i_new
     428              :                END DO
     429         8269 :                i_val_p => cp_to_array(i_first)
     430         8269 :                CALL cp_dealloc(i_first)
     431              :             ELSE
     432       133533 :                ALLOCATE (i_val_p(n_var))
     433        93994 :                DO i = 1, n_var
     434        93994 :                   CALL parser_get_object(parser, i_val_p(i))
     435              :                END DO
     436              :             END IF
     437       105574 :             IF (ASSOCIATED(i_val_p)) THEN
     438        52780 :                CALL val_create(val, i_vals_ptr=i_val_p)
     439              :             END IF
     440              :          END IF
     441              :       CASE (real_t)
     442       160279 :          NULLIFY (r_val_p)
     443       320558 :          IF (parser_test_next_token(parser) == "EOL") THEN
     444            2 :             IF (.NOT. ASSOCIATED(default_value)) THEN
     445            2 :                IF (n_var < 1) THEN
     446            2 :                   ALLOCATE (r_val_p(0))
     447            2 :                   CALL val_create(val, r_vals_ptr=r_val_p)
     448              :                ELSE
     449              :                   CALL cp_abort(__LOCATION__, &
     450              :                                 "no value was given and there is no default value"// &
     451            0 :                                 TRIM(parser_location(parser)))
     452              :                END IF
     453              :             ELSE
     454            0 :                CPASSERT(ASSOCIATED(default_value%r_val))
     455            0 :                CALL val_create(val, r_vals=default_value%r_val)
     456              :             END IF
     457              :          ELSE
     458       160277 :             IF (n_var < 1) THEN
     459        16829 :                NULLIFY (r_last, r_first)
     460        16829 :                c_val = ""
     461        16829 :                CALL get_r_val(r_val, parser, unit, default_units, c_val)
     462        16829 :                CALL cp_create(r_first, r_val)
     463        16829 :                r_last => r_first
     464       333815 :                DO WHILE (parser_test_next_token(parser) /= "EOL")
     465       316986 :                   CALL get_r_val(r_val, parser, unit, default_units, c_val)
     466       316986 :                   CALL cp_create(r_new, r_val)
     467       316986 :                   r_last%rest => r_new
     468       316986 :                   r_last => r_new
     469              :                END DO
     470        16829 :                NULLIFY (r_last)
     471        16829 :                r_val_p => cp_to_array(r_first)
     472        16829 :                CALL cp_dealloc(r_first)
     473              :             ELSE
     474       430344 :                ALLOCATE (r_val_p(n_var))
     475       143448 :                c_val = ""
     476       373304 :                DO i = 1, n_var
     477       373304 :                   CALL get_r_val(r_val_p(i), parser, unit, default_units, c_val)
     478              :                END DO
     479              :             END IF
     480       320556 :             IF (ASSOCIATED(r_val_p)) THEN
     481       160277 :                CALL val_create(val, r_vals_ptr=r_val_p)
     482              :             END IF
     483              :          END IF
     484              :       CASE (char_t)
     485        76287 :          NULLIFY (c_val_p)
     486       152574 :          IF (parser_test_next_token(parser) == "EOL") THEN
     487          206 :             IF (n_var < 1) THEN
     488            2 :                ALLOCATE (c_val_p(1))
     489            2 :                c_val_p(1) = ' '
     490            2 :                CALL val_create(val, c_vals_ptr=c_val_p)
     491              :             ELSE
     492          204 :                IF (.NOT. ASSOCIATED(default_value)) THEN
     493              :                   CALL cp_abort(__LOCATION__, &
     494              :                                 "no value was given and there is no default value"// &
     495            0 :                                 TRIM(parser_location(parser)))
     496              :                ELSE
     497          204 :                   CPASSERT(ASSOCIATED(default_value%c_val))
     498          204 :                   CALL val_create(val, c_vals=default_value%c_val)
     499              :                END IF
     500              :             END IF
     501              :          ELSE
     502        76081 :             IF (n_var < 1) THEN
     503        29046 :                CPASSERT(n_var == -1)
     504        29046 :                NULLIFY (c_last, c_first)
     505        29046 :                CALL parser_get_object(parser, c_val)
     506        29046 :                CALL cp_create(c_first, c_val)
     507        29046 :                c_last => c_first
     508        37026 :                DO WHILE (parser_test_next_token(parser) /= "EOL")
     509         7980 :                   CALL parser_get_object(parser, c_val)
     510         7980 :                   CALL cp_create(c_new, c_val)
     511         7980 :                   c_last%rest => c_new
     512         7980 :                   c_last => c_new
     513              :                END DO
     514        29046 :                c_val_p => cp_to_array(c_first)
     515        29046 :                CALL cp_dealloc(c_first)
     516              :             ELSE
     517       141105 :                ALLOCATE (c_val_p(n_var))
     518       110216 :                DO i = 1, n_var
     519       110216 :                   CALL parser_get_object(parser, c_val_p(i))
     520              :                END DO
     521              :             END IF
     522       152368 :             IF (ASSOCIATED(c_val_p)) THEN
     523        76081 :                CALL val_create(val, c_vals_ptr=c_val_p)
     524              :             END IF
     525              :          END IF
     526              :       CASE (lchar_t)
     527       266593 :          IF (ASSOCIATED(default_value)) &
     528              :             CALL cp_abort(__LOCATION__, &
     529              :                           "input variables of type lchar_t cannot have a lone keyword attribute,"// &
     530              :                           " no value is interpreted as empty string"// &
     531            0 :                           TRIM(parser_location(parser)))
     532       266593 :          IF (n_var /= 1) &
     533              :             CALL cp_abort(__LOCATION__, &
     534              :                           "input variables of type lchar_t cannot be repeated,"// &
     535              :                           " one always represent a whole line, till the end"// &
     536            0 :                           TRIM(parser_location(parser)))
     537       266593 :          IF (parser_test_next_token(parser) == "EOL") THEN
     538           74 :             ALLOCATE (c_val_p(1))
     539           74 :             c_val_p(1) = ' '
     540              :          ELSE
     541       266519 :             NULLIFY (c_last, c_first)
     542       266519 :             CALL parser_get_object(parser, c_val, string_length=LEN(c_val))
     543       266519 :             IF (c_val(1:1) == parser%quote_character) THEN
     544           10 :                quoted = .TRUE.
     545           10 :                c_val(1:) = c_val(2:) ! Drop first quotation mark
     546           10 :                i = INDEX(c_val, parser%quote_character) ! Check for second quotation mark
     547           10 :                IF (i > 0) THEN
     548            0 :                   c_val(i:) = "" ! Discard stuff after second quotation mark
     549              :                   eol = .TRUE. ! Enforce end of line
     550              :                ELSE
     551              :                   eol = .FALSE.
     552              :                END IF
     553              :             ELSE
     554              :                quoted = .FALSE.
     555              :                eol = .FALSE.
     556              :             END IF
     557       266519 :             CALL cp_create(c_first, c_val)
     558       266519 :             c_last => c_first
     559       288677 :             DO WHILE ((.NOT. eol) .AND. (parser_test_next_token(parser) /= "EOL"))
     560        22158 :                CALL parser_get_object(parser, c_val, string_length=LEN(c_val))
     561        22158 :                i = INDEX(c_val, parser%quote_character) ! Check for quotation mark
     562        22158 :                IF (i > 0) THEN
     563           10 :                   IF (quoted) THEN
     564           10 :                      c_val(i:) = "" ! Discard stuff after second quotation mark
     565              :                      eol = .TRUE. ! Enforce end of line
     566              :                   ELSE
     567              :                      CALL cp_abort(__LOCATION__, &
     568              :                                    "Quotation mark found which is not the first non-blank character. "// &
     569              :                                    "Possibly the first quotation mark is missing?"// &
     570            0 :                                    TRIM(parser_location(parser)))
     571              :                   END IF
     572              :                ELSE
     573              :                   eol = .FALSE.
     574              :                END IF
     575        22158 :                CALL cp_create(c_new, c_val)
     576        22158 :                c_last%rest => c_new
     577        22158 :                c_last => c_new
     578              :             END DO
     579       266519 :             c_val_p => cp_to_array(c_first)
     580       533112 :             CALL cp_dealloc(c_first)
     581              :          END IF
     582       266593 :          CPASSERT(ASSOCIATED(c_val_p))
     583       266593 :          CALL val_create(val, lc_vals_ptr=c_val_p)
     584              :       CASE (enum_t)
     585       106504 :          CPASSERT(ASSOCIATED(enum))
     586       106504 :          NULLIFY (i_val_p)
     587       213008 :          IF (parser_test_next_token(parser) == "EOL") THEN
     588        10738 :             IF (.NOT. ASSOCIATED(default_value)) THEN
     589            0 :                IF (n_var < 1) THEN
     590            0 :                   ALLOCATE (i_val_p(0))
     591            0 :                   CALL val_create(val, i_vals_ptr=i_val_p)
     592              :                ELSE
     593              :                   CALL cp_abort(__LOCATION__, &
     594              :                                 "no value was given and there is no default value"// &
     595            0 :                                 TRIM(parser_location(parser)))
     596              :                END IF
     597              :             ELSE
     598        10738 :                CPASSERT(ASSOCIATED(default_value%i_val))
     599              :                CALL val_create(val, i_vals=default_value%i_val, &
     600        10738 :                                enum=default_value%enum)
     601              :             END IF
     602              :          ELSE
     603        95766 :             IF (n_var < 1) THEN
     604           72 :                NULLIFY (i_last, i_first)
     605           72 :                CALL parser_get_object(parser, c_val)
     606           72 :                CALL cp_create(i_first, enum_c2i(enum, c_val))
     607           72 :                i_last => i_first
     608           80 :                DO WHILE (parser_test_next_token(parser) /= "EOL")
     609            8 :                   CALL parser_get_object(parser, c_val)
     610            8 :                   CALL cp_create(i_new, enum_c2i(enum, c_val))
     611            8 :                   i_last%rest => i_new
     612            8 :                   i_last => i_new
     613              :                END DO
     614           72 :                i_val_p => cp_to_array(i_first)
     615           72 :                CALL cp_dealloc(i_first)
     616              :             ELSE
     617       287082 :                ALLOCATE (i_val_p(n_var))
     618       191388 :                DO i = 1, n_var
     619        95694 :                   CALL parser_get_object(parser, c_val)
     620       191388 :                   i_val_p(i) = enum_c2i(enum, c_val)
     621              :                END DO
     622              :             END IF
     623       202270 :             IF (ASSOCIATED(i_val_p)) THEN
     624        95766 :                CALL val_create(val, i_vals_ptr=i_val_p, enum=enum)
     625              :             END IF
     626              :          END IF
     627              :       CASE default
     628              :          CALL cp_abort(__LOCATION__, &
     629       700125 :                        "type "//cp_to_string(type_of_var)//"unknown to the parser")
     630              :       END SELECT
     631       700125 :       IF (parser_test_next_token(parser) /= "EOL") THEN
     632            0 :          location = TRIM(parser_location(parser))
     633            0 :          CALL parser_get_object(parser, info)
     634              :          CALL cp_abort(__LOCATION__, &
     635       700125 :                        "found unexpected extra argument "//TRIM(info)//" at "//location)
     636              :       END IF
     637              : 
     638       700125 :       CALL timestop(handle)
     639              : 
     640       700125 :    END SUBROUTINE val_create_parsing
     641              : 
     642              : ! **************************************************************************************************
     643              : !> \brief Reads and convert a real number from the input file
     644              : !> \param r_val ...
     645              : !> \param parser the parser from where the values should be read
     646              : !> \param unit ...
     647              : !> \param default_units ...
     648              : !> \param c_val ...
     649              : !> \author Teodoro Laino - 11.2007 [tlaino] - University of Zurich
     650              : ! **************************************************************************************************
     651      1127342 :    SUBROUTINE get_r_val(r_val, parser, unit, default_units, c_val)
     652              :       REAL(kind=dp), INTENT(OUT)                         :: r_val
     653              :       TYPE(cp_parser_type), INTENT(INOUT)                :: parser
     654              :       TYPE(cp_unit_type), POINTER                        :: unit
     655              :       TYPE(cp_unit_set_type), INTENT(IN)                 :: default_units
     656              :       CHARACTER(len=default_string_length), &
     657              :          INTENT(INOUT)                                   :: c_val
     658              : 
     659              :       TYPE(cp_unit_type), POINTER                        :: my_unit
     660              : 
     661       563671 :       NULLIFY (my_unit)
     662       563671 :       IF (ASSOCIATED(unit)) THEN
     663       106639 :          IF ('STR' == parser_test_next_token(parser)) THEN
     664        12460 :             CALL parser_get_object(parser, c_val)
     665        12460 :             IF (c_val(1:1) /= "[" .OR. c_val(LEN_TRIM(c_val):LEN_TRIM(c_val)) /= "]") THEN
     666              :                CALL cp_abort(__LOCATION__, &
     667              :                              "Invalid unit specifier or function found when parsing a number: "// &
     668            0 :                              c_val)
     669              :             END IF
     670       311500 :             ALLOCATE (my_unit)
     671        12460 :             CALL cp_unit_create(my_unit, c_val(2:LEN_TRIM(c_val) - 1))
     672              :          ELSE
     673       200818 :             IF (c_val /= "") THEN
     674        86300 :                ALLOCATE (my_unit)
     675         3452 :                CALL cp_unit_create(my_unit, c_val(2:LEN_TRIM(c_val) - 1))
     676              :             ELSE
     677        90727 :                my_unit => unit
     678              :             END IF
     679              :          END IF
     680       106639 :          IF (.NOT. cp_unit_compatible(unit, my_unit)) &
     681              :             CALL cp_abort(__LOCATION__, &
     682              :                           "Incompatible units. Defined as ("// &
     683              :                           TRIM(cp_unit_desc(unit))//") specified in input as ("// &
     684            0 :                           TRIM(cp_unit_desc(my_unit))//"). These units are incompatible!")
     685              :       END IF
     686       563671 :       CALL parser_get_object(parser, r_val)
     687       563671 :       IF (ASSOCIATED(unit)) THEN
     688       106639 :          r_val = cp_unit_to_cp2k1(r_val, my_unit, default_units)
     689       106639 :          IF (.NOT. (ASSOCIATED(my_unit, unit))) THEN
     690        15912 :             CALL cp_unit_release(my_unit)
     691        15912 :             DEALLOCATE (my_unit)
     692              :          END IF
     693              :       END IF
     694              : 
     695       563671 :    END SUBROUTINE get_r_val
     696              : 
     697              : END MODULE input_parsing
        

Generated by: LCOV version 2.0-1