13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3eaf62fffSJeremy L Thompson // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5eaf62fffSJeremy L Thompson // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7eaf62fffSJeremy L Thompson 82b730f8bSJeremy L Thompson #include <ceed-impl.h> 949aac155SJeremy L Thompson #include <ceed.h> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 11c85e8640SSebastian Grimberg #include <assert.h> 122b730f8bSJeremy L Thompson #include <math.h> 13eaf62fffSJeremy L Thompson #include <stdbool.h> 14eaf62fffSJeremy L Thompson #include <stdio.h> 15eaf62fffSJeremy L Thompson #include <string.h> 16eaf62fffSJeremy L Thompson 17eaf62fffSJeremy L Thompson /// @file 18eaf62fffSJeremy L Thompson /// Implementation of CeedOperator preconditioning interfaces 19eaf62fffSJeremy L Thompson 20eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 21eaf62fffSJeremy L Thompson /// CeedOperator Library Internal Preconditioning Functions 22eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 23eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorDeveloper 24eaf62fffSJeremy L Thompson /// @{ 25eaf62fffSJeremy L Thompson 26eaf62fffSJeremy L Thompson /** 27ca94c3ddSJeremy L Thompson @brief Duplicate a `CeedQFunction` with a reference `Ceed` to fallback for advanced `CeedOperator` functionality 289e77b9c8SJeremy L Thompson 29ca94c3ddSJeremy L Thompson @param[in] fallback_ceed `Ceed` on which to create fallback `CeedQFunction` 30ca94c3ddSJeremy L Thompson @param[in] qf `CeedQFunction` to create fallback for 31ca94c3ddSJeremy L Thompson @param[out] qf_fallback Fallback `CeedQFunction` 329e77b9c8SJeremy L Thompson 339e77b9c8SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 349e77b9c8SJeremy L Thompson 359e77b9c8SJeremy L Thompson @ref Developer 369e77b9c8SJeremy L Thompson **/ 372b730f8bSJeremy L Thompson static int CeedQFunctionCreateFallback(Ceed fallback_ceed, CeedQFunction qf, CeedQFunction *qf_fallback) { 381c66c397SJeremy L Thompson char *source_path_with_name = NULL; 391203703bSJeremy L Thompson CeedInt num_input_fields, num_output_fields; 401203703bSJeremy L Thompson Ceed ceed; 411203703bSJeremy L Thompson CeedQFunctionField *input_fields, *output_fields; 421c66c397SJeremy L Thompson 439e77b9c8SJeremy L Thompson // Check if NULL qf passed in 449e77b9c8SJeremy L Thompson if (!qf) return CEED_ERROR_SUCCESS; 459e77b9c8SJeremy L Thompson 461203703bSJeremy L Thompson CeedCall(CeedQFunctionGetCeed(qf, &ceed)); 471203703bSJeremy L Thompson CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n"); 481203703bSJeremy L Thompson CeedDebug(ceed, "Creating fallback CeedQFunction\n"); 49d04bbc78SJeremy L Thompson 509e77b9c8SJeremy L Thompson if (qf->source_path) { 512b730f8bSJeremy L Thompson size_t path_len = strlen(qf->source_path), name_len = strlen(qf->kernel_name); 522b730f8bSJeremy L Thompson CeedCall(CeedCalloc(path_len + name_len + 2, &source_path_with_name)); 539e77b9c8SJeremy L Thompson memcpy(source_path_with_name, qf->source_path, path_len); 549e77b9c8SJeremy L Thompson memcpy(&source_path_with_name[path_len], ":", 1); 559e77b9c8SJeremy L Thompson memcpy(&source_path_with_name[path_len + 1], qf->kernel_name, name_len); 569e77b9c8SJeremy L Thompson } else { 572b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &source_path_with_name)); 589e77b9c8SJeremy L Thompson } 599e77b9c8SJeremy L Thompson 601203703bSJeremy L Thompson { 611203703bSJeremy L Thompson CeedInt vec_length; 621203703bSJeremy L Thompson CeedQFunctionUser f; 631203703bSJeremy L Thompson 641203703bSJeremy L Thompson CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length)); 651203703bSJeremy L Thompson CeedCall(CeedQFunctionGetUserFunction(qf, &f)); 661203703bSJeremy L Thompson CeedCall(CeedQFunctionCreateInterior(fallback_ceed, vec_length, f, source_path_with_name, qf_fallback)); 671203703bSJeremy L Thompson } 689e77b9c8SJeremy L Thompson { 699e77b9c8SJeremy L Thompson CeedQFunctionContext ctx; 709e77b9c8SJeremy L Thompson 712b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetContext(qf, &ctx)); 722b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx)); 739e77b9c8SJeremy L Thompson } 741203703bSJeremy L Thompson CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 751203703bSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 766f8994e9SJeremy L Thompson const char *field_name; 771203703bSJeremy L Thompson CeedInt size; 781203703bSJeremy L Thompson CeedEvalMode eval_mode; 791203703bSJeremy L Thompson 80ab747706SJeremy L Thompson CeedCall(CeedQFunctionFieldGetData(input_fields[i], &field_name, &size, &eval_mode)); 811203703bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(*qf_fallback, field_name, size, eval_mode)); 829e77b9c8SJeremy L Thompson } 831203703bSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 846f8994e9SJeremy L Thompson const char *field_name; 851203703bSJeremy L Thompson CeedInt size; 861203703bSJeremy L Thompson CeedEvalMode eval_mode; 871203703bSJeremy L Thompson 88ab747706SJeremy L Thompson CeedCall(CeedQFunctionFieldGetData(output_fields[i], &field_name, &size, &eval_mode)); 891203703bSJeremy L Thompson CeedCall(CeedQFunctionAddOutput(*qf_fallback, field_name, size, eval_mode)); 909e77b9c8SJeremy L Thompson } 912b730f8bSJeremy L Thompson CeedCall(CeedFree(&source_path_with_name)); 929e77b9c8SJeremy L Thompson return CEED_ERROR_SUCCESS; 939e77b9c8SJeremy L Thompson } 949e77b9c8SJeremy L Thompson 959e77b9c8SJeremy L Thompson /** 96ca94c3ddSJeremy L Thompson @brief Duplicate a `CeedOperator` with a reference `Ceed` to fallback for advanced `CeedOperator` functionality 97eaf62fffSJeremy L Thompson 98ca94c3ddSJeremy L Thompson @param[in,out] op `CeedOperator` to create fallback for 99eaf62fffSJeremy L Thompson 100eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 101eaf62fffSJeremy L Thompson 102eaf62fffSJeremy L Thompson @ref Developer 103eaf62fffSJeremy L Thompson **/ 104d04bbc78SJeremy L Thompson static int CeedOperatorCreateFallback(CeedOperator op) { 1051c66c397SJeremy L Thompson bool is_composite; 1061203703bSJeremy L Thompson Ceed ceed, ceed_fallback; 1071c66c397SJeremy L Thompson CeedOperator op_fallback; 108eaf62fffSJeremy L Thompson 109805fe78eSJeremy L Thompson // Check not already created 110805fe78eSJeremy L Thompson if (op->op_fallback) return CEED_ERROR_SUCCESS; 111805fe78eSJeremy L Thompson 112eaf62fffSJeremy L Thompson // Fallback Ceed 1131203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 1141203703bSJeremy L Thompson CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback)); 115d04bbc78SJeremy L Thompson if (!ceed_fallback) return CEED_ERROR_SUCCESS; 116d04bbc78SJeremy L Thompson 1171203703bSJeremy L Thompson CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n"); 1181203703bSJeremy L Thompson CeedDebug(ceed, "Creating fallback CeedOperator\n"); 119eaf62fffSJeremy L Thompson 120eaf62fffSJeremy L Thompson // Clone Op 121b275c451SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 122b275c451SJeremy L Thompson if (is_composite) { 123b275c451SJeremy L Thompson CeedInt num_suboperators; 124b275c451SJeremy L Thompson CeedOperator *sub_operators; 125b275c451SJeremy L Thompson 1262b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorCreate(ceed_fallback, &op_fallback)); 127b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 128b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 129b275c451SJeremy L Thompson for (CeedInt i = 0; i < num_suboperators; i++) { 130d04bbc78SJeremy L Thompson CeedOperator op_sub_fallback; 131d04bbc78SJeremy L Thompson 132b275c451SJeremy L Thompson CeedCall(CeedOperatorGetFallback(sub_operators[i], &op_sub_fallback)); 1332b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorAddSub(op_fallback, op_sub_fallback)); 134805fe78eSJeremy L Thompson } 135805fe78eSJeremy L Thompson } else { 1361203703bSJeremy L Thompson CeedInt num_input_fields, num_output_fields; 1379e77b9c8SJeremy L Thompson CeedQFunction qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL; 1381203703bSJeremy L Thompson CeedOperatorField *input_fields, *output_fields; 1391c66c397SJeremy L Thompson 1402b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback)); 1412b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback)); 1422b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback)); 1432b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback)); 1441203703bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 1451203703bSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1466f8994e9SJeremy L Thompson const char *field_name; 1471203703bSJeremy L Thompson CeedVector vec; 1481203703bSJeremy L Thompson CeedElemRestriction rstr; 1491203703bSJeremy L Thompson CeedBasis basis; 1501203703bSJeremy L Thompson 151ab747706SJeremy L Thompson CeedCall(CeedOperatorFieldGetData(input_fields[i], &field_name, &rstr, &basis, &vec)); 1521203703bSJeremy L Thompson CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec)); 153805fe78eSJeremy L Thompson } 1541203703bSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1556f8994e9SJeremy L Thompson const char *field_name; 1561203703bSJeremy L Thompson CeedVector vec; 1571203703bSJeremy L Thompson CeedElemRestriction rstr; 1581203703bSJeremy L Thompson CeedBasis basis; 1591203703bSJeremy L Thompson 160ab747706SJeremy L Thompson CeedCall(CeedOperatorFieldGetData(output_fields[i], &field_name, &rstr, &basis, &vec)); 1611203703bSJeremy L Thompson CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec)); 162805fe78eSJeremy L Thompson } 1632b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op->qf_assembled, &op_fallback->qf_assembled)); 1649e77b9c8SJeremy L Thompson // Cleanup 1652b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&qf_fallback)); 1662b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&dqf_fallback)); 1672b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&dqfT_fallback)); 168805fe78eSJeremy L Thompson } 1692b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetName(op_fallback, op->name)); 1702b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fallback)); 171b05f7e9fSJeremy L Thompson // Note: No ref-counting here so we don't get caught in a reference loop. 172b05f7e9fSJeremy L Thompson // The op holds the only reference to op_fallback and is responsible for deleting itself and op_fallback. 173805fe78eSJeremy L Thompson op->op_fallback = op_fallback; 174b05f7e9fSJeremy L Thompson op_fallback->op_fallback_parent = op; 175eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 176eaf62fffSJeremy L Thompson } 177eaf62fffSJeremy L Thompson 178eaf62fffSJeremy L Thompson /** 179eaf62fffSJeremy L Thompson @brief Core logic for assembling operator diagonal or point block diagonal 180eaf62fffSJeremy L Thompson 181ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble point block diagonal 182ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 183bd83916cSSebastian Grimberg @param[in] is_point_block Boolean flag to assemble diagonal or point block diagonal 184ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled diagonal 185eaf62fffSJeremy L Thompson 186eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 187eaf62fffSJeremy L Thompson 188eaf62fffSJeremy L Thompson @ref Developer 189eaf62fffSJeremy L Thompson **/ 190bd83916cSSebastian Grimberg static inline int CeedSingleOperatorAssembleAddDiagonal_Core(CeedOperator op, CeedRequest *request, const bool is_point_block, CeedVector assembled) { 191eaf62fffSJeremy L Thompson Ceed ceed; 192506b1a0cSSebastian Grimberg bool is_composite; 193506b1a0cSSebastian Grimberg 194506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetCeed(op, &ceed)); 195506b1a0cSSebastian Grimberg CeedCall(CeedOperatorIsComposite(op, &is_composite)); 196506b1a0cSSebastian Grimberg CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 197506b1a0cSSebastian Grimberg 198506b1a0cSSebastian Grimberg // Assemble QFunction 199506b1a0cSSebastian Grimberg CeedInt layout_qf[3]; 200437c7c90SJeremy L Thompson const CeedScalar *assembled_qf_array; 201c5f45aeaSJeremy L Thompson CeedVector assembled_qf = NULL; 202c5f45aeaSJeremy L Thompson CeedElemRestriction assembled_elem_rstr = NULL; 203437c7c90SJeremy L Thompson 204437c7c90SJeremy L Thompson CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request)); 20556c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf)); 206437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr)); 207437c7c90SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array)); 208eaf62fffSJeremy L Thompson 209ed9e99e6SJeremy L Thompson // Get assembly data 210437c7c90SJeremy L Thompson const CeedEvalMode **eval_modes_in, **eval_modes_out; 211506b1a0cSSebastian Grimberg CeedInt num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out; 212437c7c90SJeremy L Thompson CeedSize **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components; 213506b1a0cSSebastian Grimberg CeedBasis *active_bases_in, *active_bases_out; 214506b1a0cSSebastian Grimberg CeedElemRestriction *active_elem_rstrs_in, *active_elem_rstrs_out; 2151c66c397SJeremy L Thompson CeedOperatorAssemblyData data; 2161c66c397SJeremy L Thompson 217437c7c90SJeremy L Thompson CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data)); 218506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in, 219506b1a0cSSebastian Grimberg &num_active_bases_out, &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out, 220506b1a0cSSebastian Grimberg &num_output_components)); 221506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, NULL, NULL, &active_bases_out, NULL)); 222506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs_in, NULL, &active_elem_rstrs_out)); 223506b1a0cSSebastian Grimberg 224934a29f5SSebastian Grimberg // Loop over all active bases (find matching input/output pairs) 225934a29f5SSebastian Grimberg for (CeedInt b = 0; b < CeedIntMin(num_active_bases_in, num_active_bases_out); b++) { 226934a29f5SSebastian Grimberg CeedInt b_in, b_out, num_elem, num_nodes, num_qpts, num_comp; 2271c66c397SJeremy L Thompson bool has_eval_none = false; 2281c66c397SJeremy L Thompson CeedScalar *elem_diag_array, *identity = NULL; 2291c66c397SJeremy L Thompson CeedVector elem_diag; 2307c1dbaffSSebastian Grimberg CeedElemRestriction diag_elem_rstr; 2311c66c397SJeremy L Thompson 232934a29f5SSebastian Grimberg if (num_active_bases_in <= num_active_bases_out) { 233934a29f5SSebastian Grimberg b_in = b; 234934a29f5SSebastian Grimberg for (b_out = 0; b_out < num_active_bases_out; b_out++) { 235934a29f5SSebastian Grimberg if (active_bases_in[b_in] == active_bases_out[b_out]) { 236934a29f5SSebastian Grimberg break; 237934a29f5SSebastian Grimberg } 238934a29f5SSebastian Grimberg } 239934a29f5SSebastian Grimberg if (b_out == num_active_bases_out) { 240934a29f5SSebastian Grimberg continue; 241934a29f5SSebastian Grimberg } // No matching output basis found 242934a29f5SSebastian Grimberg } else { 243934a29f5SSebastian Grimberg b_out = b; 244934a29f5SSebastian Grimberg for (b_in = 0; b_in < num_active_bases_in; b_in++) { 245934a29f5SSebastian Grimberg if (active_bases_in[b_in] == active_bases_out[b_out]) { 246934a29f5SSebastian Grimberg break; 247934a29f5SSebastian Grimberg } 248934a29f5SSebastian Grimberg } 249934a29f5SSebastian Grimberg if (b_in == num_active_bases_in) { 250934a29f5SSebastian Grimberg continue; 251934a29f5SSebastian Grimberg } // No matching output basis found 252934a29f5SSebastian Grimberg } 253934a29f5SSebastian Grimberg CeedCheck(active_elem_rstrs_in[b_in] == active_elem_rstrs_out[b_out], ceed, CEED_ERROR_UNSUPPORTED, 254506b1a0cSSebastian Grimberg "Cannot assemble operator diagonal with different input and output active element restrictions"); 255506b1a0cSSebastian Grimberg 2561c66c397SJeremy L Thompson // Assemble point block diagonal restriction, if needed 257bd83916cSSebastian Grimberg if (is_point_block) { 258934a29f5SSebastian Grimberg CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstrs_in[b_in], &diag_elem_rstr)); 2597c1dbaffSSebastian Grimberg } else { 260934a29f5SSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnsignedCopy(active_elem_rstrs_in[b_in], &diag_elem_rstr)); 261eaf62fffSJeremy L Thompson } 262eaf62fffSJeremy L Thompson 263eaf62fffSJeremy L Thompson // Create diagonal vector 264437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag)); 265eaf62fffSJeremy L Thompson 266eaf62fffSJeremy L Thompson // Assemble element operator diagonals 2672b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(elem_diag, 0.0)); 2682b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array)); 269437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem)); 270934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumNodes(active_bases_in[b_in], &num_nodes)); 271934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumComponents(active_bases_in[b_in], &num_comp)); 272934a29f5SSebastian Grimberg if (active_bases_in[b_in] == CEED_BASIS_NONE) num_qpts = num_nodes; 273934a29f5SSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(active_bases_in[b_in], &num_qpts)); 274ed9e99e6SJeremy L Thompson 275352a5e7cSSebastian Grimberg // Construct identity matrix for basis if required 276934a29f5SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in[b_in]; i++) { 277934a29f5SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_in[b_in][i] == CEED_EVAL_NONE); 278ed9e99e6SJeremy L Thompson } 279934a29f5SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out[b_out]; i++) { 280934a29f5SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_out[b_out][i] == CEED_EVAL_NONE); 281ed9e99e6SJeremy L Thompson } 282ed9e99e6SJeremy L Thompson if (has_eval_none) { 2832b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_qpts * num_nodes, &identity)); 2842b730f8bSJeremy L Thompson for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0; 285eaf62fffSJeremy L Thompson } 286352a5e7cSSebastian Grimberg 287eaf62fffSJeremy L Thompson // Compute the diagonal of B^T D B 288eaf62fffSJeremy L Thompson // Each element 289b94338b9SJed Brown for (CeedSize e = 0; e < num_elem; e++) { 290eaf62fffSJeremy L Thompson // Each basis eval mode pair 291352a5e7cSSebastian Grimberg CeedInt d_out = 0, q_comp_out; 292352a5e7cSSebastian Grimberg CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE; 2931c66c397SJeremy L Thompson 294934a29f5SSebastian Grimberg for (CeedInt e_out = 0; e_out < num_eval_modes_out[b_out]; e_out++) { 2951c66c397SJeremy L Thompson CeedInt d_in = 0, q_comp_in; 296437c7c90SJeremy L Thompson const CeedScalar *B_t = NULL; 2971c66c397SJeremy L Thompson CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE; 2981c66c397SJeremy L Thompson 299934a29f5SSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(active_bases_out[b_out], eval_modes_out[b_out][e_out], identity, &B_t)); 300934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_out[b_out], eval_modes_out[b_out][e_out], &q_comp_out)); 301352a5e7cSSebastian Grimberg if (q_comp_out > 1) { 302934a29f5SSebastian Grimberg if (e_out == 0 || eval_modes_out[b_out][e_out] != eval_mode_out_prev) d_out = 0; 303352a5e7cSSebastian Grimberg else B_t = &B_t[(++d_out) * num_qpts * num_nodes]; 304352a5e7cSSebastian Grimberg } 305934a29f5SSebastian Grimberg eval_mode_out_prev = eval_modes_out[b_out][e_out]; 306352a5e7cSSebastian Grimberg 307934a29f5SSebastian Grimberg for (CeedInt e_in = 0; e_in < num_eval_modes_in[b_in]; e_in++) { 308437c7c90SJeremy L Thompson const CeedScalar *B = NULL; 3091c66c397SJeremy L Thompson 310934a29f5SSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(active_bases_in[b_in], eval_modes_in[b_in][e_in], identity, &B)); 311934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_in[b_in], eval_modes_in[b_in][e_in], &q_comp_in)); 312352a5e7cSSebastian Grimberg if (q_comp_in > 1) { 313934a29f5SSebastian Grimberg if (e_in == 0 || eval_modes_in[b_in][e_in] != eval_mode_in_prev) d_in = 0; 314352a5e7cSSebastian Grimberg else B = &B[(++d_in) * num_qpts * num_nodes]; 315352a5e7cSSebastian Grimberg } 316934a29f5SSebastian Grimberg eval_mode_in_prev = eval_modes_in[b_in][e_in]; 317352a5e7cSSebastian Grimberg 318eaf62fffSJeremy L Thompson // Each component 319506b1a0cSSebastian Grimberg for (CeedInt c_out = 0; c_out < num_comp; c_out++) { 320437c7c90SJeremy L Thompson // Each qpt/node pair 3212b730f8bSJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 322bd83916cSSebastian Grimberg if (is_point_block) { 323eaf62fffSJeremy L Thompson // Point Block Diagonal 324506b1a0cSSebastian Grimberg for (CeedInt c_in = 0; c_in < num_comp; c_in++) { 325934a29f5SSebastian Grimberg const CeedSize c_offset = 326934a29f5SSebastian Grimberg (eval_mode_offsets_in[b_in][e_in] + c_in) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out; 327506b1a0cSSebastian Grimberg const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]]; 3281c66c397SJeremy L Thompson 3292b730f8bSJeremy L Thompson for (CeedInt n = 0; n < num_nodes; n++) { 330506b1a0cSSebastian Grimberg elem_diag_array[((e * num_comp + c_out) * num_comp + c_in) * num_nodes + n] += 331437c7c90SJeremy L Thompson B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n]; 332eaf62fffSJeremy L Thompson } 3332b730f8bSJeremy L Thompson } 334eaf62fffSJeremy L Thompson } else { 335eaf62fffSJeremy L Thompson // Diagonal Only 336934a29f5SSebastian Grimberg const CeedInt c_offset = 337934a29f5SSebastian Grimberg (eval_mode_offsets_in[b_in][e_in] + c_out) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out; 338506b1a0cSSebastian Grimberg const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]]; 3391c66c397SJeremy L Thompson 3402b730f8bSJeremy L Thompson for (CeedInt n = 0; n < num_nodes; n++) { 341506b1a0cSSebastian 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]; 342eaf62fffSJeremy L Thompson } 343eaf62fffSJeremy L Thompson } 344eaf62fffSJeremy L Thompson } 345eaf62fffSJeremy L Thompson } 3462b730f8bSJeremy L Thompson } 3472b730f8bSJeremy L Thompson } 3482b730f8bSJeremy L Thompson } 3492b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array)); 350eaf62fffSJeremy L Thompson 351eaf62fffSJeremy L Thompson // Assemble local operator diagonal 3527c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionApply(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request)); 353eaf62fffSJeremy L Thompson 354eaf62fffSJeremy L Thompson // Cleanup 3557c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr)); 3562b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&elem_diag)); 3572b730f8bSJeremy L Thompson CeedCall(CeedFree(&identity)); 358437c7c90SJeremy L Thompson } 359437c7c90SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 360437c7c90SJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled_qf)); 361eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 362eaf62fffSJeremy L Thompson } 363eaf62fffSJeremy L Thompson 364eaf62fffSJeremy L Thompson /** 365eaf62fffSJeremy L Thompson @brief Core logic for assembling composite operator diagonal 366eaf62fffSJeremy L Thompson 367ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble point block diagonal 368ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 369bd83916cSSebastian Grimberg @param[in] is_point_block Boolean flag to assemble diagonal or point block diagonal 370ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled diagonal 371eaf62fffSJeremy L Thompson 372eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 373eaf62fffSJeremy L Thompson 374eaf62fffSJeremy L Thompson @ref Developer 375eaf62fffSJeremy L Thompson **/ 376bd83916cSSebastian Grimberg static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block, 377eaf62fffSJeremy L Thompson CeedVector assembled) { 378eaf62fffSJeremy L Thompson CeedInt num_sub; 379eaf62fffSJeremy L Thompson CeedOperator *suboperators; 3801c66c397SJeremy L Thompson 381c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); 382c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators)); 383eaf62fffSJeremy L Thompson for (CeedInt i = 0; i < num_sub; i++) { 384bd83916cSSebastian Grimberg if (is_point_block) { 3852b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request)); 3866aa95790SJeremy L Thompson } else { 3872b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request)); 3886aa95790SJeremy L Thompson } 389eaf62fffSJeremy L Thompson } 390eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 391eaf62fffSJeremy L Thompson } 392eaf62fffSJeremy L Thompson 393eaf62fffSJeremy L Thompson /** 394ca94c3ddSJeremy L Thompson @brief Build nonzero pattern for non-composite CeedOperator`. 395eaf62fffSJeremy L Thompson 396ca94c3ddSJeremy L Thompson Users should generally use @ref CeedOperatorLinearAssembleSymbolic(). 397eaf62fffSJeremy L Thompson 398ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble nonzero pattern 399eaf62fffSJeremy L Thompson @param[in] offset Offset for number of entries 400eaf62fffSJeremy L Thompson @param[out] rows Row number for each entry 401eaf62fffSJeremy L Thompson @param[out] cols Column number for each entry 402eaf62fffSJeremy L Thompson 403eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 404eaf62fffSJeremy L Thompson 405eaf62fffSJeremy L Thompson @ref Developer 406eaf62fffSJeremy L Thompson **/ 4072b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) { 408f3d47e36SJeremy L Thompson Ceed ceed; 409f3d47e36SJeremy L Thompson bool is_composite; 410*81670346SSebastian Grimberg CeedSize num_nodes_in, num_nodes_out, local_num_entries, count = 0; 411506b1a0cSSebastian Grimberg CeedInt num_elem_in, elem_size_in, num_comp_in, layout_er_in[3]; 412*81670346SSebastian Grimberg CeedInt num_elem_out, elem_size_out, num_comp_out, layout_er_out[3]; 4131c66c397SJeremy L Thompson CeedScalar *array; 414506b1a0cSSebastian Grimberg const CeedScalar *elem_dof_a_in, *elem_dof_a_out; 415506b1a0cSSebastian Grimberg CeedVector index_vec_in, index_vec_out, elem_dof_in, elem_dof_out; 416506b1a0cSSebastian Grimberg CeedElemRestriction elem_rstr_in, elem_rstr_out, index_elem_rstr_in, index_elem_rstr_out; 4171c66c397SJeremy L Thompson 418f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 419f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 4206574a04fSJeremy L Thompson CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 421eaf62fffSJeremy L Thompson 422506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes_in, &num_nodes_out)); 423506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out)); 424506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in)); 425506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in)); 426506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in)); 42756c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(elem_rstr_in, layout_er_in)); 428eaf62fffSJeremy L Thompson 429506b1a0cSSebastian Grimberg // Determine elem_dof relation for input 430506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_nodes_in, &index_vec_in)); 431506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayWrite(index_vec_in, CEED_MEM_HOST, &array)); 432506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_nodes_in; i++) array[i] = i; 433506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArray(index_vec_in, &array)); 434506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_elem_in * elem_size_in * num_comp_in, &elem_dof_in)); 435506b1a0cSSebastian Grimberg CeedCall(CeedVectorSetValue(elem_dof_in, 0.0)); 436506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_in, &index_elem_rstr_in)); 437506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionApply(index_elem_rstr_in, CEED_NOTRANSPOSE, index_vec_in, elem_dof_in, CEED_REQUEST_IMMEDIATE)); 438506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayRead(elem_dof_in, CEED_MEM_HOST, &elem_dof_a_in)); 439506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&index_vec_in)); 440506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_in)); 441506b1a0cSSebastian Grimberg 442506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 443506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out)); 444506b1a0cSSebastian Grimberg CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, 445506b1a0cSSebastian Grimberg "Active input and output operator restrictions must have the same number of elements"); 446506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out)); 447506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out)); 44856c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(elem_rstr_out, layout_er_out)); 449506b1a0cSSebastian Grimberg 450506b1a0cSSebastian Grimberg // Determine elem_dof relation for output 451506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_nodes_out, &index_vec_out)); 452506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayWrite(index_vec_out, CEED_MEM_HOST, &array)); 453506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_nodes_out; i++) array[i] = i; 454506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArray(index_vec_out, &array)); 455506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_elem_out * elem_size_out * num_comp_out, &elem_dof_out)); 456506b1a0cSSebastian Grimberg CeedCall(CeedVectorSetValue(elem_dof_out, 0.0)); 457506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_out, &index_elem_rstr_out)); 458506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionApply(index_elem_rstr_out, CEED_NOTRANSPOSE, index_vec_out, elem_dof_out, CEED_REQUEST_IMMEDIATE)); 459506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayRead(elem_dof_out, CEED_MEM_HOST, &elem_dof_a_out)); 460506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&index_vec_out)); 461506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_out)); 462506b1a0cSSebastian Grimberg } else { 463506b1a0cSSebastian Grimberg num_elem_out = num_elem_in; 464506b1a0cSSebastian Grimberg elem_size_out = elem_size_in; 465506b1a0cSSebastian Grimberg num_comp_out = num_comp_in; 466506b1a0cSSebastian Grimberg layout_er_out[0] = layout_er_in[0]; 467506b1a0cSSebastian Grimberg layout_er_out[1] = layout_er_in[1]; 468506b1a0cSSebastian Grimberg layout_er_out[2] = layout_er_in[2]; 469506b1a0cSSebastian Grimberg elem_dof_a_out = elem_dof_a_in; 470506b1a0cSSebastian Grimberg } 471506b1a0cSSebastian Grimberg local_num_entries = elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in; 472eaf62fffSJeremy L Thompson 473eaf62fffSJeremy L Thompson // Determine i, j locations for element matrices 474506b1a0cSSebastian Grimberg for (CeedInt e = 0; e < num_elem_in; e++) { 475506b1a0cSSebastian Grimberg for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) { 476506b1a0cSSebastian Grimberg for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) { 477506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 478506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 479506b1a0cSSebastian Grimberg const CeedInt elem_dof_index_row = i * layout_er_out[0] + comp_out * layout_er_out[1] + e * layout_er_out[2]; 480506b1a0cSSebastian Grimberg const CeedInt elem_dof_index_col = j * layout_er_in[0] + comp_in * layout_er_in[1] + e * layout_er_in[2]; 481506b1a0cSSebastian Grimberg const CeedInt row = elem_dof_a_out[elem_dof_index_row]; 482506b1a0cSSebastian Grimberg const CeedInt col = elem_dof_a_in[elem_dof_index_col]; 483eaf62fffSJeremy L Thompson 484eaf62fffSJeremy L Thompson rows[offset + count] = row; 485eaf62fffSJeremy L Thompson cols[offset + count] = col; 486eaf62fffSJeremy L Thompson count++; 487eaf62fffSJeremy L Thompson } 488eaf62fffSJeremy L Thompson } 489eaf62fffSJeremy L Thompson } 490eaf62fffSJeremy L Thompson } 491eaf62fffSJeremy L Thompson } 4926574a04fSJeremy L Thompson CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing assembled entries"); 493506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArrayRead(elem_dof_in, &elem_dof_a_in)); 494506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&elem_dof_in)); 495506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 496506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArrayRead(elem_dof_out, &elem_dof_a_out)); 497506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&elem_dof_out)); 498506b1a0cSSebastian Grimberg } 499eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 500eaf62fffSJeremy L Thompson } 501eaf62fffSJeremy L Thompson 502eaf62fffSJeremy L Thompson /** 503ca94c3ddSJeremy L Thompson @brief Assemble nonzero entries for non-composite `CeedOperator`. 504eaf62fffSJeremy L Thompson 505ca94c3ddSJeremy L Thompson Users should generally use @ref CeedOperatorLinearAssemble(). 506eaf62fffSJeremy L Thompson 507ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 508ea61e9acSJeremy L Thompson @param[in] offset Offset for number of entries 509eaf62fffSJeremy L Thompson @param[out] values Values to assemble into matrix 510eaf62fffSJeremy L Thompson 511eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 512eaf62fffSJeremy L Thompson 513eaf62fffSJeremy L Thompson @ref Developer 514eaf62fffSJeremy L Thompson **/ 5152b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) { 516f3d47e36SJeremy L Thompson Ceed ceed; 517f3d47e36SJeremy L Thompson bool is_composite; 5181c66c397SJeremy L Thompson 519f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 520f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 5216574a04fSJeremy L Thompson CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 522f3d47e36SJeremy L Thompson 523f3d47e36SJeremy L Thompson // Early exit for empty operator 524f3d47e36SJeremy L Thompson { 525f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 526f3d47e36SJeremy L Thompson 527f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 528f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 529f3d47e36SJeremy L Thompson } 530eaf62fffSJeremy L Thompson 531cefa2673SJeremy L Thompson if (op->LinearAssembleSingle) { 532cefa2673SJeremy L Thompson // Backend version 5332b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleSingle(op, offset, values)); 534cefa2673SJeremy L Thompson return CEED_ERROR_SUCCESS; 535cefa2673SJeremy L Thompson } else { 536cefa2673SJeremy L Thompson // Operator fallback 537cefa2673SJeremy L Thompson CeedOperator op_fallback; 538cefa2673SJeremy L Thompson 5392b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 540cefa2673SJeremy L Thompson if (op_fallback) { 5412b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values)); 542cefa2673SJeremy L Thompson return CEED_ERROR_SUCCESS; 543cefa2673SJeremy L Thompson } 544cefa2673SJeremy L Thompson } 545cefa2673SJeremy L Thompson 546eaf62fffSJeremy L Thompson // Assemble QFunction 547506b1a0cSSebastian Grimberg CeedInt layout_qf[3]; 5481c66c397SJeremy L Thompson const CeedScalar *assembled_qf_array; 549c5f45aeaSJeremy L Thompson CeedVector assembled_qf = NULL; 550506b1a0cSSebastian Grimberg CeedElemRestriction assembled_elem_rstr = NULL; 551eaf62fffSJeremy L Thompson 552506b1a0cSSebastian Grimberg CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, CEED_REQUEST_IMMEDIATE)); 55356c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf)); 554506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr)); 555506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array)); 556eaf62fffSJeremy L Thompson 557ed9e99e6SJeremy L Thompson // Get assembly data 558506b1a0cSSebastian Grimberg CeedInt num_elem_in, elem_size_in, num_comp_in, num_qpts_in; 559*81670346SSebastian Grimberg CeedInt num_elem_out, elem_size_out, num_comp_out, num_qpts_out; 560*81670346SSebastian Grimberg CeedSize local_num_entries, count = 0; 561506b1a0cSSebastian Grimberg const CeedEvalMode **eval_modes_in, **eval_modes_out; 562506b1a0cSSebastian Grimberg CeedInt num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out; 563506b1a0cSSebastian Grimberg CeedBasis *active_bases_in, *active_bases_out, basis_in, basis_out; 564506b1a0cSSebastian Grimberg const CeedScalar **B_mats_in, **B_mats_out, *B_mat_in, *B_mat_out; 565506b1a0cSSebastian Grimberg CeedElemRestriction elem_rstr_in, elem_rstr_out; 566506b1a0cSSebastian Grimberg CeedRestrictionType elem_rstr_type_in, elem_rstr_type_out; 567506b1a0cSSebastian Grimberg const bool *elem_rstr_orients_in = NULL, *elem_rstr_orients_out = NULL; 568506b1a0cSSebastian Grimberg const CeedInt8 *elem_rstr_curl_orients_in = NULL, *elem_rstr_curl_orients_out = NULL; 569506b1a0cSSebastian Grimberg CeedOperatorAssemblyData data; 570eaf62fffSJeremy L Thompson 571506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data)); 572506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, NULL, &num_active_bases_out, 573506b1a0cSSebastian Grimberg &num_eval_modes_out, &eval_modes_out, NULL, NULL)); 574506b1a0cSSebastian Grimberg 575506b1a0cSSebastian Grimberg CeedCheck(num_active_bases_in == num_active_bases_out && num_active_bases_in == 1, ceed, CEED_ERROR_UNSUPPORTED, 576506b1a0cSSebastian Grimberg "Cannot assemble operator with multiple active bases"); 5776574a04fSJeremy 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"); 578eaf62fffSJeremy L Thompson 579506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, &B_mats_in, NULL, &active_bases_out, &B_mats_out)); 580506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out)); 581506b1a0cSSebastian Grimberg basis_in = active_bases_in[0]; 582506b1a0cSSebastian Grimberg basis_out = active_bases_out[0]; 583506b1a0cSSebastian Grimberg B_mat_in = B_mats_in[0]; 584506b1a0cSSebastian Grimberg B_mat_out = B_mats_out[0]; 585eaf62fffSJeremy L Thompson 586506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in)); 587506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in)); 588506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in)); 589506b1a0cSSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in; 590506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in)); 591506b1a0cSSebastian Grimberg 592506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetType(elem_rstr_in, &elem_rstr_type_in)); 593506b1a0cSSebastian Grimberg if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) { 594506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_orients_in)); 595506b1a0cSSebastian Grimberg } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 596506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_curl_orients_in)); 5977c1dbaffSSebastian Grimberg } 5987c1dbaffSSebastian Grimberg 599506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 600506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out)); 601506b1a0cSSebastian Grimberg CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, 602506b1a0cSSebastian Grimberg "Active input and output operator restrictions must have the same number of elements"); 603506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out)); 604506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out)); 605506b1a0cSSebastian Grimberg if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out; 606506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out)); 607506b1a0cSSebastian Grimberg CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED, 608506b1a0cSSebastian Grimberg "Active input and output bases must have the same number of quadrature points"); 609eaf62fffSJeremy L Thompson 610506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetType(elem_rstr_out, &elem_rstr_type_out)); 611506b1a0cSSebastian Grimberg if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) { 612506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_orients_out)); 613506b1a0cSSebastian Grimberg } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 614506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_curl_orients_out)); 615506b1a0cSSebastian Grimberg } 616506b1a0cSSebastian Grimberg } else { 617506b1a0cSSebastian Grimberg num_elem_out = num_elem_in; 618506b1a0cSSebastian Grimberg elem_size_out = elem_size_in; 619506b1a0cSSebastian Grimberg num_comp_out = num_comp_in; 620506b1a0cSSebastian Grimberg num_qpts_out = num_qpts_in; 621506b1a0cSSebastian Grimberg 622506b1a0cSSebastian Grimberg elem_rstr_orients_out = elem_rstr_orients_in; 623506b1a0cSSebastian Grimberg elem_rstr_curl_orients_out = elem_rstr_curl_orients_in; 624506b1a0cSSebastian Grimberg } 625506b1a0cSSebastian Grimberg local_num_entries = elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in; 626506b1a0cSSebastian Grimberg 627506b1a0cSSebastian Grimberg // Loop over elements and put in data structure 6287c1dbaffSSebastian Grimberg // We store B_mat_in, B_mat_out, BTD, elem_mat in row-major order 6290459ebd3SSebastian Grimberg CeedTensorContract contract; 630123d890dSSebastian Grimberg CeedScalar *vals, *BTD_mat = NULL, *elem_mat = NULL, *elem_mat_b = NULL; 631506b1a0cSSebastian Grimberg 632c22497adSSebastian Grimberg CeedCall(CeedBasisGetTensorContract(basis_in, &contract)); 633123d890dSSebastian Grimberg CeedCall(CeedCalloc(elem_size_out * num_qpts_in * num_eval_modes_in[0], &BTD_mat)); 634123d890dSSebastian Grimberg CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat)); 635506b1a0cSSebastian Grimberg if (elem_rstr_curl_orients_in || elem_rstr_curl_orients_out) CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat_b)); 6361c66c397SJeremy L Thompson 63728ec399dSJeremy L Thompson CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals)); 638506b1a0cSSebastian Grimberg for (CeedSize e = 0; e < num_elem_in; e++) { 639506b1a0cSSebastian Grimberg for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) { 640506b1a0cSSebastian Grimberg for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) { 641ed9e99e6SJeremy L Thompson // Compute B^T*D 642506b1a0cSSebastian Grimberg for (CeedSize n = 0; n < elem_size_out; n++) { 643506b1a0cSSebastian Grimberg for (CeedSize q = 0; q < num_qpts_in; q++) { 644437c7c90SJeremy L Thompson for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) { 645506b1a0cSSebastian Grimberg const CeedSize btd_index = n * (num_qpts_in * num_eval_modes_in[0]) + q * num_eval_modes_in[0] + e_in; 646067fd99fSJeremy L Thompson CeedScalar sum = 0.0; 6471c66c397SJeremy L Thompson 648437c7c90SJeremy L Thompson for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) { 649506b1a0cSSebastian Grimberg const CeedSize b_out_index = (q * num_eval_modes_out[0] + e_out) * elem_size_out + n; 650506b1a0cSSebastian 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; 651b94338b9SJed Brown const CeedSize qf_index = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2]; 6521c66c397SJeremy L Thompson 653067fd99fSJeremy L Thompson sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index]; 654eaf62fffSJeremy L Thompson } 655067fd99fSJeremy L Thompson BTD_mat[btd_index] = sum; 656ed9e99e6SJeremy L Thompson } 657ed9e99e6SJeremy L Thompson } 658eaf62fffSJeremy L Thompson } 6597c1dbaffSSebastian Grimberg 6607c1dbaffSSebastian Grimberg // Form element matrix itself (for each block component) 661e4065a52SSebastian Grimberg if (contract) { 6620459ebd3SSebastian Grimberg CeedCall(CeedTensorContractApply(contract, 1, num_qpts_in * num_eval_modes_in[0], elem_size_in, elem_size_out, BTD_mat, CEED_NOTRANSPOSE, 6630459ebd3SSebastian Grimberg false, B_mat_in, elem_mat)); 664e4065a52SSebastian Grimberg } else { 665e4065a52SSebastian 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])); 666e4065a52SSebastian Grimberg } 667eaf62fffSJeremy L Thompson 6687c1dbaffSSebastian Grimberg // Transform the element matrix if required 669506b1a0cSSebastian Grimberg if (elem_rstr_orients_out) { 670506b1a0cSSebastian Grimberg const bool *elem_orients = &elem_rstr_orients_out[e * elem_size_out]; 6711c66c397SJeremy L Thompson 672506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 673506b1a0cSSebastian Grimberg const double orient = elem_orients[i] ? -1.0 : 1.0; 674506b1a0cSSebastian Grimberg 675506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 676506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] *= orient; 6777c1dbaffSSebastian Grimberg } 6787c1dbaffSSebastian Grimberg } 679506b1a0cSSebastian Grimberg } else if (elem_rstr_curl_orients_out) { 680506b1a0cSSebastian Grimberg const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_out[e * 3 * elem_size_out]; 6811c66c397SJeremy L Thompson 6827c1dbaffSSebastian Grimberg // T^T*(B^T*D*B) 683506b1a0cSSebastian Grimberg memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar)); 684506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 685506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 686506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * i + 1] + 687506b1a0cSSebastian Grimberg (i > 0 ? elem_mat_b[(i - 1) * elem_size_in + j] * elem_curl_orients[3 * i - 1] : 0.0) + 688506b1a0cSSebastian Grimberg (i < elem_size_out - 1 ? elem_mat_b[(i + 1) * elem_size_in + j] * elem_curl_orients[3 * i + 3] : 0.0); 6897c1dbaffSSebastian Grimberg } 6907c1dbaffSSebastian Grimberg } 691506b1a0cSSebastian Grimberg } 692506b1a0cSSebastian Grimberg if (elem_rstr_orients_in) { 693506b1a0cSSebastian Grimberg const bool *elem_orients = &elem_rstr_orients_in[e * elem_size_in]; 694506b1a0cSSebastian Grimberg 695506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 696506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 697506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] *= elem_orients[j] ? -1.0 : 1.0; 698506b1a0cSSebastian Grimberg } 699506b1a0cSSebastian Grimberg } 700506b1a0cSSebastian Grimberg } else if (elem_rstr_curl_orients_in) { 701506b1a0cSSebastian Grimberg const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_in[e * 3 * elem_size_in]; 702506b1a0cSSebastian Grimberg 703506b1a0cSSebastian Grimberg // (B^T*D*B)*T 704506b1a0cSSebastian Grimberg memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar)); 705506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 706506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 707506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * j + 1] + 708506b1a0cSSebastian Grimberg (j > 0 ? elem_mat_b[i * elem_size_in + j - 1] * elem_curl_orients[3 * j - 1] : 0.0) + 709506b1a0cSSebastian Grimberg (j < elem_size_in - 1 ? elem_mat_b[i * elem_size_in + j + 1] * elem_curl_orients[3 * j + 3] : 0.0); 7107c1dbaffSSebastian Grimberg } 7117c1dbaffSSebastian Grimberg } 7127c1dbaffSSebastian Grimberg } 7137c1dbaffSSebastian Grimberg 7147c1dbaffSSebastian Grimberg // Put element matrix in coordinate data structure 715506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 716506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 717506b1a0cSSebastian Grimberg vals[offset + count] = elem_mat[i * elem_size_in + j]; 718eaf62fffSJeremy L Thompson count++; 719eaf62fffSJeremy L Thompson } 720eaf62fffSJeremy L Thompson } 721eaf62fffSJeremy L Thompson } 722eaf62fffSJeremy L Thompson } 723eaf62fffSJeremy L Thompson } 7246574a04fSJeremy L Thompson CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing entries"); 7252b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(values, &vals)); 726eaf62fffSJeremy L Thompson 727506b1a0cSSebastian Grimberg // Cleanup 728123d890dSSebastian Grimberg CeedCall(CeedFree(&BTD_mat)); 729123d890dSSebastian Grimberg CeedCall(CeedFree(&elem_mat)); 730506b1a0cSSebastian Grimberg CeedCall(CeedFree(&elem_mat_b)); 731506b1a0cSSebastian Grimberg if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) { 732506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_in, &elem_rstr_orients_in)); 733506b1a0cSSebastian Grimberg } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 734506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_in, &elem_rstr_curl_orients_in)); 735506b1a0cSSebastian Grimberg } 736506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 737506b1a0cSSebastian Grimberg if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) { 738506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_out, &elem_rstr_orients_out)); 739506b1a0cSSebastian Grimberg } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 740506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_out, &elem_rstr_curl_orients_out)); 741506b1a0cSSebastian Grimberg } 742506b1a0cSSebastian Grimberg } 7432b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 7442b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled_qf)); 745eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 746eaf62fffSJeremy L Thompson } 747eaf62fffSJeremy L Thompson 748eaf62fffSJeremy L Thompson /** 749ca94c3ddSJeremy L Thompson @brief Count number of entries for assembled `CeedOperator` 750eaf62fffSJeremy L Thompson 751ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 752eaf62fffSJeremy L Thompson @param[out] num_entries Number of entries in assembled representation 753eaf62fffSJeremy L Thompson 754eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 755eaf62fffSJeremy L Thompson 756eaf62fffSJeremy L Thompson @ref Utility 757eaf62fffSJeremy L Thompson **/ 758b94338b9SJed Brown static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedSize *num_entries) { 759b275c451SJeremy L Thompson bool is_composite; 760506b1a0cSSebastian Grimberg CeedInt num_elem_in, elem_size_in, num_comp_in, num_elem_out, elem_size_out, num_comp_out; 7611203703bSJeremy L Thompson Ceed ceed; 762506b1a0cSSebastian Grimberg CeedElemRestriction rstr_in, rstr_out; 763eaf62fffSJeremy L Thompson 7641203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 765b275c451SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 7661203703bSJeremy L Thompson CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 767506b1a0cSSebastian Grimberg 768506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out)); 769506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in)); 770506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in)); 771506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in)); 772506b1a0cSSebastian Grimberg if (rstr_in != rstr_out) { 773506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out)); 7741203703bSJeremy L Thompson CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, 775506b1a0cSSebastian Grimberg "Active input and output operator restrictions must have the same number of elements"); 776506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out)); 777506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out)); 778506b1a0cSSebastian Grimberg } else { 779506b1a0cSSebastian Grimberg num_elem_out = num_elem_in; 780506b1a0cSSebastian Grimberg elem_size_out = elem_size_in; 781506b1a0cSSebastian Grimberg num_comp_out = num_comp_in; 782506b1a0cSSebastian Grimberg } 783506b1a0cSSebastian Grimberg *num_entries = (CeedSize)elem_size_in * num_comp_in * elem_size_out * num_comp_out * num_elem_in; 784eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 785eaf62fffSJeremy L Thompson } 786eaf62fffSJeremy L Thompson 787eaf62fffSJeremy L Thompson /** 788ca94c3ddSJeremy L Thompson @brief Common code for creating a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` 789eaf62fffSJeremy L Thompson 790ca94c3ddSJeremy L Thompson @param[in] op_fine Fine grid `CeedOperator` 791ca94c3ddSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator` 792ca94c3ddSJeremy L Thompson @param[in] rstr_coarse Coarse grid `CeedElemRestriction` 793ca94c3ddSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector `CeedBasis` 794ca94c3ddSJeremy L Thompson @param[in] basis_c_to_f `CeedBasis` for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction operators 795ca94c3ddSJeremy L Thompson @param[out] op_coarse Coarse grid `CeedOperator` 796ca94c3ddSJeremy L Thompson @param[out] op_prolong Coarse to fine `CeedOperator`, or `NULL` 797ca94c3ddSJeremy L Thompson @param[out] op_restrict Fine to coarse `CeedOperator`, or `NULL` 798eaf62fffSJeremy L Thompson 799eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 800eaf62fffSJeremy L Thompson 801eaf62fffSJeremy L Thompson @ref Developer 802eaf62fffSJeremy L Thompson **/ 8032b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 8047758292fSSebastian Grimberg CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) { 8051c66c397SJeremy L Thompson bool is_composite; 806eaf62fffSJeremy L Thompson Ceed ceed; 8071203703bSJeremy L Thompson CeedInt num_comp, num_input_fields, num_output_fields; 80885bb9dcfSJeremy L Thompson CeedVector mult_vec = NULL; 8091c66c397SJeremy L Thompson CeedElemRestriction rstr_p_mult_fine = NULL, rstr_fine = NULL; 8101203703bSJeremy L Thompson CeedOperatorField *input_fields, *output_fields; 8111c66c397SJeremy L Thompson 8122b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op_fine, &ceed)); 813eaf62fffSJeremy L Thompson 814eaf62fffSJeremy L Thompson // Check for composite operator 8152b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op_fine, &is_composite)); 8166574a04fSJeremy L Thompson CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported"); 817eaf62fffSJeremy L Thompson 818eaf62fffSJeremy L Thompson // Coarse Grid 8192b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse)); 8201203703bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op_fine, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 821eaf62fffSJeremy L Thompson // -- Clone input fields 8221203703bSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 8236f8994e9SJeremy L Thompson const char *field_name; 8241203703bSJeremy L Thompson CeedVector vec; 8251203703bSJeremy L Thompson CeedElemRestriction rstr; 8261203703bSJeremy L Thompson CeedBasis basis; 8271203703bSJeremy L Thompson 8281203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetName(input_fields[i], &field_name)); 8291203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(input_fields[i], &vec)); 8301203703bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 8311203703bSJeremy L Thompson rstr = rstr_coarse; 8321203703bSJeremy L Thompson basis = basis_coarse; 8331203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_fine)); 834eaf62fffSJeremy L Thompson } else { 8351203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr)); 8361203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis)); 837eaf62fffSJeremy L Thompson } 8381203703bSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec)); 839eaf62fffSJeremy L Thompson } 840eaf62fffSJeremy L Thompson // -- Clone output fields 8411203703bSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 8426f8994e9SJeremy L Thompson const char *field_name; 8431203703bSJeremy L Thompson CeedVector vec; 8441203703bSJeremy L Thompson CeedElemRestriction rstr; 8451203703bSJeremy L Thompson CeedBasis basis; 8461203703bSJeremy L Thompson 8471203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetName(output_fields[i], &field_name)); 8481203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec)); 8491203703bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 8501203703bSJeremy L Thompson rstr = rstr_coarse; 8511203703bSJeremy L Thompson basis = basis_coarse; 8521203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_fine)); 853eaf62fffSJeremy L Thompson } else { 8541203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr)); 8551203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis)); 856eaf62fffSJeremy L Thompson } 8571203703bSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec)); 858eaf62fffSJeremy L Thompson } 859af99e877SJeremy L Thompson // -- Clone QFunctionAssemblyData 8602b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataReferenceCopy(op_fine->qf_assembled, &(*op_coarse)->qf_assembled)); 861eaf62fffSJeremy L Thompson 862eaf62fffSJeremy L Thompson // Multiplicity vector 8637758292fSSebastian Grimberg if (op_restrict || op_prolong) { 86485bb9dcfSJeremy L Thompson CeedVector mult_e_vec; 8651c66c397SJeremy L Thompson CeedRestrictionType rstr_type; 86685bb9dcfSJeremy L Thompson 8677c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionGetType(rstr_fine, &rstr_type)); 8687c1dbaffSSebastian Grimberg CeedCheck(rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_UNSUPPORTED, 8697c1dbaffSSebastian Grimberg "Element restrictions created with CeedElemRestrictionCreateCurlOriented are not supported"); 8706574a04fSJeremy L Thompson CeedCheck(p_mult_fine, ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector"); 8717c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnsignedCopy(rstr_fine, &rstr_p_mult_fine)); 8722b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec)); 8732b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(mult_e_vec, 0.0)); 874c17ec2beSJeremy L Thompson CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE)); 8752b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(mult_vec, 0.0)); 876c17ec2beSJeremy L Thompson CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE)); 8772b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&mult_e_vec)); 8782b730f8bSJeremy L Thompson CeedCall(CeedVectorReciprocal(mult_vec)); 87985bb9dcfSJeremy L Thompson } 880eaf62fffSJeremy L Thompson 881addd79feSZach Atkins // Clone name 882addd79feSZach Atkins bool has_name = op_fine->name; 883addd79feSZach Atkins size_t name_len = op_fine->name ? strlen(op_fine->name) : 0; 884addd79feSZach Atkins CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name)); 885addd79feSZach Atkins 8867758292fSSebastian Grimberg // Check that coarse to fine basis is provided if prolong/restrict operators are requested 8877758292fSSebastian Grimberg CeedCheck(basis_c_to_f || (!op_restrict && !op_prolong), ceed, CEED_ERROR_INCOMPATIBLE, 8886574a04fSJeremy L Thompson "Prolongation or restriction operator creation requires coarse-to-fine basis"); 88983d6adf3SZach Atkins 89085bb9dcfSJeremy L Thompson // Restriction/Prolongation Operators 8912b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp)); 892addd79feSZach Atkins 893addd79feSZach Atkins // Restriction 8947758292fSSebastian Grimberg if (op_restrict) { 895eaf62fffSJeremy L Thompson CeedInt *num_comp_r_data; 89685bb9dcfSJeremy L Thompson CeedQFunctionContext ctx_r; 8977758292fSSebastian Grimberg CeedQFunction qf_restrict; 89885bb9dcfSJeremy L Thompson 8997758292fSSebastian Grimberg CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict)); 9002b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &num_comp_r_data)); 901eaf62fffSJeremy L Thompson num_comp_r_data[0] = num_comp; 9022b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r)); 9032b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data)); 9047758292fSSebastian Grimberg CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r)); 9052b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(&ctx_r)); 9067758292fSSebastian Grimberg CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE)); 9077758292fSSebastian Grimberg CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE)); 9087758292fSSebastian Grimberg CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP)); 9097758292fSSebastian Grimberg CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp)); 910eaf62fffSJeremy L Thompson 9117758292fSSebastian Grimberg CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict)); 9127758292fSSebastian Grimberg CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE)); 9137758292fSSebastian Grimberg CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec)); 9147758292fSSebastian Grimberg CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE)); 915eaf62fffSJeremy L Thompson 916addd79feSZach Atkins // Set name 917addd79feSZach Atkins char *restriction_name; 9181c66c397SJeremy L Thompson 919addd79feSZach Atkins CeedCall(CeedCalloc(17 + name_len, &restriction_name)); 920addd79feSZach Atkins sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : ""); 9217758292fSSebastian Grimberg CeedCall(CeedOperatorSetName(*op_restrict, restriction_name)); 922addd79feSZach Atkins CeedCall(CeedFree(&restriction_name)); 923addd79feSZach Atkins 924addd79feSZach Atkins // Check 9257758292fSSebastian Grimberg CeedCall(CeedOperatorCheckReady(*op_restrict)); 926addd79feSZach Atkins 927addd79feSZach Atkins // Cleanup 9287758292fSSebastian Grimberg CeedCall(CeedQFunctionDestroy(&qf_restrict)); 929addd79feSZach Atkins } 930addd79feSZach Atkins 931eaf62fffSJeremy L Thompson // Prolongation 932addd79feSZach Atkins if (op_prolong) { 933eaf62fffSJeremy L Thompson CeedInt *num_comp_p_data; 93485bb9dcfSJeremy L Thompson CeedQFunctionContext ctx_p; 9351c66c397SJeremy L Thompson CeedQFunction qf_prolong; 93685bb9dcfSJeremy L Thompson 93785bb9dcfSJeremy L Thompson CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong)); 9382b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &num_comp_p_data)); 939eaf62fffSJeremy L Thompson num_comp_p_data[0] = num_comp; 9402b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p)); 9412b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data)); 9422b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p)); 9432b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(&ctx_p)); 9442b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP)); 9452b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE)); 9462b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE)); 9472b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp)); 948eaf62fffSJeremy L Thompson 9492b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong)); 9502b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE)); 951356036faSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec)); 952356036faSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE)); 953eaf62fffSJeremy L Thompson 954addd79feSZach Atkins // Set name 955ea6b5821SJeremy L Thompson char *prolongation_name; 9561c66c397SJeremy L Thompson 9572b730f8bSJeremy L Thompson CeedCall(CeedCalloc(18 + name_len, &prolongation_name)); 9582b730f8bSJeremy L Thompson sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : ""); 9592b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name)); 9602b730f8bSJeremy L Thompson CeedCall(CeedFree(&prolongation_name)); 961addd79feSZach Atkins 962addd79feSZach Atkins // Check 963addd79feSZach Atkins CeedCall(CeedOperatorCheckReady(*op_prolong)); 964addd79feSZach Atkins 965addd79feSZach Atkins // Cleanup 966addd79feSZach Atkins CeedCall(CeedQFunctionDestroy(&qf_prolong)); 967ea6b5821SJeremy L Thompson } 968ea6b5821SJeremy L Thompson 96958e4b056SJeremy L Thompson // Check 97058e4b056SJeremy L Thompson CeedCall(CeedOperatorCheckReady(*op_coarse)); 97158e4b056SJeremy L Thompson 972eaf62fffSJeremy L Thompson // Cleanup 9732b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&mult_vec)); 974c17ec2beSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine)); 9752b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_c_to_f)); 976eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 977eaf62fffSJeremy L Thompson } 978eaf62fffSJeremy L Thompson 979eaf62fffSJeremy L Thompson /** 980eaf62fffSJeremy L Thompson @brief Build 1D mass matrix and Laplacian with perturbation 981eaf62fffSJeremy L Thompson 982eaf62fffSJeremy L Thompson @param[in] interp_1d Interpolation matrix in one dimension 983eaf62fffSJeremy L Thompson @param[in] grad_1d Gradient matrix in one dimension 984eaf62fffSJeremy L Thompson @param[in] q_weight_1d Quadrature weights in one dimension 985eaf62fffSJeremy L Thompson @param[in] P_1d Number of basis nodes in one dimension 986eaf62fffSJeremy L Thompson @param[in] Q_1d Number of quadrature points in one dimension 987eaf62fffSJeremy L Thompson @param[in] dim Dimension of basis 988eaf62fffSJeremy L Thompson @param[out] mass Assembled mass matrix in one dimension 989eaf62fffSJeremy L Thompson @param[out] laplace Assembled perturbed Laplacian in one dimension 990eaf62fffSJeremy L Thompson 991eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 992eaf62fffSJeremy L Thompson 993eaf62fffSJeremy L Thompson @ref Developer 994eaf62fffSJeremy L Thompson **/ 9952c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 9962c2ea1dbSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d, CeedInt P_1d, CeedInt Q_1d, 9972c2ea1dbSJeremy L Thompson CeedInt dim, CeedScalar *mass, CeedScalar *laplace) { 9982b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) { 999eaf62fffSJeremy L Thompson for (CeedInt j = 0; j < P_1d; j++) { 1000eaf62fffSJeremy L Thompson CeedScalar sum = 0.0; 10012b730f8bSJeremy 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]; 1002eaf62fffSJeremy L Thompson mass[i + j * P_1d] = sum; 1003eaf62fffSJeremy L Thompson } 10042b730f8bSJeremy L Thompson } 1005eaf62fffSJeremy L Thompson // -- Laplacian 10062b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) { 1007eaf62fffSJeremy L Thompson for (CeedInt j = 0; j < P_1d; j++) { 1008eaf62fffSJeremy L Thompson CeedScalar sum = 0.0; 10091c66c397SJeremy L Thompson 10102b730f8bSJeremy 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]; 1011eaf62fffSJeremy L Thompson laplace[i + j * P_1d] = sum; 1012eaf62fffSJeremy L Thompson } 10132b730f8bSJeremy L Thompson } 1014eaf62fffSJeremy L Thompson CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4; 10152b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation; 1016eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1017eaf62fffSJeremy L Thompson } 10182c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 1019eaf62fffSJeremy L Thompson 1020eaf62fffSJeremy L Thompson /// @} 1021eaf62fffSJeremy L Thompson 1022eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 1023480fae85SJeremy L Thompson /// CeedOperator Backend API 1024480fae85SJeremy L Thompson /// ---------------------------------------------------------------------------- 1025480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend 1026480fae85SJeremy L Thompson /// @{ 1027480fae85SJeremy L Thompson 1028480fae85SJeremy L Thompson /** 1029004e4986SSebastian Grimberg @brief Select correct basis matrix pointer based on @ref CeedEvalMode 1030004e4986SSebastian Grimberg 1031004e4986SSebastian Grimberg @param[in] basis `CeedBasis` from which to get the basis matrix 1032004e4986SSebastian Grimberg @param[in] eval_mode Current basis evaluation mode 1033004e4986SSebastian Grimberg @param[in] identity Pointer to identity matrix 1034004e4986SSebastian Grimberg @param[out] basis_ptr `CeedBasis` pointer to set 1035004e4986SSebastian Grimberg 1036004e4986SSebastian Grimberg @ref Backend 1037004e4986SSebastian Grimberg **/ 1038004e4986SSebastian Grimberg int CeedOperatorGetBasisPointer(CeedBasis basis, CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar **basis_ptr) { 1039004e4986SSebastian Grimberg switch (eval_mode) { 1040004e4986SSebastian Grimberg case CEED_EVAL_NONE: 1041004e4986SSebastian Grimberg *basis_ptr = identity; 1042004e4986SSebastian Grimberg break; 1043004e4986SSebastian Grimberg case CEED_EVAL_INTERP: 1044004e4986SSebastian Grimberg CeedCall(CeedBasisGetInterp(basis, basis_ptr)); 1045004e4986SSebastian Grimberg break; 1046004e4986SSebastian Grimberg case CEED_EVAL_GRAD: 1047004e4986SSebastian Grimberg CeedCall(CeedBasisGetGrad(basis, basis_ptr)); 1048004e4986SSebastian Grimberg break; 1049004e4986SSebastian Grimberg case CEED_EVAL_DIV: 1050004e4986SSebastian Grimberg CeedCall(CeedBasisGetDiv(basis, basis_ptr)); 1051004e4986SSebastian Grimberg break; 1052004e4986SSebastian Grimberg case CEED_EVAL_CURL: 1053004e4986SSebastian Grimberg CeedCall(CeedBasisGetCurl(basis, basis_ptr)); 1054004e4986SSebastian Grimberg break; 1055004e4986SSebastian Grimberg case CEED_EVAL_WEIGHT: 1056004e4986SSebastian Grimberg break; // Caught by QF Assembly 1057004e4986SSebastian Grimberg } 1058004e4986SSebastian Grimberg assert(*basis_ptr != NULL); 1059004e4986SSebastian Grimberg return CEED_ERROR_SUCCESS; 1060004e4986SSebastian Grimberg } 1061004e4986SSebastian Grimberg 1062004e4986SSebastian Grimberg /** 1063ca94c3ddSJeremy L Thompson @brief Create point block restriction for active `CeedOperatorField` 1064506b1a0cSSebastian Grimberg 1065ca94c3ddSJeremy L Thompson @param[in] rstr Original `CeedElemRestriction` for active field 1066ca94c3ddSJeremy L Thompson @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 /** 1104ca94c3ddSJeremy L Thompson @brief Create object holding `CeedQFunction` assembly data for `CeedOperator` 1105480fae85SJeremy L Thompson 1106ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedQFunctionAssemblyData` 1107ca94c3ddSJeremy 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 /** 1122ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedQFunctionAssemblyData` 1123480fae85SJeremy L Thompson 1124ca94c3ddSJeremy 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 /** 1136ca94c3ddSJeremy L Thompson @brief Set re-use of `CeedQFunctionAssemblyData` 11378b919e6bSJeremy L Thompson 1138ca94c3ddSJeremy 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 /** 1152ca94c3ddSJeremy L Thompson @brief Mark `CeedQFunctionAssemblyData` as stale 1153beecbf24SJeremy L Thompson 1154ca94c3ddSJeremy 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 /** 1167ca94c3ddSJeremy L Thompson @brief Determine if `CeedQFunctionAssemblyData` needs update 11688b919e6bSJeremy L Thompson 1169ca94c3ddSJeremy 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 /** 1182ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedQFunctionAssemblyData`. 11834385fb7fSSebastian Grimberg 1184ca94c3ddSJeremy L Thompson Both pointers should be destroyed with @ref CeedQFunctionAssemblyDataDestroy(). 1185512bb800SJeremy L Thompson 1186ca94c3ddSJeremy L Thompson Note: If the value of ` *data_copy` passed to this function is non-`NULL` , then it is assumed that ` *data_copy` is a pointer to a `CeedQFunctionAssemblyData`. 1187ca94c3ddSJeremy L Thompson This `CeedQFunctionAssemblyData` will be destroyed if ` *data_copy` is the only reference to this `CeedQFunctionAssemblyData`. 1188480fae85SJeremy L Thompson 1189ca94c3ddSJeremy L Thompson @param[in] data `CeedQFunctionAssemblyData` to copy reference to 1190ea61e9acSJeremy L Thompson @param[in,out] data_copy Variable to store copied reference 1191480fae85SJeremy L Thompson 1192480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1193480fae85SJeremy L Thompson 1194480fae85SJeremy L Thompson @ref Backend 1195480fae85SJeremy L Thompson **/ 11962b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) { 11972b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataReference(data)); 11982b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy)); 1199480fae85SJeremy L Thompson *data_copy = data; 1200480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1201480fae85SJeremy L Thompson } 1202480fae85SJeremy L Thompson 1203480fae85SJeremy L Thompson /** 1204ca94c3ddSJeremy L Thompson @brief Get setup status for internal objects for `CeedQFunctionAssemblyData` 1205480fae85SJeremy L Thompson 1206ca94c3ddSJeremy L Thompson @param[in] data `CeedQFunctionAssemblyData` to retrieve status 1207480fae85SJeremy L Thompson @param[out] is_setup Boolean flag for setup status 1208480fae85SJeremy L Thompson 1209480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1210480fae85SJeremy L Thompson 1211480fae85SJeremy L Thompson @ref Backend 1212480fae85SJeremy L Thompson **/ 12132b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) { 1214480fae85SJeremy L Thompson *is_setup = data->is_setup; 1215480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1216480fae85SJeremy L Thompson } 1217480fae85SJeremy L Thompson 1218480fae85SJeremy L Thompson /** 1219ca94c3ddSJeremy L Thompson @brief Set internal objects for `CeedQFunctionAssemblyData` 1220480fae85SJeremy L Thompson 1221ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to set objects 1222ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to store assembled `CeedQFunction` at quadrature points 1223ca94c3ddSJeremy L Thompson @param[in] rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction` 1224480fae85SJeremy L Thompson 1225480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1226480fae85SJeremy L Thompson 1227480fae85SJeremy L Thompson @ref Backend 1228480fae85SJeremy L Thompson **/ 12292b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) { 12302b730f8bSJeremy L Thompson CeedCall(CeedVectorReferenceCopy(vec, &data->vec)); 12312b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr)); 1232480fae85SJeremy L Thompson 1233480fae85SJeremy L Thompson data->is_setup = true; 1234480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1235480fae85SJeremy L Thompson } 1236480fae85SJeremy L Thompson 12374dd1a9d2SSebastian Grimberg /** 1238ca94c3ddSJeremy L Thompson @brief Get internal objects for `CeedQFunctionAssemblyData` 12394dd1a9d2SSebastian Grimberg 1240ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to set objects 1241ca94c3ddSJeremy L Thompson @param[out] vec `CeedVector` to store assembled `CeedQFunction` at quadrature points 1242ca94c3ddSJeremy L Thompson @param[out] rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction` 12434dd1a9d2SSebastian Grimberg 12444dd1a9d2SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 12454dd1a9d2SSebastian Grimberg 12464dd1a9d2SSebastian Grimberg @ref Backend 12474dd1a9d2SSebastian Grimberg **/ 12482b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) { 12496574a04fSJeremy L Thompson CeedCheck(data->is_setup, data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first."); 1250480fae85SJeremy L Thompson 12512b730f8bSJeremy L Thompson CeedCall(CeedVectorReferenceCopy(data->vec, vec)); 12522b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr)); 1253480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1254480fae85SJeremy L Thompson } 1255480fae85SJeremy L Thompson 1256480fae85SJeremy L Thompson /** 1257ca94c3ddSJeremy L Thompson @brief Destroy `CeedQFunctionAssemblyData` 1258480fae85SJeremy L Thompson 1259ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to destroy 1260480fae85SJeremy L Thompson 1261480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1262480fae85SJeremy L Thompson 1263480fae85SJeremy L Thompson @ref Backend 1264480fae85SJeremy L Thompson **/ 1265480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) { 1266ad6481ceSJeremy L Thompson if (!*data || --(*data)->ref_count > 0) { 1267ad6481ceSJeremy L Thompson *data = NULL; 1268ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1269ad6481ceSJeremy L Thompson } 12702b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*data)->ceed)); 12712b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*data)->vec)); 12722b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr)); 1273480fae85SJeremy L Thompson 12742b730f8bSJeremy L Thompson CeedCall(CeedFree(data)); 1275480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1276480fae85SJeremy L Thompson } 1277480fae85SJeremy L Thompson 1278ed9e99e6SJeremy L Thompson /** 1279ca94c3ddSJeremy L Thompson @brief Get `CeedOperatorAssemblyData` 1280ed9e99e6SJeremy L Thompson 1281ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 1282ca94c3ddSJeremy L Thompson @param[out] data `CeedQFunctionAssemblyData` 1283ed9e99e6SJeremy L Thompson 1284ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1285ed9e99e6SJeremy L Thompson 1286ed9e99e6SJeremy L Thompson @ref Backend 1287ed9e99e6SJeremy L Thompson **/ 12882b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) { 1289ed9e99e6SJeremy L Thompson if (!op->op_assembled) { 1290ed9e99e6SJeremy L Thompson CeedOperatorAssemblyData data; 1291ed9e99e6SJeremy L Thompson 12922b730f8bSJeremy L Thompson CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data)); 1293ed9e99e6SJeremy L Thompson op->op_assembled = data; 1294ed9e99e6SJeremy L Thompson } 1295ed9e99e6SJeremy L Thompson *data = op->op_assembled; 1296ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1297ed9e99e6SJeremy L Thompson } 1298ed9e99e6SJeremy L Thompson 1299ed9e99e6SJeremy L Thompson /** 1300ca94c3ddSJeremy L Thompson @brief Create object holding `CeedOperator` assembly data. 1301ba746a46SJeremy L Thompson 1302ca94c3ddSJeremy L Thompson The `CeedOperatorAssemblyData` holds an array with references to every active `CeedBasis` used in the `CeedOperator`. 1303ca94c3ddSJeremy L Thompson An array with references to the corresponding active `CeedElemRestriction` is also stored. 1304ca94c3ddSJeremy L Thompson For each active `CeedBasis, the `CeedOperatorAssemblyData` holds an array of all input and output @ref CeedEvalMode for this `CeedBasis`. 1305ca94c3ddSJeremy L Thompson The `CeedOperatorAssemblyData` holds an array of offsets for indexing into the assembled `CeedQFunction` arrays to the row representing each @ref CeedEvalMode. 1306ca94c3ddSJeremy L Thompson The number of input columns across all active bases for the assembled `CeedQFunction` is also stored. 1307ca94c3ddSJeremy L Thompson Lastly, the `CeedOperatorAssembly` data holds assembled matrices representing the full action of the `CeedBasis` for all @ref CeedEvalMode. 1308ed9e99e6SJeremy L Thompson 1309ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedOperatorAssemblyData` 1310ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to be assembled 1311ca94c3ddSJeremy L Thompson @param[out] data Address of the variable where the newly created `CeedOperatorAssemblyData` will be stored 1312ed9e99e6SJeremy L Thompson 1313ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1314ed9e99e6SJeremy L Thompson 1315ed9e99e6SJeremy L Thompson @ref Backend 1316ed9e99e6SJeremy L Thompson **/ 13172b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) { 1318506b1a0cSSebastian Grimberg CeedInt num_active_bases_in = 0, num_active_bases_out = 0, offset = 0; 1319506b1a0cSSebastian Grimberg CeedInt num_input_fields, *num_eval_modes_in = NULL, num_output_fields, *num_eval_modes_out = NULL; 13201c66c397SJeremy L Thompson CeedSize **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL; 13211c66c397SJeremy L Thompson CeedEvalMode **eval_modes_in = NULL, **eval_modes_out = NULL; 13221c66c397SJeremy L Thompson CeedQFunctionField *qf_fields; 13231c66c397SJeremy L Thompson CeedQFunction qf; 13241c66c397SJeremy L Thompson CeedOperatorField *op_fields; 132501f0e615SJames Wright bool is_composite; 132601f0e615SJames Wright 132701f0e615SJames Wright CeedCall(CeedOperatorIsComposite(op, &is_composite)); 132801f0e615SJames Wright CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Can only create CeedOperator assembly data for non-composite operators."); 1329437c7c90SJeremy L Thompson 1330437c7c90SJeremy L Thompson // Allocate 13312b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, data)); 1332ed9e99e6SJeremy L Thompson (*data)->ceed = ceed; 13332b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 1334ed9e99e6SJeremy L Thompson 1335ed9e99e6SJeremy L Thompson // Build OperatorAssembly data 13362b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetQFunction(op, &qf)); 1337ed9e99e6SJeremy L Thompson 1338ed9e99e6SJeremy L Thompson // Determine active input basis 1339004e4986SSebastian Grimberg CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL)); 1340004e4986SSebastian Grimberg CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 1341ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1342ed9e99e6SJeremy L Thompson CeedVector vec; 13431c66c397SJeremy L Thompson 13442b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec)); 1345ed9e99e6SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 13467c1dbaffSSebastian Grimberg CeedInt index = -1, num_comp, q_comp; 13471c66c397SJeremy L Thompson CeedEvalMode eval_mode; 13481c66c397SJeremy L Thompson CeedBasis basis_in = NULL; 13491c66c397SJeremy L Thompson 13502b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in)); 13512b730f8bSJeremy L Thompson CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1352352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp)); 1353352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 1354506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_active_bases_in; i++) { 1355506b1a0cSSebastian Grimberg if ((*data)->active_bases_in[i] == basis_in) index = i; 1356437c7c90SJeremy L Thompson } 1357437c7c90SJeremy L Thompson if (index == -1) { 1358437c7c90SJeremy L Thompson CeedElemRestriction elem_rstr_in; 13591c66c397SJeremy L Thompson 1360506b1a0cSSebastian Grimberg index = num_active_bases_in; 1361506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_bases_in)); 1362506b1a0cSSebastian Grimberg (*data)->active_bases_in[num_active_bases_in] = NULL; 1363506b1a0cSSebastian Grimberg CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases_in[num_active_bases_in])); 1364506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_elem_rstrs_in)); 1365506b1a0cSSebastian Grimberg (*data)->active_elem_rstrs_in[num_active_bases_in] = NULL; 1366437c7c90SJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in)); 1367506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs_in[num_active_bases_in])); 1368506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &num_eval_modes_in)); 1369437c7c90SJeremy L Thompson num_eval_modes_in[index] = 0; 1370506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_modes_in)); 1371437c7c90SJeremy L Thompson eval_modes_in[index] = NULL; 1372506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_mode_offsets_in)); 1373437c7c90SJeremy L Thompson eval_mode_offsets_in[index] = NULL; 1374506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->assembled_bases_in)); 1375437c7c90SJeremy L Thompson (*data)->assembled_bases_in[index] = NULL; 1376506b1a0cSSebastian Grimberg num_active_bases_in++; 1377437c7c90SJeremy L Thompson } 1378352a5e7cSSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1379352a5e7cSSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 1380352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_modes_in[index])); 1381352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_mode_offsets_in[index])); 1382352a5e7cSSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 1383437c7c90SJeremy L Thompson eval_modes_in[index][num_eval_modes_in[index] + d] = eval_mode; 1384437c7c90SJeremy L Thompson eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset; 1385352a5e7cSSebastian Grimberg offset += num_comp; 1386ed9e99e6SJeremy L Thompson } 1387352a5e7cSSebastian Grimberg num_eval_modes_in[index] += q_comp; 1388ed9e99e6SJeremy L Thompson } 1389ed9e99e6SJeremy L Thompson } 1390ed9e99e6SJeremy L Thompson } 1391ed9e99e6SJeremy L Thompson 1392ed9e99e6SJeremy L Thompson // Determine active output basis 13932b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields)); 13942b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 1395437c7c90SJeremy L Thompson offset = 0; 1396ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1397ed9e99e6SJeremy L Thompson CeedVector vec; 13981c66c397SJeremy L Thompson 13992b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec)); 1400ed9e99e6SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 14017c1dbaffSSebastian Grimberg CeedInt index = -1, num_comp, q_comp; 14021c66c397SJeremy L Thompson CeedEvalMode eval_mode; 14031c66c397SJeremy L Thompson CeedBasis basis_out = NULL; 14041c66c397SJeremy L Thompson 1405437c7c90SJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out)); 14062b730f8bSJeremy L Thompson CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1407352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumComponents(basis_out, &num_comp)); 1408352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 1409506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_active_bases_out; i++) { 1410506b1a0cSSebastian Grimberg if ((*data)->active_bases_out[i] == basis_out) index = i; 1411437c7c90SJeremy L Thompson } 1412437c7c90SJeremy L Thompson if (index == -1) { 1413437c7c90SJeremy L Thompson CeedElemRestriction elem_rstr_out; 14141c66c397SJeremy L Thompson 1415506b1a0cSSebastian Grimberg index = num_active_bases_out; 1416506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_bases_out)); 1417506b1a0cSSebastian Grimberg (*data)->active_bases_out[num_active_bases_out] = NULL; 1418506b1a0cSSebastian Grimberg CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases_out[num_active_bases_out])); 1419506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_elem_rstrs_out)); 1420506b1a0cSSebastian Grimberg (*data)->active_elem_rstrs_out[num_active_bases_out] = NULL; 1421437c7c90SJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out)); 1422506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs_out[num_active_bases_out])); 1423506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &num_eval_modes_out)); 1424437c7c90SJeremy L Thompson num_eval_modes_out[index] = 0; 1425506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_modes_out)); 1426437c7c90SJeremy L Thompson eval_modes_out[index] = NULL; 1427506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_mode_offsets_out)); 1428437c7c90SJeremy L Thompson eval_mode_offsets_out[index] = NULL; 1429506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->assembled_bases_out)); 1430437c7c90SJeremy L Thompson (*data)->assembled_bases_out[index] = NULL; 1431506b1a0cSSebastian Grimberg num_active_bases_out++; 1432437c7c90SJeremy L Thompson } 1433352a5e7cSSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1434352a5e7cSSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 1435352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_modes_out[index])); 1436352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_mode_offsets_out[index])); 1437352a5e7cSSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 1438437c7c90SJeremy L Thompson eval_modes_out[index][num_eval_modes_out[index] + d] = eval_mode; 1439437c7c90SJeremy L Thompson eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset; 1440352a5e7cSSebastian Grimberg offset += num_comp; 1441ed9e99e6SJeremy L Thompson } 1442352a5e7cSSebastian Grimberg num_eval_modes_out[index] += q_comp; 1443ed9e99e6SJeremy L Thompson } 1444ed9e99e6SJeremy L Thompson } 1445ed9e99e6SJeremy L Thompson } 1446506b1a0cSSebastian Grimberg (*data)->num_active_bases_in = num_active_bases_in; 144727789c4aSJed Brown (*data)->num_eval_modes_in = num_eval_modes_in; 144827789c4aSJed Brown (*data)->eval_modes_in = eval_modes_in; 144927789c4aSJed Brown (*data)->eval_mode_offsets_in = eval_mode_offsets_in; 1450506b1a0cSSebastian Grimberg (*data)->num_active_bases_out = num_active_bases_out; 1451437c7c90SJeremy L Thompson (*data)->num_eval_modes_out = num_eval_modes_out; 1452437c7c90SJeremy L Thompson (*data)->eval_modes_out = eval_modes_out; 1453437c7c90SJeremy L Thompson (*data)->eval_mode_offsets_out = eval_mode_offsets_out; 1454506b1a0cSSebastian Grimberg (*data)->num_output_components = offset; 1455ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1456ed9e99e6SJeremy L Thompson } 1457ed9e99e6SJeremy L Thompson 1458ed9e99e6SJeremy L Thompson /** 1459ca94c3ddSJeremy L Thompson @brief Get `CeedOperator` @ref CeedEvalMode for assembly. 1460ba746a46SJeremy L Thompson 1461ca94c3ddSJeremy L Thompson Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object. 1462ed9e99e6SJeremy L Thompson 1463ca94c3ddSJeremy L Thompson @param[in] data `CeedOperatorAssemblyData` 1464506b1a0cSSebastian Grimberg @param[out] num_active_bases_in Total number of active bases for input 1465ca94c3ddSJeremy L Thompson @param[out] num_eval_modes_in Pointer to hold array of numbers of input @ref CeedEvalMode, or `NULL`. 1466ca94c3ddSJeremy L Thompson `eval_modes_in[0]` holds an array of eval modes for the first active `CeedBasis`. 1467ca94c3ddSJeremy L Thompson @param[out] eval_modes_in Pointer to hold arrays of input @ref CeedEvalMode, or `NULL` 1468ca94c3ddSJeremy L Thompson @param[out] eval_mode_offsets_in Pointer to hold arrays of input offsets at each quadrature point 1469506b1a0cSSebastian Grimberg @param[out] num_active_bases_out Total number of active bases for output 1470ca94c3ddSJeremy L Thompson @param[out] num_eval_modes_out Pointer to hold array of numbers of output @ref CeedEvalMode, or `NULL` 1471ca94c3ddSJeremy L Thompson @param[out] eval_modes_out Pointer to hold arrays of output @ref CeedEvalMode, or `NULL` 1472437c7c90SJeremy L Thompson @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point 1473ca94c3ddSJeremy L Thompson @param[out] num_output_components The number of columns in the assembled `CeedQFunction` matrix for each quadrature point, including contributions of all active bases 1474ed9e99e6SJeremy L Thompson 1475ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1476ed9e99e6SJeremy L Thompson 1477ed9e99e6SJeremy L Thompson @ref Backend 1478ed9e99e6SJeremy L Thompson **/ 1479506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedInt **num_eval_modes_in, 1480506b1a0cSSebastian Grimberg const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt *num_active_bases_out, 1481506b1a0cSSebastian Grimberg CeedInt **num_eval_modes_out, const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out, 1482506b1a0cSSebastian Grimberg CeedSize *num_output_components) { 1483506b1a0cSSebastian Grimberg if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in; 1484437c7c90SJeremy L Thompson if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in; 1485437c7c90SJeremy L Thompson if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in; 1486437c7c90SJeremy L Thompson if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in; 1487506b1a0cSSebastian Grimberg if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out; 1488437c7c90SJeremy L Thompson if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out; 1489437c7c90SJeremy L Thompson if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out; 1490437c7c90SJeremy L Thompson if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out; 1491437c7c90SJeremy L Thompson if (num_output_components) *num_output_components = data->num_output_components; 1492ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1493ed9e99e6SJeremy L Thompson } 1494ed9e99e6SJeremy L Thompson 1495ed9e99e6SJeremy L Thompson /** 1496ca94c3ddSJeremy L Thompson @brief Get `CeedOperator` `CeedBasis` data for assembly. 1497ba746a46SJeremy L Thompson 1498ca94c3ddSJeremy L Thompson Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object. 1499ed9e99e6SJeremy L Thompson 1500ca94c3ddSJeremy L Thompson @param[in] data `CeedOperatorAssemblyData` 1501ca94c3ddSJeremy L Thompson @param[out] num_active_bases_in Number of active input bases, or `NULL` 1502ca94c3ddSJeremy L Thompson @param[out] active_bases_in Pointer to hold active input `CeedBasis`, or `NULL` 1503ca94c3ddSJeremy L Thompson @param[out] assembled_bases_in Pointer to hold assembled active input `B` , or `NULL` 1504ca94c3ddSJeremy L Thompson @param[out] num_active_bases_out Number of active output bases, or `NULL` 1505ca94c3ddSJeremy L Thompson @param[out] active_bases_out Pointer to hold active output `CeedBasis`, or `NULL` 1506ca94c3ddSJeremy L Thompson @param[out] assembled_bases_out Pointer to hold assembled active output `B` , or `NULL` 1507ed9e99e6SJeremy L Thompson 1508ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1509ed9e99e6SJeremy L Thompson 1510ed9e99e6SJeremy L Thompson @ref Backend 1511ed9e99e6SJeremy L Thompson **/ 1512506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedBasis **active_bases_in, 1513506b1a0cSSebastian Grimberg const CeedScalar ***assembled_bases_in, CeedInt *num_active_bases_out, CeedBasis **active_bases_out, 1514506b1a0cSSebastian Grimberg const CeedScalar ***assembled_bases_out) { 1515ed9e99e6SJeremy L Thompson // Assemble B_in, B_out if needed 1516437c7c90SJeremy L Thompson if (assembled_bases_in && !data->assembled_bases_in[0]) { 1517437c7c90SJeremy L Thompson CeedInt num_qpts; 1518437c7c90SJeremy L Thompson 1519506b1a0cSSebastian Grimberg if (data->active_bases_in[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[0], &num_qpts)); 1520506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_in[0], &num_qpts)); 1521506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < data->num_active_bases_in; b++) { 15221c66c397SJeremy L Thompson bool has_eval_none = false; 1523352a5e7cSSebastian Grimberg CeedInt num_nodes; 1524437c7c90SJeremy L Thompson CeedScalar *B_in = NULL, *identity = NULL; 1525ed9e99e6SJeremy L Thompson 1526506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[b], &num_nodes)); 1527352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_in[b], &B_in)); 1528ed9e99e6SJeremy L Thompson 1529437c7c90SJeremy L Thompson for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) { 1530437c7c90SJeremy L Thompson has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE); 1531ed9e99e6SJeremy L Thompson } 1532ed9e99e6SJeremy L Thompson if (has_eval_none) { 1533352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes, &identity)); 1534352a5e7cSSebastian Grimberg for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) { 1535352a5e7cSSebastian Grimberg identity[i * num_nodes + i] = 1.0; 1536ed9e99e6SJeremy L Thompson } 1537ed9e99e6SJeremy L Thompson } 1538ed9e99e6SJeremy L Thompson 1539ed9e99e6SJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 1540352a5e7cSSebastian Grimberg for (CeedInt n = 0; n < num_nodes; n++) { 1541352a5e7cSSebastian Grimberg CeedInt d_in = 0, q_comp_in; 1542352a5e7cSSebastian Grimberg CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE; 15431c66c397SJeremy L Thompson 1544437c7c90SJeremy L Thompson for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) { 1545437c7c90SJeremy L Thompson const CeedInt qq = data->num_eval_modes_in[b] * q; 1546437c7c90SJeremy L Thompson const CeedScalar *B = NULL; 15471c66c397SJeremy L Thompson 1548506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(data->active_bases_in[b], data->eval_modes_in[b][e_in], identity, &B)); 1549506b1a0cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_in[b], data->eval_modes_in[b][e_in], &q_comp_in)); 1550352a5e7cSSebastian Grimberg if (q_comp_in > 1) { 1551352a5e7cSSebastian Grimberg if (e_in == 0 || data->eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0; 1552352a5e7cSSebastian Grimberg else B = &B[(++d_in) * num_qpts * num_nodes]; 1553352a5e7cSSebastian Grimberg } 1554352a5e7cSSebastian Grimberg eval_mode_in_prev = data->eval_modes_in[b][e_in]; 1555352a5e7cSSebastian Grimberg B_in[(qq + e_in) * num_nodes + n] = B[q * num_nodes + n]; 1556ed9e99e6SJeremy L Thompson } 1557ed9e99e6SJeremy L Thompson } 1558ed9e99e6SJeremy L Thompson } 15597c1dbaffSSebastian Grimberg if (identity) CeedCall(CeedFree(&identity)); 1560437c7c90SJeremy L Thompson data->assembled_bases_in[b] = B_in; 1561437c7c90SJeremy L Thompson } 1562ed9e99e6SJeremy L Thompson } 1563ed9e99e6SJeremy L Thompson 1564437c7c90SJeremy L Thompson if (assembled_bases_out && !data->assembled_bases_out[0]) { 1565437c7c90SJeremy L Thompson CeedInt num_qpts; 1566437c7c90SJeremy L Thompson 1567506b1a0cSSebastian Grimberg if (data->active_bases_out[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[0], &num_qpts)); 1568506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_out[0], &num_qpts)); 1569506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < data->num_active_bases_out; b++) { 1570ed9e99e6SJeremy L Thompson bool has_eval_none = false; 15711c66c397SJeremy L Thompson CeedInt num_nodes; 1572437c7c90SJeremy L Thompson CeedScalar *B_out = NULL, *identity = NULL; 1573ed9e99e6SJeremy L Thompson 1574506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[b], &num_nodes)); 1575352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_out[b], &B_out)); 1576ed9e99e6SJeremy L Thompson 1577437c7c90SJeremy L Thompson for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) { 1578437c7c90SJeremy L Thompson has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE); 1579ed9e99e6SJeremy L Thompson } 1580ed9e99e6SJeremy L Thompson if (has_eval_none) { 1581352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes, &identity)); 1582352a5e7cSSebastian Grimberg for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) { 1583352a5e7cSSebastian Grimberg identity[i * num_nodes + i] = 1.0; 1584ed9e99e6SJeremy L Thompson } 1585ed9e99e6SJeremy L Thompson } 1586ed9e99e6SJeremy L Thompson 1587ed9e99e6SJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 1588352a5e7cSSebastian Grimberg for (CeedInt n = 0; n < num_nodes; n++) { 1589352a5e7cSSebastian Grimberg CeedInt d_out = 0, q_comp_out; 1590352a5e7cSSebastian Grimberg CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE; 15911c66c397SJeremy L Thompson 1592437c7c90SJeremy L Thompson for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) { 1593437c7c90SJeremy L Thompson const CeedInt qq = data->num_eval_modes_out[b] * q; 1594437c7c90SJeremy L Thompson const CeedScalar *B = NULL; 15951c66c397SJeremy L Thompson 1596506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(data->active_bases_out[b], data->eval_modes_out[b][e_out], identity, &B)); 1597506b1a0cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_out[b], data->eval_modes_out[b][e_out], &q_comp_out)); 1598352a5e7cSSebastian Grimberg if (q_comp_out > 1) { 1599352a5e7cSSebastian Grimberg if (e_out == 0 || data->eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0; 1600352a5e7cSSebastian Grimberg else B = &B[(++d_out) * num_qpts * num_nodes]; 1601352a5e7cSSebastian Grimberg } 1602352a5e7cSSebastian Grimberg eval_mode_out_prev = data->eval_modes_out[b][e_out]; 1603352a5e7cSSebastian Grimberg B_out[(qq + e_out) * num_nodes + n] = B[q * num_nodes + n]; 1604ed9e99e6SJeremy L Thompson } 1605ed9e99e6SJeremy L Thompson } 1606ed9e99e6SJeremy L Thompson } 16077c1dbaffSSebastian Grimberg if (identity) CeedCall(CeedFree(&identity)); 1608437c7c90SJeremy L Thompson data->assembled_bases_out[b] = B_out; 1609437c7c90SJeremy L Thompson } 1610ed9e99e6SJeremy L Thompson } 1611ed9e99e6SJeremy L Thompson 1612437c7c90SJeremy L Thompson // Pass out assembled data 1613506b1a0cSSebastian Grimberg if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in; 1614506b1a0cSSebastian Grimberg if (active_bases_in) *active_bases_in = data->active_bases_in; 1615437c7c90SJeremy L Thompson if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in; 1616506b1a0cSSebastian Grimberg if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out; 1617506b1a0cSSebastian Grimberg if (active_bases_out) *active_bases_out = data->active_bases_out; 1618437c7c90SJeremy L Thompson if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out; 1619437c7c90SJeremy L Thompson return CEED_ERROR_SUCCESS; 1620437c7c90SJeremy L Thompson } 1621437c7c90SJeremy L Thompson 1622437c7c90SJeremy L Thompson /** 1623ca94c3ddSJeremy L Thompson @brief Get `CeedOperator` `CeedBasis` data for assembly. 1624ba746a46SJeremy L Thompson 1625ca94c3ddSJeremy L Thompson Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object. 1626437c7c90SJeremy L Thompson 1627ca94c3ddSJeremy L Thompson @param[in] data `CeedOperatorAssemblyData` 1628ca94c3ddSJeremy L Thompson @param[out] num_active_elem_rstrs_in Number of active input element restrictions, or `NULL` 1629ca94c3ddSJeremy L Thompson @param[out] active_elem_rstrs_in Pointer to hold active input `CeedElemRestriction`, or `NULL` 1630ca94c3ddSJeremy L Thompson @param[out] num_active_elem_rstrs_out Number of active output element restrictions, or `NULL` 1631ca94c3ddSJeremy L Thompson @param[out] active_elem_rstrs_out Pointer to hold active output `CeedElemRestriction`, or `NULL` 1632437c7c90SJeremy L Thompson 1633437c7c90SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1634437c7c90SJeremy L Thompson 1635437c7c90SJeremy L Thompson @ref Backend 1636437c7c90SJeremy L Thompson **/ 1637506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs_in, 1638506b1a0cSSebastian Grimberg CeedElemRestriction **active_elem_rstrs_in, CeedInt *num_active_elem_rstrs_out, 1639506b1a0cSSebastian Grimberg CeedElemRestriction **active_elem_rstrs_out) { 1640506b1a0cSSebastian Grimberg if (num_active_elem_rstrs_in) *num_active_elem_rstrs_in = data->num_active_bases_in; 1641506b1a0cSSebastian Grimberg if (active_elem_rstrs_in) *active_elem_rstrs_in = data->active_elem_rstrs_in; 1642506b1a0cSSebastian Grimberg if (num_active_elem_rstrs_out) *num_active_elem_rstrs_out = data->num_active_bases_out; 1643506b1a0cSSebastian Grimberg if (active_elem_rstrs_out) *active_elem_rstrs_out = data->active_elem_rstrs_out; 1644ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1645ed9e99e6SJeremy L Thompson } 1646ed9e99e6SJeremy L Thompson 1647ed9e99e6SJeremy L Thompson /** 1648ca94c3ddSJeremy L Thompson @brief Destroy `CeedOperatorAssemblyData` 1649ed9e99e6SJeremy L Thompson 1650ca94c3ddSJeremy L Thompson @param[in,out] data `CeedOperatorAssemblyData` to destroy 1651ed9e99e6SJeremy L Thompson 1652ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1653ed9e99e6SJeremy L Thompson 1654ed9e99e6SJeremy L Thompson @ref Backend 1655ed9e99e6SJeremy L Thompson **/ 1656ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) { 1657ad6481ceSJeremy L Thompson if (!*data) { 1658ad6481ceSJeremy L Thompson *data = NULL; 1659ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1660ad6481ceSJeremy L Thompson } 16612b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*data)->ceed)); 1662506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < (*data)->num_active_bases_in; b++) { 1663506b1a0cSSebastian Grimberg CeedCall(CeedBasisDestroy(&(*data)->active_bases_in[b])); 1664506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_in[b])); 1665437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_modes_in[b])); 1666437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b])); 1667437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_in[b])); 1668506b1a0cSSebastian Grimberg } 1669506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < (*data)->num_active_bases_out; b++) { 1670506b1a0cSSebastian Grimberg CeedCall(CeedBasisDestroy(&(*data)->active_bases_out[b])); 1671506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_out[b])); 1672506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->eval_modes_out[b])); 1673506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b])); 1674437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_out[b])); 1675437c7c90SJeremy L Thompson } 1676506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_bases_in)); 1677506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_bases_out)); 1678506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_elem_rstrs_in)); 1679506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_elem_rstrs_out)); 1680437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->num_eval_modes_in)); 1681437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->num_eval_modes_out)); 1682437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_modes_in)); 1683437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_modes_out)); 1684437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_mode_offsets_in)); 1685437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_mode_offsets_out)); 1686437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_in)); 1687437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_out)); 1688ed9e99e6SJeremy L Thompson 16892b730f8bSJeremy L Thompson CeedCall(CeedFree(data)); 1690ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1691ed9e99e6SJeremy L Thompson } 1692ed9e99e6SJeremy L Thompson 16934dd1a9d2SSebastian Grimberg /** 1694ca94c3ddSJeremy L Thompson @brief Retrieve fallback `CeedOperator` with a reference `Ceed` for advanced `CeedOperator` functionality 16954dd1a9d2SSebastian Grimberg 1696ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to retrieve fallback for 1697ca94c3ddSJeremy L Thompson @param[out] op_fallback Fallback `CeedOperator` 16984dd1a9d2SSebastian Grimberg 16994dd1a9d2SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 17004dd1a9d2SSebastian Grimberg 17014dd1a9d2SSebastian Grimberg @ref Backend 17024dd1a9d2SSebastian Grimberg **/ 17034dd1a9d2SSebastian Grimberg int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) { 17044dd1a9d2SSebastian Grimberg // Create if needed 17054dd1a9d2SSebastian Grimberg if (!op->op_fallback) CeedCall(CeedOperatorCreateFallback(op)); 17064dd1a9d2SSebastian Grimberg if (op->op_fallback) { 17074dd1a9d2SSebastian Grimberg bool is_debug; 17081203703bSJeremy L Thompson Ceed ceed; 17094dd1a9d2SSebastian Grimberg 17104dd1a9d2SSebastian Grimberg CeedCall(CeedOperatorGetCeed(op, &ceed)); 17111203703bSJeremy L Thompson CeedCall(CeedIsDebug(ceed, &is_debug)); 17121203703bSJeremy L Thompson if (is_debug) { 17131203703bSJeremy L Thompson Ceed ceed_fallback; 17141203703bSJeremy L Thompson const char *resource, *resource_fallback; 17151203703bSJeremy L Thompson 17164dd1a9d2SSebastian Grimberg CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback)); 17174dd1a9d2SSebastian Grimberg CeedCall(CeedGetResource(ceed, &resource)); 17184dd1a9d2SSebastian Grimberg CeedCall(CeedGetResource(ceed_fallback, &resource_fallback)); 17194dd1a9d2SSebastian Grimberg 17204dd1a9d2SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n"); 1721249f8407SJeremy L Thompson CeedDebug(ceed, "Falling back from %s operator at address %p to %s operator at address %p\n", resource, op, resource_fallback, op->op_fallback); 17224dd1a9d2SSebastian Grimberg } 17234dd1a9d2SSebastian Grimberg } 17244dd1a9d2SSebastian Grimberg *op_fallback = op->op_fallback; 17254dd1a9d2SSebastian Grimberg return CEED_ERROR_SUCCESS; 17264dd1a9d2SSebastian Grimberg } 17274dd1a9d2SSebastian Grimberg 17284dd1a9d2SSebastian Grimberg /** 1729ca94c3ddSJeremy L Thompson @brief Get the parent `CeedOperator` for a fallback `CeedOperator` 17304dd1a9d2SSebastian Grimberg 1731ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` context 1732ca94c3ddSJeremy L Thompson @param[out] parent Variable to store parent `CeedOperator` context 17334dd1a9d2SSebastian Grimberg 17344dd1a9d2SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 17354dd1a9d2SSebastian Grimberg 17364dd1a9d2SSebastian Grimberg @ref Backend 17374dd1a9d2SSebastian Grimberg **/ 17384dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParent(CeedOperator op, CeedOperator *parent) { 17394dd1a9d2SSebastian Grimberg *parent = op->op_fallback_parent ? op->op_fallback_parent : NULL; 17404dd1a9d2SSebastian Grimberg return CEED_ERROR_SUCCESS; 17414dd1a9d2SSebastian Grimberg } 17424dd1a9d2SSebastian Grimberg 17434dd1a9d2SSebastian Grimberg /** 1744ca94c3ddSJeremy L Thompson @brief Get the `Ceed` context of the parent `CeedOperator` for a fallback `CeedOperator` 17454dd1a9d2SSebastian Grimberg 1746ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` context 1747ca94c3ddSJeremy L Thompson @param[out] parent Variable to store parent `Ceed` context 17484dd1a9d2SSebastian Grimberg 17494dd1a9d2SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 17504dd1a9d2SSebastian Grimberg 17514dd1a9d2SSebastian Grimberg @ref Backend 17524dd1a9d2SSebastian Grimberg **/ 17534dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParentCeed(CeedOperator op, Ceed *parent) { 17544dd1a9d2SSebastian Grimberg *parent = op->op_fallback_parent ? op->op_fallback_parent->ceed : op->ceed; 17554dd1a9d2SSebastian Grimberg return CEED_ERROR_SUCCESS; 17564dd1a9d2SSebastian Grimberg } 17574dd1a9d2SSebastian Grimberg 1758480fae85SJeremy L Thompson /// @} 1759480fae85SJeremy L Thompson 1760480fae85SJeremy L Thompson /// ---------------------------------------------------------------------------- 1761eaf62fffSJeremy L Thompson /// CeedOperator Public API 1762eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 1763eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser 1764eaf62fffSJeremy L Thompson /// @{ 1765eaf62fffSJeremy L Thompson 1766eaf62fffSJeremy L Thompson /** 1767ca94c3ddSJeremy L Thompson @brief Assemble a linear `CeedQFunction` associated with a `CeedOperator`. 1768eaf62fffSJeremy L Thompson 1769ca94c3ddSJeremy L Thompson This returns a `CeedVector` containing a matrix at each quadrature point providing the action of the `CeedQFunction` associated with the `CeedOperator`. 1770ca94c3ddSJeremy L Thompson The vector `assembled` is of shape `[num_elements, num_input_fields, num_output_fields, num_quad_points]` and contains column-major matrices representing the action of the `CeedQFunction` for a corresponding quadrature point on an element. 1771859c15bbSJames Wright 1772ca94c3ddSJeremy L Thompson Inputs and outputs are in the order provided by the user when adding `CeedOperator` fields. 1773ca94c3ddSJeremy L Thompson For example, a `CeedQFunction` with inputs `u` and `gradu` and outputs `gradv` and `v` , provided in that order, would result in an assembled `CeedQFunction` that consists of `(1 + dim) x (dim + 1)` matrices at each quadrature point acting on the input ` [u, du_0, du_1]` and producing the output `[dv_0, dv_1, v]`. 1774eaf62fffSJeremy L Thompson 1775ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 1776f04ea552SJeremy L Thompson 1777ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 1778ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points 1779ca94c3ddSJeremy L Thompson @param[out] rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction` 1780ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1781eaf62fffSJeremy L Thompson 1782eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1783eaf62fffSJeremy L Thompson 1784eaf62fffSJeremy L Thompson @ref User 1785eaf62fffSJeremy L Thompson **/ 17862b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 17872b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 1788eaf62fffSJeremy L Thompson 1789eaf62fffSJeremy L Thompson if (op->LinearAssembleQFunction) { 1790d04bbc78SJeremy L Thompson // Backend version 17912b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request)); 1792eaf62fffSJeremy L Thompson } else { 1793d04bbc78SJeremy L Thompson // Operator fallback 17941203703bSJeremy L Thompson Ceed ceed; 1795d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1796d04bbc78SJeremy L Thompson 17971203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 17982b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 17996574a04fSJeremy L Thompson if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request)); 18001203703bSJeremy L Thompson else return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction"); 180170a7ffb3SJeremy L Thompson } 1802eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1803eaf62fffSJeremy L Thompson } 180470a7ffb3SJeremy L Thompson 180570a7ffb3SJeremy L Thompson /** 1806ca94c3ddSJeremy L Thompson @brief Assemble `CeedQFunction` and store result internally. 18074385fb7fSSebastian Grimberg 1808ea61e9acSJeremy L Thompson Return copied references of stored data to the caller. 1809ea61e9acSJeremy L Thompson Caller is responsible for ownership and destruction of the copied references. 1810ca94c3ddSJeremy L Thompson See also @ref CeedOperatorLinearAssembleQFunction(). 181170a7ffb3SJeremy L Thompson 1812ca94c3ddSJeremy 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. 1813c5f45aeaSJeremy L Thompson These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object. 1814c5f45aeaSJeremy L Thompson 1815ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 1816ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points 1817ca94c3ddSJeremy L Thompson @param[out] rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction` 1818ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 181970a7ffb3SJeremy L Thompson 182070a7ffb3SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 182170a7ffb3SJeremy L Thompson 182270a7ffb3SJeremy L Thompson @ref User 182370a7ffb3SJeremy L Thompson **/ 18242b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 1825b05f7e9fSJeremy L Thompson int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *) = NULL; 1826b05f7e9fSJeremy L Thompson CeedOperator op_assemble = NULL; 1827bb229da9SJeremy L Thompson CeedOperator op_fallback_parent = NULL; 1828b05f7e9fSJeremy L Thompson 18292b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 183070a7ffb3SJeremy L Thompson 1831b05f7e9fSJeremy L Thompson // Determine if fallback parent or operator has implementation 1832bb229da9SJeremy L Thompson CeedCall(CeedOperatorGetFallbackParent(op, &op_fallback_parent)); 1833bb229da9SJeremy L Thompson if (op_fallback_parent && op_fallback_parent->LinearAssembleQFunctionUpdate) { 1834b05f7e9fSJeremy L Thompson // -- Backend version for op fallback parent is faster, if it exists 1835bb229da9SJeremy L Thompson LinearAssembleQFunctionUpdate = op_fallback_parent->LinearAssembleQFunctionUpdate; 1836bb229da9SJeremy L Thompson op_assemble = op_fallback_parent; 1837b05f7e9fSJeremy L Thompson } else if (op->LinearAssembleQFunctionUpdate) { 1838b05f7e9fSJeremy L Thompson // -- Backend version for op 1839b05f7e9fSJeremy L Thompson LinearAssembleQFunctionUpdate = op->LinearAssembleQFunctionUpdate; 1840b05f7e9fSJeremy L Thompson op_assemble = op; 1841b05f7e9fSJeremy L Thompson } 1842b05f7e9fSJeremy L Thompson 1843b05f7e9fSJeremy L Thompson // Assemble QFunction 1844b05f7e9fSJeremy L Thompson if (LinearAssembleQFunctionUpdate) { 1845b05f7e9fSJeremy L Thompson // Backend or fallback parent version 1846480fae85SJeremy L Thompson bool qf_assembled_is_setup; 18472efa2d85SJeremy L Thompson CeedVector assembled_vec = NULL; 18482efa2d85SJeremy L Thompson CeedElemRestriction assembled_rstr = NULL; 1849480fae85SJeremy L Thompson 18502b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataIsSetup(op->qf_assembled, &qf_assembled_is_setup)); 1851480fae85SJeremy L Thompson if (qf_assembled_is_setup) { 1852d04bbc78SJeremy L Thompson bool update_needed; 1853d04bbc78SJeremy L Thompson 18542b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataGetObjects(op->qf_assembled, &assembled_vec, &assembled_rstr)); 18552b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(op->qf_assembled, &update_needed)); 1856b05f7e9fSJeremy L Thompson if (update_needed) CeedCall(LinearAssembleQFunctionUpdate(op_assemble, assembled_vec, assembled_rstr, request)); 185770a7ffb3SJeremy L Thompson } else { 1858b05f7e9fSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleQFunction(op_assemble, &assembled_vec, &assembled_rstr, request)); 18592b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataSetObjects(op->qf_assembled, assembled_vec, assembled_rstr)); 186070a7ffb3SJeremy L Thompson } 18612b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, false)); 18622efa2d85SJeremy L Thompson 1863d04bbc78SJeremy L Thompson // Copy reference from internally held copy 18642b730f8bSJeremy L Thompson CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled)); 18652b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr)); 1866c5f45aeaSJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled_vec)); 18672b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&assembled_rstr)); 186870a7ffb3SJeremy L Thompson } else { 1869d04bbc78SJeremy L Thompson // Operator fallback 18701203703bSJeremy L Thompson Ceed ceed; 1871d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1872d04bbc78SJeremy L Thompson 18731203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 18742b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 18756574a04fSJeremy L Thompson if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request)); 18761203703bSJeremy L Thompson else return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate"); 187770a7ffb3SJeremy L Thompson } 187870a7ffb3SJeremy L Thompson return CEED_ERROR_SUCCESS; 1879eaf62fffSJeremy L Thompson } 1880eaf62fffSJeremy L Thompson 1881eaf62fffSJeremy L Thompson /** 1882ca94c3ddSJeremy L Thompson @brief Assemble the diagonal of a square linear `CeedOperator` 1883eaf62fffSJeremy L Thompson 1884ca94c3ddSJeremy L Thompson This overwrites a `CeedVector` with the diagonal of a linear `CeedOperator`. 1885eaf62fffSJeremy L Thompson 1886ca94c3ddSJeremy L Thompson Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported. 1887eaf62fffSJeremy L Thompson 1888ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 1889f04ea552SJeremy L Thompson 1890ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 1891ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal 1892ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1893eaf62fffSJeremy L Thompson 1894eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1895eaf62fffSJeremy L Thompson 1896eaf62fffSJeremy L Thompson @ref User 1897eaf62fffSJeremy L Thompson **/ 18982b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 1899f3d47e36SJeremy L Thompson bool is_composite; 19001c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 19011203703bSJeremy L Thompson Ceed ceed; 19021c66c397SJeremy L Thompson 19031203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 19042b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 1905f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1906eaf62fffSJeremy L Thompson 19072b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 19081203703bSJeremy L Thompson CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 1909c9366a6bSJeremy L Thompson 1910f3d47e36SJeremy L Thompson // Early exit for empty operator 1911f3d47e36SJeremy L Thompson if (!is_composite) { 1912f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 1913f3d47e36SJeremy L Thompson 1914f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 1915f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 1916f3d47e36SJeremy L Thompson } 1917f3d47e36SJeremy L Thompson 1918eaf62fffSJeremy L Thompson if (op->LinearAssembleDiagonal) { 1919d04bbc78SJeremy L Thompson // Backend version 19202b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleDiagonal(op, assembled, request)); 1921eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1922eaf62fffSJeremy L Thompson } else if (op->LinearAssembleAddDiagonal) { 1923d04bbc78SJeremy L Thompson // Backend version with zeroing first 19242b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 19252b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request)); 1926eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1927eaf62fffSJeremy L Thompson } else { 1928d04bbc78SJeremy L Thompson // Operator fallback 1929d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1930d04bbc78SJeremy L Thompson 19312b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 1932d04bbc78SJeremy L Thompson if (op_fallback) { 19332b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request)); 1934eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1935eaf62fffSJeremy L Thompson } 1936eaf62fffSJeremy L Thompson } 1937eaf62fffSJeremy L Thompson // Default interface implementation 19382b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 19392b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request)); 1940eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1941eaf62fffSJeremy L Thompson } 1942eaf62fffSJeremy L Thompson 1943eaf62fffSJeremy L Thompson /** 1944ca94c3ddSJeremy L Thompson @brief Assemble the diagonal of a square linear `CeedOperator`. 1945eaf62fffSJeremy L Thompson 1946ca94c3ddSJeremy L Thompson This sums into a `CeedVector` the diagonal of a linear `CeedOperator`. 1947eaf62fffSJeremy L Thompson 1948ca94c3ddSJeremy L Thompson Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported. 1949eaf62fffSJeremy L Thompson 1950ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 1951f04ea552SJeremy L Thompson 1952ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 1953ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal 1954ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1955eaf62fffSJeremy L Thompson 1956eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1957eaf62fffSJeremy L Thompson 1958eaf62fffSJeremy L Thompson @ref User 1959eaf62fffSJeremy L Thompson **/ 19602b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 1961f3d47e36SJeremy L Thompson bool is_composite; 19621c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 19631203703bSJeremy L Thompson Ceed ceed; 19641c66c397SJeremy L Thompson 19651203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 19662b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 1967f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1968eaf62fffSJeremy L Thompson 19692b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 19701203703bSJeremy L Thompson CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 1971c9366a6bSJeremy L Thompson 1972f3d47e36SJeremy L Thompson // Early exit for empty operator 1973f3d47e36SJeremy L Thompson if (!is_composite) { 1974f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 1975f3d47e36SJeremy L Thompson 1976f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 1977f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 1978f3d47e36SJeremy L Thompson } 1979f3d47e36SJeremy L Thompson 1980eaf62fffSJeremy L Thompson if (op->LinearAssembleAddDiagonal) { 1981d04bbc78SJeremy L Thompson // Backend version 19822b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request)); 1983eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1984eaf62fffSJeremy L Thompson } else { 1985d04bbc78SJeremy L Thompson // Operator fallback 1986d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1987d04bbc78SJeremy L Thompson 19882b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 1989d04bbc78SJeremy L Thompson if (op_fallback) { 19902b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request)); 1991eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1992eaf62fffSJeremy L Thompson } 1993eaf62fffSJeremy L Thompson } 1994eaf62fffSJeremy L Thompson // Default interface implementation 1995eaf62fffSJeremy L Thompson if (is_composite) { 19962b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled)); 1997eaf62fffSJeremy L Thompson } else { 19982b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, false, assembled)); 1999eaf62fffSJeremy L Thompson } 2000d04bbc78SJeremy L Thompson return CEED_ERROR_SUCCESS; 2001eaf62fffSJeremy L Thompson } 2002eaf62fffSJeremy L Thompson 2003eaf62fffSJeremy L Thompson /** 2004ca94c3ddSJeremy L Thompson @brief Fully assemble the point-block diagonal pattern of a linear `CeedOperator`. 200501f0e615SJames Wright 2006ca94c3ddSJeremy L Thompson Expected to be used in conjunction with @ref CeedOperatorLinearAssemblePointBlockDiagonal(). 200701f0e615SJames Wright 2008ca94c3ddSJeremy L Thompson The assembly routines use coordinate format, with `num_entries` tuples of the form `(i, j, value)` which indicate that value should be added to the matrix in entry `(i, j)`. 2009ca94c3ddSJeremy L Thompson Note that the `(i, j)` pairs are unique. 2010ca94c3ddSJeremy L Thompson This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemblePointBlockDiagonal() provides the values in the same ordering. 201101f0e615SJames Wright 201201f0e615SJames Wright This will generally be slow unless your operator is low-order. 201301f0e615SJames Wright 2014ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 201501f0e615SJames Wright 2016ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 201701f0e615SJames Wright @param[out] num_entries Number of entries in coordinate nonzero pattern 201801f0e615SJames Wright @param[out] rows Row number for each entry 201901f0e615SJames Wright @param[out] cols Column number for each entry 202001f0e615SJames Wright 202101f0e615SJames Wright @ref User 202201f0e615SJames Wright **/ 202301f0e615SJames Wright int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) { 202401f0e615SJames Wright Ceed ceed; 202501f0e615SJames Wright bool is_composite; 202601f0e615SJames Wright CeedInt num_active_components, num_sub_operators; 202701f0e615SJames Wright CeedOperator *sub_operators; 202801f0e615SJames Wright 202901f0e615SJames Wright CeedCall(CeedOperatorGetCeed(op, &ceed)); 203001f0e615SJames Wright CeedCall(CeedOperatorIsComposite(op, &is_composite)); 203101f0e615SJames Wright 203201f0e615SJames Wright CeedSize input_size = 0, output_size = 0; 203301f0e615SJames Wright CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 203401f0e615SJames Wright CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 203501f0e615SJames Wright 203601f0e615SJames Wright if (is_composite) { 203701f0e615SJames Wright CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_operators)); 203801f0e615SJames Wright CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 203901f0e615SJames Wright } else { 204001f0e615SJames Wright sub_operators = &op; 204101f0e615SJames Wright num_sub_operators = 1; 204201f0e615SJames Wright } 204301f0e615SJames Wright 2044506b1a0cSSebastian Grimberg // Verify operator can be assembled correctly 2045506b1a0cSSebastian Grimberg { 204601f0e615SJames Wright CeedOperatorAssemblyData data; 2047506b1a0cSSebastian Grimberg CeedInt num_active_elem_rstrs, comp_stride; 204801f0e615SJames Wright CeedElemRestriction *active_elem_rstrs; 204901f0e615SJames Wright 205001f0e615SJames Wright // Get initial values to check against 205101f0e615SJames Wright CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[0], &data)); 2052506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL)); 205301f0e615SJames Wright CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[0], &comp_stride)); 205401f0e615SJames Wright CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[0], &num_active_components)); 205501f0e615SJames Wright 2056506b1a0cSSebastian Grimberg // Verify that all active element restrictions have same component stride and number of components 205701f0e615SJames Wright for (CeedInt k = 0; k < num_sub_operators; k++) { 205801f0e615SJames Wright CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[k], &data)); 2059506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL)); 206001f0e615SJames Wright for (CeedInt i = 0; i < num_active_elem_rstrs; i++) { 2061506b1a0cSSebastian Grimberg CeedInt comp_stride_sub, num_active_components_sub; 2062506b1a0cSSebastian Grimberg 206301f0e615SJames Wright CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[i], &comp_stride_sub)); 206401f0e615SJames Wright CeedCheck(comp_stride == comp_stride_sub, ceed, CEED_ERROR_DIMENSION, 206501f0e615SJames Wright "Active element restrictions must have the same component stride: %d vs %d", comp_stride, comp_stride_sub); 206601f0e615SJames Wright CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[i], &num_active_components_sub)); 206701f0e615SJames Wright CeedCheck(num_active_components == num_active_components_sub, ceed, CEED_ERROR_INCOMPATIBLE, 206801f0e615SJames Wright "All suboperators must have the same number of output components"); 206901f0e615SJames Wright } 207001f0e615SJames Wright } 207101f0e615SJames Wright } 207201f0e615SJames Wright *num_entries = input_size * num_active_components; 207301f0e615SJames Wright CeedCall(CeedCalloc(*num_entries, rows)); 207401f0e615SJames Wright CeedCall(CeedCalloc(*num_entries, cols)); 207501f0e615SJames Wright 207601f0e615SJames Wright for (CeedInt o = 0; o < num_sub_operators; o++) { 2077506b1a0cSSebastian Grimberg CeedElemRestriction active_elem_rstr, point_block_active_elem_rstr; 207801f0e615SJames Wright CeedInt comp_stride, num_elem, elem_size; 2079506b1a0cSSebastian Grimberg const CeedInt *offsets, *point_block_offsets; 208001f0e615SJames Wright 208101f0e615SJames Wright CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[o], &active_elem_rstr)); 208201f0e615SJames Wright CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstr, &comp_stride)); 208301f0e615SJames Wright CeedCall(CeedElemRestrictionGetNumElements(active_elem_rstr, &num_elem)); 208401f0e615SJames Wright CeedCall(CeedElemRestrictionGetElementSize(active_elem_rstr, &elem_size)); 208501f0e615SJames Wright CeedCall(CeedElemRestrictionGetOffsets(active_elem_rstr, CEED_MEM_HOST, &offsets)); 208601f0e615SJames Wright 2087506b1a0cSSebastian Grimberg CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstr, &point_block_active_elem_rstr)); 2088506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOffsets(point_block_active_elem_rstr, CEED_MEM_HOST, &point_block_offsets)); 208901f0e615SJames Wright 209001f0e615SJames Wright for (CeedSize i = 0; i < num_elem * elem_size; i++) { 209101f0e615SJames Wright for (CeedInt c_out = 0; c_out < num_active_components; c_out++) { 209201f0e615SJames Wright for (CeedInt c_in = 0; c_in < num_active_components; c_in++) { 2093506b1a0cSSebastian Grimberg (*rows)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_out * comp_stride; 2094506b1a0cSSebastian Grimberg (*cols)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_in * comp_stride; 209501f0e615SJames Wright } 209601f0e615SJames Wright } 209701f0e615SJames Wright } 209801f0e615SJames Wright 209901f0e615SJames Wright CeedCall(CeedElemRestrictionRestoreOffsets(active_elem_rstr, &offsets)); 2100506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOffsets(point_block_active_elem_rstr, &point_block_offsets)); 2101506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&point_block_active_elem_rstr)); 210201f0e615SJames Wright } 210301f0e615SJames Wright return CEED_ERROR_SUCCESS; 210401f0e615SJames Wright } 210501f0e615SJames Wright 210601f0e615SJames Wright /** 2107ca94c3ddSJeremy L Thompson @brief Assemble the point block diagonal of a square linear `CeedOperator`. 2108eaf62fffSJeremy L Thompson 2109ca94c3ddSJeremy L Thompson This overwrites a `CeedVector` with the point block diagonal of a linear `CeedOperator`. 2110eaf62fffSJeremy L Thompson 2111ca94c3ddSJeremy L Thompson Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported. 2112eaf62fffSJeremy L Thompson 2113ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2114f04ea552SJeremy L Thompson 2115ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 2116ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedOperator` point block diagonal, provided in row-major form with an `num_comp * num_comp` block at each node. 2117ca94c3ddSJeremy L Thompson The dimensions of this vector are derived from the active vector for the `CeedOperator`. 2118ca94c3ddSJeremy L Thompson The array has shape `[nodes, component out, component in]`. 2119ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2120eaf62fffSJeremy L Thompson 2121eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2122eaf62fffSJeremy L Thompson 2123eaf62fffSJeremy L Thompson @ref User 2124eaf62fffSJeremy L Thompson **/ 21252b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 2126f3d47e36SJeremy L Thompson bool is_composite; 21271c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 21281203703bSJeremy L Thompson Ceed ceed; 21291c66c397SJeremy L Thompson 21301203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 21312b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2132f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2133eaf62fffSJeremy L Thompson 21342b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 21351203703bSJeremy L Thompson CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 2136c9366a6bSJeremy L Thompson 2137f3d47e36SJeremy L Thompson // Early exit for empty operator 2138f3d47e36SJeremy L Thompson if (!is_composite) { 2139f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 2140f3d47e36SJeremy L Thompson 2141f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 2142f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 2143f3d47e36SJeremy L Thompson } 2144f3d47e36SJeremy L Thompson 2145eaf62fffSJeremy L Thompson if (op->LinearAssemblePointBlockDiagonal) { 2146d04bbc78SJeremy L Thompson // Backend version 21472b730f8bSJeremy L Thompson CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request)); 2148eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2149eaf62fffSJeremy L Thompson } else if (op->LinearAssembleAddPointBlockDiagonal) { 2150d04bbc78SJeremy L Thompson // Backend version with zeroing first 21512b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 21522b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request)); 2153eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2154eaf62fffSJeremy L Thompson } else { 2155d04bbc78SJeremy L Thompson // Operator fallback 2156d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2157d04bbc78SJeremy L Thompson 21582b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2159d04bbc78SJeremy L Thompson if (op_fallback) { 21602b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request)); 2161eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2162eaf62fffSJeremy L Thompson } 2163eaf62fffSJeremy L Thompson } 2164eaf62fffSJeremy L Thompson // Default interface implementation 21652b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 21662b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request)); 2167eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2168eaf62fffSJeremy L Thompson } 2169eaf62fffSJeremy L Thompson 2170eaf62fffSJeremy L Thompson /** 2171ca94c3ddSJeremy L Thompson @brief Assemble the point block diagonal of a square linear `CeedOperator`. 2172eaf62fffSJeremy L Thompson 2173ca94c3ddSJeremy L Thompson This sums into a `CeedVector` with the point block diagonal of a linear `CeedOperator`. 2174eaf62fffSJeremy L Thompson 2175ca94c3ddSJeremy L Thompson Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported. 2176eaf62fffSJeremy L Thompson 2177ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2178f04ea552SJeremy L Thompson 2179ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 2180ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled CeedOperator point block diagonal, provided in row-major form with an `num_comp * num_comp` block at each node. 2181ca94c3ddSJeremy L Thompson The dimensions of this vector are derived from the active vector for the `CeedOperator`. 2182ca94c3ddSJeremy L Thompson The array has shape `[nodes, component out, component in]`. 2183ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2184eaf62fffSJeremy L Thompson 2185eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2186eaf62fffSJeremy L Thompson 2187eaf62fffSJeremy L Thompson @ref User 2188eaf62fffSJeremy L Thompson **/ 21892b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 2190f3d47e36SJeremy L Thompson bool is_composite; 21911c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 21921203703bSJeremy L Thompson Ceed ceed; 21931c66c397SJeremy L Thompson 21941203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 21952b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2196f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2197eaf62fffSJeremy L Thompson 21982b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 21991203703bSJeremy L Thompson CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 2200c9366a6bSJeremy L Thompson 2201f3d47e36SJeremy L Thompson // Early exit for empty operator 2202f3d47e36SJeremy L Thompson if (!is_composite) { 2203f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 2204f3d47e36SJeremy L Thompson 2205f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 2206f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 2207f3d47e36SJeremy L Thompson } 2208f3d47e36SJeremy L Thompson 2209eaf62fffSJeremy L Thompson if (op->LinearAssembleAddPointBlockDiagonal) { 2210d04bbc78SJeremy L Thompson // Backend version 22112b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request)); 2212eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2213eaf62fffSJeremy L Thompson } else { 2214d04bbc78SJeremy L Thompson // Operator fallback 2215d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2216d04bbc78SJeremy L Thompson 22172b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2218d04bbc78SJeremy L Thompson if (op_fallback) { 22192b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request)); 2220eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2221eaf62fffSJeremy L Thompson } 2222eaf62fffSJeremy L Thompson } 2223ea61e9acSJeremy L Thompson // Default interface implementation 2224eaf62fffSJeremy L Thompson if (is_composite) { 22252b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled)); 2226eaf62fffSJeremy L Thompson } else { 22272b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssembleAddDiagonal_Core(op, request, true, assembled)); 2228eaf62fffSJeremy L Thompson } 2229d04bbc78SJeremy L Thompson return CEED_ERROR_SUCCESS; 2230eaf62fffSJeremy L Thompson } 2231eaf62fffSJeremy L Thompson 2232eaf62fffSJeremy L Thompson /** 2233ca94c3ddSJeremy L Thompson @brief Fully assemble the nonzero pattern of a linear `CeedOperator`. 2234eaf62fffSJeremy L Thompson 2235ca94c3ddSJeremy L Thompson Expected to be used in conjunction with @ref CeedOperatorLinearAssemble(). 2236eaf62fffSJeremy L Thompson 2237ca94c3ddSJeremy L Thompson The assembly routines use coordinate format, with `num_entries` tuples of the form `(i, j, value)` which indicate that value should be added to the matrix in entry `(i, j)`. 2238ca94c3ddSJeremy L Thompson Note that the `(i, j)` pairs are not unique and may repeat. 2239ca94c3ddSJeremy L Thompson This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemble() provides the values in the same ordering. 2240eaf62fffSJeremy L Thompson 2241eaf62fffSJeremy L Thompson This will generally be slow unless your operator is low-order. 2242eaf62fffSJeremy L Thompson 2243ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2244f04ea552SJeremy L Thompson 2245ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 2246eaf62fffSJeremy L Thompson @param[out] num_entries Number of entries in coordinate nonzero pattern 2247eaf62fffSJeremy L Thompson @param[out] rows Row number for each entry 2248eaf62fffSJeremy L Thompson @param[out] cols Column number for each entry 2249eaf62fffSJeremy L Thompson 2250eaf62fffSJeremy L Thompson @ref User 2251eaf62fffSJeremy L Thompson **/ 22522b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) { 22531c66c397SJeremy L Thompson bool is_composite; 22541c66c397SJeremy L Thompson CeedInt num_suboperators, offset = 0; 2255b94338b9SJed Brown CeedSize single_entries; 2256eaf62fffSJeremy L Thompson CeedOperator *sub_operators; 22571c66c397SJeremy L Thompson 22582b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2259f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2260eaf62fffSJeremy L Thompson 2261eaf62fffSJeremy L Thompson if (op->LinearAssembleSymbolic) { 2262d04bbc78SJeremy L Thompson // Backend version 22632b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols)); 2264eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2265eaf62fffSJeremy L Thompson } else { 2266d04bbc78SJeremy L Thompson // Operator fallback 2267d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2268d04bbc78SJeremy L Thompson 22692b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2270d04bbc78SJeremy L Thompson if (op_fallback) { 22712b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols)); 2272eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2273eaf62fffSJeremy L Thompson } 2274eaf62fffSJeremy L Thompson } 2275eaf62fffSJeremy L Thompson 2276eaf62fffSJeremy L Thompson // Default interface implementation 2277eaf62fffSJeremy L Thompson 2278506b1a0cSSebastian Grimberg // Count entries and allocate rows, cols arrays 2279eaf62fffSJeremy L Thompson *num_entries = 0; 2280eaf62fffSJeremy L Thompson if (is_composite) { 2281c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2282c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 228392ae7e47SJeremy L Thompson for (CeedInt k = 0; k < num_suboperators; ++k) { 22842b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries)); 2285eaf62fffSJeremy L Thompson *num_entries += single_entries; 2286eaf62fffSJeremy L Thompson } 2287eaf62fffSJeremy L Thompson } else { 22882b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries)); 2289eaf62fffSJeremy L Thompson *num_entries += single_entries; 2290eaf62fffSJeremy L Thompson } 22912b730f8bSJeremy L Thompson CeedCall(CeedCalloc(*num_entries, rows)); 22922b730f8bSJeremy L Thompson CeedCall(CeedCalloc(*num_entries, cols)); 2293eaf62fffSJeremy L Thompson 2294506b1a0cSSebastian Grimberg // Assemble nonzero locations 2295eaf62fffSJeremy L Thompson if (is_composite) { 2296c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2297c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 229892ae7e47SJeremy L Thompson for (CeedInt k = 0; k < num_suboperators; ++k) { 22992b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols)); 23002b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries)); 2301eaf62fffSJeremy L Thompson offset += single_entries; 2302eaf62fffSJeremy L Thompson } 2303eaf62fffSJeremy L Thompson } else { 23042b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols)); 2305eaf62fffSJeremy L Thompson } 2306eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2307eaf62fffSJeremy L Thompson } 2308eaf62fffSJeremy L Thompson 2309eaf62fffSJeremy L Thompson /** 2310eaf62fffSJeremy L Thompson @brief Fully assemble the nonzero entries of a linear operator. 2311eaf62fffSJeremy L Thompson 2312ca94c3ddSJeremy L Thompson Expected to be used in conjunction with @ref CeedOperatorLinearAssembleSymbolic(). 2313eaf62fffSJeremy L Thompson 2314ca94c3ddSJeremy L Thompson The assembly routines use coordinate format, with `num_entries` tuples of the form `(i, j, value)` which indicate that value should be added to the matrix in entry `(i, j)`. 2315ca94c3ddSJeremy L Thompson Note that the `(i, j)` pairs are not unique and may repeat. 2316ca94c3ddSJeremy L Thompson This function returns the values of the nonzero entries to be added, their `(i, j)` locations are provided by @ref CeedOperatorLinearAssembleSymbolic(). 2317eaf62fffSJeremy L Thompson 2318eaf62fffSJeremy L Thompson This will generally be slow unless your operator is low-order. 2319eaf62fffSJeremy L Thompson 2320ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2321f04ea552SJeremy L Thompson 2322ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 2323eaf62fffSJeremy L Thompson @param[out] values Values to assemble into matrix 2324eaf62fffSJeremy L Thompson 2325eaf62fffSJeremy L Thompson @ref User 2326eaf62fffSJeremy L Thompson **/ 2327eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) { 23281c66c397SJeremy L Thompson bool is_composite; 23291c66c397SJeremy L Thompson CeedInt num_suboperators, offset = 0; 2330b94338b9SJed Brown CeedSize single_entries = 0; 2331eaf62fffSJeremy L Thompson CeedOperator *sub_operators; 23321c66c397SJeremy L Thompson 23332b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2334f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2335f3d47e36SJeremy L Thompson 2336f3d47e36SJeremy L Thompson // Early exit for empty operator 2337f3d47e36SJeremy L Thompson if (!is_composite) { 2338f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 2339f3d47e36SJeremy L Thompson 2340f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 2341f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 2342f3d47e36SJeremy L Thompson } 2343eaf62fffSJeremy L Thompson 2344eaf62fffSJeremy L Thompson if (op->LinearAssemble) { 2345d04bbc78SJeremy L Thompson // Backend version 23462b730f8bSJeremy L Thompson CeedCall(op->LinearAssemble(op, values)); 2347eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2348eaf62fffSJeremy L Thompson } else { 2349d04bbc78SJeremy L Thompson // Operator fallback 2350d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2351d04bbc78SJeremy L Thompson 23522b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2353d04bbc78SJeremy L Thompson if (op_fallback) { 23542b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssemble(op_fallback, values)); 2355eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2356eaf62fffSJeremy L Thompson } 2357eaf62fffSJeremy L Thompson } 2358eaf62fffSJeremy L Thompson 2359eaf62fffSJeremy L Thompson // Default interface implementation 236028ec399dSJeremy L Thompson CeedCall(CeedVectorSetValue(values, 0.0)); 2361eaf62fffSJeremy L Thompson if (is_composite) { 2362c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2363c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 2364cefa2673SJeremy L Thompson for (CeedInt k = 0; k < num_suboperators; k++) { 23652b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values)); 23662b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries)); 2367eaf62fffSJeremy L Thompson offset += single_entries; 2368eaf62fffSJeremy L Thompson } 2369eaf62fffSJeremy L Thompson } else { 23702b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemble(op, offset, values)); 2371eaf62fffSJeremy L Thompson } 2372eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2373eaf62fffSJeremy L Thompson } 2374eaf62fffSJeremy L Thompson 2375eaf62fffSJeremy L Thompson /** 2376ca94c3ddSJeremy L Thompson @brief Get the multiplicity of nodes across sub-operators in a composite `CeedOperator`. 237775f0d5a4SJeremy L Thompson 2378ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 237975f0d5a4SJeremy L Thompson 2380ca94c3ddSJeremy L Thompson @param[in] op Composite `CeedOperator` 2381ca94c3ddSJeremy L Thompson @param[in] num_skip_indices Number of sub-operators to skip 2382ca94c3ddSJeremy L Thompson @param[in] skip_indices Array of indices of sub-operators to skip 2383ca94c3ddSJeremy L Thompson @param[out] mult Vector to store multiplicity (of size `l_size` ) 238475f0d5a4SJeremy L Thompson 238575f0d5a4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 238675f0d5a4SJeremy L Thompson 238775f0d5a4SJeremy L Thompson @ref User 238875f0d5a4SJeremy L Thompson **/ 238975f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) { 239075f0d5a4SJeremy L Thompson Ceed ceed; 2391b275c451SJeremy L Thompson CeedInt num_suboperators; 239275f0d5a4SJeremy L Thompson CeedSize l_vec_len; 239375f0d5a4SJeremy L Thompson CeedScalar *mult_array; 239475f0d5a4SJeremy L Thompson CeedVector ones_l_vec; 23957c1dbaffSSebastian Grimberg CeedElemRestriction elem_rstr, mult_elem_rstr; 2396b275c451SJeremy L Thompson CeedOperator *sub_operators; 239775f0d5a4SJeremy L Thompson 23981c66c397SJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 23991c66c397SJeremy L Thompson 240075f0d5a4SJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 240175f0d5a4SJeremy L Thompson 240275f0d5a4SJeremy L Thompson // Zero mult vector 240375f0d5a4SJeremy L Thompson CeedCall(CeedVectorSetValue(mult, 0.0)); 240475f0d5a4SJeremy L Thompson 240575f0d5a4SJeremy L Thompson // Get suboperators 2406b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2407b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 2408b275c451SJeremy L Thompson if (num_suboperators == 0) return CEED_ERROR_SUCCESS; 240975f0d5a4SJeremy L Thompson 241075f0d5a4SJeremy L Thompson // Work vector 241175f0d5a4SJeremy L Thompson CeedCall(CeedVectorGetLength(mult, &l_vec_len)); 241275f0d5a4SJeremy L Thompson CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec)); 241375f0d5a4SJeremy L Thompson CeedCall(CeedVectorSetValue(ones_l_vec, 1.0)); 241475f0d5a4SJeremy L Thompson CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array)); 241575f0d5a4SJeremy L Thompson 241675f0d5a4SJeremy L Thompson // Compute multiplicity across suboperators 2417b275c451SJeremy L Thompson for (CeedInt i = 0; i < num_suboperators; i++) { 241875f0d5a4SJeremy L Thompson const CeedScalar *sub_mult_array; 241975f0d5a4SJeremy L Thompson CeedVector sub_mult_l_vec, ones_e_vec; 242075f0d5a4SJeremy L Thompson 242175f0d5a4SJeremy L Thompson // -- Check for suboperator to skip 242275f0d5a4SJeremy L Thompson for (CeedInt j = 0; j < num_skip_indices; j++) { 242375f0d5a4SJeremy L Thompson if (skip_indices[j] == i) continue; 242475f0d5a4SJeremy L Thompson } 242575f0d5a4SJeremy L Thompson 242675f0d5a4SJeremy L Thompson // -- Sub operator multiplicity 2427437c7c90SJeremy L Thompson CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr)); 24287c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr, &mult_elem_rstr)); 24297c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionCreateVector(mult_elem_rstr, &sub_mult_l_vec, &ones_e_vec)); 243075f0d5a4SJeremy L Thompson CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0)); 24317c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE)); 24327c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE)); 243375f0d5a4SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array)); 243475f0d5a4SJeremy L Thompson // ---- Flag every node present in the current suboperator 243575f0d5a4SJeremy L Thompson for (CeedInt j = 0; j < l_vec_len; j++) { 243675f0d5a4SJeremy L Thompson if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0; 243775f0d5a4SJeremy L Thompson } 243875f0d5a4SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array)); 243975f0d5a4SJeremy L Thompson CeedCall(CeedVectorDestroy(&sub_mult_l_vec)); 244075f0d5a4SJeremy L Thompson CeedCall(CeedVectorDestroy(&ones_e_vec)); 24417c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&mult_elem_rstr)); 244275f0d5a4SJeremy L Thompson } 244375f0d5a4SJeremy L Thompson CeedCall(CeedVectorRestoreArray(mult, &mult_array)); 2444811d0ccfSJeremy L Thompson CeedCall(CeedVectorDestroy(&ones_l_vec)); 244575f0d5a4SJeremy L Thompson return CEED_ERROR_SUCCESS; 244675f0d5a4SJeremy L Thompson } 244775f0d5a4SJeremy L Thompson 244875f0d5a4SJeremy L Thompson /** 2449ca94c3ddSJeremy L Thompson @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator`, creating the prolongation basis from the fine and coarse grid interpolation. 2450eaf62fffSJeremy L Thompson 2451ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable. 2452f04ea552SJeremy L Thompson 2453ca94c3ddSJeremy L Thompson @param[in] op_fine Fine grid `CeedOperator` 2454ca94c3ddSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator` 2455ca94c3ddSJeremy L Thompson @param[in] rstr_coarse Coarse grid `CeedElemRestriction` 2456ca94c3ddSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector `CeedBasis` 2457ca94c3ddSJeremy L Thompson @param[out] op_coarse Coarse grid `CeedOperator` 2458ca94c3ddSJeremy L Thompson @param[out] op_prolong Coarse to fine `CeedOperator`, or `NULL` 2459ca94c3ddSJeremy L Thompson @param[out] op_restrict Fine to coarse `CeedOperator`, or `NULL` 2460eaf62fffSJeremy L Thompson 2461eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2462eaf62fffSJeremy L Thompson 2463eaf62fffSJeremy L Thompson @ref User 2464eaf62fffSJeremy L Thompson **/ 24652b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 24667758292fSSebastian Grimberg CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) { 24671c66c397SJeremy L Thompson CeedBasis basis_c_to_f = NULL; 24681c66c397SJeremy L Thompson 24692b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fine)); 2470eaf62fffSJeremy L Thompson 247183d6adf3SZach Atkins // Build prolongation matrix, if required 24727758292fSSebastian Grimberg if (op_prolong || op_restrict) { 247383d6adf3SZach Atkins CeedBasis basis_fine; 24741c66c397SJeremy L Thompson 24752b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine)); 24762b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f)); 247783d6adf3SZach Atkins } 2478eaf62fffSJeremy L Thompson 2479f113e5dcSJeremy L Thompson // Core code 24807758292fSSebastian Grimberg CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict)); 2481eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2482eaf62fffSJeremy L Thompson } 2483eaf62fffSJeremy L Thompson 2484eaf62fffSJeremy L Thompson /** 2485ca94c3ddSJeremy L Thompson @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a tensor basis for the active basis. 2486eaf62fffSJeremy L Thompson 2487ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable. 2488f04ea552SJeremy L Thompson 2489ca94c3ddSJeremy L Thompson @param[in] op_fine Fine grid `CeedOperator` 2490ca94c3ddSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator` 2491ca94c3ddSJeremy L Thompson @param[in] rstr_coarse Coarse grid `CeedElemRestriction` 2492ca94c3ddSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector `CeedBasis` 2493ca94c3ddSJeremy L Thompson @param[in] interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator` 2494ca94c3ddSJeremy L Thompson @param[out] op_coarse Coarse grid `CeedOperator` 2495ca94c3ddSJeremy L Thompson @param[out] op_prolong Coarse to fine `CeedOperator`, or `NULL` 2496ca94c3ddSJeremy L Thompson @param[out] op_restrict Fine to coarse `CeedOperator`, or `NULL` 2497eaf62fffSJeremy L Thompson 2498eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2499eaf62fffSJeremy L Thompson 2500eaf62fffSJeremy L Thompson @ref User 2501eaf62fffSJeremy L Thompson **/ 25022b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 25032b730f8bSJeremy L Thompson const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, 25047758292fSSebastian Grimberg CeedOperator *op_restrict) { 2505eaf62fffSJeremy L Thompson Ceed ceed; 25061c66c397SJeremy L Thompson CeedInt Q_f, Q_c; 25071c66c397SJeremy L Thompson CeedBasis basis_fine, basis_c_to_f = NULL; 25081c66c397SJeremy L Thompson 25091c66c397SJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fine)); 25102b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op_fine, &ceed)); 2511eaf62fffSJeremy L Thompson 2512eaf62fffSJeremy L Thompson // Check for compatible quadrature spaces 25132b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine)); 25142b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f)); 25152b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c)); 25166574a04fSJeremy L Thompson CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces"); 2517eaf62fffSJeremy L Thompson 251883d6adf3SZach Atkins // Create coarse to fine basis, if required 25197758292fSSebastian Grimberg if (op_prolong || op_restrict) { 25201c66c397SJeremy L Thompson CeedInt dim, num_comp, num_nodes_c, P_1d_f, P_1d_c; 25211c66c397SJeremy L Thompson CeedScalar *q_ref, *q_weight, *grad; 25221c66c397SJeremy L Thompson 252383d6adf3SZach Atkins // Check if interpolation matrix is provided 25246574a04fSJeremy L Thompson CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE, 25256574a04fSJeremy L Thompson "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix"); 25262b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_fine, &dim)); 25272b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp)); 25282b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f)); 25292b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c)); 25302b730f8bSJeremy L Thompson P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c); 25312b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_f, &q_ref)); 25322b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_f, &q_weight)); 25332b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad)); 25342b730f8bSJeremy 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)); 25352b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref)); 25362b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight)); 25372b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad)); 253883d6adf3SZach Atkins } 2539eaf62fffSJeremy L Thompson 2540eaf62fffSJeremy L Thompson // Core code 25417758292fSSebastian Grimberg CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict)); 2542eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2543eaf62fffSJeremy L Thompson } 2544eaf62fffSJeremy L Thompson 2545eaf62fffSJeremy L Thompson /** 2546ca94c3ddSJeremy L Thompson @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a non-tensor basis for the active vector 2547eaf62fffSJeremy L Thompson 2548ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable. 2549f04ea552SJeremy L Thompson 2550ca94c3ddSJeremy L Thompson @param[in] op_fine Fine grid `CeedOperator` 2551ca94c3ddSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator` 2552ca94c3ddSJeremy L Thompson @param[in] rstr_coarse Coarse grid `CeedElemRestriction` 2553ca94c3ddSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector `CeedBasis` 2554ca94c3ddSJeremy L Thompson @param[in] interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator` 2555ca94c3ddSJeremy L Thompson @param[out] op_coarse Coarse grid `CeedOperator` 2556ca94c3ddSJeremy L Thompson @param[out] op_prolong Coarse to fine `CeedOperator`, or `NULL` 2557ca94c3ddSJeremy L Thompson @param[out] op_restrict Fine to coarse `CeedOperator`, or `NULL` 2558eaf62fffSJeremy L Thompson 2559eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2560eaf62fffSJeremy L Thompson 2561eaf62fffSJeremy L Thompson @ref User 2562eaf62fffSJeremy L Thompson **/ 25632b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 25647758292fSSebastian Grimberg const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, 25657758292fSSebastian Grimberg CeedOperator *op_restrict) { 2566eaf62fffSJeremy L Thompson Ceed ceed; 25671c66c397SJeremy L Thompson CeedInt Q_f, Q_c; 25681c66c397SJeremy L Thompson CeedBasis basis_fine, basis_c_to_f = NULL; 25691c66c397SJeremy L Thompson 25701c66c397SJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fine)); 25712b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op_fine, &ceed)); 2572eaf62fffSJeremy L Thompson 2573eaf62fffSJeremy L Thompson // Check for compatible quadrature spaces 25742b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine)); 25752b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f)); 25762b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c)); 25776574a04fSJeremy L Thompson CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces"); 2578eaf62fffSJeremy L Thompson 2579eaf62fffSJeremy L Thompson // Coarse to fine basis 25807758292fSSebastian Grimberg if (op_prolong || op_restrict) { 25811c66c397SJeremy L Thompson CeedInt dim, num_comp, num_nodes_c, num_nodes_f; 25821c66c397SJeremy L Thompson CeedScalar *q_ref, *q_weight, *grad; 25831c66c397SJeremy L Thompson CeedElemTopology topo; 25841c66c397SJeremy L Thompson 258583d6adf3SZach Atkins // Check if interpolation matrix is provided 25866574a04fSJeremy L Thompson CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE, 25876574a04fSJeremy L Thompson "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix"); 25882b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopology(basis_fine, &topo)); 25892b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_fine, &dim)); 25902b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp)); 25912b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f)); 25922b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c)); 25932b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref)); 25942b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_f, &q_weight)); 25952b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad)); 25962b730f8bSJeremy 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)); 25972b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref)); 25982b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight)); 25992b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad)); 260083d6adf3SZach Atkins } 2601eaf62fffSJeremy L Thompson 2602eaf62fffSJeremy L Thompson // Core code 26037758292fSSebastian Grimberg CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict)); 2604eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2605eaf62fffSJeremy L Thompson } 2606eaf62fffSJeremy L Thompson 2607eaf62fffSJeremy L Thompson /** 2608ca94c3ddSJeremy L Thompson @brief Build a FDM based approximate inverse for each element for a `CeedOperator`. 2609eaf62fffSJeremy L Thompson 2610ca94c3ddSJeremy L Thompson This returns a `CeedOperator` and `CeedVector` to apply a Fast Diagonalization Method based approximate inverse. 2611859c15bbSJames 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$. 2612ca94c3ddSJeremy L Thompson The assembled `CeedQFunction` is used to modify the eigenvalues from simultaneous diagonalization and obtain an approximate inverse of the form \f$V^T \hat S V\f$. 2613ca94c3ddSJeremy L Thompson The `CeedOperator` must be linear and non-composite. 2614ca94c3ddSJeremy L Thompson The associated `CeedQFunction` must therefore also be linear. 2615eaf62fffSJeremy L Thompson 2616ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2617f04ea552SJeremy L Thompson 2618ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to create element inverses 2619ca94c3ddSJeremy L Thompson @param[out] fdm_inv `CeedOperator` to apply the action of a FDM based inverse for each element 2620ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2621eaf62fffSJeremy L Thompson 2622eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2623eaf62fffSJeremy L Thompson 2624480fae85SJeremy L Thompson @ref User 2625eaf62fffSJeremy L Thompson **/ 26262b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) { 26271c66c397SJeremy L Thompson Ceed ceed, ceed_parent; 26281c66c397SJeremy L Thompson bool interp = false, grad = false, is_tensor_basis = true; 26291c66c397SJeremy L Thompson CeedInt num_input_fields, P_1d, Q_1d, num_nodes, num_qpts, dim, num_comp = 1, num_elem = 1; 26301c66c397SJeremy L Thompson CeedSize l_size = 1; 26311c66c397SJeremy L Thompson CeedScalar *mass, *laplace, *x, *fdm_interp, *lambda, *elem_avg; 26321c66c397SJeremy L Thompson const CeedScalar *interp_1d, *grad_1d, *q_weight_1d; 26331c66c397SJeremy L Thompson CeedVector q_data; 26341c66c397SJeremy L Thompson CeedElemRestriction rstr = NULL, rstr_qd_i; 26351c66c397SJeremy L Thompson CeedBasis basis = NULL, fdm_basis; 26361c66c397SJeremy L Thompson CeedQFunctionContext ctx_fdm; 26371c66c397SJeremy L Thompson CeedQFunctionField *qf_fields; 26381c66c397SJeremy L Thompson CeedQFunction qf, qf_fdm; 26391c66c397SJeremy L Thompson CeedOperatorField *op_fields; 26401c66c397SJeremy L Thompson 26412b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2642eaf62fffSJeremy L Thompson 2643eaf62fffSJeremy L Thompson if (op->CreateFDMElementInverse) { 2644d04bbc78SJeremy L Thompson // Backend version 26452b730f8bSJeremy L Thompson CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request)); 2646eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2647eaf62fffSJeremy L Thompson } else { 2648d04bbc78SJeremy L Thompson // Operator fallback 2649d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2650d04bbc78SJeremy L Thompson 26512b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2652d04bbc78SJeremy L Thompson if (op_fallback) { 26532b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request)); 2654eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2655eaf62fffSJeremy L Thompson } 2656eaf62fffSJeremy L Thompson } 2657eaf62fffSJeremy L Thompson 2658d04bbc78SJeremy L Thompson // Default interface implementation 26592b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 2660bb229da9SJeremy L Thompson CeedCall(CeedOperatorGetFallbackParentCeed(op, &ceed_parent)); 26612b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetQFunction(op, &qf)); 2662eaf62fffSJeremy L Thompson 2663eaf62fffSJeremy L Thompson // Determine active input basis 26642b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL)); 26652b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 2666eaf62fffSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 2667eaf62fffSJeremy L Thompson CeedVector vec; 26681c66c397SJeremy L Thompson 26692b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec)); 2670eaf62fffSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 2671eaf62fffSJeremy L Thompson CeedEvalMode eval_mode; 26721c66c397SJeremy L Thompson 26732b730f8bSJeremy L Thompson CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 2674eaf62fffSJeremy L Thompson interp = interp || eval_mode == CEED_EVAL_INTERP; 2675eaf62fffSJeremy L Thompson grad = grad || eval_mode == CEED_EVAL_GRAD; 26762b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 26772b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr)); 2678eaf62fffSJeremy L Thompson } 2679eaf62fffSJeremy L Thompson } 26806574a04fSJeremy L Thompson CeedCheck(basis, ceed, CEED_ERROR_BACKEND, "No active field set"); 26812b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 2682352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 26832b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 26842b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 26852b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 26862b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 26872b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem)); 26882b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size)); 2689eaf62fffSJeremy L Thompson 2690eaf62fffSJeremy L Thompson // Build and diagonalize 1D Mass and Laplacian 26916574a04fSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 26926574a04fSJeremy L Thompson CeedCheck(is_tensor_basis, ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases"); 26932b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &mass)); 26942b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &laplace)); 26952b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &x)); 26962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp)); 26972b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d, &lambda)); 2698eaf62fffSJeremy L Thompson // -- Build matrices 26992b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 27002b730f8bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 27012b730f8bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d)); 27022b730f8bSJeremy L Thompson CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace)); 2703eaf62fffSJeremy L Thompson 2704eaf62fffSJeremy L Thompson // -- Diagonalize 27052b730f8bSJeremy L Thompson CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d)); 27062b730f8bSJeremy L Thompson CeedCall(CeedFree(&mass)); 27072b730f8bSJeremy L Thompson CeedCall(CeedFree(&laplace)); 27082b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) { 27092b730f8bSJeremy L Thompson for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d]; 27102b730f8bSJeremy L Thompson } 27112b730f8bSJeremy L Thompson CeedCall(CeedFree(&x)); 2712eaf62fffSJeremy L Thompson 27131c66c397SJeremy L Thompson { 27141c66c397SJeremy L Thompson CeedInt layout[3], num_modes = (interp ? 1 : 0) + (grad ? dim : 0); 27151c66c397SJeremy L Thompson CeedScalar max_norm = 0; 27161c66c397SJeremy L Thompson const CeedScalar *assembled_array, *q_weight_array; 27171c66c397SJeremy L Thompson CeedVector assembled = NULL, q_weight; 2718c5f45aeaSJeremy L Thompson CeedElemRestriction rstr_qf = NULL; 27191c66c397SJeremy L Thompson 27201c66c397SJeremy L Thompson // Assemble QFunction 27212b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request)); 272256c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(rstr_qf, layout)); 27232b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_qf)); 27242b730f8bSJeremy L Thompson CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm)); 2725eaf62fffSJeremy L Thompson 2726eaf62fffSJeremy L Thompson // Calculate element averages 27272b730f8bSJeremy L Thompson CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight)); 27282b730f8bSJeremy L Thompson CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight)); 27292b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array)); 27302b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array)); 27312b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_elem, &elem_avg)); 2732eaf62fffSJeremy L Thompson const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON; 27331c66c397SJeremy L Thompson 2734eaf62fffSJeremy L Thompson for (CeedInt e = 0; e < num_elem; e++) { 2735eaf62fffSJeremy L Thompson CeedInt count = 0; 27361c66c397SJeremy L Thompson 27372b730f8bSJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 27382b730f8bSJeremy L Thompson for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) { 27392b730f8bSJeremy L Thompson if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) { 27402b730f8bSJeremy L Thompson elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q]; 2741eaf62fffSJeremy L Thompson count++; 2742eaf62fffSJeremy L Thompson } 27432b730f8bSJeremy L Thompson } 27442b730f8bSJeremy L Thompson } 2745eaf62fffSJeremy L Thompson if (count) { 2746eaf62fffSJeremy L Thompson elem_avg[e] /= count; 2747eaf62fffSJeremy L Thompson } else { 2748eaf62fffSJeremy L Thompson elem_avg[e] = 1.0; 2749eaf62fffSJeremy L Thompson } 2750eaf62fffSJeremy L Thompson } 27512b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array)); 27522b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled)); 27532b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array)); 27542b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&q_weight)); 27551c66c397SJeremy L Thompson } 2756eaf62fffSJeremy L Thompson 2757eaf62fffSJeremy L Thompson // Build FDM diagonal 27581c66c397SJeremy L Thompson { 2759eaf62fffSJeremy L Thompson CeedScalar *q_data_array, *fdm_diagonal; 27601c66c397SJeremy L Thompson 2761352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_comp * num_nodes, &fdm_diagonal)); 2762352a5e7cSSebastian Grimberg const CeedScalar fdm_diagonal_bound = num_nodes * CEED_EPSILON; 27632b730f8bSJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) { 2764352a5e7cSSebastian Grimberg for (CeedInt n = 0; n < num_nodes; n++) { 2765352a5e7cSSebastian Grimberg if (interp) fdm_diagonal[c * num_nodes + n] = 1.0; 27662b730f8bSJeremy L Thompson if (grad) { 2767eaf62fffSJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 2768eaf62fffSJeremy L Thompson CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d; 2769352a5e7cSSebastian Grimberg fdm_diagonal[c * num_nodes + n] += lambda[i]; 2770eaf62fffSJeremy L Thompson } 2771eaf62fffSJeremy L Thompson } 2772352a5e7cSSebastian Grimberg if (fabs(fdm_diagonal[c * num_nodes + n]) < fdm_diagonal_bound) fdm_diagonal[c * num_nodes + n] = fdm_diagonal_bound; 27732b730f8bSJeremy L Thompson } 27742b730f8bSJeremy L Thompson } 2775352a5e7cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * num_nodes, &q_data)); 27762b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(q_data, 0.0)); 27772b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array)); 27782b730f8bSJeremy L Thompson for (CeedInt e = 0; e < num_elem; e++) { 27792b730f8bSJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) { 27801c66c397SJeremy L Thompson for (CeedInt n = 0; n < num_nodes; n++) 27811c66c397SJeremy L Thompson q_data_array[(e * num_comp + c) * num_nodes + n] = 1. / (elem_avg[e] * fdm_diagonal[c * num_nodes + n]); 27822b730f8bSJeremy L Thompson } 27832b730f8bSJeremy L Thompson } 27842b730f8bSJeremy L Thompson CeedCall(CeedFree(&elem_avg)); 27852b730f8bSJeremy L Thompson CeedCall(CeedFree(&fdm_diagonal)); 27862b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(q_data, &q_data_array)); 27871c66c397SJeremy L Thompson } 2788eaf62fffSJeremy L Thompson 2789eaf62fffSJeremy L Thompson // Setup FDM operator 2790eaf62fffSJeremy L Thompson // -- Basis 27911c66c397SJeremy L Thompson { 2792eaf62fffSJeremy L Thompson CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy; 27931c66c397SJeremy L Thompson 27942b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy)); 27952b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d, &q_ref_dummy)); 27962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d, &q_weight_dummy)); 27972b730f8bSJeremy 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)); 27982b730f8bSJeremy L Thompson CeedCall(CeedFree(&fdm_interp)); 27992b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_dummy)); 28002b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref_dummy)); 28012b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight_dummy)); 28022b730f8bSJeremy L Thompson CeedCall(CeedFree(&lambda)); 28031c66c397SJeremy L Thompson } 2804eaf62fffSJeremy L Thompson 2805eaf62fffSJeremy L Thompson // -- Restriction 28061c66c397SJeremy L Thompson { 2807352a5e7cSSebastian Grimberg CeedInt strides[3] = {1, num_nodes, num_nodes * num_comp}; 2808352a5e7cSSebastian Grimberg CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, num_nodes, num_comp, num_elem * num_comp * num_nodes, strides, &rstr_qd_i)); 28091c66c397SJeremy L Thompson } 28101c66c397SJeremy L Thompson 2811eaf62fffSJeremy L Thompson // -- QFunction 28122b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm)); 28132b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP)); 28142b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE)); 28152b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP)); 28162b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp)); 28171c66c397SJeremy L Thompson 2818eaf62fffSJeremy L Thompson // -- QFunction context 28191c66c397SJeremy L Thompson { 2820eaf62fffSJeremy L Thompson CeedInt *num_comp_data; 28211c66c397SJeremy L Thompson 28222b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &num_comp_data)); 2823eaf62fffSJeremy L Thompson num_comp_data[0] = num_comp; 28242b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm)); 28252b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data)); 28261c66c397SJeremy L Thompson } 28272b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm)); 28282b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(&ctx_fdm)); 28291c66c397SJeremy L Thompson 2830eaf62fffSJeremy L Thompson // -- Operator 28312b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv)); 28322b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE)); 2833356036faSJeremy L Thompson CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_NONE, q_data)); 28342b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE)); 2835eaf62fffSJeremy L Thompson 2836eaf62fffSJeremy L Thompson // Cleanup 28372b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&q_data)); 28382b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(&fdm_basis)); 28392b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i)); 28402b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&qf_fdm)); 2841eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2842eaf62fffSJeremy L Thompson } 2843eaf62fffSJeremy L Thompson 2844eaf62fffSJeremy L Thompson /// @} 2845