13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3eaf62fffSJeremy L Thompson // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5eaf62fffSJeremy L Thompson // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7eaf62fffSJeremy L Thompson 82b730f8bSJeremy L Thompson #include <ceed-impl.h> 949aac155SJeremy L Thompson #include <ceed.h> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 11c85e8640SSebastian Grimberg #include <assert.h> 122b730f8bSJeremy L Thompson #include <math.h> 13eaf62fffSJeremy L Thompson #include <stdbool.h> 14eaf62fffSJeremy L Thompson #include <stdio.h> 15eaf62fffSJeremy L Thompson #include <string.h> 16eaf62fffSJeremy L Thompson 17eaf62fffSJeremy L Thompson /// @file 18eaf62fffSJeremy L Thompson /// Implementation of CeedOperator preconditioning interfaces 19eaf62fffSJeremy L Thompson 20eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 21eaf62fffSJeremy L Thompson /// CeedOperator Library Internal Preconditioning Functions 22eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 23eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorDeveloper 24eaf62fffSJeremy L Thompson /// @{ 25eaf62fffSJeremy L Thompson 26eaf62fffSJeremy L Thompson /** 27ca94c3ddSJeremy L Thompson @brief Duplicate a `CeedQFunction` with a reference `Ceed` to fallback for advanced `CeedOperator` functionality 289e77b9c8SJeremy L Thompson 29ca94c3ddSJeremy L Thompson @param[in] fallback_ceed `Ceed` on which to create fallback `CeedQFunction` 30ca94c3ddSJeremy L Thompson @param[in] qf `CeedQFunction` to create fallback for 31ca94c3ddSJeremy L Thompson @param[out] qf_fallback Fallback `CeedQFunction` 329e77b9c8SJeremy L Thompson 339e77b9c8SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 349e77b9c8SJeremy L Thompson 359e77b9c8SJeremy L Thompson @ref Developer 369e77b9c8SJeremy L Thompson **/ 372b730f8bSJeremy L Thompson static int CeedQFunctionCreateFallback(Ceed fallback_ceed, CeedQFunction qf, CeedQFunction *qf_fallback) { 381c66c397SJeremy L Thompson char *source_path_with_name = NULL; 391203703bSJeremy L Thompson CeedInt num_input_fields, num_output_fields; 401203703bSJeremy L Thompson Ceed ceed; 411203703bSJeremy L Thompson CeedQFunctionField *input_fields, *output_fields; 421c66c397SJeremy L Thompson 439e77b9c8SJeremy L Thompson // Check if NULL qf passed in 449e77b9c8SJeremy L Thompson if (!qf) return CEED_ERROR_SUCCESS; 459e77b9c8SJeremy L Thompson 461203703bSJeremy L Thompson CeedCall(CeedQFunctionGetCeed(qf, &ceed)); 471203703bSJeremy L Thompson CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n"); 481203703bSJeremy L Thompson CeedDebug(ceed, "Creating fallback CeedQFunction\n"); 49d04bbc78SJeremy L Thompson 509e77b9c8SJeremy L Thompson if (qf->source_path) { 512b730f8bSJeremy L Thompson size_t path_len = strlen(qf->source_path), name_len = strlen(qf->kernel_name); 522b730f8bSJeremy L Thompson CeedCall(CeedCalloc(path_len + name_len + 2, &source_path_with_name)); 539e77b9c8SJeremy L Thompson memcpy(source_path_with_name, qf->source_path, path_len); 549e77b9c8SJeremy L Thompson memcpy(&source_path_with_name[path_len], ":", 1); 559e77b9c8SJeremy L Thompson memcpy(&source_path_with_name[path_len + 1], qf->kernel_name, name_len); 569e77b9c8SJeremy L Thompson } else { 572b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &source_path_with_name)); 589e77b9c8SJeremy L Thompson } 599e77b9c8SJeremy L Thompson 601203703bSJeremy L Thompson { 611203703bSJeremy L Thompson CeedInt vec_length; 621203703bSJeremy L Thompson CeedQFunctionUser f; 631203703bSJeremy L Thompson 641203703bSJeremy L Thompson CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length)); 651203703bSJeremy L Thompson CeedCall(CeedQFunctionGetUserFunction(qf, &f)); 661203703bSJeremy L Thompson CeedCall(CeedQFunctionCreateInterior(fallback_ceed, vec_length, f, source_path_with_name, qf_fallback)); 671203703bSJeremy L Thompson } 689e77b9c8SJeremy L Thompson { 699e77b9c8SJeremy L Thompson CeedQFunctionContext ctx; 709e77b9c8SJeremy L Thompson 712b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetContext(qf, &ctx)); 722b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx)); 739e77b9c8SJeremy L Thompson } 741203703bSJeremy L Thompson CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 751203703bSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 766f8994e9SJeremy L Thompson const char *field_name; 771203703bSJeremy L Thompson CeedInt size; 781203703bSJeremy L Thompson CeedEvalMode eval_mode; 791203703bSJeremy L Thompson 80ab747706SJeremy L Thompson CeedCall(CeedQFunctionFieldGetData(input_fields[i], &field_name, &size, &eval_mode)); 811203703bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(*qf_fallback, field_name, size, eval_mode)); 829e77b9c8SJeremy L Thompson } 831203703bSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 846f8994e9SJeremy L Thompson const char *field_name; 851203703bSJeremy L Thompson CeedInt size; 861203703bSJeremy L Thompson CeedEvalMode eval_mode; 871203703bSJeremy L Thompson 88ab747706SJeremy L Thompson CeedCall(CeedQFunctionFieldGetData(output_fields[i], &field_name, &size, &eval_mode)); 891203703bSJeremy L Thompson CeedCall(CeedQFunctionAddOutput(*qf_fallback, field_name, size, eval_mode)); 909e77b9c8SJeremy L Thompson } 912b730f8bSJeremy L Thompson CeedCall(CeedFree(&source_path_with_name)); 929e77b9c8SJeremy L Thompson return CEED_ERROR_SUCCESS; 939e77b9c8SJeremy L Thompson } 949e77b9c8SJeremy L Thompson 959e77b9c8SJeremy L Thompson /** 96ca94c3ddSJeremy L Thompson @brief Duplicate a `CeedOperator` with a reference `Ceed` to fallback for advanced `CeedOperator` functionality 97eaf62fffSJeremy L Thompson 98ca94c3ddSJeremy L Thompson @param[in,out] op `CeedOperator` to create fallback for 99eaf62fffSJeremy L Thompson 100eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 101eaf62fffSJeremy L Thompson 102eaf62fffSJeremy L Thompson @ref Developer 103eaf62fffSJeremy L Thompson **/ 104d04bbc78SJeremy L Thompson static int CeedOperatorCreateFallback(CeedOperator op) { 1051c66c397SJeremy L Thompson bool is_composite; 1061203703bSJeremy L Thompson Ceed ceed, ceed_fallback; 1071c66c397SJeremy L Thompson CeedOperator op_fallback; 108eaf62fffSJeremy L Thompson 109805fe78eSJeremy L Thompson // Check not already created 110805fe78eSJeremy L Thompson if (op->op_fallback) return CEED_ERROR_SUCCESS; 111805fe78eSJeremy L Thompson 112eaf62fffSJeremy L Thompson // Fallback Ceed 1131203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 1141203703bSJeremy L Thompson CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback)); 115d04bbc78SJeremy L Thompson if (!ceed_fallback) return CEED_ERROR_SUCCESS; 116d04bbc78SJeremy L Thompson 1171203703bSJeremy L Thompson CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n"); 1181203703bSJeremy L Thompson CeedDebug(ceed, "Creating fallback CeedOperator\n"); 119eaf62fffSJeremy L Thompson 120eaf62fffSJeremy L Thompson // Clone Op 121b275c451SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 122b275c451SJeremy L Thompson if (is_composite) { 123b275c451SJeremy L Thompson CeedInt num_suboperators; 124b275c451SJeremy L Thompson CeedOperator *sub_operators; 125b275c451SJeremy L Thompson 1262b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorCreate(ceed_fallback, &op_fallback)); 127b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 128b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 129b275c451SJeremy L Thompson for (CeedInt i = 0; i < num_suboperators; i++) { 130d04bbc78SJeremy L Thompson CeedOperator op_sub_fallback; 131d04bbc78SJeremy L Thompson 132b275c451SJeremy L Thompson CeedCall(CeedOperatorGetFallback(sub_operators[i], &op_sub_fallback)); 1332b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorAddSub(op_fallback, op_sub_fallback)); 134805fe78eSJeremy L Thompson } 135805fe78eSJeremy L Thompson } else { 1361203703bSJeremy L Thompson CeedInt num_input_fields, num_output_fields; 1379e77b9c8SJeremy L Thompson CeedQFunction qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL; 1381203703bSJeremy L Thompson CeedOperatorField *input_fields, *output_fields; 1391c66c397SJeremy L Thompson 1402b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback)); 1412b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback)); 1422b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback)); 1432b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback)); 1441203703bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 1451203703bSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1466f8994e9SJeremy L Thompson const char *field_name; 1471203703bSJeremy L Thompson CeedVector vec; 1481203703bSJeremy L Thompson CeedElemRestriction rstr; 1491203703bSJeremy L Thompson CeedBasis basis; 1501203703bSJeremy L Thompson 151ab747706SJeremy L Thompson CeedCall(CeedOperatorFieldGetData(input_fields[i], &field_name, &rstr, &basis, &vec)); 1521203703bSJeremy L Thompson CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec)); 153805fe78eSJeremy L Thompson } 1541203703bSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1556f8994e9SJeremy L Thompson const char *field_name; 1561203703bSJeremy L Thompson CeedVector vec; 1571203703bSJeremy L Thompson CeedElemRestriction rstr; 1581203703bSJeremy L Thompson CeedBasis basis; 1591203703bSJeremy L Thompson 160ab747706SJeremy L Thompson CeedCall(CeedOperatorFieldGetData(output_fields[i], &field_name, &rstr, &basis, &vec)); 1611203703bSJeremy L Thompson CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec)); 162805fe78eSJeremy L Thompson } 1632b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op->qf_assembled, &op_fallback->qf_assembled)); 1649e77b9c8SJeremy L Thompson // Cleanup 1652b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&qf_fallback)); 1662b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&dqf_fallback)); 1672b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&dqfT_fallback)); 168805fe78eSJeremy L Thompson } 1692b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetName(op_fallback, op->name)); 1702b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fallback)); 171b05f7e9fSJeremy L Thompson // Note: No ref-counting here so we don't get caught in a reference loop. 172b05f7e9fSJeremy L Thompson // The op holds the only reference to op_fallback and is responsible for deleting itself and op_fallback. 173805fe78eSJeremy L Thompson op->op_fallback = op_fallback; 174b05f7e9fSJeremy L Thompson op_fallback->op_fallback_parent = op; 175eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 176eaf62fffSJeremy L Thompson } 177eaf62fffSJeremy L Thompson 178eaf62fffSJeremy L Thompson /** 179eaf62fffSJeremy L Thompson @brief Core logic for assembling operator diagonal or point block diagonal 180eaf62fffSJeremy L Thompson 181ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble point block diagonal 182ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 183bd83916cSSebastian Grimberg @param[in] is_point_block Boolean flag to assemble diagonal or point block diagonal 184ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled diagonal 185eaf62fffSJeremy L Thompson 186eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 187eaf62fffSJeremy L Thompson 188eaf62fffSJeremy L Thompson @ref Developer 189eaf62fffSJeremy L Thompson **/ 190*f3bd9308SJeremy L Thompson static inline int CeedSingleOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block, 191*f3bd9308SJeremy L Thompson CeedVector assembled) { 192eaf62fffSJeremy L Thompson Ceed ceed; 193506b1a0cSSebastian Grimberg bool is_composite; 194506b1a0cSSebastian Grimberg 195506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetCeed(op, &ceed)); 196506b1a0cSSebastian Grimberg CeedCall(CeedOperatorIsComposite(op, &is_composite)); 197506b1a0cSSebastian Grimberg CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 198506b1a0cSSebastian Grimberg 199506b1a0cSSebastian Grimberg // Assemble QFunction 200506b1a0cSSebastian Grimberg CeedInt layout_qf[3]; 201437c7c90SJeremy L Thompson const CeedScalar *assembled_qf_array; 202c5f45aeaSJeremy L Thompson CeedVector assembled_qf = NULL; 203c5f45aeaSJeremy L Thompson CeedElemRestriction assembled_elem_rstr = NULL; 204437c7c90SJeremy L Thompson 205437c7c90SJeremy L Thompson CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request)); 20656c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf)); 207437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr)); 208437c7c90SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array)); 209eaf62fffSJeremy L Thompson 210ed9e99e6SJeremy L Thompson // Get assembly data 211437c7c90SJeremy L Thompson const CeedEvalMode **eval_modes_in, **eval_modes_out; 212506b1a0cSSebastian Grimberg CeedInt num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out; 213437c7c90SJeremy L Thompson CeedSize **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components; 214506b1a0cSSebastian Grimberg CeedBasis *active_bases_in, *active_bases_out; 215506b1a0cSSebastian Grimberg CeedElemRestriction *active_elem_rstrs_in, *active_elem_rstrs_out; 2161c66c397SJeremy L Thompson CeedOperatorAssemblyData data; 2171c66c397SJeremy L Thompson 218437c7c90SJeremy L Thompson CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data)); 219506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in, 220506b1a0cSSebastian Grimberg &num_active_bases_out, &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out, 221506b1a0cSSebastian Grimberg &num_output_components)); 222506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, NULL, NULL, &active_bases_out, NULL)); 223506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs_in, NULL, &active_elem_rstrs_out)); 224506b1a0cSSebastian Grimberg 225934a29f5SSebastian Grimberg // Loop over all active bases (find matching input/output pairs) 226934a29f5SSebastian Grimberg for (CeedInt b = 0; b < CeedIntMin(num_active_bases_in, num_active_bases_out); b++) { 227934a29f5SSebastian Grimberg CeedInt b_in, b_out, num_elem, num_nodes, num_qpts, num_comp; 2281c66c397SJeremy L Thompson bool has_eval_none = false; 2291c66c397SJeremy L Thompson CeedScalar *elem_diag_array, *identity = NULL; 2301c66c397SJeremy L Thompson CeedVector elem_diag; 2317c1dbaffSSebastian Grimberg CeedElemRestriction diag_elem_rstr; 2321c66c397SJeremy L Thompson 233934a29f5SSebastian Grimberg if (num_active_bases_in <= num_active_bases_out) { 234934a29f5SSebastian Grimberg b_in = b; 235934a29f5SSebastian Grimberg for (b_out = 0; b_out < num_active_bases_out; b_out++) { 236934a29f5SSebastian Grimberg if (active_bases_in[b_in] == active_bases_out[b_out]) { 237934a29f5SSebastian Grimberg break; 238934a29f5SSebastian Grimberg } 239934a29f5SSebastian Grimberg } 240934a29f5SSebastian Grimberg if (b_out == num_active_bases_out) { 241934a29f5SSebastian Grimberg continue; 242934a29f5SSebastian Grimberg } // No matching output basis found 243934a29f5SSebastian Grimberg } else { 244934a29f5SSebastian Grimberg b_out = b; 245934a29f5SSebastian Grimberg for (b_in = 0; b_in < num_active_bases_in; b_in++) { 246934a29f5SSebastian Grimberg if (active_bases_in[b_in] == active_bases_out[b_out]) { 247934a29f5SSebastian Grimberg break; 248934a29f5SSebastian Grimberg } 249934a29f5SSebastian Grimberg } 250934a29f5SSebastian Grimberg if (b_in == num_active_bases_in) { 251934a29f5SSebastian Grimberg continue; 252934a29f5SSebastian Grimberg } // No matching output basis found 253934a29f5SSebastian Grimberg } 254934a29f5SSebastian Grimberg CeedCheck(active_elem_rstrs_in[b_in] == active_elem_rstrs_out[b_out], ceed, CEED_ERROR_UNSUPPORTED, 255506b1a0cSSebastian Grimberg "Cannot assemble operator diagonal with different input and output active element restrictions"); 256506b1a0cSSebastian Grimberg 2571c66c397SJeremy L Thompson // Assemble point block diagonal restriction, if needed 258bd83916cSSebastian Grimberg if (is_point_block) { 259934a29f5SSebastian Grimberg CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstrs_in[b_in], &diag_elem_rstr)); 2607c1dbaffSSebastian Grimberg } else { 261934a29f5SSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnsignedCopy(active_elem_rstrs_in[b_in], &diag_elem_rstr)); 262eaf62fffSJeremy L Thompson } 263eaf62fffSJeremy L Thompson 264eaf62fffSJeremy L Thompson // Create diagonal vector 265437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag)); 266eaf62fffSJeremy L Thompson 267eaf62fffSJeremy L Thompson // Assemble element operator diagonals 2682b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(elem_diag, 0.0)); 2692b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array)); 270437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem)); 271934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumNodes(active_bases_in[b_in], &num_nodes)); 272934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumComponents(active_bases_in[b_in], &num_comp)); 273934a29f5SSebastian Grimberg if (active_bases_in[b_in] == CEED_BASIS_NONE) num_qpts = num_nodes; 274934a29f5SSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(active_bases_in[b_in], &num_qpts)); 275ed9e99e6SJeremy L Thompson 276352a5e7cSSebastian Grimberg // Construct identity matrix for basis if required 277934a29f5SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in[b_in]; i++) { 278934a29f5SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_in[b_in][i] == CEED_EVAL_NONE); 279ed9e99e6SJeremy L Thompson } 280934a29f5SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out[b_out]; i++) { 281934a29f5SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_out[b_out][i] == CEED_EVAL_NONE); 282ed9e99e6SJeremy L Thompson } 283ed9e99e6SJeremy L Thompson if (has_eval_none) { 2842b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_qpts * num_nodes, &identity)); 2852b730f8bSJeremy L Thompson for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0; 286eaf62fffSJeremy L Thompson } 287352a5e7cSSebastian Grimberg 288eaf62fffSJeremy L Thompson // Compute the diagonal of B^T D B 289eaf62fffSJeremy L Thompson // Each element 290b94338b9SJed Brown for (CeedSize e = 0; e < num_elem; e++) { 291eaf62fffSJeremy L Thompson // Each basis eval mode pair 292352a5e7cSSebastian Grimberg CeedInt d_out = 0, q_comp_out; 293352a5e7cSSebastian Grimberg CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE; 2941c66c397SJeremy L Thompson 295934a29f5SSebastian Grimberg for (CeedInt e_out = 0; e_out < num_eval_modes_out[b_out]; e_out++) { 2961c66c397SJeremy L Thompson CeedInt d_in = 0, q_comp_in; 297437c7c90SJeremy L Thompson const CeedScalar *B_t = NULL; 2981c66c397SJeremy L Thompson CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE; 2991c66c397SJeremy L Thompson 300934a29f5SSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(active_bases_out[b_out], eval_modes_out[b_out][e_out], identity, &B_t)); 301934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_out[b_out], eval_modes_out[b_out][e_out], &q_comp_out)); 302352a5e7cSSebastian Grimberg if (q_comp_out > 1) { 303934a29f5SSebastian Grimberg if (e_out == 0 || eval_modes_out[b_out][e_out] != eval_mode_out_prev) d_out = 0; 304352a5e7cSSebastian Grimberg else B_t = &B_t[(++d_out) * num_qpts * num_nodes]; 305352a5e7cSSebastian Grimberg } 306934a29f5SSebastian Grimberg eval_mode_out_prev = eval_modes_out[b_out][e_out]; 307352a5e7cSSebastian Grimberg 308934a29f5SSebastian Grimberg for (CeedInt e_in = 0; e_in < num_eval_modes_in[b_in]; e_in++) { 309437c7c90SJeremy L Thompson const CeedScalar *B = NULL; 3101c66c397SJeremy L Thompson 311934a29f5SSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(active_bases_in[b_in], eval_modes_in[b_in][e_in], identity, &B)); 312934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_in[b_in], eval_modes_in[b_in][e_in], &q_comp_in)); 313352a5e7cSSebastian Grimberg if (q_comp_in > 1) { 314934a29f5SSebastian Grimberg if (e_in == 0 || eval_modes_in[b_in][e_in] != eval_mode_in_prev) d_in = 0; 315352a5e7cSSebastian Grimberg else B = &B[(++d_in) * num_qpts * num_nodes]; 316352a5e7cSSebastian Grimberg } 317934a29f5SSebastian Grimberg eval_mode_in_prev = eval_modes_in[b_in][e_in]; 318352a5e7cSSebastian Grimberg 319eaf62fffSJeremy L Thompson // Each component 320506b1a0cSSebastian Grimberg for (CeedInt c_out = 0; c_out < num_comp; c_out++) { 321437c7c90SJeremy L Thompson // Each qpt/node pair 3222b730f8bSJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 323bd83916cSSebastian Grimberg if (is_point_block) { 324eaf62fffSJeremy L Thompson // Point Block Diagonal 325506b1a0cSSebastian Grimberg for (CeedInt c_in = 0; c_in < num_comp; c_in++) { 326934a29f5SSebastian Grimberg const CeedSize c_offset = 327934a29f5SSebastian Grimberg (eval_mode_offsets_in[b_in][e_in] + c_in) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out; 328506b1a0cSSebastian Grimberg const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]]; 3291c66c397SJeremy L Thompson 3302b730f8bSJeremy L Thompson for (CeedInt n = 0; n < num_nodes; n++) { 331506b1a0cSSebastian Grimberg elem_diag_array[((e * num_comp + c_out) * num_comp + c_in) * num_nodes + n] += 332437c7c90SJeremy L Thompson B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n]; 333eaf62fffSJeremy L Thompson } 3342b730f8bSJeremy L Thompson } 335eaf62fffSJeremy L Thompson } else { 336eaf62fffSJeremy L Thompson // Diagonal Only 337934a29f5SSebastian Grimberg const CeedInt c_offset = 338934a29f5SSebastian Grimberg (eval_mode_offsets_in[b_in][e_in] + c_out) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out; 339506b1a0cSSebastian Grimberg const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]]; 3401c66c397SJeremy L Thompson 3412b730f8bSJeremy L Thompson for (CeedInt n = 0; n < num_nodes; n++) { 342506b1a0cSSebastian Grimberg elem_diag_array[(e * num_comp + c_out) * num_nodes + n] += B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n]; 343eaf62fffSJeremy L Thompson } 344eaf62fffSJeremy L Thompson } 345eaf62fffSJeremy L Thompson } 346eaf62fffSJeremy L Thompson } 3472b730f8bSJeremy L Thompson } 3482b730f8bSJeremy L Thompson } 3492b730f8bSJeremy L Thompson } 3502b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array)); 351eaf62fffSJeremy L Thompson 352eaf62fffSJeremy L Thompson // Assemble local operator diagonal 3537c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionApply(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request)); 354eaf62fffSJeremy L Thompson 355eaf62fffSJeremy L Thompson // Cleanup 3567c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr)); 3572b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&elem_diag)); 3582b730f8bSJeremy L Thompson CeedCall(CeedFree(&identity)); 359437c7c90SJeremy L Thompson } 360437c7c90SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 361437c7c90SJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled_qf)); 362eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 363eaf62fffSJeremy L Thompson } 364eaf62fffSJeremy L Thompson 365eaf62fffSJeremy L Thompson /** 366eaf62fffSJeremy L Thompson @brief Core logic for assembling composite operator diagonal 367eaf62fffSJeremy L Thompson 368ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble point block diagonal 369ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 370bd83916cSSebastian Grimberg @param[in] is_point_block Boolean flag to assemble diagonal or point block diagonal 371ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled diagonal 372eaf62fffSJeremy L Thompson 373eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 374eaf62fffSJeremy L Thompson 375eaf62fffSJeremy L Thompson @ref Developer 376eaf62fffSJeremy L Thompson **/ 377bd83916cSSebastian Grimberg static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block, 378eaf62fffSJeremy L Thompson CeedVector assembled) { 379eaf62fffSJeremy L Thompson CeedInt num_sub; 380eaf62fffSJeremy L Thompson CeedOperator *suboperators; 3811c66c397SJeremy L Thompson 382c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); 383c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators)); 384eaf62fffSJeremy L Thompson for (CeedInt i = 0; i < num_sub; i++) { 385bd83916cSSebastian Grimberg if (is_point_block) { 3862b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request)); 3876aa95790SJeremy L Thompson } else { 3882b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request)); 3896aa95790SJeremy L Thompson } 390eaf62fffSJeremy L Thompson } 391eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 392eaf62fffSJeremy L Thompson } 393eaf62fffSJeremy L Thompson 394eaf62fffSJeremy L Thompson /** 395ca94c3ddSJeremy L Thompson @brief Build nonzero pattern for non-composite CeedOperator`. 396eaf62fffSJeremy L Thompson 397ca94c3ddSJeremy L Thompson Users should generally use @ref CeedOperatorLinearAssembleSymbolic(). 398eaf62fffSJeremy L Thompson 399ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble nonzero pattern 400eaf62fffSJeremy L Thompson @param[in] offset Offset for number of entries 401eaf62fffSJeremy L Thompson @param[out] rows Row number for each entry 402eaf62fffSJeremy L Thompson @param[out] cols Column number for each entry 403eaf62fffSJeremy L Thompson 404eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 405eaf62fffSJeremy L Thompson 406eaf62fffSJeremy L Thompson @ref Developer 407eaf62fffSJeremy L Thompson **/ 4082b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) { 409f3d47e36SJeremy L Thompson Ceed ceed; 410f3d47e36SJeremy L Thompson bool is_composite; 41181670346SSebastian Grimberg CeedSize num_nodes_in, num_nodes_out, local_num_entries, count = 0; 412506b1a0cSSebastian Grimberg CeedInt num_elem_in, elem_size_in, num_comp_in, layout_er_in[3]; 41381670346SSebastian Grimberg CeedInt num_elem_out, elem_size_out, num_comp_out, layout_er_out[3]; 4141c66c397SJeremy L Thompson CeedScalar *array; 415506b1a0cSSebastian Grimberg const CeedScalar *elem_dof_a_in, *elem_dof_a_out; 416506b1a0cSSebastian Grimberg CeedVector index_vec_in, index_vec_out, elem_dof_in, elem_dof_out; 417506b1a0cSSebastian Grimberg CeedElemRestriction elem_rstr_in, elem_rstr_out, index_elem_rstr_in, index_elem_rstr_out; 4181c66c397SJeremy L Thompson 419f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 420f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 4216574a04fSJeremy L Thompson CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 422eaf62fffSJeremy L Thompson 423506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes_in, &num_nodes_out)); 424506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out)); 425506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in)); 426506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in)); 427506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in)); 42856c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(elem_rstr_in, layout_er_in)); 429eaf62fffSJeremy L Thompson 430506b1a0cSSebastian Grimberg // Determine elem_dof relation for input 431506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_nodes_in, &index_vec_in)); 432506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayWrite(index_vec_in, CEED_MEM_HOST, &array)); 433506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_nodes_in; i++) array[i] = i; 434506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArray(index_vec_in, &array)); 435506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_elem_in * elem_size_in * num_comp_in, &elem_dof_in)); 436506b1a0cSSebastian Grimberg CeedCall(CeedVectorSetValue(elem_dof_in, 0.0)); 437506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_in, &index_elem_rstr_in)); 438506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionApply(index_elem_rstr_in, CEED_NOTRANSPOSE, index_vec_in, elem_dof_in, CEED_REQUEST_IMMEDIATE)); 439506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayRead(elem_dof_in, CEED_MEM_HOST, &elem_dof_a_in)); 440506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&index_vec_in)); 441506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_in)); 442506b1a0cSSebastian Grimberg 443506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 444506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out)); 445506b1a0cSSebastian Grimberg CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, 446506b1a0cSSebastian Grimberg "Active input and output operator restrictions must have the same number of elements"); 447506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out)); 448506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out)); 44956c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(elem_rstr_out, layout_er_out)); 450506b1a0cSSebastian Grimberg 451506b1a0cSSebastian Grimberg // Determine elem_dof relation for output 452506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_nodes_out, &index_vec_out)); 453506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayWrite(index_vec_out, CEED_MEM_HOST, &array)); 454506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_nodes_out; i++) array[i] = i; 455506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArray(index_vec_out, &array)); 456506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_elem_out * elem_size_out * num_comp_out, &elem_dof_out)); 457506b1a0cSSebastian Grimberg CeedCall(CeedVectorSetValue(elem_dof_out, 0.0)); 458506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_out, &index_elem_rstr_out)); 459506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionApply(index_elem_rstr_out, CEED_NOTRANSPOSE, index_vec_out, elem_dof_out, CEED_REQUEST_IMMEDIATE)); 460506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayRead(elem_dof_out, CEED_MEM_HOST, &elem_dof_a_out)); 461506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&index_vec_out)); 462506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_out)); 463506b1a0cSSebastian Grimberg } else { 464506b1a0cSSebastian Grimberg num_elem_out = num_elem_in; 465506b1a0cSSebastian Grimberg elem_size_out = elem_size_in; 466506b1a0cSSebastian Grimberg num_comp_out = num_comp_in; 467506b1a0cSSebastian Grimberg layout_er_out[0] = layout_er_in[0]; 468506b1a0cSSebastian Grimberg layout_er_out[1] = layout_er_in[1]; 469506b1a0cSSebastian Grimberg layout_er_out[2] = layout_er_in[2]; 470506b1a0cSSebastian Grimberg elem_dof_a_out = elem_dof_a_in; 471506b1a0cSSebastian Grimberg } 472506b1a0cSSebastian Grimberg local_num_entries = elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in; 473eaf62fffSJeremy L Thompson 474eaf62fffSJeremy L Thompson // Determine i, j locations for element matrices 475506b1a0cSSebastian Grimberg for (CeedInt e = 0; e < num_elem_in; e++) { 476506b1a0cSSebastian Grimberg for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) { 477506b1a0cSSebastian Grimberg for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) { 478506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 479506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 480506b1a0cSSebastian Grimberg const CeedInt elem_dof_index_row = i * layout_er_out[0] + comp_out * layout_er_out[1] + e * layout_er_out[2]; 481506b1a0cSSebastian Grimberg const CeedInt elem_dof_index_col = j * layout_er_in[0] + comp_in * layout_er_in[1] + e * layout_er_in[2]; 482506b1a0cSSebastian Grimberg const CeedInt row = elem_dof_a_out[elem_dof_index_row]; 483506b1a0cSSebastian Grimberg const CeedInt col = elem_dof_a_in[elem_dof_index_col]; 484eaf62fffSJeremy L Thompson 485eaf62fffSJeremy L Thompson rows[offset + count] = row; 486eaf62fffSJeremy L Thompson cols[offset + count] = col; 487eaf62fffSJeremy L Thompson count++; 488eaf62fffSJeremy L Thompson } 489eaf62fffSJeremy L Thompson } 490eaf62fffSJeremy L Thompson } 491eaf62fffSJeremy L Thompson } 492eaf62fffSJeremy L Thompson } 4936574a04fSJeremy L Thompson CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing assembled entries"); 494506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArrayRead(elem_dof_in, &elem_dof_a_in)); 495506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&elem_dof_in)); 496506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 497506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArrayRead(elem_dof_out, &elem_dof_a_out)); 498506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&elem_dof_out)); 499506b1a0cSSebastian Grimberg } 500eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 501eaf62fffSJeremy L Thompson } 502eaf62fffSJeremy L Thompson 503eaf62fffSJeremy L Thompson /** 504ca94c3ddSJeremy L Thompson @brief Assemble nonzero entries for non-composite `CeedOperator`. 505eaf62fffSJeremy L Thompson 506ca94c3ddSJeremy L Thompson Users should generally use @ref CeedOperatorLinearAssemble(). 507eaf62fffSJeremy L Thompson 508ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 509ea61e9acSJeremy L Thompson @param[in] offset Offset for number of entries 510eaf62fffSJeremy L Thompson @param[out] values Values to assemble into matrix 511eaf62fffSJeremy L Thompson 512eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 513eaf62fffSJeremy L Thompson 514eaf62fffSJeremy L Thompson @ref Developer 515eaf62fffSJeremy L Thompson **/ 5162b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) { 517f3d47e36SJeremy L Thompson Ceed ceed; 518f3d47e36SJeremy L Thompson bool is_composite; 5191c66c397SJeremy L Thompson 520f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 521f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 5226574a04fSJeremy L Thompson CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 523f3d47e36SJeremy L Thompson 524f3d47e36SJeremy L Thompson // Early exit for empty operator 525f3d47e36SJeremy L Thompson { 526f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 527f3d47e36SJeremy L Thompson 528f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 529f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 530f3d47e36SJeremy L Thompson } 531eaf62fffSJeremy L Thompson 532cefa2673SJeremy L Thompson if (op->LinearAssembleSingle) { 533cefa2673SJeremy L Thompson // Backend version 5342b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleSingle(op, offset, values)); 535cefa2673SJeremy L Thompson return CEED_ERROR_SUCCESS; 536cefa2673SJeremy L Thompson } else { 537cefa2673SJeremy L Thompson // Operator fallback 538cefa2673SJeremy L Thompson CeedOperator op_fallback; 539cefa2673SJeremy L Thompson 5402b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 541cefa2673SJeremy L Thompson if (op_fallback) { 5422b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values)); 543cefa2673SJeremy L Thompson return CEED_ERROR_SUCCESS; 544cefa2673SJeremy L Thompson } 545cefa2673SJeremy L Thompson } 546cefa2673SJeremy L Thompson 547eaf62fffSJeremy L Thompson // Assemble QFunction 548506b1a0cSSebastian Grimberg CeedInt layout_qf[3]; 5491c66c397SJeremy L Thompson const CeedScalar *assembled_qf_array; 550c5f45aeaSJeremy L Thompson CeedVector assembled_qf = NULL; 551506b1a0cSSebastian Grimberg CeedElemRestriction assembled_elem_rstr = NULL; 552eaf62fffSJeremy L Thompson 553506b1a0cSSebastian Grimberg CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, CEED_REQUEST_IMMEDIATE)); 55456c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf)); 555506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr)); 556506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array)); 557eaf62fffSJeremy L Thompson 558ed9e99e6SJeremy L Thompson // Get assembly data 559506b1a0cSSebastian Grimberg CeedInt num_elem_in, elem_size_in, num_comp_in, num_qpts_in; 56081670346SSebastian Grimberg CeedInt num_elem_out, elem_size_out, num_comp_out, num_qpts_out; 56181670346SSebastian Grimberg CeedSize local_num_entries, count = 0; 562506b1a0cSSebastian Grimberg const CeedEvalMode **eval_modes_in, **eval_modes_out; 563506b1a0cSSebastian Grimberg CeedInt num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out; 564506b1a0cSSebastian Grimberg CeedBasis *active_bases_in, *active_bases_out, basis_in, basis_out; 565506b1a0cSSebastian Grimberg const CeedScalar **B_mats_in, **B_mats_out, *B_mat_in, *B_mat_out; 566506b1a0cSSebastian Grimberg CeedElemRestriction elem_rstr_in, elem_rstr_out; 567506b1a0cSSebastian Grimberg CeedRestrictionType elem_rstr_type_in, elem_rstr_type_out; 568506b1a0cSSebastian Grimberg const bool *elem_rstr_orients_in = NULL, *elem_rstr_orients_out = NULL; 569506b1a0cSSebastian Grimberg const CeedInt8 *elem_rstr_curl_orients_in = NULL, *elem_rstr_curl_orients_out = NULL; 570506b1a0cSSebastian Grimberg CeedOperatorAssemblyData data; 571eaf62fffSJeremy L Thompson 572506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data)); 573506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, NULL, &num_active_bases_out, 574506b1a0cSSebastian Grimberg &num_eval_modes_out, &eval_modes_out, NULL, NULL)); 575506b1a0cSSebastian Grimberg 576506b1a0cSSebastian Grimberg CeedCheck(num_active_bases_in == num_active_bases_out && num_active_bases_in == 1, ceed, CEED_ERROR_UNSUPPORTED, 577506b1a0cSSebastian Grimberg "Cannot assemble operator with multiple active bases"); 5786574a04fSJeremy L Thompson CeedCheck(num_eval_modes_in[0] > 0 && num_eval_modes_out[0] > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs"); 579eaf62fffSJeremy L Thompson 580506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, &B_mats_in, NULL, &active_bases_out, &B_mats_out)); 581506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out)); 582506b1a0cSSebastian Grimberg basis_in = active_bases_in[0]; 583506b1a0cSSebastian Grimberg basis_out = active_bases_out[0]; 584506b1a0cSSebastian Grimberg B_mat_in = B_mats_in[0]; 585506b1a0cSSebastian Grimberg B_mat_out = B_mats_out[0]; 586eaf62fffSJeremy L Thompson 587506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in)); 588506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in)); 589506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in)); 590506b1a0cSSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in; 591506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in)); 592506b1a0cSSebastian Grimberg 593506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetType(elem_rstr_in, &elem_rstr_type_in)); 594506b1a0cSSebastian Grimberg if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) { 595506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_orients_in)); 596506b1a0cSSebastian Grimberg } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 597506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_curl_orients_in)); 5987c1dbaffSSebastian Grimberg } 5997c1dbaffSSebastian Grimberg 600506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 601506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out)); 602506b1a0cSSebastian Grimberg CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, 603506b1a0cSSebastian Grimberg "Active input and output operator restrictions must have the same number of elements"); 604506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out)); 605506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out)); 606506b1a0cSSebastian Grimberg if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out; 607506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out)); 608506b1a0cSSebastian Grimberg CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED, 609506b1a0cSSebastian Grimberg "Active input and output bases must have the same number of quadrature points"); 610eaf62fffSJeremy L Thompson 611506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetType(elem_rstr_out, &elem_rstr_type_out)); 612506b1a0cSSebastian Grimberg if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) { 613506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_orients_out)); 614506b1a0cSSebastian Grimberg } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 615506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_curl_orients_out)); 616506b1a0cSSebastian Grimberg } 617506b1a0cSSebastian Grimberg } else { 618506b1a0cSSebastian Grimberg num_elem_out = num_elem_in; 619506b1a0cSSebastian Grimberg elem_size_out = elem_size_in; 620506b1a0cSSebastian Grimberg num_comp_out = num_comp_in; 621506b1a0cSSebastian Grimberg num_qpts_out = num_qpts_in; 622506b1a0cSSebastian Grimberg 623506b1a0cSSebastian Grimberg elem_rstr_orients_out = elem_rstr_orients_in; 624506b1a0cSSebastian Grimberg elem_rstr_curl_orients_out = elem_rstr_curl_orients_in; 625506b1a0cSSebastian Grimberg } 626506b1a0cSSebastian Grimberg local_num_entries = elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in; 627506b1a0cSSebastian Grimberg 628506b1a0cSSebastian Grimberg // Loop over elements and put in data structure 6297c1dbaffSSebastian Grimberg // We store B_mat_in, B_mat_out, BTD, elem_mat in row-major order 6300459ebd3SSebastian Grimberg CeedTensorContract contract; 631123d890dSSebastian Grimberg CeedScalar *vals, *BTD_mat = NULL, *elem_mat = NULL, *elem_mat_b = NULL; 632506b1a0cSSebastian Grimberg 633c22497adSSebastian Grimberg CeedCall(CeedBasisGetTensorContract(basis_in, &contract)); 634123d890dSSebastian Grimberg CeedCall(CeedCalloc(elem_size_out * num_qpts_in * num_eval_modes_in[0], &BTD_mat)); 635123d890dSSebastian Grimberg CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat)); 636506b1a0cSSebastian Grimberg if (elem_rstr_curl_orients_in || elem_rstr_curl_orients_out) CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat_b)); 6371c66c397SJeremy L Thompson 63828ec399dSJeremy L Thompson CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals)); 639506b1a0cSSebastian Grimberg for (CeedSize e = 0; e < num_elem_in; e++) { 640506b1a0cSSebastian Grimberg for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) { 641506b1a0cSSebastian Grimberg for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) { 642ed9e99e6SJeremy L Thompson // Compute B^T*D 643506b1a0cSSebastian Grimberg for (CeedSize n = 0; n < elem_size_out; n++) { 644506b1a0cSSebastian Grimberg for (CeedSize q = 0; q < num_qpts_in; q++) { 645437c7c90SJeremy L Thompson for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) { 646506b1a0cSSebastian Grimberg const CeedSize btd_index = n * (num_qpts_in * num_eval_modes_in[0]) + q * num_eval_modes_in[0] + e_in; 647067fd99fSJeremy L Thompson CeedScalar sum = 0.0; 6481c66c397SJeremy L Thompson 649437c7c90SJeremy L Thompson for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) { 650506b1a0cSSebastian Grimberg const CeedSize b_out_index = (q * num_eval_modes_out[0] + e_out) * elem_size_out + n; 651506b1a0cSSebastian 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; 652b94338b9SJed Brown const CeedSize qf_index = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2]; 6531c66c397SJeremy L Thompson 654067fd99fSJeremy L Thompson sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index]; 655eaf62fffSJeremy L Thompson } 656067fd99fSJeremy L Thompson BTD_mat[btd_index] = sum; 657ed9e99e6SJeremy L Thompson } 658ed9e99e6SJeremy L Thompson } 659eaf62fffSJeremy L Thompson } 6607c1dbaffSSebastian Grimberg 6617c1dbaffSSebastian Grimberg // Form element matrix itself (for each block component) 662e4065a52SSebastian Grimberg if (contract) { 6630459ebd3SSebastian Grimberg CeedCall(CeedTensorContractApply(contract, 1, num_qpts_in * num_eval_modes_in[0], elem_size_in, elem_size_out, BTD_mat, CEED_NOTRANSPOSE, 6640459ebd3SSebastian Grimberg false, B_mat_in, elem_mat)); 665e4065a52SSebastian Grimberg } else { 666e4065a52SSebastian 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])); 667e4065a52SSebastian Grimberg } 668eaf62fffSJeremy L Thompson 6697c1dbaffSSebastian Grimberg // Transform the element matrix if required 670506b1a0cSSebastian Grimberg if (elem_rstr_orients_out) { 671506b1a0cSSebastian Grimberg const bool *elem_orients = &elem_rstr_orients_out[e * elem_size_out]; 6721c66c397SJeremy L Thompson 673506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 674506b1a0cSSebastian Grimberg const double orient = elem_orients[i] ? -1.0 : 1.0; 675506b1a0cSSebastian Grimberg 676506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 677506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] *= orient; 6787c1dbaffSSebastian Grimberg } 6797c1dbaffSSebastian Grimberg } 680506b1a0cSSebastian Grimberg } else if (elem_rstr_curl_orients_out) { 681506b1a0cSSebastian Grimberg const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_out[e * 3 * elem_size_out]; 6821c66c397SJeremy L Thompson 6837c1dbaffSSebastian Grimberg // T^T*(B^T*D*B) 684506b1a0cSSebastian Grimberg memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar)); 685506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 686506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 687506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * i + 1] + 688506b1a0cSSebastian Grimberg (i > 0 ? elem_mat_b[(i - 1) * elem_size_in + j] * elem_curl_orients[3 * i - 1] : 0.0) + 689506b1a0cSSebastian Grimberg (i < elem_size_out - 1 ? elem_mat_b[(i + 1) * elem_size_in + j] * elem_curl_orients[3 * i + 3] : 0.0); 6907c1dbaffSSebastian Grimberg } 6917c1dbaffSSebastian Grimberg } 692506b1a0cSSebastian Grimberg } 693506b1a0cSSebastian Grimberg if (elem_rstr_orients_in) { 694506b1a0cSSebastian Grimberg const bool *elem_orients = &elem_rstr_orients_in[e * elem_size_in]; 695506b1a0cSSebastian Grimberg 696506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 697506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 698506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] *= elem_orients[j] ? -1.0 : 1.0; 699506b1a0cSSebastian Grimberg } 700506b1a0cSSebastian Grimberg } 701506b1a0cSSebastian Grimberg } else if (elem_rstr_curl_orients_in) { 702506b1a0cSSebastian Grimberg const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_in[e * 3 * elem_size_in]; 703506b1a0cSSebastian Grimberg 704506b1a0cSSebastian Grimberg // (B^T*D*B)*T 705506b1a0cSSebastian Grimberg memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar)); 706506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 707506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 708506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * j + 1] + 709506b1a0cSSebastian Grimberg (j > 0 ? elem_mat_b[i * elem_size_in + j - 1] * elem_curl_orients[3 * j - 1] : 0.0) + 710506b1a0cSSebastian Grimberg (j < elem_size_in - 1 ? elem_mat_b[i * elem_size_in + j + 1] * elem_curl_orients[3 * j + 3] : 0.0); 7117c1dbaffSSebastian Grimberg } 7127c1dbaffSSebastian Grimberg } 7137c1dbaffSSebastian Grimberg } 7147c1dbaffSSebastian Grimberg 7157c1dbaffSSebastian Grimberg // Put element matrix in coordinate data structure 716506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 717506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 718506b1a0cSSebastian Grimberg vals[offset + count] = elem_mat[i * elem_size_in + j]; 719eaf62fffSJeremy L Thompson count++; 720eaf62fffSJeremy L Thompson } 721eaf62fffSJeremy L Thompson } 722eaf62fffSJeremy L Thompson } 723eaf62fffSJeremy L Thompson } 724eaf62fffSJeremy L Thompson } 7256574a04fSJeremy L Thompson CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing entries"); 7262b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(values, &vals)); 727eaf62fffSJeremy L Thompson 728506b1a0cSSebastian Grimberg // Cleanup 729123d890dSSebastian Grimberg CeedCall(CeedFree(&BTD_mat)); 730123d890dSSebastian Grimberg CeedCall(CeedFree(&elem_mat)); 731506b1a0cSSebastian Grimberg CeedCall(CeedFree(&elem_mat_b)); 732506b1a0cSSebastian Grimberg if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) { 733506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_in, &elem_rstr_orients_in)); 734506b1a0cSSebastian Grimberg } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 735506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_in, &elem_rstr_curl_orients_in)); 736506b1a0cSSebastian Grimberg } 737506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 738506b1a0cSSebastian Grimberg if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) { 739506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_out, &elem_rstr_orients_out)); 740506b1a0cSSebastian Grimberg } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 741506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_out, &elem_rstr_curl_orients_out)); 742506b1a0cSSebastian Grimberg } 743506b1a0cSSebastian Grimberg } 7442b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 7452b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled_qf)); 746eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 747eaf62fffSJeremy L Thompson } 748eaf62fffSJeremy L Thompson 749eaf62fffSJeremy L Thompson /** 750ca94c3ddSJeremy L Thompson @brief Count number of entries for assembled `CeedOperator` 751eaf62fffSJeremy L Thompson 752ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 753eaf62fffSJeremy L Thompson @param[out] num_entries Number of entries in assembled representation 754eaf62fffSJeremy L Thompson 755eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 756eaf62fffSJeremy L Thompson 757eaf62fffSJeremy L Thompson @ref Utility 758eaf62fffSJeremy L Thompson **/ 759b94338b9SJed Brown static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedSize *num_entries) { 760b275c451SJeremy L Thompson bool is_composite; 761506b1a0cSSebastian Grimberg CeedInt num_elem_in, elem_size_in, num_comp_in, num_elem_out, elem_size_out, num_comp_out; 7621203703bSJeremy L Thompson Ceed ceed; 763506b1a0cSSebastian Grimberg CeedElemRestriction rstr_in, rstr_out; 764eaf62fffSJeremy L Thompson 7651203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 766b275c451SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 7671203703bSJeremy L Thompson CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 768506b1a0cSSebastian Grimberg 769506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out)); 770506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in)); 771506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in)); 772506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in)); 773506b1a0cSSebastian Grimberg if (rstr_in != rstr_out) { 774506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out)); 7751203703bSJeremy L Thompson CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, 776506b1a0cSSebastian Grimberg "Active input and output operator restrictions must have the same number of elements"); 777506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out)); 778506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out)); 779506b1a0cSSebastian Grimberg } else { 780506b1a0cSSebastian Grimberg num_elem_out = num_elem_in; 781506b1a0cSSebastian Grimberg elem_size_out = elem_size_in; 782506b1a0cSSebastian Grimberg num_comp_out = num_comp_in; 783506b1a0cSSebastian Grimberg } 784506b1a0cSSebastian Grimberg *num_entries = (CeedSize)elem_size_in * num_comp_in * elem_size_out * num_comp_out * num_elem_in; 785eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 786eaf62fffSJeremy L Thompson } 787eaf62fffSJeremy L Thompson 788eaf62fffSJeremy L Thompson /** 789ca94c3ddSJeremy L Thompson @brief Common code for creating a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` 790eaf62fffSJeremy L Thompson 791ca94c3ddSJeremy L Thompson @param[in] op_fine Fine grid `CeedOperator` 792ca94c3ddSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator` 793ca94c3ddSJeremy L Thompson @param[in] rstr_coarse Coarse grid `CeedElemRestriction` 794ca94c3ddSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector `CeedBasis` 795ca94c3ddSJeremy L Thompson @param[in] basis_c_to_f `CeedBasis` for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction operators 796ca94c3ddSJeremy L Thompson @param[out] op_coarse Coarse grid `CeedOperator` 797ca94c3ddSJeremy L Thompson @param[out] op_prolong Coarse to fine `CeedOperator`, or `NULL` 798ca94c3ddSJeremy L Thompson @param[out] op_restrict Fine to coarse `CeedOperator`, or `NULL` 799eaf62fffSJeremy L Thompson 800eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 801eaf62fffSJeremy L Thompson 802eaf62fffSJeremy L Thompson @ref Developer 803eaf62fffSJeremy L Thompson **/ 8042b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 8057758292fSSebastian Grimberg CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) { 8061c66c397SJeremy L Thompson bool is_composite; 807eaf62fffSJeremy L Thompson Ceed ceed; 8081203703bSJeremy L Thompson CeedInt num_comp, num_input_fields, num_output_fields; 80985bb9dcfSJeremy L Thompson CeedVector mult_vec = NULL; 8101c66c397SJeremy L Thompson CeedElemRestriction rstr_p_mult_fine = NULL, rstr_fine = NULL; 8111203703bSJeremy L Thompson CeedOperatorField *input_fields, *output_fields; 8121c66c397SJeremy L Thompson 8132b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op_fine, &ceed)); 814eaf62fffSJeremy L Thompson 815eaf62fffSJeremy L Thompson // Check for composite operator 8162b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op_fine, &is_composite)); 8176574a04fSJeremy L Thompson CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported"); 818eaf62fffSJeremy L Thompson 819eaf62fffSJeremy L Thompson // Coarse Grid 8202b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse)); 8211203703bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op_fine, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 822eaf62fffSJeremy L Thompson // -- Clone input fields 8231203703bSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 8246f8994e9SJeremy L Thompson const char *field_name; 8251203703bSJeremy L Thompson CeedVector vec; 8261203703bSJeremy L Thompson CeedElemRestriction rstr; 8271203703bSJeremy L Thompson CeedBasis basis; 8281203703bSJeremy L Thompson 8291203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetName(input_fields[i], &field_name)); 8301203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(input_fields[i], &vec)); 8311203703bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 8321203703bSJeremy L Thompson rstr = rstr_coarse; 8331203703bSJeremy L Thompson basis = basis_coarse; 8341203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_fine)); 835eaf62fffSJeremy L Thompson } else { 8361203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr)); 8371203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis)); 838eaf62fffSJeremy L Thompson } 8391203703bSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec)); 840eaf62fffSJeremy L Thompson } 841eaf62fffSJeremy L Thompson // -- Clone output fields 8421203703bSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 8436f8994e9SJeremy L Thompson const char *field_name; 8441203703bSJeremy L Thompson CeedVector vec; 8451203703bSJeremy L Thompson CeedElemRestriction rstr; 8461203703bSJeremy L Thompson CeedBasis basis; 8471203703bSJeremy L Thompson 8481203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetName(output_fields[i], &field_name)); 8491203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec)); 8501203703bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 8511203703bSJeremy L Thompson rstr = rstr_coarse; 8521203703bSJeremy L Thompson basis = basis_coarse; 8531203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_fine)); 854eaf62fffSJeremy L Thompson } else { 8551203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr)); 8561203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis)); 857eaf62fffSJeremy L Thompson } 8581203703bSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec)); 859eaf62fffSJeremy L Thompson } 860af99e877SJeremy L Thompson // -- Clone QFunctionAssemblyData 8612b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op_fine->qf_assembled, &(*op_coarse)->qf_assembled)); 862eaf62fffSJeremy L Thompson 863eaf62fffSJeremy L Thompson // Multiplicity vector 8647758292fSSebastian Grimberg if (op_restrict || op_prolong) { 86585bb9dcfSJeremy L Thompson CeedVector mult_e_vec; 8661c66c397SJeremy L Thompson CeedRestrictionType rstr_type; 86785bb9dcfSJeremy L Thompson 8687c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionGetType(rstr_fine, &rstr_type)); 8697c1dbaffSSebastian Grimberg CeedCheck(rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_UNSUPPORTED, 8707c1dbaffSSebastian Grimberg "Element restrictions created with CeedElemRestrictionCreateCurlOriented are not supported"); 8716574a04fSJeremy L Thompson CeedCheck(p_mult_fine, ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector"); 8727c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnsignedCopy(rstr_fine, &rstr_p_mult_fine)); 8732b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec)); 8742b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(mult_e_vec, 0.0)); 875c17ec2beSJeremy L Thompson CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE)); 8762b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(mult_vec, 0.0)); 877c17ec2beSJeremy L Thompson CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE)); 8782b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&mult_e_vec)); 8792b730f8bSJeremy L Thompson CeedCall(CeedVectorReciprocal(mult_vec)); 88085bb9dcfSJeremy L Thompson } 881eaf62fffSJeremy L Thompson 882addd79feSZach Atkins // Clone name 883addd79feSZach Atkins bool has_name = op_fine->name; 884addd79feSZach Atkins size_t name_len = op_fine->name ? strlen(op_fine->name) : 0; 885addd79feSZach Atkins CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name)); 886addd79feSZach Atkins 8877758292fSSebastian Grimberg // Check that coarse to fine basis is provided if prolong/restrict operators are requested 8887758292fSSebastian Grimberg CeedCheck(basis_c_to_f || (!op_restrict && !op_prolong), ceed, CEED_ERROR_INCOMPATIBLE, 8896574a04fSJeremy L Thompson "Prolongation or restriction operator creation requires coarse-to-fine basis"); 89083d6adf3SZach Atkins 89185bb9dcfSJeremy L Thompson // Restriction/Prolongation Operators 8922b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp)); 893addd79feSZach Atkins 894addd79feSZach Atkins // Restriction 8957758292fSSebastian Grimberg if (op_restrict) { 896eaf62fffSJeremy L Thompson CeedInt *num_comp_r_data; 89785bb9dcfSJeremy L Thompson CeedQFunctionContext ctx_r; 8987758292fSSebastian Grimberg CeedQFunction qf_restrict; 89985bb9dcfSJeremy L Thompson 9007758292fSSebastian Grimberg CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict)); 9012b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &num_comp_r_data)); 902eaf62fffSJeremy L Thompson num_comp_r_data[0] = num_comp; 9032b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r)); 9042b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data)); 9057758292fSSebastian Grimberg CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r)); 9062b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(&ctx_r)); 9077758292fSSebastian Grimberg CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE)); 9087758292fSSebastian Grimberg CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE)); 9097758292fSSebastian Grimberg CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP)); 9107758292fSSebastian Grimberg CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp)); 911eaf62fffSJeremy L Thompson 9127758292fSSebastian Grimberg CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict)); 9137758292fSSebastian Grimberg CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE)); 9147758292fSSebastian Grimberg CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec)); 9157758292fSSebastian Grimberg CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE)); 916eaf62fffSJeremy L Thompson 917addd79feSZach Atkins // Set name 918addd79feSZach Atkins char *restriction_name; 9191c66c397SJeremy L Thompson 920addd79feSZach Atkins CeedCall(CeedCalloc(17 + name_len, &restriction_name)); 921addd79feSZach Atkins sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : ""); 9227758292fSSebastian Grimberg CeedCall(CeedOperatorSetName(*op_restrict, restriction_name)); 923addd79feSZach Atkins CeedCall(CeedFree(&restriction_name)); 924addd79feSZach Atkins 925addd79feSZach Atkins // Check 9267758292fSSebastian Grimberg CeedCall(CeedOperatorCheckReady(*op_restrict)); 927addd79feSZach Atkins 928addd79feSZach Atkins // Cleanup 9297758292fSSebastian Grimberg CeedCall(CeedQFunctionDestroy(&qf_restrict)); 930addd79feSZach Atkins } 931addd79feSZach Atkins 932eaf62fffSJeremy L Thompson // Prolongation 933addd79feSZach Atkins if (op_prolong) { 934eaf62fffSJeremy L Thompson CeedInt *num_comp_p_data; 93585bb9dcfSJeremy L Thompson CeedQFunctionContext ctx_p; 9361c66c397SJeremy L Thompson CeedQFunction qf_prolong; 93785bb9dcfSJeremy L Thompson 93885bb9dcfSJeremy L Thompson CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong)); 9392b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &num_comp_p_data)); 940eaf62fffSJeremy L Thompson num_comp_p_data[0] = num_comp; 9412b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p)); 9422b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data)); 9432b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p)); 9442b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(&ctx_p)); 9452b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP)); 9462b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE)); 9472b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE)); 9482b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp)); 949eaf62fffSJeremy L Thompson 9502b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong)); 9512b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE)); 952356036faSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec)); 953356036faSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE)); 954eaf62fffSJeremy L Thompson 955addd79feSZach Atkins // Set name 956ea6b5821SJeremy L Thompson char *prolongation_name; 9571c66c397SJeremy L Thompson 9582b730f8bSJeremy L Thompson CeedCall(CeedCalloc(18 + name_len, &prolongation_name)); 9592b730f8bSJeremy L Thompson sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : ""); 9602b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name)); 9612b730f8bSJeremy L Thompson CeedCall(CeedFree(&prolongation_name)); 962addd79feSZach Atkins 963addd79feSZach Atkins // Check 964addd79feSZach Atkins CeedCall(CeedOperatorCheckReady(*op_prolong)); 965addd79feSZach Atkins 966addd79feSZach Atkins // Cleanup 967addd79feSZach Atkins CeedCall(CeedQFunctionDestroy(&qf_prolong)); 968ea6b5821SJeremy L Thompson } 969ea6b5821SJeremy L Thompson 97058e4b056SJeremy L Thompson // Check 97158e4b056SJeremy L Thompson CeedCall(CeedOperatorCheckReady(*op_coarse)); 97258e4b056SJeremy L Thompson 973eaf62fffSJeremy L Thompson // Cleanup 9742b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&mult_vec)); 975c17ec2beSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine)); 9762b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_c_to_f)); 977eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 978eaf62fffSJeremy L Thompson } 979eaf62fffSJeremy L Thompson 980eaf62fffSJeremy L Thompson /** 981eaf62fffSJeremy L Thompson @brief Build 1D mass matrix and Laplacian with perturbation 982eaf62fffSJeremy L Thompson 983eaf62fffSJeremy L Thompson @param[in] interp_1d Interpolation matrix in one dimension 984eaf62fffSJeremy L Thompson @param[in] grad_1d Gradient matrix in one dimension 985eaf62fffSJeremy L Thompson @param[in] q_weight_1d Quadrature weights in one dimension 986eaf62fffSJeremy L Thompson @param[in] P_1d Number of basis nodes in one dimension 987eaf62fffSJeremy L Thompson @param[in] Q_1d Number of quadrature points in one dimension 988eaf62fffSJeremy L Thompson @param[in] dim Dimension of basis 989eaf62fffSJeremy L Thompson @param[out] mass Assembled mass matrix in one dimension 990eaf62fffSJeremy L Thompson @param[out] laplace Assembled perturbed Laplacian in one dimension 991eaf62fffSJeremy L Thompson 992eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 993eaf62fffSJeremy L Thompson 994eaf62fffSJeremy L Thompson @ref Developer 995eaf62fffSJeremy L Thompson **/ 9962c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 9972c2ea1dbSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d, CeedInt P_1d, CeedInt Q_1d, 9982c2ea1dbSJeremy L Thompson CeedInt dim, CeedScalar *mass, CeedScalar *laplace) { 9992b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) { 1000eaf62fffSJeremy L Thompson for (CeedInt j = 0; j < P_1d; j++) { 1001eaf62fffSJeremy L Thompson CeedScalar sum = 0.0; 10022b730f8bSJeremy 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]; 1003eaf62fffSJeremy L Thompson mass[i + j * P_1d] = sum; 1004eaf62fffSJeremy L Thompson } 10052b730f8bSJeremy L Thompson } 1006eaf62fffSJeremy L Thompson // -- Laplacian 10072b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) { 1008eaf62fffSJeremy L Thompson for (CeedInt j = 0; j < P_1d; j++) { 1009eaf62fffSJeremy L Thompson CeedScalar sum = 0.0; 10101c66c397SJeremy L Thompson 10112b730f8bSJeremy 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]; 1012eaf62fffSJeremy L Thompson laplace[i + j * P_1d] = sum; 1013eaf62fffSJeremy L Thompson } 10142b730f8bSJeremy L Thompson } 1015eaf62fffSJeremy L Thompson CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4; 10162b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation; 1017eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1018eaf62fffSJeremy L Thompson } 10192c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 1020eaf62fffSJeremy L Thompson 1021eaf62fffSJeremy L Thompson /// @} 1022eaf62fffSJeremy L Thompson 1023eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 1024480fae85SJeremy L Thompson /// CeedOperator Backend API 1025480fae85SJeremy L Thompson /// ---------------------------------------------------------------------------- 1026480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend 1027480fae85SJeremy L Thompson /// @{ 1028480fae85SJeremy L Thompson 1029480fae85SJeremy L Thompson /** 1030004e4986SSebastian Grimberg @brief Select correct basis matrix pointer based on @ref CeedEvalMode 1031004e4986SSebastian Grimberg 1032004e4986SSebastian Grimberg @param[in] basis `CeedBasis` from which to get the basis matrix 1033004e4986SSebastian Grimberg @param[in] eval_mode Current basis evaluation mode 1034004e4986SSebastian Grimberg @param[in] identity Pointer to identity matrix 1035004e4986SSebastian Grimberg @param[out] basis_ptr `CeedBasis` pointer to set 1036004e4986SSebastian Grimberg 1037004e4986SSebastian Grimberg @ref Backend 1038004e4986SSebastian Grimberg **/ 1039004e4986SSebastian Grimberg int CeedOperatorGetBasisPointer(CeedBasis basis, CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar **basis_ptr) { 1040004e4986SSebastian Grimberg switch (eval_mode) { 1041004e4986SSebastian Grimberg case CEED_EVAL_NONE: 1042004e4986SSebastian Grimberg *basis_ptr = identity; 1043004e4986SSebastian Grimberg break; 1044004e4986SSebastian Grimberg case CEED_EVAL_INTERP: 1045004e4986SSebastian Grimberg CeedCall(CeedBasisGetInterp(basis, basis_ptr)); 1046004e4986SSebastian Grimberg break; 1047004e4986SSebastian Grimberg case CEED_EVAL_GRAD: 1048004e4986SSebastian Grimberg CeedCall(CeedBasisGetGrad(basis, basis_ptr)); 1049004e4986SSebastian Grimberg break; 1050004e4986SSebastian Grimberg case CEED_EVAL_DIV: 1051004e4986SSebastian Grimberg CeedCall(CeedBasisGetDiv(basis, basis_ptr)); 1052004e4986SSebastian Grimberg break; 1053004e4986SSebastian Grimberg case CEED_EVAL_CURL: 1054004e4986SSebastian Grimberg CeedCall(CeedBasisGetCurl(basis, basis_ptr)); 1055004e4986SSebastian Grimberg break; 1056004e4986SSebastian Grimberg case CEED_EVAL_WEIGHT: 1057004e4986SSebastian Grimberg break; // Caught by QF Assembly 1058004e4986SSebastian Grimberg } 1059004e4986SSebastian Grimberg assert(*basis_ptr != NULL); 1060004e4986SSebastian Grimberg return CEED_ERROR_SUCCESS; 1061004e4986SSebastian Grimberg } 1062004e4986SSebastian Grimberg 1063004e4986SSebastian Grimberg /** 1064ca94c3ddSJeremy L Thompson @brief Create point block restriction for active `CeedOperatorField` 1065506b1a0cSSebastian Grimberg 1066ca94c3ddSJeremy L Thompson @param[in] rstr Original `CeedElemRestriction` for active field 1067ca94c3ddSJeremy L Thompson @param[out] point_block_rstr Address of the variable where the newly created `CeedElemRestriction` will be stored 1068506b1a0cSSebastian Grimberg 1069506b1a0cSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1070506b1a0cSSebastian Grimberg 1071506b1a0cSSebastian Grimberg @ref Backend 1072506b1a0cSSebastian Grimberg **/ 1073506b1a0cSSebastian Grimberg int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *point_block_rstr) { 1074506b1a0cSSebastian Grimberg Ceed ceed; 1075506b1a0cSSebastian Grimberg CeedInt num_elem, num_comp, shift, elem_size, comp_stride, *point_block_offsets; 1076506b1a0cSSebastian Grimberg CeedSize l_size; 1077506b1a0cSSebastian Grimberg const CeedInt *offsets; 1078506b1a0cSSebastian Grimberg 1079506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed)); 1080506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets)); 1081506b1a0cSSebastian Grimberg 1082506b1a0cSSebastian Grimberg // Expand offsets 1083506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem)); 1084506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp)); 1085506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size)); 1086506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride)); 1087506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size)); 1088506b1a0cSSebastian Grimberg shift = num_comp; 1089506b1a0cSSebastian Grimberg if (comp_stride != 1) shift *= num_comp; 1090506b1a0cSSebastian Grimberg CeedCall(CeedCalloc(num_elem * elem_size, &point_block_offsets)); 1091506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_elem * elem_size; i++) { 1092506b1a0cSSebastian Grimberg point_block_offsets[i] = offsets[i] * shift; 1093506b1a0cSSebastian Grimberg } 1094506b1a0cSSebastian Grimberg 1095506b1a0cSSebastian Grimberg // Create new restriction 1096506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER, 1097506b1a0cSSebastian Grimberg point_block_offsets, point_block_rstr)); 1098506b1a0cSSebastian Grimberg 1099506b1a0cSSebastian Grimberg // Cleanup 1100506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets)); 1101506b1a0cSSebastian Grimberg return CEED_ERROR_SUCCESS; 1102506b1a0cSSebastian Grimberg } 1103506b1a0cSSebastian Grimberg 1104506b1a0cSSebastian Grimberg /** 1105ca94c3ddSJeremy L Thompson @brief Create object holding `CeedQFunction` assembly data for `CeedOperator` 1106480fae85SJeremy L Thompson 1107ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedQFunctionAssemblyData` 1108ca94c3ddSJeremy L Thompson @param[out] data Address of the variable where the newly created `CeedQFunctionAssemblyData` will be stored 1109480fae85SJeremy L Thompson 1110480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1111480fae85SJeremy L Thompson 1112480fae85SJeremy L Thompson @ref Backend 1113480fae85SJeremy L Thompson **/ 1114ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) { 11152b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, data)); 1116480fae85SJeremy L Thompson (*data)->ref_count = 1; 1117480fae85SJeremy L Thompson (*data)->ceed = ceed; 11182b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 1119480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1120480fae85SJeremy L Thompson } 1121480fae85SJeremy L Thompson 1122480fae85SJeremy L Thompson /** 1123ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedQFunctionAssemblyData` 1124480fae85SJeremy L Thompson 1125ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to increment the reference counter 1126480fae85SJeremy L Thompson 1127480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1128480fae85SJeremy L Thompson 1129480fae85SJeremy L Thompson @ref Backend 1130480fae85SJeremy L Thompson **/ 1131480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) { 1132480fae85SJeremy L Thompson data->ref_count++; 1133480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1134480fae85SJeremy L Thompson } 1135480fae85SJeremy L Thompson 1136480fae85SJeremy L Thompson /** 1137ca94c3ddSJeremy L Thompson @brief Set re-use of `CeedQFunctionAssemblyData` 11388b919e6bSJeremy L Thompson 1139ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to mark for reuse 1140ea61e9acSJeremy L Thompson @param[in] reuse_data Boolean flag indicating data re-use 11418b919e6bSJeremy L Thompson 11428b919e6bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 11438b919e6bSJeremy L Thompson 11448b919e6bSJeremy L Thompson @ref Backend 11458b919e6bSJeremy L Thompson **/ 11462b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) { 1147beecbf24SJeremy L Thompson data->reuse_data = reuse_data; 1148beecbf24SJeremy L Thompson data->needs_data_update = true; 1149beecbf24SJeremy L Thompson return CEED_ERROR_SUCCESS; 1150beecbf24SJeremy L Thompson } 1151beecbf24SJeremy L Thompson 1152beecbf24SJeremy L Thompson /** 1153ca94c3ddSJeremy L Thompson @brief Mark `CeedQFunctionAssemblyData` as stale 1154beecbf24SJeremy L Thompson 1155ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to mark as stale 1156ea61e9acSJeremy L Thompson @param[in] needs_data_update Boolean flag indicating if update is needed or completed 1157beecbf24SJeremy L Thompson 1158beecbf24SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1159beecbf24SJeremy L Thompson 1160beecbf24SJeremy L Thompson @ref Backend 1161beecbf24SJeremy L Thompson **/ 11622b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) { 1163beecbf24SJeremy L Thompson data->needs_data_update = needs_data_update; 11648b919e6bSJeremy L Thompson return CEED_ERROR_SUCCESS; 11658b919e6bSJeremy L Thompson } 11668b919e6bSJeremy L Thompson 11678b919e6bSJeremy L Thompson /** 1168ca94c3ddSJeremy L Thompson @brief Determine if `CeedQFunctionAssemblyData` needs update 11698b919e6bSJeremy L Thompson 1170ca94c3ddSJeremy L Thompson @param[in] data `CeedQFunctionAssemblyData` to mark as stale 11718b919e6bSJeremy L Thompson @param[out] is_update_needed Boolean flag indicating if re-assembly is required 11728b919e6bSJeremy L Thompson 11738b919e6bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 11748b919e6bSJeremy L Thompson 11758b919e6bSJeremy L Thompson @ref Backend 11768b919e6bSJeremy L Thompson **/ 11772b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) { 1178beecbf24SJeremy L Thompson *is_update_needed = !data->reuse_data || data->needs_data_update; 11798b919e6bSJeremy L Thompson return CEED_ERROR_SUCCESS; 11808b919e6bSJeremy L Thompson } 11818b919e6bSJeremy L Thompson 11828b919e6bSJeremy L Thompson /** 1183ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedQFunctionAssemblyData`. 11844385fb7fSSebastian Grimberg 1185ca94c3ddSJeremy L Thompson Both pointers should be destroyed with @ref CeedQFunctionAssemblyDataDestroy(). 1186512bb800SJeremy L Thompson 1187ca94c3ddSJeremy 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`. 1188ca94c3ddSJeremy L Thompson This `CeedQFunctionAssemblyData` will be destroyed if ` *data_copy` is the only reference to this `CeedQFunctionAssemblyData`. 1189480fae85SJeremy L Thompson 1190ca94c3ddSJeremy L Thompson @param[in] data `CeedQFunctionAssemblyData` to copy reference to 1191ea61e9acSJeremy L Thompson @param[in,out] data_copy Variable to store copied reference 1192480fae85SJeremy L Thompson 1193480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1194480fae85SJeremy L Thompson 1195480fae85SJeremy L Thompson @ref Backend 1196480fae85SJeremy L Thompson **/ 11972b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) { 11982b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataReference(data)); 11992b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy)); 1200480fae85SJeremy L Thompson *data_copy = data; 1201480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1202480fae85SJeremy L Thompson } 1203480fae85SJeremy L Thompson 1204480fae85SJeremy L Thompson /** 1205ca94c3ddSJeremy L Thompson @brief Get setup status for internal objects for `CeedQFunctionAssemblyData` 1206480fae85SJeremy L Thompson 1207ca94c3ddSJeremy L Thompson @param[in] data `CeedQFunctionAssemblyData` to retrieve status 1208480fae85SJeremy L Thompson @param[out] is_setup Boolean flag for setup status 1209480fae85SJeremy L Thompson 1210480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1211480fae85SJeremy L Thompson 1212480fae85SJeremy L Thompson @ref Backend 1213480fae85SJeremy L Thompson **/ 12142b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) { 1215480fae85SJeremy L Thompson *is_setup = data->is_setup; 1216480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1217480fae85SJeremy L Thompson } 1218480fae85SJeremy L Thompson 1219480fae85SJeremy L Thompson /** 1220ca94c3ddSJeremy L Thompson @brief Set internal objects for `CeedQFunctionAssemblyData` 1221480fae85SJeremy L Thompson 1222ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to set objects 1223ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to store assembled `CeedQFunction` at quadrature points 1224ca94c3ddSJeremy L Thompson @param[in] rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction` 1225480fae85SJeremy L Thompson 1226480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1227480fae85SJeremy L Thompson 1228480fae85SJeremy L Thompson @ref Backend 1229480fae85SJeremy L Thompson **/ 12302b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) { 12312b730f8bSJeremy L Thompson CeedCall(CeedVectorReferenceCopy(vec, &data->vec)); 12322b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr)); 1233480fae85SJeremy L Thompson 1234480fae85SJeremy L Thompson data->is_setup = true; 1235480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1236480fae85SJeremy L Thompson } 1237480fae85SJeremy L Thompson 12384dd1a9d2SSebastian Grimberg /** 1239ca94c3ddSJeremy L Thompson @brief Get internal objects for `CeedQFunctionAssemblyData` 12404dd1a9d2SSebastian Grimberg 1241ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to set objects 1242ca94c3ddSJeremy L Thompson @param[out] vec `CeedVector` to store assembled `CeedQFunction` at quadrature points 1243ca94c3ddSJeremy L Thompson @param[out] rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction` 12444dd1a9d2SSebastian Grimberg 12454dd1a9d2SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 12464dd1a9d2SSebastian Grimberg 12474dd1a9d2SSebastian Grimberg @ref Backend 12484dd1a9d2SSebastian Grimberg **/ 12492b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) { 12506574a04fSJeremy L Thompson CeedCheck(data->is_setup, data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first."); 1251480fae85SJeremy L Thompson 12522b730f8bSJeremy L Thompson CeedCall(CeedVectorReferenceCopy(data->vec, vec)); 12532b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr)); 1254480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1255480fae85SJeremy L Thompson } 1256480fae85SJeremy L Thompson 1257480fae85SJeremy L Thompson /** 1258ca94c3ddSJeremy L Thompson @brief Destroy `CeedQFunctionAssemblyData` 1259480fae85SJeremy L Thompson 1260ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to destroy 1261480fae85SJeremy L Thompson 1262480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1263480fae85SJeremy L Thompson 1264480fae85SJeremy L Thompson @ref Backend 1265480fae85SJeremy L Thompson **/ 1266480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) { 1267ad6481ceSJeremy L Thompson if (!*data || --(*data)->ref_count > 0) { 1268ad6481ceSJeremy L Thompson *data = NULL; 1269ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1270ad6481ceSJeremy L Thompson } 12712b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*data)->ceed)); 12722b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*data)->vec)); 12732b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr)); 1274480fae85SJeremy L Thompson 12752b730f8bSJeremy L Thompson CeedCall(CeedFree(data)); 1276480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1277480fae85SJeremy L Thompson } 1278480fae85SJeremy L Thompson 1279ed9e99e6SJeremy L Thompson /** 1280ca94c3ddSJeremy L Thompson @brief Get `CeedOperatorAssemblyData` 1281ed9e99e6SJeremy L Thompson 1282ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 1283ca94c3ddSJeremy L Thompson @param[out] data `CeedQFunctionAssemblyData` 1284ed9e99e6SJeremy L Thompson 1285ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1286ed9e99e6SJeremy L Thompson 1287ed9e99e6SJeremy L Thompson @ref Backend 1288ed9e99e6SJeremy L Thompson **/ 12892b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) { 1290ed9e99e6SJeremy L Thompson if (!op->op_assembled) { 1291ed9e99e6SJeremy L Thompson CeedOperatorAssemblyData data; 1292ed9e99e6SJeremy L Thompson 12932b730f8bSJeremy L Thompson CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data)); 1294ed9e99e6SJeremy L Thompson op->op_assembled = data; 1295ed9e99e6SJeremy L Thompson } 1296ed9e99e6SJeremy L Thompson *data = op->op_assembled; 1297ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1298ed9e99e6SJeremy L Thompson } 1299ed9e99e6SJeremy L Thompson 1300ed9e99e6SJeremy L Thompson /** 1301ca94c3ddSJeremy L Thompson @brief Create object holding `CeedOperator` assembly data. 1302ba746a46SJeremy L Thompson 1303ca94c3ddSJeremy L Thompson The `CeedOperatorAssemblyData` holds an array with references to every active `CeedBasis` used in the `CeedOperator`. 1304ca94c3ddSJeremy L Thompson An array with references to the corresponding active `CeedElemRestriction` is also stored. 1305ca94c3ddSJeremy L Thompson For each active `CeedBasis, the `CeedOperatorAssemblyData` holds an array of all input and output @ref CeedEvalMode for this `CeedBasis`. 1306ca94c3ddSJeremy L Thompson The `CeedOperatorAssemblyData` holds an array of offsets for indexing into the assembled `CeedQFunction` arrays to the row representing each @ref CeedEvalMode. 1307ca94c3ddSJeremy L Thompson The number of input columns across all active bases for the assembled `CeedQFunction` is also stored. 1308ca94c3ddSJeremy L Thompson Lastly, the `CeedOperatorAssembly` data holds assembled matrices representing the full action of the `CeedBasis` for all @ref CeedEvalMode. 1309ed9e99e6SJeremy L Thompson 1310ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedOperatorAssemblyData` 1311ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to be assembled 1312ca94c3ddSJeremy L Thompson @param[out] data Address of the variable where the newly created `CeedOperatorAssemblyData` will be stored 1313ed9e99e6SJeremy L Thompson 1314ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1315ed9e99e6SJeremy L Thompson 1316ed9e99e6SJeremy L Thompson @ref Backend 1317ed9e99e6SJeremy L Thompson **/ 13182b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) { 1319506b1a0cSSebastian Grimberg CeedInt num_active_bases_in = 0, num_active_bases_out = 0, offset = 0; 1320506b1a0cSSebastian Grimberg CeedInt num_input_fields, *num_eval_modes_in = NULL, num_output_fields, *num_eval_modes_out = NULL; 13211c66c397SJeremy L Thompson CeedSize **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL; 13221c66c397SJeremy L Thompson CeedEvalMode **eval_modes_in = NULL, **eval_modes_out = NULL; 13231c66c397SJeremy L Thompson CeedQFunctionField *qf_fields; 13241c66c397SJeremy L Thompson CeedQFunction qf; 13251c66c397SJeremy L Thompson CeedOperatorField *op_fields; 132601f0e615SJames Wright bool is_composite; 132701f0e615SJames Wright 132801f0e615SJames Wright CeedCall(CeedOperatorIsComposite(op, &is_composite)); 132901f0e615SJames Wright CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Can only create CeedOperator assembly data for non-composite operators."); 1330437c7c90SJeremy L Thompson 1331437c7c90SJeremy L Thompson // Allocate 13322b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, data)); 1333ed9e99e6SJeremy L Thompson (*data)->ceed = ceed; 13342b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 1335ed9e99e6SJeremy L Thompson 1336ed9e99e6SJeremy L Thompson // Build OperatorAssembly data 13372b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetQFunction(op, &qf)); 1338ed9e99e6SJeremy L Thompson 1339ed9e99e6SJeremy L Thompson // Determine active input basis 1340004e4986SSebastian Grimberg CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL)); 1341004e4986SSebastian Grimberg CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 1342ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1343ed9e99e6SJeremy L Thompson CeedVector vec; 13441c66c397SJeremy L Thompson 13452b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec)); 1346ed9e99e6SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 13477c1dbaffSSebastian Grimberg CeedInt index = -1, num_comp, q_comp; 13481c66c397SJeremy L Thompson CeedEvalMode eval_mode; 13491c66c397SJeremy L Thompson CeedBasis basis_in = NULL; 13501c66c397SJeremy L Thompson 13512b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in)); 13522b730f8bSJeremy L Thompson CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1353352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp)); 1354352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 1355506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_active_bases_in; i++) { 1356506b1a0cSSebastian Grimberg if ((*data)->active_bases_in[i] == basis_in) index = i; 1357437c7c90SJeremy L Thompson } 1358437c7c90SJeremy L Thompson if (index == -1) { 1359437c7c90SJeremy L Thompson CeedElemRestriction elem_rstr_in; 13601c66c397SJeremy L Thompson 1361506b1a0cSSebastian Grimberg index = num_active_bases_in; 1362506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_bases_in)); 1363506b1a0cSSebastian Grimberg (*data)->active_bases_in[num_active_bases_in] = NULL; 1364506b1a0cSSebastian Grimberg CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases_in[num_active_bases_in])); 1365506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_elem_rstrs_in)); 1366506b1a0cSSebastian Grimberg (*data)->active_elem_rstrs_in[num_active_bases_in] = NULL; 1367437c7c90SJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in)); 1368506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs_in[num_active_bases_in])); 1369506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &num_eval_modes_in)); 1370437c7c90SJeremy L Thompson num_eval_modes_in[index] = 0; 1371506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_modes_in)); 1372437c7c90SJeremy L Thompson eval_modes_in[index] = NULL; 1373506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_mode_offsets_in)); 1374437c7c90SJeremy L Thompson eval_mode_offsets_in[index] = NULL; 1375506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->assembled_bases_in)); 1376437c7c90SJeremy L Thompson (*data)->assembled_bases_in[index] = NULL; 1377506b1a0cSSebastian Grimberg num_active_bases_in++; 1378437c7c90SJeremy L Thompson } 1379352a5e7cSSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1380352a5e7cSSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 1381352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_modes_in[index])); 1382352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_mode_offsets_in[index])); 1383352a5e7cSSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 1384437c7c90SJeremy L Thompson eval_modes_in[index][num_eval_modes_in[index] + d] = eval_mode; 1385437c7c90SJeremy L Thompson eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset; 1386352a5e7cSSebastian Grimberg offset += num_comp; 1387ed9e99e6SJeremy L Thompson } 1388352a5e7cSSebastian Grimberg num_eval_modes_in[index] += q_comp; 1389ed9e99e6SJeremy L Thompson } 1390ed9e99e6SJeremy L Thompson } 1391ed9e99e6SJeremy L Thompson } 1392ed9e99e6SJeremy L Thompson 1393ed9e99e6SJeremy L Thompson // Determine active output basis 13942b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields)); 13952b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 1396437c7c90SJeremy L Thompson offset = 0; 1397ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1398ed9e99e6SJeremy L Thompson CeedVector vec; 13991c66c397SJeremy L Thompson 14002b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec)); 1401ed9e99e6SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 14027c1dbaffSSebastian Grimberg CeedInt index = -1, num_comp, q_comp; 14031c66c397SJeremy L Thompson CeedEvalMode eval_mode; 14041c66c397SJeremy L Thompson CeedBasis basis_out = NULL; 14051c66c397SJeremy L Thompson 1406437c7c90SJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out)); 14072b730f8bSJeremy L Thompson CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1408352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumComponents(basis_out, &num_comp)); 1409352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 1410506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_active_bases_out; i++) { 1411506b1a0cSSebastian Grimberg if ((*data)->active_bases_out[i] == basis_out) index = i; 1412437c7c90SJeremy L Thompson } 1413437c7c90SJeremy L Thompson if (index == -1) { 1414437c7c90SJeremy L Thompson CeedElemRestriction elem_rstr_out; 14151c66c397SJeremy L Thompson 1416506b1a0cSSebastian Grimberg index = num_active_bases_out; 1417506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_bases_out)); 1418506b1a0cSSebastian Grimberg (*data)->active_bases_out[num_active_bases_out] = NULL; 1419506b1a0cSSebastian Grimberg CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases_out[num_active_bases_out])); 1420506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_elem_rstrs_out)); 1421506b1a0cSSebastian Grimberg (*data)->active_elem_rstrs_out[num_active_bases_out] = NULL; 1422437c7c90SJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out)); 1423506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs_out[num_active_bases_out])); 1424506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &num_eval_modes_out)); 1425437c7c90SJeremy L Thompson num_eval_modes_out[index] = 0; 1426506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_modes_out)); 1427437c7c90SJeremy L Thompson eval_modes_out[index] = NULL; 1428506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_mode_offsets_out)); 1429437c7c90SJeremy L Thompson eval_mode_offsets_out[index] = NULL; 1430506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->assembled_bases_out)); 1431437c7c90SJeremy L Thompson (*data)->assembled_bases_out[index] = NULL; 1432506b1a0cSSebastian Grimberg num_active_bases_out++; 1433437c7c90SJeremy L Thompson } 1434352a5e7cSSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1435352a5e7cSSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 1436352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_modes_out[index])); 1437352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_mode_offsets_out[index])); 1438352a5e7cSSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 1439437c7c90SJeremy L Thompson eval_modes_out[index][num_eval_modes_out[index] + d] = eval_mode; 1440437c7c90SJeremy L Thompson eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset; 1441352a5e7cSSebastian Grimberg offset += num_comp; 1442ed9e99e6SJeremy L Thompson } 1443352a5e7cSSebastian Grimberg num_eval_modes_out[index] += q_comp; 1444ed9e99e6SJeremy L Thompson } 1445ed9e99e6SJeremy L Thompson } 1446ed9e99e6SJeremy L Thompson } 1447506b1a0cSSebastian Grimberg (*data)->num_active_bases_in = num_active_bases_in; 144827789c4aSJed Brown (*data)->num_eval_modes_in = num_eval_modes_in; 144927789c4aSJed Brown (*data)->eval_modes_in = eval_modes_in; 145027789c4aSJed Brown (*data)->eval_mode_offsets_in = eval_mode_offsets_in; 1451506b1a0cSSebastian Grimberg (*data)->num_active_bases_out = num_active_bases_out; 1452437c7c90SJeremy L Thompson (*data)->num_eval_modes_out = num_eval_modes_out; 1453437c7c90SJeremy L Thompson (*data)->eval_modes_out = eval_modes_out; 1454437c7c90SJeremy L Thompson (*data)->eval_mode_offsets_out = eval_mode_offsets_out; 1455506b1a0cSSebastian Grimberg (*data)->num_output_components = offset; 1456ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1457ed9e99e6SJeremy L Thompson } 1458ed9e99e6SJeremy L Thompson 1459ed9e99e6SJeremy L Thompson /** 1460ca94c3ddSJeremy L Thompson @brief Get `CeedOperator` @ref CeedEvalMode for assembly. 1461ba746a46SJeremy L Thompson 1462ca94c3ddSJeremy L Thompson Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object. 1463ed9e99e6SJeremy L Thompson 1464ca94c3ddSJeremy L Thompson @param[in] data `CeedOperatorAssemblyData` 1465506b1a0cSSebastian Grimberg @param[out] num_active_bases_in Total number of active bases for input 1466ca94c3ddSJeremy L Thompson @param[out] num_eval_modes_in Pointer to hold array of numbers of input @ref CeedEvalMode, or `NULL`. 1467ca94c3ddSJeremy L Thompson `eval_modes_in[0]` holds an array of eval modes for the first active `CeedBasis`. 1468ca94c3ddSJeremy L Thompson @param[out] eval_modes_in Pointer to hold arrays of input @ref CeedEvalMode, or `NULL` 1469ca94c3ddSJeremy L Thompson @param[out] eval_mode_offsets_in Pointer to hold arrays of input offsets at each quadrature point 1470506b1a0cSSebastian Grimberg @param[out] num_active_bases_out Total number of active bases for output 1471ca94c3ddSJeremy L Thompson @param[out] num_eval_modes_out Pointer to hold array of numbers of output @ref CeedEvalMode, or `NULL` 1472ca94c3ddSJeremy L Thompson @param[out] eval_modes_out Pointer to hold arrays of output @ref CeedEvalMode, or `NULL` 1473437c7c90SJeremy L Thompson @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point 1474ca94c3ddSJeremy 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 1475ed9e99e6SJeremy L Thompson 1476ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1477ed9e99e6SJeremy L Thompson 1478ed9e99e6SJeremy L Thompson @ref Backend 1479ed9e99e6SJeremy L Thompson **/ 1480506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedInt **num_eval_modes_in, 1481506b1a0cSSebastian Grimberg const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt *num_active_bases_out, 1482506b1a0cSSebastian Grimberg CeedInt **num_eval_modes_out, const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out, 1483506b1a0cSSebastian Grimberg CeedSize *num_output_components) { 1484506b1a0cSSebastian Grimberg if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in; 1485437c7c90SJeremy L Thompson if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in; 1486437c7c90SJeremy L Thompson if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in; 1487437c7c90SJeremy L Thompson if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in; 1488506b1a0cSSebastian Grimberg if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out; 1489437c7c90SJeremy L Thompson if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out; 1490437c7c90SJeremy L Thompson if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out; 1491437c7c90SJeremy L Thompson if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out; 1492437c7c90SJeremy L Thompson if (num_output_components) *num_output_components = data->num_output_components; 1493ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1494ed9e99e6SJeremy L Thompson } 1495ed9e99e6SJeremy L Thompson 1496ed9e99e6SJeremy L Thompson /** 1497ca94c3ddSJeremy L Thompson @brief Get `CeedOperator` `CeedBasis` data for assembly. 1498ba746a46SJeremy L Thompson 1499ca94c3ddSJeremy L Thompson Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object. 1500ed9e99e6SJeremy L Thompson 1501ca94c3ddSJeremy L Thompson @param[in] data `CeedOperatorAssemblyData` 1502ca94c3ddSJeremy L Thompson @param[out] num_active_bases_in Number of active input bases, or `NULL` 1503ca94c3ddSJeremy L Thompson @param[out] active_bases_in Pointer to hold active input `CeedBasis`, or `NULL` 1504ca94c3ddSJeremy L Thompson @param[out] assembled_bases_in Pointer to hold assembled active input `B` , or `NULL` 1505ca94c3ddSJeremy L Thompson @param[out] num_active_bases_out Number of active output bases, or `NULL` 1506ca94c3ddSJeremy L Thompson @param[out] active_bases_out Pointer to hold active output `CeedBasis`, or `NULL` 1507ca94c3ddSJeremy L Thompson @param[out] assembled_bases_out Pointer to hold assembled active output `B` , or `NULL` 1508ed9e99e6SJeremy L Thompson 1509ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1510ed9e99e6SJeremy L Thompson 1511ed9e99e6SJeremy L Thompson @ref Backend 1512ed9e99e6SJeremy L Thompson **/ 1513506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedBasis **active_bases_in, 1514506b1a0cSSebastian Grimberg const CeedScalar ***assembled_bases_in, CeedInt *num_active_bases_out, CeedBasis **active_bases_out, 1515506b1a0cSSebastian Grimberg const CeedScalar ***assembled_bases_out) { 1516ed9e99e6SJeremy L Thompson // Assemble B_in, B_out if needed 1517437c7c90SJeremy L Thompson if (assembled_bases_in && !data->assembled_bases_in[0]) { 1518437c7c90SJeremy L Thompson CeedInt num_qpts; 1519437c7c90SJeremy L Thompson 1520506b1a0cSSebastian Grimberg if (data->active_bases_in[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[0], &num_qpts)); 1521506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_in[0], &num_qpts)); 1522506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < data->num_active_bases_in; b++) { 15231c66c397SJeremy L Thompson bool has_eval_none = false; 1524352a5e7cSSebastian Grimberg CeedInt num_nodes; 1525437c7c90SJeremy L Thompson CeedScalar *B_in = NULL, *identity = NULL; 1526ed9e99e6SJeremy L Thompson 1527506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[b], &num_nodes)); 1528352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_in[b], &B_in)); 1529ed9e99e6SJeremy L Thompson 1530437c7c90SJeremy L Thompson for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) { 1531437c7c90SJeremy L Thompson has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE); 1532ed9e99e6SJeremy L Thompson } 1533ed9e99e6SJeremy L Thompson if (has_eval_none) { 1534352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes, &identity)); 1535352a5e7cSSebastian Grimberg for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) { 1536352a5e7cSSebastian Grimberg identity[i * num_nodes + i] = 1.0; 1537ed9e99e6SJeremy L Thompson } 1538ed9e99e6SJeremy L Thompson } 1539ed9e99e6SJeremy L Thompson 1540ed9e99e6SJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 1541352a5e7cSSebastian Grimberg for (CeedInt n = 0; n < num_nodes; n++) { 1542352a5e7cSSebastian Grimberg CeedInt d_in = 0, q_comp_in; 1543352a5e7cSSebastian Grimberg CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE; 15441c66c397SJeremy L Thompson 1545437c7c90SJeremy L Thompson for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) { 1546437c7c90SJeremy L Thompson const CeedInt qq = data->num_eval_modes_in[b] * q; 1547437c7c90SJeremy L Thompson const CeedScalar *B = NULL; 15481c66c397SJeremy L Thompson 1549506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(data->active_bases_in[b], data->eval_modes_in[b][e_in], identity, &B)); 1550506b1a0cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_in[b], data->eval_modes_in[b][e_in], &q_comp_in)); 1551352a5e7cSSebastian Grimberg if (q_comp_in > 1) { 1552352a5e7cSSebastian Grimberg if (e_in == 0 || data->eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0; 1553352a5e7cSSebastian Grimberg else B = &B[(++d_in) * num_qpts * num_nodes]; 1554352a5e7cSSebastian Grimberg } 1555352a5e7cSSebastian Grimberg eval_mode_in_prev = data->eval_modes_in[b][e_in]; 1556352a5e7cSSebastian Grimberg B_in[(qq + e_in) * num_nodes + n] = B[q * num_nodes + n]; 1557ed9e99e6SJeremy L Thompson } 1558ed9e99e6SJeremy L Thompson } 1559ed9e99e6SJeremy L Thompson } 15607c1dbaffSSebastian Grimberg if (identity) CeedCall(CeedFree(&identity)); 1561437c7c90SJeremy L Thompson data->assembled_bases_in[b] = B_in; 1562437c7c90SJeremy L Thompson } 1563ed9e99e6SJeremy L Thompson } 1564ed9e99e6SJeremy L Thompson 1565437c7c90SJeremy L Thompson if (assembled_bases_out && !data->assembled_bases_out[0]) { 1566437c7c90SJeremy L Thompson CeedInt num_qpts; 1567437c7c90SJeremy L Thompson 1568506b1a0cSSebastian Grimberg if (data->active_bases_out[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[0], &num_qpts)); 1569506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_out[0], &num_qpts)); 1570506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < data->num_active_bases_out; b++) { 1571ed9e99e6SJeremy L Thompson bool has_eval_none = false; 15721c66c397SJeremy L Thompson CeedInt num_nodes; 1573437c7c90SJeremy L Thompson CeedScalar *B_out = NULL, *identity = NULL; 1574ed9e99e6SJeremy L Thompson 1575506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[b], &num_nodes)); 1576352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_out[b], &B_out)); 1577ed9e99e6SJeremy L Thompson 1578437c7c90SJeremy L Thompson for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) { 1579437c7c90SJeremy L Thompson has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE); 1580ed9e99e6SJeremy L Thompson } 1581ed9e99e6SJeremy L Thompson if (has_eval_none) { 1582352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes, &identity)); 1583352a5e7cSSebastian Grimberg for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) { 1584352a5e7cSSebastian Grimberg identity[i * num_nodes + i] = 1.0; 1585ed9e99e6SJeremy L Thompson } 1586ed9e99e6SJeremy L Thompson } 1587ed9e99e6SJeremy L Thompson 1588ed9e99e6SJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 1589352a5e7cSSebastian Grimberg for (CeedInt n = 0; n < num_nodes; n++) { 1590352a5e7cSSebastian Grimberg CeedInt d_out = 0, q_comp_out; 1591352a5e7cSSebastian Grimberg CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE; 15921c66c397SJeremy L Thompson 1593437c7c90SJeremy L Thompson for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) { 1594437c7c90SJeremy L Thompson const CeedInt qq = data->num_eval_modes_out[b] * q; 1595437c7c90SJeremy L Thompson const CeedScalar *B = NULL; 15961c66c397SJeremy L Thompson 1597506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(data->active_bases_out[b], data->eval_modes_out[b][e_out], identity, &B)); 1598506b1a0cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_out[b], data->eval_modes_out[b][e_out], &q_comp_out)); 1599352a5e7cSSebastian Grimberg if (q_comp_out > 1) { 1600352a5e7cSSebastian Grimberg if (e_out == 0 || data->eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0; 1601352a5e7cSSebastian Grimberg else B = &B[(++d_out) * num_qpts * num_nodes]; 1602352a5e7cSSebastian Grimberg } 1603352a5e7cSSebastian Grimberg eval_mode_out_prev = data->eval_modes_out[b][e_out]; 1604352a5e7cSSebastian Grimberg B_out[(qq + e_out) * num_nodes + n] = B[q * num_nodes + n]; 1605ed9e99e6SJeremy L Thompson } 1606ed9e99e6SJeremy L Thompson } 1607ed9e99e6SJeremy L Thompson } 16087c1dbaffSSebastian Grimberg if (identity) CeedCall(CeedFree(&identity)); 1609437c7c90SJeremy L Thompson data->assembled_bases_out[b] = B_out; 1610437c7c90SJeremy L Thompson } 1611ed9e99e6SJeremy L Thompson } 1612ed9e99e6SJeremy L Thompson 1613437c7c90SJeremy L Thompson // Pass out assembled data 1614506b1a0cSSebastian Grimberg if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in; 1615506b1a0cSSebastian Grimberg if (active_bases_in) *active_bases_in = data->active_bases_in; 1616437c7c90SJeremy L Thompson if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in; 1617506b1a0cSSebastian Grimberg if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out; 1618506b1a0cSSebastian Grimberg if (active_bases_out) *active_bases_out = data->active_bases_out; 1619437c7c90SJeremy L Thompson if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out; 1620437c7c90SJeremy L Thompson return CEED_ERROR_SUCCESS; 1621437c7c90SJeremy L Thompson } 1622437c7c90SJeremy L Thompson 1623437c7c90SJeremy L Thompson /** 1624ca94c3ddSJeremy L Thompson @brief Get `CeedOperator` `CeedBasis` data for assembly. 1625ba746a46SJeremy L Thompson 1626ca94c3ddSJeremy L Thompson Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object. 1627437c7c90SJeremy L Thompson 1628ca94c3ddSJeremy L Thompson @param[in] data `CeedOperatorAssemblyData` 1629ca94c3ddSJeremy L Thompson @param[out] num_active_elem_rstrs_in Number of active input element restrictions, or `NULL` 1630ca94c3ddSJeremy L Thompson @param[out] active_elem_rstrs_in Pointer to hold active input `CeedElemRestriction`, or `NULL` 1631ca94c3ddSJeremy L Thompson @param[out] num_active_elem_rstrs_out Number of active output element restrictions, or `NULL` 1632ca94c3ddSJeremy L Thompson @param[out] active_elem_rstrs_out Pointer to hold active output `CeedElemRestriction`, or `NULL` 1633437c7c90SJeremy L Thompson 1634437c7c90SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1635437c7c90SJeremy L Thompson 1636437c7c90SJeremy L Thompson @ref Backend 1637437c7c90SJeremy L Thompson **/ 1638506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs_in, 1639506b1a0cSSebastian Grimberg CeedElemRestriction **active_elem_rstrs_in, CeedInt *num_active_elem_rstrs_out, 1640506b1a0cSSebastian Grimberg CeedElemRestriction **active_elem_rstrs_out) { 1641506b1a0cSSebastian Grimberg if (num_active_elem_rstrs_in) *num_active_elem_rstrs_in = data->num_active_bases_in; 1642506b1a0cSSebastian Grimberg if (active_elem_rstrs_in) *active_elem_rstrs_in = data->active_elem_rstrs_in; 1643506b1a0cSSebastian Grimberg if (num_active_elem_rstrs_out) *num_active_elem_rstrs_out = data->num_active_bases_out; 1644506b1a0cSSebastian Grimberg if (active_elem_rstrs_out) *active_elem_rstrs_out = data->active_elem_rstrs_out; 1645ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1646ed9e99e6SJeremy L Thompson } 1647ed9e99e6SJeremy L Thompson 1648ed9e99e6SJeremy L Thompson /** 1649ca94c3ddSJeremy L Thompson @brief Destroy `CeedOperatorAssemblyData` 1650ed9e99e6SJeremy L Thompson 1651ca94c3ddSJeremy L Thompson @param[in,out] data `CeedOperatorAssemblyData` to destroy 1652ed9e99e6SJeremy L Thompson 1653ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1654ed9e99e6SJeremy L Thompson 1655ed9e99e6SJeremy L Thompson @ref Backend 1656ed9e99e6SJeremy L Thompson **/ 1657ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) { 1658ad6481ceSJeremy L Thompson if (!*data) { 1659ad6481ceSJeremy L Thompson *data = NULL; 1660ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1661ad6481ceSJeremy L Thompson } 16622b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*data)->ceed)); 1663506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < (*data)->num_active_bases_in; b++) { 1664506b1a0cSSebastian Grimberg CeedCall(CeedBasisDestroy(&(*data)->active_bases_in[b])); 1665506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_in[b])); 1666437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_modes_in[b])); 1667437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b])); 1668437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_in[b])); 1669506b1a0cSSebastian Grimberg } 1670506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < (*data)->num_active_bases_out; b++) { 1671506b1a0cSSebastian Grimberg CeedCall(CeedBasisDestroy(&(*data)->active_bases_out[b])); 1672506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_out[b])); 1673506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->eval_modes_out[b])); 1674506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b])); 1675437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_out[b])); 1676437c7c90SJeremy L Thompson } 1677506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_bases_in)); 1678506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_bases_out)); 1679506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_elem_rstrs_in)); 1680506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_elem_rstrs_out)); 1681437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->num_eval_modes_in)); 1682437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->num_eval_modes_out)); 1683437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_modes_in)); 1684437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_modes_out)); 1685437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_mode_offsets_in)); 1686437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_mode_offsets_out)); 1687437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_in)); 1688437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_out)); 1689ed9e99e6SJeremy L Thompson 16902b730f8bSJeremy L Thompson CeedCall(CeedFree(data)); 1691ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1692ed9e99e6SJeremy L Thompson } 1693ed9e99e6SJeremy L Thompson 16944dd1a9d2SSebastian Grimberg /** 1695ca94c3ddSJeremy L Thompson @brief Retrieve fallback `CeedOperator` with a reference `Ceed` for advanced `CeedOperator` functionality 16964dd1a9d2SSebastian Grimberg 1697ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to retrieve fallback for 1698ca94c3ddSJeremy L Thompson @param[out] op_fallback Fallback `CeedOperator` 16994dd1a9d2SSebastian Grimberg 17004dd1a9d2SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 17014dd1a9d2SSebastian Grimberg 17024dd1a9d2SSebastian Grimberg @ref Backend 17034dd1a9d2SSebastian Grimberg **/ 17044dd1a9d2SSebastian Grimberg int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) { 17054dd1a9d2SSebastian Grimberg // Create if needed 17064dd1a9d2SSebastian Grimberg if (!op->op_fallback) CeedCall(CeedOperatorCreateFallback(op)); 17074dd1a9d2SSebastian Grimberg if (op->op_fallback) { 17084dd1a9d2SSebastian Grimberg bool is_debug; 17091203703bSJeremy L Thompson Ceed ceed; 17104dd1a9d2SSebastian Grimberg 17114dd1a9d2SSebastian Grimberg CeedCall(CeedOperatorGetCeed(op, &ceed)); 17121203703bSJeremy L Thompson CeedCall(CeedIsDebug(ceed, &is_debug)); 17131203703bSJeremy L Thompson if (is_debug) { 17141203703bSJeremy L Thompson Ceed ceed_fallback; 17151203703bSJeremy L Thompson const char *resource, *resource_fallback; 17161203703bSJeremy L Thompson 17174dd1a9d2SSebastian Grimberg CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback)); 17184dd1a9d2SSebastian Grimberg CeedCall(CeedGetResource(ceed, &resource)); 17194dd1a9d2SSebastian Grimberg CeedCall(CeedGetResource(ceed_fallback, &resource_fallback)); 17204dd1a9d2SSebastian Grimberg 17214dd1a9d2SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n"); 1722249f8407SJeremy L Thompson CeedDebug(ceed, "Falling back from %s operator at address %p to %s operator at address %p\n", resource, op, resource_fallback, op->op_fallback); 17234dd1a9d2SSebastian Grimberg } 17244dd1a9d2SSebastian Grimberg } 17254dd1a9d2SSebastian Grimberg *op_fallback = op->op_fallback; 17264dd1a9d2SSebastian Grimberg return CEED_ERROR_SUCCESS; 17274dd1a9d2SSebastian Grimberg } 17284dd1a9d2SSebastian Grimberg 17294dd1a9d2SSebastian Grimberg /** 1730ca94c3ddSJeremy L Thompson @brief Get the parent `CeedOperator` for a fallback `CeedOperator` 17314dd1a9d2SSebastian Grimberg 1732ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` context 1733ca94c3ddSJeremy L Thompson @param[out] parent Variable to store parent `CeedOperator` context 17344dd1a9d2SSebastian Grimberg 17354dd1a9d2SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 17364dd1a9d2SSebastian Grimberg 17374dd1a9d2SSebastian Grimberg @ref Backend 17384dd1a9d2SSebastian Grimberg **/ 17394dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParent(CeedOperator op, CeedOperator *parent) { 17404dd1a9d2SSebastian Grimberg *parent = op->op_fallback_parent ? op->op_fallback_parent : NULL; 17414dd1a9d2SSebastian Grimberg return CEED_ERROR_SUCCESS; 17424dd1a9d2SSebastian Grimberg } 17434dd1a9d2SSebastian Grimberg 17444dd1a9d2SSebastian Grimberg /** 1745ca94c3ddSJeremy L Thompson @brief Get the `Ceed` context of the parent `CeedOperator` for a fallback `CeedOperator` 17464dd1a9d2SSebastian Grimberg 1747ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` context 1748ca94c3ddSJeremy L Thompson @param[out] parent Variable to store parent `Ceed` context 17494dd1a9d2SSebastian Grimberg 17504dd1a9d2SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 17514dd1a9d2SSebastian Grimberg 17524dd1a9d2SSebastian Grimberg @ref Backend 17534dd1a9d2SSebastian Grimberg **/ 17544dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParentCeed(CeedOperator op, Ceed *parent) { 17554dd1a9d2SSebastian Grimberg *parent = op->op_fallback_parent ? op->op_fallback_parent->ceed : op->ceed; 17564dd1a9d2SSebastian Grimberg return CEED_ERROR_SUCCESS; 17574dd1a9d2SSebastian Grimberg } 17584dd1a9d2SSebastian Grimberg 1759480fae85SJeremy L Thompson /// @} 1760480fae85SJeremy L Thompson 1761480fae85SJeremy L Thompson /// ---------------------------------------------------------------------------- 1762eaf62fffSJeremy L Thompson /// CeedOperator Public API 1763eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 1764eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser 1765eaf62fffSJeremy L Thompson /// @{ 1766eaf62fffSJeremy L Thompson 1767eaf62fffSJeremy L Thompson /** 1768ca94c3ddSJeremy L Thompson @brief Assemble a linear `CeedQFunction` associated with a `CeedOperator`. 1769eaf62fffSJeremy L Thompson 1770ca94c3ddSJeremy L Thompson This returns a `CeedVector` containing a matrix at each quadrature point providing the action of the `CeedQFunction` associated with the `CeedOperator`. 1771ca94c3ddSJeremy 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. 1772859c15bbSJames Wright 1773ca94c3ddSJeremy L Thompson Inputs and outputs are in the order provided by the user when adding `CeedOperator` fields. 1774ca94c3ddSJeremy 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]`. 1775eaf62fffSJeremy L Thompson 1776ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 1777f04ea552SJeremy L Thompson 1778ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 1779ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points 1780ca94c3ddSJeremy L Thompson @param[out] rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction` 1781ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1782eaf62fffSJeremy L Thompson 1783eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1784eaf62fffSJeremy L Thompson 1785eaf62fffSJeremy L Thompson @ref User 1786eaf62fffSJeremy L Thompson **/ 17872b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 17882b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 1789eaf62fffSJeremy L Thompson 1790eaf62fffSJeremy L Thompson if (op->LinearAssembleQFunction) { 1791d04bbc78SJeremy L Thompson // Backend version 17922b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request)); 1793eaf62fffSJeremy L Thompson } else { 1794d04bbc78SJeremy L Thompson // Operator fallback 17951203703bSJeremy L Thompson Ceed ceed; 1796d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1797d04bbc78SJeremy L Thompson 17981203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 17992b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 18006574a04fSJeremy L Thompson if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request)); 18011203703bSJeremy L Thompson else return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction"); 180270a7ffb3SJeremy L Thompson } 1803eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1804eaf62fffSJeremy L Thompson } 180570a7ffb3SJeremy L Thompson 180670a7ffb3SJeremy L Thompson /** 1807ca94c3ddSJeremy L Thompson @brief Assemble `CeedQFunction` and store result internally. 18084385fb7fSSebastian Grimberg 1809ea61e9acSJeremy L Thompson Return copied references of stored data to the caller. 1810ea61e9acSJeremy L Thompson Caller is responsible for ownership and destruction of the copied references. 1811ca94c3ddSJeremy L Thompson See also @ref CeedOperatorLinearAssembleQFunction(). 181270a7ffb3SJeremy L Thompson 1813ca94c3ddSJeremy 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. 1814c5f45aeaSJeremy L Thompson These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object. 1815c5f45aeaSJeremy L Thompson 1816ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 1817ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points 1818ca94c3ddSJeremy L Thompson @param[out] rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction` 1819ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 182070a7ffb3SJeremy L Thompson 182170a7ffb3SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 182270a7ffb3SJeremy L Thompson 182370a7ffb3SJeremy L Thompson @ref User 182470a7ffb3SJeremy L Thompson **/ 18252b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 1826b05f7e9fSJeremy L Thompson int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *) = NULL; 1827b05f7e9fSJeremy L Thompson CeedOperator op_assemble = NULL; 1828bb229da9SJeremy L Thompson CeedOperator op_fallback_parent = NULL; 1829b05f7e9fSJeremy L Thompson 18302b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 183170a7ffb3SJeremy L Thompson 1832b05f7e9fSJeremy L Thompson // Determine if fallback parent or operator has implementation 1833bb229da9SJeremy L Thompson CeedCall(CeedOperatorGetFallbackParent(op, &op_fallback_parent)); 1834bb229da9SJeremy L Thompson if (op_fallback_parent && op_fallback_parent->LinearAssembleQFunctionUpdate) { 1835b05f7e9fSJeremy L Thompson // -- Backend version for op fallback parent is faster, if it exists 1836bb229da9SJeremy L Thompson LinearAssembleQFunctionUpdate = op_fallback_parent->LinearAssembleQFunctionUpdate; 1837bb229da9SJeremy L Thompson op_assemble = op_fallback_parent; 1838b05f7e9fSJeremy L Thompson } else if (op->LinearAssembleQFunctionUpdate) { 1839b05f7e9fSJeremy L Thompson // -- Backend version for op 1840b05f7e9fSJeremy L Thompson LinearAssembleQFunctionUpdate = op->LinearAssembleQFunctionUpdate; 1841b05f7e9fSJeremy L Thompson op_assemble = op; 1842b05f7e9fSJeremy L Thompson } 1843b05f7e9fSJeremy L Thompson 1844b05f7e9fSJeremy L Thompson // Assemble QFunction 1845b05f7e9fSJeremy L Thompson if (LinearAssembleQFunctionUpdate) { 1846b05f7e9fSJeremy L Thompson // Backend or fallback parent version 1847480fae85SJeremy L Thompson bool qf_assembled_is_setup; 18482efa2d85SJeremy L Thompson CeedVector assembled_vec = NULL; 18492efa2d85SJeremy L Thompson CeedElemRestriction assembled_rstr = NULL; 1850480fae85SJeremy L Thompson 18512b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataIsSetup(op->qf_assembled, &qf_assembled_is_setup)); 1852480fae85SJeremy L Thompson if (qf_assembled_is_setup) { 1853d04bbc78SJeremy L Thompson bool update_needed; 1854d04bbc78SJeremy L Thompson 18552b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataGetObjects(op->qf_assembled, &assembled_vec, &assembled_rstr)); 18562b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(op->qf_assembled, &update_needed)); 1857b05f7e9fSJeremy L Thompson if (update_needed) CeedCall(LinearAssembleQFunctionUpdate(op_assemble, assembled_vec, assembled_rstr, request)); 185870a7ffb3SJeremy L Thompson } else { 1859b05f7e9fSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleQFunction(op_assemble, &assembled_vec, &assembled_rstr, request)); 18602b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataSetObjects(op->qf_assembled, assembled_vec, assembled_rstr)); 186170a7ffb3SJeremy L Thompson } 18622b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, false)); 18632efa2d85SJeremy L Thompson 1864d04bbc78SJeremy L Thompson // Copy reference from internally held copy 18652b730f8bSJeremy L Thompson CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled)); 18662b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr)); 1867c5f45aeaSJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled_vec)); 18682b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&assembled_rstr)); 186970a7ffb3SJeremy L Thompson } else { 1870d04bbc78SJeremy L Thompson // Operator fallback 18711203703bSJeremy L Thompson Ceed ceed; 1872d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1873d04bbc78SJeremy L Thompson 18741203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 18752b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 18766574a04fSJeremy L Thompson if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request)); 18771203703bSJeremy L Thompson else return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate"); 187870a7ffb3SJeremy L Thompson } 187970a7ffb3SJeremy L Thompson return CEED_ERROR_SUCCESS; 1880eaf62fffSJeremy L Thompson } 1881eaf62fffSJeremy L Thompson 1882eaf62fffSJeremy L Thompson /** 1883ca94c3ddSJeremy L Thompson @brief Assemble the diagonal of a square linear `CeedOperator` 1884eaf62fffSJeremy L Thompson 1885ca94c3ddSJeremy L Thompson This overwrites a `CeedVector` with the diagonal of a linear `CeedOperator`. 1886eaf62fffSJeremy L Thompson 1887ca94c3ddSJeremy L Thompson Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported. 1888eaf62fffSJeremy L Thompson 1889ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 1890f04ea552SJeremy L Thompson 1891ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 1892ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal 1893ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1894eaf62fffSJeremy L Thompson 1895eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1896eaf62fffSJeremy L Thompson 1897eaf62fffSJeremy L Thompson @ref User 1898eaf62fffSJeremy L Thompson **/ 18992b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 1900f3d47e36SJeremy L Thompson bool is_composite; 19011c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 19021203703bSJeremy L Thompson Ceed ceed; 19031c66c397SJeremy L Thompson 19041203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 19052b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 1906f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1907eaf62fffSJeremy L Thompson 19082b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 19091203703bSJeremy L Thompson CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 1910c9366a6bSJeremy L Thompson 1911f3d47e36SJeremy L Thompson // Early exit for empty operator 1912f3d47e36SJeremy L Thompson if (!is_composite) { 1913f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 1914f3d47e36SJeremy L Thompson 1915f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 1916f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 1917f3d47e36SJeremy L Thompson } 1918f3d47e36SJeremy L Thompson 1919eaf62fffSJeremy L Thompson if (op->LinearAssembleDiagonal) { 1920d04bbc78SJeremy L Thompson // Backend version 19212b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleDiagonal(op, assembled, request)); 1922eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1923eaf62fffSJeremy L Thompson } else if (op->LinearAssembleAddDiagonal) { 1924d04bbc78SJeremy L Thompson // Backend version with zeroing first 19252b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 19262b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request)); 1927eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1928eaf62fffSJeremy L Thompson } else { 1929d04bbc78SJeremy L Thompson // Operator fallback 1930d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1931d04bbc78SJeremy L Thompson 19322b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 1933d04bbc78SJeremy L Thompson if (op_fallback) { 19342b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request)); 1935eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1936eaf62fffSJeremy L Thompson } 1937eaf62fffSJeremy L Thompson } 1938eaf62fffSJeremy L Thompson // Default interface implementation 19392b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 19402b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request)); 1941eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1942eaf62fffSJeremy L Thompson } 1943eaf62fffSJeremy L Thompson 1944eaf62fffSJeremy L Thompson /** 1945ca94c3ddSJeremy L Thompson @brief Assemble the diagonal of a square linear `CeedOperator`. 1946eaf62fffSJeremy L Thompson 1947ca94c3ddSJeremy L Thompson This sums into a `CeedVector` the diagonal of a linear `CeedOperator`. 1948eaf62fffSJeremy L Thompson 1949ca94c3ddSJeremy L Thompson Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported. 1950eaf62fffSJeremy L Thompson 1951ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 1952f04ea552SJeremy L Thompson 1953ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 1954ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal 1955ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1956eaf62fffSJeremy L Thompson 1957eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1958eaf62fffSJeremy L Thompson 1959eaf62fffSJeremy L Thompson @ref User 1960eaf62fffSJeremy L Thompson **/ 19612b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 1962f3d47e36SJeremy L Thompson bool is_composite; 19631c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 19641203703bSJeremy L Thompson Ceed ceed; 19651c66c397SJeremy L Thompson 19661203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 19672b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 1968f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1969eaf62fffSJeremy L Thompson 19702b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 19711203703bSJeremy L Thompson CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 1972c9366a6bSJeremy L Thompson 1973f3d47e36SJeremy L Thompson // Early exit for empty operator 1974f3d47e36SJeremy L Thompson if (!is_composite) { 1975f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 1976f3d47e36SJeremy L Thompson 1977f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 1978f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 1979f3d47e36SJeremy L Thompson } 1980f3d47e36SJeremy L Thompson 1981eaf62fffSJeremy L Thompson if (op->LinearAssembleAddDiagonal) { 1982d04bbc78SJeremy L Thompson // Backend version 19832b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request)); 1984eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1985eaf62fffSJeremy L Thompson } else { 1986d04bbc78SJeremy L Thompson // Operator fallback 1987d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1988d04bbc78SJeremy L Thompson 19892b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 1990d04bbc78SJeremy L Thompson if (op_fallback) { 19912b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request)); 1992eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1993eaf62fffSJeremy L Thompson } 1994eaf62fffSJeremy L Thompson } 1995eaf62fffSJeremy L Thompson // Default interface implementation 1996eaf62fffSJeremy L Thompson if (is_composite) { 19972b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled)); 1998eaf62fffSJeremy L Thompson } else { 1999*f3bd9308SJeremy L Thompson CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal(op, request, false, assembled)); 2000eaf62fffSJeremy L Thompson } 2001d04bbc78SJeremy L Thompson return CEED_ERROR_SUCCESS; 2002eaf62fffSJeremy L Thompson } 2003eaf62fffSJeremy L Thompson 2004eaf62fffSJeremy L Thompson /** 2005ca94c3ddSJeremy L Thompson @brief Fully assemble the point-block diagonal pattern of a linear `CeedOperator`. 200601f0e615SJames Wright 2007ca94c3ddSJeremy L Thompson Expected to be used in conjunction with @ref CeedOperatorLinearAssemblePointBlockDiagonal(). 200801f0e615SJames Wright 2009ca94c3ddSJeremy 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)`. 2010ca94c3ddSJeremy L Thompson Note that the `(i, j)` pairs are unique. 2011ca94c3ddSJeremy L Thompson This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemblePointBlockDiagonal() provides the values in the same ordering. 201201f0e615SJames Wright 201301f0e615SJames Wright This will generally be slow unless your operator is low-order. 201401f0e615SJames Wright 2015ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 201601f0e615SJames Wright 2017ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 201801f0e615SJames Wright @param[out] num_entries Number of entries in coordinate nonzero pattern 201901f0e615SJames Wright @param[out] rows Row number for each entry 202001f0e615SJames Wright @param[out] cols Column number for each entry 202101f0e615SJames Wright 202201f0e615SJames Wright @ref User 202301f0e615SJames Wright **/ 202401f0e615SJames Wright int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) { 202501f0e615SJames Wright Ceed ceed; 202601f0e615SJames Wright bool is_composite; 202701f0e615SJames Wright CeedInt num_active_components, num_sub_operators; 202801f0e615SJames Wright CeedOperator *sub_operators; 202901f0e615SJames Wright 203001f0e615SJames Wright CeedCall(CeedOperatorGetCeed(op, &ceed)); 203101f0e615SJames Wright CeedCall(CeedOperatorIsComposite(op, &is_composite)); 203201f0e615SJames Wright 203301f0e615SJames Wright CeedSize input_size = 0, output_size = 0; 203401f0e615SJames Wright CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 203501f0e615SJames Wright CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 203601f0e615SJames Wright 203701f0e615SJames Wright if (is_composite) { 203801f0e615SJames Wright CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_operators)); 203901f0e615SJames Wright CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 204001f0e615SJames Wright } else { 204101f0e615SJames Wright sub_operators = &op; 204201f0e615SJames Wright num_sub_operators = 1; 204301f0e615SJames Wright } 204401f0e615SJames Wright 2045506b1a0cSSebastian Grimberg // Verify operator can be assembled correctly 2046506b1a0cSSebastian Grimberg { 204701f0e615SJames Wright CeedOperatorAssemblyData data; 2048506b1a0cSSebastian Grimberg CeedInt num_active_elem_rstrs, comp_stride; 204901f0e615SJames Wright CeedElemRestriction *active_elem_rstrs; 205001f0e615SJames Wright 205101f0e615SJames Wright // Get initial values to check against 205201f0e615SJames Wright CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[0], &data)); 2053506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL)); 205401f0e615SJames Wright CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[0], &comp_stride)); 205501f0e615SJames Wright CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[0], &num_active_components)); 205601f0e615SJames Wright 2057506b1a0cSSebastian Grimberg // Verify that all active element restrictions have same component stride and number of components 205801f0e615SJames Wright for (CeedInt k = 0; k < num_sub_operators; k++) { 205901f0e615SJames Wright CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[k], &data)); 2060506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL)); 206101f0e615SJames Wright for (CeedInt i = 0; i < num_active_elem_rstrs; i++) { 2062506b1a0cSSebastian Grimberg CeedInt comp_stride_sub, num_active_components_sub; 2063506b1a0cSSebastian Grimberg 206401f0e615SJames Wright CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[i], &comp_stride_sub)); 206501f0e615SJames Wright CeedCheck(comp_stride == comp_stride_sub, ceed, CEED_ERROR_DIMENSION, 206601f0e615SJames Wright "Active element restrictions must have the same component stride: %d vs %d", comp_stride, comp_stride_sub); 206701f0e615SJames Wright CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[i], &num_active_components_sub)); 206801f0e615SJames Wright CeedCheck(num_active_components == num_active_components_sub, ceed, CEED_ERROR_INCOMPATIBLE, 206901f0e615SJames Wright "All suboperators must have the same number of output components"); 207001f0e615SJames Wright } 207101f0e615SJames Wright } 207201f0e615SJames Wright } 207301f0e615SJames Wright *num_entries = input_size * num_active_components; 207401f0e615SJames Wright CeedCall(CeedCalloc(*num_entries, rows)); 207501f0e615SJames Wright CeedCall(CeedCalloc(*num_entries, cols)); 207601f0e615SJames Wright 207701f0e615SJames Wright for (CeedInt o = 0; o < num_sub_operators; o++) { 2078506b1a0cSSebastian Grimberg CeedElemRestriction active_elem_rstr, point_block_active_elem_rstr; 207901f0e615SJames Wright CeedInt comp_stride, num_elem, elem_size; 2080506b1a0cSSebastian Grimberg const CeedInt *offsets, *point_block_offsets; 208101f0e615SJames Wright 208201f0e615SJames Wright CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[o], &active_elem_rstr)); 208301f0e615SJames Wright CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstr, &comp_stride)); 208401f0e615SJames Wright CeedCall(CeedElemRestrictionGetNumElements(active_elem_rstr, &num_elem)); 208501f0e615SJames Wright CeedCall(CeedElemRestrictionGetElementSize(active_elem_rstr, &elem_size)); 208601f0e615SJames Wright CeedCall(CeedElemRestrictionGetOffsets(active_elem_rstr, CEED_MEM_HOST, &offsets)); 208701f0e615SJames Wright 2088506b1a0cSSebastian Grimberg CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstr, &point_block_active_elem_rstr)); 2089506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOffsets(point_block_active_elem_rstr, CEED_MEM_HOST, &point_block_offsets)); 209001f0e615SJames Wright 209101f0e615SJames Wright for (CeedSize i = 0; i < num_elem * elem_size; i++) { 209201f0e615SJames Wright for (CeedInt c_out = 0; c_out < num_active_components; c_out++) { 209301f0e615SJames Wright for (CeedInt c_in = 0; c_in < num_active_components; c_in++) { 2094506b1a0cSSebastian Grimberg (*rows)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_out * comp_stride; 2095506b1a0cSSebastian Grimberg (*cols)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_in * comp_stride; 209601f0e615SJames Wright } 209701f0e615SJames Wright } 209801f0e615SJames Wright } 209901f0e615SJames Wright 210001f0e615SJames Wright CeedCall(CeedElemRestrictionRestoreOffsets(active_elem_rstr, &offsets)); 2101506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOffsets(point_block_active_elem_rstr, &point_block_offsets)); 2102506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&point_block_active_elem_rstr)); 210301f0e615SJames Wright } 210401f0e615SJames Wright return CEED_ERROR_SUCCESS; 210501f0e615SJames Wright } 210601f0e615SJames Wright 210701f0e615SJames Wright /** 2108ca94c3ddSJeremy L Thompson @brief Assemble the point block diagonal of a square linear `CeedOperator`. 2109eaf62fffSJeremy L Thompson 2110ca94c3ddSJeremy L Thompson This overwrites a `CeedVector` with the point block diagonal of a linear `CeedOperator`. 2111eaf62fffSJeremy L Thompson 2112ca94c3ddSJeremy L Thompson Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported. 2113eaf62fffSJeremy L Thompson 2114ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2115f04ea552SJeremy L Thompson 2116ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 2117ca94c3ddSJeremy 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. 2118ca94c3ddSJeremy L Thompson The dimensions of this vector are derived from the active vector for the `CeedOperator`. 2119ca94c3ddSJeremy L Thompson The array has shape `[nodes, component out, component in]`. 2120ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2121eaf62fffSJeremy L Thompson 2122eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2123eaf62fffSJeremy L Thompson 2124eaf62fffSJeremy L Thompson @ref User 2125eaf62fffSJeremy L Thompson **/ 21262b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 2127f3d47e36SJeremy L Thompson bool is_composite; 21281c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 21291203703bSJeremy L Thompson Ceed ceed; 21301c66c397SJeremy L Thompson 21311203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 21322b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2133f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2134eaf62fffSJeremy L Thompson 21352b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 21361203703bSJeremy L Thompson CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 2137c9366a6bSJeremy L Thompson 2138f3d47e36SJeremy L Thompson // Early exit for empty operator 2139f3d47e36SJeremy L Thompson if (!is_composite) { 2140f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 2141f3d47e36SJeremy L Thompson 2142f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 2143f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 2144f3d47e36SJeremy L Thompson } 2145f3d47e36SJeremy L Thompson 2146eaf62fffSJeremy L Thompson if (op->LinearAssemblePointBlockDiagonal) { 2147d04bbc78SJeremy L Thompson // Backend version 21482b730f8bSJeremy L Thompson CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request)); 2149eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2150eaf62fffSJeremy L Thompson } else if (op->LinearAssembleAddPointBlockDiagonal) { 2151d04bbc78SJeremy L Thompson // Backend version with zeroing first 21522b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 21532b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request)); 2154eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2155eaf62fffSJeremy L Thompson } else { 2156d04bbc78SJeremy L Thompson // Operator fallback 2157d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2158d04bbc78SJeremy L Thompson 21592b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2160d04bbc78SJeremy L Thompson if (op_fallback) { 21612b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request)); 2162eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2163eaf62fffSJeremy L Thompson } 2164eaf62fffSJeremy L Thompson } 2165eaf62fffSJeremy L Thompson // Default interface implementation 21662b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 21672b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request)); 2168eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2169eaf62fffSJeremy L Thompson } 2170eaf62fffSJeremy L Thompson 2171eaf62fffSJeremy L Thompson /** 2172ca94c3ddSJeremy L Thompson @brief Assemble the point block diagonal of a square linear `CeedOperator`. 2173eaf62fffSJeremy L Thompson 2174ca94c3ddSJeremy L Thompson This sums into a `CeedVector` with the point block diagonal of a linear `CeedOperator`. 2175eaf62fffSJeremy L Thompson 2176ca94c3ddSJeremy L Thompson Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported. 2177eaf62fffSJeremy L Thompson 2178ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2179f04ea552SJeremy L Thompson 2180ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 2181ca94c3ddSJeremy 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. 2182ca94c3ddSJeremy L Thompson The dimensions of this vector are derived from the active vector for the `CeedOperator`. 2183ca94c3ddSJeremy L Thompson The array has shape `[nodes, component out, component in]`. 2184ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2185eaf62fffSJeremy L Thompson 2186eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2187eaf62fffSJeremy L Thompson 2188eaf62fffSJeremy L Thompson @ref User 2189eaf62fffSJeremy L Thompson **/ 21902b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 2191f3d47e36SJeremy L Thompson bool is_composite; 21921c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 21931203703bSJeremy L Thompson Ceed ceed; 21941c66c397SJeremy L Thompson 21951203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 21962b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2197f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2198eaf62fffSJeremy L Thompson 21992b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 22001203703bSJeremy L Thompson CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 2201c9366a6bSJeremy L Thompson 2202f3d47e36SJeremy L Thompson // Early exit for empty operator 2203f3d47e36SJeremy L Thompson if (!is_composite) { 2204f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 2205f3d47e36SJeremy L Thompson 2206f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 2207f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 2208f3d47e36SJeremy L Thompson } 2209f3d47e36SJeremy L Thompson 2210eaf62fffSJeremy L Thompson if (op->LinearAssembleAddPointBlockDiagonal) { 2211d04bbc78SJeremy L Thompson // Backend version 22122b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request)); 2213eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2214eaf62fffSJeremy L Thompson } else { 2215d04bbc78SJeremy L Thompson // Operator fallback 2216d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2217d04bbc78SJeremy L Thompson 22182b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2219d04bbc78SJeremy L Thompson if (op_fallback) { 22202b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request)); 2221eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2222eaf62fffSJeremy L Thompson } 2223eaf62fffSJeremy L Thompson } 2224ea61e9acSJeremy L Thompson // Default interface implementation 2225eaf62fffSJeremy L Thompson if (is_composite) { 22262b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled)); 2227eaf62fffSJeremy L Thompson } else { 2228*f3bd9308SJeremy L Thompson CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal(op, request, true, assembled)); 2229eaf62fffSJeremy L Thompson } 2230d04bbc78SJeremy L Thompson return CEED_ERROR_SUCCESS; 2231eaf62fffSJeremy L Thompson } 2232eaf62fffSJeremy L Thompson 2233eaf62fffSJeremy L Thompson /** 2234ca94c3ddSJeremy L Thompson @brief Fully assemble the nonzero pattern of a linear `CeedOperator`. 2235eaf62fffSJeremy L Thompson 2236ca94c3ddSJeremy L Thompson Expected to be used in conjunction with @ref CeedOperatorLinearAssemble(). 2237eaf62fffSJeremy L Thompson 2238ca94c3ddSJeremy 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)`. 2239ca94c3ddSJeremy L Thompson Note that the `(i, j)` pairs are not unique and may repeat. 2240ca94c3ddSJeremy L Thompson This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemble() provides the values in the same ordering. 2241eaf62fffSJeremy L Thompson 2242eaf62fffSJeremy L Thompson This will generally be slow unless your operator is low-order. 2243eaf62fffSJeremy L Thompson 2244ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2245f04ea552SJeremy L Thompson 2246ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 2247eaf62fffSJeremy L Thompson @param[out] num_entries Number of entries in coordinate nonzero pattern 2248eaf62fffSJeremy L Thompson @param[out] rows Row number for each entry 2249eaf62fffSJeremy L Thompson @param[out] cols Column number for each entry 2250eaf62fffSJeremy L Thompson 2251eaf62fffSJeremy L Thompson @ref User 2252eaf62fffSJeremy L Thompson **/ 22532b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) { 22541c66c397SJeremy L Thompson bool is_composite; 22551c66c397SJeremy L Thompson CeedInt num_suboperators, offset = 0; 2256b94338b9SJed Brown CeedSize single_entries; 2257eaf62fffSJeremy L Thompson CeedOperator *sub_operators; 22581c66c397SJeremy L Thompson 22592b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2260f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2261eaf62fffSJeremy L Thompson 2262eaf62fffSJeremy L Thompson if (op->LinearAssembleSymbolic) { 2263d04bbc78SJeremy L Thompson // Backend version 22642b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols)); 2265eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2266eaf62fffSJeremy L Thompson } else { 2267d04bbc78SJeremy L Thompson // Operator fallback 2268d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2269d04bbc78SJeremy L Thompson 22702b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2271d04bbc78SJeremy L Thompson if (op_fallback) { 22722b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols)); 2273eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2274eaf62fffSJeremy L Thompson } 2275eaf62fffSJeremy L Thompson } 2276eaf62fffSJeremy L Thompson 2277eaf62fffSJeremy L Thompson // Default interface implementation 2278eaf62fffSJeremy L Thompson 2279506b1a0cSSebastian Grimberg // Count entries and allocate rows, cols arrays 2280eaf62fffSJeremy L Thompson *num_entries = 0; 2281eaf62fffSJeremy L Thompson if (is_composite) { 2282c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2283c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 228492ae7e47SJeremy L Thompson for (CeedInt k = 0; k < num_suboperators; ++k) { 22852b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries)); 2286eaf62fffSJeremy L Thompson *num_entries += single_entries; 2287eaf62fffSJeremy L Thompson } 2288eaf62fffSJeremy L Thompson } else { 22892b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries)); 2290eaf62fffSJeremy L Thompson *num_entries += single_entries; 2291eaf62fffSJeremy L Thompson } 22922b730f8bSJeremy L Thompson CeedCall(CeedCalloc(*num_entries, rows)); 22932b730f8bSJeremy L Thompson CeedCall(CeedCalloc(*num_entries, cols)); 2294eaf62fffSJeremy L Thompson 2295506b1a0cSSebastian Grimberg // Assemble nonzero locations 2296eaf62fffSJeremy L Thompson if (is_composite) { 2297c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2298c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 229992ae7e47SJeremy L Thompson for (CeedInt k = 0; k < num_suboperators; ++k) { 23002b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols)); 23012b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries)); 2302eaf62fffSJeremy L Thompson offset += single_entries; 2303eaf62fffSJeremy L Thompson } 2304eaf62fffSJeremy L Thompson } else { 23052b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols)); 2306eaf62fffSJeremy L Thompson } 2307eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2308eaf62fffSJeremy L Thompson } 2309eaf62fffSJeremy L Thompson 2310eaf62fffSJeremy L Thompson /** 2311eaf62fffSJeremy L Thompson @brief Fully assemble the nonzero entries of a linear operator. 2312eaf62fffSJeremy L Thompson 2313ca94c3ddSJeremy L Thompson Expected to be used in conjunction with @ref CeedOperatorLinearAssembleSymbolic(). 2314eaf62fffSJeremy L Thompson 2315ca94c3ddSJeremy 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)`. 2316ca94c3ddSJeremy L Thompson Note that the `(i, j)` pairs are not unique and may repeat. 2317ca94c3ddSJeremy L Thompson This function returns the values of the nonzero entries to be added, their `(i, j)` locations are provided by @ref CeedOperatorLinearAssembleSymbolic(). 2318eaf62fffSJeremy L Thompson 2319eaf62fffSJeremy L Thompson This will generally be slow unless your operator is low-order. 2320eaf62fffSJeremy L Thompson 2321ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2322f04ea552SJeremy L Thompson 2323ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 2324eaf62fffSJeremy L Thompson @param[out] values Values to assemble into matrix 2325eaf62fffSJeremy L Thompson 2326eaf62fffSJeremy L Thompson @ref User 2327eaf62fffSJeremy L Thompson **/ 2328eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) { 23291c66c397SJeremy L Thompson bool is_composite; 23301c66c397SJeremy L Thompson CeedInt num_suboperators, offset = 0; 2331b94338b9SJed Brown CeedSize single_entries = 0; 2332eaf62fffSJeremy L Thompson CeedOperator *sub_operators; 23331c66c397SJeremy L Thompson 23342b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2335f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2336f3d47e36SJeremy L Thompson 2337f3d47e36SJeremy L Thompson // Early exit for empty operator 2338f3d47e36SJeremy L Thompson if (!is_composite) { 2339f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 2340f3d47e36SJeremy L Thompson 2341f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 2342f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 2343f3d47e36SJeremy L Thompson } 2344eaf62fffSJeremy L Thompson 2345eaf62fffSJeremy L Thompson if (op->LinearAssemble) { 2346d04bbc78SJeremy L Thompson // Backend version 23472b730f8bSJeremy L Thompson CeedCall(op->LinearAssemble(op, values)); 2348eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2349eaf62fffSJeremy L Thompson } else { 2350d04bbc78SJeremy L Thompson // Operator fallback 2351d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2352d04bbc78SJeremy L Thompson 23532b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2354d04bbc78SJeremy L Thompson if (op_fallback) { 23552b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssemble(op_fallback, values)); 2356eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2357eaf62fffSJeremy L Thompson } 2358eaf62fffSJeremy L Thompson } 2359eaf62fffSJeremy L Thompson 2360eaf62fffSJeremy L Thompson // Default interface implementation 236128ec399dSJeremy L Thompson CeedCall(CeedVectorSetValue(values, 0.0)); 2362eaf62fffSJeremy L Thompson if (is_composite) { 2363c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2364c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 2365cefa2673SJeremy L Thompson for (CeedInt k = 0; k < num_suboperators; k++) { 23662b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values)); 23672b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries)); 2368eaf62fffSJeremy L Thompson offset += single_entries; 2369eaf62fffSJeremy L Thompson } 2370eaf62fffSJeremy L Thompson } else { 23712b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemble(op, offset, values)); 2372eaf62fffSJeremy L Thompson } 2373eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2374eaf62fffSJeremy L Thompson } 2375eaf62fffSJeremy L Thompson 2376eaf62fffSJeremy L Thompson /** 2377ca94c3ddSJeremy L Thompson @brief Get the multiplicity of nodes across sub-operators in a composite `CeedOperator`. 237875f0d5a4SJeremy L Thompson 2379ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 238075f0d5a4SJeremy L Thompson 2381ca94c3ddSJeremy L Thompson @param[in] op Composite `CeedOperator` 2382ca94c3ddSJeremy L Thompson @param[in] num_skip_indices Number of sub-operators to skip 2383ca94c3ddSJeremy L Thompson @param[in] skip_indices Array of indices of sub-operators to skip 2384ca94c3ddSJeremy L Thompson @param[out] mult Vector to store multiplicity (of size `l_size` ) 238575f0d5a4SJeremy L Thompson 238675f0d5a4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 238775f0d5a4SJeremy L Thompson 238875f0d5a4SJeremy L Thompson @ref User 238975f0d5a4SJeremy L Thompson **/ 239075f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) { 239175f0d5a4SJeremy L Thompson Ceed ceed; 2392b275c451SJeremy L Thompson CeedInt num_suboperators; 239375f0d5a4SJeremy L Thompson CeedSize l_vec_len; 239475f0d5a4SJeremy L Thompson CeedScalar *mult_array; 239575f0d5a4SJeremy L Thompson CeedVector ones_l_vec; 23967c1dbaffSSebastian Grimberg CeedElemRestriction elem_rstr, mult_elem_rstr; 2397b275c451SJeremy L Thompson CeedOperator *sub_operators; 239875f0d5a4SJeremy L Thompson 23991c66c397SJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 24001c66c397SJeremy L Thompson 240175f0d5a4SJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 240275f0d5a4SJeremy L Thompson 240375f0d5a4SJeremy L Thompson // Zero mult vector 240475f0d5a4SJeremy L Thompson CeedCall(CeedVectorSetValue(mult, 0.0)); 240575f0d5a4SJeremy L Thompson 240675f0d5a4SJeremy L Thompson // Get suboperators 2407b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2408b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 2409b275c451SJeremy L Thompson if (num_suboperators == 0) return CEED_ERROR_SUCCESS; 241075f0d5a4SJeremy L Thompson 241175f0d5a4SJeremy L Thompson // Work vector 241275f0d5a4SJeremy L Thompson CeedCall(CeedVectorGetLength(mult, &l_vec_len)); 241375f0d5a4SJeremy L Thompson CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec)); 241475f0d5a4SJeremy L Thompson CeedCall(CeedVectorSetValue(ones_l_vec, 1.0)); 241575f0d5a4SJeremy L Thompson CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array)); 241675f0d5a4SJeremy L Thompson 241775f0d5a4SJeremy L Thompson // Compute multiplicity across suboperators 2418b275c451SJeremy L Thompson for (CeedInt i = 0; i < num_suboperators; i++) { 241975f0d5a4SJeremy L Thompson const CeedScalar *sub_mult_array; 242075f0d5a4SJeremy L Thompson CeedVector sub_mult_l_vec, ones_e_vec; 242175f0d5a4SJeremy L Thompson 242275f0d5a4SJeremy L Thompson // -- Check for suboperator to skip 242375f0d5a4SJeremy L Thompson for (CeedInt j = 0; j < num_skip_indices; j++) { 242475f0d5a4SJeremy L Thompson if (skip_indices[j] == i) continue; 242575f0d5a4SJeremy L Thompson } 242675f0d5a4SJeremy L Thompson 242775f0d5a4SJeremy L Thompson // -- Sub operator multiplicity 2428437c7c90SJeremy L Thompson CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr)); 24297c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr, &mult_elem_rstr)); 24307c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionCreateVector(mult_elem_rstr, &sub_mult_l_vec, &ones_e_vec)); 243175f0d5a4SJeremy L Thompson CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0)); 24327c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE)); 24337c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE)); 243475f0d5a4SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array)); 243575f0d5a4SJeremy L Thompson // ---- Flag every node present in the current suboperator 243675f0d5a4SJeremy L Thompson for (CeedInt j = 0; j < l_vec_len; j++) { 243775f0d5a4SJeremy L Thompson if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0; 243875f0d5a4SJeremy L Thompson } 243975f0d5a4SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array)); 244075f0d5a4SJeremy L Thompson CeedCall(CeedVectorDestroy(&sub_mult_l_vec)); 244175f0d5a4SJeremy L Thompson CeedCall(CeedVectorDestroy(&ones_e_vec)); 24427c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&mult_elem_rstr)); 244375f0d5a4SJeremy L Thompson } 244475f0d5a4SJeremy L Thompson CeedCall(CeedVectorRestoreArray(mult, &mult_array)); 2445811d0ccfSJeremy L Thompson CeedCall(CeedVectorDestroy(&ones_l_vec)); 244675f0d5a4SJeremy L Thompson return CEED_ERROR_SUCCESS; 244775f0d5a4SJeremy L Thompson } 244875f0d5a4SJeremy L Thompson 244975f0d5a4SJeremy L Thompson /** 2450ca94c3ddSJeremy 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. 2451eaf62fffSJeremy L Thompson 2452ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable. 2453f04ea552SJeremy L Thompson 2454ca94c3ddSJeremy L Thompson @param[in] op_fine Fine grid `CeedOperator` 2455ca94c3ddSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator` 2456ca94c3ddSJeremy L Thompson @param[in] rstr_coarse Coarse grid `CeedElemRestriction` 2457ca94c3ddSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector `CeedBasis` 2458ca94c3ddSJeremy L Thompson @param[out] op_coarse Coarse grid `CeedOperator` 2459ca94c3ddSJeremy L Thompson @param[out] op_prolong Coarse to fine `CeedOperator`, or `NULL` 2460ca94c3ddSJeremy L Thompson @param[out] op_restrict Fine to coarse `CeedOperator`, or `NULL` 2461eaf62fffSJeremy L Thompson 2462eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2463eaf62fffSJeremy L Thompson 2464eaf62fffSJeremy L Thompson @ref User 2465eaf62fffSJeremy L Thompson **/ 24662b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 24677758292fSSebastian Grimberg CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) { 24681c66c397SJeremy L Thompson CeedBasis basis_c_to_f = NULL; 24691c66c397SJeremy L Thompson 24702b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fine)); 2471eaf62fffSJeremy L Thompson 247283d6adf3SZach Atkins // Build prolongation matrix, if required 24737758292fSSebastian Grimberg if (op_prolong || op_restrict) { 247483d6adf3SZach Atkins CeedBasis basis_fine; 24751c66c397SJeremy L Thompson 24762b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine)); 24772b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f)); 247883d6adf3SZach Atkins } 2479eaf62fffSJeremy L Thompson 2480f113e5dcSJeremy L Thompson // Core code 24817758292fSSebastian Grimberg CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict)); 2482eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2483eaf62fffSJeremy L Thompson } 2484eaf62fffSJeremy L Thompson 2485eaf62fffSJeremy L Thompson /** 2486ca94c3ddSJeremy L Thompson @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a tensor basis for the active basis. 2487eaf62fffSJeremy L Thompson 2488ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable. 2489f04ea552SJeremy L Thompson 2490ca94c3ddSJeremy L Thompson @param[in] op_fine Fine grid `CeedOperator` 2491ca94c3ddSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator` 2492ca94c3ddSJeremy L Thompson @param[in] rstr_coarse Coarse grid `CeedElemRestriction` 2493ca94c3ddSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector `CeedBasis` 2494ca94c3ddSJeremy L Thompson @param[in] interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator` 2495ca94c3ddSJeremy L Thompson @param[out] op_coarse Coarse grid `CeedOperator` 2496ca94c3ddSJeremy L Thompson @param[out] op_prolong Coarse to fine `CeedOperator`, or `NULL` 2497ca94c3ddSJeremy L Thompson @param[out] op_restrict Fine to coarse `CeedOperator`, or `NULL` 2498eaf62fffSJeremy L Thompson 2499eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2500eaf62fffSJeremy L Thompson 2501eaf62fffSJeremy L Thompson @ref User 2502eaf62fffSJeremy L Thompson **/ 25032b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 25042b730f8bSJeremy L Thompson const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, 25057758292fSSebastian Grimberg CeedOperator *op_restrict) { 2506eaf62fffSJeremy L Thompson Ceed ceed; 25071c66c397SJeremy L Thompson CeedInt Q_f, Q_c; 25081c66c397SJeremy L Thompson CeedBasis basis_fine, basis_c_to_f = NULL; 25091c66c397SJeremy L Thompson 25101c66c397SJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fine)); 25112b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op_fine, &ceed)); 2512eaf62fffSJeremy L Thompson 2513eaf62fffSJeremy L Thompson // Check for compatible quadrature spaces 25142b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine)); 25152b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f)); 25162b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c)); 25176574a04fSJeremy L Thompson CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces"); 2518eaf62fffSJeremy L Thompson 251983d6adf3SZach Atkins // Create coarse to fine basis, if required 25207758292fSSebastian Grimberg if (op_prolong || op_restrict) { 25211c66c397SJeremy L Thompson CeedInt dim, num_comp, num_nodes_c, P_1d_f, P_1d_c; 25221c66c397SJeremy L Thompson CeedScalar *q_ref, *q_weight, *grad; 25231c66c397SJeremy L Thompson 252483d6adf3SZach Atkins // Check if interpolation matrix is provided 25256574a04fSJeremy L Thompson CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE, 25266574a04fSJeremy L Thompson "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix"); 25272b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_fine, &dim)); 25282b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp)); 25292b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f)); 25302b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c)); 25312b730f8bSJeremy L Thompson P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c); 25322b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_f, &q_ref)); 25332b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_f, &q_weight)); 25342b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad)); 25352b730f8bSJeremy 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)); 25362b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref)); 25372b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight)); 25382b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad)); 253983d6adf3SZach Atkins } 2540eaf62fffSJeremy L Thompson 2541eaf62fffSJeremy L Thompson // Core code 25427758292fSSebastian Grimberg CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict)); 2543eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2544eaf62fffSJeremy L Thompson } 2545eaf62fffSJeremy L Thompson 2546eaf62fffSJeremy L Thompson /** 2547ca94c3ddSJeremy L Thompson @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a non-tensor basis for the active vector 2548eaf62fffSJeremy L Thompson 2549ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable. 2550f04ea552SJeremy L Thompson 2551ca94c3ddSJeremy L Thompson @param[in] op_fine Fine grid `CeedOperator` 2552ca94c3ddSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator` 2553ca94c3ddSJeremy L Thompson @param[in] rstr_coarse Coarse grid `CeedElemRestriction` 2554ca94c3ddSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector `CeedBasis` 2555ca94c3ddSJeremy L Thompson @param[in] interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator` 2556ca94c3ddSJeremy L Thompson @param[out] op_coarse Coarse grid `CeedOperator` 2557ca94c3ddSJeremy L Thompson @param[out] op_prolong Coarse to fine `CeedOperator`, or `NULL` 2558ca94c3ddSJeremy L Thompson @param[out] op_restrict Fine to coarse `CeedOperator`, or `NULL` 2559eaf62fffSJeremy L Thompson 2560eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2561eaf62fffSJeremy L Thompson 2562eaf62fffSJeremy L Thompson @ref User 2563eaf62fffSJeremy L Thompson **/ 25642b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 25657758292fSSebastian Grimberg const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, 25667758292fSSebastian Grimberg CeedOperator *op_restrict) { 2567eaf62fffSJeremy L Thompson Ceed ceed; 25681c66c397SJeremy L Thompson CeedInt Q_f, Q_c; 25691c66c397SJeremy L Thompson CeedBasis basis_fine, basis_c_to_f = NULL; 25701c66c397SJeremy L Thompson 25711c66c397SJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fine)); 25722b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op_fine, &ceed)); 2573eaf62fffSJeremy L Thompson 2574eaf62fffSJeremy L Thompson // Check for compatible quadrature spaces 25752b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine)); 25762b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f)); 25772b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c)); 25786574a04fSJeremy L Thompson CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces"); 2579eaf62fffSJeremy L Thompson 2580eaf62fffSJeremy L Thompson // Coarse to fine basis 25817758292fSSebastian Grimberg if (op_prolong || op_restrict) { 25821c66c397SJeremy L Thompson CeedInt dim, num_comp, num_nodes_c, num_nodes_f; 25831c66c397SJeremy L Thompson CeedScalar *q_ref, *q_weight, *grad; 25841c66c397SJeremy L Thompson CeedElemTopology topo; 25851c66c397SJeremy L Thompson 258683d6adf3SZach Atkins // Check if interpolation matrix is provided 25876574a04fSJeremy L Thompson CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE, 25886574a04fSJeremy L Thompson "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix"); 25892b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopology(basis_fine, &topo)); 25902b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_fine, &dim)); 25912b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp)); 25922b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f)); 25932b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c)); 25942b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref)); 25952b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_f, &q_weight)); 25962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad)); 25972b730f8bSJeremy 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)); 25982b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref)); 25992b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight)); 26002b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad)); 260183d6adf3SZach Atkins } 2602eaf62fffSJeremy L Thompson 2603eaf62fffSJeremy L Thompson // Core code 26047758292fSSebastian Grimberg CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict)); 2605eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2606eaf62fffSJeremy L Thompson } 2607eaf62fffSJeremy L Thompson 2608eaf62fffSJeremy L Thompson /** 2609ca94c3ddSJeremy L Thompson @brief Build a FDM based approximate inverse for each element for a `CeedOperator`. 2610eaf62fffSJeremy L Thompson 2611ca94c3ddSJeremy L Thompson This returns a `CeedOperator` and `CeedVector` to apply a Fast Diagonalization Method based approximate inverse. 2612859c15bbSJames 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$. 2613ca94c3ddSJeremy 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$. 2614ca94c3ddSJeremy L Thompson The `CeedOperator` must be linear and non-composite. 2615ca94c3ddSJeremy L Thompson The associated `CeedQFunction` must therefore also be linear. 2616eaf62fffSJeremy L Thompson 2617ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2618f04ea552SJeremy L Thompson 2619ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to create element inverses 2620ca94c3ddSJeremy L Thompson @param[out] fdm_inv `CeedOperator` to apply the action of a FDM based inverse for each element 2621ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2622eaf62fffSJeremy L Thompson 2623eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2624eaf62fffSJeremy L Thompson 2625480fae85SJeremy L Thompson @ref User 2626eaf62fffSJeremy L Thompson **/ 26272b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) { 26281c66c397SJeremy L Thompson Ceed ceed, ceed_parent; 26291c66c397SJeremy L Thompson bool interp = false, grad = false, is_tensor_basis = true; 26301c66c397SJeremy L Thompson CeedInt num_input_fields, P_1d, Q_1d, num_nodes, num_qpts, dim, num_comp = 1, num_elem = 1; 26311c66c397SJeremy L Thompson CeedSize l_size = 1; 26321c66c397SJeremy L Thompson CeedScalar *mass, *laplace, *x, *fdm_interp, *lambda, *elem_avg; 26331c66c397SJeremy L Thompson const CeedScalar *interp_1d, *grad_1d, *q_weight_1d; 26341c66c397SJeremy L Thompson CeedVector q_data; 26351c66c397SJeremy L Thompson CeedElemRestriction rstr = NULL, rstr_qd_i; 26361c66c397SJeremy L Thompson CeedBasis basis = NULL, fdm_basis; 26371c66c397SJeremy L Thompson CeedQFunctionContext ctx_fdm; 26381c66c397SJeremy L Thompson CeedQFunctionField *qf_fields; 26391c66c397SJeremy L Thompson CeedQFunction qf, qf_fdm; 26401c66c397SJeremy L Thompson CeedOperatorField *op_fields; 26411c66c397SJeremy L Thompson 26422b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2643eaf62fffSJeremy L Thompson 2644eaf62fffSJeremy L Thompson if (op->CreateFDMElementInverse) { 2645d04bbc78SJeremy L Thompson // Backend version 26462b730f8bSJeremy L Thompson CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request)); 2647eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2648eaf62fffSJeremy L Thompson } else { 2649d04bbc78SJeremy L Thompson // Operator fallback 2650d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2651d04bbc78SJeremy L Thompson 26522b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2653d04bbc78SJeremy L Thompson if (op_fallback) { 26542b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request)); 2655eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2656eaf62fffSJeremy L Thompson } 2657eaf62fffSJeremy L Thompson } 2658eaf62fffSJeremy L Thompson 2659d04bbc78SJeremy L Thompson // Default interface implementation 26602b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 2661bb229da9SJeremy L Thompson CeedCall(CeedOperatorGetFallbackParentCeed(op, &ceed_parent)); 26622b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetQFunction(op, &qf)); 2663eaf62fffSJeremy L Thompson 2664eaf62fffSJeremy L Thompson // Determine active input basis 26652b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL)); 26662b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 2667eaf62fffSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 2668eaf62fffSJeremy L Thompson CeedVector vec; 26691c66c397SJeremy L Thompson 26702b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec)); 2671eaf62fffSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 2672eaf62fffSJeremy L Thompson CeedEvalMode eval_mode; 26731c66c397SJeremy L Thompson 26742b730f8bSJeremy L Thompson CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 2675eaf62fffSJeremy L Thompson interp = interp || eval_mode == CEED_EVAL_INTERP; 2676eaf62fffSJeremy L Thompson grad = grad || eval_mode == CEED_EVAL_GRAD; 26772b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 26782b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr)); 2679eaf62fffSJeremy L Thompson } 2680eaf62fffSJeremy L Thompson } 26816574a04fSJeremy L Thompson CeedCheck(basis, ceed, CEED_ERROR_BACKEND, "No active field set"); 26822b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 2683352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 26842b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 26852b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 26862b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 26872b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 26882b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem)); 26892b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size)); 2690eaf62fffSJeremy L Thompson 2691eaf62fffSJeremy L Thompson // Build and diagonalize 1D Mass and Laplacian 26926574a04fSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 26936574a04fSJeremy L Thompson CeedCheck(is_tensor_basis, ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases"); 26942b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &mass)); 26952b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &laplace)); 26962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &x)); 26972b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp)); 26982b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d, &lambda)); 2699eaf62fffSJeremy L Thompson // -- Build matrices 27002b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 27012b730f8bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 27022b730f8bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d)); 27032b730f8bSJeremy L Thompson CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace)); 2704eaf62fffSJeremy L Thompson 2705eaf62fffSJeremy L Thompson // -- Diagonalize 27062b730f8bSJeremy L Thompson CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d)); 27072b730f8bSJeremy L Thompson CeedCall(CeedFree(&mass)); 27082b730f8bSJeremy L Thompson CeedCall(CeedFree(&laplace)); 27092b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) { 27102b730f8bSJeremy L Thompson for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d]; 27112b730f8bSJeremy L Thompson } 27122b730f8bSJeremy L Thompson CeedCall(CeedFree(&x)); 2713eaf62fffSJeremy L Thompson 27141c66c397SJeremy L Thompson { 27151c66c397SJeremy L Thompson CeedInt layout[3], num_modes = (interp ? 1 : 0) + (grad ? dim : 0); 27161c66c397SJeremy L Thompson CeedScalar max_norm = 0; 27171c66c397SJeremy L Thompson const CeedScalar *assembled_array, *q_weight_array; 27181c66c397SJeremy L Thompson CeedVector assembled = NULL, q_weight; 2719c5f45aeaSJeremy L Thompson CeedElemRestriction rstr_qf = NULL; 27201c66c397SJeremy L Thompson 27211c66c397SJeremy L Thompson // Assemble QFunction 27222b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request)); 272356c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(rstr_qf, layout)); 27242b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_qf)); 27252b730f8bSJeremy L Thompson CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm)); 2726eaf62fffSJeremy L Thompson 2727eaf62fffSJeremy L Thompson // Calculate element averages 27282b730f8bSJeremy L Thompson CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight)); 27292b730f8bSJeremy L Thompson CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight)); 27302b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array)); 27312b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array)); 27322b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_elem, &elem_avg)); 2733eaf62fffSJeremy L Thompson const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON; 27341c66c397SJeremy L Thompson 2735eaf62fffSJeremy L Thompson for (CeedInt e = 0; e < num_elem; e++) { 2736eaf62fffSJeremy L Thompson CeedInt count = 0; 27371c66c397SJeremy L Thompson 27382b730f8bSJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 27392b730f8bSJeremy L Thompson for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) { 27402b730f8bSJeremy L Thompson if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) { 27412b730f8bSJeremy L Thompson elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q]; 2742eaf62fffSJeremy L Thompson count++; 2743eaf62fffSJeremy L Thompson } 27442b730f8bSJeremy L Thompson } 27452b730f8bSJeremy L Thompson } 2746eaf62fffSJeremy L Thompson if (count) { 2747eaf62fffSJeremy L Thompson elem_avg[e] /= count; 2748eaf62fffSJeremy L Thompson } else { 2749eaf62fffSJeremy L Thompson elem_avg[e] = 1.0; 2750eaf62fffSJeremy L Thompson } 2751eaf62fffSJeremy L Thompson } 27522b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array)); 27532b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled)); 27542b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array)); 27552b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&q_weight)); 27561c66c397SJeremy L Thompson } 2757eaf62fffSJeremy L Thompson 2758eaf62fffSJeremy L Thompson // Build FDM diagonal 27591c66c397SJeremy L Thompson { 2760eaf62fffSJeremy L Thompson CeedScalar *q_data_array, *fdm_diagonal; 27611c66c397SJeremy L Thompson 2762352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_comp * num_nodes, &fdm_diagonal)); 2763352a5e7cSSebastian Grimberg const CeedScalar fdm_diagonal_bound = num_nodes * CEED_EPSILON; 27642b730f8bSJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) { 2765352a5e7cSSebastian Grimberg for (CeedInt n = 0; n < num_nodes; n++) { 2766352a5e7cSSebastian Grimberg if (interp) fdm_diagonal[c * num_nodes + n] = 1.0; 27672b730f8bSJeremy L Thompson if (grad) { 2768eaf62fffSJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 2769eaf62fffSJeremy L Thompson CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d; 2770352a5e7cSSebastian Grimberg fdm_diagonal[c * num_nodes + n] += lambda[i]; 2771eaf62fffSJeremy L Thompson } 2772eaf62fffSJeremy L Thompson } 2773352a5e7cSSebastian Grimberg if (fabs(fdm_diagonal[c * num_nodes + n]) < fdm_diagonal_bound) fdm_diagonal[c * num_nodes + n] = fdm_diagonal_bound; 27742b730f8bSJeremy L Thompson } 27752b730f8bSJeremy L Thompson } 2776352a5e7cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * num_nodes, &q_data)); 27772b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(q_data, 0.0)); 27782b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array)); 27792b730f8bSJeremy L Thompson for (CeedInt e = 0; e < num_elem; e++) { 27802b730f8bSJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) { 27811c66c397SJeremy L Thompson for (CeedInt n = 0; n < num_nodes; n++) 27821c66c397SJeremy L Thompson q_data_array[(e * num_comp + c) * num_nodes + n] = 1. / (elem_avg[e] * fdm_diagonal[c * num_nodes + n]); 27832b730f8bSJeremy L Thompson } 27842b730f8bSJeremy L Thompson } 27852b730f8bSJeremy L Thompson CeedCall(CeedFree(&elem_avg)); 27862b730f8bSJeremy L Thompson CeedCall(CeedFree(&fdm_diagonal)); 27872b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(q_data, &q_data_array)); 27881c66c397SJeremy L Thompson } 2789eaf62fffSJeremy L Thompson 2790eaf62fffSJeremy L Thompson // Setup FDM operator 2791eaf62fffSJeremy L Thompson // -- Basis 27921c66c397SJeremy L Thompson { 2793eaf62fffSJeremy L Thompson CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy; 27941c66c397SJeremy L Thompson 27952b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy)); 27962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d, &q_ref_dummy)); 27972b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d, &q_weight_dummy)); 27982b730f8bSJeremy 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)); 27992b730f8bSJeremy L Thompson CeedCall(CeedFree(&fdm_interp)); 28002b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_dummy)); 28012b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref_dummy)); 28022b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight_dummy)); 28032b730f8bSJeremy L Thompson CeedCall(CeedFree(&lambda)); 28041c66c397SJeremy L Thompson } 2805eaf62fffSJeremy L Thompson 2806eaf62fffSJeremy L Thompson // -- Restriction 28071c66c397SJeremy L Thompson { 2808352a5e7cSSebastian Grimberg CeedInt strides[3] = {1, num_nodes, num_nodes * num_comp}; 2809352a5e7cSSebastian Grimberg CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, num_nodes, num_comp, num_elem * num_comp * num_nodes, strides, &rstr_qd_i)); 28101c66c397SJeremy L Thompson } 28111c66c397SJeremy L Thompson 2812eaf62fffSJeremy L Thompson // -- QFunction 28132b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm)); 28142b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP)); 28152b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE)); 28162b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP)); 28172b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp)); 28181c66c397SJeremy L Thompson 2819eaf62fffSJeremy L Thompson // -- QFunction context 28201c66c397SJeremy L Thompson { 2821eaf62fffSJeremy L Thompson CeedInt *num_comp_data; 28221c66c397SJeremy L Thompson 28232b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &num_comp_data)); 2824eaf62fffSJeremy L Thompson num_comp_data[0] = num_comp; 28252b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm)); 28262b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data)); 28271c66c397SJeremy L Thompson } 28282b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm)); 28292b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(&ctx_fdm)); 28301c66c397SJeremy L Thompson 2831eaf62fffSJeremy L Thompson // -- Operator 28322b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv)); 28332b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE)); 2834356036faSJeremy L Thompson CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_NONE, q_data)); 28352b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE)); 2836eaf62fffSJeremy L Thompson 2837eaf62fffSJeremy L Thompson // Cleanup 28382b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&q_data)); 28392b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(&fdm_basis)); 28402b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i)); 28412b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&qf_fdm)); 2842eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2843eaf62fffSJeremy L Thompson } 2844eaf62fffSJeremy L Thompson 2845eaf62fffSJeremy L Thompson /// @} 2846