{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Symbolic Displacement Notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp\n",
    "\n",
    "from mechpy.core.symbolic.coord import (\n",
    "    SymbolicCartesianCoordSystem,\n",
    "    SymbolicCylindricalCoordSystem,\n",
    ")\n",
    "from mechpy.core.symbolic.displacement import SymbolicDisplacement"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cartesian Coord"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}x + 2 y + 3 z & 4 x + 5 y + 6 z & 7 x + 8 y + 9 z\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[x + 2*y + 3*z, 4*x + 5*y + 6*z, 7*x + 8*y + 9*z]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data = sp.Array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "displacement_field = SymbolicDisplacement.create_linear(data=data)\n",
    "display(displacement_field.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 5 & 9 & 6 & 14 & 10\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[1, 5, 9, 6, 14, 10]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 5 & 9\\\\5 & 3 & 7\\\\9 & 7 & 5\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[1, 5, 9], [5, 3, 7], [9, 7, 5]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "strain_tensor = displacement_field.strain_tensor()\n",
    "display(strain_tensor.data)\n",
    "display(strain_tensor.to_general().data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cylindrical Coord"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sqrt{x^{2} + y^{2}}$"
      ],
      "text/plain": [
       "sqrt(x**2 + y**2)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\operatorname{atan}_{2}{\\left(y,x \\right)}$"
      ],
      "text/plain": [
       "atan2(y, x)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle z$"
      ],
      "text/plain": [
       "z"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cartesian_system = SymbolicCartesianCoordSystem()\n",
    "coord_system = cartesian_system.to_cylindrical()\n",
    "display(*coord_system.basis)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}x^{2} + y^{2} + z & \\operatorname{atan}_{2}{\\left(y,x \\right)} & z + \\sqrt{x^{2} + y^{2}}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[x**2 + y**2 + z, atan2(y, x), z + sqrt(x**2 + y**2)]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x1, x2, x3 = coord_system.basis\n",
    "data = sp.NDimArray([x1*x1+x3, x2, x1+x3])\n",
    "displacement_field = SymbolicDisplacement(coord_system=cartesian_system, data=data)\n",
    "display(displacement_field.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}2 x & \\frac{x}{x^{2} + y^{2}} & 1\\\\\\frac{x}{x^{2} + y^{2}} & y - \\frac{y}{2 \\left(x^{2} + y^{2}\\right)} & \\frac{y}{2 \\sqrt{x^{2} + y^{2}}}\\\\1 & \\frac{y}{2 \\sqrt{x^{2} + y^{2}}} & \\frac{x}{2 \\sqrt{x^{2} + y^{2}}} + \\frac{1}{2}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[2*x, x/(x**2 + y**2), 1], [x/(x**2 + y**2), y - y/(2*(x**2 + y**2)), y/(2*sqrt(x**2 + y**2))], [1, y/(2*sqrt(x**2 + y**2)), x/(2*sqrt(x**2 + y**2)) + 1/2]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "strain_tensor = displacement_field.strain_tensor(cartesian_system)\n",
    "display(strain_tensor.to_general().data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(x, y, z)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(r, theta, z)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(x(x, y), theta(x), z)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cartesian_system = SymbolicCartesianCoordSystem()\n",
    "display(cartesian_system.basis)\n",
    "x1, x2, x3 = cartesian_system.basis\n",
    "cylindrical_system = SymbolicCylindricalCoordSystem()\n",
    "display(cylindrical_system.basis)\n",
    "y1, y2, y3 = cylindrical_system.basis\n",
    "custom_basis = sp.Function(x1)(x1, x2), sp.Function(y2)(x1), y3\n",
    "display(custom_basis)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SymbolicCylindricalCoordSystem(origin=(0, 0, 0), basis=(x(x, y), theta(x), z))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}x^{2}{\\left(x,y \\right)} & \\theta{\\left(x \\right)} + x{\\left(x,y \\right)} & z^{2}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[x(x, y)**2, theta(x) + x(x, y), z**2]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "custom_cylindrical_system = SymbolicCylindricalCoordSystem(basis=custom_basis)\n",
    "display(custom_cylindrical_system)\n",
    "x1, x2, x3 = custom_cylindrical_system.basis\n",
    "data = sp.NDimArray([x1 * x1, x1 + x2, x3 * x3])\n",
    "displacement_field = SymbolicDisplacement.create(\n",
    "    coord_system=custom_cylindrical_system,\n",
    "    data=data,\n",
    "    symbols_validation=False,\n",
    ")\n",
    "display(displacement_field.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}2 x{\\left(x,y \\right)} \\frac{\\partial}{\\partial x} x{\\left(x,y \\right)} & \\frac{\\partial}{\\partial y} x{\\left(x,y \\right)} & 2 z & 2 x{\\left(x,y \\right)} \\frac{\\partial}{\\partial y} x{\\left(x,y \\right)} + \\frac{d}{d x} \\theta{\\left(x \\right)} + \\frac{\\partial}{\\partial x} x{\\left(x,y \\right)} & 0 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[2*x(x, y)*Derivative(x(x, y), x), Derivative(x(x, y), y), 2*z, 2*x(x, y)*Derivative(x(x, y), y) + Derivative(theta(x), x) + Derivative(x(x, y), x), 0, 0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}2 x{\\left(x,y \\right)} \\frac{\\partial}{\\partial x} x{\\left(x,y \\right)} & \\frac{\\partial}{\\partial y} x{\\left(x,y \\right)} & 2 z\\\\\\frac{\\partial}{\\partial y} x{\\left(x,y \\right)} & x{\\left(x,y \\right)} \\frac{\\partial}{\\partial y} x{\\left(x,y \\right)} + \\frac{\\frac{d}{d x} \\theta{\\left(x \\right)}}{2} + \\frac{\\frac{\\partial}{\\partial x} x{\\left(x,y \\right)}}{2} & 0\\\\2 z & 0 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[2*x(x, y)*Derivative(x(x, y), x), Derivative(x(x, y), y), 2*z], [Derivative(x(x, y), y), x(x, y)*Derivative(x(x, y), y) + Derivative(theta(x), x)/2 + Derivative(x(x, y), x)/2, 0], [2*z, 0, 0]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "strain_tensor = displacement_field.strain_tensor(cartesian_system)\n",
    "display(strain_tensor.data)\n",
    "display(strain_tensor.to_general().data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}