Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements command line parser for the 3D mesher #388

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fortran/meshfem3d/shared/CMakeLists.txt
icui marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set(MESHFEM3D_SHARED_MODULE
merge_sort.f90
netlib_specfun_erf.f90
prepare_assemble_MPI.f90
command_line_arguments.f90
read_parameter_file.F90
read_topo_bathy_file.f90
read_value_parameters.f90
Expand Down
74 changes: 74 additions & 0 deletions fortran/meshfem3d/shared/command_line_arguments.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

subroutine print_help_message()

implicit none

write(*,*) ' '
write(*,*) 'Usage: meshfem3D [options]'
write(*,*) ' '
write(*,*) 'Options:'
write(*,*) ' '
write(*,*) ' -h, --help'
write(*,*) ' Print this help message'
write(*,*) ' '
write(*,*) ' -p, --Par_File'
write(*,*) ' Specify the parameter file to use'
write(*,*) ' '

end subroutine print_help_message

subroutine parse_command_line_arguments()

use constants, only: MAX_STRING_LEN, one
use shared_parameters, only: Par_file

implicit none

integer :: i = 1, n_args

character(len=MAX_STRING_LEN) :: arg

! get the number of command line arguments
n_args = command_argument_count()

if (n_args == 0) then
! print help message
call print_help_message()
! stop the code
stop
endif

! loop over the command line arguments
do while(.TRUE.)
! get the i-th command line argument
call get_command_argument(i, arg)

select case (arg)
case ('-h', '--help')
! print help message
call print_help_message()
! stop the code gracefully
call finalize_mpi()
call EXIT(0)
case ('-p', '--Par_File')
if (i == n_args) then
! print error message
print *, 'Error: missing command line argument for option '//trim(arg)
stop
endif
! get the next command line argument
call get_command_argument(i+1, Par_file)
! skip the next command line argument
i = i + 1
case default
! print error message
print *, 'Error: unknown command line argument '//trim(arg)
stop
end select

if (i == n_args) exit
! increment the counter
i = i + 1
end do

end subroutine parse_command_line_arguments
2 changes: 2 additions & 0 deletions fortran/meshfem3d/shared/read_parameter_file.F90
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ subroutine read_parameter_file(BROADCAST_AFTER_READ)
! to avoid an I/O bottleneck in the case of very large runs
if (myrank == 0) then

call parse_command_line_arguments()

! opens file Par_file
call open_parameter_file(ier)

Expand Down
23 changes: 5 additions & 18 deletions fortran/meshfem3d/shared/read_value_parameters.f90
Original file line number Diff line number Diff line change
Expand Up @@ -157,34 +157,21 @@ end subroutine open_parameter_file_from_main_only
subroutine open_parameter_file(ier)

use constants, only: MAX_STRING_LEN,IN_DATA_FILES
use shared_input_parameters, only: Par_File

implicit none

integer :: ier
character(len=MAX_STRING_LEN) :: filename_main,filename_run0001

filename_main = IN_DATA_FILES(1:len_trim(IN_DATA_FILES))//'Par_file'

! note: for simultaneous runs, we require only a single Par_file in the main root directory DATA/Par_file
! that is, no other files in the run directories are needed, like run0001/DATA/Par_file, run0001/DATA/Par_file, etc.
! this avoids potential problems if different Par_files would have different settings (e.g., NPROC, MODEL, ..).

! to be gentle, we also allow for a setup where the main Par_file is put into run0001/DATA/
! in case we are running several independent runs in parallel
! to do so, add the right directory for that run for the main process only here
filename_run0001 = 'run0001/'//filename_main(1:len_trim(filename_main))
filename_main = Par_File

call param_open(filename_main, len(filename_main), ier)

if (ier /= 0) then
! checks second option with Par_file in run0001/DATA/
call param_open(filename_run0001, len(filename_run0001), ier)
if (ier /= 0) then
print *
print *,'Opening file failed, please check your file path and run-directory.'
print *,'checked first: ',trim(filename_main)
print *,' and then: ',trim(filename_run0001)
stop 'Error opening Par_file'
endif
print *, 'Error opening Par_file: ',trim(filename_main)
stop
endif

end subroutine open_parameter_file
Expand Down
2 changes: 2 additions & 0 deletions fortran/meshfem3d/shared/shared_par.F90
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ module shared_input_parameters

character(len=MAX_STRING_LEN) :: TOMOGRAPHY_PATH

character(len=MAX_STRING_LEN) :: Par_File

! attenuation
! reference frequency of seismic model
double precision :: ATTENUATION_f0_REFERENCE
Expand Down
Loading