-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from comecattin/parser
Better parser
- Loading branch information
Showing
8 changed files
with
121 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ bin/ | |
*.arc | ||
trajectories.dat | ||
input.dat | ||
input.in | ||
energies.dat | ||
energies.pdf | ||
md_simulation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
n_atoms : 30 # Number of atoms in the system | ||
n_steps : 10000 # Number of time steps | ||
dt : 0.001 # Time step | ||
box_length : 10.0 # Length of the PBC box | ||
tolerance : 1.0e-6 # SHAKE tolerance | ||
max_iter : 100 # Maximum iteration for SHAKE algorithm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module parser_module | ||
implicit none | ||
|
||
contains | ||
|
||
subroutine read_config(input_file, n_atoms, n_steps, dt, box_length, tolerance, max_iter) | ||
! | ||
! Routine to read the input configuration file | ||
! | ||
character(len=100):: input_file | ||
|
||
integer :: num_args | ||
character(len=100) :: command | ||
|
||
integer, intent(out) :: n_atoms, n_steps, max_iter | ||
real(8), intent(out) :: dt, box_length, tolerance | ||
|
||
! Check that CLI parameters is correct | ||
num_args = command_argument_count() | ||
if (num_args /= 1) then | ||
print *, 'Usage: ./md_simulation input_file' | ||
stop | ||
else | ||
call get_command_argument(1, input_file) | ||
print *, 'Input file: ', input_file | ||
end if | ||
|
||
! Create the python command | ||
write(command, '(A, A)') 'python input_parser.py ', trim(input_file) | ||
call system(command) | ||
|
||
! Read the temporary file created by python | ||
input_file = 'parsed_config.tmp' | ||
open(unit=20, file=input_file) | ||
read(20, *) n_atoms | ||
read(20, *) n_steps | ||
read(20, *) dt | ||
read(20, *) box_length | ||
read(20, *) tolerance | ||
read(20, *) max_iter | ||
close(20) | ||
|
||
print *, 'Number of atoms: ', n_atoms | ||
print *, 'Number of steps: ', n_steps | ||
print *, 'Time step: ', dt | ||
print *, 'Box length: ', box_length | ||
print *, 'SHAKE Tolerance: ', tolerance | ||
print *, 'SHAKE Max iterations: ', max_iter | ||
|
||
! Remove the temporary file | ||
call system('rm parsed_config.tmp') | ||
|
||
|
||
end subroutine read_config | ||
end module parser_module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env python3 | ||
"""Parsing of the input file.""" | ||
import argparse | ||
|
||
def parse_config(file_name): | ||
|
||
config = {} | ||
|
||
with open(file_name, 'r') as f: | ||
for line in f: | ||
line = line.strip() | ||
if not line or line.startswith('#'): | ||
continue | ||
key, value = line.split(':', 1) | ||
value = value.split('#', 1)[0].strip() | ||
config[key.strip()] = value.strip() | ||
|
||
return config | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="Parsing of the input file") | ||
parser.add_argument("file_name", help="Path to the input file") | ||
args = parser.parse_args() | ||
config = parse_config(args.file_name) | ||
|
||
dt = config.get('dt', '0.001') | ||
n_steps = config.get('n_steps', '1000') | ||
n_atoms = config.get('n_atoms', '30') | ||
box_length = config.get('box_length', '10.0') | ||
tolerance = config.get('tolerance', '1e-6') | ||
max_iter = config.get('max_iter', '100') | ||
|
||
with open('parsed_config.tmp', 'w') as f: | ||
f.write(f'{n_atoms}\n') | ||
f.write(f'{n_steps}\n') | ||
f.write(f'{dt}\n') | ||
f.write(f'{box_length}\n') | ||
f.write(f'{tolerance}\n') | ||
f.write(f'{max_iter}\n') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters