Skip to content

Commit

Permalink
update elasticity
Browse files Browse the repository at this point in the history
  • Loading branch information
obouchaara committed Jun 19, 2024
1 parent 201f758 commit b863264
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 44 deletions.
216 changes: 193 additions & 23 deletions notebooks/symbolic/elasticity.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Symblic Elasticity Notebook"
"# Symbolic Elasticity Notebook"
]
},
{
Expand Down Expand Up @@ -44,9 +44,7 @@
{
"data": {
"text/plain": [
"SymbolicIsotropicMaterial(\n",
"{'youngs_modulus': E, 'poisson_ratio': nu}\n",
")"
"SymbolicIsotropicMaterial(E=E, nu=nu)"
]
},
"metadata": {},
Expand All @@ -66,9 +64,10 @@
}
],
"source": [
"symbolic_isotropic_material = SymbolicIsotropicMaterial()\n",
"display(symbolic_isotropic_material)\n",
"compliance_tensor = symbolic_isotropic_material.compliance_tensor()\n",
"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)"
]
},
Expand All @@ -84,6 +83,18 @@
"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": [
Expand All @@ -98,15 +109,16 @@
}
],
"source": [
"stress_tensor = SymbolicStressTensor.create(notation=2)\n",
"subs_dict = {\n",
"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(subs_dict, keys=True)\n",
"stress_tensor.subs_tensor_components(components_values)\n",
"display(stress_tensor.data)"
]
},
Expand All @@ -133,12 +145,85 @@
},
"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=2)\n",
"strain_tensor = SymbolicStrainTensor.create(notation=\"voigt\")\n",
"strain_tensor_expr = SymbolicLinearElasticity.hookes_law(compliance_tensor, stress_tensor)\n",
"display(sp.Eq(strain_tensor.data, strain_tensor_expr.data))"
"display(sp.Equality(strain_tensor.data, strain_tensor_expr.data))\n",
"display(*[sp.Equality(strain_tensor[_], strain_tensor_expr[_]) for _ in range(6)])"
]
},
{
Expand All @@ -163,9 +248,7 @@
{
"data": {
"text/plain": [
"SymbolicIsotropicMaterial(\n",
"{'youngs_modulus': E, 'poisson_ratio': nu}\n",
")"
"SymbolicIsotropicMaterial(E=E, nu=nu)"
]
},
"metadata": {},
Expand All @@ -185,9 +268,10 @@
}
],
"source": [
"symbolic_isotropic_material = SymbolicIsotropicMaterial()\n",
"display(symbolic_isotropic_material)\n",
"stiffness_tensor = symbolic_isotropic_material.stiffness_tensor(lames_param=False)\n",
"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)"
]
},
Expand All @@ -203,6 +287,18 @@
"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": [
Expand All @@ -217,15 +313,16 @@
}
],
"source": [
"strain_tensor = SymbolicStrainTensor.create(notation=2)\n",
"subs_dict = {\n",
"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(subs_dict, keys=True)\n",
"strain_tensor.subs_tensor_components(components_values)\n",
"display(strain_tensor.data)"
]
},
Expand All @@ -252,12 +349,85 @@
},
"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=2)\n",
"stress_tensor = SymbolicStressTensor.create(notation=\"voigt\")\n",
"stress_tensor_expr = SymbolicLinearElasticity.hookes_law_inverse(stiffness_tensor, strain_tensor)\n",
"display(sp.Eq(stress_tensor.data, stress_tensor_expr.data))"
"display(sp.Equality(stress_tensor.data, stress_tensor_expr.data))\n",
"display(*[sp.Equality(stress_tensor[_], stress_tensor_expr[_]) for _ in range(6)])"
]
},
{
Expand Down
12 changes: 2 additions & 10 deletions src/mechpy/core/symbolic/elasticity.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,13 @@ def hookes_law(
compliance_tensor: SymbolicComplianceTensor,
stress_tensor: SymbolicStressTensor,
) -> SymbolicStrainTensor:
compliance_matrix = sp.Matrix(compliance_tensor.data.tolist())
strain_matrix = sp.Matrix(stress_tensor.data.tolist())
strain_matrix = compliance_matrix @ strain_matrix
strain_data = sp.ImmutableDenseNDimArray(strain_matrix, (6,))
strain_tensor = SymbolicStressTensor(strain_data, notation=2)
strain_tensor = compliance_tensor @ stress_tensor
return strain_tensor

@staticmethod
def hookes_law_inverse(
stiffness_tensor: SymbolicStiffnessTensor,
strain_tensor: SymbolicStrainTensor,
) -> SymbolicStressTensor:
stiffness_matrix = sp.Matrix(stiffness_tensor.data.tolist())
strain_matrix = sp.Matrix(strain_tensor.data.tolist())
stress_matrix = stiffness_matrix @ strain_matrix
stress_data = sp.ImmutableDenseNDimArray(stress_matrix, (6,))
stress_tensor = SymbolicStressTensor(stress_data, notation=2)
stress_tensor = stiffness_tensor @ strain_tensor
return stress_tensor
27 changes: 16 additions & 11 deletions src/mechpy/core/symbolic/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,22 @@ def subs(self, subs_dict):

def __matmul__(self, other):
if not isinstance(other, SymbolicTensor):
raise ValueError("The other operand must be an instance of SymbolicTensor")

if not (self.is_second_rank() and other.is_second_rank()):
raise ValueError("Requires second rank tensors")

if self.data.shape[1] != other.data.shape[0]:
raise ValueError(
"Shapes are not aligned for matrix multiplication: "
f"{self.data.shape} and {other.data.shape}"
)
result_data = sp.NDimArray(self.to_matrix() @ other.to_matrix())
raise ValueError("The other operand must be an instance of SymbolicTensor.")

if self.data.shape == (6, 6) and other.data.shape == (6,):
result = self.to_matrix() @ sp.Matrix(other.data.tolist())
result_data = sp.NDimArray(result, shape=(6,))

elif self.is_second_rank() and other.is_second_rank():
if self.data.shape[1] != other.data.shape[0]:
raise ValueError(
"Shapes are not aligned for matrix multiplication: "
f"{self.data.shape} and {other.data.shape}"
)
result = self.to_matrix() @ other.to_matrix()
result_data = sp.NDimArray(result)
else:
raise NotImplementedError()
return SymbolicTensor(result_data)

def __getitem__(self, key):
Expand Down
Loading

0 comments on commit b863264

Please sign in to comment.