xref: /libCEED/interface/ceed-preconditioning.c (revision 7d5185d77fd3980a37189da5411f3a999b5628b9)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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 /**
27ca94c3ddSJeremy L Thompson   @brief Duplicate a `CeedQFunction` with a reference `Ceed` to fallback for advanced `CeedOperator` functionality
289e77b9c8SJeremy L Thompson 
29ca94c3ddSJeremy L Thompson   @param[in]  fallback_ceed `Ceed` on which to create fallback `CeedQFunction`
30ca94c3ddSJeremy L Thompson   @param[in]  qf            `CeedQFunction` to create fallback for
31ca94c3ddSJeremy L Thompson   @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;
391203703bSJeremy L Thompson   CeedInt             num_input_fields, num_output_fields;
401203703bSJeremy L Thompson   Ceed                ceed;
411203703bSJeremy L Thompson   CeedQFunctionField *input_fields, *output_fields;
421c66c397SJeremy L Thompson 
439e77b9c8SJeremy L Thompson   // Check if NULL qf passed in
449e77b9c8SJeremy L Thompson   if (!qf) return CEED_ERROR_SUCCESS;
459e77b9c8SJeremy L Thompson 
461203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetCeed(qf, &ceed));
471203703bSJeremy L Thompson   CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n");
481203703bSJeremy L Thompson   CeedDebug(ceed, "Creating fallback CeedQFunction\n");
49d04bbc78SJeremy L Thompson 
509e77b9c8SJeremy L Thompson   if (qf->source_path) {
512b730f8bSJeremy L Thompson     size_t path_len = strlen(qf->source_path), name_len = strlen(qf->kernel_name);
522b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(path_len + name_len + 2, &source_path_with_name));
539e77b9c8SJeremy L Thompson     memcpy(source_path_with_name, qf->source_path, path_len);
549e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len], ":", 1);
559e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len + 1], qf->kernel_name, name_len);
569e77b9c8SJeremy L Thompson   } else {
572b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &source_path_with_name));
589e77b9c8SJeremy L Thompson   }
599e77b9c8SJeremy L Thompson 
601203703bSJeremy L Thompson   {
611203703bSJeremy L Thompson     CeedInt           vec_length;
621203703bSJeremy L Thompson     CeedQFunctionUser f;
631203703bSJeremy L Thompson 
641203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length));
651203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetUserFunction(qf, &f));
661203703bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(fallback_ceed, vec_length, f, source_path_with_name, qf_fallback));
671203703bSJeremy L Thompson   }
689e77b9c8SJeremy L Thompson   {
699e77b9c8SJeremy L Thompson     CeedQFunctionContext ctx;
709e77b9c8SJeremy L Thompson 
712b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
722b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx));
739e77b9c8SJeremy L Thompson   }
741203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
751203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
766f8994e9SJeremy L Thompson     const char  *field_name;
771203703bSJeremy L Thompson     CeedInt      size;
781203703bSJeremy L Thompson     CeedEvalMode eval_mode;
791203703bSJeremy L Thompson 
80ab747706SJeremy L Thompson     CeedCall(CeedQFunctionFieldGetData(input_fields[i], &field_name, &size, &eval_mode));
811203703bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(*qf_fallback, field_name, size, eval_mode));
829e77b9c8SJeremy L Thompson   }
831203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
846f8994e9SJeremy L Thompson     const char  *field_name;
851203703bSJeremy L Thompson     CeedInt      size;
861203703bSJeremy L Thompson     CeedEvalMode eval_mode;
871203703bSJeremy L Thompson 
88ab747706SJeremy L Thompson     CeedCall(CeedQFunctionFieldGetData(output_fields[i], &field_name, &size, &eval_mode));
891203703bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(*qf_fallback, field_name, size, eval_mode));
909e77b9c8SJeremy L Thompson   }
912b730f8bSJeremy L Thompson   CeedCall(CeedFree(&source_path_with_name));
929e77b9c8SJeremy L Thompson   return CEED_ERROR_SUCCESS;
939e77b9c8SJeremy L Thompson }
949e77b9c8SJeremy L Thompson 
959e77b9c8SJeremy L Thompson /**
96ca94c3ddSJeremy L Thompson   @brief Duplicate a `CeedOperator` with a reference `Ceed` to fallback for advanced `CeedOperator` functionality
97eaf62fffSJeremy L Thompson 
98ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to create fallback for
99eaf62fffSJeremy L Thompson 
100eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
101eaf62fffSJeremy L Thompson 
102eaf62fffSJeremy L Thompson   @ref Developer
103eaf62fffSJeremy L Thompson **/
104d04bbc78SJeremy L Thompson static int CeedOperatorCreateFallback(CeedOperator op) {
1051c66c397SJeremy L Thompson   bool         is_composite;
1061203703bSJeremy L Thompson   Ceed         ceed, ceed_fallback;
1071c66c397SJeremy L Thompson   CeedOperator op_fallback;
108eaf62fffSJeremy L Thompson 
109805fe78eSJeremy L Thompson   // Check not already created
110805fe78eSJeremy L Thompson   if (op->op_fallback) return CEED_ERROR_SUCCESS;
111805fe78eSJeremy L Thompson 
112eaf62fffSJeremy L Thompson   // Fallback Ceed
1131203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1141203703bSJeremy L Thompson   CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
115d04bbc78SJeremy L Thompson   if (!ceed_fallback) return CEED_ERROR_SUCCESS;
116d04bbc78SJeremy L Thompson 
1171203703bSJeremy L Thompson   CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n");
1181203703bSJeremy L Thompson   CeedDebug(ceed, "Creating fallback CeedOperator\n");
119eaf62fffSJeremy L Thompson 
120eaf62fffSJeremy L Thompson   // Clone Op
121b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
122b275c451SJeremy L Thompson   if (is_composite) {
123b275c451SJeremy L Thompson     CeedInt       num_suboperators;
124b275c451SJeremy L Thompson     CeedOperator *sub_operators;
125b275c451SJeremy L Thompson 
1262b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorCreate(ceed_fallback, &op_fallback));
127b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
128b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
129b275c451SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
130d04bbc78SJeremy L Thompson       CeedOperator op_sub_fallback;
131d04bbc78SJeremy L Thompson 
132b275c451SJeremy L Thompson       CeedCall(CeedOperatorGetFallback(sub_operators[i], &op_sub_fallback));
1332b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorAddSub(op_fallback, op_sub_fallback));
134805fe78eSJeremy L Thompson     }
135805fe78eSJeremy L Thompson   } else {
1361203703bSJeremy L Thompson     CeedInt            num_input_fields, num_output_fields;
1379e77b9c8SJeremy L Thompson     CeedQFunction      qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL;
1381203703bSJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
1391c66c397SJeremy L Thompson 
1402b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback));
1412b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback));
1422b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback));
1432b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback));
1441203703bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1451203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1466f8994e9SJeremy L Thompson       const char         *field_name;
1471203703bSJeremy L Thompson       CeedVector          vec;
1481203703bSJeremy L Thompson       CeedElemRestriction rstr;
1491203703bSJeremy L Thompson       CeedBasis           basis;
1501203703bSJeremy L Thompson 
151ab747706SJeremy L Thompson       CeedCall(CeedOperatorFieldGetData(input_fields[i], &field_name, &rstr, &basis, &vec));
1521203703bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec));
153805fe78eSJeremy L Thompson     }
1541203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1556f8994e9SJeremy L Thompson       const char         *field_name;
1561203703bSJeremy L Thompson       CeedVector          vec;
1571203703bSJeremy L Thompson       CeedElemRestriction rstr;
1581203703bSJeremy L Thompson       CeedBasis           basis;
1591203703bSJeremy L Thompson 
160ab747706SJeremy L Thompson       CeedCall(CeedOperatorFieldGetData(output_fields[i], &field_name, &rstr, &basis, &vec));
1611203703bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec));
162805fe78eSJeremy L Thompson     }
163*7d5185d7SSebastian Grimberg     {
164*7d5185d7SSebastian Grimberg       CeedQFunctionAssemblyData data;
165*7d5185d7SSebastian Grimberg 
166*7d5185d7SSebastian Grimberg       CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
167*7d5185d7SSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataReferenceCopy(data, &op_fallback->qf_assembled));
168*7d5185d7SSebastian Grimberg     }
1699e77b9c8SJeremy L Thompson     // Cleanup
1702b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf_fallback));
1712b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqf_fallback));
1722b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqfT_fallback));
173805fe78eSJeremy L Thompson   }
1742b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetName(op_fallback, op->name));
1752b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fallback));
176b05f7e9fSJeremy L Thompson   // Note: No ref-counting here so we don't get caught in a reference loop.
177b05f7e9fSJeremy L Thompson   //       The op holds the only reference to op_fallback and is responsible for deleting itself and op_fallback.
178805fe78eSJeremy L Thompson   op->op_fallback                 = op_fallback;
179b05f7e9fSJeremy L Thompson   op_fallback->op_fallback_parent = op;
180eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
181eaf62fffSJeremy L Thompson }
182eaf62fffSJeremy L Thompson 
183eaf62fffSJeremy L Thompson /**
184eaf62fffSJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
185eaf62fffSJeremy L Thompson 
1860cd9fdf4SJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble diagonal or point block diagonal
187ca94c3ddSJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
188bd83916cSSebastian Grimberg   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
189ca94c3ddSJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
190eaf62fffSJeremy L Thompson 
191eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
192eaf62fffSJeremy L Thompson 
193eaf62fffSJeremy L Thompson   @ref Developer
194eaf62fffSJeremy L Thompson **/
1950cd9fdf4SJeremy L Thompson static inline int CeedSingleOperatorLinearAssembleAddDiagonal_Mesh(CeedOperator op, CeedRequest *request, const bool is_point_block,
196f3bd9308SJeremy L Thompson                                                                    CeedVector assembled) {
197eaf62fffSJeremy L Thompson   Ceed ceed;
198506b1a0cSSebastian Grimberg   bool is_composite;
199506b1a0cSSebastian Grimberg 
200506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetCeed(op, &ceed));
201506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorIsComposite(op, &is_composite));
202506b1a0cSSebastian Grimberg   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
203506b1a0cSSebastian Grimberg 
204506b1a0cSSebastian Grimberg   // Assemble QFunction
205506b1a0cSSebastian Grimberg   CeedInt             layout_qf[3];
206437c7c90SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
207c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
208c5f45aeaSJeremy L Thompson   CeedElemRestriction assembled_elem_rstr = NULL;
209437c7c90SJeremy L Thompson 
210437c7c90SJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request));
21156c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf));
212437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
213437c7c90SJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
214eaf62fffSJeremy L Thompson 
215ed9e99e6SJeremy L Thompson   // Get assembly data
216437c7c90SJeremy L Thompson   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
217506b1a0cSSebastian Grimberg   CeedInt                  num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out;
218437c7c90SJeremy L Thompson   CeedSize               **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components;
219506b1a0cSSebastian Grimberg   CeedBasis               *active_bases_in, *active_bases_out;
220506b1a0cSSebastian Grimberg   CeedElemRestriction     *active_elem_rstrs_in, *active_elem_rstrs_out;
2211c66c397SJeremy L Thompson   CeedOperatorAssemblyData data;
2221c66c397SJeremy L Thompson 
223437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
224506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in,
225506b1a0cSSebastian Grimberg                                                 &num_active_bases_out, &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out,
226506b1a0cSSebastian Grimberg                                                 &num_output_components));
227506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, NULL, NULL, &active_bases_out, NULL));
228506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs_in, NULL, &active_elem_rstrs_out));
229506b1a0cSSebastian Grimberg 
230934a29f5SSebastian Grimberg   // Loop over all active bases (find matching input/output pairs)
231934a29f5SSebastian Grimberg   for (CeedInt b = 0; b < CeedIntMin(num_active_bases_in, num_active_bases_out); b++) {
232934a29f5SSebastian Grimberg     CeedInt             b_in, b_out, num_elem, num_nodes, num_qpts, num_comp;
2331c66c397SJeremy L Thompson     bool                has_eval_none = false;
2341c66c397SJeremy L Thompson     CeedScalar         *elem_diag_array, *identity = NULL;
2351c66c397SJeremy L Thompson     CeedVector          elem_diag;
2367c1dbaffSSebastian Grimberg     CeedElemRestriction diag_elem_rstr;
2371c66c397SJeremy L Thompson 
238934a29f5SSebastian Grimberg     if (num_active_bases_in <= num_active_bases_out) {
239934a29f5SSebastian Grimberg       b_in = b;
240934a29f5SSebastian Grimberg       for (b_out = 0; b_out < num_active_bases_out; b_out++) {
241934a29f5SSebastian Grimberg         if (active_bases_in[b_in] == active_bases_out[b_out]) {
242934a29f5SSebastian Grimberg           break;
243934a29f5SSebastian Grimberg         }
244934a29f5SSebastian Grimberg       }
245934a29f5SSebastian Grimberg       if (b_out == num_active_bases_out) {
246934a29f5SSebastian Grimberg         continue;
247934a29f5SSebastian Grimberg       }  // No matching output basis found
248934a29f5SSebastian Grimberg     } else {
249934a29f5SSebastian Grimberg       b_out = b;
250934a29f5SSebastian Grimberg       for (b_in = 0; b_in < num_active_bases_in; b_in++) {
251934a29f5SSebastian Grimberg         if (active_bases_in[b_in] == active_bases_out[b_out]) {
252934a29f5SSebastian Grimberg           break;
253934a29f5SSebastian Grimberg         }
254934a29f5SSebastian Grimberg       }
255934a29f5SSebastian Grimberg       if (b_in == num_active_bases_in) {
256934a29f5SSebastian Grimberg         continue;
257934a29f5SSebastian Grimberg       }  // No matching output basis found
258934a29f5SSebastian Grimberg     }
259934a29f5SSebastian Grimberg     CeedCheck(active_elem_rstrs_in[b_in] == active_elem_rstrs_out[b_out], ceed, CEED_ERROR_UNSUPPORTED,
260506b1a0cSSebastian Grimberg               "Cannot assemble operator diagonal with different input and output active element restrictions");
261506b1a0cSSebastian Grimberg 
2621c66c397SJeremy L Thompson     // Assemble point block diagonal restriction, if needed
263bd83916cSSebastian Grimberg     if (is_point_block) {
264934a29f5SSebastian Grimberg       CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstrs_in[b_in], &diag_elem_rstr));
2657c1dbaffSSebastian Grimberg     } else {
266934a29f5SSebastian Grimberg       CeedCall(CeedElemRestrictionCreateUnsignedCopy(active_elem_rstrs_in[b_in], &diag_elem_rstr));
267eaf62fffSJeremy L Thompson     }
268eaf62fffSJeremy L Thompson 
269eaf62fffSJeremy L Thompson     // Create diagonal vector
270437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag));
271eaf62fffSJeremy L Thompson 
272eaf62fffSJeremy L Thompson     // Assemble element operator diagonals
2732b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(elem_diag, 0.0));
2742b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array));
275437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem));
276934a29f5SSebastian Grimberg     CeedCall(CeedBasisGetNumNodes(active_bases_in[b_in], &num_nodes));
277934a29f5SSebastian Grimberg     CeedCall(CeedBasisGetNumComponents(active_bases_in[b_in], &num_comp));
278934a29f5SSebastian Grimberg     if (active_bases_in[b_in] == CEED_BASIS_NONE) num_qpts = num_nodes;
279934a29f5SSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(active_bases_in[b_in], &num_qpts));
280ed9e99e6SJeremy L Thompson 
281352a5e7cSSebastian Grimberg     // Construct identity matrix for basis if required
282934a29f5SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in[b_in]; i++) {
283934a29f5SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[b_in][i] == CEED_EVAL_NONE);
284ed9e99e6SJeremy L Thompson     }
285934a29f5SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out[b_out]; i++) {
286934a29f5SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[b_out][i] == CEED_EVAL_NONE);
287ed9e99e6SJeremy L Thompson     }
288ed9e99e6SJeremy L Thompson     if (has_eval_none) {
2892b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
2902b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
291eaf62fffSJeremy L Thompson     }
292352a5e7cSSebastian Grimberg 
293eaf62fffSJeremy L Thompson     // Compute the diagonal of B^T D B
294eaf62fffSJeremy L Thompson     // Each element
295b94338b9SJed Brown     for (CeedSize e = 0; e < num_elem; e++) {
296eaf62fffSJeremy L Thompson       // Each basis eval mode pair
297352a5e7cSSebastian Grimberg       CeedInt      d_out              = 0, q_comp_out;
298352a5e7cSSebastian Grimberg       CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
2991c66c397SJeremy L Thompson 
300934a29f5SSebastian Grimberg       for (CeedInt e_out = 0; e_out < num_eval_modes_out[b_out]; e_out++) {
3011c66c397SJeremy L Thompson         CeedInt           d_in              = 0, q_comp_in;
302437c7c90SJeremy L Thompson         const CeedScalar *B_t               = NULL;
3031c66c397SJeremy L Thompson         CeedEvalMode      eval_mode_in_prev = CEED_EVAL_NONE;
3041c66c397SJeremy L Thompson 
305934a29f5SSebastian Grimberg         CeedCall(CeedOperatorGetBasisPointer(active_bases_out[b_out], eval_modes_out[b_out][e_out], identity, &B_t));
306934a29f5SSebastian Grimberg         CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_out[b_out], eval_modes_out[b_out][e_out], &q_comp_out));
307352a5e7cSSebastian Grimberg         if (q_comp_out > 1) {
308934a29f5SSebastian Grimberg           if (e_out == 0 || eval_modes_out[b_out][e_out] != eval_mode_out_prev) d_out = 0;
309352a5e7cSSebastian Grimberg           else B_t = &B_t[(++d_out) * num_qpts * num_nodes];
310352a5e7cSSebastian Grimberg         }
311934a29f5SSebastian Grimberg         eval_mode_out_prev = eval_modes_out[b_out][e_out];
312352a5e7cSSebastian Grimberg 
313934a29f5SSebastian Grimberg         for (CeedInt e_in = 0; e_in < num_eval_modes_in[b_in]; e_in++) {
314437c7c90SJeremy L Thompson           const CeedScalar *B = NULL;
3151c66c397SJeremy L Thompson 
316934a29f5SSebastian Grimberg           CeedCall(CeedOperatorGetBasisPointer(active_bases_in[b_in], eval_modes_in[b_in][e_in], identity, &B));
317934a29f5SSebastian Grimberg           CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_in[b_in], eval_modes_in[b_in][e_in], &q_comp_in));
318352a5e7cSSebastian Grimberg           if (q_comp_in > 1) {
319934a29f5SSebastian Grimberg             if (e_in == 0 || eval_modes_in[b_in][e_in] != eval_mode_in_prev) d_in = 0;
320352a5e7cSSebastian Grimberg             else B = &B[(++d_in) * num_qpts * num_nodes];
321352a5e7cSSebastian Grimberg           }
322934a29f5SSebastian Grimberg           eval_mode_in_prev = eval_modes_in[b_in][e_in];
323352a5e7cSSebastian Grimberg 
324eaf62fffSJeremy L Thompson           // Each component
325506b1a0cSSebastian Grimberg           for (CeedInt c_out = 0; c_out < num_comp; c_out++) {
326437c7c90SJeremy L Thompson             // Each qpt/node pair
3272b730f8bSJeremy L Thompson             for (CeedInt q = 0; q < num_qpts; q++) {
328bd83916cSSebastian Grimberg               if (is_point_block) {
329eaf62fffSJeremy L Thompson                 // Point Block Diagonal
330506b1a0cSSebastian Grimberg                 for (CeedInt c_in = 0; c_in < num_comp; c_in++) {
331934a29f5SSebastian Grimberg                   const CeedSize c_offset =
332934a29f5SSebastian Grimberg                       (eval_mode_offsets_in[b_in][e_in] + c_in) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out;
333506b1a0cSSebastian Grimberg                   const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]];
3341c66c397SJeremy L Thompson 
3352b730f8bSJeremy L Thompson                   for (CeedInt n = 0; n < num_nodes; n++) {
336506b1a0cSSebastian Grimberg                     elem_diag_array[((e * num_comp + c_out) * num_comp + c_in) * num_nodes + n] +=
337437c7c90SJeremy L Thompson                         B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n];
338eaf62fffSJeremy L Thompson                   }
3392b730f8bSJeremy L Thompson                 }
340eaf62fffSJeremy L Thompson               } else {
341eaf62fffSJeremy L Thompson                 // Diagonal Only
342934a29f5SSebastian Grimberg                 const CeedInt c_offset =
343934a29f5SSebastian Grimberg                     (eval_mode_offsets_in[b_in][e_in] + c_out) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out;
344506b1a0cSSebastian Grimberg                 const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]];
3451c66c397SJeremy L Thompson 
3462b730f8bSJeremy L Thompson                 for (CeedInt n = 0; n < num_nodes; n++) {
347506b1a0cSSebastian Grimberg                   elem_diag_array[(e * num_comp + c_out) * num_nodes + n] += B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n];
348eaf62fffSJeremy L Thompson                 }
349eaf62fffSJeremy L Thompson               }
350eaf62fffSJeremy L Thompson             }
351eaf62fffSJeremy L Thompson           }
3522b730f8bSJeremy L Thompson         }
3532b730f8bSJeremy L Thompson       }
3542b730f8bSJeremy L Thompson     }
3552b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
356eaf62fffSJeremy L Thompson 
357eaf62fffSJeremy L Thompson     // Assemble local operator diagonal
3587c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
359eaf62fffSJeremy L Thompson 
360eaf62fffSJeremy L Thompson     // Cleanup
3617c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr));
3622b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&elem_diag));
3632b730f8bSJeremy L Thompson     CeedCall(CeedFree(&identity));
364437c7c90SJeremy L Thompson   }
365437c7c90SJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
366437c7c90SJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
367eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
368eaf62fffSJeremy L Thompson }
369eaf62fffSJeremy L Thompson 
370eaf62fffSJeremy L Thompson /**
3710cd9fdf4SJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
3720cd9fdf4SJeremy L Thompson 
3730cd9fdf4SJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble diagonal or point block diagonal
3740cd9fdf4SJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
3750cd9fdf4SJeremy L Thompson   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
3760cd9fdf4SJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
3770cd9fdf4SJeremy L Thompson 
3780cd9fdf4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3790cd9fdf4SJeremy L Thompson 
3800cd9fdf4SJeremy L Thompson   @ref Developer
3810cd9fdf4SJeremy L Thompson **/
3820cd9fdf4SJeremy L Thompson static inline int CeedSingleOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block,
3830cd9fdf4SJeremy L Thompson                                                               CeedVector assembled) {
3840cd9fdf4SJeremy L Thompson   Ceed ceed;
3850cd9fdf4SJeremy L Thompson   bool is_at_points;
3860cd9fdf4SJeremy L Thompson 
3870cd9fdf4SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
3880cd9fdf4SJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
38942461424SJeremy L Thompson   CeedCheck(!is_at_points, ceed, CEED_ERROR_UNSUPPORTED, "AtPoints operator not supported");
3900cd9fdf4SJeremy L Thompson   CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal_Mesh(op, request, is_point_block, assembled));
3910cd9fdf4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3920cd9fdf4SJeremy L Thompson }
3930cd9fdf4SJeremy L Thompson 
3940cd9fdf4SJeremy L Thompson /**
395eaf62fffSJeremy L Thompson   @brief Core logic for assembling composite operator diagonal
396eaf62fffSJeremy L Thompson 
397ca94c3ddSJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble point block diagonal
398ca94c3ddSJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
399bd83916cSSebastian Grimberg   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
400ca94c3ddSJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
401eaf62fffSJeremy L Thompson 
402eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
403eaf62fffSJeremy L Thompson 
404eaf62fffSJeremy L Thompson   @ref Developer
405eaf62fffSJeremy L Thompson **/
406bd83916cSSebastian Grimberg static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block,
407eaf62fffSJeremy L Thompson                                                                  CeedVector assembled) {
408eaf62fffSJeremy L Thompson   CeedInt       num_sub;
409eaf62fffSJeremy L Thompson   CeedOperator *suboperators;
4101c66c397SJeremy L Thompson 
411c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
412c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators));
413eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_sub; i++) {
414bd83916cSSebastian Grimberg     if (is_point_block) {
4152b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request));
4166aa95790SJeremy L Thompson     } else {
4172b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request));
4186aa95790SJeremy L Thompson     }
419eaf62fffSJeremy L Thompson   }
420eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
421eaf62fffSJeremy L Thompson }
422eaf62fffSJeremy L Thompson 
423eaf62fffSJeremy L Thompson /**
424ca94c3ddSJeremy L Thompson   @brief Build nonzero pattern for non-composite CeedOperator`.
425eaf62fffSJeremy L Thompson 
426ca94c3ddSJeremy L Thompson   Users should generally use @ref CeedOperatorLinearAssembleSymbolic().
427eaf62fffSJeremy L Thompson 
428ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` to assemble nonzero pattern
429eaf62fffSJeremy L Thompson   @param[in]  offset Offset for number of entries
430eaf62fffSJeremy L Thompson   @param[out] rows   Row number for each entry
431eaf62fffSJeremy L Thompson   @param[out] cols   Column number for each entry
432eaf62fffSJeremy L Thompson 
433eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
434eaf62fffSJeremy L Thompson 
435eaf62fffSJeremy L Thompson   @ref Developer
436eaf62fffSJeremy L Thompson **/
4372b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) {
438f3d47e36SJeremy L Thompson   Ceed                ceed;
439f3d47e36SJeremy L Thompson   bool                is_composite;
44081670346SSebastian Grimberg   CeedSize            num_nodes_in, num_nodes_out, local_num_entries, count = 0;
441506b1a0cSSebastian Grimberg   CeedInt             num_elem_in, elem_size_in, num_comp_in, layout_er_in[3];
44281670346SSebastian Grimberg   CeedInt             num_elem_out, elem_size_out, num_comp_out, layout_er_out[3];
4431c66c397SJeremy L Thompson   CeedScalar         *array;
444506b1a0cSSebastian Grimberg   const CeedScalar   *elem_dof_a_in, *elem_dof_a_out;
445506b1a0cSSebastian Grimberg   CeedVector          index_vec_in, index_vec_out, elem_dof_in, elem_dof_out;
446506b1a0cSSebastian Grimberg   CeedElemRestriction elem_rstr_in, elem_rstr_out, index_elem_rstr_in, index_elem_rstr_out;
4471c66c397SJeremy L Thompson 
448f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
449f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4506574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
451eaf62fffSJeremy L Thompson 
452506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes_in, &num_nodes_out));
453506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out));
454506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in));
455506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in));
456506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in));
45756c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(elem_rstr_in, layout_er_in));
458eaf62fffSJeremy L Thompson 
459506b1a0cSSebastian Grimberg   // Determine elem_dof relation for input
460506b1a0cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed, num_nodes_in, &index_vec_in));
461506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayWrite(index_vec_in, CEED_MEM_HOST, &array));
462506b1a0cSSebastian Grimberg   for (CeedInt i = 0; i < num_nodes_in; i++) array[i] = i;
463506b1a0cSSebastian Grimberg   CeedCall(CeedVectorRestoreArray(index_vec_in, &array));
464506b1a0cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed, num_elem_in * elem_size_in * num_comp_in, &elem_dof_in));
465506b1a0cSSebastian Grimberg   CeedCall(CeedVectorSetValue(elem_dof_in, 0.0));
466506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_in, &index_elem_rstr_in));
467506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionApply(index_elem_rstr_in, CEED_NOTRANSPOSE, index_vec_in, elem_dof_in, CEED_REQUEST_IMMEDIATE));
468506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayRead(elem_dof_in, CEED_MEM_HOST, &elem_dof_a_in));
469506b1a0cSSebastian Grimberg   CeedCall(CeedVectorDestroy(&index_vec_in));
470506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_in));
471506b1a0cSSebastian Grimberg 
472506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
473506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out));
474506b1a0cSSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
475506b1a0cSSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
476506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out));
477506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out));
47856c48462SJeremy L Thompson     CeedCall(CeedElemRestrictionGetELayout(elem_rstr_out, layout_er_out));
479506b1a0cSSebastian Grimberg 
480506b1a0cSSebastian Grimberg     // Determine elem_dof relation for output
481506b1a0cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed, num_nodes_out, &index_vec_out));
482506b1a0cSSebastian Grimberg     CeedCall(CeedVectorGetArrayWrite(index_vec_out, CEED_MEM_HOST, &array));
483506b1a0cSSebastian Grimberg     for (CeedInt i = 0; i < num_nodes_out; i++) array[i] = i;
484506b1a0cSSebastian Grimberg     CeedCall(CeedVectorRestoreArray(index_vec_out, &array));
485506b1a0cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed, num_elem_out * elem_size_out * num_comp_out, &elem_dof_out));
486506b1a0cSSebastian Grimberg     CeedCall(CeedVectorSetValue(elem_dof_out, 0.0));
487506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_out, &index_elem_rstr_out));
488506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(index_elem_rstr_out, CEED_NOTRANSPOSE, index_vec_out, elem_dof_out, CEED_REQUEST_IMMEDIATE));
489506b1a0cSSebastian Grimberg     CeedCall(CeedVectorGetArrayRead(elem_dof_out, CEED_MEM_HOST, &elem_dof_a_out));
490506b1a0cSSebastian Grimberg     CeedCall(CeedVectorDestroy(&index_vec_out));
491506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_out));
492506b1a0cSSebastian Grimberg   } else {
493506b1a0cSSebastian Grimberg     num_elem_out     = num_elem_in;
494506b1a0cSSebastian Grimberg     elem_size_out    = elem_size_in;
495506b1a0cSSebastian Grimberg     num_comp_out     = num_comp_in;
496506b1a0cSSebastian Grimberg     layout_er_out[0] = layout_er_in[0];
497506b1a0cSSebastian Grimberg     layout_er_out[1] = layout_er_in[1];
498506b1a0cSSebastian Grimberg     layout_er_out[2] = layout_er_in[2];
499506b1a0cSSebastian Grimberg     elem_dof_a_out   = elem_dof_a_in;
500506b1a0cSSebastian Grimberg   }
501506b1a0cSSebastian Grimberg   local_num_entries = elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in;
502eaf62fffSJeremy L Thompson 
503eaf62fffSJeremy L Thompson   // Determine i, j locations for element matrices
504506b1a0cSSebastian Grimberg   for (CeedInt e = 0; e < num_elem_in; e++) {
505506b1a0cSSebastian Grimberg     for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) {
506506b1a0cSSebastian Grimberg       for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) {
507506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < elem_size_out; i++) {
508506b1a0cSSebastian Grimberg           for (CeedInt j = 0; j < elem_size_in; j++) {
509506b1a0cSSebastian Grimberg             const CeedInt elem_dof_index_row = i * layout_er_out[0] + comp_out * layout_er_out[1] + e * layout_er_out[2];
510506b1a0cSSebastian Grimberg             const CeedInt elem_dof_index_col = j * layout_er_in[0] + comp_in * layout_er_in[1] + e * layout_er_in[2];
511506b1a0cSSebastian Grimberg             const CeedInt row                = elem_dof_a_out[elem_dof_index_row];
512506b1a0cSSebastian Grimberg             const CeedInt col                = elem_dof_a_in[elem_dof_index_col];
513eaf62fffSJeremy L Thompson 
514eaf62fffSJeremy L Thompson             rows[offset + count] = row;
515eaf62fffSJeremy L Thompson             cols[offset + count] = col;
516eaf62fffSJeremy L Thompson             count++;
517eaf62fffSJeremy L Thompson           }
518eaf62fffSJeremy L Thompson         }
519eaf62fffSJeremy L Thompson       }
520eaf62fffSJeremy L Thompson     }
521eaf62fffSJeremy L Thompson   }
5226574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing assembled entries");
523506b1a0cSSebastian Grimberg   CeedCall(CeedVectorRestoreArrayRead(elem_dof_in, &elem_dof_a_in));
524506b1a0cSSebastian Grimberg   CeedCall(CeedVectorDestroy(&elem_dof_in));
525506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
526506b1a0cSSebastian Grimberg     CeedCall(CeedVectorRestoreArrayRead(elem_dof_out, &elem_dof_a_out));
527506b1a0cSSebastian Grimberg     CeedCall(CeedVectorDestroy(&elem_dof_out));
528506b1a0cSSebastian Grimberg   }
529eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
530eaf62fffSJeremy L Thompson }
531eaf62fffSJeremy L Thompson 
532eaf62fffSJeremy L Thompson /**
533ca94c3ddSJeremy L Thompson   @brief Assemble nonzero entries for non-composite `CeedOperator`.
534eaf62fffSJeremy L Thompson 
535ca94c3ddSJeremy L Thompson   Users should generally use @ref CeedOperatorLinearAssemble().
536eaf62fffSJeremy L Thompson 
537ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` to assemble
538ea61e9acSJeremy L Thompson   @param[in]  offset Offset for number of entries
539eaf62fffSJeremy L Thompson   @param[out] values Values to assemble into matrix
540eaf62fffSJeremy L Thompson 
541eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
542eaf62fffSJeremy L Thompson 
543eaf62fffSJeremy L Thompson   @ref Developer
544eaf62fffSJeremy L Thompson **/
5452b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) {
546f3d47e36SJeremy L Thompson   Ceed ceed;
547f3d47e36SJeremy L Thompson   bool is_composite;
5481c66c397SJeremy L Thompson 
549f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
550f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
5516574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
552f3d47e36SJeremy L Thompson 
553f3d47e36SJeremy L Thompson   // Early exit for empty operator
554f3d47e36SJeremy L Thompson   {
555f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
556f3d47e36SJeremy L Thompson 
557f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
558f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
559f3d47e36SJeremy L Thompson   }
560eaf62fffSJeremy L Thompson 
561cefa2673SJeremy L Thompson   if (op->LinearAssembleSingle) {
562cefa2673SJeremy L Thompson     // Backend version
5632b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSingle(op, offset, values));
564cefa2673SJeremy L Thompson     return CEED_ERROR_SUCCESS;
565cefa2673SJeremy L Thompson   } else {
566cefa2673SJeremy L Thompson     // Operator fallback
567cefa2673SJeremy L Thompson     CeedOperator op_fallback;
568cefa2673SJeremy L Thompson 
5692b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
570cefa2673SJeremy L Thompson     if (op_fallback) {
5712b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values));
572cefa2673SJeremy L Thompson       return CEED_ERROR_SUCCESS;
573cefa2673SJeremy L Thompson     }
574cefa2673SJeremy L Thompson   }
575cefa2673SJeremy L Thompson 
576eaf62fffSJeremy L Thompson   // Assemble QFunction
577506b1a0cSSebastian Grimberg   CeedInt             layout_qf[3];
5781c66c397SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
579c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
580506b1a0cSSebastian Grimberg   CeedElemRestriction assembled_elem_rstr = NULL;
581eaf62fffSJeremy L Thompson 
582506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, CEED_REQUEST_IMMEDIATE));
58356c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf));
584506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
585506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
586eaf62fffSJeremy L Thompson 
587ed9e99e6SJeremy L Thompson   // Get assembly data
588506b1a0cSSebastian Grimberg   CeedInt                  num_elem_in, elem_size_in, num_comp_in, num_qpts_in;
58981670346SSebastian Grimberg   CeedInt                  num_elem_out, elem_size_out, num_comp_out, num_qpts_out;
59081670346SSebastian Grimberg   CeedSize                 local_num_entries, count = 0;
591506b1a0cSSebastian Grimberg   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
592506b1a0cSSebastian Grimberg   CeedInt                  num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out;
593506b1a0cSSebastian Grimberg   CeedBasis               *active_bases_in, *active_bases_out, basis_in, basis_out;
594506b1a0cSSebastian Grimberg   const CeedScalar       **B_mats_in, **B_mats_out, *B_mat_in, *B_mat_out;
595506b1a0cSSebastian Grimberg   CeedElemRestriction      elem_rstr_in, elem_rstr_out;
596506b1a0cSSebastian Grimberg   CeedRestrictionType      elem_rstr_type_in, elem_rstr_type_out;
597506b1a0cSSebastian Grimberg   const bool              *elem_rstr_orients_in = NULL, *elem_rstr_orients_out = NULL;
598506b1a0cSSebastian Grimberg   const CeedInt8          *elem_rstr_curl_orients_in = NULL, *elem_rstr_curl_orients_out = NULL;
599506b1a0cSSebastian Grimberg   CeedOperatorAssemblyData data;
600eaf62fffSJeremy L Thompson 
601506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
602506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, NULL, &num_active_bases_out,
603506b1a0cSSebastian Grimberg                                                 &num_eval_modes_out, &eval_modes_out, NULL, NULL));
604506b1a0cSSebastian Grimberg 
605506b1a0cSSebastian Grimberg   CeedCheck(num_active_bases_in == num_active_bases_out && num_active_bases_in == 1, ceed, CEED_ERROR_UNSUPPORTED,
606506b1a0cSSebastian Grimberg             "Cannot assemble operator with multiple active bases");
6076574a04fSJeremy L Thompson   CeedCheck(num_eval_modes_in[0] > 0 && num_eval_modes_out[0] > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
608eaf62fffSJeremy L Thompson 
609506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, &B_mats_in, NULL, &active_bases_out, &B_mats_out));
610506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out));
611506b1a0cSSebastian Grimberg   basis_in  = active_bases_in[0];
612506b1a0cSSebastian Grimberg   basis_out = active_bases_out[0];
613506b1a0cSSebastian Grimberg   B_mat_in  = B_mats_in[0];
614506b1a0cSSebastian Grimberg   B_mat_out = B_mats_out[0];
615eaf62fffSJeremy L Thompson 
616506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in));
617506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in));
618506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in));
619506b1a0cSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
620506b1a0cSSebastian Grimberg   else CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
621506b1a0cSSebastian Grimberg 
622506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetType(elem_rstr_in, &elem_rstr_type_in));
623506b1a0cSSebastian Grimberg   if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) {
624506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_orients_in));
625506b1a0cSSebastian Grimberg   } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
626506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_curl_orients_in));
6277c1dbaffSSebastian Grimberg   }
6287c1dbaffSSebastian Grimberg 
629506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
630506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out));
631506b1a0cSSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
632506b1a0cSSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
633506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out));
634506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out));
635506b1a0cSSebastian Grimberg     if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
636506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
637506b1a0cSSebastian Grimberg     CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
638506b1a0cSSebastian Grimberg               "Active input and output bases must have the same number of quadrature points");
639eaf62fffSJeremy L Thompson 
640506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(elem_rstr_out, &elem_rstr_type_out));
641506b1a0cSSebastian Grimberg     if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) {
642506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_orients_out));
643506b1a0cSSebastian Grimberg     } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
644506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_curl_orients_out));
645506b1a0cSSebastian Grimberg     }
646506b1a0cSSebastian Grimberg   } else {
647506b1a0cSSebastian Grimberg     num_elem_out  = num_elem_in;
648506b1a0cSSebastian Grimberg     elem_size_out = elem_size_in;
649506b1a0cSSebastian Grimberg     num_comp_out  = num_comp_in;
650506b1a0cSSebastian Grimberg     num_qpts_out  = num_qpts_in;
651506b1a0cSSebastian Grimberg 
652506b1a0cSSebastian Grimberg     elem_rstr_orients_out      = elem_rstr_orients_in;
653506b1a0cSSebastian Grimberg     elem_rstr_curl_orients_out = elem_rstr_curl_orients_in;
654506b1a0cSSebastian Grimberg   }
655506b1a0cSSebastian Grimberg   local_num_entries = elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in;
656506b1a0cSSebastian Grimberg 
657506b1a0cSSebastian Grimberg   // Loop over elements and put in data structure
6587c1dbaffSSebastian Grimberg   // We store B_mat_in, B_mat_out, BTD, elem_mat in row-major order
6590459ebd3SSebastian Grimberg   CeedTensorContract contract;
660123d890dSSebastian Grimberg   CeedScalar        *vals, *BTD_mat = NULL, *elem_mat = NULL, *elem_mat_b = NULL;
661506b1a0cSSebastian Grimberg 
662c22497adSSebastian Grimberg   CeedCall(CeedBasisGetTensorContract(basis_in, &contract));
663123d890dSSebastian Grimberg   CeedCall(CeedCalloc(elem_size_out * num_qpts_in * num_eval_modes_in[0], &BTD_mat));
664123d890dSSebastian Grimberg   CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat));
665506b1a0cSSebastian Grimberg   if (elem_rstr_curl_orients_in || elem_rstr_curl_orients_out) CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat_b));
6661c66c397SJeremy L Thompson 
66728ec399dSJeremy L Thompson   CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals));
668506b1a0cSSebastian Grimberg   for (CeedSize e = 0; e < num_elem_in; e++) {
669506b1a0cSSebastian Grimberg     for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) {
670506b1a0cSSebastian Grimberg       for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) {
671ed9e99e6SJeremy L Thompson         // Compute B^T*D
672506b1a0cSSebastian Grimberg         for (CeedSize n = 0; n < elem_size_out; n++) {
673506b1a0cSSebastian Grimberg           for (CeedSize q = 0; q < num_qpts_in; q++) {
674437c7c90SJeremy L Thompson             for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) {
675506b1a0cSSebastian Grimberg               const CeedSize btd_index = n * (num_qpts_in * num_eval_modes_in[0]) + q * num_eval_modes_in[0] + e_in;
676067fd99fSJeremy L Thompson               CeedScalar     sum       = 0.0;
6771c66c397SJeremy L Thompson 
678437c7c90SJeremy L Thompson               for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) {
679506b1a0cSSebastian Grimberg                 const CeedSize b_out_index     = (q * num_eval_modes_out[0] + e_out) * elem_size_out + n;
680506b1a0cSSebastian Grimberg                 const CeedSize eval_mode_index = ((e_in * num_comp_in + comp_in) * num_eval_modes_out[0] + e_out) * num_comp_out + comp_out;
681b94338b9SJed Brown                 const CeedSize qf_index        = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2];
6821c66c397SJeremy L Thompson 
683067fd99fSJeremy L Thompson                 sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index];
684eaf62fffSJeremy L Thompson               }
685067fd99fSJeremy L Thompson               BTD_mat[btd_index] = sum;
686ed9e99e6SJeremy L Thompson             }
687ed9e99e6SJeremy L Thompson           }
688eaf62fffSJeremy L Thompson         }
6897c1dbaffSSebastian Grimberg 
6907c1dbaffSSebastian Grimberg         // Form element matrix itself (for each block component)
691e4065a52SSebastian Grimberg         if (contract) {
6920459ebd3SSebastian Grimberg           CeedCall(CeedTensorContractApply(contract, 1, num_qpts_in * num_eval_modes_in[0], elem_size_in, elem_size_out, BTD_mat, CEED_NOTRANSPOSE,
6930459ebd3SSebastian Grimberg                                            false, B_mat_in, elem_mat));
694e4065a52SSebastian Grimberg         } else {
695e4065a52SSebastian Grimberg           CeedCall(CeedMatrixMatrixMultiply(ceed, BTD_mat, B_mat_in, elem_mat, elem_size_out, elem_size_in, num_qpts_in * num_eval_modes_in[0]));
696e4065a52SSebastian Grimberg         }
697eaf62fffSJeremy L Thompson 
6987c1dbaffSSebastian Grimberg         // Transform the element matrix if required
699506b1a0cSSebastian Grimberg         if (elem_rstr_orients_out) {
700506b1a0cSSebastian Grimberg           const bool *elem_orients = &elem_rstr_orients_out[e * elem_size_out];
7011c66c397SJeremy L Thompson 
702506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
703506b1a0cSSebastian Grimberg             const double orient = elem_orients[i] ? -1.0 : 1.0;
704506b1a0cSSebastian Grimberg 
705506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
706506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] *= orient;
7077c1dbaffSSebastian Grimberg             }
7087c1dbaffSSebastian Grimberg           }
709506b1a0cSSebastian Grimberg         } else if (elem_rstr_curl_orients_out) {
710506b1a0cSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_out[e * 3 * elem_size_out];
7111c66c397SJeremy L Thompson 
7127c1dbaffSSebastian Grimberg           // T^T*(B^T*D*B)
713506b1a0cSSebastian Grimberg           memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar));
714506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
715506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
716506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * i + 1] +
717506b1a0cSSebastian Grimberg                                                (i > 0 ? elem_mat_b[(i - 1) * elem_size_in + j] * elem_curl_orients[3 * i - 1] : 0.0) +
718506b1a0cSSebastian Grimberg                                                (i < elem_size_out - 1 ? elem_mat_b[(i + 1) * elem_size_in + j] * elem_curl_orients[3 * i + 3] : 0.0);
7197c1dbaffSSebastian Grimberg             }
7207c1dbaffSSebastian Grimberg           }
721506b1a0cSSebastian Grimberg         }
722506b1a0cSSebastian Grimberg         if (elem_rstr_orients_in) {
723506b1a0cSSebastian Grimberg           const bool *elem_orients = &elem_rstr_orients_in[e * elem_size_in];
724506b1a0cSSebastian Grimberg 
725506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
726506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
727506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] *= elem_orients[j] ? -1.0 : 1.0;
728506b1a0cSSebastian Grimberg             }
729506b1a0cSSebastian Grimberg           }
730506b1a0cSSebastian Grimberg         } else if (elem_rstr_curl_orients_in) {
731506b1a0cSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_in[e * 3 * elem_size_in];
732506b1a0cSSebastian Grimberg 
733506b1a0cSSebastian Grimberg           // (B^T*D*B)*T
734506b1a0cSSebastian Grimberg           memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar));
735506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
736506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
737506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * j + 1] +
738506b1a0cSSebastian Grimberg                                                (j > 0 ? elem_mat_b[i * elem_size_in + j - 1] * elem_curl_orients[3 * j - 1] : 0.0) +
739506b1a0cSSebastian Grimberg                                                (j < elem_size_in - 1 ? elem_mat_b[i * elem_size_in + j + 1] * elem_curl_orients[3 * j + 3] : 0.0);
7407c1dbaffSSebastian Grimberg             }
7417c1dbaffSSebastian Grimberg           }
7427c1dbaffSSebastian Grimberg         }
7437c1dbaffSSebastian Grimberg 
7447c1dbaffSSebastian Grimberg         // Put element matrix in coordinate data structure
745506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < elem_size_out; i++) {
746506b1a0cSSebastian Grimberg           for (CeedInt j = 0; j < elem_size_in; j++) {
747506b1a0cSSebastian Grimberg             vals[offset + count] = elem_mat[i * elem_size_in + j];
748eaf62fffSJeremy L Thompson             count++;
749eaf62fffSJeremy L Thompson           }
750eaf62fffSJeremy L Thompson         }
751eaf62fffSJeremy L Thompson       }
752eaf62fffSJeremy L Thompson     }
753eaf62fffSJeremy L Thompson   }
7546574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing entries");
7552b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(values, &vals));
756eaf62fffSJeremy L Thompson 
757506b1a0cSSebastian Grimberg   // Cleanup
758123d890dSSebastian Grimberg   CeedCall(CeedFree(&BTD_mat));
759123d890dSSebastian Grimberg   CeedCall(CeedFree(&elem_mat));
760506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&elem_mat_b));
761506b1a0cSSebastian Grimberg   if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) {
762506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_in, &elem_rstr_orients_in));
763506b1a0cSSebastian Grimberg   } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
764506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_in, &elem_rstr_curl_orients_in));
765506b1a0cSSebastian Grimberg   }
766506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
767506b1a0cSSebastian Grimberg     if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) {
768506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_out, &elem_rstr_orients_out));
769506b1a0cSSebastian Grimberg     } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
770506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_out, &elem_rstr_curl_orients_out));
771506b1a0cSSebastian Grimberg     }
772506b1a0cSSebastian Grimberg   }
7732b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
7742b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
775eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
776eaf62fffSJeremy L Thompson }
777eaf62fffSJeremy L Thompson 
778eaf62fffSJeremy L Thompson /**
779ca94c3ddSJeremy L Thompson   @brief Count number of entries for assembled `CeedOperator`
780eaf62fffSJeremy L Thompson 
781ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to assemble
782eaf62fffSJeremy L Thompson   @param[out] num_entries Number of entries in assembled representation
783eaf62fffSJeremy L Thompson 
784eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
785eaf62fffSJeremy L Thompson 
786eaf62fffSJeremy L Thompson   @ref Utility
787eaf62fffSJeremy L Thompson **/
788b94338b9SJed Brown static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedSize *num_entries) {
789b275c451SJeremy L Thompson   bool                is_composite;
790506b1a0cSSebastian Grimberg   CeedInt             num_elem_in, elem_size_in, num_comp_in, num_elem_out, elem_size_out, num_comp_out;
7911203703bSJeremy L Thompson   Ceed                ceed;
792506b1a0cSSebastian Grimberg   CeedElemRestriction rstr_in, rstr_out;
793eaf62fffSJeremy L Thompson 
7941203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
795b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
7961203703bSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
797506b1a0cSSebastian Grimberg 
798506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
799506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
800506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
801506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
802506b1a0cSSebastian Grimberg   if (rstr_in != rstr_out) {
803506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
8041203703bSJeremy L Thompson     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
805506b1a0cSSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
806506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
807506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
808506b1a0cSSebastian Grimberg   } else {
809506b1a0cSSebastian Grimberg     num_elem_out  = num_elem_in;
810506b1a0cSSebastian Grimberg     elem_size_out = elem_size_in;
811506b1a0cSSebastian Grimberg     num_comp_out  = num_comp_in;
812506b1a0cSSebastian Grimberg   }
813506b1a0cSSebastian Grimberg   *num_entries = (CeedSize)elem_size_in * num_comp_in * elem_size_out * num_comp_out * num_elem_in;
814eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
815eaf62fffSJeremy L Thompson }
816eaf62fffSJeremy L Thompson 
817eaf62fffSJeremy L Thompson /**
818ca94c3ddSJeremy L Thompson   @brief Common code for creating a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator`
819eaf62fffSJeremy L Thompson 
820ca94c3ddSJeremy L Thompson   @param[in]  op_fine      Fine grid `CeedOperator`
821ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
822ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid `CeedElemRestriction`
823ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector `CeedBasis`
824ca94c3ddSJeremy L Thompson   @param[in]  basis_c_to_f `CeedBasis` for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction operators
825ca94c3ddSJeremy L Thompson   @param[out] op_coarse    Coarse grid `CeedOperator`
826ca94c3ddSJeremy L Thompson   @param[out] op_prolong   Coarse to fine `CeedOperator`, or `NULL`
827ca94c3ddSJeremy L Thompson   @param[out] op_restrict  Fine to coarse `CeedOperator`, or `NULL`
828eaf62fffSJeremy L Thompson 
829eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
830eaf62fffSJeremy L Thompson 
831eaf62fffSJeremy L Thompson   @ref Developer
832eaf62fffSJeremy L Thompson **/
8332b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
8347758292fSSebastian Grimberg                                             CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
8351c66c397SJeremy L Thompson   bool                is_composite;
836eaf62fffSJeremy L Thompson   Ceed                ceed;
8371203703bSJeremy L Thompson   CeedInt             num_comp, num_input_fields, num_output_fields;
83885bb9dcfSJeremy L Thompson   CeedVector          mult_vec         = NULL;
8391c66c397SJeremy L Thompson   CeedElemRestriction rstr_p_mult_fine = NULL, rstr_fine = NULL;
8401203703bSJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
8411c66c397SJeremy L Thompson 
8422b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
843eaf62fffSJeremy L Thompson 
844eaf62fffSJeremy L Thompson   // Check for composite operator
8452b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op_fine, &is_composite));
8466574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported");
847eaf62fffSJeremy L Thompson 
848eaf62fffSJeremy L Thompson   // Coarse Grid
8492b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
8501203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op_fine, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
851eaf62fffSJeremy L Thompson   // -- Clone input fields
8521203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
8536f8994e9SJeremy L Thompson     const char         *field_name;
8541203703bSJeremy L Thompson     CeedVector          vec;
8551203703bSJeremy L Thompson     CeedElemRestriction rstr;
8561203703bSJeremy L Thompson     CeedBasis           basis;
8571203703bSJeremy L Thompson 
8581203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetName(input_fields[i], &field_name));
8591203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(input_fields[i], &vec));
8601203703bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
8611203703bSJeremy L Thompson       rstr  = rstr_coarse;
8621203703bSJeremy L Thompson       basis = basis_coarse;
8631203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_fine));
864eaf62fffSJeremy L Thompson     } else {
8651203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr));
8661203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis));
867eaf62fffSJeremy L Thompson     }
8681203703bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec));
869eaf62fffSJeremy L Thompson   }
870eaf62fffSJeremy L Thompson   // -- Clone output fields
8711203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
8726f8994e9SJeremy L Thompson     const char         *field_name;
8731203703bSJeremy L Thompson     CeedVector          vec;
8741203703bSJeremy L Thompson     CeedElemRestriction rstr;
8751203703bSJeremy L Thompson     CeedBasis           basis;
8761203703bSJeremy L Thompson 
8771203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetName(output_fields[i], &field_name));
8781203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
8791203703bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
8801203703bSJeremy L Thompson       rstr  = rstr_coarse;
8811203703bSJeremy L Thompson       basis = basis_coarse;
8821203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_fine));
883eaf62fffSJeremy L Thompson     } else {
8841203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr));
8851203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis));
886eaf62fffSJeremy L Thompson     }
8871203703bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec));
888eaf62fffSJeremy L Thompson   }
889af99e877SJeremy L Thompson   // -- Clone QFunctionAssemblyData
890*7d5185d7SSebastian Grimberg   {
891*7d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData fine_data;
892*7d5185d7SSebastian Grimberg 
893*7d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op_fine, &fine_data));
894*7d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataReferenceCopy(fine_data, &(*op_coarse)->qf_assembled));
895*7d5185d7SSebastian Grimberg   }
896eaf62fffSJeremy L Thompson 
897eaf62fffSJeremy L Thompson   // Multiplicity vector
8987758292fSSebastian Grimberg   if (op_restrict || op_prolong) {
89985bb9dcfSJeremy L Thompson     CeedVector          mult_e_vec;
9001c66c397SJeremy L Thompson     CeedRestrictionType rstr_type;
90185bb9dcfSJeremy L Thompson 
9027c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr_fine, &rstr_type));
9037c1dbaffSSebastian Grimberg     CeedCheck(rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_UNSUPPORTED,
9047c1dbaffSSebastian Grimberg               "Element restrictions created with CeedElemRestrictionCreateCurlOriented are not supported");
9056574a04fSJeremy L Thompson     CeedCheck(p_mult_fine, ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector");
9067c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnsignedCopy(rstr_fine, &rstr_p_mult_fine));
9072b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec));
9082b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_e_vec, 0.0));
909c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE));
9102b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_vec, 0.0));
911c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE));
9122b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&mult_e_vec));
9132b730f8bSJeremy L Thompson     CeedCall(CeedVectorReciprocal(mult_vec));
91485bb9dcfSJeremy L Thompson   }
915eaf62fffSJeremy L Thompson 
916addd79feSZach Atkins   // Clone name
917addd79feSZach Atkins   bool   has_name = op_fine->name;
918addd79feSZach Atkins   size_t name_len = op_fine->name ? strlen(op_fine->name) : 0;
919addd79feSZach Atkins   CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name));
920addd79feSZach Atkins 
9217758292fSSebastian Grimberg   // Check that coarse to fine basis is provided if prolong/restrict operators are requested
9227758292fSSebastian Grimberg   CeedCheck(basis_c_to_f || (!op_restrict && !op_prolong), ceed, CEED_ERROR_INCOMPATIBLE,
9236574a04fSJeremy L Thompson             "Prolongation or restriction operator creation requires coarse-to-fine basis");
92483d6adf3SZach Atkins 
92585bb9dcfSJeremy L Thompson   // Restriction/Prolongation Operators
9262b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp));
927addd79feSZach Atkins 
928addd79feSZach Atkins   // Restriction
9297758292fSSebastian Grimberg   if (op_restrict) {
930eaf62fffSJeremy L Thompson     CeedInt             *num_comp_r_data;
93185bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_r;
9327758292fSSebastian Grimberg     CeedQFunction        qf_restrict;
93385bb9dcfSJeremy L Thompson 
9347758292fSSebastian Grimberg     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict));
9352b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_r_data));
936eaf62fffSJeremy L Thompson     num_comp_r_data[0] = num_comp;
9372b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r));
9382b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data));
9397758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r));
9402b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_r));
9417758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE));
9427758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE));
9437758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP));
9447758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp));
945eaf62fffSJeremy L Thompson 
9467758292fSSebastian Grimberg     CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict));
9477758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
9487758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
9497758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
950eaf62fffSJeremy L Thompson 
951addd79feSZach Atkins     // Set name
952addd79feSZach Atkins     char *restriction_name;
9531c66c397SJeremy L Thompson 
954addd79feSZach Atkins     CeedCall(CeedCalloc(17 + name_len, &restriction_name));
955addd79feSZach Atkins     sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
9567758292fSSebastian Grimberg     CeedCall(CeedOperatorSetName(*op_restrict, restriction_name));
957addd79feSZach Atkins     CeedCall(CeedFree(&restriction_name));
958addd79feSZach Atkins 
959addd79feSZach Atkins     // Check
9607758292fSSebastian Grimberg     CeedCall(CeedOperatorCheckReady(*op_restrict));
961addd79feSZach Atkins 
962addd79feSZach Atkins     // Cleanup
9637758292fSSebastian Grimberg     CeedCall(CeedQFunctionDestroy(&qf_restrict));
964addd79feSZach Atkins   }
965addd79feSZach Atkins 
966eaf62fffSJeremy L Thompson   // Prolongation
967addd79feSZach Atkins   if (op_prolong) {
968eaf62fffSJeremy L Thompson     CeedInt             *num_comp_p_data;
96985bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_p;
9701c66c397SJeremy L Thompson     CeedQFunction        qf_prolong;
97185bb9dcfSJeremy L Thompson 
97285bb9dcfSJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong));
9732b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_p_data));
974eaf62fffSJeremy L Thompson     num_comp_p_data[0] = num_comp;
9752b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p));
9762b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data));
9772b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p));
9782b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_p));
9792b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP));
9802b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE));
9812b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE));
9822b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp));
983eaf62fffSJeremy L Thompson 
9842b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong));
9852b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
986356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
987356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
988eaf62fffSJeremy L Thompson 
989addd79feSZach Atkins     // Set name
990ea6b5821SJeremy L Thompson     char *prolongation_name;
9911c66c397SJeremy L Thompson 
9922b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(18 + name_len, &prolongation_name));
9932b730f8bSJeremy L Thompson     sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
9942b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name));
9952b730f8bSJeremy L Thompson     CeedCall(CeedFree(&prolongation_name));
996addd79feSZach Atkins 
997addd79feSZach Atkins     // Check
998addd79feSZach Atkins     CeedCall(CeedOperatorCheckReady(*op_prolong));
999addd79feSZach Atkins 
1000addd79feSZach Atkins     // Cleanup
1001addd79feSZach Atkins     CeedCall(CeedQFunctionDestroy(&qf_prolong));
1002ea6b5821SJeremy L Thompson   }
1003ea6b5821SJeremy L Thompson 
100458e4b056SJeremy L Thompson   // Check
100558e4b056SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(*op_coarse));
100658e4b056SJeremy L Thompson 
1007eaf62fffSJeremy L Thompson   // Cleanup
10082b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_vec));
1009c17ec2beSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine));
10102b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis_c_to_f));
1011eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1012eaf62fffSJeremy L Thompson }
1013eaf62fffSJeremy L Thompson 
1014eaf62fffSJeremy L Thompson /**
1015eaf62fffSJeremy L Thompson   @brief Build 1D mass matrix and Laplacian with perturbation
1016eaf62fffSJeremy L Thompson 
1017eaf62fffSJeremy L Thompson   @param[in]  interp_1d   Interpolation matrix in one dimension
1018eaf62fffSJeremy L Thompson   @param[in]  grad_1d     Gradient matrix in one dimension
1019eaf62fffSJeremy L Thompson   @param[in]  q_weight_1d Quadrature weights in one dimension
1020eaf62fffSJeremy L Thompson   @param[in]  P_1d        Number of basis nodes in one dimension
1021eaf62fffSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
1022eaf62fffSJeremy L Thompson   @param[in]  dim         Dimension of basis
1023eaf62fffSJeremy L Thompson   @param[out] mass        Assembled mass matrix in one dimension
1024eaf62fffSJeremy L Thompson   @param[out] laplace     Assembled perturbed Laplacian in one dimension
1025eaf62fffSJeremy L Thompson 
1026eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1027eaf62fffSJeremy L Thompson 
1028eaf62fffSJeremy L Thompson   @ref Developer
1029eaf62fffSJeremy L Thompson **/
10302c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
10312c2ea1dbSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d, CeedInt P_1d, CeedInt Q_1d,
10322c2ea1dbSJeremy L Thompson                                 CeedInt dim, CeedScalar *mass, CeedScalar *laplace) {
10332b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
1034eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
1035eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
10362b730f8bSJeremy 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];
1037eaf62fffSJeremy L Thompson       mass[i + j * P_1d] = sum;
1038eaf62fffSJeremy L Thompson     }
10392b730f8bSJeremy L Thompson   }
1040eaf62fffSJeremy L Thompson   // -- Laplacian
10412b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
1042eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
1043eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
10441c66c397SJeremy L Thompson 
10452b730f8bSJeremy 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];
1046eaf62fffSJeremy L Thompson       laplace[i + j * P_1d] = sum;
1047eaf62fffSJeremy L Thompson     }
10482b730f8bSJeremy L Thompson   }
1049eaf62fffSJeremy L Thompson   CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4;
10502b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation;
1051eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1052eaf62fffSJeremy L Thompson }
10532c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
1054eaf62fffSJeremy L Thompson 
1055eaf62fffSJeremy L Thompson /// @}
1056eaf62fffSJeremy L Thompson 
1057eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1058480fae85SJeremy L Thompson /// CeedOperator Backend API
1059480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1060480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend
1061480fae85SJeremy L Thompson /// @{
1062480fae85SJeremy L Thompson 
1063480fae85SJeremy L Thompson /**
1064004e4986SSebastian Grimberg   @brief Select correct basis matrix pointer based on @ref CeedEvalMode
1065004e4986SSebastian Grimberg 
1066004e4986SSebastian Grimberg   @param[in]  basis     `CeedBasis` from which to get the basis matrix
1067004e4986SSebastian Grimberg   @param[in]  eval_mode Current basis evaluation mode
1068004e4986SSebastian Grimberg   @param[in]  identity  Pointer to identity matrix
1069004e4986SSebastian Grimberg   @param[out] basis_ptr `CeedBasis` pointer to set
1070004e4986SSebastian Grimberg 
1071004e4986SSebastian Grimberg   @ref Backend
1072004e4986SSebastian Grimberg **/
1073004e4986SSebastian Grimberg int CeedOperatorGetBasisPointer(CeedBasis basis, CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar **basis_ptr) {
1074004e4986SSebastian Grimberg   switch (eval_mode) {
1075004e4986SSebastian Grimberg     case CEED_EVAL_NONE:
1076004e4986SSebastian Grimberg       *basis_ptr = identity;
1077004e4986SSebastian Grimberg       break;
1078004e4986SSebastian Grimberg     case CEED_EVAL_INTERP:
1079004e4986SSebastian Grimberg       CeedCall(CeedBasisGetInterp(basis, basis_ptr));
1080004e4986SSebastian Grimberg       break;
1081004e4986SSebastian Grimberg     case CEED_EVAL_GRAD:
1082004e4986SSebastian Grimberg       CeedCall(CeedBasisGetGrad(basis, basis_ptr));
1083004e4986SSebastian Grimberg       break;
1084004e4986SSebastian Grimberg     case CEED_EVAL_DIV:
1085004e4986SSebastian Grimberg       CeedCall(CeedBasisGetDiv(basis, basis_ptr));
1086004e4986SSebastian Grimberg       break;
1087004e4986SSebastian Grimberg     case CEED_EVAL_CURL:
1088004e4986SSebastian Grimberg       CeedCall(CeedBasisGetCurl(basis, basis_ptr));
1089004e4986SSebastian Grimberg       break;
1090004e4986SSebastian Grimberg     case CEED_EVAL_WEIGHT:
1091004e4986SSebastian Grimberg       break;  // Caught by QF Assembly
1092004e4986SSebastian Grimberg   }
1093004e4986SSebastian Grimberg   assert(*basis_ptr != NULL);
1094004e4986SSebastian Grimberg   return CEED_ERROR_SUCCESS;
1095004e4986SSebastian Grimberg }
1096004e4986SSebastian Grimberg 
1097004e4986SSebastian Grimberg /**
1098ca94c3ddSJeremy L Thompson   @brief Create point block restriction for active `CeedOperatorField`
1099506b1a0cSSebastian Grimberg 
1100ca94c3ddSJeremy L Thompson   @param[in]  rstr             Original `CeedElemRestriction` for active field
1101ca94c3ddSJeremy L Thompson   @param[out] point_block_rstr Address of the variable where the newly created `CeedElemRestriction` will be stored
1102506b1a0cSSebastian Grimberg 
1103506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1104506b1a0cSSebastian Grimberg 
1105506b1a0cSSebastian Grimberg   @ref Backend
1106506b1a0cSSebastian Grimberg **/
1107506b1a0cSSebastian Grimberg int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *point_block_rstr) {
1108506b1a0cSSebastian Grimberg   Ceed           ceed;
1109506b1a0cSSebastian Grimberg   CeedInt        num_elem, num_comp, shift, elem_size, comp_stride, *point_block_offsets;
1110506b1a0cSSebastian Grimberg   CeedSize       l_size;
1111506b1a0cSSebastian Grimberg   const CeedInt *offsets;
1112506b1a0cSSebastian Grimberg 
1113506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
1114506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
1115506b1a0cSSebastian Grimberg 
1116506b1a0cSSebastian Grimberg   // Expand offsets
1117506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
1118506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1119506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
1120506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
1121506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
1122506b1a0cSSebastian Grimberg   shift = num_comp;
1123506b1a0cSSebastian Grimberg   if (comp_stride != 1) shift *= num_comp;
1124506b1a0cSSebastian Grimberg   CeedCall(CeedCalloc(num_elem * elem_size, &point_block_offsets));
1125506b1a0cSSebastian Grimberg   for (CeedInt i = 0; i < num_elem * elem_size; i++) {
1126506b1a0cSSebastian Grimberg     point_block_offsets[i] = offsets[i] * shift;
1127506b1a0cSSebastian Grimberg   }
1128506b1a0cSSebastian Grimberg 
1129506b1a0cSSebastian Grimberg   // Create new restriction
1130506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
1131506b1a0cSSebastian Grimberg                                      point_block_offsets, point_block_rstr));
1132506b1a0cSSebastian Grimberg 
1133506b1a0cSSebastian Grimberg   // Cleanup
1134506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
1135506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1136506b1a0cSSebastian Grimberg }
1137506b1a0cSSebastian Grimberg 
1138506b1a0cSSebastian Grimberg /**
1139*7d5185d7SSebastian Grimberg   @brief Get `CeedQFunctionAssemblyData`
1140*7d5185d7SSebastian Grimberg 
1141*7d5185d7SSebastian Grimberg   @param[in]  op   `CeedOperator` to assemble
1142*7d5185d7SSebastian Grimberg   @param[out] data `CeedQFunctionAssemblyData`
1143*7d5185d7SSebastian Grimberg 
1144*7d5185d7SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1145*7d5185d7SSebastian Grimberg 
1146*7d5185d7SSebastian Grimberg   @ref Backend
1147*7d5185d7SSebastian Grimberg **/
1148*7d5185d7SSebastian Grimberg int CeedOperatorGetQFunctionAssemblyData(CeedOperator op, CeedQFunctionAssemblyData *data) {
1149*7d5185d7SSebastian Grimberg   if (!op->qf_assembled) {
1150*7d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
1151*7d5185d7SSebastian Grimberg 
1152*7d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataCreate(op->ceed, &data));
1153*7d5185d7SSebastian Grimberg     op->qf_assembled = data;
1154*7d5185d7SSebastian Grimberg   }
1155*7d5185d7SSebastian Grimberg   *data = op->qf_assembled;
1156*7d5185d7SSebastian Grimberg   return CEED_ERROR_SUCCESS;
1157*7d5185d7SSebastian Grimberg }
1158*7d5185d7SSebastian Grimberg 
1159*7d5185d7SSebastian Grimberg /**
1160ca94c3ddSJeremy L Thompson   @brief Create object holding `CeedQFunction` assembly data for `CeedOperator`
1161480fae85SJeremy L Thompson 
1162ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedQFunctionAssemblyData`
1163ca94c3ddSJeremy L Thompson   @param[out] data Address of the variable where the newly created `CeedQFunctionAssemblyData` will be stored
1164480fae85SJeremy L Thompson 
1165480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1166480fae85SJeremy L Thompson 
1167480fae85SJeremy L Thompson   @ref Backend
1168480fae85SJeremy L Thompson **/
1169ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) {
11702b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1171480fae85SJeremy L Thompson   (*data)->ref_count = 1;
1172480fae85SJeremy L Thompson   (*data)->ceed      = ceed;
11732b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1174480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1175480fae85SJeremy L Thompson }
1176480fae85SJeremy L Thompson 
1177480fae85SJeremy L Thompson /**
1178ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedQFunctionAssemblyData`
1179480fae85SJeremy L Thompson 
1180ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to increment the reference counter
1181480fae85SJeremy L Thompson 
1182480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1183480fae85SJeremy L Thompson 
1184480fae85SJeremy L Thompson   @ref Backend
1185480fae85SJeremy L Thompson **/
1186480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) {
1187480fae85SJeremy L Thompson   data->ref_count++;
1188480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1189480fae85SJeremy L Thompson }
1190480fae85SJeremy L Thompson 
1191480fae85SJeremy L Thompson /**
1192ca94c3ddSJeremy L Thompson   @brief Set re-use of `CeedQFunctionAssemblyData`
11938b919e6bSJeremy L Thompson 
1194ca94c3ddSJeremy L Thompson   @param[in,out] data       `CeedQFunctionAssemblyData` to mark for reuse
1195ea61e9acSJeremy L Thompson   @param[in]     reuse_data Boolean flag indicating data re-use
11968b919e6bSJeremy L Thompson 
11978b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
11988b919e6bSJeremy L Thompson 
11998b919e6bSJeremy L Thompson   @ref Backend
12008b919e6bSJeremy L Thompson **/
12012b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) {
1202beecbf24SJeremy L Thompson   data->reuse_data        = reuse_data;
1203beecbf24SJeremy L Thompson   data->needs_data_update = true;
1204beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1205beecbf24SJeremy L Thompson }
1206beecbf24SJeremy L Thompson 
1207beecbf24SJeremy L Thompson /**
1208ca94c3ddSJeremy L Thompson   @brief Mark `CeedQFunctionAssemblyData` as stale
1209beecbf24SJeremy L Thompson 
1210ca94c3ddSJeremy L Thompson   @param[in,out] data              `CeedQFunctionAssemblyData` to mark as stale
1211ea61e9acSJeremy L Thompson   @param[in]     needs_data_update Boolean flag indicating if update is needed or completed
1212beecbf24SJeremy L Thompson 
1213beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1214beecbf24SJeremy L Thompson 
1215beecbf24SJeremy L Thompson   @ref Backend
1216beecbf24SJeremy L Thompson **/
12172b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) {
1218beecbf24SJeremy L Thompson   data->needs_data_update = needs_data_update;
12198b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12208b919e6bSJeremy L Thompson }
12218b919e6bSJeremy L Thompson 
12228b919e6bSJeremy L Thompson /**
1223ca94c3ddSJeremy L Thompson   @brief Determine if `CeedQFunctionAssemblyData` needs update
12248b919e6bSJeremy L Thompson 
1225ca94c3ddSJeremy L Thompson   @param[in]  data             `CeedQFunctionAssemblyData` to mark as stale
12268b919e6bSJeremy L Thompson   @param[out] is_update_needed Boolean flag indicating if re-assembly is required
12278b919e6bSJeremy L Thompson 
12288b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12298b919e6bSJeremy L Thompson 
12308b919e6bSJeremy L Thompson   @ref Backend
12318b919e6bSJeremy L Thompson **/
12322b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) {
1233beecbf24SJeremy L Thompson   *is_update_needed = !data->reuse_data || data->needs_data_update;
12348b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12358b919e6bSJeremy L Thompson }
12368b919e6bSJeremy L Thompson 
12378b919e6bSJeremy L Thompson /**
1238ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedQFunctionAssemblyData`.
12394385fb7fSSebastian Grimberg 
1240ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedQFunctionAssemblyDataDestroy().
1241512bb800SJeremy L Thompson 
1242ca94c3ddSJeremy 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 `CeedQFunctionAssemblyData`.
1243ca94c3ddSJeremy L Thompson         This `CeedQFunctionAssemblyData` will be destroyed if ` *data_copy` is the only reference to this `CeedQFunctionAssemblyData`.
1244480fae85SJeremy L Thompson 
1245ca94c3ddSJeremy L Thompson   @param[in]     data      `CeedQFunctionAssemblyData` to copy reference to
1246ea61e9acSJeremy L Thompson   @param[in,out] data_copy Variable to store copied reference
1247480fae85SJeremy L Thompson 
1248480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1249480fae85SJeremy L Thompson 
1250480fae85SJeremy L Thompson   @ref Backend
1251480fae85SJeremy L Thompson **/
12522b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) {
12532b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReference(data));
12542b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy));
1255480fae85SJeremy L Thompson   *data_copy = data;
1256480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1257480fae85SJeremy L Thompson }
1258480fae85SJeremy L Thompson 
1259480fae85SJeremy L Thompson /**
1260ca94c3ddSJeremy L Thompson   @brief Get setup status for internal objects for `CeedQFunctionAssemblyData`
1261480fae85SJeremy L Thompson 
1262ca94c3ddSJeremy L Thompson   @param[in]  data     `CeedQFunctionAssemblyData` to retrieve status
1263480fae85SJeremy L Thompson   @param[out] is_setup Boolean flag for setup status
1264480fae85SJeremy L Thompson 
1265480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1266480fae85SJeremy L Thompson 
1267480fae85SJeremy L Thompson   @ref Backend
1268480fae85SJeremy L Thompson **/
12692b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) {
1270480fae85SJeremy L Thompson   *is_setup = data->is_setup;
1271480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1272480fae85SJeremy L Thompson }
1273480fae85SJeremy L Thompson 
1274480fae85SJeremy L Thompson /**
1275ca94c3ddSJeremy L Thompson   @brief Set internal objects for `CeedQFunctionAssemblyData`
1276480fae85SJeremy L Thompson 
1277ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to set objects
1278ca94c3ddSJeremy L Thompson   @param[in]     vec  `CeedVector` to store assembled `CeedQFunction` at quadrature points
1279ca94c3ddSJeremy L Thompson   @param[in]     rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1280480fae85SJeremy L Thompson 
1281480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1282480fae85SJeremy L Thompson 
1283480fae85SJeremy L Thompson   @ref Backend
1284480fae85SJeremy L Thompson **/
12852b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) {
12862b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(vec, &data->vec));
12872b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr));
1288480fae85SJeremy L Thompson 
1289480fae85SJeremy L Thompson   data->is_setup = true;
1290480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1291480fae85SJeremy L Thompson }
1292480fae85SJeremy L Thompson 
12934dd1a9d2SSebastian Grimberg /**
1294ca94c3ddSJeremy L Thompson   @brief Get internal objects for `CeedQFunctionAssemblyData`
12954dd1a9d2SSebastian Grimberg 
1296ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to set objects
1297ca94c3ddSJeremy L Thompson   @param[out]    vec  `CeedVector` to store assembled `CeedQFunction` at quadrature points
1298ca94c3ddSJeremy L Thompson   @param[out]    rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
12994dd1a9d2SSebastian Grimberg 
13004dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
13014dd1a9d2SSebastian Grimberg 
13024dd1a9d2SSebastian Grimberg   @ref Backend
13034dd1a9d2SSebastian Grimberg **/
13042b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) {
13056574a04fSJeremy L Thompson   CeedCheck(data->is_setup, data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first.");
1306480fae85SJeremy L Thompson 
13072b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(data->vec, vec));
13082b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr));
1309480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1310480fae85SJeremy L Thompson }
1311480fae85SJeremy L Thompson 
1312480fae85SJeremy L Thompson /**
1313ca94c3ddSJeremy L Thompson   @brief Destroy `CeedQFunctionAssemblyData`
1314480fae85SJeremy L Thompson 
1315ca94c3ddSJeremy L Thompson   @param[in,out] data  `CeedQFunctionAssemblyData` to destroy
1316480fae85SJeremy L Thompson 
1317480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1318480fae85SJeremy L Thompson 
1319480fae85SJeremy L Thompson   @ref Backend
1320480fae85SJeremy L Thompson **/
1321480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) {
1322ad6481ceSJeremy L Thompson   if (!*data || --(*data)->ref_count > 0) {
1323ad6481ceSJeremy L Thompson     *data = NULL;
1324ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1325ad6481ceSJeremy L Thompson   }
13262b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
13272b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*data)->vec));
13282b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr));
1329480fae85SJeremy L Thompson 
13302b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1331480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1332480fae85SJeremy L Thompson }
1333480fae85SJeremy L Thompson 
1334ed9e99e6SJeremy L Thompson /**
1335ca94c3ddSJeremy L Thompson   @brief Get `CeedOperatorAssemblyData`
1336ed9e99e6SJeremy L Thompson 
1337ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator` to assemble
1338*7d5185d7SSebastian Grimberg   @param[out] data `CeedOperatorAssemblyData`
1339ed9e99e6SJeremy L Thompson 
1340ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1341ed9e99e6SJeremy L Thompson 
1342ed9e99e6SJeremy L Thompson   @ref Backend
1343ed9e99e6SJeremy L Thompson **/
13442b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) {
1345ed9e99e6SJeremy L Thompson   if (!op->op_assembled) {
1346ed9e99e6SJeremy L Thompson     CeedOperatorAssemblyData data;
1347ed9e99e6SJeremy L Thompson 
13482b730f8bSJeremy L Thompson     CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data));
1349ed9e99e6SJeremy L Thompson     op->op_assembled = data;
1350ed9e99e6SJeremy L Thompson   }
1351ed9e99e6SJeremy L Thompson   *data = op->op_assembled;
1352ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1353ed9e99e6SJeremy L Thompson }
1354ed9e99e6SJeremy L Thompson 
1355ed9e99e6SJeremy L Thompson /**
1356ca94c3ddSJeremy L Thompson   @brief Create object holding `CeedOperator` assembly data.
1357ba746a46SJeremy L Thompson 
1358ca94c3ddSJeremy L Thompson   The `CeedOperatorAssemblyData` holds an array with references to every active `CeedBasis` used in the `CeedOperator`.
1359ca94c3ddSJeremy L Thompson   An array with references to the corresponding active `CeedElemRestriction` is also stored.
1360ca94c3ddSJeremy L Thompson   For each active `CeedBasis, the `CeedOperatorAssemblyData` holds an array of all input and output @ref CeedEvalMode for this `CeedBasis`.
1361ca94c3ddSJeremy L Thompson   The `CeedOperatorAssemblyData` holds an array of offsets for indexing into the assembled `CeedQFunction` arrays to the row representing each @ref CeedEvalMode.
1362ca94c3ddSJeremy L Thompson   The number of input columns across all active bases for the assembled `CeedQFunction` is also stored.
1363ca94c3ddSJeremy L Thompson   Lastly, the `CeedOperatorAssembly` data holds assembled matrices representing the full action of the `CeedBasis` for all @ref CeedEvalMode.
1364ed9e99e6SJeremy L Thompson 
1365ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperatorAssemblyData`
1366ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator` to be assembled
1367ca94c3ddSJeremy L Thompson   @param[out] data Address of the variable where the newly created `CeedOperatorAssemblyData` will be stored
1368ed9e99e6SJeremy L Thompson 
1369ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1370ed9e99e6SJeremy L Thompson 
1371ed9e99e6SJeremy L Thompson   @ref Backend
1372ed9e99e6SJeremy L Thompson **/
13732b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) {
1374506b1a0cSSebastian Grimberg   CeedInt             num_active_bases_in = 0, num_active_bases_out = 0, offset = 0;
1375506b1a0cSSebastian Grimberg   CeedInt             num_input_fields, *num_eval_modes_in = NULL, num_output_fields, *num_eval_modes_out = NULL;
13761c66c397SJeremy L Thompson   CeedSize          **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL;
13771c66c397SJeremy L Thompson   CeedEvalMode      **eval_modes_in = NULL, **eval_modes_out = NULL;
13781c66c397SJeremy L Thompson   CeedQFunctionField *qf_fields;
13791c66c397SJeremy L Thompson   CeedQFunction       qf;
13801c66c397SJeremy L Thompson   CeedOperatorField  *op_fields;
138101f0e615SJames Wright   bool                is_composite;
138201f0e615SJames Wright 
138301f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
138401f0e615SJames Wright   CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Can only create CeedOperator assembly data for non-composite operators.");
1385437c7c90SJeremy L Thompson 
1386437c7c90SJeremy L Thompson   // Allocate
13872b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1388ed9e99e6SJeremy L Thompson   (*data)->ceed = ceed;
13892b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1390ed9e99e6SJeremy L Thompson 
1391ed9e99e6SJeremy L Thompson   // Build OperatorAssembly data
13922b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
1393ed9e99e6SJeremy L Thompson 
1394ed9e99e6SJeremy L Thompson   // Determine active input basis
1395004e4986SSebastian Grimberg   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL));
1396004e4986SSebastian Grimberg   CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1397ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1398ed9e99e6SJeremy L Thompson     CeedVector vec;
13991c66c397SJeremy L Thompson 
14002b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1401ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
14027c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
14031c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
14041c66c397SJeremy L Thompson       CeedBasis    basis_in = NULL;
14051c66c397SJeremy L Thompson 
14062b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
14072b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1408352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp));
1409352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1410506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_active_bases_in; i++) {
1411506b1a0cSSebastian Grimberg         if ((*data)->active_bases_in[i] == basis_in) index = i;
1412437c7c90SJeremy L Thompson       }
1413437c7c90SJeremy L Thompson       if (index == -1) {
1414437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_in;
14151c66c397SJeremy L Thompson 
1416506b1a0cSSebastian Grimberg         index = num_active_bases_in;
1417506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_bases_in));
1418506b1a0cSSebastian Grimberg         (*data)->active_bases_in[num_active_bases_in] = NULL;
1419506b1a0cSSebastian Grimberg         CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases_in[num_active_bases_in]));
1420506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_elem_rstrs_in));
1421506b1a0cSSebastian Grimberg         (*data)->active_elem_rstrs_in[num_active_bases_in] = NULL;
1422437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in));
1423506b1a0cSSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs_in[num_active_bases_in]));
1424506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &num_eval_modes_in));
1425437c7c90SJeremy L Thompson         num_eval_modes_in[index] = 0;
1426506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_modes_in));
1427437c7c90SJeremy L Thompson         eval_modes_in[index] = NULL;
1428506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_mode_offsets_in));
1429437c7c90SJeremy L Thompson         eval_mode_offsets_in[index] = NULL;
1430506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->assembled_bases_in));
1431437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index] = NULL;
1432506b1a0cSSebastian Grimberg         num_active_bases_in++;
1433437c7c90SJeremy L Thompson       }
1434352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1435352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1436352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_modes_in[index]));
1437352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_mode_offsets_in[index]));
1438352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1439437c7c90SJeremy L Thompson           eval_modes_in[index][num_eval_modes_in[index] + d]        = eval_mode;
1440437c7c90SJeremy L Thompson           eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset;
1441352a5e7cSSebastian Grimberg           offset += num_comp;
1442ed9e99e6SJeremy L Thompson         }
1443352a5e7cSSebastian Grimberg         num_eval_modes_in[index] += q_comp;
1444ed9e99e6SJeremy L Thompson       }
1445ed9e99e6SJeremy L Thompson     }
1446ed9e99e6SJeremy L Thompson   }
1447ed9e99e6SJeremy L Thompson 
1448ed9e99e6SJeremy L Thompson   // Determine active output basis
14492b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields));
14502b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1451437c7c90SJeremy L Thompson   offset = 0;
1452ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1453ed9e99e6SJeremy L Thompson     CeedVector vec;
14541c66c397SJeremy L Thompson 
14552b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1456ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
14577c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
14581c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
14591c66c397SJeremy L Thompson       CeedBasis    basis_out = NULL;
14601c66c397SJeremy L Thompson 
1461437c7c90SJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
14622b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1463352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_out, &num_comp));
1464352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1465506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_active_bases_out; i++) {
1466506b1a0cSSebastian Grimberg         if ((*data)->active_bases_out[i] == basis_out) index = i;
1467437c7c90SJeremy L Thompson       }
1468437c7c90SJeremy L Thompson       if (index == -1) {
1469437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_out;
14701c66c397SJeremy L Thompson 
1471506b1a0cSSebastian Grimberg         index = num_active_bases_out;
1472506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_bases_out));
1473506b1a0cSSebastian Grimberg         (*data)->active_bases_out[num_active_bases_out] = NULL;
1474506b1a0cSSebastian Grimberg         CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases_out[num_active_bases_out]));
1475506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_elem_rstrs_out));
1476506b1a0cSSebastian Grimberg         (*data)->active_elem_rstrs_out[num_active_bases_out] = NULL;
1477437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out));
1478506b1a0cSSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs_out[num_active_bases_out]));
1479506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &num_eval_modes_out));
1480437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1481506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_modes_out));
1482437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1483506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_mode_offsets_out));
1484437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1485506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->assembled_bases_out));
1486437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1487506b1a0cSSebastian Grimberg         num_active_bases_out++;
1488437c7c90SJeremy L Thompson       }
1489352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1490352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1491352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_modes_out[index]));
1492352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_mode_offsets_out[index]));
1493352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1494437c7c90SJeremy L Thompson           eval_modes_out[index][num_eval_modes_out[index] + d]        = eval_mode;
1495437c7c90SJeremy L Thompson           eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset;
1496352a5e7cSSebastian Grimberg           offset += num_comp;
1497ed9e99e6SJeremy L Thompson         }
1498352a5e7cSSebastian Grimberg         num_eval_modes_out[index] += q_comp;
1499ed9e99e6SJeremy L Thompson       }
1500ed9e99e6SJeremy L Thompson     }
1501ed9e99e6SJeremy L Thompson   }
1502506b1a0cSSebastian Grimberg   (*data)->num_active_bases_in   = num_active_bases_in;
150327789c4aSJed Brown   (*data)->num_eval_modes_in     = num_eval_modes_in;
150427789c4aSJed Brown   (*data)->eval_modes_in         = eval_modes_in;
150527789c4aSJed Brown   (*data)->eval_mode_offsets_in  = eval_mode_offsets_in;
1506506b1a0cSSebastian Grimberg   (*data)->num_active_bases_out  = num_active_bases_out;
1507437c7c90SJeremy L Thompson   (*data)->num_eval_modes_out    = num_eval_modes_out;
1508437c7c90SJeremy L Thompson   (*data)->eval_modes_out        = eval_modes_out;
1509437c7c90SJeremy L Thompson   (*data)->eval_mode_offsets_out = eval_mode_offsets_out;
1510506b1a0cSSebastian Grimberg   (*data)->num_output_components = offset;
1511ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1512ed9e99e6SJeremy L Thompson }
1513ed9e99e6SJeremy L Thompson 
1514ed9e99e6SJeremy L Thompson /**
1515ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` @ref CeedEvalMode for assembly.
1516ba746a46SJeremy L Thompson 
1517ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1518ed9e99e6SJeremy L Thompson 
1519ca94c3ddSJeremy L Thompson   @param[in]  data                  `CeedOperatorAssemblyData`
1520506b1a0cSSebastian Grimberg   @param[out] num_active_bases_in   Total number of active bases for input
1521ca94c3ddSJeremy L Thompson   @param[out] num_eval_modes_in     Pointer to hold array of numbers of input @ref CeedEvalMode, or `NULL`.
1522ca94c3ddSJeremy L Thompson                                       `eval_modes_in[0]` holds an array of eval modes for the first active `CeedBasis`.
1523ca94c3ddSJeremy L Thompson   @param[out] eval_modes_in         Pointer to hold arrays of input @ref CeedEvalMode, or `NULL`
1524ca94c3ddSJeremy L Thompson   @param[out] eval_mode_offsets_in  Pointer to hold arrays of input offsets at each quadrature point
1525506b1a0cSSebastian Grimberg   @param[out] num_active_bases_out  Total number of active bases for output
1526ca94c3ddSJeremy L Thompson   @param[out] num_eval_modes_out    Pointer to hold array of numbers of output @ref CeedEvalMode, or `NULL`
1527ca94c3ddSJeremy L Thompson   @param[out] eval_modes_out        Pointer to hold arrays of output @ref CeedEvalMode, or `NULL`
1528437c7c90SJeremy L Thompson   @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point
1529ca94c3ddSJeremy L Thompson   @param[out] num_output_components The number of columns in the assembled `CeedQFunction` matrix for each quadrature point, including contributions of all active bases
1530ed9e99e6SJeremy L Thompson 
1531ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1532ed9e99e6SJeremy L Thompson 
1533ed9e99e6SJeremy L Thompson   @ref Backend
1534ed9e99e6SJeremy L Thompson **/
1535506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedInt **num_eval_modes_in,
1536506b1a0cSSebastian Grimberg                                          const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt *num_active_bases_out,
1537506b1a0cSSebastian Grimberg                                          CeedInt **num_eval_modes_out, const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out,
1538506b1a0cSSebastian Grimberg                                          CeedSize *num_output_components) {
1539506b1a0cSSebastian Grimberg   if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in;
1540437c7c90SJeremy L Thompson   if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in;
1541437c7c90SJeremy L Thompson   if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in;
1542437c7c90SJeremy L Thompson   if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in;
1543506b1a0cSSebastian Grimberg   if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out;
1544437c7c90SJeremy L Thompson   if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out;
1545437c7c90SJeremy L Thompson   if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out;
1546437c7c90SJeremy L Thompson   if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out;
1547437c7c90SJeremy L Thompson   if (num_output_components) *num_output_components = data->num_output_components;
1548ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1549ed9e99e6SJeremy L Thompson }
1550ed9e99e6SJeremy L Thompson 
1551ed9e99e6SJeremy L Thompson /**
1552ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` `CeedBasis` data for assembly.
1553ba746a46SJeremy L Thompson 
1554ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1555ed9e99e6SJeremy L Thompson 
1556ca94c3ddSJeremy L Thompson   @param[in]  data                 `CeedOperatorAssemblyData`
1557ca94c3ddSJeremy L Thompson   @param[out] num_active_bases_in  Number of active input bases, or `NULL`
1558ca94c3ddSJeremy L Thompson   @param[out] active_bases_in      Pointer to hold active input `CeedBasis`, or `NULL`
1559ca94c3ddSJeremy L Thompson   @param[out] assembled_bases_in   Pointer to hold assembled active input `B` , or `NULL`
1560ca94c3ddSJeremy L Thompson   @param[out] num_active_bases_out Number of active output bases, or `NULL`
1561ca94c3ddSJeremy L Thompson   @param[out] active_bases_out     Pointer to hold active output `CeedBasis`, or `NULL`
1562ca94c3ddSJeremy L Thompson   @param[out] assembled_bases_out  Pointer to hold assembled active output `B` , or `NULL`
1563ed9e99e6SJeremy L Thompson 
1564ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1565ed9e99e6SJeremy L Thompson 
1566ed9e99e6SJeremy L Thompson   @ref Backend
1567ed9e99e6SJeremy L Thompson **/
1568506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedBasis **active_bases_in,
1569506b1a0cSSebastian Grimberg                                      const CeedScalar ***assembled_bases_in, CeedInt *num_active_bases_out, CeedBasis **active_bases_out,
1570506b1a0cSSebastian Grimberg                                      const CeedScalar ***assembled_bases_out) {
1571ed9e99e6SJeremy L Thompson   // Assemble B_in, B_out if needed
1572437c7c90SJeremy L Thompson   if (assembled_bases_in && !data->assembled_bases_in[0]) {
1573437c7c90SJeremy L Thompson     CeedInt num_qpts;
1574437c7c90SJeremy L Thompson 
1575506b1a0cSSebastian Grimberg     if (data->active_bases_in[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[0], &num_qpts));
1576506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_in[0], &num_qpts));
1577506b1a0cSSebastian Grimberg     for (CeedInt b = 0; b < data->num_active_bases_in; b++) {
15781c66c397SJeremy L Thompson       bool        has_eval_none = false;
1579352a5e7cSSebastian Grimberg       CeedInt     num_nodes;
1580437c7c90SJeremy L Thompson       CeedScalar *B_in = NULL, *identity = NULL;
1581ed9e99e6SJeremy L Thompson 
1582506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[b], &num_nodes));
1583352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_in[b], &B_in));
1584ed9e99e6SJeremy L Thompson 
1585437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) {
1586437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE);
1587ed9e99e6SJeremy L Thompson       }
1588ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1589352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1590352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1591352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1592ed9e99e6SJeremy L Thompson         }
1593ed9e99e6SJeremy L Thompson       }
1594ed9e99e6SJeremy L Thompson 
1595ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1596352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1597352a5e7cSSebastian Grimberg           CeedInt      d_in              = 0, q_comp_in;
1598352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE;
15991c66c397SJeremy L Thompson 
1600437c7c90SJeremy L Thompson           for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) {
1601437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_in[b] * q;
1602437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
16031c66c397SJeremy L Thompson 
1604506b1a0cSSebastian Grimberg             CeedCall(CeedOperatorGetBasisPointer(data->active_bases_in[b], data->eval_modes_in[b][e_in], identity, &B));
1605506b1a0cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_in[b], data->eval_modes_in[b][e_in], &q_comp_in));
1606352a5e7cSSebastian Grimberg             if (q_comp_in > 1) {
1607352a5e7cSSebastian Grimberg               if (e_in == 0 || data->eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0;
1608352a5e7cSSebastian Grimberg               else B = &B[(++d_in) * num_qpts * num_nodes];
1609352a5e7cSSebastian Grimberg             }
1610352a5e7cSSebastian Grimberg             eval_mode_in_prev                 = data->eval_modes_in[b][e_in];
1611352a5e7cSSebastian Grimberg             B_in[(qq + e_in) * num_nodes + n] = B[q * num_nodes + n];
1612ed9e99e6SJeremy L Thompson           }
1613ed9e99e6SJeremy L Thompson         }
1614ed9e99e6SJeremy L Thompson       }
16157c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1616437c7c90SJeremy L Thompson       data->assembled_bases_in[b] = B_in;
1617437c7c90SJeremy L Thompson     }
1618ed9e99e6SJeremy L Thompson   }
1619ed9e99e6SJeremy L Thompson 
1620437c7c90SJeremy L Thompson   if (assembled_bases_out && !data->assembled_bases_out[0]) {
1621437c7c90SJeremy L Thompson     CeedInt num_qpts;
1622437c7c90SJeremy L Thompson 
1623506b1a0cSSebastian Grimberg     if (data->active_bases_out[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[0], &num_qpts));
1624506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_out[0], &num_qpts));
1625506b1a0cSSebastian Grimberg     for (CeedInt b = 0; b < data->num_active_bases_out; b++) {
1626ed9e99e6SJeremy L Thompson       bool        has_eval_none = false;
16271c66c397SJeremy L Thompson       CeedInt     num_nodes;
1628437c7c90SJeremy L Thompson       CeedScalar *B_out = NULL, *identity = NULL;
1629ed9e99e6SJeremy L Thompson 
1630506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[b], &num_nodes));
1631352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_out[b], &B_out));
1632ed9e99e6SJeremy L Thompson 
1633437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) {
1634437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE);
1635ed9e99e6SJeremy L Thompson       }
1636ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1637352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1638352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1639352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1640ed9e99e6SJeremy L Thompson         }
1641ed9e99e6SJeremy L Thompson       }
1642ed9e99e6SJeremy L Thompson 
1643ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1644352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1645352a5e7cSSebastian Grimberg           CeedInt      d_out              = 0, q_comp_out;
1646352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
16471c66c397SJeremy L Thompson 
1648437c7c90SJeremy L Thompson           for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) {
1649437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_out[b] * q;
1650437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
16511c66c397SJeremy L Thompson 
1652506b1a0cSSebastian Grimberg             CeedCall(CeedOperatorGetBasisPointer(data->active_bases_out[b], data->eval_modes_out[b][e_out], identity, &B));
1653506b1a0cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_out[b], data->eval_modes_out[b][e_out], &q_comp_out));
1654352a5e7cSSebastian Grimberg             if (q_comp_out > 1) {
1655352a5e7cSSebastian Grimberg               if (e_out == 0 || data->eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0;
1656352a5e7cSSebastian Grimberg               else B = &B[(++d_out) * num_qpts * num_nodes];
1657352a5e7cSSebastian Grimberg             }
1658352a5e7cSSebastian Grimberg             eval_mode_out_prev                  = data->eval_modes_out[b][e_out];
1659352a5e7cSSebastian Grimberg             B_out[(qq + e_out) * num_nodes + n] = B[q * num_nodes + n];
1660ed9e99e6SJeremy L Thompson           }
1661ed9e99e6SJeremy L Thompson         }
1662ed9e99e6SJeremy L Thompson       }
16637c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1664437c7c90SJeremy L Thompson       data->assembled_bases_out[b] = B_out;
1665437c7c90SJeremy L Thompson     }
1666ed9e99e6SJeremy L Thompson   }
1667ed9e99e6SJeremy L Thompson 
1668437c7c90SJeremy L Thompson   // Pass out assembled data
1669506b1a0cSSebastian Grimberg   if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in;
1670506b1a0cSSebastian Grimberg   if (active_bases_in) *active_bases_in = data->active_bases_in;
1671437c7c90SJeremy L Thompson   if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in;
1672506b1a0cSSebastian Grimberg   if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out;
1673506b1a0cSSebastian Grimberg   if (active_bases_out) *active_bases_out = data->active_bases_out;
1674437c7c90SJeremy L Thompson   if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out;
1675437c7c90SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1676437c7c90SJeremy L Thompson }
1677437c7c90SJeremy L Thompson 
1678437c7c90SJeremy L Thompson /**
1679ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` `CeedBasis` data for assembly.
1680ba746a46SJeremy L Thompson 
1681ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1682437c7c90SJeremy L Thompson 
1683ca94c3ddSJeremy L Thompson   @param[in]  data                      `CeedOperatorAssemblyData`
1684ca94c3ddSJeremy L Thompson   @param[out] num_active_elem_rstrs_in  Number of active input element restrictions, or `NULL`
1685ca94c3ddSJeremy L Thompson   @param[out] active_elem_rstrs_in      Pointer to hold active input `CeedElemRestriction`, or `NULL`
1686ca94c3ddSJeremy L Thompson   @param[out] num_active_elem_rstrs_out Number of active output element restrictions, or `NULL`
1687ca94c3ddSJeremy L Thompson   @param[out] active_elem_rstrs_out     Pointer to hold active output `CeedElemRestriction`, or `NULL`
1688437c7c90SJeremy L Thompson 
1689437c7c90SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1690437c7c90SJeremy L Thompson 
1691437c7c90SJeremy L Thompson   @ref Backend
1692437c7c90SJeremy L Thompson **/
1693506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs_in,
1694506b1a0cSSebastian Grimberg                                                 CeedElemRestriction **active_elem_rstrs_in, CeedInt *num_active_elem_rstrs_out,
1695506b1a0cSSebastian Grimberg                                                 CeedElemRestriction **active_elem_rstrs_out) {
1696506b1a0cSSebastian Grimberg   if (num_active_elem_rstrs_in) *num_active_elem_rstrs_in = data->num_active_bases_in;
1697506b1a0cSSebastian Grimberg   if (active_elem_rstrs_in) *active_elem_rstrs_in = data->active_elem_rstrs_in;
1698506b1a0cSSebastian Grimberg   if (num_active_elem_rstrs_out) *num_active_elem_rstrs_out = data->num_active_bases_out;
1699506b1a0cSSebastian Grimberg   if (active_elem_rstrs_out) *active_elem_rstrs_out = data->active_elem_rstrs_out;
1700ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1701ed9e99e6SJeremy L Thompson }
1702ed9e99e6SJeremy L Thompson 
1703ed9e99e6SJeremy L Thompson /**
1704ca94c3ddSJeremy L Thompson   @brief Destroy `CeedOperatorAssemblyData`
1705ed9e99e6SJeremy L Thompson 
1706ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedOperatorAssemblyData` to destroy
1707ed9e99e6SJeremy L Thompson 
1708ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1709ed9e99e6SJeremy L Thompson 
1710ed9e99e6SJeremy L Thompson   @ref Backend
1711ed9e99e6SJeremy L Thompson **/
1712ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) {
1713ad6481ceSJeremy L Thompson   if (!*data) {
1714ad6481ceSJeremy L Thompson     *data = NULL;
1715ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1716ad6481ceSJeremy L Thompson   }
17172b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
1718506b1a0cSSebastian Grimberg   for (CeedInt b = 0; b < (*data)->num_active_bases_in; b++) {
1719506b1a0cSSebastian Grimberg     CeedCall(CeedBasisDestroy(&(*data)->active_bases_in[b]));
1720506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_in[b]));
1721437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_in[b]));
1722437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b]));
1723437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_in[b]));
1724506b1a0cSSebastian Grimberg   }
1725506b1a0cSSebastian Grimberg   for (CeedInt b = 0; b < (*data)->num_active_bases_out; b++) {
1726506b1a0cSSebastian Grimberg     CeedCall(CeedBasisDestroy(&(*data)->active_bases_out[b]));
1727506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_out[b]));
1728506b1a0cSSebastian Grimberg     CeedCall(CeedFree(&(*data)->eval_modes_out[b]));
1729506b1a0cSSebastian Grimberg     CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b]));
1730437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_out[b]));
1731437c7c90SJeremy L Thompson   }
1732506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_bases_in));
1733506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_bases_out));
1734506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_elem_rstrs_in));
1735506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_elem_rstrs_out));
1736437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_in));
1737437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_out));
1738437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_in));
1739437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_out));
1740437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_in));
1741437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_out));
1742437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_in));
1743437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_out));
1744ed9e99e6SJeremy L Thompson 
17452b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1746ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1747ed9e99e6SJeremy L Thompson }
1748ed9e99e6SJeremy L Thompson 
17494dd1a9d2SSebastian Grimberg /**
1750ca94c3ddSJeremy L Thompson   @brief Retrieve fallback `CeedOperator` with a reference `Ceed` for advanced `CeedOperator` functionality
17514dd1a9d2SSebastian Grimberg 
1752ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to retrieve fallback for
1753ca94c3ddSJeremy L Thompson   @param[out] op_fallback Fallback `CeedOperator`
17544dd1a9d2SSebastian Grimberg 
17554dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
17564dd1a9d2SSebastian Grimberg 
17574dd1a9d2SSebastian Grimberg   @ref Backend
17584dd1a9d2SSebastian Grimberg **/
17594dd1a9d2SSebastian Grimberg int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) {
17604dd1a9d2SSebastian Grimberg   // Create if needed
17614dd1a9d2SSebastian Grimberg   if (!op->op_fallback) CeedCall(CeedOperatorCreateFallback(op));
17624dd1a9d2SSebastian Grimberg   if (op->op_fallback) {
17634dd1a9d2SSebastian Grimberg     bool is_debug;
17641203703bSJeremy L Thompson     Ceed ceed;
17654dd1a9d2SSebastian Grimberg 
17664dd1a9d2SSebastian Grimberg     CeedCall(CeedOperatorGetCeed(op, &ceed));
17671203703bSJeremy L Thompson     CeedCall(CeedIsDebug(ceed, &is_debug));
17681203703bSJeremy L Thompson     if (is_debug) {
17691203703bSJeremy L Thompson       Ceed        ceed_fallback;
17701203703bSJeremy L Thompson       const char *resource, *resource_fallback;
17711203703bSJeremy L Thompson 
17724dd1a9d2SSebastian Grimberg       CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
17734dd1a9d2SSebastian Grimberg       CeedCall(CeedGetResource(ceed, &resource));
17744dd1a9d2SSebastian Grimberg       CeedCall(CeedGetResource(ceed_fallback, &resource_fallback));
17754dd1a9d2SSebastian Grimberg 
17764dd1a9d2SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n");
1777249f8407SJeremy L Thompson       CeedDebug(ceed, "Falling back from %s operator at address %p to %s operator at address %p\n", resource, op, resource_fallback, op->op_fallback);
17784dd1a9d2SSebastian Grimberg     }
17794dd1a9d2SSebastian Grimberg   }
17804dd1a9d2SSebastian Grimberg   *op_fallback = op->op_fallback;
17814dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
17824dd1a9d2SSebastian Grimberg }
17834dd1a9d2SSebastian Grimberg 
17844dd1a9d2SSebastian Grimberg /**
1785ca94c3ddSJeremy L Thompson   @brief Get the parent `CeedOperator` for a fallback `CeedOperator`
17864dd1a9d2SSebastian Grimberg 
1787ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` context
1788ca94c3ddSJeremy L Thompson   @param[out] parent Variable to store parent `CeedOperator` context
17894dd1a9d2SSebastian Grimberg 
17904dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
17914dd1a9d2SSebastian Grimberg 
17924dd1a9d2SSebastian Grimberg   @ref Backend
17934dd1a9d2SSebastian Grimberg **/
17944dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParent(CeedOperator op, CeedOperator *parent) {
17954dd1a9d2SSebastian Grimberg   *parent = op->op_fallback_parent ? op->op_fallback_parent : NULL;
17964dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
17974dd1a9d2SSebastian Grimberg }
17984dd1a9d2SSebastian Grimberg 
17994dd1a9d2SSebastian Grimberg /**
1800ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` context of the parent `CeedOperator` for a fallback `CeedOperator`
18014dd1a9d2SSebastian Grimberg 
1802ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` context
1803ca94c3ddSJeremy L Thompson   @param[out] parent Variable to store parent `Ceed` context
18044dd1a9d2SSebastian Grimberg 
18054dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
18064dd1a9d2SSebastian Grimberg 
18074dd1a9d2SSebastian Grimberg   @ref Backend
18084dd1a9d2SSebastian Grimberg **/
18094dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParentCeed(CeedOperator op, Ceed *parent) {
18104dd1a9d2SSebastian Grimberg   *parent = op->op_fallback_parent ? op->op_fallback_parent->ceed : op->ceed;
18114dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
18124dd1a9d2SSebastian Grimberg }
18134dd1a9d2SSebastian Grimberg 
1814480fae85SJeremy L Thompson /// @}
1815480fae85SJeremy L Thompson 
1816480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1817eaf62fffSJeremy L Thompson /// CeedOperator Public API
1818eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1819eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser
1820eaf62fffSJeremy L Thompson /// @{
1821eaf62fffSJeremy L Thompson 
1822eaf62fffSJeremy L Thompson /**
1823ca94c3ddSJeremy L Thompson   @brief Assemble a linear `CeedQFunction` associated with a `CeedOperator`.
1824eaf62fffSJeremy L Thompson 
1825ca94c3ddSJeremy L Thompson   This returns a `CeedVector` containing a matrix at each quadrature point providing the action of the `CeedQFunction` associated with the `CeedOperator`.
1826ca94c3ddSJeremy L Thompson   The vector `assembled` is of shape `[num_elements, num_input_fields, num_output_fields, num_quad_points]` and contains column-major matrices representing the action of the `CeedQFunction` for a corresponding quadrature point on an element.
1827859c15bbSJames Wright 
1828ca94c3ddSJeremy L Thompson   Inputs and outputs are in the order provided by the user when adding `CeedOperator` fields.
1829ca94c3ddSJeremy L Thompson   For example, a `CeedQFunction` with inputs `u` and `gradu` and outputs `gradv` and `v` , provided in that order, would result in an assembled `CeedQFunction` 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]`.
1830eaf62fffSJeremy L Thompson 
1831ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1832f04ea552SJeremy L Thompson 
1833ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1834ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points
1835ca94c3ddSJeremy L Thompson   @param[out] rstr      `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1836ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1837eaf62fffSJeremy L Thompson 
1838eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1839eaf62fffSJeremy L Thompson 
1840eaf62fffSJeremy L Thompson   @ref User
1841eaf62fffSJeremy L Thompson **/
18422b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
18432b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1844eaf62fffSJeremy L Thompson 
1845eaf62fffSJeremy L Thompson   if (op->LinearAssembleQFunction) {
1846d04bbc78SJeremy L Thompson     // Backend version
18472b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request));
1848eaf62fffSJeremy L Thompson   } else {
1849d04bbc78SJeremy L Thompson     // Operator fallback
18501203703bSJeremy L Thompson     Ceed         ceed;
1851d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1852d04bbc78SJeremy L Thompson 
18531203703bSJeremy L Thompson     CeedCall(CeedOperatorGetCeed(op, &ceed));
18542b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
18556574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request));
18561203703bSJeremy L Thompson     else return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction");
185770a7ffb3SJeremy L Thompson   }
1858eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1859eaf62fffSJeremy L Thompson }
186070a7ffb3SJeremy L Thompson 
186170a7ffb3SJeremy L Thompson /**
1862ca94c3ddSJeremy L Thompson   @brief Assemble `CeedQFunction` and store result internally.
18634385fb7fSSebastian Grimberg 
1864ea61e9acSJeremy L Thompson   Return copied references of stored data to the caller.
1865ea61e9acSJeremy L Thompson   Caller is responsible for ownership and destruction of the copied references.
1866ca94c3ddSJeremy L Thompson   See also @ref CeedOperatorLinearAssembleQFunction().
186770a7ffb3SJeremy L Thompson 
1868ca94c3ddSJeremy 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.
1869c5f45aeaSJeremy L Thompson         These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object.
1870c5f45aeaSJeremy L Thompson 
1871ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1872ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points
1873ca94c3ddSJeremy L Thompson   @param[out] rstr      `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1874ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
187570a7ffb3SJeremy L Thompson 
187670a7ffb3SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
187770a7ffb3SJeremy L Thompson 
187870a7ffb3SJeremy L Thompson   @ref User
187970a7ffb3SJeremy L Thompson **/
18802b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
1881b05f7e9fSJeremy L Thompson   int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *) = NULL;
1882b05f7e9fSJeremy L Thompson   CeedOperator op_assemble                                                                           = NULL;
1883bb229da9SJeremy L Thompson   CeedOperator op_fallback_parent                                                                    = NULL;
1884b05f7e9fSJeremy L Thompson 
18852b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
188670a7ffb3SJeremy L Thompson 
1887b05f7e9fSJeremy L Thompson   // Determine if fallback parent or operator has implementation
1888bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParent(op, &op_fallback_parent));
1889bb229da9SJeremy L Thompson   if (op_fallback_parent && op_fallback_parent->LinearAssembleQFunctionUpdate) {
1890b05f7e9fSJeremy L Thompson     // -- Backend version for op fallback parent is faster, if it exists
1891bb229da9SJeremy L Thompson     LinearAssembleQFunctionUpdate = op_fallback_parent->LinearAssembleQFunctionUpdate;
1892bb229da9SJeremy L Thompson     op_assemble                   = op_fallback_parent;
1893b05f7e9fSJeremy L Thompson   } else if (op->LinearAssembleQFunctionUpdate) {
1894b05f7e9fSJeremy L Thompson     // -- Backend version for op
1895b05f7e9fSJeremy L Thompson     LinearAssembleQFunctionUpdate = op->LinearAssembleQFunctionUpdate;
1896b05f7e9fSJeremy L Thompson     op_assemble                   = op;
1897b05f7e9fSJeremy L Thompson   }
1898b05f7e9fSJeremy L Thompson 
1899b05f7e9fSJeremy L Thompson   // Assemble QFunction
1900b05f7e9fSJeremy L Thompson   if (LinearAssembleQFunctionUpdate) {
1901b05f7e9fSJeremy L Thompson     // Backend or fallback parent version
1902*7d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
1903*7d5185d7SSebastian Grimberg     bool                      data_is_setup;
19042efa2d85SJeremy L Thompson     CeedVector                assembled_vec  = NULL;
19052efa2d85SJeremy L Thompson     CeedElemRestriction       assembled_rstr = NULL;
1906480fae85SJeremy L Thompson 
1907*7d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
1908*7d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataIsSetup(data, &data_is_setup));
1909*7d5185d7SSebastian Grimberg     if (data_is_setup) {
1910d04bbc78SJeremy L Thompson       bool update_needed;
1911d04bbc78SJeremy L Thompson 
1912*7d5185d7SSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataGetObjects(data, &assembled_vec, &assembled_rstr));
1913*7d5185d7SSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(data, &update_needed));
1914b05f7e9fSJeremy L Thompson       if (update_needed) CeedCall(LinearAssembleQFunctionUpdate(op_assemble, assembled_vec, assembled_rstr, request));
191570a7ffb3SJeremy L Thompson     } else {
1916b05f7e9fSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunction(op_assemble, &assembled_vec, &assembled_rstr, request));
1917*7d5185d7SSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataSetObjects(data, assembled_vec, assembled_rstr));
191870a7ffb3SJeremy L Thompson     }
1919*7d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(data, false));
19202efa2d85SJeremy L Thompson 
1921d04bbc78SJeremy L Thompson     // Copy reference from internally held copy
19222b730f8bSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled));
19232b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr));
1924c5f45aeaSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled_vec));
19252b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&assembled_rstr));
192670a7ffb3SJeremy L Thompson   } else {
1927d04bbc78SJeremy L Thompson     // Operator fallback
19281203703bSJeremy L Thompson     Ceed         ceed;
1929d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1930d04bbc78SJeremy L Thompson 
19311203703bSJeremy L Thompson     CeedCall(CeedOperatorGetCeed(op, &ceed));
19322b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
19336574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request));
19341203703bSJeremy L Thompson     else return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate");
193570a7ffb3SJeremy L Thompson   }
193670a7ffb3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1937eaf62fffSJeremy L Thompson }
1938eaf62fffSJeremy L Thompson 
1939eaf62fffSJeremy L Thompson /**
1940ca94c3ddSJeremy L Thompson   @brief Assemble the diagonal of a square linear `CeedOperator`
1941eaf62fffSJeremy L Thompson 
1942ca94c3ddSJeremy L Thompson   This overwrites a `CeedVector` with the diagonal of a linear `CeedOperator`.
1943eaf62fffSJeremy L Thompson 
1944ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
1945eaf62fffSJeremy L Thompson 
1946ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1947f04ea552SJeremy L Thompson 
1948ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1949ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal
1950ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1951eaf62fffSJeremy L Thompson 
1952eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1953eaf62fffSJeremy L Thompson 
1954eaf62fffSJeremy L Thompson   @ref User
1955eaf62fffSJeremy L Thompson **/
19562b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1957f3d47e36SJeremy L Thompson   bool     is_composite;
19581c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
19591203703bSJeremy L Thompson   Ceed     ceed;
19601c66c397SJeremy L Thompson 
19611203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
19622b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1963f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1964eaf62fffSJeremy L Thompson 
19652b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
19661203703bSJeremy L Thompson   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1967c9366a6bSJeremy L Thompson 
1968f3d47e36SJeremy L Thompson   // Early exit for empty operator
1969f3d47e36SJeremy L Thompson   if (!is_composite) {
1970f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1971f3d47e36SJeremy L Thompson 
1972f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1973f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1974f3d47e36SJeremy L Thompson   }
1975f3d47e36SJeremy L Thompson 
1976eaf62fffSJeremy L Thompson   if (op->LinearAssembleDiagonal) {
1977d04bbc78SJeremy L Thompson     // Backend version
19782b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleDiagonal(op, assembled, request));
1979eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1980eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddDiagonal) {
1981d04bbc78SJeremy L Thompson     // Backend version with zeroing first
19822b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
19832b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1984eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1985eaf62fffSJeremy L Thompson   } else {
1986d04bbc78SJeremy L Thompson     // Operator fallback
1987d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1988d04bbc78SJeremy L Thompson 
19892b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1990d04bbc78SJeremy L Thompson     if (op_fallback) {
19912b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request));
1992eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1993eaf62fffSJeremy L Thompson     }
1994eaf62fffSJeremy L Thompson   }
1995eaf62fffSJeremy L Thompson   // Default interface implementation
19962b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
19972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request));
1998eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1999eaf62fffSJeremy L Thompson }
2000eaf62fffSJeremy L Thompson 
2001eaf62fffSJeremy L Thompson /**
2002ca94c3ddSJeremy L Thompson   @brief Assemble the diagonal of a square linear `CeedOperator`.
2003eaf62fffSJeremy L Thompson 
2004ca94c3ddSJeremy L Thompson   This sums into a `CeedVector` the diagonal of a linear `CeedOperator`.
2005eaf62fffSJeremy L Thompson 
2006ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2007eaf62fffSJeremy L Thompson 
2008ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2009f04ea552SJeremy L Thompson 
2010ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2011ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal
2012ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2013eaf62fffSJeremy L Thompson 
2014eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2015eaf62fffSJeremy L Thompson 
2016eaf62fffSJeremy L Thompson   @ref User
2017eaf62fffSJeremy L Thompson **/
20182b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2019f3d47e36SJeremy L Thompson   bool     is_composite;
20201c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
20211203703bSJeremy L Thompson   Ceed     ceed;
20221c66c397SJeremy L Thompson 
20231203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
20242b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2025f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2026eaf62fffSJeremy L Thompson 
20272b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
20281203703bSJeremy L Thompson   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
2029c9366a6bSJeremy L Thompson 
2030f3d47e36SJeremy L Thompson   // Early exit for empty operator
2031f3d47e36SJeremy L Thompson   if (!is_composite) {
2032f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2033f3d47e36SJeremy L Thompson 
2034f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2035f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2036f3d47e36SJeremy L Thompson   }
2037f3d47e36SJeremy L Thompson 
2038eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddDiagonal) {
2039d04bbc78SJeremy L Thompson     // Backend version
20402b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
2041eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2042eaf62fffSJeremy L Thompson   } else {
2043d04bbc78SJeremy L Thompson     // Operator fallback
2044d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2045d04bbc78SJeremy L Thompson 
20462b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2047d04bbc78SJeremy L Thompson     if (op_fallback) {
20482b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
2049eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2050eaf62fffSJeremy L Thompson     }
2051eaf62fffSJeremy L Thompson   }
2052eaf62fffSJeremy L Thompson   // Default interface implementation
2053eaf62fffSJeremy L Thompson   if (is_composite) {
20542b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
2055eaf62fffSJeremy L Thompson   } else {
2056f3bd9308SJeremy L Thompson     CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
2057eaf62fffSJeremy L Thompson   }
2058d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2059eaf62fffSJeremy L Thompson }
2060eaf62fffSJeremy L Thompson 
2061eaf62fffSJeremy L Thompson /**
2062ca94c3ddSJeremy L Thompson    @brief Fully assemble the point-block diagonal pattern of a linear `CeedOperator`.
206301f0e615SJames Wright 
2064ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssemblePointBlockDiagonal().
206501f0e615SJames Wright 
2066ca94c3ddSJeremy 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 matrix in entry `(i, j)`.
2067ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are unique.
2068ca94c3ddSJeremy L Thompson    This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemblePointBlockDiagonal() provides the values in the same ordering.
206901f0e615SJames Wright 
207001f0e615SJames Wright    This will generally be slow unless your operator is low-order.
207101f0e615SJames Wright 
2072ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
207301f0e615SJames Wright 
2074ca94c3ddSJeremy L Thompson    @param[in]  op          `CeedOperator` to assemble
207501f0e615SJames Wright    @param[out] num_entries Number of entries in coordinate nonzero pattern
207601f0e615SJames Wright    @param[out] rows        Row number for each entry
207701f0e615SJames Wright    @param[out] cols        Column number for each entry
207801f0e615SJames Wright 
207901f0e615SJames Wright    @ref User
208001f0e615SJames Wright **/
208101f0e615SJames Wright int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
208201f0e615SJames Wright   Ceed          ceed;
208301f0e615SJames Wright   bool          is_composite;
208401f0e615SJames Wright   CeedInt       num_active_components, num_sub_operators;
208501f0e615SJames Wright   CeedOperator *sub_operators;
208601f0e615SJames Wright 
208701f0e615SJames Wright   CeedCall(CeedOperatorGetCeed(op, &ceed));
208801f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
208901f0e615SJames Wright 
209001f0e615SJames Wright   CeedSize input_size = 0, output_size = 0;
209101f0e615SJames Wright   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
209201f0e615SJames Wright   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
209301f0e615SJames Wright 
209401f0e615SJames Wright   if (is_composite) {
209501f0e615SJames Wright     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_operators));
209601f0e615SJames Wright     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
209701f0e615SJames Wright   } else {
209801f0e615SJames Wright     sub_operators     = &op;
209901f0e615SJames Wright     num_sub_operators = 1;
210001f0e615SJames Wright   }
210101f0e615SJames Wright 
2102506b1a0cSSebastian Grimberg   // Verify operator can be assembled correctly
2103506b1a0cSSebastian Grimberg   {
210401f0e615SJames Wright     CeedOperatorAssemblyData data;
2105506b1a0cSSebastian Grimberg     CeedInt                  num_active_elem_rstrs, comp_stride;
210601f0e615SJames Wright     CeedElemRestriction     *active_elem_rstrs;
210701f0e615SJames Wright 
210801f0e615SJames Wright     // Get initial values to check against
210901f0e615SJames Wright     CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[0], &data));
2110506b1a0cSSebastian Grimberg     CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL));
211101f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[0], &comp_stride));
211201f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[0], &num_active_components));
211301f0e615SJames Wright 
2114506b1a0cSSebastian Grimberg     // Verify that all active element restrictions have same component stride and number of components
211501f0e615SJames Wright     for (CeedInt k = 0; k < num_sub_operators; k++) {
211601f0e615SJames Wright       CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[k], &data));
2117506b1a0cSSebastian Grimberg       CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL));
211801f0e615SJames Wright       for (CeedInt i = 0; i < num_active_elem_rstrs; i++) {
2119506b1a0cSSebastian Grimberg         CeedInt comp_stride_sub, num_active_components_sub;
2120506b1a0cSSebastian Grimberg 
212101f0e615SJames Wright         CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[i], &comp_stride_sub));
212201f0e615SJames Wright         CeedCheck(comp_stride == comp_stride_sub, ceed, CEED_ERROR_DIMENSION,
212301f0e615SJames Wright                   "Active element restrictions must have the same component stride: %d vs %d", comp_stride, comp_stride_sub);
212401f0e615SJames Wright         CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[i], &num_active_components_sub));
212501f0e615SJames Wright         CeedCheck(num_active_components == num_active_components_sub, ceed, CEED_ERROR_INCOMPATIBLE,
212601f0e615SJames Wright                   "All suboperators must have the same number of output components");
212701f0e615SJames Wright       }
212801f0e615SJames Wright     }
212901f0e615SJames Wright   }
213001f0e615SJames Wright   *num_entries = input_size * num_active_components;
213101f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, rows));
213201f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, cols));
213301f0e615SJames Wright 
213401f0e615SJames Wright   for (CeedInt o = 0; o < num_sub_operators; o++) {
2135506b1a0cSSebastian Grimberg     CeedElemRestriction active_elem_rstr, point_block_active_elem_rstr;
213601f0e615SJames Wright     CeedInt             comp_stride, num_elem, elem_size;
2137506b1a0cSSebastian Grimberg     const CeedInt      *offsets, *point_block_offsets;
213801f0e615SJames Wright 
213901f0e615SJames Wright     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[o], &active_elem_rstr));
214001f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstr, &comp_stride));
214101f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumElements(active_elem_rstr, &num_elem));
214201f0e615SJames Wright     CeedCall(CeedElemRestrictionGetElementSize(active_elem_rstr, &elem_size));
214301f0e615SJames Wright     CeedCall(CeedElemRestrictionGetOffsets(active_elem_rstr, CEED_MEM_HOST, &offsets));
214401f0e615SJames Wright 
2145506b1a0cSSebastian Grimberg     CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstr, &point_block_active_elem_rstr));
2146506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOffsets(point_block_active_elem_rstr, CEED_MEM_HOST, &point_block_offsets));
214701f0e615SJames Wright 
214801f0e615SJames Wright     for (CeedSize i = 0; i < num_elem * elem_size; i++) {
214901f0e615SJames Wright       for (CeedInt c_out = 0; c_out < num_active_components; c_out++) {
215001f0e615SJames Wright         for (CeedInt c_in = 0; c_in < num_active_components; c_in++) {
2151506b1a0cSSebastian Grimberg           (*rows)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_out * comp_stride;
2152506b1a0cSSebastian Grimberg           (*cols)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_in * comp_stride;
215301f0e615SJames Wright         }
215401f0e615SJames Wright       }
215501f0e615SJames Wright     }
215601f0e615SJames Wright 
215701f0e615SJames Wright     CeedCall(CeedElemRestrictionRestoreOffsets(active_elem_rstr, &offsets));
2158506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOffsets(point_block_active_elem_rstr, &point_block_offsets));
2159506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&point_block_active_elem_rstr));
216001f0e615SJames Wright   }
216101f0e615SJames Wright   return CEED_ERROR_SUCCESS;
216201f0e615SJames Wright }
216301f0e615SJames Wright 
216401f0e615SJames Wright /**
2165ca94c3ddSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear `CeedOperator`.
2166eaf62fffSJeremy L Thompson 
2167ca94c3ddSJeremy L Thompson   This overwrites a `CeedVector` with the point block diagonal of a linear `CeedOperator`.
2168eaf62fffSJeremy L Thompson 
2169ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2170eaf62fffSJeremy L Thompson 
2171ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2172f04ea552SJeremy L Thompson 
2173ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2174ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedOperator` point block diagonal, provided in row-major form with an `num_comp * num_comp` block at each node.
2175ca94c3ddSJeremy L Thompson                           The dimensions of this vector are derived from the active vector for the `CeedOperator`.
2176ca94c3ddSJeremy L Thompson                           The array has shape `[nodes, component out, component in]`.
2177ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2178eaf62fffSJeremy L Thompson 
2179eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2180eaf62fffSJeremy L Thompson 
2181eaf62fffSJeremy L Thompson   @ref User
2182eaf62fffSJeremy L Thompson **/
21832b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2184f3d47e36SJeremy L Thompson   bool     is_composite;
21851c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
21861203703bSJeremy L Thompson   Ceed     ceed;
21871c66c397SJeremy L Thompson 
21881203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
21892b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2190f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2191eaf62fffSJeremy L Thompson 
21922b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
21931203703bSJeremy L Thompson   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
2194c9366a6bSJeremy L Thompson 
2195f3d47e36SJeremy L Thompson   // Early exit for empty operator
2196f3d47e36SJeremy L Thompson   if (!is_composite) {
2197f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2198f3d47e36SJeremy L Thompson 
2199f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2200f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2201f3d47e36SJeremy L Thompson   }
2202f3d47e36SJeremy L Thompson 
2203eaf62fffSJeremy L Thompson   if (op->LinearAssemblePointBlockDiagonal) {
2204d04bbc78SJeremy L Thompson     // Backend version
22052b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request));
2206eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2207eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddPointBlockDiagonal) {
2208d04bbc78SJeremy L Thompson     // Backend version with zeroing first
22092b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
22102b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
2211eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2212eaf62fffSJeremy L Thompson   } else {
2213d04bbc78SJeremy L Thompson     // Operator fallback
2214d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2215d04bbc78SJeremy L Thompson 
22162b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2217d04bbc78SJeremy L Thompson     if (op_fallback) {
22182b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request));
2219eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2220eaf62fffSJeremy L Thompson     }
2221eaf62fffSJeremy L Thompson   }
2222eaf62fffSJeremy L Thompson   // Default interface implementation
22232b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
22242b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
2225eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2226eaf62fffSJeremy L Thompson }
2227eaf62fffSJeremy L Thompson 
2228eaf62fffSJeremy L Thompson /**
2229ca94c3ddSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear `CeedOperator`.
2230eaf62fffSJeremy L Thompson 
2231ca94c3ddSJeremy L Thompson   This sums into a `CeedVector` with the point block diagonal of a linear `CeedOperator`.
2232eaf62fffSJeremy L Thompson 
2233ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2234eaf62fffSJeremy L Thompson 
2235ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2236f04ea552SJeremy L Thompson 
2237ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2238ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled CeedOperator point block diagonal, provided in row-major form with an `num_comp * num_comp` block at each node.
2239ca94c3ddSJeremy L Thompson                           The dimensions of this vector are derived from the active vector for the `CeedOperator`.
2240ca94c3ddSJeremy L Thompson                           The array has shape `[nodes, component out, component in]`.
2241ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2242eaf62fffSJeremy L Thompson 
2243eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2244eaf62fffSJeremy L Thompson 
2245eaf62fffSJeremy L Thompson   @ref User
2246eaf62fffSJeremy L Thompson **/
22472b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2248f3d47e36SJeremy L Thompson   bool     is_composite;
22491c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
22501203703bSJeremy L Thompson   Ceed     ceed;
22511c66c397SJeremy L Thompson 
22521203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
22532b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2254f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2255eaf62fffSJeremy L Thompson 
22562b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
22571203703bSJeremy L Thompson   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
2258c9366a6bSJeremy L Thompson 
2259f3d47e36SJeremy L Thompson   // Early exit for empty operator
2260f3d47e36SJeremy L Thompson   if (!is_composite) {
2261f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2262f3d47e36SJeremy L Thompson 
2263f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2264f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2265f3d47e36SJeremy L Thompson   }
2266f3d47e36SJeremy L Thompson 
2267eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddPointBlockDiagonal) {
2268d04bbc78SJeremy L Thompson     // Backend version
22692b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request));
2270eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2271eaf62fffSJeremy L Thompson   } else {
2272d04bbc78SJeremy L Thompson     // Operator fallback
2273d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2274d04bbc78SJeremy L Thompson 
22752b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2276d04bbc78SJeremy L Thompson     if (op_fallback) {
22772b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request));
2278eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2279eaf62fffSJeremy L Thompson     }
2280eaf62fffSJeremy L Thompson   }
2281ea61e9acSJeremy L Thompson   // Default interface implementation
2282eaf62fffSJeremy L Thompson   if (is_composite) {
22832b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
2284eaf62fffSJeremy L Thompson   } else {
2285f3bd9308SJeremy L Thompson     CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
2286eaf62fffSJeremy L Thompson   }
2287d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2288eaf62fffSJeremy L Thompson }
2289eaf62fffSJeremy L Thompson 
2290eaf62fffSJeremy L Thompson /**
2291ca94c3ddSJeremy L Thompson    @brief Fully assemble the nonzero pattern of a linear `CeedOperator`.
2292eaf62fffSJeremy L Thompson 
2293ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssemble().
2294eaf62fffSJeremy L Thompson 
2295ca94c3ddSJeremy 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 matrix in entry `(i, j)`.
2296ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are not unique and may repeat.
2297ca94c3ddSJeremy L Thompson    This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemble() provides the values in the same ordering.
2298eaf62fffSJeremy L Thompson 
2299eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2300eaf62fffSJeremy L Thompson 
2301ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2302f04ea552SJeremy L Thompson 
2303ca94c3ddSJeremy L Thompson    @param[in]  op          `CeedOperator` to assemble
2304eaf62fffSJeremy L Thompson    @param[out] num_entries Number of entries in coordinate nonzero pattern
2305eaf62fffSJeremy L Thompson    @param[out] rows        Row number for each entry
2306eaf62fffSJeremy L Thompson    @param[out] cols        Column number for each entry
2307eaf62fffSJeremy L Thompson 
2308eaf62fffSJeremy L Thompson    @ref User
2309eaf62fffSJeremy L Thompson **/
23102b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
23111c66c397SJeremy L Thompson   bool          is_composite;
23121c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2313b94338b9SJed Brown   CeedSize      single_entries;
2314eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
23151c66c397SJeremy L Thompson 
23162b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2317f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2318eaf62fffSJeremy L Thompson 
2319eaf62fffSJeremy L Thompson   if (op->LinearAssembleSymbolic) {
2320d04bbc78SJeremy L Thompson     // Backend version
23212b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols));
2322eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2323eaf62fffSJeremy L Thompson   } else {
2324d04bbc78SJeremy L Thompson     // Operator fallback
2325d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2326d04bbc78SJeremy L Thompson 
23272b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2328d04bbc78SJeremy L Thompson     if (op_fallback) {
23292b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols));
2330eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2331eaf62fffSJeremy L Thompson     }
2332eaf62fffSJeremy L Thompson   }
2333eaf62fffSJeremy L Thompson 
2334eaf62fffSJeremy L Thompson   // Default interface implementation
2335eaf62fffSJeremy L Thompson 
2336506b1a0cSSebastian Grimberg   // Count entries and allocate rows, cols arrays
2337eaf62fffSJeremy L Thompson   *num_entries = 0;
2338eaf62fffSJeremy L Thompson   if (is_composite) {
2339c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2340c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
234192ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
23422b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2343eaf62fffSJeremy L Thompson       *num_entries += single_entries;
2344eaf62fffSJeremy L Thompson     }
2345eaf62fffSJeremy L Thompson   } else {
23462b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries));
2347eaf62fffSJeremy L Thompson     *num_entries += single_entries;
2348eaf62fffSJeremy L Thompson   }
23492b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, rows));
23502b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, cols));
2351eaf62fffSJeremy L Thompson 
2352506b1a0cSSebastian Grimberg   // Assemble nonzero locations
2353eaf62fffSJeremy L Thompson   if (is_composite) {
2354c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2355c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
235692ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
23572b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols));
23582b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2359eaf62fffSJeremy L Thompson       offset += single_entries;
2360eaf62fffSJeremy L Thompson     }
2361eaf62fffSJeremy L Thompson   } else {
23622b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols));
2363eaf62fffSJeremy L Thompson   }
2364eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2365eaf62fffSJeremy L Thompson }
2366eaf62fffSJeremy L Thompson 
2367eaf62fffSJeremy L Thompson /**
2368eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero entries of a linear operator.
2369eaf62fffSJeremy L Thompson 
2370ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssembleSymbolic().
2371eaf62fffSJeremy L Thompson 
2372ca94c3ddSJeremy 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 matrix in entry `(i, j)`.
2373ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are not unique and may repeat.
2374ca94c3ddSJeremy L Thompson    This function returns the values of the nonzero entries to be added, their `(i, j)` locations are provided by @ref CeedOperatorLinearAssembleSymbolic().
2375eaf62fffSJeremy L Thompson 
2376eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2377eaf62fffSJeremy L Thompson 
2378ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2379f04ea552SJeremy L Thompson 
2380ca94c3ddSJeremy L Thompson    @param[in]  op     `CeedOperator` to assemble
2381eaf62fffSJeremy L Thompson    @param[out] values Values to assemble into matrix
2382eaf62fffSJeremy L Thompson 
2383eaf62fffSJeremy L Thompson    @ref User
2384eaf62fffSJeremy L Thompson **/
2385eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) {
23861c66c397SJeremy L Thompson   bool          is_composite;
23871c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2388b94338b9SJed Brown   CeedSize      single_entries = 0;
2389eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
23901c66c397SJeremy L Thompson 
23912b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2392f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2393f3d47e36SJeremy L Thompson 
2394f3d47e36SJeremy L Thompson   // Early exit for empty operator
2395f3d47e36SJeremy L Thompson   if (!is_composite) {
2396f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2397f3d47e36SJeremy L Thompson 
2398f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2399f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2400f3d47e36SJeremy L Thompson   }
2401eaf62fffSJeremy L Thompson 
2402eaf62fffSJeremy L Thompson   if (op->LinearAssemble) {
2403d04bbc78SJeremy L Thompson     // Backend version
24042b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemble(op, values));
2405eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2406eaf62fffSJeremy L Thompson   } else {
2407d04bbc78SJeremy L Thompson     // Operator fallback
2408d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2409d04bbc78SJeremy L Thompson 
24102b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2411d04bbc78SJeremy L Thompson     if (op_fallback) {
24122b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemble(op_fallback, values));
2413eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2414eaf62fffSJeremy L Thompson     }
2415eaf62fffSJeremy L Thompson   }
2416eaf62fffSJeremy L Thompson 
2417eaf62fffSJeremy L Thompson   // Default interface implementation
241828ec399dSJeremy L Thompson   CeedCall(CeedVectorSetValue(values, 0.0));
2419eaf62fffSJeremy L Thompson   if (is_composite) {
2420c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2421c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2422cefa2673SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; k++) {
24232b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values));
24242b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2425eaf62fffSJeremy L Thompson       offset += single_entries;
2426eaf62fffSJeremy L Thompson     }
2427eaf62fffSJeremy L Thompson   } else {
24282b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemble(op, offset, values));
2429eaf62fffSJeremy L Thompson   }
2430eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2431eaf62fffSJeremy L Thompson }
2432eaf62fffSJeremy L Thompson 
2433eaf62fffSJeremy L Thompson /**
2434ca94c3ddSJeremy L Thompson   @brief Get the multiplicity of nodes across sub-operators in a composite `CeedOperator`.
243575f0d5a4SJeremy L Thompson 
2436ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
243775f0d5a4SJeremy L Thompson 
2438ca94c3ddSJeremy L Thompson   @param[in]  op               Composite `CeedOperator`
2439ca94c3ddSJeremy L Thompson   @param[in]  num_skip_indices Number of sub-operators to skip
2440ca94c3ddSJeremy L Thompson   @param[in]  skip_indices     Array of indices of sub-operators to skip
2441ca94c3ddSJeremy L Thompson   @param[out] mult             Vector to store multiplicity (of size `l_size` )
244275f0d5a4SJeremy L Thompson 
244375f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
244475f0d5a4SJeremy L Thompson 
244575f0d5a4SJeremy L Thompson   @ref User
244675f0d5a4SJeremy L Thompson **/
244775f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) {
244875f0d5a4SJeremy L Thompson   Ceed                ceed;
2449b275c451SJeremy L Thompson   CeedInt             num_suboperators;
245075f0d5a4SJeremy L Thompson   CeedSize            l_vec_len;
245175f0d5a4SJeremy L Thompson   CeedScalar         *mult_array;
245275f0d5a4SJeremy L Thompson   CeedVector          ones_l_vec;
24537c1dbaffSSebastian Grimberg   CeedElemRestriction elem_rstr, mult_elem_rstr;
2454b275c451SJeremy L Thompson   CeedOperator       *sub_operators;
245575f0d5a4SJeremy L Thompson 
24561c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
24571c66c397SJeremy L Thompson 
245875f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
245975f0d5a4SJeremy L Thompson 
246075f0d5a4SJeremy L Thompson   // Zero mult vector
246175f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
246275f0d5a4SJeremy L Thompson 
246375f0d5a4SJeremy L Thompson   // Get suboperators
2464b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2465b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2466b275c451SJeremy L Thompson   if (num_suboperators == 0) return CEED_ERROR_SUCCESS;
246775f0d5a4SJeremy L Thompson 
246875f0d5a4SJeremy L Thompson   // Work vector
246975f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetLength(mult, &l_vec_len));
247075f0d5a4SJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec));
247175f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(ones_l_vec, 1.0));
247275f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array));
247375f0d5a4SJeremy L Thompson 
247475f0d5a4SJeremy L Thompson   // Compute multiplicity across suboperators
2475b275c451SJeremy L Thompson   for (CeedInt i = 0; i < num_suboperators; i++) {
247675f0d5a4SJeremy L Thompson     const CeedScalar *sub_mult_array;
247775f0d5a4SJeremy L Thompson     CeedVector        sub_mult_l_vec, ones_e_vec;
247875f0d5a4SJeremy L Thompson 
247975f0d5a4SJeremy L Thompson     // -- Check for suboperator to skip
248075f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < num_skip_indices; j++) {
248175f0d5a4SJeremy L Thompson       if (skip_indices[j] == i) continue;
248275f0d5a4SJeremy L Thompson     }
248375f0d5a4SJeremy L Thompson 
248475f0d5a4SJeremy L Thompson     // -- Sub operator multiplicity
2485437c7c90SJeremy L Thompson     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr));
24867c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr, &mult_elem_rstr));
24877c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateVector(mult_elem_rstr, &sub_mult_l_vec, &ones_e_vec));
248875f0d5a4SJeremy L Thompson     CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0));
24897c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE));
24907c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE));
249175f0d5a4SJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array));
249275f0d5a4SJeremy L Thompson     // ---- Flag every node present in the current suboperator
249375f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < l_vec_len; j++) {
249475f0d5a4SJeremy L Thompson       if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0;
249575f0d5a4SJeremy L Thompson     }
249675f0d5a4SJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array));
249775f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&sub_mult_l_vec));
249875f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&ones_e_vec));
24997c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&mult_elem_rstr));
250075f0d5a4SJeremy L Thompson   }
250175f0d5a4SJeremy L Thompson   CeedCall(CeedVectorRestoreArray(mult, &mult_array));
2502811d0ccfSJeremy L Thompson   CeedCall(CeedVectorDestroy(&ones_l_vec));
250375f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
250475f0d5a4SJeremy L Thompson }
250575f0d5a4SJeremy L Thompson 
250675f0d5a4SJeremy L Thompson /**
2507ca94c3ddSJeremy L Thompson   @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator`, creating the prolongation basis from the fine and coarse grid interpolation.
2508eaf62fffSJeremy L Thompson 
2509ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2510f04ea552SJeremy L Thompson 
2511ca94c3ddSJeremy L Thompson   @param[in]  op_fine      Fine grid `CeedOperator`
2512ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2513ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid `CeedElemRestriction`
2514ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector `CeedBasis`
2515ca94c3ddSJeremy L Thompson   @param[out] op_coarse    Coarse grid `CeedOperator`
2516ca94c3ddSJeremy L Thompson   @param[out] op_prolong   Coarse to fine `CeedOperator`, or `NULL`
2517ca94c3ddSJeremy L Thompson   @param[out] op_restrict  Fine to coarse `CeedOperator`, or `NULL`
2518eaf62fffSJeremy L Thompson 
2519eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2520eaf62fffSJeremy L Thompson 
2521eaf62fffSJeremy L Thompson   @ref User
2522eaf62fffSJeremy L Thompson **/
25232b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
25247758292fSSebastian Grimberg                                      CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
25251c66c397SJeremy L Thompson   CeedBasis basis_c_to_f = NULL;
25261c66c397SJeremy L Thompson 
25272b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2528eaf62fffSJeremy L Thompson 
252983d6adf3SZach Atkins   // Build prolongation matrix, if required
25307758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
253183d6adf3SZach Atkins     CeedBasis basis_fine;
25321c66c397SJeremy L Thompson 
25332b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
25342b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f));
253583d6adf3SZach Atkins   }
2536eaf62fffSJeremy L Thompson 
2537f113e5dcSJeremy L Thompson   // Core code
25387758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2539eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2540eaf62fffSJeremy L Thompson }
2541eaf62fffSJeremy L Thompson 
2542eaf62fffSJeremy L Thompson /**
2543ca94c3ddSJeremy L Thompson   @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a tensor basis for the active basis.
2544eaf62fffSJeremy L Thompson 
2545ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2546f04ea552SJeremy L Thompson 
2547ca94c3ddSJeremy L Thompson   @param[in]  op_fine       Fine grid `CeedOperator`
2548ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2549ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid `CeedElemRestriction`
2550ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector `CeedBasis`
2551ca94c3ddSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator`
2552ca94c3ddSJeremy L Thompson   @param[out] op_coarse     Coarse grid `CeedOperator`
2553ca94c3ddSJeremy L Thompson   @param[out] op_prolong    Coarse to fine `CeedOperator`, or `NULL`
2554ca94c3ddSJeremy L Thompson   @param[out] op_restrict   Fine to coarse `CeedOperator`, or `NULL`
2555eaf62fffSJeremy L Thompson 
2556eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2557eaf62fffSJeremy L Thompson 
2558eaf62fffSJeremy L Thompson   @ref User
2559eaf62fffSJeremy L Thompson **/
25602b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
25612b730f8bSJeremy L Thompson                                              const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
25627758292fSSebastian Grimberg                                              CeedOperator *op_restrict) {
2563eaf62fffSJeremy L Thompson   Ceed      ceed;
25641c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
25651c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
25661c66c397SJeremy L Thompson 
25671c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
25682b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2569eaf62fffSJeremy L Thompson 
2570eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
25712b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
25722b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
25732b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
25746574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2575eaf62fffSJeremy L Thompson 
257683d6adf3SZach Atkins   // Create coarse to fine basis, if required
25777758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
25781c66c397SJeremy L Thompson     CeedInt     dim, num_comp, num_nodes_c, P_1d_f, P_1d_c;
25791c66c397SJeremy L Thompson     CeedScalar *q_ref, *q_weight, *grad;
25801c66c397SJeremy L Thompson 
258183d6adf3SZach Atkins     // Check if interpolation matrix is provided
25826574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
25836574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
25842b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
25852b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
25862b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f));
25872b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
25882b730f8bSJeremy L Thompson     P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c);
25892b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_ref));
25902b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_weight));
25912b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad));
25922b730f8bSJeremy 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));
25932b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
25942b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
25952b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
259683d6adf3SZach Atkins   }
2597eaf62fffSJeremy L Thompson 
2598eaf62fffSJeremy L Thompson   // Core code
25997758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2600eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2601eaf62fffSJeremy L Thompson }
2602eaf62fffSJeremy L Thompson 
2603eaf62fffSJeremy L Thompson /**
2604ca94c3ddSJeremy L Thompson   @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a non-tensor basis for the active vector
2605eaf62fffSJeremy L Thompson 
2606ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2607f04ea552SJeremy L Thompson 
2608ca94c3ddSJeremy L Thompson   @param[in]  op_fine       Fine grid `CeedOperator`
2609ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2610ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid `CeedElemRestriction`
2611ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector `CeedBasis`
2612ca94c3ddSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator`
2613ca94c3ddSJeremy L Thompson   @param[out] op_coarse     Coarse grid `CeedOperator`
2614ca94c3ddSJeremy L Thompson   @param[out] op_prolong    Coarse to fine `CeedOperator`, or `NULL`
2615ca94c3ddSJeremy L Thompson   @param[out] op_restrict   Fine to coarse `CeedOperator`, or `NULL`
2616eaf62fffSJeremy L Thompson 
2617eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2618eaf62fffSJeremy L Thompson 
2619eaf62fffSJeremy L Thompson   @ref User
2620eaf62fffSJeremy L Thompson **/
26212b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
26227758292fSSebastian Grimberg                                        const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
26237758292fSSebastian Grimberg                                        CeedOperator *op_restrict) {
2624eaf62fffSJeremy L Thompson   Ceed      ceed;
26251c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
26261c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
26271c66c397SJeremy L Thompson 
26281c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
26292b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2630eaf62fffSJeremy L Thompson 
2631eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
26322b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
26332b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
26342b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
26356574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2636eaf62fffSJeremy L Thompson 
2637eaf62fffSJeremy L Thompson   // Coarse to fine basis
26387758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
26391c66c397SJeremy L Thompson     CeedInt          dim, num_comp, num_nodes_c, num_nodes_f;
26401c66c397SJeremy L Thompson     CeedScalar      *q_ref, *q_weight, *grad;
26411c66c397SJeremy L Thompson     CeedElemTopology topo;
26421c66c397SJeremy L Thompson 
264383d6adf3SZach Atkins     // Check if interpolation matrix is provided
26446574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
26456574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
26462b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_fine, &topo));
26472b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
26482b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
26492b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f));
26502b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
26512b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref));
26522b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f, &q_weight));
26532b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad));
26542b730f8bSJeremy 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));
26552b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
26562b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
26572b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
265883d6adf3SZach Atkins   }
2659eaf62fffSJeremy L Thompson 
2660eaf62fffSJeremy L Thompson   // Core code
26617758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2662eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2663eaf62fffSJeremy L Thompson }
2664eaf62fffSJeremy L Thompson 
2665eaf62fffSJeremy L Thompson /**
2666ca94c3ddSJeremy L Thompson   @brief Build a FDM based approximate inverse for each element for a `CeedOperator`.
2667eaf62fffSJeremy L Thompson 
2668ca94c3ddSJeremy L Thompson   This returns a `CeedOperator` and `CeedVector` to apply a Fast Diagonalization Method based approximate inverse.
2669859c15bbSJames 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$.
2670ca94c3ddSJeremy L Thompson   The assembled `CeedQFunction` is used to modify the eigenvalues from simultaneous diagonalization and obtain an approximate inverse of the form \f$V^T \hat S V\f$.
2671ca94c3ddSJeremy L Thompson   The `CeedOperator` must be linear and non-composite.
2672ca94c3ddSJeremy L Thompson   The associated `CeedQFunction` must therefore also be linear.
2673eaf62fffSJeremy L Thompson 
2674ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2675f04ea552SJeremy L Thompson 
2676ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to create element inverses
2677ca94c3ddSJeremy L Thompson   @param[out] fdm_inv `CeedOperator` to apply the action of a FDM based inverse for each element
2678ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2679eaf62fffSJeremy L Thompson 
2680eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2681eaf62fffSJeremy L Thompson 
2682480fae85SJeremy L Thompson   @ref User
2683eaf62fffSJeremy L Thompson **/
26842b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) {
26851c66c397SJeremy L Thompson   Ceed                 ceed, ceed_parent;
26861c66c397SJeremy L Thompson   bool                 interp = false, grad = false, is_tensor_basis = true;
26871c66c397SJeremy L Thompson   CeedInt              num_input_fields, P_1d, Q_1d, num_nodes, num_qpts, dim, num_comp = 1, num_elem = 1;
26881c66c397SJeremy L Thompson   CeedSize             l_size = 1;
26891c66c397SJeremy L Thompson   CeedScalar          *mass, *laplace, *x, *fdm_interp, *lambda, *elem_avg;
26901c66c397SJeremy L Thompson   const CeedScalar    *interp_1d, *grad_1d, *q_weight_1d;
26911c66c397SJeremy L Thompson   CeedVector           q_data;
26921c66c397SJeremy L Thompson   CeedElemRestriction  rstr  = NULL, rstr_qd_i;
26931c66c397SJeremy L Thompson   CeedBasis            basis = NULL, fdm_basis;
26941c66c397SJeremy L Thompson   CeedQFunctionContext ctx_fdm;
26951c66c397SJeremy L Thompson   CeedQFunctionField  *qf_fields;
26961c66c397SJeremy L Thompson   CeedQFunction        qf, qf_fdm;
26971c66c397SJeremy L Thompson   CeedOperatorField   *op_fields;
26981c66c397SJeremy L Thompson 
26992b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2700eaf62fffSJeremy L Thompson 
2701eaf62fffSJeremy L Thompson   if (op->CreateFDMElementInverse) {
2702d04bbc78SJeremy L Thompson     // Backend version
27032b730f8bSJeremy L Thompson     CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request));
2704eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2705eaf62fffSJeremy L Thompson   } else {
2706d04bbc78SJeremy L Thompson     // Operator fallback
2707d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2708d04bbc78SJeremy L Thompson 
27092b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2710d04bbc78SJeremy L Thompson     if (op_fallback) {
27112b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request));
2712eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2713eaf62fffSJeremy L Thompson     }
2714eaf62fffSJeremy L Thompson   }
2715eaf62fffSJeremy L Thompson 
2716d04bbc78SJeremy L Thompson   // Default interface implementation
27172b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
2718bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
27192b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
2720eaf62fffSJeremy L Thompson 
2721eaf62fffSJeremy L Thompson   // Determine active input basis
27222b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL));
27232b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
2724eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
2725eaf62fffSJeremy L Thompson     CeedVector vec;
27261c66c397SJeremy L Thompson 
27272b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
2728eaf62fffSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2729eaf62fffSJeremy L Thompson       CeedEvalMode eval_mode;
27301c66c397SJeremy L Thompson 
27312b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
2732eaf62fffSJeremy L Thompson       interp = interp || eval_mode == CEED_EVAL_INTERP;
2733eaf62fffSJeremy L Thompson       grad   = grad || eval_mode == CEED_EVAL_GRAD;
27342b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis));
27352b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
2736eaf62fffSJeremy L Thompson     }
2737eaf62fffSJeremy L Thompson   }
27386574a04fSJeremy L Thompson   CeedCheck(basis, ceed, CEED_ERROR_BACKEND, "No active field set");
27392b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
2740352a5e7cSSebastian Grimberg   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
27412b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
27422b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
27432b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
27442b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
27452b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
27462b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
2747eaf62fffSJeremy L Thompson 
2748eaf62fffSJeremy L Thompson   // Build and diagonalize 1D Mass and Laplacian
27496574a04fSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
27506574a04fSJeremy L Thompson   CeedCheck(is_tensor_basis, ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases");
27512b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &mass));
27522b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &laplace));
27532b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &x));
27542b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp));
27552b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &lambda));
2756eaf62fffSJeremy L Thompson   // -- Build matrices
27572b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
27582b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
27592b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
27602b730f8bSJeremy L Thompson   CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace));
2761eaf62fffSJeremy L Thompson 
2762eaf62fffSJeremy L Thompson   // -- Diagonalize
27632b730f8bSJeremy L Thompson   CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d));
27642b730f8bSJeremy L Thompson   CeedCall(CeedFree(&mass));
27652b730f8bSJeremy L Thompson   CeedCall(CeedFree(&laplace));
27662b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
27672b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d];
27682b730f8bSJeremy L Thompson   }
27692b730f8bSJeremy L Thompson   CeedCall(CeedFree(&x));
2770eaf62fffSJeremy L Thompson 
27711c66c397SJeremy L Thompson   {
27721c66c397SJeremy L Thompson     CeedInt             layout[3], num_modes = (interp ? 1 : 0) + (grad ? dim : 0);
27731c66c397SJeremy L Thompson     CeedScalar          max_norm = 0;
27741c66c397SJeremy L Thompson     const CeedScalar   *assembled_array, *q_weight_array;
27751c66c397SJeremy L Thompson     CeedVector          assembled = NULL, q_weight;
2776c5f45aeaSJeremy L Thompson     CeedElemRestriction rstr_qf   = NULL;
27771c66c397SJeremy L Thompson 
27781c66c397SJeremy L Thompson     // Assemble QFunction
27792b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request));
278056c48462SJeremy L Thompson     CeedCall(CeedElemRestrictionGetELayout(rstr_qf, layout));
27812b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr_qf));
27822b730f8bSJeremy L Thompson     CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm));
2783eaf62fffSJeremy L Thompson 
2784eaf62fffSJeremy L Thompson     // Calculate element averages
27852b730f8bSJeremy L Thompson     CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight));
27862b730f8bSJeremy L Thompson     CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight));
27872b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array));
27882b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array));
27892b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_elem, &elem_avg));
2790eaf62fffSJeremy L Thompson     const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON;
27911c66c397SJeremy L Thompson 
2792eaf62fffSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
2793eaf62fffSJeremy L Thompson       CeedInt count = 0;
27941c66c397SJeremy L Thompson 
27952b730f8bSJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
27962b730f8bSJeremy L Thompson         for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) {
27972b730f8bSJeremy L Thompson           if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) {
27982b730f8bSJeremy L Thompson             elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q];
2799eaf62fffSJeremy L Thompson             count++;
2800eaf62fffSJeremy L Thompson           }
28012b730f8bSJeremy L Thompson         }
28022b730f8bSJeremy L Thompson       }
2803eaf62fffSJeremy L Thompson       if (count) {
2804eaf62fffSJeremy L Thompson         elem_avg[e] /= count;
2805eaf62fffSJeremy L Thompson       } else {
2806eaf62fffSJeremy L Thompson         elem_avg[e] = 1.0;
2807eaf62fffSJeremy L Thompson       }
2808eaf62fffSJeremy L Thompson     }
28092b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array));
28102b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled));
28112b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array));
28122b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&q_weight));
28131c66c397SJeremy L Thompson   }
2814eaf62fffSJeremy L Thompson 
2815eaf62fffSJeremy L Thompson   // Build FDM diagonal
28161c66c397SJeremy L Thompson   {
2817eaf62fffSJeremy L Thompson     CeedScalar *q_data_array, *fdm_diagonal;
28181c66c397SJeremy L Thompson 
2819352a5e7cSSebastian Grimberg     CeedCall(CeedCalloc(num_comp * num_nodes, &fdm_diagonal));
2820352a5e7cSSebastian Grimberg     const CeedScalar fdm_diagonal_bound = num_nodes * CEED_EPSILON;
28212b730f8bSJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
2822352a5e7cSSebastian Grimberg       for (CeedInt n = 0; n < num_nodes; n++) {
2823352a5e7cSSebastian Grimberg         if (interp) fdm_diagonal[c * num_nodes + n] = 1.0;
28242b730f8bSJeremy L Thompson         if (grad) {
2825eaf62fffSJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
2826eaf62fffSJeremy L Thompson             CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d;
2827352a5e7cSSebastian Grimberg             fdm_diagonal[c * num_nodes + n] += lambda[i];
2828eaf62fffSJeremy L Thompson           }
2829eaf62fffSJeremy L Thompson         }
2830352a5e7cSSebastian Grimberg         if (fabs(fdm_diagonal[c * num_nodes + n]) < fdm_diagonal_bound) fdm_diagonal[c * num_nodes + n] = fdm_diagonal_bound;
28312b730f8bSJeremy L Thompson       }
28322b730f8bSJeremy L Thompson     }
2833352a5e7cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * num_nodes, &q_data));
28342b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(q_data, 0.0));
28352b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array));
28362b730f8bSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
28372b730f8bSJeremy L Thompson       for (CeedInt c = 0; c < num_comp; c++) {
28381c66c397SJeremy L Thompson         for (CeedInt n = 0; n < num_nodes; n++)
28391c66c397SJeremy L Thompson           q_data_array[(e * num_comp + c) * num_nodes + n] = 1. / (elem_avg[e] * fdm_diagonal[c * num_nodes + n]);
28402b730f8bSJeremy L Thompson       }
28412b730f8bSJeremy L Thompson     }
28422b730f8bSJeremy L Thompson     CeedCall(CeedFree(&elem_avg));
28432b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_diagonal));
28442b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(q_data, &q_data_array));
28451c66c397SJeremy L Thompson   }
2846eaf62fffSJeremy L Thompson 
2847eaf62fffSJeremy L Thompson   // Setup FDM operator
2848eaf62fffSJeremy L Thompson   // -- Basis
28491c66c397SJeremy L Thompson   {
2850eaf62fffSJeremy L Thompson     CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy;
28511c66c397SJeremy L Thompson 
28522b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy));
28532b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_ref_dummy));
28542b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_weight_dummy));
28552b730f8bSJeremy 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));
28562b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_interp));
28572b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad_dummy));
28582b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref_dummy));
28592b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight_dummy));
28602b730f8bSJeremy L Thompson     CeedCall(CeedFree(&lambda));
28611c66c397SJeremy L Thompson   }
2862eaf62fffSJeremy L Thompson 
2863eaf62fffSJeremy L Thompson   // -- Restriction
28641c66c397SJeremy L Thompson   {
2865352a5e7cSSebastian Grimberg     CeedInt strides[3] = {1, num_nodes, num_nodes * num_comp};
2866352a5e7cSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, num_nodes, num_comp, num_elem * num_comp * num_nodes, strides, &rstr_qd_i));
28671c66c397SJeremy L Thompson   }
28681c66c397SJeremy L Thompson 
2869eaf62fffSJeremy L Thompson   // -- QFunction
28702b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm));
28712b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP));
28722b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE));
28732b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP));
28742b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp));
28751c66c397SJeremy L Thompson 
2876eaf62fffSJeremy L Thompson   // -- QFunction context
28771c66c397SJeremy L Thompson   {
2878eaf62fffSJeremy L Thompson     CeedInt *num_comp_data;
28791c66c397SJeremy L Thompson 
28802b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_data));
2881eaf62fffSJeremy L Thompson     num_comp_data[0] = num_comp;
28822b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm));
28832b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data));
28841c66c397SJeremy L Thompson   }
28852b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm));
28862b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_fdm));
28871c66c397SJeremy L Thompson 
2888eaf62fffSJeremy L Thompson   // -- Operator
28892b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv));
28902b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2891356036faSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_NONE, q_data));
28922b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2893eaf62fffSJeremy L Thompson 
2894eaf62fffSJeremy L Thompson   // Cleanup
28952b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_data));
28962b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&fdm_basis));
28972b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i));
28982b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_fdm));
2899eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2900eaf62fffSJeremy L Thompson }
2901eaf62fffSJeremy L Thompson 
2902eaf62fffSJeremy L Thompson /// @}
2903