# Plot the pile response at the end of loading from the files recorded by
# staticBNWFpile.tcl or staticBNWFpile.py (run in the same directory).
#
#   python plotResults.py

import numpy as np
import matplotlib.pyplot as plt

# geometry used in the analysis
L1 = 1.0                    # pile head above ground surface (m)
L2 = 20.0                   # embedded pile length (m)
nElePile = 84
eleSize = (L1 + L2) / nElePile

# last recorded step (full lateral load)
disp = np.loadtxt('pileDisp.out')[-1, 1:]      # x, y, z at pile nodes 201..285
force = np.loadtxt('pileForce.out')[-1, 1:]    # 12 global forces per pile element
react = np.loadtxt('reaction.out')[-1, 1:]     # x reaction at spring nodes 1..81

# depth below the ground surface (nodes are numbered from the pile tip up)
zNode = eleSize * np.arange(nElePile + 1)
depthNode = L2 - zNode
depthEle = L2 - (zNode[:-1] + 0.5 * eleSize)
depthSpring = depthNode[:len(react)]

ux = disp[0::3]                                 # lateral displacement
F = force.reshape(nElePile, 12)
V = -F[:, 0]                                    # shear (end i, sign flipped to element convention)
M = F[:, 4]                                     # moment about y-axis at end i
p = react / eleSize                             # soil reaction per unit length

fig, ax = plt.subplots(1, 4, figsize=(12, 5), sharey=True)
data = [(ux, depthNode, 'Displacement (m)'),
        (V, depthEle, 'Shear (kN)'),
        (M, depthEle, 'Moment (kN-m)'),
        (p, depthSpring, 'Soil reaction (kN/m)')]
for a, (x, y, label) in zip(ax, data):
    a.plot(x, y)
    a.axvline(0.0, color='0.6', lw=0.8)
    a.axhline(0.0, color='0.6', lw=0.8, ls='--')
    a.set_xlabel(label)
    a.grid(True, alpha=0.3)
ax[0].set_ylabel('Depth below ground surface (m)')
ax[0].invert_yaxis()
fig.suptitle('Pile response under 3500 kN lateral load')
fig.tight_layout()
fig.savefig('pileResults.png', dpi=150)
plt.show()
