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 void Can_Solve_Nonlinear_System_with_3_Variables() { var problem = new EquationSystem(); var x1 = new Variable("x1", 1); var x2 = new Variable("x2", 1); var x3 = new Variable("x3", 1); problem.AddVariables(x1, x2, x3); problem.AddConstraints(new Equation((3 * x1 - Sym.Cos(x2 * x3) - 3.0 / 2.0))); problem.AddConstraints(new Equation(4 * Sym.Pow(x1, 2) - 625 * Sym.Pow(x2, 2) + 2 * x2 - 1)); problem.AddConstraints(new Equation(Sym.Exp(-x1 * x2) + 20 * x3 + (10 * Math.PI - 3.0) / 3.0)); var solver = new Newton(); solver.Solve(problem); Assert.AreEqual(true, solver.IsConverged); Assert.AreEqual(8, solver.Iterations); }
protected void AddEquationToEquationSystem(EquationSystem system, Equation eq, string group) { eq.ModelName = Name; eq.ModelClass = Class; eq.Group = group; if (String.IsNullOrEmpty(eq.Name)) { eq.Name = "EQ" + (system.Equations.Count + 1).ToString("00000"); } system.AddConstraints(eq); }
static void Test4() { Console.WriteLine(); Console.WriteLine("### Test Problem: Powell Badly Scaled Function"); var problem = new EquationSystem(); var x1 = new Variable("x1", 2); var x2 = new Variable("x2", 1); problem.AddVariables(x1, x2); problem.AddConstraints(new Equation(10000 * x1 * x2 - 1)); problem.AddConstraints(new Equation(Sym.Exp(-x1) + Sym.Exp(-x2) - 1.0001)); // var solver = new Newton(); solver.OnLog += Console.WriteLine; solver.OnLogDebug += Console.WriteLine; solver.OnLogError += Console.WriteLine; solver.OnLogSuccess += Console.WriteLine; solver.Solve(problem); }
void PrecalculateZP(MaterialStream stream) { stream.Mixed.TotalMolarflow.ValueInSI = stream.Mixed.ComponentMolarflow.Sum(v => v.ValueInSI); var NC = stream.System.Components.Count; for (int i = 0; i < NC; i++) { stream.Mixed.ComponentMolarFraction[i].ValueInSI = stream.Mixed.ComponentMolarflow[i].ValueInSI / stream.Mixed.TotalMolarflow.ValueInSI; stream.Liquid.ComponentMolarFraction[i].ValueInSI = stream.Mixed.ComponentMolarFraction[i].ValueInSI; stream.Vapor.ComponentMolarFraction[i].ValueInSI = stream.Mixed.ComponentMolarFraction[i].ValueInSI; } var rachfordRice = Sym.Sum(0, NC, i => stream.Mixed.ComponentMolarFraction[i] * (1 - stream.KValues[i]) / (1 + stream.Vfmolar * (stream.KValues[i] - 1))); var rachfordRiceEq = rachfordRice.IsEqualTo(0); var problem2 = new EquationSystem() { Name = "Rachford-Rice" }; if (stream.Vfmolar.ValueInSI <= 1e-10) { problem2.AddConstraints(Sym.Sum(0, NC, i => stream.Mixed.ComponentMolarFraction[i] * stream.KValues[i]).IsEqualTo(1)); } else if (stream.Vfmolar.ValueInSI >= 1 - 1e-10) { problem2.AddConstraints(Sym.Sum(0, NC, i => stream.Mixed.ComponentMolarFraction[i] / stream.KValues[i]).IsEqualTo(1)); } else { problem2.AddConstraints(rachfordRiceEq); } problem2.AddVariables(stream.Mixed.Temperature); _solver.Solve(problem2); performMassBalance(stream, stream.KValues); performDensityUpdate(stream); performEnthalpyUpdate(stream); }
public void CanSolveMinProblem() { var problem = new EquationSystem(); var x1 = new Variable("x1", 6); problem.AddVariables(x1); problem.AddConstraints((Sym.Min(x1, 25)).IsEqualTo(5)); var solver = new Newton(); solver.Solve(problem); Assert.AreEqual(5, x1.ValueInSI, 1e-5); Assert.AreEqual(true, solver.IsConverged); }
/*Jacobian Density = 88,889% * Hessian Density = 0,000% * Solving NLP Problem(3 Variables, 3 Equations) * ITER SCALE D_NORM INF_PR ALG * 0001 1 1.6 620 ___(NEWTON, LU/MinimumDegreeAtPlusA) * 0002 1 0.48 153.0758 ___(NEWTON, LU/MinimumDegreeAtPlusA) * 0003 1 0.12 38.338978 ___(NEWTON, LU/MinimumDegreeAtPlusA) * 0004 1 0.055 9.1587665 ___(NEWTON, LU/MinimumDegreeAtPlusA) * 0005 1 0.02 1.9173079 ___(NEWTON, LU/MinimumDegreeAtPlusA) * 0006 1 0.0035 0.24865722 ___(NEWTON, LU/MinimumDegreeAtPlusA) * 0007 1 0.00011 0.0076250521 ___(NEWTON, LU/MinimumDegreeAtPlusA) * 0008 1 1.2E-07 8.137733E-06 ___(NEWTON, LU/MinimumDegreeAtPlusA) * [NewtonSolver] * Problem NLP Problem was successfully solved because constraint violation is below tolerance(8 iterations, 0,04 seconds, problem size: 3) ### Printing debugging information NLP Problem ###x1 = 0,833196581863439 ###x2 = 0,0549436583091183 ###x3 = -0,521361434378159 ### ### Generic 0,0000 ( ((4*(x1)^(2) - 625*(x2)^(2)) + 2*x2 - 1) == 0 ) ### Generic 0,0000 ( exp(-(x1)*x2) + 20*x3 + 9.471976 == 0 ) ### Generic 0,0000 ( ((3*x1 - cos(x2* x3)) - 1.5) == 0 )*/ static void Test1() { Console.WriteLine(); Console.WriteLine("### Test Problem: Difficult Function"); var problem = new EquationSystem(); var x1 = new Variable("x1", 1); var x2 = new Variable("x2", 1); var x3 = new Variable("x3", 1); problem.AddVariables(x1, x2, x3); problem.AddConstraints(new Equation((3 * x1 - Sym.Cos(x2 * x3) - 3.0 / 2.0))); problem.AddConstraints(new Equation(4 * Sym.Pow(x1, 2) - 625 * Sym.Pow(x2, 2) + 2 * x2 - 1)); problem.AddConstraints(new Equation(Sym.Exp(-x1 * x2) + 20 * x3 + (10 * Math.PI - 3.0) / 3.0)); var solver = new Newton(); solver.OnLog += Console.WriteLine; solver.OnLogError += Console.WriteLine; solver.OnLogSuccess += Console.WriteLine; solver.Solve(problem); }
static void Test3() { Console.WriteLine(); Console.WriteLine("### Test Problem: Well-conditioned at solution"); var problem = new EquationSystem(); var x1 = new Variable("x", 2); problem.AddVariables(x1); problem.AddConstraints(new Equation(Sym.Pow(x1, 3) - 1)); //problem.OnLog += Console.WriteLine; var solver = new Newton(); solver.OnLog += Console.WriteLine; solver.OnLogDebug += Console.WriteLine; solver.OnLogError += Console.WriteLine; solver.OnLogSuccess += Console.WriteLine; solver.Solve(problem); }
void PrecalculateTP(MaterialStream stream) { stream.Mixed.TotalMolarflow.ValueInSI = stream.Mixed.ComponentMolarflow.Sum(v => v.ValueInSI); var NC = stream.System.Components.Count; for (int i = 0; i < NC; i++) { stream.Mixed.ComponentMolarFraction[i].ValueInSI = stream.Mixed.ComponentMolarflow[i].ValueInSI / stream.Mixed.TotalMolarflow.ValueInSI; stream.Liquid.ComponentMolarFraction[i].ValueInSI = stream.Mixed.ComponentMolarFraction[i].ValueInSI; stream.Vapor.ComponentMolarFraction[i].ValueInSI = stream.Mixed.ComponentMolarFraction[i].ValueInSI; } //Solving Rachford Rice equation to get actual vapour fraction var x = stream.Mixed.ComponentMolarFraction; var K = stream.KValues; var a = stream.Vfmolar; var rachfordRice = Sym.Sum(0, NC, i => x[i] * (1 - K[i]) / (1 + a * (K[i] - 1))); //Evaluate Rachford-Rice at the edge cases of the VLE area stream.Vfmolar.ValueInSI = 0; var rrAt0 = rachfordRice.Eval(new Evaluator()); stream.Vfmolar.ValueInSI = 1; var rrAt1 = rachfordRice.Eval(new Evaluator()); stream.Vfmolar.ValueInSI = 0.5; if (rrAt0 > 0) { stream.Vfmolar.ValueInSI = 0; } else if (Math.Abs(rrAt0) < 1e-8) { stream.Vfmolar.ValueInSI = 0; } else if (Math.Abs(rrAt1) < 1e-8) { stream.Vfmolar.ValueInSI = 1; } else if (rrAt1 < 0) { stream.Vfmolar.ValueInSI = 1; } if (stream.Vfmolar.ValueInSI == 0.5 || (rrAt0 == 0 && rrAt1 == 0)) { for (int i = 0; i < NC; i++) { stream.Liquid.ComponentMolarFraction[i].ValueInSI = 0.95 * stream.Mixed.ComponentMolarFraction[i].ValueInSI; stream.Vapor.ComponentMolarFraction[i].ValueInSI = 1.05 * stream.Mixed.ComponentMolarFraction[i].ValueInSI; } stream.Vfmolar.ValueInSI = 0.5; var problem2 = new EquationSystem() { Name = "Rachford-Rice" }; stream.Vfmolar.LowerBound = -5; stream.Vfmolar.UpperBound = 5; problem2.AddConstraints(rachfordRice.IsEqualTo(0)); problem2.AddVariables(stream.Vfmolar); _solver.Solve(problem2); } if (Double.IsNaN(stream.Vfmolar.ValueInSI)) { stream.Vfmolar.ValueInSI = 0; } stream.Vfmolar.LowerBound = 0; stream.Vfmolar.UpperBound = 1; if (stream.Vfmolar.ValueInSI > 1) { stream.Vfmolar.ValueInSI = 1; } if (stream.Vfmolar.ValueInSI < 0) { stream.Vfmolar.ValueInSI = 0; } performMassBalance(stream, stream.KValues); performDensityUpdate(stream); performEnthalpyUpdate(stream); }