xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-preconditioning.c (revision 1203703b5dc87b4acbe66c9a27384ca8ad07798d)
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 /**
27ca94c3ddSJeremy L Thompson   @brief Duplicate a `CeedQFunction` with a reference `Ceed` to fallback for advanced `CeedOperator` functionality
289e77b9c8SJeremy L Thompson 
29ca94c3ddSJeremy L Thompson   @param[in]  fallback_ceed `Ceed` on which to create fallback `CeedQFunction`
30ca94c3ddSJeremy L Thompson   @param[in]  qf            `CeedQFunction` to create fallback for
31ca94c3ddSJeremy L Thompson   @param[out] qf_fallback   Fallback `CeedQFunction`
329e77b9c8SJeremy L Thompson 
339e77b9c8SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
349e77b9c8SJeremy L Thompson 
359e77b9c8SJeremy L Thompson   @ref Developer
369e77b9c8SJeremy L Thompson **/
372b730f8bSJeremy L Thompson static int CeedQFunctionCreateFallback(Ceed fallback_ceed, CeedQFunction qf, CeedQFunction *qf_fallback) {
381c66c397SJeremy L Thompson   char               *source_path_with_name = NULL;
39*1203703bSJeremy L Thompson   CeedInt             num_input_fields, num_output_fields;
40*1203703bSJeremy L Thompson   Ceed                ceed;
41*1203703bSJeremy L Thompson   CeedQFunctionField *input_fields, *output_fields;
421c66c397SJeremy L Thompson 
439e77b9c8SJeremy L Thompson   // Check if NULL qf passed in
449e77b9c8SJeremy L Thompson   if (!qf) return CEED_ERROR_SUCCESS;
459e77b9c8SJeremy L Thompson 
46*1203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetCeed(qf, &ceed));
47*1203703bSJeremy L Thompson   CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n");
48*1203703bSJeremy L Thompson   CeedDebug(ceed, "Creating fallback CeedQFunction\n");
49d04bbc78SJeremy L Thompson 
509e77b9c8SJeremy L Thompson   if (qf->source_path) {
512b730f8bSJeremy L Thompson     size_t path_len = strlen(qf->source_path), name_len = strlen(qf->kernel_name);
522b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(path_len + name_len + 2, &source_path_with_name));
539e77b9c8SJeremy L Thompson     memcpy(source_path_with_name, qf->source_path, path_len);
549e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len], ":", 1);
559e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len + 1], qf->kernel_name, name_len);
569e77b9c8SJeremy L Thompson   } else {
572b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &source_path_with_name));
589e77b9c8SJeremy L Thompson   }
599e77b9c8SJeremy L Thompson 
60*1203703bSJeremy L Thompson   {
61*1203703bSJeremy L Thompson     CeedInt           vec_length;
62*1203703bSJeremy L Thompson     CeedQFunctionUser f;
63*1203703bSJeremy L Thompson 
64*1203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length));
65*1203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetUserFunction(qf, &f));
66*1203703bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(fallback_ceed, vec_length, f, source_path_with_name, qf_fallback));
67*1203703bSJeremy L Thompson   }
689e77b9c8SJeremy L Thompson   {
699e77b9c8SJeremy L Thompson     CeedQFunctionContext ctx;
709e77b9c8SJeremy L Thompson 
712b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
722b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx));
739e77b9c8SJeremy L Thompson   }
74*1203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
75*1203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
76*1203703bSJeremy L Thompson     char        *field_name;
77*1203703bSJeremy L Thompson     CeedInt      size;
78*1203703bSJeremy L Thompson     CeedEvalMode eval_mode;
79*1203703bSJeremy L Thompson 
80*1203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetName(input_fields[i], &field_name));
81*1203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetSize(input_fields[i], &size));
82*1203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetEvalMode(input_fields[i], &eval_mode));
83*1203703bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(*qf_fallback, field_name, size, eval_mode));
849e77b9c8SJeremy L Thompson   }
85*1203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
86*1203703bSJeremy L Thompson     char        *field_name;
87*1203703bSJeremy L Thompson     CeedInt      size;
88*1203703bSJeremy L Thompson     CeedEvalMode eval_mode;
89*1203703bSJeremy L Thompson 
90*1203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetName(output_fields[i], &field_name));
91*1203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetSize(output_fields[i], &size));
92*1203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetEvalMode(output_fields[i], &eval_mode));
93*1203703bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(*qf_fallback, field_name, size, eval_mode));
949e77b9c8SJeremy L Thompson   }
952b730f8bSJeremy L Thompson   CeedCall(CeedFree(&source_path_with_name));
969e77b9c8SJeremy L Thompson   return CEED_ERROR_SUCCESS;
979e77b9c8SJeremy L Thompson }
989e77b9c8SJeremy L Thompson 
999e77b9c8SJeremy L Thompson /**
100ca94c3ddSJeremy L Thompson   @brief Duplicate a `CeedOperator` with a reference `Ceed` to fallback for advanced `CeedOperator` functionality
101eaf62fffSJeremy L Thompson 
102ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to create fallback for
103eaf62fffSJeremy L Thompson 
104eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
105eaf62fffSJeremy L Thompson 
106eaf62fffSJeremy L Thompson   @ref Developer
107eaf62fffSJeremy L Thompson **/
108d04bbc78SJeremy L Thompson static int CeedOperatorCreateFallback(CeedOperator op) {
1091c66c397SJeremy L Thompson   bool         is_composite;
110*1203703bSJeremy L Thompson   Ceed         ceed, ceed_fallback;
1111c66c397SJeremy L Thompson   CeedOperator op_fallback;
112eaf62fffSJeremy L Thompson 
113805fe78eSJeremy L Thompson   // Check not already created
114805fe78eSJeremy L Thompson   if (op->op_fallback) return CEED_ERROR_SUCCESS;
115805fe78eSJeremy L Thompson 
116eaf62fffSJeremy L Thompson   // Fallback Ceed
117*1203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
118*1203703bSJeremy L Thompson   CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
119d04bbc78SJeremy L Thompson   if (!ceed_fallback) return CEED_ERROR_SUCCESS;
120d04bbc78SJeremy L Thompson 
121*1203703bSJeremy L Thompson   CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n");
122*1203703bSJeremy L Thompson   CeedDebug(ceed, "Creating fallback CeedOperator\n");
123eaf62fffSJeremy L Thompson 
124eaf62fffSJeremy L Thompson   // Clone Op
125b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
126b275c451SJeremy L Thompson   if (is_composite) {
127b275c451SJeremy L Thompson     CeedInt       num_suboperators;
128b275c451SJeremy L Thompson     CeedOperator *sub_operators;
129b275c451SJeremy L Thompson 
1302b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorCreate(ceed_fallback, &op_fallback));
131b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
132b275c451SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
133b275c451SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
134d04bbc78SJeremy L Thompson       CeedOperator op_sub_fallback;
135d04bbc78SJeremy L Thompson 
136b275c451SJeremy L Thompson       CeedCall(CeedOperatorGetFallback(sub_operators[i], &op_sub_fallback));
1372b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorAddSub(op_fallback, op_sub_fallback));
138805fe78eSJeremy L Thompson     }
139805fe78eSJeremy L Thompson   } else {
140*1203703bSJeremy L Thompson     CeedInt            num_input_fields, num_output_fields;
1419e77b9c8SJeremy L Thompson     CeedQFunction      qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL;
142*1203703bSJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
1431c66c397SJeremy L Thompson 
1442b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback));
1452b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback));
1462b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback));
1472b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback));
148*1203703bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
149*1203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
150*1203703bSJeremy L Thompson       char               *field_name;
151*1203703bSJeremy L Thompson       CeedVector          vec;
152*1203703bSJeremy L Thompson       CeedElemRestriction rstr;
153*1203703bSJeremy L Thompson       CeedBasis           basis;
154*1203703bSJeremy L Thompson 
155*1203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetName(input_fields[i], &field_name));
156*1203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetVector(input_fields[i], &vec));
157*1203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr));
158*1203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis));
159*1203703bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec));
160805fe78eSJeremy L Thompson     }
161*1203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
162*1203703bSJeremy L Thompson       char               *field_name;
163*1203703bSJeremy L Thompson       CeedVector          vec;
164*1203703bSJeremy L Thompson       CeedElemRestriction rstr;
165*1203703bSJeremy L Thompson       CeedBasis           basis;
166*1203703bSJeremy L Thompson 
167*1203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetName(output_fields[i], &field_name));
168*1203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
169*1203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr));
170*1203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis));
171*1203703bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec));
172805fe78eSJeremy L Thompson     }
1732b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op->qf_assembled, &op_fallback->qf_assembled));
1749e77b9c8SJeremy L Thompson     // Cleanup
1752b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf_fallback));
1762b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqf_fallback));
1772b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqfT_fallback));
178805fe78eSJeremy L Thompson   }
1792b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetName(op_fallback, op->name));
1802b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fallback));
181b05f7e9fSJeremy L Thompson   // Note: No ref-counting here so we don't get caught in a reference loop.
182b05f7e9fSJeremy L Thompson   //       The op holds the only reference to op_fallback and is responsible for deleting itself and op_fallback.
183805fe78eSJeremy L Thompson   op->op_fallback                 = op_fallback;
184b05f7e9fSJeremy L Thompson   op_fallback->op_fallback_parent = op;
185eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
186eaf62fffSJeremy L Thompson }
187eaf62fffSJeremy L Thompson 
188eaf62fffSJeremy L Thompson /**
189eaf62fffSJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
190eaf62fffSJeremy L Thompson 
191ca94c3ddSJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble point block diagonal
192ca94c3ddSJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
193bd83916cSSebastian Grimberg   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
194ca94c3ddSJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
195eaf62fffSJeremy L Thompson 
196eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
197eaf62fffSJeremy L Thompson 
198eaf62fffSJeremy L Thompson   @ref Developer
199eaf62fffSJeremy L Thompson **/
200bd83916cSSebastian Grimberg static inline int CeedSingleOperatorAssembleAddDiagonal_Core(CeedOperator op, CeedRequest *request, const bool is_point_block, CeedVector assembled) {
201eaf62fffSJeremy L Thompson   Ceed ceed;
202506b1a0cSSebastian Grimberg   bool is_composite;
203506b1a0cSSebastian Grimberg 
204506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetCeed(op, &ceed));
205506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorIsComposite(op, &is_composite));
206506b1a0cSSebastian Grimberg   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
207506b1a0cSSebastian Grimberg 
208506b1a0cSSebastian Grimberg   // Assemble QFunction
209506b1a0cSSebastian Grimberg   CeedInt             layout_qf[3];
210437c7c90SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
211c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
212c5f45aeaSJeremy L Thompson   CeedElemRestriction assembled_elem_rstr = NULL;
213437c7c90SJeremy L Thompson 
214437c7c90SJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request));
21556c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf));
216437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
217437c7c90SJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
218eaf62fffSJeremy L Thompson 
219ed9e99e6SJeremy L Thompson   // Get assembly data
220437c7c90SJeremy L Thompson   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
221506b1a0cSSebastian Grimberg   CeedInt                  num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out;
222437c7c90SJeremy L Thompson   CeedSize               **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components;
223506b1a0cSSebastian Grimberg   CeedBasis               *active_bases_in, *active_bases_out;
224506b1a0cSSebastian Grimberg   CeedElemRestriction     *active_elem_rstrs_in, *active_elem_rstrs_out;
2251c66c397SJeremy L Thompson   CeedOperatorAssemblyData data;
2261c66c397SJeremy L Thompson 
227437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
228506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in,
229506b1a0cSSebastian Grimberg                                                 &num_active_bases_out, &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out,
230506b1a0cSSebastian Grimberg                                                 &num_output_components));
231506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, NULL, NULL, &active_bases_out, NULL));
232506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs_in, NULL, &active_elem_rstrs_out));
233506b1a0cSSebastian Grimberg 
234934a29f5SSebastian Grimberg   // Loop over all active bases (find matching input/output pairs)
235934a29f5SSebastian Grimberg   for (CeedInt b = 0; b < CeedIntMin(num_active_bases_in, num_active_bases_out); b++) {
236934a29f5SSebastian Grimberg     CeedInt             b_in, b_out, num_elem, num_nodes, num_qpts, num_comp;
2371c66c397SJeremy L Thompson     bool                has_eval_none = false;
2381c66c397SJeremy L Thompson     CeedScalar         *elem_diag_array, *identity = NULL;
2391c66c397SJeremy L Thompson     CeedVector          elem_diag;
2407c1dbaffSSebastian Grimberg     CeedElemRestriction diag_elem_rstr;
2411c66c397SJeremy L Thompson 
242934a29f5SSebastian Grimberg     if (num_active_bases_in <= num_active_bases_out) {
243934a29f5SSebastian Grimberg       b_in = b;
244934a29f5SSebastian Grimberg       for (b_out = 0; b_out < num_active_bases_out; b_out++) {
245934a29f5SSebastian Grimberg         if (active_bases_in[b_in] == active_bases_out[b_out]) {
246934a29f5SSebastian Grimberg           break;
247934a29f5SSebastian Grimberg         }
248934a29f5SSebastian Grimberg       }
249934a29f5SSebastian Grimberg       if (b_out == num_active_bases_out) {
250934a29f5SSebastian Grimberg         continue;
251934a29f5SSebastian Grimberg       }  // No matching output basis found
252934a29f5SSebastian Grimberg     } else {
253934a29f5SSebastian Grimberg       b_out = b;
254934a29f5SSebastian Grimberg       for (b_in = 0; b_in < num_active_bases_in; b_in++) {
255934a29f5SSebastian Grimberg         if (active_bases_in[b_in] == active_bases_out[b_out]) {
256934a29f5SSebastian Grimberg           break;
257934a29f5SSebastian Grimberg         }
258934a29f5SSebastian Grimberg       }
259934a29f5SSebastian Grimberg       if (b_in == num_active_bases_in) {
260934a29f5SSebastian Grimberg         continue;
261934a29f5SSebastian Grimberg       }  // No matching output basis found
262934a29f5SSebastian Grimberg     }
263934a29f5SSebastian Grimberg     CeedCheck(active_elem_rstrs_in[b_in] == active_elem_rstrs_out[b_out], ceed, CEED_ERROR_UNSUPPORTED,
264506b1a0cSSebastian Grimberg               "Cannot assemble operator diagonal with different input and output active element restrictions");
265506b1a0cSSebastian Grimberg 
2661c66c397SJeremy L Thompson     // Assemble point block diagonal restriction, if needed
267bd83916cSSebastian Grimberg     if (is_point_block) {
268934a29f5SSebastian Grimberg       CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstrs_in[b_in], &diag_elem_rstr));
2697c1dbaffSSebastian Grimberg     } else {
270934a29f5SSebastian Grimberg       CeedCall(CeedElemRestrictionCreateUnsignedCopy(active_elem_rstrs_in[b_in], &diag_elem_rstr));
271eaf62fffSJeremy L Thompson     }
272eaf62fffSJeremy L Thompson 
273eaf62fffSJeremy L Thompson     // Create diagonal vector
274437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag));
275eaf62fffSJeremy L Thompson 
276eaf62fffSJeremy L Thompson     // Assemble element operator diagonals
2772b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(elem_diag, 0.0));
2782b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array));
279437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem));
280934a29f5SSebastian Grimberg     CeedCall(CeedBasisGetNumNodes(active_bases_in[b_in], &num_nodes));
281934a29f5SSebastian Grimberg     CeedCall(CeedBasisGetNumComponents(active_bases_in[b_in], &num_comp));
282934a29f5SSebastian Grimberg     if (active_bases_in[b_in] == CEED_BASIS_NONE) num_qpts = num_nodes;
283934a29f5SSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(active_bases_in[b_in], &num_qpts));
284ed9e99e6SJeremy L Thompson 
285352a5e7cSSebastian Grimberg     // Construct identity matrix for basis if required
286934a29f5SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in[b_in]; i++) {
287934a29f5SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[b_in][i] == CEED_EVAL_NONE);
288ed9e99e6SJeremy L Thompson     }
289934a29f5SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out[b_out]; i++) {
290934a29f5SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[b_out][i] == CEED_EVAL_NONE);
291ed9e99e6SJeremy L Thompson     }
292ed9e99e6SJeremy L Thompson     if (has_eval_none) {
2932b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
2942b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
295eaf62fffSJeremy L Thompson     }
296352a5e7cSSebastian Grimberg 
297eaf62fffSJeremy L Thompson     // Compute the diagonal of B^T D B
298eaf62fffSJeremy L Thompson     // Each element
299b94338b9SJed Brown     for (CeedSize e = 0; e < num_elem; e++) {
300eaf62fffSJeremy L Thompson       // Each basis eval mode pair
301352a5e7cSSebastian Grimberg       CeedInt      d_out              = 0, q_comp_out;
302352a5e7cSSebastian Grimberg       CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
3031c66c397SJeremy L Thompson 
304934a29f5SSebastian Grimberg       for (CeedInt e_out = 0; e_out < num_eval_modes_out[b_out]; e_out++) {
3051c66c397SJeremy L Thompson         CeedInt           d_in              = 0, q_comp_in;
306437c7c90SJeremy L Thompson         const CeedScalar *B_t               = NULL;
3071c66c397SJeremy L Thompson         CeedEvalMode      eval_mode_in_prev = CEED_EVAL_NONE;
3081c66c397SJeremy L Thompson 
309934a29f5SSebastian Grimberg         CeedCall(CeedOperatorGetBasisPointer(active_bases_out[b_out], eval_modes_out[b_out][e_out], identity, &B_t));
310934a29f5SSebastian Grimberg         CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_out[b_out], eval_modes_out[b_out][e_out], &q_comp_out));
311352a5e7cSSebastian Grimberg         if (q_comp_out > 1) {
312934a29f5SSebastian Grimberg           if (e_out == 0 || eval_modes_out[b_out][e_out] != eval_mode_out_prev) d_out = 0;
313352a5e7cSSebastian Grimberg           else B_t = &B_t[(++d_out) * num_qpts * num_nodes];
314352a5e7cSSebastian Grimberg         }
315934a29f5SSebastian Grimberg         eval_mode_out_prev = eval_modes_out[b_out][e_out];
316352a5e7cSSebastian Grimberg 
317934a29f5SSebastian Grimberg         for (CeedInt e_in = 0; e_in < num_eval_modes_in[b_in]; e_in++) {
318437c7c90SJeremy L Thompson           const CeedScalar *B = NULL;
3191c66c397SJeremy L Thompson 
320934a29f5SSebastian Grimberg           CeedCall(CeedOperatorGetBasisPointer(active_bases_in[b_in], eval_modes_in[b_in][e_in], identity, &B));
321934a29f5SSebastian Grimberg           CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_in[b_in], eval_modes_in[b_in][e_in], &q_comp_in));
322352a5e7cSSebastian Grimberg           if (q_comp_in > 1) {
323934a29f5SSebastian Grimberg             if (e_in == 0 || eval_modes_in[b_in][e_in] != eval_mode_in_prev) d_in = 0;
324352a5e7cSSebastian Grimberg             else B = &B[(++d_in) * num_qpts * num_nodes];
325352a5e7cSSebastian Grimberg           }
326934a29f5SSebastian Grimberg           eval_mode_in_prev = eval_modes_in[b_in][e_in];
327352a5e7cSSebastian Grimberg 
328eaf62fffSJeremy L Thompson           // Each component
329506b1a0cSSebastian Grimberg           for (CeedInt c_out = 0; c_out < num_comp; c_out++) {
330437c7c90SJeremy L Thompson             // Each qpt/node pair
3312b730f8bSJeremy L Thompson             for (CeedInt q = 0; q < num_qpts; q++) {
332bd83916cSSebastian Grimberg               if (is_point_block) {
333eaf62fffSJeremy L Thompson                 // Point Block Diagonal
334506b1a0cSSebastian Grimberg                 for (CeedInt c_in = 0; c_in < num_comp; c_in++) {
335934a29f5SSebastian Grimberg                   const CeedSize c_offset =
336934a29f5SSebastian Grimberg                       (eval_mode_offsets_in[b_in][e_in] + c_in) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out;
337506b1a0cSSebastian Grimberg                   const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]];
3381c66c397SJeremy L Thompson 
3392b730f8bSJeremy L Thompson                   for (CeedInt n = 0; n < num_nodes; n++) {
340506b1a0cSSebastian Grimberg                     elem_diag_array[((e * num_comp + c_out) * num_comp + c_in) * num_nodes + n] +=
341437c7c90SJeremy L Thompson                         B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n];
342eaf62fffSJeremy L Thompson                   }
3432b730f8bSJeremy L Thompson                 }
344eaf62fffSJeremy L Thompson               } else {
345eaf62fffSJeremy L Thompson                 // Diagonal Only
346934a29f5SSebastian Grimberg                 const CeedInt c_offset =
347934a29f5SSebastian Grimberg                     (eval_mode_offsets_in[b_in][e_in] + c_out) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out;
348506b1a0cSSebastian Grimberg                 const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]];
3491c66c397SJeremy L Thompson 
3502b730f8bSJeremy L Thompson                 for (CeedInt n = 0; n < num_nodes; n++) {
351506b1a0cSSebastian 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];
352eaf62fffSJeremy L Thompson                 }
353eaf62fffSJeremy L Thompson               }
354eaf62fffSJeremy L Thompson             }
355eaf62fffSJeremy L Thompson           }
3562b730f8bSJeremy L Thompson         }
3572b730f8bSJeremy L Thompson       }
3582b730f8bSJeremy L Thompson     }
3592b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
360eaf62fffSJeremy L Thompson 
361eaf62fffSJeremy L Thompson     // Assemble local operator diagonal
3627c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
363eaf62fffSJeremy L Thompson 
364eaf62fffSJeremy L Thompson     // Cleanup
3657c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr));
3662b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&elem_diag));
3672b730f8bSJeremy L Thompson     CeedCall(CeedFree(&identity));
368437c7c90SJeremy L Thompson   }
369437c7c90SJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
370437c7c90SJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
371eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
372eaf62fffSJeremy L Thompson }
373eaf62fffSJeremy L Thompson 
374eaf62fffSJeremy L Thompson /**
375eaf62fffSJeremy L Thompson   @brief Core logic for assembling composite operator diagonal
376eaf62fffSJeremy L Thompson 
377ca94c3ddSJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble point block diagonal
378ca94c3ddSJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
379bd83916cSSebastian Grimberg   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
380ca94c3ddSJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
381eaf62fffSJeremy L Thompson 
382eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
383eaf62fffSJeremy L Thompson 
384eaf62fffSJeremy L Thompson   @ref Developer
385eaf62fffSJeremy L Thompson **/
386bd83916cSSebastian Grimberg static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block,
387eaf62fffSJeremy L Thompson                                                                  CeedVector assembled) {
388eaf62fffSJeremy L Thompson   CeedInt       num_sub;
389eaf62fffSJeremy L Thompson   CeedOperator *suboperators;
3901c66c397SJeremy L Thompson 
391c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
392c6ebc35dSJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators));
393eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_sub; i++) {
394bd83916cSSebastian Grimberg     if (is_point_block) {
3952b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request));
3966aa95790SJeremy L Thompson     } else {
3972b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request));
3986aa95790SJeremy L Thompson     }
399eaf62fffSJeremy L Thompson   }
400eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
401eaf62fffSJeremy L Thompson }
402eaf62fffSJeremy L Thompson 
403eaf62fffSJeremy L Thompson /**
404ca94c3ddSJeremy L Thompson   @brief Build nonzero pattern for non-composite CeedOperator`.
405eaf62fffSJeremy L Thompson 
406ca94c3ddSJeremy L Thompson   Users should generally use @ref CeedOperatorLinearAssembleSymbolic().
407eaf62fffSJeremy L Thompson 
408ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` to assemble nonzero pattern
409eaf62fffSJeremy L Thompson   @param[in]  offset Offset for number of entries
410eaf62fffSJeremy L Thompson   @param[out] rows   Row number for each entry
411eaf62fffSJeremy L Thompson   @param[out] cols   Column number for each entry
412eaf62fffSJeremy L Thompson 
413eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
414eaf62fffSJeremy L Thompson 
415eaf62fffSJeremy L Thompson   @ref Developer
416eaf62fffSJeremy L Thompson **/
4172b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) {
418f3d47e36SJeremy L Thompson   Ceed                ceed;
419f3d47e36SJeremy L Thompson   bool                is_composite;
420506b1a0cSSebastian Grimberg   CeedSize            num_nodes_in, num_nodes_out, count = 0;
421506b1a0cSSebastian Grimberg   CeedInt             num_elem_in, elem_size_in, num_comp_in, layout_er_in[3];
422506b1a0cSSebastian Grimberg   CeedInt             num_elem_out, elem_size_out, num_comp_out, layout_er_out[3], local_num_entries;
4231c66c397SJeremy L Thompson   CeedScalar         *array;
424506b1a0cSSebastian Grimberg   const CeedScalar   *elem_dof_a_in, *elem_dof_a_out;
425506b1a0cSSebastian Grimberg   CeedVector          index_vec_in, index_vec_out, elem_dof_in, elem_dof_out;
426506b1a0cSSebastian Grimberg   CeedElemRestriction elem_rstr_in, elem_rstr_out, index_elem_rstr_in, index_elem_rstr_out;
4271c66c397SJeremy L Thompson 
428f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
429f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4306574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
431eaf62fffSJeremy L Thompson 
432506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes_in, &num_nodes_out));
433506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out));
434506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in));
435506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in));
436506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in));
43756c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(elem_rstr_in, layout_er_in));
438eaf62fffSJeremy L Thompson 
439506b1a0cSSebastian Grimberg   // Determine elem_dof relation for input
440506b1a0cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed, num_nodes_in, &index_vec_in));
441506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayWrite(index_vec_in, CEED_MEM_HOST, &array));
442506b1a0cSSebastian Grimberg   for (CeedInt i = 0; i < num_nodes_in; i++) array[i] = i;
443506b1a0cSSebastian Grimberg   CeedCall(CeedVectorRestoreArray(index_vec_in, &array));
444506b1a0cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed, num_elem_in * elem_size_in * num_comp_in, &elem_dof_in));
445506b1a0cSSebastian Grimberg   CeedCall(CeedVectorSetValue(elem_dof_in, 0.0));
446506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_in, &index_elem_rstr_in));
447506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionApply(index_elem_rstr_in, CEED_NOTRANSPOSE, index_vec_in, elem_dof_in, CEED_REQUEST_IMMEDIATE));
448506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayRead(elem_dof_in, CEED_MEM_HOST, &elem_dof_a_in));
449506b1a0cSSebastian Grimberg   CeedCall(CeedVectorDestroy(&index_vec_in));
450506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_in));
451506b1a0cSSebastian Grimberg 
452506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
453506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out));
454506b1a0cSSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
455506b1a0cSSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
456506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out));
457506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out));
45856c48462SJeremy L Thompson     CeedCall(CeedElemRestrictionGetELayout(elem_rstr_out, layout_er_out));
459506b1a0cSSebastian Grimberg 
460506b1a0cSSebastian Grimberg     // Determine elem_dof relation for output
461506b1a0cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed, num_nodes_out, &index_vec_out));
462506b1a0cSSebastian Grimberg     CeedCall(CeedVectorGetArrayWrite(index_vec_out, CEED_MEM_HOST, &array));
463506b1a0cSSebastian Grimberg     for (CeedInt i = 0; i < num_nodes_out; i++) array[i] = i;
464506b1a0cSSebastian Grimberg     CeedCall(CeedVectorRestoreArray(index_vec_out, &array));
465506b1a0cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed, num_elem_out * elem_size_out * num_comp_out, &elem_dof_out));
466506b1a0cSSebastian Grimberg     CeedCall(CeedVectorSetValue(elem_dof_out, 0.0));
467506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_out, &index_elem_rstr_out));
468506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(index_elem_rstr_out, CEED_NOTRANSPOSE, index_vec_out, elem_dof_out, CEED_REQUEST_IMMEDIATE));
469506b1a0cSSebastian Grimberg     CeedCall(CeedVectorGetArrayRead(elem_dof_out, CEED_MEM_HOST, &elem_dof_a_out));
470506b1a0cSSebastian Grimberg     CeedCall(CeedVectorDestroy(&index_vec_out));
471506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_out));
472506b1a0cSSebastian Grimberg   } else {
473506b1a0cSSebastian Grimberg     num_elem_out     = num_elem_in;
474506b1a0cSSebastian Grimberg     elem_size_out    = elem_size_in;
475506b1a0cSSebastian Grimberg     num_comp_out     = num_comp_in;
476506b1a0cSSebastian Grimberg     layout_er_out[0] = layout_er_in[0];
477506b1a0cSSebastian Grimberg     layout_er_out[1] = layout_er_in[1];
478506b1a0cSSebastian Grimberg     layout_er_out[2] = layout_er_in[2];
479506b1a0cSSebastian Grimberg     elem_dof_a_out   = elem_dof_a_in;
480506b1a0cSSebastian Grimberg   }
481506b1a0cSSebastian Grimberg   local_num_entries = elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in;
482eaf62fffSJeremy L Thompson 
483eaf62fffSJeremy L Thompson   // Determine i, j locations for element matrices
484506b1a0cSSebastian Grimberg   for (CeedInt e = 0; e < num_elem_in; e++) {
485506b1a0cSSebastian Grimberg     for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) {
486506b1a0cSSebastian Grimberg       for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) {
487506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < elem_size_out; i++) {
488506b1a0cSSebastian Grimberg           for (CeedInt j = 0; j < elem_size_in; j++) {
489506b1a0cSSebastian Grimberg             const CeedInt elem_dof_index_row = i * layout_er_out[0] + comp_out * layout_er_out[1] + e * layout_er_out[2];
490506b1a0cSSebastian Grimberg             const CeedInt elem_dof_index_col = j * layout_er_in[0] + comp_in * layout_er_in[1] + e * layout_er_in[2];
491506b1a0cSSebastian Grimberg             const CeedInt row                = elem_dof_a_out[elem_dof_index_row];
492506b1a0cSSebastian Grimberg             const CeedInt col                = elem_dof_a_in[elem_dof_index_col];
493eaf62fffSJeremy L Thompson 
494eaf62fffSJeremy L Thompson             rows[offset + count] = row;
495eaf62fffSJeremy L Thompson             cols[offset + count] = col;
496eaf62fffSJeremy L Thompson             count++;
497eaf62fffSJeremy L Thompson           }
498eaf62fffSJeremy L Thompson         }
499eaf62fffSJeremy L Thompson       }
500eaf62fffSJeremy L Thompson     }
501eaf62fffSJeremy L Thompson   }
5026574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing assembled entries");
503506b1a0cSSebastian Grimberg   CeedCall(CeedVectorRestoreArrayRead(elem_dof_in, &elem_dof_a_in));
504506b1a0cSSebastian Grimberg   CeedCall(CeedVectorDestroy(&elem_dof_in));
505506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
506506b1a0cSSebastian Grimberg     CeedCall(CeedVectorRestoreArrayRead(elem_dof_out, &elem_dof_a_out));
507506b1a0cSSebastian Grimberg     CeedCall(CeedVectorDestroy(&elem_dof_out));
508506b1a0cSSebastian Grimberg   }
509eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
510eaf62fffSJeremy L Thompson }
511eaf62fffSJeremy L Thompson 
512eaf62fffSJeremy L Thompson /**
513ca94c3ddSJeremy L Thompson   @brief Assemble nonzero entries for non-composite `CeedOperator`.
514eaf62fffSJeremy L Thompson 
515ca94c3ddSJeremy L Thompson   Users should generally use @ref CeedOperatorLinearAssemble().
516eaf62fffSJeremy L Thompson 
517ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` to assemble
518ea61e9acSJeremy L Thompson   @param[in]  offset Offset for number of entries
519eaf62fffSJeremy L Thompson   @param[out] values Values to assemble into matrix
520eaf62fffSJeremy L Thompson 
521eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
522eaf62fffSJeremy L Thompson 
523eaf62fffSJeremy L Thompson   @ref Developer
524eaf62fffSJeremy L Thompson **/
5252b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) {
526f3d47e36SJeremy L Thompson   Ceed ceed;
527f3d47e36SJeremy L Thompson   bool is_composite;
5281c66c397SJeremy L Thompson 
529f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
530f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
5316574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
532f3d47e36SJeremy L Thompson 
533f3d47e36SJeremy L Thompson   // Early exit for empty operator
534f3d47e36SJeremy L Thompson   {
535f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
536f3d47e36SJeremy L Thompson 
537f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
538f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
539f3d47e36SJeremy L Thompson   }
540eaf62fffSJeremy L Thompson 
541cefa2673SJeremy L Thompson   if (op->LinearAssembleSingle) {
542cefa2673SJeremy L Thompson     // Backend version
5432b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSingle(op, offset, values));
544cefa2673SJeremy L Thompson     return CEED_ERROR_SUCCESS;
545cefa2673SJeremy L Thompson   } else {
546cefa2673SJeremy L Thompson     // Operator fallback
547cefa2673SJeremy L Thompson     CeedOperator op_fallback;
548cefa2673SJeremy L Thompson 
5492b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
550cefa2673SJeremy L Thompson     if (op_fallback) {
5512b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values));
552cefa2673SJeremy L Thompson       return CEED_ERROR_SUCCESS;
553cefa2673SJeremy L Thompson     }
554cefa2673SJeremy L Thompson   }
555cefa2673SJeremy L Thompson 
556eaf62fffSJeremy L Thompson   // Assemble QFunction
557506b1a0cSSebastian Grimberg   CeedInt             layout_qf[3];
5581c66c397SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
559c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
560506b1a0cSSebastian Grimberg   CeedElemRestriction assembled_elem_rstr = NULL;
561eaf62fffSJeremy L Thompson 
562506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, CEED_REQUEST_IMMEDIATE));
56356c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf));
564506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
565506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
566eaf62fffSJeremy L Thompson 
567ed9e99e6SJeremy L Thompson   // Get assembly data
568506b1a0cSSebastian Grimberg   CeedInt                  num_elem_in, elem_size_in, num_comp_in, num_qpts_in;
569506b1a0cSSebastian Grimberg   CeedInt                  num_elem_out, elem_size_out, num_comp_out, num_qpts_out, local_num_entries;
570506b1a0cSSebastian Grimberg   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
571506b1a0cSSebastian Grimberg   CeedInt                  num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out;
572506b1a0cSSebastian Grimberg   CeedBasis               *active_bases_in, *active_bases_out, basis_in, basis_out;
573506b1a0cSSebastian Grimberg   const CeedScalar       **B_mats_in, **B_mats_out, *B_mat_in, *B_mat_out;
574506b1a0cSSebastian Grimberg   CeedElemRestriction      elem_rstr_in, elem_rstr_out;
575506b1a0cSSebastian Grimberg   CeedRestrictionType      elem_rstr_type_in, elem_rstr_type_out;
576506b1a0cSSebastian Grimberg   const bool              *elem_rstr_orients_in = NULL, *elem_rstr_orients_out = NULL;
577506b1a0cSSebastian Grimberg   const CeedInt8          *elem_rstr_curl_orients_in = NULL, *elem_rstr_curl_orients_out = NULL;
578506b1a0cSSebastian Grimberg   CeedOperatorAssemblyData data;
579eaf62fffSJeremy L Thompson 
580506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
581506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, NULL, &num_active_bases_out,
582506b1a0cSSebastian Grimberg                                                 &num_eval_modes_out, &eval_modes_out, NULL, NULL));
583506b1a0cSSebastian Grimberg 
584506b1a0cSSebastian Grimberg   CeedCheck(num_active_bases_in == num_active_bases_out && num_active_bases_in == 1, ceed, CEED_ERROR_UNSUPPORTED,
585506b1a0cSSebastian Grimberg             "Cannot assemble operator with multiple active bases");
5866574a04fSJeremy 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");
587eaf62fffSJeremy L Thompson 
588506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, &B_mats_in, NULL, &active_bases_out, &B_mats_out));
589506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out));
590506b1a0cSSebastian Grimberg   basis_in  = active_bases_in[0];
591506b1a0cSSebastian Grimberg   basis_out = active_bases_out[0];
592506b1a0cSSebastian Grimberg   B_mat_in  = B_mats_in[0];
593506b1a0cSSebastian Grimberg   B_mat_out = B_mats_out[0];
594eaf62fffSJeremy L Thompson 
595506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in));
596506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in));
597506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in));
598506b1a0cSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
599506b1a0cSSebastian Grimberg   else CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
600506b1a0cSSebastian Grimberg 
601506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetType(elem_rstr_in, &elem_rstr_type_in));
602506b1a0cSSebastian Grimberg   if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) {
603506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_orients_in));
604506b1a0cSSebastian Grimberg   } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
605506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_curl_orients_in));
6067c1dbaffSSebastian Grimberg   }
6077c1dbaffSSebastian Grimberg 
608506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
609506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out));
610506b1a0cSSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
611506b1a0cSSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
612506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out));
613506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out));
614506b1a0cSSebastian Grimberg     if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
615506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
616506b1a0cSSebastian Grimberg     CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
617506b1a0cSSebastian Grimberg               "Active input and output bases must have the same number of quadrature points");
618eaf62fffSJeremy L Thompson 
619506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(elem_rstr_out, &elem_rstr_type_out));
620506b1a0cSSebastian Grimberg     if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) {
621506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_orients_out));
622506b1a0cSSebastian Grimberg     } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
623506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_curl_orients_out));
624506b1a0cSSebastian Grimberg     }
625506b1a0cSSebastian Grimberg   } else {
626506b1a0cSSebastian Grimberg     num_elem_out  = num_elem_in;
627506b1a0cSSebastian Grimberg     elem_size_out = elem_size_in;
628506b1a0cSSebastian Grimberg     num_comp_out  = num_comp_in;
629506b1a0cSSebastian Grimberg     num_qpts_out  = num_qpts_in;
630506b1a0cSSebastian Grimberg 
631506b1a0cSSebastian Grimberg     elem_rstr_orients_out      = elem_rstr_orients_in;
632506b1a0cSSebastian Grimberg     elem_rstr_curl_orients_out = elem_rstr_curl_orients_in;
633506b1a0cSSebastian Grimberg   }
634506b1a0cSSebastian Grimberg   local_num_entries = elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in;
635506b1a0cSSebastian Grimberg 
636506b1a0cSSebastian Grimberg   // Loop over elements and put in data structure
6377c1dbaffSSebastian Grimberg   // We store B_mat_in, B_mat_out, BTD, elem_mat in row-major order
6380459ebd3SSebastian Grimberg   CeedTensorContract contract;
6394a9a33d7SSebastian Grimberg   CeedSize           count = 0;
640123d890dSSebastian Grimberg   CeedScalar        *vals, *BTD_mat = NULL, *elem_mat = NULL, *elem_mat_b = NULL;
641506b1a0cSSebastian Grimberg 
642c22497adSSebastian Grimberg   CeedCall(CeedBasisGetTensorContract(basis_in, &contract));
643123d890dSSebastian Grimberg   CeedCall(CeedCalloc(elem_size_out * num_qpts_in * num_eval_modes_in[0], &BTD_mat));
644123d890dSSebastian Grimberg   CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat));
645506b1a0cSSebastian Grimberg   if (elem_rstr_curl_orients_in || elem_rstr_curl_orients_out) CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat_b));
6461c66c397SJeremy L Thompson 
64728ec399dSJeremy L Thompson   CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals));
648506b1a0cSSebastian Grimberg   for (CeedSize e = 0; e < num_elem_in; e++) {
649506b1a0cSSebastian Grimberg     for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) {
650506b1a0cSSebastian Grimberg       for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) {
651ed9e99e6SJeremy L Thompson         // Compute B^T*D
652506b1a0cSSebastian Grimberg         for (CeedSize n = 0; n < elem_size_out; n++) {
653506b1a0cSSebastian Grimberg           for (CeedSize q = 0; q < num_qpts_in; q++) {
654437c7c90SJeremy L Thompson             for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) {
655506b1a0cSSebastian Grimberg               const CeedSize btd_index = n * (num_qpts_in * num_eval_modes_in[0]) + q * num_eval_modes_in[0] + e_in;
656067fd99fSJeremy L Thompson               CeedScalar     sum       = 0.0;
6571c66c397SJeremy L Thompson 
658437c7c90SJeremy L Thompson               for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) {
659506b1a0cSSebastian Grimberg                 const CeedSize b_out_index     = (q * num_eval_modes_out[0] + e_out) * elem_size_out + n;
660506b1a0cSSebastian 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;
661b94338b9SJed Brown                 const CeedSize qf_index        = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2];
6621c66c397SJeremy L Thompson 
663067fd99fSJeremy L Thompson                 sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index];
664eaf62fffSJeremy L Thompson               }
665067fd99fSJeremy L Thompson               BTD_mat[btd_index] = sum;
666ed9e99e6SJeremy L Thompson             }
667ed9e99e6SJeremy L Thompson           }
668eaf62fffSJeremy L Thompson         }
6697c1dbaffSSebastian Grimberg 
6707c1dbaffSSebastian Grimberg         // Form element matrix itself (for each block component)
671e4065a52SSebastian Grimberg         if (contract) {
6720459ebd3SSebastian Grimberg           CeedCall(CeedTensorContractApply(contract, 1, num_qpts_in * num_eval_modes_in[0], elem_size_in, elem_size_out, BTD_mat, CEED_NOTRANSPOSE,
6730459ebd3SSebastian Grimberg                                            false, B_mat_in, elem_mat));
674e4065a52SSebastian Grimberg         } else {
675e4065a52SSebastian 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]));
676e4065a52SSebastian Grimberg         }
677eaf62fffSJeremy L Thompson 
6787c1dbaffSSebastian Grimberg         // Transform the element matrix if required
679506b1a0cSSebastian Grimberg         if (elem_rstr_orients_out) {
680506b1a0cSSebastian Grimberg           const bool *elem_orients = &elem_rstr_orients_out[e * elem_size_out];
6811c66c397SJeremy L Thompson 
682506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
683506b1a0cSSebastian Grimberg             const double orient = elem_orients[i] ? -1.0 : 1.0;
684506b1a0cSSebastian Grimberg 
685506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
686506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] *= orient;
6877c1dbaffSSebastian Grimberg             }
6887c1dbaffSSebastian Grimberg           }
689506b1a0cSSebastian Grimberg         } else if (elem_rstr_curl_orients_out) {
690506b1a0cSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_out[e * 3 * elem_size_out];
6911c66c397SJeremy L Thompson 
6927c1dbaffSSebastian Grimberg           // T^T*(B^T*D*B)
693506b1a0cSSebastian Grimberg           memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar));
694506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
695506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
696506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * i + 1] +
697506b1a0cSSebastian Grimberg                                                (i > 0 ? elem_mat_b[(i - 1) * elem_size_in + j] * elem_curl_orients[3 * i - 1] : 0.0) +
698506b1a0cSSebastian Grimberg                                                (i < elem_size_out - 1 ? elem_mat_b[(i + 1) * elem_size_in + j] * elem_curl_orients[3 * i + 3] : 0.0);
6997c1dbaffSSebastian Grimberg             }
7007c1dbaffSSebastian Grimberg           }
701506b1a0cSSebastian Grimberg         }
702506b1a0cSSebastian Grimberg         if (elem_rstr_orients_in) {
703506b1a0cSSebastian Grimberg           const bool *elem_orients = &elem_rstr_orients_in[e * elem_size_in];
704506b1a0cSSebastian Grimberg 
705506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
706506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
707506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] *= elem_orients[j] ? -1.0 : 1.0;
708506b1a0cSSebastian Grimberg             }
709506b1a0cSSebastian Grimberg           }
710506b1a0cSSebastian Grimberg         } else if (elem_rstr_curl_orients_in) {
711506b1a0cSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_in[e * 3 * elem_size_in];
712506b1a0cSSebastian Grimberg 
713506b1a0cSSebastian Grimberg           // (B^T*D*B)*T
714506b1a0cSSebastian Grimberg           memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar));
715506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
716506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
717506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * j + 1] +
718506b1a0cSSebastian Grimberg                                                (j > 0 ? elem_mat_b[i * elem_size_in + j - 1] * elem_curl_orients[3 * j - 1] : 0.0) +
719506b1a0cSSebastian Grimberg                                                (j < elem_size_in - 1 ? elem_mat_b[i * elem_size_in + j + 1] * elem_curl_orients[3 * j + 3] : 0.0);
7207c1dbaffSSebastian Grimberg             }
7217c1dbaffSSebastian Grimberg           }
7227c1dbaffSSebastian Grimberg         }
7237c1dbaffSSebastian Grimberg 
7247c1dbaffSSebastian Grimberg         // Put element matrix in coordinate data structure
725506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < elem_size_out; i++) {
726506b1a0cSSebastian Grimberg           for (CeedInt j = 0; j < elem_size_in; j++) {
727506b1a0cSSebastian Grimberg             vals[offset + count] = elem_mat[i * elem_size_in + j];
728eaf62fffSJeremy L Thompson             count++;
729eaf62fffSJeremy L Thompson           }
730eaf62fffSJeremy L Thompson         }
731eaf62fffSJeremy L Thompson       }
732eaf62fffSJeremy L Thompson     }
733eaf62fffSJeremy L Thompson   }
7346574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing entries");
7352b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(values, &vals));
736eaf62fffSJeremy L Thompson 
737506b1a0cSSebastian Grimberg   // Cleanup
738123d890dSSebastian Grimberg   CeedCall(CeedFree(&BTD_mat));
739123d890dSSebastian Grimberg   CeedCall(CeedFree(&elem_mat));
740506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&elem_mat_b));
741506b1a0cSSebastian Grimberg   if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) {
742506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_in, &elem_rstr_orients_in));
743506b1a0cSSebastian Grimberg   } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
744506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_in, &elem_rstr_curl_orients_in));
745506b1a0cSSebastian Grimberg   }
746506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
747506b1a0cSSebastian Grimberg     if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) {
748506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_out, &elem_rstr_orients_out));
749506b1a0cSSebastian Grimberg     } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
750506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_out, &elem_rstr_curl_orients_out));
751506b1a0cSSebastian Grimberg     }
752506b1a0cSSebastian Grimberg   }
7532b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
7542b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
755eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
756eaf62fffSJeremy L Thompson }
757eaf62fffSJeremy L Thompson 
758eaf62fffSJeremy L Thompson /**
759ca94c3ddSJeremy L Thompson   @brief Count number of entries for assembled `CeedOperator`
760eaf62fffSJeremy L Thompson 
761ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to assemble
762eaf62fffSJeremy L Thompson   @param[out] num_entries Number of entries in assembled representation
763eaf62fffSJeremy L Thompson 
764eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
765eaf62fffSJeremy L Thompson 
766eaf62fffSJeremy L Thompson   @ref Utility
767eaf62fffSJeremy L Thompson **/
768b94338b9SJed Brown static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedSize *num_entries) {
769b275c451SJeremy L Thompson   bool                is_composite;
770506b1a0cSSebastian Grimberg   CeedInt             num_elem_in, elem_size_in, num_comp_in, num_elem_out, elem_size_out, num_comp_out;
771*1203703bSJeremy L Thompson   Ceed                ceed;
772506b1a0cSSebastian Grimberg   CeedElemRestriction rstr_in, rstr_out;
773eaf62fffSJeremy L Thompson 
774*1203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
775b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
776*1203703bSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
777506b1a0cSSebastian Grimberg 
778506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
779506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
780506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
781506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
782506b1a0cSSebastian Grimberg   if (rstr_in != rstr_out) {
783506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
784*1203703bSJeremy L Thompson     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
785506b1a0cSSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
786506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
787506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
788506b1a0cSSebastian Grimberg   } else {
789506b1a0cSSebastian Grimberg     num_elem_out  = num_elem_in;
790506b1a0cSSebastian Grimberg     elem_size_out = elem_size_in;
791506b1a0cSSebastian Grimberg     num_comp_out  = num_comp_in;
792506b1a0cSSebastian Grimberg   }
793506b1a0cSSebastian Grimberg   *num_entries = (CeedSize)elem_size_in * num_comp_in * elem_size_out * num_comp_out * num_elem_in;
794eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
795eaf62fffSJeremy L Thompson }
796eaf62fffSJeremy L Thompson 
797eaf62fffSJeremy L Thompson /**
798ca94c3ddSJeremy L Thompson   @brief Common code for creating a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator`
799eaf62fffSJeremy L Thompson 
800ca94c3ddSJeremy L Thompson   @param[in]  op_fine      Fine grid `CeedOperator`
801ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
802ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid `CeedElemRestriction`
803ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector `CeedBasis`
804ca94c3ddSJeremy L Thompson   @param[in]  basis_c_to_f `CeedBasis` for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction operators
805ca94c3ddSJeremy L Thompson   @param[out] op_coarse    Coarse grid `CeedOperator`
806ca94c3ddSJeremy L Thompson   @param[out] op_prolong   Coarse to fine `CeedOperator`, or `NULL`
807ca94c3ddSJeremy L Thompson   @param[out] op_restrict  Fine to coarse `CeedOperator`, or `NULL`
808eaf62fffSJeremy L Thompson 
809eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
810eaf62fffSJeremy L Thompson 
811eaf62fffSJeremy L Thompson   @ref Developer
812eaf62fffSJeremy L Thompson **/
8132b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
8147758292fSSebastian Grimberg                                             CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
8151c66c397SJeremy L Thompson   bool                is_composite;
816eaf62fffSJeremy L Thompson   Ceed                ceed;
817*1203703bSJeremy L Thompson   CeedInt             num_comp, num_input_fields, num_output_fields;
81885bb9dcfSJeremy L Thompson   CeedVector          mult_vec         = NULL;
8191c66c397SJeremy L Thompson   CeedElemRestriction rstr_p_mult_fine = NULL, rstr_fine = NULL;
820*1203703bSJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
8211c66c397SJeremy L Thompson 
8222b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
823eaf62fffSJeremy L Thompson 
824eaf62fffSJeremy L Thompson   // Check for composite operator
8252b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op_fine, &is_composite));
8266574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported");
827eaf62fffSJeremy L Thompson 
828eaf62fffSJeremy L Thompson   // Coarse Grid
8292b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
830*1203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op_fine, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
831eaf62fffSJeremy L Thompson   // -- Clone input fields
832*1203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
833*1203703bSJeremy L Thompson     char               *field_name;
834*1203703bSJeremy L Thompson     CeedVector          vec;
835*1203703bSJeremy L Thompson     CeedElemRestriction rstr;
836*1203703bSJeremy L Thompson     CeedBasis           basis;
837*1203703bSJeremy L Thompson 
838*1203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetName(input_fields[i], &field_name));
839*1203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(input_fields[i], &vec));
840*1203703bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
841*1203703bSJeremy L Thompson       rstr  = rstr_coarse;
842*1203703bSJeremy L Thompson       basis = basis_coarse;
843*1203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_fine));
844eaf62fffSJeremy L Thompson     } else {
845*1203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr));
846*1203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis));
847eaf62fffSJeremy L Thompson     }
848*1203703bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec));
849eaf62fffSJeremy L Thompson   }
850eaf62fffSJeremy L Thompson   // -- Clone output fields
851*1203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
852*1203703bSJeremy L Thompson     char               *field_name;
853*1203703bSJeremy L Thompson     CeedVector          vec;
854*1203703bSJeremy L Thompson     CeedElemRestriction rstr;
855*1203703bSJeremy L Thompson     CeedBasis           basis;
856*1203703bSJeremy L Thompson 
857*1203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetName(output_fields[i], &field_name));
858*1203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
859*1203703bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
860*1203703bSJeremy L Thompson       rstr  = rstr_coarse;
861*1203703bSJeremy L Thompson       basis = basis_coarse;
862*1203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_fine));
863eaf62fffSJeremy L Thompson     } else {
864*1203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr));
865*1203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis));
866eaf62fffSJeremy L Thompson     }
867*1203703bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec));
868eaf62fffSJeremy L Thompson   }
869af99e877SJeremy L Thompson   // -- Clone QFunctionAssemblyData
8702b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op_fine->qf_assembled, &(*op_coarse)->qf_assembled));
871eaf62fffSJeremy L Thompson 
872eaf62fffSJeremy L Thompson   // Multiplicity vector
8737758292fSSebastian Grimberg   if (op_restrict || op_prolong) {
87485bb9dcfSJeremy L Thompson     CeedVector          mult_e_vec;
8751c66c397SJeremy L Thompson     CeedRestrictionType rstr_type;
87685bb9dcfSJeremy L Thompson 
8777c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr_fine, &rstr_type));
8787c1dbaffSSebastian Grimberg     CeedCheck(rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_UNSUPPORTED,
8797c1dbaffSSebastian Grimberg               "Element restrictions created with CeedElemRestrictionCreateCurlOriented are not supported");
8806574a04fSJeremy L Thompson     CeedCheck(p_mult_fine, ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector");
8817c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnsignedCopy(rstr_fine, &rstr_p_mult_fine));
8822b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec));
8832b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_e_vec, 0.0));
884c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE));
8852b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_vec, 0.0));
886c17ec2beSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE));
8872b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&mult_e_vec));
8882b730f8bSJeremy L Thompson     CeedCall(CeedVectorReciprocal(mult_vec));
88985bb9dcfSJeremy L Thompson   }
890eaf62fffSJeremy L Thompson 
891addd79feSZach Atkins   // Clone name
892addd79feSZach Atkins   bool   has_name = op_fine->name;
893addd79feSZach Atkins   size_t name_len = op_fine->name ? strlen(op_fine->name) : 0;
894addd79feSZach Atkins   CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name));
895addd79feSZach Atkins 
8967758292fSSebastian Grimberg   // Check that coarse to fine basis is provided if prolong/restrict operators are requested
8977758292fSSebastian Grimberg   CeedCheck(basis_c_to_f || (!op_restrict && !op_prolong), ceed, CEED_ERROR_INCOMPATIBLE,
8986574a04fSJeremy L Thompson             "Prolongation or restriction operator creation requires coarse-to-fine basis");
89983d6adf3SZach Atkins 
90085bb9dcfSJeremy L Thompson   // Restriction/Prolongation Operators
9012b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp));
902addd79feSZach Atkins 
903addd79feSZach Atkins   // Restriction
9047758292fSSebastian Grimberg   if (op_restrict) {
905eaf62fffSJeremy L Thompson     CeedInt             *num_comp_r_data;
90685bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_r;
9077758292fSSebastian Grimberg     CeedQFunction        qf_restrict;
90885bb9dcfSJeremy L Thompson 
9097758292fSSebastian Grimberg     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict));
9102b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_r_data));
911eaf62fffSJeremy L Thompson     num_comp_r_data[0] = num_comp;
9122b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r));
9132b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data));
9147758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r));
9152b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_r));
9167758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE));
9177758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE));
9187758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP));
9197758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp));
920eaf62fffSJeremy L Thompson 
9217758292fSSebastian Grimberg     CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict));
9227758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
9237758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
9247758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
925eaf62fffSJeremy L Thompson 
926addd79feSZach Atkins     // Set name
927addd79feSZach Atkins     char *restriction_name;
9281c66c397SJeremy L Thompson 
929addd79feSZach Atkins     CeedCall(CeedCalloc(17 + name_len, &restriction_name));
930addd79feSZach Atkins     sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
9317758292fSSebastian Grimberg     CeedCall(CeedOperatorSetName(*op_restrict, restriction_name));
932addd79feSZach Atkins     CeedCall(CeedFree(&restriction_name));
933addd79feSZach Atkins 
934addd79feSZach Atkins     // Check
9357758292fSSebastian Grimberg     CeedCall(CeedOperatorCheckReady(*op_restrict));
936addd79feSZach Atkins 
937addd79feSZach Atkins     // Cleanup
9387758292fSSebastian Grimberg     CeedCall(CeedQFunctionDestroy(&qf_restrict));
939addd79feSZach Atkins   }
940addd79feSZach Atkins 
941eaf62fffSJeremy L Thompson   // Prolongation
942addd79feSZach Atkins   if (op_prolong) {
943eaf62fffSJeremy L Thompson     CeedInt             *num_comp_p_data;
94485bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_p;
9451c66c397SJeremy L Thompson     CeedQFunction        qf_prolong;
94685bb9dcfSJeremy L Thompson 
94785bb9dcfSJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong));
9482b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_p_data));
949eaf62fffSJeremy L Thompson     num_comp_p_data[0] = num_comp;
9502b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p));
9512b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data));
9522b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p));
9532b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_p));
9542b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP));
9552b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE));
9562b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE));
9572b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp));
958eaf62fffSJeremy L Thompson 
9592b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong));
9602b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
961356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
962356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
963eaf62fffSJeremy L Thompson 
964addd79feSZach Atkins     // Set name
965ea6b5821SJeremy L Thompson     char *prolongation_name;
9661c66c397SJeremy L Thompson 
9672b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(18 + name_len, &prolongation_name));
9682b730f8bSJeremy L Thompson     sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
9692b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name));
9702b730f8bSJeremy L Thompson     CeedCall(CeedFree(&prolongation_name));
971addd79feSZach Atkins 
972addd79feSZach Atkins     // Check
973addd79feSZach Atkins     CeedCall(CeedOperatorCheckReady(*op_prolong));
974addd79feSZach Atkins 
975addd79feSZach Atkins     // Cleanup
976addd79feSZach Atkins     CeedCall(CeedQFunctionDestroy(&qf_prolong));
977ea6b5821SJeremy L Thompson   }
978ea6b5821SJeremy L Thompson 
97958e4b056SJeremy L Thompson   // Check
98058e4b056SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(*op_coarse));
98158e4b056SJeremy L Thompson 
982eaf62fffSJeremy L Thompson   // Cleanup
9832b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_vec));
984c17ec2beSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine));
9852b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis_c_to_f));
986eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
987eaf62fffSJeremy L Thompson }
988eaf62fffSJeremy L Thompson 
989eaf62fffSJeremy L Thompson /**
990eaf62fffSJeremy L Thompson   @brief Build 1D mass matrix and Laplacian with perturbation
991eaf62fffSJeremy L Thompson 
992eaf62fffSJeremy L Thompson   @param[in]  interp_1d   Interpolation matrix in one dimension
993eaf62fffSJeremy L Thompson   @param[in]  grad_1d     Gradient matrix in one dimension
994eaf62fffSJeremy L Thompson   @param[in]  q_weight_1d Quadrature weights in one dimension
995eaf62fffSJeremy L Thompson   @param[in]  P_1d        Number of basis nodes in one dimension
996eaf62fffSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
997eaf62fffSJeremy L Thompson   @param[in]  dim         Dimension of basis
998eaf62fffSJeremy L Thompson   @param[out] mass        Assembled mass matrix in one dimension
999eaf62fffSJeremy L Thompson   @param[out] laplace     Assembled perturbed Laplacian in one dimension
1000eaf62fffSJeremy L Thompson 
1001eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1002eaf62fffSJeremy L Thompson 
1003eaf62fffSJeremy L Thompson   @ref Developer
1004eaf62fffSJeremy L Thompson **/
10052c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
10062c2ea1dbSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d, CeedInt P_1d, CeedInt Q_1d,
10072c2ea1dbSJeremy L Thompson                                 CeedInt dim, CeedScalar *mass, CeedScalar *laplace) {
10082b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
1009eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
1010eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
10112b730f8bSJeremy 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];
1012eaf62fffSJeremy L Thompson       mass[i + j * P_1d] = sum;
1013eaf62fffSJeremy L Thompson     }
10142b730f8bSJeremy L Thompson   }
1015eaf62fffSJeremy L Thompson   // -- Laplacian
10162b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
1017eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
1018eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
10191c66c397SJeremy L Thompson 
10202b730f8bSJeremy 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];
1021eaf62fffSJeremy L Thompson       laplace[i + j * P_1d] = sum;
1022eaf62fffSJeremy L Thompson     }
10232b730f8bSJeremy L Thompson   }
1024eaf62fffSJeremy L Thompson   CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4;
10252b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation;
1026eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1027eaf62fffSJeremy L Thompson }
10282c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
1029eaf62fffSJeremy L Thompson 
1030eaf62fffSJeremy L Thompson /// @}
1031eaf62fffSJeremy L Thompson 
1032eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1033480fae85SJeremy L Thompson /// CeedOperator Backend API
1034480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1035480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend
1036480fae85SJeremy L Thompson /// @{
1037480fae85SJeremy L Thompson 
1038480fae85SJeremy L Thompson /**
1039004e4986SSebastian Grimberg   @brief Select correct basis matrix pointer based on @ref CeedEvalMode
1040004e4986SSebastian Grimberg 
1041004e4986SSebastian Grimberg   @param[in]  basis     `CeedBasis` from which to get the basis matrix
1042004e4986SSebastian Grimberg   @param[in]  eval_mode Current basis evaluation mode
1043004e4986SSebastian Grimberg   @param[in]  identity  Pointer to identity matrix
1044004e4986SSebastian Grimberg   @param[out] basis_ptr `CeedBasis` pointer to set
1045004e4986SSebastian Grimberg 
1046004e4986SSebastian Grimberg   @ref Backend
1047004e4986SSebastian Grimberg **/
1048004e4986SSebastian Grimberg int CeedOperatorGetBasisPointer(CeedBasis basis, CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar **basis_ptr) {
1049004e4986SSebastian Grimberg   switch (eval_mode) {
1050004e4986SSebastian Grimberg     case CEED_EVAL_NONE:
1051004e4986SSebastian Grimberg       *basis_ptr = identity;
1052004e4986SSebastian Grimberg       break;
1053004e4986SSebastian Grimberg     case CEED_EVAL_INTERP:
1054004e4986SSebastian Grimberg       CeedCall(CeedBasisGetInterp(basis, basis_ptr));
1055004e4986SSebastian Grimberg       break;
1056004e4986SSebastian Grimberg     case CEED_EVAL_GRAD:
1057004e4986SSebastian Grimberg       CeedCall(CeedBasisGetGrad(basis, basis_ptr));
1058004e4986SSebastian Grimberg       break;
1059004e4986SSebastian Grimberg     case CEED_EVAL_DIV:
1060004e4986SSebastian Grimberg       CeedCall(CeedBasisGetDiv(basis, basis_ptr));
1061004e4986SSebastian Grimberg       break;
1062004e4986SSebastian Grimberg     case CEED_EVAL_CURL:
1063004e4986SSebastian Grimberg       CeedCall(CeedBasisGetCurl(basis, basis_ptr));
1064004e4986SSebastian Grimberg       break;
1065004e4986SSebastian Grimberg     case CEED_EVAL_WEIGHT:
1066004e4986SSebastian Grimberg       break;  // Caught by QF Assembly
1067004e4986SSebastian Grimberg   }
1068004e4986SSebastian Grimberg   assert(*basis_ptr != NULL);
1069004e4986SSebastian Grimberg   return CEED_ERROR_SUCCESS;
1070004e4986SSebastian Grimberg }
1071004e4986SSebastian Grimberg 
1072004e4986SSebastian Grimberg /**
1073ca94c3ddSJeremy L Thompson   @brief Create point block restriction for active `CeedOperatorField`
1074506b1a0cSSebastian Grimberg 
1075ca94c3ddSJeremy L Thompson   @param[in]  rstr             Original `CeedElemRestriction` for active field
1076ca94c3ddSJeremy L Thompson   @param[out] point_block_rstr Address of the variable where the newly created `CeedElemRestriction` will be stored
1077506b1a0cSSebastian Grimberg 
1078506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1079506b1a0cSSebastian Grimberg 
1080506b1a0cSSebastian Grimberg   @ref Backend
1081506b1a0cSSebastian Grimberg **/
1082506b1a0cSSebastian Grimberg int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *point_block_rstr) {
1083506b1a0cSSebastian Grimberg   Ceed           ceed;
1084506b1a0cSSebastian Grimberg   CeedInt        num_elem, num_comp, shift, elem_size, comp_stride, *point_block_offsets;
1085506b1a0cSSebastian Grimberg   CeedSize       l_size;
1086506b1a0cSSebastian Grimberg   const CeedInt *offsets;
1087506b1a0cSSebastian Grimberg 
1088506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
1089506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
1090506b1a0cSSebastian Grimberg 
1091506b1a0cSSebastian Grimberg   // Expand offsets
1092506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
1093506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1094506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
1095506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
1096506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
1097506b1a0cSSebastian Grimberg   shift = num_comp;
1098506b1a0cSSebastian Grimberg   if (comp_stride != 1) shift *= num_comp;
1099506b1a0cSSebastian Grimberg   CeedCall(CeedCalloc(num_elem * elem_size, &point_block_offsets));
1100506b1a0cSSebastian Grimberg   for (CeedInt i = 0; i < num_elem * elem_size; i++) {
1101506b1a0cSSebastian Grimberg     point_block_offsets[i] = offsets[i] * shift;
1102506b1a0cSSebastian Grimberg   }
1103506b1a0cSSebastian Grimberg 
1104506b1a0cSSebastian Grimberg   // Create new restriction
1105506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
1106506b1a0cSSebastian Grimberg                                      point_block_offsets, point_block_rstr));
1107506b1a0cSSebastian Grimberg 
1108506b1a0cSSebastian Grimberg   // Cleanup
1109506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
1110506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1111506b1a0cSSebastian Grimberg }
1112506b1a0cSSebastian Grimberg 
1113506b1a0cSSebastian Grimberg /**
1114ca94c3ddSJeremy L Thompson   @brief Create object holding `CeedQFunction` assembly data for `CeedOperator`
1115480fae85SJeremy L Thompson 
1116ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedQFunctionAssemblyData`
1117ca94c3ddSJeremy L Thompson   @param[out] data Address of the variable where the newly created `CeedQFunctionAssemblyData` will be stored
1118480fae85SJeremy L Thompson 
1119480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1120480fae85SJeremy L Thompson 
1121480fae85SJeremy L Thompson   @ref Backend
1122480fae85SJeremy L Thompson **/
1123ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) {
11242b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1125480fae85SJeremy L Thompson   (*data)->ref_count = 1;
1126480fae85SJeremy L Thompson   (*data)->ceed      = ceed;
11272b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1128480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1129480fae85SJeremy L Thompson }
1130480fae85SJeremy L Thompson 
1131480fae85SJeremy L Thompson /**
1132ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedQFunctionAssemblyData`
1133480fae85SJeremy L Thompson 
1134ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to increment the reference counter
1135480fae85SJeremy L Thompson 
1136480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1137480fae85SJeremy L Thompson 
1138480fae85SJeremy L Thompson   @ref Backend
1139480fae85SJeremy L Thompson **/
1140480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) {
1141480fae85SJeremy L Thompson   data->ref_count++;
1142480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1143480fae85SJeremy L Thompson }
1144480fae85SJeremy L Thompson 
1145480fae85SJeremy L Thompson /**
1146ca94c3ddSJeremy L Thompson   @brief Set re-use of `CeedQFunctionAssemblyData`
11478b919e6bSJeremy L Thompson 
1148ca94c3ddSJeremy L Thompson   @param[in,out] data       `CeedQFunctionAssemblyData` to mark for reuse
1149ea61e9acSJeremy L Thompson   @param[in]     reuse_data Boolean flag indicating data re-use
11508b919e6bSJeremy L Thompson 
11518b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
11528b919e6bSJeremy L Thompson 
11538b919e6bSJeremy L Thompson   @ref Backend
11548b919e6bSJeremy L Thompson **/
11552b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) {
1156beecbf24SJeremy L Thompson   data->reuse_data        = reuse_data;
1157beecbf24SJeremy L Thompson   data->needs_data_update = true;
1158beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1159beecbf24SJeremy L Thompson }
1160beecbf24SJeremy L Thompson 
1161beecbf24SJeremy L Thompson /**
1162ca94c3ddSJeremy L Thompson   @brief Mark `CeedQFunctionAssemblyData` as stale
1163beecbf24SJeremy L Thompson 
1164ca94c3ddSJeremy L Thompson   @param[in,out] data              `CeedQFunctionAssemblyData` to mark as stale
1165ea61e9acSJeremy L Thompson   @param[in]     needs_data_update Boolean flag indicating if update is needed or completed
1166beecbf24SJeremy L Thompson 
1167beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1168beecbf24SJeremy L Thompson 
1169beecbf24SJeremy L Thompson   @ref Backend
1170beecbf24SJeremy L Thompson **/
11712b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) {
1172beecbf24SJeremy L Thompson   data->needs_data_update = needs_data_update;
11738b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
11748b919e6bSJeremy L Thompson }
11758b919e6bSJeremy L Thompson 
11768b919e6bSJeremy L Thompson /**
1177ca94c3ddSJeremy L Thompson   @brief Determine if `CeedQFunctionAssemblyData` needs update
11788b919e6bSJeremy L Thompson 
1179ca94c3ddSJeremy L Thompson   @param[in]  data             `CeedQFunctionAssemblyData` to mark as stale
11808b919e6bSJeremy L Thompson   @param[out] is_update_needed Boolean flag indicating if re-assembly is required
11818b919e6bSJeremy L Thompson 
11828b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
11838b919e6bSJeremy L Thompson 
11848b919e6bSJeremy L Thompson   @ref Backend
11858b919e6bSJeremy L Thompson **/
11862b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) {
1187beecbf24SJeremy L Thompson   *is_update_needed = !data->reuse_data || data->needs_data_update;
11888b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
11898b919e6bSJeremy L Thompson }
11908b919e6bSJeremy L Thompson 
11918b919e6bSJeremy L Thompson /**
1192ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedQFunctionAssemblyData`.
11934385fb7fSSebastian Grimberg 
1194ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedQFunctionAssemblyDataDestroy().
1195512bb800SJeremy L Thompson 
1196ca94c3ddSJeremy L Thompson   Note: If the value of ` *data_copy` passed to this function is non-`NULL` , then it is assumed that ` *data_copy` is a pointer to a `CeedQFunctionAssemblyData`.
1197ca94c3ddSJeremy L Thompson         This `CeedQFunctionAssemblyData` will be destroyed if ` *data_copy` is the only reference to this `CeedQFunctionAssemblyData`.
1198480fae85SJeremy L Thompson 
1199ca94c3ddSJeremy L Thompson   @param[in]     data      `CeedQFunctionAssemblyData` to copy reference to
1200ea61e9acSJeremy L Thompson   @param[in,out] data_copy Variable to store copied reference
1201480fae85SJeremy L Thompson 
1202480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1203480fae85SJeremy L Thompson 
1204480fae85SJeremy L Thompson   @ref Backend
1205480fae85SJeremy L Thompson **/
12062b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) {
12072b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReference(data));
12082b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy));
1209480fae85SJeremy L Thompson   *data_copy = data;
1210480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1211480fae85SJeremy L Thompson }
1212480fae85SJeremy L Thompson 
1213480fae85SJeremy L Thompson /**
1214ca94c3ddSJeremy L Thompson   @brief Get setup status for internal objects for `CeedQFunctionAssemblyData`
1215480fae85SJeremy L Thompson 
1216ca94c3ddSJeremy L Thompson   @param[in]  data     `CeedQFunctionAssemblyData` to retrieve status
1217480fae85SJeremy L Thompson   @param[out] is_setup Boolean flag for setup status
1218480fae85SJeremy L Thompson 
1219480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1220480fae85SJeremy L Thompson 
1221480fae85SJeremy L Thompson   @ref Backend
1222480fae85SJeremy L Thompson **/
12232b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) {
1224480fae85SJeremy L Thompson   *is_setup = data->is_setup;
1225480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1226480fae85SJeremy L Thompson }
1227480fae85SJeremy L Thompson 
1228480fae85SJeremy L Thompson /**
1229ca94c3ddSJeremy L Thompson   @brief Set internal objects for `CeedQFunctionAssemblyData`
1230480fae85SJeremy L Thompson 
1231ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to set objects
1232ca94c3ddSJeremy L Thompson   @param[in]     vec  `CeedVector` to store assembled `CeedQFunction` at quadrature points
1233ca94c3ddSJeremy L Thompson   @param[in]     rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1234480fae85SJeremy L Thompson 
1235480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1236480fae85SJeremy L Thompson 
1237480fae85SJeremy L Thompson   @ref Backend
1238480fae85SJeremy L Thompson **/
12392b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) {
12402b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(vec, &data->vec));
12412b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr));
1242480fae85SJeremy L Thompson 
1243480fae85SJeremy L Thompson   data->is_setup = true;
1244480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1245480fae85SJeremy L Thompson }
1246480fae85SJeremy L Thompson 
12474dd1a9d2SSebastian Grimberg /**
1248ca94c3ddSJeremy L Thompson   @brief Get internal objects for `CeedQFunctionAssemblyData`
12494dd1a9d2SSebastian Grimberg 
1250ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to set objects
1251ca94c3ddSJeremy L Thompson   @param[out]    vec  `CeedVector` to store assembled `CeedQFunction` at quadrature points
1252ca94c3ddSJeremy L Thompson   @param[out]    rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
12534dd1a9d2SSebastian Grimberg 
12544dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
12554dd1a9d2SSebastian Grimberg 
12564dd1a9d2SSebastian Grimberg   @ref Backend
12574dd1a9d2SSebastian Grimberg **/
12582b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) {
12596574a04fSJeremy L Thompson   CeedCheck(data->is_setup, data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first.");
1260480fae85SJeremy L Thompson 
12612b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(data->vec, vec));
12622b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr));
1263480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1264480fae85SJeremy L Thompson }
1265480fae85SJeremy L Thompson 
1266480fae85SJeremy L Thompson /**
1267ca94c3ddSJeremy L Thompson   @brief Destroy `CeedQFunctionAssemblyData`
1268480fae85SJeremy L Thompson 
1269ca94c3ddSJeremy L Thompson   @param[in,out] data  `CeedQFunctionAssemblyData` to destroy
1270480fae85SJeremy L Thompson 
1271480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1272480fae85SJeremy L Thompson 
1273480fae85SJeremy L Thompson   @ref Backend
1274480fae85SJeremy L Thompson **/
1275480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) {
1276ad6481ceSJeremy L Thompson   if (!*data || --(*data)->ref_count > 0) {
1277ad6481ceSJeremy L Thompson     *data = NULL;
1278ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1279ad6481ceSJeremy L Thompson   }
12802b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
12812b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*data)->vec));
12822b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr));
1283480fae85SJeremy L Thompson 
12842b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1285480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1286480fae85SJeremy L Thompson }
1287480fae85SJeremy L Thompson 
1288ed9e99e6SJeremy L Thompson /**
1289ca94c3ddSJeremy L Thompson   @brief Get `CeedOperatorAssemblyData`
1290ed9e99e6SJeremy L Thompson 
1291ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator` to assemble
1292ca94c3ddSJeremy L Thompson   @param[out] data `CeedQFunctionAssemblyData`
1293ed9e99e6SJeremy L Thompson 
1294ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1295ed9e99e6SJeremy L Thompson 
1296ed9e99e6SJeremy L Thompson   @ref Backend
1297ed9e99e6SJeremy L Thompson **/
12982b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) {
1299ed9e99e6SJeremy L Thompson   if (!op->op_assembled) {
1300ed9e99e6SJeremy L Thompson     CeedOperatorAssemblyData data;
1301ed9e99e6SJeremy L Thompson 
13022b730f8bSJeremy L Thompson     CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data));
1303ed9e99e6SJeremy L Thompson     op->op_assembled = data;
1304ed9e99e6SJeremy L Thompson   }
1305ed9e99e6SJeremy L Thompson   *data = op->op_assembled;
1306ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1307ed9e99e6SJeremy L Thompson }
1308ed9e99e6SJeremy L Thompson 
1309ed9e99e6SJeremy L Thompson /**
1310ca94c3ddSJeremy L Thompson   @brief Create object holding `CeedOperator` assembly data.
1311ba746a46SJeremy L Thompson 
1312ca94c3ddSJeremy L Thompson   The `CeedOperatorAssemblyData` holds an array with references to every active `CeedBasis` used in the `CeedOperator`.
1313ca94c3ddSJeremy L Thompson   An array with references to the corresponding active `CeedElemRestriction` is also stored.
1314ca94c3ddSJeremy L Thompson   For each active `CeedBasis, the `CeedOperatorAssemblyData` holds an array of all input and output @ref CeedEvalMode for this `CeedBasis`.
1315ca94c3ddSJeremy L Thompson   The `CeedOperatorAssemblyData` holds an array of offsets for indexing into the assembled `CeedQFunction` arrays to the row representing each @ref CeedEvalMode.
1316ca94c3ddSJeremy L Thompson   The number of input columns across all active bases for the assembled `CeedQFunction` is also stored.
1317ca94c3ddSJeremy L Thompson   Lastly, the `CeedOperatorAssembly` data holds assembled matrices representing the full action of the `CeedBasis` for all @ref CeedEvalMode.
1318ed9e99e6SJeremy L Thompson 
1319ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperatorAssemblyData`
1320ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator` to be assembled
1321ca94c3ddSJeremy L Thompson   @param[out] data Address of the variable where the newly created `CeedOperatorAssemblyData` will be stored
1322ed9e99e6SJeremy L Thompson 
1323ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1324ed9e99e6SJeremy L Thompson 
1325ed9e99e6SJeremy L Thompson   @ref Backend
1326ed9e99e6SJeremy L Thompson **/
13272b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) {
1328506b1a0cSSebastian Grimberg   CeedInt             num_active_bases_in = 0, num_active_bases_out = 0, offset = 0;
1329506b1a0cSSebastian Grimberg   CeedInt             num_input_fields, *num_eval_modes_in = NULL, num_output_fields, *num_eval_modes_out = NULL;
13301c66c397SJeremy L Thompson   CeedSize          **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL;
13311c66c397SJeremy L Thompson   CeedEvalMode      **eval_modes_in = NULL, **eval_modes_out = NULL;
13321c66c397SJeremy L Thompson   CeedQFunctionField *qf_fields;
13331c66c397SJeremy L Thompson   CeedQFunction       qf;
13341c66c397SJeremy L Thompson   CeedOperatorField  *op_fields;
133501f0e615SJames Wright   bool                is_composite;
133601f0e615SJames Wright 
133701f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
133801f0e615SJames Wright   CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Can only create CeedOperator assembly data for non-composite operators.");
1339437c7c90SJeremy L Thompson 
1340437c7c90SJeremy L Thompson   // Allocate
13412b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1342ed9e99e6SJeremy L Thompson   (*data)->ceed = ceed;
13432b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
1344ed9e99e6SJeremy L Thompson 
1345ed9e99e6SJeremy L Thompson   // Build OperatorAssembly data
13462b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
1347ed9e99e6SJeremy L Thompson 
1348ed9e99e6SJeremy L Thompson   // Determine active input basis
1349004e4986SSebastian Grimberg   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL));
1350004e4986SSebastian Grimberg   CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1351ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1352ed9e99e6SJeremy L Thompson     CeedVector vec;
13531c66c397SJeremy L Thompson 
13542b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1355ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
13567c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
13571c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
13581c66c397SJeremy L Thompson       CeedBasis    basis_in = NULL;
13591c66c397SJeremy L Thompson 
13602b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
13612b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1362352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp));
1363352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1364506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_active_bases_in; i++) {
1365506b1a0cSSebastian Grimberg         if ((*data)->active_bases_in[i] == basis_in) index = i;
1366437c7c90SJeremy L Thompson       }
1367437c7c90SJeremy L Thompson       if (index == -1) {
1368437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_in;
13691c66c397SJeremy L Thompson 
1370506b1a0cSSebastian Grimberg         index = num_active_bases_in;
1371506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_bases_in));
1372506b1a0cSSebastian Grimberg         (*data)->active_bases_in[num_active_bases_in] = NULL;
1373506b1a0cSSebastian Grimberg         CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases_in[num_active_bases_in]));
1374506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_elem_rstrs_in));
1375506b1a0cSSebastian Grimberg         (*data)->active_elem_rstrs_in[num_active_bases_in] = NULL;
1376437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in));
1377506b1a0cSSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs_in[num_active_bases_in]));
1378506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &num_eval_modes_in));
1379437c7c90SJeremy L Thompson         num_eval_modes_in[index] = 0;
1380506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_modes_in));
1381437c7c90SJeremy L Thompson         eval_modes_in[index] = NULL;
1382506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_mode_offsets_in));
1383437c7c90SJeremy L Thompson         eval_mode_offsets_in[index] = NULL;
1384506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->assembled_bases_in));
1385437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index] = NULL;
1386506b1a0cSSebastian Grimberg         num_active_bases_in++;
1387437c7c90SJeremy L Thompson       }
1388352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1389352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1390352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_modes_in[index]));
1391352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_mode_offsets_in[index]));
1392352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1393437c7c90SJeremy L Thompson           eval_modes_in[index][num_eval_modes_in[index] + d]        = eval_mode;
1394437c7c90SJeremy L Thompson           eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset;
1395352a5e7cSSebastian Grimberg           offset += num_comp;
1396ed9e99e6SJeremy L Thompson         }
1397352a5e7cSSebastian Grimberg         num_eval_modes_in[index] += q_comp;
1398ed9e99e6SJeremy L Thompson       }
1399ed9e99e6SJeremy L Thompson     }
1400ed9e99e6SJeremy L Thompson   }
1401ed9e99e6SJeremy L Thompson 
1402ed9e99e6SJeremy L Thompson   // Determine active output basis
14032b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields));
14042b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1405437c7c90SJeremy L Thompson   offset = 0;
1406ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1407ed9e99e6SJeremy L Thompson     CeedVector vec;
14081c66c397SJeremy L Thompson 
14092b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1410ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
14117c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
14121c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
14131c66c397SJeremy L Thompson       CeedBasis    basis_out = NULL;
14141c66c397SJeremy L Thompson 
1415437c7c90SJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
14162b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1417352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_out, &num_comp));
1418352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1419506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_active_bases_out; i++) {
1420506b1a0cSSebastian Grimberg         if ((*data)->active_bases_out[i] == basis_out) index = i;
1421437c7c90SJeremy L Thompson       }
1422437c7c90SJeremy L Thompson       if (index == -1) {
1423437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_out;
14241c66c397SJeremy L Thompson 
1425506b1a0cSSebastian Grimberg         index = num_active_bases_out;
1426506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_bases_out));
1427506b1a0cSSebastian Grimberg         (*data)->active_bases_out[num_active_bases_out] = NULL;
1428506b1a0cSSebastian Grimberg         CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases_out[num_active_bases_out]));
1429506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_elem_rstrs_out));
1430506b1a0cSSebastian Grimberg         (*data)->active_elem_rstrs_out[num_active_bases_out] = NULL;
1431437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out));
1432506b1a0cSSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs_out[num_active_bases_out]));
1433506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &num_eval_modes_out));
1434437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1435506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_modes_out));
1436437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1437506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_mode_offsets_out));
1438437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1439506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->assembled_bases_out));
1440437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1441506b1a0cSSebastian Grimberg         num_active_bases_out++;
1442437c7c90SJeremy L Thompson       }
1443352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1444352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1445352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_modes_out[index]));
1446352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_mode_offsets_out[index]));
1447352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1448437c7c90SJeremy L Thompson           eval_modes_out[index][num_eval_modes_out[index] + d]        = eval_mode;
1449437c7c90SJeremy L Thompson           eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset;
1450352a5e7cSSebastian Grimberg           offset += num_comp;
1451ed9e99e6SJeremy L Thompson         }
1452352a5e7cSSebastian Grimberg         num_eval_modes_out[index] += q_comp;
1453ed9e99e6SJeremy L Thompson       }
1454ed9e99e6SJeremy L Thompson     }
1455ed9e99e6SJeremy L Thompson   }
1456506b1a0cSSebastian Grimberg   (*data)->num_active_bases_in   = num_active_bases_in;
145727789c4aSJed Brown   (*data)->num_eval_modes_in     = num_eval_modes_in;
145827789c4aSJed Brown   (*data)->eval_modes_in         = eval_modes_in;
145927789c4aSJed Brown   (*data)->eval_mode_offsets_in  = eval_mode_offsets_in;
1460506b1a0cSSebastian Grimberg   (*data)->num_active_bases_out  = num_active_bases_out;
1461437c7c90SJeremy L Thompson   (*data)->num_eval_modes_out    = num_eval_modes_out;
1462437c7c90SJeremy L Thompson   (*data)->eval_modes_out        = eval_modes_out;
1463437c7c90SJeremy L Thompson   (*data)->eval_mode_offsets_out = eval_mode_offsets_out;
1464506b1a0cSSebastian Grimberg   (*data)->num_output_components = offset;
1465ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1466ed9e99e6SJeremy L Thompson }
1467ed9e99e6SJeremy L Thompson 
1468ed9e99e6SJeremy L Thompson /**
1469ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` @ref CeedEvalMode for assembly.
1470ba746a46SJeremy L Thompson 
1471ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1472ed9e99e6SJeremy L Thompson 
1473ca94c3ddSJeremy L Thompson   @param[in]  data                  `CeedOperatorAssemblyData`
1474506b1a0cSSebastian Grimberg   @param[out] num_active_bases_in   Total number of active bases for input
1475ca94c3ddSJeremy L Thompson   @param[out] num_eval_modes_in     Pointer to hold array of numbers of input @ref CeedEvalMode, or `NULL`.
1476ca94c3ddSJeremy L Thompson                                       `eval_modes_in[0]` holds an array of eval modes for the first active `CeedBasis`.
1477ca94c3ddSJeremy L Thompson   @param[out] eval_modes_in         Pointer to hold arrays of input @ref CeedEvalMode, or `NULL`
1478ca94c3ddSJeremy L Thompson   @param[out] eval_mode_offsets_in  Pointer to hold arrays of input offsets at each quadrature point
1479506b1a0cSSebastian Grimberg   @param[out] num_active_bases_out  Total number of active bases for output
1480ca94c3ddSJeremy L Thompson   @param[out] num_eval_modes_out    Pointer to hold array of numbers of output @ref CeedEvalMode, or `NULL`
1481ca94c3ddSJeremy L Thompson   @param[out] eval_modes_out        Pointer to hold arrays of output @ref CeedEvalMode, or `NULL`
1482437c7c90SJeremy L Thompson   @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point
1483ca94c3ddSJeremy L Thompson   @param[out] num_output_components The number of columns in the assembled `CeedQFunction` matrix for each quadrature point, including contributions of all active bases
1484ed9e99e6SJeremy L Thompson 
1485ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1486ed9e99e6SJeremy L Thompson 
1487ed9e99e6SJeremy L Thompson   @ref Backend
1488ed9e99e6SJeremy L Thompson **/
1489506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedInt **num_eval_modes_in,
1490506b1a0cSSebastian Grimberg                                          const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt *num_active_bases_out,
1491506b1a0cSSebastian Grimberg                                          CeedInt **num_eval_modes_out, const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out,
1492506b1a0cSSebastian Grimberg                                          CeedSize *num_output_components) {
1493506b1a0cSSebastian Grimberg   if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in;
1494437c7c90SJeremy L Thompson   if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in;
1495437c7c90SJeremy L Thompson   if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in;
1496437c7c90SJeremy L Thompson   if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in;
1497506b1a0cSSebastian Grimberg   if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out;
1498437c7c90SJeremy L Thompson   if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out;
1499437c7c90SJeremy L Thompson   if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out;
1500437c7c90SJeremy L Thompson   if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out;
1501437c7c90SJeremy L Thompson   if (num_output_components) *num_output_components = data->num_output_components;
1502ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1503ed9e99e6SJeremy L Thompson }
1504ed9e99e6SJeremy L Thompson 
1505ed9e99e6SJeremy L Thompson /**
1506ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` `CeedBasis` data for assembly.
1507ba746a46SJeremy L Thompson 
1508ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1509ed9e99e6SJeremy L Thompson 
1510ca94c3ddSJeremy L Thompson   @param[in]  data                 `CeedOperatorAssemblyData`
1511ca94c3ddSJeremy L Thompson   @param[out] num_active_bases_in  Number of active input bases, or `NULL`
1512ca94c3ddSJeremy L Thompson   @param[out] active_bases_in      Pointer to hold active input `CeedBasis`, or `NULL`
1513ca94c3ddSJeremy L Thompson   @param[out] assembled_bases_in   Pointer to hold assembled active input `B` , or `NULL`
1514ca94c3ddSJeremy L Thompson   @param[out] num_active_bases_out Number of active output bases, or `NULL`
1515ca94c3ddSJeremy L Thompson   @param[out] active_bases_out     Pointer to hold active output `CeedBasis`, or `NULL`
1516ca94c3ddSJeremy L Thompson   @param[out] assembled_bases_out  Pointer to hold assembled active output `B` , or `NULL`
1517ed9e99e6SJeremy L Thompson 
1518ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1519ed9e99e6SJeremy L Thompson 
1520ed9e99e6SJeremy L Thompson   @ref Backend
1521ed9e99e6SJeremy L Thompson **/
1522506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedBasis **active_bases_in,
1523506b1a0cSSebastian Grimberg                                      const CeedScalar ***assembled_bases_in, CeedInt *num_active_bases_out, CeedBasis **active_bases_out,
1524506b1a0cSSebastian Grimberg                                      const CeedScalar ***assembled_bases_out) {
1525ed9e99e6SJeremy L Thompson   // Assemble B_in, B_out if needed
1526437c7c90SJeremy L Thompson   if (assembled_bases_in && !data->assembled_bases_in[0]) {
1527437c7c90SJeremy L Thompson     CeedInt num_qpts;
1528437c7c90SJeremy L Thompson 
1529506b1a0cSSebastian Grimberg     if (data->active_bases_in[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[0], &num_qpts));
1530506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_in[0], &num_qpts));
1531506b1a0cSSebastian Grimberg     for (CeedInt b = 0; b < data->num_active_bases_in; b++) {
15321c66c397SJeremy L Thompson       bool        has_eval_none = false;
1533352a5e7cSSebastian Grimberg       CeedInt     num_nodes;
1534437c7c90SJeremy L Thompson       CeedScalar *B_in = NULL, *identity = NULL;
1535ed9e99e6SJeremy L Thompson 
1536506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[b], &num_nodes));
1537352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_in[b], &B_in));
1538ed9e99e6SJeremy L Thompson 
1539437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) {
1540437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE);
1541ed9e99e6SJeremy L Thompson       }
1542ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1543352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1544352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1545352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1546ed9e99e6SJeremy L Thompson         }
1547ed9e99e6SJeremy L Thompson       }
1548ed9e99e6SJeremy L Thompson 
1549ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1550352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1551352a5e7cSSebastian Grimberg           CeedInt      d_in              = 0, q_comp_in;
1552352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE;
15531c66c397SJeremy L Thompson 
1554437c7c90SJeremy L Thompson           for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) {
1555437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_in[b] * q;
1556437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
15571c66c397SJeremy L Thompson 
1558506b1a0cSSebastian Grimberg             CeedCall(CeedOperatorGetBasisPointer(data->active_bases_in[b], data->eval_modes_in[b][e_in], identity, &B));
1559506b1a0cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_in[b], data->eval_modes_in[b][e_in], &q_comp_in));
1560352a5e7cSSebastian Grimberg             if (q_comp_in > 1) {
1561352a5e7cSSebastian Grimberg               if (e_in == 0 || data->eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0;
1562352a5e7cSSebastian Grimberg               else B = &B[(++d_in) * num_qpts * num_nodes];
1563352a5e7cSSebastian Grimberg             }
1564352a5e7cSSebastian Grimberg             eval_mode_in_prev                 = data->eval_modes_in[b][e_in];
1565352a5e7cSSebastian Grimberg             B_in[(qq + e_in) * num_nodes + n] = B[q * num_nodes + n];
1566ed9e99e6SJeremy L Thompson           }
1567ed9e99e6SJeremy L Thompson         }
1568ed9e99e6SJeremy L Thompson       }
15697c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1570437c7c90SJeremy L Thompson       data->assembled_bases_in[b] = B_in;
1571437c7c90SJeremy L Thompson     }
1572ed9e99e6SJeremy L Thompson   }
1573ed9e99e6SJeremy L Thompson 
1574437c7c90SJeremy L Thompson   if (assembled_bases_out && !data->assembled_bases_out[0]) {
1575437c7c90SJeremy L Thompson     CeedInt num_qpts;
1576437c7c90SJeremy L Thompson 
1577506b1a0cSSebastian Grimberg     if (data->active_bases_out[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[0], &num_qpts));
1578506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_out[0], &num_qpts));
1579506b1a0cSSebastian Grimberg     for (CeedInt b = 0; b < data->num_active_bases_out; b++) {
1580ed9e99e6SJeremy L Thompson       bool        has_eval_none = false;
15811c66c397SJeremy L Thompson       CeedInt     num_nodes;
1582437c7c90SJeremy L Thompson       CeedScalar *B_out = NULL, *identity = NULL;
1583ed9e99e6SJeremy L Thompson 
1584506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[b], &num_nodes));
1585352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_out[b], &B_out));
1586ed9e99e6SJeremy L Thompson 
1587437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) {
1588437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE);
1589ed9e99e6SJeremy L Thompson       }
1590ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1591352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1592352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1593352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1594ed9e99e6SJeremy L Thompson         }
1595ed9e99e6SJeremy L Thompson       }
1596ed9e99e6SJeremy L Thompson 
1597ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1598352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1599352a5e7cSSebastian Grimberg           CeedInt      d_out              = 0, q_comp_out;
1600352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
16011c66c397SJeremy L Thompson 
1602437c7c90SJeremy L Thompson           for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) {
1603437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_out[b] * q;
1604437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
16051c66c397SJeremy L Thompson 
1606506b1a0cSSebastian Grimberg             CeedCall(CeedOperatorGetBasisPointer(data->active_bases_out[b], data->eval_modes_out[b][e_out], identity, &B));
1607506b1a0cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_out[b], data->eval_modes_out[b][e_out], &q_comp_out));
1608352a5e7cSSebastian Grimberg             if (q_comp_out > 1) {
1609352a5e7cSSebastian Grimberg               if (e_out == 0 || data->eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0;
1610352a5e7cSSebastian Grimberg               else B = &B[(++d_out) * num_qpts * num_nodes];
1611352a5e7cSSebastian Grimberg             }
1612352a5e7cSSebastian Grimberg             eval_mode_out_prev                  = data->eval_modes_out[b][e_out];
1613352a5e7cSSebastian Grimberg             B_out[(qq + e_out) * num_nodes + n] = B[q * num_nodes + n];
1614ed9e99e6SJeremy L Thompson           }
1615ed9e99e6SJeremy L Thompson         }
1616ed9e99e6SJeremy L Thompson       }
16177c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1618437c7c90SJeremy L Thompson       data->assembled_bases_out[b] = B_out;
1619437c7c90SJeremy L Thompson     }
1620ed9e99e6SJeremy L Thompson   }
1621ed9e99e6SJeremy L Thompson 
1622437c7c90SJeremy L Thompson   // Pass out assembled data
1623506b1a0cSSebastian Grimberg   if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in;
1624506b1a0cSSebastian Grimberg   if (active_bases_in) *active_bases_in = data->active_bases_in;
1625437c7c90SJeremy L Thompson   if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in;
1626506b1a0cSSebastian Grimberg   if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out;
1627506b1a0cSSebastian Grimberg   if (active_bases_out) *active_bases_out = data->active_bases_out;
1628437c7c90SJeremy L Thompson   if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out;
1629437c7c90SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1630437c7c90SJeremy L Thompson }
1631437c7c90SJeremy L Thompson 
1632437c7c90SJeremy L Thompson /**
1633ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` `CeedBasis` data for assembly.
1634ba746a46SJeremy L Thompson 
1635ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1636437c7c90SJeremy L Thompson 
1637ca94c3ddSJeremy L Thompson   @param[in]  data                      `CeedOperatorAssemblyData`
1638ca94c3ddSJeremy L Thompson   @param[out] num_active_elem_rstrs_in  Number of active input element restrictions, or `NULL`
1639ca94c3ddSJeremy L Thompson   @param[out] active_elem_rstrs_in      Pointer to hold active input `CeedElemRestriction`, or `NULL`
1640ca94c3ddSJeremy L Thompson   @param[out] num_active_elem_rstrs_out Number of active output element restrictions, or `NULL`
1641ca94c3ddSJeremy L Thompson   @param[out] active_elem_rstrs_out     Pointer to hold active output `CeedElemRestriction`, or `NULL`
1642437c7c90SJeremy L Thompson 
1643437c7c90SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1644437c7c90SJeremy L Thompson 
1645437c7c90SJeremy L Thompson   @ref Backend
1646437c7c90SJeremy L Thompson **/
1647506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs_in,
1648506b1a0cSSebastian Grimberg                                                 CeedElemRestriction **active_elem_rstrs_in, CeedInt *num_active_elem_rstrs_out,
1649506b1a0cSSebastian Grimberg                                                 CeedElemRestriction **active_elem_rstrs_out) {
1650506b1a0cSSebastian Grimberg   if (num_active_elem_rstrs_in) *num_active_elem_rstrs_in = data->num_active_bases_in;
1651506b1a0cSSebastian Grimberg   if (active_elem_rstrs_in) *active_elem_rstrs_in = data->active_elem_rstrs_in;
1652506b1a0cSSebastian Grimberg   if (num_active_elem_rstrs_out) *num_active_elem_rstrs_out = data->num_active_bases_out;
1653506b1a0cSSebastian Grimberg   if (active_elem_rstrs_out) *active_elem_rstrs_out = data->active_elem_rstrs_out;
1654ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1655ed9e99e6SJeremy L Thompson }
1656ed9e99e6SJeremy L Thompson 
1657ed9e99e6SJeremy L Thompson /**
1658ca94c3ddSJeremy L Thompson   @brief Destroy `CeedOperatorAssemblyData`
1659ed9e99e6SJeremy L Thompson 
1660ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedOperatorAssemblyData` to destroy
1661ed9e99e6SJeremy L Thompson 
1662ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1663ed9e99e6SJeremy L Thompson 
1664ed9e99e6SJeremy L Thompson   @ref Backend
1665ed9e99e6SJeremy L Thompson **/
1666ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) {
1667ad6481ceSJeremy L Thompson   if (!*data) {
1668ad6481ceSJeremy L Thompson     *data = NULL;
1669ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1670ad6481ceSJeremy L Thompson   }
16712b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
1672506b1a0cSSebastian Grimberg   for (CeedInt b = 0; b < (*data)->num_active_bases_in; b++) {
1673506b1a0cSSebastian Grimberg     CeedCall(CeedBasisDestroy(&(*data)->active_bases_in[b]));
1674506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_in[b]));
1675437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_in[b]));
1676437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b]));
1677437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_in[b]));
1678506b1a0cSSebastian Grimberg   }
1679506b1a0cSSebastian Grimberg   for (CeedInt b = 0; b < (*data)->num_active_bases_out; b++) {
1680506b1a0cSSebastian Grimberg     CeedCall(CeedBasisDestroy(&(*data)->active_bases_out[b]));
1681506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_out[b]));
1682506b1a0cSSebastian Grimberg     CeedCall(CeedFree(&(*data)->eval_modes_out[b]));
1683506b1a0cSSebastian Grimberg     CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b]));
1684437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_out[b]));
1685437c7c90SJeremy L Thompson   }
1686506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_bases_in));
1687506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_bases_out));
1688506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_elem_rstrs_in));
1689506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_elem_rstrs_out));
1690437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_in));
1691437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_out));
1692437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_in));
1693437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_out));
1694437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_in));
1695437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_out));
1696437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_in));
1697437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_out));
1698ed9e99e6SJeremy L Thompson 
16992b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1700ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1701ed9e99e6SJeremy L Thompson }
1702ed9e99e6SJeremy L Thompson 
17034dd1a9d2SSebastian Grimberg /**
1704ca94c3ddSJeremy L Thompson   @brief Retrieve fallback `CeedOperator` with a reference `Ceed` for advanced `CeedOperator` functionality
17054dd1a9d2SSebastian Grimberg 
1706ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to retrieve fallback for
1707ca94c3ddSJeremy L Thompson   @param[out] op_fallback Fallback `CeedOperator`
17084dd1a9d2SSebastian Grimberg 
17094dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
17104dd1a9d2SSebastian Grimberg 
17114dd1a9d2SSebastian Grimberg   @ref Backend
17124dd1a9d2SSebastian Grimberg **/
17134dd1a9d2SSebastian Grimberg int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) {
17144dd1a9d2SSebastian Grimberg   // Create if needed
17154dd1a9d2SSebastian Grimberg   if (!op->op_fallback) CeedCall(CeedOperatorCreateFallback(op));
17164dd1a9d2SSebastian Grimberg   if (op->op_fallback) {
17174dd1a9d2SSebastian Grimberg     bool is_debug;
1718*1203703bSJeremy L Thompson     Ceed ceed;
17194dd1a9d2SSebastian Grimberg 
17204dd1a9d2SSebastian Grimberg     CeedCall(CeedOperatorGetCeed(op, &ceed));
1721*1203703bSJeremy L Thompson     CeedCall(CeedIsDebug(ceed, &is_debug));
1722*1203703bSJeremy L Thompson     if (is_debug) {
1723*1203703bSJeremy L Thompson       Ceed        ceed_fallback;
1724*1203703bSJeremy L Thompson       const char *resource, *resource_fallback;
1725*1203703bSJeremy L Thompson 
17264dd1a9d2SSebastian Grimberg       CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
17274dd1a9d2SSebastian Grimberg       CeedCall(CeedGetResource(ceed, &resource));
17284dd1a9d2SSebastian Grimberg       CeedCall(CeedGetResource(ceed_fallback, &resource_fallback));
17294dd1a9d2SSebastian Grimberg 
17304dd1a9d2SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n");
1731249f8407SJeremy L Thompson       CeedDebug(ceed, "Falling back from %s operator at address %p to %s operator at address %p\n", resource, op, resource_fallback, op->op_fallback);
17324dd1a9d2SSebastian Grimberg     }
17334dd1a9d2SSebastian Grimberg   }
17344dd1a9d2SSebastian Grimberg   *op_fallback = op->op_fallback;
17354dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
17364dd1a9d2SSebastian Grimberg }
17374dd1a9d2SSebastian Grimberg 
17384dd1a9d2SSebastian Grimberg /**
1739ca94c3ddSJeremy L Thompson   @brief Get the parent `CeedOperator` for a fallback `CeedOperator`
17404dd1a9d2SSebastian Grimberg 
1741ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` context
1742ca94c3ddSJeremy L Thompson   @param[out] parent Variable to store parent `CeedOperator` context
17434dd1a9d2SSebastian Grimberg 
17444dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
17454dd1a9d2SSebastian Grimberg 
17464dd1a9d2SSebastian Grimberg   @ref Backend
17474dd1a9d2SSebastian Grimberg **/
17484dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParent(CeedOperator op, CeedOperator *parent) {
17494dd1a9d2SSebastian Grimberg   *parent = op->op_fallback_parent ? op->op_fallback_parent : NULL;
17504dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
17514dd1a9d2SSebastian Grimberg }
17524dd1a9d2SSebastian Grimberg 
17534dd1a9d2SSebastian Grimberg /**
1754ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` context of the parent `CeedOperator` for a fallback `CeedOperator`
17554dd1a9d2SSebastian Grimberg 
1756ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` context
1757ca94c3ddSJeremy L Thompson   @param[out] parent Variable to store parent `Ceed` context
17584dd1a9d2SSebastian Grimberg 
17594dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
17604dd1a9d2SSebastian Grimberg 
17614dd1a9d2SSebastian Grimberg   @ref Backend
17624dd1a9d2SSebastian Grimberg **/
17634dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParentCeed(CeedOperator op, Ceed *parent) {
17644dd1a9d2SSebastian Grimberg   *parent = op->op_fallback_parent ? op->op_fallback_parent->ceed : op->ceed;
17654dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
17664dd1a9d2SSebastian Grimberg }
17674dd1a9d2SSebastian Grimberg 
1768480fae85SJeremy L Thompson /// @}
1769480fae85SJeremy L Thompson 
1770480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1771eaf62fffSJeremy L Thompson /// CeedOperator Public API
1772eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1773eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser
1774eaf62fffSJeremy L Thompson /// @{
1775eaf62fffSJeremy L Thompson 
1776eaf62fffSJeremy L Thompson /**
1777ca94c3ddSJeremy L Thompson   @brief Assemble a linear `CeedQFunction` associated with a `CeedOperator`.
1778eaf62fffSJeremy L Thompson 
1779ca94c3ddSJeremy L Thompson   This returns a `CeedVector` containing a matrix at each quadrature point providing the action of the `CeedQFunction` associated with the `CeedOperator`.
1780ca94c3ddSJeremy L Thompson   The vector `assembled` is of shape `[num_elements, num_input_fields, num_output_fields, num_quad_points]` and contains column-major matrices representing the action of the `CeedQFunction` for a corresponding quadrature point on an element.
1781859c15bbSJames Wright 
1782ca94c3ddSJeremy L Thompson   Inputs and outputs are in the order provided by the user when adding `CeedOperator` fields.
1783ca94c3ddSJeremy L Thompson   For example, a `CeedQFunction` with inputs `u` and `gradu` and outputs `gradv` and `v` , provided in that order, would result in an assembled `CeedQFunction` that consists of `(1 + dim) x (dim + 1)` matrices at each quadrature point acting on the input ` [u, du_0, du_1]` and producing the output `[dv_0, dv_1, v]`.
1784eaf62fffSJeremy L Thompson 
1785ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1786f04ea552SJeremy L Thompson 
1787ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1788ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points
1789ca94c3ddSJeremy L Thompson   @param[out] rstr      `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1790ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1791eaf62fffSJeremy L Thompson 
1792eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1793eaf62fffSJeremy L Thompson 
1794eaf62fffSJeremy L Thompson   @ref User
1795eaf62fffSJeremy L Thompson **/
17962b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
17972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1798eaf62fffSJeremy L Thompson 
1799eaf62fffSJeremy L Thompson   if (op->LinearAssembleQFunction) {
1800d04bbc78SJeremy L Thompson     // Backend version
18012b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request));
1802eaf62fffSJeremy L Thompson   } else {
1803d04bbc78SJeremy L Thompson     // Operator fallback
1804*1203703bSJeremy L Thompson     Ceed         ceed;
1805d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1806d04bbc78SJeremy L Thompson 
1807*1203703bSJeremy L Thompson     CeedCall(CeedOperatorGetCeed(op, &ceed));
18082b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
18096574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request));
1810*1203703bSJeremy L Thompson     else return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction");
181170a7ffb3SJeremy L Thompson   }
1812eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1813eaf62fffSJeremy L Thompson }
181470a7ffb3SJeremy L Thompson 
181570a7ffb3SJeremy L Thompson /**
1816ca94c3ddSJeremy L Thompson   @brief Assemble `CeedQFunction` and store result internally.
18174385fb7fSSebastian Grimberg 
1818ea61e9acSJeremy L Thompson   Return copied references of stored data to the caller.
1819ea61e9acSJeremy L Thompson   Caller is responsible for ownership and destruction of the copied references.
1820ca94c3ddSJeremy L Thompson   See also @ref CeedOperatorLinearAssembleQFunction().
182170a7ffb3SJeremy L Thompson 
1822ca94c3ddSJeremy 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.
1823c5f45aeaSJeremy L Thompson         These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object.
1824c5f45aeaSJeremy L Thompson 
1825ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1826ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points
1827ca94c3ddSJeremy L Thompson   @param[out] rstr      `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1828ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
182970a7ffb3SJeremy L Thompson 
183070a7ffb3SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
183170a7ffb3SJeremy L Thompson 
183270a7ffb3SJeremy L Thompson   @ref User
183370a7ffb3SJeremy L Thompson **/
18342b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
1835b05f7e9fSJeremy L Thompson   int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *) = NULL;
1836b05f7e9fSJeremy L Thompson   CeedOperator op_assemble                                                                           = NULL;
1837bb229da9SJeremy L Thompson   CeedOperator op_fallback_parent                                                                    = NULL;
1838b05f7e9fSJeremy L Thompson 
18392b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
184070a7ffb3SJeremy L Thompson 
1841b05f7e9fSJeremy L Thompson   // Determine if fallback parent or operator has implementation
1842bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParent(op, &op_fallback_parent));
1843bb229da9SJeremy L Thompson   if (op_fallback_parent && op_fallback_parent->LinearAssembleQFunctionUpdate) {
1844b05f7e9fSJeremy L Thompson     // -- Backend version for op fallback parent is faster, if it exists
1845bb229da9SJeremy L Thompson     LinearAssembleQFunctionUpdate = op_fallback_parent->LinearAssembleQFunctionUpdate;
1846bb229da9SJeremy L Thompson     op_assemble                   = op_fallback_parent;
1847b05f7e9fSJeremy L Thompson   } else if (op->LinearAssembleQFunctionUpdate) {
1848b05f7e9fSJeremy L Thompson     // -- Backend version for op
1849b05f7e9fSJeremy L Thompson     LinearAssembleQFunctionUpdate = op->LinearAssembleQFunctionUpdate;
1850b05f7e9fSJeremy L Thompson     op_assemble                   = op;
1851b05f7e9fSJeremy L Thompson   }
1852b05f7e9fSJeremy L Thompson 
1853b05f7e9fSJeremy L Thompson   // Assemble QFunction
1854b05f7e9fSJeremy L Thompson   if (LinearAssembleQFunctionUpdate) {
1855b05f7e9fSJeremy L Thompson     // Backend or fallback parent version
1856480fae85SJeremy L Thompson     bool                qf_assembled_is_setup;
18572efa2d85SJeremy L Thompson     CeedVector          assembled_vec  = NULL;
18582efa2d85SJeremy L Thompson     CeedElemRestriction assembled_rstr = NULL;
1859480fae85SJeremy L Thompson 
18602b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataIsSetup(op->qf_assembled, &qf_assembled_is_setup));
1861480fae85SJeremy L Thompson     if (qf_assembled_is_setup) {
1862d04bbc78SJeremy L Thompson       bool update_needed;
1863d04bbc78SJeremy L Thompson 
18642b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataGetObjects(op->qf_assembled, &assembled_vec, &assembled_rstr));
18652b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(op->qf_assembled, &update_needed));
1866b05f7e9fSJeremy L Thompson       if (update_needed) CeedCall(LinearAssembleQFunctionUpdate(op_assemble, assembled_vec, assembled_rstr, request));
186770a7ffb3SJeremy L Thompson     } else {
1868b05f7e9fSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunction(op_assemble, &assembled_vec, &assembled_rstr, request));
18692b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataSetObjects(op->qf_assembled, assembled_vec, assembled_rstr));
187070a7ffb3SJeremy L Thompson     }
18712b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, false));
18722efa2d85SJeremy L Thompson 
1873d04bbc78SJeremy L Thompson     // Copy reference from internally held copy
18742b730f8bSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled));
18752b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr));
1876c5f45aeaSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled_vec));
18772b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&assembled_rstr));
187870a7ffb3SJeremy L Thompson   } else {
1879d04bbc78SJeremy L Thompson     // Operator fallback
1880*1203703bSJeremy L Thompson     Ceed         ceed;
1881d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1882d04bbc78SJeremy L Thompson 
1883*1203703bSJeremy L Thompson     CeedCall(CeedOperatorGetCeed(op, &ceed));
18842b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
18856574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request));
1886*1203703bSJeremy L Thompson     else return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate");
188770a7ffb3SJeremy L Thompson   }
188870a7ffb3SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1889eaf62fffSJeremy L Thompson }
1890eaf62fffSJeremy L Thompson 
1891eaf62fffSJeremy L Thompson /**
1892ca94c3ddSJeremy L Thompson   @brief Assemble the diagonal of a square linear `CeedOperator`
1893eaf62fffSJeremy L Thompson 
1894ca94c3ddSJeremy L Thompson   This overwrites a `CeedVector` with the diagonal of a linear `CeedOperator`.
1895eaf62fffSJeremy L Thompson 
1896ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
1897eaf62fffSJeremy L Thompson 
1898ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1899f04ea552SJeremy L Thompson 
1900ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1901ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal
1902ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1903eaf62fffSJeremy L Thompson 
1904eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1905eaf62fffSJeremy L Thompson 
1906eaf62fffSJeremy L Thompson   @ref User
1907eaf62fffSJeremy L Thompson **/
19082b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1909f3d47e36SJeremy L Thompson   bool     is_composite;
19101c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
1911*1203703bSJeremy L Thompson   Ceed     ceed;
19121c66c397SJeremy L Thompson 
1913*1203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
19142b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1915f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1916eaf62fffSJeremy L Thompson 
19172b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
1918*1203703bSJeremy L Thompson   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1919c9366a6bSJeremy L Thompson 
1920f3d47e36SJeremy L Thompson   // Early exit for empty operator
1921f3d47e36SJeremy L Thompson   if (!is_composite) {
1922f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1923f3d47e36SJeremy L Thompson 
1924f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1925f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1926f3d47e36SJeremy L Thompson   }
1927f3d47e36SJeremy L Thompson 
1928eaf62fffSJeremy L Thompson   if (op->LinearAssembleDiagonal) {
1929d04bbc78SJeremy L Thompson     // Backend version
19302b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleDiagonal(op, assembled, request));
1931eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1932eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddDiagonal) {
1933d04bbc78SJeremy L Thompson     // Backend version with zeroing first
19342b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
19352b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1936eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1937eaf62fffSJeremy L Thompson   } else {
1938d04bbc78SJeremy L Thompson     // Operator fallback
1939d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1940d04bbc78SJeremy L Thompson 
19412b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1942d04bbc78SJeremy L Thompson     if (op_fallback) {
19432b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request));
1944eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
1945eaf62fffSJeremy L Thompson     }
1946eaf62fffSJeremy L Thompson   }
1947eaf62fffSJeremy L Thompson   // Default interface implementation
19482b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
19492b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request));
1950eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1951eaf62fffSJeremy L Thompson }
1952eaf62fffSJeremy L Thompson 
1953eaf62fffSJeremy L Thompson /**
1954ca94c3ddSJeremy L Thompson   @brief Assemble the diagonal of a square linear `CeedOperator`.
1955eaf62fffSJeremy L Thompson 
1956ca94c3ddSJeremy L Thompson   This sums into a `CeedVector` the diagonal of a linear `CeedOperator`.
1957eaf62fffSJeremy L Thompson 
1958ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
1959eaf62fffSJeremy L Thompson 
1960ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1961f04ea552SJeremy L Thompson 
1962ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
1963ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal
1964ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1965eaf62fffSJeremy L Thompson 
1966eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1967eaf62fffSJeremy L Thompson 
1968eaf62fffSJeremy L Thompson   @ref User
1969eaf62fffSJeremy L Thompson **/
19702b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1971f3d47e36SJeremy L Thompson   bool     is_composite;
19721c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
1973*1203703bSJeremy L Thompson   Ceed     ceed;
19741c66c397SJeremy L Thompson 
1975*1203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
19762b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1977f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1978eaf62fffSJeremy L Thompson 
19792b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
1980*1203703bSJeremy L Thompson   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
1981c9366a6bSJeremy L Thompson 
1982f3d47e36SJeremy L Thompson   // Early exit for empty operator
1983f3d47e36SJeremy L Thompson   if (!is_composite) {
1984f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
1985f3d47e36SJeremy L Thompson 
1986f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1987f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1988f3d47e36SJeremy L Thompson   }
1989f3d47e36SJeremy L Thompson 
1990eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddDiagonal) {
1991d04bbc78SJeremy L Thompson     // Backend version
19922b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
1993eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1994eaf62fffSJeremy L Thompson   } else {
1995d04bbc78SJeremy L Thompson     // Operator fallback
1996d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
1997d04bbc78SJeremy L Thompson 
19982b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
1999d04bbc78SJeremy L Thompson     if (op_fallback) {
20002b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
2001eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2002eaf62fffSJeremy L Thompson     }
2003eaf62fffSJeremy L Thompson   }
2004eaf62fffSJeremy L Thompson   // Default interface implementation
2005eaf62fffSJeremy L Thompson   if (is_composite) {
20062b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled));
2007eaf62fffSJeremy L Thompson   } else {
20082b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, false, assembled));
2009eaf62fffSJeremy L Thompson   }
2010d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2011eaf62fffSJeremy L Thompson }
2012eaf62fffSJeremy L Thompson 
2013eaf62fffSJeremy L Thompson /**
2014ca94c3ddSJeremy L Thompson    @brief Fully assemble the point-block diagonal pattern of a linear `CeedOperator`.
201501f0e615SJames Wright 
2016ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssemblePointBlockDiagonal().
201701f0e615SJames Wright 
2018ca94c3ddSJeremy L Thompson    The assembly routines use coordinate format, with `num_entries` tuples of the form `(i, j, value)` which indicate that value should be added to the matrix in entry `(i, j)`.
2019ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are unique.
2020ca94c3ddSJeremy L Thompson    This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemblePointBlockDiagonal() provides the values in the same ordering.
202101f0e615SJames Wright 
202201f0e615SJames Wright    This will generally be slow unless your operator is low-order.
202301f0e615SJames Wright 
2024ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
202501f0e615SJames Wright 
2026ca94c3ddSJeremy L Thompson    @param[in]  op          `CeedOperator` to assemble
202701f0e615SJames Wright    @param[out] num_entries Number of entries in coordinate nonzero pattern
202801f0e615SJames Wright    @param[out] rows        Row number for each entry
202901f0e615SJames Wright    @param[out] cols        Column number for each entry
203001f0e615SJames Wright 
203101f0e615SJames Wright    @ref User
203201f0e615SJames Wright **/
203301f0e615SJames Wright int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
203401f0e615SJames Wright   Ceed          ceed;
203501f0e615SJames Wright   bool          is_composite;
203601f0e615SJames Wright   CeedInt       num_active_components, num_sub_operators;
203701f0e615SJames Wright   CeedOperator *sub_operators;
203801f0e615SJames Wright 
203901f0e615SJames Wright   CeedCall(CeedOperatorGetCeed(op, &ceed));
204001f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
204101f0e615SJames Wright 
204201f0e615SJames Wright   CeedSize input_size = 0, output_size = 0;
204301f0e615SJames Wright   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
204401f0e615SJames Wright   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
204501f0e615SJames Wright 
204601f0e615SJames Wright   if (is_composite) {
204701f0e615SJames Wright     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_operators));
204801f0e615SJames Wright     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
204901f0e615SJames Wright   } else {
205001f0e615SJames Wright     sub_operators     = &op;
205101f0e615SJames Wright     num_sub_operators = 1;
205201f0e615SJames Wright   }
205301f0e615SJames Wright 
2054506b1a0cSSebastian Grimberg   // Verify operator can be assembled correctly
2055506b1a0cSSebastian Grimberg   {
205601f0e615SJames Wright     CeedOperatorAssemblyData data;
2057506b1a0cSSebastian Grimberg     CeedInt                  num_active_elem_rstrs, comp_stride;
205801f0e615SJames Wright     CeedElemRestriction     *active_elem_rstrs;
205901f0e615SJames Wright 
206001f0e615SJames Wright     // Get initial values to check against
206101f0e615SJames Wright     CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[0], &data));
2062506b1a0cSSebastian Grimberg     CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL));
206301f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[0], &comp_stride));
206401f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[0], &num_active_components));
206501f0e615SJames Wright 
2066506b1a0cSSebastian Grimberg     // Verify that all active element restrictions have same component stride and number of components
206701f0e615SJames Wright     for (CeedInt k = 0; k < num_sub_operators; k++) {
206801f0e615SJames Wright       CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[k], &data));
2069506b1a0cSSebastian Grimberg       CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL));
207001f0e615SJames Wright       for (CeedInt i = 0; i < num_active_elem_rstrs; i++) {
2071506b1a0cSSebastian Grimberg         CeedInt comp_stride_sub, num_active_components_sub;
2072506b1a0cSSebastian Grimberg 
207301f0e615SJames Wright         CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[i], &comp_stride_sub));
207401f0e615SJames Wright         CeedCheck(comp_stride == comp_stride_sub, ceed, CEED_ERROR_DIMENSION,
207501f0e615SJames Wright                   "Active element restrictions must have the same component stride: %d vs %d", comp_stride, comp_stride_sub);
207601f0e615SJames Wright         CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[i], &num_active_components_sub));
207701f0e615SJames Wright         CeedCheck(num_active_components == num_active_components_sub, ceed, CEED_ERROR_INCOMPATIBLE,
207801f0e615SJames Wright                   "All suboperators must have the same number of output components");
207901f0e615SJames Wright       }
208001f0e615SJames Wright     }
208101f0e615SJames Wright   }
208201f0e615SJames Wright   *num_entries = input_size * num_active_components;
208301f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, rows));
208401f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, cols));
208501f0e615SJames Wright 
208601f0e615SJames Wright   for (CeedInt o = 0; o < num_sub_operators; o++) {
2087506b1a0cSSebastian Grimberg     CeedElemRestriction active_elem_rstr, point_block_active_elem_rstr;
208801f0e615SJames Wright     CeedInt             comp_stride, num_elem, elem_size;
2089506b1a0cSSebastian Grimberg     const CeedInt      *offsets, *point_block_offsets;
209001f0e615SJames Wright 
209101f0e615SJames Wright     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[o], &active_elem_rstr));
209201f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstr, &comp_stride));
209301f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumElements(active_elem_rstr, &num_elem));
209401f0e615SJames Wright     CeedCall(CeedElemRestrictionGetElementSize(active_elem_rstr, &elem_size));
209501f0e615SJames Wright     CeedCall(CeedElemRestrictionGetOffsets(active_elem_rstr, CEED_MEM_HOST, &offsets));
209601f0e615SJames Wright 
2097506b1a0cSSebastian Grimberg     CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstr, &point_block_active_elem_rstr));
2098506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOffsets(point_block_active_elem_rstr, CEED_MEM_HOST, &point_block_offsets));
209901f0e615SJames Wright 
210001f0e615SJames Wright     for (CeedSize i = 0; i < num_elem * elem_size; i++) {
210101f0e615SJames Wright       for (CeedInt c_out = 0; c_out < num_active_components; c_out++) {
210201f0e615SJames Wright         for (CeedInt c_in = 0; c_in < num_active_components; c_in++) {
2103506b1a0cSSebastian Grimberg           (*rows)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_out * comp_stride;
2104506b1a0cSSebastian Grimberg           (*cols)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_in * comp_stride;
210501f0e615SJames Wright         }
210601f0e615SJames Wright       }
210701f0e615SJames Wright     }
210801f0e615SJames Wright 
210901f0e615SJames Wright     CeedCall(CeedElemRestrictionRestoreOffsets(active_elem_rstr, &offsets));
2110506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOffsets(point_block_active_elem_rstr, &point_block_offsets));
2111506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&point_block_active_elem_rstr));
211201f0e615SJames Wright   }
211301f0e615SJames Wright   return CEED_ERROR_SUCCESS;
211401f0e615SJames Wright }
211501f0e615SJames Wright 
211601f0e615SJames Wright /**
2117ca94c3ddSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear `CeedOperator`.
2118eaf62fffSJeremy L Thompson 
2119ca94c3ddSJeremy L Thompson   This overwrites a `CeedVector` with the point block diagonal of a linear `CeedOperator`.
2120eaf62fffSJeremy L Thompson 
2121ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2122eaf62fffSJeremy L Thompson 
2123ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2124f04ea552SJeremy L Thompson 
2125ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2126ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedOperator` point block diagonal, provided in row-major form with an `num_comp * num_comp` block at each node.
2127ca94c3ddSJeremy L Thompson                           The dimensions of this vector are derived from the active vector for the `CeedOperator`.
2128ca94c3ddSJeremy L Thompson                           The array has shape `[nodes, component out, component in]`.
2129ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2130eaf62fffSJeremy L Thompson 
2131eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2132eaf62fffSJeremy L Thompson 
2133eaf62fffSJeremy L Thompson   @ref User
2134eaf62fffSJeremy L Thompson **/
21352b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2136f3d47e36SJeremy L Thompson   bool     is_composite;
21371c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
2138*1203703bSJeremy L Thompson   Ceed     ceed;
21391c66c397SJeremy L Thompson 
2140*1203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
21412b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2142f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2143eaf62fffSJeremy L Thompson 
21442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
2145*1203703bSJeremy L Thompson   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
2146c9366a6bSJeremy L Thompson 
2147f3d47e36SJeremy L Thompson   // Early exit for empty operator
2148f3d47e36SJeremy L Thompson   if (!is_composite) {
2149f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2150f3d47e36SJeremy L Thompson 
2151f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2152f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2153f3d47e36SJeremy L Thompson   }
2154f3d47e36SJeremy L Thompson 
2155eaf62fffSJeremy L Thompson   if (op->LinearAssemblePointBlockDiagonal) {
2156d04bbc78SJeremy L Thompson     // Backend version
21572b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request));
2158eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2159eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddPointBlockDiagonal) {
2160d04bbc78SJeremy L Thompson     // Backend version with zeroing first
21612b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
21622b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
2163eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2164eaf62fffSJeremy L Thompson   } else {
2165d04bbc78SJeremy L Thompson     // Operator fallback
2166d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2167d04bbc78SJeremy L Thompson 
21682b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2169d04bbc78SJeremy L Thompson     if (op_fallback) {
21702b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request));
2171eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2172eaf62fffSJeremy L Thompson     }
2173eaf62fffSJeremy L Thompson   }
2174eaf62fffSJeremy L Thompson   // Default interface implementation
21752b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
21762b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
2177eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2178eaf62fffSJeremy L Thompson }
2179eaf62fffSJeremy L Thompson 
2180eaf62fffSJeremy L Thompson /**
2181ca94c3ddSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear `CeedOperator`.
2182eaf62fffSJeremy L Thompson 
2183ca94c3ddSJeremy L Thompson   This sums into a `CeedVector` with the point block diagonal of a linear `CeedOperator`.
2184eaf62fffSJeremy L Thompson 
2185ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2186eaf62fffSJeremy L Thompson 
2187ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2188f04ea552SJeremy L Thompson 
2189ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2190ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled CeedOperator point block diagonal, provided in row-major form with an `num_comp * num_comp` block at each node.
2191ca94c3ddSJeremy L Thompson                           The dimensions of this vector are derived from the active vector for the `CeedOperator`.
2192ca94c3ddSJeremy L Thompson                           The array has shape `[nodes, component out, component in]`.
2193ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2194eaf62fffSJeremy L Thompson 
2195eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2196eaf62fffSJeremy L Thompson 
2197eaf62fffSJeremy L Thompson   @ref User
2198eaf62fffSJeremy L Thompson **/
21992b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2200f3d47e36SJeremy L Thompson   bool     is_composite;
22011c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
2202*1203703bSJeremy L Thompson   Ceed     ceed;
22031c66c397SJeremy L Thompson 
2204*1203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
22052b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2206f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2207eaf62fffSJeremy L Thompson 
22082b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
2209*1203703bSJeremy L Thompson   CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square");
2210c9366a6bSJeremy L Thompson 
2211f3d47e36SJeremy L Thompson   // Early exit for empty operator
2212f3d47e36SJeremy L Thompson   if (!is_composite) {
2213f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2214f3d47e36SJeremy L Thompson 
2215f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2216f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2217f3d47e36SJeremy L Thompson   }
2218f3d47e36SJeremy L Thompson 
2219eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddPointBlockDiagonal) {
2220d04bbc78SJeremy L Thompson     // Backend version
22212b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request));
2222eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2223eaf62fffSJeremy L Thompson   } else {
2224d04bbc78SJeremy L Thompson     // Operator fallback
2225d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2226d04bbc78SJeremy L Thompson 
22272b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2228d04bbc78SJeremy L Thompson     if (op_fallback) {
22292b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request));
2230eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2231eaf62fffSJeremy L Thompson     }
2232eaf62fffSJeremy L Thompson   }
2233ea61e9acSJeremy L Thompson   // Default interface implementation
2234eaf62fffSJeremy L Thompson   if (is_composite) {
22352b730f8bSJeremy L Thompson     CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled));
2236eaf62fffSJeremy L Thompson   } else {
22372b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, true, assembled));
2238eaf62fffSJeremy L Thompson   }
2239d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2240eaf62fffSJeremy L Thompson }
2241eaf62fffSJeremy L Thompson 
2242eaf62fffSJeremy L Thompson /**
2243ca94c3ddSJeremy L Thompson    @brief Fully assemble the nonzero pattern of a linear `CeedOperator`.
2244eaf62fffSJeremy L Thompson 
2245ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssemble().
2246eaf62fffSJeremy L Thompson 
2247ca94c3ddSJeremy L Thompson    The assembly routines use coordinate format, with `num_entries` tuples of the form `(i, j, value)` which indicate that value should be added to the matrix in entry `(i, j)`.
2248ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are not unique and may repeat.
2249ca94c3ddSJeremy L Thompson    This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemble() provides the values in the same ordering.
2250eaf62fffSJeremy L Thompson 
2251eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2252eaf62fffSJeremy L Thompson 
2253ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2254f04ea552SJeremy L Thompson 
2255ca94c3ddSJeremy L Thompson    @param[in]  op          `CeedOperator` to assemble
2256eaf62fffSJeremy L Thompson    @param[out] num_entries Number of entries in coordinate nonzero pattern
2257eaf62fffSJeremy L Thompson    @param[out] rows        Row number for each entry
2258eaf62fffSJeremy L Thompson    @param[out] cols        Column number for each entry
2259eaf62fffSJeremy L Thompson 
2260eaf62fffSJeremy L Thompson    @ref User
2261eaf62fffSJeremy L Thompson **/
22622b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
22631c66c397SJeremy L Thompson   bool          is_composite;
22641c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2265b94338b9SJed Brown   CeedSize      single_entries;
2266eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
22671c66c397SJeremy L Thompson 
22682b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2269f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2270eaf62fffSJeremy L Thompson 
2271eaf62fffSJeremy L Thompson   if (op->LinearAssembleSymbolic) {
2272d04bbc78SJeremy L Thompson     // Backend version
22732b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols));
2274eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2275eaf62fffSJeremy L Thompson   } else {
2276d04bbc78SJeremy L Thompson     // Operator fallback
2277d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2278d04bbc78SJeremy L Thompson 
22792b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2280d04bbc78SJeremy L Thompson     if (op_fallback) {
22812b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols));
2282eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2283eaf62fffSJeremy L Thompson     }
2284eaf62fffSJeremy L Thompson   }
2285eaf62fffSJeremy L Thompson 
2286eaf62fffSJeremy L Thompson   // Default interface implementation
2287eaf62fffSJeremy L Thompson 
2288506b1a0cSSebastian Grimberg   // Count entries and allocate rows, cols arrays
2289eaf62fffSJeremy L Thompson   *num_entries = 0;
2290eaf62fffSJeremy L Thompson   if (is_composite) {
2291c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2292c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
229392ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
22942b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2295eaf62fffSJeremy L Thompson       *num_entries += single_entries;
2296eaf62fffSJeremy L Thompson     }
2297eaf62fffSJeremy L Thompson   } else {
22982b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries));
2299eaf62fffSJeremy L Thompson     *num_entries += single_entries;
2300eaf62fffSJeremy L Thompson   }
23012b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, rows));
23022b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, cols));
2303eaf62fffSJeremy L Thompson 
2304506b1a0cSSebastian Grimberg   // Assemble nonzero locations
2305eaf62fffSJeremy L Thompson   if (is_composite) {
2306c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2307c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
230892ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
23092b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols));
23102b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2311eaf62fffSJeremy L Thompson       offset += single_entries;
2312eaf62fffSJeremy L Thompson     }
2313eaf62fffSJeremy L Thompson   } else {
23142b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols));
2315eaf62fffSJeremy L Thompson   }
2316eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2317eaf62fffSJeremy L Thompson }
2318eaf62fffSJeremy L Thompson 
2319eaf62fffSJeremy L Thompson /**
2320eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero entries of a linear operator.
2321eaf62fffSJeremy L Thompson 
2322ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssembleSymbolic().
2323eaf62fffSJeremy L Thompson 
2324ca94c3ddSJeremy L Thompson    The assembly routines use coordinate format, with `num_entries` tuples of the form `(i, j, value)` which indicate that value should be added to the matrix in entry `(i, j)`.
2325ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are not unique and may repeat.
2326ca94c3ddSJeremy L Thompson    This function returns the values of the nonzero entries to be added, their `(i, j)` locations are provided by @ref CeedOperatorLinearAssembleSymbolic().
2327eaf62fffSJeremy L Thompson 
2328eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2329eaf62fffSJeremy L Thompson 
2330ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2331f04ea552SJeremy L Thompson 
2332ca94c3ddSJeremy L Thompson    @param[in]  op     `CeedOperator` to assemble
2333eaf62fffSJeremy L Thompson    @param[out] values Values to assemble into matrix
2334eaf62fffSJeremy L Thompson 
2335eaf62fffSJeremy L Thompson    @ref User
2336eaf62fffSJeremy L Thompson **/
2337eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) {
23381c66c397SJeremy L Thompson   bool          is_composite;
23391c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2340b94338b9SJed Brown   CeedSize      single_entries = 0;
2341eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
23421c66c397SJeremy L Thompson 
23432b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2344f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2345f3d47e36SJeremy L Thompson 
2346f3d47e36SJeremy L Thompson   // Early exit for empty operator
2347f3d47e36SJeremy L Thompson   if (!is_composite) {
2348f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2349f3d47e36SJeremy L Thompson 
2350f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2351f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2352f3d47e36SJeremy L Thompson   }
2353eaf62fffSJeremy L Thompson 
2354eaf62fffSJeremy L Thompson   if (op->LinearAssemble) {
2355d04bbc78SJeremy L Thompson     // Backend version
23562b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemble(op, values));
2357eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2358eaf62fffSJeremy L Thompson   } else {
2359d04bbc78SJeremy L Thompson     // Operator fallback
2360d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2361d04bbc78SJeremy L Thompson 
23622b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2363d04bbc78SJeremy L Thompson     if (op_fallback) {
23642b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemble(op_fallback, values));
2365eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2366eaf62fffSJeremy L Thompson     }
2367eaf62fffSJeremy L Thompson   }
2368eaf62fffSJeremy L Thompson 
2369eaf62fffSJeremy L Thompson   // Default interface implementation
237028ec399dSJeremy L Thompson   CeedCall(CeedVectorSetValue(values, 0.0));
2371eaf62fffSJeremy L Thompson   if (is_composite) {
2372c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2373c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2374cefa2673SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; k++) {
23752b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values));
23762b730f8bSJeremy L Thompson       CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries));
2377eaf62fffSJeremy L Thompson       offset += single_entries;
2378eaf62fffSJeremy L Thompson     }
2379eaf62fffSJeremy L Thompson   } else {
23802b730f8bSJeremy L Thompson     CeedCall(CeedSingleOperatorAssemble(op, offset, values));
2381eaf62fffSJeremy L Thompson   }
2382eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2383eaf62fffSJeremy L Thompson }
2384eaf62fffSJeremy L Thompson 
2385eaf62fffSJeremy L Thompson /**
2386ca94c3ddSJeremy L Thompson   @brief Get the multiplicity of nodes across sub-operators in a composite `CeedOperator`.
238775f0d5a4SJeremy L Thompson 
2388ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
238975f0d5a4SJeremy L Thompson 
2390ca94c3ddSJeremy L Thompson   @param[in]  op               Composite `CeedOperator`
2391ca94c3ddSJeremy L Thompson   @param[in]  num_skip_indices Number of sub-operators to skip
2392ca94c3ddSJeremy L Thompson   @param[in]  skip_indices     Array of indices of sub-operators to skip
2393ca94c3ddSJeremy L Thompson   @param[out] mult             Vector to store multiplicity (of size `l_size` )
239475f0d5a4SJeremy L Thompson 
239575f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
239675f0d5a4SJeremy L Thompson 
239775f0d5a4SJeremy L Thompson   @ref User
239875f0d5a4SJeremy L Thompson **/
239975f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) {
240075f0d5a4SJeremy L Thompson   Ceed                ceed;
2401b275c451SJeremy L Thompson   CeedInt             num_suboperators;
240275f0d5a4SJeremy L Thompson   CeedSize            l_vec_len;
240375f0d5a4SJeremy L Thompson   CeedScalar         *mult_array;
240475f0d5a4SJeremy L Thompson   CeedVector          ones_l_vec;
24057c1dbaffSSebastian Grimberg   CeedElemRestriction elem_rstr, mult_elem_rstr;
2406b275c451SJeremy L Thompson   CeedOperator       *sub_operators;
240775f0d5a4SJeremy L Thompson 
24081c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
24091c66c397SJeremy L Thompson 
241075f0d5a4SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
241175f0d5a4SJeremy L Thompson 
241275f0d5a4SJeremy L Thompson   // Zero mult vector
241375f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
241475f0d5a4SJeremy L Thompson 
241575f0d5a4SJeremy L Thompson   // Get suboperators
2416b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2417b275c451SJeremy L Thompson   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2418b275c451SJeremy L Thompson   if (num_suboperators == 0) return CEED_ERROR_SUCCESS;
241975f0d5a4SJeremy L Thompson 
242075f0d5a4SJeremy L Thompson   // Work vector
242175f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetLength(mult, &l_vec_len));
242275f0d5a4SJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec));
242375f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(ones_l_vec, 1.0));
242475f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array));
242575f0d5a4SJeremy L Thompson 
242675f0d5a4SJeremy L Thompson   // Compute multiplicity across suboperators
2427b275c451SJeremy L Thompson   for (CeedInt i = 0; i < num_suboperators; i++) {
242875f0d5a4SJeremy L Thompson     const CeedScalar *sub_mult_array;
242975f0d5a4SJeremy L Thompson     CeedVector        sub_mult_l_vec, ones_e_vec;
243075f0d5a4SJeremy L Thompson 
243175f0d5a4SJeremy L Thompson     // -- Check for suboperator to skip
243275f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < num_skip_indices; j++) {
243375f0d5a4SJeremy L Thompson       if (skip_indices[j] == i) continue;
243475f0d5a4SJeremy L Thompson     }
243575f0d5a4SJeremy L Thompson 
243675f0d5a4SJeremy L Thompson     // -- Sub operator multiplicity
2437437c7c90SJeremy L Thompson     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr));
24387c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr, &mult_elem_rstr));
24397c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateVector(mult_elem_rstr, &sub_mult_l_vec, &ones_e_vec));
244075f0d5a4SJeremy L Thompson     CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0));
24417c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE));
24427c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE));
244375f0d5a4SJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array));
244475f0d5a4SJeremy L Thompson     // ---- Flag every node present in the current suboperator
244575f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < l_vec_len; j++) {
244675f0d5a4SJeremy L Thompson       if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0;
244775f0d5a4SJeremy L Thompson     }
244875f0d5a4SJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array));
244975f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&sub_mult_l_vec));
245075f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&ones_e_vec));
24517c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&mult_elem_rstr));
245275f0d5a4SJeremy L Thompson   }
245375f0d5a4SJeremy L Thompson   CeedCall(CeedVectorRestoreArray(mult, &mult_array));
2454811d0ccfSJeremy L Thompson   CeedCall(CeedVectorDestroy(&ones_l_vec));
245575f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
245675f0d5a4SJeremy L Thompson }
245775f0d5a4SJeremy L Thompson 
245875f0d5a4SJeremy L Thompson /**
2459ca94c3ddSJeremy L Thompson   @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator`, creating the prolongation basis from the fine and coarse grid interpolation.
2460eaf62fffSJeremy L Thompson 
2461ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2462f04ea552SJeremy L Thompson 
2463ca94c3ddSJeremy L Thompson   @param[in]  op_fine      Fine grid `CeedOperator`
2464ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2465ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid `CeedElemRestriction`
2466ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector `CeedBasis`
2467ca94c3ddSJeremy L Thompson   @param[out] op_coarse    Coarse grid `CeedOperator`
2468ca94c3ddSJeremy L Thompson   @param[out] op_prolong   Coarse to fine `CeedOperator`, or `NULL`
2469ca94c3ddSJeremy L Thompson   @param[out] op_restrict  Fine to coarse `CeedOperator`, or `NULL`
2470eaf62fffSJeremy L Thompson 
2471eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2472eaf62fffSJeremy L Thompson 
2473eaf62fffSJeremy L Thompson   @ref User
2474eaf62fffSJeremy L Thompson **/
24752b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
24767758292fSSebastian Grimberg                                      CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
24771c66c397SJeremy L Thompson   CeedBasis basis_c_to_f = NULL;
24781c66c397SJeremy L Thompson 
24792b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2480eaf62fffSJeremy L Thompson 
248183d6adf3SZach Atkins   // Build prolongation matrix, if required
24827758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
248383d6adf3SZach Atkins     CeedBasis basis_fine;
24841c66c397SJeremy L Thompson 
24852b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
24862b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f));
248783d6adf3SZach Atkins   }
2488eaf62fffSJeremy L Thompson 
2489f113e5dcSJeremy L Thompson   // Core code
24907758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2491eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2492eaf62fffSJeremy L Thompson }
2493eaf62fffSJeremy L Thompson 
2494eaf62fffSJeremy L Thompson /**
2495ca94c3ddSJeremy L Thompson   @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a tensor basis for the active basis.
2496eaf62fffSJeremy L Thompson 
2497ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2498f04ea552SJeremy L Thompson 
2499ca94c3ddSJeremy L Thompson   @param[in]  op_fine       Fine grid `CeedOperator`
2500ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2501ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid `CeedElemRestriction`
2502ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector `CeedBasis`
2503ca94c3ddSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator`
2504ca94c3ddSJeremy L Thompson   @param[out] op_coarse     Coarse grid `CeedOperator`
2505ca94c3ddSJeremy L Thompson   @param[out] op_prolong    Coarse to fine `CeedOperator`, or `NULL`
2506ca94c3ddSJeremy L Thompson   @param[out] op_restrict   Fine to coarse `CeedOperator`, or `NULL`
2507eaf62fffSJeremy L Thompson 
2508eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2509eaf62fffSJeremy L Thompson 
2510eaf62fffSJeremy L Thompson   @ref User
2511eaf62fffSJeremy L Thompson **/
25122b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
25132b730f8bSJeremy L Thompson                                              const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
25147758292fSSebastian Grimberg                                              CeedOperator *op_restrict) {
2515eaf62fffSJeremy L Thompson   Ceed      ceed;
25161c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
25171c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
25181c66c397SJeremy L Thompson 
25191c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
25202b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2521eaf62fffSJeremy L Thompson 
2522eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
25232b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
25242b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
25252b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
25266574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2527eaf62fffSJeremy L Thompson 
252883d6adf3SZach Atkins   // Create coarse to fine basis, if required
25297758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
25301c66c397SJeremy L Thompson     CeedInt     dim, num_comp, num_nodes_c, P_1d_f, P_1d_c;
25311c66c397SJeremy L Thompson     CeedScalar *q_ref, *q_weight, *grad;
25321c66c397SJeremy L Thompson 
253383d6adf3SZach Atkins     // Check if interpolation matrix is provided
25346574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
25356574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
25362b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
25372b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
25382b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f));
25392b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
25402b730f8bSJeremy L Thompson     P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c);
25412b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_ref));
25422b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_weight));
25432b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad));
25442b730f8bSJeremy 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));
25452b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
25462b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
25472b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
254883d6adf3SZach Atkins   }
2549eaf62fffSJeremy L Thompson 
2550eaf62fffSJeremy L Thompson   // Core code
25517758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2552eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2553eaf62fffSJeremy L Thompson }
2554eaf62fffSJeremy L Thompson 
2555eaf62fffSJeremy L Thompson /**
2556ca94c3ddSJeremy L Thompson   @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a non-tensor basis for the active vector
2557eaf62fffSJeremy L Thompson 
2558ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2559f04ea552SJeremy L Thompson 
2560ca94c3ddSJeremy L Thompson   @param[in]  op_fine       Fine grid `CeedOperator`
2561ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2562ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid `CeedElemRestriction`
2563ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector `CeedBasis`
2564ca94c3ddSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator`
2565ca94c3ddSJeremy L Thompson   @param[out] op_coarse     Coarse grid `CeedOperator`
2566ca94c3ddSJeremy L Thompson   @param[out] op_prolong    Coarse to fine `CeedOperator`, or `NULL`
2567ca94c3ddSJeremy L Thompson   @param[out] op_restrict   Fine to coarse `CeedOperator`, or `NULL`
2568eaf62fffSJeremy L Thompson 
2569eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2570eaf62fffSJeremy L Thompson 
2571eaf62fffSJeremy L Thompson   @ref User
2572eaf62fffSJeremy L Thompson **/
25732b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
25747758292fSSebastian Grimberg                                        const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
25757758292fSSebastian Grimberg                                        CeedOperator *op_restrict) {
2576eaf62fffSJeremy L Thompson   Ceed      ceed;
25771c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
25781c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
25791c66c397SJeremy L Thompson 
25801c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
25812b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2582eaf62fffSJeremy L Thompson 
2583eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
25842b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
25852b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
25862b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
25876574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2588eaf62fffSJeremy L Thompson 
2589eaf62fffSJeremy L Thompson   // Coarse to fine basis
25907758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
25911c66c397SJeremy L Thompson     CeedInt          dim, num_comp, num_nodes_c, num_nodes_f;
25921c66c397SJeremy L Thompson     CeedScalar      *q_ref, *q_weight, *grad;
25931c66c397SJeremy L Thompson     CeedElemTopology topo;
25941c66c397SJeremy L Thompson 
259583d6adf3SZach Atkins     // Check if interpolation matrix is provided
25966574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
25976574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
25982b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_fine, &topo));
25992b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
26002b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
26012b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f));
26022b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
26032b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref));
26042b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f, &q_weight));
26052b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad));
26062b730f8bSJeremy 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));
26072b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
26082b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
26092b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
261083d6adf3SZach Atkins   }
2611eaf62fffSJeremy L Thompson 
2612eaf62fffSJeremy L Thompson   // Core code
26137758292fSSebastian Grimberg   CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict));
2614eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2615eaf62fffSJeremy L Thompson }
2616eaf62fffSJeremy L Thompson 
2617eaf62fffSJeremy L Thompson /**
2618ca94c3ddSJeremy L Thompson   @brief Build a FDM based approximate inverse for each element for a `CeedOperator`.
2619eaf62fffSJeremy L Thompson 
2620ca94c3ddSJeremy L Thompson   This returns a `CeedOperator` and `CeedVector` to apply a Fast Diagonalization Method based approximate inverse.
2621859c15bbSJames 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$.
2622ca94c3ddSJeremy L Thompson   The assembled `CeedQFunction` is used to modify the eigenvalues from simultaneous diagonalization and obtain an approximate inverse of the form \f$V^T \hat S V\f$.
2623ca94c3ddSJeremy L Thompson   The `CeedOperator` must be linear and non-composite.
2624ca94c3ddSJeremy L Thompson   The associated `CeedQFunction` must therefore also be linear.
2625eaf62fffSJeremy L Thompson 
2626ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2627f04ea552SJeremy L Thompson 
2628ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to create element inverses
2629ca94c3ddSJeremy L Thompson   @param[out] fdm_inv `CeedOperator` to apply the action of a FDM based inverse for each element
2630ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2631eaf62fffSJeremy L Thompson 
2632eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2633eaf62fffSJeremy L Thompson 
2634480fae85SJeremy L Thompson   @ref User
2635eaf62fffSJeremy L Thompson **/
26362b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) {
26371c66c397SJeremy L Thompson   Ceed                 ceed, ceed_parent;
26381c66c397SJeremy L Thompson   bool                 interp = false, grad = false, is_tensor_basis = true;
26391c66c397SJeremy L Thompson   CeedInt              num_input_fields, P_1d, Q_1d, num_nodes, num_qpts, dim, num_comp = 1, num_elem = 1;
26401c66c397SJeremy L Thompson   CeedSize             l_size = 1;
26411c66c397SJeremy L Thompson   CeedScalar          *mass, *laplace, *x, *fdm_interp, *lambda, *elem_avg;
26421c66c397SJeremy L Thompson   const CeedScalar    *interp_1d, *grad_1d, *q_weight_1d;
26431c66c397SJeremy L Thompson   CeedVector           q_data;
26441c66c397SJeremy L Thompson   CeedElemRestriction  rstr  = NULL, rstr_qd_i;
26451c66c397SJeremy L Thompson   CeedBasis            basis = NULL, fdm_basis;
26461c66c397SJeremy L Thompson   CeedQFunctionContext ctx_fdm;
26471c66c397SJeremy L Thompson   CeedQFunctionField  *qf_fields;
26481c66c397SJeremy L Thompson   CeedQFunction        qf, qf_fdm;
26491c66c397SJeremy L Thompson   CeedOperatorField   *op_fields;
26501c66c397SJeremy L Thompson 
26512b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2652eaf62fffSJeremy L Thompson 
2653eaf62fffSJeremy L Thompson   if (op->CreateFDMElementInverse) {
2654d04bbc78SJeremy L Thompson     // Backend version
26552b730f8bSJeremy L Thompson     CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request));
2656eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2657eaf62fffSJeremy L Thompson   } else {
2658d04bbc78SJeremy L Thompson     // Operator fallback
2659d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2660d04bbc78SJeremy L Thompson 
26612b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2662d04bbc78SJeremy L Thompson     if (op_fallback) {
26632b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request));
2664eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2665eaf62fffSJeremy L Thompson     }
2666eaf62fffSJeremy L Thompson   }
2667eaf62fffSJeremy L Thompson 
2668d04bbc78SJeremy L Thompson   // Default interface implementation
26692b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
2670bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
26712b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
2672eaf62fffSJeremy L Thompson 
2673eaf62fffSJeremy L Thompson   // Determine active input basis
26742b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL));
26752b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
2676eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
2677eaf62fffSJeremy L Thompson     CeedVector vec;
26781c66c397SJeremy L Thompson 
26792b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
2680eaf62fffSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2681eaf62fffSJeremy L Thompson       CeedEvalMode eval_mode;
26821c66c397SJeremy L Thompson 
26832b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
2684eaf62fffSJeremy L Thompson       interp = interp || eval_mode == CEED_EVAL_INTERP;
2685eaf62fffSJeremy L Thompson       grad   = grad || eval_mode == CEED_EVAL_GRAD;
26862b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis));
26872b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
2688eaf62fffSJeremy L Thompson     }
2689eaf62fffSJeremy L Thompson   }
26906574a04fSJeremy L Thompson   CeedCheck(basis, ceed, CEED_ERROR_BACKEND, "No active field set");
26912b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
2692352a5e7cSSebastian Grimberg   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
26932b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
26942b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
26952b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
26962b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
26972b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
26982b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
2699eaf62fffSJeremy L Thompson 
2700eaf62fffSJeremy L Thompson   // Build and diagonalize 1D Mass and Laplacian
27016574a04fSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
27026574a04fSJeremy L Thompson   CeedCheck(is_tensor_basis, ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases");
27032b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &mass));
27042b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &laplace));
27052b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &x));
27062b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp));
27072b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &lambda));
2708eaf62fffSJeremy L Thompson   // -- Build matrices
27092b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
27102b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
27112b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
27122b730f8bSJeremy L Thompson   CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace));
2713eaf62fffSJeremy L Thompson 
2714eaf62fffSJeremy L Thompson   // -- Diagonalize
27152b730f8bSJeremy L Thompson   CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d));
27162b730f8bSJeremy L Thompson   CeedCall(CeedFree(&mass));
27172b730f8bSJeremy L Thompson   CeedCall(CeedFree(&laplace));
27182b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
27192b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d];
27202b730f8bSJeremy L Thompson   }
27212b730f8bSJeremy L Thompson   CeedCall(CeedFree(&x));
2722eaf62fffSJeremy L Thompson 
27231c66c397SJeremy L Thompson   {
27241c66c397SJeremy L Thompson     CeedInt             layout[3], num_modes = (interp ? 1 : 0) + (grad ? dim : 0);
27251c66c397SJeremy L Thompson     CeedScalar          max_norm = 0;
27261c66c397SJeremy L Thompson     const CeedScalar   *assembled_array, *q_weight_array;
27271c66c397SJeremy L Thompson     CeedVector          assembled = NULL, q_weight;
2728c5f45aeaSJeremy L Thompson     CeedElemRestriction rstr_qf   = NULL;
27291c66c397SJeremy L Thompson 
27301c66c397SJeremy L Thompson     // Assemble QFunction
27312b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request));
273256c48462SJeremy L Thompson     CeedCall(CeedElemRestrictionGetELayout(rstr_qf, layout));
27332b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr_qf));
27342b730f8bSJeremy L Thompson     CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm));
2735eaf62fffSJeremy L Thompson 
2736eaf62fffSJeremy L Thompson     // Calculate element averages
27372b730f8bSJeremy L Thompson     CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight));
27382b730f8bSJeremy L Thompson     CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight));
27392b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array));
27402b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array));
27412b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_elem, &elem_avg));
2742eaf62fffSJeremy L Thompson     const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON;
27431c66c397SJeremy L Thompson 
2744eaf62fffSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
2745eaf62fffSJeremy L Thompson       CeedInt count = 0;
27461c66c397SJeremy L Thompson 
27472b730f8bSJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
27482b730f8bSJeremy L Thompson         for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) {
27492b730f8bSJeremy L Thompson           if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) {
27502b730f8bSJeremy L Thompson             elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q];
2751eaf62fffSJeremy L Thompson             count++;
2752eaf62fffSJeremy L Thompson           }
27532b730f8bSJeremy L Thompson         }
27542b730f8bSJeremy L Thompson       }
2755eaf62fffSJeremy L Thompson       if (count) {
2756eaf62fffSJeremy L Thompson         elem_avg[e] /= count;
2757eaf62fffSJeremy L Thompson       } else {
2758eaf62fffSJeremy L Thompson         elem_avg[e] = 1.0;
2759eaf62fffSJeremy L Thompson       }
2760eaf62fffSJeremy L Thompson     }
27612b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array));
27622b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled));
27632b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array));
27642b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&q_weight));
27651c66c397SJeremy L Thompson   }
2766eaf62fffSJeremy L Thompson 
2767eaf62fffSJeremy L Thompson   // Build FDM diagonal
27681c66c397SJeremy L Thompson   {
2769eaf62fffSJeremy L Thompson     CeedScalar *q_data_array, *fdm_diagonal;
27701c66c397SJeremy L Thompson 
2771352a5e7cSSebastian Grimberg     CeedCall(CeedCalloc(num_comp * num_nodes, &fdm_diagonal));
2772352a5e7cSSebastian Grimberg     const CeedScalar fdm_diagonal_bound = num_nodes * CEED_EPSILON;
27732b730f8bSJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
2774352a5e7cSSebastian Grimberg       for (CeedInt n = 0; n < num_nodes; n++) {
2775352a5e7cSSebastian Grimberg         if (interp) fdm_diagonal[c * num_nodes + n] = 1.0;
27762b730f8bSJeremy L Thompson         if (grad) {
2777eaf62fffSJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
2778eaf62fffSJeremy L Thompson             CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d;
2779352a5e7cSSebastian Grimberg             fdm_diagonal[c * num_nodes + n] += lambda[i];
2780eaf62fffSJeremy L Thompson           }
2781eaf62fffSJeremy L Thompson         }
2782352a5e7cSSebastian Grimberg         if (fabs(fdm_diagonal[c * num_nodes + n]) < fdm_diagonal_bound) fdm_diagonal[c * num_nodes + n] = fdm_diagonal_bound;
27832b730f8bSJeremy L Thompson       }
27842b730f8bSJeremy L Thompson     }
2785352a5e7cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * num_nodes, &q_data));
27862b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(q_data, 0.0));
27872b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array));
27882b730f8bSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
27892b730f8bSJeremy L Thompson       for (CeedInt c = 0; c < num_comp; c++) {
27901c66c397SJeremy L Thompson         for (CeedInt n = 0; n < num_nodes; n++)
27911c66c397SJeremy L Thompson           q_data_array[(e * num_comp + c) * num_nodes + n] = 1. / (elem_avg[e] * fdm_diagonal[c * num_nodes + n]);
27922b730f8bSJeremy L Thompson       }
27932b730f8bSJeremy L Thompson     }
27942b730f8bSJeremy L Thompson     CeedCall(CeedFree(&elem_avg));
27952b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_diagonal));
27962b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(q_data, &q_data_array));
27971c66c397SJeremy L Thompson   }
2798eaf62fffSJeremy L Thompson 
2799eaf62fffSJeremy L Thompson   // Setup FDM operator
2800eaf62fffSJeremy L Thompson   // -- Basis
28011c66c397SJeremy L Thompson   {
2802eaf62fffSJeremy L Thompson     CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy;
28031c66c397SJeremy L Thompson 
28042b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy));
28052b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_ref_dummy));
28062b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_weight_dummy));
28072b730f8bSJeremy 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));
28082b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_interp));
28092b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad_dummy));
28102b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref_dummy));
28112b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight_dummy));
28122b730f8bSJeremy L Thompson     CeedCall(CeedFree(&lambda));
28131c66c397SJeremy L Thompson   }
2814eaf62fffSJeremy L Thompson 
2815eaf62fffSJeremy L Thompson   // -- Restriction
28161c66c397SJeremy L Thompson   {
2817352a5e7cSSebastian Grimberg     CeedInt strides[3] = {1, num_nodes, num_nodes * num_comp};
2818352a5e7cSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, num_nodes, num_comp, num_elem * num_comp * num_nodes, strides, &rstr_qd_i));
28191c66c397SJeremy L Thompson   }
28201c66c397SJeremy L Thompson 
2821eaf62fffSJeremy L Thompson   // -- QFunction
28222b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm));
28232b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP));
28242b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE));
28252b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP));
28262b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp));
28271c66c397SJeremy L Thompson 
2828eaf62fffSJeremy L Thompson   // -- QFunction context
28291c66c397SJeremy L Thompson   {
2830eaf62fffSJeremy L Thompson     CeedInt *num_comp_data;
28311c66c397SJeremy L Thompson 
28322b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_data));
2833eaf62fffSJeremy L Thompson     num_comp_data[0] = num_comp;
28342b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm));
28352b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data));
28361c66c397SJeremy L Thompson   }
28372b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm));
28382b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_fdm));
28391c66c397SJeremy L Thompson 
2840eaf62fffSJeremy L Thompson   // -- Operator
28412b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv));
28422b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2843356036faSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_NONE, q_data));
28442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
2845eaf62fffSJeremy L Thompson 
2846eaf62fffSJeremy L Thompson   // Cleanup
28472b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_data));
28482b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&fdm_basis));
28492b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i));
28502b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_fdm));
2851eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2852eaf62fffSJeremy L Thompson }
2853eaf62fffSJeremy L Thompson 
2854eaf62fffSJeremy L Thompson /// @}
2855