xref: /libCEED/interface/ceed-preconditioning.c (revision 915834c9f1e582e3fdfc87db6b4fa4e010d293bb)
1d275d636SJeremy L Thompson // Copyright (c) 2017-2025, 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 {
139bcd92680SJeremy L Thompson     bool               is_at_points = false;
1401203703bSJeremy L Thompson     CeedInt            num_input_fields, num_output_fields;
1419e77b9c8SJeremy L Thompson     CeedQFunction      qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL;
1421203703bSJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
1431c66c397SJeremy L Thompson 
1442b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback));
1452b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback));
1462b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback));
147bcd92680SJeremy L Thompson     CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
148bcd92680SJeremy L Thompson     if (is_at_points) {
149bcd92680SJeremy L Thompson       CeedVector          points;
150bcd92680SJeremy L Thompson       CeedElemRestriction rstr_points;
151bcd92680SJeremy L Thompson 
152bcd92680SJeremy L Thompson       CeedCall(CeedOperatorCreateAtPoints(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback));
153bcd92680SJeremy L Thompson       CeedCall(CeedOperatorAtPointsGetPoints(op, &rstr_points, &points));
154bcd92680SJeremy L Thompson       CeedCall(CeedOperatorAtPointsSetPoints(op_fallback, rstr_points, points));
155bcd92680SJeremy L Thompson       CeedCall(CeedVectorDestroy(&points));
156bcd92680SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&rstr_points));
157bcd92680SJeremy L Thompson     } else {
1582b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback));
159bcd92680SJeremy L Thompson     }
1601203703bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1611203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1626f8994e9SJeremy L Thompson       const char         *field_name;
1631203703bSJeremy L Thompson       CeedVector          vec;
1641203703bSJeremy L Thompson       CeedElemRestriction rstr;
1651203703bSJeremy L Thompson       CeedBasis           basis;
1661203703bSJeremy L Thompson 
167ab747706SJeremy L Thompson       CeedCall(CeedOperatorFieldGetData(input_fields[i], &field_name, &rstr, &basis, &vec));
1681203703bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec));
169681d0ea7SJeremy L Thompson       CeedCall(CeedVectorDestroy(&vec));
170681d0ea7SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&rstr));
171681d0ea7SJeremy L Thompson       CeedCall(CeedBasisDestroy(&basis));
172805fe78eSJeremy L Thompson     }
1731203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1746f8994e9SJeremy L Thompson       const char         *field_name;
1751203703bSJeremy L Thompson       CeedVector          vec;
1761203703bSJeremy L Thompson       CeedElemRestriction rstr;
1771203703bSJeremy L Thompson       CeedBasis           basis;
1781203703bSJeremy L Thompson 
179ab747706SJeremy L Thompson       CeedCall(CeedOperatorFieldGetData(output_fields[i], &field_name, &rstr, &basis, &vec));
1801203703bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec));
181681d0ea7SJeremy L Thompson       CeedCall(CeedVectorDestroy(&vec));
182681d0ea7SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&rstr));
183681d0ea7SJeremy L Thompson       CeedCall(CeedBasisDestroy(&basis));
184805fe78eSJeremy L Thompson     }
1857d5185d7SSebastian Grimberg     {
1867d5185d7SSebastian Grimberg       CeedQFunctionAssemblyData data;
1877d5185d7SSebastian Grimberg 
1887d5185d7SSebastian Grimberg       CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
1897d5185d7SSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataReferenceCopy(data, &op_fallback->qf_assembled));
1907d5185d7SSebastian Grimberg     }
1919e77b9c8SJeremy L Thompson     // Cleanup
1922b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf_fallback));
1932b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqf_fallback));
1942b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqfT_fallback));
195805fe78eSJeremy L Thompson   }
1962b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetName(op_fallback, op->name));
1972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fallback));
198b05f7e9fSJeremy L Thompson   // Note: No ref-counting here so we don't get caught in a reference loop.
199b05f7e9fSJeremy L Thompson   //       The op holds the only reference to op_fallback and is responsible for deleting itself and op_fallback.
200805fe78eSJeremy L Thompson   op->op_fallback                 = op_fallback;
201b05f7e9fSJeremy L Thompson   op_fallback->op_fallback_parent = op;
2029bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed_fallback));
203eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
204eaf62fffSJeremy L Thompson }
205eaf62fffSJeremy L Thompson 
206eaf62fffSJeremy L Thompson /**
207eaf62fffSJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
208eaf62fffSJeremy L Thompson 
2090cd9fdf4SJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble diagonal or point block diagonal
210ca94c3ddSJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
211bd83916cSSebastian Grimberg   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
212ca94c3ddSJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
213eaf62fffSJeremy L Thompson 
214eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
215eaf62fffSJeremy L Thompson 
216eaf62fffSJeremy L Thompson   @ref Developer
217eaf62fffSJeremy L Thompson **/
2180cd9fdf4SJeremy L Thompson static inline int CeedSingleOperatorLinearAssembleAddDiagonal_Mesh(CeedOperator op, CeedRequest *request, const bool is_point_block,
219f3bd9308SJeremy L Thompson                                                                    CeedVector assembled) {
220506b1a0cSSebastian Grimberg   bool is_composite;
221506b1a0cSSebastian Grimberg 
222506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2239bc66399SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
224506b1a0cSSebastian Grimberg 
225506b1a0cSSebastian Grimberg   // Assemble QFunction
226506b1a0cSSebastian Grimberg   CeedInt             layout_qf[3];
227437c7c90SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
228c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
229c5f45aeaSJeremy L Thompson   CeedElemRestriction assembled_elem_rstr = NULL;
230437c7c90SJeremy L Thompson 
231437c7c90SJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request));
23256c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf));
233437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
234437c7c90SJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
235eaf62fffSJeremy L Thompson 
236ed9e99e6SJeremy L Thompson   // Get assembly data
237437c7c90SJeremy L Thompson   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
238506b1a0cSSebastian Grimberg   CeedInt                  num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out;
239437c7c90SJeremy L Thompson   CeedSize               **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components;
240506b1a0cSSebastian Grimberg   CeedBasis               *active_bases_in, *active_bases_out;
241506b1a0cSSebastian Grimberg   CeedElemRestriction     *active_elem_rstrs_in, *active_elem_rstrs_out;
2421c66c397SJeremy L Thompson   CeedOperatorAssemblyData data;
2431c66c397SJeremy L Thompson 
244437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
245506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in,
246506b1a0cSSebastian Grimberg                                                 &num_active_bases_out, &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out,
247506b1a0cSSebastian Grimberg                                                 &num_output_components));
248506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, NULL, NULL, &active_bases_out, NULL));
249506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs_in, NULL, &active_elem_rstrs_out));
250506b1a0cSSebastian Grimberg 
251934a29f5SSebastian Grimberg   // Loop over all active bases (find matching input/output pairs)
252934a29f5SSebastian Grimberg   for (CeedInt b = 0; b < CeedIntMin(num_active_bases_in, num_active_bases_out); b++) {
253934a29f5SSebastian Grimberg     CeedInt             b_in, b_out, num_elem, num_nodes, num_qpts, num_comp;
2541c66c397SJeremy L Thompson     bool                has_eval_none = false;
2551c66c397SJeremy L Thompson     CeedScalar         *elem_diag_array, *identity = NULL;
2561c66c397SJeremy L Thompson     CeedVector          elem_diag;
2577c1dbaffSSebastian Grimberg     CeedElemRestriction diag_elem_rstr;
2581c66c397SJeremy L Thompson 
259934a29f5SSebastian Grimberg     if (num_active_bases_in <= num_active_bases_out) {
260934a29f5SSebastian Grimberg       b_in = b;
261934a29f5SSebastian Grimberg       for (b_out = 0; b_out < num_active_bases_out; b_out++) {
262934a29f5SSebastian Grimberg         if (active_bases_in[b_in] == active_bases_out[b_out]) {
263934a29f5SSebastian Grimberg           break;
264934a29f5SSebastian Grimberg         }
265934a29f5SSebastian Grimberg       }
266934a29f5SSebastian Grimberg       if (b_out == num_active_bases_out) {
267934a29f5SSebastian Grimberg         continue;
268934a29f5SSebastian Grimberg       }  // No matching output basis found
269934a29f5SSebastian Grimberg     } else {
270934a29f5SSebastian Grimberg       b_out = b;
271934a29f5SSebastian Grimberg       for (b_in = 0; b_in < num_active_bases_in; b_in++) {
272934a29f5SSebastian Grimberg         if (active_bases_in[b_in] == active_bases_out[b_out]) {
273934a29f5SSebastian Grimberg           break;
274934a29f5SSebastian Grimberg         }
275934a29f5SSebastian Grimberg       }
276934a29f5SSebastian Grimberg       if (b_in == num_active_bases_in) {
277934a29f5SSebastian Grimberg         continue;
278934a29f5SSebastian Grimberg       }  // No matching output basis found
279934a29f5SSebastian Grimberg     }
2809bc66399SJeremy L Thompson     CeedCheck(active_elem_rstrs_in[b_in] == active_elem_rstrs_out[b_out], CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
281506b1a0cSSebastian Grimberg               "Cannot assemble operator diagonal with different input and output active element restrictions");
282506b1a0cSSebastian Grimberg 
2831c66c397SJeremy L Thompson     // Assemble point block diagonal restriction, if needed
284bd83916cSSebastian Grimberg     if (is_point_block) {
285934a29f5SSebastian Grimberg       CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstrs_in[b_in], &diag_elem_rstr));
2867c1dbaffSSebastian Grimberg     } else {
287934a29f5SSebastian Grimberg       CeedCall(CeedElemRestrictionCreateUnsignedCopy(active_elem_rstrs_in[b_in], &diag_elem_rstr));
288eaf62fffSJeremy L Thompson     }
289eaf62fffSJeremy L Thompson 
290eaf62fffSJeremy L Thompson     // Create diagonal vector
291437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag));
292eaf62fffSJeremy L Thompson 
293eaf62fffSJeremy L Thompson     // Assemble element operator diagonals
2942b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(elem_diag, 0.0));
2952b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array));
296437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem));
297934a29f5SSebastian Grimberg     CeedCall(CeedBasisGetNumNodes(active_bases_in[b_in], &num_nodes));
298934a29f5SSebastian Grimberg     CeedCall(CeedBasisGetNumComponents(active_bases_in[b_in], &num_comp));
299934a29f5SSebastian Grimberg     if (active_bases_in[b_in] == CEED_BASIS_NONE) num_qpts = num_nodes;
300934a29f5SSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(active_bases_in[b_in], &num_qpts));
301ed9e99e6SJeremy L Thompson 
302352a5e7cSSebastian Grimberg     // Construct identity matrix for basis if required
303934a29f5SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in[b_in]; i++) {
304934a29f5SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[b_in][i] == CEED_EVAL_NONE);
305ed9e99e6SJeremy L Thompson     }
306934a29f5SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out[b_out]; i++) {
307934a29f5SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[b_out][i] == CEED_EVAL_NONE);
308ed9e99e6SJeremy L Thompson     }
309ed9e99e6SJeremy L Thompson     if (has_eval_none) {
3102b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
3112b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
312eaf62fffSJeremy L Thompson     }
313352a5e7cSSebastian Grimberg 
314eaf62fffSJeremy L Thompson     // Compute the diagonal of B^T D B
315eaf62fffSJeremy L Thompson     // Each element
316b94338b9SJed Brown     for (CeedSize e = 0; e < num_elem; e++) {
317eaf62fffSJeremy L Thompson       // Each basis eval mode pair
318352a5e7cSSebastian Grimberg       CeedInt      d_out              = 0, q_comp_out;
319352a5e7cSSebastian Grimberg       CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
3201c66c397SJeremy L Thompson 
321934a29f5SSebastian Grimberg       for (CeedInt e_out = 0; e_out < num_eval_modes_out[b_out]; e_out++) {
3221c66c397SJeremy L Thompson         CeedInt           d_in              = 0, q_comp_in;
323437c7c90SJeremy L Thompson         const CeedScalar *B_t               = NULL;
3241c66c397SJeremy L Thompson         CeedEvalMode      eval_mode_in_prev = CEED_EVAL_NONE;
3251c66c397SJeremy L Thompson 
326934a29f5SSebastian Grimberg         CeedCall(CeedOperatorGetBasisPointer(active_bases_out[b_out], eval_modes_out[b_out][e_out], identity, &B_t));
327934a29f5SSebastian Grimberg         CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_out[b_out], eval_modes_out[b_out][e_out], &q_comp_out));
328352a5e7cSSebastian Grimberg         if (q_comp_out > 1) {
329934a29f5SSebastian Grimberg           if (e_out == 0 || eval_modes_out[b_out][e_out] != eval_mode_out_prev) d_out = 0;
330352a5e7cSSebastian Grimberg           else B_t = &B_t[(++d_out) * num_qpts * num_nodes];
331352a5e7cSSebastian Grimberg         }
332934a29f5SSebastian Grimberg         eval_mode_out_prev = eval_modes_out[b_out][e_out];
333352a5e7cSSebastian Grimberg 
334934a29f5SSebastian Grimberg         for (CeedInt e_in = 0; e_in < num_eval_modes_in[b_in]; e_in++) {
335437c7c90SJeremy L Thompson           const CeedScalar *B = NULL;
3361c66c397SJeremy L Thompson 
337934a29f5SSebastian Grimberg           CeedCall(CeedOperatorGetBasisPointer(active_bases_in[b_in], eval_modes_in[b_in][e_in], identity, &B));
338934a29f5SSebastian Grimberg           CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_in[b_in], eval_modes_in[b_in][e_in], &q_comp_in));
339352a5e7cSSebastian Grimberg           if (q_comp_in > 1) {
340934a29f5SSebastian Grimberg             if (e_in == 0 || eval_modes_in[b_in][e_in] != eval_mode_in_prev) d_in = 0;
341352a5e7cSSebastian Grimberg             else B = &B[(++d_in) * num_qpts * num_nodes];
342352a5e7cSSebastian Grimberg           }
343934a29f5SSebastian Grimberg           eval_mode_in_prev = eval_modes_in[b_in][e_in];
344352a5e7cSSebastian Grimberg 
345eaf62fffSJeremy L Thompson           // Each component
346506b1a0cSSebastian Grimberg           for (CeedInt c_out = 0; c_out < num_comp; c_out++) {
347437c7c90SJeremy L Thompson             // Each qpt/node pair
3482b730f8bSJeremy L Thompson             for (CeedInt q = 0; q < num_qpts; q++) {
349bd83916cSSebastian Grimberg               if (is_point_block) {
350eaf62fffSJeremy L Thompson                 // Point Block Diagonal
351506b1a0cSSebastian Grimberg                 for (CeedInt c_in = 0; c_in < num_comp; c_in++) {
352934a29f5SSebastian Grimberg                   const CeedSize c_offset =
353934a29f5SSebastian Grimberg                       (eval_mode_offsets_in[b_in][e_in] + c_in) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out;
354506b1a0cSSebastian Grimberg                   const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]];
3551c66c397SJeremy L Thompson 
3562b730f8bSJeremy L Thompson                   for (CeedInt n = 0; n < num_nodes; n++) {
357506b1a0cSSebastian Grimberg                     elem_diag_array[((e * num_comp + c_out) * num_comp + c_in) * num_nodes + n] +=
358437c7c90SJeremy L Thompson                         B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n];
359eaf62fffSJeremy L Thompson                   }
3602b730f8bSJeremy L Thompson                 }
361eaf62fffSJeremy L Thompson               } else {
362eaf62fffSJeremy L Thompson                 // Diagonal Only
363934a29f5SSebastian Grimberg                 const CeedInt c_offset =
364934a29f5SSebastian Grimberg                     (eval_mode_offsets_in[b_in][e_in] + c_out) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out;
365506b1a0cSSebastian Grimberg                 const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]];
3661c66c397SJeremy L Thompson 
3672b730f8bSJeremy L Thompson                 for (CeedInt n = 0; n < num_nodes; n++) {
368506b1a0cSSebastian 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];
369eaf62fffSJeremy L Thompson                 }
370eaf62fffSJeremy L Thompson               }
371eaf62fffSJeremy L Thompson             }
372eaf62fffSJeremy L Thompson           }
3732b730f8bSJeremy L Thompson         }
3742b730f8bSJeremy L Thompson       }
3752b730f8bSJeremy L Thompson     }
3762b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
377eaf62fffSJeremy L Thompson 
378eaf62fffSJeremy L Thompson     // Assemble local operator diagonal
3797c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
380eaf62fffSJeremy L Thompson 
381eaf62fffSJeremy L Thompson     // Cleanup
3827c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr));
3832b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&elem_diag));
3842b730f8bSJeremy L Thompson     CeedCall(CeedFree(&identity));
385437c7c90SJeremy L Thompson   }
386437c7c90SJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
387437c7c90SJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
388eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
389eaf62fffSJeremy L Thompson }
390eaf62fffSJeremy L Thompson 
391eaf62fffSJeremy L Thompson /**
3920cd9fdf4SJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
3930cd9fdf4SJeremy L Thompson 
3940cd9fdf4SJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble diagonal or point block diagonal
3950cd9fdf4SJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
3960cd9fdf4SJeremy L Thompson   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
3970cd9fdf4SJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
3980cd9fdf4SJeremy L Thompson 
3990cd9fdf4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4000cd9fdf4SJeremy L Thompson 
4010cd9fdf4SJeremy L Thompson   @ref Developer
4020cd9fdf4SJeremy L Thompson **/
4030cd9fdf4SJeremy L Thompson static inline int CeedSingleOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block,
4040cd9fdf4SJeremy L Thompson                                                               CeedVector assembled) {
4050cd9fdf4SJeremy L Thompson   bool is_at_points;
4060cd9fdf4SJeremy L Thompson 
4070cd9fdf4SJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
4089bc66399SJeremy L Thompson   CeedCheck(!is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "AtPoints operator not supported");
4090cd9fdf4SJeremy L Thompson   CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal_Mesh(op, request, is_point_block, assembled));
4100cd9fdf4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4110cd9fdf4SJeremy L Thompson }
4120cd9fdf4SJeremy L Thompson 
4130cd9fdf4SJeremy L Thompson /**
414eaf62fffSJeremy L Thompson   @brief Core logic for assembling composite operator diagonal
415eaf62fffSJeremy L Thompson 
416ca94c3ddSJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble point block diagonal
417ca94c3ddSJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
418bd83916cSSebastian Grimberg   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
419ca94c3ddSJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
420eaf62fffSJeremy L Thompson 
421eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
422eaf62fffSJeremy L Thompson 
423eaf62fffSJeremy L Thompson   @ref Developer
424eaf62fffSJeremy L Thompson **/
425bd83916cSSebastian Grimberg static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block,
426eaf62fffSJeremy L Thompson                                                                  CeedVector assembled) {
427eaf62fffSJeremy L Thompson   CeedInt       num_sub;
428eaf62fffSJeremy L Thompson   CeedOperator *suboperators;
4291c66c397SJeremy L Thompson 
430c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
431c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators));
432eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_sub; i++) {
433bd83916cSSebastian Grimberg     if (is_point_block) {
4342b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request));
4356aa95790SJeremy L Thompson     } else {
4362b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request));
4376aa95790SJeremy L Thompson     }
438eaf62fffSJeremy L Thompson   }
439eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
440eaf62fffSJeremy L Thompson }
441eaf62fffSJeremy L Thompson 
442eaf62fffSJeremy L Thompson /**
443ca94c3ddSJeremy L Thompson   @brief Build nonzero pattern for non-composite CeedOperator`.
444eaf62fffSJeremy L Thompson 
445ca94c3ddSJeremy L Thompson   Users should generally use @ref CeedOperatorLinearAssembleSymbolic().
446eaf62fffSJeremy L Thompson 
447ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` to assemble nonzero pattern
448eaf62fffSJeremy L Thompson   @param[in]  offset Offset for number of entries
449eaf62fffSJeremy L Thompson   @param[out] rows   Row number for each entry
450eaf62fffSJeremy L Thompson   @param[out] cols   Column number for each entry
451eaf62fffSJeremy L Thompson 
452eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
453eaf62fffSJeremy L Thompson 
454eaf62fffSJeremy L Thompson   @ref Developer
455eaf62fffSJeremy L Thompson **/
4562b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) {
457f3d47e36SJeremy L Thompson   Ceed                ceed;
458f3d47e36SJeremy L Thompson   bool                is_composite;
45981670346SSebastian Grimberg   CeedSize            num_nodes_in, num_nodes_out, local_num_entries, count = 0;
460506b1a0cSSebastian Grimberg   CeedInt             num_elem_in, elem_size_in, num_comp_in, layout_er_in[3];
46181670346SSebastian Grimberg   CeedInt             num_elem_out, elem_size_out, num_comp_out, layout_er_out[3];
4621c66c397SJeremy L Thompson   CeedScalar         *array;
463506b1a0cSSebastian Grimberg   const CeedScalar   *elem_dof_a_in, *elem_dof_a_out;
464506b1a0cSSebastian Grimberg   CeedVector          index_vec_in, index_vec_out, elem_dof_in, elem_dof_out;
465506b1a0cSSebastian Grimberg   CeedElemRestriction elem_rstr_in, elem_rstr_out, index_elem_rstr_in, index_elem_rstr_out;
4661c66c397SJeremy L Thompson 
467f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4689bc66399SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
4695e1f751eSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
470eaf62fffSJeremy L Thompson 
471506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes_in, &num_nodes_out));
472506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out));
473506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in));
474506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in));
475506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in));
47656c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(elem_rstr_in, layout_er_in));
477eaf62fffSJeremy L Thompson 
478506b1a0cSSebastian Grimberg   // Determine elem_dof relation for input
479506b1a0cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed, num_nodes_in, &index_vec_in));
480506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayWrite(index_vec_in, CEED_MEM_HOST, &array));
481c81f2b9dSJames Wright   for (CeedSize i = 0; i < num_nodes_in; i++) array[i] = i;
482506b1a0cSSebastian Grimberg   CeedCall(CeedVectorRestoreArray(index_vec_in, &array));
483506b1a0cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed, num_elem_in * elem_size_in * num_comp_in, &elem_dof_in));
484506b1a0cSSebastian Grimberg   CeedCall(CeedVectorSetValue(elem_dof_in, 0.0));
485506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_in, &index_elem_rstr_in));
486506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionApply(index_elem_rstr_in, CEED_NOTRANSPOSE, index_vec_in, elem_dof_in, CEED_REQUEST_IMMEDIATE));
487506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayRead(elem_dof_in, CEED_MEM_HOST, &elem_dof_a_in));
488506b1a0cSSebastian Grimberg   CeedCall(CeedVectorDestroy(&index_vec_in));
489506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_in));
490506b1a0cSSebastian Grimberg 
491506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
492506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out));
493506b1a0cSSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
4943f08121cSJeremy L Thompson               "Active input and output operator restrictions must have the same number of elements."
4953f08121cSJeremy L Thompson               " Input has %" CeedInt_FMT " elements; output has %" CeedInt_FMT "elements.",
4963f08121cSJeremy L Thompson               num_elem_in, num_elem_out);
497506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out));
498506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out));
49956c48462SJeremy L Thompson     CeedCall(CeedElemRestrictionGetELayout(elem_rstr_out, layout_er_out));
500506b1a0cSSebastian Grimberg 
501506b1a0cSSebastian Grimberg     // Determine elem_dof relation for output
502506b1a0cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed, num_nodes_out, &index_vec_out));
503506b1a0cSSebastian Grimberg     CeedCall(CeedVectorGetArrayWrite(index_vec_out, CEED_MEM_HOST, &array));
504c81f2b9dSJames Wright     for (CeedSize i = 0; i < num_nodes_out; i++) array[i] = i;
505506b1a0cSSebastian Grimberg     CeedCall(CeedVectorRestoreArray(index_vec_out, &array));
506506b1a0cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed, num_elem_out * elem_size_out * num_comp_out, &elem_dof_out));
507506b1a0cSSebastian Grimberg     CeedCall(CeedVectorSetValue(elem_dof_out, 0.0));
508506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_out, &index_elem_rstr_out));
509506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(index_elem_rstr_out, CEED_NOTRANSPOSE, index_vec_out, elem_dof_out, CEED_REQUEST_IMMEDIATE));
510506b1a0cSSebastian Grimberg     CeedCall(CeedVectorGetArrayRead(elem_dof_out, CEED_MEM_HOST, &elem_dof_a_out));
511506b1a0cSSebastian Grimberg     CeedCall(CeedVectorDestroy(&index_vec_out));
512506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_out));
513506b1a0cSSebastian Grimberg   } else {
514506b1a0cSSebastian Grimberg     num_elem_out     = num_elem_in;
515506b1a0cSSebastian Grimberg     elem_size_out    = elem_size_in;
516506b1a0cSSebastian Grimberg     num_comp_out     = num_comp_in;
517506b1a0cSSebastian Grimberg     layout_er_out[0] = layout_er_in[0];
518506b1a0cSSebastian Grimberg     layout_er_out[1] = layout_er_in[1];
519506b1a0cSSebastian Grimberg     layout_er_out[2] = layout_er_in[2];
520506b1a0cSSebastian Grimberg     elem_dof_a_out   = elem_dof_a_in;
521506b1a0cSSebastian Grimberg   }
522c81f2b9dSJames Wright   local_num_entries = (CeedSize)elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in;
523eaf62fffSJeremy L Thompson 
524eaf62fffSJeremy L Thompson   // Determine i, j locations for element matrices
525506b1a0cSSebastian Grimberg   for (CeedInt e = 0; e < num_elem_in; e++) {
526506b1a0cSSebastian Grimberg     for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) {
527506b1a0cSSebastian Grimberg       for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) {
528506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < elem_size_out; i++) {
529506b1a0cSSebastian Grimberg           for (CeedInt j = 0; j < elem_size_in; j++) {
530506b1a0cSSebastian Grimberg             const CeedInt elem_dof_index_row = i * layout_er_out[0] + comp_out * layout_er_out[1] + e * layout_er_out[2];
531506b1a0cSSebastian Grimberg             const CeedInt elem_dof_index_col = j * layout_er_in[0] + comp_in * layout_er_in[1] + e * layout_er_in[2];
532506b1a0cSSebastian Grimberg             const CeedInt row                = elem_dof_a_out[elem_dof_index_row];
533506b1a0cSSebastian Grimberg             const CeedInt col                = elem_dof_a_in[elem_dof_index_col];
534eaf62fffSJeremy L Thompson 
535eaf62fffSJeremy L Thompson             rows[offset + count] = row;
536eaf62fffSJeremy L Thompson             cols[offset + count] = col;
537eaf62fffSJeremy L Thompson             count++;
538eaf62fffSJeremy L Thompson           }
539eaf62fffSJeremy L Thompson         }
540eaf62fffSJeremy L Thompson       }
541eaf62fffSJeremy L Thompson     }
542eaf62fffSJeremy L Thompson   }
5436574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing assembled entries");
544506b1a0cSSebastian Grimberg   CeedCall(CeedVectorRestoreArrayRead(elem_dof_in, &elem_dof_a_in));
545506b1a0cSSebastian Grimberg   CeedCall(CeedVectorDestroy(&elem_dof_in));
546506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
547506b1a0cSSebastian Grimberg     CeedCall(CeedVectorRestoreArrayRead(elem_dof_out, &elem_dof_a_out));
548506b1a0cSSebastian Grimberg     CeedCall(CeedVectorDestroy(&elem_dof_out));
549506b1a0cSSebastian Grimberg   }
550681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&elem_rstr_in));
551681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&elem_rstr_out));
5529bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
553eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
554eaf62fffSJeremy L Thompson }
555eaf62fffSJeremy L Thompson 
556eaf62fffSJeremy L Thompson /**
557ca94c3ddSJeremy L Thompson   @brief Assemble nonzero entries for non-composite `CeedOperator`.
558eaf62fffSJeremy L Thompson 
559ca94c3ddSJeremy L Thompson   Users should generally use @ref CeedOperatorLinearAssemble().
560eaf62fffSJeremy L Thompson 
561ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` to assemble
562ea61e9acSJeremy L Thompson   @param[in]  offset Offset for number of entries
563eaf62fffSJeremy L Thompson   @param[out] values Values to assemble into matrix
564eaf62fffSJeremy L Thompson 
565eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
566eaf62fffSJeremy L Thompson 
567eaf62fffSJeremy L Thompson   @ref Developer
568eaf62fffSJeremy L Thompson **/
5690183ed61SJeremy L Thompson int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) {
5701b95d8c6SJeremy L Thompson   bool is_composite, is_at_points;
5711c66c397SJeremy L Thompson 
572f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
5739bc66399SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
574f3d47e36SJeremy L Thompson 
575f3d47e36SJeremy L Thompson   // Early exit for empty operator
576f3d47e36SJeremy L Thompson   {
577f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
578f3d47e36SJeremy L Thompson 
579f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
580f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
581f3d47e36SJeremy L Thompson   }
582eaf62fffSJeremy L Thompson 
583cefa2673SJeremy L Thompson   if (op->LinearAssembleSingle) {
584cefa2673SJeremy L Thompson     // Backend version
5852b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSingle(op, offset, values));
586cefa2673SJeremy L Thompson     return CEED_ERROR_SUCCESS;
587cefa2673SJeremy L Thompson   } else {
588cefa2673SJeremy L Thompson     // Operator fallback
589cefa2673SJeremy L Thompson     CeedOperator op_fallback;
590cefa2673SJeremy L Thompson 
5912b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
592cefa2673SJeremy L Thompson     if (op_fallback) {
5932b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values));
594cefa2673SJeremy L Thompson       return CEED_ERROR_SUCCESS;
595cefa2673SJeremy L Thompson     }
596cefa2673SJeremy L Thompson   }
597cefa2673SJeremy L Thompson 
5981b95d8c6SJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
5991b95d8c6SJeremy L Thompson   CeedCheck(!is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
6001b95d8c6SJeremy L Thompson             "Backend does not implement CeedOperatorLinearAssemble for AtPoints operator");
6011b95d8c6SJeremy L Thompson 
602eaf62fffSJeremy L Thompson   // Assemble QFunction
603506b1a0cSSebastian Grimberg   CeedInt             layout_qf[3];
6041c66c397SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
605c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
606506b1a0cSSebastian Grimberg   CeedElemRestriction assembled_elem_rstr = NULL;
607eaf62fffSJeremy L Thompson 
608506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, CEED_REQUEST_IMMEDIATE));
60956c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf));
610506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
611506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
612eaf62fffSJeremy L Thompson 
613ed9e99e6SJeremy L Thompson   // Get assembly data
614506b1a0cSSebastian Grimberg   CeedInt                  num_elem_in, elem_size_in, num_comp_in, num_qpts_in;
61581670346SSebastian Grimberg   CeedInt                  num_elem_out, elem_size_out, num_comp_out, num_qpts_out;
61681670346SSebastian Grimberg   CeedSize                 local_num_entries, count = 0;
617506b1a0cSSebastian Grimberg   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
618506b1a0cSSebastian Grimberg   CeedInt                  num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out;
619506b1a0cSSebastian Grimberg   CeedBasis               *active_bases_in, *active_bases_out, basis_in, basis_out;
620506b1a0cSSebastian Grimberg   const CeedScalar       **B_mats_in, **B_mats_out, *B_mat_in, *B_mat_out;
621506b1a0cSSebastian Grimberg   CeedElemRestriction      elem_rstr_in, elem_rstr_out;
622506b1a0cSSebastian Grimberg   CeedRestrictionType      elem_rstr_type_in, elem_rstr_type_out;
623506b1a0cSSebastian Grimberg   const bool              *elem_rstr_orients_in = NULL, *elem_rstr_orients_out = NULL;
624506b1a0cSSebastian Grimberg   const CeedInt8          *elem_rstr_curl_orients_in = NULL, *elem_rstr_curl_orients_out = NULL;
625506b1a0cSSebastian Grimberg   CeedOperatorAssemblyData data;
626eaf62fffSJeremy L Thompson 
627506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
628506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, NULL, &num_active_bases_out,
629506b1a0cSSebastian Grimberg                                                 &num_eval_modes_out, &eval_modes_out, NULL, NULL));
630506b1a0cSSebastian Grimberg 
6319bc66399SJeremy L Thompson   CeedCheck(num_active_bases_in == 1 && num_active_bases_out == 1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
632506b1a0cSSebastian Grimberg             "Cannot assemble operator with multiple active bases");
6339bc66399SJeremy L Thompson   CeedCheck(num_eval_modes_in[0] > 0 && num_eval_modes_out[0] > 0, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
6349bc66399SJeremy L Thompson             "Cannot assemble operator without inputs/outputs");
635eaf62fffSJeremy L Thompson 
636506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, &B_mats_in, NULL, &active_bases_out, &B_mats_out));
637506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out));
638506b1a0cSSebastian Grimberg   basis_in  = active_bases_in[0];
639506b1a0cSSebastian Grimberg   basis_out = active_bases_out[0];
640506b1a0cSSebastian Grimberg   B_mat_in  = B_mats_in[0];
641506b1a0cSSebastian Grimberg   B_mat_out = B_mats_out[0];
642eaf62fffSJeremy L Thompson 
643506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in));
644506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in));
645506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in));
646506b1a0cSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
647506b1a0cSSebastian Grimberg   else CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
648506b1a0cSSebastian Grimberg 
649506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetType(elem_rstr_in, &elem_rstr_type_in));
650506b1a0cSSebastian Grimberg   if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) {
651506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_orients_in));
652506b1a0cSSebastian Grimberg   } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
653506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_curl_orients_in));
6547c1dbaffSSebastian Grimberg   }
6557c1dbaffSSebastian Grimberg 
656506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
657506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out));
6589bc66399SJeremy L Thompson     CeedCheck(num_elem_in == num_elem_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
6593f08121cSJeremy L Thompson               "Active input and output operator restrictions must have the same number of elements."
6603f08121cSJeremy L Thompson               " Input has %" CeedInt_FMT " elements; output has %" CeedInt_FMT "elements.",
6613f08121cSJeremy L Thompson               num_elem_in, num_elem_out);
662506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out));
663506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out));
664506b1a0cSSebastian Grimberg     if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
665506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
6669bc66399SJeremy L Thompson     CeedCheck(num_qpts_in == num_qpts_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
6673f08121cSJeremy L Thompson               "Active input and output bases must have the same number of quadrature points."
6683f08121cSJeremy L Thompson               " Input has %" CeedInt_FMT " points; output has %" CeedInt_FMT "points.",
6693f08121cSJeremy L Thompson               num_qpts_in, num_qpts_out);
670eaf62fffSJeremy L Thompson 
671506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(elem_rstr_out, &elem_rstr_type_out));
672506b1a0cSSebastian Grimberg     if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) {
673506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_orients_out));
674506b1a0cSSebastian Grimberg     } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
675506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_curl_orients_out));
676506b1a0cSSebastian Grimberg     }
677506b1a0cSSebastian Grimberg   } else {
678506b1a0cSSebastian Grimberg     num_elem_out  = num_elem_in;
679506b1a0cSSebastian Grimberg     elem_size_out = elem_size_in;
680506b1a0cSSebastian Grimberg     num_comp_out  = num_comp_in;
681506b1a0cSSebastian Grimberg     num_qpts_out  = num_qpts_in;
682506b1a0cSSebastian Grimberg 
683506b1a0cSSebastian Grimberg     elem_rstr_orients_out      = elem_rstr_orients_in;
684506b1a0cSSebastian Grimberg     elem_rstr_curl_orients_out = elem_rstr_curl_orients_in;
685506b1a0cSSebastian Grimberg   }
686c81f2b9dSJames Wright   local_num_entries = (CeedSize)elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in;
687506b1a0cSSebastian Grimberg 
688506b1a0cSSebastian Grimberg   // Loop over elements and put in data structure
6897c1dbaffSSebastian Grimberg   // We store B_mat_in, B_mat_out, BTD, elem_mat in row-major order
6900459ebd3SSebastian Grimberg   CeedTensorContract contract;
691123d890dSSebastian Grimberg   CeedScalar        *vals, *BTD_mat = NULL, *elem_mat = NULL, *elem_mat_b = NULL;
692506b1a0cSSebastian Grimberg 
693c22497adSSebastian Grimberg   CeedCall(CeedBasisGetTensorContract(basis_in, &contract));
694123d890dSSebastian Grimberg   CeedCall(CeedCalloc(elem_size_out * num_qpts_in * num_eval_modes_in[0], &BTD_mat));
695123d890dSSebastian Grimberg   CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat));
696506b1a0cSSebastian Grimberg   if (elem_rstr_curl_orients_in || elem_rstr_curl_orients_out) CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat_b));
6971c66c397SJeremy L Thompson 
69828ec399dSJeremy L Thompson   CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals));
699506b1a0cSSebastian Grimberg   for (CeedSize e = 0; e < num_elem_in; e++) {
700506b1a0cSSebastian Grimberg     for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) {
701506b1a0cSSebastian Grimberg       for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) {
702ed9e99e6SJeremy L Thompson         // Compute B^T*D
703506b1a0cSSebastian Grimberg         for (CeedSize n = 0; n < elem_size_out; n++) {
704506b1a0cSSebastian Grimberg           for (CeedSize q = 0; q < num_qpts_in; q++) {
705437c7c90SJeremy L Thompson             for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) {
706506b1a0cSSebastian Grimberg               const CeedSize btd_index = n * (num_qpts_in * num_eval_modes_in[0]) + q * num_eval_modes_in[0] + e_in;
707067fd99fSJeremy L Thompson               CeedScalar     sum       = 0.0;
7081c66c397SJeremy L Thompson 
709437c7c90SJeremy L Thompson               for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) {
710506b1a0cSSebastian Grimberg                 const CeedSize b_out_index     = (q * num_eval_modes_out[0] + e_out) * elem_size_out + n;
711506b1a0cSSebastian 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;
712b94338b9SJed Brown                 const CeedSize qf_index        = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2];
7131c66c397SJeremy L Thompson 
714067fd99fSJeremy L Thompson                 sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index];
715eaf62fffSJeremy L Thompson               }
716067fd99fSJeremy L Thompson               BTD_mat[btd_index] = sum;
717ed9e99e6SJeremy L Thompson             }
718ed9e99e6SJeremy L Thompson           }
719eaf62fffSJeremy L Thompson         }
7207c1dbaffSSebastian Grimberg 
7217c1dbaffSSebastian Grimberg         // Form element matrix itself (for each block component)
722e4065a52SSebastian Grimberg         if (contract) {
7230459ebd3SSebastian Grimberg           CeedCall(CeedTensorContractApply(contract, 1, num_qpts_in * num_eval_modes_in[0], elem_size_in, elem_size_out, BTD_mat, CEED_NOTRANSPOSE,
7240459ebd3SSebastian Grimberg                                            false, B_mat_in, elem_mat));
725e4065a52SSebastian Grimberg         } else {
7269bc66399SJeremy L Thompson           Ceed ceed;
7279bc66399SJeremy L Thompson 
7289bc66399SJeremy L Thompson           CeedCall(CeedOperatorGetCeed(op, &ceed));
729e4065a52SSebastian 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]));
7309bc66399SJeremy L Thompson           CeedCall(CeedDestroy(&ceed));
731e4065a52SSebastian Grimberg         }
732eaf62fffSJeremy L Thompson 
7337c1dbaffSSebastian Grimberg         // Transform the element matrix if required
734506b1a0cSSebastian Grimberg         if (elem_rstr_orients_out) {
735506b1a0cSSebastian Grimberg           const bool *elem_orients = &elem_rstr_orients_out[e * elem_size_out];
7361c66c397SJeremy L Thompson 
737506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
738506b1a0cSSebastian Grimberg             const double orient = elem_orients[i] ? -1.0 : 1.0;
739506b1a0cSSebastian Grimberg 
740506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
741506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] *= orient;
7427c1dbaffSSebastian Grimberg             }
7437c1dbaffSSebastian Grimberg           }
744506b1a0cSSebastian Grimberg         } else if (elem_rstr_curl_orients_out) {
745506b1a0cSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_out[e * 3 * elem_size_out];
7461c66c397SJeremy L Thompson 
7477c1dbaffSSebastian Grimberg           // T^T*(B^T*D*B)
748506b1a0cSSebastian Grimberg           memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar));
749506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
750506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
751506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * i + 1] +
752506b1a0cSSebastian Grimberg                                                (i > 0 ? elem_mat_b[(i - 1) * elem_size_in + j] * elem_curl_orients[3 * i - 1] : 0.0) +
753506b1a0cSSebastian Grimberg                                                (i < elem_size_out - 1 ? elem_mat_b[(i + 1) * elem_size_in + j] * elem_curl_orients[3 * i + 3] : 0.0);
7547c1dbaffSSebastian Grimberg             }
7557c1dbaffSSebastian Grimberg           }
756506b1a0cSSebastian Grimberg         }
757506b1a0cSSebastian Grimberg         if (elem_rstr_orients_in) {
758506b1a0cSSebastian Grimberg           const bool *elem_orients = &elem_rstr_orients_in[e * elem_size_in];
759506b1a0cSSebastian Grimberg 
760506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
761506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
762506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] *= elem_orients[j] ? -1.0 : 1.0;
763506b1a0cSSebastian Grimberg             }
764506b1a0cSSebastian Grimberg           }
765506b1a0cSSebastian Grimberg         } else if (elem_rstr_curl_orients_in) {
766506b1a0cSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_in[e * 3 * elem_size_in];
767506b1a0cSSebastian Grimberg 
768506b1a0cSSebastian Grimberg           // (B^T*D*B)*T
769506b1a0cSSebastian Grimberg           memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar));
770506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
771506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
772506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * j + 1] +
773506b1a0cSSebastian Grimberg                                                (j > 0 ? elem_mat_b[i * elem_size_in + j - 1] * elem_curl_orients[3 * j - 1] : 0.0) +
774506b1a0cSSebastian Grimberg                                                (j < elem_size_in - 1 ? elem_mat_b[i * elem_size_in + j + 1] * elem_curl_orients[3 * j + 3] : 0.0);
7757c1dbaffSSebastian Grimberg             }
7767c1dbaffSSebastian Grimberg           }
7777c1dbaffSSebastian Grimberg         }
7787c1dbaffSSebastian Grimberg 
7797c1dbaffSSebastian Grimberg         // Put element matrix in coordinate data structure
780506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < elem_size_out; i++) {
781506b1a0cSSebastian Grimberg           for (CeedInt j = 0; j < elem_size_in; j++) {
782506b1a0cSSebastian Grimberg             vals[offset + count] = elem_mat[i * elem_size_in + j];
783eaf62fffSJeremy L Thompson             count++;
784eaf62fffSJeremy L Thompson           }
785eaf62fffSJeremy L Thompson         }
786eaf62fffSJeremy L Thompson       }
787eaf62fffSJeremy L Thompson     }
788eaf62fffSJeremy L Thompson   }
7899bc66399SJeremy L Thompson   CeedCheck(count == local_num_entries, CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR, "Error computing entries");
7902b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(values, &vals));
791eaf62fffSJeremy L Thompson 
792506b1a0cSSebastian Grimberg   // Cleanup
793123d890dSSebastian Grimberg   CeedCall(CeedFree(&BTD_mat));
794123d890dSSebastian Grimberg   CeedCall(CeedFree(&elem_mat));
795506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&elem_mat_b));
796506b1a0cSSebastian Grimberg   if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) {
797506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_in, &elem_rstr_orients_in));
798506b1a0cSSebastian Grimberg   } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
799506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_in, &elem_rstr_curl_orients_in));
800506b1a0cSSebastian Grimberg   }
801506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
802506b1a0cSSebastian Grimberg     if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) {
803506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_out, &elem_rstr_orients_out));
804506b1a0cSSebastian Grimberg     } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
805506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_out, &elem_rstr_curl_orients_out));
806506b1a0cSSebastian Grimberg     }
807506b1a0cSSebastian Grimberg   }
8082b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
8092b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
810681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&elem_rstr_in));
811681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&elem_rstr_out));
812eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
813eaf62fffSJeremy L Thompson }
814eaf62fffSJeremy L Thompson 
815eaf62fffSJeremy L Thompson /**
816ca94c3ddSJeremy L Thompson   @brief Count number of entries for assembled `CeedOperator`
817eaf62fffSJeremy L Thompson 
818ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to assemble
819eaf62fffSJeremy L Thompson   @param[out] num_entries Number of entries in assembled representation
820eaf62fffSJeremy L Thompson 
821eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
822eaf62fffSJeremy L Thompson 
823eaf62fffSJeremy L Thompson   @ref Utility
824eaf62fffSJeremy L Thompson **/
825b94338b9SJed Brown static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedSize *num_entries) {
826b275c451SJeremy L Thompson   bool                is_composite;
827506b1a0cSSebastian Grimberg   CeedInt             num_elem_in, elem_size_in, num_comp_in, num_elem_out, elem_size_out, num_comp_out;
828506b1a0cSSebastian Grimberg   CeedElemRestriction rstr_in, rstr_out;
829eaf62fffSJeremy L Thompson 
830b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
8319bc66399SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
832506b1a0cSSebastian Grimberg 
833506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
834506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
835506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
836506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
837506b1a0cSSebastian Grimberg   if (rstr_in != rstr_out) {
838506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
8399bc66399SJeremy L Thompson     CeedCheck(num_elem_in == num_elem_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
8403f08121cSJeremy L Thompson               "Active input and output operator restrictions must have the same number of elements."
8413f08121cSJeremy L Thompson               " Input has %" CeedInt_FMT " elements; output has %" CeedInt_FMT "elements.",
8423f08121cSJeremy L Thompson               num_elem_in, num_elem_out);
843506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
844506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
845506b1a0cSSebastian Grimberg   } else {
846506b1a0cSSebastian Grimberg     num_elem_out  = num_elem_in;
847506b1a0cSSebastian Grimberg     elem_size_out = elem_size_in;
848506b1a0cSSebastian Grimberg     num_comp_out  = num_comp_in;
849506b1a0cSSebastian Grimberg   }
850681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_in));
851681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_out));
852506b1a0cSSebastian Grimberg   *num_entries = (CeedSize)elem_size_in * num_comp_in * elem_size_out * num_comp_out * num_elem_in;
853eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
854eaf62fffSJeremy L Thompson }
855eaf62fffSJeremy L Thompson 
856eaf62fffSJeremy L Thompson /**
857ca94c3ddSJeremy L Thompson   @brief Common code for creating a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator`
858eaf62fffSJeremy L Thompson 
859ca94c3ddSJeremy L Thompson   @param[in]  op_fine      Fine grid `CeedOperator`
860ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
861ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid `CeedElemRestriction`
862ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector `CeedBasis`
863ca94c3ddSJeremy L Thompson   @param[in]  basis_c_to_f `CeedBasis` for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction operators
864ca94c3ddSJeremy L Thompson   @param[out] op_coarse    Coarse grid `CeedOperator`
865ca94c3ddSJeremy L Thompson   @param[out] op_prolong   Coarse to fine `CeedOperator`, or `NULL`
866ca94c3ddSJeremy L Thompson   @param[out] op_restrict  Fine to coarse `CeedOperator`, or `NULL`
867eaf62fffSJeremy L Thompson 
868eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
869eaf62fffSJeremy L Thompson 
870eaf62fffSJeremy L Thompson   @ref Developer
871eaf62fffSJeremy L Thompson **/
8722b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
8737758292fSSebastian Grimberg                                             CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
8741c66c397SJeremy L Thompson   bool                is_composite;
875eaf62fffSJeremy L Thompson   Ceed                ceed;
8761203703bSJeremy L Thompson   CeedInt             num_comp, num_input_fields, num_output_fields;
87785bb9dcfSJeremy L Thompson   CeedVector          mult_vec         = NULL;
8781c66c397SJeremy L Thompson   CeedElemRestriction rstr_p_mult_fine = NULL, rstr_fine = NULL;
8791203703bSJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
8801c66c397SJeremy L Thompson 
8812b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
882eaf62fffSJeremy L Thompson 
883eaf62fffSJeremy L Thompson   // Check for composite operator
8842b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op_fine, &is_composite));
8856574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported");
886eaf62fffSJeremy L Thompson 
887eaf62fffSJeremy L Thompson   // Coarse Grid
88899641342SJeremy L Thompson   {
88999641342SJeremy L Thompson     bool is_at_points;
89099641342SJeremy L Thompson 
89199641342SJeremy L Thompson     CeedCall(CeedOperatorIsAtPoints(op_fine, &is_at_points));
89299641342SJeremy L Thompson     if (is_at_points) {
89399641342SJeremy L Thompson       CeedVector          point_coords;
89499641342SJeremy L Thompson       CeedElemRestriction rstr_points;
89599641342SJeremy L Thompson 
89699641342SJeremy L Thompson       CeedCall(CeedOperatorCreateAtPoints(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
89799641342SJeremy L Thompson       CeedCall(CeedOperatorAtPointsGetPoints(op_fine, &rstr_points, &point_coords));
89899641342SJeremy L Thompson       CeedCall(CeedOperatorAtPointsSetPoints(*op_coarse, rstr_points, point_coords));
89999641342SJeremy L Thompson       CeedCall(CeedVectorDestroy(&point_coords));
90099641342SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&rstr_points));
90199641342SJeremy L Thompson     } else {
9022b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
90399641342SJeremy L Thompson     }
90499641342SJeremy L Thompson   }
9051203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op_fine, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
906eaf62fffSJeremy L Thompson   // -- Clone input fields
9071203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
9086f8994e9SJeremy L Thompson     const char         *field_name;
9091203703bSJeremy L Thompson     CeedVector          vec;
910681d0ea7SJeremy L Thompson     CeedElemRestriction rstr  = NULL;
911681d0ea7SJeremy L Thompson     CeedBasis           basis = NULL;
9121203703bSJeremy L Thompson 
9131203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetName(input_fields[i], &field_name));
9141203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(input_fields[i], &vec));
9151203703bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
916681d0ea7SJeremy L Thompson       CeedCall(CeedElemRestrictionReferenceCopy(rstr_coarse, &rstr));
917681d0ea7SJeremy L Thompson       CeedCall(CeedBasisReferenceCopy(basis_coarse, &basis));
918681d0ea7SJeremy L Thompson       if (!rstr_fine) CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_fine));
919eaf62fffSJeremy L Thompson     } else {
9201203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr));
9211203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis));
922eaf62fffSJeremy L Thompson     }
9231203703bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec));
924681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
925681d0ea7SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr));
926681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis));
927eaf62fffSJeremy L Thompson   }
928eaf62fffSJeremy L Thompson   // -- Clone output fields
9291203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
9306f8994e9SJeremy L Thompson     const char         *field_name;
9311203703bSJeremy L Thompson     CeedVector          vec;
932681d0ea7SJeremy L Thompson     CeedElemRestriction rstr  = NULL;
933681d0ea7SJeremy L Thompson     CeedBasis           basis = NULL;
9341203703bSJeremy L Thompson 
9351203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetName(output_fields[i], &field_name));
9361203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
9371203703bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
938681d0ea7SJeremy L Thompson       CeedCall(CeedElemRestrictionReferenceCopy(rstr_coarse, &rstr));
939681d0ea7SJeremy L Thompson       CeedCall(CeedBasisReferenceCopy(basis_coarse, &basis));
940681d0ea7SJeremy L Thompson       if (!rstr_fine) CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_fine));
941eaf62fffSJeremy L Thompson     } else {
9421203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr));
9431203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis));
944eaf62fffSJeremy L Thompson     }
9451203703bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec));
946681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
947681d0ea7SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr));
948681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis));
949eaf62fffSJeremy L Thompson   }
950af99e877SJeremy L Thompson   // -- Clone QFunctionAssemblyData
9517d5185d7SSebastian Grimberg   {
9527d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData fine_data;
9537d5185d7SSebastian Grimberg 
9547d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op_fine, &fine_data));
9557d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataReferenceCopy(fine_data, &(*op_coarse)->qf_assembled));
9567d5185d7SSebastian Grimberg   }
957eaf62fffSJeremy L Thompson 
958eaf62fffSJeremy L Thompson   // Multiplicity vector
9597758292fSSebastian Grimberg   if (op_restrict || op_prolong) {
96085bb9dcfSJeremy L Thompson     CeedVector          mult_e_vec;
9611c66c397SJeremy L Thompson     CeedRestrictionType rstr_type;
96285bb9dcfSJeremy L Thompson 
9637c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr_fine, &rstr_type));
9647c1dbaffSSebastian Grimberg     CeedCheck(rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_UNSUPPORTED,
9657c1dbaffSSebastian Grimberg               "Element restrictions created with CeedElemRestrictionCreateCurlOriented are not supported");
9666574a04fSJeremy L Thompson     CeedCheck(p_mult_fine, ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector");
9677c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnsignedCopy(rstr_fine, &rstr_p_mult_fine));
9682b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec));
9692b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_e_vec, 0.0));
970c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE));
9712b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_vec, 0.0));
972c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE));
9732b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&mult_e_vec));
9742b730f8bSJeremy L Thompson     CeedCall(CeedVectorReciprocal(mult_vec));
97585bb9dcfSJeremy L Thompson   }
976eaf62fffSJeremy L Thompson 
977addd79feSZach Atkins   // Clone name
978addd79feSZach Atkins   bool   has_name = op_fine->name;
979addd79feSZach Atkins   size_t name_len = op_fine->name ? strlen(op_fine->name) : 0;
980addd79feSZach Atkins   CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name));
981addd79feSZach Atkins 
9827758292fSSebastian Grimberg   // Check that coarse to fine basis is provided if prolong/restrict operators are requested
9837758292fSSebastian Grimberg   CeedCheck(basis_c_to_f || (!op_restrict && !op_prolong), ceed, CEED_ERROR_INCOMPATIBLE,
9846574a04fSJeremy L Thompson             "Prolongation or restriction operator creation requires coarse-to-fine basis");
98583d6adf3SZach Atkins 
98685bb9dcfSJeremy L Thompson   // Restriction/Prolongation Operators
9872b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp));
988addd79feSZach Atkins 
989addd79feSZach Atkins   // Restriction
9907758292fSSebastian Grimberg   if (op_restrict) {
991eaf62fffSJeremy L Thompson     CeedInt             *num_comp_r_data;
99285bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_r;
9937758292fSSebastian Grimberg     CeedQFunction        qf_restrict;
99485bb9dcfSJeremy L Thompson 
9957758292fSSebastian Grimberg     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict));
9962b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_r_data));
997eaf62fffSJeremy L Thompson     num_comp_r_data[0] = num_comp;
9982b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r));
9992b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data));
10007758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r));
10012b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_r));
10027758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE));
10037758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE));
10047758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP));
10057758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp));
1006eaf62fffSJeremy L Thompson 
10077758292fSSebastian Grimberg     CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict));
10087758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
10097758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
10107758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
1011eaf62fffSJeremy L Thompson 
1012addd79feSZach Atkins     // Set name
1013addd79feSZach Atkins     char *restriction_name;
10141c66c397SJeremy L Thompson 
1015addd79feSZach Atkins     CeedCall(CeedCalloc(17 + name_len, &restriction_name));
1016addd79feSZach Atkins     sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
10177758292fSSebastian Grimberg     CeedCall(CeedOperatorSetName(*op_restrict, restriction_name));
1018addd79feSZach Atkins     CeedCall(CeedFree(&restriction_name));
1019addd79feSZach Atkins 
1020addd79feSZach Atkins     // Check
10217758292fSSebastian Grimberg     CeedCall(CeedOperatorCheckReady(*op_restrict));
1022addd79feSZach Atkins 
1023addd79feSZach Atkins     // Cleanup
10247758292fSSebastian Grimberg     CeedCall(CeedQFunctionDestroy(&qf_restrict));
1025addd79feSZach Atkins   }
1026addd79feSZach Atkins 
1027eaf62fffSJeremy L Thompson   // Prolongation
1028addd79feSZach Atkins   if (op_prolong) {
1029eaf62fffSJeremy L Thompson     CeedInt             *num_comp_p_data;
103085bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_p;
10311c66c397SJeremy L Thompson     CeedQFunction        qf_prolong;
103285bb9dcfSJeremy L Thompson 
103385bb9dcfSJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong));
10342b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_p_data));
1035eaf62fffSJeremy L Thompson     num_comp_p_data[0] = num_comp;
10362b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p));
10372b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data));
10382b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p));
10392b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_p));
10402b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP));
10412b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE));
10422b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE));
10432b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp));
1044eaf62fffSJeremy L Thompson 
10452b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong));
10462b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
1047356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
1048356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
1049eaf62fffSJeremy L Thompson 
1050addd79feSZach Atkins     // Set name
1051ea6b5821SJeremy L Thompson     char *prolongation_name;
10521c66c397SJeremy L Thompson 
10532b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(18 + name_len, &prolongation_name));
10542b730f8bSJeremy L Thompson     sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
10552b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name));
10562b730f8bSJeremy L Thompson     CeedCall(CeedFree(&prolongation_name));
1057addd79feSZach Atkins 
1058addd79feSZach Atkins     // Check
1059addd79feSZach Atkins     CeedCall(CeedOperatorCheckReady(*op_prolong));
1060addd79feSZach Atkins 
1061addd79feSZach Atkins     // Cleanup
1062addd79feSZach Atkins     CeedCall(CeedQFunctionDestroy(&qf_prolong));
1063ea6b5821SJeremy L Thompson   }
1064ea6b5821SJeremy L Thompson 
106558e4b056SJeremy L Thompson   // Check
106658e4b056SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(*op_coarse));
106758e4b056SJeremy L Thompson 
1068eaf62fffSJeremy L Thompson   // Cleanup
10699bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
10702b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_vec));
1071681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_fine));
1072c17ec2beSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine));
10732b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis_c_to_f));
1074eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1075eaf62fffSJeremy L Thompson }
1076eaf62fffSJeremy L Thompson 
1077eaf62fffSJeremy L Thompson /**
1078eaf62fffSJeremy L Thompson   @brief Build 1D mass matrix and Laplacian with perturbation
1079eaf62fffSJeremy L Thompson 
1080eaf62fffSJeremy L Thompson   @param[in]  interp_1d   Interpolation matrix in one dimension
1081eaf62fffSJeremy L Thompson   @param[in]  grad_1d     Gradient matrix in one dimension
1082eaf62fffSJeremy L Thompson   @param[in]  q_weight_1d Quadrature weights in one dimension
1083eaf62fffSJeremy L Thompson   @param[in]  P_1d        Number of basis nodes in one dimension
1084eaf62fffSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
1085eaf62fffSJeremy L Thompson   @param[in]  dim         Dimension of basis
1086eaf62fffSJeremy L Thompson   @param[out] mass        Assembled mass matrix in one dimension
1087eaf62fffSJeremy L Thompson   @param[out] laplace     Assembled perturbed Laplacian in one dimension
1088eaf62fffSJeremy L Thompson 
1089eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1090eaf62fffSJeremy L Thompson 
1091eaf62fffSJeremy L Thompson   @ref Developer
1092eaf62fffSJeremy L Thompson **/
10932c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
10942c2ea1dbSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d, CeedInt P_1d, CeedInt Q_1d,
10952c2ea1dbSJeremy L Thompson                                 CeedInt dim, CeedScalar *mass, CeedScalar *laplace) {
10962b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
1097eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
1098eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
10992b730f8bSJeremy 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];
1100eaf62fffSJeremy L Thompson       mass[i + j * P_1d] = sum;
1101eaf62fffSJeremy L Thompson     }
11022b730f8bSJeremy L Thompson   }
1103eaf62fffSJeremy L Thompson   // -- Laplacian
11042b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
1105eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
1106eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
11071c66c397SJeremy L Thompson 
11082b730f8bSJeremy 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];
1109eaf62fffSJeremy L Thompson       laplace[i + j * P_1d] = sum;
1110eaf62fffSJeremy L Thompson     }
11112b730f8bSJeremy L Thompson   }
1112eaf62fffSJeremy L Thompson   CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4;
11132b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation;
1114eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1115eaf62fffSJeremy L Thompson }
11162c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
1117eaf62fffSJeremy L Thompson 
1118eaf62fffSJeremy L Thompson /// @}
1119eaf62fffSJeremy L Thompson 
1120eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1121480fae85SJeremy L Thompson /// CeedOperator Backend API
1122480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1123480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend
1124480fae85SJeremy L Thompson /// @{
1125480fae85SJeremy L Thompson 
1126480fae85SJeremy L Thompson /**
1127004e4986SSebastian Grimberg   @brief Select correct basis matrix pointer based on @ref CeedEvalMode
1128004e4986SSebastian Grimberg 
1129004e4986SSebastian Grimberg   @param[in]  basis     `CeedBasis` from which to get the basis matrix
1130004e4986SSebastian Grimberg   @param[in]  eval_mode Current basis evaluation mode
1131004e4986SSebastian Grimberg   @param[in]  identity  Pointer to identity matrix
1132004e4986SSebastian Grimberg   @param[out] basis_ptr `CeedBasis` pointer to set
1133004e4986SSebastian Grimberg 
1134004e4986SSebastian Grimberg   @ref Backend
1135004e4986SSebastian Grimberg **/
1136004e4986SSebastian Grimberg int CeedOperatorGetBasisPointer(CeedBasis basis, CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar **basis_ptr) {
1137004e4986SSebastian Grimberg   switch (eval_mode) {
1138004e4986SSebastian Grimberg     case CEED_EVAL_NONE:
1139004e4986SSebastian Grimberg       *basis_ptr = identity;
1140004e4986SSebastian Grimberg       break;
1141004e4986SSebastian Grimberg     case CEED_EVAL_INTERP:
1142004e4986SSebastian Grimberg       CeedCall(CeedBasisGetInterp(basis, basis_ptr));
1143004e4986SSebastian Grimberg       break;
1144004e4986SSebastian Grimberg     case CEED_EVAL_GRAD:
1145004e4986SSebastian Grimberg       CeedCall(CeedBasisGetGrad(basis, basis_ptr));
1146004e4986SSebastian Grimberg       break;
1147004e4986SSebastian Grimberg     case CEED_EVAL_DIV:
1148004e4986SSebastian Grimberg       CeedCall(CeedBasisGetDiv(basis, basis_ptr));
1149004e4986SSebastian Grimberg       break;
1150004e4986SSebastian Grimberg     case CEED_EVAL_CURL:
1151004e4986SSebastian Grimberg       CeedCall(CeedBasisGetCurl(basis, basis_ptr));
1152004e4986SSebastian Grimberg       break;
1153004e4986SSebastian Grimberg     case CEED_EVAL_WEIGHT:
1154004e4986SSebastian Grimberg       break;  // Caught by QF Assembly
1155004e4986SSebastian Grimberg   }
1156004e4986SSebastian Grimberg   assert(*basis_ptr != NULL);
1157004e4986SSebastian Grimberg   return CEED_ERROR_SUCCESS;
1158004e4986SSebastian Grimberg }
1159004e4986SSebastian Grimberg 
1160004e4986SSebastian Grimberg /**
1161ca94c3ddSJeremy L Thompson   @brief Create point block restriction for active `CeedOperatorField`
1162506b1a0cSSebastian Grimberg 
1163ca94c3ddSJeremy L Thompson   @param[in]  rstr             Original `CeedElemRestriction` for active field
1164ca94c3ddSJeremy L Thompson   @param[out] point_block_rstr Address of the variable where the newly created `CeedElemRestriction` will be stored
1165506b1a0cSSebastian Grimberg 
1166506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1167506b1a0cSSebastian Grimberg 
1168506b1a0cSSebastian Grimberg   @ref Backend
1169506b1a0cSSebastian Grimberg **/
1170506b1a0cSSebastian Grimberg int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *point_block_rstr) {
1171506b1a0cSSebastian Grimberg   Ceed           ceed;
1172506b1a0cSSebastian Grimberg   CeedInt        num_elem, num_comp, shift, elem_size, comp_stride, *point_block_offsets;
1173506b1a0cSSebastian Grimberg   CeedSize       l_size;
1174506b1a0cSSebastian Grimberg   const CeedInt *offsets;
1175506b1a0cSSebastian Grimberg 
1176506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
1177506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
1178506b1a0cSSebastian Grimberg 
1179506b1a0cSSebastian Grimberg   // Expand offsets
1180506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
1181506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1182506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
1183506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
1184506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
1185506b1a0cSSebastian Grimberg   shift = num_comp;
1186506b1a0cSSebastian Grimberg   if (comp_stride != 1) shift *= num_comp;
1187506b1a0cSSebastian Grimberg   CeedCall(CeedCalloc(num_elem * elem_size, &point_block_offsets));
1188506b1a0cSSebastian Grimberg   for (CeedInt i = 0; i < num_elem * elem_size; i++) {
1189506b1a0cSSebastian Grimberg     point_block_offsets[i] = offsets[i] * shift;
1190506b1a0cSSebastian Grimberg   }
1191506b1a0cSSebastian Grimberg 
1192506b1a0cSSebastian Grimberg   // Create new restriction
1193506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
1194506b1a0cSSebastian Grimberg                                      point_block_offsets, point_block_rstr));
1195506b1a0cSSebastian Grimberg 
1196506b1a0cSSebastian Grimberg   // Cleanup
1197506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
11989bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
1199506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1200506b1a0cSSebastian Grimberg }
1201506b1a0cSSebastian Grimberg 
1202506b1a0cSSebastian Grimberg /**
12037d5185d7SSebastian Grimberg   @brief Get `CeedQFunctionAssemblyData`
12047d5185d7SSebastian Grimberg 
12057d5185d7SSebastian Grimberg   @param[in]  op   `CeedOperator` to assemble
12067d5185d7SSebastian Grimberg   @param[out] data `CeedQFunctionAssemblyData`
12077d5185d7SSebastian Grimberg 
12087d5185d7SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
12097d5185d7SSebastian Grimberg 
12107d5185d7SSebastian Grimberg   @ref Backend
12117d5185d7SSebastian Grimberg **/
12127d5185d7SSebastian Grimberg int CeedOperatorGetQFunctionAssemblyData(CeedOperator op, CeedQFunctionAssemblyData *data) {
12137d5185d7SSebastian Grimberg   if (!op->qf_assembled) {
12147d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
12157d5185d7SSebastian Grimberg 
12167d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataCreate(op->ceed, &data));
12177d5185d7SSebastian Grimberg     op->qf_assembled = data;
12187d5185d7SSebastian Grimberg   }
12197d5185d7SSebastian Grimberg   *data = op->qf_assembled;
12207d5185d7SSebastian Grimberg   return CEED_ERROR_SUCCESS;
12217d5185d7SSebastian Grimberg }
12227d5185d7SSebastian Grimberg 
12237d5185d7SSebastian Grimberg /**
1224ca94c3ddSJeremy L Thompson   @brief Create object holding `CeedQFunction` assembly data for `CeedOperator`
1225480fae85SJeremy L Thompson 
1226ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedQFunctionAssemblyData`
1227ca94c3ddSJeremy L Thompson   @param[out] data Address of the variable where the newly created `CeedQFunctionAssemblyData` will be stored
1228480fae85SJeremy L Thompson 
1229480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1230480fae85SJeremy L Thompson 
1231480fae85SJeremy L Thompson   @ref Backend
1232480fae85SJeremy L Thompson **/
1233ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) {
12342b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1235480fae85SJeremy L Thompson   (*data)->ref_count = 1;
1236480fae85SJeremy L Thompson   (*data)->ceed      = ceed;
12372b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1238480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1239480fae85SJeremy L Thompson }
1240480fae85SJeremy L Thompson 
1241480fae85SJeremy L Thompson /**
1242ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedQFunctionAssemblyData`
1243480fae85SJeremy L Thompson 
1244ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to increment the reference counter
1245480fae85SJeremy L Thompson 
1246480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1247480fae85SJeremy L Thompson 
1248480fae85SJeremy L Thompson   @ref Backend
1249480fae85SJeremy L Thompson **/
1250480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) {
1251480fae85SJeremy L Thompson   data->ref_count++;
1252480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1253480fae85SJeremy L Thompson }
1254480fae85SJeremy L Thompson 
1255480fae85SJeremy L Thompson /**
1256ca94c3ddSJeremy L Thompson   @brief Set re-use of `CeedQFunctionAssemblyData`
12578b919e6bSJeremy L Thompson 
1258ca94c3ddSJeremy L Thompson   @param[in,out] data       `CeedQFunctionAssemblyData` to mark for reuse
1259ea61e9acSJeremy L Thompson   @param[in]     reuse_data Boolean flag indicating data re-use
12608b919e6bSJeremy L Thompson 
12618b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12628b919e6bSJeremy L Thompson 
12638b919e6bSJeremy L Thompson   @ref Backend
12648b919e6bSJeremy L Thompson **/
12652b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) {
1266beecbf24SJeremy L Thompson   data->reuse_data        = reuse_data;
1267beecbf24SJeremy L Thompson   data->needs_data_update = true;
1268beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1269beecbf24SJeremy L Thompson }
1270beecbf24SJeremy L Thompson 
1271beecbf24SJeremy L Thompson /**
1272ca94c3ddSJeremy L Thompson   @brief Mark `CeedQFunctionAssemblyData` as stale
1273beecbf24SJeremy L Thompson 
1274ca94c3ddSJeremy L Thompson   @param[in,out] data              `CeedQFunctionAssemblyData` to mark as stale
1275ea61e9acSJeremy L Thompson   @param[in]     needs_data_update Boolean flag indicating if update is needed or completed
1276beecbf24SJeremy L Thompson 
1277beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1278beecbf24SJeremy L Thompson 
1279beecbf24SJeremy L Thompson   @ref Backend
1280beecbf24SJeremy L Thompson **/
12812b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) {
1282beecbf24SJeremy L Thompson   data->needs_data_update = needs_data_update;
12838b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12848b919e6bSJeremy L Thompson }
12858b919e6bSJeremy L Thompson 
12868b919e6bSJeremy L Thompson /**
1287ca94c3ddSJeremy L Thompson   @brief Determine if `CeedQFunctionAssemblyData` needs update
12888b919e6bSJeremy L Thompson 
1289ca94c3ddSJeremy L Thompson   @param[in]  data             `CeedQFunctionAssemblyData` to mark as stale
12908b919e6bSJeremy L Thompson   @param[out] is_update_needed Boolean flag indicating if re-assembly is required
12918b919e6bSJeremy L Thompson 
12928b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12938b919e6bSJeremy L Thompson 
12948b919e6bSJeremy L Thompson   @ref Backend
12958b919e6bSJeremy L Thompson **/
12962b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) {
1297beecbf24SJeremy L Thompson   *is_update_needed = !data->reuse_data || data->needs_data_update;
12988b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12998b919e6bSJeremy L Thompson }
13008b919e6bSJeremy L Thompson 
13018b919e6bSJeremy L Thompson /**
1302ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedQFunctionAssemblyData`.
13034385fb7fSSebastian Grimberg 
1304ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedQFunctionAssemblyDataDestroy().
1305512bb800SJeremy L Thompson 
1306ca94c3ddSJeremy 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`.
1307ca94c3ddSJeremy L Thompson         This `CeedQFunctionAssemblyData` will be destroyed if ` *data_copy` is the only reference to this `CeedQFunctionAssemblyData`.
1308480fae85SJeremy L Thompson 
1309ca94c3ddSJeremy L Thompson   @param[in]     data      `CeedQFunctionAssemblyData` to copy reference to
1310ea61e9acSJeremy L Thompson   @param[in,out] data_copy Variable to store copied reference
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 CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) {
13172b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReference(data));
13182b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy));
1319480fae85SJeremy L Thompson   *data_copy = data;
1320480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1321480fae85SJeremy L Thompson }
1322480fae85SJeremy L Thompson 
1323480fae85SJeremy L Thompson /**
1324ca94c3ddSJeremy L Thompson   @brief Get setup status for internal objects for `CeedQFunctionAssemblyData`
1325480fae85SJeremy L Thompson 
1326ca94c3ddSJeremy L Thompson   @param[in]  data     `CeedQFunctionAssemblyData` to retrieve status
1327480fae85SJeremy L Thompson   @param[out] is_setup Boolean flag for setup status
1328480fae85SJeremy L Thompson 
1329480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1330480fae85SJeremy L Thompson 
1331480fae85SJeremy L Thompson   @ref Backend
1332480fae85SJeremy L Thompson **/
13332b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) {
1334480fae85SJeremy L Thompson   *is_setup = data->is_setup;
1335480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1336480fae85SJeremy L Thompson }
1337480fae85SJeremy L Thompson 
1338480fae85SJeremy L Thompson /**
1339ca94c3ddSJeremy L Thompson   @brief Set internal objects for `CeedQFunctionAssemblyData`
1340480fae85SJeremy L Thompson 
1341ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to set objects
1342ca94c3ddSJeremy L Thompson   @param[in]     vec  `CeedVector` to store assembled `CeedQFunction` at quadrature points
1343ca94c3ddSJeremy L Thompson   @param[in]     rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1344480fae85SJeremy L Thompson 
1345480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1346480fae85SJeremy L Thompson 
1347480fae85SJeremy L Thompson   @ref Backend
1348480fae85SJeremy L Thompson **/
13492b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) {
13502b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(vec, &data->vec));
13512b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr));
1352480fae85SJeremy L Thompson 
1353480fae85SJeremy L Thompson   data->is_setup = true;
1354480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1355480fae85SJeremy L Thompson }
1356480fae85SJeremy L Thompson 
13574dd1a9d2SSebastian Grimberg /**
1358ca94c3ddSJeremy L Thompson   @brief Get internal objects for `CeedQFunctionAssemblyData`
13594dd1a9d2SSebastian Grimberg 
1360ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to set objects
1361ca94c3ddSJeremy L Thompson   @param[out]    vec  `CeedVector` to store assembled `CeedQFunction` at quadrature points
1362ca94c3ddSJeremy L Thompson   @param[out]    rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
13634dd1a9d2SSebastian Grimberg 
13644dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
13654dd1a9d2SSebastian Grimberg 
13664dd1a9d2SSebastian Grimberg   @ref Backend
13674dd1a9d2SSebastian Grimberg **/
13682b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) {
13696574a04fSJeremy L Thompson   CeedCheck(data->is_setup, data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first.");
1370480fae85SJeremy L Thompson 
13712b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(data->vec, vec));
13722b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr));
1373480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1374480fae85SJeremy L Thompson }
1375480fae85SJeremy L Thompson 
1376480fae85SJeremy L Thompson /**
1377ca94c3ddSJeremy L Thompson   @brief Destroy `CeedQFunctionAssemblyData`
1378480fae85SJeremy L Thompson 
1379ca94c3ddSJeremy L Thompson   @param[in,out] data  `CeedQFunctionAssemblyData` to destroy
1380480fae85SJeremy L Thompson 
1381480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1382480fae85SJeremy L Thompson 
1383480fae85SJeremy L Thompson   @ref Backend
1384480fae85SJeremy L Thompson **/
1385480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) {
1386ad6481ceSJeremy L Thompson   if (!*data || --(*data)->ref_count > 0) {
1387ad6481ceSJeremy L Thompson     *data = NULL;
1388ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1389ad6481ceSJeremy L Thompson   }
13902b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
13912b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*data)->vec));
13922b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr));
1393480fae85SJeremy L Thompson 
13942b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1395480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1396480fae85SJeremy L Thompson }
1397480fae85SJeremy L Thompson 
1398ed9e99e6SJeremy L Thompson /**
1399ca94c3ddSJeremy L Thompson   @brief Get `CeedOperatorAssemblyData`
1400ed9e99e6SJeremy L Thompson 
1401ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator` to assemble
14027d5185d7SSebastian Grimberg   @param[out] data `CeedOperatorAssemblyData`
1403ed9e99e6SJeremy L Thompson 
1404ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1405ed9e99e6SJeremy L Thompson 
1406ed9e99e6SJeremy L Thompson   @ref Backend
1407ed9e99e6SJeremy L Thompson **/
14082b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) {
1409ed9e99e6SJeremy L Thompson   if (!op->op_assembled) {
1410ed9e99e6SJeremy L Thompson     CeedOperatorAssemblyData data;
1411ed9e99e6SJeremy L Thompson 
14122b730f8bSJeremy L Thompson     CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data));
1413ed9e99e6SJeremy L Thompson     op->op_assembled = data;
1414ed9e99e6SJeremy L Thompson   }
1415ed9e99e6SJeremy L Thompson   *data = op->op_assembled;
1416ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1417ed9e99e6SJeremy L Thompson }
1418ed9e99e6SJeremy L Thompson 
1419ed9e99e6SJeremy L Thompson /**
1420ca94c3ddSJeremy L Thompson   @brief Create object holding `CeedOperator` assembly data.
1421ba746a46SJeremy L Thompson 
1422ca94c3ddSJeremy L Thompson   The `CeedOperatorAssemblyData` holds an array with references to every active `CeedBasis` used in the `CeedOperator`.
1423ca94c3ddSJeremy L Thompson   An array with references to the corresponding active `CeedElemRestriction` is also stored.
1424ca94c3ddSJeremy L Thompson   For each active `CeedBasis, the `CeedOperatorAssemblyData` holds an array of all input and output @ref CeedEvalMode for this `CeedBasis`.
1425ca94c3ddSJeremy L Thompson   The `CeedOperatorAssemblyData` holds an array of offsets for indexing into the assembled `CeedQFunction` arrays to the row representing each @ref CeedEvalMode.
1426ca94c3ddSJeremy L Thompson   The number of input columns across all active bases for the assembled `CeedQFunction` is also stored.
1427ca94c3ddSJeremy L Thompson   Lastly, the `CeedOperatorAssembly` data holds assembled matrices representing the full action of the `CeedBasis` for all @ref CeedEvalMode.
1428ed9e99e6SJeremy L Thompson 
1429ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperatorAssemblyData`
1430ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator` to be assembled
1431ca94c3ddSJeremy L Thompson   @param[out] data Address of the variable where the newly created `CeedOperatorAssemblyData` will be stored
1432ed9e99e6SJeremy L Thompson 
1433ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1434ed9e99e6SJeremy L Thompson 
1435ed9e99e6SJeremy L Thompson   @ref Backend
1436ed9e99e6SJeremy L Thompson **/
14372b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) {
1438506b1a0cSSebastian Grimberg   CeedInt             num_active_bases_in = 0, num_active_bases_out = 0, offset = 0;
1439506b1a0cSSebastian Grimberg   CeedInt             num_input_fields, *num_eval_modes_in = NULL, num_output_fields, *num_eval_modes_out = NULL;
14401c66c397SJeremy L Thompson   CeedSize          **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL;
14411c66c397SJeremy L Thompson   CeedEvalMode      **eval_modes_in = NULL, **eval_modes_out = NULL;
14421c66c397SJeremy L Thompson   CeedQFunctionField *qf_fields;
14431c66c397SJeremy L Thompson   CeedQFunction       qf;
14441c66c397SJeremy L Thompson   CeedOperatorField  *op_fields;
144501f0e615SJames Wright   bool                is_composite;
144601f0e615SJames Wright 
144701f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
144801f0e615SJames Wright   CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Can only create CeedOperator assembly data for non-composite operators.");
1449437c7c90SJeremy L Thompson 
1450437c7c90SJeremy L Thompson   // Allocate
14512b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1452ed9e99e6SJeremy L Thompson   (*data)->ceed = ceed;
14532b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1454ed9e99e6SJeremy L Thompson 
1455ed9e99e6SJeremy L Thompson   // Build OperatorAssembly data
14562b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
1457ed9e99e6SJeremy L Thompson 
1458ed9e99e6SJeremy L Thompson   // Determine active input basis
1459004e4986SSebastian Grimberg   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL));
1460004e4986SSebastian Grimberg   CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1461ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1462ed9e99e6SJeremy L Thompson     CeedVector vec;
14631c66c397SJeremy L Thompson 
14642b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1465ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
14667c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
14671c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
14681c66c397SJeremy L Thompson       CeedBasis    basis_in = NULL;
14691c66c397SJeremy L Thompson 
14702b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
14712b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1472352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp));
1473352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1474506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_active_bases_in; i++) {
1475506b1a0cSSebastian Grimberg         if ((*data)->active_bases_in[i] == basis_in) index = i;
1476437c7c90SJeremy L Thompson       }
1477437c7c90SJeremy L Thompson       if (index == -1) {
1478437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_in;
14791c66c397SJeremy L Thompson 
1480506b1a0cSSebastian Grimberg         index = num_active_bases_in;
1481506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_bases_in));
1482506b1a0cSSebastian Grimberg         (*data)->active_bases_in[num_active_bases_in] = NULL;
1483506b1a0cSSebastian Grimberg         CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases_in[num_active_bases_in]));
1484506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_elem_rstrs_in));
1485506b1a0cSSebastian Grimberg         (*data)->active_elem_rstrs_in[num_active_bases_in] = NULL;
1486437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in));
1487506b1a0cSSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs_in[num_active_bases_in]));
1488681d0ea7SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&elem_rstr_in));
1489506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &num_eval_modes_in));
1490437c7c90SJeremy L Thompson         num_eval_modes_in[index] = 0;
1491506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_modes_in));
1492437c7c90SJeremy L Thompson         eval_modes_in[index] = NULL;
1493506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_mode_offsets_in));
1494437c7c90SJeremy L Thompson         eval_mode_offsets_in[index] = NULL;
1495506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->assembled_bases_in));
1496437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index] = NULL;
1497506b1a0cSSebastian Grimberg         num_active_bases_in++;
1498437c7c90SJeremy L Thompson       }
1499352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1500352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1501352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_modes_in[index]));
1502352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_mode_offsets_in[index]));
1503352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1504437c7c90SJeremy L Thompson           eval_modes_in[index][num_eval_modes_in[index] + d]        = eval_mode;
1505437c7c90SJeremy L Thompson           eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset;
1506352a5e7cSSebastian Grimberg           offset += num_comp;
1507ed9e99e6SJeremy L Thompson         }
1508352a5e7cSSebastian Grimberg         num_eval_modes_in[index] += q_comp;
1509ed9e99e6SJeremy L Thompson       }
1510681d0ea7SJeremy L Thompson       CeedCall(CeedBasisDestroy(&basis_in));
1511ed9e99e6SJeremy L Thompson     }
1512681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
1513ed9e99e6SJeremy L Thompson   }
1514ed9e99e6SJeremy L Thompson 
1515ed9e99e6SJeremy L Thompson   // Determine active output basis
15162b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields));
15172b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1518437c7c90SJeremy L Thompson   offset = 0;
1519ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1520ed9e99e6SJeremy L Thompson     CeedVector vec;
15211c66c397SJeremy L Thompson 
15222b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1523ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
15247c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
15251c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
15261c66c397SJeremy L Thompson       CeedBasis    basis_out = NULL;
15271c66c397SJeremy L Thompson 
1528437c7c90SJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
15292b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1530352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_out, &num_comp));
1531352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1532506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_active_bases_out; i++) {
1533506b1a0cSSebastian Grimberg         if ((*data)->active_bases_out[i] == basis_out) index = i;
1534437c7c90SJeremy L Thompson       }
1535437c7c90SJeremy L Thompson       if (index == -1) {
1536437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_out;
15371c66c397SJeremy L Thompson 
1538506b1a0cSSebastian Grimberg         index = num_active_bases_out;
1539506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_bases_out));
1540506b1a0cSSebastian Grimberg         (*data)->active_bases_out[num_active_bases_out] = NULL;
1541506b1a0cSSebastian Grimberg         CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases_out[num_active_bases_out]));
1542506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_elem_rstrs_out));
1543506b1a0cSSebastian Grimberg         (*data)->active_elem_rstrs_out[num_active_bases_out] = NULL;
1544437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out));
1545506b1a0cSSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs_out[num_active_bases_out]));
1546681d0ea7SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&elem_rstr_out));
1547506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &num_eval_modes_out));
1548437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1549506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_modes_out));
1550437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1551506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_mode_offsets_out));
1552437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1553506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->assembled_bases_out));
1554437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1555506b1a0cSSebastian Grimberg         num_active_bases_out++;
1556437c7c90SJeremy L Thompson       }
1557352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1558352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1559352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_modes_out[index]));
1560352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_mode_offsets_out[index]));
1561352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1562437c7c90SJeremy L Thompson           eval_modes_out[index][num_eval_modes_out[index] + d]        = eval_mode;
1563437c7c90SJeremy L Thompson           eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset;
1564352a5e7cSSebastian Grimberg           offset += num_comp;
1565ed9e99e6SJeremy L Thompson         }
1566352a5e7cSSebastian Grimberg         num_eval_modes_out[index] += q_comp;
1567ed9e99e6SJeremy L Thompson       }
1568681d0ea7SJeremy L Thompson       CeedCall(CeedBasisDestroy(&basis_out));
1569ed9e99e6SJeremy L Thompson     }
1570681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
1571ed9e99e6SJeremy L Thompson   }
1572c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
1573506b1a0cSSebastian Grimberg   (*data)->num_active_bases_in   = num_active_bases_in;
157427789c4aSJed Brown   (*data)->num_eval_modes_in     = num_eval_modes_in;
157527789c4aSJed Brown   (*data)->eval_modes_in         = eval_modes_in;
157627789c4aSJed Brown   (*data)->eval_mode_offsets_in  = eval_mode_offsets_in;
1577506b1a0cSSebastian Grimberg   (*data)->num_active_bases_out  = num_active_bases_out;
1578437c7c90SJeremy L Thompson   (*data)->num_eval_modes_out    = num_eval_modes_out;
1579437c7c90SJeremy L Thompson   (*data)->eval_modes_out        = eval_modes_out;
1580437c7c90SJeremy L Thompson   (*data)->eval_mode_offsets_out = eval_mode_offsets_out;
1581506b1a0cSSebastian Grimberg   (*data)->num_output_components = offset;
1582ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1583ed9e99e6SJeremy L Thompson }
1584ed9e99e6SJeremy L Thompson 
1585ed9e99e6SJeremy L Thompson /**
1586ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` @ref CeedEvalMode for assembly.
1587ba746a46SJeremy L Thompson 
1588ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1589ed9e99e6SJeremy L Thompson 
1590ca94c3ddSJeremy L Thompson   @param[in]  data                  `CeedOperatorAssemblyData`
1591506b1a0cSSebastian Grimberg   @param[out] num_active_bases_in   Total number of active bases for input
1592ca94c3ddSJeremy L Thompson   @param[out] num_eval_modes_in     Pointer to hold array of numbers of input @ref CeedEvalMode, or `NULL`.
1593ca94c3ddSJeremy L Thompson                                       `eval_modes_in[0]` holds an array of eval modes for the first active `CeedBasis`.
1594ca94c3ddSJeremy L Thompson   @param[out] eval_modes_in         Pointer to hold arrays of input @ref CeedEvalMode, or `NULL`
1595ca94c3ddSJeremy L Thompson   @param[out] eval_mode_offsets_in  Pointer to hold arrays of input offsets at each quadrature point
1596506b1a0cSSebastian Grimberg   @param[out] num_active_bases_out  Total number of active bases for output
1597ca94c3ddSJeremy L Thompson   @param[out] num_eval_modes_out    Pointer to hold array of numbers of output @ref CeedEvalMode, or `NULL`
1598ca94c3ddSJeremy L Thompson   @param[out] eval_modes_out        Pointer to hold arrays of output @ref CeedEvalMode, or `NULL`
1599437c7c90SJeremy L Thompson   @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point
1600ca94c3ddSJeremy 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
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 CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedInt **num_eval_modes_in,
1607506b1a0cSSebastian Grimberg                                          const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt *num_active_bases_out,
1608506b1a0cSSebastian Grimberg                                          CeedInt **num_eval_modes_out, const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out,
1609506b1a0cSSebastian Grimberg                                          CeedSize *num_output_components) {
1610506b1a0cSSebastian Grimberg   if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in;
1611437c7c90SJeremy L Thompson   if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in;
1612437c7c90SJeremy L Thompson   if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in;
1613437c7c90SJeremy L Thompson   if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in;
1614506b1a0cSSebastian Grimberg   if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out;
1615437c7c90SJeremy L Thompson   if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out;
1616437c7c90SJeremy L Thompson   if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out;
1617437c7c90SJeremy L Thompson   if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out;
1618437c7c90SJeremy L Thompson   if (num_output_components) *num_output_components = data->num_output_components;
1619ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1620ed9e99e6SJeremy L Thompson }
1621ed9e99e6SJeremy L Thompson 
1622ed9e99e6SJeremy L Thompson /**
1623ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` `CeedBasis` data for assembly.
1624ba746a46SJeremy L Thompson 
1625ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1626ed9e99e6SJeremy L Thompson 
1627ca94c3ddSJeremy L Thompson   @param[in]  data                 `CeedOperatorAssemblyData`
1628ca94c3ddSJeremy L Thompson   @param[out] num_active_bases_in  Number of active input bases, or `NULL`
1629ca94c3ddSJeremy L Thompson   @param[out] active_bases_in      Pointer to hold active input `CeedBasis`, or `NULL`
1630ca94c3ddSJeremy L Thompson   @param[out] assembled_bases_in   Pointer to hold assembled active input `B` , or `NULL`
1631ca94c3ddSJeremy L Thompson   @param[out] num_active_bases_out Number of active output bases, or `NULL`
1632ca94c3ddSJeremy L Thompson   @param[out] active_bases_out     Pointer to hold active output `CeedBasis`, or `NULL`
1633ca94c3ddSJeremy L Thompson   @param[out] assembled_bases_out  Pointer to hold assembled active output `B` , or `NULL`
1634ed9e99e6SJeremy L Thompson 
1635ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1636ed9e99e6SJeremy L Thompson 
1637ed9e99e6SJeremy L Thompson   @ref Backend
1638ed9e99e6SJeremy L Thompson **/
1639506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedBasis **active_bases_in,
1640506b1a0cSSebastian Grimberg                                      const CeedScalar ***assembled_bases_in, CeedInt *num_active_bases_out, CeedBasis **active_bases_out,
1641506b1a0cSSebastian Grimberg                                      const CeedScalar ***assembled_bases_out) {
1642ed9e99e6SJeremy L Thompson   // Assemble B_in, B_out if needed
1643437c7c90SJeremy L Thompson   if (assembled_bases_in && !data->assembled_bases_in[0]) {
1644437c7c90SJeremy L Thompson     CeedInt num_qpts;
1645437c7c90SJeremy L Thompson 
1646506b1a0cSSebastian Grimberg     if (data->active_bases_in[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[0], &num_qpts));
1647506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_in[0], &num_qpts));
1648506b1a0cSSebastian Grimberg     for (CeedInt b = 0; b < data->num_active_bases_in; b++) {
16491c66c397SJeremy L Thompson       bool        has_eval_none = false;
1650352a5e7cSSebastian Grimberg       CeedInt     num_nodes;
1651437c7c90SJeremy L Thompson       CeedScalar *B_in = NULL, *identity = NULL;
1652ed9e99e6SJeremy L Thompson 
1653506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[b], &num_nodes));
1654352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_in[b], &B_in));
1655ed9e99e6SJeremy L Thompson 
1656437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) {
1657437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE);
1658ed9e99e6SJeremy L Thompson       }
1659ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1660352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1661352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1662352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1663ed9e99e6SJeremy L Thompson         }
1664ed9e99e6SJeremy L Thompson       }
1665ed9e99e6SJeremy L Thompson 
1666ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1667352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1668352a5e7cSSebastian Grimberg           CeedInt      d_in              = 0, q_comp_in;
1669352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE;
16701c66c397SJeremy L Thompson 
1671437c7c90SJeremy L Thompson           for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) {
1672437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_in[b] * q;
1673437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
16741c66c397SJeremy L Thompson 
1675506b1a0cSSebastian Grimberg             CeedCall(CeedOperatorGetBasisPointer(data->active_bases_in[b], data->eval_modes_in[b][e_in], identity, &B));
1676506b1a0cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_in[b], data->eval_modes_in[b][e_in], &q_comp_in));
1677352a5e7cSSebastian Grimberg             if (q_comp_in > 1) {
1678352a5e7cSSebastian Grimberg               if (e_in == 0 || data->eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0;
1679352a5e7cSSebastian Grimberg               else B = &B[(++d_in) * num_qpts * num_nodes];
1680352a5e7cSSebastian Grimberg             }
1681352a5e7cSSebastian Grimberg             eval_mode_in_prev                 = data->eval_modes_in[b][e_in];
1682352a5e7cSSebastian Grimberg             B_in[(qq + e_in) * num_nodes + n] = B[q * num_nodes + n];
1683ed9e99e6SJeremy L Thompson           }
1684ed9e99e6SJeremy L Thompson         }
1685ed9e99e6SJeremy L Thompson       }
16867c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1687437c7c90SJeremy L Thompson       data->assembled_bases_in[b] = B_in;
1688437c7c90SJeremy L Thompson     }
1689ed9e99e6SJeremy L Thompson   }
1690ed9e99e6SJeremy L Thompson 
1691437c7c90SJeremy L Thompson   if (assembled_bases_out && !data->assembled_bases_out[0]) {
1692437c7c90SJeremy L Thompson     CeedInt num_qpts;
1693437c7c90SJeremy L Thompson 
1694506b1a0cSSebastian Grimberg     if (data->active_bases_out[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[0], &num_qpts));
1695506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_out[0], &num_qpts));
1696506b1a0cSSebastian Grimberg     for (CeedInt b = 0; b < data->num_active_bases_out; b++) {
1697ed9e99e6SJeremy L Thompson       bool        has_eval_none = false;
16981c66c397SJeremy L Thompson       CeedInt     num_nodes;
1699437c7c90SJeremy L Thompson       CeedScalar *B_out = NULL, *identity = NULL;
1700ed9e99e6SJeremy L Thompson 
1701506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[b], &num_nodes));
1702352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_out[b], &B_out));
1703ed9e99e6SJeremy L Thompson 
1704437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) {
1705437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE);
1706ed9e99e6SJeremy L Thompson       }
1707ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1708352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1709352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1710352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1711ed9e99e6SJeremy L Thompson         }
1712ed9e99e6SJeremy L Thompson       }
1713ed9e99e6SJeremy L Thompson 
1714ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1715352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1716352a5e7cSSebastian Grimberg           CeedInt      d_out              = 0, q_comp_out;
1717352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
17181c66c397SJeremy L Thompson 
1719437c7c90SJeremy L Thompson           for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) {
1720437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_out[b] * q;
1721437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
17221c66c397SJeremy L Thompson 
1723506b1a0cSSebastian Grimberg             CeedCall(CeedOperatorGetBasisPointer(data->active_bases_out[b], data->eval_modes_out[b][e_out], identity, &B));
1724506b1a0cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_out[b], data->eval_modes_out[b][e_out], &q_comp_out));
1725352a5e7cSSebastian Grimberg             if (q_comp_out > 1) {
1726352a5e7cSSebastian Grimberg               if (e_out == 0 || data->eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0;
1727352a5e7cSSebastian Grimberg               else B = &B[(++d_out) * num_qpts * num_nodes];
1728352a5e7cSSebastian Grimberg             }
1729352a5e7cSSebastian Grimberg             eval_mode_out_prev                  = data->eval_modes_out[b][e_out];
1730352a5e7cSSebastian Grimberg             B_out[(qq + e_out) * num_nodes + n] = B[q * num_nodes + n];
1731ed9e99e6SJeremy L Thompson           }
1732ed9e99e6SJeremy L Thompson         }
1733ed9e99e6SJeremy L Thompson       }
17347c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1735437c7c90SJeremy L Thompson       data->assembled_bases_out[b] = B_out;
1736437c7c90SJeremy L Thompson     }
1737ed9e99e6SJeremy L Thompson   }
1738ed9e99e6SJeremy L Thompson 
1739437c7c90SJeremy L Thompson   // Pass out assembled data
1740506b1a0cSSebastian Grimberg   if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in;
1741506b1a0cSSebastian Grimberg   if (active_bases_in) *active_bases_in = data->active_bases_in;
1742437c7c90SJeremy L Thompson   if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in;
1743506b1a0cSSebastian Grimberg   if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out;
1744506b1a0cSSebastian Grimberg   if (active_bases_out) *active_bases_out = data->active_bases_out;
1745437c7c90SJeremy L Thompson   if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out;
1746437c7c90SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1747437c7c90SJeremy L Thompson }
1748437c7c90SJeremy L Thompson 
1749437c7c90SJeremy L Thompson /**
1750ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` `CeedBasis` data for assembly.
1751ba746a46SJeremy L Thompson 
1752ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1753437c7c90SJeremy L Thompson 
1754ca94c3ddSJeremy L Thompson   @param[in]  data                      `CeedOperatorAssemblyData`
1755ca94c3ddSJeremy L Thompson   @param[out] num_active_elem_rstrs_in  Number of active input element restrictions, or `NULL`
1756ca94c3ddSJeremy L Thompson   @param[out] active_elem_rstrs_in      Pointer to hold active input `CeedElemRestriction`, or `NULL`
1757ca94c3ddSJeremy L Thompson   @param[out] num_active_elem_rstrs_out Number of active output element restrictions, or `NULL`
1758ca94c3ddSJeremy L Thompson   @param[out] active_elem_rstrs_out     Pointer to hold active output `CeedElemRestriction`, or `NULL`
1759437c7c90SJeremy L Thompson 
1760437c7c90SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1761437c7c90SJeremy L Thompson 
1762437c7c90SJeremy L Thompson   @ref Backend
1763437c7c90SJeremy L Thompson **/
1764506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs_in,
1765506b1a0cSSebastian Grimberg                                                 CeedElemRestriction **active_elem_rstrs_in, CeedInt *num_active_elem_rstrs_out,
1766506b1a0cSSebastian Grimberg                                                 CeedElemRestriction **active_elem_rstrs_out) {
1767506b1a0cSSebastian Grimberg   if (num_active_elem_rstrs_in) *num_active_elem_rstrs_in = data->num_active_bases_in;
1768506b1a0cSSebastian Grimberg   if (active_elem_rstrs_in) *active_elem_rstrs_in = data->active_elem_rstrs_in;
1769506b1a0cSSebastian Grimberg   if (num_active_elem_rstrs_out) *num_active_elem_rstrs_out = data->num_active_bases_out;
1770506b1a0cSSebastian Grimberg   if (active_elem_rstrs_out) *active_elem_rstrs_out = data->active_elem_rstrs_out;
1771ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1772ed9e99e6SJeremy L Thompson }
1773ed9e99e6SJeremy L Thompson 
1774ed9e99e6SJeremy L Thompson /**
1775ca94c3ddSJeremy L Thompson   @brief Destroy `CeedOperatorAssemblyData`
1776ed9e99e6SJeremy L Thompson 
1777ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedOperatorAssemblyData` to destroy
1778ed9e99e6SJeremy L Thompson 
1779ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1780ed9e99e6SJeremy L Thompson 
1781ed9e99e6SJeremy L Thompson   @ref Backend
1782ed9e99e6SJeremy L Thompson **/
1783ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) {
1784ad6481ceSJeremy L Thompson   if (!*data) {
1785ad6481ceSJeremy L Thompson     *data = NULL;
1786ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1787ad6481ceSJeremy L Thompson   }
17882b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
1789506b1a0cSSebastian Grimberg   for (CeedInt b = 0; b < (*data)->num_active_bases_in; b++) {
1790506b1a0cSSebastian Grimberg     CeedCall(CeedBasisDestroy(&(*data)->active_bases_in[b]));
1791506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_in[b]));
1792437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_in[b]));
1793437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b]));
1794437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_in[b]));
1795506b1a0cSSebastian Grimberg   }
1796506b1a0cSSebastian Grimberg   for (CeedInt b = 0; b < (*data)->num_active_bases_out; b++) {
1797506b1a0cSSebastian Grimberg     CeedCall(CeedBasisDestroy(&(*data)->active_bases_out[b]));
1798506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_out[b]));
1799506b1a0cSSebastian Grimberg     CeedCall(CeedFree(&(*data)->eval_modes_out[b]));
1800506b1a0cSSebastian Grimberg     CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b]));
1801437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_out[b]));
1802437c7c90SJeremy L Thompson   }
1803506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_bases_in));
1804506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_bases_out));
1805506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_elem_rstrs_in));
1806506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_elem_rstrs_out));
1807437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_in));
1808437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_out));
1809437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_in));
1810437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_out));
1811437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_in));
1812437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_out));
1813437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_in));
1814437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_out));
1815ed9e99e6SJeremy L Thompson 
18162b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1817ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1818ed9e99e6SJeremy L Thompson }
1819ed9e99e6SJeremy L Thompson 
18204dd1a9d2SSebastian Grimberg /**
1821ca94c3ddSJeremy L Thompson   @brief Retrieve fallback `CeedOperator` with a reference `Ceed` for advanced `CeedOperator` functionality
18224dd1a9d2SSebastian Grimberg 
1823ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to retrieve fallback for
1824ca94c3ddSJeremy L Thompson   @param[out] op_fallback Fallback `CeedOperator`
18254dd1a9d2SSebastian Grimberg 
18264dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
18274dd1a9d2SSebastian Grimberg 
18284dd1a9d2SSebastian Grimberg   @ref Backend
18294dd1a9d2SSebastian Grimberg **/
18304dd1a9d2SSebastian Grimberg int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) {
18314dd1a9d2SSebastian Grimberg   // Create if needed
18324dd1a9d2SSebastian Grimberg   if (!op->op_fallback) CeedCall(CeedOperatorCreateFallback(op));
18334dd1a9d2SSebastian Grimberg   if (op->op_fallback) {
18344dd1a9d2SSebastian Grimberg     bool is_debug;
18351203703bSJeremy L Thompson     Ceed ceed;
18364dd1a9d2SSebastian Grimberg 
18374dd1a9d2SSebastian Grimberg     CeedCall(CeedOperatorGetCeed(op, &ceed));
18381203703bSJeremy L Thompson     CeedCall(CeedIsDebug(ceed, &is_debug));
18391203703bSJeremy L Thompson     if (is_debug) {
18401203703bSJeremy L Thompson       Ceed        ceed_fallback;
18411203703bSJeremy L Thompson       const char *resource, *resource_fallback;
18421203703bSJeremy L Thompson 
18434dd1a9d2SSebastian Grimberg       CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
18444dd1a9d2SSebastian Grimberg       CeedCall(CeedGetResource(ceed, &resource));
18454dd1a9d2SSebastian Grimberg       CeedCall(CeedGetResource(ceed_fallback, &resource_fallback));
18464dd1a9d2SSebastian Grimberg 
18474dd1a9d2SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n");
1848249f8407SJeremy 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);
18499bc66399SJeremy L Thompson       CeedCall(CeedDestroy(&ceed_fallback));
18504dd1a9d2SSebastian Grimberg     }
18519bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&ceed));
18524dd1a9d2SSebastian Grimberg   }
18534dd1a9d2SSebastian Grimberg   *op_fallback = op->op_fallback;
18544dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
18554dd1a9d2SSebastian Grimberg }
18564dd1a9d2SSebastian Grimberg 
18574dd1a9d2SSebastian Grimberg /**
1858ca94c3ddSJeremy L Thompson   @brief Get the parent `CeedOperator` for a fallback `CeedOperator`
18594dd1a9d2SSebastian Grimberg 
1860ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` context
1861ca94c3ddSJeremy L Thompson   @param[out] parent Variable to store parent `CeedOperator` context
18624dd1a9d2SSebastian Grimberg 
18634dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
18644dd1a9d2SSebastian Grimberg 
18654dd1a9d2SSebastian Grimberg   @ref Backend
18664dd1a9d2SSebastian Grimberg **/
18674dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParent(CeedOperator op, CeedOperator *parent) {
18684dd1a9d2SSebastian Grimberg   *parent = op->op_fallback_parent ? op->op_fallback_parent : NULL;
18694dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
18704dd1a9d2SSebastian Grimberg }
18714dd1a9d2SSebastian Grimberg 
18724dd1a9d2SSebastian Grimberg /**
1873ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` context of the parent `CeedOperator` for a fallback `CeedOperator`
18744dd1a9d2SSebastian Grimberg 
1875ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` context
1876ca94c3ddSJeremy L Thompson   @param[out] parent Variable to store parent `Ceed` context
18774dd1a9d2SSebastian Grimberg 
18784dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
18794dd1a9d2SSebastian Grimberg 
18804dd1a9d2SSebastian Grimberg   @ref Backend
18814dd1a9d2SSebastian Grimberg **/
18824dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParentCeed(CeedOperator op, Ceed *parent) {
18839bc66399SJeremy L Thompson   *parent = NULL;
18849bc66399SJeremy L Thompson   if (op->op_fallback_parent) CeedCall(CeedReferenceCopy(op->op_fallback_parent->ceed, parent));
18859bc66399SJeremy L Thompson   else CeedCall(CeedReferenceCopy(CeedOperatorReturnCeed(op), parent));
18864dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
18874dd1a9d2SSebastian Grimberg }
18884dd1a9d2SSebastian Grimberg 
1889480fae85SJeremy L Thompson /// @}
1890480fae85SJeremy L Thompson 
1891480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1892eaf62fffSJeremy L Thompson /// CeedOperator Public API
1893eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1894eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser
1895eaf62fffSJeremy L Thompson /// @{
1896eaf62fffSJeremy L Thompson 
1897eaf62fffSJeremy L Thompson /**
1898ca94c3ddSJeremy L Thompson   @brief Assemble a linear `CeedQFunction` associated with a `CeedOperator`.
1899eaf62fffSJeremy L Thompson 
1900ca94c3ddSJeremy L Thompson   This returns a `CeedVector` containing a matrix at each quadrature point providing the action of the `CeedQFunction` associated with the `CeedOperator`.
1901ca94c3ddSJeremy 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.
1902859c15bbSJames Wright 
1903ca94c3ddSJeremy L Thompson   Inputs and outputs are in the order provided by the user when adding `CeedOperator` fields.
1904ca94c3ddSJeremy 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]`.
1905eaf62fffSJeremy L Thompson 
1906ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1907f04ea552SJeremy L Thompson 
1908ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1909ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points
1910ca94c3ddSJeremy L Thompson   @param[out] rstr      `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1911ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1912eaf62fffSJeremy L Thompson 
1913eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1914eaf62fffSJeremy L Thompson 
1915eaf62fffSJeremy L Thompson   @ref User
1916eaf62fffSJeremy L Thompson **/
19172b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
19182b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1919eaf62fffSJeremy L Thompson 
1920eaf62fffSJeremy L Thompson   if (op->LinearAssembleQFunction) {
1921d04bbc78SJeremy L Thompson     // Backend version
19222b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request));
1923eaf62fffSJeremy L Thompson   } else {
1924d04bbc78SJeremy L Thompson     // Operator fallback
1925d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1926d04bbc78SJeremy L Thompson 
19272b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
19286574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request));
19299bc66399SJeremy L Thompson     else return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction");
193070a7ffb3SJeremy L Thompson   }
1931eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1932eaf62fffSJeremy L Thompson }
193370a7ffb3SJeremy L Thompson 
193470a7ffb3SJeremy L Thompson /**
1935ca94c3ddSJeremy L Thompson   @brief Assemble `CeedQFunction` and store result internally.
19364385fb7fSSebastian Grimberg 
1937ea61e9acSJeremy L Thompson   Return copied references of stored data to the caller.
1938ea61e9acSJeremy L Thompson   Caller is responsible for ownership and destruction of the copied references.
1939ca94c3ddSJeremy L Thompson   See also @ref CeedOperatorLinearAssembleQFunction().
194070a7ffb3SJeremy L Thompson 
1941ca94c3ddSJeremy 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.
1942c5f45aeaSJeremy L Thompson         These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object.
1943c5f45aeaSJeremy L Thompson 
1944ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1945ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points
1946ca94c3ddSJeremy L Thompson   @param[out] rstr      `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1947ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
194870a7ffb3SJeremy L Thompson 
194970a7ffb3SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
195070a7ffb3SJeremy L Thompson 
195170a7ffb3SJeremy L Thompson   @ref User
195270a7ffb3SJeremy L Thompson **/
19532b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
1954b05f7e9fSJeremy L Thompson   int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *) = NULL;
1955b05f7e9fSJeremy L Thompson   CeedOperator op_assemble                                                                           = NULL;
1956bb229da9SJeremy L Thompson   CeedOperator op_fallback_parent                                                                    = NULL;
1957b05f7e9fSJeremy L Thompson 
19582b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
195970a7ffb3SJeremy L Thompson 
1960b05f7e9fSJeremy L Thompson   // Determine if fallback parent or operator has implementation
1961bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParent(op, &op_fallback_parent));
1962bb229da9SJeremy L Thompson   if (op_fallback_parent && op_fallback_parent->LinearAssembleQFunctionUpdate) {
1963b05f7e9fSJeremy L Thompson     // -- Backend version for op fallback parent is faster, if it exists
1964bb229da9SJeremy L Thompson     LinearAssembleQFunctionUpdate = op_fallback_parent->LinearAssembleQFunctionUpdate;
1965bb229da9SJeremy L Thompson     op_assemble                   = op_fallback_parent;
1966b05f7e9fSJeremy L Thompson   } else if (op->LinearAssembleQFunctionUpdate) {
1967b05f7e9fSJeremy L Thompson     // -- Backend version for op
1968b05f7e9fSJeremy L Thompson     LinearAssembleQFunctionUpdate = op->LinearAssembleQFunctionUpdate;
1969b05f7e9fSJeremy L Thompson     op_assemble                   = op;
1970b05f7e9fSJeremy L Thompson   }
1971b05f7e9fSJeremy L Thompson 
1972b05f7e9fSJeremy L Thompson   // Assemble QFunction
1973b05f7e9fSJeremy L Thompson   if (LinearAssembleQFunctionUpdate) {
1974b05f7e9fSJeremy L Thompson     // Backend or fallback parent version
19757d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
19767d5185d7SSebastian Grimberg     bool                      data_is_setup;
19772efa2d85SJeremy L Thompson     CeedVector                assembled_vec  = NULL;
19782efa2d85SJeremy L Thompson     CeedElemRestriction       assembled_rstr = NULL;
1979480fae85SJeremy L Thompson 
19807d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
19817d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataIsSetup(data, &data_is_setup));
19827d5185d7SSebastian Grimberg     if (data_is_setup) {
1983d04bbc78SJeremy L Thompson       bool update_needed;
1984d04bbc78SJeremy L Thompson 
19857d5185d7SSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataGetObjects(data, &assembled_vec, &assembled_rstr));
19867d5185d7SSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(data, &update_needed));
1987b05f7e9fSJeremy L Thompson       if (update_needed) CeedCall(LinearAssembleQFunctionUpdate(op_assemble, assembled_vec, assembled_rstr, request));
198870a7ffb3SJeremy L Thompson     } else {
1989b05f7e9fSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunction(op_assemble, &assembled_vec, &assembled_rstr, request));
19907d5185d7SSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataSetObjects(data, assembled_vec, assembled_rstr));
199170a7ffb3SJeremy L Thompson     }
19927d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(data, false));
19932efa2d85SJeremy L Thompson 
1994d04bbc78SJeremy L Thompson     // Copy reference from internally held copy
19952b730f8bSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled));
19962b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr));
1997c5f45aeaSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled_vec));
19982b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&assembled_rstr));
199970a7ffb3SJeremy L Thompson   } else {
2000d04bbc78SJeremy L Thompson     // Operator fallback
2001d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2002d04bbc78SJeremy L Thompson 
20032b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
20046574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request));
20059bc66399SJeremy L Thompson     else return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate");
200670a7ffb3SJeremy L Thompson   }
200770a7ffb3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2008eaf62fffSJeremy L Thompson }
2009eaf62fffSJeremy L Thompson 
2010eaf62fffSJeremy L Thompson /**
2011ca94c3ddSJeremy L Thompson   @brief Assemble the diagonal of a square linear `CeedOperator`
2012eaf62fffSJeremy L Thompson 
2013ca94c3ddSJeremy L Thompson   This overwrites a `CeedVector` with the diagonal of a linear `CeedOperator`.
2014eaf62fffSJeremy L Thompson 
2015ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2016eaf62fffSJeremy L Thompson 
2017ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2018f04ea552SJeremy L Thompson 
2019ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2020ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal
2021ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2022eaf62fffSJeremy L Thompson 
2023eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2024eaf62fffSJeremy L Thompson 
2025eaf62fffSJeremy L Thompson   @ref User
2026eaf62fffSJeremy L Thompson **/
20272b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2028f3d47e36SJeremy L Thompson   bool     is_composite;
20291c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
20301c66c397SJeremy L Thompson 
20312b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2032f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2033eaf62fffSJeremy L Thompson 
20342b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
20359bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
2036c9366a6bSJeremy L Thompson 
2037f3d47e36SJeremy L Thompson   // Early exit for empty operator
2038f3d47e36SJeremy L Thompson   if (!is_composite) {
2039f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2040f3d47e36SJeremy L Thompson 
2041f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2042f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2043f3d47e36SJeremy L Thompson   }
2044f3d47e36SJeremy L Thompson 
2045eaf62fffSJeremy L Thompson   if (op->LinearAssembleDiagonal) {
2046d04bbc78SJeremy L Thompson     // Backend version
20472b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleDiagonal(op, assembled, request));
2048eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2049eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddDiagonal) {
2050d04bbc78SJeremy L Thompson     // Backend version with zeroing first
20512b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
20522b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
2053eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
20540183ed61SJeremy L Thompson   } else if (is_composite) {
20550183ed61SJeremy L Thompson     // Default to summing contributions of suboperators
20560183ed61SJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
20570183ed61SJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
20580183ed61SJeremy L Thompson     return CEED_ERROR_SUCCESS;
2059eaf62fffSJeremy L Thompson   } else {
2060d04bbc78SJeremy L Thompson     // Operator fallback
2061d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2062d04bbc78SJeremy L Thompson 
20632b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2064d04bbc78SJeremy L Thompson     if (op_fallback) {
20652b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request));
2066eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2067eaf62fffSJeremy L Thompson     }
2068eaf62fffSJeremy L Thompson   }
2069eaf62fffSJeremy L Thompson   // Default interface implementation
20702b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
20712b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request));
2072eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2073eaf62fffSJeremy L Thompson }
2074eaf62fffSJeremy L Thompson 
2075eaf62fffSJeremy L Thompson /**
2076ca94c3ddSJeremy L Thompson   @brief Assemble the diagonal of a square linear `CeedOperator`.
2077eaf62fffSJeremy L Thompson 
2078ca94c3ddSJeremy L Thompson   This sums into a `CeedVector` the diagonal of a linear `CeedOperator`.
2079eaf62fffSJeremy L Thompson 
2080ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2081eaf62fffSJeremy L Thompson 
2082ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2083f04ea552SJeremy L Thompson 
2084ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2085ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal
2086ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2087eaf62fffSJeremy L Thompson 
2088eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2089eaf62fffSJeremy L Thompson 
2090eaf62fffSJeremy L Thompson   @ref User
2091eaf62fffSJeremy L Thompson **/
20922b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2093f3d47e36SJeremy L Thompson   bool     is_composite;
20941c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
20951c66c397SJeremy L Thompson 
20962b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2097f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2098eaf62fffSJeremy L Thompson 
20992b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
21009bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
2101c9366a6bSJeremy L Thompson 
2102f3d47e36SJeremy L Thompson   // Early exit for empty operator
2103f3d47e36SJeremy L Thompson   if (!is_composite) {
2104f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2105f3d47e36SJeremy L Thompson 
2106f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2107f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2108f3d47e36SJeremy L Thompson   }
2109f3d47e36SJeremy L Thompson 
2110eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddDiagonal) {
2111d04bbc78SJeremy L Thompson     // Backend version
21122b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
2113eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
21140183ed61SJeremy L Thompson   } else if (is_composite) {
21150183ed61SJeremy L Thompson     // Default to summing contributions of suboperators
21160183ed61SJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
2117eaf62fffSJeremy L Thompson   } else {
2118d04bbc78SJeremy L Thompson     // Operator fallback
2119d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2120d04bbc78SJeremy L Thompson 
21212b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2122d04bbc78SJeremy L Thompson     if (op_fallback) {
21232b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
2124eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2125eaf62fffSJeremy L Thompson     }
2126eaf62fffSJeremy L Thompson   }
2127eaf62fffSJeremy L Thompson   // Default interface implementation
2128f3bd9308SJeremy L Thompson   CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
2129d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2130eaf62fffSJeremy L Thompson }
2131eaf62fffSJeremy L Thompson 
2132eaf62fffSJeremy L Thompson /**
2133ca94c3ddSJeremy L Thompson    @brief Fully assemble the point-block diagonal pattern of a linear `CeedOperator`.
213401f0e615SJames Wright 
2135ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssemblePointBlockDiagonal().
213601f0e615SJames Wright 
2137ca94c3ddSJeremy 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)`.
2138ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are unique.
2139ca94c3ddSJeremy L Thompson    This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemblePointBlockDiagonal() provides the values in the same ordering.
214001f0e615SJames Wright 
214101f0e615SJames Wright    This will generally be slow unless your operator is low-order.
214201f0e615SJames Wright 
2143ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
214401f0e615SJames Wright 
2145ca94c3ddSJeremy L Thompson    @param[in]  op          `CeedOperator` to assemble
214601f0e615SJames Wright    @param[out] num_entries Number of entries in coordinate nonzero pattern
214701f0e615SJames Wright    @param[out] rows        Row number for each entry
214801f0e615SJames Wright    @param[out] cols        Column number for each entry
214901f0e615SJames Wright 
215001f0e615SJames Wright    @ref User
215101f0e615SJames Wright **/
215201f0e615SJames Wright int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
215301f0e615SJames Wright   bool          is_composite;
215401f0e615SJames Wright   CeedInt       num_active_components, num_sub_operators;
215501f0e615SJames Wright   CeedOperator *sub_operators;
215601f0e615SJames Wright 
215701f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
215801f0e615SJames Wright 
215901f0e615SJames Wright   CeedSize input_size = 0, output_size = 0;
216001f0e615SJames Wright   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
21619bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
216201f0e615SJames Wright 
216301f0e615SJames Wright   if (is_composite) {
216401f0e615SJames Wright     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_operators));
216501f0e615SJames Wright     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
216601f0e615SJames Wright   } else {
216701f0e615SJames Wright     sub_operators     = &op;
216801f0e615SJames Wright     num_sub_operators = 1;
216901f0e615SJames Wright   }
217001f0e615SJames Wright 
2171506b1a0cSSebastian Grimberg   // Verify operator can be assembled correctly
2172506b1a0cSSebastian Grimberg   {
217301f0e615SJames Wright     CeedOperatorAssemblyData data;
2174506b1a0cSSebastian Grimberg     CeedInt                  num_active_elem_rstrs, comp_stride;
217501f0e615SJames Wright     CeedElemRestriction     *active_elem_rstrs;
217601f0e615SJames Wright 
217701f0e615SJames Wright     // Get initial values to check against
217801f0e615SJames Wright     CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[0], &data));
2179506b1a0cSSebastian Grimberg     CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL));
218001f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[0], &comp_stride));
218101f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[0], &num_active_components));
218201f0e615SJames Wright 
2183506b1a0cSSebastian Grimberg     // Verify that all active element restrictions have same component stride and number of components
218401f0e615SJames Wright     for (CeedInt k = 0; k < num_sub_operators; k++) {
218501f0e615SJames Wright       CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[k], &data));
2186506b1a0cSSebastian Grimberg       CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL));
218701f0e615SJames Wright       for (CeedInt i = 0; i < num_active_elem_rstrs; i++) {
2188506b1a0cSSebastian Grimberg         CeedInt comp_stride_sub, num_active_components_sub;
2189506b1a0cSSebastian Grimberg 
219001f0e615SJames Wright         CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[i], &comp_stride_sub));
21919bc66399SJeremy L Thompson         CeedCheck(comp_stride == comp_stride_sub, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION,
219201f0e615SJames Wright                   "Active element restrictions must have the same component stride: %d vs %d", comp_stride, comp_stride_sub);
219301f0e615SJames Wright         CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[i], &num_active_components_sub));
21949bc66399SJeremy L Thompson         CeedCheck(num_active_components == num_active_components_sub, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
21953f08121cSJeremy L Thompson                   "All suboperators must have the same number of output components."
21963f08121cSJeremy L Thompson                   " Previous: %" CeedInt_FMT " Current: %" CeedInt_FMT,
21973f08121cSJeremy L Thompson                   num_active_components, num_active_components_sub);
219801f0e615SJames Wright       }
219901f0e615SJames Wright     }
220001f0e615SJames Wright   }
220101f0e615SJames Wright   *num_entries = input_size * num_active_components;
220201f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, rows));
220301f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, cols));
220401f0e615SJames Wright 
220501f0e615SJames Wright   for (CeedInt o = 0; o < num_sub_operators; o++) {
2206506b1a0cSSebastian Grimberg     CeedElemRestriction active_elem_rstr, point_block_active_elem_rstr;
220701f0e615SJames Wright     CeedInt             comp_stride, num_elem, elem_size;
2208506b1a0cSSebastian Grimberg     const CeedInt      *offsets, *point_block_offsets;
220901f0e615SJames Wright 
221001f0e615SJames Wright     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[o], &active_elem_rstr));
221101f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstr, &comp_stride));
221201f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumElements(active_elem_rstr, &num_elem));
221301f0e615SJames Wright     CeedCall(CeedElemRestrictionGetElementSize(active_elem_rstr, &elem_size));
221401f0e615SJames Wright     CeedCall(CeedElemRestrictionGetOffsets(active_elem_rstr, CEED_MEM_HOST, &offsets));
221501f0e615SJames Wright 
2216506b1a0cSSebastian Grimberg     CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstr, &point_block_active_elem_rstr));
2217506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOffsets(point_block_active_elem_rstr, CEED_MEM_HOST, &point_block_offsets));
221801f0e615SJames Wright 
221901f0e615SJames Wright     for (CeedSize i = 0; i < num_elem * elem_size; i++) {
222001f0e615SJames Wright       for (CeedInt c_out = 0; c_out < num_active_components; c_out++) {
222101f0e615SJames Wright         for (CeedInt c_in = 0; c_in < num_active_components; c_in++) {
2222506b1a0cSSebastian Grimberg           (*rows)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_out * comp_stride;
2223506b1a0cSSebastian Grimberg           (*cols)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_in * comp_stride;
222401f0e615SJames Wright         }
222501f0e615SJames Wright       }
222601f0e615SJames Wright     }
222701f0e615SJames Wright 
222801f0e615SJames Wright     CeedCall(CeedElemRestrictionRestoreOffsets(active_elem_rstr, &offsets));
2229506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOffsets(point_block_active_elem_rstr, &point_block_offsets));
2230681d0ea7SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&active_elem_rstr));
2231506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&point_block_active_elem_rstr));
223201f0e615SJames Wright   }
223301f0e615SJames Wright   return CEED_ERROR_SUCCESS;
223401f0e615SJames Wright }
223501f0e615SJames Wright 
223601f0e615SJames Wright /**
2237ca94c3ddSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear `CeedOperator`.
2238eaf62fffSJeremy L Thompson 
2239ca94c3ddSJeremy L Thompson   This overwrites a `CeedVector` with the point block diagonal of a linear `CeedOperator`.
2240eaf62fffSJeremy L Thompson 
2241ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2242eaf62fffSJeremy L Thompson 
2243ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2244f04ea552SJeremy L Thompson 
2245ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2246ca94c3ddSJeremy 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.
2247ca94c3ddSJeremy L Thompson                           The dimensions of this vector are derived from the active vector for the `CeedOperator`.
2248ca94c3ddSJeremy L Thompson                           The array has shape `[nodes, component out, component in]`.
2249ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2250eaf62fffSJeremy L Thompson 
2251eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2252eaf62fffSJeremy L Thompson 
2253eaf62fffSJeremy L Thompson   @ref User
2254eaf62fffSJeremy L Thompson **/
22552b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2256f3d47e36SJeremy L Thompson   bool     is_composite;
22571c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
22581c66c397SJeremy L Thompson 
22592b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2260f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2261eaf62fffSJeremy L Thompson 
22622b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
22639bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
2264c9366a6bSJeremy L Thompson 
2265f3d47e36SJeremy L Thompson   // Early exit for empty operator
2266f3d47e36SJeremy L Thompson   if (!is_composite) {
2267f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2268f3d47e36SJeremy L Thompson 
2269f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2270f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2271f3d47e36SJeremy L Thompson   }
2272f3d47e36SJeremy L Thompson 
2273eaf62fffSJeremy L Thompson   if (op->LinearAssemblePointBlockDiagonal) {
2274d04bbc78SJeremy L Thompson     // Backend version
22752b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request));
2276eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2277eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddPointBlockDiagonal) {
2278d04bbc78SJeremy L Thompson     // Backend version with zeroing first
22792b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
22802b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
2281eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2282eaf62fffSJeremy L Thompson   } else {
2283d04bbc78SJeremy L Thompson     // Operator fallback
2284d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2285d04bbc78SJeremy L Thompson 
22862b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2287d04bbc78SJeremy L Thompson     if (op_fallback) {
22882b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request));
2289eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2290eaf62fffSJeremy L Thompson     }
2291eaf62fffSJeremy L Thompson   }
2292eaf62fffSJeremy L Thompson   // Default interface implementation
22932b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
22942b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
2295eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2296eaf62fffSJeremy L Thompson }
2297eaf62fffSJeremy L Thompson 
2298eaf62fffSJeremy L Thompson /**
2299ca94c3ddSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear `CeedOperator`.
2300eaf62fffSJeremy L Thompson 
2301ca94c3ddSJeremy L Thompson   This sums into a `CeedVector` with the point block diagonal of a linear `CeedOperator`.
2302eaf62fffSJeremy L Thompson 
2303ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2304eaf62fffSJeremy L Thompson 
2305ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2306f04ea552SJeremy L Thompson 
2307ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2308ca94c3ddSJeremy 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.
2309ca94c3ddSJeremy L Thompson                           The dimensions of this vector are derived from the active vector for the `CeedOperator`.
2310ca94c3ddSJeremy L Thompson                           The array has shape `[nodes, component out, component in]`.
2311ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2312eaf62fffSJeremy L Thompson 
2313eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2314eaf62fffSJeremy L Thompson 
2315eaf62fffSJeremy L Thompson   @ref User
2316eaf62fffSJeremy L Thompson **/
23172b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2318f3d47e36SJeremy L Thompson   bool     is_composite;
23191c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
23201c66c397SJeremy L Thompson 
23212b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2322f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2323eaf62fffSJeremy L Thompson 
23242b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
23259bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
2326c9366a6bSJeremy L Thompson 
2327f3d47e36SJeremy L Thompson   // Early exit for empty operator
2328f3d47e36SJeremy L Thompson   if (!is_composite) {
2329f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2330f3d47e36SJeremy L Thompson 
2331f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2332f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2333f3d47e36SJeremy L Thompson   }
2334f3d47e36SJeremy L Thompson 
2335eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddPointBlockDiagonal) {
2336d04bbc78SJeremy L Thompson     // Backend version
23372b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request));
2338eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2339eaf62fffSJeremy L Thompson   } else {
2340d04bbc78SJeremy L Thompson     // Operator fallback
2341d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2342d04bbc78SJeremy L Thompson 
23432b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2344d04bbc78SJeremy L Thompson     if (op_fallback) {
23452b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request));
2346eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2347eaf62fffSJeremy L Thompson     }
2348eaf62fffSJeremy L Thompson   }
2349ea61e9acSJeremy L Thompson   // Default interface implementation
2350eaf62fffSJeremy L Thompson   if (is_composite) {
23512b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
2352eaf62fffSJeremy L Thompson   } else {
2353f3bd9308SJeremy L Thompson     CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
2354eaf62fffSJeremy L Thompson   }
2355d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2356eaf62fffSJeremy L Thompson }
2357eaf62fffSJeremy L Thompson 
2358eaf62fffSJeremy L Thompson /**
2359ca94c3ddSJeremy L Thompson    @brief Fully assemble the nonzero pattern of a linear `CeedOperator`.
2360eaf62fffSJeremy L Thompson 
2361ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssemble().
2362eaf62fffSJeremy L Thompson 
2363ca94c3ddSJeremy 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)`.
2364ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are not unique and may repeat.
2365ca94c3ddSJeremy L Thompson    This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemble() provides the values in the same ordering.
2366eaf62fffSJeremy L Thompson 
2367eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2368eaf62fffSJeremy L Thompson 
2369ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2370f04ea552SJeremy L Thompson 
2371ca94c3ddSJeremy L Thompson    @param[in]  op          `CeedOperator` to assemble
2372eaf62fffSJeremy L Thompson    @param[out] num_entries Number of entries in coordinate nonzero pattern
2373eaf62fffSJeremy L Thompson    @param[out] rows        Row number for each entry
2374eaf62fffSJeremy L Thompson    @param[out] cols        Column number for each entry
2375eaf62fffSJeremy L Thompson 
2376eaf62fffSJeremy L Thompson    @ref User
2377eaf62fffSJeremy L Thompson **/
23782b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
23791c66c397SJeremy L Thompson   bool          is_composite;
23801c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2381b94338b9SJed Brown   CeedSize      single_entries;
2382eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
23831c66c397SJeremy L Thompson 
23842b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2385f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2386eaf62fffSJeremy L Thompson 
2387eaf62fffSJeremy L Thompson   if (op->LinearAssembleSymbolic) {
2388d04bbc78SJeremy L Thompson     // Backend version
23892b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols));
2390eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2391eaf62fffSJeremy L Thompson   } else {
2392d04bbc78SJeremy L Thompson     // Operator fallback
2393d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2394d04bbc78SJeremy L Thompson 
23952b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2396d04bbc78SJeremy L Thompson     if (op_fallback) {
23972b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols));
2398eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2399eaf62fffSJeremy L Thompson     }
2400eaf62fffSJeremy L Thompson   }
2401eaf62fffSJeremy L Thompson 
2402eaf62fffSJeremy L Thompson   // Default interface implementation
2403eaf62fffSJeremy L Thompson 
2404506b1a0cSSebastian Grimberg   // Count entries and allocate rows, cols arrays
2405eaf62fffSJeremy L Thompson   *num_entries = 0;
2406eaf62fffSJeremy L Thompson   if (is_composite) {
2407c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2408c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
240992ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
24102b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2411eaf62fffSJeremy L Thompson       *num_entries += single_entries;
2412eaf62fffSJeremy L Thompson     }
2413eaf62fffSJeremy L Thompson   } else {
24142b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries));
2415eaf62fffSJeremy L Thompson     *num_entries += single_entries;
2416eaf62fffSJeremy L Thompson   }
24172b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, rows));
24182b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, cols));
2419eaf62fffSJeremy L Thompson 
2420506b1a0cSSebastian Grimberg   // Assemble nonzero locations
2421eaf62fffSJeremy L Thompson   if (is_composite) {
2422c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2423c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
242492ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
24252b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols));
24262b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2427eaf62fffSJeremy L Thompson       offset += single_entries;
2428eaf62fffSJeremy L Thompson     }
2429eaf62fffSJeremy L Thompson   } else {
24302b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols));
2431eaf62fffSJeremy L Thompson   }
2432eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2433eaf62fffSJeremy L Thompson }
2434eaf62fffSJeremy L Thompson 
2435eaf62fffSJeremy L Thompson /**
2436eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero entries of a linear operator.
2437eaf62fffSJeremy L Thompson 
2438ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssembleSymbolic().
2439eaf62fffSJeremy L Thompson 
2440ca94c3ddSJeremy 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)`.
2441ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are not unique and may repeat.
2442ca94c3ddSJeremy L Thompson    This function returns the values of the nonzero entries to be added, their `(i, j)` locations are provided by @ref CeedOperatorLinearAssembleSymbolic().
2443eaf62fffSJeremy L Thompson 
2444eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2445eaf62fffSJeremy L Thompson 
2446ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2447f04ea552SJeremy L Thompson 
2448ca94c3ddSJeremy L Thompson    @param[in]  op     `CeedOperator` to assemble
2449eaf62fffSJeremy L Thompson    @param[out] values Values to assemble into matrix
2450eaf62fffSJeremy L Thompson 
2451eaf62fffSJeremy L Thompson    @ref User
2452eaf62fffSJeremy L Thompson **/
2453eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) {
2454*915834c9SZach Atkins   bool          is_composite, has_linear_assemble_single;
24551c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2456b94338b9SJed Brown   CeedSize      single_entries = 0;
2457eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
24581c66c397SJeremy L Thompson 
24592b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2460f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2461f3d47e36SJeremy L Thompson 
2462f3d47e36SJeremy L Thompson   // Early exit for empty operator
2463f3d47e36SJeremy L Thompson   if (!is_composite) {
2464f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2465f3d47e36SJeremy L Thompson 
2466f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2467f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2468*915834c9SZach Atkins     has_linear_assemble_single = op->LinearAssembleSingle != NULL;
2469*915834c9SZach Atkins   } else {
2470*915834c9SZach Atkins     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2471*915834c9SZach Atkins     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2472*915834c9SZach Atkins     has_linear_assemble_single = true;
2473*915834c9SZach Atkins     for (CeedInt i = 0; i < num_suboperators; i++) {
2474*915834c9SZach Atkins       has_linear_assemble_single = has_linear_assemble_single && sub_operators[i]->LinearAssembleSingle != NULL;
2475*915834c9SZach Atkins     }
2476f3d47e36SJeremy L Thompson   }
2477eaf62fffSJeremy L Thompson 
2478eaf62fffSJeremy L Thompson   if (op->LinearAssemble) {
2479d04bbc78SJeremy L Thompson     // Backend version
24802b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemble(op, values));
2481eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2482*915834c9SZach Atkins   } else if (has_linear_assemble_single) {
2483*915834c9SZach Atkins     // Default to summing contributions of suboperators
2484*915834c9SZach Atkins     CeedCall(CeedVectorSetValue(values, 0.0));
2485*915834c9SZach Atkins     if (is_composite && num_suboperators > 0 && sub_operators[0]) {
2486*915834c9SZach Atkins       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2487*915834c9SZach Atkins       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2488*915834c9SZach Atkins       for (CeedInt k = 0; k < num_suboperators; k++) {
2489*915834c9SZach Atkins         CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values));
2490*915834c9SZach Atkins         CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2491*915834c9SZach Atkins         offset += single_entries;
2492*915834c9SZach Atkins       }
2493*915834c9SZach Atkins     } else {
2494*915834c9SZach Atkins       CeedCall(CeedSingleOperatorAssemble(op, offset, values));
2495*915834c9SZach Atkins     }
2496*915834c9SZach Atkins     return CEED_ERROR_SUCCESS;
2497eaf62fffSJeremy L Thompson   } else {
2498d04bbc78SJeremy L Thompson     // Operator fallback
2499d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2500d04bbc78SJeremy L Thompson 
25012b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2502d04bbc78SJeremy L Thompson     if (op_fallback) {
25032b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemble(op_fallback, values));
2504eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2505eaf62fffSJeremy L Thompson     }
2506eaf62fffSJeremy L Thompson   }
2507eaf62fffSJeremy L Thompson 
2508eaf62fffSJeremy L Thompson   // Default interface implementation
250928ec399dSJeremy L Thompson   CeedCall(CeedVectorSetValue(values, 0.0));
2510eaf62fffSJeremy L Thompson   if (is_composite) {
2511c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2512c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2513cefa2673SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; k++) {
25142b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values));
25152b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2516eaf62fffSJeremy L Thompson       offset += single_entries;
2517eaf62fffSJeremy L Thompson     }
2518eaf62fffSJeremy L Thompson   } else {
25192b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemble(op, offset, values));
2520eaf62fffSJeremy L Thompson   }
2521eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2522eaf62fffSJeremy L Thompson }
2523eaf62fffSJeremy L Thompson 
2524eaf62fffSJeremy L Thompson /**
2525ca94c3ddSJeremy L Thompson   @brief Get the multiplicity of nodes across sub-operators in a composite `CeedOperator`.
252675f0d5a4SJeremy L Thompson 
2527ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
252875f0d5a4SJeremy L Thompson 
2529ca94c3ddSJeremy L Thompson   @param[in]  op               Composite `CeedOperator`
2530ca94c3ddSJeremy L Thompson   @param[in]  num_skip_indices Number of sub-operators to skip
2531ca94c3ddSJeremy L Thompson   @param[in]  skip_indices     Array of indices of sub-operators to skip
2532ca94c3ddSJeremy L Thompson   @param[out] mult             Vector to store multiplicity (of size `l_size` )
253375f0d5a4SJeremy L Thompson 
253475f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
253575f0d5a4SJeremy L Thompson 
253675f0d5a4SJeremy L Thompson   @ref User
253775f0d5a4SJeremy L Thompson **/
253875f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) {
253975f0d5a4SJeremy L Thompson   Ceed                ceed;
2540b275c451SJeremy L Thompson   CeedInt             num_suboperators;
254175f0d5a4SJeremy L Thompson   CeedSize            l_vec_len;
254275f0d5a4SJeremy L Thompson   CeedScalar         *mult_array;
254375f0d5a4SJeremy L Thompson   CeedVector          ones_l_vec;
25447c1dbaffSSebastian Grimberg   CeedElemRestriction elem_rstr, mult_elem_rstr;
2545b275c451SJeremy L Thompson   CeedOperator       *sub_operators;
254675f0d5a4SJeremy L Thompson 
25471c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
25481c66c397SJeremy L Thompson 
254975f0d5a4SJeremy L Thompson   // Zero mult vector
255075f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
255175f0d5a4SJeremy L Thompson 
255275f0d5a4SJeremy L Thompson   // Get suboperators
2553b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2554b275c451SJeremy L Thompson   if (num_suboperators == 0) return CEED_ERROR_SUCCESS;
25559bc66399SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
255675f0d5a4SJeremy L Thompson 
255775f0d5a4SJeremy L Thompson   // Work vector
255875f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetLength(mult, &l_vec_len));
25599bc66399SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
256075f0d5a4SJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec));
25619bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
256275f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(ones_l_vec, 1.0));
256375f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array));
256475f0d5a4SJeremy L Thompson 
256575f0d5a4SJeremy L Thompson   // Compute multiplicity across suboperators
2566b275c451SJeremy L Thompson   for (CeedInt i = 0; i < num_suboperators; i++) {
256775f0d5a4SJeremy L Thompson     const CeedScalar *sub_mult_array;
256875f0d5a4SJeremy L Thompson     CeedVector        sub_mult_l_vec, ones_e_vec;
256975f0d5a4SJeremy L Thompson 
257075f0d5a4SJeremy L Thompson     // -- Check for suboperator to skip
257175f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < num_skip_indices; j++) {
257275f0d5a4SJeremy L Thompson       if (skip_indices[j] == i) continue;
257375f0d5a4SJeremy L Thompson     }
257475f0d5a4SJeremy L Thompson 
257575f0d5a4SJeremy L Thompson     // -- Sub operator multiplicity
2576437c7c90SJeremy L Thompson     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr));
25777c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr, &mult_elem_rstr));
2578681d0ea7SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&elem_rstr));
25797c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateVector(mult_elem_rstr, &sub_mult_l_vec, &ones_e_vec));
258075f0d5a4SJeremy L Thompson     CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0));
25817c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE));
25827c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE));
258375f0d5a4SJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array));
258475f0d5a4SJeremy L Thompson     // ---- Flag every node present in the current suboperator
2585c81f2b9dSJames Wright     for (CeedSize j = 0; j < l_vec_len; j++) {
258675f0d5a4SJeremy L Thompson       if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0;
258775f0d5a4SJeremy L Thompson     }
258875f0d5a4SJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array));
258975f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&sub_mult_l_vec));
259075f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&ones_e_vec));
25917c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&mult_elem_rstr));
259275f0d5a4SJeremy L Thompson   }
259375f0d5a4SJeremy L Thompson   CeedCall(CeedVectorRestoreArray(mult, &mult_array));
2594811d0ccfSJeremy L Thompson   CeedCall(CeedVectorDestroy(&ones_l_vec));
259575f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
259675f0d5a4SJeremy L Thompson }
259775f0d5a4SJeremy L Thompson 
259875f0d5a4SJeremy L Thompson /**
2599ca94c3ddSJeremy 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.
2600eaf62fffSJeremy L Thompson 
2601ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2602f04ea552SJeremy L Thompson 
2603ca94c3ddSJeremy L Thompson   @param[in]  op_fine      Fine grid `CeedOperator`
2604ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2605ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid `CeedElemRestriction`
2606ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector `CeedBasis`
2607ca94c3ddSJeremy L Thompson   @param[out] op_coarse    Coarse grid `CeedOperator`
2608ca94c3ddSJeremy L Thompson   @param[out] op_prolong   Coarse to fine `CeedOperator`, or `NULL`
2609ca94c3ddSJeremy L Thompson   @param[out] op_restrict  Fine to coarse `CeedOperator`, or `NULL`
2610eaf62fffSJeremy L Thompson 
2611eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2612eaf62fffSJeremy L Thompson 
2613eaf62fffSJeremy L Thompson   @ref User
2614eaf62fffSJeremy L Thompson **/
26152b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
26167758292fSSebastian Grimberg                                      CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
26171c66c397SJeremy L Thompson   CeedBasis basis_c_to_f = NULL;
26181c66c397SJeremy L Thompson 
26192b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2620eaf62fffSJeremy L Thompson 
262183d6adf3SZach Atkins   // Build prolongation matrix, if required
26227758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
262383d6adf3SZach Atkins     CeedBasis basis_fine;
26241c66c397SJeremy L Thompson 
26252b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
26262b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f));
2627681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis_fine));
262883d6adf3SZach Atkins   }
2629eaf62fffSJeremy L Thompson 
2630f113e5dcSJeremy L Thompson   // Core code
26317758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2632eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2633eaf62fffSJeremy L Thompson }
2634eaf62fffSJeremy L Thompson 
2635eaf62fffSJeremy L Thompson /**
2636ca94c3ddSJeremy L Thompson   @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a tensor basis for the active basis.
2637eaf62fffSJeremy L Thompson 
2638ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2639f04ea552SJeremy L Thompson 
2640ca94c3ddSJeremy L Thompson   @param[in]  op_fine       Fine grid `CeedOperator`
2641ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2642ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid `CeedElemRestriction`
2643ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector `CeedBasis`
2644ca94c3ddSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator`
2645ca94c3ddSJeremy L Thompson   @param[out] op_coarse     Coarse grid `CeedOperator`
2646ca94c3ddSJeremy L Thompson   @param[out] op_prolong    Coarse to fine `CeedOperator`, or `NULL`
2647ca94c3ddSJeremy L Thompson   @param[out] op_restrict   Fine to coarse `CeedOperator`, or `NULL`
2648eaf62fffSJeremy L Thompson 
2649eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2650eaf62fffSJeremy L Thompson 
2651eaf62fffSJeremy L Thompson   @ref User
2652eaf62fffSJeremy L Thompson **/
26532b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
26542b730f8bSJeremy L Thompson                                              const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
26557758292fSSebastian Grimberg                                              CeedOperator *op_restrict) {
2656eaf62fffSJeremy L Thompson   Ceed      ceed;
26571c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
26581c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
26591c66c397SJeremy L Thompson 
26601c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
26612b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2662eaf62fffSJeremy L Thompson 
2663eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
26642b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
26652b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
26662b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
26673f08121cSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION,
26683f08121cSJeremy L Thompson             "Bases must have compatible quadrature spaces."
26693f08121cSJeremy L Thompson             " Fine grid: %" CeedInt_FMT " points, Coarse grid: %" CeedInt_FMT " points",
26703f08121cSJeremy L Thompson             Q_f, Q_c);
2671eaf62fffSJeremy L Thompson 
267283d6adf3SZach Atkins   // Create coarse to fine basis, if required
26737758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
26741c66c397SJeremy L Thompson     CeedInt     dim, num_comp, num_nodes_c, P_1d_f, P_1d_c;
26751c66c397SJeremy L Thompson     CeedScalar *q_ref, *q_weight, *grad;
26761c66c397SJeremy L Thompson 
267783d6adf3SZach Atkins     // Check if interpolation matrix is provided
26786574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
26796574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
26802b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
26812b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
26822b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f));
2683681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis_fine));
26842b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
26852b730f8bSJeremy L Thompson     P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c);
26862b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_ref));
26872b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_weight));
26882b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad));
26892b730f8bSJeremy 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));
26902b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
26912b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
26922b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
269383d6adf3SZach Atkins   }
2694eaf62fffSJeremy L Thompson 
2695eaf62fffSJeremy L Thompson   // Core code
26967758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
26979bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
2698eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2699eaf62fffSJeremy L Thompson }
2700eaf62fffSJeremy L Thompson 
2701eaf62fffSJeremy L Thompson /**
2702ca94c3ddSJeremy L Thompson   @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a non-tensor basis for the active vector
2703eaf62fffSJeremy L Thompson 
2704ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2705f04ea552SJeremy L Thompson 
2706ca94c3ddSJeremy L Thompson   @param[in]  op_fine       Fine grid `CeedOperator`
2707ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2708ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid `CeedElemRestriction`
2709ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector `CeedBasis`
2710ca94c3ddSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator`
2711ca94c3ddSJeremy L Thompson   @param[out] op_coarse     Coarse grid `CeedOperator`
2712ca94c3ddSJeremy L Thompson   @param[out] op_prolong    Coarse to fine `CeedOperator`, or `NULL`
2713ca94c3ddSJeremy L Thompson   @param[out] op_restrict   Fine to coarse `CeedOperator`, or `NULL`
2714eaf62fffSJeremy L Thompson 
2715eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2716eaf62fffSJeremy L Thompson 
2717eaf62fffSJeremy L Thompson   @ref User
2718eaf62fffSJeremy L Thompson **/
27192b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
27207758292fSSebastian Grimberg                                        const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
27217758292fSSebastian Grimberg                                        CeedOperator *op_restrict) {
2722eaf62fffSJeremy L Thompson   Ceed      ceed;
27231c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
27241c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
27251c66c397SJeremy L Thompson 
27261c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
27272b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2728eaf62fffSJeremy L Thompson 
2729eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
27302b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
27312b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
27322b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
27336574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2734eaf62fffSJeremy L Thompson 
2735eaf62fffSJeremy L Thompson   // Coarse to fine basis
27367758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
27371c66c397SJeremy L Thompson     CeedInt          dim, num_comp, num_nodes_c, num_nodes_f;
27381c66c397SJeremy L Thompson     CeedScalar      *q_ref, *q_weight, *grad;
27391c66c397SJeremy L Thompson     CeedElemTopology topo;
27401c66c397SJeremy L Thompson 
274183d6adf3SZach Atkins     // Check if interpolation matrix is provided
27426574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
27436574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
27442b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_fine, &topo));
27452b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
27462b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
27472b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f));
2748681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis_fine));
27492b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
27502b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref));
27512b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f, &q_weight));
27522b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad));
27532b730f8bSJeremy 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));
27542b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
27552b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
27562b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
275783d6adf3SZach Atkins   }
2758eaf62fffSJeremy L Thompson 
2759eaf62fffSJeremy L Thompson   // Core code
27607758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
27619bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
2762eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2763eaf62fffSJeremy L Thompson }
2764eaf62fffSJeremy L Thompson 
2765eaf62fffSJeremy L Thompson /**
2766ca94c3ddSJeremy L Thompson   @brief Build a FDM based approximate inverse for each element for a `CeedOperator`.
2767eaf62fffSJeremy L Thompson 
2768ca94c3ddSJeremy L Thompson   This returns a `CeedOperator` and `CeedVector` to apply a Fast Diagonalization Method based approximate inverse.
2769859c15bbSJames 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$.
2770ca94c3ddSJeremy 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$.
2771ca94c3ddSJeremy L Thompson   The `CeedOperator` must be linear and non-composite.
2772ca94c3ddSJeremy L Thompson   The associated `CeedQFunction` must therefore also be linear.
2773eaf62fffSJeremy L Thompson 
2774ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2775f04ea552SJeremy L Thompson 
2776ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to create element inverses
2777ca94c3ddSJeremy L Thompson   @param[out] fdm_inv `CeedOperator` to apply the action of a FDM based inverse for each element
2778ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2779eaf62fffSJeremy L Thompson 
2780eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2781eaf62fffSJeremy L Thompson 
2782480fae85SJeremy L Thompson   @ref User
2783eaf62fffSJeremy L Thompson **/
27842b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) {
27851c66c397SJeremy L Thompson   Ceed                 ceed, ceed_parent;
27861c66c397SJeremy L Thompson   bool                 interp = false, grad = false, is_tensor_basis = true;
27871c66c397SJeremy L Thompson   CeedInt              num_input_fields, P_1d, Q_1d, num_nodes, num_qpts, dim, num_comp = 1, num_elem = 1;
27881c66c397SJeremy L Thompson   CeedScalar          *mass, *laplace, *x, *fdm_interp, *lambda, *elem_avg;
27891c66c397SJeremy L Thompson   const CeedScalar    *interp_1d, *grad_1d, *q_weight_1d;
27901c66c397SJeremy L Thompson   CeedVector           q_data;
27911c66c397SJeremy L Thompson   CeedElemRestriction  rstr  = NULL, rstr_qd_i;
27921c66c397SJeremy L Thompson   CeedBasis            basis = NULL, fdm_basis;
27931c66c397SJeremy L Thompson   CeedQFunctionContext ctx_fdm;
27941c66c397SJeremy L Thompson   CeedQFunctionField  *qf_fields;
27951c66c397SJeremy L Thompson   CeedQFunction        qf, qf_fdm;
27961c66c397SJeremy L Thompson   CeedOperatorField   *op_fields;
27971c66c397SJeremy L Thompson 
27982b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2799eaf62fffSJeremy L Thompson 
2800eaf62fffSJeremy L Thompson   if (op->CreateFDMElementInverse) {
2801d04bbc78SJeremy L Thompson     // Backend version
28022b730f8bSJeremy L Thompson     CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request));
2803eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2804eaf62fffSJeremy L Thompson   } else {
2805d04bbc78SJeremy L Thompson     // Operator fallback
2806d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2807d04bbc78SJeremy L Thompson 
28082b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2809d04bbc78SJeremy L Thompson     if (op_fallback) {
28102b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request));
2811eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2812eaf62fffSJeremy L Thompson     }
2813eaf62fffSJeremy L Thompson   }
2814eaf62fffSJeremy L Thompson 
2815d04bbc78SJeremy L Thompson   // Default interface implementation
28162b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
2817bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
28182b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
2819eaf62fffSJeremy L Thompson 
2820eaf62fffSJeremy L Thompson   // Determine active input basis
28212b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL));
28222b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
2823eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
2824eaf62fffSJeremy L Thompson     CeedVector vec;
28251c66c397SJeremy L Thompson 
28262b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
2827eaf62fffSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2828eaf62fffSJeremy L Thompson       CeedEvalMode eval_mode;
28291c66c397SJeremy L Thompson 
28302b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
2831eaf62fffSJeremy L Thompson       interp = interp || eval_mode == CEED_EVAL_INTERP;
2832eaf62fffSJeremy L Thompson       grad   = grad || eval_mode == CEED_EVAL_GRAD;
2833681d0ea7SJeremy L Thompson       if (!basis) CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis));
2834681d0ea7SJeremy L Thompson       if (!rstr) CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
2835eaf62fffSJeremy L Thompson     }
2836681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
2837eaf62fffSJeremy L Thompson   }
28386574a04fSJeremy L Thompson   CeedCheck(basis, ceed, CEED_ERROR_BACKEND, "No active field set");
28392b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
2840352a5e7cSSebastian Grimberg   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
28412b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
28422b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
28432b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
28442b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
28452b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
2846eaf62fffSJeremy L Thompson 
2847eaf62fffSJeremy L Thompson   // Build and diagonalize 1D Mass and Laplacian
28486574a04fSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
28496574a04fSJeremy L Thompson   CeedCheck(is_tensor_basis, ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases");
28502b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &mass));
28512b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &laplace));
28522b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &x));
28532b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp));
28542b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &lambda));
2855eaf62fffSJeremy L Thompson   // -- Build matrices
28562b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
28572b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
28582b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
28592b730f8bSJeremy L Thompson   CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace));
2860eaf62fffSJeremy L Thompson 
2861eaf62fffSJeremy L Thompson   // -- Diagonalize
28622b730f8bSJeremy L Thompson   CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d));
28632b730f8bSJeremy L Thompson   CeedCall(CeedFree(&mass));
28642b730f8bSJeremy L Thompson   CeedCall(CeedFree(&laplace));
28652b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
28662b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d];
28672b730f8bSJeremy L Thompson   }
28682b730f8bSJeremy L Thompson   CeedCall(CeedFree(&x));
2869eaf62fffSJeremy L Thompson 
28701c66c397SJeremy L Thompson   {
28711c66c397SJeremy L Thompson     CeedInt             layout[3], num_modes = (interp ? 1 : 0) + (grad ? dim : 0);
28721c66c397SJeremy L Thompson     CeedScalar          max_norm = 0;
28731c66c397SJeremy L Thompson     const CeedScalar   *assembled_array, *q_weight_array;
28741c66c397SJeremy L Thompson     CeedVector          assembled = NULL, q_weight;
2875c5f45aeaSJeremy L Thompson     CeedElemRestriction rstr_qf   = NULL;
28761c66c397SJeremy L Thompson 
28771c66c397SJeremy L Thompson     // Assemble QFunction
28782b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request));
287956c48462SJeremy L Thompson     CeedCall(CeedElemRestrictionGetELayout(rstr_qf, layout));
28802b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr_qf));
28812b730f8bSJeremy L Thompson     CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm));
2882eaf62fffSJeremy L Thompson 
2883eaf62fffSJeremy L Thompson     // Calculate element averages
28842b730f8bSJeremy L Thompson     CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight));
28852b730f8bSJeremy L Thompson     CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight));
28862b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array));
28872b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array));
28882b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_elem, &elem_avg));
2889eaf62fffSJeremy L Thompson     const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON;
28901c66c397SJeremy L Thompson 
2891eaf62fffSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
2892eaf62fffSJeremy L Thompson       CeedInt count = 0;
28931c66c397SJeremy L Thompson 
28942b730f8bSJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
28952b730f8bSJeremy L Thompson         for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) {
28962b730f8bSJeremy L Thompson           if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) {
28972b730f8bSJeremy L Thompson             elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q];
2898eaf62fffSJeremy L Thompson             count++;
2899eaf62fffSJeremy L Thompson           }
29002b730f8bSJeremy L Thompson         }
29012b730f8bSJeremy L Thompson       }
2902eaf62fffSJeremy L Thompson       if (count) {
2903eaf62fffSJeremy L Thompson         elem_avg[e] /= count;
2904eaf62fffSJeremy L Thompson       } else {
2905eaf62fffSJeremy L Thompson         elem_avg[e] = 1.0;
2906eaf62fffSJeremy L Thompson       }
2907eaf62fffSJeremy L Thompson     }
29082b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array));
29092b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled));
29102b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array));
29112b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&q_weight));
29121c66c397SJeremy L Thompson   }
2913eaf62fffSJeremy L Thompson 
2914eaf62fffSJeremy L Thompson   // Build FDM diagonal
29151c66c397SJeremy L Thompson   {
2916eaf62fffSJeremy L Thompson     CeedScalar *q_data_array, *fdm_diagonal;
29171c66c397SJeremy L Thompson 
2918352a5e7cSSebastian Grimberg     CeedCall(CeedCalloc(num_comp * num_nodes, &fdm_diagonal));
2919352a5e7cSSebastian Grimberg     const CeedScalar fdm_diagonal_bound = num_nodes * CEED_EPSILON;
29202b730f8bSJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
2921352a5e7cSSebastian Grimberg       for (CeedInt n = 0; n < num_nodes; n++) {
2922352a5e7cSSebastian Grimberg         if (interp) fdm_diagonal[c * num_nodes + n] = 1.0;
29232b730f8bSJeremy L Thompson         if (grad) {
2924eaf62fffSJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
2925eaf62fffSJeremy L Thompson             CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d;
2926352a5e7cSSebastian Grimberg             fdm_diagonal[c * num_nodes + n] += lambda[i];
2927eaf62fffSJeremy L Thompson           }
2928eaf62fffSJeremy L Thompson         }
2929352a5e7cSSebastian Grimberg         if (fabs(fdm_diagonal[c * num_nodes + n]) < fdm_diagonal_bound) fdm_diagonal[c * num_nodes + n] = fdm_diagonal_bound;
29302b730f8bSJeremy L Thompson       }
29312b730f8bSJeremy L Thompson     }
2932352a5e7cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * num_nodes, &q_data));
29332b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(q_data, 0.0));
29342b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array));
29352b730f8bSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
29362b730f8bSJeremy L Thompson       for (CeedInt c = 0; c < num_comp; c++) {
29376c10af5dSJeremy L Thompson         for (CeedInt n = 0; n < num_nodes; n++) {
29381c66c397SJeremy L Thompson           q_data_array[(e * num_comp + c) * num_nodes + n] = 1. / (elem_avg[e] * fdm_diagonal[c * num_nodes + n]);
29392b730f8bSJeremy L Thompson         }
29402b730f8bSJeremy L Thompson       }
29416c10af5dSJeremy L Thompson     }
29422b730f8bSJeremy L Thompson     CeedCall(CeedFree(&elem_avg));
29432b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_diagonal));
29442b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(q_data, &q_data_array));
29451c66c397SJeremy L Thompson   }
2946eaf62fffSJeremy L Thompson 
2947eaf62fffSJeremy L Thompson   // Setup FDM operator
2948eaf62fffSJeremy L Thompson   // -- Basis
29491c66c397SJeremy L Thompson   {
2950eaf62fffSJeremy L Thompson     CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy;
29511c66c397SJeremy L Thompson 
29522b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy));
29532b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_ref_dummy));
29542b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_weight_dummy));
29552b730f8bSJeremy 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));
29562b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_interp));
29572b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad_dummy));
29582b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref_dummy));
29592b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight_dummy));
29602b730f8bSJeremy L Thompson     CeedCall(CeedFree(&lambda));
29611c66c397SJeremy L Thompson   }
2962eaf62fffSJeremy L Thompson 
2963eaf62fffSJeremy L Thompson   // -- Restriction
29641c66c397SJeremy L Thompson   {
2965352a5e7cSSebastian Grimberg     CeedInt strides[3] = {1, num_nodes, num_nodes * num_comp};
29660a5597ceSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, num_nodes, num_comp,
29670a5597ceSJeremy L Thompson                                               (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_nodes, strides, &rstr_qd_i));
29681c66c397SJeremy L Thompson   }
29691c66c397SJeremy L Thompson 
2970eaf62fffSJeremy L Thompson   // -- QFunction
29712b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm));
29722b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP));
29732b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE));
29742b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP));
29752b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp));
29761c66c397SJeremy L Thompson 
2977eaf62fffSJeremy L Thompson   // -- QFunction context
29781c66c397SJeremy L Thompson   {
2979eaf62fffSJeremy L Thompson     CeedInt *num_comp_data;
29801c66c397SJeremy L Thompson 
29812b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_data));
2982eaf62fffSJeremy L Thompson     num_comp_data[0] = num_comp;
29832b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm));
29842b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data));
29851c66c397SJeremy L Thompson   }
29862b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm));
29872b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_fdm));
29881c66c397SJeremy L Thompson 
2989eaf62fffSJeremy L Thompson   // -- Operator
29902b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv));
29912b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2992356036faSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_NONE, q_data));
29932b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2994eaf62fffSJeremy L Thompson 
2995eaf62fffSJeremy L Thompson   // Cleanup
29969bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
29979bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed_parent));
29982b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_data));
2999681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr));
30002b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i));
3001681d0ea7SJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis));
3002681d0ea7SJeremy L Thompson   CeedCall(CeedBasisDestroy(&fdm_basis));
3003c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
30042b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_fdm));
3005eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
3006eaf62fffSJeremy L Thompson }
3007eaf62fffSJeremy L Thompson 
3008eaf62fffSJeremy L Thompson /// @}
3009