DanWBR / dwsim

DWSIM is a Steady-State and Dynamic Sequential Modular Chemical Process Simulator for Windows, Linux and macOS.
https://dwsim.org
GNU General Public License v3.0
287 stars 92 forks source link

PV Flash calculation non-convergence alternating between two values #149

Closed msakai closed 3 years ago

msakai commented 3 years ago

Describe the bug

I'm trying to use DTL_v6.3.7634 and I encountered a case where the PV Flash calculation alternated between two values (T = 337.027167079011 and T = 338.027167079011) and did not converge.

I guess such non-convergence is not a bug, but I report it just in case it might be useful for future development.

To Reproduce

Run the following C# program.

using System;
using DWSIM.Interfaces.Enums;

namespace TestMaxIterationError
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            var dtlc = new DWSIM.Thermodynamics.CalculatorInterface.Calculator();
            dtlc.Initialize();
            dtlc.SetDebugLevel(3);

            var prpp = dtlc.GetPropPackInstance("Peng-Robinson (PR)");
            prpp.FlashSettings[FlashSetting.NL_FastMode] = "False";
            prpp.FlashSettings[FlashSetting.PVFlash_MaximumTemperatureChange] = "1.0";
            prpp.FlashSettings[FlashSetting.PTFlash_Maximum_Number_Of_External_Iterations] = "1000";

            string[] compounds = { "Ethanol", "Water" };
            double[] mol_fractions = { 0.420707766066154, 0.579292233933846 };
            var mix = dtlc.CreateMaterialStream(compounds, mol_fractions);
            prpp.CurrentMaterialStream = mix;

            prpp.CalculateEquilibrium2(
                DWSIM.Interfaces.Enums.FlashCalculationType.PressureEnthalpy,
                101325, -646.276372295191, 0.0);
        }
    }
}

I got the following result:

[Thread ID: 1][2021/05/22 12:15:29] PH Flash [NL]: Current T = 346.316957349526, Current H Error = 558.355677974773
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #1, T = 363.671768605777, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #2, T = 363.322202241657, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #3, T = 363.080123382976, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #4, T = 362.924352228075, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #5, T = 362.828682010207, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #6, T = 362.763162863928, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #7, T = 362.691775185291, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #8, T = 362.537485788384, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #9, T = 362.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #10, T = 361.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #11, T = 360.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #12, T = 359.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #13, T = 358.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #14, T = 357.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #15, T = 356.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #16, T = 355.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #17, T = 354.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #18, T = 353.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #19, T = 352.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #20, T = 351.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #21, T = 350.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #22, T = 349.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #23, T = 348.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #24, T = 347.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #25, T = 346.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #26, T = 347.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #27, T = 346.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #28, T = 345.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #29, T = 344.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #30, T = 345.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #31, T = 344.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #32, T = 343.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #33, T = 344.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #34, T = 343.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #35, T = 342.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #36, T = 343.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #37, T = 342.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #38, T = 341.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #39, T = 342.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #40, T = 341.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #41, T = 340.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #42, T = 341.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #43, T = 340.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #44, T = 339.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #45, T = 340.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #46, T = 339.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #47, T = 338.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #48, T = 339.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #49, T = 338.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #50, T = 339.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #51, T = 338.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #52, T = 337.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #53, T = 338.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:29] PV Flash [NL]: Iteration #54, T = 337.027167079011, VF = 0.447260236654397
...
[Thread ID: 1][2021/05/22 12:15:30] PV Flash [NL]: Iteration #995, T = 338.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:30] PV Flash [NL]: Iteration #996, T = 337.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:30] PV Flash [NL]: Iteration #997, T = 338.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:30] PV Flash [NL]: Iteration #998, T = 337.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:30] PV Flash [NL]: Iteration #999, T = 338.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:30] PV Flash [NL]: Iteration #1000, T = 337.027167079011, VF = 0.447260236654397
[Thread ID: 1][2021/05/22 12:15:30] PV Flash [NL]: Iteration #1001, T = 338.027167079011, VF = 0.447260236654397

Unhandled Exception:
System.Exception: Flash PT: Máx Iterações Atingido. (T = 338.03 K, P = 101,325.00 Pa, MoleFracs = { 0.420707766066154, 0.579292233933846, })
  at DWSIM.Thermodynamics.PropertyPackages.Auxiliary.FlashAlgorithms.FlashAlgorithm.CalculateEquilibrium (DWSIM.Thermodynamics.PropertyPackages.FlashSpec spec1, DWSIM.Thermodynamics.PropertyPackages.FlashSpec spec2, System.Double val1, System.Double val2, DWSIM.Thermodynamics.PropertyPackages.PropertyPackage pp, System.Double[] mixmolefrac, System.Double[] initialKval, System.Double initialestimate) [0x0141f] in <3b8aec3983b24a018a3abcda5b2b4b41>:0 
  at DWSIM.Thermodynamics.PropertyPackages.PropertyPackage.CalculateEquilibrium (DWSIM.Interfaces.Enums.FlashCalculationType calctype, System.Double val1, System.Double val2, System.Double[] mixmolefrac, System.Double[] initialKval, System.Double initialestimate) [0x00063] in <3b8aec3983b24a018a3abcda5b2b4b41>:0 
  at DWSIM.Thermodynamics.PropertyPackages.PropertyPackage.CalculateEquilibrium2 (DWSIM.Interfaces.Enums.FlashCalculationType calctype, System.Double val1, System.Double val2, System.Double initialestimate) [0x0000c] in <3b8aec3983b24a018a3abcda5b2b4b41>:0 
  at TestMaxIterationError.MainClass.Main (System.String[] args) [0x000a3] in /Users/sakai/Projects/DTL_Test/TestMaxIterationError/TestMaxIterationError.cs:24 
[ERROR] FATAL UNHANDLED EXCEPTION: System.Exception: Flash PT: Máx Iterações Atingido. (T = 338.03 K, P = 101,325.00 Pa, MoleFracs = { 0.420707766066154, 0.579292233933846, })
  at DWSIM.Thermodynamics.PropertyPackages.Auxiliary.FlashAlgorithms.FlashAlgorithm.CalculateEquilibrium (DWSIM.Thermodynamics.PropertyPackages.FlashSpec spec1, DWSIM.Thermodynamics.PropertyPackages.FlashSpec spec2, System.Double val1, System.Double val2, DWSIM.Thermodynamics.PropertyPackages.PropertyPackage pp, System.Double[] mixmolefrac, System.Double[] initialKval, System.Double initialestimate) [0x0141f] in <3b8aec3983b24a018a3abcda5b2b4b41>:0 
  at DWSIM.Thermodynamics.PropertyPackages.PropertyPackage.CalculateEquilibrium (DWSIM.Interfaces.Enums.FlashCalculationType calctype, System.Double val1, System.Double val2, System.Double[] mixmolefrac, System.Double[] initialKval, System.Double initialestimate) [0x00063] in <3b8aec3983b24a018a3abcda5b2b4b41>:0 
  at DWSIM.Thermodynamics.PropertyPackages.PropertyPackage.CalculateEquilibrium2 (DWSIM.Interfaces.Enums.FlashCalculationType calctype, System.Double val1, System.Double val2, System.Double initialestimate) [0x0000c] in <3b8aec3983b24a018a3abcda5b2b4b41>:0 
  at TestMaxIterationError.MainClass.Main (System.String[] args) [0x000a3] in /Users/sakai/Projects/DTL_Test/TestMaxIterationError/TestMaxIterationError.cs:24 

Expected behavior

I expect the computation converged without making PTFlash_Maximum_Number_Of_External_Iterations too big or PVFlash_MaximumTemperatureChange too small.

Desktop (please complete the following information):

DanWBR commented 3 years ago

@msakai a special procedure was added to handle azeotropes, not sure if it is included in DTL v6.3.

msakai commented 3 years ago

If you mean the commit 60c2877f5c98d1f3aaaa412baa98a1a335ea75ab, it is in included in v6.1 and newer tags, and DTL_v6.3.7634 is based on DWSIM v6.3 according to its CHANGELOG.txt.

Anyway, it seems the newly released DTL_v6.5.0 does not have the problem. Thank you for releasing the new version!

DanWBR commented 3 years ago

@msakai I always do some work in the flash algorithms, maybe it was something I did after that.