public bool CalculatePQ(MaterialStream stream, double enthalpy) { var copy = new MaterialStream("copy", stream.System); copy.CopyFrom(stream); PrecalculateTP(copy); var problem2 = new EquationSystem() { Name = "PQ-Flash" }; copy.GetVariable("p").IsFixed = true; copy.GetVariable("T").IsFixed = false; copy.GetVariable("VF").IsFixed = false; copy.Init("VF", stream.Vfmolar.ValueInSI); foreach (var comp in stream.System.Components) { copy.GetVariable("n[" + comp.ID + "]").IsFixed = true; } problem2.AddConstraints((copy.Mixed.SpecificEnthalpy * copy.Mixed.TotalMolarflow).IsEqualTo(enthalpy)); copy.FillEquationSystem(problem2); var solver = new Decomposer(); solver.Solve(problem2); performMassBalance(copy, copy.KValues); performDensityUpdate(copy); performEnthalpyUpdate(copy); stream.CopyFrom(copy); return(true); }
public override ProcessUnit Initialize() { int NC = System.Components.Count; var In = FindMaterialPort("In"); var Vap = FindMaterialPort("Vap"); var Liq = FindMaterialPort("Liq"); var flash = new FlashRoutines(new Numerics.Solvers.Newton()); var flashStream = new MaterialStream("FLASH", System); flashStream.CopyFrom(In.Streams[0]); if (p.IsFixed) { flashStream.Specify("p", p.ValueInSI); } else if (dp.IsFixed) { flashStream.Specify("p", In.Streams[0].Mixed.Pressure.ValueInSI - dp.ValueInSI); } if (T.IsFixed) { flashStream.Specify("T", T.ValueInSI); } else { flashStream.Specify("T", In.Streams[0].Mixed.Temperature.ValueInSI); } if (VF.IsFixed) { flashStream.Specify("VF", VF.ValueInSI); } else { flashStream.Specify("VF", In.Streams[0].Vfmolar.ValueInSI); } if (T.IsFixed && p.IsFixed) { flash.CalculateTP(flashStream); } else if (VF.IsFixed && p.IsFixed) { flash.CalculateZP(flashStream); } else if (Q.IsFixed && p.IsFixed) { flashStream.Init("VF", In.Streams[0].GetVariable("VF").ValueInSI); flashStream.Init("T", In.Streams[0].GetVariable("T").ValueInSI); flash.CalculatePQ(flashStream, In.Streams[0].Mixed.SpecificEnthalpy.ValueInSI * In.Streams[0].Mixed.TotalMolarflow.ValueInSI); } else if (Q.IsFixed) { flashStream.Init("VF", In.Streams[0].GetVariable("VF").ValueInSI); flashStream.Init("T", In.Streams[0].GetVariable("T").ValueInSI); flash.CalculatePQ(flashStream, In.Streams[0].Mixed.SpecificEnthalpy.ValueInSI * In.Streams[0].Mixed.TotalMolarflow.ValueInSI); } else { flash.CalculateTP(flashStream); } // p.ValueInSI = In.Streams[0].Mixed.Pressure.ValueInSI; var eval = new Evaluator(); for (int i = 0; i < NC; i++) { Vap.Streams[0].Mixed.ComponentMolarflow[i].ValueInSI = (flashStream.Vapor.ComponentMolarflow[i]).Eval(eval); Liq.Streams[0].Mixed.ComponentMolarflow[i].ValueInSI = (flashStream.Liquid.ComponentMolarflow[i]).Eval(eval); } if (T.IsFixed) { Vap.Streams[0].Mixed.Temperature.ValueInSI = T.ValueInSI; Liq.Streams[0].Mixed.Temperature.ValueInSI = T.ValueInSI; } else { Vap.Streams[0].Mixed.Temperature.ValueInSI = flashStream.Mixed.Temperature.ValueInSI; Liq.Streams[0].Mixed.Temperature.ValueInSI = flashStream.Mixed.Temperature.ValueInSI; T.ValueInSI = flashStream.Mixed.Temperature.ValueInSI; } if (p.IsFixed) { Vap.Streams[0].Mixed.Pressure.ValueInSI = p.ValueInSI; Liq.Streams[0].Mixed.Pressure.ValueInSI = p.ValueInSI; } else { Vap.Streams[0].Mixed.Pressure.ValueInSI = flashStream.Mixed.Pressure.ValueInSI; Liq.Streams[0].Mixed.Pressure.ValueInSI = flashStream.Mixed.Pressure.ValueInSI; p.ValueInSI = flashStream.Mixed.Pressure.ValueInSI; } if (!Q.IsFixed) { Q.ValueInSI = -(In.Streams[0].Mixed.SpecificEnthalpy * In.Streams[0].Mixed.TotalMolarflow - Liq.Streams[0].Mixed.SpecificEnthalpy * Liq.Streams[0].Mixed.TotalMolarflow - Vap.Streams[0].Mixed.SpecificEnthalpy * Vap.Streams[0].Mixed.TotalMolarflow).Eval(eval); } if (!VF.IsFixed) { VF.ValueInSI = flashStream.Vfmolar.ValueInSI; } Vap.Streams[0].GetVariable("VF").SetValue(1); Liq.Streams[0].GetVariable("VF").SetValue(0); flash.CalculateTP(Vap.Streams[0]); flash.CalculateTP(Liq.Streams[0]); Vap.Streams[0].State = PhaseState.DewPoint; Liq.Streams[0].State = PhaseState.BubblePoint; return(this); }