xref: /libCEED/interface/ceed-preconditioning.c (revision 75f0d5a43978d294e569c8e99a70cefd4b888bc4)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3eaf62fffSJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5eaf62fffSJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7eaf62fffSJeremy L Thompson 
8ed9e99e6SJeremy L Thompson #include <assert.h>
92b730f8bSJeremy L Thompson #include <ceed-impl.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
112b730f8bSJeremy L Thompson #include <ceed/ceed.h>
122b730f8bSJeremy L Thompson #include <math.h>
13eaf62fffSJeremy L Thompson #include <stdbool.h>
14eaf62fffSJeremy L Thompson #include <stdio.h>
15eaf62fffSJeremy L Thompson #include <string.h>
16eaf62fffSJeremy L Thompson 
17eaf62fffSJeremy L Thompson /// @file
18eaf62fffSJeremy L Thompson /// Implementation of CeedOperator preconditioning interfaces
19eaf62fffSJeremy L Thompson 
20eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
21eaf62fffSJeremy L Thompson /// CeedOperator Library Internal Preconditioning Functions
22eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
23eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorDeveloper
24eaf62fffSJeremy L Thompson /// @{
25eaf62fffSJeremy L Thompson 
26eaf62fffSJeremy L Thompson /**
27ea61e9acSJeremy L Thompson   @brief Duplicate a CeedQFunction with a reference Ceed to fallback for advanced CeedOperator functionality
289e77b9c8SJeremy L Thompson 
2901ea9c81SJed Brown   @param[in]  fallback_ceed Ceed on which to create fallback CeedQFunction
309e77b9c8SJeremy L Thompson   @param[in]  qf            CeedQFunction to create fallback for
3101ea9c81SJed Brown   @param[out] qf_fallback   fallback CeedQFunction
329e77b9c8SJeremy L Thompson 
339e77b9c8SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
349e77b9c8SJeremy L Thompson 
359e77b9c8SJeremy L Thompson   @ref Developer
369e77b9c8SJeremy L Thompson **/
372b730f8bSJeremy L Thompson static int CeedQFunctionCreateFallback(Ceed fallback_ceed, CeedQFunction qf, CeedQFunction *qf_fallback) {
389e77b9c8SJeremy L Thompson   // Check if NULL qf passed in
399e77b9c8SJeremy L Thompson   if (!qf) return CEED_ERROR_SUCCESS;
409e77b9c8SJeremy L Thompson 
41d04bbc78SJeremy L Thompson   CeedDebug256(qf->ceed, 1, "---------- CeedOperator Fallback ----------\n");
4213f886e9SJeremy L Thompson   CeedDebug(qf->ceed, "Creating fallback CeedQFunction\n");
43d04bbc78SJeremy L Thompson 
449e77b9c8SJeremy L Thompson   char *source_path_with_name = "";
459e77b9c8SJeremy L Thompson   if (qf->source_path) {
462b730f8bSJeremy L Thompson     size_t path_len = strlen(qf->source_path), name_len = strlen(qf->kernel_name);
472b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(path_len + name_len + 2, &source_path_with_name));
489e77b9c8SJeremy L Thompson     memcpy(source_path_with_name, qf->source_path, path_len);
499e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len], ":", 1);
509e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len + 1], qf->kernel_name, name_len);
519e77b9c8SJeremy L Thompson   } else {
522b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &source_path_with_name));
539e77b9c8SJeremy L Thompson   }
549e77b9c8SJeremy L Thompson 
552b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(fallback_ceed, qf->vec_length, qf->function, source_path_with_name, qf_fallback));
569e77b9c8SJeremy L Thompson   {
579e77b9c8SJeremy L Thompson     CeedQFunctionContext ctx;
589e77b9c8SJeremy L Thompson 
592b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
602b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx));
619e77b9c8SJeremy L Thompson   }
629e77b9c8SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
632b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(*qf_fallback, qf->input_fields[i]->field_name, qf->input_fields[i]->size, qf->input_fields[i]->eval_mode));
649e77b9c8SJeremy L Thompson   }
659e77b9c8SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
662b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(*qf_fallback, qf->output_fields[i]->field_name, qf->output_fields[i]->size, qf->output_fields[i]->eval_mode));
679e77b9c8SJeremy L Thompson   }
682b730f8bSJeremy L Thompson   CeedCall(CeedFree(&source_path_with_name));
699e77b9c8SJeremy L Thompson 
709e77b9c8SJeremy L Thompson   return CEED_ERROR_SUCCESS;
719e77b9c8SJeremy L Thompson }
729e77b9c8SJeremy L Thompson 
739e77b9c8SJeremy L Thompson /**
74ea61e9acSJeremy L Thompson   @brief Duplicate a CeedOperator with a reference Ceed to fallback for advanced CeedOperator functionality
75eaf62fffSJeremy L Thompson 
76ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to create fallback for
77eaf62fffSJeremy L Thompson 
78eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
79eaf62fffSJeremy L Thompson 
80eaf62fffSJeremy L Thompson   @ref Developer
81eaf62fffSJeremy L Thompson **/
82d04bbc78SJeremy L Thompson static int CeedOperatorCreateFallback(CeedOperator op) {
839e77b9c8SJeremy L Thompson   Ceed ceed_fallback;
84eaf62fffSJeremy L Thompson 
85805fe78eSJeremy L Thompson   // Check not already created
86805fe78eSJeremy L Thompson   if (op->op_fallback) return CEED_ERROR_SUCCESS;
87805fe78eSJeremy L Thompson 
88eaf62fffSJeremy L Thompson   // Fallback Ceed
892b730f8bSJeremy L Thompson   CeedCall(CeedGetOperatorFallbackCeed(op->ceed, &ceed_fallback));
90d04bbc78SJeremy L Thompson   if (!ceed_fallback) return CEED_ERROR_SUCCESS;
91d04bbc78SJeremy L Thompson 
92d04bbc78SJeremy L Thompson   CeedDebug256(op->ceed, 1, "---------- CeedOperator Fallback ----------\n");
9313f886e9SJeremy L Thompson   CeedDebug(op->ceed, "Creating fallback CeedOperator\n");
94eaf62fffSJeremy L Thompson 
95eaf62fffSJeremy L Thompson   // Clone Op
96805fe78eSJeremy L Thompson   CeedOperator op_fallback;
97805fe78eSJeremy L Thompson   if (op->is_composite) {
982b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorCreate(ceed_fallback, &op_fallback));
99805fe78eSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
100d04bbc78SJeremy L Thompson       CeedOperator op_sub_fallback;
101d04bbc78SJeremy L Thompson 
1022b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFallback(op->sub_operators[i], &op_sub_fallback));
1032b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorAddSub(op_fallback, op_sub_fallback));
104805fe78eSJeremy L Thompson     }
105805fe78eSJeremy L Thompson   } else {
1069e77b9c8SJeremy L Thompson     CeedQFunction qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL;
1072b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback));
1082b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback));
1092b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback));
1102b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback));
111805fe78eSJeremy L Thompson     for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
1122b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, op->input_fields[i]->field_name, op->input_fields[i]->elem_restr, op->input_fields[i]->basis,
1132b730f8bSJeremy L Thompson                                     op->input_fields[i]->vec));
114805fe78eSJeremy L Thompson     }
115805fe78eSJeremy L Thompson     for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
1162b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, op->output_fields[i]->field_name, op->output_fields[i]->elem_restr, op->output_fields[i]->basis,
1172b730f8bSJeremy L Thompson                                     op->output_fields[i]->vec));
118805fe78eSJeremy L Thompson     }
1192b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op->qf_assembled, &op_fallback->qf_assembled));
120805fe78eSJeremy L Thompson     if (op_fallback->num_qpts == 0) {
1212b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetNumQuadraturePoints(op_fallback, op->num_qpts));
122805fe78eSJeremy L Thompson     }
1239e77b9c8SJeremy L Thompson     // Cleanup
1242b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf_fallback));
1252b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqf_fallback));
1262b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqfT_fallback));
127805fe78eSJeremy L Thompson   }
1282b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetName(op_fallback, op->name));
1292b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fallback));
130805fe78eSJeremy L Thompson   op->op_fallback = op_fallback;
131eaf62fffSJeremy L Thompson 
132eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
133eaf62fffSJeremy L Thompson }
134eaf62fffSJeremy L Thompson 
135eaf62fffSJeremy L Thompson /**
136ea61e9acSJeremy L Thompson   @brief Retrieve fallback CeedOperator with a reference Ceed for advanced CeedOperator functionality
137d04bbc78SJeremy L Thompson 
138d04bbc78SJeremy L Thompson   @param[in]  op          CeedOperator to retrieve fallback for
139d04bbc78SJeremy L Thompson   @param[out] op_fallback Fallback CeedOperator
140d04bbc78SJeremy L Thompson 
141d04bbc78SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
142d04bbc78SJeremy L Thompson 
143d04bbc78SJeremy L Thompson   @ref Developer
144d04bbc78SJeremy L Thompson **/
145d04bbc78SJeremy L Thompson int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) {
146d04bbc78SJeremy L Thompson   // Create if needed
147d04bbc78SJeremy L Thompson   if (!op->op_fallback) {
1482b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreateFallback(op));
149d04bbc78SJeremy L Thompson   }
150d04bbc78SJeremy L Thompson   if (op->op_fallback) {
151d04bbc78SJeremy L Thompson     bool is_debug;
152d04bbc78SJeremy L Thompson 
1532b730f8bSJeremy L Thompson     CeedCall(CeedIsDebug(op->ceed, &is_debug));
154d04bbc78SJeremy L Thompson     if (is_debug) {
155d04bbc78SJeremy L Thompson       Ceed        ceed_fallback;
156d04bbc78SJeremy L Thompson       const char *resource, *resource_fallback;
157d04bbc78SJeremy L Thompson 
1582b730f8bSJeremy L Thompson       CeedCall(CeedGetOperatorFallbackCeed(op->ceed, &ceed_fallback));
1592b730f8bSJeremy L Thompson       CeedCall(CeedGetResource(op->ceed, &resource));
1602b730f8bSJeremy L Thompson       CeedCall(CeedGetResource(ceed_fallback, &resource_fallback));
161d04bbc78SJeremy L Thompson 
162d04bbc78SJeremy L Thompson       CeedDebug256(op->ceed, 1, "---------- CeedOperator Fallback ----------\n");
1632b730f8bSJeremy L Thompson       CeedDebug(op->ceed, "Falling back from %s operator at address %ld to %s operator at address %ld\n", resource, op, resource_fallback,
1642b730f8bSJeremy L Thompson                 op->op_fallback);
165d04bbc78SJeremy L Thompson     }
166d04bbc78SJeremy L Thompson   }
167d04bbc78SJeremy L Thompson   *op_fallback = op->op_fallback;
168d04bbc78SJeremy L Thompson 
169d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
170d04bbc78SJeremy L Thompson }
171d04bbc78SJeremy L Thompson 
172d04bbc78SJeremy L Thompson /**
173eaf62fffSJeremy L Thompson   @brief Select correct basis matrix pointer based on CeedEvalMode
174eaf62fffSJeremy L Thompson 
175eaf62fffSJeremy L Thompson   @param[in]  eval_mode Current basis evaluation mode
176eaf62fffSJeremy L Thompson   @param[in]  identity  Pointer to identity matrix
177eaf62fffSJeremy L Thompson   @param[in]  interp    Pointer to interpolation matrix
178eaf62fffSJeremy L Thompson   @param[in]  grad      Pointer to gradient matrix
179eaf62fffSJeremy L Thompson   @param[out] basis_ptr Basis pointer to set
180eaf62fffSJeremy L Thompson 
181eaf62fffSJeremy L Thompson   @ref Developer
182eaf62fffSJeremy L Thompson **/
1832b730f8bSJeremy L Thompson static inline void CeedOperatorGetBasisPointer(CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar *interp, const CeedScalar *grad,
1842b730f8bSJeremy L Thompson                                                const CeedScalar **basis_ptr) {
185eaf62fffSJeremy L Thompson   switch (eval_mode) {
186eaf62fffSJeremy L Thompson     case CEED_EVAL_NONE:
187eaf62fffSJeremy L Thompson       *basis_ptr = identity;
188eaf62fffSJeremy L Thompson       break;
189eaf62fffSJeremy L Thompson     case CEED_EVAL_INTERP:
190eaf62fffSJeremy L Thompson       *basis_ptr = interp;
191eaf62fffSJeremy L Thompson       break;
192eaf62fffSJeremy L Thompson     case CEED_EVAL_GRAD:
193eaf62fffSJeremy L Thompson       *basis_ptr = grad;
194eaf62fffSJeremy L Thompson       break;
195eaf62fffSJeremy L Thompson     case CEED_EVAL_WEIGHT:
196eaf62fffSJeremy L Thompson     case CEED_EVAL_DIV:
197eaf62fffSJeremy L Thompson     case CEED_EVAL_CURL:
198eaf62fffSJeremy L Thompson       break;  // Caught by QF Assembly
199eaf62fffSJeremy L Thompson   }
200ed9e99e6SJeremy L Thompson   assert(*basis_ptr != NULL);
201eaf62fffSJeremy L Thompson }
202eaf62fffSJeremy L Thompson 
203eaf62fffSJeremy L Thompson /**
204eaf62fffSJeremy L Thompson   @brief Create point block restriction for active operator field
205eaf62fffSJeremy L Thompson 
206eaf62fffSJeremy L Thompson   @param[in]  rstr            Original CeedElemRestriction for active field
207ea61e9acSJeremy L Thompson   @param[out] pointblock_rstr Address of the variable where the newly created CeedElemRestriction will be stored
208eaf62fffSJeremy L Thompson 
209eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
210eaf62fffSJeremy L Thompson 
211eaf62fffSJeremy L Thompson   @ref Developer
212eaf62fffSJeremy L Thompson **/
2132b730f8bSJeremy L Thompson static int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *pointblock_rstr) {
214eaf62fffSJeremy L Thompson   Ceed ceed;
2152b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
216eaf62fffSJeremy L Thompson   const CeedInt *offsets;
2172b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
218eaf62fffSJeremy L Thompson 
219eaf62fffSJeremy L Thompson   // Expand offsets
2207b63f5c6SJed Brown   CeedInt  num_elem, num_comp, elem_size, comp_stride, *pointblock_offsets;
2217b63f5c6SJed Brown   CeedSize l_size;
2222b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
2232b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
2242b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
2252b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
2262b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
227eaf62fffSJeremy L Thompson   CeedInt shift = num_comp;
2282b730f8bSJeremy L Thompson   if (comp_stride != 1) shift *= num_comp;
2292b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_elem * elem_size, &pointblock_offsets));
230eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_elem * elem_size; i++) {
231eaf62fffSJeremy L Thompson     pointblock_offsets[i] = offsets[i] * shift;
232eaf62fffSJeremy L Thompson   }
233eaf62fffSJeremy L Thompson 
234eaf62fffSJeremy L Thompson   // Create new restriction
2352b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
2362b730f8bSJeremy L Thompson                                      pointblock_offsets, pointblock_rstr));
237eaf62fffSJeremy L Thompson 
238eaf62fffSJeremy L Thompson   // Cleanup
2392b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
240eaf62fffSJeremy L Thompson 
241eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
242eaf62fffSJeremy L Thompson }
243eaf62fffSJeremy L Thompson 
244eaf62fffSJeremy L Thompson /**
245eaf62fffSJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
246eaf62fffSJeremy L Thompson 
247eaf62fffSJeremy L Thompson   @param[in]  op            CeedOperator to assemble point block diagonal
248ea61e9acSJeremy L Thompson   @param[in]  request       Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE
249eaf62fffSJeremy L Thompson   @param[in]  is_pointblock Boolean flag to assemble diagonal or point block diagonal
250eaf62fffSJeremy L Thompson   @param[out] assembled     CeedVector to store assembled diagonal
251eaf62fffSJeremy L Thompson 
252eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
253eaf62fffSJeremy L Thompson 
254eaf62fffSJeremy L Thompson   @ref Developer
255eaf62fffSJeremy L Thompson **/
2562b730f8bSJeremy L Thompson static inline int CeedSingleOperatorAssembleAddDiagonal_Core(CeedOperator op, CeedRequest *request, const bool is_pointblock, CeedVector assembled) {
257eaf62fffSJeremy L Thompson   Ceed ceed;
2582b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
259eaf62fffSJeremy L Thompson 
260eaf62fffSJeremy L Thompson   // Assemble QFunction
261eaf62fffSJeremy L Thompson   CeedQFunction qf;
2622b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
263eaf62fffSJeremy L Thompson   CeedInt num_input_fields, num_output_fields;
2642b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
265eaf62fffSJeremy L Thompson   CeedVector          assembled_qf;
266eaf62fffSJeremy L Thompson   CeedElemRestriction rstr;
2672b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr, request));
268eaf62fffSJeremy L Thompson   CeedInt layout[3];
2692b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr, &layout));
2702b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr));
271eaf62fffSJeremy L Thompson 
272ed9e99e6SJeremy L Thompson   // Get assembly data
273ed9e99e6SJeremy L Thompson   CeedOperatorAssemblyData data;
2742b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
275ed9e99e6SJeremy L Thompson   const CeedEvalMode *eval_mode_in, *eval_mode_out;
276ed9e99e6SJeremy L Thompson   CeedInt             num_eval_mode_in, num_eval_mode_out;
2772b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_eval_mode_in, &eval_mode_in, &num_eval_mode_out, &eval_mode_out));
278ed9e99e6SJeremy L Thompson   CeedBasis basis_in, basis_out;
2792b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, &basis_in, NULL, &basis_out, NULL));
280ed9e99e6SJeremy L Thompson   CeedInt num_comp;
2812b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp));
282eaf62fffSJeremy L Thompson 
283eaf62fffSJeremy L Thompson   // Assemble point block diagonal restriction, if needed
284ed9e99e6SJeremy L Thompson   CeedElemRestriction diag_rstr;
2852b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &diag_rstr));
286eaf62fffSJeremy L Thompson   if (is_pointblock) {
287ed9e99e6SJeremy L Thompson     CeedElemRestriction point_block_rstr;
2882b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreateActivePointBlockRestriction(diag_rstr, &point_block_rstr));
289ed9e99e6SJeremy L Thompson     diag_rstr = point_block_rstr;
290eaf62fffSJeremy L Thompson   }
291eaf62fffSJeremy L Thompson 
292eaf62fffSJeremy L Thompson   // Create diagonal vector
293eaf62fffSJeremy L Thompson   CeedVector elem_diag;
2942b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreateVector(diag_rstr, NULL, &elem_diag));
295eaf62fffSJeremy L Thompson 
296eaf62fffSJeremy L Thompson   // Assemble element operator diagonals
2979c774eddSJeremy L Thompson   CeedScalar       *elem_diag_array;
2989c774eddSJeremy L Thompson   const CeedScalar *assembled_qf_array;
2992b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(elem_diag, 0.0));
3002b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array));
3012b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
302eaf62fffSJeremy L Thompson   CeedInt num_elem, num_nodes, num_qpts;
3032b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
3042b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis_in, &num_nodes));
3052b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
306ed9e99e6SJeremy L Thompson 
307eaf62fffSJeremy L Thompson   // Basis matrices
308eaf62fffSJeremy L Thompson   const CeedScalar *interp_in, *interp_out, *grad_in, *grad_out;
309eaf62fffSJeremy L Thompson   CeedScalar       *identity      = NULL;
310ed9e99e6SJeremy L Thompson   bool              has_eval_none = false;
311ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_eval_mode_in; i++) {
312ed9e99e6SJeremy L Thompson     has_eval_none = has_eval_none || (eval_mode_in[i] == CEED_EVAL_NONE);
313ed9e99e6SJeremy L Thompson   }
314ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_eval_mode_out; i++) {
315ed9e99e6SJeremy L Thompson     has_eval_none = has_eval_none || (eval_mode_out[i] == CEED_EVAL_NONE);
316ed9e99e6SJeremy L Thompson   }
317ed9e99e6SJeremy L Thompson   if (has_eval_none) {
3182b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
3192b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
320eaf62fffSJeremy L Thompson   }
3212b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp(basis_in, &interp_in));
3222b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp(basis_out, &interp_out));
3232b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad(basis_in, &grad_in));
3242b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad(basis_out, &grad_out));
325eaf62fffSJeremy L Thompson   // Compute the diagonal of B^T D B
326eaf62fffSJeremy L Thompson   // Each element
327eaf62fffSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
328eaf62fffSJeremy L Thompson     CeedInt d_out = -1;
329eaf62fffSJeremy L Thompson     // Each basis eval mode pair
330eaf62fffSJeremy L Thompson     for (CeedInt e_out = 0; e_out < num_eval_mode_out; e_out++) {
331eaf62fffSJeremy L Thompson       const CeedScalar *bt = NULL;
3322b730f8bSJeremy L Thompson       if (eval_mode_out[e_out] == CEED_EVAL_GRAD) d_out += 1;
3332b730f8bSJeremy L Thompson       CeedOperatorGetBasisPointer(eval_mode_out[e_out], identity, interp_out, &grad_out[d_out * num_qpts * num_nodes], &bt);
334eaf62fffSJeremy L Thompson       CeedInt d_in = -1;
335eaf62fffSJeremy L Thompson       for (CeedInt e_in = 0; e_in < num_eval_mode_in; e_in++) {
336eaf62fffSJeremy L Thompson         const CeedScalar *b = NULL;
3372b730f8bSJeremy L Thompson         if (eval_mode_in[e_in] == CEED_EVAL_GRAD) d_in += 1;
3382b730f8bSJeremy L Thompson         CeedOperatorGetBasisPointer(eval_mode_in[e_in], identity, interp_in, &grad_in[d_in * num_qpts * num_nodes], &b);
339eaf62fffSJeremy L Thompson         // Each component
3402b730f8bSJeremy L Thompson         for (CeedInt c_out = 0; c_out < num_comp; c_out++) {
341eaf62fffSJeremy L Thompson           // Each qpoint/node pair
3422b730f8bSJeremy L Thompson           for (CeedInt q = 0; q < num_qpts; q++) {
343eaf62fffSJeremy L Thompson             if (is_pointblock) {
344eaf62fffSJeremy L Thompson               // Point Block Diagonal
345eaf62fffSJeremy L Thompson               for (CeedInt c_in = 0; c_in < num_comp; c_in++) {
346eaf62fffSJeremy L Thompson                 const CeedScalar qf_value =
3472b730f8bSJeremy L Thompson                     assembled_qf_array[q * layout[0] + (((e_in * num_comp + c_in) * num_eval_mode_out + e_out) * num_comp + c_out) * layout[1] +
3482b730f8bSJeremy L Thompson                                        e * layout[2]];
3492b730f8bSJeremy L Thompson                 for (CeedInt n = 0; n < num_nodes; n++) {
350eaf62fffSJeremy L Thompson                   elem_diag_array[((e * num_comp + c_out) * num_comp + c_in) * num_nodes + n] +=
351eaf62fffSJeremy L Thompson                       bt[q * num_nodes + n] * qf_value * b[q * num_nodes + n];
352eaf62fffSJeremy L Thompson                 }
3532b730f8bSJeremy L Thompson               }
354eaf62fffSJeremy L Thompson             } else {
355eaf62fffSJeremy L Thompson               // Diagonal Only
356eaf62fffSJeremy L Thompson               const CeedScalar qf_value =
3572b730f8bSJeremy L Thompson                   assembled_qf_array[q * layout[0] + (((e_in * num_comp + c_out) * num_eval_mode_out + e_out) * num_comp + c_out) * layout[1] +
3582b730f8bSJeremy L Thompson                                      e * layout[2]];
3592b730f8bSJeremy L Thompson               for (CeedInt n = 0; n < num_nodes; n++) {
3602b730f8bSJeremy L Thompson                 elem_diag_array[(e * num_comp + c_out) * num_nodes + n] += bt[q * num_nodes + n] * qf_value * b[q * num_nodes + n];
361eaf62fffSJeremy L Thompson               }
362eaf62fffSJeremy L Thompson             }
363eaf62fffSJeremy L Thompson           }
364eaf62fffSJeremy L Thompson         }
3652b730f8bSJeremy L Thompson       }
3662b730f8bSJeremy L Thompson     }
3672b730f8bSJeremy L Thompson   }
3682b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
3692b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
370eaf62fffSJeremy L Thompson 
371eaf62fffSJeremy L Thompson   // Assemble local operator diagonal
3722b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
373eaf62fffSJeremy L Thompson 
374eaf62fffSJeremy L Thompson   // Cleanup
375eaf62fffSJeremy L Thompson   if (is_pointblock) {
3762b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&diag_rstr));
377eaf62fffSJeremy L Thompson   }
3782b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
3792b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&elem_diag));
3802b730f8bSJeremy L Thompson   CeedCall(CeedFree(&identity));
381eaf62fffSJeremy L Thompson 
382eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
383eaf62fffSJeremy L Thompson }
384eaf62fffSJeremy L Thompson 
385eaf62fffSJeremy L Thompson /**
386eaf62fffSJeremy L Thompson   @brief Core logic for assembling composite operator diagonal
387eaf62fffSJeremy L Thompson 
388eaf62fffSJeremy L Thompson   @param[in]  op            CeedOperator to assemble point block diagonal
389ea61e9acSJeremy L Thompson   @param[in]  request       Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE
390eaf62fffSJeremy L Thompson   @param[in]  is_pointblock Boolean flag to assemble diagonal or point block diagonal
391eaf62fffSJeremy L Thompson   @param[out] assembled     CeedVector to store assembled diagonal
392eaf62fffSJeremy L Thompson 
393eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
394eaf62fffSJeremy L Thompson 
395eaf62fffSJeremy L Thompson   @ref Developer
396eaf62fffSJeremy L Thompson **/
3972b730f8bSJeremy L Thompson static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_pointblock,
398eaf62fffSJeremy L Thompson                                                                  CeedVector assembled) {
399eaf62fffSJeremy L Thompson   CeedInt       num_sub;
400eaf62fffSJeremy L Thompson   CeedOperator *suboperators;
4012b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumSub(op, &num_sub));
4022b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetSubList(op, &suboperators));
403eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_sub; i++) {
4046aa95790SJeremy L Thompson     if (is_pointblock) {
4052b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request));
4066aa95790SJeremy L Thompson     } else {
4072b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request));
4086aa95790SJeremy L Thompson     }
409eaf62fffSJeremy L Thompson   }
410eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
411eaf62fffSJeremy L Thompson }
412eaf62fffSJeremy L Thompson 
413eaf62fffSJeremy L Thompson /**
414eaf62fffSJeremy L Thompson   @brief Build nonzero pattern for non-composite operator
415eaf62fffSJeremy L Thompson 
416eaf62fffSJeremy L Thompson   Users should generally use CeedOperatorLinearAssembleSymbolic()
417eaf62fffSJeremy L Thompson 
418eaf62fffSJeremy L Thompson   @param[in]  op     CeedOperator to assemble nonzero pattern
419eaf62fffSJeremy L Thompson   @param[in]  offset Offset for number of entries
420eaf62fffSJeremy L Thompson   @param[out] rows   Row number for each entry
421eaf62fffSJeremy L Thompson   @param[out] cols   Column number for each entry
422eaf62fffSJeremy L Thompson 
423eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
424eaf62fffSJeremy L Thompson 
425eaf62fffSJeremy L Thompson   @ref Developer
426eaf62fffSJeremy L Thompson **/
4272b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) {
428eaf62fffSJeremy L Thompson   Ceed ceed = op->ceed;
4292b730f8bSJeremy L Thompson   if (op->is_composite) {
430eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
4312b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
432eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
4332b730f8bSJeremy L Thompson   }
434eaf62fffSJeremy L Thompson 
435c9366a6bSJeremy L Thompson   CeedSize num_nodes;
4362b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes, NULL));
437eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_in;
4382b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &rstr_in));
439e79b91d9SJeremy L Thompson   CeedInt num_elem, elem_size, num_comp;
4402b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem));
4412b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size));
4422b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp));
443eaf62fffSJeremy L Thompson   CeedInt layout_er[3];
4442b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_in, &layout_er));
445eaf62fffSJeremy L Thompson 
446eaf62fffSJeremy L Thompson   CeedInt local_num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
447eaf62fffSJeremy L Thompson 
448eaf62fffSJeremy L Thompson   // Determine elem_dof relation
449eaf62fffSJeremy L Thompson   CeedVector index_vec;
4502b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, num_nodes, &index_vec));
451eaf62fffSJeremy L Thompson   CeedScalar *array;
4522b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(index_vec, CEED_MEM_HOST, &array));
453ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_nodes; i++) array[i] = i;
4542b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(index_vec, &array));
455eaf62fffSJeremy L Thompson   CeedVector elem_dof;
4562b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, num_elem * elem_size * num_comp, &elem_dof));
4572b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(elem_dof, 0.0));
4582b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(rstr_in, CEED_NOTRANSPOSE, index_vec, elem_dof, CEED_REQUEST_IMMEDIATE));
459eaf62fffSJeremy L Thompson   const CeedScalar *elem_dof_a;
4602b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(elem_dof, CEED_MEM_HOST, &elem_dof_a));
4612b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&index_vec));
462eaf62fffSJeremy L Thompson 
463eaf62fffSJeremy L Thompson   // Determine i, j locations for element matrices
464eaf62fffSJeremy L Thompson   CeedInt count = 0;
465ed9e99e6SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
466ed9e99e6SJeremy L Thompson     for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
467ed9e99e6SJeremy L Thompson       for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
468ed9e99e6SJeremy L Thompson         for (CeedInt i = 0; i < elem_size; i++) {
469ed9e99e6SJeremy L Thompson           for (CeedInt j = 0; j < elem_size; j++) {
4702b730f8bSJeremy L Thompson             const CeedInt elem_dof_index_row = i * layout_er[0] + (comp_out)*layout_er[1] + e * layout_er[2];
4712b730f8bSJeremy L Thompson             const CeedInt elem_dof_index_col = j * layout_er[0] + comp_in * layout_er[1] + e * layout_er[2];
472eaf62fffSJeremy L Thompson 
473eaf62fffSJeremy L Thompson             const CeedInt row = elem_dof_a[elem_dof_index_row];
474eaf62fffSJeremy L Thompson             const CeedInt col = elem_dof_a[elem_dof_index_col];
475eaf62fffSJeremy L Thompson 
476eaf62fffSJeremy L Thompson             rows[offset + count] = row;
477eaf62fffSJeremy L Thompson             cols[offset + count] = col;
478eaf62fffSJeremy L Thompson             count++;
479eaf62fffSJeremy L Thompson           }
480eaf62fffSJeremy L Thompson         }
481eaf62fffSJeremy L Thompson       }
482eaf62fffSJeremy L Thompson     }
483eaf62fffSJeremy L Thompson   }
4842b730f8bSJeremy L Thompson   if (count != local_num_entries) {
485eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
486eaf62fffSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MAJOR, "Error computing assembled entries");
487eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
4882b730f8bSJeremy L Thompson   }
4892b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(elem_dof, &elem_dof_a));
4902b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&elem_dof));
491eaf62fffSJeremy L Thompson 
492eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
493eaf62fffSJeremy L Thompson }
494eaf62fffSJeremy L Thompson 
495eaf62fffSJeremy L Thompson /**
496eaf62fffSJeremy L Thompson   @brief Assemble nonzero entries for non-composite operator
497eaf62fffSJeremy L Thompson 
498eaf62fffSJeremy L Thompson   Users should generally use CeedOperatorLinearAssemble()
499eaf62fffSJeremy L Thompson 
500eaf62fffSJeremy L Thompson   @param[in]  op     CeedOperator to assemble
501ea61e9acSJeremy L Thompson   @param[in]  offset Offset for number of entries
502eaf62fffSJeremy L Thompson   @param[out] values Values to assemble into matrix
503eaf62fffSJeremy L Thompson 
504eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
505eaf62fffSJeremy L Thompson 
506eaf62fffSJeremy L Thompson   @ref Developer
507eaf62fffSJeremy L Thompson **/
5082b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) {
509eaf62fffSJeremy L Thompson   Ceed ceed = op->ceed;
5102b730f8bSJeremy L Thompson   if (op->is_composite) {
511eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
5122b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
513eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
5142b730f8bSJeremy L Thompson   }
51552b3e6a7SJed Brown   if (op->num_elem == 0) return CEED_ERROR_SUCCESS;
516eaf62fffSJeremy L Thompson 
517cefa2673SJeremy L Thompson   if (op->LinearAssembleSingle) {
518cefa2673SJeremy L Thompson     // Backend version
5192b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSingle(op, offset, values));
520cefa2673SJeremy L Thompson     return CEED_ERROR_SUCCESS;
521cefa2673SJeremy L Thompson   } else {
522cefa2673SJeremy L Thompson     // Operator fallback
523cefa2673SJeremy L Thompson     CeedOperator op_fallback;
524cefa2673SJeremy L Thompson 
5252b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
526cefa2673SJeremy L Thompson     if (op_fallback) {
5272b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values));
528cefa2673SJeremy L Thompson       return CEED_ERROR_SUCCESS;
529cefa2673SJeremy L Thompson     }
530cefa2673SJeremy L Thompson   }
531cefa2673SJeremy L Thompson 
532eaf62fffSJeremy L Thompson   // Assemble QFunction
533eaf62fffSJeremy L Thompson   CeedQFunction qf;
5342b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
535eaf62fffSJeremy L Thompson   CeedVector          assembled_qf;
536eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_q;
5372b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE));
5381f9221feSJeremy L Thompson   CeedSize qf_length;
5392b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(assembled_qf, &qf_length));
540eaf62fffSJeremy L Thompson 
5417e7773b5SJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
542eaf62fffSJeremy L Thompson   CeedOperatorField *input_fields;
543eaf62fffSJeremy L Thompson   CeedOperatorField *output_fields;
5442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
545eaf62fffSJeremy L Thompson 
546ed9e99e6SJeremy L Thompson   // Get assembly data
547ed9e99e6SJeremy L Thompson   CeedOperatorAssemblyData data;
5482b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
549ed9e99e6SJeremy L Thompson   const CeedEvalMode *eval_mode_in, *eval_mode_out;
550ed9e99e6SJeremy L Thompson   CeedInt             num_eval_mode_in, num_eval_mode_out;
5512b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_eval_mode_in, &eval_mode_in, &num_eval_mode_out, &eval_mode_out));
552ed9e99e6SJeremy L Thompson   CeedBasis basis_in, basis_out;
5532b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, &basis_in, NULL, &basis_out, NULL));
554eaf62fffSJeremy L Thompson 
5552b730f8bSJeremy L Thompson   if (num_eval_mode_in == 0 || num_eval_mode_out == 0) {
556eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
5572b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator with out inputs/outputs");
558eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
5592b730f8bSJeremy L Thompson   }
560eaf62fffSJeremy L Thompson 
561ed9e99e6SJeremy L Thompson   CeedElemRestriction active_rstr;
562eaf62fffSJeremy L Thompson   CeedInt             num_elem, elem_size, num_qpts, num_comp;
5632b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &active_rstr));
5642b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(active_rstr, &num_elem));
5652b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(active_rstr, &elem_size));
5662b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(active_rstr, &num_comp));
5672b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
568eaf62fffSJeremy L Thompson 
569eaf62fffSJeremy L Thompson   CeedInt local_num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
570eaf62fffSJeremy L Thompson 
571eaf62fffSJeremy L Thompson   // loop over elements and put in data structure
572eaf62fffSJeremy L Thompson   const CeedScalar *interp_in, *grad_in;
5732b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp(basis_in, &interp_in));
5742b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad(basis_in, &grad_in));
575eaf62fffSJeremy L Thompson 
576eaf62fffSJeremy L Thompson   const CeedScalar *assembled_qf_array;
5772b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
578eaf62fffSJeremy L Thompson 
579eaf62fffSJeremy L Thompson   CeedInt layout_qf[3];
5802b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_q, &layout_qf));
5812b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_q));
582eaf62fffSJeremy L Thompson 
583eaf62fffSJeremy L Thompson   // we store B_mat_in, B_mat_out, BTD, elem_mat in row-major order
584ed9e99e6SJeremy L Thompson   const CeedScalar *B_mat_in, *B_mat_out;
5852b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &B_mat_in, NULL, &B_mat_out));
586ed9e99e6SJeremy L Thompson   CeedScalar  BTD_mat[elem_size * num_qpts * num_eval_mode_in];
587eaf62fffSJeremy L Thompson   CeedScalar  elem_mat[elem_size * elem_size];
58892ae7e47SJeremy L Thompson   CeedInt     count = 0;
589eaf62fffSJeremy L Thompson   CeedScalar *vals;
5902b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(values, CEED_MEM_HOST, &vals));
591ed9e99e6SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
592ed9e99e6SJeremy L Thompson     for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
593ed9e99e6SJeremy L Thompson       for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
594ed9e99e6SJeremy L Thompson         // Compute B^T*D
595ed9e99e6SJeremy L Thompson         for (CeedInt n = 0; n < elem_size; n++) {
596ed9e99e6SJeremy L Thompson           for (CeedInt q = 0; q < num_qpts; q++) {
597ed9e99e6SJeremy L Thompson             for (CeedInt e_in = 0; e_in < num_eval_mode_in; e_in++) {
5982b730f8bSJeremy L Thompson               const CeedInt btd_index = n * (num_qpts * num_eval_mode_in) + (num_eval_mode_in * q + e_in);
599067fd99fSJeremy L Thompson               CeedScalar    sum       = 0.0;
600067fd99fSJeremy L Thompson               for (CeedInt e_out = 0; e_out < num_eval_mode_out; e_out++) {
601ed9e99e6SJeremy L Thompson                 const CeedInt b_out_index     = (num_eval_mode_out * q + e_out) * elem_size + n;
6022b730f8bSJeremy L Thompson                 const CeedInt eval_mode_index = ((e_in * num_comp + comp_in) * num_eval_mode_out + e_out) * num_comp + comp_out;
6032b730f8bSJeremy L Thompson                 const CeedInt qf_index        = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2];
604067fd99fSJeremy L Thompson                 sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index];
605eaf62fffSJeremy L Thompson               }
606067fd99fSJeremy L Thompson               BTD_mat[btd_index] = sum;
607ed9e99e6SJeremy L Thompson             }
608ed9e99e6SJeremy L Thompson           }
609eaf62fffSJeremy L Thompson         }
610eaf62fffSJeremy L Thompson         // form element matrix itself (for each block component)
6112b730f8bSJeremy L Thompson         CeedCall(CeedMatrixMatrixMultiply(ceed, BTD_mat, B_mat_in, elem_mat, elem_size, elem_size, num_qpts * num_eval_mode_in));
612eaf62fffSJeremy L Thompson 
613eaf62fffSJeremy L Thompson         // put element matrix in coordinate data structure
614ed9e99e6SJeremy L Thompson         for (CeedInt i = 0; i < elem_size; i++) {
615ed9e99e6SJeremy L Thompson           for (CeedInt j = 0; j < elem_size; j++) {
616eaf62fffSJeremy L Thompson             vals[offset + count] = elem_mat[i * elem_size + j];
617eaf62fffSJeremy L Thompson             count++;
618eaf62fffSJeremy L Thompson           }
619eaf62fffSJeremy L Thompson         }
620eaf62fffSJeremy L Thompson       }
621eaf62fffSJeremy L Thompson     }
622eaf62fffSJeremy L Thompson   }
6232b730f8bSJeremy L Thompson   if (count != local_num_entries) {
624eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
625eaf62fffSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MAJOR, "Error computing entries");
626eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
6272b730f8bSJeremy L Thompson   }
6282b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(values, &vals));
629eaf62fffSJeremy L Thompson 
6302b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
6312b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
632eaf62fffSJeremy L Thompson 
633eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
634eaf62fffSJeremy L Thompson }
635eaf62fffSJeremy L Thompson 
636eaf62fffSJeremy L Thompson /**
637eaf62fffSJeremy L Thompson   @brief Count number of entries for assembled CeedOperator
638eaf62fffSJeremy L Thompson 
639eaf62fffSJeremy L Thompson   @param[in]  op          CeedOperator to assemble
640eaf62fffSJeremy L Thompson   @param[out] num_entries Number of entries in assembled representation
641eaf62fffSJeremy L Thompson 
642eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
643eaf62fffSJeremy L Thompson 
644eaf62fffSJeremy L Thompson   @ref Utility
645eaf62fffSJeremy L Thompson **/
6462b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedInt *num_entries) {
647eaf62fffSJeremy L Thompson   CeedElemRestriction rstr;
648eaf62fffSJeremy L Thompson   CeedInt             num_elem, elem_size, num_comp;
649eaf62fffSJeremy L Thompson 
6502b730f8bSJeremy L Thompson   if (op->is_composite) {
651eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
6522b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
653eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
6542b730f8bSJeremy L Thompson   }
6552b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &rstr));
6562b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
6572b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
6582b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
659eaf62fffSJeremy L Thompson   *num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
660eaf62fffSJeremy L Thompson 
661eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
662eaf62fffSJeremy L Thompson }
663eaf62fffSJeremy L Thompson 
664eaf62fffSJeremy L Thompson /**
665ea61e9acSJeremy L Thompson   @brief Common code for creating a multigrid coarse operator and level transfer operators for a CeedOperator
666eaf62fffSJeremy L Thompson 
667eaf62fffSJeremy L Thompson   @param[in]  op_fine      Fine grid operator
668eaf62fffSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter
669eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid restriction
670eaf62fffSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector basis
671eaf62fffSJeremy L Thompson   @param[in]  basis_c_to_f Basis for coarse to fine interpolation
672eaf62fffSJeremy L Thompson   @param[out] op_coarse    Coarse grid operator
673eaf62fffSJeremy L Thompson   @param[out] op_prolong   Coarse to fine operator
674eaf62fffSJeremy L Thompson   @param[out] op_restrict  Fine to coarse operator
675eaf62fffSJeremy L Thompson 
676eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
677eaf62fffSJeremy L Thompson 
678eaf62fffSJeremy L Thompson   @ref Developer
679eaf62fffSJeremy L Thompson **/
6802b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
6812b730f8bSJeremy L Thompson                                             CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
682eaf62fffSJeremy L Thompson   Ceed ceed;
6832b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
684eaf62fffSJeremy L Thompson 
685eaf62fffSJeremy L Thompson   // Check for composite operator
686eaf62fffSJeremy L Thompson   bool is_composite;
6872b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op_fine, &is_composite));
6882b730f8bSJeremy L Thompson   if (is_composite) {
689eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
6902b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported");
691eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
6922b730f8bSJeremy L Thompson   }
693eaf62fffSJeremy L Thompson 
694eaf62fffSJeremy L Thompson   // Coarse Grid
6952b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
696eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_fine = NULL;
697eaf62fffSJeremy L Thompson   // -- Clone input fields
69892ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < op_fine->qf->num_input_fields; i++) {
699eaf62fffSJeremy L Thompson     if (op_fine->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
700eaf62fffSJeremy L Thompson       rstr_fine = op_fine->input_fields[i]->elem_restr;
7012b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE));
702eaf62fffSJeremy L Thompson     } else {
7032b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, op_fine->input_fields[i]->elem_restr,
7042b730f8bSJeremy L Thompson                                     op_fine->input_fields[i]->basis, op_fine->input_fields[i]->vec));
705eaf62fffSJeremy L Thompson     }
706eaf62fffSJeremy L Thompson   }
707eaf62fffSJeremy L Thompson   // -- Clone output fields
70892ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < op_fine->qf->num_output_fields; i++) {
709eaf62fffSJeremy L Thompson     if (op_fine->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
7102b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE));
711eaf62fffSJeremy L Thompson     } else {
7122b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, op_fine->output_fields[i]->elem_restr,
7132b730f8bSJeremy L Thompson                                     op_fine->output_fields[i]->basis, op_fine->output_fields[i]->vec));
714eaf62fffSJeremy L Thompson     }
715eaf62fffSJeremy L Thompson   }
716af99e877SJeremy L Thompson   // -- Clone QFunctionAssemblyData
7172b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op_fine->qf_assembled, &(*op_coarse)->qf_assembled));
718eaf62fffSJeremy L Thompson 
719eaf62fffSJeremy L Thompson   // Multiplicity vector
720eaf62fffSJeremy L Thompson   CeedVector mult_vec, mult_e_vec;
7212b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec));
7222b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(mult_e_vec, 0.0));
7232b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(rstr_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE));
7242b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(mult_vec, 0.0));
7252b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(rstr_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE));
7262b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_e_vec));
7272b730f8bSJeremy L Thompson   CeedCall(CeedVectorReciprocal(mult_vec));
728eaf62fffSJeremy L Thompson 
729eaf62fffSJeremy L Thompson   // Restriction
730eaf62fffSJeremy L Thompson   CeedInt num_comp;
7312b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp));
732eaf62fffSJeremy L Thompson   CeedQFunction qf_restrict;
7332b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict));
734eaf62fffSJeremy L Thompson   CeedInt *num_comp_r_data;
7352b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, &num_comp_r_data));
736eaf62fffSJeremy L Thompson   num_comp_r_data[0] = num_comp;
737eaf62fffSJeremy L Thompson   CeedQFunctionContext ctx_r;
7382b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r));
7392b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data));
7402b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r));
7412b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_r));
7422b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE));
7432b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE));
7442b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP));
7452b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp));
746eaf62fffSJeremy L Thompson 
7472b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict));
7482b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE));
7492b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_fine, CEED_BASIS_COLLOCATED, mult_vec));
7502b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
751eaf62fffSJeremy L Thompson 
752eaf62fffSJeremy L Thompson   // Prolongation
753eaf62fffSJeremy L Thompson   CeedQFunction qf_prolong;
7542b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong));
755eaf62fffSJeremy L Thompson   CeedInt *num_comp_p_data;
7562b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, &num_comp_p_data));
757eaf62fffSJeremy L Thompson   num_comp_p_data[0] = num_comp;
758eaf62fffSJeremy L Thompson   CeedQFunctionContext ctx_p;
7592b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p));
7602b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data));
7612b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p));
7622b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_p));
7632b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP));
7642b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE));
7652b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE));
7662b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp));
767eaf62fffSJeremy L Thompson 
7682b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong));
7692b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
7702b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_fine, CEED_BASIS_COLLOCATED, mult_vec));
7712b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE));
772eaf62fffSJeremy L Thompson 
773ea6b5821SJeremy L Thompson   // Clone name
774ea6b5821SJeremy L Thompson   bool   has_name = op_fine->name;
775ea6b5821SJeremy L Thompson   size_t name_len = op_fine->name ? strlen(op_fine->name) : 0;
7762b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name));
777ea6b5821SJeremy L Thompson   {
778ea6b5821SJeremy L Thompson     char *prolongation_name;
7792b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(18 + name_len, &prolongation_name));
7802b730f8bSJeremy L Thompson     sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
7812b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name));
7822b730f8bSJeremy L Thompson     CeedCall(CeedFree(&prolongation_name));
783ea6b5821SJeremy L Thompson   }
784ea6b5821SJeremy L Thompson   {
785ea6b5821SJeremy L Thompson     char *restriction_name;
7862b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(17 + name_len, &restriction_name));
7872b730f8bSJeremy L Thompson     sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
7882b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_restrict, restriction_name));
7892b730f8bSJeremy L Thompson     CeedCall(CeedFree(&restriction_name));
790ea6b5821SJeremy L Thompson   }
791ea6b5821SJeremy L Thompson 
792eaf62fffSJeremy L Thompson   // Cleanup
7932b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_vec));
7942b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis_c_to_f));
7952b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_restrict));
7962b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_prolong));
797805fe78eSJeremy L Thompson 
798eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
799eaf62fffSJeremy L Thompson }
800eaf62fffSJeremy L Thompson 
801eaf62fffSJeremy L Thompson /**
802eaf62fffSJeremy L Thompson   @brief Build 1D mass matrix and Laplacian with perturbation
803eaf62fffSJeremy L Thompson 
804eaf62fffSJeremy L Thompson   @param[in]  interp_1d   Interpolation matrix in one dimension
805eaf62fffSJeremy L Thompson   @param[in]  grad_1d     Gradient matrix in one dimension
806eaf62fffSJeremy L Thompson   @param[in]  q_weight_1d Quadrature weights in one dimension
807eaf62fffSJeremy L Thompson   @param[in]  P_1d        Number of basis nodes in one dimension
808eaf62fffSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
809eaf62fffSJeremy L Thompson   @param[in]  dim         Dimension of basis
810eaf62fffSJeremy L Thompson   @param[out] mass        Assembled mass matrix in one dimension
811eaf62fffSJeremy L Thompson   @param[out] laplace     Assembled perturbed Laplacian in one dimension
812eaf62fffSJeremy L Thompson 
813eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
814eaf62fffSJeremy L Thompson 
815eaf62fffSJeremy L Thompson   @ref Developer
816eaf62fffSJeremy L Thompson **/
8172b730f8bSJeremy L Thompson CeedPragmaOptimizeOff static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d,
8182b730f8bSJeremy L Thompson                                                       CeedInt P_1d, CeedInt Q_1d, CeedInt dim, CeedScalar *mass, CeedScalar *laplace) {
8192b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
820eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
821eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
8222b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < Q_1d; k++) sum += interp_1d[k * P_1d + i] * q_weight_1d[k] * interp_1d[k * P_1d + j];
823eaf62fffSJeremy L Thompson       mass[i + j * P_1d] = sum;
824eaf62fffSJeremy L Thompson     }
8252b730f8bSJeremy L Thompson   }
826eaf62fffSJeremy L Thompson   // -- Laplacian
8272b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
828eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
829eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
8302b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < Q_1d; k++) sum += grad_1d[k * P_1d + i] * q_weight_1d[k] * grad_1d[k * P_1d + j];
831eaf62fffSJeremy L Thompson       laplace[i + j * P_1d] = sum;
832eaf62fffSJeremy L Thompson     }
8332b730f8bSJeremy L Thompson   }
834eaf62fffSJeremy L Thompson   CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4;
8352b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation;
836eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
837eaf62fffSJeremy L Thompson }
838ea61e9acSJeremy L Thompson CeedPragmaOptimizeOn;
839eaf62fffSJeremy L Thompson 
840eaf62fffSJeremy L Thompson /// @}
841eaf62fffSJeremy L Thompson 
842eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
843480fae85SJeremy L Thompson /// CeedOperator Backend API
844480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
845480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend
846480fae85SJeremy L Thompson /// @{
847480fae85SJeremy L Thompson 
848480fae85SJeremy L Thompson /**
849480fae85SJeremy L Thompson   @brief Create object holding CeedQFunction assembly data for CeedOperator
850480fae85SJeremy L Thompson 
851480fae85SJeremy L Thompson   @param[in]  ceed A Ceed object where the CeedQFunctionAssemblyData will be created
852ea61e9acSJeremy L Thompson   @param[out] data Address of the variable where the newly created CeedQFunctionAssemblyData will be stored
853480fae85SJeremy L Thompson 
854480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
855480fae85SJeremy L Thompson 
856480fae85SJeremy L Thompson   @ref Backend
857480fae85SJeremy L Thompson **/
858ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) {
8592b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
860480fae85SJeremy L Thompson   (*data)->ref_count = 1;
861480fae85SJeremy L Thompson   (*data)->ceed      = ceed;
8622b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
863480fae85SJeremy L Thompson 
864480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
865480fae85SJeremy L Thompson }
866480fae85SJeremy L Thompson 
867480fae85SJeremy L Thompson /**
868480fae85SJeremy L Thompson   @brief Increment the reference counter for a CeedQFunctionAssemblyData
869480fae85SJeremy L Thompson 
870ea61e9acSJeremy L Thompson   @param[in,out] data CeedQFunctionAssemblyData to increment the reference counter
871480fae85SJeremy L Thompson 
872480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
873480fae85SJeremy L Thompson 
874480fae85SJeremy L Thompson   @ref Backend
875480fae85SJeremy L Thompson **/
876480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) {
877480fae85SJeremy L Thompson   data->ref_count++;
878480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
879480fae85SJeremy L Thompson }
880480fae85SJeremy L Thompson 
881480fae85SJeremy L Thompson /**
882beecbf24SJeremy L Thompson   @brief Set re-use of CeedQFunctionAssemblyData
8838b919e6bSJeremy L Thompson 
884ea61e9acSJeremy L Thompson   @param[in,out] data       CeedQFunctionAssemblyData to mark for reuse
885ea61e9acSJeremy L Thompson   @param[in]     reuse_data Boolean flag indicating data re-use
8868b919e6bSJeremy L Thompson 
8878b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
8888b919e6bSJeremy L Thompson 
8898b919e6bSJeremy L Thompson   @ref Backend
8908b919e6bSJeremy L Thompson **/
8912b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) {
892beecbf24SJeremy L Thompson   data->reuse_data        = reuse_data;
893beecbf24SJeremy L Thompson   data->needs_data_update = true;
894beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
895beecbf24SJeremy L Thompson }
896beecbf24SJeremy L Thompson 
897beecbf24SJeremy L Thompson /**
898beecbf24SJeremy L Thompson   @brief Mark QFunctionAssemblyData as stale
899beecbf24SJeremy L Thompson 
900ea61e9acSJeremy L Thompson   @param[in,out] data              CeedQFunctionAssemblyData to mark as stale
901ea61e9acSJeremy L Thompson   @param[in]     needs_data_update Boolean flag indicating if update is needed or completed
902beecbf24SJeremy L Thompson 
903beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
904beecbf24SJeremy L Thompson 
905beecbf24SJeremy L Thompson   @ref Backend
906beecbf24SJeremy L Thompson **/
9072b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) {
908beecbf24SJeremy L Thompson   data->needs_data_update = needs_data_update;
9098b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
9108b919e6bSJeremy L Thompson }
9118b919e6bSJeremy L Thompson 
9128b919e6bSJeremy L Thompson /**
9138b919e6bSJeremy L Thompson   @brief Determine if QFunctionAssemblyData needs update
9148b919e6bSJeremy L Thompson 
9158b919e6bSJeremy L Thompson   @param[in]  data             CeedQFunctionAssemblyData to mark as stale
9168b919e6bSJeremy L Thompson   @param[out] is_update_needed Boolean flag indicating if re-assembly is required
9178b919e6bSJeremy L Thompson 
9188b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9198b919e6bSJeremy L Thompson 
9208b919e6bSJeremy L Thompson   @ref Backend
9218b919e6bSJeremy L Thompson **/
9222b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) {
923beecbf24SJeremy L Thompson   *is_update_needed = !data->reuse_data || data->needs_data_update;
9248b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
9258b919e6bSJeremy L Thompson }
9268b919e6bSJeremy L Thompson 
9278b919e6bSJeremy L Thompson /**
928ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedQFunctionAssemblyData.
929ea61e9acSJeremy L Thompson            Both pointers should be destroyed with `CeedCeedQFunctionAssemblyDataDestroy()`.
930ea61e9acSJeremy L Thompson            Note: If `*data_copy` is non-NULL, then it is assumed that `*data_copy` is a pointer to a CeedQFunctionAssemblyData.
931ea61e9acSJeremy L Thompson              This CeedQFunctionAssemblyData will be destroyed if `*data_copy` is the only reference to this CeedQFunctionAssemblyData.
932480fae85SJeremy L Thompson 
933ea61e9acSJeremy L Thompson   @param[in]     data      CeedQFunctionAssemblyData to copy reference to
934ea61e9acSJeremy L Thompson   @param[in,out] data_copy Variable to store copied reference
935480fae85SJeremy L Thompson 
936480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
937480fae85SJeremy L Thompson 
938480fae85SJeremy L Thompson   @ref Backend
939480fae85SJeremy L Thompson **/
9402b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) {
9412b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReference(data));
9422b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy));
943480fae85SJeremy L Thompson   *data_copy = data;
944480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
945480fae85SJeremy L Thompson }
946480fae85SJeremy L Thompson 
947480fae85SJeremy L Thompson /**
948480fae85SJeremy L Thompson   @brief Get setup status for internal objects for CeedQFunctionAssemblyData
949480fae85SJeremy L Thompson 
950ea61e9acSJeremy L Thompson   @param[in]  data     CeedQFunctionAssemblyData to retrieve status
951480fae85SJeremy L Thompson   @param[out] is_setup Boolean flag for setup status
952480fae85SJeremy L Thompson 
953480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
954480fae85SJeremy L Thompson 
955480fae85SJeremy L Thompson   @ref Backend
956480fae85SJeremy L Thompson **/
9572b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) {
958480fae85SJeremy L Thompson   *is_setup = data->is_setup;
959480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
960480fae85SJeremy L Thompson }
961480fae85SJeremy L Thompson 
962480fae85SJeremy L Thompson /**
963480fae85SJeremy L Thompson   @brief Set internal objects for CeedQFunctionAssemblyData
964480fae85SJeremy L Thompson 
965ea61e9acSJeremy L Thompson   @param[in,out] data CeedQFunctionAssemblyData to set objects
966480fae85SJeremy L Thompson   @param[in]     vec  CeedVector to store assembled CeedQFunction at quadrature points
967480fae85SJeremy L Thompson   @param[in]     rstr CeedElemRestriction for CeedVector containing assembled CeedQFunction
968480fae85SJeremy L Thompson 
969480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
970480fae85SJeremy L Thompson 
971480fae85SJeremy L Thompson   @ref Backend
972480fae85SJeremy L Thompson **/
9732b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) {
9742b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(vec, &data->vec));
9752b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr));
976480fae85SJeremy L Thompson 
977480fae85SJeremy L Thompson   data->is_setup = true;
978480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
979480fae85SJeremy L Thompson }
980480fae85SJeremy L Thompson 
9812b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) {
9822b730f8bSJeremy L Thompson   if (!data->is_setup) {
983480fae85SJeremy L Thompson     // LCOV_EXCL_START
9842b730f8bSJeremy L Thompson     return CeedError(data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first.");
985480fae85SJeremy L Thompson     // LCOV_EXCL_STOP
9862b730f8bSJeremy L Thompson   }
987480fae85SJeremy L Thompson 
9882b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(data->vec, vec));
9892b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr));
990480fae85SJeremy L Thompson 
991480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
992480fae85SJeremy L Thompson }
993480fae85SJeremy L Thompson 
994480fae85SJeremy L Thompson /**
995480fae85SJeremy L Thompson   @brief Destroy CeedQFunctionAssemblyData
996480fae85SJeremy L Thompson 
997ea61e9acSJeremy L Thompson   @param[in,out] data  CeedQFunctionAssemblyData to destroy
998480fae85SJeremy L Thompson 
999480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1000480fae85SJeremy L Thompson 
1001480fae85SJeremy L Thompson   @ref Backend
1002480fae85SJeremy L Thompson **/
1003480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) {
1004480fae85SJeremy L Thompson   if (!*data || --(*data)->ref_count > 0) return CEED_ERROR_SUCCESS;
1005480fae85SJeremy L Thompson 
10062b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
10072b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*data)->vec));
10082b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr));
1009480fae85SJeremy L Thompson 
10102b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1011480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1012480fae85SJeremy L Thompson }
1013480fae85SJeremy L Thompson 
1014ed9e99e6SJeremy L Thompson /**
1015ed9e99e6SJeremy L Thompson   @brief Get CeedOperatorAssemblyData
1016ed9e99e6SJeremy L Thompson 
1017ed9e99e6SJeremy L Thompson   @param[in]  op   CeedOperator to assemble
1018ed9e99e6SJeremy L Thompson   @param[out] data CeedQFunctionAssemblyData
1019ed9e99e6SJeremy L Thompson 
1020ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1021ed9e99e6SJeremy L Thompson 
1022ed9e99e6SJeremy L Thompson   @ref Backend
1023ed9e99e6SJeremy L Thompson **/
10242b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) {
1025ed9e99e6SJeremy L Thompson   if (!op->op_assembled) {
1026ed9e99e6SJeremy L Thompson     CeedOperatorAssemblyData data;
1027ed9e99e6SJeremy L Thompson 
10282b730f8bSJeremy L Thompson     CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data));
1029ed9e99e6SJeremy L Thompson     op->op_assembled = data;
1030ed9e99e6SJeremy L Thompson   }
1031ed9e99e6SJeremy L Thompson   *data = op->op_assembled;
1032ed9e99e6SJeremy L Thompson 
1033ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1034ed9e99e6SJeremy L Thompson }
1035ed9e99e6SJeremy L Thompson 
1036ed9e99e6SJeremy L Thompson /**
1037ed9e99e6SJeremy L Thompson   @brief Create object holding CeedOperator assembly data
1038ed9e99e6SJeremy L Thompson 
1039ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperatorAssemblyData will be created
1040ed9e99e6SJeremy L Thompson   @param[in]  op   CeedOperator to be assembled
1041ea61e9acSJeremy L Thompson   @param[out] data Address of the variable where the newly created CeedOperatorAssemblyData will be stored
1042ed9e99e6SJeremy L Thompson 
1043ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1044ed9e99e6SJeremy L Thompson 
1045ed9e99e6SJeremy L Thompson   @ref Backend
1046ed9e99e6SJeremy L Thompson **/
10472b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) {
10482b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1049ed9e99e6SJeremy L Thompson   (*data)->ceed = ceed;
10502b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1051ed9e99e6SJeremy L Thompson 
1052ed9e99e6SJeremy L Thompson   // Build OperatorAssembly data
1053ed9e99e6SJeremy L Thompson   CeedQFunction       qf;
1054ed9e99e6SJeremy L Thompson   CeedQFunctionField *qf_fields;
1055ed9e99e6SJeremy L Thompson   CeedOperatorField  *op_fields;
1056ed9e99e6SJeremy L Thompson   CeedInt             num_input_fields;
10572b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
10582b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL));
10592b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1060ed9e99e6SJeremy L Thompson 
1061ed9e99e6SJeremy L Thompson   // Determine active input basis
1062ed9e99e6SJeremy L Thompson   CeedInt       num_eval_mode_in = 0, dim = 1;
1063ed9e99e6SJeremy L Thompson   CeedEvalMode *eval_mode_in = NULL;
1064ed9e99e6SJeremy L Thompson   CeedBasis     basis_in     = NULL;
1065ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1066ed9e99e6SJeremy L Thompson     CeedVector vec;
10672b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1068ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
10692b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
10702b730f8bSJeremy L Thompson       CeedCall(CeedBasisGetDimension(basis_in, &dim));
1071ed9e99e6SJeremy L Thompson       CeedEvalMode eval_mode;
10722b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1073ed9e99e6SJeremy L Thompson       switch (eval_mode) {
1074ed9e99e6SJeremy L Thompson         case CEED_EVAL_NONE:
1075ed9e99e6SJeremy L Thompson         case CEED_EVAL_INTERP:
10762b730f8bSJeremy L Thompson           CeedCall(CeedRealloc(num_eval_mode_in + 1, &eval_mode_in));
1077ed9e99e6SJeremy L Thompson           eval_mode_in[num_eval_mode_in] = eval_mode;
1078ed9e99e6SJeremy L Thompson           num_eval_mode_in += 1;
1079ed9e99e6SJeremy L Thompson           break;
1080ed9e99e6SJeremy L Thompson         case CEED_EVAL_GRAD:
10812b730f8bSJeremy L Thompson           CeedCall(CeedRealloc(num_eval_mode_in + dim, &eval_mode_in));
1082ed9e99e6SJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
1083ed9e99e6SJeremy L Thompson             eval_mode_in[num_eval_mode_in + d] = eval_mode;
1084ed9e99e6SJeremy L Thompson           }
1085ed9e99e6SJeremy L Thompson           num_eval_mode_in += dim;
1086ed9e99e6SJeremy L Thompson           break;
1087ed9e99e6SJeremy L Thompson         case CEED_EVAL_WEIGHT:
1088ed9e99e6SJeremy L Thompson         case CEED_EVAL_DIV:
1089ed9e99e6SJeremy L Thompson         case CEED_EVAL_CURL:
1090ed9e99e6SJeremy L Thompson           break;  // Caught by QF Assembly
1091ed9e99e6SJeremy L Thompson       }
1092ed9e99e6SJeremy L Thompson     }
1093ed9e99e6SJeremy L Thompson   }
1094ed9e99e6SJeremy L Thompson   (*data)->num_eval_mode_in = num_eval_mode_in;
1095ed9e99e6SJeremy L Thompson   (*data)->eval_mode_in     = eval_mode_in;
10962b730f8bSJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->basis_in));
1097ed9e99e6SJeremy L Thompson 
1098ed9e99e6SJeremy L Thompson   // Determine active output basis
1099ed9e99e6SJeremy L Thompson   CeedInt num_output_fields;
11002b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields));
11012b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1102ed9e99e6SJeremy L Thompson   CeedInt       num_eval_mode_out = 0;
1103ed9e99e6SJeremy L Thompson   CeedEvalMode *eval_mode_out     = NULL;
1104ed9e99e6SJeremy L Thompson   CeedBasis     basis_out         = NULL;
1105ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1106ed9e99e6SJeremy L Thompson     CeedVector vec;
11072b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1108ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
11092b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1110ed9e99e6SJeremy L Thompson       CeedEvalMode eval_mode;
11112b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1112ed9e99e6SJeremy L Thompson       switch (eval_mode) {
1113ed9e99e6SJeremy L Thompson         case CEED_EVAL_NONE:
1114ed9e99e6SJeremy L Thompson         case CEED_EVAL_INTERP:
11152b730f8bSJeremy L Thompson           CeedCall(CeedRealloc(num_eval_mode_out + 1, &eval_mode_out));
1116ed9e99e6SJeremy L Thompson           eval_mode_out[num_eval_mode_out] = eval_mode;
1117ed9e99e6SJeremy L Thompson           num_eval_mode_out += 1;
1118ed9e99e6SJeremy L Thompson           break;
1119ed9e99e6SJeremy L Thompson         case CEED_EVAL_GRAD:
11202b730f8bSJeremy L Thompson           CeedCall(CeedRealloc(num_eval_mode_out + dim, &eval_mode_out));
1121ed9e99e6SJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
1122ed9e99e6SJeremy L Thompson             eval_mode_out[num_eval_mode_out + d] = eval_mode;
1123ed9e99e6SJeremy L Thompson           }
1124ed9e99e6SJeremy L Thompson           num_eval_mode_out += dim;
1125ed9e99e6SJeremy L Thompson           break;
1126ed9e99e6SJeremy L Thompson         case CEED_EVAL_WEIGHT:
1127ed9e99e6SJeremy L Thompson         case CEED_EVAL_DIV:
1128ed9e99e6SJeremy L Thompson         case CEED_EVAL_CURL:
1129ed9e99e6SJeremy L Thompson           break;  // Caught by QF Assembly
1130ed9e99e6SJeremy L Thompson       }
1131ed9e99e6SJeremy L Thompson     }
1132ed9e99e6SJeremy L Thompson   }
1133ed9e99e6SJeremy L Thompson   (*data)->num_eval_mode_out = num_eval_mode_out;
1134ed9e99e6SJeremy L Thompson   (*data)->eval_mode_out     = eval_mode_out;
11352b730f8bSJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->basis_out));
1136ed9e99e6SJeremy L Thompson 
1137ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1138ed9e99e6SJeremy L Thompson }
1139ed9e99e6SJeremy L Thompson 
1140ed9e99e6SJeremy L Thompson /**
1141ed9e99e6SJeremy L Thompson   @brief Get CeedOperator CeedEvalModes for assembly
1142ed9e99e6SJeremy L Thompson 
1143ed9e99e6SJeremy L Thompson   @param[in]  data              CeedOperatorAssemblyData
1144ed9e99e6SJeremy L Thompson   @param[out] num_eval_mode_in  Pointer to hold number of input CeedEvalModes, or NULL
1145ed9e99e6SJeremy L Thompson   @param[out] eval_mode_in      Pointer to hold input CeedEvalModes, or NULL
1146ed9e99e6SJeremy L Thompson   @param[out] num_eval_mode_out Pointer to hold number of output CeedEvalModes, or NULL
1147ed9e99e6SJeremy L Thompson   @param[out] eval_mode_out     Pointer to hold output CeedEvalModes, or NULL
1148ed9e99e6SJeremy L Thompson 
1149ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1150ed9e99e6SJeremy L Thompson 
1151ed9e99e6SJeremy L Thompson   @ref Backend
1152ed9e99e6SJeremy L Thompson **/
11532b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_eval_mode_in, const CeedEvalMode **eval_mode_in,
1154ed9e99e6SJeremy L Thompson                                          CeedInt *num_eval_mode_out, const CeedEvalMode **eval_mode_out) {
1155ed9e99e6SJeremy L Thompson   if (num_eval_mode_in) *num_eval_mode_in = data->num_eval_mode_in;
1156ed9e99e6SJeremy L Thompson   if (eval_mode_in) *eval_mode_in = data->eval_mode_in;
1157ed9e99e6SJeremy L Thompson   if (num_eval_mode_out) *num_eval_mode_out = data->num_eval_mode_out;
1158ed9e99e6SJeremy L Thompson   if (eval_mode_out) *eval_mode_out = data->eval_mode_out;
1159ed9e99e6SJeremy L Thompson 
1160ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1161ed9e99e6SJeremy L Thompson }
1162ed9e99e6SJeremy L Thompson 
1163ed9e99e6SJeremy L Thompson /**
1164ed9e99e6SJeremy L Thompson   @brief Get CeedOperator CeedBasis data for assembly
1165ed9e99e6SJeremy L Thompson 
1166ed9e99e6SJeremy L Thompson   @param[in]  data      CeedOperatorAssemblyData
1167ed9e99e6SJeremy L Thompson   @param[out] basis_in  Pointer to hold active input CeedBasis, or NULL
1168ed9e99e6SJeremy L Thompson   @param[out] B_in      Pointer to hold assembled active input B, or NULL
1169ed9e99e6SJeremy L Thompson   @param[out] basis_out Pointer to hold active output CeedBasis, or NULL
1170ed9e99e6SJeremy L Thompson   @param[out] B_out     Pointer to hold assembled active output B, or NULL
1171ed9e99e6SJeremy L Thompson 
1172ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1173ed9e99e6SJeremy L Thompson 
1174ed9e99e6SJeremy L Thompson   @ref Backend
1175ed9e99e6SJeremy L Thompson **/
11762b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedBasis *basis_in, const CeedScalar **B_in, CeedBasis *basis_out,
1177ed9e99e6SJeremy L Thompson                                      const CeedScalar **B_out) {
1178ed9e99e6SJeremy L Thompson   // Assemble B_in, B_out if needed
1179ed9e99e6SJeremy L Thompson   if (B_in && !data->B_in) {
1180ed9e99e6SJeremy L Thompson     CeedInt           num_qpts, elem_size;
1181ed9e99e6SJeremy L Thompson     CeedScalar       *B_in, *identity = NULL;
1182ed9e99e6SJeremy L Thompson     const CeedScalar *interp_in, *grad_in;
1183ed9e99e6SJeremy L Thompson     bool              has_eval_none = false;
1184ed9e99e6SJeremy L Thompson 
11852b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(data->basis_in, &num_qpts));
11862b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(data->basis_in, &elem_size));
11872b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_qpts * elem_size * data->num_eval_mode_in, &B_in));
1188ed9e99e6SJeremy L Thompson 
1189ed9e99e6SJeremy L Thompson     for (CeedInt i = 0; i < data->num_eval_mode_in; i++) {
1190ed9e99e6SJeremy L Thompson       has_eval_none = has_eval_none || (data->eval_mode_in[i] == CEED_EVAL_NONE);
1191ed9e99e6SJeremy L Thompson     }
1192ed9e99e6SJeremy L Thompson     if (has_eval_none) {
11932b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * elem_size, &identity));
1194ed9e99e6SJeremy L Thompson       for (CeedInt i = 0; i < (elem_size < num_qpts ? elem_size : num_qpts); i++) {
1195ed9e99e6SJeremy L Thompson         identity[i * elem_size + i] = 1.0;
1196ed9e99e6SJeremy L Thompson       }
1197ed9e99e6SJeremy L Thompson     }
11982b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(data->basis_in, &interp_in));
11992b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetGrad(data->basis_in, &grad_in));
1200ed9e99e6SJeremy L Thompson 
1201ed9e99e6SJeremy L Thompson     for (CeedInt q = 0; q < num_qpts; q++) {
1202ed9e99e6SJeremy L Thompson       for (CeedInt n = 0; n < elem_size; n++) {
1203ed9e99e6SJeremy L Thompson         CeedInt d_in = -1;
1204ed9e99e6SJeremy L Thompson         for (CeedInt e_in = 0; e_in < data->num_eval_mode_in; e_in++) {
1205ed9e99e6SJeremy L Thompson           const CeedInt     qq = data->num_eval_mode_in * q;
1206ed9e99e6SJeremy L Thompson           const CeedScalar *b  = NULL;
1207ed9e99e6SJeremy L Thompson 
1208ed9e99e6SJeremy L Thompson           if (data->eval_mode_in[e_in] == CEED_EVAL_GRAD) d_in++;
12092b730f8bSJeremy L Thompson           CeedOperatorGetBasisPointer(data->eval_mode_in[e_in], identity, interp_in, &grad_in[d_in * num_qpts * elem_size], &b);
1210ed9e99e6SJeremy L Thompson           B_in[(qq + e_in) * elem_size + n] = b[q * elem_size + n];
1211ed9e99e6SJeremy L Thompson         }
1212ed9e99e6SJeremy L Thompson       }
1213ed9e99e6SJeremy L Thompson     }
1214ed9e99e6SJeremy L Thompson     data->B_in = B_in;
1215ed9e99e6SJeremy L Thompson   }
1216ed9e99e6SJeremy L Thompson 
1217ed9e99e6SJeremy L Thompson   if (B_out && !data->B_out) {
1218ed9e99e6SJeremy L Thompson     CeedInt           num_qpts, elem_size;
1219ed9e99e6SJeremy L Thompson     CeedScalar       *B_out, *identity = NULL;
1220ed9e99e6SJeremy L Thompson     const CeedScalar *interp_out, *grad_out;
1221ed9e99e6SJeremy L Thompson     bool              has_eval_none = false;
1222ed9e99e6SJeremy L Thompson 
12232b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(data->basis_out, &num_qpts));
12242b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(data->basis_out, &elem_size));
12252b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_qpts * elem_size * data->num_eval_mode_out, &B_out));
1226ed9e99e6SJeremy L Thompson 
1227ed9e99e6SJeremy L Thompson     for (CeedInt i = 0; i < data->num_eval_mode_out; i++) {
1228ed9e99e6SJeremy L Thompson       has_eval_none = has_eval_none || (data->eval_mode_out[i] == CEED_EVAL_NONE);
1229ed9e99e6SJeremy L Thompson     }
1230ed9e99e6SJeremy L Thompson     if (has_eval_none) {
12312b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * elem_size, &identity));
1232ed9e99e6SJeremy L Thompson       for (CeedInt i = 0; i < (elem_size < num_qpts ? elem_size : num_qpts); i++) {
1233ed9e99e6SJeremy L Thompson         identity[i * elem_size + i] = 1.0;
1234ed9e99e6SJeremy L Thompson       }
1235ed9e99e6SJeremy L Thompson     }
12362b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(data->basis_out, &interp_out));
12372b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetGrad(data->basis_out, &grad_out));
1238ed9e99e6SJeremy L Thompson 
1239ed9e99e6SJeremy L Thompson     for (CeedInt q = 0; q < num_qpts; q++) {
1240ed9e99e6SJeremy L Thompson       for (CeedInt n = 0; n < elem_size; n++) {
1241ed9e99e6SJeremy L Thompson         CeedInt d_out = -1;
1242ed9e99e6SJeremy L Thompson         for (CeedInt e_out = 0; e_out < data->num_eval_mode_out; e_out++) {
1243ed9e99e6SJeremy L Thompson           const CeedInt     qq = data->num_eval_mode_out * q;
1244ed9e99e6SJeremy L Thompson           const CeedScalar *b  = NULL;
1245ed9e99e6SJeremy L Thompson 
1246ed9e99e6SJeremy L Thompson           if (data->eval_mode_out[e_out] == CEED_EVAL_GRAD) d_out++;
12472b730f8bSJeremy L Thompson           CeedOperatorGetBasisPointer(data->eval_mode_out[e_out], identity, interp_out, &grad_out[d_out * num_qpts * elem_size], &b);
1248ed9e99e6SJeremy L Thompson           B_out[(qq + e_out) * elem_size + n] = b[q * elem_size + n];
1249ed9e99e6SJeremy L Thompson         }
1250ed9e99e6SJeremy L Thompson       }
1251ed9e99e6SJeremy L Thompson     }
1252ed9e99e6SJeremy L Thompson     data->B_out = B_out;
1253ed9e99e6SJeremy L Thompson   }
1254ed9e99e6SJeremy L Thompson 
1255ed9e99e6SJeremy L Thompson   if (basis_in) *basis_in = data->basis_in;
1256ed9e99e6SJeremy L Thompson   if (B_in) *B_in = data->B_in;
1257ed9e99e6SJeremy L Thompson   if (basis_out) *basis_out = data->basis_out;
1258ed9e99e6SJeremy L Thompson   if (B_out) *B_out = data->B_out;
1259ed9e99e6SJeremy L Thompson 
1260ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1261ed9e99e6SJeremy L Thompson }
1262ed9e99e6SJeremy L Thompson 
1263ed9e99e6SJeremy L Thompson /**
1264ed9e99e6SJeremy L Thompson   @brief Destroy CeedOperatorAssemblyData
1265ed9e99e6SJeremy L Thompson 
1266ea61e9acSJeremy L Thompson   @param[in,out] data CeedOperatorAssemblyData to destroy
1267ed9e99e6SJeremy L Thompson 
1268ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1269ed9e99e6SJeremy L Thompson 
1270ed9e99e6SJeremy L Thompson   @ref Backend
1271ed9e99e6SJeremy L Thompson **/
1272ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) {
1273ed9e99e6SJeremy L Thompson   if (!*data) return CEED_ERROR_SUCCESS;
1274ed9e99e6SJeremy L Thompson 
12752b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
12762b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&(*data)->basis_in));
12772b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&(*data)->basis_out));
12782b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_in));
12792b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_out));
12802b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*data)->B_in));
12812b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*data)->B_out));
1282ed9e99e6SJeremy L Thompson 
12832b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1284ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1285ed9e99e6SJeremy L Thompson }
1286ed9e99e6SJeremy L Thompson 
1287480fae85SJeremy L Thompson /// @}
1288480fae85SJeremy L Thompson 
1289480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1290eaf62fffSJeremy L Thompson /// CeedOperator Public API
1291eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1292eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser
1293eaf62fffSJeremy L Thompson /// @{
1294eaf62fffSJeremy L Thompson 
1295eaf62fffSJeremy L Thompson /**
1296eaf62fffSJeremy L Thompson   @brief Assemble a linear CeedQFunction associated with a CeedOperator
1297eaf62fffSJeremy L Thompson 
1298ea61e9acSJeremy L Thompson   This returns a CeedVector containing a matrix at each quadrature point providing the action of the CeedQFunction associated with the CeedOperator.
1299ea61e9acSJeremy L Thompson     The vector 'assembled' is of shape [num_elements, num_input_fields, num_output_fields, num_quad_points] and contains column-major matrices
1300ea61e9acSJeremy L Thompson representing the action of the CeedQFunction for a corresponding quadrature point on an element. Inputs and outputs are in the order provided by the
1301ea61e9acSJeremy L Thompson user when adding CeedOperator fields. For example, a CeedQFunction with inputs 'u' and 'gradu' and outputs 'gradv' and 'v', provided in that order,
1302ea61e9acSJeremy L Thompson would result in an assembled QFunction that consists of (1 + dim) x (dim + 1) matrices at each quadrature point acting on the input [u, du_0, du_1]
1303ea61e9acSJeremy L Thompson and producing the output [dv_0, dv_1, v].
1304eaf62fffSJeremy L Thompson 
1305ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1306f04ea552SJeremy L Thompson 
1307ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1308ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points
1309ea61e9acSJeremy L Thompson   @param[out] rstr      CeedElemRestriction for CeedVector containing assembled CeedQFunction
1310ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1311eaf62fffSJeremy L Thompson 
1312eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1313eaf62fffSJeremy L Thompson 
1314eaf62fffSJeremy L Thompson   @ref User
1315eaf62fffSJeremy L Thompson **/
13162b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
13172b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1318eaf62fffSJeremy L Thompson 
1319eaf62fffSJeremy L Thompson   if (op->LinearAssembleQFunction) {
1320d04bbc78SJeremy L Thompson     // Backend version
13212b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request));
1322eaf62fffSJeremy L Thompson   } else {
1323d04bbc78SJeremy L Thompson     // Operator fallback
1324d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1325d04bbc78SJeremy L Thompson 
13262b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1327d04bbc78SJeremy L Thompson     if (op_fallback) {
13282b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request));
1329d04bbc78SJeremy L Thompson     } else {
1330d04bbc78SJeremy L Thompson       // LCOV_EXCL_START
13312b730f8bSJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction");
1332d04bbc78SJeremy L Thompson       // LCOV_EXCL_STOP
1333d04bbc78SJeremy L Thompson     }
133470a7ffb3SJeremy L Thompson   }
1335eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1336eaf62fffSJeremy L Thompson }
133770a7ffb3SJeremy L Thompson 
133870a7ffb3SJeremy L Thompson /**
1339ea61e9acSJeremy L Thompson   @brief Assemble CeedQFunction and store result internally.
1340ea61e9acSJeremy L Thompson            Return copied references of stored data to the caller.
1341ea61e9acSJeremy L Thompson            Caller is responsible for ownership and destruction of the copied references.
1342ea61e9acSJeremy L Thompson            See also @ref CeedOperatorLinearAssembleQFunction
134370a7ffb3SJeremy L Thompson 
1344ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1345ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points
1346ea61e9acSJeremy L Thompson   @param[out] rstr      CeedElemRestriction for CeedVector containing assembledCeedQFunction
1347ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
134870a7ffb3SJeremy L Thompson 
134970a7ffb3SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
135070a7ffb3SJeremy L Thompson 
135170a7ffb3SJeremy L Thompson   @ref User
135270a7ffb3SJeremy L Thompson **/
13532b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
13542b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
135570a7ffb3SJeremy L Thompson 
135670a7ffb3SJeremy L Thompson   if (op->LinearAssembleQFunctionUpdate) {
1357d04bbc78SJeremy L Thompson     // Backend version
1358480fae85SJeremy L Thompson     bool                qf_assembled_is_setup;
13592efa2d85SJeremy L Thompson     CeedVector          assembled_vec  = NULL;
13602efa2d85SJeremy L Thompson     CeedElemRestriction assembled_rstr = NULL;
1361480fae85SJeremy L Thompson 
13622b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataIsSetup(op->qf_assembled, &qf_assembled_is_setup));
1363480fae85SJeremy L Thompson     if (qf_assembled_is_setup) {
1364d04bbc78SJeremy L Thompson       bool update_needed;
1365d04bbc78SJeremy L Thompson 
13662b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataGetObjects(op->qf_assembled, &assembled_vec, &assembled_rstr));
13672b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(op->qf_assembled, &update_needed));
13688b919e6bSJeremy L Thompson       if (update_needed) {
13692b730f8bSJeremy L Thompson         CeedCall(op->LinearAssembleQFunctionUpdate(op, assembled_vec, assembled_rstr, request));
13708b919e6bSJeremy L Thompson       }
137170a7ffb3SJeremy L Thompson     } else {
13722b730f8bSJeremy L Thompson       CeedCall(op->LinearAssembleQFunction(op, &assembled_vec, &assembled_rstr, request));
13732b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataSetObjects(op->qf_assembled, assembled_vec, assembled_rstr));
137470a7ffb3SJeremy L Thompson     }
13752b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, false));
13762efa2d85SJeremy L Thompson 
1377d04bbc78SJeremy L Thompson     // Copy reference from internally held copy
137870a7ffb3SJeremy L Thompson     *assembled = NULL;
137970a7ffb3SJeremy L Thompson     *rstr      = NULL;
13802b730f8bSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled));
13812b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled_vec));
13822b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr));
13832b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&assembled_rstr));
138470a7ffb3SJeremy L Thompson   } else {
1385d04bbc78SJeremy L Thompson     // Operator fallback
1386d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1387d04bbc78SJeremy L Thompson 
13882b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1389d04bbc78SJeremy L Thompson     if (op_fallback) {
13902b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request));
1391d04bbc78SJeremy L Thompson     } else {
1392d04bbc78SJeremy L Thompson       // LCOV_EXCL_START
13932b730f8bSJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate");
1394d04bbc78SJeremy L Thompson       // LCOV_EXCL_STOP
139570a7ffb3SJeremy L Thompson     }
139670a7ffb3SJeremy L Thompson   }
139770a7ffb3SJeremy L Thompson 
139870a7ffb3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1399eaf62fffSJeremy L Thompson }
1400eaf62fffSJeremy L Thompson 
1401eaf62fffSJeremy L Thompson /**
1402eaf62fffSJeremy L Thompson   @brief Assemble the diagonal of a square linear CeedOperator
1403eaf62fffSJeremy L Thompson 
1404eaf62fffSJeremy L Thompson   This overwrites a CeedVector with the diagonal of a linear CeedOperator.
1405eaf62fffSJeremy L Thompson 
1406ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1407eaf62fffSJeremy L Thompson 
1408ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1409f04ea552SJeremy L Thompson 
1410ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1411eaf62fffSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator diagonal
1412ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1413eaf62fffSJeremy L Thompson 
1414eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1415eaf62fffSJeremy L Thompson 
1416eaf62fffSJeremy L Thompson   @ref User
1417eaf62fffSJeremy L Thompson **/
14182b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
14192b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1420eaf62fffSJeremy L Thompson 
1421c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
14222b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
14232b730f8bSJeremy L Thompson   if (input_size != output_size) {
1424c9366a6bSJeremy L Thompson     // LCOV_EXCL_START
1425c9366a6bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1426c9366a6bSJeremy L Thompson     // LCOV_EXCL_STOP
14272b730f8bSJeremy L Thompson   }
1428c9366a6bSJeremy L Thompson 
1429eaf62fffSJeremy L Thompson   if (op->LinearAssembleDiagonal) {
1430d04bbc78SJeremy L Thompson     // Backend version
14312b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleDiagonal(op, assembled, request));
1432eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1433eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddDiagonal) {
1434d04bbc78SJeremy L Thompson     // Backend version with zeroing first
14352b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
14362b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1437eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1438eaf62fffSJeremy L Thompson   } else {
1439d04bbc78SJeremy L Thompson     // Operator fallback
1440d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1441d04bbc78SJeremy L Thompson 
14422b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1443d04bbc78SJeremy L Thompson     if (op_fallback) {
14442b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request));
1445eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1446eaf62fffSJeremy L Thompson     }
1447eaf62fffSJeremy L Thompson   }
1448eaf62fffSJeremy L Thompson   // Default interface implementation
14492b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
14502b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request));
1451d04bbc78SJeremy L Thompson 
1452eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1453eaf62fffSJeremy L Thompson }
1454eaf62fffSJeremy L Thompson 
1455eaf62fffSJeremy L Thompson /**
1456eaf62fffSJeremy L Thompson   @brief Assemble the diagonal of a square linear CeedOperator
1457eaf62fffSJeremy L Thompson 
1458eaf62fffSJeremy L Thompson   This sums into a CeedVector the diagonal of a linear CeedOperator.
1459eaf62fffSJeremy L Thompson 
1460ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1461eaf62fffSJeremy L Thompson 
1462ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1463f04ea552SJeremy L Thompson 
1464ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1465eaf62fffSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator diagonal
1466ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1467eaf62fffSJeremy L Thompson 
1468eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1469eaf62fffSJeremy L Thompson 
1470eaf62fffSJeremy L Thompson   @ref User
1471eaf62fffSJeremy L Thompson **/
14722b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
14732b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1474eaf62fffSJeremy L Thompson 
1475c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
14762b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
14772b730f8bSJeremy L Thompson   if (input_size != output_size) {
1478c9366a6bSJeremy L Thompson     // LCOV_EXCL_START
1479c9366a6bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1480c9366a6bSJeremy L Thompson     // LCOV_EXCL_STOP
14812b730f8bSJeremy L Thompson   }
1482c9366a6bSJeremy L Thompson 
1483eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddDiagonal) {
1484d04bbc78SJeremy L Thompson     // Backend version
14852b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1486eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1487eaf62fffSJeremy L Thompson   } else {
1488d04bbc78SJeremy L Thompson     // Operator fallback
1489d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1490d04bbc78SJeremy L Thompson 
14912b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1492d04bbc78SJeremy L Thompson     if (op_fallback) {
14932b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
1494eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1495eaf62fffSJeremy L Thompson     }
1496eaf62fffSJeremy L Thompson   }
1497eaf62fffSJeremy L Thompson   // Default interface implementation
1498eaf62fffSJeremy L Thompson   bool is_composite;
14992b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1500eaf62fffSJeremy L Thompson   if (is_composite) {
15012b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
1502eaf62fffSJeremy L Thompson   } else {
15032b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, false, assembled));
1504eaf62fffSJeremy L Thompson   }
1505d04bbc78SJeremy L Thompson 
1506d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1507eaf62fffSJeremy L Thompson }
1508eaf62fffSJeremy L Thompson 
1509eaf62fffSJeremy L Thompson /**
1510eaf62fffSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear CeedOperator
1511eaf62fffSJeremy L Thompson 
1512ea61e9acSJeremy L Thompson   This overwrites a CeedVector with the point block diagonal of a linear CeedOperator.
1513eaf62fffSJeremy L Thompson 
1514ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1515eaf62fffSJeremy L Thompson 
1516ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1517f04ea552SJeremy L Thompson 
1518ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1519ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator point block diagonal, provided in row-major form with an @a num_comp * @a num_comp
1520ea61e9acSJeremy L Thompson block at each node. The dimensions of this vector are derived from the active vector for the CeedOperator. The array has shape [nodes, component out,
1521ea61e9acSJeremy L Thompson component in].
1522ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1523eaf62fffSJeremy L Thompson 
1524eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1525eaf62fffSJeremy L Thompson 
1526eaf62fffSJeremy L Thompson   @ref User
1527eaf62fffSJeremy L Thompson **/
15282b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
15292b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1530eaf62fffSJeremy L Thompson 
1531c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
15322b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
15332b730f8bSJeremy L Thompson   if (input_size != output_size) {
1534c9366a6bSJeremy L Thompson     // LCOV_EXCL_START
1535c9366a6bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1536c9366a6bSJeremy L Thompson     // LCOV_EXCL_STOP
15372b730f8bSJeremy L Thompson   }
1538c9366a6bSJeremy L Thompson 
1539eaf62fffSJeremy L Thompson   if (op->LinearAssemblePointBlockDiagonal) {
1540d04bbc78SJeremy L Thompson     // Backend version
15412b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request));
1542eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1543eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddPointBlockDiagonal) {
1544d04bbc78SJeremy L Thompson     // Backend version with zeroing first
15452b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
15462b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
1547eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1548eaf62fffSJeremy L Thompson   } else {
1549d04bbc78SJeremy L Thompson     // Operator fallback
1550d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1551d04bbc78SJeremy L Thompson 
15522b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1553d04bbc78SJeremy L Thompson     if (op_fallback) {
15542b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request));
1555eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1556eaf62fffSJeremy L Thompson     }
1557eaf62fffSJeremy L Thompson   }
1558eaf62fffSJeremy L Thompson   // Default interface implementation
15592b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
15602b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
1561d04bbc78SJeremy L Thompson 
1562eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1563eaf62fffSJeremy L Thompson }
1564eaf62fffSJeremy L Thompson 
1565eaf62fffSJeremy L Thompson /**
1566eaf62fffSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear CeedOperator
1567eaf62fffSJeremy L Thompson 
1568ea61e9acSJeremy L Thompson   This sums into a CeedVector with the point block diagonal of a linear CeedOperator.
1569eaf62fffSJeremy L Thompson 
1570ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1571eaf62fffSJeremy L Thompson 
1572ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1573f04ea552SJeremy L Thompson 
1574ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1575ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator point block diagonal, provided in row-major form with an @a num_comp * @a num_comp
1576ea61e9acSJeremy L Thompson block at each node. The dimensions of this vector are derived from the active vector for the CeedOperator. The array has shape [nodes, component out,
1577ea61e9acSJeremy L Thompson component in].
1578ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1579eaf62fffSJeremy L Thompson 
1580eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1581eaf62fffSJeremy L Thompson 
1582eaf62fffSJeremy L Thompson   @ref User
1583eaf62fffSJeremy L Thompson **/
15842b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
15852b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1586eaf62fffSJeremy L Thompson 
1587c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
15882b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
15892b730f8bSJeremy L Thompson   if (input_size != output_size) {
1590c9366a6bSJeremy L Thompson     // LCOV_EXCL_START
1591c9366a6bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1592c9366a6bSJeremy L Thompson     // LCOV_EXCL_STOP
15932b730f8bSJeremy L Thompson   }
1594c9366a6bSJeremy L Thompson 
1595eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddPointBlockDiagonal) {
1596d04bbc78SJeremy L Thompson     // Backend version
15972b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request));
1598eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1599eaf62fffSJeremy L Thompson   } else {
1600d04bbc78SJeremy L Thompson     // Operator fallback
1601d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1602d04bbc78SJeremy L Thompson 
16032b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1604d04bbc78SJeremy L Thompson     if (op_fallback) {
16052b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request));
1606eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1607eaf62fffSJeremy L Thompson     }
1608eaf62fffSJeremy L Thompson   }
1609ea61e9acSJeremy L Thompson   // Default interface implementation
1610eaf62fffSJeremy L Thompson   bool is_composite;
16112b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1612eaf62fffSJeremy L Thompson   if (is_composite) {
16132b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
1614eaf62fffSJeremy L Thompson   } else {
16152b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, true, assembled));
1616eaf62fffSJeremy L Thompson   }
1617d04bbc78SJeremy L Thompson 
1618d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1619eaf62fffSJeremy L Thompson }
1620eaf62fffSJeremy L Thompson 
1621eaf62fffSJeremy L Thompson /**
1622eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero pattern of a linear operator.
1623eaf62fffSJeremy L Thompson 
1624ea61e9acSJeremy L Thompson    Expected to be used in conjunction with CeedOperatorLinearAssemble().
1625eaf62fffSJeremy L Thompson 
1626ea61e9acSJeremy L Thompson    The assembly routines use coordinate format, with num_entries tuples of the form (i, j, value) which indicate that value should be added to the
1627ea61e9acSJeremy L Thompson matrix in entry (i, j). Note that the (i, j) pairs are not unique and may repeat. This function returns the number of entries and their (i, j)
1628ea61e9acSJeremy L Thompson locations, while CeedOperatorLinearAssemble() provides the values in the same ordering.
1629eaf62fffSJeremy L Thompson 
1630eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
1631eaf62fffSJeremy L Thompson 
1632ea61e9acSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1633f04ea552SJeremy L Thompson 
1634eaf62fffSJeremy L Thompson    @param[in]  op          CeedOperator to assemble
1635eaf62fffSJeremy L Thompson    @param[out] num_entries Number of entries in coordinate nonzero pattern
1636eaf62fffSJeremy L Thompson    @param[out] rows        Row number for each entry
1637eaf62fffSJeremy L Thompson    @param[out] cols        Column number for each entry
1638eaf62fffSJeremy L Thompson 
1639eaf62fffSJeremy L Thompson    @ref User
1640eaf62fffSJeremy L Thompson **/
16412b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
1642eaf62fffSJeremy L Thompson   CeedInt       num_suboperators, single_entries;
1643eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
1644eaf62fffSJeremy L Thompson   bool          is_composite;
16452b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1646eaf62fffSJeremy L Thompson 
1647eaf62fffSJeremy L Thompson   if (op->LinearAssembleSymbolic) {
1648d04bbc78SJeremy L Thompson     // Backend version
16492b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols));
1650eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1651eaf62fffSJeremy L Thompson   } else {
1652d04bbc78SJeremy L Thompson     // Operator fallback
1653d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1654d04bbc78SJeremy L Thompson 
16552b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1656d04bbc78SJeremy L Thompson     if (op_fallback) {
16572b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols));
1658eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1659eaf62fffSJeremy L Thompson     }
1660eaf62fffSJeremy L Thompson   }
1661eaf62fffSJeremy L Thompson 
1662eaf62fffSJeremy L Thompson   // Default interface implementation
1663eaf62fffSJeremy L Thompson 
1664eaf62fffSJeremy L Thompson   // count entries and allocate rows, cols arrays
16652b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1666eaf62fffSJeremy L Thompson   *num_entries = 0;
1667eaf62fffSJeremy L Thompson   if (is_composite) {
16682b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumSub(op, &num_suboperators));
16692b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetSubList(op, &sub_operators));
167092ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
16712b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
1672eaf62fffSJeremy L Thompson       *num_entries += single_entries;
1673eaf62fffSJeremy L Thompson     }
1674eaf62fffSJeremy L Thompson   } else {
16752b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries));
1676eaf62fffSJeremy L Thompson     *num_entries += single_entries;
1677eaf62fffSJeremy L Thompson   }
16782b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, rows));
16792b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, cols));
1680eaf62fffSJeremy L Thompson 
1681eaf62fffSJeremy L Thompson   // assemble nonzero locations
1682eaf62fffSJeremy L Thompson   CeedInt offset = 0;
1683eaf62fffSJeremy L Thompson   if (is_composite) {
16842b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumSub(op, &num_suboperators));
16852b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetSubList(op, &sub_operators));
168692ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
16872b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols));
16882b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
1689eaf62fffSJeremy L Thompson       offset += single_entries;
1690eaf62fffSJeremy L Thompson     }
1691eaf62fffSJeremy L Thompson   } else {
16922b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols));
1693eaf62fffSJeremy L Thompson   }
1694eaf62fffSJeremy L Thompson 
1695eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1696eaf62fffSJeremy L Thompson }
1697eaf62fffSJeremy L Thompson 
1698eaf62fffSJeremy L Thompson /**
1699eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero entries of a linear operator.
1700eaf62fffSJeremy L Thompson 
1701ea61e9acSJeremy L Thompson    Expected to be used in conjunction with CeedOperatorLinearAssembleSymbolic().
1702eaf62fffSJeremy L Thompson 
1703ea61e9acSJeremy L Thompson    The assembly routines use coordinate format, with num_entries tuples of the form (i, j, value) which indicate that value should be added to the
1704ea61e9acSJeremy L Thompson matrix in entry (i, j). Note that the (i, j) pairs are not unique and may repeat. This function returns the values of the nonzero entries to be added,
1705ea61e9acSJeremy L Thompson their (i, j) locations are provided by CeedOperatorLinearAssembleSymbolic()
1706eaf62fffSJeremy L Thompson 
1707eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
1708eaf62fffSJeremy L Thompson 
1709ea61e9acSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1710f04ea552SJeremy L Thompson 
1711eaf62fffSJeremy L Thompson    @param[in]  op     CeedOperator to assemble
1712eaf62fffSJeremy L Thompson    @param[out] values Values to assemble into matrix
1713eaf62fffSJeremy L Thompson 
1714eaf62fffSJeremy L Thompson    @ref User
1715eaf62fffSJeremy L Thompson **/
1716eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) {
1717eaf62fffSJeremy L Thompson   CeedInt       num_suboperators, single_entries = 0;
1718eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
17192b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1720eaf62fffSJeremy L Thompson 
1721eaf62fffSJeremy L Thompson   if (op->LinearAssemble) {
1722d04bbc78SJeremy L Thompson     // Backend version
17232b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemble(op, values));
1724eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1725eaf62fffSJeremy L Thompson   } else {
1726d04bbc78SJeremy L Thompson     // Operator fallback
1727d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1728d04bbc78SJeremy L Thompson 
17292b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1730d04bbc78SJeremy L Thompson     if (op_fallback) {
17312b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemble(op_fallback, values));
1732eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1733eaf62fffSJeremy L Thompson     }
1734eaf62fffSJeremy L Thompson   }
1735eaf62fffSJeremy L Thompson 
1736eaf62fffSJeremy L Thompson   // Default interface implementation
1737eaf62fffSJeremy L Thompson   bool is_composite;
17382b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1739eaf62fffSJeremy L Thompson 
1740eaf62fffSJeremy L Thompson   CeedInt offset = 0;
1741eaf62fffSJeremy L Thompson   if (is_composite) {
17422b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumSub(op, &num_suboperators));
17432b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetSubList(op, &sub_operators));
1744cefa2673SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; k++) {
17452b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values));
17462b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
1747eaf62fffSJeremy L Thompson       offset += single_entries;
1748eaf62fffSJeremy L Thompson     }
1749eaf62fffSJeremy L Thompson   } else {
17502b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemble(op, offset, values));
1751eaf62fffSJeremy L Thompson   }
1752eaf62fffSJeremy L Thompson 
1753eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1754eaf62fffSJeremy L Thompson }
1755eaf62fffSJeremy L Thompson 
1756eaf62fffSJeremy L Thompson /**
1757*75f0d5a4SJeremy L Thompson   @brief Get the multiplicity of nodes across suboperators in a composite CeedOperator
1758*75f0d5a4SJeremy L Thompson 
1759*75f0d5a4SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1760*75f0d5a4SJeremy L Thompson 
1761*75f0d5a4SJeremy L Thompson   @param[in]  op               Composite CeedOperator
1762*75f0d5a4SJeremy L Thompson   @param[in]  num_skip_indices Number of suboperators to skip
1763*75f0d5a4SJeremy L Thompson   @param[in]  skip_indices     Array of indices of suboperators to skip
1764*75f0d5a4SJeremy L Thompson   @param[out] mult             Vector to store multiplicity (of size l_size)
1765*75f0d5a4SJeremy L Thompson 
1766*75f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1767*75f0d5a4SJeremy L Thompson 
1768*75f0d5a4SJeremy L Thompson   @ref User
1769*75f0d5a4SJeremy L Thompson **/
1770*75f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) {
1771*75f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1772*75f0d5a4SJeremy L Thompson 
1773*75f0d5a4SJeremy L Thompson   Ceed                ceed;
1774*75f0d5a4SJeremy L Thompson   CeedInt             num_sub_ops;
1775*75f0d5a4SJeremy L Thompson   CeedSize            l_vec_len;
1776*75f0d5a4SJeremy L Thompson   CeedScalar         *mult_array;
1777*75f0d5a4SJeremy L Thompson   CeedVector          ones_l_vec;
1778*75f0d5a4SJeremy L Thompson   CeedElemRestriction elem_restr;
1779*75f0d5a4SJeremy L Thompson   CeedOperator       *sub_ops;
1780*75f0d5a4SJeremy L Thompson 
1781*75f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1782*75f0d5a4SJeremy L Thompson 
1783*75f0d5a4SJeremy L Thompson   // Zero mult vector
1784*75f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
1785*75f0d5a4SJeremy L Thompson 
1786*75f0d5a4SJeremy L Thompson   // Get suboperators
1787*75f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorGetNumSub(op, &num_sub_ops));
1788*75f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorGetSubList(op, &sub_ops));
1789*75f0d5a4SJeremy L Thompson   if (num_sub_ops == 0) return CEED_ERROR_SUCCESS;
1790*75f0d5a4SJeremy L Thompson 
1791*75f0d5a4SJeremy L Thompson   // Work vector
1792*75f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetLength(mult, &l_vec_len));
1793*75f0d5a4SJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec));
1794*75f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(ones_l_vec, 1.0));
1795*75f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array));
1796*75f0d5a4SJeremy L Thompson 
1797*75f0d5a4SJeremy L Thompson   // Compute multiplicity across suboperators
1798*75f0d5a4SJeremy L Thompson   for (CeedInt i = 0; i < num_sub_ops; i++) {
1799*75f0d5a4SJeremy L Thompson     const CeedScalar *sub_mult_array;
1800*75f0d5a4SJeremy L Thompson     CeedVector        sub_mult_l_vec, ones_e_vec;
1801*75f0d5a4SJeremy L Thompson 
1802*75f0d5a4SJeremy L Thompson     // -- Check for suboperator to skip
1803*75f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < num_skip_indices; j++) {
1804*75f0d5a4SJeremy L Thompson       if (skip_indices[j] == i) continue;
1805*75f0d5a4SJeremy L Thompson     }
1806*75f0d5a4SJeremy L Thompson 
1807*75f0d5a4SJeremy L Thompson     // -- Sub operator multiplicity
1808*75f0d5a4SJeremy L Thompson     CeedCall(CeedOperatorGetActiveElemRestriction(sub_ops[i], &elem_restr));
1809*75f0d5a4SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(elem_restr, &sub_mult_l_vec, &ones_e_vec));
1810*75f0d5a4SJeremy L Thompson     CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0));
1811*75f0d5a4SJeremy L Thompson     CeedCall(CeedElemRestrictionApply(elem_restr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE));
1812*75f0d5a4SJeremy L Thompson     CeedCall(CeedElemRestrictionApply(elem_restr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE));
1813*75f0d5a4SJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array));
1814*75f0d5a4SJeremy L Thompson     // ---- Flag every node present in the current suboperator
1815*75f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < l_vec_len; j++) {
1816*75f0d5a4SJeremy L Thompson       if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0;
1817*75f0d5a4SJeremy L Thompson     }
1818*75f0d5a4SJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array));
1819*75f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&sub_mult_l_vec));
1820*75f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&ones_e_vec));
1821*75f0d5a4SJeremy L Thompson   }
1822*75f0d5a4SJeremy L Thompson   CeedCall(CeedVectorRestoreArray(mult, &mult_array));
1823*75f0d5a4SJeremy L Thompson 
1824*75f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1825*75f0d5a4SJeremy L Thompson }
1826*75f0d5a4SJeremy L Thompson 
1827*75f0d5a4SJeremy L Thompson /**
1828ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator, creating the prolongation basis from the fine and coarse
1829ea61e9acSJeremy L Thompson grid interpolation
1830eaf62fffSJeremy L Thompson 
1831ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1832f04ea552SJeremy L Thompson 
1833eaf62fffSJeremy L Thompson   @param[in]  op_fine      Fine grid operator
1834eaf62fffSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter
1835eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid restriction
1836eaf62fffSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector basis
1837eaf62fffSJeremy L Thompson   @param[out] op_coarse    Coarse grid operator
1838eaf62fffSJeremy L Thompson   @param[out] op_prolong   Coarse to fine operator
1839eaf62fffSJeremy L Thompson   @param[out] op_restrict  Fine to coarse operator
1840eaf62fffSJeremy L Thompson 
1841eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1842eaf62fffSJeremy L Thompson 
1843eaf62fffSJeremy L Thompson   @ref User
1844eaf62fffSJeremy L Thompson **/
18452b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
18462b730f8bSJeremy L Thompson                                      CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
18472b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
1848eaf62fffSJeremy L Thompson 
1849f113e5dcSJeremy L Thompson   // Build prolongation matrix
1850f113e5dcSJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f;
18512b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
18522b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f));
1853eaf62fffSJeremy L Thompson 
1854f113e5dcSJeremy L Thompson   // Core code
18552b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
1856f113e5dcSJeremy L Thompson 
1857eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1858eaf62fffSJeremy L Thompson }
1859eaf62fffSJeremy L Thompson 
1860eaf62fffSJeremy L Thompson /**
1861ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a tensor basis for the active basis
1862eaf62fffSJeremy L Thompson 
1863ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1864f04ea552SJeremy L Thompson 
1865eaf62fffSJeremy L Thompson   @param[in]  op_fine       Fine grid operator
1866eaf62fffSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter
1867eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid restriction
1868eaf62fffSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector basis
1869eaf62fffSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation
1870eaf62fffSJeremy L Thompson   @param[out] op_coarse     Coarse grid operator
1871eaf62fffSJeremy L Thompson   @param[out] op_prolong    Coarse to fine operator
1872eaf62fffSJeremy L Thompson   @param[out] op_restrict   Fine to coarse operator
1873eaf62fffSJeremy L Thompson 
1874eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1875eaf62fffSJeremy L Thompson 
1876eaf62fffSJeremy L Thompson   @ref User
1877eaf62fffSJeremy L Thompson **/
18782b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
18792b730f8bSJeremy L Thompson                                              const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
18802b730f8bSJeremy L Thompson                                              CeedOperator *op_restrict) {
18812b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
1882eaf62fffSJeremy L Thompson   Ceed ceed;
18832b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
1884eaf62fffSJeremy L Thompson 
1885eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
1886eaf62fffSJeremy L Thompson   CeedBasis basis_fine;
18872b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
1888eaf62fffSJeremy L Thompson   CeedInt Q_f, Q_c;
18892b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
18902b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
18912b730f8bSJeremy L Thompson   if (Q_f != Q_c) {
1892eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
18932b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
1894eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
18952b730f8bSJeremy L Thompson   }
1896eaf62fffSJeremy L Thompson 
1897eaf62fffSJeremy L Thompson   // Coarse to fine basis
1898eaf62fffSJeremy L Thompson   CeedInt dim, num_comp, num_nodes_c, P_1d_f, P_1d_c;
18992b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis_fine, &dim));
19002b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
19012b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f));
19022b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
19032b730f8bSJeremy L Thompson   P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c);
1904eaf62fffSJeremy L Thompson   CeedScalar *q_ref, *q_weight, *grad;
19052b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d_f, &q_ref));
19062b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d_f, &q_weight));
19072b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad));
1908eaf62fffSJeremy L Thompson   CeedBasis basis_c_to_f;
19092b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_c, P_1d_f, interp_c_to_f, grad, q_ref, q_weight, &basis_c_to_f));
19102b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref));
19112b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight));
19122b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad));
1913eaf62fffSJeremy L Thompson 
1914eaf62fffSJeremy L Thompson   // Core code
19152b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
1916eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1917eaf62fffSJeremy L Thompson }
1918eaf62fffSJeremy L Thompson 
1919eaf62fffSJeremy L Thompson /**
1920ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a non-tensor basis for the active vector
1921eaf62fffSJeremy L Thompson 
1922ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1923f04ea552SJeremy L Thompson 
1924eaf62fffSJeremy L Thompson   @param[in]  op_fine       Fine grid operator
1925eaf62fffSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter
1926eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid restriction
1927eaf62fffSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector basis
1928eaf62fffSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation
1929eaf62fffSJeremy L Thompson   @param[out] op_coarse     Coarse grid operator
1930eaf62fffSJeremy L Thompson   @param[out] op_prolong    Coarse to fine operator
1931eaf62fffSJeremy L Thompson   @param[out] op_restrict   Fine to coarse operator
1932eaf62fffSJeremy L Thompson 
1933eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1934eaf62fffSJeremy L Thompson 
1935eaf62fffSJeremy L Thompson   @ref User
1936eaf62fffSJeremy L Thompson **/
19372b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
19382b730f8bSJeremy L Thompson                                        const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
1939eaf62fffSJeremy L Thompson                                        CeedOperator *op_restrict) {
19402b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
1941eaf62fffSJeremy L Thompson   Ceed ceed;
19422b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
1943eaf62fffSJeremy L Thompson 
1944eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
1945eaf62fffSJeremy L Thompson   CeedBasis basis_fine;
19462b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
1947eaf62fffSJeremy L Thompson   CeedInt Q_f, Q_c;
19482b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
19492b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
19502b730f8bSJeremy L Thompson   if (Q_f != Q_c) {
1951eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
19522b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
1953eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
19542b730f8bSJeremy L Thompson   }
1955eaf62fffSJeremy L Thompson 
1956eaf62fffSJeremy L Thompson   // Coarse to fine basis
1957eaf62fffSJeremy L Thompson   CeedElemTopology topo;
19582b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetTopology(basis_fine, &topo));
1959eaf62fffSJeremy L Thompson   CeedInt dim, num_comp, num_nodes_c, num_nodes_f;
19602b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis_fine, &dim));
19612b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
19622b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f));
19632b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
1964eaf62fffSJeremy L Thompson   CeedScalar *q_ref, *q_weight, *grad;
19652b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref));
19662b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_nodes_f, &q_weight));
19672b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad));
1968eaf62fffSJeremy L Thompson   CeedBasis basis_c_to_f;
19692b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_c, num_nodes_f, interp_c_to_f, grad, q_ref, q_weight, &basis_c_to_f));
19702b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref));
19712b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight));
19722b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad));
1973eaf62fffSJeremy L Thompson 
1974eaf62fffSJeremy L Thompson   // Core code
19752b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
1976eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1977eaf62fffSJeremy L Thompson }
1978eaf62fffSJeremy L Thompson 
1979eaf62fffSJeremy L Thompson /**
1980ea61e9acSJeremy L Thompson   @brief Build a FDM based approximate inverse for each element for a CeedOperator
1981eaf62fffSJeremy L Thompson 
1982ea61e9acSJeremy L Thompson   This returns a CeedOperator and CeedVector to apply a Fast Diagonalization Method based approximate inverse.
1983ea61e9acSJeremy L Thompson     This function obtains the simultaneous diagonalization for the 1D mass and Laplacian operators, M = V^T V, K = V^T S V.
1984ea61e9acSJeremy L Thompson     The assembled QFunction is used to modify the eigenvalues from simultaneous diagonalization and obtain an approximate inverse of the form V^T
1985ea61e9acSJeremy L Thompson S^hat V. The CeedOperator must be linear and non-composite. The associated CeedQFunction must therefore also be linear.
1986eaf62fffSJeremy L Thompson 
1987ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1988f04ea552SJeremy L Thompson 
1989ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to create element inverses
1990ea61e9acSJeremy L Thompson   @param[out] fdm_inv CeedOperator to apply the action of a FDM based inverse for each element
1991ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1992eaf62fffSJeremy L Thompson 
1993eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1994eaf62fffSJeremy L Thompson 
1995480fae85SJeremy L Thompson   @ref User
1996eaf62fffSJeremy L Thompson **/
19972b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) {
19982b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1999eaf62fffSJeremy L Thompson 
2000eaf62fffSJeremy L Thompson   if (op->CreateFDMElementInverse) {
2001d04bbc78SJeremy L Thompson     // Backend version
20022b730f8bSJeremy L Thompson     CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request));
2003eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2004eaf62fffSJeremy L Thompson   } else {
2005d04bbc78SJeremy L Thompson     // Operator fallback
2006d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2007d04bbc78SJeremy L Thompson 
20082b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2009d04bbc78SJeremy L Thompson     if (op_fallback) {
20102b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request));
2011eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2012eaf62fffSJeremy L Thompson     }
2013eaf62fffSJeremy L Thompson   }
2014eaf62fffSJeremy L Thompson 
2015d04bbc78SJeremy L Thompson   // Default interface implementation
2016eaf62fffSJeremy L Thompson   Ceed ceed, ceed_parent;
20172b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
20182b730f8bSJeremy L Thompson   CeedCall(CeedGetOperatorFallbackParentCeed(ceed, &ceed_parent));
2019eaf62fffSJeremy L Thompson   ceed_parent = ceed_parent ? ceed_parent : ceed;
2020eaf62fffSJeremy L Thompson   CeedQFunction qf;
20212b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
2022eaf62fffSJeremy L Thompson 
2023eaf62fffSJeremy L Thompson   // Determine active input basis
2024eaf62fffSJeremy L Thompson   bool                interp = false, grad = false;
2025eaf62fffSJeremy L Thompson   CeedBasis           basis = NULL;
2026eaf62fffSJeremy L Thompson   CeedElemRestriction rstr  = NULL;
2027eaf62fffSJeremy L Thompson   CeedOperatorField  *op_fields;
2028eaf62fffSJeremy L Thompson   CeedQFunctionField *qf_fields;
2029eaf62fffSJeremy L Thompson   CeedInt             num_input_fields;
20302b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL));
20312b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
2032eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
2033eaf62fffSJeremy L Thompson     CeedVector vec;
20342b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
2035eaf62fffSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2036eaf62fffSJeremy L Thompson       CeedEvalMode eval_mode;
20372b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
2038eaf62fffSJeremy L Thompson       interp = interp || eval_mode == CEED_EVAL_INTERP;
2039eaf62fffSJeremy L Thompson       grad   = grad || eval_mode == CEED_EVAL_GRAD;
20402b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis));
20412b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
2042eaf62fffSJeremy L Thompson     }
2043eaf62fffSJeremy L Thompson   }
20442b730f8bSJeremy L Thompson   if (!basis) {
2045eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
2046eaf62fffSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "No active field set");
2047eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
20482b730f8bSJeremy L Thompson   }
2049e79b91d9SJeremy L Thompson   CeedSize l_size = 1;
2050e79b91d9SJeremy L Thompson   CeedInt  P_1d, Q_1d, elem_size, num_qpts, dim, num_comp = 1, num_elem = 1;
20512b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
20522b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis, &elem_size));
20532b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
20542b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
20552b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
20562b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
20572b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
20582b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
2059eaf62fffSJeremy L Thompson 
2060eaf62fffSJeremy L Thompson   // Build and diagonalize 1D Mass and Laplacian
2061eaf62fffSJeremy L Thompson   bool tensor_basis;
20622b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &tensor_basis));
20632b730f8bSJeremy L Thompson   if (!tensor_basis) {
2064eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
20652b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases");
2066eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
20672b730f8bSJeremy L Thompson   }
2068eaf62fffSJeremy L Thompson   CeedScalar *mass, *laplace, *x, *fdm_interp, *lambda;
20692b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &mass));
20702b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &laplace));
20712b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &x));
20722b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp));
20732b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &lambda));
2074eaf62fffSJeremy L Thompson   // -- Build matrices
2075eaf62fffSJeremy L Thompson   const CeedScalar *interp_1d, *grad_1d, *q_weight_1d;
20762b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
20772b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
20782b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
20792b730f8bSJeremy L Thompson   CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace));
2080eaf62fffSJeremy L Thompson 
2081eaf62fffSJeremy L Thompson   // -- Diagonalize
20822b730f8bSJeremy L Thompson   CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d));
20832b730f8bSJeremy L Thompson   CeedCall(CeedFree(&mass));
20842b730f8bSJeremy L Thompson   CeedCall(CeedFree(&laplace));
20852b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
20862b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d];
20872b730f8bSJeremy L Thompson   }
20882b730f8bSJeremy L Thompson   CeedCall(CeedFree(&x));
2089eaf62fffSJeremy L Thompson 
2090eaf62fffSJeremy L Thompson   // Assemble QFunction
2091eaf62fffSJeremy L Thompson   CeedVector          assembled;
2092eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_qf;
20932b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request));
2094eaf62fffSJeremy L Thompson   CeedInt layout[3];
20952b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_qf, &layout));
20962b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qf));
2097eaf62fffSJeremy L Thompson   CeedScalar max_norm = 0;
20982b730f8bSJeremy L Thompson   CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm));
2099eaf62fffSJeremy L Thompson 
2100eaf62fffSJeremy L Thompson   // Calculate element averages
2101eaf62fffSJeremy L Thompson   CeedInt           num_modes = (interp ? 1 : 0) + (grad ? dim : 0);
2102eaf62fffSJeremy L Thompson   CeedScalar       *elem_avg;
2103eaf62fffSJeremy L Thompson   const CeedScalar *assembled_array, *q_weight_array;
2104eaf62fffSJeremy L Thompson   CeedVector        q_weight;
21052b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight));
21062b730f8bSJeremy L Thompson   CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight));
21072b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array));
21082b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array));
21092b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_elem, &elem_avg));
2110eaf62fffSJeremy L Thompson   const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON;
2111eaf62fffSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
2112eaf62fffSJeremy L Thompson     CeedInt count = 0;
21132b730f8bSJeremy L Thompson     for (CeedInt q = 0; q < num_qpts; q++) {
21142b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) {
21152b730f8bSJeremy L Thompson         if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) {
21162b730f8bSJeremy L Thompson           elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q];
2117eaf62fffSJeremy L Thompson           count++;
2118eaf62fffSJeremy L Thompson         }
21192b730f8bSJeremy L Thompson       }
21202b730f8bSJeremy L Thompson     }
2121eaf62fffSJeremy L Thompson     if (count) {
2122eaf62fffSJeremy L Thompson       elem_avg[e] /= count;
2123eaf62fffSJeremy L Thompson     } else {
2124eaf62fffSJeremy L Thompson       elem_avg[e] = 1.0;
2125eaf62fffSJeremy L Thompson     }
2126eaf62fffSJeremy L Thompson   }
21272b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array));
21282b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled));
21292b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array));
21302b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_weight));
2131eaf62fffSJeremy L Thompson 
2132eaf62fffSJeremy L Thompson   // Build FDM diagonal
2133eaf62fffSJeremy L Thompson   CeedVector  q_data;
2134eaf62fffSJeremy L Thompson   CeedScalar *q_data_array, *fdm_diagonal;
21352b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_comp * elem_size, &fdm_diagonal));
2136eaf62fffSJeremy L Thompson   const CeedScalar fdm_diagonal_bound = elem_size * CEED_EPSILON;
21372b730f8bSJeremy L Thompson   for (CeedInt c = 0; c < num_comp; c++) {
2138eaf62fffSJeremy L Thompson     for (CeedInt n = 0; n < elem_size; n++) {
21392b730f8bSJeremy L Thompson       if (interp) fdm_diagonal[c * elem_size + n] = 1.0;
21402b730f8bSJeremy L Thompson       if (grad) {
2141eaf62fffSJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
2142eaf62fffSJeremy L Thompson           CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d;
2143eaf62fffSJeremy L Thompson           fdm_diagonal[c * elem_size + n] += lambda[i];
2144eaf62fffSJeremy L Thompson         }
2145eaf62fffSJeremy L Thompson       }
21462b730f8bSJeremy L Thompson       if (fabs(fdm_diagonal[c * elem_size + n]) < fdm_diagonal_bound) fdm_diagonal[c * elem_size + n] = fdm_diagonal_bound;
21472b730f8bSJeremy L Thompson     }
21482b730f8bSJeremy L Thompson   }
21492b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * elem_size, &q_data));
21502b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(q_data, 0.0));
21512b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array));
21522b730f8bSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
21532b730f8bSJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
21542b730f8bSJeremy L Thompson       for (CeedInt n = 0; n < elem_size; n++) q_data_array[(e * num_comp + c) * elem_size + n] = 1. / (elem_avg[e] * fdm_diagonal[c * elem_size + n]);
21552b730f8bSJeremy L Thompson     }
21562b730f8bSJeremy L Thompson   }
21572b730f8bSJeremy L Thompson   CeedCall(CeedFree(&elem_avg));
21582b730f8bSJeremy L Thompson   CeedCall(CeedFree(&fdm_diagonal));
21592b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(q_data, &q_data_array));
2160eaf62fffSJeremy L Thompson 
2161eaf62fffSJeremy L Thompson   // Setup FDM operator
2162eaf62fffSJeremy L Thompson   // -- Basis
2163eaf62fffSJeremy L Thompson   CeedBasis   fdm_basis;
2164eaf62fffSJeremy L Thompson   CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy;
21652b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy));
21662b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &q_ref_dummy));
21672b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &q_weight_dummy));
21682b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateTensorH1(ceed_parent, dim, num_comp, P_1d, P_1d, fdm_interp, grad_dummy, q_ref_dummy, q_weight_dummy, &fdm_basis));
21692b730f8bSJeremy L Thompson   CeedCall(CeedFree(&fdm_interp));
21702b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_dummy));
21712b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref_dummy));
21722b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight_dummy));
21732b730f8bSJeremy L Thompson   CeedCall(CeedFree(&lambda));
2174eaf62fffSJeremy L Thompson 
2175eaf62fffSJeremy L Thompson   // -- Restriction
2176eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_qd_i;
2177eaf62fffSJeremy L Thompson   CeedInt             strides[3] = {1, elem_size, elem_size * num_comp};
21782b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, elem_size, num_comp, num_elem * num_comp * elem_size, strides, &rstr_qd_i));
2179eaf62fffSJeremy L Thompson   // -- QFunction
2180eaf62fffSJeremy L Thompson   CeedQFunction qf_fdm;
21812b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm));
21822b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP));
21832b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE));
21842b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP));
21852b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp));
2186eaf62fffSJeremy L Thompson   // -- QFunction context
2187eaf62fffSJeremy L Thompson   CeedInt *num_comp_data;
21882b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, &num_comp_data));
2189eaf62fffSJeremy L Thompson   num_comp_data[0] = num_comp;
2190eaf62fffSJeremy L Thompson   CeedQFunctionContext ctx_fdm;
21912b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm));
21922b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data));
21932b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm));
21942b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_fdm));
2195eaf62fffSJeremy L Thompson   // -- Operator
21962b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv));
21972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
21982b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_COLLOCATED, q_data));
21992b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2200eaf62fffSJeremy L Thompson 
2201eaf62fffSJeremy L Thompson   // Cleanup
22022b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_data));
22032b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&fdm_basis));
22042b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i));
22052b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_fdm));
2206eaf62fffSJeremy L Thompson 
2207eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2208eaf62fffSJeremy L Thompson }
2209eaf62fffSJeremy L Thompson 
2210eaf62fffSJeremy L Thompson /// @}
2211