xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-preconditioning.c (revision 7758292ff870800397ca2688e3f814e66472cc67)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3eaf62fffSJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5eaf62fffSJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7eaf62fffSJeremy L Thompson 
82b730f8bSJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
11c85e8640SSebastian Grimberg #include <assert.h>
122b730f8bSJeremy L Thompson #include <math.h>
13eaf62fffSJeremy L Thompson #include <stdbool.h>
14eaf62fffSJeremy L Thompson #include <stdio.h>
15eaf62fffSJeremy L Thompson #include <string.h>
16eaf62fffSJeremy L Thompson 
17eaf62fffSJeremy L Thompson /// @file
18eaf62fffSJeremy L Thompson /// Implementation of CeedOperator preconditioning interfaces
19eaf62fffSJeremy L Thompson 
20eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
21eaf62fffSJeremy L Thompson /// CeedOperator Library Internal Preconditioning Functions
22eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
23eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorDeveloper
24eaf62fffSJeremy L Thompson /// @{
25eaf62fffSJeremy L Thompson 
26eaf62fffSJeremy L Thompson /**
27ea61e9acSJeremy L Thompson   @brief Duplicate a CeedQFunction with a reference Ceed to fallback for advanced CeedOperator functionality
289e77b9c8SJeremy L Thompson 
2901ea9c81SJed Brown   @param[in]  fallback_ceed Ceed on which to create fallback CeedQFunction
309e77b9c8SJeremy L Thompson   @param[in]  qf            CeedQFunction to create fallback for
3101ea9c81SJed Brown   @param[out] qf_fallback   fallback CeedQFunction
329e77b9c8SJeremy L Thompson 
339e77b9c8SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
349e77b9c8SJeremy L Thompson 
359e77b9c8SJeremy L Thompson   @ref Developer
369e77b9c8SJeremy L Thompson **/
372b730f8bSJeremy L Thompson static int CeedQFunctionCreateFallback(Ceed fallback_ceed, CeedQFunction qf, CeedQFunction *qf_fallback) {
381c66c397SJeremy L Thompson   char *source_path_with_name = NULL;
391c66c397SJeremy L Thompson 
409e77b9c8SJeremy L Thompson   // Check if NULL qf passed in
419e77b9c8SJeremy L Thompson   if (!qf) return CEED_ERROR_SUCCESS;
429e77b9c8SJeremy L Thompson 
43d04bbc78SJeremy L Thompson   CeedDebug256(qf->ceed, 1, "---------- CeedOperator Fallback ----------\n");
4413f886e9SJeremy L Thompson   CeedDebug(qf->ceed, "Creating fallback CeedQFunction\n");
45d04bbc78SJeremy L Thompson 
469e77b9c8SJeremy L Thompson   if (qf->source_path) {
472b730f8bSJeremy L Thompson     size_t path_len = strlen(qf->source_path), name_len = strlen(qf->kernel_name);
482b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(path_len + name_len + 2, &source_path_with_name));
499e77b9c8SJeremy L Thompson     memcpy(source_path_with_name, qf->source_path, path_len);
509e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len], ":", 1);
519e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len + 1], qf->kernel_name, name_len);
529e77b9c8SJeremy L Thompson   } else {
532b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &source_path_with_name));
549e77b9c8SJeremy L Thompson   }
559e77b9c8SJeremy L Thompson 
562b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(fallback_ceed, qf->vec_length, qf->function, source_path_with_name, qf_fallback));
579e77b9c8SJeremy L Thompson   {
589e77b9c8SJeremy L Thompson     CeedQFunctionContext ctx;
599e77b9c8SJeremy L Thompson 
602b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
612b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx));
629e77b9c8SJeremy L Thompson   }
639e77b9c8SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
642b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(*qf_fallback, qf->input_fields[i]->field_name, qf->input_fields[i]->size, qf->input_fields[i]->eval_mode));
659e77b9c8SJeremy L Thompson   }
669e77b9c8SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
672b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(*qf_fallback, qf->output_fields[i]->field_name, qf->output_fields[i]->size, qf->output_fields[i]->eval_mode));
689e77b9c8SJeremy L Thompson   }
692b730f8bSJeremy L Thompson   CeedCall(CeedFree(&source_path_with_name));
709e77b9c8SJeremy L Thompson   return CEED_ERROR_SUCCESS;
719e77b9c8SJeremy L Thompson }
729e77b9c8SJeremy L Thompson 
739e77b9c8SJeremy L Thompson /**
74ea61e9acSJeremy L Thompson   @brief Duplicate a CeedOperator with a reference Ceed to fallback for advanced CeedOperator functionality
75eaf62fffSJeremy L Thompson 
76ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to create fallback for
77eaf62fffSJeremy L Thompson 
78eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
79eaf62fffSJeremy L Thompson 
80eaf62fffSJeremy L Thompson   @ref Developer
81eaf62fffSJeremy L Thompson **/
82d04bbc78SJeremy L Thompson static int CeedOperatorCreateFallback(CeedOperator op) {
839e77b9c8SJeremy L Thompson   Ceed         ceed_fallback;
841c66c397SJeremy L Thompson   bool         is_composite;
851c66c397SJeremy L Thompson   CeedOperator op_fallback;
86eaf62fffSJeremy L Thompson 
87805fe78eSJeremy L Thompson   // Check not already created
88805fe78eSJeremy L Thompson   if (op->op_fallback) return CEED_ERROR_SUCCESS;
89805fe78eSJeremy L Thompson 
90eaf62fffSJeremy L Thompson   // Fallback Ceed
912b730f8bSJeremy L Thompson   CeedCall(CeedGetOperatorFallbackCeed(op->ceed, &ceed_fallback));
92d04bbc78SJeremy L Thompson   if (!ceed_fallback) return CEED_ERROR_SUCCESS;
93d04bbc78SJeremy L Thompson 
94d04bbc78SJeremy L Thompson   CeedDebug256(op->ceed, 1, "---------- CeedOperator Fallback ----------\n");
9513f886e9SJeremy L Thompson   CeedDebug(op->ceed, "Creating fallback CeedOperator\n");
96eaf62fffSJeremy L Thompson 
97eaf62fffSJeremy L Thompson   // Clone Op
98b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
99b275c451SJeremy L Thompson   if (is_composite) {
100b275c451SJeremy L Thompson     CeedInt       num_suboperators;
101b275c451SJeremy L Thompson     CeedOperator *sub_operators;
102b275c451SJeremy L Thompson 
1032b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorCreate(ceed_fallback, &op_fallback));
104b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
105b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
106b275c451SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
107d04bbc78SJeremy L Thompson       CeedOperator op_sub_fallback;
108d04bbc78SJeremy L Thompson 
109b275c451SJeremy L Thompson       CeedCall(CeedOperatorGetFallback(sub_operators[i], &op_sub_fallback));
1102b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorAddSub(op_fallback, op_sub_fallback));
111805fe78eSJeremy L Thompson     }
112805fe78eSJeremy L Thompson   } else {
1139e77b9c8SJeremy L Thompson     CeedQFunction qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL;
1141c66c397SJeremy L Thompson 
1152b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback));
1162b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback));
1172b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback));
1182b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback));
119805fe78eSJeremy L Thompson     for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
120437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, op->input_fields[i]->field_name, op->input_fields[i]->elem_rstr, op->input_fields[i]->basis,
1212b730f8bSJeremy L Thompson                                     op->input_fields[i]->vec));
122805fe78eSJeremy L Thompson     }
123805fe78eSJeremy L Thompson     for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
124437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, op->output_fields[i]->field_name, op->output_fields[i]->elem_rstr, op->output_fields[i]->basis,
1252b730f8bSJeremy L Thompson                                     op->output_fields[i]->vec));
126805fe78eSJeremy L Thompson     }
1272b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op->qf_assembled, &op_fallback->qf_assembled));
128febe2972SJeremy L Thompson     if (op_fallback->num_qpts == 0) CeedCall(CeedOperatorSetNumQuadraturePoints(op_fallback, op->num_qpts));
1299e77b9c8SJeremy L Thompson     // Cleanup
1302b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf_fallback));
1312b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqf_fallback));
1322b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqfT_fallback));
133805fe78eSJeremy L Thompson   }
1342b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetName(op_fallback, op->name));
1352b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fallback));
136b05f7e9fSJeremy L Thompson   // Note: No ref-counting here so we don't get caught in a reference loop.
137b05f7e9fSJeremy L Thompson   //       The op holds the only reference to op_fallback and is responsible for deleting itself and op_fallback.
138805fe78eSJeremy L Thompson   op->op_fallback                 = op_fallback;
139b05f7e9fSJeremy L Thompson   op_fallback->op_fallback_parent = op;
140eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
141eaf62fffSJeremy L Thompson }
142eaf62fffSJeremy L Thompson 
143eaf62fffSJeremy L Thompson /**
144ea61e9acSJeremy L Thompson   @brief Retrieve fallback CeedOperator with a reference Ceed for advanced CeedOperator functionality
145d04bbc78SJeremy L Thompson 
146d04bbc78SJeremy L Thompson   @param[in]  op          CeedOperator to retrieve fallback for
147d04bbc78SJeremy L Thompson   @param[out] op_fallback Fallback CeedOperator
148d04bbc78SJeremy L Thompson 
149d04bbc78SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
150d04bbc78SJeremy L Thompson 
151d04bbc78SJeremy L Thompson   @ref Developer
152d04bbc78SJeremy L Thompson **/
153d04bbc78SJeremy L Thompson int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) {
154d04bbc78SJeremy L Thompson   // Create if needed
1551c66c397SJeremy L Thompson   if (!op->op_fallback) CeedCall(CeedOperatorCreateFallback(op));
156d04bbc78SJeremy L Thompson   if (op->op_fallback) {
157d04bbc78SJeremy L Thompson     bool is_debug;
158d04bbc78SJeremy L Thompson 
1592b730f8bSJeremy L Thompson     CeedCall(CeedIsDebug(op->ceed, &is_debug));
160d04bbc78SJeremy L Thompson     if (is_debug) {
161b275c451SJeremy L Thompson       Ceed        ceed, ceed_fallback;
162d04bbc78SJeremy L Thompson       const char *resource, *resource_fallback;
163d04bbc78SJeremy L Thompson 
164b275c451SJeremy L Thompson       CeedCall(CeedOperatorGetCeed(op, &ceed));
165b275c451SJeremy L Thompson       CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
166b275c451SJeremy L Thompson       CeedCall(CeedGetResource(ceed, &resource));
1672b730f8bSJeremy L Thompson       CeedCall(CeedGetResource(ceed_fallback, &resource_fallback));
168d04bbc78SJeremy L Thompson 
16923d4529eSJeremy L Thompson       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n");
170b275c451SJeremy L Thompson       CeedDebug(ceed, "Falling back from %s operator at address %ld to %s operator at address %ld\n", resource, op, resource_fallback,
1712b730f8bSJeremy L Thompson                 op->op_fallback);
172d04bbc78SJeremy L Thompson     }
173d04bbc78SJeremy L Thompson   }
174d04bbc78SJeremy L Thompson   *op_fallback = op->op_fallback;
175d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
176d04bbc78SJeremy L Thompson }
177d04bbc78SJeremy L Thompson 
178d04bbc78SJeremy L Thompson /**
1792e8f5c67SJeremy L Thompson   @brief Get the parent CeedOperator for a fallback CeedOperator
180bb229da9SJeremy L Thompson 
181bb229da9SJeremy L Thompson   @param[in]  op     CeedOperator context
182bb229da9SJeremy L Thompson   @param[out] parent Variable to store parent CeedOperator context
183bb229da9SJeremy L Thompson 
184bb229da9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
185bb229da9SJeremy L Thompson 
186bb229da9SJeremy L Thompson   @ref Developer
187bb229da9SJeremy L Thompson **/
188bb229da9SJeremy L Thompson int CeedOperatorGetFallbackParent(CeedOperator op, CeedOperator *parent) {
189bb229da9SJeremy L Thompson   *parent = op->op_fallback_parent ? op->op_fallback_parent : NULL;
190bb229da9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
191bb229da9SJeremy L Thompson }
192bb229da9SJeremy L Thompson 
193bb229da9SJeremy L Thompson /**
1942e8f5c67SJeremy L Thompson   @brief Get the Ceed context of the parent CeedOperator for a fallback CeedOperator
195bb229da9SJeremy L Thompson 
196bb229da9SJeremy L Thompson   @param[in]  op     CeedOperator context
197bb229da9SJeremy L Thompson   @param[out] parent Variable to store parent Ceed context
198bb229da9SJeremy L Thompson 
199bb229da9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
200bb229da9SJeremy L Thompson 
201bb229da9SJeremy L Thompson   @ref Developer
202bb229da9SJeremy L Thompson **/
203bb229da9SJeremy L Thompson int CeedOperatorGetFallbackParentCeed(CeedOperator op, Ceed *parent) {
204e984cf9aSJeremy L Thompson   *parent = op->op_fallback_parent ? op->op_fallback_parent->ceed : op->ceed;
205bb229da9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
206bb229da9SJeremy L Thompson }
207bb229da9SJeremy L Thompson 
208bb229da9SJeremy L Thompson /**
209eaf62fffSJeremy L Thompson   @brief Select correct basis matrix pointer based on CeedEvalMode
210eaf62fffSJeremy L Thompson 
211352a5e7cSSebastian Grimberg   @param[in]  basis     CeedBasis from which to get the basis matrix
212eaf62fffSJeremy L Thompson   @param[in]  eval_mode Current basis evaluation mode
213eaf62fffSJeremy L Thompson   @param[in]  identity  Pointer to identity matrix
214eaf62fffSJeremy L Thompson   @param[out] basis_ptr Basis pointer to set
215eaf62fffSJeremy L Thompson 
216eaf62fffSJeremy L Thompson   @ref Developer
217eaf62fffSJeremy L Thompson **/
218352a5e7cSSebastian Grimberg static inline int CeedOperatorGetBasisPointer(CeedBasis basis, CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar **basis_ptr) {
219eaf62fffSJeremy L Thompson   switch (eval_mode) {
220eaf62fffSJeremy L Thompson     case CEED_EVAL_NONE:
221eaf62fffSJeremy L Thompson       *basis_ptr = identity;
222eaf62fffSJeremy L Thompson       break;
223eaf62fffSJeremy L Thompson     case CEED_EVAL_INTERP:
224352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetInterp(basis, basis_ptr));
225eaf62fffSJeremy L Thompson       break;
226eaf62fffSJeremy L Thompson     case CEED_EVAL_GRAD:
227352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetGrad(basis, basis_ptr));
228352a5e7cSSebastian Grimberg       break;
229352a5e7cSSebastian Grimberg     case CEED_EVAL_DIV:
230352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetDiv(basis, basis_ptr));
231352a5e7cSSebastian Grimberg       break;
232352a5e7cSSebastian Grimberg     case CEED_EVAL_CURL:
233352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetCurl(basis, basis_ptr));
234eaf62fffSJeremy L Thompson       break;
235eaf62fffSJeremy L Thompson     case CEED_EVAL_WEIGHT:
236eaf62fffSJeremy L Thompson       break;  // Caught by QF Assembly
237eaf62fffSJeremy L Thompson   }
238ed9e99e6SJeremy L Thompson   assert(*basis_ptr != NULL);
239352a5e7cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
240eaf62fffSJeremy L Thompson }
241eaf62fffSJeremy L Thompson 
242eaf62fffSJeremy L Thompson /**
243eaf62fffSJeremy L Thompson   @brief Create point block restriction for active operator field
244eaf62fffSJeremy L Thompson 
245eaf62fffSJeremy L Thompson   @param[in]  rstr            Original CeedElemRestriction for active field
246ea61e9acSJeremy L Thompson   @param[out] pointblock_rstr Address of the variable where the newly created CeedElemRestriction will be stored
247eaf62fffSJeremy L Thompson 
248eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
249eaf62fffSJeremy L Thompson 
250eaf62fffSJeremy L Thompson   @ref Developer
251eaf62fffSJeremy L Thompson **/
2522b730f8bSJeremy L Thompson static int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *pointblock_rstr) {
253eaf62fffSJeremy L Thompson   Ceed           ceed;
2541c66c397SJeremy L Thompson   CeedInt        num_elem, num_comp, shift, elem_size, comp_stride, *pointblock_offsets;
2551c66c397SJeremy L Thompson   CeedSize       l_size;
256eaf62fffSJeremy L Thompson   const CeedInt *offsets;
2571c66c397SJeremy L Thompson 
2581c66c397SJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
2592b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
260eaf62fffSJeremy L Thompson 
261eaf62fffSJeremy L Thompson   // Expand offsets
2622b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
2632b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
2642b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
2652b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
2662b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
2671c66c397SJeremy L Thompson   shift = num_comp;
2682b730f8bSJeremy L Thompson   if (comp_stride != 1) shift *= num_comp;
2692b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(num_elem * elem_size, &pointblock_offsets));
270eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_elem * elem_size; i++) {
271eaf62fffSJeremy L Thompson     pointblock_offsets[i] = offsets[i] * shift;
272eaf62fffSJeremy L Thompson   }
273eaf62fffSJeremy L Thompson 
274eaf62fffSJeremy L Thompson   // Create new restriction
2752b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
2762b730f8bSJeremy L Thompson                                      pointblock_offsets, pointblock_rstr));
277eaf62fffSJeremy L Thompson 
278eaf62fffSJeremy L Thompson   // Cleanup
2792b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
280eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
281eaf62fffSJeremy L Thompson }
282eaf62fffSJeremy L Thompson 
283eaf62fffSJeremy L Thompson /**
284eaf62fffSJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
285eaf62fffSJeremy L Thompson 
286eaf62fffSJeremy L Thompson   @param[in]  op            CeedOperator to assemble point block diagonal
287ea61e9acSJeremy L Thompson   @param[in]  request       Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE
288eaf62fffSJeremy L Thompson   @param[in]  is_pointblock Boolean flag to assemble diagonal or point block diagonal
289eaf62fffSJeremy L Thompson   @param[out] assembled     CeedVector to store assembled diagonal
290eaf62fffSJeremy L Thompson 
291eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
292eaf62fffSJeremy L Thompson 
293eaf62fffSJeremy L Thompson   @ref Developer
294eaf62fffSJeremy L Thompson **/
2952b730f8bSJeremy L Thompson static inline int CeedSingleOperatorAssembleAddDiagonal_Core(CeedOperator op, CeedRequest *request, const bool is_pointblock, CeedVector assembled) {
296eaf62fffSJeremy L Thompson   Ceed                ceed;
2971c66c397SJeremy L Thompson   CeedInt             num_input_fields, num_output_fields;
2981c66c397SJeremy L Thompson   CeedInt             layout[3];
299437c7c90SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
300c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
301c5f45aeaSJeremy L Thompson   CeedElemRestriction assembled_elem_rstr = NULL;
3021c66c397SJeremy L Thompson   CeedQFunction       qf;
303437c7c90SJeremy L Thompson 
3041c66c397SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
3051c66c397SJeremy L Thompson 
3061c66c397SJeremy L Thompson   // Assemble QFunction
307437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
308437c7c90SJeremy L Thompson   CeedCall(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
309437c7c90SJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request));
310437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, &layout));
311437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
312437c7c90SJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
313eaf62fffSJeremy L Thompson 
314ed9e99e6SJeremy L Thompson   // Get assembly data
315437c7c90SJeremy L Thompson   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
316437c7c90SJeremy L Thompson   CeedInt                 *num_eval_modes_in, *num_eval_modes_out, num_active_bases;
317437c7c90SJeremy L Thompson   CeedSize               **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components;
318437c7c90SJeremy L Thompson   CeedElemRestriction     *active_elem_rstrs;
3191c66c397SJeremy L Thompson   CeedBasis               *active_bases;
3201c66c397SJeremy L Thompson   CeedOperatorAssemblyData data;
3211c66c397SJeremy L Thompson 
322437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
323437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in,
324437c7c90SJeremy L Thompson                                                 &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out, &num_output_components));
325437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases, NULL, NULL));
326437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs));
327437c7c90SJeremy L Thompson 
328437c7c90SJeremy L Thompson   // Loop over all active bases
329437c7c90SJeremy L Thompson   for (CeedInt b = 0; b < num_active_bases; b++) {
3301c66c397SJeremy L Thompson     bool                has_eval_none = false;
3311c66c397SJeremy L Thompson     CeedInt             num_elem, num_nodes, num_qpts, num_components;
3321c66c397SJeremy L Thompson     CeedScalar         *elem_diag_array, *identity = NULL;
3331c66c397SJeremy L Thompson     CeedVector          elem_diag;
3347c1dbaffSSebastian Grimberg     CeedElemRestriction diag_elem_rstr;
3351c66c397SJeremy L Thompson 
3361c66c397SJeremy L Thompson     // Assemble point block diagonal restriction, if needed
337eaf62fffSJeremy L Thompson     if (is_pointblock) {
3387c1dbaffSSebastian Grimberg       CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstrs[b], &diag_elem_rstr));
3397c1dbaffSSebastian Grimberg     } else {
3407c1dbaffSSebastian Grimberg       CeedCall(CeedElemRestrictionCreateUnsignedCopy(active_elem_rstrs[b], &diag_elem_rstr));
341eaf62fffSJeremy L Thompson     }
342eaf62fffSJeremy L Thompson 
343eaf62fffSJeremy L Thompson     // Create diagonal vector
344437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag));
345eaf62fffSJeremy L Thompson 
346eaf62fffSJeremy L Thompson     // Assemble element operator diagonals
3472b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(elem_diag, 0.0));
3482b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array));
349437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem));
350437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(active_bases[b], &num_nodes));
351437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(active_bases[b], &num_components));
352437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(active_bases[b], &num_qpts));
353ed9e99e6SJeremy L Thompson 
354352a5e7cSSebastian Grimberg     // Construct identity matrix for basis if required
355437c7c90SJeremy L Thompson     for (CeedInt i = 0; i < num_eval_modes_in[b]; i++) {
356437c7c90SJeremy L Thompson       has_eval_none = has_eval_none || (eval_modes_in[b][i] == CEED_EVAL_NONE);
357ed9e99e6SJeremy L Thompson     }
358437c7c90SJeremy L Thompson     for (CeedInt i = 0; i < num_eval_modes_out[b]; i++) {
359437c7c90SJeremy L Thompson       has_eval_none = has_eval_none || (eval_modes_out[b][i] == CEED_EVAL_NONE);
360ed9e99e6SJeremy L Thompson     }
361ed9e99e6SJeremy L Thompson     if (has_eval_none) {
3622b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
3632b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
364eaf62fffSJeremy L Thompson     }
365352a5e7cSSebastian Grimberg 
366eaf62fffSJeremy L Thompson     // Compute the diagonal of B^T D B
367eaf62fffSJeremy L Thompson     // Each element
368b94338b9SJed Brown     for (CeedSize e = 0; e < num_elem; e++) {
369eaf62fffSJeremy L Thompson       // Each basis eval mode pair
370352a5e7cSSebastian Grimberg       CeedInt      d_out              = 0, q_comp_out;
371352a5e7cSSebastian Grimberg       CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
3721c66c397SJeremy L Thompson 
373437c7c90SJeremy L Thompson       for (CeedInt e_out = 0; e_out < num_eval_modes_out[b]; e_out++) {
3741c66c397SJeremy L Thompson         CeedInt           d_in              = 0, q_comp_in;
375437c7c90SJeremy L Thompson         const CeedScalar *B_t               = NULL;
3761c66c397SJeremy L Thompson         CeedEvalMode      eval_mode_in_prev = CEED_EVAL_NONE;
3771c66c397SJeremy L Thompson 
378352a5e7cSSebastian Grimberg         CeedOperatorGetBasisPointer(active_bases[b], eval_modes_out[b][e_out], identity, &B_t);
379352a5e7cSSebastian Grimberg         CeedCall(CeedBasisGetNumQuadratureComponents(active_bases[b], eval_modes_out[b][e_out], &q_comp_out));
380352a5e7cSSebastian Grimberg         if (q_comp_out > 1) {
381352a5e7cSSebastian Grimberg           if (e_out == 0 || eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0;
382352a5e7cSSebastian Grimberg           else B_t = &B_t[(++d_out) * num_qpts * num_nodes];
383352a5e7cSSebastian Grimberg         }
384352a5e7cSSebastian Grimberg         eval_mode_out_prev = eval_modes_out[b][e_out];
385352a5e7cSSebastian Grimberg 
386437c7c90SJeremy L Thompson         for (CeedInt e_in = 0; e_in < num_eval_modes_in[b]; e_in++) {
387437c7c90SJeremy L Thompson           const CeedScalar *B = NULL;
3881c66c397SJeremy L Thompson 
389352a5e7cSSebastian Grimberg           CeedOperatorGetBasisPointer(active_bases[b], eval_modes_in[b][e_in], identity, &B);
390352a5e7cSSebastian Grimberg           CeedCall(CeedBasisGetNumQuadratureComponents(active_bases[b], eval_modes_in[b][e_in], &q_comp_in));
391352a5e7cSSebastian Grimberg           if (q_comp_in > 1) {
392352a5e7cSSebastian Grimberg             if (e_in == 0 || eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0;
393352a5e7cSSebastian Grimberg             else B = &B[(++d_in) * num_qpts * num_nodes];
394352a5e7cSSebastian Grimberg           }
395352a5e7cSSebastian Grimberg           eval_mode_in_prev = eval_modes_in[b][e_in];
396352a5e7cSSebastian Grimberg 
397eaf62fffSJeremy L Thompson           // Each component
398437c7c90SJeremy L Thompson           for (CeedInt c_out = 0; c_out < num_components; c_out++) {
399437c7c90SJeremy L Thompson             // Each qpt/node pair
4002b730f8bSJeremy L Thompson             for (CeedInt q = 0; q < num_qpts; q++) {
401eaf62fffSJeremy L Thompson               if (is_pointblock) {
402eaf62fffSJeremy L Thompson                 // Point Block Diagonal
403437c7c90SJeremy L Thompson                 for (CeedInt c_in = 0; c_in < num_components; c_in++) {
404b94338b9SJed Brown                   const CeedSize c_offset = (eval_mode_offsets_in[b][e_in] + c_in) * num_output_components + eval_mode_offsets_out[b][e_out] + c_out;
405437c7c90SJeremy L Thompson                   const CeedScalar qf_value = assembled_qf_array[q * layout[0] + c_offset * layout[1] + e * layout[2]];
4061c66c397SJeremy L Thompson 
4072b730f8bSJeremy L Thompson                   for (CeedInt n = 0; n < num_nodes; n++) {
408437c7c90SJeremy L Thompson                     elem_diag_array[((e * num_components + c_out) * num_components + c_in) * num_nodes + n] +=
409437c7c90SJeremy L Thompson                         B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n];
410eaf62fffSJeremy L Thompson                   }
4112b730f8bSJeremy L Thompson                 }
412eaf62fffSJeremy L Thompson               } else {
413eaf62fffSJeremy L Thompson                 // Diagonal Only
414437c7c90SJeremy L Thompson                 const CeedInt    c_offset = (eval_mode_offsets_in[b][e_in] + c_out) * num_output_components + eval_mode_offsets_out[b][e_out] + c_out;
415437c7c90SJeremy L Thompson                 const CeedScalar qf_value = assembled_qf_array[q * layout[0] + c_offset * layout[1] + e * layout[2]];
4161c66c397SJeremy L Thompson 
4172b730f8bSJeremy L Thompson                 for (CeedInt n = 0; n < num_nodes; n++) {
418437c7c90SJeremy L Thompson                   elem_diag_array[(e * num_components + c_out) * num_nodes + n] += B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n];
419eaf62fffSJeremy L Thompson                 }
420eaf62fffSJeremy L Thompson               }
421eaf62fffSJeremy L Thompson             }
422eaf62fffSJeremy L Thompson           }
4232b730f8bSJeremy L Thompson         }
4242b730f8bSJeremy L Thompson       }
4252b730f8bSJeremy L Thompson     }
4262b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
427eaf62fffSJeremy L Thompson 
428eaf62fffSJeremy L Thompson     // Assemble local operator diagonal
4297c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
430eaf62fffSJeremy L Thompson 
431eaf62fffSJeremy L Thompson     // Cleanup
4327c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr));
4332b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&elem_diag));
4342b730f8bSJeremy L Thompson     CeedCall(CeedFree(&identity));
435437c7c90SJeremy L Thompson   }
436437c7c90SJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
437437c7c90SJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
438eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
439eaf62fffSJeremy L Thompson }
440eaf62fffSJeremy L Thompson 
441eaf62fffSJeremy L Thompson /**
442eaf62fffSJeremy L Thompson   @brief Core logic for assembling composite operator diagonal
443eaf62fffSJeremy L Thompson 
444eaf62fffSJeremy L Thompson   @param[in]  op            CeedOperator to assemble point block diagonal
445ea61e9acSJeremy L Thompson   @param[in]  request       Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE
446eaf62fffSJeremy L Thompson   @param[in]  is_pointblock Boolean flag to assemble diagonal or point block diagonal
447eaf62fffSJeremy L Thompson   @param[out] assembled     CeedVector to store assembled diagonal
448eaf62fffSJeremy L Thompson 
449eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
450eaf62fffSJeremy L Thompson 
451eaf62fffSJeremy L Thompson   @ref Developer
452eaf62fffSJeremy L Thompson **/
4532b730f8bSJeremy L Thompson static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_pointblock,
454eaf62fffSJeremy L Thompson                                                                  CeedVector assembled) {
455eaf62fffSJeremy L Thompson   CeedInt       num_sub;
456eaf62fffSJeremy L Thompson   CeedOperator *suboperators;
4571c66c397SJeremy L Thompson 
458c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
459c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators));
460eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_sub; i++) {
4616aa95790SJeremy L Thompson     if (is_pointblock) {
4622b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request));
4636aa95790SJeremy L Thompson     } else {
4642b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request));
4656aa95790SJeremy L Thompson     }
466eaf62fffSJeremy L Thompson   }
467eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
468eaf62fffSJeremy L Thompson }
469eaf62fffSJeremy L Thompson 
470eaf62fffSJeremy L Thompson /**
471eaf62fffSJeremy L Thompson   @brief Build nonzero pattern for non-composite operator
472eaf62fffSJeremy L Thompson 
473eaf62fffSJeremy L Thompson   Users should generally use CeedOperatorLinearAssembleSymbolic()
474eaf62fffSJeremy L Thompson 
475eaf62fffSJeremy L Thompson   @param[in]  op     CeedOperator to assemble nonzero pattern
476eaf62fffSJeremy L Thompson   @param[in]  offset Offset for number of entries
477eaf62fffSJeremy L Thompson   @param[out] rows   Row number for each entry
478eaf62fffSJeremy L Thompson   @param[out] cols   Column number for each entry
479eaf62fffSJeremy L Thompson 
480eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
481eaf62fffSJeremy L Thompson 
482eaf62fffSJeremy L Thompson   @ref Developer
483eaf62fffSJeremy L Thompson **/
4842b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) {
485f3d47e36SJeremy L Thompson   Ceed                ceed;
486f3d47e36SJeremy L Thompson   bool                is_composite;
4871c66c397SJeremy L Thompson   CeedInt             num_elem, elem_size, num_comp, layout_er[3], local_num_entries;
4881c66c397SJeremy L Thompson   CeedSize            num_nodes, count = 0;
4891c66c397SJeremy L Thompson   CeedScalar         *array;
4901c66c397SJeremy L Thompson   const CeedScalar   *elem_dof_a;
4911c66c397SJeremy L Thompson   CeedVector          index_vec, elem_dof;
4921c66c397SJeremy L Thompson   CeedElemRestriction active_rstr, index_elem_rstr;
4931c66c397SJeremy L Thompson 
494f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
495f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4966574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
497eaf62fffSJeremy L Thompson 
4982b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes, NULL));
4997c1dbaffSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestriction(op, &active_rstr));
5007c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionCreateUnorientedCopy(active_rstr, &index_elem_rstr));
5017c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(index_elem_rstr, &num_elem));
5027c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(index_elem_rstr, &elem_size));
5037c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(index_elem_rstr, &num_comp));
5047c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionGetELayout(index_elem_rstr, &layout_er));
5051c66c397SJeremy L Thompson   local_num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
506eaf62fffSJeremy L Thompson 
507eaf62fffSJeremy L Thompson   // Determine elem_dof relation
5082b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, num_nodes, &index_vec));
5092b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayWrite(index_vec, CEED_MEM_HOST, &array));
510ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_nodes; i++) array[i] = i;
5112b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(index_vec, &array));
5122b730f8bSJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, num_elem * elem_size * num_comp, &elem_dof));
5132b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(elem_dof, 0.0));
5147c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionApply(index_elem_rstr, CEED_NOTRANSPOSE, index_vec, elem_dof, CEED_REQUEST_IMMEDIATE));
5152b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(elem_dof, CEED_MEM_HOST, &elem_dof_a));
5162b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&index_vec));
517eaf62fffSJeremy L Thompson 
518eaf62fffSJeremy L Thompson   // Determine i, j locations for element matrices
519ed9e99e6SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
520ed9e99e6SJeremy L Thompson     for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
521ed9e99e6SJeremy L Thompson       for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
522ed9e99e6SJeremy L Thompson         for (CeedInt i = 0; i < elem_size; i++) {
523ed9e99e6SJeremy L Thompson           for (CeedInt j = 0; j < elem_size; j++) {
5242b730f8bSJeremy L Thompson             const CeedInt elem_dof_index_row = i * layout_er[0] + (comp_out)*layout_er[1] + e * layout_er[2];
5252b730f8bSJeremy L Thompson             const CeedInt elem_dof_index_col = j * layout_er[0] + comp_in * layout_er[1] + e * layout_er[2];
526eaf62fffSJeremy L Thompson             const CeedInt row                = elem_dof_a[elem_dof_index_row];
527eaf62fffSJeremy L Thompson             const CeedInt col                = elem_dof_a[elem_dof_index_col];
528eaf62fffSJeremy L Thompson 
529eaf62fffSJeremy L Thompson             rows[offset + count] = row;
530eaf62fffSJeremy L Thompson             cols[offset + count] = col;
531eaf62fffSJeremy L Thompson             count++;
532eaf62fffSJeremy L Thompson           }
533eaf62fffSJeremy L Thompson         }
534eaf62fffSJeremy L Thompson       }
535eaf62fffSJeremy L Thompson     }
536eaf62fffSJeremy L Thompson   }
5376574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing assembled entries");
5382b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(elem_dof, &elem_dof_a));
5392b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&elem_dof));
5407c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr));
541eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
542eaf62fffSJeremy L Thompson }
543eaf62fffSJeremy L Thompson 
544eaf62fffSJeremy L Thompson /**
545eaf62fffSJeremy L Thompson   @brief Assemble nonzero entries for non-composite operator
546eaf62fffSJeremy L Thompson 
547eaf62fffSJeremy L Thompson   Users should generally use CeedOperatorLinearAssemble()
548eaf62fffSJeremy L Thompson 
549eaf62fffSJeremy L Thompson   @param[in]  op     CeedOperator to assemble
550ea61e9acSJeremy L Thompson   @param[in]  offset Offset for number of entries
551eaf62fffSJeremy L Thompson   @param[out] values Values to assemble into matrix
552eaf62fffSJeremy L Thompson 
553eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
554eaf62fffSJeremy L Thompson 
555eaf62fffSJeremy L Thompson   @ref Developer
556eaf62fffSJeremy L Thompson **/
5572b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) {
558f3d47e36SJeremy L Thompson   Ceed ceed;
559f3d47e36SJeremy L Thompson   bool is_composite;
5601c66c397SJeremy L Thompson 
561f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
562f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
563f3d47e36SJeremy L Thompson 
5646574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
565f3d47e36SJeremy L Thompson 
566f3d47e36SJeremy L Thompson   // Early exit for empty operator
567f3d47e36SJeremy L Thompson   {
568f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
569f3d47e36SJeremy L Thompson 
570f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
571f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
572f3d47e36SJeremy L Thompson   }
573eaf62fffSJeremy L Thompson 
574cefa2673SJeremy L Thompson   if (op->LinearAssembleSingle) {
575cefa2673SJeremy L Thompson     // Backend version
5762b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSingle(op, offset, values));
577cefa2673SJeremy L Thompson     return CEED_ERROR_SUCCESS;
578cefa2673SJeremy L Thompson   } else {
579cefa2673SJeremy L Thompson     // Operator fallback
580cefa2673SJeremy L Thompson     CeedOperator op_fallback;
581cefa2673SJeremy L Thompson 
5822b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
583cefa2673SJeremy L Thompson     if (op_fallback) {
5842b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values));
585cefa2673SJeremy L Thompson       return CEED_ERROR_SUCCESS;
586cefa2673SJeremy L Thompson     }
587cefa2673SJeremy L Thompson   }
588cefa2673SJeremy L Thompson 
589eaf62fffSJeremy L Thompson   // Assemble QFunction
5901c66c397SJeremy L Thompson   const bool     *orients      = NULL;
5911c66c397SJeremy L Thompson   const CeedInt8 *curl_orients = NULL;
5921c66c397SJeremy L Thompson   CeedInt *num_eval_modes_in, *num_eval_modes_out, num_active_bases, num_input_fields, num_output_fields, num_elem, elem_size, num_qpts, num_comp,
5931c66c397SJeremy L Thompson       local_num_entries, layout_qf[3];
5941c66c397SJeremy L Thompson   const CeedScalar        *assembled_qf_array;
595c5f45aeaSJeremy L Thompson   CeedVector               assembled_qf = NULL;
5961c66c397SJeremy L Thompson   CeedRestrictionType      rstr_type;
5971c66c397SJeremy L Thompson   CeedElemRestriction      rstr_q = NULL, active_rstr;
5981c66c397SJeremy L Thompson   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
5991c66c397SJeremy L Thompson   CeedBasis               *bases, basis_in;
6001c66c397SJeremy L Thompson   CeedQFunction            qf;
6011c66c397SJeremy L Thompson   CeedOperatorAssemblyData data;
6021c66c397SJeremy L Thompson   CeedOperatorField       *input_fields, *output_fields;
603eaf62fffSJeremy L Thompson 
6041c66c397SJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
6051c66c397SJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE));
6062b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
607eaf62fffSJeremy L Thompson 
608ed9e99e6SJeremy L Thompson   // Get assembly data
6092b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
610437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases, &num_eval_modes_in, &eval_modes_in, NULL, &num_eval_modes_out,
611437c7c90SJeremy L Thompson                                                 &eval_modes_out, NULL, NULL));
612437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &bases, NULL, NULL));
6131c66c397SJeremy L Thompson   basis_in = bases[0];
614eaf62fffSJeremy L Thompson 
6156574a04fSJeremy L Thompson   CeedCheck(num_active_bases == 1, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator with multiple active bases");
6166574a04fSJeremy L Thompson   CeedCheck(num_eval_modes_in[0] > 0 && num_eval_modes_out[0] > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator with out inputs/outputs");
617eaf62fffSJeremy L Thompson 
6182b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &active_rstr));
6192b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(active_rstr, &num_elem));
6202b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(active_rstr, &elem_size));
6212b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(active_rstr, &num_comp));
6222b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
6231c66c397SJeremy L Thompson   local_num_entries = elem_size * num_comp * elem_size * num_comp * num_elem;
624eaf62fffSJeremy L Thompson 
6257c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionGetType(active_rstr, &rstr_type));
6267c1dbaffSSebastian Grimberg   if (rstr_type == CEED_RESTRICTION_ORIENTED) {
6277c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOrientations(active_rstr, CEED_MEM_HOST, &orients));
6287c1dbaffSSebastian Grimberg   } else if (rstr_type == CEED_RESTRICTION_CURL_ORIENTED) {
6297c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetCurlOrientations(active_rstr, CEED_MEM_HOST, &curl_orients));
6307c1dbaffSSebastian Grimberg   }
6317c1dbaffSSebastian Grimberg 
6327c1dbaffSSebastian Grimberg   // Loop over elements and put in data structure
6332b730f8bSJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
6342b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(rstr_q, &layout_qf));
6352b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_q));
636eaf62fffSJeremy L Thompson 
6377c1dbaffSSebastian Grimberg   // We store B_mat_in, B_mat_out, BTD, elem_mat in row-major order
6381c66c397SJeremy L Thompson   CeedSize           count = 0;
6391c66c397SJeremy L Thompson   CeedScalar        *vals, BTD_mat[elem_size * num_qpts * num_eval_modes_in[0]], elem_mat[elem_size * elem_size];
640437c7c90SJeremy L Thompson   const CeedScalar **B_mats_in, **B_mats_out;
641437c7c90SJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, NULL, &B_mats_in, &B_mats_out));
642437c7c90SJeremy L Thompson   const CeedScalar *B_mat_in = B_mats_in[0], *B_mat_out = B_mats_out[0];
6431c66c397SJeremy L Thompson 
64428ec399dSJeremy L Thompson   CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals));
645b94338b9SJed Brown   for (CeedSize e = 0; e < num_elem; e++) {
646ed9e99e6SJeremy L Thompson     for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
647ed9e99e6SJeremy L Thompson       for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
648ed9e99e6SJeremy L Thompson         // Compute B^T*D
649b94338b9SJed Brown         for (CeedSize n = 0; n < elem_size; n++) {
650b94338b9SJed Brown           for (CeedSize q = 0; q < num_qpts; q++) {
651437c7c90SJeremy L Thompson             for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) {
652b94338b9SJed Brown               const CeedSize btd_index = n * (num_qpts * num_eval_modes_in[0]) + (num_eval_modes_in[0] * q + e_in);
653067fd99fSJeremy L Thompson               CeedScalar     sum       = 0.0;
6541c66c397SJeremy L Thompson 
655437c7c90SJeremy L Thompson               for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) {
656b94338b9SJed Brown                 const CeedSize b_out_index     = (num_eval_modes_out[0] * q + e_out) * elem_size + n;
657b94338b9SJed Brown                 const CeedSize eval_mode_index = ((e_in * num_comp + comp_in) * num_eval_modes_out[0] + e_out) * num_comp + comp_out;
658b94338b9SJed Brown                 const CeedSize qf_index        = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2];
6591c66c397SJeremy L Thompson 
660067fd99fSJeremy L Thompson                 sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index];
661eaf62fffSJeremy L Thompson               }
662067fd99fSJeremy L Thompson               BTD_mat[btd_index] = sum;
663ed9e99e6SJeremy L Thompson             }
664ed9e99e6SJeremy L Thompson           }
665eaf62fffSJeremy L Thompson         }
6667c1dbaffSSebastian Grimberg 
6677c1dbaffSSebastian Grimberg         // Form element matrix itself (for each block component)
668437c7c90SJeremy L Thompson         CeedCall(CeedMatrixMatrixMultiply(ceed, BTD_mat, B_mat_in, elem_mat, elem_size, elem_size, num_qpts * num_eval_modes_in[0]));
669eaf62fffSJeremy L Thompson 
6707c1dbaffSSebastian Grimberg         // Transform the element matrix if required
6717c1dbaffSSebastian Grimberg         if (orients) {
6727c1dbaffSSebastian Grimberg           const bool *elem_orients = &orients[e * elem_size];
6731c66c397SJeremy L Thompson 
6747c1dbaffSSebastian Grimberg           for (CeedInt i = 0; i < elem_size; i++) {
6757c1dbaffSSebastian Grimberg             for (CeedInt j = 0; j < elem_size; j++) {
6767c1dbaffSSebastian Grimberg               elem_mat[i * elem_size + j] *= elem_orients[i] ? -1.0 : 1.0;
6777c1dbaffSSebastian Grimberg               elem_mat[i * elem_size + j] *= elem_orients[j] ? -1.0 : 1.0;
6787c1dbaffSSebastian Grimberg             }
6797c1dbaffSSebastian Grimberg           }
6807c1dbaffSSebastian Grimberg         } else if (curl_orients) {
6817c1dbaffSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &curl_orients[e * 3 * elem_size];
6827c1dbaffSSebastian Grimberg           CeedScalar      o_elem_mat[elem_size * elem_size];
6831c66c397SJeremy L Thompson 
6847c1dbaffSSebastian Grimberg           // T^T*(B^T*D*B)
6857c1dbaffSSebastian Grimberg           for (CeedInt i = 0; i < elem_size; i++) {
6867c1dbaffSSebastian Grimberg             for (CeedInt j = 0; j < elem_size; j++) {
6877c1dbaffSSebastian Grimberg               o_elem_mat[i * elem_size + j] = elem_mat[i * elem_size + j] * elem_curl_orients[3 * i + 1] +
6887c1dbaffSSebastian Grimberg                                               (i > 0 ? elem_mat[(i - 1) * elem_size + j] * elem_curl_orients[3 * i - 1] : 0.0) +
6897c1dbaffSSebastian Grimberg                                               (i < elem_size - 1 ? elem_mat[(i + 1) * elem_size + j] * elem_curl_orients[3 * i + 3] : 0.0);
6907c1dbaffSSebastian Grimberg             }
6917c1dbaffSSebastian Grimberg           }
6927c1dbaffSSebastian Grimberg           // T^T*(B^T*D*B)*T
6937c1dbaffSSebastian Grimberg           for (CeedInt i = 0; i < elem_size; i++) {
6947c1dbaffSSebastian Grimberg             for (CeedInt j = 0; j < elem_size; j++) {
6957c1dbaffSSebastian Grimberg               elem_mat[i * elem_size + j] = o_elem_mat[i * elem_size + j] * elem_curl_orients[3 * j + 1] +
6967c1dbaffSSebastian Grimberg                                             (j > 0 ? o_elem_mat[i * elem_size + j - 1] * elem_curl_orients[3 * j - 1] : 0.0) +
6977c1dbaffSSebastian Grimberg                                             (j < elem_size - 1 ? o_elem_mat[i * elem_size + j + 1] * elem_curl_orients[3 * j + 3] : 0.0);
6987c1dbaffSSebastian Grimberg             }
6997c1dbaffSSebastian Grimberg           }
7007c1dbaffSSebastian Grimberg         }
7017c1dbaffSSebastian Grimberg 
7027c1dbaffSSebastian Grimberg         // Put element matrix in coordinate data structure
703ed9e99e6SJeremy L Thompson         for (CeedInt i = 0; i < elem_size; i++) {
704ed9e99e6SJeremy L Thompson           for (CeedInt j = 0; j < elem_size; j++) {
705eaf62fffSJeremy L Thompson             vals[offset + count] = elem_mat[i * elem_size + j];
706eaf62fffSJeremy L Thompson             count++;
707eaf62fffSJeremy L Thompson           }
708eaf62fffSJeremy L Thompson         }
709eaf62fffSJeremy L Thompson       }
710eaf62fffSJeremy L Thompson     }
711eaf62fffSJeremy L Thompson   }
7126574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing entries");
7132b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(values, &vals));
714eaf62fffSJeremy L Thompson 
7152b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
7162b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
717eaf62fffSJeremy L Thompson 
7187c1dbaffSSebastian Grimberg   if (rstr_type == CEED_RESTRICTION_ORIENTED) {
7197c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOrientations(active_rstr, &orients));
7207c1dbaffSSebastian Grimberg   } else if (rstr_type == CEED_RESTRICTION_CURL_ORIENTED) {
7217c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreCurlOrientations(active_rstr, &curl_orients));
7227c1dbaffSSebastian Grimberg   }
723eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
724eaf62fffSJeremy L Thompson }
725eaf62fffSJeremy L Thompson 
726eaf62fffSJeremy L Thompson /**
727eaf62fffSJeremy L Thompson   @brief Count number of entries for assembled CeedOperator
728eaf62fffSJeremy L Thompson 
729eaf62fffSJeremy L Thompson   @param[in]  op          CeedOperator to assemble
730eaf62fffSJeremy L Thompson   @param[out] num_entries Number of entries in assembled representation
731eaf62fffSJeremy L Thompson 
732eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
733eaf62fffSJeremy L Thompson 
734eaf62fffSJeremy L Thompson   @ref Utility
735eaf62fffSJeremy L Thompson **/
736b94338b9SJed Brown static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedSize *num_entries) {
737b275c451SJeremy L Thompson   bool                is_composite;
738eaf62fffSJeremy L Thompson   CeedInt             num_elem, elem_size, num_comp;
7391c66c397SJeremy L Thompson   CeedElemRestriction rstr;
740eaf62fffSJeremy L Thompson 
741b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
7426574a04fSJeremy L Thompson   CeedCheck(!is_composite, op->ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
7432b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveElemRestriction(op, &rstr));
7442b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
7452b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
7462b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
747b94338b9SJed Brown   *num_entries = (CeedSize)elem_size * num_comp * elem_size * num_comp * num_elem;
748eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
749eaf62fffSJeremy L Thompson }
750eaf62fffSJeremy L Thompson 
751eaf62fffSJeremy L Thompson /**
752ea61e9acSJeremy L Thompson   @brief Common code for creating a multigrid coarse operator and level transfer operators for a CeedOperator
753eaf62fffSJeremy L Thompson 
754eaf62fffSJeremy L Thompson   @param[in]  op_fine      Fine grid operator
75585bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
756eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid restriction
757eaf62fffSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector basis
75885bb9dcfSJeremy L Thompson   @param[in]  basis_c_to_f Basis for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
759eaf62fffSJeremy L Thompson   @param[out] op_coarse    Coarse grid operator
76085bb9dcfSJeremy L Thompson   @param[out] op_prolong   Coarse to fine operator, or NULL
761*7758292fSSebastian Grimberg   @param[out] op_restrict  Fine to coarse operator, or NULL
762eaf62fffSJeremy L Thompson 
763eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
764eaf62fffSJeremy L Thompson 
765eaf62fffSJeremy L Thompson   @ref Developer
766eaf62fffSJeremy L Thompson **/
7672b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
768*7758292fSSebastian Grimberg                                             CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
7691c66c397SJeremy L Thompson   bool                is_composite;
770eaf62fffSJeremy L Thompson   Ceed                ceed;
7711c66c397SJeremy L Thompson   CeedInt             num_comp;
77285bb9dcfSJeremy L Thompson   CeedVector          mult_vec         = NULL;
7731c66c397SJeremy L Thompson   CeedElemRestriction rstr_p_mult_fine = NULL, rstr_fine = NULL;
7741c66c397SJeremy L Thompson 
7752b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
776eaf62fffSJeremy L Thompson 
777eaf62fffSJeremy L Thompson   // Check for composite operator
7782b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op_fine, &is_composite));
7796574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported");
780eaf62fffSJeremy L Thompson 
781eaf62fffSJeremy L Thompson   // Coarse Grid
7822b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
783eaf62fffSJeremy L Thompson   // -- Clone input fields
78492ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < op_fine->qf->num_input_fields; i++) {
785eaf62fffSJeremy L Thompson     if (op_fine->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
786437c7c90SJeremy L Thompson       rstr_fine = op_fine->input_fields[i]->elem_rstr;
7872b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE));
788eaf62fffSJeremy L Thompson     } else {
789437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, op_fine->input_fields[i]->elem_rstr,
7902b730f8bSJeremy L Thompson                                     op_fine->input_fields[i]->basis, op_fine->input_fields[i]->vec));
791eaf62fffSJeremy L Thompson     }
792eaf62fffSJeremy L Thompson   }
793eaf62fffSJeremy L Thompson   // -- Clone output fields
79492ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < op_fine->qf->num_output_fields; i++) {
795eaf62fffSJeremy L Thompson     if (op_fine->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
7962b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE));
797eaf62fffSJeremy L Thompson     } else {
798437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, op_fine->output_fields[i]->elem_rstr,
7992b730f8bSJeremy L Thompson                                     op_fine->output_fields[i]->basis, op_fine->output_fields[i]->vec));
800eaf62fffSJeremy L Thompson     }
801eaf62fffSJeremy L Thompson   }
802af99e877SJeremy L Thompson   // -- Clone QFunctionAssemblyData
8032b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op_fine->qf_assembled, &(*op_coarse)->qf_assembled));
804eaf62fffSJeremy L Thompson 
805eaf62fffSJeremy L Thompson   // Multiplicity vector
806*7758292fSSebastian Grimberg   if (op_restrict || op_prolong) {
80785bb9dcfSJeremy L Thompson     CeedVector          mult_e_vec;
8081c66c397SJeremy L Thompson     CeedRestrictionType rstr_type;
80985bb9dcfSJeremy L Thompson 
8107c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr_fine, &rstr_type));
8117c1dbaffSSebastian Grimberg     CeedCheck(rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_UNSUPPORTED,
8127c1dbaffSSebastian Grimberg               "Element restrictions created with CeedElemRestrictionCreateCurlOriented are not supported");
8136574a04fSJeremy L Thompson     CeedCheck(p_mult_fine, ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector");
8147c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnsignedCopy(rstr_fine, &rstr_p_mult_fine));
8152b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec));
8162b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_e_vec, 0.0));
817c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE));
8182b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_vec, 0.0));
819c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE));
8202b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&mult_e_vec));
8212b730f8bSJeremy L Thompson     CeedCall(CeedVectorReciprocal(mult_vec));
82285bb9dcfSJeremy L Thompson   }
823eaf62fffSJeremy L Thompson 
824addd79feSZach Atkins   // Clone name
825addd79feSZach Atkins   bool   has_name = op_fine->name;
826addd79feSZach Atkins   size_t name_len = op_fine->name ? strlen(op_fine->name) : 0;
827addd79feSZach Atkins   CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name));
828addd79feSZach Atkins 
829*7758292fSSebastian Grimberg   // Check that coarse to fine basis is provided if prolong/restrict operators are requested
830*7758292fSSebastian Grimberg   CeedCheck(basis_c_to_f || (!op_restrict && !op_prolong), ceed, CEED_ERROR_INCOMPATIBLE,
8316574a04fSJeremy L Thompson             "Prolongation or restriction operator creation requires coarse-to-fine basis");
83283d6adf3SZach Atkins 
83385bb9dcfSJeremy L Thompson   // Restriction/Prolongation Operators
8342b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp));
835addd79feSZach Atkins 
836addd79feSZach Atkins   // Restriction
837*7758292fSSebastian Grimberg   if (op_restrict) {
838eaf62fffSJeremy L Thompson     CeedInt             *num_comp_r_data;
83985bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_r;
840*7758292fSSebastian Grimberg     CeedQFunction        qf_restrict;
84185bb9dcfSJeremy L Thompson 
842*7758292fSSebastian Grimberg     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict));
8432b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_r_data));
844eaf62fffSJeremy L Thompson     num_comp_r_data[0] = num_comp;
8452b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r));
8462b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data));
847*7758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r));
8482b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_r));
849*7758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE));
850*7758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE));
851*7758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP));
852*7758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp));
853eaf62fffSJeremy L Thompson 
854*7758292fSSebastian Grimberg     CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict));
855*7758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
856*7758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
857*7758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
858eaf62fffSJeremy L Thompson 
859addd79feSZach Atkins     // Set name
860addd79feSZach Atkins     char *restriction_name;
8611c66c397SJeremy L Thompson 
862addd79feSZach Atkins     CeedCall(CeedCalloc(17 + name_len, &restriction_name));
863addd79feSZach Atkins     sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
864*7758292fSSebastian Grimberg     CeedCall(CeedOperatorSetName(*op_restrict, restriction_name));
865addd79feSZach Atkins     CeedCall(CeedFree(&restriction_name));
866addd79feSZach Atkins 
867addd79feSZach Atkins     // Check
868*7758292fSSebastian Grimberg     CeedCall(CeedOperatorCheckReady(*op_restrict));
869addd79feSZach Atkins 
870addd79feSZach Atkins     // Cleanup
871*7758292fSSebastian Grimberg     CeedCall(CeedQFunctionDestroy(&qf_restrict));
872addd79feSZach Atkins   }
873addd79feSZach Atkins 
874eaf62fffSJeremy L Thompson   // Prolongation
875addd79feSZach Atkins   if (op_prolong) {
876eaf62fffSJeremy L Thompson     CeedInt             *num_comp_p_data;
87785bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_p;
8781c66c397SJeremy L Thompson     CeedQFunction        qf_prolong;
87985bb9dcfSJeremy L Thompson 
88085bb9dcfSJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong));
8812b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_p_data));
882eaf62fffSJeremy L Thompson     num_comp_p_data[0] = num_comp;
8832b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p));
8842b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data));
8852b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p));
8862b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_p));
8872b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP));
8882b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE));
8892b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE));
8902b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp));
891eaf62fffSJeremy L Thompson 
8922b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong));
8932b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
894356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
895356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
896eaf62fffSJeremy L Thompson 
897addd79feSZach Atkins     // Set name
898ea6b5821SJeremy L Thompson     char *prolongation_name;
8991c66c397SJeremy L Thompson 
9002b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(18 + name_len, &prolongation_name));
9012b730f8bSJeremy L Thompson     sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
9022b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name));
9032b730f8bSJeremy L Thompson     CeedCall(CeedFree(&prolongation_name));
904addd79feSZach Atkins 
905addd79feSZach Atkins     // Check
906addd79feSZach Atkins     CeedCall(CeedOperatorCheckReady(*op_prolong));
907addd79feSZach Atkins 
908addd79feSZach Atkins     // Cleanup
909addd79feSZach Atkins     CeedCall(CeedQFunctionDestroy(&qf_prolong));
910ea6b5821SJeremy L Thompson   }
911ea6b5821SJeremy L Thompson 
91258e4b056SJeremy L Thompson   // Check
91358e4b056SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(*op_coarse));
91458e4b056SJeremy L Thompson 
915eaf62fffSJeremy L Thompson   // Cleanup
9162b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_vec));
917c17ec2beSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine));
9182b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis_c_to_f));
919eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
920eaf62fffSJeremy L Thompson }
921eaf62fffSJeremy L Thompson 
922eaf62fffSJeremy L Thompson /**
923eaf62fffSJeremy L Thompson   @brief Build 1D mass matrix and Laplacian with perturbation
924eaf62fffSJeremy L Thompson 
925eaf62fffSJeremy L Thompson   @param[in]  interp_1d   Interpolation matrix in one dimension
926eaf62fffSJeremy L Thompson   @param[in]  grad_1d     Gradient matrix in one dimension
927eaf62fffSJeremy L Thompson   @param[in]  q_weight_1d Quadrature weights in one dimension
928eaf62fffSJeremy L Thompson   @param[in]  P_1d        Number of basis nodes in one dimension
929eaf62fffSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
930eaf62fffSJeremy L Thompson   @param[in]  dim         Dimension of basis
931eaf62fffSJeremy L Thompson   @param[out] mass        Assembled mass matrix in one dimension
932eaf62fffSJeremy L Thompson   @param[out] laplace     Assembled perturbed Laplacian in one dimension
933eaf62fffSJeremy L Thompson 
934eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
935eaf62fffSJeremy L Thompson 
936eaf62fffSJeremy L Thompson   @ref Developer
937eaf62fffSJeremy L Thompson **/
9382c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
9392c2ea1dbSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d, CeedInt P_1d, CeedInt Q_1d,
9402c2ea1dbSJeremy L Thompson                                 CeedInt dim, CeedScalar *mass, CeedScalar *laplace) {
9412b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
942eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
943eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
9442b730f8bSJeremy 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];
945eaf62fffSJeremy L Thompson       mass[i + j * P_1d] = sum;
946eaf62fffSJeremy L Thompson     }
9472b730f8bSJeremy L Thompson   }
948eaf62fffSJeremy L Thompson   // -- Laplacian
9492b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
950eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
951eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
9521c66c397SJeremy L Thompson 
9532b730f8bSJeremy 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];
954eaf62fffSJeremy L Thompson       laplace[i + j * P_1d] = sum;
955eaf62fffSJeremy L Thompson     }
9562b730f8bSJeremy L Thompson   }
957eaf62fffSJeremy L Thompson   CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4;
9582b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation;
959eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
960eaf62fffSJeremy L Thompson }
9612c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
962eaf62fffSJeremy L Thompson 
963eaf62fffSJeremy L Thompson /// @}
964eaf62fffSJeremy L Thompson 
965eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
966480fae85SJeremy L Thompson /// CeedOperator Backend API
967480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
968480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend
969480fae85SJeremy L Thompson /// @{
970480fae85SJeremy L Thompson 
971480fae85SJeremy L Thompson /**
972480fae85SJeremy L Thompson   @brief Create object holding CeedQFunction assembly data for CeedOperator
973480fae85SJeremy L Thompson 
974480fae85SJeremy L Thompson   @param[in]  ceed A Ceed object where the CeedQFunctionAssemblyData will be created
975ea61e9acSJeremy L Thompson   @param[out] data Address of the variable where the newly created CeedQFunctionAssemblyData will be stored
976480fae85SJeremy L Thompson 
977480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
978480fae85SJeremy L Thompson 
979480fae85SJeremy L Thompson   @ref Backend
980480fae85SJeremy L Thompson **/
981ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) {
9822b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
983480fae85SJeremy L Thompson   (*data)->ref_count = 1;
984480fae85SJeremy L Thompson   (*data)->ceed      = ceed;
9852b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
986480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
987480fae85SJeremy L Thompson }
988480fae85SJeremy L Thompson 
989480fae85SJeremy L Thompson /**
990480fae85SJeremy L Thompson   @brief Increment the reference counter for a CeedQFunctionAssemblyData
991480fae85SJeremy L Thompson 
992ea61e9acSJeremy L Thompson   @param[in,out] data CeedQFunctionAssemblyData to increment the reference counter
993480fae85SJeremy L Thompson 
994480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
995480fae85SJeremy L Thompson 
996480fae85SJeremy L Thompson   @ref Backend
997480fae85SJeremy L Thompson **/
998480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) {
999480fae85SJeremy L Thompson   data->ref_count++;
1000480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1001480fae85SJeremy L Thompson }
1002480fae85SJeremy L Thompson 
1003480fae85SJeremy L Thompson /**
1004beecbf24SJeremy L Thompson   @brief Set re-use of CeedQFunctionAssemblyData
10058b919e6bSJeremy L Thompson 
1006ea61e9acSJeremy L Thompson   @param[in,out] data       CeedQFunctionAssemblyData to mark for reuse
1007ea61e9acSJeremy L Thompson   @param[in]     reuse_data Boolean flag indicating data re-use
10088b919e6bSJeremy L Thompson 
10098b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
10108b919e6bSJeremy L Thompson 
10118b919e6bSJeremy L Thompson   @ref Backend
10128b919e6bSJeremy L Thompson **/
10132b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) {
1014beecbf24SJeremy L Thompson   data->reuse_data        = reuse_data;
1015beecbf24SJeremy L Thompson   data->needs_data_update = true;
1016beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1017beecbf24SJeremy L Thompson }
1018beecbf24SJeremy L Thompson 
1019beecbf24SJeremy L Thompson /**
1020beecbf24SJeremy L Thompson   @brief Mark QFunctionAssemblyData as stale
1021beecbf24SJeremy L Thompson 
1022ea61e9acSJeremy L Thompson   @param[in,out] data              CeedQFunctionAssemblyData to mark as stale
1023ea61e9acSJeremy L Thompson   @param[in]     needs_data_update Boolean flag indicating if update is needed or completed
1024beecbf24SJeremy L Thompson 
1025beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1026beecbf24SJeremy L Thompson 
1027beecbf24SJeremy L Thompson   @ref Backend
1028beecbf24SJeremy L Thompson **/
10292b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) {
1030beecbf24SJeremy L Thompson   data->needs_data_update = needs_data_update;
10318b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
10328b919e6bSJeremy L Thompson }
10338b919e6bSJeremy L Thompson 
10348b919e6bSJeremy L Thompson /**
10358b919e6bSJeremy L Thompson   @brief Determine if QFunctionAssemblyData needs update
10368b919e6bSJeremy L Thompson 
10378b919e6bSJeremy L Thompson   @param[in]  data             CeedQFunctionAssemblyData to mark as stale
10388b919e6bSJeremy L Thompson   @param[out] is_update_needed Boolean flag indicating if re-assembly is required
10398b919e6bSJeremy L Thompson 
10408b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
10418b919e6bSJeremy L Thompson 
10428b919e6bSJeremy L Thompson   @ref Backend
10438b919e6bSJeremy L Thompson **/
10442b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) {
1045beecbf24SJeremy L Thompson   *is_update_needed = !data->reuse_data || data->needs_data_update;
10468b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
10478b919e6bSJeremy L Thompson }
10488b919e6bSJeremy L Thompson 
10498b919e6bSJeremy L Thompson /**
1050ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedQFunctionAssemblyData.
10514385fb7fSSebastian Grimberg 
1052ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedCeedQFunctionAssemblyDataDestroy()`.
1053512bb800SJeremy L Thompson 
1054512bb800SJeremy L Thompson   Note: If the value of `data_copy` passed to this function is non-NULL, then it is assumed that `*data_copy` is a pointer to a
1055512bb800SJeremy L Thompson         CeedQFunctionAssemblyData. This CeedQFunctionAssemblyData will be destroyed if `data_copy` is the only reference to this
1056512bb800SJeremy L Thompson         CeedQFunctionAssemblyData.
1057480fae85SJeremy L Thompson 
1058ea61e9acSJeremy L Thompson   @param[in]     data      CeedQFunctionAssemblyData to copy reference to
1059ea61e9acSJeremy L Thompson   @param[in,out] data_copy Variable to store copied reference
1060480fae85SJeremy L Thompson 
1061480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1062480fae85SJeremy L Thompson 
1063480fae85SJeremy L Thompson   @ref Backend
1064480fae85SJeremy L Thompson **/
10652b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) {
10662b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReference(data));
10672b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy));
1068480fae85SJeremy L Thompson   *data_copy = data;
1069480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1070480fae85SJeremy L Thompson }
1071480fae85SJeremy L Thompson 
1072480fae85SJeremy L Thompson /**
1073480fae85SJeremy L Thompson   @brief Get setup status for internal objects for CeedQFunctionAssemblyData
1074480fae85SJeremy L Thompson 
1075ea61e9acSJeremy L Thompson   @param[in]  data     CeedQFunctionAssemblyData to retrieve status
1076480fae85SJeremy L Thompson   @param[out] is_setup Boolean flag for setup status
1077480fae85SJeremy L Thompson 
1078480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1079480fae85SJeremy L Thompson 
1080480fae85SJeremy L Thompson   @ref Backend
1081480fae85SJeremy L Thompson **/
10822b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) {
1083480fae85SJeremy L Thompson   *is_setup = data->is_setup;
1084480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1085480fae85SJeremy L Thompson }
1086480fae85SJeremy L Thompson 
1087480fae85SJeremy L Thompson /**
1088480fae85SJeremy L Thompson   @brief Set internal objects for CeedQFunctionAssemblyData
1089480fae85SJeremy L Thompson 
1090ea61e9acSJeremy L Thompson   @param[in,out] data CeedQFunctionAssemblyData to set objects
1091480fae85SJeremy L Thompson   @param[in]     vec  CeedVector to store assembled CeedQFunction at quadrature points
1092480fae85SJeremy L Thompson   @param[in]     rstr CeedElemRestriction for CeedVector containing assembled CeedQFunction
1093480fae85SJeremy L Thompson 
1094480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1095480fae85SJeremy L Thompson 
1096480fae85SJeremy L Thompson   @ref Backend
1097480fae85SJeremy L Thompson **/
10982b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) {
10992b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(vec, &data->vec));
11002b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr));
1101480fae85SJeremy L Thompson 
1102480fae85SJeremy L Thompson   data->is_setup = true;
1103480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1104480fae85SJeremy L Thompson }
1105480fae85SJeremy L Thompson 
11062b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) {
11076574a04fSJeremy L Thompson   CeedCheck(data->is_setup, data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first.");
1108480fae85SJeremy L Thompson 
11092b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(data->vec, vec));
11102b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr));
1111480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1112480fae85SJeremy L Thompson }
1113480fae85SJeremy L Thompson 
1114480fae85SJeremy L Thompson /**
1115480fae85SJeremy L Thompson   @brief Destroy CeedQFunctionAssemblyData
1116480fae85SJeremy L Thompson 
1117ea61e9acSJeremy L Thompson   @param[in,out] data  CeedQFunctionAssemblyData to destroy
1118480fae85SJeremy L Thompson 
1119480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1120480fae85SJeremy L Thompson 
1121480fae85SJeremy L Thompson   @ref Backend
1122480fae85SJeremy L Thompson **/
1123480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) {
1124ad6481ceSJeremy L Thompson   if (!*data || --(*data)->ref_count > 0) {
1125ad6481ceSJeremy L Thompson     *data = NULL;
1126ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1127ad6481ceSJeremy L Thompson   }
11282b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
11292b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*data)->vec));
11302b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr));
1131480fae85SJeremy L Thompson 
11322b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1133480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1134480fae85SJeremy L Thompson }
1135480fae85SJeremy L Thompson 
1136ed9e99e6SJeremy L Thompson /**
1137ed9e99e6SJeremy L Thompson   @brief Get CeedOperatorAssemblyData
1138ed9e99e6SJeremy L Thompson 
1139ed9e99e6SJeremy L Thompson   @param[in]  op   CeedOperator to assemble
1140ed9e99e6SJeremy L Thompson   @param[out] data CeedQFunctionAssemblyData
1141ed9e99e6SJeremy L Thompson 
1142ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1143ed9e99e6SJeremy L Thompson 
1144ed9e99e6SJeremy L Thompson   @ref Backend
1145ed9e99e6SJeremy L Thompson **/
11462b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) {
1147ed9e99e6SJeremy L Thompson   if (!op->op_assembled) {
1148ed9e99e6SJeremy L Thompson     CeedOperatorAssemblyData data;
1149ed9e99e6SJeremy L Thompson 
11502b730f8bSJeremy L Thompson     CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data));
1151ed9e99e6SJeremy L Thompson     op->op_assembled = data;
1152ed9e99e6SJeremy L Thompson   }
1153ed9e99e6SJeremy L Thompson   *data = op->op_assembled;
1154ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1155ed9e99e6SJeremy L Thompson }
1156ed9e99e6SJeremy L Thompson 
1157ed9e99e6SJeremy L Thompson /**
1158ba746a46SJeremy L Thompson   @brief Create object holding CeedOperator assembly data.
1159ba746a46SJeremy L Thompson 
1160ba746a46SJeremy L Thompson   The CeedOperatorAssemblyData holds an array with references to every active CeedBasis used in the CeedOperator.
1161ba746a46SJeremy L Thompson   An array with references to the corresponding active CeedElemRestrictions is also stored.
1162ba746a46SJeremy L Thompson   For each active CeedBasis, the CeedOperatorAssemblyData holds an array of all input and output CeedEvalModes for this CeedBasis.
1163ba746a46SJeremy L Thompson   The CeedOperatorAssemblyData holds an array of offsets for indexing into the assembled CeedQFunction arrays to the row representing each
1164ba746a46SJeremy L Thompson CeedEvalMode.
1165ba746a46SJeremy L Thompson   The number of input columns across all active bases for the assembled CeedQFunction is also stored.
1166ba746a46SJeremy L Thompson   Lastly, the CeedOperatorAssembly data holds assembled matrices representing the full action of the CeedBasis for all CeedEvalModes.
1167ed9e99e6SJeremy L Thompson 
1168ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperatorAssemblyData will be created
1169ed9e99e6SJeremy L Thompson   @param[in]  op   CeedOperator to be assembled
1170ea61e9acSJeremy L Thompson   @param[out] data Address of the variable where the newly created CeedOperatorAssemblyData will be stored
1171ed9e99e6SJeremy L Thompson 
1172ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1173ed9e99e6SJeremy L Thompson 
1174ed9e99e6SJeremy L Thompson   @ref Backend
1175ed9e99e6SJeremy L Thompson **/
11762b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) {
11771c66c397SJeremy L Thompson   CeedInt             num_active_bases = 0, num_input_fields, *num_eval_modes_in = NULL, *num_eval_modes_out = NULL, offset = 0, num_output_fields;
11781c66c397SJeremy L Thompson   CeedSize          **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL;
11791c66c397SJeremy L Thompson   CeedEvalMode      **eval_modes_in = NULL, **eval_modes_out = NULL;
11801c66c397SJeremy L Thompson   CeedQFunctionField *qf_fields;
11811c66c397SJeremy L Thompson   CeedQFunction       qf;
11821c66c397SJeremy L Thompson   CeedOperatorField  *op_fields;
118301f0e615SJames Wright   bool                is_composite;
118401f0e615SJames Wright 
118501f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
118601f0e615SJames Wright   CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Can only create CeedOperator assembly data for non-composite operators.");
1187437c7c90SJeremy L Thompson 
1188437c7c90SJeremy L Thompson   // Allocate
11892b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1190ed9e99e6SJeremy L Thompson   (*data)->ceed = ceed;
11912b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1192ed9e99e6SJeremy L Thompson 
1193ed9e99e6SJeremy L Thompson   // Build OperatorAssembly data
11942b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
11952b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL));
11962b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1197ed9e99e6SJeremy L Thompson 
1198ed9e99e6SJeremy L Thompson   // Determine active input basis
1199ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1200ed9e99e6SJeremy L Thompson     CeedVector vec;
12011c66c397SJeremy L Thompson 
12022b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1203ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
12047c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
12051c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
12061c66c397SJeremy L Thompson       CeedBasis    basis_in = NULL;
12071c66c397SJeremy L Thompson 
12082b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
12092b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1210352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp));
1211352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1212437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < num_active_bases; i++) {
1213437c7c90SJeremy L Thompson         if ((*data)->active_bases[i] == basis_in) index = i;
1214437c7c90SJeremy L Thompson       }
1215437c7c90SJeremy L Thompson       if (index == -1) {
1216437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_in;
12171c66c397SJeremy L Thompson 
1218437c7c90SJeremy L Thompson         index = num_active_bases;
1219437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_bases));
1220437c7c90SJeremy L Thompson         (*data)->active_bases[num_active_bases] = NULL;
1221437c7c90SJeremy L Thompson         CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases[num_active_bases]));
1222437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_elem_rstrs));
1223437c7c90SJeremy L Thompson         (*data)->active_elem_rstrs[num_active_bases] = NULL;
1224437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in));
1225437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs[num_active_bases]));
1226437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_in));
1227437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_out));
1228437c7c90SJeremy L Thompson         num_eval_modes_in[index]  = 0;
1229437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1230437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_in));
1231437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_out));
1232437c7c90SJeremy L Thompson         eval_modes_in[index]  = NULL;
1233437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1234437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_in));
1235437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_out));
1236437c7c90SJeremy L Thompson         eval_mode_offsets_in[index]  = NULL;
1237437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1238437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_in));
1239437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_out));
1240437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index]  = NULL;
1241437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1242437c7c90SJeremy L Thompson         num_active_bases++;
1243437c7c90SJeremy L Thompson       }
1244352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1245352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1246352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_modes_in[index]));
1247352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_mode_offsets_in[index]));
1248352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1249437c7c90SJeremy L Thompson           eval_modes_in[index][num_eval_modes_in[index] + d]        = eval_mode;
1250437c7c90SJeremy L Thompson           eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset;
1251352a5e7cSSebastian Grimberg           offset += num_comp;
1252ed9e99e6SJeremy L Thompson         }
1253352a5e7cSSebastian Grimberg         num_eval_modes_in[index] += q_comp;
1254ed9e99e6SJeremy L Thompson       }
1255ed9e99e6SJeremy L Thompson     }
1256ed9e99e6SJeremy L Thompson   }
1257ed9e99e6SJeremy L Thompson 
1258ed9e99e6SJeremy L Thompson   // Determine active output basis
12592b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields));
12602b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1261437c7c90SJeremy L Thompson   offset = 0;
1262ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1263ed9e99e6SJeremy L Thompson     CeedVector vec;
12641c66c397SJeremy L Thompson 
12652b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1266ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
12677c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
12681c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
12691c66c397SJeremy L Thompson       CeedBasis    basis_out = NULL;
12701c66c397SJeremy L Thompson 
1271437c7c90SJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
12722b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1273352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_out, &num_comp));
1274352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1275437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < num_active_bases; i++) {
1276437c7c90SJeremy L Thompson         if ((*data)->active_bases[i] == basis_out) index = i;
1277437c7c90SJeremy L Thompson       }
1278437c7c90SJeremy L Thompson       if (index == -1) {
1279437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_out;
12801c66c397SJeremy L Thompson 
1281437c7c90SJeremy L Thompson         index = num_active_bases;
1282437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_bases));
1283437c7c90SJeremy L Thompson         (*data)->active_bases[num_active_bases] = NULL;
1284437c7c90SJeremy L Thompson         CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases[num_active_bases]));
1285437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->active_elem_rstrs));
1286437c7c90SJeremy L Thompson         (*data)->active_elem_rstrs[num_active_bases] = NULL;
1287437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out));
1288437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs[num_active_bases]));
1289437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_in));
1290437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &num_eval_modes_out));
1291437c7c90SJeremy L Thompson         num_eval_modes_in[index]  = 0;
1292437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1293437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_in));
1294437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_modes_out));
1295437c7c90SJeremy L Thompson         eval_modes_in[index]  = NULL;
1296437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1297437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_in));
1298437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &eval_mode_offsets_out));
1299437c7c90SJeremy L Thompson         eval_mode_offsets_in[index]  = NULL;
1300437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1301437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_in));
1302437c7c90SJeremy L Thompson         CeedCall(CeedRealloc(num_active_bases + 1, &(*data)->assembled_bases_out));
1303437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index]  = NULL;
1304437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1305437c7c90SJeremy L Thompson         num_active_bases++;
1306437c7c90SJeremy L Thompson       }
1307352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1308352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1309352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_modes_out[index]));
1310352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_mode_offsets_out[index]));
1311352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1312437c7c90SJeremy L Thompson           eval_modes_out[index][num_eval_modes_out[index] + d]        = eval_mode;
1313437c7c90SJeremy L Thompson           eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset;
1314352a5e7cSSebastian Grimberg           offset += num_comp;
1315ed9e99e6SJeremy L Thompson         }
1316352a5e7cSSebastian Grimberg         num_eval_modes_out[index] += q_comp;
1317ed9e99e6SJeremy L Thompson       }
1318ed9e99e6SJeremy L Thompson     }
1319ed9e99e6SJeremy L Thompson   }
132027789c4aSJed Brown   (*data)->num_eval_modes_in     = num_eval_modes_in;
132127789c4aSJed Brown   (*data)->eval_modes_in         = eval_modes_in;
132227789c4aSJed Brown   (*data)->eval_mode_offsets_in  = eval_mode_offsets_in;
1323437c7c90SJeremy L Thompson   (*data)->num_output_components = offset;
1324437c7c90SJeremy L Thompson   (*data)->num_eval_modes_out    = num_eval_modes_out;
1325437c7c90SJeremy L Thompson   (*data)->eval_modes_out        = eval_modes_out;
1326437c7c90SJeremy L Thompson   (*data)->eval_mode_offsets_out = eval_mode_offsets_out;
1327437c7c90SJeremy L Thompson   (*data)->num_active_bases      = num_active_bases;
1328ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1329ed9e99e6SJeremy L Thompson }
1330ed9e99e6SJeremy L Thompson 
1331ed9e99e6SJeremy L Thompson /**
1332ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedEvalModes for assembly.
1333ba746a46SJeremy L Thompson 
1334ba746a46SJeremy L Thompson   Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1335ed9e99e6SJeremy L Thompson 
1336ed9e99e6SJeremy L Thompson   @param[in]  data                  CeedOperatorAssemblyData
1337ba746a46SJeremy L Thompson   @param[out] num_active_bases      Total number of active bases
1338c5d0f995SJed Brown   @param[out] num_eval_modes_in     Pointer to hold array of numbers of input CeedEvalModes, or NULL.
1339ba746a46SJeremy L Thompson                                       `eval_modes_in[0]` holds an array of eval modes for the first active basis.
1340c5d0f995SJed Brown   @param[out] eval_modes_in         Pointer to hold arrays of input CeedEvalModes, or NULL.
1341ba746a46SJeremy L Thompson   @param[out] eval_mode_offsets_in  Pointer to hold arrays of input offsets at each quadrature point.
1342c5d0f995SJed Brown   @param[out] num_eval_modes_out    Pointer to hold array of numbers of output CeedEvalModes, or NULL
1343c5d0f995SJed Brown   @param[out] eval_modes_out        Pointer to hold arrays of output CeedEvalModes, or NULL.
1344437c7c90SJeremy L Thompson   @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point
1345ba746a46SJeremy L Thompson   @param[out] num_output_components The number of columns in the assembled CeedQFunction matrix for each quadrature point,
1346ba746a46SJeremy L Thompson                                       including contributions of all active bases
1347ed9e99e6SJeremy L Thompson 
1348ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1349ed9e99e6SJeremy L Thompson 
1350c5d0f995SJed Brown 
1351ed9e99e6SJeremy L Thompson   @ref Backend
1352ed9e99e6SJeremy L Thompson **/
1353437c7c90SJeremy L Thompson int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases, CeedInt **num_eval_modes_in,
1354437c7c90SJeremy L Thompson                                          const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt **num_eval_modes_out,
1355437c7c90SJeremy L Thompson                                          const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out, CeedSize *num_output_components) {
1356437c7c90SJeremy L Thompson   if (num_active_bases) *num_active_bases = data->num_active_bases;
1357437c7c90SJeremy L Thompson   if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in;
1358437c7c90SJeremy L Thompson   if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in;
1359437c7c90SJeremy L Thompson   if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in;
1360437c7c90SJeremy L Thompson   if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out;
1361437c7c90SJeremy L Thompson   if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out;
1362437c7c90SJeremy L Thompson   if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out;
1363437c7c90SJeremy L Thompson   if (num_output_components) *num_output_components = data->num_output_components;
1364ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1365ed9e99e6SJeremy L Thompson }
1366ed9e99e6SJeremy L Thompson 
1367ed9e99e6SJeremy L Thompson /**
1368ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedBasis data for assembly.
1369ba746a46SJeremy L Thompson 
1370ba746a46SJeremy L Thompson   Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1371ed9e99e6SJeremy L Thompson 
1372ed9e99e6SJeremy L Thompson   @param[in]  data                CeedOperatorAssemblyData
1373437c7c90SJeremy L Thompson   @param[out] num_active_bases    Number of active bases, or NULL
1374437c7c90SJeremy L Thompson   @param[out] active_bases        Pointer to hold active CeedBasis, or NULL
1375437c7c90SJeremy L Thompson   @param[out] assembled_bases_in  Pointer to hold assembled active input B, or NULL
1376437c7c90SJeremy L Thompson   @param[out] assembled_bases_out Pointer to hold assembled active output B, or NULL
1377ed9e99e6SJeremy L Thompson 
1378ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1379ed9e99e6SJeremy L Thompson 
1380ed9e99e6SJeremy L Thompson   @ref Backend
1381ed9e99e6SJeremy L Thompson **/
1382437c7c90SJeremy L Thompson int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases, CeedBasis **active_bases,
1383437c7c90SJeremy L Thompson                                      const CeedScalar ***assembled_bases_in, const CeedScalar ***assembled_bases_out) {
1384ed9e99e6SJeremy L Thompson   // Assemble B_in, B_out if needed
1385437c7c90SJeremy L Thompson   if (assembled_bases_in && !data->assembled_bases_in[0]) {
1386437c7c90SJeremy L Thompson     CeedInt num_qpts;
1387437c7c90SJeremy L Thompson 
1388437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases[0], &num_qpts));
1389437c7c90SJeremy L Thompson     for (CeedInt b = 0; b < data->num_active_bases; b++) {
13901c66c397SJeremy L Thompson       bool        has_eval_none = false;
1391352a5e7cSSebastian Grimberg       CeedInt     num_nodes;
1392437c7c90SJeremy L Thompson       CeedScalar *B_in = NULL, *identity = NULL;
1393ed9e99e6SJeremy L Thompson 
1394352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumNodes(data->active_bases[b], &num_nodes));
1395352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_in[b], &B_in));
1396ed9e99e6SJeremy L Thompson 
1397437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) {
1398437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE);
1399ed9e99e6SJeremy L Thompson       }
1400ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1401352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1402352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1403352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1404ed9e99e6SJeremy L Thompson         }
1405ed9e99e6SJeremy L Thompson       }
1406ed9e99e6SJeremy L Thompson 
1407ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1408352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1409352a5e7cSSebastian Grimberg           CeedInt      d_in              = 0, q_comp_in;
1410352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE;
14111c66c397SJeremy L Thompson 
1412437c7c90SJeremy L Thompson           for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) {
1413437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_in[b] * q;
1414437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
14151c66c397SJeremy L Thompson 
1416352a5e7cSSebastian Grimberg             CeedOperatorGetBasisPointer(data->active_bases[b], data->eval_modes_in[b][e_in], identity, &B);
1417352a5e7cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases[b], data->eval_modes_in[b][e_in], &q_comp_in));
1418352a5e7cSSebastian Grimberg             if (q_comp_in > 1) {
1419352a5e7cSSebastian Grimberg               if (e_in == 0 || data->eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0;
1420352a5e7cSSebastian Grimberg               else B = &B[(++d_in) * num_qpts * num_nodes];
1421352a5e7cSSebastian Grimberg             }
1422352a5e7cSSebastian Grimberg             eval_mode_in_prev                 = data->eval_modes_in[b][e_in];
1423352a5e7cSSebastian Grimberg             B_in[(qq + e_in) * num_nodes + n] = B[q * num_nodes + n];
1424ed9e99e6SJeremy L Thompson           }
1425ed9e99e6SJeremy L Thompson         }
1426ed9e99e6SJeremy L Thompson       }
14277c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1428437c7c90SJeremy L Thompson       data->assembled_bases_in[b] = B_in;
1429437c7c90SJeremy L Thompson     }
1430ed9e99e6SJeremy L Thompson   }
1431ed9e99e6SJeremy L Thompson 
1432437c7c90SJeremy L Thompson   if (assembled_bases_out && !data->assembled_bases_out[0]) {
1433437c7c90SJeremy L Thompson     CeedInt num_qpts;
1434437c7c90SJeremy L Thompson 
1435437c7c90SJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases[0], &num_qpts));
1436437c7c90SJeremy L Thompson     for (CeedInt b = 0; b < data->num_active_bases; b++) {
1437ed9e99e6SJeremy L Thompson       bool        has_eval_none = false;
14381c66c397SJeremy L Thompson       CeedInt     num_nodes;
1439437c7c90SJeremy L Thompson       CeedScalar *B_out = NULL, *identity = NULL;
1440ed9e99e6SJeremy L Thompson 
1441352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumNodes(data->active_bases[b], &num_nodes));
1442352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_out[b], &B_out));
1443ed9e99e6SJeremy L Thompson 
1444437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) {
1445437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE);
1446ed9e99e6SJeremy L Thompson       }
1447ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1448352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1449352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1450352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1451ed9e99e6SJeremy L Thompson         }
1452ed9e99e6SJeremy L Thompson       }
1453ed9e99e6SJeremy L Thompson 
1454ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1455352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1456352a5e7cSSebastian Grimberg           CeedInt      d_out              = 0, q_comp_out;
1457352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
14581c66c397SJeremy L Thompson 
1459437c7c90SJeremy L Thompson           for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) {
1460437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_out[b] * q;
1461437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
14621c66c397SJeremy L Thompson 
1463352a5e7cSSebastian Grimberg             CeedOperatorGetBasisPointer(data->active_bases[b], data->eval_modes_out[b][e_out], identity, &B);
1464352a5e7cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases[b], data->eval_modes_out[b][e_out], &q_comp_out));
1465352a5e7cSSebastian Grimberg             if (q_comp_out > 1) {
1466352a5e7cSSebastian Grimberg               if (e_out == 0 || data->eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0;
1467352a5e7cSSebastian Grimberg               else B = &B[(++d_out) * num_qpts * num_nodes];
1468352a5e7cSSebastian Grimberg             }
1469352a5e7cSSebastian Grimberg             eval_mode_out_prev                  = data->eval_modes_out[b][e_out];
1470352a5e7cSSebastian Grimberg             B_out[(qq + e_out) * num_nodes + n] = B[q * num_nodes + n];
1471ed9e99e6SJeremy L Thompson           }
1472ed9e99e6SJeremy L Thompson         }
1473ed9e99e6SJeremy L Thompson       }
14747c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1475437c7c90SJeremy L Thompson       data->assembled_bases_out[b] = B_out;
1476437c7c90SJeremy L Thompson     }
1477ed9e99e6SJeremy L Thompson   }
1478ed9e99e6SJeremy L Thompson 
1479437c7c90SJeremy L Thompson   // Pass out assembled data
1480437c7c90SJeremy L Thompson   if (active_bases) *active_bases = data->active_bases;
1481437c7c90SJeremy L Thompson   if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in;
1482437c7c90SJeremy L Thompson   if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out;
1483437c7c90SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1484437c7c90SJeremy L Thompson }
1485437c7c90SJeremy L Thompson 
1486437c7c90SJeremy L Thompson /**
1487ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedBasis data for assembly.
1488ba746a46SJeremy L Thompson 
1489ba746a46SJeremy L Thompson   Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1490437c7c90SJeremy L Thompson 
1491437c7c90SJeremy L Thompson   @param[in]  data                  CeedOperatorAssemblyData
1492437c7c90SJeremy L Thompson   @param[out] num_active_elem_rstrs Number of active element restrictions, or NULL
1493437c7c90SJeremy L Thompson   @param[out] active_elem_rstrs     Pointer to hold active CeedElemRestrictions, or NULL
1494437c7c90SJeremy L Thompson 
1495437c7c90SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1496437c7c90SJeremy L Thompson 
1497437c7c90SJeremy L Thompson   @ref Backend
1498437c7c90SJeremy L Thompson **/
1499437c7c90SJeremy L Thompson int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs,
1500437c7c90SJeremy L Thompson                                                 CeedElemRestriction **active_elem_rstrs) {
1501437c7c90SJeremy L Thompson   if (num_active_elem_rstrs) *num_active_elem_rstrs = data->num_active_bases;
1502437c7c90SJeremy L Thompson   if (active_elem_rstrs) *active_elem_rstrs = data->active_elem_rstrs;
1503ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1504ed9e99e6SJeremy L Thompson }
1505ed9e99e6SJeremy L Thompson 
1506ed9e99e6SJeremy L Thompson /**
1507ed9e99e6SJeremy L Thompson   @brief Destroy CeedOperatorAssemblyData
1508ed9e99e6SJeremy L Thompson 
1509ea61e9acSJeremy L Thompson   @param[in,out] data CeedOperatorAssemblyData to destroy
1510ed9e99e6SJeremy L Thompson 
1511ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1512ed9e99e6SJeremy L Thompson 
1513ed9e99e6SJeremy L Thompson   @ref Backend
1514ed9e99e6SJeremy L Thompson **/
1515ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) {
1516ad6481ceSJeremy L Thompson   if (!*data) {
1517ad6481ceSJeremy L Thompson     *data = NULL;
1518ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1519ad6481ceSJeremy L Thompson   }
15202b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
1521437c7c90SJeremy L Thompson   for (CeedInt b = 0; b < (*data)->num_active_bases; b++) {
1522437c7c90SJeremy L Thompson     CeedCall(CeedBasisDestroy(&(*data)->active_bases[b]));
1523437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs[b]));
1524437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_in[b]));
1525437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_out[b]));
1526437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b]));
1527437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b]));
1528437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_in[b]));
1529437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_out[b]));
1530437c7c90SJeremy L Thompson   }
1531437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->active_bases));
1532437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->active_elem_rstrs));
1533437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_in));
1534437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_out));
1535437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_in));
1536437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_out));
1537437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_in));
1538437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_out));
1539437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_in));
1540437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_out));
1541ed9e99e6SJeremy L Thompson 
15422b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1543ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1544ed9e99e6SJeremy L Thompson }
1545ed9e99e6SJeremy L Thompson 
1546480fae85SJeremy L Thompson /// @}
1547480fae85SJeremy L Thompson 
1548480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1549eaf62fffSJeremy L Thompson /// CeedOperator Public API
1550eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1551eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser
1552eaf62fffSJeremy L Thompson /// @{
1553eaf62fffSJeremy L Thompson 
1554eaf62fffSJeremy L Thompson /**
1555eaf62fffSJeremy L Thompson   @brief Assemble a linear CeedQFunction associated with a CeedOperator
1556eaf62fffSJeremy L Thompson 
1557ea61e9acSJeremy L Thompson   This returns a CeedVector containing a matrix at each quadrature point providing the action of the CeedQFunction associated with the CeedOperator.
1558859c15bbSJames Wright   The vector `assembled` is of shape `[num_elements, num_input_fields, num_output_fields, num_quad_points]` and contains column-major matrices
1559859c15bbSJames Wright representing the action of the CeedQFunction for a corresponding quadrature point on an element.
1560859c15bbSJames Wright 
15619fd66db6SSebastian Grimberg   Inputs and outputs are in the order provided by the user when adding CeedOperator fields.
15629fd66db6SSebastian Grimberg   For example, a CeedQFunction with inputs 'u' and 'gradu' and outputs 'gradv' and 'v', provided in that order, would result in an assembled QFunction
15639fd66db6SSebastian Grimberg that consists of (1 + dim) x (dim + 1) matrices at each quadrature point acting on the input [u, du_0, du_1] and producing the output [dv_0, dv_1, v].
1564eaf62fffSJeremy L Thompson 
1565ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1566f04ea552SJeremy L Thompson 
1567ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1568ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points
1569ea61e9acSJeremy L Thompson   @param[out] rstr      CeedElemRestriction for CeedVector containing assembled CeedQFunction
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 CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
15772b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1578eaf62fffSJeremy L Thompson 
1579eaf62fffSJeremy L Thompson   if (op->LinearAssembleQFunction) {
1580d04bbc78SJeremy L Thompson     // Backend version
15812b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request));
1582eaf62fffSJeremy L Thompson   } else {
1583d04bbc78SJeremy L Thompson     // Operator fallback
1584d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1585d04bbc78SJeremy L Thompson 
15862b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
15876574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request));
15886574a04fSJeremy L Thompson     else return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction");
158970a7ffb3SJeremy L Thompson   }
1590eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1591eaf62fffSJeremy L Thompson }
159270a7ffb3SJeremy L Thompson 
159370a7ffb3SJeremy L Thompson /**
1594ea61e9acSJeremy L Thompson   @brief Assemble CeedQFunction and store result internally.
15954385fb7fSSebastian Grimberg 
1596ea61e9acSJeremy L Thompson   Return copied references of stored data to the caller.
1597ea61e9acSJeremy L Thompson   Caller is responsible for ownership and destruction of the copied references.
1598ea61e9acSJeremy L Thompson   See also @ref CeedOperatorLinearAssembleQFunction
159970a7ffb3SJeremy L Thompson 
1600c5f45aeaSJeremy L Thompson   Note: If the value of `assembled` or `rstr` passed to this function are non-NULL, then it is assumed that they hold valid pointers.
1601c5f45aeaSJeremy L Thompson         These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object.
1602c5f45aeaSJeremy L Thompson 
1603ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1604ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points
1605ea61e9acSJeremy L Thompson   @param[out] rstr      CeedElemRestriction for CeedVector containing assembledCeedQFunction
1606ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
160770a7ffb3SJeremy L Thompson 
160870a7ffb3SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
160970a7ffb3SJeremy L Thompson 
161070a7ffb3SJeremy L Thompson   @ref User
161170a7ffb3SJeremy L Thompson **/
16122b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
1613b05f7e9fSJeremy L Thompson   int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *) = NULL;
1614b05f7e9fSJeremy L Thompson   CeedOperator op_assemble                                                                           = NULL;
1615bb229da9SJeremy L Thompson   CeedOperator op_fallback_parent                                                                    = NULL;
1616b05f7e9fSJeremy L Thompson 
16172b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
161870a7ffb3SJeremy L Thompson 
1619b05f7e9fSJeremy L Thompson   // Determine if fallback parent or operator has implementation
1620bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParent(op, &op_fallback_parent));
1621bb229da9SJeremy L Thompson   if (op_fallback_parent && op_fallback_parent->LinearAssembleQFunctionUpdate) {
1622b05f7e9fSJeremy L Thompson     // -- Backend version for op fallback parent is faster, if it exists
1623bb229da9SJeremy L Thompson     LinearAssembleQFunctionUpdate = op_fallback_parent->LinearAssembleQFunctionUpdate;
1624bb229da9SJeremy L Thompson     op_assemble                   = op_fallback_parent;
1625b05f7e9fSJeremy L Thompson   } else if (op->LinearAssembleQFunctionUpdate) {
1626b05f7e9fSJeremy L Thompson     // -- Backend version for op
1627b05f7e9fSJeremy L Thompson     LinearAssembleQFunctionUpdate = op->LinearAssembleQFunctionUpdate;
1628b05f7e9fSJeremy L Thompson     op_assemble                   = op;
1629b05f7e9fSJeremy L Thompson   }
1630b05f7e9fSJeremy L Thompson 
1631b05f7e9fSJeremy L Thompson   // Assemble QFunction
1632b05f7e9fSJeremy L Thompson   if (LinearAssembleQFunctionUpdate) {
1633b05f7e9fSJeremy L Thompson     // Backend or fallback parent version
1634480fae85SJeremy L Thompson     bool                qf_assembled_is_setup;
16352efa2d85SJeremy L Thompson     CeedVector          assembled_vec  = NULL;
16362efa2d85SJeremy L Thompson     CeedElemRestriction assembled_rstr = NULL;
1637480fae85SJeremy L Thompson 
16382b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataIsSetup(op->qf_assembled, &qf_assembled_is_setup));
1639480fae85SJeremy L Thompson     if (qf_assembled_is_setup) {
1640d04bbc78SJeremy L Thompson       bool update_needed;
1641d04bbc78SJeremy L Thompson 
16422b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataGetObjects(op->qf_assembled, &assembled_vec, &assembled_rstr));
16432b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(op->qf_assembled, &update_needed));
1644b05f7e9fSJeremy L Thompson       if (update_needed) CeedCall(LinearAssembleQFunctionUpdate(op_assemble, assembled_vec, assembled_rstr, request));
164570a7ffb3SJeremy L Thompson     } else {
1646b05f7e9fSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunction(op_assemble, &assembled_vec, &assembled_rstr, request));
16472b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataSetObjects(op->qf_assembled, assembled_vec, assembled_rstr));
164870a7ffb3SJeremy L Thompson     }
16492b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, false));
16502efa2d85SJeremy L Thompson 
1651d04bbc78SJeremy L Thompson     // Copy reference from internally held copy
16522b730f8bSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled));
16532b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr));
1654c5f45aeaSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled_vec));
16552b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&assembled_rstr));
165670a7ffb3SJeremy L Thompson   } else {
1657d04bbc78SJeremy L Thompson     // Operator fallback
1658d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1659d04bbc78SJeremy L Thompson 
16602b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
16616574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request));
16626574a04fSJeremy L Thompson     else return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate");
166370a7ffb3SJeremy L Thompson   }
166470a7ffb3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1665eaf62fffSJeremy L Thompson }
1666eaf62fffSJeremy L Thompson 
1667eaf62fffSJeremy L Thompson /**
1668eaf62fffSJeremy L Thompson   @brief Assemble the diagonal of a square linear CeedOperator
1669eaf62fffSJeremy L Thompson 
1670eaf62fffSJeremy L Thompson   This overwrites a CeedVector with the diagonal of a linear CeedOperator.
1671eaf62fffSJeremy L Thompson 
1672ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1673eaf62fffSJeremy L Thompson 
1674ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1675f04ea552SJeremy L Thompson 
1676ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1677eaf62fffSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator diagonal
1678ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1679eaf62fffSJeremy L Thompson 
1680eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1681eaf62fffSJeremy L Thompson 
1682eaf62fffSJeremy L Thompson   @ref User
1683eaf62fffSJeremy L Thompson **/
16842b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1685f3d47e36SJeremy L Thompson   bool     is_composite;
16861c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
16871c66c397SJeremy L Thompson 
16882b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1689f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1690eaf62fffSJeremy L Thompson 
16912b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
16926574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1693c9366a6bSJeremy L Thompson 
1694f3d47e36SJeremy L Thompson   // Early exit for empty operator
1695f3d47e36SJeremy L Thompson   if (!is_composite) {
1696f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1697f3d47e36SJeremy L Thompson 
1698f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1699f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1700f3d47e36SJeremy L Thompson   }
1701f3d47e36SJeremy L Thompson 
1702eaf62fffSJeremy L Thompson   if (op->LinearAssembleDiagonal) {
1703d04bbc78SJeremy L Thompson     // Backend version
17042b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleDiagonal(op, assembled, request));
1705eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1706eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddDiagonal) {
1707d04bbc78SJeremy L Thompson     // Backend version with zeroing first
17082b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
17092b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1710eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1711eaf62fffSJeremy L Thompson   } else {
1712d04bbc78SJeremy L Thompson     // Operator fallback
1713d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1714d04bbc78SJeremy L Thompson 
17152b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1716d04bbc78SJeremy L Thompson     if (op_fallback) {
17172b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request));
1718eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1719eaf62fffSJeremy L Thompson     }
1720eaf62fffSJeremy L Thompson   }
1721eaf62fffSJeremy L Thompson   // Default interface implementation
17222b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
17232b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request));
1724eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1725eaf62fffSJeremy L Thompson }
1726eaf62fffSJeremy L Thompson 
1727eaf62fffSJeremy L Thompson /**
1728eaf62fffSJeremy L Thompson   @brief Assemble the diagonal of a square linear CeedOperator
1729eaf62fffSJeremy L Thompson 
1730eaf62fffSJeremy L Thompson   This sums into a CeedVector the diagonal of a linear CeedOperator.
1731eaf62fffSJeremy L Thompson 
1732ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1733eaf62fffSJeremy L Thompson 
1734ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1735f04ea552SJeremy L Thompson 
1736ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1737eaf62fffSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator diagonal
1738ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1739eaf62fffSJeremy L Thompson 
1740eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1741eaf62fffSJeremy L Thompson 
1742eaf62fffSJeremy L Thompson   @ref User
1743eaf62fffSJeremy L Thompson **/
17442b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1745f3d47e36SJeremy L Thompson   bool     is_composite;
17461c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
17471c66c397SJeremy L Thompson 
17482b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1749f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1750eaf62fffSJeremy L Thompson 
17512b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
17526574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1753c9366a6bSJeremy L Thompson 
1754f3d47e36SJeremy L Thompson   // Early exit for empty operator
1755f3d47e36SJeremy L Thompson   if (!is_composite) {
1756f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1757f3d47e36SJeremy L Thompson 
1758f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1759f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1760f3d47e36SJeremy L Thompson   }
1761f3d47e36SJeremy L Thompson 
1762eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddDiagonal) {
1763d04bbc78SJeremy L Thompson     // Backend version
17642b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1765eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1766eaf62fffSJeremy L Thompson   } else {
1767d04bbc78SJeremy L Thompson     // Operator fallback
1768d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1769d04bbc78SJeremy L Thompson 
17702b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1771d04bbc78SJeremy L Thompson     if (op_fallback) {
17722b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
1773eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1774eaf62fffSJeremy L Thompson     }
1775eaf62fffSJeremy L Thompson   }
1776eaf62fffSJeremy L Thompson   // Default interface implementation
1777eaf62fffSJeremy L Thompson   if (is_composite) {
17782b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
1779eaf62fffSJeremy L Thompson   } else {
17802b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, false, assembled));
1781eaf62fffSJeremy L Thompson   }
1782d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1783eaf62fffSJeremy L Thompson }
1784eaf62fffSJeremy L Thompson 
1785eaf62fffSJeremy L Thompson /**
178601f0e615SJames Wright    @brief Fully assemble the point-block diagonal pattern of a linear operator.
178701f0e615SJames Wright 
178801f0e615SJames Wright    Expected to be used in conjunction with CeedOperatorLinearAssemblePointBlockDiagonal().
178901f0e615SJames Wright 
179001f0e615SJames Wright    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
179101f0e615SJames Wright matrix in entry (i, j).
179201f0e615SJames Wright   Note that the (i, j) pairs are unique.
179301f0e615SJames Wright   This function returns the number of entries and their (i, j) locations, while CeedOperatorLinearAssemblePointBlockDiagonal() provides the values in
179401f0e615SJames Wright the same ordering.
179501f0e615SJames Wright 
179601f0e615SJames Wright    This will generally be slow unless your operator is low-order.
179701f0e615SJames Wright 
179801f0e615SJames Wright    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
179901f0e615SJames Wright 
180001f0e615SJames Wright    @param[in]  op          CeedOperator to assemble
180101f0e615SJames Wright    @param[out] num_entries Number of entries in coordinate nonzero pattern
180201f0e615SJames Wright    @param[out] rows        Row number for each entry
180301f0e615SJames Wright    @param[out] cols        Column number for each entry
180401f0e615SJames Wright 
180501f0e615SJames Wright    @ref User
180601f0e615SJames Wright **/
180701f0e615SJames Wright int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
180801f0e615SJames Wright   Ceed          ceed;
180901f0e615SJames Wright   bool          is_composite;
181001f0e615SJames Wright   CeedInt       num_active_components, num_sub_operators;
181101f0e615SJames Wright   CeedOperator *sub_operators;
181201f0e615SJames Wright 
181301f0e615SJames Wright   CeedCall(CeedOperatorGetCeed(op, &ceed));
181401f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
181501f0e615SJames Wright 
181601f0e615SJames Wright   CeedSize input_size = 0, output_size = 0;
181701f0e615SJames Wright   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
181801f0e615SJames Wright   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
181901f0e615SJames Wright 
182001f0e615SJames Wright   if (is_composite) {
182101f0e615SJames Wright     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_operators));
182201f0e615SJames Wright     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
182301f0e615SJames Wright   } else {
182401f0e615SJames Wright     sub_operators     = &op;
182501f0e615SJames Wright     num_sub_operators = 1;
182601f0e615SJames Wright   }
182701f0e615SJames Wright 
182801f0e615SJames Wright   {  // Verify operator can be assembled correctly
182901f0e615SJames Wright     CeedInt                  num_active_elem_rstrs, comp_stride;
183001f0e615SJames Wright     CeedOperatorAssemblyData data;
183101f0e615SJames Wright     CeedElemRestriction     *active_elem_rstrs;
183201f0e615SJames Wright 
183301f0e615SJames Wright     // Get initial values to check against
183401f0e615SJames Wright     CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[0], &data));
183501f0e615SJames Wright     CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs));
183601f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[0], &comp_stride));
183701f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[0], &num_active_components));
183801f0e615SJames Wright 
183901f0e615SJames Wright     for (CeedInt k = 0; k < num_sub_operators; k++) {
184001f0e615SJames Wright       CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[k], &data));
184101f0e615SJames Wright 
184201f0e615SJames Wright       // Verify that all active element restrictions have same component stride and number of components
184301f0e615SJames Wright       CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs));
184401f0e615SJames Wright       CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[0], &comp_stride));
184501f0e615SJames Wright       for (CeedInt i = 0; i < num_active_elem_rstrs; i++) {
184601f0e615SJames Wright         CeedInt comp_stride_sub;
184701f0e615SJames Wright         CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[i], &comp_stride_sub));
184801f0e615SJames Wright         CeedCheck(comp_stride == comp_stride_sub, ceed, CEED_ERROR_DIMENSION,
184901f0e615SJames Wright                   "Active element restrictions must have the same component stride: %d vs %d", comp_stride, comp_stride_sub);
185001f0e615SJames Wright 
185101f0e615SJames Wright         CeedInt num_active_components_sub;
185201f0e615SJames Wright         CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[i], &num_active_components_sub));
185301f0e615SJames Wright         CeedCheck(num_active_components == num_active_components_sub, ceed, CEED_ERROR_INCOMPATIBLE,
185401f0e615SJames Wright                   "All suboperators must have the same number of output components");
185501f0e615SJames Wright       }
185601f0e615SJames Wright     }
185701f0e615SJames Wright   }
185801f0e615SJames Wright 
185901f0e615SJames Wright   *num_entries = input_size * num_active_components;
186001f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, rows));
186101f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, cols));
186201f0e615SJames Wright 
186301f0e615SJames Wright   for (CeedInt o = 0; o < num_sub_operators; o++) {
186401f0e615SJames Wright     CeedElemRestriction active_elem_rstr, pb_active_elem_rstr;
186501f0e615SJames Wright     CeedInt             comp_stride, num_elem, elem_size;
186601f0e615SJames Wright     const CeedInt      *offsets, *pb_offsets;
186701f0e615SJames Wright 
186801f0e615SJames Wright     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[o], &active_elem_rstr));
186901f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstr, &comp_stride));
187001f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumElements(active_elem_rstr, &num_elem));
187101f0e615SJames Wright     CeedCall(CeedElemRestrictionGetElementSize(active_elem_rstr, &elem_size));
187201f0e615SJames Wright     CeedCall(CeedElemRestrictionGetOffsets(active_elem_rstr, CEED_MEM_HOST, &offsets));
187301f0e615SJames Wright 
187401f0e615SJames Wright     CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstr, &pb_active_elem_rstr));
187501f0e615SJames Wright     CeedCall(CeedElemRestrictionGetOffsets(pb_active_elem_rstr, CEED_MEM_HOST, &pb_offsets));
187601f0e615SJames Wright 
187701f0e615SJames Wright     for (CeedSize i = 0; i < num_elem * elem_size; i++) {
187801f0e615SJames Wright       for (CeedInt c_out = 0; c_out < num_active_components; c_out++) {
187901f0e615SJames Wright         for (CeedInt c_in = 0; c_in < num_active_components; c_in++) {
188001f0e615SJames Wright           (*rows)[pb_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_out * comp_stride;
188101f0e615SJames Wright           (*cols)[pb_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_in * comp_stride;
188201f0e615SJames Wright         }
188301f0e615SJames Wright       }
188401f0e615SJames Wright     }
188501f0e615SJames Wright 
188601f0e615SJames Wright     CeedCall(CeedElemRestrictionRestoreOffsets(active_elem_rstr, &offsets));
188701f0e615SJames Wright     CeedCall(CeedElemRestrictionRestoreOffsets(pb_active_elem_rstr, &pb_offsets));
188801f0e615SJames Wright     CeedCall(CeedElemRestrictionDestroy(&pb_active_elem_rstr));
188901f0e615SJames Wright   }
189001f0e615SJames Wright   return CEED_ERROR_SUCCESS;
189101f0e615SJames Wright }
189201f0e615SJames Wright 
189301f0e615SJames Wright /**
1894eaf62fffSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear CeedOperator
1895eaf62fffSJeremy L Thompson 
1896ea61e9acSJeremy L Thompson   This overwrites a CeedVector with the point block diagonal of a linear CeedOperator.
1897eaf62fffSJeremy L Thompson 
1898ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1899eaf62fffSJeremy L Thompson 
1900ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1901f04ea552SJeremy L Thompson 
1902ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1903ea61e9acSJeremy 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
1904ea61e9acSJeremy 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,
1905ea61e9acSJeremy L Thompson component in].
1906ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1907eaf62fffSJeremy L Thompson 
1908eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1909eaf62fffSJeremy L Thompson 
1910eaf62fffSJeremy L Thompson   @ref User
1911eaf62fffSJeremy L Thompson **/
19122b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1913f3d47e36SJeremy L Thompson   bool     is_composite;
19141c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
19151c66c397SJeremy L Thompson 
19162b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1917f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1918eaf62fffSJeremy L Thompson 
19192b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
19206574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1921c9366a6bSJeremy L Thompson 
1922f3d47e36SJeremy L Thompson   // Early exit for empty operator
1923f3d47e36SJeremy L Thompson   if (!is_composite) {
1924f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1925f3d47e36SJeremy L Thompson 
1926f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1927f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1928f3d47e36SJeremy L Thompson   }
1929f3d47e36SJeremy L Thompson 
1930eaf62fffSJeremy L Thompson   if (op->LinearAssemblePointBlockDiagonal) {
1931d04bbc78SJeremy L Thompson     // Backend version
19322b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request));
1933eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1934eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddPointBlockDiagonal) {
1935d04bbc78SJeremy L Thompson     // Backend version with zeroing first
19362b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
19372b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
1938eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1939eaf62fffSJeremy L Thompson   } else {
1940d04bbc78SJeremy L Thompson     // Operator fallback
1941d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1942d04bbc78SJeremy L Thompson 
19432b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1944d04bbc78SJeremy L Thompson     if (op_fallback) {
19452b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request));
1946eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1947eaf62fffSJeremy L Thompson     }
1948eaf62fffSJeremy L Thompson   }
1949eaf62fffSJeremy L Thompson   // Default interface implementation
19502b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
19512b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
1952eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1953eaf62fffSJeremy L Thompson }
1954eaf62fffSJeremy L Thompson 
1955eaf62fffSJeremy L Thompson /**
1956eaf62fffSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear CeedOperator
1957eaf62fffSJeremy L Thompson 
1958ea61e9acSJeremy L Thompson   This sums into a CeedVector with the point block diagonal of a linear CeedOperator.
1959eaf62fffSJeremy L Thompson 
1960ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1961eaf62fffSJeremy L Thompson 
1962ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1963f04ea552SJeremy L Thompson 
1964ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1965ea61e9acSJeremy 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
1966ea61e9acSJeremy 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,
1967ea61e9acSJeremy L Thompson component in].
1968ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1969eaf62fffSJeremy L Thompson 
1970eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1971eaf62fffSJeremy L Thompson 
1972eaf62fffSJeremy L Thompson   @ref User
1973eaf62fffSJeremy L Thompson **/
19742b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1975f3d47e36SJeremy L Thompson   bool     is_composite;
19761c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
19771c66c397SJeremy L Thompson 
19782b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1979f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1980eaf62fffSJeremy L Thompson 
19812b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
19826574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1983c9366a6bSJeremy L Thompson 
1984f3d47e36SJeremy L Thompson   // Early exit for empty operator
1985f3d47e36SJeremy L Thompson   if (!is_composite) {
1986f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1987f3d47e36SJeremy L Thompson 
1988f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1989f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1990f3d47e36SJeremy L Thompson   }
1991f3d47e36SJeremy L Thompson 
1992eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddPointBlockDiagonal) {
1993d04bbc78SJeremy L Thompson     // Backend version
19942b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request));
1995eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1996eaf62fffSJeremy L Thompson   } else {
1997d04bbc78SJeremy L Thompson     // Operator fallback
1998d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1999d04bbc78SJeremy L Thompson 
20002b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2001d04bbc78SJeremy L Thompson     if (op_fallback) {
20022b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request));
2003eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2004eaf62fffSJeremy L Thompson     }
2005eaf62fffSJeremy L Thompson   }
2006ea61e9acSJeremy L Thompson   // Default interface implementation
2007eaf62fffSJeremy L Thompson   if (is_composite) {
20082b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
2009eaf62fffSJeremy L Thompson   } else {
20102b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, true, assembled));
2011eaf62fffSJeremy L Thompson   }
2012d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2013eaf62fffSJeremy L Thompson }
2014eaf62fffSJeremy L Thompson 
2015eaf62fffSJeremy L Thompson /**
2016eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero pattern of a linear operator.
2017eaf62fffSJeremy L Thompson 
2018ea61e9acSJeremy L Thompson    Expected to be used in conjunction with CeedOperatorLinearAssemble().
2019eaf62fffSJeremy L Thompson 
2020ea61e9acSJeremy 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
20219fd66db6SSebastian Grimberg matrix in entry (i, j).
20229fd66db6SSebastian Grimberg   Note that the (i, j) pairs are not unique and may repeat.
20239fd66db6SSebastian Grimberg   This function returns the number of entries and their (i, j) locations, while CeedOperatorLinearAssemble() provides the values in the same ordering.
2024eaf62fffSJeremy L Thompson 
2025eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2026eaf62fffSJeremy L Thompson 
2027ea61e9acSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2028f04ea552SJeremy L Thompson 
2029eaf62fffSJeremy L Thompson    @param[in]  op          CeedOperator to assemble
2030eaf62fffSJeremy L Thompson    @param[out] num_entries Number of entries in coordinate nonzero pattern
2031eaf62fffSJeremy L Thompson    @param[out] rows        Row number for each entry
2032eaf62fffSJeremy L Thompson    @param[out] cols        Column number for each entry
2033eaf62fffSJeremy L Thompson 
2034eaf62fffSJeremy L Thompson    @ref User
2035eaf62fffSJeremy L Thompson **/
20362b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
20371c66c397SJeremy L Thompson   bool          is_composite;
20381c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2039b94338b9SJed Brown   CeedSize      single_entries;
2040eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
20411c66c397SJeremy L Thompson 
20422b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2043f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2044eaf62fffSJeremy L Thompson 
2045eaf62fffSJeremy L Thompson   if (op->LinearAssembleSymbolic) {
2046d04bbc78SJeremy L Thompson     // Backend version
20472b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols));
2048eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2049eaf62fffSJeremy L Thompson   } else {
2050d04bbc78SJeremy L Thompson     // Operator fallback
2051d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2052d04bbc78SJeremy L Thompson 
20532b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2054d04bbc78SJeremy L Thompson     if (op_fallback) {
20552b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols));
2056eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2057eaf62fffSJeremy L Thompson     }
2058eaf62fffSJeremy L Thompson   }
2059eaf62fffSJeremy L Thompson 
2060eaf62fffSJeremy L Thompson   // Default interface implementation
2061eaf62fffSJeremy L Thompson 
2062eaf62fffSJeremy L Thompson   // count entries and allocate rows, cols arrays
2063eaf62fffSJeremy L Thompson   *num_entries = 0;
2064eaf62fffSJeremy L Thompson   if (is_composite) {
2065c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2066c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
206792ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
20682b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2069eaf62fffSJeremy L Thompson       *num_entries += single_entries;
2070eaf62fffSJeremy L Thompson     }
2071eaf62fffSJeremy L Thompson   } else {
20722b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries));
2073eaf62fffSJeremy L Thompson     *num_entries += single_entries;
2074eaf62fffSJeremy L Thompson   }
20752b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, rows));
20762b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, cols));
2077eaf62fffSJeremy L Thompson 
2078eaf62fffSJeremy L Thompson   // assemble nonzero locations
2079eaf62fffSJeremy L Thompson   if (is_composite) {
2080c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2081c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
208292ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
20832b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols));
20842b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2085eaf62fffSJeremy L Thompson       offset += single_entries;
2086eaf62fffSJeremy L Thompson     }
2087eaf62fffSJeremy L Thompson   } else {
20882b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols));
2089eaf62fffSJeremy L Thompson   }
2090eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2091eaf62fffSJeremy L Thompson }
2092eaf62fffSJeremy L Thompson 
2093eaf62fffSJeremy L Thompson /**
2094eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero entries of a linear operator.
2095eaf62fffSJeremy L Thompson 
2096ea61e9acSJeremy L Thompson    Expected to be used in conjunction with CeedOperatorLinearAssembleSymbolic().
2097eaf62fffSJeremy L Thompson 
2098ea61e9acSJeremy 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
20999fd66db6SSebastian Grimberg matrix in entry (i, j).
21009fd66db6SSebastian Grimberg   Note that the (i, j) pairs are not unique and may repeat.
21019fd66db6SSebastian Grimberg   This function returns the values of the nonzero entries to be added, their (i, j) locations are provided by CeedOperatorLinearAssembleSymbolic()
2102eaf62fffSJeremy L Thompson 
2103eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2104eaf62fffSJeremy L Thompson 
2105ea61e9acSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2106f04ea552SJeremy L Thompson 
2107eaf62fffSJeremy L Thompson    @param[in]  op     CeedOperator to assemble
2108eaf62fffSJeremy L Thompson    @param[out] values Values to assemble into matrix
2109eaf62fffSJeremy L Thompson 
2110eaf62fffSJeremy L Thompson    @ref User
2111eaf62fffSJeremy L Thompson **/
2112eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) {
21131c66c397SJeremy L Thompson   bool          is_composite;
21141c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2115b94338b9SJed Brown   CeedSize      single_entries = 0;
2116eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
21171c66c397SJeremy L Thompson 
21182b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2119f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2120f3d47e36SJeremy L Thompson 
2121f3d47e36SJeremy L Thompson   // Early exit for empty operator
2122f3d47e36SJeremy L Thompson   if (!is_composite) {
2123f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2124f3d47e36SJeremy L Thompson 
2125f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2126f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2127f3d47e36SJeremy L Thompson   }
2128eaf62fffSJeremy L Thompson 
2129eaf62fffSJeremy L Thompson   if (op->LinearAssemble) {
2130d04bbc78SJeremy L Thompson     // Backend version
21312b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemble(op, values));
2132eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2133eaf62fffSJeremy L Thompson   } else {
2134d04bbc78SJeremy L Thompson     // Operator fallback
2135d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2136d04bbc78SJeremy L Thompson 
21372b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2138d04bbc78SJeremy L Thompson     if (op_fallback) {
21392b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemble(op_fallback, values));
2140eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2141eaf62fffSJeremy L Thompson     }
2142eaf62fffSJeremy L Thompson   }
2143eaf62fffSJeremy L Thompson 
2144eaf62fffSJeremy L Thompson   // Default interface implementation
214528ec399dSJeremy L Thompson   CeedCall(CeedVectorSetValue(values, 0.0));
2146eaf62fffSJeremy L Thompson   if (is_composite) {
2147c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2148c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2149cefa2673SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; k++) {
21502b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values));
21512b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2152eaf62fffSJeremy L Thompson       offset += single_entries;
2153eaf62fffSJeremy L Thompson     }
2154eaf62fffSJeremy L Thompson   } else {
21552b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemble(op, offset, values));
2156eaf62fffSJeremy L Thompson   }
2157eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2158eaf62fffSJeremy L Thompson }
2159eaf62fffSJeremy L Thompson 
2160eaf62fffSJeremy L Thompson /**
216175f0d5a4SJeremy L Thompson   @brief Get the multiplicity of nodes across suboperators in a composite CeedOperator
216275f0d5a4SJeremy L Thompson 
216375f0d5a4SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
216475f0d5a4SJeremy L Thompson 
216575f0d5a4SJeremy L Thompson   @param[in]  op               Composite CeedOperator
216675f0d5a4SJeremy L Thompson   @param[in]  num_skip_indices Number of suboperators to skip
216775f0d5a4SJeremy L Thompson   @param[in]  skip_indices     Array of indices of suboperators to skip
216875f0d5a4SJeremy L Thompson   @param[out] mult             Vector to store multiplicity (of size l_size)
216975f0d5a4SJeremy L Thompson 
217075f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
217175f0d5a4SJeremy L Thompson 
217275f0d5a4SJeremy L Thompson   @ref User
217375f0d5a4SJeremy L Thompson **/
217475f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) {
217575f0d5a4SJeremy L Thompson   Ceed                ceed;
2176b275c451SJeremy L Thompson   CeedInt             num_suboperators;
217775f0d5a4SJeremy L Thompson   CeedSize            l_vec_len;
217875f0d5a4SJeremy L Thompson   CeedScalar         *mult_array;
217975f0d5a4SJeremy L Thompson   CeedVector          ones_l_vec;
21807c1dbaffSSebastian Grimberg   CeedElemRestriction elem_rstr, mult_elem_rstr;
2181b275c451SJeremy L Thompson   CeedOperator       *sub_operators;
218275f0d5a4SJeremy L Thompson 
21831c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
21841c66c397SJeremy L Thompson 
218575f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
218675f0d5a4SJeremy L Thompson 
218775f0d5a4SJeremy L Thompson   // Zero mult vector
218875f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
218975f0d5a4SJeremy L Thompson 
219075f0d5a4SJeremy L Thompson   // Get suboperators
2191b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2192b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2193b275c451SJeremy L Thompson   if (num_suboperators == 0) return CEED_ERROR_SUCCESS;
219475f0d5a4SJeremy L Thompson 
219575f0d5a4SJeremy L Thompson   // Work vector
219675f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetLength(mult, &l_vec_len));
219775f0d5a4SJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec));
219875f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(ones_l_vec, 1.0));
219975f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array));
220075f0d5a4SJeremy L Thompson 
220175f0d5a4SJeremy L Thompson   // Compute multiplicity across suboperators
2202b275c451SJeremy L Thompson   for (CeedInt i = 0; i < num_suboperators; i++) {
220375f0d5a4SJeremy L Thompson     const CeedScalar *sub_mult_array;
220475f0d5a4SJeremy L Thompson     CeedVector        sub_mult_l_vec, ones_e_vec;
220575f0d5a4SJeremy L Thompson 
220675f0d5a4SJeremy L Thompson     // -- Check for suboperator to skip
220775f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < num_skip_indices; j++) {
220875f0d5a4SJeremy L Thompson       if (skip_indices[j] == i) continue;
220975f0d5a4SJeremy L Thompson     }
221075f0d5a4SJeremy L Thompson 
221175f0d5a4SJeremy L Thompson     // -- Sub operator multiplicity
2212437c7c90SJeremy L Thompson     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr));
22137c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr, &mult_elem_rstr));
22147c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateVector(mult_elem_rstr, &sub_mult_l_vec, &ones_e_vec));
221575f0d5a4SJeremy L Thompson     CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0));
22167c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE));
22177c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE));
221875f0d5a4SJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array));
221975f0d5a4SJeremy L Thompson     // ---- Flag every node present in the current suboperator
222075f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < l_vec_len; j++) {
222175f0d5a4SJeremy L Thompson       if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0;
222275f0d5a4SJeremy L Thompson     }
222375f0d5a4SJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array));
222475f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&sub_mult_l_vec));
222575f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&ones_e_vec));
22267c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&mult_elem_rstr));
222775f0d5a4SJeremy L Thompson   }
222875f0d5a4SJeremy L Thompson   CeedCall(CeedVectorRestoreArray(mult, &mult_array));
2229811d0ccfSJeremy L Thompson   CeedCall(CeedVectorDestroy(&ones_l_vec));
223075f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
223175f0d5a4SJeremy L Thompson }
223275f0d5a4SJeremy L Thompson 
223375f0d5a4SJeremy L Thompson /**
2234ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator, creating the prolongation basis from the fine and coarse
2235ea61e9acSJeremy L Thompson grid interpolation
2236eaf62fffSJeremy L Thompson 
223758e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2238f04ea552SJeremy L Thompson 
2239eaf62fffSJeremy L Thompson   @param[in]  op_fine      Fine grid operator
224085bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2241eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid restriction
2242eaf62fffSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector basis
2243eaf62fffSJeremy L Thompson   @param[out] op_coarse    Coarse grid operator
224485bb9dcfSJeremy L Thompson   @param[out] op_prolong   Coarse to fine operator, or NULL
2245*7758292fSSebastian Grimberg   @param[out] op_restrict  Fine to coarse operator, or NULL
2246eaf62fffSJeremy L Thompson 
2247eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2248eaf62fffSJeremy L Thompson 
2249eaf62fffSJeremy L Thompson   @ref User
2250eaf62fffSJeremy L Thompson **/
22512b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
2252*7758292fSSebastian Grimberg                                      CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
22531c66c397SJeremy L Thompson   CeedBasis basis_c_to_f = NULL;
22541c66c397SJeremy L Thompson 
22552b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2256eaf62fffSJeremy L Thompson 
225783d6adf3SZach Atkins   // Build prolongation matrix, if required
2258*7758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
225983d6adf3SZach Atkins     CeedBasis basis_fine;
22601c66c397SJeremy L Thompson 
22612b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
22622b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f));
226383d6adf3SZach Atkins   }
2264eaf62fffSJeremy L Thompson 
2265f113e5dcSJeremy L Thompson   // Core code
2266*7758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2267eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2268eaf62fffSJeremy L Thompson }
2269eaf62fffSJeremy L Thompson 
2270eaf62fffSJeremy L Thompson /**
2271ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a tensor basis for the active basis
2272eaf62fffSJeremy L Thompson 
227358e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2274f04ea552SJeremy L Thompson 
2275eaf62fffSJeremy L Thompson   @param[in]  op_fine       Fine grid operator
227685bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2277eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid restriction
2278eaf62fffSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector basis
227985bb9dcfSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
2280eaf62fffSJeremy L Thompson   @param[out] op_coarse     Coarse grid operator
228185bb9dcfSJeremy L Thompson   @param[out] op_prolong    Coarse to fine operator, or NULL
2282*7758292fSSebastian Grimberg   @param[out] op_restrict   Fine to coarse operator, or NULL
2283eaf62fffSJeremy L Thompson 
2284eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2285eaf62fffSJeremy L Thompson 
2286eaf62fffSJeremy L Thompson   @ref User
2287eaf62fffSJeremy L Thompson **/
22882b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
22892b730f8bSJeremy L Thompson                                              const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
2290*7758292fSSebastian Grimberg                                              CeedOperator *op_restrict) {
2291eaf62fffSJeremy L Thompson   Ceed      ceed;
22921c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
22931c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
22941c66c397SJeremy L Thompson 
22951c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
22962b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2297eaf62fffSJeremy L Thompson 
2298eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
22992b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
23002b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
23012b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
23026574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2303eaf62fffSJeremy L Thompson 
230483d6adf3SZach Atkins   // Create coarse to fine basis, if required
2305*7758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
23061c66c397SJeremy L Thompson     CeedInt     dim, num_comp, num_nodes_c, P_1d_f, P_1d_c;
23071c66c397SJeremy L Thompson     CeedScalar *q_ref, *q_weight, *grad;
23081c66c397SJeremy L Thompson 
230983d6adf3SZach Atkins     // Check if interpolation matrix is provided
23106574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
23116574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
23122b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
23132b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
23142b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f));
23152b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
23162b730f8bSJeremy L Thompson     P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c);
23172b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_ref));
23182b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_weight));
23192b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad));
23202b730f8bSJeremy 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));
23212b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
23222b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
23232b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
232483d6adf3SZach Atkins   }
2325eaf62fffSJeremy L Thompson 
2326eaf62fffSJeremy L Thompson   // Core code
2327*7758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2328eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2329eaf62fffSJeremy L Thompson }
2330eaf62fffSJeremy L Thompson 
2331eaf62fffSJeremy L Thompson /**
2332ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a non-tensor basis for the active vector
2333eaf62fffSJeremy L Thompson 
233458e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2335f04ea552SJeremy L Thompson 
2336eaf62fffSJeremy L Thompson   @param[in]  op_fine       Fine grid operator
233785bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2338eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid restriction
2339eaf62fffSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector basis
234085bb9dcfSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
2341eaf62fffSJeremy L Thompson   @param[out] op_coarse     Coarse grid operator
234285bb9dcfSJeremy L Thompson   @param[out] op_prolong    Coarse to fine operator, or NULL
2343*7758292fSSebastian Grimberg   @param[out] op_restrict   Fine to coarse operator, or NULL
2344eaf62fffSJeremy L Thompson 
2345eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2346eaf62fffSJeremy L Thompson 
2347eaf62fffSJeremy L Thompson   @ref User
2348eaf62fffSJeremy L Thompson **/
23492b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
2350*7758292fSSebastian Grimberg                                        const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
2351*7758292fSSebastian Grimberg                                        CeedOperator *op_restrict) {
2352eaf62fffSJeremy L Thompson   Ceed      ceed;
23531c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
23541c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
23551c66c397SJeremy L Thompson 
23561c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
23572b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2358eaf62fffSJeremy L Thompson 
2359eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
23602b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
23612b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
23622b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
23636574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2364eaf62fffSJeremy L Thompson 
2365eaf62fffSJeremy L Thompson   // Coarse to fine basis
2366*7758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
23671c66c397SJeremy L Thompson     CeedInt          dim, num_comp, num_nodes_c, num_nodes_f;
23681c66c397SJeremy L Thompson     CeedScalar      *q_ref, *q_weight, *grad;
23691c66c397SJeremy L Thompson     CeedElemTopology topo;
23701c66c397SJeremy L Thompson 
237183d6adf3SZach Atkins     // Check if interpolation matrix is provided
23726574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
23736574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
23742b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_fine, &topo));
23752b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
23762b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
23772b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f));
23782b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
23792b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref));
23802b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f, &q_weight));
23812b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad));
23822b730f8bSJeremy 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));
23832b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
23842b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
23852b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
238683d6adf3SZach Atkins   }
2387eaf62fffSJeremy L Thompson 
2388eaf62fffSJeremy L Thompson   // Core code
2389*7758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2390eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2391eaf62fffSJeremy L Thompson }
2392eaf62fffSJeremy L Thompson 
2393eaf62fffSJeremy L Thompson /**
2394ea61e9acSJeremy L Thompson   @brief Build a FDM based approximate inverse for each element for a CeedOperator
2395eaf62fffSJeremy L Thompson 
2396ea61e9acSJeremy L Thompson   This returns a CeedOperator and CeedVector to apply a Fast Diagonalization Method based approximate inverse.
2397859c15bbSJames Wright   This function obtains the simultaneous diagonalization for the 1D mass and Laplacian operators, \f$M = V^T V, K = V^T S V\f$.
2398859c15bbSJames Wright   The assembled QFunction is used to modify the eigenvalues from simultaneous diagonalization and obtain an approximate inverse of the form \f$V^T
23999fd66db6SSebastian Grimberg \hat S V\f$.
24009fd66db6SSebastian Grimberg   The CeedOperator must be linear and non-composite.
24019fd66db6SSebastian Grimberg   The associated CeedQFunction must therefore also be linear.
2402eaf62fffSJeremy L Thompson 
2403ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2404f04ea552SJeremy L Thompson 
2405ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to create element inverses
2406ea61e9acSJeremy L Thompson   @param[out] fdm_inv CeedOperator to apply the action of a FDM based inverse for each element
2407ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2408eaf62fffSJeremy L Thompson 
2409eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2410eaf62fffSJeremy L Thompson 
2411480fae85SJeremy L Thompson   @ref User
2412eaf62fffSJeremy L Thompson **/
24132b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) {
24141c66c397SJeremy L Thompson   Ceed                 ceed, ceed_parent;
24151c66c397SJeremy L Thompson   bool                 interp = false, grad = false, is_tensor_basis = true;
24161c66c397SJeremy L Thompson   CeedInt              num_input_fields, P_1d, Q_1d, num_nodes, num_qpts, dim, num_comp = 1, num_elem = 1;
24171c66c397SJeremy L Thompson   CeedSize             l_size = 1;
24181c66c397SJeremy L Thompson   CeedScalar          *mass, *laplace, *x, *fdm_interp, *lambda, *elem_avg;
24191c66c397SJeremy L Thompson   const CeedScalar    *interp_1d, *grad_1d, *q_weight_1d;
24201c66c397SJeremy L Thompson   CeedVector           q_data;
24211c66c397SJeremy L Thompson   CeedElemRestriction  rstr  = NULL, rstr_qd_i;
24221c66c397SJeremy L Thompson   CeedBasis            basis = NULL, fdm_basis;
24231c66c397SJeremy L Thompson   CeedQFunctionContext ctx_fdm;
24241c66c397SJeremy L Thompson   CeedQFunctionField  *qf_fields;
24251c66c397SJeremy L Thompson   CeedQFunction        qf, qf_fdm;
24261c66c397SJeremy L Thompson   CeedOperatorField   *op_fields;
24271c66c397SJeremy L Thompson 
24282b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2429eaf62fffSJeremy L Thompson 
2430eaf62fffSJeremy L Thompson   if (op->CreateFDMElementInverse) {
2431d04bbc78SJeremy L Thompson     // Backend version
24322b730f8bSJeremy L Thompson     CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request));
2433eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2434eaf62fffSJeremy L Thompson   } else {
2435d04bbc78SJeremy L Thompson     // Operator fallback
2436d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2437d04bbc78SJeremy L Thompson 
24382b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2439d04bbc78SJeremy L Thompson     if (op_fallback) {
24402b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request));
2441eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2442eaf62fffSJeremy L Thompson     }
2443eaf62fffSJeremy L Thompson   }
2444eaf62fffSJeremy L Thompson 
2445d04bbc78SJeremy L Thompson   // Default interface implementation
24462b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
2447bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
24482b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
2449eaf62fffSJeremy L Thompson 
2450eaf62fffSJeremy L Thompson   // Determine active input basis
24512b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL));
24522b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
2453eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
2454eaf62fffSJeremy L Thompson     CeedVector vec;
24551c66c397SJeremy L Thompson 
24562b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
2457eaf62fffSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2458eaf62fffSJeremy L Thompson       CeedEvalMode eval_mode;
24591c66c397SJeremy L Thompson 
24602b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
2461eaf62fffSJeremy L Thompson       interp = interp || eval_mode == CEED_EVAL_INTERP;
2462eaf62fffSJeremy L Thompson       grad   = grad || eval_mode == CEED_EVAL_GRAD;
24632b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis));
24642b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
2465eaf62fffSJeremy L Thompson     }
2466eaf62fffSJeremy L Thompson   }
24676574a04fSJeremy L Thompson   CeedCheck(basis, ceed, CEED_ERROR_BACKEND, "No active field set");
24682b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
2469352a5e7cSSebastian Grimberg   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
24702b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
24712b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
24722b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
24732b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
24742b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
24752b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
2476eaf62fffSJeremy L Thompson 
2477eaf62fffSJeremy L Thompson   // Build and diagonalize 1D Mass and Laplacian
24786574a04fSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
24796574a04fSJeremy L Thompson   CeedCheck(is_tensor_basis, ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases");
24802b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &mass));
24812b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &laplace));
24822b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &x));
24832b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp));
24842b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &lambda));
2485eaf62fffSJeremy L Thompson   // -- Build matrices
24862b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
24872b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
24882b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
24892b730f8bSJeremy L Thompson   CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace));
2490eaf62fffSJeremy L Thompson 
2491eaf62fffSJeremy L Thompson   // -- Diagonalize
24922b730f8bSJeremy L Thompson   CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d));
24932b730f8bSJeremy L Thompson   CeedCall(CeedFree(&mass));
24942b730f8bSJeremy L Thompson   CeedCall(CeedFree(&laplace));
24952b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
24962b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d];
24972b730f8bSJeremy L Thompson   }
24982b730f8bSJeremy L Thompson   CeedCall(CeedFree(&x));
2499eaf62fffSJeremy L Thompson 
25001c66c397SJeremy L Thompson   {
25011c66c397SJeremy L Thompson     CeedInt             layout[3], num_modes = (interp ? 1 : 0) + (grad ? dim : 0);
25021c66c397SJeremy L Thompson     CeedScalar          max_norm = 0;
25031c66c397SJeremy L Thompson     const CeedScalar   *assembled_array, *q_weight_array;
25041c66c397SJeremy L Thompson     CeedVector          assembled = NULL, q_weight;
2505c5f45aeaSJeremy L Thompson     CeedElemRestriction rstr_qf   = NULL;
25061c66c397SJeremy L Thompson 
25071c66c397SJeremy L Thompson     // Assemble QFunction
25082b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request));
25092b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetELayout(rstr_qf, &layout));
25102b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr_qf));
25112b730f8bSJeremy L Thompson     CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm));
2512eaf62fffSJeremy L Thompson 
2513eaf62fffSJeremy L Thompson     // Calculate element averages
25142b730f8bSJeremy L Thompson     CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight));
25152b730f8bSJeremy L Thompson     CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight));
25162b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array));
25172b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array));
25182b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_elem, &elem_avg));
2519eaf62fffSJeremy L Thompson     const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON;
25201c66c397SJeremy L Thompson 
2521eaf62fffSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
2522eaf62fffSJeremy L Thompson       CeedInt count = 0;
25231c66c397SJeremy L Thompson 
25242b730f8bSJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
25252b730f8bSJeremy L Thompson         for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) {
25262b730f8bSJeremy L Thompson           if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) {
25272b730f8bSJeremy L Thompson             elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q];
2528eaf62fffSJeremy L Thompson             count++;
2529eaf62fffSJeremy L Thompson           }
25302b730f8bSJeremy L Thompson         }
25312b730f8bSJeremy L Thompson       }
2532eaf62fffSJeremy L Thompson       if (count) {
2533eaf62fffSJeremy L Thompson         elem_avg[e] /= count;
2534eaf62fffSJeremy L Thompson       } else {
2535eaf62fffSJeremy L Thompson         elem_avg[e] = 1.0;
2536eaf62fffSJeremy L Thompson       }
2537eaf62fffSJeremy L Thompson     }
25382b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array));
25392b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled));
25402b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array));
25412b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&q_weight));
25421c66c397SJeremy L Thompson   }
2543eaf62fffSJeremy L Thompson 
2544eaf62fffSJeremy L Thompson   // Build FDM diagonal
25451c66c397SJeremy L Thompson   {
2546eaf62fffSJeremy L Thompson     CeedScalar *q_data_array, *fdm_diagonal;
25471c66c397SJeremy L Thompson 
2548352a5e7cSSebastian Grimberg     CeedCall(CeedCalloc(num_comp * num_nodes, &fdm_diagonal));
2549352a5e7cSSebastian Grimberg     const CeedScalar fdm_diagonal_bound = num_nodes * CEED_EPSILON;
25502b730f8bSJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
2551352a5e7cSSebastian Grimberg       for (CeedInt n = 0; n < num_nodes; n++) {
2552352a5e7cSSebastian Grimberg         if (interp) fdm_diagonal[c * num_nodes + n] = 1.0;
25532b730f8bSJeremy L Thompson         if (grad) {
2554eaf62fffSJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
2555eaf62fffSJeremy L Thompson             CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d;
2556352a5e7cSSebastian Grimberg             fdm_diagonal[c * num_nodes + n] += lambda[i];
2557eaf62fffSJeremy L Thompson           }
2558eaf62fffSJeremy L Thompson         }
2559352a5e7cSSebastian Grimberg         if (fabs(fdm_diagonal[c * num_nodes + n]) < fdm_diagonal_bound) fdm_diagonal[c * num_nodes + n] = fdm_diagonal_bound;
25602b730f8bSJeremy L Thompson       }
25612b730f8bSJeremy L Thompson     }
2562352a5e7cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * num_nodes, &q_data));
25632b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(q_data, 0.0));
25642b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array));
25652b730f8bSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
25662b730f8bSJeremy L Thompson       for (CeedInt c = 0; c < num_comp; c++) {
25671c66c397SJeremy L Thompson         for (CeedInt n = 0; n < num_nodes; n++)
25681c66c397SJeremy L Thompson           q_data_array[(e * num_comp + c) * num_nodes + n] = 1. / (elem_avg[e] * fdm_diagonal[c * num_nodes + n]);
25692b730f8bSJeremy L Thompson       }
25702b730f8bSJeremy L Thompson     }
25712b730f8bSJeremy L Thompson     CeedCall(CeedFree(&elem_avg));
25722b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_diagonal));
25732b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(q_data, &q_data_array));
25741c66c397SJeremy L Thompson   }
2575eaf62fffSJeremy L Thompson 
2576eaf62fffSJeremy L Thompson   // Setup FDM operator
2577eaf62fffSJeremy L Thompson   // -- Basis
25781c66c397SJeremy L Thompson   {
2579eaf62fffSJeremy L Thompson     CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy;
25801c66c397SJeremy L Thompson 
25812b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy));
25822b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_ref_dummy));
25832b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_weight_dummy));
25842b730f8bSJeremy 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));
25852b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_interp));
25862b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad_dummy));
25872b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref_dummy));
25882b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight_dummy));
25892b730f8bSJeremy L Thompson     CeedCall(CeedFree(&lambda));
25901c66c397SJeremy L Thompson   }
2591eaf62fffSJeremy L Thompson 
2592eaf62fffSJeremy L Thompson   // -- Restriction
25931c66c397SJeremy L Thompson   {
2594352a5e7cSSebastian Grimberg     CeedInt strides[3] = {1, num_nodes, num_nodes * num_comp};
2595352a5e7cSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, num_nodes, num_comp, num_elem * num_comp * num_nodes, strides, &rstr_qd_i));
25961c66c397SJeremy L Thompson   }
25971c66c397SJeremy L Thompson 
2598eaf62fffSJeremy L Thompson   // -- QFunction
25992b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm));
26002b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP));
26012b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE));
26022b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP));
26032b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp));
26041c66c397SJeremy L Thompson 
2605eaf62fffSJeremy L Thompson   // -- QFunction context
26061c66c397SJeremy L Thompson   {
2607eaf62fffSJeremy L Thompson     CeedInt *num_comp_data;
26081c66c397SJeremy L Thompson 
26092b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_data));
2610eaf62fffSJeremy L Thompson     num_comp_data[0] = num_comp;
26112b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm));
26122b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data));
26131c66c397SJeremy L Thompson   }
26142b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm));
26152b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_fdm));
26161c66c397SJeremy L Thompson 
2617eaf62fffSJeremy L Thompson   // -- Operator
26182b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv));
26192b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2620356036faSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_NONE, q_data));
26212b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2622eaf62fffSJeremy L Thompson 
2623eaf62fffSJeremy L Thompson   // Cleanup
26242b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_data));
26252b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&fdm_basis));
26262b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i));
26272b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_fdm));
2628eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2629eaf62fffSJeremy L Thompson }
2630eaf62fffSJeremy L Thompson 
2631eaf62fffSJeremy L Thompson /// @}
2632