xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-preconditioning.c (revision c5d0f99595ca9b554c7cfa8252936d76dafb0ce5)
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) {
83b275c451SJeremy L Thompson   bool is_composite;
849e77b9c8SJeremy L Thompson   Ceed ceed_fallback;
85eaf62fffSJeremy L Thompson 
86805fe78eSJeremy L Thompson   // Check not already created
87805fe78eSJeremy L Thompson   if (op->op_fallback) return CEED_ERROR_SUCCESS;
88805fe78eSJeremy L Thompson 
89eaf62fffSJeremy L Thompson   // Fallback Ceed
902b730f8bSJeremy L Thompson   CeedCall(CeedGetOperatorFallbackCeed(op->ceed, &ceed_fallback));
91d04bbc78SJeremy L Thompson   if (!ceed_fallback) return CEED_ERROR_SUCCESS;
92d04bbc78SJeremy L Thompson 
93d04bbc78SJeremy L Thompson   CeedDebug256(op->ceed, 1, "---------- CeedOperator Fallback ----------\n");
9413f886e9SJeremy L Thompson   CeedDebug(op->ceed, "Creating fallback CeedOperator\n");
95eaf62fffSJeremy L Thompson 
96eaf62fffSJeremy L Thompson   // Clone Op
97805fe78eSJeremy L Thompson   CeedOperator op_fallback;
98b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
99b275c451SJeremy L Thompson   if (is_composite) {
100b275c451SJeremy L Thompson     CeedInt       num_suboperators;
101b275c451SJeremy L Thompson     CeedOperator *sub_operators;
102b275c451SJeremy L Thompson 
1032b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorCreate(ceed_fallback, &op_fallback));
104b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
105b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
106b275c451SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
107d04bbc78SJeremy L Thompson       CeedOperator op_sub_fallback;
108d04bbc78SJeremy L Thompson 
109b275c451SJeremy L Thompson       CeedCall(CeedOperatorGetFallback(sub_operators[i], &op_sub_fallback));
1102b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorAddSub(op_fallback, op_sub_fallback));
111805fe78eSJeremy L Thompson     }
112805fe78eSJeremy L Thompson   } else {
1139e77b9c8SJeremy L Thompson     CeedQFunction qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL;
1142b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback));
1152b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback));
1162b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback));
1172b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback));
118805fe78eSJeremy L Thompson     for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
119437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, op->input_fields[i]->field_name, op->input_fields[i]->elem_rstr, op->input_fields[i]->basis,
1202b730f8bSJeremy L Thompson                                     op->input_fields[i]->vec));
121805fe78eSJeremy L Thompson     }
122805fe78eSJeremy L Thompson     for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
123437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, op->output_fields[i]->field_name, op->output_fields[i]->elem_rstr, op->output_fields[i]->basis,
1242b730f8bSJeremy L Thompson                                     op->output_fields[i]->vec));
125805fe78eSJeremy L Thompson     }
1262b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op->qf_assembled, &op_fallback->qf_assembled));
127805fe78eSJeremy L Thompson     if (op_fallback->num_qpts == 0) {
1282b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetNumQuadraturePoints(op_fallback, op->num_qpts));
129805fe78eSJeremy L Thompson     }
1309e77b9c8SJeremy L Thompson     // Cleanup
1312b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf_fallback));
1322b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqf_fallback));
1332b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqfT_fallback));
134805fe78eSJeremy L Thompson   }
1352b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetName(op_fallback, op->name));
1362b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fallback));
137805fe78eSJeremy L Thompson   op->op_fallback = op_fallback;
138eaf62fffSJeremy L Thompson 
139eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
140eaf62fffSJeremy L Thompson }
141eaf62fffSJeremy L Thompson 
142eaf62fffSJeremy L Thompson /**
143ea61e9acSJeremy L Thompson   @brief Retrieve fallback CeedOperator with a reference Ceed for advanced CeedOperator functionality
144d04bbc78SJeremy L Thompson 
145d04bbc78SJeremy L Thompson   @param[in]  op          CeedOperator to retrieve fallback for
146d04bbc78SJeremy L Thompson   @param[out] op_fallback Fallback CeedOperator
147d04bbc78SJeremy L Thompson 
148d04bbc78SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
149d04bbc78SJeremy L Thompson 
150d04bbc78SJeremy L Thompson   @ref Developer
151d04bbc78SJeremy L Thompson **/
152d04bbc78SJeremy L Thompson int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) {
153d04bbc78SJeremy L Thompson   // Create if needed
154d04bbc78SJeremy L Thompson   if (!op->op_fallback) {
1552b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreateFallback(op));
156d04bbc78SJeremy L Thompson   }
157d04bbc78SJeremy L Thompson   if (op->op_fallback) {
158d04bbc78SJeremy L Thompson     bool is_debug;
159d04bbc78SJeremy L Thompson 
1602b730f8bSJeremy L Thompson     CeedCall(CeedIsDebug(op->ceed, &is_debug));
161d04bbc78SJeremy L Thompson     if (is_debug) {
162b275c451SJeremy L Thompson       Ceed        ceed, ceed_fallback;
163d04bbc78SJeremy L Thompson       const char *resource, *resource_fallback;
164d04bbc78SJeremy L Thompson 
165b275c451SJeremy L Thompson       CeedCall(CeedOperatorGetCeed(op, &ceed));
166b275c451SJeremy L Thompson       CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
167b275c451SJeremy L Thompson       CeedCall(CeedGetResource(ceed, &resource));
1682b730f8bSJeremy L Thompson       CeedCall(CeedGetResource(ceed_fallback, &resource_fallback));
169d04bbc78SJeremy L Thompson 
170b275c451SJeremy L Thompson       CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n");
171b275c451SJeremy L Thompson       CeedDebug(ceed, "Falling back from %s operator at address %ld to %s operator at address %ld\n", resource, op, resource_fallback,
1722b730f8bSJeremy L Thompson                 op->op_fallback);
173d04bbc78SJeremy L Thompson     }
174d04bbc78SJeremy L Thompson   }
175d04bbc78SJeremy L Thompson   *op_fallback = op->op_fallback;
176d04bbc78SJeremy L Thompson 
177d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
178d04bbc78SJeremy L Thompson }
179d04bbc78SJeremy L Thompson 
180d04bbc78SJeremy L Thompson /**
181eaf62fffSJeremy L Thompson   @brief Select correct basis matrix pointer based on CeedEvalMode
182eaf62fffSJeremy L Thompson 
183eaf62fffSJeremy L Thompson   @param[in]  eval_mode Current basis evaluation mode
184eaf62fffSJeremy L Thompson   @param[in]  identity  Pointer to identity matrix
185eaf62fffSJeremy L Thompson   @param[in]  interp    Pointer to interpolation matrix
186eaf62fffSJeremy L Thompson   @param[in]  grad      Pointer to gradient matrix
187eaf62fffSJeremy L Thompson   @param[out] basis_ptr Basis pointer to set
188eaf62fffSJeremy L Thompson 
189eaf62fffSJeremy L Thompson   @ref Developer
190eaf62fffSJeremy L Thompson **/
1912b730f8bSJeremy L Thompson static inline void CeedOperatorGetBasisPointer(CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar *interp, const CeedScalar *grad,
1922b730f8bSJeremy L Thompson                                                const CeedScalar **basis_ptr) {
193eaf62fffSJeremy L Thompson   switch (eval_mode) {
194eaf62fffSJeremy L Thompson     case CEED_EVAL_NONE:
195eaf62fffSJeremy L Thompson       *basis_ptr = identity;
196eaf62fffSJeremy L Thompson       break;
197eaf62fffSJeremy L Thompson     case CEED_EVAL_INTERP:
198eaf62fffSJeremy L Thompson       *basis_ptr = interp;
199eaf62fffSJeremy L Thompson       break;
200eaf62fffSJeremy L Thompson     case CEED_EVAL_GRAD:
201eaf62fffSJeremy L Thompson       *basis_ptr = grad;
202eaf62fffSJeremy L Thompson       break;
203eaf62fffSJeremy L Thompson     case CEED_EVAL_WEIGHT:
204eaf62fffSJeremy L Thompson     case CEED_EVAL_DIV:
205eaf62fffSJeremy L Thompson     case CEED_EVAL_CURL:
206eaf62fffSJeremy L Thompson       break;  // Caught by QF Assembly
207eaf62fffSJeremy L Thompson   }
208ed9e99e6SJeremy L Thompson   assert(*basis_ptr != NULL);
209eaf62fffSJeremy L Thompson }
210eaf62fffSJeremy L Thompson 
211eaf62fffSJeremy L Thompson /**
212eaf62fffSJeremy L Thompson   @brief Create point block restriction for active operator field
213eaf62fffSJeremy L Thompson 
214eaf62fffSJeremy L Thompson   @param[in]  rstr            Original CeedElemRestriction for active field
215ea61e9acSJeremy L Thompson   @param[out] pointblock_rstr Address of the variable where the newly created CeedElemRestriction will be stored
216eaf62fffSJeremy L Thompson 
217eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
218eaf62fffSJeremy L Thompson 
219eaf62fffSJeremy L Thompson   @ref Developer
220eaf62fffSJeremy L Thompson **/
2212b730f8bSJeremy L Thompson static int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *pointblock_rstr) {
222eaf62fffSJeremy L Thompson   Ceed ceed;
2232b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
224eaf62fffSJeremy L Thompson   const CeedInt *offsets;
2252b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
226eaf62fffSJeremy L Thompson 
227eaf62fffSJeremy L Thompson   // Expand offsets
2287b63f5c6SJed Brown   CeedInt  num_elem, num_comp, elem_size, comp_stride, *pointblock_offsets;
2297b63f5c6SJed Brown   CeedSize l_size;
2302b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
2312b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
2322b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
2332b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
2342b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
235eaf62fffSJeremy L Thompson   CeedInt shift = num_comp;
2362b730f8bSJeremy L Thompson   if (comp_stride != 1) shift *= num_comp;
2372b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_elem * elem_size, &pointblock_offsets));
238eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_elem * elem_size; i++) {
239eaf62fffSJeremy L Thompson     pointblock_offsets[i] = offsets[i] * shift;
240eaf62fffSJeremy L Thompson   }
241eaf62fffSJeremy L Thompson 
242eaf62fffSJeremy L Thompson   // Create new restriction
2432b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
2442b730f8bSJeremy L Thompson                                      pointblock_offsets, pointblock_rstr));
245eaf62fffSJeremy L Thompson 
246eaf62fffSJeremy L Thompson   // Cleanup
2472b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
248eaf62fffSJeremy L Thompson 
249eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
250eaf62fffSJeremy L Thompson }
251eaf62fffSJeremy L Thompson 
252eaf62fffSJeremy L Thompson /**
253eaf62fffSJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
254eaf62fffSJeremy L Thompson 
255eaf62fffSJeremy L Thompson   @param[in]  op            CeedOperator to assemble point block diagonal
256ea61e9acSJeremy L Thompson   @param[in]  request       Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE
257eaf62fffSJeremy L Thompson   @param[in]  is_pointblock Boolean flag to assemble diagonal or point block diagonal
258eaf62fffSJeremy L Thompson   @param[out] assembled     CeedVector to store assembled diagonal
259eaf62fffSJeremy L Thompson 
260eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
261eaf62fffSJeremy L Thompson 
262eaf62fffSJeremy L Thompson   @ref Developer
263eaf62fffSJeremy L Thompson **/
2642b730f8bSJeremy L Thompson static inline int CeedSingleOperatorAssembleAddDiagonal_Core(CeedOperator op, CeedRequest *request, const bool is_pointblock, CeedVector assembled) {
265eaf62fffSJeremy L Thompson   Ceed ceed;
2662b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
267eaf62fffSJeremy L Thompson 
268eaf62fffSJeremy L Thompson   // Assemble QFunction
269eaf62fffSJeremy L Thompson   CeedQFunction       qf;
270437c7c90SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
271eaf62fffSJeremy L Thompson   CeedVector          assembled_qf;
272437c7c90SJeremy L Thompson   CeedElemRestriction assembled_elem_rstr;
273437c7c90SJeremy L Thompson   CeedInt             num_input_fields, num_output_fields;
274eaf62fffSJeremy L Thompson   CeedInt             layout[3];
275437c7c90SJeremy L Thompson 
276437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
277437c7c90SJeremy L Thompson   CeedCall(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
278437c7c90SJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request));
279437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, &layout));
280437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
281437c7c90SJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
282eaf62fffSJeremy L Thompson 
283ed9e99e6SJeremy L Thompson   // Get assembly data
284ed9e99e6SJeremy L Thompson   CeedOperatorAssemblyData data;
285437c7c90SJeremy L Thompson   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
286437c7c90SJeremy L Thompson   CeedInt                 *num_eval_modes_in, *num_eval_modes_out, num_active_bases;
287437c7c90SJeremy L Thompson   CeedSize               **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components;
288437c7c90SJeremy L Thompson   CeedBasis               *active_bases;
289437c7c90SJeremy L Thompson   CeedElemRestriction     *active_elem_rstrs;
290eaf62fffSJeremy L Thompson 
291437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
292437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in,
293437c7c90SJeremy L Thompson                                                 &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out, &num_output_components));
294437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases, NULL, NULL));
295437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs));
296437c7c90SJeremy L Thompson 
297437c7c90SJeremy L Thompson   // Loop over all active bases
298437c7c90SJeremy L Thompson   for (CeedInt b = 0; b < num_active_bases; b++) {
299eaf62fffSJeremy L Thompson     // Assemble point block diagonal restriction, if needed
300437c7c90SJeremy L Thompson     CeedElemRestriction diag_elem_rstr = active_elem_rstrs[b];
301437c7c90SJeremy L Thompson 
302eaf62fffSJeremy L Thompson     if (is_pointblock) {
303437c7c90SJeremy L Thompson       CeedElemRestriction point_block_elem_rstr;
304437c7c90SJeremy L Thompson 
305437c7c90SJeremy L Thompson       CeedCall(CeedOperatorCreateActivePointBlockRestriction(diag_elem_rstr, &point_block_elem_rstr));
306437c7c90SJeremy L Thompson       diag_elem_rstr = point_block_elem_rstr;
307eaf62fffSJeremy L Thompson     }
308eaf62fffSJeremy L Thompson 
309eaf62fffSJeremy L Thompson     // Create diagonal vector
310eaf62fffSJeremy L Thompson     CeedVector elem_diag;
311437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag));
312eaf62fffSJeremy L Thompson 
313eaf62fffSJeremy L Thompson     // Assemble element operator diagonals
3149c774eddSJeremy L Thompson     CeedScalar *elem_diag_array;
315437c7c90SJeremy L Thompson     CeedInt     num_elem, num_nodes, num_qpts, num_components;
316437c7c90SJeremy L Thompson 
3172b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(elem_diag, 0.0));
3182b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array));
319437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem));
320437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(active_bases[b], &num_nodes));
321437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(active_bases[b], &num_components));
322437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(active_bases[b], &num_qpts));
323ed9e99e6SJeremy L Thompson 
324eaf62fffSJeremy L Thompson     // Basis matrices
325437c7c90SJeremy L Thompson     const CeedScalar *interp, *grad;
326eaf62fffSJeremy L Thompson     CeedScalar       *identity      = NULL;
327ed9e99e6SJeremy L Thompson     bool              has_eval_none = false;
328437c7c90SJeremy L Thompson     for (CeedInt i = 0; i < num_eval_modes_in[b]; i++) {
329437c7c90SJeremy L Thompson       has_eval_none = has_eval_none || (eval_modes_in[b][i] == CEED_EVAL_NONE);
330ed9e99e6SJeremy L Thompson     }
331437c7c90SJeremy L Thompson     for (CeedInt i = 0; i < num_eval_modes_out[b]; i++) {
332437c7c90SJeremy L Thompson       has_eval_none = has_eval_none || (eval_modes_out[b][i] == CEED_EVAL_NONE);
333ed9e99e6SJeremy L Thompson     }
334ed9e99e6SJeremy L Thompson     if (has_eval_none) {
3352b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
3362b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
337eaf62fffSJeremy L Thompson     }
338437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetInterp(active_bases[b], &interp));
339437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetGrad(active_bases[b], &grad));
340eaf62fffSJeremy L Thompson     // Compute the diagonal of B^T D B
341eaf62fffSJeremy L Thompson     // Each element
342eaf62fffSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
343eaf62fffSJeremy L Thompson       CeedInt d_out = -1;
344eaf62fffSJeremy L Thompson       // Each basis eval mode pair
345437c7c90SJeremy L Thompson       for (CeedInt e_out = 0; e_out < num_eval_modes_out[b]; e_out++) {
346437c7c90SJeremy L Thompson         const CeedScalar *B_t = NULL;
347437c7c90SJeremy L Thompson         if (eval_modes_out[b][e_out] == CEED_EVAL_GRAD) d_out += 1;
348437c7c90SJeremy L Thompson         CeedOperatorGetBasisPointer(eval_modes_out[b][e_out], identity, interp, &grad[d_out * num_qpts * num_nodes], &B_t);
349eaf62fffSJeremy L Thompson         CeedInt d_in = -1;
350437c7c90SJeremy L Thompson         for (CeedInt e_in = 0; e_in < num_eval_modes_in[b]; e_in++) {
351437c7c90SJeremy L Thompson           const CeedScalar *B = NULL;
352437c7c90SJeremy L Thompson           if (eval_modes_in[b][e_in] == CEED_EVAL_GRAD) d_in += 1;
353437c7c90SJeremy L Thompson           CeedOperatorGetBasisPointer(eval_modes_in[b][e_in], identity, interp, &grad[d_in * num_qpts * num_nodes], &B);
354eaf62fffSJeremy L Thompson           // Each component
355437c7c90SJeremy L Thompson           for (CeedInt c_out = 0; c_out < num_components; c_out++) {
356437c7c90SJeremy L Thompson             // Each qpt/node pair
3572b730f8bSJeremy L Thompson             for (CeedInt q = 0; q < num_qpts; q++) {
358eaf62fffSJeremy L Thompson               if (is_pointblock) {
359eaf62fffSJeremy L Thompson                 // Point Block Diagonal
360437c7c90SJeremy L Thompson                 for (CeedInt c_in = 0; c_in < num_components; c_in++) {
361437c7c90SJeremy L Thompson                   const CeedInt c_offset = (eval_mode_offsets_in[b][e_in] + c_in) * num_output_components + eval_mode_offsets_out[b][e_out] + c_out;
362437c7c90SJeremy L Thompson                   const CeedScalar qf_value = assembled_qf_array[q * layout[0] + c_offset * layout[1] + e * layout[2]];
3632b730f8bSJeremy L Thompson                   for (CeedInt n = 0; n < num_nodes; n++) {
364437c7c90SJeremy L Thompson                     elem_diag_array[((e * num_components + c_out) * num_components + c_in) * num_nodes + n] +=
365437c7c90SJeremy L Thompson                         B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n];
366eaf62fffSJeremy L Thompson                   }
3672b730f8bSJeremy L Thompson                 }
368eaf62fffSJeremy L Thompson               } else {
369eaf62fffSJeremy L Thompson                 // Diagonal Only
370437c7c90SJeremy L Thompson                 const CeedInt    c_offset = (eval_mode_offsets_in[b][e_in] + c_out) * num_output_components + eval_mode_offsets_out[b][e_out] + c_out;
371437c7c90SJeremy L Thompson                 const CeedScalar qf_value = assembled_qf_array[q * layout[0] + c_offset * layout[1] + e * layout[2]];
3722b730f8bSJeremy L Thompson                 for (CeedInt n = 0; n < num_nodes; n++) {
373437c7c90SJeremy L Thompson                   elem_diag_array[(e * num_components + c_out) * num_nodes + n] += B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n];
374eaf62fffSJeremy L Thompson                 }
375eaf62fffSJeremy L Thompson               }
376eaf62fffSJeremy L Thompson             }
377eaf62fffSJeremy L Thompson           }
3782b730f8bSJeremy L Thompson         }
3792b730f8bSJeremy L Thompson       }
3802b730f8bSJeremy L Thompson     }
3812b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
382eaf62fffSJeremy L Thompson 
383eaf62fffSJeremy L Thompson     // Assemble local operator diagonal
384437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionApply(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
385eaf62fffSJeremy L Thompson 
386eaf62fffSJeremy L Thompson     // Cleanup
387437c7c90SJeremy L Thompson     if (is_pointblock) CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr));
3882b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&elem_diag));
3892b730f8bSJeremy L Thompson     CeedCall(CeedFree(&identity));
390437c7c90SJeremy L Thompson   }
391437c7c90SJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
392437c7c90SJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
393eaf62fffSJeremy L Thompson 
394eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
395eaf62fffSJeremy L Thompson }
396eaf62fffSJeremy L Thompson 
397eaf62fffSJeremy L Thompson /**
398eaf62fffSJeremy L Thompson   @brief Core logic for assembling composite operator diagonal
399eaf62fffSJeremy L Thompson 
400eaf62fffSJeremy L Thompson   @param[in]  op            CeedOperator to assemble point block diagonal
401ea61e9acSJeremy L Thompson   @param[in]  request       Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE
402eaf62fffSJeremy L Thompson   @param[in]  is_pointblock Boolean flag to assemble diagonal or point block diagonal
403eaf62fffSJeremy L Thompson   @param[out] assembled     CeedVector to store assembled diagonal
404eaf62fffSJeremy L Thompson 
405eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
406eaf62fffSJeremy L Thompson 
407eaf62fffSJeremy L Thompson   @ref Developer
408eaf62fffSJeremy L Thompson **/
4092b730f8bSJeremy L Thompson static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_pointblock,
410eaf62fffSJeremy L Thompson                                                                  CeedVector assembled) {
411eaf62fffSJeremy L Thompson   CeedInt       num_sub;
412eaf62fffSJeremy L Thompson   CeedOperator *suboperators;
413c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
414c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators));
415eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_sub; i++) {
4166aa95790SJeremy L Thompson     if (is_pointblock) {
4172b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request));
4186aa95790SJeremy L Thompson     } else {
4192b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request));
4206aa95790SJeremy L Thompson     }
421eaf62fffSJeremy L Thompson   }
422eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
423eaf62fffSJeremy L Thompson }
424eaf62fffSJeremy L Thompson 
425eaf62fffSJeremy L Thompson /**
426eaf62fffSJeremy L Thompson   @brief Build nonzero pattern for non-composite operator
427eaf62fffSJeremy L Thompson 
428eaf62fffSJeremy L Thompson   Users should generally use CeedOperatorLinearAssembleSymbolic()
429eaf62fffSJeremy L Thompson 
430eaf62fffSJeremy L Thompson   @param[in]  op     CeedOperator to assemble nonzero pattern
431eaf62fffSJeremy L Thompson   @param[in]  offset Offset for number of entries
432eaf62fffSJeremy L Thompson   @param[out] rows   Row number for each entry
433eaf62fffSJeremy L Thompson   @param[out] cols   Column number for each entry
434eaf62fffSJeremy L Thompson 
435eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
436eaf62fffSJeremy L Thompson 
437eaf62fffSJeremy L Thompson   @ref Developer
438eaf62fffSJeremy L Thompson **/
4392b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) {
440f3d47e36SJeremy L Thompson   Ceed ceed;
441f3d47e36SJeremy L Thompson   bool is_composite;
442f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
443f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
444f3d47e36SJeremy L Thompson 
445b275c451SJeremy L Thompson   if (is_composite) {
446eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
4472b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
448eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
4492b730f8bSJeremy L Thompson   }
450eaf62fffSJeremy L Thompson 
451c9366a6bSJeremy L Thompson   CeedSize num_nodes;
4522b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes, NULL));
453eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_in;
4542b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &rstr_in));
455e79b91d9SJeremy L Thompson   CeedInt num_elem, elem_size, num_comp;
4562b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem));
4572b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size));
4582b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp));
459eaf62fffSJeremy L Thompson   CeedInt layout_er[3];
4602b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_in, &layout_er));
461eaf62fffSJeremy L Thompson 
462eaf62fffSJeremy L Thompson   CeedInt local_num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
463eaf62fffSJeremy L Thompson 
464eaf62fffSJeremy L Thompson   // Determine elem_dof relation
465eaf62fffSJeremy L Thompson   CeedVector index_vec;
4662b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, num_nodes, &index_vec));
467eaf62fffSJeremy L Thompson   CeedScalar *array;
4682b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(index_vec, CEED_MEM_HOST, &array));
469ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_nodes; i++) array[i] = i;
4702b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(index_vec, &array));
471eaf62fffSJeremy L Thompson   CeedVector elem_dof;
4722b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, num_elem * elem_size * num_comp, &elem_dof));
4732b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(elem_dof, 0.0));
4742b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(rstr_in, CEED_NOTRANSPOSE, index_vec, elem_dof, CEED_REQUEST_IMMEDIATE));
475eaf62fffSJeremy L Thompson   const CeedScalar *elem_dof_a;
4762b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(elem_dof, CEED_MEM_HOST, &elem_dof_a));
4772b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&index_vec));
478eaf62fffSJeremy L Thompson 
479eaf62fffSJeremy L Thompson   // Determine i, j locations for element matrices
480eaf62fffSJeremy L Thompson   CeedInt count = 0;
481ed9e99e6SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
482ed9e99e6SJeremy L Thompson     for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
483ed9e99e6SJeremy L Thompson       for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
484ed9e99e6SJeremy L Thompson         for (CeedInt i = 0; i < elem_size; i++) {
485ed9e99e6SJeremy L Thompson           for (CeedInt j = 0; j < elem_size; j++) {
4862b730f8bSJeremy L Thompson             const CeedInt elem_dof_index_row = i * layout_er[0] + (comp_out)*layout_er[1] + e * layout_er[2];
4872b730f8bSJeremy L Thompson             const CeedInt elem_dof_index_col = j * layout_er[0] + comp_in * layout_er[1] + e * layout_er[2];
488eaf62fffSJeremy L Thompson 
489eaf62fffSJeremy L Thompson             const CeedInt row = elem_dof_a[elem_dof_index_row];
490eaf62fffSJeremy L Thompson             const CeedInt col = elem_dof_a[elem_dof_index_col];
491eaf62fffSJeremy L Thompson 
492eaf62fffSJeremy L Thompson             rows[offset + count] = row;
493eaf62fffSJeremy L Thompson             cols[offset + count] = col;
494eaf62fffSJeremy L Thompson             count++;
495eaf62fffSJeremy L Thompson           }
496eaf62fffSJeremy L Thompson         }
497eaf62fffSJeremy L Thompson       }
498eaf62fffSJeremy L Thompson     }
499eaf62fffSJeremy L Thompson   }
5002b730f8bSJeremy L Thompson   if (count != local_num_entries) {
501eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
502eaf62fffSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MAJOR, "Error computing assembled entries");
503eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
5042b730f8bSJeremy L Thompson   }
5052b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(elem_dof, &elem_dof_a));
5062b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&elem_dof));
507eaf62fffSJeremy L Thompson 
508eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
509eaf62fffSJeremy L Thompson }
510eaf62fffSJeremy L Thompson 
511eaf62fffSJeremy L Thompson /**
512eaf62fffSJeremy L Thompson   @brief Assemble nonzero entries for non-composite operator
513eaf62fffSJeremy L Thompson 
514eaf62fffSJeremy L Thompson   Users should generally use CeedOperatorLinearAssemble()
515eaf62fffSJeremy L Thompson 
516eaf62fffSJeremy L Thompson   @param[in]  op     CeedOperator to assemble
517ea61e9acSJeremy L Thompson   @param[in]  offset Offset for number of entries
518eaf62fffSJeremy L Thompson   @param[out] values Values to assemble into matrix
519eaf62fffSJeremy L Thompson 
520eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
521eaf62fffSJeremy L Thompson 
522eaf62fffSJeremy L Thompson   @ref Developer
523eaf62fffSJeremy L Thompson **/
5242b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) {
525f3d47e36SJeremy L Thompson   Ceed ceed;
526f3d47e36SJeremy L Thompson   bool is_composite;
527f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
528f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
529f3d47e36SJeremy L Thompson 
530f3d47e36SJeremy L Thompson   if (is_composite) {
531eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
5322b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
533eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
5342b730f8bSJeremy L Thompson   }
535f3d47e36SJeremy L Thompson 
536f3d47e36SJeremy L Thompson   // Early exit for empty operator
537f3d47e36SJeremy L Thompson   {
538f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
539f3d47e36SJeremy L Thompson 
540f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
541f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
542f3d47e36SJeremy L Thompson   }
543eaf62fffSJeremy L Thompson 
544cefa2673SJeremy L Thompson   if (op->LinearAssembleSingle) {
545cefa2673SJeremy L Thompson     // Backend version
5462b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSingle(op, offset, values));
547cefa2673SJeremy L Thompson     return CEED_ERROR_SUCCESS;
548cefa2673SJeremy L Thompson   } else {
549cefa2673SJeremy L Thompson     // Operator fallback
550cefa2673SJeremy L Thompson     CeedOperator op_fallback;
551cefa2673SJeremy L Thompson 
5522b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
553cefa2673SJeremy L Thompson     if (op_fallback) {
5542b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values));
555cefa2673SJeremy L Thompson       return CEED_ERROR_SUCCESS;
556cefa2673SJeremy L Thompson     }
557cefa2673SJeremy L Thompson   }
558cefa2673SJeremy L Thompson 
559eaf62fffSJeremy L Thompson   // Assemble QFunction
560eaf62fffSJeremy L Thompson   CeedQFunction qf;
5612b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
562eaf62fffSJeremy L Thompson   CeedVector          assembled_qf;
563eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_q;
5642b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE));
5651f9221feSJeremy L Thompson   CeedSize qf_length;
5662b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(assembled_qf, &qf_length));
567eaf62fffSJeremy L Thompson 
5687e7773b5SJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
569eaf62fffSJeremy L Thompson   CeedOperatorField *input_fields;
570eaf62fffSJeremy L Thompson   CeedOperatorField *output_fields;
5712b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
572eaf62fffSJeremy L Thompson 
573ed9e99e6SJeremy L Thompson   // Get assembly data
574ed9e99e6SJeremy L Thompson   CeedOperatorAssemblyData data;
5752b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
576437c7c90SJeremy L Thompson   const CeedEvalMode **eval_modes_in, **eval_modes_out;
577437c7c90SJeremy L Thompson   CeedInt             *num_eval_modes_in, *num_eval_modes_out, num_active_bases;
578437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases, &num_eval_modes_in, &eval_modes_in, NULL, &num_eval_modes_out,
579437c7c90SJeremy L Thompson                                                 &eval_modes_out, NULL, NULL));
580437c7c90SJeremy L Thompson   CeedBasis *bases;
581437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &bases, NULL, NULL));
582437c7c90SJeremy L Thompson   CeedBasis basis_in = bases[0];
583eaf62fffSJeremy L Thompson 
584437c7c90SJeremy L Thompson   if (num_active_bases > 1) {
585437c7c90SJeremy L Thompson     // LCOV_EXCL_START
586437c7c90SJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator with multiple active bases");
587437c7c90SJeremy L Thompson     // LCOV_EXCL_STOP
588437c7c90SJeremy L Thompson   }
589437c7c90SJeremy L Thompson   if (num_eval_modes_in[0] == 0 || num_eval_modes_out[0] == 0) {
590eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
5912b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator with out inputs/outputs");
592eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
5932b730f8bSJeremy L Thompson   }
594eaf62fffSJeremy L Thompson 
595ed9e99e6SJeremy L Thompson   CeedElemRestriction active_rstr;
596eaf62fffSJeremy L Thompson   CeedInt             num_elem, elem_size, num_qpts, num_comp;
5972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &active_rstr));
5982b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(active_rstr, &num_elem));
5992b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(active_rstr, &elem_size));
6002b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(active_rstr, &num_comp));
6012b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
602eaf62fffSJeremy L Thompson 
603eaf62fffSJeremy L Thompson   CeedInt local_num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
604eaf62fffSJeremy L Thompson 
605eaf62fffSJeremy L Thompson   // loop over elements and put in data structure
606eaf62fffSJeremy L Thompson   const CeedScalar *interp_in, *grad_in;
6072b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp(basis_in, &interp_in));
6082b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad(basis_in, &grad_in));
609eaf62fffSJeremy L Thompson 
610eaf62fffSJeremy L Thompson   const CeedScalar *assembled_qf_array;
6112b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
612eaf62fffSJeremy L Thompson 
613eaf62fffSJeremy L Thompson   CeedInt layout_qf[3];
6142b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_q, &layout_qf));
6152b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_q));
616eaf62fffSJeremy L Thompson 
617eaf62fffSJeremy L Thompson   // we store B_mat_in, B_mat_out, BTD, elem_mat in row-major order
618437c7c90SJeremy L Thompson   const CeedScalar **B_mats_in, **B_mats_out;
619437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, NULL, &B_mats_in, &B_mats_out));
620437c7c90SJeremy L Thompson   const CeedScalar *B_mat_in = B_mats_in[0], *B_mat_out = B_mats_out[0];
621437c7c90SJeremy L Thompson   CeedScalar        BTD_mat[elem_size * num_qpts * num_eval_modes_in[0]];
622eaf62fffSJeremy L Thompson   CeedScalar        elem_mat[elem_size * elem_size];
62392ae7e47SJeremy L Thompson   CeedInt           count = 0;
624eaf62fffSJeremy L Thompson   CeedScalar       *vals;
62528ec399dSJeremy L Thompson   CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals));
626ed9e99e6SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
627ed9e99e6SJeremy L Thompson     for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
628ed9e99e6SJeremy L Thompson       for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
629ed9e99e6SJeremy L Thompson         // Compute B^T*D
630ed9e99e6SJeremy L Thompson         for (CeedInt n = 0; n < elem_size; n++) {
631ed9e99e6SJeremy L Thompson           for (CeedInt q = 0; q < num_qpts; q++) {
632437c7c90SJeremy L Thompson             for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) {
633437c7c90SJeremy L Thompson               const CeedInt btd_index = n * (num_qpts * num_eval_modes_in[0]) + (num_eval_modes_in[0] * q + e_in);
634067fd99fSJeremy L Thompson               CeedScalar    sum       = 0.0;
635437c7c90SJeremy L Thompson               for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) {
636437c7c90SJeremy L Thompson                 const CeedInt b_out_index     = (num_eval_modes_out[0] * q + e_out) * elem_size + n;
637437c7c90SJeremy L Thompson                 const CeedInt eval_mode_index = ((e_in * num_comp + comp_in) * num_eval_modes_out[0] + e_out) * num_comp + comp_out;
6382b730f8bSJeremy L Thompson                 const CeedInt qf_index        = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2];
639067fd99fSJeremy L Thompson                 sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index];
640eaf62fffSJeremy L Thompson               }
641067fd99fSJeremy L Thompson               BTD_mat[btd_index] = sum;
642ed9e99e6SJeremy L Thompson             }
643ed9e99e6SJeremy L Thompson           }
644eaf62fffSJeremy L Thompson         }
645eaf62fffSJeremy L Thompson         // form element matrix itself (for each block component)
646437c7c90SJeremy L Thompson         CeedCall(CeedMatrixMatrixMultiply(ceed, BTD_mat, B_mat_in, elem_mat, elem_size, elem_size, num_qpts * num_eval_modes_in[0]));
647eaf62fffSJeremy L Thompson 
648eaf62fffSJeremy L Thompson         // put element matrix in coordinate data structure
649ed9e99e6SJeremy L Thompson         for (CeedInt i = 0; i < elem_size; i++) {
650ed9e99e6SJeremy L Thompson           for (CeedInt j = 0; j < elem_size; j++) {
651eaf62fffSJeremy L Thompson             vals[offset + count] = elem_mat[i * elem_size + j];
652eaf62fffSJeremy L Thompson             count++;
653eaf62fffSJeremy L Thompson           }
654eaf62fffSJeremy L Thompson         }
655eaf62fffSJeremy L Thompson       }
656eaf62fffSJeremy L Thompson     }
657eaf62fffSJeremy L Thompson   }
6582b730f8bSJeremy L Thompson   if (count != local_num_entries) {
659eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
660eaf62fffSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MAJOR, "Error computing entries");
661eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
6622b730f8bSJeremy L Thompson   }
6632b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(values, &vals));
664eaf62fffSJeremy L Thompson 
6652b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
6662b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
667eaf62fffSJeremy L Thompson 
668eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
669eaf62fffSJeremy L Thompson }
670eaf62fffSJeremy L Thompson 
671eaf62fffSJeremy L Thompson /**
672eaf62fffSJeremy L Thompson   @brief Count number of entries for assembled CeedOperator
673eaf62fffSJeremy L Thompson 
674eaf62fffSJeremy L Thompson   @param[in]  op          CeedOperator to assemble
675eaf62fffSJeremy L Thompson   @param[out] num_entries Number of entries in assembled representation
676eaf62fffSJeremy L Thompson 
677eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
678eaf62fffSJeremy L Thompson 
679eaf62fffSJeremy L Thompson   @ref Utility
680eaf62fffSJeremy L Thompson **/
6812b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedInt *num_entries) {
682b275c451SJeremy L Thompson   bool                is_composite;
683eaf62fffSJeremy L Thompson   CeedElemRestriction rstr;
684eaf62fffSJeremy L Thompson   CeedInt             num_elem, elem_size, num_comp;
685eaf62fffSJeremy L Thompson 
686b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
687b275c451SJeremy L Thompson   if (is_composite) {
688eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
6892b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
690eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
6912b730f8bSJeremy L Thompson   }
6922b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &rstr));
6932b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
6942b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
6952b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
696eaf62fffSJeremy L Thompson   *num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
697eaf62fffSJeremy L Thompson 
698eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
699eaf62fffSJeremy L Thompson }
700eaf62fffSJeremy L Thompson 
701eaf62fffSJeremy L Thompson /**
702ea61e9acSJeremy L Thompson   @brief Common code for creating a multigrid coarse operator and level transfer operators for a CeedOperator
703eaf62fffSJeremy L Thompson 
704eaf62fffSJeremy L Thompson   @param[in]  op_fine      Fine grid operator
70585bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
706eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid restriction
707eaf62fffSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector basis
70885bb9dcfSJeremy L Thompson   @param[in]  basis_c_to_f Basis for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
709eaf62fffSJeremy L Thompson   @param[out] op_coarse    Coarse grid operator
71085bb9dcfSJeremy L Thompson   @param[out] op_prolong   Coarse to fine operator, or NULL
71185bb9dcfSJeremy L Thompson   @param[out] op_restrict  Fine to coarse operator, or NULL
712eaf62fffSJeremy L Thompson 
713eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
714eaf62fffSJeremy L Thompson 
715eaf62fffSJeremy L Thompson   @ref Developer
716eaf62fffSJeremy L Thompson **/
7172b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
7182b730f8bSJeremy L Thompson                                             CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
719eaf62fffSJeremy L Thompson   Ceed       ceed;
72085bb9dcfSJeremy L Thompson   CeedVector mult_vec = NULL;
7212b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
722eaf62fffSJeremy L Thompson 
723eaf62fffSJeremy L Thompson   // Check for composite operator
724eaf62fffSJeremy L Thompson   bool is_composite;
7252b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op_fine, &is_composite));
7262b730f8bSJeremy L Thompson   if (is_composite) {
727eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
7282b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported");
729eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
7302b730f8bSJeremy L Thompson   }
731eaf62fffSJeremy L Thompson 
732eaf62fffSJeremy L Thompson   // Coarse Grid
7332b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
734eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_fine = NULL;
735eaf62fffSJeremy L Thompson   // -- Clone input fields
73692ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < op_fine->qf->num_input_fields; i++) {
737eaf62fffSJeremy L Thompson     if (op_fine->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
738437c7c90SJeremy L Thompson       rstr_fine = op_fine->input_fields[i]->elem_rstr;
7392b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE));
740eaf62fffSJeremy L Thompson     } else {
741437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, op_fine->input_fields[i]->elem_rstr,
7422b730f8bSJeremy L Thompson                                     op_fine->input_fields[i]->basis, op_fine->input_fields[i]->vec));
743eaf62fffSJeremy L Thompson     }
744eaf62fffSJeremy L Thompson   }
745eaf62fffSJeremy L Thompson   // -- Clone output fields
74692ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < op_fine->qf->num_output_fields; i++) {
747eaf62fffSJeremy L Thompson     if (op_fine->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
7482b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE));
749eaf62fffSJeremy L Thompson     } else {
750437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, op_fine->output_fields[i]->elem_rstr,
7512b730f8bSJeremy L Thompson                                     op_fine->output_fields[i]->basis, op_fine->output_fields[i]->vec));
752eaf62fffSJeremy L Thompson     }
753eaf62fffSJeremy L Thompson   }
754af99e877SJeremy L Thompson   // -- Clone QFunctionAssemblyData
7552b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op_fine->qf_assembled, &(*op_coarse)->qf_assembled));
756eaf62fffSJeremy L Thompson 
757eaf62fffSJeremy L Thompson   // Multiplicity vector
75885bb9dcfSJeremy L Thompson   if (op_restrict || op_prolong) {
75985bb9dcfSJeremy L Thompson     CeedVector mult_e_vec;
76085bb9dcfSJeremy L Thompson 
76185bb9dcfSJeremy L Thompson     if (!p_mult_fine) {
76285bb9dcfSJeremy L Thompson       // LCOV_EXCL_START
76385bb9dcfSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector");
76485bb9dcfSJeremy L Thompson       // LCOV_EXCL_STOP
76585bb9dcfSJeremy L Thompson     }
7662b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec));
7672b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_e_vec, 0.0));
7682b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE));
7692b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_vec, 0.0));
7702b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE));
7712b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&mult_e_vec));
7722b730f8bSJeremy L Thompson     CeedCall(CeedVectorReciprocal(mult_vec));
77385bb9dcfSJeremy L Thompson   }
774eaf62fffSJeremy L Thompson 
775addd79feSZach Atkins   // Clone name
776addd79feSZach Atkins   bool   has_name = op_fine->name;
777addd79feSZach Atkins   size_t name_len = op_fine->name ? strlen(op_fine->name) : 0;
778addd79feSZach Atkins   CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name));
779addd79feSZach Atkins 
78083d6adf3SZach Atkins   // Check that coarse to fine basis is provided if prolong/restrict operators are requested
78183d6adf3SZach Atkins   if ((op_restrict || op_prolong) && !basis_c_to_f) {
78283d6adf3SZach Atkins     // LCOV_EXCL_START
78383d6adf3SZach Atkins     return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires coarse-to-fine basis");
78483d6adf3SZach Atkins     // LCOV_EXCL_STOP
78583d6adf3SZach Atkins   }
78683d6adf3SZach Atkins 
78785bb9dcfSJeremy L Thompson   // Restriction/Prolongation Operators
788eaf62fffSJeremy L Thompson   CeedInt num_comp;
7892b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp));
790addd79feSZach Atkins 
791addd79feSZach Atkins   // Restriction
792addd79feSZach Atkins   if (op_restrict) {
793eaf62fffSJeremy L Thompson     CeedInt             *num_comp_r_data;
79485bb9dcfSJeremy L Thompson     CeedQFunction        qf_restrict;
79585bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_r;
79685bb9dcfSJeremy L Thompson 
79785bb9dcfSJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict));
7982b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_r_data));
799eaf62fffSJeremy L Thompson     num_comp_r_data[0] = num_comp;
8002b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r));
8012b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data));
8022b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r));
8032b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_r));
8042b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE));
8052b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE));
8062b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP));
8072b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp));
808eaf62fffSJeremy L Thompson 
8092b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict));
8102b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE));
8112b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_fine, CEED_BASIS_COLLOCATED, mult_vec));
8122b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
813eaf62fffSJeremy L Thompson 
814addd79feSZach Atkins     // Set name
815addd79feSZach Atkins     char *restriction_name;
816addd79feSZach Atkins     CeedCall(CeedCalloc(17 + name_len, &restriction_name));
817addd79feSZach Atkins     sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
818addd79feSZach Atkins     CeedCall(CeedOperatorSetName(*op_restrict, restriction_name));
819addd79feSZach Atkins     CeedCall(CeedFree(&restriction_name));
820addd79feSZach Atkins 
821addd79feSZach Atkins     // Check
822addd79feSZach Atkins     CeedCall(CeedOperatorCheckReady(*op_restrict));
823addd79feSZach Atkins 
824addd79feSZach Atkins     // Cleanup
825addd79feSZach Atkins     CeedCall(CeedQFunctionDestroy(&qf_restrict));
826addd79feSZach Atkins   }
827addd79feSZach Atkins 
828eaf62fffSJeremy L Thompson   // Prolongation
829addd79feSZach Atkins   if (op_prolong) {
830eaf62fffSJeremy L Thompson     CeedInt             *num_comp_p_data;
83185bb9dcfSJeremy L Thompson     CeedQFunction        qf_prolong;
83285bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_p;
83385bb9dcfSJeremy L Thompson 
83485bb9dcfSJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong));
8352b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_p_data));
836eaf62fffSJeremy L Thompson     num_comp_p_data[0] = num_comp;
8372b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p));
8382b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data));
8392b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p));
8402b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_p));
8412b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP));
8422b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE));
8432b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE));
8442b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp));
845eaf62fffSJeremy L Thompson 
8462b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong));
8472b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
8482b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_fine, CEED_BASIS_COLLOCATED, mult_vec));
8492b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE));
850eaf62fffSJeremy L Thompson 
851addd79feSZach Atkins     // Set name
852ea6b5821SJeremy L Thompson     char *prolongation_name;
8532b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(18 + name_len, &prolongation_name));
8542b730f8bSJeremy L Thompson     sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
8552b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name));
8562b730f8bSJeremy L Thompson     CeedCall(CeedFree(&prolongation_name));
857addd79feSZach Atkins 
858addd79feSZach Atkins     // Check
859addd79feSZach Atkins     CeedCall(CeedOperatorCheckReady(*op_prolong));
860addd79feSZach Atkins 
861addd79feSZach Atkins     // Cleanup
862addd79feSZach Atkins     CeedCall(CeedQFunctionDestroy(&qf_prolong));
863ea6b5821SJeremy L Thompson   }
864ea6b5821SJeremy L Thompson 
86558e4b056SJeremy L Thompson   // Check
86658e4b056SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(*op_coarse));
86758e4b056SJeremy L Thompson 
868eaf62fffSJeremy L Thompson   // Cleanup
8692b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_vec));
8702b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis_c_to_f));
871805fe78eSJeremy L Thompson 
872eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
873eaf62fffSJeremy L Thompson }
874eaf62fffSJeremy L Thompson 
875eaf62fffSJeremy L Thompson /**
876eaf62fffSJeremy L Thompson   @brief Build 1D mass matrix and Laplacian with perturbation
877eaf62fffSJeremy L Thompson 
878eaf62fffSJeremy L Thompson   @param[in]  interp_1d   Interpolation matrix in one dimension
879eaf62fffSJeremy L Thompson   @param[in]  grad_1d     Gradient matrix in one dimension
880eaf62fffSJeremy L Thompson   @param[in]  q_weight_1d Quadrature weights in one dimension
881eaf62fffSJeremy L Thompson   @param[in]  P_1d        Number of basis nodes in one dimension
882eaf62fffSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
883eaf62fffSJeremy L Thompson   @param[in]  dim         Dimension of basis
884eaf62fffSJeremy L Thompson   @param[out] mass        Assembled mass matrix in one dimension
885eaf62fffSJeremy L Thompson   @param[out] laplace     Assembled perturbed Laplacian in one dimension
886eaf62fffSJeremy L Thompson 
887eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
888eaf62fffSJeremy L Thompson 
889eaf62fffSJeremy L Thompson   @ref Developer
890eaf62fffSJeremy L Thompson **/
8912b730f8bSJeremy L Thompson CeedPragmaOptimizeOff static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d,
8922b730f8bSJeremy L Thompson                                                       CeedInt P_1d, CeedInt Q_1d, CeedInt dim, CeedScalar *mass, CeedScalar *laplace) {
8932b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
894eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
895eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
8962b730f8bSJeremy 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];
897eaf62fffSJeremy L Thompson       mass[i + j * P_1d] = sum;
898eaf62fffSJeremy L Thompson     }
8992b730f8bSJeremy L Thompson   }
900eaf62fffSJeremy L Thompson   // -- Laplacian
9012b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
902eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
903eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
9042b730f8bSJeremy 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];
905eaf62fffSJeremy L Thompson       laplace[i + j * P_1d] = sum;
906eaf62fffSJeremy L Thompson     }
9072b730f8bSJeremy L Thompson   }
908eaf62fffSJeremy L Thompson   CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4;
9092b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation;
910eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
911eaf62fffSJeremy L Thompson }
912ea61e9acSJeremy L Thompson CeedPragmaOptimizeOn;
913eaf62fffSJeremy L Thompson 
914eaf62fffSJeremy L Thompson /// @}
915eaf62fffSJeremy L Thompson 
916eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
917480fae85SJeremy L Thompson /// CeedOperator Backend API
918480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
919480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend
920480fae85SJeremy L Thompson /// @{
921480fae85SJeremy L Thompson 
922480fae85SJeremy L Thompson /**
923480fae85SJeremy L Thompson   @brief Create object holding CeedQFunction assembly data for CeedOperator
924480fae85SJeremy L Thompson 
925480fae85SJeremy L Thompson   @param[in]  ceed A Ceed object where the CeedQFunctionAssemblyData will be created
926ea61e9acSJeremy L Thompson   @param[out] data Address of the variable where the newly created CeedQFunctionAssemblyData will be stored
927480fae85SJeremy L Thompson 
928480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
929480fae85SJeremy L Thompson 
930480fae85SJeremy L Thompson   @ref Backend
931480fae85SJeremy L Thompson **/
932ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) {
9332b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
934480fae85SJeremy L Thompson   (*data)->ref_count = 1;
935480fae85SJeremy L Thompson   (*data)->ceed      = ceed;
9362b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
937480fae85SJeremy L Thompson 
938480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
939480fae85SJeremy L Thompson }
940480fae85SJeremy L Thompson 
941480fae85SJeremy L Thompson /**
942480fae85SJeremy L Thompson   @brief Increment the reference counter for a CeedQFunctionAssemblyData
943480fae85SJeremy L Thompson 
944ea61e9acSJeremy L Thompson   @param[in,out] data CeedQFunctionAssemblyData to increment the reference counter
945480fae85SJeremy L Thompson 
946480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
947480fae85SJeremy L Thompson 
948480fae85SJeremy L Thompson   @ref Backend
949480fae85SJeremy L Thompson **/
950480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) {
951480fae85SJeremy L Thompson   data->ref_count++;
952480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
953480fae85SJeremy L Thompson }
954480fae85SJeremy L Thompson 
955480fae85SJeremy L Thompson /**
956beecbf24SJeremy L Thompson   @brief Set re-use of CeedQFunctionAssemblyData
9578b919e6bSJeremy L Thompson 
958ea61e9acSJeremy L Thompson   @param[in,out] data       CeedQFunctionAssemblyData to mark for reuse
959ea61e9acSJeremy L Thompson   @param[in]     reuse_data Boolean flag indicating data re-use
9608b919e6bSJeremy L Thompson 
9618b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9628b919e6bSJeremy L Thompson 
9638b919e6bSJeremy L Thompson   @ref Backend
9648b919e6bSJeremy L Thompson **/
9652b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) {
966beecbf24SJeremy L Thompson   data->reuse_data        = reuse_data;
967beecbf24SJeremy L Thompson   data->needs_data_update = true;
968beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
969beecbf24SJeremy L Thompson }
970beecbf24SJeremy L Thompson 
971beecbf24SJeremy L Thompson /**
972beecbf24SJeremy L Thompson   @brief Mark QFunctionAssemblyData as stale
973beecbf24SJeremy L Thompson 
974ea61e9acSJeremy L Thompson   @param[in,out] data              CeedQFunctionAssemblyData to mark as stale
975ea61e9acSJeremy L Thompson   @param[in]     needs_data_update Boolean flag indicating if update is needed or completed
976beecbf24SJeremy L Thompson 
977beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
978beecbf24SJeremy L Thompson 
979beecbf24SJeremy L Thompson   @ref Backend
980beecbf24SJeremy L Thompson **/
9812b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) {
982beecbf24SJeremy L Thompson   data->needs_data_update = needs_data_update;
9838b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
9848b919e6bSJeremy L Thompson }
9858b919e6bSJeremy L Thompson 
9868b919e6bSJeremy L Thompson /**
9878b919e6bSJeremy L Thompson   @brief Determine if QFunctionAssemblyData needs update
9888b919e6bSJeremy L Thompson 
9898b919e6bSJeremy L Thompson   @param[in]  data             CeedQFunctionAssemblyData to mark as stale
9908b919e6bSJeremy L Thompson   @param[out] is_update_needed Boolean flag indicating if re-assembly is required
9918b919e6bSJeremy L Thompson 
9928b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9938b919e6bSJeremy L Thompson 
9948b919e6bSJeremy L Thompson   @ref Backend
9958b919e6bSJeremy L Thompson **/
9962b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) {
997beecbf24SJeremy L Thompson   *is_update_needed = !data->reuse_data || data->needs_data_update;
9988b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
9998b919e6bSJeremy L Thompson }
10008b919e6bSJeremy L Thompson 
10018b919e6bSJeremy L Thompson /**
1002ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedQFunctionAssemblyData.
1003ea61e9acSJeremy L Thompson            Both pointers should be destroyed with `CeedCeedQFunctionAssemblyDataDestroy()`.
1004ea61e9acSJeremy L Thompson            Note: If `*data_copy` is non-NULL, then it is assumed that `*data_copy` is a pointer to a CeedQFunctionAssemblyData.
1005ea61e9acSJeremy L Thompson              This CeedQFunctionAssemblyData will be destroyed if `*data_copy` is the only reference to this CeedQFunctionAssemblyData.
1006480fae85SJeremy L Thompson 
1007ea61e9acSJeremy L Thompson   @param[in]     data      CeedQFunctionAssemblyData to copy reference to
1008ea61e9acSJeremy L Thompson   @param[in,out] data_copy Variable to store copied reference
1009480fae85SJeremy L Thompson 
1010480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1011480fae85SJeremy L Thompson 
1012480fae85SJeremy L Thompson   @ref Backend
1013480fae85SJeremy L Thompson **/
10142b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) {
10152b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReference(data));
10162b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy));
1017480fae85SJeremy L Thompson   *data_copy = data;
1018480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1019480fae85SJeremy L Thompson }
1020480fae85SJeremy L Thompson 
1021480fae85SJeremy L Thompson /**
1022480fae85SJeremy L Thompson   @brief Get setup status for internal objects for CeedQFunctionAssemblyData
1023480fae85SJeremy L Thompson 
1024ea61e9acSJeremy L Thompson   @param[in]  data     CeedQFunctionAssemblyData to retrieve status
1025480fae85SJeremy L Thompson   @param[out] is_setup Boolean flag for setup status
1026480fae85SJeremy L Thompson 
1027480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1028480fae85SJeremy L Thompson 
1029480fae85SJeremy L Thompson   @ref Backend
1030480fae85SJeremy L Thompson **/
10312b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) {
1032480fae85SJeremy L Thompson   *is_setup = data->is_setup;
1033480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1034480fae85SJeremy L Thompson }
1035480fae85SJeremy L Thompson 
1036480fae85SJeremy L Thompson /**
1037480fae85SJeremy L Thompson   @brief Set internal objects for CeedQFunctionAssemblyData
1038480fae85SJeremy L Thompson 
1039ea61e9acSJeremy L Thompson   @param[in,out] data CeedQFunctionAssemblyData to set objects
1040480fae85SJeremy L Thompson   @param[in]     vec  CeedVector to store assembled CeedQFunction at quadrature points
1041480fae85SJeremy L Thompson   @param[in]     rstr CeedElemRestriction for CeedVector containing assembled CeedQFunction
1042480fae85SJeremy L Thompson 
1043480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1044480fae85SJeremy L Thompson 
1045480fae85SJeremy L Thompson   @ref Backend
1046480fae85SJeremy L Thompson **/
10472b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) {
10482b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(vec, &data->vec));
10492b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr));
1050480fae85SJeremy L Thompson 
1051480fae85SJeremy L Thompson   data->is_setup = true;
1052480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1053480fae85SJeremy L Thompson }
1054480fae85SJeremy L Thompson 
10552b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) {
10562b730f8bSJeremy L Thompson   if (!data->is_setup) {
1057480fae85SJeremy L Thompson     // LCOV_EXCL_START
10582b730f8bSJeremy L Thompson     return CeedError(data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first.");
1059480fae85SJeremy L Thompson     // LCOV_EXCL_STOP
10602b730f8bSJeremy L Thompson   }
1061480fae85SJeremy L Thompson 
10622b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(data->vec, vec));
10632b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr));
1064480fae85SJeremy L Thompson 
1065480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1066480fae85SJeremy L Thompson }
1067480fae85SJeremy L Thompson 
1068480fae85SJeremy L Thompson /**
1069480fae85SJeremy L Thompson   @brief Destroy CeedQFunctionAssemblyData
1070480fae85SJeremy L Thompson 
1071ea61e9acSJeremy L Thompson   @param[in,out] data  CeedQFunctionAssemblyData to destroy
1072480fae85SJeremy L Thompson 
1073480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1074480fae85SJeremy L Thompson 
1075480fae85SJeremy L Thompson   @ref Backend
1076480fae85SJeremy L Thompson **/
1077480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) {
1078480fae85SJeremy L Thompson   if (!*data || --(*data)->ref_count > 0) return CEED_ERROR_SUCCESS;
1079480fae85SJeremy L Thompson 
10802b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
10812b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*data)->vec));
10822b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr));
1083480fae85SJeremy L Thompson 
10842b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1085480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1086480fae85SJeremy L Thompson }
1087480fae85SJeremy L Thompson 
1088ed9e99e6SJeremy L Thompson /**
1089ed9e99e6SJeremy L Thompson   @brief Get CeedOperatorAssemblyData
1090ed9e99e6SJeremy L Thompson 
1091ed9e99e6SJeremy L Thompson   @param[in]  op   CeedOperator to assemble
1092ed9e99e6SJeremy L Thompson   @param[out] data CeedQFunctionAssemblyData
1093ed9e99e6SJeremy L Thompson 
1094ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1095ed9e99e6SJeremy L Thompson 
1096ed9e99e6SJeremy L Thompson   @ref Backend
1097ed9e99e6SJeremy L Thompson **/
10982b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) {
1099ed9e99e6SJeremy L Thompson   if (!op->op_assembled) {
1100ed9e99e6SJeremy L Thompson     CeedOperatorAssemblyData data;
1101ed9e99e6SJeremy L Thompson 
11022b730f8bSJeremy L Thompson     CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data));
1103ed9e99e6SJeremy L Thompson     op->op_assembled = data;
1104ed9e99e6SJeremy L Thompson   }
1105ed9e99e6SJeremy L Thompson   *data = op->op_assembled;
1106ed9e99e6SJeremy L Thompson 
1107ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1108ed9e99e6SJeremy L Thompson }
1109ed9e99e6SJeremy L Thompson 
1110ed9e99e6SJeremy L Thompson /**
1111ba746a46SJeremy L Thompson   @brief Create object holding CeedOperator assembly data.
1112ba746a46SJeremy L Thompson 
1113ba746a46SJeremy L Thompson     The CeedOperatorAssemblyData holds an array with references to every active CeedBasis used in the CeedOperator.
1114ba746a46SJeremy L Thompson     An array with references to the corresponding active CeedElemRestrictions is also stored.
1115ba746a46SJeremy L Thompson     For each active CeedBasis, the CeedOperatorAssemblyData holds an array of all input and output CeedEvalModes for this CeedBasis.
1116ba746a46SJeremy L Thompson     The CeedOperatorAssemblyData holds an array of offsets for indexing into the assembled CeedQFunction arrays to the row representing each
1117ba746a46SJeremy L Thompson       CeedEvalMode.
1118ba746a46SJeremy L Thompson     The number of input columns across all active bases for the assembled CeedQFunction is also stored.
1119ba746a46SJeremy L Thompson     Lastly, the CeedOperatorAssembly data holds assembled matrices representing the full action of the CeedBasis for all CeedEvalModes.
1120ed9e99e6SJeremy L Thompson 
1121ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperatorAssemblyData will be created
1122ed9e99e6SJeremy L Thompson   @param[in]  op   CeedOperator to be assembled
1123ea61e9acSJeremy L Thompson   @param[out] data Address of the variable where the newly created CeedOperatorAssemblyData will be stored
1124ed9e99e6SJeremy L Thompson 
1125ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1126ed9e99e6SJeremy L Thompson 
1127ed9e99e6SJeremy L Thompson   @ref Backend
1128ed9e99e6SJeremy L Thompson **/
11292b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) {
1130437c7c90SJeremy L Thompson   CeedInt num_active_bases = 0;
1131437c7c90SJeremy L Thompson 
1132437c7c90SJeremy L Thompson   // Allocate
11332b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1134ed9e99e6SJeremy L Thompson   (*data)->ceed = ceed;
11352b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1136ed9e99e6SJeremy L Thompson 
1137ed9e99e6SJeremy L Thompson   // Build OperatorAssembly data
1138ed9e99e6SJeremy L Thompson   CeedQFunction       qf;
1139ed9e99e6SJeremy L Thompson   CeedQFunctionField *qf_fields;
1140ed9e99e6SJeremy L Thompson   CeedOperatorField  *op_fields;
1141ed9e99e6SJeremy L Thompson   CeedInt             num_input_fields;
11422b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
11432b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL));
11442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1145ed9e99e6SJeremy L Thompson 
1146ed9e99e6SJeremy L Thompson   // Determine active input basis
1147437c7c90SJeremy L Thompson   CeedInt       *num_eval_modes_in = NULL, *num_eval_modes_out = NULL, offset = 0;
1148437c7c90SJeremy L Thompson   CeedEvalMode **eval_modes_in = NULL, **eval_modes_out = NULL;
1149437c7c90SJeremy L Thompson   CeedSize     **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL;
1150437c7c90SJeremy L Thompson 
1151ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1152ed9e99e6SJeremy L Thompson     CeedVector vec;
1153437c7c90SJeremy L Thompson 
11542b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1155ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
1156437c7c90SJeremy L Thompson       CeedInt      index = -1, dim = 1, num_components;
1157437c7c90SJeremy L Thompson       CeedBasis    basis_in = NULL;
1158437c7c90SJeremy L Thompson       CeedEvalMode eval_mode;
1159437c7c90SJeremy L Thompson 
11602b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
11612b730f8bSJeremy L Thompson       CeedCall(CeedBasisGetDimension(basis_in, &dim));
1162437c7c90SJeremy L Thompson       CeedCall(CeedBasisGetNumComponents(basis_in, &num_components));
11632b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1164437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < num_active_bases; i++) {
1165437c7c90SJeremy L Thompson         if ((*data)->active_bases[i] == basis_in) index = i;
1166437c7c90SJeremy L Thompson       }
1167437c7c90SJeremy L Thompson       if (index == -1) {
1168437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_in;
1169437c7c90SJeremy L Thompson 
1170437c7c90SJeremy L Thompson         index = num_active_bases;
1171437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_bases));
1172437c7c90SJeremy L Thompson         (*data)->active_bases[num_active_bases] = NULL;
1173437c7c90SJeremy L Thompson         CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases[num_active_bases]));
1174437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_elem_rstrs));
1175437c7c90SJeremy L Thompson         (*data)->active_elem_rstrs[num_active_bases] = NULL;
1176437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in));
1177437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs[num_active_bases]));
1178437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_in));
1179437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_out));
1180437c7c90SJeremy L Thompson         num_eval_modes_in[index]  = 0;
1181437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1182437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_in));
1183437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_out));
1184437c7c90SJeremy L Thompson         eval_modes_in[index]  = NULL;
1185437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1186437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_in));
1187437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_out));
1188437c7c90SJeremy L Thompson         eval_mode_offsets_in[index]  = NULL;
1189437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1190437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_in));
1191437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_out));
1192437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index]  = NULL;
1193437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1194437c7c90SJeremy L Thompson         num_active_bases++;
1195437c7c90SJeremy L Thompson       }
1196ed9e99e6SJeremy L Thompson       switch (eval_mode) {
1197ed9e99e6SJeremy L Thompson         case CEED_EVAL_NONE:
1198ed9e99e6SJeremy L Thompson         case CEED_EVAL_INTERP:
1199437c7c90SJeremy L Thompson           CeedCall(CeedRealloc(num_eval_modes_in[index] + 1, &eval_modes_in[index]));
1200437c7c90SJeremy L Thompson           CeedCall(CeedRealloc(num_eval_modes_in[index] + 1, &eval_mode_offsets_in[index]));
1201437c7c90SJeremy L Thompson           eval_modes_in[index][num_eval_modes_in[index]]        = eval_mode;
1202437c7c90SJeremy L Thompson           eval_mode_offsets_in[index][num_eval_modes_in[index]] = offset;
1203437c7c90SJeremy L Thompson           offset += num_components;
1204437c7c90SJeremy L Thompson           num_eval_modes_in[index] += 1;
1205ed9e99e6SJeremy L Thompson           break;
1206ed9e99e6SJeremy L Thompson         case CEED_EVAL_GRAD:
1207437c7c90SJeremy L Thompson           CeedCall(CeedRealloc(num_eval_modes_in[index] + dim, &eval_modes_in[index]));
1208437c7c90SJeremy L Thompson           CeedCall(CeedRealloc(num_eval_modes_in[index] + dim, &eval_mode_offsets_in[index]));
1209ed9e99e6SJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
1210437c7c90SJeremy L Thompson             eval_modes_in[index][num_eval_modes_in[index] + d]        = eval_mode;
1211437c7c90SJeremy L Thompson             eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset;
1212437c7c90SJeremy L Thompson             offset += num_components;
1213ed9e99e6SJeremy L Thompson           }
1214437c7c90SJeremy L Thompson           num_eval_modes_in[index] += dim;
1215ed9e99e6SJeremy L Thompson           break;
1216ed9e99e6SJeremy L Thompson         case CEED_EVAL_WEIGHT:
1217ed9e99e6SJeremy L Thompson         case CEED_EVAL_DIV:
1218ed9e99e6SJeremy L Thompson         case CEED_EVAL_CURL:
1219ed9e99e6SJeremy L Thompson           break;  // Caught by QF Assembly
1220ed9e99e6SJeremy L Thompson       }
1221ed9e99e6SJeremy L Thompson     }
1222ed9e99e6SJeremy L Thompson   }
1223437c7c90SJeremy L Thompson   (*data)->num_eval_modes_in    = num_eval_modes_in;
1224437c7c90SJeremy L Thompson   (*data)->eval_modes_in        = eval_modes_in;
1225437c7c90SJeremy L Thompson   (*data)->eval_mode_offsets_in = eval_mode_offsets_in;
1226ed9e99e6SJeremy L Thompson 
1227ed9e99e6SJeremy L Thompson   // Determine active output basis
1228ed9e99e6SJeremy L Thompson   CeedInt num_output_fields;
1229437c7c90SJeremy L Thompson 
12302b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields));
12312b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1232437c7c90SJeremy L Thompson   offset = 0;
1233ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1234ed9e99e6SJeremy L Thompson     CeedVector vec;
1235437c7c90SJeremy L Thompson 
12362b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1237ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
1238437c7c90SJeremy L Thompson       CeedInt      index = -1, dim = 1, num_components;
1239437c7c90SJeremy L Thompson       CeedBasis    basis_out = NULL;
1240ed9e99e6SJeremy L Thompson       CeedEvalMode eval_mode;
1241437c7c90SJeremy L Thompson 
1242437c7c90SJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1243437c7c90SJeremy L Thompson       CeedCall(CeedBasisGetDimension(basis_out, &dim));
1244437c7c90SJeremy L Thompson       CeedCall(CeedBasisGetNumComponents(basis_out, &num_components));
12452b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1246437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < num_active_bases; i++) {
1247437c7c90SJeremy L Thompson         if ((*data)->active_bases[i] == basis_out) index = i;
1248437c7c90SJeremy L Thompson       }
1249437c7c90SJeremy L Thompson       if (index == -1) {
1250437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_out;
1251437c7c90SJeremy L Thompson 
1252437c7c90SJeremy L Thompson         index = num_active_bases;
1253437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_bases));
1254437c7c90SJeremy L Thompson         (*data)->active_bases[num_active_bases] = NULL;
1255437c7c90SJeremy L Thompson         CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases[num_active_bases]));
1256437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_elem_rstrs));
1257437c7c90SJeremy L Thompson         (*data)->active_elem_rstrs[num_active_bases] = NULL;
1258437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out));
1259437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs[num_active_bases]));
1260437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_in));
1261437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_out));
1262437c7c90SJeremy L Thompson         num_eval_modes_in[index]  = 0;
1263437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1264437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_in));
1265437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_out));
1266437c7c90SJeremy L Thompson         eval_modes_in[index]  = NULL;
1267437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1268437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_in));
1269437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_out));
1270437c7c90SJeremy L Thompson         eval_mode_offsets_in[index]  = NULL;
1271437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1272437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_in));
1273437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_out));
1274437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index]  = NULL;
1275437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1276437c7c90SJeremy L Thompson         num_active_bases++;
1277437c7c90SJeremy L Thompson       }
1278ed9e99e6SJeremy L Thompson       switch (eval_mode) {
1279ed9e99e6SJeremy L Thompson         case CEED_EVAL_NONE:
1280ed9e99e6SJeremy L Thompson         case CEED_EVAL_INTERP:
1281437c7c90SJeremy L Thompson           CeedCall(CeedRealloc(num_eval_modes_out[index] + 1, &eval_modes_out[index]));
1282437c7c90SJeremy L Thompson           CeedCall(CeedRealloc(num_eval_modes_out[index] + 1, &eval_mode_offsets_out[index]));
1283437c7c90SJeremy L Thompson           eval_modes_out[index][num_eval_modes_out[index]]        = eval_mode;
1284437c7c90SJeremy L Thompson           eval_mode_offsets_out[index][num_eval_modes_out[index]] = offset;
1285437c7c90SJeremy L Thompson           offset += num_components;
1286437c7c90SJeremy L Thompson           num_eval_modes_out[index] += 1;
1287ed9e99e6SJeremy L Thompson           break;
1288ed9e99e6SJeremy L Thompson         case CEED_EVAL_GRAD:
1289437c7c90SJeremy L Thompson           CeedCall(CeedRealloc(num_eval_modes_out[index] + dim, &eval_modes_out[index]));
1290437c7c90SJeremy L Thompson           CeedCall(CeedRealloc(num_eval_modes_out[index] + dim, &eval_mode_offsets_out[index]));
1291ed9e99e6SJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
1292437c7c90SJeremy L Thompson             eval_modes_out[index][num_eval_modes_out[index] + d]        = eval_mode;
1293437c7c90SJeremy L Thompson             eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset;
1294437c7c90SJeremy L Thompson             offset += num_components;
1295ed9e99e6SJeremy L Thompson           }
1296437c7c90SJeremy L Thompson           num_eval_modes_out[index] += dim;
1297ed9e99e6SJeremy L Thompson           break;
1298ed9e99e6SJeremy L Thompson         case CEED_EVAL_WEIGHT:
1299ed9e99e6SJeremy L Thompson         case CEED_EVAL_DIV:
1300ed9e99e6SJeremy L Thompson         case CEED_EVAL_CURL:
1301ed9e99e6SJeremy L Thompson           break;  // Caught by QF Assembly
1302ed9e99e6SJeremy L Thompson       }
1303ed9e99e6SJeremy L Thompson     }
1304ed9e99e6SJeremy L Thompson   }
1305437c7c90SJeremy L Thompson   (*data)->num_output_components = offset;
1306437c7c90SJeremy L Thompson   (*data)->num_eval_modes_out    = num_eval_modes_out;
1307437c7c90SJeremy L Thompson   (*data)->eval_modes_out        = eval_modes_out;
1308437c7c90SJeremy L Thompson   (*data)->eval_mode_offsets_out = eval_mode_offsets_out;
1309437c7c90SJeremy L Thompson   (*data)->num_active_bases      = num_active_bases;
1310ed9e99e6SJeremy L Thompson 
1311ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1312ed9e99e6SJeremy L Thompson }
1313ed9e99e6SJeremy L Thompson 
1314ed9e99e6SJeremy L Thompson /**
1315ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedEvalModes for assembly.
1316ba746a46SJeremy L Thompson 
1317ba746a46SJeremy L Thompson     Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1318ed9e99e6SJeremy L Thompson 
1319ed9e99e6SJeremy L Thompson   @param[in]  data                  CeedOperatorAssemblyData
1320ba746a46SJeremy L Thompson   @param[out] num_active_bases      Total number of active bases
1321*c5d0f995SJed Brown   @param[out] num_eval_modes_in     Pointer to hold array of numbers of input CeedEvalModes, or NULL.
1322ba746a46SJeremy L Thompson                                       `eval_modes_in[0]` holds an array of eval modes for the first active basis.
1323*c5d0f995SJed Brown   @param[out] eval_modes_in         Pointer to hold arrays of input CeedEvalModes, or NULL.
1324ba746a46SJeremy L Thompson   @param[out] eval_mode_offsets_in  Pointer to hold arrays of input offsets at each quadrature point.
1325*c5d0f995SJed Brown   @param[out] num_eval_modes_out    Pointer to hold array of numbers of output CeedEvalModes, or NULL
1326*c5d0f995SJed Brown   @param[out] eval_modes_out        Pointer to hold arrays of output CeedEvalModes, or NULL.
1327437c7c90SJeremy L Thompson   @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point
1328ba746a46SJeremy L Thompson   @param[out] num_output_components The number of columns in the assembled CeedQFunction matrix for each quadrature point,
1329ba746a46SJeremy L Thompson                                       including contributions of all active bases
1330ed9e99e6SJeremy L Thompson 
1331ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1332ed9e99e6SJeremy L Thompson 
1333*c5d0f995SJed Brown 
1334ed9e99e6SJeremy L Thompson   @ref Backend
1335ed9e99e6SJeremy L Thompson **/
1336437c7c90SJeremy L Thompson int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases, CeedInt **num_eval_modes_in,
1337437c7c90SJeremy L Thompson                                          const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt **num_eval_modes_out,
1338437c7c90SJeremy L Thompson                                          const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out, CeedSize *num_output_components) {
1339437c7c90SJeremy L Thompson   if (num_active_bases) *num_active_bases = data->num_active_bases;
1340437c7c90SJeremy L Thompson   if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in;
1341437c7c90SJeremy L Thompson   if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in;
1342437c7c90SJeremy L Thompson   if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in;
1343437c7c90SJeremy L Thompson   if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out;
1344437c7c90SJeremy L Thompson   if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out;
1345437c7c90SJeremy L Thompson   if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out;
1346437c7c90SJeremy L Thompson   if (num_output_components) *num_output_components = data->num_output_components;
1347ed9e99e6SJeremy L Thompson 
1348ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1349ed9e99e6SJeremy L Thompson }
1350ed9e99e6SJeremy L Thompson 
1351ed9e99e6SJeremy L Thompson /**
1352ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedBasis data for assembly.
1353ba746a46SJeremy L Thompson 
1354ba746a46SJeremy L Thompson     Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1355ed9e99e6SJeremy L Thompson 
1356ed9e99e6SJeremy L Thompson   @param[in]  data                CeedOperatorAssemblyData
1357437c7c90SJeremy L Thompson   @param[out] num_active_bases    Number of active bases, or NULL
1358437c7c90SJeremy L Thompson   @param[out] active_bases        Pointer to hold active CeedBasis, or NULL
1359437c7c90SJeremy L Thompson   @param[out] assembled_bases_in  Pointer to hold assembled active input B, or NULL
1360437c7c90SJeremy L Thompson   @param[out] assembled_bases_out Pointer to hold assembled active output B, or NULL
1361ed9e99e6SJeremy L Thompson 
1362ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1363ed9e99e6SJeremy L Thompson 
1364ed9e99e6SJeremy L Thompson   @ref Backend
1365ed9e99e6SJeremy L Thompson **/
1366437c7c90SJeremy L Thompson int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases, CeedBasis **active_bases,
1367437c7c90SJeremy L Thompson                                      const CeedScalar ***assembled_bases_in, const CeedScalar ***assembled_bases_out) {
1368ed9e99e6SJeremy L Thompson   // Assemble B_in, B_out if needed
1369437c7c90SJeremy L Thompson   if (assembled_bases_in && !data->assembled_bases_in[0]) {
1370437c7c90SJeremy L Thompson     CeedInt num_qpts;
1371437c7c90SJeremy L Thompson 
1372437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases[0], &num_qpts));
1373437c7c90SJeremy L Thompson     for (CeedInt b = 0; b < data->num_active_bases; b++) {
1374437c7c90SJeremy L Thompson       CeedInt           elem_size;
1375437c7c90SJeremy L Thompson       CeedScalar       *B_in = NULL, *identity = NULL;
1376ed9e99e6SJeremy L Thompson       const CeedScalar *interp_in, *grad_in;
1377ed9e99e6SJeremy L Thompson       bool              has_eval_none = false;
1378ed9e99e6SJeremy L Thompson 
1379437c7c90SJeremy L Thompson       CeedCall(CeedBasisGetNumNodes(data->active_bases[b], &elem_size));
1380437c7c90SJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * elem_size * data->num_eval_modes_in[b], &B_in));
1381ed9e99e6SJeremy L Thompson 
1382437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) {
1383437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE);
1384ed9e99e6SJeremy L Thompson       }
1385ed9e99e6SJeremy L Thompson       if (has_eval_none) {
13862b730f8bSJeremy L Thompson         CeedCall(CeedCalloc(num_qpts * elem_size, &identity));
1387ed9e99e6SJeremy L Thompson         for (CeedInt i = 0; i < (elem_size < num_qpts ? elem_size : num_qpts); i++) {
1388ed9e99e6SJeremy L Thompson           identity[i * elem_size + i] = 1.0;
1389ed9e99e6SJeremy L Thompson         }
1390ed9e99e6SJeremy L Thompson       }
1391437c7c90SJeremy L Thompson       CeedCall(CeedBasisGetInterp(data->active_bases[b], &interp_in));
1392437c7c90SJeremy L Thompson       CeedCall(CeedBasisGetGrad(data->active_bases[b], &grad_in));
1393ed9e99e6SJeremy L Thompson 
1394ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1395ed9e99e6SJeremy L Thompson         for (CeedInt n = 0; n < elem_size; n++) {
1396ed9e99e6SJeremy L Thompson           CeedInt d_in = -1;
1397437c7c90SJeremy L Thompson           for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) {
1398437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_in[b] * q;
1399437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
1400ed9e99e6SJeremy L Thompson 
1401437c7c90SJeremy L Thompson             if (data->eval_modes_in[b][e_in] == CEED_EVAL_GRAD) d_in++;
1402437c7c90SJeremy L Thompson             CeedOperatorGetBasisPointer(data->eval_modes_in[b][e_in], identity, interp_in, &grad_in[d_in * num_qpts * elem_size], &B);
1403437c7c90SJeremy L Thompson             B_in[(qq + e_in) * elem_size + n] = B[q * elem_size + n];
1404ed9e99e6SJeremy L Thompson           }
1405ed9e99e6SJeremy L Thompson         }
1406ed9e99e6SJeremy L Thompson       }
1407437c7c90SJeremy L Thompson       if (identity) CeedCall(CeedFree(identity));
1408437c7c90SJeremy L Thompson       data->assembled_bases_in[b] = B_in;
1409437c7c90SJeremy L Thompson     }
1410ed9e99e6SJeremy L Thompson   }
1411ed9e99e6SJeremy L Thompson 
1412437c7c90SJeremy L Thompson   if (assembled_bases_out && !data->assembled_bases_out[0]) {
1413437c7c90SJeremy L Thompson     CeedInt num_qpts;
1414437c7c90SJeremy L Thompson 
1415437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases[0], &num_qpts));
1416437c7c90SJeremy L Thompson     for (CeedInt b = 0; b < data->num_active_bases; b++) {
1417437c7c90SJeremy L Thompson       CeedInt           elem_size;
1418ed9e99e6SJeremy L Thompson       const CeedScalar *interp_out, *grad_out;
1419ed9e99e6SJeremy L Thompson       bool              has_eval_none = false;
1420437c7c90SJeremy L Thompson       CeedScalar       *B_out = NULL, *identity = NULL;
1421ed9e99e6SJeremy L Thompson 
1422437c7c90SJeremy L Thompson       CeedCall(CeedBasisGetNumNodes(data->active_bases[b], &elem_size));
1423437c7c90SJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * elem_size * data->num_eval_modes_out[b], &B_out));
1424ed9e99e6SJeremy L Thompson 
1425437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) {
1426437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE);
1427ed9e99e6SJeremy L Thompson       }
1428ed9e99e6SJeremy L Thompson       if (has_eval_none) {
14292b730f8bSJeremy L Thompson         CeedCall(CeedCalloc(num_qpts * elem_size, &identity));
1430ed9e99e6SJeremy L Thompson         for (CeedInt i = 0; i < (elem_size < num_qpts ? elem_size : num_qpts); i++) {
1431ed9e99e6SJeremy L Thompson           identity[i * elem_size + i] = 1.0;
1432ed9e99e6SJeremy L Thompson         }
1433ed9e99e6SJeremy L Thompson       }
1434437c7c90SJeremy L Thompson       CeedCall(CeedBasisGetInterp(data->active_bases[b], &interp_out));
1435437c7c90SJeremy L Thompson       CeedCall(CeedBasisGetGrad(data->active_bases[b], &grad_out));
1436ed9e99e6SJeremy L Thompson 
1437ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1438ed9e99e6SJeremy L Thompson         for (CeedInt n = 0; n < elem_size; n++) {
1439ed9e99e6SJeremy L Thompson           CeedInt d_out = -1;
1440437c7c90SJeremy L Thompson           for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) {
1441437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_out[b] * q;
1442437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
1443ed9e99e6SJeremy L Thompson 
1444437c7c90SJeremy L Thompson             if (data->eval_modes_out[b][e_out] == CEED_EVAL_GRAD) d_out++;
1445437c7c90SJeremy L Thompson             CeedOperatorGetBasisPointer(data->eval_modes_out[b][e_out], identity, interp_out, &grad_out[d_out * num_qpts * elem_size], &B);
1446437c7c90SJeremy L Thompson             B_out[(qq + e_out) * elem_size + n] = B[q * elem_size + n];
1447ed9e99e6SJeremy L Thompson           }
1448ed9e99e6SJeremy L Thompson         }
1449ed9e99e6SJeremy L Thompson       }
1450437c7c90SJeremy L Thompson       if (identity) CeedCall(CeedFree(identity));
1451437c7c90SJeremy L Thompson       data->assembled_bases_out[b] = B_out;
1452437c7c90SJeremy L Thompson     }
1453ed9e99e6SJeremy L Thompson   }
1454ed9e99e6SJeremy L Thompson 
1455437c7c90SJeremy L Thompson   // Pass out assembled data
1456437c7c90SJeremy L Thompson   if (active_bases) *active_bases = data->active_bases;
1457437c7c90SJeremy L Thompson   if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in;
1458437c7c90SJeremy L Thompson   if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out;
1459437c7c90SJeremy L Thompson 
1460437c7c90SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1461437c7c90SJeremy L Thompson }
1462437c7c90SJeremy L Thompson 
1463437c7c90SJeremy L Thompson /**
1464ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedBasis data for assembly.
1465ba746a46SJeremy L Thompson 
1466ba746a46SJeremy L Thompson   Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1467437c7c90SJeremy L Thompson 
1468437c7c90SJeremy L Thompson   @param[in]  data                  CeedOperatorAssemblyData
1469437c7c90SJeremy L Thompson   @param[out] num_active_elem_rstrs Number of active element restrictions, or NULL
1470437c7c90SJeremy L Thompson   @param[out] active_elem_rstrs     Pointer to hold active CeedElemRestrictions, or NULL
1471437c7c90SJeremy L Thompson 
1472437c7c90SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1473437c7c90SJeremy L Thompson 
1474437c7c90SJeremy L Thompson   @ref Backend
1475437c7c90SJeremy L Thompson **/
1476437c7c90SJeremy L Thompson int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs,
1477437c7c90SJeremy L Thompson                                                 CeedElemRestriction **active_elem_rstrs) {
1478437c7c90SJeremy L Thompson   if (num_active_elem_rstrs) *num_active_elem_rstrs = data->num_active_bases;
1479437c7c90SJeremy L Thompson   if (active_elem_rstrs) *active_elem_rstrs = data->active_elem_rstrs;
1480ed9e99e6SJeremy L Thompson 
1481ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1482ed9e99e6SJeremy L Thompson }
1483ed9e99e6SJeremy L Thompson 
1484ed9e99e6SJeremy L Thompson /**
1485ed9e99e6SJeremy L Thompson   @brief Destroy CeedOperatorAssemblyData
1486ed9e99e6SJeremy L Thompson 
1487ea61e9acSJeremy L Thompson   @param[in,out] data CeedOperatorAssemblyData to destroy
1488ed9e99e6SJeremy L Thompson 
1489ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1490ed9e99e6SJeremy L Thompson 
1491ed9e99e6SJeremy L Thompson   @ref Backend
1492ed9e99e6SJeremy L Thompson **/
1493ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) {
1494ed9e99e6SJeremy L Thompson   if (!*data) return CEED_ERROR_SUCCESS;
1495ed9e99e6SJeremy L Thompson 
14962b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
1497437c7c90SJeremy L Thompson   for (CeedInt b = 0; b < (*data)->num_active_bases; b++) {
1498437c7c90SJeremy L Thompson     CeedCall(CeedBasisDestroy(&(*data)->active_bases[b]));
1499437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs[b]));
1500437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_in[b]));
1501437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_out[b]));
1502437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b]));
1503437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b]));
1504437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_in[b]));
1505437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_out[b]));
1506437c7c90SJeremy L Thompson   }
1507437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->active_bases));
1508437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->active_elem_rstrs));
1509437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_in));
1510437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_out));
1511437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_in));
1512437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_out));
1513437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_in));
1514437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_out));
1515437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_in));
1516437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_out));
1517ed9e99e6SJeremy L Thompson 
15182b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1519ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1520ed9e99e6SJeremy L Thompson }
1521ed9e99e6SJeremy L Thompson 
1522480fae85SJeremy L Thompson /// @}
1523480fae85SJeremy L Thompson 
1524480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1525eaf62fffSJeremy L Thompson /// CeedOperator Public API
1526eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1527eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser
1528eaf62fffSJeremy L Thompson /// @{
1529eaf62fffSJeremy L Thompson 
1530eaf62fffSJeremy L Thompson /**
1531eaf62fffSJeremy L Thompson   @brief Assemble a linear CeedQFunction associated with a CeedOperator
1532eaf62fffSJeremy L Thompson 
1533ea61e9acSJeremy L Thompson   This returns a CeedVector containing a matrix at each quadrature point providing the action of the CeedQFunction associated with the CeedOperator.
1534ea61e9acSJeremy L Thompson     The vector 'assembled' is of shape [num_elements, num_input_fields, num_output_fields, num_quad_points] and contains column-major matrices
1535ea61e9acSJeremy 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
1536ea61e9acSJeremy 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,
1537ea61e9acSJeremy 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]
1538ea61e9acSJeremy L Thompson and producing the output [dv_0, dv_1, v].
1539eaf62fffSJeremy L Thompson 
1540ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1541f04ea552SJeremy L Thompson 
1542ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1543ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points
1544ea61e9acSJeremy L Thompson   @param[out] rstr      CeedElemRestriction for CeedVector containing assembled CeedQFunction
1545ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1546eaf62fffSJeremy L Thompson 
1547eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1548eaf62fffSJeremy L Thompson 
1549eaf62fffSJeremy L Thompson   @ref User
1550eaf62fffSJeremy L Thompson **/
15512b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
15522b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1553eaf62fffSJeremy L Thompson 
1554eaf62fffSJeremy L Thompson   if (op->LinearAssembleQFunction) {
1555d04bbc78SJeremy L Thompson     // Backend version
15562b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request));
1557eaf62fffSJeremy L Thompson   } else {
1558d04bbc78SJeremy L Thompson     // Operator fallback
1559d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1560d04bbc78SJeremy L Thompson 
15612b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1562d04bbc78SJeremy L Thompson     if (op_fallback) {
15632b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request));
1564d04bbc78SJeremy L Thompson     } else {
1565d04bbc78SJeremy L Thompson       // LCOV_EXCL_START
15662b730f8bSJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction");
1567d04bbc78SJeremy L Thompson       // LCOV_EXCL_STOP
1568d04bbc78SJeremy L Thompson     }
156970a7ffb3SJeremy L Thompson   }
1570eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1571eaf62fffSJeremy L Thompson }
157270a7ffb3SJeremy L Thompson 
157370a7ffb3SJeremy L Thompson /**
1574ea61e9acSJeremy L Thompson   @brief Assemble CeedQFunction and store result internally.
1575ea61e9acSJeremy L Thompson            Return copied references of stored data to the caller.
1576ea61e9acSJeremy L Thompson            Caller is responsible for ownership and destruction of the copied references.
1577ea61e9acSJeremy L Thompson            See also @ref CeedOperatorLinearAssembleQFunction
157870a7ffb3SJeremy L Thompson 
1579ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1580ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points
1581ea61e9acSJeremy L Thompson   @param[out] rstr      CeedElemRestriction for CeedVector containing assembledCeedQFunction
1582ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
158370a7ffb3SJeremy L Thompson 
158470a7ffb3SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
158570a7ffb3SJeremy L Thompson 
158670a7ffb3SJeremy L Thompson   @ref User
158770a7ffb3SJeremy L Thompson **/
15882b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
15892b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
159070a7ffb3SJeremy L Thompson 
159170a7ffb3SJeremy L Thompson   if (op->LinearAssembleQFunctionUpdate) {
1592d04bbc78SJeremy L Thompson     // Backend version
1593480fae85SJeremy L Thompson     bool                qf_assembled_is_setup;
15942efa2d85SJeremy L Thompson     CeedVector          assembled_vec  = NULL;
15952efa2d85SJeremy L Thompson     CeedElemRestriction assembled_rstr = NULL;
1596480fae85SJeremy L Thompson 
15972b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataIsSetup(op->qf_assembled, &qf_assembled_is_setup));
1598480fae85SJeremy L Thompson     if (qf_assembled_is_setup) {
1599d04bbc78SJeremy L Thompson       bool update_needed;
1600d04bbc78SJeremy L Thompson 
16012b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataGetObjects(op->qf_assembled, &assembled_vec, &assembled_rstr));
16022b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(op->qf_assembled, &update_needed));
16038b919e6bSJeremy L Thompson       if (update_needed) {
16042b730f8bSJeremy L Thompson         CeedCall(op->LinearAssembleQFunctionUpdate(op, assembled_vec, assembled_rstr, request));
16058b919e6bSJeremy L Thompson       }
160670a7ffb3SJeremy L Thompson     } else {
16072b730f8bSJeremy L Thompson       CeedCall(op->LinearAssembleQFunction(op, &assembled_vec, &assembled_rstr, request));
16082b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataSetObjects(op->qf_assembled, assembled_vec, assembled_rstr));
160970a7ffb3SJeremy L Thompson     }
16102b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, false));
16112efa2d85SJeremy L Thompson 
1612d04bbc78SJeremy L Thompson     // Copy reference from internally held copy
161370a7ffb3SJeremy L Thompson     *assembled = NULL;
161470a7ffb3SJeremy L Thompson     *rstr      = NULL;
16152b730f8bSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled));
16162b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled_vec));
16172b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr));
16182b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&assembled_rstr));
161970a7ffb3SJeremy L Thompson   } else {
1620d04bbc78SJeremy L Thompson     // Operator fallback
1621d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1622d04bbc78SJeremy L Thompson 
16232b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1624d04bbc78SJeremy L Thompson     if (op_fallback) {
16252b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request));
1626d04bbc78SJeremy L Thompson     } else {
1627d04bbc78SJeremy L Thompson       // LCOV_EXCL_START
16282b730f8bSJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate");
1629d04bbc78SJeremy L Thompson       // LCOV_EXCL_STOP
163070a7ffb3SJeremy L Thompson     }
163170a7ffb3SJeremy L Thompson   }
163270a7ffb3SJeremy L Thompson 
163370a7ffb3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1634eaf62fffSJeremy L Thompson }
1635eaf62fffSJeremy L Thompson 
1636eaf62fffSJeremy L Thompson /**
1637eaf62fffSJeremy L Thompson   @brief Assemble the diagonal of a square linear CeedOperator
1638eaf62fffSJeremy L Thompson 
1639eaf62fffSJeremy L Thompson   This overwrites a CeedVector with the diagonal of a linear CeedOperator.
1640eaf62fffSJeremy L Thompson 
1641ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1642eaf62fffSJeremy L Thompson 
1643ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1644f04ea552SJeremy L Thompson 
1645ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1646eaf62fffSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator diagonal
1647ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1648eaf62fffSJeremy L Thompson 
1649eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1650eaf62fffSJeremy L Thompson 
1651eaf62fffSJeremy L Thompson   @ref User
1652eaf62fffSJeremy L Thompson **/
16532b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1654f3d47e36SJeremy L Thompson   bool is_composite;
16552b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1656f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1657eaf62fffSJeremy L Thompson 
1658c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
16592b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
16602b730f8bSJeremy L Thompson   if (input_size != output_size) {
1661c9366a6bSJeremy L Thompson     // LCOV_EXCL_START
1662c9366a6bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1663c9366a6bSJeremy L Thompson     // LCOV_EXCL_STOP
16642b730f8bSJeremy L Thompson   }
1665c9366a6bSJeremy L Thompson 
1666f3d47e36SJeremy L Thompson   // Early exit for empty operator
1667f3d47e36SJeremy L Thompson   if (!is_composite) {
1668f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1669f3d47e36SJeremy L Thompson 
1670f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1671f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1672f3d47e36SJeremy L Thompson   }
1673f3d47e36SJeremy L Thompson 
1674eaf62fffSJeremy L Thompson   if (op->LinearAssembleDiagonal) {
1675d04bbc78SJeremy L Thompson     // Backend version
16762b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleDiagonal(op, assembled, request));
1677eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1678eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddDiagonal) {
1679d04bbc78SJeremy L Thompson     // Backend version with zeroing first
16802b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
16812b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1682eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1683eaf62fffSJeremy L Thompson   } else {
1684d04bbc78SJeremy L Thompson     // Operator fallback
1685d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1686d04bbc78SJeremy L Thompson 
16872b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1688d04bbc78SJeremy L Thompson     if (op_fallback) {
16892b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request));
1690eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1691eaf62fffSJeremy L Thompson     }
1692eaf62fffSJeremy L Thompson   }
1693eaf62fffSJeremy L Thompson   // Default interface implementation
16942b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
16952b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request));
1696d04bbc78SJeremy L Thompson 
1697eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1698eaf62fffSJeremy L Thompson }
1699eaf62fffSJeremy L Thompson 
1700eaf62fffSJeremy L Thompson /**
1701eaf62fffSJeremy L Thompson   @brief Assemble the diagonal of a square linear CeedOperator
1702eaf62fffSJeremy L Thompson 
1703eaf62fffSJeremy L Thompson   This sums into a CeedVector the diagonal of a linear CeedOperator.
1704eaf62fffSJeremy L Thompson 
1705ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1706eaf62fffSJeremy L Thompson 
1707ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1708f04ea552SJeremy L Thompson 
1709ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1710eaf62fffSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator diagonal
1711ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1712eaf62fffSJeremy L Thompson 
1713eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1714eaf62fffSJeremy L Thompson 
1715eaf62fffSJeremy L Thompson   @ref User
1716eaf62fffSJeremy L Thompson **/
17172b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1718f3d47e36SJeremy L Thompson   bool is_composite;
17192b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1720f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1721eaf62fffSJeremy L Thompson 
1722c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
17232b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
17242b730f8bSJeremy L Thompson   if (input_size != output_size) {
1725c9366a6bSJeremy L Thompson     // LCOV_EXCL_START
1726c9366a6bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1727c9366a6bSJeremy L Thompson     // LCOV_EXCL_STOP
17282b730f8bSJeremy L Thompson   }
1729c9366a6bSJeremy L Thompson 
1730f3d47e36SJeremy L Thompson   // Early exit for empty operator
1731f3d47e36SJeremy L Thompson   if (!is_composite) {
1732f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1733f3d47e36SJeremy L Thompson 
1734f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1735f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1736f3d47e36SJeremy L Thompson   }
1737f3d47e36SJeremy L Thompson 
1738eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddDiagonal) {
1739d04bbc78SJeremy L Thompson     // Backend version
17402b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1741eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1742eaf62fffSJeremy L Thompson   } else {
1743d04bbc78SJeremy L Thompson     // Operator fallback
1744d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1745d04bbc78SJeremy L Thompson 
17462b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1747d04bbc78SJeremy L Thompson     if (op_fallback) {
17482b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
1749eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1750eaf62fffSJeremy L Thompson     }
1751eaf62fffSJeremy L Thompson   }
1752eaf62fffSJeremy L Thompson   // Default interface implementation
1753eaf62fffSJeremy L Thompson   if (is_composite) {
17542b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
1755eaf62fffSJeremy L Thompson   } else {
17562b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, false, assembled));
1757eaf62fffSJeremy L Thompson   }
1758d04bbc78SJeremy L Thompson 
1759d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1760eaf62fffSJeremy L Thompson }
1761eaf62fffSJeremy L Thompson 
1762eaf62fffSJeremy L Thompson /**
1763eaf62fffSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear CeedOperator
1764eaf62fffSJeremy L Thompson 
1765ea61e9acSJeremy L Thompson   This overwrites a CeedVector with the point block diagonal of a linear CeedOperator.
1766eaf62fffSJeremy L Thompson 
1767ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1768eaf62fffSJeremy L Thompson 
1769ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1770f04ea552SJeremy L Thompson 
1771ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1772ea61e9acSJeremy 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
1773ea61e9acSJeremy 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,
1774ea61e9acSJeremy L Thompson component in].
1775ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1776eaf62fffSJeremy L Thompson 
1777eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1778eaf62fffSJeremy L Thompson 
1779eaf62fffSJeremy L Thompson   @ref User
1780eaf62fffSJeremy L Thompson **/
17812b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1782f3d47e36SJeremy L Thompson   bool is_composite;
17832b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1784f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1785eaf62fffSJeremy L Thompson 
1786c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
17872b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
17882b730f8bSJeremy L Thompson   if (input_size != output_size) {
1789c9366a6bSJeremy L Thompson     // LCOV_EXCL_START
1790c9366a6bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1791c9366a6bSJeremy L Thompson     // LCOV_EXCL_STOP
17922b730f8bSJeremy L Thompson   }
1793c9366a6bSJeremy L Thompson 
1794f3d47e36SJeremy L Thompson   // Early exit for empty operator
1795f3d47e36SJeremy L Thompson   if (!is_composite) {
1796f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1797f3d47e36SJeremy L Thompson 
1798f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1799f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1800f3d47e36SJeremy L Thompson   }
1801f3d47e36SJeremy L Thompson 
1802eaf62fffSJeremy L Thompson   if (op->LinearAssemblePointBlockDiagonal) {
1803d04bbc78SJeremy L Thompson     // Backend version
18042b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request));
1805eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1806eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddPointBlockDiagonal) {
1807d04bbc78SJeremy L Thompson     // Backend version with zeroing first
18082b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
18092b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
1810eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1811eaf62fffSJeremy L Thompson   } else {
1812d04bbc78SJeremy L Thompson     // Operator fallback
1813d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1814d04bbc78SJeremy L Thompson 
18152b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1816d04bbc78SJeremy L Thompson     if (op_fallback) {
18172b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request));
1818eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1819eaf62fffSJeremy L Thompson     }
1820eaf62fffSJeremy L Thompson   }
1821eaf62fffSJeremy L Thompson   // Default interface implementation
18222b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
18232b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
1824d04bbc78SJeremy L Thompson 
1825eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1826eaf62fffSJeremy L Thompson }
1827eaf62fffSJeremy L Thompson 
1828eaf62fffSJeremy L Thompson /**
1829eaf62fffSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear CeedOperator
1830eaf62fffSJeremy L Thompson 
1831ea61e9acSJeremy L Thompson   This sums into a CeedVector with the point block diagonal of a linear CeedOperator.
1832eaf62fffSJeremy L Thompson 
1833ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1834eaf62fffSJeremy L Thompson 
1835ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1836f04ea552SJeremy L Thompson 
1837ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1838ea61e9acSJeremy 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
1839ea61e9acSJeremy 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,
1840ea61e9acSJeremy L Thompson component in].
1841ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1842eaf62fffSJeremy L Thompson 
1843eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1844eaf62fffSJeremy L Thompson 
1845eaf62fffSJeremy L Thompson   @ref User
1846eaf62fffSJeremy L Thompson **/
18472b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1848f3d47e36SJeremy L Thompson   bool is_composite;
18492b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1850f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1851eaf62fffSJeremy L Thompson 
1852c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
18532b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
18542b730f8bSJeremy L Thompson   if (input_size != output_size) {
1855c9366a6bSJeremy L Thompson     // LCOV_EXCL_START
1856c9366a6bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1857c9366a6bSJeremy L Thompson     // LCOV_EXCL_STOP
18582b730f8bSJeremy L Thompson   }
1859c9366a6bSJeremy L Thompson 
1860f3d47e36SJeremy L Thompson   // Early exit for empty operator
1861f3d47e36SJeremy L Thompson   if (!is_composite) {
1862f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1863f3d47e36SJeremy L Thompson 
1864f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1865f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1866f3d47e36SJeremy L Thompson   }
1867f3d47e36SJeremy L Thompson 
1868eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddPointBlockDiagonal) {
1869d04bbc78SJeremy L Thompson     // Backend version
18702b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request));
1871eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1872eaf62fffSJeremy L Thompson   } else {
1873d04bbc78SJeremy L Thompson     // Operator fallback
1874d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1875d04bbc78SJeremy L Thompson 
18762b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1877d04bbc78SJeremy L Thompson     if (op_fallback) {
18782b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request));
1879eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1880eaf62fffSJeremy L Thompson     }
1881eaf62fffSJeremy L Thompson   }
1882ea61e9acSJeremy L Thompson   // Default interface implementation
1883eaf62fffSJeremy L Thompson   if (is_composite) {
18842b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
1885eaf62fffSJeremy L Thompson   } else {
18862b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, true, assembled));
1887eaf62fffSJeremy L Thompson   }
1888d04bbc78SJeremy L Thompson 
1889d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1890eaf62fffSJeremy L Thompson }
1891eaf62fffSJeremy L Thompson 
1892eaf62fffSJeremy L Thompson /**
1893eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero pattern of a linear operator.
1894eaf62fffSJeremy L Thompson 
1895ea61e9acSJeremy L Thompson    Expected to be used in conjunction with CeedOperatorLinearAssemble().
1896eaf62fffSJeremy L Thompson 
1897ea61e9acSJeremy 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
1898ea61e9acSJeremy 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)
1899ea61e9acSJeremy L Thompson locations, while CeedOperatorLinearAssemble() provides the values in the same ordering.
1900eaf62fffSJeremy L Thompson 
1901eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
1902eaf62fffSJeremy L Thompson 
1903ea61e9acSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1904f04ea552SJeremy L Thompson 
1905eaf62fffSJeremy L Thompson    @param[in]  op          CeedOperator to assemble
1906eaf62fffSJeremy L Thompson    @param[out] num_entries Number of entries in coordinate nonzero pattern
1907eaf62fffSJeremy L Thompson    @param[out] rows        Row number for each entry
1908eaf62fffSJeremy L Thompson    @param[out] cols        Column number for each entry
1909eaf62fffSJeremy L Thompson 
1910eaf62fffSJeremy L Thompson    @ref User
1911eaf62fffSJeremy L Thompson **/
19122b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
1913eaf62fffSJeremy L Thompson   CeedInt       num_suboperators, single_entries;
1914eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
1915eaf62fffSJeremy L Thompson   bool          is_composite;
19162b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1917f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1918eaf62fffSJeremy L Thompson 
1919eaf62fffSJeremy L Thompson   if (op->LinearAssembleSymbolic) {
1920d04bbc78SJeremy L Thompson     // Backend version
19212b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols));
1922eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1923eaf62fffSJeremy L Thompson   } else {
1924d04bbc78SJeremy L Thompson     // Operator fallback
1925d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1926d04bbc78SJeremy L Thompson 
19272b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1928d04bbc78SJeremy L Thompson     if (op_fallback) {
19292b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols));
1930eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1931eaf62fffSJeremy L Thompson     }
1932eaf62fffSJeremy L Thompson   }
1933eaf62fffSJeremy L Thompson 
1934eaf62fffSJeremy L Thompson   // Default interface implementation
1935eaf62fffSJeremy L Thompson 
1936eaf62fffSJeremy L Thompson   // count entries and allocate rows, cols arrays
1937eaf62fffSJeremy L Thompson   *num_entries = 0;
1938eaf62fffSJeremy L Thompson   if (is_composite) {
1939c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1940c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
194192ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
19422b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
1943eaf62fffSJeremy L Thompson       *num_entries += single_entries;
1944eaf62fffSJeremy L Thompson     }
1945eaf62fffSJeremy L Thompson   } else {
19462b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries));
1947eaf62fffSJeremy L Thompson     *num_entries += single_entries;
1948eaf62fffSJeremy L Thompson   }
19492b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, rows));
19502b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, cols));
1951eaf62fffSJeremy L Thompson 
1952eaf62fffSJeremy L Thompson   // assemble nonzero locations
1953eaf62fffSJeremy L Thompson   CeedInt offset = 0;
1954eaf62fffSJeremy L Thompson   if (is_composite) {
1955c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1956c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
195792ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
19582b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols));
19592b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
1960eaf62fffSJeremy L Thompson       offset += single_entries;
1961eaf62fffSJeremy L Thompson     }
1962eaf62fffSJeremy L Thompson   } else {
19632b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols));
1964eaf62fffSJeremy L Thompson   }
1965eaf62fffSJeremy L Thompson 
1966eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1967eaf62fffSJeremy L Thompson }
1968eaf62fffSJeremy L Thompson 
1969eaf62fffSJeremy L Thompson /**
1970eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero entries of a linear operator.
1971eaf62fffSJeremy L Thompson 
1972ea61e9acSJeremy L Thompson    Expected to be used in conjunction with CeedOperatorLinearAssembleSymbolic().
1973eaf62fffSJeremy L Thompson 
1974ea61e9acSJeremy 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
1975ea61e9acSJeremy 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,
1976ea61e9acSJeremy L Thompson their (i, j) locations are provided by CeedOperatorLinearAssembleSymbolic()
1977eaf62fffSJeremy L Thompson 
1978eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
1979eaf62fffSJeremy L Thompson 
1980ea61e9acSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1981f04ea552SJeremy L Thompson 
1982eaf62fffSJeremy L Thompson    @param[in]  op     CeedOperator to assemble
1983eaf62fffSJeremy L Thompson    @param[out] values Values to assemble into matrix
1984eaf62fffSJeremy L Thompson 
1985eaf62fffSJeremy L Thompson    @ref User
1986eaf62fffSJeremy L Thompson **/
1987eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) {
1988eaf62fffSJeremy L Thompson   CeedInt       num_suboperators, single_entries = 0;
1989eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
1990f3d47e36SJeremy L Thompson   bool          is_composite;
19912b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1992f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1993f3d47e36SJeremy L Thompson 
1994f3d47e36SJeremy L Thompson   // Early exit for empty operator
1995f3d47e36SJeremy L Thompson   if (!is_composite) {
1996f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1997f3d47e36SJeremy L Thompson 
1998f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1999f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2000f3d47e36SJeremy L Thompson   }
2001eaf62fffSJeremy L Thompson 
2002eaf62fffSJeremy L Thompson   if (op->LinearAssemble) {
2003d04bbc78SJeremy L Thompson     // Backend version
20042b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemble(op, values));
2005eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2006eaf62fffSJeremy L Thompson   } else {
2007d04bbc78SJeremy L Thompson     // Operator fallback
2008d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2009d04bbc78SJeremy L Thompson 
20102b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2011d04bbc78SJeremy L Thompson     if (op_fallback) {
20122b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemble(op_fallback, values));
2013eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2014eaf62fffSJeremy L Thompson     }
2015eaf62fffSJeremy L Thompson   }
2016eaf62fffSJeremy L Thompson 
2017eaf62fffSJeremy L Thompson   // Default interface implementation
2018eaf62fffSJeremy L Thompson   CeedInt offset = 0;
201928ec399dSJeremy L Thompson   CeedCall(CeedVectorSetValue(values, 0.0));
2020eaf62fffSJeremy L Thompson   if (is_composite) {
2021c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2022c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2023cefa2673SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; k++) {
20242b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values));
20252b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2026eaf62fffSJeremy L Thompson       offset += single_entries;
2027eaf62fffSJeremy L Thompson     }
2028eaf62fffSJeremy L Thompson   } else {
20292b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemble(op, offset, values));
2030eaf62fffSJeremy L Thompson   }
2031eaf62fffSJeremy L Thompson 
2032eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2033eaf62fffSJeremy L Thompson }
2034eaf62fffSJeremy L Thompson 
2035eaf62fffSJeremy L Thompson /**
203675f0d5a4SJeremy L Thompson   @brief Get the multiplicity of nodes across suboperators in a composite CeedOperator
203775f0d5a4SJeremy L Thompson 
203875f0d5a4SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
203975f0d5a4SJeremy L Thompson 
204075f0d5a4SJeremy L Thompson   @param[in]  op               Composite CeedOperator
204175f0d5a4SJeremy L Thompson   @param[in]  num_skip_indices Number of suboperators to skip
204275f0d5a4SJeremy L Thompson   @param[in]  skip_indices     Array of indices of suboperators to skip
204375f0d5a4SJeremy L Thompson   @param[out] mult             Vector to store multiplicity (of size l_size)
204475f0d5a4SJeremy L Thompson 
204575f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
204675f0d5a4SJeremy L Thompson 
204775f0d5a4SJeremy L Thompson   @ref User
204875f0d5a4SJeremy L Thompson **/
204975f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) {
205075f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
205175f0d5a4SJeremy L Thompson 
205275f0d5a4SJeremy L Thompson   Ceed                ceed;
2053b275c451SJeremy L Thompson   CeedInt             num_suboperators;
205475f0d5a4SJeremy L Thompson   CeedSize            l_vec_len;
205575f0d5a4SJeremy L Thompson   CeedScalar         *mult_array;
205675f0d5a4SJeremy L Thompson   CeedVector          ones_l_vec;
2057437c7c90SJeremy L Thompson   CeedElemRestriction elem_rstr;
2058b275c451SJeremy L Thompson   CeedOperator       *sub_operators;
205975f0d5a4SJeremy L Thompson 
206075f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
206175f0d5a4SJeremy L Thompson 
206275f0d5a4SJeremy L Thompson   // Zero mult vector
206375f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
206475f0d5a4SJeremy L Thompson 
206575f0d5a4SJeremy L Thompson   // Get suboperators
2066b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2067b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2068b275c451SJeremy L Thompson   if (num_suboperators == 0) return CEED_ERROR_SUCCESS;
206975f0d5a4SJeremy L Thompson 
207075f0d5a4SJeremy L Thompson   // Work vector
207175f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetLength(mult, &l_vec_len));
207275f0d5a4SJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec));
207375f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(ones_l_vec, 1.0));
207475f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array));
207575f0d5a4SJeremy L Thompson 
207675f0d5a4SJeremy L Thompson   // Compute multiplicity across suboperators
2077b275c451SJeremy L Thompson   for (CeedInt i = 0; i < num_suboperators; i++) {
207875f0d5a4SJeremy L Thompson     const CeedScalar *sub_mult_array;
207975f0d5a4SJeremy L Thompson     CeedVector        sub_mult_l_vec, ones_e_vec;
208075f0d5a4SJeremy L Thompson 
208175f0d5a4SJeremy L Thompson     // -- Check for suboperator to skip
208275f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < num_skip_indices; j++) {
208375f0d5a4SJeremy L Thompson       if (skip_indices[j] == i) continue;
208475f0d5a4SJeremy L Thompson     }
208575f0d5a4SJeremy L Thompson 
208675f0d5a4SJeremy L Thompson     // -- Sub operator multiplicity
2087437c7c90SJeremy L Thompson     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr));
2088437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(elem_rstr, &sub_mult_l_vec, &ones_e_vec));
208975f0d5a4SJeremy L Thompson     CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0));
2090437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE));
2091437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE));
209275f0d5a4SJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array));
209375f0d5a4SJeremy L Thompson     // ---- Flag every node present in the current suboperator
209475f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < l_vec_len; j++) {
209575f0d5a4SJeremy L Thompson       if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0;
209675f0d5a4SJeremy L Thompson     }
209775f0d5a4SJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array));
209875f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&sub_mult_l_vec));
209975f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&ones_e_vec));
210075f0d5a4SJeremy L Thompson   }
210175f0d5a4SJeremy L Thompson   CeedCall(CeedVectorRestoreArray(mult, &mult_array));
2102811d0ccfSJeremy L Thompson   CeedCall(CeedVectorDestroy(&ones_l_vec));
210375f0d5a4SJeremy L Thompson 
210475f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
210575f0d5a4SJeremy L Thompson }
210675f0d5a4SJeremy L Thompson 
210775f0d5a4SJeremy L Thompson /**
2108ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator, creating the prolongation basis from the fine and coarse
2109ea61e9acSJeremy L Thompson grid interpolation
2110eaf62fffSJeremy L Thompson 
211158e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2112f04ea552SJeremy L Thompson 
2113eaf62fffSJeremy L Thompson   @param[in]  op_fine      Fine grid operator
211485bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2115eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid restriction
2116eaf62fffSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector basis
2117eaf62fffSJeremy L Thompson   @param[out] op_coarse    Coarse grid operator
211885bb9dcfSJeremy L Thompson   @param[out] op_prolong   Coarse to fine operator, or NULL
211985bb9dcfSJeremy L Thompson   @param[out] op_restrict  Fine to coarse operator, or NULL
2120eaf62fffSJeremy L Thompson 
2121eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2122eaf62fffSJeremy L Thompson 
2123eaf62fffSJeremy L Thompson   @ref User
2124eaf62fffSJeremy L Thompson **/
21252b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
21262b730f8bSJeremy L Thompson                                      CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
21272b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2128eaf62fffSJeremy L Thompson 
212983d6adf3SZach Atkins   // Build prolongation matrix, if required
213083d6adf3SZach Atkins   CeedBasis basis_c_to_f = NULL;
213183d6adf3SZach Atkins   if (op_prolong || op_restrict) {
213283d6adf3SZach Atkins     CeedBasis basis_fine;
21332b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
21342b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f));
213583d6adf3SZach Atkins   }
2136eaf62fffSJeremy L Thompson 
2137f113e5dcSJeremy L Thompson   // Core code
21382b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2139f113e5dcSJeremy L Thompson 
2140eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2141eaf62fffSJeremy L Thompson }
2142eaf62fffSJeremy L Thompson 
2143eaf62fffSJeremy L Thompson /**
2144ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a tensor basis for the active basis
2145eaf62fffSJeremy L Thompson 
214658e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2147f04ea552SJeremy L Thompson 
2148eaf62fffSJeremy L Thompson   @param[in]  op_fine       Fine grid operator
214985bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2150eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid restriction
2151eaf62fffSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector basis
215285bb9dcfSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
2153eaf62fffSJeremy L Thompson   @param[out] op_coarse     Coarse grid operator
215485bb9dcfSJeremy L Thompson   @param[out] op_prolong    Coarse to fine operator, or NULL
215585bb9dcfSJeremy L Thompson   @param[out] op_restrict   Fine to coarse operator, or NULL
2156eaf62fffSJeremy L Thompson 
2157eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2158eaf62fffSJeremy L Thompson 
2159eaf62fffSJeremy L Thompson   @ref User
2160eaf62fffSJeremy L Thompson **/
21612b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
21622b730f8bSJeremy L Thompson                                              const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
21632b730f8bSJeremy L Thompson                                              CeedOperator *op_restrict) {
21642b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2165eaf62fffSJeremy L Thompson   Ceed ceed;
21662b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2167eaf62fffSJeremy L Thompson 
2168eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
2169eaf62fffSJeremy L Thompson   CeedBasis basis_fine;
21702b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
2171eaf62fffSJeremy L Thompson   CeedInt Q_f, Q_c;
21722b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
21732b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
21742b730f8bSJeremy L Thompson   if (Q_f != Q_c) {
2175eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
21762b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2177eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
21782b730f8bSJeremy L Thompson   }
2179eaf62fffSJeremy L Thompson 
218083d6adf3SZach Atkins   // Create coarse to fine basis, if required
218183d6adf3SZach Atkins   CeedBasis basis_c_to_f = NULL;
218283d6adf3SZach Atkins   if (op_prolong || op_restrict) {
218383d6adf3SZach Atkins     // Check if interpolation matrix is provided
218483d6adf3SZach Atkins     if (!interp_c_to_f) {
218583d6adf3SZach Atkins       // LCOV_EXCL_START
218683d6adf3SZach Atkins       return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
218783d6adf3SZach Atkins       // LCOV_EXCL_STOP
218883d6adf3SZach Atkins     }
2189eaf62fffSJeremy L Thompson     CeedInt dim, num_comp, num_nodes_c, P_1d_f, P_1d_c;
21902b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
21912b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
21922b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f));
21932b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
21942b730f8bSJeremy L Thompson     P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c);
2195eaf62fffSJeremy L Thompson     CeedScalar *q_ref, *q_weight, *grad;
21962b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_ref));
21972b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_weight));
21982b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad));
21992b730f8bSJeremy 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));
22002b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
22012b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
22022b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
220383d6adf3SZach Atkins   }
2204eaf62fffSJeremy L Thompson 
2205eaf62fffSJeremy L Thompson   // Core code
22062b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2207eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2208eaf62fffSJeremy L Thompson }
2209eaf62fffSJeremy L Thompson 
2210eaf62fffSJeremy L Thompson /**
2211ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a non-tensor basis for the active vector
2212eaf62fffSJeremy L Thompson 
221358e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2214f04ea552SJeremy L Thompson 
2215eaf62fffSJeremy L Thompson   @param[in]  op_fine       Fine grid operator
221685bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2217eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid restriction
2218eaf62fffSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector basis
221985bb9dcfSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
2220eaf62fffSJeremy L Thompson   @param[out] op_coarse     Coarse grid operator
222185bb9dcfSJeremy L Thompson   @param[out] op_prolong    Coarse to fine operator, or NULL
222285bb9dcfSJeremy L Thompson   @param[out] op_restrict   Fine to coarse operator, or NULL
2223eaf62fffSJeremy L Thompson 
2224eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2225eaf62fffSJeremy L Thompson 
2226eaf62fffSJeremy L Thompson   @ref User
2227eaf62fffSJeremy L Thompson **/
22282b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
22292b730f8bSJeremy L Thompson                                        const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
2230eaf62fffSJeremy L Thompson                                        CeedOperator *op_restrict) {
22312b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2232eaf62fffSJeremy L Thompson   Ceed ceed;
22332b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2234eaf62fffSJeremy L Thompson 
2235eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
2236eaf62fffSJeremy L Thompson   CeedBasis basis_fine;
22372b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
2238eaf62fffSJeremy L Thompson   CeedInt Q_f, Q_c;
22392b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
22402b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
22412b730f8bSJeremy L Thompson   if (Q_f != Q_c) {
2242eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
22432b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2244eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
22452b730f8bSJeremy L Thompson   }
2246eaf62fffSJeremy L Thompson 
2247eaf62fffSJeremy L Thompson   // Coarse to fine basis
224883d6adf3SZach Atkins   CeedBasis basis_c_to_f = NULL;
224983d6adf3SZach Atkins   if (op_prolong || op_restrict) {
225083d6adf3SZach Atkins     // Check if interpolation matrix is provided
225183d6adf3SZach Atkins     if (!interp_c_to_f) {
225283d6adf3SZach Atkins       // LCOV_EXCL_START
225383d6adf3SZach Atkins       return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
225483d6adf3SZach Atkins       // LCOV_EXCL_STOP
225583d6adf3SZach Atkins     }
2256eaf62fffSJeremy L Thompson     CeedElemTopology topo;
22572b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_fine, &topo));
2258eaf62fffSJeremy L Thompson     CeedInt dim, num_comp, num_nodes_c, num_nodes_f;
22592b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
22602b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
22612b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f));
22622b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
2263eaf62fffSJeremy L Thompson     CeedScalar *q_ref, *q_weight, *grad;
22642b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref));
22652b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f, &q_weight));
22662b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad));
22672b730f8bSJeremy 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));
22682b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
22692b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
22702b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
227183d6adf3SZach Atkins   }
2272eaf62fffSJeremy L Thompson 
2273eaf62fffSJeremy L Thompson   // Core code
22742b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2275eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2276eaf62fffSJeremy L Thompson }
2277eaf62fffSJeremy L Thompson 
2278eaf62fffSJeremy L Thompson /**
2279ea61e9acSJeremy L Thompson   @brief Build a FDM based approximate inverse for each element for a CeedOperator
2280eaf62fffSJeremy L Thompson 
2281ea61e9acSJeremy L Thompson   This returns a CeedOperator and CeedVector to apply a Fast Diagonalization Method based approximate inverse.
2282ea61e9acSJeremy L Thompson     This function obtains the simultaneous diagonalization for the 1D mass and Laplacian operators, M = V^T V, K = V^T S V.
2283ea61e9acSJeremy L Thompson     The assembled QFunction is used to modify the eigenvalues from simultaneous diagonalization and obtain an approximate inverse of the form V^T
2284ea61e9acSJeremy L Thompson S^hat V. The CeedOperator must be linear and non-composite. The associated CeedQFunction must therefore also be linear.
2285eaf62fffSJeremy L Thompson 
2286ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2287f04ea552SJeremy L Thompson 
2288ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to create element inverses
2289ea61e9acSJeremy L Thompson   @param[out] fdm_inv CeedOperator to apply the action of a FDM based inverse for each element
2290ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2291eaf62fffSJeremy L Thompson 
2292eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2293eaf62fffSJeremy L Thompson 
2294480fae85SJeremy L Thompson   @ref User
2295eaf62fffSJeremy L Thompson **/
22962b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) {
22972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2298eaf62fffSJeremy L Thompson 
2299eaf62fffSJeremy L Thompson   if (op->CreateFDMElementInverse) {
2300d04bbc78SJeremy L Thompson     // Backend version
23012b730f8bSJeremy L Thompson     CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request));
2302eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2303eaf62fffSJeremy L Thompson   } else {
2304d04bbc78SJeremy L Thompson     // Operator fallback
2305d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2306d04bbc78SJeremy L Thompson 
23072b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2308d04bbc78SJeremy L Thompson     if (op_fallback) {
23092b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request));
2310eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2311eaf62fffSJeremy L Thompson     }
2312eaf62fffSJeremy L Thompson   }
2313eaf62fffSJeremy L Thompson 
2314d04bbc78SJeremy L Thompson   // Default interface implementation
2315eaf62fffSJeremy L Thompson   Ceed ceed, ceed_parent;
23162b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
23172b730f8bSJeremy L Thompson   CeedCall(CeedGetOperatorFallbackParentCeed(ceed, &ceed_parent));
2318eaf62fffSJeremy L Thompson   ceed_parent = ceed_parent ? ceed_parent : ceed;
2319eaf62fffSJeremy L Thompson   CeedQFunction qf;
23202b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
2321eaf62fffSJeremy L Thompson 
2322eaf62fffSJeremy L Thompson   // Determine active input basis
2323eaf62fffSJeremy L Thompson   bool                interp = false, grad = false;
2324eaf62fffSJeremy L Thompson   CeedBasis           basis = NULL;
2325eaf62fffSJeremy L Thompson   CeedElemRestriction rstr  = NULL;
2326eaf62fffSJeremy L Thompson   CeedOperatorField  *op_fields;
2327eaf62fffSJeremy L Thompson   CeedQFunctionField *qf_fields;
2328eaf62fffSJeremy L Thompson   CeedInt             num_input_fields;
23292b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL));
23302b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
2331eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
2332eaf62fffSJeremy L Thompson     CeedVector vec;
23332b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
2334eaf62fffSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2335eaf62fffSJeremy L Thompson       CeedEvalMode eval_mode;
23362b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
2337eaf62fffSJeremy L Thompson       interp = interp || eval_mode == CEED_EVAL_INTERP;
2338eaf62fffSJeremy L Thompson       grad   = grad || eval_mode == CEED_EVAL_GRAD;
23392b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis));
23402b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
2341eaf62fffSJeremy L Thompson     }
2342eaf62fffSJeremy L Thompson   }
23432b730f8bSJeremy L Thompson   if (!basis) {
2344eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
2345eaf62fffSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "No active field set");
2346eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
23472b730f8bSJeremy L Thompson   }
2348e79b91d9SJeremy L Thompson   CeedSize l_size = 1;
2349e79b91d9SJeremy L Thompson   CeedInt  P_1d, Q_1d, elem_size, num_qpts, dim, num_comp = 1, num_elem = 1;
23502b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
23512b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis, &elem_size));
23522b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
23532b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
23542b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
23552b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
23562b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
23572b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
2358eaf62fffSJeremy L Thompson 
2359eaf62fffSJeremy L Thompson   // Build and diagonalize 1D Mass and Laplacian
2360eaf62fffSJeremy L Thompson   bool tensor_basis;
23612b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &tensor_basis));
23622b730f8bSJeremy L Thompson   if (!tensor_basis) {
2363eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
23642b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases");
2365eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
23662b730f8bSJeremy L Thompson   }
2367eaf62fffSJeremy L Thompson   CeedScalar *mass, *laplace, *x, *fdm_interp, *lambda;
23682b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &mass));
23692b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &laplace));
23702b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &x));
23712b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp));
23722b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &lambda));
2373eaf62fffSJeremy L Thompson   // -- Build matrices
2374eaf62fffSJeremy L Thompson   const CeedScalar *interp_1d, *grad_1d, *q_weight_1d;
23752b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
23762b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
23772b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
23782b730f8bSJeremy L Thompson   CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace));
2379eaf62fffSJeremy L Thompson 
2380eaf62fffSJeremy L Thompson   // -- Diagonalize
23812b730f8bSJeremy L Thompson   CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d));
23822b730f8bSJeremy L Thompson   CeedCall(CeedFree(&mass));
23832b730f8bSJeremy L Thompson   CeedCall(CeedFree(&laplace));
23842b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
23852b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d];
23862b730f8bSJeremy L Thompson   }
23872b730f8bSJeremy L Thompson   CeedCall(CeedFree(&x));
2388eaf62fffSJeremy L Thompson 
2389eaf62fffSJeremy L Thompson   // Assemble QFunction
2390eaf62fffSJeremy L Thompson   CeedVector          assembled;
2391eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_qf;
23922b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request));
2393eaf62fffSJeremy L Thompson   CeedInt layout[3];
23942b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_qf, &layout));
23952b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qf));
2396eaf62fffSJeremy L Thompson   CeedScalar max_norm = 0;
23972b730f8bSJeremy L Thompson   CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm));
2398eaf62fffSJeremy L Thompson 
2399eaf62fffSJeremy L Thompson   // Calculate element averages
2400eaf62fffSJeremy L Thompson   CeedInt           num_modes = (interp ? 1 : 0) + (grad ? dim : 0);
2401eaf62fffSJeremy L Thompson   CeedScalar       *elem_avg;
2402eaf62fffSJeremy L Thompson   const CeedScalar *assembled_array, *q_weight_array;
2403eaf62fffSJeremy L Thompson   CeedVector        q_weight;
24042b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight));
24052b730f8bSJeremy L Thompson   CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight));
24062b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array));
24072b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array));
24082b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_elem, &elem_avg));
2409eaf62fffSJeremy L Thompson   const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON;
2410eaf62fffSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
2411eaf62fffSJeremy L Thompson     CeedInt count = 0;
24122b730f8bSJeremy L Thompson     for (CeedInt q = 0; q < num_qpts; q++) {
24132b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) {
24142b730f8bSJeremy L Thompson         if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) {
24152b730f8bSJeremy L Thompson           elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q];
2416eaf62fffSJeremy L Thompson           count++;
2417eaf62fffSJeremy L Thompson         }
24182b730f8bSJeremy L Thompson       }
24192b730f8bSJeremy L Thompson     }
2420eaf62fffSJeremy L Thompson     if (count) {
2421eaf62fffSJeremy L Thompson       elem_avg[e] /= count;
2422eaf62fffSJeremy L Thompson     } else {
2423eaf62fffSJeremy L Thompson       elem_avg[e] = 1.0;
2424eaf62fffSJeremy L Thompson     }
2425eaf62fffSJeremy L Thompson   }
24262b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array));
24272b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled));
24282b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array));
24292b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_weight));
2430eaf62fffSJeremy L Thompson 
2431eaf62fffSJeremy L Thompson   // Build FDM diagonal
2432eaf62fffSJeremy L Thompson   CeedVector  q_data;
2433eaf62fffSJeremy L Thompson   CeedScalar *q_data_array, *fdm_diagonal;
24342b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_comp * elem_size, &fdm_diagonal));
2435eaf62fffSJeremy L Thompson   const CeedScalar fdm_diagonal_bound = elem_size * CEED_EPSILON;
24362b730f8bSJeremy L Thompson   for (CeedInt c = 0; c < num_comp; c++) {
2437eaf62fffSJeremy L Thompson     for (CeedInt n = 0; n < elem_size; n++) {
24382b730f8bSJeremy L Thompson       if (interp) fdm_diagonal[c * elem_size + n] = 1.0;
24392b730f8bSJeremy L Thompson       if (grad) {
2440eaf62fffSJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
2441eaf62fffSJeremy L Thompson           CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d;
2442eaf62fffSJeremy L Thompson           fdm_diagonal[c * elem_size + n] += lambda[i];
2443eaf62fffSJeremy L Thompson         }
2444eaf62fffSJeremy L Thompson       }
24452b730f8bSJeremy L Thompson       if (fabs(fdm_diagonal[c * elem_size + n]) < fdm_diagonal_bound) fdm_diagonal[c * elem_size + n] = fdm_diagonal_bound;
24462b730f8bSJeremy L Thompson     }
24472b730f8bSJeremy L Thompson   }
24482b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * elem_size, &q_data));
24492b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(q_data, 0.0));
24502b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array));
24512b730f8bSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
24522b730f8bSJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
24532b730f8bSJeremy 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]);
24542b730f8bSJeremy L Thompson     }
24552b730f8bSJeremy L Thompson   }
24562b730f8bSJeremy L Thompson   CeedCall(CeedFree(&elem_avg));
24572b730f8bSJeremy L Thompson   CeedCall(CeedFree(&fdm_diagonal));
24582b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(q_data, &q_data_array));
2459eaf62fffSJeremy L Thompson 
2460eaf62fffSJeremy L Thompson   // Setup FDM operator
2461eaf62fffSJeremy L Thompson   // -- Basis
2462eaf62fffSJeremy L Thompson   CeedBasis   fdm_basis;
2463eaf62fffSJeremy L Thompson   CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy;
24642b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy));
24652b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &q_ref_dummy));
24662b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &q_weight_dummy));
24672b730f8bSJeremy 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));
24682b730f8bSJeremy L Thompson   CeedCall(CeedFree(&fdm_interp));
24692b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_dummy));
24702b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref_dummy));
24712b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight_dummy));
24722b730f8bSJeremy L Thompson   CeedCall(CeedFree(&lambda));
2473eaf62fffSJeremy L Thompson 
2474eaf62fffSJeremy L Thompson   // -- Restriction
2475eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_qd_i;
2476eaf62fffSJeremy L Thompson   CeedInt             strides[3] = {1, elem_size, elem_size * num_comp};
24772b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, elem_size, num_comp, num_elem * num_comp * elem_size, strides, &rstr_qd_i));
2478eaf62fffSJeremy L Thompson   // -- QFunction
2479eaf62fffSJeremy L Thompson   CeedQFunction qf_fdm;
24802b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm));
24812b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP));
24822b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE));
24832b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP));
24842b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp));
2485eaf62fffSJeremy L Thompson   // -- QFunction context
2486eaf62fffSJeremy L Thompson   CeedInt *num_comp_data;
24872b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, &num_comp_data));
2488eaf62fffSJeremy L Thompson   num_comp_data[0] = num_comp;
2489eaf62fffSJeremy L Thompson   CeedQFunctionContext ctx_fdm;
24902b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm));
24912b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data));
24922b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm));
24932b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_fdm));
2494eaf62fffSJeremy L Thompson   // -- Operator
24952b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv));
24962b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
24972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_COLLOCATED, q_data));
24982b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2499eaf62fffSJeremy L Thompson 
2500eaf62fffSJeremy L Thompson   // Cleanup
25012b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_data));
25022b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&fdm_basis));
25032b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i));
25042b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_fdm));
2505eaf62fffSJeremy L Thompson 
2506eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2507eaf62fffSJeremy L Thompson }
2508eaf62fffSJeremy L Thompson 
2509eaf62fffSJeremy L Thompson /// @}
2510