我目前正在开发一个风电场设计软件,基本上,我必须分析某些风力涡轮机在另一个逆风上的发生率,以便计算出当下游涡轮“看到”流动时,风力的速度下降。
我已经整理了大部分的代码,它可以工作,但是这段代码的计算速度非常慢。
for wd in wind_direction_index:
self.list_for_speed.append([])
for ws in wind_speed_index:
self.list_for_speed[wd].append([])
wind_speed = self._wind_speeds[ws]
#For every wind turbine, remember this list is still ordered from up to dow
dummylist=copy.deepcopy(self.list_for_direction[wd])
for WT in dummylist:
#Replace No deficit with 0
if WT[6]=='NO_deficit':
WT[6]=[0.0]
else:
pass
#For every WT that causes a wake effect on current one
for upWT in WT[3]:
#Define current turbine and rotating upwind turbine
for ready in dummylist:
if upWT[0]==ready[0]:
upWT[6]=ready[6]
else:
pass
current_turbine=WT
upwind_turbine=upWT
#Calculate wind speeds deficits in every current WT due to every upwind WT to it only if here are upwind WTS
if len(current_turbine[3])!=0:
self.calculate_deficits(current_turbine,upwind_turbine,wind_speed)
else:
pass
#Now we have appended all deficits in current_turbine(6) Time to add them up, find the speed and replace
#First, I square the list of deficits
WT[6]=[i**2 for i in WT[6]]
#Now, i add the squares of the deficits
deficit_squared=sum(WT[6])
#Take the square root
deficit_current=sqrt(deficit_squared)
#Finally calculate wind speed at the rotor
U=wind_speed*(1-deficit_current)
#And I replace the list of wind speed deficits with the wind speed at the rotor
WT[6]=U
_Ct=self.team.rna_analysts.get_Ct(U)
#Save results to local wind speeds array
self.local_wind_speeds[WT[0]][wd][ws]=[U,_Ct]
self.list_for_speed[wd][ws].append(dummylist)我知道这有点麻烦,但您可以或多或少地看到我的数据结构是如何安排的。我必须根据风向和风速计算每个涡轮机的风速“赤字”(由local_wind_speeds证明)。有人能发现如何使这段代码变得更快吗?
发布于 2014-05-13 18:13:20
我怀疑copy.deepcopy花费了你很多时间,如果可能的话,尽量避免它。
另外,您只是在复制self.list_for_direction[wd],是否有必要多次复制for ws in wind_speed_index?
我会尽我最大的努力猜出你想要达到的目标,但并不保证我的代码会做你想做的事情。在这里,除了self.list_for_speed[wd][ws].append(dummylist)之外,不再需要虚拟列表。除非以后真的需要self.list_for_speed,而且共享相同wd的对象是不同的对象,否则我担心您无法节省做这么多copy.deepcopy的时间。
让我感到困惑的是,您有嵌套的for WT in dummylist和for ready in dummylist。当第一个for-循环中的6是"NO_deficit"时,您可以更改WT循环。假设您在第一个for循环中的i-th迭代和第二个for-循环中的j-th迭代。如果是j>i,则ready[6]可能是"NO_deficit"。我想这不是你想要的。
def U_Ct(OBJ,WT6,wind_speed):
if WT6 == 'NO_deficit':
deficit_current = 0.0
else:
deficit_squared = sum([i**2 for i in WT6])
deficit_current = sqrt(deficit_squared)
U = wind_speed*(1-deficit_current)
_Ct = OBJ.team.rna_analysts.get_Ct(U)
return U,_Ct
def get_turbine(WT,upWT):
''' WT is a value in the dummylist
upWT is the WT[3] where WT is from the dummylist
'''
if WT[6] == 'NO_deficit':
WT[6] = [0.0]
for ready in WT:
if upWT[0] == ready[0]:
upWT[6] = ready[6]
current_turbine = WT
upwind_turbine = upWT
return current_turbine,upwind_turbine
for wd in wind_direction_index:
for ws in wind_speed_index:
dummylist = copy.deepcopy(self.list_for_direction[wd])
self.list_for_speed[wd][ws].append(dummylist)
wind_speed = self._wind_speeds[ws]
for WT in self.list_for_direction[wd]:
for upWT in WT[3]:
current_turbine,upwind_turbine = get_turbine(WT,upWT)
if len(current_turbine[3])!=0:
# What does this function do? It doesn't return anything
self.calculate_deficits(current_turbine,upwind_turbine,wind_speed)
U,_Ct = U_Ct(self,WT[6],wind_speed)
self.local_wind_speeds[WT[0]][wd][ws] = [U,_Ct]https://codereview.stackexchange.com/questions/49339
复制相似问题