"""Tilt of a circular thrust pad at constant film thickness at the centre
======================================================================

The runner is tilted about the x axis until the gap at the rim has closed to
1 µm. Tilt is an ordinary position coordinate, so its restoring moment and
tilt stiffness come out of the same solve as the load.
"""

import matplotlib.pyplot as plt
import numpy as np

from openairbearing import core
from openairbearing.plots import COLOURS

# the series of every plot in blue, red, black and grey, in that order
plt.rcParams["axes.prop_cycle"] = plt.cycler(color=COLOURS)

RADIUS = 37e-3 / 2
H0 = 5e-6


def main(n_tilt=41):
    film = core.circular_pad(
        RADIUS, porous=core.Porous(kappa=1e-15, hp=4.5e-3), ps=0.7e6
    )
    tilt_max = (H0 - 1e-6) / RADIUS
    tilts = np.linspace(-tilt_max, tilt_max, n_tilt)
    states = [core.solve_static(film, [H0, rx, 0.0]) for rx in tilts]
    moment = np.array([s.force("rx") for s in states])

    fig, axes = plt.subplots(1, 3, figsize=(13, 4))
    urad = tilts * 1e6
    axes[0].plot(urad, [s.force("h") for s in states])
    axes[0].set_ylabel("Load [N]")
    axes[1].plot(urad, moment)
    axes[1].set_ylabel("Restoring moment [N·m]")
    axes[2].plot(urad, -np.gradient(moment, tilts) * 1e-3)
    axes[2].set_ylabel("Tilt stiffness [N·m/mrad]")
    for ax in axes:
        ax.set_xlabel("Tilt about x [µrad]")
    rim = (H0 - tilt_max * RADIUS) * 1e6
    axes[0].set_title(f"Gap at the rim closes to {rim:.0f} µm at full tilt")
    fig.tight_layout()
    plt.show()


if __name__ == "__main__":
    main()
