44 USE omp_lib,
ONLY: omp_get_max_threads,&
61 USE iso_fortran_env,
ONLY: compiler_options,&
65 #include "../base/base_uses.f90"
69 CHARACTER(LEN=default_path_length) :: input_file_name, output_file_name, &
71 CHARACTER(LEN=default_path_length), &
72 DIMENSION(:, :),
ALLOCATABLE :: initial_variables, initial_variables_tmp
73 CHARACTER(LEN=:),
ALLOCATABLE :: compiler_options_string
74 INTEGER :: output_unit, l, i, var_set_sep, inp_var_idx
75 INTEGER :: ierr, i_arg
76 LOGICAL :: check, usage, echo_input, command_line_error
77 LOGICAL :: run_it, force_run, has_input, xml, print_version, print_license, shell_mode
78 TYPE(section_type),
POINTER :: input_declaration
80 NULLIFY (input_declaration)
93 print_version = .false.
94 print_license = .false.
95 command_line_error = .false.
97 input_file_name =
"Missing input file name"
98 output_file_name =
"__STD_OUT__"
99 ALLOCATE (initial_variables(2, 1:0))
102 CALL get_command_argument(number=0,
VALUE=command, status=ierr)
104 l = len_trim(command)
106 IF (command(i:i) ==
"/" .OR. command(i:i) ==
"\")
EXIT
108 command = command(i + 1:l)
112 DO WHILE (i_arg < command_argument_count())
114 CALL get_command_argument(number=i_arg,
VALUE=arg_att, status=ierr)
116 SELECT CASE (arg_att)
118 IF (output_file_name ==
"__STD_OUT__")
THEN
121 CALL get_command_argument(number=i_arg,
VALUE=arg_att, status=ierr)
123 IF (arg_att(1:1) ==
"-")
THEN
124 WRITE (output_unit,
"(/,T2,A)") &
125 "ERROR: The output file name "//trim(arg_att)//
" starts with -"
126 command_line_error = .true.
128 output_file_name = arg_att
129 CALL open_file(file_name=output_file_name, &
130 file_status=
"UNKNOWN", &
131 file_action=
"WRITE", &
132 file_position=
"APPEND", &
133 skip_get_unit_number=.true., &
134 unit_number=output_unit)
138 WRITE (output_unit,
"(/,T2,A)") &
139 "ERROR: The command line flag -o has been specified multiple times"
140 command_line_error = .true.
146 IF (command(1:10) ==
"cp2k_shell")
THEN
149 ELSE IF (command_argument_count() < 1)
THEN
150 WRITE (output_unit,
"(/,T2,A)") &
151 "ERROR: At least one command line argument must be specified"
152 command_line_error = .true.
156 l = len_trim(command)
157 IF (command(l - 4:l) ==
".sopt" .OR. command(l - 4:l) ==
".popt")
THEN
158 CALL omp_set_num_threads(1)
162 IF (omp_get_max_threads() > 1)
THEN
164 CHARACTER(len=default_path_length) :: env_var
165 INTEGER :: veclib_max_threads, ierr
166 CALL get_environment_variable(
"VECLIB_MAXIMUM_THREADS", env_var, status=ierr)
167 veclib_max_threads = 0
169 READ (env_var, *) veclib_max_threads
170 IF (ierr == 1 .OR. (ierr == 0 .AND. veclib_max_threads > 1))
THEN
171 CALL cp_warn(__location__, &
172 "macOS' Accelerate framework has its own threading enabled which may interfere"// &
173 " with the OpenMP threading. You can disable the Accelerate threading by setting"// &
174 " the environment variable VECLIB_MAXIMUM_THREADS=1")
181 arg_loop:
DO WHILE (i_arg < command_argument_count())
183 CALL get_command_argument(i_arg, arg_att, status=ierr)
185 SELECT CASE (arg_att)
186 CASE (
"--check",
"-c")
190 CASE (
"--echo",
"-e")
194 CASE (
"-v",
"--version")
195 print_version = .true.
198 print_license = .true.
202 CASE (
"--shell",
"-s")
205 CASE (
"-help",
"--help",
"-h")
210 CALL get_command_argument(i_arg, arg_att, status=ierr)
213 IF (.NOT. arg_att(1:1) ==
"-")
THEN
214 input_file_name = arg_att
217 WRITE (output_unit,
"(/,T2,A)") &
218 "ERROR: The input file name "//trim(arg_att)//
" starts with -"
219 command_line_error = .true.
224 CALL get_command_argument(i_arg, arg_att, status=ierr)
227 var_set_sep = index(arg_att,
'=')
229 IF (var_set_sep < 2)
THEN
230 WRITE (output_unit,
"(/,T2,A)")
"ERROR: Invalid initializer for preprocessor variable: "//trim(arg_att)
231 command_line_error = .true.
235 DO inp_var_idx = 1,
SIZE(initial_variables, 2)
237 IF (trim(initial_variables(1, inp_var_idx)) == arg_att(:var_set_sep - 1)) &
241 IF (inp_var_idx >
SIZE(initial_variables, 2))
THEN
243 ALLOCATE (initial_variables_tmp(2,
SIZE(initial_variables, 2) + 1))
244 initial_variables_tmp(:, 1:
SIZE(initial_variables, 2)) = initial_variables
245 CALL move_alloc(initial_variables_tmp, initial_variables)
248 initial_variables(1, inp_var_idx) = arg_att(:var_set_sep - 1)
249 initial_variables(2, inp_var_idx) = arg_att(var_set_sep + 1:)
253 CALL get_command_argument(i_arg, arg_att, status=ierr)
255 IF (arg_att(1:1) ==
"-")
EXIT arg_loop
262 IF ((.NOT. has_input) .AND. &
263 (i_arg == command_argument_count()) .AND. &
264 (.NOT. arg_att(1:1) ==
"-"))
THEN
265 input_file_name = arg_att
267 ELSE IF (has_input .AND. &
268 (.NOT. arg_att(1:1) ==
"-"))
THEN
269 WRITE (output_unit,
"(/,T2,A)") &
270 "Error: Tried to specify two input files"
271 command_line_error = .true.
277 IF ((run_it .OR. force_run .OR. check .OR. echo_input) .AND. &
278 (.NOT. has_input) .AND. (.NOT. command_line_error))
THEN
279 WRITE (unit=output_unit, fmt=
"(/,T2,A)") &
280 "ERROR: An input file name is required"
281 command_line_error = .true.
284 CALL init_cp2k(init_mpi=.true., ierr=ierr)
289 IF (usage .OR. command_line_error)
THEN
290 IF (default_para_env%is_source())
THEN
291 l = len_trim(command)
292 WRITE (unit=output_unit, fmt=
"(/,(T2,A))") &
293 trim(command)//
" [-c|--check] [-e|--echo] [-h|--help]", &
294 repeat(
" ", l)//
" [-i] <input_file>", &
295 repeat(
" ", l)//
" [-mpi-mapping|--mpi-mapping] <method>", &
296 repeat(
" ", l)//
" [-o] <output_file>", &
297 repeat(
" ", l)//
" [-r|-run] [-s|--shell] [--xml]"
298 WRITE (unit=output_unit, fmt=
"(/,T2,A,/,/,T2,A,/,/,T2,A,/,/,(T3,A))") &
299 "starts the CP2K program, see <https://www.cp2k.org/>", &
300 "The easiest way is "//trim(command)//
" <input_file>", &
301 "The following options can be used:", &
302 "-i <input_file> : provides an input file name, if it is the last", &
303 " argument, the -i flag is not needed", &
304 "-o <output_file> : provides an output file name [default: screen]"
305 WRITE (unit=output_unit, fmt=
"(/,T2,A,/,/,(T3,A))") &
306 "These switches skip the simulation, unless [-r|-run] is specified:", &
307 "--check, -c : performs a syntax check of the <input_file>", &
308 "--echo, -e : echoes the <input_file>, and make all defaults explicit", &
309 " The input is also checked, but only a failure is reported", &
310 "--help, -h : writes this message", &
311 "--license : prints the CP2K license", &
312 "--mpi-mapping : applies a given MPI reordering to CP2K", &
313 "--run, -r : forces a CP2K run regardless of other specified flags", &
314 "--shell, -s : start interactive shell mode", &
315 "--version, -v : prints the CP2K version and the revision number", &
316 "--xml : dumps the whole CP2K input structure as a XML file", &
317 " xml2htm generates a HTML manual from this XML file", &
318 "--set, -E name=value : set the initial value of a preprocessor value", &
323 IF (.NOT. command_line_error)
THEN
326 IF (print_version)
THEN
327 IF (default_para_env%is_source())
THEN
328 WRITE (output_unit,
"(T2,A)") cp2k_version, &
329 "Source code revision "//trim(compile_revision), &
331 compiler_options_string = compiler_options()
332 WRITE (output_unit,
"(T2,A,A)")
"compiler: ", compiler_version()
333 WRITE (output_unit,
"(T2,A)")
"compiler options:"
334 DO i = 0, (len(compiler_options_string) - 1)/68
335 WRITE (output_unit,
"(T4,A)") &
336 compiler_options_string(i*68 + 1:min(len(compiler_options_string), (i + 1)*68))
338 DEALLOCATE (compiler_options_string)
343 IF (print_license)
THEN
344 IF (default_para_env%is_source())
THEN
345 CALL print_cp2k_license(output_unit)
350 IF (default_para_env%is_source())
THEN
351 CALL write_xml_file()
355 CALL create_cp2k_root_section(input_declaration)
358 CALL check_input(input_declaration, input_file_name, output_file_name, &
359 echo_input=echo_input, ierr=ierr, initial_variables=initial_variables)
360 IF (default_para_env%is_source())
THEN
362 IF (.NOT. echo_input)
THEN
363 WRITE (output_unit,
"(A)")
"SUCCESS, the input could be parsed correctly."
364 WRITE (output_unit,
"(A)")
" This does not guarantee that this input is meaningful"
365 WRITE (output_unit,
"(A)")
" or will run successfully"
368 WRITE (output_unit,
"(A)")
"ERROR, the input could *NOT* be parsed correctly."
369 WRITE (output_unit,
"(A)")
" Please, check and correct it"
375 CALL launch_cp2k_shell(input_declaration)
378 IF (run_it .OR. force_run)
THEN
379 CALL run_input(input_declaration, input_file_name, output_file_name, initial_variables)
382 CALL section_release(input_declaration)
385 WRITE (unit=output_unit, fmt=
"(/,A)")
"initial setup (MPI ?) error"
389 CALL finalize_cp2k(finalize_mpi=.true., ierr=ierr)
390 DEALLOCATE (initial_variables)
program cp2k
Main program of CP2K.
some minimal info about CP2K, including its version and license
subroutine, public print_cp2k_license(iunit)
...
character(len= *), parameter, public compile_revision
character(len=10 *default_string_length) function, public cp2k_flags()
list all compile time options that influence the capabilities of cp2k. All new flags should be added ...
character(len= *), parameter, public cp2k_version
subroutine, public run_input(input_declaration, input_file_path, output_file_path, initial_variables, mpi_comm)
runs the given input
subroutine, public write_xml_file()
...
Interactive shell of CP2K.
subroutine, public launch_cp2k_shell(input_declaration)
Launch the interactive CP2K shell.
Utility routines to open and close files. Tracking of preconnections.
subroutine, public open_file(file_name, file_status, file_form, file_action, file_position, file_pad, unit_number, debug, skip_get_unit_number, file_access)
Opens the requested file using a free unit number.
interface to use cp2k as library
type(mp_para_env_type), pointer, save, public default_para_env
subroutine, public init_cp2k(init_mpi, ierr)
initializes cp2k, needs to be called once before using any of the other functions when using cp2k as ...
subroutine, public finalize_cp2k(finalize_mpi, ierr)
cleanup after you have finished using this interface
subroutine, public check_input(input_declaration, input_file_path, output_file_path, echo_input, mpi_comm, initial_variables, ierr)
performs a check of the input
Defines the basic variable types.
integer, parameter, public default_path_length
Machine interface based on Fortran 2003 and POSIX.
integer, parameter, public default_output_unit