我创建了一个class capacity
,它通过迭代值x
到for-loop
进行一些计算。计算过程没有问题。我只需要打破或停止方程,当这个条件匹配。在
self.Nrd= Ned
为了得到上述结果,我创建了以下条件:
^{pr2}$但这并不能产生一个好的和令人满意的结果,最好的说法是没有真正有效地工作。在
我希望你能帮助我,当达到Nrd=Ned条件时,停止方程。在
第二个问题是,我并没有用__str__
方法得到任何东西。值如何返回?在
代码:
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
class capacity:
def __init__(self,b,h,y,ecu,cb,ct,fyd,fcd,Esd,Ned):
d = h - cb
ds = ct
Ast = int(3.1416/4*(y)**2*6.67)
Asb = int(3.1416/4*(y)**2*6.67)
for x in np.linspace(1,h,10000):
esc=ecu/x*(ds-x)
es=ecu/x*(d-x)
sisc=Esd*esc
sis=min(Esd*es,fyd)
if sisc <= -fyd:
sisc=-fyd
elif sisc >= -fyd and sisc < 0:
sisc= sisc
else:
sisc=min(sisc,fyd)
self.Nrd=int((0.8*x*b*fcd+Ast*sisc-Asb*sis)/1000)
self.Mrd=(0.8*x*b*fcd*(h/2-0.4*x)+Ast*sisc*(h/2-ds)+Asb*sis*(d-h/2))/1000000
self.x = x
e=float(Nrd)-Ned
if e>= 0 and e<=1:
break
if x==h and Nrd != Ned:
print('Errors','Compression over whole section', sep=' ')
app = QtWidgets.QApplication([])
error_dialog = QtWidgets.QErrorMessage()
error_dialog.showMessage('Increase size of column, compressed reinforced column!!')
app.exec_()
def __str__(self):
return print(self.x , self.Nrd, self.Mrd)
foo = capacity(b= 300,h=300,y=12,ecu= 0.0035,cb=35,ct=35,fyd=435,fcd=20,Esd=2e5,Ned=1000)
感谢您的帮助,欢迎您改进代码。谢谢
代码更新:
根据上面代码的最新讨论,我设法定义了一个执行计算的函数,但是我想问的问题是,如何在python中将它实现为一个类。获取与按函数显示相同的打印。在
def capacity(b,h,y,ecu,cb,ct,fyd,fcd,Esd,Ned):
d = h - cb
ds = ct
Ast = int(3.1416/4*(y)**2*6.67)
Asb = int(3.1416/4*(y)**2*6.67)
for x in np.linspace(1,h,10000):
try:
esc=ecu/x*(ds-x)
es=ecu/x*(d-x)
except (ZeroDivisionError, RuntimeWarning):
esc = 0
es = 0
sisc=Esd*esc
sis=min(Esd*es,fyd)
if sisc <= -fyd:
sisc=fyd
elif sisc >= -fyd and sisc < 0:
sisc= sisc
else:
sisc=min(sisc,fyd)
Nrd=int((0.8*x*b*fcd+Ast*sisc-Asb*sis)/1000)
Mrd=(0.8*x*b*fcd*(h/2-0.4*x)+Ast*sisc*(h/2-ds)+Asb*sis*(d-h/2))/1000000
e=float(Nrd)-float(Ned)
if e>= 0 and e<=0.5:
return print('\n','x value:', x,'\n' ,'Normalforce: ', Nrd,'\n','Moment capacity :', Mrd,'\n','Bottom steel strain :',es,'\n',
'Top steel strain :', esc,'\n', 'Bottom steel stress :', sisc,'\n','Top steel stress :' ,sis )
break
if x==h and Nrd != Ned:
print('Errors','Tryk over hele tværsnit', sep=' ')
app = QtWidgets.QApplication([])
error_dialog = QtWidgets.QErrorMessage()
error_dialog.showMessage('Increase size of column, compressed reinforced column!!')
app.exec_()
return print(x, Nrd, Mrd)
foo = Capacity(b= 300,h=250,y=12,ecu= 0.0035,cb=50,ct=50,fyd=435,fcd=20,Esd=2e5,Ned=800)
结果:
There's a live version online of the ^{} class from this answer that you can try for yourself
你的代码是正确的。有很多小东西可以清理(例如,可以使用^{} 构造检查循环是否没有中断)。您还需要
__str__
的正确实现。下面是完成所有这些工作的Capacity
类的完整工作实现:{cd1>你可以使用这个类:
^{pr2}$将输出:
您可能正在寻找类似bellow的东西,将您的类
attrs
创建为空,并调用一个将它们作为__init__
的一部分进行填充的类方法,然后可以使用__str__
的重载来获得所需的输出。f字串也可能更干净。(我没有时间收集和测试您的代码,所以您可能需要做一些按摩)相关问题 更多 >
编程相关推荐