protected override double RunSolverOneStep(int TimestepNo, double phystime, double dt)
        {
            //phystime = 1.8;
            LsUpdate(phystime);

            // operator-matrix assemblieren
            OperatorMatrix    = new BlockMsrMatrix(ProblemMapping);
            AltOperatorMatrix = new MsrMatrix(ProblemMapping);
            double[] Affine = new double[OperatorMatrix.RowPartitioning.LocalLength];
            MultiphaseCellAgglomerator Agg;

            // Agglomerator setup
            //Agg = new MultiphaseCellAgglomerator(new CutCellMetrics(MomentFittingVariant, m_quadOrder, LsTrk, LsTrk.GetSpeciesId("B")), this.THRESHOLD, false);
            Agg = LsTrk.GetAgglomerator(new SpeciesId[] { LsTrk.GetSpeciesId("B") }, m_quadOrder, __AgglomerationTreshold: this.THRESHOLD);
            Console.WriteLine("Inter-Process agglomeration? " + Agg.GetAgglomerator(LsTrk.GetSpeciesId("B")).AggInfo.InterProcessAgglomeration);

            // operator matrix assembly
            Op.ComputeMatrixEx(LsTrk,
                               ProblemMapping, null, ProblemMapping,
                               OperatorMatrix, Affine, false, 0.0, true,
                               Agg.CellLengthScales, null, null,
                               LsTrk.SpeciesIdS.ToArray());
            Agg.ManipulateMatrixAndRHS(OperatorMatrix, Affine, this.ProblemMapping, this.ProblemMapping);

            Op.ComputeMatrixEx(LsTrk,
                               ProblemMapping, null, ProblemMapping,
                               AltOperatorMatrix, Affine, false, 0.0, true,
                               Agg.CellLengthScales, null, null,
                               LsTrk.SpeciesIdS.ToArray());
            Agg.ManipulateMatrixAndRHS(AltOperatorMatrix, Affine, this.ProblemMapping, this.ProblemMapping);


            int nnz = this.OperatorMatrix.GetTotalNoOfNonZeros();

            Console.WriteLine("Number of non-zeros in matrix: " + nnz);

            int nnz2 = this.AltOperatorMatrix.GetTotalNoOfNonZeros();

            Assert.IsTrue(nnz == nnz2, "Number of non-zeros in matrix different for " + OperatorMatrix.GetType() + " and " + AltOperatorMatrix.GetType());
            Console.WriteLine("Number of non-zeros in matrix (reference): " + nnz2);

            MsrMatrix Comp = AltOperatorMatrix.CloneAs();

            Comp.Acc(-1.0, OperatorMatrix);
            double CompErr    = Comp.InfNorm();
            double Denom      = Math.Max(AltOperatorMatrix.InfNorm(), OperatorMatrix.InfNorm());
            double CompErrRel = Denom > Math.Sqrt(double.Epsilon) ? CompErr / Denom : CompErr;

            Console.WriteLine("Comparison: " + CompErrRel);

            Assert.LessOrEqual(CompErrRel, 1.0e-7, "Huge difference between MsrMatrix and BlockMsrMatrix.");

            base.TerminationKey = true;
            return(0.0);
        }
Example #2
0
        public static void RestrictionOfSystemOpTest()
        {
            Basis B1 = new Basis(grid, 0), B2 = new Basis(grid, 2);
            var   Map = new UnsetteledCoordinateMapping(B1, B2);

            var Lev0Basis = new AggregationGridBasis(B2, TestProgram.MgSeq[0]);
            var Lev1Basis = new AggregationGridBasis(B2, TestProgram.MgSeq[1]);


            var Lev0 = new MultigridMapping(Map, new AggregationGridBasis[] { Lev0Basis, Lev0Basis }, new int[] { B1.Degree, B2.Degree });
            var Lev1 = new MultigridMapping(Map, new AggregationGridBasis[] { Lev1Basis, Lev1Basis }, new int[] { B1.Degree, B2.Degree });


            int[] I0col = Lev0.GetSubvectorIndices(new int[] { 0 });
            int[] I1col = Lev0.GetSubvectorIndices(new int[] { 1 });
            int[] I0row = Lev1.GetSubvectorIndices(new int[] { 0 });
            int[] I1row = Lev1.GetSubvectorIndices(new int[] { 1 });

            var RestMtx = Lev1.FromOtherLevelMatrix(Lev0);

            MsrMatrix Rest00 = new MsrMatrix(I0row.Length, I0col.Length, 1, 1);

            RestMtx.WriteSubMatrixTo(Rest00, I0row, default(int[]), I0col, default(int[]));
            MsrMatrix Rest01 = new MsrMatrix(I0row.Length, I1col.Length, 1, 1);

            RestMtx.WriteSubMatrixTo(Rest01, I0row, default(int[]), I1col, default(int[]));
            MsrMatrix Rest10 = new MsrMatrix(I1row.Length, I0col.Length, 1, 1);

            RestMtx.WriteSubMatrixTo(Rest10, I1row, default(int[]), I0col, default(int[]));
            MsrMatrix Rest11 = new MsrMatrix(I1row.Length, I1col.Length, 1, 1);

            RestMtx.WriteSubMatrixTo(Rest11, I1row, default(int[]), I1col, default(int[]));

            Debug.Assert(Rest10.InfNorm() == 0.0);
            Debug.Assert(Rest01.InfNorm() == 0.0);
            Debug.Assert(Rest00.InfNorm() != 0.0);
            Debug.Assert(Rest11.InfNorm() != 0.0);
        }
Example #3
0
        public static void RestrictionOfSystemOpTest()
        {
            Basis B1 = new Basis(grid, 0), B2 = new Basis(grid, 2);
            var   Map = new UnsetteledCoordinateMapping(B1, B2);

            AggregationGridBasis[][] aB = AggregationGridBasis.CreateSequence(TestProgram.MgSeq.Take(2), new Basis[] { B1, B2 });

            var Lev0 = new MultigridMapping(Map, aB[0], new int[] { B1.Degree, B2.Degree });
            var Lev1 = new MultigridMapping(Map, aB[1], new int[] { B1.Degree, B2.Degree });


            int[] I0col = Lev0.GetSubvectorIndices(new int[] { 0 });
            int[] I1col = Lev0.GetSubvectorIndices(new int[] { 1 });
            int[] I0row = Lev1.GetSubvectorIndices(new int[] { 0 });
            int[] I1row = Lev1.GetSubvectorIndices(new int[] { 1 });

            var RestMtx = Lev1.FromOtherLevelMatrix(Lev0);

            MsrMatrix Rest00 = new MsrMatrix(I0row.Length, I0col.Length, 1, 1);

            RestMtx.WriteSubMatrixTo(Rest00, I0row, default(int[]), I0col, default(int[]));
            MsrMatrix Rest01 = new MsrMatrix(I0row.Length, I1col.Length, 1, 1);

            RestMtx.WriteSubMatrixTo(Rest01, I0row, default(int[]), I1col, default(int[]));
            MsrMatrix Rest10 = new MsrMatrix(I1row.Length, I0col.Length, 1, 1);

            RestMtx.WriteSubMatrixTo(Rest10, I1row, default(int[]), I0col, default(int[]));
            MsrMatrix Rest11 = new MsrMatrix(I1row.Length, I1col.Length, 1, 1);

            RestMtx.WriteSubMatrixTo(Rest11, I1row, default(int[]), I1col, default(int[]));

            Assert.IsTrue(Rest10.InfNorm() == 0.0);
            Assert.IsTrue(Rest01.InfNorm() == 0.0);
            Assert.IsTrue(Rest00.InfNorm() != 0.0);
            Assert.IsTrue(Rest11.InfNorm() != 0.0);
        }
        void ExtractMatrices()
        {
            // dispose old solver, if required
            // ===============================

            if (this.m_PressureSolver != null)
            {
                this.m_PressureSolver.Dispose();
                this.m_PressureSolver = null;
            }


            // sub-matrices for the Potential Solver
            // =====================================
            int VelocityLength = this.USubMatrixIdx_Row.Length;
            int PressureLength = this.PSubMatrixIdx_Row.Length;


            this.PressureGrad = new MsrMatrix(VelocityLength, PressureLength, 1, 1);
            this.VelocityDiv  = new MsrMatrix(PressureLength, VelocityLength, 1, 1);
            this.Stab         = new MsrMatrix(PressureLength, PressureLength, 1, 1);

            var WholeSystemMatrix = this.m_MgOp.OperatorMatrix;

            WholeSystemMatrix.WriteSubMatrixTo(PressureGrad, USubMatrixIdx_Row, default(int[]), PSubMatrixIdx_Row, default(int[]));
            WholeSystemMatrix.WriteSubMatrixTo(VelocityDiv, PSubMatrixIdx_Row, default(int[]), USubMatrixIdx_Row, default(int[]));
            WholeSystemMatrix.WriteSubMatrixTo(Stab, PSubMatrixIdx_Row, default(int[]), PSubMatrixIdx_Row, default(int[]));



            // inverse mass matrix
            // ===================

            if (this.m_SIMPLEOptions.PotentialSolver_UseMassMatrix)
            {
                // extract the mass-matrix block for the velocity part
                // ---------------------------------------------------
                MsrMatrix MM = new MsrMatrix(this.PressureGrad.RowPartitioning, this.VelocityDiv.ColPartition);
                this.m_MgOp.MassMatrix.WriteSubMatrixTo(MM, this.USubMatrixIdx_Row, default(int[]), this.USubMatrixIdx_Row, default(int[]));

                // invert mass matrix
                // ------------------

                int D = this.LsTrk.GridDat.SpatialDimension;
                this.invMM = new MsrMatrix(MM.RowPartitioning, MM.ColPartition);

                MultidimensionalArray Block    = new MultidimensionalArray(2);
                MultidimensionalArray InvBlock = new MultidimensionalArray(2);

                int   iRow0   = MM.RowPartitioning.i0;
                int   JAGG    = this.m_MgOp.Mapping.AggGrid.iLogicalCells.NoOfLocalUpdatedCells;
                int[] DegreeS = this.m_MgOp.Mapping.DgDegree;
                for (int jagg = 0; jagg < JAGG; jagg++) // loop over aggregate cells...
                {
                    for (int d = 0; d < D; d++)         // loop over velocity components...
                    {
                        int N = this.m_MgOp.Mapping.AggBasis[d].GetLength(jagg, DegreeS[d]);
                        if (Block.GetLength(0) != N)
                        {
                            Block.Allocate(N, N);
                            InvBlock.Allocate(N, N);
                        }

                        for (int n = 0; n < N; n++)
                        {
                            CheckMatrix(MM, iRow0, N, iRow0 + n);
                            for (int m = 0; m < N; m++)
                            {
                                Block[n, m] = MM[iRow0 + n, iRow0 + m];
                            }
                        }

                        Block.InvertTo(InvBlock);

                        for (int n = 0; n < N; n++)
                        {
                            for (int m = 0; m < N; m++)
                            {
                                this.invMM[iRow0 + n, iRow0 + m] = InvBlock[n, m];
                            }
                        }

                        iRow0 += N;
                    }
                }

                Debug.Assert(iRow0 == MM.RowPartitioning.iE);

#if DEBUG
                var    CheckMX = MM * invMM;
                double TRESH   = Math.Max(MM.InfNorm(), invMM.InfNorm()) * 1.0e-10;
                for (int iRow = CheckMX.RowPartitioning.i0; iRow < CheckMX.RowPartitioning.iE; iRow++)
                {
                    if (Math.Abs(CheckMX.GetDiagonalElement(iRow) - 1.0) > TRESH)
                    {
                        throw new ArithmeticException("AapproxInverse is not the Inverse of the Aapprox-Matrix");
                    }
                }
#endif
            }
        }
Example #5
0
        private void ApproximationMatrix()
        {
            this.Aapprox        = null;
            this.AapproxInverse = null;

            //MsrMatrix AapproxComp = null;

            /*
             * switch(m_SIMPLEOptions.Option_Approximation_Predictor) {
             *  case ApproxPredictor.MassMatrix:
             *  case ApproxPredictor.LocalizedOperator: { break; }
             *  default: {
             *      Aapprox = ConvDiff.CloneAs();
             *      //switch (m_SIMPLEOptions.Option_Timestepper) {
             *      //    case Timestepper.Steady: break;
             *      //    case Timestepper.ImplicitEuler: {
             *      //            Aapprox.Acc(1.0 / dt, MassMatrix);
             *      //            break;
             *      //        }
             *      //    default: {
             *      //            throw new NotImplementedException("Unknown Timestepper");
             *      //        }
             *      //}
             *      AapproxComp = new MsrMatrix(USubMatrixIdx.Length, USubMatrixIdx.Length, MassMatrix.RowPartitioning.BlockSize / (2 * D), MassMatrix.ColPartition.BlockSize / (2 * D));
             *      Aapprox.WriteSubMatrixTo(AapproxComp, USubMatrixIdx, default(int[]), USubMatrixIdx, default(int[]));
             *      break;
             *  }
             * }
             */
            switch (m_SIMPLEOptions.Option_Approximation_Predictor)
            {
            case ApproxPredictor.MassMatrix: {
                BlockMsrMatrix MM;
                if (!double.IsPositiveInfinity(this.m_SIMPLEOptions.dt))
                {
                    // instationary SIMPLE

                    //MM = this.m_MgOp.MassMatrix.CloneAs();


                    // hier muss ich mir nochmal was überlegen --
                    // für einige Präkond.-Optionen
                    // (genau jene, welche die XDG-Basen für beide Phasen in Cut-Zellen mischen),
                    // wie etwa
                    //   MultigridOperator.Mode.SymPart_DiagBlockEquilib
                    // ist eine Block-Skalierung mit rho_A und rho_B
                    // inkonsistent!
                    //

                    throw new NotImplementedException("todo");
                }
                else
                {
                    MM = this.m_MgOp.MassMatrix;
                }
                Aapprox = new MsrMatrix(this.ConvDiff.RowPartitioning);
                this.m_MgOp.MassMatrix.WriteSubMatrixTo(Aapprox, this.USubMatrixIdx_Row, default(int[]), this.USubMatrixIdx_Row, default(int[]));

                //AapproxInverse = MassMatrixInv._ToMsrMatrix();// Aapprox.Invert();
                //switch(m_SIMPLEOptions.Option_Timestepper) {
                //    case Timestepper.Steady: break;
                //    case Timestepper.ImplicitEuler: {
                //        Aapprox.Scale(1 + 1.0 / dt);
                //        AapproxInverse.Scale(1 / (1 + 1.0 / dt));
                //        /*#if DEBUG
                //                                            var CheckMX = AapproxInverse * Aapprox;
                //                                            foreach (int i in USubMatrixIdx) {
                //                                                if (Math.Abs(CheckMX.GetDiagonalElement(i) - 1.0) > 1e-10) throw new ArithmeticException("AapproxInverse is not the Inverse of the Aapprox-Matrix");
                //                                            }

                //        #endif*/
                //        break;
                //    }
                //    default: {
                //        throw new NotImplementedException("Unknown Timestepper");
                //    }
                //}
                break;
            }

            case ApproxPredictor.Exact: {
                if (this.LsTrk.GridDat.CellPartitioning.MpiSize > 1)
                {
                    throw new NotSupportedException("Not implemented for MPI-parallel runs.");
                }

                //RowIdx = VelocityMapping.GetSubvectorIndices(this.LsTrk, D.ForLoop(d => d), _SpcIds: this.LsTrk.SpeciesIdS, drk: this.TransportAgglomerator);
                //ColIdx = RowIdx;

                if (USubMatrixIdx_Row.Length > 4500)
                {
                    Console.WriteLine(string.Format("WARNING: you don't really want to invert a {0}x{0} matrix.", USubMatrixIdx_Row.Length));
                }

                this.Aapprox = this.ConvDiff;

                MultidimensionalArray AapproxFull        = Aapprox.ToFullMatrixOnProc0();
                MultidimensionalArray AapproxInverseFull = AapproxFull.GetInverse();
                this.AapproxInverse = new MsrMatrix(new Partitioning(USubMatrixIdx_Row.Length));
                this.AapproxInverse.AccDenseMatrix(1.0, AapproxInverseFull);
                break;
            }

            case ApproxPredictor.Diagonal: {
                /*
                 * Aapprox = new MsrMatrix(ConvDiff.RowPartitioning, ConvDiff.ColPartition);
                 * AapproxInverse = new MsrMatrix(ConvDiff.RowPartitioning, ConvDiff.ColPartition);
                 * foreach(int i in USubMatrixIdx) {
                 *  int[] j = new int[] { i };
                 *  double Value = ConvDiff.GetValues(i, j)[0];
                 *  //Value += MassMatrix.GetValues(i, j)[0];
                 *  Aapprox.SetDiagonalElement(i, Value);
                 *  if(Value == 0) {
                 *      AapproxInverse.SetDiagonalElement(i, 0);
                 *  } else {
                 *      AapproxInverse.SetDiagonalElement(i, 1 / Value);
                 *  }
                 * }
                 #if DEBUG
                 * Aapprox.VerifyDataStructure();
                 * AapproxInverse.VerifyDataStructure();
                 * var CheckMX = AapproxInverse * Aapprox;
                 * foreach(int i in USubMatrixIdx) {
                 *  if(Math.Abs(CheckMX.GetDiagonalElement(i) - 1.0) > 1e-13) throw new ArithmeticException("AapproxInverse is not the Inverse of the Operator Matrix");
                 * }
                 #endif
                 * break;
                 */
                throw new NotImplementedException("todo");
            }

            case ApproxPredictor.BlockDiagonal: {
                /*
                 * AapproxInverse = new MsrMatrix(Aapprox.RowPartitioning, Aapprox.ColPartition);
                 * var AapproxCompBD = new BlockDiagonalMatrix(AapproxComp);
                 * var AapproxCompInvBD = AapproxCompBD.Invert();
                 * AapproxCompBD._ToMsrMatrix().WriteSubMatrixTo(Aapprox, default(int[]), USubMatrixIdx, default(int[]), USubMatrixIdx);
                 * AapproxCompInvBD._ToMsrMatrix().WriteSubMatrixTo(AapproxInverse, default(int[]), USubMatrixIdx, default(int[]), USubMatrixIdx);
                 * //#if DEBUG
                 * Aapprox.VerifyDataStructure();
                 * AapproxInverse.VerifyDataStructure();
                 * var CheckMX = AapproxInverse * Aapprox;
                 * foreach(int i in USubMatrixIdx) {
                 *  if(Math.Abs(CheckMX.GetDiagonalElement(i) - 1.0) > 1e-12) throw new ArithmeticException("AapproxInverse is not the Inverse of the Operator Matrix");
                 * }
                 * //#endif
                 *
                 * break;
                 */
                throw new NotImplementedException("todo");
            }

            case ApproxPredictor.BlockSum: {
                /*
                 * Console.WriteLine("BlockSum is not properly tested yet and did not work in previous tests");
                 * int AccdBlockSize = ConvDiff.RowPartitioning.BlockSize / (2 * D);
                 * var AapproxBD = new BlockDiagonalMatrix(ConvDiff.RowPartitioning);
                 * var Aapprox = new MsrMatrix(ConvDiff.RowPartitioning);
                 * int[] indexer = new int[AccdBlockSize];
                 * for(int i = 0; i < indexer.Length; i++) {
                 *  indexer[i] = i;
                 * }
                 * int[] rowindexer = indexer.CloneAs();
                 *
                 *
                 * for(int i = 0; i < ConvDiff.NoOfRows / AccdBlockSize; i++) {
                 *  int[] colindexer = indexer.CloneAs();
                 *  for(int j = 0; j < ConvDiff.NoOfCols / AccdBlockSize; j++) {
                 *      ConvDiff.AccSubMatrixTo(1.0, Aapprox, rowindexer, rowindexer, colindexer, rowindexer);
                 *      for(int r = 0; r < indexer.Length; r++) {
                 *          colindexer[r] += AccdBlockSize;
                 *      }
                 *  }
                 *  for(int r = 0; r < indexer.Length; r++) {
                 *      rowindexer[r] += AccdBlockSize;
                 *  }
                 * }
                 * //if (m_SIMPLEOptions.Option_Timestepper == Timestepper.ImplicitEuler) {
                 * //    Aapprox.Acc(1 / dt, MassMatrix);
                 * //}
                 * AapproxComp = new MsrMatrix(USubMatrixIdx.Length, USubMatrixIdx.Length, AccdBlockSize, AccdBlockSize);
                 * Aapprox.WriteSubMatrixTo(AapproxComp, USubMatrixIdx, default(int[]), USubMatrixIdx, default(int[]));
                 * AapproxInverse = new MsrMatrix(Aapprox.RowPartitioning, Aapprox.ColPartition);
                 * var AapproxCompBD = new BlockDiagonalMatrix(AapproxComp);
                 * var AapproxCompInvBD = AapproxCompBD.Invert();
                 * AapproxCompInvBD._ToMsrMatrix().WriteSubMatrixTo(AapproxInverse, default(int[]), USubMatrixIdx, default(int[]), USubMatrixIdx);
                 #if DEBUG
                 * Aapprox.VerifyDataStructure();
                 * AapproxInverse.VerifyDataStructure();
                 *
                 * // Check copying back and forth
                 * var CheckAapproxComp = new MsrMatrix(Aapprox.RowPartitioning, Aapprox.ColPartition);
                 * AapproxComp.WriteSubMatrixTo(CheckAapproxComp, default(int[]), USubMatrixIdx, default(int[]), USubMatrixIdx);
                 * CheckAapproxComp.Acc(-1.0, Aapprox);
                 * if(CheckAapproxComp.InfNorm() > 1e-14) throw new ArithmeticException("Something went wrong while copying the Aapprox Matrix");
                 *
                 * //Check Transformation to BlockdiagonalMatrix
                 * var CheckAapproxCompBD = new MsrMatrix(Aapprox.RowPartitioning, Aapprox.ColPartition);
                 * AapproxCompBD._ToMsrMatrix().WriteSubMatrixTo(CheckAapproxCompBD, default(int[]), USubMatrixIdx, default(int[]), USubMatrixIdx);
                 * CheckAapproxCompBD.Acc(-1.0, Aapprox);
                 * if(CheckAapproxCompBD.InfNorm() > 1e-14) throw new ArithmeticException("Something went wrong while copying the Aapprox Matrix");
                 *
                 * //Check Matrix Inversion
                 * var CheckMX = AapproxInverse * Aapprox;
                 * foreach(int i in USubMatrixIdx) {
                 *  if(Math.Abs(CheckMX.GetDiagonalElement(i) - 1.0) > 1e-12) throw new ArithmeticException("AapproxInverse is not the Inverse of the Operator Matrix");
                 * }
                 #endif
                 * break;
                 */
                throw new NotImplementedException("todo");
            }

            case ApproxPredictor.Neumann: {
                /*
                 * Console.WriteLine("Neumann did not work in previous Tests, Series does typically not converge");
                 * int serieslength = 10;
                 *
                 * Aapprox = ConvDiff.CloneAs();
                 * //if (m_SIMPLEOptions.Option_Timestepper != Timestepper.Steady) {
                 * //    Aapprox.Acc(1.0 / dt, MassMatrix);
                 * //}
                 * var B = Aapprox.CloneAs();
                 * double ScalingFactor = Aapprox.InfNorm();
                 * B.Scale((-1.0 / ScalingFactor.Pow(0))); //Scaling Power up to 4 tried
                 * B.AccEyeSp(1.0);
                 * AapproxInverse = B;
                 * AapproxInverse.AccEyeSp(1.0);
                 * MsrMatrix OldMoment = B;
                 * for(int i = 0; i <= serieslength; i++) {
                 *  var NewMoment = OldMoment * B;
                 *  AapproxInverse.Acc(1.0, NewMoment);
                 *  //Debug
                 *  Console.WriteLine("MomentNumber #{0}, InfNormOf Inverse #{1}", i, NewMoment.InfNorm());
                 *  OldMoment = NewMoment;
                 * }
                 * AapproxInverse.Scale((1.0 / ScalingFactor.Pow(0))); //Scaling Power up to 4 tried
                 * break;
                 */
                throw new NotImplementedException("todo");
            }

            case ApproxPredictor.LocalizedOperator: {
                /*
                 * Console.WriteLine("Localized Operator did not work in previous Tests");
                 * double[] LocalizedOpAffine;
                 * MultiphaseCellAgglomerator LocalizedAgglomerator;
                 * Aapprox = new MsrMatrix(MassMatrix.RowPartitioning, MassMatrix.ColPartition);
                 * TransportOpLocalized.AssembleMatrix(
                 *  out Aapprox, out LocalizedOpAffine,
                 *  out LocalizedAgglomerator, out TransportMassFact,
                 *  this.Velocity.Current, null,
                 *  this.LevSet, null, Curv,
                 *  VelocityMapping, VelocityMapping);
                 * if(Option_Timestepper == Timestepper.ImplicitEuler) {
                 *  Aapprox.Acc(1 / dt, MassMatrix);
                 * }
                 *
                 * var DiagAverage = Aapprox.GetDiagVector().Average();
                 * foreach(int i in RowIdx) {
                 *  if(Aapprox.GetDiagonalElement(i) == 0.0) {
                 *      //Aapprox.SetDiagonalElement(i, MassMatrix.GetDiagonalElement(i));
                 *      Aapprox.SetDiagonalElement(i, DiagAverage);
                 *  }
                 * }
                 * AapproxComp = new MsrMatrix(RowIdx.Length, RowIdx.Length, MassMatrix.RowPartitioning.BlockSize / (2 * D), MassMatrix.ColPartition.BlockSize / (2 * D));
                 * Aapprox.WriteSubMatrixTo(AapproxComp, RowIdx, default(int[]), ColIdx, default(int[]));
                 * AapproxInverse = new MsrMatrix(Aapprox.RowPartitioning, Aapprox.ColPartition);
                 * var AapproxCompBD = new BlockDiagonalMatrix(AapproxComp);
                 * var AapproxCompInvBD = AapproxCompBD.Invert();
                 * AapproxCompInvBD._ToMsrMatrix().WriteSubMatrixTo(AapproxInverse, default(int[]), RowIdx, default(int[]), ColIdx);
                 * //AapproxComp._ToMsrMatrix().WriteSubMatrixTo(Aapprox, default(int[]), RowIdx, default(int[]), ColIdx);
                 *
                 *
                 #if DEBUG
                 * Aapprox.VerifyDataStructure();
                 * AapproxInverse.VerifyDataStructure();
                 * var CheckAapproxCompBD = new MsrMatrix(Aapprox.RowPartitioning, Aapprox.ColPartition);
                 * AapproxCompBD._ToMsrMatrix().WriteSubMatrixTo(CheckAapproxCompBD, default(int[]), RowIdx, default(int[]), ColIdx);
                 * CheckAapproxCompBD.Acc(-1.0, Aapprox);
                 * if(CheckAapproxCompBD.InfNorm() > 1e-14) throw new ArithmeticException("Something went wrong while copying the Aapprox Matrix");
                 * var CheckMX = AapproxInverse * Aapprox;
                 * foreach(int i in RowIdx) {
                 *  if(Math.Abs(CheckMX.GetDiagonalElement(i) - 1.0) > 1e-12) throw new ArithmeticException("AapproxInverse is not the Inverse of the Operator Matrix");
                 * }
                 #endif
                 * break;
                 */
                throw new NotImplementedException("todo");
            }

            default:
                throw new NotImplementedException("todo");
            }


            if (this.AapproxInverse == null)
            {
                // block-inversion is required.
                // ++++++++++++++++++++++++++++

                int D = this.LsTrk.GridDat.SpatialDimension;
                this.AapproxInverse = new MsrMatrix(this.Aapprox.RowPartitioning, this.Aapprox.ColPartition);

                //int N = this.m_MgOp.Mapping.AggBasis.GetMinimalLength(this.m_MgOp.Mapping.DgDegree[0]);
                //Debug.Assert(this.Aapprox.RowPartitioning.LocalLength % N == 0);
                MultidimensionalArray Block    = new MultidimensionalArray(2);
                MultidimensionalArray InvBlock = new MultidimensionalArray(2);


                int   iRow0   = this.Aapprox.RowPartitioning.i0;
                int   JAGG    = this.m_MgOp.Mapping.AggGrid.iLogicalCells.NoOfLocalUpdatedCells;
                int[] DegreeS = this.m_MgOp.Mapping.DgDegree;
                for (int jagg = 0; jagg < JAGG; jagg++) // loop over aggregate cells...
                {
                    for (int d = 0; d < D; d++)         // loop over velocity components...
                    {
                        int N = this.m_MgOp.Mapping.AggBasis[d].GetLength(jagg, DegreeS[d]);
                        if (Block.GetLength(0) != N)
                        {
                            Block.Allocate(N, N);
                            InvBlock.Allocate(N, N);
                        }

                        for (int n = 0; n < N; n++)
                        {
#if DEBUG
                            int iRow = iRow0 + n;
                            {
                                int[]    Cols = null;
                                double[] Vals = null;
                                int      LR   = Aapprox.GetRow(iRow, ref Cols, ref Vals);
                                int      cMin = int.MaxValue;
                                int      cMax = int.MinValue;
                                for (int lr = 0; lr < LR; lr++)
                                {
                                    if (Vals[lr] != 0.0)
                                    {
                                        cMin = Math.Min(cMin, Cols[lr]);
                                        cMax = Math.Max(cMax, Cols[lr]);
                                    }
                                }
                                Debug.Assert(cMin >= iRow0);
                                Debug.Assert(cMax < iRow0 + N);
                            }
#endif
                            for (int m = 0; m < N; m++)
                            {
                                Block[n, m] = this.Aapprox[iRow0 + n, iRow0 + m];
                            }
                        }

                        Block.InvertTo(InvBlock);

                        for (int n = 0; n < N; n++)
                        {
                            for (int m = 0; m < N; m++)
                            {
                                this.AapproxInverse[iRow0 + n, iRow0 + m] = InvBlock[n, m];
                            }
                        }

                        iRow0 += N;
                    }
                }

                Debug.Assert(iRow0 == this.Aapprox.RowPartitioning.iE);
            }

#if DEBUG
            var    CheckMX = AapproxInverse * Aapprox;
            double TRESH   = Math.Max(AapproxInverse.InfNorm(), Aapprox.InfNorm()) * 1.0e-10;
            for (int iRow = CheckMX.RowPartitioning.i0; iRow < CheckMX.RowPartitioning.iE; iRow++)
            {
                if (Math.Abs(CheckMX.GetDiagonalElement(iRow) - 1.0) > TRESH)
                {
                    throw new ArithmeticException("AapproxInverse is not the Inverse of the Aapprox-Matrix");
                }
            }
#endif
        }
Example #6
0
        protected override double RunSolverOneStep(int TimestepNo, double phystime, double dt)
        {
            LsUpdate(phystime);

            // operator-matrix assemblieren
            OperatorMatrix    = new BlockMsrMatrix(MG_Mapping.ProblemMapping);
            AltOperatorMatrix = new MsrMatrix(MG_Mapping.ProblemMapping);
            double[] Affine = new double[OperatorMatrix.RowPartitioning.LocalLength];
            MultiphaseCellAgglomerator Agg;

            Agg = LsTrk.GetAgglomerator(this.LsTrk.SpeciesIdS.ToArray(), m_quadOrder, __AgglomerationTreshold: this.THRESHOLD);

            XSpatialOperatorMk2.XEvaluatorLinear mtxBuilder = Op.GetMatrixBuilder(base.LsTrk, MG_Mapping.ProblemMapping, null, MG_Mapping.ProblemMapping);
            mtxBuilder.time = 0.0;
            mtxBuilder.ComputeMatrix(OperatorMatrix, Affine);
            Agg.ManipulateMatrixAndRHS(OperatorMatrix, Affine, MG_Mapping.ProblemMapping, MG_Mapping.ProblemMapping);

            foreach (var S in this.LsTrk.SpeciesNames)
            {
                Console.WriteLine("  Species {0}: no of agglomerated cells: {1}",
                                  S, Agg.GetAgglomerator(this.LsTrk.GetSpeciesId(S)).AggInfo.SourceCells.NoOfItemsLocally);
            }


            MGOp = new MultigridOperator(XAggB, map,
                                         OperatorMatrix,
                                         this.massFact.GetMassMatrix(map, false),
                                         OpConfig, null);
            Debug.Assert(MGOp.OperatorMatrix != null);
            Debug.Assert(MGOp.Mapping != null);

            someVec = GetRHS(Affine, OperatorMatrix);

            mtxBuilder.ComputeMatrix(AltOperatorMatrix, Affine);
            Agg.ManipulateMatrixAndRHS(AltOperatorMatrix, Affine, MG_Mapping.ProblemMapping, MG_Mapping.ProblemMapping);


            //LsTrk.GetSpeciesName(((XdgAggregationBasis)MGOp.Mapping.AggBasis[0]).UsedSpecies[1]);
            //LsTrk.GetSpeciesName(((XdgAggregationBasis)MGOp.Mapping.AggBasis[0]).UsedSpecies[0]);

            int nnz = this.OperatorMatrix.GetTotalNoOfNonZeros();

            Console.WriteLine("Number of non-zeros in matrix: " + nnz);

            int nnz2 = this.AltOperatorMatrix.GetTotalNoOfNonZeros();

            Assert.IsTrue(nnz == nnz2, "Number of non-zeros in matrix different for " + OperatorMatrix.GetType() + " and " + AltOperatorMatrix.GetType());
            Console.WriteLine("Number of non-zeros in matrix (reference): " + nnz2);

            MsrMatrix Comp = AltOperatorMatrix.CloneAs();

            Comp.Acc(-1.0, OperatorMatrix);
            double CompErr    = Comp.InfNorm();
            double Denom      = Math.Max(AltOperatorMatrix.InfNorm(), OperatorMatrix.InfNorm());
            double CompErrRel = Denom > Math.Sqrt(double.Epsilon) ? CompErr / Denom : CompErr;

            Console.WriteLine("Comparison: " + CompErrRel);

            Assert.LessOrEqual(CompErrRel, 1.0e-7, "Huge difference between MsrMatrix and BlockMsrMatrix.");

            base.TerminationKey = true;
            return(0.0);
        }
Example #7
0
        /// <summary>
        /// Includes assembly of the matrix.
        /// </summary>
        /// <param name="L"></param>
        protected override void CreateEquationsAndSolvers(GridUpdateDataVaultBase L)
        {
            using (FuncTrace tr = new FuncTrace()) {
                // create operator
                // ===============
                SpatialOperator LapaceIp;
                {
                    double D              = this.GridData.SpatialDimension;
                    double penalty_base   = (T.Basis.Degree + 1) * (T.Basis.Degree + D) / D;
                    double penalty_factor = base.Control.penalty_poisson;

                    BoundaryCondMap <BoundaryType> PoissonBcMap = new BoundaryCondMap <BoundaryType>(this.GridData, this.Control.BoundaryValues, "T");

                    LapaceIp = new SpatialOperator(1, 1, QuadOrderFunc.SumOfMaxDegrees(), "T", "T");
                    var flux = new ipFlux(penalty_base * base.Control.penalty_poisson, this.GridData.Cells.cj, PoissonBcMap);
                    LapaceIp.EquationComponents["T"].Add(flux);

                    LapaceIp.Commit();
                }

                // Create Matrices
                // ===============

                {
                    // time measurement for matrix assembly
                    Stopwatch stw = new Stopwatch();
                    stw.Start();

                    // console
                    Console.WriteLine("creating sparse system for {0} DOF's ...", T.Mapping.Ntotal);

                    // quadrature domain
                    var volQrSch = new CellQuadratureScheme(true, CellMask.GetFullMask(this.GridData));
                    var edgQrSch = new EdgeQuadratureScheme(true, EdgeMask.GetFullMask(this.GridData));

#if DEBUG
                    // in DEBUG mode, we compare 'MsrMatrix' (old, reference implementation) and 'BlockMsrMatrix' (new standard)
                    var RefLaplaceMtx = new MsrMatrix(T.Mapping);
#endif
                    using (new BlockTrace("SipMatrixAssembly", tr)) {
                        LaplaceMtx    = new BlockMsrMatrix(T.Mapping);
                        LaplaceAffine = new double[T.Mapping.LocalLength];

                        LapaceIp.ComputeMatrixEx(T.Mapping, null, T.Mapping,
                                                 LaplaceMtx, LaplaceAffine,
                                                 volQuadScheme: volQrSch, edgeQuadScheme: edgQrSch);
                    }
#if DEBUG
                    LaplaceAffine.ClearEntries();
                    LapaceIp.ComputeMatrixEx(T.Mapping, null, T.Mapping,
                                             RefLaplaceMtx, LaplaceAffine,
                                             volQuadScheme: volQrSch, edgeQuadScheme: edgQrSch);
                    MsrMatrix ErrMtx = RefLaplaceMtx.CloneAs();
                    ErrMtx.Acc(-1.0, LaplaceMtx);
                    double err    = ErrMtx.InfNorm();
                    double infNrm = LaplaceMtx.InfNorm();
                    Console.WriteLine("Matrix comparison error: " + err + ", matrix norm is: " + infNrm);
                    Assert.Less(err, infNrm * 1e-10, "MsrMatrix2 comparison failed.");
#endif
                    stw.Stop();
                    Console.WriteLine("done {0} sec.", stw.Elapsed.TotalSeconds);
                }


                //double condNo = LaplaceMtx.condest(BatchmodeConnector.Flavor.Octave);
                //Console.WriteLine("condition number: {0:0.####E-00} ",condNo);
            }
        }