public static void GetDefaultSelection(this SubBlockSelector sbs, SelectionType SType, int iCell) { SpeciesId A = ((XdgAggregationBasis)sbs.GetMapping.AggBasis[0]).UsedSpecies[0]; SpeciesId B = ((XdgAggregationBasis)sbs.GetMapping.AggBasis[0]).UsedSpecies[1]; sbs.CellSelector(iCell, false); //do not change this, selection corresponds to hardcoded masking //see GetSubIndices switch (SType) { case SelectionType.degrees: sbs.ModeSelector(p => p == 1); break; case SelectionType.species: sbs.SpeciesSelector(A); break; case SelectionType.variables: sbs.VariableSelector(1); break; case SelectionType.all_combined: sbs.ModeSelector(p => p == 1); sbs.SpeciesSelector(A); sbs.VariableSelector(1); break; } }
private static void DefaultCellSplit(this SubBlockSelector sbs, bool upper, bool islocal = true) { List <int> odds = new List <int>(); List <int> even = new List <int>(); int i0, iE; if (islocal) { i0 = 0; iE = sbs.GetMapping.LocalNoOfBlocks; } else { i0 = sbs.GetMapping.LocalNoOfBlocks; iE = sbs.GetMapping.AggGrid.iLogicalCells.NoOfExternalCells + sbs.GetMapping.LocalNoOfBlocks; } for (int i = i0; i < iE; i++) { if (i % 2 != 0) { odds.Add(i); } else { even.Add(i); } } sbs.CellSelector(upper ? odds : even, false); }
public static int[] AllExternalCellsSelection(this SubBlockSelector sbs) { var map = sbs.GetMapping; var extcells = GetAllExternalCells(map); sbs.CellSelector(extcells, false); return(extcells); }
public static void SubSelection( [Values(XDGusage.none, XDGusage.all)] XDGusage UseXdg, [Values(2)] int DGOrder, [Values(MatrixShape.full_var_spec, MatrixShape.full_spec, MatrixShape.full_var, MatrixShape.full)] MatrixShape MShape, [Values(4)] int Res) { Utils.TestInit((int)UseXdg, DGOrder, (int)MShape, Res); Console.WriteLine("SubSelection({0},{1},{2},{3})", UseXdg, DGOrder, MShape, Res); //Arrange --- create test matrix, MG mapping MultigridOperator mgo = Utils.CreateTestMGOperator(UseXdg, DGOrder, MShape, Res); MultigridMapping map = mgo.Mapping; BlockMsrMatrix M = mgo.OperatorMatrix; //Arrange --- get mask int[] cells = Utils.GetCellsOfOverlappingTestBlock(map); Array.Sort(cells); var sbs = new SubBlockSelector(map); sbs.CellSelector(cells, false); BlockMsrMatrix M_ext = BlockMask.GetAllExternalRows(map, M); var mask = new BlockMask(sbs, M_ext); //Arrange --- get GlobalIdxList int[] idc = Utils.GetIdcOfSubBlock(map, cells); bool[] coup = Utils.SetCoupling(MShape); var M_sub = mask.GetSubBlockMatrix(M, false, coup[0], coup[1]); var infNorm = MultidimensionalArray.Create(4, 1); int rank = map.MpiRank; using (BatchmodeConnector matlab = new BatchmodeConnector()) { double[] GlobIdx = idc.Count().ForLoop(i => (double)idc[i] + 1.0); Assert.IsTrue(GlobIdx.Length == M_sub.NoOfRows); matlab.PutSparseMatrix(M, "M"); // note: M_sub lives on Comm_Self, therefore we have to distinguish between procs ... matlab.PutSparseMatrixRankExclusive(M_sub, "M_sub"); matlab.PutVectorRankExclusive(GlobIdx, "Idx"); matlab.Cmd("M_0 = full(M(Idx_0, Idx_0));"); matlab.Cmd("M_1 = full(M(Idx_1, Idx_1));"); matlab.Cmd("M_2 = full(M(Idx_2, Idx_2));"); matlab.Cmd("M_3 = full(M(Idx_3, Idx_3));"); matlab.Cmd("n=[0; 0; 0; 0];"); matlab.Cmd("n(1,1)=norm(M_0-M_sub_0,inf);"); matlab.Cmd("n(2,1)=norm(M_1-M_sub_1,inf);"); matlab.Cmd("n(3,1)=norm(M_2-M_sub_2,inf);"); matlab.Cmd("n(4,1)=norm(M_3-M_sub_3,inf);"); matlab.GetMatrix(infNorm, "n"); matlab.Execute(); } Assert.IsTrue(infNorm[rank, 0] == 0.0); }
/// <summary> /// Local condition number formed by the block of each cell and its neighbors. /// </summary> /// <returns> /// one value per cell: /// - index: local cell index /// - content: condition number (one norm) of the local stencil /// </returns> public double[] StencilCondNumbers() { using (new FuncTrace()) { int J = m_map.LocalNoOfBlocks; Debug.Assert(J == m_map.GridDat.iLogicalCells.NoOfLocalUpdatedCells); var Mtx = m_MultigridOp.OperatorMatrix; Debug.Assert(Mtx._ColPartitioning.LocalNoOfBlocks == J); Debug.Assert(Mtx._RowPartitioning.LocalNoOfBlocks == J); var grd = m_MultigridOp.Mapping.AggGrid; double[] BCN = new double[J]; for (int j = 0; j < J; j++) { var LocBlk = grd.GetCellNeighboursViaEdges(j).Select(t => t.Item1).ToList(); LocBlk.Add(j); for (int i = 0; i < LocBlk.Count; i++) { if (LocBlk[i] >= J) { LocBlk.RemoveAt(i); i--; } } var Sel = new SubBlockSelector(m_MultigridOp.Mapping); Sel.VariableSelector(this.VarGroup); Sel.CellSelector(LocBlk, global: false); var Mask = new BlockMask(Sel); MultidimensionalArray[,] Blocks = Mask.GetFullSubBlocks(Mtx, ignoreSpecCoupling: false, ignoreVarCoupling: false); MultidimensionalArray FullBlock = Blocks.Cat(); BCN[j] = FullBlock.Cond('I'); } return(BCN); } }
/// <summary> /// returns the condition number of the full matrix using MUMPS; /// Note: /// </summary> public double CondNumMUMPS() { using (new FuncTrace()) { int[] DepVars = this.VarGroup; var grd = m_map.GridDat; int NoOfCells = grd.Grid.NumberOfCells; int NoOfBdryCells = grd.GetBoundaryCells().NoOfItemsLocally_WithExternal; var Mtx = m_MultigridOp.OperatorMatrix; // Blocks and selectors // ==================== var InnerCellsMask = grd.GetBoundaryCells().Complement(); var FullSel = new SubBlockSelector(m_MultigridOp.Mapping); FullSel.VariableSelector(this.VarGroup); var InnerSel = new SubBlockSelector(m_MultigridOp.Mapping); InnerSel.VariableSelector(this.VarGroup); InnerSel.CellSelector(InnerCellsMask); // MUMPS condition number // ====================== double condestFullMUMPS = (new BlockMask(FullSel)).GetSubBlockMatrix(Mtx).Condest_MUMPS(); //double condestInnerMUMPS = 1.0; //if(InnerCellsMask.NoOfItemsLocally.MPISum() > 0) { // condestInnerMUMPS = (new BlockMask(InnerSel)).GetSubBlockMatrix(Mtx).Condest_MUMPS(); //} //return new[] { condestFullMUMPS, condestInnerMUMPS }; return(condestFullMUMPS); } }
/// <summary> /// returns the condition number of the full matrix and the inner matrix without boundary terms /// </summary> /// <returns> /// [ConditionNumberFullOp, ConditionNumberInnerOp] /// </returns> public (double, double) CondNumMatlab2() { int[] DepVars = this.VarGroup; var grd = m_map.GridDat; int NoOfCells = grd.Grid.NumberOfCells; int NoOfBdryCells = grd.GetBoundaryCells().NoOfItemsLocally_WithExternal; var Mtx = m_MultigridOp.OperatorMatrix; // Blocks and selectors // ==================== var InnerCellsMask = grd.GetBoundaryCells().Complement(); var FullSel = new SubBlockSelector(m_MultigridOp.Mapping); FullSel.VariableSelector(this.VarGroup); var InnerSel = new SubBlockSelector(m_MultigridOp.Mapping); InnerSel.VariableSelector(this.VarGroup); InnerSel.CellSelector(InnerCellsMask); // Matlab // ====== double[] Full_0Vars = (new BlockMask(FullSel)).GlobalIndices.Select(i => i + 1.0).ToArray(); double[] Inner_0Vars = (new BlockMask(InnerSel)).GlobalIndices.Select(i => i + 1.0).ToArray(); MultidimensionalArray output = MultidimensionalArray.Create(2, 1); string[] names = new string[] { "Full_0Vars", "Inner_0Vars" }; using (BatchmodeConnector bmc = new BatchmodeConnector()) { // if Octave should be used instead of Matlab.... // BatchmodeConnector.Flav = BatchmodeConnector.Flavor.Octave; bmc.PutSparseMatrix(Mtx, "FullMatrix"); if (Inner_0Vars.Length > 0) { bmc.PutVector(Inner_0Vars, "Inner_0Vars"); } bmc.PutVector(Full_0Vars, "Full_0Vars"); bmc.Cmd("output = ones(2,1);"); bmc.Cmd("output(1) = condest(FullMatrix(Full_0Vars,Full_0Vars));"); if (Inner_0Vars.Length > 0) { bmc.Cmd("output(2) = condest(FullMatrix(Inner_0Vars,Inner_0Vars));"); } bmc.GetMatrix(output, "output"); bmc.Execute(false); double condestFull = output[0, 0]; double condestInner = output[1, 0]; Debug.Assert(condestFull.MPIEquals(), "value does not match on procs"); Debug.Assert(condestInner.MPIEquals(), "value does not match on procs"); Console.WriteLine($"MATLAB condition number: {condestFull:0.###e-00}"); return(condestFull, condestInner); } }