xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-preconditioning.c (revision 5e1f751ef2b988c9b3bd40351d2ca6b559914046)
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   CeedQFunctionField *input_fields, *output_fields;
411c66c397SJeremy L Thompson 
429e77b9c8SJeremy L Thompson   // Check if NULL qf passed in
439e77b9c8SJeremy L Thompson   if (!qf) return CEED_ERROR_SUCCESS;
449e77b9c8SJeremy L Thompson 
459bc66399SJeremy L Thompson   CeedDebug256(CeedQFunctionReturnCeed(qf), 1, "---------- CeedOperator Fallback ----------\n");
469bc66399SJeremy L Thompson   CeedDebug(CeedQFunctionReturnCeed(qf), "Creating fallback CeedQFunction\n");
47d04bbc78SJeremy L Thompson 
489e77b9c8SJeremy L Thompson   if (qf->source_path) {
492b730f8bSJeremy L Thompson     size_t path_len = strlen(qf->source_path), name_len = strlen(qf->kernel_name);
509c25dd66SJeremy L Thompson 
512b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(path_len + name_len + 2, &source_path_with_name));
529e77b9c8SJeremy L Thompson     memcpy(source_path_with_name, qf->source_path, path_len);
539e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len], ":", 1);
549e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len + 1], qf->kernel_name, name_len);
559c25dd66SJeremy L Thompson   } else if (qf->user_source) {
569c25dd66SJeremy L Thompson     CeedCall(CeedStringAllocCopy(qf->user_source, &source_path_with_name));
579e77b9c8SJeremy L Thompson   } else {
582b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &source_path_with_name));
599e77b9c8SJeremy L Thompson   }
609e77b9c8SJeremy L Thompson 
611203703bSJeremy L Thompson   {
621203703bSJeremy L Thompson     CeedInt           vec_length;
631203703bSJeremy L Thompson     CeedQFunctionUser f;
641203703bSJeremy L Thompson 
651203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length));
661203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetUserFunction(qf, &f));
671203703bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(fallback_ceed, vec_length, f, source_path_with_name, qf_fallback));
681203703bSJeremy L Thompson   }
699e77b9c8SJeremy L Thompson   {
709e77b9c8SJeremy L Thompson     CeedQFunctionContext ctx;
719e77b9c8SJeremy L Thompson 
722b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
732b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx));
741485364cSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx));
759e77b9c8SJeremy L Thompson   }
761203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
771203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
786f8994e9SJeremy L Thompson     const char  *field_name;
791203703bSJeremy L Thompson     CeedInt      size;
801203703bSJeremy L Thompson     CeedEvalMode eval_mode;
811203703bSJeremy L Thompson 
82ab747706SJeremy L Thompson     CeedCall(CeedQFunctionFieldGetData(input_fields[i], &field_name, &size, &eval_mode));
831203703bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(*qf_fallback, field_name, size, eval_mode));
849e77b9c8SJeremy L Thompson   }
851203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
866f8994e9SJeremy L Thompson     const char  *field_name;
871203703bSJeremy L Thompson     CeedInt      size;
881203703bSJeremy L Thompson     CeedEvalMode eval_mode;
891203703bSJeremy L Thompson 
90ab747706SJeremy L Thompson     CeedCall(CeedQFunctionFieldGetData(output_fields[i], &field_name, &size, &eval_mode));
911203703bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(*qf_fallback, field_name, size, eval_mode));
929e77b9c8SJeremy L Thompson   }
932b730f8bSJeremy L Thompson   CeedCall(CeedFree(&source_path_with_name));
949e77b9c8SJeremy L Thompson   return CEED_ERROR_SUCCESS;
959e77b9c8SJeremy L Thompson }
969e77b9c8SJeremy L Thompson 
979e77b9c8SJeremy L Thompson /**
98ca94c3ddSJeremy L Thompson   @brief Duplicate a `CeedOperator` with a reference `Ceed` to fallback for advanced `CeedOperator` functionality
99eaf62fffSJeremy L Thompson 
100ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to create fallback for
101eaf62fffSJeremy L Thompson 
102eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
103eaf62fffSJeremy L Thompson 
104eaf62fffSJeremy L Thompson   @ref Developer
105eaf62fffSJeremy L Thompson **/
106d04bbc78SJeremy L Thompson static int CeedOperatorCreateFallback(CeedOperator op) {
1071c66c397SJeremy L Thompson   bool         is_composite;
1081203703bSJeremy L Thompson   Ceed         ceed, ceed_fallback;
1091c66c397SJeremy L Thompson   CeedOperator op_fallback;
110eaf62fffSJeremy L Thompson 
111805fe78eSJeremy L Thompson   // Check not already created
112805fe78eSJeremy L Thompson   if (op->op_fallback) return CEED_ERROR_SUCCESS;
113805fe78eSJeremy L Thompson 
114eaf62fffSJeremy L Thompson   // Fallback Ceed
1151203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1161203703bSJeremy L Thompson   CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
1179bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
118d04bbc78SJeremy L Thompson   if (!ceed_fallback) return CEED_ERROR_SUCCESS;
119d04bbc78SJeremy L Thompson 
1209bc66399SJeremy L Thompson   CeedDebug256(CeedOperatorReturnCeed(op), 1, "---------- CeedOperator Fallback ----------\n");
1219bc66399SJeremy L Thompson   CeedDebug(CeedOperatorReturnCeed(op), "Creating fallback CeedOperator\n");
122eaf62fffSJeremy L Thompson 
123eaf62fffSJeremy L Thompson   // Clone Op
124b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
125b275c451SJeremy L Thompson   if (is_composite) {
126b275c451SJeremy L Thompson     CeedInt       num_suboperators;
127b275c451SJeremy L Thompson     CeedOperator *sub_operators;
128b275c451SJeremy L Thompson 
1292b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorCreate(ceed_fallback, &op_fallback));
130b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
131b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
132b275c451SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
133d04bbc78SJeremy L Thompson       CeedOperator op_sub_fallback;
134d04bbc78SJeremy L Thompson 
135b275c451SJeremy L Thompson       CeedCall(CeedOperatorGetFallback(sub_operators[i], &op_sub_fallback));
1362b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorAddSub(op_fallback, op_sub_fallback));
137805fe78eSJeremy L Thompson     }
138805fe78eSJeremy L Thompson   } else {
1391203703bSJeremy L Thompson     CeedInt            num_input_fields, num_output_fields;
1409e77b9c8SJeremy L Thompson     CeedQFunction      qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL;
1411203703bSJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
1421c66c397SJeremy L Thompson 
1432b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback));
1442b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback));
1452b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback));
1462b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback));
1471203703bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1481203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1496f8994e9SJeremy L Thompson       const char         *field_name;
1501203703bSJeremy L Thompson       CeedVector          vec;
1511203703bSJeremy L Thompson       CeedElemRestriction rstr;
1521203703bSJeremy L Thompson       CeedBasis           basis;
1531203703bSJeremy L Thompson 
154ab747706SJeremy L Thompson       CeedCall(CeedOperatorFieldGetData(input_fields[i], &field_name, &rstr, &basis, &vec));
1551203703bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec));
156681d0ea7SJeremy L Thompson       CeedCall(CeedVectorDestroy(&vec));
157681d0ea7SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&rstr));
158681d0ea7SJeremy L Thompson       CeedCall(CeedBasisDestroy(&basis));
159805fe78eSJeremy L Thompson     }
1601203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1616f8994e9SJeremy L Thompson       const char         *field_name;
1621203703bSJeremy L Thompson       CeedVector          vec;
1631203703bSJeremy L Thompson       CeedElemRestriction rstr;
1641203703bSJeremy L Thompson       CeedBasis           basis;
1651203703bSJeremy L Thompson 
166ab747706SJeremy L Thompson       CeedCall(CeedOperatorFieldGetData(output_fields[i], &field_name, &rstr, &basis, &vec));
1671203703bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec));
168681d0ea7SJeremy L Thompson       CeedCall(CeedVectorDestroy(&vec));
169681d0ea7SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&rstr));
170681d0ea7SJeremy L Thompson       CeedCall(CeedBasisDestroy(&basis));
171805fe78eSJeremy L Thompson     }
1727d5185d7SSebastian Grimberg     {
1737d5185d7SSebastian Grimberg       CeedQFunctionAssemblyData data;
1747d5185d7SSebastian Grimberg 
1757d5185d7SSebastian Grimberg       CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
1767d5185d7SSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataReferenceCopy(data, &op_fallback->qf_assembled));
1777d5185d7SSebastian Grimberg     }
1789e77b9c8SJeremy L Thompson     // Cleanup
1792b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf_fallback));
1802b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqf_fallback));
1812b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqfT_fallback));
182805fe78eSJeremy L Thompson   }
1832b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetName(op_fallback, op->name));
1842b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fallback));
185b05f7e9fSJeremy L Thompson   // Note: No ref-counting here so we don't get caught in a reference loop.
186b05f7e9fSJeremy L Thompson   //       The op holds the only reference to op_fallback and is responsible for deleting itself and op_fallback.
187805fe78eSJeremy L Thompson   op->op_fallback                 = op_fallback;
188b05f7e9fSJeremy L Thompson   op_fallback->op_fallback_parent = op;
1899bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed_fallback));
190eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
191eaf62fffSJeremy L Thompson }
192eaf62fffSJeremy L Thompson 
193eaf62fffSJeremy L Thompson /**
194eaf62fffSJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
195eaf62fffSJeremy L Thompson 
1960cd9fdf4SJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble diagonal or point block diagonal
197ca94c3ddSJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
198bd83916cSSebastian Grimberg   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
199ca94c3ddSJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
200eaf62fffSJeremy L Thompson 
201eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
202eaf62fffSJeremy L Thompson 
203eaf62fffSJeremy L Thompson   @ref Developer
204eaf62fffSJeremy L Thompson **/
2050cd9fdf4SJeremy L Thompson static inline int CeedSingleOperatorLinearAssembleAddDiagonal_Mesh(CeedOperator op, CeedRequest *request, const bool is_point_block,
206f3bd9308SJeremy L Thompson                                                                    CeedVector assembled) {
207506b1a0cSSebastian Grimberg   bool is_composite;
208506b1a0cSSebastian Grimberg 
209506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2109bc66399SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
211506b1a0cSSebastian Grimberg 
212506b1a0cSSebastian Grimberg   // Assemble QFunction
213506b1a0cSSebastian Grimberg   CeedInt             layout_qf[3];
214437c7c90SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
215c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
216c5f45aeaSJeremy L Thompson   CeedElemRestriction assembled_elem_rstr = NULL;
217437c7c90SJeremy L Thompson 
218437c7c90SJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request));
21956c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf));
220437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
221437c7c90SJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
222eaf62fffSJeremy L Thompson 
223ed9e99e6SJeremy L Thompson   // Get assembly data
224437c7c90SJeremy L Thompson   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
225506b1a0cSSebastian Grimberg   CeedInt                  num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out;
226437c7c90SJeremy L Thompson   CeedSize               **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components;
227506b1a0cSSebastian Grimberg   CeedBasis               *active_bases_in, *active_bases_out;
228506b1a0cSSebastian Grimberg   CeedElemRestriction     *active_elem_rstrs_in, *active_elem_rstrs_out;
2291c66c397SJeremy L Thompson   CeedOperatorAssemblyData data;
2301c66c397SJeremy L Thompson 
231437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
232506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in,
233506b1a0cSSebastian Grimberg                                                 &num_active_bases_out, &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out,
234506b1a0cSSebastian Grimberg                                                 &num_output_components));
235506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, NULL, NULL, &active_bases_out, NULL));
236506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs_in, NULL, &active_elem_rstrs_out));
237506b1a0cSSebastian Grimberg 
238934a29f5SSebastian Grimberg   // Loop over all active bases (find matching input/output pairs)
239934a29f5SSebastian Grimberg   for (CeedInt b = 0; b < CeedIntMin(num_active_bases_in, num_active_bases_out); b++) {
240934a29f5SSebastian Grimberg     CeedInt             b_in, b_out, num_elem, num_nodes, num_qpts, num_comp;
2411c66c397SJeremy L Thompson     bool                has_eval_none = false;
2421c66c397SJeremy L Thompson     CeedScalar         *elem_diag_array, *identity = NULL;
2431c66c397SJeremy L Thompson     CeedVector          elem_diag;
2447c1dbaffSSebastian Grimberg     CeedElemRestriction diag_elem_rstr;
2451c66c397SJeremy L Thompson 
246934a29f5SSebastian Grimberg     if (num_active_bases_in <= num_active_bases_out) {
247934a29f5SSebastian Grimberg       b_in = b;
248934a29f5SSebastian Grimberg       for (b_out = 0; b_out < num_active_bases_out; b_out++) {
249934a29f5SSebastian Grimberg         if (active_bases_in[b_in] == active_bases_out[b_out]) {
250934a29f5SSebastian Grimberg           break;
251934a29f5SSebastian Grimberg         }
252934a29f5SSebastian Grimberg       }
253934a29f5SSebastian Grimberg       if (b_out == num_active_bases_out) {
254934a29f5SSebastian Grimberg         continue;
255934a29f5SSebastian Grimberg       }  // No matching output basis found
256934a29f5SSebastian Grimberg     } else {
257934a29f5SSebastian Grimberg       b_out = b;
258934a29f5SSebastian Grimberg       for (b_in = 0; b_in < num_active_bases_in; b_in++) {
259934a29f5SSebastian Grimberg         if (active_bases_in[b_in] == active_bases_out[b_out]) {
260934a29f5SSebastian Grimberg           break;
261934a29f5SSebastian Grimberg         }
262934a29f5SSebastian Grimberg       }
263934a29f5SSebastian Grimberg       if (b_in == num_active_bases_in) {
264934a29f5SSebastian Grimberg         continue;
265934a29f5SSebastian Grimberg       }  // No matching output basis found
266934a29f5SSebastian Grimberg     }
2679bc66399SJeremy L Thompson     CeedCheck(active_elem_rstrs_in[b_in] == active_elem_rstrs_out[b_out], CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
268506b1a0cSSebastian Grimberg               "Cannot assemble operator diagonal with different input and output active element restrictions");
269506b1a0cSSebastian Grimberg 
2701c66c397SJeremy L Thompson     // Assemble point block diagonal restriction, if needed
271bd83916cSSebastian Grimberg     if (is_point_block) {
272934a29f5SSebastian Grimberg       CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstrs_in[b_in], &diag_elem_rstr));
2737c1dbaffSSebastian Grimberg     } else {
274934a29f5SSebastian Grimberg       CeedCall(CeedElemRestrictionCreateUnsignedCopy(active_elem_rstrs_in[b_in], &diag_elem_rstr));
275eaf62fffSJeremy L Thompson     }
276eaf62fffSJeremy L Thompson 
277eaf62fffSJeremy L Thompson     // Create diagonal vector
278437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag));
279eaf62fffSJeremy L Thompson 
280eaf62fffSJeremy L Thompson     // Assemble element operator diagonals
2812b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(elem_diag, 0.0));
2822b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array));
283437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem));
284934a29f5SSebastian Grimberg     CeedCall(CeedBasisGetNumNodes(active_bases_in[b_in], &num_nodes));
285934a29f5SSebastian Grimberg     CeedCall(CeedBasisGetNumComponents(active_bases_in[b_in], &num_comp));
286934a29f5SSebastian Grimberg     if (active_bases_in[b_in] == CEED_BASIS_NONE) num_qpts = num_nodes;
287934a29f5SSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(active_bases_in[b_in], &num_qpts));
288ed9e99e6SJeremy L Thompson 
289352a5e7cSSebastian Grimberg     // Construct identity matrix for basis if required
290934a29f5SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in[b_in]; i++) {
291934a29f5SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[b_in][i] == CEED_EVAL_NONE);
292ed9e99e6SJeremy L Thompson     }
293934a29f5SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out[b_out]; i++) {
294934a29f5SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[b_out][i] == CEED_EVAL_NONE);
295ed9e99e6SJeremy L Thompson     }
296ed9e99e6SJeremy L Thompson     if (has_eval_none) {
2972b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
2982b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
299eaf62fffSJeremy L Thompson     }
300352a5e7cSSebastian Grimberg 
301eaf62fffSJeremy L Thompson     // Compute the diagonal of B^T D B
302eaf62fffSJeremy L Thompson     // Each element
303b94338b9SJed Brown     for (CeedSize e = 0; e < num_elem; e++) {
304eaf62fffSJeremy L Thompson       // Each basis eval mode pair
305352a5e7cSSebastian Grimberg       CeedInt      d_out              = 0, q_comp_out;
306352a5e7cSSebastian Grimberg       CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
3071c66c397SJeremy L Thompson 
308934a29f5SSebastian Grimberg       for (CeedInt e_out = 0; e_out < num_eval_modes_out[b_out]; e_out++) {
3091c66c397SJeremy L Thompson         CeedInt           d_in              = 0, q_comp_in;
310437c7c90SJeremy L Thompson         const CeedScalar *B_t               = NULL;
3111c66c397SJeremy L Thompson         CeedEvalMode      eval_mode_in_prev = CEED_EVAL_NONE;
3121c66c397SJeremy L Thompson 
313934a29f5SSebastian Grimberg         CeedCall(CeedOperatorGetBasisPointer(active_bases_out[b_out], eval_modes_out[b_out][e_out], identity, &B_t));
314934a29f5SSebastian Grimberg         CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_out[b_out], eval_modes_out[b_out][e_out], &q_comp_out));
315352a5e7cSSebastian Grimberg         if (q_comp_out > 1) {
316934a29f5SSebastian Grimberg           if (e_out == 0 || eval_modes_out[b_out][e_out] != eval_mode_out_prev) d_out = 0;
317352a5e7cSSebastian Grimberg           else B_t = &B_t[(++d_out) * num_qpts * num_nodes];
318352a5e7cSSebastian Grimberg         }
319934a29f5SSebastian Grimberg         eval_mode_out_prev = eval_modes_out[b_out][e_out];
320352a5e7cSSebastian Grimberg 
321934a29f5SSebastian Grimberg         for (CeedInt e_in = 0; e_in < num_eval_modes_in[b_in]; e_in++) {
322437c7c90SJeremy L Thompson           const CeedScalar *B = NULL;
3231c66c397SJeremy L Thompson 
324934a29f5SSebastian Grimberg           CeedCall(CeedOperatorGetBasisPointer(active_bases_in[b_in], eval_modes_in[b_in][e_in], identity, &B));
325934a29f5SSebastian Grimberg           CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_in[b_in], eval_modes_in[b_in][e_in], &q_comp_in));
326352a5e7cSSebastian Grimberg           if (q_comp_in > 1) {
327934a29f5SSebastian Grimberg             if (e_in == 0 || eval_modes_in[b_in][e_in] != eval_mode_in_prev) d_in = 0;
328352a5e7cSSebastian Grimberg             else B = &B[(++d_in) * num_qpts * num_nodes];
329352a5e7cSSebastian Grimberg           }
330934a29f5SSebastian Grimberg           eval_mode_in_prev = eval_modes_in[b_in][e_in];
331352a5e7cSSebastian Grimberg 
332eaf62fffSJeremy L Thompson           // Each component
333506b1a0cSSebastian Grimberg           for (CeedInt c_out = 0; c_out < num_comp; c_out++) {
334437c7c90SJeremy L Thompson             // Each qpt/node pair
3352b730f8bSJeremy L Thompson             for (CeedInt q = 0; q < num_qpts; q++) {
336bd83916cSSebastian Grimberg               if (is_point_block) {
337eaf62fffSJeremy L Thompson                 // Point Block Diagonal
338506b1a0cSSebastian Grimberg                 for (CeedInt c_in = 0; c_in < num_comp; c_in++) {
339934a29f5SSebastian Grimberg                   const CeedSize c_offset =
340934a29f5SSebastian Grimberg                       (eval_mode_offsets_in[b_in][e_in] + c_in) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out;
341506b1a0cSSebastian Grimberg                   const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]];
3421c66c397SJeremy L Thompson 
3432b730f8bSJeremy L Thompson                   for (CeedInt n = 0; n < num_nodes; n++) {
344506b1a0cSSebastian Grimberg                     elem_diag_array[((e * num_comp + c_out) * num_comp + c_in) * num_nodes + n] +=
345437c7c90SJeremy L Thompson                         B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n];
346eaf62fffSJeremy L Thompson                   }
3472b730f8bSJeremy L Thompson                 }
348eaf62fffSJeremy L Thompson               } else {
349eaf62fffSJeremy L Thompson                 // Diagonal Only
350934a29f5SSebastian Grimberg                 const CeedInt c_offset =
351934a29f5SSebastian Grimberg                     (eval_mode_offsets_in[b_in][e_in] + c_out) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out;
352506b1a0cSSebastian Grimberg                 const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]];
3531c66c397SJeremy L Thompson 
3542b730f8bSJeremy L Thompson                 for (CeedInt n = 0; n < num_nodes; n++) {
355506b1a0cSSebastian 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];
356eaf62fffSJeremy L Thompson                 }
357eaf62fffSJeremy L Thompson               }
358eaf62fffSJeremy L Thompson             }
359eaf62fffSJeremy L Thompson           }
3602b730f8bSJeremy L Thompson         }
3612b730f8bSJeremy L Thompson       }
3622b730f8bSJeremy L Thompson     }
3632b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
364eaf62fffSJeremy L Thompson 
365eaf62fffSJeremy L Thompson     // Assemble local operator diagonal
3667c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
367eaf62fffSJeremy L Thompson 
368eaf62fffSJeremy L Thompson     // Cleanup
3697c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr));
3702b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&elem_diag));
3712b730f8bSJeremy L Thompson     CeedCall(CeedFree(&identity));
372437c7c90SJeremy L Thompson   }
373437c7c90SJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
374437c7c90SJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
375eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
376eaf62fffSJeremy L Thompson }
377eaf62fffSJeremy L Thompson 
378eaf62fffSJeremy L Thompson /**
3790cd9fdf4SJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
3800cd9fdf4SJeremy L Thompson 
3810cd9fdf4SJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble diagonal or point block diagonal
3820cd9fdf4SJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
3830cd9fdf4SJeremy L Thompson   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
3840cd9fdf4SJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
3850cd9fdf4SJeremy L Thompson 
3860cd9fdf4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3870cd9fdf4SJeremy L Thompson 
3880cd9fdf4SJeremy L Thompson   @ref Developer
3890cd9fdf4SJeremy L Thompson **/
3900cd9fdf4SJeremy L Thompson static inline int CeedSingleOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block,
3910cd9fdf4SJeremy L Thompson                                                               CeedVector assembled) {
3920cd9fdf4SJeremy L Thompson   bool is_at_points;
3930cd9fdf4SJeremy L Thompson 
3940cd9fdf4SJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
3959bc66399SJeremy L Thompson   CeedCheck(!is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "AtPoints operator not supported");
3960cd9fdf4SJeremy L Thompson   CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal_Mesh(op, request, is_point_block, assembled));
3970cd9fdf4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3980cd9fdf4SJeremy L Thompson }
3990cd9fdf4SJeremy L Thompson 
4000cd9fdf4SJeremy L Thompson /**
401eaf62fffSJeremy L Thompson   @brief Core logic for assembling composite operator diagonal
402eaf62fffSJeremy L Thompson 
403ca94c3ddSJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble point block diagonal
404ca94c3ddSJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
405bd83916cSSebastian Grimberg   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
406ca94c3ddSJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
407eaf62fffSJeremy L Thompson 
408eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
409eaf62fffSJeremy L Thompson 
410eaf62fffSJeremy L Thompson   @ref Developer
411eaf62fffSJeremy L Thompson **/
412bd83916cSSebastian Grimberg static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block,
413eaf62fffSJeremy L Thompson                                                                  CeedVector assembled) {
414eaf62fffSJeremy L Thompson   CeedInt       num_sub;
415eaf62fffSJeremy L Thompson   CeedOperator *suboperators;
4161c66c397SJeremy L Thompson 
417c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
418c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators));
419eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_sub; i++) {
420bd83916cSSebastian Grimberg     if (is_point_block) {
4212b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request));
4226aa95790SJeremy L Thompson     } else {
4232b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request));
4246aa95790SJeremy L Thompson     }
425eaf62fffSJeremy L Thompson   }
426eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
427eaf62fffSJeremy L Thompson }
428eaf62fffSJeremy L Thompson 
429eaf62fffSJeremy L Thompson /**
430ca94c3ddSJeremy L Thompson   @brief Build nonzero pattern for non-composite CeedOperator`.
431eaf62fffSJeremy L Thompson 
432ca94c3ddSJeremy L Thompson   Users should generally use @ref CeedOperatorLinearAssembleSymbolic().
433eaf62fffSJeremy L Thompson 
434ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` to assemble nonzero pattern
435eaf62fffSJeremy L Thompson   @param[in]  offset Offset for number of entries
436eaf62fffSJeremy L Thompson   @param[out] rows   Row number for each entry
437eaf62fffSJeremy L Thompson   @param[out] cols   Column number for each entry
438eaf62fffSJeremy L Thompson 
439eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
440eaf62fffSJeremy L Thompson 
441eaf62fffSJeremy L Thompson   @ref Developer
442eaf62fffSJeremy L Thompson **/
4432b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) {
444f3d47e36SJeremy L Thompson   Ceed                ceed;
445f3d47e36SJeremy L Thompson   bool                is_composite;
44681670346SSebastian Grimberg   CeedSize            num_nodes_in, num_nodes_out, local_num_entries, count = 0;
447506b1a0cSSebastian Grimberg   CeedInt             num_elem_in, elem_size_in, num_comp_in, layout_er_in[3];
44881670346SSebastian Grimberg   CeedInt             num_elem_out, elem_size_out, num_comp_out, layout_er_out[3];
4491c66c397SJeremy L Thompson   CeedScalar         *array;
450506b1a0cSSebastian Grimberg   const CeedScalar   *elem_dof_a_in, *elem_dof_a_out;
451506b1a0cSSebastian Grimberg   CeedVector          index_vec_in, index_vec_out, elem_dof_in, elem_dof_out;
452506b1a0cSSebastian Grimberg   CeedElemRestriction elem_rstr_in, elem_rstr_out, index_elem_rstr_in, index_elem_rstr_out;
4531c66c397SJeremy L Thompson 
454f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4559bc66399SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
456*5e1f751eSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
457eaf62fffSJeremy L Thompson 
458506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes_in, &num_nodes_out));
459506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out));
460506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in));
461506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in));
462506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in));
46356c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(elem_rstr_in, layout_er_in));
464eaf62fffSJeremy L Thompson 
465506b1a0cSSebastian Grimberg   // Determine elem_dof relation for input
466506b1a0cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed, num_nodes_in, &index_vec_in));
467506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayWrite(index_vec_in, CEED_MEM_HOST, &array));
468c81f2b9dSJames Wright   for (CeedSize i = 0; i < num_nodes_in; i++) array[i] = i;
469506b1a0cSSebastian Grimberg   CeedCall(CeedVectorRestoreArray(index_vec_in, &array));
470506b1a0cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed, num_elem_in * elem_size_in * num_comp_in, &elem_dof_in));
471506b1a0cSSebastian Grimberg   CeedCall(CeedVectorSetValue(elem_dof_in, 0.0));
472506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_in, &index_elem_rstr_in));
473506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionApply(index_elem_rstr_in, CEED_NOTRANSPOSE, index_vec_in, elem_dof_in, CEED_REQUEST_IMMEDIATE));
474506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayRead(elem_dof_in, CEED_MEM_HOST, &elem_dof_a_in));
475506b1a0cSSebastian Grimberg   CeedCall(CeedVectorDestroy(&index_vec_in));
476506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_in));
477506b1a0cSSebastian Grimberg 
478506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
479506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out));
480506b1a0cSSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
4813f08121cSJeremy L Thompson               "Active input and output operator restrictions must have the same number of elements."
4823f08121cSJeremy L Thompson               " Input has %" CeedInt_FMT " elements; output has %" CeedInt_FMT "elements.",
4833f08121cSJeremy L Thompson               num_elem_in, num_elem_out);
484506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out));
485506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out));
48656c48462SJeremy L Thompson     CeedCall(CeedElemRestrictionGetELayout(elem_rstr_out, layout_er_out));
487506b1a0cSSebastian Grimberg 
488506b1a0cSSebastian Grimberg     // Determine elem_dof relation for output
489506b1a0cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed, num_nodes_out, &index_vec_out));
490506b1a0cSSebastian Grimberg     CeedCall(CeedVectorGetArrayWrite(index_vec_out, CEED_MEM_HOST, &array));
491c81f2b9dSJames Wright     for (CeedSize i = 0; i < num_nodes_out; i++) array[i] = i;
492506b1a0cSSebastian Grimberg     CeedCall(CeedVectorRestoreArray(index_vec_out, &array));
493506b1a0cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed, num_elem_out * elem_size_out * num_comp_out, &elem_dof_out));
494506b1a0cSSebastian Grimberg     CeedCall(CeedVectorSetValue(elem_dof_out, 0.0));
495506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_out, &index_elem_rstr_out));
496506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(index_elem_rstr_out, CEED_NOTRANSPOSE, index_vec_out, elem_dof_out, CEED_REQUEST_IMMEDIATE));
497506b1a0cSSebastian Grimberg     CeedCall(CeedVectorGetArrayRead(elem_dof_out, CEED_MEM_HOST, &elem_dof_a_out));
498506b1a0cSSebastian Grimberg     CeedCall(CeedVectorDestroy(&index_vec_out));
499506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_out));
500506b1a0cSSebastian Grimberg   } else {
501506b1a0cSSebastian Grimberg     num_elem_out     = num_elem_in;
502506b1a0cSSebastian Grimberg     elem_size_out    = elem_size_in;
503506b1a0cSSebastian Grimberg     num_comp_out     = num_comp_in;
504506b1a0cSSebastian Grimberg     layout_er_out[0] = layout_er_in[0];
505506b1a0cSSebastian Grimberg     layout_er_out[1] = layout_er_in[1];
506506b1a0cSSebastian Grimberg     layout_er_out[2] = layout_er_in[2];
507506b1a0cSSebastian Grimberg     elem_dof_a_out   = elem_dof_a_in;
508506b1a0cSSebastian Grimberg   }
509c81f2b9dSJames Wright   local_num_entries = (CeedSize)elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in;
510eaf62fffSJeremy L Thompson 
511eaf62fffSJeremy L Thompson   // Determine i, j locations for element matrices
512506b1a0cSSebastian Grimberg   for (CeedInt e = 0; e < num_elem_in; e++) {
513506b1a0cSSebastian Grimberg     for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) {
514506b1a0cSSebastian Grimberg       for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) {
515506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < elem_size_out; i++) {
516506b1a0cSSebastian Grimberg           for (CeedInt j = 0; j < elem_size_in; j++) {
517506b1a0cSSebastian Grimberg             const CeedInt elem_dof_index_row = i * layout_er_out[0] + comp_out * layout_er_out[1] + e * layout_er_out[2];
518506b1a0cSSebastian Grimberg             const CeedInt elem_dof_index_col = j * layout_er_in[0] + comp_in * layout_er_in[1] + e * layout_er_in[2];
519506b1a0cSSebastian Grimberg             const CeedInt row                = elem_dof_a_out[elem_dof_index_row];
520506b1a0cSSebastian Grimberg             const CeedInt col                = elem_dof_a_in[elem_dof_index_col];
521eaf62fffSJeremy L Thompson 
522eaf62fffSJeremy L Thompson             rows[offset + count] = row;
523eaf62fffSJeremy L Thompson             cols[offset + count] = col;
524eaf62fffSJeremy L Thompson             count++;
525eaf62fffSJeremy L Thompson           }
526eaf62fffSJeremy L Thompson         }
527eaf62fffSJeremy L Thompson       }
528eaf62fffSJeremy L Thompson     }
529eaf62fffSJeremy L Thompson   }
5306574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing assembled entries");
531506b1a0cSSebastian Grimberg   CeedCall(CeedVectorRestoreArrayRead(elem_dof_in, &elem_dof_a_in));
532506b1a0cSSebastian Grimberg   CeedCall(CeedVectorDestroy(&elem_dof_in));
533506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
534506b1a0cSSebastian Grimberg     CeedCall(CeedVectorRestoreArrayRead(elem_dof_out, &elem_dof_a_out));
535506b1a0cSSebastian Grimberg     CeedCall(CeedVectorDestroy(&elem_dof_out));
536506b1a0cSSebastian Grimberg   }
537681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&elem_rstr_in));
538681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&elem_rstr_out));
5399bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
540eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
541eaf62fffSJeremy L Thompson }
542eaf62fffSJeremy L Thompson 
543eaf62fffSJeremy L Thompson /**
544ca94c3ddSJeremy L Thompson   @brief Assemble nonzero entries for non-composite `CeedOperator`.
545eaf62fffSJeremy L Thompson 
546ca94c3ddSJeremy L Thompson   Users should generally use @ref CeedOperatorLinearAssemble().
547eaf62fffSJeremy L Thompson 
548ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` to assemble
549ea61e9acSJeremy L Thompson   @param[in]  offset Offset for number of entries
550eaf62fffSJeremy L Thompson   @param[out] values Values to assemble into matrix
551eaf62fffSJeremy L Thompson 
552eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
553eaf62fffSJeremy L Thompson 
554eaf62fffSJeremy L Thompson   @ref Developer
555eaf62fffSJeremy L Thompson **/
5562b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) {
557f3d47e36SJeremy L Thompson   bool is_composite;
5581c66c397SJeremy L Thompson 
559f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
5609bc66399SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
561f3d47e36SJeremy L Thompson 
562f3d47e36SJeremy L Thompson   // Early exit for empty operator
563f3d47e36SJeremy L Thompson   {
564f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
565f3d47e36SJeremy L Thompson 
566f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
567f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
568f3d47e36SJeremy L Thompson   }
569eaf62fffSJeremy L Thompson 
570cefa2673SJeremy L Thompson   if (op->LinearAssembleSingle) {
571cefa2673SJeremy L Thompson     // Backend version
5722b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSingle(op, offset, values));
573cefa2673SJeremy L Thompson     return CEED_ERROR_SUCCESS;
574cefa2673SJeremy L Thompson   } else {
575cefa2673SJeremy L Thompson     // Operator fallback
576cefa2673SJeremy L Thompson     CeedOperator op_fallback;
577cefa2673SJeremy L Thompson 
5782b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
579cefa2673SJeremy L Thompson     if (op_fallback) {
5802b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values));
581cefa2673SJeremy L Thompson       return CEED_ERROR_SUCCESS;
582cefa2673SJeremy L Thompson     }
583cefa2673SJeremy L Thompson   }
584cefa2673SJeremy L Thompson 
585eaf62fffSJeremy L Thompson   // Assemble QFunction
586506b1a0cSSebastian Grimberg   CeedInt             layout_qf[3];
5871c66c397SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
588c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
589506b1a0cSSebastian Grimberg   CeedElemRestriction assembled_elem_rstr = NULL;
590eaf62fffSJeremy L Thompson 
591506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, CEED_REQUEST_IMMEDIATE));
59256c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf));
593506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
594506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
595eaf62fffSJeremy L Thompson 
596ed9e99e6SJeremy L Thompson   // Get assembly data
597506b1a0cSSebastian Grimberg   CeedInt                  num_elem_in, elem_size_in, num_comp_in, num_qpts_in;
59881670346SSebastian Grimberg   CeedInt                  num_elem_out, elem_size_out, num_comp_out, num_qpts_out;
59981670346SSebastian Grimberg   CeedSize                 local_num_entries, count = 0;
600506b1a0cSSebastian Grimberg   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
601506b1a0cSSebastian Grimberg   CeedInt                  num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out;
602506b1a0cSSebastian Grimberg   CeedBasis               *active_bases_in, *active_bases_out, basis_in, basis_out;
603506b1a0cSSebastian Grimberg   const CeedScalar       **B_mats_in, **B_mats_out, *B_mat_in, *B_mat_out;
604506b1a0cSSebastian Grimberg   CeedElemRestriction      elem_rstr_in, elem_rstr_out;
605506b1a0cSSebastian Grimberg   CeedRestrictionType      elem_rstr_type_in, elem_rstr_type_out;
606506b1a0cSSebastian Grimberg   const bool              *elem_rstr_orients_in = NULL, *elem_rstr_orients_out = NULL;
607506b1a0cSSebastian Grimberg   const CeedInt8          *elem_rstr_curl_orients_in = NULL, *elem_rstr_curl_orients_out = NULL;
608506b1a0cSSebastian Grimberg   CeedOperatorAssemblyData data;
609eaf62fffSJeremy L Thompson 
610506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
611506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, NULL, &num_active_bases_out,
612506b1a0cSSebastian Grimberg                                                 &num_eval_modes_out, &eval_modes_out, NULL, NULL));
613506b1a0cSSebastian Grimberg 
6149bc66399SJeremy L Thompson   CeedCheck(num_active_bases_in == 1 && num_active_bases_out == 1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
615506b1a0cSSebastian Grimberg             "Cannot assemble operator with multiple active bases");
6169bc66399SJeremy L Thompson   CeedCheck(num_eval_modes_in[0] > 0 && num_eval_modes_out[0] > 0, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
6179bc66399SJeremy L Thompson             "Cannot assemble operator without inputs/outputs");
618eaf62fffSJeremy L Thompson 
619506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, &B_mats_in, NULL, &active_bases_out, &B_mats_out));
620506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out));
621506b1a0cSSebastian Grimberg   basis_in  = active_bases_in[0];
622506b1a0cSSebastian Grimberg   basis_out = active_bases_out[0];
623506b1a0cSSebastian Grimberg   B_mat_in  = B_mats_in[0];
624506b1a0cSSebastian Grimberg   B_mat_out = B_mats_out[0];
625eaf62fffSJeremy L Thompson 
626506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in));
627506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in));
628506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in));
629506b1a0cSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
630506b1a0cSSebastian Grimberg   else CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
631506b1a0cSSebastian Grimberg 
632506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetType(elem_rstr_in, &elem_rstr_type_in));
633506b1a0cSSebastian Grimberg   if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) {
634506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_orients_in));
635506b1a0cSSebastian Grimberg   } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
636506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_curl_orients_in));
6377c1dbaffSSebastian Grimberg   }
6387c1dbaffSSebastian Grimberg 
639506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
640506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out));
6419bc66399SJeremy L Thompson     CeedCheck(num_elem_in == num_elem_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
6423f08121cSJeremy L Thompson               "Active input and output operator restrictions must have the same number of elements."
6433f08121cSJeremy L Thompson               " Input has %" CeedInt_FMT " elements; output has %" CeedInt_FMT "elements.",
6443f08121cSJeremy L Thompson               num_elem_in, num_elem_out);
645506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out));
646506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out));
647506b1a0cSSebastian Grimberg     if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
648506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
6499bc66399SJeremy L Thompson     CeedCheck(num_qpts_in == num_qpts_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
6503f08121cSJeremy L Thompson               "Active input and output bases must have the same number of quadrature points."
6513f08121cSJeremy L Thompson               " Input has %" CeedInt_FMT " points; output has %" CeedInt_FMT "points.",
6523f08121cSJeremy L Thompson               num_qpts_in, num_qpts_out);
653eaf62fffSJeremy L Thompson 
654506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(elem_rstr_out, &elem_rstr_type_out));
655506b1a0cSSebastian Grimberg     if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) {
656506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_orients_out));
657506b1a0cSSebastian Grimberg     } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
658506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_curl_orients_out));
659506b1a0cSSebastian Grimberg     }
660506b1a0cSSebastian Grimberg   } else {
661506b1a0cSSebastian Grimberg     num_elem_out  = num_elem_in;
662506b1a0cSSebastian Grimberg     elem_size_out = elem_size_in;
663506b1a0cSSebastian Grimberg     num_comp_out  = num_comp_in;
664506b1a0cSSebastian Grimberg     num_qpts_out  = num_qpts_in;
665506b1a0cSSebastian Grimberg 
666506b1a0cSSebastian Grimberg     elem_rstr_orients_out      = elem_rstr_orients_in;
667506b1a0cSSebastian Grimberg     elem_rstr_curl_orients_out = elem_rstr_curl_orients_in;
668506b1a0cSSebastian Grimberg   }
669c81f2b9dSJames Wright   local_num_entries = (CeedSize)elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in;
670506b1a0cSSebastian Grimberg 
671506b1a0cSSebastian Grimberg   // Loop over elements and put in data structure
6727c1dbaffSSebastian Grimberg   // We store B_mat_in, B_mat_out, BTD, elem_mat in row-major order
6730459ebd3SSebastian Grimberg   CeedTensorContract contract;
674123d890dSSebastian Grimberg   CeedScalar        *vals, *BTD_mat = NULL, *elem_mat = NULL, *elem_mat_b = NULL;
675506b1a0cSSebastian Grimberg 
676c22497adSSebastian Grimberg   CeedCall(CeedBasisGetTensorContract(basis_in, &contract));
677123d890dSSebastian Grimberg   CeedCall(CeedCalloc(elem_size_out * num_qpts_in * num_eval_modes_in[0], &BTD_mat));
678123d890dSSebastian Grimberg   CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat));
679506b1a0cSSebastian Grimberg   if (elem_rstr_curl_orients_in || elem_rstr_curl_orients_out) CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat_b));
6801c66c397SJeremy L Thompson 
68128ec399dSJeremy L Thompson   CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals));
682506b1a0cSSebastian Grimberg   for (CeedSize e = 0; e < num_elem_in; e++) {
683506b1a0cSSebastian Grimberg     for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) {
684506b1a0cSSebastian Grimberg       for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) {
685ed9e99e6SJeremy L Thompson         // Compute B^T*D
686506b1a0cSSebastian Grimberg         for (CeedSize n = 0; n < elem_size_out; n++) {
687506b1a0cSSebastian Grimberg           for (CeedSize q = 0; q < num_qpts_in; q++) {
688437c7c90SJeremy L Thompson             for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) {
689506b1a0cSSebastian Grimberg               const CeedSize btd_index = n * (num_qpts_in * num_eval_modes_in[0]) + q * num_eval_modes_in[0] + e_in;
690067fd99fSJeremy L Thompson               CeedScalar     sum       = 0.0;
6911c66c397SJeremy L Thompson 
692437c7c90SJeremy L Thompson               for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) {
693506b1a0cSSebastian Grimberg                 const CeedSize b_out_index     = (q * num_eval_modes_out[0] + e_out) * elem_size_out + n;
694506b1a0cSSebastian 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;
695b94338b9SJed Brown                 const CeedSize qf_index        = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2];
6961c66c397SJeremy L Thompson 
697067fd99fSJeremy L Thompson                 sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index];
698eaf62fffSJeremy L Thompson               }
699067fd99fSJeremy L Thompson               BTD_mat[btd_index] = sum;
700ed9e99e6SJeremy L Thompson             }
701ed9e99e6SJeremy L Thompson           }
702eaf62fffSJeremy L Thompson         }
7037c1dbaffSSebastian Grimberg 
7047c1dbaffSSebastian Grimberg         // Form element matrix itself (for each block component)
705e4065a52SSebastian Grimberg         if (contract) {
7060459ebd3SSebastian Grimberg           CeedCall(CeedTensorContractApply(contract, 1, num_qpts_in * num_eval_modes_in[0], elem_size_in, elem_size_out, BTD_mat, CEED_NOTRANSPOSE,
7070459ebd3SSebastian Grimberg                                            false, B_mat_in, elem_mat));
708e4065a52SSebastian Grimberg         } else {
7099bc66399SJeremy L Thompson           Ceed ceed;
7109bc66399SJeremy L Thompson 
7119bc66399SJeremy L Thompson           CeedCall(CeedOperatorGetCeed(op, &ceed));
712e4065a52SSebastian 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]));
7139bc66399SJeremy L Thompson           CeedCall(CeedDestroy(&ceed));
714e4065a52SSebastian Grimberg         }
715eaf62fffSJeremy L Thompson 
7167c1dbaffSSebastian Grimberg         // Transform the element matrix if required
717506b1a0cSSebastian Grimberg         if (elem_rstr_orients_out) {
718506b1a0cSSebastian Grimberg           const bool *elem_orients = &elem_rstr_orients_out[e * elem_size_out];
7191c66c397SJeremy L Thompson 
720506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
721506b1a0cSSebastian Grimberg             const double orient = elem_orients[i] ? -1.0 : 1.0;
722506b1a0cSSebastian Grimberg 
723506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
724506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] *= orient;
7257c1dbaffSSebastian Grimberg             }
7267c1dbaffSSebastian Grimberg           }
727506b1a0cSSebastian Grimberg         } else if (elem_rstr_curl_orients_out) {
728506b1a0cSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_out[e * 3 * elem_size_out];
7291c66c397SJeremy L Thompson 
7307c1dbaffSSebastian Grimberg           // T^T*(B^T*D*B)
731506b1a0cSSebastian Grimberg           memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar));
732506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
733506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
734506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * i + 1] +
735506b1a0cSSebastian Grimberg                                                (i > 0 ? elem_mat_b[(i - 1) * elem_size_in + j] * elem_curl_orients[3 * i - 1] : 0.0) +
736506b1a0cSSebastian Grimberg                                                (i < elem_size_out - 1 ? elem_mat_b[(i + 1) * elem_size_in + j] * elem_curl_orients[3 * i + 3] : 0.0);
7377c1dbaffSSebastian Grimberg             }
7387c1dbaffSSebastian Grimberg           }
739506b1a0cSSebastian Grimberg         }
740506b1a0cSSebastian Grimberg         if (elem_rstr_orients_in) {
741506b1a0cSSebastian Grimberg           const bool *elem_orients = &elem_rstr_orients_in[e * elem_size_in];
742506b1a0cSSebastian Grimberg 
743506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
744506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
745506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] *= elem_orients[j] ? -1.0 : 1.0;
746506b1a0cSSebastian Grimberg             }
747506b1a0cSSebastian Grimberg           }
748506b1a0cSSebastian Grimberg         } else if (elem_rstr_curl_orients_in) {
749506b1a0cSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_in[e * 3 * elem_size_in];
750506b1a0cSSebastian Grimberg 
751506b1a0cSSebastian Grimberg           // (B^T*D*B)*T
752506b1a0cSSebastian Grimberg           memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar));
753506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
754506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
755506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * j + 1] +
756506b1a0cSSebastian Grimberg                                                (j > 0 ? elem_mat_b[i * elem_size_in + j - 1] * elem_curl_orients[3 * j - 1] : 0.0) +
757506b1a0cSSebastian Grimberg                                                (j < elem_size_in - 1 ? elem_mat_b[i * elem_size_in + j + 1] * elem_curl_orients[3 * j + 3] : 0.0);
7587c1dbaffSSebastian Grimberg             }
7597c1dbaffSSebastian Grimberg           }
7607c1dbaffSSebastian Grimberg         }
7617c1dbaffSSebastian Grimberg 
7627c1dbaffSSebastian Grimberg         // Put element matrix in coordinate data structure
763506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < elem_size_out; i++) {
764506b1a0cSSebastian Grimberg           for (CeedInt j = 0; j < elem_size_in; j++) {
765506b1a0cSSebastian Grimberg             vals[offset + count] = elem_mat[i * elem_size_in + j];
766eaf62fffSJeremy L Thompson             count++;
767eaf62fffSJeremy L Thompson           }
768eaf62fffSJeremy L Thompson         }
769eaf62fffSJeremy L Thompson       }
770eaf62fffSJeremy L Thompson     }
771eaf62fffSJeremy L Thompson   }
7729bc66399SJeremy L Thompson   CeedCheck(count == local_num_entries, CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR, "Error computing entries");
7732b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(values, &vals));
774eaf62fffSJeremy L Thompson 
775506b1a0cSSebastian Grimberg   // Cleanup
776123d890dSSebastian Grimberg   CeedCall(CeedFree(&BTD_mat));
777123d890dSSebastian Grimberg   CeedCall(CeedFree(&elem_mat));
778506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&elem_mat_b));
779506b1a0cSSebastian Grimberg   if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) {
780506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_in, &elem_rstr_orients_in));
781506b1a0cSSebastian Grimberg   } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
782506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_in, &elem_rstr_curl_orients_in));
783506b1a0cSSebastian Grimberg   }
784506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
785506b1a0cSSebastian Grimberg     if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) {
786506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_out, &elem_rstr_orients_out));
787506b1a0cSSebastian Grimberg     } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
788506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_out, &elem_rstr_curl_orients_out));
789506b1a0cSSebastian Grimberg     }
790506b1a0cSSebastian Grimberg   }
7912b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
7922b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
793681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&elem_rstr_in));
794681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&elem_rstr_out));
795eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
796eaf62fffSJeremy L Thompson }
797eaf62fffSJeremy L Thompson 
798eaf62fffSJeremy L Thompson /**
799ca94c3ddSJeremy L Thompson   @brief Count number of entries for assembled `CeedOperator`
800eaf62fffSJeremy L Thompson 
801ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to assemble
802eaf62fffSJeremy L Thompson   @param[out] num_entries Number of entries in assembled representation
803eaf62fffSJeremy L Thompson 
804eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
805eaf62fffSJeremy L Thompson 
806eaf62fffSJeremy L Thompson   @ref Utility
807eaf62fffSJeremy L Thompson **/
808b94338b9SJed Brown static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedSize *num_entries) {
809b275c451SJeremy L Thompson   bool                is_composite;
810506b1a0cSSebastian Grimberg   CeedInt             num_elem_in, elem_size_in, num_comp_in, num_elem_out, elem_size_out, num_comp_out;
811506b1a0cSSebastian Grimberg   CeedElemRestriction rstr_in, rstr_out;
812eaf62fffSJeremy L Thompson 
813b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
8149bc66399SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
815506b1a0cSSebastian Grimberg 
816506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
817506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
818506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
819506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
820506b1a0cSSebastian Grimberg   if (rstr_in != rstr_out) {
821506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
8229bc66399SJeremy L Thompson     CeedCheck(num_elem_in == num_elem_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
8233f08121cSJeremy L Thompson               "Active input and output operator restrictions must have the same number of elements."
8243f08121cSJeremy L Thompson               " Input has %" CeedInt_FMT " elements; output has %" CeedInt_FMT "elements.",
8253f08121cSJeremy L Thompson               num_elem_in, num_elem_out);
826506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
827506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
828506b1a0cSSebastian Grimberg   } else {
829506b1a0cSSebastian Grimberg     num_elem_out  = num_elem_in;
830506b1a0cSSebastian Grimberg     elem_size_out = elem_size_in;
831506b1a0cSSebastian Grimberg     num_comp_out  = num_comp_in;
832506b1a0cSSebastian Grimberg   }
833681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_in));
834681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_out));
835506b1a0cSSebastian Grimberg   *num_entries = (CeedSize)elem_size_in * num_comp_in * elem_size_out * num_comp_out * num_elem_in;
836eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
837eaf62fffSJeremy L Thompson }
838eaf62fffSJeremy L Thompson 
839eaf62fffSJeremy L Thompson /**
840ca94c3ddSJeremy L Thompson   @brief Common code for creating a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator`
841eaf62fffSJeremy L Thompson 
842ca94c3ddSJeremy L Thompson   @param[in]  op_fine      Fine grid `CeedOperator`
843ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
844ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid `CeedElemRestriction`
845ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector `CeedBasis`
846ca94c3ddSJeremy L Thompson   @param[in]  basis_c_to_f `CeedBasis` for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction operators
847ca94c3ddSJeremy L Thompson   @param[out] op_coarse    Coarse grid `CeedOperator`
848ca94c3ddSJeremy L Thompson   @param[out] op_prolong   Coarse to fine `CeedOperator`, or `NULL`
849ca94c3ddSJeremy L Thompson   @param[out] op_restrict  Fine to coarse `CeedOperator`, or `NULL`
850eaf62fffSJeremy L Thompson 
851eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
852eaf62fffSJeremy L Thompson 
853eaf62fffSJeremy L Thompson   @ref Developer
854eaf62fffSJeremy L Thompson **/
8552b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
8567758292fSSebastian Grimberg                                             CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
8571c66c397SJeremy L Thompson   bool                is_composite;
858eaf62fffSJeremy L Thompson   Ceed                ceed;
8591203703bSJeremy L Thompson   CeedInt             num_comp, num_input_fields, num_output_fields;
86085bb9dcfSJeremy L Thompson   CeedVector          mult_vec         = NULL;
8611c66c397SJeremy L Thompson   CeedElemRestriction rstr_p_mult_fine = NULL, rstr_fine = NULL;
8621203703bSJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
8631c66c397SJeremy L Thompson 
8642b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
865eaf62fffSJeremy L Thompson 
866eaf62fffSJeremy L Thompson   // Check for composite operator
8672b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op_fine, &is_composite));
8686574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported");
869eaf62fffSJeremy L Thompson 
870eaf62fffSJeremy L Thompson   // Coarse Grid
8712b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
8721203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op_fine, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
873eaf62fffSJeremy L Thompson   // -- Clone input fields
8741203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
8756f8994e9SJeremy L Thompson     const char         *field_name;
8761203703bSJeremy L Thompson     CeedVector          vec;
877681d0ea7SJeremy L Thompson     CeedElemRestriction rstr  = NULL;
878681d0ea7SJeremy L Thompson     CeedBasis           basis = NULL;
8791203703bSJeremy L Thompson 
8801203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetName(input_fields[i], &field_name));
8811203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(input_fields[i], &vec));
8821203703bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
883681d0ea7SJeremy L Thompson       CeedCall(CeedElemRestrictionReferenceCopy(rstr_coarse, &rstr));
884681d0ea7SJeremy L Thompson       CeedCall(CeedBasisReferenceCopy(basis_coarse, &basis));
885681d0ea7SJeremy L Thompson       if (!rstr_fine) CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_fine));
886eaf62fffSJeremy L Thompson     } else {
8871203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr));
8881203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis));
889eaf62fffSJeremy L Thompson     }
8901203703bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec));
891681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
892681d0ea7SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr));
893681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis));
894eaf62fffSJeremy L Thompson   }
895eaf62fffSJeremy L Thompson   // -- Clone output fields
8961203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
8976f8994e9SJeremy L Thompson     const char         *field_name;
8981203703bSJeremy L Thompson     CeedVector          vec;
899681d0ea7SJeremy L Thompson     CeedElemRestriction rstr  = NULL;
900681d0ea7SJeremy L Thompson     CeedBasis           basis = NULL;
9011203703bSJeremy L Thompson 
9021203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetName(output_fields[i], &field_name));
9031203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
9041203703bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
905681d0ea7SJeremy L Thompson       CeedCall(CeedElemRestrictionReferenceCopy(rstr_coarse, &rstr));
906681d0ea7SJeremy L Thompson       CeedCall(CeedBasisReferenceCopy(basis_coarse, &basis));
907681d0ea7SJeremy L Thompson       if (!rstr_fine) CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_fine));
908eaf62fffSJeremy L Thompson     } else {
9091203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr));
9101203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis));
911eaf62fffSJeremy L Thompson     }
9121203703bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec));
913681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
914681d0ea7SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr));
915681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis));
916eaf62fffSJeremy L Thompson   }
917af99e877SJeremy L Thompson   // -- Clone QFunctionAssemblyData
9187d5185d7SSebastian Grimberg   {
9197d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData fine_data;
9207d5185d7SSebastian Grimberg 
9217d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op_fine, &fine_data));
9227d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataReferenceCopy(fine_data, &(*op_coarse)->qf_assembled));
9237d5185d7SSebastian Grimberg   }
924eaf62fffSJeremy L Thompson 
925eaf62fffSJeremy L Thompson   // Multiplicity vector
9267758292fSSebastian Grimberg   if (op_restrict || op_prolong) {
92785bb9dcfSJeremy L Thompson     CeedVector          mult_e_vec;
9281c66c397SJeremy L Thompson     CeedRestrictionType rstr_type;
92985bb9dcfSJeremy L Thompson 
9307c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr_fine, &rstr_type));
9317c1dbaffSSebastian Grimberg     CeedCheck(rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_UNSUPPORTED,
9327c1dbaffSSebastian Grimberg               "Element restrictions created with CeedElemRestrictionCreateCurlOriented are not supported");
9336574a04fSJeremy L Thompson     CeedCheck(p_mult_fine, ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector");
9347c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnsignedCopy(rstr_fine, &rstr_p_mult_fine));
9352b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec));
9362b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_e_vec, 0.0));
937c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE));
9382b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_vec, 0.0));
939c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE));
9402b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&mult_e_vec));
9412b730f8bSJeremy L Thompson     CeedCall(CeedVectorReciprocal(mult_vec));
94285bb9dcfSJeremy L Thompson   }
943eaf62fffSJeremy L Thompson 
944addd79feSZach Atkins   // Clone name
945addd79feSZach Atkins   bool   has_name = op_fine->name;
946addd79feSZach Atkins   size_t name_len = op_fine->name ? strlen(op_fine->name) : 0;
947addd79feSZach Atkins   CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name));
948addd79feSZach Atkins 
9497758292fSSebastian Grimberg   // Check that coarse to fine basis is provided if prolong/restrict operators are requested
9507758292fSSebastian Grimberg   CeedCheck(basis_c_to_f || (!op_restrict && !op_prolong), ceed, CEED_ERROR_INCOMPATIBLE,
9516574a04fSJeremy L Thompson             "Prolongation or restriction operator creation requires coarse-to-fine basis");
95283d6adf3SZach Atkins 
95385bb9dcfSJeremy L Thompson   // Restriction/Prolongation Operators
9542b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp));
955addd79feSZach Atkins 
956addd79feSZach Atkins   // Restriction
9577758292fSSebastian Grimberg   if (op_restrict) {
958eaf62fffSJeremy L Thompson     CeedInt             *num_comp_r_data;
95985bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_r;
9607758292fSSebastian Grimberg     CeedQFunction        qf_restrict;
96185bb9dcfSJeremy L Thompson 
9627758292fSSebastian Grimberg     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict));
9632b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_r_data));
964eaf62fffSJeremy L Thompson     num_comp_r_data[0] = num_comp;
9652b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r));
9662b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data));
9677758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r));
9682b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_r));
9697758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE));
9707758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE));
9717758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP));
9727758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp));
973eaf62fffSJeremy L Thompson 
9747758292fSSebastian Grimberg     CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict));
9757758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
9767758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
9777758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
978eaf62fffSJeremy L Thompson 
979addd79feSZach Atkins     // Set name
980addd79feSZach Atkins     char *restriction_name;
9811c66c397SJeremy L Thompson 
982addd79feSZach Atkins     CeedCall(CeedCalloc(17 + name_len, &restriction_name));
983addd79feSZach Atkins     sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
9847758292fSSebastian Grimberg     CeedCall(CeedOperatorSetName(*op_restrict, restriction_name));
985addd79feSZach Atkins     CeedCall(CeedFree(&restriction_name));
986addd79feSZach Atkins 
987addd79feSZach Atkins     // Check
9887758292fSSebastian Grimberg     CeedCall(CeedOperatorCheckReady(*op_restrict));
989addd79feSZach Atkins 
990addd79feSZach Atkins     // Cleanup
9917758292fSSebastian Grimberg     CeedCall(CeedQFunctionDestroy(&qf_restrict));
992addd79feSZach Atkins   }
993addd79feSZach Atkins 
994eaf62fffSJeremy L Thompson   // Prolongation
995addd79feSZach Atkins   if (op_prolong) {
996eaf62fffSJeremy L Thompson     CeedInt             *num_comp_p_data;
99785bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_p;
9981c66c397SJeremy L Thompson     CeedQFunction        qf_prolong;
99985bb9dcfSJeremy L Thompson 
100085bb9dcfSJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong));
10012b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_p_data));
1002eaf62fffSJeremy L Thompson     num_comp_p_data[0] = num_comp;
10032b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p));
10042b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data));
10052b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p));
10062b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_p));
10072b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP));
10082b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE));
10092b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE));
10102b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp));
1011eaf62fffSJeremy L Thompson 
10122b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong));
10132b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
1014356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
1015356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
1016eaf62fffSJeremy L Thompson 
1017addd79feSZach Atkins     // Set name
1018ea6b5821SJeremy L Thompson     char *prolongation_name;
10191c66c397SJeremy L Thompson 
10202b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(18 + name_len, &prolongation_name));
10212b730f8bSJeremy L Thompson     sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
10222b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name));
10232b730f8bSJeremy L Thompson     CeedCall(CeedFree(&prolongation_name));
1024addd79feSZach Atkins 
1025addd79feSZach Atkins     // Check
1026addd79feSZach Atkins     CeedCall(CeedOperatorCheckReady(*op_prolong));
1027addd79feSZach Atkins 
1028addd79feSZach Atkins     // Cleanup
1029addd79feSZach Atkins     CeedCall(CeedQFunctionDestroy(&qf_prolong));
1030ea6b5821SJeremy L Thompson   }
1031ea6b5821SJeremy L Thompson 
103258e4b056SJeremy L Thompson   // Check
103358e4b056SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(*op_coarse));
103458e4b056SJeremy L Thompson 
1035eaf62fffSJeremy L Thompson   // Cleanup
10369bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
10372b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_vec));
1038681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_fine));
1039c17ec2beSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine));
10402b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis_c_to_f));
1041eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1042eaf62fffSJeremy L Thompson }
1043eaf62fffSJeremy L Thompson 
1044eaf62fffSJeremy L Thompson /**
1045eaf62fffSJeremy L Thompson   @brief Build 1D mass matrix and Laplacian with perturbation
1046eaf62fffSJeremy L Thompson 
1047eaf62fffSJeremy L Thompson   @param[in]  interp_1d   Interpolation matrix in one dimension
1048eaf62fffSJeremy L Thompson   @param[in]  grad_1d     Gradient matrix in one dimension
1049eaf62fffSJeremy L Thompson   @param[in]  q_weight_1d Quadrature weights in one dimension
1050eaf62fffSJeremy L Thompson   @param[in]  P_1d        Number of basis nodes in one dimension
1051eaf62fffSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
1052eaf62fffSJeremy L Thompson   @param[in]  dim         Dimension of basis
1053eaf62fffSJeremy L Thompson   @param[out] mass        Assembled mass matrix in one dimension
1054eaf62fffSJeremy L Thompson   @param[out] laplace     Assembled perturbed Laplacian in one dimension
1055eaf62fffSJeremy L Thompson 
1056eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1057eaf62fffSJeremy L Thompson 
1058eaf62fffSJeremy L Thompson   @ref Developer
1059eaf62fffSJeremy L Thompson **/
10602c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
10612c2ea1dbSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d, CeedInt P_1d, CeedInt Q_1d,
10622c2ea1dbSJeremy L Thompson                                 CeedInt dim, CeedScalar *mass, CeedScalar *laplace) {
10632b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
1064eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
1065eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
10662b730f8bSJeremy 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];
1067eaf62fffSJeremy L Thompson       mass[i + j * P_1d] = sum;
1068eaf62fffSJeremy L Thompson     }
10692b730f8bSJeremy L Thompson   }
1070eaf62fffSJeremy L Thompson   // -- Laplacian
10712b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
1072eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
1073eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
10741c66c397SJeremy L Thompson 
10752b730f8bSJeremy 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];
1076eaf62fffSJeremy L Thompson       laplace[i + j * P_1d] = sum;
1077eaf62fffSJeremy L Thompson     }
10782b730f8bSJeremy L Thompson   }
1079eaf62fffSJeremy L Thompson   CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4;
10802b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation;
1081eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1082eaf62fffSJeremy L Thompson }
10832c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
1084eaf62fffSJeremy L Thompson 
1085eaf62fffSJeremy L Thompson /// @}
1086eaf62fffSJeremy L Thompson 
1087eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1088480fae85SJeremy L Thompson /// CeedOperator Backend API
1089480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1090480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend
1091480fae85SJeremy L Thompson /// @{
1092480fae85SJeremy L Thompson 
1093480fae85SJeremy L Thompson /**
1094004e4986SSebastian Grimberg   @brief Select correct basis matrix pointer based on @ref CeedEvalMode
1095004e4986SSebastian Grimberg 
1096004e4986SSebastian Grimberg   @param[in]  basis     `CeedBasis` from which to get the basis matrix
1097004e4986SSebastian Grimberg   @param[in]  eval_mode Current basis evaluation mode
1098004e4986SSebastian Grimberg   @param[in]  identity  Pointer to identity matrix
1099004e4986SSebastian Grimberg   @param[out] basis_ptr `CeedBasis` pointer to set
1100004e4986SSebastian Grimberg 
1101004e4986SSebastian Grimberg   @ref Backend
1102004e4986SSebastian Grimberg **/
1103004e4986SSebastian Grimberg int CeedOperatorGetBasisPointer(CeedBasis basis, CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar **basis_ptr) {
1104004e4986SSebastian Grimberg   switch (eval_mode) {
1105004e4986SSebastian Grimberg     case CEED_EVAL_NONE:
1106004e4986SSebastian Grimberg       *basis_ptr = identity;
1107004e4986SSebastian Grimberg       break;
1108004e4986SSebastian Grimberg     case CEED_EVAL_INTERP:
1109004e4986SSebastian Grimberg       CeedCall(CeedBasisGetInterp(basis, basis_ptr));
1110004e4986SSebastian Grimberg       break;
1111004e4986SSebastian Grimberg     case CEED_EVAL_GRAD:
1112004e4986SSebastian Grimberg       CeedCall(CeedBasisGetGrad(basis, basis_ptr));
1113004e4986SSebastian Grimberg       break;
1114004e4986SSebastian Grimberg     case CEED_EVAL_DIV:
1115004e4986SSebastian Grimberg       CeedCall(CeedBasisGetDiv(basis, basis_ptr));
1116004e4986SSebastian Grimberg       break;
1117004e4986SSebastian Grimberg     case CEED_EVAL_CURL:
1118004e4986SSebastian Grimberg       CeedCall(CeedBasisGetCurl(basis, basis_ptr));
1119004e4986SSebastian Grimberg       break;
1120004e4986SSebastian Grimberg     case CEED_EVAL_WEIGHT:
1121004e4986SSebastian Grimberg       break;  // Caught by QF Assembly
1122004e4986SSebastian Grimberg   }
1123004e4986SSebastian Grimberg   assert(*basis_ptr != NULL);
1124004e4986SSebastian Grimberg   return CEED_ERROR_SUCCESS;
1125004e4986SSebastian Grimberg }
1126004e4986SSebastian Grimberg 
1127004e4986SSebastian Grimberg /**
1128ca94c3ddSJeremy L Thompson   @brief Create point block restriction for active `CeedOperatorField`
1129506b1a0cSSebastian Grimberg 
1130ca94c3ddSJeremy L Thompson   @param[in]  rstr             Original `CeedElemRestriction` for active field
1131ca94c3ddSJeremy L Thompson   @param[out] point_block_rstr Address of the variable where the newly created `CeedElemRestriction` will be stored
1132506b1a0cSSebastian Grimberg 
1133506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1134506b1a0cSSebastian Grimberg 
1135506b1a0cSSebastian Grimberg   @ref Backend
1136506b1a0cSSebastian Grimberg **/
1137506b1a0cSSebastian Grimberg int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *point_block_rstr) {
1138506b1a0cSSebastian Grimberg   Ceed           ceed;
1139506b1a0cSSebastian Grimberg   CeedInt        num_elem, num_comp, shift, elem_size, comp_stride, *point_block_offsets;
1140506b1a0cSSebastian Grimberg   CeedSize       l_size;
1141506b1a0cSSebastian Grimberg   const CeedInt *offsets;
1142506b1a0cSSebastian Grimberg 
1143506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
1144506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
1145506b1a0cSSebastian Grimberg 
1146506b1a0cSSebastian Grimberg   // Expand offsets
1147506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
1148506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1149506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
1150506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
1151506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
1152506b1a0cSSebastian Grimberg   shift = num_comp;
1153506b1a0cSSebastian Grimberg   if (comp_stride != 1) shift *= num_comp;
1154506b1a0cSSebastian Grimberg   CeedCall(CeedCalloc(num_elem * elem_size, &point_block_offsets));
1155506b1a0cSSebastian Grimberg   for (CeedInt i = 0; i < num_elem * elem_size; i++) {
1156506b1a0cSSebastian Grimberg     point_block_offsets[i] = offsets[i] * shift;
1157506b1a0cSSebastian Grimberg   }
1158506b1a0cSSebastian Grimberg 
1159506b1a0cSSebastian Grimberg   // Create new restriction
1160506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
1161506b1a0cSSebastian Grimberg                                      point_block_offsets, point_block_rstr));
1162506b1a0cSSebastian Grimberg 
1163506b1a0cSSebastian Grimberg   // Cleanup
1164506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
11659bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
1166506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1167506b1a0cSSebastian Grimberg }
1168506b1a0cSSebastian Grimberg 
1169506b1a0cSSebastian Grimberg /**
11707d5185d7SSebastian Grimberg   @brief Get `CeedQFunctionAssemblyData`
11717d5185d7SSebastian Grimberg 
11727d5185d7SSebastian Grimberg   @param[in]  op   `CeedOperator` to assemble
11737d5185d7SSebastian Grimberg   @param[out] data `CeedQFunctionAssemblyData`
11747d5185d7SSebastian Grimberg 
11757d5185d7SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
11767d5185d7SSebastian Grimberg 
11777d5185d7SSebastian Grimberg   @ref Backend
11787d5185d7SSebastian Grimberg **/
11797d5185d7SSebastian Grimberg int CeedOperatorGetQFunctionAssemblyData(CeedOperator op, CeedQFunctionAssemblyData *data) {
11807d5185d7SSebastian Grimberg   if (!op->qf_assembled) {
11817d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
11827d5185d7SSebastian Grimberg 
11837d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataCreate(op->ceed, &data));
11847d5185d7SSebastian Grimberg     op->qf_assembled = data;
11857d5185d7SSebastian Grimberg   }
11867d5185d7SSebastian Grimberg   *data = op->qf_assembled;
11877d5185d7SSebastian Grimberg   return CEED_ERROR_SUCCESS;
11887d5185d7SSebastian Grimberg }
11897d5185d7SSebastian Grimberg 
11907d5185d7SSebastian Grimberg /**
1191ca94c3ddSJeremy L Thompson   @brief Create object holding `CeedQFunction` assembly data for `CeedOperator`
1192480fae85SJeremy L Thompson 
1193ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedQFunctionAssemblyData`
1194ca94c3ddSJeremy L Thompson   @param[out] data Address of the variable where the newly created `CeedQFunctionAssemblyData` will be stored
1195480fae85SJeremy L Thompson 
1196480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1197480fae85SJeremy L Thompson 
1198480fae85SJeremy L Thompson   @ref Backend
1199480fae85SJeremy L Thompson **/
1200ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) {
12012b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1202480fae85SJeremy L Thompson   (*data)->ref_count = 1;
1203480fae85SJeremy L Thompson   (*data)->ceed      = ceed;
12042b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1205480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1206480fae85SJeremy L Thompson }
1207480fae85SJeremy L Thompson 
1208480fae85SJeremy L Thompson /**
1209ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedQFunctionAssemblyData`
1210480fae85SJeremy L Thompson 
1211ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to increment the reference counter
1212480fae85SJeremy L Thompson 
1213480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1214480fae85SJeremy L Thompson 
1215480fae85SJeremy L Thompson   @ref Backend
1216480fae85SJeremy L Thompson **/
1217480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) {
1218480fae85SJeremy L Thompson   data->ref_count++;
1219480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1220480fae85SJeremy L Thompson }
1221480fae85SJeremy L Thompson 
1222480fae85SJeremy L Thompson /**
1223ca94c3ddSJeremy L Thompson   @brief Set re-use of `CeedQFunctionAssemblyData`
12248b919e6bSJeremy L Thompson 
1225ca94c3ddSJeremy L Thompson   @param[in,out] data       `CeedQFunctionAssemblyData` to mark for reuse
1226ea61e9acSJeremy L Thompson   @param[in]     reuse_data Boolean flag indicating data re-use
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 CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) {
1233beecbf24SJeremy L Thompson   data->reuse_data        = reuse_data;
1234beecbf24SJeremy L Thompson   data->needs_data_update = true;
1235beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1236beecbf24SJeremy L Thompson }
1237beecbf24SJeremy L Thompson 
1238beecbf24SJeremy L Thompson /**
1239ca94c3ddSJeremy L Thompson   @brief Mark `CeedQFunctionAssemblyData` as stale
1240beecbf24SJeremy L Thompson 
1241ca94c3ddSJeremy L Thompson   @param[in,out] data              `CeedQFunctionAssemblyData` to mark as stale
1242ea61e9acSJeremy L Thompson   @param[in]     needs_data_update Boolean flag indicating if update is needed or completed
1243beecbf24SJeremy L Thompson 
1244beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1245beecbf24SJeremy L Thompson 
1246beecbf24SJeremy L Thompson   @ref Backend
1247beecbf24SJeremy L Thompson **/
12482b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) {
1249beecbf24SJeremy L Thompson   data->needs_data_update = needs_data_update;
12508b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12518b919e6bSJeremy L Thompson }
12528b919e6bSJeremy L Thompson 
12538b919e6bSJeremy L Thompson /**
1254ca94c3ddSJeremy L Thompson   @brief Determine if `CeedQFunctionAssemblyData` needs update
12558b919e6bSJeremy L Thompson 
1256ca94c3ddSJeremy L Thompson   @param[in]  data             `CeedQFunctionAssemblyData` to mark as stale
12578b919e6bSJeremy L Thompson   @param[out] is_update_needed Boolean flag indicating if re-assembly is required
12588b919e6bSJeremy L Thompson 
12598b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12608b919e6bSJeremy L Thompson 
12618b919e6bSJeremy L Thompson   @ref Backend
12628b919e6bSJeremy L Thompson **/
12632b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) {
1264beecbf24SJeremy L Thompson   *is_update_needed = !data->reuse_data || data->needs_data_update;
12658b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12668b919e6bSJeremy L Thompson }
12678b919e6bSJeremy L Thompson 
12688b919e6bSJeremy L Thompson /**
1269ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedQFunctionAssemblyData`.
12704385fb7fSSebastian Grimberg 
1271ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedQFunctionAssemblyDataDestroy().
1272512bb800SJeremy L Thompson 
1273ca94c3ddSJeremy 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`.
1274ca94c3ddSJeremy L Thompson         This `CeedQFunctionAssemblyData` will be destroyed if ` *data_copy` is the only reference to this `CeedQFunctionAssemblyData`.
1275480fae85SJeremy L Thompson 
1276ca94c3ddSJeremy L Thompson   @param[in]     data      `CeedQFunctionAssemblyData` to copy reference to
1277ea61e9acSJeremy L Thompson   @param[in,out] data_copy Variable to store copied reference
1278480fae85SJeremy L Thompson 
1279480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1280480fae85SJeremy L Thompson 
1281480fae85SJeremy L Thompson   @ref Backend
1282480fae85SJeremy L Thompson **/
12832b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) {
12842b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReference(data));
12852b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy));
1286480fae85SJeremy L Thompson   *data_copy = data;
1287480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1288480fae85SJeremy L Thompson }
1289480fae85SJeremy L Thompson 
1290480fae85SJeremy L Thompson /**
1291ca94c3ddSJeremy L Thompson   @brief Get setup status for internal objects for `CeedQFunctionAssemblyData`
1292480fae85SJeremy L Thompson 
1293ca94c3ddSJeremy L Thompson   @param[in]  data     `CeedQFunctionAssemblyData` to retrieve status
1294480fae85SJeremy L Thompson   @param[out] is_setup Boolean flag for setup status
1295480fae85SJeremy L Thompson 
1296480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1297480fae85SJeremy L Thompson 
1298480fae85SJeremy L Thompson   @ref Backend
1299480fae85SJeremy L Thompson **/
13002b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) {
1301480fae85SJeremy L Thompson   *is_setup = data->is_setup;
1302480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1303480fae85SJeremy L Thompson }
1304480fae85SJeremy L Thompson 
1305480fae85SJeremy L Thompson /**
1306ca94c3ddSJeremy L Thompson   @brief Set internal objects for `CeedQFunctionAssemblyData`
1307480fae85SJeremy L Thompson 
1308ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to set objects
1309ca94c3ddSJeremy L Thompson   @param[in]     vec  `CeedVector` to store assembled `CeedQFunction` at quadrature points
1310ca94c3ddSJeremy L Thompson   @param[in]     rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1311480fae85SJeremy L Thompson 
1312480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1313480fae85SJeremy L Thompson 
1314480fae85SJeremy L Thompson   @ref Backend
1315480fae85SJeremy L Thompson **/
13162b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) {
13172b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(vec, &data->vec));
13182b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr));
1319480fae85SJeremy L Thompson 
1320480fae85SJeremy L Thompson   data->is_setup = true;
1321480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1322480fae85SJeremy L Thompson }
1323480fae85SJeremy L Thompson 
13244dd1a9d2SSebastian Grimberg /**
1325ca94c3ddSJeremy L Thompson   @brief Get internal objects for `CeedQFunctionAssemblyData`
13264dd1a9d2SSebastian Grimberg 
1327ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to set objects
1328ca94c3ddSJeremy L Thompson   @param[out]    vec  `CeedVector` to store assembled `CeedQFunction` at quadrature points
1329ca94c3ddSJeremy L Thompson   @param[out]    rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
13304dd1a9d2SSebastian Grimberg 
13314dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
13324dd1a9d2SSebastian Grimberg 
13334dd1a9d2SSebastian Grimberg   @ref Backend
13344dd1a9d2SSebastian Grimberg **/
13352b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) {
13366574a04fSJeremy L Thompson   CeedCheck(data->is_setup, data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first.");
1337480fae85SJeremy L Thompson 
13382b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(data->vec, vec));
13392b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr));
1340480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1341480fae85SJeremy L Thompson }
1342480fae85SJeremy L Thompson 
1343480fae85SJeremy L Thompson /**
1344ca94c3ddSJeremy L Thompson   @brief Destroy `CeedQFunctionAssemblyData`
1345480fae85SJeremy L Thompson 
1346ca94c3ddSJeremy L Thompson   @param[in,out] data  `CeedQFunctionAssemblyData` to destroy
1347480fae85SJeremy L Thompson 
1348480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1349480fae85SJeremy L Thompson 
1350480fae85SJeremy L Thompson   @ref Backend
1351480fae85SJeremy L Thompson **/
1352480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) {
1353ad6481ceSJeremy L Thompson   if (!*data || --(*data)->ref_count > 0) {
1354ad6481ceSJeremy L Thompson     *data = NULL;
1355ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1356ad6481ceSJeremy L Thompson   }
13572b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
13582b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*data)->vec));
13592b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr));
1360480fae85SJeremy L Thompson 
13612b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1362480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1363480fae85SJeremy L Thompson }
1364480fae85SJeremy L Thompson 
1365ed9e99e6SJeremy L Thompson /**
1366ca94c3ddSJeremy L Thompson   @brief Get `CeedOperatorAssemblyData`
1367ed9e99e6SJeremy L Thompson 
1368ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator` to assemble
13697d5185d7SSebastian Grimberg   @param[out] data `CeedOperatorAssemblyData`
1370ed9e99e6SJeremy L Thompson 
1371ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1372ed9e99e6SJeremy L Thompson 
1373ed9e99e6SJeremy L Thompson   @ref Backend
1374ed9e99e6SJeremy L Thompson **/
13752b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) {
1376ed9e99e6SJeremy L Thompson   if (!op->op_assembled) {
1377ed9e99e6SJeremy L Thompson     CeedOperatorAssemblyData data;
1378ed9e99e6SJeremy L Thompson 
13792b730f8bSJeremy L Thompson     CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data));
1380ed9e99e6SJeremy L Thompson     op->op_assembled = data;
1381ed9e99e6SJeremy L Thompson   }
1382ed9e99e6SJeremy L Thompson   *data = op->op_assembled;
1383ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1384ed9e99e6SJeremy L Thompson }
1385ed9e99e6SJeremy L Thompson 
1386ed9e99e6SJeremy L Thompson /**
1387ca94c3ddSJeremy L Thompson   @brief Create object holding `CeedOperator` assembly data.
1388ba746a46SJeremy L Thompson 
1389ca94c3ddSJeremy L Thompson   The `CeedOperatorAssemblyData` holds an array with references to every active `CeedBasis` used in the `CeedOperator`.
1390ca94c3ddSJeremy L Thompson   An array with references to the corresponding active `CeedElemRestriction` is also stored.
1391ca94c3ddSJeremy L Thompson   For each active `CeedBasis, the `CeedOperatorAssemblyData` holds an array of all input and output @ref CeedEvalMode for this `CeedBasis`.
1392ca94c3ddSJeremy L Thompson   The `CeedOperatorAssemblyData` holds an array of offsets for indexing into the assembled `CeedQFunction` arrays to the row representing each @ref CeedEvalMode.
1393ca94c3ddSJeremy L Thompson   The number of input columns across all active bases for the assembled `CeedQFunction` is also stored.
1394ca94c3ddSJeremy L Thompson   Lastly, the `CeedOperatorAssembly` data holds assembled matrices representing the full action of the `CeedBasis` for all @ref CeedEvalMode.
1395ed9e99e6SJeremy L Thompson 
1396ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperatorAssemblyData`
1397ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator` to be assembled
1398ca94c3ddSJeremy L Thompson   @param[out] data Address of the variable where the newly created `CeedOperatorAssemblyData` will be stored
1399ed9e99e6SJeremy L Thompson 
1400ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1401ed9e99e6SJeremy L Thompson 
1402ed9e99e6SJeremy L Thompson   @ref Backend
1403ed9e99e6SJeremy L Thompson **/
14042b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) {
1405506b1a0cSSebastian Grimberg   CeedInt             num_active_bases_in = 0, num_active_bases_out = 0, offset = 0;
1406506b1a0cSSebastian Grimberg   CeedInt             num_input_fields, *num_eval_modes_in = NULL, num_output_fields, *num_eval_modes_out = NULL;
14071c66c397SJeremy L Thompson   CeedSize          **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL;
14081c66c397SJeremy L Thompson   CeedEvalMode      **eval_modes_in = NULL, **eval_modes_out = NULL;
14091c66c397SJeremy L Thompson   CeedQFunctionField *qf_fields;
14101c66c397SJeremy L Thompson   CeedQFunction       qf;
14111c66c397SJeremy L Thompson   CeedOperatorField  *op_fields;
141201f0e615SJames Wright   bool                is_composite;
141301f0e615SJames Wright 
141401f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
141501f0e615SJames Wright   CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Can only create CeedOperator assembly data for non-composite operators.");
1416437c7c90SJeremy L Thompson 
1417437c7c90SJeremy L Thompson   // Allocate
14182b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1419ed9e99e6SJeremy L Thompson   (*data)->ceed = ceed;
14202b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1421ed9e99e6SJeremy L Thompson 
1422ed9e99e6SJeremy L Thompson   // Build OperatorAssembly data
14232b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
1424ed9e99e6SJeremy L Thompson 
1425ed9e99e6SJeremy L Thompson   // Determine active input basis
1426004e4986SSebastian Grimberg   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL));
1427004e4986SSebastian Grimberg   CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1428ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1429ed9e99e6SJeremy L Thompson     CeedVector vec;
14301c66c397SJeremy L Thompson 
14312b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1432ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
14337c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
14341c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
14351c66c397SJeremy L Thompson       CeedBasis    basis_in = NULL;
14361c66c397SJeremy L Thompson 
14372b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
14382b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1439352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp));
1440352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1441506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_active_bases_in; i++) {
1442506b1a0cSSebastian Grimberg         if ((*data)->active_bases_in[i] == basis_in) index = i;
1443437c7c90SJeremy L Thompson       }
1444437c7c90SJeremy L Thompson       if (index == -1) {
1445437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_in;
14461c66c397SJeremy L Thompson 
1447506b1a0cSSebastian Grimberg         index = num_active_bases_in;
1448506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_bases_in));
1449506b1a0cSSebastian Grimberg         (*data)->active_bases_in[num_active_bases_in] = NULL;
1450506b1a0cSSebastian Grimberg         CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases_in[num_active_bases_in]));
1451506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_elem_rstrs_in));
1452506b1a0cSSebastian Grimberg         (*data)->active_elem_rstrs_in[num_active_bases_in] = NULL;
1453437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in));
1454506b1a0cSSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs_in[num_active_bases_in]));
1455681d0ea7SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&elem_rstr_in));
1456506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &num_eval_modes_in));
1457437c7c90SJeremy L Thompson         num_eval_modes_in[index] = 0;
1458506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_modes_in));
1459437c7c90SJeremy L Thompson         eval_modes_in[index] = NULL;
1460506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_mode_offsets_in));
1461437c7c90SJeremy L Thompson         eval_mode_offsets_in[index] = NULL;
1462506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->assembled_bases_in));
1463437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index] = NULL;
1464506b1a0cSSebastian Grimberg         num_active_bases_in++;
1465437c7c90SJeremy L Thompson       }
1466352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1467352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1468352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_modes_in[index]));
1469352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_mode_offsets_in[index]));
1470352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1471437c7c90SJeremy L Thompson           eval_modes_in[index][num_eval_modes_in[index] + d]        = eval_mode;
1472437c7c90SJeremy L Thompson           eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset;
1473352a5e7cSSebastian Grimberg           offset += num_comp;
1474ed9e99e6SJeremy L Thompson         }
1475352a5e7cSSebastian Grimberg         num_eval_modes_in[index] += q_comp;
1476ed9e99e6SJeremy L Thompson       }
1477681d0ea7SJeremy L Thompson       CeedCall(CeedBasisDestroy(&basis_in));
1478ed9e99e6SJeremy L Thompson     }
1479681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
1480ed9e99e6SJeremy L Thompson   }
1481ed9e99e6SJeremy L Thompson 
1482ed9e99e6SJeremy L Thompson   // Determine active output basis
14832b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields));
14842b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1485437c7c90SJeremy L Thompson   offset = 0;
1486ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1487ed9e99e6SJeremy L Thompson     CeedVector vec;
14881c66c397SJeremy L Thompson 
14892b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1490ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
14917c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
14921c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
14931c66c397SJeremy L Thompson       CeedBasis    basis_out = NULL;
14941c66c397SJeremy L Thompson 
1495437c7c90SJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
14962b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1497352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_out, &num_comp));
1498352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1499506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_active_bases_out; i++) {
1500506b1a0cSSebastian Grimberg         if ((*data)->active_bases_out[i] == basis_out) index = i;
1501437c7c90SJeremy L Thompson       }
1502437c7c90SJeremy L Thompson       if (index == -1) {
1503437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_out;
15041c66c397SJeremy L Thompson 
1505506b1a0cSSebastian Grimberg         index = num_active_bases_out;
1506506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_bases_out));
1507506b1a0cSSebastian Grimberg         (*data)->active_bases_out[num_active_bases_out] = NULL;
1508506b1a0cSSebastian Grimberg         CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases_out[num_active_bases_out]));
1509506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_elem_rstrs_out));
1510506b1a0cSSebastian Grimberg         (*data)->active_elem_rstrs_out[num_active_bases_out] = NULL;
1511437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out));
1512506b1a0cSSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs_out[num_active_bases_out]));
1513681d0ea7SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&elem_rstr_out));
1514506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &num_eval_modes_out));
1515437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1516506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_modes_out));
1517437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1518506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_mode_offsets_out));
1519437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1520506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->assembled_bases_out));
1521437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1522506b1a0cSSebastian Grimberg         num_active_bases_out++;
1523437c7c90SJeremy L Thompson       }
1524352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1525352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1526352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_modes_out[index]));
1527352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_mode_offsets_out[index]));
1528352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1529437c7c90SJeremy L Thompson           eval_modes_out[index][num_eval_modes_out[index] + d]        = eval_mode;
1530437c7c90SJeremy L Thompson           eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset;
1531352a5e7cSSebastian Grimberg           offset += num_comp;
1532ed9e99e6SJeremy L Thompson         }
1533352a5e7cSSebastian Grimberg         num_eval_modes_out[index] += q_comp;
1534ed9e99e6SJeremy L Thompson       }
1535681d0ea7SJeremy L Thompson       CeedCall(CeedBasisDestroy(&basis_out));
1536ed9e99e6SJeremy L Thompson     }
1537681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
1538ed9e99e6SJeremy L Thompson   }
1539c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
1540506b1a0cSSebastian Grimberg   (*data)->num_active_bases_in   = num_active_bases_in;
154127789c4aSJed Brown   (*data)->num_eval_modes_in     = num_eval_modes_in;
154227789c4aSJed Brown   (*data)->eval_modes_in         = eval_modes_in;
154327789c4aSJed Brown   (*data)->eval_mode_offsets_in  = eval_mode_offsets_in;
1544506b1a0cSSebastian Grimberg   (*data)->num_active_bases_out  = num_active_bases_out;
1545437c7c90SJeremy L Thompson   (*data)->num_eval_modes_out    = num_eval_modes_out;
1546437c7c90SJeremy L Thompson   (*data)->eval_modes_out        = eval_modes_out;
1547437c7c90SJeremy L Thompson   (*data)->eval_mode_offsets_out = eval_mode_offsets_out;
1548506b1a0cSSebastian Grimberg   (*data)->num_output_components = offset;
1549ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1550ed9e99e6SJeremy L Thompson }
1551ed9e99e6SJeremy L Thompson 
1552ed9e99e6SJeremy L Thompson /**
1553ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` @ref CeedEvalMode for assembly.
1554ba746a46SJeremy L Thompson 
1555ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1556ed9e99e6SJeremy L Thompson 
1557ca94c3ddSJeremy L Thompson   @param[in]  data                  `CeedOperatorAssemblyData`
1558506b1a0cSSebastian Grimberg   @param[out] num_active_bases_in   Total number of active bases for input
1559ca94c3ddSJeremy L Thompson   @param[out] num_eval_modes_in     Pointer to hold array of numbers of input @ref CeedEvalMode, or `NULL`.
1560ca94c3ddSJeremy L Thompson                                       `eval_modes_in[0]` holds an array of eval modes for the first active `CeedBasis`.
1561ca94c3ddSJeremy L Thompson   @param[out] eval_modes_in         Pointer to hold arrays of input @ref CeedEvalMode, or `NULL`
1562ca94c3ddSJeremy L Thompson   @param[out] eval_mode_offsets_in  Pointer to hold arrays of input offsets at each quadrature point
1563506b1a0cSSebastian Grimberg   @param[out] num_active_bases_out  Total number of active bases for output
1564ca94c3ddSJeremy L Thompson   @param[out] num_eval_modes_out    Pointer to hold array of numbers of output @ref CeedEvalMode, or `NULL`
1565ca94c3ddSJeremy L Thompson   @param[out] eval_modes_out        Pointer to hold arrays of output @ref CeedEvalMode, or `NULL`
1566437c7c90SJeremy L Thompson   @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point
1567ca94c3ddSJeremy 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
1568ed9e99e6SJeremy L Thompson 
1569ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1570ed9e99e6SJeremy L Thompson 
1571ed9e99e6SJeremy L Thompson   @ref Backend
1572ed9e99e6SJeremy L Thompson **/
1573506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedInt **num_eval_modes_in,
1574506b1a0cSSebastian Grimberg                                          const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt *num_active_bases_out,
1575506b1a0cSSebastian Grimberg                                          CeedInt **num_eval_modes_out, const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out,
1576506b1a0cSSebastian Grimberg                                          CeedSize *num_output_components) {
1577506b1a0cSSebastian Grimberg   if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in;
1578437c7c90SJeremy L Thompson   if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in;
1579437c7c90SJeremy L Thompson   if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in;
1580437c7c90SJeremy L Thompson   if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in;
1581506b1a0cSSebastian Grimberg   if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out;
1582437c7c90SJeremy L Thompson   if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out;
1583437c7c90SJeremy L Thompson   if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out;
1584437c7c90SJeremy L Thompson   if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out;
1585437c7c90SJeremy L Thompson   if (num_output_components) *num_output_components = data->num_output_components;
1586ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1587ed9e99e6SJeremy L Thompson }
1588ed9e99e6SJeremy L Thompson 
1589ed9e99e6SJeremy L Thompson /**
1590ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` `CeedBasis` data for assembly.
1591ba746a46SJeremy L Thompson 
1592ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1593ed9e99e6SJeremy L Thompson 
1594ca94c3ddSJeremy L Thompson   @param[in]  data                 `CeedOperatorAssemblyData`
1595ca94c3ddSJeremy L Thompson   @param[out] num_active_bases_in  Number of active input bases, or `NULL`
1596ca94c3ddSJeremy L Thompson   @param[out] active_bases_in      Pointer to hold active input `CeedBasis`, or `NULL`
1597ca94c3ddSJeremy L Thompson   @param[out] assembled_bases_in   Pointer to hold assembled active input `B` , or `NULL`
1598ca94c3ddSJeremy L Thompson   @param[out] num_active_bases_out Number of active output bases, or `NULL`
1599ca94c3ddSJeremy L Thompson   @param[out] active_bases_out     Pointer to hold active output `CeedBasis`, or `NULL`
1600ca94c3ddSJeremy L Thompson   @param[out] assembled_bases_out  Pointer to hold assembled active output `B` , or `NULL`
1601ed9e99e6SJeremy L Thompson 
1602ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1603ed9e99e6SJeremy L Thompson 
1604ed9e99e6SJeremy L Thompson   @ref Backend
1605ed9e99e6SJeremy L Thompson **/
1606506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedBasis **active_bases_in,
1607506b1a0cSSebastian Grimberg                                      const CeedScalar ***assembled_bases_in, CeedInt *num_active_bases_out, CeedBasis **active_bases_out,
1608506b1a0cSSebastian Grimberg                                      const CeedScalar ***assembled_bases_out) {
1609ed9e99e6SJeremy L Thompson   // Assemble B_in, B_out if needed
1610437c7c90SJeremy L Thompson   if (assembled_bases_in && !data->assembled_bases_in[0]) {
1611437c7c90SJeremy L Thompson     CeedInt num_qpts;
1612437c7c90SJeremy L Thompson 
1613506b1a0cSSebastian Grimberg     if (data->active_bases_in[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[0], &num_qpts));
1614506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_in[0], &num_qpts));
1615506b1a0cSSebastian Grimberg     for (CeedInt b = 0; b < data->num_active_bases_in; b++) {
16161c66c397SJeremy L Thompson       bool        has_eval_none = false;
1617352a5e7cSSebastian Grimberg       CeedInt     num_nodes;
1618437c7c90SJeremy L Thompson       CeedScalar *B_in = NULL, *identity = NULL;
1619ed9e99e6SJeremy L Thompson 
1620506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[b], &num_nodes));
1621352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_in[b], &B_in));
1622ed9e99e6SJeremy L Thompson 
1623437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) {
1624437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE);
1625ed9e99e6SJeremy L Thompson       }
1626ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1627352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1628352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1629352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1630ed9e99e6SJeremy L Thompson         }
1631ed9e99e6SJeremy L Thompson       }
1632ed9e99e6SJeremy L Thompson 
1633ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1634352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1635352a5e7cSSebastian Grimberg           CeedInt      d_in              = 0, q_comp_in;
1636352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE;
16371c66c397SJeremy L Thompson 
1638437c7c90SJeremy L Thompson           for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) {
1639437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_in[b] * q;
1640437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
16411c66c397SJeremy L Thompson 
1642506b1a0cSSebastian Grimberg             CeedCall(CeedOperatorGetBasisPointer(data->active_bases_in[b], data->eval_modes_in[b][e_in], identity, &B));
1643506b1a0cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_in[b], data->eval_modes_in[b][e_in], &q_comp_in));
1644352a5e7cSSebastian Grimberg             if (q_comp_in > 1) {
1645352a5e7cSSebastian Grimberg               if (e_in == 0 || data->eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0;
1646352a5e7cSSebastian Grimberg               else B = &B[(++d_in) * num_qpts * num_nodes];
1647352a5e7cSSebastian Grimberg             }
1648352a5e7cSSebastian Grimberg             eval_mode_in_prev                 = data->eval_modes_in[b][e_in];
1649352a5e7cSSebastian Grimberg             B_in[(qq + e_in) * num_nodes + n] = B[q * num_nodes + n];
1650ed9e99e6SJeremy L Thompson           }
1651ed9e99e6SJeremy L Thompson         }
1652ed9e99e6SJeremy L Thompson       }
16537c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1654437c7c90SJeremy L Thompson       data->assembled_bases_in[b] = B_in;
1655437c7c90SJeremy L Thompson     }
1656ed9e99e6SJeremy L Thompson   }
1657ed9e99e6SJeremy L Thompson 
1658437c7c90SJeremy L Thompson   if (assembled_bases_out && !data->assembled_bases_out[0]) {
1659437c7c90SJeremy L Thompson     CeedInt num_qpts;
1660437c7c90SJeremy L Thompson 
1661506b1a0cSSebastian Grimberg     if (data->active_bases_out[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[0], &num_qpts));
1662506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_out[0], &num_qpts));
1663506b1a0cSSebastian Grimberg     for (CeedInt b = 0; b < data->num_active_bases_out; b++) {
1664ed9e99e6SJeremy L Thompson       bool        has_eval_none = false;
16651c66c397SJeremy L Thompson       CeedInt     num_nodes;
1666437c7c90SJeremy L Thompson       CeedScalar *B_out = NULL, *identity = NULL;
1667ed9e99e6SJeremy L Thompson 
1668506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[b], &num_nodes));
1669352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_out[b], &B_out));
1670ed9e99e6SJeremy L Thompson 
1671437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) {
1672437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE);
1673ed9e99e6SJeremy L Thompson       }
1674ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1675352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1676352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1677352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1678ed9e99e6SJeremy L Thompson         }
1679ed9e99e6SJeremy L Thompson       }
1680ed9e99e6SJeremy L Thompson 
1681ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1682352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1683352a5e7cSSebastian Grimberg           CeedInt      d_out              = 0, q_comp_out;
1684352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
16851c66c397SJeremy L Thompson 
1686437c7c90SJeremy L Thompson           for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) {
1687437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_out[b] * q;
1688437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
16891c66c397SJeremy L Thompson 
1690506b1a0cSSebastian Grimberg             CeedCall(CeedOperatorGetBasisPointer(data->active_bases_out[b], data->eval_modes_out[b][e_out], identity, &B));
1691506b1a0cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_out[b], data->eval_modes_out[b][e_out], &q_comp_out));
1692352a5e7cSSebastian Grimberg             if (q_comp_out > 1) {
1693352a5e7cSSebastian Grimberg               if (e_out == 0 || data->eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0;
1694352a5e7cSSebastian Grimberg               else B = &B[(++d_out) * num_qpts * num_nodes];
1695352a5e7cSSebastian Grimberg             }
1696352a5e7cSSebastian Grimberg             eval_mode_out_prev                  = data->eval_modes_out[b][e_out];
1697352a5e7cSSebastian Grimberg             B_out[(qq + e_out) * num_nodes + n] = B[q * num_nodes + n];
1698ed9e99e6SJeremy L Thompson           }
1699ed9e99e6SJeremy L Thompson         }
1700ed9e99e6SJeremy L Thompson       }
17017c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1702437c7c90SJeremy L Thompson       data->assembled_bases_out[b] = B_out;
1703437c7c90SJeremy L Thompson     }
1704ed9e99e6SJeremy L Thompson   }
1705ed9e99e6SJeremy L Thompson 
1706437c7c90SJeremy L Thompson   // Pass out assembled data
1707506b1a0cSSebastian Grimberg   if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in;
1708506b1a0cSSebastian Grimberg   if (active_bases_in) *active_bases_in = data->active_bases_in;
1709437c7c90SJeremy L Thompson   if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in;
1710506b1a0cSSebastian Grimberg   if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out;
1711506b1a0cSSebastian Grimberg   if (active_bases_out) *active_bases_out = data->active_bases_out;
1712437c7c90SJeremy L Thompson   if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out;
1713437c7c90SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1714437c7c90SJeremy L Thompson }
1715437c7c90SJeremy L Thompson 
1716437c7c90SJeremy L Thompson /**
1717ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` `CeedBasis` data for assembly.
1718ba746a46SJeremy L Thompson 
1719ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1720437c7c90SJeremy L Thompson 
1721ca94c3ddSJeremy L Thompson   @param[in]  data                      `CeedOperatorAssemblyData`
1722ca94c3ddSJeremy L Thompson   @param[out] num_active_elem_rstrs_in  Number of active input element restrictions, or `NULL`
1723ca94c3ddSJeremy L Thompson   @param[out] active_elem_rstrs_in      Pointer to hold active input `CeedElemRestriction`, or `NULL`
1724ca94c3ddSJeremy L Thompson   @param[out] num_active_elem_rstrs_out Number of active output element restrictions, or `NULL`
1725ca94c3ddSJeremy L Thompson   @param[out] active_elem_rstrs_out     Pointer to hold active output `CeedElemRestriction`, or `NULL`
1726437c7c90SJeremy L Thompson 
1727437c7c90SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1728437c7c90SJeremy L Thompson 
1729437c7c90SJeremy L Thompson   @ref Backend
1730437c7c90SJeremy L Thompson **/
1731506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs_in,
1732506b1a0cSSebastian Grimberg                                                 CeedElemRestriction **active_elem_rstrs_in, CeedInt *num_active_elem_rstrs_out,
1733506b1a0cSSebastian Grimberg                                                 CeedElemRestriction **active_elem_rstrs_out) {
1734506b1a0cSSebastian Grimberg   if (num_active_elem_rstrs_in) *num_active_elem_rstrs_in = data->num_active_bases_in;
1735506b1a0cSSebastian Grimberg   if (active_elem_rstrs_in) *active_elem_rstrs_in = data->active_elem_rstrs_in;
1736506b1a0cSSebastian Grimberg   if (num_active_elem_rstrs_out) *num_active_elem_rstrs_out = data->num_active_bases_out;
1737506b1a0cSSebastian Grimberg   if (active_elem_rstrs_out) *active_elem_rstrs_out = data->active_elem_rstrs_out;
1738ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1739ed9e99e6SJeremy L Thompson }
1740ed9e99e6SJeremy L Thompson 
1741ed9e99e6SJeremy L Thompson /**
1742ca94c3ddSJeremy L Thompson   @brief Destroy `CeedOperatorAssemblyData`
1743ed9e99e6SJeremy L Thompson 
1744ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedOperatorAssemblyData` to destroy
1745ed9e99e6SJeremy L Thompson 
1746ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1747ed9e99e6SJeremy L Thompson 
1748ed9e99e6SJeremy L Thompson   @ref Backend
1749ed9e99e6SJeremy L Thompson **/
1750ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) {
1751ad6481ceSJeremy L Thompson   if (!*data) {
1752ad6481ceSJeremy L Thompson     *data = NULL;
1753ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1754ad6481ceSJeremy L Thompson   }
17552b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
1756506b1a0cSSebastian Grimberg   for (CeedInt b = 0; b < (*data)->num_active_bases_in; b++) {
1757506b1a0cSSebastian Grimberg     CeedCall(CeedBasisDestroy(&(*data)->active_bases_in[b]));
1758506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_in[b]));
1759437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_in[b]));
1760437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b]));
1761437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_in[b]));
1762506b1a0cSSebastian Grimberg   }
1763506b1a0cSSebastian Grimberg   for (CeedInt b = 0; b < (*data)->num_active_bases_out; b++) {
1764506b1a0cSSebastian Grimberg     CeedCall(CeedBasisDestroy(&(*data)->active_bases_out[b]));
1765506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_out[b]));
1766506b1a0cSSebastian Grimberg     CeedCall(CeedFree(&(*data)->eval_modes_out[b]));
1767506b1a0cSSebastian Grimberg     CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b]));
1768437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_out[b]));
1769437c7c90SJeremy L Thompson   }
1770506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_bases_in));
1771506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_bases_out));
1772506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_elem_rstrs_in));
1773506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_elem_rstrs_out));
1774437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_in));
1775437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_out));
1776437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_in));
1777437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_out));
1778437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_in));
1779437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_out));
1780437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_in));
1781437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_out));
1782ed9e99e6SJeremy L Thompson 
17832b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1784ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1785ed9e99e6SJeremy L Thompson }
1786ed9e99e6SJeremy L Thompson 
17874dd1a9d2SSebastian Grimberg /**
1788ca94c3ddSJeremy L Thompson   @brief Retrieve fallback `CeedOperator` with a reference `Ceed` for advanced `CeedOperator` functionality
17894dd1a9d2SSebastian Grimberg 
1790ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to retrieve fallback for
1791ca94c3ddSJeremy L Thompson   @param[out] op_fallback Fallback `CeedOperator`
17924dd1a9d2SSebastian Grimberg 
17934dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
17944dd1a9d2SSebastian Grimberg 
17954dd1a9d2SSebastian Grimberg   @ref Backend
17964dd1a9d2SSebastian Grimberg **/
17974dd1a9d2SSebastian Grimberg int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) {
17984dd1a9d2SSebastian Grimberg   // Create if needed
17994dd1a9d2SSebastian Grimberg   if (!op->op_fallback) CeedCall(CeedOperatorCreateFallback(op));
18004dd1a9d2SSebastian Grimberg   if (op->op_fallback) {
18014dd1a9d2SSebastian Grimberg     bool is_debug;
18021203703bSJeremy L Thompson     Ceed ceed;
18034dd1a9d2SSebastian Grimberg 
18044dd1a9d2SSebastian Grimberg     CeedCall(CeedOperatorGetCeed(op, &ceed));
18051203703bSJeremy L Thompson     CeedCall(CeedIsDebug(ceed, &is_debug));
18061203703bSJeremy L Thompson     if (is_debug) {
18071203703bSJeremy L Thompson       Ceed        ceed_fallback;
18081203703bSJeremy L Thompson       const char *resource, *resource_fallback;
18091203703bSJeremy L Thompson 
18104dd1a9d2SSebastian Grimberg       CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
18114dd1a9d2SSebastian Grimberg       CeedCall(CeedGetResource(ceed, &resource));
18124dd1a9d2SSebastian Grimberg       CeedCall(CeedGetResource(ceed_fallback, &resource_fallback));
18134dd1a9d2SSebastian Grimberg 
18144dd1a9d2SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n");
1815249f8407SJeremy 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);
18169bc66399SJeremy L Thompson       CeedCall(CeedDestroy(&ceed_fallback));
18174dd1a9d2SSebastian Grimberg     }
18189bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&ceed));
18194dd1a9d2SSebastian Grimberg   }
18204dd1a9d2SSebastian Grimberg   *op_fallback = op->op_fallback;
18214dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
18224dd1a9d2SSebastian Grimberg }
18234dd1a9d2SSebastian Grimberg 
18244dd1a9d2SSebastian Grimberg /**
1825ca94c3ddSJeremy L Thompson   @brief Get the parent `CeedOperator` for a fallback `CeedOperator`
18264dd1a9d2SSebastian Grimberg 
1827ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` context
1828ca94c3ddSJeremy L Thompson   @param[out] parent Variable to store parent `CeedOperator` context
18294dd1a9d2SSebastian Grimberg 
18304dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
18314dd1a9d2SSebastian Grimberg 
18324dd1a9d2SSebastian Grimberg   @ref Backend
18334dd1a9d2SSebastian Grimberg **/
18344dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParent(CeedOperator op, CeedOperator *parent) {
18354dd1a9d2SSebastian Grimberg   *parent = op->op_fallback_parent ? op->op_fallback_parent : NULL;
18364dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
18374dd1a9d2SSebastian Grimberg }
18384dd1a9d2SSebastian Grimberg 
18394dd1a9d2SSebastian Grimberg /**
1840ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` context of the parent `CeedOperator` for a fallback `CeedOperator`
18414dd1a9d2SSebastian Grimberg 
1842ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` context
1843ca94c3ddSJeremy L Thompson   @param[out] parent Variable to store parent `Ceed` context
18444dd1a9d2SSebastian Grimberg 
18454dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
18464dd1a9d2SSebastian Grimberg 
18474dd1a9d2SSebastian Grimberg   @ref Backend
18484dd1a9d2SSebastian Grimberg **/
18494dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParentCeed(CeedOperator op, Ceed *parent) {
18509bc66399SJeremy L Thompson   *parent = NULL;
18519bc66399SJeremy L Thompson   if (op->op_fallback_parent) CeedCall(CeedReferenceCopy(op->op_fallback_parent->ceed, parent));
18529bc66399SJeremy L Thompson   else CeedCall(CeedReferenceCopy(CeedOperatorReturnCeed(op), parent));
18534dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
18544dd1a9d2SSebastian Grimberg }
18554dd1a9d2SSebastian Grimberg 
1856480fae85SJeremy L Thompson /// @}
1857480fae85SJeremy L Thompson 
1858480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1859eaf62fffSJeremy L Thompson /// CeedOperator Public API
1860eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1861eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser
1862eaf62fffSJeremy L Thompson /// @{
1863eaf62fffSJeremy L Thompson 
1864eaf62fffSJeremy L Thompson /**
1865ca94c3ddSJeremy L Thompson   @brief Assemble a linear `CeedQFunction` associated with a `CeedOperator`.
1866eaf62fffSJeremy L Thompson 
1867ca94c3ddSJeremy L Thompson   This returns a `CeedVector` containing a matrix at each quadrature point providing the action of the `CeedQFunction` associated with the `CeedOperator`.
1868ca94c3ddSJeremy 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.
1869859c15bbSJames Wright 
1870ca94c3ddSJeremy L Thompson   Inputs and outputs are in the order provided by the user when adding `CeedOperator` fields.
1871ca94c3ddSJeremy 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]`.
1872eaf62fffSJeremy L Thompson 
1873ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1874f04ea552SJeremy L Thompson 
1875ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1876ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points
1877ca94c3ddSJeremy L Thompson   @param[out] rstr      `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1878ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1879eaf62fffSJeremy L Thompson 
1880eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1881eaf62fffSJeremy L Thompson 
1882eaf62fffSJeremy L Thompson   @ref User
1883eaf62fffSJeremy L Thompson **/
18842b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
18852b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1886eaf62fffSJeremy L Thompson 
1887eaf62fffSJeremy L Thompson   if (op->LinearAssembleQFunction) {
1888d04bbc78SJeremy L Thompson     // Backend version
18892b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request));
1890eaf62fffSJeremy L Thompson   } else {
1891d04bbc78SJeremy L Thompson     // Operator fallback
1892d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1893d04bbc78SJeremy L Thompson 
18942b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
18956574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request));
18969bc66399SJeremy L Thompson     else return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction");
189770a7ffb3SJeremy L Thompson   }
1898eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1899eaf62fffSJeremy L Thompson }
190070a7ffb3SJeremy L Thompson 
190170a7ffb3SJeremy L Thompson /**
1902ca94c3ddSJeremy L Thompson   @brief Assemble `CeedQFunction` and store result internally.
19034385fb7fSSebastian Grimberg 
1904ea61e9acSJeremy L Thompson   Return copied references of stored data to the caller.
1905ea61e9acSJeremy L Thompson   Caller is responsible for ownership and destruction of the copied references.
1906ca94c3ddSJeremy L Thompson   See also @ref CeedOperatorLinearAssembleQFunction().
190770a7ffb3SJeremy L Thompson 
1908ca94c3ddSJeremy 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.
1909c5f45aeaSJeremy L Thompson         These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object.
1910c5f45aeaSJeremy L Thompson 
1911ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1912ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points
1913ca94c3ddSJeremy L Thompson   @param[out] rstr      `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1914ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
191570a7ffb3SJeremy L Thompson 
191670a7ffb3SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
191770a7ffb3SJeremy L Thompson 
191870a7ffb3SJeremy L Thompson   @ref User
191970a7ffb3SJeremy L Thompson **/
19202b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
1921b05f7e9fSJeremy L Thompson   int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *) = NULL;
1922b05f7e9fSJeremy L Thompson   CeedOperator op_assemble                                                                           = NULL;
1923bb229da9SJeremy L Thompson   CeedOperator op_fallback_parent                                                                    = NULL;
1924b05f7e9fSJeremy L Thompson 
19252b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
192670a7ffb3SJeremy L Thompson 
1927b05f7e9fSJeremy L Thompson   // Determine if fallback parent or operator has implementation
1928bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParent(op, &op_fallback_parent));
1929bb229da9SJeremy L Thompson   if (op_fallback_parent && op_fallback_parent->LinearAssembleQFunctionUpdate) {
1930b05f7e9fSJeremy L Thompson     // -- Backend version for op fallback parent is faster, if it exists
1931bb229da9SJeremy L Thompson     LinearAssembleQFunctionUpdate = op_fallback_parent->LinearAssembleQFunctionUpdate;
1932bb229da9SJeremy L Thompson     op_assemble                   = op_fallback_parent;
1933b05f7e9fSJeremy L Thompson   } else if (op->LinearAssembleQFunctionUpdate) {
1934b05f7e9fSJeremy L Thompson     // -- Backend version for op
1935b05f7e9fSJeremy L Thompson     LinearAssembleQFunctionUpdate = op->LinearAssembleQFunctionUpdate;
1936b05f7e9fSJeremy L Thompson     op_assemble                   = op;
1937b05f7e9fSJeremy L Thompson   }
1938b05f7e9fSJeremy L Thompson 
1939b05f7e9fSJeremy L Thompson   // Assemble QFunction
1940b05f7e9fSJeremy L Thompson   if (LinearAssembleQFunctionUpdate) {
1941b05f7e9fSJeremy L Thompson     // Backend or fallback parent version
19427d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
19437d5185d7SSebastian Grimberg     bool                      data_is_setup;
19442efa2d85SJeremy L Thompson     CeedVector                assembled_vec  = NULL;
19452efa2d85SJeremy L Thompson     CeedElemRestriction       assembled_rstr = NULL;
1946480fae85SJeremy L Thompson 
19477d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
19487d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataIsSetup(data, &data_is_setup));
19497d5185d7SSebastian Grimberg     if (data_is_setup) {
1950d04bbc78SJeremy L Thompson       bool update_needed;
1951d04bbc78SJeremy L Thompson 
19527d5185d7SSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataGetObjects(data, &assembled_vec, &assembled_rstr));
19537d5185d7SSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(data, &update_needed));
1954b05f7e9fSJeremy L Thompson       if (update_needed) CeedCall(LinearAssembleQFunctionUpdate(op_assemble, assembled_vec, assembled_rstr, request));
195570a7ffb3SJeremy L Thompson     } else {
1956b05f7e9fSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunction(op_assemble, &assembled_vec, &assembled_rstr, request));
19577d5185d7SSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataSetObjects(data, assembled_vec, assembled_rstr));
195870a7ffb3SJeremy L Thompson     }
19597d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(data, false));
19602efa2d85SJeremy L Thompson 
1961d04bbc78SJeremy L Thompson     // Copy reference from internally held copy
19622b730f8bSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled));
19632b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr));
1964c5f45aeaSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled_vec));
19652b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&assembled_rstr));
196670a7ffb3SJeremy L Thompson   } else {
1967d04bbc78SJeremy L Thompson     // Operator fallback
1968d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1969d04bbc78SJeremy L Thompson 
19702b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
19716574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request));
19729bc66399SJeremy L Thompson     else return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate");
197370a7ffb3SJeremy L Thompson   }
197470a7ffb3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1975eaf62fffSJeremy L Thompson }
1976eaf62fffSJeremy L Thompson 
1977eaf62fffSJeremy L Thompson /**
1978ca94c3ddSJeremy L Thompson   @brief Assemble the diagonal of a square linear `CeedOperator`
1979eaf62fffSJeremy L Thompson 
1980ca94c3ddSJeremy L Thompson   This overwrites a `CeedVector` with the diagonal of a linear `CeedOperator`.
1981eaf62fffSJeremy L Thompson 
1982ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
1983eaf62fffSJeremy L Thompson 
1984ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1985f04ea552SJeremy L Thompson 
1986ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1987ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal
1988ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1989eaf62fffSJeremy L Thompson 
1990eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1991eaf62fffSJeremy L Thompson 
1992eaf62fffSJeremy L Thompson   @ref User
1993eaf62fffSJeremy L Thompson **/
19942b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1995f3d47e36SJeremy L Thompson   bool     is_composite;
19961c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
19971c66c397SJeremy L Thompson 
19982b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1999f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2000eaf62fffSJeremy L Thompson 
20012b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
20029bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
2003c9366a6bSJeremy L Thompson 
2004f3d47e36SJeremy L Thompson   // Early exit for empty operator
2005f3d47e36SJeremy L Thompson   if (!is_composite) {
2006f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2007f3d47e36SJeremy L Thompson 
2008f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2009f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2010f3d47e36SJeremy L Thompson   }
2011f3d47e36SJeremy L Thompson 
2012eaf62fffSJeremy L Thompson   if (op->LinearAssembleDiagonal) {
2013d04bbc78SJeremy L Thompson     // Backend version
20142b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleDiagonal(op, assembled, request));
2015eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2016eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddDiagonal) {
2017d04bbc78SJeremy L Thompson     // Backend version with zeroing first
20182b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
20192b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
2020eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2021eaf62fffSJeremy L Thompson   } else {
2022d04bbc78SJeremy L Thompson     // Operator fallback
2023d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2024d04bbc78SJeremy L Thompson 
20252b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2026d04bbc78SJeremy L Thompson     if (op_fallback) {
20272b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request));
2028eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2029eaf62fffSJeremy L Thompson     }
2030eaf62fffSJeremy L Thompson   }
2031eaf62fffSJeremy L Thompson   // Default interface implementation
20322b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
20332b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request));
2034eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2035eaf62fffSJeremy L Thompson }
2036eaf62fffSJeremy L Thompson 
2037eaf62fffSJeremy L Thompson /**
2038ca94c3ddSJeremy L Thompson   @brief Assemble the diagonal of a square linear `CeedOperator`.
2039eaf62fffSJeremy L Thompson 
2040ca94c3ddSJeremy L Thompson   This sums into a `CeedVector` the diagonal of a linear `CeedOperator`.
2041eaf62fffSJeremy L Thompson 
2042ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2043eaf62fffSJeremy L Thompson 
2044ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2045f04ea552SJeremy L Thompson 
2046ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2047ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal
2048ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2049eaf62fffSJeremy L Thompson 
2050eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2051eaf62fffSJeremy L Thompson 
2052eaf62fffSJeremy L Thompson   @ref User
2053eaf62fffSJeremy L Thompson **/
20542b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2055f3d47e36SJeremy L Thompson   bool     is_composite;
20561c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
20571c66c397SJeremy L Thompson 
20582b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2059f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2060eaf62fffSJeremy L Thompson 
20612b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
20629bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
2063c9366a6bSJeremy L Thompson 
2064f3d47e36SJeremy L Thompson   // Early exit for empty operator
2065f3d47e36SJeremy L Thompson   if (!is_composite) {
2066f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2067f3d47e36SJeremy L Thompson 
2068f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2069f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2070f3d47e36SJeremy L Thompson   }
2071f3d47e36SJeremy L Thompson 
2072eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddDiagonal) {
2073d04bbc78SJeremy L Thompson     // Backend version
20742b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
2075eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2076eaf62fffSJeremy L Thompson   } else {
2077d04bbc78SJeremy L Thompson     // Operator fallback
2078d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2079d04bbc78SJeremy L Thompson 
20802b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2081d04bbc78SJeremy L Thompson     if (op_fallback) {
20822b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
2083eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2084eaf62fffSJeremy L Thompson     }
2085eaf62fffSJeremy L Thompson   }
2086eaf62fffSJeremy L Thompson   // Default interface implementation
2087eaf62fffSJeremy L Thompson   if (is_composite) {
20882b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
2089eaf62fffSJeremy L Thompson   } else {
2090f3bd9308SJeremy L Thompson     CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
2091eaf62fffSJeremy L Thompson   }
2092d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2093eaf62fffSJeremy L Thompson }
2094eaf62fffSJeremy L Thompson 
2095eaf62fffSJeremy L Thompson /**
2096ca94c3ddSJeremy L Thompson    @brief Fully assemble the point-block diagonal pattern of a linear `CeedOperator`.
209701f0e615SJames Wright 
2098ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssemblePointBlockDiagonal().
209901f0e615SJames Wright 
2100ca94c3ddSJeremy 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)`.
2101ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are unique.
2102ca94c3ddSJeremy L Thompson    This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemblePointBlockDiagonal() provides the values in the same ordering.
210301f0e615SJames Wright 
210401f0e615SJames Wright    This will generally be slow unless your operator is low-order.
210501f0e615SJames Wright 
2106ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
210701f0e615SJames Wright 
2108ca94c3ddSJeremy L Thompson    @param[in]  op          `CeedOperator` to assemble
210901f0e615SJames Wright    @param[out] num_entries Number of entries in coordinate nonzero pattern
211001f0e615SJames Wright    @param[out] rows        Row number for each entry
211101f0e615SJames Wright    @param[out] cols        Column number for each entry
211201f0e615SJames Wright 
211301f0e615SJames Wright    @ref User
211401f0e615SJames Wright **/
211501f0e615SJames Wright int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
211601f0e615SJames Wright   bool          is_composite;
211701f0e615SJames Wright   CeedInt       num_active_components, num_sub_operators;
211801f0e615SJames Wright   CeedOperator *sub_operators;
211901f0e615SJames Wright 
212001f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
212101f0e615SJames Wright 
212201f0e615SJames Wright   CeedSize input_size = 0, output_size = 0;
212301f0e615SJames Wright   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
21249bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
212501f0e615SJames Wright 
212601f0e615SJames Wright   if (is_composite) {
212701f0e615SJames Wright     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_operators));
212801f0e615SJames Wright     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
212901f0e615SJames Wright   } else {
213001f0e615SJames Wright     sub_operators     = &op;
213101f0e615SJames Wright     num_sub_operators = 1;
213201f0e615SJames Wright   }
213301f0e615SJames Wright 
2134506b1a0cSSebastian Grimberg   // Verify operator can be assembled correctly
2135506b1a0cSSebastian Grimberg   {
213601f0e615SJames Wright     CeedOperatorAssemblyData data;
2137506b1a0cSSebastian Grimberg     CeedInt                  num_active_elem_rstrs, comp_stride;
213801f0e615SJames Wright     CeedElemRestriction     *active_elem_rstrs;
213901f0e615SJames Wright 
214001f0e615SJames Wright     // Get initial values to check against
214101f0e615SJames Wright     CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[0], &data));
2142506b1a0cSSebastian Grimberg     CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL));
214301f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[0], &comp_stride));
214401f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[0], &num_active_components));
214501f0e615SJames Wright 
2146506b1a0cSSebastian Grimberg     // Verify that all active element restrictions have same component stride and number of components
214701f0e615SJames Wright     for (CeedInt k = 0; k < num_sub_operators; k++) {
214801f0e615SJames Wright       CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[k], &data));
2149506b1a0cSSebastian Grimberg       CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL));
215001f0e615SJames Wright       for (CeedInt i = 0; i < num_active_elem_rstrs; i++) {
2151506b1a0cSSebastian Grimberg         CeedInt comp_stride_sub, num_active_components_sub;
2152506b1a0cSSebastian Grimberg 
215301f0e615SJames Wright         CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[i], &comp_stride_sub));
21549bc66399SJeremy L Thompson         CeedCheck(comp_stride == comp_stride_sub, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION,
215501f0e615SJames Wright                   "Active element restrictions must have the same component stride: %d vs %d", comp_stride, comp_stride_sub);
215601f0e615SJames Wright         CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[i], &num_active_components_sub));
21579bc66399SJeremy L Thompson         CeedCheck(num_active_components == num_active_components_sub, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
21583f08121cSJeremy L Thompson                   "All suboperators must have the same number of output components."
21593f08121cSJeremy L Thompson                   " Previous: %" CeedInt_FMT " Current: %" CeedInt_FMT,
21603f08121cSJeremy L Thompson                   num_active_components, num_active_components_sub);
216101f0e615SJames Wright       }
216201f0e615SJames Wright     }
216301f0e615SJames Wright   }
216401f0e615SJames Wright   *num_entries = input_size * num_active_components;
216501f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, rows));
216601f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, cols));
216701f0e615SJames Wright 
216801f0e615SJames Wright   for (CeedInt o = 0; o < num_sub_operators; o++) {
2169506b1a0cSSebastian Grimberg     CeedElemRestriction active_elem_rstr, point_block_active_elem_rstr;
217001f0e615SJames Wright     CeedInt             comp_stride, num_elem, elem_size;
2171506b1a0cSSebastian Grimberg     const CeedInt      *offsets, *point_block_offsets;
217201f0e615SJames Wright 
217301f0e615SJames Wright     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[o], &active_elem_rstr));
217401f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstr, &comp_stride));
217501f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumElements(active_elem_rstr, &num_elem));
217601f0e615SJames Wright     CeedCall(CeedElemRestrictionGetElementSize(active_elem_rstr, &elem_size));
217701f0e615SJames Wright     CeedCall(CeedElemRestrictionGetOffsets(active_elem_rstr, CEED_MEM_HOST, &offsets));
217801f0e615SJames Wright 
2179506b1a0cSSebastian Grimberg     CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstr, &point_block_active_elem_rstr));
2180506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOffsets(point_block_active_elem_rstr, CEED_MEM_HOST, &point_block_offsets));
218101f0e615SJames Wright 
218201f0e615SJames Wright     for (CeedSize i = 0; i < num_elem * elem_size; i++) {
218301f0e615SJames Wright       for (CeedInt c_out = 0; c_out < num_active_components; c_out++) {
218401f0e615SJames Wright         for (CeedInt c_in = 0; c_in < num_active_components; c_in++) {
2185506b1a0cSSebastian Grimberg           (*rows)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_out * comp_stride;
2186506b1a0cSSebastian Grimberg           (*cols)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_in * comp_stride;
218701f0e615SJames Wright         }
218801f0e615SJames Wright       }
218901f0e615SJames Wright     }
219001f0e615SJames Wright 
219101f0e615SJames Wright     CeedCall(CeedElemRestrictionRestoreOffsets(active_elem_rstr, &offsets));
2192506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOffsets(point_block_active_elem_rstr, &point_block_offsets));
2193681d0ea7SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&active_elem_rstr));
2194506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&point_block_active_elem_rstr));
219501f0e615SJames Wright   }
219601f0e615SJames Wright   return CEED_ERROR_SUCCESS;
219701f0e615SJames Wright }
219801f0e615SJames Wright 
219901f0e615SJames Wright /**
2200ca94c3ddSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear `CeedOperator`.
2201eaf62fffSJeremy L Thompson 
2202ca94c3ddSJeremy L Thompson   This overwrites a `CeedVector` with the point block diagonal of a linear `CeedOperator`.
2203eaf62fffSJeremy L Thompson 
2204ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2205eaf62fffSJeremy L Thompson 
2206ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2207f04ea552SJeremy L Thompson 
2208ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2209ca94c3ddSJeremy 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.
2210ca94c3ddSJeremy L Thompson                           The dimensions of this vector are derived from the active vector for the `CeedOperator`.
2211ca94c3ddSJeremy L Thompson                           The array has shape `[nodes, component out, component in]`.
2212ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2213eaf62fffSJeremy L Thompson 
2214eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2215eaf62fffSJeremy L Thompson 
2216eaf62fffSJeremy L Thompson   @ref User
2217eaf62fffSJeremy L Thompson **/
22182b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2219f3d47e36SJeremy L Thompson   bool     is_composite;
22201c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
22211c66c397SJeremy L Thompson 
22222b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2223f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2224eaf62fffSJeremy L Thompson 
22252b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
22269bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
2227c9366a6bSJeremy L Thompson 
2228f3d47e36SJeremy L Thompson   // Early exit for empty operator
2229f3d47e36SJeremy L Thompson   if (!is_composite) {
2230f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2231f3d47e36SJeremy L Thompson 
2232f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2233f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2234f3d47e36SJeremy L Thompson   }
2235f3d47e36SJeremy L Thompson 
2236eaf62fffSJeremy L Thompson   if (op->LinearAssemblePointBlockDiagonal) {
2237d04bbc78SJeremy L Thompson     // Backend version
22382b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request));
2239eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2240eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddPointBlockDiagonal) {
2241d04bbc78SJeremy L Thompson     // Backend version with zeroing first
22422b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
22432b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
2244eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2245eaf62fffSJeremy L Thompson   } else {
2246d04bbc78SJeremy L Thompson     // Operator fallback
2247d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2248d04bbc78SJeremy L Thompson 
22492b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2250d04bbc78SJeremy L Thompson     if (op_fallback) {
22512b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request));
2252eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2253eaf62fffSJeremy L Thompson     }
2254eaf62fffSJeremy L Thompson   }
2255eaf62fffSJeremy L Thompson   // Default interface implementation
22562b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
22572b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
2258eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2259eaf62fffSJeremy L Thompson }
2260eaf62fffSJeremy L Thompson 
2261eaf62fffSJeremy L Thompson /**
2262ca94c3ddSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear `CeedOperator`.
2263eaf62fffSJeremy L Thompson 
2264ca94c3ddSJeremy L Thompson   This sums into a `CeedVector` with the point block diagonal of a linear `CeedOperator`.
2265eaf62fffSJeremy L Thompson 
2266ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2267eaf62fffSJeremy L Thompson 
2268ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2269f04ea552SJeremy L Thompson 
2270ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2271ca94c3ddSJeremy 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.
2272ca94c3ddSJeremy L Thompson                           The dimensions of this vector are derived from the active vector for the `CeedOperator`.
2273ca94c3ddSJeremy L Thompson                           The array has shape `[nodes, component out, component in]`.
2274ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2275eaf62fffSJeremy L Thompson 
2276eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2277eaf62fffSJeremy L Thompson 
2278eaf62fffSJeremy L Thompson   @ref User
2279eaf62fffSJeremy L Thompson **/
22802b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2281f3d47e36SJeremy L Thompson   bool     is_composite;
22821c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
22831c66c397SJeremy L Thompson 
22842b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2285f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2286eaf62fffSJeremy L Thompson 
22872b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
22889bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
2289c9366a6bSJeremy L Thompson 
2290f3d47e36SJeremy L Thompson   // Early exit for empty operator
2291f3d47e36SJeremy L Thompson   if (!is_composite) {
2292f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2293f3d47e36SJeremy L Thompson 
2294f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2295f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2296f3d47e36SJeremy L Thompson   }
2297f3d47e36SJeremy L Thompson 
2298eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddPointBlockDiagonal) {
2299d04bbc78SJeremy L Thompson     // Backend version
23002b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request));
2301eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2302eaf62fffSJeremy L Thompson   } else {
2303d04bbc78SJeremy L Thompson     // Operator fallback
2304d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2305d04bbc78SJeremy L Thompson 
23062b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2307d04bbc78SJeremy L Thompson     if (op_fallback) {
23082b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request));
2309eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2310eaf62fffSJeremy L Thompson     }
2311eaf62fffSJeremy L Thompson   }
2312ea61e9acSJeremy L Thompson   // Default interface implementation
2313eaf62fffSJeremy L Thompson   if (is_composite) {
23142b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
2315eaf62fffSJeremy L Thompson   } else {
2316f3bd9308SJeremy L Thompson     CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
2317eaf62fffSJeremy L Thompson   }
2318d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2319eaf62fffSJeremy L Thompson }
2320eaf62fffSJeremy L Thompson 
2321eaf62fffSJeremy L Thompson /**
2322ca94c3ddSJeremy L Thompson    @brief Fully assemble the nonzero pattern of a linear `CeedOperator`.
2323eaf62fffSJeremy L Thompson 
2324ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssemble().
2325eaf62fffSJeremy L Thompson 
2326ca94c3ddSJeremy 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)`.
2327ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are not unique and may repeat.
2328ca94c3ddSJeremy L Thompson    This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemble() provides the values in the same ordering.
2329eaf62fffSJeremy L Thompson 
2330eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2331eaf62fffSJeremy L Thompson 
2332ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2333f04ea552SJeremy L Thompson 
2334ca94c3ddSJeremy L Thompson    @param[in]  op          `CeedOperator` to assemble
2335eaf62fffSJeremy L Thompson    @param[out] num_entries Number of entries in coordinate nonzero pattern
2336eaf62fffSJeremy L Thompson    @param[out] rows        Row number for each entry
2337eaf62fffSJeremy L Thompson    @param[out] cols        Column number for each entry
2338eaf62fffSJeremy L Thompson 
2339eaf62fffSJeremy L Thompson    @ref User
2340eaf62fffSJeremy L Thompson **/
23412b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
23421c66c397SJeremy L Thompson   bool          is_composite;
23431c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2344b94338b9SJed Brown   CeedSize      single_entries;
2345eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
23461c66c397SJeremy L Thompson 
23472b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2348f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2349eaf62fffSJeremy L Thompson 
2350eaf62fffSJeremy L Thompson   if (op->LinearAssembleSymbolic) {
2351d04bbc78SJeremy L Thompson     // Backend version
23522b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols));
2353eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2354eaf62fffSJeremy L Thompson   } else {
2355d04bbc78SJeremy L Thompson     // Operator fallback
2356d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2357d04bbc78SJeremy L Thompson 
23582b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2359d04bbc78SJeremy L Thompson     if (op_fallback) {
23602b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols));
2361eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2362eaf62fffSJeremy L Thompson     }
2363eaf62fffSJeremy L Thompson   }
2364eaf62fffSJeremy L Thompson 
2365eaf62fffSJeremy L Thompson   // Default interface implementation
2366eaf62fffSJeremy L Thompson 
2367506b1a0cSSebastian Grimberg   // Count entries and allocate rows, cols arrays
2368eaf62fffSJeremy L Thompson   *num_entries = 0;
2369eaf62fffSJeremy L Thompson   if (is_composite) {
2370c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2371c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
237292ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
23732b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2374eaf62fffSJeremy L Thompson       *num_entries += single_entries;
2375eaf62fffSJeremy L Thompson     }
2376eaf62fffSJeremy L Thompson   } else {
23772b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries));
2378eaf62fffSJeremy L Thompson     *num_entries += single_entries;
2379eaf62fffSJeremy L Thompson   }
23802b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, rows));
23812b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, cols));
2382eaf62fffSJeremy L Thompson 
2383506b1a0cSSebastian Grimberg   // Assemble nonzero locations
2384eaf62fffSJeremy L Thompson   if (is_composite) {
2385c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2386c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
238792ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
23882b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols));
23892b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2390eaf62fffSJeremy L Thompson       offset += single_entries;
2391eaf62fffSJeremy L Thompson     }
2392eaf62fffSJeremy L Thompson   } else {
23932b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols));
2394eaf62fffSJeremy L Thompson   }
2395eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2396eaf62fffSJeremy L Thompson }
2397eaf62fffSJeremy L Thompson 
2398eaf62fffSJeremy L Thompson /**
2399eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero entries of a linear operator.
2400eaf62fffSJeremy L Thompson 
2401ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssembleSymbolic().
2402eaf62fffSJeremy L Thompson 
2403ca94c3ddSJeremy 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)`.
2404ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are not unique and may repeat.
2405ca94c3ddSJeremy L Thompson    This function returns the values of the nonzero entries to be added, their `(i, j)` locations are provided by @ref CeedOperatorLinearAssembleSymbolic().
2406eaf62fffSJeremy L Thompson 
2407eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2408eaf62fffSJeremy L Thompson 
2409ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2410f04ea552SJeremy L Thompson 
2411ca94c3ddSJeremy L Thompson    @param[in]  op     `CeedOperator` to assemble
2412eaf62fffSJeremy L Thompson    @param[out] values Values to assemble into matrix
2413eaf62fffSJeremy L Thompson 
2414eaf62fffSJeremy L Thompson    @ref User
2415eaf62fffSJeremy L Thompson **/
2416eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) {
24171c66c397SJeremy L Thompson   bool          is_composite;
24181c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2419b94338b9SJed Brown   CeedSize      single_entries = 0;
2420eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
24211c66c397SJeremy L Thompson 
24222b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2423f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2424f3d47e36SJeremy L Thompson 
2425f3d47e36SJeremy L Thompson   // Early exit for empty operator
2426f3d47e36SJeremy L Thompson   if (!is_composite) {
2427f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2428f3d47e36SJeremy L Thompson 
2429f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2430f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2431f3d47e36SJeremy L Thompson   }
2432eaf62fffSJeremy L Thompson 
2433eaf62fffSJeremy L Thompson   if (op->LinearAssemble) {
2434d04bbc78SJeremy L Thompson     // Backend version
24352b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemble(op, values));
2436eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2437eaf62fffSJeremy L Thompson   } else {
2438d04bbc78SJeremy L Thompson     // Operator fallback
2439d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2440d04bbc78SJeremy L Thompson 
24412b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2442d04bbc78SJeremy L Thompson     if (op_fallback) {
24432b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemble(op_fallback, values));
2444eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2445eaf62fffSJeremy L Thompson     }
2446eaf62fffSJeremy L Thompson   }
2447eaf62fffSJeremy L Thompson 
2448eaf62fffSJeremy L Thompson   // Default interface implementation
244928ec399dSJeremy L Thompson   CeedCall(CeedVectorSetValue(values, 0.0));
2450eaf62fffSJeremy L Thompson   if (is_composite) {
2451c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2452c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2453cefa2673SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; k++) {
24542b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values));
24552b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2456eaf62fffSJeremy L Thompson       offset += single_entries;
2457eaf62fffSJeremy L Thompson     }
2458eaf62fffSJeremy L Thompson   } else {
24592b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemble(op, offset, values));
2460eaf62fffSJeremy L Thompson   }
2461eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2462eaf62fffSJeremy L Thompson }
2463eaf62fffSJeremy L Thompson 
2464eaf62fffSJeremy L Thompson /**
2465ca94c3ddSJeremy L Thompson   @brief Get the multiplicity of nodes across sub-operators in a composite `CeedOperator`.
246675f0d5a4SJeremy L Thompson 
2467ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
246875f0d5a4SJeremy L Thompson 
2469ca94c3ddSJeremy L Thompson   @param[in]  op               Composite `CeedOperator`
2470ca94c3ddSJeremy L Thompson   @param[in]  num_skip_indices Number of sub-operators to skip
2471ca94c3ddSJeremy L Thompson   @param[in]  skip_indices     Array of indices of sub-operators to skip
2472ca94c3ddSJeremy L Thompson   @param[out] mult             Vector to store multiplicity (of size `l_size` )
247375f0d5a4SJeremy L Thompson 
247475f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
247575f0d5a4SJeremy L Thompson 
247675f0d5a4SJeremy L Thompson   @ref User
247775f0d5a4SJeremy L Thompson **/
247875f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) {
247975f0d5a4SJeremy L Thompson   Ceed                ceed;
2480b275c451SJeremy L Thompson   CeedInt             num_suboperators;
248175f0d5a4SJeremy L Thompson   CeedSize            l_vec_len;
248275f0d5a4SJeremy L Thompson   CeedScalar         *mult_array;
248375f0d5a4SJeremy L Thompson   CeedVector          ones_l_vec;
24847c1dbaffSSebastian Grimberg   CeedElemRestriction elem_rstr, mult_elem_rstr;
2485b275c451SJeremy L Thompson   CeedOperator       *sub_operators;
248675f0d5a4SJeremy L Thompson 
24871c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
24881c66c397SJeremy L Thompson 
248975f0d5a4SJeremy L Thompson   // Zero mult vector
249075f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
249175f0d5a4SJeremy L Thompson 
249275f0d5a4SJeremy L Thompson   // Get suboperators
2493b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2494b275c451SJeremy L Thompson   if (num_suboperators == 0) return CEED_ERROR_SUCCESS;
24959bc66399SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
249675f0d5a4SJeremy L Thompson 
249775f0d5a4SJeremy L Thompson   // Work vector
249875f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetLength(mult, &l_vec_len));
24999bc66399SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
250075f0d5a4SJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec));
25019bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
250275f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(ones_l_vec, 1.0));
250375f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array));
250475f0d5a4SJeremy L Thompson 
250575f0d5a4SJeremy L Thompson   // Compute multiplicity across suboperators
2506b275c451SJeremy L Thompson   for (CeedInt i = 0; i < num_suboperators; i++) {
250775f0d5a4SJeremy L Thompson     const CeedScalar *sub_mult_array;
250875f0d5a4SJeremy L Thompson     CeedVector        sub_mult_l_vec, ones_e_vec;
250975f0d5a4SJeremy L Thompson 
251075f0d5a4SJeremy L Thompson     // -- Check for suboperator to skip
251175f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < num_skip_indices; j++) {
251275f0d5a4SJeremy L Thompson       if (skip_indices[j] == i) continue;
251375f0d5a4SJeremy L Thompson     }
251475f0d5a4SJeremy L Thompson 
251575f0d5a4SJeremy L Thompson     // -- Sub operator multiplicity
2516437c7c90SJeremy L Thompson     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr));
25177c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr, &mult_elem_rstr));
2518681d0ea7SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&elem_rstr));
25197c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateVector(mult_elem_rstr, &sub_mult_l_vec, &ones_e_vec));
252075f0d5a4SJeremy L Thompson     CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0));
25217c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE));
25227c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE));
252375f0d5a4SJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array));
252475f0d5a4SJeremy L Thompson     // ---- Flag every node present in the current suboperator
2525c81f2b9dSJames Wright     for (CeedSize j = 0; j < l_vec_len; j++) {
252675f0d5a4SJeremy L Thompson       if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0;
252775f0d5a4SJeremy L Thompson     }
252875f0d5a4SJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array));
252975f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&sub_mult_l_vec));
253075f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&ones_e_vec));
25317c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&mult_elem_rstr));
253275f0d5a4SJeremy L Thompson   }
253375f0d5a4SJeremy L Thompson   CeedCall(CeedVectorRestoreArray(mult, &mult_array));
2534811d0ccfSJeremy L Thompson   CeedCall(CeedVectorDestroy(&ones_l_vec));
253575f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
253675f0d5a4SJeremy L Thompson }
253775f0d5a4SJeremy L Thompson 
253875f0d5a4SJeremy L Thompson /**
2539ca94c3ddSJeremy 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.
2540eaf62fffSJeremy L Thompson 
2541ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2542f04ea552SJeremy L Thompson 
2543ca94c3ddSJeremy L Thompson   @param[in]  op_fine      Fine grid `CeedOperator`
2544ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2545ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid `CeedElemRestriction`
2546ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector `CeedBasis`
2547ca94c3ddSJeremy L Thompson   @param[out] op_coarse    Coarse grid `CeedOperator`
2548ca94c3ddSJeremy L Thompson   @param[out] op_prolong   Coarse to fine `CeedOperator`, or `NULL`
2549ca94c3ddSJeremy L Thompson   @param[out] op_restrict  Fine to coarse `CeedOperator`, or `NULL`
2550eaf62fffSJeremy L Thompson 
2551eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2552eaf62fffSJeremy L Thompson 
2553eaf62fffSJeremy L Thompson   @ref User
2554eaf62fffSJeremy L Thompson **/
25552b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
25567758292fSSebastian Grimberg                                      CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
25571c66c397SJeremy L Thompson   CeedBasis basis_c_to_f = NULL;
25581c66c397SJeremy L Thompson 
25592b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2560eaf62fffSJeremy L Thompson 
256183d6adf3SZach Atkins   // Build prolongation matrix, if required
25627758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
256383d6adf3SZach Atkins     CeedBasis basis_fine;
25641c66c397SJeremy L Thompson 
25652b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
25662b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f));
2567681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis_fine));
256883d6adf3SZach Atkins   }
2569eaf62fffSJeremy L Thompson 
2570f113e5dcSJeremy L Thompson   // Core code
25717758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2572eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2573eaf62fffSJeremy L Thompson }
2574eaf62fffSJeremy L Thompson 
2575eaf62fffSJeremy L Thompson /**
2576ca94c3ddSJeremy L Thompson   @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a tensor basis for the active basis.
2577eaf62fffSJeremy L Thompson 
2578ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2579f04ea552SJeremy L Thompson 
2580ca94c3ddSJeremy L Thompson   @param[in]  op_fine       Fine grid `CeedOperator`
2581ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2582ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid `CeedElemRestriction`
2583ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector `CeedBasis`
2584ca94c3ddSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator`
2585ca94c3ddSJeremy L Thompson   @param[out] op_coarse     Coarse grid `CeedOperator`
2586ca94c3ddSJeremy L Thompson   @param[out] op_prolong    Coarse to fine `CeedOperator`, or `NULL`
2587ca94c3ddSJeremy L Thompson   @param[out] op_restrict   Fine to coarse `CeedOperator`, or `NULL`
2588eaf62fffSJeremy L Thompson 
2589eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2590eaf62fffSJeremy L Thompson 
2591eaf62fffSJeremy L Thompson   @ref User
2592eaf62fffSJeremy L Thompson **/
25932b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
25942b730f8bSJeremy L Thompson                                              const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
25957758292fSSebastian Grimberg                                              CeedOperator *op_restrict) {
2596eaf62fffSJeremy L Thompson   Ceed      ceed;
25971c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
25981c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
25991c66c397SJeremy L Thompson 
26001c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
26012b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2602eaf62fffSJeremy L Thompson 
2603eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
26042b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
26052b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
26062b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
26073f08121cSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION,
26083f08121cSJeremy L Thompson             "Bases must have compatible quadrature spaces."
26093f08121cSJeremy L Thompson             " Fine grid: %" CeedInt_FMT " points, Coarse grid: %" CeedInt_FMT " points",
26103f08121cSJeremy L Thompson             Q_f, Q_c);
2611eaf62fffSJeremy L Thompson 
261283d6adf3SZach Atkins   // Create coarse to fine basis, if required
26137758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
26141c66c397SJeremy L Thompson     CeedInt     dim, num_comp, num_nodes_c, P_1d_f, P_1d_c;
26151c66c397SJeremy L Thompson     CeedScalar *q_ref, *q_weight, *grad;
26161c66c397SJeremy L Thompson 
261783d6adf3SZach Atkins     // Check if interpolation matrix is provided
26186574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
26196574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
26202b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
26212b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
26222b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f));
2623681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis_fine));
26242b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
26252b730f8bSJeremy L Thompson     P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c);
26262b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_ref));
26272b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_weight));
26282b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad));
26292b730f8bSJeremy 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));
26302b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
26312b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
26322b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
263383d6adf3SZach Atkins   }
2634eaf62fffSJeremy L Thompson 
2635eaf62fffSJeremy L Thompson   // Core code
26367758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
26379bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
2638eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2639eaf62fffSJeremy L Thompson }
2640eaf62fffSJeremy L Thompson 
2641eaf62fffSJeremy L Thompson /**
2642ca94c3ddSJeremy L Thompson   @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a non-tensor basis for the active vector
2643eaf62fffSJeremy L Thompson 
2644ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2645f04ea552SJeremy L Thompson 
2646ca94c3ddSJeremy L Thompson   @param[in]  op_fine       Fine grid `CeedOperator`
2647ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2648ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid `CeedElemRestriction`
2649ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector `CeedBasis`
2650ca94c3ddSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator`
2651ca94c3ddSJeremy L Thompson   @param[out] op_coarse     Coarse grid `CeedOperator`
2652ca94c3ddSJeremy L Thompson   @param[out] op_prolong    Coarse to fine `CeedOperator`, or `NULL`
2653ca94c3ddSJeremy L Thompson   @param[out] op_restrict   Fine to coarse `CeedOperator`, or `NULL`
2654eaf62fffSJeremy L Thompson 
2655eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2656eaf62fffSJeremy L Thompson 
2657eaf62fffSJeremy L Thompson   @ref User
2658eaf62fffSJeremy L Thompson **/
26592b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
26607758292fSSebastian Grimberg                                        const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
26617758292fSSebastian Grimberg                                        CeedOperator *op_restrict) {
2662eaf62fffSJeremy L Thompson   Ceed      ceed;
26631c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
26641c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
26651c66c397SJeremy L Thompson 
26661c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
26672b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2668eaf62fffSJeremy L Thompson 
2669eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
26702b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
26712b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
26722b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
26736574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2674eaf62fffSJeremy L Thompson 
2675eaf62fffSJeremy L Thompson   // Coarse to fine basis
26767758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
26771c66c397SJeremy L Thompson     CeedInt          dim, num_comp, num_nodes_c, num_nodes_f;
26781c66c397SJeremy L Thompson     CeedScalar      *q_ref, *q_weight, *grad;
26791c66c397SJeremy L Thompson     CeedElemTopology topo;
26801c66c397SJeremy L Thompson 
268183d6adf3SZach Atkins     // Check if interpolation matrix is provided
26826574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
26836574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
26842b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_fine, &topo));
26852b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
26862b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
26872b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f));
2688681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis_fine));
26892b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
26902b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref));
26912b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f, &q_weight));
26922b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad));
26932b730f8bSJeremy 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));
26942b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
26952b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
26962b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
269783d6adf3SZach Atkins   }
2698eaf62fffSJeremy L Thompson 
2699eaf62fffSJeremy L Thompson   // Core code
27007758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
27019bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
2702eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2703eaf62fffSJeremy L Thompson }
2704eaf62fffSJeremy L Thompson 
2705eaf62fffSJeremy L Thompson /**
2706ca94c3ddSJeremy L Thompson   @brief Build a FDM based approximate inverse for each element for a `CeedOperator`.
2707eaf62fffSJeremy L Thompson 
2708ca94c3ddSJeremy L Thompson   This returns a `CeedOperator` and `CeedVector` to apply a Fast Diagonalization Method based approximate inverse.
2709859c15bbSJames 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$.
2710ca94c3ddSJeremy 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$.
2711ca94c3ddSJeremy L Thompson   The `CeedOperator` must be linear and non-composite.
2712ca94c3ddSJeremy L Thompson   The associated `CeedQFunction` must therefore also be linear.
2713eaf62fffSJeremy L Thompson 
2714ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2715f04ea552SJeremy L Thompson 
2716ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to create element inverses
2717ca94c3ddSJeremy L Thompson   @param[out] fdm_inv `CeedOperator` to apply the action of a FDM based inverse for each element
2718ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2719eaf62fffSJeremy L Thompson 
2720eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2721eaf62fffSJeremy L Thompson 
2722480fae85SJeremy L Thompson   @ref User
2723eaf62fffSJeremy L Thompson **/
27242b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) {
27251c66c397SJeremy L Thompson   Ceed                 ceed, ceed_parent;
27261c66c397SJeremy L Thompson   bool                 interp = false, grad = false, is_tensor_basis = true;
27271c66c397SJeremy L Thompson   CeedInt              num_input_fields, P_1d, Q_1d, num_nodes, num_qpts, dim, num_comp = 1, num_elem = 1;
27281c66c397SJeremy L Thompson   CeedScalar          *mass, *laplace, *x, *fdm_interp, *lambda, *elem_avg;
27291c66c397SJeremy L Thompson   const CeedScalar    *interp_1d, *grad_1d, *q_weight_1d;
27301c66c397SJeremy L Thompson   CeedVector           q_data;
27311c66c397SJeremy L Thompson   CeedElemRestriction  rstr  = NULL, rstr_qd_i;
27321c66c397SJeremy L Thompson   CeedBasis            basis = NULL, fdm_basis;
27331c66c397SJeremy L Thompson   CeedQFunctionContext ctx_fdm;
27341c66c397SJeremy L Thompson   CeedQFunctionField  *qf_fields;
27351c66c397SJeremy L Thompson   CeedQFunction        qf, qf_fdm;
27361c66c397SJeremy L Thompson   CeedOperatorField   *op_fields;
27371c66c397SJeremy L Thompson 
27382b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2739eaf62fffSJeremy L Thompson 
2740eaf62fffSJeremy L Thompson   if (op->CreateFDMElementInverse) {
2741d04bbc78SJeremy L Thompson     // Backend version
27422b730f8bSJeremy L Thompson     CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request));
2743eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2744eaf62fffSJeremy L Thompson   } else {
2745d04bbc78SJeremy L Thompson     // Operator fallback
2746d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2747d04bbc78SJeremy L Thompson 
27482b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2749d04bbc78SJeremy L Thompson     if (op_fallback) {
27502b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request));
2751eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2752eaf62fffSJeremy L Thompson     }
2753eaf62fffSJeremy L Thompson   }
2754eaf62fffSJeremy L Thompson 
2755d04bbc78SJeremy L Thompson   // Default interface implementation
27562b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
2757bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
27582b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
2759eaf62fffSJeremy L Thompson 
2760eaf62fffSJeremy L Thompson   // Determine active input basis
27612b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL));
27622b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
2763eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
2764eaf62fffSJeremy L Thompson     CeedVector vec;
27651c66c397SJeremy L Thompson 
27662b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
2767eaf62fffSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2768eaf62fffSJeremy L Thompson       CeedEvalMode eval_mode;
27691c66c397SJeremy L Thompson 
27702b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
2771eaf62fffSJeremy L Thompson       interp = interp || eval_mode == CEED_EVAL_INTERP;
2772eaf62fffSJeremy L Thompson       grad   = grad || eval_mode == CEED_EVAL_GRAD;
2773681d0ea7SJeremy L Thompson       if (!basis) CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis));
2774681d0ea7SJeremy L Thompson       if (!rstr) CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
2775eaf62fffSJeremy L Thompson     }
2776681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
2777eaf62fffSJeremy L Thompson   }
27786574a04fSJeremy L Thompson   CeedCheck(basis, ceed, CEED_ERROR_BACKEND, "No active field set");
27792b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
2780352a5e7cSSebastian Grimberg   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
27812b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
27822b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
27832b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
27842b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
27852b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
2786eaf62fffSJeremy L Thompson 
2787eaf62fffSJeremy L Thompson   // Build and diagonalize 1D Mass and Laplacian
27886574a04fSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
27896574a04fSJeremy L Thompson   CeedCheck(is_tensor_basis, ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases");
27902b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &mass));
27912b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &laplace));
27922b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &x));
27932b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp));
27942b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &lambda));
2795eaf62fffSJeremy L Thompson   // -- Build matrices
27962b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
27972b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
27982b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
27992b730f8bSJeremy L Thompson   CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace));
2800eaf62fffSJeremy L Thompson 
2801eaf62fffSJeremy L Thompson   // -- Diagonalize
28022b730f8bSJeremy L Thompson   CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d));
28032b730f8bSJeremy L Thompson   CeedCall(CeedFree(&mass));
28042b730f8bSJeremy L Thompson   CeedCall(CeedFree(&laplace));
28052b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
28062b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d];
28072b730f8bSJeremy L Thompson   }
28082b730f8bSJeremy L Thompson   CeedCall(CeedFree(&x));
2809eaf62fffSJeremy L Thompson 
28101c66c397SJeremy L Thompson   {
28111c66c397SJeremy L Thompson     CeedInt             layout[3], num_modes = (interp ? 1 : 0) + (grad ? dim : 0);
28121c66c397SJeremy L Thompson     CeedScalar          max_norm = 0;
28131c66c397SJeremy L Thompson     const CeedScalar   *assembled_array, *q_weight_array;
28141c66c397SJeremy L Thompson     CeedVector          assembled = NULL, q_weight;
2815c5f45aeaSJeremy L Thompson     CeedElemRestriction rstr_qf   = NULL;
28161c66c397SJeremy L Thompson 
28171c66c397SJeremy L Thompson     // Assemble QFunction
28182b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request));
281956c48462SJeremy L Thompson     CeedCall(CeedElemRestrictionGetELayout(rstr_qf, layout));
28202b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr_qf));
28212b730f8bSJeremy L Thompson     CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm));
2822eaf62fffSJeremy L Thompson 
2823eaf62fffSJeremy L Thompson     // Calculate element averages
28242b730f8bSJeremy L Thompson     CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight));
28252b730f8bSJeremy L Thompson     CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight));
28262b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array));
28272b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array));
28282b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_elem, &elem_avg));
2829eaf62fffSJeremy L Thompson     const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON;
28301c66c397SJeremy L Thompson 
2831eaf62fffSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
2832eaf62fffSJeremy L Thompson       CeedInt count = 0;
28331c66c397SJeremy L Thompson 
28342b730f8bSJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
28352b730f8bSJeremy L Thompson         for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) {
28362b730f8bSJeremy L Thompson           if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) {
28372b730f8bSJeremy L Thompson             elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q];
2838eaf62fffSJeremy L Thompson             count++;
2839eaf62fffSJeremy L Thompson           }
28402b730f8bSJeremy L Thompson         }
28412b730f8bSJeremy L Thompson       }
2842eaf62fffSJeremy L Thompson       if (count) {
2843eaf62fffSJeremy L Thompson         elem_avg[e] /= count;
2844eaf62fffSJeremy L Thompson       } else {
2845eaf62fffSJeremy L Thompson         elem_avg[e] = 1.0;
2846eaf62fffSJeremy L Thompson       }
2847eaf62fffSJeremy L Thompson     }
28482b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array));
28492b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled));
28502b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array));
28512b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&q_weight));
28521c66c397SJeremy L Thompson   }
2853eaf62fffSJeremy L Thompson 
2854eaf62fffSJeremy L Thompson   // Build FDM diagonal
28551c66c397SJeremy L Thompson   {
2856eaf62fffSJeremy L Thompson     CeedScalar *q_data_array, *fdm_diagonal;
28571c66c397SJeremy L Thompson 
2858352a5e7cSSebastian Grimberg     CeedCall(CeedCalloc(num_comp * num_nodes, &fdm_diagonal));
2859352a5e7cSSebastian Grimberg     const CeedScalar fdm_diagonal_bound = num_nodes * CEED_EPSILON;
28602b730f8bSJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
2861352a5e7cSSebastian Grimberg       for (CeedInt n = 0; n < num_nodes; n++) {
2862352a5e7cSSebastian Grimberg         if (interp) fdm_diagonal[c * num_nodes + n] = 1.0;
28632b730f8bSJeremy L Thompson         if (grad) {
2864eaf62fffSJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
2865eaf62fffSJeremy L Thompson             CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d;
2866352a5e7cSSebastian Grimberg             fdm_diagonal[c * num_nodes + n] += lambda[i];
2867eaf62fffSJeremy L Thompson           }
2868eaf62fffSJeremy L Thompson         }
2869352a5e7cSSebastian Grimberg         if (fabs(fdm_diagonal[c * num_nodes + n]) < fdm_diagonal_bound) fdm_diagonal[c * num_nodes + n] = fdm_diagonal_bound;
28702b730f8bSJeremy L Thompson       }
28712b730f8bSJeremy L Thompson     }
2872352a5e7cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * num_nodes, &q_data));
28732b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(q_data, 0.0));
28742b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array));
28752b730f8bSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
28762b730f8bSJeremy L Thompson       for (CeedInt c = 0; c < num_comp; c++) {
28776c10af5dSJeremy L Thompson         for (CeedInt n = 0; n < num_nodes; n++) {
28781c66c397SJeremy L Thompson           q_data_array[(e * num_comp + c) * num_nodes + n] = 1. / (elem_avg[e] * fdm_diagonal[c * num_nodes + n]);
28792b730f8bSJeremy L Thompson         }
28802b730f8bSJeremy L Thompson       }
28816c10af5dSJeremy L Thompson     }
28822b730f8bSJeremy L Thompson     CeedCall(CeedFree(&elem_avg));
28832b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_diagonal));
28842b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(q_data, &q_data_array));
28851c66c397SJeremy L Thompson   }
2886eaf62fffSJeremy L Thompson 
2887eaf62fffSJeremy L Thompson   // Setup FDM operator
2888eaf62fffSJeremy L Thompson   // -- Basis
28891c66c397SJeremy L Thompson   {
2890eaf62fffSJeremy L Thompson     CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy;
28911c66c397SJeremy L Thompson 
28922b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy));
28932b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_ref_dummy));
28942b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_weight_dummy));
28952b730f8bSJeremy 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));
28962b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_interp));
28972b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad_dummy));
28982b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref_dummy));
28992b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight_dummy));
29002b730f8bSJeremy L Thompson     CeedCall(CeedFree(&lambda));
29011c66c397SJeremy L Thompson   }
2902eaf62fffSJeremy L Thompson 
2903eaf62fffSJeremy L Thompson   // -- Restriction
29041c66c397SJeremy L Thompson   {
2905352a5e7cSSebastian Grimberg     CeedInt strides[3] = {1, num_nodes, num_nodes * num_comp};
29060a5597ceSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, num_nodes, num_comp,
29070a5597ceSJeremy L Thompson                                               (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_nodes, strides, &rstr_qd_i));
29081c66c397SJeremy L Thompson   }
29091c66c397SJeremy L Thompson 
2910eaf62fffSJeremy L Thompson   // -- QFunction
29112b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm));
29122b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP));
29132b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE));
29142b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP));
29152b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp));
29161c66c397SJeremy L Thompson 
2917eaf62fffSJeremy L Thompson   // -- QFunction context
29181c66c397SJeremy L Thompson   {
2919eaf62fffSJeremy L Thompson     CeedInt *num_comp_data;
29201c66c397SJeremy L Thompson 
29212b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_data));
2922eaf62fffSJeremy L Thompson     num_comp_data[0] = num_comp;
29232b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm));
29242b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data));
29251c66c397SJeremy L Thompson   }
29262b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm));
29272b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_fdm));
29281c66c397SJeremy L Thompson 
2929eaf62fffSJeremy L Thompson   // -- Operator
29302b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv));
29312b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2932356036faSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_NONE, q_data));
29332b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2934eaf62fffSJeremy L Thompson 
2935eaf62fffSJeremy L Thompson   // Cleanup
29369bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
29379bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed_parent));
29382b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_data));
2939681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr));
29402b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i));
2941681d0ea7SJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis));
2942681d0ea7SJeremy L Thompson   CeedCall(CeedBasisDestroy(&fdm_basis));
2943c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
29442b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_fdm));
2945eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2946eaf62fffSJeremy L Thompson }
2947eaf62fffSJeremy L Thompson 
2948eaf62fffSJeremy L Thompson /// @}
2949