xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-preconditioning.c (revision 5aed82e4fa97acf4ba24a7f10a35f5303a6798e0)
1*5aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3eaf62fffSJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5eaf62fffSJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7eaf62fffSJeremy L Thompson 
82b730f8bSJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
11c85e8640SSebastian Grimberg #include <assert.h>
122b730f8bSJeremy L Thompson #include <math.h>
13eaf62fffSJeremy L Thompson #include <stdbool.h>
14eaf62fffSJeremy L Thompson #include <stdio.h>
15eaf62fffSJeremy L Thompson #include <string.h>
16eaf62fffSJeremy L Thompson 
17eaf62fffSJeremy L Thompson /// @file
18eaf62fffSJeremy L Thompson /// Implementation of CeedOperator preconditioning interfaces
19eaf62fffSJeremy L Thompson 
20eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
21eaf62fffSJeremy L Thompson /// CeedOperator Library Internal Preconditioning Functions
22eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
23eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorDeveloper
24eaf62fffSJeremy L Thompson /// @{
25eaf62fffSJeremy L Thompson 
26eaf62fffSJeremy L Thompson /**
27ca94c3ddSJeremy L Thompson   @brief Duplicate a `CeedQFunction` with a reference `Ceed` to fallback for advanced `CeedOperator` functionality
289e77b9c8SJeremy L Thompson 
29ca94c3ddSJeremy L Thompson   @param[in]  fallback_ceed `Ceed` on which to create fallback `CeedQFunction`
30ca94c3ddSJeremy L Thompson   @param[in]  qf            `CeedQFunction` to create fallback for
31ca94c3ddSJeremy L Thompson   @param[out] qf_fallback   Fallback `CeedQFunction`
329e77b9c8SJeremy L Thompson 
339e77b9c8SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
349e77b9c8SJeremy L Thompson 
359e77b9c8SJeremy L Thompson   @ref Developer
369e77b9c8SJeremy L Thompson **/
372b730f8bSJeremy L Thompson static int CeedQFunctionCreateFallback(Ceed fallback_ceed, CeedQFunction qf, CeedQFunction *qf_fallback) {
381c66c397SJeremy L Thompson   char               *source_path_with_name = NULL;
391203703bSJeremy L Thompson   CeedInt             num_input_fields, num_output_fields;
401203703bSJeremy L Thompson   Ceed                ceed;
411203703bSJeremy L Thompson   CeedQFunctionField *input_fields, *output_fields;
421c66c397SJeremy L Thompson 
439e77b9c8SJeremy L Thompson   // Check if NULL qf passed in
449e77b9c8SJeremy L Thompson   if (!qf) return CEED_ERROR_SUCCESS;
459e77b9c8SJeremy L Thompson 
461203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetCeed(qf, &ceed));
471203703bSJeremy L Thompson   CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n");
481203703bSJeremy L Thompson   CeedDebug(ceed, "Creating fallback CeedQFunction\n");
49d04bbc78SJeremy L Thompson 
509e77b9c8SJeremy L Thompson   if (qf->source_path) {
512b730f8bSJeremy L Thompson     size_t path_len = strlen(qf->source_path), name_len = strlen(qf->kernel_name);
522b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(path_len + name_len + 2, &source_path_with_name));
539e77b9c8SJeremy L Thompson     memcpy(source_path_with_name, qf->source_path, path_len);
549e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len], ":", 1);
559e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len + 1], qf->kernel_name, name_len);
569e77b9c8SJeremy L Thompson   } else {
572b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &source_path_with_name));
589e77b9c8SJeremy L Thompson   }
599e77b9c8SJeremy L Thompson 
601203703bSJeremy L Thompson   {
611203703bSJeremy L Thompson     CeedInt           vec_length;
621203703bSJeremy L Thompson     CeedQFunctionUser f;
631203703bSJeremy L Thompson 
641203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length));
651203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetUserFunction(qf, &f));
661203703bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(fallback_ceed, vec_length, f, source_path_with_name, qf_fallback));
671203703bSJeremy L Thompson   }
689e77b9c8SJeremy L Thompson   {
699e77b9c8SJeremy L Thompson     CeedQFunctionContext ctx;
709e77b9c8SJeremy L Thompson 
712b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
722b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx));
739e77b9c8SJeremy L Thompson   }
741203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
751203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
766f8994e9SJeremy L Thompson     const char  *field_name;
771203703bSJeremy L Thompson     CeedInt      size;
781203703bSJeremy L Thompson     CeedEvalMode eval_mode;
791203703bSJeremy L Thompson 
80ab747706SJeremy L Thompson     CeedCall(CeedQFunctionFieldGetData(input_fields[i], &field_name, &size, &eval_mode));
811203703bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(*qf_fallback, field_name, size, eval_mode));
829e77b9c8SJeremy L Thompson   }
831203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
846f8994e9SJeremy L Thompson     const char  *field_name;
851203703bSJeremy L Thompson     CeedInt      size;
861203703bSJeremy L Thompson     CeedEvalMode eval_mode;
871203703bSJeremy L Thompson 
88ab747706SJeremy L Thompson     CeedCall(CeedQFunctionFieldGetData(output_fields[i], &field_name, &size, &eval_mode));
891203703bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(*qf_fallback, field_name, size, eval_mode));
909e77b9c8SJeremy L Thompson   }
912b730f8bSJeremy L Thompson   CeedCall(CeedFree(&source_path_with_name));
929e77b9c8SJeremy L Thompson   return CEED_ERROR_SUCCESS;
939e77b9c8SJeremy L Thompson }
949e77b9c8SJeremy L Thompson 
959e77b9c8SJeremy L Thompson /**
96ca94c3ddSJeremy L Thompson   @brief Duplicate a `CeedOperator` with a reference `Ceed` to fallback for advanced `CeedOperator` functionality
97eaf62fffSJeremy L Thompson 
98ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to create fallback for
99eaf62fffSJeremy L Thompson 
100eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
101eaf62fffSJeremy L Thompson 
102eaf62fffSJeremy L Thompson   @ref Developer
103eaf62fffSJeremy L Thompson **/
104d04bbc78SJeremy L Thompson static int CeedOperatorCreateFallback(CeedOperator op) {
1051c66c397SJeremy L Thompson   bool         is_composite;
1061203703bSJeremy L Thompson   Ceed         ceed, ceed_fallback;
1071c66c397SJeremy L Thompson   CeedOperator op_fallback;
108eaf62fffSJeremy L Thompson 
109805fe78eSJeremy L Thompson   // Check not already created
110805fe78eSJeremy L Thompson   if (op->op_fallback) return CEED_ERROR_SUCCESS;
111805fe78eSJeremy L Thompson 
112eaf62fffSJeremy L Thompson   // Fallback Ceed
1131203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1141203703bSJeremy L Thompson   CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
115d04bbc78SJeremy L Thompson   if (!ceed_fallback) return CEED_ERROR_SUCCESS;
116d04bbc78SJeremy L Thompson 
1171203703bSJeremy L Thompson   CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n");
1181203703bSJeremy L Thompson   CeedDebug(ceed, "Creating fallback CeedOperator\n");
119eaf62fffSJeremy L Thompson 
120eaf62fffSJeremy L Thompson   // Clone Op
121b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
122b275c451SJeremy L Thompson   if (is_composite) {
123b275c451SJeremy L Thompson     CeedInt       num_suboperators;
124b275c451SJeremy L Thompson     CeedOperator *sub_operators;
125b275c451SJeremy L Thompson 
1262b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorCreate(ceed_fallback, &op_fallback));
127b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
128b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
129b275c451SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
130d04bbc78SJeremy L Thompson       CeedOperator op_sub_fallback;
131d04bbc78SJeremy L Thompson 
132b275c451SJeremy L Thompson       CeedCall(CeedOperatorGetFallback(sub_operators[i], &op_sub_fallback));
1332b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorAddSub(op_fallback, op_sub_fallback));
134805fe78eSJeremy L Thompson     }
135805fe78eSJeremy L Thompson   } else {
1361203703bSJeremy L Thompson     CeedInt            num_input_fields, num_output_fields;
1379e77b9c8SJeremy L Thompson     CeedQFunction      qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL;
1381203703bSJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
1391c66c397SJeremy L Thompson 
1402b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback));
1412b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback));
1422b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback));
1432b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback));
1441203703bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1451203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1466f8994e9SJeremy L Thompson       const char         *field_name;
1471203703bSJeremy L Thompson       CeedVector          vec;
1481203703bSJeremy L Thompson       CeedElemRestriction rstr;
1491203703bSJeremy L Thompson       CeedBasis           basis;
1501203703bSJeremy L Thompson 
151ab747706SJeremy L Thompson       CeedCall(CeedOperatorFieldGetData(input_fields[i], &field_name, &rstr, &basis, &vec));
1521203703bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec));
153805fe78eSJeremy L Thompson     }
1541203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1556f8994e9SJeremy L Thompson       const char         *field_name;
1561203703bSJeremy L Thompson       CeedVector          vec;
1571203703bSJeremy L Thompson       CeedElemRestriction rstr;
1581203703bSJeremy L Thompson       CeedBasis           basis;
1591203703bSJeremy L Thompson 
160ab747706SJeremy L Thompson       CeedCall(CeedOperatorFieldGetData(output_fields[i], &field_name, &rstr, &basis, &vec));
1611203703bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec));
162805fe78eSJeremy L Thompson     }
1632b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op->qf_assembled, &op_fallback->qf_assembled));
1649e77b9c8SJeremy L Thompson     // Cleanup
1652b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf_fallback));
1662b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqf_fallback));
1672b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqfT_fallback));
168805fe78eSJeremy L Thompson   }
1692b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetName(op_fallback, op->name));
1702b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fallback));
171b05f7e9fSJeremy L Thompson   // Note: No ref-counting here so we don't get caught in a reference loop.
172b05f7e9fSJeremy L Thompson   //       The op holds the only reference to op_fallback and is responsible for deleting itself and op_fallback.
173805fe78eSJeremy L Thompson   op->op_fallback                 = op_fallback;
174b05f7e9fSJeremy L Thompson   op_fallback->op_fallback_parent = op;
175eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
176eaf62fffSJeremy L Thompson }
177eaf62fffSJeremy L Thompson 
178eaf62fffSJeremy L Thompson /**
179eaf62fffSJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
180eaf62fffSJeremy L Thompson 
1810cd9fdf4SJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble diagonal or point block diagonal
182ca94c3ddSJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
183bd83916cSSebastian Grimberg   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
184ca94c3ddSJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
185eaf62fffSJeremy L Thompson 
186eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
187eaf62fffSJeremy L Thompson 
188eaf62fffSJeremy L Thompson   @ref Developer
189eaf62fffSJeremy L Thompson **/
1900cd9fdf4SJeremy L Thompson static inline int CeedSingleOperatorLinearAssembleAddDiagonal_Mesh(CeedOperator op, CeedRequest *request, const bool is_point_block,
191f3bd9308SJeremy L Thompson                                                                    CeedVector assembled) {
192eaf62fffSJeremy L Thompson   Ceed ceed;
193506b1a0cSSebastian Grimberg   bool is_composite;
194506b1a0cSSebastian Grimberg 
195506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetCeed(op, &ceed));
196506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorIsComposite(op, &is_composite));
197506b1a0cSSebastian Grimberg   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
198506b1a0cSSebastian Grimberg 
199506b1a0cSSebastian Grimberg   // Assemble QFunction
200506b1a0cSSebastian Grimberg   CeedInt             layout_qf[3];
201437c7c90SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
202c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
203c5f45aeaSJeremy L Thompson   CeedElemRestriction assembled_elem_rstr = NULL;
204437c7c90SJeremy L Thompson 
205437c7c90SJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request));
20656c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf));
207437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
208437c7c90SJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
209eaf62fffSJeremy L Thompson 
210ed9e99e6SJeremy L Thompson   // Get assembly data
211437c7c90SJeremy L Thompson   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
212506b1a0cSSebastian Grimberg   CeedInt                  num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out;
213437c7c90SJeremy L Thompson   CeedSize               **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components;
214506b1a0cSSebastian Grimberg   CeedBasis               *active_bases_in, *active_bases_out;
215506b1a0cSSebastian Grimberg   CeedElemRestriction     *active_elem_rstrs_in, *active_elem_rstrs_out;
2161c66c397SJeremy L Thompson   CeedOperatorAssemblyData data;
2171c66c397SJeremy L Thompson 
218437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
219506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in,
220506b1a0cSSebastian Grimberg                                                 &num_active_bases_out, &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out,
221506b1a0cSSebastian Grimberg                                                 &num_output_components));
222506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, NULL, NULL, &active_bases_out, NULL));
223506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs_in, NULL, &active_elem_rstrs_out));
224506b1a0cSSebastian Grimberg 
225934a29f5SSebastian Grimberg   // Loop over all active bases (find matching input/output pairs)
226934a29f5SSebastian Grimberg   for (CeedInt b = 0; b < CeedIntMin(num_active_bases_in, num_active_bases_out); b++) {
227934a29f5SSebastian Grimberg     CeedInt             b_in, b_out, num_elem, num_nodes, num_qpts, num_comp;
2281c66c397SJeremy L Thompson     bool                has_eval_none = false;
2291c66c397SJeremy L Thompson     CeedScalar         *elem_diag_array, *identity = NULL;
2301c66c397SJeremy L Thompson     CeedVector          elem_diag;
2317c1dbaffSSebastian Grimberg     CeedElemRestriction diag_elem_rstr;
2321c66c397SJeremy L Thompson 
233934a29f5SSebastian Grimberg     if (num_active_bases_in <= num_active_bases_out) {
234934a29f5SSebastian Grimberg       b_in = b;
235934a29f5SSebastian Grimberg       for (b_out = 0; b_out < num_active_bases_out; b_out++) {
236934a29f5SSebastian Grimberg         if (active_bases_in[b_in] == active_bases_out[b_out]) {
237934a29f5SSebastian Grimberg           break;
238934a29f5SSebastian Grimberg         }
239934a29f5SSebastian Grimberg       }
240934a29f5SSebastian Grimberg       if (b_out == num_active_bases_out) {
241934a29f5SSebastian Grimberg         continue;
242934a29f5SSebastian Grimberg       }  // No matching output basis found
243934a29f5SSebastian Grimberg     } else {
244934a29f5SSebastian Grimberg       b_out = b;
245934a29f5SSebastian Grimberg       for (b_in = 0; b_in < num_active_bases_in; b_in++) {
246934a29f5SSebastian Grimberg         if (active_bases_in[b_in] == active_bases_out[b_out]) {
247934a29f5SSebastian Grimberg           break;
248934a29f5SSebastian Grimberg         }
249934a29f5SSebastian Grimberg       }
250934a29f5SSebastian Grimberg       if (b_in == num_active_bases_in) {
251934a29f5SSebastian Grimberg         continue;
252934a29f5SSebastian Grimberg       }  // No matching output basis found
253934a29f5SSebastian Grimberg     }
254934a29f5SSebastian Grimberg     CeedCheck(active_elem_rstrs_in[b_in] == active_elem_rstrs_out[b_out], ceed, CEED_ERROR_UNSUPPORTED,
255506b1a0cSSebastian Grimberg               "Cannot assemble operator diagonal with different input and output active element restrictions");
256506b1a0cSSebastian Grimberg 
2571c66c397SJeremy L Thompson     // Assemble point block diagonal restriction, if needed
258bd83916cSSebastian Grimberg     if (is_point_block) {
259934a29f5SSebastian Grimberg       CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstrs_in[b_in], &diag_elem_rstr));
2607c1dbaffSSebastian Grimberg     } else {
261934a29f5SSebastian Grimberg       CeedCall(CeedElemRestrictionCreateUnsignedCopy(active_elem_rstrs_in[b_in], &diag_elem_rstr));
262eaf62fffSJeremy L Thompson     }
263eaf62fffSJeremy L Thompson 
264eaf62fffSJeremy L Thompson     // Create diagonal vector
265437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag));
266eaf62fffSJeremy L Thompson 
267eaf62fffSJeremy L Thompson     // Assemble element operator diagonals
2682b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(elem_diag, 0.0));
2692b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array));
270437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem));
271934a29f5SSebastian Grimberg     CeedCall(CeedBasisGetNumNodes(active_bases_in[b_in], &num_nodes));
272934a29f5SSebastian Grimberg     CeedCall(CeedBasisGetNumComponents(active_bases_in[b_in], &num_comp));
273934a29f5SSebastian Grimberg     if (active_bases_in[b_in] == CEED_BASIS_NONE) num_qpts = num_nodes;
274934a29f5SSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(active_bases_in[b_in], &num_qpts));
275ed9e99e6SJeremy L Thompson 
276352a5e7cSSebastian Grimberg     // Construct identity matrix for basis if required
277934a29f5SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in[b_in]; i++) {
278934a29f5SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[b_in][i] == CEED_EVAL_NONE);
279ed9e99e6SJeremy L Thompson     }
280934a29f5SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out[b_out]; i++) {
281934a29f5SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[b_out][i] == CEED_EVAL_NONE);
282ed9e99e6SJeremy L Thompson     }
283ed9e99e6SJeremy L Thompson     if (has_eval_none) {
2842b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
2852b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
286eaf62fffSJeremy L Thompson     }
287352a5e7cSSebastian Grimberg 
288eaf62fffSJeremy L Thompson     // Compute the diagonal of B^T D B
289eaf62fffSJeremy L Thompson     // Each element
290b94338b9SJed Brown     for (CeedSize e = 0; e < num_elem; e++) {
291eaf62fffSJeremy L Thompson       // Each basis eval mode pair
292352a5e7cSSebastian Grimberg       CeedInt      d_out              = 0, q_comp_out;
293352a5e7cSSebastian Grimberg       CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
2941c66c397SJeremy L Thompson 
295934a29f5SSebastian Grimberg       for (CeedInt e_out = 0; e_out < num_eval_modes_out[b_out]; e_out++) {
2961c66c397SJeremy L Thompson         CeedInt           d_in              = 0, q_comp_in;
297437c7c90SJeremy L Thompson         const CeedScalar *B_t               = NULL;
2981c66c397SJeremy L Thompson         CeedEvalMode      eval_mode_in_prev = CEED_EVAL_NONE;
2991c66c397SJeremy L Thompson 
300934a29f5SSebastian Grimberg         CeedCall(CeedOperatorGetBasisPointer(active_bases_out[b_out], eval_modes_out[b_out][e_out], identity, &B_t));
301934a29f5SSebastian Grimberg         CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_out[b_out], eval_modes_out[b_out][e_out], &q_comp_out));
302352a5e7cSSebastian Grimberg         if (q_comp_out > 1) {
303934a29f5SSebastian Grimberg           if (e_out == 0 || eval_modes_out[b_out][e_out] != eval_mode_out_prev) d_out = 0;
304352a5e7cSSebastian Grimberg           else B_t = &B_t[(++d_out) * num_qpts * num_nodes];
305352a5e7cSSebastian Grimberg         }
306934a29f5SSebastian Grimberg         eval_mode_out_prev = eval_modes_out[b_out][e_out];
307352a5e7cSSebastian Grimberg 
308934a29f5SSebastian Grimberg         for (CeedInt e_in = 0; e_in < num_eval_modes_in[b_in]; e_in++) {
309437c7c90SJeremy L Thompson           const CeedScalar *B = NULL;
3101c66c397SJeremy L Thompson 
311934a29f5SSebastian Grimberg           CeedCall(CeedOperatorGetBasisPointer(active_bases_in[b_in], eval_modes_in[b_in][e_in], identity, &B));
312934a29f5SSebastian Grimberg           CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_in[b_in], eval_modes_in[b_in][e_in], &q_comp_in));
313352a5e7cSSebastian Grimberg           if (q_comp_in > 1) {
314934a29f5SSebastian Grimberg             if (e_in == 0 || eval_modes_in[b_in][e_in] != eval_mode_in_prev) d_in = 0;
315352a5e7cSSebastian Grimberg             else B = &B[(++d_in) * num_qpts * num_nodes];
316352a5e7cSSebastian Grimberg           }
317934a29f5SSebastian Grimberg           eval_mode_in_prev = eval_modes_in[b_in][e_in];
318352a5e7cSSebastian Grimberg 
319eaf62fffSJeremy L Thompson           // Each component
320506b1a0cSSebastian Grimberg           for (CeedInt c_out = 0; c_out < num_comp; c_out++) {
321437c7c90SJeremy L Thompson             // Each qpt/node pair
3222b730f8bSJeremy L Thompson             for (CeedInt q = 0; q < num_qpts; q++) {
323bd83916cSSebastian Grimberg               if (is_point_block) {
324eaf62fffSJeremy L Thompson                 // Point Block Diagonal
325506b1a0cSSebastian Grimberg                 for (CeedInt c_in = 0; c_in < num_comp; c_in++) {
326934a29f5SSebastian Grimberg                   const CeedSize c_offset =
327934a29f5SSebastian Grimberg                       (eval_mode_offsets_in[b_in][e_in] + c_in) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out;
328506b1a0cSSebastian Grimberg                   const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]];
3291c66c397SJeremy L Thompson 
3302b730f8bSJeremy L Thompson                   for (CeedInt n = 0; n < num_nodes; n++) {
331506b1a0cSSebastian Grimberg                     elem_diag_array[((e * num_comp + c_out) * num_comp + c_in) * num_nodes + n] +=
332437c7c90SJeremy L Thompson                         B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n];
333eaf62fffSJeremy L Thompson                   }
3342b730f8bSJeremy L Thompson                 }
335eaf62fffSJeremy L Thompson               } else {
336eaf62fffSJeremy L Thompson                 // Diagonal Only
337934a29f5SSebastian Grimberg                 const CeedInt c_offset =
338934a29f5SSebastian Grimberg                     (eval_mode_offsets_in[b_in][e_in] + c_out) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out;
339506b1a0cSSebastian Grimberg                 const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]];
3401c66c397SJeremy L Thompson 
3412b730f8bSJeremy L Thompson                 for (CeedInt n = 0; n < num_nodes; n++) {
342506b1a0cSSebastian 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];
343eaf62fffSJeremy L Thompson                 }
344eaf62fffSJeremy L Thompson               }
345eaf62fffSJeremy L Thompson             }
346eaf62fffSJeremy L Thompson           }
3472b730f8bSJeremy L Thompson         }
3482b730f8bSJeremy L Thompson       }
3492b730f8bSJeremy L Thompson     }
3502b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
351eaf62fffSJeremy L Thompson 
352eaf62fffSJeremy L Thompson     // Assemble local operator diagonal
3537c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
354eaf62fffSJeremy L Thompson 
355eaf62fffSJeremy L Thompson     // Cleanup
3567c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr));
3572b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&elem_diag));
3582b730f8bSJeremy L Thompson     CeedCall(CeedFree(&identity));
359437c7c90SJeremy L Thompson   }
360437c7c90SJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
361437c7c90SJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
362eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
363eaf62fffSJeremy L Thompson }
364eaf62fffSJeremy L Thompson 
365eaf62fffSJeremy L Thompson /**
3660cd9fdf4SJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
3670cd9fdf4SJeremy L Thompson 
3680cd9fdf4SJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble diagonal or point block diagonal
3690cd9fdf4SJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
3700cd9fdf4SJeremy L Thompson   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
3710cd9fdf4SJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
3720cd9fdf4SJeremy L Thompson 
3730cd9fdf4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3740cd9fdf4SJeremy L Thompson 
3750cd9fdf4SJeremy L Thompson   @ref Developer
3760cd9fdf4SJeremy L Thompson **/
3770cd9fdf4SJeremy L Thompson static inline int CeedSingleOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block,
3780cd9fdf4SJeremy L Thompson                                                               CeedVector assembled) {
3790cd9fdf4SJeremy L Thompson   Ceed ceed;
3800cd9fdf4SJeremy L Thompson   bool is_at_points;
3810cd9fdf4SJeremy L Thompson 
3820cd9fdf4SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
3830cd9fdf4SJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
38442461424SJeremy L Thompson   CeedCheck(!is_at_points, ceed, CEED_ERROR_UNSUPPORTED, "AtPoints operator not supported");
3850cd9fdf4SJeremy L Thompson   CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal_Mesh(op, request, is_point_block, assembled));
3860cd9fdf4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3870cd9fdf4SJeremy L Thompson }
3880cd9fdf4SJeremy L Thompson 
3890cd9fdf4SJeremy L Thompson /**
390eaf62fffSJeremy L Thompson   @brief Core logic for assembling composite operator diagonal
391eaf62fffSJeremy L Thompson 
392ca94c3ddSJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble point block diagonal
393ca94c3ddSJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
394bd83916cSSebastian Grimberg   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
395ca94c3ddSJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
396eaf62fffSJeremy L Thompson 
397eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
398eaf62fffSJeremy L Thompson 
399eaf62fffSJeremy L Thompson   @ref Developer
400eaf62fffSJeremy L Thompson **/
401bd83916cSSebastian Grimberg static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block,
402eaf62fffSJeremy L Thompson                                                                  CeedVector assembled) {
403eaf62fffSJeremy L Thompson   CeedInt       num_sub;
404eaf62fffSJeremy L Thompson   CeedOperator *suboperators;
4051c66c397SJeremy L Thompson 
406c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
407c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators));
408eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_sub; i++) {
409bd83916cSSebastian Grimberg     if (is_point_block) {
4102b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request));
4116aa95790SJeremy L Thompson     } else {
4122b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request));
4136aa95790SJeremy L Thompson     }
414eaf62fffSJeremy L Thompson   }
415eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
416eaf62fffSJeremy L Thompson }
417eaf62fffSJeremy L Thompson 
418eaf62fffSJeremy L Thompson /**
419ca94c3ddSJeremy L Thompson   @brief Build nonzero pattern for non-composite CeedOperator`.
420eaf62fffSJeremy L Thompson 
421ca94c3ddSJeremy L Thompson   Users should generally use @ref CeedOperatorLinearAssembleSymbolic().
422eaf62fffSJeremy L Thompson 
423ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` to assemble nonzero pattern
424eaf62fffSJeremy L Thompson   @param[in]  offset Offset for number of entries
425eaf62fffSJeremy L Thompson   @param[out] rows   Row number for each entry
426eaf62fffSJeremy L Thompson   @param[out] cols   Column number for each entry
427eaf62fffSJeremy L Thompson 
428eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
429eaf62fffSJeremy L Thompson 
430eaf62fffSJeremy L Thompson   @ref Developer
431eaf62fffSJeremy L Thompson **/
4322b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) {
433f3d47e36SJeremy L Thompson   Ceed                ceed;
434f3d47e36SJeremy L Thompson   bool                is_composite;
43581670346SSebastian Grimberg   CeedSize            num_nodes_in, num_nodes_out, local_num_entries, count = 0;
436506b1a0cSSebastian Grimberg   CeedInt             num_elem_in, elem_size_in, num_comp_in, layout_er_in[3];
43781670346SSebastian Grimberg   CeedInt             num_elem_out, elem_size_out, num_comp_out, layout_er_out[3];
4381c66c397SJeremy L Thompson   CeedScalar         *array;
439506b1a0cSSebastian Grimberg   const CeedScalar   *elem_dof_a_in, *elem_dof_a_out;
440506b1a0cSSebastian Grimberg   CeedVector          index_vec_in, index_vec_out, elem_dof_in, elem_dof_out;
441506b1a0cSSebastian Grimberg   CeedElemRestriction elem_rstr_in, elem_rstr_out, index_elem_rstr_in, index_elem_rstr_out;
4421c66c397SJeremy L Thompson 
443f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
444f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4456574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
446eaf62fffSJeremy L Thompson 
447506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes_in, &num_nodes_out));
448506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out));
449506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in));
450506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in));
451506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in));
45256c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(elem_rstr_in, layout_er_in));
453eaf62fffSJeremy L Thompson 
454506b1a0cSSebastian Grimberg   // Determine elem_dof relation for input
455506b1a0cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed, num_nodes_in, &index_vec_in));
456506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayWrite(index_vec_in, CEED_MEM_HOST, &array));
457506b1a0cSSebastian Grimberg   for (CeedInt i = 0; i < num_nodes_in; i++) array[i] = i;
458506b1a0cSSebastian Grimberg   CeedCall(CeedVectorRestoreArray(index_vec_in, &array));
459506b1a0cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed, num_elem_in * elem_size_in * num_comp_in, &elem_dof_in));
460506b1a0cSSebastian Grimberg   CeedCall(CeedVectorSetValue(elem_dof_in, 0.0));
461506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_in, &index_elem_rstr_in));
462506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionApply(index_elem_rstr_in, CEED_NOTRANSPOSE, index_vec_in, elem_dof_in, CEED_REQUEST_IMMEDIATE));
463506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayRead(elem_dof_in, CEED_MEM_HOST, &elem_dof_a_in));
464506b1a0cSSebastian Grimberg   CeedCall(CeedVectorDestroy(&index_vec_in));
465506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_in));
466506b1a0cSSebastian Grimberg 
467506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
468506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out));
469506b1a0cSSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
470506b1a0cSSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
471506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out));
472506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out));
47356c48462SJeremy L Thompson     CeedCall(CeedElemRestrictionGetELayout(elem_rstr_out, layout_er_out));
474506b1a0cSSebastian Grimberg 
475506b1a0cSSebastian Grimberg     // Determine elem_dof relation for output
476506b1a0cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed, num_nodes_out, &index_vec_out));
477506b1a0cSSebastian Grimberg     CeedCall(CeedVectorGetArrayWrite(index_vec_out, CEED_MEM_HOST, &array));
478506b1a0cSSebastian Grimberg     for (CeedInt i = 0; i < num_nodes_out; i++) array[i] = i;
479506b1a0cSSebastian Grimberg     CeedCall(CeedVectorRestoreArray(index_vec_out, &array));
480506b1a0cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed, num_elem_out * elem_size_out * num_comp_out, &elem_dof_out));
481506b1a0cSSebastian Grimberg     CeedCall(CeedVectorSetValue(elem_dof_out, 0.0));
482506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_out, &index_elem_rstr_out));
483506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(index_elem_rstr_out, CEED_NOTRANSPOSE, index_vec_out, elem_dof_out, CEED_REQUEST_IMMEDIATE));
484506b1a0cSSebastian Grimberg     CeedCall(CeedVectorGetArrayRead(elem_dof_out, CEED_MEM_HOST, &elem_dof_a_out));
485506b1a0cSSebastian Grimberg     CeedCall(CeedVectorDestroy(&index_vec_out));
486506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_out));
487506b1a0cSSebastian Grimberg   } else {
488506b1a0cSSebastian Grimberg     num_elem_out     = num_elem_in;
489506b1a0cSSebastian Grimberg     elem_size_out    = elem_size_in;
490506b1a0cSSebastian Grimberg     num_comp_out     = num_comp_in;
491506b1a0cSSebastian Grimberg     layout_er_out[0] = layout_er_in[0];
492506b1a0cSSebastian Grimberg     layout_er_out[1] = layout_er_in[1];
493506b1a0cSSebastian Grimberg     layout_er_out[2] = layout_er_in[2];
494506b1a0cSSebastian Grimberg     elem_dof_a_out   = elem_dof_a_in;
495506b1a0cSSebastian Grimberg   }
496506b1a0cSSebastian Grimberg   local_num_entries = elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in;
497eaf62fffSJeremy L Thompson 
498eaf62fffSJeremy L Thompson   // Determine i, j locations for element matrices
499506b1a0cSSebastian Grimberg   for (CeedInt e = 0; e < num_elem_in; e++) {
500506b1a0cSSebastian Grimberg     for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) {
501506b1a0cSSebastian Grimberg       for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) {
502506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < elem_size_out; i++) {
503506b1a0cSSebastian Grimberg           for (CeedInt j = 0; j < elem_size_in; j++) {
504506b1a0cSSebastian Grimberg             const CeedInt elem_dof_index_row = i * layout_er_out[0] + comp_out * layout_er_out[1] + e * layout_er_out[2];
505506b1a0cSSebastian Grimberg             const CeedInt elem_dof_index_col = j * layout_er_in[0] + comp_in * layout_er_in[1] + e * layout_er_in[2];
506506b1a0cSSebastian Grimberg             const CeedInt row                = elem_dof_a_out[elem_dof_index_row];
507506b1a0cSSebastian Grimberg             const CeedInt col                = elem_dof_a_in[elem_dof_index_col];
508eaf62fffSJeremy L Thompson 
509eaf62fffSJeremy L Thompson             rows[offset + count] = row;
510eaf62fffSJeremy L Thompson             cols[offset + count] = col;
511eaf62fffSJeremy L Thompson             count++;
512eaf62fffSJeremy L Thompson           }
513eaf62fffSJeremy L Thompson         }
514eaf62fffSJeremy L Thompson       }
515eaf62fffSJeremy L Thompson     }
516eaf62fffSJeremy L Thompson   }
5176574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing assembled entries");
518506b1a0cSSebastian Grimberg   CeedCall(CeedVectorRestoreArrayRead(elem_dof_in, &elem_dof_a_in));
519506b1a0cSSebastian Grimberg   CeedCall(CeedVectorDestroy(&elem_dof_in));
520506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
521506b1a0cSSebastian Grimberg     CeedCall(CeedVectorRestoreArrayRead(elem_dof_out, &elem_dof_a_out));
522506b1a0cSSebastian Grimberg     CeedCall(CeedVectorDestroy(&elem_dof_out));
523506b1a0cSSebastian Grimberg   }
524eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
525eaf62fffSJeremy L Thompson }
526eaf62fffSJeremy L Thompson 
527eaf62fffSJeremy L Thompson /**
528ca94c3ddSJeremy L Thompson   @brief Assemble nonzero entries for non-composite `CeedOperator`.
529eaf62fffSJeremy L Thompson 
530ca94c3ddSJeremy L Thompson   Users should generally use @ref CeedOperatorLinearAssemble().
531eaf62fffSJeremy L Thompson 
532ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` to assemble
533ea61e9acSJeremy L Thompson   @param[in]  offset Offset for number of entries
534eaf62fffSJeremy L Thompson   @param[out] values Values to assemble into matrix
535eaf62fffSJeremy L Thompson 
536eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
537eaf62fffSJeremy L Thompson 
538eaf62fffSJeremy L Thompson   @ref Developer
539eaf62fffSJeremy L Thompson **/
5402b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) {
541f3d47e36SJeremy L Thompson   Ceed ceed;
542f3d47e36SJeremy L Thompson   bool is_composite;
5431c66c397SJeremy L Thompson 
544f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
545f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
5466574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
547f3d47e36SJeremy L Thompson 
548f3d47e36SJeremy L Thompson   // Early exit for empty operator
549f3d47e36SJeremy L Thompson   {
550f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
551f3d47e36SJeremy L Thompson 
552f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
553f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
554f3d47e36SJeremy L Thompson   }
555eaf62fffSJeremy L Thompson 
556cefa2673SJeremy L Thompson   if (op->LinearAssembleSingle) {
557cefa2673SJeremy L Thompson     // Backend version
5582b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSingle(op, offset, values));
559cefa2673SJeremy L Thompson     return CEED_ERROR_SUCCESS;
560cefa2673SJeremy L Thompson   } else {
561cefa2673SJeremy L Thompson     // Operator fallback
562cefa2673SJeremy L Thompson     CeedOperator op_fallback;
563cefa2673SJeremy L Thompson 
5642b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
565cefa2673SJeremy L Thompson     if (op_fallback) {
5662b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values));
567cefa2673SJeremy L Thompson       return CEED_ERROR_SUCCESS;
568cefa2673SJeremy L Thompson     }
569cefa2673SJeremy L Thompson   }
570cefa2673SJeremy L Thompson 
571eaf62fffSJeremy L Thompson   // Assemble QFunction
572506b1a0cSSebastian Grimberg   CeedInt             layout_qf[3];
5731c66c397SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
574c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
575506b1a0cSSebastian Grimberg   CeedElemRestriction assembled_elem_rstr = NULL;
576eaf62fffSJeremy L Thompson 
577506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, CEED_REQUEST_IMMEDIATE));
57856c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf));
579506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
580506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
581eaf62fffSJeremy L Thompson 
582ed9e99e6SJeremy L Thompson   // Get assembly data
583506b1a0cSSebastian Grimberg   CeedInt                  num_elem_in, elem_size_in, num_comp_in, num_qpts_in;
58481670346SSebastian Grimberg   CeedInt                  num_elem_out, elem_size_out, num_comp_out, num_qpts_out;
58581670346SSebastian Grimberg   CeedSize                 local_num_entries, count = 0;
586506b1a0cSSebastian Grimberg   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
587506b1a0cSSebastian Grimberg   CeedInt                  num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out;
588506b1a0cSSebastian Grimberg   CeedBasis               *active_bases_in, *active_bases_out, basis_in, basis_out;
589506b1a0cSSebastian Grimberg   const CeedScalar       **B_mats_in, **B_mats_out, *B_mat_in, *B_mat_out;
590506b1a0cSSebastian Grimberg   CeedElemRestriction      elem_rstr_in, elem_rstr_out;
591506b1a0cSSebastian Grimberg   CeedRestrictionType      elem_rstr_type_in, elem_rstr_type_out;
592506b1a0cSSebastian Grimberg   const bool              *elem_rstr_orients_in = NULL, *elem_rstr_orients_out = NULL;
593506b1a0cSSebastian Grimberg   const CeedInt8          *elem_rstr_curl_orients_in = NULL, *elem_rstr_curl_orients_out = NULL;
594506b1a0cSSebastian Grimberg   CeedOperatorAssemblyData data;
595eaf62fffSJeremy L Thompson 
596506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
597506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, NULL, &num_active_bases_out,
598506b1a0cSSebastian Grimberg                                                 &num_eval_modes_out, &eval_modes_out, NULL, NULL));
599506b1a0cSSebastian Grimberg 
600506b1a0cSSebastian Grimberg   CeedCheck(num_active_bases_in == num_active_bases_out && num_active_bases_in == 1, ceed, CEED_ERROR_UNSUPPORTED,
601506b1a0cSSebastian Grimberg             "Cannot assemble operator with multiple active bases");
6026574a04fSJeremy L Thompson   CeedCheck(num_eval_modes_in[0] > 0 && num_eval_modes_out[0] > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
603eaf62fffSJeremy L Thompson 
604506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, &B_mats_in, NULL, &active_bases_out, &B_mats_out));
605506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out));
606506b1a0cSSebastian Grimberg   basis_in  = active_bases_in[0];
607506b1a0cSSebastian Grimberg   basis_out = active_bases_out[0];
608506b1a0cSSebastian Grimberg   B_mat_in  = B_mats_in[0];
609506b1a0cSSebastian Grimberg   B_mat_out = B_mats_out[0];
610eaf62fffSJeremy L Thompson 
611506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in));
612506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in));
613506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in));
614506b1a0cSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
615506b1a0cSSebastian Grimberg   else CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
616506b1a0cSSebastian Grimberg 
617506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetType(elem_rstr_in, &elem_rstr_type_in));
618506b1a0cSSebastian Grimberg   if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) {
619506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_orients_in));
620506b1a0cSSebastian Grimberg   } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
621506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_curl_orients_in));
6227c1dbaffSSebastian Grimberg   }
6237c1dbaffSSebastian Grimberg 
624506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
625506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out));
626506b1a0cSSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
627506b1a0cSSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
628506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out));
629506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out));
630506b1a0cSSebastian Grimberg     if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
631506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
632506b1a0cSSebastian Grimberg     CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
633506b1a0cSSebastian Grimberg               "Active input and output bases must have the same number of quadrature points");
634eaf62fffSJeremy L Thompson 
635506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(elem_rstr_out, &elem_rstr_type_out));
636506b1a0cSSebastian Grimberg     if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) {
637506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_orients_out));
638506b1a0cSSebastian Grimberg     } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
639506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_curl_orients_out));
640506b1a0cSSebastian Grimberg     }
641506b1a0cSSebastian Grimberg   } else {
642506b1a0cSSebastian Grimberg     num_elem_out  = num_elem_in;
643506b1a0cSSebastian Grimberg     elem_size_out = elem_size_in;
644506b1a0cSSebastian Grimberg     num_comp_out  = num_comp_in;
645506b1a0cSSebastian Grimberg     num_qpts_out  = num_qpts_in;
646506b1a0cSSebastian Grimberg 
647506b1a0cSSebastian Grimberg     elem_rstr_orients_out      = elem_rstr_orients_in;
648506b1a0cSSebastian Grimberg     elem_rstr_curl_orients_out = elem_rstr_curl_orients_in;
649506b1a0cSSebastian Grimberg   }
650506b1a0cSSebastian Grimberg   local_num_entries = elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in;
651506b1a0cSSebastian Grimberg 
652506b1a0cSSebastian Grimberg   // Loop over elements and put in data structure
6537c1dbaffSSebastian Grimberg   // We store B_mat_in, B_mat_out, BTD, elem_mat in row-major order
6540459ebd3SSebastian Grimberg   CeedTensorContract contract;
655123d890dSSebastian Grimberg   CeedScalar        *vals, *BTD_mat = NULL, *elem_mat = NULL, *elem_mat_b = NULL;
656506b1a0cSSebastian Grimberg 
657c22497adSSebastian Grimberg   CeedCall(CeedBasisGetTensorContract(basis_in, &contract));
658123d890dSSebastian Grimberg   CeedCall(CeedCalloc(elem_size_out * num_qpts_in * num_eval_modes_in[0], &BTD_mat));
659123d890dSSebastian Grimberg   CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat));
660506b1a0cSSebastian Grimberg   if (elem_rstr_curl_orients_in || elem_rstr_curl_orients_out) CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat_b));
6611c66c397SJeremy L Thompson 
66228ec399dSJeremy L Thompson   CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals));
663506b1a0cSSebastian Grimberg   for (CeedSize e = 0; e < num_elem_in; e++) {
664506b1a0cSSebastian Grimberg     for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) {
665506b1a0cSSebastian Grimberg       for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) {
666ed9e99e6SJeremy L Thompson         // Compute B^T*D
667506b1a0cSSebastian Grimberg         for (CeedSize n = 0; n < elem_size_out; n++) {
668506b1a0cSSebastian Grimberg           for (CeedSize q = 0; q < num_qpts_in; q++) {
669437c7c90SJeremy L Thompson             for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) {
670506b1a0cSSebastian Grimberg               const CeedSize btd_index = n * (num_qpts_in * num_eval_modes_in[0]) + q * num_eval_modes_in[0] + e_in;
671067fd99fSJeremy L Thompson               CeedScalar     sum       = 0.0;
6721c66c397SJeremy L Thompson 
673437c7c90SJeremy L Thompson               for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) {
674506b1a0cSSebastian Grimberg                 const CeedSize b_out_index     = (q * num_eval_modes_out[0] + e_out) * elem_size_out + n;
675506b1a0cSSebastian 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;
676b94338b9SJed Brown                 const CeedSize qf_index        = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2];
6771c66c397SJeremy L Thompson 
678067fd99fSJeremy L Thompson                 sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index];
679eaf62fffSJeremy L Thompson               }
680067fd99fSJeremy L Thompson               BTD_mat[btd_index] = sum;
681ed9e99e6SJeremy L Thompson             }
682ed9e99e6SJeremy L Thompson           }
683eaf62fffSJeremy L Thompson         }
6847c1dbaffSSebastian Grimberg 
6857c1dbaffSSebastian Grimberg         // Form element matrix itself (for each block component)
686e4065a52SSebastian Grimberg         if (contract) {
6870459ebd3SSebastian Grimberg           CeedCall(CeedTensorContractApply(contract, 1, num_qpts_in * num_eval_modes_in[0], elem_size_in, elem_size_out, BTD_mat, CEED_NOTRANSPOSE,
6880459ebd3SSebastian Grimberg                                            false, B_mat_in, elem_mat));
689e4065a52SSebastian Grimberg         } else {
690e4065a52SSebastian 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]));
691e4065a52SSebastian Grimberg         }
692eaf62fffSJeremy L Thompson 
6937c1dbaffSSebastian Grimberg         // Transform the element matrix if required
694506b1a0cSSebastian Grimberg         if (elem_rstr_orients_out) {
695506b1a0cSSebastian Grimberg           const bool *elem_orients = &elem_rstr_orients_out[e * elem_size_out];
6961c66c397SJeremy L Thompson 
697506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
698506b1a0cSSebastian Grimberg             const double orient = elem_orients[i] ? -1.0 : 1.0;
699506b1a0cSSebastian Grimberg 
700506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
701506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] *= orient;
7027c1dbaffSSebastian Grimberg             }
7037c1dbaffSSebastian Grimberg           }
704506b1a0cSSebastian Grimberg         } else if (elem_rstr_curl_orients_out) {
705506b1a0cSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_out[e * 3 * elem_size_out];
7061c66c397SJeremy L Thompson 
7077c1dbaffSSebastian Grimberg           // T^T*(B^T*D*B)
708506b1a0cSSebastian Grimberg           memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar));
709506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
710506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
711506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * i + 1] +
712506b1a0cSSebastian Grimberg                                                (i > 0 ? elem_mat_b[(i - 1) * elem_size_in + j] * elem_curl_orients[3 * i - 1] : 0.0) +
713506b1a0cSSebastian Grimberg                                                (i < elem_size_out - 1 ? elem_mat_b[(i + 1) * elem_size_in + j] * elem_curl_orients[3 * i + 3] : 0.0);
7147c1dbaffSSebastian Grimberg             }
7157c1dbaffSSebastian Grimberg           }
716506b1a0cSSebastian Grimberg         }
717506b1a0cSSebastian Grimberg         if (elem_rstr_orients_in) {
718506b1a0cSSebastian Grimberg           const bool *elem_orients = &elem_rstr_orients_in[e * elem_size_in];
719506b1a0cSSebastian Grimberg 
720506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
721506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
722506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] *= elem_orients[j] ? -1.0 : 1.0;
723506b1a0cSSebastian Grimberg             }
724506b1a0cSSebastian Grimberg           }
725506b1a0cSSebastian Grimberg         } else if (elem_rstr_curl_orients_in) {
726506b1a0cSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_in[e * 3 * elem_size_in];
727506b1a0cSSebastian Grimberg 
728506b1a0cSSebastian Grimberg           // (B^T*D*B)*T
729506b1a0cSSebastian Grimberg           memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar));
730506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
731506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
732506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * j + 1] +
733506b1a0cSSebastian Grimberg                                                (j > 0 ? elem_mat_b[i * elem_size_in + j - 1] * elem_curl_orients[3 * j - 1] : 0.0) +
734506b1a0cSSebastian Grimberg                                                (j < elem_size_in - 1 ? elem_mat_b[i * elem_size_in + j + 1] * elem_curl_orients[3 * j + 3] : 0.0);
7357c1dbaffSSebastian Grimberg             }
7367c1dbaffSSebastian Grimberg           }
7377c1dbaffSSebastian Grimberg         }
7387c1dbaffSSebastian Grimberg 
7397c1dbaffSSebastian Grimberg         // Put element matrix in coordinate data structure
740506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < elem_size_out; i++) {
741506b1a0cSSebastian Grimberg           for (CeedInt j = 0; j < elem_size_in; j++) {
742506b1a0cSSebastian Grimberg             vals[offset + count] = elem_mat[i * elem_size_in + j];
743eaf62fffSJeremy L Thompson             count++;
744eaf62fffSJeremy L Thompson           }
745eaf62fffSJeremy L Thompson         }
746eaf62fffSJeremy L Thompson       }
747eaf62fffSJeremy L Thompson     }
748eaf62fffSJeremy L Thompson   }
7496574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing entries");
7502b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(values, &vals));
751eaf62fffSJeremy L Thompson 
752506b1a0cSSebastian Grimberg   // Cleanup
753123d890dSSebastian Grimberg   CeedCall(CeedFree(&BTD_mat));
754123d890dSSebastian Grimberg   CeedCall(CeedFree(&elem_mat));
755506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&elem_mat_b));
756506b1a0cSSebastian Grimberg   if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) {
757506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_in, &elem_rstr_orients_in));
758506b1a0cSSebastian Grimberg   } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
759506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_in, &elem_rstr_curl_orients_in));
760506b1a0cSSebastian Grimberg   }
761506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
762506b1a0cSSebastian Grimberg     if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) {
763506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_out, &elem_rstr_orients_out));
764506b1a0cSSebastian Grimberg     } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
765506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_out, &elem_rstr_curl_orients_out));
766506b1a0cSSebastian Grimberg     }
767506b1a0cSSebastian Grimberg   }
7682b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
7692b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
770eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
771eaf62fffSJeremy L Thompson }
772eaf62fffSJeremy L Thompson 
773eaf62fffSJeremy L Thompson /**
774ca94c3ddSJeremy L Thompson   @brief Count number of entries for assembled `CeedOperator`
775eaf62fffSJeremy L Thompson 
776ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to assemble
777eaf62fffSJeremy L Thompson   @param[out] num_entries Number of entries in assembled representation
778eaf62fffSJeremy L Thompson 
779eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
780eaf62fffSJeremy L Thompson 
781eaf62fffSJeremy L Thompson   @ref Utility
782eaf62fffSJeremy L Thompson **/
783b94338b9SJed Brown static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedSize *num_entries) {
784b275c451SJeremy L Thompson   bool                is_composite;
785506b1a0cSSebastian Grimberg   CeedInt             num_elem_in, elem_size_in, num_comp_in, num_elem_out, elem_size_out, num_comp_out;
7861203703bSJeremy L Thompson   Ceed                ceed;
787506b1a0cSSebastian Grimberg   CeedElemRestriction rstr_in, rstr_out;
788eaf62fffSJeremy L Thompson 
7891203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
790b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
7911203703bSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
792506b1a0cSSebastian Grimberg 
793506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
794506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
795506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
796506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
797506b1a0cSSebastian Grimberg   if (rstr_in != rstr_out) {
798506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
7991203703bSJeremy L Thompson     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
800506b1a0cSSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
801506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
802506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
803506b1a0cSSebastian Grimberg   } else {
804506b1a0cSSebastian Grimberg     num_elem_out  = num_elem_in;
805506b1a0cSSebastian Grimberg     elem_size_out = elem_size_in;
806506b1a0cSSebastian Grimberg     num_comp_out  = num_comp_in;
807506b1a0cSSebastian Grimberg   }
808506b1a0cSSebastian Grimberg   *num_entries = (CeedSize)elem_size_in * num_comp_in * elem_size_out * num_comp_out * num_elem_in;
809eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
810eaf62fffSJeremy L Thompson }
811eaf62fffSJeremy L Thompson 
812eaf62fffSJeremy L Thompson /**
813ca94c3ddSJeremy L Thompson   @brief Common code for creating a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator`
814eaf62fffSJeremy L Thompson 
815ca94c3ddSJeremy L Thompson   @param[in]  op_fine      Fine grid `CeedOperator`
816ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
817ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid `CeedElemRestriction`
818ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector `CeedBasis`
819ca94c3ddSJeremy L Thompson   @param[in]  basis_c_to_f `CeedBasis` for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction operators
820ca94c3ddSJeremy L Thompson   @param[out] op_coarse    Coarse grid `CeedOperator`
821ca94c3ddSJeremy L Thompson   @param[out] op_prolong   Coarse to fine `CeedOperator`, or `NULL`
822ca94c3ddSJeremy L Thompson   @param[out] op_restrict  Fine to coarse `CeedOperator`, or `NULL`
823eaf62fffSJeremy L Thompson 
824eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
825eaf62fffSJeremy L Thompson 
826eaf62fffSJeremy L Thompson   @ref Developer
827eaf62fffSJeremy L Thompson **/
8282b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
8297758292fSSebastian Grimberg                                             CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
8301c66c397SJeremy L Thompson   bool                is_composite;
831eaf62fffSJeremy L Thompson   Ceed                ceed;
8321203703bSJeremy L Thompson   CeedInt             num_comp, num_input_fields, num_output_fields;
83385bb9dcfSJeremy L Thompson   CeedVector          mult_vec         = NULL;
8341c66c397SJeremy L Thompson   CeedElemRestriction rstr_p_mult_fine = NULL, rstr_fine = NULL;
8351203703bSJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
8361c66c397SJeremy L Thompson 
8372b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
838eaf62fffSJeremy L Thompson 
839eaf62fffSJeremy L Thompson   // Check for composite operator
8402b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op_fine, &is_composite));
8416574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported");
842eaf62fffSJeremy L Thompson 
843eaf62fffSJeremy L Thompson   // Coarse Grid
8442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
8451203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op_fine, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
846eaf62fffSJeremy L Thompson   // -- Clone input fields
8471203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
8486f8994e9SJeremy L Thompson     const char         *field_name;
8491203703bSJeremy L Thompson     CeedVector          vec;
8501203703bSJeremy L Thompson     CeedElemRestriction rstr;
8511203703bSJeremy L Thompson     CeedBasis           basis;
8521203703bSJeremy L Thompson 
8531203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetName(input_fields[i], &field_name));
8541203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(input_fields[i], &vec));
8551203703bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
8561203703bSJeremy L Thompson       rstr  = rstr_coarse;
8571203703bSJeremy L Thompson       basis = basis_coarse;
8581203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_fine));
859eaf62fffSJeremy L Thompson     } else {
8601203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr));
8611203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis));
862eaf62fffSJeremy L Thompson     }
8631203703bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec));
864eaf62fffSJeremy L Thompson   }
865eaf62fffSJeremy L Thompson   // -- Clone output fields
8661203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
8676f8994e9SJeremy L Thompson     const char         *field_name;
8681203703bSJeremy L Thompson     CeedVector          vec;
8691203703bSJeremy L Thompson     CeedElemRestriction rstr;
8701203703bSJeremy L Thompson     CeedBasis           basis;
8711203703bSJeremy L Thompson 
8721203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetName(output_fields[i], &field_name));
8731203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
8741203703bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
8751203703bSJeremy L Thompson       rstr  = rstr_coarse;
8761203703bSJeremy L Thompson       basis = basis_coarse;
8771203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_fine));
878eaf62fffSJeremy L Thompson     } else {
8791203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr));
8801203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis));
881eaf62fffSJeremy L Thompson     }
8821203703bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec));
883eaf62fffSJeremy L Thompson   }
884af99e877SJeremy L Thompson   // -- Clone QFunctionAssemblyData
8852b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op_fine->qf_assembled, &(*op_coarse)->qf_assembled));
886eaf62fffSJeremy L Thompson 
887eaf62fffSJeremy L Thompson   // Multiplicity vector
8887758292fSSebastian Grimberg   if (op_restrict || op_prolong) {
88985bb9dcfSJeremy L Thompson     CeedVector          mult_e_vec;
8901c66c397SJeremy L Thompson     CeedRestrictionType rstr_type;
89185bb9dcfSJeremy L Thompson 
8927c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr_fine, &rstr_type));
8937c1dbaffSSebastian Grimberg     CeedCheck(rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_UNSUPPORTED,
8947c1dbaffSSebastian Grimberg               "Element restrictions created with CeedElemRestrictionCreateCurlOriented are not supported");
8956574a04fSJeremy L Thompson     CeedCheck(p_mult_fine, ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector");
8967c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnsignedCopy(rstr_fine, &rstr_p_mult_fine));
8972b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec));
8982b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_e_vec, 0.0));
899c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE));
9002b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_vec, 0.0));
901c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE));
9022b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&mult_e_vec));
9032b730f8bSJeremy L Thompson     CeedCall(CeedVectorReciprocal(mult_vec));
90485bb9dcfSJeremy L Thompson   }
905eaf62fffSJeremy L Thompson 
906addd79feSZach Atkins   // Clone name
907addd79feSZach Atkins   bool   has_name = op_fine->name;
908addd79feSZach Atkins   size_t name_len = op_fine->name ? strlen(op_fine->name) : 0;
909addd79feSZach Atkins   CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name));
910addd79feSZach Atkins 
9117758292fSSebastian Grimberg   // Check that coarse to fine basis is provided if prolong/restrict operators are requested
9127758292fSSebastian Grimberg   CeedCheck(basis_c_to_f || (!op_restrict && !op_prolong), ceed, CEED_ERROR_INCOMPATIBLE,
9136574a04fSJeremy L Thompson             "Prolongation or restriction operator creation requires coarse-to-fine basis");
91483d6adf3SZach Atkins 
91585bb9dcfSJeremy L Thompson   // Restriction/Prolongation Operators
9162b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp));
917addd79feSZach Atkins 
918addd79feSZach Atkins   // Restriction
9197758292fSSebastian Grimberg   if (op_restrict) {
920eaf62fffSJeremy L Thompson     CeedInt             *num_comp_r_data;
92185bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_r;
9227758292fSSebastian Grimberg     CeedQFunction        qf_restrict;
92385bb9dcfSJeremy L Thompson 
9247758292fSSebastian Grimberg     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict));
9252b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_r_data));
926eaf62fffSJeremy L Thompson     num_comp_r_data[0] = num_comp;
9272b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r));
9282b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data));
9297758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r));
9302b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_r));
9317758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE));
9327758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE));
9337758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP));
9347758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp));
935eaf62fffSJeremy L Thompson 
9367758292fSSebastian Grimberg     CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict));
9377758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
9387758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
9397758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
940eaf62fffSJeremy L Thompson 
941addd79feSZach Atkins     // Set name
942addd79feSZach Atkins     char *restriction_name;
9431c66c397SJeremy L Thompson 
944addd79feSZach Atkins     CeedCall(CeedCalloc(17 + name_len, &restriction_name));
945addd79feSZach Atkins     sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
9467758292fSSebastian Grimberg     CeedCall(CeedOperatorSetName(*op_restrict, restriction_name));
947addd79feSZach Atkins     CeedCall(CeedFree(&restriction_name));
948addd79feSZach Atkins 
949addd79feSZach Atkins     // Check
9507758292fSSebastian Grimberg     CeedCall(CeedOperatorCheckReady(*op_restrict));
951addd79feSZach Atkins 
952addd79feSZach Atkins     // Cleanup
9537758292fSSebastian Grimberg     CeedCall(CeedQFunctionDestroy(&qf_restrict));
954addd79feSZach Atkins   }
955addd79feSZach Atkins 
956eaf62fffSJeremy L Thompson   // Prolongation
957addd79feSZach Atkins   if (op_prolong) {
958eaf62fffSJeremy L Thompson     CeedInt             *num_comp_p_data;
95985bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_p;
9601c66c397SJeremy L Thompson     CeedQFunction        qf_prolong;
96185bb9dcfSJeremy L Thompson 
96285bb9dcfSJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong));
9632b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_p_data));
964eaf62fffSJeremy L Thompson     num_comp_p_data[0] = num_comp;
9652b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p));
9662b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data));
9672b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p));
9682b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_p));
9692b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP));
9702b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE));
9712b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE));
9722b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp));
973eaf62fffSJeremy L Thompson 
9742b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong));
9752b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
976356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
977356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
978eaf62fffSJeremy L Thompson 
979addd79feSZach Atkins     // Set name
980ea6b5821SJeremy L Thompson     char *prolongation_name;
9811c66c397SJeremy L Thompson 
9822b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(18 + name_len, &prolongation_name));
9832b730f8bSJeremy L Thompson     sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
9842b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name));
9852b730f8bSJeremy L Thompson     CeedCall(CeedFree(&prolongation_name));
986addd79feSZach Atkins 
987addd79feSZach Atkins     // Check
988addd79feSZach Atkins     CeedCall(CeedOperatorCheckReady(*op_prolong));
989addd79feSZach Atkins 
990addd79feSZach Atkins     // Cleanup
991addd79feSZach Atkins     CeedCall(CeedQFunctionDestroy(&qf_prolong));
992ea6b5821SJeremy L Thompson   }
993ea6b5821SJeremy L Thompson 
99458e4b056SJeremy L Thompson   // Check
99558e4b056SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(*op_coarse));
99658e4b056SJeremy L Thompson 
997eaf62fffSJeremy L Thompson   // Cleanup
9982b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_vec));
999c17ec2beSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine));
10002b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis_c_to_f));
1001eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1002eaf62fffSJeremy L Thompson }
1003eaf62fffSJeremy L Thompson 
1004eaf62fffSJeremy L Thompson /**
1005eaf62fffSJeremy L Thompson   @brief Build 1D mass matrix and Laplacian with perturbation
1006eaf62fffSJeremy L Thompson 
1007eaf62fffSJeremy L Thompson   @param[in]  interp_1d   Interpolation matrix in one dimension
1008eaf62fffSJeremy L Thompson   @param[in]  grad_1d     Gradient matrix in one dimension
1009eaf62fffSJeremy L Thompson   @param[in]  q_weight_1d Quadrature weights in one dimension
1010eaf62fffSJeremy L Thompson   @param[in]  P_1d        Number of basis nodes in one dimension
1011eaf62fffSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
1012eaf62fffSJeremy L Thompson   @param[in]  dim         Dimension of basis
1013eaf62fffSJeremy L Thompson   @param[out] mass        Assembled mass matrix in one dimension
1014eaf62fffSJeremy L Thompson   @param[out] laplace     Assembled perturbed Laplacian in one dimension
1015eaf62fffSJeremy L Thompson 
1016eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1017eaf62fffSJeremy L Thompson 
1018eaf62fffSJeremy L Thompson   @ref Developer
1019eaf62fffSJeremy L Thompson **/
10202c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
10212c2ea1dbSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d, CeedInt P_1d, CeedInt Q_1d,
10222c2ea1dbSJeremy L Thompson                                 CeedInt dim, CeedScalar *mass, CeedScalar *laplace) {
10232b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
1024eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
1025eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
10262b730f8bSJeremy 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];
1027eaf62fffSJeremy L Thompson       mass[i + j * P_1d] = sum;
1028eaf62fffSJeremy L Thompson     }
10292b730f8bSJeremy L Thompson   }
1030eaf62fffSJeremy L Thompson   // -- Laplacian
10312b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
1032eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
1033eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
10341c66c397SJeremy L Thompson 
10352b730f8bSJeremy 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];
1036eaf62fffSJeremy L Thompson       laplace[i + j * P_1d] = sum;
1037eaf62fffSJeremy L Thompson     }
10382b730f8bSJeremy L Thompson   }
1039eaf62fffSJeremy L Thompson   CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4;
10402b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation;
1041eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1042eaf62fffSJeremy L Thompson }
10432c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
1044eaf62fffSJeremy L Thompson 
1045eaf62fffSJeremy L Thompson /// @}
1046eaf62fffSJeremy L Thompson 
1047eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1048480fae85SJeremy L Thompson /// CeedOperator Backend API
1049480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1050480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend
1051480fae85SJeremy L Thompson /// @{
1052480fae85SJeremy L Thompson 
1053480fae85SJeremy L Thompson /**
1054004e4986SSebastian Grimberg   @brief Select correct basis matrix pointer based on @ref CeedEvalMode
1055004e4986SSebastian Grimberg 
1056004e4986SSebastian Grimberg   @param[in]  basis     `CeedBasis` from which to get the basis matrix
1057004e4986SSebastian Grimberg   @param[in]  eval_mode Current basis evaluation mode
1058004e4986SSebastian Grimberg   @param[in]  identity  Pointer to identity matrix
1059004e4986SSebastian Grimberg   @param[out] basis_ptr `CeedBasis` pointer to set
1060004e4986SSebastian Grimberg 
1061004e4986SSebastian Grimberg   @ref Backend
1062004e4986SSebastian Grimberg **/
1063004e4986SSebastian Grimberg int CeedOperatorGetBasisPointer(CeedBasis basis, CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar **basis_ptr) {
1064004e4986SSebastian Grimberg   switch (eval_mode) {
1065004e4986SSebastian Grimberg     case CEED_EVAL_NONE:
1066004e4986SSebastian Grimberg       *basis_ptr = identity;
1067004e4986SSebastian Grimberg       break;
1068004e4986SSebastian Grimberg     case CEED_EVAL_INTERP:
1069004e4986SSebastian Grimberg       CeedCall(CeedBasisGetInterp(basis, basis_ptr));
1070004e4986SSebastian Grimberg       break;
1071004e4986SSebastian Grimberg     case CEED_EVAL_GRAD:
1072004e4986SSebastian Grimberg       CeedCall(CeedBasisGetGrad(basis, basis_ptr));
1073004e4986SSebastian Grimberg       break;
1074004e4986SSebastian Grimberg     case CEED_EVAL_DIV:
1075004e4986SSebastian Grimberg       CeedCall(CeedBasisGetDiv(basis, basis_ptr));
1076004e4986SSebastian Grimberg       break;
1077004e4986SSebastian Grimberg     case CEED_EVAL_CURL:
1078004e4986SSebastian Grimberg       CeedCall(CeedBasisGetCurl(basis, basis_ptr));
1079004e4986SSebastian Grimberg       break;
1080004e4986SSebastian Grimberg     case CEED_EVAL_WEIGHT:
1081004e4986SSebastian Grimberg       break;  // Caught by QF Assembly
1082004e4986SSebastian Grimberg   }
1083004e4986SSebastian Grimberg   assert(*basis_ptr != NULL);
1084004e4986SSebastian Grimberg   return CEED_ERROR_SUCCESS;
1085004e4986SSebastian Grimberg }
1086004e4986SSebastian Grimberg 
1087004e4986SSebastian Grimberg /**
1088ca94c3ddSJeremy L Thompson   @brief Create point block restriction for active `CeedOperatorField`
1089506b1a0cSSebastian Grimberg 
1090ca94c3ddSJeremy L Thompson   @param[in]  rstr             Original `CeedElemRestriction` for active field
1091ca94c3ddSJeremy L Thompson   @param[out] point_block_rstr Address of the variable where the newly created `CeedElemRestriction` will be stored
1092506b1a0cSSebastian Grimberg 
1093506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1094506b1a0cSSebastian Grimberg 
1095506b1a0cSSebastian Grimberg   @ref Backend
1096506b1a0cSSebastian Grimberg **/
1097506b1a0cSSebastian Grimberg int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *point_block_rstr) {
1098506b1a0cSSebastian Grimberg   Ceed           ceed;
1099506b1a0cSSebastian Grimberg   CeedInt        num_elem, num_comp, shift, elem_size, comp_stride, *point_block_offsets;
1100506b1a0cSSebastian Grimberg   CeedSize       l_size;
1101506b1a0cSSebastian Grimberg   const CeedInt *offsets;
1102506b1a0cSSebastian Grimberg 
1103506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
1104506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
1105506b1a0cSSebastian Grimberg 
1106506b1a0cSSebastian Grimberg   // Expand offsets
1107506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
1108506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1109506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
1110506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
1111506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
1112506b1a0cSSebastian Grimberg   shift = num_comp;
1113506b1a0cSSebastian Grimberg   if (comp_stride != 1) shift *= num_comp;
1114506b1a0cSSebastian Grimberg   CeedCall(CeedCalloc(num_elem * elem_size, &point_block_offsets));
1115506b1a0cSSebastian Grimberg   for (CeedInt i = 0; i < num_elem * elem_size; i++) {
1116506b1a0cSSebastian Grimberg     point_block_offsets[i] = offsets[i] * shift;
1117506b1a0cSSebastian Grimberg   }
1118506b1a0cSSebastian Grimberg 
1119506b1a0cSSebastian Grimberg   // Create new restriction
1120506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
1121506b1a0cSSebastian Grimberg                                      point_block_offsets, point_block_rstr));
1122506b1a0cSSebastian Grimberg 
1123506b1a0cSSebastian Grimberg   // Cleanup
1124506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
1125506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1126506b1a0cSSebastian Grimberg }
1127506b1a0cSSebastian Grimberg 
1128506b1a0cSSebastian Grimberg /**
1129ca94c3ddSJeremy L Thompson   @brief Create object holding `CeedQFunction` assembly data for `CeedOperator`
1130480fae85SJeremy L Thompson 
1131ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedQFunctionAssemblyData`
1132ca94c3ddSJeremy L Thompson   @param[out] data Address of the variable where the newly created `CeedQFunctionAssemblyData` will be stored
1133480fae85SJeremy L Thompson 
1134480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1135480fae85SJeremy L Thompson 
1136480fae85SJeremy L Thompson   @ref Backend
1137480fae85SJeremy L Thompson **/
1138ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) {
11392b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1140480fae85SJeremy L Thompson   (*data)->ref_count = 1;
1141480fae85SJeremy L Thompson   (*data)->ceed      = ceed;
11422b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1143480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1144480fae85SJeremy L Thompson }
1145480fae85SJeremy L Thompson 
1146480fae85SJeremy L Thompson /**
1147ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedQFunctionAssemblyData`
1148480fae85SJeremy L Thompson 
1149ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to increment the reference counter
1150480fae85SJeremy L Thompson 
1151480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1152480fae85SJeremy L Thompson 
1153480fae85SJeremy L Thompson   @ref Backend
1154480fae85SJeremy L Thompson **/
1155480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) {
1156480fae85SJeremy L Thompson   data->ref_count++;
1157480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1158480fae85SJeremy L Thompson }
1159480fae85SJeremy L Thompson 
1160480fae85SJeremy L Thompson /**
1161ca94c3ddSJeremy L Thompson   @brief Set re-use of `CeedQFunctionAssemblyData`
11628b919e6bSJeremy L Thompson 
1163ca94c3ddSJeremy L Thompson   @param[in,out] data       `CeedQFunctionAssemblyData` to mark for reuse
1164ea61e9acSJeremy L Thompson   @param[in]     reuse_data Boolean flag indicating data re-use
11658b919e6bSJeremy L Thompson 
11668b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
11678b919e6bSJeremy L Thompson 
11688b919e6bSJeremy L Thompson   @ref Backend
11698b919e6bSJeremy L Thompson **/
11702b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) {
1171beecbf24SJeremy L Thompson   data->reuse_data        = reuse_data;
1172beecbf24SJeremy L Thompson   data->needs_data_update = true;
1173beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1174beecbf24SJeremy L Thompson }
1175beecbf24SJeremy L Thompson 
1176beecbf24SJeremy L Thompson /**
1177ca94c3ddSJeremy L Thompson   @brief Mark `CeedQFunctionAssemblyData` as stale
1178beecbf24SJeremy L Thompson 
1179ca94c3ddSJeremy L Thompson   @param[in,out] data              `CeedQFunctionAssemblyData` to mark as stale
1180ea61e9acSJeremy L Thompson   @param[in]     needs_data_update Boolean flag indicating if update is needed or completed
1181beecbf24SJeremy L Thompson 
1182beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1183beecbf24SJeremy L Thompson 
1184beecbf24SJeremy L Thompson   @ref Backend
1185beecbf24SJeremy L Thompson **/
11862b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) {
1187beecbf24SJeremy L Thompson   data->needs_data_update = needs_data_update;
11888b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
11898b919e6bSJeremy L Thompson }
11908b919e6bSJeremy L Thompson 
11918b919e6bSJeremy L Thompson /**
1192ca94c3ddSJeremy L Thompson   @brief Determine if `CeedQFunctionAssemblyData` needs update
11938b919e6bSJeremy L Thompson 
1194ca94c3ddSJeremy L Thompson   @param[in]  data             `CeedQFunctionAssemblyData` to mark as stale
11958b919e6bSJeremy L Thompson   @param[out] is_update_needed Boolean flag indicating if re-assembly is required
11968b919e6bSJeremy L Thompson 
11978b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
11988b919e6bSJeremy L Thompson 
11998b919e6bSJeremy L Thompson   @ref Backend
12008b919e6bSJeremy L Thompson **/
12012b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) {
1202beecbf24SJeremy L Thompson   *is_update_needed = !data->reuse_data || data->needs_data_update;
12038b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12048b919e6bSJeremy L Thompson }
12058b919e6bSJeremy L Thompson 
12068b919e6bSJeremy L Thompson /**
1207ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedQFunctionAssemblyData`.
12084385fb7fSSebastian Grimberg 
1209ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedQFunctionAssemblyDataDestroy().
1210512bb800SJeremy L Thompson 
1211ca94c3ddSJeremy 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`.
1212ca94c3ddSJeremy L Thompson         This `CeedQFunctionAssemblyData` will be destroyed if ` *data_copy` is the only reference to this `CeedQFunctionAssemblyData`.
1213480fae85SJeremy L Thompson 
1214ca94c3ddSJeremy L Thompson   @param[in]     data      `CeedQFunctionAssemblyData` to copy reference to
1215ea61e9acSJeremy L Thompson   @param[in,out] data_copy Variable to store copied reference
1216480fae85SJeremy L Thompson 
1217480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1218480fae85SJeremy L Thompson 
1219480fae85SJeremy L Thompson   @ref Backend
1220480fae85SJeremy L Thompson **/
12212b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) {
12222b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReference(data));
12232b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy));
1224480fae85SJeremy L Thompson   *data_copy = data;
1225480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1226480fae85SJeremy L Thompson }
1227480fae85SJeremy L Thompson 
1228480fae85SJeremy L Thompson /**
1229ca94c3ddSJeremy L Thompson   @brief Get setup status for internal objects for `CeedQFunctionAssemblyData`
1230480fae85SJeremy L Thompson 
1231ca94c3ddSJeremy L Thompson   @param[in]  data     `CeedQFunctionAssemblyData` to retrieve status
1232480fae85SJeremy L Thompson   @param[out] is_setup Boolean flag for setup status
1233480fae85SJeremy L Thompson 
1234480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1235480fae85SJeremy L Thompson 
1236480fae85SJeremy L Thompson   @ref Backend
1237480fae85SJeremy L Thompson **/
12382b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) {
1239480fae85SJeremy L Thompson   *is_setup = data->is_setup;
1240480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1241480fae85SJeremy L Thompson }
1242480fae85SJeremy L Thompson 
1243480fae85SJeremy L Thompson /**
1244ca94c3ddSJeremy L Thompson   @brief Set internal objects for `CeedQFunctionAssemblyData`
1245480fae85SJeremy L Thompson 
1246ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to set objects
1247ca94c3ddSJeremy L Thompson   @param[in]     vec  `CeedVector` to store assembled `CeedQFunction` at quadrature points
1248ca94c3ddSJeremy L Thompson   @param[in]     rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1249480fae85SJeremy L Thompson 
1250480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1251480fae85SJeremy L Thompson 
1252480fae85SJeremy L Thompson   @ref Backend
1253480fae85SJeremy L Thompson **/
12542b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) {
12552b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(vec, &data->vec));
12562b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr));
1257480fae85SJeremy L Thompson 
1258480fae85SJeremy L Thompson   data->is_setup = true;
1259480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1260480fae85SJeremy L Thompson }
1261480fae85SJeremy L Thompson 
12624dd1a9d2SSebastian Grimberg /**
1263ca94c3ddSJeremy L Thompson   @brief Get internal objects for `CeedQFunctionAssemblyData`
12644dd1a9d2SSebastian Grimberg 
1265ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to set objects
1266ca94c3ddSJeremy L Thompson   @param[out]    vec  `CeedVector` to store assembled `CeedQFunction` at quadrature points
1267ca94c3ddSJeremy L Thompson   @param[out]    rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
12684dd1a9d2SSebastian Grimberg 
12694dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
12704dd1a9d2SSebastian Grimberg 
12714dd1a9d2SSebastian Grimberg   @ref Backend
12724dd1a9d2SSebastian Grimberg **/
12732b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) {
12746574a04fSJeremy L Thompson   CeedCheck(data->is_setup, data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first.");
1275480fae85SJeremy L Thompson 
12762b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(data->vec, vec));
12772b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr));
1278480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1279480fae85SJeremy L Thompson }
1280480fae85SJeremy L Thompson 
1281480fae85SJeremy L Thompson /**
1282ca94c3ddSJeremy L Thompson   @brief Destroy `CeedQFunctionAssemblyData`
1283480fae85SJeremy L Thompson 
1284ca94c3ddSJeremy L Thompson   @param[in,out] data  `CeedQFunctionAssemblyData` to destroy
1285480fae85SJeremy L Thompson 
1286480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1287480fae85SJeremy L Thompson 
1288480fae85SJeremy L Thompson   @ref Backend
1289480fae85SJeremy L Thompson **/
1290480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) {
1291ad6481ceSJeremy L Thompson   if (!*data || --(*data)->ref_count > 0) {
1292ad6481ceSJeremy L Thompson     *data = NULL;
1293ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1294ad6481ceSJeremy L Thompson   }
12952b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
12962b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*data)->vec));
12972b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr));
1298480fae85SJeremy L Thompson 
12992b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1300480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1301480fae85SJeremy L Thompson }
1302480fae85SJeremy L Thompson 
1303ed9e99e6SJeremy L Thompson /**
1304ca94c3ddSJeremy L Thompson   @brief Get `CeedOperatorAssemblyData`
1305ed9e99e6SJeremy L Thompson 
1306ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator` to assemble
1307ca94c3ddSJeremy L Thompson   @param[out] data `CeedQFunctionAssemblyData`
1308ed9e99e6SJeremy L Thompson 
1309ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1310ed9e99e6SJeremy L Thompson 
1311ed9e99e6SJeremy L Thompson   @ref Backend
1312ed9e99e6SJeremy L Thompson **/
13132b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) {
1314ed9e99e6SJeremy L Thompson   if (!op->op_assembled) {
1315ed9e99e6SJeremy L Thompson     CeedOperatorAssemblyData data;
1316ed9e99e6SJeremy L Thompson 
13172b730f8bSJeremy L Thompson     CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data));
1318ed9e99e6SJeremy L Thompson     op->op_assembled = data;
1319ed9e99e6SJeremy L Thompson   }
1320ed9e99e6SJeremy L Thompson   *data = op->op_assembled;
1321ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1322ed9e99e6SJeremy L Thompson }
1323ed9e99e6SJeremy L Thompson 
1324ed9e99e6SJeremy L Thompson /**
1325ca94c3ddSJeremy L Thompson   @brief Create object holding `CeedOperator` assembly data.
1326ba746a46SJeremy L Thompson 
1327ca94c3ddSJeremy L Thompson   The `CeedOperatorAssemblyData` holds an array with references to every active `CeedBasis` used in the `CeedOperator`.
1328ca94c3ddSJeremy L Thompson   An array with references to the corresponding active `CeedElemRestriction` is also stored.
1329ca94c3ddSJeremy L Thompson   For each active `CeedBasis, the `CeedOperatorAssemblyData` holds an array of all input and output @ref CeedEvalMode for this `CeedBasis`.
1330ca94c3ddSJeremy L Thompson   The `CeedOperatorAssemblyData` holds an array of offsets for indexing into the assembled `CeedQFunction` arrays to the row representing each @ref CeedEvalMode.
1331ca94c3ddSJeremy L Thompson   The number of input columns across all active bases for the assembled `CeedQFunction` is also stored.
1332ca94c3ddSJeremy L Thompson   Lastly, the `CeedOperatorAssembly` data holds assembled matrices representing the full action of the `CeedBasis` for all @ref CeedEvalMode.
1333ed9e99e6SJeremy L Thompson 
1334ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperatorAssemblyData`
1335ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator` to be assembled
1336ca94c3ddSJeremy L Thompson   @param[out] data Address of the variable where the newly created `CeedOperatorAssemblyData` will be stored
1337ed9e99e6SJeremy L Thompson 
1338ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1339ed9e99e6SJeremy L Thompson 
1340ed9e99e6SJeremy L Thompson   @ref Backend
1341ed9e99e6SJeremy L Thompson **/
13422b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) {
1343506b1a0cSSebastian Grimberg   CeedInt             num_active_bases_in = 0, num_active_bases_out = 0, offset = 0;
1344506b1a0cSSebastian Grimberg   CeedInt             num_input_fields, *num_eval_modes_in = NULL, num_output_fields, *num_eval_modes_out = NULL;
13451c66c397SJeremy L Thompson   CeedSize          **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL;
13461c66c397SJeremy L Thompson   CeedEvalMode      **eval_modes_in = NULL, **eval_modes_out = NULL;
13471c66c397SJeremy L Thompson   CeedQFunctionField *qf_fields;
13481c66c397SJeremy L Thompson   CeedQFunction       qf;
13491c66c397SJeremy L Thompson   CeedOperatorField  *op_fields;
135001f0e615SJames Wright   bool                is_composite;
135101f0e615SJames Wright 
135201f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
135301f0e615SJames Wright   CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Can only create CeedOperator assembly data for non-composite operators.");
1354437c7c90SJeremy L Thompson 
1355437c7c90SJeremy L Thompson   // Allocate
13562b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1357ed9e99e6SJeremy L Thompson   (*data)->ceed = ceed;
13582b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1359ed9e99e6SJeremy L Thompson 
1360ed9e99e6SJeremy L Thompson   // Build OperatorAssembly data
13612b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
1362ed9e99e6SJeremy L Thompson 
1363ed9e99e6SJeremy L Thompson   // Determine active input basis
1364004e4986SSebastian Grimberg   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL));
1365004e4986SSebastian Grimberg   CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1366ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1367ed9e99e6SJeremy L Thompson     CeedVector vec;
13681c66c397SJeremy L Thompson 
13692b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1370ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
13717c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
13721c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
13731c66c397SJeremy L Thompson       CeedBasis    basis_in = NULL;
13741c66c397SJeremy L Thompson 
13752b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
13762b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1377352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp));
1378352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1379506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_active_bases_in; i++) {
1380506b1a0cSSebastian Grimberg         if ((*data)->active_bases_in[i] == basis_in) index = i;
1381437c7c90SJeremy L Thompson       }
1382437c7c90SJeremy L Thompson       if (index == -1) {
1383437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_in;
13841c66c397SJeremy L Thompson 
1385506b1a0cSSebastian Grimberg         index = num_active_bases_in;
1386506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_bases_in));
1387506b1a0cSSebastian Grimberg         (*data)->active_bases_in[num_active_bases_in] = NULL;
1388506b1a0cSSebastian Grimberg         CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases_in[num_active_bases_in]));
1389506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_elem_rstrs_in));
1390506b1a0cSSebastian Grimberg         (*data)->active_elem_rstrs_in[num_active_bases_in] = NULL;
1391437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in));
1392506b1a0cSSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs_in[num_active_bases_in]));
1393506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &num_eval_modes_in));
1394437c7c90SJeremy L Thompson         num_eval_modes_in[index] = 0;
1395506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_modes_in));
1396437c7c90SJeremy L Thompson         eval_modes_in[index] = NULL;
1397506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_mode_offsets_in));
1398437c7c90SJeremy L Thompson         eval_mode_offsets_in[index] = NULL;
1399506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->assembled_bases_in));
1400437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index] = NULL;
1401506b1a0cSSebastian Grimberg         num_active_bases_in++;
1402437c7c90SJeremy L Thompson       }
1403352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1404352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1405352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_modes_in[index]));
1406352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_mode_offsets_in[index]));
1407352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1408437c7c90SJeremy L Thompson           eval_modes_in[index][num_eval_modes_in[index] + d]        = eval_mode;
1409437c7c90SJeremy L Thompson           eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset;
1410352a5e7cSSebastian Grimberg           offset += num_comp;
1411ed9e99e6SJeremy L Thompson         }
1412352a5e7cSSebastian Grimberg         num_eval_modes_in[index] += q_comp;
1413ed9e99e6SJeremy L Thompson       }
1414ed9e99e6SJeremy L Thompson     }
1415ed9e99e6SJeremy L Thompson   }
1416ed9e99e6SJeremy L Thompson 
1417ed9e99e6SJeremy L Thompson   // Determine active output basis
14182b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields));
14192b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1420437c7c90SJeremy L Thompson   offset = 0;
1421ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1422ed9e99e6SJeremy L Thompson     CeedVector vec;
14231c66c397SJeremy L Thompson 
14242b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1425ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
14267c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
14271c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
14281c66c397SJeremy L Thompson       CeedBasis    basis_out = NULL;
14291c66c397SJeremy L Thompson 
1430437c7c90SJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
14312b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1432352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_out, &num_comp));
1433352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1434506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_active_bases_out; i++) {
1435506b1a0cSSebastian Grimberg         if ((*data)->active_bases_out[i] == basis_out) index = i;
1436437c7c90SJeremy L Thompson       }
1437437c7c90SJeremy L Thompson       if (index == -1) {
1438437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_out;
14391c66c397SJeremy L Thompson 
1440506b1a0cSSebastian Grimberg         index = num_active_bases_out;
1441506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_bases_out));
1442506b1a0cSSebastian Grimberg         (*data)->active_bases_out[num_active_bases_out] = NULL;
1443506b1a0cSSebastian Grimberg         CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases_out[num_active_bases_out]));
1444506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_elem_rstrs_out));
1445506b1a0cSSebastian Grimberg         (*data)->active_elem_rstrs_out[num_active_bases_out] = NULL;
1446437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out));
1447506b1a0cSSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs_out[num_active_bases_out]));
1448506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &num_eval_modes_out));
1449437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1450506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_modes_out));
1451437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1452506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_mode_offsets_out));
1453437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1454506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->assembled_bases_out));
1455437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1456506b1a0cSSebastian Grimberg         num_active_bases_out++;
1457437c7c90SJeremy L Thompson       }
1458352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1459352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1460352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_modes_out[index]));
1461352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_mode_offsets_out[index]));
1462352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1463437c7c90SJeremy L Thompson           eval_modes_out[index][num_eval_modes_out[index] + d]        = eval_mode;
1464437c7c90SJeremy L Thompson           eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset;
1465352a5e7cSSebastian Grimberg           offset += num_comp;
1466ed9e99e6SJeremy L Thompson         }
1467352a5e7cSSebastian Grimberg         num_eval_modes_out[index] += q_comp;
1468ed9e99e6SJeremy L Thompson       }
1469ed9e99e6SJeremy L Thompson     }
1470ed9e99e6SJeremy L Thompson   }
1471506b1a0cSSebastian Grimberg   (*data)->num_active_bases_in   = num_active_bases_in;
147227789c4aSJed Brown   (*data)->num_eval_modes_in     = num_eval_modes_in;
147327789c4aSJed Brown   (*data)->eval_modes_in         = eval_modes_in;
147427789c4aSJed Brown   (*data)->eval_mode_offsets_in  = eval_mode_offsets_in;
1475506b1a0cSSebastian Grimberg   (*data)->num_active_bases_out  = num_active_bases_out;
1476437c7c90SJeremy L Thompson   (*data)->num_eval_modes_out    = num_eval_modes_out;
1477437c7c90SJeremy L Thompson   (*data)->eval_modes_out        = eval_modes_out;
1478437c7c90SJeremy L Thompson   (*data)->eval_mode_offsets_out = eval_mode_offsets_out;
1479506b1a0cSSebastian Grimberg   (*data)->num_output_components = offset;
1480ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1481ed9e99e6SJeremy L Thompson }
1482ed9e99e6SJeremy L Thompson 
1483ed9e99e6SJeremy L Thompson /**
1484ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` @ref CeedEvalMode for assembly.
1485ba746a46SJeremy L Thompson 
1486ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1487ed9e99e6SJeremy L Thompson 
1488ca94c3ddSJeremy L Thompson   @param[in]  data                  `CeedOperatorAssemblyData`
1489506b1a0cSSebastian Grimberg   @param[out] num_active_bases_in   Total number of active bases for input
1490ca94c3ddSJeremy L Thompson   @param[out] num_eval_modes_in     Pointer to hold array of numbers of input @ref CeedEvalMode, or `NULL`.
1491ca94c3ddSJeremy L Thompson                                       `eval_modes_in[0]` holds an array of eval modes for the first active `CeedBasis`.
1492ca94c3ddSJeremy L Thompson   @param[out] eval_modes_in         Pointer to hold arrays of input @ref CeedEvalMode, or `NULL`
1493ca94c3ddSJeremy L Thompson   @param[out] eval_mode_offsets_in  Pointer to hold arrays of input offsets at each quadrature point
1494506b1a0cSSebastian Grimberg   @param[out] num_active_bases_out  Total number of active bases for output
1495ca94c3ddSJeremy L Thompson   @param[out] num_eval_modes_out    Pointer to hold array of numbers of output @ref CeedEvalMode, or `NULL`
1496ca94c3ddSJeremy L Thompson   @param[out] eval_modes_out        Pointer to hold arrays of output @ref CeedEvalMode, or `NULL`
1497437c7c90SJeremy L Thompson   @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point
1498ca94c3ddSJeremy 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
1499ed9e99e6SJeremy L Thompson 
1500ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1501ed9e99e6SJeremy L Thompson 
1502ed9e99e6SJeremy L Thompson   @ref Backend
1503ed9e99e6SJeremy L Thompson **/
1504506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedInt **num_eval_modes_in,
1505506b1a0cSSebastian Grimberg                                          const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt *num_active_bases_out,
1506506b1a0cSSebastian Grimberg                                          CeedInt **num_eval_modes_out, const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out,
1507506b1a0cSSebastian Grimberg                                          CeedSize *num_output_components) {
1508506b1a0cSSebastian Grimberg   if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in;
1509437c7c90SJeremy L Thompson   if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in;
1510437c7c90SJeremy L Thompson   if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in;
1511437c7c90SJeremy L Thompson   if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in;
1512506b1a0cSSebastian Grimberg   if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out;
1513437c7c90SJeremy L Thompson   if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out;
1514437c7c90SJeremy L Thompson   if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out;
1515437c7c90SJeremy L Thompson   if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out;
1516437c7c90SJeremy L Thompson   if (num_output_components) *num_output_components = data->num_output_components;
1517ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1518ed9e99e6SJeremy L Thompson }
1519ed9e99e6SJeremy L Thompson 
1520ed9e99e6SJeremy L Thompson /**
1521ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` `CeedBasis` data for assembly.
1522ba746a46SJeremy L Thompson 
1523ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1524ed9e99e6SJeremy L Thompson 
1525ca94c3ddSJeremy L Thompson   @param[in]  data                 `CeedOperatorAssemblyData`
1526ca94c3ddSJeremy L Thompson   @param[out] num_active_bases_in  Number of active input bases, or `NULL`
1527ca94c3ddSJeremy L Thompson   @param[out] active_bases_in      Pointer to hold active input `CeedBasis`, or `NULL`
1528ca94c3ddSJeremy L Thompson   @param[out] assembled_bases_in   Pointer to hold assembled active input `B` , or `NULL`
1529ca94c3ddSJeremy L Thompson   @param[out] num_active_bases_out Number of active output bases, or `NULL`
1530ca94c3ddSJeremy L Thompson   @param[out] active_bases_out     Pointer to hold active output `CeedBasis`, or `NULL`
1531ca94c3ddSJeremy L Thompson   @param[out] assembled_bases_out  Pointer to hold assembled active output `B` , or `NULL`
1532ed9e99e6SJeremy L Thompson 
1533ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1534ed9e99e6SJeremy L Thompson 
1535ed9e99e6SJeremy L Thompson   @ref Backend
1536ed9e99e6SJeremy L Thompson **/
1537506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedBasis **active_bases_in,
1538506b1a0cSSebastian Grimberg                                      const CeedScalar ***assembled_bases_in, CeedInt *num_active_bases_out, CeedBasis **active_bases_out,
1539506b1a0cSSebastian Grimberg                                      const CeedScalar ***assembled_bases_out) {
1540ed9e99e6SJeremy L Thompson   // Assemble B_in, B_out if needed
1541437c7c90SJeremy L Thompson   if (assembled_bases_in && !data->assembled_bases_in[0]) {
1542437c7c90SJeremy L Thompson     CeedInt num_qpts;
1543437c7c90SJeremy L Thompson 
1544506b1a0cSSebastian Grimberg     if (data->active_bases_in[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[0], &num_qpts));
1545506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_in[0], &num_qpts));
1546506b1a0cSSebastian Grimberg     for (CeedInt b = 0; b < data->num_active_bases_in; b++) {
15471c66c397SJeremy L Thompson       bool        has_eval_none = false;
1548352a5e7cSSebastian Grimberg       CeedInt     num_nodes;
1549437c7c90SJeremy L Thompson       CeedScalar *B_in = NULL, *identity = NULL;
1550ed9e99e6SJeremy L Thompson 
1551506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[b], &num_nodes));
1552352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_in[b], &B_in));
1553ed9e99e6SJeremy L Thompson 
1554437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) {
1555437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE);
1556ed9e99e6SJeremy L Thompson       }
1557ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1558352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1559352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1560352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1561ed9e99e6SJeremy L Thompson         }
1562ed9e99e6SJeremy L Thompson       }
1563ed9e99e6SJeremy L Thompson 
1564ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1565352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1566352a5e7cSSebastian Grimberg           CeedInt      d_in              = 0, q_comp_in;
1567352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE;
15681c66c397SJeremy L Thompson 
1569437c7c90SJeremy L Thompson           for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) {
1570437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_in[b] * q;
1571437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
15721c66c397SJeremy L Thompson 
1573506b1a0cSSebastian Grimberg             CeedCall(CeedOperatorGetBasisPointer(data->active_bases_in[b], data->eval_modes_in[b][e_in], identity, &B));
1574506b1a0cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_in[b], data->eval_modes_in[b][e_in], &q_comp_in));
1575352a5e7cSSebastian Grimberg             if (q_comp_in > 1) {
1576352a5e7cSSebastian Grimberg               if (e_in == 0 || data->eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0;
1577352a5e7cSSebastian Grimberg               else B = &B[(++d_in) * num_qpts * num_nodes];
1578352a5e7cSSebastian Grimberg             }
1579352a5e7cSSebastian Grimberg             eval_mode_in_prev                 = data->eval_modes_in[b][e_in];
1580352a5e7cSSebastian Grimberg             B_in[(qq + e_in) * num_nodes + n] = B[q * num_nodes + n];
1581ed9e99e6SJeremy L Thompson           }
1582ed9e99e6SJeremy L Thompson         }
1583ed9e99e6SJeremy L Thompson       }
15847c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1585437c7c90SJeremy L Thompson       data->assembled_bases_in[b] = B_in;
1586437c7c90SJeremy L Thompson     }
1587ed9e99e6SJeremy L Thompson   }
1588ed9e99e6SJeremy L Thompson 
1589437c7c90SJeremy L Thompson   if (assembled_bases_out && !data->assembled_bases_out[0]) {
1590437c7c90SJeremy L Thompson     CeedInt num_qpts;
1591437c7c90SJeremy L Thompson 
1592506b1a0cSSebastian Grimberg     if (data->active_bases_out[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[0], &num_qpts));
1593506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_out[0], &num_qpts));
1594506b1a0cSSebastian Grimberg     for (CeedInt b = 0; b < data->num_active_bases_out; b++) {
1595ed9e99e6SJeremy L Thompson       bool        has_eval_none = false;
15961c66c397SJeremy L Thompson       CeedInt     num_nodes;
1597437c7c90SJeremy L Thompson       CeedScalar *B_out = NULL, *identity = NULL;
1598ed9e99e6SJeremy L Thompson 
1599506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[b], &num_nodes));
1600352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_out[b], &B_out));
1601ed9e99e6SJeremy L Thompson 
1602437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) {
1603437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE);
1604ed9e99e6SJeremy L Thompson       }
1605ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1606352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1607352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1608352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1609ed9e99e6SJeremy L Thompson         }
1610ed9e99e6SJeremy L Thompson       }
1611ed9e99e6SJeremy L Thompson 
1612ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1613352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1614352a5e7cSSebastian Grimberg           CeedInt      d_out              = 0, q_comp_out;
1615352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
16161c66c397SJeremy L Thompson 
1617437c7c90SJeremy L Thompson           for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) {
1618437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_out[b] * q;
1619437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
16201c66c397SJeremy L Thompson 
1621506b1a0cSSebastian Grimberg             CeedCall(CeedOperatorGetBasisPointer(data->active_bases_out[b], data->eval_modes_out[b][e_out], identity, &B));
1622506b1a0cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_out[b], data->eval_modes_out[b][e_out], &q_comp_out));
1623352a5e7cSSebastian Grimberg             if (q_comp_out > 1) {
1624352a5e7cSSebastian Grimberg               if (e_out == 0 || data->eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0;
1625352a5e7cSSebastian Grimberg               else B = &B[(++d_out) * num_qpts * num_nodes];
1626352a5e7cSSebastian Grimberg             }
1627352a5e7cSSebastian Grimberg             eval_mode_out_prev                  = data->eval_modes_out[b][e_out];
1628352a5e7cSSebastian Grimberg             B_out[(qq + e_out) * num_nodes + n] = B[q * num_nodes + n];
1629ed9e99e6SJeremy L Thompson           }
1630ed9e99e6SJeremy L Thompson         }
1631ed9e99e6SJeremy L Thompson       }
16327c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1633437c7c90SJeremy L Thompson       data->assembled_bases_out[b] = B_out;
1634437c7c90SJeremy L Thompson     }
1635ed9e99e6SJeremy L Thompson   }
1636ed9e99e6SJeremy L Thompson 
1637437c7c90SJeremy L Thompson   // Pass out assembled data
1638506b1a0cSSebastian Grimberg   if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in;
1639506b1a0cSSebastian Grimberg   if (active_bases_in) *active_bases_in = data->active_bases_in;
1640437c7c90SJeremy L Thompson   if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in;
1641506b1a0cSSebastian Grimberg   if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out;
1642506b1a0cSSebastian Grimberg   if (active_bases_out) *active_bases_out = data->active_bases_out;
1643437c7c90SJeremy L Thompson   if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out;
1644437c7c90SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1645437c7c90SJeremy L Thompson }
1646437c7c90SJeremy L Thompson 
1647437c7c90SJeremy L Thompson /**
1648ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` `CeedBasis` data for assembly.
1649ba746a46SJeremy L Thompson 
1650ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1651437c7c90SJeremy L Thompson 
1652ca94c3ddSJeremy L Thompson   @param[in]  data                      `CeedOperatorAssemblyData`
1653ca94c3ddSJeremy L Thompson   @param[out] num_active_elem_rstrs_in  Number of active input element restrictions, or `NULL`
1654ca94c3ddSJeremy L Thompson   @param[out] active_elem_rstrs_in      Pointer to hold active input `CeedElemRestriction`, or `NULL`
1655ca94c3ddSJeremy L Thompson   @param[out] num_active_elem_rstrs_out Number of active output element restrictions, or `NULL`
1656ca94c3ddSJeremy L Thompson   @param[out] active_elem_rstrs_out     Pointer to hold active output `CeedElemRestriction`, or `NULL`
1657437c7c90SJeremy L Thompson 
1658437c7c90SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1659437c7c90SJeremy L Thompson 
1660437c7c90SJeremy L Thompson   @ref Backend
1661437c7c90SJeremy L Thompson **/
1662506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs_in,
1663506b1a0cSSebastian Grimberg                                                 CeedElemRestriction **active_elem_rstrs_in, CeedInt *num_active_elem_rstrs_out,
1664506b1a0cSSebastian Grimberg                                                 CeedElemRestriction **active_elem_rstrs_out) {
1665506b1a0cSSebastian Grimberg   if (num_active_elem_rstrs_in) *num_active_elem_rstrs_in = data->num_active_bases_in;
1666506b1a0cSSebastian Grimberg   if (active_elem_rstrs_in) *active_elem_rstrs_in = data->active_elem_rstrs_in;
1667506b1a0cSSebastian Grimberg   if (num_active_elem_rstrs_out) *num_active_elem_rstrs_out = data->num_active_bases_out;
1668506b1a0cSSebastian Grimberg   if (active_elem_rstrs_out) *active_elem_rstrs_out = data->active_elem_rstrs_out;
1669ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1670ed9e99e6SJeremy L Thompson }
1671ed9e99e6SJeremy L Thompson 
1672ed9e99e6SJeremy L Thompson /**
1673ca94c3ddSJeremy L Thompson   @brief Destroy `CeedOperatorAssemblyData`
1674ed9e99e6SJeremy L Thompson 
1675ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedOperatorAssemblyData` to destroy
1676ed9e99e6SJeremy L Thompson 
1677ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1678ed9e99e6SJeremy L Thompson 
1679ed9e99e6SJeremy L Thompson   @ref Backend
1680ed9e99e6SJeremy L Thompson **/
1681ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) {
1682ad6481ceSJeremy L Thompson   if (!*data) {
1683ad6481ceSJeremy L Thompson     *data = NULL;
1684ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1685ad6481ceSJeremy L Thompson   }
16862b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
1687506b1a0cSSebastian Grimberg   for (CeedInt b = 0; b < (*data)->num_active_bases_in; b++) {
1688506b1a0cSSebastian Grimberg     CeedCall(CeedBasisDestroy(&(*data)->active_bases_in[b]));
1689506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_in[b]));
1690437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_in[b]));
1691437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b]));
1692437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_in[b]));
1693506b1a0cSSebastian Grimberg   }
1694506b1a0cSSebastian Grimberg   for (CeedInt b = 0; b < (*data)->num_active_bases_out; b++) {
1695506b1a0cSSebastian Grimberg     CeedCall(CeedBasisDestroy(&(*data)->active_bases_out[b]));
1696506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_out[b]));
1697506b1a0cSSebastian Grimberg     CeedCall(CeedFree(&(*data)->eval_modes_out[b]));
1698506b1a0cSSebastian Grimberg     CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b]));
1699437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_out[b]));
1700437c7c90SJeremy L Thompson   }
1701506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_bases_in));
1702506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_bases_out));
1703506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_elem_rstrs_in));
1704506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_elem_rstrs_out));
1705437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_in));
1706437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_out));
1707437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_in));
1708437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_out));
1709437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_in));
1710437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_out));
1711437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_in));
1712437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_out));
1713ed9e99e6SJeremy L Thompson 
17142b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1715ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1716ed9e99e6SJeremy L Thompson }
1717ed9e99e6SJeremy L Thompson 
17184dd1a9d2SSebastian Grimberg /**
1719ca94c3ddSJeremy L Thompson   @brief Retrieve fallback `CeedOperator` with a reference `Ceed` for advanced `CeedOperator` functionality
17204dd1a9d2SSebastian Grimberg 
1721ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to retrieve fallback for
1722ca94c3ddSJeremy L Thompson   @param[out] op_fallback Fallback `CeedOperator`
17234dd1a9d2SSebastian Grimberg 
17244dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
17254dd1a9d2SSebastian Grimberg 
17264dd1a9d2SSebastian Grimberg   @ref Backend
17274dd1a9d2SSebastian Grimberg **/
17284dd1a9d2SSebastian Grimberg int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) {
17294dd1a9d2SSebastian Grimberg   // Create if needed
17304dd1a9d2SSebastian Grimberg   if (!op->op_fallback) CeedCall(CeedOperatorCreateFallback(op));
17314dd1a9d2SSebastian Grimberg   if (op->op_fallback) {
17324dd1a9d2SSebastian Grimberg     bool is_debug;
17331203703bSJeremy L Thompson     Ceed ceed;
17344dd1a9d2SSebastian Grimberg 
17354dd1a9d2SSebastian Grimberg     CeedCall(CeedOperatorGetCeed(op, &ceed));
17361203703bSJeremy L Thompson     CeedCall(CeedIsDebug(ceed, &is_debug));
17371203703bSJeremy L Thompson     if (is_debug) {
17381203703bSJeremy L Thompson       Ceed        ceed_fallback;
17391203703bSJeremy L Thompson       const char *resource, *resource_fallback;
17401203703bSJeremy L Thompson 
17414dd1a9d2SSebastian Grimberg       CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
17424dd1a9d2SSebastian Grimberg       CeedCall(CeedGetResource(ceed, &resource));
17434dd1a9d2SSebastian Grimberg       CeedCall(CeedGetResource(ceed_fallback, &resource_fallback));
17444dd1a9d2SSebastian Grimberg 
17454dd1a9d2SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n");
1746249f8407SJeremy 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);
17474dd1a9d2SSebastian Grimberg     }
17484dd1a9d2SSebastian Grimberg   }
17494dd1a9d2SSebastian Grimberg   *op_fallback = op->op_fallback;
17504dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
17514dd1a9d2SSebastian Grimberg }
17524dd1a9d2SSebastian Grimberg 
17534dd1a9d2SSebastian Grimberg /**
1754ca94c3ddSJeremy L Thompson   @brief Get the parent `CeedOperator` for a fallback `CeedOperator`
17554dd1a9d2SSebastian Grimberg 
1756ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` context
1757ca94c3ddSJeremy L Thompson   @param[out] parent Variable to store parent `CeedOperator` context
17584dd1a9d2SSebastian Grimberg 
17594dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
17604dd1a9d2SSebastian Grimberg 
17614dd1a9d2SSebastian Grimberg   @ref Backend
17624dd1a9d2SSebastian Grimberg **/
17634dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParent(CeedOperator op, CeedOperator *parent) {
17644dd1a9d2SSebastian Grimberg   *parent = op->op_fallback_parent ? op->op_fallback_parent : NULL;
17654dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
17664dd1a9d2SSebastian Grimberg }
17674dd1a9d2SSebastian Grimberg 
17684dd1a9d2SSebastian Grimberg /**
1769ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` context of the parent `CeedOperator` for a fallback `CeedOperator`
17704dd1a9d2SSebastian Grimberg 
1771ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` context
1772ca94c3ddSJeremy L Thompson   @param[out] parent Variable to store parent `Ceed` context
17734dd1a9d2SSebastian Grimberg 
17744dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
17754dd1a9d2SSebastian Grimberg 
17764dd1a9d2SSebastian Grimberg   @ref Backend
17774dd1a9d2SSebastian Grimberg **/
17784dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParentCeed(CeedOperator op, Ceed *parent) {
17794dd1a9d2SSebastian Grimberg   *parent = op->op_fallback_parent ? op->op_fallback_parent->ceed : op->ceed;
17804dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
17814dd1a9d2SSebastian Grimberg }
17824dd1a9d2SSebastian Grimberg 
1783480fae85SJeremy L Thompson /// @}
1784480fae85SJeremy L Thompson 
1785480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1786eaf62fffSJeremy L Thompson /// CeedOperator Public API
1787eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1788eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser
1789eaf62fffSJeremy L Thompson /// @{
1790eaf62fffSJeremy L Thompson 
1791eaf62fffSJeremy L Thompson /**
1792ca94c3ddSJeremy L Thompson   @brief Assemble a linear `CeedQFunction` associated with a `CeedOperator`.
1793eaf62fffSJeremy L Thompson 
1794ca94c3ddSJeremy L Thompson   This returns a `CeedVector` containing a matrix at each quadrature point providing the action of the `CeedQFunction` associated with the `CeedOperator`.
1795ca94c3ddSJeremy 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.
1796859c15bbSJames Wright 
1797ca94c3ddSJeremy L Thompson   Inputs and outputs are in the order provided by the user when adding `CeedOperator` fields.
1798ca94c3ddSJeremy 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]`.
1799eaf62fffSJeremy L Thompson 
1800ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1801f04ea552SJeremy L Thompson 
1802ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1803ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points
1804ca94c3ddSJeremy L Thompson   @param[out] rstr      `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1805ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1806eaf62fffSJeremy L Thompson 
1807eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1808eaf62fffSJeremy L Thompson 
1809eaf62fffSJeremy L Thompson   @ref User
1810eaf62fffSJeremy L Thompson **/
18112b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
18122b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1813eaf62fffSJeremy L Thompson 
1814eaf62fffSJeremy L Thompson   if (op->LinearAssembleQFunction) {
1815d04bbc78SJeremy L Thompson     // Backend version
18162b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request));
1817eaf62fffSJeremy L Thompson   } else {
1818d04bbc78SJeremy L Thompson     // Operator fallback
18191203703bSJeremy L Thompson     Ceed         ceed;
1820d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1821d04bbc78SJeremy L Thompson 
18221203703bSJeremy L Thompson     CeedCall(CeedOperatorGetCeed(op, &ceed));
18232b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
18246574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request));
18251203703bSJeremy L Thompson     else return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction");
182670a7ffb3SJeremy L Thompson   }
1827eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1828eaf62fffSJeremy L Thompson }
182970a7ffb3SJeremy L Thompson 
183070a7ffb3SJeremy L Thompson /**
1831ca94c3ddSJeremy L Thompson   @brief Assemble `CeedQFunction` and store result internally.
18324385fb7fSSebastian Grimberg 
1833ea61e9acSJeremy L Thompson   Return copied references of stored data to the caller.
1834ea61e9acSJeremy L Thompson   Caller is responsible for ownership and destruction of the copied references.
1835ca94c3ddSJeremy L Thompson   See also @ref CeedOperatorLinearAssembleQFunction().
183670a7ffb3SJeremy L Thompson 
1837ca94c3ddSJeremy 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.
1838c5f45aeaSJeremy L Thompson         These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object.
1839c5f45aeaSJeremy L Thompson 
1840ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1841ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points
1842ca94c3ddSJeremy L Thompson   @param[out] rstr      `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1843ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
184470a7ffb3SJeremy L Thompson 
184570a7ffb3SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
184670a7ffb3SJeremy L Thompson 
184770a7ffb3SJeremy L Thompson   @ref User
184870a7ffb3SJeremy L Thompson **/
18492b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
1850b05f7e9fSJeremy L Thompson   int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *) = NULL;
1851b05f7e9fSJeremy L Thompson   CeedOperator op_assemble                                                                           = NULL;
1852bb229da9SJeremy L Thompson   CeedOperator op_fallback_parent                                                                    = NULL;
1853b05f7e9fSJeremy L Thompson 
18542b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
185570a7ffb3SJeremy L Thompson 
1856b05f7e9fSJeremy L Thompson   // Determine if fallback parent or operator has implementation
1857bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParent(op, &op_fallback_parent));
1858bb229da9SJeremy L Thompson   if (op_fallback_parent && op_fallback_parent->LinearAssembleQFunctionUpdate) {
1859b05f7e9fSJeremy L Thompson     // -- Backend version for op fallback parent is faster, if it exists
1860bb229da9SJeremy L Thompson     LinearAssembleQFunctionUpdate = op_fallback_parent->LinearAssembleQFunctionUpdate;
1861bb229da9SJeremy L Thompson     op_assemble                   = op_fallback_parent;
1862b05f7e9fSJeremy L Thompson   } else if (op->LinearAssembleQFunctionUpdate) {
1863b05f7e9fSJeremy L Thompson     // -- Backend version for op
1864b05f7e9fSJeremy L Thompson     LinearAssembleQFunctionUpdate = op->LinearAssembleQFunctionUpdate;
1865b05f7e9fSJeremy L Thompson     op_assemble                   = op;
1866b05f7e9fSJeremy L Thompson   }
1867b05f7e9fSJeremy L Thompson 
1868b05f7e9fSJeremy L Thompson   // Assemble QFunction
1869b05f7e9fSJeremy L Thompson   if (LinearAssembleQFunctionUpdate) {
1870b05f7e9fSJeremy L Thompson     // Backend or fallback parent version
1871480fae85SJeremy L Thompson     bool                qf_assembled_is_setup;
18722efa2d85SJeremy L Thompson     CeedVector          assembled_vec  = NULL;
18732efa2d85SJeremy L Thompson     CeedElemRestriction assembled_rstr = NULL;
1874480fae85SJeremy L Thompson 
18752b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataIsSetup(op->qf_assembled, &qf_assembled_is_setup));
1876480fae85SJeremy L Thompson     if (qf_assembled_is_setup) {
1877d04bbc78SJeremy L Thompson       bool update_needed;
1878d04bbc78SJeremy L Thompson 
18792b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataGetObjects(op->qf_assembled, &assembled_vec, &assembled_rstr));
18802b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(op->qf_assembled, &update_needed));
1881b05f7e9fSJeremy L Thompson       if (update_needed) CeedCall(LinearAssembleQFunctionUpdate(op_assemble, assembled_vec, assembled_rstr, request));
188270a7ffb3SJeremy L Thompson     } else {
1883b05f7e9fSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunction(op_assemble, &assembled_vec, &assembled_rstr, request));
18842b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataSetObjects(op->qf_assembled, assembled_vec, assembled_rstr));
188570a7ffb3SJeremy L Thompson     }
18862b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, false));
18872efa2d85SJeremy L Thompson 
1888d04bbc78SJeremy L Thompson     // Copy reference from internally held copy
18892b730f8bSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled));
18902b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr));
1891c5f45aeaSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled_vec));
18922b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&assembled_rstr));
189370a7ffb3SJeremy L Thompson   } else {
1894d04bbc78SJeremy L Thompson     // Operator fallback
18951203703bSJeremy L Thompson     Ceed         ceed;
1896d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1897d04bbc78SJeremy L Thompson 
18981203703bSJeremy L Thompson     CeedCall(CeedOperatorGetCeed(op, &ceed));
18992b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
19006574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request));
19011203703bSJeremy L Thompson     else return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate");
190270a7ffb3SJeremy L Thompson   }
190370a7ffb3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1904eaf62fffSJeremy L Thompson }
1905eaf62fffSJeremy L Thompson 
1906eaf62fffSJeremy L Thompson /**
1907ca94c3ddSJeremy L Thompson   @brief Assemble the diagonal of a square linear `CeedOperator`
1908eaf62fffSJeremy L Thompson 
1909ca94c3ddSJeremy L Thompson   This overwrites a `CeedVector` with the diagonal of a linear `CeedOperator`.
1910eaf62fffSJeremy L Thompson 
1911ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
1912eaf62fffSJeremy L Thompson 
1913ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1914f04ea552SJeremy L Thompson 
1915ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1916ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal
1917ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1918eaf62fffSJeremy L Thompson 
1919eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1920eaf62fffSJeremy L Thompson 
1921eaf62fffSJeremy L Thompson   @ref User
1922eaf62fffSJeremy L Thompson **/
19232b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1924f3d47e36SJeremy L Thompson   bool     is_composite;
19251c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
19261203703bSJeremy L Thompson   Ceed     ceed;
19271c66c397SJeremy L Thompson 
19281203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
19292b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1930f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1931eaf62fffSJeremy L Thompson 
19322b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
19331203703bSJeremy L Thompson   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1934c9366a6bSJeremy L Thompson 
1935f3d47e36SJeremy L Thompson   // Early exit for empty operator
1936f3d47e36SJeremy L Thompson   if (!is_composite) {
1937f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1938f3d47e36SJeremy L Thompson 
1939f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1940f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1941f3d47e36SJeremy L Thompson   }
1942f3d47e36SJeremy L Thompson 
1943eaf62fffSJeremy L Thompson   if (op->LinearAssembleDiagonal) {
1944d04bbc78SJeremy L Thompson     // Backend version
19452b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleDiagonal(op, assembled, request));
1946eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1947eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddDiagonal) {
1948d04bbc78SJeremy L Thompson     // Backend version with zeroing first
19492b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
19502b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1951eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1952eaf62fffSJeremy L Thompson   } else {
1953d04bbc78SJeremy L Thompson     // Operator fallback
1954d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1955d04bbc78SJeremy L Thompson 
19562b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1957d04bbc78SJeremy L Thompson     if (op_fallback) {
19582b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request));
1959eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1960eaf62fffSJeremy L Thompson     }
1961eaf62fffSJeremy L Thompson   }
1962eaf62fffSJeremy L Thompson   // Default interface implementation
19632b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
19642b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request));
1965eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1966eaf62fffSJeremy L Thompson }
1967eaf62fffSJeremy L Thompson 
1968eaf62fffSJeremy L Thompson /**
1969ca94c3ddSJeremy L Thompson   @brief Assemble the diagonal of a square linear `CeedOperator`.
1970eaf62fffSJeremy L Thompson 
1971ca94c3ddSJeremy L Thompson   This sums into a `CeedVector` the diagonal of a linear `CeedOperator`.
1972eaf62fffSJeremy L Thompson 
1973ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
1974eaf62fffSJeremy L Thompson 
1975ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1976f04ea552SJeremy L Thompson 
1977ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1978ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal
1979ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1980eaf62fffSJeremy L Thompson 
1981eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1982eaf62fffSJeremy L Thompson 
1983eaf62fffSJeremy L Thompson   @ref User
1984eaf62fffSJeremy L Thompson **/
19852b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1986f3d47e36SJeremy L Thompson   bool     is_composite;
19871c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
19881203703bSJeremy L Thompson   Ceed     ceed;
19891c66c397SJeremy L Thompson 
19901203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
19912b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1992f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1993eaf62fffSJeremy L Thompson 
19942b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
19951203703bSJeremy L Thompson   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1996c9366a6bSJeremy L Thompson 
1997f3d47e36SJeremy L Thompson   // Early exit for empty operator
1998f3d47e36SJeremy L Thompson   if (!is_composite) {
1999f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2000f3d47e36SJeremy L Thompson 
2001f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2002f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2003f3d47e36SJeremy L Thompson   }
2004f3d47e36SJeremy L Thompson 
2005eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddDiagonal) {
2006d04bbc78SJeremy L Thompson     // Backend version
20072b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
2008eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2009eaf62fffSJeremy L Thompson   } else {
2010d04bbc78SJeremy L Thompson     // Operator fallback
2011d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2012d04bbc78SJeremy L Thompson 
20132b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2014d04bbc78SJeremy L Thompson     if (op_fallback) {
20152b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
2016eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2017eaf62fffSJeremy L Thompson     }
2018eaf62fffSJeremy L Thompson   }
2019eaf62fffSJeremy L Thompson   // Default interface implementation
2020eaf62fffSJeremy L Thompson   if (is_composite) {
20212b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
2022eaf62fffSJeremy L Thompson   } else {
2023f3bd9308SJeremy L Thompson     CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
2024eaf62fffSJeremy L Thompson   }
2025d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2026eaf62fffSJeremy L Thompson }
2027eaf62fffSJeremy L Thompson 
2028eaf62fffSJeremy L Thompson /**
2029ca94c3ddSJeremy L Thompson    @brief Fully assemble the point-block diagonal pattern of a linear `CeedOperator`.
203001f0e615SJames Wright 
2031ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssemblePointBlockDiagonal().
203201f0e615SJames Wright 
2033ca94c3ddSJeremy 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)`.
2034ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are unique.
2035ca94c3ddSJeremy L Thompson    This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemblePointBlockDiagonal() provides the values in the same ordering.
203601f0e615SJames Wright 
203701f0e615SJames Wright    This will generally be slow unless your operator is low-order.
203801f0e615SJames Wright 
2039ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
204001f0e615SJames Wright 
2041ca94c3ddSJeremy L Thompson    @param[in]  op          `CeedOperator` to assemble
204201f0e615SJames Wright    @param[out] num_entries Number of entries in coordinate nonzero pattern
204301f0e615SJames Wright    @param[out] rows        Row number for each entry
204401f0e615SJames Wright    @param[out] cols        Column number for each entry
204501f0e615SJames Wright 
204601f0e615SJames Wright    @ref User
204701f0e615SJames Wright **/
204801f0e615SJames Wright int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
204901f0e615SJames Wright   Ceed          ceed;
205001f0e615SJames Wright   bool          is_composite;
205101f0e615SJames Wright   CeedInt       num_active_components, num_sub_operators;
205201f0e615SJames Wright   CeedOperator *sub_operators;
205301f0e615SJames Wright 
205401f0e615SJames Wright   CeedCall(CeedOperatorGetCeed(op, &ceed));
205501f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
205601f0e615SJames Wright 
205701f0e615SJames Wright   CeedSize input_size = 0, output_size = 0;
205801f0e615SJames Wright   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
205901f0e615SJames Wright   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
206001f0e615SJames Wright 
206101f0e615SJames Wright   if (is_composite) {
206201f0e615SJames Wright     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_operators));
206301f0e615SJames Wright     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
206401f0e615SJames Wright   } else {
206501f0e615SJames Wright     sub_operators     = &op;
206601f0e615SJames Wright     num_sub_operators = 1;
206701f0e615SJames Wright   }
206801f0e615SJames Wright 
2069506b1a0cSSebastian Grimberg   // Verify operator can be assembled correctly
2070506b1a0cSSebastian Grimberg   {
207101f0e615SJames Wright     CeedOperatorAssemblyData data;
2072506b1a0cSSebastian Grimberg     CeedInt                  num_active_elem_rstrs, comp_stride;
207301f0e615SJames Wright     CeedElemRestriction     *active_elem_rstrs;
207401f0e615SJames Wright 
207501f0e615SJames Wright     // Get initial values to check against
207601f0e615SJames Wright     CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[0], &data));
2077506b1a0cSSebastian Grimberg     CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL));
207801f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[0], &comp_stride));
207901f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[0], &num_active_components));
208001f0e615SJames Wright 
2081506b1a0cSSebastian Grimberg     // Verify that all active element restrictions have same component stride and number of components
208201f0e615SJames Wright     for (CeedInt k = 0; k < num_sub_operators; k++) {
208301f0e615SJames Wright       CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[k], &data));
2084506b1a0cSSebastian Grimberg       CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL));
208501f0e615SJames Wright       for (CeedInt i = 0; i < num_active_elem_rstrs; i++) {
2086506b1a0cSSebastian Grimberg         CeedInt comp_stride_sub, num_active_components_sub;
2087506b1a0cSSebastian Grimberg 
208801f0e615SJames Wright         CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[i], &comp_stride_sub));
208901f0e615SJames Wright         CeedCheck(comp_stride == comp_stride_sub, ceed, CEED_ERROR_DIMENSION,
209001f0e615SJames Wright                   "Active element restrictions must have the same component stride: %d vs %d", comp_stride, comp_stride_sub);
209101f0e615SJames Wright         CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[i], &num_active_components_sub));
209201f0e615SJames Wright         CeedCheck(num_active_components == num_active_components_sub, ceed, CEED_ERROR_INCOMPATIBLE,
209301f0e615SJames Wright                   "All suboperators must have the same number of output components");
209401f0e615SJames Wright       }
209501f0e615SJames Wright     }
209601f0e615SJames Wright   }
209701f0e615SJames Wright   *num_entries = input_size * num_active_components;
209801f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, rows));
209901f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, cols));
210001f0e615SJames Wright 
210101f0e615SJames Wright   for (CeedInt o = 0; o < num_sub_operators; o++) {
2102506b1a0cSSebastian Grimberg     CeedElemRestriction active_elem_rstr, point_block_active_elem_rstr;
210301f0e615SJames Wright     CeedInt             comp_stride, num_elem, elem_size;
2104506b1a0cSSebastian Grimberg     const CeedInt      *offsets, *point_block_offsets;
210501f0e615SJames Wright 
210601f0e615SJames Wright     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[o], &active_elem_rstr));
210701f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstr, &comp_stride));
210801f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumElements(active_elem_rstr, &num_elem));
210901f0e615SJames Wright     CeedCall(CeedElemRestrictionGetElementSize(active_elem_rstr, &elem_size));
211001f0e615SJames Wright     CeedCall(CeedElemRestrictionGetOffsets(active_elem_rstr, CEED_MEM_HOST, &offsets));
211101f0e615SJames Wright 
2112506b1a0cSSebastian Grimberg     CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstr, &point_block_active_elem_rstr));
2113506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOffsets(point_block_active_elem_rstr, CEED_MEM_HOST, &point_block_offsets));
211401f0e615SJames Wright 
211501f0e615SJames Wright     for (CeedSize i = 0; i < num_elem * elem_size; i++) {
211601f0e615SJames Wright       for (CeedInt c_out = 0; c_out < num_active_components; c_out++) {
211701f0e615SJames Wright         for (CeedInt c_in = 0; c_in < num_active_components; c_in++) {
2118506b1a0cSSebastian Grimberg           (*rows)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_out * comp_stride;
2119506b1a0cSSebastian Grimberg           (*cols)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_in * comp_stride;
212001f0e615SJames Wright         }
212101f0e615SJames Wright       }
212201f0e615SJames Wright     }
212301f0e615SJames Wright 
212401f0e615SJames Wright     CeedCall(CeedElemRestrictionRestoreOffsets(active_elem_rstr, &offsets));
2125506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOffsets(point_block_active_elem_rstr, &point_block_offsets));
2126506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&point_block_active_elem_rstr));
212701f0e615SJames Wright   }
212801f0e615SJames Wright   return CEED_ERROR_SUCCESS;
212901f0e615SJames Wright }
213001f0e615SJames Wright 
213101f0e615SJames Wright /**
2132ca94c3ddSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear `CeedOperator`.
2133eaf62fffSJeremy L Thompson 
2134ca94c3ddSJeremy L Thompson   This overwrites a `CeedVector` with the point block diagonal of a linear `CeedOperator`.
2135eaf62fffSJeremy L Thompson 
2136ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2137eaf62fffSJeremy L Thompson 
2138ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2139f04ea552SJeremy L Thompson 
2140ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2141ca94c3ddSJeremy 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.
2142ca94c3ddSJeremy L Thompson                           The dimensions of this vector are derived from the active vector for the `CeedOperator`.
2143ca94c3ddSJeremy L Thompson                           The array has shape `[nodes, component out, component in]`.
2144ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2145eaf62fffSJeremy L Thompson 
2146eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2147eaf62fffSJeremy L Thompson 
2148eaf62fffSJeremy L Thompson   @ref User
2149eaf62fffSJeremy L Thompson **/
21502b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2151f3d47e36SJeremy L Thompson   bool     is_composite;
21521c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
21531203703bSJeremy L Thompson   Ceed     ceed;
21541c66c397SJeremy L Thompson 
21551203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
21562b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2157f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2158eaf62fffSJeremy L Thompson 
21592b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
21601203703bSJeremy L Thompson   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
2161c9366a6bSJeremy L Thompson 
2162f3d47e36SJeremy L Thompson   // Early exit for empty operator
2163f3d47e36SJeremy L Thompson   if (!is_composite) {
2164f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2165f3d47e36SJeremy L Thompson 
2166f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2167f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2168f3d47e36SJeremy L Thompson   }
2169f3d47e36SJeremy L Thompson 
2170eaf62fffSJeremy L Thompson   if (op->LinearAssemblePointBlockDiagonal) {
2171d04bbc78SJeremy L Thompson     // Backend version
21722b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request));
2173eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2174eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddPointBlockDiagonal) {
2175d04bbc78SJeremy L Thompson     // Backend version with zeroing first
21762b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
21772b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
2178eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2179eaf62fffSJeremy L Thompson   } else {
2180d04bbc78SJeremy L Thompson     // Operator fallback
2181d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2182d04bbc78SJeremy L Thompson 
21832b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2184d04bbc78SJeremy L Thompson     if (op_fallback) {
21852b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request));
2186eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2187eaf62fffSJeremy L Thompson     }
2188eaf62fffSJeremy L Thompson   }
2189eaf62fffSJeremy L Thompson   // Default interface implementation
21902b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
21912b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
2192eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2193eaf62fffSJeremy L Thompson }
2194eaf62fffSJeremy L Thompson 
2195eaf62fffSJeremy L Thompson /**
2196ca94c3ddSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear `CeedOperator`.
2197eaf62fffSJeremy L Thompson 
2198ca94c3ddSJeremy L Thompson   This sums into a `CeedVector` with the point block diagonal of a linear `CeedOperator`.
2199eaf62fffSJeremy L Thompson 
2200ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2201eaf62fffSJeremy L Thompson 
2202ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2203f04ea552SJeremy L Thompson 
2204ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2205ca94c3ddSJeremy 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.
2206ca94c3ddSJeremy L Thompson                           The dimensions of this vector are derived from the active vector for the `CeedOperator`.
2207ca94c3ddSJeremy L Thompson                           The array has shape `[nodes, component out, component in]`.
2208ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2209eaf62fffSJeremy L Thompson 
2210eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2211eaf62fffSJeremy L Thompson 
2212eaf62fffSJeremy L Thompson   @ref User
2213eaf62fffSJeremy L Thompson **/
22142b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2215f3d47e36SJeremy L Thompson   bool     is_composite;
22161c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
22171203703bSJeremy L Thompson   Ceed     ceed;
22181c66c397SJeremy L Thompson 
22191203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
22202b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2221f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2222eaf62fffSJeremy L Thompson 
22232b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
22241203703bSJeremy L Thompson   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
2225c9366a6bSJeremy L Thompson 
2226f3d47e36SJeremy L Thompson   // Early exit for empty operator
2227f3d47e36SJeremy L Thompson   if (!is_composite) {
2228f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2229f3d47e36SJeremy L Thompson 
2230f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2231f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2232f3d47e36SJeremy L Thompson   }
2233f3d47e36SJeremy L Thompson 
2234eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddPointBlockDiagonal) {
2235d04bbc78SJeremy L Thompson     // Backend version
22362b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request));
2237eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2238eaf62fffSJeremy L Thompson   } else {
2239d04bbc78SJeremy L Thompson     // Operator fallback
2240d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2241d04bbc78SJeremy L Thompson 
22422b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2243d04bbc78SJeremy L Thompson     if (op_fallback) {
22442b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request));
2245eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2246eaf62fffSJeremy L Thompson     }
2247eaf62fffSJeremy L Thompson   }
2248ea61e9acSJeremy L Thompson   // Default interface implementation
2249eaf62fffSJeremy L Thompson   if (is_composite) {
22502b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
2251eaf62fffSJeremy L Thompson   } else {
2252f3bd9308SJeremy L Thompson     CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
2253eaf62fffSJeremy L Thompson   }
2254d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2255eaf62fffSJeremy L Thompson }
2256eaf62fffSJeremy L Thompson 
2257eaf62fffSJeremy L Thompson /**
2258ca94c3ddSJeremy L Thompson    @brief Fully assemble the nonzero pattern of a linear `CeedOperator`.
2259eaf62fffSJeremy L Thompson 
2260ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssemble().
2261eaf62fffSJeremy L Thompson 
2262ca94c3ddSJeremy 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)`.
2263ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are not unique and may repeat.
2264ca94c3ddSJeremy L Thompson    This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemble() provides the values in the same ordering.
2265eaf62fffSJeremy L Thompson 
2266eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2267eaf62fffSJeremy L Thompson 
2268ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2269f04ea552SJeremy L Thompson 
2270ca94c3ddSJeremy L Thompson    @param[in]  op          `CeedOperator` to assemble
2271eaf62fffSJeremy L Thompson    @param[out] num_entries Number of entries in coordinate nonzero pattern
2272eaf62fffSJeremy L Thompson    @param[out] rows        Row number for each entry
2273eaf62fffSJeremy L Thompson    @param[out] cols        Column number for each entry
2274eaf62fffSJeremy L Thompson 
2275eaf62fffSJeremy L Thompson    @ref User
2276eaf62fffSJeremy L Thompson **/
22772b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
22781c66c397SJeremy L Thompson   bool          is_composite;
22791c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2280b94338b9SJed Brown   CeedSize      single_entries;
2281eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
22821c66c397SJeremy L Thompson 
22832b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2284f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2285eaf62fffSJeremy L Thompson 
2286eaf62fffSJeremy L Thompson   if (op->LinearAssembleSymbolic) {
2287d04bbc78SJeremy L Thompson     // Backend version
22882b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols));
2289eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2290eaf62fffSJeremy L Thompson   } else {
2291d04bbc78SJeremy L Thompson     // Operator fallback
2292d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2293d04bbc78SJeremy L Thompson 
22942b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2295d04bbc78SJeremy L Thompson     if (op_fallback) {
22962b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols));
2297eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2298eaf62fffSJeremy L Thompson     }
2299eaf62fffSJeremy L Thompson   }
2300eaf62fffSJeremy L Thompson 
2301eaf62fffSJeremy L Thompson   // Default interface implementation
2302eaf62fffSJeremy L Thompson 
2303506b1a0cSSebastian Grimberg   // Count entries and allocate rows, cols arrays
2304eaf62fffSJeremy L Thompson   *num_entries = 0;
2305eaf62fffSJeremy L Thompson   if (is_composite) {
2306c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2307c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
230892ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
23092b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2310eaf62fffSJeremy L Thompson       *num_entries += single_entries;
2311eaf62fffSJeremy L Thompson     }
2312eaf62fffSJeremy L Thompson   } else {
23132b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries));
2314eaf62fffSJeremy L Thompson     *num_entries += single_entries;
2315eaf62fffSJeremy L Thompson   }
23162b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, rows));
23172b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, cols));
2318eaf62fffSJeremy L Thompson 
2319506b1a0cSSebastian Grimberg   // Assemble nonzero locations
2320eaf62fffSJeremy L Thompson   if (is_composite) {
2321c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2322c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
232392ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
23242b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols));
23252b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2326eaf62fffSJeremy L Thompson       offset += single_entries;
2327eaf62fffSJeremy L Thompson     }
2328eaf62fffSJeremy L Thompson   } else {
23292b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols));
2330eaf62fffSJeremy L Thompson   }
2331eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2332eaf62fffSJeremy L Thompson }
2333eaf62fffSJeremy L Thompson 
2334eaf62fffSJeremy L Thompson /**
2335eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero entries of a linear operator.
2336eaf62fffSJeremy L Thompson 
2337ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssembleSymbolic().
2338eaf62fffSJeremy L Thompson 
2339ca94c3ddSJeremy 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)`.
2340ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are not unique and may repeat.
2341ca94c3ddSJeremy L Thompson    This function returns the values of the nonzero entries to be added, their `(i, j)` locations are provided by @ref CeedOperatorLinearAssembleSymbolic().
2342eaf62fffSJeremy L Thompson 
2343eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2344eaf62fffSJeremy L Thompson 
2345ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2346f04ea552SJeremy L Thompson 
2347ca94c3ddSJeremy L Thompson    @param[in]  op     `CeedOperator` to assemble
2348eaf62fffSJeremy L Thompson    @param[out] values Values to assemble into matrix
2349eaf62fffSJeremy L Thompson 
2350eaf62fffSJeremy L Thompson    @ref User
2351eaf62fffSJeremy L Thompson **/
2352eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) {
23531c66c397SJeremy L Thompson   bool          is_composite;
23541c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2355b94338b9SJed Brown   CeedSize      single_entries = 0;
2356eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
23571c66c397SJeremy L Thompson 
23582b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2359f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2360f3d47e36SJeremy L Thompson 
2361f3d47e36SJeremy L Thompson   // Early exit for empty operator
2362f3d47e36SJeremy L Thompson   if (!is_composite) {
2363f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2364f3d47e36SJeremy L Thompson 
2365f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2366f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2367f3d47e36SJeremy L Thompson   }
2368eaf62fffSJeremy L Thompson 
2369eaf62fffSJeremy L Thompson   if (op->LinearAssemble) {
2370d04bbc78SJeremy L Thompson     // Backend version
23712b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemble(op, values));
2372eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2373eaf62fffSJeremy L Thompson   } else {
2374d04bbc78SJeremy L Thompson     // Operator fallback
2375d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2376d04bbc78SJeremy L Thompson 
23772b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2378d04bbc78SJeremy L Thompson     if (op_fallback) {
23792b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemble(op_fallback, values));
2380eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2381eaf62fffSJeremy L Thompson     }
2382eaf62fffSJeremy L Thompson   }
2383eaf62fffSJeremy L Thompson 
2384eaf62fffSJeremy L Thompson   // Default interface implementation
238528ec399dSJeremy L Thompson   CeedCall(CeedVectorSetValue(values, 0.0));
2386eaf62fffSJeremy L Thompson   if (is_composite) {
2387c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2388c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2389cefa2673SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; k++) {
23902b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values));
23912b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2392eaf62fffSJeremy L Thompson       offset += single_entries;
2393eaf62fffSJeremy L Thompson     }
2394eaf62fffSJeremy L Thompson   } else {
23952b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemble(op, offset, values));
2396eaf62fffSJeremy L Thompson   }
2397eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2398eaf62fffSJeremy L Thompson }
2399eaf62fffSJeremy L Thompson 
2400eaf62fffSJeremy L Thompson /**
2401ca94c3ddSJeremy L Thompson   @brief Get the multiplicity of nodes across sub-operators in a composite `CeedOperator`.
240275f0d5a4SJeremy L Thompson 
2403ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
240475f0d5a4SJeremy L Thompson 
2405ca94c3ddSJeremy L Thompson   @param[in]  op               Composite `CeedOperator`
2406ca94c3ddSJeremy L Thompson   @param[in]  num_skip_indices Number of sub-operators to skip
2407ca94c3ddSJeremy L Thompson   @param[in]  skip_indices     Array of indices of sub-operators to skip
2408ca94c3ddSJeremy L Thompson   @param[out] mult             Vector to store multiplicity (of size `l_size` )
240975f0d5a4SJeremy L Thompson 
241075f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
241175f0d5a4SJeremy L Thompson 
241275f0d5a4SJeremy L Thompson   @ref User
241375f0d5a4SJeremy L Thompson **/
241475f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) {
241575f0d5a4SJeremy L Thompson   Ceed                ceed;
2416b275c451SJeremy L Thompson   CeedInt             num_suboperators;
241775f0d5a4SJeremy L Thompson   CeedSize            l_vec_len;
241875f0d5a4SJeremy L Thompson   CeedScalar         *mult_array;
241975f0d5a4SJeremy L Thompson   CeedVector          ones_l_vec;
24207c1dbaffSSebastian Grimberg   CeedElemRestriction elem_rstr, mult_elem_rstr;
2421b275c451SJeremy L Thompson   CeedOperator       *sub_operators;
242275f0d5a4SJeremy L Thompson 
24231c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
24241c66c397SJeremy L Thompson 
242575f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
242675f0d5a4SJeremy L Thompson 
242775f0d5a4SJeremy L Thompson   // Zero mult vector
242875f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
242975f0d5a4SJeremy L Thompson 
243075f0d5a4SJeremy L Thompson   // Get suboperators
2431b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2432b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2433b275c451SJeremy L Thompson   if (num_suboperators == 0) return CEED_ERROR_SUCCESS;
243475f0d5a4SJeremy L Thompson 
243575f0d5a4SJeremy L Thompson   // Work vector
243675f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetLength(mult, &l_vec_len));
243775f0d5a4SJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec));
243875f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(ones_l_vec, 1.0));
243975f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array));
244075f0d5a4SJeremy L Thompson 
244175f0d5a4SJeremy L Thompson   // Compute multiplicity across suboperators
2442b275c451SJeremy L Thompson   for (CeedInt i = 0; i < num_suboperators; i++) {
244375f0d5a4SJeremy L Thompson     const CeedScalar *sub_mult_array;
244475f0d5a4SJeremy L Thompson     CeedVector        sub_mult_l_vec, ones_e_vec;
244575f0d5a4SJeremy L Thompson 
244675f0d5a4SJeremy L Thompson     // -- Check for suboperator to skip
244775f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < num_skip_indices; j++) {
244875f0d5a4SJeremy L Thompson       if (skip_indices[j] == i) continue;
244975f0d5a4SJeremy L Thompson     }
245075f0d5a4SJeremy L Thompson 
245175f0d5a4SJeremy L Thompson     // -- Sub operator multiplicity
2452437c7c90SJeremy L Thompson     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr));
24537c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr, &mult_elem_rstr));
24547c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateVector(mult_elem_rstr, &sub_mult_l_vec, &ones_e_vec));
245575f0d5a4SJeremy L Thompson     CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0));
24567c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE));
24577c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE));
245875f0d5a4SJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array));
245975f0d5a4SJeremy L Thompson     // ---- Flag every node present in the current suboperator
246075f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < l_vec_len; j++) {
246175f0d5a4SJeremy L Thompson       if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0;
246275f0d5a4SJeremy L Thompson     }
246375f0d5a4SJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array));
246475f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&sub_mult_l_vec));
246575f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&ones_e_vec));
24667c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&mult_elem_rstr));
246775f0d5a4SJeremy L Thompson   }
246875f0d5a4SJeremy L Thompson   CeedCall(CeedVectorRestoreArray(mult, &mult_array));
2469811d0ccfSJeremy L Thompson   CeedCall(CeedVectorDestroy(&ones_l_vec));
247075f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
247175f0d5a4SJeremy L Thompson }
247275f0d5a4SJeremy L Thompson 
247375f0d5a4SJeremy L Thompson /**
2474ca94c3ddSJeremy 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.
2475eaf62fffSJeremy L Thompson 
2476ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2477f04ea552SJeremy L Thompson 
2478ca94c3ddSJeremy L Thompson   @param[in]  op_fine      Fine grid `CeedOperator`
2479ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2480ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid `CeedElemRestriction`
2481ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector `CeedBasis`
2482ca94c3ddSJeremy L Thompson   @param[out] op_coarse    Coarse grid `CeedOperator`
2483ca94c3ddSJeremy L Thompson   @param[out] op_prolong   Coarse to fine `CeedOperator`, or `NULL`
2484ca94c3ddSJeremy L Thompson   @param[out] op_restrict  Fine to coarse `CeedOperator`, or `NULL`
2485eaf62fffSJeremy L Thompson 
2486eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2487eaf62fffSJeremy L Thompson 
2488eaf62fffSJeremy L Thompson   @ref User
2489eaf62fffSJeremy L Thompson **/
24902b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
24917758292fSSebastian Grimberg                                      CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
24921c66c397SJeremy L Thompson   CeedBasis basis_c_to_f = NULL;
24931c66c397SJeremy L Thompson 
24942b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2495eaf62fffSJeremy L Thompson 
249683d6adf3SZach Atkins   // Build prolongation matrix, if required
24977758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
249883d6adf3SZach Atkins     CeedBasis basis_fine;
24991c66c397SJeremy L Thompson 
25002b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
25012b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f));
250283d6adf3SZach Atkins   }
2503eaf62fffSJeremy L Thompson 
2504f113e5dcSJeremy L Thompson   // Core code
25057758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2506eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2507eaf62fffSJeremy L Thompson }
2508eaf62fffSJeremy L Thompson 
2509eaf62fffSJeremy L Thompson /**
2510ca94c3ddSJeremy L Thompson   @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a tensor basis for the active basis.
2511eaf62fffSJeremy L Thompson 
2512ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2513f04ea552SJeremy L Thompson 
2514ca94c3ddSJeremy L Thompson   @param[in]  op_fine       Fine grid `CeedOperator`
2515ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2516ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid `CeedElemRestriction`
2517ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector `CeedBasis`
2518ca94c3ddSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator`
2519ca94c3ddSJeremy L Thompson   @param[out] op_coarse     Coarse grid `CeedOperator`
2520ca94c3ddSJeremy L Thompson   @param[out] op_prolong    Coarse to fine `CeedOperator`, or `NULL`
2521ca94c3ddSJeremy L Thompson   @param[out] op_restrict   Fine to coarse `CeedOperator`, or `NULL`
2522eaf62fffSJeremy L Thompson 
2523eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2524eaf62fffSJeremy L Thompson 
2525eaf62fffSJeremy L Thompson   @ref User
2526eaf62fffSJeremy L Thompson **/
25272b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
25282b730f8bSJeremy L Thompson                                              const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
25297758292fSSebastian Grimberg                                              CeedOperator *op_restrict) {
2530eaf62fffSJeremy L Thompson   Ceed      ceed;
25311c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
25321c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
25331c66c397SJeremy L Thompson 
25341c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
25352b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2536eaf62fffSJeremy L Thompson 
2537eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
25382b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
25392b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
25402b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
25416574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2542eaf62fffSJeremy L Thompson 
254383d6adf3SZach Atkins   // Create coarse to fine basis, if required
25447758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
25451c66c397SJeremy L Thompson     CeedInt     dim, num_comp, num_nodes_c, P_1d_f, P_1d_c;
25461c66c397SJeremy L Thompson     CeedScalar *q_ref, *q_weight, *grad;
25471c66c397SJeremy L Thompson 
254883d6adf3SZach Atkins     // Check if interpolation matrix is provided
25496574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
25506574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
25512b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
25522b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
25532b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f));
25542b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
25552b730f8bSJeremy L Thompson     P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c);
25562b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_ref));
25572b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_weight));
25582b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad));
25592b730f8bSJeremy 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));
25602b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
25612b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
25622b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
256383d6adf3SZach Atkins   }
2564eaf62fffSJeremy L Thompson 
2565eaf62fffSJeremy L Thompson   // Core code
25667758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2567eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2568eaf62fffSJeremy L Thompson }
2569eaf62fffSJeremy L Thompson 
2570eaf62fffSJeremy L Thompson /**
2571ca94c3ddSJeremy L Thompson   @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a non-tensor basis for the active vector
2572eaf62fffSJeremy L Thompson 
2573ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2574f04ea552SJeremy L Thompson 
2575ca94c3ddSJeremy L Thompson   @param[in]  op_fine       Fine grid `CeedOperator`
2576ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2577ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid `CeedElemRestriction`
2578ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector `CeedBasis`
2579ca94c3ddSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator`
2580ca94c3ddSJeremy L Thompson   @param[out] op_coarse     Coarse grid `CeedOperator`
2581ca94c3ddSJeremy L Thompson   @param[out] op_prolong    Coarse to fine `CeedOperator`, or `NULL`
2582ca94c3ddSJeremy L Thompson   @param[out] op_restrict   Fine to coarse `CeedOperator`, or `NULL`
2583eaf62fffSJeremy L Thompson 
2584eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2585eaf62fffSJeremy L Thompson 
2586eaf62fffSJeremy L Thompson   @ref User
2587eaf62fffSJeremy L Thompson **/
25882b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
25897758292fSSebastian Grimberg                                        const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
25907758292fSSebastian Grimberg                                        CeedOperator *op_restrict) {
2591eaf62fffSJeremy L Thompson   Ceed      ceed;
25921c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
25931c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
25941c66c397SJeremy L Thompson 
25951c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
25962b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2597eaf62fffSJeremy L Thompson 
2598eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
25992b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
26002b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
26012b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
26026574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2603eaf62fffSJeremy L Thompson 
2604eaf62fffSJeremy L Thompson   // Coarse to fine basis
26057758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
26061c66c397SJeremy L Thompson     CeedInt          dim, num_comp, num_nodes_c, num_nodes_f;
26071c66c397SJeremy L Thompson     CeedScalar      *q_ref, *q_weight, *grad;
26081c66c397SJeremy L Thompson     CeedElemTopology topo;
26091c66c397SJeremy L Thompson 
261083d6adf3SZach Atkins     // Check if interpolation matrix is provided
26116574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
26126574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
26132b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_fine, &topo));
26142b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
26152b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
26162b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f));
26172b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
26182b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref));
26192b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f, &q_weight));
26202b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad));
26212b730f8bSJeremy 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));
26222b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
26232b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
26242b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
262583d6adf3SZach Atkins   }
2626eaf62fffSJeremy L Thompson 
2627eaf62fffSJeremy L Thompson   // Core code
26287758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2629eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2630eaf62fffSJeremy L Thompson }
2631eaf62fffSJeremy L Thompson 
2632eaf62fffSJeremy L Thompson /**
2633ca94c3ddSJeremy L Thompson   @brief Build a FDM based approximate inverse for each element for a `CeedOperator`.
2634eaf62fffSJeremy L Thompson 
2635ca94c3ddSJeremy L Thompson   This returns a `CeedOperator` and `CeedVector` to apply a Fast Diagonalization Method based approximate inverse.
2636859c15bbSJames 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$.
2637ca94c3ddSJeremy 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$.
2638ca94c3ddSJeremy L Thompson   The `CeedOperator` must be linear and non-composite.
2639ca94c3ddSJeremy L Thompson   The associated `CeedQFunction` must therefore also be linear.
2640eaf62fffSJeremy L Thompson 
2641ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2642f04ea552SJeremy L Thompson 
2643ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to create element inverses
2644ca94c3ddSJeremy L Thompson   @param[out] fdm_inv `CeedOperator` to apply the action of a FDM based inverse for each element
2645ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2646eaf62fffSJeremy L Thompson 
2647eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2648eaf62fffSJeremy L Thompson 
2649480fae85SJeremy L Thompson   @ref User
2650eaf62fffSJeremy L Thompson **/
26512b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) {
26521c66c397SJeremy L Thompson   Ceed                 ceed, ceed_parent;
26531c66c397SJeremy L Thompson   bool                 interp = false, grad = false, is_tensor_basis = true;
26541c66c397SJeremy L Thompson   CeedInt              num_input_fields, P_1d, Q_1d, num_nodes, num_qpts, dim, num_comp = 1, num_elem = 1;
26551c66c397SJeremy L Thompson   CeedSize             l_size = 1;
26561c66c397SJeremy L Thompson   CeedScalar          *mass, *laplace, *x, *fdm_interp, *lambda, *elem_avg;
26571c66c397SJeremy L Thompson   const CeedScalar    *interp_1d, *grad_1d, *q_weight_1d;
26581c66c397SJeremy L Thompson   CeedVector           q_data;
26591c66c397SJeremy L Thompson   CeedElemRestriction  rstr  = NULL, rstr_qd_i;
26601c66c397SJeremy L Thompson   CeedBasis            basis = NULL, fdm_basis;
26611c66c397SJeremy L Thompson   CeedQFunctionContext ctx_fdm;
26621c66c397SJeremy L Thompson   CeedQFunctionField  *qf_fields;
26631c66c397SJeremy L Thompson   CeedQFunction        qf, qf_fdm;
26641c66c397SJeremy L Thompson   CeedOperatorField   *op_fields;
26651c66c397SJeremy L Thompson 
26662b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2667eaf62fffSJeremy L Thompson 
2668eaf62fffSJeremy L Thompson   if (op->CreateFDMElementInverse) {
2669d04bbc78SJeremy L Thompson     // Backend version
26702b730f8bSJeremy L Thompson     CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request));
2671eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2672eaf62fffSJeremy L Thompson   } else {
2673d04bbc78SJeremy L Thompson     // Operator fallback
2674d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2675d04bbc78SJeremy L Thompson 
26762b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2677d04bbc78SJeremy L Thompson     if (op_fallback) {
26782b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request));
2679eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2680eaf62fffSJeremy L Thompson     }
2681eaf62fffSJeremy L Thompson   }
2682eaf62fffSJeremy L Thompson 
2683d04bbc78SJeremy L Thompson   // Default interface implementation
26842b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
2685bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
26862b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
2687eaf62fffSJeremy L Thompson 
2688eaf62fffSJeremy L Thompson   // Determine active input basis
26892b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL));
26902b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
2691eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
2692eaf62fffSJeremy L Thompson     CeedVector vec;
26931c66c397SJeremy L Thompson 
26942b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
2695eaf62fffSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2696eaf62fffSJeremy L Thompson       CeedEvalMode eval_mode;
26971c66c397SJeremy L Thompson 
26982b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
2699eaf62fffSJeremy L Thompson       interp = interp || eval_mode == CEED_EVAL_INTERP;
2700eaf62fffSJeremy L Thompson       grad   = grad || eval_mode == CEED_EVAL_GRAD;
27012b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis));
27022b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
2703eaf62fffSJeremy L Thompson     }
2704eaf62fffSJeremy L Thompson   }
27056574a04fSJeremy L Thompson   CeedCheck(basis, ceed, CEED_ERROR_BACKEND, "No active field set");
27062b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
2707352a5e7cSSebastian Grimberg   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
27082b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
27092b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
27102b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
27112b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
27122b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
27132b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
2714eaf62fffSJeremy L Thompson 
2715eaf62fffSJeremy L Thompson   // Build and diagonalize 1D Mass and Laplacian
27166574a04fSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
27176574a04fSJeremy L Thompson   CeedCheck(is_tensor_basis, ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases");
27182b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &mass));
27192b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &laplace));
27202b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &x));
27212b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp));
27222b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &lambda));
2723eaf62fffSJeremy L Thompson   // -- Build matrices
27242b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
27252b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
27262b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
27272b730f8bSJeremy L Thompson   CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace));
2728eaf62fffSJeremy L Thompson 
2729eaf62fffSJeremy L Thompson   // -- Diagonalize
27302b730f8bSJeremy L Thompson   CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d));
27312b730f8bSJeremy L Thompson   CeedCall(CeedFree(&mass));
27322b730f8bSJeremy L Thompson   CeedCall(CeedFree(&laplace));
27332b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
27342b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d];
27352b730f8bSJeremy L Thompson   }
27362b730f8bSJeremy L Thompson   CeedCall(CeedFree(&x));
2737eaf62fffSJeremy L Thompson 
27381c66c397SJeremy L Thompson   {
27391c66c397SJeremy L Thompson     CeedInt             layout[3], num_modes = (interp ? 1 : 0) + (grad ? dim : 0);
27401c66c397SJeremy L Thompson     CeedScalar          max_norm = 0;
27411c66c397SJeremy L Thompson     const CeedScalar   *assembled_array, *q_weight_array;
27421c66c397SJeremy L Thompson     CeedVector          assembled = NULL, q_weight;
2743c5f45aeaSJeremy L Thompson     CeedElemRestriction rstr_qf   = NULL;
27441c66c397SJeremy L Thompson 
27451c66c397SJeremy L Thompson     // Assemble QFunction
27462b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request));
274756c48462SJeremy L Thompson     CeedCall(CeedElemRestrictionGetELayout(rstr_qf, layout));
27482b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr_qf));
27492b730f8bSJeremy L Thompson     CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm));
2750eaf62fffSJeremy L Thompson 
2751eaf62fffSJeremy L Thompson     // Calculate element averages
27522b730f8bSJeremy L Thompson     CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight));
27532b730f8bSJeremy L Thompson     CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight));
27542b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array));
27552b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array));
27562b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_elem, &elem_avg));
2757eaf62fffSJeremy L Thompson     const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON;
27581c66c397SJeremy L Thompson 
2759eaf62fffSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
2760eaf62fffSJeremy L Thompson       CeedInt count = 0;
27611c66c397SJeremy L Thompson 
27622b730f8bSJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
27632b730f8bSJeremy L Thompson         for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) {
27642b730f8bSJeremy L Thompson           if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) {
27652b730f8bSJeremy L Thompson             elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q];
2766eaf62fffSJeremy L Thompson             count++;
2767eaf62fffSJeremy L Thompson           }
27682b730f8bSJeremy L Thompson         }
27692b730f8bSJeremy L Thompson       }
2770eaf62fffSJeremy L Thompson       if (count) {
2771eaf62fffSJeremy L Thompson         elem_avg[e] /= count;
2772eaf62fffSJeremy L Thompson       } else {
2773eaf62fffSJeremy L Thompson         elem_avg[e] = 1.0;
2774eaf62fffSJeremy L Thompson       }
2775eaf62fffSJeremy L Thompson     }
27762b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array));
27772b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled));
27782b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array));
27792b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&q_weight));
27801c66c397SJeremy L Thompson   }
2781eaf62fffSJeremy L Thompson 
2782eaf62fffSJeremy L Thompson   // Build FDM diagonal
27831c66c397SJeremy L Thompson   {
2784eaf62fffSJeremy L Thompson     CeedScalar *q_data_array, *fdm_diagonal;
27851c66c397SJeremy L Thompson 
2786352a5e7cSSebastian Grimberg     CeedCall(CeedCalloc(num_comp * num_nodes, &fdm_diagonal));
2787352a5e7cSSebastian Grimberg     const CeedScalar fdm_diagonal_bound = num_nodes * CEED_EPSILON;
27882b730f8bSJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
2789352a5e7cSSebastian Grimberg       for (CeedInt n = 0; n < num_nodes; n++) {
2790352a5e7cSSebastian Grimberg         if (interp) fdm_diagonal[c * num_nodes + n] = 1.0;
27912b730f8bSJeremy L Thompson         if (grad) {
2792eaf62fffSJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
2793eaf62fffSJeremy L Thompson             CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d;
2794352a5e7cSSebastian Grimberg             fdm_diagonal[c * num_nodes + n] += lambda[i];
2795eaf62fffSJeremy L Thompson           }
2796eaf62fffSJeremy L Thompson         }
2797352a5e7cSSebastian Grimberg         if (fabs(fdm_diagonal[c * num_nodes + n]) < fdm_diagonal_bound) fdm_diagonal[c * num_nodes + n] = fdm_diagonal_bound;
27982b730f8bSJeremy L Thompson       }
27992b730f8bSJeremy L Thompson     }
2800352a5e7cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * num_nodes, &q_data));
28012b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(q_data, 0.0));
28022b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array));
28032b730f8bSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
28042b730f8bSJeremy L Thompson       for (CeedInt c = 0; c < num_comp; c++) {
28051c66c397SJeremy L Thompson         for (CeedInt n = 0; n < num_nodes; n++)
28061c66c397SJeremy L Thompson           q_data_array[(e * num_comp + c) * num_nodes + n] = 1. / (elem_avg[e] * fdm_diagonal[c * num_nodes + n]);
28072b730f8bSJeremy L Thompson       }
28082b730f8bSJeremy L Thompson     }
28092b730f8bSJeremy L Thompson     CeedCall(CeedFree(&elem_avg));
28102b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_diagonal));
28112b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(q_data, &q_data_array));
28121c66c397SJeremy L Thompson   }
2813eaf62fffSJeremy L Thompson 
2814eaf62fffSJeremy L Thompson   // Setup FDM operator
2815eaf62fffSJeremy L Thompson   // -- Basis
28161c66c397SJeremy L Thompson   {
2817eaf62fffSJeremy L Thompson     CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy;
28181c66c397SJeremy L Thompson 
28192b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy));
28202b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_ref_dummy));
28212b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_weight_dummy));
28222b730f8bSJeremy 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));
28232b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_interp));
28242b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad_dummy));
28252b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref_dummy));
28262b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight_dummy));
28272b730f8bSJeremy L Thompson     CeedCall(CeedFree(&lambda));
28281c66c397SJeremy L Thompson   }
2829eaf62fffSJeremy L Thompson 
2830eaf62fffSJeremy L Thompson   // -- Restriction
28311c66c397SJeremy L Thompson   {
2832352a5e7cSSebastian Grimberg     CeedInt strides[3] = {1, num_nodes, num_nodes * num_comp};
2833352a5e7cSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, num_nodes, num_comp, num_elem * num_comp * num_nodes, strides, &rstr_qd_i));
28341c66c397SJeremy L Thompson   }
28351c66c397SJeremy L Thompson 
2836eaf62fffSJeremy L Thompson   // -- QFunction
28372b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm));
28382b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP));
28392b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE));
28402b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP));
28412b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp));
28421c66c397SJeremy L Thompson 
2843eaf62fffSJeremy L Thompson   // -- QFunction context
28441c66c397SJeremy L Thompson   {
2845eaf62fffSJeremy L Thompson     CeedInt *num_comp_data;
28461c66c397SJeremy L Thompson 
28472b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_data));
2848eaf62fffSJeremy L Thompson     num_comp_data[0] = num_comp;
28492b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm));
28502b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data));
28511c66c397SJeremy L Thompson   }
28522b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm));
28532b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_fdm));
28541c66c397SJeremy L Thompson 
2855eaf62fffSJeremy L Thompson   // -- Operator
28562b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv));
28572b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2858356036faSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_NONE, q_data));
28592b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2860eaf62fffSJeremy L Thompson 
2861eaf62fffSJeremy L Thompson   // Cleanup
28622b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_data));
28632b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&fdm_basis));
28642b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i));
28652b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_fdm));
2866eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2867eaf62fffSJeremy L Thompson }
2868eaf62fffSJeremy L Thompson 
2869eaf62fffSJeremy L Thompson /// @}
2870