xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-preconditioning.c (revision b275c451f7d9cb51b3a6c202c002e8e25d861b8d)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3eaf62fffSJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5eaf62fffSJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7eaf62fffSJeremy L Thompson 
8ed9e99e6SJeremy L Thompson #include <assert.h>
92b730f8bSJeremy L Thompson #include <ceed-impl.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
112b730f8bSJeremy L Thompson #include <ceed/ceed.h>
122b730f8bSJeremy L Thompson #include <math.h>
13eaf62fffSJeremy L Thompson #include <stdbool.h>
14eaf62fffSJeremy L Thompson #include <stdio.h>
15eaf62fffSJeremy L Thompson #include <string.h>
16eaf62fffSJeremy L Thompson 
17eaf62fffSJeremy L Thompson /// @file
18eaf62fffSJeremy L Thompson /// Implementation of CeedOperator preconditioning interfaces
19eaf62fffSJeremy L Thompson 
20eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
21eaf62fffSJeremy L Thompson /// CeedOperator Library Internal Preconditioning Functions
22eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
23eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorDeveloper
24eaf62fffSJeremy L Thompson /// @{
25eaf62fffSJeremy L Thompson 
26eaf62fffSJeremy L Thompson /**
27ea61e9acSJeremy L Thompson   @brief Duplicate a CeedQFunction with a reference Ceed to fallback for advanced CeedOperator functionality
289e77b9c8SJeremy L Thompson 
2901ea9c81SJed Brown   @param[in]  fallback_ceed Ceed on which to create fallback CeedQFunction
309e77b9c8SJeremy L Thompson   @param[in]  qf            CeedQFunction to create fallback for
3101ea9c81SJed Brown   @param[out] qf_fallback   fallback CeedQFunction
329e77b9c8SJeremy L Thompson 
339e77b9c8SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
349e77b9c8SJeremy L Thompson 
359e77b9c8SJeremy L Thompson   @ref Developer
369e77b9c8SJeremy L Thompson **/
372b730f8bSJeremy L Thompson static int CeedQFunctionCreateFallback(Ceed fallback_ceed, CeedQFunction qf, CeedQFunction *qf_fallback) {
389e77b9c8SJeremy L Thompson   // Check if NULL qf passed in
399e77b9c8SJeremy L Thompson   if (!qf) return CEED_ERROR_SUCCESS;
409e77b9c8SJeremy L Thompson 
41d04bbc78SJeremy L Thompson   CeedDebug256(qf->ceed, 1, "---------- CeedOperator Fallback ----------\n");
4213f886e9SJeremy L Thompson   CeedDebug(qf->ceed, "Creating fallback CeedQFunction\n");
43d04bbc78SJeremy L Thompson 
449e77b9c8SJeremy L Thompson   char *source_path_with_name = "";
459e77b9c8SJeremy L Thompson   if (qf->source_path) {
462b730f8bSJeremy L Thompson     size_t path_len = strlen(qf->source_path), name_len = strlen(qf->kernel_name);
472b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(path_len + name_len + 2, &source_path_with_name));
489e77b9c8SJeremy L Thompson     memcpy(source_path_with_name, qf->source_path, path_len);
499e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len], ":", 1);
509e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len + 1], qf->kernel_name, name_len);
519e77b9c8SJeremy L Thompson   } else {
522b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &source_path_with_name));
539e77b9c8SJeremy L Thompson   }
549e77b9c8SJeremy L Thompson 
552b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(fallback_ceed, qf->vec_length, qf->function, source_path_with_name, qf_fallback));
569e77b9c8SJeremy L Thompson   {
579e77b9c8SJeremy L Thompson     CeedQFunctionContext ctx;
589e77b9c8SJeremy L Thompson 
592b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
602b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx));
619e77b9c8SJeremy L Thompson   }
629e77b9c8SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
632b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(*qf_fallback, qf->input_fields[i]->field_name, qf->input_fields[i]->size, qf->input_fields[i]->eval_mode));
649e77b9c8SJeremy L Thompson   }
659e77b9c8SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
662b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(*qf_fallback, qf->output_fields[i]->field_name, qf->output_fields[i]->size, qf->output_fields[i]->eval_mode));
679e77b9c8SJeremy L Thompson   }
682b730f8bSJeremy L Thompson   CeedCall(CeedFree(&source_path_with_name));
699e77b9c8SJeremy L Thompson 
709e77b9c8SJeremy L Thompson   return CEED_ERROR_SUCCESS;
719e77b9c8SJeremy L Thompson }
729e77b9c8SJeremy L Thompson 
739e77b9c8SJeremy L Thompson /**
74ea61e9acSJeremy L Thompson   @brief Duplicate a CeedOperator with a reference Ceed to fallback for advanced CeedOperator functionality
75eaf62fffSJeremy L Thompson 
76ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to create fallback for
77eaf62fffSJeremy L Thompson 
78eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
79eaf62fffSJeremy L Thompson 
80eaf62fffSJeremy L Thompson   @ref Developer
81eaf62fffSJeremy L Thompson **/
82d04bbc78SJeremy L Thompson static int CeedOperatorCreateFallback(CeedOperator op) {
83*b275c451SJeremy 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;
98*b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
99*b275c451SJeremy L Thompson   if (is_composite) {
100*b275c451SJeremy L Thompson     CeedInt       num_suboperators;
101*b275c451SJeremy L Thompson     CeedOperator *sub_operators;
102*b275c451SJeremy L Thompson 
1032b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorCreate(ceed_fallback, &op_fallback));
104*b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
105*b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
106*b275c451SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
107d04bbc78SJeremy L Thompson       CeedOperator op_sub_fallback;
108d04bbc78SJeremy L Thompson 
109*b275c451SJeremy 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++) {
1192b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, op->input_fields[i]->field_name, op->input_fields[i]->elem_restr, 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++) {
1232b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, op->output_fields[i]->field_name, op->output_fields[i]->elem_restr, op->output_fields[i]->basis,
1242b730f8bSJeremy L Thompson                                     op->output_fields[i]->vec));
125805fe78eSJeremy L Thompson     }
1262b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op->qf_assembled, &op_fallback->qf_assembled));
127805fe78eSJeremy L Thompson     if (op_fallback->num_qpts == 0) {
1282b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetNumQuadraturePoints(op_fallback, op->num_qpts));
129805fe78eSJeremy L Thompson     }
1309e77b9c8SJeremy L Thompson     // Cleanup
1312b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf_fallback));
1322b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqf_fallback));
1332b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqfT_fallback));
134805fe78eSJeremy L Thompson   }
1352b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetName(op_fallback, op->name));
1362b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fallback));
137805fe78eSJeremy L Thompson   op->op_fallback = op_fallback;
138eaf62fffSJeremy L Thompson 
139eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
140eaf62fffSJeremy L Thompson }
141eaf62fffSJeremy L Thompson 
142eaf62fffSJeremy L Thompson /**
143ea61e9acSJeremy L Thompson   @brief Retrieve fallback CeedOperator with a reference Ceed for advanced CeedOperator functionality
144d04bbc78SJeremy L Thompson 
145d04bbc78SJeremy L Thompson   @param[in]  op          CeedOperator to retrieve fallback for
146d04bbc78SJeremy L Thompson   @param[out] op_fallback Fallback CeedOperator
147d04bbc78SJeremy L Thompson 
148d04bbc78SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
149d04bbc78SJeremy L Thompson 
150d04bbc78SJeremy L Thompson   @ref Developer
151d04bbc78SJeremy L Thompson **/
152d04bbc78SJeremy L Thompson int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) {
153d04bbc78SJeremy L Thompson   // Create if needed
154d04bbc78SJeremy L Thompson   if (!op->op_fallback) {
1552b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreateFallback(op));
156d04bbc78SJeremy L Thompson   }
157d04bbc78SJeremy L Thompson   if (op->op_fallback) {
158d04bbc78SJeremy L Thompson     bool is_debug;
159d04bbc78SJeremy L Thompson 
1602b730f8bSJeremy L Thompson     CeedCall(CeedIsDebug(op->ceed, &is_debug));
161d04bbc78SJeremy L Thompson     if (is_debug) {
162*b275c451SJeremy L Thompson       Ceed        ceed, ceed_fallback;
163d04bbc78SJeremy L Thompson       const char *resource, *resource_fallback;
164d04bbc78SJeremy L Thompson 
165*b275c451SJeremy L Thompson       CeedCall(CeedOperatorGetCeed(op, &ceed));
166*b275c451SJeremy L Thompson       CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
167*b275c451SJeremy L Thompson       CeedCall(CeedGetResource(ceed, &resource));
1682b730f8bSJeremy L Thompson       CeedCall(CeedGetResource(ceed_fallback, &resource_fallback));
169d04bbc78SJeremy L Thompson 
170*b275c451SJeremy L Thompson       CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n");
171*b275c451SJeremy L Thompson       CeedDebug(ceed, "Falling back from %s operator at address %ld to %s operator at address %ld\n", resource, op, resource_fallback,
1722b730f8bSJeremy L Thompson                 op->op_fallback);
173d04bbc78SJeremy L Thompson     }
174d04bbc78SJeremy L Thompson   }
175d04bbc78SJeremy L Thompson   *op_fallback = op->op_fallback;
176d04bbc78SJeremy L Thompson 
177d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
178d04bbc78SJeremy L Thompson }
179d04bbc78SJeremy L Thompson 
180d04bbc78SJeremy L Thompson /**
181eaf62fffSJeremy L Thompson   @brief Select correct basis matrix pointer based on CeedEvalMode
182eaf62fffSJeremy L Thompson 
183eaf62fffSJeremy L Thompson   @param[in]  eval_mode Current basis evaluation mode
184eaf62fffSJeremy L Thompson   @param[in]  identity  Pointer to identity matrix
185eaf62fffSJeremy L Thompson   @param[in]  interp    Pointer to interpolation matrix
186eaf62fffSJeremy L Thompson   @param[in]  grad      Pointer to gradient matrix
187eaf62fffSJeremy L Thompson   @param[out] basis_ptr Basis pointer to set
188eaf62fffSJeremy L Thompson 
189eaf62fffSJeremy L Thompson   @ref Developer
190eaf62fffSJeremy L Thompson **/
1912b730f8bSJeremy L Thompson static inline void CeedOperatorGetBasisPointer(CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar *interp, const CeedScalar *grad,
1922b730f8bSJeremy L Thompson                                                const CeedScalar **basis_ptr) {
193eaf62fffSJeremy L Thompson   switch (eval_mode) {
194eaf62fffSJeremy L Thompson     case CEED_EVAL_NONE:
195eaf62fffSJeremy L Thompson       *basis_ptr = identity;
196eaf62fffSJeremy L Thompson       break;
197eaf62fffSJeremy L Thompson     case CEED_EVAL_INTERP:
198eaf62fffSJeremy L Thompson       *basis_ptr = interp;
199eaf62fffSJeremy L Thompson       break;
200eaf62fffSJeremy L Thompson     case CEED_EVAL_GRAD:
201eaf62fffSJeremy L Thompson       *basis_ptr = grad;
202eaf62fffSJeremy L Thompson       break;
203eaf62fffSJeremy L Thompson     case CEED_EVAL_WEIGHT:
204eaf62fffSJeremy L Thompson     case CEED_EVAL_DIV:
205eaf62fffSJeremy L Thompson     case CEED_EVAL_CURL:
206eaf62fffSJeremy L Thompson       break;  // Caught by QF Assembly
207eaf62fffSJeremy L Thompson   }
208ed9e99e6SJeremy L Thompson   assert(*basis_ptr != NULL);
209eaf62fffSJeremy L Thompson }
210eaf62fffSJeremy L Thompson 
211eaf62fffSJeremy L Thompson /**
212eaf62fffSJeremy L Thompson   @brief Create point block restriction for active operator field
213eaf62fffSJeremy L Thompson 
214eaf62fffSJeremy L Thompson   @param[in]  rstr            Original CeedElemRestriction for active field
215ea61e9acSJeremy L Thompson   @param[out] pointblock_rstr Address of the variable where the newly created CeedElemRestriction will be stored
216eaf62fffSJeremy L Thompson 
217eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
218eaf62fffSJeremy L Thompson 
219eaf62fffSJeremy L Thompson   @ref Developer
220eaf62fffSJeremy L Thompson **/
2212b730f8bSJeremy L Thompson static int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *pointblock_rstr) {
222eaf62fffSJeremy L Thompson   Ceed ceed;
2232b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
224eaf62fffSJeremy L Thompson   const CeedInt *offsets;
2252b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
226eaf62fffSJeremy L Thompson 
227eaf62fffSJeremy L Thompson   // Expand offsets
2287b63f5c6SJed Brown   CeedInt  num_elem, num_comp, elem_size, comp_stride, *pointblock_offsets;
2297b63f5c6SJed Brown   CeedSize l_size;
2302b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
2312b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
2322b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
2332b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
2342b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
235eaf62fffSJeremy L Thompson   CeedInt shift = num_comp;
2362b730f8bSJeremy L Thompson   if (comp_stride != 1) shift *= num_comp;
2372b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_elem * elem_size, &pointblock_offsets));
238eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_elem * elem_size; i++) {
239eaf62fffSJeremy L Thompson     pointblock_offsets[i] = offsets[i] * shift;
240eaf62fffSJeremy L Thompson   }
241eaf62fffSJeremy L Thompson 
242eaf62fffSJeremy L Thompson   // Create new restriction
2432b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
2442b730f8bSJeremy L Thompson                                      pointblock_offsets, pointblock_rstr));
245eaf62fffSJeremy L Thompson 
246eaf62fffSJeremy L Thompson   // Cleanup
2472b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
248eaf62fffSJeremy L Thompson 
249eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
250eaf62fffSJeremy L Thompson }
251eaf62fffSJeremy L Thompson 
252eaf62fffSJeremy L Thompson /**
253eaf62fffSJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
254eaf62fffSJeremy L Thompson 
255eaf62fffSJeremy L Thompson   @param[in]  op            CeedOperator to assemble point block diagonal
256ea61e9acSJeremy L Thompson   @param[in]  request       Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE
257eaf62fffSJeremy L Thompson   @param[in]  is_pointblock Boolean flag to assemble diagonal or point block diagonal
258eaf62fffSJeremy L Thompson   @param[out] assembled     CeedVector to store assembled diagonal
259eaf62fffSJeremy L Thompson 
260eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
261eaf62fffSJeremy L Thompson 
262eaf62fffSJeremy L Thompson   @ref Developer
263eaf62fffSJeremy L Thompson **/
2642b730f8bSJeremy L Thompson static inline int CeedSingleOperatorAssembleAddDiagonal_Core(CeedOperator op, CeedRequest *request, const bool is_pointblock, CeedVector assembled) {
265eaf62fffSJeremy L Thompson   Ceed ceed;
2662b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
267eaf62fffSJeremy L Thompson 
268eaf62fffSJeremy L Thompson   // Assemble QFunction
269eaf62fffSJeremy L Thompson   CeedQFunction qf;
2702b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
271eaf62fffSJeremy L Thompson   CeedInt num_input_fields, num_output_fields;
2722b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
273eaf62fffSJeremy L Thompson   CeedVector          assembled_qf;
274eaf62fffSJeremy L Thompson   CeedElemRestriction rstr;
2752b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr, request));
276eaf62fffSJeremy L Thompson   CeedInt layout[3];
2772b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr, &layout));
2782b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr));
279eaf62fffSJeremy L Thompson 
280ed9e99e6SJeremy L Thompson   // Get assembly data
281ed9e99e6SJeremy L Thompson   CeedOperatorAssemblyData data;
2822b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
283ed9e99e6SJeremy L Thompson   const CeedEvalMode *eval_mode_in, *eval_mode_out;
284ed9e99e6SJeremy L Thompson   CeedInt             num_eval_mode_in, num_eval_mode_out;
2852b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_eval_mode_in, &eval_mode_in, &num_eval_mode_out, &eval_mode_out));
286ed9e99e6SJeremy L Thompson   CeedBasis basis_in, basis_out;
2872b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, &basis_in, NULL, &basis_out, NULL));
288ed9e99e6SJeremy L Thompson   CeedInt num_comp;
2892b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp));
290eaf62fffSJeremy L Thompson 
291eaf62fffSJeremy L Thompson   // Assemble point block diagonal restriction, if needed
292ed9e99e6SJeremy L Thompson   CeedElemRestriction diag_rstr;
2932b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &diag_rstr));
294eaf62fffSJeremy L Thompson   if (is_pointblock) {
295ed9e99e6SJeremy L Thompson     CeedElemRestriction point_block_rstr;
2962b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreateActivePointBlockRestriction(diag_rstr, &point_block_rstr));
297ed9e99e6SJeremy L Thompson     diag_rstr = point_block_rstr;
298eaf62fffSJeremy L Thompson   }
299eaf62fffSJeremy L Thompson 
300eaf62fffSJeremy L Thompson   // Create diagonal vector
301eaf62fffSJeremy L Thompson   CeedVector elem_diag;
3022b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreateVector(diag_rstr, NULL, &elem_diag));
303eaf62fffSJeremy L Thompson 
304eaf62fffSJeremy L Thompson   // Assemble element operator diagonals
3059c774eddSJeremy L Thompson   CeedScalar       *elem_diag_array;
3069c774eddSJeremy L Thompson   const CeedScalar *assembled_qf_array;
3072b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(elem_diag, 0.0));
3082b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array));
3092b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
310eaf62fffSJeremy L Thompson   CeedInt num_elem, num_nodes, num_qpts;
3112b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
3122b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis_in, &num_nodes));
3132b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
314ed9e99e6SJeremy L Thompson 
315eaf62fffSJeremy L Thompson   // Basis matrices
316eaf62fffSJeremy L Thompson   const CeedScalar *interp_in, *interp_out, *grad_in, *grad_out;
317eaf62fffSJeremy L Thompson   CeedScalar       *identity      = NULL;
318ed9e99e6SJeremy L Thompson   bool              has_eval_none = false;
319ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_eval_mode_in; i++) {
320ed9e99e6SJeremy L Thompson     has_eval_none = has_eval_none || (eval_mode_in[i] == CEED_EVAL_NONE);
321ed9e99e6SJeremy L Thompson   }
322ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_eval_mode_out; i++) {
323ed9e99e6SJeremy L Thompson     has_eval_none = has_eval_none || (eval_mode_out[i] == CEED_EVAL_NONE);
324ed9e99e6SJeremy L Thompson   }
325ed9e99e6SJeremy L Thompson   if (has_eval_none) {
3262b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
3272b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
328eaf62fffSJeremy L Thompson   }
3292b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp(basis_in, &interp_in));
3302b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp(basis_out, &interp_out));
3312b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad(basis_in, &grad_in));
3322b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad(basis_out, &grad_out));
333eaf62fffSJeremy L Thompson   // Compute the diagonal of B^T D B
334eaf62fffSJeremy L Thompson   // Each element
335eaf62fffSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
336eaf62fffSJeremy L Thompson     CeedInt d_out = -1;
337eaf62fffSJeremy L Thompson     // Each basis eval mode pair
338eaf62fffSJeremy L Thompson     for (CeedInt e_out = 0; e_out < num_eval_mode_out; e_out++) {
339eaf62fffSJeremy L Thompson       const CeedScalar *bt = NULL;
3402b730f8bSJeremy L Thompson       if (eval_mode_out[e_out] == CEED_EVAL_GRAD) d_out += 1;
3412b730f8bSJeremy L Thompson       CeedOperatorGetBasisPointer(eval_mode_out[e_out], identity, interp_out, &grad_out[d_out * num_qpts * num_nodes], &bt);
342eaf62fffSJeremy L Thompson       CeedInt d_in = -1;
343eaf62fffSJeremy L Thompson       for (CeedInt e_in = 0; e_in < num_eval_mode_in; e_in++) {
344eaf62fffSJeremy L Thompson         const CeedScalar *b = NULL;
3452b730f8bSJeremy L Thompson         if (eval_mode_in[e_in] == CEED_EVAL_GRAD) d_in += 1;
3462b730f8bSJeremy L Thompson         CeedOperatorGetBasisPointer(eval_mode_in[e_in], identity, interp_in, &grad_in[d_in * num_qpts * num_nodes], &b);
347eaf62fffSJeremy L Thompson         // Each component
3482b730f8bSJeremy L Thompson         for (CeedInt c_out = 0; c_out < num_comp; c_out++) {
349eaf62fffSJeremy L Thompson           // Each qpoint/node pair
3502b730f8bSJeremy L Thompson           for (CeedInt q = 0; q < num_qpts; q++) {
351eaf62fffSJeremy L Thompson             if (is_pointblock) {
352eaf62fffSJeremy L Thompson               // Point Block Diagonal
353eaf62fffSJeremy L Thompson               for (CeedInt c_in = 0; c_in < num_comp; c_in++) {
354eaf62fffSJeremy L Thompson                 const CeedScalar qf_value =
3552b730f8bSJeremy L Thompson                     assembled_qf_array[q * layout[0] + (((e_in * num_comp + c_in) * num_eval_mode_out + e_out) * num_comp + c_out) * layout[1] +
3562b730f8bSJeremy L Thompson                                        e * layout[2]];
3572b730f8bSJeremy L Thompson                 for (CeedInt n = 0; n < num_nodes; n++) {
358eaf62fffSJeremy L Thompson                   elem_diag_array[((e * num_comp + c_out) * num_comp + c_in) * num_nodes + n] +=
359eaf62fffSJeremy L Thompson                       bt[q * num_nodes + n] * qf_value * b[q * num_nodes + n];
360eaf62fffSJeremy L Thompson                 }
3612b730f8bSJeremy L Thompson               }
362eaf62fffSJeremy L Thompson             } else {
363eaf62fffSJeremy L Thompson               // Diagonal Only
364eaf62fffSJeremy L Thompson               const CeedScalar qf_value =
3652b730f8bSJeremy L Thompson                   assembled_qf_array[q * layout[0] + (((e_in * num_comp + c_out) * num_eval_mode_out + e_out) * num_comp + c_out) * layout[1] +
3662b730f8bSJeremy L Thompson                                      e * layout[2]];
3672b730f8bSJeremy L Thompson               for (CeedInt n = 0; n < num_nodes; n++) {
3682b730f8bSJeremy L Thompson                 elem_diag_array[(e * num_comp + c_out) * num_nodes + n] += bt[q * num_nodes + n] * qf_value * b[q * num_nodes + n];
369eaf62fffSJeremy L Thompson               }
370eaf62fffSJeremy L Thompson             }
371eaf62fffSJeremy L Thompson           }
372eaf62fffSJeremy L Thompson         }
3732b730f8bSJeremy L Thompson       }
3742b730f8bSJeremy L Thompson     }
3752b730f8bSJeremy L Thompson   }
3762b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
3772b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
378eaf62fffSJeremy L Thompson 
379eaf62fffSJeremy L Thompson   // Assemble local operator diagonal
3802b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
381eaf62fffSJeremy L Thompson 
382eaf62fffSJeremy L Thompson   // Cleanup
383eaf62fffSJeremy L Thompson   if (is_pointblock) {
3842b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&diag_rstr));
385eaf62fffSJeremy L Thompson   }
3862b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
3872b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&elem_diag));
3882b730f8bSJeremy L Thompson   CeedCall(CeedFree(&identity));
389eaf62fffSJeremy L Thompson 
390eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
391eaf62fffSJeremy L Thompson }
392eaf62fffSJeremy L Thompson 
393eaf62fffSJeremy L Thompson /**
394eaf62fffSJeremy L Thompson   @brief Core logic for assembling composite operator diagonal
395eaf62fffSJeremy L Thompson 
396eaf62fffSJeremy L Thompson   @param[in]  op            CeedOperator to assemble point block diagonal
397ea61e9acSJeremy L Thompson   @param[in]  request       Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE
398eaf62fffSJeremy L Thompson   @param[in]  is_pointblock Boolean flag to assemble diagonal or point block diagonal
399eaf62fffSJeremy L Thompson   @param[out] assembled     CeedVector to store assembled diagonal
400eaf62fffSJeremy L Thompson 
401eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
402eaf62fffSJeremy L Thompson 
403eaf62fffSJeremy L Thompson   @ref Developer
404eaf62fffSJeremy L Thompson **/
4052b730f8bSJeremy L Thompson static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_pointblock,
406eaf62fffSJeremy L Thompson                                                                  CeedVector assembled) {
407eaf62fffSJeremy L Thompson   CeedInt       num_sub;
408eaf62fffSJeremy L Thompson   CeedOperator *suboperators;
409c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
410c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators));
411eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_sub; i++) {
4126aa95790SJeremy L Thompson     if (is_pointblock) {
4132b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request));
4146aa95790SJeremy L Thompson     } else {
4152b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request));
4166aa95790SJeremy L Thompson     }
417eaf62fffSJeremy L Thompson   }
418eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
419eaf62fffSJeremy L Thompson }
420eaf62fffSJeremy L Thompson 
421eaf62fffSJeremy L Thompson /**
422eaf62fffSJeremy L Thompson   @brief Build nonzero pattern for non-composite operator
423eaf62fffSJeremy L Thompson 
424eaf62fffSJeremy L Thompson   Users should generally use CeedOperatorLinearAssembleSymbolic()
425eaf62fffSJeremy L Thompson 
426eaf62fffSJeremy L Thompson   @param[in]  op     CeedOperator to assemble nonzero pattern
427eaf62fffSJeremy L Thompson   @param[in]  offset Offset for number of entries
428eaf62fffSJeremy L Thompson   @param[out] rows   Row number for each entry
429eaf62fffSJeremy L Thompson   @param[out] cols   Column number for each entry
430eaf62fffSJeremy L Thompson 
431eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
432eaf62fffSJeremy L Thompson 
433eaf62fffSJeremy L Thompson   @ref Developer
434eaf62fffSJeremy L Thompson **/
4352b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) {
436f3d47e36SJeremy L Thompson   Ceed ceed;
437f3d47e36SJeremy L Thompson   bool is_composite;
438f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
439f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
440f3d47e36SJeremy L Thompson 
441*b275c451SJeremy L Thompson   if (is_composite) {
442eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
4432b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
444eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
4452b730f8bSJeremy L Thompson   }
446eaf62fffSJeremy L Thompson 
447c9366a6bSJeremy L Thompson   CeedSize num_nodes;
4482b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes, NULL));
449eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_in;
4502b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &rstr_in));
451e79b91d9SJeremy L Thompson   CeedInt num_elem, elem_size, num_comp;
4522b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem));
4532b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size));
4542b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp));
455eaf62fffSJeremy L Thompson   CeedInt layout_er[3];
4562b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_in, &layout_er));
457eaf62fffSJeremy L Thompson 
458eaf62fffSJeremy L Thompson   CeedInt local_num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
459eaf62fffSJeremy L Thompson 
460eaf62fffSJeremy L Thompson   // Determine elem_dof relation
461eaf62fffSJeremy L Thompson   CeedVector index_vec;
4622b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, num_nodes, &index_vec));
463eaf62fffSJeremy L Thompson   CeedScalar *array;
4642b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(index_vec, CEED_MEM_HOST, &array));
465ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_nodes; i++) array[i] = i;
4662b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(index_vec, &array));
467eaf62fffSJeremy L Thompson   CeedVector elem_dof;
4682b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, num_elem * elem_size * num_comp, &elem_dof));
4692b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(elem_dof, 0.0));
4702b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(rstr_in, CEED_NOTRANSPOSE, index_vec, elem_dof, CEED_REQUEST_IMMEDIATE));
471eaf62fffSJeremy L Thompson   const CeedScalar *elem_dof_a;
4722b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(elem_dof, CEED_MEM_HOST, &elem_dof_a));
4732b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&index_vec));
474eaf62fffSJeremy L Thompson 
475eaf62fffSJeremy L Thompson   // Determine i, j locations for element matrices
476eaf62fffSJeremy L Thompson   CeedInt count = 0;
477ed9e99e6SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
478ed9e99e6SJeremy L Thompson     for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
479ed9e99e6SJeremy L Thompson       for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
480ed9e99e6SJeremy L Thompson         for (CeedInt i = 0; i < elem_size; i++) {
481ed9e99e6SJeremy L Thompson           for (CeedInt j = 0; j < elem_size; j++) {
4822b730f8bSJeremy L Thompson             const CeedInt elem_dof_index_row = i * layout_er[0] + (comp_out)*layout_er[1] + e * layout_er[2];
4832b730f8bSJeremy L Thompson             const CeedInt elem_dof_index_col = j * layout_er[0] + comp_in * layout_er[1] + e * layout_er[2];
484eaf62fffSJeremy L Thompson 
485eaf62fffSJeremy L Thompson             const CeedInt row = elem_dof_a[elem_dof_index_row];
486eaf62fffSJeremy L Thompson             const CeedInt col = elem_dof_a[elem_dof_index_col];
487eaf62fffSJeremy L Thompson 
488eaf62fffSJeremy L Thompson             rows[offset + count] = row;
489eaf62fffSJeremy L Thompson             cols[offset + count] = col;
490eaf62fffSJeremy L Thompson             count++;
491eaf62fffSJeremy L Thompson           }
492eaf62fffSJeremy L Thompson         }
493eaf62fffSJeremy L Thompson       }
494eaf62fffSJeremy L Thompson     }
495eaf62fffSJeremy L Thompson   }
4962b730f8bSJeremy L Thompson   if (count != local_num_entries) {
497eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
498eaf62fffSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MAJOR, "Error computing assembled entries");
499eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
5002b730f8bSJeremy L Thompson   }
5012b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(elem_dof, &elem_dof_a));
5022b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&elem_dof));
503eaf62fffSJeremy L Thompson 
504eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
505eaf62fffSJeremy L Thompson }
506eaf62fffSJeremy L Thompson 
507eaf62fffSJeremy L Thompson /**
508eaf62fffSJeremy L Thompson   @brief Assemble nonzero entries for non-composite operator
509eaf62fffSJeremy L Thompson 
510eaf62fffSJeremy L Thompson   Users should generally use CeedOperatorLinearAssemble()
511eaf62fffSJeremy L Thompson 
512eaf62fffSJeremy L Thompson   @param[in]  op     CeedOperator to assemble
513ea61e9acSJeremy L Thompson   @param[in]  offset Offset for number of entries
514eaf62fffSJeremy L Thompson   @param[out] values Values to assemble into matrix
515eaf62fffSJeremy L Thompson 
516eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
517eaf62fffSJeremy L Thompson 
518eaf62fffSJeremy L Thompson   @ref Developer
519eaf62fffSJeremy L Thompson **/
5202b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) {
521f3d47e36SJeremy L Thompson   Ceed ceed;
522f3d47e36SJeremy L Thompson   bool is_composite;
523f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
524f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
525f3d47e36SJeremy L Thompson 
526f3d47e36SJeremy L Thompson   if (is_composite) {
527eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
5282b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
529eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
5302b730f8bSJeremy L Thompson   }
531f3d47e36SJeremy L Thompson 
532f3d47e36SJeremy L Thompson   // Early exit for empty operator
533f3d47e36SJeremy L Thompson   {
534f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
535f3d47e36SJeremy L Thompson 
536f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
537f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
538f3d47e36SJeremy L Thompson   }
539eaf62fffSJeremy L Thompson 
540cefa2673SJeremy L Thompson   if (op->LinearAssembleSingle) {
541cefa2673SJeremy L Thompson     // Backend version
5422b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSingle(op, offset, values));
543cefa2673SJeremy L Thompson     return CEED_ERROR_SUCCESS;
544cefa2673SJeremy L Thompson   } else {
545cefa2673SJeremy L Thompson     // Operator fallback
546cefa2673SJeremy L Thompson     CeedOperator op_fallback;
547cefa2673SJeremy L Thompson 
5482b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
549cefa2673SJeremy L Thompson     if (op_fallback) {
5502b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values));
551cefa2673SJeremy L Thompson       return CEED_ERROR_SUCCESS;
552cefa2673SJeremy L Thompson     }
553cefa2673SJeremy L Thompson   }
554cefa2673SJeremy L Thompson 
555eaf62fffSJeremy L Thompson   // Assemble QFunction
556eaf62fffSJeremy L Thompson   CeedQFunction qf;
5572b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
558eaf62fffSJeremy L Thompson   CeedVector          assembled_qf;
559eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_q;
5602b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE));
5611f9221feSJeremy L Thompson   CeedSize qf_length;
5622b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetLength(assembled_qf, &qf_length));
563eaf62fffSJeremy L Thompson 
5647e7773b5SJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
565eaf62fffSJeremy L Thompson   CeedOperatorField *input_fields;
566eaf62fffSJeremy L Thompson   CeedOperatorField *output_fields;
5672b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
568eaf62fffSJeremy L Thompson 
569ed9e99e6SJeremy L Thompson   // Get assembly data
570ed9e99e6SJeremy L Thompson   CeedOperatorAssemblyData data;
5712b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
572ed9e99e6SJeremy L Thompson   const CeedEvalMode *eval_mode_in, *eval_mode_out;
573ed9e99e6SJeremy L Thompson   CeedInt             num_eval_mode_in, num_eval_mode_out;
5742b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_eval_mode_in, &eval_mode_in, &num_eval_mode_out, &eval_mode_out));
575ed9e99e6SJeremy L Thompson   CeedBasis basis_in, basis_out;
5762b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, &basis_in, NULL, &basis_out, NULL));
577eaf62fffSJeremy L Thompson 
5782b730f8bSJeremy L Thompson   if (num_eval_mode_in == 0 || num_eval_mode_out == 0) {
579eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
5802b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator with out inputs/outputs");
581eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
5822b730f8bSJeremy L Thompson   }
583eaf62fffSJeremy L Thompson 
584ed9e99e6SJeremy L Thompson   CeedElemRestriction active_rstr;
585eaf62fffSJeremy L Thompson   CeedInt             num_elem, elem_size, num_qpts, num_comp;
5862b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &active_rstr));
5872b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(active_rstr, &num_elem));
5882b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(active_rstr, &elem_size));
5892b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(active_rstr, &num_comp));
5902b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
591eaf62fffSJeremy L Thompson 
592eaf62fffSJeremy L Thompson   CeedInt local_num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
593eaf62fffSJeremy L Thompson 
594eaf62fffSJeremy L Thompson   // loop over elements and put in data structure
595eaf62fffSJeremy L Thompson   const CeedScalar *interp_in, *grad_in;
5962b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp(basis_in, &interp_in));
5972b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad(basis_in, &grad_in));
598eaf62fffSJeremy L Thompson 
599eaf62fffSJeremy L Thompson   const CeedScalar *assembled_qf_array;
6002b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
601eaf62fffSJeremy L Thompson 
602eaf62fffSJeremy L Thompson   CeedInt layout_qf[3];
6032b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_q, &layout_qf));
6042b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_q));
605eaf62fffSJeremy L Thompson 
606eaf62fffSJeremy L Thompson   // we store B_mat_in, B_mat_out, BTD, elem_mat in row-major order
607ed9e99e6SJeremy L Thompson   const CeedScalar *B_mat_in, *B_mat_out;
6082b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &B_mat_in, NULL, &B_mat_out));
609ed9e99e6SJeremy L Thompson   CeedScalar  BTD_mat[elem_size * num_qpts * num_eval_mode_in];
610eaf62fffSJeremy L Thompson   CeedScalar  elem_mat[elem_size * elem_size];
61192ae7e47SJeremy L Thompson   CeedInt     count = 0;
612eaf62fffSJeremy L Thompson   CeedScalar *vals;
6132b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(values, CEED_MEM_HOST, &vals));
614ed9e99e6SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
615ed9e99e6SJeremy L Thompson     for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
616ed9e99e6SJeremy L Thompson       for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
617ed9e99e6SJeremy L Thompson         // Compute B^T*D
618ed9e99e6SJeremy L Thompson         for (CeedInt n = 0; n < elem_size; n++) {
619ed9e99e6SJeremy L Thompson           for (CeedInt q = 0; q < num_qpts; q++) {
620ed9e99e6SJeremy L Thompson             for (CeedInt e_in = 0; e_in < num_eval_mode_in; e_in++) {
6212b730f8bSJeremy L Thompson               const CeedInt btd_index = n * (num_qpts * num_eval_mode_in) + (num_eval_mode_in * q + e_in);
622067fd99fSJeremy L Thompson               CeedScalar    sum       = 0.0;
623067fd99fSJeremy L Thompson               for (CeedInt e_out = 0; e_out < num_eval_mode_out; e_out++) {
624ed9e99e6SJeremy L Thompson                 const CeedInt b_out_index     = (num_eval_mode_out * q + e_out) * elem_size + n;
6252b730f8bSJeremy L Thompson                 const CeedInt eval_mode_index = ((e_in * num_comp + comp_in) * num_eval_mode_out + e_out) * num_comp + comp_out;
6262b730f8bSJeremy L Thompson                 const CeedInt qf_index        = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2];
627067fd99fSJeremy L Thompson                 sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index];
628eaf62fffSJeremy L Thompson               }
629067fd99fSJeremy L Thompson               BTD_mat[btd_index] = sum;
630ed9e99e6SJeremy L Thompson             }
631ed9e99e6SJeremy L Thompson           }
632eaf62fffSJeremy L Thompson         }
633eaf62fffSJeremy L Thompson         // form element matrix itself (for each block component)
6342b730f8bSJeremy L Thompson         CeedCall(CeedMatrixMatrixMultiply(ceed, BTD_mat, B_mat_in, elem_mat, elem_size, elem_size, num_qpts * num_eval_mode_in));
635eaf62fffSJeremy L Thompson 
636eaf62fffSJeremy L Thompson         // put element matrix in coordinate data structure
637ed9e99e6SJeremy L Thompson         for (CeedInt i = 0; i < elem_size; i++) {
638ed9e99e6SJeremy L Thompson           for (CeedInt j = 0; j < elem_size; j++) {
639eaf62fffSJeremy L Thompson             vals[offset + count] = elem_mat[i * elem_size + j];
640eaf62fffSJeremy L Thompson             count++;
641eaf62fffSJeremy L Thompson           }
642eaf62fffSJeremy L Thompson         }
643eaf62fffSJeremy L Thompson       }
644eaf62fffSJeremy L Thompson     }
645eaf62fffSJeremy L Thompson   }
6462b730f8bSJeremy L Thompson   if (count != local_num_entries) {
647eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
648eaf62fffSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MAJOR, "Error computing entries");
649eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
6502b730f8bSJeremy L Thompson   }
6512b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(values, &vals));
652eaf62fffSJeremy L Thompson 
6532b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
6542b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
655eaf62fffSJeremy L Thompson 
656eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
657eaf62fffSJeremy L Thompson }
658eaf62fffSJeremy L Thompson 
659eaf62fffSJeremy L Thompson /**
660eaf62fffSJeremy L Thompson   @brief Count number of entries for assembled CeedOperator
661eaf62fffSJeremy L Thompson 
662eaf62fffSJeremy L Thompson   @param[in]  op          CeedOperator to assemble
663eaf62fffSJeremy L Thompson   @param[out] num_entries Number of entries in assembled representation
664eaf62fffSJeremy L Thompson 
665eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
666eaf62fffSJeremy L Thompson 
667eaf62fffSJeremy L Thompson   @ref Utility
668eaf62fffSJeremy L Thompson **/
6692b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedInt *num_entries) {
670*b275c451SJeremy L Thompson   bool                is_composite;
671eaf62fffSJeremy L Thompson   CeedElemRestriction rstr;
672eaf62fffSJeremy L Thompson   CeedInt             num_elem, elem_size, num_comp;
673eaf62fffSJeremy L Thompson 
674*b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
675*b275c451SJeremy L Thompson   if (is_composite) {
676eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
6772b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
678eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
6792b730f8bSJeremy L Thompson   }
6802b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &rstr));
6812b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
6822b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
6832b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
684eaf62fffSJeremy L Thompson   *num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
685eaf62fffSJeremy L Thompson 
686eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
687eaf62fffSJeremy L Thompson }
688eaf62fffSJeremy L Thompson 
689eaf62fffSJeremy L Thompson /**
690ea61e9acSJeremy L Thompson   @brief Common code for creating a multigrid coarse operator and level transfer operators for a CeedOperator
691eaf62fffSJeremy L Thompson 
692eaf62fffSJeremy L Thompson   @param[in]  op_fine      Fine grid operator
693eaf62fffSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter
694eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid restriction
695eaf62fffSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector basis
696eaf62fffSJeremy L Thompson   @param[in]  basis_c_to_f Basis for coarse to fine interpolation
697eaf62fffSJeremy L Thompson   @param[out] op_coarse    Coarse grid operator
698eaf62fffSJeremy L Thompson   @param[out] op_prolong   Coarse to fine operator
699eaf62fffSJeremy L Thompson   @param[out] op_restrict  Fine to coarse operator
700eaf62fffSJeremy L Thompson 
701eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
702eaf62fffSJeremy L Thompson 
703eaf62fffSJeremy L Thompson   @ref Developer
704eaf62fffSJeremy L Thompson **/
7052b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
7062b730f8bSJeremy L Thompson                                             CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
707eaf62fffSJeremy L Thompson   Ceed ceed;
7082b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
709eaf62fffSJeremy L Thompson 
710eaf62fffSJeremy L Thompson   // Check for composite operator
711eaf62fffSJeremy L Thompson   bool is_composite;
7122b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op_fine, &is_composite));
7132b730f8bSJeremy L Thompson   if (is_composite) {
714eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
7152b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported");
716eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
7172b730f8bSJeremy L Thompson   }
718eaf62fffSJeremy L Thompson 
719eaf62fffSJeremy L Thompson   // Coarse Grid
7202b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
721eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_fine = NULL;
722eaf62fffSJeremy L Thompson   // -- Clone input fields
72392ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < op_fine->qf->num_input_fields; i++) {
724eaf62fffSJeremy L Thompson     if (op_fine->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
725eaf62fffSJeremy L Thompson       rstr_fine = op_fine->input_fields[i]->elem_restr;
7262b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE));
727eaf62fffSJeremy L Thompson     } else {
7282b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, op_fine->input_fields[i]->elem_restr,
7292b730f8bSJeremy L Thompson                                     op_fine->input_fields[i]->basis, op_fine->input_fields[i]->vec));
730eaf62fffSJeremy L Thompson     }
731eaf62fffSJeremy L Thompson   }
732eaf62fffSJeremy L Thompson   // -- Clone output fields
73392ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < op_fine->qf->num_output_fields; i++) {
734eaf62fffSJeremy L Thompson     if (op_fine->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
7352b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE));
736eaf62fffSJeremy L Thompson     } else {
7372b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, op_fine->output_fields[i]->elem_restr,
7382b730f8bSJeremy L Thompson                                     op_fine->output_fields[i]->basis, op_fine->output_fields[i]->vec));
739eaf62fffSJeremy L Thompson     }
740eaf62fffSJeremy L Thompson   }
741af99e877SJeremy L Thompson   // -- Clone QFunctionAssemblyData
7422b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op_fine->qf_assembled, &(*op_coarse)->qf_assembled));
743eaf62fffSJeremy L Thompson 
744eaf62fffSJeremy L Thompson   // Multiplicity vector
745eaf62fffSJeremy L Thompson   CeedVector mult_vec, mult_e_vec;
7462b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec));
7472b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(mult_e_vec, 0.0));
7482b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(rstr_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE));
7492b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(mult_vec, 0.0));
7502b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(rstr_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE));
7512b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_e_vec));
7522b730f8bSJeremy L Thompson   CeedCall(CeedVectorReciprocal(mult_vec));
753eaf62fffSJeremy L Thompson 
754eaf62fffSJeremy L Thompson   // Restriction
755eaf62fffSJeremy L Thompson   CeedInt num_comp;
7562b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp));
757eaf62fffSJeremy L Thompson   CeedQFunction qf_restrict;
7582b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict));
759eaf62fffSJeremy L Thompson   CeedInt *num_comp_r_data;
7602b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, &num_comp_r_data));
761eaf62fffSJeremy L Thompson   num_comp_r_data[0] = num_comp;
762eaf62fffSJeremy L Thompson   CeedQFunctionContext ctx_r;
7632b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r));
7642b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data));
7652b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r));
7662b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_r));
7672b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE));
7682b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE));
7692b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP));
7702b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp));
771eaf62fffSJeremy L Thompson 
7722b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict));
7732b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE));
7742b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_fine, CEED_BASIS_COLLOCATED, mult_vec));
7752b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
776eaf62fffSJeremy L Thompson 
777eaf62fffSJeremy L Thompson   // Prolongation
778eaf62fffSJeremy L Thompson   CeedQFunction qf_prolong;
7792b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong));
780eaf62fffSJeremy L Thompson   CeedInt *num_comp_p_data;
7812b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, &num_comp_p_data));
782eaf62fffSJeremy L Thompson   num_comp_p_data[0] = num_comp;
783eaf62fffSJeremy L Thompson   CeedQFunctionContext ctx_p;
7842b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p));
7852b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data));
7862b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p));
7872b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_p));
7882b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP));
7892b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE));
7902b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE));
7912b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp));
792eaf62fffSJeremy L Thompson 
7932b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong));
7942b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
7952b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_fine, CEED_BASIS_COLLOCATED, mult_vec));
7962b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE));
797eaf62fffSJeremy L Thompson 
798ea6b5821SJeremy L Thompson   // Clone name
799ea6b5821SJeremy L Thompson   bool   has_name = op_fine->name;
800ea6b5821SJeremy L Thompson   size_t name_len = op_fine->name ? strlen(op_fine->name) : 0;
8012b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name));
802ea6b5821SJeremy L Thompson   {
803ea6b5821SJeremy L Thompson     char *prolongation_name;
8042b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(18 + name_len, &prolongation_name));
8052b730f8bSJeremy L Thompson     sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
8062b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name));
8072b730f8bSJeremy L Thompson     CeedCall(CeedFree(&prolongation_name));
808ea6b5821SJeremy L Thompson   }
809ea6b5821SJeremy L Thompson   {
810ea6b5821SJeremy L Thompson     char *restriction_name;
8112b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(17 + name_len, &restriction_name));
8122b730f8bSJeremy L Thompson     sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
8132b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_restrict, restriction_name));
8142b730f8bSJeremy L Thompson     CeedCall(CeedFree(&restriction_name));
815ea6b5821SJeremy L Thompson   }
816ea6b5821SJeremy L Thompson 
81758e4b056SJeremy L Thompson   // Check
81858e4b056SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(*op_coarse));
81958e4b056SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(*op_prolong));
82058e4b056SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(*op_restrict));
82158e4b056SJeremy L Thompson 
822eaf62fffSJeremy L Thompson   // Cleanup
8232b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_vec));
8242b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis_c_to_f));
8252b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_restrict));
8262b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_prolong));
827805fe78eSJeremy L Thompson 
828eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
829eaf62fffSJeremy L Thompson }
830eaf62fffSJeremy L Thompson 
831eaf62fffSJeremy L Thompson /**
832eaf62fffSJeremy L Thompson   @brief Build 1D mass matrix and Laplacian with perturbation
833eaf62fffSJeremy L Thompson 
834eaf62fffSJeremy L Thompson   @param[in]  interp_1d   Interpolation matrix in one dimension
835eaf62fffSJeremy L Thompson   @param[in]  grad_1d     Gradient matrix in one dimension
836eaf62fffSJeremy L Thompson   @param[in]  q_weight_1d Quadrature weights in one dimension
837eaf62fffSJeremy L Thompson   @param[in]  P_1d        Number of basis nodes in one dimension
838eaf62fffSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
839eaf62fffSJeremy L Thompson   @param[in]  dim         Dimension of basis
840eaf62fffSJeremy L Thompson   @param[out] mass        Assembled mass matrix in one dimension
841eaf62fffSJeremy L Thompson   @param[out] laplace     Assembled perturbed Laplacian in one dimension
842eaf62fffSJeremy L Thompson 
843eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
844eaf62fffSJeremy L Thompson 
845eaf62fffSJeremy L Thompson   @ref Developer
846eaf62fffSJeremy L Thompson **/
8472b730f8bSJeremy L Thompson CeedPragmaOptimizeOff static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d,
8482b730f8bSJeremy L Thompson                                                       CeedInt P_1d, CeedInt Q_1d, CeedInt dim, CeedScalar *mass, CeedScalar *laplace) {
8492b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
850eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
851eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
8522b730f8bSJeremy 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];
853eaf62fffSJeremy L Thompson       mass[i + j * P_1d] = sum;
854eaf62fffSJeremy L Thompson     }
8552b730f8bSJeremy L Thompson   }
856eaf62fffSJeremy L Thompson   // -- Laplacian
8572b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
858eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
859eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
8602b730f8bSJeremy 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];
861eaf62fffSJeremy L Thompson       laplace[i + j * P_1d] = sum;
862eaf62fffSJeremy L Thompson     }
8632b730f8bSJeremy L Thompson   }
864eaf62fffSJeremy L Thompson   CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4;
8652b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation;
866eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
867eaf62fffSJeremy L Thompson }
868ea61e9acSJeremy L Thompson CeedPragmaOptimizeOn;
869eaf62fffSJeremy L Thompson 
870eaf62fffSJeremy L Thompson /// @}
871eaf62fffSJeremy L Thompson 
872eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
873480fae85SJeremy L Thompson /// CeedOperator Backend API
874480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
875480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend
876480fae85SJeremy L Thompson /// @{
877480fae85SJeremy L Thompson 
878480fae85SJeremy L Thompson /**
879480fae85SJeremy L Thompson   @brief Create object holding CeedQFunction assembly data for CeedOperator
880480fae85SJeremy L Thompson 
881480fae85SJeremy L Thompson   @param[in]  ceed A Ceed object where the CeedQFunctionAssemblyData will be created
882ea61e9acSJeremy L Thompson   @param[out] data Address of the variable where the newly created CeedQFunctionAssemblyData will be stored
883480fae85SJeremy L Thompson 
884480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
885480fae85SJeremy L Thompson 
886480fae85SJeremy L Thompson   @ref Backend
887480fae85SJeremy L Thompson **/
888ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) {
8892b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
890480fae85SJeremy L Thompson   (*data)->ref_count = 1;
891480fae85SJeremy L Thompson   (*data)->ceed      = ceed;
8922b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
893480fae85SJeremy L Thompson 
894480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
895480fae85SJeremy L Thompson }
896480fae85SJeremy L Thompson 
897480fae85SJeremy L Thompson /**
898480fae85SJeremy L Thompson   @brief Increment the reference counter for a CeedQFunctionAssemblyData
899480fae85SJeremy L Thompson 
900ea61e9acSJeremy L Thompson   @param[in,out] data CeedQFunctionAssemblyData to increment the reference counter
901480fae85SJeremy L Thompson 
902480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
903480fae85SJeremy L Thompson 
904480fae85SJeremy L Thompson   @ref Backend
905480fae85SJeremy L Thompson **/
906480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) {
907480fae85SJeremy L Thompson   data->ref_count++;
908480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
909480fae85SJeremy L Thompson }
910480fae85SJeremy L Thompson 
911480fae85SJeremy L Thompson /**
912beecbf24SJeremy L Thompson   @brief Set re-use of CeedQFunctionAssemblyData
9138b919e6bSJeremy L Thompson 
914ea61e9acSJeremy L Thompson   @param[in,out] data       CeedQFunctionAssemblyData to mark for reuse
915ea61e9acSJeremy L Thompson   @param[in]     reuse_data Boolean flag indicating data re-use
9168b919e6bSJeremy L Thompson 
9178b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9188b919e6bSJeremy L Thompson 
9198b919e6bSJeremy L Thompson   @ref Backend
9208b919e6bSJeremy L Thompson **/
9212b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) {
922beecbf24SJeremy L Thompson   data->reuse_data        = reuse_data;
923beecbf24SJeremy L Thompson   data->needs_data_update = true;
924beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
925beecbf24SJeremy L Thompson }
926beecbf24SJeremy L Thompson 
927beecbf24SJeremy L Thompson /**
928beecbf24SJeremy L Thompson   @brief Mark QFunctionAssemblyData as stale
929beecbf24SJeremy L Thompson 
930ea61e9acSJeremy L Thompson   @param[in,out] data              CeedQFunctionAssemblyData to mark as stale
931ea61e9acSJeremy L Thompson   @param[in]     needs_data_update Boolean flag indicating if update is needed or completed
932beecbf24SJeremy L Thompson 
933beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
934beecbf24SJeremy L Thompson 
935beecbf24SJeremy L Thompson   @ref Backend
936beecbf24SJeremy L Thompson **/
9372b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) {
938beecbf24SJeremy L Thompson   data->needs_data_update = needs_data_update;
9398b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
9408b919e6bSJeremy L Thompson }
9418b919e6bSJeremy L Thompson 
9428b919e6bSJeremy L Thompson /**
9438b919e6bSJeremy L Thompson   @brief Determine if QFunctionAssemblyData needs update
9448b919e6bSJeremy L Thompson 
9458b919e6bSJeremy L Thompson   @param[in]  data             CeedQFunctionAssemblyData to mark as stale
9468b919e6bSJeremy L Thompson   @param[out] is_update_needed Boolean flag indicating if re-assembly is required
9478b919e6bSJeremy L Thompson 
9488b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9498b919e6bSJeremy L Thompson 
9508b919e6bSJeremy L Thompson   @ref Backend
9518b919e6bSJeremy L Thompson **/
9522b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) {
953beecbf24SJeremy L Thompson   *is_update_needed = !data->reuse_data || data->needs_data_update;
9548b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
9558b919e6bSJeremy L Thompson }
9568b919e6bSJeremy L Thompson 
9578b919e6bSJeremy L Thompson /**
958ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedQFunctionAssemblyData.
959ea61e9acSJeremy L Thompson            Both pointers should be destroyed with `CeedCeedQFunctionAssemblyDataDestroy()`.
960ea61e9acSJeremy L Thompson            Note: If `*data_copy` is non-NULL, then it is assumed that `*data_copy` is a pointer to a CeedQFunctionAssemblyData.
961ea61e9acSJeremy L Thompson              This CeedQFunctionAssemblyData will be destroyed if `*data_copy` is the only reference to this CeedQFunctionAssemblyData.
962480fae85SJeremy L Thompson 
963ea61e9acSJeremy L Thompson   @param[in]     data      CeedQFunctionAssemblyData to copy reference to
964ea61e9acSJeremy L Thompson   @param[in,out] data_copy Variable to store copied reference
965480fae85SJeremy L Thompson 
966480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
967480fae85SJeremy L Thompson 
968480fae85SJeremy L Thompson   @ref Backend
969480fae85SJeremy L Thompson **/
9702b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) {
9712b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReference(data));
9722b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy));
973480fae85SJeremy L Thompson   *data_copy = data;
974480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
975480fae85SJeremy L Thompson }
976480fae85SJeremy L Thompson 
977480fae85SJeremy L Thompson /**
978480fae85SJeremy L Thompson   @brief Get setup status for internal objects for CeedQFunctionAssemblyData
979480fae85SJeremy L Thompson 
980ea61e9acSJeremy L Thompson   @param[in]  data     CeedQFunctionAssemblyData to retrieve status
981480fae85SJeremy L Thompson   @param[out] is_setup Boolean flag for setup status
982480fae85SJeremy L Thompson 
983480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
984480fae85SJeremy L Thompson 
985480fae85SJeremy L Thompson   @ref Backend
986480fae85SJeremy L Thompson **/
9872b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) {
988480fae85SJeremy L Thompson   *is_setup = data->is_setup;
989480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
990480fae85SJeremy L Thompson }
991480fae85SJeremy L Thompson 
992480fae85SJeremy L Thompson /**
993480fae85SJeremy L Thompson   @brief Set internal objects for CeedQFunctionAssemblyData
994480fae85SJeremy L Thompson 
995ea61e9acSJeremy L Thompson   @param[in,out] data CeedQFunctionAssemblyData to set objects
996480fae85SJeremy L Thompson   @param[in]     vec  CeedVector to store assembled CeedQFunction at quadrature points
997480fae85SJeremy L Thompson   @param[in]     rstr CeedElemRestriction for CeedVector containing assembled CeedQFunction
998480fae85SJeremy L Thompson 
999480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1000480fae85SJeremy L Thompson 
1001480fae85SJeremy L Thompson   @ref Backend
1002480fae85SJeremy L Thompson **/
10032b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) {
10042b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(vec, &data->vec));
10052b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr));
1006480fae85SJeremy L Thompson 
1007480fae85SJeremy L Thompson   data->is_setup = true;
1008480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1009480fae85SJeremy L Thompson }
1010480fae85SJeremy L Thompson 
10112b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) {
10122b730f8bSJeremy L Thompson   if (!data->is_setup) {
1013480fae85SJeremy L Thompson     // LCOV_EXCL_START
10142b730f8bSJeremy L Thompson     return CeedError(data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first.");
1015480fae85SJeremy L Thompson     // LCOV_EXCL_STOP
10162b730f8bSJeremy L Thompson   }
1017480fae85SJeremy L Thompson 
10182b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(data->vec, vec));
10192b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr));
1020480fae85SJeremy L Thompson 
1021480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1022480fae85SJeremy L Thompson }
1023480fae85SJeremy L Thompson 
1024480fae85SJeremy L Thompson /**
1025480fae85SJeremy L Thompson   @brief Destroy CeedQFunctionAssemblyData
1026480fae85SJeremy L Thompson 
1027ea61e9acSJeremy L Thompson   @param[in,out] data  CeedQFunctionAssemblyData to destroy
1028480fae85SJeremy L Thompson 
1029480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1030480fae85SJeremy L Thompson 
1031480fae85SJeremy L Thompson   @ref Backend
1032480fae85SJeremy L Thompson **/
1033480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) {
1034480fae85SJeremy L Thompson   if (!*data || --(*data)->ref_count > 0) return CEED_ERROR_SUCCESS;
1035480fae85SJeremy L Thompson 
10362b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
10372b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*data)->vec));
10382b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr));
1039480fae85SJeremy L Thompson 
10402b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1041480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1042480fae85SJeremy L Thompson }
1043480fae85SJeremy L Thompson 
1044ed9e99e6SJeremy L Thompson /**
1045ed9e99e6SJeremy L Thompson   @brief Get CeedOperatorAssemblyData
1046ed9e99e6SJeremy L Thompson 
1047ed9e99e6SJeremy L Thompson   @param[in]  op   CeedOperator to assemble
1048ed9e99e6SJeremy L Thompson   @param[out] data CeedQFunctionAssemblyData
1049ed9e99e6SJeremy L Thompson 
1050ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1051ed9e99e6SJeremy L Thompson 
1052ed9e99e6SJeremy L Thompson   @ref Backend
1053ed9e99e6SJeremy L Thompson **/
10542b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) {
1055ed9e99e6SJeremy L Thompson   if (!op->op_assembled) {
1056ed9e99e6SJeremy L Thompson     CeedOperatorAssemblyData data;
1057ed9e99e6SJeremy L Thompson 
10582b730f8bSJeremy L Thompson     CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data));
1059ed9e99e6SJeremy L Thompson     op->op_assembled = data;
1060ed9e99e6SJeremy L Thompson   }
1061ed9e99e6SJeremy L Thompson   *data = op->op_assembled;
1062ed9e99e6SJeremy L Thompson 
1063ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1064ed9e99e6SJeremy L Thompson }
1065ed9e99e6SJeremy L Thompson 
1066ed9e99e6SJeremy L Thompson /**
1067ed9e99e6SJeremy L Thompson   @brief Create object holding CeedOperator assembly data
1068ed9e99e6SJeremy L Thompson 
1069ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperatorAssemblyData will be created
1070ed9e99e6SJeremy L Thompson   @param[in]  op   CeedOperator to be assembled
1071ea61e9acSJeremy L Thompson   @param[out] data Address of the variable where the newly created CeedOperatorAssemblyData will be stored
1072ed9e99e6SJeremy L Thompson 
1073ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1074ed9e99e6SJeremy L Thompson 
1075ed9e99e6SJeremy L Thompson   @ref Backend
1076ed9e99e6SJeremy L Thompson **/
10772b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) {
10782b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1079ed9e99e6SJeremy L Thompson   (*data)->ceed = ceed;
10802b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1081ed9e99e6SJeremy L Thompson 
1082ed9e99e6SJeremy L Thompson   // Build OperatorAssembly data
1083ed9e99e6SJeremy L Thompson   CeedQFunction       qf;
1084ed9e99e6SJeremy L Thompson   CeedQFunctionField *qf_fields;
1085ed9e99e6SJeremy L Thompson   CeedOperatorField  *op_fields;
1086ed9e99e6SJeremy L Thompson   CeedInt             num_input_fields;
10872b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
10882b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL));
10892b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1090ed9e99e6SJeremy L Thompson 
1091ed9e99e6SJeremy L Thompson   // Determine active input basis
1092ed9e99e6SJeremy L Thompson   CeedInt       num_eval_mode_in = 0, dim = 1;
1093ed9e99e6SJeremy L Thompson   CeedEvalMode *eval_mode_in = NULL;
1094ed9e99e6SJeremy L Thompson   CeedBasis     basis_in     = NULL;
1095ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1096ed9e99e6SJeremy L Thompson     CeedVector vec;
10972b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1098ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
10992b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
11002b730f8bSJeremy L Thompson       CeedCall(CeedBasisGetDimension(basis_in, &dim));
1101ed9e99e6SJeremy L Thompson       CeedEvalMode eval_mode;
11022b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1103ed9e99e6SJeremy L Thompson       switch (eval_mode) {
1104ed9e99e6SJeremy L Thompson         case CEED_EVAL_NONE:
1105ed9e99e6SJeremy L Thompson         case CEED_EVAL_INTERP:
11062b730f8bSJeremy L Thompson           CeedCall(CeedRealloc(num_eval_mode_in + 1, &eval_mode_in));
1107ed9e99e6SJeremy L Thompson           eval_mode_in[num_eval_mode_in] = eval_mode;
1108ed9e99e6SJeremy L Thompson           num_eval_mode_in += 1;
1109ed9e99e6SJeremy L Thompson           break;
1110ed9e99e6SJeremy L Thompson         case CEED_EVAL_GRAD:
11112b730f8bSJeremy L Thompson           CeedCall(CeedRealloc(num_eval_mode_in + dim, &eval_mode_in));
1112ed9e99e6SJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
1113ed9e99e6SJeremy L Thompson             eval_mode_in[num_eval_mode_in + d] = eval_mode;
1114ed9e99e6SJeremy L Thompson           }
1115ed9e99e6SJeremy L Thompson           num_eval_mode_in += dim;
1116ed9e99e6SJeremy L Thompson           break;
1117ed9e99e6SJeremy L Thompson         case CEED_EVAL_WEIGHT:
1118ed9e99e6SJeremy L Thompson         case CEED_EVAL_DIV:
1119ed9e99e6SJeremy L Thompson         case CEED_EVAL_CURL:
1120ed9e99e6SJeremy L Thompson           break;  // Caught by QF Assembly
1121ed9e99e6SJeremy L Thompson       }
1122ed9e99e6SJeremy L Thompson     }
1123ed9e99e6SJeremy L Thompson   }
1124ed9e99e6SJeremy L Thompson   (*data)->num_eval_mode_in = num_eval_mode_in;
1125ed9e99e6SJeremy L Thompson   (*data)->eval_mode_in     = eval_mode_in;
11262b730f8bSJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->basis_in));
1127ed9e99e6SJeremy L Thompson 
1128ed9e99e6SJeremy L Thompson   // Determine active output basis
1129ed9e99e6SJeremy L Thompson   CeedInt num_output_fields;
11302b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields));
11312b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1132ed9e99e6SJeremy L Thompson   CeedInt       num_eval_mode_out = 0;
1133ed9e99e6SJeremy L Thompson   CeedEvalMode *eval_mode_out     = NULL;
1134ed9e99e6SJeremy L Thompson   CeedBasis     basis_out         = NULL;
1135ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1136ed9e99e6SJeremy L Thompson     CeedVector vec;
11372b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1138ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
11392b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1140ed9e99e6SJeremy L Thompson       CeedEvalMode eval_mode;
11412b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1142ed9e99e6SJeremy L Thompson       switch (eval_mode) {
1143ed9e99e6SJeremy L Thompson         case CEED_EVAL_NONE:
1144ed9e99e6SJeremy L Thompson         case CEED_EVAL_INTERP:
11452b730f8bSJeremy L Thompson           CeedCall(CeedRealloc(num_eval_mode_out + 1, &eval_mode_out));
1146ed9e99e6SJeremy L Thompson           eval_mode_out[num_eval_mode_out] = eval_mode;
1147ed9e99e6SJeremy L Thompson           num_eval_mode_out += 1;
1148ed9e99e6SJeremy L Thompson           break;
1149ed9e99e6SJeremy L Thompson         case CEED_EVAL_GRAD:
11502b730f8bSJeremy L Thompson           CeedCall(CeedRealloc(num_eval_mode_out + dim, &eval_mode_out));
1151ed9e99e6SJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
1152ed9e99e6SJeremy L Thompson             eval_mode_out[num_eval_mode_out + d] = eval_mode;
1153ed9e99e6SJeremy L Thompson           }
1154ed9e99e6SJeremy L Thompson           num_eval_mode_out += dim;
1155ed9e99e6SJeremy L Thompson           break;
1156ed9e99e6SJeremy L Thompson         case CEED_EVAL_WEIGHT:
1157ed9e99e6SJeremy L Thompson         case CEED_EVAL_DIV:
1158ed9e99e6SJeremy L Thompson         case CEED_EVAL_CURL:
1159ed9e99e6SJeremy L Thompson           break;  // Caught by QF Assembly
1160ed9e99e6SJeremy L Thompson       }
1161ed9e99e6SJeremy L Thompson     }
1162ed9e99e6SJeremy L Thompson   }
1163ed9e99e6SJeremy L Thompson   (*data)->num_eval_mode_out = num_eval_mode_out;
1164ed9e99e6SJeremy L Thompson   (*data)->eval_mode_out     = eval_mode_out;
11652b730f8bSJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->basis_out));
1166ed9e99e6SJeremy L Thompson 
1167ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1168ed9e99e6SJeremy L Thompson }
1169ed9e99e6SJeremy L Thompson 
1170ed9e99e6SJeremy L Thompson /**
1171ed9e99e6SJeremy L Thompson   @brief Get CeedOperator CeedEvalModes for assembly
1172ed9e99e6SJeremy L Thompson 
1173ed9e99e6SJeremy L Thompson   @param[in]  data              CeedOperatorAssemblyData
1174ed9e99e6SJeremy L Thompson   @param[out] num_eval_mode_in  Pointer to hold number of input CeedEvalModes, or NULL
1175ed9e99e6SJeremy L Thompson   @param[out] eval_mode_in      Pointer to hold input CeedEvalModes, or NULL
1176ed9e99e6SJeremy L Thompson   @param[out] num_eval_mode_out Pointer to hold number of output CeedEvalModes, or NULL
1177ed9e99e6SJeremy L Thompson   @param[out] eval_mode_out     Pointer to hold output CeedEvalModes, or NULL
1178ed9e99e6SJeremy L Thompson 
1179ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1180ed9e99e6SJeremy L Thompson 
1181ed9e99e6SJeremy L Thompson   @ref Backend
1182ed9e99e6SJeremy L Thompson **/
11832b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_eval_mode_in, const CeedEvalMode **eval_mode_in,
1184ed9e99e6SJeremy L Thompson                                          CeedInt *num_eval_mode_out, const CeedEvalMode **eval_mode_out) {
1185ed9e99e6SJeremy L Thompson   if (num_eval_mode_in) *num_eval_mode_in = data->num_eval_mode_in;
1186ed9e99e6SJeremy L Thompson   if (eval_mode_in) *eval_mode_in = data->eval_mode_in;
1187ed9e99e6SJeremy L Thompson   if (num_eval_mode_out) *num_eval_mode_out = data->num_eval_mode_out;
1188ed9e99e6SJeremy L Thompson   if (eval_mode_out) *eval_mode_out = data->eval_mode_out;
1189ed9e99e6SJeremy L Thompson 
1190ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1191ed9e99e6SJeremy L Thompson }
1192ed9e99e6SJeremy L Thompson 
1193ed9e99e6SJeremy L Thompson /**
1194ed9e99e6SJeremy L Thompson   @brief Get CeedOperator CeedBasis data for assembly
1195ed9e99e6SJeremy L Thompson 
1196ed9e99e6SJeremy L Thompson   @param[in]  data      CeedOperatorAssemblyData
1197ed9e99e6SJeremy L Thompson   @param[out] basis_in  Pointer to hold active input CeedBasis, or NULL
1198ed9e99e6SJeremy L Thompson   @param[out] B_in      Pointer to hold assembled active input B, or NULL
1199ed9e99e6SJeremy L Thompson   @param[out] basis_out Pointer to hold active output CeedBasis, or NULL
1200ed9e99e6SJeremy L Thompson   @param[out] B_out     Pointer to hold assembled active output B, or NULL
1201ed9e99e6SJeremy L Thompson 
1202ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1203ed9e99e6SJeremy L Thompson 
1204ed9e99e6SJeremy L Thompson   @ref Backend
1205ed9e99e6SJeremy L Thompson **/
12062b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedBasis *basis_in, const CeedScalar **B_in, CeedBasis *basis_out,
1207ed9e99e6SJeremy L Thompson                                      const CeedScalar **B_out) {
1208ed9e99e6SJeremy L Thompson   // Assemble B_in, B_out if needed
1209ed9e99e6SJeremy L Thompson   if (B_in && !data->B_in) {
1210ed9e99e6SJeremy L Thompson     CeedInt           num_qpts, elem_size;
1211ed9e99e6SJeremy L Thompson     CeedScalar       *B_in, *identity = NULL;
1212ed9e99e6SJeremy L Thompson     const CeedScalar *interp_in, *grad_in;
1213ed9e99e6SJeremy L Thompson     bool              has_eval_none = false;
1214ed9e99e6SJeremy L Thompson 
12152b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(data->basis_in, &num_qpts));
12162b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(data->basis_in, &elem_size));
12172b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_qpts * elem_size * data->num_eval_mode_in, &B_in));
1218ed9e99e6SJeremy L Thompson 
1219ed9e99e6SJeremy L Thompson     for (CeedInt i = 0; i < data->num_eval_mode_in; i++) {
1220ed9e99e6SJeremy L Thompson       has_eval_none = has_eval_none || (data->eval_mode_in[i] == CEED_EVAL_NONE);
1221ed9e99e6SJeremy L Thompson     }
1222ed9e99e6SJeremy L Thompson     if (has_eval_none) {
12232b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * elem_size, &identity));
1224ed9e99e6SJeremy L Thompson       for (CeedInt i = 0; i < (elem_size < num_qpts ? elem_size : num_qpts); i++) {
1225ed9e99e6SJeremy L Thompson         identity[i * elem_size + i] = 1.0;
1226ed9e99e6SJeremy L Thompson       }
1227ed9e99e6SJeremy L Thompson     }
12282b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(data->basis_in, &interp_in));
12292b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetGrad(data->basis_in, &grad_in));
1230ed9e99e6SJeremy L Thompson 
1231ed9e99e6SJeremy L Thompson     for (CeedInt q = 0; q < num_qpts; q++) {
1232ed9e99e6SJeremy L Thompson       for (CeedInt n = 0; n < elem_size; n++) {
1233ed9e99e6SJeremy L Thompson         CeedInt d_in = -1;
1234ed9e99e6SJeremy L Thompson         for (CeedInt e_in = 0; e_in < data->num_eval_mode_in; e_in++) {
1235ed9e99e6SJeremy L Thompson           const CeedInt     qq = data->num_eval_mode_in * q;
1236ed9e99e6SJeremy L Thompson           const CeedScalar *b  = NULL;
1237ed9e99e6SJeremy L Thompson 
1238ed9e99e6SJeremy L Thompson           if (data->eval_mode_in[e_in] == CEED_EVAL_GRAD) d_in++;
12392b730f8bSJeremy L Thompson           CeedOperatorGetBasisPointer(data->eval_mode_in[e_in], identity, interp_in, &grad_in[d_in * num_qpts * elem_size], &b);
1240ed9e99e6SJeremy L Thompson           B_in[(qq + e_in) * elem_size + n] = b[q * elem_size + n];
1241ed9e99e6SJeremy L Thompson         }
1242ed9e99e6SJeremy L Thompson       }
1243ed9e99e6SJeremy L Thompson     }
1244ed9e99e6SJeremy L Thompson     data->B_in = B_in;
1245ed9e99e6SJeremy L Thompson   }
1246ed9e99e6SJeremy L Thompson 
1247ed9e99e6SJeremy L Thompson   if (B_out && !data->B_out) {
1248ed9e99e6SJeremy L Thompson     CeedInt           num_qpts, elem_size;
1249ed9e99e6SJeremy L Thompson     CeedScalar       *B_out, *identity = NULL;
1250ed9e99e6SJeremy L Thompson     const CeedScalar *interp_out, *grad_out;
1251ed9e99e6SJeremy L Thompson     bool              has_eval_none = false;
1252ed9e99e6SJeremy L Thompson 
12532b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(data->basis_out, &num_qpts));
12542b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(data->basis_out, &elem_size));
12552b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_qpts * elem_size * data->num_eval_mode_out, &B_out));
1256ed9e99e6SJeremy L Thompson 
1257ed9e99e6SJeremy L Thompson     for (CeedInt i = 0; i < data->num_eval_mode_out; i++) {
1258ed9e99e6SJeremy L Thompson       has_eval_none = has_eval_none || (data->eval_mode_out[i] == CEED_EVAL_NONE);
1259ed9e99e6SJeremy L Thompson     }
1260ed9e99e6SJeremy L Thompson     if (has_eval_none) {
12612b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * elem_size, &identity));
1262ed9e99e6SJeremy L Thompson       for (CeedInt i = 0; i < (elem_size < num_qpts ? elem_size : num_qpts); i++) {
1263ed9e99e6SJeremy L Thompson         identity[i * elem_size + i] = 1.0;
1264ed9e99e6SJeremy L Thompson       }
1265ed9e99e6SJeremy L Thompson     }
12662b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetInterp(data->basis_out, &interp_out));
12672b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetGrad(data->basis_out, &grad_out));
1268ed9e99e6SJeremy L Thompson 
1269ed9e99e6SJeremy L Thompson     for (CeedInt q = 0; q < num_qpts; q++) {
1270ed9e99e6SJeremy L Thompson       for (CeedInt n = 0; n < elem_size; n++) {
1271ed9e99e6SJeremy L Thompson         CeedInt d_out = -1;
1272ed9e99e6SJeremy L Thompson         for (CeedInt e_out = 0; e_out < data->num_eval_mode_out; e_out++) {
1273ed9e99e6SJeremy L Thompson           const CeedInt     qq = data->num_eval_mode_out * q;
1274ed9e99e6SJeremy L Thompson           const CeedScalar *b  = NULL;
1275ed9e99e6SJeremy L Thompson 
1276ed9e99e6SJeremy L Thompson           if (data->eval_mode_out[e_out] == CEED_EVAL_GRAD) d_out++;
12772b730f8bSJeremy L Thompson           CeedOperatorGetBasisPointer(data->eval_mode_out[e_out], identity, interp_out, &grad_out[d_out * num_qpts * elem_size], &b);
1278ed9e99e6SJeremy L Thompson           B_out[(qq + e_out) * elem_size + n] = b[q * elem_size + n];
1279ed9e99e6SJeremy L Thompson         }
1280ed9e99e6SJeremy L Thompson       }
1281ed9e99e6SJeremy L Thompson     }
1282ed9e99e6SJeremy L Thompson     data->B_out = B_out;
1283ed9e99e6SJeremy L Thompson   }
1284ed9e99e6SJeremy L Thompson 
1285ed9e99e6SJeremy L Thompson   if (basis_in) *basis_in = data->basis_in;
1286ed9e99e6SJeremy L Thompson   if (B_in) *B_in = data->B_in;
1287ed9e99e6SJeremy L Thompson   if (basis_out) *basis_out = data->basis_out;
1288ed9e99e6SJeremy L Thompson   if (B_out) *B_out = data->B_out;
1289ed9e99e6SJeremy L Thompson 
1290ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1291ed9e99e6SJeremy L Thompson }
1292ed9e99e6SJeremy L Thompson 
1293ed9e99e6SJeremy L Thompson /**
1294ed9e99e6SJeremy L Thompson   @brief Destroy CeedOperatorAssemblyData
1295ed9e99e6SJeremy L Thompson 
1296ea61e9acSJeremy L Thompson   @param[in,out] data CeedOperatorAssemblyData to destroy
1297ed9e99e6SJeremy L Thompson 
1298ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1299ed9e99e6SJeremy L Thompson 
1300ed9e99e6SJeremy L Thompson   @ref Backend
1301ed9e99e6SJeremy L Thompson **/
1302ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) {
1303ed9e99e6SJeremy L Thompson   if (!*data) return CEED_ERROR_SUCCESS;
1304ed9e99e6SJeremy L Thompson 
13052b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
13062b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&(*data)->basis_in));
13072b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&(*data)->basis_out));
13082b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_in));
13092b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_out));
13102b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*data)->B_in));
13112b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*data)->B_out));
1312ed9e99e6SJeremy L Thompson 
13132b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1314ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1315ed9e99e6SJeremy L Thompson }
1316ed9e99e6SJeremy L Thompson 
1317480fae85SJeremy L Thompson /// @}
1318480fae85SJeremy L Thompson 
1319480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1320eaf62fffSJeremy L Thompson /// CeedOperator Public API
1321eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1322eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser
1323eaf62fffSJeremy L Thompson /// @{
1324eaf62fffSJeremy L Thompson 
1325eaf62fffSJeremy L Thompson /**
1326eaf62fffSJeremy L Thompson   @brief Assemble a linear CeedQFunction associated with a CeedOperator
1327eaf62fffSJeremy L Thompson 
1328ea61e9acSJeremy L Thompson   This returns a CeedVector containing a matrix at each quadrature point providing the action of the CeedQFunction associated with the CeedOperator.
1329ea61e9acSJeremy L Thompson     The vector 'assembled' is of shape [num_elements, num_input_fields, num_output_fields, num_quad_points] and contains column-major matrices
1330ea61e9acSJeremy L Thompson representing the action of the CeedQFunction for a corresponding quadrature point on an element. Inputs and outputs are in the order provided by the
1331ea61e9acSJeremy L Thompson user when adding CeedOperator fields. For example, a CeedQFunction with inputs 'u' and 'gradu' and outputs 'gradv' and 'v', provided in that order,
1332ea61e9acSJeremy L Thompson would result in an assembled QFunction that consists of (1 + dim) x (dim + 1) matrices at each quadrature point acting on the input [u, du_0, du_1]
1333ea61e9acSJeremy L Thompson and producing the output [dv_0, dv_1, v].
1334eaf62fffSJeremy L Thompson 
1335ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1336f04ea552SJeremy L Thompson 
1337ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1338ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points
1339ea61e9acSJeremy L Thompson   @param[out] rstr      CeedElemRestriction for CeedVector containing assembled CeedQFunction
1340ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1341eaf62fffSJeremy L Thompson 
1342eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1343eaf62fffSJeremy L Thompson 
1344eaf62fffSJeremy L Thompson   @ref User
1345eaf62fffSJeremy L Thompson **/
13462b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
13472b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1348eaf62fffSJeremy L Thompson 
1349eaf62fffSJeremy L Thompson   if (op->LinearAssembleQFunction) {
1350d04bbc78SJeremy L Thompson     // Backend version
13512b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request));
1352eaf62fffSJeremy L Thompson   } else {
1353d04bbc78SJeremy L Thompson     // Operator fallback
1354d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1355d04bbc78SJeremy L Thompson 
13562b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1357d04bbc78SJeremy L Thompson     if (op_fallback) {
13582b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request));
1359d04bbc78SJeremy L Thompson     } else {
1360d04bbc78SJeremy L Thompson       // LCOV_EXCL_START
13612b730f8bSJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction");
1362d04bbc78SJeremy L Thompson       // LCOV_EXCL_STOP
1363d04bbc78SJeremy L Thompson     }
136470a7ffb3SJeremy L Thompson   }
1365eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1366eaf62fffSJeremy L Thompson }
136770a7ffb3SJeremy L Thompson 
136870a7ffb3SJeremy L Thompson /**
1369ea61e9acSJeremy L Thompson   @brief Assemble CeedQFunction and store result internally.
1370ea61e9acSJeremy L Thompson            Return copied references of stored data to the caller.
1371ea61e9acSJeremy L Thompson            Caller is responsible for ownership and destruction of the copied references.
1372ea61e9acSJeremy L Thompson            See also @ref CeedOperatorLinearAssembleQFunction
137370a7ffb3SJeremy L Thompson 
1374ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1375ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points
1376ea61e9acSJeremy L Thompson   @param[out] rstr      CeedElemRestriction for CeedVector containing assembledCeedQFunction
1377ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
137870a7ffb3SJeremy L Thompson 
137970a7ffb3SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
138070a7ffb3SJeremy L Thompson 
138170a7ffb3SJeremy L Thompson   @ref User
138270a7ffb3SJeremy L Thompson **/
13832b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
13842b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
138570a7ffb3SJeremy L Thompson 
138670a7ffb3SJeremy L Thompson   if (op->LinearAssembleQFunctionUpdate) {
1387d04bbc78SJeremy L Thompson     // Backend version
1388480fae85SJeremy L Thompson     bool                qf_assembled_is_setup;
13892efa2d85SJeremy L Thompson     CeedVector          assembled_vec  = NULL;
13902efa2d85SJeremy L Thompson     CeedElemRestriction assembled_rstr = NULL;
1391480fae85SJeremy L Thompson 
13922b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataIsSetup(op->qf_assembled, &qf_assembled_is_setup));
1393480fae85SJeremy L Thompson     if (qf_assembled_is_setup) {
1394d04bbc78SJeremy L Thompson       bool update_needed;
1395d04bbc78SJeremy L Thompson 
13962b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataGetObjects(op->qf_assembled, &assembled_vec, &assembled_rstr));
13972b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(op->qf_assembled, &update_needed));
13988b919e6bSJeremy L Thompson       if (update_needed) {
13992b730f8bSJeremy L Thompson         CeedCall(op->LinearAssembleQFunctionUpdate(op, assembled_vec, assembled_rstr, request));
14008b919e6bSJeremy L Thompson       }
140170a7ffb3SJeremy L Thompson     } else {
14022b730f8bSJeremy L Thompson       CeedCall(op->LinearAssembleQFunction(op, &assembled_vec, &assembled_rstr, request));
14032b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataSetObjects(op->qf_assembled, assembled_vec, assembled_rstr));
140470a7ffb3SJeremy L Thompson     }
14052b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, false));
14062efa2d85SJeremy L Thompson 
1407d04bbc78SJeremy L Thompson     // Copy reference from internally held copy
140870a7ffb3SJeremy L Thompson     *assembled = NULL;
140970a7ffb3SJeremy L Thompson     *rstr      = NULL;
14102b730f8bSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled));
14112b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled_vec));
14122b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr));
14132b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&assembled_rstr));
141470a7ffb3SJeremy L Thompson   } else {
1415d04bbc78SJeremy L Thompson     // Operator fallback
1416d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1417d04bbc78SJeremy L Thompson 
14182b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1419d04bbc78SJeremy L Thompson     if (op_fallback) {
14202b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request));
1421d04bbc78SJeremy L Thompson     } else {
1422d04bbc78SJeremy L Thompson       // LCOV_EXCL_START
14232b730f8bSJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate");
1424d04bbc78SJeremy L Thompson       // LCOV_EXCL_STOP
142570a7ffb3SJeremy L Thompson     }
142670a7ffb3SJeremy L Thompson   }
142770a7ffb3SJeremy L Thompson 
142870a7ffb3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1429eaf62fffSJeremy L Thompson }
1430eaf62fffSJeremy L Thompson 
1431eaf62fffSJeremy L Thompson /**
1432eaf62fffSJeremy L Thompson   @brief Assemble the diagonal of a square linear CeedOperator
1433eaf62fffSJeremy L Thompson 
1434eaf62fffSJeremy L Thompson   This overwrites a CeedVector with the diagonal of a linear CeedOperator.
1435eaf62fffSJeremy L Thompson 
1436ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1437eaf62fffSJeremy L Thompson 
1438ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1439f04ea552SJeremy L Thompson 
1440ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1441eaf62fffSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator diagonal
1442ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1443eaf62fffSJeremy L Thompson 
1444eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1445eaf62fffSJeremy L Thompson 
1446eaf62fffSJeremy L Thompson   @ref User
1447eaf62fffSJeremy L Thompson **/
14482b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1449f3d47e36SJeremy L Thompson   bool is_composite;
14502b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1451f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1452eaf62fffSJeremy L Thompson 
1453c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
14542b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
14552b730f8bSJeremy L Thompson   if (input_size != output_size) {
1456c9366a6bSJeremy L Thompson     // LCOV_EXCL_START
1457c9366a6bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1458c9366a6bSJeremy L Thompson     // LCOV_EXCL_STOP
14592b730f8bSJeremy L Thompson   }
1460c9366a6bSJeremy L Thompson 
1461f3d47e36SJeremy L Thompson   // Early exit for empty operator
1462f3d47e36SJeremy L Thompson   if (!is_composite) {
1463f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1464f3d47e36SJeremy L Thompson 
1465f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1466f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1467f3d47e36SJeremy L Thompson   }
1468f3d47e36SJeremy L Thompson 
1469eaf62fffSJeremy L Thompson   if (op->LinearAssembleDiagonal) {
1470d04bbc78SJeremy L Thompson     // Backend version
14712b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleDiagonal(op, assembled, request));
1472eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1473eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddDiagonal) {
1474d04bbc78SJeremy L Thompson     // Backend version with zeroing first
14752b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
14762b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1477eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1478eaf62fffSJeremy L Thompson   } else {
1479d04bbc78SJeremy L Thompson     // Operator fallback
1480d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1481d04bbc78SJeremy L Thompson 
14822b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1483d04bbc78SJeremy L Thompson     if (op_fallback) {
14842b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request));
1485eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1486eaf62fffSJeremy L Thompson     }
1487eaf62fffSJeremy L Thompson   }
1488eaf62fffSJeremy L Thompson   // Default interface implementation
14892b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
14902b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request));
1491d04bbc78SJeremy L Thompson 
1492eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1493eaf62fffSJeremy L Thompson }
1494eaf62fffSJeremy L Thompson 
1495eaf62fffSJeremy L Thompson /**
1496eaf62fffSJeremy L Thompson   @brief Assemble the diagonal of a square linear CeedOperator
1497eaf62fffSJeremy L Thompson 
1498eaf62fffSJeremy L Thompson   This sums into a CeedVector the diagonal of a linear CeedOperator.
1499eaf62fffSJeremy L Thompson 
1500ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1501eaf62fffSJeremy L Thompson 
1502ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1503f04ea552SJeremy L Thompson 
1504ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1505eaf62fffSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator diagonal
1506ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1507eaf62fffSJeremy L Thompson 
1508eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1509eaf62fffSJeremy L Thompson 
1510eaf62fffSJeremy L Thompson   @ref User
1511eaf62fffSJeremy L Thompson **/
15122b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1513f3d47e36SJeremy L Thompson   bool is_composite;
15142b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1515f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1516eaf62fffSJeremy L Thompson 
1517c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
15182b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
15192b730f8bSJeremy L Thompson   if (input_size != output_size) {
1520c9366a6bSJeremy L Thompson     // LCOV_EXCL_START
1521c9366a6bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1522c9366a6bSJeremy L Thompson     // LCOV_EXCL_STOP
15232b730f8bSJeremy L Thompson   }
1524c9366a6bSJeremy L Thompson 
1525f3d47e36SJeremy L Thompson   // Early exit for empty operator
1526f3d47e36SJeremy L Thompson   if (!is_composite) {
1527f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1528f3d47e36SJeremy L Thompson 
1529f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1530f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1531f3d47e36SJeremy L Thompson   }
1532f3d47e36SJeremy L Thompson 
1533eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddDiagonal) {
1534d04bbc78SJeremy L Thompson     // Backend version
15352b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1536eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1537eaf62fffSJeremy L Thompson   } else {
1538d04bbc78SJeremy L Thompson     // Operator fallback
1539d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1540d04bbc78SJeremy L Thompson 
15412b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1542d04bbc78SJeremy L Thompson     if (op_fallback) {
15432b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
1544eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1545eaf62fffSJeremy L Thompson     }
1546eaf62fffSJeremy L Thompson   }
1547eaf62fffSJeremy L Thompson   // Default interface implementation
1548eaf62fffSJeremy L Thompson   if (is_composite) {
15492b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
1550eaf62fffSJeremy L Thompson   } else {
15512b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, false, assembled));
1552eaf62fffSJeremy L Thompson   }
1553d04bbc78SJeremy L Thompson 
1554d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1555eaf62fffSJeremy L Thompson }
1556eaf62fffSJeremy L Thompson 
1557eaf62fffSJeremy L Thompson /**
1558eaf62fffSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear CeedOperator
1559eaf62fffSJeremy L Thompson 
1560ea61e9acSJeremy L Thompson   This overwrites a CeedVector with the point block diagonal of a linear CeedOperator.
1561eaf62fffSJeremy L Thompson 
1562ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1563eaf62fffSJeremy L Thompson 
1564ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1565f04ea552SJeremy L Thompson 
1566ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1567ea61e9acSJeremy 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
1568ea61e9acSJeremy 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,
1569ea61e9acSJeremy L Thompson component in].
1570ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1571eaf62fffSJeremy L Thompson 
1572eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1573eaf62fffSJeremy L Thompson 
1574eaf62fffSJeremy L Thompson   @ref User
1575eaf62fffSJeremy L Thompson **/
15762b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1577f3d47e36SJeremy L Thompson   bool is_composite;
15782b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1579f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1580eaf62fffSJeremy L Thompson 
1581c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
15822b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
15832b730f8bSJeremy L Thompson   if (input_size != output_size) {
1584c9366a6bSJeremy L Thompson     // LCOV_EXCL_START
1585c9366a6bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1586c9366a6bSJeremy L Thompson     // LCOV_EXCL_STOP
15872b730f8bSJeremy L Thompson   }
1588c9366a6bSJeremy L Thompson 
1589f3d47e36SJeremy L Thompson   // Early exit for empty operator
1590f3d47e36SJeremy L Thompson   if (!is_composite) {
1591f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1592f3d47e36SJeremy L Thompson 
1593f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1594f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1595f3d47e36SJeremy L Thompson   }
1596f3d47e36SJeremy L Thompson 
1597eaf62fffSJeremy L Thompson   if (op->LinearAssemblePointBlockDiagonal) {
1598d04bbc78SJeremy L Thompson     // Backend version
15992b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request));
1600eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1601eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddPointBlockDiagonal) {
1602d04bbc78SJeremy L Thompson     // Backend version with zeroing first
16032b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
16042b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
1605eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1606eaf62fffSJeremy L Thompson   } else {
1607d04bbc78SJeremy L Thompson     // Operator fallback
1608d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1609d04bbc78SJeremy L Thompson 
16102b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1611d04bbc78SJeremy L Thompson     if (op_fallback) {
16122b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request));
1613eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1614eaf62fffSJeremy L Thompson     }
1615eaf62fffSJeremy L Thompson   }
1616eaf62fffSJeremy L Thompson   // Default interface implementation
16172b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
16182b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
1619d04bbc78SJeremy L Thompson 
1620eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1621eaf62fffSJeremy L Thompson }
1622eaf62fffSJeremy L Thompson 
1623eaf62fffSJeremy L Thompson /**
1624eaf62fffSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear CeedOperator
1625eaf62fffSJeremy L Thompson 
1626ea61e9acSJeremy L Thompson   This sums into a CeedVector with the point block diagonal of a linear CeedOperator.
1627eaf62fffSJeremy L Thompson 
1628ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1629eaf62fffSJeremy L Thompson 
1630ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1631f04ea552SJeremy L Thompson 
1632ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1633ea61e9acSJeremy 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
1634ea61e9acSJeremy 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,
1635ea61e9acSJeremy L Thompson component in].
1636ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1637eaf62fffSJeremy L Thompson 
1638eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1639eaf62fffSJeremy L Thompson 
1640eaf62fffSJeremy L Thompson   @ref User
1641eaf62fffSJeremy L Thompson **/
16422b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1643f3d47e36SJeremy L Thompson   bool is_composite;
16442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1645f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1646eaf62fffSJeremy L Thompson 
1647c9366a6bSJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
16482b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
16492b730f8bSJeremy L Thompson   if (input_size != output_size) {
1650c9366a6bSJeremy L Thompson     // LCOV_EXCL_START
1651c9366a6bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1652c9366a6bSJeremy L Thompson     // LCOV_EXCL_STOP
16532b730f8bSJeremy L Thompson   }
1654c9366a6bSJeremy L Thompson 
1655f3d47e36SJeremy L Thompson   // Early exit for empty operator
1656f3d47e36SJeremy L Thompson   if (!is_composite) {
1657f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1658f3d47e36SJeremy L Thompson 
1659f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1660f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1661f3d47e36SJeremy L Thompson   }
1662f3d47e36SJeremy L Thompson 
1663eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddPointBlockDiagonal) {
1664d04bbc78SJeremy L Thompson     // Backend version
16652b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request));
1666eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1667eaf62fffSJeremy L Thompson   } else {
1668d04bbc78SJeremy L Thompson     // Operator fallback
1669d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1670d04bbc78SJeremy L Thompson 
16712b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1672d04bbc78SJeremy L Thompson     if (op_fallback) {
16732b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request));
1674eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1675eaf62fffSJeremy L Thompson     }
1676eaf62fffSJeremy L Thompson   }
1677ea61e9acSJeremy L Thompson   // Default interface implementation
1678eaf62fffSJeremy L Thompson   if (is_composite) {
16792b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
1680eaf62fffSJeremy L Thompson   } else {
16812b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, true, assembled));
1682eaf62fffSJeremy L Thompson   }
1683d04bbc78SJeremy L Thompson 
1684d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1685eaf62fffSJeremy L Thompson }
1686eaf62fffSJeremy L Thompson 
1687eaf62fffSJeremy L Thompson /**
1688eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero pattern of a linear operator.
1689eaf62fffSJeremy L Thompson 
1690ea61e9acSJeremy L Thompson    Expected to be used in conjunction with CeedOperatorLinearAssemble().
1691eaf62fffSJeremy L Thompson 
1692ea61e9acSJeremy 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
1693ea61e9acSJeremy L Thompson matrix in entry (i, j). Note that the (i, j) pairs are not unique and may repeat. This function returns the number of entries and their (i, j)
1694ea61e9acSJeremy L Thompson locations, while CeedOperatorLinearAssemble() provides the values in the same ordering.
1695eaf62fffSJeremy L Thompson 
1696eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
1697eaf62fffSJeremy L Thompson 
1698ea61e9acSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1699f04ea552SJeremy L Thompson 
1700eaf62fffSJeremy L Thompson    @param[in]  op          CeedOperator to assemble
1701eaf62fffSJeremy L Thompson    @param[out] num_entries Number of entries in coordinate nonzero pattern
1702eaf62fffSJeremy L Thompson    @param[out] rows        Row number for each entry
1703eaf62fffSJeremy L Thompson    @param[out] cols        Column number for each entry
1704eaf62fffSJeremy L Thompson 
1705eaf62fffSJeremy L Thompson    @ref User
1706eaf62fffSJeremy L Thompson **/
17072b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
1708eaf62fffSJeremy L Thompson   CeedInt       num_suboperators, single_entries;
1709eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
1710eaf62fffSJeremy L Thompson   bool          is_composite;
17112b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1712f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1713eaf62fffSJeremy L Thompson 
1714eaf62fffSJeremy L Thompson   if (op->LinearAssembleSymbolic) {
1715d04bbc78SJeremy L Thompson     // Backend version
17162b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols));
1717eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1718eaf62fffSJeremy L Thompson   } else {
1719d04bbc78SJeremy L Thompson     // Operator fallback
1720d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1721d04bbc78SJeremy L Thompson 
17222b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1723d04bbc78SJeremy L Thompson     if (op_fallback) {
17242b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols));
1725eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1726eaf62fffSJeremy L Thompson     }
1727eaf62fffSJeremy L Thompson   }
1728eaf62fffSJeremy L Thompson 
1729eaf62fffSJeremy L Thompson   // Default interface implementation
1730eaf62fffSJeremy L Thompson 
1731eaf62fffSJeremy L Thompson   // count entries and allocate rows, cols arrays
1732eaf62fffSJeremy L Thompson   *num_entries = 0;
1733eaf62fffSJeremy L Thompson   if (is_composite) {
1734c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1735c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
173692ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
17372b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
1738eaf62fffSJeremy L Thompson       *num_entries += single_entries;
1739eaf62fffSJeremy L Thompson     }
1740eaf62fffSJeremy L Thompson   } else {
17412b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries));
1742eaf62fffSJeremy L Thompson     *num_entries += single_entries;
1743eaf62fffSJeremy L Thompson   }
17442b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, rows));
17452b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, cols));
1746eaf62fffSJeremy L Thompson 
1747eaf62fffSJeremy L Thompson   // assemble nonzero locations
1748eaf62fffSJeremy L Thompson   CeedInt offset = 0;
1749eaf62fffSJeremy L Thompson   if (is_composite) {
1750c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1751c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
175292ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
17532b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols));
17542b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
1755eaf62fffSJeremy L Thompson       offset += single_entries;
1756eaf62fffSJeremy L Thompson     }
1757eaf62fffSJeremy L Thompson   } else {
17582b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols));
1759eaf62fffSJeremy L Thompson   }
1760eaf62fffSJeremy L Thompson 
1761eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1762eaf62fffSJeremy L Thompson }
1763eaf62fffSJeremy L Thompson 
1764eaf62fffSJeremy L Thompson /**
1765eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero entries of a linear operator.
1766eaf62fffSJeremy L Thompson 
1767ea61e9acSJeremy L Thompson    Expected to be used in conjunction with CeedOperatorLinearAssembleSymbolic().
1768eaf62fffSJeremy L Thompson 
1769ea61e9acSJeremy 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
1770ea61e9acSJeremy L Thompson matrix in entry (i, j). Note that the (i, j) pairs are not unique and may repeat. This function returns the values of the nonzero entries to be added,
1771ea61e9acSJeremy L Thompson their (i, j) locations are provided by CeedOperatorLinearAssembleSymbolic()
1772eaf62fffSJeremy L Thompson 
1773eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
1774eaf62fffSJeremy L Thompson 
1775ea61e9acSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1776f04ea552SJeremy L Thompson 
1777eaf62fffSJeremy L Thompson    @param[in]  op     CeedOperator to assemble
1778eaf62fffSJeremy L Thompson    @param[out] values Values to assemble into matrix
1779eaf62fffSJeremy L Thompson 
1780eaf62fffSJeremy L Thompson    @ref User
1781eaf62fffSJeremy L Thompson **/
1782eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) {
1783eaf62fffSJeremy L Thompson   CeedInt       num_suboperators, single_entries = 0;
1784eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
1785f3d47e36SJeremy L Thompson   bool          is_composite;
17862b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1787f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1788f3d47e36SJeremy L Thompson 
1789f3d47e36SJeremy L Thompson   // Early exit for empty operator
1790f3d47e36SJeremy L Thompson   if (!is_composite) {
1791f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1792f3d47e36SJeremy L Thompson 
1793f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1794f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1795f3d47e36SJeremy L Thompson   }
1796eaf62fffSJeremy L Thompson 
1797eaf62fffSJeremy L Thompson   if (op->LinearAssemble) {
1798d04bbc78SJeremy L Thompson     // Backend version
17992b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemble(op, values));
1800eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1801eaf62fffSJeremy L Thompson   } else {
1802d04bbc78SJeremy L Thompson     // Operator fallback
1803d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1804d04bbc78SJeremy L Thompson 
18052b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1806d04bbc78SJeremy L Thompson     if (op_fallback) {
18072b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemble(op_fallback, values));
1808eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1809eaf62fffSJeremy L Thompson     }
1810eaf62fffSJeremy L Thompson   }
1811eaf62fffSJeremy L Thompson 
1812eaf62fffSJeremy L Thompson   // Default interface implementation
1813eaf62fffSJeremy L Thompson   CeedInt offset = 0;
1814eaf62fffSJeremy L Thompson   if (is_composite) {
1815c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1816c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1817cefa2673SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; k++) {
18182b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values));
18192b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
1820eaf62fffSJeremy L Thompson       offset += single_entries;
1821eaf62fffSJeremy L Thompson     }
1822eaf62fffSJeremy L Thompson   } else {
18232b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemble(op, offset, values));
1824eaf62fffSJeremy L Thompson   }
1825eaf62fffSJeremy L Thompson 
1826eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1827eaf62fffSJeremy L Thompson }
1828eaf62fffSJeremy L Thompson 
1829eaf62fffSJeremy L Thompson /**
183075f0d5a4SJeremy L Thompson   @brief Get the multiplicity of nodes across suboperators in a composite CeedOperator
183175f0d5a4SJeremy L Thompson 
183275f0d5a4SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
183375f0d5a4SJeremy L Thompson 
183475f0d5a4SJeremy L Thompson   @param[in]  op               Composite CeedOperator
183575f0d5a4SJeremy L Thompson   @param[in]  num_skip_indices Number of suboperators to skip
183675f0d5a4SJeremy L Thompson   @param[in]  skip_indices     Array of indices of suboperators to skip
183775f0d5a4SJeremy L Thompson   @param[out] mult             Vector to store multiplicity (of size l_size)
183875f0d5a4SJeremy L Thompson 
183975f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
184075f0d5a4SJeremy L Thompson 
184175f0d5a4SJeremy L Thompson   @ref User
184275f0d5a4SJeremy L Thompson **/
184375f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) {
184475f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
184575f0d5a4SJeremy L Thompson 
184675f0d5a4SJeremy L Thompson   Ceed                ceed;
1847*b275c451SJeremy L Thompson   CeedInt             num_suboperators;
184875f0d5a4SJeremy L Thompson   CeedSize            l_vec_len;
184975f0d5a4SJeremy L Thompson   CeedScalar         *mult_array;
185075f0d5a4SJeremy L Thompson   CeedVector          ones_l_vec;
185175f0d5a4SJeremy L Thompson   CeedElemRestriction elem_restr;
1852*b275c451SJeremy L Thompson   CeedOperator       *sub_operators;
185375f0d5a4SJeremy L Thompson 
185475f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
185575f0d5a4SJeremy L Thompson 
185675f0d5a4SJeremy L Thompson   // Zero mult vector
185775f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
185875f0d5a4SJeremy L Thompson 
185975f0d5a4SJeremy L Thompson   // Get suboperators
1860*b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1861*b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1862*b275c451SJeremy L Thompson   if (num_suboperators == 0) return CEED_ERROR_SUCCESS;
186375f0d5a4SJeremy L Thompson 
186475f0d5a4SJeremy L Thompson   // Work vector
186575f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetLength(mult, &l_vec_len));
186675f0d5a4SJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec));
186775f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(ones_l_vec, 1.0));
186875f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array));
186975f0d5a4SJeremy L Thompson 
187075f0d5a4SJeremy L Thompson   // Compute multiplicity across suboperators
1871*b275c451SJeremy L Thompson   for (CeedInt i = 0; i < num_suboperators; i++) {
187275f0d5a4SJeremy L Thompson     const CeedScalar *sub_mult_array;
187375f0d5a4SJeremy L Thompson     CeedVector        sub_mult_l_vec, ones_e_vec;
187475f0d5a4SJeremy L Thompson 
187575f0d5a4SJeremy L Thompson     // -- Check for suboperator to skip
187675f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < num_skip_indices; j++) {
187775f0d5a4SJeremy L Thompson       if (skip_indices[j] == i) continue;
187875f0d5a4SJeremy L Thompson     }
187975f0d5a4SJeremy L Thompson 
188075f0d5a4SJeremy L Thompson     // -- Sub operator multiplicity
1881*b275c451SJeremy L Thompson     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_restr));
188275f0d5a4SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(elem_restr, &sub_mult_l_vec, &ones_e_vec));
188375f0d5a4SJeremy L Thompson     CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0));
188475f0d5a4SJeremy L Thompson     CeedCall(CeedElemRestrictionApply(elem_restr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE));
188575f0d5a4SJeremy L Thompson     CeedCall(CeedElemRestrictionApply(elem_restr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE));
188675f0d5a4SJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array));
188775f0d5a4SJeremy L Thompson     // ---- Flag every node present in the current suboperator
188875f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < l_vec_len; j++) {
188975f0d5a4SJeremy L Thompson       if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0;
189075f0d5a4SJeremy L Thompson     }
189175f0d5a4SJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array));
189275f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&sub_mult_l_vec));
189375f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&ones_e_vec));
189475f0d5a4SJeremy L Thompson   }
189575f0d5a4SJeremy L Thompson   CeedCall(CeedVectorRestoreArray(mult, &mult_array));
1896811d0ccfSJeremy L Thompson   CeedCall(CeedVectorDestroy(&ones_l_vec));
189775f0d5a4SJeremy L Thompson 
189875f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
189975f0d5a4SJeremy L Thompson }
190075f0d5a4SJeremy L Thompson 
190175f0d5a4SJeremy L Thompson /**
1902ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator, creating the prolongation basis from the fine and coarse
1903ea61e9acSJeremy L Thompson grid interpolation
1904eaf62fffSJeremy L Thompson 
190558e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
1906f04ea552SJeremy L Thompson 
1907eaf62fffSJeremy L Thompson   @param[in]  op_fine      Fine grid operator
1908eaf62fffSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter
1909eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid restriction
1910eaf62fffSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector basis
1911eaf62fffSJeremy L Thompson   @param[out] op_coarse    Coarse grid operator
1912eaf62fffSJeremy L Thompson   @param[out] op_prolong   Coarse to fine operator
1913eaf62fffSJeremy L Thompson   @param[out] op_restrict  Fine to coarse operator
1914eaf62fffSJeremy L Thompson 
1915eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1916eaf62fffSJeremy L Thompson 
1917eaf62fffSJeremy L Thompson   @ref User
1918eaf62fffSJeremy L Thompson **/
19192b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
19202b730f8bSJeremy L Thompson                                      CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
19212b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
1922eaf62fffSJeremy L Thompson 
1923f113e5dcSJeremy L Thompson   // Build prolongation matrix
1924f113e5dcSJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f;
19252b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
19262b730f8bSJeremy L Thompson   CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f));
1927eaf62fffSJeremy L Thompson 
1928f113e5dcSJeremy L Thompson   // Core code
19292b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
1930f113e5dcSJeremy L Thompson 
1931eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1932eaf62fffSJeremy L Thompson }
1933eaf62fffSJeremy L Thompson 
1934eaf62fffSJeremy L Thompson /**
1935ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a tensor basis for the active basis
1936eaf62fffSJeremy L Thompson 
193758e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
1938f04ea552SJeremy L Thompson 
1939eaf62fffSJeremy L Thompson   @param[in]  op_fine       Fine grid operator
1940eaf62fffSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter
1941eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid restriction
1942eaf62fffSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector basis
1943eaf62fffSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation
1944eaf62fffSJeremy L Thompson   @param[out] op_coarse     Coarse grid operator
1945eaf62fffSJeremy L Thompson   @param[out] op_prolong    Coarse to fine operator
1946eaf62fffSJeremy L Thompson   @param[out] op_restrict   Fine to coarse operator
1947eaf62fffSJeremy L Thompson 
1948eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1949eaf62fffSJeremy L Thompson 
1950eaf62fffSJeremy L Thompson   @ref User
1951eaf62fffSJeremy L Thompson **/
19522b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
19532b730f8bSJeremy L Thompson                                              const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
19542b730f8bSJeremy L Thompson                                              CeedOperator *op_restrict) {
19552b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
1956eaf62fffSJeremy L Thompson   Ceed ceed;
19572b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
1958eaf62fffSJeremy L Thompson 
1959eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
1960eaf62fffSJeremy L Thompson   CeedBasis basis_fine;
19612b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
1962eaf62fffSJeremy L Thompson   CeedInt Q_f, Q_c;
19632b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
19642b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
19652b730f8bSJeremy L Thompson   if (Q_f != Q_c) {
1966eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
19672b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
1968eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
19692b730f8bSJeremy L Thompson   }
1970eaf62fffSJeremy L Thompson 
1971eaf62fffSJeremy L Thompson   // Coarse to fine basis
1972eaf62fffSJeremy L Thompson   CeedInt dim, num_comp, num_nodes_c, P_1d_f, P_1d_c;
19732b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis_fine, &dim));
19742b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
19752b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f));
19762b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
19772b730f8bSJeremy L Thompson   P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c);
1978eaf62fffSJeremy L Thompson   CeedScalar *q_ref, *q_weight, *grad;
19792b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d_f, &q_ref));
19802b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d_f, &q_weight));
19812b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad));
1982eaf62fffSJeremy L Thompson   CeedBasis basis_c_to_f;
19832b730f8bSJeremy 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));
19842b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref));
19852b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight));
19862b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad));
1987eaf62fffSJeremy L Thompson 
1988eaf62fffSJeremy L Thompson   // Core code
19892b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
1990eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1991eaf62fffSJeremy L Thompson }
1992eaf62fffSJeremy L Thompson 
1993eaf62fffSJeremy L Thompson /**
1994ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a non-tensor basis for the active vector
1995eaf62fffSJeremy L Thompson 
199658e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
1997f04ea552SJeremy L Thompson 
1998eaf62fffSJeremy L Thompson   @param[in]  op_fine       Fine grid operator
1999eaf62fffSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter
2000eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid restriction
2001eaf62fffSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector basis
2002eaf62fffSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation
2003eaf62fffSJeremy L Thompson   @param[out] op_coarse     Coarse grid operator
2004eaf62fffSJeremy L Thompson   @param[out] op_prolong    Coarse to fine operator
2005eaf62fffSJeremy L Thompson   @param[out] op_restrict   Fine to coarse operator
2006eaf62fffSJeremy L Thompson 
2007eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2008eaf62fffSJeremy L Thompson 
2009eaf62fffSJeremy L Thompson   @ref User
2010eaf62fffSJeremy L Thompson **/
20112b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
20122b730f8bSJeremy L Thompson                                        const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
2013eaf62fffSJeremy L Thompson                                        CeedOperator *op_restrict) {
20142b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2015eaf62fffSJeremy L Thompson   Ceed ceed;
20162b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2017eaf62fffSJeremy L Thompson 
2018eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
2019eaf62fffSJeremy L Thompson   CeedBasis basis_fine;
20202b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
2021eaf62fffSJeremy L Thompson   CeedInt Q_f, Q_c;
20222b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
20232b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
20242b730f8bSJeremy L Thompson   if (Q_f != Q_c) {
2025eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
20262b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2027eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
20282b730f8bSJeremy L Thompson   }
2029eaf62fffSJeremy L Thompson 
2030eaf62fffSJeremy L Thompson   // Coarse to fine basis
2031eaf62fffSJeremy L Thompson   CeedElemTopology topo;
20322b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetTopology(basis_fine, &topo));
2033eaf62fffSJeremy L Thompson   CeedInt dim, num_comp, num_nodes_c, num_nodes_f;
20342b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis_fine, &dim));
20352b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
20362b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f));
20372b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
2038eaf62fffSJeremy L Thompson   CeedScalar *q_ref, *q_weight, *grad;
20392b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref));
20402b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_nodes_f, &q_weight));
20412b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad));
2042eaf62fffSJeremy L Thompson   CeedBasis basis_c_to_f;
20432b730f8bSJeremy 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));
20442b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref));
20452b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight));
20462b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad));
2047eaf62fffSJeremy L Thompson 
2048eaf62fffSJeremy L Thompson   // Core code
20492b730f8bSJeremy L Thompson   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2050eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2051eaf62fffSJeremy L Thompson }
2052eaf62fffSJeremy L Thompson 
2053eaf62fffSJeremy L Thompson /**
2054ea61e9acSJeremy L Thompson   @brief Build a FDM based approximate inverse for each element for a CeedOperator
2055eaf62fffSJeremy L Thompson 
2056ea61e9acSJeremy L Thompson   This returns a CeedOperator and CeedVector to apply a Fast Diagonalization Method based approximate inverse.
2057ea61e9acSJeremy L Thompson     This function obtains the simultaneous diagonalization for the 1D mass and Laplacian operators, M = V^T V, K = V^T S V.
2058ea61e9acSJeremy L Thompson     The assembled QFunction is used to modify the eigenvalues from simultaneous diagonalization and obtain an approximate inverse of the form V^T
2059ea61e9acSJeremy L Thompson S^hat V. The CeedOperator must be linear and non-composite. The associated CeedQFunction must therefore also be linear.
2060eaf62fffSJeremy L Thompson 
2061ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2062f04ea552SJeremy L Thompson 
2063ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to create element inverses
2064ea61e9acSJeremy L Thompson   @param[out] fdm_inv CeedOperator to apply the action of a FDM based inverse for each element
2065ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2066eaf62fffSJeremy L Thompson 
2067eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2068eaf62fffSJeremy L Thompson 
2069480fae85SJeremy L Thompson   @ref User
2070eaf62fffSJeremy L Thompson **/
20712b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) {
20722b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2073eaf62fffSJeremy L Thompson 
2074eaf62fffSJeremy L Thompson   if (op->CreateFDMElementInverse) {
2075d04bbc78SJeremy L Thompson     // Backend version
20762b730f8bSJeremy L Thompson     CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request));
2077eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2078eaf62fffSJeremy L Thompson   } else {
2079d04bbc78SJeremy L Thompson     // Operator fallback
2080d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2081d04bbc78SJeremy L Thompson 
20822b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2083d04bbc78SJeremy L Thompson     if (op_fallback) {
20842b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request));
2085eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2086eaf62fffSJeremy L Thompson     }
2087eaf62fffSJeremy L Thompson   }
2088eaf62fffSJeremy L Thompson 
2089d04bbc78SJeremy L Thompson   // Default interface implementation
2090eaf62fffSJeremy L Thompson   Ceed ceed, ceed_parent;
20912b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
20922b730f8bSJeremy L Thompson   CeedCall(CeedGetOperatorFallbackParentCeed(ceed, &ceed_parent));
2093eaf62fffSJeremy L Thompson   ceed_parent = ceed_parent ? ceed_parent : ceed;
2094eaf62fffSJeremy L Thompson   CeedQFunction qf;
20952b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
2096eaf62fffSJeremy L Thompson 
2097eaf62fffSJeremy L Thompson   // Determine active input basis
2098eaf62fffSJeremy L Thompson   bool                interp = false, grad = false;
2099eaf62fffSJeremy L Thompson   CeedBasis           basis = NULL;
2100eaf62fffSJeremy L Thompson   CeedElemRestriction rstr  = NULL;
2101eaf62fffSJeremy L Thompson   CeedOperatorField  *op_fields;
2102eaf62fffSJeremy L Thompson   CeedQFunctionField *qf_fields;
2103eaf62fffSJeremy L Thompson   CeedInt             num_input_fields;
21042b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL));
21052b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
2106eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
2107eaf62fffSJeremy L Thompson     CeedVector vec;
21082b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
2109eaf62fffSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2110eaf62fffSJeremy L Thompson       CeedEvalMode eval_mode;
21112b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
2112eaf62fffSJeremy L Thompson       interp = interp || eval_mode == CEED_EVAL_INTERP;
2113eaf62fffSJeremy L Thompson       grad   = grad || eval_mode == CEED_EVAL_GRAD;
21142b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis));
21152b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
2116eaf62fffSJeremy L Thompson     }
2117eaf62fffSJeremy L Thompson   }
21182b730f8bSJeremy L Thompson   if (!basis) {
2119eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
2120eaf62fffSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "No active field set");
2121eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
21222b730f8bSJeremy L Thompson   }
2123e79b91d9SJeremy L Thompson   CeedSize l_size = 1;
2124e79b91d9SJeremy L Thompson   CeedInt  P_1d, Q_1d, elem_size, num_qpts, dim, num_comp = 1, num_elem = 1;
21252b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
21262b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes(basis, &elem_size));
21272b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
21282b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
21292b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
21302b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
21312b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
21322b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
2133eaf62fffSJeremy L Thompson 
2134eaf62fffSJeremy L Thompson   // Build and diagonalize 1D Mass and Laplacian
2135eaf62fffSJeremy L Thompson   bool tensor_basis;
21362b730f8bSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &tensor_basis));
21372b730f8bSJeremy L Thompson   if (!tensor_basis) {
2138eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
21392b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases");
2140eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
21412b730f8bSJeremy L Thompson   }
2142eaf62fffSJeremy L Thompson   CeedScalar *mass, *laplace, *x, *fdm_interp, *lambda;
21432b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &mass));
21442b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &laplace));
21452b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &x));
21462b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp));
21472b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &lambda));
2148eaf62fffSJeremy L Thompson   // -- Build matrices
2149eaf62fffSJeremy L Thompson   const CeedScalar *interp_1d, *grad_1d, *q_weight_1d;
21502b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
21512b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
21522b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
21532b730f8bSJeremy L Thompson   CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace));
2154eaf62fffSJeremy L Thompson 
2155eaf62fffSJeremy L Thompson   // -- Diagonalize
21562b730f8bSJeremy L Thompson   CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d));
21572b730f8bSJeremy L Thompson   CeedCall(CeedFree(&mass));
21582b730f8bSJeremy L Thompson   CeedCall(CeedFree(&laplace));
21592b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
21602b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d];
21612b730f8bSJeremy L Thompson   }
21622b730f8bSJeremy L Thompson   CeedCall(CeedFree(&x));
2163eaf62fffSJeremy L Thompson 
2164eaf62fffSJeremy L Thompson   // Assemble QFunction
2165eaf62fffSJeremy L Thompson   CeedVector          assembled;
2166eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_qf;
21672b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request));
2168eaf62fffSJeremy L Thompson   CeedInt layout[3];
21692b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_qf, &layout));
21702b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qf));
2171eaf62fffSJeremy L Thompson   CeedScalar max_norm = 0;
21722b730f8bSJeremy L Thompson   CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm));
2173eaf62fffSJeremy L Thompson 
2174eaf62fffSJeremy L Thompson   // Calculate element averages
2175eaf62fffSJeremy L Thompson   CeedInt           num_modes = (interp ? 1 : 0) + (grad ? dim : 0);
2176eaf62fffSJeremy L Thompson   CeedScalar       *elem_avg;
2177eaf62fffSJeremy L Thompson   const CeedScalar *assembled_array, *q_weight_array;
2178eaf62fffSJeremy L Thompson   CeedVector        q_weight;
21792b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight));
21802b730f8bSJeremy L Thompson   CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight));
21812b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array));
21822b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array));
21832b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_elem, &elem_avg));
2184eaf62fffSJeremy L Thompson   const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON;
2185eaf62fffSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
2186eaf62fffSJeremy L Thompson     CeedInt count = 0;
21872b730f8bSJeremy L Thompson     for (CeedInt q = 0; q < num_qpts; q++) {
21882b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) {
21892b730f8bSJeremy L Thompson         if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) {
21902b730f8bSJeremy L Thompson           elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q];
2191eaf62fffSJeremy L Thompson           count++;
2192eaf62fffSJeremy L Thompson         }
21932b730f8bSJeremy L Thompson       }
21942b730f8bSJeremy L Thompson     }
2195eaf62fffSJeremy L Thompson     if (count) {
2196eaf62fffSJeremy L Thompson       elem_avg[e] /= count;
2197eaf62fffSJeremy L Thompson     } else {
2198eaf62fffSJeremy L Thompson       elem_avg[e] = 1.0;
2199eaf62fffSJeremy L Thompson     }
2200eaf62fffSJeremy L Thompson   }
22012b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array));
22022b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled));
22032b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array));
22042b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_weight));
2205eaf62fffSJeremy L Thompson 
2206eaf62fffSJeremy L Thompson   // Build FDM diagonal
2207eaf62fffSJeremy L Thompson   CeedVector  q_data;
2208eaf62fffSJeremy L Thompson   CeedScalar *q_data_array, *fdm_diagonal;
22092b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_comp * elem_size, &fdm_diagonal));
2210eaf62fffSJeremy L Thompson   const CeedScalar fdm_diagonal_bound = elem_size * CEED_EPSILON;
22112b730f8bSJeremy L Thompson   for (CeedInt c = 0; c < num_comp; c++) {
2212eaf62fffSJeremy L Thompson     for (CeedInt n = 0; n < elem_size; n++) {
22132b730f8bSJeremy L Thompson       if (interp) fdm_diagonal[c * elem_size + n] = 1.0;
22142b730f8bSJeremy L Thompson       if (grad) {
2215eaf62fffSJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
2216eaf62fffSJeremy L Thompson           CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d;
2217eaf62fffSJeremy L Thompson           fdm_diagonal[c * elem_size + n] += lambda[i];
2218eaf62fffSJeremy L Thompson         }
2219eaf62fffSJeremy L Thompson       }
22202b730f8bSJeremy L Thompson       if (fabs(fdm_diagonal[c * elem_size + n]) < fdm_diagonal_bound) fdm_diagonal[c * elem_size + n] = fdm_diagonal_bound;
22212b730f8bSJeremy L Thompson     }
22222b730f8bSJeremy L Thompson   }
22232b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * elem_size, &q_data));
22242b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(q_data, 0.0));
22252b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array));
22262b730f8bSJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
22272b730f8bSJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
22282b730f8bSJeremy L Thompson       for (CeedInt n = 0; n < elem_size; n++) q_data_array[(e * num_comp + c) * elem_size + n] = 1. / (elem_avg[e] * fdm_diagonal[c * elem_size + n]);
22292b730f8bSJeremy L Thompson     }
22302b730f8bSJeremy L Thompson   }
22312b730f8bSJeremy L Thompson   CeedCall(CeedFree(&elem_avg));
22322b730f8bSJeremy L Thompson   CeedCall(CeedFree(&fdm_diagonal));
22332b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(q_data, &q_data_array));
2234eaf62fffSJeremy L Thompson 
2235eaf62fffSJeremy L Thompson   // Setup FDM operator
2236eaf62fffSJeremy L Thompson   // -- Basis
2237eaf62fffSJeremy L Thompson   CeedBasis   fdm_basis;
2238eaf62fffSJeremy L Thompson   CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy;
22392b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy));
22402b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &q_ref_dummy));
22412b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &q_weight_dummy));
22422b730f8bSJeremy 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));
22432b730f8bSJeremy L Thompson   CeedCall(CeedFree(&fdm_interp));
22442b730f8bSJeremy L Thompson   CeedCall(CeedFree(&grad_dummy));
22452b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_ref_dummy));
22462b730f8bSJeremy L Thompson   CeedCall(CeedFree(&q_weight_dummy));
22472b730f8bSJeremy L Thompson   CeedCall(CeedFree(&lambda));
2248eaf62fffSJeremy L Thompson 
2249eaf62fffSJeremy L Thompson   // -- Restriction
2250eaf62fffSJeremy L Thompson   CeedElemRestriction rstr_qd_i;
2251eaf62fffSJeremy L Thompson   CeedInt             strides[3] = {1, elem_size, elem_size * num_comp};
22522b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, elem_size, num_comp, num_elem * num_comp * elem_size, strides, &rstr_qd_i));
2253eaf62fffSJeremy L Thompson   // -- QFunction
2254eaf62fffSJeremy L Thompson   CeedQFunction qf_fdm;
22552b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm));
22562b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP));
22572b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE));
22582b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP));
22592b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp));
2260eaf62fffSJeremy L Thompson   // -- QFunction context
2261eaf62fffSJeremy L Thompson   CeedInt *num_comp_data;
22622b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, &num_comp_data));
2263eaf62fffSJeremy L Thompson   num_comp_data[0] = num_comp;
2264eaf62fffSJeremy L Thompson   CeedQFunctionContext ctx_fdm;
22652b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm));
22662b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data));
22672b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm));
22682b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_fdm));
2269eaf62fffSJeremy L Thompson   // -- Operator
22702b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv));
22712b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
22722b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_COLLOCATED, q_data));
22732b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2274eaf62fffSJeremy L Thompson 
2275eaf62fffSJeremy L Thompson   // Cleanup
22762b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_data));
22772b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&fdm_basis));
22782b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i));
22792b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_fdm));
2280eaf62fffSJeremy L Thompson 
2281eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2282eaf62fffSJeremy L Thompson }
2283eaf62fffSJeremy L Thompson 
2284eaf62fffSJeremy L Thompson /// @}
2285