protected override void OnGenerateEquations(EquationSystem sys) { sys.AddParameter(dx); sys.AddParameter(dy); //sketch.GenerateEquations(sys); base.OnGenerateEquations(sys); }
public override void FillEquationSystem(EquationSystem problem) { int NC = System.Components.Count; var In = FindMaterialPort("In"); var Out1 = FindMaterialPort("Out1"); var Out2 = FindMaterialPort("Out2"); for (int i = 0; i < NC; i++) { var cindex = i; AddEquationToEquationSystem(problem, (SplitFactor * In.Streams[0].Mixed.ComponentMolarflow[cindex]) .IsEqualTo(Out1.Streams[0].Mixed.ComponentMolarflow[cindex]), "Mass Balance"); } for (int i = 0; i < NC; i++) { var cindex = i; AddEquationToEquationSystem(problem, (Sym.Par(1 - SplitFactor) * In.Streams[0].Mixed.ComponentMolarflow[cindex]) .IsEqualTo(Out2.Streams[0].Mixed.ComponentMolarflow[cindex]), "Mass Balance"); } AddEquationToEquationSystem(problem, (p / 1e4).IsEqualTo((In.Streams[0].Mixed.Pressure - dp) / 1e4), "Pressure drop"); AddEquationToEquationSystem(problem, (Out1.Streams[0].Mixed.Pressure / 1e4).IsEqualTo(Sym.Par(p) / 1e4), "Pressure Balance"); AddEquationToEquationSystem(problem, (Out1.Streams[0].Mixed.Temperature / 1e3).IsEqualTo(Sym.Par(In.Streams[0].Mixed.Temperature) / 1e3), "Temperature Balance"); AddEquationToEquationSystem(problem, (Out2.Streams[0].Mixed.Pressure / 1e4).IsEqualTo(Sym.Par(p) / 1e4), "Pressure Balance"); AddEquationToEquationSystem(problem, (Out2.Streams[0].Mixed.Temperature / 1e3).IsEqualTo(Sym.Par(In.Streams[0].Mixed.Temperature) / 1e3), "Temperature Balance"); base.FillEquationSystem(problem); }
/// <summary>Gets coeffitients for Gear integration method of given order.</summary> /// <param name="order"></param> /// <returns></returns> public static double[] GetCoefficients(int order) { if (order < 0) { throw new ArgumentOutOfRangeException(nameof(order)); } // see http://qucs.sourceforge.net/tech/node24.html#SECTION00713100000000000000 for details var es = new EquationSystem(order + 1); es.RightHandSide[0] = 1; for (var i = 1; i < es.VariablesCount; i++) { es.RightHandSide[i] = 1; es.Matrix[0, i] = 1; es.Matrix[i, 0] = i; var b = -(i - 1); for (var row = 1; row < es.VariablesCount; row++) { es.Matrix[row, i] = b; b *= -(i - 1); } } es.Solve(); return(es.Solution); }
/// <summary>Gets coefficients for the Adams-Moulton integration of given order.</summary> /// <param name="order">Order of the integration method</param> /// <returns></returns> public static double[] GetCoefficients(int order) { if (order <= 0) { throw new ArgumentOutOfRangeException(nameof(order)); } // see http://qucs.sourceforge.net/tech/node24.html#eq:MoultonInt for details var es = new EquationSystem(order); es.Matrix[0, 0] = 1; es.RightHandSide[0] = 1; for (var i = 1; i < order; i++) { var parity = i % 2 > 0 ? -1 : 1; es.Matrix[0, i] = 1; es.Matrix[i, 0] = parity; es.RightHandSide[i] = parity / (i + 1.0); var b = i - 1; for (var row = 1; row < order; row++) { es.Matrix[row, i] = b; b *= i - 1; } } es.Solve(); return(es.Solution); }
protected void AddVariableToEquationSystem(EquationSystem system, Variable vari, string group) { vari.ModelName = Name; vari.ModelClass = Class; vari.Group = group; system.AddVariables(vari); }
public EquationSystem.SolveResult Solve() { var sys = new EquationSystem(); GenerateEquations(sys); return(sys.Solve()); }
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 void FillEquationSystem(EquationSystem problem) { int NC = System.Components.Count; var In = FindMaterialPort("In"); var Out = FindMaterialPort("Out"); for (int i = 0; i < NC; i++) { var cindex = i; AddEquationToEquationSystem(problem, Sym.Sum(0, In.NumberOfStreams, (j) => In.Streams[j].Mixed.ComponentMolarflow[cindex]) .IsEqualTo(Sym.Sum(0, Out.NumberOfStreams, (j) => Out.Streams[j].Mixed.ComponentMolarflow[cindex])), "Mass Balance"); } AddEquationToEquationSystem(problem, (p / 1e4).IsEqualTo(Sym.Par(Sym.Min(In.Streams[0].Mixed.Pressure, In.Streams[1].Mixed.Pressure) - dp) / 1e4), "Pressure Balance"); foreach (var outlet in Out.Streams) { AddEquationToEquationSystem(problem, (outlet.Mixed.Pressure / 1e4).IsEqualTo(Sym.Par(p) / 1e4), "Pressure drop"); } AddEquationToEquationSystem(problem, ((Sym.Sum(0, In.NumberOfStreams, (i) => In.Streams[i].Mixed.SpecificEnthalpy * In.Streams[i].Mixed.TotalMolarflow) / 1e4)) .IsEqualTo(Sym.Sum(0, Out.NumberOfStreams, (i) => Out.Streams[i].Mixed.SpecificEnthalpy * Out.Streams[i].Mixed.TotalMolarflow) / 1e4), "Heat Balance"); base.FillEquationSystem(problem); }
public static bool Solve(EquationSystem system, float[] x0, out float[] x, int iterations, float precision, out int iterationsUsed) { var dimension = x0.Length; var x01 = new float[dimension]; var y = new float[dimension]; x = new float[dimension]; x0.CopyTo(x01, 0); iterationsUsed = iterations; for (int index = 1; index <= iterations; ++index) { if (!Iteration(system, x01, ref x)) { return(false); } system.Calculate(x, y); float num = 0; for (int i = 0; i < y.Length; i++) { num += y[i] * y[i]; } if (num <= precision) { iterationsUsed = index - 1; return(true); } x.CopyTo(x01, 0); } return(false); }
private void SolveButton_Click(object sender, EventArgs e) { if (E1C1textBox.Text == "" | E1C2textBox.Text == "" | E1C3textBox.Text == "" | E1C4textBox.Text == "" | E2C1textBox.Text == "" | E2C2textBox.Text == "" | E2C3textBox.Text == "" | E2C4textBox.Text == "" | E3C1textBox.Text == "" | E3C2textBox.Text == "" | E3C3textBox.Text == "" | E3C4textBox.Text == "") { MessageBox.Show("Введены не все коэффициенты уравнений в системе.", "Ошибка!"); } else { coeffs[0, 0] = double.Parse(E1C1textBox.Text); coeffs[0, 1] = double.Parse(E1C2textBox.Text); coeffs[0, 2] = double.Parse(E1C3textBox.Text); coeffs[0, 3] = double.Parse(E1C4textBox.Text); coeffs[1, 0] = double.Parse(E2C1textBox.Text); coeffs[1, 1] = double.Parse(E2C2textBox.Text); coeffs[1, 2] = double.Parse(E2C3textBox.Text); coeffs[1, 3] = double.Parse(E2C4textBox.Text); coeffs[2, 0] = double.Parse(E3C1textBox.Text); coeffs[2, 1] = double.Parse(E3C2textBox.Text); coeffs[2, 2] = double.Parse(E3C3textBox.Text); coeffs[2, 3] = double.Parse(E3C4textBox.Text); char[] varChars = { 'x', 'y', 'z' }; EquationSystem equationSystemWith3Eq = new EquationSystem(3, coeffs, varChars); MessageBox.Show($"{equationSystemWith3Eq.SolveEquationsSystem()}", "Решение"); } }
protected override bool OnSatisfy() { EquationSystem sys = new EquationSystem(); sys.AddParameters(parameters); var exprs = equations.ToList(); sys.AddEquations(equations); double bestI = 0.0; double min = -1.0; for (double i = 0.0; i < 1.0; i += 0.25 / 2.0) { value.value = i; sys.Solve(); double cur_value = exprs.Sum(e => Math.Abs(e.Eval())); if (min >= 0.0 && min < cur_value) { continue; } bestI = value.value; min = cur_value; } value.value = bestI; return(true); }
EquationSystem GetEquationSystem(MaterialStream stream) { var eq = new EquationSystem(); eq.TreatFixedVariablesAsConstants = true; stream.FillEquationSystem(eq); return(eq); }
protected virtual bool OnSatisfy() { EquationSystem sys = new EquationSystem(); sys.AddParameter(value); sys.AddEquations(equations); return(sys.Solve() == EquationSystem.SolveResult.OKAY); }
public override void GenerateEquations(EquationSystem sys) { base.GenerateEquations(sys); sketch.GenerateEquations(sys); if (solveParent && source != null) { source.GenerateEquations(sys); } }
public EquationSystem Solve(Flowsheet flowsheet) { var equationSystem = new EquationSystem(); equationSystem.Name = flowsheet.Name; flowsheet.FillEquationSystem(equationSystem); newton.Solve(equationSystem); return(equationSystem); }
public EquationSystem Decompose(Flowsheet flowsheet) { var equationSystem = new EquationSystem(); equationSystem.Name = flowsheet.Name; flowsheet.FillEquationSystem(equationSystem); decomp.Solve(equationSystem); return(equationSystem); }
protected virtual bool OnSatisfy() { EquationSystem sys = new EquationSystem(); sys.revertWhenNotConverged = false; sys.AddParameter(t0); sys.AddParameter(t1); sys.AddEquations(equations); return(sys.Solve() == EquationSystem.SolveResult.OKAY); }
protected override void OnGenerateEquations(EquationSystem sys) { if (!angleFixed) { sys.AddParameter(angle); } if (!stepFixed) { sys.AddParameter(step); } }
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); }
public void GenerateEquations(EquationSystem sys) { sys.AddParameters(parameters); sys.AddEquation(u.Magnitude() - 1.0); sys.AddEquation(v.Magnitude() - 1.0); var cross = ExpVector.Cross(u, v); var dot = ExpVector.Dot(u, v); sys.AddEquation(Exp.Atan2(cross.Magnitude(), dot) - Math.PI / 2); sys.AddEquation(n - ExpVector.Cross(u, v)); }
public void GenerateEquations(EquationSystem system) { foreach (var e in entities) { system.AddParameters(e.parameters); system.AddEquations(e.equations); } foreach (var c in constraints) { system.AddParameters(c.parameters); system.AddEquations(c.equations); } }
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); }
public DulmageMendelsohn Generate(EquationSystem problem) { var A = CSparseWrapper.ConvertSparsityJacobian(problem); var dm = DulmageMendelsohn.Generate(A, 1); A.PermuteRows(dm.p); A.PermuteColumns(dm.q); //foreach (var value in A.EnumerateIndexed()) //{ // sw.WriteLine("{0},{1}", value.Item1, value.Item2); //} StringBuilder sb = new StringBuilder(); sb.AppendLine("Coarse Structure"); sb.AppendLine("Underdetermined : "); sb.AppendLine("Determined : "); sb.AppendLine("Overdetermined : "); sb.AppendLine("Fine Structure"); for (int i = dm.Blocks - 1; i >= 0; i--) { sb.AppendLine(String.Format("Block {0}: V {1} - {2} E {3} - {4}", i, dm.s[i], dm.s[i + 1] - 1, dm.r[i], dm.r[i + 1] - 1)); var varcount = dm.s[i + 1] - dm.s[i]; for (int j = 0; j < varcount; j++) { var vari = dm.q[dm.s[i] + j]; sb.Append(problem.Variables[vari] + ", "); } var eqcount = dm.r[i + 1] - dm.r[i]; for (int j = 0; j < eqcount; j++) { var vari = dm.p[dm.r[i] + j]; // sb.Append(problem.Constraints[vari] + ", "); } sb.AppendLine(""); } // Console.WriteLine((sb.ToString())); return(dm); }
public static Vector FillResiduals(EquationSystem system, Evaluator evaluator) { var b = new Vector(system.Equations.Count); for (int i = 0; i < system.Equations.Count; i++) { //system.Equations[i].ResetIncidenceVector(); var val = system.Equations[i].Residual(evaluator); if (Double.IsNaN(val) || Double.IsInfinity(val)) { continue; } b[i] = val; } return(b); }
public override void FillEquationSystem(EquationSystem problem) { int NC = System.Components.Count; var In = FindMaterialPort("In"); var Out = FindMaterialPort("Out"); Expression DHRtotal = 0; for (int i = 0; i < NC; i++) { var cindex = i; Expression reactingMoles = 0; for (int j = 0; j < _numberOfReactions; j++) { if (Math.Abs(_stochiometry[j, i]) > 1e-16) { reactingMoles += _stochiometry[j, i] * R[j]; if (Math.Abs(DHR[j].ValueInSI) > 1e-16) { DHRtotal += reactingMoles * DHR[j]; } } } AddEquationToEquationSystem(problem, Sym.Sum(0, In.NumberOfStreams, (j) => In.Streams[j].Mixed.ComponentMolarflow[cindex] + reactingMoles) .IsEqualTo(Sym.Sum(0, Out.NumberOfStreams, (j) => Out.Streams[j].Mixed.ComponentMolarflow[cindex])), "Mass Balance"); } AddEquationToEquationSystem(problem, (p / 1e4).IsEqualTo(Sym.Par(In.Streams[0].Mixed.Pressure - dp) / 1e4), "Pressure Balance"); foreach (var outlet in Out.Streams) { AddEquationToEquationSystem(problem, (outlet.Mixed.Pressure / 1e4).IsEqualTo(p / 1e4), "Pressure drop"); AddEquationToEquationSystem(problem, (outlet.Mixed.Temperature / 1e3).IsEqualTo(T / 1e3), "Heat Balance"); } AddEquationToEquationSystem(problem, ((Sym.Sum(0, In.NumberOfStreams, (i) => In.Streams[i].Mixed.SpecificEnthalpy * In.Streams[i].Mixed.TotalMolarflow + Q + DHRtotal) / 1e4)) .IsEqualTo(Sym.Par(Sym.Sum(0, Out.NumberOfStreams, (i) => Out.Streams[i].Mixed.SpecificEnthalpy * Out.Streams[i].Mixed.TotalMolarflow)) / 1e4), "Heat Balance"); base.FillEquationSystem(problem); }
public virtual void FillEquationSystem(EquationSystem problem) { foreach (var vari in Variables) { vari.ModelClass = Class; vari.ModelName = Name; if (vari.IsFixed) { if (!problem.TreatFixedVariablesAsConstants && !vari.IsConstant) { //if (vari.DefiningExpression == null) //{ // problem.AddVariables(vari); //} double scale = 1.0; if (vari.Dimension == PhysicalDimension.Pressure) { scale = 1e5; } if (vari.Dimension == PhysicalDimension.Temperature) { scale = 1e3; } AddEquationToEquationSystem(problem, (vari / scale).IsEqualTo(vari.ValueInSI / scale), "Specification"); // problem.RemoveVariable(vari); } else { problem.RemoveVariable(vari); } } if (vari.DefiningExpression != null) { problem.AddDefinedVariables(vari); } else { if (!(problem.TreatFixedVariablesAsConstants && vari.IsFixed) && !vari.IsConstant) { problem.AddVariables(vari); } } } }
public EquationSystem Solve(Flowsheet flowsheet, double brakeFactor) { var equationSystem = new EquationSystem(); equationSystem.Name = flowsheet.Name; flowsheet.FillEquationSystem(equationSystem); var oldState = newton.DoLinesearch; var oldFactor = newton.BrakeFactor; newton.DoLinesearch = false; newton.BrakeFactor = brakeFactor; newton.Solve(equationSystem); newton.BrakeFactor = oldFactor; newton.DoLinesearch = oldState; return(equationSystem); }
public override void FillEquationSystem(EquationSystem problem) { int NC = System.Components.Count; var In = FindMaterialPort("In"); var Out = FindMaterialPort("Out"); for (int i = 0; i < NC; i++) { var cindex = i; AddEquationToEquationSystem(problem, Sym.Sum(0, In.NumberOfStreams, (j) => In.Streams[j].Mixed.ComponentMolarflow[cindex]) .IsEqualTo(Sym.Sum(0, Out.NumberOfStreams, (j) => Out.Streams[j].Mixed.ComponentMolarflow[cindex])), "Mass Balance"); } AddEquationToEquationSystem(problem, (p1 / 1e4).IsEqualTo(Sym.Par(In.Streams[0].Mixed.Pressure) / 1e4), "Pressure Balance"); AddEquationToEquationSystem(problem, (p2 / 1e4).IsEqualTo(Sym.Par(Out.Streams[0].Mixed.Pressure) / 1e4), "Pressure Balance"); AddEquationToEquationSystem(problem, (dp / 1e4).IsEqualTo(Sym.Par(p1 - p2) / 1e4), "Pressure drop"); if (CharacteristicCurve != ValveCharacteristic.User) { AddEquationToEquationSystem(problem, (Opening).IsEqualTo(GetCharacteristicCurve()), "Performance"); } AddEquationToEquationSystem(problem, (KV).IsEqualTo(Opening / 100 * KVS), "Performance"); if (Mode == FlowMode.Compressible) { AddEquationToEquationSystem(problem, (Sym.Convert(Out.Streams[0].Mixed.TotalVolumeflow, (SI.m ^ 3) / SI.h)).IsEqualTo(KV * Sym.Sqrt(Sym.Convert(dp, METRIC.bar) * Out.Streams[0].Mixed.Density / 999.07)), "Performance"); } else if (Mode == FlowMode.Incompressible) { } else if (Mode == FlowMode.Multiphase) { } //Isenthalpic Valve AddEquationToEquationSystem(problem, ((Sym.Sum(0, In.NumberOfStreams, (i) => In.Streams[i].Mixed.SpecificEnthalpy * In.Streams[i].Mixed.TotalMolarflow) / 1e4)) .IsEqualTo(Sym.Par(Sym.Sum(0, Out.NumberOfStreams, (i) => Out.Streams[i].Mixed.SpecificEnthalpy * Out.Streams[i].Mixed.TotalMolarflow)) / 1e4), "Heat Balance"); base.FillEquationSystem(problem); }
/// <summary> /// Solves the unit together with the output material streams as a single flowsheet. When using this method, the unit has to be specified fully. /// </summary> public virtual ProcessUnit Solve() { var decomp = new Decomposer(); var flowsheet = new Flowsheet(Name); flowsheet.AddUnit(this); foreach (var stream in MaterialPorts.Where(p => p.Direction == PortDirection.Out && p.IsConnected).Select(p => p.Streams.ToArray())) { flowsheet.AddMaterialStreams(stream); } var problem = new EquationSystem(); flowsheet.FillEquationSystem(problem); decomp.Solve(problem); return(this); }
public override void FillEquationSystem(EquationSystem problem) { int NC = System.Components.Count; var In = FindMaterialPort("In"); var VIN = FindMaterialPort("VIn"); var LIN = FindMaterialPort("LIn"); var Vap = FindMaterialPort("VOut"); var Liq = FindMaterialPort("LOut"); Vap.Streams[0].State = PhaseState.DewPoint; Liq.Streams[0].State = PhaseState.BubblePoint; for (int i = 0; i < NC; i++) { var cindex = i; AddEquationToEquationSystem(problem, (In.Streams[0].Mixed.ComponentMolarflow[cindex] + VIN.Streams[0].Mixed.ComponentMolarflow[cindex] + LIN.Streams[0].Mixed.ComponentMolarflow[cindex]) .IsEqualTo(Vap.Streams[0].Mixed.ComponentMolarflow[cindex] + Liq.Streams[0].Mixed.ComponentMolarflow[cindex]), "Mass Balance"); } AddEquationToEquationSystem(problem, (p / 1e4).IsEqualTo(Sym.Par(Sym.Min(In.Streams[0].Mixed.Pressure, VIN.Streams[0].Mixed.Pressure) - dp) / 1e4), "Pressure drop"); AddEquationToEquationSystem(problem, (Vap.Streams[0].Mixed.Pressure / 1e4).IsEqualTo(Sym.Par(p) / 1e4), "Pressure Balance"); AddEquationToEquationSystem(problem, (Vap.Streams[0].Mixed.Temperature / 1e3).IsEqualTo(T / 1e3), "Temperature Balance"); AddEquationToEquationSystem(problem, (Liq.Streams[0].Mixed.Pressure / 1e4).IsEqualTo(Sym.Par(p) / 1e4), "Pressure Balance"); AddEquationToEquationSystem(problem, (Liq.Streams[0].Mixed.Temperature / 1e3).IsEqualTo(T / 1e3), "Temperature Balance"); AddEquationToEquationSystem(problem, ((In.Streams[0].Mixed.SpecificEnthalpy * In.Streams[0].Mixed.TotalMolarflow + LIN.Streams[0].Mixed.SpecificEnthalpy * LIN.Streams[0].Mixed.TotalMolarflow + VIN.Streams[0].Mixed.SpecificEnthalpy * VIN.Streams[0].Mixed.TotalMolarflow + Q) / 1e4) .IsEqualTo(Sym.Par(Vap.Streams[0].Mixed.SpecificEnthalpy * Vap.Streams[0].Mixed.TotalMolarflow + Liq.Streams[0].Mixed.SpecificEnthalpy * Liq.Streams[0].Mixed.TotalMolarflow) / 1e4), "Heat Balance"); for (int i = 0; i < NC; i++) { System.EquationFactory.EquilibriumCoefficient(System, K[i], T, p, Liq.Streams[0].Mixed.ComponentMolarFraction, Vap.Streams[0].Mixed.ComponentMolarFraction, i); AddEquationToEquationSystem(problem, Vap.Streams[0].Mixed.ComponentMolarFraction[i].IsEqualTo(K[i] * Liq.Streams[0].Mixed.ComponentMolarFraction[i]), "Equilibrium"); } base.FillEquationSystem(problem); }