LCOV - code coverage report
Current view: top level - src - input_optimize_basis.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:ccc2433) Lines: 129 129 100.0 %
Date: 2024-04-25 07:09:54 Functions: 6 6 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 builds the input structure for optimize_basis
      10             : !> \par History
      11             : !>      03.2012 created [Florian Schiffmann]
      12             : !> \author Florian Schiffmann
      13             : ! **************************************************************************************************
      14             : MODULE input_optimize_basis
      15             : 
      16             :    USE input_constants,                 ONLY: do_opt_all,&
      17             :                                               do_opt_coeff,&
      18             :                                               do_opt_exps,&
      19             :                                               do_opt_none
      20             :    USE input_keyword_types,             ONLY: keyword_create,&
      21             :                                               keyword_release,&
      22             :                                               keyword_type
      23             :    USE input_section_types,             ONLY: section_add_keyword,&
      24             :                                               section_add_subsection,&
      25             :                                               section_create,&
      26             :                                               section_release,&
      27             :                                               section_type
      28             :    USE input_val_types,                 ONLY: char_t,&
      29             :                                               integer_t,&
      30             :                                               real_t
      31             :    USE kinds,                           ONLY: dp
      32             :    USE string_utilities,                ONLY: s2a
      33             : #include "./base/base_uses.f90"
      34             : 
      35             :    IMPLICIT NONE
      36             :    PRIVATE
      37             : 
      38             :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'input_optimize_basis'
      39             :    PUBLIC :: create_optimize_basis_section
      40             : 
      41             : CONTAINS
      42             : 
      43             : ! **************************************************************************************************
      44             : !> \brief creates the optimize_basis section
      45             : !> \param section ...
      46             : !> \author Florian Schiffmann
      47             : ! **************************************************************************************************
      48        8392 :    SUBROUTINE create_optimize_basis_section(section)
      49             :       TYPE(section_type), POINTER                        :: section
      50             : 
      51             :       TYPE(keyword_type), POINTER                        :: keyword
      52             :       TYPE(section_type), POINTER                        :: subsection
      53             : 
      54        8392 :       CPASSERT(.NOT. ASSOCIATED(section))
      55             :       CALL section_create(section, __LOCATION__, name="OPTIMIZE_BASIS", &
      56             :                           description="describes a basis optimization job, in which an ADMM like approach is used to"// &
      57             :                           " find the best exponents and/or coefficients to match a given training set.", &
      58        8392 :                           repeats=.FALSE.)
      59        8392 :       NULLIFY (keyword, subsection)
      60             : 
      61             :       CALL keyword_create(keyword, __LOCATION__, name="BASIS_TEMPLATE_FILE", &
      62             :                           description="Name of the basis set file, containing the structure of the new basis set", &
      63             :                           usage="BASIS_TEMPLATE_FILE <FILENAME>", &
      64             :                           type_of_var=char_t, repeats=.FALSE., &
      65        8392 :                           default_c_val="BASIS_SET", n_var=-1)
      66        8392 :       CALL section_add_keyword(section, keyword)
      67        8392 :       CALL keyword_release(keyword)
      68             : 
      69             :       CALL keyword_create(keyword, __LOCATION__, name="BASIS_WORK_FILE", &
      70             :                           description="Name of the basis set file which is created to be read as initial guess", &
      71             :                           usage="BASIS_WORK_FILE <FILENAME>", &
      72             :                           type_of_var=char_t, repeats=.FALSE., &
      73        8392 :                           default_c_val="BASIS_WORK_FILE", n_var=-1)
      74        8392 :       CALL section_add_keyword(section, keyword)
      75        8392 :       CALL keyword_release(keyword)
      76             : 
      77             :       CALL keyword_create(keyword, __LOCATION__, name="BASIS_OUTPUT_FILE", &
      78             :                           description="Name of the basis set file containing the optimized basis", &
      79             :                           usage="BASIS_OUTPUT_FILE <FILENAME>", &
      80             :                           type_of_var=char_t, repeats=.FALSE., &
      81        8392 :                           default_c_val="BASIS_OUTPUT_FILE", n_var=-1)
      82        8392 :       CALL section_add_keyword(section, keyword)
      83        8392 :       CALL keyword_release(keyword)
      84             : 
      85             :       CALL keyword_create(keyword, __LOCATION__, name="WRITE_FREQUENCY", &
      86             :                           description="Frequency at which the intermediate results should be written", &
      87             :                           usage="WRITE_FREQUENCY 1000", &
      88        8392 :                           default_i_val=5000)
      89        8392 :       CALL section_add_keyword(section, keyword)
      90        8392 :       CALL keyword_release(keyword)
      91             : 
      92             :       CALL keyword_create(keyword, __LOCATION__, name="USE_CONDITION_NUMBER", &
      93             :                           description="Determines whether condition number should be part of optimization or not", &
      94             :                           usage="USE_CONDITION_NUMBER", &
      95        8392 :                           default_l_val=.FALSE., lone_keyword_l_val=.TRUE.)
      96        8392 :       CALL section_add_keyword(section, keyword)
      97        8392 :       CALL keyword_release(keyword)
      98             : 
      99             :       CALL keyword_create( &
     100             :          keyword, __LOCATION__, name="BASIS_COMBINATIONS", &
     101             :          description="If multiple atomic kinds are fitted at the same time, this keyword "// &
     102             :          "allows to specify which basis sets should be used together in optimization (underived set ID=0). "// &
     103             :          "If skipped all combinations are used. The order is taken as the kinds and sets are specified in the input", &
     104             :          repeats=.TRUE., &
     105        8392 :          usage="BASIS_COMBINATIONS SET_ID(KIND1) SET_ID(KIND2) ... ", type_of_var=integer_t, n_var=-1)
     106        8392 :       CALL section_add_keyword(section, keyword)
     107        8392 :       CALL keyword_release(keyword)
     108             : 
     109             :       CALL keyword_create( &
     110             :          keyword, __LOCATION__, name="RESIDUUM_WEIGHT", &
     111             :          description="This keyword allows to give different weight factors to the "// &
     112             :          "residuum of the different basis combinations. "// &
     113             :          "The first entry corresponds to the original basis sets. Every further value is assigned to the combinations "// &
     114             :          "in the order given for BASIS_COMBINATIONS.", &
     115             :          repeats=.TRUE., &
     116        8392 :          usage="RESIDUUM_WEIGHT REAL ", default_r_val=1.0_dp)
     117        8392 :       CALL section_add_keyword(section, keyword)
     118        8392 :       CALL keyword_release(keyword)
     119             : 
     120             :       CALL keyword_create( &
     121             :          keyword, __LOCATION__, name="CONDITION_WEIGHT", &
     122             :          description="This keyword allows to give different weight factors to the "// &
     123             :          "condition number of different basis combinations (LOG(cond) is used). "// &
     124             :          "The first entry corresponds to the original basis sets. Every further value is assigned to the combinations "// &
     125             :          "in the order given for BASIS_COMBINATIONS.", &
     126             :          repeats=.TRUE., &
     127        8392 :          usage="CONTITION_WEIGHT REAL ", default_r_val=1.0_dp)
     128        8392 :       CALL section_add_keyword(section, keyword)
     129        8392 :       CALL keyword_release(keyword)
     130             : 
     131             :       CALL keyword_create(keyword, __LOCATION__, name="GROUP_PARTITION", &
     132             :                           description="Allows the specification of the group mpi group sizes in parallel "// &
     133             :                           "runs. If less Groups than tasks are speciefied, consecutive calculations "// &
     134             :                           "Will be assigned to one group (derived basis sets and then training sets) "// &
     135             :                           "If keyword is skipped, equal group sizes will be generated trying to fit all calculations.", &
     136             :                           repeats=.TRUE., &
     137        8392 :                           usage="GROUP_PARTITION INT INT ... ", type_of_var=integer_t, n_var=-1)
     138        8392 :       CALL section_add_keyword(section, keyword)
     139        8392 :       CALL keyword_release(keyword)
     140             : 
     141        8392 :       CALL create_fit_kinds_section(subsection)
     142        8392 :       CALL section_add_subsection(section, subsection)
     143        8392 :       CALL section_release(subsection)
     144             : 
     145        8392 :       CALL create_training_section(subsection)
     146        8392 :       CALL section_add_subsection(section, subsection)
     147        8392 :       CALL section_release(subsection)
     148             : 
     149        8392 :       CALL create_powell_section(subsection)
     150        8392 :       CALL section_add_subsection(section, subsection)
     151        8392 :       CALL section_release(subsection)
     152             : 
     153        8392 :    END SUBROUTINE create_optimize_basis_section
     154             : 
     155             : ! **************************************************************************************************
     156             : !> \brief ...
     157             : !> \param section ...
     158             : ! **************************************************************************************************
     159        8392 :    SUBROUTINE create_fit_kinds_section(section)
     160             :       TYPE(section_type), POINTER                        :: section
     161             : 
     162             :       TYPE(keyword_type), POINTER                        :: keyword
     163             :       TYPE(section_type), POINTER                        :: subsection
     164             : 
     165        8392 :       NULLIFY (keyword, subsection)
     166        8392 :       CPASSERT(.NOT. ASSOCIATED(section))
     167             :       CALL section_create(section, __LOCATION__, name="FIT_KIND", &
     168             :                           description="specicifies the atomic kinds to be fitted and the basis"// &
     169             :                           " sets associated with the kind.", &
     170        8392 :                           repeats=.TRUE.)
     171             : 
     172             :       CALL keyword_create(keyword, __LOCATION__, name="_SECTION_PARAMETERS_", &
     173             :                           description="The name of the kind described in this section.", &
     174        8392 :                           usage="H", default_c_val="DEFAULT")
     175        8392 :       CALL section_add_keyword(section, keyword)
     176        8392 :       CALL keyword_release(keyword)
     177             : 
     178             :       CALL keyword_create(keyword, __LOCATION__, name="BASIS_SET", &
     179             :                           description="The name of the basis set for the kind. Has to be specified in BASIS_TEMPLATE_FILE.", &
     180        8392 :                           usage="H", default_c_val="DEFAULT")
     181        8392 :       CALL section_add_keyword(section, keyword)
     182        8392 :       CALL keyword_release(keyword)
     183             : 
     184             :       CALL keyword_create(keyword, __LOCATION__, name="INITIAL_DEGREES_OF_FREEDOM", &
     185             :                           description="Specifies the initial degrees of freedom in the basis optimization. "// &
     186             :                           "This can be used to make further specifications easier", &
     187             :                           usage="INITIAL_DEGREES_OF_FREEDOM ALL", &
     188             :                           enum_c_vals=s2a("ALL", "NONE", "COEFFICIENTS", "EXPONENTS"), &
     189             :                           enum_desc=s2a("Set all parameters in the basis to be variable.", &
     190             :                                         "Set all parameters in the basis to be fixed.", &
     191             :                                         "Set all coefficients in the basis set to be variable.", &
     192             :                                         "Set all exponents in the basis to be variable."), &
     193             :                           enum_i_vals=(/do_opt_all, do_opt_none, do_opt_coeff, do_opt_exps/), &
     194        8392 :                           default_i_val=do_opt_coeff)
     195        8392 :       CALL section_add_keyword(section, keyword)
     196        8392 :       CALL keyword_release(keyword)
     197             : 
     198             :       CALL keyword_create(keyword, __LOCATION__, name="SWITCH_COEFF_STATE", &
     199             :                           description="Allows to switch the state of a given coefficient from current state "// &
     200             :                           "(varibale/fixed)) to the opposite state. The three integers indicate "// &
     201             :                           "the set number, the angular momentum i'th contraction and i'th coefficient", repeats=.TRUE., &
     202        8392 :                           usage="SWITCH_COEFF_STATE SET L CONTRACTION IPGF", type_of_var=integer_t, n_var=4)
     203        8392 :       CALL section_add_keyword(section, keyword)
     204        8392 :       CALL keyword_release(keyword)
     205             : 
     206             :       CALL keyword_create(keyword, __LOCATION__, name="SWITCH_CONTRACTION_STATE", &
     207             :                           description="Allows to switch the state of a given contraction from current state "// &
     208             :                           "(varibale/fixed)) to the opposite state. The three integers indicate "// &
     209             :                           "the set number, the angular momentum and i'th contraction ", repeats=.TRUE., &
     210        8392 :                           usage="SWITCH_CONTRACTION_STATE SET L CONTRACTION ", type_of_var=integer_t, n_var=3)
     211        8392 :       CALL section_add_keyword(section, keyword)
     212        8392 :       CALL keyword_release(keyword)
     213             : 
     214             :       CALL keyword_create(keyword, __LOCATION__, name="SWITCH_EXP_STATE", &
     215             :                           description="Allows to switch the state of a given exponent from current state "// &
     216             :                           "(varibale/fixed)) to the opposite state. The two integers indicate "// &
     217             :                           "the set number and i'th exponent", repeats=.TRUE., &
     218        8392 :                           usage="SWITCH_EXP_STATE SET IEXP", type_of_var=integer_t, n_var=2)
     219        8392 :       CALL section_add_keyword(section, keyword)
     220        8392 :       CALL keyword_release(keyword)
     221             : 
     222             :       CALL keyword_create(keyword, __LOCATION__, name="SWITCH_SET_STATE", &
     223             :                           description="Allows to switch the states of in a set from current state "// &
     224             :                           "(varibale/fixed)) to the opposite state. The two integers indicate "// &
     225             :                           "the affected part (0=ALL,1=EXPS,2=COEFF) and i'th set", repeats=.TRUE., &
     226        8392 :                           usage="SWITCH_SET_STATE SET IEXP", type_of_var=integer_t, n_var=2)
     227        8392 :       CALL section_add_keyword(section, keyword)
     228        8392 :       CALL keyword_release(keyword)
     229             : 
     230        8392 :       CALL create_constrain_exp_section(subsection)
     231        8392 :       CALL section_add_subsection(section, subsection)
     232        8392 :       CALL section_release(subsection)
     233             : 
     234        8392 :       CALL create_derived_sets_section(subsection)
     235        8392 :       CALL section_add_subsection(section, subsection)
     236        8392 :       CALL section_release(subsection)
     237             : 
     238        8392 :    END SUBROUTINE create_fit_kinds_section
     239             : 
     240             : ! **************************************************************************************************
     241             : !> \brief ...
     242             : !> \param section ...
     243             : ! **************************************************************************************************
     244        8392 :    SUBROUTINE create_derived_sets_section(section)
     245             :       TYPE(section_type), POINTER                        :: section
     246             : 
     247             :       TYPE(keyword_type), POINTER                        :: keyword
     248             : 
     249        8392 :       NULLIFY (keyword)
     250        8392 :       CPASSERT(.NOT. ASSOCIATED(section))
     251             :       CALL section_create(section, __LOCATION__, name="DERIVED_BASIS_SETS", &
     252             :                           description="This section can be used to create subsets of a basis"// &
     253             :                           " which will be fitted at the same time. This is especially useful if connected"// &
     254             :                           " bsis sets e.g. TZVP, DZVP, SZV should be fitted.", &
     255        8392 :                           repeats=.TRUE.)
     256             : 
     257             :       CALL keyword_create(keyword, __LOCATION__, name="BASIS_SET_NAME", &
     258             :                           description="Defines the name of the derived basis set, which will be "// &
     259             :                           "automatically generated otherwise.", &
     260             :                           usage="BASIS_SET_NAME {word}", &
     261             :                           type_of_var=char_t, &
     262             :                           repeats=.FALSE., &
     263        8392 :                           default_c_val="")
     264        8392 :       CALL section_add_keyword(section, keyword)
     265        8392 :       CALL keyword_release(keyword)
     266             : 
     267             :       CALL keyword_create(keyword, __LOCATION__, name="REFERENCE_SET", &
     268             :                           description="Specifies the reference basis ID which is used as template to create the new set. "// &
     269             :                           "The original basis has ID 0. All following sets are counted in order as specified in the Input."// &
     270             :                           " The descriptors always assume the structure of the input basis set.", &
     271        8392 :                           repeats=.FALSE., usage="REFERNCE_SET INTEGER", default_i_val=0)
     272        8392 :       CALL section_add_keyword(section, keyword)
     273        8392 :       CALL keyword_release(keyword)
     274             : 
     275             :       CALL keyword_create(keyword, __LOCATION__, name="REMOVE_CONTRACTION", &
     276             :                           description="Can be used to remove a contraction from the reference basis set. "// &
     277             :                           "The contraction is speciefied by set number, angular momentum and number of contraction."// &
     278             :                           " The descriptors always assume the structure of the input basis set.", &
     279        8392 :                           repeats=.TRUE., usage="REMOVE_CONTRACTION SET L ICONTRACTION", type_of_var=integer_t, n_var=3)
     280        8392 :       CALL section_add_keyword(section, keyword)
     281        8392 :       CALL keyword_release(keyword)
     282             : 
     283             :       CALL keyword_create(keyword, __LOCATION__, name="REMOVE_SET", &
     284             :                           description="Can be used to remove a set from the reference basis set. ", &
     285        8392 :                           repeats=.TRUE., usage="REMOVE_SET SET", type_of_var=integer_t, n_var=1)
     286        8392 :       CALL section_add_keyword(section, keyword)
     287        8392 :       CALL keyword_release(keyword)
     288             : 
     289        8392 :    END SUBROUTINE create_derived_sets_section
     290             : 
     291             : ! **************************************************************************************************
     292             : !> \brief ...
     293             : !> \param section ...
     294             : ! **************************************************************************************************
     295        8392 :    SUBROUTINE create_constrain_exp_section(section)
     296             :       TYPE(section_type), POINTER                        :: section
     297             : 
     298             :       TYPE(keyword_type), POINTER                        :: keyword
     299             : 
     300        8392 :       NULLIFY (keyword)
     301        8392 :       CPASSERT(.NOT. ASSOCIATED(section))
     302             :       CALL section_create(section, __LOCATION__, name="CONSTRAIN_EXPONENTS", &
     303             :                           description="specicifies constraints for the exponents to be fitted."// &
     304             :                           " Only a single constraint can be applied to an exponent", &
     305        8392 :                           repeats=.TRUE.)
     306             : 
     307             :       CALL keyword_create(keyword, __LOCATION__, name="USE_EXP", &
     308             :                           description="Defines the exponent to be constraint. The two integers indicate "// &
     309             :                           "the set number and i'th exponent. The value -1 can be used to mark all sets/exponents in a set.", &
     310        8392 :                           repeats=.FALSE., usage="USE_EXP SET IEXP", type_of_var=integer_t, n_var=2)
     311        8392 :       CALL section_add_keyword(section, keyword)
     312        8392 :       CALL keyword_release(keyword)
     313             : 
     314             :       CALL keyword_create(keyword, __LOCATION__, name="BOUNDARIES", &
     315             :                           description="Defines the boundaries to which the optimization is restricted."// &
     316             :                           " First value is the lower bound, second value is the upper bound.", &
     317        8392 :                           repeats=.FALSE., usage="BOUNDARIES LOWER UPPER", type_of_var=real_t, n_var=2)
     318        8392 :       CALL section_add_keyword(section, keyword)
     319        8392 :       CALL keyword_release(keyword)
     320             : 
     321             :       CALL keyword_create(keyword, __LOCATION__, name="MAX_VAR_FRACTION", &
     322             :                           description="Defines the maximum fractionr by which the exponent is allowed to vary."// &
     323             :                           " e.g. 0.5 allows the exp to vary by 0.5*exp in both directions.", &
     324        8392 :                           repeats=.FALSE., usage="MAX_VAR_FRACTION REAL", type_of_var=real_t, n_var=1)
     325        8392 :       CALL section_add_keyword(section, keyword)
     326        8392 :       CALL keyword_release(keyword)
     327             : 
     328        8392 :    END SUBROUTINE create_constrain_exp_section
     329             : 
     330             : ! **************************************************************************************************
     331             : !> \brief ...
     332             : !> \param section ...
     333             : ! **************************************************************************************************
     334        8392 :    SUBROUTINE create_training_section(section)
     335             :       TYPE(section_type), POINTER                        :: section
     336             : 
     337             :       TYPE(keyword_type), POINTER                        :: keyword
     338             : 
     339        8392 :       NULLIFY (keyword)
     340        8392 :       CPASSERT(.NOT. ASSOCIATED(section))
     341             :       CALL section_create(section, __LOCATION__, name="TRAINING_FILES", &
     342             :                           description="specicifies the location in which the files necessary for"// &
     343             :                           " fitting procedure are located. Each Training set needs a repetition of this section.", &
     344        8392 :                           repeats=.TRUE.)
     345             : 
     346             :       CALL keyword_create(keyword, __LOCATION__, name="DIRECTORY", &
     347             :                           description="the directory in which the files are placed", &
     348             :                           usage="DIRECTORY /my/path", &
     349        8392 :                           default_lc_val=".")
     350        8392 :       CALL section_add_keyword(section, keyword)
     351        8392 :       CALL keyword_release(keyword)
     352             : 
     353             :       CALL keyword_create(keyword, __LOCATION__, name="INPUT_FILE_NAME", &
     354             :                           description="the filename of the input file used to run the original calculation", &
     355             :                           usage="INPUT_FILE_NAME my_input.inp", &
     356        8392 :                           default_lc_val="input.inp")
     357        8392 :       CALL section_add_keyword(section, keyword)
     358        8392 :       CALL keyword_release(keyword)
     359             : 
     360        8392 :    END SUBROUTINE create_training_section
     361             : 
     362             : ! **************************************************************************************************
     363             : !> \brief ...
     364             : !> \param section ...
     365             : ! **************************************************************************************************
     366        8392 :    SUBROUTINE create_powell_section(section)
     367             :       TYPE(section_type), POINTER                        :: section
     368             : 
     369             :       TYPE(keyword_type), POINTER                        :: keyword
     370             : 
     371        8392 :       NULLIFY (keyword)
     372        8392 :       CPASSERT(.NOT. ASSOCIATED(section))
     373             :       CALL section_create(section, __LOCATION__, name="OPTIMIZATION", &
     374             :                           description="sets the parameters for optimizition, output frequency and restarts", &
     375        8392 :                           repeats=.FALSE.)
     376             : 
     377             :       CALL keyword_create(keyword, __LOCATION__, name="ACCURACY", &
     378             :                           description="Final accuracy requested in optimization (RHOEND)", &
     379             :                           usage="ACCURACY 0.00001", &
     380        8392 :                           default_r_val=1.e-5_dp)
     381        8392 :       CALL section_add_keyword(section, keyword)
     382        8392 :       CALL keyword_release(keyword)
     383             : 
     384             :       CALL keyword_create(keyword, __LOCATION__, name="STEP_SIZE", &
     385             :                           description="Initial step size for search algorithm (RHOBEG)", &
     386             :                           usage="STEP_SIZE 0.005", &
     387        8392 :                           default_r_val=0.1_dp)
     388        8392 :       CALL section_add_keyword(section, keyword)
     389        8392 :       CALL keyword_release(keyword)
     390             : 
     391             :       CALL keyword_create(keyword, __LOCATION__, name="MAX_FUN", &
     392             :                           description="Maximum number of function evaluations", &
     393             :                           usage="MAX_FUN 1000", &
     394        8392 :                           default_i_val=5000)
     395        8392 :       CALL section_add_keyword(section, keyword)
     396        8392 :       CALL keyword_release(keyword)
     397             : 
     398        8392 :    END SUBROUTINE create_powell_section
     399             : 
     400             : END MODULE input_optimize_basis

Generated by: LCOV version 1.15