teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

9

コードを修正

2020/10/19 00:47

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -108,7 +108,6 @@
108
108
  P_total=[]
109
109
 
110
110
  def reactor(N, γ, ΔT):
111
- boolean=False
112
111
  P_log=[[]]
113
112
  P_list=[]
114
113
  N=N

8

コードを修正しました。

2020/10/19 00:47

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -15,7 +15,7 @@
15
15
  print(z)
16
16
  #return zは値を返さない
17
17
  return z
18
- print(division(2,0))
18
+ division(2,0)
19
19
  ```
20
20
 
21
21
 

7

コードを修正しました。

2020/10/18 16:03

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -15,7 +15,7 @@
15
15
  print(z)
16
16
  #return zは値を返さない
17
17
  return z
18
- print(2,0)
18
+ print(division(2,0))
19
19
  ```
20
20
 
21
21
 

6

解決策を追記しました。

2020/10/18 16:02

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -76,6 +76,7 @@
76
76
  P_log: i段目の反応器の消費電力を計算する際の挙動をi番目の要素に記録する
77
77
  (record the behavior when calculating the power consumption of the i-th reactor in the i-th element)
78
78
  P_list: 反応器毎の攪拌消費電力を記録する(record the power consumption of each reactor)
79
+ P_total: 最終的な計算結果を保存する(save the final result of calculation)
79
80
  """
80
81
 
81
82
  """

5

解決策を追記しました。

2020/10/18 15:58

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,4 @@
1
+ returnで値が正しく返ってきません。
1
2
  ```Python
2
3
  def division(x,y):
3
4
  boolean=False

4

解決策を追記しました。

2020/10/18 15:53

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -15,4 +15,142 @@
15
15
  #return zは値を返さない
16
16
  return z
17
17
  print(2,0)
18
+ ```
19
+
20
+
21
+ 多くの方にご回答いただき誠にありがとうございました。おかげさまで書きたかったプログラムが書けました。下のコードのように書くと、引数を更新して正しく計算されるまで関数を呼び直すという動作ができます。
22
+ ```Python
23
+ Z=[]
24
+ def division(x,y):
25
+ boolean=False
26
+ x=x
27
+ y=y
28
+ try:
29
+ z=x/y
30
+ boolean=True
31
+ Z.append(z)
32
+ except ZeroDivisionError:
33
+ y+=1
34
+ division(x,y)
35
+
36
+ division(2,0)
37
+ print(Z[0])
38
+ ```
39
+
40
+ また、これを応用して、反応器段数、原料濃度、溶液と冷媒の温度差を変数にとり連続混合槽型反応器のシミュレーションを行うコードを作成しました。(大学で学んでいる化学工学の問題です。かなりマニアックな領域なので化学工学に馴染みのない方が見るのは難しいかもしれません。)
41
+ ```Python
42
+ """
43
+ 与えられた定数(prepared constants)
44
+ Gp: 生産量(product flow rate) [kg/s]
45
+ ζ: 反応転化率(conversion)
46
+ γ: 原料中のブタジエン重量割合(weight fraction of monomer in the feed)
47
+ τ: 滞留時間(residence time) [s]
48
+ ρ: 溶液密度(density of mixture) [kg/m^3]
49
+ α: 反応器の高さ/直径比(ratio of height to diameter of the reactor)
50
+ ΔH: ブタジエンの反応熱(heat of polymerization of butadiene) [J/mol]
51
+ ΔT: 反応溶液と冷媒の温度差(temperature difference between reactant and coolant) [℃]
52
+ λ: 溶液の熱伝導率(thermal conductivity of solution) [W/(m・K)]
53
+ μ: 溶液の粘度(viscosity of solution) [Pa・s]
54
+ Cp: 溶液の比熱(specific heat of solution) [J/(kg・K)]
55
+ M: ブタジエンの分子量(molecular weight of butadiene) [g/mol]
56
+ """
57
+
58
+ """
59
+ 計算により求められる値(values obtained by calculation)
60
+ v: 溶液の体積流量(volumetric flow rate of solution) [m^3/s]
61
+ V: 反応器体積(volume of the reactor) [m^3]
62
+ D: 反応器直径(diameter of the reactor) [m]
63
+ Qp: 反応熱(heat of reaction) [J]
64
+ P: 攪拌の消費電力(power consumption of stirring) [kW]
65
+ h: 冷却装置の熱伝達率(heat transfer coefficiency of the coolant) [W/m^2・K]
66
+ Nu: ヌッセルト数(Nusselt number)
67
+ Pr: プラントル数(Prantdl number)
68
+ Re: レイノルズ数(Reynols number)
69
+ n: 1分あたり攪拌回数(number of stirring per minute) [/s]
70
+ Np: 動力数(power number)
71
+ """
72
+
73
+ """
74
+ 値の記録に使用するリスト(list to record the value)
75
+ P_log: i段目の反応器の消費電力を計算する際の挙動をi番目の要素に記録する
76
+ (record the behavior when calculating the power consumption of the i-th reactor in the i-th element)
77
+ P_list: 反応器毎の攪拌消費電力を記録する(record the power consumption of each reactor)
78
+ """
79
+
80
+ """
81
+ 例外処理(exception handling)
82
+ 排熱を処理しきれない場合は冷媒の温度を1度ずつ下げる
83
+ (if the exhaust heat cannot be processed, lower the temperature of the refrigerant by 1 degree.)
84
+ """
85
+
86
+ import math
87
+ Gp = 0.729
88
+ ζ = 0.60
89
+ γ_1 = 0.05
90
+ τ_1 = 18000
91
+ ρ = 850
92
+ α = 1.3
93
+ ΔH = 72800
94
+ λ = 0.128
95
+ Cp = 1680
96
+ M = 54.0
97
+ V_1=514.6 #V_1=v*τ_1
98
+ v_1=0.02859 #v_1=Gp/(ρ*γ_1*ζ)
99
+
100
+ #反応速度定数を求める(find the reaction rate constant)
101
+ ζ_total=ζ
102
+ k=v_1*ζ_total/(V_1*(1-ζ_total))
103
+
104
+ P_log=[[]]
105
+ P_list=[]
106
+ P_total=[]
107
+
108
+ def reactor(N, γ, ΔT):
109
+ boolean=False
110
+ P_log=[[]]
111
+ P_list=[]
112
+ N=N
113
+ γ=γ
114
+ ΔT=ΔT
115
+ #反応器のサイズを求める(find the size of each reactor)
116
+ τ=(1/k)*(1/math.pow(1-ζ_total,1/N)-1)
117
+ v=Gp/(ρ*γ*ζ_total)
118
+ V=v*τ
119
+ D=math.pow(4*V/(1.3*math.pi),1/3)
120
+ #各反応器の熱収支を計算する(calculate heat balance of each reactor)
121
+ ζ_each=1-math.pow(1-ζ_total,1/N)
122
+ try:
123
+ for i in range(N):
124
+ P_log.append([])
125
+ P_log[i+1].append(0)
126
+ Qp=(ΔH/(M*0.001))*k*ρ*V*(1-ζ_each)**(i+1)*γ
127
+ h=Qp/(1.3*math.pi*(D**2)*ΔT)
128
+ Nu=h*D/λ
129
+ μ=0.0010*math.pow(50,1.7)*math.pow(1-(1-ζ_each)**(i+1),2.5)*math.exp(21*γ)
130
+ Pr=μ*Cp/λ
131
+ Re=math.pow((1/0.5)*Nu*math.pow(Pr,-1/3),3/2)
132
+ n=μ*Re/(ρ*(D/2)**2)
133
+ Np=14.6*math.pow(Re,-0.28)
134
+ P=0.0010*Np*ρ*(n**3)*((D/2)**5)
135
+ P_log[i+1].append(P)
136
+ while abs((P-P_log[i+1][len(P_log[i+1])-2])/P)>1.00*math.pow(10, -3):
137
+ h=(Qp+P*1000)/(1.3*math.pi*(D**2)*ΔT)
138
+ Nu=h*D/λ
139
+ Pr=μ*Cp/λ
140
+ Re=math.pow((1/0.5)*Nu*math.pow(Pr,-1/3),3/2)
141
+ n=μ*Re/(ρ*(D/2)**2)
142
+ Np=14.6*math.pow(Re,-0.28)
143
+ P=0.0010*Np*ρ*(n**3)*((D/2)**5)
144
+ P_log[i+1].append(P)
145
+ P_list.append(P_log[i+1][-1])
146
+ P_total.append(sum(P_list))
147
+ P_total.append(ΔT)
148
+ except OverflowError:
149
+ if ΔT<=50+273:
150
+ ΔT+=1
151
+ reactor(N, γ, ΔT)
152
+ else:
153
+ print("Warning: The feed is too thick. Reduce γ.")
154
+ reactor(1, 0.07, 65)
155
+ print(P_total)
18
156
  ```

3

コードを少し修正しました。

2020/10/18 15:52

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -9,12 +9,10 @@
9
9
  except ZeroDivisionError:
10
10
  y+=1
11
11
  division(x,y)
12
- else:
13
- Z=z
14
- pass
15
12
  if boolean==True:
16
13
  #print(z)は正しく値を返す
17
14
  print(z)
18
15
  #return zは値を返さない
19
- return Z
16
+ return z
17
+ print(2,0)
20
18
  ```

2

正しくマークダウン記法を使って書き直しました。また、プログラムを短いものに差し替えました。

2020/10/18 15:00

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -13,8 +13,8 @@
13
13
  Z=z
14
14
  pass
15
15
  if boolean==True:
16
+ #print(z)は正しく値を返す
16
17
  print(z)
17
- #print(z)は正しく値を返す
18
- return zは値を返さない
18
+ #return zは値を返さない
19
19
  return Z
20
20
  ```

1

正しくマークダウン記法で書き直しました。また、プログラムの重要な箇所だけ抽出して書き直しました。

2020/10/18 14:52

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,95 +1,20 @@
1
- ### 前提・実現したいこと
2
-
3
- 化学工学の課題で書いたプログラムです。
4
- 反応器の攪拌に必要な消費電力Powerを計算したいです。
5
-
6
- ### 発生している問題・エラーメッセージ
7
-
8
- printではきちんと値を表示できるのに、returnを使うと何も返ってきません。
9
-
10
-
11
- ### 該当のソースコード
12
-
13
- import math
14
- Gp = 0.729
15
- ζ = 0.60
16
- γ_1 = 0.05
17
- τ_1 = 18000
18
- ρ = 850
19
- α = 1.3
20
- ΔH = 72800
21
- λ = 0.128
22
- Cp = 1680
23
- M = 54.0
24
- V_1=514.6 #V_1=v*τ_1
25
- v_1=0.02859 #v_1=Gp/(ρ*γ_1*ζ)
26
-
27
- #反応速度定数を求める(find the reaction rate constant)
28
- ζ_total=ζ
29
- k=v_1*ζ_total/(V_1*(1-ζ_total))
30
-
31
- P_log=[[]]
32
- P_list=[]
1
+ ```Python
33
-
34
- def reactor(N, γ, ΔT):
2
+ def division(x,y):
35
- boolean=False
3
+ boolean=False
36
- P_log=[[]]
37
- P_list=[]
38
- N=N
4
+ x=x
39
- γ=γ
5
+ y=y
40
- ΔT=ΔT
41
- #反応器のサイズを求める(find the size of each reactor)
42
- τ=(1/k)*(1/math.pow(1-ζ_total,1/N)-1)
43
- v=Gp/(ρ*γ*ζ_total)
44
- V=v*τ
45
- D=math.pow(4*V/(1.3*math.pi),1/3)
46
- #各反応器の熱収支を計算する(calculate heat balance of each reactor)
47
- ζ_each=1-math.pow(1-ζ_total,1/N)
48
- try:
6
+ try:
49
- for i in range(N):
50
- P_log.append([])
51
- P_log[i+1].append(0)
52
- Qp=(ΔH/(M*0.001))*k*ρ*V*(1-ζ_each)**(i+1)*γ
53
- h=Qp/(1.3*math.pi*(D**2)*ΔT)
54
- Nu=h*D/λ
7
+ z=x/y
55
- μ=0.0010*math.pow(50,1.7)*math.pow(1-(1-ζ_each)**(i+1),2.5)*math.exp(21*γ)
56
- Pr=μ*Cp/λ
57
- Re=math.pow((1/0.5)*Nu*math.pow(Pr,-1/3),3/2)
58
- n=μ*Re/(ρ*(D/2)**2)
59
- Np=14.6*math.pow(Re,-0.28)
60
- P=0.0010*Np*ρ*(n**3)*((D/2)**5)
61
- P_log[i+1].append(P)
62
- while abs((P-P_log[i+1][len(P_log[i+1])-2])/P)>1.00*math.pow(10, -3):
63
- h=(Qp+P*1000)/(1.3*math.pi*(D**2)*ΔT)
64
- Nu=h*D/λ
65
- Pr=μ*Cp/λ
66
- Re=math.pow((1/0.5)*Nu*math.pow(Pr,-1/3),3/2)
67
- n=μ*Re/(ρ*(D/2)**2)
68
- Np=14.6*math.pow(Re,-0.28)
69
- P=0.0010*Np*ρ*(n**3)*((D/2)**5)
70
- P_log[i+1].append(P)
71
- P_list.append(P_log[i+1][-1])
72
- if len(P_list)==N:
73
- boolean=True
8
+ boolean=True
74
- except OverflowError:
9
+ except ZeroDivisionError:
75
- if ΔT<=50+273:
76
- ΔT+=1
10
+ y+=1
77
- reactor(N, γ, ΔT)
11
+ division(x,y)
78
- else:
12
+ else:
13
+ Z=z
79
- print("Warning: The feed is too thick. Reduce γ.")
14
+ pass
80
- if boolean==True:
15
+ if boolean==True:
81
- Power=P_list[-1]
82
- print(Power)
16
+ print(z)
17
+ #print(z)は正しく値を返す
18
+ return zは値を返さない
83
- return Power
19
+ return Z
84
-
85
- reactor(1, 0.07, 67)
86
-
87
- ### 試したこと
20
+ ```
88
-
89
- 最後のreactor(1, 0.07, 67)をprint(reactor(1, 0.07, 67))とするとPowerの値ではなく"None"が返ってきました。
90
-
91
-
92
- ### 補足情報(FW/ツールのバージョンなど)
93
-
94
- jupyter notebookで実行しています。
95
- printで表示すると確かに値を持っているのに、returnを使うとNoneになってしまう…。このようなことは初めてでとても困惑しています。