xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-preconditioning.c (revision 7c1dbaff56f8afaddfc574209b09713e25514e81)
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 
441862681bSJeremy 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));
127febe2972SJeremy L Thompson     if (op_fallback->num_qpts == 0) CeedCall(CeedOperatorSetNumQuadraturePoints(op_fallback, op->num_qpts));
1289e77b9c8SJeremy L Thompson     // Cleanup
1292b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf_fallback));
1302b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqf_fallback));
1312b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqfT_fallback));
132805fe78eSJeremy L Thompson   }
1332b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetName(op_fallback, op->name));
1342b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fallback));
135b05f7e9fSJeremy L Thompson   // Note: No ref-counting here so we don't get caught in a reference loop.
136b05f7e9fSJeremy L Thompson   //       The op holds the only reference to op_fallback and is responsible for deleting itself and op_fallback.
137805fe78eSJeremy L Thompson   op->op_fallback                 = op_fallback;
138b05f7e9fSJeremy L Thompson   op_fallback->op_fallback_parent = op;
139eaf62fffSJeremy L Thompson 
140eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
141eaf62fffSJeremy L Thompson }
142eaf62fffSJeremy L Thompson 
143eaf62fffSJeremy L Thompson /**
144ea61e9acSJeremy L Thompson   @brief Retrieve fallback CeedOperator with a reference Ceed for advanced CeedOperator functionality
145d04bbc78SJeremy L Thompson 
146d04bbc78SJeremy L Thompson   @param[in]  op          CeedOperator to retrieve fallback for
147d04bbc78SJeremy L Thompson   @param[out] op_fallback Fallback CeedOperator
148d04bbc78SJeremy L Thompson 
149d04bbc78SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
150d04bbc78SJeremy L Thompson 
151d04bbc78SJeremy L Thompson   @ref Developer
152d04bbc78SJeremy L Thompson **/
153d04bbc78SJeremy L Thompson int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) {
154d04bbc78SJeremy L Thompson   // Create if needed
155d04bbc78SJeremy L Thompson   if (!op->op_fallback) {
1562b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreateFallback(op));
157d04bbc78SJeremy L Thompson   }
158d04bbc78SJeremy L Thompson   if (op->op_fallback) {
159d04bbc78SJeremy L Thompson     bool is_debug;
160d04bbc78SJeremy L Thompson 
1612b730f8bSJeremy L Thompson     CeedCall(CeedIsDebug(op->ceed, &is_debug));
162d04bbc78SJeremy L Thompson     if (is_debug) {
163b275c451SJeremy L Thompson       Ceed        ceed, ceed_fallback;
164d04bbc78SJeremy L Thompson       const char *resource, *resource_fallback;
165d04bbc78SJeremy L Thompson 
166b275c451SJeremy L Thompson       CeedCall(CeedOperatorGetCeed(op, &ceed));
167b275c451SJeremy L Thompson       CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
168b275c451SJeremy L Thompson       CeedCall(CeedGetResource(ceed, &resource));
1692b730f8bSJeremy L Thompson       CeedCall(CeedGetResource(ceed_fallback, &resource_fallback));
170d04bbc78SJeremy L Thompson 
17123d4529eSJeremy L Thompson       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n");
172b275c451SJeremy L Thompson       CeedDebug(ceed, "Falling back from %s operator at address %ld to %s operator at address %ld\n", resource, op, resource_fallback,
1732b730f8bSJeremy L Thompson                 op->op_fallback);
174d04bbc78SJeremy L Thompson     }
175d04bbc78SJeremy L Thompson   }
176d04bbc78SJeremy L Thompson   *op_fallback = op->op_fallback;
177d04bbc78SJeremy L Thompson 
178d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
179d04bbc78SJeremy L Thompson }
180d04bbc78SJeremy L Thompson 
181d04bbc78SJeremy L Thompson /**
182eaf62fffSJeremy L Thompson   @brief Select correct basis matrix pointer based on CeedEvalMode
183eaf62fffSJeremy L Thompson 
184352a5e7cSSebastian Grimberg   @param[in]  basis     CeedBasis from which to get the basis matrix
185eaf62fffSJeremy L Thompson   @param[in]  eval_mode Current basis evaluation mode
186eaf62fffSJeremy L Thompson   @param[in]  identity  Pointer to identity matrix
187eaf62fffSJeremy L Thompson   @param[out] basis_ptr Basis pointer to set
188eaf62fffSJeremy L Thompson 
189eaf62fffSJeremy L Thompson   @ref Developer
190eaf62fffSJeremy L Thompson **/
191352a5e7cSSebastian Grimberg static inline int CeedOperatorGetBasisPointer(CeedBasis basis, CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar **basis_ptr) {
192eaf62fffSJeremy L Thompson   switch (eval_mode) {
193eaf62fffSJeremy L Thompson     case CEED_EVAL_NONE:
194eaf62fffSJeremy L Thompson       *basis_ptr = identity;
195eaf62fffSJeremy L Thompson       break;
196eaf62fffSJeremy L Thompson     case CEED_EVAL_INTERP:
197352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetInterp(basis, basis_ptr));
198eaf62fffSJeremy L Thompson       break;
199eaf62fffSJeremy L Thompson     case CEED_EVAL_GRAD:
200352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetGrad(basis, basis_ptr));
201352a5e7cSSebastian Grimberg       break;
202352a5e7cSSebastian Grimberg     case CEED_EVAL_DIV:
203352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetDiv(basis, basis_ptr));
204352a5e7cSSebastian Grimberg       break;
205352a5e7cSSebastian Grimberg     case CEED_EVAL_CURL:
206352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetCurl(basis, basis_ptr));
207eaf62fffSJeremy L Thompson       break;
208eaf62fffSJeremy L Thompson     case CEED_EVAL_WEIGHT:
209eaf62fffSJeremy L Thompson       break;  // Caught by QF Assembly
210eaf62fffSJeremy L Thompson   }
211ed9e99e6SJeremy L Thompson   assert(*basis_ptr != NULL);
212352a5e7cSSebastian Grimberg 
213352a5e7cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
214eaf62fffSJeremy L Thompson }
215eaf62fffSJeremy L Thompson 
216eaf62fffSJeremy L Thompson /**
217eaf62fffSJeremy L Thompson   @brief Create point block restriction for active operator field
218eaf62fffSJeremy L Thompson 
219eaf62fffSJeremy L Thompson   @param[in]  rstr            Original CeedElemRestriction for active field
220ea61e9acSJeremy L Thompson   @param[out] pointblock_rstr Address of the variable where the newly created CeedElemRestriction will be stored
221eaf62fffSJeremy L Thompson 
222eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
223eaf62fffSJeremy L Thompson 
224eaf62fffSJeremy L Thompson   @ref Developer
225eaf62fffSJeremy L Thompson **/
2262b730f8bSJeremy L Thompson static int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *pointblock_rstr) {
227eaf62fffSJeremy L Thompson   Ceed ceed;
2282b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
229eaf62fffSJeremy L Thompson   const CeedInt *offsets;
2302b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
231eaf62fffSJeremy L Thompson 
232eaf62fffSJeremy L Thompson   // Expand offsets
2337b63f5c6SJed Brown   CeedInt  num_elem, num_comp, elem_size, comp_stride, *pointblock_offsets;
2347b63f5c6SJed Brown   CeedSize l_size;
2352b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
2362b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
2372b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
2382b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
2392b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
240eaf62fffSJeremy L Thompson   CeedInt shift = num_comp;
2412b730f8bSJeremy L Thompson   if (comp_stride != 1) shift *= num_comp;
2422b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_elem * elem_size, &pointblock_offsets));
243eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_elem * elem_size; i++) {
244eaf62fffSJeremy L Thompson     pointblock_offsets[i] = offsets[i] * shift;
245eaf62fffSJeremy L Thompson   }
246eaf62fffSJeremy L Thompson 
247eaf62fffSJeremy L Thompson   // Create new restriction
2482b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
2492b730f8bSJeremy L Thompson                                      pointblock_offsets, pointblock_rstr));
250eaf62fffSJeremy L Thompson 
251eaf62fffSJeremy L Thompson   // Cleanup
2522b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
253eaf62fffSJeremy L Thompson 
254eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
255eaf62fffSJeremy L Thompson }
256eaf62fffSJeremy L Thompson 
257eaf62fffSJeremy L Thompson /**
258eaf62fffSJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
259eaf62fffSJeremy L Thompson 
260eaf62fffSJeremy L Thompson   @param[in]  op            CeedOperator to assemble point block diagonal
261ea61e9acSJeremy L Thompson   @param[in]  request       Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE
262eaf62fffSJeremy L Thompson   @param[in]  is_pointblock Boolean flag to assemble diagonal or point block diagonal
263eaf62fffSJeremy L Thompson   @param[out] assembled     CeedVector to store assembled diagonal
264eaf62fffSJeremy L Thompson 
265eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
266eaf62fffSJeremy L Thompson 
267eaf62fffSJeremy L Thompson   @ref Developer
268eaf62fffSJeremy L Thompson **/
2692b730f8bSJeremy L Thompson static inline int CeedSingleOperatorAssembleAddDiagonal_Core(CeedOperator op, CeedRequest *request, const bool is_pointblock, CeedVector assembled) {
270eaf62fffSJeremy L Thompson   Ceed ceed;
2712b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
272eaf62fffSJeremy L Thompson 
273eaf62fffSJeremy L Thompson   // Assemble QFunction
274eaf62fffSJeremy L Thompson   CeedQFunction       qf;
275437c7c90SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
276c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
277c5f45aeaSJeremy L Thompson   CeedElemRestriction assembled_elem_rstr = NULL;
278437c7c90SJeremy L Thompson   CeedInt             num_input_fields, num_output_fields;
279eaf62fffSJeremy L Thompson   CeedInt             layout[3];
280437c7c90SJeremy L Thompson 
281437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
282437c7c90SJeremy L Thompson   CeedCall(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
283437c7c90SJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request));
284437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, &layout));
285437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
286437c7c90SJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
287eaf62fffSJeremy L Thompson 
288ed9e99e6SJeremy L Thompson   // Get assembly data
289ed9e99e6SJeremy L Thompson   CeedOperatorAssemblyData data;
290437c7c90SJeremy L Thompson   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
291437c7c90SJeremy L Thompson   CeedInt                 *num_eval_modes_in, *num_eval_modes_out, num_active_bases;
292437c7c90SJeremy L Thompson   CeedSize               **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components;
293437c7c90SJeremy L Thompson   CeedBasis               *active_bases;
294437c7c90SJeremy L Thompson   CeedElemRestriction     *active_elem_rstrs;
295437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
296437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in,
297437c7c90SJeremy L Thompson                                                 &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out, &num_output_components));
298437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases, NULL, NULL));
299437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs));
300437c7c90SJeremy L Thompson 
301437c7c90SJeremy L Thompson   // Loop over all active bases
302437c7c90SJeremy L Thompson   for (CeedInt b = 0; b < num_active_bases; b++) {
303eaf62fffSJeremy L Thompson     // Assemble point block diagonal restriction, if needed
304*7c1dbaffSSebastian Grimberg     CeedElemRestriction diag_elem_rstr;
305eaf62fffSJeremy L Thompson     if (is_pointblock) {
306*7c1dbaffSSebastian Grimberg       CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstrs[b], &diag_elem_rstr));
307*7c1dbaffSSebastian Grimberg     } else {
308*7c1dbaffSSebastian Grimberg       CeedCall(CeedElemRestrictionCreateUnsignedCopy(active_elem_rstrs[b], &diag_elem_rstr));
309eaf62fffSJeremy L Thompson     }
310eaf62fffSJeremy L Thompson 
311eaf62fffSJeremy L Thompson     // Create diagonal vector
312eaf62fffSJeremy L Thompson     CeedVector elem_diag;
313437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag));
314eaf62fffSJeremy L Thompson 
315eaf62fffSJeremy L Thompson     // Assemble element operator diagonals
3169c774eddSJeremy L Thompson     CeedScalar *elem_diag_array;
317437c7c90SJeremy L Thompson     CeedInt     num_elem, num_nodes, num_qpts, num_components;
318437c7c90SJeremy L Thompson 
3192b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(elem_diag, 0.0));
3202b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array));
321437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem));
322437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(active_bases[b], &num_nodes));
323437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(active_bases[b], &num_components));
324437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(active_bases[b], &num_qpts));
325ed9e99e6SJeremy L Thompson 
326352a5e7cSSebastian Grimberg     // Construct identity matrix for basis if required
327ed9e99e6SJeremy L Thompson     bool        has_eval_none = false;
328352a5e7cSSebastian Grimberg     CeedScalar *identity      = NULL;
329437c7c90SJeremy L Thompson     for (CeedInt i = 0; i < num_eval_modes_in[b]; i++) {
330437c7c90SJeremy L Thompson       has_eval_none = has_eval_none || (eval_modes_in[b][i] == CEED_EVAL_NONE);
331ed9e99e6SJeremy L Thompson     }
332437c7c90SJeremy L Thompson     for (CeedInt i = 0; i < num_eval_modes_out[b]; i++) {
333437c7c90SJeremy L Thompson       has_eval_none = has_eval_none || (eval_modes_out[b][i] == CEED_EVAL_NONE);
334ed9e99e6SJeremy L Thompson     }
335ed9e99e6SJeremy L Thompson     if (has_eval_none) {
3362b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
3372b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
338eaf62fffSJeremy L Thompson     }
339352a5e7cSSebastian Grimberg 
340eaf62fffSJeremy L Thompson     // Compute the diagonal of B^T D B
341eaf62fffSJeremy L Thompson     // Each element
342b94338b9SJed Brown     for (CeedSize e = 0; e < num_elem; e++) {
343eaf62fffSJeremy L Thompson       // Each basis eval mode pair
344352a5e7cSSebastian Grimberg       CeedInt      d_out              = 0, q_comp_out;
345352a5e7cSSebastian Grimberg       CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
346437c7c90SJeremy L Thompson       for (CeedInt e_out = 0; e_out < num_eval_modes_out[b]; e_out++) {
347437c7c90SJeremy L Thompson         const CeedScalar *B_t = NULL;
348352a5e7cSSebastian Grimberg         CeedOperatorGetBasisPointer(active_bases[b], eval_modes_out[b][e_out], identity, &B_t);
349352a5e7cSSebastian Grimberg         CeedCall(CeedBasisGetNumQuadratureComponents(active_bases[b], eval_modes_out[b][e_out], &q_comp_out));
350352a5e7cSSebastian Grimberg         if (q_comp_out > 1) {
351352a5e7cSSebastian Grimberg           if (e_out == 0 || eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0;
352352a5e7cSSebastian Grimberg           else B_t = &B_t[(++d_out) * num_qpts * num_nodes];
353352a5e7cSSebastian Grimberg         }
354352a5e7cSSebastian Grimberg         eval_mode_out_prev = eval_modes_out[b][e_out];
355352a5e7cSSebastian Grimberg 
356352a5e7cSSebastian Grimberg         CeedInt      d_in              = 0, q_comp_in;
357352a5e7cSSebastian Grimberg         CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE;
358437c7c90SJeremy L Thompson         for (CeedInt e_in = 0; e_in < num_eval_modes_in[b]; e_in++) {
359437c7c90SJeremy L Thompson           const CeedScalar *B = NULL;
360352a5e7cSSebastian Grimberg           CeedOperatorGetBasisPointer(active_bases[b], eval_modes_in[b][e_in], identity, &B);
361352a5e7cSSebastian Grimberg           CeedCall(CeedBasisGetNumQuadratureComponents(active_bases[b], eval_modes_in[b][e_in], &q_comp_in));
362352a5e7cSSebastian Grimberg           if (q_comp_in > 1) {
363352a5e7cSSebastian Grimberg             if (e_in == 0 || eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0;
364352a5e7cSSebastian Grimberg             else B = &B[(++d_in) * num_qpts * num_nodes];
365352a5e7cSSebastian Grimberg           }
366352a5e7cSSebastian Grimberg           eval_mode_in_prev = eval_modes_in[b][e_in];
367352a5e7cSSebastian Grimberg 
368eaf62fffSJeremy L Thompson           // Each component
369437c7c90SJeremy L Thompson           for (CeedInt c_out = 0; c_out < num_components; c_out++) {
370437c7c90SJeremy L Thompson             // Each qpt/node pair
3712b730f8bSJeremy L Thompson             for (CeedInt q = 0; q < num_qpts; q++) {
372eaf62fffSJeremy L Thompson               if (is_pointblock) {
373eaf62fffSJeremy L Thompson                 // Point Block Diagonal
374437c7c90SJeremy L Thompson                 for (CeedInt c_in = 0; c_in < num_components; c_in++) {
375b94338b9SJed Brown                   const CeedSize c_offset = (eval_mode_offsets_in[b][e_in] + c_in) * num_output_components + eval_mode_offsets_out[b][e_out] + c_out;
376437c7c90SJeremy L Thompson                   const CeedScalar qf_value = assembled_qf_array[q * layout[0] + c_offset * layout[1] + e * layout[2]];
3772b730f8bSJeremy L Thompson                   for (CeedInt n = 0; n < num_nodes; n++) {
378437c7c90SJeremy L Thompson                     elem_diag_array[((e * num_components + c_out) * num_components + c_in) * num_nodes + n] +=
379437c7c90SJeremy L Thompson                         B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n];
380eaf62fffSJeremy L Thompson                   }
3812b730f8bSJeremy L Thompson                 }
382eaf62fffSJeremy L Thompson               } else {
383eaf62fffSJeremy L Thompson                 // Diagonal Only
384437c7c90SJeremy 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;
385437c7c90SJeremy L Thompson                 const CeedScalar qf_value = assembled_qf_array[q * layout[0] + c_offset * layout[1] + e * layout[2]];
3862b730f8bSJeremy L Thompson                 for (CeedInt n = 0; n < num_nodes; n++) {
387437c7c90SJeremy 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];
388eaf62fffSJeremy L Thompson                 }
389eaf62fffSJeremy L Thompson               }
390eaf62fffSJeremy L Thompson             }
391eaf62fffSJeremy L Thompson           }
3922b730f8bSJeremy L Thompson         }
3932b730f8bSJeremy L Thompson       }
3942b730f8bSJeremy L Thompson     }
3952b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
396eaf62fffSJeremy L Thompson 
397eaf62fffSJeremy L Thompson     // Assemble local operator diagonal
398*7c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
399eaf62fffSJeremy L Thompson 
400eaf62fffSJeremy L Thompson     // Cleanup
401*7c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr));
4022b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&elem_diag));
4032b730f8bSJeremy L Thompson     CeedCall(CeedFree(&identity));
404437c7c90SJeremy L Thompson   }
405437c7c90SJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
406437c7c90SJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
407eaf62fffSJeremy L Thompson 
408eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
409eaf62fffSJeremy L Thompson }
410eaf62fffSJeremy L Thompson 
411eaf62fffSJeremy L Thompson /**
412eaf62fffSJeremy L Thompson   @brief Core logic for assembling composite operator diagonal
413eaf62fffSJeremy L Thompson 
414eaf62fffSJeremy L Thompson   @param[in]  op            CeedOperator to assemble point block diagonal
415ea61e9acSJeremy L Thompson   @param[in]  request       Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE
416eaf62fffSJeremy L Thompson   @param[in]  is_pointblock Boolean flag to assemble diagonal or point block diagonal
417eaf62fffSJeremy L Thompson   @param[out] assembled     CeedVector to store assembled diagonal
418eaf62fffSJeremy L Thompson 
419eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
420eaf62fffSJeremy L Thompson 
421eaf62fffSJeremy L Thompson   @ref Developer
422eaf62fffSJeremy L Thompson **/
4232b730f8bSJeremy L Thompson static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_pointblock,
424eaf62fffSJeremy L Thompson                                                                  CeedVector assembled) {
425eaf62fffSJeremy L Thompson   CeedInt       num_sub;
426eaf62fffSJeremy L Thompson   CeedOperator *suboperators;
427c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
428c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators));
429eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_sub; i++) {
4306aa95790SJeremy L Thompson     if (is_pointblock) {
4312b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request));
4326aa95790SJeremy L Thompson     } else {
4332b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request));
4346aa95790SJeremy L Thompson     }
435eaf62fffSJeremy L Thompson   }
436eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
437eaf62fffSJeremy L Thompson }
438eaf62fffSJeremy L Thompson 
439eaf62fffSJeremy L Thompson /**
440eaf62fffSJeremy L Thompson   @brief Build nonzero pattern for non-composite operator
441eaf62fffSJeremy L Thompson 
442eaf62fffSJeremy L Thompson   Users should generally use CeedOperatorLinearAssembleSymbolic()
443eaf62fffSJeremy L Thompson 
444eaf62fffSJeremy L Thompson   @param[in]  op     CeedOperator to assemble nonzero pattern
445eaf62fffSJeremy L Thompson   @param[in]  offset Offset for number of entries
446eaf62fffSJeremy L Thompson   @param[out] rows   Row number for each entry
447eaf62fffSJeremy L Thompson   @param[out] cols   Column number for each entry
448eaf62fffSJeremy L Thompson 
449eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
450eaf62fffSJeremy L Thompson 
451eaf62fffSJeremy L Thompson   @ref Developer
452eaf62fffSJeremy L Thompson **/
4532b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) {
454f3d47e36SJeremy L Thompson   Ceed ceed;
455f3d47e36SJeremy L Thompson   bool is_composite;
456f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
457f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4586574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
459eaf62fffSJeremy L Thompson 
460c9366a6bSJeremy L Thompson   CeedSize num_nodes;
4612b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes, NULL));
462*7c1dbaffSSebastian Grimberg   CeedElemRestriction active_rstr;
463*7c1dbaffSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestriction(op, &active_rstr));
464*7c1dbaffSSebastian Grimberg   CeedElemRestriction index_elem_rstr;
465*7c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionCreateUnorientedCopy(active_rstr, &index_elem_rstr));
466e79b91d9SJeremy L Thompson   CeedInt num_elem, elem_size, num_comp;
467*7c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(index_elem_rstr, &num_elem));
468*7c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(index_elem_rstr, &elem_size));
469*7c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(index_elem_rstr, &num_comp));
470eaf62fffSJeremy L Thompson   CeedInt layout_er[3];
471*7c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionGetELayout(index_elem_rstr, &layout_er));
472eaf62fffSJeremy L Thompson 
473eaf62fffSJeremy L Thompson   CeedInt local_num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
474eaf62fffSJeremy L Thompson 
475eaf62fffSJeremy L Thompson   // Determine elem_dof relation
476eaf62fffSJeremy L Thompson   CeedVector index_vec;
4772b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, num_nodes, &index_vec));
478eaf62fffSJeremy L Thompson   CeedScalar *array;
4792b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(index_vec, CEED_MEM_HOST, &array));
480ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_nodes; i++) array[i] = i;
4812b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(index_vec, &array));
482eaf62fffSJeremy L Thompson   CeedVector elem_dof;
4832b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, num_elem * elem_size * num_comp, &elem_dof));
4842b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(elem_dof, 0.0));
485*7c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionApply(index_elem_rstr, CEED_NOTRANSPOSE, index_vec, elem_dof, CEED_REQUEST_IMMEDIATE));
486eaf62fffSJeremy L Thompson   const CeedScalar *elem_dof_a;
4872b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(elem_dof, CEED_MEM_HOST, &elem_dof_a));
4882b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&index_vec));
489eaf62fffSJeremy L Thompson 
490eaf62fffSJeremy L Thompson   // Determine i, j locations for element matrices
491b94338b9SJed Brown   CeedSize count = 0;
492ed9e99e6SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
493ed9e99e6SJeremy L Thompson     for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
494ed9e99e6SJeremy L Thompson       for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
495ed9e99e6SJeremy L Thompson         for (CeedInt i = 0; i < elem_size; i++) {
496ed9e99e6SJeremy L Thompson           for (CeedInt j = 0; j < elem_size; j++) {
4972b730f8bSJeremy L Thompson             const CeedInt elem_dof_index_row = i * layout_er[0] + (comp_out)*layout_er[1] + e * layout_er[2];
4982b730f8bSJeremy L Thompson             const CeedInt elem_dof_index_col = j * layout_er[0] + comp_in * layout_er[1] + e * layout_er[2];
499eaf62fffSJeremy L Thompson 
500eaf62fffSJeremy L Thompson             const CeedInt row = elem_dof_a[elem_dof_index_row];
501eaf62fffSJeremy L Thompson             const CeedInt col = elem_dof_a[elem_dof_index_col];
502eaf62fffSJeremy L Thompson 
503eaf62fffSJeremy L Thompson             rows[offset + count] = row;
504eaf62fffSJeremy L Thompson             cols[offset + count] = col;
505eaf62fffSJeremy L Thompson             count++;
506eaf62fffSJeremy L Thompson           }
507eaf62fffSJeremy L Thompson         }
508eaf62fffSJeremy L Thompson       }
509eaf62fffSJeremy L Thompson     }
510eaf62fffSJeremy L Thompson   }
5116574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing assembled entries");
5122b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(elem_dof, &elem_dof_a));
5132b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&elem_dof));
514*7c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr));
515eaf62fffSJeremy L Thompson 
516eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
517eaf62fffSJeremy L Thompson }
518eaf62fffSJeremy L Thompson 
519eaf62fffSJeremy L Thompson /**
520eaf62fffSJeremy L Thompson   @brief Assemble nonzero entries for non-composite operator
521eaf62fffSJeremy L Thompson 
522eaf62fffSJeremy L Thompson   Users should generally use CeedOperatorLinearAssemble()
523eaf62fffSJeremy L Thompson 
524eaf62fffSJeremy L Thompson   @param[in]  op     CeedOperator to assemble
525ea61e9acSJeremy L Thompson   @param[in]  offset Offset for number of entries
526eaf62fffSJeremy L Thompson   @param[out] values Values to assemble into matrix
527eaf62fffSJeremy L Thompson 
528eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
529eaf62fffSJeremy L Thompson 
530eaf62fffSJeremy L Thompson   @ref Developer
531eaf62fffSJeremy L Thompson **/
5322b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) {
533f3d47e36SJeremy L Thompson   Ceed ceed;
534f3d47e36SJeremy L Thompson   bool is_composite;
535f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
536f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
537f3d47e36SJeremy L Thompson 
5386574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
539f3d47e36SJeremy L Thompson 
540f3d47e36SJeremy L Thompson   // Early exit for empty operator
541f3d47e36SJeremy L Thompson   {
542f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
543f3d47e36SJeremy L Thompson 
544f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
545f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
546f3d47e36SJeremy L Thompson   }
547eaf62fffSJeremy L Thompson 
548cefa2673SJeremy L Thompson   if (op->LinearAssembleSingle) {
549cefa2673SJeremy L Thompson     // Backend version
5502b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSingle(op, offset, values));
551cefa2673SJeremy L Thompson     return CEED_ERROR_SUCCESS;
552cefa2673SJeremy L Thompson   } else {
553cefa2673SJeremy L Thompson     // Operator fallback
554cefa2673SJeremy L Thompson     CeedOperator op_fallback;
555cefa2673SJeremy L Thompson 
5562b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
557cefa2673SJeremy L Thompson     if (op_fallback) {
5582b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values));
559cefa2673SJeremy L Thompson       return CEED_ERROR_SUCCESS;
560cefa2673SJeremy L Thompson     }
561cefa2673SJeremy L Thompson   }
562cefa2673SJeremy L Thompson 
563eaf62fffSJeremy L Thompson   // Assemble QFunction
564eaf62fffSJeremy L Thompson   CeedQFunction qf;
5652b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
566c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf = NULL;
567c5f45aeaSJeremy L Thompson   CeedElemRestriction rstr_q       = NULL;
5682b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE));
569eaf62fffSJeremy L Thompson 
5707e7773b5SJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
571eaf62fffSJeremy L Thompson   CeedOperatorField *input_fields;
572eaf62fffSJeremy L Thompson   CeedOperatorField *output_fields;
5732b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
574eaf62fffSJeremy L Thompson 
575ed9e99e6SJeremy L Thompson   // Get assembly data
576ed9e99e6SJeremy L Thompson   CeedOperatorAssemblyData data;
5772b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
578437c7c90SJeremy L Thompson   const CeedEvalMode **eval_modes_in, **eval_modes_out;
579437c7c90SJeremy L Thompson   CeedInt             *num_eval_modes_in, *num_eval_modes_out, num_active_bases;
580437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases, &num_eval_modes_in, &eval_modes_in, NULL, &num_eval_modes_out,
581437c7c90SJeremy L Thompson                                                 &eval_modes_out, NULL, NULL));
582437c7c90SJeremy L Thompson   CeedBasis *bases;
583437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &bases, NULL, NULL));
584437c7c90SJeremy L Thompson   CeedBasis basis_in = bases[0];
585eaf62fffSJeremy L Thompson 
5866574a04fSJeremy L Thompson   CeedCheck(num_active_bases == 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator with multiple active bases");
5876574a04fSJeremy 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");
588eaf62fffSJeremy L Thompson 
589ed9e99e6SJeremy L Thompson   CeedElemRestriction active_rstr;
5902b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &active_rstr));
591*7c1dbaffSSebastian Grimberg   CeedInt num_elem, elem_size, num_qpts, num_comp;
5922b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(active_rstr, &num_elem));
5932b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(active_rstr, &elem_size));
5942b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(active_rstr, &num_comp));
5952b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
596eaf62fffSJeremy L Thompson 
597*7c1dbaffSSebastian Grimberg   CeedRestrictionType rstr_type;
598*7c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionGetType(active_rstr, &rstr_type));
599*7c1dbaffSSebastian Grimberg   const bool     *orients      = NULL;
600*7c1dbaffSSebastian Grimberg   const CeedInt8 *curl_orients = NULL;
601*7c1dbaffSSebastian Grimberg   if (rstr_type == CEED_RESTRICTION_ORIENTED) {
602*7c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOrientations(active_rstr, CEED_MEM_HOST, &orients));
603*7c1dbaffSSebastian Grimberg   } else if (rstr_type == CEED_RESTRICTION_CURL_ORIENTED) {
604*7c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetCurlOrientations(active_rstr, CEED_MEM_HOST, &curl_orients));
605*7c1dbaffSSebastian Grimberg   }
606*7c1dbaffSSebastian Grimberg 
607eaf62fffSJeremy L Thompson   CeedInt local_num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
608eaf62fffSJeremy L Thompson 
609*7c1dbaffSSebastian Grimberg   // Loop over elements and put in data structure
610eaf62fffSJeremy L Thompson   const CeedScalar *assembled_qf_array;
6112b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
612eaf62fffSJeremy L Thompson 
613eaf62fffSJeremy L Thompson   CeedInt layout_qf[3];
6142b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_q, &layout_qf));
6152b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_q));
616eaf62fffSJeremy L Thompson 
617*7c1dbaffSSebastian Grimberg   // We store B_mat_in, B_mat_out, BTD, elem_mat in row-major order
618437c7c90SJeremy L Thompson   const CeedScalar **B_mats_in, **B_mats_out;
619437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, NULL, &B_mats_in, &B_mats_out));
620437c7c90SJeremy L Thompson   const CeedScalar *B_mat_in = B_mats_in[0], *B_mat_out = B_mats_out[0];
621437c7c90SJeremy L Thompson   CeedScalar        BTD_mat[elem_size * num_qpts * num_eval_modes_in[0]];
622eaf62fffSJeremy L Thompson   CeedScalar        elem_mat[elem_size * elem_size];
623b94338b9SJed Brown   CeedSize          count = 0;
624eaf62fffSJeremy L Thompson   CeedScalar       *vals;
62528ec399dSJeremy L Thompson   CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals));
626b94338b9SJed Brown   for (CeedSize e = 0; e < num_elem; e++) {
627ed9e99e6SJeremy L Thompson     for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
628ed9e99e6SJeremy L Thompson       for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
629ed9e99e6SJeremy L Thompson         // Compute B^T*D
630b94338b9SJed Brown         for (CeedSize n = 0; n < elem_size; n++) {
631b94338b9SJed Brown           for (CeedSize q = 0; q < num_qpts; q++) {
632437c7c90SJeremy L Thompson             for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) {
633b94338b9SJed Brown               const CeedSize btd_index = n * (num_qpts * num_eval_modes_in[0]) + (num_eval_modes_in[0] * q + e_in);
634067fd99fSJeremy L Thompson               CeedScalar     sum       = 0.0;
635437c7c90SJeremy L Thompson               for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) {
636b94338b9SJed Brown                 const CeedSize b_out_index     = (num_eval_modes_out[0] * q + e_out) * elem_size + n;
637b94338b9SJed Brown                 const CeedSize eval_mode_index = ((e_in * num_comp + comp_in) * num_eval_modes_out[0] + e_out) * num_comp + comp_out;
638b94338b9SJed Brown                 const CeedSize qf_index        = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2];
639067fd99fSJeremy L Thompson                 sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index];
640eaf62fffSJeremy L Thompson               }
641067fd99fSJeremy L Thompson               BTD_mat[btd_index] = sum;
642ed9e99e6SJeremy L Thompson             }
643ed9e99e6SJeremy L Thompson           }
644eaf62fffSJeremy L Thompson         }
645*7c1dbaffSSebastian Grimberg 
646*7c1dbaffSSebastian Grimberg         // Form element matrix itself (for each block component)
647437c7c90SJeremy L Thompson         CeedCall(CeedMatrixMatrixMultiply(ceed, BTD_mat, B_mat_in, elem_mat, elem_size, elem_size, num_qpts * num_eval_modes_in[0]));
648eaf62fffSJeremy L Thompson 
649*7c1dbaffSSebastian Grimberg         // Transform the element matrix if required
650*7c1dbaffSSebastian Grimberg         if (orients) {
651*7c1dbaffSSebastian Grimberg           const bool *elem_orients = &orients[e * elem_size];
652*7c1dbaffSSebastian Grimberg           for (CeedInt i = 0; i < elem_size; i++) {
653*7c1dbaffSSebastian Grimberg             for (CeedInt j = 0; j < elem_size; j++) {
654*7c1dbaffSSebastian Grimberg               elem_mat[i * elem_size + j] *= elem_orients[i] ? -1.0 : 1.0;
655*7c1dbaffSSebastian Grimberg               elem_mat[i * elem_size + j] *= elem_orients[j] ? -1.0 : 1.0;
656*7c1dbaffSSebastian Grimberg             }
657*7c1dbaffSSebastian Grimberg           }
658*7c1dbaffSSebastian Grimberg         } else if (curl_orients) {
659*7c1dbaffSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &curl_orients[e * 3 * elem_size];
660*7c1dbaffSSebastian Grimberg           CeedScalar      o_elem_mat[elem_size * elem_size];
661*7c1dbaffSSebastian Grimberg           // T^T*(B^T*D*B)
662*7c1dbaffSSebastian Grimberg           for (CeedInt i = 0; i < elem_size; i++) {
663*7c1dbaffSSebastian Grimberg             for (CeedInt j = 0; j < elem_size; j++) {
664*7c1dbaffSSebastian Grimberg               o_elem_mat[i * elem_size + j] = elem_mat[i * elem_size + j] * elem_curl_orients[3 * i + 1] +
665*7c1dbaffSSebastian Grimberg                                               (i > 0 ? elem_mat[(i - 1) * elem_size + j] * elem_curl_orients[3 * i - 1] : 0.0) +
666*7c1dbaffSSebastian Grimberg                                               (i < elem_size - 1 ? elem_mat[(i + 1) * elem_size + j] * elem_curl_orients[3 * i + 3] : 0.0);
667*7c1dbaffSSebastian Grimberg             }
668*7c1dbaffSSebastian Grimberg           }
669*7c1dbaffSSebastian Grimberg           // T^T*(B^T*D*B)*T
670*7c1dbaffSSebastian Grimberg           for (CeedInt i = 0; i < elem_size; i++) {
671*7c1dbaffSSebastian Grimberg             for (CeedInt j = 0; j < elem_size; j++) {
672*7c1dbaffSSebastian Grimberg               elem_mat[i * elem_size + j] = o_elem_mat[i * elem_size + j] * elem_curl_orients[3 * j + 1] +
673*7c1dbaffSSebastian Grimberg                                             (j > 0 ? o_elem_mat[i * elem_size + j - 1] * elem_curl_orients[3 * j - 1] : 0.0) +
674*7c1dbaffSSebastian Grimberg                                             (j < elem_size - 1 ? o_elem_mat[i * elem_size + j + 1] * elem_curl_orients[3 * j + 3] : 0.0);
675*7c1dbaffSSebastian Grimberg             }
676*7c1dbaffSSebastian Grimberg           }
677*7c1dbaffSSebastian Grimberg         }
678*7c1dbaffSSebastian Grimberg 
679*7c1dbaffSSebastian Grimberg         // Put element matrix in coordinate data structure
680ed9e99e6SJeremy L Thompson         for (CeedInt i = 0; i < elem_size; i++) {
681ed9e99e6SJeremy L Thompson           for (CeedInt j = 0; j < elem_size; j++) {
682eaf62fffSJeremy L Thompson             vals[offset + count] = elem_mat[i * elem_size + j];
683eaf62fffSJeremy L Thompson             count++;
684eaf62fffSJeremy L Thompson           }
685eaf62fffSJeremy L Thompson         }
686eaf62fffSJeremy L Thompson       }
687eaf62fffSJeremy L Thompson     }
688eaf62fffSJeremy L Thompson   }
6896574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing entries");
6902b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(values, &vals));
691eaf62fffSJeremy L Thompson 
6922b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
6932b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
694eaf62fffSJeremy L Thompson 
695*7c1dbaffSSebastian Grimberg   if (rstr_type == CEED_RESTRICTION_ORIENTED) {
696*7c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOrientations(active_rstr, &orients));
697*7c1dbaffSSebastian Grimberg   } else if (rstr_type == CEED_RESTRICTION_CURL_ORIENTED) {
698*7c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreCurlOrientations(active_rstr, &curl_orients));
699*7c1dbaffSSebastian Grimberg   }
700*7c1dbaffSSebastian Grimberg 
701eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
702eaf62fffSJeremy L Thompson }
703eaf62fffSJeremy L Thompson 
704eaf62fffSJeremy L Thompson /**
705eaf62fffSJeremy L Thompson   @brief Count number of entries for assembled CeedOperator
706eaf62fffSJeremy L Thompson 
707eaf62fffSJeremy L Thompson   @param[in]  op          CeedOperator to assemble
708eaf62fffSJeremy L Thompson   @param[out] num_entries Number of entries in assembled representation
709eaf62fffSJeremy L Thompson 
710eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
711eaf62fffSJeremy L Thompson 
712eaf62fffSJeremy L Thompson   @ref Utility
713eaf62fffSJeremy L Thompson **/
714b94338b9SJed Brown static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedSize *num_entries) {
715b275c451SJeremy L Thompson   bool                is_composite;
716eaf62fffSJeremy L Thompson   CeedElemRestriction rstr;
717eaf62fffSJeremy L Thompson   CeedInt             num_elem, elem_size, num_comp;
718eaf62fffSJeremy L Thompson 
719b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
7206574a04fSJeremy L Thompson   CeedCheck(!is_composite, op->ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
7212b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &rstr));
7222b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
7232b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
7242b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
725b94338b9SJed Brown   *num_entries = (CeedSize)elem_size * num_comp * elem_size * num_comp * num_elem;
726eaf62fffSJeremy L Thompson 
727eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
728eaf62fffSJeremy L Thompson }
729eaf62fffSJeremy L Thompson 
730eaf62fffSJeremy L Thompson /**
731ea61e9acSJeremy L Thompson   @brief Common code for creating a multigrid coarse operator and level transfer operators for a CeedOperator
732eaf62fffSJeremy L Thompson 
733eaf62fffSJeremy L Thompson   @param[in]  op_fine      Fine grid operator
73485bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
735eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid restriction
736eaf62fffSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector basis
73785bb9dcfSJeremy L Thompson   @param[in]  basis_c_to_f Basis for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
738eaf62fffSJeremy L Thompson   @param[out] op_coarse    Coarse grid operator
73985bb9dcfSJeremy L Thompson   @param[out] op_prolong   Coarse to fine operator, or NULL
74085bb9dcfSJeremy L Thompson   @param[out] op_restrict  Fine to coarse operator, or NULL
741eaf62fffSJeremy L Thompson 
742eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
743eaf62fffSJeremy L Thompson 
744eaf62fffSJeremy L Thompson   @ref Developer
745eaf62fffSJeremy L Thompson **/
7462b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
7472b730f8bSJeremy L Thompson                                             CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
748eaf62fffSJeremy L Thompson   Ceed                ceed;
74985bb9dcfSJeremy L Thompson   CeedVector          mult_vec         = NULL;
750c17ec2beSJeremy L Thompson   CeedElemRestriction rstr_p_mult_fine = NULL;
7512b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
752eaf62fffSJeremy L Thompson 
753eaf62fffSJeremy L Thompson   // Check for composite operator
754eaf62fffSJeremy L Thompson   bool is_composite;
7552b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op_fine, &is_composite));
7566574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported");
757eaf62fffSJeremy L Thompson 
758eaf62fffSJeremy L Thompson   // Coarse Grid
7592b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
760eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_fine = NULL;
761eaf62fffSJeremy L Thompson   // -- Clone input fields
76292ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < op_fine->qf->num_input_fields; i++) {
763eaf62fffSJeremy L Thompson     if (op_fine->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
764437c7c90SJeremy L Thompson       rstr_fine = op_fine->input_fields[i]->elem_rstr;
7652b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE));
766eaf62fffSJeremy L Thompson     } else {
767437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, op_fine->input_fields[i]->elem_rstr,
7682b730f8bSJeremy L Thompson                                     op_fine->input_fields[i]->basis, op_fine->input_fields[i]->vec));
769eaf62fffSJeremy L Thompson     }
770eaf62fffSJeremy L Thompson   }
771eaf62fffSJeremy L Thompson   // -- Clone output fields
77292ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < op_fine->qf->num_output_fields; i++) {
773eaf62fffSJeremy L Thompson     if (op_fine->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
7742b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE));
775eaf62fffSJeremy L Thompson     } else {
776437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, op_fine->output_fields[i]->elem_rstr,
7772b730f8bSJeremy L Thompson                                     op_fine->output_fields[i]->basis, op_fine->output_fields[i]->vec));
778eaf62fffSJeremy L Thompson     }
779eaf62fffSJeremy L Thompson   }
780af99e877SJeremy L Thompson   // -- Clone QFunctionAssemblyData
7812b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op_fine->qf_assembled, &(*op_coarse)->qf_assembled));
782eaf62fffSJeremy L Thompson 
783eaf62fffSJeremy L Thompson   // Multiplicity vector
78485bb9dcfSJeremy L Thompson   if (op_restrict || op_prolong) {
785*7c1dbaffSSebastian Grimberg     CeedRestrictionType rstr_type;
78685bb9dcfSJeremy L Thompson     CeedVector          mult_e_vec;
78785bb9dcfSJeremy L Thompson 
788*7c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr_fine, &rstr_type));
789*7c1dbaffSSebastian Grimberg     CeedCheck(rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_UNSUPPORTED,
790*7c1dbaffSSebastian Grimberg               "Element restrictions created with CeedElemRestrictionCreateCurlOriented are not supported");
7916574a04fSJeremy L Thompson     CeedCheck(p_mult_fine, ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector");
792*7c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnsignedCopy(rstr_fine, &rstr_p_mult_fine));
7932b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec));
7942b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_e_vec, 0.0));
795c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE));
7962b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_vec, 0.0));
797c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE));
7982b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&mult_e_vec));
7992b730f8bSJeremy L Thompson     CeedCall(CeedVectorReciprocal(mult_vec));
80085bb9dcfSJeremy L Thompson   }
801eaf62fffSJeremy L Thompson 
802addd79feSZach Atkins   // Clone name
803addd79feSZach Atkins   bool   has_name = op_fine->name;
804addd79feSZach Atkins   size_t name_len = op_fine->name ? strlen(op_fine->name) : 0;
805addd79feSZach Atkins   CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name));
806addd79feSZach Atkins 
80783d6adf3SZach Atkins   // Check that coarse to fine basis is provided if prolong/restrict operators are requested
8086574a04fSJeremy L Thompson   CeedCheck(basis_c_to_f || (!op_restrict && !op_prolong), ceed, CEED_ERROR_INCOMPATIBLE,
8096574a04fSJeremy L Thompson             "Prolongation or restriction operator creation requires coarse-to-fine basis");
81083d6adf3SZach Atkins 
81185bb9dcfSJeremy L Thompson   // Restriction/Prolongation Operators
812eaf62fffSJeremy L Thompson   CeedInt num_comp;
8132b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp));
814addd79feSZach Atkins 
815addd79feSZach Atkins   // Restriction
816addd79feSZach Atkins   if (op_restrict) {
817eaf62fffSJeremy L Thompson     CeedInt             *num_comp_r_data;
81885bb9dcfSJeremy L Thompson     CeedQFunction        qf_restrict;
81985bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_r;
82085bb9dcfSJeremy L Thompson 
82185bb9dcfSJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict));
8222b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_r_data));
823eaf62fffSJeremy L Thompson     num_comp_r_data[0] = num_comp;
8242b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r));
8252b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data));
8262b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r));
8272b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_r));
8282b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE));
8292b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE));
8302b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP));
8312b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp));
832eaf62fffSJeremy L Thompson 
8332b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict));
8342b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE));
835c17ec2beSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_p_mult_fine, CEED_BASIS_COLLOCATED, mult_vec));
8362b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
837eaf62fffSJeremy L Thompson 
838addd79feSZach Atkins     // Set name
839addd79feSZach Atkins     char *restriction_name;
840addd79feSZach Atkins     CeedCall(CeedCalloc(17 + name_len, &restriction_name));
841addd79feSZach Atkins     sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
842addd79feSZach Atkins     CeedCall(CeedOperatorSetName(*op_restrict, restriction_name));
843addd79feSZach Atkins     CeedCall(CeedFree(&restriction_name));
844addd79feSZach Atkins 
845addd79feSZach Atkins     // Check
846addd79feSZach Atkins     CeedCall(CeedOperatorCheckReady(*op_restrict));
847addd79feSZach Atkins 
848addd79feSZach Atkins     // Cleanup
849addd79feSZach Atkins     CeedCall(CeedQFunctionDestroy(&qf_restrict));
850addd79feSZach Atkins   }
851addd79feSZach Atkins 
852eaf62fffSJeremy L Thompson   // Prolongation
853addd79feSZach Atkins   if (op_prolong) {
854eaf62fffSJeremy L Thompson     CeedInt             *num_comp_p_data;
85585bb9dcfSJeremy L Thompson     CeedQFunction        qf_prolong;
85685bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_p;
85785bb9dcfSJeremy L Thompson 
85885bb9dcfSJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong));
8592b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_p_data));
860eaf62fffSJeremy L Thompson     num_comp_p_data[0] = num_comp;
8612b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p));
8622b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data));
8632b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p));
8642b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_p));
8652b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP));
8662b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE));
8672b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE));
8682b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp));
869eaf62fffSJeremy L Thompson 
8702b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong));
8712b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
872c17ec2beSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_p_mult_fine, CEED_BASIS_COLLOCATED, mult_vec));
8732b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE));
874eaf62fffSJeremy L Thompson 
875addd79feSZach Atkins     // Set name
876ea6b5821SJeremy L Thompson     char *prolongation_name;
8772b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(18 + name_len, &prolongation_name));
8782b730f8bSJeremy L Thompson     sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
8792b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name));
8802b730f8bSJeremy L Thompson     CeedCall(CeedFree(&prolongation_name));
881addd79feSZach Atkins 
882addd79feSZach Atkins     // Check
883addd79feSZach Atkins     CeedCall(CeedOperatorCheckReady(*op_prolong));
884addd79feSZach Atkins 
885addd79feSZach Atkins     // Cleanup
886addd79feSZach Atkins     CeedCall(CeedQFunctionDestroy(&qf_prolong));
887ea6b5821SJeremy L Thompson   }
888ea6b5821SJeremy L Thompson 
88958e4b056SJeremy L Thompson   // Check
89058e4b056SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(*op_coarse));
89158e4b056SJeremy L Thompson 
892eaf62fffSJeremy L Thompson   // Cleanup
8932b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_vec));
894c17ec2beSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine));
8952b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis_c_to_f));
896805fe78eSJeremy L Thompson 
897eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
898eaf62fffSJeremy L Thompson }
899eaf62fffSJeremy L Thompson 
900eaf62fffSJeremy L Thompson /**
901eaf62fffSJeremy L Thompson   @brief Build 1D mass matrix and Laplacian with perturbation
902eaf62fffSJeremy L Thompson 
903eaf62fffSJeremy L Thompson   @param[in]  interp_1d   Interpolation matrix in one dimension
904eaf62fffSJeremy L Thompson   @param[in]  grad_1d     Gradient matrix in one dimension
905eaf62fffSJeremy L Thompson   @param[in]  q_weight_1d Quadrature weights in one dimension
906eaf62fffSJeremy L Thompson   @param[in]  P_1d        Number of basis nodes in one dimension
907eaf62fffSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
908eaf62fffSJeremy L Thompson   @param[in]  dim         Dimension of basis
909eaf62fffSJeremy L Thompson   @param[out] mass        Assembled mass matrix in one dimension
910eaf62fffSJeremy L Thompson   @param[out] laplace     Assembled perturbed Laplacian in one dimension
911eaf62fffSJeremy L Thompson 
912eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
913eaf62fffSJeremy L Thompson 
914eaf62fffSJeremy L Thompson   @ref Developer
915eaf62fffSJeremy L Thompson **/
9162c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
9172c2ea1dbSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d, CeedInt P_1d, CeedInt Q_1d,
9182c2ea1dbSJeremy L Thompson                                 CeedInt dim, CeedScalar *mass, CeedScalar *laplace) {
9192b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
920eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
921eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
9222b730f8bSJeremy 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];
923eaf62fffSJeremy L Thompson       mass[i + j * P_1d] = sum;
924eaf62fffSJeremy L Thompson     }
9252b730f8bSJeremy L Thompson   }
926eaf62fffSJeremy L Thompson   // -- Laplacian
9272b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
928eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
929eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
9302b730f8bSJeremy 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];
931eaf62fffSJeremy L Thompson       laplace[i + j * P_1d] = sum;
932eaf62fffSJeremy L Thompson     }
9332b730f8bSJeremy L Thompson   }
934eaf62fffSJeremy L Thompson   CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4;
9352b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation;
936eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
937eaf62fffSJeremy L Thompson }
9382c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
939eaf62fffSJeremy L Thompson 
940eaf62fffSJeremy L Thompson /// @}
941eaf62fffSJeremy L Thompson 
942eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
943480fae85SJeremy L Thompson /// CeedOperator Backend API
944480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
945480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend
946480fae85SJeremy L Thompson /// @{
947480fae85SJeremy L Thompson 
948480fae85SJeremy L Thompson /**
949480fae85SJeremy L Thompson   @brief Create object holding CeedQFunction assembly data for CeedOperator
950480fae85SJeremy L Thompson 
951480fae85SJeremy L Thompson   @param[in]  ceed A Ceed object where the CeedQFunctionAssemblyData will be created
952ea61e9acSJeremy L Thompson   @param[out] data Address of the variable where the newly created CeedQFunctionAssemblyData will be stored
953480fae85SJeremy L Thompson 
954480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
955480fae85SJeremy L Thompson 
956480fae85SJeremy L Thompson   @ref Backend
957480fae85SJeremy L Thompson **/
958ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) {
9592b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
960480fae85SJeremy L Thompson   (*data)->ref_count = 1;
961480fae85SJeremy L Thompson   (*data)->ceed      = ceed;
9622b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
963480fae85SJeremy L Thompson 
964480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
965480fae85SJeremy L Thompson }
966480fae85SJeremy L Thompson 
967480fae85SJeremy L Thompson /**
968480fae85SJeremy L Thompson   @brief Increment the reference counter for a CeedQFunctionAssemblyData
969480fae85SJeremy L Thompson 
970ea61e9acSJeremy L Thompson   @param[in,out] data CeedQFunctionAssemblyData to increment the reference counter
971480fae85SJeremy L Thompson 
972480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
973480fae85SJeremy L Thompson 
974480fae85SJeremy L Thompson   @ref Backend
975480fae85SJeremy L Thompson **/
976480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) {
977480fae85SJeremy L Thompson   data->ref_count++;
978480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
979480fae85SJeremy L Thompson }
980480fae85SJeremy L Thompson 
981480fae85SJeremy L Thompson /**
982beecbf24SJeremy L Thompson   @brief Set re-use of CeedQFunctionAssemblyData
9838b919e6bSJeremy L Thompson 
984ea61e9acSJeremy L Thompson   @param[in,out] data       CeedQFunctionAssemblyData to mark for reuse
985ea61e9acSJeremy L Thompson   @param[in]     reuse_data Boolean flag indicating data re-use
9868b919e6bSJeremy L Thompson 
9878b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9888b919e6bSJeremy L Thompson 
9898b919e6bSJeremy L Thompson   @ref Backend
9908b919e6bSJeremy L Thompson **/
9912b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) {
992beecbf24SJeremy L Thompson   data->reuse_data        = reuse_data;
993beecbf24SJeremy L Thompson   data->needs_data_update = true;
994beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
995beecbf24SJeremy L Thompson }
996beecbf24SJeremy L Thompson 
997beecbf24SJeremy L Thompson /**
998beecbf24SJeremy L Thompson   @brief Mark QFunctionAssemblyData as stale
999beecbf24SJeremy L Thompson 
1000ea61e9acSJeremy L Thompson   @param[in,out] data              CeedQFunctionAssemblyData to mark as stale
1001ea61e9acSJeremy L Thompson   @param[in]     needs_data_update Boolean flag indicating if update is needed or completed
1002beecbf24SJeremy L Thompson 
1003beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1004beecbf24SJeremy L Thompson 
1005beecbf24SJeremy L Thompson   @ref Backend
1006beecbf24SJeremy L Thompson **/
10072b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) {
1008beecbf24SJeremy L Thompson   data->needs_data_update = needs_data_update;
10098b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
10108b919e6bSJeremy L Thompson }
10118b919e6bSJeremy L Thompson 
10128b919e6bSJeremy L Thompson /**
10138b919e6bSJeremy L Thompson   @brief Determine if QFunctionAssemblyData needs update
10148b919e6bSJeremy L Thompson 
10158b919e6bSJeremy L Thompson   @param[in]  data             CeedQFunctionAssemblyData to mark as stale
10168b919e6bSJeremy L Thompson   @param[out] is_update_needed Boolean flag indicating if re-assembly is required
10178b919e6bSJeremy L Thompson 
10188b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
10198b919e6bSJeremy L Thompson 
10208b919e6bSJeremy L Thompson   @ref Backend
10218b919e6bSJeremy L Thompson **/
10222b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) {
1023beecbf24SJeremy L Thompson   *is_update_needed = !data->reuse_data || data->needs_data_update;
10248b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
10258b919e6bSJeremy L Thompson }
10268b919e6bSJeremy L Thompson 
10278b919e6bSJeremy L Thompson /**
1028ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedQFunctionAssemblyData.
10294385fb7fSSebastian Grimberg 
1030ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedCeedQFunctionAssemblyDataDestroy()`.
1031512bb800SJeremy L Thompson 
1032512bb800SJeremy 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
1033512bb800SJeremy L Thompson         CeedQFunctionAssemblyData. This CeedQFunctionAssemblyData will be destroyed if `data_copy` is the only reference to this
1034512bb800SJeremy L Thompson         CeedQFunctionAssemblyData.
1035480fae85SJeremy L Thompson 
1036ea61e9acSJeremy L Thompson   @param[in]     data      CeedQFunctionAssemblyData to copy reference to
1037ea61e9acSJeremy L Thompson   @param[in,out] data_copy Variable to store copied reference
1038480fae85SJeremy L Thompson 
1039480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1040480fae85SJeremy L Thompson 
1041480fae85SJeremy L Thompson   @ref Backend
1042480fae85SJeremy L Thompson **/
10432b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) {
10442b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReference(data));
10452b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy));
1046480fae85SJeremy L Thompson   *data_copy = data;
1047480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1048480fae85SJeremy L Thompson }
1049480fae85SJeremy L Thompson 
1050480fae85SJeremy L Thompson /**
1051480fae85SJeremy L Thompson   @brief Get setup status for internal objects for CeedQFunctionAssemblyData
1052480fae85SJeremy L Thompson 
1053ea61e9acSJeremy L Thompson   @param[in]  data     CeedQFunctionAssemblyData to retrieve status
1054480fae85SJeremy L Thompson   @param[out] is_setup Boolean flag for setup status
1055480fae85SJeremy L Thompson 
1056480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1057480fae85SJeremy L Thompson 
1058480fae85SJeremy L Thompson   @ref Backend
1059480fae85SJeremy L Thompson **/
10602b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) {
1061480fae85SJeremy L Thompson   *is_setup = data->is_setup;
1062480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1063480fae85SJeremy L Thompson }
1064480fae85SJeremy L Thompson 
1065480fae85SJeremy L Thompson /**
1066480fae85SJeremy L Thompson   @brief Set internal objects for CeedQFunctionAssemblyData
1067480fae85SJeremy L Thompson 
1068ea61e9acSJeremy L Thompson   @param[in,out] data CeedQFunctionAssemblyData to set objects
1069480fae85SJeremy L Thompson   @param[in]     vec  CeedVector to store assembled CeedQFunction at quadrature points
1070480fae85SJeremy L Thompson   @param[in]     rstr CeedElemRestriction for CeedVector containing assembled CeedQFunction
1071480fae85SJeremy L Thompson 
1072480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1073480fae85SJeremy L Thompson 
1074480fae85SJeremy L Thompson   @ref Backend
1075480fae85SJeremy L Thompson **/
10762b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) {
10772b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(vec, &data->vec));
10782b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr));
1079480fae85SJeremy L Thompson 
1080480fae85SJeremy L Thompson   data->is_setup = true;
1081480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1082480fae85SJeremy L Thompson }
1083480fae85SJeremy L Thompson 
10842b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) {
10856574a04fSJeremy L Thompson   CeedCheck(data->is_setup, data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first.");
1086480fae85SJeremy L Thompson 
10872b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(data->vec, vec));
10882b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr));
1089480fae85SJeremy L Thompson 
1090480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1091480fae85SJeremy L Thompson }
1092480fae85SJeremy L Thompson 
1093480fae85SJeremy L Thompson /**
1094480fae85SJeremy L Thompson   @brief Destroy CeedQFunctionAssemblyData
1095480fae85SJeremy L Thompson 
1096ea61e9acSJeremy L Thompson   @param[in,out] data  CeedQFunctionAssemblyData to destroy
1097480fae85SJeremy L Thompson 
1098480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1099480fae85SJeremy L Thompson 
1100480fae85SJeremy L Thompson   @ref Backend
1101480fae85SJeremy L Thompson **/
1102480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) {
1103ad6481ceSJeremy L Thompson   if (!*data || --(*data)->ref_count > 0) {
1104ad6481ceSJeremy L Thompson     *data = NULL;
1105ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1106ad6481ceSJeremy L Thompson   }
11072b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
11082b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*data)->vec));
11092b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr));
1110480fae85SJeremy L Thompson 
11112b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1112480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1113480fae85SJeremy L Thompson }
1114480fae85SJeremy L Thompson 
1115ed9e99e6SJeremy L Thompson /**
1116ed9e99e6SJeremy L Thompson   @brief Get CeedOperatorAssemblyData
1117ed9e99e6SJeremy L Thompson 
1118ed9e99e6SJeremy L Thompson   @param[in]  op   CeedOperator to assemble
1119ed9e99e6SJeremy L Thompson   @param[out] data CeedQFunctionAssemblyData
1120ed9e99e6SJeremy L Thompson 
1121ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1122ed9e99e6SJeremy L Thompson 
1123ed9e99e6SJeremy L Thompson   @ref Backend
1124ed9e99e6SJeremy L Thompson **/
11252b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) {
1126ed9e99e6SJeremy L Thompson   if (!op->op_assembled) {
1127ed9e99e6SJeremy L Thompson     CeedOperatorAssemblyData data;
1128ed9e99e6SJeremy L Thompson 
11292b730f8bSJeremy L Thompson     CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data));
1130ed9e99e6SJeremy L Thompson     op->op_assembled = data;
1131ed9e99e6SJeremy L Thompson   }
1132ed9e99e6SJeremy L Thompson   *data = op->op_assembled;
1133ed9e99e6SJeremy L Thompson 
1134ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1135ed9e99e6SJeremy L Thompson }
1136ed9e99e6SJeremy L Thompson 
1137ed9e99e6SJeremy L Thompson /**
1138ba746a46SJeremy L Thompson   @brief Create object holding CeedOperator assembly data.
1139ba746a46SJeremy L Thompson 
1140ba746a46SJeremy L Thompson   The CeedOperatorAssemblyData holds an array with references to every active CeedBasis used in the CeedOperator.
1141ba746a46SJeremy L Thompson   An array with references to the corresponding active CeedElemRestrictions is also stored.
1142ba746a46SJeremy L Thompson   For each active CeedBasis, the CeedOperatorAssemblyData holds an array of all input and output CeedEvalModes for this CeedBasis.
1143ba746a46SJeremy L Thompson   The CeedOperatorAssemblyData holds an array of offsets for indexing into the assembled CeedQFunction arrays to the row representing each
1144ba746a46SJeremy L Thompson CeedEvalMode.
1145ba746a46SJeremy L Thompson   The number of input columns across all active bases for the assembled CeedQFunction is also stored.
1146ba746a46SJeremy L Thompson   Lastly, the CeedOperatorAssembly data holds assembled matrices representing the full action of the CeedBasis for all CeedEvalModes.
1147ed9e99e6SJeremy L Thompson 
1148ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperatorAssemblyData will be created
1149ed9e99e6SJeremy L Thompson   @param[in]  op   CeedOperator to be assembled
1150ea61e9acSJeremy L Thompson   @param[out] data Address of the variable where the newly created CeedOperatorAssemblyData will be stored
1151ed9e99e6SJeremy L Thompson 
1152ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1153ed9e99e6SJeremy L Thompson 
1154ed9e99e6SJeremy L Thompson   @ref Backend
1155ed9e99e6SJeremy L Thompson **/
11562b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) {
1157437c7c90SJeremy L Thompson   CeedInt num_active_bases = 0;
1158437c7c90SJeremy L Thompson 
1159437c7c90SJeremy L Thompson   // Allocate
11602b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1161ed9e99e6SJeremy L Thompson   (*data)->ceed = ceed;
11622b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1163ed9e99e6SJeremy L Thompson 
1164ed9e99e6SJeremy L Thompson   // Build OperatorAssembly data
1165ed9e99e6SJeremy L Thompson   CeedQFunction       qf;
1166ed9e99e6SJeremy L Thompson   CeedQFunctionField *qf_fields;
1167ed9e99e6SJeremy L Thompson   CeedOperatorField  *op_fields;
1168ed9e99e6SJeremy L Thompson   CeedInt             num_input_fields;
11692b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
11702b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL));
11712b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1172ed9e99e6SJeremy L Thompson 
1173ed9e99e6SJeremy L Thompson   // Determine active input basis
1174437c7c90SJeremy L Thompson   CeedInt       *num_eval_modes_in = NULL, *num_eval_modes_out = NULL, offset = 0;
1175437c7c90SJeremy L Thompson   CeedEvalMode **eval_modes_in = NULL, **eval_modes_out = NULL;
1176437c7c90SJeremy L Thompson   CeedSize     **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL;
1177ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1178ed9e99e6SJeremy L Thompson     CeedVector vec;
11792b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1180ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
1181437c7c90SJeremy L Thompson       CeedBasis    basis_in = NULL;
1182437c7c90SJeremy L Thompson       CeedEvalMode eval_mode;
1183*7c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
11842b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
11852b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1186352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp));
1187352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1188437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < num_active_bases; i++) {
1189437c7c90SJeremy L Thompson         if ((*data)->active_bases[i] == basis_in) index = i;
1190437c7c90SJeremy L Thompson       }
1191437c7c90SJeremy L Thompson       if (index == -1) {
1192437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_in;
1193437c7c90SJeremy L Thompson         index = num_active_bases;
1194437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_bases));
1195437c7c90SJeremy L Thompson         (*data)->active_bases[num_active_bases] = NULL;
1196437c7c90SJeremy L Thompson         CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases[num_active_bases]));
1197437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_elem_rstrs));
1198437c7c90SJeremy L Thompson         (*data)->active_elem_rstrs[num_active_bases] = NULL;
1199437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in));
1200437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs[num_active_bases]));
1201437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_in));
1202437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_out));
1203437c7c90SJeremy L Thompson         num_eval_modes_in[index]  = 0;
1204437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1205437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_in));
1206437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_out));
1207437c7c90SJeremy L Thompson         eval_modes_in[index]  = NULL;
1208437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1209437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_in));
1210437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_out));
1211437c7c90SJeremy L Thompson         eval_mode_offsets_in[index]  = NULL;
1212437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1213437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_in));
1214437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_out));
1215437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index]  = NULL;
1216437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1217437c7c90SJeremy L Thompson         num_active_bases++;
1218437c7c90SJeremy L Thompson       }
1219352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1220352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1221352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_modes_in[index]));
1222352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_mode_offsets_in[index]));
1223352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1224437c7c90SJeremy L Thompson           eval_modes_in[index][num_eval_modes_in[index] + d]        = eval_mode;
1225437c7c90SJeremy L Thompson           eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset;
1226352a5e7cSSebastian Grimberg           offset += num_comp;
1227ed9e99e6SJeremy L Thompson         }
1228352a5e7cSSebastian Grimberg         num_eval_modes_in[index] += q_comp;
1229ed9e99e6SJeremy L Thompson       }
1230ed9e99e6SJeremy L Thompson     }
1231ed9e99e6SJeremy L Thompson   }
1232437c7c90SJeremy L Thompson   (*data)->num_eval_modes_in    = num_eval_modes_in;
1233437c7c90SJeremy L Thompson   (*data)->eval_modes_in        = eval_modes_in;
1234437c7c90SJeremy L Thompson   (*data)->eval_mode_offsets_in = eval_mode_offsets_in;
1235ed9e99e6SJeremy L Thompson 
1236ed9e99e6SJeremy L Thompson   // Determine active output basis
1237ed9e99e6SJeremy L Thompson   CeedInt num_output_fields;
12382b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields));
12392b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1240437c7c90SJeremy L Thompson   offset = 0;
1241ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1242ed9e99e6SJeremy L Thompson     CeedVector vec;
12432b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1244ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
1245437c7c90SJeremy L Thompson       CeedBasis    basis_out = NULL;
1246ed9e99e6SJeremy L Thompson       CeedEvalMode eval_mode;
1247*7c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
1248437c7c90SJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
12492b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1250352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_out, &num_comp));
1251352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1252437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < num_active_bases; i++) {
1253437c7c90SJeremy L Thompson         if ((*data)->active_bases[i] == basis_out) index = i;
1254437c7c90SJeremy L Thompson       }
1255437c7c90SJeremy L Thompson       if (index == -1) {
1256437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_out;
1257437c7c90SJeremy L Thompson         index = num_active_bases;
1258437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_bases));
1259437c7c90SJeremy L Thompson         (*data)->active_bases[num_active_bases] = NULL;
1260437c7c90SJeremy L Thompson         CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases[num_active_bases]));
1261437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_elem_rstrs));
1262437c7c90SJeremy L Thompson         (*data)->active_elem_rstrs[num_active_bases] = NULL;
1263437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out));
1264437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs[num_active_bases]));
1265437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_in));
1266437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_out));
1267437c7c90SJeremy L Thompson         num_eval_modes_in[index]  = 0;
1268437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1269437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_in));
1270437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_out));
1271437c7c90SJeremy L Thompson         eval_modes_in[index]  = NULL;
1272437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1273437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_in));
1274437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_out));
1275437c7c90SJeremy L Thompson         eval_mode_offsets_in[index]  = NULL;
1276437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1277437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_in));
1278437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_out));
1279437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index]  = NULL;
1280437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1281437c7c90SJeremy L Thompson         num_active_bases++;
1282437c7c90SJeremy L Thompson       }
1283352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1284352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1285352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_modes_out[index]));
1286352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_mode_offsets_out[index]));
1287352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1288437c7c90SJeremy L Thompson           eval_modes_out[index][num_eval_modes_out[index] + d]        = eval_mode;
1289437c7c90SJeremy L Thompson           eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset;
1290352a5e7cSSebastian Grimberg           offset += num_comp;
1291ed9e99e6SJeremy L Thompson         }
1292352a5e7cSSebastian Grimberg         num_eval_modes_out[index] += q_comp;
1293ed9e99e6SJeremy L Thompson       }
1294ed9e99e6SJeremy L Thompson     }
1295ed9e99e6SJeremy L Thompson   }
1296437c7c90SJeremy L Thompson   (*data)->num_output_components = offset;
1297437c7c90SJeremy L Thompson   (*data)->num_eval_modes_out    = num_eval_modes_out;
1298437c7c90SJeremy L Thompson   (*data)->eval_modes_out        = eval_modes_out;
1299437c7c90SJeremy L Thompson   (*data)->eval_mode_offsets_out = eval_mode_offsets_out;
1300437c7c90SJeremy L Thompson   (*data)->num_active_bases      = num_active_bases;
1301ed9e99e6SJeremy L Thompson 
1302ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1303ed9e99e6SJeremy L Thompson }
1304ed9e99e6SJeremy L Thompson 
1305ed9e99e6SJeremy L Thompson /**
1306ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedEvalModes for assembly.
1307ba746a46SJeremy L Thompson 
1308ba746a46SJeremy L Thompson   Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1309ed9e99e6SJeremy L Thompson 
1310ed9e99e6SJeremy L Thompson   @param[in]  data                  CeedOperatorAssemblyData
1311ba746a46SJeremy L Thompson   @param[out] num_active_bases      Total number of active bases
1312c5d0f995SJed Brown   @param[out] num_eval_modes_in     Pointer to hold array of numbers of input CeedEvalModes, or NULL.
1313ba746a46SJeremy L Thompson                                       `eval_modes_in[0]` holds an array of eval modes for the first active basis.
1314c5d0f995SJed Brown   @param[out] eval_modes_in         Pointer to hold arrays of input CeedEvalModes, or NULL.
1315ba746a46SJeremy L Thompson   @param[out] eval_mode_offsets_in  Pointer to hold arrays of input offsets at each quadrature point.
1316c5d0f995SJed Brown   @param[out] num_eval_modes_out    Pointer to hold array of numbers of output CeedEvalModes, or NULL
1317c5d0f995SJed Brown   @param[out] eval_modes_out        Pointer to hold arrays of output CeedEvalModes, or NULL.
1318437c7c90SJeremy L Thompson   @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point
1319ba746a46SJeremy L Thompson   @param[out] num_output_components The number of columns in the assembled CeedQFunction matrix for each quadrature point,
1320ba746a46SJeremy L Thompson                                       including contributions of all active bases
1321ed9e99e6SJeremy L Thompson 
1322ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1323ed9e99e6SJeremy L Thompson 
1324c5d0f995SJed Brown 
1325ed9e99e6SJeremy L Thompson   @ref Backend
1326ed9e99e6SJeremy L Thompson **/
1327437c7c90SJeremy L Thompson int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases, CeedInt **num_eval_modes_in,
1328437c7c90SJeremy L Thompson                                          const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt **num_eval_modes_out,
1329437c7c90SJeremy L Thompson                                          const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out, CeedSize *num_output_components) {
1330437c7c90SJeremy L Thompson   if (num_active_bases) *num_active_bases = data->num_active_bases;
1331437c7c90SJeremy L Thompson   if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in;
1332437c7c90SJeremy L Thompson   if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in;
1333437c7c90SJeremy L Thompson   if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in;
1334437c7c90SJeremy L Thompson   if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out;
1335437c7c90SJeremy L Thompson   if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out;
1336437c7c90SJeremy L Thompson   if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out;
1337437c7c90SJeremy L Thompson   if (num_output_components) *num_output_components = data->num_output_components;
1338ed9e99e6SJeremy L Thompson 
1339ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1340ed9e99e6SJeremy L Thompson }
1341ed9e99e6SJeremy L Thompson 
1342ed9e99e6SJeremy L Thompson /**
1343ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedBasis data for assembly.
1344ba746a46SJeremy L Thompson 
1345ba746a46SJeremy L Thompson   Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1346ed9e99e6SJeremy L Thompson 
1347ed9e99e6SJeremy L Thompson   @param[in]  data                CeedOperatorAssemblyData
1348437c7c90SJeremy L Thompson   @param[out] num_active_bases    Number of active bases, or NULL
1349437c7c90SJeremy L Thompson   @param[out] active_bases        Pointer to hold active CeedBasis, or NULL
1350437c7c90SJeremy L Thompson   @param[out] assembled_bases_in  Pointer to hold assembled active input B, or NULL
1351437c7c90SJeremy L Thompson   @param[out] assembled_bases_out Pointer to hold assembled active output B, or NULL
1352ed9e99e6SJeremy L Thompson 
1353ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1354ed9e99e6SJeremy L Thompson 
1355ed9e99e6SJeremy L Thompson   @ref Backend
1356ed9e99e6SJeremy L Thompson **/
1357437c7c90SJeremy L Thompson int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases, CeedBasis **active_bases,
1358437c7c90SJeremy L Thompson                                      const CeedScalar ***assembled_bases_in, const CeedScalar ***assembled_bases_out) {
1359ed9e99e6SJeremy L Thompson   // Assemble B_in, B_out if needed
1360437c7c90SJeremy L Thompson   if (assembled_bases_in && !data->assembled_bases_in[0]) {
1361437c7c90SJeremy L Thompson     CeedInt num_qpts;
1362437c7c90SJeremy L Thompson 
1363437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases[0], &num_qpts));
1364437c7c90SJeremy L Thompson     for (CeedInt b = 0; b < data->num_active_bases; b++) {
1365352a5e7cSSebastian Grimberg       CeedInt     num_nodes;
1366437c7c90SJeremy L Thompson       CeedScalar *B_in = NULL, *identity = NULL;
1367ed9e99e6SJeremy L Thompson       bool        has_eval_none = false;
1368ed9e99e6SJeremy L Thompson 
1369352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumNodes(data->active_bases[b], &num_nodes));
1370352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_in[b], &B_in));
1371ed9e99e6SJeremy L Thompson 
1372437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) {
1373437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE);
1374ed9e99e6SJeremy L Thompson       }
1375ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1376352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1377352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1378352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1379ed9e99e6SJeremy L Thompson         }
1380ed9e99e6SJeremy L Thompson       }
1381ed9e99e6SJeremy L Thompson 
1382ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1383352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1384352a5e7cSSebastian Grimberg           CeedInt      d_in              = 0, q_comp_in;
1385352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE;
1386437c7c90SJeremy L Thompson           for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) {
1387437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_in[b] * q;
1388437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
1389352a5e7cSSebastian Grimberg             CeedOperatorGetBasisPointer(data->active_bases[b], data->eval_modes_in[b][e_in], identity, &B);
1390352a5e7cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases[b], data->eval_modes_in[b][e_in], &q_comp_in));
1391352a5e7cSSebastian Grimberg             if (q_comp_in > 1) {
1392352a5e7cSSebastian Grimberg               if (e_in == 0 || data->eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0;
1393352a5e7cSSebastian Grimberg               else B = &B[(++d_in) * num_qpts * num_nodes];
1394352a5e7cSSebastian Grimberg             }
1395352a5e7cSSebastian Grimberg             eval_mode_in_prev                 = data->eval_modes_in[b][e_in];
1396352a5e7cSSebastian Grimberg             B_in[(qq + e_in) * num_nodes + n] = B[q * num_nodes + n];
1397ed9e99e6SJeremy L Thompson           }
1398ed9e99e6SJeremy L Thompson         }
1399ed9e99e6SJeremy L Thompson       }
1400*7c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1401437c7c90SJeremy L Thompson       data->assembled_bases_in[b] = B_in;
1402437c7c90SJeremy L Thompson     }
1403ed9e99e6SJeremy L Thompson   }
1404ed9e99e6SJeremy L Thompson 
1405437c7c90SJeremy L Thompson   if (assembled_bases_out && !data->assembled_bases_out[0]) {
1406437c7c90SJeremy L Thompson     CeedInt num_qpts;
1407437c7c90SJeremy L Thompson 
1408437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases[0], &num_qpts));
1409437c7c90SJeremy L Thompson     for (CeedInt b = 0; b < data->num_active_bases; b++) {
1410352a5e7cSSebastian Grimberg       CeedInt     num_nodes;
1411ed9e99e6SJeremy L Thompson       bool        has_eval_none = false;
1412437c7c90SJeremy L Thompson       CeedScalar *B_out = NULL, *identity = NULL;
1413ed9e99e6SJeremy L Thompson 
1414352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumNodes(data->active_bases[b], &num_nodes));
1415352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_out[b], &B_out));
1416ed9e99e6SJeremy L Thompson 
1417437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) {
1418437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE);
1419ed9e99e6SJeremy L Thompson       }
1420ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1421352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1422352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1423352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1424ed9e99e6SJeremy L Thompson         }
1425ed9e99e6SJeremy L Thompson       }
1426ed9e99e6SJeremy L Thompson 
1427ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1428352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1429352a5e7cSSebastian Grimberg           CeedInt      d_out              = 0, q_comp_out;
1430352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
1431437c7c90SJeremy L Thompson           for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) {
1432437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_out[b] * q;
1433437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
1434352a5e7cSSebastian Grimberg             CeedOperatorGetBasisPointer(data->active_bases[b], data->eval_modes_out[b][e_out], identity, &B);
1435352a5e7cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases[b], data->eval_modes_out[b][e_out], &q_comp_out));
1436352a5e7cSSebastian Grimberg             if (q_comp_out > 1) {
1437352a5e7cSSebastian Grimberg               if (e_out == 0 || data->eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0;
1438352a5e7cSSebastian Grimberg               else B = &B[(++d_out) * num_qpts * num_nodes];
1439352a5e7cSSebastian Grimberg             }
1440352a5e7cSSebastian Grimberg             eval_mode_out_prev                  = data->eval_modes_out[b][e_out];
1441352a5e7cSSebastian Grimberg             B_out[(qq + e_out) * num_nodes + n] = B[q * num_nodes + n];
1442ed9e99e6SJeremy L Thompson           }
1443ed9e99e6SJeremy L Thompson         }
1444ed9e99e6SJeremy L Thompson       }
1445*7c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1446437c7c90SJeremy L Thompson       data->assembled_bases_out[b] = B_out;
1447437c7c90SJeremy L Thompson     }
1448ed9e99e6SJeremy L Thompson   }
1449ed9e99e6SJeremy L Thompson 
1450437c7c90SJeremy L Thompson   // Pass out assembled data
1451437c7c90SJeremy L Thompson   if (active_bases) *active_bases = data->active_bases;
1452437c7c90SJeremy L Thompson   if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in;
1453437c7c90SJeremy L Thompson   if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out;
1454437c7c90SJeremy L Thompson 
1455437c7c90SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1456437c7c90SJeremy L Thompson }
1457437c7c90SJeremy L Thompson 
1458437c7c90SJeremy L Thompson /**
1459ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedBasis data for assembly.
1460ba746a46SJeremy L Thompson 
1461ba746a46SJeremy L Thompson   Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1462437c7c90SJeremy L Thompson 
1463437c7c90SJeremy L Thompson   @param[in]  data                  CeedOperatorAssemblyData
1464437c7c90SJeremy L Thompson   @param[out] num_active_elem_rstrs Number of active element restrictions, or NULL
1465437c7c90SJeremy L Thompson   @param[out] active_elem_rstrs     Pointer to hold active CeedElemRestrictions, or NULL
1466437c7c90SJeremy L Thompson 
1467437c7c90SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1468437c7c90SJeremy L Thompson 
1469437c7c90SJeremy L Thompson   @ref Backend
1470437c7c90SJeremy L Thompson **/
1471437c7c90SJeremy L Thompson int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs,
1472437c7c90SJeremy L Thompson                                                 CeedElemRestriction **active_elem_rstrs) {
1473437c7c90SJeremy L Thompson   if (num_active_elem_rstrs) *num_active_elem_rstrs = data->num_active_bases;
1474437c7c90SJeremy L Thompson   if (active_elem_rstrs) *active_elem_rstrs = data->active_elem_rstrs;
1475ed9e99e6SJeremy L Thompson 
1476ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1477ed9e99e6SJeremy L Thompson }
1478ed9e99e6SJeremy L Thompson 
1479ed9e99e6SJeremy L Thompson /**
1480ed9e99e6SJeremy L Thompson   @brief Destroy CeedOperatorAssemblyData
1481ed9e99e6SJeremy L Thompson 
1482ea61e9acSJeremy L Thompson   @param[in,out] data CeedOperatorAssemblyData to destroy
1483ed9e99e6SJeremy L Thompson 
1484ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1485ed9e99e6SJeremy L Thompson 
1486ed9e99e6SJeremy L Thompson   @ref Backend
1487ed9e99e6SJeremy L Thompson **/
1488ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) {
1489ad6481ceSJeremy L Thompson   if (!*data) {
1490ad6481ceSJeremy L Thompson     *data = NULL;
1491ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1492ad6481ceSJeremy L Thompson   }
14932b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
1494437c7c90SJeremy L Thompson   for (CeedInt b = 0; b < (*data)->num_active_bases; b++) {
1495437c7c90SJeremy L Thompson     CeedCall(CeedBasisDestroy(&(*data)->active_bases[b]));
1496437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs[b]));
1497437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_in[b]));
1498437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_out[b]));
1499437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b]));
1500437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b]));
1501437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_in[b]));
1502437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_out[b]));
1503437c7c90SJeremy L Thompson   }
1504437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->active_bases));
1505437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->active_elem_rstrs));
1506437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_in));
1507437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_out));
1508437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_in));
1509437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_out));
1510437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_in));
1511437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_out));
1512437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_in));
1513437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_out));
1514ed9e99e6SJeremy L Thompson 
15152b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1516ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1517ed9e99e6SJeremy L Thompson }
1518ed9e99e6SJeremy L Thompson 
1519480fae85SJeremy L Thompson /// @}
1520480fae85SJeremy L Thompson 
1521480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1522eaf62fffSJeremy L Thompson /// CeedOperator Public API
1523eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1524eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser
1525eaf62fffSJeremy L Thompson /// @{
1526eaf62fffSJeremy L Thompson 
1527eaf62fffSJeremy L Thompson /**
1528eaf62fffSJeremy L Thompson   @brief Assemble a linear CeedQFunction associated with a CeedOperator
1529eaf62fffSJeremy L Thompson 
1530ea61e9acSJeremy L Thompson   This returns a CeedVector containing a matrix at each quadrature point providing the action of the CeedQFunction associated with the CeedOperator.
1531859c15bbSJames Wright   The vector `assembled` is of shape `[num_elements, num_input_fields, num_output_fields, num_quad_points]` and contains column-major matrices
1532859c15bbSJames Wright representing the action of the CeedQFunction for a corresponding quadrature point on an element.
1533859c15bbSJames Wright 
15349fd66db6SSebastian Grimberg   Inputs and outputs are in the order provided by the user when adding CeedOperator fields.
15359fd66db6SSebastian 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
15369fd66db6SSebastian 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].
1537eaf62fffSJeremy L Thompson 
1538ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1539f04ea552SJeremy L Thompson 
1540ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1541ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points
1542ea61e9acSJeremy L Thompson   @param[out] rstr      CeedElemRestriction for CeedVector containing assembled CeedQFunction
1543ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1544eaf62fffSJeremy L Thompson 
1545eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1546eaf62fffSJeremy L Thompson 
1547eaf62fffSJeremy L Thompson   @ref User
1548eaf62fffSJeremy L Thompson **/
15492b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
15502b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1551eaf62fffSJeremy L Thompson 
1552eaf62fffSJeremy L Thompson   if (op->LinearAssembleQFunction) {
1553d04bbc78SJeremy L Thompson     // Backend version
15542b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request));
1555eaf62fffSJeremy L Thompson   } else {
1556d04bbc78SJeremy L Thompson     // Operator fallback
1557d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1558d04bbc78SJeremy L Thompson 
15592b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
15606574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request));
15616574a04fSJeremy L Thompson     else return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction");
156270a7ffb3SJeremy L Thompson   }
1563eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1564eaf62fffSJeremy L Thompson }
156570a7ffb3SJeremy L Thompson 
156670a7ffb3SJeremy L Thompson /**
1567ea61e9acSJeremy L Thompson   @brief Assemble CeedQFunction and store result internally.
15684385fb7fSSebastian Grimberg 
1569ea61e9acSJeremy L Thompson   Return copied references of stored data to the caller.
1570ea61e9acSJeremy L Thompson   Caller is responsible for ownership and destruction of the copied references.
1571ea61e9acSJeremy L Thompson   See also @ref CeedOperatorLinearAssembleQFunction
157270a7ffb3SJeremy L Thompson 
1573c5f45aeaSJeremy 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.
1574c5f45aeaSJeremy L Thompson         These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object.
1575c5f45aeaSJeremy L Thompson 
1576ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1577ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points
1578ea61e9acSJeremy L Thompson   @param[out] rstr      CeedElemRestriction for CeedVector containing assembledCeedQFunction
1579ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
158070a7ffb3SJeremy L Thompson 
158170a7ffb3SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
158270a7ffb3SJeremy L Thompson 
158370a7ffb3SJeremy L Thompson   @ref User
158470a7ffb3SJeremy L Thompson **/
15852b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
1586b05f7e9fSJeremy L Thompson   int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *) = NULL;
1587b05f7e9fSJeremy L Thompson   CeedOperator op_assemble                                                                           = NULL;
1588b05f7e9fSJeremy L Thompson 
15892b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
159070a7ffb3SJeremy L Thompson 
1591b05f7e9fSJeremy L Thompson   // Determine if fallback parent or operator has implementation
1592b05f7e9fSJeremy L Thompson   if (op->op_fallback_parent && op->op_fallback_parent->LinearAssembleQFunctionUpdate) {
1593b05f7e9fSJeremy L Thompson     // -- Backend version for op fallback parent is faster, if it exists
1594b05f7e9fSJeremy L Thompson     LinearAssembleQFunctionUpdate = op->op_fallback_parent->LinearAssembleQFunctionUpdate;
1595b05f7e9fSJeremy L Thompson     op_assemble                   = op->op_fallback_parent;
1596b05f7e9fSJeremy L Thompson   } else if (op->LinearAssembleQFunctionUpdate) {
1597b05f7e9fSJeremy L Thompson     // -- Backend version for op
1598b05f7e9fSJeremy L Thompson     LinearAssembleQFunctionUpdate = op->LinearAssembleQFunctionUpdate;
1599b05f7e9fSJeremy L Thompson     op_assemble                   = op;
1600b05f7e9fSJeremy L Thompson   }
1601b05f7e9fSJeremy L Thompson 
1602b05f7e9fSJeremy L Thompson   // Assemble QFunction
1603b05f7e9fSJeremy L Thompson   if (LinearAssembleQFunctionUpdate) {
1604b05f7e9fSJeremy L Thompson     // Backend or fallback parent version
1605480fae85SJeremy L Thompson     bool                qf_assembled_is_setup;
16062efa2d85SJeremy L Thompson     CeedVector          assembled_vec  = NULL;
16072efa2d85SJeremy L Thompson     CeedElemRestriction assembled_rstr = NULL;
1608480fae85SJeremy L Thompson 
16092b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataIsSetup(op->qf_assembled, &qf_assembled_is_setup));
1610480fae85SJeremy L Thompson     if (qf_assembled_is_setup) {
1611d04bbc78SJeremy L Thompson       bool update_needed;
1612d04bbc78SJeremy L Thompson 
16132b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataGetObjects(op->qf_assembled, &assembled_vec, &assembled_rstr));
16142b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(op->qf_assembled, &update_needed));
1615b05f7e9fSJeremy L Thompson       if (update_needed) CeedCall(LinearAssembleQFunctionUpdate(op_assemble, assembled_vec, assembled_rstr, request));
161670a7ffb3SJeremy L Thompson     } else {
1617b05f7e9fSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunction(op_assemble, &assembled_vec, &assembled_rstr, request));
16182b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataSetObjects(op->qf_assembled, assembled_vec, assembled_rstr));
161970a7ffb3SJeremy L Thompson     }
16202b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, false));
16212efa2d85SJeremy L Thompson 
1622d04bbc78SJeremy L Thompson     // Copy reference from internally held copy
16232b730f8bSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled));
16242b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr));
1625c5f45aeaSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled_vec));
16262b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&assembled_rstr));
162770a7ffb3SJeremy L Thompson   } else {
1628d04bbc78SJeremy L Thompson     // Operator fallback
1629d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1630d04bbc78SJeremy L Thompson 
16312b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
16326574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request));
16336574a04fSJeremy L Thompson     else return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate");
163470a7ffb3SJeremy L Thompson   }
163570a7ffb3SJeremy L Thompson 
163670a7ffb3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1637eaf62fffSJeremy L Thompson }
1638eaf62fffSJeremy L Thompson 
1639eaf62fffSJeremy L Thompson /**
1640eaf62fffSJeremy L Thompson   @brief Assemble the diagonal of a square linear CeedOperator
1641eaf62fffSJeremy L Thompson 
1642eaf62fffSJeremy L Thompson   This overwrites a CeedVector with the diagonal of a linear CeedOperator.
1643eaf62fffSJeremy L Thompson 
1644ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1645eaf62fffSJeremy L Thompson 
1646ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1647f04ea552SJeremy L Thompson 
1648ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1649eaf62fffSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator diagonal
1650ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1651eaf62fffSJeremy L Thompson 
1652eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1653eaf62fffSJeremy L Thompson 
1654eaf62fffSJeremy L Thompson   @ref User
1655eaf62fffSJeremy L Thompson **/
16562b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1657f3d47e36SJeremy L Thompson   bool is_composite;
16582b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1659f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1660eaf62fffSJeremy L Thompson 
1661c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
16622b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
16636574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1664c9366a6bSJeremy L Thompson 
1665f3d47e36SJeremy L Thompson   // Early exit for empty operator
1666f3d47e36SJeremy L Thompson   if (!is_composite) {
1667f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1668f3d47e36SJeremy L Thompson 
1669f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1670f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1671f3d47e36SJeremy L Thompson   }
1672f3d47e36SJeremy L Thompson 
1673eaf62fffSJeremy L Thompson   if (op->LinearAssembleDiagonal) {
1674d04bbc78SJeremy L Thompson     // Backend version
16752b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleDiagonal(op, assembled, request));
1676eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1677eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddDiagonal) {
1678d04bbc78SJeremy L Thompson     // Backend version with zeroing first
16792b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
16802b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1681eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1682eaf62fffSJeremy L Thompson   } else {
1683d04bbc78SJeremy L Thompson     // Operator fallback
1684d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1685d04bbc78SJeremy L Thompson 
16862b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1687d04bbc78SJeremy L Thompson     if (op_fallback) {
16882b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request));
1689eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1690eaf62fffSJeremy L Thompson     }
1691eaf62fffSJeremy L Thompson   }
1692eaf62fffSJeremy L Thompson   // Default interface implementation
16932b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
16942b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request));
1695d04bbc78SJeremy L Thompson 
1696eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1697eaf62fffSJeremy L Thompson }
1698eaf62fffSJeremy L Thompson 
1699eaf62fffSJeremy L Thompson /**
1700eaf62fffSJeremy L Thompson   @brief Assemble the diagonal of a square linear CeedOperator
1701eaf62fffSJeremy L Thompson 
1702eaf62fffSJeremy L Thompson   This sums into a CeedVector the diagonal of a linear CeedOperator.
1703eaf62fffSJeremy L Thompson 
1704ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1705eaf62fffSJeremy L Thompson 
1706ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1707f04ea552SJeremy L Thompson 
1708ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1709eaf62fffSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator diagonal
1710ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1711eaf62fffSJeremy L Thompson 
1712eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1713eaf62fffSJeremy L Thompson 
1714eaf62fffSJeremy L Thompson   @ref User
1715eaf62fffSJeremy L Thompson **/
17162b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1717f3d47e36SJeremy L Thompson   bool is_composite;
17182b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1719f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1720eaf62fffSJeremy L Thompson 
1721c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
17222b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
17236574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1724c9366a6bSJeremy L Thompson 
1725f3d47e36SJeremy L Thompson   // Early exit for empty operator
1726f3d47e36SJeremy L Thompson   if (!is_composite) {
1727f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1728f3d47e36SJeremy L Thompson 
1729f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1730f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1731f3d47e36SJeremy L Thompson   }
1732f3d47e36SJeremy L Thompson 
1733eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddDiagonal) {
1734d04bbc78SJeremy L Thompson     // Backend version
17352b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1736eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1737eaf62fffSJeremy L Thompson   } else {
1738d04bbc78SJeremy L Thompson     // Operator fallback
1739d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1740d04bbc78SJeremy L Thompson 
17412b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1742d04bbc78SJeremy L Thompson     if (op_fallback) {
17432b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
1744eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1745eaf62fffSJeremy L Thompson     }
1746eaf62fffSJeremy L Thompson   }
1747eaf62fffSJeremy L Thompson   // Default interface implementation
1748eaf62fffSJeremy L Thompson   if (is_composite) {
17492b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
1750eaf62fffSJeremy L Thompson   } else {
17512b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, false, assembled));
1752eaf62fffSJeremy L Thompson   }
1753d04bbc78SJeremy L Thompson 
1754d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1755eaf62fffSJeremy L Thompson }
1756eaf62fffSJeremy L Thompson 
1757eaf62fffSJeremy L Thompson /**
1758eaf62fffSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear CeedOperator
1759eaf62fffSJeremy L Thompson 
1760ea61e9acSJeremy L Thompson   This overwrites a CeedVector with the point block diagonal of a linear CeedOperator.
1761eaf62fffSJeremy L Thompson 
1762ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1763eaf62fffSJeremy L Thompson 
1764ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1765f04ea552SJeremy L Thompson 
1766ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1767ea61e9acSJeremy 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
1768ea61e9acSJeremy 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,
1769ea61e9acSJeremy L Thompson component in].
1770ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1771eaf62fffSJeremy L Thompson 
1772eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1773eaf62fffSJeremy L Thompson 
1774eaf62fffSJeremy L Thompson   @ref User
1775eaf62fffSJeremy L Thompson **/
17762b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1777f3d47e36SJeremy L Thompson   bool is_composite;
17782b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1779f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1780eaf62fffSJeremy L Thompson 
1781c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
17822b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
17836574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1784c9366a6bSJeremy L Thompson 
1785f3d47e36SJeremy L Thompson   // Early exit for empty operator
1786f3d47e36SJeremy L Thompson   if (!is_composite) {
1787f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1788f3d47e36SJeremy L Thompson 
1789f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1790f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1791f3d47e36SJeremy L Thompson   }
1792f3d47e36SJeremy L Thompson 
1793eaf62fffSJeremy L Thompson   if (op->LinearAssemblePointBlockDiagonal) {
1794d04bbc78SJeremy L Thompson     // Backend version
17952b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request));
1796eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1797eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddPointBlockDiagonal) {
1798d04bbc78SJeremy L Thompson     // Backend version with zeroing first
17992b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
18002b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
1801eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1802eaf62fffSJeremy L Thompson   } else {
1803d04bbc78SJeremy L Thompson     // Operator fallback
1804d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1805d04bbc78SJeremy L Thompson 
18062b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1807d04bbc78SJeremy L Thompson     if (op_fallback) {
18082b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request));
1809eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1810eaf62fffSJeremy L Thompson     }
1811eaf62fffSJeremy L Thompson   }
1812eaf62fffSJeremy L Thompson   // Default interface implementation
18132b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
18142b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
1815d04bbc78SJeremy L Thompson 
1816eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1817eaf62fffSJeremy L Thompson }
1818eaf62fffSJeremy L Thompson 
1819eaf62fffSJeremy L Thompson /**
1820eaf62fffSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear CeedOperator
1821eaf62fffSJeremy L Thompson 
1822ea61e9acSJeremy L Thompson   This sums into a CeedVector with the point block diagonal of a linear CeedOperator.
1823eaf62fffSJeremy L Thompson 
1824ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1825eaf62fffSJeremy L Thompson 
1826ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1827f04ea552SJeremy L Thompson 
1828ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1829ea61e9acSJeremy 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
1830ea61e9acSJeremy 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,
1831ea61e9acSJeremy L Thompson component in].
1832ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1833eaf62fffSJeremy L Thompson 
1834eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1835eaf62fffSJeremy L Thompson 
1836eaf62fffSJeremy L Thompson   @ref User
1837eaf62fffSJeremy L Thompson **/
18382b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1839f3d47e36SJeremy L Thompson   bool is_composite;
18402b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1841f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1842eaf62fffSJeremy L Thompson 
1843c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
18442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
18456574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1846c9366a6bSJeremy L Thompson 
1847f3d47e36SJeremy L Thompson   // Early exit for empty operator
1848f3d47e36SJeremy L Thompson   if (!is_composite) {
1849f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1850f3d47e36SJeremy L Thompson 
1851f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1852f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1853f3d47e36SJeremy L Thompson   }
1854f3d47e36SJeremy L Thompson 
1855eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddPointBlockDiagonal) {
1856d04bbc78SJeremy L Thompson     // Backend version
18572b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request));
1858eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1859eaf62fffSJeremy L Thompson   } else {
1860d04bbc78SJeremy L Thompson     // Operator fallback
1861d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1862d04bbc78SJeremy L Thompson 
18632b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1864d04bbc78SJeremy L Thompson     if (op_fallback) {
18652b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request));
1866eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1867eaf62fffSJeremy L Thompson     }
1868eaf62fffSJeremy L Thompson   }
1869ea61e9acSJeremy L Thompson   // Default interface implementation
1870eaf62fffSJeremy L Thompson   if (is_composite) {
18712b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
1872eaf62fffSJeremy L Thompson   } else {
18732b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, true, assembled));
1874eaf62fffSJeremy L Thompson   }
1875d04bbc78SJeremy L Thompson 
1876d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1877eaf62fffSJeremy L Thompson }
1878eaf62fffSJeremy L Thompson 
1879eaf62fffSJeremy L Thompson /**
1880eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero pattern of a linear operator.
1881eaf62fffSJeremy L Thompson 
1882ea61e9acSJeremy L Thompson    Expected to be used in conjunction with CeedOperatorLinearAssemble().
1883eaf62fffSJeremy L Thompson 
1884ea61e9acSJeremy 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
18859fd66db6SSebastian Grimberg matrix in entry (i, j).
18869fd66db6SSebastian Grimberg   Note that the (i, j) pairs are not unique and may repeat.
18879fd66db6SSebastian Grimberg   This function returns the number of entries and their (i, j) locations, while CeedOperatorLinearAssemble() provides the values in the same ordering.
1888eaf62fffSJeremy L Thompson 
1889eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
1890eaf62fffSJeremy L Thompson 
1891ea61e9acSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1892f04ea552SJeremy L Thompson 
1893eaf62fffSJeremy L Thompson    @param[in]  op          CeedOperator to assemble
1894eaf62fffSJeremy L Thompson    @param[out] num_entries Number of entries in coordinate nonzero pattern
1895eaf62fffSJeremy L Thompson    @param[out] rows        Row number for each entry
1896eaf62fffSJeremy L Thompson    @param[out] cols        Column number for each entry
1897eaf62fffSJeremy L Thompson 
1898eaf62fffSJeremy L Thompson    @ref User
1899eaf62fffSJeremy L Thompson **/
19002b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
1901b94338b9SJed Brown   CeedInt       num_suboperators;
1902b94338b9SJed Brown   CeedSize      single_entries;
1903eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
1904eaf62fffSJeremy L Thompson   bool          is_composite;
19052b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1906f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1907eaf62fffSJeremy L Thompson 
1908eaf62fffSJeremy L Thompson   if (op->LinearAssembleSymbolic) {
1909d04bbc78SJeremy L Thompson     // Backend version
19102b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols));
1911eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1912eaf62fffSJeremy L Thompson   } else {
1913d04bbc78SJeremy L Thompson     // Operator fallback
1914d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1915d04bbc78SJeremy L Thompson 
19162b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1917d04bbc78SJeremy L Thompson     if (op_fallback) {
19182b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols));
1919eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1920eaf62fffSJeremy L Thompson     }
1921eaf62fffSJeremy L Thompson   }
1922eaf62fffSJeremy L Thompson 
1923eaf62fffSJeremy L Thompson   // Default interface implementation
1924eaf62fffSJeremy L Thompson 
1925eaf62fffSJeremy L Thompson   // count entries and allocate rows, cols arrays
1926eaf62fffSJeremy L Thompson   *num_entries = 0;
1927eaf62fffSJeremy L Thompson   if (is_composite) {
1928c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1929c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
193092ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
19312b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
1932eaf62fffSJeremy L Thompson       *num_entries += single_entries;
1933eaf62fffSJeremy L Thompson     }
1934eaf62fffSJeremy L Thompson   } else {
19352b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries));
1936eaf62fffSJeremy L Thompson     *num_entries += single_entries;
1937eaf62fffSJeremy L Thompson   }
19382b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, rows));
19392b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, cols));
1940eaf62fffSJeremy L Thompson 
1941eaf62fffSJeremy L Thompson   // assemble nonzero locations
1942eaf62fffSJeremy L Thompson   CeedInt offset = 0;
1943eaf62fffSJeremy L Thompson   if (is_composite) {
1944c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1945c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
194692ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
19472b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols));
19482b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
1949eaf62fffSJeremy L Thompson       offset += single_entries;
1950eaf62fffSJeremy L Thompson     }
1951eaf62fffSJeremy L Thompson   } else {
19522b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols));
1953eaf62fffSJeremy L Thompson   }
1954eaf62fffSJeremy L Thompson 
1955eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1956eaf62fffSJeremy L Thompson }
1957eaf62fffSJeremy L Thompson 
1958eaf62fffSJeremy L Thompson /**
1959eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero entries of a linear operator.
1960eaf62fffSJeremy L Thompson 
1961ea61e9acSJeremy L Thompson    Expected to be used in conjunction with CeedOperatorLinearAssembleSymbolic().
1962eaf62fffSJeremy L Thompson 
1963ea61e9acSJeremy 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
19649fd66db6SSebastian Grimberg matrix in entry (i, j).
19659fd66db6SSebastian Grimberg   Note that the (i, j) pairs are not unique and may repeat.
19669fd66db6SSebastian Grimberg   This function returns the values of the nonzero entries to be added, their (i, j) locations are provided by CeedOperatorLinearAssembleSymbolic()
1967eaf62fffSJeremy L Thompson 
1968eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
1969eaf62fffSJeremy L Thompson 
1970ea61e9acSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1971f04ea552SJeremy L Thompson 
1972eaf62fffSJeremy L Thompson    @param[in]  op     CeedOperator to assemble
1973eaf62fffSJeremy L Thompson    @param[out] values Values to assemble into matrix
1974eaf62fffSJeremy L Thompson 
1975eaf62fffSJeremy L Thompson    @ref User
1976eaf62fffSJeremy L Thompson **/
1977eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) {
1978b94338b9SJed Brown   CeedInt       num_suboperators;
1979b94338b9SJed Brown   CeedSize      single_entries = 0;
1980eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
1981f3d47e36SJeremy L Thompson   bool          is_composite;
19822b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1983f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1984f3d47e36SJeremy L Thompson 
1985f3d47e36SJeremy L Thompson   // Early exit for empty operator
1986f3d47e36SJeremy L Thompson   if (!is_composite) {
1987f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1988f3d47e36SJeremy L Thompson 
1989f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1990f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1991f3d47e36SJeremy L Thompson   }
1992eaf62fffSJeremy L Thompson 
1993eaf62fffSJeremy L Thompson   if (op->LinearAssemble) {
1994d04bbc78SJeremy L Thompson     // Backend version
19952b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemble(op, values));
1996eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1997eaf62fffSJeremy L Thompson   } else {
1998d04bbc78SJeremy L Thompson     // Operator fallback
1999d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2000d04bbc78SJeremy L Thompson 
20012b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2002d04bbc78SJeremy L Thompson     if (op_fallback) {
20032b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemble(op_fallback, values));
2004eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2005eaf62fffSJeremy L Thompson     }
2006eaf62fffSJeremy L Thompson   }
2007eaf62fffSJeremy L Thompson 
2008eaf62fffSJeremy L Thompson   // Default interface implementation
2009eaf62fffSJeremy L Thompson   CeedInt offset = 0;
201028ec399dSJeremy L Thompson   CeedCall(CeedVectorSetValue(values, 0.0));
2011eaf62fffSJeremy L Thompson   if (is_composite) {
2012c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2013c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2014cefa2673SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; k++) {
20152b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values));
20162b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2017eaf62fffSJeremy L Thompson       offset += single_entries;
2018eaf62fffSJeremy L Thompson     }
2019eaf62fffSJeremy L Thompson   } else {
20202b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemble(op, offset, values));
2021eaf62fffSJeremy L Thompson   }
2022eaf62fffSJeremy L Thompson 
2023eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2024eaf62fffSJeremy L Thompson }
2025eaf62fffSJeremy L Thompson 
2026eaf62fffSJeremy L Thompson /**
202775f0d5a4SJeremy L Thompson   @brief Get the multiplicity of nodes across suboperators in a composite CeedOperator
202875f0d5a4SJeremy L Thompson 
202975f0d5a4SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
203075f0d5a4SJeremy L Thompson 
203175f0d5a4SJeremy L Thompson   @param[in]  op               Composite CeedOperator
203275f0d5a4SJeremy L Thompson   @param[in]  num_skip_indices Number of suboperators to skip
203375f0d5a4SJeremy L Thompson   @param[in]  skip_indices     Array of indices of suboperators to skip
203475f0d5a4SJeremy L Thompson   @param[out] mult             Vector to store multiplicity (of size l_size)
203575f0d5a4SJeremy L Thompson 
203675f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
203775f0d5a4SJeremy L Thompson 
203875f0d5a4SJeremy L Thompson   @ref User
203975f0d5a4SJeremy L Thompson **/
204075f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) {
204175f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
204275f0d5a4SJeremy L Thompson 
204375f0d5a4SJeremy L Thompson   Ceed                ceed;
2044b275c451SJeremy L Thompson   CeedInt             num_suboperators;
204575f0d5a4SJeremy L Thompson   CeedSize            l_vec_len;
204675f0d5a4SJeremy L Thompson   CeedScalar         *mult_array;
204775f0d5a4SJeremy L Thompson   CeedVector          ones_l_vec;
2048*7c1dbaffSSebastian Grimberg   CeedElemRestriction elem_rstr, mult_elem_rstr;
2049b275c451SJeremy L Thompson   CeedOperator       *sub_operators;
205075f0d5a4SJeremy L Thompson 
205175f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
205275f0d5a4SJeremy L Thompson 
205375f0d5a4SJeremy L Thompson   // Zero mult vector
205475f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
205575f0d5a4SJeremy L Thompson 
205675f0d5a4SJeremy L Thompson   // Get suboperators
2057b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2058b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2059b275c451SJeremy L Thompson   if (num_suboperators == 0) return CEED_ERROR_SUCCESS;
206075f0d5a4SJeremy L Thompson 
206175f0d5a4SJeremy L Thompson   // Work vector
206275f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetLength(mult, &l_vec_len));
206375f0d5a4SJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec));
206475f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(ones_l_vec, 1.0));
206575f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array));
206675f0d5a4SJeremy L Thompson 
206775f0d5a4SJeremy L Thompson   // Compute multiplicity across suboperators
2068b275c451SJeremy L Thompson   for (CeedInt i = 0; i < num_suboperators; i++) {
206975f0d5a4SJeremy L Thompson     const CeedScalar *sub_mult_array;
207075f0d5a4SJeremy L Thompson     CeedVector        sub_mult_l_vec, ones_e_vec;
207175f0d5a4SJeremy L Thompson 
207275f0d5a4SJeremy L Thompson     // -- Check for suboperator to skip
207375f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < num_skip_indices; j++) {
207475f0d5a4SJeremy L Thompson       if (skip_indices[j] == i) continue;
207575f0d5a4SJeremy L Thompson     }
207675f0d5a4SJeremy L Thompson 
207775f0d5a4SJeremy L Thompson     // -- Sub operator multiplicity
2078437c7c90SJeremy L Thompson     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr));
2079*7c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr, &mult_elem_rstr));
2080*7c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateVector(mult_elem_rstr, &sub_mult_l_vec, &ones_e_vec));
208175f0d5a4SJeremy L Thompson     CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0));
2082*7c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE));
2083*7c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE));
208475f0d5a4SJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array));
208575f0d5a4SJeremy L Thompson     // ---- Flag every node present in the current suboperator
208675f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < l_vec_len; j++) {
208775f0d5a4SJeremy L Thompson       if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0;
208875f0d5a4SJeremy L Thompson     }
208975f0d5a4SJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array));
209075f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&sub_mult_l_vec));
209175f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&ones_e_vec));
2092*7c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&mult_elem_rstr));
209375f0d5a4SJeremy L Thompson   }
209475f0d5a4SJeremy L Thompson   CeedCall(CeedVectorRestoreArray(mult, &mult_array));
2095811d0ccfSJeremy L Thompson   CeedCall(CeedVectorDestroy(&ones_l_vec));
209675f0d5a4SJeremy L Thompson 
209775f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
209875f0d5a4SJeremy L Thompson }
209975f0d5a4SJeremy L Thompson 
210075f0d5a4SJeremy L Thompson /**
2101ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator, creating the prolongation basis from the fine and coarse
2102ea61e9acSJeremy L Thompson grid interpolation
2103eaf62fffSJeremy L Thompson 
210458e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2105f04ea552SJeremy L Thompson 
2106eaf62fffSJeremy L Thompson   @param[in]  op_fine      Fine grid operator
210785bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2108eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid restriction
2109eaf62fffSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector basis
2110eaf62fffSJeremy L Thompson   @param[out] op_coarse    Coarse grid operator
211185bb9dcfSJeremy L Thompson   @param[out] op_prolong   Coarse to fine operator, or NULL
211285bb9dcfSJeremy L Thompson   @param[out] op_restrict  Fine to coarse operator, or NULL
2113eaf62fffSJeremy L Thompson 
2114eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2115eaf62fffSJeremy L Thompson 
2116eaf62fffSJeremy L Thompson   @ref User
2117eaf62fffSJeremy L Thompson **/
21182b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
21192b730f8bSJeremy L Thompson                                      CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
21202b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2121eaf62fffSJeremy L Thompson 
212283d6adf3SZach Atkins   // Build prolongation matrix, if required
212383d6adf3SZach Atkins   CeedBasis basis_c_to_f = NULL;
212483d6adf3SZach Atkins   if (op_prolong || op_restrict) {
212583d6adf3SZach Atkins     CeedBasis basis_fine;
21262b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
21272b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f));
212883d6adf3SZach Atkins   }
2129eaf62fffSJeremy L Thompson 
2130f113e5dcSJeremy L Thompson   // Core code
21312b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2132f113e5dcSJeremy L Thompson 
2133eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2134eaf62fffSJeremy L Thompson }
2135eaf62fffSJeremy L Thompson 
2136eaf62fffSJeremy L Thompson /**
2137ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a tensor basis for the active basis
2138eaf62fffSJeremy L Thompson 
213958e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2140f04ea552SJeremy L Thompson 
2141eaf62fffSJeremy L Thompson   @param[in]  op_fine       Fine grid operator
214285bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2143eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid restriction
2144eaf62fffSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector basis
214585bb9dcfSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
2146eaf62fffSJeremy L Thompson   @param[out] op_coarse     Coarse grid operator
214785bb9dcfSJeremy L Thompson   @param[out] op_prolong    Coarse to fine operator, or NULL
214885bb9dcfSJeremy L Thompson   @param[out] op_restrict   Fine to coarse operator, or NULL
2149eaf62fffSJeremy L Thompson 
2150eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2151eaf62fffSJeremy L Thompson 
2152eaf62fffSJeremy L Thompson   @ref User
2153eaf62fffSJeremy L Thompson **/
21542b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
21552b730f8bSJeremy L Thompson                                              const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
21562b730f8bSJeremy L Thompson                                              CeedOperator *op_restrict) {
21572b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2158eaf62fffSJeremy L Thompson   Ceed ceed;
21592b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2160eaf62fffSJeremy L Thompson 
2161eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
2162eaf62fffSJeremy L Thompson   CeedBasis basis_fine;
21632b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
2164eaf62fffSJeremy L Thompson   CeedInt Q_f, Q_c;
21652b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
21662b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
21676574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2168eaf62fffSJeremy L Thompson 
216983d6adf3SZach Atkins   // Create coarse to fine basis, if required
217083d6adf3SZach Atkins   CeedBasis basis_c_to_f = NULL;
217183d6adf3SZach Atkins   if (op_prolong || op_restrict) {
217283d6adf3SZach Atkins     // Check if interpolation matrix is provided
21736574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
21746574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
2175eaf62fffSJeremy L Thompson     CeedInt dim, num_comp, num_nodes_c, P_1d_f, P_1d_c;
21762b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
21772b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
21782b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f));
21792b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
21802b730f8bSJeremy L Thompson     P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c);
2181eaf62fffSJeremy L Thompson     CeedScalar *q_ref, *q_weight, *grad;
21822b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_ref));
21832b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_weight));
21842b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad));
21852b730f8bSJeremy 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));
21862b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
21872b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
21882b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
218983d6adf3SZach Atkins   }
2190eaf62fffSJeremy L Thompson 
2191eaf62fffSJeremy L Thompson   // Core code
21922b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2193eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2194eaf62fffSJeremy L Thompson }
2195eaf62fffSJeremy L Thompson 
2196eaf62fffSJeremy L Thompson /**
2197ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a non-tensor basis for the active vector
2198eaf62fffSJeremy L Thompson 
219958e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2200f04ea552SJeremy L Thompson 
2201eaf62fffSJeremy L Thompson   @param[in]  op_fine       Fine grid operator
220285bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2203eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid restriction
2204eaf62fffSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector basis
220585bb9dcfSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
2206eaf62fffSJeremy L Thompson   @param[out] op_coarse     Coarse grid operator
220785bb9dcfSJeremy L Thompson   @param[out] op_prolong    Coarse to fine operator, or NULL
220885bb9dcfSJeremy L Thompson   @param[out] op_restrict   Fine to coarse operator, or NULL
2209eaf62fffSJeremy L Thompson 
2210eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2211eaf62fffSJeremy L Thompson 
2212eaf62fffSJeremy L Thompson   @ref User
2213eaf62fffSJeremy L Thompson **/
22142b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
22152b730f8bSJeremy L Thompson                                        const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
2216eaf62fffSJeremy L Thompson                                        CeedOperator *op_restrict) {
22172b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2218eaf62fffSJeremy L Thompson   Ceed ceed;
22192b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2220eaf62fffSJeremy L Thompson 
2221eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
2222eaf62fffSJeremy L Thompson   CeedBasis basis_fine;
22232b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
2224eaf62fffSJeremy L Thompson   CeedInt Q_f, Q_c;
22252b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
22262b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
22276574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2228eaf62fffSJeremy L Thompson 
2229eaf62fffSJeremy L Thompson   // Coarse to fine basis
223083d6adf3SZach Atkins   CeedBasis basis_c_to_f = NULL;
223183d6adf3SZach Atkins   if (op_prolong || op_restrict) {
223283d6adf3SZach Atkins     // Check if interpolation matrix is provided
22336574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
22346574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
2235eaf62fffSJeremy L Thompson     CeedElemTopology topo;
22362b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_fine, &topo));
2237eaf62fffSJeremy L Thompson     CeedInt dim, num_comp, num_nodes_c, num_nodes_f;
22382b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
22392b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
22402b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f));
22412b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
2242eaf62fffSJeremy L Thompson     CeedScalar *q_ref, *q_weight, *grad;
22432b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref));
22442b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f, &q_weight));
22452b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad));
22462b730f8bSJeremy 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));
22472b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
22482b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
22492b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
225083d6adf3SZach Atkins   }
2251eaf62fffSJeremy L Thompson 
2252eaf62fffSJeremy L Thompson   // Core code
22532b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2254eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2255eaf62fffSJeremy L Thompson }
2256eaf62fffSJeremy L Thompson 
2257eaf62fffSJeremy L Thompson /**
2258ea61e9acSJeremy L Thompson   @brief Build a FDM based approximate inverse for each element for a CeedOperator
2259eaf62fffSJeremy L Thompson 
2260ea61e9acSJeremy L Thompson   This returns a CeedOperator and CeedVector to apply a Fast Diagonalization Method based approximate inverse.
2261859c15bbSJames 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$.
2262859c15bbSJames Wright   The assembled QFunction is used to modify the eigenvalues from simultaneous diagonalization and obtain an approximate inverse of the form \f$V^T
22639fd66db6SSebastian Grimberg \hat S V\f$.
22649fd66db6SSebastian Grimberg   The CeedOperator must be linear and non-composite.
22659fd66db6SSebastian Grimberg   The associated CeedQFunction must therefore also be linear.
2266eaf62fffSJeremy L Thompson 
2267ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2268f04ea552SJeremy L Thompson 
2269ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to create element inverses
2270ea61e9acSJeremy L Thompson   @param[out] fdm_inv CeedOperator to apply the action of a FDM based inverse for each element
2271ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2272eaf62fffSJeremy L Thompson 
2273eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2274eaf62fffSJeremy L Thompson 
2275480fae85SJeremy L Thompson   @ref User
2276eaf62fffSJeremy L Thompson **/
22772b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) {
22782b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2279eaf62fffSJeremy L Thompson 
2280eaf62fffSJeremy L Thompson   if (op->CreateFDMElementInverse) {
2281d04bbc78SJeremy L Thompson     // Backend version
22822b730f8bSJeremy L Thompson     CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request));
2283eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2284eaf62fffSJeremy L Thompson   } else {
2285d04bbc78SJeremy L Thompson     // Operator fallback
2286d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2287d04bbc78SJeremy L Thompson 
22882b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2289d04bbc78SJeremy L Thompson     if (op_fallback) {
22902b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request));
2291eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2292eaf62fffSJeremy L Thompson     }
2293eaf62fffSJeremy L Thompson   }
2294eaf62fffSJeremy L Thompson 
2295d04bbc78SJeremy L Thompson   // Default interface implementation
2296eaf62fffSJeremy L Thompson   Ceed ceed, ceed_parent;
22972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
22982b730f8bSJeremy L Thompson   CeedCall(CeedGetOperatorFallbackParentCeed(ceed, &ceed_parent));
2299eaf62fffSJeremy L Thompson   ceed_parent = ceed_parent ? ceed_parent : ceed;
2300eaf62fffSJeremy L Thompson   CeedQFunction qf;
23012b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
2302eaf62fffSJeremy L Thompson 
2303eaf62fffSJeremy L Thompson   // Determine active input basis
2304eaf62fffSJeremy L Thompson   bool                interp = false, grad = false;
2305eaf62fffSJeremy L Thompson   CeedBasis           basis = NULL;
2306eaf62fffSJeremy L Thompson   CeedElemRestriction rstr  = NULL;
2307eaf62fffSJeremy L Thompson   CeedOperatorField  *op_fields;
2308eaf62fffSJeremy L Thompson   CeedQFunctionField *qf_fields;
2309eaf62fffSJeremy L Thompson   CeedInt             num_input_fields;
23102b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL));
23112b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
2312eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
2313eaf62fffSJeremy L Thompson     CeedVector vec;
23142b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
2315eaf62fffSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2316eaf62fffSJeremy L Thompson       CeedEvalMode eval_mode;
23172b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
2318eaf62fffSJeremy L Thompson       interp = interp || eval_mode == CEED_EVAL_INTERP;
2319eaf62fffSJeremy L Thompson       grad   = grad || eval_mode == CEED_EVAL_GRAD;
23202b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis));
23212b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
2322eaf62fffSJeremy L Thompson     }
2323eaf62fffSJeremy L Thompson   }
23246574a04fSJeremy L Thompson   CeedCheck(basis, ceed, CEED_ERROR_BACKEND, "No active field set");
2325e79b91d9SJeremy L Thompson   CeedSize l_size = 1;
2326352a5e7cSSebastian Grimberg   CeedInt  P_1d, Q_1d, num_nodes, num_qpts, dim, num_comp = 1, num_elem = 1;
23272b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
2328352a5e7cSSebastian Grimberg   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
23292b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
23302b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
23312b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
23322b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
23332b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
23342b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
2335eaf62fffSJeremy L Thompson 
2336eaf62fffSJeremy L Thompson   // Build and diagonalize 1D Mass and Laplacian
23376574a04fSJeremy L Thompson   bool is_tensor_basis;
23386574a04fSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
23396574a04fSJeremy L Thompson   CeedCheck(is_tensor_basis, ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases");
2340eaf62fffSJeremy L Thompson   CeedScalar *mass, *laplace, *x, *fdm_interp, *lambda;
23412b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &mass));
23422b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &laplace));
23432b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &x));
23442b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp));
23452b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &lambda));
2346eaf62fffSJeremy L Thompson   // -- Build matrices
2347eaf62fffSJeremy L Thompson   const CeedScalar *interp_1d, *grad_1d, *q_weight_1d;
23482b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
23492b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
23502b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
23512b730f8bSJeremy L Thompson   CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace));
2352eaf62fffSJeremy L Thompson 
2353eaf62fffSJeremy L Thompson   // -- Diagonalize
23542b730f8bSJeremy L Thompson   CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d));
23552b730f8bSJeremy L Thompson   CeedCall(CeedFree(&mass));
23562b730f8bSJeremy L Thompson   CeedCall(CeedFree(&laplace));
23572b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
23582b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d];
23592b730f8bSJeremy L Thompson   }
23602b730f8bSJeremy L Thompson   CeedCall(CeedFree(&x));
2361eaf62fffSJeremy L Thompson 
2362eaf62fffSJeremy L Thompson   // Assemble QFunction
2363c5f45aeaSJeremy L Thompson   CeedVector          assembled = NULL;
2364c5f45aeaSJeremy L Thompson   CeedElemRestriction rstr_qf   = NULL;
23652b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request));
2366eaf62fffSJeremy L Thompson   CeedInt layout[3];
23672b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_qf, &layout));
23682b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qf));
2369eaf62fffSJeremy L Thompson   CeedScalar max_norm = 0;
23702b730f8bSJeremy L Thompson   CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm));
2371eaf62fffSJeremy L Thompson 
2372eaf62fffSJeremy L Thompson   // Calculate element averages
2373eaf62fffSJeremy L Thompson   CeedInt           num_modes = (interp ? 1 : 0) + (grad ? dim : 0);
2374eaf62fffSJeremy L Thompson   CeedScalar       *elem_avg;
2375eaf62fffSJeremy L Thompson   const CeedScalar *assembled_array, *q_weight_array;
2376eaf62fffSJeremy L Thompson   CeedVector        q_weight;
23772b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight));
23782b730f8bSJeremy L Thompson   CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight));
23792b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array));
23802b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array));
23812b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_elem, &elem_avg));
2382eaf62fffSJeremy L Thompson   const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON;
2383eaf62fffSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
2384eaf62fffSJeremy L Thompson     CeedInt count = 0;
23852b730f8bSJeremy L Thompson     for (CeedInt q = 0; q < num_qpts; q++) {
23862b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) {
23872b730f8bSJeremy L Thompson         if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) {
23882b730f8bSJeremy L Thompson           elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q];
2389eaf62fffSJeremy L Thompson           count++;
2390eaf62fffSJeremy L Thompson         }
23912b730f8bSJeremy L Thompson       }
23922b730f8bSJeremy L Thompson     }
2393eaf62fffSJeremy L Thompson     if (count) {
2394eaf62fffSJeremy L Thompson       elem_avg[e] /= count;
2395eaf62fffSJeremy L Thompson     } else {
2396eaf62fffSJeremy L Thompson       elem_avg[e] = 1.0;
2397eaf62fffSJeremy L Thompson     }
2398eaf62fffSJeremy L Thompson   }
23992b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array));
24002b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled));
24012b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array));
24022b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_weight));
2403eaf62fffSJeremy L Thompson 
2404eaf62fffSJeremy L Thompson   // Build FDM diagonal
2405eaf62fffSJeremy L Thompson   CeedVector  q_data;
2406eaf62fffSJeremy L Thompson   CeedScalar *q_data_array, *fdm_diagonal;
2407352a5e7cSSebastian Grimberg   CeedCall(CeedCalloc(num_comp * num_nodes, &fdm_diagonal));
2408352a5e7cSSebastian Grimberg   const CeedScalar fdm_diagonal_bound = num_nodes * CEED_EPSILON;
24092b730f8bSJeremy L Thompson   for (CeedInt c = 0; c < num_comp; c++) {
2410352a5e7cSSebastian Grimberg     for (CeedInt n = 0; n < num_nodes; n++) {
2411352a5e7cSSebastian Grimberg       if (interp) fdm_diagonal[c * num_nodes + n] = 1.0;
24122b730f8bSJeremy L Thompson       if (grad) {
2413eaf62fffSJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
2414eaf62fffSJeremy L Thompson           CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d;
2415352a5e7cSSebastian Grimberg           fdm_diagonal[c * num_nodes + n] += lambda[i];
2416eaf62fffSJeremy L Thompson         }
2417eaf62fffSJeremy L Thompson       }
2418352a5e7cSSebastian Grimberg       if (fabs(fdm_diagonal[c * num_nodes + n]) < fdm_diagonal_bound) fdm_diagonal[c * num_nodes + n] = fdm_diagonal_bound;
24192b730f8bSJeremy L Thompson     }
24202b730f8bSJeremy L Thompson   }
2421352a5e7cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * num_nodes, &q_data));
24222b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(q_data, 0.0));
24232b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array));
24242b730f8bSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
24252b730f8bSJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
2426352a5e7cSSebastian 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]);
24272b730f8bSJeremy L Thompson     }
24282b730f8bSJeremy L Thompson   }
24292b730f8bSJeremy L Thompson   CeedCall(CeedFree(&elem_avg));
24302b730f8bSJeremy L Thompson   CeedCall(CeedFree(&fdm_diagonal));
24312b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(q_data, &q_data_array));
2432eaf62fffSJeremy L Thompson 
2433eaf62fffSJeremy L Thompson   // Setup FDM operator
2434eaf62fffSJeremy L Thompson   // -- Basis
2435eaf62fffSJeremy L Thompson   CeedBasis   fdm_basis;
2436eaf62fffSJeremy L Thompson   CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy;
24372b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy));
24382b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &q_ref_dummy));
24392b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &q_weight_dummy));
24402b730f8bSJeremy 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));
24412b730f8bSJeremy L Thompson   CeedCall(CeedFree(&fdm_interp));
24422b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_dummy));
24432b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref_dummy));
24442b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight_dummy));
24452b730f8bSJeremy L Thompson   CeedCall(CeedFree(&lambda));
2446eaf62fffSJeremy L Thompson 
2447eaf62fffSJeremy L Thompson   // -- Restriction
2448eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_qd_i;
2449352a5e7cSSebastian Grimberg   CeedInt             strides[3] = {1, num_nodes, num_nodes * num_comp};
2450352a5e7cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, num_nodes, num_comp, num_elem * num_comp * num_nodes, strides, &rstr_qd_i));
2451eaf62fffSJeremy L Thompson   // -- QFunction
2452eaf62fffSJeremy L Thompson   CeedQFunction qf_fdm;
24532b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm));
24542b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP));
24552b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE));
24562b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP));
24572b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp));
2458eaf62fffSJeremy L Thompson   // -- QFunction context
2459eaf62fffSJeremy L Thompson   CeedInt *num_comp_data;
24602b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, &num_comp_data));
2461eaf62fffSJeremy L Thompson   num_comp_data[0] = num_comp;
2462eaf62fffSJeremy L Thompson   CeedQFunctionContext ctx_fdm;
24632b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm));
24642b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data));
24652b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm));
24662b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_fdm));
2467eaf62fffSJeremy L Thompson   // -- Operator
24682b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv));
24692b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
24702b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_COLLOCATED, q_data));
24712b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2472eaf62fffSJeremy L Thompson 
2473eaf62fffSJeremy L Thompson   // Cleanup
24742b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_data));
24752b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&fdm_basis));
24762b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i));
24772b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_fdm));
2478eaf62fffSJeremy L Thompson 
2479eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2480eaf62fffSJeremy L Thompson }
2481eaf62fffSJeremy L Thompson 
2482eaf62fffSJeremy L Thompson /// @}
2483