Skip to content

Commit

Permalink
Merge pull request #15 from comecattin/parser
Browse files Browse the repository at this point in the history
Better parser
  • Loading branch information
comecattin authored Jun 6, 2024
2 parents f5dc02f + 11e2ee2 commit 2b09ac5
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ bin/
*.arc
trajectories.dat
input.dat
input.in
energies.dat
energies.pdf
md_simulation
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# F-MD, Fortran Molecular Dynamic

<p align='center'>
<img src=https://github.com/comecattin/F-MD/assets/75748278/1d46d2ed-7f07-4c99-9086-a80ddbaf25b5>
</p>
Expand All @@ -25,16 +26,18 @@ make

## Run an MD simulation

To run an MD simulation simply run `./md_simulation input.dat` in the `src` directory.
To run an MD simulation simply run `./md_simulation input.in` in the `src` directory.

The file `input.dat` is the input file and contain in the following order:
The file `input.in` is the input file and contain:

- The number of particles
- The number of time steps
- The time step
- The box length
- The number of particles (`n_atoms`, default `30`)
- The number of time steps (`n_steps`, default `1000`)
- The time step (`dt`, default `0.001`)
- The box length (`box_length`, default `10.0`)
- The tolerance of the SHAKE algorithm for angle and bonds length constraints (`tolerance`, default `1e-6`)
- The maximum number of iteration for the SHAKE algorithm (`max_iter`, default `100`)

An example is given in the `examples/example_input.dat` file.
An example is given in the `examples/example_input.in` file.

## Output and plot the trajectories

Expand Down
6 changes: 0 additions & 6 deletions examples/example_input.dat

This file was deleted.

6 changes: 6 additions & 0 deletions examples/example_input.in
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
55 changes: 55 additions & 0 deletions src/input_parser.f90
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
42 changes: 42 additions & 0 deletions src/input_parser.py
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')

30 changes: 3 additions & 27 deletions src/main.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
program md_simulation
use parser_module, only : read_config
use initialization_module, only : initialize, initialize_water
use forces_module, only : compute_forces, compute_forces_water
use energies_module
Expand All @@ -9,7 +10,7 @@ program md_simulation
! Define parameters
integer :: n_atoms, n_steps, step, num_args, max_iter
real(8) :: dt, box_length, tolerance
character(len=100) :: input_file, output_file, output_file_energies
character(len=100) :: input_file, output_file, output_file_energies, command
logical :: file_exists

! Define position, velocity and forces arrays
Expand All @@ -18,32 +19,7 @@ program md_simulation
! Kinetic, potential and total energies
real(8) :: ke, pe, te

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

! Read the input parameters
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 the input parameters
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
call read_config(input_file, n_atoms, n_steps, dt, box_length, tolerance, max_iter)

! Allocate the arrays
allocate(positions(3, n_atoms))
Expand Down
5 changes: 4 additions & 1 deletion src/makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FC = gfortran
OBJ = initialization.o forces.o contraints.o integration.o output_module.o energies.o main.o
OBJ = input_parser.o initialization.o forces.o contraints.o integration.o output_module.o energies.o main.o

md_simulation: $(OBJ)
$(FC) -o md_simulation $(OBJ)
Expand All @@ -25,6 +25,9 @@ output_module.o: output_module.f90
contraints.o: contraints.f90
$(FC) -c contraints.f90

input_parser.o: input_parser.f90
$(FC) -c input_parser.f90

clean:
rm -f *.o *.mod md_simulation trajectories.dat

0 comments on commit 2b09ac5

Please sign in to comment.