13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3eaf62fffSJeremy L Thompson // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5eaf62fffSJeremy L Thompson // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7eaf62fffSJeremy L Thompson 82b730f8bSJeremy L Thompson #include <ceed-impl.h> 949aac155SJeremy L Thompson #include <ceed.h> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 11c85e8640SSebastian Grimberg #include <assert.h> 122b730f8bSJeremy L Thompson #include <math.h> 13eaf62fffSJeremy L Thompson #include <stdbool.h> 14eaf62fffSJeremy L Thompson #include <stdio.h> 15eaf62fffSJeremy L Thompson #include <string.h> 16eaf62fffSJeremy L Thompson 17eaf62fffSJeremy L Thompson /// @file 18eaf62fffSJeremy L Thompson /// Implementation of CeedOperator preconditioning interfaces 19eaf62fffSJeremy L Thompson 20eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 21eaf62fffSJeremy L Thompson /// CeedOperator Library Internal Preconditioning Functions 22eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 23eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorDeveloper 24eaf62fffSJeremy L Thompson /// @{ 25eaf62fffSJeremy L Thompson 26eaf62fffSJeremy L Thompson /** 27ea61e9acSJeremy L Thompson @brief Duplicate a CeedQFunction with a reference Ceed to fallback for advanced CeedOperator functionality 289e77b9c8SJeremy L Thompson 2901ea9c81SJed Brown @param[in] fallback_ceed Ceed on which to create fallback CeedQFunction 309e77b9c8SJeremy L Thompson @param[in] qf CeedQFunction to create fallback for 3101ea9c81SJed Brown @param[out] qf_fallback fallback CeedQFunction 329e77b9c8SJeremy L Thompson 339e77b9c8SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 349e77b9c8SJeremy L Thompson 359e77b9c8SJeremy L Thompson @ref Developer 369e77b9c8SJeremy L Thompson **/ 372b730f8bSJeremy L Thompson static int CeedQFunctionCreateFallback(Ceed fallback_ceed, CeedQFunction qf, CeedQFunction *qf_fallback) { 381c66c397SJeremy L Thompson char *source_path_with_name = NULL; 391c66c397SJeremy L Thompson 409e77b9c8SJeremy L Thompson // Check if NULL qf passed in 419e77b9c8SJeremy L Thompson if (!qf) return CEED_ERROR_SUCCESS; 429e77b9c8SJeremy L Thompson 43d04bbc78SJeremy L Thompson CeedDebug256(qf->ceed, 1, "---------- CeedOperator Fallback ----------\n"); 4413f886e9SJeremy L Thompson CeedDebug(qf->ceed, "Creating fallback CeedQFunction\n"); 45d04bbc78SJeremy L Thompson 469e77b9c8SJeremy L Thompson if (qf->source_path) { 472b730f8bSJeremy L Thompson size_t path_len = strlen(qf->source_path), name_len = strlen(qf->kernel_name); 482b730f8bSJeremy L Thompson CeedCall(CeedCalloc(path_len + name_len + 2, &source_path_with_name)); 499e77b9c8SJeremy L Thompson memcpy(source_path_with_name, qf->source_path, path_len); 509e77b9c8SJeremy L Thompson memcpy(&source_path_with_name[path_len], ":", 1); 519e77b9c8SJeremy L Thompson memcpy(&source_path_with_name[path_len + 1], qf->kernel_name, name_len); 529e77b9c8SJeremy L Thompson } else { 532b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &source_path_with_name)); 549e77b9c8SJeremy L Thompson } 559e77b9c8SJeremy L Thompson 562b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateInterior(fallback_ceed, qf->vec_length, qf->function, source_path_with_name, qf_fallback)); 579e77b9c8SJeremy L Thompson { 589e77b9c8SJeremy L Thompson CeedQFunctionContext ctx; 599e77b9c8SJeremy L Thompson 602b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetContext(qf, &ctx)); 612b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx)); 629e77b9c8SJeremy L Thompson } 639e77b9c8SJeremy L Thompson for (CeedInt i = 0; i < qf->num_input_fields; i++) { 642b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(*qf_fallback, qf->input_fields[i]->field_name, qf->input_fields[i]->size, qf->input_fields[i]->eval_mode)); 659e77b9c8SJeremy L Thompson } 669e77b9c8SJeremy L Thompson for (CeedInt i = 0; i < qf->num_output_fields; i++) { 672b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddOutput(*qf_fallback, qf->output_fields[i]->field_name, qf->output_fields[i]->size, qf->output_fields[i]->eval_mode)); 689e77b9c8SJeremy L Thompson } 692b730f8bSJeremy L Thompson CeedCall(CeedFree(&source_path_with_name)); 709e77b9c8SJeremy L Thompson return CEED_ERROR_SUCCESS; 719e77b9c8SJeremy L Thompson } 729e77b9c8SJeremy L Thompson 739e77b9c8SJeremy L Thompson /** 74ea61e9acSJeremy L Thompson @brief Duplicate a CeedOperator with a reference Ceed to fallback for advanced CeedOperator functionality 75eaf62fffSJeremy L Thompson 76ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator to create fallback for 77eaf62fffSJeremy L Thompson 78eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 79eaf62fffSJeremy L Thompson 80eaf62fffSJeremy L Thompson @ref Developer 81eaf62fffSJeremy L Thompson **/ 82d04bbc78SJeremy L Thompson static int CeedOperatorCreateFallback(CeedOperator op) { 839e77b9c8SJeremy L Thompson Ceed ceed_fallback; 841c66c397SJeremy L Thompson bool is_composite; 851c66c397SJeremy L Thompson CeedOperator op_fallback; 86eaf62fffSJeremy L Thompson 87805fe78eSJeremy L Thompson // Check not already created 88805fe78eSJeremy L Thompson if (op->op_fallback) return CEED_ERROR_SUCCESS; 89805fe78eSJeremy L Thompson 90eaf62fffSJeremy L Thompson // Fallback Ceed 912b730f8bSJeremy L Thompson CeedCall(CeedGetOperatorFallbackCeed(op->ceed, &ceed_fallback)); 92d04bbc78SJeremy L Thompson if (!ceed_fallback) return CEED_ERROR_SUCCESS; 93d04bbc78SJeremy L Thompson 94d04bbc78SJeremy L Thompson CeedDebug256(op->ceed, 1, "---------- CeedOperator Fallback ----------\n"); 9513f886e9SJeremy L Thompson CeedDebug(op->ceed, "Creating fallback CeedOperator\n"); 96eaf62fffSJeremy L Thompson 97eaf62fffSJeremy L Thompson // Clone Op 98b275c451SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 99b275c451SJeremy L Thompson if (is_composite) { 100b275c451SJeremy L Thompson CeedInt num_suboperators; 101b275c451SJeremy L Thompson CeedOperator *sub_operators; 102b275c451SJeremy L Thompson 1032b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorCreate(ceed_fallback, &op_fallback)); 104b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 105b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 106b275c451SJeremy L Thompson for (CeedInt i = 0; i < num_suboperators; i++) { 107d04bbc78SJeremy L Thompson CeedOperator op_sub_fallback; 108d04bbc78SJeremy L Thompson 109b275c451SJeremy L Thompson CeedCall(CeedOperatorGetFallback(sub_operators[i], &op_sub_fallback)); 1102b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorAddSub(op_fallback, op_sub_fallback)); 111805fe78eSJeremy L Thompson } 112805fe78eSJeremy L Thompson } else { 1139e77b9c8SJeremy L Thompson CeedQFunction qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL; 1141c66c397SJeremy L Thompson 1152b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback)); 1162b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback)); 1172b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback)); 1182b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback)); 119805fe78eSJeremy L Thompson for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 120437c7c90SJeremy L Thompson CeedCall(CeedOperatorSetField(op_fallback, op->input_fields[i]->field_name, op->input_fields[i]->elem_rstr, op->input_fields[i]->basis, 1212b730f8bSJeremy L Thompson op->input_fields[i]->vec)); 122805fe78eSJeremy L Thompson } 123805fe78eSJeremy L Thompson for (CeedInt i = 0; i < op->qf->num_output_fields; i++) { 124437c7c90SJeremy L Thompson CeedCall(CeedOperatorSetField(op_fallback, op->output_fields[i]->field_name, op->output_fields[i]->elem_rstr, op->output_fields[i]->basis, 1252b730f8bSJeremy L Thompson op->output_fields[i]->vec)); 126805fe78eSJeremy L Thompson } 1272b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op->qf_assembled, &op_fallback->qf_assembled)); 128febe2972SJeremy L Thompson if (op_fallback->num_qpts == 0) CeedCall(CeedOperatorSetNumQuadraturePoints(op_fallback, op->num_qpts)); 1299e77b9c8SJeremy L Thompson // Cleanup 1302b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&qf_fallback)); 1312b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&dqf_fallback)); 1322b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&dqfT_fallback)); 133805fe78eSJeremy L Thompson } 1342b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetName(op_fallback, op->name)); 1352b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fallback)); 136b05f7e9fSJeremy L Thompson // Note: No ref-counting here so we don't get caught in a reference loop. 137b05f7e9fSJeremy L Thompson // The op holds the only reference to op_fallback and is responsible for deleting itself and op_fallback. 138805fe78eSJeremy L Thompson op->op_fallback = op_fallback; 139b05f7e9fSJeremy L Thompson op_fallback->op_fallback_parent = op; 140eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 141eaf62fffSJeremy L Thompson } 142eaf62fffSJeremy L Thompson 143eaf62fffSJeremy L Thompson /** 144ea61e9acSJeremy L Thompson @brief Retrieve fallback CeedOperator with a reference Ceed for advanced CeedOperator functionality 145d04bbc78SJeremy L Thompson 146d04bbc78SJeremy L Thompson @param[in] op CeedOperator to retrieve fallback for 147d04bbc78SJeremy L Thompson @param[out] op_fallback Fallback CeedOperator 148d04bbc78SJeremy L Thompson 149d04bbc78SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 150d04bbc78SJeremy L Thompson 151d04bbc78SJeremy L Thompson @ref Developer 152d04bbc78SJeremy L Thompson **/ 153d04bbc78SJeremy L Thompson int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) { 154d04bbc78SJeremy L Thompson // Create if needed 1551c66c397SJeremy L Thompson if (!op->op_fallback) CeedCall(CeedOperatorCreateFallback(op)); 156d04bbc78SJeremy L Thompson if (op->op_fallback) { 157d04bbc78SJeremy L Thompson bool is_debug; 158d04bbc78SJeremy L Thompson 1592b730f8bSJeremy L Thompson CeedCall(CeedIsDebug(op->ceed, &is_debug)); 160d04bbc78SJeremy L Thompson if (is_debug) { 161b275c451SJeremy L Thompson Ceed ceed, ceed_fallback; 162d04bbc78SJeremy L Thompson const char *resource, *resource_fallback; 163d04bbc78SJeremy L Thompson 164b275c451SJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 165b275c451SJeremy L Thompson CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback)); 166b275c451SJeremy L Thompson CeedCall(CeedGetResource(ceed, &resource)); 1672b730f8bSJeremy L Thompson CeedCall(CeedGetResource(ceed_fallback, &resource_fallback)); 168d04bbc78SJeremy L Thompson 16923d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n"); 170b275c451SJeremy L Thompson CeedDebug(ceed, "Falling back from %s operator at address %ld to %s operator at address %ld\n", resource, op, resource_fallback, 1712b730f8bSJeremy L Thompson op->op_fallback); 172d04bbc78SJeremy L Thompson } 173d04bbc78SJeremy L Thompson } 174d04bbc78SJeremy L Thompson *op_fallback = op->op_fallback; 175d04bbc78SJeremy L Thompson return CEED_ERROR_SUCCESS; 176d04bbc78SJeremy L Thompson } 177d04bbc78SJeremy L Thompson 178d04bbc78SJeremy L Thompson /** 1792e8f5c67SJeremy L Thompson @brief Get the parent CeedOperator for a fallback CeedOperator 180bb229da9SJeremy L Thompson 181bb229da9SJeremy L Thompson @param[in] op CeedOperator context 182bb229da9SJeremy L Thompson @param[out] parent Variable to store parent CeedOperator context 183bb229da9SJeremy L Thompson 184bb229da9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 185bb229da9SJeremy L Thompson 186bb229da9SJeremy L Thompson @ref Developer 187bb229da9SJeremy L Thompson **/ 188bb229da9SJeremy L Thompson int CeedOperatorGetFallbackParent(CeedOperator op, CeedOperator *parent) { 189bb229da9SJeremy L Thompson *parent = op->op_fallback_parent ? op->op_fallback_parent : NULL; 190bb229da9SJeremy L Thompson return CEED_ERROR_SUCCESS; 191bb229da9SJeremy L Thompson } 192bb229da9SJeremy L Thompson 193bb229da9SJeremy L Thompson /** 1942e8f5c67SJeremy L Thompson @brief Get the Ceed context of the parent CeedOperator for a fallback CeedOperator 195bb229da9SJeremy L Thompson 196bb229da9SJeremy L Thompson @param[in] op CeedOperator context 197bb229da9SJeremy L Thompson @param[out] parent Variable to store parent Ceed context 198bb229da9SJeremy L Thompson 199bb229da9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 200bb229da9SJeremy L Thompson 201bb229da9SJeremy L Thompson @ref Developer 202bb229da9SJeremy L Thompson **/ 203bb229da9SJeremy L Thompson int CeedOperatorGetFallbackParentCeed(CeedOperator op, Ceed *parent) { 204e984cf9aSJeremy L Thompson *parent = op->op_fallback_parent ? op->op_fallback_parent->ceed : op->ceed; 205bb229da9SJeremy L Thompson return CEED_ERROR_SUCCESS; 206bb229da9SJeremy L Thompson } 207bb229da9SJeremy L Thompson 208bb229da9SJeremy L Thompson /** 209eaf62fffSJeremy L Thompson @brief Select correct basis matrix pointer based on CeedEvalMode 210eaf62fffSJeremy L Thompson 211352a5e7cSSebastian Grimberg @param[in] basis CeedBasis from which to get the basis matrix 212eaf62fffSJeremy L Thompson @param[in] eval_mode Current basis evaluation mode 213eaf62fffSJeremy L Thompson @param[in] identity Pointer to identity matrix 214eaf62fffSJeremy L Thompson @param[out] basis_ptr Basis pointer to set 215eaf62fffSJeremy L Thompson 216eaf62fffSJeremy L Thompson @ref Developer 217eaf62fffSJeremy L Thompson **/ 218352a5e7cSSebastian Grimberg static inline int CeedOperatorGetBasisPointer(CeedBasis basis, CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar **basis_ptr) { 219eaf62fffSJeremy L Thompson switch (eval_mode) { 220eaf62fffSJeremy L Thompson case CEED_EVAL_NONE: 221eaf62fffSJeremy L Thompson *basis_ptr = identity; 222eaf62fffSJeremy L Thompson break; 223eaf62fffSJeremy L Thompson case CEED_EVAL_INTERP: 224352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetInterp(basis, basis_ptr)); 225eaf62fffSJeremy L Thompson break; 226eaf62fffSJeremy L Thompson case CEED_EVAL_GRAD: 227352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetGrad(basis, basis_ptr)); 228352a5e7cSSebastian Grimberg break; 229352a5e7cSSebastian Grimberg case CEED_EVAL_DIV: 230352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetDiv(basis, basis_ptr)); 231352a5e7cSSebastian Grimberg break; 232352a5e7cSSebastian Grimberg case CEED_EVAL_CURL: 233352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetCurl(basis, basis_ptr)); 234eaf62fffSJeremy L Thompson break; 235eaf62fffSJeremy L Thompson case CEED_EVAL_WEIGHT: 236eaf62fffSJeremy L Thompson break; // Caught by QF Assembly 237eaf62fffSJeremy L Thompson } 238ed9e99e6SJeremy L Thompson assert(*basis_ptr != NULL); 239352a5e7cSSebastian Grimberg return CEED_ERROR_SUCCESS; 240eaf62fffSJeremy L Thompson } 241eaf62fffSJeremy L Thompson 242eaf62fffSJeremy L Thompson /** 243eaf62fffSJeremy L Thompson @brief Core logic for assembling operator diagonal or point block diagonal 244eaf62fffSJeremy L Thompson 245eaf62fffSJeremy L Thompson @param[in] op CeedOperator to assemble point block diagonal 246ea61e9acSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE 247bd83916cSSebastian Grimberg @param[in] is_point_block Boolean flag to assemble diagonal or point block diagonal 248eaf62fffSJeremy L Thompson @param[out] assembled CeedVector to store assembled diagonal 249eaf62fffSJeremy L Thompson 250eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 251eaf62fffSJeremy L Thompson 252eaf62fffSJeremy L Thompson @ref Developer 253eaf62fffSJeremy L Thompson **/ 254bd83916cSSebastian Grimberg static inline int CeedSingleOperatorAssembleAddDiagonal_Core(CeedOperator op, CeedRequest *request, const bool is_point_block, CeedVector assembled) { 255eaf62fffSJeremy L Thompson Ceed ceed; 256506b1a0cSSebastian Grimberg bool is_composite; 257506b1a0cSSebastian Grimberg 258506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetCeed(op, &ceed)); 259506b1a0cSSebastian Grimberg CeedCall(CeedOperatorIsComposite(op, &is_composite)); 260506b1a0cSSebastian Grimberg CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 261506b1a0cSSebastian Grimberg 262506b1a0cSSebastian Grimberg // Assemble QFunction 263506b1a0cSSebastian Grimberg CeedInt layout_qf[3]; 264437c7c90SJeremy L Thompson const CeedScalar *assembled_qf_array; 265c5f45aeaSJeremy L Thompson CeedVector assembled_qf = NULL; 266c5f45aeaSJeremy L Thompson CeedElemRestriction assembled_elem_rstr = NULL; 267437c7c90SJeremy L Thompson 268437c7c90SJeremy L Thompson CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request)); 269506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, &layout_qf)); 270437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr)); 271437c7c90SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array)); 272eaf62fffSJeremy L Thompson 273ed9e99e6SJeremy L Thompson // Get assembly data 274437c7c90SJeremy L Thompson const CeedEvalMode **eval_modes_in, **eval_modes_out; 275506b1a0cSSebastian Grimberg CeedInt num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out; 276437c7c90SJeremy L Thompson CeedSize **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components; 277506b1a0cSSebastian Grimberg CeedBasis *active_bases_in, *active_bases_out; 278506b1a0cSSebastian Grimberg CeedElemRestriction *active_elem_rstrs_in, *active_elem_rstrs_out; 2791c66c397SJeremy L Thompson CeedOperatorAssemblyData data; 2801c66c397SJeremy L Thompson 281437c7c90SJeremy L Thompson CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data)); 282506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in, 283506b1a0cSSebastian Grimberg &num_active_bases_out, &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out, 284506b1a0cSSebastian Grimberg &num_output_components)); 285506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, NULL, NULL, &active_bases_out, NULL)); 286506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs_in, NULL, &active_elem_rstrs_out)); 287506b1a0cSSebastian Grimberg 288*934a29f5SSebastian Grimberg // Loop over all active bases (find matching input/output pairs) 289*934a29f5SSebastian Grimberg for (CeedInt b = 0; b < CeedIntMin(num_active_bases_in, num_active_bases_out); b++) { 290*934a29f5SSebastian Grimberg CeedInt b_in, b_out, num_elem, num_nodes, num_qpts, num_comp; 2911c66c397SJeremy L Thompson bool has_eval_none = false; 2921c66c397SJeremy L Thompson CeedScalar *elem_diag_array, *identity = NULL; 2931c66c397SJeremy L Thompson CeedVector elem_diag; 2947c1dbaffSSebastian Grimberg CeedElemRestriction diag_elem_rstr; 2951c66c397SJeremy L Thompson 296*934a29f5SSebastian Grimberg if (num_active_bases_in <= num_active_bases_out) { 297*934a29f5SSebastian Grimberg b_in = b; 298*934a29f5SSebastian Grimberg for (b_out = 0; b_out < num_active_bases_out; b_out++) { 299*934a29f5SSebastian Grimberg if (active_bases_in[b_in] == active_bases_out[b_out]) { 300*934a29f5SSebastian Grimberg break; 301*934a29f5SSebastian Grimberg } 302*934a29f5SSebastian Grimberg } 303*934a29f5SSebastian Grimberg if (b_out == num_active_bases_out) { 304*934a29f5SSebastian Grimberg continue; 305*934a29f5SSebastian Grimberg } // No matching output basis found 306*934a29f5SSebastian Grimberg } else { 307*934a29f5SSebastian Grimberg b_out = b; 308*934a29f5SSebastian Grimberg for (b_in = 0; b_in < num_active_bases_in; b_in++) { 309*934a29f5SSebastian Grimberg if (active_bases_in[b_in] == active_bases_out[b_out]) { 310*934a29f5SSebastian Grimberg break; 311*934a29f5SSebastian Grimberg } 312*934a29f5SSebastian Grimberg } 313*934a29f5SSebastian Grimberg if (b_in == num_active_bases_in) { 314*934a29f5SSebastian Grimberg continue; 315*934a29f5SSebastian Grimberg } // No matching output basis found 316*934a29f5SSebastian Grimberg } 317*934a29f5SSebastian Grimberg CeedCheck(active_elem_rstrs_in[b_in] == active_elem_rstrs_out[b_out], ceed, CEED_ERROR_UNSUPPORTED, 318506b1a0cSSebastian Grimberg "Cannot assemble operator diagonal with different input and output active element restrictions"); 319506b1a0cSSebastian Grimberg 3201c66c397SJeremy L Thompson // Assemble point block diagonal restriction, if needed 321bd83916cSSebastian Grimberg if (is_point_block) { 322*934a29f5SSebastian Grimberg CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstrs_in[b_in], &diag_elem_rstr)); 3237c1dbaffSSebastian Grimberg } else { 324*934a29f5SSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnsignedCopy(active_elem_rstrs_in[b_in], &diag_elem_rstr)); 325eaf62fffSJeremy L Thompson } 326eaf62fffSJeremy L Thompson 327eaf62fffSJeremy L Thompson // Create diagonal vector 328437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag)); 329eaf62fffSJeremy L Thompson 330eaf62fffSJeremy L Thompson // Assemble element operator diagonals 3312b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(elem_diag, 0.0)); 3322b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array)); 333437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem)); 334*934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumNodes(active_bases_in[b_in], &num_nodes)); 335*934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumComponents(active_bases_in[b_in], &num_comp)); 336*934a29f5SSebastian Grimberg if (active_bases_in[b_in] == CEED_BASIS_NONE) num_qpts = num_nodes; 337*934a29f5SSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(active_bases_in[b_in], &num_qpts)); 338ed9e99e6SJeremy L Thompson 339352a5e7cSSebastian Grimberg // Construct identity matrix for basis if required 340*934a29f5SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in[b_in]; i++) { 341*934a29f5SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_in[b_in][i] == CEED_EVAL_NONE); 342ed9e99e6SJeremy L Thompson } 343*934a29f5SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out[b_out]; i++) { 344*934a29f5SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_out[b_out][i] == CEED_EVAL_NONE); 345ed9e99e6SJeremy L Thompson } 346ed9e99e6SJeremy L Thompson if (has_eval_none) { 3472b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_qpts * num_nodes, &identity)); 3482b730f8bSJeremy L Thompson for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0; 349eaf62fffSJeremy L Thompson } 350352a5e7cSSebastian Grimberg 351eaf62fffSJeremy L Thompson // Compute the diagonal of B^T D B 352eaf62fffSJeremy L Thompson // Each element 353b94338b9SJed Brown for (CeedSize e = 0; e < num_elem; e++) { 354eaf62fffSJeremy L Thompson // Each basis eval mode pair 355352a5e7cSSebastian Grimberg CeedInt d_out = 0, q_comp_out; 356352a5e7cSSebastian Grimberg CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE; 3571c66c397SJeremy L Thompson 358*934a29f5SSebastian Grimberg for (CeedInt e_out = 0; e_out < num_eval_modes_out[b_out]; e_out++) { 3591c66c397SJeremy L Thompson CeedInt d_in = 0, q_comp_in; 360437c7c90SJeremy L Thompson const CeedScalar *B_t = NULL; 3611c66c397SJeremy L Thompson CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE; 3621c66c397SJeremy L Thompson 363*934a29f5SSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(active_bases_out[b_out], eval_modes_out[b_out][e_out], identity, &B_t)); 364*934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_out[b_out], eval_modes_out[b_out][e_out], &q_comp_out)); 365352a5e7cSSebastian Grimberg if (q_comp_out > 1) { 366*934a29f5SSebastian Grimberg if (e_out == 0 || eval_modes_out[b_out][e_out] != eval_mode_out_prev) d_out = 0; 367352a5e7cSSebastian Grimberg else B_t = &B_t[(++d_out) * num_qpts * num_nodes]; 368352a5e7cSSebastian Grimberg } 369*934a29f5SSebastian Grimberg eval_mode_out_prev = eval_modes_out[b_out][e_out]; 370352a5e7cSSebastian Grimberg 371*934a29f5SSebastian Grimberg for (CeedInt e_in = 0; e_in < num_eval_modes_in[b_in]; e_in++) { 372437c7c90SJeremy L Thompson const CeedScalar *B = NULL; 3731c66c397SJeremy L Thompson 374*934a29f5SSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(active_bases_in[b_in], eval_modes_in[b_in][e_in], identity, &B)); 375*934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_in[b_in], eval_modes_in[b_in][e_in], &q_comp_in)); 376352a5e7cSSebastian Grimberg if (q_comp_in > 1) { 377*934a29f5SSebastian Grimberg if (e_in == 0 || eval_modes_in[b_in][e_in] != eval_mode_in_prev) d_in = 0; 378352a5e7cSSebastian Grimberg else B = &B[(++d_in) * num_qpts * num_nodes]; 379352a5e7cSSebastian Grimberg } 380*934a29f5SSebastian Grimberg eval_mode_in_prev = eval_modes_in[b_in][e_in]; 381352a5e7cSSebastian Grimberg 382eaf62fffSJeremy L Thompson // Each component 383506b1a0cSSebastian Grimberg for (CeedInt c_out = 0; c_out < num_comp; c_out++) { 384437c7c90SJeremy L Thompson // Each qpt/node pair 3852b730f8bSJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 386bd83916cSSebastian Grimberg if (is_point_block) { 387eaf62fffSJeremy L Thompson // Point Block Diagonal 388506b1a0cSSebastian Grimberg for (CeedInt c_in = 0; c_in < num_comp; c_in++) { 389*934a29f5SSebastian Grimberg const CeedSize c_offset = 390*934a29f5SSebastian Grimberg (eval_mode_offsets_in[b_in][e_in] + c_in) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out; 391506b1a0cSSebastian Grimberg const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]]; 3921c66c397SJeremy L Thompson 3932b730f8bSJeremy L Thompson for (CeedInt n = 0; n < num_nodes; n++) { 394506b1a0cSSebastian Grimberg elem_diag_array[((e * num_comp + c_out) * num_comp + c_in) * num_nodes + n] += 395437c7c90SJeremy L Thompson B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n]; 396eaf62fffSJeremy L Thompson } 3972b730f8bSJeremy L Thompson } 398eaf62fffSJeremy L Thompson } else { 399eaf62fffSJeremy L Thompson // Diagonal Only 400*934a29f5SSebastian Grimberg const CeedInt c_offset = 401*934a29f5SSebastian Grimberg (eval_mode_offsets_in[b_in][e_in] + c_out) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out; 402506b1a0cSSebastian Grimberg const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]]; 4031c66c397SJeremy L Thompson 4042b730f8bSJeremy L Thompson for (CeedInt n = 0; n < num_nodes; n++) { 405506b1a0cSSebastian 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]; 406eaf62fffSJeremy L Thompson } 407eaf62fffSJeremy L Thompson } 408eaf62fffSJeremy L Thompson } 409eaf62fffSJeremy L Thompson } 4102b730f8bSJeremy L Thompson } 4112b730f8bSJeremy L Thompson } 4122b730f8bSJeremy L Thompson } 4132b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array)); 414eaf62fffSJeremy L Thompson 415eaf62fffSJeremy L Thompson // Assemble local operator diagonal 4167c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionApply(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request)); 417eaf62fffSJeremy L Thompson 418eaf62fffSJeremy L Thompson // Cleanup 4197c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr)); 4202b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&elem_diag)); 4212b730f8bSJeremy L Thompson CeedCall(CeedFree(&identity)); 422437c7c90SJeremy L Thompson } 423437c7c90SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 424437c7c90SJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled_qf)); 425eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 426eaf62fffSJeremy L Thompson } 427eaf62fffSJeremy L Thompson 428eaf62fffSJeremy L Thompson /** 429eaf62fffSJeremy L Thompson @brief Core logic for assembling composite operator diagonal 430eaf62fffSJeremy L Thompson 431eaf62fffSJeremy L Thompson @param[in] op CeedOperator to assemble point block diagonal 432ea61e9acSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else CEED_REQUEST_IMMEDIATE 433bd83916cSSebastian Grimberg @param[in] is_point_block Boolean flag to assemble diagonal or point block diagonal 434eaf62fffSJeremy L Thompson @param[out] assembled CeedVector to store assembled diagonal 435eaf62fffSJeremy L Thompson 436eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 437eaf62fffSJeremy L Thompson 438eaf62fffSJeremy L Thompson @ref Developer 439eaf62fffSJeremy L Thompson **/ 440bd83916cSSebastian Grimberg static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block, 441eaf62fffSJeremy L Thompson CeedVector assembled) { 442eaf62fffSJeremy L Thompson CeedInt num_sub; 443eaf62fffSJeremy L Thompson CeedOperator *suboperators; 4441c66c397SJeremy L Thompson 445c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); 446c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators)); 447eaf62fffSJeremy L Thompson for (CeedInt i = 0; i < num_sub; i++) { 448bd83916cSSebastian Grimberg if (is_point_block) { 4492b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request)); 4506aa95790SJeremy L Thompson } else { 4512b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request)); 4526aa95790SJeremy L Thompson } 453eaf62fffSJeremy L Thompson } 454eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 455eaf62fffSJeremy L Thompson } 456eaf62fffSJeremy L Thompson 457eaf62fffSJeremy L Thompson /** 458eaf62fffSJeremy L Thompson @brief Build nonzero pattern for non-composite operator 459eaf62fffSJeremy L Thompson 460eaf62fffSJeremy L Thompson Users should generally use CeedOperatorLinearAssembleSymbolic() 461eaf62fffSJeremy L Thompson 462eaf62fffSJeremy L Thompson @param[in] op CeedOperator to assemble nonzero pattern 463eaf62fffSJeremy L Thompson @param[in] offset Offset for number of entries 464eaf62fffSJeremy L Thompson @param[out] rows Row number for each entry 465eaf62fffSJeremy L Thompson @param[out] cols Column number for each entry 466eaf62fffSJeremy L Thompson 467eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 468eaf62fffSJeremy L Thompson 469eaf62fffSJeremy L Thompson @ref Developer 470eaf62fffSJeremy L Thompson **/ 4712b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) { 472f3d47e36SJeremy L Thompson Ceed ceed; 473f3d47e36SJeremy L Thompson bool is_composite; 474506b1a0cSSebastian Grimberg CeedSize num_nodes_in, num_nodes_out, count = 0; 475506b1a0cSSebastian Grimberg CeedInt num_elem_in, elem_size_in, num_comp_in, layout_er_in[3]; 476506b1a0cSSebastian Grimberg CeedInt num_elem_out, elem_size_out, num_comp_out, layout_er_out[3], local_num_entries; 4771c66c397SJeremy L Thompson CeedScalar *array; 478506b1a0cSSebastian Grimberg const CeedScalar *elem_dof_a_in, *elem_dof_a_out; 479506b1a0cSSebastian Grimberg CeedVector index_vec_in, index_vec_out, elem_dof_in, elem_dof_out; 480506b1a0cSSebastian Grimberg CeedElemRestriction elem_rstr_in, elem_rstr_out, index_elem_rstr_in, index_elem_rstr_out; 4811c66c397SJeremy L Thompson 482f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 483f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 4846574a04fSJeremy L Thompson CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 485eaf62fffSJeremy L Thompson 486506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes_in, &num_nodes_out)); 487506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out)); 488506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in)); 489506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in)); 490506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in)); 491506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetELayout(elem_rstr_in, &layout_er_in)); 492eaf62fffSJeremy L Thompson 493506b1a0cSSebastian Grimberg // Determine elem_dof relation for input 494506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_nodes_in, &index_vec_in)); 495506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayWrite(index_vec_in, CEED_MEM_HOST, &array)); 496506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_nodes_in; i++) array[i] = i; 497506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArray(index_vec_in, &array)); 498506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_elem_in * elem_size_in * num_comp_in, &elem_dof_in)); 499506b1a0cSSebastian Grimberg CeedCall(CeedVectorSetValue(elem_dof_in, 0.0)); 500506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_in, &index_elem_rstr_in)); 501506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionApply(index_elem_rstr_in, CEED_NOTRANSPOSE, index_vec_in, elem_dof_in, CEED_REQUEST_IMMEDIATE)); 502506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayRead(elem_dof_in, CEED_MEM_HOST, &elem_dof_a_in)); 503506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&index_vec_in)); 504506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_in)); 505506b1a0cSSebastian Grimberg 506506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 507506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out)); 508506b1a0cSSebastian Grimberg CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, 509506b1a0cSSebastian Grimberg "Active input and output operator restrictions must have the same number of elements"); 510506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out)); 511506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out)); 512506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetELayout(elem_rstr_out, &layout_er_out)); 513506b1a0cSSebastian Grimberg 514506b1a0cSSebastian Grimberg // Determine elem_dof relation for output 515506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_nodes_out, &index_vec_out)); 516506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayWrite(index_vec_out, CEED_MEM_HOST, &array)); 517506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_nodes_out; i++) array[i] = i; 518506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArray(index_vec_out, &array)); 519506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_elem_out * elem_size_out * num_comp_out, &elem_dof_out)); 520506b1a0cSSebastian Grimberg CeedCall(CeedVectorSetValue(elem_dof_out, 0.0)); 521506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_out, &index_elem_rstr_out)); 522506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionApply(index_elem_rstr_out, CEED_NOTRANSPOSE, index_vec_out, elem_dof_out, CEED_REQUEST_IMMEDIATE)); 523506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayRead(elem_dof_out, CEED_MEM_HOST, &elem_dof_a_out)); 524506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&index_vec_out)); 525506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_out)); 526506b1a0cSSebastian Grimberg } else { 527506b1a0cSSebastian Grimberg num_elem_out = num_elem_in; 528506b1a0cSSebastian Grimberg elem_size_out = elem_size_in; 529506b1a0cSSebastian Grimberg num_comp_out = num_comp_in; 530506b1a0cSSebastian Grimberg layout_er_out[0] = layout_er_in[0]; 531506b1a0cSSebastian Grimberg layout_er_out[1] = layout_er_in[1]; 532506b1a0cSSebastian Grimberg layout_er_out[2] = layout_er_in[2]; 533506b1a0cSSebastian Grimberg elem_dof_a_out = elem_dof_a_in; 534506b1a0cSSebastian Grimberg } 535506b1a0cSSebastian Grimberg local_num_entries = elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in; 536eaf62fffSJeremy L Thompson 537eaf62fffSJeremy L Thompson // Determine i, j locations for element matrices 538506b1a0cSSebastian Grimberg for (CeedInt e = 0; e < num_elem_in; e++) { 539506b1a0cSSebastian Grimberg for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) { 540506b1a0cSSebastian Grimberg for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) { 541506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 542506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 543506b1a0cSSebastian Grimberg const CeedInt elem_dof_index_row = i * layout_er_out[0] + comp_out * layout_er_out[1] + e * layout_er_out[2]; 544506b1a0cSSebastian Grimberg const CeedInt elem_dof_index_col = j * layout_er_in[0] + comp_in * layout_er_in[1] + e * layout_er_in[2]; 545506b1a0cSSebastian Grimberg const CeedInt row = elem_dof_a_out[elem_dof_index_row]; 546506b1a0cSSebastian Grimberg const CeedInt col = elem_dof_a_in[elem_dof_index_col]; 547eaf62fffSJeremy L Thompson 548eaf62fffSJeremy L Thompson rows[offset + count] = row; 549eaf62fffSJeremy L Thompson cols[offset + count] = col; 550eaf62fffSJeremy L Thompson count++; 551eaf62fffSJeremy L Thompson } 552eaf62fffSJeremy L Thompson } 553eaf62fffSJeremy L Thompson } 554eaf62fffSJeremy L Thompson } 555eaf62fffSJeremy L Thompson } 5566574a04fSJeremy L Thompson CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing assembled entries"); 557506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArrayRead(elem_dof_in, &elem_dof_a_in)); 558506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&elem_dof_in)); 559506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 560506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArrayRead(elem_dof_out, &elem_dof_a_out)); 561506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&elem_dof_out)); 562506b1a0cSSebastian Grimberg } 563eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 564eaf62fffSJeremy L Thompson } 565eaf62fffSJeremy L Thompson 566eaf62fffSJeremy L Thompson /** 567eaf62fffSJeremy L Thompson @brief Assemble nonzero entries for non-composite operator 568eaf62fffSJeremy L Thompson 569eaf62fffSJeremy L Thompson Users should generally use CeedOperatorLinearAssemble() 570eaf62fffSJeremy L Thompson 571eaf62fffSJeremy L Thompson @param[in] op CeedOperator to assemble 572ea61e9acSJeremy L Thompson @param[in] offset Offset for number of entries 573eaf62fffSJeremy L Thompson @param[out] values Values to assemble into matrix 574eaf62fffSJeremy L Thompson 575eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 576eaf62fffSJeremy L Thompson 577eaf62fffSJeremy L Thompson @ref Developer 578eaf62fffSJeremy L Thompson **/ 5792b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) { 580f3d47e36SJeremy L Thompson Ceed ceed; 581f3d47e36SJeremy L Thompson bool is_composite; 5821c66c397SJeremy L Thompson 583f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 584f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 5856574a04fSJeremy L Thompson CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 586f3d47e36SJeremy L Thompson 587f3d47e36SJeremy L Thompson // Early exit for empty operator 588f3d47e36SJeremy L Thompson { 589f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 590f3d47e36SJeremy L Thompson 591f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 592f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 593f3d47e36SJeremy L Thompson } 594eaf62fffSJeremy L Thompson 595cefa2673SJeremy L Thompson if (op->LinearAssembleSingle) { 596cefa2673SJeremy L Thompson // Backend version 5972b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleSingle(op, offset, values)); 598cefa2673SJeremy L Thompson return CEED_ERROR_SUCCESS; 599cefa2673SJeremy L Thompson } else { 600cefa2673SJeremy L Thompson // Operator fallback 601cefa2673SJeremy L Thompson CeedOperator op_fallback; 602cefa2673SJeremy L Thompson 6032b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 604cefa2673SJeremy L Thompson if (op_fallback) { 6052b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values)); 606cefa2673SJeremy L Thompson return CEED_ERROR_SUCCESS; 607cefa2673SJeremy L Thompson } 608cefa2673SJeremy L Thompson } 609cefa2673SJeremy L Thompson 610eaf62fffSJeremy L Thompson // Assemble QFunction 611506b1a0cSSebastian Grimberg CeedInt layout_qf[3]; 6121c66c397SJeremy L Thompson const CeedScalar *assembled_qf_array; 613c5f45aeaSJeremy L Thompson CeedVector assembled_qf = NULL; 614506b1a0cSSebastian Grimberg CeedElemRestriction assembled_elem_rstr = NULL; 615eaf62fffSJeremy L Thompson 616506b1a0cSSebastian Grimberg CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, CEED_REQUEST_IMMEDIATE)); 617506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, &layout_qf)); 618506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr)); 619506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array)); 620eaf62fffSJeremy L Thompson 621ed9e99e6SJeremy L Thompson // Get assembly data 622506b1a0cSSebastian Grimberg CeedInt num_elem_in, elem_size_in, num_comp_in, num_qpts_in; 623506b1a0cSSebastian Grimberg CeedInt num_elem_out, elem_size_out, num_comp_out, num_qpts_out, local_num_entries; 624506b1a0cSSebastian Grimberg const CeedEvalMode **eval_modes_in, **eval_modes_out; 625506b1a0cSSebastian Grimberg CeedInt num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out; 626506b1a0cSSebastian Grimberg CeedBasis *active_bases_in, *active_bases_out, basis_in, basis_out; 627506b1a0cSSebastian Grimberg const CeedScalar **B_mats_in, **B_mats_out, *B_mat_in, *B_mat_out; 628506b1a0cSSebastian Grimberg CeedElemRestriction elem_rstr_in, elem_rstr_out; 629506b1a0cSSebastian Grimberg CeedRestrictionType elem_rstr_type_in, elem_rstr_type_out; 630506b1a0cSSebastian Grimberg const bool *elem_rstr_orients_in = NULL, *elem_rstr_orients_out = NULL; 631506b1a0cSSebastian Grimberg const CeedInt8 *elem_rstr_curl_orients_in = NULL, *elem_rstr_curl_orients_out = NULL; 632506b1a0cSSebastian Grimberg CeedOperatorAssemblyData data; 633eaf62fffSJeremy L Thompson 634506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data)); 635506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, NULL, &num_active_bases_out, 636506b1a0cSSebastian Grimberg &num_eval_modes_out, &eval_modes_out, NULL, NULL)); 637506b1a0cSSebastian Grimberg 638506b1a0cSSebastian Grimberg CeedCheck(num_active_bases_in == num_active_bases_out && num_active_bases_in == 1, ceed, CEED_ERROR_UNSUPPORTED, 639506b1a0cSSebastian Grimberg "Cannot assemble operator with multiple active bases"); 6406574a04fSJeremy 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"); 641eaf62fffSJeremy L Thompson 642506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, &B_mats_in, NULL, &active_bases_out, &B_mats_out)); 643506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out)); 644506b1a0cSSebastian Grimberg basis_in = active_bases_in[0]; 645506b1a0cSSebastian Grimberg basis_out = active_bases_out[0]; 646506b1a0cSSebastian Grimberg B_mat_in = B_mats_in[0]; 647506b1a0cSSebastian Grimberg B_mat_out = B_mats_out[0]; 648eaf62fffSJeremy L Thompson 649506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in)); 650506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in)); 651506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in)); 652506b1a0cSSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in; 653506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in)); 654506b1a0cSSebastian Grimberg 655506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetType(elem_rstr_in, &elem_rstr_type_in)); 656506b1a0cSSebastian Grimberg if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) { 657506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_orients_in)); 658506b1a0cSSebastian Grimberg } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 659506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_curl_orients_in)); 6607c1dbaffSSebastian Grimberg } 6617c1dbaffSSebastian Grimberg 662506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 663506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out)); 664506b1a0cSSebastian Grimberg CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, 665506b1a0cSSebastian Grimberg "Active input and output operator restrictions must have the same number of elements"); 666506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out)); 667506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out)); 668506b1a0cSSebastian Grimberg if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out; 669506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out)); 670506b1a0cSSebastian Grimberg CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED, 671506b1a0cSSebastian Grimberg "Active input and output bases must have the same number of quadrature points"); 672eaf62fffSJeremy L Thompson 673506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetType(elem_rstr_out, &elem_rstr_type_out)); 674506b1a0cSSebastian Grimberg if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) { 675506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_orients_out)); 676506b1a0cSSebastian Grimberg } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 677506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_curl_orients_out)); 678506b1a0cSSebastian Grimberg } 679506b1a0cSSebastian Grimberg } else { 680506b1a0cSSebastian Grimberg num_elem_out = num_elem_in; 681506b1a0cSSebastian Grimberg elem_size_out = elem_size_in; 682506b1a0cSSebastian Grimberg num_comp_out = num_comp_in; 683506b1a0cSSebastian Grimberg num_qpts_out = num_qpts_in; 684506b1a0cSSebastian Grimberg 685506b1a0cSSebastian Grimberg elem_rstr_orients_out = elem_rstr_orients_in; 686506b1a0cSSebastian Grimberg elem_rstr_curl_orients_out = elem_rstr_curl_orients_in; 687506b1a0cSSebastian Grimberg } 688506b1a0cSSebastian Grimberg local_num_entries = elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in; 689506b1a0cSSebastian Grimberg 690506b1a0cSSebastian Grimberg // Loop over elements and put in data structure 6917c1dbaffSSebastian Grimberg // We store B_mat_in, B_mat_out, BTD, elem_mat in row-major order 6921c66c397SJeremy L Thompson CeedSize count = 0; 693123d890dSSebastian Grimberg CeedScalar *vals, *BTD_mat = NULL, *elem_mat = NULL, *elem_mat_b = NULL; 694506b1a0cSSebastian Grimberg 695123d890dSSebastian Grimberg CeedCall(CeedCalloc(elem_size_out * num_qpts_in * num_eval_modes_in[0], &BTD_mat)); 696123d890dSSebastian Grimberg CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat)); 697506b1a0cSSebastian Grimberg if (elem_rstr_curl_orients_in || elem_rstr_curl_orients_out) CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat_b)); 6981c66c397SJeremy L Thompson 69928ec399dSJeremy L Thompson CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals)); 700506b1a0cSSebastian Grimberg for (CeedSize e = 0; e < num_elem_in; e++) { 701506b1a0cSSebastian Grimberg for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) { 702506b1a0cSSebastian Grimberg for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) { 703ed9e99e6SJeremy L Thompson // Compute B^T*D 704506b1a0cSSebastian Grimberg for (CeedSize n = 0; n < elem_size_out; n++) { 705506b1a0cSSebastian Grimberg for (CeedSize q = 0; q < num_qpts_in; q++) { 706437c7c90SJeremy L Thompson for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) { 707506b1a0cSSebastian Grimberg const CeedSize btd_index = n * (num_qpts_in * num_eval_modes_in[0]) + q * num_eval_modes_in[0] + e_in; 708067fd99fSJeremy L Thompson CeedScalar sum = 0.0; 7091c66c397SJeremy L Thompson 710437c7c90SJeremy L Thompson for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) { 711506b1a0cSSebastian Grimberg const CeedSize b_out_index = (q * num_eval_modes_out[0] + e_out) * elem_size_out + n; 712506b1a0cSSebastian 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; 713b94338b9SJed Brown const CeedSize qf_index = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2]; 7141c66c397SJeremy L Thompson 715067fd99fSJeremy L Thompson sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index]; 716eaf62fffSJeremy L Thompson } 717067fd99fSJeremy L Thompson BTD_mat[btd_index] = sum; 718ed9e99e6SJeremy L Thompson } 719ed9e99e6SJeremy L Thompson } 720eaf62fffSJeremy L Thompson } 7217c1dbaffSSebastian Grimberg 7227c1dbaffSSebastian Grimberg // Form element matrix itself (for each block component) 723506b1a0cSSebastian 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])); 724eaf62fffSJeremy L Thompson 7257c1dbaffSSebastian Grimberg // Transform the element matrix if required 726506b1a0cSSebastian Grimberg if (elem_rstr_orients_out) { 727506b1a0cSSebastian Grimberg const bool *elem_orients = &elem_rstr_orients_out[e * elem_size_out]; 7281c66c397SJeremy L Thompson 729506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 730506b1a0cSSebastian Grimberg const double orient = elem_orients[i] ? -1.0 : 1.0; 731506b1a0cSSebastian Grimberg 732506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 733506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] *= orient; 7347c1dbaffSSebastian Grimberg } 7357c1dbaffSSebastian Grimberg } 736506b1a0cSSebastian Grimberg } else if (elem_rstr_curl_orients_out) { 737506b1a0cSSebastian Grimberg const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_out[e * 3 * elem_size_out]; 7381c66c397SJeremy L Thompson 7397c1dbaffSSebastian Grimberg // T^T*(B^T*D*B) 740506b1a0cSSebastian Grimberg memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar)); 741506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 742506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 743506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * i + 1] + 744506b1a0cSSebastian Grimberg (i > 0 ? elem_mat_b[(i - 1) * elem_size_in + j] * elem_curl_orients[3 * i - 1] : 0.0) + 745506b1a0cSSebastian Grimberg (i < elem_size_out - 1 ? elem_mat_b[(i + 1) * elem_size_in + j] * elem_curl_orients[3 * i + 3] : 0.0); 7467c1dbaffSSebastian Grimberg } 7477c1dbaffSSebastian Grimberg } 748506b1a0cSSebastian Grimberg } 749506b1a0cSSebastian Grimberg if (elem_rstr_orients_in) { 750506b1a0cSSebastian Grimberg const bool *elem_orients = &elem_rstr_orients_in[e * elem_size_in]; 751506b1a0cSSebastian Grimberg 752506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 753506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 754506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] *= elem_orients[j] ? -1.0 : 1.0; 755506b1a0cSSebastian Grimberg } 756506b1a0cSSebastian Grimberg } 757506b1a0cSSebastian Grimberg } else if (elem_rstr_curl_orients_in) { 758506b1a0cSSebastian Grimberg const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_in[e * 3 * elem_size_in]; 759506b1a0cSSebastian Grimberg 760506b1a0cSSebastian Grimberg // (B^T*D*B)*T 761506b1a0cSSebastian Grimberg memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar)); 762506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 763506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 764506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * j + 1] + 765506b1a0cSSebastian Grimberg (j > 0 ? elem_mat_b[i * elem_size_in + j - 1] * elem_curl_orients[3 * j - 1] : 0.0) + 766506b1a0cSSebastian Grimberg (j < elem_size_in - 1 ? elem_mat_b[i * elem_size_in + j + 1] * elem_curl_orients[3 * j + 3] : 0.0); 7677c1dbaffSSebastian Grimberg } 7687c1dbaffSSebastian Grimberg } 7697c1dbaffSSebastian Grimberg } 7707c1dbaffSSebastian Grimberg 7717c1dbaffSSebastian Grimberg // Put element matrix in coordinate data structure 772506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 773506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 774506b1a0cSSebastian Grimberg vals[offset + count] = elem_mat[i * elem_size_in + j]; 775eaf62fffSJeremy L Thompson count++; 776eaf62fffSJeremy L Thompson } 777eaf62fffSJeremy L Thompson } 778eaf62fffSJeremy L Thompson } 779eaf62fffSJeremy L Thompson } 780eaf62fffSJeremy L Thompson } 7816574a04fSJeremy L Thompson CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing entries"); 7822b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(values, &vals)); 783eaf62fffSJeremy L Thompson 784506b1a0cSSebastian Grimberg // Cleanup 785123d890dSSebastian Grimberg CeedCall(CeedFree(&BTD_mat)); 786123d890dSSebastian Grimberg CeedCall(CeedFree(&elem_mat)); 787506b1a0cSSebastian Grimberg CeedCall(CeedFree(&elem_mat_b)); 788506b1a0cSSebastian Grimberg if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) { 789506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_in, &elem_rstr_orients_in)); 790506b1a0cSSebastian Grimberg } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 791506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_in, &elem_rstr_curl_orients_in)); 792506b1a0cSSebastian Grimberg } 793506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 794506b1a0cSSebastian Grimberg if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) { 795506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_out, &elem_rstr_orients_out)); 796506b1a0cSSebastian Grimberg } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 797506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_out, &elem_rstr_curl_orients_out)); 798506b1a0cSSebastian Grimberg } 799506b1a0cSSebastian Grimberg } 8002b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 8012b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled_qf)); 802eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 803eaf62fffSJeremy L Thompson } 804eaf62fffSJeremy L Thompson 805eaf62fffSJeremy L Thompson /** 806eaf62fffSJeremy L Thompson @brief Count number of entries for assembled CeedOperator 807eaf62fffSJeremy L Thompson 808eaf62fffSJeremy L Thompson @param[in] op CeedOperator to assemble 809eaf62fffSJeremy L Thompson @param[out] num_entries Number of entries in assembled representation 810eaf62fffSJeremy L Thompson 811eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 812eaf62fffSJeremy L Thompson 813eaf62fffSJeremy L Thompson @ref Utility 814eaf62fffSJeremy L Thompson **/ 815b94338b9SJed Brown static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedSize *num_entries) { 816b275c451SJeremy L Thompson bool is_composite; 817506b1a0cSSebastian Grimberg CeedInt num_elem_in, elem_size_in, num_comp_in, num_elem_out, elem_size_out, num_comp_out; 818506b1a0cSSebastian Grimberg CeedElemRestriction rstr_in, rstr_out; 819eaf62fffSJeremy L Thompson 820b275c451SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 8216574a04fSJeremy L Thompson CeedCheck(!is_composite, op->ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 822506b1a0cSSebastian Grimberg 823506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out)); 824506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in)); 825506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in)); 826506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in)); 827506b1a0cSSebastian Grimberg if (rstr_in != rstr_out) { 828506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out)); 829506b1a0cSSebastian Grimberg CeedCheck(num_elem_in == num_elem_out, op->ceed, CEED_ERROR_UNSUPPORTED, 830506b1a0cSSebastian Grimberg "Active input and output operator restrictions must have the same number of elements"); 831506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out)); 832506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out)); 833506b1a0cSSebastian Grimberg } else { 834506b1a0cSSebastian Grimberg num_elem_out = num_elem_in; 835506b1a0cSSebastian Grimberg elem_size_out = elem_size_in; 836506b1a0cSSebastian Grimberg num_comp_out = num_comp_in; 837506b1a0cSSebastian Grimberg } 838506b1a0cSSebastian Grimberg *num_entries = (CeedSize)elem_size_in * num_comp_in * elem_size_out * num_comp_out * num_elem_in; 839eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 840eaf62fffSJeremy L Thompson } 841eaf62fffSJeremy L Thompson 842eaf62fffSJeremy L Thompson /** 843ea61e9acSJeremy L Thompson @brief Common code for creating a multigrid coarse operator and level transfer operators for a CeedOperator 844eaf62fffSJeremy L Thompson 845eaf62fffSJeremy L Thompson @param[in] op_fine Fine grid operator 84685bb9dcfSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators 847eaf62fffSJeremy L Thompson @param[in] rstr_coarse Coarse grid restriction 848eaf62fffSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector basis 84985bb9dcfSJeremy L Thompson @param[in] basis_c_to_f Basis for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators 850eaf62fffSJeremy L Thompson @param[out] op_coarse Coarse grid operator 85185bb9dcfSJeremy L Thompson @param[out] op_prolong Coarse to fine operator, or NULL 8527758292fSSebastian Grimberg @param[out] op_restrict Fine to coarse operator, or NULL 853eaf62fffSJeremy L Thompson 854eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 855eaf62fffSJeremy L Thompson 856eaf62fffSJeremy L Thompson @ref Developer 857eaf62fffSJeremy L Thompson **/ 8582b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 8597758292fSSebastian Grimberg CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) { 8601c66c397SJeremy L Thompson bool is_composite; 861eaf62fffSJeremy L Thompson Ceed ceed; 8621c66c397SJeremy L Thompson CeedInt num_comp; 86385bb9dcfSJeremy L Thompson CeedVector mult_vec = NULL; 8641c66c397SJeremy L Thompson CeedElemRestriction rstr_p_mult_fine = NULL, rstr_fine = NULL; 8651c66c397SJeremy L Thompson 8662b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op_fine, &ceed)); 867eaf62fffSJeremy L Thompson 868eaf62fffSJeremy L Thompson // Check for composite operator 8692b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op_fine, &is_composite)); 8706574a04fSJeremy L Thompson CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported"); 871eaf62fffSJeremy L Thompson 872eaf62fffSJeremy L Thompson // Coarse Grid 8732b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse)); 874eaf62fffSJeremy L Thompson // -- Clone input fields 87592ae7e47SJeremy L Thompson for (CeedInt i = 0; i < op_fine->qf->num_input_fields; i++) { 876eaf62fffSJeremy L Thompson if (op_fine->input_fields[i]->vec == CEED_VECTOR_ACTIVE) { 877437c7c90SJeremy L Thompson rstr_fine = op_fine->input_fields[i]->elem_rstr; 8782b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE)); 879eaf62fffSJeremy L Thompson } else { 880437c7c90SJeremy L Thompson CeedCall(CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, op_fine->input_fields[i]->elem_rstr, 8812b730f8bSJeremy L Thompson op_fine->input_fields[i]->basis, op_fine->input_fields[i]->vec)); 882eaf62fffSJeremy L Thompson } 883eaf62fffSJeremy L Thompson } 884eaf62fffSJeremy L Thompson // -- Clone output fields 88592ae7e47SJeremy L Thompson for (CeedInt i = 0; i < op_fine->qf->num_output_fields; i++) { 886eaf62fffSJeremy L Thompson if (op_fine->output_fields[i]->vec == CEED_VECTOR_ACTIVE) { 8872b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE)); 888eaf62fffSJeremy L Thompson } else { 889437c7c90SJeremy L Thompson CeedCall(CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, op_fine->output_fields[i]->elem_rstr, 8902b730f8bSJeremy L Thompson op_fine->output_fields[i]->basis, op_fine->output_fields[i]->vec)); 891eaf62fffSJeremy L Thompson } 892eaf62fffSJeremy L Thompson } 893af99e877SJeremy L Thompson // -- Clone QFunctionAssemblyData 8942b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op_fine->qf_assembled, &(*op_coarse)->qf_assembled)); 895eaf62fffSJeremy L Thompson 896eaf62fffSJeremy L Thompson // Multiplicity vector 8977758292fSSebastian Grimberg if (op_restrict || op_prolong) { 89885bb9dcfSJeremy L Thompson CeedVector mult_e_vec; 8991c66c397SJeremy L Thompson CeedRestrictionType rstr_type; 90085bb9dcfSJeremy L Thompson 9017c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionGetType(rstr_fine, &rstr_type)); 9027c1dbaffSSebastian Grimberg CeedCheck(rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_UNSUPPORTED, 9037c1dbaffSSebastian Grimberg "Element restrictions created with CeedElemRestrictionCreateCurlOriented are not supported"); 9046574a04fSJeremy L Thompson CeedCheck(p_mult_fine, ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector"); 9057c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnsignedCopy(rstr_fine, &rstr_p_mult_fine)); 9062b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec)); 9072b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(mult_e_vec, 0.0)); 908c17ec2beSJeremy L Thompson CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE)); 9092b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(mult_vec, 0.0)); 910c17ec2beSJeremy L Thompson CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE)); 9112b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&mult_e_vec)); 9122b730f8bSJeremy L Thompson CeedCall(CeedVectorReciprocal(mult_vec)); 91385bb9dcfSJeremy L Thompson } 914eaf62fffSJeremy L Thompson 915addd79feSZach Atkins // Clone name 916addd79feSZach Atkins bool has_name = op_fine->name; 917addd79feSZach Atkins size_t name_len = op_fine->name ? strlen(op_fine->name) : 0; 918addd79feSZach Atkins CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name)); 919addd79feSZach Atkins 9207758292fSSebastian Grimberg // Check that coarse to fine basis is provided if prolong/restrict operators are requested 9217758292fSSebastian Grimberg CeedCheck(basis_c_to_f || (!op_restrict && !op_prolong), ceed, CEED_ERROR_INCOMPATIBLE, 9226574a04fSJeremy L Thompson "Prolongation or restriction operator creation requires coarse-to-fine basis"); 92383d6adf3SZach Atkins 92485bb9dcfSJeremy L Thompson // Restriction/Prolongation Operators 9252b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp)); 926addd79feSZach Atkins 927addd79feSZach Atkins // Restriction 9287758292fSSebastian Grimberg if (op_restrict) { 929eaf62fffSJeremy L Thompson CeedInt *num_comp_r_data; 93085bb9dcfSJeremy L Thompson CeedQFunctionContext ctx_r; 9317758292fSSebastian Grimberg CeedQFunction qf_restrict; 93285bb9dcfSJeremy L Thompson 9337758292fSSebastian Grimberg CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict)); 9342b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &num_comp_r_data)); 935eaf62fffSJeremy L Thompson num_comp_r_data[0] = num_comp; 9362b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r)); 9372b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data)); 9387758292fSSebastian Grimberg CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r)); 9392b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(&ctx_r)); 9407758292fSSebastian Grimberg CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE)); 9417758292fSSebastian Grimberg CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE)); 9427758292fSSebastian Grimberg CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP)); 9437758292fSSebastian Grimberg CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp)); 944eaf62fffSJeremy L Thompson 9457758292fSSebastian Grimberg CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict)); 9467758292fSSebastian Grimberg CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE)); 9477758292fSSebastian Grimberg CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec)); 9487758292fSSebastian Grimberg CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE)); 949eaf62fffSJeremy L Thompson 950addd79feSZach Atkins // Set name 951addd79feSZach Atkins char *restriction_name; 9521c66c397SJeremy L Thompson 953addd79feSZach Atkins CeedCall(CeedCalloc(17 + name_len, &restriction_name)); 954addd79feSZach Atkins sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : ""); 9557758292fSSebastian Grimberg CeedCall(CeedOperatorSetName(*op_restrict, restriction_name)); 956addd79feSZach Atkins CeedCall(CeedFree(&restriction_name)); 957addd79feSZach Atkins 958addd79feSZach Atkins // Check 9597758292fSSebastian Grimberg CeedCall(CeedOperatorCheckReady(*op_restrict)); 960addd79feSZach Atkins 961addd79feSZach Atkins // Cleanup 9627758292fSSebastian Grimberg CeedCall(CeedQFunctionDestroy(&qf_restrict)); 963addd79feSZach Atkins } 964addd79feSZach Atkins 965eaf62fffSJeremy L Thompson // Prolongation 966addd79feSZach Atkins if (op_prolong) { 967eaf62fffSJeremy L Thompson CeedInt *num_comp_p_data; 96885bb9dcfSJeremy L Thompson CeedQFunctionContext ctx_p; 9691c66c397SJeremy L Thompson CeedQFunction qf_prolong; 97085bb9dcfSJeremy L Thompson 97185bb9dcfSJeremy L Thompson CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong)); 9722b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &num_comp_p_data)); 973eaf62fffSJeremy L Thompson num_comp_p_data[0] = num_comp; 9742b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p)); 9752b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data)); 9762b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p)); 9772b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(&ctx_p)); 9782b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP)); 9792b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE)); 9802b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE)); 9812b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp)); 982eaf62fffSJeremy L Thompson 9832b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong)); 9842b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE)); 985356036faSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec)); 986356036faSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE)); 987eaf62fffSJeremy L Thompson 988addd79feSZach Atkins // Set name 989ea6b5821SJeremy L Thompson char *prolongation_name; 9901c66c397SJeremy L Thompson 9912b730f8bSJeremy L Thompson CeedCall(CeedCalloc(18 + name_len, &prolongation_name)); 9922b730f8bSJeremy L Thompson sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : ""); 9932b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name)); 9942b730f8bSJeremy L Thompson CeedCall(CeedFree(&prolongation_name)); 995addd79feSZach Atkins 996addd79feSZach Atkins // Check 997addd79feSZach Atkins CeedCall(CeedOperatorCheckReady(*op_prolong)); 998addd79feSZach Atkins 999addd79feSZach Atkins // Cleanup 1000addd79feSZach Atkins CeedCall(CeedQFunctionDestroy(&qf_prolong)); 1001ea6b5821SJeremy L Thompson } 1002ea6b5821SJeremy L Thompson 100358e4b056SJeremy L Thompson // Check 100458e4b056SJeremy L Thompson CeedCall(CeedOperatorCheckReady(*op_coarse)); 100558e4b056SJeremy L Thompson 1006eaf62fffSJeremy L Thompson // Cleanup 10072b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&mult_vec)); 1008c17ec2beSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine)); 10092b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_c_to_f)); 1010eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1011eaf62fffSJeremy L Thompson } 1012eaf62fffSJeremy L Thompson 1013eaf62fffSJeremy L Thompson /** 1014eaf62fffSJeremy L Thompson @brief Build 1D mass matrix and Laplacian with perturbation 1015eaf62fffSJeremy L Thompson 1016eaf62fffSJeremy L Thompson @param[in] interp_1d Interpolation matrix in one dimension 1017eaf62fffSJeremy L Thompson @param[in] grad_1d Gradient matrix in one dimension 1018eaf62fffSJeremy L Thompson @param[in] q_weight_1d Quadrature weights in one dimension 1019eaf62fffSJeremy L Thompson @param[in] P_1d Number of basis nodes in one dimension 1020eaf62fffSJeremy L Thompson @param[in] Q_1d Number of quadrature points in one dimension 1021eaf62fffSJeremy L Thompson @param[in] dim Dimension of basis 1022eaf62fffSJeremy L Thompson @param[out] mass Assembled mass matrix in one dimension 1023eaf62fffSJeremy L Thompson @param[out] laplace Assembled perturbed Laplacian in one dimension 1024eaf62fffSJeremy L Thompson 1025eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1026eaf62fffSJeremy L Thompson 1027eaf62fffSJeremy L Thompson @ref Developer 1028eaf62fffSJeremy L Thompson **/ 10292c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 10302c2ea1dbSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d, CeedInt P_1d, CeedInt Q_1d, 10312c2ea1dbSJeremy L Thompson CeedInt dim, CeedScalar *mass, CeedScalar *laplace) { 10322b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) { 1033eaf62fffSJeremy L Thompson for (CeedInt j = 0; j < P_1d; j++) { 1034eaf62fffSJeremy L Thompson CeedScalar sum = 0.0; 10352b730f8bSJeremy 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]; 1036eaf62fffSJeremy L Thompson mass[i + j * P_1d] = sum; 1037eaf62fffSJeremy L Thompson } 10382b730f8bSJeremy L Thompson } 1039eaf62fffSJeremy L Thompson // -- Laplacian 10402b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) { 1041eaf62fffSJeremy L Thompson for (CeedInt j = 0; j < P_1d; j++) { 1042eaf62fffSJeremy L Thompson CeedScalar sum = 0.0; 10431c66c397SJeremy L Thompson 10442b730f8bSJeremy 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]; 1045eaf62fffSJeremy L Thompson laplace[i + j * P_1d] = sum; 1046eaf62fffSJeremy L Thompson } 10472b730f8bSJeremy L Thompson } 1048eaf62fffSJeremy L Thompson CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4; 10492b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation; 1050eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1051eaf62fffSJeremy L Thompson } 10522c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 1053eaf62fffSJeremy L Thompson 1054eaf62fffSJeremy L Thompson /// @} 1055eaf62fffSJeremy L Thompson 1056eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 1057480fae85SJeremy L Thompson /// CeedOperator Backend API 1058480fae85SJeremy L Thompson /// ---------------------------------------------------------------------------- 1059480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend 1060480fae85SJeremy L Thompson /// @{ 1061480fae85SJeremy L Thompson 1062480fae85SJeremy L Thompson /** 1063506b1a0cSSebastian Grimberg @brief Create point block restriction for active operator field 1064506b1a0cSSebastian Grimberg 1065506b1a0cSSebastian Grimberg @param[in] rstr Original CeedElemRestriction for active field 1066506b1a0cSSebastian Grimberg @param[out] point_block_rstr Address of the variable where the newly created CeedElemRestriction will be stored 1067506b1a0cSSebastian Grimberg 1068506b1a0cSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1069506b1a0cSSebastian Grimberg 1070506b1a0cSSebastian Grimberg @ref Backend 1071506b1a0cSSebastian Grimberg **/ 1072506b1a0cSSebastian Grimberg int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *point_block_rstr) { 1073506b1a0cSSebastian Grimberg Ceed ceed; 1074506b1a0cSSebastian Grimberg CeedInt num_elem, num_comp, shift, elem_size, comp_stride, *point_block_offsets; 1075506b1a0cSSebastian Grimberg CeedSize l_size; 1076506b1a0cSSebastian Grimberg const CeedInt *offsets; 1077506b1a0cSSebastian Grimberg 1078506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed)); 1079506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets)); 1080506b1a0cSSebastian Grimberg 1081506b1a0cSSebastian Grimberg // Expand offsets 1082506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem)); 1083506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp)); 1084506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size)); 1085506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride)); 1086506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size)); 1087506b1a0cSSebastian Grimberg shift = num_comp; 1088506b1a0cSSebastian Grimberg if (comp_stride != 1) shift *= num_comp; 1089506b1a0cSSebastian Grimberg CeedCall(CeedCalloc(num_elem * elem_size, &point_block_offsets)); 1090506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_elem * elem_size; i++) { 1091506b1a0cSSebastian Grimberg point_block_offsets[i] = offsets[i] * shift; 1092506b1a0cSSebastian Grimberg } 1093506b1a0cSSebastian Grimberg 1094506b1a0cSSebastian Grimberg // Create new restriction 1095506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER, 1096506b1a0cSSebastian Grimberg point_block_offsets, point_block_rstr)); 1097506b1a0cSSebastian Grimberg 1098506b1a0cSSebastian Grimberg // Cleanup 1099506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets)); 1100506b1a0cSSebastian Grimberg return CEED_ERROR_SUCCESS; 1101506b1a0cSSebastian Grimberg } 1102506b1a0cSSebastian Grimberg 1103506b1a0cSSebastian Grimberg /** 1104480fae85SJeremy L Thompson @brief Create object holding CeedQFunction assembly data for CeedOperator 1105480fae85SJeremy L Thompson 1106480fae85SJeremy L Thompson @param[in] ceed A Ceed object where the CeedQFunctionAssemblyData will be created 1107ea61e9acSJeremy L Thompson @param[out] data Address of the variable where the newly created CeedQFunctionAssemblyData will be stored 1108480fae85SJeremy L Thompson 1109480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1110480fae85SJeremy L Thompson 1111480fae85SJeremy L Thompson @ref Backend 1112480fae85SJeremy L Thompson **/ 1113ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) { 11142b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, data)); 1115480fae85SJeremy L Thompson (*data)->ref_count = 1; 1116480fae85SJeremy L Thompson (*data)->ceed = ceed; 11172b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 1118480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1119480fae85SJeremy L Thompson } 1120480fae85SJeremy L Thompson 1121480fae85SJeremy L Thompson /** 1122480fae85SJeremy L Thompson @brief Increment the reference counter for a CeedQFunctionAssemblyData 1123480fae85SJeremy L Thompson 1124ea61e9acSJeremy L Thompson @param[in,out] data CeedQFunctionAssemblyData to increment the reference counter 1125480fae85SJeremy L Thompson 1126480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1127480fae85SJeremy L Thompson 1128480fae85SJeremy L Thompson @ref Backend 1129480fae85SJeremy L Thompson **/ 1130480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) { 1131480fae85SJeremy L Thompson data->ref_count++; 1132480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1133480fae85SJeremy L Thompson } 1134480fae85SJeremy L Thompson 1135480fae85SJeremy L Thompson /** 1136beecbf24SJeremy L Thompson @brief Set re-use of CeedQFunctionAssemblyData 11378b919e6bSJeremy L Thompson 1138ea61e9acSJeremy L Thompson @param[in,out] data CeedQFunctionAssemblyData to mark for reuse 1139ea61e9acSJeremy L Thompson @param[in] reuse_data Boolean flag indicating data re-use 11408b919e6bSJeremy L Thompson 11418b919e6bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 11428b919e6bSJeremy L Thompson 11438b919e6bSJeremy L Thompson @ref Backend 11448b919e6bSJeremy L Thompson **/ 11452b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) { 1146beecbf24SJeremy L Thompson data->reuse_data = reuse_data; 1147beecbf24SJeremy L Thompson data->needs_data_update = true; 1148beecbf24SJeremy L Thompson return CEED_ERROR_SUCCESS; 1149beecbf24SJeremy L Thompson } 1150beecbf24SJeremy L Thompson 1151beecbf24SJeremy L Thompson /** 1152beecbf24SJeremy L Thompson @brief Mark QFunctionAssemblyData as stale 1153beecbf24SJeremy L Thompson 1154ea61e9acSJeremy L Thompson @param[in,out] data CeedQFunctionAssemblyData to mark as stale 1155ea61e9acSJeremy L Thompson @param[in] needs_data_update Boolean flag indicating if update is needed or completed 1156beecbf24SJeremy L Thompson 1157beecbf24SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1158beecbf24SJeremy L Thompson 1159beecbf24SJeremy L Thompson @ref Backend 1160beecbf24SJeremy L Thompson **/ 11612b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) { 1162beecbf24SJeremy L Thompson data->needs_data_update = needs_data_update; 11638b919e6bSJeremy L Thompson return CEED_ERROR_SUCCESS; 11648b919e6bSJeremy L Thompson } 11658b919e6bSJeremy L Thompson 11668b919e6bSJeremy L Thompson /** 11678b919e6bSJeremy L Thompson @brief Determine if QFunctionAssemblyData needs update 11688b919e6bSJeremy L Thompson 11698b919e6bSJeremy L Thompson @param[in] data CeedQFunctionAssemblyData to mark as stale 11708b919e6bSJeremy L Thompson @param[out] is_update_needed Boolean flag indicating if re-assembly is required 11718b919e6bSJeremy L Thompson 11728b919e6bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 11738b919e6bSJeremy L Thompson 11748b919e6bSJeremy L Thompson @ref Backend 11758b919e6bSJeremy L Thompson **/ 11762b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) { 1177beecbf24SJeremy L Thompson *is_update_needed = !data->reuse_data || data->needs_data_update; 11788b919e6bSJeremy L Thompson return CEED_ERROR_SUCCESS; 11798b919e6bSJeremy L Thompson } 11808b919e6bSJeremy L Thompson 11818b919e6bSJeremy L Thompson /** 1182ea61e9acSJeremy L Thompson @brief Copy the pointer to a CeedQFunctionAssemblyData. 11834385fb7fSSebastian Grimberg 1184ea61e9acSJeremy L Thompson Both pointers should be destroyed with `CeedCeedQFunctionAssemblyDataDestroy()`. 1185512bb800SJeremy L Thompson 1186512bb800SJeremy 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 1187512bb800SJeremy L Thompson CeedQFunctionAssemblyData. This CeedQFunctionAssemblyData will be destroyed if `data_copy` is the only reference to this 1188512bb800SJeremy L Thompson CeedQFunctionAssemblyData. 1189480fae85SJeremy L Thompson 1190ea61e9acSJeremy 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 /** 1205480fae85SJeremy L Thompson @brief Get setup status for internal objects for CeedQFunctionAssemblyData 1206480fae85SJeremy L Thompson 1207ea61e9acSJeremy 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 /** 1220480fae85SJeremy L Thompson @brief Set internal objects for CeedQFunctionAssemblyData 1221480fae85SJeremy L Thompson 1222ea61e9acSJeremy L Thompson @param[in,out] data CeedQFunctionAssemblyData to set objects 1223480fae85SJeremy L Thompson @param[in] vec CeedVector to store assembled CeedQFunction at quadrature points 1224480fae85SJeremy 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 12382b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) { 12396574a04fSJeremy L Thompson CeedCheck(data->is_setup, data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first."); 1240480fae85SJeremy L Thompson 12412b730f8bSJeremy L Thompson CeedCall(CeedVectorReferenceCopy(data->vec, vec)); 12422b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr)); 1243480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1244480fae85SJeremy L Thompson } 1245480fae85SJeremy L Thompson 1246480fae85SJeremy L Thompson /** 1247480fae85SJeremy L Thompson @brief Destroy CeedQFunctionAssemblyData 1248480fae85SJeremy L Thompson 1249ea61e9acSJeremy L Thompson @param[in,out] data CeedQFunctionAssemblyData to destroy 1250480fae85SJeremy L Thompson 1251480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1252480fae85SJeremy L Thompson 1253480fae85SJeremy L Thompson @ref Backend 1254480fae85SJeremy L Thompson **/ 1255480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) { 1256ad6481ceSJeremy L Thompson if (!*data || --(*data)->ref_count > 0) { 1257ad6481ceSJeremy L Thompson *data = NULL; 1258ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1259ad6481ceSJeremy L Thompson } 12602b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*data)->ceed)); 12612b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*data)->vec)); 12622b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr)); 1263480fae85SJeremy L Thompson 12642b730f8bSJeremy L Thompson CeedCall(CeedFree(data)); 1265480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1266480fae85SJeremy L Thompson } 1267480fae85SJeremy L Thompson 1268ed9e99e6SJeremy L Thompson /** 1269ed9e99e6SJeremy L Thompson @brief Get CeedOperatorAssemblyData 1270ed9e99e6SJeremy L Thompson 1271ed9e99e6SJeremy L Thompson @param[in] op CeedOperator to assemble 1272ed9e99e6SJeremy L Thompson @param[out] data CeedQFunctionAssemblyData 1273ed9e99e6SJeremy L Thompson 1274ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1275ed9e99e6SJeremy L Thompson 1276ed9e99e6SJeremy L Thompson @ref Backend 1277ed9e99e6SJeremy L Thompson **/ 12782b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) { 1279ed9e99e6SJeremy L Thompson if (!op->op_assembled) { 1280ed9e99e6SJeremy L Thompson CeedOperatorAssemblyData data; 1281ed9e99e6SJeremy L Thompson 12822b730f8bSJeremy L Thompson CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data)); 1283ed9e99e6SJeremy L Thompson op->op_assembled = data; 1284ed9e99e6SJeremy L Thompson } 1285ed9e99e6SJeremy L Thompson *data = op->op_assembled; 1286ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1287ed9e99e6SJeremy L Thompson } 1288ed9e99e6SJeremy L Thompson 1289ed9e99e6SJeremy L Thompson /** 1290ba746a46SJeremy L Thompson @brief Create object holding CeedOperator assembly data. 1291ba746a46SJeremy L Thompson 1292ba746a46SJeremy L Thompson The CeedOperatorAssemblyData holds an array with references to every active CeedBasis used in the CeedOperator. 1293ba746a46SJeremy L Thompson An array with references to the corresponding active CeedElemRestrictions is also stored. 1294ba746a46SJeremy L Thompson For each active CeedBasis, the CeedOperatorAssemblyData holds an array of all input and output CeedEvalModes for this CeedBasis. 1295ba746a46SJeremy L Thompson The CeedOperatorAssemblyData holds an array of offsets for indexing into the assembled CeedQFunction arrays to the row representing each 1296ba746a46SJeremy L Thompson CeedEvalMode. 1297ba746a46SJeremy L Thompson The number of input columns across all active bases for the assembled CeedQFunction is also stored. 1298ba746a46SJeremy L Thompson Lastly, the CeedOperatorAssembly data holds assembled matrices representing the full action of the CeedBasis for all CeedEvalModes. 1299ed9e99e6SJeremy L Thompson 1300ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedOperatorAssemblyData will be created 1301ed9e99e6SJeremy L Thompson @param[in] op CeedOperator to be assembled 1302ea61e9acSJeremy L Thompson @param[out] data Address of the variable where the newly created CeedOperatorAssemblyData will be stored 1303ed9e99e6SJeremy L Thompson 1304ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1305ed9e99e6SJeremy L Thompson 1306ed9e99e6SJeremy L Thompson @ref Backend 1307ed9e99e6SJeremy L Thompson **/ 13082b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) { 1309506b1a0cSSebastian Grimberg CeedInt num_active_bases_in = 0, num_active_bases_out = 0, offset = 0; 1310506b1a0cSSebastian Grimberg CeedInt num_input_fields, *num_eval_modes_in = NULL, num_output_fields, *num_eval_modes_out = NULL; 13111c66c397SJeremy L Thompson CeedSize **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL; 13121c66c397SJeremy L Thompson CeedEvalMode **eval_modes_in = NULL, **eval_modes_out = NULL; 13131c66c397SJeremy L Thompson CeedQFunctionField *qf_fields; 13141c66c397SJeremy L Thompson CeedQFunction qf; 13151c66c397SJeremy L Thompson CeedOperatorField *op_fields; 131601f0e615SJames Wright bool is_composite; 131701f0e615SJames Wright 131801f0e615SJames Wright CeedCall(CeedOperatorIsComposite(op, &is_composite)); 131901f0e615SJames Wright CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Can only create CeedOperator assembly data for non-composite operators."); 1320437c7c90SJeremy L Thompson 1321437c7c90SJeremy L Thompson // Allocate 13222b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, data)); 1323ed9e99e6SJeremy L Thompson (*data)->ceed = ceed; 13242b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 1325ed9e99e6SJeremy L Thompson 1326ed9e99e6SJeremy L Thompson // Build OperatorAssembly data 13272b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetQFunction(op, &qf)); 13282b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL)); 13292b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 1330ed9e99e6SJeremy L Thompson 1331ed9e99e6SJeremy L Thompson // Determine active input basis 1332ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1333ed9e99e6SJeremy L Thompson CeedVector vec; 13341c66c397SJeremy L Thompson 13352b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec)); 1336ed9e99e6SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 13377c1dbaffSSebastian Grimberg CeedInt index = -1, num_comp, q_comp; 13381c66c397SJeremy L Thompson CeedEvalMode eval_mode; 13391c66c397SJeremy L Thompson CeedBasis basis_in = NULL; 13401c66c397SJeremy L Thompson 13412b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in)); 13422b730f8bSJeremy L Thompson CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1343352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp)); 1344352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 1345506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_active_bases_in; i++) { 1346506b1a0cSSebastian Grimberg if ((*data)->active_bases_in[i] == basis_in) index = i; 1347437c7c90SJeremy L Thompson } 1348437c7c90SJeremy L Thompson if (index == -1) { 1349437c7c90SJeremy L Thompson CeedElemRestriction elem_rstr_in; 13501c66c397SJeremy L Thompson 1351506b1a0cSSebastian Grimberg index = num_active_bases_in; 1352506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_bases_in)); 1353506b1a0cSSebastian Grimberg (*data)->active_bases_in[num_active_bases_in] = NULL; 1354506b1a0cSSebastian Grimberg CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases_in[num_active_bases_in])); 1355506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_elem_rstrs_in)); 1356506b1a0cSSebastian Grimberg (*data)->active_elem_rstrs_in[num_active_bases_in] = NULL; 1357437c7c90SJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in)); 1358506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs_in[num_active_bases_in])); 1359506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &num_eval_modes_in)); 1360437c7c90SJeremy L Thompson num_eval_modes_in[index] = 0; 1361506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_modes_in)); 1362437c7c90SJeremy L Thompson eval_modes_in[index] = NULL; 1363506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_mode_offsets_in)); 1364437c7c90SJeremy L Thompson eval_mode_offsets_in[index] = NULL; 1365506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->assembled_bases_in)); 1366437c7c90SJeremy L Thompson (*data)->assembled_bases_in[index] = NULL; 1367506b1a0cSSebastian Grimberg num_active_bases_in++; 1368437c7c90SJeremy L Thompson } 1369352a5e7cSSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1370352a5e7cSSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 1371352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_modes_in[index])); 1372352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_mode_offsets_in[index])); 1373352a5e7cSSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 1374437c7c90SJeremy L Thompson eval_modes_in[index][num_eval_modes_in[index] + d] = eval_mode; 1375437c7c90SJeremy L Thompson eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset; 1376352a5e7cSSebastian Grimberg offset += num_comp; 1377ed9e99e6SJeremy L Thompson } 1378352a5e7cSSebastian Grimberg num_eval_modes_in[index] += q_comp; 1379ed9e99e6SJeremy L Thompson } 1380ed9e99e6SJeremy L Thompson } 1381ed9e99e6SJeremy L Thompson } 1382ed9e99e6SJeremy L Thompson 1383ed9e99e6SJeremy L Thompson // Determine active output basis 13842b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields)); 13852b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 1386437c7c90SJeremy L Thompson offset = 0; 1387ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1388ed9e99e6SJeremy L Thompson CeedVector vec; 13891c66c397SJeremy L Thompson 13902b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec)); 1391ed9e99e6SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 13927c1dbaffSSebastian Grimberg CeedInt index = -1, num_comp, q_comp; 13931c66c397SJeremy L Thompson CeedEvalMode eval_mode; 13941c66c397SJeremy L Thompson CeedBasis basis_out = NULL; 13951c66c397SJeremy L Thompson 1396437c7c90SJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out)); 13972b730f8bSJeremy L Thompson CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1398352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumComponents(basis_out, &num_comp)); 1399352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 1400506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_active_bases_out; i++) { 1401506b1a0cSSebastian Grimberg if ((*data)->active_bases_out[i] == basis_out) index = i; 1402437c7c90SJeremy L Thompson } 1403437c7c90SJeremy L Thompson if (index == -1) { 1404437c7c90SJeremy L Thompson CeedElemRestriction elem_rstr_out; 14051c66c397SJeremy L Thompson 1406506b1a0cSSebastian Grimberg index = num_active_bases_out; 1407506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_bases_out)); 1408506b1a0cSSebastian Grimberg (*data)->active_bases_out[num_active_bases_out] = NULL; 1409506b1a0cSSebastian Grimberg CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases_out[num_active_bases_out])); 1410506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_elem_rstrs_out)); 1411506b1a0cSSebastian Grimberg (*data)->active_elem_rstrs_out[num_active_bases_out] = NULL; 1412437c7c90SJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out)); 1413506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs_out[num_active_bases_out])); 1414506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &num_eval_modes_out)); 1415437c7c90SJeremy L Thompson num_eval_modes_out[index] = 0; 1416506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_modes_out)); 1417437c7c90SJeremy L Thompson eval_modes_out[index] = NULL; 1418506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_mode_offsets_out)); 1419437c7c90SJeremy L Thompson eval_mode_offsets_out[index] = NULL; 1420506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->assembled_bases_out)); 1421437c7c90SJeremy L Thompson (*data)->assembled_bases_out[index] = NULL; 1422506b1a0cSSebastian Grimberg num_active_bases_out++; 1423437c7c90SJeremy L Thompson } 1424352a5e7cSSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1425352a5e7cSSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 1426352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_modes_out[index])); 1427352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_mode_offsets_out[index])); 1428352a5e7cSSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 1429437c7c90SJeremy L Thompson eval_modes_out[index][num_eval_modes_out[index] + d] = eval_mode; 1430437c7c90SJeremy L Thompson eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset; 1431352a5e7cSSebastian Grimberg offset += num_comp; 1432ed9e99e6SJeremy L Thompson } 1433352a5e7cSSebastian Grimberg num_eval_modes_out[index] += q_comp; 1434ed9e99e6SJeremy L Thompson } 1435ed9e99e6SJeremy L Thompson } 1436ed9e99e6SJeremy L Thompson } 1437506b1a0cSSebastian Grimberg (*data)->num_active_bases_in = num_active_bases_in; 143827789c4aSJed Brown (*data)->num_eval_modes_in = num_eval_modes_in; 143927789c4aSJed Brown (*data)->eval_modes_in = eval_modes_in; 144027789c4aSJed Brown (*data)->eval_mode_offsets_in = eval_mode_offsets_in; 1441506b1a0cSSebastian Grimberg (*data)->num_active_bases_out = num_active_bases_out; 1442437c7c90SJeremy L Thompson (*data)->num_eval_modes_out = num_eval_modes_out; 1443437c7c90SJeremy L Thompson (*data)->eval_modes_out = eval_modes_out; 1444437c7c90SJeremy L Thompson (*data)->eval_mode_offsets_out = eval_mode_offsets_out; 1445506b1a0cSSebastian Grimberg (*data)->num_output_components = offset; 1446ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1447ed9e99e6SJeremy L Thompson } 1448ed9e99e6SJeremy L Thompson 1449ed9e99e6SJeremy L Thompson /** 1450ba746a46SJeremy L Thompson @brief Get CeedOperator CeedEvalModes for assembly. 1451ba746a46SJeremy L Thompson 1452ba746a46SJeremy L Thompson Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object. 1453ed9e99e6SJeremy L Thompson 1454ed9e99e6SJeremy L Thompson @param[in] data CeedOperatorAssemblyData 1455506b1a0cSSebastian Grimberg @param[out] num_active_bases_in Total number of active bases for input 1456c5d0f995SJed Brown @param[out] num_eval_modes_in Pointer to hold array of numbers of input CeedEvalModes, or NULL. 1457ba746a46SJeremy L Thompson `eval_modes_in[0]` holds an array of eval modes for the first active basis. 1458c5d0f995SJed Brown @param[out] eval_modes_in Pointer to hold arrays of input CeedEvalModes, or NULL. 1459ba746a46SJeremy L Thompson @param[out] eval_mode_offsets_in Pointer to hold arrays of input offsets at each quadrature point. 1460506b1a0cSSebastian Grimberg @param[out] num_active_bases_out Total number of active bases for output 1461c5d0f995SJed Brown @param[out] num_eval_modes_out Pointer to hold array of numbers of output CeedEvalModes, or NULL 1462c5d0f995SJed Brown @param[out] eval_modes_out Pointer to hold arrays of output CeedEvalModes, or NULL. 1463437c7c90SJeremy L Thompson @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point 1464ba746a46SJeremy L Thompson @param[out] num_output_components The number of columns in the assembled CeedQFunction matrix for each quadrature point, 1465ba746a46SJeremy L Thompson including contributions of all active bases 1466ed9e99e6SJeremy L Thompson 1467ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1468ed9e99e6SJeremy L Thompson 1469ed9e99e6SJeremy L Thompson @ref Backend 1470ed9e99e6SJeremy L Thompson **/ 1471506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedInt **num_eval_modes_in, 1472506b1a0cSSebastian Grimberg const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt *num_active_bases_out, 1473506b1a0cSSebastian Grimberg CeedInt **num_eval_modes_out, const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out, 1474506b1a0cSSebastian Grimberg CeedSize *num_output_components) { 1475506b1a0cSSebastian Grimberg if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in; 1476437c7c90SJeremy L Thompson if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in; 1477437c7c90SJeremy L Thompson if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in; 1478437c7c90SJeremy L Thompson if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in; 1479506b1a0cSSebastian Grimberg if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out; 1480437c7c90SJeremy L Thompson if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out; 1481437c7c90SJeremy L Thompson if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out; 1482437c7c90SJeremy L Thompson if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out; 1483437c7c90SJeremy L Thompson if (num_output_components) *num_output_components = data->num_output_components; 1484ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1485ed9e99e6SJeremy L Thompson } 1486ed9e99e6SJeremy L Thompson 1487ed9e99e6SJeremy L Thompson /** 1488ba746a46SJeremy L Thompson @brief Get CeedOperator CeedBasis data for assembly. 1489ba746a46SJeremy L Thompson 1490ba746a46SJeremy L Thompson Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object. 1491ed9e99e6SJeremy L Thompson 1492ed9e99e6SJeremy L Thompson @param[in] data CeedOperatorAssemblyData 1493506b1a0cSSebastian Grimberg @param[out] num_active_bases_in Number of active input bases, or NULL 1494506b1a0cSSebastian Grimberg @param[out] active_bases_in Pointer to hold active input CeedBasis, or NULL 1495437c7c90SJeremy L Thompson @param[out] assembled_bases_in Pointer to hold assembled active input B, or NULL 1496506b1a0cSSebastian Grimberg @param[out] num_active_bases_out Number of active output bases, or NULL 1497506b1a0cSSebastian Grimberg @param[out] active_bases_out Pointer to hold active output CeedBasis, or NULL 1498437c7c90SJeremy L Thompson @param[out] assembled_bases_out Pointer to hold assembled active output B, or NULL 1499ed9e99e6SJeremy L Thompson 1500ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1501ed9e99e6SJeremy L Thompson 1502ed9e99e6SJeremy L Thompson @ref Backend 1503ed9e99e6SJeremy L Thompson **/ 1504506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedBasis **active_bases_in, 1505506b1a0cSSebastian Grimberg const CeedScalar ***assembled_bases_in, CeedInt *num_active_bases_out, CeedBasis **active_bases_out, 1506506b1a0cSSebastian Grimberg const CeedScalar ***assembled_bases_out) { 1507ed9e99e6SJeremy L Thompson // Assemble B_in, B_out if needed 1508437c7c90SJeremy L Thompson if (assembled_bases_in && !data->assembled_bases_in[0]) { 1509437c7c90SJeremy L Thompson CeedInt num_qpts; 1510437c7c90SJeremy L Thompson 1511506b1a0cSSebastian Grimberg if (data->active_bases_in[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[0], &num_qpts)); 1512506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_in[0], &num_qpts)); 1513506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < data->num_active_bases_in; b++) { 15141c66c397SJeremy L Thompson bool has_eval_none = false; 1515352a5e7cSSebastian Grimberg CeedInt num_nodes; 1516437c7c90SJeremy L Thompson CeedScalar *B_in = NULL, *identity = NULL; 1517ed9e99e6SJeremy L Thompson 1518506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[b], &num_nodes)); 1519352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_in[b], &B_in)); 1520ed9e99e6SJeremy L Thompson 1521437c7c90SJeremy L Thompson for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) { 1522437c7c90SJeremy L Thompson has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE); 1523ed9e99e6SJeremy L Thompson } 1524ed9e99e6SJeremy L Thompson if (has_eval_none) { 1525352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes, &identity)); 1526352a5e7cSSebastian Grimberg for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) { 1527352a5e7cSSebastian Grimberg identity[i * num_nodes + i] = 1.0; 1528ed9e99e6SJeremy L Thompson } 1529ed9e99e6SJeremy L Thompson } 1530ed9e99e6SJeremy L Thompson 1531ed9e99e6SJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 1532352a5e7cSSebastian Grimberg for (CeedInt n = 0; n < num_nodes; n++) { 1533352a5e7cSSebastian Grimberg CeedInt d_in = 0, q_comp_in; 1534352a5e7cSSebastian Grimberg CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE; 15351c66c397SJeremy L Thompson 1536437c7c90SJeremy L Thompson for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) { 1537437c7c90SJeremy L Thompson const CeedInt qq = data->num_eval_modes_in[b] * q; 1538437c7c90SJeremy L Thompson const CeedScalar *B = NULL; 15391c66c397SJeremy L Thompson 1540506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(data->active_bases_in[b], data->eval_modes_in[b][e_in], identity, &B)); 1541506b1a0cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_in[b], data->eval_modes_in[b][e_in], &q_comp_in)); 1542352a5e7cSSebastian Grimberg if (q_comp_in > 1) { 1543352a5e7cSSebastian Grimberg if (e_in == 0 || data->eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0; 1544352a5e7cSSebastian Grimberg else B = &B[(++d_in) * num_qpts * num_nodes]; 1545352a5e7cSSebastian Grimberg } 1546352a5e7cSSebastian Grimberg eval_mode_in_prev = data->eval_modes_in[b][e_in]; 1547352a5e7cSSebastian Grimberg B_in[(qq + e_in) * num_nodes + n] = B[q * num_nodes + n]; 1548ed9e99e6SJeremy L Thompson } 1549ed9e99e6SJeremy L Thompson } 1550ed9e99e6SJeremy L Thompson } 15517c1dbaffSSebastian Grimberg if (identity) CeedCall(CeedFree(&identity)); 1552437c7c90SJeremy L Thompson data->assembled_bases_in[b] = B_in; 1553437c7c90SJeremy L Thompson } 1554ed9e99e6SJeremy L Thompson } 1555ed9e99e6SJeremy L Thompson 1556437c7c90SJeremy L Thompson if (assembled_bases_out && !data->assembled_bases_out[0]) { 1557437c7c90SJeremy L Thompson CeedInt num_qpts; 1558437c7c90SJeremy L Thompson 1559506b1a0cSSebastian Grimberg if (data->active_bases_out[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[0], &num_qpts)); 1560506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_out[0], &num_qpts)); 1561506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < data->num_active_bases_out; b++) { 1562ed9e99e6SJeremy L Thompson bool has_eval_none = false; 15631c66c397SJeremy L Thompson CeedInt num_nodes; 1564437c7c90SJeremy L Thompson CeedScalar *B_out = NULL, *identity = NULL; 1565ed9e99e6SJeremy L Thompson 1566506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[b], &num_nodes)); 1567352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_out[b], &B_out)); 1568ed9e99e6SJeremy L Thompson 1569437c7c90SJeremy L Thompson for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) { 1570437c7c90SJeremy L Thompson has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE); 1571ed9e99e6SJeremy L Thompson } 1572ed9e99e6SJeremy L Thompson if (has_eval_none) { 1573352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes, &identity)); 1574352a5e7cSSebastian Grimberg for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) { 1575352a5e7cSSebastian Grimberg identity[i * num_nodes + i] = 1.0; 1576ed9e99e6SJeremy L Thompson } 1577ed9e99e6SJeremy L Thompson } 1578ed9e99e6SJeremy L Thompson 1579ed9e99e6SJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 1580352a5e7cSSebastian Grimberg for (CeedInt n = 0; n < num_nodes; n++) { 1581352a5e7cSSebastian Grimberg CeedInt d_out = 0, q_comp_out; 1582352a5e7cSSebastian Grimberg CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE; 15831c66c397SJeremy L Thompson 1584437c7c90SJeremy L Thompson for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) { 1585437c7c90SJeremy L Thompson const CeedInt qq = data->num_eval_modes_out[b] * q; 1586437c7c90SJeremy L Thompson const CeedScalar *B = NULL; 15871c66c397SJeremy L Thompson 1588506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(data->active_bases_out[b], data->eval_modes_out[b][e_out], identity, &B)); 1589506b1a0cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_out[b], data->eval_modes_out[b][e_out], &q_comp_out)); 1590352a5e7cSSebastian Grimberg if (q_comp_out > 1) { 1591352a5e7cSSebastian Grimberg if (e_out == 0 || data->eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0; 1592352a5e7cSSebastian Grimberg else B = &B[(++d_out) * num_qpts * num_nodes]; 1593352a5e7cSSebastian Grimberg } 1594352a5e7cSSebastian Grimberg eval_mode_out_prev = data->eval_modes_out[b][e_out]; 1595352a5e7cSSebastian Grimberg B_out[(qq + e_out) * num_nodes + n] = B[q * num_nodes + n]; 1596ed9e99e6SJeremy L Thompson } 1597ed9e99e6SJeremy L Thompson } 1598ed9e99e6SJeremy L Thompson } 15997c1dbaffSSebastian Grimberg if (identity) CeedCall(CeedFree(&identity)); 1600437c7c90SJeremy L Thompson data->assembled_bases_out[b] = B_out; 1601437c7c90SJeremy L Thompson } 1602ed9e99e6SJeremy L Thompson } 1603ed9e99e6SJeremy L Thompson 1604437c7c90SJeremy L Thompson // Pass out assembled data 1605506b1a0cSSebastian Grimberg if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in; 1606506b1a0cSSebastian Grimberg if (active_bases_in) *active_bases_in = data->active_bases_in; 1607437c7c90SJeremy L Thompson if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in; 1608506b1a0cSSebastian Grimberg if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out; 1609506b1a0cSSebastian Grimberg if (active_bases_out) *active_bases_out = data->active_bases_out; 1610437c7c90SJeremy L Thompson if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out; 1611437c7c90SJeremy L Thompson return CEED_ERROR_SUCCESS; 1612437c7c90SJeremy L Thompson } 1613437c7c90SJeremy L Thompson 1614437c7c90SJeremy L Thompson /** 1615ba746a46SJeremy L Thompson @brief Get CeedOperator CeedBasis data for assembly. 1616ba746a46SJeremy L Thompson 1617ba746a46SJeremy L Thompson Note: See CeedOperatorAssemblyDataCreate for a full description of the data stored in this object. 1618437c7c90SJeremy L Thompson 1619437c7c90SJeremy L Thompson @param[in] data CeedOperatorAssemblyData 1620506b1a0cSSebastian Grimberg @param[out] num_active_elem_rstrs_in Number of active input element restrictions, or NULL 1621506b1a0cSSebastian Grimberg @param[out] active_elem_rstrs_in Pointer to hold active input CeedElemRestrictions, or NULL 1622506b1a0cSSebastian Grimberg @param[out] num_active_elem_rstrs_out Number of active output element restrictions, or NULL 1623506b1a0cSSebastian Grimberg @param[out] active_elem_rstrs_out Pointer to hold active output CeedElemRestrictions, or NULL 1624437c7c90SJeremy L Thompson 1625437c7c90SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1626437c7c90SJeremy L Thompson 1627437c7c90SJeremy L Thompson @ref Backend 1628437c7c90SJeremy L Thompson **/ 1629506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs_in, 1630506b1a0cSSebastian Grimberg CeedElemRestriction **active_elem_rstrs_in, CeedInt *num_active_elem_rstrs_out, 1631506b1a0cSSebastian Grimberg CeedElemRestriction **active_elem_rstrs_out) { 1632506b1a0cSSebastian Grimberg if (num_active_elem_rstrs_in) *num_active_elem_rstrs_in = data->num_active_bases_in; 1633506b1a0cSSebastian Grimberg if (active_elem_rstrs_in) *active_elem_rstrs_in = data->active_elem_rstrs_in; 1634506b1a0cSSebastian Grimberg if (num_active_elem_rstrs_out) *num_active_elem_rstrs_out = data->num_active_bases_out; 1635506b1a0cSSebastian Grimberg if (active_elem_rstrs_out) *active_elem_rstrs_out = data->active_elem_rstrs_out; 1636ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1637ed9e99e6SJeremy L Thompson } 1638ed9e99e6SJeremy L Thompson 1639ed9e99e6SJeremy L Thompson /** 1640ed9e99e6SJeremy L Thompson @brief Destroy CeedOperatorAssemblyData 1641ed9e99e6SJeremy L Thompson 1642ea61e9acSJeremy L Thompson @param[in,out] data CeedOperatorAssemblyData to destroy 1643ed9e99e6SJeremy L Thompson 1644ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1645ed9e99e6SJeremy L Thompson 1646ed9e99e6SJeremy L Thompson @ref Backend 1647ed9e99e6SJeremy L Thompson **/ 1648ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) { 1649ad6481ceSJeremy L Thompson if (!*data) { 1650ad6481ceSJeremy L Thompson *data = NULL; 1651ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1652ad6481ceSJeremy L Thompson } 16532b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*data)->ceed)); 1654506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < (*data)->num_active_bases_in; b++) { 1655506b1a0cSSebastian Grimberg CeedCall(CeedBasisDestroy(&(*data)->active_bases_in[b])); 1656506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_in[b])); 1657437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_modes_in[b])); 1658437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b])); 1659437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_in[b])); 1660506b1a0cSSebastian Grimberg } 1661506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < (*data)->num_active_bases_out; b++) { 1662506b1a0cSSebastian Grimberg CeedCall(CeedBasisDestroy(&(*data)->active_bases_out[b])); 1663506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_out[b])); 1664506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->eval_modes_out[b])); 1665506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b])); 1666437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_out[b])); 1667437c7c90SJeremy L Thompson } 1668506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_bases_in)); 1669506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_bases_out)); 1670506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_elem_rstrs_in)); 1671506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_elem_rstrs_out)); 1672437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->num_eval_modes_in)); 1673437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->num_eval_modes_out)); 1674437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_modes_in)); 1675437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_modes_out)); 1676437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_mode_offsets_in)); 1677437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_mode_offsets_out)); 1678437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_in)); 1679437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_out)); 1680ed9e99e6SJeremy L Thompson 16812b730f8bSJeremy L Thompson CeedCall(CeedFree(data)); 1682ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1683ed9e99e6SJeremy L Thompson } 1684ed9e99e6SJeremy L Thompson 1685480fae85SJeremy L Thompson /// @} 1686480fae85SJeremy L Thompson 1687480fae85SJeremy L Thompson /// ---------------------------------------------------------------------------- 1688eaf62fffSJeremy L Thompson /// CeedOperator Public API 1689eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 1690eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser 1691eaf62fffSJeremy L Thompson /// @{ 1692eaf62fffSJeremy L Thompson 1693eaf62fffSJeremy L Thompson /** 1694eaf62fffSJeremy L Thompson @brief Assemble a linear CeedQFunction associated with a CeedOperator 1695eaf62fffSJeremy L Thompson 1696ea61e9acSJeremy L Thompson This returns a CeedVector containing a matrix at each quadrature point providing the action of the CeedQFunction associated with the CeedOperator. 1697859c15bbSJames Wright The vector `assembled` is of shape `[num_elements, num_input_fields, num_output_fields, num_quad_points]` and contains column-major matrices 1698859c15bbSJames Wright representing the action of the CeedQFunction for a corresponding quadrature point on an element. 1699859c15bbSJames Wright 17009fd66db6SSebastian Grimberg Inputs and outputs are in the order provided by the user when adding CeedOperator fields. 17019fd66db6SSebastian Grimberg For example, a CeedQFunction with inputs 'u' and 'gradu' and outputs 'gradv' and 'v', provided in that order, would result in an assembled QFunction 17029fd66db6SSebastian Grimberg that consists of (1 + dim) x (dim + 1) matrices at each quadrature point acting on the input [u, du_0, du_1] and producing the output [dv_0, dv_1, v]. 1703eaf62fffSJeremy L Thompson 1704ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 1705f04ea552SJeremy L Thompson 1706ea61e9acSJeremy L Thompson @param[in] op CeedOperator to assemble CeedQFunction 1707ea61e9acSJeremy L Thompson @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points 1708ea61e9acSJeremy L Thompson @param[out] rstr CeedElemRestriction for CeedVector containing assembled CeedQFunction 1709ea61e9acSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1710eaf62fffSJeremy L Thompson 1711eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1712eaf62fffSJeremy L Thompson 1713eaf62fffSJeremy L Thompson @ref User 1714eaf62fffSJeremy L Thompson **/ 17152b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 17162b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 1717eaf62fffSJeremy L Thompson 1718eaf62fffSJeremy L Thompson if (op->LinearAssembleQFunction) { 1719d04bbc78SJeremy L Thompson // Backend version 17202b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request)); 1721eaf62fffSJeremy L Thompson } else { 1722d04bbc78SJeremy L Thompson // Operator fallback 1723d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1724d04bbc78SJeremy L Thompson 17252b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 17266574a04fSJeremy L Thompson if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request)); 17276574a04fSJeremy L Thompson else return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction"); 172870a7ffb3SJeremy L Thompson } 1729eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1730eaf62fffSJeremy L Thompson } 173170a7ffb3SJeremy L Thompson 173270a7ffb3SJeremy L Thompson /** 1733ea61e9acSJeremy L Thompson @brief Assemble CeedQFunction and store result internally. 17344385fb7fSSebastian Grimberg 1735ea61e9acSJeremy L Thompson Return copied references of stored data to the caller. 1736ea61e9acSJeremy L Thompson Caller is responsible for ownership and destruction of the copied references. 1737ea61e9acSJeremy L Thompson See also @ref CeedOperatorLinearAssembleQFunction 173870a7ffb3SJeremy L Thompson 1739c5f45aeaSJeremy 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. 1740c5f45aeaSJeremy L Thompson These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object. 1741c5f45aeaSJeremy L Thompson 1742ea61e9acSJeremy L Thompson @param[in] op CeedOperator to assemble CeedQFunction 1743ea61e9acSJeremy L Thompson @param[out] assembled CeedVector to store assembled CeedQFunction at quadrature points 1744ea61e9acSJeremy L Thompson @param[out] rstr CeedElemRestriction for CeedVector containing assembledCeedQFunction 1745ea61e9acSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 174670a7ffb3SJeremy L Thompson 174770a7ffb3SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 174870a7ffb3SJeremy L Thompson 174970a7ffb3SJeremy L Thompson @ref User 175070a7ffb3SJeremy L Thompson **/ 17512b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 1752b05f7e9fSJeremy L Thompson int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *) = NULL; 1753b05f7e9fSJeremy L Thompson CeedOperator op_assemble = NULL; 1754bb229da9SJeremy L Thompson CeedOperator op_fallback_parent = NULL; 1755b05f7e9fSJeremy L Thompson 17562b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 175770a7ffb3SJeremy L Thompson 1758b05f7e9fSJeremy L Thompson // Determine if fallback parent or operator has implementation 1759bb229da9SJeremy L Thompson CeedCall(CeedOperatorGetFallbackParent(op, &op_fallback_parent)); 1760bb229da9SJeremy L Thompson if (op_fallback_parent && op_fallback_parent->LinearAssembleQFunctionUpdate) { 1761b05f7e9fSJeremy L Thompson // -- Backend version for op fallback parent is faster, if it exists 1762bb229da9SJeremy L Thompson LinearAssembleQFunctionUpdate = op_fallback_parent->LinearAssembleQFunctionUpdate; 1763bb229da9SJeremy L Thompson op_assemble = op_fallback_parent; 1764b05f7e9fSJeremy L Thompson } else if (op->LinearAssembleQFunctionUpdate) { 1765b05f7e9fSJeremy L Thompson // -- Backend version for op 1766b05f7e9fSJeremy L Thompson LinearAssembleQFunctionUpdate = op->LinearAssembleQFunctionUpdate; 1767b05f7e9fSJeremy L Thompson op_assemble = op; 1768b05f7e9fSJeremy L Thompson } 1769b05f7e9fSJeremy L Thompson 1770b05f7e9fSJeremy L Thompson // Assemble QFunction 1771b05f7e9fSJeremy L Thompson if (LinearAssembleQFunctionUpdate) { 1772b05f7e9fSJeremy L Thompson // Backend or fallback parent version 1773480fae85SJeremy L Thompson bool qf_assembled_is_setup; 17742efa2d85SJeremy L Thompson CeedVector assembled_vec = NULL; 17752efa2d85SJeremy L Thompson CeedElemRestriction assembled_rstr = NULL; 1776480fae85SJeremy L Thompson 17772b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataIsSetup(op->qf_assembled, &qf_assembled_is_setup)); 1778480fae85SJeremy L Thompson if (qf_assembled_is_setup) { 1779d04bbc78SJeremy L Thompson bool update_needed; 1780d04bbc78SJeremy L Thompson 17812b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataGetObjects(op->qf_assembled, &assembled_vec, &assembled_rstr)); 17822b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(op->qf_assembled, &update_needed)); 1783b05f7e9fSJeremy L Thompson if (update_needed) CeedCall(LinearAssembleQFunctionUpdate(op_assemble, assembled_vec, assembled_rstr, request)); 178470a7ffb3SJeremy L Thompson } else { 1785b05f7e9fSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleQFunction(op_assemble, &assembled_vec, &assembled_rstr, request)); 17862b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataSetObjects(op->qf_assembled, assembled_vec, assembled_rstr)); 178770a7ffb3SJeremy L Thompson } 17882b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, false)); 17892efa2d85SJeremy L Thompson 1790d04bbc78SJeremy L Thompson // Copy reference from internally held copy 17912b730f8bSJeremy L Thompson CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled)); 17922b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr)); 1793c5f45aeaSJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled_vec)); 17942b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&assembled_rstr)); 179570a7ffb3SJeremy L Thompson } else { 1796d04bbc78SJeremy L Thompson // Operator fallback 1797d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1798d04bbc78SJeremy L Thompson 17992b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 18006574a04fSJeremy L Thompson if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request)); 18016574a04fSJeremy L Thompson else return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate"); 180270a7ffb3SJeremy L Thompson } 180370a7ffb3SJeremy L Thompson return CEED_ERROR_SUCCESS; 1804eaf62fffSJeremy L Thompson } 1805eaf62fffSJeremy L Thompson 1806eaf62fffSJeremy L Thompson /** 1807eaf62fffSJeremy L Thompson @brief Assemble the diagonal of a square linear CeedOperator 1808eaf62fffSJeremy L Thompson 1809eaf62fffSJeremy L Thompson This overwrites a CeedVector with the diagonal of a linear CeedOperator. 1810eaf62fffSJeremy L Thompson 1811ea61e9acSJeremy L Thompson Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported. 1812eaf62fffSJeremy L Thompson 1813ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 1814f04ea552SJeremy L Thompson 1815ea61e9acSJeremy L Thompson @param[in] op CeedOperator to assemble CeedQFunction 1816eaf62fffSJeremy L Thompson @param[out] assembled CeedVector to store assembled CeedOperator diagonal 1817ea61e9acSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1818eaf62fffSJeremy L Thompson 1819eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1820eaf62fffSJeremy L Thompson 1821eaf62fffSJeremy L Thompson @ref User 1822eaf62fffSJeremy L Thompson **/ 18232b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 1824f3d47e36SJeremy L Thompson bool is_composite; 18251c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 18261c66c397SJeremy L Thompson 18272b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 1828f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1829eaf62fffSJeremy L Thompson 18302b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 18316574a04fSJeremy L Thompson CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 1832c9366a6bSJeremy L Thompson 1833f3d47e36SJeremy L Thompson // Early exit for empty operator 1834f3d47e36SJeremy L Thompson if (!is_composite) { 1835f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 1836f3d47e36SJeremy L Thompson 1837f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 1838f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 1839f3d47e36SJeremy L Thompson } 1840f3d47e36SJeremy L Thompson 1841eaf62fffSJeremy L Thompson if (op->LinearAssembleDiagonal) { 1842d04bbc78SJeremy L Thompson // Backend version 18432b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleDiagonal(op, assembled, request)); 1844eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1845eaf62fffSJeremy L Thompson } else if (op->LinearAssembleAddDiagonal) { 1846d04bbc78SJeremy L Thompson // Backend version with zeroing first 18472b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 18482b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request)); 1849eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1850eaf62fffSJeremy L Thompson } else { 1851d04bbc78SJeremy L Thompson // Operator fallback 1852d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1853d04bbc78SJeremy L Thompson 18542b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 1855d04bbc78SJeremy L Thompson if (op_fallback) { 18562b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request)); 1857eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1858eaf62fffSJeremy L Thompson } 1859eaf62fffSJeremy L Thompson } 1860eaf62fffSJeremy L Thompson // Default interface implementation 18612b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 18622b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request)); 1863eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1864eaf62fffSJeremy L Thompson } 1865eaf62fffSJeremy L Thompson 1866eaf62fffSJeremy L Thompson /** 1867eaf62fffSJeremy L Thompson @brief Assemble the diagonal of a square linear CeedOperator 1868eaf62fffSJeremy L Thompson 1869eaf62fffSJeremy L Thompson This sums into a CeedVector the diagonal of a linear CeedOperator. 1870eaf62fffSJeremy L Thompson 1871ea61e9acSJeremy L Thompson Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported. 1872eaf62fffSJeremy L Thompson 1873ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 1874f04ea552SJeremy L Thompson 1875ea61e9acSJeremy L Thompson @param[in] op CeedOperator to assemble CeedQFunction 1876eaf62fffSJeremy L Thompson @param[out] assembled CeedVector to store assembled CeedOperator diagonal 1877ea61e9acSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1878eaf62fffSJeremy L Thompson 1879eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1880eaf62fffSJeremy L Thompson 1881eaf62fffSJeremy L Thompson @ref User 1882eaf62fffSJeremy L Thompson **/ 18832b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 1884f3d47e36SJeremy L Thompson bool is_composite; 18851c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 18861c66c397SJeremy L Thompson 18872b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 1888f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1889eaf62fffSJeremy L Thompson 18902b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 18916574a04fSJeremy L Thompson CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 1892c9366a6bSJeremy L Thompson 1893f3d47e36SJeremy L Thompson // Early exit for empty operator 1894f3d47e36SJeremy L Thompson if (!is_composite) { 1895f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 1896f3d47e36SJeremy L Thompson 1897f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 1898f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 1899f3d47e36SJeremy L Thompson } 1900f3d47e36SJeremy L Thompson 1901eaf62fffSJeremy L Thompson if (op->LinearAssembleAddDiagonal) { 1902d04bbc78SJeremy L Thompson // Backend version 19032b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request)); 1904eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1905eaf62fffSJeremy L Thompson } else { 1906d04bbc78SJeremy L Thompson // Operator fallback 1907d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1908d04bbc78SJeremy L Thompson 19092b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 1910d04bbc78SJeremy L Thompson if (op_fallback) { 19112b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request)); 1912eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1913eaf62fffSJeremy L Thompson } 1914eaf62fffSJeremy L Thompson } 1915eaf62fffSJeremy L Thompson // Default interface implementation 1916eaf62fffSJeremy L Thompson if (is_composite) { 19172b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled)); 1918eaf62fffSJeremy L Thompson } else { 19192b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, false, assembled)); 1920eaf62fffSJeremy L Thompson } 1921d04bbc78SJeremy L Thompson return CEED_ERROR_SUCCESS; 1922eaf62fffSJeremy L Thompson } 1923eaf62fffSJeremy L Thompson 1924eaf62fffSJeremy L Thompson /** 192501f0e615SJames Wright @brief Fully assemble the point-block diagonal pattern of a linear operator. 192601f0e615SJames Wright 192701f0e615SJames Wright Expected to be used in conjunction with CeedOperatorLinearAssemblePointBlockDiagonal(). 192801f0e615SJames Wright 192901f0e615SJames Wright The assembly routines use coordinate format, with `num_entries` tuples of the form (i, j, value) which indicate that value should be added to the 193001f0e615SJames Wright matrix in entry (i, j). 193101f0e615SJames Wright Note that the (i, j) pairs are unique. 193201f0e615SJames Wright This function returns the number of entries and their (i, j) locations, while CeedOperatorLinearAssemblePointBlockDiagonal() provides the values in 193301f0e615SJames Wright the same ordering. 193401f0e615SJames Wright 193501f0e615SJames Wright This will generally be slow unless your operator is low-order. 193601f0e615SJames Wright 193701f0e615SJames Wright Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 193801f0e615SJames Wright 193901f0e615SJames Wright @param[in] op CeedOperator to assemble 194001f0e615SJames Wright @param[out] num_entries Number of entries in coordinate nonzero pattern 194101f0e615SJames Wright @param[out] rows Row number for each entry 194201f0e615SJames Wright @param[out] cols Column number for each entry 194301f0e615SJames Wright 194401f0e615SJames Wright @ref User 194501f0e615SJames Wright **/ 194601f0e615SJames Wright int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) { 194701f0e615SJames Wright Ceed ceed; 194801f0e615SJames Wright bool is_composite; 194901f0e615SJames Wright CeedInt num_active_components, num_sub_operators; 195001f0e615SJames Wright CeedOperator *sub_operators; 195101f0e615SJames Wright 195201f0e615SJames Wright CeedCall(CeedOperatorGetCeed(op, &ceed)); 195301f0e615SJames Wright CeedCall(CeedOperatorIsComposite(op, &is_composite)); 195401f0e615SJames Wright 195501f0e615SJames Wright CeedSize input_size = 0, output_size = 0; 195601f0e615SJames Wright CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 195701f0e615SJames Wright CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 195801f0e615SJames Wright 195901f0e615SJames Wright if (is_composite) { 196001f0e615SJames Wright CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_operators)); 196101f0e615SJames Wright CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 196201f0e615SJames Wright } else { 196301f0e615SJames Wright sub_operators = &op; 196401f0e615SJames Wright num_sub_operators = 1; 196501f0e615SJames Wright } 196601f0e615SJames Wright 1967506b1a0cSSebastian Grimberg // Verify operator can be assembled correctly 1968506b1a0cSSebastian Grimberg { 196901f0e615SJames Wright CeedOperatorAssemblyData data; 1970506b1a0cSSebastian Grimberg CeedInt num_active_elem_rstrs, comp_stride; 197101f0e615SJames Wright CeedElemRestriction *active_elem_rstrs; 197201f0e615SJames Wright 197301f0e615SJames Wright // Get initial values to check against 197401f0e615SJames Wright CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[0], &data)); 1975506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL)); 197601f0e615SJames Wright CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[0], &comp_stride)); 197701f0e615SJames Wright CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[0], &num_active_components)); 197801f0e615SJames Wright 1979506b1a0cSSebastian Grimberg // Verify that all active element restrictions have same component stride and number of components 198001f0e615SJames Wright for (CeedInt k = 0; k < num_sub_operators; k++) { 198101f0e615SJames Wright CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[k], &data)); 1982506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL)); 198301f0e615SJames Wright for (CeedInt i = 0; i < num_active_elem_rstrs; i++) { 1984506b1a0cSSebastian Grimberg CeedInt comp_stride_sub, num_active_components_sub; 1985506b1a0cSSebastian Grimberg 198601f0e615SJames Wright CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[i], &comp_stride_sub)); 198701f0e615SJames Wright CeedCheck(comp_stride == comp_stride_sub, ceed, CEED_ERROR_DIMENSION, 198801f0e615SJames Wright "Active element restrictions must have the same component stride: %d vs %d", comp_stride, comp_stride_sub); 198901f0e615SJames Wright CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[i], &num_active_components_sub)); 199001f0e615SJames Wright CeedCheck(num_active_components == num_active_components_sub, ceed, CEED_ERROR_INCOMPATIBLE, 199101f0e615SJames Wright "All suboperators must have the same number of output components"); 199201f0e615SJames Wright } 199301f0e615SJames Wright } 199401f0e615SJames Wright } 199501f0e615SJames Wright *num_entries = input_size * num_active_components; 199601f0e615SJames Wright CeedCall(CeedCalloc(*num_entries, rows)); 199701f0e615SJames Wright CeedCall(CeedCalloc(*num_entries, cols)); 199801f0e615SJames Wright 199901f0e615SJames Wright for (CeedInt o = 0; o < num_sub_operators; o++) { 2000506b1a0cSSebastian Grimberg CeedElemRestriction active_elem_rstr, point_block_active_elem_rstr; 200101f0e615SJames Wright CeedInt comp_stride, num_elem, elem_size; 2002506b1a0cSSebastian Grimberg const CeedInt *offsets, *point_block_offsets; 200301f0e615SJames Wright 200401f0e615SJames Wright CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[o], &active_elem_rstr)); 200501f0e615SJames Wright CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstr, &comp_stride)); 200601f0e615SJames Wright CeedCall(CeedElemRestrictionGetNumElements(active_elem_rstr, &num_elem)); 200701f0e615SJames Wright CeedCall(CeedElemRestrictionGetElementSize(active_elem_rstr, &elem_size)); 200801f0e615SJames Wright CeedCall(CeedElemRestrictionGetOffsets(active_elem_rstr, CEED_MEM_HOST, &offsets)); 200901f0e615SJames Wright 2010506b1a0cSSebastian Grimberg CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstr, &point_block_active_elem_rstr)); 2011506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOffsets(point_block_active_elem_rstr, CEED_MEM_HOST, &point_block_offsets)); 201201f0e615SJames Wright 201301f0e615SJames Wright for (CeedSize i = 0; i < num_elem * elem_size; i++) { 201401f0e615SJames Wright for (CeedInt c_out = 0; c_out < num_active_components; c_out++) { 201501f0e615SJames Wright for (CeedInt c_in = 0; c_in < num_active_components; c_in++) { 2016506b1a0cSSebastian Grimberg (*rows)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_out * comp_stride; 2017506b1a0cSSebastian Grimberg (*cols)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_in * comp_stride; 201801f0e615SJames Wright } 201901f0e615SJames Wright } 202001f0e615SJames Wright } 202101f0e615SJames Wright 202201f0e615SJames Wright CeedCall(CeedElemRestrictionRestoreOffsets(active_elem_rstr, &offsets)); 2023506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOffsets(point_block_active_elem_rstr, &point_block_offsets)); 2024506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&point_block_active_elem_rstr)); 202501f0e615SJames Wright } 202601f0e615SJames Wright return CEED_ERROR_SUCCESS; 202701f0e615SJames Wright } 202801f0e615SJames Wright 202901f0e615SJames Wright /** 2030eaf62fffSJeremy L Thompson @brief Assemble the point block diagonal of a square linear CeedOperator 2031eaf62fffSJeremy L Thompson 2032ea61e9acSJeremy L Thompson This overwrites a CeedVector with the point block diagonal of a linear CeedOperator. 2033eaf62fffSJeremy L Thompson 2034ea61e9acSJeremy L Thompson Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported. 2035eaf62fffSJeremy L Thompson 2036ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 2037f04ea552SJeremy L Thompson 2038ea61e9acSJeremy L Thompson @param[in] op CeedOperator to assemble CeedQFunction 2039ea61e9acSJeremy L Thompson @param[out] assembled CeedVector to store assembled CeedOperator point block diagonal, provided in row-major form with an @a num_comp * @a num_comp 2040ea61e9acSJeremy L Thompson block at each node. The dimensions of this vector are derived from the active vector for the CeedOperator. The array has shape [nodes, component out, 2041ea61e9acSJeremy L Thompson component in]. 2042ea61e9acSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2043eaf62fffSJeremy L Thompson 2044eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2045eaf62fffSJeremy L Thompson 2046eaf62fffSJeremy L Thompson @ref User 2047eaf62fffSJeremy L Thompson **/ 20482b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 2049f3d47e36SJeremy L Thompson bool is_composite; 20501c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 20511c66c397SJeremy L Thompson 20522b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2053f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2054eaf62fffSJeremy L Thompson 20552b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 20566574a04fSJeremy L Thompson CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 2057c9366a6bSJeremy L Thompson 2058f3d47e36SJeremy L Thompson // Early exit for empty operator 2059f3d47e36SJeremy L Thompson if (!is_composite) { 2060f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 2061f3d47e36SJeremy L Thompson 2062f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 2063f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 2064f3d47e36SJeremy L Thompson } 2065f3d47e36SJeremy L Thompson 2066eaf62fffSJeremy L Thompson if (op->LinearAssemblePointBlockDiagonal) { 2067d04bbc78SJeremy L Thompson // Backend version 20682b730f8bSJeremy L Thompson CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request)); 2069eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2070eaf62fffSJeremy L Thompson } else if (op->LinearAssembleAddPointBlockDiagonal) { 2071d04bbc78SJeremy L Thompson // Backend version with zeroing first 20722b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 20732b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request)); 2074eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2075eaf62fffSJeremy L Thompson } else { 2076d04bbc78SJeremy L Thompson // Operator fallback 2077d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2078d04bbc78SJeremy L Thompson 20792b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2080d04bbc78SJeremy L Thompson if (op_fallback) { 20812b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request)); 2082eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2083eaf62fffSJeremy L Thompson } 2084eaf62fffSJeremy L Thompson } 2085eaf62fffSJeremy L Thompson // Default interface implementation 20862b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 20872b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request)); 2088eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2089eaf62fffSJeremy L Thompson } 2090eaf62fffSJeremy L Thompson 2091eaf62fffSJeremy L Thompson /** 2092eaf62fffSJeremy L Thompson @brief Assemble the point block diagonal of a square linear CeedOperator 2093eaf62fffSJeremy L Thompson 2094ea61e9acSJeremy L Thompson This sums into a CeedVector with the point block diagonal of a linear CeedOperator. 2095eaf62fffSJeremy L Thompson 2096ea61e9acSJeremy L Thompson Note: Currently only non-composite CeedOperators with a single field and composite CeedOperators with single field sub-operators are supported. 2097eaf62fffSJeremy L Thompson 2098ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 2099f04ea552SJeremy L Thompson 2100ea61e9acSJeremy L Thompson @param[in] op CeedOperator to assemble CeedQFunction 2101ea61e9acSJeremy L Thompson @param[out] assembled CeedVector to store assembled CeedOperator point block diagonal, provided in row-major form with an @a num_comp * @a num_comp 2102ea61e9acSJeremy L Thompson block at each node. The dimensions of this vector are derived from the active vector for the CeedOperator. The array has shape [nodes, component out, 2103ea61e9acSJeremy L Thompson component in]. 2104ea61e9acSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2105eaf62fffSJeremy L Thompson 2106eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2107eaf62fffSJeremy L Thompson 2108eaf62fffSJeremy L Thompson @ref User 2109eaf62fffSJeremy L Thompson **/ 21102b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 2111f3d47e36SJeremy L Thompson bool is_composite; 21121c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 21131c66c397SJeremy L Thompson 21142b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2115f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2116eaf62fffSJeremy L Thompson 21172b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 21186574a04fSJeremy L Thompson CeedCheck(input_size == output_size, op->ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 2119c9366a6bSJeremy L Thompson 2120f3d47e36SJeremy L Thompson // Early exit for empty operator 2121f3d47e36SJeremy L Thompson if (!is_composite) { 2122f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 2123f3d47e36SJeremy L Thompson 2124f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 2125f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 2126f3d47e36SJeremy L Thompson } 2127f3d47e36SJeremy L Thompson 2128eaf62fffSJeremy L Thompson if (op->LinearAssembleAddPointBlockDiagonal) { 2129d04bbc78SJeremy L Thompson // Backend version 21302b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request)); 2131eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2132eaf62fffSJeremy L Thompson } else { 2133d04bbc78SJeremy L Thompson // Operator fallback 2134d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2135d04bbc78SJeremy L Thompson 21362b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2137d04bbc78SJeremy L Thompson if (op_fallback) { 21382b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request)); 2139eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2140eaf62fffSJeremy L Thompson } 2141eaf62fffSJeremy L Thompson } 2142ea61e9acSJeremy L Thompson // Default interface implementation 2143eaf62fffSJeremy L Thompson if (is_composite) { 21442b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled)); 2145eaf62fffSJeremy L Thompson } else { 21462b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, true, assembled)); 2147eaf62fffSJeremy L Thompson } 2148d04bbc78SJeremy L Thompson return CEED_ERROR_SUCCESS; 2149eaf62fffSJeremy L Thompson } 2150eaf62fffSJeremy L Thompson 2151eaf62fffSJeremy L Thompson /** 2152eaf62fffSJeremy L Thompson @brief Fully assemble the nonzero pattern of a linear operator. 2153eaf62fffSJeremy L Thompson 2154ea61e9acSJeremy L Thompson Expected to be used in conjunction with CeedOperatorLinearAssemble(). 2155eaf62fffSJeremy L Thompson 2156ea61e9acSJeremy 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 21579fd66db6SSebastian Grimberg matrix in entry (i, j). 21589fd66db6SSebastian Grimberg Note that the (i, j) pairs are not unique and may repeat. 21599fd66db6SSebastian Grimberg This function returns the number of entries and their (i, j) locations, while CeedOperatorLinearAssemble() provides the values in the same ordering. 2160eaf62fffSJeremy L Thompson 2161eaf62fffSJeremy L Thompson This will generally be slow unless your operator is low-order. 2162eaf62fffSJeremy L Thompson 2163ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 2164f04ea552SJeremy L Thompson 2165eaf62fffSJeremy L Thompson @param[in] op CeedOperator to assemble 2166eaf62fffSJeremy L Thompson @param[out] num_entries Number of entries in coordinate nonzero pattern 2167eaf62fffSJeremy L Thompson @param[out] rows Row number for each entry 2168eaf62fffSJeremy L Thompson @param[out] cols Column number for each entry 2169eaf62fffSJeremy L Thompson 2170eaf62fffSJeremy L Thompson @ref User 2171eaf62fffSJeremy L Thompson **/ 21722b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) { 21731c66c397SJeremy L Thompson bool is_composite; 21741c66c397SJeremy L Thompson CeedInt num_suboperators, offset = 0; 2175b94338b9SJed Brown CeedSize single_entries; 2176eaf62fffSJeremy L Thompson CeedOperator *sub_operators; 21771c66c397SJeremy L Thompson 21782b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2179f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2180eaf62fffSJeremy L Thompson 2181eaf62fffSJeremy L Thompson if (op->LinearAssembleSymbolic) { 2182d04bbc78SJeremy L Thompson // Backend version 21832b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols)); 2184eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2185eaf62fffSJeremy L Thompson } else { 2186d04bbc78SJeremy L Thompson // Operator fallback 2187d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2188d04bbc78SJeremy L Thompson 21892b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2190d04bbc78SJeremy L Thompson if (op_fallback) { 21912b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols)); 2192eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2193eaf62fffSJeremy L Thompson } 2194eaf62fffSJeremy L Thompson } 2195eaf62fffSJeremy L Thompson 2196eaf62fffSJeremy L Thompson // Default interface implementation 2197eaf62fffSJeremy L Thompson 2198506b1a0cSSebastian Grimberg // Count entries and allocate rows, cols arrays 2199eaf62fffSJeremy L Thompson *num_entries = 0; 2200eaf62fffSJeremy L Thompson if (is_composite) { 2201c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2202c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 220392ae7e47SJeremy L Thompson for (CeedInt k = 0; k < num_suboperators; ++k) { 22042b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries)); 2205eaf62fffSJeremy L Thompson *num_entries += single_entries; 2206eaf62fffSJeremy L Thompson } 2207eaf62fffSJeremy L Thompson } else { 22082b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries)); 2209eaf62fffSJeremy L Thompson *num_entries += single_entries; 2210eaf62fffSJeremy L Thompson } 22112b730f8bSJeremy L Thompson CeedCall(CeedCalloc(*num_entries, rows)); 22122b730f8bSJeremy L Thompson CeedCall(CeedCalloc(*num_entries, cols)); 2213eaf62fffSJeremy L Thompson 2214506b1a0cSSebastian Grimberg // Assemble nonzero locations 2215eaf62fffSJeremy L Thompson if (is_composite) { 2216c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2217c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 221892ae7e47SJeremy L Thompson for (CeedInt k = 0; k < num_suboperators; ++k) { 22192b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols)); 22202b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries)); 2221eaf62fffSJeremy L Thompson offset += single_entries; 2222eaf62fffSJeremy L Thompson } 2223eaf62fffSJeremy L Thompson } else { 22242b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols)); 2225eaf62fffSJeremy L Thompson } 2226eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2227eaf62fffSJeremy L Thompson } 2228eaf62fffSJeremy L Thompson 2229eaf62fffSJeremy L Thompson /** 2230eaf62fffSJeremy L Thompson @brief Fully assemble the nonzero entries of a linear operator. 2231eaf62fffSJeremy L Thompson 2232ea61e9acSJeremy L Thompson Expected to be used in conjunction with CeedOperatorLinearAssembleSymbolic(). 2233eaf62fffSJeremy L Thompson 2234ea61e9acSJeremy 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 22359fd66db6SSebastian Grimberg matrix in entry (i, j). 22369fd66db6SSebastian Grimberg Note that the (i, j) pairs are not unique and may repeat. 22379fd66db6SSebastian Grimberg This function returns the values of the nonzero entries to be added, their (i, j) locations are provided by CeedOperatorLinearAssembleSymbolic() 2238eaf62fffSJeremy L Thompson 2239eaf62fffSJeremy L Thompson This will generally be slow unless your operator is low-order. 2240eaf62fffSJeremy L Thompson 2241ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 2242f04ea552SJeremy L Thompson 2243eaf62fffSJeremy L Thompson @param[in] op CeedOperator to assemble 2244eaf62fffSJeremy L Thompson @param[out] values Values to assemble into matrix 2245eaf62fffSJeremy L Thompson 2246eaf62fffSJeremy L Thompson @ref User 2247eaf62fffSJeremy L Thompson **/ 2248eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) { 22491c66c397SJeremy L Thompson bool is_composite; 22501c66c397SJeremy L Thompson CeedInt num_suboperators, offset = 0; 2251b94338b9SJed Brown CeedSize single_entries = 0; 2252eaf62fffSJeremy L Thompson CeedOperator *sub_operators; 22531c66c397SJeremy L Thompson 22542b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2255f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2256f3d47e36SJeremy L Thompson 2257f3d47e36SJeremy L Thompson // Early exit for empty operator 2258f3d47e36SJeremy L Thompson if (!is_composite) { 2259f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 2260f3d47e36SJeremy L Thompson 2261f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 2262f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 2263f3d47e36SJeremy L Thompson } 2264eaf62fffSJeremy L Thompson 2265eaf62fffSJeremy L Thompson if (op->LinearAssemble) { 2266d04bbc78SJeremy L Thompson // Backend version 22672b730f8bSJeremy L Thompson CeedCall(op->LinearAssemble(op, values)); 2268eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2269eaf62fffSJeremy L Thompson } else { 2270d04bbc78SJeremy L Thompson // Operator fallback 2271d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2272d04bbc78SJeremy L Thompson 22732b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2274d04bbc78SJeremy L Thompson if (op_fallback) { 22752b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssemble(op_fallback, values)); 2276eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2277eaf62fffSJeremy L Thompson } 2278eaf62fffSJeremy L Thompson } 2279eaf62fffSJeremy L Thompson 2280eaf62fffSJeremy L Thompson // Default interface implementation 228128ec399dSJeremy L Thompson CeedCall(CeedVectorSetValue(values, 0.0)); 2282eaf62fffSJeremy L Thompson if (is_composite) { 2283c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2284c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 2285cefa2673SJeremy L Thompson for (CeedInt k = 0; k < num_suboperators; k++) { 22862b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values)); 22872b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries)); 2288eaf62fffSJeremy L Thompson offset += single_entries; 2289eaf62fffSJeremy L Thompson } 2290eaf62fffSJeremy L Thompson } else { 22912b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemble(op, offset, values)); 2292eaf62fffSJeremy L Thompson } 2293eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2294eaf62fffSJeremy L Thompson } 2295eaf62fffSJeremy L Thompson 2296eaf62fffSJeremy L Thompson /** 229775f0d5a4SJeremy L Thompson @brief Get the multiplicity of nodes across suboperators in a composite CeedOperator 229875f0d5a4SJeremy L Thompson 229975f0d5a4SJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 230075f0d5a4SJeremy L Thompson 230175f0d5a4SJeremy L Thompson @param[in] op Composite CeedOperator 230275f0d5a4SJeremy L Thompson @param[in] num_skip_indices Number of suboperators to skip 230375f0d5a4SJeremy L Thompson @param[in] skip_indices Array of indices of suboperators to skip 230475f0d5a4SJeremy L Thompson @param[out] mult Vector to store multiplicity (of size l_size) 230575f0d5a4SJeremy L Thompson 230675f0d5a4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 230775f0d5a4SJeremy L Thompson 230875f0d5a4SJeremy L Thompson @ref User 230975f0d5a4SJeremy L Thompson **/ 231075f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) { 231175f0d5a4SJeremy L Thompson Ceed ceed; 2312b275c451SJeremy L Thompson CeedInt num_suboperators; 231375f0d5a4SJeremy L Thompson CeedSize l_vec_len; 231475f0d5a4SJeremy L Thompson CeedScalar *mult_array; 231575f0d5a4SJeremy L Thompson CeedVector ones_l_vec; 23167c1dbaffSSebastian Grimberg CeedElemRestriction elem_rstr, mult_elem_rstr; 2317b275c451SJeremy L Thompson CeedOperator *sub_operators; 231875f0d5a4SJeremy L Thompson 23191c66c397SJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 23201c66c397SJeremy L Thompson 232175f0d5a4SJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 232275f0d5a4SJeremy L Thompson 232375f0d5a4SJeremy L Thompson // Zero mult vector 232475f0d5a4SJeremy L Thompson CeedCall(CeedVectorSetValue(mult, 0.0)); 232575f0d5a4SJeremy L Thompson 232675f0d5a4SJeremy L Thompson // Get suboperators 2327b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2328b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 2329b275c451SJeremy L Thompson if (num_suboperators == 0) return CEED_ERROR_SUCCESS; 233075f0d5a4SJeremy L Thompson 233175f0d5a4SJeremy L Thompson // Work vector 233275f0d5a4SJeremy L Thompson CeedCall(CeedVectorGetLength(mult, &l_vec_len)); 233375f0d5a4SJeremy L Thompson CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec)); 233475f0d5a4SJeremy L Thompson CeedCall(CeedVectorSetValue(ones_l_vec, 1.0)); 233575f0d5a4SJeremy L Thompson CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array)); 233675f0d5a4SJeremy L Thompson 233775f0d5a4SJeremy L Thompson // Compute multiplicity across suboperators 2338b275c451SJeremy L Thompson for (CeedInt i = 0; i < num_suboperators; i++) { 233975f0d5a4SJeremy L Thompson const CeedScalar *sub_mult_array; 234075f0d5a4SJeremy L Thompson CeedVector sub_mult_l_vec, ones_e_vec; 234175f0d5a4SJeremy L Thompson 234275f0d5a4SJeremy L Thompson // -- Check for suboperator to skip 234375f0d5a4SJeremy L Thompson for (CeedInt j = 0; j < num_skip_indices; j++) { 234475f0d5a4SJeremy L Thompson if (skip_indices[j] == i) continue; 234575f0d5a4SJeremy L Thompson } 234675f0d5a4SJeremy L Thompson 234775f0d5a4SJeremy L Thompson // -- Sub operator multiplicity 2348437c7c90SJeremy L Thompson CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr)); 23497c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr, &mult_elem_rstr)); 23507c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionCreateVector(mult_elem_rstr, &sub_mult_l_vec, &ones_e_vec)); 235175f0d5a4SJeremy L Thompson CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0)); 23527c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE)); 23537c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE)); 235475f0d5a4SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array)); 235575f0d5a4SJeremy L Thompson // ---- Flag every node present in the current suboperator 235675f0d5a4SJeremy L Thompson for (CeedInt j = 0; j < l_vec_len; j++) { 235775f0d5a4SJeremy L Thompson if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0; 235875f0d5a4SJeremy L Thompson } 235975f0d5a4SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array)); 236075f0d5a4SJeremy L Thompson CeedCall(CeedVectorDestroy(&sub_mult_l_vec)); 236175f0d5a4SJeremy L Thompson CeedCall(CeedVectorDestroy(&ones_e_vec)); 23627c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&mult_elem_rstr)); 236375f0d5a4SJeremy L Thompson } 236475f0d5a4SJeremy L Thompson CeedCall(CeedVectorRestoreArray(mult, &mult_array)); 2365811d0ccfSJeremy L Thompson CeedCall(CeedVectorDestroy(&ones_l_vec)); 236675f0d5a4SJeremy L Thompson return CEED_ERROR_SUCCESS; 236775f0d5a4SJeremy L Thompson } 236875f0d5a4SJeremy L Thompson 236975f0d5a4SJeremy L Thompson /** 2370ea61e9acSJeremy L Thompson @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator, creating the prolongation basis from the fine and coarse 2371ea61e9acSJeremy L Thompson grid interpolation 2372eaf62fffSJeremy L Thompson 237358e4b056SJeremy L Thompson Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable. 2374f04ea552SJeremy L Thompson 2375eaf62fffSJeremy L Thompson @param[in] op_fine Fine grid operator 237685bb9dcfSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators 2377eaf62fffSJeremy L Thompson @param[in] rstr_coarse Coarse grid restriction 2378eaf62fffSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector basis 2379eaf62fffSJeremy L Thompson @param[out] op_coarse Coarse grid operator 238085bb9dcfSJeremy L Thompson @param[out] op_prolong Coarse to fine operator, or NULL 23817758292fSSebastian Grimberg @param[out] op_restrict Fine to coarse operator, or NULL 2382eaf62fffSJeremy L Thompson 2383eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2384eaf62fffSJeremy L Thompson 2385eaf62fffSJeremy L Thompson @ref User 2386eaf62fffSJeremy L Thompson **/ 23872b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 23887758292fSSebastian Grimberg CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) { 23891c66c397SJeremy L Thompson CeedBasis basis_c_to_f = NULL; 23901c66c397SJeremy L Thompson 23912b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fine)); 2392eaf62fffSJeremy L Thompson 239383d6adf3SZach Atkins // Build prolongation matrix, if required 23947758292fSSebastian Grimberg if (op_prolong || op_restrict) { 239583d6adf3SZach Atkins CeedBasis basis_fine; 23961c66c397SJeremy L Thompson 23972b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine)); 23982b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f)); 239983d6adf3SZach Atkins } 2400eaf62fffSJeremy L Thompson 2401f113e5dcSJeremy L Thompson // Core code 24027758292fSSebastian Grimberg CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict)); 2403eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2404eaf62fffSJeremy L Thompson } 2405eaf62fffSJeremy L Thompson 2406eaf62fffSJeremy L Thompson /** 2407ea61e9acSJeremy L Thompson @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a tensor basis for the active basis 2408eaf62fffSJeremy L Thompson 240958e4b056SJeremy L Thompson Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable. 2410f04ea552SJeremy L Thompson 2411eaf62fffSJeremy L Thompson @param[in] op_fine Fine grid operator 241285bb9dcfSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators 2413eaf62fffSJeremy L Thompson @param[in] rstr_coarse Coarse grid restriction 2414eaf62fffSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector basis 241585bb9dcfSJeremy L Thompson @param[in] interp_c_to_f Matrix for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators 2416eaf62fffSJeremy L Thompson @param[out] op_coarse Coarse grid operator 241785bb9dcfSJeremy L Thompson @param[out] op_prolong Coarse to fine operator, or NULL 24187758292fSSebastian Grimberg @param[out] op_restrict Fine to coarse operator, or NULL 2419eaf62fffSJeremy L Thompson 2420eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2421eaf62fffSJeremy L Thompson 2422eaf62fffSJeremy L Thompson @ref User 2423eaf62fffSJeremy L Thompson **/ 24242b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 24252b730f8bSJeremy L Thompson const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, 24267758292fSSebastian Grimberg CeedOperator *op_restrict) { 2427eaf62fffSJeremy L Thompson Ceed ceed; 24281c66c397SJeremy L Thompson CeedInt Q_f, Q_c; 24291c66c397SJeremy L Thompson CeedBasis basis_fine, basis_c_to_f = NULL; 24301c66c397SJeremy L Thompson 24311c66c397SJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fine)); 24322b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op_fine, &ceed)); 2433eaf62fffSJeremy L Thompson 2434eaf62fffSJeremy L Thompson // Check for compatible quadrature spaces 24352b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine)); 24362b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f)); 24372b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c)); 24386574a04fSJeremy L Thompson CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces"); 2439eaf62fffSJeremy L Thompson 244083d6adf3SZach Atkins // Create coarse to fine basis, if required 24417758292fSSebastian Grimberg if (op_prolong || op_restrict) { 24421c66c397SJeremy L Thompson CeedInt dim, num_comp, num_nodes_c, P_1d_f, P_1d_c; 24431c66c397SJeremy L Thompson CeedScalar *q_ref, *q_weight, *grad; 24441c66c397SJeremy L Thompson 244583d6adf3SZach Atkins // Check if interpolation matrix is provided 24466574a04fSJeremy L Thompson CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE, 24476574a04fSJeremy L Thompson "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix"); 24482b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_fine, &dim)); 24492b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp)); 24502b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f)); 24512b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c)); 24522b730f8bSJeremy L Thompson P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c); 24532b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_f, &q_ref)); 24542b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_f, &q_weight)); 24552b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad)); 24562b730f8bSJeremy 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)); 24572b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref)); 24582b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight)); 24592b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad)); 246083d6adf3SZach Atkins } 2461eaf62fffSJeremy L Thompson 2462eaf62fffSJeremy L Thompson // Core code 24637758292fSSebastian Grimberg CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict)); 2464eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2465eaf62fffSJeremy L Thompson } 2466eaf62fffSJeremy L Thompson 2467eaf62fffSJeremy L Thompson /** 2468ea61e9acSJeremy L Thompson @brief Create a multigrid coarse operator and level transfer operators for a CeedOperator with a non-tensor basis for the active vector 2469eaf62fffSJeremy L Thompson 247058e4b056SJeremy L Thompson Note: Calling this function asserts that setup is complete and sets all four CeedOperators as immutable. 2471f04ea552SJeremy L Thompson 2472eaf62fffSJeremy L Thompson @param[in] op_fine Fine grid operator 247385bb9dcfSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or NULL if not creating prolongation/restriction operators 2474eaf62fffSJeremy L Thompson @param[in] rstr_coarse Coarse grid restriction 2475eaf62fffSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector basis 247685bb9dcfSJeremy L Thompson @param[in] interp_c_to_f Matrix for coarse to fine interpolation, or NULL if not creating prolongation/restriction operators 2477eaf62fffSJeremy L Thompson @param[out] op_coarse Coarse grid operator 247885bb9dcfSJeremy L Thompson @param[out] op_prolong Coarse to fine operator, or NULL 24797758292fSSebastian Grimberg @param[out] op_restrict Fine to coarse operator, or NULL 2480eaf62fffSJeremy L Thompson 2481eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2482eaf62fffSJeremy L Thompson 2483eaf62fffSJeremy L Thompson @ref User 2484eaf62fffSJeremy L Thompson **/ 24852b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 24867758292fSSebastian Grimberg const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, 24877758292fSSebastian Grimberg CeedOperator *op_restrict) { 2488eaf62fffSJeremy L Thompson Ceed ceed; 24891c66c397SJeremy L Thompson CeedInt Q_f, Q_c; 24901c66c397SJeremy L Thompson CeedBasis basis_fine, basis_c_to_f = NULL; 24911c66c397SJeremy L Thompson 24921c66c397SJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fine)); 24932b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op_fine, &ceed)); 2494eaf62fffSJeremy L Thompson 2495eaf62fffSJeremy L Thompson // Check for compatible quadrature spaces 24962b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine)); 24972b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f)); 24982b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c)); 24996574a04fSJeremy L Thompson CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces"); 2500eaf62fffSJeremy L Thompson 2501eaf62fffSJeremy L Thompson // Coarse to fine basis 25027758292fSSebastian Grimberg if (op_prolong || op_restrict) { 25031c66c397SJeremy L Thompson CeedInt dim, num_comp, num_nodes_c, num_nodes_f; 25041c66c397SJeremy L Thompson CeedScalar *q_ref, *q_weight, *grad; 25051c66c397SJeremy L Thompson CeedElemTopology topo; 25061c66c397SJeremy L Thompson 250783d6adf3SZach Atkins // Check if interpolation matrix is provided 25086574a04fSJeremy L Thompson CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE, 25096574a04fSJeremy L Thompson "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix"); 25102b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopology(basis_fine, &topo)); 25112b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_fine, &dim)); 25122b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp)); 25132b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f)); 25142b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c)); 25152b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref)); 25162b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_f, &q_weight)); 25172b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad)); 25182b730f8bSJeremy 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)); 25192b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref)); 25202b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight)); 25212b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad)); 252283d6adf3SZach Atkins } 2523eaf62fffSJeremy L Thompson 2524eaf62fffSJeremy L Thompson // Core code 25257758292fSSebastian Grimberg CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict)); 2526eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2527eaf62fffSJeremy L Thompson } 2528eaf62fffSJeremy L Thompson 2529eaf62fffSJeremy L Thompson /** 2530ea61e9acSJeremy L Thompson @brief Build a FDM based approximate inverse for each element for a CeedOperator 2531eaf62fffSJeremy L Thompson 2532ea61e9acSJeremy L Thompson This returns a CeedOperator and CeedVector to apply a Fast Diagonalization Method based approximate inverse. 2533859c15bbSJames 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$. 2534859c15bbSJames Wright The assembled QFunction is used to modify the eigenvalues from simultaneous diagonalization and obtain an approximate inverse of the form \f$V^T 25359fd66db6SSebastian Grimberg \hat S V\f$. 25369fd66db6SSebastian Grimberg The CeedOperator must be linear and non-composite. 25379fd66db6SSebastian Grimberg The associated CeedQFunction must therefore also be linear. 2538eaf62fffSJeremy L Thompson 2539ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 2540f04ea552SJeremy L Thompson 2541ea61e9acSJeremy L Thompson @param[in] op CeedOperator to create element inverses 2542ea61e9acSJeremy L Thompson @param[out] fdm_inv CeedOperator to apply the action of a FDM based inverse for each element 2543ea61e9acSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2544eaf62fffSJeremy L Thompson 2545eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2546eaf62fffSJeremy L Thompson 2547480fae85SJeremy L Thompson @ref User 2548eaf62fffSJeremy L Thompson **/ 25492b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) { 25501c66c397SJeremy L Thompson Ceed ceed, ceed_parent; 25511c66c397SJeremy L Thompson bool interp = false, grad = false, is_tensor_basis = true; 25521c66c397SJeremy L Thompson CeedInt num_input_fields, P_1d, Q_1d, num_nodes, num_qpts, dim, num_comp = 1, num_elem = 1; 25531c66c397SJeremy L Thompson CeedSize l_size = 1; 25541c66c397SJeremy L Thompson CeedScalar *mass, *laplace, *x, *fdm_interp, *lambda, *elem_avg; 25551c66c397SJeremy L Thompson const CeedScalar *interp_1d, *grad_1d, *q_weight_1d; 25561c66c397SJeremy L Thompson CeedVector q_data; 25571c66c397SJeremy L Thompson CeedElemRestriction rstr = NULL, rstr_qd_i; 25581c66c397SJeremy L Thompson CeedBasis basis = NULL, fdm_basis; 25591c66c397SJeremy L Thompson CeedQFunctionContext ctx_fdm; 25601c66c397SJeremy L Thompson CeedQFunctionField *qf_fields; 25611c66c397SJeremy L Thompson CeedQFunction qf, qf_fdm; 25621c66c397SJeremy L Thompson CeedOperatorField *op_fields; 25631c66c397SJeremy L Thompson 25642b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2565eaf62fffSJeremy L Thompson 2566eaf62fffSJeremy L Thompson if (op->CreateFDMElementInverse) { 2567d04bbc78SJeremy L Thompson // Backend version 25682b730f8bSJeremy L Thompson CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request)); 2569eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2570eaf62fffSJeremy L Thompson } else { 2571d04bbc78SJeremy L Thompson // Operator fallback 2572d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2573d04bbc78SJeremy L Thompson 25742b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2575d04bbc78SJeremy L Thompson if (op_fallback) { 25762b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request)); 2577eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2578eaf62fffSJeremy L Thompson } 2579eaf62fffSJeremy L Thompson } 2580eaf62fffSJeremy L Thompson 2581d04bbc78SJeremy L Thompson // Default interface implementation 25822b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 2583bb229da9SJeremy L Thompson CeedCall(CeedOperatorGetFallbackParentCeed(op, &ceed_parent)); 25842b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetQFunction(op, &qf)); 2585eaf62fffSJeremy L Thompson 2586eaf62fffSJeremy L Thompson // Determine active input basis 25872b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL)); 25882b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 2589eaf62fffSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 2590eaf62fffSJeremy L Thompson CeedVector vec; 25911c66c397SJeremy L Thompson 25922b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec)); 2593eaf62fffSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 2594eaf62fffSJeremy L Thompson CeedEvalMode eval_mode; 25951c66c397SJeremy L Thompson 25962b730f8bSJeremy L Thompson CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 2597eaf62fffSJeremy L Thompson interp = interp || eval_mode == CEED_EVAL_INTERP; 2598eaf62fffSJeremy L Thompson grad = grad || eval_mode == CEED_EVAL_GRAD; 25992b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 26002b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr)); 2601eaf62fffSJeremy L Thompson } 2602eaf62fffSJeremy L Thompson } 26036574a04fSJeremy L Thompson CeedCheck(basis, ceed, CEED_ERROR_BACKEND, "No active field set"); 26042b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 2605352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 26062b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 26072b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 26082b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 26092b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 26102b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem)); 26112b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size)); 2612eaf62fffSJeremy L Thompson 2613eaf62fffSJeremy L Thompson // Build and diagonalize 1D Mass and Laplacian 26146574a04fSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 26156574a04fSJeremy L Thompson CeedCheck(is_tensor_basis, ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases"); 26162b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &mass)); 26172b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &laplace)); 26182b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &x)); 26192b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp)); 26202b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d, &lambda)); 2621eaf62fffSJeremy L Thompson // -- Build matrices 26222b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 26232b730f8bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 26242b730f8bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d)); 26252b730f8bSJeremy L Thompson CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace)); 2626eaf62fffSJeremy L Thompson 2627eaf62fffSJeremy L Thompson // -- Diagonalize 26282b730f8bSJeremy L Thompson CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d)); 26292b730f8bSJeremy L Thompson CeedCall(CeedFree(&mass)); 26302b730f8bSJeremy L Thompson CeedCall(CeedFree(&laplace)); 26312b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) { 26322b730f8bSJeremy L Thompson for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d]; 26332b730f8bSJeremy L Thompson } 26342b730f8bSJeremy L Thompson CeedCall(CeedFree(&x)); 2635eaf62fffSJeremy L Thompson 26361c66c397SJeremy L Thompson { 26371c66c397SJeremy L Thompson CeedInt layout[3], num_modes = (interp ? 1 : 0) + (grad ? dim : 0); 26381c66c397SJeremy L Thompson CeedScalar max_norm = 0; 26391c66c397SJeremy L Thompson const CeedScalar *assembled_array, *q_weight_array; 26401c66c397SJeremy L Thompson CeedVector assembled = NULL, q_weight; 2641c5f45aeaSJeremy L Thompson CeedElemRestriction rstr_qf = NULL; 26421c66c397SJeremy L Thompson 26431c66c397SJeremy L Thompson // Assemble QFunction 26442b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request)); 26452b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(rstr_qf, &layout)); 26462b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_qf)); 26472b730f8bSJeremy L Thompson CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm)); 2648eaf62fffSJeremy L Thompson 2649eaf62fffSJeremy L Thompson // Calculate element averages 26502b730f8bSJeremy L Thompson CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight)); 26512b730f8bSJeremy L Thompson CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight)); 26522b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array)); 26532b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array)); 26542b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_elem, &elem_avg)); 2655eaf62fffSJeremy L Thompson const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON; 26561c66c397SJeremy L Thompson 2657eaf62fffSJeremy L Thompson for (CeedInt e = 0; e < num_elem; e++) { 2658eaf62fffSJeremy L Thompson CeedInt count = 0; 26591c66c397SJeremy L Thompson 26602b730f8bSJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 26612b730f8bSJeremy L Thompson for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) { 26622b730f8bSJeremy L Thompson if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) { 26632b730f8bSJeremy L Thompson elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q]; 2664eaf62fffSJeremy L Thompson count++; 2665eaf62fffSJeremy L Thompson } 26662b730f8bSJeremy L Thompson } 26672b730f8bSJeremy L Thompson } 2668eaf62fffSJeremy L Thompson if (count) { 2669eaf62fffSJeremy L Thompson elem_avg[e] /= count; 2670eaf62fffSJeremy L Thompson } else { 2671eaf62fffSJeremy L Thompson elem_avg[e] = 1.0; 2672eaf62fffSJeremy L Thompson } 2673eaf62fffSJeremy L Thompson } 26742b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array)); 26752b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled)); 26762b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array)); 26772b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&q_weight)); 26781c66c397SJeremy L Thompson } 2679eaf62fffSJeremy L Thompson 2680eaf62fffSJeremy L Thompson // Build FDM diagonal 26811c66c397SJeremy L Thompson { 2682eaf62fffSJeremy L Thompson CeedScalar *q_data_array, *fdm_diagonal; 26831c66c397SJeremy L Thompson 2684352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_comp * num_nodes, &fdm_diagonal)); 2685352a5e7cSSebastian Grimberg const CeedScalar fdm_diagonal_bound = num_nodes * CEED_EPSILON; 26862b730f8bSJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) { 2687352a5e7cSSebastian Grimberg for (CeedInt n = 0; n < num_nodes; n++) { 2688352a5e7cSSebastian Grimberg if (interp) fdm_diagonal[c * num_nodes + n] = 1.0; 26892b730f8bSJeremy L Thompson if (grad) { 2690eaf62fffSJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 2691eaf62fffSJeremy L Thompson CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d; 2692352a5e7cSSebastian Grimberg fdm_diagonal[c * num_nodes + n] += lambda[i]; 2693eaf62fffSJeremy L Thompson } 2694eaf62fffSJeremy L Thompson } 2695352a5e7cSSebastian Grimberg if (fabs(fdm_diagonal[c * num_nodes + n]) < fdm_diagonal_bound) fdm_diagonal[c * num_nodes + n] = fdm_diagonal_bound; 26962b730f8bSJeremy L Thompson } 26972b730f8bSJeremy L Thompson } 2698352a5e7cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * num_nodes, &q_data)); 26992b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(q_data, 0.0)); 27002b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array)); 27012b730f8bSJeremy L Thompson for (CeedInt e = 0; e < num_elem; e++) { 27022b730f8bSJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) { 27031c66c397SJeremy L Thompson for (CeedInt n = 0; n < num_nodes; n++) 27041c66c397SJeremy L Thompson q_data_array[(e * num_comp + c) * num_nodes + n] = 1. / (elem_avg[e] * fdm_diagonal[c * num_nodes + n]); 27052b730f8bSJeremy L Thompson } 27062b730f8bSJeremy L Thompson } 27072b730f8bSJeremy L Thompson CeedCall(CeedFree(&elem_avg)); 27082b730f8bSJeremy L Thompson CeedCall(CeedFree(&fdm_diagonal)); 27092b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(q_data, &q_data_array)); 27101c66c397SJeremy L Thompson } 2711eaf62fffSJeremy L Thompson 2712eaf62fffSJeremy L Thompson // Setup FDM operator 2713eaf62fffSJeremy L Thompson // -- Basis 27141c66c397SJeremy L Thompson { 2715eaf62fffSJeremy L Thompson CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy; 27161c66c397SJeremy L Thompson 27172b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy)); 27182b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d, &q_ref_dummy)); 27192b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d, &q_weight_dummy)); 27202b730f8bSJeremy 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)); 27212b730f8bSJeremy L Thompson CeedCall(CeedFree(&fdm_interp)); 27222b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_dummy)); 27232b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref_dummy)); 27242b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight_dummy)); 27252b730f8bSJeremy L Thompson CeedCall(CeedFree(&lambda)); 27261c66c397SJeremy L Thompson } 2727eaf62fffSJeremy L Thompson 2728eaf62fffSJeremy L Thompson // -- Restriction 27291c66c397SJeremy L Thompson { 2730352a5e7cSSebastian Grimberg CeedInt strides[3] = {1, num_nodes, num_nodes * num_comp}; 2731352a5e7cSSebastian Grimberg CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, num_nodes, num_comp, num_elem * num_comp * num_nodes, strides, &rstr_qd_i)); 27321c66c397SJeremy L Thompson } 27331c66c397SJeremy L Thompson 2734eaf62fffSJeremy L Thompson // -- QFunction 27352b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm)); 27362b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP)); 27372b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE)); 27382b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP)); 27392b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp)); 27401c66c397SJeremy L Thompson 2741eaf62fffSJeremy L Thompson // -- QFunction context 27421c66c397SJeremy L Thompson { 2743eaf62fffSJeremy L Thompson CeedInt *num_comp_data; 27441c66c397SJeremy L Thompson 27452b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &num_comp_data)); 2746eaf62fffSJeremy L Thompson num_comp_data[0] = num_comp; 27472b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm)); 27482b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data)); 27491c66c397SJeremy L Thompson } 27502b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm)); 27512b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(&ctx_fdm)); 27521c66c397SJeremy L Thompson 2753eaf62fffSJeremy L Thompson // -- Operator 27542b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv)); 27552b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE)); 2756356036faSJeremy L Thompson CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_NONE, q_data)); 27572b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE)); 2758eaf62fffSJeremy L Thompson 2759eaf62fffSJeremy L Thompson // Cleanup 27602b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&q_data)); 27612b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(&fdm_basis)); 27622b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i)); 27632b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&qf_fdm)); 2764eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2765eaf62fffSJeremy L Thompson } 2766eaf62fffSJeremy L Thompson 2767eaf62fffSJeremy L Thompson /// @} 2768