13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3eaf62fffSJeremy L Thompson // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5eaf62fffSJeremy L Thompson // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7eaf62fffSJeremy L Thompson 8eaf62fffSJeremy L Thompson #include <ceed/ceed.h> 9eaf62fffSJeremy L Thompson #include <ceed/backend.h> 10eaf62fffSJeremy L Thompson #include <ceed-impl.h> 11eaf62fffSJeremy L Thompson #include <math.h> 12ed9e99e6SJeremy L Thompson #include <assert.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 /** 279e77b9c8SJeremy L Thompson @brief Duplicate a CeedQFunction with a reference Ceed to fallback for advanced 289e77b9c8SJeremy L Thompson CeedOperator functionality 299e77b9c8SJeremy L Thompson 3001ea9c81SJed Brown @param[in] fallback_ceed Ceed on which to create fallback CeedQFunction 319e77b9c8SJeremy L Thompson @param[in] qf CeedQFunction to create fallback for 3201ea9c81SJed Brown @param[out] qf_fallback fallback CeedQFunction 339e77b9c8SJeremy L Thompson 349e77b9c8SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 359e77b9c8SJeremy L Thompson 369e77b9c8SJeremy L Thompson @ref Developer 379e77b9c8SJeremy L Thompson **/ 389e77b9c8SJeremy L Thompson static int CeedQFunctionCreateFallback(Ceed fallback_ceed, CeedQFunction qf, 399e77b9c8SJeremy L Thompson CeedQFunction *qf_fallback) { 409e77b9c8SJeremy L Thompson int ierr; 419e77b9c8SJeremy L Thompson 429e77b9c8SJeremy L Thompson // Check if NULL qf passed in 439e77b9c8SJeremy L Thompson if (!qf) return CEED_ERROR_SUCCESS; 449e77b9c8SJeremy L Thompson 45d04bbc78SJeremy L Thompson CeedDebug256(qf->ceed, 1, "---------- CeedOperator Fallback ----------\n"); 46d04bbc78SJeremy L Thompson CeedDebug256(qf->ceed, 255, "Creating fallback CeedQFunction\n"); 47d04bbc78SJeremy L Thompson 489e77b9c8SJeremy L Thompson char *source_path_with_name = ""; 499e77b9c8SJeremy L Thompson if (qf->source_path) { 509e77b9c8SJeremy L Thompson size_t path_len = strlen(qf->source_path), 519e77b9c8SJeremy L Thompson name_len = strlen(qf->kernel_name); 529e77b9c8SJeremy L Thompson ierr = CeedCalloc(path_len + name_len + 2, &source_path_with_name); 539e77b9c8SJeremy L Thompson CeedChk(ierr); 549e77b9c8SJeremy L Thompson memcpy(source_path_with_name, qf->source_path, path_len); 559e77b9c8SJeremy L Thompson memcpy(&source_path_with_name[path_len], ":", 1); 569e77b9c8SJeremy L Thompson memcpy(&source_path_with_name[path_len + 1], qf->kernel_name, name_len); 579e77b9c8SJeremy L Thompson } else { 589e77b9c8SJeremy L Thompson ierr = CeedCalloc(1, &source_path_with_name); CeedChk(ierr); 599e77b9c8SJeremy L Thompson } 609e77b9c8SJeremy L Thompson 619e77b9c8SJeremy L Thompson ierr = CeedQFunctionCreateInterior(fallback_ceed, qf->vec_length, 629e77b9c8SJeremy L Thompson qf->function, source_path_with_name, 639e77b9c8SJeremy L Thompson qf_fallback); CeedChk(ierr); 649e77b9c8SJeremy L Thompson { 659e77b9c8SJeremy L Thompson CeedQFunctionContext ctx; 669e77b9c8SJeremy L Thompson 679e77b9c8SJeremy L Thompson ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr); 689e77b9c8SJeremy L Thompson ierr = CeedQFunctionSetContext(*qf_fallback, ctx); CeedChk(ierr); 699e77b9c8SJeremy L Thompson } 709e77b9c8SJeremy L Thompson for (CeedInt i = 0; i < qf->num_input_fields; i++) { 719e77b9c8SJeremy L Thompson ierr = CeedQFunctionAddInput(*qf_fallback, qf->input_fields[i]->field_name, 729e77b9c8SJeremy L Thompson qf->input_fields[i]->size, 739e77b9c8SJeremy L Thompson qf->input_fields[i]->eval_mode); CeedChk(ierr); 749e77b9c8SJeremy L Thompson } 759e77b9c8SJeremy L Thompson for (CeedInt i = 0; i < qf->num_output_fields; i++) { 769e77b9c8SJeremy L Thompson ierr = CeedQFunctionAddOutput(*qf_fallback, qf->output_fields[i]->field_name, 779e77b9c8SJeremy L Thompson qf->output_fields[i]->size, 789e77b9c8SJeremy L Thompson qf->output_fields[i]->eval_mode); CeedChk(ierr); 799e77b9c8SJeremy L Thompson } 809e77b9c8SJeremy L Thompson ierr = CeedFree(&source_path_with_name); CeedChk(ierr); 819e77b9c8SJeremy L Thompson 829e77b9c8SJeremy L Thompson return CEED_ERROR_SUCCESS; 839e77b9c8SJeremy L Thompson } 849e77b9c8SJeremy L Thompson 859e77b9c8SJeremy L Thompson /** 86eaf62fffSJeremy L Thompson @brief Duplicate a CeedOperator with a reference Ceed to fallback for advanced 87eaf62fffSJeremy L Thompson CeedOperator functionality 88eaf62fffSJeremy L Thompson 89eaf62fffSJeremy L Thompson @param op CeedOperator to create fallback for 90eaf62fffSJeremy L Thompson 91eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 92eaf62fffSJeremy L Thompson 93eaf62fffSJeremy L Thompson @ref Developer 94eaf62fffSJeremy L Thompson **/ 95d04bbc78SJeremy L Thompson static int CeedOperatorCreateFallback(CeedOperator op) { 96eaf62fffSJeremy L Thompson int ierr; 979e77b9c8SJeremy L Thompson Ceed ceed_fallback; 98eaf62fffSJeremy L Thompson 99805fe78eSJeremy L Thompson // Check not already created 100805fe78eSJeremy L Thompson if (op->op_fallback) return CEED_ERROR_SUCCESS; 101805fe78eSJeremy L Thompson 102eaf62fffSJeremy L Thompson // Fallback Ceed 1039e77b9c8SJeremy L Thompson ierr = CeedGetOperatorFallbackCeed(op->ceed, &ceed_fallback); CeedChk(ierr); 104d04bbc78SJeremy L Thompson if (!ceed_fallback) return CEED_ERROR_SUCCESS; 105d04bbc78SJeremy L Thompson 106d04bbc78SJeremy L Thompson CeedDebug256(op->ceed, 1, "---------- CeedOperator Fallback ----------\n"); 107d04bbc78SJeremy L Thompson CeedDebug256(op->ceed, 255, "Creating fallback CeedOperator\n"); 108eaf62fffSJeremy L Thompson 109eaf62fffSJeremy L Thompson // Clone Op 110805fe78eSJeremy L Thompson CeedOperator op_fallback; 111805fe78eSJeremy L Thompson if (op->is_composite) { 1129e77b9c8SJeremy L Thompson ierr = CeedCompositeOperatorCreate(ceed_fallback, &op_fallback); 113805fe78eSJeremy L Thompson CeedChk(ierr); 114805fe78eSJeremy L Thompson for (CeedInt i = 0; i < op->num_suboperators; i++) { 115d04bbc78SJeremy L Thompson CeedOperator op_sub_fallback; 116d04bbc78SJeremy L Thompson 117d04bbc78SJeremy L Thompson ierr = CeedOperatorGetFallback(op->sub_operators[i], &op_sub_fallback); 118805fe78eSJeremy L Thompson CeedChk(ierr); 119d04bbc78SJeremy L Thompson ierr = CeedCompositeOperatorAddSub(op_fallback, op_sub_fallback); CeedChk(ierr); 120805fe78eSJeremy L Thompson } 121805fe78eSJeremy L Thompson } else { 1229e77b9c8SJeremy L Thompson CeedQFunction qf_fallback = NULL, dqf_fallback = NULL, dqfT_fallback = NULL; 1239e77b9c8SJeremy L Thompson ierr = CeedQFunctionCreateFallback(ceed_fallback, op->qf, &qf_fallback); 1249e77b9c8SJeremy L Thompson CeedChk(ierr); 1259e77b9c8SJeremy L Thompson ierr = CeedQFunctionCreateFallback(ceed_fallback, op->dqf, &dqf_fallback); 1269e77b9c8SJeremy L Thompson CeedChk(ierr); 1279e77b9c8SJeremy L Thompson ierr = CeedQFunctionCreateFallback(ceed_fallback, op->dqfT, &dqfT_fallback); 1289e77b9c8SJeremy L Thompson CeedChk(ierr); 1299e77b9c8SJeremy L Thompson ierr = CeedOperatorCreate(ceed_fallback, qf_fallback, dqf_fallback, 1309e77b9c8SJeremy L Thompson dqfT_fallback, &op_fallback); CeedChk(ierr); 131805fe78eSJeremy L Thompson for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 132805fe78eSJeremy L Thompson ierr = CeedOperatorSetField(op_fallback, op->input_fields[i]->field_name, 133805fe78eSJeremy L Thompson op->input_fields[i]->elem_restr, 134805fe78eSJeremy L Thompson op->input_fields[i]->basis, 135805fe78eSJeremy L Thompson op->input_fields[i]->vec); CeedChk(ierr); 136805fe78eSJeremy L Thompson } 137805fe78eSJeremy L Thompson for (CeedInt i = 0; i < op->qf->num_output_fields; i++) { 138805fe78eSJeremy L Thompson ierr = CeedOperatorSetField(op_fallback, op->output_fields[i]->field_name, 139805fe78eSJeremy L Thompson op->output_fields[i]->elem_restr, 140805fe78eSJeremy L Thompson op->output_fields[i]->basis, 141805fe78eSJeremy L Thompson op->output_fields[i]->vec); CeedChk(ierr); 142805fe78eSJeremy L Thompson } 143480fae85SJeremy L Thompson ierr = CeedQFunctionAssemblyDataReferenceCopy(op->qf_assembled, 144805fe78eSJeremy L Thompson &op_fallback->qf_assembled); CeedChk(ierr); 145805fe78eSJeremy L Thompson if (op_fallback->num_qpts == 0) { 146805fe78eSJeremy L Thompson ierr = CeedOperatorSetNumQuadraturePoints(op_fallback, op->num_qpts); 147805fe78eSJeremy L Thompson CeedChk(ierr); 148805fe78eSJeremy L Thompson } 1499e77b9c8SJeremy L Thompson // Cleanup 1509e77b9c8SJeremy L Thompson ierr = CeedQFunctionDestroy(&qf_fallback); CeedChk(ierr); 1519e77b9c8SJeremy L Thompson ierr = CeedQFunctionDestroy(&dqf_fallback); CeedChk(ierr); 1529e77b9c8SJeremy L Thompson ierr = CeedQFunctionDestroy(&dqfT_fallback); CeedChk(ierr); 153805fe78eSJeremy L Thompson } 154805fe78eSJeremy L Thompson ierr = CeedOperatorSetName(op_fallback, op->name); CeedChk(ierr); 155805fe78eSJeremy L Thompson ierr = CeedOperatorCheckReady(op_fallback); CeedChk(ierr); 156805fe78eSJeremy L Thompson op->op_fallback = op_fallback; 157eaf62fffSJeremy L Thompson 158eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 159eaf62fffSJeremy L Thompson } 160eaf62fffSJeremy L Thompson 161eaf62fffSJeremy L Thompson /** 162d04bbc78SJeremy L Thompson @brief Retreive fallback CeedOperator with a reference Ceed for advanced CeedOperator functionality 163d04bbc78SJeremy L Thompson 164d04bbc78SJeremy L Thompson @param[in] op CeedOperator to retrieve fallback for 165d04bbc78SJeremy L Thompson @param[out] op_fallback Fallback CeedOperator 166d04bbc78SJeremy L Thompson 167d04bbc78SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 168d04bbc78SJeremy L Thompson 169d04bbc78SJeremy L Thompson @ref Developer 170d04bbc78SJeremy L Thompson **/ 171d04bbc78SJeremy L Thompson int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) { 172d04bbc78SJeremy L Thompson int ierr; 173d04bbc78SJeremy L Thompson 174d04bbc78SJeremy L Thompson // Create if needed 175d04bbc78SJeremy L Thompson if (!op->op_fallback) { 176d04bbc78SJeremy L Thompson ierr = CeedOperatorCreateFallback(op); CeedChk(ierr); 177d04bbc78SJeremy L Thompson } 178d04bbc78SJeremy L Thompson if (op->op_fallback) { 179d04bbc78SJeremy L Thompson bool is_debug; 180d04bbc78SJeremy L Thompson 181d04bbc78SJeremy L Thompson ierr = CeedIsDebug(op->ceed, &is_debug); CeedChk(ierr); 182d04bbc78SJeremy L Thompson if (is_debug) { 183d04bbc78SJeremy L Thompson Ceed ceed_fallback; 184d04bbc78SJeremy L Thompson const char *resource, *resource_fallback; 185d04bbc78SJeremy L Thompson 186d04bbc78SJeremy L Thompson ierr = CeedGetOperatorFallbackCeed(op->ceed, &ceed_fallback); CeedChk(ierr); 187d04bbc78SJeremy L Thompson ierr = CeedGetResource(op->ceed, &resource); CeedChk(ierr); 188d04bbc78SJeremy L Thompson ierr = CeedGetResource(ceed_fallback, &resource_fallback); CeedChk(ierr); 189d04bbc78SJeremy L Thompson 190d04bbc78SJeremy L Thompson CeedDebug256(op->ceed, 1, "---------- CeedOperator Fallback ----------\n"); 191d04bbc78SJeremy L Thompson CeedDebug256(op->ceed, 255, 192d04bbc78SJeremy L Thompson "Falling back from %s operator at address %ld to %s operator at address %ld\n", 193d04bbc78SJeremy L Thompson resource, op, resource_fallback, op->op_fallback); 194d04bbc78SJeremy L Thompson } 195d04bbc78SJeremy L Thompson } 196d04bbc78SJeremy L Thompson *op_fallback = op->op_fallback; 197d04bbc78SJeremy L Thompson 198d04bbc78SJeremy L Thompson return CEED_ERROR_SUCCESS; 199d04bbc78SJeremy L Thompson } 200d04bbc78SJeremy L Thompson 201d04bbc78SJeremy L Thompson /** 202eaf62fffSJeremy L Thompson @brief Select correct basis matrix pointer based on CeedEvalMode 203eaf62fffSJeremy L Thompson 204eaf62fffSJeremy L Thompson @param[in] eval_mode Current basis evaluation mode 205eaf62fffSJeremy L Thompson @param[in] identity Pointer to identity matrix 206eaf62fffSJeremy L Thompson @param[in] interp Pointer to interpolation matrix 207eaf62fffSJeremy L Thompson @param[in] grad Pointer to gradient matrix 208eaf62fffSJeremy L Thompson @param[out] basis_ptr Basis pointer to set 209eaf62fffSJeremy L Thompson 210eaf62fffSJeremy L Thompson @ref Developer 211eaf62fffSJeremy L Thompson **/ 212eaf62fffSJeremy L Thompson static inline void CeedOperatorGetBasisPointer(CeedEvalMode eval_mode, 213eaf62fffSJeremy L Thompson const CeedScalar *identity, const CeedScalar *interp, 214eaf62fffSJeremy L Thompson const CeedScalar *grad, const CeedScalar **basis_ptr) { 215eaf62fffSJeremy L Thompson switch (eval_mode) { 216eaf62fffSJeremy L Thompson case CEED_EVAL_NONE: 217eaf62fffSJeremy L Thompson *basis_ptr = identity; 218eaf62fffSJeremy L Thompson break; 219eaf62fffSJeremy L Thompson case CEED_EVAL_INTERP: 220eaf62fffSJeremy L Thompson *basis_ptr = interp; 221eaf62fffSJeremy L Thompson break; 222eaf62fffSJeremy L Thompson case CEED_EVAL_GRAD: 223eaf62fffSJeremy L Thompson *basis_ptr = grad; 224eaf62fffSJeremy L Thompson break; 225eaf62fffSJeremy L Thompson case CEED_EVAL_WEIGHT: 226eaf62fffSJeremy L Thompson case CEED_EVAL_DIV: 227eaf62fffSJeremy L Thompson case CEED_EVAL_CURL: 228eaf62fffSJeremy L Thompson break; // Caught by QF Assembly 229eaf62fffSJeremy L Thompson } 230ed9e99e6SJeremy L Thompson assert(*basis_ptr != NULL); 231eaf62fffSJeremy L Thompson } 232eaf62fffSJeremy L Thompson 233eaf62fffSJeremy L Thompson /** 234eaf62fffSJeremy L Thompson @brief Create point block restriction for active operator field 235eaf62fffSJeremy L Thompson 236eaf62fffSJeremy L Thompson @param[in] rstr Original CeedElemRestriction for active field 237eaf62fffSJeremy L Thompson @param[out] pointblock_rstr Address of the variable where the newly created 238eaf62fffSJeremy L Thompson CeedElemRestriction will be stored 239eaf62fffSJeremy L Thompson 240eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 241eaf62fffSJeremy L Thompson 242eaf62fffSJeremy L Thompson @ref Developer 243eaf62fffSJeremy L Thompson **/ 244eaf62fffSJeremy L Thompson static int CeedOperatorCreateActivePointBlockRestriction( 245eaf62fffSJeremy L Thompson CeedElemRestriction rstr, 246eaf62fffSJeremy L Thompson CeedElemRestriction *pointblock_rstr) { 247eaf62fffSJeremy L Thompson int ierr; 248eaf62fffSJeremy L Thompson Ceed ceed; 249eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetCeed(rstr, &ceed); CeedChk(ierr); 250eaf62fffSJeremy L Thompson const CeedInt *offsets; 251eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets); 252eaf62fffSJeremy L Thompson CeedChk(ierr); 253eaf62fffSJeremy L Thompson 254eaf62fffSJeremy L Thompson // Expand offsets 2557b63f5c6SJed Brown CeedInt num_elem, num_comp, elem_size, comp_stride, *pointblock_offsets; 2567b63f5c6SJed Brown CeedSize l_size; 257eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetNumElements(rstr, &num_elem); CeedChk(ierr); 258eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetNumComponents(rstr, &num_comp); CeedChk(ierr); 259eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetElementSize(rstr, &elem_size); CeedChk(ierr); 260eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetCompStride(rstr, &comp_stride); CeedChk(ierr); 2617b63f5c6SJed Brown ierr = CeedElemRestrictionGetLVectorSize(rstr, &l_size); CeedChk(ierr); 262eaf62fffSJeremy L Thompson CeedInt shift = num_comp; 263eaf62fffSJeremy L Thompson if (comp_stride != 1) 264eaf62fffSJeremy L Thompson shift *= num_comp; 265eaf62fffSJeremy L Thompson ierr = CeedCalloc(num_elem*elem_size, &pointblock_offsets); 266eaf62fffSJeremy L Thompson CeedChk(ierr); 267eaf62fffSJeremy L Thompson for (CeedInt i = 0; i < num_elem*elem_size; i++) { 268eaf62fffSJeremy L Thompson pointblock_offsets[i] = offsets[i]*shift; 269eaf62fffSJeremy L Thompson } 270eaf62fffSJeremy L Thompson 271eaf62fffSJeremy L Thompson // Create new restriction 272eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp*num_comp, 2737b63f5c6SJed Brown 1, l_size * num_comp, CEED_MEM_HOST, 274eaf62fffSJeremy L Thompson CEED_OWN_POINTER, pointblock_offsets, pointblock_rstr); 275eaf62fffSJeremy L Thompson CeedChk(ierr); 276eaf62fffSJeremy L Thompson 277eaf62fffSJeremy L Thompson // Cleanup 278eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionRestoreOffsets(rstr, &offsets); CeedChk(ierr); 279eaf62fffSJeremy L Thompson 280eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 281eaf62fffSJeremy L Thompson } 282eaf62fffSJeremy L Thompson 283eaf62fffSJeremy L Thompson /** 284eaf62fffSJeremy L Thompson @brief Core logic for assembling operator diagonal or point block diagonal 285eaf62fffSJeremy L Thompson 286eaf62fffSJeremy L Thompson @param[in] op CeedOperator to assemble point block diagonal 287eaf62fffSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else 288eaf62fffSJeremy L Thompson CEED_REQUEST_IMMEDIATE 289eaf62fffSJeremy L Thompson @param[in] is_pointblock Boolean flag to assemble diagonal or point block diagonal 290eaf62fffSJeremy L Thompson @param[out] assembled CeedVector to store assembled diagonal 291eaf62fffSJeremy L Thompson 292eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 293eaf62fffSJeremy L Thompson 294eaf62fffSJeremy L Thompson @ref Developer 295eaf62fffSJeremy L Thompson **/ 2960e455453SJeremy L Thompson static inline int CeedSingleOperatorAssembleAddDiagonal_Core(CeedOperator op, 297eaf62fffSJeremy L Thompson CeedRequest *request, const bool is_pointblock, CeedVector assembled) { 298eaf62fffSJeremy L Thompson int ierr; 299eaf62fffSJeremy L Thompson Ceed ceed; 300eaf62fffSJeremy L Thompson ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr); 301eaf62fffSJeremy L Thompson 302eaf62fffSJeremy L Thompson // Assemble QFunction 303eaf62fffSJeremy L Thompson CeedQFunction qf; 304eaf62fffSJeremy L Thompson ierr = CeedOperatorGetQFunction(op, &qf); CeedChk(ierr); 305eaf62fffSJeremy L Thompson CeedInt num_input_fields, num_output_fields; 306eaf62fffSJeremy L Thompson ierr= CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields); 307eaf62fffSJeremy L Thompson CeedChk(ierr); 308eaf62fffSJeremy L Thompson CeedVector assembled_qf; 309eaf62fffSJeremy L Thompson CeedElemRestriction rstr; 31070a7ffb3SJeremy L Thompson ierr = CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, 31170a7ffb3SJeremy L Thompson &rstr, request); CeedChk(ierr); 312eaf62fffSJeremy L Thompson CeedInt layout[3]; 313eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetELayout(rstr, &layout); CeedChk(ierr); 314eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionDestroy(&rstr); CeedChk(ierr); 315eaf62fffSJeremy L Thompson 316ed9e99e6SJeremy L Thompson // Get assembly data 317ed9e99e6SJeremy L Thompson CeedOperatorAssemblyData data; 318ed9e99e6SJeremy L Thompson ierr = CeedOperatorGetOperatorAssemblyData(op, &data); CeedChk(ierr); 319ed9e99e6SJeremy L Thompson const CeedEvalMode *eval_mode_in, *eval_mode_out; 320ed9e99e6SJeremy L Thompson CeedInt num_eval_mode_in, num_eval_mode_out; 321ed9e99e6SJeremy L Thompson ierr = CeedOperatorAssemblyDataGetEvalModes(data, &num_eval_mode_in, 322ed9e99e6SJeremy L Thompson &eval_mode_in, &num_eval_mode_out, &eval_mode_out); CeedChk(ierr); 323ed9e99e6SJeremy L Thompson CeedBasis basis_in, basis_out; 324ed9e99e6SJeremy L Thompson ierr = CeedOperatorAssemblyDataGetBases(data, &basis_in, NULL, &basis_out, 325ed9e99e6SJeremy L Thompson NULL); CeedChk(ierr); 326ed9e99e6SJeremy L Thompson CeedInt num_comp; 327eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumComponents(basis_in, &num_comp); CeedChk(ierr); 328eaf62fffSJeremy L Thompson 329eaf62fffSJeremy L Thompson // Assemble point block diagonal restriction, if needed 330ed9e99e6SJeremy L Thompson CeedElemRestriction diag_rstr; 331ed9e99e6SJeremy L Thompson ierr = CeedOperatorGetActiveElemRestriction(op, &diag_rstr); CeedChk(ierr); 332eaf62fffSJeremy L Thompson if (is_pointblock) { 333ed9e99e6SJeremy L Thompson CeedElemRestriction point_block_rstr; 334ed9e99e6SJeremy L Thompson ierr = CeedOperatorCreateActivePointBlockRestriction(diag_rstr, 335ed9e99e6SJeremy L Thompson &point_block_rstr); 336eaf62fffSJeremy L Thompson CeedChk(ierr); 337ed9e99e6SJeremy L Thompson diag_rstr = point_block_rstr; 338eaf62fffSJeremy L Thompson } 339eaf62fffSJeremy L Thompson 340eaf62fffSJeremy L Thompson // Create diagonal vector 341eaf62fffSJeremy L Thompson CeedVector elem_diag; 342eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionCreateVector(diag_rstr, NULL, &elem_diag); 343eaf62fffSJeremy L Thompson CeedChk(ierr); 344eaf62fffSJeremy L Thompson 345eaf62fffSJeremy L Thompson // Assemble element operator diagonals 3469c774eddSJeremy L Thompson CeedScalar *elem_diag_array; 3479c774eddSJeremy L Thompson const CeedScalar *assembled_qf_array; 348eaf62fffSJeremy L Thompson ierr = CeedVectorSetValue(elem_diag, 0.0); CeedChk(ierr); 349eaf62fffSJeremy L Thompson ierr = CeedVectorGetArray(elem_diag, CEED_MEM_HOST, &elem_diag_array); 350eaf62fffSJeremy L Thompson CeedChk(ierr); 3519c774eddSJeremy L Thompson ierr = CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array); 352eaf62fffSJeremy L Thompson CeedChk(ierr); 353eaf62fffSJeremy L Thompson CeedInt num_elem, num_nodes, num_qpts; 354eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetNumElements(diag_rstr, &num_elem); CeedChk(ierr); 355eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumNodes(basis_in, &num_nodes); CeedChk(ierr); 356eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts); CeedChk(ierr); 357ed9e99e6SJeremy L Thompson 358eaf62fffSJeremy L Thompson // Basis matrices 359eaf62fffSJeremy L Thompson const CeedScalar *interp_in, *interp_out, *grad_in, *grad_out; 360eaf62fffSJeremy L Thompson CeedScalar *identity = NULL; 361ed9e99e6SJeremy L Thompson bool has_eval_none = false; 362ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < num_eval_mode_in; i++) { 363ed9e99e6SJeremy L Thompson has_eval_none = has_eval_none || (eval_mode_in[i] == CEED_EVAL_NONE); 364ed9e99e6SJeremy L Thompson } 365ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i<num_eval_mode_out; i++) { 366ed9e99e6SJeremy L Thompson has_eval_none = has_eval_none || (eval_mode_out[i] == CEED_EVAL_NONE); 367ed9e99e6SJeremy L Thompson } 368ed9e99e6SJeremy L Thompson if (has_eval_none) { 369eaf62fffSJeremy L Thompson ierr = CeedCalloc(num_qpts*num_nodes, &identity); CeedChk(ierr); 370eaf62fffSJeremy L Thompson for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) 371eaf62fffSJeremy L Thompson identity[i*num_nodes+i] = 1.0; 372eaf62fffSJeremy L Thompson } 373eaf62fffSJeremy L Thompson ierr = CeedBasisGetInterp(basis_in, &interp_in); CeedChk(ierr); 374eaf62fffSJeremy L Thompson ierr = CeedBasisGetInterp(basis_out, &interp_out); CeedChk(ierr); 375eaf62fffSJeremy L Thompson ierr = CeedBasisGetGrad(basis_in, &grad_in); CeedChk(ierr); 376eaf62fffSJeremy L Thompson ierr = CeedBasisGetGrad(basis_out, &grad_out); CeedChk(ierr); 377eaf62fffSJeremy L Thompson // Compute the diagonal of B^T D B 378eaf62fffSJeremy L Thompson // Each element 379eaf62fffSJeremy L Thompson for (CeedInt e=0; e<num_elem; e++) { 380eaf62fffSJeremy L Thompson CeedInt d_out = -1; 381eaf62fffSJeremy L Thompson // Each basis eval mode pair 382eaf62fffSJeremy L Thompson for (CeedInt e_out=0; e_out<num_eval_mode_out; e_out++) { 383eaf62fffSJeremy L Thompson const CeedScalar *bt = NULL; 384eaf62fffSJeremy L Thompson if (eval_mode_out[e_out] == CEED_EVAL_GRAD) 385eaf62fffSJeremy L Thompson d_out += 1; 386eaf62fffSJeremy L Thompson CeedOperatorGetBasisPointer(eval_mode_out[e_out], identity, interp_out, 387eaf62fffSJeremy L Thompson &grad_out[d_out*num_qpts*num_nodes], &bt); 388eaf62fffSJeremy L Thompson CeedInt d_in = -1; 389eaf62fffSJeremy L Thompson for (CeedInt e_in=0; e_in<num_eval_mode_in; e_in++) { 390eaf62fffSJeremy L Thompson const CeedScalar *b = NULL; 391eaf62fffSJeremy L Thompson if (eval_mode_in[e_in] == CEED_EVAL_GRAD) 392eaf62fffSJeremy L Thompson d_in += 1; 393eaf62fffSJeremy L Thompson CeedOperatorGetBasisPointer(eval_mode_in[e_in], identity, interp_in, 394eaf62fffSJeremy L Thompson &grad_in[d_in*num_qpts*num_nodes], &b); 395eaf62fffSJeremy L Thompson // Each component 396eaf62fffSJeremy L Thompson for (CeedInt c_out=0; c_out<num_comp; c_out++) 397eaf62fffSJeremy L Thompson // Each qpoint/node pair 398eaf62fffSJeremy L Thompson for (CeedInt q=0; q<num_qpts; q++) 399eaf62fffSJeremy L Thompson if (is_pointblock) { 400eaf62fffSJeremy L Thompson // Point Block Diagonal 401eaf62fffSJeremy L Thompson for (CeedInt c_in=0; c_in<num_comp; c_in++) { 402eaf62fffSJeremy L Thompson const CeedScalar qf_value = 403eaf62fffSJeremy L Thompson assembled_qf_array[q*layout[0] + (((e_in*num_comp+c_in)* 404eaf62fffSJeremy L Thompson num_eval_mode_out+e_out)*num_comp+c_out)*layout[1] + e*layout[2]]; 405eaf62fffSJeremy L Thompson for (CeedInt n=0; n<num_nodes; n++) 406eaf62fffSJeremy L Thompson elem_diag_array[((e*num_comp+c_out)*num_comp+c_in)*num_nodes+n] += 407eaf62fffSJeremy L Thompson bt[q*num_nodes+n] * qf_value * b[q*num_nodes+n]; 408eaf62fffSJeremy L Thompson } 409eaf62fffSJeremy L Thompson } else { 410eaf62fffSJeremy L Thompson // Diagonal Only 411eaf62fffSJeremy L Thompson const CeedScalar qf_value = 412eaf62fffSJeremy L Thompson assembled_qf_array[q*layout[0] + (((e_in*num_comp+c_out)* 413eaf62fffSJeremy L Thompson num_eval_mode_out+e_out)*num_comp+c_out)*layout[1] + e*layout[2]]; 414eaf62fffSJeremy L Thompson for (CeedInt n=0; n<num_nodes; n++) 415eaf62fffSJeremy L Thompson elem_diag_array[(e*num_comp+c_out)*num_nodes+n] += 416eaf62fffSJeremy L Thompson bt[q*num_nodes+n] * qf_value * b[q*num_nodes+n]; 417eaf62fffSJeremy L Thompson } 418eaf62fffSJeremy L Thompson } 419eaf62fffSJeremy L Thompson } 420eaf62fffSJeremy L Thompson } 421eaf62fffSJeremy L Thompson ierr = CeedVectorRestoreArray(elem_diag, &elem_diag_array); CeedChk(ierr); 4229c774eddSJeremy L Thompson ierr = CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array); 4239c774eddSJeremy L Thompson CeedChk(ierr); 424eaf62fffSJeremy L Thompson 425eaf62fffSJeremy L Thompson // Assemble local operator diagonal 426eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, 427eaf62fffSJeremy L Thompson assembled, request); CeedChk(ierr); 428eaf62fffSJeremy L Thompson 429eaf62fffSJeremy L Thompson // Cleanup 430eaf62fffSJeremy L Thompson if (is_pointblock) { 431eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionDestroy(&diag_rstr); CeedChk(ierr); 432eaf62fffSJeremy L Thompson } 433eaf62fffSJeremy L Thompson ierr = CeedVectorDestroy(&assembled_qf); CeedChk(ierr); 434eaf62fffSJeremy L Thompson ierr = CeedVectorDestroy(&elem_diag); CeedChk(ierr); 435eaf62fffSJeremy L Thompson ierr = CeedFree(&identity); CeedChk(ierr); 436eaf62fffSJeremy L Thompson 437eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 438eaf62fffSJeremy L Thompson } 439eaf62fffSJeremy L Thompson 440eaf62fffSJeremy L Thompson /** 441eaf62fffSJeremy L Thompson @brief Core logic for assembling composite operator diagonal 442eaf62fffSJeremy L Thompson 443eaf62fffSJeremy L Thompson @param[in] op CeedOperator to assemble point block diagonal 444eaf62fffSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else 445eaf62fffSJeremy L Thompson CEED_REQUEST_IMMEDIATE 446eaf62fffSJeremy L Thompson @param[in] is_pointblock Boolean flag to assemble diagonal or point block diagonal 447eaf62fffSJeremy L Thompson @param[out] assembled CeedVector to store assembled diagonal 448eaf62fffSJeremy L Thompson 449eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 450eaf62fffSJeremy L Thompson 451eaf62fffSJeremy L Thompson @ref Developer 452eaf62fffSJeremy L Thompson **/ 453eaf62fffSJeremy L Thompson static inline int CeedCompositeOperatorLinearAssembleAddDiagonal( 454eaf62fffSJeremy L Thompson CeedOperator op, CeedRequest *request, const bool is_pointblock, 455eaf62fffSJeremy L Thompson CeedVector assembled) { 456eaf62fffSJeremy L Thompson int ierr; 457eaf62fffSJeremy L Thompson CeedInt num_sub; 458eaf62fffSJeremy L Thompson CeedOperator *suboperators; 459eaf62fffSJeremy L Thompson ierr = CeedOperatorGetNumSub(op, &num_sub); CeedChk(ierr); 460eaf62fffSJeremy L Thompson ierr = CeedOperatorGetSubList(op, &suboperators); CeedChk(ierr); 461eaf62fffSJeremy L Thompson for (CeedInt i = 0; i < num_sub; i++) { 4626aa95790SJeremy L Thompson if (is_pointblock) { 4636aa95790SJeremy L Thompson ierr = CeedOperatorLinearAssembleAddPointBlockDiagonal(suboperators[i], 4646aa95790SJeremy L Thompson assembled, request); CeedChk(ierr); 4656aa95790SJeremy L Thompson } else { 4666aa95790SJeremy L Thompson ierr = CeedOperatorLinearAssembleAddDiagonal(suboperators[i], assembled, 4676aa95790SJeremy L Thompson request); CeedChk(ierr); 4686aa95790SJeremy L Thompson } 469eaf62fffSJeremy L Thompson } 470eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 471eaf62fffSJeremy L Thompson } 472eaf62fffSJeremy L Thompson 473eaf62fffSJeremy L Thompson /** 474eaf62fffSJeremy L Thompson @brief Build nonzero pattern for non-composite operator 475eaf62fffSJeremy L Thompson 476eaf62fffSJeremy L Thompson Users should generally use CeedOperatorLinearAssembleSymbolic() 477eaf62fffSJeremy L Thompson 478eaf62fffSJeremy L Thompson @param[in] op CeedOperator to assemble nonzero pattern 479eaf62fffSJeremy L Thompson @param[in] offset Offset for number of entries 480eaf62fffSJeremy L Thompson @param[out] rows Row number for each entry 481eaf62fffSJeremy L Thompson @param[out] cols Column number for each entry 482eaf62fffSJeremy L Thompson 483eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 484eaf62fffSJeremy L Thompson 485eaf62fffSJeremy L Thompson @ref Developer 486eaf62fffSJeremy L Thompson **/ 487eaf62fffSJeremy L Thompson static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, 488eaf62fffSJeremy L Thompson CeedInt *rows, CeedInt *cols) { 489eaf62fffSJeremy L Thompson int ierr; 490eaf62fffSJeremy L Thompson Ceed ceed = op->ceed; 491f04ea552SJeremy L Thompson if (op->is_composite) 492eaf62fffSJeremy L Thompson // LCOV_EXCL_START 493eaf62fffSJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 494eaf62fffSJeremy L Thompson "Composite operator not supported"); 495eaf62fffSJeremy L Thompson // LCOV_EXCL_STOP 496eaf62fffSJeremy L Thompson 497c9366a6bSJeremy L Thompson CeedSize num_nodes; 498c9366a6bSJeremy L Thompson ierr = CeedOperatorGetActiveVectorLengths(op, &num_nodes, NULL); CeedChk(ierr); 499eaf62fffSJeremy L Thompson CeedElemRestriction rstr_in; 500eaf62fffSJeremy L Thompson ierr = CeedOperatorGetActiveElemRestriction(op, &rstr_in); CeedChk(ierr); 501e79b91d9SJeremy L Thompson CeedInt num_elem, elem_size, num_comp; 502eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetNumElements(rstr_in, &num_elem); CeedChk(ierr); 503eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetElementSize(rstr_in, &elem_size); CeedChk(ierr); 504eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetNumComponents(rstr_in, &num_comp); CeedChk(ierr); 505eaf62fffSJeremy L Thompson CeedInt layout_er[3]; 506eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetELayout(rstr_in, &layout_er); CeedChk(ierr); 507eaf62fffSJeremy L Thompson 508eaf62fffSJeremy L Thompson CeedInt local_num_entries = elem_size*num_comp * elem_size*num_comp * num_elem; 509eaf62fffSJeremy L Thompson 510eaf62fffSJeremy L Thompson // Determine elem_dof relation 511eaf62fffSJeremy L Thompson CeedVector index_vec; 512eaf62fffSJeremy L Thompson ierr = CeedVectorCreate(ceed, num_nodes, &index_vec); CeedChk(ierr); 513eaf62fffSJeremy L Thompson CeedScalar *array; 5149c774eddSJeremy L Thompson ierr = CeedVectorGetArrayWrite(index_vec, CEED_MEM_HOST, &array); CeedChk(ierr); 515ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < num_nodes; i++) array[i] = i; 516eaf62fffSJeremy L Thompson ierr = CeedVectorRestoreArray(index_vec, &array); CeedChk(ierr); 517eaf62fffSJeremy L Thompson CeedVector elem_dof; 518eaf62fffSJeremy L Thompson ierr = CeedVectorCreate(ceed, num_elem * elem_size * num_comp, &elem_dof); 519eaf62fffSJeremy L Thompson CeedChk(ierr); 520eaf62fffSJeremy L Thompson ierr = CeedVectorSetValue(elem_dof, 0.0); CeedChk(ierr); 521eaf62fffSJeremy L Thompson CeedElemRestrictionApply(rstr_in, CEED_NOTRANSPOSE, index_vec, 522eaf62fffSJeremy L Thompson elem_dof, CEED_REQUEST_IMMEDIATE); CeedChk(ierr); 523eaf62fffSJeremy L Thompson const CeedScalar *elem_dof_a; 524eaf62fffSJeremy L Thompson ierr = CeedVectorGetArrayRead(elem_dof, CEED_MEM_HOST, &elem_dof_a); 525eaf62fffSJeremy L Thompson CeedChk(ierr); 526eaf62fffSJeremy L Thompson ierr = CeedVectorDestroy(&index_vec); CeedChk(ierr); 527eaf62fffSJeremy L Thompson 528eaf62fffSJeremy L Thompson // Determine i, j locations for element matrices 529eaf62fffSJeremy L Thompson CeedInt count = 0; 530ed9e99e6SJeremy L Thompson for (CeedInt e = 0; e < num_elem; e++) { 531ed9e99e6SJeremy L Thompson for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) { 532ed9e99e6SJeremy L Thompson for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) { 533ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < elem_size; i++) { 534ed9e99e6SJeremy L Thompson for (CeedInt j = 0; j < elem_size; j++) { 535ed9e99e6SJeremy L Thompson const CeedInt elem_dof_index_row = i*layout_er[0] + 536eaf62fffSJeremy L Thompson (comp_out)*layout_er[1] + e*layout_er[2]; 537ed9e99e6SJeremy L Thompson const CeedInt elem_dof_index_col = j*layout_er[0] + 538ed9e99e6SJeremy L Thompson comp_in*layout_er[1] + e*layout_er[2]; 539eaf62fffSJeremy L Thompson 540eaf62fffSJeremy L Thompson const CeedInt row = elem_dof_a[elem_dof_index_row]; 541eaf62fffSJeremy L Thompson const CeedInt col = elem_dof_a[elem_dof_index_col]; 542eaf62fffSJeremy L Thompson 543eaf62fffSJeremy L Thompson rows[offset + count] = row; 544eaf62fffSJeremy L Thompson cols[offset + count] = col; 545eaf62fffSJeremy L Thompson count++; 546eaf62fffSJeremy L Thompson } 547eaf62fffSJeremy L Thompson } 548eaf62fffSJeremy L Thompson } 549eaf62fffSJeremy L Thompson } 550eaf62fffSJeremy L Thompson } 551eaf62fffSJeremy L Thompson if (count != local_num_entries) 552eaf62fffSJeremy L Thompson // LCOV_EXCL_START 553eaf62fffSJeremy L Thompson return CeedError(ceed, CEED_ERROR_MAJOR, "Error computing assembled entries"); 554eaf62fffSJeremy L Thompson // LCOV_EXCL_STOP 555eaf62fffSJeremy L Thompson ierr = CeedVectorRestoreArrayRead(elem_dof, &elem_dof_a); CeedChk(ierr); 556eaf62fffSJeremy L Thompson ierr = CeedVectorDestroy(&elem_dof); CeedChk(ierr); 557eaf62fffSJeremy L Thompson 558eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 559eaf62fffSJeremy L Thompson } 560eaf62fffSJeremy L Thompson 561eaf62fffSJeremy L Thompson /** 562eaf62fffSJeremy L Thompson @brief Assemble nonzero entries for non-composite operator 563eaf62fffSJeremy L Thompson 564eaf62fffSJeremy L Thompson Users should generally use CeedOperatorLinearAssemble() 565eaf62fffSJeremy L Thompson 566eaf62fffSJeremy L Thompson @param[in] op CeedOperator to assemble 56752b3e6a7SJed Brown @param[in] offset Offest for number of entries 568eaf62fffSJeremy L Thompson @param[out] values Values to assemble into matrix 569eaf62fffSJeremy L Thompson 570eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 571eaf62fffSJeremy L Thompson 572eaf62fffSJeremy L Thompson @ref Developer 573eaf62fffSJeremy L Thompson **/ 574eaf62fffSJeremy L Thompson static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, 575eaf62fffSJeremy L Thompson CeedVector values) { 576eaf62fffSJeremy L Thompson int ierr; 577eaf62fffSJeremy L Thompson Ceed ceed = op->ceed; 578f04ea552SJeremy L Thompson if (op->is_composite) 579eaf62fffSJeremy L Thompson // LCOV_EXCL_START 580eaf62fffSJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 581eaf62fffSJeremy L Thompson "Composite operator not supported"); 582eaf62fffSJeremy L Thompson // LCOV_EXCL_STOP 58352b3e6a7SJed Brown if (op->num_elem == 0) return CEED_ERROR_SUCCESS; 584eaf62fffSJeremy L Thompson 585cefa2673SJeremy L Thompson if (op->LinearAssembleSingle) { 586cefa2673SJeremy L Thompson // Backend version 587cefa2673SJeremy L Thompson ierr = op->LinearAssembleSingle(op, offset, values); CeedChk(ierr); 588cefa2673SJeremy L Thompson return CEED_ERROR_SUCCESS; 589cefa2673SJeremy L Thompson } else { 590cefa2673SJeremy L Thompson // Operator fallback 591cefa2673SJeremy L Thompson CeedOperator op_fallback; 592cefa2673SJeremy L Thompson 593cefa2673SJeremy L Thompson ierr = CeedOperatorGetFallback(op, &op_fallback); CeedChk(ierr); 594cefa2673SJeremy L Thompson if (op_fallback) { 595cefa2673SJeremy L Thompson ierr = CeedSingleOperatorAssemble(op_fallback, offset, values); 596cefa2673SJeremy L Thompson CeedChk(ierr); 597cefa2673SJeremy L Thompson return CEED_ERROR_SUCCESS; 598cefa2673SJeremy L Thompson } 599cefa2673SJeremy L Thompson } 600cefa2673SJeremy L Thompson 601eaf62fffSJeremy L Thompson // Assemble QFunction 602eaf62fffSJeremy L Thompson CeedQFunction qf; 603eaf62fffSJeremy L Thompson ierr = CeedOperatorGetQFunction(op, &qf); CeedChk(ierr); 604eaf62fffSJeremy L Thompson CeedVector assembled_qf; 605eaf62fffSJeremy L Thompson CeedElemRestriction rstr_q; 60670a7ffb3SJeremy L Thompson ierr = CeedOperatorLinearAssembleQFunctionBuildOrUpdate( 607eaf62fffSJeremy L Thompson op, &assembled_qf, &rstr_q, CEED_REQUEST_IMMEDIATE); CeedChk(ierr); 6081f9221feSJeremy L Thompson CeedSize qf_length; 609eaf62fffSJeremy L Thompson ierr = CeedVectorGetLength(assembled_qf, &qf_length); CeedChk(ierr); 610eaf62fffSJeremy L Thompson 6117e7773b5SJeremy L Thompson CeedInt num_input_fields, num_output_fields; 612eaf62fffSJeremy L Thompson CeedOperatorField *input_fields; 613eaf62fffSJeremy L Thompson CeedOperatorField *output_fields; 6147e7773b5SJeremy L Thompson ierr = CeedOperatorGetFields(op, &num_input_fields, &input_fields, 6157e7773b5SJeremy L Thompson &num_output_fields, &output_fields); CeedChk(ierr); 616eaf62fffSJeremy L Thompson 617ed9e99e6SJeremy L Thompson // Get assembly data 618ed9e99e6SJeremy L Thompson CeedOperatorAssemblyData data; 619ed9e99e6SJeremy L Thompson ierr = CeedOperatorGetOperatorAssemblyData(op, &data); CeedChk(ierr); 620ed9e99e6SJeremy L Thompson const CeedEvalMode *eval_mode_in, *eval_mode_out; 621ed9e99e6SJeremy L Thompson CeedInt num_eval_mode_in, num_eval_mode_out; 622ed9e99e6SJeremy L Thompson ierr = CeedOperatorAssemblyDataGetEvalModes(data, &num_eval_mode_in, 623ed9e99e6SJeremy L Thompson &eval_mode_in, &num_eval_mode_out, &eval_mode_out); CeedChk(ierr); 624ed9e99e6SJeremy L Thompson CeedBasis basis_in, basis_out; 625ed9e99e6SJeremy L Thompson ierr = CeedOperatorAssemblyDataGetBases(data, &basis_in, NULL, &basis_out, 626ed9e99e6SJeremy L Thompson NULL); CeedChk(ierr); 627eaf62fffSJeremy L Thompson 628eaf62fffSJeremy L Thompson if (num_eval_mode_in == 0 || num_eval_mode_out == 0) 629eaf62fffSJeremy L Thompson // LCOV_EXCL_START 630eaf62fffSJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 631eaf62fffSJeremy L Thompson "Cannot assemble operator with out inputs/outputs"); 632eaf62fffSJeremy L Thompson // LCOV_EXCL_STOP 633eaf62fffSJeremy L Thompson 634ed9e99e6SJeremy L Thompson CeedElemRestriction active_rstr; 635eaf62fffSJeremy L Thompson CeedInt num_elem, elem_size, num_qpts, num_comp; 636ed9e99e6SJeremy L Thompson ierr = CeedOperatorGetActiveElemRestriction(op, &active_rstr); CeedChk(ierr); 637ed9e99e6SJeremy L Thompson ierr = CeedElemRestrictionGetNumElements(active_rstr, &num_elem); CeedChk(ierr); 638ed9e99e6SJeremy L Thompson ierr = CeedElemRestrictionGetElementSize(active_rstr, &elem_size); 639ed9e99e6SJeremy L Thompson CeedChk(ierr); 640ed9e99e6SJeremy L Thompson ierr = CeedElemRestrictionGetNumComponents(active_rstr, &num_comp); 641ed9e99e6SJeremy L Thompson CeedChk(ierr); 642eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts); CeedChk(ierr); 643eaf62fffSJeremy L Thompson 644eaf62fffSJeremy L Thompson CeedInt local_num_entries = elem_size*num_comp * elem_size*num_comp * num_elem; 645eaf62fffSJeremy L Thompson 646eaf62fffSJeremy L Thompson // loop over elements and put in data structure 647eaf62fffSJeremy L Thompson const CeedScalar *interp_in, *grad_in; 648eaf62fffSJeremy L Thompson ierr = CeedBasisGetInterp(basis_in, &interp_in); CeedChk(ierr); 649eaf62fffSJeremy L Thompson ierr = CeedBasisGetGrad(basis_in, &grad_in); CeedChk(ierr); 650eaf62fffSJeremy L Thompson 651eaf62fffSJeremy L Thompson const CeedScalar *assembled_qf_array; 652eaf62fffSJeremy L Thompson ierr = CeedVectorGetArrayRead(assembled_qf, CEED_MEM_HOST, &assembled_qf_array); 653eaf62fffSJeremy L Thompson CeedChk(ierr); 654eaf62fffSJeremy L Thompson 655eaf62fffSJeremy L Thompson CeedInt layout_qf[3]; 656eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetELayout(rstr_q, &layout_qf); CeedChk(ierr); 657eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionDestroy(&rstr_q); CeedChk(ierr); 658eaf62fffSJeremy L Thompson 659eaf62fffSJeremy L Thompson // we store B_mat_in, B_mat_out, BTD, elem_mat in row-major order 660ed9e99e6SJeremy L Thompson const CeedScalar *B_mat_in, *B_mat_out; 661ed9e99e6SJeremy L Thompson ierr = CeedOperatorAssemblyDataGetBases(data, NULL, &B_mat_in, NULL, 662ed9e99e6SJeremy L Thompson &B_mat_out); CeedChk(ierr); 663ed9e99e6SJeremy L Thompson CeedScalar BTD_mat[elem_size * num_qpts*num_eval_mode_in]; 664eaf62fffSJeremy L Thompson CeedScalar elem_mat[elem_size * elem_size]; 66592ae7e47SJeremy L Thompson CeedInt count = 0; 666eaf62fffSJeremy L Thompson CeedScalar *vals; 6679c774eddSJeremy L Thompson ierr = CeedVectorGetArrayWrite(values, CEED_MEM_HOST, &vals); CeedChk(ierr); 668ed9e99e6SJeremy L Thompson for (CeedInt e = 0; e < num_elem; e++) { 669ed9e99e6SJeremy L Thompson for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) { 670ed9e99e6SJeremy L Thompson for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) { 671ed9e99e6SJeremy L Thompson // Compute B^T*D 672ed9e99e6SJeremy L Thompson for (CeedInt n = 0; n < elem_size; n++) { 673ed9e99e6SJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 674ed9e99e6SJeremy L Thompson for (CeedInt e_in = 0; e_in < num_eval_mode_in; e_in++) { 675ed9e99e6SJeremy L Thompson const CeedInt btd_index = n*(num_qpts*num_eval_mode_in) + 676ed9e99e6SJeremy L Thompson (num_eval_mode_in*q + e_in); 677067fd99fSJeremy L Thompson CeedScalar sum = 0.0; 678067fd99fSJeremy L Thompson for (CeedInt e_out = 0; e_out < num_eval_mode_out; e_out++) { 679ed9e99e6SJeremy L Thompson const CeedInt b_out_index = (num_eval_mode_out*q + e_out)*elem_size + n; 680ed9e99e6SJeremy L Thompson const CeedInt eval_mode_index = ((e_in*num_comp+comp_in)*num_eval_mode_out 681ed9e99e6SJeremy L Thompson +e_out)*num_comp + comp_out; 682ed9e99e6SJeremy L Thompson const CeedInt qf_index = q*layout_qf[0] + eval_mode_index*layout_qf[1] + 683ed9e99e6SJeremy L Thompson e*layout_qf[2]; 684067fd99fSJeremy L Thompson sum += B_mat_out[b_out_index] * assembled_qf_array[qf_index]; 685eaf62fffSJeremy L Thompson } 686067fd99fSJeremy L Thompson BTD_mat[btd_index] = sum; 687ed9e99e6SJeremy L Thompson } 688ed9e99e6SJeremy L Thompson } 689eaf62fffSJeremy L Thompson } 690eaf62fffSJeremy L Thompson // form element matrix itself (for each block component) 691ed9e99e6SJeremy L Thompson ierr = CeedMatrixMatrixMultiply(ceed, BTD_mat, B_mat_in, elem_mat, elem_size, 692eaf62fffSJeremy L Thompson elem_size, num_qpts*num_eval_mode_in); CeedChk(ierr); 693eaf62fffSJeremy L Thompson 694eaf62fffSJeremy L Thompson // put element matrix in coordinate data structure 695ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < elem_size; i++) { 696ed9e99e6SJeremy L Thompson for (CeedInt j = 0; j < elem_size; j++) { 697eaf62fffSJeremy L Thompson vals[offset + count] = elem_mat[i*elem_size + j]; 698eaf62fffSJeremy L Thompson count++; 699eaf62fffSJeremy L Thompson } 700eaf62fffSJeremy L Thompson } 701eaf62fffSJeremy L Thompson } 702eaf62fffSJeremy L Thompson } 703eaf62fffSJeremy L Thompson } 704eaf62fffSJeremy L Thompson if (count != local_num_entries) 705eaf62fffSJeremy L Thompson // LCOV_EXCL_START 706eaf62fffSJeremy L Thompson return CeedError(ceed, CEED_ERROR_MAJOR, "Error computing entries"); 707eaf62fffSJeremy L Thompson // LCOV_EXCL_STOP 708eaf62fffSJeremy L Thompson ierr = CeedVectorRestoreArray(values, &vals); CeedChk(ierr); 709eaf62fffSJeremy L Thompson 710eaf62fffSJeremy L Thompson ierr = CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array); 711eaf62fffSJeremy L Thompson CeedChk(ierr); 712eaf62fffSJeremy L Thompson ierr = CeedVectorDestroy(&assembled_qf); CeedChk(ierr); 713eaf62fffSJeremy L Thompson 714eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 715eaf62fffSJeremy L Thompson } 716eaf62fffSJeremy L Thompson 717eaf62fffSJeremy L Thompson /** 718eaf62fffSJeremy L Thompson @brief Count number of entries for assembled CeedOperator 719eaf62fffSJeremy L Thompson 720eaf62fffSJeremy L Thompson @param[in] op CeedOperator to assemble 721eaf62fffSJeremy L Thompson @param[out] num_entries Number of entries in assembled representation 722eaf62fffSJeremy L Thompson 723eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 724eaf62fffSJeremy L Thompson 725eaf62fffSJeremy L Thompson @ref Utility 726eaf62fffSJeremy L Thompson **/ 727eaf62fffSJeremy L Thompson static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, 728eaf62fffSJeremy L Thompson CeedInt *num_entries) { 729eaf62fffSJeremy L Thompson int ierr; 730eaf62fffSJeremy L Thompson CeedElemRestriction rstr; 731eaf62fffSJeremy L Thompson CeedInt num_elem, elem_size, num_comp; 732eaf62fffSJeremy L Thompson 733f04ea552SJeremy L Thompson if (op->is_composite) 734eaf62fffSJeremy L Thompson // LCOV_EXCL_START 735eaf62fffSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, 736eaf62fffSJeremy L Thompson "Composite operator not supported"); 737eaf62fffSJeremy L Thompson // LCOV_EXCL_STOP 738eaf62fffSJeremy L Thompson ierr = CeedOperatorGetActiveElemRestriction(op, &rstr); CeedChk(ierr); 739eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetNumElements(rstr, &num_elem); CeedChk(ierr); 740eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetElementSize(rstr, &elem_size); CeedChk(ierr); 741eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetNumComponents(rstr, &num_comp); CeedChk(ierr); 742eaf62fffSJeremy L Thompson *num_entries = elem_size*num_comp * elem_size*num_comp * num_elem; 743eaf62fffSJeremy L Thompson 744eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 745eaf62fffSJeremy L Thompson } 746eaf62fffSJeremy L Thompson 747eaf62fffSJeremy L Thompson /** 748eaf62fffSJeremy L Thompson @brief Common code for creating a multigrid coarse operator and level 749eaf62fffSJeremy L Thompson transfer operators for a CeedOperator 750eaf62fffSJeremy L Thompson 751eaf62fffSJeremy L Thompson @param[in] op_fine Fine grid operator 752eaf62fffSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter 753eaf62fffSJeremy L Thompson @param[in] rstr_coarse Coarse grid restriction 754eaf62fffSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector basis 755eaf62fffSJeremy L Thompson @param[in] basis_c_to_f Basis for coarse to fine interpolation 756eaf62fffSJeremy L Thompson @param[out] op_coarse Coarse grid operator 757eaf62fffSJeremy L Thompson @param[out] op_prolong Coarse to fine operator 758eaf62fffSJeremy L Thompson @param[out] op_restrict Fine to coarse operator 759eaf62fffSJeremy L Thompson 760eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 761eaf62fffSJeremy L Thompson 762eaf62fffSJeremy L Thompson @ref Developer 763eaf62fffSJeremy L Thompson **/ 764eaf62fffSJeremy L Thompson static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, 765eaf62fffSJeremy L Thompson CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 766eaf62fffSJeremy L Thompson CeedBasis basis_c_to_f, CeedOperator *op_coarse, CeedOperator *op_prolong, 767eaf62fffSJeremy L Thompson CeedOperator *op_restrict) { 768eaf62fffSJeremy L Thompson int ierr; 769eaf62fffSJeremy L Thompson Ceed ceed; 770eaf62fffSJeremy L Thompson ierr = CeedOperatorGetCeed(op_fine, &ceed); CeedChk(ierr); 771eaf62fffSJeremy L Thompson 772eaf62fffSJeremy L Thompson // Check for composite operator 773eaf62fffSJeremy L Thompson bool is_composite; 774eaf62fffSJeremy L Thompson ierr = CeedOperatorIsComposite(op_fine, &is_composite); CeedChk(ierr); 775eaf62fffSJeremy L Thompson if (is_composite) 776eaf62fffSJeremy L Thompson // LCOV_EXCL_START 777eaf62fffSJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 778eaf62fffSJeremy L Thompson "Automatic multigrid setup for composite operators not supported"); 779eaf62fffSJeremy L Thompson // LCOV_EXCL_STOP 780eaf62fffSJeremy L Thompson 781eaf62fffSJeremy L Thompson // Coarse Grid 782eaf62fffSJeremy L Thompson ierr = CeedOperatorCreate(ceed, op_fine->qf, op_fine->dqf, op_fine->dqfT, 783eaf62fffSJeremy L Thompson op_coarse); CeedChk(ierr); 784eaf62fffSJeremy L Thompson CeedElemRestriction rstr_fine = NULL; 785eaf62fffSJeremy L Thompson // -- Clone input fields 78692ae7e47SJeremy L Thompson for (CeedInt i = 0; i < op_fine->qf->num_input_fields; i++) { 787eaf62fffSJeremy L Thompson if (op_fine->input_fields[i]->vec == CEED_VECTOR_ACTIVE) { 788eaf62fffSJeremy L Thompson rstr_fine = op_fine->input_fields[i]->elem_restr; 789eaf62fffSJeremy L Thompson ierr = CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, 790eaf62fffSJeremy L Thompson rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE); 791eaf62fffSJeremy L Thompson CeedChk(ierr); 792eaf62fffSJeremy L Thompson } else { 793eaf62fffSJeremy L Thompson ierr = CeedOperatorSetField(*op_coarse, op_fine->input_fields[i]->field_name, 794eaf62fffSJeremy L Thompson op_fine->input_fields[i]->elem_restr, 795eaf62fffSJeremy L Thompson op_fine->input_fields[i]->basis, 796eaf62fffSJeremy L Thompson op_fine->input_fields[i]->vec); CeedChk(ierr); 797eaf62fffSJeremy L Thompson } 798eaf62fffSJeremy L Thompson } 799eaf62fffSJeremy L Thompson // -- Clone output fields 80092ae7e47SJeremy L Thompson for (CeedInt i = 0; i < op_fine->qf->num_output_fields; i++) { 801eaf62fffSJeremy L Thompson if (op_fine->output_fields[i]->vec == CEED_VECTOR_ACTIVE) { 802eaf62fffSJeremy L Thompson ierr = CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, 803eaf62fffSJeremy L Thompson rstr_coarse, basis_coarse, CEED_VECTOR_ACTIVE); 804eaf62fffSJeremy L Thompson CeedChk(ierr); 805eaf62fffSJeremy L Thompson } else { 806eaf62fffSJeremy L Thompson ierr = CeedOperatorSetField(*op_coarse, op_fine->output_fields[i]->field_name, 807eaf62fffSJeremy L Thompson op_fine->output_fields[i]->elem_restr, 808eaf62fffSJeremy L Thompson op_fine->output_fields[i]->basis, 809eaf62fffSJeremy L Thompson op_fine->output_fields[i]->vec); CeedChk(ierr); 810eaf62fffSJeremy L Thompson } 811eaf62fffSJeremy L Thompson } 812af99e877SJeremy L Thompson // -- Clone QFunctionAssemblyData 813af99e877SJeremy L Thompson ierr = CeedQFunctionAssemblyDataReferenceCopy(op_fine->qf_assembled, 814af99e877SJeremy L Thompson &(*op_coarse)->qf_assembled); CeedChk(ierr); 815eaf62fffSJeremy L Thompson 816eaf62fffSJeremy L Thompson // Multiplicity vector 817eaf62fffSJeremy L Thompson CeedVector mult_vec, mult_e_vec; 818eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionCreateVector(rstr_fine, &mult_vec, &mult_e_vec); 819eaf62fffSJeremy L Thompson CeedChk(ierr); 820eaf62fffSJeremy L Thompson ierr = CeedVectorSetValue(mult_e_vec, 0.0); CeedChk(ierr); 821eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionApply(rstr_fine, CEED_NOTRANSPOSE, p_mult_fine, 822eaf62fffSJeremy L Thompson mult_e_vec, CEED_REQUEST_IMMEDIATE); CeedChk(ierr); 823eaf62fffSJeremy L Thompson ierr = CeedVectorSetValue(mult_vec, 0.0); CeedChk(ierr); 824eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionApply(rstr_fine, CEED_TRANSPOSE, mult_e_vec, mult_vec, 825eaf62fffSJeremy L Thompson CEED_REQUEST_IMMEDIATE); CeedChk(ierr); 826eaf62fffSJeremy L Thompson ierr = CeedVectorDestroy(&mult_e_vec); CeedChk(ierr); 827eaf62fffSJeremy L Thompson ierr = CeedVectorReciprocal(mult_vec); CeedChk(ierr); 828eaf62fffSJeremy L Thompson 829eaf62fffSJeremy L Thompson // Restriction 830eaf62fffSJeremy L Thompson CeedInt num_comp; 831eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumComponents(basis_coarse, &num_comp); CeedChk(ierr); 832eaf62fffSJeremy L Thompson CeedQFunction qf_restrict; 833eaf62fffSJeremy L Thompson ierr = CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_restrict); 834eaf62fffSJeremy L Thompson CeedChk(ierr); 835eaf62fffSJeremy L Thompson CeedInt *num_comp_r_data; 836eaf62fffSJeremy L Thompson ierr = CeedCalloc(1, &num_comp_r_data); CeedChk(ierr); 837eaf62fffSJeremy L Thompson num_comp_r_data[0] = num_comp; 838eaf62fffSJeremy L Thompson CeedQFunctionContext ctx_r; 839eaf62fffSJeremy L Thompson ierr = CeedQFunctionContextCreate(ceed, &ctx_r); CeedChk(ierr); 840eaf62fffSJeremy L Thompson ierr = CeedQFunctionContextSetData(ctx_r, CEED_MEM_HOST, CEED_OWN_POINTER, 841eaf62fffSJeremy L Thompson sizeof(*num_comp_r_data), num_comp_r_data); 842eaf62fffSJeremy L Thompson CeedChk(ierr); 843eaf62fffSJeremy L Thompson ierr = CeedQFunctionSetContext(qf_restrict, ctx_r); CeedChk(ierr); 844eaf62fffSJeremy L Thompson ierr = CeedQFunctionContextDestroy(&ctx_r); CeedChk(ierr); 845eaf62fffSJeremy L Thompson ierr = CeedQFunctionAddInput(qf_restrict, "input", num_comp, CEED_EVAL_NONE); 846eaf62fffSJeremy L Thompson CeedChk(ierr); 847eaf62fffSJeremy L Thompson ierr = CeedQFunctionAddInput(qf_restrict, "scale", num_comp, CEED_EVAL_NONE); 848eaf62fffSJeremy L Thompson CeedChk(ierr); 849eaf62fffSJeremy L Thompson ierr = CeedQFunctionAddOutput(qf_restrict, "output", num_comp, 850eaf62fffSJeremy L Thompson CEED_EVAL_INTERP); CeedChk(ierr); 8516e15d496SJeremy L Thompson ierr = CeedQFunctionSetUserFlopsEstimate(qf_restrict, num_comp); CeedChk(ierr); 852eaf62fffSJeremy L Thompson 853eaf62fffSJeremy L Thompson ierr = CeedOperatorCreate(ceed, qf_restrict, CEED_QFUNCTION_NONE, 854eaf62fffSJeremy L Thompson CEED_QFUNCTION_NONE, op_restrict); 855eaf62fffSJeremy L Thompson CeedChk(ierr); 856eaf62fffSJeremy L Thompson ierr = CeedOperatorSetField(*op_restrict, "input", rstr_fine, 857eaf62fffSJeremy L Thompson CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 858eaf62fffSJeremy L Thompson CeedChk(ierr); 859eaf62fffSJeremy L Thompson ierr = CeedOperatorSetField(*op_restrict, "scale", rstr_fine, 860eaf62fffSJeremy L Thompson CEED_BASIS_COLLOCATED, mult_vec); 861eaf62fffSJeremy L Thompson CeedChk(ierr); 862eaf62fffSJeremy L Thompson ierr = CeedOperatorSetField(*op_restrict, "output", rstr_coarse, basis_c_to_f, 863eaf62fffSJeremy L Thompson CEED_VECTOR_ACTIVE); CeedChk(ierr); 864eaf62fffSJeremy L Thompson 865eaf62fffSJeremy L Thompson // Prolongation 866eaf62fffSJeremy L Thompson CeedQFunction qf_prolong; 867eaf62fffSJeremy L Thompson ierr = CeedQFunctionCreateInteriorByName(ceed, "Scale", &qf_prolong); 868eaf62fffSJeremy L Thompson CeedChk(ierr); 869eaf62fffSJeremy L Thompson CeedInt *num_comp_p_data; 870eaf62fffSJeremy L Thompson ierr = CeedCalloc(1, &num_comp_p_data); CeedChk(ierr); 871eaf62fffSJeremy L Thompson num_comp_p_data[0] = num_comp; 872eaf62fffSJeremy L Thompson CeedQFunctionContext ctx_p; 873eaf62fffSJeremy L Thompson ierr = CeedQFunctionContextCreate(ceed, &ctx_p); CeedChk(ierr); 874eaf62fffSJeremy L Thompson ierr = CeedQFunctionContextSetData(ctx_p, CEED_MEM_HOST, CEED_OWN_POINTER, 875eaf62fffSJeremy L Thompson sizeof(*num_comp_p_data), num_comp_p_data); 876eaf62fffSJeremy L Thompson CeedChk(ierr); 877eaf62fffSJeremy L Thompson ierr = CeedQFunctionSetContext(qf_prolong, ctx_p); CeedChk(ierr); 878eaf62fffSJeremy L Thompson ierr = CeedQFunctionContextDestroy(&ctx_p); CeedChk(ierr); 879eaf62fffSJeremy L Thompson ierr = CeedQFunctionAddInput(qf_prolong, "input", num_comp, CEED_EVAL_INTERP); 880eaf62fffSJeremy L Thompson CeedChk(ierr); 881eaf62fffSJeremy L Thompson ierr = CeedQFunctionAddInput(qf_prolong, "scale", num_comp, CEED_EVAL_NONE); 882eaf62fffSJeremy L Thompson CeedChk(ierr); 883eaf62fffSJeremy L Thompson ierr = CeedQFunctionAddOutput(qf_prolong, "output", num_comp, CEED_EVAL_NONE); 884eaf62fffSJeremy L Thompson CeedChk(ierr); 8856e15d496SJeremy L Thompson ierr = CeedQFunctionSetUserFlopsEstimate(qf_prolong, num_comp); CeedChk(ierr); 886eaf62fffSJeremy L Thompson 887eaf62fffSJeremy L Thompson ierr = CeedOperatorCreate(ceed, qf_prolong, CEED_QFUNCTION_NONE, 888eaf62fffSJeremy L Thompson CEED_QFUNCTION_NONE, op_prolong); 889eaf62fffSJeremy L Thompson CeedChk(ierr); 890eaf62fffSJeremy L Thompson ierr = CeedOperatorSetField(*op_prolong, "input", rstr_coarse, basis_c_to_f, 891eaf62fffSJeremy L Thompson CEED_VECTOR_ACTIVE); CeedChk(ierr); 892eaf62fffSJeremy L Thompson ierr = CeedOperatorSetField(*op_prolong, "scale", rstr_fine, 893eaf62fffSJeremy L Thompson CEED_BASIS_COLLOCATED, mult_vec); 894eaf62fffSJeremy L Thompson CeedChk(ierr); 895eaf62fffSJeremy L Thompson ierr = CeedOperatorSetField(*op_prolong, "output", rstr_fine, 896eaf62fffSJeremy L Thompson CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 897eaf62fffSJeremy L Thompson CeedChk(ierr); 898eaf62fffSJeremy L Thompson 899ea6b5821SJeremy L Thompson // Clone name 900ea6b5821SJeremy L Thompson bool has_name = op_fine->name; 901ea6b5821SJeremy L Thompson size_t name_len = op_fine->name ? strlen(op_fine->name) : 0; 902ea6b5821SJeremy L Thompson ierr = CeedOperatorSetName(*op_coarse, op_fine->name); CeedChk(ierr); 903ea6b5821SJeremy L Thompson { 904ea6b5821SJeremy L Thompson char *prolongation_name; 905ea6b5821SJeremy L Thompson ierr = CeedCalloc(18 + name_len, &prolongation_name); CeedChk(ierr); 906ea6b5821SJeremy L Thompson sprintf(prolongation_name, "prolongation%s%s", has_name ? " for " : "", 9073129f025Srezgarshakeri has_name ? op_fine->name : ""); 908ea6b5821SJeremy L Thompson ierr = CeedOperatorSetName(*op_prolong, prolongation_name); CeedChk(ierr); 909ea6b5821SJeremy L Thompson ierr = CeedFree(&prolongation_name); CeedChk(ierr); 910ea6b5821SJeremy L Thompson } 911ea6b5821SJeremy L Thompson { 912ea6b5821SJeremy L Thompson char *restriction_name; 913ea6b5821SJeremy L Thompson ierr = CeedCalloc(17 + name_len, &restriction_name); CeedChk(ierr); 914ea6b5821SJeremy L Thompson sprintf(restriction_name, "restriction%s%s", has_name ? " for " : "", 9153129f025Srezgarshakeri has_name ? op_fine->name : ""); 916ea6b5821SJeremy L Thompson ierr = CeedOperatorSetName(*op_restrict, restriction_name); CeedChk(ierr); 917ea6b5821SJeremy L Thompson ierr = CeedFree(&restriction_name); CeedChk(ierr); 918ea6b5821SJeremy L Thompson } 919ea6b5821SJeremy L Thompson 920eaf62fffSJeremy L Thompson // Cleanup 921eaf62fffSJeremy L Thompson ierr = CeedVectorDestroy(&mult_vec); CeedChk(ierr); 922eaf62fffSJeremy L Thompson ierr = CeedBasisDestroy(&basis_c_to_f); CeedChk(ierr); 923eaf62fffSJeremy L Thompson ierr = CeedQFunctionDestroy(&qf_restrict); CeedChk(ierr); 924eaf62fffSJeremy L Thompson ierr = CeedQFunctionDestroy(&qf_prolong); CeedChk(ierr); 925805fe78eSJeremy L Thompson 926eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 927eaf62fffSJeremy L Thompson } 928eaf62fffSJeremy L Thompson 929eaf62fffSJeremy L Thompson /** 930eaf62fffSJeremy L Thompson @brief Build 1D mass matrix and Laplacian with perturbation 931eaf62fffSJeremy L Thompson 932eaf62fffSJeremy L Thompson @param[in] interp_1d Interpolation matrix in one dimension 933eaf62fffSJeremy L Thompson @param[in] grad_1d Gradient matrix in one dimension 934eaf62fffSJeremy L Thompson @param[in] q_weight_1d Quadrature weights in one dimension 935eaf62fffSJeremy L Thompson @param[in] P_1d Number of basis nodes in one dimension 936eaf62fffSJeremy L Thompson @param[in] Q_1d Number of quadrature points in one dimension 937eaf62fffSJeremy L Thompson @param[in] dim Dimension of basis 938eaf62fffSJeremy L Thompson @param[out] mass Assembled mass matrix in one dimension 939eaf62fffSJeremy L Thompson @param[out] laplace Assembled perturbed Laplacian in one dimension 940eaf62fffSJeremy L Thompson 941eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 942eaf62fffSJeremy L Thompson 943eaf62fffSJeremy L Thompson @ref Developer 944eaf62fffSJeremy L Thompson **/ 945eaf62fffSJeremy L Thompson CeedPragmaOptimizeOff 946eaf62fffSJeremy L Thompson static int CeedBuildMassLaplace(const CeedScalar *interp_1d, 947eaf62fffSJeremy L Thompson const CeedScalar *grad_1d, 948eaf62fffSJeremy L Thompson const CeedScalar *q_weight_1d, CeedInt P_1d, 949eaf62fffSJeremy L Thompson CeedInt Q_1d, CeedInt dim, 950eaf62fffSJeremy L Thompson CeedScalar *mass, CeedScalar *laplace) { 951eaf62fffSJeremy L Thompson 952eaf62fffSJeremy L Thompson for (CeedInt i=0; i<P_1d; i++) 953eaf62fffSJeremy L Thompson for (CeedInt j=0; j<P_1d; j++) { 954eaf62fffSJeremy L Thompson CeedScalar sum = 0.0; 955eaf62fffSJeremy L Thompson for (CeedInt k=0; k<Q_1d; k++) 956eaf62fffSJeremy L Thompson sum += interp_1d[k*P_1d+i]*q_weight_1d[k]*interp_1d[k*P_1d+j]; 957eaf62fffSJeremy L Thompson mass[i+j*P_1d] = sum; 958eaf62fffSJeremy L Thompson } 959eaf62fffSJeremy L Thompson // -- Laplacian 960eaf62fffSJeremy L Thompson for (CeedInt i=0; i<P_1d; i++) 961eaf62fffSJeremy L Thompson for (CeedInt j=0; j<P_1d; j++) { 962eaf62fffSJeremy L Thompson CeedScalar sum = 0.0; 963eaf62fffSJeremy L Thompson for (CeedInt k=0; k<Q_1d; k++) 964eaf62fffSJeremy L Thompson sum += grad_1d[k*P_1d+i]*q_weight_1d[k]*grad_1d[k*P_1d+j]; 965eaf62fffSJeremy L Thompson laplace[i+j*P_1d] = sum; 966eaf62fffSJeremy L Thompson } 967eaf62fffSJeremy L Thompson CeedScalar perturbation = dim>2 ? 1e-6 : 1e-4; 968eaf62fffSJeremy L Thompson for (CeedInt i=0; i<P_1d; i++) 969eaf62fffSJeremy L Thompson laplace[i+P_1d*i] += perturbation; 970eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 971eaf62fffSJeremy L Thompson } 972eaf62fffSJeremy L Thompson CeedPragmaOptimizeOn 973eaf62fffSJeremy L Thompson 974eaf62fffSJeremy L Thompson /// @} 975eaf62fffSJeremy L Thompson 976eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 977480fae85SJeremy L Thompson /// CeedOperator Backend API 978480fae85SJeremy L Thompson /// ---------------------------------------------------------------------------- 979480fae85SJeremy L Thompson /// @addtogroup CeedOperatorBackend 980480fae85SJeremy L Thompson /// @{ 981480fae85SJeremy L Thompson 982480fae85SJeremy L Thompson /** 983480fae85SJeremy L Thompson @brief Create object holding CeedQFunction assembly data for CeedOperator 984480fae85SJeremy L Thompson 985480fae85SJeremy L Thompson @param[in] ceed A Ceed object where the CeedQFunctionAssemblyData will be created 986480fae85SJeremy L Thompson @param[out] data Address of the variable where the newly created 987480fae85SJeremy L Thompson CeedQFunctionAssemblyData will be stored 988480fae85SJeremy L Thompson 989480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 990480fae85SJeremy L Thompson 991480fae85SJeremy L Thompson @ref Backend 992480fae85SJeremy L Thompson **/ 993480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataCreate(Ceed ceed, 994480fae85SJeremy L Thompson CeedQFunctionAssemblyData *data) { 995480fae85SJeremy L Thompson int ierr; 996480fae85SJeremy L Thompson 997480fae85SJeremy L Thompson ierr = CeedCalloc(1, data); CeedChk(ierr); 998480fae85SJeremy L Thompson (*data)->ref_count = 1; 999480fae85SJeremy L Thompson (*data)->ceed = ceed; 1000480fae85SJeremy L Thompson ierr = CeedReference(ceed); CeedChk(ierr); 1001480fae85SJeremy L Thompson 1002480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1003480fae85SJeremy L Thompson } 1004480fae85SJeremy L Thompson 1005480fae85SJeremy L Thompson /** 1006480fae85SJeremy L Thompson @brief Increment the reference counter for a CeedQFunctionAssemblyData 1007480fae85SJeremy L Thompson 1008480fae85SJeremy L Thompson @param data CeedQFunctionAssemblyData to increment the reference counter 1009480fae85SJeremy L Thompson 1010480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1011480fae85SJeremy L Thompson 1012480fae85SJeremy L Thompson @ref Backend 1013480fae85SJeremy L Thompson **/ 1014480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReference(CeedQFunctionAssemblyData data) { 1015480fae85SJeremy L Thompson data->ref_count++; 1016480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1017480fae85SJeremy L Thompson } 1018480fae85SJeremy L Thompson 1019480fae85SJeremy L Thompson /** 1020beecbf24SJeremy L Thompson @brief Set re-use of CeedQFunctionAssemblyData 10218b919e6bSJeremy L Thompson 1022beecbf24SJeremy L Thompson @param data CeedQFunctionAssemblyData to mark for reuse 1023beecbf24SJeremy L Thompson @param reuse_data Boolean flag indicating data re-use 10248b919e6bSJeremy L Thompson 10258b919e6bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 10268b919e6bSJeremy L Thompson 10278b919e6bSJeremy L Thompson @ref Backend 10288b919e6bSJeremy L Thompson **/ 1029beecbf24SJeremy L Thompson int CeedQFunctionAssemblyDataSetReuse(CeedQFunctionAssemblyData data, 1030beecbf24SJeremy L Thompson bool reuse_data) { 1031beecbf24SJeremy L Thompson data->reuse_data = reuse_data; 1032beecbf24SJeremy L Thompson data->needs_data_update = true; 1033beecbf24SJeremy L Thompson return CEED_ERROR_SUCCESS; 1034beecbf24SJeremy L Thompson } 1035beecbf24SJeremy L Thompson 1036beecbf24SJeremy L Thompson /** 1037beecbf24SJeremy L Thompson @brief Mark QFunctionAssemblyData as stale 1038beecbf24SJeremy L Thompson 1039beecbf24SJeremy L Thompson @param data CeedQFunctionAssemblyData to mark as stale 1040beecbf24SJeremy L Thompson @param needs_data_update Boolean flag indicating if update is needed or completed 1041beecbf24SJeremy L Thompson 1042beecbf24SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1043beecbf24SJeremy L Thompson 1044beecbf24SJeremy L Thompson @ref Backend 1045beecbf24SJeremy L Thompson **/ 1046beecbf24SJeremy L Thompson int CeedQFunctionAssemblyDataSetUpdateNeeded(CeedQFunctionAssemblyData data, 1047beecbf24SJeremy L Thompson bool needs_data_update) { 1048beecbf24SJeremy L Thompson data->needs_data_update = needs_data_update; 10498b919e6bSJeremy L Thompson return CEED_ERROR_SUCCESS; 10508b919e6bSJeremy L Thompson } 10518b919e6bSJeremy L Thompson 10528b919e6bSJeremy L Thompson /** 10538b919e6bSJeremy L Thompson @brief Determine if QFunctionAssemblyData needs update 10548b919e6bSJeremy L Thompson 10558b919e6bSJeremy L Thompson @param[in] data CeedQFunctionAssemblyData to mark as stale 10568b919e6bSJeremy L Thompson @param[out] is_update_needed Boolean flag indicating if re-assembly is required 10578b919e6bSJeremy L Thompson 10588b919e6bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 10598b919e6bSJeremy L Thompson 10608b919e6bSJeremy L Thompson @ref Backend 10618b919e6bSJeremy L Thompson **/ 10628b919e6bSJeremy L Thompson int CeedQFunctionAssemblyDataIsUpdateNeeded(CeedQFunctionAssemblyData data, 10638b919e6bSJeremy L Thompson bool *is_update_needed) { 1064beecbf24SJeremy L Thompson *is_update_needed = !data->reuse_data || data->needs_data_update; 10658b919e6bSJeremy L Thompson return CEED_ERROR_SUCCESS; 10668b919e6bSJeremy L Thompson } 10678b919e6bSJeremy L Thompson 10688b919e6bSJeremy L Thompson /** 1069480fae85SJeremy L Thompson @brief Copy the pointer to a CeedQFunctionAssemblyData. Both pointers should 1070480fae85SJeremy L Thompson be destroyed with `CeedCeedQFunctionAssemblyDataDestroy()`; 1071480fae85SJeremy L Thompson Note: If `*data_copy` is non-NULL, then it is assumed that 1072480fae85SJeremy L Thompson `*data_copy` is a pointer to a CeedQFunctionAssemblyData. This 1073480fae85SJeremy L Thompson CeedQFunctionAssemblyData will be destroyed if `*data_copy` is 1074480fae85SJeremy L Thompson the only reference to this CeedQFunctionAssemblyData. 1075480fae85SJeremy L Thompson 1076480fae85SJeremy L Thompson @param data CeedQFunctionAssemblyData to copy reference to 1077480fae85SJeremy L Thompson @param[out] data_copy Variable to store copied reference 1078480fae85SJeremy L Thompson 1079480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1080480fae85SJeremy L Thompson 1081480fae85SJeremy L Thompson @ref Backend 1082480fae85SJeremy L Thompson **/ 1083480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataReferenceCopy(CeedQFunctionAssemblyData data, 1084480fae85SJeremy L Thompson CeedQFunctionAssemblyData *data_copy) { 1085480fae85SJeremy L Thompson int ierr; 1086480fae85SJeremy L Thompson 1087480fae85SJeremy L Thompson ierr = CeedQFunctionAssemblyDataReference(data); CeedChk(ierr); 1088480fae85SJeremy L Thompson ierr = CeedQFunctionAssemblyDataDestroy(data_copy); CeedChk(ierr); 1089480fae85SJeremy L Thompson *data_copy = data; 1090480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1091480fae85SJeremy L Thompson } 1092480fae85SJeremy L Thompson 1093480fae85SJeremy L Thompson /** 1094480fae85SJeremy L Thompson @brief Get setup status for internal objects for CeedQFunctionAssemblyData 1095480fae85SJeremy L Thompson 1096480fae85SJeremy L Thompson @param[in] data CeedQFunctionAssemblyData to retreive status 1097480fae85SJeremy L Thompson @param[out] is_setup Boolean flag for setup status 1098480fae85SJeremy L Thompson 1099480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1100480fae85SJeremy L Thompson 1101480fae85SJeremy L Thompson @ref Backend 1102480fae85SJeremy L Thompson **/ 1103480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataIsSetup(CeedQFunctionAssemblyData data, 1104480fae85SJeremy L Thompson bool *is_setup) { 1105480fae85SJeremy L Thompson *is_setup = data->is_setup; 1106480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1107480fae85SJeremy L Thompson } 1108480fae85SJeremy L Thompson 1109480fae85SJeremy L Thompson /** 1110480fae85SJeremy L Thompson @brief Set internal objects for CeedQFunctionAssemblyData 1111480fae85SJeremy L Thompson 1112480fae85SJeremy L Thompson @param[in] data CeedQFunctionAssemblyData to set objects 1113480fae85SJeremy L Thompson @param[in] vec CeedVector to store assembled CeedQFunction at quadrature points 1114480fae85SJeremy L Thompson @param[in] rstr CeedElemRestriction for CeedVector containing assembled CeedQFunction 1115480fae85SJeremy L Thompson 1116480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1117480fae85SJeremy L Thompson 1118480fae85SJeremy L Thompson @ref Backend 1119480fae85SJeremy L Thompson **/ 1120480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataSetObjects(CeedQFunctionAssemblyData data, 1121480fae85SJeremy L Thompson CeedVector vec, CeedElemRestriction rstr) { 1122480fae85SJeremy L Thompson int ierr; 1123480fae85SJeremy L Thompson 11242efa2d85SJeremy L Thompson ierr = CeedVectorReferenceCopy(vec, &data->vec); CeedChk(ierr); 11252efa2d85SJeremy L Thompson ierr = CeedElemRestrictionReferenceCopy(rstr, &data->rstr); CeedChk(ierr); 1126480fae85SJeremy L Thompson 1127480fae85SJeremy L Thompson data->is_setup = true; 1128480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1129480fae85SJeremy L Thompson } 1130480fae85SJeremy L Thompson 1131480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataGetObjects(CeedQFunctionAssemblyData data, 1132480fae85SJeremy L Thompson CeedVector *vec, CeedElemRestriction *rstr) { 11332efa2d85SJeremy L Thompson int ierr; 11342efa2d85SJeremy L Thompson 1135480fae85SJeremy L Thompson if (!data->is_setup) 1136480fae85SJeremy L Thompson // LCOV_EXCL_START 1137480fae85SJeremy L Thompson return CeedError(data->ceed, CEED_ERROR_INCOMPLETE, 1138480fae85SJeremy L Thompson "Internal objects not set; must call CeedQFunctionAssemblyDataSetObjects first."); 1139480fae85SJeremy L Thompson // LCOV_EXCL_STOP 1140480fae85SJeremy L Thompson 11412efa2d85SJeremy L Thompson ierr = CeedVectorReferenceCopy(data->vec, vec); CeedChk(ierr); 11422efa2d85SJeremy L Thompson ierr = CeedElemRestrictionReferenceCopy(data->rstr, rstr); CeedChk(ierr); 1143480fae85SJeremy L Thompson 1144480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1145480fae85SJeremy L Thompson } 1146480fae85SJeremy L Thompson 1147480fae85SJeremy L Thompson /** 1148480fae85SJeremy L Thompson @brief Destroy CeedQFunctionAssemblyData 1149480fae85SJeremy L Thompson 1150480fae85SJeremy L Thompson @param[out] data CeedQFunctionAssemblyData to destroy 1151480fae85SJeremy L Thompson 1152480fae85SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1153480fae85SJeremy L Thompson 1154480fae85SJeremy L Thompson @ref Backend 1155480fae85SJeremy L Thompson **/ 1156480fae85SJeremy L Thompson int CeedQFunctionAssemblyDataDestroy(CeedQFunctionAssemblyData *data) { 1157480fae85SJeremy L Thompson int ierr; 1158480fae85SJeremy L Thompson 1159480fae85SJeremy L Thompson if (!*data || --(*data)->ref_count > 0) return CEED_ERROR_SUCCESS; 1160480fae85SJeremy L Thompson 1161480fae85SJeremy L Thompson ierr = CeedDestroy(&(*data)->ceed); CeedChk(ierr); 1162480fae85SJeremy L Thompson ierr = CeedVectorDestroy(&(*data)->vec); CeedChk(ierr); 1163480fae85SJeremy L Thompson ierr = CeedElemRestrictionDestroy(&(*data)->rstr); CeedChk(ierr); 1164480fae85SJeremy L Thompson 1165480fae85SJeremy L Thompson ierr = CeedFree(data); CeedChk(ierr); 1166480fae85SJeremy L Thompson return CEED_ERROR_SUCCESS; 1167480fae85SJeremy L Thompson } 1168480fae85SJeremy L Thompson 1169ed9e99e6SJeremy L Thompson /** 1170ed9e99e6SJeremy L Thompson @brief Get CeedOperatorAssemblyData 1171ed9e99e6SJeremy L Thompson 1172ed9e99e6SJeremy L Thompson @param[in] op CeedOperator to assemble 1173ed9e99e6SJeremy L Thompson @param[out] data CeedQFunctionAssemblyData 1174ed9e99e6SJeremy L Thompson 1175ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1176ed9e99e6SJeremy L Thompson 1177ed9e99e6SJeremy L Thompson @ref Backend 1178ed9e99e6SJeremy L Thompson **/ 1179ed9e99e6SJeremy L Thompson int CeedOperatorGetOperatorAssemblyData(CeedOperator op, 1180ed9e99e6SJeremy L Thompson CeedOperatorAssemblyData *data) { 1181ed9e99e6SJeremy L Thompson int ierr; 1182ed9e99e6SJeremy L Thompson 1183ed9e99e6SJeremy L Thompson if (!op->op_assembled) { 1184ed9e99e6SJeremy L Thompson CeedOperatorAssemblyData data; 1185ed9e99e6SJeremy L Thompson 1186ed9e99e6SJeremy L Thompson ierr = CeedOperatorAssemblyDataCreate(op->ceed, op, &data); CeedChk(ierr); 1187ed9e99e6SJeremy L Thompson op->op_assembled = data; 1188ed9e99e6SJeremy L Thompson } 1189ed9e99e6SJeremy L Thompson *data = op->op_assembled; 1190ed9e99e6SJeremy L Thompson 1191ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1192ed9e99e6SJeremy L Thompson } 1193ed9e99e6SJeremy L Thompson 1194ed9e99e6SJeremy L Thompson /** 1195ed9e99e6SJeremy L Thompson @brief Create object holding CeedOperator assembly data 1196ed9e99e6SJeremy L Thompson 1197ed9e99e6SJeremy L Thompson @param[in] ceed A Ceed object where the CeedOperatorAssemblyData will be created 1198ed9e99e6SJeremy L Thompson @param[in] op CeedOperator to be assembled 1199ed9e99e6SJeremy L Thompson @param[out] data Address of the variable where the newly created 1200ed9e99e6SJeremy L Thompson CeedOperatorAssemblyData will be stored 1201ed9e99e6SJeremy L Thompson 1202ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1203ed9e99e6SJeremy L Thompson 1204ed9e99e6SJeremy L Thompson @ref Backend 1205ed9e99e6SJeremy L Thompson **/ 1206ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, 1207ed9e99e6SJeremy L Thompson CeedOperatorAssemblyData *data) { 1208ed9e99e6SJeremy L Thompson int ierr; 1209ed9e99e6SJeremy L Thompson 1210ed9e99e6SJeremy L Thompson ierr = CeedCalloc(1, data); CeedChk(ierr); 1211ed9e99e6SJeremy L Thompson (*data)->ceed = ceed; 1212ed9e99e6SJeremy L Thompson ierr = CeedReference(ceed); CeedChk(ierr); 1213ed9e99e6SJeremy L Thompson 1214ed9e99e6SJeremy L Thompson // Build OperatorAssembly data 1215ed9e99e6SJeremy L Thompson CeedQFunction qf; 1216ed9e99e6SJeremy L Thompson CeedQFunctionField *qf_fields; 1217ed9e99e6SJeremy L Thompson CeedOperatorField *op_fields; 1218ed9e99e6SJeremy L Thompson CeedInt num_input_fields; 1219ed9e99e6SJeremy L Thompson ierr = CeedOperatorGetQFunction(op, &qf); CeedChk(ierr); 1220ed9e99e6SJeremy L Thompson ierr = CeedQFunctionGetFields(qf, &num_input_fields, &qf_fields, NULL, NULL); 1221ed9e99e6SJeremy L Thompson CeedChk(ierr); 1222ed9e99e6SJeremy L Thompson ierr = CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL); CeedChk(ierr); 1223ed9e99e6SJeremy L Thompson 1224ed9e99e6SJeremy L Thompson // Determine active input basis 1225ed9e99e6SJeremy L Thompson CeedInt num_eval_mode_in = 0, dim = 1; 1226ed9e99e6SJeremy L Thompson CeedEvalMode *eval_mode_in = NULL; 1227ed9e99e6SJeremy L Thompson CeedBasis basis_in = NULL; 1228ed9e99e6SJeremy L Thompson for (CeedInt i=0; i<num_input_fields; i++) { 1229ed9e99e6SJeremy L Thompson CeedVector vec; 1230ed9e99e6SJeremy L Thompson ierr = CeedOperatorFieldGetVector(op_fields[i], &vec); CeedChk(ierr); 1231ed9e99e6SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 1232ed9e99e6SJeremy L Thompson ierr = CeedOperatorFieldGetBasis(op_fields[i], &basis_in); 1233ed9e99e6SJeremy L Thompson CeedChk(ierr); 1234ed9e99e6SJeremy L Thompson ierr = CeedBasisGetDimension(basis_in, &dim); CeedChk(ierr); 1235ed9e99e6SJeremy L Thompson CeedEvalMode eval_mode; 1236ed9e99e6SJeremy L Thompson ierr = CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode); 1237ed9e99e6SJeremy L Thompson CeedChk(ierr); 1238ed9e99e6SJeremy L Thompson switch (eval_mode) { 1239ed9e99e6SJeremy L Thompson case CEED_EVAL_NONE: 1240ed9e99e6SJeremy L Thompson case CEED_EVAL_INTERP: 1241ed9e99e6SJeremy L Thompson ierr = CeedRealloc(num_eval_mode_in + 1, &eval_mode_in); CeedChk(ierr); 1242ed9e99e6SJeremy L Thompson eval_mode_in[num_eval_mode_in] = eval_mode; 1243ed9e99e6SJeremy L Thompson num_eval_mode_in += 1; 1244ed9e99e6SJeremy L Thompson break; 1245ed9e99e6SJeremy L Thompson case CEED_EVAL_GRAD: 1246ed9e99e6SJeremy L Thompson ierr = CeedRealloc(num_eval_mode_in + dim, &eval_mode_in); CeedChk(ierr); 1247ed9e99e6SJeremy L Thompson for (CeedInt d=0; d<dim; d++) { 1248ed9e99e6SJeremy L Thompson eval_mode_in[num_eval_mode_in+d] = eval_mode; 1249ed9e99e6SJeremy L Thompson } 1250ed9e99e6SJeremy L Thompson num_eval_mode_in += dim; 1251ed9e99e6SJeremy L Thompson break; 1252ed9e99e6SJeremy L Thompson case CEED_EVAL_WEIGHT: 1253ed9e99e6SJeremy L Thompson case CEED_EVAL_DIV: 1254ed9e99e6SJeremy L Thompson case CEED_EVAL_CURL: 1255ed9e99e6SJeremy L Thompson break; // Caught by QF Assembly 1256ed9e99e6SJeremy L Thompson } 1257ed9e99e6SJeremy L Thompson } 1258ed9e99e6SJeremy L Thompson } 1259ed9e99e6SJeremy L Thompson (*data)->num_eval_mode_in = num_eval_mode_in; 1260ed9e99e6SJeremy L Thompson (*data)->eval_mode_in = eval_mode_in; 1261ed9e99e6SJeremy L Thompson ierr = CeedBasisReferenceCopy(basis_in, &(*data)->basis_in); CeedChk(ierr); 1262ed9e99e6SJeremy L Thompson 1263ed9e99e6SJeremy L Thompson // Determine active output basis 1264ed9e99e6SJeremy L Thompson CeedInt num_output_fields; 1265ed9e99e6SJeremy L Thompson ierr = CeedQFunctionGetFields(qf, NULL, NULL, &num_output_fields, &qf_fields); 1266ed9e99e6SJeremy L Thompson CeedChk(ierr); 1267ed9e99e6SJeremy L Thompson ierr = CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields); CeedChk(ierr); 1268ed9e99e6SJeremy L Thompson CeedInt num_eval_mode_out = 0; 1269ed9e99e6SJeremy L Thompson CeedEvalMode *eval_mode_out = NULL; 1270ed9e99e6SJeremy L Thompson CeedBasis basis_out = NULL; 1271ed9e99e6SJeremy L Thompson for (CeedInt i=0; i<num_output_fields; i++) { 1272ed9e99e6SJeremy L Thompson CeedVector vec; 1273ed9e99e6SJeremy L Thompson ierr = CeedOperatorFieldGetVector(op_fields[i], &vec); CeedChk(ierr); 1274ed9e99e6SJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 1275ed9e99e6SJeremy L Thompson ierr = CeedOperatorFieldGetBasis(op_fields[i], &basis_out); 1276ed9e99e6SJeremy L Thompson CeedChk(ierr); 1277ed9e99e6SJeremy L Thompson CeedEvalMode eval_mode; 1278ed9e99e6SJeremy L Thompson ierr = CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode); 1279ed9e99e6SJeremy L Thompson CeedChk(ierr); 1280ed9e99e6SJeremy L Thompson switch (eval_mode) { 1281ed9e99e6SJeremy L Thompson case CEED_EVAL_NONE: 1282ed9e99e6SJeremy L Thompson case CEED_EVAL_INTERP: 1283ed9e99e6SJeremy L Thompson ierr = CeedRealloc(num_eval_mode_out + 1, &eval_mode_out); CeedChk(ierr); 1284ed9e99e6SJeremy L Thompson eval_mode_out[num_eval_mode_out] = eval_mode; 1285ed9e99e6SJeremy L Thompson num_eval_mode_out += 1; 1286ed9e99e6SJeremy L Thompson break; 1287ed9e99e6SJeremy L Thompson case CEED_EVAL_GRAD: 1288ed9e99e6SJeremy L Thompson ierr = CeedRealloc(num_eval_mode_out + dim, &eval_mode_out); CeedChk(ierr); 1289ed9e99e6SJeremy L Thompson for (CeedInt d=0; d<dim; d++) { 1290ed9e99e6SJeremy L Thompson eval_mode_out[num_eval_mode_out+d] = eval_mode; 1291ed9e99e6SJeremy L Thompson } 1292ed9e99e6SJeremy L Thompson num_eval_mode_out += dim; 1293ed9e99e6SJeremy L Thompson break; 1294ed9e99e6SJeremy L Thompson case CEED_EVAL_WEIGHT: 1295ed9e99e6SJeremy L Thompson case CEED_EVAL_DIV: 1296ed9e99e6SJeremy L Thompson case CEED_EVAL_CURL: 1297ed9e99e6SJeremy L Thompson break; // Caught by QF Assembly 1298ed9e99e6SJeremy L Thompson } 1299ed9e99e6SJeremy L Thompson } 1300ed9e99e6SJeremy L Thompson } 1301ed9e99e6SJeremy L Thompson (*data)->num_eval_mode_out = num_eval_mode_out; 1302ed9e99e6SJeremy L Thompson (*data)->eval_mode_out = eval_mode_out; 1303ed9e99e6SJeremy L Thompson ierr = CeedBasisReferenceCopy(basis_out, &(*data)->basis_out); CeedChk(ierr); 1304ed9e99e6SJeremy L Thompson 1305ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1306ed9e99e6SJeremy L Thompson } 1307ed9e99e6SJeremy L Thompson 1308ed9e99e6SJeremy L Thompson /** 1309ed9e99e6SJeremy L Thompson @brief Get CeedOperator CeedEvalModes for assembly 1310ed9e99e6SJeremy L Thompson 1311ed9e99e6SJeremy L Thompson @param[in] data CeedOperatorAssemblyData 1312ed9e99e6SJeremy L Thompson @param[out] num_eval_mode_in Pointer to hold number of input CeedEvalModes, or NULL 1313ed9e99e6SJeremy L Thompson @param[out] eval_mode_in Pointer to hold input CeedEvalModes, or NULL 1314ed9e99e6SJeremy L Thompson @param[out] num_eval_mode_out Pointer to hold number of output CeedEvalModes, or NULL 1315ed9e99e6SJeremy L Thompson @param[out] eval_mode_out Pointer to hold output CeedEvalModes, or NULL 1316ed9e99e6SJeremy L Thompson 1317ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1318ed9e99e6SJeremy L Thompson 1319ed9e99e6SJeremy L Thompson @ref Backend 1320ed9e99e6SJeremy L Thompson **/ 1321ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataGetEvalModes(CeedOperatorAssemblyData data, 1322ed9e99e6SJeremy L Thompson CeedInt *num_eval_mode_in, const CeedEvalMode **eval_mode_in, 1323ed9e99e6SJeremy L Thompson CeedInt *num_eval_mode_out, const CeedEvalMode **eval_mode_out) { 1324ed9e99e6SJeremy L Thompson if (num_eval_mode_in) *num_eval_mode_in = data->num_eval_mode_in; 1325ed9e99e6SJeremy L Thompson if (eval_mode_in) *eval_mode_in = data->eval_mode_in; 1326ed9e99e6SJeremy L Thompson if (num_eval_mode_out) *num_eval_mode_out = data->num_eval_mode_out; 1327ed9e99e6SJeremy L Thompson if (eval_mode_out) *eval_mode_out = data->eval_mode_out; 1328ed9e99e6SJeremy L Thompson 1329ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1330ed9e99e6SJeremy L Thompson } 1331ed9e99e6SJeremy L Thompson 1332ed9e99e6SJeremy L Thompson /** 1333ed9e99e6SJeremy L Thompson @brief Get CeedOperator CeedBasis data for assembly 1334ed9e99e6SJeremy L Thompson 1335ed9e99e6SJeremy L Thompson @param[in] data CeedOperatorAssemblyData 1336ed9e99e6SJeremy L Thompson @param[out] basis_in Pointer to hold active input CeedBasis, or NULL 1337ed9e99e6SJeremy L Thompson @param[out] B_in Pointer to hold assembled active input B, or NULL 1338ed9e99e6SJeremy L Thompson @param[out] basis_out Pointer to hold active output CeedBasis, or NULL 1339ed9e99e6SJeremy L Thompson @param[out] B_out Pointer to hold assembled active output B, or NULL 1340ed9e99e6SJeremy L Thompson 1341ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1342ed9e99e6SJeremy L Thompson 1343ed9e99e6SJeremy L Thompson @ref Backend 1344ed9e99e6SJeremy L Thompson **/ 1345ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataGetBases(CeedOperatorAssemblyData data, 1346ed9e99e6SJeremy L Thompson CeedBasis *basis_in, const CeedScalar **B_in, CeedBasis *basis_out, 1347ed9e99e6SJeremy L Thompson const CeedScalar **B_out) { 1348ed9e99e6SJeremy L Thompson int ierr; 1349ed9e99e6SJeremy L Thompson 1350ed9e99e6SJeremy L Thompson // Assemble B_in, B_out if needed 1351ed9e99e6SJeremy L Thompson if (B_in && !data->B_in) { 1352ed9e99e6SJeremy L Thompson CeedInt num_qpts, elem_size; 1353ed9e99e6SJeremy L Thompson CeedScalar *B_in, *identity = NULL; 1354ed9e99e6SJeremy L Thompson const CeedScalar *interp_in, *grad_in; 1355ed9e99e6SJeremy L Thompson bool has_eval_none = false; 1356ed9e99e6SJeremy L Thompson 1357ed9e99e6SJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints(data->basis_in, &num_qpts); 1358ed9e99e6SJeremy L Thompson CeedChk(ierr); 1359ed9e99e6SJeremy L Thompson ierr = CeedBasisGetNumNodes(data->basis_in, &elem_size); CeedChk(ierr); 1360ed9e99e6SJeremy L Thompson ierr = CeedCalloc(num_qpts * elem_size * data->num_eval_mode_in, &B_in); 1361ed9e99e6SJeremy L Thompson CeedChk(ierr); 1362ed9e99e6SJeremy L Thompson 1363ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < data->num_eval_mode_in; i++) { 1364ed9e99e6SJeremy L Thompson has_eval_none = has_eval_none || (data->eval_mode_in[i] == CEED_EVAL_NONE); 1365ed9e99e6SJeremy L Thompson } 1366ed9e99e6SJeremy L Thompson if (has_eval_none) { 1367ed9e99e6SJeremy L Thompson ierr = CeedCalloc(num_qpts * elem_size, &identity); CeedChk(ierr); 1368ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < (elem_size < num_qpts ? elem_size : num_qpts); i++) { 1369ed9e99e6SJeremy L Thompson identity[i * elem_size + i] = 1.0; 1370ed9e99e6SJeremy L Thompson } 1371ed9e99e6SJeremy L Thompson } 1372ed9e99e6SJeremy L Thompson ierr = CeedBasisGetInterp(data->basis_in, &interp_in); CeedChk(ierr); 1373ed9e99e6SJeremy L Thompson ierr = CeedBasisGetGrad(data->basis_in, &grad_in); CeedChk(ierr); 1374ed9e99e6SJeremy L Thompson 1375ed9e99e6SJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 1376ed9e99e6SJeremy L Thompson for (CeedInt n = 0; n < elem_size; n++) { 1377ed9e99e6SJeremy L Thompson CeedInt d_in = -1; 1378ed9e99e6SJeremy L Thompson for (CeedInt e_in = 0; e_in < data->num_eval_mode_in; e_in++) { 1379ed9e99e6SJeremy L Thompson const CeedInt qq = data->num_eval_mode_in * q; 1380ed9e99e6SJeremy L Thompson const CeedScalar *b = NULL; 1381ed9e99e6SJeremy L Thompson 1382ed9e99e6SJeremy L Thompson if (data->eval_mode_in[e_in] == CEED_EVAL_GRAD) d_in++; 1383ed9e99e6SJeremy L Thompson CeedOperatorGetBasisPointer(data->eval_mode_in[e_in], identity, 1384ed9e99e6SJeremy L Thompson interp_in, &grad_in[d_in * num_qpts * elem_size], &b); CeedChk(ierr); 1385ed9e99e6SJeremy L Thompson B_in[(qq + e_in)*elem_size + n] = b[q * elem_size + n]; 1386ed9e99e6SJeremy L Thompson } 1387ed9e99e6SJeremy L Thompson } 1388ed9e99e6SJeremy L Thompson } 1389ed9e99e6SJeremy L Thompson data->B_in = B_in; 1390ed9e99e6SJeremy L Thompson } 1391ed9e99e6SJeremy L Thompson 1392ed9e99e6SJeremy L Thompson if (B_out && !data->B_out) { 1393ed9e99e6SJeremy L Thompson CeedInt num_qpts, elem_size; 1394ed9e99e6SJeremy L Thompson CeedScalar *B_out, *identity = NULL; 1395ed9e99e6SJeremy L Thompson const CeedScalar *interp_out, *grad_out; 1396ed9e99e6SJeremy L Thompson bool has_eval_none = false; 1397ed9e99e6SJeremy L Thompson 1398ed9e99e6SJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints(data->basis_out, &num_qpts); 1399ed9e99e6SJeremy L Thompson CeedChk(ierr); 1400ed9e99e6SJeremy L Thompson ierr = CeedBasisGetNumNodes(data->basis_out, &elem_size); CeedChk(ierr); 1401ed9e99e6SJeremy L Thompson ierr = CeedCalloc(num_qpts * elem_size * data->num_eval_mode_out, &B_out); 1402ed9e99e6SJeremy L Thompson CeedChk(ierr); 1403ed9e99e6SJeremy L Thompson 1404ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < data->num_eval_mode_out; i++) { 1405ed9e99e6SJeremy L Thompson has_eval_none = has_eval_none || (data->eval_mode_out[i] == CEED_EVAL_NONE); 1406ed9e99e6SJeremy L Thompson } 1407ed9e99e6SJeremy L Thompson if (has_eval_none) { 1408ed9e99e6SJeremy L Thompson ierr = CeedCalloc(num_qpts * elem_size, &identity); CeedChk(ierr); 1409ed9e99e6SJeremy L Thompson for (CeedInt i = 0; i < (elem_size < num_qpts ? elem_size : num_qpts); i++) { 1410ed9e99e6SJeremy L Thompson identity[i * elem_size + i] = 1.0; 1411ed9e99e6SJeremy L Thompson } 1412ed9e99e6SJeremy L Thompson } 1413ed9e99e6SJeremy L Thompson ierr = CeedBasisGetInterp(data->basis_out, &interp_out); CeedChk(ierr); 1414ed9e99e6SJeremy L Thompson ierr = CeedBasisGetGrad(data->basis_out, &grad_out); CeedChk(ierr); 1415ed9e99e6SJeremy L Thompson 1416ed9e99e6SJeremy L Thompson for (CeedInt q = 0; q < num_qpts; q++) { 1417ed9e99e6SJeremy L Thompson for (CeedInt n = 0; n < elem_size; n++) { 1418ed9e99e6SJeremy L Thompson CeedInt d_out = -1; 1419ed9e99e6SJeremy L Thompson for (CeedInt e_out = 0; e_out < data->num_eval_mode_out; e_out++) { 1420ed9e99e6SJeremy L Thompson const CeedInt qq = data->num_eval_mode_out * q; 1421ed9e99e6SJeremy L Thompson const CeedScalar *b = NULL; 1422ed9e99e6SJeremy L Thompson 1423ed9e99e6SJeremy L Thompson if (data->eval_mode_out[e_out] == CEED_EVAL_GRAD) d_out++; 1424ed9e99e6SJeremy L Thompson CeedOperatorGetBasisPointer(data->eval_mode_out[e_out], identity, 1425ed9e99e6SJeremy L Thompson interp_out, &grad_out[d_out * num_qpts * elem_size], &b); CeedChk(ierr); 1426ed9e99e6SJeremy L Thompson B_out[(qq + e_out)*elem_size + n] = b[q * elem_size + n]; 1427ed9e99e6SJeremy L Thompson } 1428ed9e99e6SJeremy L Thompson } 1429ed9e99e6SJeremy L Thompson } 1430ed9e99e6SJeremy L Thompson data->B_out = B_out; 1431ed9e99e6SJeremy L Thompson } 1432ed9e99e6SJeremy L Thompson 1433ed9e99e6SJeremy L Thompson if (basis_in) *basis_in = data->basis_in; 1434ed9e99e6SJeremy L Thompson if (B_in) *B_in = data->B_in; 1435ed9e99e6SJeremy L Thompson if (basis_out) *basis_out = data->basis_out; 1436ed9e99e6SJeremy L Thompson if (B_out) *B_out = data->B_out; 1437ed9e99e6SJeremy L Thompson 1438ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1439ed9e99e6SJeremy L Thompson } 1440ed9e99e6SJeremy L Thompson 1441ed9e99e6SJeremy L Thompson /** 1442ed9e99e6SJeremy L Thompson @brief Destroy CeedOperatorAssemblyData 1443ed9e99e6SJeremy L Thompson 1444ed9e99e6SJeremy L Thompson @param[out] data CeedOperatorAssemblyData to destroy 1445ed9e99e6SJeremy L Thompson 1446ed9e99e6SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1447ed9e99e6SJeremy L Thompson 1448ed9e99e6SJeremy L Thompson @ref Backend 1449ed9e99e6SJeremy L Thompson **/ 1450ed9e99e6SJeremy L Thompson int CeedOperatorAssemblyDataDestroy(CeedOperatorAssemblyData *data) { 1451ed9e99e6SJeremy L Thompson int ierr; 1452ed9e99e6SJeremy L Thompson 1453ed9e99e6SJeremy L Thompson if (!*data) return CEED_ERROR_SUCCESS; 1454ed9e99e6SJeremy L Thompson 1455ed9e99e6SJeremy L Thompson ierr = CeedDestroy(&(*data)->ceed); CeedChk(ierr); 1456ed9e99e6SJeremy L Thompson ierr = CeedBasisDestroy(&(*data)->basis_in); CeedChk(ierr); 1457ed9e99e6SJeremy L Thompson ierr = CeedBasisDestroy(&(*data)->basis_out); CeedChk(ierr); 1458ed9e99e6SJeremy L Thompson ierr = CeedFree(&(*data)->B_in); CeedChk(ierr); 1459ed9e99e6SJeremy L Thompson ierr = CeedFree(&(*data)->B_out); CeedChk(ierr); 1460ed9e99e6SJeremy L Thompson 1461ed9e99e6SJeremy L Thompson ierr = CeedFree(data); CeedChk(ierr); 1462ed9e99e6SJeremy L Thompson return CEED_ERROR_SUCCESS; 1463ed9e99e6SJeremy L Thompson } 1464ed9e99e6SJeremy L Thompson 1465480fae85SJeremy L Thompson /// @} 1466480fae85SJeremy L Thompson 1467480fae85SJeremy L Thompson /// ---------------------------------------------------------------------------- 1468eaf62fffSJeremy L Thompson /// CeedOperator Public API 1469eaf62fffSJeremy L Thompson /// ---------------------------------------------------------------------------- 1470eaf62fffSJeremy L Thompson /// @addtogroup CeedOperatorUser 1471eaf62fffSJeremy L Thompson /// @{ 1472eaf62fffSJeremy L Thompson 1473eaf62fffSJeremy L Thompson /** 1474eaf62fffSJeremy L Thompson @brief Assemble a linear CeedQFunction associated with a CeedOperator 1475eaf62fffSJeremy L Thompson 1476eaf62fffSJeremy L Thompson This returns a CeedVector containing a matrix at each quadrature point 1477eaf62fffSJeremy L Thompson providing the action of the CeedQFunction associated with the CeedOperator. 1478eaf62fffSJeremy L Thompson The vector 'assembled' is of shape 1479eaf62fffSJeremy L Thompson [num_elements, num_input_fields, num_output_fields, num_quad_points] 1480eaf62fffSJeremy L Thompson and contains column-major matrices representing the action of the 1481eaf62fffSJeremy L Thompson CeedQFunction for a corresponding quadrature point on an element. Inputs and 1482eaf62fffSJeremy L Thompson outputs are in the order provided by the user when adding CeedOperator fields. 1483eaf62fffSJeremy L Thompson For example, a CeedQFunction with inputs 'u' and 'gradu' and outputs 'gradv' and 1484eaf62fffSJeremy L Thompson 'v', provided in that order, would result in an assembled QFunction that 1485eaf62fffSJeremy L Thompson consists of (1 + dim) x (dim + 1) matrices at each quadrature point acting 1486eaf62fffSJeremy L Thompson on the input [u, du_0, du_1] and producing the output [dv_0, dv_1, v]. 1487eaf62fffSJeremy L Thompson 1488f04ea552SJeremy L Thompson Note: Calling this function asserts that setup is complete 1489f04ea552SJeremy L Thompson and sets the CeedOperator as immutable. 1490f04ea552SJeremy L Thompson 1491eaf62fffSJeremy L Thompson @param op CeedOperator to assemble CeedQFunction 1492eaf62fffSJeremy L Thompson @param[out] assembled CeedVector to store assembled CeedQFunction at 1493eaf62fffSJeremy L Thompson quadrature points 1494eaf62fffSJeremy L Thompson @param[out] rstr CeedElemRestriction for CeedVector containing assembled 1495eaf62fffSJeremy L Thompson CeedQFunction 1496eaf62fffSJeremy L Thompson @param request Address of CeedRequest for non-blocking completion, else 1497eaf62fffSJeremy L Thompson @ref CEED_REQUEST_IMMEDIATE 1498eaf62fffSJeremy L Thompson 1499eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1500eaf62fffSJeremy L Thompson 1501eaf62fffSJeremy L Thompson @ref User 1502eaf62fffSJeremy L Thompson **/ 1503eaf62fffSJeremy L Thompson int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, 1504eaf62fffSJeremy L Thompson CeedElemRestriction *rstr, 1505eaf62fffSJeremy L Thompson CeedRequest *request) { 1506eaf62fffSJeremy L Thompson int ierr; 1507eaf62fffSJeremy L Thompson ierr = CeedOperatorCheckReady(op); CeedChk(ierr); 1508eaf62fffSJeremy L Thompson 1509eaf62fffSJeremy L Thompson if (op->LinearAssembleQFunction) { 1510d04bbc78SJeremy L Thompson // Backend version 151170a7ffb3SJeremy L Thompson ierr = op->LinearAssembleQFunction(op, assembled, rstr, request); 151270a7ffb3SJeremy L Thompson CeedChk(ierr); 1513eaf62fffSJeremy L Thompson } else { 1514d04bbc78SJeremy L Thompson // Operator fallback 1515d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1516d04bbc78SJeremy L Thompson 1517d04bbc78SJeremy L Thompson ierr = CeedOperatorGetFallback(op, &op_fallback); CeedChk(ierr); 1518d04bbc78SJeremy L Thompson if (op_fallback) { 1519d04bbc78SJeremy L Thompson ierr = CeedOperatorLinearAssembleQFunction(op_fallback, assembled, 1520eaf62fffSJeremy L Thompson rstr, request); CeedChk(ierr); 1521d04bbc78SJeremy L Thompson } else { 1522d04bbc78SJeremy L Thompson // LCOV_EXCL_START 1523d04bbc78SJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, 1524d04bbc78SJeremy L Thompson "Backend does not support CeedOperatorLinearAssembleQFunction"); 1525d04bbc78SJeremy L Thompson // LCOV_EXCL_STOP 1526d04bbc78SJeremy L Thompson } 152770a7ffb3SJeremy L Thompson } 1528eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1529eaf62fffSJeremy L Thompson } 153070a7ffb3SJeremy L Thompson 153170a7ffb3SJeremy L Thompson /** 153270a7ffb3SJeremy L Thompson @brief Assemble CeedQFunction and store result internall. Return copied 153370a7ffb3SJeremy L Thompson references of stored data to the caller. Caller is responsible for 153470a7ffb3SJeremy L Thompson ownership and destruction of the copied references. See also 153570a7ffb3SJeremy L Thompson @ref CeedOperatorLinearAssembleQFunction 153670a7ffb3SJeremy L Thompson 153770a7ffb3SJeremy L Thompson @param op CeedOperator to assemble CeedQFunction 153870a7ffb3SJeremy L Thompson @param assembled CeedVector to store assembled CeedQFunction at 153970a7ffb3SJeremy L Thompson quadrature points 154070a7ffb3SJeremy L Thompson @param rstr CeedElemRestriction for CeedVector containing assembled 154170a7ffb3SJeremy L Thompson CeedQFunction 154270a7ffb3SJeremy L Thompson @param request Address of CeedRequest for non-blocking completion, else 154370a7ffb3SJeremy L Thompson @ref CEED_REQUEST_IMMEDIATE 154470a7ffb3SJeremy L Thompson 154570a7ffb3SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 154670a7ffb3SJeremy L Thompson 154770a7ffb3SJeremy L Thompson @ref User 154870a7ffb3SJeremy L Thompson **/ 154970a7ffb3SJeremy L Thompson int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, 155070a7ffb3SJeremy L Thompson CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { 155170a7ffb3SJeremy L Thompson int ierr; 155270a7ffb3SJeremy L Thompson ierr = CeedOperatorCheckReady(op); CeedChk(ierr); 155370a7ffb3SJeremy L Thompson 155470a7ffb3SJeremy L Thompson if (op->LinearAssembleQFunctionUpdate) { 1555d04bbc78SJeremy L Thompson // Backend version 1556480fae85SJeremy L Thompson bool qf_assembled_is_setup; 15572efa2d85SJeremy L Thompson CeedVector assembled_vec = NULL; 15582efa2d85SJeremy L Thompson CeedElemRestriction assembled_rstr = NULL; 1559480fae85SJeremy L Thompson 1560480fae85SJeremy L Thompson ierr = CeedQFunctionAssemblyDataIsSetup(op->qf_assembled, 1561480fae85SJeremy L Thompson &qf_assembled_is_setup); CeedChk(ierr); 1562480fae85SJeremy L Thompson if (qf_assembled_is_setup) { 1563d04bbc78SJeremy L Thompson bool update_needed; 1564d04bbc78SJeremy L Thompson 1565480fae85SJeremy L Thompson ierr = CeedQFunctionAssemblyDataGetObjects(op->qf_assembled, &assembled_vec, 1566480fae85SJeremy L Thompson &assembled_rstr); CeedChk(ierr); 15678b919e6bSJeremy L Thompson ierr = CeedQFunctionAssemblyDataIsUpdateNeeded(op->qf_assembled, 15688b919e6bSJeremy L Thompson &update_needed); CeedChk(ierr); 15698b919e6bSJeremy L Thompson if (update_needed) { 1570480fae85SJeremy L Thompson ierr = op->LinearAssembleQFunctionUpdate(op, assembled_vec, assembled_rstr, 1571480fae85SJeremy L Thompson request); CeedChk(ierr); 15728b919e6bSJeremy L Thompson } 157370a7ffb3SJeremy L Thompson } else { 1574480fae85SJeremy L Thompson ierr = op->LinearAssembleQFunction(op, &assembled_vec, &assembled_rstr, 1575480fae85SJeremy L Thompson request); CeedChk(ierr); 1576480fae85SJeremy L Thompson ierr = CeedQFunctionAssemblyDataSetObjects(op->qf_assembled, assembled_vec, 1577480fae85SJeremy L Thompson assembled_rstr); CeedChk(ierr); 157870a7ffb3SJeremy L Thompson } 1579beecbf24SJeremy L Thompson ierr = CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, false); 15808b919e6bSJeremy L Thompson CeedChk(ierr); 15812efa2d85SJeremy L Thompson 1582d04bbc78SJeremy L Thompson // Copy reference from internally held copy 158370a7ffb3SJeremy L Thompson *assembled = NULL; 158470a7ffb3SJeremy L Thompson *rstr = NULL; 1585480fae85SJeremy L Thompson ierr = CeedVectorReferenceCopy(assembled_vec, assembled); CeedChk(ierr); 15862efa2d85SJeremy L Thompson ierr = CeedVectorDestroy(&assembled_vec); CeedChk(ierr); 1587480fae85SJeremy L Thompson ierr = CeedElemRestrictionReferenceCopy(assembled_rstr, rstr); CeedChk(ierr); 15882efa2d85SJeremy L Thompson ierr = CeedElemRestrictionDestroy(&assembled_rstr); CeedChk(ierr); 158970a7ffb3SJeremy L Thompson } else { 1590d04bbc78SJeremy L Thompson // Operator fallback 1591d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1592d04bbc78SJeremy L Thompson 1593d04bbc78SJeremy L Thompson ierr = CeedOperatorGetFallback(op, &op_fallback); CeedChk(ierr); 1594d04bbc78SJeremy L Thompson if (op_fallback) { 1595d04bbc78SJeremy L Thompson ierr = CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, 1596d04bbc78SJeremy L Thompson rstr, request); CeedChk(ierr); 1597d04bbc78SJeremy L Thompson } else { 1598d04bbc78SJeremy L Thompson // LCOV_EXCL_START 1599d04bbc78SJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, 1600d04bbc78SJeremy L Thompson "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate"); 1601d04bbc78SJeremy L Thompson // LCOV_EXCL_STOP 160270a7ffb3SJeremy L Thompson } 160370a7ffb3SJeremy L Thompson } 160470a7ffb3SJeremy L Thompson 160570a7ffb3SJeremy L Thompson return CEED_ERROR_SUCCESS; 1606eaf62fffSJeremy L Thompson } 1607eaf62fffSJeremy L Thompson 1608eaf62fffSJeremy L Thompson /** 1609eaf62fffSJeremy L Thompson @brief Assemble the diagonal of a square linear CeedOperator 1610eaf62fffSJeremy L Thompson 1611eaf62fffSJeremy L Thompson This overwrites a CeedVector with the diagonal of a linear CeedOperator. 1612eaf62fffSJeremy L Thompson 1613eaf62fffSJeremy L Thompson Note: Currently only non-composite CeedOperators with a single field and 1614eaf62fffSJeremy L Thompson composite CeedOperators with single field sub-operators are supported. 1615eaf62fffSJeremy L Thompson 1616f04ea552SJeremy L Thompson Note: Calling this function asserts that setup is complete 1617f04ea552SJeremy L Thompson and sets the CeedOperator as immutable. 1618f04ea552SJeremy L Thompson 1619eaf62fffSJeremy L Thompson @param op CeedOperator to assemble CeedQFunction 1620eaf62fffSJeremy L Thompson @param[out] assembled CeedVector to store assembled CeedOperator diagonal 1621eaf62fffSJeremy L Thompson @param request Address of CeedRequest for non-blocking completion, else 1622eaf62fffSJeremy L Thompson @ref CEED_REQUEST_IMMEDIATE 1623eaf62fffSJeremy L Thompson 1624eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1625eaf62fffSJeremy L Thompson 1626eaf62fffSJeremy L Thompson @ref User 1627eaf62fffSJeremy L Thompson **/ 1628eaf62fffSJeremy L Thompson int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, 1629eaf62fffSJeremy L Thompson CeedRequest *request) { 1630eaf62fffSJeremy L Thompson int ierr; 1631eaf62fffSJeremy L Thompson ierr = CeedOperatorCheckReady(op); CeedChk(ierr); 1632eaf62fffSJeremy L Thompson 1633c9366a6bSJeremy L Thompson CeedSize input_size = 0, output_size = 0; 1634c9366a6bSJeremy L Thompson ierr = CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size); 1635c9366a6bSJeremy L Thompson CeedChk(ierr); 1636c9366a6bSJeremy L Thompson if (input_size != output_size) 1637c9366a6bSJeremy L Thompson // LCOV_EXCL_START 1638c9366a6bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 1639c9366a6bSJeremy L Thompson // LCOV_EXCL_STOP 1640c9366a6bSJeremy L Thompson 1641eaf62fffSJeremy L Thompson if (op->LinearAssembleDiagonal) { 1642d04bbc78SJeremy L Thompson // Backend version 1643eaf62fffSJeremy L Thompson ierr = op->LinearAssembleDiagonal(op, assembled, request); CeedChk(ierr); 1644eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1645eaf62fffSJeremy L Thompson } else if (op->LinearAssembleAddDiagonal) { 1646d04bbc78SJeremy L Thompson // Backend version with zeroing first 1647eaf62fffSJeremy L Thompson ierr = CeedVectorSetValue(assembled, 0.0); CeedChk(ierr); 1648eaf62fffSJeremy L Thompson ierr = op->LinearAssembleAddDiagonal(op, assembled, request); CeedChk(ierr); 1649eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1650eaf62fffSJeremy L Thompson } else { 1651d04bbc78SJeremy L Thompson // Operator fallback 1652d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1653d04bbc78SJeremy L Thompson 1654d04bbc78SJeremy L Thompson ierr = CeedOperatorGetFallback(op, &op_fallback); CeedChk(ierr); 1655d04bbc78SJeremy L Thompson if (op_fallback) { 1656d04bbc78SJeremy L Thompson ierr = CeedOperatorLinearAssembleDiagonal(op_fallback, assembled, request); 1657eaf62fffSJeremy L Thompson CeedChk(ierr); 1658eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1659eaf62fffSJeremy L Thompson } 1660eaf62fffSJeremy L Thompson } 1661eaf62fffSJeremy L Thompson // Default interface implementation 1662eaf62fffSJeremy L Thompson ierr = CeedVectorSetValue(assembled, 0.0); CeedChk(ierr); 1663eaf62fffSJeremy L Thompson ierr = CeedOperatorLinearAssembleAddDiagonal(op, assembled, request); 1664eaf62fffSJeremy L Thompson CeedChk(ierr); 1665d04bbc78SJeremy L Thompson 1666eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1667eaf62fffSJeremy L Thompson } 1668eaf62fffSJeremy L Thompson 1669eaf62fffSJeremy L Thompson /** 1670eaf62fffSJeremy L Thompson @brief Assemble the diagonal of a square linear CeedOperator 1671eaf62fffSJeremy L Thompson 1672eaf62fffSJeremy L Thompson This sums into a CeedVector the diagonal of a linear CeedOperator. 1673eaf62fffSJeremy L Thompson 1674eaf62fffSJeremy L Thompson Note: Currently only non-composite CeedOperators with a single field and 1675eaf62fffSJeremy L Thompson composite CeedOperators with single field sub-operators are supported. 1676eaf62fffSJeremy L Thompson 1677f04ea552SJeremy L Thompson Note: Calling this function asserts that setup is complete 1678f04ea552SJeremy L Thompson and sets the CeedOperator as immutable. 1679f04ea552SJeremy L Thompson 1680eaf62fffSJeremy L Thompson @param op CeedOperator to assemble CeedQFunction 1681eaf62fffSJeremy L Thompson @param[out] assembled CeedVector to store assembled CeedOperator diagonal 1682eaf62fffSJeremy L Thompson @param request Address of CeedRequest for non-blocking completion, else 1683eaf62fffSJeremy L Thompson @ref CEED_REQUEST_IMMEDIATE 1684eaf62fffSJeremy L Thompson 1685eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1686eaf62fffSJeremy L Thompson 1687eaf62fffSJeremy L Thompson @ref User 1688eaf62fffSJeremy L Thompson **/ 1689eaf62fffSJeremy L Thompson int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, 1690eaf62fffSJeremy L Thompson CeedRequest *request) { 1691eaf62fffSJeremy L Thompson int ierr; 1692eaf62fffSJeremy L Thompson ierr = CeedOperatorCheckReady(op); CeedChk(ierr); 1693eaf62fffSJeremy L Thompson 1694c9366a6bSJeremy L Thompson CeedSize input_size = 0, output_size = 0; 1695c9366a6bSJeremy L Thompson ierr = CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size); 1696c9366a6bSJeremy L Thompson CeedChk(ierr); 1697c9366a6bSJeremy L Thompson if (input_size != output_size) 1698c9366a6bSJeremy L Thompson // LCOV_EXCL_START 1699c9366a6bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 1700c9366a6bSJeremy L Thompson // LCOV_EXCL_STOP 1701c9366a6bSJeremy L Thompson 1702eaf62fffSJeremy L Thompson if (op->LinearAssembleAddDiagonal) { 1703d04bbc78SJeremy L Thompson // Backend version 1704eaf62fffSJeremy L Thompson ierr = op->LinearAssembleAddDiagonal(op, assembled, request); CeedChk(ierr); 1705eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1706eaf62fffSJeremy L Thompson } else { 1707d04bbc78SJeremy L Thompson // Operator fallback 1708d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1709d04bbc78SJeremy L Thompson 1710d04bbc78SJeremy L Thompson ierr = CeedOperatorGetFallback(op, &op_fallback); CeedChk(ierr); 1711d04bbc78SJeremy L Thompson if (op_fallback) { 1712d04bbc78SJeremy L Thompson ierr = CeedOperatorLinearAssembleAddDiagonal(op_fallback, assembled, request); 1713eaf62fffSJeremy L Thompson CeedChk(ierr); 1714eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1715eaf62fffSJeremy L Thompson } 1716eaf62fffSJeremy L Thompson } 1717eaf62fffSJeremy L Thompson // Default interface implementation 1718eaf62fffSJeremy L Thompson bool is_composite; 1719eaf62fffSJeremy L Thompson ierr = CeedOperatorIsComposite(op, &is_composite); CeedChk(ierr); 1720eaf62fffSJeremy L Thompson if (is_composite) { 1721eaf62fffSJeremy L Thompson ierr = CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, 1722eaf62fffSJeremy L Thompson false, assembled); CeedChk(ierr); 1723eaf62fffSJeremy L Thompson } else { 17240e455453SJeremy L Thompson ierr = CeedSingleOperatorAssembleAddDiagonal_Core(op, request, false, 17250e455453SJeremy L Thompson assembled); CeedChk(ierr); 1726eaf62fffSJeremy L Thompson } 1727d04bbc78SJeremy L Thompson 1728d04bbc78SJeremy L Thompson return CEED_ERROR_SUCCESS; 1729eaf62fffSJeremy L Thompson } 1730eaf62fffSJeremy L Thompson 1731eaf62fffSJeremy L Thompson /** 1732eaf62fffSJeremy L Thompson @brief Assemble the point block diagonal of a square linear CeedOperator 1733eaf62fffSJeremy L Thompson 1734eaf62fffSJeremy L Thompson This overwrites a CeedVector with the point block diagonal of a linear 1735eaf62fffSJeremy L Thompson CeedOperator. 1736eaf62fffSJeremy L Thompson 1737eaf62fffSJeremy L Thompson Note: Currently only non-composite CeedOperators with a single field and 1738eaf62fffSJeremy L Thompson composite CeedOperators with single field sub-operators are supported. 1739eaf62fffSJeremy L Thompson 1740f04ea552SJeremy L Thompson Note: Calling this function asserts that setup is complete 1741f04ea552SJeremy L Thompson and sets the CeedOperator as immutable. 1742f04ea552SJeremy L Thompson 1743eaf62fffSJeremy L Thompson @param op CeedOperator to assemble CeedQFunction 1744eaf62fffSJeremy L Thompson @param[out] assembled CeedVector to store assembled CeedOperator point block 1745eaf62fffSJeremy L Thompson diagonal, provided in row-major form with an 1746eaf62fffSJeremy L Thompson @a num_comp * @a num_comp block at each node. The dimensions 1747eaf62fffSJeremy L Thompson of this vector are derived from the active vector 1748eaf62fffSJeremy L Thompson for the CeedOperator. The array has shape 1749eaf62fffSJeremy L Thompson [nodes, component out, component in]. 1750eaf62fffSJeremy L Thompson @param request Address of CeedRequest for non-blocking completion, else 1751eaf62fffSJeremy L Thompson CEED_REQUEST_IMMEDIATE 1752eaf62fffSJeremy L Thompson 1753eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1754eaf62fffSJeremy L Thompson 1755eaf62fffSJeremy L Thompson @ref User 1756eaf62fffSJeremy L Thompson **/ 1757eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, 1758eaf62fffSJeremy L Thompson CeedVector assembled, CeedRequest *request) { 1759eaf62fffSJeremy L Thompson int ierr; 1760eaf62fffSJeremy L Thompson ierr = CeedOperatorCheckReady(op); CeedChk(ierr); 1761eaf62fffSJeremy L Thompson 1762c9366a6bSJeremy L Thompson CeedSize input_size = 0, output_size = 0; 1763c9366a6bSJeremy L Thompson ierr = CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size); 1764c9366a6bSJeremy L Thompson CeedChk(ierr); 1765c9366a6bSJeremy L Thompson if (input_size != output_size) 1766c9366a6bSJeremy L Thompson // LCOV_EXCL_START 1767c9366a6bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 1768c9366a6bSJeremy L Thompson // LCOV_EXCL_STOP 1769c9366a6bSJeremy L Thompson 1770eaf62fffSJeremy L Thompson if (op->LinearAssemblePointBlockDiagonal) { 1771d04bbc78SJeremy L Thompson // Backend version 1772eaf62fffSJeremy L Thompson ierr = op->LinearAssemblePointBlockDiagonal(op, assembled, request); 1773eaf62fffSJeremy L Thompson CeedChk(ierr); 1774eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1775eaf62fffSJeremy L Thompson } else if (op->LinearAssembleAddPointBlockDiagonal) { 1776d04bbc78SJeremy L Thompson // Backend version with zeroing first 1777eaf62fffSJeremy L Thompson ierr = CeedVectorSetValue(assembled, 0.0); CeedChk(ierr); 1778eaf62fffSJeremy L Thompson ierr = CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, 1779eaf62fffSJeremy L Thompson request); CeedChk(ierr); 1780eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1781eaf62fffSJeremy L Thompson } else { 1782d04bbc78SJeremy L Thompson // Operator fallback 1783d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1784d04bbc78SJeremy L Thompson 1785d04bbc78SJeremy L Thompson ierr = CeedOperatorGetFallback(op, &op_fallback); CeedChk(ierr); 1786d04bbc78SJeremy L Thompson if (op_fallback) { 1787d04bbc78SJeremy L Thompson ierr = CeedOperatorLinearAssemblePointBlockDiagonal(op_fallback, assembled, 1788d04bbc78SJeremy L Thompson request); CeedChk(ierr); 1789eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1790eaf62fffSJeremy L Thompson } 1791eaf62fffSJeremy L Thompson } 1792eaf62fffSJeremy L Thompson // Default interface implementation 1793eaf62fffSJeremy L Thompson ierr = CeedVectorSetValue(assembled, 0.0); CeedChk(ierr); 1794eaf62fffSJeremy L Thompson ierr = CeedOperatorLinearAssembleAddPointBlockDiagonal(op, assembled, request); 1795eaf62fffSJeremy L Thompson CeedChk(ierr); 1796d04bbc78SJeremy L Thompson 1797eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1798eaf62fffSJeremy L Thompson } 1799eaf62fffSJeremy L Thompson 1800eaf62fffSJeremy L Thompson /** 1801eaf62fffSJeremy L Thompson @brief Assemble the point block diagonal of a square linear CeedOperator 1802eaf62fffSJeremy L Thompson 1803eaf62fffSJeremy L Thompson This sums into a CeedVector with the point block diagonal of a linear 1804eaf62fffSJeremy L Thompson CeedOperator. 1805eaf62fffSJeremy L Thompson 1806eaf62fffSJeremy L Thompson Note: Currently only non-composite CeedOperators with a single field and 1807eaf62fffSJeremy L Thompson composite CeedOperators with single field sub-operators are supported. 1808eaf62fffSJeremy L Thompson 1809f04ea552SJeremy L Thompson Note: Calling this function asserts that setup is complete 1810f04ea552SJeremy L Thompson and sets the CeedOperator as immutable. 1811f04ea552SJeremy L Thompson 1812eaf62fffSJeremy L Thompson @param op CeedOperator to assemble CeedQFunction 1813eaf62fffSJeremy L Thompson @param[out] assembled CeedVector to store assembled CeedOperator point block 1814eaf62fffSJeremy L Thompson diagonal, provided in row-major form with an 1815eaf62fffSJeremy L Thompson @a num_comp * @a num_comp block at each node. The dimensions 1816eaf62fffSJeremy L Thompson of this vector are derived from the active vector 1817eaf62fffSJeremy L Thompson for the CeedOperator. The array has shape 1818eaf62fffSJeremy L Thompson [nodes, component out, component in]. 1819eaf62fffSJeremy L Thompson @param request Address of CeedRequest for non-blocking completion, else 1820eaf62fffSJeremy L Thompson CEED_REQUEST_IMMEDIATE 1821eaf62fffSJeremy L Thompson 1822eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1823eaf62fffSJeremy L Thompson 1824eaf62fffSJeremy L Thompson @ref User 1825eaf62fffSJeremy L Thompson **/ 1826eaf62fffSJeremy L Thompson int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, 1827eaf62fffSJeremy L Thompson CeedVector assembled, CeedRequest *request) { 1828eaf62fffSJeremy L Thompson int ierr; 1829eaf62fffSJeremy L Thompson ierr = CeedOperatorCheckReady(op); CeedChk(ierr); 1830eaf62fffSJeremy L Thompson 1831c9366a6bSJeremy L Thompson CeedSize input_size = 0, output_size = 0; 1832c9366a6bSJeremy L Thompson ierr = CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size); 1833c9366a6bSJeremy L Thompson CeedChk(ierr); 1834c9366a6bSJeremy L Thompson if (input_size != output_size) 1835c9366a6bSJeremy L Thompson // LCOV_EXCL_START 1836c9366a6bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_DIMENSION, "Operator must be square"); 1837c9366a6bSJeremy L Thompson // LCOV_EXCL_STOP 1838c9366a6bSJeremy L Thompson 1839eaf62fffSJeremy L Thompson if (op->LinearAssembleAddPointBlockDiagonal) { 1840d04bbc78SJeremy L Thompson // Backend version 1841eaf62fffSJeremy L Thompson ierr = op->LinearAssembleAddPointBlockDiagonal(op, assembled, request); 1842eaf62fffSJeremy L Thompson CeedChk(ierr); 1843eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1844eaf62fffSJeremy L Thompson } else { 1845d04bbc78SJeremy L Thompson // Operator fallback 1846d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1847d04bbc78SJeremy L Thompson 1848d04bbc78SJeremy L Thompson ierr = CeedOperatorGetFallback(op, &op_fallback); CeedChk(ierr); 1849d04bbc78SJeremy L Thompson if (op_fallback) { 1850d04bbc78SJeremy L Thompson ierr = CeedOperatorLinearAssembleAddPointBlockDiagonal(op_fallback, assembled, 1851d04bbc78SJeremy L Thompson request); CeedChk(ierr); 1852eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1853eaf62fffSJeremy L Thompson } 1854eaf62fffSJeremy L Thompson } 1855eaf62fffSJeremy L Thompson // Default interface implemenation 1856eaf62fffSJeremy L Thompson bool is_composite; 1857eaf62fffSJeremy L Thompson ierr = CeedOperatorIsComposite(op, &is_composite); CeedChk(ierr); 1858eaf62fffSJeremy L Thompson if (is_composite) { 1859eaf62fffSJeremy L Thompson ierr = CeedCompositeOperatorLinearAssembleAddDiagonal(op, request, 1860eaf62fffSJeremy L Thompson true, assembled); CeedChk(ierr); 1861eaf62fffSJeremy L Thompson } else { 18620e455453SJeremy L Thompson ierr = CeedSingleOperatorAssembleAddDiagonal_Core(op, request, true, assembled); 1863eaf62fffSJeremy L Thompson CeedChk(ierr); 1864eaf62fffSJeremy L Thompson } 1865d04bbc78SJeremy L Thompson 1866d04bbc78SJeremy L Thompson return CEED_ERROR_SUCCESS; 1867eaf62fffSJeremy L Thompson } 1868eaf62fffSJeremy L Thompson 1869eaf62fffSJeremy L Thompson /** 1870eaf62fffSJeremy L Thompson @brief Fully assemble the nonzero pattern of a linear operator. 1871eaf62fffSJeremy L Thompson 1872eaf62fffSJeremy L Thompson Expected to be used in conjunction with CeedOperatorLinearAssemble() 1873eaf62fffSJeremy L Thompson 1874eaf62fffSJeremy L Thompson The assembly routines use coordinate format, with num_entries tuples of the 1875eaf62fffSJeremy L Thompson form (i, j, value) which indicate that value should be added to the matrix 1876eaf62fffSJeremy L Thompson in entry (i, j). Note that the (i, j) pairs are not unique and may repeat. 1877eaf62fffSJeremy L Thompson This function returns the number of entries and their (i, j) locations, 1878eaf62fffSJeremy L Thompson while CeedOperatorLinearAssemble() provides the values in the same 1879eaf62fffSJeremy L Thompson ordering. 1880eaf62fffSJeremy L Thompson 1881eaf62fffSJeremy L Thompson This will generally be slow unless your operator is low-order. 1882eaf62fffSJeremy L Thompson 1883f04ea552SJeremy L Thompson Note: Calling this function asserts that setup is complete 1884f04ea552SJeremy L Thompson and sets the CeedOperator as immutable. 1885f04ea552SJeremy L Thompson 1886eaf62fffSJeremy L Thompson @param[in] op CeedOperator to assemble 1887eaf62fffSJeremy L Thompson @param[out] num_entries Number of entries in coordinate nonzero pattern 1888eaf62fffSJeremy L Thompson @param[out] rows Row number for each entry 1889eaf62fffSJeremy L Thompson @param[out] cols Column number for each entry 1890eaf62fffSJeremy L Thompson 1891eaf62fffSJeremy L Thompson @ref User 1892eaf62fffSJeremy L Thompson **/ 1893c30b7fbdSJeremy L Thompson int CeedOperatorLinearAssembleSymbolic(CeedOperator op, CeedSize *num_entries, 1894eaf62fffSJeremy L Thompson CeedInt **rows, CeedInt **cols) { 1895eaf62fffSJeremy L Thompson int ierr; 1896eaf62fffSJeremy L Thompson CeedInt num_suboperators, single_entries; 1897eaf62fffSJeremy L Thompson CeedOperator *sub_operators; 1898eaf62fffSJeremy L Thompson bool is_composite; 1899eaf62fffSJeremy L Thompson ierr = CeedOperatorCheckReady(op); CeedChk(ierr); 1900eaf62fffSJeremy L Thompson 1901eaf62fffSJeremy L Thompson if (op->LinearAssembleSymbolic) { 1902d04bbc78SJeremy L Thompson // Backend version 1903eaf62fffSJeremy L Thompson ierr = op->LinearAssembleSymbolic(op, num_entries, rows, cols); CeedChk(ierr); 1904eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1905eaf62fffSJeremy L Thompson } else { 1906d04bbc78SJeremy L Thompson // Operator fallback 1907d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1908d04bbc78SJeremy L Thompson 1909d04bbc78SJeremy L Thompson ierr = CeedOperatorGetFallback(op, &op_fallback); CeedChk(ierr); 1910d04bbc78SJeremy L Thompson if (op_fallback) { 1911d04bbc78SJeremy L Thompson ierr = CeedOperatorLinearAssembleSymbolic(op_fallback, num_entries, rows, cols); 1912eaf62fffSJeremy L Thompson CeedChk(ierr); 1913eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1914eaf62fffSJeremy L Thompson } 1915eaf62fffSJeremy L Thompson } 1916eaf62fffSJeremy L Thompson 1917eaf62fffSJeremy L Thompson // Default interface implementation 1918eaf62fffSJeremy L Thompson 1919eaf62fffSJeremy L Thompson // count entries and allocate rows, cols arrays 1920eaf62fffSJeremy L Thompson ierr = CeedOperatorIsComposite(op, &is_composite); CeedChk(ierr); 1921eaf62fffSJeremy L Thompson *num_entries = 0; 1922eaf62fffSJeremy L Thompson if (is_composite) { 1923eaf62fffSJeremy L Thompson ierr = CeedOperatorGetNumSub(op, &num_suboperators); CeedChk(ierr); 1924eaf62fffSJeremy L Thompson ierr = CeedOperatorGetSubList(op, &sub_operators); CeedChk(ierr); 192592ae7e47SJeremy L Thompson for (CeedInt k = 0; k < num_suboperators; ++k) { 1926eaf62fffSJeremy L Thompson ierr = CeedSingleOperatorAssemblyCountEntries(sub_operators[k], 1927eaf62fffSJeremy L Thompson &single_entries); CeedChk(ierr); 1928eaf62fffSJeremy L Thompson *num_entries += single_entries; 1929eaf62fffSJeremy L Thompson } 1930eaf62fffSJeremy L Thompson } else { 1931eaf62fffSJeremy L Thompson ierr = CeedSingleOperatorAssemblyCountEntries(op, 1932eaf62fffSJeremy L Thompson &single_entries); CeedChk(ierr); 1933eaf62fffSJeremy L Thompson *num_entries += single_entries; 1934eaf62fffSJeremy L Thompson } 1935eaf62fffSJeremy L Thompson ierr = CeedCalloc(*num_entries, rows); CeedChk(ierr); 1936eaf62fffSJeremy L Thompson ierr = CeedCalloc(*num_entries, cols); CeedChk(ierr); 1937eaf62fffSJeremy L Thompson 1938eaf62fffSJeremy L Thompson // assemble nonzero locations 1939eaf62fffSJeremy L Thompson CeedInt offset = 0; 1940eaf62fffSJeremy L Thompson if (is_composite) { 1941eaf62fffSJeremy L Thompson ierr = CeedOperatorGetNumSub(op, &num_suboperators); CeedChk(ierr); 1942eaf62fffSJeremy L Thompson ierr = CeedOperatorGetSubList(op, &sub_operators); CeedChk(ierr); 194392ae7e47SJeremy L Thompson for (CeedInt k = 0; k < num_suboperators; ++k) { 1944eaf62fffSJeremy L Thompson ierr = CeedSingleOperatorAssembleSymbolic(sub_operators[k], offset, *rows, 1945eaf62fffSJeremy L Thompson *cols); CeedChk(ierr); 1946eaf62fffSJeremy L Thompson ierr = CeedSingleOperatorAssemblyCountEntries(sub_operators[k], 1947eaf62fffSJeremy L Thompson &single_entries); 1948eaf62fffSJeremy L Thompson CeedChk(ierr); 1949eaf62fffSJeremy L Thompson offset += single_entries; 1950eaf62fffSJeremy L Thompson } 1951eaf62fffSJeremy L Thompson } else { 1952eaf62fffSJeremy L Thompson ierr = CeedSingleOperatorAssembleSymbolic(op, offset, *rows, *cols); 1953eaf62fffSJeremy L Thompson CeedChk(ierr); 1954eaf62fffSJeremy L Thompson } 1955eaf62fffSJeremy L Thompson 1956eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1957eaf62fffSJeremy L Thompson } 1958eaf62fffSJeremy L Thompson 1959eaf62fffSJeremy L Thompson /** 1960eaf62fffSJeremy L Thompson @brief Fully assemble the nonzero entries of a linear operator. 1961eaf62fffSJeremy L Thompson 1962eaf62fffSJeremy L Thompson Expected to be used in conjunction with CeedOperatorLinearAssembleSymbolic() 1963eaf62fffSJeremy L Thompson 1964eaf62fffSJeremy L Thompson The assembly routines use coordinate format, with num_entries tuples of the 1965eaf62fffSJeremy L Thompson form (i, j, value) which indicate that value should be added to the matrix 1966eaf62fffSJeremy L Thompson in entry (i, j). Note that the (i, j) pairs are not unique and may repeat. 1967eaf62fffSJeremy L Thompson This function returns the values of the nonzero entries to be added, their 1968eaf62fffSJeremy L Thompson (i, j) locations are provided by CeedOperatorLinearAssembleSymbolic() 1969eaf62fffSJeremy L Thompson 1970eaf62fffSJeremy L Thompson This will generally be slow unless your operator is low-order. 1971eaf62fffSJeremy L Thompson 1972f04ea552SJeremy L Thompson Note: Calling this function asserts that setup is complete 1973f04ea552SJeremy L Thompson and sets the CeedOperator as immutable. 1974f04ea552SJeremy L Thompson 1975eaf62fffSJeremy L Thompson @param[in] op CeedOperator to assemble 1976eaf62fffSJeremy L Thompson @param[out] values Values to assemble into matrix 1977eaf62fffSJeremy L Thompson 1978eaf62fffSJeremy L Thompson @ref User 1979eaf62fffSJeremy L Thompson **/ 1980eaf62fffSJeremy L Thompson int CeedOperatorLinearAssemble(CeedOperator op, CeedVector values) { 1981eaf62fffSJeremy L Thompson int ierr; 1982eaf62fffSJeremy L Thompson CeedInt num_suboperators, single_entries = 0; 1983eaf62fffSJeremy L Thompson CeedOperator *sub_operators; 1984eaf62fffSJeremy L Thompson ierr = CeedOperatorCheckReady(op); CeedChk(ierr); 1985eaf62fffSJeremy L Thompson 1986eaf62fffSJeremy L Thompson if (op->LinearAssemble) { 1987d04bbc78SJeremy L Thompson // Backend version 1988eaf62fffSJeremy L Thompson ierr = op->LinearAssemble(op, values); CeedChk(ierr); 1989eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1990eaf62fffSJeremy L Thompson } else { 1991d04bbc78SJeremy L Thompson // Operator fallback 1992d04bbc78SJeremy L Thompson CeedOperator op_fallback; 1993d04bbc78SJeremy L Thompson 1994d04bbc78SJeremy L Thompson ierr = CeedOperatorGetFallback(op, &op_fallback); CeedChk(ierr); 1995d04bbc78SJeremy L Thompson if (op_fallback) { 1996d04bbc78SJeremy L Thompson ierr = CeedOperatorLinearAssemble(op_fallback, values); CeedChk(ierr); 1997eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 1998eaf62fffSJeremy L Thompson } 1999eaf62fffSJeremy L Thompson } 2000eaf62fffSJeremy L Thompson 2001eaf62fffSJeremy L Thompson // Default interface implementation 2002eaf62fffSJeremy L Thompson bool is_composite; 2003eaf62fffSJeremy L Thompson ierr = CeedOperatorIsComposite(op, &is_composite); CeedChk(ierr); 2004eaf62fffSJeremy L Thompson 2005eaf62fffSJeremy L Thompson CeedInt offset = 0; 2006eaf62fffSJeremy L Thompson if (is_composite) { 2007eaf62fffSJeremy L Thompson ierr = CeedOperatorGetNumSub(op, &num_suboperators); CeedChk(ierr); 2008eaf62fffSJeremy L Thompson ierr = CeedOperatorGetSubList(op, &sub_operators); CeedChk(ierr); 2009cefa2673SJeremy L Thompson for (CeedInt k = 0; k < num_suboperators; k++) { 2010eaf62fffSJeremy L Thompson ierr = CeedSingleOperatorAssemble(sub_operators[k], offset, values); 2011eaf62fffSJeremy L Thompson CeedChk(ierr); 2012eaf62fffSJeremy L Thompson ierr = CeedSingleOperatorAssemblyCountEntries(sub_operators[k], 2013eaf62fffSJeremy L Thompson &single_entries); 2014eaf62fffSJeremy L Thompson CeedChk(ierr); 2015eaf62fffSJeremy L Thompson offset += single_entries; 2016eaf62fffSJeremy L Thompson } 2017eaf62fffSJeremy L Thompson } else { 2018eaf62fffSJeremy L Thompson ierr = CeedSingleOperatorAssemble(op, offset, values); CeedChk(ierr); 2019eaf62fffSJeremy L Thompson } 2020eaf62fffSJeremy L Thompson 2021eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2022eaf62fffSJeremy L Thompson } 2023eaf62fffSJeremy L Thompson 2024eaf62fffSJeremy L Thompson /** 2025eaf62fffSJeremy L Thompson @brief Create a multigrid coarse operator and level transfer operators 2026eaf62fffSJeremy L Thompson for a CeedOperator, creating the prolongation basis from the 2027eaf62fffSJeremy L Thompson fine and coarse grid interpolation 2028eaf62fffSJeremy L Thompson 2029f04ea552SJeremy L Thompson Note: Calling this function asserts that setup is complete 2030f04ea552SJeremy L Thompson and sets the CeedOperator as immutable. 2031f04ea552SJeremy L Thompson 2032eaf62fffSJeremy L Thompson @param[in] op_fine Fine grid operator 2033eaf62fffSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter 2034eaf62fffSJeremy L Thompson @param[in] rstr_coarse Coarse grid restriction 2035eaf62fffSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector basis 2036eaf62fffSJeremy L Thompson @param[out] op_coarse Coarse grid operator 2037eaf62fffSJeremy L Thompson @param[out] op_prolong Coarse to fine operator 2038eaf62fffSJeremy L Thompson @param[out] op_restrict Fine to coarse operator 2039eaf62fffSJeremy L Thompson 2040eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2041eaf62fffSJeremy L Thompson 2042eaf62fffSJeremy L Thompson @ref User 2043eaf62fffSJeremy L Thompson **/ 2044eaf62fffSJeremy L Thompson int CeedOperatorMultigridLevelCreate(CeedOperator op_fine, 2045eaf62fffSJeremy L Thompson CeedVector p_mult_fine, 2046eaf62fffSJeremy L Thompson CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 2047eaf62fffSJeremy L Thompson CeedOperator *op_coarse, CeedOperator *op_prolong, 2048eaf62fffSJeremy L Thompson CeedOperator *op_restrict) { 2049eaf62fffSJeremy L Thompson int ierr; 2050f04ea552SJeremy L Thompson ierr = CeedOperatorCheckReady(op_fine); CeedChk(ierr); 2051eaf62fffSJeremy L Thompson 2052f113e5dcSJeremy L Thompson // Build prolongation matrix 2053f113e5dcSJeremy L Thompson CeedBasis basis_fine, basis_c_to_f; 2054eaf62fffSJeremy L Thompson ierr = CeedOperatorGetActiveBasis(op_fine, &basis_fine); CeedChk(ierr); 2055*446e7af4SJeremy L Thompson ierr = CeedBasisCreateProjection(basis_coarse, basis_fine, &basis_c_to_f); 2056eaf62fffSJeremy L Thompson CeedChk(ierr); 2057eaf62fffSJeremy L Thompson 2058f113e5dcSJeremy L Thompson // Core code 2059f113e5dcSJeremy L Thompson ierr = CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, 2060f113e5dcSJeremy L Thompson basis_coarse, basis_c_to_f, op_coarse, 2061f113e5dcSJeremy L Thompson op_prolong, op_restrict); 2062f113e5dcSJeremy L Thompson CeedChk(ierr); 2063f113e5dcSJeremy L Thompson 2064eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2065eaf62fffSJeremy L Thompson } 2066eaf62fffSJeremy L Thompson 2067eaf62fffSJeremy L Thompson /** 2068eaf62fffSJeremy L Thompson @brief Create a multigrid coarse operator and level transfer operators 2069eaf62fffSJeremy L Thompson for a CeedOperator with a tensor basis for the active basis 2070eaf62fffSJeremy L Thompson 2071f04ea552SJeremy L Thompson Note: Calling this function asserts that setup is complete 2072f04ea552SJeremy L Thompson and sets the CeedOperator as immutable. 2073f04ea552SJeremy L Thompson 2074eaf62fffSJeremy L Thompson @param[in] op_fine Fine grid operator 2075eaf62fffSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter 2076eaf62fffSJeremy L Thompson @param[in] rstr_coarse Coarse grid restriction 2077eaf62fffSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector basis 2078eaf62fffSJeremy L Thompson @param[in] interp_c_to_f Matrix for coarse to fine interpolation 2079eaf62fffSJeremy L Thompson @param[out] op_coarse Coarse grid operator 2080eaf62fffSJeremy L Thompson @param[out] op_prolong Coarse to fine operator 2081eaf62fffSJeremy L Thompson @param[out] op_restrict Fine to coarse operator 2082eaf62fffSJeremy L Thompson 2083eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2084eaf62fffSJeremy L Thompson 2085eaf62fffSJeremy L Thompson @ref User 2086eaf62fffSJeremy L Thompson **/ 2087eaf62fffSJeremy L Thompson int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, 2088eaf62fffSJeremy L Thompson CeedVector p_mult_fine, CeedElemRestriction rstr_coarse, CeedBasis basis_coarse, 2089eaf62fffSJeremy L Thompson const CeedScalar *interp_c_to_f, CeedOperator *op_coarse, 2090eaf62fffSJeremy L Thompson CeedOperator *op_prolong, CeedOperator *op_restrict) { 2091eaf62fffSJeremy L Thompson int ierr; 2092f04ea552SJeremy L Thompson ierr = CeedOperatorCheckReady(op_fine); CeedChk(ierr); 2093eaf62fffSJeremy L Thompson Ceed ceed; 2094eaf62fffSJeremy L Thompson ierr = CeedOperatorGetCeed(op_fine, &ceed); CeedChk(ierr); 2095eaf62fffSJeremy L Thompson 2096eaf62fffSJeremy L Thompson // Check for compatible quadrature spaces 2097eaf62fffSJeremy L Thompson CeedBasis basis_fine; 2098eaf62fffSJeremy L Thompson ierr = CeedOperatorGetActiveBasis(op_fine, &basis_fine); CeedChk(ierr); 2099eaf62fffSJeremy L Thompson CeedInt Q_f, Q_c; 2100eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f); CeedChk(ierr); 2101eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c); CeedChk(ierr); 2102eaf62fffSJeremy L Thompson if (Q_f != Q_c) 2103eaf62fffSJeremy L Thompson // LCOV_EXCL_START 2104eaf62fffSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 2105eaf62fffSJeremy L Thompson "Bases must have compatible quadrature spaces"); 2106eaf62fffSJeremy L Thompson // LCOV_EXCL_STOP 2107eaf62fffSJeremy L Thompson 2108eaf62fffSJeremy L Thompson // Coarse to fine basis 2109eaf62fffSJeremy L Thompson CeedInt dim, num_comp, num_nodes_c, P_1d_f, P_1d_c; 2110eaf62fffSJeremy L Thompson ierr = CeedBasisGetDimension(basis_fine, &dim); CeedChk(ierr); 2111eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumComponents(basis_fine, &num_comp); CeedChk(ierr); 2112eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumNodes1D(basis_fine, &P_1d_f); CeedChk(ierr); 2113eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c); 2114eaf62fffSJeremy L Thompson CeedChk(ierr); 2115eaf62fffSJeremy L Thompson P_1d_c = dim == 1 ? num_nodes_c : 2116eaf62fffSJeremy L Thompson dim == 2 ? sqrt(num_nodes_c) : 2117eaf62fffSJeremy L Thompson cbrt(num_nodes_c); 2118eaf62fffSJeremy L Thompson CeedScalar *q_ref, *q_weight, *grad; 2119eaf62fffSJeremy L Thompson ierr = CeedCalloc(P_1d_f, &q_ref); CeedChk(ierr); 2120eaf62fffSJeremy L Thompson ierr = CeedCalloc(P_1d_f, &q_weight); CeedChk(ierr); 2121eaf62fffSJeremy L Thompson ierr = CeedCalloc(P_1d_f*P_1d_c*dim, &grad); CeedChk(ierr); 2122eaf62fffSJeremy L Thompson CeedBasis basis_c_to_f; 2123eaf62fffSJeremy L Thompson ierr = CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d_c, P_1d_f, 2124eaf62fffSJeremy L Thompson interp_c_to_f, grad, q_ref, q_weight, &basis_c_to_f); 2125eaf62fffSJeremy L Thompson CeedChk(ierr); 2126eaf62fffSJeremy L Thompson ierr = CeedFree(&q_ref); CeedChk(ierr); 2127eaf62fffSJeremy L Thompson ierr = CeedFree(&q_weight); CeedChk(ierr); 2128eaf62fffSJeremy L Thompson ierr = CeedFree(&grad); CeedChk(ierr); 2129eaf62fffSJeremy L Thompson 2130eaf62fffSJeremy L Thompson // Core code 2131eaf62fffSJeremy L Thompson ierr = CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, 2132eaf62fffSJeremy L Thompson basis_coarse, basis_c_to_f, op_coarse, 2133eaf62fffSJeremy L Thompson op_prolong, op_restrict); 2134eaf62fffSJeremy L Thompson CeedChk(ierr); 2135eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2136eaf62fffSJeremy L Thompson } 2137eaf62fffSJeremy L Thompson 2138eaf62fffSJeremy L Thompson /** 2139eaf62fffSJeremy L Thompson @brief Create a multigrid coarse operator and level transfer operators 2140eaf62fffSJeremy L Thompson for a CeedOperator with a non-tensor basis for the active vector 2141eaf62fffSJeremy L Thompson 2142f04ea552SJeremy L Thompson Note: Calling this function asserts that setup is complete 2143f04ea552SJeremy L Thompson and sets the CeedOperator as immutable. 2144f04ea552SJeremy L Thompson 2145eaf62fffSJeremy L Thompson @param[in] op_fine Fine grid operator 2146eaf62fffSJeremy L Thompson @param[in] p_mult_fine L-vector multiplicity in parallel gather/scatter 2147eaf62fffSJeremy L Thompson @param[in] rstr_coarse Coarse grid restriction 2148eaf62fffSJeremy L Thompson @param[in] basis_coarse Coarse grid active vector basis 2149eaf62fffSJeremy L Thompson @param[in] interp_c_to_f Matrix for coarse to fine interpolation 2150eaf62fffSJeremy L Thompson @param[out] op_coarse Coarse grid operator 2151eaf62fffSJeremy L Thompson @param[out] op_prolong Coarse to fine operator 2152eaf62fffSJeremy L Thompson @param[out] op_restrict Fine to coarse operator 2153eaf62fffSJeremy L Thompson 2154eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2155eaf62fffSJeremy L Thompson 2156eaf62fffSJeremy L Thompson @ref User 2157eaf62fffSJeremy L Thompson **/ 2158eaf62fffSJeremy L Thompson int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, 2159eaf62fffSJeremy L Thompson CeedVector p_mult_fine, 2160eaf62fffSJeremy L Thompson CeedElemRestriction rstr_coarse, 2161eaf62fffSJeremy L Thompson CeedBasis basis_coarse, 2162eaf62fffSJeremy L Thompson const CeedScalar *interp_c_to_f, 2163eaf62fffSJeremy L Thompson CeedOperator *op_coarse, 2164eaf62fffSJeremy L Thompson CeedOperator *op_prolong, 2165eaf62fffSJeremy L Thompson CeedOperator *op_restrict) { 2166eaf62fffSJeremy L Thompson int ierr; 2167f04ea552SJeremy L Thompson ierr = CeedOperatorCheckReady(op_fine); CeedChk(ierr); 2168eaf62fffSJeremy L Thompson Ceed ceed; 2169eaf62fffSJeremy L Thompson ierr = CeedOperatorGetCeed(op_fine, &ceed); CeedChk(ierr); 2170eaf62fffSJeremy L Thompson 2171eaf62fffSJeremy L Thompson // Check for compatible quadrature spaces 2172eaf62fffSJeremy L Thompson CeedBasis basis_fine; 2173eaf62fffSJeremy L Thompson ierr = CeedOperatorGetActiveBasis(op_fine, &basis_fine); CeedChk(ierr); 2174eaf62fffSJeremy L Thompson CeedInt Q_f, Q_c; 2175eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints(basis_fine, &Q_f); CeedChk(ierr); 2176eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints(basis_coarse, &Q_c); CeedChk(ierr); 2177eaf62fffSJeremy L Thompson if (Q_f != Q_c) 2178eaf62fffSJeremy L Thompson // LCOV_EXCL_START 2179eaf62fffSJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 2180eaf62fffSJeremy L Thompson "Bases must have compatible quadrature spaces"); 2181eaf62fffSJeremy L Thompson // LCOV_EXCL_STOP 2182eaf62fffSJeremy L Thompson 2183eaf62fffSJeremy L Thompson // Coarse to fine basis 2184eaf62fffSJeremy L Thompson CeedElemTopology topo; 2185eaf62fffSJeremy L Thompson ierr = CeedBasisGetTopology(basis_fine, &topo); CeedChk(ierr); 2186eaf62fffSJeremy L Thompson CeedInt dim, num_comp, num_nodes_c, num_nodes_f; 2187eaf62fffSJeremy L Thompson ierr = CeedBasisGetDimension(basis_fine, &dim); CeedChk(ierr); 2188eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumComponents(basis_fine, &num_comp); CeedChk(ierr); 2189eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumNodes(basis_fine, &num_nodes_f); CeedChk(ierr); 2190eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetElementSize(rstr_coarse, &num_nodes_c); 2191eaf62fffSJeremy L Thompson CeedChk(ierr); 2192eaf62fffSJeremy L Thompson CeedScalar *q_ref, *q_weight, *grad; 2193f9e0419eSJeremy L Thompson ierr = CeedCalloc(num_nodes_f*dim, &q_ref); CeedChk(ierr); 2194eaf62fffSJeremy L Thompson ierr = CeedCalloc(num_nodes_f, &q_weight); CeedChk(ierr); 2195eaf62fffSJeremy L Thompson ierr = CeedCalloc(num_nodes_f*num_nodes_c*dim, &grad); CeedChk(ierr); 2196eaf62fffSJeremy L Thompson CeedBasis basis_c_to_f; 2197eaf62fffSJeremy L Thompson ierr = CeedBasisCreateH1(ceed, topo, num_comp, num_nodes_c, num_nodes_f, 2198eaf62fffSJeremy L Thompson interp_c_to_f, grad, q_ref, q_weight, &basis_c_to_f); 2199eaf62fffSJeremy L Thompson CeedChk(ierr); 2200eaf62fffSJeremy L Thompson ierr = CeedFree(&q_ref); CeedChk(ierr); 2201eaf62fffSJeremy L Thompson ierr = CeedFree(&q_weight); CeedChk(ierr); 2202eaf62fffSJeremy L Thompson ierr = CeedFree(&grad); CeedChk(ierr); 2203eaf62fffSJeremy L Thompson 2204eaf62fffSJeremy L Thompson // Core code 2205eaf62fffSJeremy L Thompson ierr = CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, 2206eaf62fffSJeremy L Thompson basis_coarse, basis_c_to_f, op_coarse, 2207eaf62fffSJeremy L Thompson op_prolong, op_restrict); 2208eaf62fffSJeremy L Thompson CeedChk(ierr); 2209eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2210eaf62fffSJeremy L Thompson } 2211eaf62fffSJeremy L Thompson 2212eaf62fffSJeremy L Thompson /** 2213eaf62fffSJeremy L Thompson @brief Build a FDM based approximate inverse for each element for a 2214eaf62fffSJeremy L Thompson CeedOperator 2215eaf62fffSJeremy L Thompson 2216eaf62fffSJeremy L Thompson This returns a CeedOperator and CeedVector to apply a Fast Diagonalization 2217eaf62fffSJeremy L Thompson Method based approximate inverse. This function obtains the simultaneous 2218eaf62fffSJeremy L Thompson diagonalization for the 1D mass and Laplacian operators, 2219eaf62fffSJeremy L Thompson M = V^T V, K = V^T S V. 2220eaf62fffSJeremy L Thompson The assembled QFunction is used to modify the eigenvalues from simultaneous 2221eaf62fffSJeremy L Thompson diagonalization and obtain an approximate inverse of the form 2222eaf62fffSJeremy L Thompson V^T S^hat V. The CeedOperator must be linear and non-composite. The 2223eaf62fffSJeremy L Thompson associated CeedQFunction must therefore also be linear. 2224eaf62fffSJeremy L Thompson 2225f04ea552SJeremy L Thompson Note: Calling this function asserts that setup is complete 2226f04ea552SJeremy L Thompson and sets the CeedOperator as immutable. 2227f04ea552SJeremy L Thompson 2228eaf62fffSJeremy L Thompson @param op CeedOperator to create element inverses 2229eaf62fffSJeremy L Thompson @param[out] fdm_inv CeedOperator to apply the action of a FDM based inverse 2230eaf62fffSJeremy L Thompson for each element 2231eaf62fffSJeremy L Thompson @param request Address of CeedRequest for non-blocking completion, else 2232eaf62fffSJeremy L Thompson @ref CEED_REQUEST_IMMEDIATE 2233eaf62fffSJeremy L Thompson 2234eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2235eaf62fffSJeremy L Thompson 2236480fae85SJeremy L Thompson @ref User 2237eaf62fffSJeremy L Thompson **/ 2238eaf62fffSJeremy L Thompson int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, 2239eaf62fffSJeremy L Thompson CeedRequest *request) { 2240eaf62fffSJeremy L Thompson int ierr; 2241eaf62fffSJeremy L Thompson ierr = CeedOperatorCheckReady(op); CeedChk(ierr); 2242eaf62fffSJeremy L Thompson 2243eaf62fffSJeremy L Thompson if (op->CreateFDMElementInverse) { 2244d04bbc78SJeremy L Thompson // Backend version 2245eaf62fffSJeremy L Thompson ierr = op->CreateFDMElementInverse(op, fdm_inv, request); CeedChk(ierr); 2246eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2247eaf62fffSJeremy L Thompson } else { 2248d04bbc78SJeremy L Thompson // Operator fallback 2249d04bbc78SJeremy L Thompson CeedOperator op_fallback; 2250d04bbc78SJeremy L Thompson 2251d04bbc78SJeremy L Thompson ierr = CeedOperatorGetFallback(op, &op_fallback); CeedChk(ierr); 2252d04bbc78SJeremy L Thompson if (op_fallback) { 2253d04bbc78SJeremy L Thompson ierr = CeedOperatorCreateFDMElementInverse(op_fallback, fdm_inv, request); 2254eaf62fffSJeremy L Thompson CeedChk(ierr); 2255eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2256eaf62fffSJeremy L Thompson } 2257eaf62fffSJeremy L Thompson } 2258eaf62fffSJeremy L Thompson 2259d04bbc78SJeremy L Thompson // Default interface implementation 2260eaf62fffSJeremy L Thompson Ceed ceed, ceed_parent; 2261eaf62fffSJeremy L Thompson ierr = CeedOperatorGetCeed(op, &ceed); CeedChk(ierr); 2262eaf62fffSJeremy L Thompson ierr = CeedGetOperatorFallbackParentCeed(ceed, &ceed_parent); CeedChk(ierr); 2263eaf62fffSJeremy L Thompson ceed_parent = ceed_parent ? ceed_parent : ceed; 2264eaf62fffSJeremy L Thompson CeedQFunction qf; 2265eaf62fffSJeremy L Thompson ierr = CeedOperatorGetQFunction(op, &qf); CeedChk(ierr); 2266eaf62fffSJeremy L Thompson 2267eaf62fffSJeremy L Thompson // Determine active input basis 2268eaf62fffSJeremy L Thompson bool interp = false, grad = false; 2269eaf62fffSJeremy L Thompson CeedBasis basis = NULL; 2270eaf62fffSJeremy L Thompson CeedElemRestriction rstr = NULL; 2271eaf62fffSJeremy L Thompson CeedOperatorField *op_fields; 2272eaf62fffSJeremy L Thompson CeedQFunctionField *qf_fields; 2273eaf62fffSJeremy L Thompson CeedInt num_input_fields; 22747e7773b5SJeremy L Thompson ierr = CeedOperatorGetFields(op, &num_input_fields, &op_fields, NULL, NULL); 22757e7773b5SJeremy L Thompson CeedChk(ierr); 22767e7773b5SJeremy L Thompson ierr = CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL); CeedChk(ierr); 2277eaf62fffSJeremy L Thompson for (CeedInt i=0; i<num_input_fields; i++) { 2278eaf62fffSJeremy L Thompson CeedVector vec; 2279eaf62fffSJeremy L Thompson ierr = CeedOperatorFieldGetVector(op_fields[i], &vec); CeedChk(ierr); 2280eaf62fffSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) { 2281eaf62fffSJeremy L Thompson CeedEvalMode eval_mode; 2282eaf62fffSJeremy L Thompson ierr = CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode); CeedChk(ierr); 2283eaf62fffSJeremy L Thompson interp = interp || eval_mode == CEED_EVAL_INTERP; 2284eaf62fffSJeremy L Thompson grad = grad || eval_mode == CEED_EVAL_GRAD; 2285eaf62fffSJeremy L Thompson ierr = CeedOperatorFieldGetBasis(op_fields[i], &basis); CeedChk(ierr); 2286eaf62fffSJeremy L Thompson ierr = CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr); CeedChk(ierr); 2287eaf62fffSJeremy L Thompson } 2288eaf62fffSJeremy L Thompson } 2289eaf62fffSJeremy L Thompson if (!basis) 2290eaf62fffSJeremy L Thompson // LCOV_EXCL_START 2291eaf62fffSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "No active field set"); 2292eaf62fffSJeremy L Thompson // LCOV_EXCL_STOP 2293e79b91d9SJeremy L Thompson CeedSize l_size = 1; 2294e79b91d9SJeremy L Thompson CeedInt P_1d, Q_1d, elem_size, num_qpts, dim, num_comp = 1, num_elem = 1; 2295eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumNodes1D(basis, &P_1d); CeedChk(ierr); 2296eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumNodes(basis, &elem_size); CeedChk(ierr); 2297eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d); CeedChk(ierr); 2298eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumQuadraturePoints(basis, &num_qpts); CeedChk(ierr); 2299eaf62fffSJeremy L Thompson ierr = CeedBasisGetDimension(basis, &dim); CeedChk(ierr); 2300eaf62fffSJeremy L Thompson ierr = CeedBasisGetNumComponents(basis, &num_comp); CeedChk(ierr); 2301eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetNumElements(rstr, &num_elem); CeedChk(ierr); 2302eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetLVectorSize(rstr, &l_size); CeedChk(ierr); 2303eaf62fffSJeremy L Thompson 2304eaf62fffSJeremy L Thompson // Build and diagonalize 1D Mass and Laplacian 2305eaf62fffSJeremy L Thompson bool tensor_basis; 2306eaf62fffSJeremy L Thompson ierr = CeedBasisIsTensor(basis, &tensor_basis); CeedChk(ierr); 2307eaf62fffSJeremy L Thompson if (!tensor_basis) 2308eaf62fffSJeremy L Thompson // LCOV_EXCL_START 2309eaf62fffSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, 2310eaf62fffSJeremy L Thompson "FDMElementInverse only supported for tensor " 2311eaf62fffSJeremy L Thompson "bases"); 2312eaf62fffSJeremy L Thompson // LCOV_EXCL_STOP 2313eaf62fffSJeremy L Thompson CeedScalar *mass, *laplace, *x, *fdm_interp, *lambda; 2314eaf62fffSJeremy L Thompson ierr = CeedCalloc(P_1d*P_1d, &mass); CeedChk(ierr); 2315eaf62fffSJeremy L Thompson ierr = CeedCalloc(P_1d*P_1d, &laplace); CeedChk(ierr); 2316eaf62fffSJeremy L Thompson ierr = CeedCalloc(P_1d*P_1d, &x); CeedChk(ierr); 2317eaf62fffSJeremy L Thompson ierr = CeedCalloc(P_1d*P_1d, &fdm_interp); CeedChk(ierr); 2318eaf62fffSJeremy L Thompson ierr = CeedCalloc(P_1d, &lambda); CeedChk(ierr); 2319eaf62fffSJeremy L Thompson // -- Build matrices 2320eaf62fffSJeremy L Thompson const CeedScalar *interp_1d, *grad_1d, *q_weight_1d; 2321eaf62fffSJeremy L Thompson ierr = CeedBasisGetInterp1D(basis, &interp_1d); CeedChk(ierr); 2322eaf62fffSJeremy L Thompson ierr = CeedBasisGetGrad1D(basis, &grad_1d); CeedChk(ierr); 2323eaf62fffSJeremy L Thompson ierr = CeedBasisGetQWeights(basis, &q_weight_1d); CeedChk(ierr); 2324eaf62fffSJeremy L Thompson ierr = CeedBuildMassLaplace(interp_1d, grad_1d, q_weight_1d, P_1d, Q_1d, dim, 2325eaf62fffSJeremy L Thompson mass, laplace); CeedChk(ierr); 2326eaf62fffSJeremy L Thompson 2327eaf62fffSJeremy L Thompson // -- Diagonalize 2328eaf62fffSJeremy L Thompson ierr = CeedSimultaneousDiagonalization(ceed, laplace, mass, x, lambda, P_1d); 2329eaf62fffSJeremy L Thompson CeedChk(ierr); 2330eaf62fffSJeremy L Thompson ierr = CeedFree(&mass); CeedChk(ierr); 2331eaf62fffSJeremy L Thompson ierr = CeedFree(&laplace); CeedChk(ierr); 2332eaf62fffSJeremy L Thompson for (CeedInt i=0; i<P_1d; i++) 2333eaf62fffSJeremy L Thompson for (CeedInt j=0; j<P_1d; j++) 2334eaf62fffSJeremy L Thompson fdm_interp[i+j*P_1d] = x[j+i*P_1d]; 2335eaf62fffSJeremy L Thompson ierr = CeedFree(&x); CeedChk(ierr); 2336eaf62fffSJeremy L Thompson 2337eaf62fffSJeremy L Thompson // Assemble QFunction 2338eaf62fffSJeremy L Thompson CeedVector assembled; 2339eaf62fffSJeremy L Thompson CeedElemRestriction rstr_qf; 234070a7ffb3SJeremy L Thompson ierr = CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled, 234170a7ffb3SJeremy L Thompson &rstr_qf, request); CeedChk(ierr); 2342eaf62fffSJeremy L Thompson CeedInt layout[3]; 2343eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionGetELayout(rstr_qf, &layout); CeedChk(ierr); 2344eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionDestroy(&rstr_qf); CeedChk(ierr); 2345eaf62fffSJeremy L Thompson CeedScalar max_norm = 0; 2346eaf62fffSJeremy L Thompson ierr = CeedVectorNorm(assembled, CEED_NORM_MAX, &max_norm); CeedChk(ierr); 2347eaf62fffSJeremy L Thompson 2348eaf62fffSJeremy L Thompson // Calculate element averages 2349eaf62fffSJeremy L Thompson CeedInt num_modes = (interp?1:0) + (grad?dim:0); 2350eaf62fffSJeremy L Thompson CeedScalar *elem_avg; 2351eaf62fffSJeremy L Thompson const CeedScalar *assembled_array, *q_weight_array; 2352eaf62fffSJeremy L Thompson CeedVector q_weight; 2353eaf62fffSJeremy L Thompson ierr = CeedVectorCreate(ceed_parent, num_qpts, &q_weight); CeedChk(ierr); 2354eaf62fffSJeremy L Thompson ierr = CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, 2355eaf62fffSJeremy L Thompson CEED_VECTOR_NONE, q_weight); CeedChk(ierr); 2356eaf62fffSJeremy L Thompson ierr = CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array); 2357eaf62fffSJeremy L Thompson CeedChk(ierr); 2358eaf62fffSJeremy L Thompson ierr = CeedVectorGetArrayRead(q_weight, CEED_MEM_HOST, &q_weight_array); 2359eaf62fffSJeremy L Thompson CeedChk(ierr); 2360eaf62fffSJeremy L Thompson ierr = CeedCalloc(num_elem, &elem_avg); CeedChk(ierr); 2361eaf62fffSJeremy L Thompson const CeedScalar qf_value_bound = max_norm*100*CEED_EPSILON; 2362eaf62fffSJeremy L Thompson for (CeedInt e=0; e<num_elem; e++) { 2363eaf62fffSJeremy L Thompson CeedInt count = 0; 2364eaf62fffSJeremy L Thompson for (CeedInt q=0; q<num_qpts; q++) 2365eaf62fffSJeremy L Thompson for (CeedInt i=0; i<num_comp*num_comp*num_modes*num_modes; i++) 2366eaf62fffSJeremy L Thompson if (fabs(assembled_array[q*layout[0] + i*layout[1] + e*layout[2]]) > 2367eaf62fffSJeremy L Thompson qf_value_bound) { 2368eaf62fffSJeremy L Thompson elem_avg[e] += assembled_array[q*layout[0] + i*layout[1] + e*layout[2]] / 2369eaf62fffSJeremy L Thompson q_weight_array[q]; 2370eaf62fffSJeremy L Thompson count++; 2371eaf62fffSJeremy L Thompson } 2372eaf62fffSJeremy L Thompson if (count) { 2373eaf62fffSJeremy L Thompson elem_avg[e] /= count; 2374eaf62fffSJeremy L Thompson } else { 2375eaf62fffSJeremy L Thompson elem_avg[e] = 1.0; 2376eaf62fffSJeremy L Thompson } 2377eaf62fffSJeremy L Thompson } 2378eaf62fffSJeremy L Thompson ierr = CeedVectorRestoreArrayRead(assembled, &assembled_array); CeedChk(ierr); 2379eaf62fffSJeremy L Thompson ierr = CeedVectorDestroy(&assembled); CeedChk(ierr); 2380eaf62fffSJeremy L Thompson ierr = CeedVectorRestoreArrayRead(q_weight, &q_weight_array); CeedChk(ierr); 2381eaf62fffSJeremy L Thompson ierr = CeedVectorDestroy(&q_weight); CeedChk(ierr); 2382eaf62fffSJeremy L Thompson 2383eaf62fffSJeremy L Thompson // Build FDM diagonal 2384eaf62fffSJeremy L Thompson CeedVector q_data; 2385eaf62fffSJeremy L Thompson CeedScalar *q_data_array, *fdm_diagonal; 2386eaf62fffSJeremy L Thompson ierr = CeedCalloc(num_comp*elem_size, &fdm_diagonal); CeedChk(ierr); 2387eaf62fffSJeremy L Thompson const CeedScalar fdm_diagonal_bound = elem_size*CEED_EPSILON; 2388eaf62fffSJeremy L Thompson for (CeedInt c=0; c<num_comp; c++) 2389eaf62fffSJeremy L Thompson for (CeedInt n=0; n<elem_size; n++) { 2390eaf62fffSJeremy L Thompson if (interp) 2391eaf62fffSJeremy L Thompson fdm_diagonal[c*elem_size + n] = 1.0; 2392eaf62fffSJeremy L Thompson if (grad) 2393eaf62fffSJeremy L Thompson for (CeedInt d=0; d<dim; d++) { 2394eaf62fffSJeremy L Thompson CeedInt i = (n / CeedIntPow(P_1d, d)) % P_1d; 2395eaf62fffSJeremy L Thompson fdm_diagonal[c*elem_size + n] += lambda[i]; 2396eaf62fffSJeremy L Thompson } 2397eaf62fffSJeremy L Thompson if (fabs(fdm_diagonal[c*elem_size + n]) < fdm_diagonal_bound) 2398eaf62fffSJeremy L Thompson fdm_diagonal[c*elem_size + n] = fdm_diagonal_bound; 2399eaf62fffSJeremy L Thompson } 2400eaf62fffSJeremy L Thompson ierr = CeedVectorCreate(ceed_parent, num_elem*num_comp*elem_size, &q_data); 2401eaf62fffSJeremy L Thompson CeedChk(ierr); 2402eaf62fffSJeremy L Thompson ierr = CeedVectorSetValue(q_data, 0.0); CeedChk(ierr); 24039c774eddSJeremy L Thompson ierr = CeedVectorGetArrayWrite(q_data, CEED_MEM_HOST, &q_data_array); 24049c774eddSJeremy L Thompson CeedChk(ierr); 2405eaf62fffSJeremy L Thompson for (CeedInt e=0; e<num_elem; e++) 2406eaf62fffSJeremy L Thompson for (CeedInt c=0; c<num_comp; c++) 2407eaf62fffSJeremy L Thompson for (CeedInt n=0; n<elem_size; n++) 2408eaf62fffSJeremy L Thompson q_data_array[(e*num_comp+c)*elem_size+n] = 1. / (elem_avg[e] * 2409eaf62fffSJeremy L Thompson fdm_diagonal[c*elem_size + n]); 2410eaf62fffSJeremy L Thompson ierr = CeedFree(&elem_avg); CeedChk(ierr); 2411eaf62fffSJeremy L Thompson ierr = CeedFree(&fdm_diagonal); CeedChk(ierr); 2412eaf62fffSJeremy L Thompson ierr = CeedVectorRestoreArray(q_data, &q_data_array); CeedChk(ierr); 2413eaf62fffSJeremy L Thompson 2414eaf62fffSJeremy L Thompson // Setup FDM operator 2415eaf62fffSJeremy L Thompson // -- Basis 2416eaf62fffSJeremy L Thompson CeedBasis fdm_basis; 2417eaf62fffSJeremy L Thompson CeedScalar *grad_dummy, *q_ref_dummy, *q_weight_dummy; 2418eaf62fffSJeremy L Thompson ierr = CeedCalloc(P_1d*P_1d, &grad_dummy); CeedChk(ierr); 2419eaf62fffSJeremy L Thompson ierr = CeedCalloc(P_1d, &q_ref_dummy); CeedChk(ierr); 2420eaf62fffSJeremy L Thompson ierr = CeedCalloc(P_1d, &q_weight_dummy); CeedChk(ierr); 2421eaf62fffSJeremy L Thompson ierr = CeedBasisCreateTensorH1(ceed_parent, dim, num_comp, P_1d, P_1d, 2422eaf62fffSJeremy L Thompson fdm_interp, grad_dummy, q_ref_dummy, 2423eaf62fffSJeremy L Thompson q_weight_dummy, &fdm_basis); CeedChk(ierr); 2424eaf62fffSJeremy L Thompson ierr = CeedFree(&fdm_interp); CeedChk(ierr); 2425eaf62fffSJeremy L Thompson ierr = CeedFree(&grad_dummy); CeedChk(ierr); 2426eaf62fffSJeremy L Thompson ierr = CeedFree(&q_ref_dummy); CeedChk(ierr); 2427eaf62fffSJeremy L Thompson ierr = CeedFree(&q_weight_dummy); CeedChk(ierr); 2428eaf62fffSJeremy L Thompson ierr = CeedFree(&lambda); CeedChk(ierr); 2429eaf62fffSJeremy L Thompson 2430eaf62fffSJeremy L Thompson // -- Restriction 2431eaf62fffSJeremy L Thompson CeedElemRestriction rstr_qd_i; 2432eaf62fffSJeremy L Thompson CeedInt strides[3] = {1, elem_size, elem_size*num_comp}; 2433eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionCreateStrided(ceed_parent, num_elem, elem_size, 2434eaf62fffSJeremy L Thompson num_comp, num_elem*num_comp*elem_size, 2435eaf62fffSJeremy L Thompson strides, &rstr_qd_i); CeedChk(ierr); 2436eaf62fffSJeremy L Thompson // -- QFunction 2437eaf62fffSJeremy L Thompson CeedQFunction qf_fdm; 2438eaf62fffSJeremy L Thompson ierr = CeedQFunctionCreateInteriorByName(ceed_parent, "Scale", &qf_fdm); 2439eaf62fffSJeremy L Thompson CeedChk(ierr); 2440eaf62fffSJeremy L Thompson ierr = CeedQFunctionAddInput(qf_fdm, "input", num_comp, CEED_EVAL_INTERP); 2441eaf62fffSJeremy L Thompson CeedChk(ierr); 2442eaf62fffSJeremy L Thompson ierr = CeedQFunctionAddInput(qf_fdm, "scale", num_comp, CEED_EVAL_NONE); 2443eaf62fffSJeremy L Thompson CeedChk(ierr); 2444eaf62fffSJeremy L Thompson ierr = CeedQFunctionAddOutput(qf_fdm, "output", num_comp, CEED_EVAL_INTERP); 2445eaf62fffSJeremy L Thompson CeedChk(ierr); 24466e15d496SJeremy L Thompson ierr = CeedQFunctionSetUserFlopsEstimate(qf_fdm, num_comp); CeedChk(ierr); 2447eaf62fffSJeremy L Thompson // -- QFunction context 2448eaf62fffSJeremy L Thompson CeedInt *num_comp_data; 2449eaf62fffSJeremy L Thompson ierr = CeedCalloc(1, &num_comp_data); CeedChk(ierr); 2450eaf62fffSJeremy L Thompson num_comp_data[0] = num_comp; 2451eaf62fffSJeremy L Thompson CeedQFunctionContext ctx_fdm; 2452eaf62fffSJeremy L Thompson ierr = CeedQFunctionContextCreate(ceed, &ctx_fdm); CeedChk(ierr); 2453eaf62fffSJeremy L Thompson ierr = CeedQFunctionContextSetData(ctx_fdm, CEED_MEM_HOST, CEED_OWN_POINTER, 2454eaf62fffSJeremy L Thompson sizeof(*num_comp_data), num_comp_data); 2455eaf62fffSJeremy L Thompson CeedChk(ierr); 2456eaf62fffSJeremy L Thompson ierr = CeedQFunctionSetContext(qf_fdm, ctx_fdm); CeedChk(ierr); 2457eaf62fffSJeremy L Thompson ierr = CeedQFunctionContextDestroy(&ctx_fdm); CeedChk(ierr); 2458eaf62fffSJeremy L Thompson // -- Operator 2459eaf62fffSJeremy L Thompson ierr = CeedOperatorCreate(ceed_parent, qf_fdm, NULL, NULL, fdm_inv); 2460eaf62fffSJeremy L Thompson CeedChk(ierr); 2461eaf62fffSJeremy L Thompson CeedOperatorSetField(*fdm_inv, "input", rstr, fdm_basis, CEED_VECTOR_ACTIVE); 2462eaf62fffSJeremy L Thompson CeedChk(ierr); 2463eaf62fffSJeremy L Thompson CeedOperatorSetField(*fdm_inv, "scale", rstr_qd_i, CEED_BASIS_COLLOCATED, 2464eaf62fffSJeremy L Thompson q_data); CeedChk(ierr); 2465eaf62fffSJeremy L Thompson CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE); 2466eaf62fffSJeremy L Thompson CeedChk(ierr); 2467eaf62fffSJeremy L Thompson 2468eaf62fffSJeremy L Thompson // Cleanup 2469eaf62fffSJeremy L Thompson ierr = CeedVectorDestroy(&q_data); CeedChk(ierr); 2470eaf62fffSJeremy L Thompson ierr = CeedBasisDestroy(&fdm_basis); CeedChk(ierr); 2471eaf62fffSJeremy L Thompson ierr = CeedElemRestrictionDestroy(&rstr_qd_i); CeedChk(ierr); 2472eaf62fffSJeremy L Thompson ierr = CeedQFunctionDestroy(&qf_fdm); CeedChk(ierr); 2473eaf62fffSJeremy L Thompson 2474eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 2475eaf62fffSJeremy L Thompson } 2476eaf62fffSJeremy L Thompson 2477eaf62fffSJeremy L Thompson /// @} 2478