xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-preconditioning.c (revision 1862681b00cfb75effddf0202f2fd4709130cdc7)
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 
82b730f8bSJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
11c85e8640SSebastian Grimberg #include <assert.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 
44*1862681bSJeremy Luke Thompson   char *source_path_with_name = NULL;
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 
183352a5e7cSSebastian Grimberg   @param[in]  basis     CeedBasis from which to get the basis matrix
184eaf62fffSJeremy L Thompson   @param[in]  eval_mode Current basis evaluation mode
185eaf62fffSJeremy L Thompson   @param[in]  identity  Pointer to identity matrix
186eaf62fffSJeremy L Thompson   @param[out] basis_ptr Basis pointer to set
187eaf62fffSJeremy L Thompson 
188eaf62fffSJeremy L Thompson   @ref Developer
189eaf62fffSJeremy L Thompson **/
190352a5e7cSSebastian Grimberg static inline int CeedOperatorGetBasisPointer(CeedBasis basis, CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar **basis_ptr) {
191eaf62fffSJeremy L Thompson   switch (eval_mode) {
192eaf62fffSJeremy L Thompson     case CEED_EVAL_NONE:
193eaf62fffSJeremy L Thompson       *basis_ptr = identity;
194eaf62fffSJeremy L Thompson       break;
195eaf62fffSJeremy L Thompson     case CEED_EVAL_INTERP:
196352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetInterp(basis, basis_ptr));
197eaf62fffSJeremy L Thompson       break;
198eaf62fffSJeremy L Thompson     case CEED_EVAL_GRAD:
199352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetGrad(basis, basis_ptr));
200352a5e7cSSebastian Grimberg       break;
201352a5e7cSSebastian Grimberg     case CEED_EVAL_DIV:
202352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetDiv(basis, basis_ptr));
203352a5e7cSSebastian Grimberg       break;
204352a5e7cSSebastian Grimberg     case CEED_EVAL_CURL:
205352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetCurl(basis, basis_ptr));
206eaf62fffSJeremy L Thompson       break;
207eaf62fffSJeremy L Thompson     case CEED_EVAL_WEIGHT:
208eaf62fffSJeremy L Thompson       break;  // Caught by QF Assembly
209eaf62fffSJeremy L Thompson   }
210ed9e99e6SJeremy L Thompson   assert(*basis_ptr != NULL);
211352a5e7cSSebastian Grimberg 
212352a5e7cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
213eaf62fffSJeremy L Thompson }
214eaf62fffSJeremy L Thompson 
215eaf62fffSJeremy L Thompson /**
216eaf62fffSJeremy L Thompson   @brief Create point block restriction for active operator field
217eaf62fffSJeremy L Thompson 
218eaf62fffSJeremy L Thompson   @param[in]  rstr            Original CeedElemRestriction for active field
219ea61e9acSJeremy L Thompson   @param[out] pointblock_rstr Address of the variable where the newly created CeedElemRestriction will be stored
220eaf62fffSJeremy L Thompson 
221eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
222eaf62fffSJeremy L Thompson 
223eaf62fffSJeremy L Thompson   @ref Developer
224eaf62fffSJeremy L Thompson **/
2252b730f8bSJeremy L Thompson static int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *pointblock_rstr) {
226eaf62fffSJeremy L Thompson   Ceed ceed;
2272b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
228eaf62fffSJeremy L Thompson   const CeedInt *offsets;
2292b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
230eaf62fffSJeremy L Thompson 
231eaf62fffSJeremy L Thompson   // Expand offsets
2327b63f5c6SJed Brown   CeedInt  num_elem, num_comp, elem_size, comp_stride, *pointblock_offsets;
2337b63f5c6SJed Brown   CeedSize l_size;
2342b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
2352b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
2362b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
2372b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
2382b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
239eaf62fffSJeremy L Thompson   CeedInt shift = num_comp;
2402b730f8bSJeremy L Thompson   if (comp_stride != 1) shift *= num_comp;
2412b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_elem * elem_size, &pointblock_offsets));
242eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_elem * elem_size; i++) {
243eaf62fffSJeremy L Thompson     pointblock_offsets[i] = offsets[i] * shift;
244eaf62fffSJeremy L Thompson   }
245eaf62fffSJeremy L Thompson 
246eaf62fffSJeremy L Thompson   // Create new restriction
2472b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
2482b730f8bSJeremy L Thompson                                      pointblock_offsets, pointblock_rstr));
249eaf62fffSJeremy L Thompson 
250eaf62fffSJeremy L Thompson   // Cleanup
2512b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
252eaf62fffSJeremy L Thompson 
253eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
254eaf62fffSJeremy L Thompson }
255eaf62fffSJeremy L Thompson 
256eaf62fffSJeremy L Thompson /**
257eaf62fffSJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
258eaf62fffSJeremy L Thompson 
259eaf62fffSJeremy L Thompson   @param[in]  op            CeedOperator to assemble point block diagonal
260ea61e9acSJeremy L Thompson   @param[in]  request       Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE
261eaf62fffSJeremy L Thompson   @param[in]  is_pointblock Boolean flag to assemble diagonal or point block diagonal
262eaf62fffSJeremy L Thompson   @param[out] assembled     CeedVector to store assembled diagonal
263eaf62fffSJeremy L Thompson 
264eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
265eaf62fffSJeremy L Thompson 
266eaf62fffSJeremy L Thompson   @ref Developer
267eaf62fffSJeremy L Thompson **/
2682b730f8bSJeremy L Thompson static inline int CeedSingleOperatorAssembleAddDiagonal_Core(CeedOperator op, CeedRequest *request, const bool is_pointblock, CeedVector assembled) {
269eaf62fffSJeremy L Thompson   Ceed ceed;
2702b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
271eaf62fffSJeremy L Thompson 
272eaf62fffSJeremy L Thompson   // Assemble QFunction
273eaf62fffSJeremy L Thompson   CeedQFunction       qf;
274437c7c90SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
275c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
276c5f45aeaSJeremy L Thompson   CeedElemRestriction assembled_elem_rstr = NULL;
277437c7c90SJeremy L Thompson   CeedInt             num_input_fields, num_output_fields;
278eaf62fffSJeremy L Thompson   CeedInt             layout[3];
279437c7c90SJeremy L Thompson 
280437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
281437c7c90SJeremy L Thompson   CeedCall(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
282437c7c90SJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request));
283437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, &layout));
284437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
285437c7c90SJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
286eaf62fffSJeremy L Thompson 
287ed9e99e6SJeremy L Thompson   // Get assembly data
288ed9e99e6SJeremy L Thompson   CeedOperatorAssemblyData data;
289437c7c90SJeremy L Thompson   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
290437c7c90SJeremy L Thompson   CeedInt                 *num_eval_modes_in, *num_eval_modes_out, num_active_bases;
291437c7c90SJeremy L Thompson   CeedSize               **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components;
292437c7c90SJeremy L Thompson   CeedBasis               *active_bases;
293437c7c90SJeremy L Thompson   CeedElemRestriction     *active_elem_rstrs;
294437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
295437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in,
296437c7c90SJeremy L Thompson                                                 &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out, &num_output_components));
297437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases, NULL, NULL));
298437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs));
299437c7c90SJeremy L Thompson 
300437c7c90SJeremy L Thompson   // Loop over all active bases
301437c7c90SJeremy L Thompson   for (CeedInt b = 0; b < num_active_bases; b++) {
302eaf62fffSJeremy L Thompson     // Assemble point block diagonal restriction, if needed
303437c7c90SJeremy L Thompson     CeedElemRestriction diag_elem_rstr = active_elem_rstrs[b];
304437c7c90SJeremy L Thompson 
305eaf62fffSJeremy L Thompson     if (is_pointblock) {
306437c7c90SJeremy L Thompson       CeedElemRestriction point_block_elem_rstr;
307437c7c90SJeremy L Thompson 
308437c7c90SJeremy L Thompson       CeedCall(CeedOperatorCreateActivePointBlockRestriction(diag_elem_rstr, &point_block_elem_rstr));
309437c7c90SJeremy L Thompson       diag_elem_rstr = point_block_elem_rstr;
310eaf62fffSJeremy L Thompson     }
311eaf62fffSJeremy L Thompson 
312eaf62fffSJeremy L Thompson     // Create diagonal vector
313eaf62fffSJeremy L Thompson     CeedVector elem_diag;
314437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag));
315eaf62fffSJeremy L Thompson 
316eaf62fffSJeremy L Thompson     // Assemble element operator diagonals
3179c774eddSJeremy L Thompson     CeedScalar *elem_diag_array;
318437c7c90SJeremy L Thompson     CeedInt     num_elem, num_nodes, num_qpts, num_components;
319437c7c90SJeremy L Thompson 
3202b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(elem_diag, 0.0));
3212b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array));
322437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem));
323437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(active_bases[b], &num_nodes));
324437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(active_bases[b], &num_components));
325437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(active_bases[b], &num_qpts));
326ed9e99e6SJeremy L Thompson 
327352a5e7cSSebastian Grimberg     // Construct identity matrix for basis if required
328ed9e99e6SJeremy L Thompson     bool        has_eval_none = false;
329352a5e7cSSebastian Grimberg     CeedScalar *identity      = NULL;
330437c7c90SJeremy L Thompson     for (CeedInt i = 0; i < num_eval_modes_in[b]; i++) {
331437c7c90SJeremy L Thompson       has_eval_none = has_eval_none || (eval_modes_in[b][i] == CEED_EVAL_NONE);
332ed9e99e6SJeremy L Thompson     }
333437c7c90SJeremy L Thompson     for (CeedInt i = 0; i < num_eval_modes_out[b]; i++) {
334437c7c90SJeremy L Thompson       has_eval_none = has_eval_none || (eval_modes_out[b][i] == CEED_EVAL_NONE);
335ed9e99e6SJeremy L Thompson     }
336ed9e99e6SJeremy L Thompson     if (has_eval_none) {
3372b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
3382b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
339eaf62fffSJeremy L Thompson     }
340352a5e7cSSebastian Grimberg 
341eaf62fffSJeremy L Thompson     // Compute the diagonal of B^T D B
342eaf62fffSJeremy L Thompson     // Each element
343eaf62fffSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
344eaf62fffSJeremy L Thompson       // Each basis eval mode pair
345352a5e7cSSebastian Grimberg       CeedInt      d_out              = 0, q_comp_out;
346352a5e7cSSebastian Grimberg       CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
347437c7c90SJeremy L Thompson       for (CeedInt e_out = 0; e_out < num_eval_modes_out[b]; e_out++) {
348437c7c90SJeremy L Thompson         const CeedScalar *B_t = NULL;
349352a5e7cSSebastian Grimberg         CeedOperatorGetBasisPointer(active_bases[b], eval_modes_out[b][e_out], identity, &B_t);
350352a5e7cSSebastian Grimberg         CeedCall(CeedBasisGetNumQuadratureComponents(active_bases[b], eval_modes_out[b][e_out], &q_comp_out));
351352a5e7cSSebastian Grimberg         if (q_comp_out > 1) {
352352a5e7cSSebastian Grimberg           if (e_out == 0 || eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0;
353352a5e7cSSebastian Grimberg           else B_t = &B_t[(++d_out) * num_qpts * num_nodes];
354352a5e7cSSebastian Grimberg         }
355352a5e7cSSebastian Grimberg         eval_mode_out_prev = eval_modes_out[b][e_out];
356352a5e7cSSebastian Grimberg 
357352a5e7cSSebastian Grimberg         CeedInt      d_in              = 0, q_comp_in;
358352a5e7cSSebastian Grimberg         CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE;
359437c7c90SJeremy L Thompson         for (CeedInt e_in = 0; e_in < num_eval_modes_in[b]; e_in++) {
360437c7c90SJeremy L Thompson           const CeedScalar *B = NULL;
361352a5e7cSSebastian Grimberg           CeedOperatorGetBasisPointer(active_bases[b], eval_modes_in[b][e_in], identity, &B);
362352a5e7cSSebastian Grimberg           CeedCall(CeedBasisGetNumQuadratureComponents(active_bases[b], eval_modes_in[b][e_in], &q_comp_in));
363352a5e7cSSebastian Grimberg           if (q_comp_in > 1) {
364352a5e7cSSebastian Grimberg             if (e_in == 0 || eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0;
365352a5e7cSSebastian Grimberg             else B = &B[(++d_in) * num_qpts * num_nodes];
366352a5e7cSSebastian Grimberg           }
367352a5e7cSSebastian Grimberg           eval_mode_in_prev = eval_modes_in[b][e_in];
368352a5e7cSSebastian Grimberg 
369eaf62fffSJeremy L Thompson           // Each component
370437c7c90SJeremy L Thompson           for (CeedInt c_out = 0; c_out < num_components; c_out++) {
371437c7c90SJeremy L Thompson             // Each qpt/node pair
3722b730f8bSJeremy L Thompson             for (CeedInt q = 0; q < num_qpts; q++) {
373eaf62fffSJeremy L Thompson               if (is_pointblock) {
374eaf62fffSJeremy L Thompson                 // Point Block Diagonal
375437c7c90SJeremy L Thompson                 for (CeedInt c_in = 0; c_in < num_components; c_in++) {
376437c7c90SJeremy 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;
377437c7c90SJeremy L Thompson                   const CeedScalar qf_value = assembled_qf_array[q * layout[0] + c_offset * layout[1] + e * layout[2]];
3782b730f8bSJeremy L Thompson                   for (CeedInt n = 0; n < num_nodes; n++) {
379437c7c90SJeremy L Thompson                     elem_diag_array[((e * num_components + c_out) * num_components + c_in) * num_nodes + n] +=
380437c7c90SJeremy L Thompson                         B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n];
381eaf62fffSJeremy L Thompson                   }
3822b730f8bSJeremy L Thompson                 }
383eaf62fffSJeremy L Thompson               } else {
384eaf62fffSJeremy L Thompson                 // Diagonal Only
385437c7c90SJeremy 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;
386437c7c90SJeremy L Thompson                 const CeedScalar qf_value = assembled_qf_array[q * layout[0] + c_offset * layout[1] + e * layout[2]];
3872b730f8bSJeremy L Thompson                 for (CeedInt n = 0; n < num_nodes; n++) {
388437c7c90SJeremy 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];
389eaf62fffSJeremy L Thompson                 }
390eaf62fffSJeremy L Thompson               }
391eaf62fffSJeremy L Thompson             }
392eaf62fffSJeremy L Thompson           }
3932b730f8bSJeremy L Thompson         }
3942b730f8bSJeremy L Thompson       }
3952b730f8bSJeremy L Thompson     }
3962b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
397eaf62fffSJeremy L Thompson 
398eaf62fffSJeremy L Thompson     // Assemble local operator diagonal
399f30b1135SSebastian Grimberg     CeedCall(CeedElemRestrictionApplyUnsigned(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
400eaf62fffSJeremy L Thompson 
401eaf62fffSJeremy L Thompson     // Cleanup
402437c7c90SJeremy L Thompson     if (is_pointblock) CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr));
4032b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&elem_diag));
4042b730f8bSJeremy L Thompson     CeedCall(CeedFree(&identity));
405437c7c90SJeremy L Thompson   }
406437c7c90SJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
407437c7c90SJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
408eaf62fffSJeremy L Thompson 
409eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
410eaf62fffSJeremy L Thompson }
411eaf62fffSJeremy L Thompson 
412eaf62fffSJeremy L Thompson /**
413eaf62fffSJeremy L Thompson   @brief Core logic for assembling composite operator diagonal
414eaf62fffSJeremy L Thompson 
415eaf62fffSJeremy L Thompson   @param[in]  op            CeedOperator to assemble point block diagonal
416ea61e9acSJeremy L Thompson   @param[in]  request       Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE
417eaf62fffSJeremy L Thompson   @param[in]  is_pointblock Boolean flag to assemble diagonal or point block diagonal
418eaf62fffSJeremy L Thompson   @param[out] assembled     CeedVector to store assembled diagonal
419eaf62fffSJeremy L Thompson 
420eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
421eaf62fffSJeremy L Thompson 
422eaf62fffSJeremy L Thompson   @ref Developer
423eaf62fffSJeremy L Thompson **/
4242b730f8bSJeremy L Thompson static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_pointblock,
425eaf62fffSJeremy L Thompson                                                                  CeedVector assembled) {
426eaf62fffSJeremy L Thompson   CeedInt       num_sub;
427eaf62fffSJeremy L Thompson   CeedOperator *suboperators;
428c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
429c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators));
430eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_sub; i++) {
4316aa95790SJeremy L Thompson     if (is_pointblock) {
4322b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request));
4336aa95790SJeremy L Thompson     } else {
4342b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request));
4356aa95790SJeremy L Thompson     }
436eaf62fffSJeremy L Thompson   }
437eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
438eaf62fffSJeremy L Thompson }
439eaf62fffSJeremy L Thompson 
440eaf62fffSJeremy L Thompson /**
441eaf62fffSJeremy L Thompson   @brief Build nonzero pattern for non-composite operator
442eaf62fffSJeremy L Thompson 
443eaf62fffSJeremy L Thompson   Users should generally use CeedOperatorLinearAssembleSymbolic()
444eaf62fffSJeremy L Thompson 
4453bdd4e5aSSebastian Grimberg   Note: For operators using oriented element restrictions, entries in rows or cols may be negative indicating the assembled value at this nonzero
4463bdd4e5aSSebastian Grimberg should be negated
447f30b1135SSebastian Grimberg 
448eaf62fffSJeremy L Thompson   @param[in]  op     CeedOperator to assemble nonzero pattern
449eaf62fffSJeremy L Thompson   @param[in]  offset Offset for number of entries
450eaf62fffSJeremy L Thompson   @param[out] rows   Row number for each entry
451eaf62fffSJeremy L Thompson   @param[out] cols   Column number for each entry
452eaf62fffSJeremy L Thompson 
453eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
454eaf62fffSJeremy L Thompson 
455eaf62fffSJeremy L Thompson   @ref Developer
456eaf62fffSJeremy L Thompson **/
4572b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) {
458f3d47e36SJeremy L Thompson   Ceed ceed;
459f3d47e36SJeremy L Thompson   bool is_composite;
460f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
461f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
462f3d47e36SJeremy L Thompson 
4636574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
464eaf62fffSJeremy L Thompson 
465c9366a6bSJeremy L Thompson   CeedSize num_nodes;
4662b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes, NULL));
467eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_in;
4682b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &rstr_in));
469e79b91d9SJeremy L Thompson   CeedInt num_elem, elem_size, num_comp;
4702b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem));
4712b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size));
4722b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp));
473eaf62fffSJeremy L Thompson   CeedInt layout_er[3];
4742b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_in, &layout_er));
475eaf62fffSJeremy L Thompson 
476eaf62fffSJeremy L Thompson   CeedInt local_num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
477eaf62fffSJeremy L Thompson 
478eaf62fffSJeremy L Thompson   // Determine elem_dof relation
479eaf62fffSJeremy L Thompson   CeedVector index_vec;
4802b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, num_nodes, &index_vec));
481eaf62fffSJeremy L Thompson   CeedScalar *array;
4822b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(index_vec, CEED_MEM_HOST, &array));
483ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_nodes; i++) array[i] = i;
4842b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(index_vec, &array));
485eaf62fffSJeremy L Thompson   CeedVector elem_dof;
4862b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, num_elem * elem_size * num_comp, &elem_dof));
4872b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(elem_dof, 0.0));
4882b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(rstr_in, CEED_NOTRANSPOSE, index_vec, elem_dof, CEED_REQUEST_IMMEDIATE));
489eaf62fffSJeremy L Thompson   const CeedScalar *elem_dof_a;
4902b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(elem_dof, CEED_MEM_HOST, &elem_dof_a));
4912b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&index_vec));
492eaf62fffSJeremy L Thompson 
493eaf62fffSJeremy L Thompson   // Determine i, j locations for element matrices
494eaf62fffSJeremy L Thompson   CeedInt count = 0;
495ed9e99e6SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
496ed9e99e6SJeremy L Thompson     for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
497ed9e99e6SJeremy L Thompson       for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
498ed9e99e6SJeremy L Thompson         for (CeedInt i = 0; i < elem_size; i++) {
499ed9e99e6SJeremy L Thompson           for (CeedInt j = 0; j < elem_size; j++) {
5002b730f8bSJeremy L Thompson             const CeedInt elem_dof_index_row = i * layout_er[0] + (comp_out)*layout_er[1] + e * layout_er[2];
5012b730f8bSJeremy L Thompson             const CeedInt elem_dof_index_col = j * layout_er[0] + comp_in * layout_er[1] + e * layout_er[2];
502eaf62fffSJeremy L Thompson 
503eaf62fffSJeremy L Thompson             const CeedInt row = elem_dof_a[elem_dof_index_row];
504eaf62fffSJeremy L Thompson             const CeedInt col = elem_dof_a[elem_dof_index_col];
505eaf62fffSJeremy L Thompson 
506eaf62fffSJeremy L Thompson             rows[offset + count] = row;
507eaf62fffSJeremy L Thompson             cols[offset + count] = col;
508eaf62fffSJeremy L Thompson             count++;
509eaf62fffSJeremy L Thompson           }
510eaf62fffSJeremy L Thompson         }
511eaf62fffSJeremy L Thompson       }
512eaf62fffSJeremy L Thompson     }
513eaf62fffSJeremy L Thompson   }
5146574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing assembled entries");
5152b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(elem_dof, &elem_dof_a));
5162b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&elem_dof));
517eaf62fffSJeremy L Thompson 
518eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
519eaf62fffSJeremy L Thompson }
520eaf62fffSJeremy L Thompson 
521eaf62fffSJeremy L Thompson /**
522eaf62fffSJeremy L Thompson   @brief Assemble nonzero entries for non-composite operator
523eaf62fffSJeremy L Thompson 
524eaf62fffSJeremy L Thompson   Users should generally use CeedOperatorLinearAssemble()
525eaf62fffSJeremy L Thompson 
526eaf62fffSJeremy L Thompson   @param[in]  op     CeedOperator to assemble
527ea61e9acSJeremy L Thompson   @param[in]  offset Offset for number of entries
528eaf62fffSJeremy L Thompson   @param[out] values Values to assemble into matrix
529eaf62fffSJeremy L Thompson 
530eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
531eaf62fffSJeremy L Thompson 
532eaf62fffSJeremy L Thompson   @ref Developer
533eaf62fffSJeremy L Thompson **/
5342b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) {
535f3d47e36SJeremy L Thompson   Ceed ceed;
536f3d47e36SJeremy L Thompson   bool is_composite;
537f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
538f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
539f3d47e36SJeremy L Thompson 
5406574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
541f3d47e36SJeremy L Thompson 
542f3d47e36SJeremy L Thompson   // Early exit for empty operator
543f3d47e36SJeremy L Thompson   {
544f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
545f3d47e36SJeremy L Thompson 
546f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
547f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
548f3d47e36SJeremy L Thompson   }
549eaf62fffSJeremy L Thompson 
550cefa2673SJeremy L Thompson   if (op->LinearAssembleSingle) {
551cefa2673SJeremy L Thompson     // Backend version
5522b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSingle(op, offset, values));
553cefa2673SJeremy L Thompson     return CEED_ERROR_SUCCESS;
554cefa2673SJeremy L Thompson   } else {
555cefa2673SJeremy L Thompson     // Operator fallback
556cefa2673SJeremy L Thompson     CeedOperator op_fallback;
557cefa2673SJeremy L Thompson 
5582b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
559cefa2673SJeremy L Thompson     if (op_fallback) {
5602b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values));
561cefa2673SJeremy L Thompson       return CEED_ERROR_SUCCESS;
562cefa2673SJeremy L Thompson     }
563cefa2673SJeremy L Thompson   }
564cefa2673SJeremy L Thompson 
565eaf62fffSJeremy L Thompson   // Assemble QFunction
566eaf62fffSJeremy L Thompson   CeedQFunction qf;
5672b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
568c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf = NULL;
569c5f45aeaSJeremy L Thompson   CeedElemRestriction rstr_q       = NULL;
5702b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE));
5711f9221feSJeremy L Thompson   CeedSize qf_length;
5722b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(assembled_qf, &qf_length));
573eaf62fffSJeremy L Thompson 
5747e7773b5SJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
575eaf62fffSJeremy L Thompson   CeedOperatorField *input_fields;
576eaf62fffSJeremy L Thompson   CeedOperatorField *output_fields;
5772b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
578eaf62fffSJeremy L Thompson 
579ed9e99e6SJeremy L Thompson   // Get assembly data
580ed9e99e6SJeremy L Thompson   CeedOperatorAssemblyData data;
5812b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
582437c7c90SJeremy L Thompson   const CeedEvalMode **eval_modes_in, **eval_modes_out;
583437c7c90SJeremy L Thompson   CeedInt             *num_eval_modes_in, *num_eval_modes_out, num_active_bases;
584437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases, &num_eval_modes_in, &eval_modes_in, NULL, &num_eval_modes_out,
585437c7c90SJeremy L Thompson                                                 &eval_modes_out, NULL, NULL));
586437c7c90SJeremy L Thompson   CeedBasis *bases;
587437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &bases, NULL, NULL));
588437c7c90SJeremy L Thompson   CeedBasis basis_in = bases[0];
589eaf62fffSJeremy L Thompson 
5906574a04fSJeremy L Thompson   CeedCheck(num_active_bases == 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator with multiple active bases");
5916574a04fSJeremy L Thompson   CeedCheck(num_eval_modes_in[0] > 0 && num_eval_modes_out[0] > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator with out inputs/outputs");
592eaf62fffSJeremy L Thompson 
593ed9e99e6SJeremy L Thompson   CeedElemRestriction active_rstr;
594eaf62fffSJeremy L Thompson   CeedInt             num_elem, elem_size, num_qpts, num_comp;
5952b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &active_rstr));
5962b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(active_rstr, &num_elem));
5972b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(active_rstr, &elem_size));
5982b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(active_rstr, &num_comp));
5992b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
600eaf62fffSJeremy L Thompson 
601eaf62fffSJeremy L Thompson   CeedInt local_num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
602eaf62fffSJeremy L Thompson 
603eaf62fffSJeremy L Thompson   // loop over elements and put in data structure
604eaf62fffSJeremy L Thompson   const CeedScalar *assembled_qf_array;
6052b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
606eaf62fffSJeremy L Thompson 
607eaf62fffSJeremy L Thompson   CeedInt layout_qf[3];
6082b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_q, &layout_qf));
6092b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_q));
610eaf62fffSJeremy L Thompson 
611eaf62fffSJeremy L Thompson   // we store B_mat_in, B_mat_out, BTD, elem_mat in row-major order
612437c7c90SJeremy L Thompson   const CeedScalar **B_mats_in, **B_mats_out;
613437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, NULL, &B_mats_in, &B_mats_out));
614437c7c90SJeremy L Thompson   const CeedScalar *B_mat_in = B_mats_in[0], *B_mat_out = B_mats_out[0];
615437c7c90SJeremy L Thompson   CeedScalar        BTD_mat[elem_size * num_qpts * num_eval_modes_in[0]];
616eaf62fffSJeremy L Thompson   CeedScalar        elem_mat[elem_size * elem_size];
61792ae7e47SJeremy L Thompson   CeedInt           count = 0;
618eaf62fffSJeremy L Thompson   CeedScalar       *vals;
61928ec399dSJeremy L Thompson   CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals));
620ed9e99e6SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
621ed9e99e6SJeremy L Thompson     for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
622ed9e99e6SJeremy L Thompson       for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
623ed9e99e6SJeremy L Thompson         // Compute B^T*D
624ed9e99e6SJeremy L Thompson         for (CeedInt n = 0; n < elem_size; n++) {
625ed9e99e6SJeremy L Thompson           for (CeedInt q = 0; q < num_qpts; q++) {
626437c7c90SJeremy L Thompson             for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) {
627437c7c90SJeremy L Thompson               const CeedInt btd_index = n * (num_qpts * num_eval_modes_in[0]) + (num_eval_modes_in[0] * q + e_in);
628067fd99fSJeremy L Thompson               CeedScalar    sum       = 0.0;
629437c7c90SJeremy L Thompson               for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) {
630437c7c90SJeremy L Thompson                 const CeedInt b_out_index     = (num_eval_modes_out[0] * q + e_out) * elem_size + n;
631437c7c90SJeremy L Thompson                 const CeedInt eval_mode_index = ((e_in * num_comp + comp_in) * num_eval_modes_out[0] + e_out) * num_comp + comp_out;
6322b730f8bSJeremy L Thompson                 const CeedInt qf_index        = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2];
633067fd99fSJeremy L Thompson                 sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index];
634eaf62fffSJeremy L Thompson               }
635067fd99fSJeremy L Thompson               BTD_mat[btd_index] = sum;
636ed9e99e6SJeremy L Thompson             }
637ed9e99e6SJeremy L Thompson           }
638eaf62fffSJeremy L Thompson         }
639eaf62fffSJeremy L Thompson         // form element matrix itself (for each block component)
640437c7c90SJeremy L Thompson         CeedCall(CeedMatrixMatrixMultiply(ceed, BTD_mat, B_mat_in, elem_mat, elem_size, elem_size, num_qpts * num_eval_modes_in[0]));
641eaf62fffSJeremy L Thompson 
642eaf62fffSJeremy L Thompson         // put element matrix in coordinate data structure
643ed9e99e6SJeremy L Thompson         for (CeedInt i = 0; i < elem_size; i++) {
644ed9e99e6SJeremy L Thompson           for (CeedInt j = 0; j < elem_size; j++) {
645eaf62fffSJeremy L Thompson             vals[offset + count] = elem_mat[i * elem_size + j];
646eaf62fffSJeremy L Thompson             count++;
647eaf62fffSJeremy L Thompson           }
648eaf62fffSJeremy L Thompson         }
649eaf62fffSJeremy L Thompson       }
650eaf62fffSJeremy L Thompson     }
651eaf62fffSJeremy L Thompson   }
6526574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing entries");
6532b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(values, &vals));
654eaf62fffSJeremy L Thompson 
6552b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
6562b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
657eaf62fffSJeremy L Thompson 
658eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
659eaf62fffSJeremy L Thompson }
660eaf62fffSJeremy L Thompson 
661eaf62fffSJeremy L Thompson /**
662eaf62fffSJeremy L Thompson   @brief Count number of entries for assembled CeedOperator
663eaf62fffSJeremy L Thompson 
664eaf62fffSJeremy L Thompson   @param[in]  op          CeedOperator to assemble
665eaf62fffSJeremy L Thompson   @param[out] num_entries Number of entries in assembled representation
666eaf62fffSJeremy L Thompson 
667eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
668eaf62fffSJeremy L Thompson 
669eaf62fffSJeremy L Thompson   @ref Utility
670eaf62fffSJeremy L Thompson **/
6712b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedInt *num_entries) {
672b275c451SJeremy L Thompson   bool                is_composite;
673eaf62fffSJeremy L Thompson   CeedElemRestriction rstr;
674eaf62fffSJeremy L Thompson   CeedInt             num_elem, elem_size, num_comp;
675eaf62fffSJeremy L Thompson 
676b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
6776574a04fSJeremy L Thompson   CeedCheck(!is_composite, op->ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
6782b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &rstr));
6792b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
6802b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
6812b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
682eaf62fffSJeremy L Thompson   *num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
683eaf62fffSJeremy L Thompson 
684eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
685eaf62fffSJeremy L Thompson }
686eaf62fffSJeremy L Thompson 
687eaf62fffSJeremy L Thompson /**
688ea61e9acSJeremy L Thompson   @brief Common code for creating a multigrid coarse operator and level transfer operators for a CeedOperator
689eaf62fffSJeremy L Thompson 
690eaf62fffSJeremy L Thompson   @param[in]  op_fine      Fine grid operator
69185bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
692eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid restriction
693eaf62fffSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector basis
69485bb9dcfSJeremy L Thompson   @param[in]  basis_c_to_f Basis for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
695eaf62fffSJeremy L Thompson   @param[out] op_coarse    Coarse grid operator
69685bb9dcfSJeremy L Thompson   @param[out] op_prolong   Coarse to fine operator, or NULL
69785bb9dcfSJeremy L Thompson   @param[out] op_restrict  Fine to coarse operator, or NULL
698eaf62fffSJeremy L Thompson 
699eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
700eaf62fffSJeremy L Thompson 
701eaf62fffSJeremy L Thompson   @ref Developer
702eaf62fffSJeremy L Thompson **/
7032b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
7042b730f8bSJeremy L Thompson                                             CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
705eaf62fffSJeremy L Thompson   Ceed                ceed;
70685bb9dcfSJeremy L Thompson   CeedVector          mult_vec         = NULL;
707c17ec2beSJeremy L Thompson   CeedElemRestriction rstr_p_mult_fine = NULL;
7082b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
709eaf62fffSJeremy L Thompson 
710eaf62fffSJeremy L Thompson   // Check for composite operator
711eaf62fffSJeremy L Thompson   bool is_composite;
7122b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op_fine, &is_composite));
7136574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported");
714eaf62fffSJeremy L Thompson 
715eaf62fffSJeremy L Thompson   // Coarse Grid
7162b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
717eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_fine = NULL;
718eaf62fffSJeremy L Thompson   // -- Clone input fields
71992ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < op_fine->qf->num_input_fields; i++) {
720eaf62fffSJeremy L Thompson     if (op_fine->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
721437c7c90SJeremy L Thompson       rstr_fine = op_fine->input_fields[i]->elem_rstr;
7222b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE));
723eaf62fffSJeremy L Thompson     } else {
724437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, op_fine->input_fields[i]->elem_rstr,
7252b730f8bSJeremy L Thompson                                     op_fine->input_fields[i]->basis, op_fine->input_fields[i]->vec));
726eaf62fffSJeremy L Thompson     }
727eaf62fffSJeremy L Thompson   }
728eaf62fffSJeremy L Thompson   // -- Clone output fields
72992ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < op_fine->qf->num_output_fields; i++) {
730eaf62fffSJeremy L Thompson     if (op_fine->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
7312b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE));
732eaf62fffSJeremy L Thompson     } else {
733437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, op_fine->output_fields[i]->elem_rstr,
7342b730f8bSJeremy L Thompson                                     op_fine->output_fields[i]->basis, op_fine->output_fields[i]->vec));
735eaf62fffSJeremy L Thompson     }
736eaf62fffSJeremy L Thompson   }
737af99e877SJeremy L Thompson   // -- Clone QFunctionAssemblyData
7382b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op_fine->qf_assembled, &(*op_coarse)->qf_assembled));
739eaf62fffSJeremy L Thompson 
740eaf62fffSJeremy L Thompson   // Multiplicity vector
74185bb9dcfSJeremy L Thompson   if (op_restrict || op_prolong) {
74285bb9dcfSJeremy L Thompson     CeedVector mult_e_vec;
74385bb9dcfSJeremy L Thompson 
744c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateUnsignedCopy(rstr_fine, &rstr_p_mult_fine));
7456574a04fSJeremy L Thompson     CeedCheck(p_mult_fine, ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector");
7462b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec));
7472b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_e_vec, 0.0));
748c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE));
7492b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_vec, 0.0));
750c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE));
7512b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&mult_e_vec));
7522b730f8bSJeremy L Thompson     CeedCall(CeedVectorReciprocal(mult_vec));
75385bb9dcfSJeremy L Thompson   }
754eaf62fffSJeremy L Thompson 
755addd79feSZach Atkins   // Clone name
756addd79feSZach Atkins   bool   has_name = op_fine->name;
757addd79feSZach Atkins   size_t name_len = op_fine->name ? strlen(op_fine->name) : 0;
758addd79feSZach Atkins   CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name));
759addd79feSZach Atkins 
76083d6adf3SZach Atkins   // Check that coarse to fine basis is provided if prolong/restrict operators are requested
7616574a04fSJeremy L Thompson   CeedCheck(basis_c_to_f || (!op_restrict && !op_prolong), ceed, CEED_ERROR_INCOMPATIBLE,
7626574a04fSJeremy L Thompson             "Prolongation or restriction operator creation requires coarse-to-fine basis");
76383d6adf3SZach Atkins 
76485bb9dcfSJeremy L Thompson   // Restriction/Prolongation Operators
765eaf62fffSJeremy L Thompson   CeedInt num_comp;
7662b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp));
767addd79feSZach Atkins 
768addd79feSZach Atkins   // Restriction
769addd79feSZach Atkins   if (op_restrict) {
770eaf62fffSJeremy L Thompson     CeedInt             *num_comp_r_data;
77185bb9dcfSJeremy L Thompson     CeedQFunction        qf_restrict;
77285bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_r;
77385bb9dcfSJeremy L Thompson 
77485bb9dcfSJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict));
7752b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_r_data));
776eaf62fffSJeremy L Thompson     num_comp_r_data[0] = num_comp;
7772b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r));
7782b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data));
7792b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r));
7802b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_r));
7812b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE));
7822b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE));
7832b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP));
7842b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp));
785eaf62fffSJeremy L Thompson 
7862b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict));
7872b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE));
788c17ec2beSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_p_mult_fine, CEED_BASIS_COLLOCATED, mult_vec));
7892b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
790eaf62fffSJeremy L Thompson 
791addd79feSZach Atkins     // Set name
792addd79feSZach Atkins     char *restriction_name;
793addd79feSZach Atkins     CeedCall(CeedCalloc(17 + name_len, &restriction_name));
794addd79feSZach Atkins     sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
795addd79feSZach Atkins     CeedCall(CeedOperatorSetName(*op_restrict, restriction_name));
796addd79feSZach Atkins     CeedCall(CeedFree(&restriction_name));
797addd79feSZach Atkins 
798addd79feSZach Atkins     // Check
799addd79feSZach Atkins     CeedCall(CeedOperatorCheckReady(*op_restrict));
800addd79feSZach Atkins 
801addd79feSZach Atkins     // Cleanup
802addd79feSZach Atkins     CeedCall(CeedQFunctionDestroy(&qf_restrict));
803addd79feSZach Atkins   }
804addd79feSZach Atkins 
805eaf62fffSJeremy L Thompson   // Prolongation
806addd79feSZach Atkins   if (op_prolong) {
807eaf62fffSJeremy L Thompson     CeedInt             *num_comp_p_data;
80885bb9dcfSJeremy L Thompson     CeedQFunction        qf_prolong;
80985bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_p;
81085bb9dcfSJeremy L Thompson 
81185bb9dcfSJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong));
8122b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_p_data));
813eaf62fffSJeremy L Thompson     num_comp_p_data[0] = num_comp;
8142b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p));
8152b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data));
8162b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p));
8172b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_p));
8182b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP));
8192b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE));
8202b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE));
8212b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp));
822eaf62fffSJeremy L Thompson 
8232b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong));
8242b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
825c17ec2beSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_p_mult_fine, CEED_BASIS_COLLOCATED, mult_vec));
8262b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE));
827eaf62fffSJeremy L Thompson 
828addd79feSZach Atkins     // Set name
829ea6b5821SJeremy L Thompson     char *prolongation_name;
8302b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(18 + name_len, &prolongation_name));
8312b730f8bSJeremy L Thompson     sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
8322b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name));
8332b730f8bSJeremy L Thompson     CeedCall(CeedFree(&prolongation_name));
834addd79feSZach Atkins 
835addd79feSZach Atkins     // Check
836addd79feSZach Atkins     CeedCall(CeedOperatorCheckReady(*op_prolong));
837addd79feSZach Atkins 
838addd79feSZach Atkins     // Cleanup
839addd79feSZach Atkins     CeedCall(CeedQFunctionDestroy(&qf_prolong));
840ea6b5821SJeremy L Thompson   }
841ea6b5821SJeremy L Thompson 
84258e4b056SJeremy L Thompson   // Check
84358e4b056SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(*op_coarse));
84458e4b056SJeremy L Thompson 
845eaf62fffSJeremy L Thompson   // Cleanup
8462b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_vec));
847c17ec2beSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine));
8482b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis_c_to_f));
849805fe78eSJeremy L Thompson 
850eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
851eaf62fffSJeremy L Thompson }
852eaf62fffSJeremy L Thompson 
853eaf62fffSJeremy L Thompson /**
854eaf62fffSJeremy L Thompson   @brief Build 1D mass matrix and Laplacian with perturbation
855eaf62fffSJeremy L Thompson 
856eaf62fffSJeremy L Thompson   @param[in]  interp_1d   Interpolation matrix in one dimension
857eaf62fffSJeremy L Thompson   @param[in]  grad_1d     Gradient matrix in one dimension
858eaf62fffSJeremy L Thompson   @param[in]  q_weight_1d Quadrature weights in one dimension
859eaf62fffSJeremy L Thompson   @param[in]  P_1d        Number of basis nodes in one dimension
860eaf62fffSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
861eaf62fffSJeremy L Thompson   @param[in]  dim         Dimension of basis
862eaf62fffSJeremy L Thompson   @param[out] mass        Assembled mass matrix in one dimension
863eaf62fffSJeremy L Thompson   @param[out] laplace     Assembled perturbed Laplacian in one dimension
864eaf62fffSJeremy L Thompson 
865eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
866eaf62fffSJeremy L Thompson 
867eaf62fffSJeremy L Thompson   @ref Developer
868eaf62fffSJeremy L Thompson **/
8692c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
8702c2ea1dbSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d, CeedInt P_1d, CeedInt Q_1d,
8712c2ea1dbSJeremy L Thompson                                 CeedInt dim, CeedScalar *mass, CeedScalar *laplace) {
8722b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
873eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
874eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
8752b730f8bSJeremy 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];
876eaf62fffSJeremy L Thompson       mass[i + j * P_1d] = sum;
877eaf62fffSJeremy L Thompson     }
8782b730f8bSJeremy L Thompson   }
879eaf62fffSJeremy L Thompson   // -- Laplacian
8802b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
881eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
882eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
8832b730f8bSJeremy 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];
884eaf62fffSJeremy L Thompson       laplace[i + j * P_1d] = sum;
885eaf62fffSJeremy L Thompson     }
8862b730f8bSJeremy L Thompson   }
887eaf62fffSJeremy L Thompson   CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4;
8882b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation;
889eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
890eaf62fffSJeremy L Thompson }
8912c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
892eaf62fffSJeremy L Thompson 
893eaf62fffSJeremy L Thompson /// @}
894eaf62fffSJeremy L Thompson 
895eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
896480fae85SJeremy L Thompson /// CeedOperator Backend API
897480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
898480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend
899480fae85SJeremy L Thompson /// @{
900480fae85SJeremy L Thompson 
901480fae85SJeremy L Thompson /**
902480fae85SJeremy L Thompson   @brief Create object holding CeedQFunction assembly data for CeedOperator
903480fae85SJeremy L Thompson 
904480fae85SJeremy L Thompson   @param[in]  ceed A Ceed object where the CeedQFunctionAssemblyData will be created
905ea61e9acSJeremy L Thompson   @param[out] data Address of the variable where the newly created CeedQFunctionAssemblyData will be stored
906480fae85SJeremy L Thompson 
907480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
908480fae85SJeremy L Thompson 
909480fae85SJeremy L Thompson   @ref Backend
910480fae85SJeremy L Thompson **/
911ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) {
9122b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
913480fae85SJeremy L Thompson   (*data)->ref_count = 1;
914480fae85SJeremy L Thompson   (*data)->ceed      = ceed;
9152b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
916480fae85SJeremy L Thompson 
917480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
918480fae85SJeremy L Thompson }
919480fae85SJeremy L Thompson 
920480fae85SJeremy L Thompson /**
921480fae85SJeremy L Thompson   @brief Increment the reference counter for a CeedQFunctionAssemblyData
922480fae85SJeremy L Thompson 
923ea61e9acSJeremy L Thompson   @param[in,out] data CeedQFunctionAssemblyData to increment the reference counter
924480fae85SJeremy L Thompson 
925480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
926480fae85SJeremy L Thompson 
927480fae85SJeremy L Thompson   @ref Backend
928480fae85SJeremy L Thompson **/
929480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) {
930480fae85SJeremy L Thompson   data->ref_count++;
931480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
932480fae85SJeremy L Thompson }
933480fae85SJeremy L Thompson 
934480fae85SJeremy L Thompson /**
935beecbf24SJeremy L Thompson   @brief Set re-use of CeedQFunctionAssemblyData
9368b919e6bSJeremy L Thompson 
937ea61e9acSJeremy L Thompson   @param[in,out] data       CeedQFunctionAssemblyData to mark for reuse
938ea61e9acSJeremy L Thompson   @param[in]     reuse_data Boolean flag indicating data re-use
9398b919e6bSJeremy L Thompson 
9408b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9418b919e6bSJeremy L Thompson 
9428b919e6bSJeremy L Thompson   @ref Backend
9438b919e6bSJeremy L Thompson **/
9442b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) {
945beecbf24SJeremy L Thompson   data->reuse_data        = reuse_data;
946beecbf24SJeremy L Thompson   data->needs_data_update = true;
947beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
948beecbf24SJeremy L Thompson }
949beecbf24SJeremy L Thompson 
950beecbf24SJeremy L Thompson /**
951beecbf24SJeremy L Thompson   @brief Mark QFunctionAssemblyData as stale
952beecbf24SJeremy L Thompson 
953ea61e9acSJeremy L Thompson   @param[in,out] data              CeedQFunctionAssemblyData to mark as stale
954ea61e9acSJeremy L Thompson   @param[in]     needs_data_update Boolean flag indicating if update is needed or completed
955beecbf24SJeremy L Thompson 
956beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
957beecbf24SJeremy L Thompson 
958beecbf24SJeremy L Thompson   @ref Backend
959beecbf24SJeremy L Thompson **/
9602b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) {
961beecbf24SJeremy L Thompson   data->needs_data_update = needs_data_update;
9628b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
9638b919e6bSJeremy L Thompson }
9648b919e6bSJeremy L Thompson 
9658b919e6bSJeremy L Thompson /**
9668b919e6bSJeremy L Thompson   @brief Determine if QFunctionAssemblyData needs update
9678b919e6bSJeremy L Thompson 
9688b919e6bSJeremy L Thompson   @param[in]  data             CeedQFunctionAssemblyData to mark as stale
9698b919e6bSJeremy L Thompson   @param[out] is_update_needed Boolean flag indicating if re-assembly is required
9708b919e6bSJeremy L Thompson 
9718b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9728b919e6bSJeremy L Thompson 
9738b919e6bSJeremy L Thompson   @ref Backend
9748b919e6bSJeremy L Thompson **/
9752b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) {
976beecbf24SJeremy L Thompson   *is_update_needed = !data->reuse_data || data->needs_data_update;
9778b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
9788b919e6bSJeremy L Thompson }
9798b919e6bSJeremy L Thompson 
9808b919e6bSJeremy L Thompson /**
981ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedQFunctionAssemblyData.
9824385fb7fSSebastian Grimberg 
983ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedCeedQFunctionAssemblyDataDestroy()`.
984512bb800SJeremy L Thompson 
985512bb800SJeremy L Thompson   Note: If the value of `data_copy` passed to this function is non-NULL, then it is assumed that `*data_copy` is a pointer to a
986512bb800SJeremy L Thompson         CeedQFunctionAssemblyData. This CeedQFunctionAssemblyData will be destroyed if `data_copy` is the only reference to this
987512bb800SJeremy L Thompson         CeedQFunctionAssemblyData.
988480fae85SJeremy L Thompson 
989ea61e9acSJeremy L Thompson   @param[in]     data      CeedQFunctionAssemblyData to copy reference to
990ea61e9acSJeremy L Thompson   @param[in,out] data_copy Variable to store copied reference
991480fae85SJeremy L Thompson 
992480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
993480fae85SJeremy L Thompson 
994480fae85SJeremy L Thompson   @ref Backend
995480fae85SJeremy L Thompson **/
9962b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) {
9972b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReference(data));
9982b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy));
999480fae85SJeremy L Thompson   *data_copy = data;
1000480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1001480fae85SJeremy L Thompson }
1002480fae85SJeremy L Thompson 
1003480fae85SJeremy L Thompson /**
1004480fae85SJeremy L Thompson   @brief Get setup status for internal objects for CeedQFunctionAssemblyData
1005480fae85SJeremy L Thompson 
1006ea61e9acSJeremy L Thompson   @param[in]  data     CeedQFunctionAssemblyData to retrieve status
1007480fae85SJeremy L Thompson   @param[out] is_setup Boolean flag for setup status
1008480fae85SJeremy L Thompson 
1009480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1010480fae85SJeremy L Thompson 
1011480fae85SJeremy L Thompson   @ref Backend
1012480fae85SJeremy L Thompson **/
10132b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) {
1014480fae85SJeremy L Thompson   *is_setup = data->is_setup;
1015480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1016480fae85SJeremy L Thompson }
1017480fae85SJeremy L Thompson 
1018480fae85SJeremy L Thompson /**
1019480fae85SJeremy L Thompson   @brief Set internal objects for CeedQFunctionAssemblyData
1020480fae85SJeremy L Thompson 
1021ea61e9acSJeremy L Thompson   @param[in,out] data CeedQFunctionAssemblyData to set objects
1022480fae85SJeremy L Thompson   @param[in]     vec  CeedVector to store assembled CeedQFunction at quadrature points
1023480fae85SJeremy L Thompson   @param[in]     rstr CeedElemRestriction for CeedVector containing assembled CeedQFunction
1024480fae85SJeremy L Thompson 
1025480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1026480fae85SJeremy L Thompson 
1027480fae85SJeremy L Thompson   @ref Backend
1028480fae85SJeremy L Thompson **/
10292b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) {
10302b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(vec, &data->vec));
10312b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr));
1032480fae85SJeremy L Thompson 
1033480fae85SJeremy L Thompson   data->is_setup = true;
1034480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1035480fae85SJeremy L Thompson }
1036480fae85SJeremy L Thompson 
10372b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) {
10386574a04fSJeremy L Thompson   CeedCheck(data->is_setup, data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first.");
1039480fae85SJeremy L Thompson 
10402b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(data->vec, vec));
10412b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr));
1042480fae85SJeremy L Thompson 
1043480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1044480fae85SJeremy L Thompson }
1045480fae85SJeremy L Thompson 
1046480fae85SJeremy L Thompson /**
1047480fae85SJeremy L Thompson   @brief Destroy CeedQFunctionAssemblyData
1048480fae85SJeremy L Thompson 
1049ea61e9acSJeremy L Thompson   @param[in,out] data  CeedQFunctionAssemblyData to destroy
1050480fae85SJeremy L Thompson 
1051480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1052480fae85SJeremy L Thompson 
1053480fae85SJeremy L Thompson   @ref Backend
1054480fae85SJeremy L Thompson **/
1055480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) {
1056ad6481ceSJeremy L Thompson   if (!*data || --(*data)->ref_count > 0) {
1057ad6481ceSJeremy L Thompson     *data = NULL;
1058ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1059ad6481ceSJeremy L Thompson   }
10602b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
10612b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*data)->vec));
10622b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr));
1063480fae85SJeremy L Thompson 
10642b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1065480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1066480fae85SJeremy L Thompson }
1067480fae85SJeremy L Thompson 
1068ed9e99e6SJeremy L Thompson /**
1069ed9e99e6SJeremy L Thompson   @brief Get CeedOperatorAssemblyData
1070ed9e99e6SJeremy L Thompson 
1071ed9e99e6SJeremy L Thompson   @param[in]  op   CeedOperator to assemble
1072ed9e99e6SJeremy L Thompson   @param[out] data CeedQFunctionAssemblyData
1073ed9e99e6SJeremy L Thompson 
1074ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1075ed9e99e6SJeremy L Thompson 
1076ed9e99e6SJeremy L Thompson   @ref Backend
1077ed9e99e6SJeremy L Thompson **/
10782b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) {
1079ed9e99e6SJeremy L Thompson   if (!op->op_assembled) {
1080ed9e99e6SJeremy L Thompson     CeedOperatorAssemblyData data;
1081ed9e99e6SJeremy L Thompson 
10822b730f8bSJeremy L Thompson     CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data));
1083ed9e99e6SJeremy L Thompson     op->op_assembled = data;
1084ed9e99e6SJeremy L Thompson   }
1085ed9e99e6SJeremy L Thompson   *data = op->op_assembled;
1086ed9e99e6SJeremy L Thompson 
1087ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1088ed9e99e6SJeremy L Thompson }
1089ed9e99e6SJeremy L Thompson 
1090ed9e99e6SJeremy L Thompson /**
1091ba746a46SJeremy L Thompson   @brief Create object holding CeedOperator assembly data.
1092ba746a46SJeremy L Thompson 
1093ba746a46SJeremy L Thompson   The CeedOperatorAssemblyData holds an array with references to every active CeedBasis used in the CeedOperator.
1094ba746a46SJeremy L Thompson   An array with references to the corresponding active CeedElemRestrictions is also stored.
1095ba746a46SJeremy L Thompson   For each active CeedBasis, the CeedOperatorAssemblyData holds an array of all input and output CeedEvalModes for this CeedBasis.
1096ba746a46SJeremy L Thompson   The CeedOperatorAssemblyData holds an array of offsets for indexing into the assembled CeedQFunction arrays to the row representing each
1097ba746a46SJeremy L Thompson CeedEvalMode.
1098ba746a46SJeremy L Thompson   The number of input columns across all active bases for the assembled CeedQFunction is also stored.
1099ba746a46SJeremy L Thompson   Lastly, the CeedOperatorAssembly data holds assembled matrices representing the full action of the CeedBasis for all CeedEvalModes.
1100ed9e99e6SJeremy L Thompson 
1101ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperatorAssemblyData will be created
1102ed9e99e6SJeremy L Thompson   @param[in]  op   CeedOperator to be assembled
1103ea61e9acSJeremy L Thompson   @param[out] data Address of the variable where the newly created CeedOperatorAssemblyData will be stored
1104ed9e99e6SJeremy L Thompson 
1105ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1106ed9e99e6SJeremy L Thompson 
1107ed9e99e6SJeremy L Thompson   @ref Backend
1108ed9e99e6SJeremy L Thompson **/
11092b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) {
1110437c7c90SJeremy L Thompson   CeedInt num_active_bases = 0;
1111437c7c90SJeremy L Thompson 
1112437c7c90SJeremy L Thompson   // Allocate
11132b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1114ed9e99e6SJeremy L Thompson   (*data)->ceed = ceed;
11152b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1116ed9e99e6SJeremy L Thompson 
1117ed9e99e6SJeremy L Thompson   // Build OperatorAssembly data
1118ed9e99e6SJeremy L Thompson   CeedQFunction       qf;
1119ed9e99e6SJeremy L Thompson   CeedQFunctionField *qf_fields;
1120ed9e99e6SJeremy L Thompson   CeedOperatorField  *op_fields;
1121ed9e99e6SJeremy L Thompson   CeedInt             num_input_fields;
11222b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
11232b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL));
11242b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1125ed9e99e6SJeremy L Thompson 
1126ed9e99e6SJeremy L Thompson   // Determine active input basis
1127437c7c90SJeremy L Thompson   CeedInt       *num_eval_modes_in = NULL, *num_eval_modes_out = NULL, offset = 0;
1128437c7c90SJeremy L Thompson   CeedEvalMode **eval_modes_in = NULL, **eval_modes_out = NULL;
1129437c7c90SJeremy L Thompson   CeedSize     **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL;
1130ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1131ed9e99e6SJeremy L Thompson     CeedVector vec;
11322b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1133ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
1134437c7c90SJeremy L Thompson       CeedBasis    basis_in = NULL;
1135437c7c90SJeremy L Thompson       CeedEvalMode eval_mode;
1136352a5e7cSSebastian Grimberg       CeedInt      index = -1, dim, num_comp, q_comp;
11372b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
11382b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1139352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetDimension(basis_in, &dim));
1140352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp));
1141352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1142437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < num_active_bases; i++) {
1143437c7c90SJeremy L Thompson         if ((*data)->active_bases[i] == basis_in) index = i;
1144437c7c90SJeremy L Thompson       }
1145437c7c90SJeremy L Thompson       if (index == -1) {
1146437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_in;
1147437c7c90SJeremy L Thompson         index = num_active_bases;
1148437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_bases));
1149437c7c90SJeremy L Thompson         (*data)->active_bases[num_active_bases] = NULL;
1150437c7c90SJeremy L Thompson         CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases[num_active_bases]));
1151437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_elem_rstrs));
1152437c7c90SJeremy L Thompson         (*data)->active_elem_rstrs[num_active_bases] = NULL;
1153437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in));
1154437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs[num_active_bases]));
1155437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_in));
1156437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_out));
1157437c7c90SJeremy L Thompson         num_eval_modes_in[index]  = 0;
1158437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1159437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_in));
1160437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_out));
1161437c7c90SJeremy L Thompson         eval_modes_in[index]  = NULL;
1162437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1163437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_in));
1164437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_out));
1165437c7c90SJeremy L Thompson         eval_mode_offsets_in[index]  = NULL;
1166437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1167437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_in));
1168437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_out));
1169437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index]  = NULL;
1170437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1171437c7c90SJeremy L Thompson         num_active_bases++;
1172437c7c90SJeremy L Thompson       }
1173352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1174352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1175352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_modes_in[index]));
1176352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_mode_offsets_in[index]));
1177352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1178437c7c90SJeremy L Thompson           eval_modes_in[index][num_eval_modes_in[index] + d]        = eval_mode;
1179437c7c90SJeremy L Thompson           eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset;
1180352a5e7cSSebastian Grimberg           offset += num_comp;
1181ed9e99e6SJeremy L Thompson         }
1182352a5e7cSSebastian Grimberg         num_eval_modes_in[index] += q_comp;
1183ed9e99e6SJeremy L Thompson       }
1184ed9e99e6SJeremy L Thompson     }
1185ed9e99e6SJeremy L Thompson   }
1186437c7c90SJeremy L Thompson   (*data)->num_eval_modes_in    = num_eval_modes_in;
1187437c7c90SJeremy L Thompson   (*data)->eval_modes_in        = eval_modes_in;
1188437c7c90SJeremy L Thompson   (*data)->eval_mode_offsets_in = eval_mode_offsets_in;
1189ed9e99e6SJeremy L Thompson 
1190ed9e99e6SJeremy L Thompson   // Determine active output basis
1191ed9e99e6SJeremy L Thompson   CeedInt num_output_fields;
11922b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields));
11932b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1194437c7c90SJeremy L Thompson   offset = 0;
1195ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1196ed9e99e6SJeremy L Thompson     CeedVector vec;
11972b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1198ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
1199437c7c90SJeremy L Thompson       CeedBasis    basis_out = NULL;
1200ed9e99e6SJeremy L Thompson       CeedEvalMode eval_mode;
1201352a5e7cSSebastian Grimberg       CeedInt      index = -1, dim, num_comp, q_comp;
1202437c7c90SJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
12032b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1204352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetDimension(basis_out, &dim));
1205352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_out, &num_comp));
1206352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1207437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < num_active_bases; i++) {
1208437c7c90SJeremy L Thompson         if ((*data)->active_bases[i] == basis_out) index = i;
1209437c7c90SJeremy L Thompson       }
1210437c7c90SJeremy L Thompson       if (index == -1) {
1211437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_out;
1212437c7c90SJeremy L Thompson 
1213437c7c90SJeremy L Thompson         index = num_active_bases;
1214437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_bases));
1215437c7c90SJeremy L Thompson         (*data)->active_bases[num_active_bases] = NULL;
1216437c7c90SJeremy L Thompson         CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases[num_active_bases]));
1217437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_elem_rstrs));
1218437c7c90SJeremy L Thompson         (*data)->active_elem_rstrs[num_active_bases] = NULL;
1219437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out));
1220437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs[num_active_bases]));
1221437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_in));
1222437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_out));
1223437c7c90SJeremy L Thompson         num_eval_modes_in[index]  = 0;
1224437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1225437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_in));
1226437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_out));
1227437c7c90SJeremy L Thompson         eval_modes_in[index]  = NULL;
1228437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1229437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_in));
1230437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_out));
1231437c7c90SJeremy L Thompson         eval_mode_offsets_in[index]  = NULL;
1232437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1233437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_in));
1234437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_out));
1235437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index]  = NULL;
1236437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1237437c7c90SJeremy L Thompson         num_active_bases++;
1238437c7c90SJeremy L Thompson       }
1239352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1240352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1241352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_modes_out[index]));
1242352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_mode_offsets_out[index]));
1243352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1244437c7c90SJeremy L Thompson           eval_modes_out[index][num_eval_modes_out[index] + d]        = eval_mode;
1245437c7c90SJeremy L Thompson           eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset;
1246352a5e7cSSebastian Grimberg           offset += num_comp;
1247ed9e99e6SJeremy L Thompson         }
1248352a5e7cSSebastian Grimberg         num_eval_modes_out[index] += q_comp;
1249ed9e99e6SJeremy L Thompson       }
1250ed9e99e6SJeremy L Thompson     }
1251ed9e99e6SJeremy L Thompson   }
1252437c7c90SJeremy L Thompson   (*data)->num_output_components = offset;
1253437c7c90SJeremy L Thompson   (*data)->num_eval_modes_out    = num_eval_modes_out;
1254437c7c90SJeremy L Thompson   (*data)->eval_modes_out        = eval_modes_out;
1255437c7c90SJeremy L Thompson   (*data)->eval_mode_offsets_out = eval_mode_offsets_out;
1256437c7c90SJeremy L Thompson   (*data)->num_active_bases      = num_active_bases;
1257ed9e99e6SJeremy L Thompson 
1258ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1259ed9e99e6SJeremy L Thompson }
1260ed9e99e6SJeremy L Thompson 
1261ed9e99e6SJeremy L Thompson /**
1262ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedEvalModes for assembly.
1263ba746a46SJeremy L Thompson 
1264ba746a46SJeremy L Thompson   Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1265ed9e99e6SJeremy L Thompson 
1266ed9e99e6SJeremy L Thompson   @param[in]  data                  CeedOperatorAssemblyData
1267ba746a46SJeremy L Thompson   @param[out] num_active_bases      Total number of active bases
1268c5d0f995SJed Brown   @param[out] num_eval_modes_in     Pointer to hold array of numbers of input CeedEvalModes, or NULL.
1269ba746a46SJeremy L Thompson                                       `eval_modes_in[0]` holds an array of eval modes for the first active basis.
1270c5d0f995SJed Brown   @param[out] eval_modes_in         Pointer to hold arrays of input CeedEvalModes, or NULL.
1271ba746a46SJeremy L Thompson   @param[out] eval_mode_offsets_in  Pointer to hold arrays of input offsets at each quadrature point.
1272c5d0f995SJed Brown   @param[out] num_eval_modes_out    Pointer to hold array of numbers of output CeedEvalModes, or NULL
1273c5d0f995SJed Brown   @param[out] eval_modes_out        Pointer to hold arrays of output CeedEvalModes, or NULL.
1274437c7c90SJeremy L Thompson   @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point
1275ba746a46SJeremy L Thompson   @param[out] num_output_components The number of columns in the assembled CeedQFunction matrix for each quadrature point,
1276ba746a46SJeremy L Thompson                                       including contributions of all active bases
1277ed9e99e6SJeremy L Thompson 
1278ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1279ed9e99e6SJeremy L Thompson 
1280c5d0f995SJed Brown 
1281ed9e99e6SJeremy L Thompson   @ref Backend
1282ed9e99e6SJeremy L Thompson **/
1283437c7c90SJeremy L Thompson int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases, CeedInt **num_eval_modes_in,
1284437c7c90SJeremy L Thompson                                          const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt **num_eval_modes_out,
1285437c7c90SJeremy L Thompson                                          const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out, CeedSize *num_output_components) {
1286437c7c90SJeremy L Thompson   if (num_active_bases) *num_active_bases = data->num_active_bases;
1287437c7c90SJeremy L Thompson   if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in;
1288437c7c90SJeremy L Thompson   if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in;
1289437c7c90SJeremy L Thompson   if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in;
1290437c7c90SJeremy L Thompson   if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out;
1291437c7c90SJeremy L Thompson   if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out;
1292437c7c90SJeremy L Thompson   if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out;
1293437c7c90SJeremy L Thompson   if (num_output_components) *num_output_components = data->num_output_components;
1294ed9e99e6SJeremy L Thompson 
1295ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1296ed9e99e6SJeremy L Thompson }
1297ed9e99e6SJeremy L Thompson 
1298ed9e99e6SJeremy L Thompson /**
1299ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedBasis data for assembly.
1300ba746a46SJeremy L Thompson 
1301ba746a46SJeremy L Thompson   Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1302ed9e99e6SJeremy L Thompson 
1303ed9e99e6SJeremy L Thompson   @param[in]  data                CeedOperatorAssemblyData
1304437c7c90SJeremy L Thompson   @param[out] num_active_bases    Number of active bases, or NULL
1305437c7c90SJeremy L Thompson   @param[out] active_bases        Pointer to hold active CeedBasis, or NULL
1306437c7c90SJeremy L Thompson   @param[out] assembled_bases_in  Pointer to hold assembled active input B, or NULL
1307437c7c90SJeremy L Thompson   @param[out] assembled_bases_out Pointer to hold assembled active output B, or NULL
1308ed9e99e6SJeremy L Thompson 
1309ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1310ed9e99e6SJeremy L Thompson 
1311ed9e99e6SJeremy L Thompson   @ref Backend
1312ed9e99e6SJeremy L Thompson **/
1313437c7c90SJeremy L Thompson int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases, CeedBasis **active_bases,
1314437c7c90SJeremy L Thompson                                      const CeedScalar ***assembled_bases_in, const CeedScalar ***assembled_bases_out) {
1315ed9e99e6SJeremy L Thompson   // Assemble B_in, B_out if needed
1316437c7c90SJeremy L Thompson   if (assembled_bases_in && !data->assembled_bases_in[0]) {
1317437c7c90SJeremy L Thompson     CeedInt num_qpts;
1318437c7c90SJeremy L Thompson 
1319437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases[0], &num_qpts));
1320437c7c90SJeremy L Thompson     for (CeedInt b = 0; b < data->num_active_bases; b++) {
1321352a5e7cSSebastian Grimberg       CeedInt     num_nodes;
1322437c7c90SJeremy L Thompson       CeedScalar *B_in = NULL, *identity = NULL;
1323ed9e99e6SJeremy L Thompson       bool        has_eval_none = false;
1324ed9e99e6SJeremy L Thompson 
1325352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumNodes(data->active_bases[b], &num_nodes));
1326352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_in[b], &B_in));
1327ed9e99e6SJeremy L Thompson 
1328437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) {
1329437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE);
1330ed9e99e6SJeremy L Thompson       }
1331ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1332352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1333352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1334352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1335ed9e99e6SJeremy L Thompson         }
1336ed9e99e6SJeremy L Thompson       }
1337ed9e99e6SJeremy L Thompson 
1338ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1339352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1340352a5e7cSSebastian Grimberg           CeedInt      d_in              = 0, q_comp_in;
1341352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE;
1342437c7c90SJeremy L Thompson           for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) {
1343437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_in[b] * q;
1344437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
1345352a5e7cSSebastian Grimberg             CeedOperatorGetBasisPointer(data->active_bases[b], data->eval_modes_in[b][e_in], identity, &B);
1346352a5e7cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases[b], data->eval_modes_in[b][e_in], &q_comp_in));
1347352a5e7cSSebastian Grimberg             if (q_comp_in > 1) {
1348352a5e7cSSebastian Grimberg               if (e_in == 0 || data->eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0;
1349352a5e7cSSebastian Grimberg               else B = &B[(++d_in) * num_qpts * num_nodes];
1350352a5e7cSSebastian Grimberg             }
1351352a5e7cSSebastian Grimberg             eval_mode_in_prev                 = data->eval_modes_in[b][e_in];
1352352a5e7cSSebastian Grimberg             B_in[(qq + e_in) * num_nodes + n] = B[q * num_nodes + n];
1353ed9e99e6SJeremy L Thompson           }
1354ed9e99e6SJeremy L Thompson         }
1355ed9e99e6SJeremy L Thompson       }
1356437c7c90SJeremy L Thompson       if (identity) CeedCall(CeedFree(identity));
1357437c7c90SJeremy L Thompson       data->assembled_bases_in[b] = B_in;
1358437c7c90SJeremy L Thompson     }
1359ed9e99e6SJeremy L Thompson   }
1360ed9e99e6SJeremy L Thompson 
1361437c7c90SJeremy L Thompson   if (assembled_bases_out && !data->assembled_bases_out[0]) {
1362437c7c90SJeremy L Thompson     CeedInt num_qpts;
1363437c7c90SJeremy L Thompson 
1364437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases[0], &num_qpts));
1365437c7c90SJeremy L Thompson     for (CeedInt b = 0; b < data->num_active_bases; b++) {
1366352a5e7cSSebastian Grimberg       CeedInt     num_nodes;
1367ed9e99e6SJeremy L Thompson       bool        has_eval_none = false;
1368437c7c90SJeremy L Thompson       CeedScalar *B_out = NULL, *identity = NULL;
1369ed9e99e6SJeremy L Thompson 
1370352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumNodes(data->active_bases[b], &num_nodes));
1371352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_out[b], &B_out));
1372ed9e99e6SJeremy L Thompson 
1373437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) {
1374437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE);
1375ed9e99e6SJeremy L Thompson       }
1376ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1377352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1378352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1379352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1380ed9e99e6SJeremy L Thompson         }
1381ed9e99e6SJeremy L Thompson       }
1382ed9e99e6SJeremy L Thompson 
1383ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1384352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1385352a5e7cSSebastian Grimberg           CeedInt      d_out              = 0, q_comp_out;
1386352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
1387437c7c90SJeremy L Thompson           for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) {
1388437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_out[b] * q;
1389437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
1390352a5e7cSSebastian Grimberg             CeedOperatorGetBasisPointer(data->active_bases[b], data->eval_modes_out[b][e_out], identity, &B);
1391352a5e7cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases[b], data->eval_modes_out[b][e_out], &q_comp_out));
1392352a5e7cSSebastian Grimberg             if (q_comp_out > 1) {
1393352a5e7cSSebastian Grimberg               if (e_out == 0 || data->eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0;
1394352a5e7cSSebastian Grimberg               else B = &B[(++d_out) * num_qpts * num_nodes];
1395352a5e7cSSebastian Grimberg             }
1396352a5e7cSSebastian Grimberg             eval_mode_out_prev                  = data->eval_modes_out[b][e_out];
1397352a5e7cSSebastian Grimberg             B_out[(qq + e_out) * num_nodes + n] = B[q * num_nodes + n];
1398ed9e99e6SJeremy L Thompson           }
1399ed9e99e6SJeremy L Thompson         }
1400ed9e99e6SJeremy L Thompson       }
1401437c7c90SJeremy L Thompson       if (identity) CeedCall(CeedFree(identity));
1402437c7c90SJeremy L Thompson       data->assembled_bases_out[b] = B_out;
1403437c7c90SJeremy L Thompson     }
1404ed9e99e6SJeremy L Thompson   }
1405ed9e99e6SJeremy L Thompson 
1406437c7c90SJeremy L Thompson   // Pass out assembled data
1407437c7c90SJeremy L Thompson   if (active_bases) *active_bases = data->active_bases;
1408437c7c90SJeremy L Thompson   if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in;
1409437c7c90SJeremy L Thompson   if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out;
1410437c7c90SJeremy L Thompson 
1411437c7c90SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1412437c7c90SJeremy L Thompson }
1413437c7c90SJeremy L Thompson 
1414437c7c90SJeremy L Thompson /**
1415ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedBasis data for assembly.
1416ba746a46SJeremy L Thompson 
1417ba746a46SJeremy L Thompson   Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1418437c7c90SJeremy L Thompson 
1419437c7c90SJeremy L Thompson   @param[in]  data                  CeedOperatorAssemblyData
1420437c7c90SJeremy L Thompson   @param[out] num_active_elem_rstrs Number of active element restrictions, or NULL
1421437c7c90SJeremy L Thompson   @param[out] active_elem_rstrs     Pointer to hold active CeedElemRestrictions, or NULL
1422437c7c90SJeremy L Thompson 
1423437c7c90SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1424437c7c90SJeremy L Thompson 
1425437c7c90SJeremy L Thompson   @ref Backend
1426437c7c90SJeremy L Thompson **/
1427437c7c90SJeremy L Thompson int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs,
1428437c7c90SJeremy L Thompson                                                 CeedElemRestriction **active_elem_rstrs) {
1429437c7c90SJeremy L Thompson   if (num_active_elem_rstrs) *num_active_elem_rstrs = data->num_active_bases;
1430437c7c90SJeremy L Thompson   if (active_elem_rstrs) *active_elem_rstrs = data->active_elem_rstrs;
1431ed9e99e6SJeremy L Thompson 
1432ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1433ed9e99e6SJeremy L Thompson }
1434ed9e99e6SJeremy L Thompson 
1435ed9e99e6SJeremy L Thompson /**
1436ed9e99e6SJeremy L Thompson   @brief Destroy CeedOperatorAssemblyData
1437ed9e99e6SJeremy L Thompson 
1438ea61e9acSJeremy L Thompson   @param[in,out] data CeedOperatorAssemblyData to destroy
1439ed9e99e6SJeremy L Thompson 
1440ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1441ed9e99e6SJeremy L Thompson 
1442ed9e99e6SJeremy L Thompson   @ref Backend
1443ed9e99e6SJeremy L Thompson **/
1444ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) {
1445ad6481ceSJeremy L Thompson   if (!*data) {
1446ad6481ceSJeremy L Thompson     *data = NULL;
1447ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1448ad6481ceSJeremy L Thompson   }
14492b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
1450437c7c90SJeremy L Thompson   for (CeedInt b = 0; b < (*data)->num_active_bases; b++) {
1451437c7c90SJeremy L Thompson     CeedCall(CeedBasisDestroy(&(*data)->active_bases[b]));
1452437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs[b]));
1453437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_in[b]));
1454437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_out[b]));
1455437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b]));
1456437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b]));
1457437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_in[b]));
1458437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_out[b]));
1459437c7c90SJeremy L Thompson   }
1460437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->active_bases));
1461437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->active_elem_rstrs));
1462437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_in));
1463437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_out));
1464437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_in));
1465437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_out));
1466437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_in));
1467437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_out));
1468437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_in));
1469437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_out));
1470ed9e99e6SJeremy L Thompson 
14712b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1472ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1473ed9e99e6SJeremy L Thompson }
1474ed9e99e6SJeremy L Thompson 
1475480fae85SJeremy L Thompson /// @}
1476480fae85SJeremy L Thompson 
1477480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1478eaf62fffSJeremy L Thompson /// CeedOperator Public API
1479eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1480eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser
1481eaf62fffSJeremy L Thompson /// @{
1482eaf62fffSJeremy L Thompson 
1483eaf62fffSJeremy L Thompson /**
1484eaf62fffSJeremy L Thompson   @brief Assemble a linear CeedQFunction associated with a CeedOperator
1485eaf62fffSJeremy L Thompson 
1486ea61e9acSJeremy L Thompson   This returns a CeedVector containing a matrix at each quadrature point providing the action of the CeedQFunction associated with the CeedOperator.
1487859c15bbSJames Wright   The vector `assembled` is of shape `[num_elements, num_input_fields, num_output_fields, num_quad_points]` and contains column-major matrices
1488859c15bbSJames Wright representing the action of the CeedQFunction for a corresponding quadrature point on an element.
1489859c15bbSJames Wright 
14909fd66db6SSebastian Grimberg   Inputs and outputs are in the order provided by the user when adding CeedOperator fields.
14919fd66db6SSebastian Grimberg   For example, a CeedQFunction with inputs 'u' and 'gradu' and outputs 'gradv' and 'v', provided in that order, would result in an assembled QFunction
14929fd66db6SSebastian Grimberg that consists of (1 + dim) x (dim + 1) matrices at each quadrature point acting on the input [u, du_0, du_1] and producing the output [dv_0, dv_1, v].
1493eaf62fffSJeremy L Thompson 
1494ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1495f04ea552SJeremy L Thompson 
1496ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1497ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points
1498ea61e9acSJeremy L Thompson   @param[out] rstr      CeedElemRestriction for CeedVector containing assembled CeedQFunction
1499ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1500eaf62fffSJeremy L Thompson 
1501eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1502eaf62fffSJeremy L Thompson 
1503eaf62fffSJeremy L Thompson   @ref User
1504eaf62fffSJeremy L Thompson **/
15052b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
15062b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1507eaf62fffSJeremy L Thompson 
1508eaf62fffSJeremy L Thompson   if (op->LinearAssembleQFunction) {
1509d04bbc78SJeremy L Thompson     // Backend version
15102b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request));
1511eaf62fffSJeremy L Thompson   } else {
1512d04bbc78SJeremy L Thompson     // Operator fallback
1513d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1514d04bbc78SJeremy L Thompson 
15152b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
15166574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request));
15176574a04fSJeremy L Thompson     else return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction");
151870a7ffb3SJeremy L Thompson   }
1519eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1520eaf62fffSJeremy L Thompson }
152170a7ffb3SJeremy L Thompson 
152270a7ffb3SJeremy L Thompson /**
1523ea61e9acSJeremy L Thompson   @brief Assemble CeedQFunction and store result internally.
15244385fb7fSSebastian Grimberg 
1525ea61e9acSJeremy L Thompson   Return copied references of stored data to the caller.
1526ea61e9acSJeremy L Thompson   Caller is responsible for ownership and destruction of the copied references.
1527ea61e9acSJeremy L Thompson   See also @ref CeedOperatorLinearAssembleQFunction
152870a7ffb3SJeremy L Thompson 
1529c5f45aeaSJeremy L Thompson   Note: If the value of `assembled` or `rstr` passed to this function are non-NULL, then it is assumed that they hold valid pointers.
1530c5f45aeaSJeremy L Thompson         These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object.
1531c5f45aeaSJeremy L Thompson 
1532ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1533ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points
1534ea61e9acSJeremy L Thompson   @param[out] rstr      CeedElemRestriction for CeedVector containing assembledCeedQFunction
1535ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
153670a7ffb3SJeremy L Thompson 
153770a7ffb3SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
153870a7ffb3SJeremy L Thompson 
153970a7ffb3SJeremy L Thompson   @ref User
154070a7ffb3SJeremy L Thompson **/
15412b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
15422b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
154370a7ffb3SJeremy L Thompson 
154470a7ffb3SJeremy L Thompson   if (op->LinearAssembleQFunctionUpdate) {
1545d04bbc78SJeremy L Thompson     // Backend version
1546480fae85SJeremy L Thompson     bool                qf_assembled_is_setup;
15472efa2d85SJeremy L Thompson     CeedVector          assembled_vec  = NULL;
15482efa2d85SJeremy L Thompson     CeedElemRestriction assembled_rstr = NULL;
1549480fae85SJeremy L Thompson 
15502b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataIsSetup(op->qf_assembled, &qf_assembled_is_setup));
1551480fae85SJeremy L Thompson     if (qf_assembled_is_setup) {
1552d04bbc78SJeremy L Thompson       bool update_needed;
1553d04bbc78SJeremy L Thompson 
15542b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataGetObjects(op->qf_assembled, &assembled_vec, &assembled_rstr));
15552b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(op->qf_assembled, &update_needed));
1556c5f45aeaSJeremy L Thompson       if (update_needed) CeedCall(op->LinearAssembleQFunctionUpdate(op, assembled_vec, assembled_rstr, request));
155770a7ffb3SJeremy L Thompson     } else {
15582b730f8bSJeremy L Thompson       CeedCall(op->LinearAssembleQFunction(op, &assembled_vec, &assembled_rstr, request));
15592b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataSetObjects(op->qf_assembled, assembled_vec, assembled_rstr));
156070a7ffb3SJeremy L Thompson     }
15612b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, false));
15622efa2d85SJeremy L Thompson 
1563d04bbc78SJeremy L Thompson     // Copy reference from internally held copy
15642b730f8bSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled));
15652b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr));
1566c5f45aeaSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled_vec));
15672b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&assembled_rstr));
156870a7ffb3SJeremy L Thompson   } else {
1569d04bbc78SJeremy L Thompson     // Operator fallback
1570d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1571d04bbc78SJeremy L Thompson 
15722b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
15736574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request));
15746574a04fSJeremy L Thompson     else return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate");
157570a7ffb3SJeremy L Thompson   }
157670a7ffb3SJeremy L Thompson 
157770a7ffb3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1578eaf62fffSJeremy L Thompson }
1579eaf62fffSJeremy L Thompson 
1580eaf62fffSJeremy L Thompson /**
1581eaf62fffSJeremy L Thompson   @brief Assemble the diagonal of a square linear CeedOperator
1582eaf62fffSJeremy L Thompson 
1583eaf62fffSJeremy L Thompson   This overwrites a CeedVector with the diagonal of a linear CeedOperator.
1584eaf62fffSJeremy L Thompson 
1585ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1586eaf62fffSJeremy L Thompson 
1587ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1588f04ea552SJeremy L Thompson 
1589ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1590eaf62fffSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator diagonal
1591ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1592eaf62fffSJeremy L Thompson 
1593eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1594eaf62fffSJeremy L Thompson 
1595eaf62fffSJeremy L Thompson   @ref User
1596eaf62fffSJeremy L Thompson **/
15972b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1598f3d47e36SJeremy L Thompson   bool is_composite;
15992b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1600f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1601eaf62fffSJeremy L Thompson 
1602c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
16032b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
16046574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1605c9366a6bSJeremy L Thompson 
1606f3d47e36SJeremy L Thompson   // Early exit for empty operator
1607f3d47e36SJeremy L Thompson   if (!is_composite) {
1608f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1609f3d47e36SJeremy L Thompson 
1610f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1611f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1612f3d47e36SJeremy L Thompson   }
1613f3d47e36SJeremy L Thompson 
1614eaf62fffSJeremy L Thompson   if (op->LinearAssembleDiagonal) {
1615d04bbc78SJeremy L Thompson     // Backend version
16162b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleDiagonal(op, assembled, request));
1617eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1618eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddDiagonal) {
1619d04bbc78SJeremy L Thompson     // Backend version with zeroing first
16202b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
16212b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1622eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1623eaf62fffSJeremy L Thompson   } else {
1624d04bbc78SJeremy L Thompson     // Operator fallback
1625d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1626d04bbc78SJeremy L Thompson 
16272b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1628d04bbc78SJeremy L Thompson     if (op_fallback) {
16292b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request));
1630eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1631eaf62fffSJeremy L Thompson     }
1632eaf62fffSJeremy L Thompson   }
1633eaf62fffSJeremy L Thompson   // Default interface implementation
16342b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
16352b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request));
1636d04bbc78SJeremy L Thompson 
1637eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1638eaf62fffSJeremy L Thompson }
1639eaf62fffSJeremy L Thompson 
1640eaf62fffSJeremy L Thompson /**
1641eaf62fffSJeremy L Thompson   @brief Assemble the diagonal of a square linear CeedOperator
1642eaf62fffSJeremy L Thompson 
1643eaf62fffSJeremy L Thompson   This sums into a CeedVector the diagonal of a linear CeedOperator.
1644eaf62fffSJeremy L Thompson 
1645ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1646eaf62fffSJeremy L Thompson 
1647ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1648f04ea552SJeremy L Thompson 
1649ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1650eaf62fffSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator diagonal
1651ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1652eaf62fffSJeremy L Thompson 
1653eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1654eaf62fffSJeremy L Thompson 
1655eaf62fffSJeremy L Thompson   @ref User
1656eaf62fffSJeremy L Thompson **/
16572b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1658f3d47e36SJeremy L Thompson   bool is_composite;
16592b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1660f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1661eaf62fffSJeremy L Thompson 
1662c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
16632b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
16646574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
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->LinearAssembleAddDiagonal) {
1675d04bbc78SJeremy L Thompson     // Backend version
16762b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1677eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1678eaf62fffSJeremy L Thompson   } else {
1679d04bbc78SJeremy L Thompson     // Operator fallback
1680d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1681d04bbc78SJeremy L Thompson 
16822b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1683d04bbc78SJeremy L Thompson     if (op_fallback) {
16842b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
1685eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1686eaf62fffSJeremy L Thompson     }
1687eaf62fffSJeremy L Thompson   }
1688eaf62fffSJeremy L Thompson   // Default interface implementation
1689eaf62fffSJeremy L Thompson   if (is_composite) {
16902b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
1691eaf62fffSJeremy L Thompson   } else {
16922b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, false, assembled));
1693eaf62fffSJeremy L Thompson   }
1694d04bbc78SJeremy L Thompson 
1695d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1696eaf62fffSJeremy L Thompson }
1697eaf62fffSJeremy L Thompson 
1698eaf62fffSJeremy L Thompson /**
1699eaf62fffSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear CeedOperator
1700eaf62fffSJeremy L Thompson 
1701ea61e9acSJeremy L Thompson   This overwrites a CeedVector with the point block diagonal of a linear CeedOperator.
1702eaf62fffSJeremy L Thompson 
1703ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1704eaf62fffSJeremy L Thompson 
1705ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1706f04ea552SJeremy L Thompson 
1707ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1708ea61e9acSJeremy 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
1709ea61e9acSJeremy 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,
1710ea61e9acSJeremy L Thompson component in].
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 CeedOperatorLinearAssemblePointBlockDiagonal(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));
17246574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1725c9366a6bSJeremy L Thompson 
1726f3d47e36SJeremy L Thompson   // Early exit for empty operator
1727f3d47e36SJeremy L Thompson   if (!is_composite) {
1728f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1729f3d47e36SJeremy L Thompson 
1730f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1731f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1732f3d47e36SJeremy L Thompson   }
1733f3d47e36SJeremy L Thompson 
1734eaf62fffSJeremy L Thompson   if (op->LinearAssemblePointBlockDiagonal) {
1735d04bbc78SJeremy L Thompson     // Backend version
17362b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request));
1737eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1738eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddPointBlockDiagonal) {
1739d04bbc78SJeremy L Thompson     // Backend version with zeroing first
17402b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
17412b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
1742eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1743eaf62fffSJeremy L Thompson   } else {
1744d04bbc78SJeremy L Thompson     // Operator fallback
1745d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1746d04bbc78SJeremy L Thompson 
17472b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1748d04bbc78SJeremy L Thompson     if (op_fallback) {
17492b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request));
1750eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1751eaf62fffSJeremy L Thompson     }
1752eaf62fffSJeremy L Thompson   }
1753eaf62fffSJeremy L Thompson   // Default interface implementation
17542b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
17552b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
1756d04bbc78SJeremy L Thompson 
1757eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1758eaf62fffSJeremy L Thompson }
1759eaf62fffSJeremy L Thompson 
1760eaf62fffSJeremy L Thompson /**
1761eaf62fffSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear CeedOperator
1762eaf62fffSJeremy L Thompson 
1763ea61e9acSJeremy L Thompson   This sums into a CeedVector with the point block diagonal of a linear CeedOperator.
1764eaf62fffSJeremy L Thompson 
1765ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1766eaf62fffSJeremy L Thompson 
1767ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1768f04ea552SJeremy L Thompson 
1769ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1770ea61e9acSJeremy 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
1771ea61e9acSJeremy 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,
1772ea61e9acSJeremy L Thompson component in].
1773ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1774eaf62fffSJeremy L Thompson 
1775eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1776eaf62fffSJeremy L Thompson 
1777eaf62fffSJeremy L Thompson   @ref User
1778eaf62fffSJeremy L Thompson **/
17792b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1780f3d47e36SJeremy L Thompson   bool is_composite;
17812b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1782f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1783eaf62fffSJeremy L Thompson 
1784c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
17852b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
17866574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1787c9366a6bSJeremy L Thompson 
1788f3d47e36SJeremy L Thompson   // Early exit for empty operator
1789f3d47e36SJeremy L Thompson   if (!is_composite) {
1790f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1791f3d47e36SJeremy L Thompson 
1792f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1793f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1794f3d47e36SJeremy L Thompson   }
1795f3d47e36SJeremy L Thompson 
1796eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddPointBlockDiagonal) {
1797d04bbc78SJeremy L Thompson     // Backend version
17982b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request));
1799eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1800eaf62fffSJeremy L Thompson   } else {
1801d04bbc78SJeremy L Thompson     // Operator fallback
1802d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1803d04bbc78SJeremy L Thompson 
18042b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1805d04bbc78SJeremy L Thompson     if (op_fallback) {
18062b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request));
1807eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1808eaf62fffSJeremy L Thompson     }
1809eaf62fffSJeremy L Thompson   }
1810ea61e9acSJeremy L Thompson   // Default interface implementation
1811eaf62fffSJeremy L Thompson   if (is_composite) {
18122b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
1813eaf62fffSJeremy L Thompson   } else {
18142b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, true, assembled));
1815eaf62fffSJeremy L Thompson   }
1816d04bbc78SJeremy L Thompson 
1817d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1818eaf62fffSJeremy L Thompson }
1819eaf62fffSJeremy L Thompson 
1820eaf62fffSJeremy L Thompson /**
1821eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero pattern of a linear operator.
1822eaf62fffSJeremy L Thompson 
1823ea61e9acSJeremy L Thompson    Expected to be used in conjunction with CeedOperatorLinearAssemble().
1824eaf62fffSJeremy L Thompson 
1825ea61e9acSJeremy 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
18269fd66db6SSebastian Grimberg matrix in entry (i, j).
18279fd66db6SSebastian Grimberg   Note that the (i, j) pairs are not unique and may repeat.
18289fd66db6SSebastian Grimberg   This function returns the number of entries and their (i, j) locations, while CeedOperatorLinearAssemble() provides the values in the same ordering.
1829eaf62fffSJeremy L Thompson 
1830eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
1831eaf62fffSJeremy L Thompson 
1832ea61e9acSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1833f04ea552SJeremy L Thompson 
1834eaf62fffSJeremy L Thompson    @param[in]  op          CeedOperator to assemble
1835eaf62fffSJeremy L Thompson    @param[out] num_entries Number of entries in coordinate nonzero pattern
1836eaf62fffSJeremy L Thompson    @param[out] rows        Row number for each entry
1837eaf62fffSJeremy L Thompson    @param[out] cols        Column number for each entry
1838eaf62fffSJeremy L Thompson 
1839eaf62fffSJeremy L Thompson    @ref User
1840eaf62fffSJeremy L Thompson **/
18412b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
1842eaf62fffSJeremy L Thompson   CeedInt       num_suboperators, single_entries;
1843eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
1844eaf62fffSJeremy L Thompson   bool          is_composite;
18452b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1846f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1847eaf62fffSJeremy L Thompson 
1848eaf62fffSJeremy L Thompson   if (op->LinearAssembleSymbolic) {
1849d04bbc78SJeremy L Thompson     // Backend version
18502b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols));
1851eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1852eaf62fffSJeremy L Thompson   } else {
1853d04bbc78SJeremy L Thompson     // Operator fallback
1854d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1855d04bbc78SJeremy L Thompson 
18562b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1857d04bbc78SJeremy L Thompson     if (op_fallback) {
18582b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols));
1859eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1860eaf62fffSJeremy L Thompson     }
1861eaf62fffSJeremy L Thompson   }
1862eaf62fffSJeremy L Thompson 
1863eaf62fffSJeremy L Thompson   // Default interface implementation
1864eaf62fffSJeremy L Thompson 
1865eaf62fffSJeremy L Thompson   // count entries and allocate rows, cols arrays
1866eaf62fffSJeremy L Thompson   *num_entries = 0;
1867eaf62fffSJeremy L Thompson   if (is_composite) {
1868c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1869c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
187092ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
18712b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
1872eaf62fffSJeremy L Thompson       *num_entries += single_entries;
1873eaf62fffSJeremy L Thompson     }
1874eaf62fffSJeremy L Thompson   } else {
18752b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries));
1876eaf62fffSJeremy L Thompson     *num_entries += single_entries;
1877eaf62fffSJeremy L Thompson   }
18782b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, rows));
18792b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, cols));
1880eaf62fffSJeremy L Thompson 
1881eaf62fffSJeremy L Thompson   // assemble nonzero locations
1882eaf62fffSJeremy L Thompson   CeedInt offset = 0;
1883eaf62fffSJeremy L Thompson   if (is_composite) {
1884c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1885c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
188692ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
18872b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols));
18882b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
1889eaf62fffSJeremy L Thompson       offset += single_entries;
1890eaf62fffSJeremy L Thompson     }
1891eaf62fffSJeremy L Thompson   } else {
18922b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols));
1893eaf62fffSJeremy L Thompson   }
1894eaf62fffSJeremy L Thompson 
1895eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1896eaf62fffSJeremy L Thompson }
1897eaf62fffSJeremy L Thompson 
1898eaf62fffSJeremy L Thompson /**
1899eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero entries of a linear operator.
1900eaf62fffSJeremy L Thompson 
1901ea61e9acSJeremy L Thompson    Expected to be used in conjunction with CeedOperatorLinearAssembleSymbolic().
1902eaf62fffSJeremy L Thompson 
1903ea61e9acSJeremy 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
19049fd66db6SSebastian Grimberg matrix in entry (i, j).
19059fd66db6SSebastian Grimberg   Note that the (i, j) pairs are not unique and may repeat.
19069fd66db6SSebastian Grimberg   This function returns the values of the nonzero entries to be added, their (i, j) locations are provided by CeedOperatorLinearAssembleSymbolic()
1907eaf62fffSJeremy L Thompson 
1908eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
1909eaf62fffSJeremy L Thompson 
1910ea61e9acSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1911f04ea552SJeremy L Thompson 
1912eaf62fffSJeremy L Thompson    @param[in]  op     CeedOperator to assemble
1913eaf62fffSJeremy L Thompson    @param[out] values Values to assemble into matrix
1914eaf62fffSJeremy L Thompson 
1915eaf62fffSJeremy L Thompson    @ref User
1916eaf62fffSJeremy L Thompson **/
1917eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) {
1918eaf62fffSJeremy L Thompson   CeedInt       num_suboperators, single_entries = 0;
1919eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
1920f3d47e36SJeremy L Thompson   bool          is_composite;
19212b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1922f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1923f3d47e36SJeremy L Thompson 
1924f3d47e36SJeremy L Thompson   // Early exit for empty operator
1925f3d47e36SJeremy L Thompson   if (!is_composite) {
1926f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1927f3d47e36SJeremy L Thompson 
1928f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1929f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1930f3d47e36SJeremy L Thompson   }
1931eaf62fffSJeremy L Thompson 
1932eaf62fffSJeremy L Thompson   if (op->LinearAssemble) {
1933d04bbc78SJeremy L Thompson     // Backend version
19342b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemble(op, values));
1935eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1936eaf62fffSJeremy L Thompson   } else {
1937d04bbc78SJeremy L Thompson     // Operator fallback
1938d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1939d04bbc78SJeremy L Thompson 
19402b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1941d04bbc78SJeremy L Thompson     if (op_fallback) {
19422b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemble(op_fallback, values));
1943eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1944eaf62fffSJeremy L Thompson     }
1945eaf62fffSJeremy L Thompson   }
1946eaf62fffSJeremy L Thompson 
1947eaf62fffSJeremy L Thompson   // Default interface implementation
1948eaf62fffSJeremy L Thompson   CeedInt offset = 0;
194928ec399dSJeremy L Thompson   CeedCall(CeedVectorSetValue(values, 0.0));
1950eaf62fffSJeremy L Thompson   if (is_composite) {
1951c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1952c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1953cefa2673SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; k++) {
19542b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values));
19552b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
1956eaf62fffSJeremy L Thompson       offset += single_entries;
1957eaf62fffSJeremy L Thompson     }
1958eaf62fffSJeremy L Thompson   } else {
19592b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemble(op, offset, values));
1960eaf62fffSJeremy L Thompson   }
1961eaf62fffSJeremy L Thompson 
1962eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1963eaf62fffSJeremy L Thompson }
1964eaf62fffSJeremy L Thompson 
1965eaf62fffSJeremy L Thompson /**
196675f0d5a4SJeremy L Thompson   @brief Get the multiplicity of nodes across suboperators in a composite CeedOperator
196775f0d5a4SJeremy L Thompson 
196875f0d5a4SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
196975f0d5a4SJeremy L Thompson 
197075f0d5a4SJeremy L Thompson   @param[in]  op               Composite CeedOperator
197175f0d5a4SJeremy L Thompson   @param[in]  num_skip_indices Number of suboperators to skip
197275f0d5a4SJeremy L Thompson   @param[in]  skip_indices     Array of indices of suboperators to skip
197375f0d5a4SJeremy L Thompson   @param[out] mult             Vector to store multiplicity (of size l_size)
197475f0d5a4SJeremy L Thompson 
197575f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
197675f0d5a4SJeremy L Thompson 
197775f0d5a4SJeremy L Thompson   @ref User
197875f0d5a4SJeremy L Thompson **/
197975f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) {
198075f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
198175f0d5a4SJeremy L Thompson 
198275f0d5a4SJeremy L Thompson   Ceed                ceed;
1983b275c451SJeremy L Thompson   CeedInt             num_suboperators;
198475f0d5a4SJeremy L Thompson   CeedSize            l_vec_len;
198575f0d5a4SJeremy L Thompson   CeedScalar         *mult_array;
198675f0d5a4SJeremy L Thompson   CeedVector          ones_l_vec;
1987437c7c90SJeremy L Thompson   CeedElemRestriction elem_rstr;
1988b275c451SJeremy L Thompson   CeedOperator       *sub_operators;
198975f0d5a4SJeremy L Thompson 
199075f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
199175f0d5a4SJeremy L Thompson 
199275f0d5a4SJeremy L Thompson   // Zero mult vector
199375f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
199475f0d5a4SJeremy L Thompson 
199575f0d5a4SJeremy L Thompson   // Get suboperators
1996b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1997b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1998b275c451SJeremy L Thompson   if (num_suboperators == 0) return CEED_ERROR_SUCCESS;
199975f0d5a4SJeremy L Thompson 
200075f0d5a4SJeremy L Thompson   // Work vector
200175f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetLength(mult, &l_vec_len));
200275f0d5a4SJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec));
200375f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(ones_l_vec, 1.0));
200475f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array));
200575f0d5a4SJeremy L Thompson 
200675f0d5a4SJeremy L Thompson   // Compute multiplicity across suboperators
2007b275c451SJeremy L Thompson   for (CeedInt i = 0; i < num_suboperators; i++) {
200875f0d5a4SJeremy L Thompson     const CeedScalar *sub_mult_array;
200975f0d5a4SJeremy L Thompson     CeedVector        sub_mult_l_vec, ones_e_vec;
201075f0d5a4SJeremy L Thompson 
201175f0d5a4SJeremy L Thompson     // -- Check for suboperator to skip
201275f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < num_skip_indices; j++) {
201375f0d5a4SJeremy L Thompson       if (skip_indices[j] == i) continue;
201475f0d5a4SJeremy L Thompson     }
201575f0d5a4SJeremy L Thompson 
201675f0d5a4SJeremy L Thompson     // -- Sub operator multiplicity
2017437c7c90SJeremy L Thompson     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr));
2018437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(elem_rstr, &sub_mult_l_vec, &ones_e_vec));
201975f0d5a4SJeremy L Thompson     CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0));
2020437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE));
2021437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE));
202275f0d5a4SJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array));
202375f0d5a4SJeremy L Thompson     // ---- Flag every node present in the current suboperator
202475f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < l_vec_len; j++) {
202575f0d5a4SJeremy L Thompson       if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0;
202675f0d5a4SJeremy L Thompson     }
202775f0d5a4SJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array));
202875f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&sub_mult_l_vec));
202975f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&ones_e_vec));
203075f0d5a4SJeremy L Thompson   }
203175f0d5a4SJeremy L Thompson   CeedCall(CeedVectorRestoreArray(mult, &mult_array));
2032811d0ccfSJeremy L Thompson   CeedCall(CeedVectorDestroy(&ones_l_vec));
203375f0d5a4SJeremy L Thompson 
203475f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
203575f0d5a4SJeremy L Thompson }
203675f0d5a4SJeremy L Thompson 
203775f0d5a4SJeremy L Thompson /**
2038ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator, creating the prolongation basis from the fine and coarse
2039ea61e9acSJeremy L Thompson grid interpolation
2040eaf62fffSJeremy L Thompson 
204158e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2042f04ea552SJeremy L Thompson 
2043eaf62fffSJeremy L Thompson   @param[in]  op_fine      Fine grid operator
204485bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2045eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid restriction
2046eaf62fffSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector basis
2047eaf62fffSJeremy L Thompson   @param[out] op_coarse    Coarse grid operator
204885bb9dcfSJeremy L Thompson   @param[out] op_prolong   Coarse to fine operator, or NULL
204985bb9dcfSJeremy L Thompson   @param[out] op_restrict  Fine to coarse operator, or NULL
2050eaf62fffSJeremy L Thompson 
2051eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2052eaf62fffSJeremy L Thompson 
2053eaf62fffSJeremy L Thompson   @ref User
2054eaf62fffSJeremy L Thompson **/
20552b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
20562b730f8bSJeremy L Thompson                                      CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
20572b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2058eaf62fffSJeremy L Thompson 
205983d6adf3SZach Atkins   // Build prolongation matrix, if required
206083d6adf3SZach Atkins   CeedBasis basis_c_to_f = NULL;
206183d6adf3SZach Atkins   if (op_prolong || op_restrict) {
206283d6adf3SZach Atkins     CeedBasis basis_fine;
20632b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
20642b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f));
206583d6adf3SZach Atkins   }
2066eaf62fffSJeremy L Thompson 
2067f113e5dcSJeremy L Thompson   // Core code
20682b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2069f113e5dcSJeremy L Thompson 
2070eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2071eaf62fffSJeremy L Thompson }
2072eaf62fffSJeremy L Thompson 
2073eaf62fffSJeremy L Thompson /**
2074ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a tensor basis for the active basis
2075eaf62fffSJeremy L Thompson 
207658e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2077f04ea552SJeremy L Thompson 
2078eaf62fffSJeremy L Thompson   @param[in]  op_fine       Fine grid operator
207985bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2080eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid restriction
2081eaf62fffSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector basis
208285bb9dcfSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
2083eaf62fffSJeremy L Thompson   @param[out] op_coarse     Coarse grid operator
208485bb9dcfSJeremy L Thompson   @param[out] op_prolong    Coarse to fine operator, or NULL
208585bb9dcfSJeremy L Thompson   @param[out] op_restrict   Fine to coarse operator, or NULL
2086eaf62fffSJeremy L Thompson 
2087eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2088eaf62fffSJeremy L Thompson 
2089eaf62fffSJeremy L Thompson   @ref User
2090eaf62fffSJeremy L Thompson **/
20912b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
20922b730f8bSJeremy L Thompson                                              const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
20932b730f8bSJeremy L Thompson                                              CeedOperator *op_restrict) {
20942b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2095eaf62fffSJeremy L Thompson   Ceed ceed;
20962b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2097eaf62fffSJeremy L Thompson 
2098eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
2099eaf62fffSJeremy L Thompson   CeedBasis basis_fine;
21002b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
2101eaf62fffSJeremy L Thompson   CeedInt Q_f, Q_c;
21022b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
21032b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
21046574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2105eaf62fffSJeremy L Thompson 
210683d6adf3SZach Atkins   // Create coarse to fine basis, if required
210783d6adf3SZach Atkins   CeedBasis basis_c_to_f = NULL;
210883d6adf3SZach Atkins   if (op_prolong || op_restrict) {
210983d6adf3SZach Atkins     // Check if interpolation matrix is provided
21106574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
21116574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
2112eaf62fffSJeremy L Thompson     CeedInt dim, num_comp, num_nodes_c, P_1d_f, P_1d_c;
21132b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
21142b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
21152b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f));
21162b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
21172b730f8bSJeremy L Thompson     P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c);
2118eaf62fffSJeremy L Thompson     CeedScalar *q_ref, *q_weight, *grad;
21192b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_ref));
21202b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_weight));
21212b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad));
21222b730f8bSJeremy 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));
21232b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
21242b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
21252b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
212683d6adf3SZach Atkins   }
2127eaf62fffSJeremy L Thompson 
2128eaf62fffSJeremy L Thompson   // Core code
21292b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2130eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2131eaf62fffSJeremy L Thompson }
2132eaf62fffSJeremy L Thompson 
2133eaf62fffSJeremy L Thompson /**
2134ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a non-tensor basis for the active vector
2135eaf62fffSJeremy L Thompson 
213658e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2137f04ea552SJeremy L Thompson 
2138eaf62fffSJeremy L Thompson   @param[in]  op_fine       Fine grid operator
213985bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2140eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid restriction
2141eaf62fffSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector basis
214285bb9dcfSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
2143eaf62fffSJeremy L Thompson   @param[out] op_coarse     Coarse grid operator
214485bb9dcfSJeremy L Thompson   @param[out] op_prolong    Coarse to fine operator, or NULL
214585bb9dcfSJeremy L Thompson   @param[out] op_restrict   Fine to coarse operator, or NULL
2146eaf62fffSJeremy L Thompson 
2147eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2148eaf62fffSJeremy L Thompson 
2149eaf62fffSJeremy L Thompson   @ref User
2150eaf62fffSJeremy L Thompson **/
21512b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
21522b730f8bSJeremy L Thompson                                        const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
2153eaf62fffSJeremy L Thompson                                        CeedOperator *op_restrict) {
21542b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2155eaf62fffSJeremy L Thompson   Ceed ceed;
21562b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2157eaf62fffSJeremy L Thompson 
2158eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
2159eaf62fffSJeremy L Thompson   CeedBasis basis_fine;
21602b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
2161eaf62fffSJeremy L Thompson   CeedInt Q_f, Q_c;
21622b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
21632b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
21646574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2165eaf62fffSJeremy L Thompson 
2166eaf62fffSJeremy L Thompson   // Coarse to fine basis
216783d6adf3SZach Atkins   CeedBasis basis_c_to_f = NULL;
216883d6adf3SZach Atkins   if (op_prolong || op_restrict) {
216983d6adf3SZach Atkins     // Check if interpolation matrix is provided
21706574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
21716574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
2172eaf62fffSJeremy L Thompson     CeedElemTopology topo;
21732b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_fine, &topo));
2174eaf62fffSJeremy L Thompson     CeedInt dim, num_comp, num_nodes_c, num_nodes_f;
21752b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
21762b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
21772b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f));
21782b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
2179eaf62fffSJeremy L Thompson     CeedScalar *q_ref, *q_weight, *grad;
21802b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref));
21812b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f, &q_weight));
21822b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad));
21832b730f8bSJeremy 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));
21842b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
21852b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
21862b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
218783d6adf3SZach Atkins   }
2188eaf62fffSJeremy L Thompson 
2189eaf62fffSJeremy L Thompson   // Core code
21902b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2191eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2192eaf62fffSJeremy L Thompson }
2193eaf62fffSJeremy L Thompson 
2194eaf62fffSJeremy L Thompson /**
2195ea61e9acSJeremy L Thompson   @brief Build a FDM based approximate inverse for each element for a CeedOperator
2196eaf62fffSJeremy L Thompson 
2197ea61e9acSJeremy L Thompson   This returns a CeedOperator and CeedVector to apply a Fast Diagonalization Method based approximate inverse.
2198859c15bbSJames Wright   This function obtains the simultaneous diagonalization for the 1D mass and Laplacian operators, \f$M = V^T V, K = V^T S V\f$.
2199859c15bbSJames Wright   The assembled QFunction is used to modify the eigenvalues from simultaneous diagonalization and obtain an approximate inverse of the form \f$V^T
22009fd66db6SSebastian Grimberg \hat S V\f$.
22019fd66db6SSebastian Grimberg   The CeedOperator must be linear and non-composite.
22029fd66db6SSebastian Grimberg   The associated CeedQFunction must therefore also be linear.
2203eaf62fffSJeremy L Thompson 
2204ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2205f04ea552SJeremy L Thompson 
2206ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to create element inverses
2207ea61e9acSJeremy L Thompson   @param[out] fdm_inv CeedOperator to apply the action of a FDM based inverse for each element
2208ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2209eaf62fffSJeremy L Thompson 
2210eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2211eaf62fffSJeremy L Thompson 
2212480fae85SJeremy L Thompson   @ref User
2213eaf62fffSJeremy L Thompson **/
22142b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) {
22152b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2216eaf62fffSJeremy L Thompson 
2217eaf62fffSJeremy L Thompson   if (op->CreateFDMElementInverse) {
2218d04bbc78SJeremy L Thompson     // Backend version
22192b730f8bSJeremy L Thompson     CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request));
2220eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2221eaf62fffSJeremy L Thompson   } else {
2222d04bbc78SJeremy L Thompson     // Operator fallback
2223d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2224d04bbc78SJeremy L Thompson 
22252b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2226d04bbc78SJeremy L Thompson     if (op_fallback) {
22272b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request));
2228eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2229eaf62fffSJeremy L Thompson     }
2230eaf62fffSJeremy L Thompson   }
2231eaf62fffSJeremy L Thompson 
2232d04bbc78SJeremy L Thompson   // Default interface implementation
2233eaf62fffSJeremy L Thompson   Ceed ceed, ceed_parent;
22342b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
22352b730f8bSJeremy L Thompson   CeedCall(CeedGetOperatorFallbackParentCeed(ceed, &ceed_parent));
2236eaf62fffSJeremy L Thompson   ceed_parent = ceed_parent ? ceed_parent : ceed;
2237eaf62fffSJeremy L Thompson   CeedQFunction qf;
22382b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
2239eaf62fffSJeremy L Thompson 
2240eaf62fffSJeremy L Thompson   // Determine active input basis
2241eaf62fffSJeremy L Thompson   bool                interp = false, grad = false;
2242eaf62fffSJeremy L Thompson   CeedBasis           basis = NULL;
2243eaf62fffSJeremy L Thompson   CeedElemRestriction rstr  = NULL;
2244eaf62fffSJeremy L Thompson   CeedOperatorField  *op_fields;
2245eaf62fffSJeremy L Thompson   CeedQFunctionField *qf_fields;
2246eaf62fffSJeremy L Thompson   CeedInt             num_input_fields;
22472b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL));
22482b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
2249eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
2250eaf62fffSJeremy L Thompson     CeedVector vec;
22512b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
2252eaf62fffSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2253eaf62fffSJeremy L Thompson       CeedEvalMode eval_mode;
22542b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
2255eaf62fffSJeremy L Thompson       interp = interp || eval_mode == CEED_EVAL_INTERP;
2256eaf62fffSJeremy L Thompson       grad   = grad || eval_mode == CEED_EVAL_GRAD;
22572b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis));
22582b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
2259eaf62fffSJeremy L Thompson     }
2260eaf62fffSJeremy L Thompson   }
22616574a04fSJeremy L Thompson   CeedCheck(basis, ceed, CEED_ERROR_BACKEND, "No active field set");
2262e79b91d9SJeremy L Thompson   CeedSize l_size = 1;
2263352a5e7cSSebastian Grimberg   CeedInt  P_1d, Q_1d, num_nodes, num_qpts, dim, num_comp = 1, num_elem = 1;
22642b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
2265352a5e7cSSebastian Grimberg   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
22662b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
22672b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
22682b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
22692b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
22702b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
22712b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
2272eaf62fffSJeremy L Thompson 
2273eaf62fffSJeremy L Thompson   // Build and diagonalize 1D Mass and Laplacian
22746574a04fSJeremy L Thompson   bool is_tensor_basis;
22756574a04fSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
22766574a04fSJeremy L Thompson   CeedCheck(is_tensor_basis, ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases");
2277eaf62fffSJeremy L Thompson   CeedScalar *mass, *laplace, *x, *fdm_interp, *lambda;
22782b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &mass));
22792b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &laplace));
22802b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &x));
22812b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp));
22822b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &lambda));
2283eaf62fffSJeremy L Thompson   // -- Build matrices
2284eaf62fffSJeremy L Thompson   const CeedScalar *interp_1d, *grad_1d, *q_weight_1d;
22852b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
22862b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
22872b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
22882b730f8bSJeremy L Thompson   CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace));
2289eaf62fffSJeremy L Thompson 
2290eaf62fffSJeremy L Thompson   // -- Diagonalize
22912b730f8bSJeremy L Thompson   CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d));
22922b730f8bSJeremy L Thompson   CeedCall(CeedFree(&mass));
22932b730f8bSJeremy L Thompson   CeedCall(CeedFree(&laplace));
22942b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
22952b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d];
22962b730f8bSJeremy L Thompson   }
22972b730f8bSJeremy L Thompson   CeedCall(CeedFree(&x));
2298eaf62fffSJeremy L Thompson 
2299eaf62fffSJeremy L Thompson   // Assemble QFunction
2300c5f45aeaSJeremy L Thompson   CeedVector          assembled = NULL;
2301c5f45aeaSJeremy L Thompson   CeedElemRestriction rstr_qf   = NULL;
23022b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request));
2303eaf62fffSJeremy L Thompson   CeedInt layout[3];
23042b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_qf, &layout));
23052b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qf));
2306eaf62fffSJeremy L Thompson   CeedScalar max_norm = 0;
23072b730f8bSJeremy L Thompson   CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm));
2308eaf62fffSJeremy L Thompson 
2309eaf62fffSJeremy L Thompson   // Calculate element averages
2310eaf62fffSJeremy L Thompson   CeedInt           num_modes = (interp ? 1 : 0) + (grad ? dim : 0);
2311eaf62fffSJeremy L Thompson   CeedScalar       *elem_avg;
2312eaf62fffSJeremy L Thompson   const CeedScalar *assembled_array, *q_weight_array;
2313eaf62fffSJeremy L Thompson   CeedVector        q_weight;
23142b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight));
23152b730f8bSJeremy L Thompson   CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight));
23162b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array));
23172b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array));
23182b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_elem, &elem_avg));
2319eaf62fffSJeremy L Thompson   const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON;
2320eaf62fffSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
2321eaf62fffSJeremy L Thompson     CeedInt count = 0;
23222b730f8bSJeremy L Thompson     for (CeedInt q = 0; q < num_qpts; q++) {
23232b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) {
23242b730f8bSJeremy L Thompson         if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) {
23252b730f8bSJeremy L Thompson           elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q];
2326eaf62fffSJeremy L Thompson           count++;
2327eaf62fffSJeremy L Thompson         }
23282b730f8bSJeremy L Thompson       }
23292b730f8bSJeremy L Thompson     }
2330eaf62fffSJeremy L Thompson     if (count) {
2331eaf62fffSJeremy L Thompson       elem_avg[e] /= count;
2332eaf62fffSJeremy L Thompson     } else {
2333eaf62fffSJeremy L Thompson       elem_avg[e] = 1.0;
2334eaf62fffSJeremy L Thompson     }
2335eaf62fffSJeremy L Thompson   }
23362b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array));
23372b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled));
23382b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array));
23392b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_weight));
2340eaf62fffSJeremy L Thompson 
2341eaf62fffSJeremy L Thompson   // Build FDM diagonal
2342eaf62fffSJeremy L Thompson   CeedVector  q_data;
2343eaf62fffSJeremy L Thompson   CeedScalar *q_data_array, *fdm_diagonal;
2344352a5e7cSSebastian Grimberg   CeedCall(CeedCalloc(num_comp * num_nodes, &fdm_diagonal));
2345352a5e7cSSebastian Grimberg   const CeedScalar fdm_diagonal_bound = num_nodes * CEED_EPSILON;
23462b730f8bSJeremy L Thompson   for (CeedInt c = 0; c < num_comp; c++) {
2347352a5e7cSSebastian Grimberg     for (CeedInt n = 0; n < num_nodes; n++) {
2348352a5e7cSSebastian Grimberg       if (interp) fdm_diagonal[c * num_nodes + n] = 1.0;
23492b730f8bSJeremy L Thompson       if (grad) {
2350eaf62fffSJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
2351eaf62fffSJeremy L Thompson           CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d;
2352352a5e7cSSebastian Grimberg           fdm_diagonal[c * num_nodes + n] += lambda[i];
2353eaf62fffSJeremy L Thompson         }
2354eaf62fffSJeremy L Thompson       }
2355352a5e7cSSebastian Grimberg       if (fabs(fdm_diagonal[c * num_nodes + n]) < fdm_diagonal_bound) fdm_diagonal[c * num_nodes + n] = fdm_diagonal_bound;
23562b730f8bSJeremy L Thompson     }
23572b730f8bSJeremy L Thompson   }
2358352a5e7cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * num_nodes, &q_data));
23592b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(q_data, 0.0));
23602b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array));
23612b730f8bSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
23622b730f8bSJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
2363352a5e7cSSebastian Grimberg       for (CeedInt n = 0; n < num_nodes; n++) q_data_array[(e * num_comp + c) * num_nodes + n] = 1. / (elem_avg[e] * fdm_diagonal[c * num_nodes + n]);
23642b730f8bSJeremy L Thompson     }
23652b730f8bSJeremy L Thompson   }
23662b730f8bSJeremy L Thompson   CeedCall(CeedFree(&elem_avg));
23672b730f8bSJeremy L Thompson   CeedCall(CeedFree(&fdm_diagonal));
23682b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(q_data, &q_data_array));
2369eaf62fffSJeremy L Thompson 
2370eaf62fffSJeremy L Thompson   // Setup FDM operator
2371eaf62fffSJeremy L Thompson   // -- Basis
2372eaf62fffSJeremy L Thompson   CeedBasis   fdm_basis;
2373eaf62fffSJeremy L Thompson   CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy;
23742b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy));
23752b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &q_ref_dummy));
23762b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &q_weight_dummy));
23772b730f8bSJeremy 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));
23782b730f8bSJeremy L Thompson   CeedCall(CeedFree(&fdm_interp));
23792b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_dummy));
23802b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref_dummy));
23812b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight_dummy));
23822b730f8bSJeremy L Thompson   CeedCall(CeedFree(&lambda));
2383eaf62fffSJeremy L Thompson 
2384eaf62fffSJeremy L Thompson   // -- Restriction
2385eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_qd_i;
2386352a5e7cSSebastian Grimberg   CeedInt             strides[3] = {1, num_nodes, num_nodes * num_comp};
2387352a5e7cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, num_nodes, num_comp, num_elem * num_comp * num_nodes, strides, &rstr_qd_i));
2388eaf62fffSJeremy L Thompson   // -- QFunction
2389eaf62fffSJeremy L Thompson   CeedQFunction qf_fdm;
23902b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm));
23912b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP));
23922b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE));
23932b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP));
23942b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp));
2395eaf62fffSJeremy L Thompson   // -- QFunction context
2396eaf62fffSJeremy L Thompson   CeedInt *num_comp_data;
23972b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, &num_comp_data));
2398eaf62fffSJeremy L Thompson   num_comp_data[0] = num_comp;
2399eaf62fffSJeremy L Thompson   CeedQFunctionContext ctx_fdm;
24002b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm));
24012b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data));
24022b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm));
24032b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_fdm));
2404eaf62fffSJeremy L Thompson   // -- Operator
24052b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv));
24062b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
24072b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_COLLOCATED, q_data));
24082b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2409eaf62fffSJeremy L Thompson 
2410eaf62fffSJeremy L Thompson   // Cleanup
24112b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_data));
24122b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&fdm_basis));
24132b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i));
24142b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_fdm));
2415eaf62fffSJeremy L Thompson 
2416eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2417eaf62fffSJeremy L Thompson }
2418eaf62fffSJeremy L Thompson 
2419eaf62fffSJeremy L Thompson /// @}
2420