for k in range(0, len(qq)): # 从1开始 计算扩散 cc = gaussnmdl_ins(qq[k], tt[k], t_end, umean, h_leak, sigmax, sigmay, sigmaz, X, Y, h_observe, inverse, hi=10e50) # 计算扩散浓度 cc1 = np.flip(cc, axis=0) cc = np.concatenate((cc, cc1), axis=0) total_leak = cc.sum() # 对每个网格的浓度进行求和 cc[cc >= uel] = 0 cc[cc <= 0.9 * lel] = 0 ccb = cc.copy() part_leak = ccb.sum() x1 = ccb.sum(axis=0) y1 = np.arange(-len(x1) / 2, len(x1) / 2) c1 = x1 * y1 numerator = c1.sum() denominator = cc.sum() + 1e-6 # 防止出现0 if numerator > 0: O = (numerator / denominator) * (rangex * 2 / gridnumber) # 计算参与爆炸的蒸汽云的质心坐标,防止分母为0 r = part_leak / (total_leak + 1e-6) Mg = qq[k] * r * 0.2 else: O = (numerator / denominator) * (rangex * 2 / gridnumber) Mg = 1e-6 # 计算超压值 Pa = Pa_vce(x, y, O, Hc, H_TNT, Mg, fe_del) Pa_arr.append(Pa) MG.append("距离") distance = np.sqrt((x - O) ** 2 + y ** 2) MG.append(format(distance, ".1f")) MG.append("蒸汽云质量") MG.append(format(Mg, ".1f")) # 计算概率与后果 pign = pdel * p_fire # 本阶段的点火概率 p += pign MG.append("点火概率" + format(pign, ".2g")) p_up, ttf = P_upgrade("超压", tank2_pressure, Pa, tank2_V) p_add += pign * p_up MG.append("累计概率" + str(p_add)) pdel = 1 - p 从python语法角度这段代码怎么优化
时间: 2024-01-24 19:03:59 浏览: 68
从语法角度来看,这段代码的优化建议如下:
1. 减少不必要的括号和空格,使代码更加简洁易读。
2. 尽量使用向量化计算,避免循环,提高计算效率。
3. 使用更加具有表现力的变量名,增加代码的可读性。
4. 使用常量或者全局变量代替重复计算的值,减少计算量。
5. 使用列表推导式代替循环生成列表,提高代码的效率。
6. 将代码分成多个函数,使得每个函数只完成一个功能,提高代码的可维护性。
下面是对应的优化代码:
```python
def calculate_diffusion(qq, tt, t_end, umean, h_leak, sigmax, sigmay, sigmaz, X, Y, h_observe, inverse, hi):
cc_arr = []
for q, t in zip(qq, tt):
cc = gaussnmdl_ins(q, t, t_end, umean, h_leak, sigmax, sigmay, sigmaz, X, Y, h_observe, inverse, hi)
cc1 = np.flip(cc, axis=0)
cc = np.concatenate((cc, cc1), axis=0)
cc[cc >= uel] = 0
cc[cc <= 0.9 * lel] = 0
cc_arr.append(cc)
return cc_arr
def calculate_leak(cc_arr):
total_leak_arr = []
part_leak_arr = []
for cc in cc_arr:
total_leak = cc.sum()
part_leak = cc.copy().sum()
total_leak_arr.append(total_leak)
part_leak_arr.append(part_leak)
return total_leak_arr, part_leak_arr
def calculate_mg(qq, total_leak_arr, part_leak_arr, MG):
for q, total_leak, part_leak in zip(qq, total_leak_arr, part_leak_arr):
numerator = ((cc := np.sum(cc_arr, axis=0)) * (y1 := np.arange(-len(cc) / 2, len(cc) / 2))).sum()
denominator = cc.sum() + 1e-6
O = (numerator / denominator) * (rangex * 2 / gridnumber)
r = q * part_leak / (total_leak + 1e-6)
Mg = r * 0.2 if numerator > 0 else 1e-6
MG.extend(["距离", format(np.sqrt((x - O) ** 2 + y ** 2), ".1f"), "蒸汽云质量", format(Mg, ".1f")])
return MG
def calculate_pa(qq, total_leak_arr, part_leak_arr, MG):
Pa_arr = []
for q, total_leak, part_leak in zip(qq, total_leak_arr, part_leak_arr):
numerator = ((cc := np.sum(cc_arr, axis=0)) * (y1 := np.arange(-len(cc) / 2, len(cc) / 2))).sum()
denominator = cc.sum() + 1e-6
O = (numerator / denominator) * (rangex * 2 / gridnumber)
Mg = r * 0.2 if numerator > 0 else 1e-6
Pa = Pa_vce(x, y, O, Hc, H_TNT, Mg, fe_del)
Pa_arr.append(Pa)
MG.extend(["点火概率" + format(pign := pdel * p_fire, ".2g"), "累计概率" + str(p_add := p_add + pign * P_upgrade("超压", tank2_pressure, Pa, tank2_V)[0])])
pdel = 1 - p
return Pa_arr, MG
cc_arr = calculate_diffusion(qq, tt, t_end, umean, h_leak, sigmax, sigmay, sigmaz, X, Y, h_observe, inverse, hi)
total_leak_arr, part_leak_arr = calculate_leak(cc_arr)
MG = calculate_mg(qq, total_leak_arr, part_leak_arr, MG)
Pa_arr, MG = calculate_pa(qq, total_leak_arr, part_leak_arr, MG)
```
阅读全文