{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Symbolic Elasticity Notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp\n",
    "\n",
    "from mechpy.core.symbolic.material import SymbolicIsotropicMaterial\n",
    "from mechpy.core.symbolic.coord import SymbolicCartesianCoordSystem\n",
    "from mechpy.core.symbolic.stress import SymbolicStressTensor\n",
    "from mechpy.core.symbolic.strain import SymbolicStrainTensor\n",
    "from mechpy.core.symbolic.elasticity import SymbolicLinearElasticity\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simple Traction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compliance Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SymbolicIsotropicMaterial(E=E, nu=nu)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{1}{E} & - \\frac{\\nu}{E} & - \\frac{\\nu}{E} & 0 & 0 & 0\\\\- \\frac{\\nu}{E} & \\frac{1}{E} & - \\frac{\\nu}{E} & 0 & 0 & 0\\\\- \\frac{\\nu}{E} & - \\frac{\\nu}{E} & \\frac{1}{E} & 0 & 0 & 0\\\\0 & 0 & 0 & \\frac{2 \\left(\\nu + 1\\right)}{E} & 0 & 0\\\\0 & 0 & 0 & 0 & \\frac{2 \\left(\\nu + 1\\right)}{E} & 0\\\\0 & 0 & 0 & 0 & 0 & \\frac{2 \\left(\\nu + 1\\right)}{E}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[1/E, -nu/E, -nu/E, 0, 0, 0], [-nu/E, 1/E, -nu/E, 0, 0, 0], [-nu/E, -nu/E, 1/E, 0, 0, 0], [0, 0, 0, 2*(nu + 1)/E, 0, 0], [0, 0, 0, 0, 2*(nu + 1)/E, 0], [0, 0, 0, 0, 0, 2*(nu + 1)/E]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "material_props = {\"E\": sp.symbols(\"E\"), \"nu\": sp.symbols(\"nu\")}\n",
    "material = SymbolicIsotropicMaterial(**material_props)\n",
    "display(material)\n",
    "compliance_tensor = material.compliance_tensor()\n",
    "display(compliance_tensor.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Stress Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\sigma_{11} & \\sigma_{22} & \\sigma_{33} & \\sigma_{23} & \\sigma_{13} & \\sigma_{12}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[\\sigma_11, \\sigma_22, \\sigma_33, \\sigma_23, \\sigma_13, \\sigma_12]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\sigma_{11} & 0 & 0 & 0 & 0 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[\\sigma_11, 0, 0, 0, 0, 0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "stress_tensor = SymbolicStressTensor.create(notation=\"voigt\")\n",
    "display(stress_tensor.data)\n",
    "components_values = {\n",
    "    1: 0,\n",
    "    2: 0,\n",
    "    3: 0,\n",
    "    4: 0,\n",
    "    5: 0,\n",
    "}\n",
    "stress_tensor.subs_tensor_components(components_values)\n",
    "display(stress_tensor.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\epsilon_{11} & \\epsilon_{22} & \\epsilon_{33} & 2 \\epsilon_{23} & 2 \\epsilon_{13} & 2 \\epsilon_{12}\\end{matrix}\\right] = \\left[\\begin{matrix}\\frac{\\sigma_{11}}{E} & - \\frac{\\sigma_{11} \\nu}{E} & - \\frac{\\sigma_{11} \\nu}{E} & 0 & 0 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Eq([\\epsilon_11, \\epsilon_22, \\epsilon_33, 2*\\epsilon_23, 2*\\epsilon_13, 2*\\epsilon_12], [\\sigma_11/E, -\\sigma_11*nu/E, -\\sigma_11*nu/E, 0, 0, 0])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\epsilon_{11} = \\frac{\\sigma_{11}}{E}$"
      ],
      "text/plain": [
       "Eq(\\epsilon_11, \\sigma_11/E)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\epsilon_{22} = - \\frac{\\sigma_{11} \\nu}{E}$"
      ],
      "text/plain": [
       "Eq(\\epsilon_22, -\\sigma_11*nu/E)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\epsilon_{33} = - \\frac{\\sigma_{11} \\nu}{E}$"
      ],
      "text/plain": [
       "Eq(\\epsilon_33, -\\sigma_11*nu/E)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 2 \\epsilon_{23} = 0$"
      ],
      "text/plain": [
       "Eq(2*\\epsilon_23, 0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 2 \\epsilon_{13} = 0$"
      ],
      "text/plain": [
       "Eq(2*\\epsilon_13, 0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 2 \\epsilon_{12} = 0$"
      ],
      "text/plain": [
       "Eq(2*\\epsilon_12, 0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "strain_tensor = SymbolicStrainTensor.create(notation=\"voigt\")\n",
    "strain_tensor_expr = SymbolicLinearElasticity.hookes_law(compliance_tensor, stress_tensor)\n",
    "display(sp.Equality(strain_tensor.data, strain_tensor_expr.data))\n",
    "display(*[sp.Equality(strain_tensor[_], strain_tensor_expr[_]) for _ in range(6)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simple Deformation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Stiffness Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SymbolicIsotropicMaterial(E=E, nu=nu)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{E \\left(\\nu - 1\\right)}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)} & - \\frac{E \\nu}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)} & - \\frac{E \\nu}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)} & 0 & 0 & 0\\\\- \\frac{E \\nu}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)} & \\frac{E \\left(\\nu - 1\\right)}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)} & - \\frac{E \\nu}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)} & 0 & 0 & 0\\\\- \\frac{E \\nu}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)} & - \\frac{E \\nu}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)} & \\frac{E \\left(\\nu - 1\\right)}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)} & 0 & 0 & 0\\\\0 & 0 & 0 & \\frac{E}{2 \\left(\\nu + 1\\right)} & 0 & 0\\\\0 & 0 & 0 & 0 & \\frac{E}{2 \\left(\\nu + 1\\right)} & 0\\\\0 & 0 & 0 & 0 & 0 & \\frac{E}{2 \\left(\\nu + 1\\right)}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[E*(nu - 1)/((nu + 1)*(2*nu - 1)), -E*nu/((nu + 1)*(2*nu - 1)), -E*nu/((nu + 1)*(2*nu - 1)), 0, 0, 0], [-E*nu/((nu + 1)*(2*nu - 1)), E*(nu - 1)/((nu + 1)*(2*nu - 1)), -E*nu/((nu + 1)*(2*nu - 1)), 0, 0, 0], [-E*nu/((nu + 1)*(2*nu - 1)), -E*nu/((nu + 1)*(2*nu - 1)), E*(nu - 1)/((nu + 1)*(2*nu - 1)), 0, 0, 0], [0, 0, 0, E/(2*(nu + 1)), 0, 0], [0, 0, 0, 0, E/(2*(nu + 1)), 0], [0, 0, 0, 0, 0, E/(2*(nu + 1))]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "material_props = {\"E\": sp.symbols(\"E\"), \"nu\": sp.symbols(\"nu\")}\n",
    "material = SymbolicIsotropicMaterial(**material_props)\n",
    "display(material)\n",
    "stiffness_tensor = material.stiffness_tensor()\n",
    "display(stiffness_tensor.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Strain Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\epsilon_{11} & \\epsilon_{22} & \\epsilon_{33} & 2 \\epsilon_{23} & 2 \\epsilon_{13} & 2 \\epsilon_{12}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[\\epsilon_11, \\epsilon_22, \\epsilon_33, 2*\\epsilon_23, 2*\\epsilon_13, 2*\\epsilon_12]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\epsilon_{11} & 0 & 0 & 0 & 0 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[\\epsilon_11, 0, 0, 0, 0, 0]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "strain_tensor = SymbolicStrainTensor.create(notation=\"voigt\")\n",
    "display(strain_tensor.data)\n",
    "components_values = {\n",
    "    1: 0,\n",
    "    2: 0,\n",
    "    3: 0,\n",
    "    4: 0,\n",
    "    5: 0,\n",
    "}\n",
    "strain_tensor.subs_tensor_components(components_values)\n",
    "display(strain_tensor.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\sigma_{11} & \\sigma_{22} & \\sigma_{33} & \\sigma_{23} & \\sigma_{13} & \\sigma_{12}\\end{matrix}\\right] = \\left[\\begin{matrix}\\frac{E \\epsilon_{11} \\left(\\nu - 1\\right)}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)} & - \\frac{E \\epsilon_{11} \\nu}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)} & - \\frac{E \\epsilon_{11} \\nu}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)} & 0 & 0 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Eq([\\sigma_11, \\sigma_22, \\sigma_33, \\sigma_23, \\sigma_13, \\sigma_12], [E*\\epsilon_11*(nu - 1)/((nu + 1)*(2*nu - 1)), -E*\\epsilon_11*nu/((nu + 1)*(2*nu - 1)), -E*\\epsilon_11*nu/((nu + 1)*(2*nu - 1)), 0, 0, 0])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sigma_{11} = \\frac{E \\epsilon_{11} \\left(\\nu - 1\\right)}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)}$"
      ],
      "text/plain": [
       "Eq(\\sigma_11, E*\\epsilon_11*(nu - 1)/((nu + 1)*(2*nu - 1)))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sigma_{22} = - \\frac{E \\epsilon_{11} \\nu}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)}$"
      ],
      "text/plain": [
       "Eq(\\sigma_22, -E*\\epsilon_11*nu/((nu + 1)*(2*nu - 1)))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sigma_{33} = - \\frac{E \\epsilon_{11} \\nu}{\\left(\\nu + 1\\right) \\left(2 \\nu - 1\\right)}$"
      ],
      "text/plain": [
       "Eq(\\sigma_33, -E*\\epsilon_11*nu/((nu + 1)*(2*nu - 1)))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sigma_{23} = 0$"
      ],
      "text/plain": [
       "Eq(\\sigma_23, 0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sigma_{13} = 0$"
      ],
      "text/plain": [
       "Eq(\\sigma_13, 0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sigma_{12} = 0$"
      ],
      "text/plain": [
       "Eq(\\sigma_12, 0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "stress_tensor = SymbolicStressTensor.create(notation=\"voigt\")\n",
    "stress_tensor_expr = SymbolicLinearElasticity.hookes_law_inverse(stiffness_tensor, strain_tensor)\n",
    "display(sp.Equality(stress_tensor.data, stress_tensor_expr.data))\n",
    "display(*[sp.Equality(stress_tensor[_], stress_tensor_expr[_]) for _ in range(6)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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
}