1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| import os import math import numpy as np import matplotlib.pyplot as plt import scipy
class spectrum: sw=np.array([]) v=np.array([]) su=np.array([]) tf=np.array([]) u=np.array([]) u0=np.array([]) u00=np.array([]) saT=np.array([]) sa=np.array([]) sv=np.array([]) sd=np.array([])
PGA=0 PGD=0 PGV=0 t=0 def __init__(self,path,t): file_4=open(path,"r") str4="" for p in range(0,4): str4=str4+file_4.readline() data=file_4.readlines() file_4.close() lit=[] for d in data: tmpd=d.split() for num_tmpd in range(0,len(tmpd)): lit.append(float(tmpd[num_tmpd])) self.sw=np.array(lit) self.t=t return def newmark_beta(self,T,ksi=0.05,gamma=0.5,beta=0.25): if T==0: T=0.00000001 n=len(self.sw) m=1.0 c=2*m*ksi*(2*np.pi/T) u00=np.zeros(n) u0=np.zeros(n) u=np.zeros(n) k=(2*np.pi/T)**2*m u00[0]=-self.sw[0]-c*u0[0]-k*u[0] a1=m/(beta*(self.t**2))+gamma*c/(beta*self.t) a2=m/(beta*self.t)+(gamma/beta-1)*c a3=(1/(2*beta)-1)*m+self.t*(gamma/(2*beta)-1)*c k_hat=k+a1
for i in range(1,n): p_hat=-self.sw[i]+a1*u[i-1]+a2*u0[i-1]+a3*u00[i-1] u[i]=p_hat/k_hat u0[i]=gamma/(beta*self.t)*(u[i]-u[i-1])+(1-gamma/beta)*u0[i-1]+self.t*(1-gamma/(2*beta))*u00[i-1] u00[i]=1/(beta*(self.t**2))*(u[i]-u[i-1])-u0[i-1]/(beta*self.t)-(1/(2*beta)-1)*u00[i-1] self.u=u self.u0=u0 self.u00=u00+self.sw return u,u0,u00+self.sw def central_difference(self,T,ksi=0.05,gamma=0.5,beta=0.25): m=1.0 n=len(self.sw) u00=np.zeros(n) u0=np.zeros(n) u=np.zeros(n+1) c=2*m*ksi*(2*np.pi/T) k=(2*np.pi/T)**2*m u00[0]=-self.sw[0]-c*u0[0]-k*u[0] u_1=u[0]-self.t*u0[0]+self.t**2*u00[0]/2 k_hat=m/(self.t**2)+c/(2*self.t) a=m/(self.t**2)-c/(2*self.t) b=k-2*m/(self.t**2) for i in range(n): if i==1: p_hat=-self.sw[i]-a*u_1-b*u[i] else: p_hat=-self.sw[i]-a*u[i-1]-b*u[i] u[i+1]=p_hat/k_hat u0[i]=(u[i+1]-u[i-1])/(2*self.t) u00[i]=(u[i+1]-2*u[i]+u[i-1])/(self.t**2) self.u=u[:n] self.u0=u0 self.u00=u00+self.sw return def get_PGA(self): self.PGA=max(abs(self.sw)) return max(abs(self.sw)) def get_PGV(self): self.PGV=max(abs(self.v)) return max(abs(self.v)) def get_PGD(self): self.PGD=max(abs(self.su)) return max(abs(self.su)) def get_sa(self,begin,end,step): T=np.arange(begin,end,step) sa=np.array([]) sv=np.array([]) su=np.array([]) for i in T: u,v,a=self.newmark_beta(i) sa=np.append(sa,max(abs(a))) sv=np.append(sv,max(abs(v))) su=np.append(su,max(abs(u))) self.saT=T self.sa=sa self.sd=su self.sv=sv return def get_v(self): v=[] for i in range(len(self.sw)): v.append(np.trapz(self.sw[:i+1],dx=self.t)) self.v=np.array(v) return v def get_u(self): v=self.get_v() u=[] for i in range(len(self.sw)): u.append(np.trapz(v[:i+1],dx=self.t)) self.su=np.array(u) return u def tiaofu_sa(self,T,target): u,v,a=self.newmark_beta(T) sa=max(abs(a)) self.tf=self.sw self.sw=self.sw*(target/sa) self.get_v() self.get_u() return target/sa def fypa(self,T,x,path): y=np.loadtxt(path) plt.rcParams['font.family'] = ['sans-serif'] plt.rcParams['font.sans-serif'] = ['SimHei'] plt.figure(figsize=(10,5)) plt.title("标准加速度反应谱") self.get_PGA() plt.plot(T,x/self.PGA,label="python") plt.plot(T,y/self.PGA,label="SPECTR") plt.xlabel("T(s)") plt.ylabel("$\\beta$") plt.legend() def fypv(self,T,x,path): y=np.loadtxt(path)/1000 plt.rcParams['font.family'] = ['sans-serif'] plt.rcParams['font.sans-serif'] = ['SimHei'] plt.figure(figsize=(10,5)) plt.title("标准速度反应谱") self.get_PGV() plt.plot(T,x/self.PGV,label="python") plt.plot(T,y/self.PGV,label="SPECTR") plt.xlabel("T(s)") plt.ylabel("$\\beta$") plt.legend() def fypu(self,T,x,path): y=np.loadtxt(path)/1000 plt.rcParams['font.family'] = ['sans-serif'] plt.rcParams['font.sans-serif'] = ['SimHei'] plt.figure(figsize=(10,5)) plt.title("标准位移反应谱") self.get_PGD() plt.plot(T,x/self.PGD,label="python") plt.plot(T,y/self.PGD,label="SPECTR") plt.xlabel("T(s)") plt.ylabel("$\\beta$") plt.legend() def sw_a(self): plt.rcParams['font.family'] = ['sans-serif'] plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus']=False plt.figure(figsize=(10,5)) plt.title("地震波加速度时程") t=np.linspace(0,len(self.sw)*self.t,len(self.sw)) plt.plot(t,self.sw) plt.xlabel("t(s)") plt.ylabel("a(g)")
|