xref: /libCEED/interface/ceed-preconditioning.c (revision 33490f6ef26c584470cb54133cdd71c442f1841b)
19ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, 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 **/
CeedQFunctionCreateFallback(Ceed fallback_ceed,CeedQFunction qf,CeedQFunction * qf_fallback)372b730f8bSJeremy L Thompson static int CeedQFunctionCreateFallback(Ceed fallback_ceed, CeedQFunction qf, CeedQFunction *qf_fallback) {
381c66c397SJeremy L Thompson   char               *source_path_with_name = NULL;
391203703bSJeremy L Thompson   CeedInt             num_input_fields, num_output_fields;
401203703bSJeremy L Thompson   CeedQFunctionField *input_fields, *output_fields;
411c66c397SJeremy L Thompson 
429e77b9c8SJeremy L Thompson   // Check if NULL qf passed in
439e77b9c8SJeremy L Thompson   if (!qf) return CEED_ERROR_SUCCESS;
449e77b9c8SJeremy L Thompson 
459bc66399SJeremy L Thompson   CeedDebug(CeedQFunctionReturnCeed(qf), "Creating fallback CeedQFunction\n");
46d04bbc78SJeremy L Thompson 
479e77b9c8SJeremy L Thompson   if (qf->source_path) {
482b730f8bSJeremy L Thompson     size_t path_len = strlen(qf->source_path), name_len = strlen(qf->kernel_name);
499c25dd66SJeremy L Thompson 
502b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(path_len + name_len + 2, &source_path_with_name));
519e77b9c8SJeremy L Thompson     memcpy(source_path_with_name, qf->source_path, path_len);
529e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len], ":", 1);
539e77b9c8SJeremy L Thompson     memcpy(&source_path_with_name[path_len + 1], qf->kernel_name, name_len);
549c25dd66SJeremy L Thompson   } else if (qf->user_source) {
559c25dd66SJeremy L Thompson     CeedCall(CeedStringAllocCopy(qf->user_source, &source_path_with_name));
569e77b9c8SJeremy L Thompson   } else {
572b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &source_path_with_name));
589e77b9c8SJeremy L Thompson   }
599e77b9c8SJeremy L Thompson 
601203703bSJeremy L Thompson   {
611203703bSJeremy L Thompson     CeedInt           vec_length;
621203703bSJeremy L Thompson     CeedQFunctionUser f;
631203703bSJeremy L Thompson 
641203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length));
651203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetUserFunction(qf, &f));
661203703bSJeremy L Thompson     CeedCall(CeedQFunctionCreateInterior(fallback_ceed, vec_length, f, source_path_with_name, qf_fallback));
671203703bSJeremy L Thompson   }
689e77b9c8SJeremy L Thompson   {
699e77b9c8SJeremy L Thompson     CeedQFunctionContext ctx;
709e77b9c8SJeremy L Thompson 
712b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
722b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx));
731485364cSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx));
749e77b9c8SJeremy L Thompson   }
751203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
761203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
776f8994e9SJeremy L Thompson     const char  *field_name;
781203703bSJeremy L Thompson     CeedInt      size;
791203703bSJeremy L Thompson     CeedEvalMode eval_mode;
801203703bSJeremy L Thompson 
81ab747706SJeremy L Thompson     CeedCall(CeedQFunctionFieldGetData(input_fields[i], &field_name, &size, &eval_mode));
821203703bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(*qf_fallback, field_name, size, eval_mode));
839e77b9c8SJeremy L Thompson   }
841203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
856f8994e9SJeremy L Thompson     const char  *field_name;
861203703bSJeremy L Thompson     CeedInt      size;
871203703bSJeremy L Thompson     CeedEvalMode eval_mode;
881203703bSJeremy L Thompson 
89ab747706SJeremy L Thompson     CeedCall(CeedQFunctionFieldGetData(output_fields[i], &field_name, &size, &eval_mode));
901203703bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(*qf_fallback, field_name, size, eval_mode));
919e77b9c8SJeremy L Thompson   }
922b730f8bSJeremy L Thompson   CeedCall(CeedFree(&source_path_with_name));
939e77b9c8SJeremy L Thompson   return CEED_ERROR_SUCCESS;
949e77b9c8SJeremy L Thompson }
959e77b9c8SJeremy L Thompson 
969e77b9c8SJeremy L Thompson /**
97ca94c3ddSJeremy L Thompson   @brief Duplicate a `CeedOperator` with a reference `Ceed` to fallback for advanced `CeedOperator` functionality
98eaf62fffSJeremy L Thompson 
99ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to create fallback for
100eaf62fffSJeremy L Thompson 
101eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
102eaf62fffSJeremy L Thompson 
103eaf62fffSJeremy L Thompson   @ref Developer
104eaf62fffSJeremy L Thompson **/
CeedOperatorCreateFallback(CeedOperator op)105d04bbc78SJeremy L Thompson static int CeedOperatorCreateFallback(CeedOperator op) {
1061c66c397SJeremy L Thompson   bool         is_composite;
1071203703bSJeremy L Thompson   Ceed         ceed, ceed_fallback;
1081c66c397SJeremy L Thompson   CeedOperator op_fallback;
109eaf62fffSJeremy L Thompson 
110805fe78eSJeremy L Thompson   // Check not already created
111805fe78eSJeremy L Thompson   if (op->op_fallback) return CEED_ERROR_SUCCESS;
112805fe78eSJeremy L Thompson 
113eaf62fffSJeremy L Thompson   // Fallback Ceed
1141203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1151203703bSJeremy L Thompson   CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
1169bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
117d04bbc78SJeremy L Thompson   if (!ceed_fallback) return CEED_ERROR_SUCCESS;
118d04bbc78SJeremy L Thompson 
1199bc66399SJeremy L Thompson   CeedDebug(CeedOperatorReturnCeed(op), "Creating fallback CeedOperator\n");
120eaf62fffSJeremy L Thompson 
121eaf62fffSJeremy L Thompson   // Clone Op
122b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
123b275c451SJeremy L Thompson   if (is_composite) {
124b275c451SJeremy L Thompson     CeedInt       num_suboperators;
125b275c451SJeremy L Thompson     CeedOperator *sub_operators;
126b275c451SJeremy L Thompson 
127ed094490SJeremy L Thompson     CeedCall(CeedOperatorCreateComposite(ceed_fallback, &op_fallback));
128ed094490SJeremy L Thompson     CeedCall(CeedOperatorCompositeGetNumSub(op, &num_suboperators));
129ed094490SJeremy L Thompson     CeedCall(CeedOperatorCompositeGetSubList(op, &sub_operators));
130b275c451SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
131d04bbc78SJeremy L Thompson       CeedOperator op_sub_fallback;
132d04bbc78SJeremy L Thompson 
133b275c451SJeremy L Thompson       CeedCall(CeedOperatorGetFallback(sub_operators[i], &op_sub_fallback));
134ed094490SJeremy L Thompson       CeedCall(CeedOperatorCompositeAddSub(op_fallback, op_sub_fallback));
135805fe78eSJeremy L Thompson     }
136805fe78eSJeremy L Thompson   } else {
137bcd92680SJeremy L Thompson     bool               is_at_points = false;
1381203703bSJeremy L Thompson     CeedInt            num_input_fields, num_output_fields;
1399e77b9c8SJeremy L Thompson     CeedQFunction      qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL;
1401203703bSJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
1411c66c397SJeremy L Thompson 
1422b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback));
1432b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback));
1442b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback));
145bcd92680SJeremy L Thompson     CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
146bcd92680SJeremy L Thompson     if (is_at_points) {
147bcd92680SJeremy L Thompson       CeedVector          points;
148bcd92680SJeremy L Thompson       CeedElemRestriction rstr_points;
149bcd92680SJeremy L Thompson 
150bcd92680SJeremy L Thompson       CeedCall(CeedOperatorCreateAtPoints(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback));
151bcd92680SJeremy L Thompson       CeedCall(CeedOperatorAtPointsGetPoints(op, &rstr_points, &points));
152bcd92680SJeremy L Thompson       CeedCall(CeedOperatorAtPointsSetPoints(op_fallback, rstr_points, points));
153bcd92680SJeremy L Thompson       CeedCall(CeedVectorDestroy(&points));
154bcd92680SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&rstr_points));
155bcd92680SJeremy L Thompson     } else {
1562b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback));
157bcd92680SJeremy L Thompson     }
1581203703bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1591203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1606f8994e9SJeremy L Thompson       const char         *field_name;
1611203703bSJeremy L Thompson       CeedVector          vec;
1621203703bSJeremy L Thompson       CeedElemRestriction rstr;
1631203703bSJeremy L Thompson       CeedBasis           basis;
1641203703bSJeremy L Thompson 
165ab747706SJeremy L Thompson       CeedCall(CeedOperatorFieldGetData(input_fields[i], &field_name, &rstr, &basis, &vec));
1661203703bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec));
167681d0ea7SJeremy L Thompson       CeedCall(CeedVectorDestroy(&vec));
168681d0ea7SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&rstr));
169681d0ea7SJeremy L Thompson       CeedCall(CeedBasisDestroy(&basis));
170805fe78eSJeremy L Thompson     }
1711203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1726f8994e9SJeremy L Thompson       const char         *field_name;
1731203703bSJeremy L Thompson       CeedVector          vec;
1741203703bSJeremy L Thompson       CeedElemRestriction rstr;
1751203703bSJeremy L Thompson       CeedBasis           basis;
1761203703bSJeremy L Thompson 
177ab747706SJeremy L Thompson       CeedCall(CeedOperatorFieldGetData(output_fields[i], &field_name, &rstr, &basis, &vec));
1781203703bSJeremy L Thompson       CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec));
179681d0ea7SJeremy L Thompson       CeedCall(CeedVectorDestroy(&vec));
180681d0ea7SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&rstr));
181681d0ea7SJeremy L Thompson       CeedCall(CeedBasisDestroy(&basis));
182805fe78eSJeremy L Thompson     }
1837d5185d7SSebastian Grimberg     {
1847d5185d7SSebastian Grimberg       CeedQFunctionAssemblyData data;
1857d5185d7SSebastian Grimberg 
1867d5185d7SSebastian Grimberg       CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
1877d5185d7SSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataReferenceCopy(data, &op_fallback->qf_assembled));
1887d5185d7SSebastian Grimberg     }
1899e77b9c8SJeremy L Thompson     // Cleanup
1902b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf_fallback));
1912b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqf_fallback));
1922b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&dqfT_fallback));
193805fe78eSJeremy L Thompson   }
1942b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetName(op_fallback, op->name));
1952b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fallback));
196b05f7e9fSJeremy L Thompson   // Note: No ref-counting here so we don't get caught in a reference loop.
197b05f7e9fSJeremy L Thompson   //       The op holds the only reference to op_fallback and is responsible for deleting itself and op_fallback.
198805fe78eSJeremy L Thompson   op->op_fallback                 = op_fallback;
199b05f7e9fSJeremy L Thompson   op_fallback->op_fallback_parent = op;
2009bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed_fallback));
201eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
202eaf62fffSJeremy L Thompson }
203eaf62fffSJeremy L Thompson 
204eaf62fffSJeremy L Thompson /**
205eaf62fffSJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
206eaf62fffSJeremy L Thompson 
2070cd9fdf4SJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble diagonal or point block diagonal
208ca94c3ddSJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
209bd83916cSSebastian Grimberg   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
210ca94c3ddSJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
211eaf62fffSJeremy L Thompson 
212eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
213eaf62fffSJeremy L Thompson 
214eaf62fffSJeremy L Thompson   @ref Developer
215eaf62fffSJeremy L Thompson **/
CeedOperatorLinearAssembleAddDiagonalSingle_Mesh(CeedOperator op,CeedRequest * request,const bool is_point_block,CeedVector assembled)216ed094490SJeremy L Thompson static inline int CeedOperatorLinearAssembleAddDiagonalSingle_Mesh(CeedOperator op, CeedRequest *request, const bool is_point_block,
217f3bd9308SJeremy L Thompson                                                                    CeedVector assembled) {
218506b1a0cSSebastian Grimberg   bool is_composite;
219506b1a0cSSebastian Grimberg 
220506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2219bc66399SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
222506b1a0cSSebastian Grimberg 
223506b1a0cSSebastian Grimberg   // Assemble QFunction
224506b1a0cSSebastian Grimberg   CeedInt             layout_qf[3];
225437c7c90SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
226c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
227c5f45aeaSJeremy L Thompson   CeedElemRestriction assembled_elem_rstr = NULL;
228437c7c90SJeremy L Thompson 
229437c7c90SJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request));
23056c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf));
231437c7c90SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
232437c7c90SJeremy L Thompson   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
233eaf62fffSJeremy L Thompson 
234ed9e99e6SJeremy L Thompson   // Get assembly data
235437c7c90SJeremy L Thompson   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
236506b1a0cSSebastian Grimberg   CeedInt                  num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out;
237437c7c90SJeremy L Thompson   CeedSize               **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components;
238506b1a0cSSebastian Grimberg   CeedBasis               *active_bases_in, *active_bases_out;
239506b1a0cSSebastian Grimberg   CeedElemRestriction     *active_elem_rstrs_in, *active_elem_rstrs_out;
2401c66c397SJeremy L Thompson   CeedOperatorAssemblyData data;
2411c66c397SJeremy L Thompson 
242437c7c90SJeremy L Thompson   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
243506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in,
244506b1a0cSSebastian Grimberg                                                 &num_active_bases_out, &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out,
245506b1a0cSSebastian Grimberg                                                 &num_output_components));
246506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, NULL, NULL, &active_bases_out, NULL));
247506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs_in, NULL, &active_elem_rstrs_out));
248506b1a0cSSebastian Grimberg 
249934a29f5SSebastian Grimberg   // Loop over all active bases (find matching input/output pairs)
250934a29f5SSebastian Grimberg   for (CeedInt b = 0; b < CeedIntMin(num_active_bases_in, num_active_bases_out); b++) {
251934a29f5SSebastian Grimberg     CeedInt             b_in, b_out, num_elem, num_nodes, num_qpts, num_comp;
2521c66c397SJeremy L Thompson     bool                has_eval_none = false;
2531c66c397SJeremy L Thompson     CeedScalar         *elem_diag_array, *identity = NULL;
2541c66c397SJeremy L Thompson     CeedVector          elem_diag;
2557c1dbaffSSebastian Grimberg     CeedElemRestriction diag_elem_rstr;
2561c66c397SJeremy L Thompson 
257934a29f5SSebastian Grimberg     if (num_active_bases_in <= num_active_bases_out) {
258934a29f5SSebastian Grimberg       b_in = b;
259934a29f5SSebastian Grimberg       for (b_out = 0; b_out < num_active_bases_out; b_out++) {
260934a29f5SSebastian Grimberg         if (active_bases_in[b_in] == active_bases_out[b_out]) {
261934a29f5SSebastian Grimberg           break;
262934a29f5SSebastian Grimberg         }
263934a29f5SSebastian Grimberg       }
264934a29f5SSebastian Grimberg       if (b_out == num_active_bases_out) {
265934a29f5SSebastian Grimberg         continue;
266934a29f5SSebastian Grimberg       }  // No matching output basis found
267934a29f5SSebastian Grimberg     } else {
268934a29f5SSebastian Grimberg       b_out = b;
269934a29f5SSebastian Grimberg       for (b_in = 0; b_in < num_active_bases_in; b_in++) {
270934a29f5SSebastian Grimberg         if (active_bases_in[b_in] == active_bases_out[b_out]) {
271934a29f5SSebastian Grimberg           break;
272934a29f5SSebastian Grimberg         }
273934a29f5SSebastian Grimberg       }
274934a29f5SSebastian Grimberg       if (b_in == num_active_bases_in) {
275934a29f5SSebastian Grimberg         continue;
276934a29f5SSebastian Grimberg       }  // No matching output basis found
277934a29f5SSebastian Grimberg     }
2789bc66399SJeremy L Thompson     CeedCheck(active_elem_rstrs_in[b_in] == active_elem_rstrs_out[b_out], CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
279506b1a0cSSebastian Grimberg               "Cannot assemble operator diagonal with different input and output active element restrictions");
280506b1a0cSSebastian Grimberg 
2811c66c397SJeremy L Thompson     // Assemble point block diagonal restriction, if needed
282bd83916cSSebastian Grimberg     if (is_point_block) {
283934a29f5SSebastian Grimberg       CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstrs_in[b_in], &diag_elem_rstr));
2847c1dbaffSSebastian Grimberg     } else {
285934a29f5SSebastian Grimberg       CeedCall(CeedElemRestrictionCreateUnsignedCopy(active_elem_rstrs_in[b_in], &diag_elem_rstr));
286eaf62fffSJeremy L Thompson     }
287eaf62fffSJeremy L Thompson 
288eaf62fffSJeremy L Thompson     // Create diagonal vector
289437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag));
290eaf62fffSJeremy L Thompson 
291eaf62fffSJeremy L Thompson     // Assemble element operator diagonals
2922b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(elem_diag, 0.0));
2932b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array));
294437c7c90SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem));
295934a29f5SSebastian Grimberg     CeedCall(CeedBasisGetNumNodes(active_bases_in[b_in], &num_nodes));
296934a29f5SSebastian Grimberg     CeedCall(CeedBasisGetNumComponents(active_bases_in[b_in], &num_comp));
297934a29f5SSebastian Grimberg     if (active_bases_in[b_in] == CEED_BASIS_NONE) num_qpts = num_nodes;
298934a29f5SSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(active_bases_in[b_in], &num_qpts));
299ed9e99e6SJeremy L Thompson 
300352a5e7cSSebastian Grimberg     // Construct identity matrix for basis if required
301934a29f5SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in[b_in]; i++) {
302934a29f5SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[b_in][i] == CEED_EVAL_NONE);
303ed9e99e6SJeremy L Thompson     }
304934a29f5SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out[b_out]; i++) {
305934a29f5SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[b_out][i] == CEED_EVAL_NONE);
306ed9e99e6SJeremy L Thompson     }
307ed9e99e6SJeremy L Thompson     if (has_eval_none) {
3082b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
3092b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
310eaf62fffSJeremy L Thompson     }
311352a5e7cSSebastian Grimberg 
312eaf62fffSJeremy L Thompson     // Compute the diagonal of B^T D B
313eaf62fffSJeremy L Thompson     // Each element
314b94338b9SJed Brown     for (CeedSize e = 0; e < num_elem; e++) {
315eaf62fffSJeremy L Thompson       // Each basis eval mode pair
316352a5e7cSSebastian Grimberg       CeedInt      d_out              = 0, q_comp_out;
317352a5e7cSSebastian Grimberg       CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
3181c66c397SJeremy L Thompson 
319934a29f5SSebastian Grimberg       for (CeedInt e_out = 0; e_out < num_eval_modes_out[b_out]; e_out++) {
3201c66c397SJeremy L Thompson         CeedInt           d_in              = 0, q_comp_in;
321437c7c90SJeremy L Thompson         const CeedScalar *B_t               = NULL;
3221c66c397SJeremy L Thompson         CeedEvalMode      eval_mode_in_prev = CEED_EVAL_NONE;
3231c66c397SJeremy L Thompson 
324934a29f5SSebastian Grimberg         CeedCall(CeedOperatorGetBasisPointer(active_bases_out[b_out], eval_modes_out[b_out][e_out], identity, &B_t));
325934a29f5SSebastian Grimberg         CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_out[b_out], eval_modes_out[b_out][e_out], &q_comp_out));
326352a5e7cSSebastian Grimberg         if (q_comp_out > 1) {
327934a29f5SSebastian Grimberg           if (e_out == 0 || eval_modes_out[b_out][e_out] != eval_mode_out_prev) d_out = 0;
328352a5e7cSSebastian Grimberg           else B_t = &B_t[(++d_out) * num_qpts * num_nodes];
329352a5e7cSSebastian Grimberg         }
330934a29f5SSebastian Grimberg         eval_mode_out_prev = eval_modes_out[b_out][e_out];
331352a5e7cSSebastian Grimberg 
332934a29f5SSebastian Grimberg         for (CeedInt e_in = 0; e_in < num_eval_modes_in[b_in]; e_in++) {
333437c7c90SJeremy L Thompson           const CeedScalar *B = NULL;
3341c66c397SJeremy L Thompson 
335934a29f5SSebastian Grimberg           CeedCall(CeedOperatorGetBasisPointer(active_bases_in[b_in], eval_modes_in[b_in][e_in], identity, &B));
336934a29f5SSebastian Grimberg           CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_in[b_in], eval_modes_in[b_in][e_in], &q_comp_in));
337352a5e7cSSebastian Grimberg           if (q_comp_in > 1) {
338934a29f5SSebastian Grimberg             if (e_in == 0 || eval_modes_in[b_in][e_in] != eval_mode_in_prev) d_in = 0;
339352a5e7cSSebastian Grimberg             else B = &B[(++d_in) * num_qpts * num_nodes];
340352a5e7cSSebastian Grimberg           }
341934a29f5SSebastian Grimberg           eval_mode_in_prev = eval_modes_in[b_in][e_in];
342352a5e7cSSebastian Grimberg 
343eaf62fffSJeremy L Thompson           // Each component
344506b1a0cSSebastian Grimberg           for (CeedInt c_out = 0; c_out < num_comp; c_out++) {
345437c7c90SJeremy L Thompson             // Each qpt/node pair
3462b730f8bSJeremy L Thompson             for (CeedInt q = 0; q < num_qpts; q++) {
347bd83916cSSebastian Grimberg               if (is_point_block) {
348eaf62fffSJeremy L Thompson                 // Point Block Diagonal
349506b1a0cSSebastian Grimberg                 for (CeedInt c_in = 0; c_in < num_comp; c_in++) {
350934a29f5SSebastian Grimberg                   const CeedSize c_offset =
351934a29f5SSebastian Grimberg                       (eval_mode_offsets_in[b_in][e_in] + c_in) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out;
352506b1a0cSSebastian Grimberg                   const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]];
3531c66c397SJeremy L Thompson 
3542b730f8bSJeremy L Thompson                   for (CeedInt n = 0; n < num_nodes; n++) {
355506b1a0cSSebastian Grimberg                     elem_diag_array[((e * num_comp + c_out) * num_comp + c_in) * num_nodes + n] +=
356437c7c90SJeremy L Thompson                         B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n];
357eaf62fffSJeremy L Thompson                   }
3582b730f8bSJeremy L Thompson                 }
359eaf62fffSJeremy L Thompson               } else {
360eaf62fffSJeremy L Thompson                 // Diagonal Only
361934a29f5SSebastian Grimberg                 const CeedInt c_offset =
362934a29f5SSebastian Grimberg                     (eval_mode_offsets_in[b_in][e_in] + c_out) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out;
363506b1a0cSSebastian Grimberg                 const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]];
3641c66c397SJeremy L Thompson 
3652b730f8bSJeremy L Thompson                 for (CeedInt n = 0; n < num_nodes; n++) {
366506b1a0cSSebastian 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];
367eaf62fffSJeremy L Thompson                 }
368eaf62fffSJeremy L Thompson               }
369eaf62fffSJeremy L Thompson             }
370eaf62fffSJeremy L Thompson           }
3712b730f8bSJeremy L Thompson         }
3722b730f8bSJeremy L Thompson       }
3732b730f8bSJeremy L Thompson     }
3742b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
375eaf62fffSJeremy L Thompson 
376eaf62fffSJeremy L Thompson     // Assemble local operator diagonal
3777c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
378eaf62fffSJeremy L Thompson 
379eaf62fffSJeremy L Thompson     // Cleanup
3807c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr));
3812b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&elem_diag));
3822b730f8bSJeremy L Thompson     CeedCall(CeedFree(&identity));
383437c7c90SJeremy L Thompson   }
384437c7c90SJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
385437c7c90SJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
386eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
387eaf62fffSJeremy L Thompson }
388eaf62fffSJeremy L Thompson 
389eaf62fffSJeremy L Thompson /**
3900cd9fdf4SJeremy L Thompson   @brief Core logic for assembling operator diagonal or point block diagonal
3910cd9fdf4SJeremy L Thompson 
3920cd9fdf4SJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble diagonal or point block diagonal
3930cd9fdf4SJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
3940cd9fdf4SJeremy L Thompson   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
3950cd9fdf4SJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
3960cd9fdf4SJeremy L Thompson 
3970cd9fdf4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3980cd9fdf4SJeremy L Thompson 
3990cd9fdf4SJeremy L Thompson   @ref Developer
4000cd9fdf4SJeremy L Thompson **/
CeedOperatorLinearAssembleAddDiagonalSingle(CeedOperator op,CeedRequest * request,const bool is_point_block,CeedVector assembled)401ed094490SJeremy L Thompson static inline int CeedOperatorLinearAssembleAddDiagonalSingle(CeedOperator op, CeedRequest *request, const bool is_point_block,
4020cd9fdf4SJeremy L Thompson                                                               CeedVector assembled) {
4030cd9fdf4SJeremy L Thompson   bool is_at_points;
4040cd9fdf4SJeremy L Thompson 
4050cd9fdf4SJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
4069bc66399SJeremy L Thompson   CeedCheck(!is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "AtPoints operator not supported");
407ed094490SJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonalSingle_Mesh(op, request, is_point_block, assembled));
4080cd9fdf4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4090cd9fdf4SJeremy L Thompson }
4100cd9fdf4SJeremy L Thompson 
4110cd9fdf4SJeremy L Thompson /**
412eaf62fffSJeremy L Thompson   @brief Core logic for assembling composite operator diagonal
413eaf62fffSJeremy L Thompson 
414ca94c3ddSJeremy L Thompson   @param[in]  op             `CeedOperator` to assemble point block diagonal
415ca94c3ddSJeremy L Thompson   @param[in]  request        Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
416bd83916cSSebastian Grimberg   @param[in]  is_point_block Boolean flag to assemble diagonal or point block diagonal
417ca94c3ddSJeremy L Thompson   @param[out] assembled      `CeedVector` to store assembled diagonal
418eaf62fffSJeremy L Thompson 
419eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
420eaf62fffSJeremy L Thompson 
421eaf62fffSJeremy L Thompson   @ref Developer
422eaf62fffSJeremy L Thompson **/
CeedOperatorLinearAssembleAddDiagonalComposite(CeedOperator op,CeedRequest * request,const bool is_point_block,CeedVector assembled)423ed094490SJeremy L Thompson static inline int CeedOperatorLinearAssembleAddDiagonalComposite(CeedOperator op, CeedRequest *request, const bool is_point_block,
424eaf62fffSJeremy L Thompson                                                                  CeedVector assembled) {
425eaf62fffSJeremy L Thompson   CeedInt       num_sub;
426eaf62fffSJeremy L Thompson   CeedOperator *suboperators;
4271c66c397SJeremy L Thompson 
428ed094490SJeremy L Thompson   CeedCall(CeedOperatorCompositeGetNumSub(op, &num_sub));
429ed094490SJeremy L Thompson   CeedCall(CeedOperatorCompositeGetSubList(op, &suboperators));
430eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_sub; i++) {
431bd83916cSSebastian Grimberg     if (is_point_block) {
4322b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request));
4336aa95790SJeremy L Thompson     } else {
4342b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request));
4356aa95790SJeremy L Thompson     }
436eaf62fffSJeremy L Thompson   }
437eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
438eaf62fffSJeremy L Thompson }
439eaf62fffSJeremy L Thompson 
440eaf62fffSJeremy L Thompson /**
441ca94c3ddSJeremy L Thompson   @brief Build nonzero pattern for non-composite CeedOperator`.
442eaf62fffSJeremy L Thompson 
443ca94c3ddSJeremy L Thompson   Users should generally use @ref CeedOperatorLinearAssembleSymbolic().
444eaf62fffSJeremy L Thompson 
445ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` to assemble nonzero pattern
446eaf62fffSJeremy L Thompson   @param[in]  offset Offset for number of entries
447eaf62fffSJeremy L Thompson   @param[out] rows   Row number for each entry
448eaf62fffSJeremy L Thompson   @param[out] cols   Column number for each entry
449eaf62fffSJeremy L Thompson 
450eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
451eaf62fffSJeremy L Thompson 
452eaf62fffSJeremy L Thompson   @ref Developer
453eaf62fffSJeremy L Thompson **/
CeedOperatorAssembleSymbolicSingle(CeedOperator op,CeedInt offset,CeedInt * rows,CeedInt * cols)454ed094490SJeremy L Thompson static int CeedOperatorAssembleSymbolicSingle(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) {
455f3d47e36SJeremy L Thompson   Ceed                ceed;
456f3d47e36SJeremy L Thompson   bool                is_composite;
45781670346SSebastian Grimberg   CeedSize            num_nodes_in, num_nodes_out, local_num_entries, count = 0;
458506b1a0cSSebastian Grimberg   CeedInt             num_elem_in, elem_size_in, num_comp_in, layout_er_in[3];
45981670346SSebastian Grimberg   CeedInt             num_elem_out, elem_size_out, num_comp_out, layout_er_out[3];
4601c66c397SJeremy L Thompson   CeedScalar         *array;
461506b1a0cSSebastian Grimberg   const CeedScalar   *elem_dof_a_in, *elem_dof_a_out;
462506b1a0cSSebastian Grimberg   CeedVector          index_vec_in, index_vec_out, elem_dof_in, elem_dof_out;
463506b1a0cSSebastian Grimberg   CeedElemRestriction elem_rstr_in, elem_rstr_out, index_elem_rstr_in, index_elem_rstr_out;
4641c66c397SJeremy L Thompson 
465f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4669bc66399SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
4675e1f751eSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
468eaf62fffSJeremy L Thompson 
469506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes_in, &num_nodes_out));
470506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out));
471506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in));
472506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in));
473506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in));
47456c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(elem_rstr_in, layout_er_in));
475eaf62fffSJeremy L Thompson 
476506b1a0cSSebastian Grimberg   // Determine elem_dof relation for input
477506b1a0cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed, num_nodes_in, &index_vec_in));
478506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayWrite(index_vec_in, CEED_MEM_HOST, &array));
479c81f2b9dSJames Wright   for (CeedSize i = 0; i < num_nodes_in; i++) array[i] = i;
480506b1a0cSSebastian Grimberg   CeedCall(CeedVectorRestoreArray(index_vec_in, &array));
481506b1a0cSSebastian Grimberg   CeedCall(CeedVectorCreate(ceed, num_elem_in * elem_size_in * num_comp_in, &elem_dof_in));
482506b1a0cSSebastian Grimberg   CeedCall(CeedVectorSetValue(elem_dof_in, 0.0));
483506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_in, &index_elem_rstr_in));
484506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionApply(index_elem_rstr_in, CEED_NOTRANSPOSE, index_vec_in, elem_dof_in, CEED_REQUEST_IMMEDIATE));
485506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayRead(elem_dof_in, CEED_MEM_HOST, &elem_dof_a_in));
486506b1a0cSSebastian Grimberg   CeedCall(CeedVectorDestroy(&index_vec_in));
487506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_in));
488506b1a0cSSebastian Grimberg 
489506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
490506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out));
491506b1a0cSSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
4923f08121cSJeremy L Thompson               "Active input and output operator restrictions must have the same number of elements."
4933f08121cSJeremy L Thompson               " Input has %" CeedInt_FMT " elements; output has %" CeedInt_FMT "elements.",
4943f08121cSJeremy L Thompson               num_elem_in, num_elem_out);
495506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out));
496506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out));
49756c48462SJeremy L Thompson     CeedCall(CeedElemRestrictionGetELayout(elem_rstr_out, layout_er_out));
498506b1a0cSSebastian Grimberg 
499506b1a0cSSebastian Grimberg     // Determine elem_dof relation for output
500506b1a0cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed, num_nodes_out, &index_vec_out));
501506b1a0cSSebastian Grimberg     CeedCall(CeedVectorGetArrayWrite(index_vec_out, CEED_MEM_HOST, &array));
502c81f2b9dSJames Wright     for (CeedSize i = 0; i < num_nodes_out; i++) array[i] = i;
503506b1a0cSSebastian Grimberg     CeedCall(CeedVectorRestoreArray(index_vec_out, &array));
504506b1a0cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed, num_elem_out * elem_size_out * num_comp_out, &elem_dof_out));
505506b1a0cSSebastian Grimberg     CeedCall(CeedVectorSetValue(elem_dof_out, 0.0));
506506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_out, &index_elem_rstr_out));
507506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(index_elem_rstr_out, CEED_NOTRANSPOSE, index_vec_out, elem_dof_out, CEED_REQUEST_IMMEDIATE));
508506b1a0cSSebastian Grimberg     CeedCall(CeedVectorGetArrayRead(elem_dof_out, CEED_MEM_HOST, &elem_dof_a_out));
509506b1a0cSSebastian Grimberg     CeedCall(CeedVectorDestroy(&index_vec_out));
510506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_out));
511506b1a0cSSebastian Grimberg   } else {
512506b1a0cSSebastian Grimberg     num_elem_out     = num_elem_in;
513506b1a0cSSebastian Grimberg     elem_size_out    = elem_size_in;
514506b1a0cSSebastian Grimberg     num_comp_out     = num_comp_in;
515506b1a0cSSebastian Grimberg     layout_er_out[0] = layout_er_in[0];
516506b1a0cSSebastian Grimberg     layout_er_out[1] = layout_er_in[1];
517506b1a0cSSebastian Grimberg     layout_er_out[2] = layout_er_in[2];
518506b1a0cSSebastian Grimberg     elem_dof_a_out   = elem_dof_a_in;
519506b1a0cSSebastian Grimberg   }
520c81f2b9dSJames Wright   local_num_entries = (CeedSize)elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in;
521eaf62fffSJeremy L Thompson 
522eaf62fffSJeremy L Thompson   // Determine i, j locations for element matrices
523506b1a0cSSebastian Grimberg   for (CeedInt e = 0; e < num_elem_in; e++) {
524506b1a0cSSebastian Grimberg     for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) {
525506b1a0cSSebastian Grimberg       for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) {
526506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < elem_size_out; i++) {
527506b1a0cSSebastian Grimberg           for (CeedInt j = 0; j < elem_size_in; j++) {
528506b1a0cSSebastian Grimberg             const CeedInt elem_dof_index_row = i * layout_er_out[0] + comp_out * layout_er_out[1] + e * layout_er_out[2];
529506b1a0cSSebastian Grimberg             const CeedInt elem_dof_index_col = j * layout_er_in[0] + comp_in * layout_er_in[1] + e * layout_er_in[2];
530506b1a0cSSebastian Grimberg             const CeedInt row                = elem_dof_a_out[elem_dof_index_row];
531506b1a0cSSebastian Grimberg             const CeedInt col                = elem_dof_a_in[elem_dof_index_col];
532eaf62fffSJeremy L Thompson 
533eaf62fffSJeremy L Thompson             rows[offset + count] = row;
534eaf62fffSJeremy L Thompson             cols[offset + count] = col;
535eaf62fffSJeremy L Thompson             count++;
536eaf62fffSJeremy L Thompson           }
537eaf62fffSJeremy L Thompson         }
538eaf62fffSJeremy L Thompson       }
539eaf62fffSJeremy L Thompson     }
540eaf62fffSJeremy L Thompson   }
5416574a04fSJeremy L Thompson   CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing assembled entries");
542506b1a0cSSebastian Grimberg   CeedCall(CeedVectorRestoreArrayRead(elem_dof_in, &elem_dof_a_in));
543506b1a0cSSebastian Grimberg   CeedCall(CeedVectorDestroy(&elem_dof_in));
544506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
545506b1a0cSSebastian Grimberg     CeedCall(CeedVectorRestoreArrayRead(elem_dof_out, &elem_dof_a_out));
546506b1a0cSSebastian Grimberg     CeedCall(CeedVectorDestroy(&elem_dof_out));
547506b1a0cSSebastian Grimberg   }
548681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&elem_rstr_in));
549681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&elem_rstr_out));
5509bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
551eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
552eaf62fffSJeremy L Thompson }
553eaf62fffSJeremy L Thompson 
554eaf62fffSJeremy L Thompson /**
5550816752eSJeremy L Thompson   @brief Core logic to assemble `CeedQFunction` and store result internally.
5560816752eSJeremy L Thompson 
5570816752eSJeremy L Thompson   Return copied references of stored data to the caller.
5580816752eSJeremy L Thompson   Caller is responsible for ownership and destruction of the copied references.
5590816752eSJeremy L Thompson   See also @ref CeedOperatorLinearAssembleQFunction().
5600816752eSJeremy L Thompson 
5610816752eSJeremy 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.
5620816752eSJeremy L Thompson         These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object.
5630816752eSJeremy L Thompson 
5640816752eSJeremy L Thompson   @param[in]  op         `CeedOperator` to assemble `CeedQFunction`
5650816752eSJeremy L Thompson   @param[in]  use_parent Boolean flag to check for fallback parent implementation
5660816752eSJeremy L Thompson   @param[out] assembled  `CeedVector` to store assembled `CeedQFunction` at quadrature points
5670816752eSJeremy L Thompson   @param[out] rstr       `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
5680816752eSJeremy L Thompson   @param[in]  request    Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
5690816752eSJeremy L Thompson 
5700816752eSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5710816752eSJeremy L Thompson 
5720816752eSJeremy L Thompson   @ref User
5730816752eSJeremy L Thompson **/
CeedOperatorLinearAssembleQFunctionBuildOrUpdate_Core(CeedOperator op,bool use_parent,CeedVector * assembled,CeedElemRestriction * rstr,CeedRequest * request)5740816752eSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionBuildOrUpdate_Core(CeedOperator op, bool use_parent, CeedVector *assembled, CeedElemRestriction *rstr,
5750816752eSJeremy L Thompson                                                                  CeedRequest *request) {
5760816752eSJeremy L Thompson   int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *) = NULL;
5770816752eSJeremy L Thompson   CeedOperator op_assemble                                                                           = NULL;
5780816752eSJeremy L Thompson   CeedOperator op_fallback_parent                                                                    = NULL;
5790816752eSJeremy L Thompson 
5800816752eSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
5810816752eSJeremy L Thompson 
5820816752eSJeremy L Thompson   // Determine if fallback parent or operator has implementation
5830816752eSJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParent(op, &op_fallback_parent));
5840816752eSJeremy L Thompson   if (op_fallback_parent && use_parent && op_fallback_parent->LinearAssembleQFunctionUpdate) {
5850816752eSJeremy L Thompson     // -- Backend version for op fallback parent is faster, if it exists
586ca38d01dSJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "Using fallback parent for CeedOperatorLinearAssembleQFunctionBuildOrUpdate\n");
5870816752eSJeremy L Thompson     LinearAssembleQFunctionUpdate = op_fallback_parent->LinearAssembleQFunctionUpdate;
5880816752eSJeremy L Thompson     op_assemble                   = op_fallback_parent;
5890816752eSJeremy L Thompson   } else if (op->LinearAssembleQFunctionUpdate) {
5900816752eSJeremy L Thompson     // -- Backend version for op
5910816752eSJeremy L Thompson     LinearAssembleQFunctionUpdate = op->LinearAssembleQFunctionUpdate;
5920816752eSJeremy L Thompson     op_assemble                   = op;
5930816752eSJeremy L Thompson   }
5940816752eSJeremy L Thompson 
5950816752eSJeremy L Thompson   // Assemble QFunction
5960816752eSJeremy L Thompson   if (LinearAssembleQFunctionUpdate) {
5970816752eSJeremy L Thompson     // Backend or fallback parent version
5980816752eSJeremy L Thompson     CeedQFunctionAssemblyData data;
5990816752eSJeremy L Thompson     bool                      data_is_setup;
6000816752eSJeremy L Thompson     CeedVector                assembled_vec  = NULL;
6010816752eSJeremy L Thompson     CeedElemRestriction       assembled_rstr = NULL;
6020816752eSJeremy L Thompson 
6030816752eSJeremy L Thompson     CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
6040816752eSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataIsSetup(data, &data_is_setup));
6050816752eSJeremy L Thompson     if (data_is_setup) {
6060816752eSJeremy L Thompson       bool update_needed;
6070816752eSJeremy L Thompson 
6080816752eSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataGetObjects(data, &assembled_vec, &assembled_rstr));
6090816752eSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(data, &update_needed));
6100816752eSJeremy L Thompson       if (update_needed) CeedCall(LinearAssembleQFunctionUpdate(op_assemble, assembled_vec, assembled_rstr, request));
6110816752eSJeremy L Thompson     } else {
6120816752eSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleQFunction(op_assemble, &assembled_vec, &assembled_rstr, request));
6130816752eSJeremy L Thompson       CeedCall(CeedQFunctionAssemblyDataSetObjects(data, assembled_vec, assembled_rstr));
6140816752eSJeremy L Thompson     }
6150816752eSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(data, false));
6160816752eSJeremy L Thompson 
6170816752eSJeremy L Thompson     // Copy reference from internally held copy
6180816752eSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled));
6190816752eSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr));
6200816752eSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled_vec));
6210816752eSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&assembled_rstr));
6220816752eSJeremy L Thompson   } else {
6230816752eSJeremy L Thompson     // Operator fallback
6240816752eSJeremy L Thompson     CeedOperator op_fallback;
6250816752eSJeremy L Thompson 
626ca38d01dSJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssembleQFunctionBuildOrUpdate\n");
6270816752eSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
6280816752eSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request));
6290816752eSJeremy L Thompson     else return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate");
6300816752eSJeremy L Thompson   }
6310816752eSJeremy L Thompson   return CEED_ERROR_SUCCESS;
6320816752eSJeremy L Thompson }
6330816752eSJeremy L Thompson 
6340816752eSJeremy L Thompson /**
6350816752eSJeremy L Thompson   @brief Assemble `CeedQFunction` and store result internally, but do not use fallback parent.
6360816752eSJeremy L Thompson 
6370816752eSJeremy L Thompson   Return copied references of stored data to the caller.
6380816752eSJeremy L Thompson   Caller is responsible for ownership and destruction of the copied references.
6390816752eSJeremy L Thompson   See also @ref CeedOperatorLinearAssembleQFunction().
6400816752eSJeremy L Thompson 
6410816752eSJeremy 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.
6420816752eSJeremy L Thompson         These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object.
6430816752eSJeremy L Thompson 
6440816752eSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
6450816752eSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points
6460816752eSJeremy L Thompson   @param[out] rstr      `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
6470816752eSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
6480816752eSJeremy L Thompson 
6490816752eSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6500816752eSJeremy L Thompson 
6510816752eSJeremy L Thompson   @ref Developer
6520816752eSJeremy L Thompson **/
CeedOperatorLinearAssembleQFunctionBuildOrUpdateFallback(CeedOperator op,CeedVector * assembled,CeedElemRestriction * rstr,CeedRequest * request)653ed094490SJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdateFallback(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr,
6540816752eSJeremy L Thompson                                                              CeedRequest *request) {
6550816752eSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionBuildOrUpdate_Core(op, false, assembled, rstr, request);
6560816752eSJeremy L Thompson }
6570816752eSJeremy L Thompson 
6580816752eSJeremy L Thompson /**
659ca94c3ddSJeremy L Thompson   @brief Assemble nonzero entries for non-composite `CeedOperator`.
660eaf62fffSJeremy L Thompson 
661ca94c3ddSJeremy L Thompson   Users should generally use @ref CeedOperatorLinearAssemble().
662eaf62fffSJeremy L Thompson 
663ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` to assemble
664ea61e9acSJeremy L Thompson   @param[in]  offset Offset for number of entries
665eaf62fffSJeremy L Thompson   @param[out] values Values to assemble into matrix
666eaf62fffSJeremy L Thompson 
667eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
668eaf62fffSJeremy L Thompson 
669eaf62fffSJeremy L Thompson   @ref Developer
670eaf62fffSJeremy L Thompson **/
CeedOperatorAssembleSingle(CeedOperator op,CeedInt offset,CeedVector values)671ed094490SJeremy L Thompson int CeedOperatorAssembleSingle(CeedOperator op, CeedInt offset, CeedVector values) {
6721b95d8c6SJeremy L Thompson   bool is_composite, is_at_points;
6731c66c397SJeremy L Thompson 
674f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
6759bc66399SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
676f3d47e36SJeremy L Thompson 
677f3d47e36SJeremy L Thompson   // Early exit for empty operator
678f3d47e36SJeremy L Thompson   {
679f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
680f3d47e36SJeremy L Thompson 
681f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
682f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
683f3d47e36SJeremy L Thompson   }
684eaf62fffSJeremy L Thompson 
685cefa2673SJeremy L Thompson   if (op->LinearAssembleSingle) {
686cefa2673SJeremy L Thompson     // Backend version
6872b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSingle(op, offset, values));
688cefa2673SJeremy L Thompson     return CEED_ERROR_SUCCESS;
689cefa2673SJeremy L Thompson   } else {
690cefa2673SJeremy L Thompson     // Operator fallback
691cefa2673SJeremy L Thompson     CeedOperator op_fallback;
692cefa2673SJeremy L Thompson 
693ed094490SJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorAssembleSingle\n");
6942b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
695cefa2673SJeremy L Thompson     if (op_fallback) {
696ed094490SJeremy L Thompson       CeedCall(CeedOperatorAssembleSingle(op_fallback, offset, values));
697cefa2673SJeremy L Thompson       return CEED_ERROR_SUCCESS;
698cefa2673SJeremy L Thompson     }
699cefa2673SJeremy L Thompson   }
700cefa2673SJeremy L Thompson 
7011b95d8c6SJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
7021b95d8c6SJeremy L Thompson   CeedCheck(!is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
7031b95d8c6SJeremy L Thompson             "Backend does not implement CeedOperatorLinearAssemble for AtPoints operator");
7041b95d8c6SJeremy L Thompson 
705eaf62fffSJeremy L Thompson   // Assemble QFunction
706506b1a0cSSebastian Grimberg   CeedInt             layout_qf[3];
7071c66c397SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
708c5f45aeaSJeremy L Thompson   CeedVector          assembled_qf        = NULL;
709506b1a0cSSebastian Grimberg   CeedElemRestriction assembled_elem_rstr = NULL;
710eaf62fffSJeremy L Thompson 
711506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, CEED_REQUEST_IMMEDIATE));
71256c48462SJeremy L Thompson   CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf));
713506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr));
714506b1a0cSSebastian Grimberg   CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array));
715eaf62fffSJeremy L Thompson 
716ed9e99e6SJeremy L Thompson   // Get assembly data
717506b1a0cSSebastian Grimberg   CeedInt                  num_elem_in, elem_size_in, num_comp_in, num_qpts_in;
71881670346SSebastian Grimberg   CeedInt                  num_elem_out, elem_size_out, num_comp_out, num_qpts_out;
71981670346SSebastian Grimberg   CeedSize                 local_num_entries, count = 0;
720506b1a0cSSebastian Grimberg   const CeedEvalMode     **eval_modes_in, **eval_modes_out;
721506b1a0cSSebastian Grimberg   CeedInt                  num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out;
722506b1a0cSSebastian Grimberg   CeedBasis               *active_bases_in, *active_bases_out, basis_in, basis_out;
723506b1a0cSSebastian Grimberg   const CeedScalar       **B_mats_in, **B_mats_out, *B_mat_in, *B_mat_out;
724506b1a0cSSebastian Grimberg   CeedElemRestriction      elem_rstr_in, elem_rstr_out;
725506b1a0cSSebastian Grimberg   CeedRestrictionType      elem_rstr_type_in, elem_rstr_type_out;
726506b1a0cSSebastian Grimberg   const bool              *elem_rstr_orients_in = NULL, *elem_rstr_orients_out = NULL;
727506b1a0cSSebastian Grimberg   const CeedInt8          *elem_rstr_curl_orients_in = NULL, *elem_rstr_curl_orients_out = NULL;
728506b1a0cSSebastian Grimberg   CeedOperatorAssemblyData data;
729eaf62fffSJeremy L Thompson 
730506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data));
731506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, NULL, &num_active_bases_out,
732506b1a0cSSebastian Grimberg                                                 &num_eval_modes_out, &eval_modes_out, NULL, NULL));
733506b1a0cSSebastian Grimberg 
7349bc66399SJeremy L Thompson   CeedCheck(num_active_bases_in == 1 && num_active_bases_out == 1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
735506b1a0cSSebastian Grimberg             "Cannot assemble operator with multiple active bases");
7369bc66399SJeremy L Thompson   CeedCheck(num_eval_modes_in[0] > 0 && num_eval_modes_out[0] > 0, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
7379bc66399SJeremy L Thompson             "Cannot assemble operator without inputs/outputs");
738eaf62fffSJeremy L Thompson 
739506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, &B_mats_in, NULL, &active_bases_out, &B_mats_out));
740506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out));
741506b1a0cSSebastian Grimberg   basis_in  = active_bases_in[0];
742506b1a0cSSebastian Grimberg   basis_out = active_bases_out[0];
743506b1a0cSSebastian Grimberg   B_mat_in  = B_mats_in[0];
744506b1a0cSSebastian Grimberg   B_mat_out = B_mats_out[0];
745eaf62fffSJeremy L Thompson 
746506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in));
747506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in));
748506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in));
749506b1a0cSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
750506b1a0cSSebastian Grimberg   else CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
751506b1a0cSSebastian Grimberg 
752506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetType(elem_rstr_in, &elem_rstr_type_in));
753506b1a0cSSebastian Grimberg   if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) {
754506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_orients_in));
755506b1a0cSSebastian Grimberg   } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
756506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_curl_orients_in));
7577c1dbaffSSebastian Grimberg   }
7587c1dbaffSSebastian Grimberg 
759506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
760506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out));
7619bc66399SJeremy L Thompson     CeedCheck(num_elem_in == num_elem_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
7623f08121cSJeremy L Thompson               "Active input and output operator restrictions must have the same number of elements."
7633f08121cSJeremy L Thompson               " Input has %" CeedInt_FMT " elements; output has %" CeedInt_FMT "elements.",
7643f08121cSJeremy L Thompson               num_elem_in, num_elem_out);
765506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out));
766506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out));
767506b1a0cSSebastian Grimberg     if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
768506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
7699bc66399SJeremy L Thompson     CeedCheck(num_qpts_in == num_qpts_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
7703f08121cSJeremy L Thompson               "Active input and output bases must have the same number of quadrature points."
7713f08121cSJeremy L Thompson               " Input has %" CeedInt_FMT " points; output has %" CeedInt_FMT "points.",
7723f08121cSJeremy L Thompson               num_qpts_in, num_qpts_out);
773eaf62fffSJeremy L Thompson 
774506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(elem_rstr_out, &elem_rstr_type_out));
775506b1a0cSSebastian Grimberg     if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) {
776506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_orients_out));
777506b1a0cSSebastian Grimberg     } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
778506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_curl_orients_out));
779506b1a0cSSebastian Grimberg     }
780506b1a0cSSebastian Grimberg   } else {
781506b1a0cSSebastian Grimberg     num_elem_out  = num_elem_in;
782506b1a0cSSebastian Grimberg     elem_size_out = elem_size_in;
783506b1a0cSSebastian Grimberg     num_comp_out  = num_comp_in;
784506b1a0cSSebastian Grimberg     num_qpts_out  = num_qpts_in;
785506b1a0cSSebastian Grimberg 
786506b1a0cSSebastian Grimberg     elem_rstr_orients_out      = elem_rstr_orients_in;
787506b1a0cSSebastian Grimberg     elem_rstr_curl_orients_out = elem_rstr_curl_orients_in;
788506b1a0cSSebastian Grimberg   }
789c81f2b9dSJames Wright   local_num_entries = (CeedSize)elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in;
790506b1a0cSSebastian Grimberg 
791506b1a0cSSebastian Grimberg   // Loop over elements and put in data structure
7927c1dbaffSSebastian Grimberg   // We store B_mat_in, B_mat_out, BTD, elem_mat in row-major order
7930459ebd3SSebastian Grimberg   CeedTensorContract contract;
794123d890dSSebastian Grimberg   CeedScalar        *vals, *BTD_mat = NULL, *elem_mat = NULL, *elem_mat_b = NULL;
795506b1a0cSSebastian Grimberg 
796c22497adSSebastian Grimberg   CeedCall(CeedBasisGetTensorContract(basis_in, &contract));
797123d890dSSebastian Grimberg   CeedCall(CeedCalloc(elem_size_out * num_qpts_in * num_eval_modes_in[0], &BTD_mat));
798123d890dSSebastian Grimberg   CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat));
799506b1a0cSSebastian Grimberg   if (elem_rstr_curl_orients_in || elem_rstr_curl_orients_out) CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat_b));
8001c66c397SJeremy L Thompson 
80128ec399dSJeremy L Thompson   CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals));
802506b1a0cSSebastian Grimberg   for (CeedSize e = 0; e < num_elem_in; e++) {
803506b1a0cSSebastian Grimberg     for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) {
804506b1a0cSSebastian Grimberg       for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) {
805ed9e99e6SJeremy L Thompson         // Compute B^T*D
806506b1a0cSSebastian Grimberg         for (CeedSize n = 0; n < elem_size_out; n++) {
807506b1a0cSSebastian Grimberg           for (CeedSize q = 0; q < num_qpts_in; q++) {
808437c7c90SJeremy L Thompson             for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) {
809506b1a0cSSebastian Grimberg               const CeedSize btd_index = n * (num_qpts_in * num_eval_modes_in[0]) + q * num_eval_modes_in[0] + e_in;
810067fd99fSJeremy L Thompson               CeedScalar     sum       = 0.0;
8111c66c397SJeremy L Thompson 
812437c7c90SJeremy L Thompson               for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) {
813506b1a0cSSebastian Grimberg                 const CeedSize b_out_index     = (q * num_eval_modes_out[0] + e_out) * elem_size_out + n;
814506b1a0cSSebastian 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;
815b94338b9SJed Brown                 const CeedSize qf_index        = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2];
8161c66c397SJeremy L Thompson 
817067fd99fSJeremy L Thompson                 sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index];
818eaf62fffSJeremy L Thompson               }
819067fd99fSJeremy L Thompson               BTD_mat[btd_index] = sum;
820ed9e99e6SJeremy L Thompson             }
821ed9e99e6SJeremy L Thompson           }
822eaf62fffSJeremy L Thompson         }
8237c1dbaffSSebastian Grimberg 
8247c1dbaffSSebastian Grimberg         // Form element matrix itself (for each block component)
825e4065a52SSebastian Grimberg         if (contract) {
8260459ebd3SSebastian Grimberg           CeedCall(CeedTensorContractApply(contract, 1, num_qpts_in * num_eval_modes_in[0], elem_size_in, elem_size_out, BTD_mat, CEED_NOTRANSPOSE,
8270459ebd3SSebastian Grimberg                                            false, B_mat_in, elem_mat));
828e4065a52SSebastian Grimberg         } else {
8299bc66399SJeremy L Thompson           Ceed ceed;
8309bc66399SJeremy L Thompson 
8319bc66399SJeremy L Thompson           CeedCall(CeedOperatorGetCeed(op, &ceed));
832e4065a52SSebastian 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]));
8339bc66399SJeremy L Thompson           CeedCall(CeedDestroy(&ceed));
834e4065a52SSebastian Grimberg         }
835eaf62fffSJeremy L Thompson 
8367c1dbaffSSebastian Grimberg         // Transform the element matrix if required
837506b1a0cSSebastian Grimberg         if (elem_rstr_orients_out) {
838506b1a0cSSebastian Grimberg           const bool *elem_orients = &elem_rstr_orients_out[e * elem_size_out];
8391c66c397SJeremy L Thompson 
840506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
841506b1a0cSSebastian Grimberg             const double orient = elem_orients[i] ? -1.0 : 1.0;
842506b1a0cSSebastian Grimberg 
843506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
844506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] *= orient;
8457c1dbaffSSebastian Grimberg             }
8467c1dbaffSSebastian Grimberg           }
847506b1a0cSSebastian Grimberg         } else if (elem_rstr_curl_orients_out) {
848506b1a0cSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_out[e * 3 * elem_size_out];
8491c66c397SJeremy L Thompson 
8507c1dbaffSSebastian Grimberg           // T^T*(B^T*D*B)
851506b1a0cSSebastian Grimberg           memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar));
852506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
853506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
854506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * i + 1] +
855506b1a0cSSebastian Grimberg                                                (i > 0 ? elem_mat_b[(i - 1) * elem_size_in + j] * elem_curl_orients[3 * i - 1] : 0.0) +
856506b1a0cSSebastian Grimberg                                                (i < elem_size_out - 1 ? elem_mat_b[(i + 1) * elem_size_in + j] * elem_curl_orients[3 * i + 3] : 0.0);
8577c1dbaffSSebastian Grimberg             }
8587c1dbaffSSebastian Grimberg           }
859506b1a0cSSebastian Grimberg         }
860506b1a0cSSebastian Grimberg         if (elem_rstr_orients_in) {
861506b1a0cSSebastian Grimberg           const bool *elem_orients = &elem_rstr_orients_in[e * elem_size_in];
862506b1a0cSSebastian Grimberg 
863506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
864506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
865506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] *= elem_orients[j] ? -1.0 : 1.0;
866506b1a0cSSebastian Grimberg             }
867506b1a0cSSebastian Grimberg           }
868506b1a0cSSebastian Grimberg         } else if (elem_rstr_curl_orients_in) {
869506b1a0cSSebastian Grimberg           const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_in[e * 3 * elem_size_in];
870506b1a0cSSebastian Grimberg 
871506b1a0cSSebastian Grimberg           // (B^T*D*B)*T
872506b1a0cSSebastian Grimberg           memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar));
873506b1a0cSSebastian Grimberg           for (CeedInt i = 0; i < elem_size_out; i++) {
874506b1a0cSSebastian Grimberg             for (CeedInt j = 0; j < elem_size_in; j++) {
875506b1a0cSSebastian Grimberg               elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * j + 1] +
876506b1a0cSSebastian Grimberg                                                (j > 0 ? elem_mat_b[i * elem_size_in + j - 1] * elem_curl_orients[3 * j - 1] : 0.0) +
877506b1a0cSSebastian Grimberg                                                (j < elem_size_in - 1 ? elem_mat_b[i * elem_size_in + j + 1] * elem_curl_orients[3 * j + 3] : 0.0);
8787c1dbaffSSebastian Grimberg             }
8797c1dbaffSSebastian Grimberg           }
8807c1dbaffSSebastian Grimberg         }
8817c1dbaffSSebastian Grimberg 
8827c1dbaffSSebastian Grimberg         // Put element matrix in coordinate data structure
883506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < elem_size_out; i++) {
884506b1a0cSSebastian Grimberg           for (CeedInt j = 0; j < elem_size_in; j++) {
885506b1a0cSSebastian Grimberg             vals[offset + count] = elem_mat[i * elem_size_in + j];
886eaf62fffSJeremy L Thompson             count++;
887eaf62fffSJeremy L Thompson           }
888eaf62fffSJeremy L Thompson         }
889eaf62fffSJeremy L Thompson       }
890eaf62fffSJeremy L Thompson     }
891eaf62fffSJeremy L Thompson   }
8929bc66399SJeremy L Thompson   CeedCheck(count == local_num_entries, CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR, "Error computing entries");
8932b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArray(values, &vals));
894eaf62fffSJeremy L Thompson 
895506b1a0cSSebastian Grimberg   // Cleanup
896123d890dSSebastian Grimberg   CeedCall(CeedFree(&BTD_mat));
897123d890dSSebastian Grimberg   CeedCall(CeedFree(&elem_mat));
898506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&elem_mat_b));
899506b1a0cSSebastian Grimberg   if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) {
900506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_in, &elem_rstr_orients_in));
901506b1a0cSSebastian Grimberg   } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
902506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_in, &elem_rstr_curl_orients_in));
903506b1a0cSSebastian Grimberg   }
904506b1a0cSSebastian Grimberg   if (elem_rstr_in != elem_rstr_out) {
905506b1a0cSSebastian Grimberg     if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) {
906506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_out, &elem_rstr_orients_out));
907506b1a0cSSebastian Grimberg     } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
908506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_out, &elem_rstr_curl_orients_out));
909506b1a0cSSebastian Grimberg     }
910506b1a0cSSebastian Grimberg   }
9112b730f8bSJeremy L Thompson   CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
9122b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&assembled_qf));
913681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&elem_rstr_in));
914681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&elem_rstr_out));
915eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
916eaf62fffSJeremy L Thompson }
917eaf62fffSJeremy L Thompson 
918eaf62fffSJeremy L Thompson /**
919ca94c3ddSJeremy L Thompson   @brief Count number of entries for assembled `CeedOperator`
920eaf62fffSJeremy L Thompson 
921ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to assemble
922eaf62fffSJeremy L Thompson   @param[out] num_entries Number of entries in assembled representation
923eaf62fffSJeremy L Thompson 
924eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
925eaf62fffSJeremy L Thompson 
926eaf62fffSJeremy L Thompson   @ref Utility
927eaf62fffSJeremy L Thompson **/
CeedOperatorAssemblyCountEntriesSingle(CeedOperator op,CeedSize * num_entries)928ed094490SJeremy L Thompson static int CeedOperatorAssemblyCountEntriesSingle(CeedOperator op, CeedSize *num_entries) {
929b275c451SJeremy L Thompson   bool                is_composite;
930506b1a0cSSebastian Grimberg   CeedInt             num_elem_in, elem_size_in, num_comp_in, num_elem_out, elem_size_out, num_comp_out;
931506b1a0cSSebastian Grimberg   CeedElemRestriction rstr_in, rstr_out;
932eaf62fffSJeremy L Thompson 
933b275c451SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
9349bc66399SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported");
935506b1a0cSSebastian Grimberg 
936506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
937506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
938506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
939506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
940506b1a0cSSebastian Grimberg   if (rstr_in != rstr_out) {
941506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
9429bc66399SJeremy L Thompson     CeedCheck(num_elem_in == num_elem_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
9433f08121cSJeremy L Thompson               "Active input and output operator restrictions must have the same number of elements."
9443f08121cSJeremy L Thompson               " Input has %" CeedInt_FMT " elements; output has %" CeedInt_FMT "elements.",
9453f08121cSJeremy L Thompson               num_elem_in, num_elem_out);
946506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
947506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
948506b1a0cSSebastian Grimberg   } else {
949506b1a0cSSebastian Grimberg     num_elem_out  = num_elem_in;
950506b1a0cSSebastian Grimberg     elem_size_out = elem_size_in;
951506b1a0cSSebastian Grimberg     num_comp_out  = num_comp_in;
952506b1a0cSSebastian Grimberg   }
953681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_in));
954681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_out));
955506b1a0cSSebastian Grimberg   *num_entries = (CeedSize)elem_size_in * num_comp_in * elem_size_out * num_comp_out * num_elem_in;
956eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
957eaf62fffSJeremy L Thompson }
958eaf62fffSJeremy L Thompson 
959eaf62fffSJeremy L Thompson /**
96056318ee4SZach Atkins   @brief Count number of entries for assembled `CeedOperator`
96156318ee4SZach Atkins 
96256318ee4SZach Atkins   @param[in]  op          `CeedOperator` to assemble
96356318ee4SZach Atkins   @param[out] num_entries Number of entries in assembled representation
96456318ee4SZach Atkins 
96556318ee4SZach Atkins   @return An error code: 0 - success, otherwise - failure
96656318ee4SZach Atkins 
96756318ee4SZach Atkins   @ref Utility
96856318ee4SZach Atkins **/
CeedOperatorLinearAssembleGetNumEntries(CeedOperator op,CeedSize * num_entries)96956318ee4SZach Atkins int CeedOperatorLinearAssembleGetNumEntries(CeedOperator op, CeedSize *num_entries) {
97056318ee4SZach Atkins   bool is_composite;
97156318ee4SZach Atkins 
97256318ee4SZach Atkins   CeedCall(CeedOperatorCheckReady(op));
97356318ee4SZach Atkins   CeedCall(CeedOperatorIsComposite(op, &is_composite));
97456318ee4SZach Atkins 
97556318ee4SZach Atkins   if (is_composite) {
97656318ee4SZach Atkins     CeedInt       num_suboperators;
97756318ee4SZach Atkins     CeedOperator *sub_operators;
97856318ee4SZach Atkins 
97956318ee4SZach Atkins     CeedCall(CeedOperatorCompositeGetNumSub(op, &num_suboperators));
98056318ee4SZach Atkins     CeedCall(CeedOperatorCompositeGetSubList(op, &sub_operators));
98156318ee4SZach Atkins 
98256318ee4SZach Atkins     *num_entries = 0;
98356318ee4SZach Atkins     for (CeedInt k = 0; k < num_suboperators; ++k) {
98456318ee4SZach Atkins       CeedSize single_entries;
98556318ee4SZach Atkins 
98656318ee4SZach Atkins       CeedCall(CeedOperatorAssemblyCountEntriesSingle(sub_operators[k], &single_entries));
98756318ee4SZach Atkins       *num_entries += single_entries;
98856318ee4SZach Atkins     }
98956318ee4SZach Atkins   } else {
99056318ee4SZach Atkins     CeedCall(CeedOperatorAssemblyCountEntriesSingle(op, num_entries));
99156318ee4SZach Atkins   }
99256318ee4SZach Atkins   return CEED_ERROR_SUCCESS;
99356318ee4SZach Atkins }
99456318ee4SZach Atkins 
99556318ee4SZach Atkins /**
996ca94c3ddSJeremy L Thompson   @brief Common code for creating a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator`
997eaf62fffSJeremy L Thompson 
998ca94c3ddSJeremy L Thompson   @param[in]  op_fine      Fine grid `CeedOperator`
999ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
1000ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid `CeedElemRestriction`
1001ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector `CeedBasis`
1002ca94c3ddSJeremy L Thompson   @param[in]  basis_c_to_f `CeedBasis` for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction operators
1003ca94c3ddSJeremy L Thompson   @param[out] op_coarse    Coarse grid `CeedOperator`
1004ca94c3ddSJeremy L Thompson   @param[out] op_prolong   Coarse to fine `CeedOperator`, or `NULL`
1005ca94c3ddSJeremy L Thompson   @param[out] op_restrict  Fine to coarse `CeedOperator`, or `NULL`
1006eaf62fffSJeremy L Thompson 
1007eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1008eaf62fffSJeremy L Thompson 
1009eaf62fffSJeremy L Thompson   @ref Developer
1010eaf62fffSJeremy L Thompson **/
CeedOperatorMultigridLevelCreateSingle_Core(CeedOperator op_fine,CeedVector p_mult_fine,CeedElemRestriction rstr_coarse,CeedBasis basis_coarse,CeedBasis basis_c_to_f,CeedOperator * op_coarse,CeedOperator * op_prolong,CeedOperator * op_restrict)1011ed094490SJeremy L Thompson static int CeedOperatorMultigridLevelCreateSingle_Core(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse,
1012ed094490SJeremy L Thompson                                                        CeedBasis basis_coarse, CeedBasis basis_c_to_f, CeedOperator *op_coarse,
1013ed094490SJeremy L Thompson                                                        CeedOperator *op_prolong, CeedOperator *op_restrict) {
10141c66c397SJeremy L Thompson   bool                is_composite;
1015eaf62fffSJeremy L Thompson   Ceed                ceed;
1016*397d7ab3SJeremy L Thompson   CeedInt             dim              = 0, num_comp, num_input_fields, num_output_fields;
101785bb9dcfSJeremy L Thompson   CeedVector          mult_vec         = NULL;
10181c66c397SJeremy L Thompson   CeedElemRestriction rstr_p_mult_fine = NULL, rstr_fine = NULL;
10191203703bSJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
10201c66c397SJeremy L Thompson 
10212b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
1022eaf62fffSJeremy L Thompson 
1023eaf62fffSJeremy L Thompson   // Check for composite operator
10242b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op_fine, &is_composite));
10256574a04fSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported");
1026eaf62fffSJeremy L Thompson 
1027eaf62fffSJeremy L Thompson   // Coarse Grid
102899641342SJeremy L Thompson   {
102999641342SJeremy L Thompson     bool is_at_points;
103099641342SJeremy L Thompson 
103199641342SJeremy L Thompson     CeedCall(CeedOperatorIsAtPoints(op_fine, &is_at_points));
103299641342SJeremy L Thompson     if (is_at_points) {
103399641342SJeremy L Thompson       CeedVector          point_coords;
103499641342SJeremy L Thompson       CeedElemRestriction rstr_points;
103599641342SJeremy L Thompson 
103699641342SJeremy L Thompson       CeedCall(CeedOperatorCreateAtPoints(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
103799641342SJeremy L Thompson       CeedCall(CeedOperatorAtPointsGetPoints(op_fine, &rstr_points, &point_coords));
103899641342SJeremy L Thompson       CeedCall(CeedOperatorAtPointsSetPoints(*op_coarse, rstr_points, point_coords));
103999641342SJeremy L Thompson       CeedCall(CeedVectorDestroy(&point_coords));
104099641342SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&rstr_points));
104199641342SJeremy L Thompson     } else {
10422b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse));
104399641342SJeremy L Thompson     }
104499641342SJeremy L Thompson   }
10451203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op_fine, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1046eaf62fffSJeremy L Thompson   // -- Clone input fields
10471203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
10486f8994e9SJeremy L Thompson     const char         *field_name;
10491203703bSJeremy L Thompson     CeedVector          vec;
1050681d0ea7SJeremy L Thompson     CeedElemRestriction rstr  = NULL;
1051681d0ea7SJeremy L Thompson     CeedBasis           basis = NULL;
10521203703bSJeremy L Thompson 
10531203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetName(input_fields[i], &field_name));
10541203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(input_fields[i], &vec));
10551203703bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
1056681d0ea7SJeremy L Thompson       CeedCall(CeedElemRestrictionReferenceCopy(rstr_coarse, &rstr));
1057681d0ea7SJeremy L Thompson       CeedCall(CeedBasisReferenceCopy(basis_coarse, &basis));
1058681d0ea7SJeremy L Thompson       if (!rstr_fine) CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_fine));
1059eaf62fffSJeremy L Thompson     } else {
10601203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr));
10611203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis));
1062eaf62fffSJeremy L Thompson     }
1063*397d7ab3SJeremy L Thompson     if (dim == 0) CeedCall(CeedBasisGetDimension(basis, &dim));
10641203703bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec));
1065681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
1066681d0ea7SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr));
1067681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis));
1068eaf62fffSJeremy L Thompson   }
1069eaf62fffSJeremy L Thompson   // -- Clone output fields
10701203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
10716f8994e9SJeremy L Thompson     const char         *field_name;
10721203703bSJeremy L Thompson     CeedVector          vec;
1073681d0ea7SJeremy L Thompson     CeedElemRestriction rstr  = NULL;
1074681d0ea7SJeremy L Thompson     CeedBasis           basis = NULL;
10751203703bSJeremy L Thompson 
10761203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetName(output_fields[i], &field_name));
10771203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
10781203703bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
1079681d0ea7SJeremy L Thompson       CeedCall(CeedElemRestrictionReferenceCopy(rstr_coarse, &rstr));
1080681d0ea7SJeremy L Thompson       CeedCall(CeedBasisReferenceCopy(basis_coarse, &basis));
1081681d0ea7SJeremy L Thompson       if (!rstr_fine) CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_fine));
1082eaf62fffSJeremy L Thompson     } else {
10831203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr));
10841203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1085eaf62fffSJeremy L Thompson     }
1086*397d7ab3SJeremy L Thompson     if (dim == 0) CeedCall(CeedBasisGetDimension(basis, &dim));
10871203703bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec));
1088681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
1089681d0ea7SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr));
1090681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis));
1091eaf62fffSJeremy L Thompson   }
1092*397d7ab3SJeremy L Thompson   dim = dim ? dim : 1;
1093af99e877SJeremy L Thompson   // -- Clone QFunctionAssemblyData
10947d5185d7SSebastian Grimberg   {
10957d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData fine_data;
10967d5185d7SSebastian Grimberg 
10977d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op_fine, &fine_data));
10987d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataReferenceCopy(fine_data, &(*op_coarse)->qf_assembled));
10997d5185d7SSebastian Grimberg   }
1100eaf62fffSJeremy L Thompson 
1101eaf62fffSJeremy L Thompson   // Multiplicity vector
1102*397d7ab3SJeremy L Thompson   bool use_scalar_mult = true;
1103*397d7ab3SJeremy L Thompson 
11047758292fSSebastian Grimberg   if (op_restrict || op_prolong) {
110532db0c4dSJeremy L Thompson     CeedInt             num_elem, num_comp, elem_size;
110632db0c4dSJeremy L Thompson     CeedVector          mult_l_vec, mult_e_vec;
11071c66c397SJeremy L Thompson     CeedRestrictionType rstr_type;
110832db0c4dSJeremy L Thompson     CeedElemRestriction rstr_p_mult_full;
110985bb9dcfSJeremy L Thompson 
11107c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr_fine, &rstr_type));
11117c1dbaffSSebastian Grimberg     CeedCheck(rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_UNSUPPORTED,
11127c1dbaffSSebastian Grimberg               "Element restrictions created with CeedElemRestrictionCreateCurlOriented are not supported");
11136574a04fSJeremy L Thompson     CeedCheck(p_mult_fine, ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector");
111432db0c4dSJeremy L Thompson 
111532db0c4dSJeremy L Thompson     // Create multiplicity multi-component l-vector
111632db0c4dSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateUnsignedCopy(rstr_fine, &rstr_p_mult_full));
111732db0c4dSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_l_vec, &mult_e_vec));
11182b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_e_vec, 0.0));
111932db0c4dSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_full, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE));
112032db0c4dSJeremy L Thompson     CeedCall(CeedVectorSetValue(mult_l_vec, 0.0));
112132db0c4dSJeremy L Thompson     CeedCall(CeedElemRestrictionApply(rstr_p_mult_full, CEED_TRANSPOSE, mult_e_vec, mult_l_vec, CEED_REQUEST_IMMEDIATE));
112232db0c4dSJeremy L Thompson     CeedCall(CeedVectorReciprocal(mult_l_vec));
112332db0c4dSJeremy L Thompson 
1124*397d7ab3SJeremy L Thompson     // Determine to use scalar multiplicity or not
1125*397d7ab3SJeremy L Thompson     {
1126*397d7ab3SJeremy L Thompson       const CeedInt p = pow(elem_size, 1.0 / dim);
1127*397d7ab3SJeremy L Thompson 
1128*397d7ab3SJeremy L Thompson       use_scalar_mult = num_comp > 1 && (dim < 3 || num_comp - 1 > (3 * (pow(p, dim - 1) - pow(p, dim - 2)) + 1) / pow(p - 1, dim));
1129*397d7ab3SJeremy L Thompson     }
1130*397d7ab3SJeremy L Thompson 
1131*397d7ab3SJeremy L Thompson     if (use_scalar_mult) {
113232db0c4dSJeremy L Thompson       // Create multiplicity single component e-vector
113332db0c4dSJeremy L Thompson       CeedCall(CeedElemRestrictionGetNumElements(rstr_p_mult_full, &num_elem));
113432db0c4dSJeremy L Thompson       CeedCall(CeedElemRestrictionGetNumComponents(rstr_p_mult_full, &num_comp));
113532db0c4dSJeremy L Thompson       CeedCall(CeedElemRestrictionGetElementSize(rstr_p_mult_full, &elem_size));
113632db0c4dSJeremy L Thompson       CeedCall(CeedElemRestrictionCreateStrided(ceed, num_elem, elem_size, 1, num_elem * elem_size, CEED_STRIDES_BACKEND, &rstr_p_mult_fine));
113732db0c4dSJeremy L Thompson       CeedCall(CeedElemRestrictionCreateVector(rstr_p_mult_fine, &mult_vec, NULL));
113832db0c4dSJeremy L Thompson       {
113932db0c4dSJeremy L Thompson         CeedQFunction qf_to_scalar;
114032db0c4dSJeremy L Thompson         CeedOperator  op_to_scalar;
114132db0c4dSJeremy L Thompson 
114232db0c4dSJeremy L Thompson         CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Identity to scalar", &qf_to_scalar));
114332db0c4dSJeremy L Thompson         CeedCall(CeedQFunctionAddInput(qf_to_scalar, "input", num_comp, CEED_EVAL_NONE));
114432db0c4dSJeremy L Thompson         CeedCall(CeedQFunctionAddOutput(qf_to_scalar, "output", 1, CEED_EVAL_NONE));
114532db0c4dSJeremy L Thompson 
114632db0c4dSJeremy L Thompson         CeedCall(CeedOperatorCreate(ceed, qf_to_scalar, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_to_scalar));
114732db0c4dSJeremy L Thompson         CeedCall(CeedOperatorSetField(op_to_scalar, "input", rstr_p_mult_full, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
114832db0c4dSJeremy L Thompson         CeedCall(CeedOperatorSetField(op_to_scalar, "output", rstr_p_mult_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
114932db0c4dSJeremy L Thompson 
115032db0c4dSJeremy L Thompson         CeedCall(CeedOperatorApply(op_to_scalar, mult_l_vec, mult_vec, CEED_REQUEST_IMMEDIATE));
115132db0c4dSJeremy L Thompson 
115232db0c4dSJeremy L Thompson         // Clean-up
115332db0c4dSJeremy L Thompson         CeedCall(CeedQFunctionDestroy(&qf_to_scalar));
115432db0c4dSJeremy L Thompson         CeedCall(CeedOperatorDestroy(&op_to_scalar));
115532db0c4dSJeremy L Thompson       }
1156*397d7ab3SJeremy L Thompson     } else {
1157*397d7ab3SJeremy L Thompson       mult_vec = NULL;
1158*397d7ab3SJeremy L Thompson       CeedCall(CeedVectorReferenceCopy(mult_l_vec, &mult_vec));
1159*397d7ab3SJeremy L Thompson       rstr_p_mult_fine = NULL;
1160*397d7ab3SJeremy L Thompson       CeedCall(CeedElemRestrictionReferenceCopy(rstr_p_mult_full, &rstr_p_mult_fine));
1161*397d7ab3SJeremy L Thompson     }
116232db0c4dSJeremy L Thompson     // Clean-up
11632b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&mult_e_vec));
116432db0c4dSJeremy L Thompson     CeedCall(CeedVectorDestroy(&mult_l_vec));
116532db0c4dSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_full));
116685bb9dcfSJeremy L Thompson   }
1167eaf62fffSJeremy L Thompson 
1168addd79feSZach Atkins   // Clone name
1169addd79feSZach Atkins   bool   has_name = op_fine->name;
1170addd79feSZach Atkins   size_t name_len = op_fine->name ? strlen(op_fine->name) : 0;
1171addd79feSZach Atkins   CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name));
1172addd79feSZach Atkins 
11737758292fSSebastian Grimberg   // Check that coarse to fine basis is provided if prolong/restrict operators are requested
11747758292fSSebastian Grimberg   CeedCheck(basis_c_to_f || (!op_restrict && !op_prolong), ceed, CEED_ERROR_INCOMPATIBLE,
11756574a04fSJeremy L Thompson             "Prolongation or restriction operator creation requires coarse-to-fine basis");
117683d6adf3SZach Atkins 
117785bb9dcfSJeremy L Thompson   // Restriction/Prolongation Operators
11782b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp));
1179addd79feSZach Atkins 
1180addd79feSZach Atkins   // Restriction
11817758292fSSebastian Grimberg   if (op_restrict) {
1182eaf62fffSJeremy L Thompson     CeedInt             *num_comp_r_data;
118385bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_r;
11847758292fSSebastian Grimberg     CeedQFunction        qf_restrict;
118585bb9dcfSJeremy L Thompson 
1186*397d7ab3SJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, use_scalar_mult ? "Scale (scalar)" : "Scale", &qf_restrict));
11872b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_r_data));
1188eaf62fffSJeremy L Thompson     num_comp_r_data[0] = num_comp;
11892b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r));
11902b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data));
11917758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r));
11922b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_r));
11937758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE));
1194*397d7ab3SJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", use_scalar_mult ? 1 : num_comp, CEED_EVAL_NONE));
11957758292fSSebastian Grimberg     CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP));
11967758292fSSebastian Grimberg     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp));
1197eaf62fffSJeremy L Thompson 
11987758292fSSebastian Grimberg     CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict));
11997758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
12007758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
12017758292fSSebastian Grimberg     CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
1202eaf62fffSJeremy L Thompson 
1203addd79feSZach Atkins     // Set name
1204addd79feSZach Atkins     char *restriction_name;
12051c66c397SJeremy L Thompson 
1206addd79feSZach Atkins     CeedCall(CeedCalloc(17 + name_len, &restriction_name));
1207addd79feSZach Atkins     sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
12087758292fSSebastian Grimberg     CeedCall(CeedOperatorSetName(*op_restrict, restriction_name));
1209addd79feSZach Atkins     CeedCall(CeedFree(&restriction_name));
1210addd79feSZach Atkins 
1211addd79feSZach Atkins     // Check
12127758292fSSebastian Grimberg     CeedCall(CeedOperatorCheckReady(*op_restrict));
1213addd79feSZach Atkins 
1214addd79feSZach Atkins     // Cleanup
12157758292fSSebastian Grimberg     CeedCall(CeedQFunctionDestroy(&qf_restrict));
1216addd79feSZach Atkins   }
1217addd79feSZach Atkins 
1218eaf62fffSJeremy L Thompson   // Prolongation
1219addd79feSZach Atkins   if (op_prolong) {
1220eaf62fffSJeremy L Thompson     CeedInt             *num_comp_p_data;
122185bb9dcfSJeremy L Thompson     CeedQFunctionContext ctx_p;
12221c66c397SJeremy L Thompson     CeedQFunction        qf_prolong;
122385bb9dcfSJeremy L Thompson 
1224*397d7ab3SJeremy L Thompson     CeedCall(CeedQFunctionCreateInteriorByName(ceed, use_scalar_mult ? "Scale (scalar)" : "Scale", &qf_prolong));
12252b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_p_data));
1226eaf62fffSJeremy L Thompson     num_comp_p_data[0] = num_comp;
12272b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p));
12282b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data));
12292b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p));
12302b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx_p));
12312b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP));
1232*397d7ab3SJeremy L Thompson     CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", use_scalar_mult ? 1 : num_comp, CEED_EVAL_NONE));
12332b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE));
12342b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp));
1235eaf62fffSJeremy L Thompson 
12362b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong));
12372b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE));
1238356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec));
1239356036faSJeremy L Thompson     CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
1240eaf62fffSJeremy L Thompson 
1241addd79feSZach Atkins     // Set name
1242ea6b5821SJeremy L Thompson     char *prolongation_name;
12431c66c397SJeremy L Thompson 
12442b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(18 + name_len, &prolongation_name));
12452b730f8bSJeremy L Thompson     sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : "");
12462b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name));
12472b730f8bSJeremy L Thompson     CeedCall(CeedFree(&prolongation_name));
1248addd79feSZach Atkins 
1249addd79feSZach Atkins     // Check
1250addd79feSZach Atkins     CeedCall(CeedOperatorCheckReady(*op_prolong));
1251addd79feSZach Atkins 
1252addd79feSZach Atkins     // Cleanup
1253addd79feSZach Atkins     CeedCall(CeedQFunctionDestroy(&qf_prolong));
1254ea6b5821SJeremy L Thompson   }
1255ea6b5821SJeremy L Thompson 
125658e4b056SJeremy L Thompson   // Check
125758e4b056SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(*op_coarse));
125858e4b056SJeremy L Thompson 
1259eaf62fffSJeremy L Thompson   // Cleanup
12609bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
12612b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&mult_vec));
1262681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_fine));
1263c17ec2beSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine));
12642b730f8bSJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis_c_to_f));
1265eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1266eaf62fffSJeremy L Thompson }
1267eaf62fffSJeremy L Thompson 
1268eaf62fffSJeremy L Thompson /**
1269eaf62fffSJeremy L Thompson   @brief Build 1D mass matrix and Laplacian with perturbation
1270eaf62fffSJeremy L Thompson 
1271eaf62fffSJeremy L Thompson   @param[in]  interp_1d   Interpolation matrix in one dimension
1272eaf62fffSJeremy L Thompson   @param[in]  grad_1d     Gradient matrix in one dimension
1273eaf62fffSJeremy L Thompson   @param[in]  q_weight_1d Quadrature weights in one dimension
1274eaf62fffSJeremy L Thompson   @param[in]  P_1d        Number of basis nodes in one dimension
1275eaf62fffSJeremy L Thompson   @param[in]  Q_1d        Number of quadrature points in one dimension
1276eaf62fffSJeremy L Thompson   @param[in]  dim         Dimension of basis
1277eaf62fffSJeremy L Thompson   @param[out] mass        Assembled mass matrix in one dimension
1278eaf62fffSJeremy L Thompson   @param[out] laplace     Assembled perturbed Laplacian in one dimension
1279eaf62fffSJeremy L Thompson 
1280eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1281eaf62fffSJeremy L Thompson 
1282eaf62fffSJeremy L Thompson   @ref Developer
1283eaf62fffSJeremy L Thompson **/
12842c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff
CeedBuildMassLaplace(const CeedScalar * interp_1d,const CeedScalar * grad_1d,const CeedScalar * q_weight_1d,CeedInt P_1d,CeedInt Q_1d,CeedInt dim,CeedScalar * mass,CeedScalar * laplace)12852c2ea1dbSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d, CeedInt P_1d, CeedInt Q_1d,
12862c2ea1dbSJeremy L Thompson                                 CeedInt dim, CeedScalar *mass, CeedScalar *laplace) {
12872b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
1288eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
1289eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
12902b730f8bSJeremy 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];
1291eaf62fffSJeremy L Thompson       mass[i + j * P_1d] = sum;
1292eaf62fffSJeremy L Thompson     }
12932b730f8bSJeremy L Thompson   }
1294eaf62fffSJeremy L Thompson   // -- Laplacian
12952b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
1296eaf62fffSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) {
1297eaf62fffSJeremy L Thompson       CeedScalar sum = 0.0;
12981c66c397SJeremy L Thompson 
12992b730f8bSJeremy 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];
1300eaf62fffSJeremy L Thompson       laplace[i + j * P_1d] = sum;
1301eaf62fffSJeremy L Thompson     }
13022b730f8bSJeremy L Thompson   }
1303eaf62fffSJeremy L Thompson   CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4;
13042b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation;
1305eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1306eaf62fffSJeremy L Thompson }
13072c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn
1308eaf62fffSJeremy L Thompson 
1309eaf62fffSJeremy L Thompson /// @}
1310eaf62fffSJeremy L Thompson 
1311eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
1312480fae85SJeremy L Thompson /// CeedOperator Backend API
1313480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
1314480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend
1315480fae85SJeremy L Thompson /// @{
1316480fae85SJeremy L Thompson 
1317480fae85SJeremy L Thompson /**
1318004e4986SSebastian Grimberg   @brief Select correct basis matrix pointer based on @ref CeedEvalMode
1319004e4986SSebastian Grimberg 
1320004e4986SSebastian Grimberg   @param[in]  basis     `CeedBasis` from which to get the basis matrix
1321004e4986SSebastian Grimberg   @param[in]  eval_mode Current basis evaluation mode
1322004e4986SSebastian Grimberg   @param[in]  identity  Pointer to identity matrix
1323004e4986SSebastian Grimberg   @param[out] basis_ptr `CeedBasis` pointer to set
1324004e4986SSebastian Grimberg 
1325004e4986SSebastian Grimberg   @ref Backend
1326004e4986SSebastian Grimberg **/
CeedOperatorGetBasisPointer(CeedBasis basis,CeedEvalMode eval_mode,const CeedScalar * identity,const CeedScalar ** basis_ptr)1327004e4986SSebastian Grimberg int CeedOperatorGetBasisPointer(CeedBasis basis, CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar **basis_ptr) {
1328004e4986SSebastian Grimberg   switch (eval_mode) {
1329004e4986SSebastian Grimberg     case CEED_EVAL_NONE:
1330004e4986SSebastian Grimberg       *basis_ptr = identity;
1331004e4986SSebastian Grimberg       break;
1332004e4986SSebastian Grimberg     case CEED_EVAL_INTERP:
1333004e4986SSebastian Grimberg       CeedCall(CeedBasisGetInterp(basis, basis_ptr));
1334004e4986SSebastian Grimberg       break;
1335004e4986SSebastian Grimberg     case CEED_EVAL_GRAD:
1336004e4986SSebastian Grimberg       CeedCall(CeedBasisGetGrad(basis, basis_ptr));
1337004e4986SSebastian Grimberg       break;
1338004e4986SSebastian Grimberg     case CEED_EVAL_DIV:
1339004e4986SSebastian Grimberg       CeedCall(CeedBasisGetDiv(basis, basis_ptr));
1340004e4986SSebastian Grimberg       break;
1341004e4986SSebastian Grimberg     case CEED_EVAL_CURL:
1342004e4986SSebastian Grimberg       CeedCall(CeedBasisGetCurl(basis, basis_ptr));
1343004e4986SSebastian Grimberg       break;
1344004e4986SSebastian Grimberg     case CEED_EVAL_WEIGHT:
1345004e4986SSebastian Grimberg       break;  // Caught by QF Assembly
1346004e4986SSebastian Grimberg   }
1347004e4986SSebastian Grimberg   assert(*basis_ptr != NULL);
1348004e4986SSebastian Grimberg   return CEED_ERROR_SUCCESS;
1349004e4986SSebastian Grimberg }
1350004e4986SSebastian Grimberg 
1351004e4986SSebastian Grimberg /**
1352ca94c3ddSJeremy L Thompson   @brief Create point block restriction for active `CeedOperatorField`
1353506b1a0cSSebastian Grimberg 
1354ca94c3ddSJeremy L Thompson   @param[in]  rstr             Original `CeedElemRestriction` for active field
1355ca94c3ddSJeremy L Thompson   @param[out] point_block_rstr Address of the variable where the newly created `CeedElemRestriction` will be stored
1356506b1a0cSSebastian Grimberg 
1357506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
1358506b1a0cSSebastian Grimberg 
1359506b1a0cSSebastian Grimberg   @ref Backend
1360506b1a0cSSebastian Grimberg **/
CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr,CeedElemRestriction * point_block_rstr)1361506b1a0cSSebastian Grimberg int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *point_block_rstr) {
1362506b1a0cSSebastian Grimberg   Ceed           ceed;
1363506b1a0cSSebastian Grimberg   CeedInt        num_elem, num_comp, shift, elem_size, comp_stride, *point_block_offsets;
1364506b1a0cSSebastian Grimberg   CeedSize       l_size;
1365506b1a0cSSebastian Grimberg   const CeedInt *offsets;
1366506b1a0cSSebastian Grimberg 
1367506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
1368506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
1369506b1a0cSSebastian Grimberg 
1370506b1a0cSSebastian Grimberg   // Expand offsets
1371506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
1372506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1373506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
1374506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
1375506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
1376506b1a0cSSebastian Grimberg   shift = num_comp;
1377506b1a0cSSebastian Grimberg   if (comp_stride != 1) shift *= num_comp;
1378506b1a0cSSebastian Grimberg   CeedCall(CeedCalloc(num_elem * elem_size, &point_block_offsets));
1379506b1a0cSSebastian Grimberg   for (CeedInt i = 0; i < num_elem * elem_size; i++) {
1380506b1a0cSSebastian Grimberg     point_block_offsets[i] = offsets[i] * shift;
1381506b1a0cSSebastian Grimberg   }
1382506b1a0cSSebastian Grimberg 
1383506b1a0cSSebastian Grimberg   // Create new restriction
1384506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
1385506b1a0cSSebastian Grimberg                                      point_block_offsets, point_block_rstr));
1386506b1a0cSSebastian Grimberg 
1387506b1a0cSSebastian Grimberg   // Cleanup
1388506b1a0cSSebastian Grimberg   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
13899bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
1390506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1391506b1a0cSSebastian Grimberg }
1392506b1a0cSSebastian Grimberg 
1393506b1a0cSSebastian Grimberg /**
13947d5185d7SSebastian Grimberg   @brief Get `CeedQFunctionAssemblyData`
13957d5185d7SSebastian Grimberg 
13967d5185d7SSebastian Grimberg   @param[in]  op   `CeedOperator` to assemble
13977d5185d7SSebastian Grimberg   @param[out] data `CeedQFunctionAssemblyData`
13987d5185d7SSebastian Grimberg 
13997d5185d7SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
14007d5185d7SSebastian Grimberg 
14017d5185d7SSebastian Grimberg   @ref Backend
14027d5185d7SSebastian Grimberg **/
CeedOperatorGetQFunctionAssemblyData(CeedOperator op,CeedQFunctionAssemblyData * data)14037d5185d7SSebastian Grimberg int CeedOperatorGetQFunctionAssemblyData(CeedOperator op, CeedQFunctionAssemblyData *data) {
14047d5185d7SSebastian Grimberg   if (!op->qf_assembled) {
14057d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
14067d5185d7SSebastian Grimberg 
1407b0f67a9cSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataCreate(CeedOperatorReturnCeed(op), &data));
14087d5185d7SSebastian Grimberg     op->qf_assembled = data;
14097d5185d7SSebastian Grimberg   }
14107d5185d7SSebastian Grimberg   *data = op->qf_assembled;
14117d5185d7SSebastian Grimberg   return CEED_ERROR_SUCCESS;
14127d5185d7SSebastian Grimberg }
14137d5185d7SSebastian Grimberg 
14147d5185d7SSebastian Grimberg /**
1415ca94c3ddSJeremy L Thompson   @brief Create object holding `CeedQFunction` assembly data for `CeedOperator`
1416480fae85SJeremy L Thompson 
1417ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedQFunctionAssemblyData`
1418ca94c3ddSJeremy L Thompson   @param[out] data Address of the variable where the newly created `CeedQFunctionAssemblyData` will be stored
1419480fae85SJeremy L Thompson 
1420480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1421480fae85SJeremy L Thompson 
1422480fae85SJeremy L Thompson   @ref Backend
1423480fae85SJeremy L Thompson **/
CeedQFunctionAssemblyDataCreate(Ceed ceed,CeedQFunctionAssemblyData * data)1424ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) {
14252b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1426480fae85SJeremy L Thompson   (*data)->ref_count = 1;
1427b0f67a9cSJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*data)->ceed));
1428480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1429480fae85SJeremy L Thompson }
1430480fae85SJeremy L Thompson 
1431480fae85SJeremy L Thompson /**
1432ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedQFunctionAssemblyData`
1433480fae85SJeremy L Thompson 
1434ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to increment the reference counter
1435480fae85SJeremy L Thompson 
1436480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1437480fae85SJeremy L Thompson 
1438480fae85SJeremy L Thompson   @ref Backend
1439480fae85SJeremy L Thompson **/
CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data)1440480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) {
1441480fae85SJeremy L Thompson   data->ref_count++;
1442480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1443480fae85SJeremy L Thompson }
1444480fae85SJeremy L Thompson 
1445480fae85SJeremy L Thompson /**
1446ca94c3ddSJeremy L Thompson   @brief Set re-use of `CeedQFunctionAssemblyData`
14478b919e6bSJeremy L Thompson 
1448ca94c3ddSJeremy L Thompson   @param[in,out] data       `CeedQFunctionAssemblyData` to mark for reuse
1449ea61e9acSJeremy L Thompson   @param[in]     reuse_data Boolean flag indicating data re-use
14508b919e6bSJeremy L Thompson 
14518b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14528b919e6bSJeremy L Thompson 
14538b919e6bSJeremy L Thompson   @ref Backend
14548b919e6bSJeremy L Thompson **/
CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data,bool reuse_data)14552b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) {
1456beecbf24SJeremy L Thompson   data->reuse_data        = reuse_data;
1457beecbf24SJeremy L Thompson   data->needs_data_update = true;
1458beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1459beecbf24SJeremy L Thompson }
1460beecbf24SJeremy L Thompson 
1461beecbf24SJeremy L Thompson /**
1462ca94c3ddSJeremy L Thompson   @brief Mark `CeedQFunctionAssemblyData` as stale
1463beecbf24SJeremy L Thompson 
1464ca94c3ddSJeremy L Thompson   @param[in,out] data              `CeedQFunctionAssemblyData` to mark as stale
1465ea61e9acSJeremy L Thompson   @param[in]     needs_data_update Boolean flag indicating if update is needed or completed
1466beecbf24SJeremy L Thompson 
1467beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1468beecbf24SJeremy L Thompson 
1469beecbf24SJeremy L Thompson   @ref Backend
1470beecbf24SJeremy L Thompson **/
CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data,bool needs_data_update)14712b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) {
1472beecbf24SJeremy L Thompson   data->needs_data_update = needs_data_update;
14738b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
14748b919e6bSJeremy L Thompson }
14758b919e6bSJeremy L Thompson 
14768b919e6bSJeremy L Thompson /**
1477ca94c3ddSJeremy L Thompson   @brief Determine if `CeedQFunctionAssemblyData` needs update
14788b919e6bSJeremy L Thompson 
1479ca94c3ddSJeremy L Thompson   @param[in]  data             `CeedQFunctionAssemblyData` to mark as stale
14808b919e6bSJeremy L Thompson   @param[out] is_update_needed Boolean flag indicating if re-assembly is required
14818b919e6bSJeremy L Thompson 
14828b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14838b919e6bSJeremy L Thompson 
14848b919e6bSJeremy L Thompson   @ref Backend
14858b919e6bSJeremy L Thompson **/
CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data,bool * is_update_needed)14862b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) {
1487beecbf24SJeremy L Thompson   *is_update_needed = !data->reuse_data || data->needs_data_update;
14888b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
14898b919e6bSJeremy L Thompson }
14908b919e6bSJeremy L Thompson 
14918b919e6bSJeremy L Thompson /**
1492ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedQFunctionAssemblyData`.
14934385fb7fSSebastian Grimberg 
1494ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedQFunctionAssemblyDataDestroy().
1495512bb800SJeremy L Thompson 
1496ca94c3ddSJeremy 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`.
1497ca94c3ddSJeremy L Thompson         This `CeedQFunctionAssemblyData` will be destroyed if ` *data_copy` is the only reference to this `CeedQFunctionAssemblyData`.
1498480fae85SJeremy L Thompson 
1499ca94c3ddSJeremy L Thompson   @param[in]     data      `CeedQFunctionAssemblyData` to copy reference to
1500ea61e9acSJeremy L Thompson   @param[in,out] data_copy Variable to store copied reference
1501480fae85SJeremy L Thompson 
1502480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1503480fae85SJeremy L Thompson 
1504480fae85SJeremy L Thompson   @ref Backend
1505480fae85SJeremy L Thompson **/
CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data,CeedQFunctionAssemblyData * data_copy)15062b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) {
15072b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataReference(data));
15082b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy));
1509480fae85SJeremy L Thompson   *data_copy = data;
1510480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1511480fae85SJeremy L Thompson }
1512480fae85SJeremy L Thompson 
1513480fae85SJeremy L Thompson /**
1514ca94c3ddSJeremy L Thompson   @brief Get setup status for internal objects for `CeedQFunctionAssemblyData`
1515480fae85SJeremy L Thompson 
1516ca94c3ddSJeremy L Thompson   @param[in]  data     `CeedQFunctionAssemblyData` to retrieve status
1517480fae85SJeremy L Thompson   @param[out] is_setup Boolean flag for setup status
1518480fae85SJeremy L Thompson 
1519480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1520480fae85SJeremy L Thompson 
1521480fae85SJeremy L Thompson   @ref Backend
1522480fae85SJeremy L Thompson **/
CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data,bool * is_setup)15232b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) {
1524480fae85SJeremy L Thompson   *is_setup = data->is_setup;
1525480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1526480fae85SJeremy L Thompson }
1527480fae85SJeremy L Thompson 
1528480fae85SJeremy L Thompson /**
1529ca94c3ddSJeremy L Thompson   @brief Set internal objects for `CeedQFunctionAssemblyData`
1530480fae85SJeremy L Thompson 
1531ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to set objects
1532ca94c3ddSJeremy L Thompson   @param[in]     vec  `CeedVector` to store assembled `CeedQFunction` at quadrature points
1533ca94c3ddSJeremy L Thompson   @param[in]     rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
1534480fae85SJeremy L Thompson 
1535480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1536480fae85SJeremy L Thompson 
1537480fae85SJeremy L Thompson   @ref Backend
1538480fae85SJeremy L Thompson **/
CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data,CeedVector vec,CeedElemRestriction rstr)15392b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) {
15402b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(vec, &data->vec));
15412b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr));
1542480fae85SJeremy L Thompson 
1543480fae85SJeremy L Thompson   data->is_setup = true;
1544480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1545480fae85SJeremy L Thompson }
1546480fae85SJeremy L Thompson 
15474dd1a9d2SSebastian Grimberg /**
1548ca94c3ddSJeremy L Thompson   @brief Get internal objects for `CeedQFunctionAssemblyData`
15494dd1a9d2SSebastian Grimberg 
1550ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedQFunctionAssemblyData` to set objects
1551ca94c3ddSJeremy L Thompson   @param[out]    vec  `CeedVector` to store assembled `CeedQFunction` at quadrature points
1552ca94c3ddSJeremy L Thompson   @param[out]    rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
15534dd1a9d2SSebastian Grimberg 
15544dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
15554dd1a9d2SSebastian Grimberg 
15564dd1a9d2SSebastian Grimberg   @ref Backend
15574dd1a9d2SSebastian Grimberg **/
CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data,CeedVector * vec,CeedElemRestriction * rstr)15582b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) {
15596574a04fSJeremy L Thompson   CeedCheck(data->is_setup, data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first.");
1560480fae85SJeremy L Thompson 
15612b730f8bSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(data->vec, vec));
15622b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr));
1563480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1564480fae85SJeremy L Thompson }
1565480fae85SJeremy L Thompson 
1566480fae85SJeremy L Thompson /**
1567ca94c3ddSJeremy L Thompson   @brief Destroy `CeedQFunctionAssemblyData`
1568480fae85SJeremy L Thompson 
1569ca94c3ddSJeremy L Thompson   @param[in,out] data  `CeedQFunctionAssemblyData` to destroy
1570480fae85SJeremy L Thompson 
1571480fae85SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1572480fae85SJeremy L Thompson 
1573480fae85SJeremy L Thompson   @ref Backend
1574480fae85SJeremy L Thompson **/
CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData * data)1575480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) {
1576ad6481ceSJeremy L Thompson   if (!*data || --(*data)->ref_count > 0) {
1577ad6481ceSJeremy L Thompson     *data = NULL;
1578ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1579ad6481ceSJeremy L Thompson   }
15802b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
15812b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*data)->vec));
15822b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr));
1583480fae85SJeremy L Thompson 
15842b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
1585480fae85SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1586480fae85SJeremy L Thompson }
1587480fae85SJeremy L Thompson 
1588ed9e99e6SJeremy L Thompson /**
1589ca94c3ddSJeremy L Thompson   @brief Get `CeedOperatorAssemblyData`
1590ed9e99e6SJeremy L Thompson 
1591ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator` to assemble
15927d5185d7SSebastian Grimberg   @param[out] data `CeedOperatorAssemblyData`
1593ed9e99e6SJeremy L Thompson 
1594ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1595ed9e99e6SJeremy L Thompson 
1596ed9e99e6SJeremy L Thompson   @ref Backend
1597ed9e99e6SJeremy L Thompson **/
CeedOperatorGetOperatorAssemblyData(CeedOperator op,CeedOperatorAssemblyData * data)15982b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) {
1599ed9e99e6SJeremy L Thompson   if (!op->op_assembled) {
1600ed9e99e6SJeremy L Thompson     CeedOperatorAssemblyData data;
1601ed9e99e6SJeremy L Thompson 
1602b0f67a9cSJeremy L Thompson     CeedCall(CeedOperatorAssemblyDataCreate(CeedOperatorReturnCeed(op), op, &data));
1603ed9e99e6SJeremy L Thompson     op->op_assembled = data;
1604ed9e99e6SJeremy L Thompson   }
1605ed9e99e6SJeremy L Thompson   *data = op->op_assembled;
1606ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1607ed9e99e6SJeremy L Thompson }
1608ed9e99e6SJeremy L Thompson 
1609ed9e99e6SJeremy L Thompson /**
1610ca94c3ddSJeremy L Thompson   @brief Create object holding `CeedOperator` assembly data.
1611ba746a46SJeremy L Thompson 
1612ca94c3ddSJeremy L Thompson   The `CeedOperatorAssemblyData` holds an array with references to every active `CeedBasis` used in the `CeedOperator`.
1613ca94c3ddSJeremy L Thompson   An array with references to the corresponding active `CeedElemRestriction` is also stored.
1614ca94c3ddSJeremy L Thompson   For each active `CeedBasis, the `CeedOperatorAssemblyData` holds an array of all input and output @ref CeedEvalMode for this `CeedBasis`.
1615ca94c3ddSJeremy L Thompson   The `CeedOperatorAssemblyData` holds an array of offsets for indexing into the assembled `CeedQFunction` arrays to the row representing each @ref CeedEvalMode.
1616ca94c3ddSJeremy L Thompson   The number of input columns across all active bases for the assembled `CeedQFunction` is also stored.
1617ca94c3ddSJeremy L Thompson   Lastly, the `CeedOperatorAssembly` data holds assembled matrices representing the full action of the `CeedBasis` for all @ref CeedEvalMode.
1618ed9e99e6SJeremy L Thompson 
1619ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperatorAssemblyData`
1620ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator` to be assembled
1621ca94c3ddSJeremy L Thompson   @param[out] data Address of the variable where the newly created `CeedOperatorAssemblyData` will be stored
1622ed9e99e6SJeremy L Thompson 
1623ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1624ed9e99e6SJeremy L Thompson 
1625ed9e99e6SJeremy L Thompson   @ref Backend
1626ed9e99e6SJeremy L Thompson **/
CeedOperatorAssemblyDataCreate(Ceed ceed,CeedOperator op,CeedOperatorAssemblyData * data)16272b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) {
1628506b1a0cSSebastian Grimberg   CeedInt             num_active_bases_in = 0, num_active_bases_out = 0, offset = 0;
1629506b1a0cSSebastian Grimberg   CeedInt             num_input_fields, *num_eval_modes_in = NULL, num_output_fields, *num_eval_modes_out = NULL;
16301c66c397SJeremy L Thompson   CeedSize          **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL;
16311c66c397SJeremy L Thompson   CeedEvalMode      **eval_modes_in = NULL, **eval_modes_out = NULL;
16321c66c397SJeremy L Thompson   CeedQFunctionField *qf_fields;
16331c66c397SJeremy L Thompson   CeedQFunction       qf;
16341c66c397SJeremy L Thompson   CeedOperatorField  *op_fields;
163501f0e615SJames Wright   bool                is_composite;
163601f0e615SJames Wright 
163701f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
163801f0e615SJames Wright   CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Can only create CeedOperator assembly data for non-composite operators.");
1639437c7c90SJeremy L Thompson 
1640437c7c90SJeremy L Thompson   // Allocate
16412b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, data));
1642b0f67a9cSJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*data)->ceed));
1643ed9e99e6SJeremy L Thompson 
1644ed9e99e6SJeremy L Thompson   // Build OperatorAssembly data
16452b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
1646ed9e99e6SJeremy L Thompson 
1647ed9e99e6SJeremy L Thompson   // Determine active input basis
1648004e4986SSebastian Grimberg   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL));
1649004e4986SSebastian Grimberg   CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1650ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1651ed9e99e6SJeremy L Thompson     CeedVector vec;
16521c66c397SJeremy L Thompson 
16532b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1654ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
16557c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
16561c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
16571c66c397SJeremy L Thompson       CeedBasis    basis_in = NULL;
16581c66c397SJeremy L Thompson 
16592b730f8bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
16602b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1661352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp));
1662352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1663506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_active_bases_in; i++) {
1664506b1a0cSSebastian Grimberg         if ((*data)->active_bases_in[i] == basis_in) index = i;
1665437c7c90SJeremy L Thompson       }
1666437c7c90SJeremy L Thompson       if (index == -1) {
1667437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_in;
16681c66c397SJeremy L Thompson 
1669506b1a0cSSebastian Grimberg         index = num_active_bases_in;
1670506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_bases_in));
1671506b1a0cSSebastian Grimberg         (*data)->active_bases_in[num_active_bases_in] = NULL;
1672506b1a0cSSebastian Grimberg         CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases_in[num_active_bases_in]));
1673506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_elem_rstrs_in));
1674506b1a0cSSebastian Grimberg         (*data)->active_elem_rstrs_in[num_active_bases_in] = NULL;
1675437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in));
1676506b1a0cSSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs_in[num_active_bases_in]));
1677681d0ea7SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&elem_rstr_in));
1678506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &num_eval_modes_in));
1679437c7c90SJeremy L Thompson         num_eval_modes_in[index] = 0;
1680506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_modes_in));
1681437c7c90SJeremy L Thompson         eval_modes_in[index] = NULL;
1682506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_mode_offsets_in));
1683437c7c90SJeremy L Thompson         eval_mode_offsets_in[index] = NULL;
1684506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->assembled_bases_in));
1685437c7c90SJeremy L Thompson         (*data)->assembled_bases_in[index] = NULL;
1686506b1a0cSSebastian Grimberg         num_active_bases_in++;
1687437c7c90SJeremy L Thompson       }
1688352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1689352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1690352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_modes_in[index]));
1691352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_mode_offsets_in[index]));
1692352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1693437c7c90SJeremy L Thompson           eval_modes_in[index][num_eval_modes_in[index] + d]        = eval_mode;
1694437c7c90SJeremy L Thompson           eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset;
1695352a5e7cSSebastian Grimberg           offset += num_comp;
1696ed9e99e6SJeremy L Thompson         }
1697352a5e7cSSebastian Grimberg         num_eval_modes_in[index] += q_comp;
1698ed9e99e6SJeremy L Thompson       }
1699681d0ea7SJeremy L Thompson       CeedCall(CeedBasisDestroy(&basis_in));
1700ed9e99e6SJeremy L Thompson     }
1701681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
1702ed9e99e6SJeremy L Thompson   }
1703ed9e99e6SJeremy L Thompson 
1704ed9e99e6SJeremy L Thompson   // Determine active output basis
17052b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields));
17062b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1707437c7c90SJeremy L Thompson   offset = 0;
1708ed9e99e6SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1709ed9e99e6SJeremy L Thompson     CeedVector vec;
17101c66c397SJeremy L Thompson 
17112b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
1712ed9e99e6SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
17137c1dbaffSSebastian Grimberg       CeedInt      index = -1, num_comp, q_comp;
17141c66c397SJeremy L Thompson       CeedEvalMode eval_mode;
17151c66c397SJeremy L Thompson       CeedBasis    basis_out = NULL;
17161c66c397SJeremy L Thompson 
1717437c7c90SJeremy L Thompson       CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
17182b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1719352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumComponents(basis_out, &num_comp));
1720352a5e7cSSebastian Grimberg       CeedCall(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1721506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_active_bases_out; i++) {
1722506b1a0cSSebastian Grimberg         if ((*data)->active_bases_out[i] == basis_out) index = i;
1723437c7c90SJeremy L Thompson       }
1724437c7c90SJeremy L Thompson       if (index == -1) {
1725437c7c90SJeremy L Thompson         CeedElemRestriction elem_rstr_out;
17261c66c397SJeremy L Thompson 
1727506b1a0cSSebastian Grimberg         index = num_active_bases_out;
1728506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_bases_out));
1729506b1a0cSSebastian Grimberg         (*data)->active_bases_out[num_active_bases_out] = NULL;
1730506b1a0cSSebastian Grimberg         CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases_out[num_active_bases_out]));
1731506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_elem_rstrs_out));
1732506b1a0cSSebastian Grimberg         (*data)->active_elem_rstrs_out[num_active_bases_out] = NULL;
1733437c7c90SJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out));
1734506b1a0cSSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs_out[num_active_bases_out]));
1735681d0ea7SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&elem_rstr_out));
1736506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &num_eval_modes_out));
1737437c7c90SJeremy L Thompson         num_eval_modes_out[index] = 0;
1738506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_modes_out));
1739437c7c90SJeremy L Thompson         eval_modes_out[index] = NULL;
1740506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_mode_offsets_out));
1741437c7c90SJeremy L Thompson         eval_mode_offsets_out[index] = NULL;
1742506b1a0cSSebastian Grimberg         CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->assembled_bases_out));
1743437c7c90SJeremy L Thompson         (*data)->assembled_bases_out[index] = NULL;
1744506b1a0cSSebastian Grimberg         num_active_bases_out++;
1745437c7c90SJeremy L Thompson       }
1746352a5e7cSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1747352a5e7cSSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1748352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_modes_out[index]));
1749352a5e7cSSebastian Grimberg         CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_mode_offsets_out[index]));
1750352a5e7cSSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1751437c7c90SJeremy L Thompson           eval_modes_out[index][num_eval_modes_out[index] + d]        = eval_mode;
1752437c7c90SJeremy L Thompson           eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset;
1753352a5e7cSSebastian Grimberg           offset += num_comp;
1754ed9e99e6SJeremy L Thompson         }
1755352a5e7cSSebastian Grimberg         num_eval_modes_out[index] += q_comp;
1756ed9e99e6SJeremy L Thompson       }
1757681d0ea7SJeremy L Thompson       CeedCall(CeedBasisDestroy(&basis_out));
1758ed9e99e6SJeremy L Thompson     }
1759681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
1760ed9e99e6SJeremy L Thompson   }
1761c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
1762506b1a0cSSebastian Grimberg   (*data)->num_active_bases_in   = num_active_bases_in;
176327789c4aSJed Brown   (*data)->num_eval_modes_in     = num_eval_modes_in;
176427789c4aSJed Brown   (*data)->eval_modes_in         = eval_modes_in;
176527789c4aSJed Brown   (*data)->eval_mode_offsets_in  = eval_mode_offsets_in;
1766506b1a0cSSebastian Grimberg   (*data)->num_active_bases_out  = num_active_bases_out;
1767437c7c90SJeremy L Thompson   (*data)->num_eval_modes_out    = num_eval_modes_out;
1768437c7c90SJeremy L Thompson   (*data)->eval_modes_out        = eval_modes_out;
1769437c7c90SJeremy L Thompson   (*data)->eval_mode_offsets_out = eval_mode_offsets_out;
1770506b1a0cSSebastian Grimberg   (*data)->num_output_components = offset;
1771ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1772ed9e99e6SJeremy L Thompson }
1773ed9e99e6SJeremy L Thompson 
1774ed9e99e6SJeremy L Thompson /**
1775ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` @ref CeedEvalMode for assembly.
1776ba746a46SJeremy L Thompson 
1777ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1778ed9e99e6SJeremy L Thompson 
1779ca94c3ddSJeremy L Thompson   @param[in]  data                  `CeedOperatorAssemblyData`
1780506b1a0cSSebastian Grimberg   @param[out] num_active_bases_in   Total number of active bases for input
1781ca94c3ddSJeremy L Thompson   @param[out] num_eval_modes_in     Pointer to hold array of numbers of input @ref CeedEvalMode, or `NULL`.
1782ca94c3ddSJeremy L Thompson                                       `eval_modes_in[0]` holds an array of eval modes for the first active `CeedBasis`.
1783ca94c3ddSJeremy L Thompson   @param[out] eval_modes_in         Pointer to hold arrays of input @ref CeedEvalMode, or `NULL`
1784ca94c3ddSJeremy L Thompson   @param[out] eval_mode_offsets_in  Pointer to hold arrays of input offsets at each quadrature point
1785506b1a0cSSebastian Grimberg   @param[out] num_active_bases_out  Total number of active bases for output
1786ca94c3ddSJeremy L Thompson   @param[out] num_eval_modes_out    Pointer to hold array of numbers of output @ref CeedEvalMode, or `NULL`
1787ca94c3ddSJeremy L Thompson   @param[out] eval_modes_out        Pointer to hold arrays of output @ref CeedEvalMode, or `NULL`
1788437c7c90SJeremy L Thompson   @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point
1789ca94c3ddSJeremy 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
1790ed9e99e6SJeremy L Thompson 
1791ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1792ed9e99e6SJeremy L Thompson 
1793ed9e99e6SJeremy L Thompson   @ref Backend
1794ed9e99e6SJeremy L Thompson **/
CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data,CeedInt * num_active_bases_in,CeedInt ** num_eval_modes_in,const CeedEvalMode *** eval_modes_in,CeedSize *** eval_mode_offsets_in,CeedInt * num_active_bases_out,CeedInt ** num_eval_modes_out,const CeedEvalMode *** eval_modes_out,CeedSize *** eval_mode_offsets_out,CeedSize * num_output_components)1795506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedInt **num_eval_modes_in,
1796506b1a0cSSebastian Grimberg                                          const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt *num_active_bases_out,
1797506b1a0cSSebastian Grimberg                                          CeedInt **num_eval_modes_out, const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out,
1798506b1a0cSSebastian Grimberg                                          CeedSize *num_output_components) {
1799506b1a0cSSebastian Grimberg   if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in;
1800437c7c90SJeremy L Thompson   if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in;
1801437c7c90SJeremy L Thompson   if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in;
1802437c7c90SJeremy L Thompson   if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in;
1803506b1a0cSSebastian Grimberg   if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out;
1804437c7c90SJeremy L Thompson   if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out;
1805437c7c90SJeremy L Thompson   if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out;
1806437c7c90SJeremy L Thompson   if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out;
1807437c7c90SJeremy L Thompson   if (num_output_components) *num_output_components = data->num_output_components;
1808ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1809ed9e99e6SJeremy L Thompson }
1810ed9e99e6SJeremy L Thompson 
1811ed9e99e6SJeremy L Thompson /**
1812ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` `CeedBasis` data for assembly.
1813ba746a46SJeremy L Thompson 
1814ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1815ed9e99e6SJeremy L Thompson 
1816ca94c3ddSJeremy L Thompson   @param[in]  data                 `CeedOperatorAssemblyData`
1817ca94c3ddSJeremy L Thompson   @param[out] num_active_bases_in  Number of active input bases, or `NULL`
1818ca94c3ddSJeremy L Thompson   @param[out] active_bases_in      Pointer to hold active input `CeedBasis`, or `NULL`
1819ca94c3ddSJeremy L Thompson   @param[out] assembled_bases_in   Pointer to hold assembled active input `B` , or `NULL`
1820ca94c3ddSJeremy L Thompson   @param[out] num_active_bases_out Number of active output bases, or `NULL`
1821ca94c3ddSJeremy L Thompson   @param[out] active_bases_out     Pointer to hold active output `CeedBasis`, or `NULL`
1822ca94c3ddSJeremy L Thompson   @param[out] assembled_bases_out  Pointer to hold assembled active output `B` , or `NULL`
1823ed9e99e6SJeremy L Thompson 
1824ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1825ed9e99e6SJeremy L Thompson 
1826ed9e99e6SJeremy L Thompson   @ref Backend
1827ed9e99e6SJeremy L Thompson **/
CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data,CeedInt * num_active_bases_in,CeedBasis ** active_bases_in,const CeedScalar *** assembled_bases_in,CeedInt * num_active_bases_out,CeedBasis ** active_bases_out,const CeedScalar *** assembled_bases_out)1828506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedBasis **active_bases_in,
1829506b1a0cSSebastian Grimberg                                      const CeedScalar ***assembled_bases_in, CeedInt *num_active_bases_out, CeedBasis **active_bases_out,
1830506b1a0cSSebastian Grimberg                                      const CeedScalar ***assembled_bases_out) {
1831ed9e99e6SJeremy L Thompson   // Assemble B_in, B_out if needed
1832437c7c90SJeremy L Thompson   if (assembled_bases_in && !data->assembled_bases_in[0]) {
1833437c7c90SJeremy L Thompson     CeedInt num_qpts;
1834437c7c90SJeremy L Thompson 
1835506b1a0cSSebastian Grimberg     if (data->active_bases_in[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[0], &num_qpts));
1836506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_in[0], &num_qpts));
1837506b1a0cSSebastian Grimberg     for (CeedInt b = 0; b < data->num_active_bases_in; b++) {
18381c66c397SJeremy L Thompson       bool        has_eval_none = false;
1839352a5e7cSSebastian Grimberg       CeedInt     num_nodes;
1840437c7c90SJeremy L Thompson       CeedScalar *B_in = NULL, *identity = NULL;
1841ed9e99e6SJeremy L Thompson 
1842506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[b], &num_nodes));
1843352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_in[b], &B_in));
1844ed9e99e6SJeremy L Thompson 
1845437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) {
1846437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE);
1847ed9e99e6SJeremy L Thompson       }
1848ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1849352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1850352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1851352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1852ed9e99e6SJeremy L Thompson         }
1853ed9e99e6SJeremy L Thompson       }
1854ed9e99e6SJeremy L Thompson 
1855ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1856352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1857352a5e7cSSebastian Grimberg           CeedInt      d_in              = 0, q_comp_in;
1858352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE;
18591c66c397SJeremy L Thompson 
1860437c7c90SJeremy L Thompson           for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) {
1861437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_in[b] * q;
1862437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
18631c66c397SJeremy L Thompson 
1864506b1a0cSSebastian Grimberg             CeedCall(CeedOperatorGetBasisPointer(data->active_bases_in[b], data->eval_modes_in[b][e_in], identity, &B));
1865506b1a0cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_in[b], data->eval_modes_in[b][e_in], &q_comp_in));
1866352a5e7cSSebastian Grimberg             if (q_comp_in > 1) {
1867352a5e7cSSebastian Grimberg               if (e_in == 0 || data->eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0;
1868352a5e7cSSebastian Grimberg               else B = &B[(++d_in) * num_qpts * num_nodes];
1869352a5e7cSSebastian Grimberg             }
1870352a5e7cSSebastian Grimberg             eval_mode_in_prev                 = data->eval_modes_in[b][e_in];
1871352a5e7cSSebastian Grimberg             B_in[(qq + e_in) * num_nodes + n] = B[q * num_nodes + n];
1872ed9e99e6SJeremy L Thompson           }
1873ed9e99e6SJeremy L Thompson         }
1874ed9e99e6SJeremy L Thompson       }
18757c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1876437c7c90SJeremy L Thompson       data->assembled_bases_in[b] = B_in;
1877437c7c90SJeremy L Thompson     }
1878ed9e99e6SJeremy L Thompson   }
1879ed9e99e6SJeremy L Thompson 
1880437c7c90SJeremy L Thompson   if (assembled_bases_out && !data->assembled_bases_out[0]) {
1881437c7c90SJeremy L Thompson     CeedInt num_qpts;
1882437c7c90SJeremy L Thompson 
1883506b1a0cSSebastian Grimberg     if (data->active_bases_out[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[0], &num_qpts));
1884506b1a0cSSebastian Grimberg     else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_out[0], &num_qpts));
1885506b1a0cSSebastian Grimberg     for (CeedInt b = 0; b < data->num_active_bases_out; b++) {
1886ed9e99e6SJeremy L Thompson       bool        has_eval_none = false;
18871c66c397SJeremy L Thompson       CeedInt     num_nodes;
1888437c7c90SJeremy L Thompson       CeedScalar *B_out = NULL, *identity = NULL;
1889ed9e99e6SJeremy L Thompson 
1890506b1a0cSSebastian Grimberg       CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[b], &num_nodes));
1891352a5e7cSSebastian Grimberg       CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_out[b], &B_out));
1892ed9e99e6SJeremy L Thompson 
1893437c7c90SJeremy L Thompson       for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) {
1894437c7c90SJeremy L Thompson         has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE);
1895ed9e99e6SJeremy L Thompson       }
1896ed9e99e6SJeremy L Thompson       if (has_eval_none) {
1897352a5e7cSSebastian Grimberg         CeedCall(CeedCalloc(num_qpts * num_nodes, &identity));
1898352a5e7cSSebastian Grimberg         for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) {
1899352a5e7cSSebastian Grimberg           identity[i * num_nodes + i] = 1.0;
1900ed9e99e6SJeremy L Thompson         }
1901ed9e99e6SJeremy L Thompson       }
1902ed9e99e6SJeremy L Thompson 
1903ed9e99e6SJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
1904352a5e7cSSebastian Grimberg         for (CeedInt n = 0; n < num_nodes; n++) {
1905352a5e7cSSebastian Grimberg           CeedInt      d_out              = 0, q_comp_out;
1906352a5e7cSSebastian Grimberg           CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE;
19071c66c397SJeremy L Thompson 
1908437c7c90SJeremy L Thompson           for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) {
1909437c7c90SJeremy L Thompson             const CeedInt     qq = data->num_eval_modes_out[b] * q;
1910437c7c90SJeremy L Thompson             const CeedScalar *B  = NULL;
19111c66c397SJeremy L Thompson 
1912506b1a0cSSebastian Grimberg             CeedCall(CeedOperatorGetBasisPointer(data->active_bases_out[b], data->eval_modes_out[b][e_out], identity, &B));
1913506b1a0cSSebastian Grimberg             CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_out[b], data->eval_modes_out[b][e_out], &q_comp_out));
1914352a5e7cSSebastian Grimberg             if (q_comp_out > 1) {
1915352a5e7cSSebastian Grimberg               if (e_out == 0 || data->eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0;
1916352a5e7cSSebastian Grimberg               else B = &B[(++d_out) * num_qpts * num_nodes];
1917352a5e7cSSebastian Grimberg             }
1918352a5e7cSSebastian Grimberg             eval_mode_out_prev                  = data->eval_modes_out[b][e_out];
1919352a5e7cSSebastian Grimberg             B_out[(qq + e_out) * num_nodes + n] = B[q * num_nodes + n];
1920ed9e99e6SJeremy L Thompson           }
1921ed9e99e6SJeremy L Thompson         }
1922ed9e99e6SJeremy L Thompson       }
19237c1dbaffSSebastian Grimberg       if (identity) CeedCall(CeedFree(&identity));
1924437c7c90SJeremy L Thompson       data->assembled_bases_out[b] = B_out;
1925437c7c90SJeremy L Thompson     }
1926ed9e99e6SJeremy L Thompson   }
1927ed9e99e6SJeremy L Thompson 
1928437c7c90SJeremy L Thompson   // Pass out assembled data
1929506b1a0cSSebastian Grimberg   if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in;
1930506b1a0cSSebastian Grimberg   if (active_bases_in) *active_bases_in = data->active_bases_in;
1931437c7c90SJeremy L Thompson   if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in;
1932506b1a0cSSebastian Grimberg   if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out;
1933506b1a0cSSebastian Grimberg   if (active_bases_out) *active_bases_out = data->active_bases_out;
1934437c7c90SJeremy L Thompson   if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out;
1935437c7c90SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1936437c7c90SJeremy L Thompson }
1937437c7c90SJeremy L Thompson 
1938437c7c90SJeremy L Thompson /**
1939ca94c3ddSJeremy L Thompson   @brief Get `CeedOperator` `CeedBasis` data for assembly.
1940ba746a46SJeremy L Thompson 
1941ca94c3ddSJeremy L Thompson   Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object.
1942437c7c90SJeremy L Thompson 
1943ca94c3ddSJeremy L Thompson   @param[in]  data                      `CeedOperatorAssemblyData`
1944ca94c3ddSJeremy L Thompson   @param[out] num_active_elem_rstrs_in  Number of active input element restrictions, or `NULL`
1945ca94c3ddSJeremy L Thompson   @param[out] active_elem_rstrs_in      Pointer to hold active input `CeedElemRestriction`, or `NULL`
1946ca94c3ddSJeremy L Thompson   @param[out] num_active_elem_rstrs_out Number of active output element restrictions, or `NULL`
1947ca94c3ddSJeremy L Thompson   @param[out] active_elem_rstrs_out     Pointer to hold active output `CeedElemRestriction`, or `NULL`
1948437c7c90SJeremy L Thompson 
1949437c7c90SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1950437c7c90SJeremy L Thompson 
1951437c7c90SJeremy L Thompson   @ref Backend
1952437c7c90SJeremy L Thompson **/
CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data,CeedInt * num_active_elem_rstrs_in,CeedElemRestriction ** active_elem_rstrs_in,CeedInt * num_active_elem_rstrs_out,CeedElemRestriction ** active_elem_rstrs_out)1953506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs_in,
1954506b1a0cSSebastian Grimberg                                                 CeedElemRestriction **active_elem_rstrs_in, CeedInt *num_active_elem_rstrs_out,
1955506b1a0cSSebastian Grimberg                                                 CeedElemRestriction **active_elem_rstrs_out) {
1956506b1a0cSSebastian Grimberg   if (num_active_elem_rstrs_in) *num_active_elem_rstrs_in = data->num_active_bases_in;
1957506b1a0cSSebastian Grimberg   if (active_elem_rstrs_in) *active_elem_rstrs_in = data->active_elem_rstrs_in;
1958506b1a0cSSebastian Grimberg   if (num_active_elem_rstrs_out) *num_active_elem_rstrs_out = data->num_active_bases_out;
1959506b1a0cSSebastian Grimberg   if (active_elem_rstrs_out) *active_elem_rstrs_out = data->active_elem_rstrs_out;
1960ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1961ed9e99e6SJeremy L Thompson }
1962ed9e99e6SJeremy L Thompson 
1963ed9e99e6SJeremy L Thompson /**
1964ca94c3ddSJeremy L Thompson   @brief Destroy `CeedOperatorAssemblyData`
1965ed9e99e6SJeremy L Thompson 
1966ca94c3ddSJeremy L Thompson   @param[in,out] data `CeedOperatorAssemblyData` to destroy
1967ed9e99e6SJeremy L Thompson 
1968ed9e99e6SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1969ed9e99e6SJeremy L Thompson 
1970ed9e99e6SJeremy L Thompson   @ref Backend
1971ed9e99e6SJeremy L Thompson **/
CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData * data)1972ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) {
1973ad6481ceSJeremy L Thompson   if (!*data) {
1974ad6481ceSJeremy L Thompson     *data = NULL;
1975ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1976ad6481ceSJeremy L Thompson   }
19772b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*data)->ceed));
1978506b1a0cSSebastian Grimberg   for (CeedInt b = 0; b < (*data)->num_active_bases_in; b++) {
1979506b1a0cSSebastian Grimberg     CeedCall(CeedBasisDestroy(&(*data)->active_bases_in[b]));
1980506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_in[b]));
1981437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_modes_in[b]));
1982437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b]));
1983437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_in[b]));
1984506b1a0cSSebastian Grimberg   }
1985506b1a0cSSebastian Grimberg   for (CeedInt b = 0; b < (*data)->num_active_bases_out; b++) {
1986506b1a0cSSebastian Grimberg     CeedCall(CeedBasisDestroy(&(*data)->active_bases_out[b]));
1987506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_out[b]));
1988506b1a0cSSebastian Grimberg     CeedCall(CeedFree(&(*data)->eval_modes_out[b]));
1989506b1a0cSSebastian Grimberg     CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b]));
1990437c7c90SJeremy L Thompson     CeedCall(CeedFree(&(*data)->assembled_bases_out[b]));
1991437c7c90SJeremy L Thompson   }
1992506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_bases_in));
1993506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_bases_out));
1994506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_elem_rstrs_in));
1995506b1a0cSSebastian Grimberg   CeedCall(CeedFree(&(*data)->active_elem_rstrs_out));
1996437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_in));
1997437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->num_eval_modes_out));
1998437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_in));
1999437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_modes_out));
2000437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_in));
2001437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->eval_mode_offsets_out));
2002437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_in));
2003437c7c90SJeremy L Thompson   CeedCall(CeedFree(&(*data)->assembled_bases_out));
2004ed9e99e6SJeremy L Thompson 
20052b730f8bSJeremy L Thompson   CeedCall(CeedFree(data));
2006ed9e99e6SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2007ed9e99e6SJeremy L Thompson }
2008ed9e99e6SJeremy L Thompson 
20094dd1a9d2SSebastian Grimberg /**
2010ca94c3ddSJeremy L Thompson   @brief Retrieve fallback `CeedOperator` with a reference `Ceed` for advanced `CeedOperator` functionality
20114dd1a9d2SSebastian Grimberg 
2012ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to retrieve fallback for
2013ca94c3ddSJeremy L Thompson   @param[out] op_fallback Fallback `CeedOperator`
20144dd1a9d2SSebastian Grimberg 
20154dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
20164dd1a9d2SSebastian Grimberg 
20174dd1a9d2SSebastian Grimberg   @ref Backend
20184dd1a9d2SSebastian Grimberg **/
CeedOperatorGetFallback(CeedOperator op,CeedOperator * op_fallback)20194dd1a9d2SSebastian Grimberg int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) {
20204dd1a9d2SSebastian Grimberg   // Create if needed
20214dd1a9d2SSebastian Grimberg   if (!op->op_fallback) CeedCall(CeedOperatorCreateFallback(op));
20224dd1a9d2SSebastian Grimberg   if (op->op_fallback) {
20234dd1a9d2SSebastian Grimberg     bool is_debug;
20241203703bSJeremy L Thompson     Ceed ceed;
20254dd1a9d2SSebastian Grimberg 
20264dd1a9d2SSebastian Grimberg     CeedCall(CeedOperatorGetCeed(op, &ceed));
20271203703bSJeremy L Thompson     CeedCall(CeedIsDebug(ceed, &is_debug));
20281203703bSJeremy L Thompson     if (is_debug) {
20291203703bSJeremy L Thompson       Ceed        ceed_fallback;
2030120566fcSJeremy L Thompson       const char *resource, *resource_fallback, *op_name;
20311203703bSJeremy L Thompson 
20324dd1a9d2SSebastian Grimberg       CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback));
20334dd1a9d2SSebastian Grimberg       CeedCall(CeedGetResource(ceed, &resource));
20344dd1a9d2SSebastian Grimberg       CeedCall(CeedGetResource(ceed_fallback, &resource_fallback));
2035120566fcSJeremy L Thompson       CeedCall(CeedOperatorGetName(op, &op_name));
20364dd1a9d2SSebastian Grimberg 
20374dd1a9d2SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n");
2038ca38d01dSJeremy L Thompson       CeedDebug(ceed, "Falling back from Operator with backend %s at address %p to Operator with backend %s at address %p for CeedOperator \"%s\"\n",
2039ca38d01dSJeremy L Thompson                 resource, op, resource_fallback, op->op_fallback, op_name);
20409bc66399SJeremy L Thompson       CeedCall(CeedDestroy(&ceed_fallback));
20414dd1a9d2SSebastian Grimberg     }
20429bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&ceed));
20434dd1a9d2SSebastian Grimberg   }
20444dd1a9d2SSebastian Grimberg   *op_fallback = op->op_fallback;
20454dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
20464dd1a9d2SSebastian Grimberg }
20474dd1a9d2SSebastian Grimberg 
20484dd1a9d2SSebastian Grimberg /**
2049ca94c3ddSJeremy L Thompson   @brief Get the parent `CeedOperator` for a fallback `CeedOperator`
20504dd1a9d2SSebastian Grimberg 
2051ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` context
2052ca94c3ddSJeremy L Thompson   @param[out] parent Variable to store parent `CeedOperator` context
20534dd1a9d2SSebastian Grimberg 
20544dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
20554dd1a9d2SSebastian Grimberg 
20564dd1a9d2SSebastian Grimberg   @ref Backend
20574dd1a9d2SSebastian Grimberg **/
CeedOperatorGetFallbackParent(CeedOperator op,CeedOperator * parent)20584dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParent(CeedOperator op, CeedOperator *parent) {
20594dd1a9d2SSebastian Grimberg   *parent = op->op_fallback_parent ? op->op_fallback_parent : NULL;
20604dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
20614dd1a9d2SSebastian Grimberg }
20624dd1a9d2SSebastian Grimberg 
20634dd1a9d2SSebastian Grimberg /**
2064ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` context of the parent `CeedOperator` for a fallback `CeedOperator`
20654dd1a9d2SSebastian Grimberg 
2066ca94c3ddSJeremy L Thompson   @param[in]  op     `CeedOperator` context
2067ca94c3ddSJeremy L Thompson   @param[out] parent Variable to store parent `Ceed` context
20684dd1a9d2SSebastian Grimberg 
20694dd1a9d2SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
20704dd1a9d2SSebastian Grimberg 
20714dd1a9d2SSebastian Grimberg   @ref Backend
20724dd1a9d2SSebastian Grimberg **/
CeedOperatorGetFallbackParentCeed(CeedOperator op,Ceed * parent)20734dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParentCeed(CeedOperator op, Ceed *parent) {
20749bc66399SJeremy L Thompson   *parent = NULL;
2075b0f67a9cSJeremy L Thompson   if (op->op_fallback_parent) CeedCall(CeedReferenceCopy(CeedOperatorReturnCeed(op->op_fallback_parent), parent));
20769bc66399SJeremy L Thompson   else CeedCall(CeedReferenceCopy(CeedOperatorReturnCeed(op), parent));
20774dd1a9d2SSebastian Grimberg   return CEED_ERROR_SUCCESS;
20784dd1a9d2SSebastian Grimberg }
20794dd1a9d2SSebastian Grimberg 
2080480fae85SJeremy L Thompson /// @}
2081480fae85SJeremy L Thompson 
2082480fae85SJeremy L Thompson /// ----------------------------------------------------------------------------
2083eaf62fffSJeremy L Thompson /// CeedOperator Public API
2084eaf62fffSJeremy L Thompson /// ----------------------------------------------------------------------------
2085eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser
2086eaf62fffSJeremy L Thompson /// @{
2087eaf62fffSJeremy L Thompson 
2088eaf62fffSJeremy L Thompson /**
2089ca94c3ddSJeremy L Thompson   @brief Assemble a linear `CeedQFunction` associated with a `CeedOperator`.
2090eaf62fffSJeremy L Thompson 
2091ca94c3ddSJeremy L Thompson   This returns a `CeedVector` containing a matrix at each quadrature point providing the action of the `CeedQFunction` associated with the `CeedOperator`.
2092ca94c3ddSJeremy 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.
2093859c15bbSJames Wright 
2094ca94c3ddSJeremy L Thompson   Inputs and outputs are in the order provided by the user when adding `CeedOperator` fields.
2095ca94c3ddSJeremy 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]`.
2096eaf62fffSJeremy L Thompson 
2097ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2098f04ea552SJeremy L Thompson 
2099ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2100ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points
2101ca94c3ddSJeremy L Thompson   @param[out] rstr      `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
2102ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2103eaf62fffSJeremy L Thompson 
2104eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2105eaf62fffSJeremy L Thompson 
2106eaf62fffSJeremy L Thompson   @ref User
2107eaf62fffSJeremy L Thompson **/
CeedOperatorLinearAssembleQFunction(CeedOperator op,CeedVector * assembled,CeedElemRestriction * rstr,CeedRequest * request)21082b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
21092b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2110eaf62fffSJeremy L Thompson 
2111eaf62fffSJeremy L Thompson   if (op->LinearAssembleQFunction) {
2112d04bbc78SJeremy L Thompson     // Backend version
21132b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request));
2114eaf62fffSJeremy L Thompson   } else {
2115d04bbc78SJeremy L Thompson     // Operator fallback
2116d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2117d04bbc78SJeremy L Thompson 
2118ca38d01dSJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssembleQFunction\n");
21192b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
21206574a04fSJeremy L Thompson     if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request));
21219bc66399SJeremy L Thompson     else return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction");
212270a7ffb3SJeremy L Thompson   }
2123eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2124eaf62fffSJeremy L Thompson }
212570a7ffb3SJeremy L Thompson 
212670a7ffb3SJeremy L Thompson /**
2127ca94c3ddSJeremy L Thompson   @brief Assemble `CeedQFunction` and store result internally.
21284385fb7fSSebastian Grimberg 
2129ea61e9acSJeremy L Thompson   Return copied references of stored data to the caller.
2130ea61e9acSJeremy L Thompson   Caller is responsible for ownership and destruction of the copied references.
2131ca94c3ddSJeremy L Thompson   See also @ref CeedOperatorLinearAssembleQFunction().
213270a7ffb3SJeremy L Thompson 
2133ca94c3ddSJeremy 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.
2134c5f45aeaSJeremy L Thompson         These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object.
2135c5f45aeaSJeremy L Thompson 
2136ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2137ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points
2138ca94c3ddSJeremy L Thompson   @param[out] rstr      `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction`
2139ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
214070a7ffb3SJeremy L Thompson 
214170a7ffb3SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
214270a7ffb3SJeremy L Thompson 
214370a7ffb3SJeremy L Thompson   @ref User
214470a7ffb3SJeremy L Thompson **/
CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op,CeedVector * assembled,CeedElemRestriction * rstr,CeedRequest * request)21452b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
21460816752eSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionBuildOrUpdate_Core(op, true, assembled, rstr, request);
2147eaf62fffSJeremy L Thompson }
2148eaf62fffSJeremy L Thompson 
2149eaf62fffSJeremy L Thompson /**
2150ca94c3ddSJeremy L Thompson   @brief Assemble the diagonal of a square linear `CeedOperator`
2151eaf62fffSJeremy L Thompson 
2152ca94c3ddSJeremy L Thompson   This overwrites a `CeedVector` with the diagonal of a linear `CeedOperator`.
2153eaf62fffSJeremy L Thompson 
2154ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2155eaf62fffSJeremy L Thompson 
2156ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2157f04ea552SJeremy L Thompson 
2158ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2159ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal
2160ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2161eaf62fffSJeremy L Thompson 
2162eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2163eaf62fffSJeremy L Thompson 
2164eaf62fffSJeremy L Thompson   @ref User
2165eaf62fffSJeremy L Thompson **/
CeedOperatorLinearAssembleDiagonal(CeedOperator op,CeedVector assembled,CeedRequest * request)21662b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2167f3d47e36SJeremy L Thompson   bool     is_composite;
21681c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
21691c66c397SJeremy L Thompson 
21702b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2171f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2172eaf62fffSJeremy L Thompson 
21732b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
21749bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
2175c9366a6bSJeremy L Thompson 
2176f3d47e36SJeremy L Thompson   // Early exit for empty operator
2177f3d47e36SJeremy L Thompson   if (!is_composite) {
2178f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2179f3d47e36SJeremy L Thompson 
2180f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2181f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2182f3d47e36SJeremy L Thompson   }
2183f3d47e36SJeremy L Thompson 
2184eaf62fffSJeremy L Thompson   if (op->LinearAssembleDiagonal) {
2185d04bbc78SJeremy L Thompson     // Backend version
21862b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleDiagonal(op, assembled, request));
2187eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2188eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddDiagonal) {
2189d04bbc78SJeremy L Thompson     // Backend version with zeroing first
21902b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
21912b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
2192eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
21930183ed61SJeremy L Thompson   } else if (is_composite) {
21940183ed61SJeremy L Thompson     // Default to summing contributions of suboperators
21950183ed61SJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
2196ed094490SJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddDiagonalComposite(op, request, false, assembled));
21970183ed61SJeremy L Thompson     return CEED_ERROR_SUCCESS;
2198eaf62fffSJeremy L Thompson   } else {
2199d04bbc78SJeremy L Thompson     // Operator fallback
2200d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2201d04bbc78SJeremy L Thompson 
2202ca38d01dSJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssembleDiagonal\n");
22032b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2204d04bbc78SJeremy L Thompson     if (op_fallback) {
22052b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request));
2206eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2207eaf62fffSJeremy L Thompson     }
2208eaf62fffSJeremy L Thompson   }
2209eaf62fffSJeremy L Thompson   // Default interface implementation
22102b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
22112b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request));
2212eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2213eaf62fffSJeremy L Thompson }
2214eaf62fffSJeremy L Thompson 
2215eaf62fffSJeremy L Thompson /**
2216ca94c3ddSJeremy L Thompson   @brief Assemble the diagonal of a square linear `CeedOperator`.
2217eaf62fffSJeremy L Thompson 
2218ca94c3ddSJeremy L Thompson   This sums into a `CeedVector` the diagonal of a linear `CeedOperator`.
2219eaf62fffSJeremy L Thompson 
2220ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2221eaf62fffSJeremy L Thompson 
2222ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
2223f04ea552SJeremy L Thompson 
2224ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2225ca94c3ddSJeremy L Thompson   @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal
2226ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2227eaf62fffSJeremy L Thompson 
2228eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2229eaf62fffSJeremy L Thompson 
2230eaf62fffSJeremy L Thompson   @ref User
2231eaf62fffSJeremy L Thompson **/
CeedOperatorLinearAssembleAddDiagonal(CeedOperator op,CeedVector assembled,CeedRequest * request)22322b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2233f3d47e36SJeremy L Thompson   bool     is_composite;
22341c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
22351c66c397SJeremy L Thompson 
22362b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2237f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2238eaf62fffSJeremy L Thompson 
22392b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
22409bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
2241c9366a6bSJeremy L Thompson 
2242f3d47e36SJeremy L Thompson   // Early exit for empty operator
2243f3d47e36SJeremy L Thompson   if (!is_composite) {
2244f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2245f3d47e36SJeremy L Thompson 
2246f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2247f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2248f3d47e36SJeremy L Thompson   }
2249f3d47e36SJeremy L Thompson 
2250eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddDiagonal) {
2251d04bbc78SJeremy L Thompson     // Backend version
22522b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request));
2253eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
22540183ed61SJeremy L Thompson   } else if (is_composite) {
22550183ed61SJeremy L Thompson     // Default to summing contributions of suboperators
2256ed094490SJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddDiagonalComposite(op, request, false, assembled));
225754d16554SHugh Carson     return CEED_ERROR_SUCCESS;
2258eaf62fffSJeremy L Thompson   } else {
2259d04bbc78SJeremy L Thompson     // Operator fallback
2260d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2261d04bbc78SJeremy L Thompson 
2262ca38d01dSJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssembleAddDiagonal\n");
22632b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2264d04bbc78SJeremy L Thompson     if (op_fallback) {
22652b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request));
2266eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2267eaf62fffSJeremy L Thompson     }
2268eaf62fffSJeremy L Thompson   }
2269eaf62fffSJeremy L Thompson   // Default interface implementation
2270ed094490SJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddDiagonalSingle(op, request, false, assembled));
2271d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2272eaf62fffSJeremy L Thompson }
2273eaf62fffSJeremy L Thompson 
2274eaf62fffSJeremy L Thompson /**
2275ca94c3ddSJeremy L Thompson    @brief Fully assemble the point-block diagonal pattern of a linear `CeedOperator`.
227601f0e615SJames Wright 
2277ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssemblePointBlockDiagonal().
227801f0e615SJames Wright 
2279ca94c3ddSJeremy 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)`.
2280ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are unique.
2281ca94c3ddSJeremy L Thompson    This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemblePointBlockDiagonal() provides the values in the same ordering.
228201f0e615SJames Wright 
228301f0e615SJames Wright    This will generally be slow unless your operator is low-order.
228401f0e615SJames Wright 
2285ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
228601f0e615SJames Wright 
2287ca94c3ddSJeremy L Thompson    @param[in]  op          `CeedOperator` to assemble
228801f0e615SJames Wright    @param[out] num_entries Number of entries in coordinate nonzero pattern
228901f0e615SJames Wright    @param[out] rows        Row number for each entry
229001f0e615SJames Wright    @param[out] cols        Column number for each entry
229101f0e615SJames Wright 
229201f0e615SJames Wright    @ref User
229301f0e615SJames Wright **/
CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op,CeedSize * num_entries,CeedInt ** rows,CeedInt ** cols)229401f0e615SJames Wright int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
229501f0e615SJames Wright   bool          is_composite;
229601f0e615SJames Wright   CeedInt       num_active_components, num_sub_operators;
229701f0e615SJames Wright   CeedOperator *sub_operators;
229801f0e615SJames Wright 
229901f0e615SJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
230001f0e615SJames Wright 
230101f0e615SJames Wright   CeedSize input_size = 0, output_size = 0;
230201f0e615SJames Wright   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
23039bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
230401f0e615SJames Wright 
230501f0e615SJames Wright   if (is_composite) {
2306ed094490SJeremy L Thompson     CeedCall(CeedOperatorCompositeGetNumSub(op, &num_sub_operators));
2307ed094490SJeremy L Thompson     CeedCall(CeedOperatorCompositeGetSubList(op, &sub_operators));
230801f0e615SJames Wright   } else {
230901f0e615SJames Wright     sub_operators     = &op;
231001f0e615SJames Wright     num_sub_operators = 1;
231101f0e615SJames Wright   }
231201f0e615SJames Wright 
2313506b1a0cSSebastian Grimberg   // Verify operator can be assembled correctly
2314506b1a0cSSebastian Grimberg   {
231501f0e615SJames Wright     CeedOperatorAssemblyData data;
2316506b1a0cSSebastian Grimberg     CeedInt                  num_active_elem_rstrs, comp_stride;
231701f0e615SJames Wright     CeedElemRestriction     *active_elem_rstrs;
231801f0e615SJames Wright 
231901f0e615SJames Wright     // Get initial values to check against
232001f0e615SJames Wright     CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[0], &data));
2321506b1a0cSSebastian Grimberg     CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL));
232201f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[0], &comp_stride));
232301f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[0], &num_active_components));
232401f0e615SJames Wright 
2325506b1a0cSSebastian Grimberg     // Verify that all active element restrictions have same component stride and number of components
232601f0e615SJames Wright     for (CeedInt k = 0; k < num_sub_operators; k++) {
232701f0e615SJames Wright       CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[k], &data));
2328506b1a0cSSebastian Grimberg       CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL));
232901f0e615SJames Wright       for (CeedInt i = 0; i < num_active_elem_rstrs; i++) {
2330506b1a0cSSebastian Grimberg         CeedInt comp_stride_sub, num_active_components_sub;
2331506b1a0cSSebastian Grimberg 
233201f0e615SJames Wright         CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[i], &comp_stride_sub));
23339bc66399SJeremy L Thompson         CeedCheck(comp_stride == comp_stride_sub, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION,
233401f0e615SJames Wright                   "Active element restrictions must have the same component stride: %d vs %d", comp_stride, comp_stride_sub);
233501f0e615SJames Wright         CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[i], &num_active_components_sub));
23369bc66399SJeremy L Thompson         CeedCheck(num_active_components == num_active_components_sub, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
23373f08121cSJeremy L Thompson                   "All suboperators must have the same number of output components."
23383f08121cSJeremy L Thompson                   " Previous: %" CeedInt_FMT " Current: %" CeedInt_FMT,
23393f08121cSJeremy L Thompson                   num_active_components, num_active_components_sub);
234001f0e615SJames Wright       }
234101f0e615SJames Wright     }
234201f0e615SJames Wright   }
234301f0e615SJames Wright   *num_entries = input_size * num_active_components;
234401f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, rows));
234501f0e615SJames Wright   CeedCall(CeedCalloc(*num_entries, cols));
234601f0e615SJames Wright 
234701f0e615SJames Wright   for (CeedInt o = 0; o < num_sub_operators; o++) {
2348506b1a0cSSebastian Grimberg     CeedElemRestriction active_elem_rstr, point_block_active_elem_rstr;
234901f0e615SJames Wright     CeedInt             comp_stride, num_elem, elem_size;
2350506b1a0cSSebastian Grimberg     const CeedInt      *offsets, *point_block_offsets;
235101f0e615SJames Wright 
235201f0e615SJames Wright     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[o], &active_elem_rstr));
235301f0e615SJames Wright     CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstr, &comp_stride));
235401f0e615SJames Wright     CeedCall(CeedElemRestrictionGetNumElements(active_elem_rstr, &num_elem));
235501f0e615SJames Wright     CeedCall(CeedElemRestrictionGetElementSize(active_elem_rstr, &elem_size));
235601f0e615SJames Wright     CeedCall(CeedElemRestrictionGetOffsets(active_elem_rstr, CEED_MEM_HOST, &offsets));
235701f0e615SJames Wright 
2358506b1a0cSSebastian Grimberg     CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstr, &point_block_active_elem_rstr));
2359506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOffsets(point_block_active_elem_rstr, CEED_MEM_HOST, &point_block_offsets));
236001f0e615SJames Wright 
236101f0e615SJames Wright     for (CeedSize i = 0; i < num_elem * elem_size; i++) {
236201f0e615SJames Wright       for (CeedInt c_out = 0; c_out < num_active_components; c_out++) {
236301f0e615SJames Wright         for (CeedInt c_in = 0; c_in < num_active_components; c_in++) {
2364506b1a0cSSebastian Grimberg           (*rows)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_out * comp_stride;
2365506b1a0cSSebastian Grimberg           (*cols)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_in * comp_stride;
236601f0e615SJames Wright         }
236701f0e615SJames Wright       }
236801f0e615SJames Wright     }
236901f0e615SJames Wright 
237001f0e615SJames Wright     CeedCall(CeedElemRestrictionRestoreOffsets(active_elem_rstr, &offsets));
2371506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOffsets(point_block_active_elem_rstr, &point_block_offsets));
2372681d0ea7SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&active_elem_rstr));
2373506b1a0cSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&point_block_active_elem_rstr));
237401f0e615SJames Wright   }
237501f0e615SJames Wright   return CEED_ERROR_SUCCESS;
237601f0e615SJames Wright }
237701f0e615SJames Wright 
237801f0e615SJames Wright /**
2379ca94c3ddSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear `CeedOperator`.
2380eaf62fffSJeremy L Thompson 
2381ca94c3ddSJeremy L Thompson   This overwrites a `CeedVector` with the point block diagonal of a linear `CeedOperator`.
2382eaf62fffSJeremy L Thompson 
2383ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2384eaf62fffSJeremy L Thompson 
2385ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2386f04ea552SJeremy L Thompson 
2387ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2388ca94c3ddSJeremy 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.
2389ca94c3ddSJeremy L Thompson                           The dimensions of this vector are derived from the active vector for the `CeedOperator`.
2390ca94c3ddSJeremy L Thompson                           The array has shape `[nodes, component out, component in]`.
2391ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2392eaf62fffSJeremy L Thompson 
2393eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2394eaf62fffSJeremy L Thompson 
2395eaf62fffSJeremy L Thompson   @ref User
2396eaf62fffSJeremy L Thompson **/
CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op,CeedVector assembled,CeedRequest * request)23972b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2398f3d47e36SJeremy L Thompson   bool     is_composite;
23991c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
24001c66c397SJeremy L Thompson 
24012b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2402f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2403eaf62fffSJeremy L Thompson 
24042b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
24059bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
2406c9366a6bSJeremy L Thompson 
2407f3d47e36SJeremy L Thompson   // Early exit for empty operator
2408f3d47e36SJeremy L Thompson   if (!is_composite) {
2409f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2410f3d47e36SJeremy L Thompson 
2411f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2412f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2413f3d47e36SJeremy L Thompson   }
2414f3d47e36SJeremy L Thompson 
2415eaf62fffSJeremy L Thompson   if (op->LinearAssemblePointBlockDiagonal) {
2416d04bbc78SJeremy L Thompson     // Backend version
24172b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request));
2418eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2419eaf62fffSJeremy L Thompson   } else if (op->LinearAssembleAddPointBlockDiagonal) {
2420d04bbc78SJeremy L Thompson     // Backend version with zeroing first
24212b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(assembled, 0.0));
24222b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
2423eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2424eaf62fffSJeremy L Thompson   } else {
2425d04bbc78SJeremy L Thompson     // Operator fallback
2426d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2427d04bbc78SJeremy L Thompson 
2428ca38d01dSJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssemblePointBlockDiagonal\n");
24292b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2430d04bbc78SJeremy L Thompson     if (op_fallback) {
24312b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request));
2432eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2433eaf62fffSJeremy L Thompson     }
2434eaf62fffSJeremy L Thompson   }
2435eaf62fffSJeremy L Thompson   // Default interface implementation
24362b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(assembled, 0.0));
24372b730f8bSJeremy L Thompson   CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request));
2438eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2439eaf62fffSJeremy L Thompson }
2440eaf62fffSJeremy L Thompson 
2441eaf62fffSJeremy L Thompson /**
2442ca94c3ddSJeremy L Thompson   @brief Assemble the point block diagonal of a square linear `CeedOperator`.
2443eaf62fffSJeremy L Thompson 
2444ca94c3ddSJeremy L Thompson   This sums into a `CeedVector` with the point block diagonal of a linear `CeedOperator`.
2445eaf62fffSJeremy L Thompson 
2446ca94c3ddSJeremy L Thompson   Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported.
2447eaf62fffSJeremy L Thompson 
2448ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2449f04ea552SJeremy L Thompson 
2450ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator` to assemble `CeedQFunction`
2451ca94c3ddSJeremy 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.
2452ca94c3ddSJeremy L Thompson                           The dimensions of this vector are derived from the active vector for the `CeedOperator`.
2453ca94c3ddSJeremy L Thompson                           The array has shape `[nodes, component out, component in]`.
2454ca94c3ddSJeremy L Thompson   @param[in]  request   Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2455eaf62fffSJeremy L Thompson 
2456eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2457eaf62fffSJeremy L Thompson 
2458eaf62fffSJeremy L Thompson   @ref User
2459eaf62fffSJeremy L Thompson **/
CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op,CeedVector assembled,CeedRequest * request)24602b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) {
2461f3d47e36SJeremy L Thompson   bool     is_composite;
24621c66c397SJeremy L Thompson   CeedSize input_size = 0, output_size = 0;
24631c66c397SJeremy L Thompson 
24642b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2465f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2466eaf62fffSJeremy L Thompson 
24672b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
24689bc66399SJeremy L Thompson   CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square");
2469c9366a6bSJeremy L Thompson 
2470f3d47e36SJeremy L Thompson   // Early exit for empty operator
2471f3d47e36SJeremy L Thompson   if (!is_composite) {
2472f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2473f3d47e36SJeremy L Thompson 
2474f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2475f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2476f3d47e36SJeremy L Thompson   }
2477f3d47e36SJeremy L Thompson 
2478eaf62fffSJeremy L Thompson   if (op->LinearAssembleAddPointBlockDiagonal) {
2479d04bbc78SJeremy L Thompson     // Backend version
24802b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request));
2481eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2482eaf62fffSJeremy L Thompson   } else {
2483d04bbc78SJeremy L Thompson     // Operator fallback
2484d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2485d04bbc78SJeremy L Thompson 
2486ca38d01dSJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssembleAddPointBlockDiagonal\n");
24872b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2488d04bbc78SJeremy L Thompson     if (op_fallback) {
24892b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request));
2490eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2491eaf62fffSJeremy L Thompson     }
2492eaf62fffSJeremy L Thompson   }
2493ea61e9acSJeremy L Thompson   // Default interface implementation
2494eaf62fffSJeremy L Thompson   if (is_composite) {
2495ed094490SJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddDiagonalComposite(op, request, true, assembled));
2496eaf62fffSJeremy L Thompson   } else {
2497ed094490SJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleAddDiagonalSingle(op, request, true, assembled));
2498eaf62fffSJeremy L Thompson   }
2499d04bbc78SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2500eaf62fffSJeremy L Thompson }
2501eaf62fffSJeremy L Thompson 
2502eaf62fffSJeremy L Thompson /**
2503ca94c3ddSJeremy L Thompson    @brief Fully assemble the nonzero pattern of a linear `CeedOperator`.
2504eaf62fffSJeremy L Thompson 
2505ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssemble().
2506eaf62fffSJeremy L Thompson 
2507ca94c3ddSJeremy 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)`.
2508ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are not unique and may repeat.
2509ca94c3ddSJeremy L Thompson    This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemble() provides the values in the same ordering.
2510eaf62fffSJeremy L Thompson 
2511eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2512eaf62fffSJeremy L Thompson 
2513ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2514f04ea552SJeremy L Thompson 
2515ca94c3ddSJeremy L Thompson    @param[in]  op          `CeedOperator` to assemble
2516eaf62fffSJeremy L Thompson    @param[out] num_entries Number of entries in coordinate nonzero pattern
2517eaf62fffSJeremy L Thompson    @param[out] rows        Row number for each entry
2518eaf62fffSJeremy L Thompson    @param[out] cols        Column number for each entry
2519eaf62fffSJeremy L Thompson 
2520eaf62fffSJeremy L Thompson    @ref User
2521eaf62fffSJeremy L Thompson **/
CeedOperatorLinearAssembleSymbolic(CeedOperator op,CeedSize * num_entries,CeedInt ** rows,CeedInt ** cols)25222b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) {
25231c66c397SJeremy L Thompson   bool          is_composite;
25241c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2525b94338b9SJed Brown   CeedSize      single_entries;
2526eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
25271c66c397SJeremy L Thompson 
25282b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2529f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2530eaf62fffSJeremy L Thompson 
2531eaf62fffSJeremy L Thompson   if (op->LinearAssembleSymbolic) {
2532d04bbc78SJeremy L Thompson     // Backend version
25332b730f8bSJeremy L Thompson     CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols));
2534eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2535eaf62fffSJeremy L Thompson   } else {
2536d04bbc78SJeremy L Thompson     // Operator fallback
2537d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2538d04bbc78SJeremy L Thompson 
2539ca38d01dSJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssembleSymbolic\n");
25402b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2541d04bbc78SJeremy L Thompson     if (op_fallback) {
25422b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols));
2543eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2544eaf62fffSJeremy L Thompson     }
2545eaf62fffSJeremy L Thompson   }
2546eaf62fffSJeremy L Thompson 
2547eaf62fffSJeremy L Thompson   // Default interface implementation
2548eaf62fffSJeremy L Thompson 
2549506b1a0cSSebastian Grimberg   // Count entries and allocate rows, cols arrays
255056318ee4SZach Atkins   CeedCall(CeedOperatorLinearAssembleGetNumEntries(op, num_entries));
25512b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, rows));
25522b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(*num_entries, cols));
2553eaf62fffSJeremy L Thompson 
2554506b1a0cSSebastian Grimberg   // Assemble nonzero locations
2555eaf62fffSJeremy L Thompson   if (is_composite) {
2556ed094490SJeremy L Thompson     CeedCall(CeedOperatorCompositeGetNumSub(op, &num_suboperators));
2557ed094490SJeremy L Thompson     CeedCall(CeedOperatorCompositeGetSubList(op, &sub_operators));
255892ae7e47SJeremy L Thompson     for (CeedInt k = 0; k < num_suboperators; ++k) {
2559ed094490SJeremy L Thompson       CeedCall(CeedOperatorAssembleSymbolicSingle(sub_operators[k], offset, *rows, *cols));
2560ed094490SJeremy L Thompson       CeedCall(CeedOperatorAssemblyCountEntriesSingle(sub_operators[k], &single_entries));
2561eaf62fffSJeremy L Thompson       offset += single_entries;
2562eaf62fffSJeremy L Thompson     }
2563eaf62fffSJeremy L Thompson   } else {
2564ed094490SJeremy L Thompson     CeedCall(CeedOperatorAssembleSymbolicSingle(op, offset, *rows, *cols));
2565eaf62fffSJeremy L Thompson   }
2566eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2567eaf62fffSJeremy L Thompson }
2568eaf62fffSJeremy L Thompson 
2569eaf62fffSJeremy L Thompson /**
2570eaf62fffSJeremy L Thompson    @brief Fully assemble the nonzero entries of a linear operator.
2571eaf62fffSJeremy L Thompson 
2572ca94c3ddSJeremy L Thompson    Expected to be used in conjunction with @ref CeedOperatorLinearAssembleSymbolic().
2573eaf62fffSJeremy L Thompson 
2574ca94c3ddSJeremy 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)`.
2575ca94c3ddSJeremy L Thompson    Note that the `(i, j)` pairs are not unique and may repeat.
2576ca94c3ddSJeremy L Thompson    This function returns the values of the nonzero entries to be added, their `(i, j)` locations are provided by @ref CeedOperatorLinearAssembleSymbolic().
2577eaf62fffSJeremy L Thompson 
2578eaf62fffSJeremy L Thompson    This will generally be slow unless your operator is low-order.
2579eaf62fffSJeremy L Thompson 
2580ca94c3ddSJeremy L Thompson    Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2581f04ea552SJeremy L Thompson 
2582ca94c3ddSJeremy L Thompson    @param[in]  op     `CeedOperator` to assemble
2583eaf62fffSJeremy L Thompson    @param[out] values Values to assemble into matrix
2584eaf62fffSJeremy L Thompson 
2585eaf62fffSJeremy L Thompson    @ref User
2586eaf62fffSJeremy L Thompson **/
CeedOperatorLinearAssemble(CeedOperator op,CeedVector values)2587eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) {
2588a34b87f3SZach Atkins   bool          is_composite;
25891c66c397SJeremy L Thompson   CeedInt       num_suboperators, offset = 0;
2590b94338b9SJed Brown   CeedSize      single_entries = 0;
2591eaf62fffSJeremy L Thompson   CeedOperator *sub_operators;
25921c66c397SJeremy L Thompson 
25932b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2594f3d47e36SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2595f3d47e36SJeremy L Thompson 
2596f3d47e36SJeremy L Thompson   // Early exit for empty operator
2597f3d47e36SJeremy L Thompson   if (!is_composite) {
2598f3d47e36SJeremy L Thompson     CeedInt num_elem = 0;
2599f3d47e36SJeremy L Thompson 
2600f3d47e36SJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
2601f3d47e36SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
2602f3d47e36SJeremy L Thompson   }
2603eaf62fffSJeremy L Thompson 
2604eaf62fffSJeremy L Thompson   if (op->LinearAssemble) {
2605d04bbc78SJeremy L Thompson     // Backend version
26062b730f8bSJeremy L Thompson     CeedCall(op->LinearAssemble(op, values));
2607eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2608a34b87f3SZach Atkins   } else if (is_composite) {
2609915834c9SZach Atkins     // Default to summing contributions of suboperators
2610915834c9SZach Atkins     CeedCall(CeedVectorSetValue(values, 0.0));
2611ed094490SJeremy L Thompson     CeedCall(CeedOperatorCompositeGetNumSub(op, &num_suboperators));
2612ed094490SJeremy L Thompson     CeedCall(CeedOperatorCompositeGetSubList(op, &sub_operators));
2613915834c9SZach Atkins     for (CeedInt k = 0; k < num_suboperators; k++) {
2614ed094490SJeremy L Thompson       CeedCall(CeedOperatorAssembleSingle(sub_operators[k], offset, values));
2615ed094490SJeremy L Thompson       CeedCall(CeedOperatorAssemblyCountEntriesSingle(sub_operators[k], &single_entries));
2616915834c9SZach Atkins       offset += single_entries;
2617915834c9SZach Atkins     }
2618a34b87f3SZach Atkins     return CEED_ERROR_SUCCESS;
2619a34b87f3SZach Atkins   } else if (op->LinearAssembleSingle) {
2620a34b87f3SZach Atkins     CeedCall(CeedVectorSetValue(values, 0.0));
2621ed094490SJeremy L Thompson     CeedCall(CeedOperatorAssembleSingle(op, offset, values));
2622915834c9SZach Atkins     return CEED_ERROR_SUCCESS;
2623eaf62fffSJeremy L Thompson   } else {
2624d04bbc78SJeremy L Thompson     // Operator fallback
2625d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2626d04bbc78SJeremy L Thompson 
2627ca38d01dSJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssemble\n");
26282b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2629d04bbc78SJeremy L Thompson     if (op_fallback) {
26302b730f8bSJeremy L Thompson       CeedCall(CeedOperatorLinearAssemble(op_fallback, values));
2631eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2632eaf62fffSJeremy L Thompson     }
2633eaf62fffSJeremy L Thompson   }
2634eaf62fffSJeremy L Thompson 
2635a34b87f3SZach Atkins   // Default to interface version if non-composite and no fallback
263628ec399dSJeremy L Thompson   CeedCall(CeedVectorSetValue(values, 0.0));
2637ed094490SJeremy L Thompson   CeedCall(CeedOperatorAssembleSingle(op, offset, values));
2638eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2639eaf62fffSJeremy L Thompson }
2640eaf62fffSJeremy L Thompson 
2641eaf62fffSJeremy L Thompson /**
2642ca94c3ddSJeremy L Thompson   @brief Get the multiplicity of nodes across sub-operators in a composite `CeedOperator`.
264375f0d5a4SJeremy L Thompson 
2644ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
264575f0d5a4SJeremy L Thompson 
2646ca94c3ddSJeremy L Thompson   @param[in]  op               Composite `CeedOperator`
2647ca94c3ddSJeremy L Thompson   @param[in]  num_skip_indices Number of sub-operators to skip
2648ca94c3ddSJeremy L Thompson   @param[in]  skip_indices     Array of indices of sub-operators to skip
2649ca94c3ddSJeremy L Thompson   @param[out] mult             Vector to store multiplicity (of size `l_size` )
265075f0d5a4SJeremy L Thompson 
265175f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
265275f0d5a4SJeremy L Thompson 
265375f0d5a4SJeremy L Thompson   @ref User
265475f0d5a4SJeremy L Thompson **/
CeedOperatorCompositeGetMultiplicity(CeedOperator op,CeedInt num_skip_indices,CeedInt * skip_indices,CeedVector mult)2655ed094490SJeremy L Thompson int CeedOperatorCompositeGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) {
265675f0d5a4SJeremy L Thompson   Ceed                ceed;
2657b275c451SJeremy L Thompson   CeedInt             num_suboperators;
265875f0d5a4SJeremy L Thompson   CeedSize            l_vec_len;
265975f0d5a4SJeremy L Thompson   CeedScalar         *mult_array;
266075f0d5a4SJeremy L Thompson   CeedVector          ones_l_vec;
26617c1dbaffSSebastian Grimberg   CeedElemRestriction elem_rstr, mult_elem_rstr;
2662b275c451SJeremy L Thompson   CeedOperator       *sub_operators;
266375f0d5a4SJeremy L Thompson 
26641c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
26651c66c397SJeremy L Thompson 
266675f0d5a4SJeremy L Thompson   // Zero mult vector
266775f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
266875f0d5a4SJeremy L Thompson 
266975f0d5a4SJeremy L Thompson   // Get suboperators
2670ed094490SJeremy L Thompson   CeedCall(CeedOperatorCompositeGetNumSub(op, &num_suboperators));
2671b275c451SJeremy L Thompson   if (num_suboperators == 0) return CEED_ERROR_SUCCESS;
2672ed094490SJeremy L Thompson   CeedCall(CeedOperatorCompositeGetSubList(op, &sub_operators));
267375f0d5a4SJeremy L Thompson 
267475f0d5a4SJeremy L Thompson   // Work vector
267575f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetLength(mult, &l_vec_len));
26769bc66399SJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
267775f0d5a4SJeremy L Thompson   CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec));
26789bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
267975f0d5a4SJeremy L Thompson   CeedCall(CeedVectorSetValue(ones_l_vec, 1.0));
268075f0d5a4SJeremy L Thompson   CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array));
268175f0d5a4SJeremy L Thompson 
268275f0d5a4SJeremy L Thompson   // Compute multiplicity across suboperators
2683b275c451SJeremy L Thompson   for (CeedInt i = 0; i < num_suboperators; i++) {
268475f0d5a4SJeremy L Thompson     const CeedScalar *sub_mult_array;
268575f0d5a4SJeremy L Thompson     CeedVector        sub_mult_l_vec, ones_e_vec;
268675f0d5a4SJeremy L Thompson 
268775f0d5a4SJeremy L Thompson     // -- Check for suboperator to skip
268875f0d5a4SJeremy L Thompson     for (CeedInt j = 0; j < num_skip_indices; j++) {
268975f0d5a4SJeremy L Thompson       if (skip_indices[j] == i) continue;
269075f0d5a4SJeremy L Thompson     }
269175f0d5a4SJeremy L Thompson 
269275f0d5a4SJeremy L Thompson     // -- Sub operator multiplicity
2693437c7c90SJeremy L Thompson     CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr));
26947c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr, &mult_elem_rstr));
2695681d0ea7SJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&elem_rstr));
26967c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionCreateVector(mult_elem_rstr, &sub_mult_l_vec, &ones_e_vec));
269775f0d5a4SJeremy L Thompson     CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0));
26987c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE));
26997c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE));
270075f0d5a4SJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array));
270175f0d5a4SJeremy L Thompson     // ---- Flag every node present in the current suboperator
2702c81f2b9dSJames Wright     for (CeedSize j = 0; j < l_vec_len; j++) {
270375f0d5a4SJeremy L Thompson       if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0;
270475f0d5a4SJeremy L Thompson     }
270575f0d5a4SJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array));
270675f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&sub_mult_l_vec));
270775f0d5a4SJeremy L Thompson     CeedCall(CeedVectorDestroy(&ones_e_vec));
27087c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionDestroy(&mult_elem_rstr));
270975f0d5a4SJeremy L Thompson   }
271075f0d5a4SJeremy L Thompson   CeedCall(CeedVectorRestoreArray(mult, &mult_array));
2711811d0ccfSJeremy L Thompson   CeedCall(CeedVectorDestroy(&ones_l_vec));
271275f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
271375f0d5a4SJeremy L Thompson }
271475f0d5a4SJeremy L Thompson 
271575f0d5a4SJeremy L Thompson /**
2716ca94c3ddSJeremy 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.
2717eaf62fffSJeremy L Thompson 
2718ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2719f04ea552SJeremy L Thompson 
2720ca94c3ddSJeremy L Thompson   @param[in]  op_fine      Fine grid `CeedOperator`
2721ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine  L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2722ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse  Coarse grid `CeedElemRestriction`
2723ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse Coarse grid active vector `CeedBasis`
2724ca94c3ddSJeremy L Thompson   @param[out] op_coarse    Coarse grid `CeedOperator`
2725ca94c3ddSJeremy L Thompson   @param[out] op_prolong   Coarse to fine `CeedOperator`, or `NULL`
2726ca94c3ddSJeremy L Thompson   @param[out] op_restrict  Fine to coarse `CeedOperator`, or `NULL`
2727eaf62fffSJeremy L Thompson 
2728eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2729eaf62fffSJeremy L Thompson 
2730eaf62fffSJeremy L Thompson   @ref User
2731eaf62fffSJeremy L Thompson **/
CeedOperatorMultigridLevelCreate(CeedOperator op_fine,CeedVector p_mult_fine,CeedElemRestriction rstr_coarse,CeedBasis basis_coarse,CeedOperator * op_coarse,CeedOperator * op_prolong,CeedOperator * op_restrict)27322b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
27337758292fSSebastian Grimberg                                      CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) {
27341c66c397SJeremy L Thompson   CeedBasis basis_c_to_f = NULL;
27351c66c397SJeremy L Thompson 
27362b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
2737eaf62fffSJeremy L Thompson 
273883d6adf3SZach Atkins   // Build prolongation matrix, if required
27397758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
274083d6adf3SZach Atkins     CeedBasis basis_fine;
27411c66c397SJeremy L Thompson 
27422b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
27432b730f8bSJeremy L Thompson     CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f));
2744681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis_fine));
274583d6adf3SZach Atkins   }
2746eaf62fffSJeremy L Thompson 
2747f113e5dcSJeremy L Thompson   // Core code
27481a8516d0SJames Wright   CeedCall(CeedOperatorMultigridLevelCreateSingle_Core(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong,
27491a8516d0SJames Wright                                                        op_restrict));
2750eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2751eaf62fffSJeremy L Thompson }
2752eaf62fffSJeremy L Thompson 
2753eaf62fffSJeremy L Thompson /**
2754ca94c3ddSJeremy L Thompson   @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a tensor basis for the active basis.
2755eaf62fffSJeremy L Thompson 
2756ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2757f04ea552SJeremy L Thompson 
2758ca94c3ddSJeremy L Thompson   @param[in]  op_fine       Fine grid `CeedOperator`
2759ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2760ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid `CeedElemRestriction`
2761ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector `CeedBasis`
2762ca94c3ddSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator`
2763ca94c3ddSJeremy L Thompson   @param[out] op_coarse     Coarse grid `CeedOperator`
2764ca94c3ddSJeremy L Thompson   @param[out] op_prolong    Coarse to fine `CeedOperator`, or `NULL`
2765ca94c3ddSJeremy L Thompson   @param[out] op_restrict   Fine to coarse `CeedOperator`, or `NULL`
2766eaf62fffSJeremy L Thompson 
2767eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2768eaf62fffSJeremy L Thompson 
2769eaf62fffSJeremy L Thompson   @ref User
2770eaf62fffSJeremy L Thompson **/
CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine,CeedVector p_mult_fine,CeedElemRestriction rstr_coarse,CeedBasis basis_coarse,const CeedScalar * interp_c_to_f,CeedOperator * op_coarse,CeedOperator * op_prolong,CeedOperator * op_restrict)27712b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
27722b730f8bSJeremy L Thompson                                              const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
27737758292fSSebastian Grimberg                                              CeedOperator *op_restrict) {
2774eaf62fffSJeremy L Thompson   Ceed      ceed;
27751c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
27761c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
27771c66c397SJeremy L Thompson 
27781c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
27792b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2780eaf62fffSJeremy L Thompson 
2781eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
27822b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
27832b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
27842b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
27853f08121cSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION,
27863f08121cSJeremy L Thompson             "Bases must have compatible quadrature spaces."
27873f08121cSJeremy L Thompson             " Fine grid: %" CeedInt_FMT " points, Coarse grid: %" CeedInt_FMT " points",
27883f08121cSJeremy L Thompson             Q_f, Q_c);
2789eaf62fffSJeremy L Thompson 
279083d6adf3SZach Atkins   // Create coarse to fine basis, if required
27917758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
27921c66c397SJeremy L Thompson     CeedInt     dim, num_comp, num_nodes_c, P_1d_f, P_1d_c;
27931c66c397SJeremy L Thompson     CeedScalar *q_ref, *q_weight, *grad;
27941c66c397SJeremy L Thompson 
279583d6adf3SZach Atkins     // Check if interpolation matrix is provided
27966574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
27976574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
27982b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
27992b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
28002b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f));
2801681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis_fine));
28022b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
28032b730f8bSJeremy L Thompson     P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c);
28042b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_ref));
28052b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f, &q_weight));
28062b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad));
28072b730f8bSJeremy 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));
28082b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
28092b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
28102b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
281183d6adf3SZach Atkins   }
2812eaf62fffSJeremy L Thompson 
2813eaf62fffSJeremy L Thompson   // Core code
28141a8516d0SJames Wright   CeedCall(CeedOperatorMultigridLevelCreateSingle_Core(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong,
28151a8516d0SJames Wright                                                        op_restrict));
28169bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
2817eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2818eaf62fffSJeremy L Thompson }
2819eaf62fffSJeremy L Thompson 
2820eaf62fffSJeremy L Thompson /**
2821ca94c3ddSJeremy L Thompson   @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a non-tensor basis for the active vector
2822eaf62fffSJeremy L Thompson 
2823ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable.
2824f04ea552SJeremy L Thompson 
2825ca94c3ddSJeremy L Thompson   @param[in]  op_fine       Fine grid `CeedOperator`
2826ca94c3ddSJeremy L Thompson   @param[in]  p_mult_fine   L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator`
2827ca94c3ddSJeremy L Thompson   @param[in]  rstr_coarse   Coarse grid `CeedElemRestriction`
2828ca94c3ddSJeremy L Thompson   @param[in]  basis_coarse  Coarse grid active vector `CeedBasis`
2829ca94c3ddSJeremy L Thompson   @param[in]  interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator`
2830ca94c3ddSJeremy L Thompson   @param[out] op_coarse     Coarse grid `CeedOperator`
2831ca94c3ddSJeremy L Thompson   @param[out] op_prolong    Coarse to fine `CeedOperator`, or `NULL`
2832ca94c3ddSJeremy L Thompson   @param[out] op_restrict   Fine to coarse `CeedOperator`, or `NULL`
2833eaf62fffSJeremy L Thompson 
2834eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2835eaf62fffSJeremy L Thompson 
2836eaf62fffSJeremy L Thompson   @ref User
2837eaf62fffSJeremy L Thompson **/
CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine,CeedVector p_mult_fine,CeedElemRestriction rstr_coarse,CeedBasis basis_coarse,const CeedScalar * interp_c_to_f,CeedOperator * op_coarse,CeedOperator * op_prolong,CeedOperator * op_restrict)28382b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse,
28397758292fSSebastian Grimberg                                        const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong,
28407758292fSSebastian Grimberg                                        CeedOperator *op_restrict) {
2841eaf62fffSJeremy L Thompson   Ceed      ceed;
28421c66c397SJeremy L Thompson   CeedInt   Q_f, Q_c;
28431c66c397SJeremy L Thompson   CeedBasis basis_fine, basis_c_to_f = NULL;
28441c66c397SJeremy L Thompson 
28451c66c397SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op_fine));
28462b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op_fine, &ceed));
2847eaf62fffSJeremy L Thompson 
2848eaf62fffSJeremy L Thompson   // Check for compatible quadrature spaces
28492b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine));
28502b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f));
28512b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c));
28526574a04fSJeremy L Thompson   CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces");
2853eaf62fffSJeremy L Thompson 
2854eaf62fffSJeremy L Thompson   // Coarse to fine basis
28557758292fSSebastian Grimberg   if (op_prolong || op_restrict) {
28561c66c397SJeremy L Thompson     CeedInt          dim, num_comp, num_nodes_c, num_nodes_f;
28571c66c397SJeremy L Thompson     CeedScalar      *q_ref, *q_weight, *grad;
28581c66c397SJeremy L Thompson     CeedElemTopology topo;
28591c66c397SJeremy L Thompson 
286083d6adf3SZach Atkins     // Check if interpolation matrix is provided
28616574a04fSJeremy L Thompson     CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE,
28626574a04fSJeremy L Thompson               "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix");
28632b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetTopology(basis_fine, &topo));
28642b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(basis_fine, &dim));
28652b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp));
28662b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f));
2867681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis_fine));
28682b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c));
28692b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref));
28702b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f, &q_weight));
28712b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad));
28722b730f8bSJeremy 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));
28732b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref));
28742b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight));
28752b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad));
287683d6adf3SZach Atkins   }
2877eaf62fffSJeremy L Thompson 
2878eaf62fffSJeremy L Thompson   // Core code
28791a8516d0SJames Wright   CeedCall(CeedOperatorMultigridLevelCreateSingle_Core(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong,
28801a8516d0SJames Wright                                                        op_restrict));
28819bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
2882eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2883eaf62fffSJeremy L Thompson }
2884eaf62fffSJeremy L Thompson 
2885eaf62fffSJeremy L Thompson /**
2886ca94c3ddSJeremy L Thompson   @brief Build a FDM based approximate inverse for each element for a `CeedOperator`.
2887eaf62fffSJeremy L Thompson 
2888ca94c3ddSJeremy L Thompson   This returns a `CeedOperator` and `CeedVector` to apply a Fast Diagonalization Method based approximate inverse.
2889859c15bbSJames 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$.
2890ca94c3ddSJeremy 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$.
2891ca94c3ddSJeremy L Thompson   The `CeedOperator` must be linear and non-composite.
2892ca94c3ddSJeremy L Thompson   The associated `CeedQFunction` must therefore also be linear.
2893eaf62fffSJeremy L Thompson 
2894ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2895f04ea552SJeremy L Thompson 
2896ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to create element inverses
2897ca94c3ddSJeremy L Thompson   @param[out] fdm_inv `CeedOperator` to apply the action of a FDM based inverse for each element
2898ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2899eaf62fffSJeremy L Thompson 
2900eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2901eaf62fffSJeremy L Thompson 
2902480fae85SJeremy L Thompson   @ref User
2903eaf62fffSJeremy L Thompson **/
CeedOperatorCreateFDMElementInverse(CeedOperator op,CeedOperator * fdm_inv,CeedRequest * request)29042b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) {
29051c66c397SJeremy L Thompson   Ceed                 ceed, ceed_parent;
29061c66c397SJeremy L Thompson   bool                 interp = false, grad = false, is_tensor_basis = true;
29071c66c397SJeremy L Thompson   CeedInt              num_input_fields, P_1d, Q_1d, num_nodes, num_qpts, dim, num_comp = 1, num_elem = 1;
29081c66c397SJeremy L Thompson   CeedScalar          *mass, *laplace, *x, *fdm_interp, *lambda, *elem_avg;
29091c66c397SJeremy L Thompson   const CeedScalar    *interp_1d, *grad_1d, *q_weight_1d;
29101c66c397SJeremy L Thompson   CeedVector           q_data;
29111c66c397SJeremy L Thompson   CeedElemRestriction  rstr  = NULL, rstr_qd_i;
29121c66c397SJeremy L Thompson   CeedBasis            basis = NULL, fdm_basis;
29131c66c397SJeremy L Thompson   CeedQFunctionContext ctx_fdm;
29141c66c397SJeremy L Thompson   CeedQFunctionField  *qf_fields;
29151c66c397SJeremy L Thompson   CeedQFunction        qf, qf_fdm;
29161c66c397SJeremy L Thompson   CeedOperatorField   *op_fields;
29171c66c397SJeremy L Thompson 
29182b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2919eaf62fffSJeremy L Thompson 
2920eaf62fffSJeremy L Thompson   if (op->CreateFDMElementInverse) {
2921d04bbc78SJeremy L Thompson     // Backend version
29222b730f8bSJeremy L Thompson     CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request));
2923eaf62fffSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2924eaf62fffSJeremy L Thompson   } else {
2925d04bbc78SJeremy L Thompson     // Operator fallback
2926d04bbc78SJeremy L Thompson     CeedOperator op_fallback;
2927d04bbc78SJeremy L Thompson 
2928ca38d01dSJeremy L Thompson     CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorCreateFDMElementInverse\n");
29292b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFallback(op, &op_fallback));
2930d04bbc78SJeremy L Thompson     if (op_fallback) {
29312b730f8bSJeremy L Thompson       CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request));
2932eaf62fffSJeremy L Thompson       return CEED_ERROR_SUCCESS;
2933eaf62fffSJeremy L Thompson     }
2934eaf62fffSJeremy L Thompson   }
2935eaf62fffSJeremy L Thompson 
2936d04bbc78SJeremy L Thompson   // Default interface implementation
29372b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
2938bb229da9SJeremy L Thompson   CeedCall(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
29392b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
2940eaf62fffSJeremy L Thompson 
2941eaf62fffSJeremy L Thompson   // Determine active input basis
29422b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL));
29432b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
2944eaf62fffSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
2945eaf62fffSJeremy L Thompson     CeedVector vec;
29461c66c397SJeremy L Thompson 
29472b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec));
2948eaf62fffSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2949eaf62fffSJeremy L Thompson       CeedEvalMode eval_mode;
29501c66c397SJeremy L Thompson 
29512b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
2952eaf62fffSJeremy L Thompson       interp = interp || eval_mode == CEED_EVAL_INTERP;
2953eaf62fffSJeremy L Thompson       grad   = grad || eval_mode == CEED_EVAL_GRAD;
2954681d0ea7SJeremy L Thompson       if (!basis) CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis));
2955681d0ea7SJeremy L Thompson       if (!rstr) CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
2956eaf62fffSJeremy L Thompson     }
2957681d0ea7SJeremy L Thompson     CeedCall(CeedVectorDestroy(&vec));
2958eaf62fffSJeremy L Thompson   }
29596574a04fSJeremy L Thompson   CeedCheck(basis, ceed, CEED_ERROR_BACKEND, "No active field set");
29602b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d));
2961352a5e7cSSebastian Grimberg   CeedCall(CeedBasisGetNumNodes(basis, &num_nodes));
29622b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
29632b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
29642b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetDimension(basis, &dim));
29652b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
29662b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
2967eaf62fffSJeremy L Thompson 
2968eaf62fffSJeremy L Thompson   // Build and diagonalize 1D Mass and Laplacian
29696574a04fSJeremy L Thompson   CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis));
29706574a04fSJeremy L Thompson   CeedCheck(is_tensor_basis, ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases");
29712b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &mass));
29722b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &laplace));
29732b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &x));
29742b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp));
29752b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(P_1d, &lambda));
2976eaf62fffSJeremy L Thompson   // -- Build matrices
29772b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetInterp1D(basis, &interp_1d));
29782b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetGrad1D(basis, &grad_1d));
29792b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d));
29802b730f8bSJeremy L Thompson   CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace));
2981eaf62fffSJeremy L Thompson 
2982eaf62fffSJeremy L Thompson   // -- Diagonalize
29832b730f8bSJeremy L Thompson   CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d));
29842b730f8bSJeremy L Thompson   CeedCall(CeedFree(&mass));
29852b730f8bSJeremy L Thompson   CeedCall(CeedFree(&laplace));
29862b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < P_1d; i++) {
29872b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d];
29882b730f8bSJeremy L Thompson   }
29892b730f8bSJeremy L Thompson   CeedCall(CeedFree(&x));
2990eaf62fffSJeremy L Thompson 
29911c66c397SJeremy L Thompson   {
29921c66c397SJeremy L Thompson     CeedInt             layout[3], num_modes = (interp ? 1 : 0) + (grad ? dim : 0);
29931c66c397SJeremy L Thompson     CeedScalar          max_norm = 0;
29941c66c397SJeremy L Thompson     const CeedScalar   *assembled_array, *q_weight_array;
29951c66c397SJeremy L Thompson     CeedVector          assembled = NULL, q_weight;
2996c5f45aeaSJeremy L Thompson     CeedElemRestriction rstr_qf   = NULL;
29971c66c397SJeremy L Thompson 
29981c66c397SJeremy L Thompson     // Assemble QFunction
29992b730f8bSJeremy L Thompson     CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request));
300056c48462SJeremy L Thompson     CeedCall(CeedElemRestrictionGetELayout(rstr_qf, layout));
30012b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr_qf));
30022b730f8bSJeremy L Thompson     CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm));
3003eaf62fffSJeremy L Thompson 
3004eaf62fffSJeremy L Thompson     // Calculate element averages
30052b730f8bSJeremy L Thompson     CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight));
30062b730f8bSJeremy L Thompson     CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight));
30072b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array));
30082b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array));
30092b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_elem, &elem_avg));
3010eaf62fffSJeremy L Thompson     const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON;
30111c66c397SJeremy L Thompson 
3012eaf62fffSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
3013eaf62fffSJeremy L Thompson       CeedInt count = 0;
30141c66c397SJeremy L Thompson 
30152b730f8bSJeremy L Thompson       for (CeedInt q = 0; q < num_qpts; q++) {
30162b730f8bSJeremy L Thompson         for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) {
30172b730f8bSJeremy L Thompson           if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) {
30182b730f8bSJeremy L Thompson             elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q];
3019eaf62fffSJeremy L Thompson             count++;
3020eaf62fffSJeremy L Thompson           }
30212b730f8bSJeremy L Thompson         }
30222b730f8bSJeremy L Thompson       }
3023eaf62fffSJeremy L Thompson       if (count) {
3024eaf62fffSJeremy L Thompson         elem_avg[e] /= count;
3025eaf62fffSJeremy L Thompson       } else {
3026eaf62fffSJeremy L Thompson         elem_avg[e] = 1.0;
3027eaf62fffSJeremy L Thompson       }
3028eaf62fffSJeremy L Thompson     }
30292b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array));
30302b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&assembled));
30312b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array));
30322b730f8bSJeremy L Thompson     CeedCall(CeedVectorDestroy(&q_weight));
30331c66c397SJeremy L Thompson   }
3034eaf62fffSJeremy L Thompson 
3035eaf62fffSJeremy L Thompson   // Build FDM diagonal
30361c66c397SJeremy L Thompson   {
3037eaf62fffSJeremy L Thompson     CeedScalar *q_data_array, *fdm_diagonal;
30381c66c397SJeremy L Thompson 
3039352a5e7cSSebastian Grimberg     CeedCall(CeedCalloc(num_comp * num_nodes, &fdm_diagonal));
3040352a5e7cSSebastian Grimberg     const CeedScalar fdm_diagonal_bound = num_nodes * CEED_EPSILON;
30412b730f8bSJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
3042352a5e7cSSebastian Grimberg       for (CeedInt n = 0; n < num_nodes; n++) {
3043352a5e7cSSebastian Grimberg         if (interp) fdm_diagonal[c * num_nodes + n] = 1.0;
30442b730f8bSJeremy L Thompson         if (grad) {
3045eaf62fffSJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
3046eaf62fffSJeremy L Thompson             CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d;
3047352a5e7cSSebastian Grimberg             fdm_diagonal[c * num_nodes + n] += lambda[i];
3048eaf62fffSJeremy L Thompson           }
3049eaf62fffSJeremy L Thompson         }
3050352a5e7cSSebastian Grimberg         if (fabs(fdm_diagonal[c * num_nodes + n]) < fdm_diagonal_bound) fdm_diagonal[c * num_nodes + n] = fdm_diagonal_bound;
30512b730f8bSJeremy L Thompson       }
30522b730f8bSJeremy L Thompson     }
3053352a5e7cSSebastian Grimberg     CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * num_nodes, &q_data));
30542b730f8bSJeremy L Thompson     CeedCall(CeedVectorSetValue(q_data, 0.0));
30552b730f8bSJeremy L Thompson     CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array));
30562b730f8bSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
30572b730f8bSJeremy L Thompson       for (CeedInt c = 0; c < num_comp; c++) {
30586c10af5dSJeremy L Thompson         for (CeedInt n = 0; n < num_nodes; n++) {
30591c66c397SJeremy L Thompson           q_data_array[(e * num_comp + c) * num_nodes + n] = 1. / (elem_avg[e] * fdm_diagonal[c * num_nodes + n]);
30602b730f8bSJeremy L Thompson         }
30612b730f8bSJeremy L Thompson       }
30626c10af5dSJeremy L Thompson     }
30632b730f8bSJeremy L Thompson     CeedCall(CeedFree(&elem_avg));
30642b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_diagonal));
30652b730f8bSJeremy L Thompson     CeedCall(CeedVectorRestoreArray(q_data, &q_data_array));
30661c66c397SJeremy L Thompson   }
3067eaf62fffSJeremy L Thompson 
3068eaf62fffSJeremy L Thompson   // Setup FDM operator
3069eaf62fffSJeremy L Thompson   // -- Basis
30701c66c397SJeremy L Thompson   {
3071eaf62fffSJeremy L Thompson     CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy;
30721c66c397SJeremy L Thompson 
30732b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy));
30742b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_ref_dummy));
30752b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(P_1d, &q_weight_dummy));
30762b730f8bSJeremy 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));
30772b730f8bSJeremy L Thompson     CeedCall(CeedFree(&fdm_interp));
30782b730f8bSJeremy L Thompson     CeedCall(CeedFree(&grad_dummy));
30792b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_ref_dummy));
30802b730f8bSJeremy L Thompson     CeedCall(CeedFree(&q_weight_dummy));
30812b730f8bSJeremy L Thompson     CeedCall(CeedFree(&lambda));
30821c66c397SJeremy L Thompson   }
3083eaf62fffSJeremy L Thompson 
3084eaf62fffSJeremy L Thompson   // -- Restriction
30851c66c397SJeremy L Thompson   {
3086352a5e7cSSebastian Grimberg     CeedInt strides[3] = {1, num_nodes, num_nodes * num_comp};
30870a5597ceSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, num_nodes, num_comp,
30880a5597ceSJeremy L Thompson                                               (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_nodes, strides, &rstr_qd_i));
30891c66c397SJeremy L Thompson   }
30901c66c397SJeremy L Thompson 
3091eaf62fffSJeremy L Thompson   // -- QFunction
30922b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm));
30932b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP));
30942b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE));
30952b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP));
30962b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp));
30971c66c397SJeremy L Thompson 
3098eaf62fffSJeremy L Thompson   // -- QFunction context
30991c66c397SJeremy L Thompson   {
3100eaf62fffSJeremy L Thompson     CeedInt *num_comp_data;
31011c66c397SJeremy L Thompson 
31022b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &num_comp_data));
3103eaf62fffSJeremy L Thompson     num_comp_data[0] = num_comp;
31042b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm));
31052b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data));
31061c66c397SJeremy L Thompson   }
31072b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm));
31082b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(&ctx_fdm));
31091c66c397SJeremy L Thompson 
3110eaf62fffSJeremy L Thompson   // -- Operator
31112b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv));
31122b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
3113356036faSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_NONE, q_data));
31142b730f8bSJeremy L Thompson   CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE));
3115eaf62fffSJeremy L Thompson 
3116eaf62fffSJeremy L Thompson   // Cleanup
31179bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
31189bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed_parent));
31192b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&q_data));
3120681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr));
31212b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i));
3122681d0ea7SJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis));
3123681d0ea7SJeremy L Thompson   CeedCall(CeedBasisDestroy(&fdm_basis));
3124c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
31252b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf_fdm));
3126eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
3127eaf62fffSJeremy L Thompson }
3128eaf62fffSJeremy L Thompson 
3129eaf62fffSJeremy L Thompson /// @}
3130