Example #1
0
        public SkylineMatrix BuildGlobalMatrix(ISubdomainFreeDofOrdering dofOrdering, IEnumerable <IElement> elements,
                                               IElementMatrixProvider matrixProvider)
        {
            if (!areIndexersCached)
            {
                skylineBuilder = SkylineBuilder.Create(dofOrdering.NumFreeDofs,
                                                       FindSkylineColumnHeights(elements, dofOrdering.NumFreeDofs, dofOrdering.FreeDofs));
                areIndexersCached = true;
            }
            else
            {
                skylineBuilder.ClearValues();
            }

            foreach (IElement element in elements)
            {
                //if (element.ID != 2) continue;
                // TODO: perhaps that could be done and cached during the dof enumeration to avoid iterating over the dofs twice
                (int[] elementDofIndices, int[] subdomainDofIndices) = dofOrdering.MapFreeDofsElementToSubdomain(element);
                IMatrix elementMatrix = matrixProvider.Matrix(element);
                skylineBuilder.AddSubmatrixSymmetric(elementMatrix, elementDofIndices, subdomainDofIndices);
            }

            //// Print matrix
            //var writer = new LinearAlgebra.Output.FullMatrixWriter();
            ////writer.NumericFormat = new FixedPointFormat() { NumDecimalDigits = 2 };
            //writer.ArrayFormat = new LinearAlgebra.Output.Formatting.Array2DFormat("", "", "", "\n", ",");
            //writer.WriteToFile(skylineBuilder.BuildSkylineMatrix()/*.DoToAllEntries(x => Math.Round(x * 1E-6, 3))*/, @"C:\Users\Serafeim\Desktop\xfem.txt");

            return(skylineBuilder.BuildSkylineMatrix());
        }
Example #2
0
        public void AddVectorSubdomainToGlobal(ISubdomain subdomain, IVectorView subdomainVector, IVector globalVector)
        {
            ISubdomainFreeDofOrdering subdomainOrdering = SubdomainDofOrderings[subdomain];

            int[] subdomainToGlobalDofs = subdomainToGlobalDofMaps[subdomain];
            globalVector.AddIntoThisNonContiguouslyFrom(subdomainToGlobalDofs, subdomainVector);
        }
Example #3
0
 public void ReorderDofs(ISubdomain subdomain, ISubdomainFreeDofOrdering originalOrdering)
 {
     foreach (var reordering in reorderingStrategies)
     {
         reordering.ReorderDofs(subdomain, originalOrdering);
     }
 }
        public CsrMatrix BuildGlobalMatrix(ISubdomainFreeDofOrdering dofOrdering, IEnumerable <IElement> elements,
                                           IElementMatrixProvider matrixProvider)
        {
            int numFreeDofs     = dofOrdering.NumFreeDofs;
            var subdomainMatrix = DokRowMajor.CreateEmpty(numFreeDofs, numFreeDofs);

            foreach (IElement element in elements)
            {
                (int[] elementDofIndices, int[] subdomainDofIndices) = dofOrdering.MapFreeDofsElementToSubdomain(element);
                IMatrix elementMatrix = matrixProvider.Matrix(element);
                subdomainMatrix.AddSubmatrixSymmetric(elementMatrix, elementDofIndices, subdomainDofIndices);
            }

            (double[] values, int[] colIndices, int[] rowOffsets) = subdomainMatrix.BuildCsrArrays(sortColsOfEachRow);
            if (!isIndexerCached)
            {
                cachedColIndices = colIndices;
                cachedRowOffsets = rowOffsets;
                isIndexerCached  = true;
            }
            else
            {
                Debug.Assert(Utilities.AreEqual(cachedColIndices, colIndices));
                Debug.Assert(Utilities.AreEqual(cachedRowOffsets, rowOffsets));
            }
            return(CsrMatrix.CreateFromArrays(numFreeDofs, numFreeDofs, values, cachedColIndices, cachedRowOffsets, false));
        }
Example #5
0
 public GlobalFreeDofAsymmetricOrderingSingle(ISubdomain subdomain,
                                              ISubdomainFreeDofOrdering subdomainRowOrdering, ISubdomainFreeDofOrdering subdomainColOrdering)
 {
     _subdomain             = subdomain;
     this.NumGlobalFreeDofs = subdomainColOrdering.NumFreeDofs;
     this.GlobalFreeDofs    = subdomainColOrdering.FreeDofs;
 }
Example #6
0
        public void ExtractVectorSubdomainFromGlobal(ISubdomain subdomain, IVectorView globalVector, IVector subdomainVector)
        {
            ISubdomainFreeDofOrdering subdomainOrdering = SubdomainDofOrderings[subdomain];

            int[] subdomainToGlobalDofs = subdomainToGlobalDofMaps[subdomain];
            subdomainVector.CopyNonContiguouslyFrom(globalVector, subdomainToGlobalDofs);
        }
Example #7
0
                IMatrixView matrixConstrConstr) BuildGlobalSubmatrices(
            ISubdomainFreeDofOrdering freeDofOrdering, ISubdomainConstrainedDofOrdering constrainedDofOrdering,
            IEnumerable <IElement> elements, IElementMatrixProvider matrixProvider)
        {
            int numFreeDofs     = freeDofOrdering.NumFreeDofs;
            var subdomainMatrix = Matrix.CreateZero(numFreeDofs, numFreeDofs);

            //TODO: also reuse the indexers of the constrained matrices.
            constrainedAssembler.InitializeNewMatrices(freeDofOrdering.NumFreeDofs, constrainedDofOrdering.NumConstrainedDofs);

            // Process the stiffness of each element
            foreach (IElement element in elements)
            {
                (int[] elementDofsFree, int[] subdomainDofsFree) = freeDofOrdering.MapFreeDofsElementToSubdomain(element);
                //IReadOnlyDictionary<int, int> elementToGlobalDofs = dofOrdering.MapFreeDofsElementToSubdomain(element);
                (int[] elementDofsConstrained, int[] subdomainDofsConstrained) =
                    constrainedDofOrdering.MapConstrainedDofsElementToSubdomain(element);

                IMatrix elementMatrix = matrixProvider.Matrix(element);
                AddElementToGlobalMatrix(subdomainMatrix, elementMatrix, elementDofsFree, subdomainDofsFree);
                constrainedAssembler.AddElementMatrix(elementMatrix, elementDofsFree, subdomainDofsFree,
                                                      elementDofsConstrained, subdomainDofsConstrained);
            }

            (CsrMatrix matrixConstrFree, CsrMatrix matrixConstrConstr) = constrainedAssembler.BuildMatrices();
            return(subdomainMatrix, matrixConstrFree.TransposeToCSC(false), matrixConstrFree, matrixConstrConstr);
        }
Example #8
0
        public SymmetricCscMatrix BuildGlobalMatrix(ISubdomainFreeDofOrdering dofOrdering, IEnumerable <IElement> elements,
                                                    IElementMatrixProvider matrixProvider)
        {
            int numFreeDofs     = dofOrdering.NumFreeDofs;
            var subdomainMatrix = DokSymmetric.CreateEmpty(numFreeDofs);

            foreach (IElement element in elements)
            {
                // TODO: perhaps that could be done and cached during the dof enumeration to avoid iterating over the dofs twice
                (int[] elementDofIndices, int[] subdomainDofIndices) = dofOrdering.MapFreeDofsElementToSubdomain(element);
                IMatrix elementMatrix = matrixProvider.Matrix(element);
                subdomainMatrix.AddSubmatrixSymmetric(elementMatrix, elementDofIndices, subdomainDofIndices);
            }

            (double[] values, int[] rowIndices, int[] colOffsets) = subdomainMatrix.BuildSymmetricCscArrays(sortColsOfEachRow);
            if (!isIndexerCached)
            {
                cachedRowIndices = rowIndices;
                cachedColOffsets = colOffsets;
                isIndexerCached  = true;
            }
            else
            {
                Debug.Assert(Utilities.AreEqual(cachedRowIndices, rowIndices));
                Debug.Assert(Utilities.AreEqual(cachedColOffsets, colOffsets));
            }


            return(SymmetricCscMatrix.CreateFromArrays(numFreeDofs, values, cachedRowIndices, cachedColOffsets, false));
        }
Example #9
0
                matrixConstrConstr) BuildGlobalSubmatrices(ISubdomainFreeDofOrdering freeDofRowOrdering,
                                                           ISubdomainFreeDofOrdering freeDofColOrdering,
                                                           ISubdomainConstrainedDofOrdering constrainedDofRowOrdering,
                                                           ISubdomainConstrainedDofOrdering constrainedDofColOrdering, IEnumerable <IElement> elements,
                                                           IElementMatrixProvider matrixProvider)
        {
            int numFreeRowDofs = freeDofRowOrdering.NumFreeDofs;
            int numFreeColDofs = freeDofColOrdering.NumFreeDofs;

            var subdomainMatrix = DokRowMajor.CreateEmpty(numFreeRowDofs, numFreeColDofs);

            constrainedAssembler.InitializeNewMatrices(freeDofRowOrdering.NumFreeDofs,
                                                       constrainedDofRowOrdering.NumConstrainedDofs);

            foreach (var element in elements)
            {
                (int[] elementRowDofsFree, int[] subdomainRowDofsFree) =
                    freeDofRowOrdering.MapFreeDofsElementToSubdomain(element);
                (int[] elementColDofsFree, int[] subdomainColDofsFree) =
                    freeDofColOrdering.MapFreeDofsElementToSubdomain(element);

                (int[] elementRowDofsConstrained, int[] subdomainRowDofsConstrained) =
                    constrainedDofRowOrdering.MapConstrainedDofsElementToSubdomain(element);
                (int[] elementColDofsConstrained, int[] subdomainColDofsConstrained) =
                    constrainedDofColOrdering.MapConstrainedDofsElementToSubdomain(element);

                IMatrix elementMatrix = matrixProvider.Matrix(element);
                subdomainMatrix.AddSubmatrix(elementMatrix, elementRowDofsFree, subdomainRowDofsFree,
                                             elementColDofsFree, subdomainColDofsFree);
                constrainedAssembler.AddElementMatrix(elementMatrix, elementRowDofsFree, subdomainRowDofsFree,
                                                      elementRowDofsConstrained, subdomainRowDofsConstrained); //TODO: check validity
            }

            (double[] values, int[] colIndices, int[] rowOffsets) = subdomainMatrix.BuildCsrArrays(sortColsOfEachRow);
            if (!isIndexerCached)
            {
                cachedColIndices = colIndices;
                cachedRowOffsets = rowOffsets;
                isIndexerCached  = true;
            }
            else
            {
                Debug.Assert(Utilities.AreEqual(cachedColIndices, colIndices));
                Debug.Assert(Utilities.AreEqual(cachedRowOffsets, rowOffsets));
            }

            subdomainMatrix = null;
            var matrixFreeFree =
                CsrMatrix.CreateFromArrays(numFreeRowDofs, numFreeColDofs, values, cachedColIndices, cachedRowOffsets,
                                           false);

            (CsrMatrix matrixConstrFree, CsrMatrix matrixConstrConstr) =
                constrainedAssembler.BuildMatrices(); // TODO: see if this work
            return(matrixFreeFree, matrixConstrFree, matrixConstrFree.TransposeToCSC(false), matrixConstrConstr);
        }
Example #10
0
        public void ReorderDofs(ISubdomain subdomain, ISubdomainFreeDofOrdering originalOrdering)
        {
            originalOrdering.Reorder(amd, subdomain);

            //TODO: Delete the next. That code has been copied to the ISubdomainFreeDofOrdering implementations. This class
            //      works for any reordering algorithm, not only AMD.
            //var pattern = SparsityPatternSymmetric.CreateEmpty(originalOrdering.NumFreeDofs);
            //foreach (var element in subdomain.Elements)
            //{
            //    (int[] elementDofIndices, int[] subdomainDofIndices) = originalOrdering.MapFreeDofsElementToSubdomain(element);

            //    //TODO: ISubdomainFreeDofOrdering could perhaps return whether the subdomainDofIndices are sorted or not.
            //    pattern.ConnectIndices(subdomainDofIndices, false);
            //}
            //(int[] permutation, bool oldToNew) = amd.FindPermutation(pattern);
        }
Example #11
0
                IMatrixView matrixConstrConstr) BuildGlobalSubmatrices(
            ISubdomainFreeDofOrdering freeDofOrdering, ISubdomainConstrainedDofOrdering constrainedDofOrdering,
            IEnumerable <IElement> elements, IElementMatrixProvider matrixProvider)
        {
            int numFreeDofs     = freeDofOrdering.NumFreeDofs;
            var subdomainMatrix = DokSymmetric.CreateEmpty(numFreeDofs);

            //TODO: also reuse the indexers of the constrained matrices.
            constrainedAssembler.InitializeNewMatrices(freeDofOrdering.NumFreeDofs, constrainedDofOrdering.NumConstrainedDofs);

            // Process the stiffness of each element
            foreach (IElement element in elements)
            {
                // TODO: perhaps that could be done and cached during the dof enumeration to avoid iterating over the dofs twice
                (int[] elementDofsFree, int[] subdomainDofsFree) = freeDofOrdering.MapFreeDofsElementToSubdomain(element);
                (int[] elementDofsConstrained, int[] subdomainDofsConstrained) =
                    constrainedDofOrdering.MapConstrainedDofsElementToSubdomain(element);

                IMatrix elementMatrix = matrixProvider.Matrix(element);
                subdomainMatrix.AddSubmatrixSymmetric(elementMatrix, elementDofsFree, subdomainDofsFree);
                constrainedAssembler.AddElementMatrix(elementMatrix, elementDofsFree, subdomainDofsFree,
                                                      elementDofsConstrained, subdomainDofsConstrained);
            }

            // Create and cache the CSC arrays for the free dofs.
            (double[] values, int[] rowIndices, int[] colOffsets) = subdomainMatrix.BuildSymmetricCscArrays(sortColsOfEachRow);
            if (!isIndexerCached)
            {
                cachedRowIndices = rowIndices;
                cachedColOffsets = colOffsets;
                isIndexerCached  = true;
            }
            else
            {
                Debug.Assert(Utilities.AreEqual(cachedRowIndices, rowIndices));
                Debug.Assert(Utilities.AreEqual(cachedColOffsets, colOffsets));
            }

            // Create the free and constrained matrices.
            subdomainMatrix = null; // Let the DOK be garbaged collected early, in case there isn't sufficient memory.
            var matrixFreeFree =
                SymmetricCscMatrix.CreateFromArrays(numFreeDofs, values, cachedRowIndices, cachedColOffsets, false);

            (CsrMatrix matrixConstrFree, CsrMatrix matrixConstrConstr) = constrainedAssembler.BuildMatrices();
            return(matrixFreeFree, matrixConstrFree.TransposeToCSC(false), matrixConstrFree, matrixConstrConstr);
        }
Example #12
0
        public Matrix BuildGlobalMatrix(ISubdomainFreeDofOrdering dofOrdering, IEnumerable <IElement> elements,
                                        IElementMatrixProvider elementMatrixProvider)
        {
            int numFreeDofs     = dofOrdering.NumFreeDofs;
            var subdomainMatrix = Matrix.CreateZero(numFreeDofs, numFreeDofs);

            // Process the stiffness of each element
            foreach (IElement element in elements)
            {
                // TODO: perhaps that could be done and cached during the dof enumeration to avoid iterating over the dofs twice
                (int[] elementDofIndices, int[] subdomainDofIndices) = dofOrdering.MapFreeDofsElementToSubdomain(element);
                //IReadOnlyDictionary<int, int> elementToGlobalDofs = dofOrdering.MapFreeDofsElementToSubdomain(element);
                IMatrix elementMatrix = elementMatrixProvider.Matrix(element);
                AddElementToGlobalMatrix(subdomainMatrix, elementMatrix, elementDofIndices, subdomainDofIndices);
            }

            return(subdomainMatrix);
        }
Example #13
0
        public GlobalFreeDofOrderingSingle(ISubdomain subdomain, ISubdomainFreeDofOrdering subdomainDofOrdering)
        {
            this.subdomain = subdomain;

            this.NumGlobalFreeDofs     = subdomainDofOrdering.NumFreeDofs;
            this.GlobalFreeDofs        = subdomainDofOrdering.FreeDofs;
            this.SubdomainDofOrderings =
                new Dictionary <ISubdomain, ISubdomainFreeDofOrdering> {
                { subdomain, subdomainDofOrdering }
            };

            // A for-loop is faster than LINQ
            this.subdomainToGlobalDofMap = new int[NumGlobalFreeDofs];
            for (int i = 0; i < NumGlobalFreeDofs; ++i)
            {
                this.subdomainToGlobalDofMap[i] = i;
            }
        }
Example #14
0
                IMatrixView matrixConstrConstr) BuildGlobalSubmatrices(
            ISubdomainFreeDofOrdering freeDofOrdering, ISubdomainConstrainedDofOrdering constrainedDofOrdering,
            IEnumerable <IElement> elements, IElementMatrixProvider matrixProvider)
        {
            if (!areIndexersCached)
            {
                skylineBuilder = SkylineBuilder.Create(freeDofOrdering.NumFreeDofs,
                                                       FindSkylineColumnHeights(elements, freeDofOrdering.NumFreeDofs, freeDofOrdering.FreeDofs));
                areIndexersCached = true;
            }
            else
            {
                skylineBuilder.ClearValues();
            }

            //TODO: also reuse the indexers of the constrained matrices.
            constrainedAssembler.InitializeNewMatrices(freeDofOrdering.NumFreeDofs, constrainedDofOrdering.NumConstrainedDofs);

            // Process the stiffness of each element
            foreach (IElement element in elements)
            {
                // TODO: perhaps that could be done and cached during the dof enumeration to avoid iterating over the dofs twice
                (int[] elementDofsFree, int[] subdomainDofsFree) = freeDofOrdering.MapFreeDofsElementToSubdomain(element);
                (int[] elementDofsConstrained, int[] subdomainDofsConstrained) =
                    constrainedDofOrdering.MapConstrainedDofsElementToSubdomain(element);

                IMatrix elementMatrix = matrixProvider.Matrix(element);
                skylineBuilder.AddSubmatrixSymmetric(elementMatrix, elementDofsFree, subdomainDofsFree);
                constrainedAssembler.AddElementMatrix(elementMatrix, elementDofsFree, subdomainDofsFree,
                                                      elementDofsConstrained, subdomainDofsConstrained);
            }

            // Create the free and constrained matrices.
            SkylineMatrix matrixFreeFree = skylineBuilder.BuildSkylineMatrix();

            (CsrMatrix matrixConstrFree, CsrMatrix matrixConstrConstr) = constrainedAssembler.BuildMatrices();
            return(matrixFreeFree, matrixConstrFree.TransposeToCSC(false), matrixConstrFree, matrixConstrConstr);
        }
Example #15
0
 public (IMatrix Kff, IMatrixView Kfc, IMatrixView Kcf, IMatrixView Kcc) BuildGlobalSubmatrices(
     ISubdomainFreeDofOrdering freeDofOrdering, ISubdomainConstrainedDofOrdering constrainedDofOrdering,
     IEnumerable <IElement> elements, IElementMatrixProvider matrixProvider)
 => assembler.BuildGlobalSubmatrices(freeDofOrdering, constrainedDofOrdering, elements, matrixProvider);
Example #16
0
 public IMatrix BuildGlobalMatrix(ISubdomainFreeDofOrdering dofOrdering, IEnumerable <IElement> elements,
                                  IElementMatrixProvider matrixProvider)
 => assembler.BuildGlobalMatrix(dofOrdering, elements, matrixProvider);
Example #17
0
 public void ReorderDofs(ISubdomain subdomain, ISubdomainFreeDofOrdering originalOrdering)
 {
     // Do nothing
 }
 public void ReorderDofs(ISubdomain subdomain, ISubdomainFreeDofOrdering originalOrdering)
 => originalOrdering.ReorderNodeMajor(subdomain.Nodes);