import matplotlib.pyplot as plt
import numpy as np

filename = "pressure.xvg"

time = []
pressure = []

with open(filename, "r") as f:
    for line in f:
        if line.startswith("#") or line.startswith("@"):
            continue

        parts = line.split()
        if len(parts) >= 2:
            time.append(float(parts[0]))
            pressure.append(float(parts[1]))

time = np.array(time)
pressure = np.array(pressure)

window = 20
smoothed = np.convolve(pressure, np.ones(window) / window, mode="valid")
time_smooth = time[:len(smoothed)]

plt.figure(figsize=(10, 6))
plt.plot(time, pressure, color="lightgreen", linewidth=1.0, alpha=0.7, label="Pressure")
plt.plot(time_smooth, smoothed, color="darkgreen", linewidth=2.0, label=f"Moving average ({window} points)")
plt.axhline(y=1.0, color="navy", linestyle="--", linewidth=1.2, label="Target pressure (1 bar)")

plt.xlabel("Time (ps)", fontsize=14)
plt.ylabel("Pressure (bar)", fontsize=14)

plt.xticks(fontsize=14)
plt.yticks(fontsize=14)

plt.grid(True, linestyle="--", alpha=0.5)
plt.legend(fontsize=12)

plt.tight_layout()
plt.savefig("pressure_profile.png", dpi=300, bbox_inches="tight")
plt.show()