{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic usage (Readme example)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# RUN THIS IF RUNNING IN FORK\n",
    "import os\n",
    "import sys\n",
    "sys.path.insert(0, os.path.abspath('../'))\n",
    "\n",
    "\n",
    "# import required classes\n",
    "from pyslope import (\n",
    "    Slope,\n",
    "    Material,\n",
    "    Udl,\n",
    "    LineLoad,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# RUN THIS CELL FIRST TO INITIALISE GOOGLE NOTEBOOK!!!!\n",
    "!pip install pyslope\n",
    "\n",
    "# import required classes\n",
    "from pyslope import (\n",
    "    Slope,\n",
    "    Material,\n",
    "    Udl,\n",
    "    LineLoad,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Arbritrary example defined in README.md\n",
    "\n",
    "# slope defined with height (m) of slope and angle (deg) or length (m) of slope,\n",
    "# depending on which value is set and which is None.\n",
    "s = Slope(height=3, angle=30, length=None)\n",
    "\n",
    "# define a material with unit weight, friction angle, cohesion and depth from top\n",
    "# of slope to bottom of layer. The model will automatically order units and if\n",
    "# a material is the lowest it will be extended to the bottom of the model\n",
    "m1 = Material(\n",
    "    unit_weight=20,\n",
    "    friction_angle=45,\n",
    "    cohesion=2,\n",
    "    depth_to_bottom=2\n",
    ")\n",
    "\n",
    "# material can also be defined with positional arguments, if the user can remember\n",
    "# the order of required parameters.\n",
    "m2 = Material(20, 30, 2, 5)\n",
    "\n",
    "# the created material objects can then be assigned to the model.\n",
    "# The same depth to bottom cant be specified for two different materials and an\n",
    "# error will be raised if this happens\n",
    "s.set_materials(m1,m2)\n",
    "\n",
    "# define uniform load objects with magnitude (kPa), offset (m) from the crest of the slope\n",
    "# length of the load (m) (if greater than length slope or None then assumed continuous).\n",
    "u1 = Udl(magnitude = 100, offset = 2, length = 1)\n",
    "\n",
    "# by default offset = 0 (m) and length = None.\n",
    "u2 = Udl(magnitude = 20)\n",
    "\n",
    "# assign uniform loads to model \n",
    "s.set_udls(u1, u2)\n",
    "\n",
    "# define line load, similiar to Udl except there is no length parameter and magnitude is in units (kN/m)\n",
    "p1 = LineLoad(magnitude = 10, offset = 3)\n",
    "\n",
    "# assign line loads to slope\n",
    "s.set_lls(p1)\n",
    "\n",
    "# Set water table if required. If None then not considered, by default no water table.\n",
    "s.set_water_table(4)\n",
    "\n",
    "# set limits on the slope failure search zone\n",
    "s.set_analysis_limits(s.get_top_coordinates()[0] - 5, s.get_bottom_coordinates()[0] + 5)\n",
    "\n",
    "# run calculations on the created slope\n",
    "s.analyse_slope()\n",
    "\n",
    "# print the critical FOS for the slope\n",
    "print('fos:',s.get_min_FOS())\n",
    "\n",
    "# plot the critical failure surface\n",
    "fig_1 = s.plot_critical()\n",
    "fig_1.show()\n",
    "\n",
    "# plot all failure planes with a FOS below 2\n",
    "fig_2 = s.plot_all_planes(max_fos=2)\n",
    "fig_2.show()\n",
    "\n",
    "\n",
    "# save the results (optional)\n",
    "# Can save figure using ``fig.write_image(\"./results.pdf\")`` (can change extension to be\n",
    "# png, jpg, svg or other formats as reired). Requires pip install -U kaleido\n",
    "\n",
    "# fig_1.write_image(\"./readme_example_plot_critical.png\")\n",
    "# fig_2.write_image(\"./readme_example_plot_all_maxfos2.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# lets say i want to see how close i can move my 100 kPa UDL\n",
    "# while keeping the FOS above 1.4\n",
    "\n",
    "# remove udl object load from slope\n",
    "s.remove_udls(u1)\n",
    "\n",
    "# if we didnt have the object we could remove all and readd the\n",
    "# other udl object\n",
    "s.remove_udls(remove_all=True)\n",
    "s.set_udls(u2)\n",
    "\n",
    "# now lets add the udl again but this time set the load as 'dynamic'\n",
    "# for all loads and materials we also have the option to set the color ourselves\n",
    "# lets try set the color as 'purple'\n",
    "s.set_udls(\n",
    "    Udl(magnitude=100, length=1, offset=2, dynamic_offset=True, color='purple')\n",
    ")\n",
    "\n",
    "# note that if we try to look at the minimum FOS now we get nothing.\n",
    "# if we change anything about the model all previosu calculations are thrown out.\n",
    "s.print_dynamic_results()\n",
    "\n",
    "# lets run a dynamic analysis to see what the offset would be:\n",
    "s.analyse_dynamic(critical_fos=1.4)\n",
    "s.print_dynamic_results()\n",
    "\n",
    "# note that values can be retrieved as a dictionary with s.get_dynamic_results()\n",
    "\n",
    "fig_3 = s.plot_critical()\n",
    "fig_3.show()\n",
    "\n",
    "fig_3.write_image(\"./readme_example_plot_dynamic.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s.remove_individual_planes()\n",
    "\n",
    "for r in [9,10,11]:\n",
    "    s.add_single_circular_plane(\n",
    "        c_x = 13.7,\n",
    "        c_y = 17.02,\n",
    "        radius = r,\n",
    "    )\n",
    "\n",
    "s.analyse_slope()\n",
    "\n",
    "fig = s.plot_all_planes()\n",
    "fig.show()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "c1b52088b69d1d854f5ac76f18cfe16b36af5758552e4f8ca8599a4433f081ce"
  },
  "kernelspec": {
   "display_name": "Python 3.8.5 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
