我正在编写一个代码来读取和显示有限元分析(FEA)计算的结果.结果存储在几个(相对较大的)文本文件中,这些文件包含节点列表(ID号,空间位置)和相关物理字段列表(节点ID,该点上字段的值).
但是,我注意到当我在后台运行FEA案例并且我尝试同时运行我的代码时它返回错误,并不总是相同的并且不总是在同一个迭代中,所有这些似乎都是随机的无需对代码或输入文件进行任何修改,只需在运行之间按秒运行RUN按钮即可.
我得到的错误示例如下:
keys[key] = np.round(np.asarray(keys[key]),7)
TypeError: can't multiply sequence by non-int of type 'float'
#-------------------------------------------------------------------------
triang = tri.Triangulation(x, y)
ValueError: x and y arrays must have a length of at least 3
#-------------------------------------------------------------------------
line = [float(n) for n in line]
ValueError: could not convert string to float: '0.1225471E'
Run Code Online (Sandbox Code Playgroud)
如果你很好奇,这是我的代码(请记住,它还没有完成,我是一名机械工程师,而不是程序员).任何有关如何使其更好的反馈也值得赞赏:
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
import os
triangle_max_radius = 0.003
respath = 'C:/path'
fields = ['TEMPERATURE']
# Plot figure definition --------------------------------------------------------------------------------------
fig, ax1 = plt.subplots()
fig.subplots_adjust(left=0, right=1, bottom=0.04, top=0.99)
ax1.set_aspect('equal')
# -------------------------------------------------------------------------------------------------------------
# Read outputfiles --------------------------------------------------------------------------------------------
resfiles = [f for f in os.listdir(respath) if (os.path.isfile(os.path.join(respath,f)) and f[:3]=='csv')]
resfiles = [[f,int(f[4:])] for f in resfiles]
resfiles = sorted(resfiles,key=lambda x: (x[1]))
resfiles = [os.path.join(respath,f[:][0]).replace("\\","/") for f in resfiles]
# -------------------------------------------------------------------------------------------------------------
# Read data inside outputfile ---------------------------------------------------------------------------------
for result_file in resfiles:
keys = {}
keywords = []
with open(result_file, 'r') as res:
for line in res:
if line[0:2] == '##':
if len(line) >= 5:
line = line[:3] + line[7:]
line = line.replace(';',' ')
line = line.split()
if line:
if line[0] == '##':
if len(line) >= 3:
keywords.append(line[1])
keys[line[1]] = []
elif line[0] in keywords:
curr_key = line[0]
else:
line = [float(n) for n in line]
keys[curr_key].append(line)
for key in keys:
keys[key] = np.round(np.asarray(keys[key]),7)
for item in fields:
gob_temp = np.empty((0,4))
for node in keys[item]:
temp_coords, = np.where(node[0] == keys['COORDINATES'][:,0])
gob_temp_coords = [node[0], keys['COORDINATES'][temp_coords,1], keys['COORDINATES'][temp_coords,2], node[1]]
gob_temp = np.append(gob_temp,[gob_temp_coords],axis=0)
x = gob_temp[:,1]
y = gob_temp[:,2]
z = gob_temp[:,3]
triang = tri.Triangulation(x, y)
triangles = triang.triangles
xtri = x[triangles] - np.roll(x[triangles], 1, axis=1)
ytri = y[triangles] - np.roll(y[triangles], 1, axis=1)
maxi = np.max(np.sqrt(xtri**2 + ytri**2), axis=1)
triang.set_mask(maxi > triangle_max_radius)
ax1.tricontourf(triang,z,100,cmap='plasma')
ax1.triplot(triang,color="black",lw=0.2)
plt.show()
Run Code Online (Sandbox Code Playgroud)
回到这个问题,python的准确性/性能是否可能受到CPU负载或任何其他"外部"因素的影响?或者这不是一个选项,我的代码肯定有问题(顺便说一下,在其他情况下效果很好)?
不,其他流程只影响您的流程执行时隙的频率 - 即,从用户的角度来看,它完成工作的速度有多快.
如果您在负载下遇到错误,这意味着您的程序逻辑中存在错误 - 最有可能的是竞争条件.它们基本上归结为对您的环境做出假设,当其中还有其他活动时,这些假设不再适用.例如:
| 归档时间: |
|
| 查看次数: |
100 次 |
| 最近记录: |