Demo: Deformation of a Unit Square
Colab note: This notebook is designed to run on Google Colab. The first code cell installs dependencies.
Source
# Install dependencies (for Google Colab or missing packages)
import sys
# Check if running in Colab
try:
import google.colab
IN_COLAB = True
print("Running in Google Colab")
except:
IN_COLAB = False
print("Running in local environment")
# Install required packages if needed
required_packages = {
'numpy': 'numpy',
'matplotlib': 'matplotlib',
'ipywidgets': 'ipywidgets'
}
missing_packages = []
for package, pip_name in required_packages.items():
try:
__import__(package)
print(f"✓ {package} is already installed")
except ImportError:
missing_packages.append(pip_name)
print(f"✗ {package} not found")
if missing_packages:
print(f"\nInstalling missing packages: {', '.join(missing_packages)}")
import subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q"] + missing_packages)
print("✓ Installation complete!")
else:
print("\n✓ All required packages are installed!")Source
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interact, FloatSliderDefine the unit square.
def unit_square():
return np.array([
[0,0],
[1,0],
[1,1],
[0,1],
[0,0]
])Define strain and rotation tensor.
def build_tensors(exx=0, ezz=0, exz=0, omega=0):
# Symmetric strain tensor
E = np.array([
[exx, exz],
[exz, ezz]
])
# Antisymmetric rotation tensor
W = np.array([
[0, -omega],
[omega, 0]
])
I = np.eye(2)
F = I + E + W
return E, W, FApply Deformation
def deform(points, F):
return points @ F.TPlot the deformation.
def plot_deformation(exx=0, ezz=0, exz=0, omega=0):
pts = unit_square()
E, W, F = build_tensors(exx, ezz, exz, omega)
pts_strain = deform(pts, np.eye(2) + E)
pts_rot = deform(pts, np.eye(2) + W)
pts_total = deform(pts, F)
fig, ax = plt.subplots(figsize=(7,7))
ax.plot(pts[:,0], pts[:,1],'w--',linewidth=2,label='Undeformed')
ax.plot(pts_strain[:,0], pts_strain[:,1],linewidth=2,label='Strain')
ax.plot(pts_rot[:,0], pts_rot[:,1],linewidth=2,label='Rotation')
ax.plot(pts_total[:,0], pts_total[:,1],linewidth=3,label='Total')
ax.set_aspect('equal')
ax.grid(True,alpha=.3)
ax.set_xlabel("x")
ax.set_ylabel("z")
ax.set_title("Strain and Rotation Effects on a Unit Square")
ax.legend()
plt.show()interact(
plot_deformation,
exx=FloatSlider(min=-0.5,max=0.5,step=0.01,value=0),
ezz=FloatSlider(min=-0.5,max=0.5,step=0.01,value=0),
exz=FloatSlider(min=-0.5,max=0.5,step=0.01,value=0),
omega=FloatSlider(min=-0.5,max=0.5,step=0.01,value=0)
);Loading...