1d275d636SJeremy L Thompson // Copyright (c) 2017-2025, 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 CeedQFunctionField *input_fields, *output_fields; 411c66c397SJeremy L Thompson 429e77b9c8SJeremy L Thompson // Check if NULL qf passed in 439e77b9c8SJeremy L Thompson if (!qf) return CEED_ERROR_SUCCESS; 449e77b9c8SJeremy L Thompson 459bc66399SJeremy L Thompson CeedDebug256(CeedQFunctionReturnCeed(qf), 1, "---------- CeedOperator Fallback ----------\n"); 469bc66399SJeremy L Thompson CeedDebug(CeedQFunctionReturnCeed(qf), "Creating fallback CeedQFunction\n"); 47d04bbc78SJeremy L Thompson 489e77b9c8SJeremy L Thompson if (qf->source_path) { 492b730f8bSJeremy L Thompson size_t path_len = strlen(qf->source_path), name_len = strlen(qf->kernel_name); 509c25dd66SJeremy L Thompson 512b730f8bSJeremy L Thompson CeedCall(CeedCalloc(path_len + name_len + 2, &source_path_with_name)); 529e77b9c8SJeremy L Thompson memcpy(source_path_with_name, qf->source_path, path_len); 539e77b9c8SJeremy L Thompson memcpy(&source_path_with_name[path_len], ":", 1); 549e77b9c8SJeremy L Thompson memcpy(&source_path_with_name[path_len + 1], qf->kernel_name, name_len); 559c25dd66SJeremy L Thompson } else if (qf->user_source) { 569c25dd66SJeremy L Thompson CeedCall(CeedStringAllocCopy(qf->user_source, &source_path_with_name)); 579e77b9c8SJeremy L Thompson } else { 582b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &source_path_with_name)); 599e77b9c8SJeremy L Thompson } 609e77b9c8SJeremy L Thompson 611203703bSJeremy L Thompson { 621203703bSJeremy L Thompson CeedInt vec_length; 631203703bSJeremy L Thompson CeedQFunctionUser f; 641203703bSJeremy L Thompson 651203703bSJeremy L Thompson CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length)); 661203703bSJeremy L Thompson CeedCall(CeedQFunctionGetUserFunction(qf, &f)); 671203703bSJeremy L Thompson CeedCall(CeedQFunctionCreateInterior(fallback_ceed, vec_length, f, source_path_with_name, qf_fallback)); 681203703bSJeremy L Thompson } 699e77b9c8SJeremy L Thompson { 709e77b9c8SJeremy L Thompson CeedQFunctionContext ctx; 719e77b9c8SJeremy L Thompson 722b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetContext(qf, &ctx)); 732b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx)); 741485364cSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(&ctx)); 759e77b9c8SJeremy L Thompson } 761203703bSJeremy L Thompson CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 771203703bSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 786f8994e9SJeremy L Thompson const char *field_name; 791203703bSJeremy L Thompson CeedInt size; 801203703bSJeremy L Thompson CeedEvalMode eval_mode; 811203703bSJeremy L Thompson 82ab747706SJeremy L Thompson CeedCall(CeedQFunctionFieldGetData(input_fields[i], &field_name, &size, &eval_mode)); 831203703bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(*qf_fallback, field_name, size, eval_mode)); 849e77b9c8SJeremy L Thompson } 851203703bSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 866f8994e9SJeremy L Thompson const char *field_name; 871203703bSJeremy L Thompson CeedInt size; 881203703bSJeremy L Thompson CeedEvalMode eval_mode; 891203703bSJeremy L Thompson 90ab747706SJeremy L Thompson CeedCall(CeedQFunctionFieldGetData(output_fields[i], &field_name, &size, &eval_mode)); 911203703bSJeremy L Thompson CeedCall(CeedQFunctionAddOutput(*qf_fallback, field_name, size, eval_mode)); 929e77b9c8SJeremy L Thompson } 932b730f8bSJeremy L Thompson CeedCall(CeedFree(&source_path_with_name)); 949e77b9c8SJeremy L Thompson return CEED_ERROR_SUCCESS; 959e77b9c8SJeremy L Thompson } 969e77b9c8SJeremy L Thompson 979e77b9c8SJeremy L Thompson /** 98ca94c3ddSJeremy L Thompson @brief Duplicate a `CeedOperator` with a reference `Ceed` to fallback for advanced `CeedOperator` functionality 99eaf62fffSJeremy L Thompson 100ca94c3ddSJeremy L Thompson @param[in,out] op `CeedOperator` to create fallback for 101eaf62fffSJeremy L Thompson 102eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 103eaf62fffSJeremy L Thompson 104eaf62fffSJeremy L Thompson @ref Developer 105eaf62fffSJeremy L Thompson **/ 106d04bbc78SJeremy L Thompson static int CeedOperatorCreateFallback(CeedOperator op) { 1071c66c397SJeremy L Thompson bool is_composite; 1081203703bSJeremy L Thompson Ceed ceed, ceed_fallback; 1091c66c397SJeremy L Thompson CeedOperator op_fallback; 110eaf62fffSJeremy L Thompson 111805fe78eSJeremy L Thompson // Check not already created 112805fe78eSJeremy L Thompson if (op->op_fallback) return CEED_ERROR_SUCCESS; 113805fe78eSJeremy L Thompson 114eaf62fffSJeremy L Thompson // Fallback Ceed 1151203703bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 1161203703bSJeremy L Thompson CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback)); 1179bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 118d04bbc78SJeremy L Thompson if (!ceed_fallback) return CEED_ERROR_SUCCESS; 119d04bbc78SJeremy L Thompson 1209bc66399SJeremy L Thompson CeedDebug256(CeedOperatorReturnCeed(op), 1, "---------- CeedOperator Fallback ----------\n"); 1219bc66399SJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "Creating fallback CeedOperator\n"); 122eaf62fffSJeremy L Thompson 123eaf62fffSJeremy L Thompson // Clone Op 124b275c451SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 125b275c451SJeremy L Thompson if (is_composite) { 126b275c451SJeremy L Thompson CeedInt num_suboperators; 127b275c451SJeremy L Thompson CeedOperator *sub_operators; 128b275c451SJeremy L Thompson 1292b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorCreate(ceed_fallback, &op_fallback)); 130b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 131b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 132b275c451SJeremy L Thompson for (CeedInt i = 0; i < num_suboperators; i++) { 133d04bbc78SJeremy L Thompson CeedOperator op_sub_fallback; 134d04bbc78SJeremy L Thompson 135b275c451SJeremy L Thompson CeedCall(CeedOperatorGetFallback(sub_operators[i], &op_sub_fallback)); 1362b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorAddSub(op_fallback, op_sub_fallback)); 137805fe78eSJeremy L Thompson } 138805fe78eSJeremy L Thompson } else { 139bcd92680SJeremy L Thompson bool is_at_points = false; 1401203703bSJeremy L Thompson CeedInt num_input_fields, num_output_fields; 1419e77b9c8SJeremy L Thompson CeedQFunction qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL; 1421203703bSJeremy L Thompson CeedOperatorField *input_fields, *output_fields; 1431c66c397SJeremy L Thompson 1442b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback)); 1452b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback)); 1462b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback)); 147bcd92680SJeremy L Thompson CeedCall(CeedOperatorIsAtPoints(op, &is_at_points)); 148bcd92680SJeremy L Thompson if (is_at_points) { 149bcd92680SJeremy L Thompson CeedVector points; 150bcd92680SJeremy L Thompson CeedElemRestriction rstr_points; 151bcd92680SJeremy L Thompson 152bcd92680SJeremy L Thompson CeedCall(CeedOperatorCreateAtPoints(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback)); 153bcd92680SJeremy L Thompson CeedCall(CeedOperatorAtPointsGetPoints(op, &rstr_points, &points)); 154bcd92680SJeremy L Thompson CeedCall(CeedOperatorAtPointsSetPoints(op_fallback, rstr_points, points)); 155bcd92680SJeremy L Thompson CeedCall(CeedVectorDestroy(&points)); 156bcd92680SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_points)); 157bcd92680SJeremy L Thompson } else { 1582b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, dqfT_fallback, &op_fallback)); 159bcd92680SJeremy L Thompson } 1601203703bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 1611203703bSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1626f8994e9SJeremy L Thompson const char *field_name; 1631203703bSJeremy L Thompson CeedVector vec; 1641203703bSJeremy L Thompson CeedElemRestriction rstr; 1651203703bSJeremy L Thompson CeedBasis basis; 1661203703bSJeremy L Thompson 167ab747706SJeremy L Thompson CeedCall(CeedOperatorFieldGetData(input_fields[i], &field_name, &rstr, &basis, &vec)); 1681203703bSJeremy L Thompson CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec)); 169681d0ea7SJeremy L Thompson CeedCall(CeedVectorDestroy(&vec)); 170681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr)); 171681d0ea7SJeremy L Thompson CeedCall(CeedBasisDestroy(&basis)); 172805fe78eSJeremy L Thompson } 1731203703bSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1746f8994e9SJeremy L Thompson const char *field_name; 1751203703bSJeremy L Thompson CeedVector vec; 1761203703bSJeremy L Thompson CeedElemRestriction rstr; 1771203703bSJeremy L Thompson CeedBasis basis; 1781203703bSJeremy L Thompson 179ab747706SJeremy L Thompson CeedCall(CeedOperatorFieldGetData(output_fields[i], &field_name, &rstr, &basis, &vec)); 1801203703bSJeremy L Thompson CeedCall(CeedOperatorSetField(op_fallback, field_name, rstr, basis, vec)); 181681d0ea7SJeremy L Thompson CeedCall(CeedVectorDestroy(&vec)); 182681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr)); 183681d0ea7SJeremy L Thompson CeedCall(CeedBasisDestroy(&basis)); 184805fe78eSJeremy L Thompson } 1857d5185d7SSebastian Grimberg { 1867d5185d7SSebastian Grimberg CeedQFunctionAssemblyData data; 1877d5185d7SSebastian Grimberg 1887d5185d7SSebastian Grimberg CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data)); 1897d5185d7SSebastian Grimberg CeedCall(CeedQFunctionAssemblyDataReferenceCopy(data, &op_fallback->qf_assembled)); 1907d5185d7SSebastian Grimberg } 1919e77b9c8SJeremy L Thompson // Cleanup 1922b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&qf_fallback)); 1932b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&dqf_fallback)); 1942b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&dqfT_fallback)); 195805fe78eSJeremy L Thompson } 1962b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetName(op_fallback, op->name)); 1972b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fallback)); 198b05f7e9fSJeremy L Thompson // Note: No ref-counting here so we don't get caught in a reference loop. 199b05f7e9fSJeremy L Thompson // The op holds the only reference to op_fallback and is responsible for deleting itself and op_fallback. 200805fe78eSJeremy L Thompson op->op_fallback = op_fallback; 201b05f7e9fSJeremy L Thompson op_fallback->op_fallback_parent = op; 2029bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_fallback)); 203eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 204eaf62fffSJeremy L Thompson } 205eaf62fffSJeremy L Thompson 206eaf62fffSJeremy L Thompson /** 207eaf62fffSJeremy L Thompson @brief Core logic for assembling operator diagonal or point block diagonal 208eaf62fffSJeremy L Thompson 2090cd9fdf4SJeremy L Thompson @param[in] op `CeedOperator` to assemble diagonal or point block diagonal 210ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 211bd83916cSSebastian Grimberg @param[in] is_point_block Boolean flag to assemble diagonal or point block diagonal 212ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled diagonal 213eaf62fffSJeremy L Thompson 214eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 215eaf62fffSJeremy L Thompson 216eaf62fffSJeremy L Thompson @ref Developer 217eaf62fffSJeremy L Thompson **/ 2180cd9fdf4SJeremy L Thompson static inline int CeedSingleOperatorLinearAssembleAddDiagonal_Mesh(CeedOperator op, CeedRequest *request, const bool is_point_block, 219f3bd9308SJeremy L Thompson CeedVector assembled) { 220506b1a0cSSebastian Grimberg bool is_composite; 221506b1a0cSSebastian Grimberg 222506b1a0cSSebastian Grimberg CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2239bc66399SJeremy L Thompson CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 224506b1a0cSSebastian Grimberg 225506b1a0cSSebastian Grimberg // Assemble QFunction 226506b1a0cSSebastian Grimberg CeedInt layout_qf[3]; 227437c7c90SJeremy L Thompson const CeedScalar *assembled_qf_array; 228c5f45aeaSJeremy L Thompson CeedVector assembled_qf = NULL; 229c5f45aeaSJeremy L Thompson CeedElemRestriction assembled_elem_rstr = NULL; 230437c7c90SJeremy L Thompson 231437c7c90SJeremy L Thompson CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, request)); 23256c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf)); 233437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr)); 234437c7c90SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array)); 235eaf62fffSJeremy L Thompson 236ed9e99e6SJeremy L Thompson // Get assembly data 237437c7c90SJeremy L Thompson const CeedEvalMode **eval_modes_in, **eval_modes_out; 238506b1a0cSSebastian Grimberg CeedInt num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out; 239437c7c90SJeremy L Thompson CeedSize **eval_mode_offsets_in, **eval_mode_offsets_out, num_output_components; 240506b1a0cSSebastian Grimberg CeedBasis *active_bases_in, *active_bases_out; 241506b1a0cSSebastian Grimberg CeedElemRestriction *active_elem_rstrs_in, *active_elem_rstrs_out; 2421c66c397SJeremy L Thompson CeedOperatorAssemblyData data; 2431c66c397SJeremy L Thompson 244437c7c90SJeremy L Thompson CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data)); 245506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, &eval_mode_offsets_in, 246506b1a0cSSebastian Grimberg &num_active_bases_out, &num_eval_modes_out, &eval_modes_out, &eval_mode_offsets_out, 247506b1a0cSSebastian Grimberg &num_output_components)); 248506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, NULL, NULL, &active_bases_out, NULL)); 249506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, NULL, &active_elem_rstrs_in, NULL, &active_elem_rstrs_out)); 250506b1a0cSSebastian Grimberg 251934a29f5SSebastian Grimberg // Loop over all active bases (find matching input/output pairs) 252934a29f5SSebastian Grimberg for (CeedInt b = 0; b < CeedIntMin(num_active_bases_in, num_active_bases_out); b++) { 253934a29f5SSebastian Grimberg CeedInt b_in, b_out, num_elem, num_nodes, num_qpts, num_comp; 2541c66c397SJeremy L Thompson bool has_eval_none = false; 2551c66c397SJeremy L Thompson CeedScalar *elem_diag_array, *identity = NULL; 2561c66c397SJeremy L Thompson CeedVector elem_diag; 2577c1dbaffSSebastian Grimberg CeedElemRestriction diag_elem_rstr; 2581c66c397SJeremy L Thompson 259934a29f5SSebastian Grimberg if (num_active_bases_in <= num_active_bases_out) { 260934a29f5SSebastian Grimberg b_in = b; 261934a29f5SSebastian Grimberg for (b_out = 0; b_out < num_active_bases_out; b_out++) { 262934a29f5SSebastian Grimberg if (active_bases_in[b_in] == active_bases_out[b_out]) { 263934a29f5SSebastian Grimberg break; 264934a29f5SSebastian Grimberg } 265934a29f5SSebastian Grimberg } 266934a29f5SSebastian Grimberg if (b_out == num_active_bases_out) { 267934a29f5SSebastian Grimberg continue; 268934a29f5SSebastian Grimberg } // No matching output basis found 269934a29f5SSebastian Grimberg } else { 270934a29f5SSebastian Grimberg b_out = b; 271934a29f5SSebastian Grimberg for (b_in = 0; b_in < num_active_bases_in; b_in++) { 272934a29f5SSebastian Grimberg if (active_bases_in[b_in] == active_bases_out[b_out]) { 273934a29f5SSebastian Grimberg break; 274934a29f5SSebastian Grimberg } 275934a29f5SSebastian Grimberg } 276934a29f5SSebastian Grimberg if (b_in == num_active_bases_in) { 277934a29f5SSebastian Grimberg continue; 278934a29f5SSebastian Grimberg } // No matching output basis found 279934a29f5SSebastian Grimberg } 2809bc66399SJeremy L Thompson CeedCheck(active_elem_rstrs_in[b_in] == active_elem_rstrs_out[b_out], CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, 281506b1a0cSSebastian Grimberg "Cannot assemble operator diagonal with different input and output active element restrictions"); 282506b1a0cSSebastian Grimberg 2831c66c397SJeremy L Thompson // Assemble point block diagonal restriction, if needed 284bd83916cSSebastian Grimberg if (is_point_block) { 285934a29f5SSebastian Grimberg CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstrs_in[b_in], &diag_elem_rstr)); 2867c1dbaffSSebastian Grimberg } else { 287934a29f5SSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnsignedCopy(active_elem_rstrs_in[b_in], &diag_elem_rstr)); 288eaf62fffSJeremy L Thompson } 289eaf62fffSJeremy L Thompson 290eaf62fffSJeremy L Thompson // Create diagonal vector 291437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionCreateVector(diag_elem_rstr, NULL, &elem_diag)); 292eaf62fffSJeremy L Thompson 293eaf62fffSJeremy L Thompson // Assemble element operator diagonals 2942b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(elem_diag, 0.0)); 2952b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array)); 296437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionGetNumElements(diag_elem_rstr, &num_elem)); 297934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumNodes(active_bases_in[b_in], &num_nodes)); 298934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumComponents(active_bases_in[b_in], &num_comp)); 299934a29f5SSebastian Grimberg if (active_bases_in[b_in] == CEED_BASIS_NONE) num_qpts = num_nodes; 300934a29f5SSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(active_bases_in[b_in], &num_qpts)); 301ed9e99e6SJeremy L Thompson 302352a5e7cSSebastian Grimberg // Construct identity matrix for basis if required 303934a29f5SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_in[b_in]; i++) { 304934a29f5SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_in[b_in][i] == CEED_EVAL_NONE); 305ed9e99e6SJeremy L Thompson } 306934a29f5SSebastian Grimberg for (CeedInt i = 0; i < num_eval_modes_out[b_out]; i++) { 307934a29f5SSebastian Grimberg has_eval_none = has_eval_none || (eval_modes_out[b_out][i] == CEED_EVAL_NONE); 308ed9e99e6SJeremy L Thompson } 309ed9e99e6SJeremy L Thompson if (has_eval_none) { 3102b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_qpts * num_nodes, &identity)); 3112b730f8bSJeremy L Thompson for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0; 312eaf62fffSJeremy L Thompson } 313352a5e7cSSebastian Grimberg 314eaf62fffSJeremy L Thompson // Compute the diagonal of B^T D B 315eaf62fffSJeremy L Thompson // Each element 316b94338b9SJed Brown for (CeedSize e = 0; e < num_elem; e++) { 317eaf62fffSJeremy L Thompson // Each basis eval mode pair 318352a5e7cSSebastian Grimberg CeedInt d_out = 0, q_comp_out; 319352a5e7cSSebastian Grimberg CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE; 3201c66c397SJeremy L Thompson 321934a29f5SSebastian Grimberg for (CeedInt e_out = 0; e_out < num_eval_modes_out[b_out]; e_out++) { 3221c66c397SJeremy L Thompson CeedInt d_in = 0, q_comp_in; 323437c7c90SJeremy L Thompson const CeedScalar *B_t = NULL; 3241c66c397SJeremy L Thompson CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE; 3251c66c397SJeremy L Thompson 326934a29f5SSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(active_bases_out[b_out], eval_modes_out[b_out][e_out], identity, &B_t)); 327934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_out[b_out], eval_modes_out[b_out][e_out], &q_comp_out)); 328352a5e7cSSebastian Grimberg if (q_comp_out > 1) { 329934a29f5SSebastian Grimberg if (e_out == 0 || eval_modes_out[b_out][e_out] != eval_mode_out_prev) d_out = 0; 330352a5e7cSSebastian Grimberg else B_t = &B_t[(++d_out) * num_qpts * num_nodes]; 331352a5e7cSSebastian Grimberg } 332934a29f5SSebastian Grimberg eval_mode_out_prev = eval_modes_out[b_out][e_out]; 333352a5e7cSSebastian Grimberg 334934a29f5SSebastian Grimberg for (CeedInt e_in = 0; e_in < num_eval_modes_in[b_in]; e_in++) { 335437c7c90SJeremy L Thompson const CeedScalar *B = NULL; 3361c66c397SJeremy L Thompson 337934a29f5SSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(active_bases_in[b_in], eval_modes_in[b_in][e_in], identity, &B)); 338934a29f5SSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(active_bases_in[b_in], eval_modes_in[b_in][e_in], &q_comp_in)); 339352a5e7cSSebastian Grimberg if (q_comp_in > 1) { 340934a29f5SSebastian Grimberg if (e_in == 0 || eval_modes_in[b_in][e_in] != eval_mode_in_prev) d_in = 0; 341352a5e7cSSebastian Grimberg else B = &B[(++d_in) * num_qpts * num_nodes]; 342352a5e7cSSebastian Grimberg } 343934a29f5SSebastian Grimberg eval_mode_in_prev = eval_modes_in[b_in][e_in]; 344352a5e7cSSebastian Grimberg 345eaf62fffSJeremy L Thompson // Each component 346506b1a0cSSebastian Grimberg for (CeedInt c_out = 0; c_out < num_comp; c_out++) { 347437c7c90SJeremy L Thompson // Each qpt/node pair 3482b730f8bSJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 349bd83916cSSebastian Grimberg if (is_point_block) { 350eaf62fffSJeremy L Thompson // Point Block Diagonal 351506b1a0cSSebastian Grimberg for (CeedInt c_in = 0; c_in < num_comp; c_in++) { 352934a29f5SSebastian Grimberg const CeedSize c_offset = 353934a29f5SSebastian Grimberg (eval_mode_offsets_in[b_in][e_in] + c_in) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out; 354506b1a0cSSebastian Grimberg const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]]; 3551c66c397SJeremy L Thompson 3562b730f8bSJeremy L Thompson for (CeedInt n = 0; n < num_nodes; n++) { 357506b1a0cSSebastian Grimberg elem_diag_array[((e * num_comp + c_out) * num_comp + c_in) * num_nodes + n] += 358437c7c90SJeremy L Thompson B_t[q * num_nodes + n] * qf_value * B[q * num_nodes + n]; 359eaf62fffSJeremy L Thompson } 3602b730f8bSJeremy L Thompson } 361eaf62fffSJeremy L Thompson } else { 362eaf62fffSJeremy L Thompson // Diagonal Only 363934a29f5SSebastian Grimberg const CeedInt c_offset = 364934a29f5SSebastian Grimberg (eval_mode_offsets_in[b_in][e_in] + c_out) * num_output_components + eval_mode_offsets_out[b_out][e_out] + c_out; 365506b1a0cSSebastian Grimberg const CeedScalar qf_value = assembled_qf_array[q * layout_qf[0] + c_offset * layout_qf[1] + e * layout_qf[2]]; 3661c66c397SJeremy L Thompson 3672b730f8bSJeremy L Thompson for (CeedInt n = 0; n < num_nodes; n++) { 368506b1a0cSSebastian 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]; 369eaf62fffSJeremy L Thompson } 370eaf62fffSJeremy L Thompson } 371eaf62fffSJeremy L Thompson } 372eaf62fffSJeremy L Thompson } 3732b730f8bSJeremy L Thompson } 3742b730f8bSJeremy L Thompson } 3752b730f8bSJeremy L Thompson } 3762b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(elem_diag, &elem_diag_array)); 377eaf62fffSJeremy L Thompson 378eaf62fffSJeremy L Thompson // Assemble local operator diagonal 3797c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionApply(diag_elem_rstr, CEED_TRANSPOSE, elem_diag, assembled, request)); 380eaf62fffSJeremy L Thompson 381eaf62fffSJeremy L Thompson // Cleanup 3827c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&diag_elem_rstr)); 3832b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&elem_diag)); 3842b730f8bSJeremy L Thompson CeedCall(CeedFree(&identity)); 385437c7c90SJeremy L Thompson } 386437c7c90SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 387437c7c90SJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled_qf)); 388eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 389eaf62fffSJeremy L Thompson } 390eaf62fffSJeremy L Thompson 391eaf62fffSJeremy L Thompson /** 3920cd9fdf4SJeremy L Thompson @brief Core logic for assembling operator diagonal or point block diagonal 3930cd9fdf4SJeremy L Thompson 3940cd9fdf4SJeremy L Thompson @param[in] op `CeedOperator` to assemble diagonal or point block diagonal 3950cd9fdf4SJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 3960cd9fdf4SJeremy L Thompson @param[in] is_point_block Boolean flag to assemble diagonal or point block diagonal 3970cd9fdf4SJeremy L Thompson @param[out] assembled `CeedVector` to store assembled diagonal 3980cd9fdf4SJeremy L Thompson 3990cd9fdf4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4000cd9fdf4SJeremy L Thompson 4010cd9fdf4SJeremy L Thompson @ref Developer 4020cd9fdf4SJeremy L Thompson **/ 4030cd9fdf4SJeremy L Thompson static inline int CeedSingleOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block, 4040cd9fdf4SJeremy L Thompson CeedVector assembled) { 4050cd9fdf4SJeremy L Thompson bool is_at_points; 4060cd9fdf4SJeremy L Thompson 4070cd9fdf4SJeremy L Thompson CeedCall(CeedOperatorIsAtPoints(op, &is_at_points)); 4089bc66399SJeremy L Thompson CeedCheck(!is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "AtPoints operator not supported"); 4090cd9fdf4SJeremy L Thompson CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal_Mesh(op, request, is_point_block, assembled)); 4100cd9fdf4SJeremy L Thompson return CEED_ERROR_SUCCESS; 4110cd9fdf4SJeremy L Thompson } 4120cd9fdf4SJeremy L Thompson 4130cd9fdf4SJeremy L Thompson /** 414eaf62fffSJeremy L Thompson @brief Core logic for assembling composite operator diagonal 415eaf62fffSJeremy L Thompson 416ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble point block diagonal 417ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 418bd83916cSSebastian Grimberg @param[in] is_point_block Boolean flag to assemble diagonal or point block diagonal 419ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled diagonal 420eaf62fffSJeremy L Thompson 421eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 422eaf62fffSJeremy L Thompson 423eaf62fffSJeremy L Thompson @ref Developer 424eaf62fffSJeremy L Thompson **/ 425bd83916cSSebastian Grimberg static inline int CeedCompositeOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block, 426eaf62fffSJeremy L Thompson CeedVector assembled) { 427eaf62fffSJeremy L Thompson CeedInt num_sub; 428eaf62fffSJeremy L Thompson CeedOperator *suboperators; 4291c66c397SJeremy L Thompson 430c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); 431c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &suboperators)); 432eaf62fffSJeremy L Thompson for (CeedInt i = 0; i < num_sub; i++) { 433bd83916cSSebastian Grimberg if (is_point_block) { 4342b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], assembled, request)); 4356aa95790SJeremy L Thompson } else { 4362b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, request)); 4376aa95790SJeremy L Thompson } 438eaf62fffSJeremy L Thompson } 439eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 440eaf62fffSJeremy L Thompson } 441eaf62fffSJeremy L Thompson 442eaf62fffSJeremy L Thompson /** 443ca94c3ddSJeremy L Thompson @brief Build nonzero pattern for non-composite CeedOperator`. 444eaf62fffSJeremy L Thompson 445ca94c3ddSJeremy L Thompson Users should generally use @ref CeedOperatorLinearAssembleSymbolic(). 446eaf62fffSJeremy L Thompson 447ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble nonzero pattern 448eaf62fffSJeremy L Thompson @param[in] offset Offset for number of entries 449eaf62fffSJeremy L Thompson @param[out] rows Row number for each entry 450eaf62fffSJeremy L Thompson @param[out] cols Column number for each entry 451eaf62fffSJeremy L Thompson 452eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 453eaf62fffSJeremy L Thompson 454eaf62fffSJeremy L Thompson @ref Developer 455eaf62fffSJeremy L Thompson **/ 4562b730f8bSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, CeedInt *rows, CeedInt *cols) { 457f3d47e36SJeremy L Thompson Ceed ceed; 458f3d47e36SJeremy L Thompson bool is_composite; 45981670346SSebastian Grimberg CeedSize num_nodes_in, num_nodes_out, local_num_entries, count = 0; 460506b1a0cSSebastian Grimberg CeedInt num_elem_in, elem_size_in, num_comp_in, layout_er_in[3]; 46181670346SSebastian Grimberg CeedInt num_elem_out, elem_size_out, num_comp_out, layout_er_out[3]; 4621c66c397SJeremy L Thompson CeedScalar *array; 463506b1a0cSSebastian Grimberg const CeedScalar *elem_dof_a_in, *elem_dof_a_out; 464506b1a0cSSebastian Grimberg CeedVector index_vec_in, index_vec_out, elem_dof_in, elem_dof_out; 465506b1a0cSSebastian Grimberg CeedElemRestriction elem_rstr_in, elem_rstr_out, index_elem_rstr_in, index_elem_rstr_out; 4661c66c397SJeremy L Thompson 467f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 4689bc66399SJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 4695e1f751eSJeremy L Thompson CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 470eaf62fffSJeremy L Thompson 471506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes_in, &num_nodes_out)); 472506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out)); 473506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in)); 474506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in)); 475506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in)); 47656c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(elem_rstr_in, layout_er_in)); 477eaf62fffSJeremy L Thompson 478506b1a0cSSebastian Grimberg // Determine elem_dof relation for input 479506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_nodes_in, &index_vec_in)); 480506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayWrite(index_vec_in, CEED_MEM_HOST, &array)); 481c81f2b9dSJames Wright for (CeedSize i = 0; i < num_nodes_in; i++) array[i] = i; 482506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArray(index_vec_in, &array)); 483506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_elem_in * elem_size_in * num_comp_in, &elem_dof_in)); 484506b1a0cSSebastian Grimberg CeedCall(CeedVectorSetValue(elem_dof_in, 0.0)); 485506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_in, &index_elem_rstr_in)); 486506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionApply(index_elem_rstr_in, CEED_NOTRANSPOSE, index_vec_in, elem_dof_in, CEED_REQUEST_IMMEDIATE)); 487506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayRead(elem_dof_in, CEED_MEM_HOST, &elem_dof_a_in)); 488506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&index_vec_in)); 489506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_in)); 490506b1a0cSSebastian Grimberg 491506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 492506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out)); 493506b1a0cSSebastian Grimberg CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, 4943f08121cSJeremy L Thompson "Active input and output operator restrictions must have the same number of elements." 4953f08121cSJeremy L Thompson " Input has %" CeedInt_FMT " elements; output has %" CeedInt_FMT "elements.", 4963f08121cSJeremy L Thompson num_elem_in, num_elem_out); 497506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out)); 498506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out)); 49956c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(elem_rstr_out, layout_er_out)); 500506b1a0cSSebastian Grimberg 501506b1a0cSSebastian Grimberg // Determine elem_dof relation for output 502506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_nodes_out, &index_vec_out)); 503506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayWrite(index_vec_out, CEED_MEM_HOST, &array)); 504c81f2b9dSJames Wright for (CeedSize i = 0; i < num_nodes_out; i++) array[i] = i; 505506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArray(index_vec_out, &array)); 506506b1a0cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed, num_elem_out * elem_size_out * num_comp_out, &elem_dof_out)); 507506b1a0cSSebastian Grimberg CeedCall(CeedVectorSetValue(elem_dof_out, 0.0)); 508506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr_out, &index_elem_rstr_out)); 509506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionApply(index_elem_rstr_out, CEED_NOTRANSPOSE, index_vec_out, elem_dof_out, CEED_REQUEST_IMMEDIATE)); 510506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayRead(elem_dof_out, CEED_MEM_HOST, &elem_dof_a_out)); 511506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&index_vec_out)); 512506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&index_elem_rstr_out)); 513506b1a0cSSebastian Grimberg } else { 514506b1a0cSSebastian Grimberg num_elem_out = num_elem_in; 515506b1a0cSSebastian Grimberg elem_size_out = elem_size_in; 516506b1a0cSSebastian Grimberg num_comp_out = num_comp_in; 517506b1a0cSSebastian Grimberg layout_er_out[0] = layout_er_in[0]; 518506b1a0cSSebastian Grimberg layout_er_out[1] = layout_er_in[1]; 519506b1a0cSSebastian Grimberg layout_er_out[2] = layout_er_in[2]; 520506b1a0cSSebastian Grimberg elem_dof_a_out = elem_dof_a_in; 521506b1a0cSSebastian Grimberg } 522c81f2b9dSJames Wright local_num_entries = (CeedSize)elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in; 523eaf62fffSJeremy L Thompson 524eaf62fffSJeremy L Thompson // Determine i, j locations for element matrices 525506b1a0cSSebastian Grimberg for (CeedInt e = 0; e < num_elem_in; e++) { 526506b1a0cSSebastian Grimberg for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) { 527506b1a0cSSebastian Grimberg for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) { 528506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 529506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 530506b1a0cSSebastian Grimberg const CeedInt elem_dof_index_row = i * layout_er_out[0] + comp_out * layout_er_out[1] + e * layout_er_out[2]; 531506b1a0cSSebastian Grimberg const CeedInt elem_dof_index_col = j * layout_er_in[0] + comp_in * layout_er_in[1] + e * layout_er_in[2]; 532506b1a0cSSebastian Grimberg const CeedInt row = elem_dof_a_out[elem_dof_index_row]; 533506b1a0cSSebastian Grimberg const CeedInt col = elem_dof_a_in[elem_dof_index_col]; 534eaf62fffSJeremy L Thompson 535eaf62fffSJeremy L Thompson rows[offset + count] = row; 536eaf62fffSJeremy L Thompson cols[offset + count] = col; 537eaf62fffSJeremy L Thompson count++; 538eaf62fffSJeremy L Thompson } 539eaf62fffSJeremy L Thompson } 540eaf62fffSJeremy L Thompson } 541eaf62fffSJeremy L Thompson } 542eaf62fffSJeremy L Thompson } 5436574a04fSJeremy L Thompson CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing assembled entries"); 544506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArrayRead(elem_dof_in, &elem_dof_a_in)); 545506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&elem_dof_in)); 546506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 547506b1a0cSSebastian Grimberg CeedCall(CeedVectorRestoreArrayRead(elem_dof_out, &elem_dof_a_out)); 548506b1a0cSSebastian Grimberg CeedCall(CeedVectorDestroy(&elem_dof_out)); 549506b1a0cSSebastian Grimberg } 550681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&elem_rstr_in)); 551681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&elem_rstr_out)); 5529bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 553eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 554eaf62fffSJeremy L Thompson } 555eaf62fffSJeremy L Thompson 556eaf62fffSJeremy L Thompson /** 5570816752eSJeremy L Thompson @brief Core logic to assemble `CeedQFunction` and store result internally. 5580816752eSJeremy L Thompson 5590816752eSJeremy L Thompson Return copied references of stored data to the caller. 5600816752eSJeremy L Thompson Caller is responsible for ownership and destruction of the copied references. 5610816752eSJeremy L Thompson See also @ref CeedOperatorLinearAssembleQFunction(). 5620816752eSJeremy L Thompson 5630816752eSJeremy 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. 5640816752eSJeremy L Thompson These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object. 5650816752eSJeremy L Thompson 5660816752eSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 5670816752eSJeremy L Thompson @param[in] use_parent Boolean flag to check for fallback parent implementation 5680816752eSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points 5690816752eSJeremy L Thompson @param[out] rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction` 5700816752eSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 5710816752eSJeremy L Thompson 5720816752eSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 5730816752eSJeremy L Thompson 5740816752eSJeremy L Thompson @ref User 5750816752eSJeremy L Thompson **/ 5760816752eSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionBuildOrUpdate_Core(CeedOperator op, bool use_parent, CeedVector *assembled, CeedElemRestriction *rstr, 5770816752eSJeremy L Thompson CeedRequest *request) { 5780816752eSJeremy L Thompson int (*LinearAssembleQFunctionUpdate)(CeedOperator, CeedVector, CeedElemRestriction, CeedRequest *) = NULL; 5790816752eSJeremy L Thompson CeedOperator op_assemble = NULL; 5800816752eSJeremy L Thompson CeedOperator op_fallback_parent = NULL; 5810816752eSJeremy L Thompson 5820816752eSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 5830816752eSJeremy L Thompson 5840816752eSJeremy L Thompson // Determine if fallback parent or operator has implementation 5850816752eSJeremy L Thompson CeedCall(CeedOperatorGetFallbackParent(op, &op_fallback_parent)); 5860816752eSJeremy L Thompson if (op_fallback_parent && use_parent && op_fallback_parent->LinearAssembleQFunctionUpdate) { 5870816752eSJeremy L Thompson // -- Backend version for op fallback parent is faster, if it exists 588*ca38d01dSJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "Using fallback parent for CeedOperatorLinearAssembleQFunctionBuildOrUpdate\n"); 5890816752eSJeremy L Thompson LinearAssembleQFunctionUpdate = op_fallback_parent->LinearAssembleQFunctionUpdate; 5900816752eSJeremy L Thompson op_assemble = op_fallback_parent; 5910816752eSJeremy L Thompson } else if (op->LinearAssembleQFunctionUpdate) { 5920816752eSJeremy L Thompson // -- Backend version for op 5930816752eSJeremy L Thompson LinearAssembleQFunctionUpdate = op->LinearAssembleQFunctionUpdate; 5940816752eSJeremy L Thompson op_assemble = op; 5950816752eSJeremy L Thompson } 5960816752eSJeremy L Thompson 5970816752eSJeremy L Thompson // Assemble QFunction 5980816752eSJeremy L Thompson if (LinearAssembleQFunctionUpdate) { 5990816752eSJeremy L Thompson // Backend or fallback parent version 6000816752eSJeremy L Thompson CeedQFunctionAssemblyData data; 6010816752eSJeremy L Thompson bool data_is_setup; 6020816752eSJeremy L Thompson CeedVector assembled_vec = NULL; 6030816752eSJeremy L Thompson CeedElemRestriction assembled_rstr = NULL; 6040816752eSJeremy L Thompson 6050816752eSJeremy L Thompson CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data)); 6060816752eSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataIsSetup(data, &data_is_setup)); 6070816752eSJeremy L Thompson if (data_is_setup) { 6080816752eSJeremy L Thompson bool update_needed; 6090816752eSJeremy L Thompson 6100816752eSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataGetObjects(data, &assembled_vec, &assembled_rstr)); 6110816752eSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataIsUpdateNeeded(data, &update_needed)); 6120816752eSJeremy L Thompson if (update_needed) CeedCall(LinearAssembleQFunctionUpdate(op_assemble, assembled_vec, assembled_rstr, request)); 6130816752eSJeremy L Thompson } else { 6140816752eSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleQFunction(op_assemble, &assembled_vec, &assembled_rstr, request)); 6150816752eSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataSetObjects(data, assembled_vec, assembled_rstr)); 6160816752eSJeremy L Thompson } 6170816752eSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(data, false)); 6180816752eSJeremy L Thompson 6190816752eSJeremy L Thompson // Copy reference from internally held copy 6200816752eSJeremy L Thompson CeedCall(CeedVectorReferenceCopy(assembled_vec, assembled)); 6210816752eSJeremy L Thompson CeedCall(CeedElemRestrictionReferenceCopy(assembled_rstr, rstr)); 6220816752eSJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled_vec)); 6230816752eSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&assembled_rstr)); 6240816752eSJeremy L Thompson } else { 6250816752eSJeremy L Thompson // Operator fallback 6260816752eSJeremy L Thompson CeedOperator op_fallback; 6270816752eSJeremy L Thompson 628*ca38d01dSJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssembleQFunctionBuildOrUpdate\n"); 6290816752eSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 6300816752eSJeremy L Thompson if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request)); 6310816752eSJeremy L Thompson else return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate"); 6320816752eSJeremy L Thompson } 6330816752eSJeremy L Thompson return CEED_ERROR_SUCCESS; 6340816752eSJeremy L Thompson } 6350816752eSJeremy L Thompson 6360816752eSJeremy L Thompson /** 6370816752eSJeremy L Thompson @brief Assemble `CeedQFunction` and store result internally, but do not use fallback parent. 6380816752eSJeremy L Thompson 6390816752eSJeremy L Thompson Return copied references of stored data to the caller. 6400816752eSJeremy L Thompson Caller is responsible for ownership and destruction of the copied references. 6410816752eSJeremy L Thompson See also @ref CeedOperatorLinearAssembleQFunction(). 6420816752eSJeremy L Thompson 6430816752eSJeremy 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. 6440816752eSJeremy L Thompson These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object. 6450816752eSJeremy L Thompson 6460816752eSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 6470816752eSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points 6480816752eSJeremy L Thompson @param[out] rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction` 6490816752eSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 6500816752eSJeremy L Thompson 6510816752eSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 6520816752eSJeremy L Thompson 6530816752eSJeremy L Thompson @ref Developer 6540816752eSJeremy L Thompson **/ 6550816752eSJeremy L Thompson int CeedOperatorFallbackLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, 6560816752eSJeremy L Thompson CeedRequest *request) { 6570816752eSJeremy L Thompson return CeedOperatorLinearAssembleQFunctionBuildOrUpdate_Core(op, false, assembled, rstr, request); 6580816752eSJeremy L Thompson } 6590816752eSJeremy L Thompson 6600816752eSJeremy L Thompson /** 661ca94c3ddSJeremy L Thompson @brief Assemble nonzero entries for non-composite `CeedOperator`. 662eaf62fffSJeremy L Thompson 663ca94c3ddSJeremy L Thompson Users should generally use @ref CeedOperatorLinearAssemble(). 664eaf62fffSJeremy L Thompson 665ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 666ea61e9acSJeremy L Thompson @param[in] offset Offset for number of entries 667eaf62fffSJeremy L Thompson @param[out] values Values to assemble into matrix 668eaf62fffSJeremy L Thompson 669eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 670eaf62fffSJeremy L Thompson 671eaf62fffSJeremy L Thompson @ref Developer 672eaf62fffSJeremy L Thompson **/ 6730183ed61SJeremy L Thompson int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) { 6741b95d8c6SJeremy L Thompson bool is_composite, is_at_points; 6751c66c397SJeremy L Thompson 676f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 6779bc66399SJeremy L Thompson CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 678f3d47e36SJeremy L Thompson 679f3d47e36SJeremy L Thompson // Early exit for empty operator 680f3d47e36SJeremy L Thompson { 681f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 682f3d47e36SJeremy L Thompson 683f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 684f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 685f3d47e36SJeremy L Thompson } 686eaf62fffSJeremy L Thompson 687cefa2673SJeremy L Thompson if (op->LinearAssembleSingle) { 688cefa2673SJeremy L Thompson // Backend version 6892b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleSingle(op, offset, values)); 690cefa2673SJeremy L Thompson return CEED_ERROR_SUCCESS; 691cefa2673SJeremy L Thompson } else { 692cefa2673SJeremy L Thompson // Operator fallback 693cefa2673SJeremy L Thompson CeedOperator op_fallback; 694cefa2673SJeremy L Thompson 695*ca38d01dSJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedSingleOperatorAssemble\n"); 6962b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 697cefa2673SJeremy L Thompson if (op_fallback) { 6982b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemble(op_fallback, offset, values)); 699cefa2673SJeremy L Thompson return CEED_ERROR_SUCCESS; 700cefa2673SJeremy L Thompson } 701cefa2673SJeremy L Thompson } 702cefa2673SJeremy L Thompson 7031b95d8c6SJeremy L Thompson CeedCall(CeedOperatorIsAtPoints(op, &is_at_points)); 7041b95d8c6SJeremy L Thompson CeedCheck(!is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, 7051b95d8c6SJeremy L Thompson "Backend does not implement CeedOperatorLinearAssemble for AtPoints operator"); 7061b95d8c6SJeremy L Thompson 707eaf62fffSJeremy L Thompson // Assemble QFunction 708506b1a0cSSebastian Grimberg CeedInt layout_qf[3]; 7091c66c397SJeremy L Thompson const CeedScalar *assembled_qf_array; 710c5f45aeaSJeremy L Thompson CeedVector assembled_qf = NULL; 711506b1a0cSSebastian Grimberg CeedElemRestriction assembled_elem_rstr = NULL; 712eaf62fffSJeremy L Thompson 713506b1a0cSSebastian Grimberg CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_elem_rstr, CEED_REQUEST_IMMEDIATE)); 71456c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(assembled_elem_rstr, layout_qf)); 715506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&assembled_elem_rstr)); 716506b1a0cSSebastian Grimberg CeedCall(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array)); 717eaf62fffSJeremy L Thompson 718ed9e99e6SJeremy L Thompson // Get assembly data 719506b1a0cSSebastian Grimberg CeedInt num_elem_in, elem_size_in, num_comp_in, num_qpts_in; 72081670346SSebastian Grimberg CeedInt num_elem_out, elem_size_out, num_comp_out, num_qpts_out; 72181670346SSebastian Grimberg CeedSize local_num_entries, count = 0; 722506b1a0cSSebastian Grimberg const CeedEvalMode **eval_modes_in, **eval_modes_out; 723506b1a0cSSebastian Grimberg CeedInt num_active_bases_in, *num_eval_modes_in, num_active_bases_out, *num_eval_modes_out; 724506b1a0cSSebastian Grimberg CeedBasis *active_bases_in, *active_bases_out, basis_in, basis_out; 725506b1a0cSSebastian Grimberg const CeedScalar **B_mats_in, **B_mats_out, *B_mat_in, *B_mat_out; 726506b1a0cSSebastian Grimberg CeedElemRestriction elem_rstr_in, elem_rstr_out; 727506b1a0cSSebastian Grimberg CeedRestrictionType elem_rstr_type_in, elem_rstr_type_out; 728506b1a0cSSebastian Grimberg const bool *elem_rstr_orients_in = NULL, *elem_rstr_orients_out = NULL; 729506b1a0cSSebastian Grimberg const CeedInt8 *elem_rstr_curl_orients_in = NULL, *elem_rstr_curl_orients_out = NULL; 730506b1a0cSSebastian Grimberg CeedOperatorAssemblyData data; 731eaf62fffSJeremy L Thompson 732506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetOperatorAssemblyData(op, &data)); 733506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, NULL, &num_active_bases_out, 734506b1a0cSSebastian Grimberg &num_eval_modes_out, &eval_modes_out, NULL, NULL)); 735506b1a0cSSebastian Grimberg 7369bc66399SJeremy L Thompson CeedCheck(num_active_bases_in == 1 && num_active_bases_out == 1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, 737506b1a0cSSebastian Grimberg "Cannot assemble operator with multiple active bases"); 7389bc66399SJeremy L Thompson CeedCheck(num_eval_modes_in[0] > 0 && num_eval_modes_out[0] > 0, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, 7399bc66399SJeremy L Thompson "Cannot assemble operator without inputs/outputs"); 740eaf62fffSJeremy L Thompson 741506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, &B_mats_in, NULL, &active_bases_out, &B_mats_out)); 742506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out)); 743506b1a0cSSebastian Grimberg basis_in = active_bases_in[0]; 744506b1a0cSSebastian Grimberg basis_out = active_bases_out[0]; 745506b1a0cSSebastian Grimberg B_mat_in = B_mats_in[0]; 746506b1a0cSSebastian Grimberg B_mat_out = B_mats_out[0]; 747eaf62fffSJeremy L Thompson 748506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_in, &num_elem_in)); 749506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_in, &elem_size_in)); 750506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_in, &num_comp_in)); 751506b1a0cSSebastian Grimberg if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in; 752506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in)); 753506b1a0cSSebastian Grimberg 754506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetType(elem_rstr_in, &elem_rstr_type_in)); 755506b1a0cSSebastian Grimberg if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) { 756506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_orients_in)); 757506b1a0cSSebastian Grimberg } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 758506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_in, CEED_MEM_HOST, &elem_rstr_curl_orients_in)); 7597c1dbaffSSebastian Grimberg } 7607c1dbaffSSebastian Grimberg 761506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 762506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out)); 7639bc66399SJeremy L Thompson CeedCheck(num_elem_in == num_elem_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, 7643f08121cSJeremy L Thompson "Active input and output operator restrictions must have the same number of elements." 7653f08121cSJeremy L Thompson " Input has %" CeedInt_FMT " elements; output has %" CeedInt_FMT "elements.", 7663f08121cSJeremy L Thompson num_elem_in, num_elem_out); 767506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(elem_rstr_out, &elem_size_out)); 768506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out)); 769506b1a0cSSebastian Grimberg if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out; 770506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out)); 7719bc66399SJeremy L Thompson CeedCheck(num_qpts_in == num_qpts_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, 7723f08121cSJeremy L Thompson "Active input and output bases must have the same number of quadrature points." 7733f08121cSJeremy L Thompson " Input has %" CeedInt_FMT " points; output has %" CeedInt_FMT "points.", 7743f08121cSJeremy L Thompson num_qpts_in, num_qpts_out); 775eaf62fffSJeremy L Thompson 776506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetType(elem_rstr_out, &elem_rstr_type_out)); 777506b1a0cSSebastian Grimberg if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) { 778506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_orients_out)); 779506b1a0cSSebastian Grimberg } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 780506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetCurlOrientations(elem_rstr_out, CEED_MEM_HOST, &elem_rstr_curl_orients_out)); 781506b1a0cSSebastian Grimberg } 782506b1a0cSSebastian Grimberg } else { 783506b1a0cSSebastian Grimberg num_elem_out = num_elem_in; 784506b1a0cSSebastian Grimberg elem_size_out = elem_size_in; 785506b1a0cSSebastian Grimberg num_comp_out = num_comp_in; 786506b1a0cSSebastian Grimberg num_qpts_out = num_qpts_in; 787506b1a0cSSebastian Grimberg 788506b1a0cSSebastian Grimberg elem_rstr_orients_out = elem_rstr_orients_in; 789506b1a0cSSebastian Grimberg elem_rstr_curl_orients_out = elem_rstr_curl_orients_in; 790506b1a0cSSebastian Grimberg } 791c81f2b9dSJames Wright local_num_entries = (CeedSize)elem_size_out * num_comp_out * elem_size_in * num_comp_in * num_elem_in; 792506b1a0cSSebastian Grimberg 793506b1a0cSSebastian Grimberg // Loop over elements and put in data structure 7947c1dbaffSSebastian Grimberg // We store B_mat_in, B_mat_out, BTD, elem_mat in row-major order 7950459ebd3SSebastian Grimberg CeedTensorContract contract; 796123d890dSSebastian Grimberg CeedScalar *vals, *BTD_mat = NULL, *elem_mat = NULL, *elem_mat_b = NULL; 797506b1a0cSSebastian Grimberg 798c22497adSSebastian Grimberg CeedCall(CeedBasisGetTensorContract(basis_in, &contract)); 799123d890dSSebastian Grimberg CeedCall(CeedCalloc(elem_size_out * num_qpts_in * num_eval_modes_in[0], &BTD_mat)); 800123d890dSSebastian Grimberg CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat)); 801506b1a0cSSebastian Grimberg if (elem_rstr_curl_orients_in || elem_rstr_curl_orients_out) CeedCall(CeedCalloc(elem_size_out * elem_size_in, &elem_mat_b)); 8021c66c397SJeremy L Thompson 80328ec399dSJeremy L Thompson CeedCall(CeedVectorGetArray(values, CEED_MEM_HOST, &vals)); 804506b1a0cSSebastian Grimberg for (CeedSize e = 0; e < num_elem_in; e++) { 805506b1a0cSSebastian Grimberg for (CeedInt comp_in = 0; comp_in < num_comp_in; comp_in++) { 806506b1a0cSSebastian Grimberg for (CeedInt comp_out = 0; comp_out < num_comp_out; comp_out++) { 807ed9e99e6SJeremy L Thompson // Compute B^T*D 808506b1a0cSSebastian Grimberg for (CeedSize n = 0; n < elem_size_out; n++) { 809506b1a0cSSebastian Grimberg for (CeedSize q = 0; q < num_qpts_in; q++) { 810437c7c90SJeremy L Thompson for (CeedInt e_in = 0; e_in < num_eval_modes_in[0]; e_in++) { 811506b1a0cSSebastian Grimberg const CeedSize btd_index = n * (num_qpts_in * num_eval_modes_in[0]) + q * num_eval_modes_in[0] + e_in; 812067fd99fSJeremy L Thompson CeedScalar sum = 0.0; 8131c66c397SJeremy L Thompson 814437c7c90SJeremy L Thompson for (CeedInt e_out = 0; e_out < num_eval_modes_out[0]; e_out++) { 815506b1a0cSSebastian Grimberg const CeedSize b_out_index = (q * num_eval_modes_out[0] + e_out) * elem_size_out + n; 816506b1a0cSSebastian 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; 817b94338b9SJed Brown const CeedSize qf_index = q * layout_qf[0] + eval_mode_index * layout_qf[1] + e * layout_qf[2]; 8181c66c397SJeremy L Thompson 819067fd99fSJeremy L Thompson sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index]; 820eaf62fffSJeremy L Thompson } 821067fd99fSJeremy L Thompson BTD_mat[btd_index] = sum; 822ed9e99e6SJeremy L Thompson } 823ed9e99e6SJeremy L Thompson } 824eaf62fffSJeremy L Thompson } 8257c1dbaffSSebastian Grimberg 8267c1dbaffSSebastian Grimberg // Form element matrix itself (for each block component) 827e4065a52SSebastian Grimberg if (contract) { 8280459ebd3SSebastian Grimberg CeedCall(CeedTensorContractApply(contract, 1, num_qpts_in * num_eval_modes_in[0], elem_size_in, elem_size_out, BTD_mat, CEED_NOTRANSPOSE, 8290459ebd3SSebastian Grimberg false, B_mat_in, elem_mat)); 830e4065a52SSebastian Grimberg } else { 8319bc66399SJeremy L Thompson Ceed ceed; 8329bc66399SJeremy L Thompson 8339bc66399SJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 834e4065a52SSebastian 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])); 8359bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 836e4065a52SSebastian Grimberg } 837eaf62fffSJeremy L Thompson 8387c1dbaffSSebastian Grimberg // Transform the element matrix if required 839506b1a0cSSebastian Grimberg if (elem_rstr_orients_out) { 840506b1a0cSSebastian Grimberg const bool *elem_orients = &elem_rstr_orients_out[e * elem_size_out]; 8411c66c397SJeremy L Thompson 842506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 843506b1a0cSSebastian Grimberg const double orient = elem_orients[i] ? -1.0 : 1.0; 844506b1a0cSSebastian Grimberg 845506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 846506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] *= orient; 8477c1dbaffSSebastian Grimberg } 8487c1dbaffSSebastian Grimberg } 849506b1a0cSSebastian Grimberg } else if (elem_rstr_curl_orients_out) { 850506b1a0cSSebastian Grimberg const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_out[e * 3 * elem_size_out]; 8511c66c397SJeremy L Thompson 8527c1dbaffSSebastian Grimberg // T^T*(B^T*D*B) 853506b1a0cSSebastian Grimberg memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar)); 854506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 855506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 856506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * i + 1] + 857506b1a0cSSebastian Grimberg (i > 0 ? elem_mat_b[(i - 1) * elem_size_in + j] * elem_curl_orients[3 * i - 1] : 0.0) + 858506b1a0cSSebastian Grimberg (i < elem_size_out - 1 ? elem_mat_b[(i + 1) * elem_size_in + j] * elem_curl_orients[3 * i + 3] : 0.0); 8597c1dbaffSSebastian Grimberg } 8607c1dbaffSSebastian Grimberg } 861506b1a0cSSebastian Grimberg } 862506b1a0cSSebastian Grimberg if (elem_rstr_orients_in) { 863506b1a0cSSebastian Grimberg const bool *elem_orients = &elem_rstr_orients_in[e * elem_size_in]; 864506b1a0cSSebastian Grimberg 865506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 866506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 867506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] *= elem_orients[j] ? -1.0 : 1.0; 868506b1a0cSSebastian Grimberg } 869506b1a0cSSebastian Grimberg } 870506b1a0cSSebastian Grimberg } else if (elem_rstr_curl_orients_in) { 871506b1a0cSSebastian Grimberg const CeedInt8 *elem_curl_orients = &elem_rstr_curl_orients_in[e * 3 * elem_size_in]; 872506b1a0cSSebastian Grimberg 873506b1a0cSSebastian Grimberg // (B^T*D*B)*T 874506b1a0cSSebastian Grimberg memcpy(elem_mat_b, elem_mat, elem_size_out * elem_size_in * sizeof(CeedScalar)); 875506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 876506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 877506b1a0cSSebastian Grimberg elem_mat[i * elem_size_in + j] = elem_mat_b[i * elem_size_in + j] * elem_curl_orients[3 * j + 1] + 878506b1a0cSSebastian Grimberg (j > 0 ? elem_mat_b[i * elem_size_in + j - 1] * elem_curl_orients[3 * j - 1] : 0.0) + 879506b1a0cSSebastian Grimberg (j < elem_size_in - 1 ? elem_mat_b[i * elem_size_in + j + 1] * elem_curl_orients[3 * j + 3] : 0.0); 8807c1dbaffSSebastian Grimberg } 8817c1dbaffSSebastian Grimberg } 8827c1dbaffSSebastian Grimberg } 8837c1dbaffSSebastian Grimberg 8847c1dbaffSSebastian Grimberg // Put element matrix in coordinate data structure 885506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < elem_size_out; i++) { 886506b1a0cSSebastian Grimberg for (CeedInt j = 0; j < elem_size_in; j++) { 887506b1a0cSSebastian Grimberg vals[offset + count] = elem_mat[i * elem_size_in + j]; 888eaf62fffSJeremy L Thompson count++; 889eaf62fffSJeremy L Thompson } 890eaf62fffSJeremy L Thompson } 891eaf62fffSJeremy L Thompson } 892eaf62fffSJeremy L Thompson } 893eaf62fffSJeremy L Thompson } 8949bc66399SJeremy L Thompson CeedCheck(count == local_num_entries, CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR, "Error computing entries"); 8952b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(values, &vals)); 896eaf62fffSJeremy L Thompson 897506b1a0cSSebastian Grimberg // Cleanup 898123d890dSSebastian Grimberg CeedCall(CeedFree(&BTD_mat)); 899123d890dSSebastian Grimberg CeedCall(CeedFree(&elem_mat)); 900506b1a0cSSebastian Grimberg CeedCall(CeedFree(&elem_mat_b)); 901506b1a0cSSebastian Grimberg if (elem_rstr_type_in == CEED_RESTRICTION_ORIENTED) { 902506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_in, &elem_rstr_orients_in)); 903506b1a0cSSebastian Grimberg } else if (elem_rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) { 904506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_in, &elem_rstr_curl_orients_in)); 905506b1a0cSSebastian Grimberg } 906506b1a0cSSebastian Grimberg if (elem_rstr_in != elem_rstr_out) { 907506b1a0cSSebastian Grimberg if (elem_rstr_type_out == CEED_RESTRICTION_ORIENTED) { 908506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOrientations(elem_rstr_out, &elem_rstr_orients_out)); 909506b1a0cSSebastian Grimberg } else if (elem_rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) { 910506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreCurlOrientations(elem_rstr_out, &elem_rstr_curl_orients_out)); 911506b1a0cSSebastian Grimberg } 912506b1a0cSSebastian Grimberg } 9132b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array)); 9142b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled_qf)); 915681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&elem_rstr_in)); 916681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&elem_rstr_out)); 917eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 918eaf62fffSJeremy L Thompson } 919eaf62fffSJeremy L Thompson 920eaf62fffSJeremy L Thompson /** 921ca94c3ddSJeremy L Thompson @brief Count number of entries for assembled `CeedOperator` 922eaf62fffSJeremy L Thompson 923ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 924eaf62fffSJeremy L Thompson @param[out] num_entries Number of entries in assembled representation 925eaf62fffSJeremy L Thompson 926eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 927eaf62fffSJeremy L Thompson 928eaf62fffSJeremy L Thompson @ref Utility 929eaf62fffSJeremy L Thompson **/ 930b94338b9SJed Brown static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedSize *num_entries) { 931b275c451SJeremy L Thompson bool is_composite; 932506b1a0cSSebastian Grimberg CeedInt num_elem_in, elem_size_in, num_comp_in, num_elem_out, elem_size_out, num_comp_out; 933506b1a0cSSebastian Grimberg CeedElemRestriction rstr_in, rstr_out; 934eaf62fffSJeremy L Thompson 935b275c451SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 9369bc66399SJeremy L Thompson CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); 937506b1a0cSSebastian Grimberg 938506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out)); 939506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in)); 940506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in)); 941506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in)); 942506b1a0cSSebastian Grimberg if (rstr_in != rstr_out) { 943506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out)); 9449bc66399SJeremy L Thompson CeedCheck(num_elem_in == num_elem_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, 9453f08121cSJeremy L Thompson "Active input and output operator restrictions must have the same number of elements." 9463f08121cSJeremy L Thompson " Input has %" CeedInt_FMT " elements; output has %" CeedInt_FMT "elements.", 9473f08121cSJeremy L Thompson num_elem_in, num_elem_out); 948506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out)); 949506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out)); 950506b1a0cSSebastian Grimberg } else { 951506b1a0cSSebastian Grimberg num_elem_out = num_elem_in; 952506b1a0cSSebastian Grimberg elem_size_out = elem_size_in; 953506b1a0cSSebastian Grimberg num_comp_out = num_comp_in; 954506b1a0cSSebastian Grimberg } 955681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_in)); 956681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_out)); 957506b1a0cSSebastian Grimberg *num_entries = (CeedSize)elem_size_in * num_comp_in * elem_size_out * num_comp_out * num_elem_in; 958eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 959eaf62fffSJeremy L Thompson } 960eaf62fffSJeremy L Thompson 961eaf62fffSJeremy L Thompson /** 962ca94c3ddSJeremy L Thompson @brief Common code for creating a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` 963eaf62fffSJeremy L Thompson 964ca94c3ddSJeremy L Thompson @param[in] op_fine Fine grid `CeedOperator` 965ca94c3ddSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator` 966ca94c3ddSJeremy L Thompson @param[in] rstr_coarse Coarse grid `CeedElemRestriction` 967ca94c3ddSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector `CeedBasis` 968ca94c3ddSJeremy L Thompson @param[in] basis_c_to_f `CeedBasis` for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction operators 969ca94c3ddSJeremy L Thompson @param[out] op_coarse Coarse grid `CeedOperator` 970ca94c3ddSJeremy L Thompson @param[out] op_prolong Coarse to fine `CeedOperator`, or `NULL` 971ca94c3ddSJeremy L Thompson @param[out] op_restrict Fine to coarse `CeedOperator`, or `NULL` 972eaf62fffSJeremy L Thompson 973eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 974eaf62fffSJeremy L Thompson 975eaf62fffSJeremy L Thompson @ref Developer 976eaf62fffSJeremy L Thompson **/ 9772b730f8bSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 9787758292fSSebastian Grimberg CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) { 9791c66c397SJeremy L Thompson bool is_composite; 980eaf62fffSJeremy L Thompson Ceed ceed; 9811203703bSJeremy L Thompson CeedInt num_comp, num_input_fields, num_output_fields; 98285bb9dcfSJeremy L Thompson CeedVector mult_vec = NULL; 9831c66c397SJeremy L Thompson CeedElemRestriction rstr_p_mult_fine = NULL, rstr_fine = NULL; 9841203703bSJeremy L Thompson CeedOperatorField *input_fields, *output_fields; 9851c66c397SJeremy L Thompson 9862b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op_fine, &ceed)); 987eaf62fffSJeremy L Thompson 988eaf62fffSJeremy L Thompson // Check for composite operator 9892b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op_fine, &is_composite)); 9906574a04fSJeremy L Thompson CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Automatic multigrid setup for composite operators not supported"); 991eaf62fffSJeremy L Thompson 992eaf62fffSJeremy L Thompson // Coarse Grid 99399641342SJeremy L Thompson { 99499641342SJeremy L Thompson bool is_at_points; 99599641342SJeremy L Thompson 99699641342SJeremy L Thompson CeedCall(CeedOperatorIsAtPoints(op_fine, &is_at_points)); 99799641342SJeremy L Thompson if (is_at_points) { 99899641342SJeremy L Thompson CeedVector point_coords; 99999641342SJeremy L Thompson CeedElemRestriction rstr_points; 100099641342SJeremy L Thompson 100199641342SJeremy L Thompson CeedCall(CeedOperatorCreateAtPoints(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse)); 100299641342SJeremy L Thompson CeedCall(CeedOperatorAtPointsGetPoints(op_fine, &rstr_points, &point_coords)); 100399641342SJeremy L Thompson CeedCall(CeedOperatorAtPointsSetPoints(*op_coarse, rstr_points, point_coords)); 100499641342SJeremy L Thompson CeedCall(CeedVectorDestroy(&point_coords)); 100599641342SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_points)); 100699641342SJeremy L Thompson } else { 10072b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, op_coarse)); 100899641342SJeremy L Thompson } 100999641342SJeremy L Thompson } 10101203703bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op_fine, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 1011eaf62fffSJeremy L Thompson // -- Clone input fields 10121203703bSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 10136f8994e9SJeremy L Thompson const char *field_name; 10141203703bSJeremy L Thompson CeedVector vec; 1015681d0ea7SJeremy L Thompson CeedElemRestriction rstr = NULL; 1016681d0ea7SJeremy L Thompson CeedBasis basis = NULL; 10171203703bSJeremy L Thompson 10181203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetName(input_fields[i], &field_name)); 10191203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(input_fields[i], &vec)); 10201203703bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 1021681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionReferenceCopy(rstr_coarse, &rstr)); 1022681d0ea7SJeremy L Thompson CeedCall(CeedBasisReferenceCopy(basis_coarse, &basis)); 1023681d0ea7SJeremy L Thompson if (!rstr_fine) CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_fine)); 1024eaf62fffSJeremy L Thompson } else { 10251203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr)); 10261203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis)); 1027eaf62fffSJeremy L Thompson } 10281203703bSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec)); 1029681d0ea7SJeremy L Thompson CeedCall(CeedVectorDestroy(&vec)); 1030681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr)); 1031681d0ea7SJeremy L Thompson CeedCall(CeedBasisDestroy(&basis)); 1032eaf62fffSJeremy L Thompson } 1033eaf62fffSJeremy L Thompson // -- Clone output fields 10341203703bSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 10356f8994e9SJeremy L Thompson const char *field_name; 10361203703bSJeremy L Thompson CeedVector vec; 1037681d0ea7SJeremy L Thompson CeedElemRestriction rstr = NULL; 1038681d0ea7SJeremy L Thompson CeedBasis basis = NULL; 10391203703bSJeremy L Thompson 10401203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetName(output_fields[i], &field_name)); 10411203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec)); 10421203703bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 1043681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionReferenceCopy(rstr_coarse, &rstr)); 1044681d0ea7SJeremy L Thompson CeedCall(CeedBasisReferenceCopy(basis_coarse, &basis)); 1045681d0ea7SJeremy L Thompson if (!rstr_fine) CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_fine)); 1046eaf62fffSJeremy L Thompson } else { 10471203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr)); 10481203703bSJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis)); 1049eaf62fffSJeremy L Thompson } 10501203703bSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_coarse, field_name, rstr, basis, vec)); 1051681d0ea7SJeremy L Thompson CeedCall(CeedVectorDestroy(&vec)); 1052681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr)); 1053681d0ea7SJeremy L Thompson CeedCall(CeedBasisDestroy(&basis)); 1054eaf62fffSJeremy L Thompson } 1055af99e877SJeremy L Thompson // -- Clone QFunctionAssemblyData 10567d5185d7SSebastian Grimberg { 10577d5185d7SSebastian Grimberg CeedQFunctionAssemblyData fine_data; 10587d5185d7SSebastian Grimberg 10597d5185d7SSebastian Grimberg CeedCall(CeedOperatorGetQFunctionAssemblyData(op_fine, &fine_data)); 10607d5185d7SSebastian Grimberg CeedCall(CeedQFunctionAssemblyDataReferenceCopy(fine_data, &(*op_coarse)->qf_assembled)); 10617d5185d7SSebastian Grimberg } 1062eaf62fffSJeremy L Thompson 1063eaf62fffSJeremy L Thompson // Multiplicity vector 10647758292fSSebastian Grimberg if (op_restrict || op_prolong) { 106585bb9dcfSJeremy L Thompson CeedVector mult_e_vec; 10661c66c397SJeremy L Thompson CeedRestrictionType rstr_type; 106785bb9dcfSJeremy L Thompson 10687c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionGetType(rstr_fine, &rstr_type)); 10697c1dbaffSSebastian Grimberg CeedCheck(rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_UNSUPPORTED, 10707c1dbaffSSebastian Grimberg "Element restrictions created with CeedElemRestrictionCreateCurlOriented are not supported"); 10716574a04fSJeremy L Thompson CeedCheck(p_mult_fine, ceed, CEED_ERROR_INCOMPATIBLE, "Prolongation or restriction operator creation requires fine grid multiplicity vector"); 10727c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnsignedCopy(rstr_fine, &rstr_p_mult_fine)); 10732b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec)); 10742b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(mult_e_vec, 0.0)); 1075c17ec2beSJeremy L Thompson CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_NOTRANSPOSE, p_mult_fine, mult_e_vec, CEED_REQUEST_IMMEDIATE)); 10762b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(mult_vec, 0.0)); 1077c17ec2beSJeremy L Thompson CeedCall(CeedElemRestrictionApply(rstr_p_mult_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, CEED_REQUEST_IMMEDIATE)); 10782b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&mult_e_vec)); 10792b730f8bSJeremy L Thompson CeedCall(CeedVectorReciprocal(mult_vec)); 108085bb9dcfSJeremy L Thompson } 1081eaf62fffSJeremy L Thompson 1082addd79feSZach Atkins // Clone name 1083addd79feSZach Atkins bool has_name = op_fine->name; 1084addd79feSZach Atkins size_t name_len = op_fine->name ? strlen(op_fine->name) : 0; 1085addd79feSZach Atkins CeedCall(CeedOperatorSetName(*op_coarse, op_fine->name)); 1086addd79feSZach Atkins 10877758292fSSebastian Grimberg // Check that coarse to fine basis is provided if prolong/restrict operators are requested 10887758292fSSebastian Grimberg CeedCheck(basis_c_to_f || (!op_restrict && !op_prolong), ceed, CEED_ERROR_INCOMPATIBLE, 10896574a04fSJeremy L Thompson "Prolongation or restriction operator creation requires coarse-to-fine basis"); 109083d6adf3SZach Atkins 109185bb9dcfSJeremy L Thompson // Restriction/Prolongation Operators 10922b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_coarse, &num_comp)); 1093addd79feSZach Atkins 1094addd79feSZach Atkins // Restriction 10957758292fSSebastian Grimberg if (op_restrict) { 1096eaf62fffSJeremy L Thompson CeedInt *num_comp_r_data; 109785bb9dcfSJeremy L Thompson CeedQFunctionContext ctx_r; 10987758292fSSebastian Grimberg CeedQFunction qf_restrict; 109985bb9dcfSJeremy L Thompson 11007758292fSSebastian Grimberg CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict)); 11012b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &num_comp_r_data)); 1102eaf62fffSJeremy L Thompson num_comp_r_data[0] = num_comp; 11032b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextCreate(ceed, &ctx_r)); 11042b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_r_data), num_comp_r_data)); 11057758292fSSebastian Grimberg CeedCall(CeedQFunctionSetContext(qf_restrict, ctx_r)); 11062b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(&ctx_r)); 11077758292fSSebastian Grimberg CeedCall(CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE)); 11087758292fSSebastian Grimberg CeedCall(CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE)); 11097758292fSSebastian Grimberg CeedCall(CeedQFunctionAddOutput(qf_restrict, "output", num_comp, CEED_EVAL_INTERP)); 11107758292fSSebastian Grimberg CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp)); 1111eaf62fffSJeremy L Thompson 11127758292fSSebastian Grimberg CeedCall(CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_restrict)); 11137758292fSSebastian Grimberg CeedCall(CeedOperatorSetField(*op_restrict, "input", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE)); 11147758292fSSebastian Grimberg CeedCall(CeedOperatorSetField(*op_restrict, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec)); 11157758292fSSebastian Grimberg CeedCall(CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE)); 1116eaf62fffSJeremy L Thompson 1117addd79feSZach Atkins // Set name 1118addd79feSZach Atkins char *restriction_name; 11191c66c397SJeremy L Thompson 1120addd79feSZach Atkins CeedCall(CeedCalloc(17 + name_len, &restriction_name)); 1121addd79feSZach Atkins sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", has_name ? op_fine->name : ""); 11227758292fSSebastian Grimberg CeedCall(CeedOperatorSetName(*op_restrict, restriction_name)); 1123addd79feSZach Atkins CeedCall(CeedFree(&restriction_name)); 1124addd79feSZach Atkins 1125addd79feSZach Atkins // Check 11267758292fSSebastian Grimberg CeedCall(CeedOperatorCheckReady(*op_restrict)); 1127addd79feSZach Atkins 1128addd79feSZach Atkins // Cleanup 11297758292fSSebastian Grimberg CeedCall(CeedQFunctionDestroy(&qf_restrict)); 1130addd79feSZach Atkins } 1131addd79feSZach Atkins 1132eaf62fffSJeremy L Thompson // Prolongation 1133addd79feSZach Atkins if (op_prolong) { 1134eaf62fffSJeremy L Thompson CeedInt *num_comp_p_data; 113585bb9dcfSJeremy L Thompson CeedQFunctionContext ctx_p; 11361c66c397SJeremy L Thompson CeedQFunction qf_prolong; 113785bb9dcfSJeremy L Thompson 113885bb9dcfSJeremy L Thompson CeedCall(CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong)); 11392b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &num_comp_p_data)); 1140eaf62fffSJeremy L Thompson num_comp_p_data[0] = num_comp; 11412b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextCreate(ceed, &ctx_p)); 11422b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_p_data), num_comp_p_data)); 11432b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetContext(qf_prolong, ctx_p)); 11442b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(&ctx_p)); 11452b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP)); 11462b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE)); 11472b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE)); 11482b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp)); 1149eaf62fffSJeremy L Thompson 11502b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, op_prolong)); 11512b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, CEED_VECTOR_ACTIVE)); 1152356036faSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_prolong, "scale", rstr_p_mult_fine, CEED_BASIS_NONE, mult_vec)); 1153356036faSJeremy L Thompson CeedCall(CeedOperatorSetField(*op_prolong, "output", rstr_fine, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE)); 1154eaf62fffSJeremy L Thompson 1155addd79feSZach Atkins // Set name 1156ea6b5821SJeremy L Thompson char *prolongation_name; 11571c66c397SJeremy L Thompson 11582b730f8bSJeremy L Thompson CeedCall(CeedCalloc(18 + name_len, &prolongation_name)); 11592b730f8bSJeremy L Thompson sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", has_name ? op_fine->name : ""); 11602b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetName(*op_prolong, prolongation_name)); 11612b730f8bSJeremy L Thompson CeedCall(CeedFree(&prolongation_name)); 1162addd79feSZach Atkins 1163addd79feSZach Atkins // Check 1164addd79feSZach Atkins CeedCall(CeedOperatorCheckReady(*op_prolong)); 1165addd79feSZach Atkins 1166addd79feSZach Atkins // Cleanup 1167addd79feSZach Atkins CeedCall(CeedQFunctionDestroy(&qf_prolong)); 1168ea6b5821SJeremy L Thompson } 1169ea6b5821SJeremy L Thompson 117058e4b056SJeremy L Thompson // Check 117158e4b056SJeremy L Thompson CeedCall(CeedOperatorCheckReady(*op_coarse)); 117258e4b056SJeremy L Thompson 1173eaf62fffSJeremy L Thompson // Cleanup 11749bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 11752b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&mult_vec)); 1176681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_fine)); 1177c17ec2beSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine)); 11782b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_c_to_f)); 1179eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1180eaf62fffSJeremy L Thompson } 1181eaf62fffSJeremy L Thompson 1182eaf62fffSJeremy L Thompson /** 1183eaf62fffSJeremy L Thompson @brief Build 1D mass matrix and Laplacian with perturbation 1184eaf62fffSJeremy L Thompson 1185eaf62fffSJeremy L Thompson @param[in] interp_1d Interpolation matrix in one dimension 1186eaf62fffSJeremy L Thompson @param[in] grad_1d Gradient matrix in one dimension 1187eaf62fffSJeremy L Thompson @param[in] q_weight_1d Quadrature weights in one dimension 1188eaf62fffSJeremy L Thompson @param[in] P_1d Number of basis nodes in one dimension 1189eaf62fffSJeremy L Thompson @param[in] Q_1d Number of quadrature points in one dimension 1190eaf62fffSJeremy L Thompson @param[in] dim Dimension of basis 1191eaf62fffSJeremy L Thompson @param[out] mass Assembled mass matrix in one dimension 1192eaf62fffSJeremy L Thompson @param[out] laplace Assembled perturbed Laplacian in one dimension 1193eaf62fffSJeremy L Thompson 1194eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1195eaf62fffSJeremy L Thompson 1196eaf62fffSJeremy L Thompson @ref Developer 1197eaf62fffSJeremy L Thompson **/ 11982c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOff 11992c2ea1dbSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_weight_1d, CeedInt P_1d, CeedInt Q_1d, 12002c2ea1dbSJeremy L Thompson CeedInt dim, CeedScalar *mass, CeedScalar *laplace) { 12012b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) { 1202eaf62fffSJeremy L Thompson for (CeedInt j = 0; j < P_1d; j++) { 1203eaf62fffSJeremy L Thompson CeedScalar sum = 0.0; 12042b730f8bSJeremy 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]; 1205eaf62fffSJeremy L Thompson mass[i + j * P_1d] = sum; 1206eaf62fffSJeremy L Thompson } 12072b730f8bSJeremy L Thompson } 1208eaf62fffSJeremy L Thompson // -- Laplacian 12092b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) { 1210eaf62fffSJeremy L Thompson for (CeedInt j = 0; j < P_1d; j++) { 1211eaf62fffSJeremy L Thompson CeedScalar sum = 0.0; 12121c66c397SJeremy L Thompson 12132b730f8bSJeremy 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]; 1214eaf62fffSJeremy L Thompson laplace[i + j * P_1d] = sum; 1215eaf62fffSJeremy L Thompson } 12162b730f8bSJeremy L Thompson } 1217eaf62fffSJeremy L Thompson CeedScalar perturbation = dim > 2 ? 1e-6 : 1e-4; 12182b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) laplace[i + P_1d * i] += perturbation; 1219eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1220eaf62fffSJeremy L Thompson } 12212c2ea1dbSJeremy L Thompson CeedPragmaOptimizeOn 1222eaf62fffSJeremy L Thompson 1223eaf62fffSJeremy L Thompson /// @} 1224eaf62fffSJeremy L Thompson 1225eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 1226480fae85SJeremy L Thompson /// CeedOperator Backend API 1227480fae85SJeremy L Thompson /// ---------------------------------------------------------------------------- 1228480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend 1229480fae85SJeremy L Thompson /// @{ 1230480fae85SJeremy L Thompson 1231480fae85SJeremy L Thompson /** 1232004e4986SSebastian Grimberg @brief Select correct basis matrix pointer based on @ref CeedEvalMode 1233004e4986SSebastian Grimberg 1234004e4986SSebastian Grimberg @param[in] basis `CeedBasis` from which to get the basis matrix 1235004e4986SSebastian Grimberg @param[in] eval_mode Current basis evaluation mode 1236004e4986SSebastian Grimberg @param[in] identity Pointer to identity matrix 1237004e4986SSebastian Grimberg @param[out] basis_ptr `CeedBasis` pointer to set 1238004e4986SSebastian Grimberg 1239004e4986SSebastian Grimberg @ref Backend 1240004e4986SSebastian Grimberg **/ 1241004e4986SSebastian Grimberg int CeedOperatorGetBasisPointer(CeedBasis basis, CeedEvalMode eval_mode, const CeedScalar *identity, const CeedScalar **basis_ptr) { 1242004e4986SSebastian Grimberg switch (eval_mode) { 1243004e4986SSebastian Grimberg case CEED_EVAL_NONE: 1244004e4986SSebastian Grimberg *basis_ptr = identity; 1245004e4986SSebastian Grimberg break; 1246004e4986SSebastian Grimberg case CEED_EVAL_INTERP: 1247004e4986SSebastian Grimberg CeedCall(CeedBasisGetInterp(basis, basis_ptr)); 1248004e4986SSebastian Grimberg break; 1249004e4986SSebastian Grimberg case CEED_EVAL_GRAD: 1250004e4986SSebastian Grimberg CeedCall(CeedBasisGetGrad(basis, basis_ptr)); 1251004e4986SSebastian Grimberg break; 1252004e4986SSebastian Grimberg case CEED_EVAL_DIV: 1253004e4986SSebastian Grimberg CeedCall(CeedBasisGetDiv(basis, basis_ptr)); 1254004e4986SSebastian Grimberg break; 1255004e4986SSebastian Grimberg case CEED_EVAL_CURL: 1256004e4986SSebastian Grimberg CeedCall(CeedBasisGetCurl(basis, basis_ptr)); 1257004e4986SSebastian Grimberg break; 1258004e4986SSebastian Grimberg case CEED_EVAL_WEIGHT: 1259004e4986SSebastian Grimberg break; // Caught by QF Assembly 1260004e4986SSebastian Grimberg } 1261004e4986SSebastian Grimberg assert(*basis_ptr != NULL); 1262004e4986SSebastian Grimberg return CEED_ERROR_SUCCESS; 1263004e4986SSebastian Grimberg } 1264004e4986SSebastian Grimberg 1265004e4986SSebastian Grimberg /** 1266ca94c3ddSJeremy L Thompson @brief Create point block restriction for active `CeedOperatorField` 1267506b1a0cSSebastian Grimberg 1268ca94c3ddSJeremy L Thompson @param[in] rstr Original `CeedElemRestriction` for active field 1269ca94c3ddSJeremy L Thompson @param[out] point_block_rstr Address of the variable where the newly created `CeedElemRestriction` will be stored 1270506b1a0cSSebastian Grimberg 1271506b1a0cSSebastian Grimberg @return An error code: 0 - success, otherwise - failure 1272506b1a0cSSebastian Grimberg 1273506b1a0cSSebastian Grimberg @ref Backend 1274506b1a0cSSebastian Grimberg **/ 1275506b1a0cSSebastian Grimberg int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, CeedElemRestriction *point_block_rstr) { 1276506b1a0cSSebastian Grimberg Ceed ceed; 1277506b1a0cSSebastian Grimberg CeedInt num_elem, num_comp, shift, elem_size, comp_stride, *point_block_offsets; 1278506b1a0cSSebastian Grimberg CeedSize l_size; 1279506b1a0cSSebastian Grimberg const CeedInt *offsets; 1280506b1a0cSSebastian Grimberg 1281506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed)); 1282506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets)); 1283506b1a0cSSebastian Grimberg 1284506b1a0cSSebastian Grimberg // Expand offsets 1285506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem)); 1286506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp)); 1287506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size)); 1288506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetCompStride(rstr, &comp_stride)); 1289506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size)); 1290506b1a0cSSebastian Grimberg shift = num_comp; 1291506b1a0cSSebastian Grimberg if (comp_stride != 1) shift *= num_comp; 1292506b1a0cSSebastian Grimberg CeedCall(CeedCalloc(num_elem * elem_size, &point_block_offsets)); 1293506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_elem * elem_size; i++) { 1294506b1a0cSSebastian Grimberg point_block_offsets[i] = offsets[i] * shift; 1295506b1a0cSSebastian Grimberg } 1296506b1a0cSSebastian Grimberg 1297506b1a0cSSebastian Grimberg // Create new restriction 1298506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp * num_comp, 1, l_size * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER, 1299506b1a0cSSebastian Grimberg point_block_offsets, point_block_rstr)); 1300506b1a0cSSebastian Grimberg 1301506b1a0cSSebastian Grimberg // Cleanup 1302506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets)); 13039bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 1304506b1a0cSSebastian Grimberg return CEED_ERROR_SUCCESS; 1305506b1a0cSSebastian Grimberg } 1306506b1a0cSSebastian Grimberg 1307506b1a0cSSebastian Grimberg /** 13087d5185d7SSebastian Grimberg @brief Get `CeedQFunctionAssemblyData` 13097d5185d7SSebastian Grimberg 13107d5185d7SSebastian Grimberg @param[in] op `CeedOperator` to assemble 13117d5185d7SSebastian Grimberg @param[out] data `CeedQFunctionAssemblyData` 13127d5185d7SSebastian Grimberg 13137d5185d7SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 13147d5185d7SSebastian Grimberg 13157d5185d7SSebastian Grimberg @ref Backend 13167d5185d7SSebastian Grimberg **/ 13177d5185d7SSebastian Grimberg int CeedOperatorGetQFunctionAssemblyData(CeedOperator op, CeedQFunctionAssemblyData *data) { 13187d5185d7SSebastian Grimberg if (!op->qf_assembled) { 13197d5185d7SSebastian Grimberg CeedQFunctionAssemblyData data; 13207d5185d7SSebastian Grimberg 13217d5185d7SSebastian Grimberg CeedCall(CeedQFunctionAssemblyDataCreate(op->ceed, &data)); 13227d5185d7SSebastian Grimberg op->qf_assembled = data; 13237d5185d7SSebastian Grimberg } 13247d5185d7SSebastian Grimberg *data = op->qf_assembled; 13257d5185d7SSebastian Grimberg return CEED_ERROR_SUCCESS; 13267d5185d7SSebastian Grimberg } 13277d5185d7SSebastian Grimberg 13287d5185d7SSebastian Grimberg /** 1329ca94c3ddSJeremy L Thompson @brief Create object holding `CeedQFunction` assembly data for `CeedOperator` 1330480fae85SJeremy L Thompson 1331ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedQFunctionAssemblyData` 1332ca94c3ddSJeremy L Thompson @param[out] data Address of the variable where the newly created `CeedQFunctionAssemblyData` will be stored 1333480fae85SJeremy L Thompson 1334480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1335480fae85SJeremy L Thompson 1336480fae85SJeremy L Thompson @ref Backend 1337480fae85SJeremy L Thompson **/ 1338ea61e9acSJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, CeedQFunctionAssemblyData *data) { 13392b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, data)); 1340480fae85SJeremy L Thompson (*data)->ref_count = 1; 1341480fae85SJeremy L Thompson (*data)->ceed = ceed; 13422b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 1343480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1344480fae85SJeremy L Thompson } 1345480fae85SJeremy L Thompson 1346480fae85SJeremy L Thompson /** 1347ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedQFunctionAssemblyData` 1348480fae85SJeremy L Thompson 1349ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to increment the reference counter 1350480fae85SJeremy L Thompson 1351480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1352480fae85SJeremy L Thompson 1353480fae85SJeremy L Thompson @ref Backend 1354480fae85SJeremy L Thompson **/ 1355480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) { 1356480fae85SJeremy L Thompson data->ref_count++; 1357480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1358480fae85SJeremy L Thompson } 1359480fae85SJeremy L Thompson 1360480fae85SJeremy L Thompson /** 1361ca94c3ddSJeremy L Thompson @brief Set re-use of `CeedQFunctionAssemblyData` 13628b919e6bSJeremy L Thompson 1363ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to mark for reuse 1364ea61e9acSJeremy L Thompson @param[in] reuse_data Boolean flag indicating data re-use 13658b919e6bSJeremy L Thompson 13668b919e6bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 13678b919e6bSJeremy L Thompson 13688b919e6bSJeremy L Thompson @ref Backend 13698b919e6bSJeremy L Thompson **/ 13702b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, bool reuse_data) { 1371beecbf24SJeremy L Thompson data->reuse_data = reuse_data; 1372beecbf24SJeremy L Thompson data->needs_data_update = true; 1373beecbf24SJeremy L Thompson return CEED_ERROR_SUCCESS; 1374beecbf24SJeremy L Thompson } 1375beecbf24SJeremy L Thompson 1376beecbf24SJeremy L Thompson /** 1377ca94c3ddSJeremy L Thompson @brief Mark `CeedQFunctionAssemblyData` as stale 1378beecbf24SJeremy L Thompson 1379ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to mark as stale 1380ea61e9acSJeremy L Thompson @param[in] needs_data_update Boolean flag indicating if update is needed or completed 1381beecbf24SJeremy L Thompson 1382beecbf24SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1383beecbf24SJeremy L Thompson 1384beecbf24SJeremy L Thompson @ref Backend 1385beecbf24SJeremy L Thompson **/ 13862b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, bool needs_data_update) { 1387beecbf24SJeremy L Thompson data->needs_data_update = needs_data_update; 13888b919e6bSJeremy L Thompson return CEED_ERROR_SUCCESS; 13898b919e6bSJeremy L Thompson } 13908b919e6bSJeremy L Thompson 13918b919e6bSJeremy L Thompson /** 1392ca94c3ddSJeremy L Thompson @brief Determine if `CeedQFunctionAssemblyData` needs update 13938b919e6bSJeremy L Thompson 1394ca94c3ddSJeremy L Thompson @param[in] data `CeedQFunctionAssemblyData` to mark as stale 13958b919e6bSJeremy L Thompson @param[out] is_update_needed Boolean flag indicating if re-assembly is required 13968b919e6bSJeremy L Thompson 13978b919e6bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 13988b919e6bSJeremy L Thompson 13998b919e6bSJeremy L Thompson @ref Backend 14008b919e6bSJeremy L Thompson **/ 14012b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, bool *is_update_needed) { 1402beecbf24SJeremy L Thompson *is_update_needed = !data->reuse_data || data->needs_data_update; 14038b919e6bSJeremy L Thompson return CEED_ERROR_SUCCESS; 14048b919e6bSJeremy L Thompson } 14058b919e6bSJeremy L Thompson 14068b919e6bSJeremy L Thompson /** 1407ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedQFunctionAssemblyData`. 14084385fb7fSSebastian Grimberg 1409ca94c3ddSJeremy L Thompson Both pointers should be destroyed with @ref CeedQFunctionAssemblyDataDestroy(). 1410512bb800SJeremy L Thompson 1411ca94c3ddSJeremy 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`. 1412ca94c3ddSJeremy L Thompson This `CeedQFunctionAssemblyData` will be destroyed if ` *data_copy` is the only reference to this `CeedQFunctionAssemblyData`. 1413480fae85SJeremy L Thompson 1414ca94c3ddSJeremy L Thompson @param[in] data `CeedQFunctionAssemblyData` to copy reference to 1415ea61e9acSJeremy L Thompson @param[in,out] data_copy Variable to store copied reference 1416480fae85SJeremy L Thompson 1417480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1418480fae85SJeremy L Thompson 1419480fae85SJeremy L Thompson @ref Backend 1420480fae85SJeremy L Thompson **/ 14212b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, CeedQFunctionAssemblyData *data_copy) { 14222b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataReference(data)); 14232b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataDestroy(data_copy)); 1424480fae85SJeremy L Thompson *data_copy = data; 1425480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1426480fae85SJeremy L Thompson } 1427480fae85SJeremy L Thompson 1428480fae85SJeremy L Thompson /** 1429ca94c3ddSJeremy L Thompson @brief Get setup status for internal objects for `CeedQFunctionAssemblyData` 1430480fae85SJeremy L Thompson 1431ca94c3ddSJeremy L Thompson @param[in] data `CeedQFunctionAssemblyData` to retrieve status 1432480fae85SJeremy L Thompson @param[out] is_setup Boolean flag for setup status 1433480fae85SJeremy L Thompson 1434480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1435480fae85SJeremy L Thompson 1436480fae85SJeremy L Thompson @ref Backend 1437480fae85SJeremy L Thompson **/ 14382b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, bool *is_setup) { 1439480fae85SJeremy L Thompson *is_setup = data->is_setup; 1440480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1441480fae85SJeremy L Thompson } 1442480fae85SJeremy L Thompson 1443480fae85SJeremy L Thompson /** 1444ca94c3ddSJeremy L Thompson @brief Set internal objects for `CeedQFunctionAssemblyData` 1445480fae85SJeremy L Thompson 1446ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to set objects 1447ca94c3ddSJeremy L Thompson @param[in] vec `CeedVector` to store assembled `CeedQFunction` at quadrature points 1448ca94c3ddSJeremy L Thompson @param[in] rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction` 1449480fae85SJeremy L Thompson 1450480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1451480fae85SJeremy L Thompson 1452480fae85SJeremy L Thompson @ref Backend 1453480fae85SJeremy L Thompson **/ 14542b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, CeedVector vec, CeedElemRestriction rstr) { 14552b730f8bSJeremy L Thompson CeedCall(CeedVectorReferenceCopy(vec, &data->vec)); 14562b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionReferenceCopy(rstr, &data->rstr)); 1457480fae85SJeremy L Thompson 1458480fae85SJeremy L Thompson data->is_setup = true; 1459480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1460480fae85SJeremy L Thompson } 1461480fae85SJeremy L Thompson 14624dd1a9d2SSebastian Grimberg /** 1463ca94c3ddSJeremy L Thompson @brief Get internal objects for `CeedQFunctionAssemblyData` 14644dd1a9d2SSebastian Grimberg 1465ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to set objects 1466ca94c3ddSJeremy L Thompson @param[out] vec `CeedVector` to store assembled `CeedQFunction` at quadrature points 1467ca94c3ddSJeremy L Thompson @param[out] rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction` 14684dd1a9d2SSebastian Grimberg 14694dd1a9d2SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 14704dd1a9d2SSebastian Grimberg 14714dd1a9d2SSebastian Grimberg @ref Backend 14724dd1a9d2SSebastian Grimberg **/ 14732b730f8bSJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, CeedVector *vec, CeedElemRestriction *rstr) { 14746574a04fSJeremy L Thompson CeedCheck(data->is_setup, data->ceed, CEED_ERROR_INCOMPLETE, "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first."); 1475480fae85SJeremy L Thompson 14762b730f8bSJeremy L Thompson CeedCall(CeedVectorReferenceCopy(data->vec, vec)); 14772b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionReferenceCopy(data->rstr, rstr)); 1478480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1479480fae85SJeremy L Thompson } 1480480fae85SJeremy L Thompson 1481480fae85SJeremy L Thompson /** 1482ca94c3ddSJeremy L Thompson @brief Destroy `CeedQFunctionAssemblyData` 1483480fae85SJeremy L Thompson 1484ca94c3ddSJeremy L Thompson @param[in,out] data `CeedQFunctionAssemblyData` to destroy 1485480fae85SJeremy L Thompson 1486480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1487480fae85SJeremy L Thompson 1488480fae85SJeremy L Thompson @ref Backend 1489480fae85SJeremy L Thompson **/ 1490480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) { 1491ad6481ceSJeremy L Thompson if (!*data || --(*data)->ref_count > 0) { 1492ad6481ceSJeremy L Thompson *data = NULL; 1493ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1494ad6481ceSJeremy L Thompson } 14952b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*data)->ceed)); 14962b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*data)->vec)); 14972b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&(*data)->rstr)); 1498480fae85SJeremy L Thompson 14992b730f8bSJeremy L Thompson CeedCall(CeedFree(data)); 1500480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1501480fae85SJeremy L Thompson } 1502480fae85SJeremy L Thompson 1503ed9e99e6SJeremy L Thompson /** 1504ca94c3ddSJeremy L Thompson @brief Get `CeedOperatorAssemblyData` 1505ed9e99e6SJeremy L Thompson 1506ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 15077d5185d7SSebastian Grimberg @param[out] data `CeedOperatorAssemblyData` 1508ed9e99e6SJeremy L Thompson 1509ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1510ed9e99e6SJeremy L Thompson 1511ed9e99e6SJeremy L Thompson @ref Backend 1512ed9e99e6SJeremy L Thompson **/ 15132b730f8bSJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, CeedOperatorAssemblyData *data) { 1514ed9e99e6SJeremy L Thompson if (!op->op_assembled) { 1515ed9e99e6SJeremy L Thompson CeedOperatorAssemblyData data; 1516ed9e99e6SJeremy L Thompson 15172b730f8bSJeremy L Thompson CeedCall(CeedOperatorAssemblyDataCreate(op->ceed, op, &data)); 1518ed9e99e6SJeremy L Thompson op->op_assembled = data; 1519ed9e99e6SJeremy L Thompson } 1520ed9e99e6SJeremy L Thompson *data = op->op_assembled; 1521ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1522ed9e99e6SJeremy L Thompson } 1523ed9e99e6SJeremy L Thompson 1524ed9e99e6SJeremy L Thompson /** 1525ca94c3ddSJeremy L Thompson @brief Create object holding `CeedOperator` assembly data. 1526ba746a46SJeremy L Thompson 1527ca94c3ddSJeremy L Thompson The `CeedOperatorAssemblyData` holds an array with references to every active `CeedBasis` used in the `CeedOperator`. 1528ca94c3ddSJeremy L Thompson An array with references to the corresponding active `CeedElemRestriction` is also stored. 1529ca94c3ddSJeremy L Thompson For each active `CeedBasis, the `CeedOperatorAssemblyData` holds an array of all input and output @ref CeedEvalMode for this `CeedBasis`. 1530ca94c3ddSJeremy L Thompson The `CeedOperatorAssemblyData` holds an array of offsets for indexing into the assembled `CeedQFunction` arrays to the row representing each @ref CeedEvalMode. 1531ca94c3ddSJeremy L Thompson The number of input columns across all active bases for the assembled `CeedQFunction` is also stored. 1532ca94c3ddSJeremy L Thompson Lastly, the `CeedOperatorAssembly` data holds assembled matrices representing the full action of the `CeedBasis` for all @ref CeedEvalMode. 1533ed9e99e6SJeremy L Thompson 1534ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedOperatorAssemblyData` 1535ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to be assembled 1536ca94c3ddSJeremy L Thompson @param[out] data Address of the variable where the newly created `CeedOperatorAssemblyData` will be stored 1537ed9e99e6SJeremy L Thompson 1538ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1539ed9e99e6SJeremy L Thompson 1540ed9e99e6SJeremy L Thompson @ref Backend 1541ed9e99e6SJeremy L Thompson **/ 15422b730f8bSJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssemblyData *data) { 1543506b1a0cSSebastian Grimberg CeedInt num_active_bases_in = 0, num_active_bases_out = 0, offset = 0; 1544506b1a0cSSebastian Grimberg CeedInt num_input_fields, *num_eval_modes_in = NULL, num_output_fields, *num_eval_modes_out = NULL; 15451c66c397SJeremy L Thompson CeedSize **eval_mode_offsets_in = NULL, **eval_mode_offsets_out = NULL; 15461c66c397SJeremy L Thompson CeedEvalMode **eval_modes_in = NULL, **eval_modes_out = NULL; 15471c66c397SJeremy L Thompson CeedQFunctionField *qf_fields; 15481c66c397SJeremy L Thompson CeedQFunction qf; 15491c66c397SJeremy L Thompson CeedOperatorField *op_fields; 155001f0e615SJames Wright bool is_composite; 155101f0e615SJames Wright 155201f0e615SJames Wright CeedCall(CeedOperatorIsComposite(op, &is_composite)); 155301f0e615SJames Wright CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Can only create CeedOperator assembly data for non-composite operators."); 1554437c7c90SJeremy L Thompson 1555437c7c90SJeremy L Thompson // Allocate 15562b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, data)); 1557ed9e99e6SJeremy L Thompson (*data)->ceed = ceed; 15582b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 1559ed9e99e6SJeremy L Thompson 1560ed9e99e6SJeremy L Thompson // Build OperatorAssembly data 15612b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetQFunction(op, &qf)); 1562ed9e99e6SJeremy L Thompson 1563ed9e99e6SJeremy L Thompson // Determine active input basis 1564004e4986SSebastian Grimberg CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL)); 1565004e4986SSebastian Grimberg CeedCall(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); 1566ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1567ed9e99e6SJeremy L Thompson CeedVector vec; 15681c66c397SJeremy L Thompson 15692b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec)); 1570ed9e99e6SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 15717c1dbaffSSebastian Grimberg CeedInt index = -1, num_comp, q_comp; 15721c66c397SJeremy L Thompson CeedEvalMode eval_mode; 15731c66c397SJeremy L Thompson CeedBasis basis_in = NULL; 15741c66c397SJeremy L Thompson 15752b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_in)); 15762b730f8bSJeremy L Thompson CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1577352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumComponents(basis_in, &num_comp)); 1578352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp)); 1579506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_active_bases_in; i++) { 1580506b1a0cSSebastian Grimberg if ((*data)->active_bases_in[i] == basis_in) index = i; 1581437c7c90SJeremy L Thompson } 1582437c7c90SJeremy L Thompson if (index == -1) { 1583437c7c90SJeremy L Thompson CeedElemRestriction elem_rstr_in; 15841c66c397SJeremy L Thompson 1585506b1a0cSSebastian Grimberg index = num_active_bases_in; 1586506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_bases_in)); 1587506b1a0cSSebastian Grimberg (*data)->active_bases_in[num_active_bases_in] = NULL; 1588506b1a0cSSebastian Grimberg CeedCall(CeedBasisReferenceCopy(basis_in, &(*data)->active_bases_in[num_active_bases_in])); 1589506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->active_elem_rstrs_in)); 1590506b1a0cSSebastian Grimberg (*data)->active_elem_rstrs_in[num_active_bases_in] = NULL; 1591437c7c90SJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_in)); 1592506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_in, &(*data)->active_elem_rstrs_in[num_active_bases_in])); 1593681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&elem_rstr_in)); 1594506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &num_eval_modes_in)); 1595437c7c90SJeremy L Thompson num_eval_modes_in[index] = 0; 1596506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_modes_in)); 1597437c7c90SJeremy L Thompson eval_modes_in[index] = NULL; 1598506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &eval_mode_offsets_in)); 1599437c7c90SJeremy L Thompson eval_mode_offsets_in[index] = NULL; 1600506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_in + 1, &(*data)->assembled_bases_in)); 1601437c7c90SJeremy L Thompson (*data)->assembled_bases_in[index] = NULL; 1602506b1a0cSSebastian Grimberg num_active_bases_in++; 1603437c7c90SJeremy L Thompson } 1604352a5e7cSSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1605352a5e7cSSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 1606352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_modes_in[index])); 1607352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_in[index] + q_comp, &eval_mode_offsets_in[index])); 1608352a5e7cSSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 1609437c7c90SJeremy L Thompson eval_modes_in[index][num_eval_modes_in[index] + d] = eval_mode; 1610437c7c90SJeremy L Thompson eval_mode_offsets_in[index][num_eval_modes_in[index] + d] = offset; 1611352a5e7cSSebastian Grimberg offset += num_comp; 1612ed9e99e6SJeremy L Thompson } 1613352a5e7cSSebastian Grimberg num_eval_modes_in[index] += q_comp; 1614ed9e99e6SJeremy L Thompson } 1615681d0ea7SJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_in)); 1616ed9e99e6SJeremy L Thompson } 1617681d0ea7SJeremy L Thompson CeedCall(CeedVectorDestroy(&vec)); 1618ed9e99e6SJeremy L Thompson } 1619ed9e99e6SJeremy L Thompson 1620ed9e99e6SJeremy L Thompson // Determine active output basis 16212b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields)); 16222b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields)); 1623437c7c90SJeremy L Thompson offset = 0; 1624ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1625ed9e99e6SJeremy L Thompson CeedVector vec; 16261c66c397SJeremy L Thompson 16272b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec)); 1628ed9e99e6SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 16297c1dbaffSSebastian Grimberg CeedInt index = -1, num_comp, q_comp; 16301c66c397SJeremy L Thompson CeedEvalMode eval_mode; 16311c66c397SJeremy L Thompson CeedBasis basis_out = NULL; 16321c66c397SJeremy L Thompson 1633437c7c90SJeremy L Thompson CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis_out)); 16342b730f8bSJeremy L Thompson CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 1635352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumComponents(basis_out, &num_comp)); 1636352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp)); 1637506b1a0cSSebastian Grimberg for (CeedInt i = 0; i < num_active_bases_out; i++) { 1638506b1a0cSSebastian Grimberg if ((*data)->active_bases_out[i] == basis_out) index = i; 1639437c7c90SJeremy L Thompson } 1640437c7c90SJeremy L Thompson if (index == -1) { 1641437c7c90SJeremy L Thompson CeedElemRestriction elem_rstr_out; 16421c66c397SJeremy L Thompson 1643506b1a0cSSebastian Grimberg index = num_active_bases_out; 1644506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_bases_out)); 1645506b1a0cSSebastian Grimberg (*data)->active_bases_out[num_active_bases_out] = NULL; 1646506b1a0cSSebastian Grimberg CeedCall(CeedBasisReferenceCopy(basis_out, &(*data)->active_bases_out[num_active_bases_out])); 1647506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->active_elem_rstrs_out)); 1648506b1a0cSSebastian Grimberg (*data)->active_elem_rstrs_out[num_active_bases_out] = NULL; 1649437c7c90SJeremy L Thompson CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr_out)); 1650506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionReferenceCopy(elem_rstr_out, &(*data)->active_elem_rstrs_out[num_active_bases_out])); 1651681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&elem_rstr_out)); 1652506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &num_eval_modes_out)); 1653437c7c90SJeremy L Thompson num_eval_modes_out[index] = 0; 1654506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_modes_out)); 1655437c7c90SJeremy L Thompson eval_modes_out[index] = NULL; 1656506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &eval_mode_offsets_out)); 1657437c7c90SJeremy L Thompson eval_mode_offsets_out[index] = NULL; 1658506b1a0cSSebastian Grimberg CeedCall(CeedRealloc(num_active_bases_out + 1, &(*data)->assembled_bases_out)); 1659437c7c90SJeremy L Thompson (*data)->assembled_bases_out[index] = NULL; 1660506b1a0cSSebastian Grimberg num_active_bases_out++; 1661437c7c90SJeremy L Thompson } 1662352a5e7cSSebastian Grimberg if (eval_mode != CEED_EVAL_WEIGHT) { 1663352a5e7cSSebastian Grimberg // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly 1664352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_modes_out[index])); 1665352a5e7cSSebastian Grimberg CeedCall(CeedRealloc(num_eval_modes_out[index] + q_comp, &eval_mode_offsets_out[index])); 1666352a5e7cSSebastian Grimberg for (CeedInt d = 0; d < q_comp; d++) { 1667437c7c90SJeremy L Thompson eval_modes_out[index][num_eval_modes_out[index] + d] = eval_mode; 1668437c7c90SJeremy L Thompson eval_mode_offsets_out[index][num_eval_modes_out[index] + d] = offset; 1669352a5e7cSSebastian Grimberg offset += num_comp; 1670ed9e99e6SJeremy L Thompson } 1671352a5e7cSSebastian Grimberg num_eval_modes_out[index] += q_comp; 1672ed9e99e6SJeremy L Thompson } 1673681d0ea7SJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_out)); 1674ed9e99e6SJeremy L Thompson } 1675681d0ea7SJeremy L Thompson CeedCall(CeedVectorDestroy(&vec)); 1676ed9e99e6SJeremy L Thompson } 1677c11e12f4SJeremy L Thompson CeedCall(CeedQFunctionDestroy(&qf)); 1678506b1a0cSSebastian Grimberg (*data)->num_active_bases_in = num_active_bases_in; 167927789c4aSJed Brown (*data)->num_eval_modes_in = num_eval_modes_in; 168027789c4aSJed Brown (*data)->eval_modes_in = eval_modes_in; 168127789c4aSJed Brown (*data)->eval_mode_offsets_in = eval_mode_offsets_in; 1682506b1a0cSSebastian Grimberg (*data)->num_active_bases_out = num_active_bases_out; 1683437c7c90SJeremy L Thompson (*data)->num_eval_modes_out = num_eval_modes_out; 1684437c7c90SJeremy L Thompson (*data)->eval_modes_out = eval_modes_out; 1685437c7c90SJeremy L Thompson (*data)->eval_mode_offsets_out = eval_mode_offsets_out; 1686506b1a0cSSebastian Grimberg (*data)->num_output_components = offset; 1687ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1688ed9e99e6SJeremy L Thompson } 1689ed9e99e6SJeremy L Thompson 1690ed9e99e6SJeremy L Thompson /** 1691ca94c3ddSJeremy L Thompson @brief Get `CeedOperator` @ref CeedEvalMode for assembly. 1692ba746a46SJeremy L Thompson 1693ca94c3ddSJeremy L Thompson Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object. 1694ed9e99e6SJeremy L Thompson 1695ca94c3ddSJeremy L Thompson @param[in] data `CeedOperatorAssemblyData` 1696506b1a0cSSebastian Grimberg @param[out] num_active_bases_in Total number of active bases for input 1697ca94c3ddSJeremy L Thompson @param[out] num_eval_modes_in Pointer to hold array of numbers of input @ref CeedEvalMode, or `NULL`. 1698ca94c3ddSJeremy L Thompson `eval_modes_in[0]` holds an array of eval modes for the first active `CeedBasis`. 1699ca94c3ddSJeremy L Thompson @param[out] eval_modes_in Pointer to hold arrays of input @ref CeedEvalMode, or `NULL` 1700ca94c3ddSJeremy L Thompson @param[out] eval_mode_offsets_in Pointer to hold arrays of input offsets at each quadrature point 1701506b1a0cSSebastian Grimberg @param[out] num_active_bases_out Total number of active bases for output 1702ca94c3ddSJeremy L Thompson @param[out] num_eval_modes_out Pointer to hold array of numbers of output @ref CeedEvalMode, or `NULL` 1703ca94c3ddSJeremy L Thompson @param[out] eval_modes_out Pointer to hold arrays of output @ref CeedEvalMode, or `NULL` 1704437c7c90SJeremy L Thompson @param[out] eval_mode_offsets_out Pointer to hold arrays of output offsets at each quadrature point 1705ca94c3ddSJeremy 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 1706ed9e99e6SJeremy L Thompson 1707ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1708ed9e99e6SJeremy L Thompson 1709ed9e99e6SJeremy L Thompson @ref Backend 1710ed9e99e6SJeremy L Thompson **/ 1711506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedInt **num_eval_modes_in, 1712506b1a0cSSebastian Grimberg const CeedEvalMode ***eval_modes_in, CeedSize ***eval_mode_offsets_in, CeedInt *num_active_bases_out, 1713506b1a0cSSebastian Grimberg CeedInt **num_eval_modes_out, const CeedEvalMode ***eval_modes_out, CeedSize ***eval_mode_offsets_out, 1714506b1a0cSSebastian Grimberg CeedSize *num_output_components) { 1715506b1a0cSSebastian Grimberg if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in; 1716437c7c90SJeremy L Thompson if (num_eval_modes_in) *num_eval_modes_in = data->num_eval_modes_in; 1717437c7c90SJeremy L Thompson if (eval_modes_in) *eval_modes_in = (const CeedEvalMode **)data->eval_modes_in; 1718437c7c90SJeremy L Thompson if (eval_mode_offsets_in) *eval_mode_offsets_in = data->eval_mode_offsets_in; 1719506b1a0cSSebastian Grimberg if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out; 1720437c7c90SJeremy L Thompson if (num_eval_modes_out) *num_eval_modes_out = data->num_eval_modes_out; 1721437c7c90SJeremy L Thompson if (eval_modes_out) *eval_modes_out = (const CeedEvalMode **)data->eval_modes_out; 1722437c7c90SJeremy L Thompson if (eval_mode_offsets_out) *eval_mode_offsets_out = data->eval_mode_offsets_out; 1723437c7c90SJeremy L Thompson if (num_output_components) *num_output_components = data->num_output_components; 1724ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1725ed9e99e6SJeremy L Thompson } 1726ed9e99e6SJeremy L Thompson 1727ed9e99e6SJeremy L Thompson /** 1728ca94c3ddSJeremy L Thompson @brief Get `CeedOperator` `CeedBasis` data for assembly. 1729ba746a46SJeremy L Thompson 1730ca94c3ddSJeremy L Thompson Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object. 1731ed9e99e6SJeremy L Thompson 1732ca94c3ddSJeremy L Thompson @param[in] data `CeedOperatorAssemblyData` 1733ca94c3ddSJeremy L Thompson @param[out] num_active_bases_in Number of active input bases, or `NULL` 1734ca94c3ddSJeremy L Thompson @param[out] active_bases_in Pointer to hold active input `CeedBasis`, or `NULL` 1735ca94c3ddSJeremy L Thompson @param[out] assembled_bases_in Pointer to hold assembled active input `B` , or `NULL` 1736ca94c3ddSJeremy L Thompson @param[out] num_active_bases_out Number of active output bases, or `NULL` 1737ca94c3ddSJeremy L Thompson @param[out] active_bases_out Pointer to hold active output `CeedBasis`, or `NULL` 1738ca94c3ddSJeremy L Thompson @param[out] assembled_bases_out Pointer to hold assembled active output `B` , or `NULL` 1739ed9e99e6SJeremy L Thompson 1740ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1741ed9e99e6SJeremy L Thompson 1742ed9e99e6SJeremy L Thompson @ref Backend 1743ed9e99e6SJeremy L Thompson **/ 1744506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, CeedInt *num_active_bases_in, CeedBasis **active_bases_in, 1745506b1a0cSSebastian Grimberg const CeedScalar ***assembled_bases_in, CeedInt *num_active_bases_out, CeedBasis **active_bases_out, 1746506b1a0cSSebastian Grimberg const CeedScalar ***assembled_bases_out) { 1747ed9e99e6SJeremy L Thompson // Assemble B_in, B_out if needed 1748437c7c90SJeremy L Thompson if (assembled_bases_in && !data->assembled_bases_in[0]) { 1749437c7c90SJeremy L Thompson CeedInt num_qpts; 1750437c7c90SJeremy L Thompson 1751506b1a0cSSebastian Grimberg if (data->active_bases_in[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[0], &num_qpts)); 1752506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_in[0], &num_qpts)); 1753506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < data->num_active_bases_in; b++) { 17541c66c397SJeremy L Thompson bool has_eval_none = false; 1755352a5e7cSSebastian Grimberg CeedInt num_nodes; 1756437c7c90SJeremy L Thompson CeedScalar *B_in = NULL, *identity = NULL; 1757ed9e99e6SJeremy L Thompson 1758506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_in[b], &num_nodes)); 1759352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_in[b], &B_in)); 1760ed9e99e6SJeremy L Thompson 1761437c7c90SJeremy L Thompson for (CeedInt i = 0; i < data->num_eval_modes_in[b]; i++) { 1762437c7c90SJeremy L Thompson has_eval_none = has_eval_none || (data->eval_modes_in[b][i] == CEED_EVAL_NONE); 1763ed9e99e6SJeremy L Thompson } 1764ed9e99e6SJeremy L Thompson if (has_eval_none) { 1765352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes, &identity)); 1766352a5e7cSSebastian Grimberg for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) { 1767352a5e7cSSebastian Grimberg identity[i * num_nodes + i] = 1.0; 1768ed9e99e6SJeremy L Thompson } 1769ed9e99e6SJeremy L Thompson } 1770ed9e99e6SJeremy L Thompson 1771ed9e99e6SJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 1772352a5e7cSSebastian Grimberg for (CeedInt n = 0; n < num_nodes; n++) { 1773352a5e7cSSebastian Grimberg CeedInt d_in = 0, q_comp_in; 1774352a5e7cSSebastian Grimberg CeedEvalMode eval_mode_in_prev = CEED_EVAL_NONE; 17751c66c397SJeremy L Thompson 1776437c7c90SJeremy L Thompson for (CeedInt e_in = 0; e_in < data->num_eval_modes_in[b]; e_in++) { 1777437c7c90SJeremy L Thompson const CeedInt qq = data->num_eval_modes_in[b] * q; 1778437c7c90SJeremy L Thompson const CeedScalar *B = NULL; 17791c66c397SJeremy L Thompson 1780506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(data->active_bases_in[b], data->eval_modes_in[b][e_in], identity, &B)); 1781506b1a0cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_in[b], data->eval_modes_in[b][e_in], &q_comp_in)); 1782352a5e7cSSebastian Grimberg if (q_comp_in > 1) { 1783352a5e7cSSebastian Grimberg if (e_in == 0 || data->eval_modes_in[b][e_in] != eval_mode_in_prev) d_in = 0; 1784352a5e7cSSebastian Grimberg else B = &B[(++d_in) * num_qpts * num_nodes]; 1785352a5e7cSSebastian Grimberg } 1786352a5e7cSSebastian Grimberg eval_mode_in_prev = data->eval_modes_in[b][e_in]; 1787352a5e7cSSebastian Grimberg B_in[(qq + e_in) * num_nodes + n] = B[q * num_nodes + n]; 1788ed9e99e6SJeremy L Thompson } 1789ed9e99e6SJeremy L Thompson } 1790ed9e99e6SJeremy L Thompson } 17917c1dbaffSSebastian Grimberg if (identity) CeedCall(CeedFree(&identity)); 1792437c7c90SJeremy L Thompson data->assembled_bases_in[b] = B_in; 1793437c7c90SJeremy L Thompson } 1794ed9e99e6SJeremy L Thompson } 1795ed9e99e6SJeremy L Thompson 1796437c7c90SJeremy L Thompson if (assembled_bases_out && !data->assembled_bases_out[0]) { 1797437c7c90SJeremy L Thompson CeedInt num_qpts; 1798437c7c90SJeremy L Thompson 1799506b1a0cSSebastian Grimberg if (data->active_bases_out[0] == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[0], &num_qpts)); 1800506b1a0cSSebastian Grimberg else CeedCall(CeedBasisGetNumQuadraturePoints(data->active_bases_out[0], &num_qpts)); 1801506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < data->num_active_bases_out; b++) { 1802ed9e99e6SJeremy L Thompson bool has_eval_none = false; 18031c66c397SJeremy L Thompson CeedInt num_nodes; 1804437c7c90SJeremy L Thompson CeedScalar *B_out = NULL, *identity = NULL; 1805ed9e99e6SJeremy L Thompson 1806506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetElementSize(data->active_elem_rstrs_out[b], &num_nodes)); 1807352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes * data->num_eval_modes_out[b], &B_out)); 1808ed9e99e6SJeremy L Thompson 1809437c7c90SJeremy L Thompson for (CeedInt i = 0; i < data->num_eval_modes_out[b]; i++) { 1810437c7c90SJeremy L Thompson has_eval_none = has_eval_none || (data->eval_modes_out[b][i] == CEED_EVAL_NONE); 1811ed9e99e6SJeremy L Thompson } 1812ed9e99e6SJeremy L Thompson if (has_eval_none) { 1813352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_qpts * num_nodes, &identity)); 1814352a5e7cSSebastian Grimberg for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) { 1815352a5e7cSSebastian Grimberg identity[i * num_nodes + i] = 1.0; 1816ed9e99e6SJeremy L Thompson } 1817ed9e99e6SJeremy L Thompson } 1818ed9e99e6SJeremy L Thompson 1819ed9e99e6SJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 1820352a5e7cSSebastian Grimberg for (CeedInt n = 0; n < num_nodes; n++) { 1821352a5e7cSSebastian Grimberg CeedInt d_out = 0, q_comp_out; 1822352a5e7cSSebastian Grimberg CeedEvalMode eval_mode_out_prev = CEED_EVAL_NONE; 18231c66c397SJeremy L Thompson 1824437c7c90SJeremy L Thompson for (CeedInt e_out = 0; e_out < data->num_eval_modes_out[b]; e_out++) { 1825437c7c90SJeremy L Thompson const CeedInt qq = data->num_eval_modes_out[b] * q; 1826437c7c90SJeremy L Thompson const CeedScalar *B = NULL; 18271c66c397SJeremy L Thompson 1828506b1a0cSSebastian Grimberg CeedCall(CeedOperatorGetBasisPointer(data->active_bases_out[b], data->eval_modes_out[b][e_out], identity, &B)); 1829506b1a0cSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(data->active_bases_out[b], data->eval_modes_out[b][e_out], &q_comp_out)); 1830352a5e7cSSebastian Grimberg if (q_comp_out > 1) { 1831352a5e7cSSebastian Grimberg if (e_out == 0 || data->eval_modes_out[b][e_out] != eval_mode_out_prev) d_out = 0; 1832352a5e7cSSebastian Grimberg else B = &B[(++d_out) * num_qpts * num_nodes]; 1833352a5e7cSSebastian Grimberg } 1834352a5e7cSSebastian Grimberg eval_mode_out_prev = data->eval_modes_out[b][e_out]; 1835352a5e7cSSebastian Grimberg B_out[(qq + e_out) * num_nodes + n] = B[q * num_nodes + n]; 1836ed9e99e6SJeremy L Thompson } 1837ed9e99e6SJeremy L Thompson } 1838ed9e99e6SJeremy L Thompson } 18397c1dbaffSSebastian Grimberg if (identity) CeedCall(CeedFree(&identity)); 1840437c7c90SJeremy L Thompson data->assembled_bases_out[b] = B_out; 1841437c7c90SJeremy L Thompson } 1842ed9e99e6SJeremy L Thompson } 1843ed9e99e6SJeremy L Thompson 1844437c7c90SJeremy L Thompson // Pass out assembled data 1845506b1a0cSSebastian Grimberg if (num_active_bases_in) *num_active_bases_in = data->num_active_bases_in; 1846506b1a0cSSebastian Grimberg if (active_bases_in) *active_bases_in = data->active_bases_in; 1847437c7c90SJeremy L Thompson if (assembled_bases_in) *assembled_bases_in = (const CeedScalar **)data->assembled_bases_in; 1848506b1a0cSSebastian Grimberg if (num_active_bases_out) *num_active_bases_out = data->num_active_bases_out; 1849506b1a0cSSebastian Grimberg if (active_bases_out) *active_bases_out = data->active_bases_out; 1850437c7c90SJeremy L Thompson if (assembled_bases_out) *assembled_bases_out = (const CeedScalar **)data->assembled_bases_out; 1851437c7c90SJeremy L Thompson return CEED_ERROR_SUCCESS; 1852437c7c90SJeremy L Thompson } 1853437c7c90SJeremy L Thompson 1854437c7c90SJeremy L Thompson /** 1855ca94c3ddSJeremy L Thompson @brief Get `CeedOperator` `CeedBasis` data for assembly. 1856ba746a46SJeremy L Thompson 1857ca94c3ddSJeremy L Thompson Note: See @ref CeedOperatorAssemblyDataCreate() for a full description of the data stored in this object. 1858437c7c90SJeremy L Thompson 1859ca94c3ddSJeremy L Thompson @param[in] data `CeedOperatorAssemblyData` 1860ca94c3ddSJeremy L Thompson @param[out] num_active_elem_rstrs_in Number of active input element restrictions, or `NULL` 1861ca94c3ddSJeremy L Thompson @param[out] active_elem_rstrs_in Pointer to hold active input `CeedElemRestriction`, or `NULL` 1862ca94c3ddSJeremy L Thompson @param[out] num_active_elem_rstrs_out Number of active output element restrictions, or `NULL` 1863ca94c3ddSJeremy L Thompson @param[out] active_elem_rstrs_out Pointer to hold active output `CeedElemRestriction`, or `NULL` 1864437c7c90SJeremy L Thompson 1865437c7c90SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1866437c7c90SJeremy L Thompson 1867437c7c90SJeremy L Thompson @ref Backend 1868437c7c90SJeremy L Thompson **/ 1869506b1a0cSSebastian Grimberg int CeedOperatorAssemblyDataGetElemRestrictions(CeedOperatorAssemblyData data, CeedInt *num_active_elem_rstrs_in, 1870506b1a0cSSebastian Grimberg CeedElemRestriction **active_elem_rstrs_in, CeedInt *num_active_elem_rstrs_out, 1871506b1a0cSSebastian Grimberg CeedElemRestriction **active_elem_rstrs_out) { 1872506b1a0cSSebastian Grimberg if (num_active_elem_rstrs_in) *num_active_elem_rstrs_in = data->num_active_bases_in; 1873506b1a0cSSebastian Grimberg if (active_elem_rstrs_in) *active_elem_rstrs_in = data->active_elem_rstrs_in; 1874506b1a0cSSebastian Grimberg if (num_active_elem_rstrs_out) *num_active_elem_rstrs_out = data->num_active_bases_out; 1875506b1a0cSSebastian Grimberg if (active_elem_rstrs_out) *active_elem_rstrs_out = data->active_elem_rstrs_out; 1876ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1877ed9e99e6SJeremy L Thompson } 1878ed9e99e6SJeremy L Thompson 1879ed9e99e6SJeremy L Thompson /** 1880ca94c3ddSJeremy L Thompson @brief Destroy `CeedOperatorAssemblyData` 1881ed9e99e6SJeremy L Thompson 1882ca94c3ddSJeremy L Thompson @param[in,out] data `CeedOperatorAssemblyData` to destroy 1883ed9e99e6SJeremy L Thompson 1884ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1885ed9e99e6SJeremy L Thompson 1886ed9e99e6SJeremy L Thompson @ref Backend 1887ed9e99e6SJeremy L Thompson **/ 1888ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) { 1889ad6481ceSJeremy L Thompson if (!*data) { 1890ad6481ceSJeremy L Thompson *data = NULL; 1891ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1892ad6481ceSJeremy L Thompson } 18932b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*data)->ceed)); 1894506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < (*data)->num_active_bases_in; b++) { 1895506b1a0cSSebastian Grimberg CeedCall(CeedBasisDestroy(&(*data)->active_bases_in[b])); 1896506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_in[b])); 1897437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_modes_in[b])); 1898437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_mode_offsets_in[b])); 1899437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_in[b])); 1900506b1a0cSSebastian Grimberg } 1901506b1a0cSSebastian Grimberg for (CeedInt b = 0; b < (*data)->num_active_bases_out; b++) { 1902506b1a0cSSebastian Grimberg CeedCall(CeedBasisDestroy(&(*data)->active_bases_out[b])); 1903506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&(*data)->active_elem_rstrs_out[b])); 1904506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->eval_modes_out[b])); 1905506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->eval_mode_offsets_out[b])); 1906437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_out[b])); 1907437c7c90SJeremy L Thompson } 1908506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_bases_in)); 1909506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_bases_out)); 1910506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_elem_rstrs_in)); 1911506b1a0cSSebastian Grimberg CeedCall(CeedFree(&(*data)->active_elem_rstrs_out)); 1912437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->num_eval_modes_in)); 1913437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->num_eval_modes_out)); 1914437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_modes_in)); 1915437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_modes_out)); 1916437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_mode_offsets_in)); 1917437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->eval_mode_offsets_out)); 1918437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_in)); 1919437c7c90SJeremy L Thompson CeedCall(CeedFree(&(*data)->assembled_bases_out)); 1920ed9e99e6SJeremy L Thompson 19212b730f8bSJeremy L Thompson CeedCall(CeedFree(data)); 1922ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1923ed9e99e6SJeremy L Thompson } 1924ed9e99e6SJeremy L Thompson 19254dd1a9d2SSebastian Grimberg /** 1926ca94c3ddSJeremy L Thompson @brief Retrieve fallback `CeedOperator` with a reference `Ceed` for advanced `CeedOperator` functionality 19274dd1a9d2SSebastian Grimberg 1928ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to retrieve fallback for 1929ca94c3ddSJeremy L Thompson @param[out] op_fallback Fallback `CeedOperator` 19304dd1a9d2SSebastian Grimberg 19314dd1a9d2SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 19324dd1a9d2SSebastian Grimberg 19334dd1a9d2SSebastian Grimberg @ref Backend 19344dd1a9d2SSebastian Grimberg **/ 19354dd1a9d2SSebastian Grimberg int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) { 19364dd1a9d2SSebastian Grimberg // Create if needed 19374dd1a9d2SSebastian Grimberg if (!op->op_fallback) CeedCall(CeedOperatorCreateFallback(op)); 19384dd1a9d2SSebastian Grimberg if (op->op_fallback) { 19394dd1a9d2SSebastian Grimberg bool is_debug; 19401203703bSJeremy L Thompson Ceed ceed; 19414dd1a9d2SSebastian Grimberg 19424dd1a9d2SSebastian Grimberg CeedCall(CeedOperatorGetCeed(op, &ceed)); 19431203703bSJeremy L Thompson CeedCall(CeedIsDebug(ceed, &is_debug)); 19441203703bSJeremy L Thompson if (is_debug) { 19451203703bSJeremy L Thompson Ceed ceed_fallback; 1946120566fcSJeremy L Thompson const char *resource, *resource_fallback, *op_name; 19471203703bSJeremy L Thompson 19484dd1a9d2SSebastian Grimberg CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback)); 19494dd1a9d2SSebastian Grimberg CeedCall(CeedGetResource(ceed, &resource)); 19504dd1a9d2SSebastian Grimberg CeedCall(CeedGetResource(ceed_fallback, &resource_fallback)); 1951120566fcSJeremy L Thompson CeedCall(CeedOperatorGetName(op, &op_name)); 19524dd1a9d2SSebastian Grimberg 19534dd1a9d2SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n"); 1954*ca38d01dSJeremy L Thompson CeedDebug(ceed, "Falling back from Operator with backend %s at address %p to Operator with backend %s at address %p for CeedOperator \"%s\"\n", 1955*ca38d01dSJeremy L Thompson resource, op, resource_fallback, op->op_fallback, op_name); 19569bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_fallback)); 19574dd1a9d2SSebastian Grimberg } 19589bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 19594dd1a9d2SSebastian Grimberg } 19604dd1a9d2SSebastian Grimberg *op_fallback = op->op_fallback; 19614dd1a9d2SSebastian Grimberg return CEED_ERROR_SUCCESS; 19624dd1a9d2SSebastian Grimberg } 19634dd1a9d2SSebastian Grimberg 19644dd1a9d2SSebastian Grimberg /** 1965ca94c3ddSJeremy L Thompson @brief Get the parent `CeedOperator` for a fallback `CeedOperator` 19664dd1a9d2SSebastian Grimberg 1967ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` context 1968ca94c3ddSJeremy L Thompson @param[out] parent Variable to store parent `CeedOperator` context 19694dd1a9d2SSebastian Grimberg 19704dd1a9d2SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 19714dd1a9d2SSebastian Grimberg 19724dd1a9d2SSebastian Grimberg @ref Backend 19734dd1a9d2SSebastian Grimberg **/ 19744dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParent(CeedOperator op, CeedOperator *parent) { 19754dd1a9d2SSebastian Grimberg *parent = op->op_fallback_parent ? op->op_fallback_parent : NULL; 19764dd1a9d2SSebastian Grimberg return CEED_ERROR_SUCCESS; 19774dd1a9d2SSebastian Grimberg } 19784dd1a9d2SSebastian Grimberg 19794dd1a9d2SSebastian Grimberg /** 1980ca94c3ddSJeremy L Thompson @brief Get the `Ceed` context of the parent `CeedOperator` for a fallback `CeedOperator` 19814dd1a9d2SSebastian Grimberg 1982ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` context 1983ca94c3ddSJeremy L Thompson @param[out] parent Variable to store parent `Ceed` context 19844dd1a9d2SSebastian Grimberg 19854dd1a9d2SSebastian Grimberg @return An error code: 0 - success, otherwise - failure 19864dd1a9d2SSebastian Grimberg 19874dd1a9d2SSebastian Grimberg @ref Backend 19884dd1a9d2SSebastian Grimberg **/ 19894dd1a9d2SSebastian Grimberg int CeedOperatorGetFallbackParentCeed(CeedOperator op, Ceed *parent) { 19909bc66399SJeremy L Thompson *parent = NULL; 19919bc66399SJeremy L Thompson if (op->op_fallback_parent) CeedCall(CeedReferenceCopy(op->op_fallback_parent->ceed, parent)); 19929bc66399SJeremy L Thompson else CeedCall(CeedReferenceCopy(CeedOperatorReturnCeed(op), parent)); 19934dd1a9d2SSebastian Grimberg return CEED_ERROR_SUCCESS; 19944dd1a9d2SSebastian Grimberg } 19954dd1a9d2SSebastian Grimberg 1996480fae85SJeremy L Thompson /// @} 1997480fae85SJeremy L Thompson 1998480fae85SJeremy L Thompson /// ---------------------------------------------------------------------------- 1999eaf62fffSJeremy L Thompson /// CeedOperator Public API 2000eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 2001eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser 2002eaf62fffSJeremy L Thompson /// @{ 2003eaf62fffSJeremy L Thompson 2004eaf62fffSJeremy L Thompson /** 2005ca94c3ddSJeremy L Thompson @brief Assemble a linear `CeedQFunction` associated with a `CeedOperator`. 2006eaf62fffSJeremy L Thompson 2007ca94c3ddSJeremy L Thompson This returns a `CeedVector` containing a matrix at each quadrature point providing the action of the `CeedQFunction` associated with the `CeedOperator`. 2008ca94c3ddSJeremy 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. 2009859c15bbSJames Wright 2010ca94c3ddSJeremy L Thompson Inputs and outputs are in the order provided by the user when adding `CeedOperator` fields. 2011ca94c3ddSJeremy 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]`. 2012eaf62fffSJeremy L Thompson 2013ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2014f04ea552SJeremy L Thompson 2015ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 2016ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points 2017ca94c3ddSJeremy L Thompson @param[out] rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction` 2018ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2019eaf62fffSJeremy L Thompson 2020eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2021eaf62fffSJeremy L Thompson 2022eaf62fffSJeremy L Thompson @ref User 2023eaf62fffSJeremy L Thompson **/ 20242b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 20252b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2026eaf62fffSJeremy L Thompson 2027eaf62fffSJeremy L Thompson if (op->LinearAssembleQFunction) { 2028d04bbc78SJeremy L Thompson // Backend version 20292b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request)); 2030eaf62fffSJeremy L Thompson } else { 2031d04bbc78SJeremy L Thompson // Operator fallback 2032d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2033d04bbc78SJeremy L Thompson 2034*ca38d01dSJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssembleQFunction\n"); 20352b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 20366574a04fSJeremy L Thompson if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request)); 20379bc66399SJeremy L Thompson else return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction"); 203870a7ffb3SJeremy L Thompson } 2039eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2040eaf62fffSJeremy L Thompson } 204170a7ffb3SJeremy L Thompson 204270a7ffb3SJeremy L Thompson /** 2043ca94c3ddSJeremy L Thompson @brief Assemble `CeedQFunction` and store result internally. 20444385fb7fSSebastian Grimberg 2045ea61e9acSJeremy L Thompson Return copied references of stored data to the caller. 2046ea61e9acSJeremy L Thompson Caller is responsible for ownership and destruction of the copied references. 2047ca94c3ddSJeremy L Thompson See also @ref CeedOperatorLinearAssembleQFunction(). 204870a7ffb3SJeremy L Thompson 2049ca94c3ddSJeremy 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. 2050c5f45aeaSJeremy L Thompson These objects will be destroyed if `*assembled` or `*rstr` is the only reference to the object. 2051c5f45aeaSJeremy L Thompson 2052ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 2053ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedQFunction` at quadrature points 2054ca94c3ddSJeremy L Thompson @param[out] rstr `CeedElemRestriction` for `CeedVector` containing assembled `CeedQFunction` 2055ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 205670a7ffb3SJeremy L Thompson 205770a7ffb3SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 205870a7ffb3SJeremy L Thompson 205970a7ffb3SJeremy L Thompson @ref User 206070a7ffb3SJeremy L Thompson **/ 20612b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 20620816752eSJeremy L Thompson return CeedOperatorLinearAssembleQFunctionBuildOrUpdate_Core(op, true, assembled, rstr, request); 2063eaf62fffSJeremy L Thompson } 2064eaf62fffSJeremy L Thompson 2065eaf62fffSJeremy L Thompson /** 2066ca94c3ddSJeremy L Thompson @brief Assemble the diagonal of a square linear `CeedOperator` 2067eaf62fffSJeremy L Thompson 2068ca94c3ddSJeremy L Thompson This overwrites a `CeedVector` with the diagonal of a linear `CeedOperator`. 2069eaf62fffSJeremy L Thompson 2070ca94c3ddSJeremy L Thompson Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported. 2071eaf62fffSJeremy L Thompson 2072ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2073f04ea552SJeremy L Thompson 2074ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 2075ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal 2076ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2077eaf62fffSJeremy L Thompson 2078eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2079eaf62fffSJeremy L Thompson 2080eaf62fffSJeremy L Thompson @ref User 2081eaf62fffSJeremy L Thompson **/ 20822b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 2083f3d47e36SJeremy L Thompson bool is_composite; 20841c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 20851c66c397SJeremy L Thompson 20862b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2087f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2088eaf62fffSJeremy L Thompson 20892b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 20909bc66399SJeremy L Thompson CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square"); 2091c9366a6bSJeremy L Thompson 2092f3d47e36SJeremy L Thompson // Early exit for empty operator 2093f3d47e36SJeremy L Thompson if (!is_composite) { 2094f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 2095f3d47e36SJeremy L Thompson 2096f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 2097f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 2098f3d47e36SJeremy L Thompson } 2099f3d47e36SJeremy L Thompson 2100eaf62fffSJeremy L Thompson if (op->LinearAssembleDiagonal) { 2101d04bbc78SJeremy L Thompson // Backend version 21022b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleDiagonal(op, assembled, request)); 2103eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2104eaf62fffSJeremy L Thompson } else if (op->LinearAssembleAddDiagonal) { 2105d04bbc78SJeremy L Thompson // Backend version with zeroing first 21062b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 21072b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request)); 2108eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 21090183ed61SJeremy L Thompson } else if (is_composite) { 21100183ed61SJeremy L Thompson // Default to summing contributions of suboperators 21110183ed61SJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 21120183ed61SJeremy L Thompson CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled)); 21130183ed61SJeremy L Thompson return CEED_ERROR_SUCCESS; 2114eaf62fffSJeremy L Thompson } else { 2115d04bbc78SJeremy L Thompson // Operator fallback 2116d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2117d04bbc78SJeremy L Thompson 2118*ca38d01dSJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssembleDiagonal\n"); 21192b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2120d04bbc78SJeremy L Thompson if (op_fallback) { 21212b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request)); 2122eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2123eaf62fffSJeremy L Thompson } 2124eaf62fffSJeremy L Thompson } 2125eaf62fffSJeremy L Thompson // Default interface implementation 21262b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 21272b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddDiagonal(op, assembled, request)); 2128eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2129eaf62fffSJeremy L Thompson } 2130eaf62fffSJeremy L Thompson 2131eaf62fffSJeremy L Thompson /** 2132ca94c3ddSJeremy L Thompson @brief Assemble the diagonal of a square linear `CeedOperator`. 2133eaf62fffSJeremy L Thompson 2134ca94c3ddSJeremy L Thompson This sums into a `CeedVector` the diagonal of a linear `CeedOperator`. 2135eaf62fffSJeremy L Thompson 2136ca94c3ddSJeremy L Thompson Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported. 2137eaf62fffSJeremy L Thompson 2138ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 2139f04ea552SJeremy L Thompson 2140ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 2141ca94c3ddSJeremy L Thompson @param[out] assembled `CeedVector` to store assembled `CeedOperator` diagonal 2142ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2143eaf62fffSJeremy L Thompson 2144eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2145eaf62fffSJeremy L Thompson 2146eaf62fffSJeremy L Thompson @ref User 2147eaf62fffSJeremy L Thompson **/ 21482b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 2149f3d47e36SJeremy L Thompson bool is_composite; 21501c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 21511c66c397SJeremy L Thompson 21522b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2153f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2154eaf62fffSJeremy L Thompson 21552b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 21569bc66399SJeremy L Thompson CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square"); 2157c9366a6bSJeremy L Thompson 2158f3d47e36SJeremy L Thompson // Early exit for empty operator 2159f3d47e36SJeremy L Thompson if (!is_composite) { 2160f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 2161f3d47e36SJeremy L Thompson 2162f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 2163f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 2164f3d47e36SJeremy L Thompson } 2165f3d47e36SJeremy L Thompson 2166eaf62fffSJeremy L Thompson if (op->LinearAssembleAddDiagonal) { 2167d04bbc78SJeremy L Thompson // Backend version 21682b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleAddDiagonal(op, assembled, request)); 2169eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 21700183ed61SJeremy L Thompson } else if (is_composite) { 21710183ed61SJeremy L Thompson // Default to summing contributions of suboperators 21720183ed61SJeremy L Thompson CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, false, assembled)); 217354d16554SHugh Carson return CEED_ERROR_SUCCESS; 2174eaf62fffSJeremy L Thompson } else { 2175d04bbc78SJeremy L Thompson // Operator fallback 2176d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2177d04bbc78SJeremy L Thompson 2178*ca38d01dSJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssembleAddDiagonal\n"); 21792b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2180d04bbc78SJeremy L Thompson if (op_fallback) { 21812b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request)); 2182eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2183eaf62fffSJeremy L Thompson } 2184eaf62fffSJeremy L Thompson } 2185eaf62fffSJeremy L Thompson // Default interface implementation 2186f3bd9308SJeremy L Thompson CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal(op, request, false, assembled)); 2187d04bbc78SJeremy L Thompson return CEED_ERROR_SUCCESS; 2188eaf62fffSJeremy L Thompson } 2189eaf62fffSJeremy L Thompson 2190eaf62fffSJeremy L Thompson /** 2191ca94c3ddSJeremy L Thompson @brief Fully assemble the point-block diagonal pattern of a linear `CeedOperator`. 219201f0e615SJames Wright 2193ca94c3ddSJeremy L Thompson Expected to be used in conjunction with @ref CeedOperatorLinearAssemblePointBlockDiagonal(). 219401f0e615SJames Wright 2195ca94c3ddSJeremy 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)`. 2196ca94c3ddSJeremy L Thompson Note that the `(i, j)` pairs are unique. 2197ca94c3ddSJeremy L Thompson This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemblePointBlockDiagonal() provides the values in the same ordering. 219801f0e615SJames Wright 219901f0e615SJames Wright This will generally be slow unless your operator is low-order. 220001f0e615SJames Wright 2201ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 220201f0e615SJames Wright 2203ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 220401f0e615SJames Wright @param[out] num_entries Number of entries in coordinate nonzero pattern 220501f0e615SJames Wright @param[out] rows Row number for each entry 220601f0e615SJames Wright @param[out] cols Column number for each entry 220701f0e615SJames Wright 220801f0e615SJames Wright @ref User 220901f0e615SJames Wright **/ 221001f0e615SJames Wright int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) { 221101f0e615SJames Wright bool is_composite; 221201f0e615SJames Wright CeedInt num_active_components, num_sub_operators; 221301f0e615SJames Wright CeedOperator *sub_operators; 221401f0e615SJames Wright 221501f0e615SJames Wright CeedCall(CeedOperatorIsComposite(op, &is_composite)); 221601f0e615SJames Wright 221701f0e615SJames Wright CeedSize input_size = 0, output_size = 0; 221801f0e615SJames Wright CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 22199bc66399SJeremy L Thompson CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square"); 222001f0e615SJames Wright 222101f0e615SJames Wright if (is_composite) { 222201f0e615SJames Wright CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_operators)); 222301f0e615SJames Wright CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 222401f0e615SJames Wright } else { 222501f0e615SJames Wright sub_operators = &op; 222601f0e615SJames Wright num_sub_operators = 1; 222701f0e615SJames Wright } 222801f0e615SJames Wright 2229506b1a0cSSebastian Grimberg // Verify operator can be assembled correctly 2230506b1a0cSSebastian Grimberg { 223101f0e615SJames Wright CeedOperatorAssemblyData data; 2232506b1a0cSSebastian Grimberg CeedInt num_active_elem_rstrs, comp_stride; 223301f0e615SJames Wright CeedElemRestriction *active_elem_rstrs; 223401f0e615SJames Wright 223501f0e615SJames Wright // Get initial values to check against 223601f0e615SJames Wright CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[0], &data)); 2237506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL)); 223801f0e615SJames Wright CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[0], &comp_stride)); 223901f0e615SJames Wright CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[0], &num_active_components)); 224001f0e615SJames Wright 2241506b1a0cSSebastian Grimberg // Verify that all active element restrictions have same component stride and number of components 224201f0e615SJames Wright for (CeedInt k = 0; k < num_sub_operators; k++) { 224301f0e615SJames Wright CeedCall(CeedOperatorGetOperatorAssemblyData(sub_operators[k], &data)); 2244506b1a0cSSebastian Grimberg CeedCall(CeedOperatorAssemblyDataGetElemRestrictions(data, &num_active_elem_rstrs, &active_elem_rstrs, NULL, NULL)); 224501f0e615SJames Wright for (CeedInt i = 0; i < num_active_elem_rstrs; i++) { 2246506b1a0cSSebastian Grimberg CeedInt comp_stride_sub, num_active_components_sub; 2247506b1a0cSSebastian Grimberg 224801f0e615SJames Wright CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[i], &comp_stride_sub)); 22499bc66399SJeremy L Thompson CeedCheck(comp_stride == comp_stride_sub, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, 225001f0e615SJames Wright "Active element restrictions must have the same component stride: %d vs %d", comp_stride, comp_stride_sub); 225101f0e615SJames Wright CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[i], &num_active_components_sub)); 22529bc66399SJeremy L Thompson CeedCheck(num_active_components == num_active_components_sub, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, 22533f08121cSJeremy L Thompson "All suboperators must have the same number of output components." 22543f08121cSJeremy L Thompson " Previous: %" CeedInt_FMT " Current: %" CeedInt_FMT, 22553f08121cSJeremy L Thompson num_active_components, num_active_components_sub); 225601f0e615SJames Wright } 225701f0e615SJames Wright } 225801f0e615SJames Wright } 225901f0e615SJames Wright *num_entries = input_size * num_active_components; 226001f0e615SJames Wright CeedCall(CeedCalloc(*num_entries, rows)); 226101f0e615SJames Wright CeedCall(CeedCalloc(*num_entries, cols)); 226201f0e615SJames Wright 226301f0e615SJames Wright for (CeedInt o = 0; o < num_sub_operators; o++) { 2264506b1a0cSSebastian Grimberg CeedElemRestriction active_elem_rstr, point_block_active_elem_rstr; 226501f0e615SJames Wright CeedInt comp_stride, num_elem, elem_size; 2266506b1a0cSSebastian Grimberg const CeedInt *offsets, *point_block_offsets; 226701f0e615SJames Wright 226801f0e615SJames Wright CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[o], &active_elem_rstr)); 226901f0e615SJames Wright CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstr, &comp_stride)); 227001f0e615SJames Wright CeedCall(CeedElemRestrictionGetNumElements(active_elem_rstr, &num_elem)); 227101f0e615SJames Wright CeedCall(CeedElemRestrictionGetElementSize(active_elem_rstr, &elem_size)); 227201f0e615SJames Wright CeedCall(CeedElemRestrictionGetOffsets(active_elem_rstr, CEED_MEM_HOST, &offsets)); 227301f0e615SJames Wright 2274506b1a0cSSebastian Grimberg CeedCall(CeedOperatorCreateActivePointBlockRestriction(active_elem_rstr, &point_block_active_elem_rstr)); 2275506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionGetOffsets(point_block_active_elem_rstr, CEED_MEM_HOST, &point_block_offsets)); 227601f0e615SJames Wright 227701f0e615SJames Wright for (CeedSize i = 0; i < num_elem * elem_size; i++) { 227801f0e615SJames Wright for (CeedInt c_out = 0; c_out < num_active_components; c_out++) { 227901f0e615SJames Wright for (CeedInt c_in = 0; c_in < num_active_components; c_in++) { 2280506b1a0cSSebastian Grimberg (*rows)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_out * comp_stride; 2281506b1a0cSSebastian Grimberg (*cols)[point_block_offsets[i] + c_out * num_active_components + c_in] = offsets[i] + c_in * comp_stride; 228201f0e615SJames Wright } 228301f0e615SJames Wright } 228401f0e615SJames Wright } 228501f0e615SJames Wright 228601f0e615SJames Wright CeedCall(CeedElemRestrictionRestoreOffsets(active_elem_rstr, &offsets)); 2287506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionRestoreOffsets(point_block_active_elem_rstr, &point_block_offsets)); 2288681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&active_elem_rstr)); 2289506b1a0cSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&point_block_active_elem_rstr)); 229001f0e615SJames Wright } 229101f0e615SJames Wright return CEED_ERROR_SUCCESS; 229201f0e615SJames Wright } 229301f0e615SJames Wright 229401f0e615SJames Wright /** 2295ca94c3ddSJeremy L Thompson @brief Assemble the point block diagonal of a square linear `CeedOperator`. 2296eaf62fffSJeremy L Thompson 2297ca94c3ddSJeremy L Thompson This overwrites a `CeedVector` with the point block diagonal of a linear `CeedOperator`. 2298eaf62fffSJeremy L Thompson 2299ca94c3ddSJeremy L Thompson Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported. 2300eaf62fffSJeremy L Thompson 2301ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2302f04ea552SJeremy L Thompson 2303ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 2304ca94c3ddSJeremy 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. 2305ca94c3ddSJeremy L Thompson The dimensions of this vector are derived from the active vector for the `CeedOperator`. 2306ca94c3ddSJeremy L Thompson The array has shape `[nodes, component out, component in]`. 2307ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2308eaf62fffSJeremy L Thompson 2309eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2310eaf62fffSJeremy L Thompson 2311eaf62fffSJeremy L Thompson @ref User 2312eaf62fffSJeremy L Thompson **/ 23132b730f8bSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 2314f3d47e36SJeremy L Thompson bool is_composite; 23151c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 23161c66c397SJeremy L Thompson 23172b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2318f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2319eaf62fffSJeremy L Thompson 23202b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 23219bc66399SJeremy L Thompson CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square"); 2322c9366a6bSJeremy L Thompson 2323f3d47e36SJeremy L Thompson // Early exit for empty operator 2324f3d47e36SJeremy L Thompson if (!is_composite) { 2325f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 2326f3d47e36SJeremy L Thompson 2327f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 2328f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 2329f3d47e36SJeremy L Thompson } 2330f3d47e36SJeremy L Thompson 2331eaf62fffSJeremy L Thompson if (op->LinearAssemblePointBlockDiagonal) { 2332d04bbc78SJeremy L Thompson // Backend version 23332b730f8bSJeremy L Thompson CeedCall(op->LinearAssemblePointBlockDiagonal(op, assembled, request)); 2334eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2335eaf62fffSJeremy L Thompson } else if (op->LinearAssembleAddPointBlockDiagonal) { 2336d04bbc78SJeremy L Thompson // Backend version with zeroing first 23372b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 23382b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request)); 2339eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2340eaf62fffSJeremy L Thompson } else { 2341d04bbc78SJeremy L Thompson // Operator fallback 2342d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2343d04bbc78SJeremy L Thompson 2344*ca38d01dSJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssemblePointBlockDiagonal\n"); 23452b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2346d04bbc78SJeremy L Thompson if (op_fallback) { 23472b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, request)); 2348eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2349eaf62fffSJeremy L Thompson } 2350eaf62fffSJeremy L Thompson } 2351eaf62fffSJeremy L Thompson // Default interface implementation 23522b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(assembled, 0.0)); 23532b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request)); 2354eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2355eaf62fffSJeremy L Thompson } 2356eaf62fffSJeremy L Thompson 2357eaf62fffSJeremy L Thompson /** 2358ca94c3ddSJeremy L Thompson @brief Assemble the point block diagonal of a square linear `CeedOperator`. 2359eaf62fffSJeremy L Thompson 2360ca94c3ddSJeremy L Thompson This sums into a `CeedVector` with the point block diagonal of a linear `CeedOperator`. 2361eaf62fffSJeremy L Thompson 2362ca94c3ddSJeremy L Thompson Note: Currently only non-composite `CeedOperator` with a single field and composite `CeedOperator` with single field sub-operators are supported. 2363eaf62fffSJeremy L Thompson 2364ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2365f04ea552SJeremy L Thompson 2366ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble `CeedQFunction` 2367ca94c3ddSJeremy 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. 2368ca94c3ddSJeremy L Thompson The dimensions of this vector are derived from the active vector for the `CeedOperator`. 2369ca94c3ddSJeremy L Thompson The array has shape `[nodes, component out, component in]`. 2370ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2371eaf62fffSJeremy L Thompson 2372eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2373eaf62fffSJeremy L Thompson 2374eaf62fffSJeremy L Thompson @ref User 2375eaf62fffSJeremy L Thompson **/ 23762b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { 2377f3d47e36SJeremy L Thompson bool is_composite; 23781c66c397SJeremy L Thompson CeedSize input_size = 0, output_size = 0; 23791c66c397SJeremy L Thompson 23802b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2381f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2382eaf62fffSJeremy L Thompson 23832b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 23849bc66399SJeremy L Thompson CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square"); 2385c9366a6bSJeremy L Thompson 2386f3d47e36SJeremy L Thompson // Early exit for empty operator 2387f3d47e36SJeremy L Thompson if (!is_composite) { 2388f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 2389f3d47e36SJeremy L Thompson 2390f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 2391f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 2392f3d47e36SJeremy L Thompson } 2393f3d47e36SJeremy L Thompson 2394eaf62fffSJeremy L Thompson if (op->LinearAssembleAddPointBlockDiagonal) { 2395d04bbc78SJeremy L Thompson // Backend version 23962b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleAddPointBlockDiagonal(op, assembled, request)); 2397eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2398eaf62fffSJeremy L Thompson } else { 2399d04bbc78SJeremy L Thompson // Operator fallback 2400d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2401d04bbc78SJeremy L Thompson 2402*ca38d01dSJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssembleAddPointBlockDiagonal\n"); 24032b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2404d04bbc78SJeremy L Thompson if (op_fallback) { 24052b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, request)); 2406eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2407eaf62fffSJeremy L Thompson } 2408eaf62fffSJeremy L Thompson } 2409ea61e9acSJeremy L Thompson // Default interface implementation 2410eaf62fffSJeremy L Thompson if (is_composite) { 24112b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, true, assembled)); 2412eaf62fffSJeremy L Thompson } else { 2413f3bd9308SJeremy L Thompson CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal(op, request, true, assembled)); 2414eaf62fffSJeremy L Thompson } 2415d04bbc78SJeremy L Thompson return CEED_ERROR_SUCCESS; 2416eaf62fffSJeremy L Thompson } 2417eaf62fffSJeremy L Thompson 2418eaf62fffSJeremy L Thompson /** 2419ca94c3ddSJeremy L Thompson @brief Fully assemble the nonzero pattern of a linear `CeedOperator`. 2420eaf62fffSJeremy L Thompson 2421ca94c3ddSJeremy L Thompson Expected to be used in conjunction with @ref CeedOperatorLinearAssemble(). 2422eaf62fffSJeremy L Thompson 2423ca94c3ddSJeremy 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)`. 2424ca94c3ddSJeremy L Thompson Note that the `(i, j)` pairs are not unique and may repeat. 2425ca94c3ddSJeremy L Thompson This function returns the number of entries and their `(i, j)` locations, while @ref CeedOperatorLinearAssemble() provides the values in the same ordering. 2426eaf62fffSJeremy L Thompson 2427eaf62fffSJeremy L Thompson This will generally be slow unless your operator is low-order. 2428eaf62fffSJeremy L Thompson 2429ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2430f04ea552SJeremy L Thompson 2431ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 2432eaf62fffSJeremy L Thompson @param[out] num_entries Number of entries in coordinate nonzero pattern 2433eaf62fffSJeremy L Thompson @param[out] rows Row number for each entry 2434eaf62fffSJeremy L Thompson @param[out] cols Column number for each entry 2435eaf62fffSJeremy L Thompson 2436eaf62fffSJeremy L Thompson @ref User 2437eaf62fffSJeremy L Thompson **/ 24382b730f8bSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) { 24391c66c397SJeremy L Thompson bool is_composite; 24401c66c397SJeremy L Thompson CeedInt num_suboperators, offset = 0; 2441b94338b9SJed Brown CeedSize single_entries; 2442eaf62fffSJeremy L Thompson CeedOperator *sub_operators; 24431c66c397SJeremy L Thompson 24442b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2445f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2446eaf62fffSJeremy L Thompson 2447eaf62fffSJeremy L Thompson if (op->LinearAssembleSymbolic) { 2448d04bbc78SJeremy L Thompson // Backend version 24492b730f8bSJeremy L Thompson CeedCall(op->LinearAssembleSymbolic(op, num_entries, rows, cols)); 2450eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2451eaf62fffSJeremy L Thompson } else { 2452d04bbc78SJeremy L Thompson // Operator fallback 2453d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2454d04bbc78SJeremy L Thompson 2455*ca38d01dSJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssembleSymbolic\n"); 24562b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2457d04bbc78SJeremy L Thompson if (op_fallback) { 24582b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols)); 2459eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2460eaf62fffSJeremy L Thompson } 2461eaf62fffSJeremy L Thompson } 2462eaf62fffSJeremy L Thompson 2463eaf62fffSJeremy L Thompson // Default interface implementation 2464eaf62fffSJeremy L Thompson 2465506b1a0cSSebastian Grimberg // Count entries and allocate rows, cols arrays 2466eaf62fffSJeremy L Thompson *num_entries = 0; 2467eaf62fffSJeremy L Thompson if (is_composite) { 2468c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2469c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 247092ae7e47SJeremy L Thompson for (CeedInt k = 0; k < num_suboperators; ++k) { 24712b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries)); 2472eaf62fffSJeremy L Thompson *num_entries += single_entries; 2473eaf62fffSJeremy L Thompson } 2474eaf62fffSJeremy L Thompson } else { 24752b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(op, &single_entries)); 2476eaf62fffSJeremy L Thompson *num_entries += single_entries; 2477eaf62fffSJeremy L Thompson } 24782b730f8bSJeremy L Thompson CeedCall(CeedCalloc(*num_entries, rows)); 24792b730f8bSJeremy L Thompson CeedCall(CeedCalloc(*num_entries, cols)); 2480eaf62fffSJeremy L Thompson 2481506b1a0cSSebastian Grimberg // Assemble nonzero locations 2482eaf62fffSJeremy L Thompson if (is_composite) { 2483c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2484c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 248592ae7e47SJeremy L Thompson for (CeedInt k = 0; k < num_suboperators; ++k) { 24862b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, *cols)); 24872b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries)); 2488eaf62fffSJeremy L Thompson offset += single_entries; 2489eaf62fffSJeremy L Thompson } 2490eaf62fffSJeremy L Thompson } else { 24912b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols)); 2492eaf62fffSJeremy L Thompson } 2493eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2494eaf62fffSJeremy L Thompson } 2495eaf62fffSJeremy L Thompson 2496eaf62fffSJeremy L Thompson /** 2497eaf62fffSJeremy L Thompson @brief Fully assemble the nonzero entries of a linear operator. 2498eaf62fffSJeremy L Thompson 2499ca94c3ddSJeremy L Thompson Expected to be used in conjunction with @ref CeedOperatorLinearAssembleSymbolic(). 2500eaf62fffSJeremy L Thompson 2501ca94c3ddSJeremy 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)`. 2502ca94c3ddSJeremy L Thompson Note that the `(i, j)` pairs are not unique and may repeat. 2503ca94c3ddSJeremy L Thompson This function returns the values of the nonzero entries to be added, their `(i, j)` locations are provided by @ref CeedOperatorLinearAssembleSymbolic(). 2504eaf62fffSJeremy L Thompson 2505eaf62fffSJeremy L Thompson This will generally be slow unless your operator is low-order. 2506eaf62fffSJeremy L Thompson 2507ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2508f04ea552SJeremy L Thompson 2509ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to assemble 2510eaf62fffSJeremy L Thompson @param[out] values Values to assemble into matrix 2511eaf62fffSJeremy L Thompson 2512eaf62fffSJeremy L Thompson @ref User 2513eaf62fffSJeremy L Thompson **/ 2514eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) { 2515a34b87f3SZach Atkins bool is_composite; 25161c66c397SJeremy L Thompson CeedInt num_suboperators, offset = 0; 2517b94338b9SJed Brown CeedSize single_entries = 0; 2518eaf62fffSJeremy L Thompson CeedOperator *sub_operators; 25191c66c397SJeremy L Thompson 25202b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2521f3d47e36SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2522f3d47e36SJeremy L Thompson 2523f3d47e36SJeremy L Thompson // Early exit for empty operator 2524f3d47e36SJeremy L Thompson if (!is_composite) { 2525f3d47e36SJeremy L Thompson CeedInt num_elem = 0; 2526f3d47e36SJeremy L Thompson 2527f3d47e36SJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 2528f3d47e36SJeremy L Thompson if (num_elem == 0) return CEED_ERROR_SUCCESS; 2529f3d47e36SJeremy L Thompson } 2530eaf62fffSJeremy L Thompson 2531eaf62fffSJeremy L Thompson if (op->LinearAssemble) { 2532d04bbc78SJeremy L Thompson // Backend version 25332b730f8bSJeremy L Thompson CeedCall(op->LinearAssemble(op, values)); 2534eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2535a34b87f3SZach Atkins } else if (is_composite) { 2536915834c9SZach Atkins // Default to summing contributions of suboperators 2537915834c9SZach Atkins CeedCall(CeedVectorSetValue(values, 0.0)); 2538915834c9SZach Atkins CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2539915834c9SZach Atkins CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 2540915834c9SZach Atkins for (CeedInt k = 0; k < num_suboperators; k++) { 2541915834c9SZach Atkins CeedCall(CeedSingleOperatorAssemble(sub_operators[k], offset, values)); 2542915834c9SZach Atkins CeedCall(CeedSingleOperatorAssemblyCountEntries(sub_operators[k], &single_entries)); 2543915834c9SZach Atkins offset += single_entries; 2544915834c9SZach Atkins } 2545a34b87f3SZach Atkins return CEED_ERROR_SUCCESS; 2546a34b87f3SZach Atkins } else if (op->LinearAssembleSingle) { 2547a34b87f3SZach Atkins CeedCall(CeedVectorSetValue(values, 0.0)); 2548915834c9SZach Atkins CeedCall(CeedSingleOperatorAssemble(op, offset, values)); 2549915834c9SZach Atkins return CEED_ERROR_SUCCESS; 2550eaf62fffSJeremy L Thompson } else { 2551d04bbc78SJeremy L Thompson // Operator fallback 2552d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2553d04bbc78SJeremy L Thompson 2554*ca38d01dSJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorLinearAssemble\n"); 25552b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2556d04bbc78SJeremy L Thompson if (op_fallback) { 25572b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssemble(op_fallback, values)); 2558eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2559eaf62fffSJeremy L Thompson } 2560eaf62fffSJeremy L Thompson } 2561eaf62fffSJeremy L Thompson 2562a34b87f3SZach Atkins // Default to interface version if non-composite and no fallback 256328ec399dSJeremy L Thompson CeedCall(CeedVectorSetValue(values, 0.0)); 25642b730f8bSJeremy L Thompson CeedCall(CeedSingleOperatorAssemble(op, offset, values)); 2565eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2566eaf62fffSJeremy L Thompson } 2567eaf62fffSJeremy L Thompson 2568eaf62fffSJeremy L Thompson /** 2569ca94c3ddSJeremy L Thompson @brief Get the multiplicity of nodes across sub-operators in a composite `CeedOperator`. 257075f0d5a4SJeremy L Thompson 2571ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 257275f0d5a4SJeremy L Thompson 2573ca94c3ddSJeremy L Thompson @param[in] op Composite `CeedOperator` 2574ca94c3ddSJeremy L Thompson @param[in] num_skip_indices Number of sub-operators to skip 2575ca94c3ddSJeremy L Thompson @param[in] skip_indices Array of indices of sub-operators to skip 2576ca94c3ddSJeremy L Thompson @param[out] mult Vector to store multiplicity (of size `l_size` ) 257775f0d5a4SJeremy L Thompson 257875f0d5a4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 257975f0d5a4SJeremy L Thompson 258075f0d5a4SJeremy L Thompson @ref User 258175f0d5a4SJeremy L Thompson **/ 258275f0d5a4SJeremy L Thompson int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indices, CeedInt *skip_indices, CeedVector mult) { 258375f0d5a4SJeremy L Thompson Ceed ceed; 2584b275c451SJeremy L Thompson CeedInt num_suboperators; 258575f0d5a4SJeremy L Thompson CeedSize l_vec_len; 258675f0d5a4SJeremy L Thompson CeedScalar *mult_array; 258775f0d5a4SJeremy L Thompson CeedVector ones_l_vec; 25887c1dbaffSSebastian Grimberg CeedElemRestriction elem_rstr, mult_elem_rstr; 2589b275c451SJeremy L Thompson CeedOperator *sub_operators; 259075f0d5a4SJeremy L Thompson 25911c66c397SJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 25921c66c397SJeremy L Thompson 259375f0d5a4SJeremy L Thompson // Zero mult vector 259475f0d5a4SJeremy L Thompson CeedCall(CeedVectorSetValue(mult, 0.0)); 259575f0d5a4SJeremy L Thompson 259675f0d5a4SJeremy L Thompson // Get suboperators 2597b275c451SJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 2598b275c451SJeremy L Thompson if (num_suboperators == 0) return CEED_ERROR_SUCCESS; 25999bc66399SJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 260075f0d5a4SJeremy L Thompson 260175f0d5a4SJeremy L Thompson // Work vector 260275f0d5a4SJeremy L Thompson CeedCall(CeedVectorGetLength(mult, &l_vec_len)); 26039bc66399SJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 260475f0d5a4SJeremy L Thompson CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec)); 26059bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 260675f0d5a4SJeremy L Thompson CeedCall(CeedVectorSetValue(ones_l_vec, 1.0)); 260775f0d5a4SJeremy L Thompson CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array)); 260875f0d5a4SJeremy L Thompson 260975f0d5a4SJeremy L Thompson // Compute multiplicity across suboperators 2610b275c451SJeremy L Thompson for (CeedInt i = 0; i < num_suboperators; i++) { 261175f0d5a4SJeremy L Thompson const CeedScalar *sub_mult_array; 261275f0d5a4SJeremy L Thompson CeedVector sub_mult_l_vec, ones_e_vec; 261375f0d5a4SJeremy L Thompson 261475f0d5a4SJeremy L Thompson // -- Check for suboperator to skip 261575f0d5a4SJeremy L Thompson for (CeedInt j = 0; j < num_skip_indices; j++) { 261675f0d5a4SJeremy L Thompson if (skip_indices[j] == i) continue; 261775f0d5a4SJeremy L Thompson } 261875f0d5a4SJeremy L Thompson 261975f0d5a4SJeremy L Thompson // -- Sub operator multiplicity 2620437c7c90SJeremy L Thompson CeedCall(CeedOperatorGetActiveElemRestriction(sub_operators[i], &elem_rstr)); 26217c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionCreateUnorientedCopy(elem_rstr, &mult_elem_rstr)); 2622681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&elem_rstr)); 26237c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionCreateVector(mult_elem_rstr, &sub_mult_l_vec, &ones_e_vec)); 262475f0d5a4SJeremy L Thompson CeedCall(CeedVectorSetValue(sub_mult_l_vec, 0.0)); 26257c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_NOTRANSPOSE, ones_l_vec, ones_e_vec, CEED_REQUEST_IMMEDIATE)); 26267c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionApply(mult_elem_rstr, CEED_TRANSPOSE, ones_e_vec, sub_mult_l_vec, CEED_REQUEST_IMMEDIATE)); 262775f0d5a4SJeremy L Thompson CeedCall(CeedVectorGetArrayRead(sub_mult_l_vec, CEED_MEM_HOST, &sub_mult_array)); 262875f0d5a4SJeremy L Thompson // ---- Flag every node present in the current suboperator 2629c81f2b9dSJames Wright for (CeedSize j = 0; j < l_vec_len; j++) { 263075f0d5a4SJeremy L Thompson if (sub_mult_array[j] > 0.0) mult_array[j] += 1.0; 263175f0d5a4SJeremy L Thompson } 263275f0d5a4SJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(sub_mult_l_vec, &sub_mult_array)); 263375f0d5a4SJeremy L Thompson CeedCall(CeedVectorDestroy(&sub_mult_l_vec)); 263475f0d5a4SJeremy L Thompson CeedCall(CeedVectorDestroy(&ones_e_vec)); 26357c1dbaffSSebastian Grimberg CeedCall(CeedElemRestrictionDestroy(&mult_elem_rstr)); 263675f0d5a4SJeremy L Thompson } 263775f0d5a4SJeremy L Thompson CeedCall(CeedVectorRestoreArray(mult, &mult_array)); 2638811d0ccfSJeremy L Thompson CeedCall(CeedVectorDestroy(&ones_l_vec)); 263975f0d5a4SJeremy L Thompson return CEED_ERROR_SUCCESS; 264075f0d5a4SJeremy L Thompson } 264175f0d5a4SJeremy L Thompson 264275f0d5a4SJeremy L Thompson /** 2643ca94c3ddSJeremy 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. 2644eaf62fffSJeremy L Thompson 2645ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable. 2646f04ea552SJeremy L Thompson 2647ca94c3ddSJeremy L Thompson @param[in] op_fine Fine grid `CeedOperator` 2648ca94c3ddSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator` 2649ca94c3ddSJeremy L Thompson @param[in] rstr_coarse Coarse grid `CeedElemRestriction` 2650ca94c3ddSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector `CeedBasis` 2651ca94c3ddSJeremy L Thompson @param[out] op_coarse Coarse grid `CeedOperator` 2652ca94c3ddSJeremy L Thompson @param[out] op_prolong Coarse to fine `CeedOperator`, or `NULL` 2653ca94c3ddSJeremy L Thompson @param[out] op_restrict Fine to coarse `CeedOperator`, or `NULL` 2654eaf62fffSJeremy L Thompson 2655eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2656eaf62fffSJeremy L Thompson 2657eaf62fffSJeremy L Thompson @ref User 2658eaf62fffSJeremy L Thompson **/ 26592b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 26607758292fSSebastian Grimberg CeedOperator *op_coarse, CeedOperator *op_prolong, CeedOperator *op_restrict) { 26611c66c397SJeremy L Thompson CeedBasis basis_c_to_f = NULL; 26621c66c397SJeremy L Thompson 26632b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fine)); 2664eaf62fffSJeremy L Thompson 266583d6adf3SZach Atkins // Build prolongation matrix, if required 26667758292fSSebastian Grimberg if (op_prolong || op_restrict) { 266783d6adf3SZach Atkins CeedBasis basis_fine; 26681c66c397SJeremy L Thompson 26692b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine)); 26702b730f8bSJeremy L Thompson CeedCall(CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f)); 2671681d0ea7SJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_fine)); 267283d6adf3SZach Atkins } 2673eaf62fffSJeremy L Thompson 2674f113e5dcSJeremy L Thompson // Core code 26757758292fSSebastian Grimberg CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict)); 2676eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2677eaf62fffSJeremy L Thompson } 2678eaf62fffSJeremy L Thompson 2679eaf62fffSJeremy L Thompson /** 2680ca94c3ddSJeremy L Thompson @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a tensor basis for the active basis. 2681eaf62fffSJeremy L Thompson 2682ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable. 2683f04ea552SJeremy L Thompson 2684ca94c3ddSJeremy L Thompson @param[in] op_fine Fine grid `CeedOperator` 2685ca94c3ddSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator` 2686ca94c3ddSJeremy L Thompson @param[in] rstr_coarse Coarse grid `CeedElemRestriction` 2687ca94c3ddSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector `CeedBasis` 2688ca94c3ddSJeremy L Thompson @param[in] interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator` 2689ca94c3ddSJeremy L Thompson @param[out] op_coarse Coarse grid `CeedOperator` 2690ca94c3ddSJeremy L Thompson @param[out] op_prolong Coarse to fine `CeedOperator`, or `NULL` 2691ca94c3ddSJeremy L Thompson @param[out] op_restrict Fine to coarse `CeedOperator`, or `NULL` 2692eaf62fffSJeremy L Thompson 2693eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2694eaf62fffSJeremy L Thompson 2695eaf62fffSJeremy L Thompson @ref User 2696eaf62fffSJeremy L Thompson **/ 26972b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 26982b730f8bSJeremy L Thompson const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, 26997758292fSSebastian Grimberg CeedOperator *op_restrict) { 2700eaf62fffSJeremy L Thompson Ceed ceed; 27011c66c397SJeremy L Thompson CeedInt Q_f, Q_c; 27021c66c397SJeremy L Thompson CeedBasis basis_fine, basis_c_to_f = NULL; 27031c66c397SJeremy L Thompson 27041c66c397SJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fine)); 27052b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op_fine, &ceed)); 2706eaf62fffSJeremy L Thompson 2707eaf62fffSJeremy L Thompson // Check for compatible quadrature spaces 27082b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine)); 27092b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f)); 27102b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c)); 27113f08121cSJeremy L Thompson CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, 27123f08121cSJeremy L Thompson "Bases must have compatible quadrature spaces." 27133f08121cSJeremy L Thompson " Fine grid: %" CeedInt_FMT " points, Coarse grid: %" CeedInt_FMT " points", 27143f08121cSJeremy L Thompson Q_f, Q_c); 2715eaf62fffSJeremy L Thompson 271683d6adf3SZach Atkins // Create coarse to fine basis, if required 27177758292fSSebastian Grimberg if (op_prolong || op_restrict) { 27181c66c397SJeremy L Thompson CeedInt dim, num_comp, num_nodes_c, P_1d_f, P_1d_c; 27191c66c397SJeremy L Thompson CeedScalar *q_ref, *q_weight, *grad; 27201c66c397SJeremy L Thompson 272183d6adf3SZach Atkins // Check if interpolation matrix is provided 27226574a04fSJeremy L Thompson CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE, 27236574a04fSJeremy L Thompson "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix"); 27242b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_fine, &dim)); 27252b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp)); 27262b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis_fine, &P_1d_f)); 2727681d0ea7SJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_fine)); 27282b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c)); 27292b730f8bSJeremy L Thompson P_1d_c = dim == 1 ? num_nodes_c : dim == 2 ? sqrt(num_nodes_c) : cbrt(num_nodes_c); 27302b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_f, &q_ref)); 27312b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_f, &q_weight)); 27322b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d_f * P_1d_c * dim, &grad)); 27332b730f8bSJeremy 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)); 27342b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref)); 27352b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight)); 27362b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad)); 273783d6adf3SZach Atkins } 2738eaf62fffSJeremy L Thompson 2739eaf62fffSJeremy L Thompson // Core code 27407758292fSSebastian Grimberg CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict)); 27419bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 2742eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2743eaf62fffSJeremy L Thompson } 2744eaf62fffSJeremy L Thompson 2745eaf62fffSJeremy L Thompson /** 2746ca94c3ddSJeremy L Thompson @brief Create a multigrid coarse `CeedOperator` and level transfer `CeedOperator` for a `CeedOperator` with a non-tensor basis for the active vector 2747eaf62fffSJeremy L Thompson 2748ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets all four `CeedOperator` as immutable. 2749f04ea552SJeremy L Thompson 2750ca94c3ddSJeremy L Thompson @param[in] op_fine Fine grid `CeedOperator` 2751ca94c3ddSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter, or `NULL` if not creating prolongation/restriction `CeedOperator` 2752ca94c3ddSJeremy L Thompson @param[in] rstr_coarse Coarse grid `CeedElemRestriction` 2753ca94c3ddSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector `CeedBasis` 2754ca94c3ddSJeremy L Thompson @param[in] interp_c_to_f Matrix for coarse to fine interpolation, or `NULL` if not creating prolongation/restriction `CeedOperator` 2755ca94c3ddSJeremy L Thompson @param[out] op_coarse Coarse grid `CeedOperator` 2756ca94c3ddSJeremy L Thompson @param[out] op_prolong Coarse to fine `CeedOperator`, or `NULL` 2757ca94c3ddSJeremy L Thompson @param[out] op_restrict Fine to coarse `CeedOperator`, or `NULL` 2758eaf62fffSJeremy L Thompson 2759eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2760eaf62fffSJeremy L Thompson 2761eaf62fffSJeremy L Thompson @ref User 2762eaf62fffSJeremy L Thompson **/ 27632b730f8bSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 27647758292fSSebastian Grimberg const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, 27657758292fSSebastian Grimberg CeedOperator *op_restrict) { 2766eaf62fffSJeremy L Thompson Ceed ceed; 27671c66c397SJeremy L Thompson CeedInt Q_f, Q_c; 27681c66c397SJeremy L Thompson CeedBasis basis_fine, basis_c_to_f = NULL; 27691c66c397SJeremy L Thompson 27701c66c397SJeremy L Thompson CeedCall(CeedOperatorCheckReady(op_fine)); 27712b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op_fine, &ceed)); 2772eaf62fffSJeremy L Thompson 2773eaf62fffSJeremy L Thompson // Check for compatible quadrature spaces 27742b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveBasis(op_fine, &basis_fine)); 27752b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f)); 27762b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c)); 27776574a04fSJeremy L Thompson CeedCheck(Q_f == Q_c, ceed, CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces"); 2778eaf62fffSJeremy L Thompson 2779eaf62fffSJeremy L Thompson // Coarse to fine basis 27807758292fSSebastian Grimberg if (op_prolong || op_restrict) { 27811c66c397SJeremy L Thompson CeedInt dim, num_comp, num_nodes_c, num_nodes_f; 27821c66c397SJeremy L Thompson CeedScalar *q_ref, *q_weight, *grad; 27831c66c397SJeremy L Thompson CeedElemTopology topo; 27841c66c397SJeremy L Thompson 278583d6adf3SZach Atkins // Check if interpolation matrix is provided 27866574a04fSJeremy L Thompson CeedCheck(interp_c_to_f, ceed, CEED_ERROR_INCOMPATIBLE, 27876574a04fSJeremy L Thompson "Prolongation or restriction operator creation requires coarse-to-fine interpolation matrix"); 27882b730f8bSJeremy L Thompson CeedCall(CeedBasisGetTopology(basis_fine, &topo)); 27892b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis_fine, &dim)); 27902b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis_fine, &num_comp)); 27912b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes(basis_fine, &num_nodes_f)); 2792681d0ea7SJeremy L Thompson CeedCall(CeedBasisDestroy(&basis_fine)); 27932b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c)); 27942b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_f * dim, &q_ref)); 27952b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_f, &q_weight)); 27962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_nodes_f * num_nodes_c * dim, &grad)); 27972b730f8bSJeremy 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)); 27982b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref)); 27992b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight)); 28002b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad)); 280183d6adf3SZach Atkins } 2802eaf62fffSJeremy L Thompson 2803eaf62fffSJeremy L Thompson // Core code 28047758292fSSebastian Grimberg CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict)); 28059bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 2806eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2807eaf62fffSJeremy L Thompson } 2808eaf62fffSJeremy L Thompson 2809eaf62fffSJeremy L Thompson /** 2810ca94c3ddSJeremy L Thompson @brief Build a FDM based approximate inverse for each element for a `CeedOperator`. 2811eaf62fffSJeremy L Thompson 2812ca94c3ddSJeremy L Thompson This returns a `CeedOperator` and `CeedVector` to apply a Fast Diagonalization Method based approximate inverse. 2813859c15bbSJames 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$. 2814ca94c3ddSJeremy 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$. 2815ca94c3ddSJeremy L Thompson The `CeedOperator` must be linear and non-composite. 2816ca94c3ddSJeremy L Thompson The associated `CeedQFunction` must therefore also be linear. 2817eaf62fffSJeremy L Thompson 2818ca94c3ddSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 2819f04ea552SJeremy L Thompson 2820ca94c3ddSJeremy L Thompson @param[in] op `CeedOperator` to create element inverses 2821ca94c3ddSJeremy L Thompson @param[out] fdm_inv `CeedOperator` to apply the action of a FDM based inverse for each element 2822ca94c3ddSJeremy L Thompson @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 2823eaf62fffSJeremy L Thompson 2824eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2825eaf62fffSJeremy L Thompson 2826480fae85SJeremy L Thompson @ref User 2827eaf62fffSJeremy L Thompson **/ 28282b730f8bSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedRequest *request) { 28291c66c397SJeremy L Thompson Ceed ceed, ceed_parent; 28301c66c397SJeremy L Thompson bool interp = false, grad = false, is_tensor_basis = true; 28311c66c397SJeremy L Thompson CeedInt num_input_fields, P_1d, Q_1d, num_nodes, num_qpts, dim, num_comp = 1, num_elem = 1; 28321c66c397SJeremy L Thompson CeedScalar *mass, *laplace, *x, *fdm_interp, *lambda, *elem_avg; 28331c66c397SJeremy L Thompson const CeedScalar *interp_1d, *grad_1d, *q_weight_1d; 28341c66c397SJeremy L Thompson CeedVector q_data; 28351c66c397SJeremy L Thompson CeedElemRestriction rstr = NULL, rstr_qd_i; 28361c66c397SJeremy L Thompson CeedBasis basis = NULL, fdm_basis; 28371c66c397SJeremy L Thompson CeedQFunctionContext ctx_fdm; 28381c66c397SJeremy L Thompson CeedQFunctionField *qf_fields; 28391c66c397SJeremy L Thompson CeedQFunction qf, qf_fdm; 28401c66c397SJeremy L Thompson CeedOperatorField *op_fields; 28411c66c397SJeremy L Thompson 28422b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 2843eaf62fffSJeremy L Thompson 2844eaf62fffSJeremy L Thompson if (op->CreateFDMElementInverse) { 2845d04bbc78SJeremy L Thompson // Backend version 28462b730f8bSJeremy L Thompson CeedCall(op->CreateFDMElementInverse(op, fdm_inv, request)); 2847eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2848eaf62fffSJeremy L Thompson } else { 2849d04bbc78SJeremy L Thompson // Operator fallback 2850d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2851d04bbc78SJeremy L Thompson 2852*ca38d01dSJeremy L Thompson CeedDebug(CeedOperatorReturnCeed(op), "\nFalling back for CeedOperatorCreateFDMElementInverse\n"); 28532b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFallback(op, &op_fallback)); 2854d04bbc78SJeremy L Thompson if (op_fallback) { 28552b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request)); 2856eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2857eaf62fffSJeremy L Thompson } 2858eaf62fffSJeremy L Thompson } 2859eaf62fffSJeremy L Thompson 2860d04bbc78SJeremy L Thompson // Default interface implementation 28612b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 2862bb229da9SJeremy L Thompson CeedCall(CeedOperatorGetFallbackParentCeed(op, &ceed_parent)); 28632b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetQFunction(op, &qf)); 2864eaf62fffSJeremy L Thompson 2865eaf62fffSJeremy L Thompson // Determine active input basis 28662b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL)); 28672b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL)); 2868eaf62fffSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 2869eaf62fffSJeremy L Thompson CeedVector vec; 28701c66c397SJeremy L Thompson 28712b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldGetVector(op_fields[i], &vec)); 2872eaf62fffSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 2873eaf62fffSJeremy L Thompson CeedEvalMode eval_mode; 28741c66c397SJeremy L Thompson 28752b730f8bSJeremy L Thompson CeedCall(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode)); 2876eaf62fffSJeremy L Thompson interp = interp || eval_mode == CEED_EVAL_INTERP; 2877eaf62fffSJeremy L Thompson grad = grad || eval_mode == CEED_EVAL_GRAD; 2878681d0ea7SJeremy L Thompson if (!basis) CeedCall(CeedOperatorFieldGetBasis(op_fields[i], &basis)); 2879681d0ea7SJeremy L Thompson if (!rstr) CeedCall(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr)); 2880eaf62fffSJeremy L Thompson } 2881681d0ea7SJeremy L Thompson CeedCall(CeedVectorDestroy(&vec)); 2882eaf62fffSJeremy L Thompson } 28836574a04fSJeremy L Thompson CeedCheck(basis, ceed, CEED_ERROR_BACKEND, "No active field set"); 28842b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); 2885352a5e7cSSebastian Grimberg CeedCall(CeedBasisGetNumNodes(basis, &num_nodes)); 28862b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 28872b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 28882b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(basis, &dim)); 28892b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 28902b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem)); 2891eaf62fffSJeremy L Thompson 2892eaf62fffSJeremy L Thompson // Build and diagonalize 1D Mass and Laplacian 28936574a04fSJeremy L Thompson CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); 28946574a04fSJeremy L Thompson CeedCheck(is_tensor_basis, ceed, CEED_ERROR_BACKEND, "FDMElementInverse only supported for tensor bases"); 28952b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &mass)); 28962b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &laplace)); 28972b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &x)); 28982b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &fdm_interp)); 28992b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d, &lambda)); 2900eaf62fffSJeremy L Thompson // -- Build matrices 29012b730f8bSJeremy L Thompson CeedCall(CeedBasisGetInterp1D(basis, &interp_1d)); 29022b730f8bSJeremy L Thompson CeedCall(CeedBasisGetGrad1D(basis, &grad_1d)); 29032b730f8bSJeremy L Thompson CeedCall(CeedBasisGetQWeights(basis, &q_weight_1d)); 29042b730f8bSJeremy L Thompson CeedCall(CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, mass, laplace)); 2905eaf62fffSJeremy L Thompson 2906eaf62fffSJeremy L Thompson // -- Diagonalize 29072b730f8bSJeremy L Thompson CeedCall(CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d)); 29082b730f8bSJeremy L Thompson CeedCall(CeedFree(&mass)); 29092b730f8bSJeremy L Thompson CeedCall(CeedFree(&laplace)); 29102b730f8bSJeremy L Thompson for (CeedInt i = 0; i < P_1d; i++) { 29112b730f8bSJeremy L Thompson for (CeedInt j = 0; j < P_1d; j++) fdm_interp[i + j * P_1d] = x[j + i * P_1d]; 29122b730f8bSJeremy L Thompson } 29132b730f8bSJeremy L Thompson CeedCall(CeedFree(&x)); 2914eaf62fffSJeremy L Thompson 29151c66c397SJeremy L Thompson { 29161c66c397SJeremy L Thompson CeedInt layout[3], num_modes = (interp ? 1 : 0) + (grad ? dim : 0); 29171c66c397SJeremy L Thompson CeedScalar max_norm = 0; 29181c66c397SJeremy L Thompson const CeedScalar *assembled_array, *q_weight_array; 29191c66c397SJeremy L Thompson CeedVector assembled = NULL, q_weight; 2920c5f45aeaSJeremy L Thompson CeedElemRestriction rstr_qf = NULL; 29211c66c397SJeremy L Thompson 29221c66c397SJeremy L Thompson // Assemble QFunction 29232b730f8bSJeremy L Thompson CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, &rstr_qf, request)); 292456c48462SJeremy L Thompson CeedCall(CeedElemRestrictionGetELayout(rstr_qf, layout)); 29252b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_qf)); 29262b730f8bSJeremy L Thompson CeedCall(CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm)); 2927eaf62fffSJeremy L Thompson 2928eaf62fffSJeremy L Thompson // Calculate element averages 29292b730f8bSJeremy L Thompson CeedCall(CeedVectorCreate(ceed_parent, num_qpts, &q_weight)); 29302b730f8bSJeremy L Thompson CeedCall(CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_weight)); 29312b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array)); 29322b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array)); 29332b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_elem, &elem_avg)); 2934eaf62fffSJeremy L Thompson const CeedScalar qf_value_bound = max_norm * 100 * CEED_EPSILON; 29351c66c397SJeremy L Thompson 2936eaf62fffSJeremy L Thompson for (CeedInt e = 0; e < num_elem; e++) { 2937eaf62fffSJeremy L Thompson CeedInt count = 0; 29381c66c397SJeremy L Thompson 29392b730f8bSJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 29402b730f8bSJeremy L Thompson for (CeedInt i = 0; i < num_comp * num_comp * num_modes * num_modes; i++) { 29412b730f8bSJeremy L Thompson if (fabs(assembled_array[q * layout[0] + i * layout[1] + e * layout[2]]) > qf_value_bound) { 29422b730f8bSJeremy L Thompson elem_avg[e] += assembled_array[q * layout[0] + i * layout[1] + e * layout[2]] / q_weight_array[q]; 2943eaf62fffSJeremy L Thompson count++; 2944eaf62fffSJeremy L Thompson } 29452b730f8bSJeremy L Thompson } 29462b730f8bSJeremy L Thompson } 2947eaf62fffSJeremy L Thompson if (count) { 2948eaf62fffSJeremy L Thompson elem_avg[e] /= count; 2949eaf62fffSJeremy L Thompson } else { 2950eaf62fffSJeremy L Thompson elem_avg[e] = 1.0; 2951eaf62fffSJeremy L Thompson } 2952eaf62fffSJeremy L Thompson } 29532b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(assembled, &assembled_array)); 29542b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&assembled)); 29552b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArrayRead(q_weight, &q_weight_array)); 29562b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&q_weight)); 29571c66c397SJeremy L Thompson } 2958eaf62fffSJeremy L Thompson 2959eaf62fffSJeremy L Thompson // Build FDM diagonal 29601c66c397SJeremy L Thompson { 2961eaf62fffSJeremy L Thompson CeedScalar *q_data_array, *fdm_diagonal; 29621c66c397SJeremy L Thompson 2963352a5e7cSSebastian Grimberg CeedCall(CeedCalloc(num_comp * num_nodes, &fdm_diagonal)); 2964352a5e7cSSebastian Grimberg const CeedScalar fdm_diagonal_bound = num_nodes * CEED_EPSILON; 29652b730f8bSJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) { 2966352a5e7cSSebastian Grimberg for (CeedInt n = 0; n < num_nodes; n++) { 2967352a5e7cSSebastian Grimberg if (interp) fdm_diagonal[c * num_nodes + n] = 1.0; 29682b730f8bSJeremy L Thompson if (grad) { 2969eaf62fffSJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 2970eaf62fffSJeremy L Thompson CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d; 2971352a5e7cSSebastian Grimberg fdm_diagonal[c * num_nodes + n] += lambda[i]; 2972eaf62fffSJeremy L Thompson } 2973eaf62fffSJeremy L Thompson } 2974352a5e7cSSebastian Grimberg if (fabs(fdm_diagonal[c * num_nodes + n]) < fdm_diagonal_bound) fdm_diagonal[c * num_nodes + n] = fdm_diagonal_bound; 29752b730f8bSJeremy L Thompson } 29762b730f8bSJeremy L Thompson } 2977352a5e7cSSebastian Grimberg CeedCall(CeedVectorCreate(ceed_parent, num_elem * num_comp * num_nodes, &q_data)); 29782b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(q_data, 0.0)); 29792b730f8bSJeremy L Thompson CeedCall(CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array)); 29802b730f8bSJeremy L Thompson for (CeedInt e = 0; e < num_elem; e++) { 29812b730f8bSJeremy L Thompson for (CeedInt c = 0; c < num_comp; c++) { 29826c10af5dSJeremy L Thompson for (CeedInt n = 0; n < num_nodes; n++) { 29831c66c397SJeremy L Thompson q_data_array[(e * num_comp + c) * num_nodes + n] = 1. / (elem_avg[e] * fdm_diagonal[c * num_nodes + n]); 29842b730f8bSJeremy L Thompson } 29852b730f8bSJeremy L Thompson } 29866c10af5dSJeremy L Thompson } 29872b730f8bSJeremy L Thompson CeedCall(CeedFree(&elem_avg)); 29882b730f8bSJeremy L Thompson CeedCall(CeedFree(&fdm_diagonal)); 29892b730f8bSJeremy L Thompson CeedCall(CeedVectorRestoreArray(q_data, &q_data_array)); 29901c66c397SJeremy L Thompson } 2991eaf62fffSJeremy L Thompson 2992eaf62fffSJeremy L Thompson // Setup FDM operator 2993eaf62fffSJeremy L Thompson // -- Basis 29941c66c397SJeremy L Thompson { 2995eaf62fffSJeremy L Thompson CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy; 29961c66c397SJeremy L Thompson 29972b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d * P_1d, &grad_dummy)); 29982b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d, &q_ref_dummy)); 29992b730f8bSJeremy L Thompson CeedCall(CeedCalloc(P_1d, &q_weight_dummy)); 30002b730f8bSJeremy 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)); 30012b730f8bSJeremy L Thompson CeedCall(CeedFree(&fdm_interp)); 30022b730f8bSJeremy L Thompson CeedCall(CeedFree(&grad_dummy)); 30032b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_ref_dummy)); 30042b730f8bSJeremy L Thompson CeedCall(CeedFree(&q_weight_dummy)); 30052b730f8bSJeremy L Thompson CeedCall(CeedFree(&lambda)); 30061c66c397SJeremy L Thompson } 3007eaf62fffSJeremy L Thompson 3008eaf62fffSJeremy L Thompson // -- Restriction 30091c66c397SJeremy L Thompson { 3010352a5e7cSSebastian Grimberg CeedInt strides[3] = {1, num_nodes, num_nodes * num_comp}; 30110a5597ceSJeremy L Thompson CeedCall(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, num_nodes, num_comp, 30120a5597ceSJeremy L Thompson (CeedSize)num_elem * (CeedSize)num_comp * (CeedSize)num_nodes, strides, &rstr_qd_i)); 30131c66c397SJeremy L Thompson } 30141c66c397SJeremy L Thompson 3015eaf62fffSJeremy L Thompson // -- QFunction 30162b730f8bSJeremy L Thompson CeedCall(CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm)); 30172b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP)); 30182b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE)); 30192b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP)); 30202b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp)); 30211c66c397SJeremy L Thompson 3022eaf62fffSJeremy L Thompson // -- QFunction context 30231c66c397SJeremy L Thompson { 3024eaf62fffSJeremy L Thompson CeedInt *num_comp_data; 30251c66c397SJeremy L Thompson 30262b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &num_comp_data)); 3027eaf62fffSJeremy L Thompson num_comp_data[0] = num_comp; 30282b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextCreate(ceed, &ctx_fdm)); 30292b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, sizeof(*num_comp_data), num_comp_data)); 30301c66c397SJeremy L Thompson } 30312b730f8bSJeremy L Thompson CeedCall(CeedQFunctionSetContext(qf_fdm, ctx_fdm)); 30322b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(&ctx_fdm)); 30331c66c397SJeremy L Thompson 3034eaf62fffSJeremy L Thompson // -- Operator 30352b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv)); 30362b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE)); 3037356036faSJeremy L Thompson CeedCall(CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_NONE, q_data)); 30382b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE)); 3039eaf62fffSJeremy L Thompson 3040eaf62fffSJeremy L Thompson // Cleanup 30419bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed)); 30429bc66399SJeremy L Thompson CeedCall(CeedDestroy(&ceed_parent)); 30432b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&q_data)); 3044681d0ea7SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr)); 30452b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i)); 3046681d0ea7SJeremy L Thompson CeedCall(CeedBasisDestroy(&basis)); 3047681d0ea7SJeremy L Thompson CeedCall(CeedBasisDestroy(&fdm_basis)); 3048c11e12f4SJeremy L Thompson CeedCall(CeedQFunctionDestroy(&qf)); 30492b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&qf_fdm)); 3050eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 3051eaf62fffSJeremy L Thompson } 3052eaf62fffSJeremy L Thompson 3053eaf62fffSJeremy L Thompson /// @} 3054