Skip to content

Commit bd1b2c3

Browse files
committed
Added GetDiagonal
1 parent 0b6fa71 commit bd1b2c3

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed

examples/Hdiv-mixed/include/setup-matops.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88

99
PetscErrorCode ApplyLocalCeedOp(Vec X, Vec Y, OperatorApplyContext op_apply_ctx);
1010
PetscErrorCode ApplyAddLocalCeedOp(Vec X, Vec Y, OperatorApplyContext op_apply_ctx);
11+
PetscErrorCode GetDiagonal(Mat A, Vec D);
1112

1213
#endif // setup_matops_h

examples/Hdiv-mixed/src/setup-matops.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "../include/setup-matops.h"
2+
#include <stdio.h>
23

34
#include "../include/setup-libceed.h"
5+
#include "ceed/ceed.h"
46

57
// -----------------------------------------------------------------------------
68
// Apply the local action of a libCEED operator and store result in PETSc vector
@@ -49,3 +51,34 @@ PetscErrorCode ApplyAddLocalCeedOp(Vec X, Vec Y, OperatorApplyContext op_apply_c
4951
};
5052

5153
// -----------------------------------------------------------------------------
54+
// This function returns the computed diagonal of the operator
55+
// -----------------------------------------------------------------------------
56+
PetscErrorCode GetDiagonal(Mat A, Vec D) {
57+
OperatorApplyContext op_apply_ctx;
58+
PetscScalar *x;
59+
PetscMemType x_mem_type;
60+
61+
PetscFunctionBeginUser;
62+
63+
PetscCall(MatShellGetContext(A, &op_apply_ctx));
64+
65+
// -- Place PETSc vector in libCEED vector
66+
PetscCall(VecGetArrayAndMemType(op_apply_ctx->X_loc, &x, &x_mem_type));
67+
CeedVectorSetArray(op_apply_ctx->x_ceed, MemTypeP2C(x_mem_type), CEED_USE_POINTER, x);
68+
69+
// -- Compute Diagonal
70+
CeedOperatorLinearAssembleDiagonal(op_apply_ctx->op_apply, op_apply_ctx->x_ceed, CEED_REQUEST_IMMEDIATE);
71+
CeedVectorView(op_apply_ctx->x_ceed, "%12.8f", stdout);
72+
// -- Local-to-Global
73+
CeedVectorTakeArray(op_apply_ctx->x_ceed, MemTypeP2C(x_mem_type), NULL);
74+
PetscCall(VecRestoreArrayAndMemType(op_apply_ctx->X_loc, &x));
75+
PetscCall(VecZeroEntries(D));
76+
PetscCall(DMLocalToGlobal(op_apply_ctx->dm, op_apply_ctx->X_loc, ADD_VALUES, D));
77+
78+
// Cleanup
79+
PetscCall(VecZeroEntries(op_apply_ctx->X_loc));
80+
81+
PetscFunctionReturn(0);
82+
};
83+
84+
// -----------------------------------------------------------------------------

examples/Hdiv-mixed/src/setup-solvers.c

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,6 @@ PetscErrorCode SNESFormJacobian(SNES snes, Vec U, Mat J, Mat J_pre, void *ctx_ja
9393
// OperatorApplyContext ctx = (OperatorApplyContext)ctx_jacobian;
9494
PetscFunctionBeginUser;
9595

96-
// Mat A;
97-
// PetscCall(DMCreateMatrix(ctx->dm, &A));
98-
//// Assemble matrix analytically
99-
// PetscCount num_entries;
100-
// CeedInt *rows, *cols;
101-
// CeedVector coo_values;
102-
// CeedOperatorLinearAssembleSymbolic(ctx->op_apply, &num_entries, &rows, &cols);
103-
// PetscCall(MatSetPreallocationCOO(A, num_entries, rows, cols));
104-
// free(rows);
105-
// free(cols);
106-
// CeedVectorCreate(ctx->ceed, num_entries, &coo_values);
107-
// CeedOperatorLinearAssemble(ctx->op_apply, coo_values);
108-
// const CeedScalar *values;
109-
// CeedVectorGetArrayRead(coo_values, CEED_MEM_HOST, &values);
110-
// PetscCall(MatSetValuesCOO(A, values, ADD_VALUES));
111-
// CeedVectorRestoreArrayRead(coo_values, &values);
112-
// MatView(A, PETSC_VIEWER_STDOUT_WORLD);
113-
//// CeedVectorView(coo_values, "%12.8f", stdout);
114-
// CeedVectorDestroy(&coo_values);
115-
// PetscCall(MatDestroy(&A));
116-
11796
// J_pre might be AIJ (e.g., when using coloring), so we need to assemble it
11897
PetscCall(MatAssemblyBegin(J_pre, MAT_FINAL_ASSEMBLY));
11998
PetscCall(MatAssemblyEnd(J_pre, MAT_FINAL_ASSEMBLY));
@@ -148,6 +127,7 @@ PetscErrorCode PDESolver(CeedData ceed_data, AppCtx app_ctx, SNES snes, KSP ksp,
148127
// -- Form Action of Jacobian on delta_u
149128
PetscCall(MatCreateShell(app_ctx->comm, U_l_size, U_l_size, U_g_size, U_g_size, app_ctx->ctx_jacobian, &mat_jacobian));
150129
PetscCall(MatShellSetOperation(mat_jacobian, MATOP_MULT, (void (*)(void))ApplyMatOp));
130+
PetscCall(MatShellSetOperation(mat_jacobian, MATOP_GET_DIAGONAL, (void (*)(void))GetDiagonal));
151131
PetscCall(MatShellSetVecType(mat_jacobian, app_ctx->ctx_jacobian->vec_type));
152132

153133
// Set SNES residual evaluation function
@@ -156,8 +136,17 @@ PetscErrorCode PDESolver(CeedData ceed_data, AppCtx app_ctx, SNES snes, KSP ksp,
156136
PetscCall(SNESSetJacobian(snes, mat_jacobian, mat_jacobian, SNESFormJacobian, app_ctx->ctx_jacobian));
157137

158138
// Setup KSP
139+
PetscCall(KSPSetType(ksp, KSPGMRES));
140+
PetscCall(KSPSetNormType(ksp, KSP_NORM_PRECONDITIONED));
141+
// PC setup
142+
PC pc;
143+
PetscCall(KSPGetPC(ksp, &pc));
144+
PetscCall(PCSetType(pc, PCJACOBI));
145+
PetscCall(PCJacobiSetType(pc, PC_JACOBI_DIAGONAL));
146+
// Set user options and view
159147
PetscCall(KSPSetFromOptions(ksp));
160-
148+
PetscCall(KSPViewFromOptions(ksp, NULL, "-ksp_view"));
149+
PetscCall(PCViewFromOptions(pc, NULL, "-pc_view"));
161150
// Default to critical-point (CP) line search (related to Wolfe's curvature condition)
162151
SNESLineSearch line_search;
163152

0 commit comments

Comments
 (0)