-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
218 lines (158 loc) · 8.82 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import math
import matplotlib.pyplot as plt
ACCELERATION_DUE_GRAVITY = 9.81
VELOCITY = [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5]
DIAMETERS = [0.00159, 0.00318, 0.00477, 0.00636, 0.00795, 0.00954, 0.01113, 0.01272, 0.01431, 0.0159]
LENGTHS = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
PIPE_ROUGHNESS = 0.03
THERMAL_CONDUCTIVITY = 401
def calculate_reynolds_number(density: float, diameter: float, velocity: float, dynamic_viscosity: float) -> float:
# This function calculates the reynolds number
reynolds_number = (density * diameter * velocity) / dynamic_viscosity
return reynolds_number
def calculate_frictional_factor_for_laminar_flow(reynold_number: float) -> float:
return 64 / reynold_number
def calculate_frictional_factor_for_turbulent(reynold_number: float, pipe_roughness: float, diameter: float) -> float:
return 2 * ((8 / reynold_number) ** 12 + (
(2.457 * math.log((0.27 * pipe_roughness / diameter) + (7 / reynold_number) ** 0.9)) ** 16 + (
37530 / reynold_number) ** 16) ** (-3 / 2)) ** (
1 / 12)
def get_frictional_factor(reynold_number: float, pipe_roughness: float, is_laminar: bool, diameter: float) -> float:
if is_laminar:
frictional_factor = calculate_frictional_factor_for_laminar_flow(reynold_number)
else:
frictional_factor = calculate_frictional_factor_for_turbulent(reynold_number=reynold_number,
pipe_roughness=pipe_roughness, diameter=diameter)
return frictional_factor
def calculate_head_loss(friction_factor: float, pipe_length: float, diameter: float, velocity: float):
head_loss = (friction_factor * pipe_length * velocity ** 2) / (diameter * 2 * ACCELERATION_DUE_GRAVITY)
return head_loss
def calculate_prandtl_number(dynamic_viscosity: float, specific_heat_capacity: float, conductivity: float) -> float:
prandtl_number = (dynamic_viscosity * specific_heat_capacity) / conductivity
return prandtl_number
def calculate_pressure(density: float, head_loss: float) -> float:
return -density * ACCELERATION_DUE_GRAVITY * head_loss
def calculate_coefficient_of_heat_transfer(reynold_number: float, diameter: float, prandtl_number: float,
conductivity: float) -> float:
return 0.023 * (reynold_number ** 0.8) * (prandtl_number ** 0.4) * conductivity / diameter
def get_values() -> dict:
# This function gets the various values and does the operation for each fluid
reynolds_number_values = [] # This list would store the reynold number for the fluid in what ever case
head_loss_values = [] # This list would store the head loss for the fluid in varuous cases
coefficient_of_heat_transfer_values = []
pressure_values = []
# Get the values that would be used for all fluids
specific_heat_capacity = float(input('Enter specific heat capacity: '))
dynamic_viscosity = float(input('Enter the dynamic viscosity: '))
density = float(input('Enter the density of the fluid: '))
for length, diameter, velocity in zip(LENGTHS, DIAMETERS, VELOCITY):
# Calculate the reynold's number.
reynold_number = calculate_reynolds_number(diameter=diameter, density=density, velocity=velocity,
dynamic_viscosity=dynamic_viscosity)
# Get if the fluid is laminar or not
is_laminar = reynold_number < 2000
# Get the prandtl number
prandtl_number = calculate_prandtl_number(specific_heat_capacity=specific_heat_capacity,
conductivity=THERMAL_CONDUCTIVITY,
dynamic_viscosity=dynamic_viscosity)
# Get the frictional factor
frictional_factor = get_frictional_factor(reynold_number=reynold_number, pipe_roughness=PIPE_ROUGHNESS,
is_laminar=is_laminar, diameter=diameter)
# Get the head loss
head_loss = calculate_head_loss(friction_factor=frictional_factor, pipe_length=length, diameter=diameter,
velocity=velocity)
# Get the pressure
pressure = calculate_pressure(density=density, head_loss=head_loss)
# Get the coefficient of heat transfer.
coefficient_of_heat_transfer = calculate_coefficient_of_heat_transfer(reynold_number=reynold_number,
diameter=diameter,
prandtl_number=prandtl_number,
conductivity=THERMAL_CONDUCTIVITY)
pressure_values.append(pressure)
coefficient_of_heat_transfer_values.append(coefficient_of_heat_transfer)
head_loss_values.append(head_loss)
reynolds_number_values.append(reynold_number)
return {
'head_loss': head_loss_values,
'pressure_loss': pressure_values,
'heat_transfer_coefficient': coefficient_of_heat_transfer_values,
'reynolds_number': reynolds_number_values,
'velocity': VELOCITY,
'diameter': DIAMETERS,
}
def plot_graph(x: list, y: list, x_axis_label, y_axis_label, plot_title):
plt.plot(x, y, color='r')
plt.xlabel(x_axis_label)
plt.ylabel(y_axis_label)
# setting the title of the plot
plt.title(plot_title)
plt.show()
# sub plots
def add_subplot_graph(axis, row: int, col: int, x: list, y: list, x_axis_label: str, y_axis_label: str,
plot_title: str, color: str):
axis[row, col].plot(x, y, color=color)
axis.set_title(plot_title)
axis.xlabel(x_axis_label)
axis.ylabel(y_axis_label)
def create_excel_sheet(fluids_values_dict: dict):
# This would create an excel file from a dictionary.
# It is kind of hard coded in this case.
# It would create multiple excel sheets for each fluids.
assert(len(fluids_values_dict) > 0)
pass
if __name__ == '__main__':
# Run some code
# fluids = ['castor_oil', 'linseed oil', 'aqua ammonia']
fluids = []
colors = ['r', 'g', 'b', 'c', 'm', 'y', 'k', 'w', 'chartreuse', 'burlywood']
print('Enter fluid names. Press enter to stop recording')
while True:
fluid_name = input('Enter fluid name: ')
if len(fluid_name) == 0:
# Enter has been entered twice
break
fluids.append(fluid_name)
print(f'Calculating for {",".join(fluids)}')
fluids_values = {}
for fluid in fluids:
print(f'<------------Calculating for {fluid}-------------->')
fluid_value = get_values()
fluids_values[fluid] = fluid_value
print('Plotting graphs >>>>>>>>>>>> Loading >>>>>>>>>>>>>>>>>>>')
# plot_graph(y=castor_oil_reynolds_number, x=DIAMETERS, x_axis_label='Diameter', y_axis_label='Reynolds Number',
# plot_title='Plot of The Reynolds Number against the Diameter')
graphs_details = [['head_loss', 'reynolds_number'], ['heat_transfer_coefficient', 'reynolds_number'],
['pressure_loss', 'reynolds_number'], ['reynolds_number', 'diameter'],
['reynolds_number', 'velocity']]
for row in range(len(graphs_details)):
graph_detail = graphs_details[row]
y_axis = graph_detail[0]
x_axis = graph_detail[1]
#
# fluid_one_y_values = fluids_values[fluids[0]][y_axis]
# fluid_one_x_values = fluids_values[fluids[0]][x_axis]
#
# fluid_two_y_values = fluids_values[fluids[1]][y_axis]
# fluid_two_x_values = fluids_values[fluids[1]][x_axis]
#
# fluid_three_y_values = fluids_values[fluids[2]][y_axis]
# fluid_three_x_values = fluids_values[fluids[2]][x_axis]
#
# fluid_four_y_values = fluids_values[fluids[3]][y_axis]
# fluid_four_x_value = fluids_values[fluids[3]][x_axis]
#
# x_values = [fluid_one_x_values, fluid_two_x_values, fluid_three_x_values, fluid_four_x_value]
# y_values = [fluid_one_y_values, fluid_two_y_values, fluid_three_y_values, fluid_four_y_values]
# x_values = [castor_oil_x_values, linseed_oil_x_values, aqua_ammonia_x_value]
# y_values = [castor_oil_y_values, linseed_oil_y_values, aqua_ammonia_y_value]
for specific_graph_number in range(len(fluids)):
# Plot all the graphs on a canvas
plt.plot(fluids_values[fluids[specific_graph_number]][x_axis],
fluids_values[fluids[specific_graph_number]][y_axis],
color=colors[specific_graph_number])
plt.xlabel(x_axis)
plt.ylabel(y_axis)
plt.title(f'graph of {y_axis} against {x_axis}')
plt.legend(fluids)
# plt.legend(['Castor Oil', 'Linseed Oil','Aqua Ammonia'])
plt.show()