Skip to content

Commit

Permalink
feat: plottings fields start
Browse files Browse the repository at this point in the history
  • Loading branch information
Parzival1918 committed Oct 3, 2023
1 parent 5ee28e6 commit b981c9e
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
Empty file.
37 changes: 37 additions & 0 deletions pu_pjr/force_fields_plotting/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import argparse
from rich.console import Console

from . import utils
from . import force_fields

def main():
parser = argparse.ArgumentParser(
description="Plot potentials.",
prog="fields",
epilog="Created by Pedro Juan Royo @UnstrayCato"
)
parser.set_defaults(which="main")

parser.add_argument(
"--version", "-v", action="version", version="%(prog)s v0.17.0"
)

available_fields = [a.value for a in utils.Fields]
parser.add_argument(
"--field", "-f", nargs="*", choices=available_fields,
help="Force field to plot"
)

args = parser.parse_args()
print(args)

console = Console()
if args.which == "main":
with console.status("[bold green]Plot created"):
force_fields.plot_field(points = utils.create_range(0.9,3,60),
epsilon = 1, sigma = 1.0, cut = 3.5)
else:
parser.print_help()

if __name__ == "__main__":
main()
28 changes: 28 additions & 0 deletions pu_pjr/force_fields_plotting/force_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from matplotlib import pyplot as plt

from . import utils

def get_field_data(
field: utils.Fields, points: list[float], **kwargs
) -> list[float]:
"""Get the values for some points in a field"""
if field == utils.Fields.LJ_CUT:
# print("lj/cut field")
return utils.lj_cut(points, **kwargs)
else:
raise NotImplementedError(f'Field {field} not implemented')

def plot_field(
field: utils.Fields = utils.Fields.LJ_CUT,
points: list[float] = utils.create_range(),
**kwargs
) -> None:
"""Plot a field"""

x = points
y = get_field_data(field, points, **kwargs)
# print(x, len(x))
# print(y)

plt.plot(x, y, 'o-')
plt.show(block=True)
37 changes: 37 additions & 0 deletions pu_pjr/force_fields_plotting/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from enum import Enum

class Fields(Enum):
LJ_CUT = 'lj/cut'

def create_range(
start: float = 0.1, end: float = 3.0, items: int = 30
) -> list[float]:
"""Return a float range between 'start' and 'end'"""
# Check end is bigger than start
if start > end:
raise ValueError("start value is bigger than end value")

# Check that items is bigger than 1
if items <= 1:
raise ValueError("items value must be bigger than 1")

step_val = (end - start) / (items - 1)
range_vals = []
for i in range(items):
range_vals.append(start + step_val*i)

return range_vals

def lj_cut(
points: list[float], epsilon: float, sigma: float, cut: float
) -> list[float | None]:
"""Lennard-Jones potential with a cut-off at `cut`"""

values = []
for r in points:
if r < cut:
values.append(4 * epsilon * ((sigma / r) ** 12 - (sigma / r) ** 6))
else:
values.append(None)

return values
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pytest = "^7.4.2"
[tool.poetry.scripts]
quickplot = "pu_pjr.plotting.__main__:main"
dirstats = "pu_pjr.dir_stats.__main__:main"
fields = "pu_pjr.force_fields_plotting.__main__:main"

[build-system]
requires = ["poetry-core"]
Expand Down
14 changes: 14 additions & 0 deletions tests/test_force_fields_plotting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pu_pjr.force_fields_plotting import utils

import pytest

def test_create_range():
assert [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.7999999999999999, 0.8999999999999999,
0.9999999999999999, 1.0999999999999999, 1.2, 1.3, 1.4, 1.5,
1.5999999999999999, 1.7, 1.8, 1.9, 2.0, 2.0999999999999996,
2.1999999999999997, 2.3, 2.4, 2.5, 2.6, 2.6999999999999997,
2.8, 2.9, 3.0] == \
utils.create_range()
assert [0, 1.0] == utils.create_range(0.0, 1.0, 2)
with pytest.raises(ValueError):
utils.create_range(1, 0)

0 comments on commit b981c9e

Please sign in to comment.