Actual source code: ex10.c

  1: #include <petscksp.h>
  2: #include <petscpc.h>

  4: typedef struct {
  5:   PetscInt  num_levels;
  6:   PetscInt *n_per_level;
  7:   Mat       stiff;
  8:   Mat      *ProlongationOps;
  9:   PetscBT  *CFMarkers;
 10:   KSP       kspHypre;
 11: } *DataCompression;

 13: PetscErrorCode Create1dLaplacian(PetscInt, Mat *);
 14: PetscErrorCode DataCompExportMats(DataCompression);
 15: PetscErrorCode DataCompDestroy(DataCompression);

 17: int main(int Argc, char **Args)
 18: {
 19:   PetscInt        n_nodes = 33;
 20:   Vec             x, b;
 21:   PC              pcHypre;
 22:   DataCompression data_comp;

 24:   PetscFunctionBeginUser;
 25:   PetscCall(PetscInitialize(&Argc, &Args, NULL, NULL));

 27:   PetscCall(PetscNew(&data_comp));

 29:   // Creating stiffness matrix
 30:   PetscCall(Create1dLaplacian(n_nodes, &data_comp->stiff));
 31:   PetscCall(PetscObjectSetName((PetscObject)data_comp->stiff, "Stiffness"));

 33:   // Set-up BoomerAMG PC to get Prolongation Operators and Coarse/Fine splittings
 34:   PetscCall(KSPCreate(PETSC_COMM_WORLD, &data_comp->kspHypre));
 35:   PetscCall(KSPSetType(data_comp->kspHypre, KSPRICHARDSON));
 36:   PetscCall(KSPGetPC(data_comp->kspHypre, &pcHypre));
 37:   PetscCall(PCSetType(pcHypre, PCHYPRE));
 38:   PetscCall(PCHYPRESetType(pcHypre, "boomeramg"));
 39:   PetscCall(PCSetFromOptions(pcHypre));
 40:   PetscCall(PCSetOperators(pcHypre, data_comp->stiff, data_comp->stiff));
 41:   PetscCall(PCSetUp(pcHypre));

 43:   PetscCall(MatCreateVecs(data_comp->stiff, &x, &b));
 44:   PetscCall(PCApply(pcHypre, x, b));
 45:   PetscCall(VecDestroy(&x));
 46:   PetscCall(VecDestroy(&b));

 48:   //Viewing the PC and Extracting the Prolongation Operators and CFMarkers
 49:   PetscCall(PCView(pcHypre, NULL));
 50:   PetscCall(PCGetInterpolations(pcHypre, &data_comp->num_levels, &data_comp->ProlongationOps));
 51:   PetscCall(PCHYPREGetCFMarkers(pcHypre, &data_comp->n_per_level, &data_comp->CFMarkers));

 53:   PetscCall(DataCompExportMats(data_comp));
 54:   PetscCall(DataCompDestroy(data_comp));
 55:   PetscCall(PetscFinalize());
 56:   return 0;
 57: }

 59: PetscErrorCode Create1dLaplacian(PetscInt n, Mat *mat)
 60: {
 61:   PetscFunctionBeginUser;
 62:   PetscCall(MatCreateSeqAIJ(PETSC_COMM_SELF, n, n, 3, NULL, mat));
 63:   PetscCall(MatSetValue(*mat, n - 1, n - 1, 2.0, INSERT_VALUES));
 64:   for (PetscInt i = 0; i < n - 1; i++) {
 65:     PetscCall(MatSetValue(*mat, i, i, 2.0, INSERT_VALUES));
 66:     PetscCall(MatSetValue(*mat, i + 1, i, -1.0, INSERT_VALUES));
 67:     PetscCall(MatSetValue(*mat, i, i + 1, -1.0, INSERT_VALUES));
 68:   }
 69:   PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
 70:   PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
 71:   PetscFunctionReturn(PETSC_SUCCESS);
 72: }

 74: PetscErrorCode DataCompExportMats(DataCompression data_comp)
 75: {
 76:   PetscFunctionBeginUser;
 77:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Num levels: %" PetscInt_FMT "\n", data_comp->num_levels));
 78:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, " -- Nodes per level --\n"));
 79:   for (PetscInt i = 0; i < data_comp->num_levels; i++) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Level %" PetscInt_FMT ": %" PetscInt_FMT "\n", i, data_comp->n_per_level[i]));

 81:   for (PetscInt i = 0; i < data_comp->num_levels - 1; i++) {
 82:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Prolongation Operator - Level %" PetscInt_FMT "\n", i));
 83:     PetscCall(PetscObjectSetName((PetscObject)data_comp->ProlongationOps[i], "P"));
 84:     PetscCall(MatView(data_comp->ProlongationOps[i], PETSC_VIEWER_STDOUT_WORLD));
 85:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\n"));
 86:   }

 88:   for (PetscInt i = 0; i < data_comp->num_levels - 1; i++) {
 89:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Coarse/Fine splitting - Level %" PetscInt_FMT "\n", i + 1));
 90:     PetscCall(PetscBTView(data_comp->n_per_level[i + 1], data_comp->CFMarkers[i], PETSC_VIEWER_STDOUT_WORLD));
 91:   }
 92:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Stiffness matrix, sparse format:\n"));
 93:   PetscCall(MatViewFromOptions(data_comp->stiff, NULL, "-mat_view_stiff"));
 94:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Finished calling the Viewer functions\n"));
 95:   PetscFunctionReturn(PETSC_SUCCESS);
 96: }

 98: PetscErrorCode DataCompDestroy(DataCompression data_comp)
 99: {
100:   PetscFunctionBeginUser;
101:   if (data_comp == NULL) PetscFunctionReturn(PETSC_SUCCESS);
102:   PetscCall(MatDestroy(&data_comp->stiff));
103:   PetscCall(KSPDestroy(&data_comp->kspHypre));
104:   for (PetscInt i = 0; i < data_comp->num_levels - 1; i++) {
105:     PetscCall(MatDestroy(&data_comp->ProlongationOps[i]));
106:     PetscCall(PetscBTDestroy(&data_comp->CFMarkers[i]));
107:   }
108:   PetscCall(PetscFree(data_comp->ProlongationOps));
109:   PetscCall(PetscFree(data_comp->n_per_level));
110:   PetscCall(PetscFree(data_comp->CFMarkers));
111:   PetscCall(PetscFree(data_comp));
112:   PetscFunctionReturn(PETSC_SUCCESS);
113: }

115: /*TEST

117:    test:
118:       requires: hypre !complex !defined(PETSC_HAVE_HYPRE_DEVICE)
119:       args: -pc_hypre_boomeramg_coarsen_type modifiedRuge-Stueben -pc_hypre_boomeramg_interp_type classical -pc_hypre_boomeramg_strong_threshold 0.25 pc_hypre_boomeramg_numfunctions 1 -pc_hypre_boomeramg_max_row_sum 1.0 -mat_view_stiff

121: TEST*/