xref: /libCEED/examples/fluids/src/petsc_ops.c (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
24021610dSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
34021610dSJames Wright //
44021610dSJames Wright // SPDX-License-Identifier: BSD-2-Clause
54021610dSJames Wright //
64021610dSJames Wright // This file is part of CEED:  http://github.com/ceed
74021610dSJames Wright 
84021610dSJames Wright #include "../include/petsc_ops.h"
94021610dSJames Wright 
104021610dSJames Wright #include <ceed.h>
114021610dSJames Wright #include <petscdm.h>
124021610dSJames Wright 
134021610dSJames Wright #include "../navierstokes.h"
144021610dSJames Wright 
15b718eef5SJames Wright // @brief Get information about a DM's local vector
DMGetLocalVectorInfo(DM dm,PetscInt * local_size,PetscInt * global_size,VecType * vec_type)16b718eef5SJames Wright PetscErrorCode DMGetLocalVectorInfo(DM dm, PetscInt *local_size, PetscInt *global_size, VecType *vec_type) {
17b718eef5SJames Wright   Vec V_loc;
18b718eef5SJames Wright 
19b718eef5SJames Wright   PetscFunctionBeginUser;
20b718eef5SJames Wright   PetscCall(DMGetLocalVector(dm, &V_loc));
21b718eef5SJames Wright   if (local_size) PetscCall(VecGetLocalSize(V_loc, local_size));
22b718eef5SJames Wright   if (global_size) PetscCall(VecGetSize(V_loc, global_size));
23b718eef5SJames Wright   if (vec_type) PetscCall(VecGetType(V_loc, vec_type));
24b718eef5SJames Wright   PetscCall(DMRestoreLocalVector(dm, &V_loc));
25ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
26b718eef5SJames Wright }
27b718eef5SJames Wright 
28b718eef5SJames Wright // @brief Get information about a DM's global vector
DMGetGlobalVectorInfo(DM dm,PetscInt * local_size,PetscInt * global_size,VecType * vec_type)29b718eef5SJames Wright PetscErrorCode DMGetGlobalVectorInfo(DM dm, PetscInt *local_size, PetscInt *global_size, VecType *vec_type) {
30b718eef5SJames Wright   Vec V;
31b718eef5SJames Wright 
32b718eef5SJames Wright   PetscFunctionBeginUser;
33b718eef5SJames Wright   PetscCall(DMGetGlobalVector(dm, &V));
34b718eef5SJames Wright   if (local_size) PetscCall(VecGetLocalSize(V, local_size));
35b718eef5SJames Wright   if (global_size) PetscCall(VecGetSize(V, global_size));
36b718eef5SJames Wright   if (vec_type) PetscCall(VecGetType(V, vec_type));
37b718eef5SJames Wright   PetscCall(DMRestoreGlobalVector(dm, &V));
38ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
39b718eef5SJames Wright }
40b718eef5SJames Wright 
412cd2c56bSJames Wright /**
422cd2c56bSJames Wright  * @brief Create OperatorApplyContext struct for applying FEM operator in a PETSc context
432cd2c56bSJames Wright  *
442cd2c56bSJames Wright  * All passed in objects are reference copied and may be destroyed if desired (with the exception of `CEED_VECTOR_NONE`).
452cd2c56bSJames Wright  * Resulting context should be destroyed with `OperatorApplyContextDestroy()`.
462cd2c56bSJames Wright  *
472cd2c56bSJames Wright  * @param[in]  dm_x     `DM` associated with the operator active input. May be `NULL`
482cd2c56bSJames Wright  * @param[in]  dm_y     `DM` associated with the operator active output. May be `NULL`
492cd2c56bSJames Wright  * @param[in]  ceed     `Ceed` object
502cd2c56bSJames Wright  * @param[in]  op_apply `CeedOperator` representing the local action of the FEM operator
512cd2c56bSJames Wright  * @param[in]  x_ceed   `CeedVector` for operator active input. May be `CEED_VECTOR_NONE` or `NULL`. If `NULL`, `CeedVector` will be automatically
522cd2c56bSJames Wright  *                      generated.
532cd2c56bSJames Wright  * @param[in]  y_ceed   `CeedVector` for operator active output. May be `CEED_VECTOR_NONE` or `NULL`. If `NULL`, `CeedVector` will be automatically
542cd2c56bSJames Wright  *                      generated.
552cd2c56bSJames Wright  * @param[in]  X_loc    Local `Vec` for operator active input. If `NULL`, vector will be obtained if needed at ApplyCeedOperator time.
562cd2c56bSJames Wright  * @param[in]  Y_loc    Local `Vec` for operator active output. If `NULL`, vector will be obtained if needed at ApplyCeedOperator time.
572cd2c56bSJames Wright  * @param[out] ctx      Struct containing all data necessary for applying the operator
582cd2c56bSJames Wright  */
OperatorApplyContextCreate(DM dm_x,DM dm_y,Ceed ceed,CeedOperator op_apply,CeedVector x_ceed,CeedVector y_ceed,Vec X_loc,Vec Y_loc,OperatorApplyContext * ctx)594021610dSJames Wright PetscErrorCode OperatorApplyContextCreate(DM dm_x, DM dm_y, Ceed ceed, CeedOperator op_apply, CeedVector x_ceed, CeedVector y_ceed, Vec X_loc,
602cd2c56bSJames Wright                                           Vec Y_loc, OperatorApplyContext *ctx) {
614021610dSJames Wright   CeedSize x_size, y_size;
622cd2c56bSJames Wright 
632cd2c56bSJames Wright   PetscFunctionBeginUser;
64a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorGetActiveVectorLengths(op_apply, &x_size, &y_size));
652cd2c56bSJames Wright   {  // Verify sizes
66dd715608SJames Wright     PetscInt X_size, Y_size, dm_X_size, dm_Y_size;
67dd715608SJames Wright     CeedSize x_ceed_size, y_ceed_size;
68dd715608SJames Wright     if (dm_x) PetscCall(DMGetLocalVectorInfo(dm_x, &dm_X_size, NULL, NULL));
69dd715608SJames Wright     if (dm_y) PetscCall(DMGetLocalVectorInfo(dm_y, &dm_Y_size, NULL, NULL));
704021610dSJames Wright     if (X_loc) {
714021610dSJames Wright       PetscCall(VecGetLocalSize(X_loc, &X_size));
724021610dSJames Wright       PetscCheck(X_size == x_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ,
734021610dSJames Wright                  "X_loc (%" PetscInt_FMT ") not correct size for CeedOperator active input size (%" CeedSize_FMT ")", X_size, x_size);
746c10af5dSJeremy L Thompson       if (dm_x) {
75dd715608SJames Wright         PetscCheck(X_size == dm_X_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ,
76dd715608SJames Wright                    "X_loc size (%" PetscInt_FMT ") does not match dm_x local vector size (%" PetscInt_FMT ")", X_size, dm_X_size);
774021610dSJames Wright       }
786c10af5dSJeremy L Thompson     }
794021610dSJames Wright     if (Y_loc) {
804021610dSJames Wright       PetscCall(VecGetLocalSize(Y_loc, &Y_size));
814021610dSJames Wright       PetscCheck(Y_size == y_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ,
824021610dSJames Wright                  "Y_loc (%" PetscInt_FMT ") not correct size for CeedOperator active output size (%" CeedSize_FMT ")", Y_size, y_size);
836c10af5dSJeremy L Thompson       if (dm_y) {
84dd715608SJames Wright         PetscCheck(Y_size == dm_Y_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ,
85dd715608SJames Wright                    "Y_loc size (%" PetscInt_FMT ") does not match dm_y local vector size (%" PetscInt_FMT ")", Y_size, dm_Y_size);
86dd715608SJames Wright       }
876c10af5dSJeremy L Thompson     }
88dd715608SJames Wright     if (x_ceed && x_ceed != CEED_VECTOR_NONE) {
89a424bcd0SJames Wright       PetscCallCeed(ceed, CeedVectorGetLength(x_ceed, &x_ceed_size));
90dd715608SJames Wright       PetscCheck(x_size >= 0 ? x_ceed_size == x_size : true, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ,
91dd715608SJames Wright                  "x_ceed (%" CeedSize_FMT ") not correct size for CeedOperator active input size (%" CeedSize_FMT ")", x_ceed_size, x_size);
926c10af5dSJeremy L Thompson       if (dm_x) {
93dd715608SJames Wright         PetscCheck(x_ceed_size == dm_X_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ,
94dd715608SJames Wright                    "x_ceed size (%" CeedSize_FMT ") does not match dm_x local vector size (%" PetscInt_FMT ")", x_ceed_size, dm_X_size);
95dd715608SJames Wright       }
966c10af5dSJeremy L Thompson     }
97dd715608SJames Wright     if (y_ceed && y_ceed != CEED_VECTOR_NONE) {
98a424bcd0SJames Wright       PetscCallCeed(ceed, CeedVectorGetLength(y_ceed, &y_ceed_size));
99dd715608SJames Wright       PetscCheck(y_ceed_size == y_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ,
100dd715608SJames Wright                  "y_ceed (%" CeedSize_FMT ") not correct size for CeedOperator active input size (%" CeedSize_FMT ")", y_ceed_size, y_size);
1016c10af5dSJeremy L Thompson       if (dm_y) {
102dd715608SJames Wright         PetscCheck(y_ceed_size == dm_Y_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ,
103dd715608SJames Wright                    "y_ceed size (%" CeedSize_FMT ") does not match dm_y local vector size (%" PetscInt_FMT ")", y_ceed_size, dm_Y_size);
1044021610dSJames Wright       }
1054021610dSJames Wright     }
1066c10af5dSJeremy L Thompson   }
1074021610dSJames Wright 
1082cd2c56bSJames Wright   PetscCall(PetscNew(ctx));
1094021610dSJames Wright 
1104021610dSJames Wright   // Copy PETSc Objects
1114021610dSJames Wright   if (dm_x) PetscCall(PetscObjectReference((PetscObject)dm_x));
1122cd2c56bSJames Wright   (*ctx)->dm_x = dm_x;
1134021610dSJames Wright   if (dm_y) PetscCall(PetscObjectReference((PetscObject)dm_y));
1142cd2c56bSJames Wright   (*ctx)->dm_y = dm_y;
1154021610dSJames Wright 
1164021610dSJames Wright   if (X_loc) PetscCall(PetscObjectReference((PetscObject)X_loc));
1172cd2c56bSJames Wright   (*ctx)->X_loc = X_loc;
1184021610dSJames Wright   if (Y_loc) PetscCall(PetscObjectReference((PetscObject)Y_loc));
1192cd2c56bSJames Wright   (*ctx)->Y_loc = Y_loc;
1204021610dSJames Wright 
1214021610dSJames Wright   // Copy libCEED objects
122a424bcd0SJames Wright   if (x_ceed) PetscCallCeed(ceed, CeedVectorReferenceCopy(x_ceed, &(*ctx)->x_ceed));
123a424bcd0SJames Wright   else PetscCallCeed(ceed, CeedVectorCreate(ceed, x_size, &(*ctx)->x_ceed));
1242cd2c56bSJames Wright 
125a424bcd0SJames Wright   if (y_ceed) PetscCallCeed(ceed, CeedVectorReferenceCopy(y_ceed, &(*ctx)->y_ceed));
126a424bcd0SJames Wright   else PetscCallCeed(ceed, CeedVectorCreate(ceed, y_size, &(*ctx)->y_ceed));
1272cd2c56bSJames Wright 
128a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorReferenceCopy(op_apply, &(*ctx)->op));
129a424bcd0SJames Wright   PetscCallCeed(ceed, CeedReferenceCopy(ceed, &(*ctx)->ceed));
130ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
1314021610dSJames Wright }
1324021610dSJames Wright 
1332cd2c56bSJames Wright /**
1342cd2c56bSJames Wright  * @brief Destroy OperatorApplyContext struct
1352cd2c56bSJames Wright  *
1362cd2c56bSJames Wright  * @param[in,out] ctx Context to destroy
1372cd2c56bSJames Wright  */
OperatorApplyContextDestroy(OperatorApplyContext ctx)1382cd2c56bSJames Wright PetscErrorCode OperatorApplyContextDestroy(OperatorApplyContext ctx) {
1394021610dSJames Wright   PetscFunctionBeginUser;
140ee4ca9cbSJames Wright   if (!ctx) PetscFunctionReturn(PETSC_SUCCESS);
141a424bcd0SJames Wright   Ceed ceed = ctx->ceed;
1424021610dSJames Wright 
1434021610dSJames Wright   // Destroy PETSc Objects
1442cd2c56bSJames Wright   PetscCall(DMDestroy(&ctx->dm_x));
1452cd2c56bSJames Wright   PetscCall(DMDestroy(&ctx->dm_y));
1462cd2c56bSJames Wright   PetscCall(VecDestroy(&ctx->X_loc));
1472cd2c56bSJames Wright   PetscCall(VecDestroy(&ctx->Y_loc));
1484021610dSJames Wright 
1494021610dSJames Wright   // Destroy libCEED Objects
150a424bcd0SJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&ctx->x_ceed));
151a424bcd0SJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&ctx->y_ceed));
152a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&ctx->op));
153a424bcd0SJames Wright   PetscCallCeed(ceed, CeedDestroy(&ctx->ceed));
1544021610dSJames Wright 
1552cd2c56bSJames Wright   PetscCall(PetscFree(ctx));
156ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
1574021610dSJames Wright }
1584021610dSJames Wright 
1594210c567SJames Wright //@brief Return VecType for the given DM
DMReturnVecType(DM dm)1604210c567SJames Wright VecType DMReturnVecType(DM dm) {
1614210c567SJames Wright   VecType vec_type;
1624210c567SJames Wright   DMGetVecType(dm, &vec_type);
1634210c567SJames Wright   return vec_type;
1644210c567SJames Wright }
1654210c567SJames Wright 
1664210c567SJames Wright /**
1674210c567SJames Wright  * @brief Create local PETSc Vecs for CeedOperator's active input/outputs
1684210c567SJames Wright  *
1694210c567SJames Wright  * This is primarily used for when the active input/ouput vector does not correspond to a `DM` object, and thus `DMCreateLocalVector` or
1704210c567SJames Wright  * `DMGetLocalVector` are not applicable.
1714210c567SJames Wright  * For example, if statitics are being store at quadrature points, a `DM`-created `Vec` will not have the
1724210c567SJames Wright  * correct size.
1734210c567SJames Wright  *
1744210c567SJames Wright  * @param[in]  op       Operator to make the Vecs for
175dcdae71aSJames Wright  * @param[in]  vec_type `VecType` for the new Vecs
176dcdae71aSJames Wright  * @param[in]  comm     `MPI_Comm` for the new Vecs
1774210c567SJames Wright  * @param[out] input    Vec for CeedOperator active input
1784210c567SJames Wright  * @param[out] output   Vec for CeedOperator active output
1794210c567SJames Wright  */
CeedOperatorCreateLocalVecs(CeedOperator op,VecType vec_type,MPI_Comm comm,Vec * input,Vec * output)1804210c567SJames Wright PetscErrorCode CeedOperatorCreateLocalVecs(CeedOperator op, VecType vec_type, MPI_Comm comm, Vec *input, Vec *output) {
1814210c567SJames Wright   CeedSize input_size, output_size;
182a424bcd0SJames Wright   Ceed     ceed;
1832788647cSJames Wright   int      comm_size;
1844210c567SJames Wright 
1854210c567SJames Wright   PetscFunctionBeginUser;
186a424bcd0SJames Wright   PetscCall(CeedOperatorGetCeed(op, &ceed));
1872788647cSJames Wright   PetscCallMPI(MPI_Comm_size(comm, &comm_size));
1882788647cSJames Wright   PetscCheck(comm_size == 1, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, "MPI_Comm must be of size 1, recieved comm of size %d", comm_size);
189a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
1904210c567SJames Wright   if (input) {
1914210c567SJames Wright     PetscCall(VecCreate(comm, input));
1924210c567SJames Wright     PetscCall(VecSetType(*input, vec_type));
1934210c567SJames Wright     PetscCall(VecSetSizes(*input, input_size, input_size));
1944210c567SJames Wright   }
1954210c567SJames Wright   if (output) {
1964210c567SJames Wright     PetscCall(VecCreate(comm, output));
1974210c567SJames Wright     PetscCall(VecSetType(*output, vec_type));
1984210c567SJames Wright     PetscCall(VecSetSizes(*output, output_size, output_size));
1994210c567SJames Wright   }
2009bc66399SJeremy L Thompson   PetscCheck(CeedDestroy(&ceed) == CEED_ERROR_SUCCESS, comm, PETSC_ERR_LIB, "Destroying Ceed object failed");
201ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
2024210c567SJames Wright }
2034210c567SJames Wright 
2044021610dSJames Wright /**
2054021610dSJames Wright  * @brief Apply FEM Operator defined by `OperatorApplyContext` to various input and output vectors
2064021610dSJames Wright  *
207f0d883e9SJames Wright  * @param[in]     X             Input global `Vec`, maybe `NULL`
208f0d883e9SJames Wright  * @param[in]     X_loc         Input local `Vec`, maybe `NULL`
209f0d883e9SJames Wright  * @param[in]     x_ceed        Input `CeedVector`, maybe `CEED_VECTOR_NONE`
210f0d883e9SJames Wright  * @param[in,out] y_ceed        Output `CeedVector`, maybe `CEED_VECTOR_NONE`
211f0d883e9SJames Wright  * @param[in,out] Y_loc         Output local `Vec`, maybe `NULL`
212f0d883e9SJames Wright  * @param[in,out] Y             Output global `Vec`, maybe `NULL`
213f0d883e9SJames Wright  * @param[in]     ctx           Context for the operator apply
214f0d883e9SJames Wright  * @param[in]     use_apply_add Whether to use `CeedOperatorApply` or `CeedOperatorApplyAdd`
2154021610dSJames Wright  */
ApplyCeedOperator_Core(Vec X,Vec X_loc,CeedVector x_ceed,CeedVector y_ceed,Vec Y_loc,Vec Y,OperatorApplyContext ctx,bool use_apply_add)2164021610dSJames Wright PetscErrorCode ApplyCeedOperator_Core(Vec X, Vec X_loc, CeedVector x_ceed, CeedVector y_ceed, Vec Y_loc, Vec Y, OperatorApplyContext ctx,
2174021610dSJames Wright                                       bool use_apply_add) {
2184021610dSJames Wright   PetscMemType x_mem_type, y_mem_type;
219a424bcd0SJames Wright   Ceed         ceed = ctx->ceed;
2204021610dSJames Wright 
2214021610dSJames Wright   PetscFunctionBeginUser;
2224021610dSJames Wright   if (X) PetscCall(DMGlobalToLocal(ctx->dm_x, X, INSERT_VALUES, X_loc));
223d0593705SJames Wright   if (X_loc) PetscCall(VecReadPetscToCeed(X_loc, &x_mem_type, x_ceed));
2244021610dSJames Wright 
225d0593705SJames Wright   if (Y_loc) PetscCall(VecPetscToCeed(Y_loc, &y_mem_type, y_ceed));
2264021610dSJames Wright 
22775d1798cSJames Wright   PetscCall(PetscLogEventBegin(FLUIDS_CeedOperatorApply, X, Y, 0, 0));
22875d1798cSJames Wright   PetscCall(PetscLogGpuTimeBegin());
229a424bcd0SJames Wright   if (use_apply_add) PetscCallCeed(ceed, CeedOperatorApplyAdd(ctx->op, x_ceed, y_ceed, CEED_REQUEST_IMMEDIATE));
230a424bcd0SJames Wright   else PetscCallCeed(ceed, CeedOperatorApply(ctx->op, x_ceed, y_ceed, CEED_REQUEST_IMMEDIATE));
23175d1798cSJames Wright   PetscCall(PetscLogGpuTimeEnd());
23275d1798cSJames Wright   PetscCall(PetscLogEventEnd(FLUIDS_CeedOperatorApply, X, Y, 0, 0));
2334021610dSJames Wright 
234d0593705SJames Wright   if (X_loc) PetscCall(VecReadCeedToPetsc(ctx->x_ceed, x_mem_type, X_loc));
2354021610dSJames Wright 
236d0593705SJames Wright   if (Y_loc) PetscCall(VecCeedToPetsc(ctx->y_ceed, y_mem_type, Y_loc));
2374021610dSJames Wright   if (Y) PetscCall(DMLocalToGlobal(ctx->dm_y, Y_loc, ADD_VALUES, Y));
238ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
2394021610dSJames Wright };
2404021610dSJames Wright 
ApplyCeedOperatorGlobalToGlobal(Vec X,Vec Y,OperatorApplyContext ctx)2414021610dSJames Wright PetscErrorCode ApplyCeedOperatorGlobalToGlobal(Vec X, Vec Y, OperatorApplyContext ctx) {
2424021610dSJames Wright   Vec X_loc = ctx->X_loc, Y_loc = ctx->Y_loc;
2434021610dSJames Wright 
2444021610dSJames Wright   PetscFunctionBeginUser;
2454021610dSJames Wright   PetscCall(VecZeroEntries(Y));
2464021610dSJames Wright 
2474021610dSJames Wright   // Get local vectors (if needed)
2484021610dSJames Wright   if (!ctx->X_loc) PetscCall(DMGetLocalVector(ctx->dm_x, &X_loc));
2494021610dSJames Wright   if (!ctx->Y_loc) PetscCall(DMGetLocalVector(ctx->dm_y, &Y_loc));
2504021610dSJames Wright 
2514021610dSJames Wright   PetscCall(ApplyCeedOperator_Core(X, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, Y, ctx, false));
2524021610dSJames Wright 
2534021610dSJames Wright   // Restore local vector (if needed)
2544021610dSJames Wright   if (!ctx->X_loc) PetscCall(DMRestoreLocalVector(ctx->dm_x, &X_loc));
2554021610dSJames Wright   if (!ctx->Y_loc) PetscCall(DMRestoreLocalVector(ctx->dm_y, &Y_loc));
256ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
2574021610dSJames Wright }
2584021610dSJames Wright 
ApplyCeedOperatorLocalToGlobal(Vec X_loc,Vec Y,OperatorApplyContext ctx)2594021610dSJames Wright PetscErrorCode ApplyCeedOperatorLocalToGlobal(Vec X_loc, Vec Y, OperatorApplyContext ctx) {
2604021610dSJames Wright   Vec Y_loc = ctx->Y_loc;
2614021610dSJames Wright 
2624021610dSJames Wright   PetscFunctionBeginUser;
2634021610dSJames Wright   PetscCall(VecZeroEntries(Y));
2644021610dSJames Wright 
2654021610dSJames Wright   // Get local vectors (if needed)
2664021610dSJames Wright   if (!ctx->Y_loc) PetscCall(DMGetLocalVector(ctx->dm_y, &Y_loc));
2674021610dSJames Wright 
2684021610dSJames Wright   PetscCall(ApplyCeedOperator_Core(NULL, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, Y, ctx, false));
2694021610dSJames Wright 
2704021610dSJames Wright   // Restore local vectors (if needed)
2714021610dSJames Wright   if (!ctx->Y_loc) PetscCall(DMRestoreLocalVector(ctx->dm_y, &Y_loc));
272ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
2734021610dSJames Wright }
2744021610dSJames Wright 
ApplyCeedOperatorGlobalToLocal(Vec X,Vec Y_loc,OperatorApplyContext ctx)2754021610dSJames Wright PetscErrorCode ApplyCeedOperatorGlobalToLocal(Vec X, Vec Y_loc, OperatorApplyContext ctx) {
2764021610dSJames Wright   Vec X_loc = ctx->X_loc;
2774021610dSJames Wright 
2784021610dSJames Wright   PetscFunctionBeginUser;
2794021610dSJames Wright   // Get local vectors (if needed)
2804021610dSJames Wright   if (!ctx->X_loc) PetscCall(DMGetLocalVector(ctx->dm_x, &X_loc));
2814021610dSJames Wright 
2824021610dSJames Wright   PetscCall(ApplyCeedOperator_Core(X, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, NULL, ctx, false));
2834021610dSJames Wright 
2844021610dSJames Wright   // Restore local vector (if needed)
2854021610dSJames Wright   if (!ctx->X_loc) PetscCall(DMRestoreLocalVector(ctx->dm_x, &X_loc));
286ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
2874021610dSJames Wright }
2884021610dSJames Wright 
ApplyCeedOperatorLocalToLocal(Vec X_loc,Vec Y_loc,OperatorApplyContext ctx)28993567eeaSJames Wright PetscErrorCode ApplyCeedOperatorLocalToLocal(Vec X_loc, Vec Y_loc, OperatorApplyContext ctx) {
29093567eeaSJames Wright   PetscFunctionBeginUser;
29193567eeaSJames Wright   PetscCall(ApplyCeedOperator_Core(NULL, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, NULL, ctx, false));
292ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
29393567eeaSJames Wright }
29493567eeaSJames Wright 
ApplyAddCeedOperatorLocalToLocal(Vec X_loc,Vec Y_loc,OperatorApplyContext ctx)2954021610dSJames Wright PetscErrorCode ApplyAddCeedOperatorLocalToLocal(Vec X_loc, Vec Y_loc, OperatorApplyContext ctx) {
2964021610dSJames Wright   PetscFunctionBeginUser;
2974021610dSJames Wright   PetscCall(ApplyCeedOperator_Core(NULL, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, NULL, ctx, true));
298ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
2994021610dSJames Wright }
3004021610dSJames Wright 
3017f2a9303SJames Wright /**
3027f2a9303SJames Wright  * @brief Return Mats for KSP solve
3037f2a9303SJames Wright  *
3047f2a9303SJames Wright  * Uses command-line flag with `ksp`'s prefix to determine if mat_ceed should be used directly or whether it should be assembled.
3057f2a9303SJames Wright  * If `Amat` is to be assembled, then `Pmat` is equal to `Amat`.
3067f2a9303SJames Wright  *
3077f2a9303SJames Wright  * If `Amat` uses `mat_ceed`, then `Pmat` is either assembled or uses `mat_ceed` based on the preconditioner choice in `ksp`.
3087f2a9303SJames Wright  *
3097f2a9303SJames Wright  * @param[in]  ksp      `KSP` object for used for solving
3107f2a9303SJames Wright  * @param[in]  mat_ceed `MATCEED` for the linear operator
3117f2a9303SJames Wright  * @param[in]  assemble Whether to assemble `Amat` and `Pmat` if they are not `mat_ceed`
3127f2a9303SJames Wright  * @param[out] Amat     `Mat` to be used for the solver `Amat`
3137f2a9303SJames Wright  * @param[out] Pmat     `Mat` to be used for the solver `Pmat`
3147f2a9303SJames Wright  */
CreateSolveOperatorsFromMatCeed(KSP ksp,Mat mat_ceed,PetscBool assemble,Mat * Amat,Mat * Pmat)3157f2a9303SJames Wright PetscErrorCode CreateSolveOperatorsFromMatCeed(KSP ksp, Mat mat_ceed, PetscBool assemble, Mat *Amat, Mat *Pmat) {
3167f2a9303SJames Wright   PetscBool use_matceed_pmat, assemble_amat = PETSC_FALSE;
3177f2a9303SJames Wright 
3187f2a9303SJames Wright   PetscFunctionBeginUser;
3197f2a9303SJames Wright   {  // Determine if Amat should be MATCEED or assembled
3207f2a9303SJames Wright     const char *ksp_prefix = NULL;
3217f2a9303SJames Wright 
3227f2a9303SJames Wright     PetscCall(KSPGetOptionsPrefix(ksp, &ksp_prefix));
3237f2a9303SJames Wright     PetscOptionsBegin(PetscObjectComm((PetscObject)mat_ceed), ksp_prefix, "", NULL);
3247f2a9303SJames Wright     PetscCall(PetscOptionsBool("-matceed_assemble_amat", "Assemble the A matrix for KSP solve", NULL, assemble_amat, &assemble_amat, NULL));
3257f2a9303SJames Wright     PetscOptionsEnd();
3267f2a9303SJames Wright   }
3277f2a9303SJames Wright 
3287f2a9303SJames Wright   if (assemble_amat) {
329a94b429fSJames Wright     PetscCall(MatCeedCreateMatCOO(mat_ceed, Amat));
3307f2a9303SJames Wright     if (assemble) PetscCall(MatCeedAssembleCOO(mat_ceed, *Amat));
3317f2a9303SJames Wright 
3327f2a9303SJames Wright     PetscCall(PetscObjectReference((PetscObject)*Amat));
3337f2a9303SJames Wright     *Pmat = *Amat;
3347f2a9303SJames Wright     PetscFunctionReturn(PETSC_SUCCESS);
3357f2a9303SJames Wright   } else {
3367f2a9303SJames Wright     PetscCall(PetscObjectReference((PetscObject)mat_ceed));
3377f2a9303SJames Wright     *Amat = mat_ceed;
3387f2a9303SJames Wright   }
3397f2a9303SJames Wright 
3407f2a9303SJames Wright   {  // Determine if Pmat should be MATCEED or assembled
3417f2a9303SJames Wright     PC     pc;
3427f2a9303SJames Wright     PCType pc_type;
3437f2a9303SJames Wright 
3447f2a9303SJames Wright     PetscCall(KSPGetPC(ksp, &pc));
3457f2a9303SJames Wright     PetscCall(PCGetType(pc, &pc_type));
346b32e1e4aSJames Wright     PetscCall(PetscStrcmpAny(pc_type, &use_matceed_pmat, PCNONE, PCJACOBI, PCVPBJACOBI, PCPBJACOBI, ""));
3477f2a9303SJames Wright   }
3487f2a9303SJames Wright 
3497f2a9303SJames Wright   if (use_matceed_pmat) {
3507f2a9303SJames Wright     PetscCall(PetscObjectReference((PetscObject)mat_ceed));
3517f2a9303SJames Wright     *Pmat = mat_ceed;
3527f2a9303SJames Wright   } else {
353a94b429fSJames Wright     PetscCall(MatCeedCreateMatCOO(mat_ceed, Pmat));
3547f2a9303SJames Wright     if (assemble) PetscCall(MatCeedAssembleCOO(mat_ceed, *Pmat));
3557f2a9303SJames Wright   }
3567f2a9303SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
3577f2a9303SJames Wright }
3587f2a9303SJames Wright 
3597f2a9303SJames Wright /**
3607f2a9303SJames Wright  * @brief Runs KSPSetFromOptions and sets Operators based on mat_ceed
3617f2a9303SJames Wright  *
3627f2a9303SJames Wright  * See CreateSolveOperatorsFromMatCeed for details on how the KSPSolve operators are set.
3637f2a9303SJames Wright  *
3647f2a9303SJames Wright  * @param[in] ksp      `KSP` of the solve
3657f2a9303SJames Wright  * @param[in] mat_ceed `MatCeed` linear operator to solve for
3667f2a9303SJames Wright  */
KSPSetFromOptions_WithMatCeed(KSP ksp,Mat mat_ceed)3677f2a9303SJames Wright PetscErrorCode KSPSetFromOptions_WithMatCeed(KSP ksp, Mat mat_ceed) {
3687f2a9303SJames Wright   Mat Amat, Pmat;
3697f2a9303SJames Wright 
3707f2a9303SJames Wright   PetscFunctionBeginUser;
3717f2a9303SJames Wright   PetscCall(KSPSetFromOptions(ksp));
3727f2a9303SJames Wright   PetscCall(CreateSolveOperatorsFromMatCeed(ksp, mat_ceed, PETSC_TRUE, &Amat, &Pmat));
3737f2a9303SJames Wright   PetscCall(KSPSetOperators(ksp, Amat, Pmat));
3747f2a9303SJames Wright   PetscCall(MatDestroy(&Amat));
3757f2a9303SJames Wright   PetscCall(MatDestroy(&Pmat));
3767f2a9303SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
3777f2a9303SJames Wright }
3784021610dSJames Wright // -----------------------------------------------------------------------------
379