xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-preconditioning.c (revision 4a9a33d7c3c276ab921efc512a0fdfc7c3da9369)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3eaf62fffSJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5eaf62fffSJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7eaf62fffSJeremy L Thompson 
82b730f8bSJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
11c85e8640SSebastian Grimberg #include <assert.h>
122b730f8bSJeremy L Thompson #include <math.h>
13eaf62fffSJeremy L Thompson #include <stdbool.h>
14eaf62fffSJeremy L Thompson #include <stdio.h>
15eaf62fffSJeremy L Thompson #include <string.h>
16eaf62fffSJeremy L Thompson 
17eaf62fffSJeremy L Thompson /// @file
18eaf62fffSJeremy L Thompson /// Implementation of CeedOperator preconditioning interfaces
19eaf62fffSJeremy L Thompson 
20eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
21eaf62fffSJeremy L Thompson /// CeedOperator Library Internal Preconditioning Functions
22eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
23eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorDeveloper
24eaf62fffSJeremy L Thompson /// @{
25eaf62fffSJeremy L Thompson 
26eaf62fffSJeremy L Thompson /**
27ea61e9acSJeremy L Thompson   @brief Duplicate a CeedQFunction with a reference Ceed to fallback for advanced CeedOperator functionality
289e77b9c8SJeremy L Thompson 
2901ea9c81SJed Brown   @param[in]  fallback_ceed Ceed on which to create fallback CeedQFunction
309e77b9c8SJeremy L Thompson   @param[in]  qf            CeedQFunction to create fallback for
3101ea9c81SJed Brown   @param[out] qf_fallback   fallback CeedQFunction
329e77b9c8SJeremy L Thompson 
339e77b9c8SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
349e77b9c8SJeremy L Thompson 
359e77b9c8SJeremy L Thompson   @ref Developer
369e77b9c8SJeremy L Thompson **/
372b730f8bSJeremy L Thompson static int CeedQFunctionCreateFallback(Ceed fallback_ceed, CeedQFunction qf, CeedQFunction *qf_fallback) {
381c66c397SJeremy L Thompson   char *source_path_with_name = NULL;
391c66c397SJeremy L Thompson 
409e77b9c8SJeremy L Thompson   // Check if NULL qf passed in
419e77b9c8SJeremy L Thompson   if (!qf) return CEED_ERROR_SUCCESS;
429e77b9c8SJeremy L Thompson 
43d04bbc78SJeremy L Thompson   CeedDebug256(qf->ceed, 1, "---------- CeedOperator Fallback ----------\n");
4413f886e9SJeremy L Thompson   CeedDebug(qf->ceed, "Creating fallback CeedQFunction\n");
45d04bbc78SJeremy L Thompson 
469e77b9c8SJeremy L Thompson   if (qf->source_path) {
472b730f8bSJeremy L Thompson     size_t path_len = strlen(qf->source_path), name_len = strlen(qf->kernel_name);
482b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(path_len + name_len + 2, &source_path_with_name));
499e77b9c8SJeremy L Thompson     memcpy(source_path_with_name, qf->source_path, path_len);
509e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len], ":", 1);
519e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len + 1], qf->kernel_name, name_len);
529e77b9c8SJeremy L Thompson   } else {
532b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &source_path_with_name));
549e77b9c8SJeremy L Thompson   }
559e77b9c8SJeremy L Thompson 
562b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInterior(fallback_ceed, qf->vec_length, qf->function, source_path_with_name, qf_fallback));
579e77b9c8SJeremy L Thompson   {
589e77b9c8SJeremy L Thompson     CeedQFunctionContext ctx;
599e77b9c8SJeremy L Thompson 
602b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
612b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx));
629e77b9c8SJeremy L Thompson   }
639e77b9c8SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_input_fields; i++) {
642b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(*qf_fallback, qf->input_fields[i]->field_name, qf->input_fields[i]->size, qf->input_fields[i]->eval_mode));
659e77b9c8SJeremy L Thompson   }
669e77b9c8SJeremy L Thompson   for (CeedInt i = 0; i < qf->num_output_fields; i++) {
672b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(*qf_fallback, qf->output_fields[i]->field_name, qf->output_fields[i]->size, qf->output_fields[i]->eval_mode));
689e77b9c8SJeremy L Thompson   }
692b730f8bSJeremy L Thompson   CeedCall(CeedFree(&source_path_with_name));
709e77b9c8SJeremy L Thompson   return CEED_ERROR_SUCCESS;
719e77b9c8SJeremy L Thompson }
729e77b9c8SJeremy L Thompson 
739e77b9c8SJeremy L Thompson /**
74ea61e9acSJeremy L Thompson   @brief Duplicate a CeedOperator with a reference Ceed to fallback for advanced CeedOperator functionality
75eaf62fffSJeremy L Thompson 
76ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to create fallback for
77eaf62fffSJeremy L Thompson 
78eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
79eaf62fffSJeremy L Thompson 
80eaf62fffSJeremy L Thompson   @ref Developer
81eaf62fffSJeremy L Thompson **/
82d04bbc78SJeremy L Thompson static int CeedOperatorCreateFallback(CeedOperator op) {
839e77b9c8SJeremy L Thompson   Ceed         ceed_fallback;
841c66c397SJeremy L Thompson   bool         is_composite;
851c66c397SJeremy L Thompson   CeedOperator op_fallback;
86eaf62fffSJeremy L Thompson 
87805fe78eSJeremy L Thompson   // Check not already created
88805fe78eSJeremy L Thompson   if (op->op_fallback) return CEED_ERROR_SUCCESS;
89805fe78eSJeremy L Thompson 
90eaf62fffSJeremy L Thompson   // Fallback Ceed
912b730f8bSJeremy L Thompson   CeedCall(CeedGetOperatorFallbackCeed(op->ceed, &ceed_fallback));
92d04bbc78SJeremy L Thompson   if (!ceed_fallback) return CEED_ERROR_SUCCESS;
93d04bbc78SJeremy L Thompson 
94d04bbc78SJeremy L Thompson   CeedDebug256(op->ceed, 1, "---------- CeedOperator Fallback ----------\n");
9513f886e9SJeremy L Thompson   CeedDebug(op->ceed, "Creating fallback CeedOperator\n");
96eaf62fffSJeremy L Thompson 
97eaf62fffSJeremy L Thompson   // Clone Op
98b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
99b275c451SJeremy L Thompson   if (is_composite) {
100b275c451SJeremy L Thompson     CeedInt       num_suboperators;
101b275c451SJeremy L Thompson     CeedOperator *sub_operators;
102b275c451SJeremy L Thompson 
1032b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorCreate(ceed_fallback, &op_fallback));
104b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
105b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
106b275c451SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
107d04bbc78SJeremy L Thompson       CeedOperator op_sub_fallback;
108d04bbc78SJeremy L Thompson 
109b275c451SJeremy L Thompson       CeedCall(CeedOperatorGetFallback(sub_operators[i], &op_sub_fallback));
1102b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorAddSub(op_fallback, op_sub_fallback));
111805fe78eSJeremy L Thompson     }
112805fe78eSJeremy L Thompson   } else {
1139e77b9c8SJeremy L Thompson     CeedQFunction qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL;
1141c66c397SJeremy L Thompson 
1152b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback));
1162b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback));
1172b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback));
1182b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback));
119805fe78eSJeremy L Thompson     for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
120437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, op->input_fields[i]->field_name, op->input_fields[i]->elem_rstr, op->input_fields[i]->basis,
1212b730f8bSJeremy L Thompson                                     op->input_fields[i]->vec));
122805fe78eSJeremy L Thompson     }
123805fe78eSJeremy L Thompson     for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
124437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, op->output_fields[i]->field_name, op->output_fields[i]->elem_rstr, op->output_fields[i]->basis,
1252b730f8bSJeremy L Thompson                                     op->output_fields[i]->vec));
126805fe78eSJeremy L Thompson     }
1272b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op->qf_assembled, &op_fallback->qf_assembled));
1289e77b9c8SJeremy L Thompson     // Cleanup
1292b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf_fallback));
1302b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqf_fallback));
1312b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqfT_fallback));
132805fe78eSJeremy L Thompson   }
1332b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetName(op_fallback, op->name));
1342b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fallback));
135b05f7e9fSJeremy L Thompson   // Note: No ref-counting here so we don't get caught in a reference loop.
136b05f7e9fSJeremy L Thompson   //       The op holds the only reference to op_fallback and is responsible for deleting itself and op_fallback.
137805fe78eSJeremy L Thompson   op->op_fallback                 = op_fallback;
138b05f7e9fSJeremy L Thompson   op_fallback->op_fallback_parent = op;
139eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
140eaf62fffSJeremy L Thompson }
141eaf62fffSJeremy L Thompson 
142eaf62fffSJeremy L Thompson /**
143eaf62fffSJeremy L Thompson   @brief Select correct basis matrix pointer based on CeedEvalMode
144eaf62fffSJeremy L Thompson 
145352a5e7cSSebastian Grimberg   @param[in]  basis     CeedBasis from which to get the basis matrix
146eaf62fffSJeremy L Thompson   @param[in]  eval_mode Current basis evaluation mode
147eaf62fffSJeremy L Thompson   @param[in]  identity  Pointer to identity matrix
148eaf62fffSJeremy L Thompson   @param[out] basis_ptr Basis pointer to set
149eaf62fffSJeremy L Thompson 
150eaf62fffSJeremy L Thompson   @ref Developer
151eaf62fffSJeremy L Thompson **/
152352a5e7cSSebastian Grimberg static inline int CeedOperatorGetBasisPointer(CeedBasis basis, CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar **basis_ptr) {
153eaf62fffSJeremy L Thompson   switch (eval_mode) {
154eaf62fffSJeremy L Thompson     case CEED_EVAL_NONE:
155eaf62fffSJeremy L Thompson       *basis_ptr = identity;
156eaf62fffSJeremy L Thompson       break;
157eaf62fffSJeremy L Thompson     case CEED_EVAL_INTERP:
158352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetInterp(basis, basis_ptr));
159eaf62fffSJeremy L Thompson       break;
160eaf62fffSJeremy L Thompson     case CEED_EVAL_GRAD:
161352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetGrad(basis, basis_ptr));
162352a5e7cSSebastian Grimberg       break;
163352a5e7cSSebastian Grimberg     case CEED_EVAL_DIV:
164352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetDiv(basis, basis_ptr));
165352a5e7cSSebastian Grimberg       break;
166352a5e7cSSebastian Grimberg     case CEED_EVAL_CURL:
167352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetCurl(basis, basis_ptr));
168eaf62fffSJeremy L Thompson       break;
169eaf62fffSJeremy L Thompson     case CEED_EVAL_WEIGHT:
170eaf62fffSJeremy L Thompson       break;  // Caught by QF Assembly
171eaf62fffSJeremy L Thompson   }
172ed9e99e6SJeremy L Thompson   assert(*basis_ptr != NULL);
173352a5e7cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
174eaf62fffSJeremy L Thompson }
175eaf62fffSJeremy L Thompson 
176eaf62fffSJeremy L Thompson /**
177eaf62fffSJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
178eaf62fffSJeremy L Thompson 
179eaf62fffSJeremy L Thompson   @param[in]  op             CeedOperator to assemble point block diagonal
180ea61e9acSJeremy L Thompson   @param[in]  request        Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE
181bd83916cSSebastian Grimberg   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
182eaf62fffSJeremy L Thompson   @param[out] assembled      CeedVector to store assembled diagonal
183eaf62fffSJeremy L Thompson 
184eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
185eaf62fffSJeremy L Thompson 
186eaf62fffSJeremy L Thompson   @ref Developer
187eaf62fffSJeremy L Thompson **/
188bd83916cSSebastian Grimberg static inline int CeedSingleOperatorAssembleAddDiagonal_Core(CeedOperator op, CeedRequest *request, const bool is_point_block, CeedVector assembled) {
189eaf62fffSJeremy L Thompson   Ceed ceed;
190506b1a0cSSebastian Grimberg   bool is_composite;
191506b1a0cSSebastian Grimberg 
192506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetCeed(op, &ceed));
193506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorIsComposite(op, &is_composite));
194506b1a0cSSebastian Grimberg   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
195506b1a0cSSebastian Grimberg 
196506b1a0cSSebastian Grimberg   // Assemble QFunction
197506b1a0cSSebastian Grimberg   CeedInt             layout_qf[3];
198437c7c90SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
199c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
200c5f45aeaSJeremy L Thompson   CeedElemRestriction assembled_elem_rstr = NULL;
201437c7c90SJeremy L Thompson 
202437c7c90SJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request));
203506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, &layout_qf));
204437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
205437c7c90SJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
206eaf62fffSJeremy L Thompson 
207ed9e99e6SJeremy L Thompson   // Get assembly data
208437c7c90SJeremy L Thompson   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
209506b1a0cSSebastian Grimberg   CeedInt                  num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out;
210437c7c90SJeremy L Thompson   CeedSize               **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components;
211506b1a0cSSebastian Grimberg   CeedBasis               *active_bases_in, *active_bases_out;
212506b1a0cSSebastian Grimberg   CeedElemRestriction     *active_elem_rstrs_in, *active_elem_rstrs_out;
2131c66c397SJeremy L Thompson   CeedOperatorAssemblyData data;
2141c66c397SJeremy L Thompson 
215437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
216506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in,
217506b1a0cSSebastian Grimberg                                                 &num_active_bases_out, &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out,
218506b1a0cSSebastian Grimberg                                                 &num_output_components));
219506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, NULL, NULL, &active_bases_out, NULL));
220506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs_in, NULL, &active_elem_rstrs_out));
221506b1a0cSSebastian Grimberg 
222934a29f5SSebastian Grimberg   // Loop over all active bases (find matching input/output pairs)
223934a29f5SSebastian Grimberg   for (CeedInt b = 0; b < CeedIntMin(num_active_bases_in, num_active_bases_out); b++) {
224934a29f5SSebastian Grimberg     CeedInt             b_in, b_out, num_elem, num_nodes, num_qpts, num_comp;
2251c66c397SJeremy L Thompson     bool                has_eval_none = false;
2261c66c397SJeremy L Thompson     CeedScalar         *elem_diag_array, *identity = NULL;
2271c66c397SJeremy L Thompson     CeedVector          elem_diag;
2287c1dbaffSSebastian Grimberg     CeedElemRestriction diag_elem_rstr;
2291c66c397SJeremy L Thompson 
230934a29f5SSebastian Grimberg     if (num_active_bases_in <= num_active_bases_out) {
231934a29f5SSebastian Grimberg       b_in = b;
232934a29f5SSebastian Grimberg       for (b_out = 0; b_out < num_active_bases_out; b_out++) {
233934a29f5SSebastian Grimberg         if (active_bases_in[b_in] == active_bases_out[b_out]) {
234934a29f5SSebastian Grimberg           break;
235934a29f5SSebastian Grimberg         }
236934a29f5SSebastian Grimberg       }
237934a29f5SSebastian Grimberg       if (b_out == num_active_bases_out) {
238934a29f5SSebastian Grimberg         continue;
239934a29f5SSebastian Grimberg       }  // No matching output basis found
240934a29f5SSebastian Grimberg     } else {
241934a29f5SSebastian Grimberg       b_out = b;
242934a29f5SSebastian Grimberg       for (b_in = 0; b_in < num_active_bases_in; b_in++) {
243934a29f5SSebastian Grimberg         if (active_bases_in[b_in] == active_bases_out[b_out]) {
244934a29f5SSebastian Grimberg           break;
245934a29f5SSebastian Grimberg         }
246934a29f5SSebastian Grimberg       }
247934a29f5SSebastian Grimberg       if (b_in == num_active_bases_in) {
248934a29f5SSebastian Grimberg         continue;
249934a29f5SSebastian Grimberg       }  // No matching output basis found
250934a29f5SSebastian Grimberg     }
251934a29f5SSebastian Grimberg     CeedCheck(active_elem_rstrs_in[b_in] == active_elem_rstrs_out[b_out], ceed, CEED_ERROR_UNSUPPORTED,
252506b1a0cSSebastian Grimberg               "Cannot assemble operator diagonal with different input and output active element restrictions");
253506b1a0cSSebastian Grimberg 
2541c66c397SJeremy L Thompson     // Assemble point block diagonal restriction, if needed
255bd83916cSSebastian Grimberg     if (is_point_block) {
256934a29f5SSebastian Grimberg       CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstrs_in[b_in], &diag_elem_rstr));
2577c1dbaffSSebastian Grimberg     } else {
258934a29f5SSebastian Grimberg       CeedCall(CeedElemRestrictionCreateUnsignedCopy(active_elem_rstrs_in[b_in], &diag_elem_rstr));
259eaf62fffSJeremy L Thompson     }
260eaf62fffSJeremy L Thompson 
261eaf62fffSJeremy L Thompson     // Create diagonal vector
262437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag));
263eaf62fffSJeremy L Thompson 
264eaf62fffSJeremy L Thompson     // Assemble element operator diagonals
2652b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(elem_diag, 0.0));
2662b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array));
267437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem));
268934a29f5SSebastian Grimberg     CeedCall(CeedBasisGetNumNodes(active_bases_in[b_in], &num_nodes));
269934a29f5SSebastian Grimberg     CeedCall(CeedBasisGetNumComponents(active_bases_in[b_in], &num_comp));
270934a29f5SSebastian Grimberg     if (active_bases_in[b_in] == CEED_BASIS_NONE) num_qpts = num_nodes;
271934a29f5SSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(active_bases_in[b_in], &num_qpts));
272ed9e99e6SJeremy L Thompson 
273352a5e7cSSebastian Grimberg     // Construct identity matrix for basis if required
274934a29f5SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in[b_in]; i++) {
275934a29f5SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[b_in][i] == CEED_EVAL_NONE);
276ed9e99e6SJeremy L Thompson     }
277934a29f5SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out[b_out]; i++) {
278934a29f5SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[b_out][i] == CEED_EVAL_NONE);
279ed9e99e6SJeremy L Thompson     }
280ed9e99e6SJeremy L Thompson     if (has_eval_none) {
2812b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
2822b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
283eaf62fffSJeremy L Thompson     }
284352a5e7cSSebastian Grimberg 
285eaf62fffSJeremy L Thompson     // Compute the diagonal of B^T D B
286eaf62fffSJeremy L Thompson     // Each element
287b94338b9SJed Brown     for (CeedSize e = 0; e < num_elem; e++) {
288eaf62fffSJeremy L Thompson       // Each basis eval mode pair
289352a5e7cSSebastian Grimberg       CeedInt      d_out              = 0, q_comp_out;
290352a5e7cSSebastian Grimberg       CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
2911c66c397SJeremy L Thompson 
292934a29f5SSebastian Grimberg       for (CeedInt e_out = 0; e_out < num_eval_modes_out[b_out]; e_out++) {
2931c66c397SJeremy L Thompson         CeedInt           d_in              = 0, q_comp_in;
294437c7c90SJeremy L Thompson         const CeedScalar *B_t               = NULL;
2951c66c397SJeremy L Thompson         CeedEvalMode      eval_mode_in_prev = CEED_EVAL_NONE;
2961c66c397SJeremy L Thompson 
297934a29f5SSebastian Grimberg         CeedCall(CeedOperatorGetBasisPointer(active_bases_out[b_out], eval_modes_out[b_out][e_out], identity, &B_t));
298934a29f5SSebastian Grimberg         CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_out[b_out], eval_modes_out[b_out][e_out], &q_comp_out));
299352a5e7cSSebastian Grimberg         if (q_comp_out > 1) {
300934a29f5SSebastian Grimberg           if (e_out == 0 || eval_modes_out[b_out][e_out] != eval_mode_out_prev) d_out = 0;
301352a5e7cSSebastian Grimberg           else B_t = &B_t[(++d_out) * num_qpts * num_nodes];
302352a5e7cSSebastian Grimberg         }
303934a29f5SSebastian Grimberg         eval_mode_out_prev = eval_modes_out[b_out][e_out];
304352a5e7cSSebastian Grimberg 
305934a29f5SSebastian Grimberg         for (CeedInt e_in = 0; e_in < num_eval_modes_in[b_in]; e_in++) {
306437c7c90SJeremy L Thompson           const CeedScalar *B = NULL;
3071c66c397SJeremy L Thompson 
308934a29f5SSebastian Grimberg           CeedCall(CeedOperatorGetBasisPointer(active_bases_in[b_in], eval_modes_in[b_in][e_in], identity, &B));
309934a29f5SSebastian Grimberg           CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_in[b_in], eval_modes_in[b_in][e_in], &q_comp_in));
310352a5e7cSSebastian Grimberg           if (q_comp_in > 1) {
311934a29f5SSebastian Grimberg             if (e_in == 0 || eval_modes_in[b_in][e_in] != eval_mode_in_prev) d_in = 0;
312352a5e7cSSebastian Grimberg             else B = &B[(++d_in) * num_qpts * num_nodes];
313352a5e7cSSebastian Grimberg           }
314934a29f5SSebastian Grimberg           eval_mode_in_prev = eval_modes_in[b_in][e_in];
315352a5e7cSSebastian Grimberg 
316eaf62fffSJeremy L Thompson           // Each component
317506b1a0cSSebastian Grimberg           for (CeedInt c_out = 0; c_out < num_comp; c_out++) {
318437c7c90SJeremy L Thompson             // Each qpt/node pair
3192b730f8bSJeremy L Thompson             for (CeedInt q = 0; q < num_qpts; q++) {
320bd83916cSSebastian Grimberg               if (is_point_block) {
321eaf62fffSJeremy L Thompson                 // Point Block Diagonal
322506b1a0cSSebastian Grimberg                 for (CeedInt c_in = 0; c_in < num_comp; c_in++) {
323934a29f5SSebastian Grimberg                   const CeedSize c_offset =
324934a29f5SSebastian Grimberg                       (eval_mode_offsets_in[b_in][e_in] + c_in) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out;
325506b1a0cSSebastian Grimberg                   const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]];
3261c66c397SJeremy L Thompson 
3272b730f8bSJeremy L Thompson                   for (CeedInt n = 0; n < num_nodes; n++) {
328506b1a0cSSebastian Grimberg                     elem_diag_array[((e * num_comp + c_out) * num_comp + c_in) * num_nodes + n] +=
329437c7c90SJeremy L Thompson                         B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n];
330eaf62fffSJeremy L Thompson                   }
3312b730f8bSJeremy L Thompson                 }
332eaf62fffSJeremy L Thompson               } else {
333eaf62fffSJeremy L Thompson                 // Diagonal Only
334934a29f5SSebastian Grimberg                 const CeedInt c_offset =
335934a29f5SSebastian Grimberg                     (eval_mode_offsets_in[b_in][e_in] + c_out) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out;
336506b1a0cSSebastian Grimberg                 const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]];
3371c66c397SJeremy L Thompson 
3382b730f8bSJeremy L Thompson                 for (CeedInt n = 0; n < num_nodes; n++) {
339506b1a0cSSebastian 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];
340eaf62fffSJeremy L Thompson                 }
341eaf62fffSJeremy L Thompson               }
342eaf62fffSJeremy L Thompson             }
343eaf62fffSJeremy L Thompson           }
3442b730f8bSJeremy L Thompson         }
3452b730f8bSJeremy L Thompson       }
3462b730f8bSJeremy L Thompson     }
3472b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
348eaf62fffSJeremy L Thompson 
349eaf62fffSJeremy L Thompson     // Assemble local operator diagonal
3507c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
351eaf62fffSJeremy L Thompson 
352eaf62fffSJeremy L Thompson     // Cleanup
3537c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr));
3542b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&elem_diag));
3552b730f8bSJeremy L Thompson     CeedCall(CeedFree(&identity));
356437c7c90SJeremy L Thompson   }
357437c7c90SJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
358437c7c90SJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
359eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
360eaf62fffSJeremy L Thompson }
361eaf62fffSJeremy L Thompson 
362eaf62fffSJeremy L Thompson /**
363eaf62fffSJeremy L Thompson   @brief Core logic for assembling composite operator diagonal
364eaf62fffSJeremy L Thompson 
365eaf62fffSJeremy L Thompson   @param[in]  op             CeedOperator to assemble point block diagonal
366ea61e9acSJeremy L Thompson   @param[in]  request        Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE
367bd83916cSSebastian Grimberg   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
368eaf62fffSJeremy L Thompson   @param[out] assembled      CeedVector to store assembled diagonal
369eaf62fffSJeremy L Thompson 
370eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
371eaf62fffSJeremy L Thompson 
372eaf62fffSJeremy L Thompson   @ref Developer
373eaf62fffSJeremy L Thompson **/
374bd83916cSSebastian Grimberg static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block,
375eaf62fffSJeremy L Thompson                                                                  CeedVector assembled) {
376eaf62fffSJeremy L Thompson   CeedInt       num_sub;
377eaf62fffSJeremy L Thompson   CeedOperator *suboperators;
3781c66c397SJeremy L Thompson 
379c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
380c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators));
381eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_sub; i++) {
382bd83916cSSebastian Grimberg     if (is_point_block) {
3832b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request));
3846aa95790SJeremy L Thompson     } else {
3852b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request));
3866aa95790SJeremy L Thompson     }
387eaf62fffSJeremy L Thompson   }
388eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
389eaf62fffSJeremy L Thompson }
390eaf62fffSJeremy L Thompson 
391eaf62fffSJeremy L Thompson /**
392eaf62fffSJeremy L Thompson   @brief Build nonzero pattern for non-composite operator
393eaf62fffSJeremy L Thompson 
394eaf62fffSJeremy L Thompson   Users should generally use CeedOperatorLinearAssembleSymbolic()
395eaf62fffSJeremy L Thompson 
396eaf62fffSJeremy L Thompson   @param[in]  op     CeedOperator to assemble nonzero pattern
397eaf62fffSJeremy L Thompson   @param[in]  offset Offset for number of entries
398eaf62fffSJeremy L Thompson   @param[out] rows   Row number for each entry
399eaf62fffSJeremy L Thompson   @param[out] cols   Column number for each entry
400eaf62fffSJeremy L Thompson 
401eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
402eaf62fffSJeremy L Thompson 
403eaf62fffSJeremy L Thompson   @ref Developer
404eaf62fffSJeremy L Thompson **/
4052b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) {
406f3d47e36SJeremy L Thompson   Ceed                ceed;
407f3d47e36SJeremy L Thompson   bool                is_composite;
408506b1a0cSSebastian Grimberg   CeedSize            num_nodes_in, num_nodes_out, count = 0;
409506b1a0cSSebastian Grimberg   CeedInt             num_elem_in, elem_size_in, num_comp_in, layout_er_in[3];
410506b1a0cSSebastian Grimberg   CeedInt             num_elem_out, elem_size_out, num_comp_out, layout_er_out[3], local_num_entries;
4111c66c397SJeremy L Thompson   CeedScalar         *array;
412506b1a0cSSebastian Grimberg   const CeedScalar   *elem_dof_a_in, *elem_dof_a_out;
413506b1a0cSSebastian Grimberg   CeedVector          index_vec_in, index_vec_out, elem_dof_in, elem_dof_out;
414506b1a0cSSebastian Grimberg   CeedElemRestriction elem_rstr_in, elem_rstr_out, index_elem_rstr_in, index_elem_rstr_out;
4151c66c397SJeremy L Thompson 
416f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
417f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4186574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
419eaf62fffSJeremy L Thompson 
420506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes_in, &num_nodes_out));
421506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out));
422506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in));
423506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in));
424506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in));
425506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetELayout(elem_rstr_in, &layout_er_in));
426eaf62fffSJeremy L Thompson 
427506b1a0cSSebastian Grimberg   // Determine elem_dof relation for input
428506b1a0cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed, num_nodes_in, &index_vec_in));
429506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayWrite(index_vec_in, CEED_MEM_HOST, &array));
430506b1a0cSSebastian Grimberg   for (CeedInt i = 0; i < num_nodes_in; i++) array[i] = i;
431506b1a0cSSebastian Grimberg   CeedCall(CeedVectorRestoreArray(index_vec_in, &array));
432506b1a0cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed, num_elem_in * elem_size_in * num_comp_in, &elem_dof_in));
433506b1a0cSSebastian Grimberg   CeedCall(CeedVectorSetValue(elem_dof_in, 0.0));
434506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_in, &index_elem_rstr_in));
435506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionApply(index_elem_rstr_in, CEED_NOTRANSPOSE, index_vec_in, elem_dof_in, CEED_REQUEST_IMMEDIATE));
436506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayRead(elem_dof_in, CEED_MEM_HOST, &elem_dof_a_in));
437506b1a0cSSebastian Grimberg   CeedCall(CeedVectorDestroy(&index_vec_in));
438506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_in));
439506b1a0cSSebastian Grimberg 
440506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
441506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out));
442506b1a0cSSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
443506b1a0cSSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
444506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out));
445506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out));
446506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetELayout(elem_rstr_out, &layout_er_out));
447506b1a0cSSebastian Grimberg 
448506b1a0cSSebastian Grimberg     // Determine elem_dof relation for output
449506b1a0cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed, num_nodes_out, &index_vec_out));
450506b1a0cSSebastian Grimberg     CeedCall(CeedVectorGetArrayWrite(index_vec_out, CEED_MEM_HOST, &array));
451506b1a0cSSebastian Grimberg     for (CeedInt i = 0; i < num_nodes_out; i++) array[i] = i;
452506b1a0cSSebastian Grimberg     CeedCall(CeedVectorRestoreArray(index_vec_out, &array));
453506b1a0cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed, num_elem_out * elem_size_out * num_comp_out, &elem_dof_out));
454506b1a0cSSebastian Grimberg     CeedCall(CeedVectorSetValue(elem_dof_out, 0.0));
455506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_out, &index_elem_rstr_out));
456506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(index_elem_rstr_out, CEED_NOTRANSPOSE, index_vec_out, elem_dof_out, CEED_REQUEST_IMMEDIATE));
457506b1a0cSSebastian Grimberg     CeedCall(CeedVectorGetArrayRead(elem_dof_out, CEED_MEM_HOST, &elem_dof_a_out));
458506b1a0cSSebastian Grimberg     CeedCall(CeedVectorDestroy(&index_vec_out));
459506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_out));
460506b1a0cSSebastian Grimberg   } else {
461506b1a0cSSebastian Grimberg     num_elem_out     = num_elem_in;
462506b1a0cSSebastian Grimberg     elem_size_out    = elem_size_in;
463506b1a0cSSebastian Grimberg     num_comp_out     = num_comp_in;
464506b1a0cSSebastian Grimberg     layout_er_out[0] = layout_er_in[0];
465506b1a0cSSebastian Grimberg     layout_er_out[1] = layout_er_in[1];
466506b1a0cSSebastian Grimberg     layout_er_out[2] = layout_er_in[2];
467506b1a0cSSebastian Grimberg     elem_dof_a_out   = elem_dof_a_in;
468506b1a0cSSebastian Grimberg   }
469506b1a0cSSebastian Grimberg   local_num_entries = elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in;
470eaf62fffSJeremy L Thompson 
471eaf62fffSJeremy L Thompson   // Determine i, j locations for element matrices
472506b1a0cSSebastian Grimberg   for (CeedInt e = 0; e < num_elem_in; e++) {
473506b1a0cSSebastian Grimberg     for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) {
474506b1a0cSSebastian Grimberg       for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) {
475506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < elem_size_out; i++) {
476506b1a0cSSebastian Grimberg           for (CeedInt j = 0; j < elem_size_in; j++) {
477506b1a0cSSebastian Grimberg             const CeedInt elem_dof_index_row = i * layout_er_out[0] + comp_out * layout_er_out[1] + e * layout_er_out[2];
478506b1a0cSSebastian Grimberg             const CeedInt elem_dof_index_col = j * layout_er_in[0] + comp_in * layout_er_in[1] + e * layout_er_in[2];
479506b1a0cSSebastian Grimberg             const CeedInt row                = elem_dof_a_out[elem_dof_index_row];
480506b1a0cSSebastian Grimberg             const CeedInt col                = elem_dof_a_in[elem_dof_index_col];
481eaf62fffSJeremy L Thompson 
482eaf62fffSJeremy L Thompson             rows[offset + count] = row;
483eaf62fffSJeremy L Thompson             cols[offset + count] = col;
484eaf62fffSJeremy L Thompson             count++;
485eaf62fffSJeremy L Thompson           }
486eaf62fffSJeremy L Thompson         }
487eaf62fffSJeremy L Thompson       }
488eaf62fffSJeremy L Thompson     }
489eaf62fffSJeremy L Thompson   }
4906574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing assembled entries");
491506b1a0cSSebastian Grimberg   CeedCall(CeedVectorRestoreArrayRead(elem_dof_in, &elem_dof_a_in));
492506b1a0cSSebastian Grimberg   CeedCall(CeedVectorDestroy(&elem_dof_in));
493506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
494506b1a0cSSebastian Grimberg     CeedCall(CeedVectorRestoreArrayRead(elem_dof_out, &elem_dof_a_out));
495506b1a0cSSebastian Grimberg     CeedCall(CeedVectorDestroy(&elem_dof_out));
496506b1a0cSSebastian Grimberg   }
497eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
498eaf62fffSJeremy L Thompson }
499eaf62fffSJeremy L Thompson 
500eaf62fffSJeremy L Thompson /**
501eaf62fffSJeremy L Thompson   @brief Assemble nonzero entries for non-composite operator
502eaf62fffSJeremy L Thompson 
503eaf62fffSJeremy L Thompson   Users should generally use CeedOperatorLinearAssemble()
504eaf62fffSJeremy L Thompson 
505eaf62fffSJeremy L Thompson   @param[in]  op     CeedOperator to assemble
506ea61e9acSJeremy L Thompson   @param[in]  offset Offset for number of entries
507eaf62fffSJeremy L Thompson   @param[out] values Values to assemble into matrix
508eaf62fffSJeremy L Thompson 
509eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
510eaf62fffSJeremy L Thompson 
511eaf62fffSJeremy L Thompson   @ref Developer
512eaf62fffSJeremy L Thompson **/
5132b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) {
514f3d47e36SJeremy L Thompson   Ceed ceed;
515f3d47e36SJeremy L Thompson   bool is_composite;
5161c66c397SJeremy L Thompson 
517f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
518f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
5196574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
520f3d47e36SJeremy L Thompson 
521f3d47e36SJeremy L Thompson   // Early exit for empty operator
522f3d47e36SJeremy L Thompson   {
523f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
524f3d47e36SJeremy L Thompson 
525f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
526f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
527f3d47e36SJeremy L Thompson   }
528eaf62fffSJeremy L Thompson 
529cefa2673SJeremy L Thompson   if (op->LinearAssembleSingle) {
530cefa2673SJeremy L Thompson     // Backend version
5312b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSingle(op, offset, values));
532cefa2673SJeremy L Thompson     return CEED_ERROR_SUCCESS;
533cefa2673SJeremy L Thompson   } else {
534cefa2673SJeremy L Thompson     // Operator fallback
535cefa2673SJeremy L Thompson     CeedOperator op_fallback;
536cefa2673SJeremy L Thompson 
5372b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
538cefa2673SJeremy L Thompson     if (op_fallback) {
5392b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values));
540cefa2673SJeremy L Thompson       return CEED_ERROR_SUCCESS;
541cefa2673SJeremy L Thompson     }
542cefa2673SJeremy L Thompson   }
543cefa2673SJeremy L Thompson 
544eaf62fffSJeremy L Thompson   // Assemble QFunction
545506b1a0cSSebastian Grimberg   CeedInt             layout_qf[3];
5461c66c397SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
547c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
548506b1a0cSSebastian Grimberg   CeedElemRestriction assembled_elem_rstr = NULL;
549eaf62fffSJeremy L Thompson 
550506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, CEED_REQUEST_IMMEDIATE));
551506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, &layout_qf));
552506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
553506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
554eaf62fffSJeremy L Thompson 
555ed9e99e6SJeremy L Thompson   // Get assembly data
556506b1a0cSSebastian Grimberg   CeedInt                  num_elem_in, elem_size_in, num_comp_in, num_qpts_in;
557506b1a0cSSebastian Grimberg   CeedInt                  num_elem_out, elem_size_out, num_comp_out, num_qpts_out, local_num_entries;
558506b1a0cSSebastian Grimberg   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
559506b1a0cSSebastian Grimberg   CeedInt                  num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out;
560506b1a0cSSebastian Grimberg   CeedBasis               *active_bases_in, *active_bases_out, basis_in, basis_out;
561506b1a0cSSebastian Grimberg   const CeedScalar       **B_mats_in, **B_mats_out, *B_mat_in, *B_mat_out;
562506b1a0cSSebastian Grimberg   CeedElemRestriction      elem_rstr_in, elem_rstr_out;
563506b1a0cSSebastian Grimberg   CeedRestrictionType      elem_rstr_type_in, elem_rstr_type_out;
564506b1a0cSSebastian Grimberg   const bool              *elem_rstr_orients_in = NULL, *elem_rstr_orients_out = NULL;
565506b1a0cSSebastian Grimberg   const CeedInt8          *elem_rstr_curl_orients_in = NULL, *elem_rstr_curl_orients_out = NULL;
566506b1a0cSSebastian Grimberg   CeedOperatorAssemblyData data;
567eaf62fffSJeremy L Thompson 
568506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
569506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, NULL, &num_active_bases_out,
570506b1a0cSSebastian Grimberg                                                 &num_eval_modes_out, &eval_modes_out, NULL, NULL));
571506b1a0cSSebastian Grimberg 
572506b1a0cSSebastian Grimberg   CeedCheck(num_active_bases_in == num_active_bases_out && num_active_bases_in == 1, ceed, CEED_ERROR_UNSUPPORTED,
573506b1a0cSSebastian Grimberg             "Cannot assemble operator with multiple active bases");
5746574a04fSJeremy 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");
575eaf62fffSJeremy L Thompson 
576506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, &B_mats_in, NULL, &active_bases_out, &B_mats_out));
577506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out));
578506b1a0cSSebastian Grimberg   basis_in  = active_bases_in[0];
579506b1a0cSSebastian Grimberg   basis_out = active_bases_out[0];
580506b1a0cSSebastian Grimberg   B_mat_in  = B_mats_in[0];
581506b1a0cSSebastian Grimberg   B_mat_out = B_mats_out[0];
582eaf62fffSJeremy L Thompson 
583506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in));
584506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in));
585506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in));
586506b1a0cSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
587506b1a0cSSebastian Grimberg   else CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
588506b1a0cSSebastian Grimberg 
589506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetType(elem_rstr_in, &elem_rstr_type_in));
590506b1a0cSSebastian Grimberg   if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) {
591506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_orients_in));
592506b1a0cSSebastian Grimberg   } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
593506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_curl_orients_in));
5947c1dbaffSSebastian Grimberg   }
5957c1dbaffSSebastian Grimberg 
596506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
597506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out));
598506b1a0cSSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
599506b1a0cSSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
600506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out));
601506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out));
602506b1a0cSSebastian Grimberg     if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
603506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
604506b1a0cSSebastian Grimberg     CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
605506b1a0cSSebastian Grimberg               "Active input and output bases must have the same number of quadrature points");
606eaf62fffSJeremy L Thompson 
607506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(elem_rstr_out, &elem_rstr_type_out));
608506b1a0cSSebastian Grimberg     if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) {
609506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_orients_out));
610506b1a0cSSebastian Grimberg     } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
611506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_curl_orients_out));
612506b1a0cSSebastian Grimberg     }
613506b1a0cSSebastian Grimberg   } else {
614506b1a0cSSebastian Grimberg     num_elem_out  = num_elem_in;
615506b1a0cSSebastian Grimberg     elem_size_out = elem_size_in;
616506b1a0cSSebastian Grimberg     num_comp_out  = num_comp_in;
617506b1a0cSSebastian Grimberg     num_qpts_out  = num_qpts_in;
618506b1a0cSSebastian Grimberg 
619506b1a0cSSebastian Grimberg     elem_rstr_orients_out      = elem_rstr_orients_in;
620506b1a0cSSebastian Grimberg     elem_rstr_curl_orients_out = elem_rstr_curl_orients_in;
621506b1a0cSSebastian Grimberg   }
622506b1a0cSSebastian Grimberg   local_num_entries = elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in;
623506b1a0cSSebastian Grimberg 
624506b1a0cSSebastian Grimberg   // Loop over elements and put in data structure
6257c1dbaffSSebastian Grimberg   // We store B_mat_in, B_mat_out, BTD, elem_mat in row-major order
6260459ebd3SSebastian Grimberg   CeedTensorContract contract;
627*4a9a33d7SSebastian Grimberg   CeedSize           count = 0;
628123d890dSSebastian Grimberg   CeedScalar        *vals, *BTD_mat = NULL, *elem_mat = NULL, *elem_mat_b = NULL;
629506b1a0cSSebastian Grimberg 
630c22497adSSebastian Grimberg   CeedCall(CeedBasisGetTensorContract(basis_in, &contract));
631123d890dSSebastian Grimberg   CeedCall(CeedCalloc(elem_size_out * num_qpts_in * num_eval_modes_in[0], &BTD_mat));
632123d890dSSebastian Grimberg   CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat));
633506b1a0cSSebastian Grimberg   if (elem_rstr_curl_orients_in || elem_rstr_curl_orients_out) CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat_b));
6341c66c397SJeremy L Thompson 
63528ec399dSJeremy L Thompson   CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals));
636506b1a0cSSebastian Grimberg   for (CeedSize e = 0; e < num_elem_in; e++) {
637506b1a0cSSebastian Grimberg     for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) {
638506b1a0cSSebastian Grimberg       for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) {
639ed9e99e6SJeremy L Thompson         // Compute B^T*D
640506b1a0cSSebastian Grimberg         for (CeedSize n = 0; n < elem_size_out; n++) {
641506b1a0cSSebastian Grimberg           for (CeedSize q = 0; q < num_qpts_in; q++) {
642437c7c90SJeremy L Thompson             for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) {
643506b1a0cSSebastian Grimberg               const CeedSize btd_index = n * (num_qpts_in * num_eval_modes_in[0]) + q * num_eval_modes_in[0] + e_in;
644067fd99fSJeremy L Thompson               CeedScalar     sum       = 0.0;
6451c66c397SJeremy L Thompson 
646437c7c90SJeremy L Thompson               for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) {
647506b1a0cSSebastian Grimberg                 const CeedSize b_out_index     = (q * num_eval_modes_out[0] + e_out) * elem_size_out + n;
648506b1a0cSSebastian 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;
649b94338b9SJed Brown                 const CeedSize qf_index        = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2];
6501c66c397SJeremy L Thompson 
651067fd99fSJeremy L Thompson                 sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index];
652eaf62fffSJeremy L Thompson               }
653067fd99fSJeremy L Thompson               BTD_mat[btd_index] = sum;
654ed9e99e6SJeremy L Thompson             }
655ed9e99e6SJeremy L Thompson           }
656eaf62fffSJeremy L Thompson         }
6577c1dbaffSSebastian Grimberg 
6587c1dbaffSSebastian Grimberg         // Form element matrix itself (for each block component)
659e4065a52SSebastian Grimberg         if (contract) {
6600459ebd3SSebastian Grimberg           CeedCall(CeedTensorContractApply(contract, 1, num_qpts_in * num_eval_modes_in[0], elem_size_in, elem_size_out, BTD_mat, CEED_NOTRANSPOSE,
6610459ebd3SSebastian Grimberg                                            false, B_mat_in, elem_mat));
662e4065a52SSebastian Grimberg         } else {
663e4065a52SSebastian 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]));
664e4065a52SSebastian Grimberg         }
665eaf62fffSJeremy L Thompson 
6667c1dbaffSSebastian Grimberg         // Transform the element matrix if required
667506b1a0cSSebastian Grimberg         if (elem_rstr_orients_out) {
668506b1a0cSSebastian Grimberg           const bool *elem_orients = &elem_rstr_orients_out[e * elem_size_out];
6691c66c397SJeremy L Thompson 
670506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
671506b1a0cSSebastian Grimberg             const double orient = elem_orients[i] ? -1.0 : 1.0;
672506b1a0cSSebastian Grimberg 
673506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
674506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] *= orient;
6757c1dbaffSSebastian Grimberg             }
6767c1dbaffSSebastian Grimberg           }
677506b1a0cSSebastian Grimberg         } else if (elem_rstr_curl_orients_out) {
678506b1a0cSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_out[e * 3 * elem_size_out];
6791c66c397SJeremy L Thompson 
6807c1dbaffSSebastian Grimberg           // T^T*(B^T*D*B)
681506b1a0cSSebastian Grimberg           memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar));
682506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
683506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
684506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * i + 1] +
685506b1a0cSSebastian Grimberg                                                (i > 0 ? elem_mat_b[(i - 1) * elem_size_in + j] * elem_curl_orients[3 * i - 1] : 0.0) +
686506b1a0cSSebastian Grimberg                                                (i < elem_size_out - 1 ? elem_mat_b[(i + 1) * elem_size_in + j] * elem_curl_orients[3 * i + 3] : 0.0);
6877c1dbaffSSebastian Grimberg             }
6887c1dbaffSSebastian Grimberg           }
689506b1a0cSSebastian Grimberg         }
690506b1a0cSSebastian Grimberg         if (elem_rstr_orients_in) {
691506b1a0cSSebastian Grimberg           const bool *elem_orients = &elem_rstr_orients_in[e * elem_size_in];
692506b1a0cSSebastian Grimberg 
693506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
694506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
695506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] *= elem_orients[j] ? -1.0 : 1.0;
696506b1a0cSSebastian Grimberg             }
697506b1a0cSSebastian Grimberg           }
698506b1a0cSSebastian Grimberg         } else if (elem_rstr_curl_orients_in) {
699506b1a0cSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_in[e * 3 * elem_size_in];
700506b1a0cSSebastian Grimberg 
701506b1a0cSSebastian Grimberg           // (B^T*D*B)*T
702506b1a0cSSebastian Grimberg           memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar));
703506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
704506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
705506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * j + 1] +
706506b1a0cSSebastian Grimberg                                                (j > 0 ? elem_mat_b[i * elem_size_in + j - 1] * elem_curl_orients[3 * j - 1] : 0.0) +
707506b1a0cSSebastian Grimberg                                                (j < elem_size_in - 1 ? elem_mat_b[i * elem_size_in + j + 1] * elem_curl_orients[3 * j + 3] : 0.0);
7087c1dbaffSSebastian Grimberg             }
7097c1dbaffSSebastian Grimberg           }
7107c1dbaffSSebastian Grimberg         }
7117c1dbaffSSebastian Grimberg 
7127c1dbaffSSebastian Grimberg         // Put element matrix in coordinate data structure
713506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < elem_size_out; i++) {
714506b1a0cSSebastian Grimberg           for (CeedInt j = 0; j < elem_size_in; j++) {
715506b1a0cSSebastian Grimberg             vals[offset + count] = elem_mat[i * elem_size_in + j];
716eaf62fffSJeremy L Thompson             count++;
717eaf62fffSJeremy L Thompson           }
718eaf62fffSJeremy L Thompson         }
719eaf62fffSJeremy L Thompson       }
720eaf62fffSJeremy L Thompson     }
721eaf62fffSJeremy L Thompson   }
7226574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing entries");
7232b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(values, &vals));
724eaf62fffSJeremy L Thompson 
725506b1a0cSSebastian Grimberg   // Cleanup
726123d890dSSebastian Grimberg   CeedCall(CeedFree(&BTD_mat));
727123d890dSSebastian Grimberg   CeedCall(CeedFree(&elem_mat));
728506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&elem_mat_b));
729506b1a0cSSebastian Grimberg   if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) {
730506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_in, &elem_rstr_orients_in));
731506b1a0cSSebastian Grimberg   } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
732506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_in, &elem_rstr_curl_orients_in));
733506b1a0cSSebastian Grimberg   }
734506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
735506b1a0cSSebastian Grimberg     if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) {
736506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_out, &elem_rstr_orients_out));
737506b1a0cSSebastian Grimberg     } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
738506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_out, &elem_rstr_curl_orients_out));
739506b1a0cSSebastian Grimberg     }
740506b1a0cSSebastian Grimberg   }
7412b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
7422b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
743eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
744eaf62fffSJeremy L Thompson }
745eaf62fffSJeremy L Thompson 
746eaf62fffSJeremy L Thompson /**
747eaf62fffSJeremy L Thompson   @brief Count number of entries for assembled CeedOperator
748eaf62fffSJeremy L Thompson 
749eaf62fffSJeremy L Thompson   @param[in]  op          CeedOperator to assemble
750eaf62fffSJeremy L Thompson   @param[out] num_entries Number of entries in assembled representation
751eaf62fffSJeremy L Thompson 
752eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
753eaf62fffSJeremy L Thompson 
754eaf62fffSJeremy L Thompson   @ref Utility
755eaf62fffSJeremy L Thompson **/
756b94338b9SJed Brown static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedSize *num_entries) {
757b275c451SJeremy L Thompson   bool                is_composite;
758506b1a0cSSebastian Grimberg   CeedInt             num_elem_in, elem_size_in, num_comp_in, num_elem_out, elem_size_out, num_comp_out;
759506b1a0cSSebastian Grimberg   CeedElemRestriction rstr_in, rstr_out;
760eaf62fffSJeremy L Thompson 
761b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
7626574a04fSJeremy L Thompson   CeedCheck(!is_composite, op->ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
763506b1a0cSSebastian Grimberg 
764506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
765506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
766506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
767506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
768506b1a0cSSebastian Grimberg   if (rstr_in != rstr_out) {
769506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
770506b1a0cSSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, op->ceed, CEED_ERROR_UNSUPPORTED,
771506b1a0cSSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
772506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
773506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
774506b1a0cSSebastian Grimberg   } else {
775506b1a0cSSebastian Grimberg     num_elem_out  = num_elem_in;
776506b1a0cSSebastian Grimberg     elem_size_out = elem_size_in;
777506b1a0cSSebastian Grimberg     num_comp_out  = num_comp_in;
778506b1a0cSSebastian Grimberg   }
779506b1a0cSSebastian Grimberg   *num_entries = (CeedSize)elem_size_in * num_comp_in * elem_size_out * num_comp_out * num_elem_in;
780eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
781eaf62fffSJeremy L Thompson }
782eaf62fffSJeremy L Thompson 
783eaf62fffSJeremy L Thompson /**
784ea61e9acSJeremy L Thompson   @brief Common code for creating a multigrid coarse operator and level transfer operators for a CeedOperator
785eaf62fffSJeremy L Thompson 
786eaf62fffSJeremy L Thompson   @param[in]  op_fine      Fine grid operator
78785bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
788eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid restriction
789eaf62fffSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector basis
79085bb9dcfSJeremy L Thompson   @param[in]  basis_c_to_f Basis for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
791eaf62fffSJeremy L Thompson   @param[out] op_coarse    Coarse grid operator
79285bb9dcfSJeremy L Thompson   @param[out] op_prolong   Coarse to fine operator, or NULL
7937758292fSSebastian Grimberg   @param[out] op_restrict  Fine to coarse operator, or NULL
794eaf62fffSJeremy L Thompson 
795eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
796eaf62fffSJeremy L Thompson 
797eaf62fffSJeremy L Thompson   @ref Developer
798eaf62fffSJeremy L Thompson **/
7992b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
8007758292fSSebastian Grimberg                                             CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
8011c66c397SJeremy L Thompson   bool                is_composite;
802eaf62fffSJeremy L Thompson   Ceed                ceed;
8031c66c397SJeremy L Thompson   CeedInt             num_comp;
80485bb9dcfSJeremy L Thompson   CeedVector          mult_vec         = NULL;
8051c66c397SJeremy L Thompson   CeedElemRestriction rstr_p_mult_fine = NULL, rstr_fine = NULL;
8061c66c397SJeremy L Thompson 
8072b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
808eaf62fffSJeremy L Thompson 
809eaf62fffSJeremy L Thompson   // Check for composite operator
8102b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op_fine, &is_composite));
8116574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported");
812eaf62fffSJeremy L Thompson 
813eaf62fffSJeremy L Thompson   // Coarse Grid
8142b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
815eaf62fffSJeremy L Thompson   // -- Clone input fields
81692ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < op_fine->qf->num_input_fields; i++) {
817eaf62fffSJeremy L Thompson     if (op_fine->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
818437c7c90SJeremy L Thompson       rstr_fine = op_fine->input_fields[i]->elem_rstr;
8192b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE));
820eaf62fffSJeremy L Thompson     } else {
821437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, op_fine->input_fields[i]->elem_rstr,
8222b730f8bSJeremy L Thompson                                     op_fine->input_fields[i]->basis, op_fine->input_fields[i]->vec));
823eaf62fffSJeremy L Thompson     }
824eaf62fffSJeremy L Thompson   }
825eaf62fffSJeremy L Thompson   // -- Clone output fields
82692ae7e47SJeremy L Thompson   for (CeedInt i = 0; i < op_fine->qf->num_output_fields; i++) {
827eaf62fffSJeremy L Thompson     if (op_fine->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
8282b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE));
829eaf62fffSJeremy L Thompson     } else {
830437c7c90SJeremy L Thompson       CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, op_fine->output_fields[i]->elem_rstr,
8312b730f8bSJeremy L Thompson                                     op_fine->output_fields[i]->basis, op_fine->output_fields[i]->vec));
832eaf62fffSJeremy L Thompson     }
833eaf62fffSJeremy L Thompson   }
834af99e877SJeremy L Thompson   // -- Clone QFunctionAssemblyData
8352b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op_fine->qf_assembled, &(*op_coarse)->qf_assembled));
836eaf62fffSJeremy L Thompson 
837eaf62fffSJeremy L Thompson   // Multiplicity vector
8387758292fSSebastian Grimberg   if (op_restrict || op_prolong) {
83985bb9dcfSJeremy L Thompson     CeedVector          mult_e_vec;
8401c66c397SJeremy L Thompson     CeedRestrictionType rstr_type;
84185bb9dcfSJeremy L Thompson 
8427c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr_fine, &rstr_type));
8437c1dbaffSSebastian Grimberg     CeedCheck(rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_UNSUPPORTED,
8447c1dbaffSSebastian Grimberg               "Element restrictions created with CeedElemRestrictionCreateCurlOriented are not supported");
8456574a04fSJeremy L Thompson     CeedCheck(p_mult_fine, ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector");
8467c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnsignedCopy(rstr_fine, &rstr_p_mult_fine));
8472b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec));
8482b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_e_vec, 0.0));
849c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE));
8502b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_vec, 0.0));
851c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE));
8522b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&mult_e_vec));
8532b730f8bSJeremy L Thompson     CeedCall(CeedVectorReciprocal(mult_vec));
85485bb9dcfSJeremy L Thompson   }
855eaf62fffSJeremy L Thompson 
856addd79feSZach Atkins   // Clone name
857addd79feSZach Atkins   bool   has_name = op_fine->name;
858addd79feSZach Atkins   size_t name_len = op_fine->name ? strlen(op_fine->name) : 0;
859addd79feSZach Atkins   CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name));
860addd79feSZach Atkins 
8617758292fSSebastian Grimberg   // Check that coarse to fine basis is provided if prolong/restrict operators are requested
8627758292fSSebastian Grimberg   CeedCheck(basis_c_to_f || (!op_restrict && !op_prolong), ceed, CEED_ERROR_INCOMPATIBLE,
8636574a04fSJeremy L Thompson             "Prolongation or restriction operator creation requires coarse-to-fine basis");
86483d6adf3SZach Atkins 
86585bb9dcfSJeremy L Thompson   // Restriction/Prolongation Operators
8662b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp));
867addd79feSZach Atkins 
868addd79feSZach Atkins   // Restriction
8697758292fSSebastian Grimberg   if (op_restrict) {
870eaf62fffSJeremy L Thompson     CeedInt             *num_comp_r_data;
87185bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_r;
8727758292fSSebastian Grimberg     CeedQFunction        qf_restrict;
87385bb9dcfSJeremy L Thompson 
8747758292fSSebastian Grimberg     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict));
8752b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_r_data));
876eaf62fffSJeremy L Thompson     num_comp_r_data[0] = num_comp;
8772b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r));
8782b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data));
8797758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r));
8802b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_r));
8817758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE));
8827758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE));
8837758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP));
8847758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp));
885eaf62fffSJeremy L Thompson 
8867758292fSSebastian Grimberg     CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict));
8877758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
8887758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
8897758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
890eaf62fffSJeremy L Thompson 
891addd79feSZach Atkins     // Set name
892addd79feSZach Atkins     char *restriction_name;
8931c66c397SJeremy L Thompson 
894addd79feSZach Atkins     CeedCall(CeedCalloc(17 + name_len, &restriction_name));
895addd79feSZach Atkins     sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
8967758292fSSebastian Grimberg     CeedCall(CeedOperatorSetName(*op_restrict, restriction_name));
897addd79feSZach Atkins     CeedCall(CeedFree(&restriction_name));
898addd79feSZach Atkins 
899addd79feSZach Atkins     // Check
9007758292fSSebastian Grimberg     CeedCall(CeedOperatorCheckReady(*op_restrict));
901addd79feSZach Atkins 
902addd79feSZach Atkins     // Cleanup
9037758292fSSebastian Grimberg     CeedCall(CeedQFunctionDestroy(&qf_restrict));
904addd79feSZach Atkins   }
905addd79feSZach Atkins 
906eaf62fffSJeremy L Thompson   // Prolongation
907addd79feSZach Atkins   if (op_prolong) {
908eaf62fffSJeremy L Thompson     CeedInt             *num_comp_p_data;
90985bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_p;
9101c66c397SJeremy L Thompson     CeedQFunction        qf_prolong;
91185bb9dcfSJeremy L Thompson 
91285bb9dcfSJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong));
9132b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_p_data));
914eaf62fffSJeremy L Thompson     num_comp_p_data[0] = num_comp;
9152b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p));
9162b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data));
9172b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p));
9182b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_p));
9192b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP));
9202b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE));
9212b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE));
9222b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp));
923eaf62fffSJeremy L Thompson 
9242b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong));
9252b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
926356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
927356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
928eaf62fffSJeremy L Thompson 
929addd79feSZach Atkins     // Set name
930ea6b5821SJeremy L Thompson     char *prolongation_name;
9311c66c397SJeremy L Thompson 
9322b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(18 + name_len, &prolongation_name));
9332b730f8bSJeremy L Thompson     sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
9342b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name));
9352b730f8bSJeremy L Thompson     CeedCall(CeedFree(&prolongation_name));
936addd79feSZach Atkins 
937addd79feSZach Atkins     // Check
938addd79feSZach Atkins     CeedCall(CeedOperatorCheckReady(*op_prolong));
939addd79feSZach Atkins 
940addd79feSZach Atkins     // Cleanup
941addd79feSZach Atkins     CeedCall(CeedQFunctionDestroy(&qf_prolong));
942ea6b5821SJeremy L Thompson   }
943ea6b5821SJeremy L Thompson 
94458e4b056SJeremy L Thompson   // Check
94558e4b056SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(*op_coarse));
94658e4b056SJeremy L Thompson 
947eaf62fffSJeremy L Thompson   // Cleanup
9482b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_vec));
949c17ec2beSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine));
9502b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis_c_to_f));
951eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
952eaf62fffSJeremy L Thompson }
953eaf62fffSJeremy L Thompson 
954eaf62fffSJeremy L Thompson /**
955eaf62fffSJeremy L Thompson   @brief Build 1D mass matrix and Laplacian with perturbation
956eaf62fffSJeremy L Thompson 
957eaf62fffSJeremy L Thompson   @param[in]  interp_1d   Interpolation matrix in one dimension
958eaf62fffSJeremy L Thompson   @param[in]  grad_1d     Gradient matrix in one dimension
959eaf62fffSJeremy L Thompson   @param[in]  q_weight_1d Quadrature weights in one dimension
960eaf62fffSJeremy L Thompson   @param[in]  P_1d        Number of basis nodes in one dimension
961eaf62fffSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
962eaf62fffSJeremy L Thompson   @param[in]  dim         Dimension of basis
963eaf62fffSJeremy L Thompson   @param[out] mass        Assembled mass matrix in one dimension
964eaf62fffSJeremy L Thompson   @param[out] laplace     Assembled perturbed Laplacian in one dimension
965eaf62fffSJeremy L Thompson 
966eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
967eaf62fffSJeremy L Thompson 
968eaf62fffSJeremy L Thompson   @ref Developer
969eaf62fffSJeremy L Thompson **/
9702c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
9712c2ea1dbSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d, CeedInt P_1d, CeedInt Q_1d,
9722c2ea1dbSJeremy L Thompson                                 CeedInt dim, CeedScalar *mass, CeedScalar *laplace) {
9732b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
974eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
975eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
9762b730f8bSJeremy 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];
977eaf62fffSJeremy L Thompson       mass[i + j * P_1d] = sum;
978eaf62fffSJeremy L Thompson     }
9792b730f8bSJeremy L Thompson   }
980eaf62fffSJeremy L Thompson   // -- Laplacian
9812b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
982eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
983eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
9841c66c397SJeremy L Thompson 
9852b730f8bSJeremy 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];
986eaf62fffSJeremy L Thompson       laplace[i + j * P_1d] = sum;
987eaf62fffSJeremy L Thompson     }
9882b730f8bSJeremy L Thompson   }
989eaf62fffSJeremy L Thompson   CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4;
9902b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation;
991eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
992eaf62fffSJeremy L Thompson }
9932c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
994eaf62fffSJeremy L Thompson 
995eaf62fffSJeremy L Thompson /// @}
996eaf62fffSJeremy L Thompson 
997eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
998480fae85SJeremy L Thompson /// CeedOperator Backend API
999480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1000480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend
1001480fae85SJeremy L Thompson /// @{
1002480fae85SJeremy L Thompson 
1003480fae85SJeremy L Thompson /**
1004506b1a0cSSebastian Grimberg   @brief Create point block restriction for active operator field
1005506b1a0cSSebastian Grimberg 
1006506b1a0cSSebastian Grimberg   @param[in]  rstr             Original CeedElemRestriction for active field
1007506b1a0cSSebastian Grimberg   @param[out] point_block_rstr Address of the variable where the newly created CeedElemRestriction will be stored
1008506b1a0cSSebastian Grimberg 
1009506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1010506b1a0cSSebastian Grimberg 
1011506b1a0cSSebastian Grimberg   @ref Backend
1012506b1a0cSSebastian Grimberg **/
1013506b1a0cSSebastian Grimberg int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *point_block_rstr) {
1014506b1a0cSSebastian Grimberg   Ceed           ceed;
1015506b1a0cSSebastian Grimberg   CeedInt        num_elem, num_comp, shift, elem_size, comp_stride, *point_block_offsets;
1016506b1a0cSSebastian Grimberg   CeedSize       l_size;
1017506b1a0cSSebastian Grimberg   const CeedInt *offsets;
1018506b1a0cSSebastian Grimberg 
1019506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
1020506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
1021506b1a0cSSebastian Grimberg 
1022506b1a0cSSebastian Grimberg   // Expand offsets
1023506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
1024506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1025506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
1026506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
1027506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
1028506b1a0cSSebastian Grimberg   shift = num_comp;
1029506b1a0cSSebastian Grimberg   if (comp_stride != 1) shift *= num_comp;
1030506b1a0cSSebastian Grimberg   CeedCall(CeedCalloc(num_elem * elem_size, &point_block_offsets));
1031506b1a0cSSebastian Grimberg   for (CeedInt i = 0; i < num_elem * elem_size; i++) {
1032506b1a0cSSebastian Grimberg     point_block_offsets[i] = offsets[i] * shift;
1033506b1a0cSSebastian Grimberg   }
1034506b1a0cSSebastian Grimberg 
1035506b1a0cSSebastian Grimberg   // Create new restriction
1036506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
1037506b1a0cSSebastian Grimberg                                      point_block_offsets, point_block_rstr));
1038506b1a0cSSebastian Grimberg 
1039506b1a0cSSebastian Grimberg   // Cleanup
1040506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
1041506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1042506b1a0cSSebastian Grimberg }
1043506b1a0cSSebastian Grimberg 
1044506b1a0cSSebastian Grimberg /**
1045480fae85SJeremy L Thompson   @brief Create object holding CeedQFunction assembly data for CeedOperator
1046480fae85SJeremy L Thompson 
1047480fae85SJeremy L Thompson   @param[in]  ceed A Ceed object where the CeedQFunctionAssemblyData will be created
1048ea61e9acSJeremy L Thompson   @param[out] data Address of the variable where the newly created CeedQFunctionAssemblyData will be stored
1049480fae85SJeremy L Thompson 
1050480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1051480fae85SJeremy L Thompson 
1052480fae85SJeremy L Thompson   @ref Backend
1053480fae85SJeremy L Thompson **/
1054ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) {
10552b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1056480fae85SJeremy L Thompson   (*data)->ref_count = 1;
1057480fae85SJeremy L Thompson   (*data)->ceed      = ceed;
10582b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1059480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1060480fae85SJeremy L Thompson }
1061480fae85SJeremy L Thompson 
1062480fae85SJeremy L Thompson /**
1063480fae85SJeremy L Thompson   @brief Increment the reference counter for a CeedQFunctionAssemblyData
1064480fae85SJeremy L Thompson 
1065ea61e9acSJeremy L Thompson   @param[in,out] data CeedQFunctionAssemblyData to increment the reference counter
1066480fae85SJeremy L Thompson 
1067480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1068480fae85SJeremy L Thompson 
1069480fae85SJeremy L Thompson   @ref Backend
1070480fae85SJeremy L Thompson **/
1071480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) {
1072480fae85SJeremy L Thompson   data->ref_count++;
1073480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1074480fae85SJeremy L Thompson }
1075480fae85SJeremy L Thompson 
1076480fae85SJeremy L Thompson /**
1077beecbf24SJeremy L Thompson   @brief Set re-use of CeedQFunctionAssemblyData
10788b919e6bSJeremy L Thompson 
1079ea61e9acSJeremy L Thompson   @param[in,out] data       CeedQFunctionAssemblyData to mark for reuse
1080ea61e9acSJeremy L Thompson   @param[in]     reuse_data Boolean flag indicating data re-use
10818b919e6bSJeremy L Thompson 
10828b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
10838b919e6bSJeremy L Thompson 
10848b919e6bSJeremy L Thompson   @ref Backend
10858b919e6bSJeremy L Thompson **/
10862b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) {
1087beecbf24SJeremy L Thompson   data->reuse_data        = reuse_data;
1088beecbf24SJeremy L Thompson   data->needs_data_update = true;
1089beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1090beecbf24SJeremy L Thompson }
1091beecbf24SJeremy L Thompson 
1092beecbf24SJeremy L Thompson /**
1093beecbf24SJeremy L Thompson   @brief Mark QFunctionAssemblyData as stale
1094beecbf24SJeremy L Thompson 
1095ea61e9acSJeremy L Thompson   @param[in,out] data              CeedQFunctionAssemblyData to mark as stale
1096ea61e9acSJeremy L Thompson   @param[in]     needs_data_update Boolean flag indicating if update is needed or completed
1097beecbf24SJeremy L Thompson 
1098beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1099beecbf24SJeremy L Thompson 
1100beecbf24SJeremy L Thompson   @ref Backend
1101beecbf24SJeremy L Thompson **/
11022b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) {
1103beecbf24SJeremy L Thompson   data->needs_data_update = needs_data_update;
11048b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
11058b919e6bSJeremy L Thompson }
11068b919e6bSJeremy L Thompson 
11078b919e6bSJeremy L Thompson /**
11088b919e6bSJeremy L Thompson   @brief Determine if QFunctionAssemblyData needs update
11098b919e6bSJeremy L Thompson 
11108b919e6bSJeremy L Thompson   @param[in]  data             CeedQFunctionAssemblyData to mark as stale
11118b919e6bSJeremy L Thompson   @param[out] is_update_needed Boolean flag indicating if re-assembly is required
11128b919e6bSJeremy L Thompson 
11138b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
11148b919e6bSJeremy L Thompson 
11158b919e6bSJeremy L Thompson   @ref Backend
11168b919e6bSJeremy L Thompson **/
11172b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) {
1118beecbf24SJeremy L Thompson   *is_update_needed = !data->reuse_data || data->needs_data_update;
11198b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
11208b919e6bSJeremy L Thompson }
11218b919e6bSJeremy L Thompson 
11228b919e6bSJeremy L Thompson /**
1123ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedQFunctionAssemblyData.
11244385fb7fSSebastian Grimberg 
1125ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedCeedQFunctionAssemblyDataDestroy()`.
1126512bb800SJeremy L Thompson 
1127512bb800SJeremy 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
1128512bb800SJeremy L Thompson         CeedQFunctionAssemblyData. This CeedQFunctionAssemblyData will be destroyed if `data_copy` is the only reference to this
1129512bb800SJeremy L Thompson         CeedQFunctionAssemblyData.
1130480fae85SJeremy L Thompson 
1131ea61e9acSJeremy L Thompson   @param[in]     data      CeedQFunctionAssemblyData to copy reference to
1132ea61e9acSJeremy L Thompson   @param[in,out] data_copy Variable to store copied reference
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 **/
11382b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) {
11392b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReference(data));
11402b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy));
1141480fae85SJeremy L Thompson   *data_copy = data;
1142480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1143480fae85SJeremy L Thompson }
1144480fae85SJeremy L Thompson 
1145480fae85SJeremy L Thompson /**
1146480fae85SJeremy L Thompson   @brief Get setup status for internal objects for CeedQFunctionAssemblyData
1147480fae85SJeremy L Thompson 
1148ea61e9acSJeremy L Thompson   @param[in]  data     CeedQFunctionAssemblyData to retrieve status
1149480fae85SJeremy L Thompson   @param[out] is_setup Boolean flag for setup status
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 **/
11552b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) {
1156480fae85SJeremy L Thompson   *is_setup = data->is_setup;
1157480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1158480fae85SJeremy L Thompson }
1159480fae85SJeremy L Thompson 
1160480fae85SJeremy L Thompson /**
1161480fae85SJeremy L Thompson   @brief Set internal objects for CeedQFunctionAssemblyData
1162480fae85SJeremy L Thompson 
1163ea61e9acSJeremy L Thompson   @param[in,out] data CeedQFunctionAssemblyData to set objects
1164480fae85SJeremy L Thompson   @param[in]     vec  CeedVector to store assembled CeedQFunction at quadrature points
1165480fae85SJeremy L Thompson   @param[in]     rstr CeedElemRestriction for CeedVector containing assembled CeedQFunction
1166480fae85SJeremy L Thompson 
1167480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1168480fae85SJeremy L Thompson 
1169480fae85SJeremy L Thompson   @ref Backend
1170480fae85SJeremy L Thompson **/
11712b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) {
11722b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(vec, &data->vec));
11732b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr));
1174480fae85SJeremy L Thompson 
1175480fae85SJeremy L Thompson   data->is_setup = true;
1176480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1177480fae85SJeremy L Thompson }
1178480fae85SJeremy L Thompson 
11794dd1a9d2SSebastian Grimberg /**
11804dd1a9d2SSebastian Grimberg   @brief Get internal objects for CeedQFunctionAssemblyData
11814dd1a9d2SSebastian Grimberg 
11824dd1a9d2SSebastian Grimberg   @param[in,out] data CeedQFunctionAssemblyData to set objects
11834dd1a9d2SSebastian Grimberg   @param[out]    vec  CeedVector to store assembled CeedQFunction at quadrature points
11844dd1a9d2SSebastian Grimberg   @param[out]    rstr CeedElemRestriction for CeedVector containing assembled CeedQFunction
11854dd1a9d2SSebastian Grimberg 
11864dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
11874dd1a9d2SSebastian Grimberg 
11884dd1a9d2SSebastian Grimberg   @ref Backend
11894dd1a9d2SSebastian Grimberg **/
11902b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) {
11916574a04fSJeremy L Thompson   CeedCheck(data->is_setup, data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first.");
1192480fae85SJeremy L Thompson 
11932b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(data->vec, vec));
11942b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr));
1195480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1196480fae85SJeremy L Thompson }
1197480fae85SJeremy L Thompson 
1198480fae85SJeremy L Thompson /**
1199480fae85SJeremy L Thompson   @brief Destroy CeedQFunctionAssemblyData
1200480fae85SJeremy L Thompson 
1201ea61e9acSJeremy L Thompson   @param[in,out] data  CeedQFunctionAssemblyData to destroy
1202480fae85SJeremy L Thompson 
1203480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1204480fae85SJeremy L Thompson 
1205480fae85SJeremy L Thompson   @ref Backend
1206480fae85SJeremy L Thompson **/
1207480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) {
1208ad6481ceSJeremy L Thompson   if (!*data || --(*data)->ref_count > 0) {
1209ad6481ceSJeremy L Thompson     *data = NULL;
1210ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1211ad6481ceSJeremy L Thompson   }
12122b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
12132b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*data)->vec));
12142b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr));
1215480fae85SJeremy L Thompson 
12162b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1217480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1218480fae85SJeremy L Thompson }
1219480fae85SJeremy L Thompson 
1220ed9e99e6SJeremy L Thompson /**
1221ed9e99e6SJeremy L Thompson   @brief Get CeedOperatorAssemblyData
1222ed9e99e6SJeremy L Thompson 
1223ed9e99e6SJeremy L Thompson   @param[in]  op   CeedOperator to assemble
1224ed9e99e6SJeremy L Thompson   @param[out] data CeedQFunctionAssemblyData
1225ed9e99e6SJeremy L Thompson 
1226ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1227ed9e99e6SJeremy L Thompson 
1228ed9e99e6SJeremy L Thompson   @ref Backend
1229ed9e99e6SJeremy L Thompson **/
12302b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) {
1231ed9e99e6SJeremy L Thompson   if (!op->op_assembled) {
1232ed9e99e6SJeremy L Thompson     CeedOperatorAssemblyData data;
1233ed9e99e6SJeremy L Thompson 
12342b730f8bSJeremy L Thompson     CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data));
1235ed9e99e6SJeremy L Thompson     op->op_assembled = data;
1236ed9e99e6SJeremy L Thompson   }
1237ed9e99e6SJeremy L Thompson   *data = op->op_assembled;
1238ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1239ed9e99e6SJeremy L Thompson }
1240ed9e99e6SJeremy L Thompson 
1241ed9e99e6SJeremy L Thompson /**
1242ba746a46SJeremy L Thompson   @brief Create object holding CeedOperator assembly data.
1243ba746a46SJeremy L Thompson 
1244ba746a46SJeremy L Thompson   The CeedOperatorAssemblyData holds an array with references to every active CeedBasis used in the CeedOperator.
1245ba746a46SJeremy L Thompson   An array with references to the corresponding active CeedElemRestrictions is also stored.
1246ba746a46SJeremy L Thompson   For each active CeedBasis, the CeedOperatorAssemblyData holds an array of all input and output CeedEvalModes for this CeedBasis.
1247ba746a46SJeremy L Thompson   The CeedOperatorAssemblyData holds an array of offsets for indexing into the assembled CeedQFunction arrays to the row representing each
1248ba746a46SJeremy L Thompson CeedEvalMode.
1249ba746a46SJeremy L Thompson   The number of input columns across all active bases for the assembled CeedQFunction is also stored.
1250ba746a46SJeremy L Thompson   Lastly, the CeedOperatorAssembly data holds assembled matrices representing the full action of the CeedBasis for all CeedEvalModes.
1251ed9e99e6SJeremy L Thompson 
1252ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperatorAssemblyData will be created
1253ed9e99e6SJeremy L Thompson   @param[in]  op   CeedOperator to be assembled
1254ea61e9acSJeremy L Thompson   @param[out] data Address of the variable where the newly created CeedOperatorAssemblyData will be stored
1255ed9e99e6SJeremy L Thompson 
1256ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1257ed9e99e6SJeremy L Thompson 
1258ed9e99e6SJeremy L Thompson   @ref Backend
1259ed9e99e6SJeremy L Thompson **/
12602b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) {
1261506b1a0cSSebastian Grimberg   CeedInt             num_active_bases_in = 0, num_active_bases_out = 0, offset = 0;
1262506b1a0cSSebastian Grimberg   CeedInt             num_input_fields, *num_eval_modes_in = NULL, num_output_fields, *num_eval_modes_out = NULL;
12631c66c397SJeremy L Thompson   CeedSize          **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL;
12641c66c397SJeremy L Thompson   CeedEvalMode      **eval_modes_in = NULL, **eval_modes_out = NULL;
12651c66c397SJeremy L Thompson   CeedQFunctionField *qf_fields;
12661c66c397SJeremy L Thompson   CeedQFunction       qf;
12671c66c397SJeremy L Thompson   CeedOperatorField  *op_fields;
126801f0e615SJames Wright   bool                is_composite;
126901f0e615SJames Wright 
127001f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
127101f0e615SJames Wright   CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Can only create CeedOperator assembly data for non-composite operators.");
1272437c7c90SJeremy L Thompson 
1273437c7c90SJeremy L Thompson   // Allocate
12742b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1275ed9e99e6SJeremy L Thompson   (*data)->ceed = ceed;
12762b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1277ed9e99e6SJeremy L Thompson 
1278ed9e99e6SJeremy L Thompson   // Build OperatorAssembly data
12792b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
12802b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL));
12812b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1282ed9e99e6SJeremy L Thompson 
1283ed9e99e6SJeremy L Thompson   // Determine active input basis
1284ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1285ed9e99e6SJeremy L Thompson     CeedVector vec;
12861c66c397SJeremy L Thompson 
12872b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1288ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
12897c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
12901c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
12911c66c397SJeremy L Thompson       CeedBasis    basis_in = NULL;
12921c66c397SJeremy L Thompson 
12932b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
12942b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1295352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp));
1296352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1297506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_active_bases_in; i++) {
1298506b1a0cSSebastian Grimberg         if ((*data)->active_bases_in[i] == basis_in) index = i;
1299437c7c90SJeremy L Thompson       }
1300437c7c90SJeremy L Thompson       if (index == -1) {
1301437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_in;
13021c66c397SJeremy L Thompson 
1303506b1a0cSSebastian Grimberg         index = num_active_bases_in;
1304506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_bases_in));
1305506b1a0cSSebastian Grimberg         (*data)->active_bases_in[num_active_bases_in] = NULL;
1306506b1a0cSSebastian Grimberg         CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases_in[num_active_bases_in]));
1307506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_elem_rstrs_in));
1308506b1a0cSSebastian Grimberg         (*data)->active_elem_rstrs_in[num_active_bases_in] = NULL;
1309437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in));
1310506b1a0cSSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs_in[num_active_bases_in]));
1311506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &num_eval_modes_in));
1312437c7c90SJeremy L Thompson         num_eval_modes_in[index] = 0;
1313506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_modes_in));
1314437c7c90SJeremy L Thompson         eval_modes_in[index] = NULL;
1315506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_mode_offsets_in));
1316437c7c90SJeremy L Thompson         eval_mode_offsets_in[index] = NULL;
1317506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->assembled_bases_in));
1318437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index] = NULL;
1319506b1a0cSSebastian Grimberg         num_active_bases_in++;
1320437c7c90SJeremy L Thompson       }
1321352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1322352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1323352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_modes_in[index]));
1324352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_mode_offsets_in[index]));
1325352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1326437c7c90SJeremy L Thompson           eval_modes_in[index][num_eval_modes_in[index] + d]        = eval_mode;
1327437c7c90SJeremy L Thompson           eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset;
1328352a5e7cSSebastian Grimberg           offset += num_comp;
1329ed9e99e6SJeremy L Thompson         }
1330352a5e7cSSebastian Grimberg         num_eval_modes_in[index] += q_comp;
1331ed9e99e6SJeremy L Thompson       }
1332ed9e99e6SJeremy L Thompson     }
1333ed9e99e6SJeremy L Thompson   }
1334ed9e99e6SJeremy L Thompson 
1335ed9e99e6SJeremy L Thompson   // Determine active output basis
13362b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields));
13372b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1338437c7c90SJeremy L Thompson   offset = 0;
1339ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1340ed9e99e6SJeremy L Thompson     CeedVector vec;
13411c66c397SJeremy L Thompson 
13422b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1343ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
13447c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
13451c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
13461c66c397SJeremy L Thompson       CeedBasis    basis_out = NULL;
13471c66c397SJeremy L Thompson 
1348437c7c90SJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
13492b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1350352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_out, &num_comp));
1351352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1352506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_active_bases_out; i++) {
1353506b1a0cSSebastian Grimberg         if ((*data)->active_bases_out[i] == basis_out) index = i;
1354437c7c90SJeremy L Thompson       }
1355437c7c90SJeremy L Thompson       if (index == -1) {
1356437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_out;
13571c66c397SJeremy L Thompson 
1358506b1a0cSSebastian Grimberg         index = num_active_bases_out;
1359506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_bases_out));
1360506b1a0cSSebastian Grimberg         (*data)->active_bases_out[num_active_bases_out] = NULL;
1361506b1a0cSSebastian Grimberg         CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases_out[num_active_bases_out]));
1362506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_elem_rstrs_out));
1363506b1a0cSSebastian Grimberg         (*data)->active_elem_rstrs_out[num_active_bases_out] = NULL;
1364437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out));
1365506b1a0cSSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs_out[num_active_bases_out]));
1366506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &num_eval_modes_out));
1367437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1368506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_modes_out));
1369437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1370506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_mode_offsets_out));
1371437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1372506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->assembled_bases_out));
1373437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1374506b1a0cSSebastian Grimberg         num_active_bases_out++;
1375437c7c90SJeremy L Thompson       }
1376352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1377352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1378352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_modes_out[index]));
1379352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_mode_offsets_out[index]));
1380352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1381437c7c90SJeremy L Thompson           eval_modes_out[index][num_eval_modes_out[index] + d]        = eval_mode;
1382437c7c90SJeremy L Thompson           eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset;
1383352a5e7cSSebastian Grimberg           offset += num_comp;
1384ed9e99e6SJeremy L Thompson         }
1385352a5e7cSSebastian Grimberg         num_eval_modes_out[index] += q_comp;
1386ed9e99e6SJeremy L Thompson       }
1387ed9e99e6SJeremy L Thompson     }
1388ed9e99e6SJeremy L Thompson   }
1389506b1a0cSSebastian Grimberg   (*data)->num_active_bases_in   = num_active_bases_in;
139027789c4aSJed Brown   (*data)->num_eval_modes_in     = num_eval_modes_in;
139127789c4aSJed Brown   (*data)->eval_modes_in         = eval_modes_in;
139227789c4aSJed Brown   (*data)->eval_mode_offsets_in  = eval_mode_offsets_in;
1393506b1a0cSSebastian Grimberg   (*data)->num_active_bases_out  = num_active_bases_out;
1394437c7c90SJeremy L Thompson   (*data)->num_eval_modes_out    = num_eval_modes_out;
1395437c7c90SJeremy L Thompson   (*data)->eval_modes_out        = eval_modes_out;
1396437c7c90SJeremy L Thompson   (*data)->eval_mode_offsets_out = eval_mode_offsets_out;
1397506b1a0cSSebastian Grimberg   (*data)->num_output_components = offset;
1398ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1399ed9e99e6SJeremy L Thompson }
1400ed9e99e6SJeremy L Thompson 
1401ed9e99e6SJeremy L Thompson /**
1402ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedEvalModes for assembly.
1403ba746a46SJeremy L Thompson 
1404ba746a46SJeremy L Thompson   Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1405ed9e99e6SJeremy L Thompson 
1406ed9e99e6SJeremy L Thompson   @param[in]  data                  CeedOperatorAssemblyData
1407506b1a0cSSebastian Grimberg   @param[out] num_active_bases_in   Total number of active bases for input
1408c5d0f995SJed Brown   @param[out] num_eval_modes_in     Pointer to hold array of numbers of input CeedEvalModes, or NULL.
1409ba746a46SJeremy L Thompson                                       `eval_modes_in[0]` holds an array of eval modes for the first active basis.
1410c5d0f995SJed Brown   @param[out] eval_modes_in         Pointer to hold arrays of input CeedEvalModes, or NULL.
1411ba746a46SJeremy L Thompson   @param[out] eval_mode_offsets_in  Pointer to hold arrays of input offsets at each quadrature point.
1412506b1a0cSSebastian Grimberg   @param[out] num_active_bases_out  Total number of active bases for output
1413c5d0f995SJed Brown   @param[out] num_eval_modes_out    Pointer to hold array of numbers of output CeedEvalModes, or NULL
1414c5d0f995SJed Brown   @param[out] eval_modes_out        Pointer to hold arrays of output CeedEvalModes, or NULL.
1415437c7c90SJeremy L Thompson   @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point
1416ba746a46SJeremy L Thompson   @param[out] num_output_components The number of columns in the assembled CeedQFunction matrix for each quadrature point,
1417ba746a46SJeremy L Thompson                                       including contributions of all active bases
1418ed9e99e6SJeremy L Thompson 
1419ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1420ed9e99e6SJeremy L Thompson 
1421ed9e99e6SJeremy L Thompson   @ref Backend
1422ed9e99e6SJeremy L Thompson **/
1423506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedInt **num_eval_modes_in,
1424506b1a0cSSebastian Grimberg                                          const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt *num_active_bases_out,
1425506b1a0cSSebastian Grimberg                                          CeedInt **num_eval_modes_out, const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out,
1426506b1a0cSSebastian Grimberg                                          CeedSize *num_output_components) {
1427506b1a0cSSebastian Grimberg   if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in;
1428437c7c90SJeremy L Thompson   if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in;
1429437c7c90SJeremy L Thompson   if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in;
1430437c7c90SJeremy L Thompson   if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in;
1431506b1a0cSSebastian Grimberg   if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out;
1432437c7c90SJeremy L Thompson   if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out;
1433437c7c90SJeremy L Thompson   if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out;
1434437c7c90SJeremy L Thompson   if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out;
1435437c7c90SJeremy L Thompson   if (num_output_components) *num_output_components = data->num_output_components;
1436ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1437ed9e99e6SJeremy L Thompson }
1438ed9e99e6SJeremy L Thompson 
1439ed9e99e6SJeremy L Thompson /**
1440ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedBasis data for assembly.
1441ba746a46SJeremy L Thompson 
1442ba746a46SJeremy L Thompson   Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1443ed9e99e6SJeremy L Thompson 
1444ed9e99e6SJeremy L Thompson   @param[in]  data                 CeedOperatorAssemblyData
1445506b1a0cSSebastian Grimberg   @param[out] num_active_bases_in  Number of active input bases, or NULL
1446506b1a0cSSebastian Grimberg   @param[out] active_bases_in      Pointer to hold active input CeedBasis, or NULL
1447437c7c90SJeremy L Thompson   @param[out] assembled_bases_in   Pointer to hold assembled active input B, or NULL
1448506b1a0cSSebastian Grimberg   @param[out] num_active_bases_out Number of active output bases, or NULL
1449506b1a0cSSebastian Grimberg   @param[out] active_bases_out     Pointer to hold active output CeedBasis, or NULL
1450437c7c90SJeremy L Thompson   @param[out] assembled_bases_out  Pointer to hold assembled active output B, or NULL
1451ed9e99e6SJeremy L Thompson 
1452ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1453ed9e99e6SJeremy L Thompson 
1454ed9e99e6SJeremy L Thompson   @ref Backend
1455ed9e99e6SJeremy L Thompson **/
1456506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedBasis **active_bases_in,
1457506b1a0cSSebastian Grimberg                                      const CeedScalar ***assembled_bases_in, CeedInt *num_active_bases_out, CeedBasis **active_bases_out,
1458506b1a0cSSebastian Grimberg                                      const CeedScalar ***assembled_bases_out) {
1459ed9e99e6SJeremy L Thompson   // Assemble B_in, B_out if needed
1460437c7c90SJeremy L Thompson   if (assembled_bases_in && !data->assembled_bases_in[0]) {
1461437c7c90SJeremy L Thompson     CeedInt num_qpts;
1462437c7c90SJeremy L Thompson 
1463506b1a0cSSebastian Grimberg     if (data->active_bases_in[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[0], &num_qpts));
1464506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_in[0], &num_qpts));
1465506b1a0cSSebastian Grimberg     for (CeedInt b = 0; b < data->num_active_bases_in; b++) {
14661c66c397SJeremy L Thompson       bool        has_eval_none = false;
1467352a5e7cSSebastian Grimberg       CeedInt     num_nodes;
1468437c7c90SJeremy L Thompson       CeedScalar *B_in = NULL, *identity = NULL;
1469ed9e99e6SJeremy L Thompson 
1470506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[b], &num_nodes));
1471352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_in[b], &B_in));
1472ed9e99e6SJeremy L Thompson 
1473437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) {
1474437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE);
1475ed9e99e6SJeremy L Thompson       }
1476ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1477352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1478352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1479352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1480ed9e99e6SJeremy L Thompson         }
1481ed9e99e6SJeremy L Thompson       }
1482ed9e99e6SJeremy L Thompson 
1483ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1484352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1485352a5e7cSSebastian Grimberg           CeedInt      d_in              = 0, q_comp_in;
1486352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE;
14871c66c397SJeremy L Thompson 
1488437c7c90SJeremy L Thompson           for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) {
1489437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_in[b] * q;
1490437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
14911c66c397SJeremy L Thompson 
1492506b1a0cSSebastian Grimberg             CeedCall(CeedOperatorGetBasisPointer(data->active_bases_in[b], data->eval_modes_in[b][e_in], identity, &B));
1493506b1a0cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_in[b], data->eval_modes_in[b][e_in], &q_comp_in));
1494352a5e7cSSebastian Grimberg             if (q_comp_in > 1) {
1495352a5e7cSSebastian Grimberg               if (e_in == 0 || data->eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0;
1496352a5e7cSSebastian Grimberg               else B = &B[(++d_in) * num_qpts * num_nodes];
1497352a5e7cSSebastian Grimberg             }
1498352a5e7cSSebastian Grimberg             eval_mode_in_prev                 = data->eval_modes_in[b][e_in];
1499352a5e7cSSebastian Grimberg             B_in[(qq + e_in) * num_nodes + n] = B[q * num_nodes + n];
1500ed9e99e6SJeremy L Thompson           }
1501ed9e99e6SJeremy L Thompson         }
1502ed9e99e6SJeremy L Thompson       }
15037c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1504437c7c90SJeremy L Thompson       data->assembled_bases_in[b] = B_in;
1505437c7c90SJeremy L Thompson     }
1506ed9e99e6SJeremy L Thompson   }
1507ed9e99e6SJeremy L Thompson 
1508437c7c90SJeremy L Thompson   if (assembled_bases_out && !data->assembled_bases_out[0]) {
1509437c7c90SJeremy L Thompson     CeedInt num_qpts;
1510437c7c90SJeremy L Thompson 
1511506b1a0cSSebastian Grimberg     if (data->active_bases_out[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[0], &num_qpts));
1512506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_out[0], &num_qpts));
1513506b1a0cSSebastian Grimberg     for (CeedInt b = 0; b < data->num_active_bases_out; b++) {
1514ed9e99e6SJeremy L Thompson       bool        has_eval_none = false;
15151c66c397SJeremy L Thompson       CeedInt     num_nodes;
1516437c7c90SJeremy L Thompson       CeedScalar *B_out = NULL, *identity = NULL;
1517ed9e99e6SJeremy L Thompson 
1518506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[b], &num_nodes));
1519352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_out[b], &B_out));
1520ed9e99e6SJeremy L Thompson 
1521437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) {
1522437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE);
1523ed9e99e6SJeremy L Thompson       }
1524ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1525352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1526352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1527352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1528ed9e99e6SJeremy L Thompson         }
1529ed9e99e6SJeremy L Thompson       }
1530ed9e99e6SJeremy L Thompson 
1531ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1532352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1533352a5e7cSSebastian Grimberg           CeedInt      d_out              = 0, q_comp_out;
1534352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
15351c66c397SJeremy L Thompson 
1536437c7c90SJeremy L Thompson           for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) {
1537437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_out[b] * q;
1538437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
15391c66c397SJeremy L Thompson 
1540506b1a0cSSebastian Grimberg             CeedCall(CeedOperatorGetBasisPointer(data->active_bases_out[b], data->eval_modes_out[b][e_out], identity, &B));
1541506b1a0cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_out[b], data->eval_modes_out[b][e_out], &q_comp_out));
1542352a5e7cSSebastian Grimberg             if (q_comp_out > 1) {
1543352a5e7cSSebastian Grimberg               if (e_out == 0 || data->eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0;
1544352a5e7cSSebastian Grimberg               else B = &B[(++d_out) * num_qpts * num_nodes];
1545352a5e7cSSebastian Grimberg             }
1546352a5e7cSSebastian Grimberg             eval_mode_out_prev                  = data->eval_modes_out[b][e_out];
1547352a5e7cSSebastian Grimberg             B_out[(qq + e_out) * num_nodes + n] = B[q * num_nodes + n];
1548ed9e99e6SJeremy L Thompson           }
1549ed9e99e6SJeremy L Thompson         }
1550ed9e99e6SJeremy L Thompson       }
15517c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1552437c7c90SJeremy L Thompson       data->assembled_bases_out[b] = B_out;
1553437c7c90SJeremy L Thompson     }
1554ed9e99e6SJeremy L Thompson   }
1555ed9e99e6SJeremy L Thompson 
1556437c7c90SJeremy L Thompson   // Pass out assembled data
1557506b1a0cSSebastian Grimberg   if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in;
1558506b1a0cSSebastian Grimberg   if (active_bases_in) *active_bases_in = data->active_bases_in;
1559437c7c90SJeremy L Thompson   if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in;
1560506b1a0cSSebastian Grimberg   if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out;
1561506b1a0cSSebastian Grimberg   if (active_bases_out) *active_bases_out = data->active_bases_out;
1562437c7c90SJeremy L Thompson   if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out;
1563437c7c90SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1564437c7c90SJeremy L Thompson }
1565437c7c90SJeremy L Thompson 
1566437c7c90SJeremy L Thompson /**
1567ba746a46SJeremy L Thompson   @brief Get CeedOperator CeedBasis data for assembly.
1568ba746a46SJeremy L Thompson 
1569ba746a46SJeremy L Thompson   Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object.
1570437c7c90SJeremy L Thompson 
1571437c7c90SJeremy L Thompson   @param[in]  data                      CeedOperatorAssemblyData
1572506b1a0cSSebastian Grimberg   @param[out] num_active_elem_rstrs_in  Number of active input element restrictions, or NULL
1573506b1a0cSSebastian Grimberg   @param[out] active_elem_rstrs_in      Pointer to hold active input CeedElemRestrictions, or NULL
1574506b1a0cSSebastian Grimberg   @param[out] num_active_elem_rstrs_out Number of active output element restrictions, or NULL
1575506b1a0cSSebastian Grimberg   @param[out] active_elem_rstrs_out     Pointer to hold active output CeedElemRestrictions, or NULL
1576437c7c90SJeremy L Thompson 
1577437c7c90SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1578437c7c90SJeremy L Thompson 
1579437c7c90SJeremy L Thompson   @ref Backend
1580437c7c90SJeremy L Thompson **/
1581506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs_in,
1582506b1a0cSSebastian Grimberg                                                 CeedElemRestriction **active_elem_rstrs_in, CeedInt *num_active_elem_rstrs_out,
1583506b1a0cSSebastian Grimberg                                                 CeedElemRestriction **active_elem_rstrs_out) {
1584506b1a0cSSebastian Grimberg   if (num_active_elem_rstrs_in) *num_active_elem_rstrs_in = data->num_active_bases_in;
1585506b1a0cSSebastian Grimberg   if (active_elem_rstrs_in) *active_elem_rstrs_in = data->active_elem_rstrs_in;
1586506b1a0cSSebastian Grimberg   if (num_active_elem_rstrs_out) *num_active_elem_rstrs_out = data->num_active_bases_out;
1587506b1a0cSSebastian Grimberg   if (active_elem_rstrs_out) *active_elem_rstrs_out = data->active_elem_rstrs_out;
1588ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1589ed9e99e6SJeremy L Thompson }
1590ed9e99e6SJeremy L Thompson 
1591ed9e99e6SJeremy L Thompson /**
1592ed9e99e6SJeremy L Thompson   @brief Destroy CeedOperatorAssemblyData
1593ed9e99e6SJeremy L Thompson 
1594ea61e9acSJeremy L Thompson   @param[in,out] data CeedOperatorAssemblyData to destroy
1595ed9e99e6SJeremy L Thompson 
1596ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1597ed9e99e6SJeremy L Thompson 
1598ed9e99e6SJeremy L Thompson   @ref Backend
1599ed9e99e6SJeremy L Thompson **/
1600ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) {
1601ad6481ceSJeremy L Thompson   if (!*data) {
1602ad6481ceSJeremy L Thompson     *data = NULL;
1603ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1604ad6481ceSJeremy L Thompson   }
16052b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
1606506b1a0cSSebastian Grimberg   for (CeedInt b = 0; b < (*data)->num_active_bases_in; b++) {
1607506b1a0cSSebastian Grimberg     CeedCall(CeedBasisDestroy(&(*data)->active_bases_in[b]));
1608506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_in[b]));
1609437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_in[b]));
1610437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b]));
1611437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_in[b]));
1612506b1a0cSSebastian Grimberg   }
1613506b1a0cSSebastian Grimberg   for (CeedInt b = 0; b < (*data)->num_active_bases_out; b++) {
1614506b1a0cSSebastian Grimberg     CeedCall(CeedBasisDestroy(&(*data)->active_bases_out[b]));
1615506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_out[b]));
1616506b1a0cSSebastian Grimberg     CeedCall(CeedFree(&(*data)->eval_modes_out[b]));
1617506b1a0cSSebastian Grimberg     CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b]));
1618437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_out[b]));
1619437c7c90SJeremy L Thompson   }
1620506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_bases_in));
1621506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_bases_out));
1622506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_elem_rstrs_in));
1623506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_elem_rstrs_out));
1624437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_in));
1625437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_out));
1626437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_in));
1627437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_out));
1628437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_in));
1629437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_out));
1630437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_in));
1631437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_out));
1632ed9e99e6SJeremy L Thompson 
16332b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1634ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1635ed9e99e6SJeremy L Thompson }
1636ed9e99e6SJeremy L Thompson 
16374dd1a9d2SSebastian Grimberg /**
16384dd1a9d2SSebastian Grimberg   @brief Retrieve fallback CeedOperator with a reference Ceed for advanced CeedOperator functionality
16394dd1a9d2SSebastian Grimberg 
16404dd1a9d2SSebastian Grimberg   @param[in]  op          CeedOperator to retrieve fallback for
16414dd1a9d2SSebastian Grimberg   @param[out] op_fallback Fallback CeedOperator
16424dd1a9d2SSebastian Grimberg 
16434dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
16444dd1a9d2SSebastian Grimberg 
16454dd1a9d2SSebastian Grimberg   @ref Backend
16464dd1a9d2SSebastian Grimberg **/
16474dd1a9d2SSebastian Grimberg int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) {
16484dd1a9d2SSebastian Grimberg   // Create if needed
16494dd1a9d2SSebastian Grimberg   if (!op->op_fallback) CeedCall(CeedOperatorCreateFallback(op));
16504dd1a9d2SSebastian Grimberg   if (op->op_fallback) {
16514dd1a9d2SSebastian Grimberg     bool is_debug;
16524dd1a9d2SSebastian Grimberg 
16534dd1a9d2SSebastian Grimberg     CeedCall(CeedIsDebug(op->ceed, &is_debug));
16544dd1a9d2SSebastian Grimberg     if (is_debug) {
16554dd1a9d2SSebastian Grimberg       Ceed        ceed, ceed_fallback;
16564dd1a9d2SSebastian Grimberg       const char *resource, *resource_fallback;
16574dd1a9d2SSebastian Grimberg 
16584dd1a9d2SSebastian Grimberg       CeedCall(CeedOperatorGetCeed(op, &ceed));
16594dd1a9d2SSebastian Grimberg       CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
16604dd1a9d2SSebastian Grimberg       CeedCall(CeedGetResource(ceed, &resource));
16614dd1a9d2SSebastian Grimberg       CeedCall(CeedGetResource(ceed_fallback, &resource_fallback));
16624dd1a9d2SSebastian Grimberg 
16634dd1a9d2SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n");
16644dd1a9d2SSebastian Grimberg       CeedDebug(ceed, "Falling back from %s operator at address %ld to %s operator at address %ld\n", resource, op, resource_fallback,
16654dd1a9d2SSebastian Grimberg                 op->op_fallback);
16664dd1a9d2SSebastian Grimberg     }
16674dd1a9d2SSebastian Grimberg   }
16684dd1a9d2SSebastian Grimberg   *op_fallback = op->op_fallback;
16694dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
16704dd1a9d2SSebastian Grimberg }
16714dd1a9d2SSebastian Grimberg 
16724dd1a9d2SSebastian Grimberg /**
16734dd1a9d2SSebastian Grimberg   @brief Get the parent CeedOperator for a fallback CeedOperator
16744dd1a9d2SSebastian Grimberg 
16754dd1a9d2SSebastian Grimberg   @param[in]  op     CeedOperator context
16764dd1a9d2SSebastian Grimberg   @param[out] parent Variable to store parent CeedOperator context
16774dd1a9d2SSebastian Grimberg 
16784dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
16794dd1a9d2SSebastian Grimberg 
16804dd1a9d2SSebastian Grimberg   @ref Backend
16814dd1a9d2SSebastian Grimberg **/
16824dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParent(CeedOperator op, CeedOperator *parent) {
16834dd1a9d2SSebastian Grimberg   *parent = op->op_fallback_parent ? op->op_fallback_parent : NULL;
16844dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
16854dd1a9d2SSebastian Grimberg }
16864dd1a9d2SSebastian Grimberg 
16874dd1a9d2SSebastian Grimberg /**
16884dd1a9d2SSebastian Grimberg   @brief Get the Ceed context of the parent CeedOperator for a fallback CeedOperator
16894dd1a9d2SSebastian Grimberg 
16904dd1a9d2SSebastian Grimberg   @param[in]  op     CeedOperator context
16914dd1a9d2SSebastian Grimberg   @param[out] parent Variable to store parent Ceed context
16924dd1a9d2SSebastian Grimberg 
16934dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
16944dd1a9d2SSebastian Grimberg 
16954dd1a9d2SSebastian Grimberg   @ref Backend
16964dd1a9d2SSebastian Grimberg **/
16974dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParentCeed(CeedOperator op, Ceed *parent) {
16984dd1a9d2SSebastian Grimberg   *parent = op->op_fallback_parent ? op->op_fallback_parent->ceed : op->ceed;
16994dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
17004dd1a9d2SSebastian Grimberg }
17014dd1a9d2SSebastian Grimberg 
1702480fae85SJeremy L Thompson /// @}
1703480fae85SJeremy L Thompson 
1704480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1705eaf62fffSJeremy L Thompson /// CeedOperator Public API
1706eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1707eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser
1708eaf62fffSJeremy L Thompson /// @{
1709eaf62fffSJeremy L Thompson 
1710eaf62fffSJeremy L Thompson /**
1711eaf62fffSJeremy L Thompson   @brief Assemble a linear CeedQFunction associated with a CeedOperator
1712eaf62fffSJeremy L Thompson 
1713ea61e9acSJeremy L Thompson   This returns a CeedVector containing a matrix at each quadrature point providing the action of the CeedQFunction associated with the CeedOperator.
1714859c15bbSJames Wright   The vector `assembled` is of shape `[num_elements, num_input_fields, num_output_fields, num_quad_points]` and contains column-major matrices
1715859c15bbSJames Wright representing the action of the CeedQFunction for a corresponding quadrature point on an element.
1716859c15bbSJames Wright 
17179fd66db6SSebastian Grimberg   Inputs and outputs are in the order provided by the user when adding CeedOperator fields.
17189fd66db6SSebastian Grimberg   For example, a CeedQFunction with inputs 'u' and 'gradu' and outputs 'gradv' and 'v', provided in that order, would result in an assembled QFunction
17199fd66db6SSebastian Grimberg that consists of (1 + dim) x (dim + 1) matrices at each quadrature point acting on the input [u, du_0, du_1] and producing the output [dv_0, dv_1, v].
1720eaf62fffSJeremy L Thompson 
1721ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1722f04ea552SJeremy L Thompson 
1723ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1724ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points
1725ea61e9acSJeremy L Thompson   @param[out] rstr      CeedElemRestriction for CeedVector containing assembled CeedQFunction
1726ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1727eaf62fffSJeremy L Thompson 
1728eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1729eaf62fffSJeremy L Thompson 
1730eaf62fffSJeremy L Thompson   @ref User
1731eaf62fffSJeremy L Thompson **/
17322b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
17332b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1734eaf62fffSJeremy L Thompson 
1735eaf62fffSJeremy L Thompson   if (op->LinearAssembleQFunction) {
1736d04bbc78SJeremy L Thompson     // Backend version
17372b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request));
1738eaf62fffSJeremy L Thompson   } else {
1739d04bbc78SJeremy L Thompson     // Operator fallback
1740d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1741d04bbc78SJeremy L Thompson 
17422b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
17436574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request));
17446574a04fSJeremy L Thompson     else return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction");
174570a7ffb3SJeremy L Thompson   }
1746eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1747eaf62fffSJeremy L Thompson }
174870a7ffb3SJeremy L Thompson 
174970a7ffb3SJeremy L Thompson /**
1750ea61e9acSJeremy L Thompson   @brief Assemble CeedQFunction and store result internally.
17514385fb7fSSebastian Grimberg 
1752ea61e9acSJeremy L Thompson   Return copied references of stored data to the caller.
1753ea61e9acSJeremy L Thompson   Caller is responsible for ownership and destruction of the copied references.
1754ea61e9acSJeremy L Thompson   See also @ref CeedOperatorLinearAssembleQFunction
175570a7ffb3SJeremy L Thompson 
1756c5f45aeaSJeremy 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.
1757c5f45aeaSJeremy L Thompson         These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object.
1758c5f45aeaSJeremy L Thompson 
1759ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1760ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points
1761ea61e9acSJeremy L Thompson   @param[out] rstr      CeedElemRestriction for CeedVector containing assembledCeedQFunction
1762ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
176370a7ffb3SJeremy L Thompson 
176470a7ffb3SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
176570a7ffb3SJeremy L Thompson 
176670a7ffb3SJeremy L Thompson   @ref User
176770a7ffb3SJeremy L Thompson **/
17682b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
1769b05f7e9fSJeremy L Thompson   int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *) = NULL;
1770b05f7e9fSJeremy L Thompson   CeedOperator op_assemble                                                                           = NULL;
1771bb229da9SJeremy L Thompson   CeedOperator op_fallback_parent                                                                    = NULL;
1772b05f7e9fSJeremy L Thompson 
17732b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
177470a7ffb3SJeremy L Thompson 
1775b05f7e9fSJeremy L Thompson   // Determine if fallback parent or operator has implementation
1776bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParent(op, &op_fallback_parent));
1777bb229da9SJeremy L Thompson   if (op_fallback_parent && op_fallback_parent->LinearAssembleQFunctionUpdate) {
1778b05f7e9fSJeremy L Thompson     // -- Backend version for op fallback parent is faster, if it exists
1779bb229da9SJeremy L Thompson     LinearAssembleQFunctionUpdate = op_fallback_parent->LinearAssembleQFunctionUpdate;
1780bb229da9SJeremy L Thompson     op_assemble                   = op_fallback_parent;
1781b05f7e9fSJeremy L Thompson   } else if (op->LinearAssembleQFunctionUpdate) {
1782b05f7e9fSJeremy L Thompson     // -- Backend version for op
1783b05f7e9fSJeremy L Thompson     LinearAssembleQFunctionUpdate = op->LinearAssembleQFunctionUpdate;
1784b05f7e9fSJeremy L Thompson     op_assemble                   = op;
1785b05f7e9fSJeremy L Thompson   }
1786b05f7e9fSJeremy L Thompson 
1787b05f7e9fSJeremy L Thompson   // Assemble QFunction
1788b05f7e9fSJeremy L Thompson   if (LinearAssembleQFunctionUpdate) {
1789b05f7e9fSJeremy L Thompson     // Backend or fallback parent version
1790480fae85SJeremy L Thompson     bool                qf_assembled_is_setup;
17912efa2d85SJeremy L Thompson     CeedVector          assembled_vec  = NULL;
17922efa2d85SJeremy L Thompson     CeedElemRestriction assembled_rstr = NULL;
1793480fae85SJeremy L Thompson 
17942b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataIsSetup(op->qf_assembled, &qf_assembled_is_setup));
1795480fae85SJeremy L Thompson     if (qf_assembled_is_setup) {
1796d04bbc78SJeremy L Thompson       bool update_needed;
1797d04bbc78SJeremy L Thompson 
17982b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataGetObjects(op->qf_assembled, &assembled_vec, &assembled_rstr));
17992b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(op->qf_assembled, &update_needed));
1800b05f7e9fSJeremy L Thompson       if (update_needed) CeedCall(LinearAssembleQFunctionUpdate(op_assemble, assembled_vec, assembled_rstr, request));
180170a7ffb3SJeremy L Thompson     } else {
1802b05f7e9fSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunction(op_assemble, &assembled_vec, &assembled_rstr, request));
18032b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataSetObjects(op->qf_assembled, assembled_vec, assembled_rstr));
180470a7ffb3SJeremy L Thompson     }
18052b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, false));
18062efa2d85SJeremy L Thompson 
1807d04bbc78SJeremy L Thompson     // Copy reference from internally held copy
18082b730f8bSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled));
18092b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr));
1810c5f45aeaSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled_vec));
18112b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&assembled_rstr));
181270a7ffb3SJeremy L Thompson   } else {
1813d04bbc78SJeremy L Thompson     // Operator fallback
1814d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1815d04bbc78SJeremy L Thompson 
18162b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
18176574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request));
18186574a04fSJeremy L Thompson     else return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate");
181970a7ffb3SJeremy L Thompson   }
182070a7ffb3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1821eaf62fffSJeremy L Thompson }
1822eaf62fffSJeremy L Thompson 
1823eaf62fffSJeremy L Thompson /**
1824eaf62fffSJeremy L Thompson   @brief Assemble the diagonal of a square linear CeedOperator
1825eaf62fffSJeremy L Thompson 
1826eaf62fffSJeremy L Thompson   This overwrites a CeedVector with the diagonal of a linear CeedOperator.
1827eaf62fffSJeremy L Thompson 
1828ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1829eaf62fffSJeremy L Thompson 
1830ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1831f04ea552SJeremy L Thompson 
1832ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1833eaf62fffSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator diagonal
1834ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1835eaf62fffSJeremy L Thompson 
1836eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1837eaf62fffSJeremy L Thompson 
1838eaf62fffSJeremy L Thompson   @ref User
1839eaf62fffSJeremy L Thompson **/
18402b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1841f3d47e36SJeremy L Thompson   bool     is_composite;
18421c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
18431c66c397SJeremy L Thompson 
18442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1845f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1846eaf62fffSJeremy L Thompson 
18472b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
18486574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1849c9366a6bSJeremy L Thompson 
1850f3d47e36SJeremy L Thompson   // Early exit for empty operator
1851f3d47e36SJeremy L Thompson   if (!is_composite) {
1852f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1853f3d47e36SJeremy L Thompson 
1854f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1855f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1856f3d47e36SJeremy L Thompson   }
1857f3d47e36SJeremy L Thompson 
1858eaf62fffSJeremy L Thompson   if (op->LinearAssembleDiagonal) {
1859d04bbc78SJeremy L Thompson     // Backend version
18602b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleDiagonal(op, assembled, request));
1861eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1862eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddDiagonal) {
1863d04bbc78SJeremy L Thompson     // Backend version with zeroing first
18642b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
18652b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1866eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1867eaf62fffSJeremy L Thompson   } else {
1868d04bbc78SJeremy L Thompson     // Operator fallback
1869d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1870d04bbc78SJeremy L Thompson 
18712b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1872d04bbc78SJeremy L Thompson     if (op_fallback) {
18732b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request));
1874eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1875eaf62fffSJeremy L Thompson     }
1876eaf62fffSJeremy L Thompson   }
1877eaf62fffSJeremy L Thompson   // Default interface implementation
18782b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
18792b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request));
1880eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1881eaf62fffSJeremy L Thompson }
1882eaf62fffSJeremy L Thompson 
1883eaf62fffSJeremy L Thompson /**
1884eaf62fffSJeremy L Thompson   @brief Assemble the diagonal of a square linear CeedOperator
1885eaf62fffSJeremy L Thompson 
1886eaf62fffSJeremy L Thompson   This sums into a CeedVector the diagonal of a linear CeedOperator.
1887eaf62fffSJeremy L Thompson 
1888ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
1889eaf62fffSJeremy L Thompson 
1890ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1891f04ea552SJeremy L Thompson 
1892ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
1893eaf62fffSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator diagonal
1894ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1895eaf62fffSJeremy L Thompson 
1896eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1897eaf62fffSJeremy L Thompson 
1898eaf62fffSJeremy L Thompson   @ref User
1899eaf62fffSJeremy L Thompson **/
19002b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1901f3d47e36SJeremy L Thompson   bool     is_composite;
19021c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
19031c66c397SJeremy L Thompson 
19042b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1905f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1906eaf62fffSJeremy L Thompson 
19072b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
19086574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1909c9366a6bSJeremy L Thompson 
1910f3d47e36SJeremy L Thompson   // Early exit for empty operator
1911f3d47e36SJeremy L Thompson   if (!is_composite) {
1912f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1913f3d47e36SJeremy L Thompson 
1914f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1915f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1916f3d47e36SJeremy L Thompson   }
1917f3d47e36SJeremy L Thompson 
1918eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddDiagonal) {
1919d04bbc78SJeremy L Thompson     // Backend version
19202b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1921eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1922eaf62fffSJeremy L Thompson   } else {
1923d04bbc78SJeremy L Thompson     // Operator fallback
1924d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1925d04bbc78SJeremy L Thompson 
19262b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1927d04bbc78SJeremy L Thompson     if (op_fallback) {
19282b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
1929eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1930eaf62fffSJeremy L Thompson     }
1931eaf62fffSJeremy L Thompson   }
1932eaf62fffSJeremy L Thompson   // Default interface implementation
1933eaf62fffSJeremy L Thompson   if (is_composite) {
19342b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
1935eaf62fffSJeremy L Thompson   } else {
19362b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, false, assembled));
1937eaf62fffSJeremy L Thompson   }
1938d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1939eaf62fffSJeremy L Thompson }
1940eaf62fffSJeremy L Thompson 
1941eaf62fffSJeremy L Thompson /**
194201f0e615SJames Wright    @brief Fully assemble the point-block diagonal pattern of a linear operator.
194301f0e615SJames Wright 
194401f0e615SJames Wright    Expected to be used in conjunction with CeedOperatorLinearAssemblePointBlockDiagonal().
194501f0e615SJames Wright 
194601f0e615SJames Wright    The assembly routines use coordinate format, with `num_entries` tuples of the form (i, j, value) which indicate that value should be added to the
194701f0e615SJames Wright matrix in entry (i, j).
194801f0e615SJames Wright   Note that the (i, j) pairs are unique.
194901f0e615SJames Wright   This function returns the number of entries and their (i, j) locations, while CeedOperatorLinearAssemblePointBlockDiagonal() provides the values in
195001f0e615SJames Wright the same ordering.
195101f0e615SJames Wright 
195201f0e615SJames Wright    This will generally be slow unless your operator is low-order.
195301f0e615SJames Wright 
195401f0e615SJames Wright    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
195501f0e615SJames Wright 
195601f0e615SJames Wright    @param[in]  op          CeedOperator to assemble
195701f0e615SJames Wright    @param[out] num_entries Number of entries in coordinate nonzero pattern
195801f0e615SJames Wright    @param[out] rows        Row number for each entry
195901f0e615SJames Wright    @param[out] cols        Column number for each entry
196001f0e615SJames Wright 
196101f0e615SJames Wright    @ref User
196201f0e615SJames Wright **/
196301f0e615SJames Wright int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
196401f0e615SJames Wright   Ceed          ceed;
196501f0e615SJames Wright   bool          is_composite;
196601f0e615SJames Wright   CeedInt       num_active_components, num_sub_operators;
196701f0e615SJames Wright   CeedOperator *sub_operators;
196801f0e615SJames Wright 
196901f0e615SJames Wright   CeedCall(CeedOperatorGetCeed(op, &ceed));
197001f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
197101f0e615SJames Wright 
197201f0e615SJames Wright   CeedSize input_size = 0, output_size = 0;
197301f0e615SJames Wright   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
197401f0e615SJames Wright   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
197501f0e615SJames Wright 
197601f0e615SJames Wright   if (is_composite) {
197701f0e615SJames Wright     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_operators));
197801f0e615SJames Wright     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
197901f0e615SJames Wright   } else {
198001f0e615SJames Wright     sub_operators     = &op;
198101f0e615SJames Wright     num_sub_operators = 1;
198201f0e615SJames Wright   }
198301f0e615SJames Wright 
1984506b1a0cSSebastian Grimberg   // Verify operator can be assembled correctly
1985506b1a0cSSebastian Grimberg   {
198601f0e615SJames Wright     CeedOperatorAssemblyData data;
1987506b1a0cSSebastian Grimberg     CeedInt                  num_active_elem_rstrs, comp_stride;
198801f0e615SJames Wright     CeedElemRestriction     *active_elem_rstrs;
198901f0e615SJames Wright 
199001f0e615SJames Wright     // Get initial values to check against
199101f0e615SJames Wright     CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[0], &data));
1992506b1a0cSSebastian Grimberg     CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL));
199301f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[0], &comp_stride));
199401f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[0], &num_active_components));
199501f0e615SJames Wright 
1996506b1a0cSSebastian Grimberg     // Verify that all active element restrictions have same component stride and number of components
199701f0e615SJames Wright     for (CeedInt k = 0; k < num_sub_operators; k++) {
199801f0e615SJames Wright       CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[k], &data));
1999506b1a0cSSebastian Grimberg       CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL));
200001f0e615SJames Wright       for (CeedInt i = 0; i < num_active_elem_rstrs; i++) {
2001506b1a0cSSebastian Grimberg         CeedInt comp_stride_sub, num_active_components_sub;
2002506b1a0cSSebastian Grimberg 
200301f0e615SJames Wright         CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[i], &comp_stride_sub));
200401f0e615SJames Wright         CeedCheck(comp_stride == comp_stride_sub, ceed, CEED_ERROR_DIMENSION,
200501f0e615SJames Wright                   "Active element restrictions must have the same component stride: %d vs %d", comp_stride, comp_stride_sub);
200601f0e615SJames Wright         CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[i], &num_active_components_sub));
200701f0e615SJames Wright         CeedCheck(num_active_components == num_active_components_sub, ceed, CEED_ERROR_INCOMPATIBLE,
200801f0e615SJames Wright                   "All suboperators must have the same number of output components");
200901f0e615SJames Wright       }
201001f0e615SJames Wright     }
201101f0e615SJames Wright   }
201201f0e615SJames Wright   *num_entries = input_size * num_active_components;
201301f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, rows));
201401f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, cols));
201501f0e615SJames Wright 
201601f0e615SJames Wright   for (CeedInt o = 0; o < num_sub_operators; o++) {
2017506b1a0cSSebastian Grimberg     CeedElemRestriction active_elem_rstr, point_block_active_elem_rstr;
201801f0e615SJames Wright     CeedInt             comp_stride, num_elem, elem_size;
2019506b1a0cSSebastian Grimberg     const CeedInt      *offsets, *point_block_offsets;
202001f0e615SJames Wright 
202101f0e615SJames Wright     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[o], &active_elem_rstr));
202201f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstr, &comp_stride));
202301f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumElements(active_elem_rstr, &num_elem));
202401f0e615SJames Wright     CeedCall(CeedElemRestrictionGetElementSize(active_elem_rstr, &elem_size));
202501f0e615SJames Wright     CeedCall(CeedElemRestrictionGetOffsets(active_elem_rstr, CEED_MEM_HOST, &offsets));
202601f0e615SJames Wright 
2027506b1a0cSSebastian Grimberg     CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstr, &point_block_active_elem_rstr));
2028506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOffsets(point_block_active_elem_rstr, CEED_MEM_HOST, &point_block_offsets));
202901f0e615SJames Wright 
203001f0e615SJames Wright     for (CeedSize i = 0; i < num_elem * elem_size; i++) {
203101f0e615SJames Wright       for (CeedInt c_out = 0; c_out < num_active_components; c_out++) {
203201f0e615SJames Wright         for (CeedInt c_in = 0; c_in < num_active_components; c_in++) {
2033506b1a0cSSebastian Grimberg           (*rows)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_out * comp_stride;
2034506b1a0cSSebastian Grimberg           (*cols)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_in * comp_stride;
203501f0e615SJames Wright         }
203601f0e615SJames Wright       }
203701f0e615SJames Wright     }
203801f0e615SJames Wright 
203901f0e615SJames Wright     CeedCall(CeedElemRestrictionRestoreOffsets(active_elem_rstr, &offsets));
2040506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOffsets(point_block_active_elem_rstr, &point_block_offsets));
2041506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&point_block_active_elem_rstr));
204201f0e615SJames Wright   }
204301f0e615SJames Wright   return CEED_ERROR_SUCCESS;
204401f0e615SJames Wright }
204501f0e615SJames Wright 
204601f0e615SJames Wright /**
2047eaf62fffSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear CeedOperator
2048eaf62fffSJeremy L Thompson 
2049ea61e9acSJeremy L Thompson   This overwrites a CeedVector with the point block diagonal of a linear CeedOperator.
2050eaf62fffSJeremy L Thompson 
2051ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
2052eaf62fffSJeremy L Thompson 
2053ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2054f04ea552SJeremy L Thompson 
2055ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
2056ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator point block diagonal, provided in row-major form with an @a num_comp * @a num_comp
2057ea61e9acSJeremy L Thompson block at each node. The dimensions of this vector are derived from the active vector for the CeedOperator. The array has shape [nodes, component out,
2058ea61e9acSJeremy L Thompson component in].
2059ea61e9acSJeremy L Thompson   @param[in]  request   Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2060eaf62fffSJeremy L Thompson 
2061eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2062eaf62fffSJeremy L Thompson 
2063eaf62fffSJeremy L Thompson   @ref User
2064eaf62fffSJeremy L Thompson **/
20652b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2066f3d47e36SJeremy L Thompson   bool     is_composite;
20671c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
20681c66c397SJeremy L Thompson 
20692b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2070f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2071eaf62fffSJeremy L Thompson 
20722b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
20736574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
2074c9366a6bSJeremy L Thompson 
2075f3d47e36SJeremy L Thompson   // Early exit for empty operator
2076f3d47e36SJeremy L Thompson   if (!is_composite) {
2077f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2078f3d47e36SJeremy L Thompson 
2079f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2080f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2081f3d47e36SJeremy L Thompson   }
2082f3d47e36SJeremy L Thompson 
2083eaf62fffSJeremy L Thompson   if (op->LinearAssemblePointBlockDiagonal) {
2084d04bbc78SJeremy L Thompson     // Backend version
20852b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request));
2086eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2087eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddPointBlockDiagonal) {
2088d04bbc78SJeremy L Thompson     // Backend version with zeroing first
20892b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
20902b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
2091eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2092eaf62fffSJeremy L Thompson   } else {
2093d04bbc78SJeremy L Thompson     // Operator fallback
2094d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2095d04bbc78SJeremy L Thompson 
20962b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2097d04bbc78SJeremy L Thompson     if (op_fallback) {
20982b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request));
2099eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2100eaf62fffSJeremy L Thompson     }
2101eaf62fffSJeremy L Thompson   }
2102eaf62fffSJeremy L Thompson   // Default interface implementation
21032b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
21042b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
2105eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2106eaf62fffSJeremy L Thompson }
2107eaf62fffSJeremy L Thompson 
2108eaf62fffSJeremy L Thompson /**
2109eaf62fffSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear CeedOperator
2110eaf62fffSJeremy L Thompson 
2111ea61e9acSJeremy L Thompson   This sums into a CeedVector with the point block diagonal of a linear CeedOperator.
2112eaf62fffSJeremy L Thompson 
2113ea61e9acSJeremy L Thompson   Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported.
2114eaf62fffSJeremy L Thompson 
2115ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2116f04ea552SJeremy L Thompson 
2117ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator to assemble CeedQFunction
2118ea61e9acSJeremy L Thompson   @param[out] assembled CeedVector to store assembled CeedOperator point block diagonal, provided in row-major form with an @a num_comp * @a num_comp
2119ea61e9acSJeremy L Thompson block at each node. The dimensions of this vector are derived from the active vector for the CeedOperator. The array has shape [nodes, component out,
2120ea61e9acSJeremy L Thompson component in].
2121ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2122eaf62fffSJeremy L Thompson 
2123eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2124eaf62fffSJeremy L Thompson 
2125eaf62fffSJeremy L Thompson   @ref User
2126eaf62fffSJeremy L Thompson **/
21272b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2128f3d47e36SJeremy L Thompson   bool     is_composite;
21291c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
21301c66c397SJeremy L Thompson 
21312b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2132f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2133eaf62fffSJeremy L Thompson 
21342b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
21356574a04fSJeremy L Thompson   CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square");
2136c9366a6bSJeremy L Thompson 
2137f3d47e36SJeremy L Thompson   // Early exit for empty operator
2138f3d47e36SJeremy L Thompson   if (!is_composite) {
2139f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2140f3d47e36SJeremy L Thompson 
2141f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2142f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2143f3d47e36SJeremy L Thompson   }
2144f3d47e36SJeremy L Thompson 
2145eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddPointBlockDiagonal) {
2146d04bbc78SJeremy L Thompson     // Backend version
21472b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request));
2148eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2149eaf62fffSJeremy L Thompson   } else {
2150d04bbc78SJeremy L Thompson     // Operator fallback
2151d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2152d04bbc78SJeremy L Thompson 
21532b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2154d04bbc78SJeremy L Thompson     if (op_fallback) {
21552b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request));
2156eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2157eaf62fffSJeremy L Thompson     }
2158eaf62fffSJeremy L Thompson   }
2159ea61e9acSJeremy L Thompson   // Default interface implementation
2160eaf62fffSJeremy L Thompson   if (is_composite) {
21612b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
2162eaf62fffSJeremy L Thompson   } else {
21632b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, true, assembled));
2164eaf62fffSJeremy L Thompson   }
2165d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2166eaf62fffSJeremy L Thompson }
2167eaf62fffSJeremy L Thompson 
2168eaf62fffSJeremy L Thompson /**
2169eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero pattern of a linear operator.
2170eaf62fffSJeremy L Thompson 
2171ea61e9acSJeremy L Thompson    Expected to be used in conjunction with CeedOperatorLinearAssemble().
2172eaf62fffSJeremy L Thompson 
2173ea61e9acSJeremy 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
21749fd66db6SSebastian Grimberg matrix in entry (i, j).
21759fd66db6SSebastian Grimberg   Note that the (i, j) pairs are not unique and may repeat.
21769fd66db6SSebastian Grimberg   This function returns the number of entries and their (i, j) locations, while CeedOperatorLinearAssemble() provides the values in the same ordering.
2177eaf62fffSJeremy L Thompson 
2178eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2179eaf62fffSJeremy L Thompson 
2180ea61e9acSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2181f04ea552SJeremy L Thompson 
2182eaf62fffSJeremy L Thompson    @param[in]  op          CeedOperator to assemble
2183eaf62fffSJeremy L Thompson    @param[out] num_entries Number of entries in coordinate nonzero pattern
2184eaf62fffSJeremy L Thompson    @param[out] rows        Row number for each entry
2185eaf62fffSJeremy L Thompson    @param[out] cols        Column number for each entry
2186eaf62fffSJeremy L Thompson 
2187eaf62fffSJeremy L Thompson    @ref User
2188eaf62fffSJeremy L Thompson **/
21892b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
21901c66c397SJeremy L Thompson   bool          is_composite;
21911c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2192b94338b9SJed Brown   CeedSize      single_entries;
2193eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
21941c66c397SJeremy L Thompson 
21952b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2196f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2197eaf62fffSJeremy L Thompson 
2198eaf62fffSJeremy L Thompson   if (op->LinearAssembleSymbolic) {
2199d04bbc78SJeremy L Thompson     // Backend version
22002b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols));
2201eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2202eaf62fffSJeremy L Thompson   } else {
2203d04bbc78SJeremy L Thompson     // Operator fallback
2204d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2205d04bbc78SJeremy L Thompson 
22062b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2207d04bbc78SJeremy L Thompson     if (op_fallback) {
22082b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols));
2209eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2210eaf62fffSJeremy L Thompson     }
2211eaf62fffSJeremy L Thompson   }
2212eaf62fffSJeremy L Thompson 
2213eaf62fffSJeremy L Thompson   // Default interface implementation
2214eaf62fffSJeremy L Thompson 
2215506b1a0cSSebastian Grimberg   // Count entries and allocate rows, cols arrays
2216eaf62fffSJeremy L Thompson   *num_entries = 0;
2217eaf62fffSJeremy L Thompson   if (is_composite) {
2218c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2219c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
222092ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
22212b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2222eaf62fffSJeremy L Thompson       *num_entries += single_entries;
2223eaf62fffSJeremy L Thompson     }
2224eaf62fffSJeremy L Thompson   } else {
22252b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries));
2226eaf62fffSJeremy L Thompson     *num_entries += single_entries;
2227eaf62fffSJeremy L Thompson   }
22282b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, rows));
22292b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, cols));
2230eaf62fffSJeremy L Thompson 
2231506b1a0cSSebastian Grimberg   // Assemble nonzero locations
2232eaf62fffSJeremy L Thompson   if (is_composite) {
2233c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2234c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
223592ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
22362b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols));
22372b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2238eaf62fffSJeremy L Thompson       offset += single_entries;
2239eaf62fffSJeremy L Thompson     }
2240eaf62fffSJeremy L Thompson   } else {
22412b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols));
2242eaf62fffSJeremy L Thompson   }
2243eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2244eaf62fffSJeremy L Thompson }
2245eaf62fffSJeremy L Thompson 
2246eaf62fffSJeremy L Thompson /**
2247eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero entries of a linear operator.
2248eaf62fffSJeremy L Thompson 
2249ea61e9acSJeremy L Thompson    Expected to be used in conjunction with CeedOperatorLinearAssembleSymbolic().
2250eaf62fffSJeremy L Thompson 
2251ea61e9acSJeremy 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
22529fd66db6SSebastian Grimberg matrix in entry (i, j).
22539fd66db6SSebastian Grimberg   Note that the (i, j) pairs are not unique and may repeat.
22549fd66db6SSebastian Grimberg   This function returns the values of the nonzero entries to be added, their (i, j) locations are provided by CeedOperatorLinearAssembleSymbolic()
2255eaf62fffSJeremy L Thompson 
2256eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2257eaf62fffSJeremy L Thompson 
2258ea61e9acSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2259f04ea552SJeremy L Thompson 
2260eaf62fffSJeremy L Thompson    @param[in]  op     CeedOperator to assemble
2261eaf62fffSJeremy L Thompson    @param[out] values Values to assemble into matrix
2262eaf62fffSJeremy L Thompson 
2263eaf62fffSJeremy L Thompson    @ref User
2264eaf62fffSJeremy L Thompson **/
2265eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) {
22661c66c397SJeremy L Thompson   bool          is_composite;
22671c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2268b94338b9SJed Brown   CeedSize      single_entries = 0;
2269eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
22701c66c397SJeremy L Thompson 
22712b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2272f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2273f3d47e36SJeremy L Thompson 
2274f3d47e36SJeremy L Thompson   // Early exit for empty operator
2275f3d47e36SJeremy L Thompson   if (!is_composite) {
2276f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2277f3d47e36SJeremy L Thompson 
2278f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2279f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2280f3d47e36SJeremy L Thompson   }
2281eaf62fffSJeremy L Thompson 
2282eaf62fffSJeremy L Thompson   if (op->LinearAssemble) {
2283d04bbc78SJeremy L Thompson     // Backend version
22842b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemble(op, values));
2285eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2286eaf62fffSJeremy L Thompson   } else {
2287d04bbc78SJeremy L Thompson     // Operator fallback
2288d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2289d04bbc78SJeremy L Thompson 
22902b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2291d04bbc78SJeremy L Thompson     if (op_fallback) {
22922b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemble(op_fallback, values));
2293eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2294eaf62fffSJeremy L Thompson     }
2295eaf62fffSJeremy L Thompson   }
2296eaf62fffSJeremy L Thompson 
2297eaf62fffSJeremy L Thompson   // Default interface implementation
229828ec399dSJeremy L Thompson   CeedCall(CeedVectorSetValue(values, 0.0));
2299eaf62fffSJeremy L Thompson   if (is_composite) {
2300c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2301c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2302cefa2673SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; k++) {
23032b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values));
23042b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2305eaf62fffSJeremy L Thompson       offset += single_entries;
2306eaf62fffSJeremy L Thompson     }
2307eaf62fffSJeremy L Thompson   } else {
23082b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemble(op, offset, values));
2309eaf62fffSJeremy L Thompson   }
2310eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2311eaf62fffSJeremy L Thompson }
2312eaf62fffSJeremy L Thompson 
2313eaf62fffSJeremy L Thompson /**
231475f0d5a4SJeremy L Thompson   @brief Get the multiplicity of nodes across suboperators in a composite CeedOperator
231575f0d5a4SJeremy L Thompson 
231675f0d5a4SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
231775f0d5a4SJeremy L Thompson 
231875f0d5a4SJeremy L Thompson   @param[in]  op               Composite CeedOperator
231975f0d5a4SJeremy L Thompson   @param[in]  num_skip_indices Number of suboperators to skip
232075f0d5a4SJeremy L Thompson   @param[in]  skip_indices     Array of indices of suboperators to skip
232175f0d5a4SJeremy L Thompson   @param[out] mult             Vector to store multiplicity (of size l_size)
232275f0d5a4SJeremy L Thompson 
232375f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
232475f0d5a4SJeremy L Thompson 
232575f0d5a4SJeremy L Thompson   @ref User
232675f0d5a4SJeremy L Thompson **/
232775f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) {
232875f0d5a4SJeremy L Thompson   Ceed                ceed;
2329b275c451SJeremy L Thompson   CeedInt             num_suboperators;
233075f0d5a4SJeremy L Thompson   CeedSize            l_vec_len;
233175f0d5a4SJeremy L Thompson   CeedScalar         *mult_array;
233275f0d5a4SJeremy L Thompson   CeedVector          ones_l_vec;
23337c1dbaffSSebastian Grimberg   CeedElemRestriction elem_rstr, mult_elem_rstr;
2334b275c451SJeremy L Thompson   CeedOperator       *sub_operators;
233575f0d5a4SJeremy L Thompson 
23361c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
23371c66c397SJeremy L Thompson 
233875f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
233975f0d5a4SJeremy L Thompson 
234075f0d5a4SJeremy L Thompson   // Zero mult vector
234175f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
234275f0d5a4SJeremy L Thompson 
234375f0d5a4SJeremy L Thompson   // Get suboperators
2344b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2345b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2346b275c451SJeremy L Thompson   if (num_suboperators == 0) return CEED_ERROR_SUCCESS;
234775f0d5a4SJeremy L Thompson 
234875f0d5a4SJeremy L Thompson   // Work vector
234975f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetLength(mult, &l_vec_len));
235075f0d5a4SJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec));
235175f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(ones_l_vec, 1.0));
235275f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array));
235375f0d5a4SJeremy L Thompson 
235475f0d5a4SJeremy L Thompson   // Compute multiplicity across suboperators
2355b275c451SJeremy L Thompson   for (CeedInt i = 0; i < num_suboperators; i++) {
235675f0d5a4SJeremy L Thompson     const CeedScalar *sub_mult_array;
235775f0d5a4SJeremy L Thompson     CeedVector        sub_mult_l_vec, ones_e_vec;
235875f0d5a4SJeremy L Thompson 
235975f0d5a4SJeremy L Thompson     // -- Check for suboperator to skip
236075f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < num_skip_indices; j++) {
236175f0d5a4SJeremy L Thompson       if (skip_indices[j] == i) continue;
236275f0d5a4SJeremy L Thompson     }
236375f0d5a4SJeremy L Thompson 
236475f0d5a4SJeremy L Thompson     // -- Sub operator multiplicity
2365437c7c90SJeremy L Thompson     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr));
23667c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr, &mult_elem_rstr));
23677c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateVector(mult_elem_rstr, &sub_mult_l_vec, &ones_e_vec));
236875f0d5a4SJeremy L Thompson     CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0));
23697c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE));
23707c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE));
237175f0d5a4SJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array));
237275f0d5a4SJeremy L Thompson     // ---- Flag every node present in the current suboperator
237375f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < l_vec_len; j++) {
237475f0d5a4SJeremy L Thompson       if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0;
237575f0d5a4SJeremy L Thompson     }
237675f0d5a4SJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array));
237775f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&sub_mult_l_vec));
237875f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&ones_e_vec));
23797c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&mult_elem_rstr));
238075f0d5a4SJeremy L Thompson   }
238175f0d5a4SJeremy L Thompson   CeedCall(CeedVectorRestoreArray(mult, &mult_array));
2382811d0ccfSJeremy L Thompson   CeedCall(CeedVectorDestroy(&ones_l_vec));
238375f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
238475f0d5a4SJeremy L Thompson }
238575f0d5a4SJeremy L Thompson 
238675f0d5a4SJeremy L Thompson /**
2387ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator, creating the prolongation basis from the fine and coarse
2388ea61e9acSJeremy L Thompson grid interpolation
2389eaf62fffSJeremy L Thompson 
239058e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2391f04ea552SJeremy L Thompson 
2392eaf62fffSJeremy L Thompson   @param[in]  op_fine      Fine grid operator
239385bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2394eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid restriction
2395eaf62fffSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector basis
2396eaf62fffSJeremy L Thompson   @param[out] op_coarse    Coarse grid operator
239785bb9dcfSJeremy L Thompson   @param[out] op_prolong   Coarse to fine operator, or NULL
23987758292fSSebastian Grimberg   @param[out] op_restrict  Fine to coarse operator, or NULL
2399eaf62fffSJeremy L Thompson 
2400eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2401eaf62fffSJeremy L Thompson 
2402eaf62fffSJeremy L Thompson   @ref User
2403eaf62fffSJeremy L Thompson **/
24042b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
24057758292fSSebastian Grimberg                                      CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
24061c66c397SJeremy L Thompson   CeedBasis basis_c_to_f = NULL;
24071c66c397SJeremy L Thompson 
24082b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2409eaf62fffSJeremy L Thompson 
241083d6adf3SZach Atkins   // Build prolongation matrix, if required
24117758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
241283d6adf3SZach Atkins     CeedBasis basis_fine;
24131c66c397SJeremy L Thompson 
24142b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
24152b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f));
241683d6adf3SZach Atkins   }
2417eaf62fffSJeremy L Thompson 
2418f113e5dcSJeremy L Thompson   // Core code
24197758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2420eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2421eaf62fffSJeremy L Thompson }
2422eaf62fffSJeremy L Thompson 
2423eaf62fffSJeremy L Thompson /**
2424ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a tensor basis for the active basis
2425eaf62fffSJeremy L Thompson 
242658e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2427f04ea552SJeremy L Thompson 
2428eaf62fffSJeremy L Thompson   @param[in]  op_fine       Fine grid operator
242985bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2430eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid restriction
2431eaf62fffSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector basis
243285bb9dcfSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
2433eaf62fffSJeremy L Thompson   @param[out] op_coarse     Coarse grid operator
243485bb9dcfSJeremy L Thompson   @param[out] op_prolong    Coarse to fine operator, or NULL
24357758292fSSebastian Grimberg   @param[out] op_restrict   Fine to coarse operator, or NULL
2436eaf62fffSJeremy L Thompson 
2437eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2438eaf62fffSJeremy L Thompson 
2439eaf62fffSJeremy L Thompson   @ref User
2440eaf62fffSJeremy L Thompson **/
24412b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
24422b730f8bSJeremy L Thompson                                              const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
24437758292fSSebastian Grimberg                                              CeedOperator *op_restrict) {
2444eaf62fffSJeremy L Thompson   Ceed      ceed;
24451c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
24461c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
24471c66c397SJeremy L Thompson 
24481c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
24492b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2450eaf62fffSJeremy L Thompson 
2451eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
24522b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
24532b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
24542b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
24556574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2456eaf62fffSJeremy L Thompson 
245783d6adf3SZach Atkins   // Create coarse to fine basis, if required
24587758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
24591c66c397SJeremy L Thompson     CeedInt     dim, num_comp, num_nodes_c, P_1d_f, P_1d_c;
24601c66c397SJeremy L Thompson     CeedScalar *q_ref, *q_weight, *grad;
24611c66c397SJeremy L Thompson 
246283d6adf3SZach Atkins     // Check if interpolation matrix is provided
24636574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
24646574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
24652b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
24662b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
24672b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f));
24682b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
24692b730f8bSJeremy L Thompson     P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c);
24702b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_ref));
24712b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_weight));
24722b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad));
24732b730f8bSJeremy 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));
24742b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
24752b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
24762b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
247783d6adf3SZach Atkins   }
2478eaf62fffSJeremy L Thompson 
2479eaf62fffSJeremy L Thompson   // Core code
24807758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2481eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2482eaf62fffSJeremy L Thompson }
2483eaf62fffSJeremy L Thompson 
2484eaf62fffSJeremy L Thompson /**
2485ea61e9acSJeremy L Thompson   @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a non-tensor basis for the active vector
2486eaf62fffSJeremy L Thompson 
248758e4b056SJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable.
2488f04ea552SJeremy L Thompson 
2489eaf62fffSJeremy L Thompson   @param[in]  op_fine       Fine grid operator
249085bb9dcfSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators
2491eaf62fffSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid restriction
2492eaf62fffSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector basis
249385bb9dcfSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators
2494eaf62fffSJeremy L Thompson   @param[out] op_coarse     Coarse grid operator
249585bb9dcfSJeremy L Thompson   @param[out] op_prolong    Coarse to fine operator, or NULL
24967758292fSSebastian Grimberg   @param[out] op_restrict   Fine to coarse operator, or NULL
2497eaf62fffSJeremy L Thompson 
2498eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2499eaf62fffSJeremy L Thompson 
2500eaf62fffSJeremy L Thompson   @ref User
2501eaf62fffSJeremy L Thompson **/
25022b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
25037758292fSSebastian Grimberg                                        const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
25047758292fSSebastian Grimberg                                        CeedOperator *op_restrict) {
2505eaf62fffSJeremy L Thompson   Ceed      ceed;
25061c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
25071c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
25081c66c397SJeremy L Thompson 
25091c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
25102b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2511eaf62fffSJeremy L Thompson 
2512eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
25132b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
25142b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
25152b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
25166574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2517eaf62fffSJeremy L Thompson 
2518eaf62fffSJeremy L Thompson   // Coarse to fine basis
25197758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
25201c66c397SJeremy L Thompson     CeedInt          dim, num_comp, num_nodes_c, num_nodes_f;
25211c66c397SJeremy L Thompson     CeedScalar      *q_ref, *q_weight, *grad;
25221c66c397SJeremy L Thompson     CeedElemTopology topo;
25231c66c397SJeremy L Thompson 
252483d6adf3SZach Atkins     // Check if interpolation matrix is provided
25256574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
25266574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
25272b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_fine, &topo));
25282b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
25292b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
25302b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f));
25312b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
25322b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref));
25332b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f, &q_weight));
25342b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad));
25352b730f8bSJeremy 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));
25362b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
25372b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
25382b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
253983d6adf3SZach Atkins   }
2540eaf62fffSJeremy L Thompson 
2541eaf62fffSJeremy L Thompson   // Core code
25427758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2543eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2544eaf62fffSJeremy L Thompson }
2545eaf62fffSJeremy L Thompson 
2546eaf62fffSJeremy L Thompson /**
2547ea61e9acSJeremy L Thompson   @brief Build a FDM based approximate inverse for each element for a CeedOperator
2548eaf62fffSJeremy L Thompson 
2549ea61e9acSJeremy L Thompson   This returns a CeedOperator and CeedVector to apply a Fast Diagonalization Method based approximate inverse.
2550859c15bbSJames 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$.
2551859c15bbSJames Wright   The assembled QFunction is used to modify the eigenvalues from simultaneous diagonalization and obtain an approximate inverse of the form \f$V^T
25529fd66db6SSebastian Grimberg \hat S V\f$.
25539fd66db6SSebastian Grimberg   The CeedOperator must be linear and non-composite.
25549fd66db6SSebastian Grimberg   The associated CeedQFunction must therefore also be linear.
2555eaf62fffSJeremy L Thompson 
2556ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2557f04ea552SJeremy L Thompson 
2558ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to create element inverses
2559ea61e9acSJeremy L Thompson   @param[out] fdm_inv CeedOperator to apply the action of a FDM based inverse for each element
2560ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2561eaf62fffSJeremy L Thompson 
2562eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2563eaf62fffSJeremy L Thompson 
2564480fae85SJeremy L Thompson   @ref User
2565eaf62fffSJeremy L Thompson **/
25662b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) {
25671c66c397SJeremy L Thompson   Ceed                 ceed, ceed_parent;
25681c66c397SJeremy L Thompson   bool                 interp = false, grad = false, is_tensor_basis = true;
25691c66c397SJeremy L Thompson   CeedInt              num_input_fields, P_1d, Q_1d, num_nodes, num_qpts, dim, num_comp = 1, num_elem = 1;
25701c66c397SJeremy L Thompson   CeedSize             l_size = 1;
25711c66c397SJeremy L Thompson   CeedScalar          *mass, *laplace, *x, *fdm_interp, *lambda, *elem_avg;
25721c66c397SJeremy L Thompson   const CeedScalar    *interp_1d, *grad_1d, *q_weight_1d;
25731c66c397SJeremy L Thompson   CeedVector           q_data;
25741c66c397SJeremy L Thompson   CeedElemRestriction  rstr  = NULL, rstr_qd_i;
25751c66c397SJeremy L Thompson   CeedBasis            basis = NULL, fdm_basis;
25761c66c397SJeremy L Thompson   CeedQFunctionContext ctx_fdm;
25771c66c397SJeremy L Thompson   CeedQFunctionField  *qf_fields;
25781c66c397SJeremy L Thompson   CeedQFunction        qf, qf_fdm;
25791c66c397SJeremy L Thompson   CeedOperatorField   *op_fields;
25801c66c397SJeremy L Thompson 
25812b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2582eaf62fffSJeremy L Thompson 
2583eaf62fffSJeremy L Thompson   if (op->CreateFDMElementInverse) {
2584d04bbc78SJeremy L Thompson     // Backend version
25852b730f8bSJeremy L Thompson     CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request));
2586eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2587eaf62fffSJeremy L Thompson   } else {
2588d04bbc78SJeremy L Thompson     // Operator fallback
2589d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2590d04bbc78SJeremy L Thompson 
25912b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2592d04bbc78SJeremy L Thompson     if (op_fallback) {
25932b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request));
2594eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2595eaf62fffSJeremy L Thompson     }
2596eaf62fffSJeremy L Thompson   }
2597eaf62fffSJeremy L Thompson 
2598d04bbc78SJeremy L Thompson   // Default interface implementation
25992b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
2600bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
26012b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
2602eaf62fffSJeremy L Thompson 
2603eaf62fffSJeremy L Thompson   // Determine active input basis
26042b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL));
26052b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
2606eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
2607eaf62fffSJeremy L Thompson     CeedVector vec;
26081c66c397SJeremy L Thompson 
26092b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
2610eaf62fffSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2611eaf62fffSJeremy L Thompson       CeedEvalMode eval_mode;
26121c66c397SJeremy L Thompson 
26132b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
2614eaf62fffSJeremy L Thompson       interp = interp || eval_mode == CEED_EVAL_INTERP;
2615eaf62fffSJeremy L Thompson       grad   = grad || eval_mode == CEED_EVAL_GRAD;
26162b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis));
26172b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
2618eaf62fffSJeremy L Thompson     }
2619eaf62fffSJeremy L Thompson   }
26206574a04fSJeremy L Thompson   CeedCheck(basis, ceed, CEED_ERROR_BACKEND, "No active field set");
26212b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
2622352a5e7cSSebastian Grimberg   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
26232b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
26242b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
26252b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
26262b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
26272b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
26282b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
2629eaf62fffSJeremy L Thompson 
2630eaf62fffSJeremy L Thompson   // Build and diagonalize 1D Mass and Laplacian
26316574a04fSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
26326574a04fSJeremy L Thompson   CeedCheck(is_tensor_basis, ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases");
26332b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &mass));
26342b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &laplace));
26352b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &x));
26362b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp));
26372b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &lambda));
2638eaf62fffSJeremy L Thompson   // -- Build matrices
26392b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
26402b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
26412b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
26422b730f8bSJeremy L Thompson   CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace));
2643eaf62fffSJeremy L Thompson 
2644eaf62fffSJeremy L Thompson   // -- Diagonalize
26452b730f8bSJeremy L Thompson   CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d));
26462b730f8bSJeremy L Thompson   CeedCall(CeedFree(&mass));
26472b730f8bSJeremy L Thompson   CeedCall(CeedFree(&laplace));
26482b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
26492b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d];
26502b730f8bSJeremy L Thompson   }
26512b730f8bSJeremy L Thompson   CeedCall(CeedFree(&x));
2652eaf62fffSJeremy L Thompson 
26531c66c397SJeremy L Thompson   {
26541c66c397SJeremy L Thompson     CeedInt             layout[3], num_modes = (interp ? 1 : 0) + (grad ? dim : 0);
26551c66c397SJeremy L Thompson     CeedScalar          max_norm = 0;
26561c66c397SJeremy L Thompson     const CeedScalar   *assembled_array, *q_weight_array;
26571c66c397SJeremy L Thompson     CeedVector          assembled = NULL, q_weight;
2658c5f45aeaSJeremy L Thompson     CeedElemRestriction rstr_qf   = NULL;
26591c66c397SJeremy L Thompson 
26601c66c397SJeremy L Thompson     // Assemble QFunction
26612b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request));
26622b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetELayout(rstr_qf, &layout));
26632b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr_qf));
26642b730f8bSJeremy L Thompson     CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm));
2665eaf62fffSJeremy L Thompson 
2666eaf62fffSJeremy L Thompson     // Calculate element averages
26672b730f8bSJeremy L Thompson     CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight));
26682b730f8bSJeremy L Thompson     CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight));
26692b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array));
26702b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array));
26712b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_elem, &elem_avg));
2672eaf62fffSJeremy L Thompson     const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON;
26731c66c397SJeremy L Thompson 
2674eaf62fffSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
2675eaf62fffSJeremy L Thompson       CeedInt count = 0;
26761c66c397SJeremy L Thompson 
26772b730f8bSJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
26782b730f8bSJeremy L Thompson         for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) {
26792b730f8bSJeremy L Thompson           if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) {
26802b730f8bSJeremy L Thompson             elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q];
2681eaf62fffSJeremy L Thompson             count++;
2682eaf62fffSJeremy L Thompson           }
26832b730f8bSJeremy L Thompson         }
26842b730f8bSJeremy L Thompson       }
2685eaf62fffSJeremy L Thompson       if (count) {
2686eaf62fffSJeremy L Thompson         elem_avg[e] /= count;
2687eaf62fffSJeremy L Thompson       } else {
2688eaf62fffSJeremy L Thompson         elem_avg[e] = 1.0;
2689eaf62fffSJeremy L Thompson       }
2690eaf62fffSJeremy L Thompson     }
26912b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array));
26922b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled));
26932b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array));
26942b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&q_weight));
26951c66c397SJeremy L Thompson   }
2696eaf62fffSJeremy L Thompson 
2697eaf62fffSJeremy L Thompson   // Build FDM diagonal
26981c66c397SJeremy L Thompson   {
2699eaf62fffSJeremy L Thompson     CeedScalar *q_data_array, *fdm_diagonal;
27001c66c397SJeremy L Thompson 
2701352a5e7cSSebastian Grimberg     CeedCall(CeedCalloc(num_comp * num_nodes, &fdm_diagonal));
2702352a5e7cSSebastian Grimberg     const CeedScalar fdm_diagonal_bound = num_nodes * CEED_EPSILON;
27032b730f8bSJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
2704352a5e7cSSebastian Grimberg       for (CeedInt n = 0; n < num_nodes; n++) {
2705352a5e7cSSebastian Grimberg         if (interp) fdm_diagonal[c * num_nodes + n] = 1.0;
27062b730f8bSJeremy L Thompson         if (grad) {
2707eaf62fffSJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
2708eaf62fffSJeremy L Thompson             CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d;
2709352a5e7cSSebastian Grimberg             fdm_diagonal[c * num_nodes + n] += lambda[i];
2710eaf62fffSJeremy L Thompson           }
2711eaf62fffSJeremy L Thompson         }
2712352a5e7cSSebastian Grimberg         if (fabs(fdm_diagonal[c * num_nodes + n]) < fdm_diagonal_bound) fdm_diagonal[c * num_nodes + n] = fdm_diagonal_bound;
27132b730f8bSJeremy L Thompson       }
27142b730f8bSJeremy L Thompson     }
2715352a5e7cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * num_nodes, &q_data));
27162b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(q_data, 0.0));
27172b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array));
27182b730f8bSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
27192b730f8bSJeremy L Thompson       for (CeedInt c = 0; c < num_comp; c++) {
27201c66c397SJeremy L Thompson         for (CeedInt n = 0; n < num_nodes; n++)
27211c66c397SJeremy L Thompson           q_data_array[(e * num_comp + c) * num_nodes + n] = 1. / (elem_avg[e] * fdm_diagonal[c * num_nodes + n]);
27222b730f8bSJeremy L Thompson       }
27232b730f8bSJeremy L Thompson     }
27242b730f8bSJeremy L Thompson     CeedCall(CeedFree(&elem_avg));
27252b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_diagonal));
27262b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(q_data, &q_data_array));
27271c66c397SJeremy L Thompson   }
2728eaf62fffSJeremy L Thompson 
2729eaf62fffSJeremy L Thompson   // Setup FDM operator
2730eaf62fffSJeremy L Thompson   // -- Basis
27311c66c397SJeremy L Thompson   {
2732eaf62fffSJeremy L Thompson     CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy;
27331c66c397SJeremy L Thompson 
27342b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy));
27352b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_ref_dummy));
27362b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_weight_dummy));
27372b730f8bSJeremy 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));
27382b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_interp));
27392b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad_dummy));
27402b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref_dummy));
27412b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight_dummy));
27422b730f8bSJeremy L Thompson     CeedCall(CeedFree(&lambda));
27431c66c397SJeremy L Thompson   }
2744eaf62fffSJeremy L Thompson 
2745eaf62fffSJeremy L Thompson   // -- Restriction
27461c66c397SJeremy L Thompson   {
2747352a5e7cSSebastian Grimberg     CeedInt strides[3] = {1, num_nodes, num_nodes * num_comp};
2748352a5e7cSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, num_nodes, num_comp, num_elem * num_comp * num_nodes, strides, &rstr_qd_i));
27491c66c397SJeremy L Thompson   }
27501c66c397SJeremy L Thompson 
2751eaf62fffSJeremy L Thompson   // -- QFunction
27522b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm));
27532b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP));
27542b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE));
27552b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP));
27562b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp));
27571c66c397SJeremy L Thompson 
2758eaf62fffSJeremy L Thompson   // -- QFunction context
27591c66c397SJeremy L Thompson   {
2760eaf62fffSJeremy L Thompson     CeedInt *num_comp_data;
27611c66c397SJeremy L Thompson 
27622b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_data));
2763eaf62fffSJeremy L Thompson     num_comp_data[0] = num_comp;
27642b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm));
27652b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data));
27661c66c397SJeremy L Thompson   }
27672b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm));
27682b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_fdm));
27691c66c397SJeremy L Thompson 
2770eaf62fffSJeremy L Thompson   // -- Operator
27712b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv));
27722b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2773356036faSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_NONE, q_data));
27742b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2775eaf62fffSJeremy L Thompson 
2776eaf62fffSJeremy L Thompson   // Cleanup
27772b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_data));
27782b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&fdm_basis));
27792b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i));
27802b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_fdm));
2781eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2782eaf62fffSJeremy L Thompson }
2783eaf62fffSJeremy L Thompson 
2784eaf62fffSJeremy L Thompson /// @}
2785