-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatchflash_sample.py
More file actions
213 lines (180 loc) · 8.06 KB
/
Copy pathbatchflash_sample.py
File metadata and controls
213 lines (180 loc) · 8.06 KB
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
import asyncio
import matplotlib.pyplot as plt
from equia.models import CalculationComposition, ApiOutputCalculationResultPoint, ProblemDetails
from equia.equia_client import EquiaClient
from equia.demofluids.demofluid1_nHexane_Ethylene_HDPE7 import demofluid1_nHexane_Ethylene_HDPE7
from equia.models.batchflash_fixed_temperature_pressure_calculation_item import BatchFlashFixedTemperaturePressureCalculationItem
from helpers import print_exception_info, print_problem_details
from shared_settings import sharedsettings
def create_client():
return EquiaClient(sharedsettings.url, sharedsettings.access_key)
def create_input(client: EquiaClient):
argument = client.get_batchflash_fixed_temperature_pressure_input()
argument.fluid = demofluid1_nHexane_Ethylene_HDPE7() #1 Use predefined demo fluid
argument.fluidid = None #No needed since we supply fluid in line above
argument.temperature = 300 # Temperature used in units 'Kelvin' as defined in units below
argument.pressure = 1 # Pressure used in units 'Bar' as defined in units below
argument.units = "C(In,Massfraction);C(Out,Massfraction);T(In,Kelvin);T(Out,Kelvin);P(In,Bar);P(Out,Bar);H(In,kJ/Kg);H(Out,kJ/Kg);S(In,kJ/(Kg Kelvin));S(Out,kJ/(Kg Kelvin));Cp(In,kJ/(Kg Kelvin));Cp(Out,kJ/(Kg Kelvin));Viscosity(In,centiPoise);Viscosity(Out,centiPoise);Surfacetension(In,N/m);Surfacetension(Out,N/m)"
argument.components = [
CalculationComposition(amount=0.78),
CalculationComposition(amount=0.02),
CalculationComposition(amount=0.20)
]
item1 = BatchFlashFixedTemperaturePressureCalculationItem()
item1.temperature = 445
item1.pressure = 20
argument.points.append(item1)
item2 = BatchFlashFixedTemperaturePressureCalculationItem()
item2.temperature = 445
item2.pressure = 45
argument.points.append(item2)
return argument
def print_value(argument):
print(argument.ljust(35), end="", flush=True)
def print_calculation_result(result: ApiOutputCalculationResultPoint):
print("")
print_value("Property")
for phase in result.phases:
print_value(phase.phase_label)
print("")
print_value(f"Temperature [{result.temperature.units}]")
print_value(str(result.temperature.value))
print("")
print_value(f"Pressure [{result.pressure.units}]")
print_value(str(result.pressure.value))
print("")
print_composition(result)
print_properties(result)
print_polymer_moments(result)
print_polymer_distributions(result)
def print_composition(result: ApiOutputCalculationResultPoint):
print("")
print("Components")
firstPhase = result.phases[0]
for i in range(len(firstPhase.composition.components)):
print_value(
f"{firstPhase.composition.components[i].name} [{firstPhase.composition.composition_units}]")
for phase in result.phases:
print_value(str(phase.composition.components[i].value))
print("")
def print_properties(result: ApiOutputCalculationResultPoint):
firstPhase = result.phases[0]
print("")
print_value("Phase Fraction [Mole]")
for phase in result.phases:
print_value(str(phase.mole_percent.value))
print("")
print_value("Phase Fraction [Weight]")
for phase in result.phases:
print_value(str(phase.weight_percent.value))
print("")
print_value("Compressibility [-]")
for phase in result.phases:
print_value(str(phase.compressibility.value))
print("")
print_value(f"Density [{firstPhase.density.units}]")
for phase in result.phases:
print_value(str(phase.density.value))
print("")
print_value(f"Molar Volumne [{firstPhase.volume.units}]")
for phase in result.phases:
print_value(str(phase.volume.value))
print("")
print_value(f"Enthalpy [{firstPhase.enthalpy.units}]")
for phase in result.phases:
print_value(str(phase.enthalpy.value))
print("")
print_value(f"Entropy [{firstPhase.entropy.units}]")
for phase in result.phases:
print_value(str(phase.entropy.value))
print("")
print_value(f"Cp [{firstPhase.cp.units}]")
for phase in result.phases:
print_value(str(phase.cp.value))
print("")
print_value(f"Cv [{firstPhase.cv.units}]")
for phase in result.phases:
print_value(str(phase.cv.value))
print("")
print_value(f"JTCoefficient [{firstPhase.jt_coefficient.units}]")
for phase in result.phases:
print_value(str(phase.jt_coefficient.value))
print("")
print_value(f"Velocity of sound [{firstPhase.speed_of_sound.units}]")
for phase in result.phases:
print_value(str(phase.speed_of_sound.value))
print("")
print_value(f"Solubility parameter [{firstPhase.solubility_parameter.units}]")
for phase in result.phases:
print_value(str(phase.solubility_parameter.value))
print("")
print_value(f"Molecular Weight [{firstPhase.molecular_weight.units}]")
for phase in result.phases:
print_value(str(phase.molecular_weight.value))
print("")
def print_polymer_moments(result: ApiOutputCalculationResultPoint):
first_phase_moments = result.phases[0].polymer_moments
for i in range(len(first_phase_moments.polymers)):
print_value(
f"Mn ({first_phase_moments.polymers[i].polymer_name}) [{first_phase_moments.moment_units}]")
for phase in result.phases:
print_value(str(phase.polymer_moments.polymers[i].mn))
print("")
print_value(
f"Mw ({first_phase_moments.polymers[i].polymer_name}) [{first_phase_moments.moment_units}]")
for phase in result.phases:
print_value(str(phase.polymer_moments.polymers[i].mw))
print("")
print_value(
f"Mz ({first_phase_moments.polymers[i].polymer_name}) [{first_phase_moments.moment_units}]")
for phase in result.phases:
print_value(str(phase.polymer_moments.polymers[i].mz))
print("")
def print_polymer_distributions(result: ApiOutputCalculationResultPoint):
firstPhase = result.phases[0]
# find components with distribution (polymers)
for compIndex in range(len(firstPhase.composition.components)):
component = firstPhase.composition.components[compIndex]
if (len(component.distribution) <= 0):
continue
# just print the name of the polymer on top of each phase column
print_value("")
for phaseIndex in range(len(result.phases)):
print_value(component.name)
# now print the actual distribution values for each phase
for distIndex in range(len(component.distribution)):
print("")
print_value("")
for phaseIndex in range(len(result.phases)):
distribution = result.phases[phaseIndex].composition.components[compIndex].distribution[distIndex]
print_value(str(distribution.value))
def draw_polymer_distributions(result: ApiOutputCalculationResultPoint):
plt.title("Distribution")
plt.xlabel("Ln(Molar mass)")
plt.xscale("log")
plt.ylabel("Mass fraction")
for i in range(len(result.phases)):
phase = result.phases[i]
for j in range(len(phase.composition.components)):
component = phase.composition.components[j]
if (len(component.distribution) > 0):
plt.plot(list(map(lambda point: point.value, component.distribution)), list(
map(lambda point: point.molar_mass, component.distribution)), label=f"{component.name}: {phase.phase_label}")
plt.legend()
plt.show()
async def call_batchflash():
client = create_client()
argument = create_input(client)
result = await client.call_batchflash_fixed_temperature_pressure_async(argument)
# Always do the cleanup
await client.cleanup()
if (isinstance(result, ProblemDetails)):
print_problem_details(result)
elif (result.success == True):
print_calculation_result(result.points[0])
print("")
print_calculation_result(result.points[1])
else:
print_exception_info(result.exception_info)
if __name__ == "__main__":
asyncio.run(call_batchflash())