gustavochm / sgtpy

Other
39 stars 15 forks source link

[SAFT-Gamma-Mie] Errors with flash calculation #17

Open YouHyin opened 6 months ago

YouHyin commented 6 months ago

Hello Gustavo!

Your updates to sgtpy have been a great help in my research. Thank you very much. The flash calculations are converging much better than before, but I have a problem.

import numpy as np
import matplotlib.pyplot as plt
from sgtpy import component, mixture, saftgammamie
from sgtpy.equilibrium import flash

# Set the necessary component and mixture settings for the actual calculation
T = 313.15  # K
P = 101000  # Pa

# mass fraction of amine
mass_amine = 0.304

# Initial lean solvent loading
lean_loading=1.5

# Flow rates of lean solvent and inlet gas 
flue_gas_flow = 220.29 # kmol/hr
lean_solvent_flow = 39.20 # kmol/hr

N2 = component(GC={'N2': 1})
CO2 = component(GC={'CO2': 1})
H2O = component(GC={'H2O': 1})
AEEA = component(GC={'NH2': 1, 'CH2': 3, 'NH':1,'CH2OH': 1})

mix = N2 + CO2 + AEEA + H2O
mix.saftgammamie()
eos = saftgammamie(mix)
# For initial lean solvent, set N2 mole fraction to nearly zero
moles_N2 = 1E-6
alpha = lean_loading

mass_amine = mass_amine
moles_amine = mass_amine / eos.Mw[2]

moles_co2 = alpha * moles_amine
mass_co2 = moles_co2 * eos.Mw[1]

mass_base = 1.
mass_water = mass_base - mass_amine - mass_co2

moles_water = mass_water / eos.Mw[3]
moles_array = np.array([moles_N2, moles_co2, moles_amine, moles_water])

# Initial lean amine solvent mole fraction (N2, CO2, amine, water 순서)
x = moles_array / np.sum(moles_array, axis=0)
x0 = x
print(f"x0: {x0}")

#flue gas composition N2, CO2, amine, water
y0 = np.array([0.771,0.119,1E-5,0.110])

# Calculate the mole flows for each component in flue gas and lean solvent
flue_gas_mole_flows = flue_gas_flow*y0
lean_solvent_mole_flows = lean_solvent_flow*x0

# Calculate the total mole flows for each component
total_mole_flows = flue_gas_mole_flows + lean_solvent_mole_flows

# Calculate the global compositions
z = total_mole_flows / np.sum(total_mole_flows)
print(f"z(global compositon): {z}")

sol=flash(x0, y0, 'LV', z, T, P, eos, minimization_approach='helmholtz',not_in_x_list=[0],full_output=True) # Set the amount of N2 in the liquid phase to zero

print(sol.X, sol.Y)

When I run this code, the liquid and vapor mole fractions come out as nan. I think that during the flash calculation process, there might be an formula in the form of 1/x, and x is becoming 0, leading to nan values. I attempted to debug and change this expression to the form 1/(x+10^-8) to prevent it from diverging to infinity, but I couldn't find the exact expression.

Can you give me any advice on how to change the formula in the flash calculation process so that the nan value does not come out? Thank as always for your help. Best wish!

gustavochm commented 6 months ago

Dear YouHyin,

Thank you very much for your question! The fact you are testing sgtpy with very challenging mixtures really helps me to keep improving the code. So there are two things that I realized based on your problem:

See some solutions with the above (I'm preventing the ASS step because is jumping between solutions)

I'll update a new version that checks if the density solver converged correctly shortly.

Edit: I just updated a new version to pypi. For now, I only uploaded a precompiled version for OSX; soon, I'll upload a compiled version for Windows.

Regards, Gustavo

YouHyin commented 5 months ago

First of all, I'm really sorry for the late response. Thank you so much for your response and updating the package so quickly. I'll refer to your answer and proceed with the flash calculation. Thanks to you, it was very helpful. Thank you always.

Best whises! YouHyin