Files
MechanicalNotes/笔记/_配图脚本/gen_figures_elastic_plastic.py

141 lines
7.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""弹性/塑性变形速查图:数学模型绘制,非材料试验数据。
运行后在 笔记/基础概念速查/images/ 生成 300 dpi PNG 和 SVG。
"""
from pathlib import Path
import json
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib import font_manager
from matplotlib.ticker import MultipleLocator
import numpy as np
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / '基础概念速查' / 'images'
OUT.mkdir(parents=True, exist_ok=True)
for p in [Path('C:/Windows/Fonts/msyh.ttc'), Path('C:/Windows/Fonts/simhei.ttf')]:
if p.exists():
font_manager.fontManager.addfont(str(p))
plt.rcParams.update({'font.sans-serif':['Microsoft YaHei','SimHei'],
'axes.unicode_minus':False, 'font.size':12,
'axes.titlesize':15, 'axes.labelsize':13,
'svg.fonttype':'path','figure.facecolor':'white',
'savefig.facecolor':'white'})
BLUE, ORANGE, GRAY = '#0072B2', '#D55E00', '#68737D'
E, SY, H = 200000.0, 250.0, 1500.0
EY, EA, EP = SY/E, 0.0008, 0.02
SP = SY+H*(EP-EY)
REC, RES = SP/E, EP-SP/E
assert np.isclose(RES+REC,EP)
assert np.isclose(E*EA,160)
assert np.isclose(E*REC,SP)
def style(ax):
ax.spines[['top','right']].set_visible(False)
ax.grid(axis='y',alpha=.16,linewidth=.7)
ax.set_axisbelow(True)
ax.set_xlabel(r'应变 $\varepsilon$(%)')
ax.set_ylabel(r'应力 $\sigma$(MPa)')
def save(fig, name):
fig.savefig(OUT/f'{name}.png', dpi=300)
fig.savefig(OUT/f'{name}.svg')
# Smaller review copy, excluded from user-facing output directory.
review=ROOT.parent/'.workbuddy'/'elastic_plastic_review'
review.mkdir(parents=True,exist_ok=True)
fig.savefig(review/f'{name}.png',dpi=110)
plt.close(fig)
def dimension(ax, x0,x1,y,label,color):
ax.annotate('',xy=(x1,y),xytext=(x0,y),
arrowprops={'arrowstyle':'<->','color':color,'lw':1.5})
ax.text((x0+x1)/2,y-12,label,ha='center',va='top',fontsize=11,color=color)
fig, axes=plt.subplots(1,2,figsize=(14,7.5))
fig.subplots_adjust(left=.075,right=.975,top=.80,bottom=.31,wspace=.30)
fig.suptitle('弹性变形与塑性变形:关键看卸载后能否恢复',fontsize=21,y=.96)
fig.text(.5,.89,'数学教学模型:E = 200 GPa,屈服应力 250 MPa;塑性段采用线性强化,非实测数据。',ha='center',fontsize=11,color=GRAY)
ax=axes[0];style(ax)
ax.set_title('A 屈服前加载,再卸载',loc='left',pad=17)
x=np.linspace(0,EY,160)
ax.plot(x*100,E*x,color=BLUE,lw=2.7)
ax.plot([0,EA*100],[0,E*EA],color=ORANGE,lw=2,ls='--')
ax.scatter([0,EA*100,EY*100],[0,E*EA,SY],color=[ORANGE,BLUE,GRAY],s=40,zorder=5)
ax.annotate('加载到 A\n160 MPa,0.080%',xy=(EA*100,E*EA),xytext=(.012,207),
arrowprops={'arrowstyle':'->','color':BLUE},fontsize=12)
ax.annotate('卸载沿原线返回 O',xy=(.035,70),xytext=(.063,45),
arrowprops={'arrowstyle':'->','color':ORANGE},fontsize=11,color=ORANGE)
ax.annotate('理想模型屈服点',xy=(EY*100,SY),xytext=(.067,285),
arrowprops={'arrowstyle':'->','color':GRAY},fontsize=10,color=GRAY)
ax.text(.01,8,'O',fontsize=12)
ax.set(xlim=(-.007,.155),ylim=(-8,310))
ax.xaxis.set_major_locator(MultipleLocator(.04))
ax.text(.5,-.31,'撤去载荷后:应变回到 0\n本模型中变形全部恢复',transform=ax.transAxes,ha='center',va='top',fontsize=13,color=BLUE,linespacing=1.7)
ax=axes[1];style(ax)
ax.set_title('B 超过屈服,产生塑性变形后卸载',loc='left',pad=17)
x=np.linspace(0,EP,1000)
y=np.where(x<=EY,E*x,SY+H*(x-EY))
ax.plot(x*100,y,color=BLUE,lw=2.7,label='加载路径')
ax.plot([EP*100,RES*100],[SP,0],color=ORANGE,lw=2.4,ls='--',label='弹性卸载路径')
ax.plot([EP*100,EP*100],[0,SP],color=GRAY,lw=.9,ls=':')
ax.scatter([EP*100,RES*100],[SP,0],s=42,color=[BLUE,ORANGE],zorder=6)
ax.annotate(f'P:总应变 2.000%\n应力 {SP:.1f} MPa',xy=(EP*100,SP),xytext=(.52,325),
arrowprops={'arrowstyle':'->','color':BLUE},fontsize=12)
ax.annotate('沿斜率 E 卸载',xy=((EP+RES)*50,SP/2),xytext=(.62,140),
arrowprops={'arrowstyle':'->','color':ORANGE},fontsize=12,color=ORANGE)
dimension(ax,0,RES*100,-28,r'塑性残余 $\varepsilon_p$ = 1.8609%',ORANGE)
ax.annotate('',xy=(EP*100,-28),xytext=(RES*100,-28),arrowprops={'arrowstyle':'<->','color':BLUE,'lw':1.5})
ax.annotate('弹性恢复\n'+r'$\varepsilon_e$ = 0.1391%',xy=((EP+RES)*50,-28),xytext=(1.28,-98),
arrowprops={'arrowstyle':'->','color':BLUE},fontsize=11,color=BLUE,ha='center')
ax.text(RES*100,-4,'R',ha='right',va='top',fontsize=11,color=ORANGE)
ax.set(xlim=(-.08,2.32),ylim=(-125,365))
ax.set_yticks([0,100,200,300]);ax.set_xticks([0,.5,1,1.5,2])
ax.legend(loc='upper left',bbox_to_anchor=(.01,.77),frameon=False,fontsize=10)
ax.text(.5,-.31,r'$\varepsilon_{\mathrm{total}}=\varepsilon_p+\varepsilon_e$'+'\n卸载后应力为 0,残余应变仍大于 0',
transform=ax.transAxes,ha='center',va='top',fontsize=13,linespacing=1.7)
fig.text(.5,.045,'说明:左右图的横轴范围不同;塑性阶段仍有弹性变形。卸载线与初始弹性段斜率相同,不是水平卸载。',ha='center',fontsize=11,color=GRAY)
save(fig,'figE01_弹性与塑性卸载对比')
# Idealized engineering stress-strain diagram with a yield plateau.
# E = 200 GPa; a simplified plateau replaces upper/lower yield details.
e0=np.linspace(0,EY,100);s0=E*e0
e1=np.linspace(EY,.018,120);s1=np.full_like(e1,SY)
e2=np.linspace(.018,.18,360);u=(e2-.018)/(.18-.018)
s2=SY+200*(2*u-u*u)
e3=np.linspace(.18,.28,200);v=(e3-.18)/.10
s3=450-100*v*v
xx=np.r_[e0,e1,e2,e3]*100;yy=np.r_[s0,s1,s2,s3]
fig,ax=plt.subplots(figsize=(13,7.8))
fig.subplots_adjust(left=.09,right=.97,top=.80,bottom=.27)
fig.suptitle('屈服强度与抗拉强度:在拉伸曲线上分别看哪里',fontsize=21,y=.96)
fig.text(.5,.89,'有明显屈服现象材料的工程应力—应变示意;数值仅用于绘图,不代表 8.8 级螺栓。',ha='center',fontsize=11,color=GRAY)
style(ax)
ax.plot(xx,yy,color=BLUE,lw=3)
ax.hlines([250,450],0,[2.0,18],color=GRAY,ls='--',lw=1)
ax.scatter([18,28],[450,350],color=[ORANGE,GRAY],s=45,zorder=5)
ax.annotate(r'$\sigma_s$:屈服强度'+'\n开始出现明显塑性变形',xy=(1.0,250),xytext=(3.5,300),
arrowprops={'arrowstyle':'->','color':ORANGE},fontsize=13,color=ORANGE)
ax.annotate(r'$\sigma_b=F_{\max}/A_0$'+'\n抗拉强度:曲线最高点',xy=(18,450),xytext=(20,480),
arrowprops={'arrowstyle':'->','color':ORANGE},fontsize=13,color=ORANGE)
ax.annotate('断裂点\n不等于抗拉强度点',xy=(28,350),xytext=(23,210),
arrowprops={'arrowstyle':'->','color':GRAY},fontsize=12,color=GRAY)
ax.text(8.6,350,'应变强化',fontsize=13,color=BLUE)
ax.text(19.5,310,'颈缩后工程应力下降',fontsize=11,color=BLUE)
ax.set(xlim=(0,30),ylim=(0,550))
ax.set_xticks([0,5,10,15,20,25,30]);ax.set_yticks([0,100,250,350,450,550])
inset=ax.inset_axes([.075,.14,.24,.24])
inset.plot([0,.125,.20],[0,250,250],color=BLUE,lw=2)
inset.set(xlim=(0,.20),ylim=(0,280),xticks=[0,.125,.20],yticks=[0,250])
inset.tick_params(labelsize=9);inset.spines[['top','right']].set_visible(False)
inset.set_title('弹性段局部放大',fontsize=10,pad=5)
inset.set_xlabel('应变(%)',fontsize=9,labelpad=2)
inset.text(.035,80,r'$\sigma=E\varepsilon$',fontsize=10,rotation=51)
fig.text(.10,.15,r'工程应力 $\sigma=F/A_0$;工程应变 $\varepsilon=\Delta L/L_0$。$A_0$、$L_0$ 为原始面积与标距。',fontsize=12)
fig.text(.10,.105,'屈服以后已经会留下永久变形;不必等到抗拉强度点或断裂点才出现塑性变形。',fontsize=12)
fig.text(.10,.05,'模型简化:省略上下屈服点细节。无明显屈服点的材料常用规定塑性延伸强度(如 Rp0.2);本图下降段为工程应力。',fontsize=10,color=GRAY)
save(fig,'figE02_屈服强度与抗拉强度')
print(json.dumps({'E_MPa':E,'yield_MPa':SY,'unload_stress_MPa':SP,
'total_strain':EP,'elastic_recovery':REC,'plastic_residual':RES,
'output':str(OUT)},ensure_ascii=False,indent=2))