###########################################################
#                                                         #
# Procedure to compute ultimate resistance, tult, and     #
#  displacement at 50% mobilization of tult, z50, for     #
#  use in t-z curves for cohesionless soil.               #
#                                                         #
#   Created by:  Chris McGann                             #
#                University of Washington                 #
#                                                         #
#   Python version of get_tzParam.tcl                     #
#                                                         #
###########################################################

# references
#  Mosher, R.L. (1984). "Load transfer criteria for numerical analysis of
#   axial loaded piles in sand." U.S. Army Engineering and Waterways
#   Experimental Station, Automatic Data Processing Center, Vicksburg, Miss.
#
#  Kulhawy, F.H. (1991). "Drilled shaft foundations." Foundation engineering
#   handbook, 2nd Ed., Chap 14, H.-Y. Fang ed., Van Nostrand Reinhold, New York

import math


def get_tzParam(phi, b, sigV, pEleLength):
    """Return [tult, z50] for a TzSimple1 t-z spring."""

    pi = 3.14159265358979

    # Compute tult based on tult = Ko*sigV*pi*dia*tan(delta), where
    #   Ko    is coeff. of lateral earth pressure at rest,
    #         taken as Ko = 0.4
    #   delta is interface friction between soil and pile,
    #         taken as delta = 0.8*phi to be representative of a
    #         smooth precast concrete pile after Kulhawy (1991)
    delta = 0.8 * phi * pi / 180

    # if z = 0 (ground surface) need to specify a small non-zero value of sigV
    if sigV == 0.0:
        sigV = 0.01

    tu = 0.4 * sigV * pi * b * math.tan(delta)

    # TzSimple1 material formulated with tult as force, not stress, multiply by tributary length of pile
    tult = tu * pEleLength

    # Mosher (1984) provides recommended initial tangents based on friction angle
    # values are in units of psf/in
    kf = [6000, 10000, 10000, 14000, 14000, 18000]
    fric = [28, 31, 32, 34, 35, 38]

    # determine kf for input value of phi, linear interpolation for intermediate values
    if phi < fric[0]:
        k = kf[0]
    elif phi > fric[-1]:
        k = kf[-1]
    else:
        for i in range(len(fric) - 1):
            if fric[i] <= phi <= fric[i + 1]:
                k = (kf[i + 1] - kf[i]) / (fric[i + 1] - fric[i]) * (phi - fric[i]) + kf[i]

    # need to convert kf to units of kN/m^3
    kSIunits = k * 1.885

    # based on a t-z curve of the shape recommended by Mosher (1984), z50 = tult/kf
    z50 = tult / kSIunits

    # return values of tult and z50 for use in t-z material
    return [tult, z50]
