Skip to content

Instantly share code, notes, and snippets.

@nicoguaro
Last active July 28, 2022 11:12
Show Gist options
  • Save nicoguaro/d9a77d5a7819e801c5b4 to your computer and use it in GitHub Desktop.
Save nicoguaro/d9a77d5a7819e801c5b4 to your computer and use it in GitHub Desktop.
Scripts to plot Mohr circles.
"""
Mohr circle in 2D.
@author: Nicolás Guarín-Zapata
@date: May 2020
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['font.family'] = 'serif'
rcParams['font.size'] = 16
def mohr(S):
"""Plot Mohr circle for a 2D tensor"""
S11 = S[0][0]
S12 = S[0][1]
S22 = S[1][1]
center = [(S11 + S22)/2.0, 0.0]
radius = np.sqrt((S11 - S22)**2/4.0 + S12**2)
Smin = center[0] - radius
Smax = center[0] + radius
print("Minimum Normal Stress: %g" % Smin)
print("Maximum Normal Stress: %g" % Smax)
print("Average Normal Stress: %g" % center[0])
print("Minimum Shear Stress: %g" % -radius)
print("Maximum Shear Stress: %g" % radius)
circ = plt.Circle((center[0],0), radius, facecolor='#cce885', lw=3,
edgecolor='#5c8037')
plt.axis('image')
ax = plt.gca()
ax.add_artist(circ)
ax.set_xlim(Smin - .1*radius, Smax + .1*radius)
ax.set_ylim(-1.1*radius, 1.1*radius)
plt.plot([S22, S11], [S12, -S12], 'ko')
plt.plot([S22, S11], [S12, -S12], 'k')
plt.plot(center[0], center[1], 'o', mfc='w')
plt.text(S22 + 0.1*radius, S12, 'A')
plt.text(S11 + 0.1*radius, -S12, 'B')
plt.xlabel(r"$\sigma$", size=18)
plt.ylabel(r"$\tau$", size=18)
plt.show()
return None
S = np.array([[1.0, 2.0],[2.0, -3.0]])
mohr(S)
plt.figure()
S2 = np.array([[2.0, 3.0],[3.0, 1.0]])
mohr(S2)
plt.show()
"""
Mohr circle in 3D.
@author: Nicolás Guarín-Zapata
@date: May 2020
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import eigvalsh
from matplotlib import rcParams
rcParams['font.family'] = 'serif'
rcParams['font.size'] = 16
def plot_mohr3d(S):
r"""Plot 3D Mohr circles."""
S3, S2, S1 = eigvalsh(S)
R_maj = 0.5*(S1 - S3)
cent_maj = 0.5*(S1+S3)
R_min = 0.5*(S2 - S3)
cent_min = 0.5*(S2 + S3)
R_mid = 0.5*(S1 - S2)
cent_mid = 0.5*(S1 + S2)
circ1 = plt.Circle((cent_maj,0), R_maj, facecolor='#cce885', lw=3,
edgecolor='#5c8037')
circ2 = plt.Circle((cent_min,0), R_min, facecolor='w', lw=3,
edgecolor='#15a1bd')
circ3 = plt.Circle((cent_mid,0), R_mid, facecolor='w', lw=3,
edgecolor='#e4612d')
plt.axis('image')
ax = plt.gca()
ax.add_artist(circ1)
ax.add_artist(circ2)
ax.add_artist(circ3)
ax.set_xlim(S3 - .1*R_maj, S1 + .1*R_maj)
ax.set_ylim(-1.1*R_maj, 1.1*R_maj)
plt.xlabel(r"$\sigma$", size=18)
plt.ylabel(r"$\tau$", size=18)
#plt.savefig('Mohr_circle_3D.svg')
plt.show()
S = np.array([
[90,0,95],
[0,96,0],
[95,0,-50]])
plt.figure()
plot_mohr3d(S)
S2 = np.array([
[1,0,0],
[0,2,0],
[0,0,4]])
plt.figure()
plot_mohr3d(S2)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment