-
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 #12 from comecattin/9-potential-and-kinetic-energies
Compute kinetic, potential and total energies
- Loading branch information
Showing
7 changed files
with
135 additions
and
4 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,4 +39,6 @@ bin/ | |
*.arc | ||
trajectories.dat | ||
input.dat | ||
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 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,48 @@ | ||
module energies_module | ||
implicit none | ||
real(8), parameter :: epsilon = 1.0 ! LJ potential well depth | ||
real(8), parameter :: sigma = 1.0 ! LJ distance where potential is zero | ||
|
||
contains | ||
subroutine compute_energies(pos, vel, n, ke, pe, te) | ||
real(8), dimension(n, 3), intent(in) :: pos, vel | ||
integer, intent(in) :: n | ||
real(8), intent(out) :: ke, pe, te | ||
|
||
call compute_kinetic_energy(vel, n, ke) | ||
call compute_potential_energy(pos, n, pe) | ||
te = ke + pe | ||
|
||
print *, "Kinetic energy: ", ke | ||
print *, "Potential energy: ", pe | ||
print *, "Total energy: ", te | ||
end subroutine compute_energies | ||
|
||
subroutine compute_kinetic_energy(vel, n, ke) | ||
real(8), dimension(n, 3), intent(in) :: vel | ||
integer, intent(in) :: n | ||
real(8), intent(out) :: ke | ||
integer :: i | ||
|
||
ke = 0.0 | ||
do i = 1, n | ||
ke = ke + 0.5 * sum(vel(i, :) ** 2) | ||
end do | ||
end subroutine compute_kinetic_energy | ||
|
||
subroutine compute_potential_energy(pos, n, pe) | ||
real(8), dimension(n, 3), intent(in) :: pos | ||
integer, intent(in) :: n | ||
real(8), intent(out) :: pe | ||
integer :: i, j | ||
real(8) :: r | ||
|
||
pe = 0.0 | ||
do i = 1, n | ||
do j = i + 1, n | ||
r = sqrt(sum((pos(i, :) - pos(j, :)) ** 2)) | ||
pe = pe + 4.0 * epsilon * ((sigma / r) ** 12 - (sigma / r) ** 6) | ||
end do | ||
end do | ||
end subroutine compute_potential_energy | ||
end module energies_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
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
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,44 @@ | ||
#!/usr/bin/env python3 | ||
"""Plot the energies of the system as a function of time.""" | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import argparse | ||
|
||
def load_data(filename): | ||
"""Load the data from the file.""" | ||
with open(filename, 'r') as f: | ||
data = f.readlines() | ||
for i, line in enumerate(data): | ||
data[i] = float(line.replace('Step:', '').replace('Kinetic Energy:', '').replace('Potential Energy:', '').replace('Total Energy:', '').replace('\n', '').replace(' ', '')) | ||
step = np.array(data[0::4], dtype=int) | ||
kinetic_energy = np.array(data[1::4]) | ||
potential_energy = np.array(data[2::4]) | ||
total_energy = np.array(data[3::4]) | ||
return kinetic_energy, potential_energy, total_energy, step | ||
|
||
def plot_energies(kinetic_energy, potential_energy, total_energy, step): | ||
"""Plot the energies as a function of time.""" | ||
plt.plot(step, kinetic_energy, label='Kinetic Energy') | ||
plt.plot(step, potential_energy, label='Potential Energy') | ||
plt.plot(step, total_energy, label='Total Energy') | ||
plt.xlabel('Time step') | ||
plt.ylabel('Energy') | ||
plt.legend() | ||
plt.savefig('energies.pdf', bbox_inches='tight', dpi=300) | ||
plt.show() | ||
|
||
def main(): | ||
"""Read the filename from the command line and plot the energies.""" | ||
parser = argparse.ArgumentParser( | ||
description='Plot the energies of the system as a function of time.' | ||
) | ||
parser.add_argument( | ||
'filename', | ||
help='The name of the file containing the energies.' | ||
) | ||
args = parser.parse_args() | ||
kinetic_energy, potential_energy, total_energy, step = load_data(args.filename) | ||
plot_energies(kinetic_energy, potential_energy, total_energy, step) | ||
|
||
if __name__ == "__main__": | ||
main() |