# Plot the results recorded by 1Dconsolidation.tcl or 1Dconsolidation.py
# (Python replacement for the original plotPorePressure.m and plotStress.m).
#
#   python plotResults.py                 # results in the current directory
#   python plotResults.py single double   # compare runs in several directories
#
# The pore pressure and stress isochrones are plotted for the first directory;
# the surface settlement is plotted for every directory given.

import os
import sys
import numpy as np
import matplotlib.pyplot as plt

dirs = sys.argv[1:] or ['.']

# times (s) at which the isochrones are drawn
plotTimes = [0.25, 1, 2, 5, 10, 20, 50, 100, 200]


def load(d, name):
    data = np.loadtxt(os.path.join(d, name))
    return data[:, 0], data[:, 1:]


fig, ax = plt.subplots(1, 3, figsize=(13, 5))
colors = plt.cm.viridis(np.linspace(0, 1, len(plotTimes)))

# pore pressure at the corner nodes on the left boundary (nodes 1, 7, ..., 61)
t, pwp = load(dirs[0], 'porepressure.out')
pwpNodes = np.arange(1, 62, 6)
depthNode = np.linspace(10, 0, 11)
for c, tp in zip(colors, plotTimes):
    k = np.argmin(abs(t - tp))
    ax[0].plot(pwp[k, pwpNodes - 1], depthNode, '-o', color=c, ms=4, label=f't = {t[k]:g} s')
ax[0].set_xlabel('Pore pressure (kPa)')
ax[0].set_ylabel('Depth (m)')
ax[0].legend(fontsize=8)

# vertical effective stress at the element centers (5 stress components per element)
t, stress = load(dirs[0], 'stress.out')
sv = -stress[:, 1::5]
depthEle = np.linspace(9.5, 0.5, sv.shape[1])
for c, tp in zip(colors, plotTimes):
    k = np.argmin(abs(t - tp))
    ax[1].plot(sv[k], depthEle, '-o', color=c, ms=4)
ax[1].set_xlabel('Vertical effective stress (kPa)')

for a in ax[:2]:
    a.invert_yaxis()

# settlement of the upper right node (node 63, vertical displacement)
for d in dirs:
    t, disp = load(d, 'displacement.out')
    ax[2].plot(t, disp[:, -1], label=None if d == '.' else d)
ax[2].set_xlabel('Time (s)')
ax[2].set_ylabel('Surface settlement (m)')
if len(dirs) > 1:
    ax[2].legend()

for a in ax:
    a.grid(True, alpha=0.3)
fig.tight_layout()
fig.savefig('consolidationResults.png', dpi=150)
plt.show()
