-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash_sample.py
217 lines (183 loc) · 8.23 KB
/
flash_sample.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
import asyncio
import matplotlib.pyplot as plt
from equia.models import CalculationComposition, ApiOutputCalculationResultPoint, ExceptionInfo, ProblemDetails
from equia.equia_client import EquiaClient
from equia.demofluids.demofluid1_nHexane_Ethylene_HDPE7 import demofluid1_nHexane_Ethylene_HDPE7
from shared_settings import sharedsettings
def create_client():
return EquiaClient(sharedsettings.url, sharedsettings.access_key)
def create_input(client: EquiaClient):
input = client.get_flash_input()
input.fluid = demofluid1_nHexane_Ethylene_HDPE7() #1 Use predefined demo fluid
input.fluidid = None #No needed since we supply fluid in line above
input.temperature = 300 # Temperature used in units 'Kelvin' as defined in units below
input.pressure = 1 # Pressure used in units 'Bar' as defined in units below
input.flash_type = "Fixed Temperature/Pressure"
input.components = [
CalculationComposition(mass=0.78),
CalculationComposition(mass=0.02),
CalculationComposition(mass=0.2)]
input.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)"
return input
def print_exception_info(exception_info: ExceptionInfo):
print(f"Date: {exception_info.date}")
print(f"Message Type: {exception_info.message_type}")
print(f"Message: {exception_info.message}")
print("")
print(f"Stack Trace: {exception_info.stack_trace}")
def print_problem_details(details: ProblemDetails):
print("------ A problem occured --------")
print(f"Title: {details.title}")
print(f"Type: {details.type}")
print(f"Status: {details.status}")
print(f"Detail: {details.detail}")
print(f"Instance: {details.instance}")
print(f"Additional properties: {details.additional_properties}")
def print_value(input):
print(input.ljust(25), 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.composition.components)):
print_value(
f"{firstPhase.composition.composition.components[i].name} [{firstPhase.composition.composition_units}]")
for phase in result.phases:
print_value(str(phase.composition.composition.components[i].value))
print("")
def print_properties(result: ApiOutputCalculationResultPoint):
firstPhase = result.phases[0]
print("")
print_value(f"Phase Fraction [Mole]")
for phase in result.phases:
print_value(str(phase.mole_percent.value))
print("")
print_value(f"Phase Fraction [Weight]")
for phase in result.phases:
print_value(str(phase.weight_percent.value))
print("")
print_value(f"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.composition.components)):
component = firstPhase.composition.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.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.composition.components)):
component = phase.composition.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_flash():
client = create_client()
input = create_input(client)
result = await client.call_flash_async(input)
# Always do the cleanup
await client.cleanup()
if (isinstance(result, ProblemDetails)):
print_problem_details(result)
elif (result.success == True):
print_calculation_result(result.point)
draw_polymer_distributions(result.point)
else:
print_exception_info(result.exception_info)
loop = asyncio.get_event_loop()
loop.run_until_complete(call_flash())