public string Solve() { Compiler compiler = new Compiler(); if (frequencies.Count == 0) { frequencies.Add(0.0); } for (int i = 0; i < elements.Count; i++) { (elements[i] as Element).SetIndex(i); } string equations = GenerateEquations(); List <IScopeElement> scopeElements = new List <IScopeElement>(); foreach (var element in elements) { if (element is IScopeElement) { scopeElements.Add(element as IScopeElement); } } SteadyStateSolution solution = new SteadyStateSolution(frequencies.Count, scopeElements.Count); int frequencyIndex = 0; foreach (var frequency in frequencies) { string system = equations; system += "//Parameters" + Environment.NewLine; system += GenerateParameters(frequency); //parse equations NonlinearEquationDescription compiledEquation = compiler.CompileEquations(system); NonlinearSystemSymbolicAnalytic nonlinearSystem = new NonlinearSystemSymbolicAnalytic(compiledEquation); //solve Vector <double> values = solver.Solve(nonlinearSystem, Vector <double> .Build.DenseOfArray(compiledEquation.InitialValues)); NonlinearSystemSolution systemSolution = compiledEquation.GetSolution(values); //save results int index = 0; foreach (var scope in scopeElements) { solution.Set(scope.GetReading(systemSolution), index, frequencyIndex); index++; } frequencyIndex++; } return(solution.ToString(scopeElements, frequencies.ToArray())); }
public ACGraphSolution SolveEquationsAC(float frequency) { string equations = EquationGeneration(frequency); //create solver Compiler compiler = new Compiler(); NonlinearEquationDescription compiledEquation = compiler.CompileEquations(equations); MathUtils.NonlinearSystemSymbolicAnalytic system = new MathUtils.NonlinearSystemSymbolicAnalytic(compiledEquation); //calc solution Vector <double> values = MathUtils.NewtonRaphsonSolver.Solve( system, Vector <double> .Build.DenseOfArray(compiledEquation.InitialValues), 25, 0.005, 1.0 ); NonlinearSystemSolution solution = compiledEquation.GetSolution(values); ACGraphSolution acSolution = new ACGraphSolution(); acSolution.frequency = frequency; for (int i = 0; i < nodesList.Count; i++) { var real = $"v_{i}_re"; var im = $"v_{i}_im"; acSolution.voltages.Add(new Complex32((float)solution.GetValue(real), (float)solution.GetValue(im))); } foreach (var element in elements) { acSolution.currents.Add(element.GetCurrent(solution, frequency)); acSolution.voltageDrops.Add(element.GetVoltageDrop(solution)); } return(acSolution); /*for (int i = 0; i < elements.Count; i++) * { * ElementsAC.Element element = elements[i]; * switch (element.ElementType) * { * case ElementsAC.ElementTypeEnum.Capacitor: * { * Complex32 voltageDrop = solution.voltages[element.nodes[1]] - solution.voltages[element.nodes[0]]; * Complex32 current = voltageDrop * new Complex32(0.0f, frequency * ((ElementsAC.Capacitor)element).capacity); * solution.currents.Add(current); * break; * } * case ElementsAC.ElementTypeEnum.Resistor: * { * Complex32 voltageDrop = solution.voltages[element.nodes[1]] - solution.voltages[element.nodes[0]]; * solution.currents.Add(voltageDrop / ((ElementsAC.Resistor)element).resistance); * break; * } * case ElementsAC.ElementTypeEnum.CurrentSource: * ElementsAC.CurrentSource csel = (ElementsAC.CurrentSource)element; * solution.currents.Add(Complex32.FromPolarCoordinates(csel.current, csel.phase)); * break; * case ElementsAC.ElementTypeEnum.Transformer2w: * solution.currents.Add(0.0f); * break; * case ElementsAC.ElementTypeEnum.Inductor: * case ElementsAC.ElementTypeEnum.Ground: * case ElementsAC.ElementTypeEnum.Line: * case ElementsAC.ElementTypeEnum.VoltageSource: * solution.currents.Add(0.0f);//Пропуск значений, поскольку данные токи присутствуют в векторе решения * break; * } * }*/ }