V3FIT
bmw_commandline_parser.f
Go to the documentation of this file.
1 !-------------------------------------------------------------------------------
2 ! The @header2, @begin_table, @item3 and @end_table commands are custom defined
3 ! commands in Doxygen.in. They are defined under ALIASES. For the page created
4 ! here, the 80 column limit is exceeded. Arguments of aliases are separated by
5 ! ','. If you intended ',' to be a string you must use an escaped comma '\,'.
6 !
46 !-------------------------------------------------------------------------------
47 !*******************************************************************************
50 !
51 ! Note separating the Doxygen comment block here so detailed decription is
52 ! found in the Module not the file.
53 !
55 !*******************************************************************************
56 
58  USE stel_kinds
59  USE file_opts, only: path_length
60  USE profiler
62 
63  IMPLICIT NONE
64 
65 !*******************************************************************************
66 ! bmw_commandline parser module parameters
67 !*******************************************************************************
69  INTEGER, PARAMETER, PRIVATE :: max_arg_length = 8
72  INTEGER, PARAMETER, PRIVATE :: max_length = path_length &
73  & + max_arg_length + 1
74 
75 ! Commandline parser error codes.
77  INTEGER, PARAMETER :: bmw_commandline_parser_no_error = 0
79  INTEGER, PARAMETER :: bmw_commandline_parser_arg_not_found = -1
80 
81 !*******************************************************************************
82 ! DERIVED-TYPE DECLARATIONS
83 ! 1) bmw_commandline parser base class
84 !
85 !*******************************************************************************
86 !-------------------------------------------------------------------------------
88 !-------------------------------------------------------------------------------
91  CHARACTER (len=max_length) :: command
93  CHARACTER (len=max_arg_length), DIMENSION(:), POINTER :: &
94  & arg => null()
97  CHARACTER (len=path_length), DIMENSION(:), POINTER :: &
98  & value => null()
99  END TYPE
100 
101  CONTAINS
102 !*******************************************************************************
103 ! CONSTRUCTION SUBROUTINES
104 !*******************************************************************************
105 !-------------------------------------------------------------------------------
116 !-------------------------------------------------------------------------------
117  FUNCTION bmw_commandline_parser_construct(parallel)
118 
119  IMPLICIT NONE
120 
121 ! Declare Arguments
122  TYPE (bmw_commandline_parser_class), POINTER :: &
124  TYPE (bmw_parallel_context_class), INTENT(in) :: parallel
125 
126 ! local variables
127  CHARACTER (len=max_length) :: temp
128  INTEGER :: num_args, i, value_index
129  REAL (rprec) :: start_time
130 
131 ! Start of executable code
132  start_time = profiler_get_start_time()
133 
134  temp = ''
135  num_args = 0
136  value_index = 0
138 
139 ! Read the zeroith arg to get the number of arguments. This should also be the
140 ! command name.
141  CALL getcarg(0, bmw_commandline_parser_construct%command, &
142  & num_args)
143 
144  IF (num_args .le. 0 .and. parallel%offset .eq. 0) THEN
146  END IF
147 
148 ! Allocate the arrays and
149  ALLOCATE(bmw_commandline_parser_construct%arg(num_args))
150  ALLOCATE(bmw_commandline_parser_construct%value(num_args))
151 
152 ! Loop through the command line arguments, and setup the argument and value
153 ! arrays
154  DO i = 1, num_args
155  CALL getcarg(i, temp, num_args)
156 
157 ! Check for a - as the first character.
158  IF (temp(1:1) .eq. '-') THEN
159  value_index = index(temp, '=')
160  IF (value_index .eq. 0) THEN
161 ! Check for help command.
162  IF (trim(temp) .eq. '-h' .and. &
163  & parallel%offset .eq. 0) THEN
165  END IF
166 
167  bmw_commandline_parser_construct%arg(i) = trim(temp)
169  ELSE
171  & temp(1:value_index - 1)
173  & temp(value_index + 1:len_trim(temp))
174  END IF
175  END IF
176 
179  END DO
180 
181  CALL profiler_set_stop_time('bmw_commandline_parser_construct', &
182  & start_time)
183 
184  END FUNCTION
185 
186 !*******************************************************************************
187 ! DESTRUCTION SUBROUTINES
188 !*******************************************************************************
189 !-------------------------------------------------------------------------------
196 !-------------------------------------------------------------------------------
197  SUBROUTINE bmw_commandline_parser_destruct(this)
198 
199  IMPLICIT NONE
200 
201 ! Declare Arguments
202  TYPE (bmw_commandline_parser_class), POINTER :: this
203 
204 ! Start of executable code
205  IF (ASSOCIATED(this%arg)) THEN
206  DEALLOCATE(this%arg)
207  this%arg => null()
208  END IF
209 
210  IF (ASSOCIATED(this%value)) THEN
211  DEALLOCATE(this%value)
212  this%value => null()
213  END IF
214 
215  DEALLOCATE(this)
216 
217  END SUBROUTINE
218 
219 !*******************************************************************************
220 ! GETTER SUBROUTINES
221 !*******************************************************************************
222 !-------------------------------------------------------------------------------
232 !-------------------------------------------------------------------------------
233  FUNCTION bmw_commandline_parser_get_string(this, arg)
234 
235  IMPLICIT NONE
236 
237 ! Declare Arguments
238  CHARACTER (len=path_length) :: bmw_commandline_parser_get_string
239  TYPE (bmw_commandline_parser_class), INTENT(in) :: this
240  CHARACTER (len=*), INTENT(in) :: arg
241 
242 ! Local arguments
243  INTEGER :: i
244  REAL (rprec) :: start_time
245 
246 ! Start of executable code
247  start_time = profiler_get_start_time()
248 
249 ! Loop through the arguments until the correct arg is found.
251 
252  IF (ASSOCIATED(this%arg)) THEN
253  DO i = 1, SIZE(this%arg)
254  IF (trim(this%arg(i)) .eq. trim(arg)) THEN
255  bmw_commandline_parser_get_string = this%value(i)
256 
257  CALL profiler_set_stop_time( &
258  & 'bmw_commandline_parser_get_string', start_time)
259 
260  RETURN
261  END IF
262  END DO
263  END IF
264 
265  CALL profiler_set_stop_time('bmw_commandline_parser_get_string', &
266  & start_time)
267 
268  END FUNCTION
269 
270 !-------------------------------------------------------------------------------
282 !-------------------------------------------------------------------------------
283  FUNCTION bmw_commandline_parser_get_integer(this, arg, &
284  & default_value)
285 
286  IMPLICIT NONE
287 
288 ! Declare Arguments
290  TYPE (bmw_commandline_parser_class), INTENT(in) :: this
291  CHARACTER (len=*), INTENT(in) :: arg
292  INTEGER, INTENT(in) :: default_value
293 
294 ! Local arguments
295  CHARACTER (len=path_length) :: value
296  INTEGER :: status
297  REAL (rprec) :: start_time
298 
299 ! Start of executable code
300  start_time = profiler_get_start_time()
301 
302  value = bmw_commandline_parser_get_string(this, arg)
303 
304  IF (trim(value) .eq. '') THEN
305  bmw_commandline_parser_get_integer = default_value
306 
307  CALL profiler_set_stop_time( &
308  & 'bmw_commandline_parser_get_integer', start_time)
309 
310  RETURN
311  END IF
312 
313  READ (value,1000,iostat=status) bmw_commandline_parser_get_integer
314 
315  IF (status .ne. 0) THEN
316  bmw_commandline_parser_get_integer = default_value
317  END IF
318 
319  CALL profiler_set_stop_time('bmw_commandline_parser_get_integer', &
320  & start_time)
321 
322 1000 FORMAT(i20)
323 
324  END FUNCTION
325 
326 !-------------------------------------------------------------------------------
338 !-------------------------------------------------------------------------------
339  FUNCTION bmw_commandline_parser_get_real(this, arg, default_value)
340 
341  IMPLICIT NONE
342 
343 ! Declare Arguments
344  REAL (rprec) :: bmw_commandline_parser_get_real
345  TYPE (bmw_commandline_parser_class), INTENT(in) :: this
346  CHARACTER (len=*), INTENT(in) :: arg
347  REAL (rprec), INTENT(in) :: default_value
348 
349 ! Local arguments
350  CHARACTER (len=path_length) :: value
351  INTEGER :: status
352  REAL (rprec) :: start_time
353 
354 ! Start of executable code
355  start_time = profiler_get_start_time()
356 
357  value = bmw_commandline_parser_get_string(this, arg)
358 
359  IF (trim(value) .eq. '') THEN
360  bmw_commandline_parser_get_real = default_value
361 
362  CALL profiler_set_stop_time('bmw_commandline_parser_get_real', &
363  & start_time)
364 
365  RETURN
366  END IF
367 
368  READ (value,*,iostat=status) bmw_commandline_parser_get_real
369 
370  IF (status .ne. 0) THEN
371  bmw_commandline_parser_get_real = default_value
372  END IF
373 
374  CALL profiler_set_stop_time('bmw_commandline_parser_get_real', &
375  & start_time)
376 
377  END FUNCTION
378 
379 !*******************************************************************************
380 ! QUERY SUBROUTINES
381 !*******************************************************************************
382 !-------------------------------------------------------------------------------
391 !-------------------------------------------------------------------------------
392  FUNCTION bmw_commandline_parser_is_flag_set(this, arg)
393 
394  IMPLICIT NONE
395 
396 ! Declare Arguments
398  TYPE (bmw_commandline_parser_class), INTENT(in) :: this
399  CHARACTER (len=*), INTENT(in) :: arg
400 
401 ! Local arguments
402  INTEGER :: i
403  REAL (rprec) :: start_time
404 
405 ! Start of executable code
406  start_time = profiler_get_start_time()
407 
408 ! Loop through the arguments until the correct arg is found.
410 
411  IF (ASSOCIATED(this%arg)) THEN
412  DO i = 1, SIZE(this%arg)
413  IF (trim(this%arg(i)) .eq. trim(arg)) THEN
415 
416  CALL profiler_set_stop_time( &
417  & 'bmw_commandline_parser_is_flag_set', start_time)
418 
419  RETURN
420  END IF
421  END DO
422  END IF
423 
424  CALL profiler_set_stop_time('bmw_commandline_parser_is_flag_set', &
425  & start_time)
426 
427  END FUNCTION
428 
429 !*******************************************************************************
430 ! UTILITY SUBROUTINES
431 !*******************************************************************************
432 !-------------------------------------------------------------------------------
448 !-------------------------------------------------------------------------------
449  SUBROUTINE bmw_commandline_parser_flag_requires_value(this, index)
450 
451  IMPLICIT NONE
452 
453 ! Declare Arguments
454  TYPE (bmw_commandline_parser_class), INTENT(in) :: this
455  INTEGER, INTENT(in) :: index
456 
457 ! Local arguments
458  REAL (rprec) :: start_time
459 
460 ! Start of executable code
461  start_time = profiler_get_start_time()
462 
463  SELECT CASE (trim(this%arg(index)))
464 
465  CASE ('-mgridf','-woutf','-siestaf','-outf','-logf')
466  IF (trim(this%value(index)) .eq. '') THEN
467  WRITE (*,1000) trim(this%arg(index)), &
468  & trim(this%arg(index))
469 
471  END IF
472 
473  END SELECT
474 
475  CALL profiler_set_stop_time( &
476  & 'bmw_commandline_parser_flag_requires_value', start_time)
477 
478 1000 FORMAT(a,' flag requires value. Usage: ',a,'=value')
479 
480  END SUBROUTINE
481 
482 !-------------------------------------------------------------------------------
490 !-------------------------------------------------------------------------------
492 
493  IMPLICIT NONE
494 
495 ! Start of executable code
496 ! All command line messages need to fit within this width.
497 ! ' s c '
498  WRITE(*,*) ' '
499  WRITE(*,*) ' BMW '
500  WRITE(*,*) ' '
501  WRITE(*,*) 'Usage: xbmw [-arg][=option] ... '
502  WRITE(*,*) ' '
503  WRITE(*,*) 'Options: '
504  WRITE(*,*) 'All options are displayed as [arg][takesoption][text]'
505  WRITE(*,*) ' -h N Display this information '
506  WRITE(*,*) ' '
507  WRITE(*,*) ' -mgridf Y Specify the mgrid file name. '
508  WRITE(*,*) ' '
509  WRITE(*,*) ' -woutf Y Specify the wout file name. '
510  WRITE(*,*) ' '
511  WRITE(*,*) ' -siestaf Y Specify the siesta restart file name. '
512  WRITE(*,*) ' When this flag is used, plasma currents '
513  WRITE(*,*) ' are computed from siesta fields instead '
514  WRITE(*,*) ' instead of vmec fields. Overrides the '
515  WRITE(*,*) ' -ju and -jv flags. '
516  WRITE(*,*) ' '
517  WRITE(*,*) ' -outf Y Specify the output file name. '
518  WRITE(*,*) ' '
519  WRITE(*,*) ' -logf Y Write screen output to a log file. '
520  WRITE(*,*) ' '
521  WRITE(*,*) ' -jv N Force balance j^v from curl derived j^u.'
522  WRITE(*,*) ' Overrdes the -ju flag. '
523  WRITE(*,*) ' '
524  WRITE(*,*) ' -ju N Force balance j^u from curl derived j^v.'
525  WRITE(*,*) ' '
526 ! WRITE(*,*) ' -p_start Y Starting phi index to compute fields. If'
527 ! WRITE(*,*) ' this flag is not set, default to index '
528 ! WRITE(*,*) ' 1. '
529 ! WRITE(*,*) ' '
530 ! WRITE(*,*) ' -p_end Y Ending phi index to compute fields. If '
531 ! WRITE(*,*) ' this flag is not set, default to the '
532 ! WRITE(*,*) ' last index. '
533 ! WRITE(*,*) ' '
534 !$ WRITE(*,*) ' -para Y Determines number of threads threads to '
535 !$ WRITE(*,*) ' run with. A value of -1 means use the '
536 !$ WRITE(*,*) ' default number of threads. '
537 !$ WRITE(*,*) ' '
538 ! WRITE(*,*) ' '
539 ! WRITE(*,*) ' -force N Force override of error conditions. To '
540 ! WRITE(*,*) ' prevent loss of valid data, BMW will '
541 ! WRITE(*,*) ' terminate if an error condition is '
542 ! WRITE(*,*) ' triggerd. This flag overrides that error'
543 ! WRITE(*,*) ' potential overwritting valid data. '
544 ! WRITE(*,*) ' '
545 
547 
548  END SUBROUTINE
549 
550  END MODULE
bmw_commandline_parser::bmw_commandline_parser_get_real
real(rprec) function bmw_commandline_parser_get_real(this, arg, default_value)
Get the value of an argument as a Real.
Definition: bmw_commandline_parser.f:340
profiler
Defines functions for measuring an tabulating performance of function and subroutine calls....
Definition: profiler.f:13
bmw_commandline_parser::bmw_commandline_parser_construct
type(bmw_commandline_parser_class) function, pointer bmw_commandline_parser_construct(parallel)
Construct a bmw_commandline_parser_class object.
Definition: bmw_commandline_parser.f:118
bmw_commandline_parser::bmw_commandline_parser_arg_not_found
integer, parameter bmw_commandline_parser_arg_not_found
Commandline argument not found.
Definition: bmw_commandline_parser.f:79
bmw_commandline_parser::bmw_commandline_parser_get_string
character(len=path_length) function bmw_commandline_parser_get_string(this, arg)
Get the value of an argument as a string.
Definition: bmw_commandline_parser.f:234
bmw_commandline_parser::max_length
integer, parameter, private max_length
Maximum length of the complete flag. All command line flags take the form of '-flag=value'.
Definition: bmw_commandline_parser.f:72
bmw_commandline_parser::bmw_commandline_parser_is_flag_set
logical function bmw_commandline_parser_is_flag_set(this, arg)
Check if a command line argument was set.
Definition: bmw_commandline_parser.f:393
bmw_parallel_context
Defines the base class of the type bmw_parallel_context_class. This contains the state variables need...
Definition: bmw_parallel_context.f:11
bmw_commandline_parser
Defines the base class of the type bmw_commandline_parser_class.
Definition: bmw_commandline_parser.f:57
bmw_commandline_parser::bmw_commandline_parser_destruct
subroutine bmw_commandline_parser_destruct(this)
Deconstruct a bmw_commandline_parser_class object.
Definition: bmw_commandline_parser.f:198
bmw_commandline_parser::bmw_commandline_parser_class
Base class containing a parsed bmw_commandline.
Definition: bmw_commandline_parser.f:89
profiler::profiler_get_start_time
real(rprec) function profiler_get_start_time()
Gets the start time of profiled function.
Definition: profiler.f:194
bmw_commandline_parser::bmw_commandline_parser_flag_requires_value
subroutine bmw_commandline_parser_flag_requires_value(this, index)
Check if a command line argument requires a value.
Definition: bmw_commandline_parser.f:450
bmw_commandline_parser::bmw_commandline_parser_no_error
integer, parameter bmw_commandline_parser_no_error
Commandline argument not found.
Definition: bmw_commandline_parser.f:77
file_opts
Contains cross platform routines for manipulating files on the file system. Defines a functions to mo...
Definition: file_opts.f:13
bmw_parallel_context::bmw_parallel_context_abort
subroutine bmw_parallel_context_abort(status)
Abort the entire program.
Definition: bmw_parallel_context.f:177
bmw_commandline_parser::max_arg_length
integer, parameter, private max_arg_length
Maximum length of the argument including the '-' character.
Definition: bmw_commandline_parser.f:69
profiler::profiler_set_stop_time
subroutine profiler_set_stop_time(symbol_name, start_time)
Gets the end time of profiled function.
Definition: profiler.f:121
bmw_commandline_parser::bmw_commandline_parser_print_help
subroutine bmw_commandline_parser_print_help
Print out help text.
Definition: bmw_commandline_parser.f:492
bmw_parallel_context::bmw_parallel_context_class
Base class representing a bmw parallel context. This contains all memory needed parameters needed to ...
Definition: bmw_parallel_context.f:26
bmw_commandline_parser::bmw_commandline_parser_get_integer
integer function bmw_commandline_parser_get_integer(this, arg, default_value)
Get the value of an argument as an integer.
Definition: bmw_commandline_parser.f:285
file_opts::path_length
integer, parameter path_length
Length of file paths.
Definition: file_opts.f:22