如何在化学迭代中使用 “for 循环”?

huangapple go评论87阅读模式
英文:

How can i use the "for loop" for a chemical iteration?

问题

我遇到一个问题,我无法弄清楚如何编写我的循环代码。

n_masssection = 100
    
for i in range(n_masssection):
   
    dn_New = dn_Sample(CO_in, H2O_in, H2_in, CO2_in)
    n_New = dn_Pronew(CO_in, H2O_in, H2_in, CO2_in, dn_New)
    dn_New1 = dn_Sample(n_New[0], n_New[1], n_New[2], n_New[3])
    n_New1 = dn_Pronew(n_New[0], n_New[1], n_New[2], n_New[3], dn_New1)

"dn_Sample" 和 "dn_Pronew" 是函数。 "dn_Sample" 返回一个值,用于计算 "n_Pronew"。 "n_Pronew" 计算4个新的气体浓度,应该放在下一个 "section" 中,以计算一个新的 "dn_New",但是我不知道如何在不为每次计算编写新对象的情况下执行这个操作,因为那会花费很多时间。

有人可以帮我解决这个问题吗?

我有一个想法,可以在循环中保存结果,但是我的函数总是需要它们的4|5个参数来进行计算。如何用前一个函数的计算结果替代它们?

英文:

I have the problem that I can't figure out how to write my loop code.

n_masssection = 100
    
for i in range(n_masssection):
   
    dn_New = dn_Sample(CO_in, H2O_in, H2_in, CO2_in)
    n_New = dn_Pronew(CO_in, H2O_in, H2_in, CO2_in, dn_New)
    dn_New1 = dn_Sample(n_New[0], n_New[1], n_New[2], n_New[3])
    n_New1 = dn_Pronew(n_New[0], n_New[1], n_New[2], n_New[3], dn_New1)

"dn_Sample, dn_Pronew" are functions. "dn_Sample" returns a value that is used for the calculation of "n_Pronew". "n_Proneu" calculates 4 new gas concentrations which should be placed in the next "section" so calculate a ne dn_New with ne Sample function but now with the new gas concentrations. I want that my loop should do this 100 times... but I don't know how to do that without writing new objects for every calculation, which would take a lot of time.

Can someone help me with this?

I had the idea to save the results in the loop but my functions always need there 4|5 arguments to calculate. How to replace theme with the results of the calculation of the function before them.

答案1

得分: 1

n_masssection = 100
t = (CO_in, H2O_in, H2_in, CO2_in)

res = [t]
for i in range(n_masssection):
dn_New = dn_Sample(*res[-1])
n_New = n_Pronew(*res[-1], dn_New)
dn_New1 = dn_Sample(*n_New)
n_New1 = n_Pronew(*n_New, dn_New1)
res.append(n_New1)

然后,我认为res是你的最终结果。

英文:
n_masssection = 100
t = (CO_in, H2O_in, H2_in, CO2_in)

res = [t]
for i in range(n_masssection):
    dn_New = dn_Sample(*res[-1])
    n_New = n_Pronew(*res[-1], dn_New)
    dn_New1 = dn_Sample(*n_New)
    n_New1 = n_Pronew(*n_New, dn_New1)
    res.append(n_New1)

Then, I think res is your final result.

huangapple
  • 本文由 发表于 2023年7月10日 19:20:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定