public static CompressedColumnStorage <double> ConvertSparsityJacobian(AlgebraicSystem problem) { var sparseJacobian = new CoordinateStorage <double>(problem.NumberOfEquations, problem.NumberOfVariables, problem.Jacobian.Count); foreach (var entry in problem.Jacobian) { sparseJacobian.At(entry.EquationIndex, entry.VariableIndex, 1); } var compJacobian = CSparse.Converter.ToCompressedColumnStorage(sparseJacobian); return(compJacobian); }
public static CompressedColumnStorage <double> FillResidualAndJacobian(AlgebraicSystem system, Vector b) { var sparseJacobian = new CoordinateStorage <double>(system.NumberOfEquations, system.NumberOfVariables, system.Jacobian.Count); int j = -1; double grad = -1; for (int i = 0; i < system.Equations.Count; i++) { system.Equations[i].Reset(); b[i] = system.Equations[i].Residual(); foreach (var variable in system.Equations[i].Variables) { if (system.VariableIndex.TryGetValue(variable, out j)) { grad = system.Equations[i].Expression.Diff(variable); sparseJacobian.At(i, j, grad); } } } /* int j = -1; * double grad = -1; * * Parallel.For(0, system.Equations.Count, i => * { * system.Equations[i].Reset(); * b[i] = system.Equations[i].Residual(); * * foreach (var variable in system.Equations[i].Variables) * { * if (system.VariableIndex.TryGetValue(variable, out j)) * { * grad = system.Equations[i].Expression.Diff(variable); * sparseJacobian.At(i, j, grad); * } * } * });*/ return(CSparse.Converter.ToCompressedColumnStorage(sparseJacobian)); }
public void Initialize(ILogger logger) { _logger = logger; StateValues = new double[DifferentialVariables.Count]; SystemToSolve = new AlgebraicSystem(Name); foreach (var vari in AlgebraicVariables) { SystemToSolve.AddVariables(vari); } foreach (var vari in DifferentialVariables) { SystemToSolve.AddVariable(vari); } foreach (var eq in Equations) { SystemToSolve.AddEquation(eq); } SystemToSolve.CreateIndex(); SystemToSolve.GenerateJacobian(); }