public static void XDG_MatrixPolynomialRestAndPrlgTest_2( [Values(0, 1, 2, 3)] int p, [Values(0.0, 0.3)] double AggregationThreshold) { var mode = MultigridOperator.Mode.IdMass; // !!!!! Test work only with orthonormalization at each level. !!!! if (AggregationThreshold < 0.1 && p >= 3 && mode == MultigridOperator.Mode.IdMass) { // this test combination is not supposed to work: // without agglomeration, for high p, the mass matrix may be indefinite in small cut-cells // => Cholesky decomposition on mass matrix fails, i.e. 'mode == IdMass' cannot succeed. return; } XQuadFactoryHelper.MomentFittingVariants variant = XQuadFactoryHelper.MomentFittingVariants.OneStepGaussAndStokes; var xt = new XDGTestSetup(p, AggregationThreshold, 1, mode, variant); // Restriction & prolongation together with orthonormalization // ----------------------------------------------------------- for (var mgop = xt.XdgMultigridOp.CoarserLevel; mgop != null; mgop = mgop.CoarserLevel) { Assert.GreaterOrEqual(mgop.LevelIndex, 1); //var Itself = mgop.Mapping.FromOtherLevelMatrix(mgop.Mapping); //Itself.AccEyeSp(-1.0); //double Itslef_Norm = Itself.InfNorm(); //Console.WriteLine("Level {0}, Restriction onto itself {1}", mgop.Mapping.AggGrid.MgLevel, Itslef_Norm); //Assert.LessOrEqual(Itslef_Norm, 1.0e-8); var map_fine = mgop.FinerLevel.Mapping; int L_fine = map_fine.LocalLength; int L_coarse = mgop.Mapping.LocalLength; // create random test vector Random rnd = new Random(mgop.LevelIndex); double[] vecCoarse = new double[L_coarse]; for (int l = 0; l < L_coarse; l++) { vecCoarse[l] = rnd.NextDouble(); } // prolongate & restrict double[] vecFine = new double[L_fine]; mgop.Prolongate(1.0, vecFine, 0.0, vecCoarse); // uses matrix double[] vecCoarse_check = new double[L_coarse]; mgop.Restrict(vecFine, vecCoarse_check); // for 'MultigridOperator.Mode.IdMass', prolongation->restriction must be the identity double err = GenericBlas.L2Dist(vecCoarse, vecCoarse_check); double Ref = Math.Max(vecCoarse.L2Norm(), vecCoarse_check.L2Norm()); Console.WriteLine("Restriction/prolongation error: " + err / Ref); Assert.LessOrEqual(err / Ref, 1.0e-8); } }
public static void XDG_PolynomialRestAndPrlgTest( [Values(0, 1, 2, 3)] int p, [Values(0.0, 0.3)] double AggregationThreshold, [Values(0, 1)] int TrackerWidth) { XQuadFactoryHelper.MomentFittingVariants variant = XQuadFactoryHelper.MomentFittingVariants.OneStepGauss; var xt = new XDGTestSetup(p, AggregationThreshold, TrackerWidth, MultigridOperator.Mode.Eye, variant); // Basic Restriction & prolongation Test // ------------------------------------- for (int iLevel = 0; iLevel < MgSeq.Length; iLevel++) { // create basis var XAggBasis = xt.XAggB[iLevel][0]; // do restriction/prolongation double[] RestVec = new double[XAggBasis.LocalDim]; XAggBasis.RestictFromFullGrid(xt.Xdg_uTest.CoordinateVector, RestVec); var Test = xt.Xdg_uTest.CloneAs(); Test.Clear(); XAggBasis.ProlongateToFullGrid(Test.CoordinateVector, RestVec); xt.agg.Extrapolate(Test.Mapping); // compare/test var ERR = xt.Xdg_uTest.CloneAs(); ERR.Acc(-1.0, Test); double ERR_NORM = ERR.L2Norm(); Console.WriteLine("Restriction/Prolongation err (p={0}, level={1}, width={2}, agg={3}): {4}", p, iLevel, TrackerWidth, AggregationThreshold, ERR_NORM); Assert.LessOrEqual(ERR_NORM, 1.0e-6); } }
public static void XDG_ProlongationTest( [Values(0, 1, 2, 3)] int p, [Values(0.0, 0.3)] double AggregationThreshold, [Values(0, 1)] int TrackerWidth, [Values(MultigridOperator.Mode.Eye, MultigridOperator.Mode.IdMass)] MultigridOperator.Mode mode) { XQuadFactoryHelper.MomentFittingVariants variant = XQuadFactoryHelper.MomentFittingVariants.OneStepGaussAndStokes; var xt = new XDGTestSetup(p, AggregationThreshold, TrackerWidth, MultigridOperator.Mode.Eye, variant //, ((Func<double[], double>)(X => X[0] + 0.75)).Vectorize() ); int Jup = grid.Cells.NoOfLocalUpdatedCells; Random rnd = new Random(); int Ltop = xt.XdgMultigridOp.Mapping.LocalLength; // Number of DOF's on top multigrid level. double[] RndVec = Ltop.ForLoop(i => rnd.NextDouble()); double[] NoJmpVec = new double[Ltop]; for (int iLevel = 0; iLevel < MgSeq.Length - 1; iLevel++) { XDG_Recursive(0, iLevel, xt.XdgMultigridOp, RndVec, NoJmpVec); // restrict RndVec downt to level 'iLevel', and back up // right now, the XDG field defined by 'NoJmpVec' should be a member // of the aggregated XDG space on level 'iLevel'; // so, there should be no inter-element jumps on the fine level, for each aggregated cell. // Let's test that! XDGField Test = new XDGField(xt.XB, "Test"); xt.XdgMultigridOp.TransformSolFrom(Test.CoordinateVector, NoJmpVec); //xt.agg.Extrapolate(Test.Mapping); var aggGrd = MgSeq[iLevel]; foreach (var spc in xt.LsTrk.SpeciesIdS) { var Test_spc = Test.GetSpeciesShadowField(spc); var SpcMask = xt.LsTrk.Regions.GetSpeciesMask(spc); BitArray AggSourceBitmask = xt.agg.GetAgglomerator(spc).AggInfo.SourceCells.GetBitMask(); double Err = 0; for (int jagg = 0; jagg < aggGrd.iLogicalCells.NoOfLocalUpdatedCells; jagg++) { BitArray CompCellMask = new BitArray(Jup); foreach (int jCell in aggGrd.iLogicalCells.AggregateCellToParts[jagg]) { if (!AggSourceBitmask[jCell]) { CompCellMask[jCell] = true; } } SubGrid CompCellSubGrid = new SubGrid((new CellMask(grid, CompCellMask)).Intersect(SpcMask)); Err += JumpNorm(Test_spc, CompCellSubGrid.InnerEdgesMask).Pow2(); } Console.WriteLine("prolongation jump test (level {0}, species {2}): {1}", iLevel, Err, xt.LsTrk.GetSpeciesName(spc)); Assert.LessOrEqual(Err, 1.0e-8); } } }
public static void XDG_MatrixPolynomialRestAndPrlgTest( [Values(0, 1, 2, 3)] int p, [Values(0.0, 0.3)] double AggregationThreshold, [Values(0, 1)] int TrackerWidth) { XQuadFactoryHelper.MomentFittingVariants variant = XQuadFactoryHelper.MomentFittingVariants.OneStepGaussAndStokes; var xt = new XDGTestSetup(p, AggregationThreshold, TrackerWidth, MultigridOperator.Mode.Eye, variant); // test matrix version of the restriction operator // ----------------------------------------------- List <MultigridMapping> MultigridMaps = new List <MultigridMapping>(); for (var mgop = xt.XdgMultigridOp; mgop != null; mgop = mgop.CoarserLevel) { MultigridMaps.Add(mgop.Mapping); } for (int iLevel = 0; iLevel < MgSeq.Length; iLevel++) { MultigridMapping mgMap = MultigridMaps[iLevel]; var XAggBasis = mgMap.AggBasis[0]; // set the test field: XDGField Test = new XDGField(xt.XB, "Test"); Random rand = new Random(); for (int i = 0; i < Test.CoordinateVector.Count; i++) { Test.CoordinateVector[i] = rand.NextDouble(); } xt.agg.ClearAgglomerated(Test.CoordinateVector, Test.Mapping); // do restriction/prolongation (Reference) double[] RestVecRef = new double[XAggBasis.LocalDim]; XAggBasis.RestictFromFullGrid(Test.CoordinateVector, RestVecRef); // and now with the matrix: BlockMsrMatrix RestMtx = new BlockMsrMatrix(mgMap, mgMap.ProblemMapping); XAggBasis.GetRestrictionMatrix(RestMtx, mgMap, 0); double[] RestVec = new double[mgMap.LocalLength]; RestMtx.SpMV(1.0, Test.CoordinateVector, 0.0, RestVec); double[] X1 = new double[xt.XdgMultigridOp.Mapping.LocalLength]; XDGField X2 = new XDGField(Test.Basis); xt.XdgMultigridOp.TransformSolInto(Test.CoordinateVector, X1); xt.XdgMultigridOp.TransformSolFrom(X2.CoordinateVector, X1); //xt.agg.Extrapolate(X2.CoordinatesAsVector, X2.Mapping); var ERR2 = Test.CloneAs(); ERR2.Acc(-1.0, X2); double ERR2Norm = ERR2.L2Norm(); //Console.WriteLine("MultigridOperator TranformInto/FransformFrom mismatch: " + ERR2Norm); Assert.LessOrEqual(ERR2Norm, 1.0e-8); // compare double ERR = 0.0; int Nmax = XAggBasis.MaximalLength; for (int jAgg = 0; jAgg < mgMap.AggGrid.iLogicalCells.NoOfLocalUpdatedCells; jAgg++) { int i0Ref = jAgg * Nmax; int i0Tst = mgMap.LocalUniqueIndex(0, jAgg, 0); int N = mgMap.GetLength(jAgg); for (int n = 0; n < N; n++) { double dist = RestVecRef[i0Ref + n] - RestVec[i0Tst + n]; ERR += dist.Pow2(); } } Console.WriteLine("Restriction matrix test (iLevel = {0}): {1}", iLevel, ERR); Assert.LessOrEqual(ERR, 1.0e-8); // double[] PrlgVecA = new double[XAggBasis.LocalDim]; double[] PrlgVecB = new double[mgMap.LocalLength]; for (int jAgg = 0; jAgg < mgMap.AggGrid.iLogicalCells.NoOfLocalUpdatedCells; jAgg++) { int i0Ref = jAgg * Nmax; int i0Tst = mgMap.LocalUniqueIndex(0, jAgg, 0); int N = mgMap.GetLength(jAgg); for (int n = 0; n < N; n++) { double rndVal = rand.NextDouble(); PrlgVecA[i0Ref + n] = rndVal; PrlgVecB[i0Tst + n] = rndVal; } } XDGField QA = new XDGField(Test.Basis); XDGField QB = new XDGField(Test.Basis); XAggBasis.ProlongateToFullGrid(QA.CoordinateVector, PrlgVecA); var PrlgMtx = RestMtx.Transpose(); PrlgMtx.SpMV(1.0, PrlgVecB, 0.0, QB.CoordinateVector); XDGField ERR5 = QA.CloneAs(); ERR5.Acc(-1.0, QB); double ERR5_Norm = ERR5.L2Norm(); Console.WriteLine("Prolongation matrix test (iLevel = {0}): {1}", iLevel, ERR5_Norm); Assert.LessOrEqual(ERR5_Norm, 1.0e-8); } }
public static void XDG_MatrixPolynomialRestAndPrlgTest_2( [Values(0, 1, 2, 3)] int p, [Values(0.0, 0.3)] double AggregationThreshold, [Values(0, 1)] int TrackerWidth, [Values(MultigridOperator.Mode.Eye, MultigridOperator.Mode.IdMass)] MultigridOperator.Mode mode) { if (AggregationThreshold < 0.1 && p >= 3 && mode == MultigridOperator.Mode.IdMass) { // this test combination is not supposed to work: // without agglomeration, for high p, the mass matrix may be indefinite in small cut-cells // => Cholesky decomposition on mass matrix fails, i.e. 'mode == IdMass' cannot succseed. return; } XQuadFactoryHelper.MomentFittingVariants variant = XQuadFactoryHelper.MomentFittingVariants.OneStepGaussAndStokes; var xt = new XDGTestSetup(p, AggregationThreshold, TrackerWidth, mode, variant); // Restriction & prolongation together with orthonormalization // ----------------------------------------------------------- for (var mgop = xt.MultigridOp; mgop != null; mgop = mgop.CoarserLevel) { var Itself = mgop.Mapping.FromOtherLevelMatrix(mgop.Mapping); Itself.AccEyeSp(-1.0); double Itslef_Norm = Itself.InfNorm(); //Console.WriteLine("Level {0}, Restriction onto itself {1}", mgm.LevelIndex, Itslef_Norm); Assert.LessOrEqual(Itslef_Norm, 1.0e-8); } { // test change of basis on top level XDGField uTestRnd = new XDGField(xt.XB); Random rnd = new Random(); for (int i = 0; i < uTestRnd.CoordinateVector.Count; i++) { uTestRnd.CoordinateVector[i] = rnd.NextDouble(); } xt.agg.ClearAgglomerated(uTestRnd.CoordinateVector, uTestRnd.Mapping); // perform change of basis on top level ... int Ltop = xt.MultigridOp.Mapping.LocalLength; double[] uTest_Fine = new double[Ltop]; xt.MultigridOp.TransformSolInto(uTestRnd.CoordinateVector, uTest_Fine); // .. and back XDGField uError2 = uTestRnd.CloneAs(); uError2.Clear(); xt.MultigridOp.TransformSolFrom(uError2.CoordinateVector, uTest_Fine); // compare: uError2.Acc(-1.0, uTestRnd); double NORM_uError = uError2.L2Norm(); // output Console.WriteLine("Top level change of basis error: {0}", NORM_uError); Assert.LessOrEqual(NORM_uError, 1.0e-8); } { // perform change of basis on top level int Ltop = xt.MultigridOp.Mapping.LocalLength; double[] uTest_Fine = new double[Ltop]; xt.MultigridOp.TransformSolInto(xt.uTest.CoordinateVector, uTest_Fine); // check for each level of the multigrid operator... for (int iLevel = 0; iLevel < MgSeq.Count() - 1; iLevel++) { double[] uTest_Prolonged = new double[Ltop]; XDG_Recursive(0, iLevel, xt.MultigridOp, uTest_Fine, uTest_Prolonged); XDGField uError = xt.uTest.CloneAs(); uError.Clear(); xt.MultigridOp.TransformSolFrom(uError.CoordinateVector, uTest_Prolonged); xt.agg.Extrapolate(uError.Mapping); uError.Acc(-1.0, xt.uTest); double NORM_uError = uError.L2Norm(); Console.WriteLine("Rest/Prlg error, level {0}: {1}", iLevel, NORM_uError); Assert.LessOrEqual(NORM_uError, 1.0e-8); } } }