public void CreateAndInvertCoarseProblemMatrix(Dictionary <int, HashSet <INode> > cornerNodesOfSubdomains, FetiDPDofSeparator dofSeparator, Dictionary <int, IFetiDPSubdomainMatrixManager> matrixManagers) { SkylineMatrix globalKccStar = CreateGlobalKccStar(cornerNodesOfSubdomains, dofSeparator, matrixManagers); this.inverseGlobalKccStar = globalKccStar.FactorLdl(true); }
/// <summary> /// Reads the values and diagonal offsets arrays of a matrix in Skyline format from 2 separate files. /// </summary> /// <param name="valuesArrayPath">Each entry on a separate line. The first must be the array length.</param> /// <param name="diagOffsetsArrayPath">Each entry on a separate line. The first must be the array length.</param> /// <param name="checkSkylineArrays"> /// If true, the provided arrays will be checked to make sure they are valid Skyline arrays, which is safer. /// If false, no such check will take place, which is faster. /// </param> public SkylineMatrix ReadSkylineMatrixFromSeparateFiles(string valuesArrayPath, string diagOffsetsArrayPath, bool checkSkylineArrays) { // Values array double[] values; using (var reader = new StreamReader(valuesArrayPath)) { string line = reader.ReadLine(); int length = int.Parse(line.Trim()); values = new double[length]; for (int i = 0; i < length; ++i) { line = reader.ReadLine(); values[i] = double.Parse(line.TrimEnd()); } } // Diagonal offsets array int[] diagOffsets; using (var reader = new StreamReader(diagOffsetsArrayPath)) { string line = reader.ReadLine(); int length = int.Parse(line.Trim()); diagOffsets = new int[length]; for (int i = 0; i < length; ++i) { line = reader.ReadLine(); diagOffsets[i] = int.Parse(line.TrimEnd()); } } return(SkylineMatrix.CreateFromArrays(diagOffsets.Length - 1, values, diagOffsets, checkSkylineArrays)); }
private static void TestSkylinePosDef() { var matrix = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); TestWriteOperation(matrix, SparsePosDef10by10.SkylineArraysPath); }
/// <summary> /// Reads the Skyline values and diagonal offsets arrays from 2 different files. The first entry in each file must be /// the length of the corresponding array. /// </summary> /// <param name="valuesPath"> /// The absolute path of an array containing the values array of the Skyline format. The first entry must be its entry, /// which is equal to the number of nonzero entries. /// </param> /// <param name="diagonalOffsetsPath"> /// The absolute path of an array containing the diagonal offsets array of the Skyline format. The first entry must be /// its entry, which is equal to the number of rows/columns + 1. /// </param> public static SkylineMatrix ReadFromFiles(string valuesPath, string diagonalOffsetsPath) { FileStream fs = File.OpenRead(diagonalOffsetsPath); BinaryReader bw = new BinaryReader(fs); int length = 0; length = bw.ReadInt32(); int[] diagOffsets = new int[length]; for (int i = 0; i < diagOffsets.Length; i++) { diagOffsets[i] = bw.ReadInt32(); } bw.Close(); fs.Close(); fs = File.OpenRead(valuesPath); bw = new BinaryReader(fs); length = bw.ReadInt32(); double[] values = new double[length]; for (int i = 0; i < values.Length; i++) { values[i] = bw.ReadDouble(); } bw.Close(); fs.Close(); return(SkylineMatrix.CreateFromArrays(diagOffsets.Length - 1, values, diagOffsets, true, false)); }
private static void TestEquality() { // Equals(SkylineMatrix) var full1 = Matrix.CreateFromArray(SparsePosDef10by10.Matrix); var skyline1 = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); Assert.True(full1.Equals(skyline1)); // Equals(CsrMatrix) var full2 = Matrix.CreateFromArray(SparseRectangular10by5.Matrix); var csr2 = CsrMatrix.CreateFromArrays(SparseRectangular10by5.NumRows, SparseRectangular10by5.NumCols, SparseRectangular10by5.CsrValues, SparseRectangular10by5.CsrColIndices, SparseRectangular10by5.CsrRowOffsets, true); Assert.True(full2.Equals(csr2)); // Equals(CscMatrix) var full3 = Matrix.CreateFromArray(SparseRectangular10by5.Matrix); var csc3 = CscMatrix.CreateFromArrays(SparseRectangular10by5.NumRows, SparseRectangular10by5.NumCols, SparseRectangular10by5.CscValues, SparseRectangular10by5.CscRowIndices, SparseRectangular10by5.CscColOffsets, true); Assert.True(full3.Equals(csc3)); }
private static void TestArrayCopy() { var skyline = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); comparer.AssertEqual(SparsePosDef10by10.Matrix, skyline.CopyToArray2D()); }
public TestSkylineMatrix() { di = new double[] { 1, 2, 3 }; al = new double[] { 1, 2, 3 }; au = new double[] { 3, 2, 1 }; ia = new int[] { 1, 1, 2, 4 }; skylineMatrix = new SkylineMatrix(di, ia, al, au); }
private static void CheckFactorization(SkylineMatrix originalMatrix, int expectedNullity, double tolerance) { var factorization = originalMatrix.FactorSemidefiniteLdl(false, tolerance); Assert.True(factorization.DependentColumns.Count == expectedNullity, $"Nullity = {factorization.DependentColumns.Count} != {expectedNullity} (expected)"); SemidefiniteCholeskyFullTests.CheckNullSpace(originalMatrix, factorization.NullSpaceBasis, tolerance); }
private static void TestEquality() { var full = Matrix.CreateFromArray(SparsePosDef10by10.Matrix); var skyline = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); Assert.True(skyline.Equals(full)); }
private static void TestClear() { var zero = Matrix.CreateZero(SparsePosDef10by10.Order, SparsePosDef10by10.Order); var skyline = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); skyline.Clear(); comparer.AssertEqual(zero, skyline); }
public TestSkylineMatrix(ITestOutputHelper testOutputHelper) { di = new double[] { 1, 2, 3 }; al = new double[] { 1, 2, 3 }; au = new double[] { 3, 2, 1 }; ia = new int[] { 1, 1, 2, 4 }; skylineMatrix = new SkylineMatrix(di, ia, al, au); _testOutputHelper = testOutputHelper; }
public void SymmetricSkylineMatrix_Foreach() { var di = new double[] { 1, 2, 3 }; var al = new double[] { 1, 2, 3 }; var au = new double[] { 1, 2, 3 }; var ia = new int[] { 1, 1, 2, 4 }; SkylineMatrix skylineMatrix = new SkylineMatrix(di, ia, al, au); Assert.True(new HashSet <(double, int, int)>(symmetricSkylineMatrix).SetEquals(skylineMatrix)); }
//TODO: Move this method to DokSymmetric, so that I can optimize access to its private data internal static SkylineMatrix GetSubmatrixSymmetricSkyline(double[] skyValues, int[] skyDiagOffsets, int[] rowsColsToKeep) { //TODO: perhaps this can be combined with the CSC and full version to get all 2 submatrices needed for // Schur complements more efficiently. int subOrder = rowsColsToKeep.Length; if (subOrder == 0) { return(SkylineMatrix.CreateFromArrays(subOrder, new double[0], new int[1] { 0 }, false, false)); } var submatrix = DokSymmetric.CreateEmpty(subOrder); for (int subCol = 0; subCol < subOrder; ++subCol) { int col = rowsColsToKeep[subCol]; int diagOffset = skyDiagOffsets[col]; int colHeight = skyDiagOffsets[col + 1] - diagOffset - 1; // excluding diagonal for (int subRow = 0; subRow <= subCol; ++subRow) { int row = rowsColsToKeep[subRow]; if (row <= col) { int entryHeight = col - row; // excluding diagonal if (entryHeight <= colHeight) // inside stored non zero pattern { double val = skyValues[diagOffset + entryHeight]; if (val != 0.0) { submatrix.SetEntryUpper(subRow, subCol, val); // Skyline format stores many unnecessary zeros. } } } else // Transpose row <-> col. The cached column height and offset must be recalculated. { int transposedDiagOffset = skyDiagOffsets[row]; int transposedColHeight = skyDiagOffsets[row + 1] - transposedDiagOffset - 1; // excluding diagonal int entryHeight = row - col; // excluding diagonal if (entryHeight <= transposedColHeight) // inside stored non zero pattern { double val = skyValues[transposedDiagOffset + entryHeight]; if (val != 0.0) { submatrix[subRow, subCol] = val; // Skyline format stores many unnecessary zeros. } } } } } return(submatrix.BuildSkylineMatrix()); }
private static void TestSparsePosDef() { var matrix = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); var writer = new FullMatrixWriter { NumericFormat = new FixedPointFormat { MaxIntegerDigits = 2 } }; TestWriteOperation(matrix, SparsePosDef10by10.FullFormatPath, writer); }
private static void TestGetColumn() { var matrix = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); for (int j = 0; j < SparsePosDef10by10.Order; ++j) { Vector colExpected = DenseStrategies.GetColumn(matrix, j); Vector colComputed = matrix.GetColumn(j); comparer.AssertEqual(colExpected, colComputed); } }
private static void TestGetRow() { var matrix = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); for (int i = 0; i < SparsePosDef10by10.Order; ++i) { Vector rowExpected = DenseStrategies.GetRow(matrix, i); Vector rowComputed = matrix.GetRow(i); comparer.AssertEqual(rowExpected, rowComputed); } }
private static void TestFactorizationOf2DStructureStiffness() // 2D problem: 3 rigid body modes, 0 are constrained { string resourcesPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName + "\\Resources"; string valuesPath = resourcesPath + "\\Quad4_20x20_stiffness_values.txt"; string diagOffsetsPath = resourcesPath + "\\Quad4_20x20_stiffness_diagonal_offsets.txt"; var reader = new RawArraysReader(); SkylineMatrix unconstrainedK = reader.ReadSkylineMatrixFromSeparateFiles(valuesPath, diagOffsetsPath, true); double tolerance = 1E-2; CheckFactorization(unconstrainedK, 3, tolerance); CheckFactorization(unconstrainedK.Scale(1E-6), 3, 1E-8); //TODO: Normalization is required when comparing the pivot with the tolerance. }
private static void TestFactorizationOfBeam3DStiffness() { double tolerance = double.Epsilon; // Unconstrained beam. 3D problem: 6 rigid body modes, 0 are constrained CheckFactorization(SkylineMatrix.CreateFromArray(Beam3DElementMatrix.UnconstrainedStiffness), 6, tolerance); // Pinned beam. 3D problem: 6 rigid body modes, 3 are constrained CheckFactorization(SkylineMatrix.CreateFromArray(Beam3DElementMatrix.PinnedStiffness), 3, tolerance); // Clamped beam. 3D problem: 6 rigid body modes, 6 are constrained CheckFactorization(SkylineMatrix.CreateFromArray(Beam3DElementMatrix.ClampedStiffness), 0, tolerance); }
private static void TestFactorizationOfQuad4Stiffness() { double tolerance = 1E-7; // Unconstrained beam. 2D problem: 3 rigid body modes, 0 are constrained CheckFactorization(SkylineMatrix.CreateFromArray(Quad4ElementMatrix.UnconstrainedStiffness), 3, tolerance); // Pinned beam. 2D problem: 3 rigid body modes, 2 are constrained CheckFactorization(SkylineMatrix.CreateFromArray(Quad4ElementMatrix.PinnedStiffness), 1, tolerance); // Clamped beam. 2D problem: 3 rigid body modes, 3 are constrained CheckFactorization(SkylineMatrix.CreateFromArray(Quad4ElementMatrix.ClampedStiffness), 0, tolerance); }
/// <summary> /// Will do nothing if it was already called. To perform this for a different stiffness matrix, first call /// <see cref="Clear"/>. /// </summary> public void Clear() { inverseKii = null; inverseKiiDiagonal = null; inverseKrr = null; Kbb = null; Kib = null; Kcc = null; Krc = null; Krr = null; KccStar = null; //linearSystem.Matrix = null; // DO NOT DO THAT!!! The analyzer manages that. }
private static void TestFactorization() { //TestSettings.RunMultiproviderTest(providers, delegate () { // var skyline = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); var expectedU = Matrix.CreateFromArray(SparsePosDef10by10.CholeskyU); CholeskySkyline factorization = skyline.FactorCholesky(false); TriangularUpper computedU = factorization.GetFactorU(); comparer.AssertEqual(expectedU, computedU); //}); }
/// <summary> /// Will do nothing if it was already called. To perform this for a different stiffness matrix, first call /// <see cref="Clear"/>. /// </summary> public void ExtractKrr(int[] remainderDofs) { if (Krr != null) { if (inverseKrr != null) { throw new InvalidOperationException("The remainder-remainder stiffness submatrix of this subdomain has" + " already been calculated and then overwritten and cannot be used anymore. Restructure your code so" + " that all operations that need Krr are finished before inverting it."); } return; } Krr = linearSystem.Matrix.GetSubmatrixSymmetricSkyline(remainderDofs); }
private static void TestSystemSolution() { int order = SparsePosDef10by10.Order; var skyline = SkylineMatrix.CreateFromArrays(order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); var dok = DokSymmetric.CreateFromSparseMatrix(skyline); var b = Vector.CreateFromArray(SparsePosDef10by10.Rhs); var xExpected = Vector.CreateFromArray(SparsePosDef10by10.Lhs); (double[] cscValues, int[] cscRowIndices, int[] cscColOffsets) = dok.BuildSymmetricCscArrays(true); var factor = CholeskyCSparseNet.Factorize(order, cscValues.Length, cscValues, cscRowIndices, cscColOffsets); Vector xComputed = factor.SolveLinearSystem(b); comparer.AssertEqual(xExpected, xComputed); }
public static void WriteToFile(SkylineMatrix Mat, int i1, int j1, string path) { var writer = new StreamWriter(path); for (int i = 0; i < 40; ++i) { for (int j = 0; j < 40; ++j) { writer.Write(Mat[i + i1, j + j1]); writer.Write(' '); } writer.WriteLine(); } writer.Flush(); }
private static void TestMatrixVectorMultiplication(/*LinearAlgebraProviderChoice providers*/) { //TestSettings.RunMultiproviderTest(providers, delegate () { // var A = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); var x = Vector.CreateFromArray(SparsePosDef10by10.Lhs); var bExpected = Vector.CreateFromArray(SparsePosDef10by10.Rhs); IVector bComputed = A.Multiply(x, false); comparer.AssertEqual(bExpected, bComputed); //}); }
private static void TestSystemSolution(/*LinearAlgebraProviderChoice providers*/) { //TestSettings.RunMultiproviderTest(providers, delegate () { // var skyline = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); var b = Vector.CreateFromArray(SparsePosDef10by10.Rhs); var xExpected = Vector.CreateFromArray(SparsePosDef10by10.Lhs); CholeskySkyline factor = skyline.FactorCholesky(false); Vector xComputed = factor.SolveLinearSystem(b); comparer.AssertEqual(xExpected, xComputed); //}); }
public void Fill() { FillFunc fillFunc = (row, col) => { return((row + 1) + (col + 1)); }; skylineMatrix.Fill(fillFunc); di = new double[] { 2, 4, 6 }; al = new double[] { 3, 4, 5 }; au = new double[] { 3, 4, 5 }; ia = new int[] { 1, 1, 2, 4 }; SkylineMatrix skyline = new SkylineMatrix(di, ia, al, au); Assert.True(new HashSet <(double, int, int)>(skylineMatrix).SetEquals(skyline)); }
private static void TestFactorizationOfHexa8Stiffness() { double toleranceMSolve = 1E-2; double toleranceAbaqus = 1E-4; // Unconstrained beam. 3D problem: 6 rigid body modes, 0 are constrained CheckFactorization(SkylineMatrix.CreateFromArray(Hexa8ElementMatrix.UnconstrainedStiffness), 6, toleranceMSolve); // Pinned beam. 3D problem: 6 rigid body modes, 3 are constrained CheckFactorization(SkylineMatrix.CreateFromArray(Hexa8ElementMatrix.PinnedStiffness), 3, toleranceMSolve); // Clamped beam. 3D problem: 6 rigid body modes, 6 are constrained CheckFactorization(SkylineMatrix.CreateFromArray(Hexa8ElementMatrix.ClampedStiffness), 0, toleranceMSolve); // Unconstrained beam. 3D problem: 6 rigid body modes, 0 are constrained CheckFactorization(SkylineMatrix.CreateFromArray(Hexa8ElementMatrixAbaqus.UnconstrainedStiffness), 6, toleranceAbaqus); }
/// <summary> /// See <see cref="IIndexable2D.this[int, int]"/>. /// </summary> public double this[int rowIdx, int colIdx] { get { SkylineMatrix.ProcessIndices(ref rowIdx, ref colIdx); int entryHeight = colIdx - rowIdx; // excluding diagonal int maxColumnHeight = diagOffsets[colIdx + 1] - diagOffsets[colIdx] - 1; // excluding diagonal if (entryHeight > maxColumnHeight) { return(0.0); // outside stored non zero pattern } else { return(values[diagOffsets[colIdx] + entryHeight]); } } }
private static void TestConversionFullToSkyline() { var matrices = new List <double[, ]> { DiagonalIndefinite.BuildeIndefiniteMatrix(20), GlobalMatrixAssembly.GlobalMatrix, SparsePosDef10by10.Matrix, SparseSymm5by5.Matrix, SymmPosDef10by10.Matrix, SymmSingular10by10.Matrix }; foreach (double[,] matrix in matrices) { var full = Matrix.CreateFromArray(matrix); var skylineFromArray = SkylineMatrix.CreateFromArray(matrix); var skylineFromFull = SkylineMatrix.CreateFromMatrix(full); comparer.AssertEqual(full, skylineFromArray); comparer.AssertEqual(full, skylineFromFull); } }