Skip to content

Commit

Permalink
Implements command line parser for the 3D mesher
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohit-Kakodkar committed Jan 14, 2025
1 parent c62b0da commit 31f37a2
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 18 deletions.
1 change: 1 addition & 0 deletions fortran/meshfem3d/meshfem3D/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(MESHFEM3D_MESH_MODULE
get_MPI_cutplanes_xi.f90
meshfem3D.F90
get_wavefield_discontinuity.f90
command_line_arguments.f90
read_mesh_parameter_file.f90
read_value_mesh_parameters.f90
save_databases.F90
Expand Down
1 change: 1 addition & 0 deletions fortran/meshfem3d/shared/CMakeLists.txt
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

0 comments on commit 31f37a2

Please sign in to comment.