1*3d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2*3d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3d7b241e6Sjeremylt // 4*3d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5d7b241e6Sjeremylt // 6*3d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7d7b241e6Sjeremylt 8ec3da8bcSJed Brown #include <ceed/ceed.h> 9ec3da8bcSJed Brown #include <ceed/backend.h> 103d3250a0SJeremy L Thompson #include <ceed/jit-tools.h> 113d576824SJeremy L Thompson #include <ceed-impl.h> 12288c0443SJeremy L Thompson #include <limits.h> 133d576824SJeremy L Thompson #include <stdbool.h> 143d576824SJeremy L Thompson #include <stdio.h> 1543e1b16fSJeremy L Thompson #include <stdlib.h> 163d576824SJeremy L Thompson #include <string.h> 17288c0443SJeremy L Thompson 187a982d89SJeremy L. Thompson /// @file 197a982d89SJeremy L. Thompson /// Implementation of public CeedQFunction interfaces 207a982d89SJeremy L. Thompson 21288c0443SJeremy L Thompson /// @cond DOXYGEN_SKIP 22442e7f0bSjeremylt static struct CeedQFunction_private ceed_qfunction_none; 23442e7f0bSjeremylt /// @endcond 24442e7f0bSjeremylt 257a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser 267a982d89SJeremy L. Thompson /// @{ 277a982d89SJeremy L. Thompson 287a982d89SJeremy L. Thompson // Indicate that no QFunction is provided by the user 297a982d89SJeremy L. Thompson const CeedQFunction CEED_QFUNCTION_NONE = &ceed_qfunction_none; 307a982d89SJeremy L. Thompson 317a982d89SJeremy L. Thompson /// @} 327a982d89SJeremy L. Thompson 33442e7f0bSjeremylt /// @cond DOXYGEN_SKIP 34288c0443SJeremy L Thompson static struct { 35288c0443SJeremy L Thompson char name[CEED_MAX_RESOURCE_LEN]; 36288c0443SJeremy L Thompson char source[CEED_MAX_RESOURCE_LEN]; 37d1d35e2fSjeremylt CeedInt vec_length; 38288c0443SJeremy L Thompson CeedQFunctionUser f; 39288c0443SJeremy L Thompson int (*init)(Ceed ceed, const char *name, CeedQFunction qf); 40d1d35e2fSjeremylt } gallery_qfunctions[1024]; 41288c0443SJeremy L Thompson static size_t num_qfunctions; 42288c0443SJeremy L Thompson /// @endcond 43d7b241e6Sjeremylt 44777ff853SJeremy L Thompson /// ---------------------------------------------------------------------------- 45777ff853SJeremy L Thompson /// CeedQFunction Library Internal Functions 46777ff853SJeremy L Thompson /// ---------------------------------------------------------------------------- 47777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper 48777ff853SJeremy L Thompson /// @{ 49777ff853SJeremy L Thompson 50b11c1e72Sjeremylt /** 51288c0443SJeremy L Thompson @brief Register a gallery QFunction 52288c0443SJeremy L Thompson 53288c0443SJeremy L Thompson @param name Name for this backend to respond to 541176cc3aSJeremy L Thompson @param source Absolute path to source of QFunction, 551176cc3aSJeremy L Thompson "\path\CEED_DIR\gallery\folder\file.h:function_name" 56d1d35e2fSjeremylt @param vec_length Vector length. Caller must ensure that number of quadrature 57d1d35e2fSjeremylt points is a multiple of vec_length. 58288c0443SJeremy L Thompson @param f Function pointer to evaluate action at quadrature points. 59288c0443SJeremy L Thompson See \ref CeedQFunctionUser. 60288c0443SJeremy L Thompson @param init Initialization function called by CeedQFunctionInit() when the 61288c0443SJeremy L Thompson QFunction is selected. 62288c0443SJeremy L Thompson 63288c0443SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 64288c0443SJeremy L Thompson 657a982d89SJeremy L. Thompson @ref Developer 66288c0443SJeremy L Thompson **/ 67288c0443SJeremy L Thompson int CeedQFunctionRegister(const char *name, const char *source, 68d1d35e2fSjeremylt CeedInt vec_length, CeedQFunctionUser f, 69288c0443SJeremy L Thompson int (*init)(Ceed, const char *, CeedQFunction)) { 70d1d35e2fSjeremylt if (num_qfunctions >= sizeof(gallery_qfunctions) / sizeof( 71d1d35e2fSjeremylt gallery_qfunctions[0])) 72c042f62fSJeremy L Thompson // LCOV_EXCL_START 73e15f9bd0SJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, "Too many gallery QFunctions"); 74c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 75c042f62fSJeremy L Thompson 768ccf1006SJeremy L Thompson CeedDebugEnv("Gallery Register: %s", name); 778ccf1006SJeremy L Thompson 78d1d35e2fSjeremylt strncpy(gallery_qfunctions[num_qfunctions].name, name, CEED_MAX_RESOURCE_LEN); 79d1d35e2fSjeremylt gallery_qfunctions[num_qfunctions].name[CEED_MAX_RESOURCE_LEN-1] = 0; 80d1d35e2fSjeremylt strncpy(gallery_qfunctions[num_qfunctions].source, source, 81d1d35e2fSjeremylt CEED_MAX_RESOURCE_LEN); 82d1d35e2fSjeremylt gallery_qfunctions[num_qfunctions].source[CEED_MAX_RESOURCE_LEN-1] = 0; 83d1d35e2fSjeremylt gallery_qfunctions[num_qfunctions].vec_length = vec_length; 84d1d35e2fSjeremylt gallery_qfunctions[num_qfunctions].f = f; 85d1d35e2fSjeremylt gallery_qfunctions[num_qfunctions].init = init; 86288c0443SJeremy L Thompson num_qfunctions++; 87e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 88288c0443SJeremy L Thompson } 89288c0443SJeremy L Thompson 90288c0443SJeremy L Thompson /** 917a982d89SJeremy L. Thompson @brief Set a CeedQFunction field, used by CeedQFunctionAddInput/Output 927a982d89SJeremy L. Thompson 937a982d89SJeremy L. Thompson @param f CeedQFunctionField 94d1d35e2fSjeremylt @param field_name Name of QFunction field 95d1d35e2fSjeremylt @param size Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or 96d1d35e2fSjeremylt (num_comp * 1) for @ref CEED_EVAL_NONE, @ref CEED_EVAL_INTERP, and @ref CEED_EVAL_WEIGHT 97d1d35e2fSjeremylt @param eval_mode \ref CEED_EVAL_NONE to use values directly, 987a982d89SJeremy L. Thompson \ref CEED_EVAL_INTERP to use interpolated values, 997a982d89SJeremy L. Thompson \ref CEED_EVAL_GRAD to use gradients, 1007a982d89SJeremy L. Thompson \ref CEED_EVAL_WEIGHT to use quadrature weights. 1017a982d89SJeremy L. Thompson 1027a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1037a982d89SJeremy L. Thompson 1047a982d89SJeremy L. Thompson @ref Developer 1057a982d89SJeremy L. Thompson **/ 106d1d35e2fSjeremylt static int CeedQFunctionFieldSet(CeedQFunctionField *f, const char *field_name, 107d1d35e2fSjeremylt CeedInt size, CeedEvalMode eval_mode) { 108cdf32b93SJeremy L Thompson int ierr; 1097a982d89SJeremy L. Thompson 110e15f9bd0SJeremy L Thompson ierr = CeedCalloc(1, f); CeedChk(ierr); 111f7e22acaSJeremy L Thompson ierr = CeedStringAllocCopy(field_name, (char **)&(*f)->field_name); 112f7e22acaSJeremy L Thompson CeedChk(ierr); 1137a982d89SJeremy L. Thompson (*f)->size = size; 114d1d35e2fSjeremylt (*f)->eval_mode = eval_mode; 115e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1167a982d89SJeremy L. Thompson } 1177a982d89SJeremy L. Thompson 1187a982d89SJeremy L. Thompson /** 1197a982d89SJeremy L. Thompson @brief View a field of a CeedQFunction 1207a982d89SJeremy L. Thompson 1217a982d89SJeremy L. Thompson @param[in] field QFunction field to view 122d1d35e2fSjeremylt @param[in] field_number Number of field being viewed 1234c4400c7SValeria Barra @param[in] in true for input field, false for output 1247a982d89SJeremy L. Thompson @param[in] stream Stream to view to, e.g., stdout 1257a982d89SJeremy L. Thompson 1267a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1277a982d89SJeremy L. Thompson 1287a982d89SJeremy L. Thompson @ref Utility 1297a982d89SJeremy L. Thompson **/ 130d1d35e2fSjeremylt static int CeedQFunctionFieldView(CeedQFunctionField field, 131d1d35e2fSjeremylt CeedInt field_number, 1327a982d89SJeremy L. Thompson bool in, FILE *stream) { 1338229195eSjeremylt int ierr; 1347a982d89SJeremy L. Thompson const char *inout = in ? "Input" : "Output"; 1358229195eSjeremylt char *field_name; 1368229195eSjeremylt ierr = CeedQFunctionFieldGetName(field, &field_name); CeedChk(ierr); 1378229195eSjeremylt CeedInt size; 1388229195eSjeremylt ierr = CeedQFunctionFieldGetSize(field, &size); CeedChk(ierr); 1398229195eSjeremylt CeedEvalMode eval_mode; 1408229195eSjeremylt ierr = CeedQFunctionFieldGetEvalMode(field, &eval_mode); CeedChk(ierr); 1417a982d89SJeremy L. Thompson fprintf(stream, " %s Field [%d]:\n" 1427a982d89SJeremy L. Thompson " Name: \"%s\"\n" 1437a982d89SJeremy L. Thompson " Size: %d\n" 1447a982d89SJeremy L. Thompson " EvalMode: \"%s\"\n", 1458229195eSjeremylt inout, field_number, field_name, size, CeedEvalModes[eval_mode]); 146e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1477a982d89SJeremy L. Thompson } 1487a982d89SJeremy L. Thompson 149777ff853SJeremy L Thompson /** 150777ff853SJeremy L Thompson @brief Set flag to determine if Fortran interface is used 151777ff853SJeremy L Thompson 152777ff853SJeremy L Thompson @param qf CeedQFunction 153777ff853SJeremy L Thompson @param status Boolean value to set as Fortran status 154777ff853SJeremy L Thompson 155777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 156777ff853SJeremy L Thompson 157777ff853SJeremy L Thompson @ref Backend 158777ff853SJeremy L Thompson **/ 159777ff853SJeremy L Thompson int CeedQFunctionSetFortranStatus(CeedQFunction qf, bool status) { 160f04ea552SJeremy L Thompson qf->is_fortran = status; 161e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 162777ff853SJeremy L Thompson } 163777ff853SJeremy L Thompson 1647a982d89SJeremy L. Thompson /// @} 1657a982d89SJeremy L. Thompson 1667a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1677a982d89SJeremy L. Thompson /// CeedQFunction Backend API 1687a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 1697a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionBackend 1707a982d89SJeremy L. Thompson /// @{ 1717a982d89SJeremy L. Thompson 1727a982d89SJeremy L. Thompson /** 1737a982d89SJeremy L. Thompson @brief Get the vector length of a CeedQFunction 1747a982d89SJeremy L. Thompson 1757a982d89SJeremy L. Thompson @param qf CeedQFunction 176d1d35e2fSjeremylt @param[out] vec_length Variable to store vector length 1777a982d89SJeremy L. Thompson 1787a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1797a982d89SJeremy L. Thompson 1807a982d89SJeremy L. Thompson @ref Backend 1817a982d89SJeremy L. Thompson **/ 182d1d35e2fSjeremylt int CeedQFunctionGetVectorLength(CeedQFunction qf, CeedInt *vec_length) { 183d1d35e2fSjeremylt *vec_length = qf->vec_length; 184e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1857a982d89SJeremy L. Thompson } 1867a982d89SJeremy L. Thompson 1877a982d89SJeremy L. Thompson /** 1887a982d89SJeremy L. Thompson @brief Get the number of inputs and outputs to a CeedQFunction 1897a982d89SJeremy L. Thompson 1907a982d89SJeremy L. Thompson @param qf CeedQFunction 191d1d35e2fSjeremylt @param[out] num_input Variable to store number of input fields 192d1d35e2fSjeremylt @param[out] num_output Variable to store number of output fields 1937a982d89SJeremy L. Thompson 1947a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1957a982d89SJeremy L. Thompson 1967a982d89SJeremy L. Thompson @ref Backend 1977a982d89SJeremy L. Thompson **/ 198d1d35e2fSjeremylt int CeedQFunctionGetNumArgs(CeedQFunction qf, CeedInt *num_input, 199d1d35e2fSjeremylt CeedInt *num_output) { 200d1d35e2fSjeremylt if (num_input) *num_input = qf->num_input_fields; 201d1d35e2fSjeremylt if (num_output) *num_output = qf->num_output_fields; 202e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2037a982d89SJeremy L. Thompson } 2047a982d89SJeremy L. Thompson 2057a982d89SJeremy L. Thompson /** 20643e1b16fSJeremy L Thompson @brief Get the name of the user function for a CeedQFunction 2077a982d89SJeremy L. Thompson 2087a982d89SJeremy L. Thompson @param qf CeedQFunction 20943e1b16fSJeremy L Thompson @param[out] kernel_name Variable to store source path string 2107a982d89SJeremy L. Thompson 2117a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 2127a982d89SJeremy L. Thompson 2137a982d89SJeremy L. Thompson @ref Backend 2147a982d89SJeremy L. Thompson **/ 21543e1b16fSJeremy L Thompson int CeedQFunctionGetKernelName(CeedQFunction qf, char **kernel_name) { 21643e1b16fSJeremy L Thompson *kernel_name = (char *) qf->kernel_name; 21743e1b16fSJeremy L Thompson return CEED_ERROR_SUCCESS; 21843e1b16fSJeremy L Thompson } 21943e1b16fSJeremy L Thompson 22043e1b16fSJeremy L Thompson /** 22143e1b16fSJeremy L Thompson @brief Get the source path string for a CeedQFunction 22243e1b16fSJeremy L Thompson 22343e1b16fSJeremy L Thompson @param qf CeedQFunction 22443e1b16fSJeremy L Thompson @param[out] source_path Variable to store source path string 22543e1b16fSJeremy L Thompson 22643e1b16fSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 22743e1b16fSJeremy L Thompson 22843e1b16fSJeremy L Thompson @ref Backend 22943e1b16fSJeremy L Thompson **/ 23043e1b16fSJeremy L Thompson int CeedQFunctionGetSourcePath(CeedQFunction qf, char **source_path) { 23143e1b16fSJeremy L Thompson *source_path = (char *) qf->source_path; 232e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2337a982d89SJeremy L. Thompson } 2347a982d89SJeremy L. Thompson 2357a982d89SJeremy L. Thompson /** 2363d3250a0SJeremy L Thompson @brief Initalize and load QFunction source file into string buffer, including 2373d3250a0SJeremy L Thompson full text of local files in place of `#include "local.h"`. 2383d3250a0SJeremy L Thompson The `buffer` is set to `NULL` if there is no QFunction source file. 2393d3250a0SJeremy L Thompson Note: Caller is responsible for freeing the string buffer with `CeedFree()`. 2403d3250a0SJeremy L Thompson 2413d3250a0SJeremy L Thompson @param qf CeedQFunction 242f74ec584SJeremy L Thompson @param[out] source_buffer String buffer for source file contents 2433d3250a0SJeremy L Thompson 2443d3250a0SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2453d3250a0SJeremy L Thompson 2463d3250a0SJeremy L Thompson @ref Backend 2473d3250a0SJeremy L Thompson **/ 2483d3250a0SJeremy L Thompson int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, char **source_buffer) { 2493d3250a0SJeremy L Thompson int ierr; 2503d3250a0SJeremy L Thompson char *source_path; 2513d3250a0SJeremy L Thompson 2523d3250a0SJeremy L Thompson ierr = CeedQFunctionGetSourcePath(qf, &source_path); CeedChk(ierr); 2533d3250a0SJeremy L Thompson *source_buffer = NULL; 2543d3250a0SJeremy L Thompson if (source_path) { 2553d3250a0SJeremy L Thompson ierr = CeedLoadSourceToBuffer(qf->ceed, source_path, source_buffer); 2563d3250a0SJeremy L Thompson CeedChk(ierr); 2573d3250a0SJeremy L Thompson } 2583d3250a0SJeremy L Thompson 2593d3250a0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2603d3250a0SJeremy L Thompson } 2613d3250a0SJeremy L Thompson 2623d3250a0SJeremy L Thompson /** 2637a982d89SJeremy L. Thompson @brief Get the User Function for a CeedQFunction 2647a982d89SJeremy L. Thompson 2657a982d89SJeremy L. Thompson @param qf CeedQFunction 2667a982d89SJeremy L. Thompson @param[out] f Variable to store user function 2677a982d89SJeremy L. Thompson 2687a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 2697a982d89SJeremy L. Thompson 2707a982d89SJeremy L. Thompson @ref Backend 2717a982d89SJeremy L. Thompson **/ 2727a982d89SJeremy L. Thompson int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) { 2737a982d89SJeremy L. Thompson *f = qf->function; 274e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2757a982d89SJeremy L. Thompson } 2767a982d89SJeremy L. Thompson 2777a982d89SJeremy L. Thompson /** 278777ff853SJeremy L Thompson @brief Get global context for a CeedQFunction. 279777ff853SJeremy L Thompson Note: For QFunctions from the Fortran interface, this 280777ff853SJeremy L Thompson function will return the Fortran context 281777ff853SJeremy L Thompson CeedQFunctionContext. 2827a982d89SJeremy L. Thompson 2837a982d89SJeremy L. Thompson @param qf CeedQFunction 284777ff853SJeremy L Thompson @param[out] ctx Variable to store CeedQFunctionContext 2857a982d89SJeremy L. Thompson 2867a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 2877a982d89SJeremy L. Thompson 2887a982d89SJeremy L. Thompson @ref Backend 2897a982d89SJeremy L. Thompson **/ 290777ff853SJeremy L Thompson int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) { 2917a982d89SJeremy L. Thompson *ctx = qf->ctx; 292e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2937a982d89SJeremy L. Thompson } 2947a982d89SJeremy L. Thompson 2957a982d89SJeremy L. Thompson /** 296441428dfSJeremy L Thompson @brief Get context data of a CeedQFunction 297441428dfSJeremy L Thompson 298441428dfSJeremy L Thompson @param qf CeedQFunction 299441428dfSJeremy L Thompson @param mem_type Memory type on which to access the data. If the backend 300441428dfSJeremy L Thompson uses a different memory type, this will perform a copy. 301441428dfSJeremy L Thompson @param[out] data Data on memory type mem_type 302441428dfSJeremy L Thompson 303441428dfSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 304441428dfSJeremy L Thompson 305441428dfSJeremy L Thompson @ref Backend 306441428dfSJeremy L Thompson **/ 307441428dfSJeremy L Thompson int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, 308441428dfSJeremy L Thompson void *data) { 309441428dfSJeremy L Thompson int ierr; 310441428dfSJeremy L Thompson bool is_writable; 311441428dfSJeremy L Thompson CeedQFunctionContext ctx; 312441428dfSJeremy L Thompson 313441428dfSJeremy L Thompson ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr); 314441428dfSJeremy L Thompson if (ctx) { 315441428dfSJeremy L Thompson ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr); 316441428dfSJeremy L Thompson if (is_writable) { 317441428dfSJeremy L Thompson ierr = CeedQFunctionContextGetData(ctx, mem_type, data); CeedChk(ierr); 318441428dfSJeremy L Thompson } else { 319441428dfSJeremy L Thompson ierr = CeedQFunctionContextGetDataRead(ctx, mem_type, data); CeedChk(ierr); 320441428dfSJeremy L Thompson } 321441428dfSJeremy L Thompson } else { 322441428dfSJeremy L Thompson *(void **)data = NULL; 323441428dfSJeremy L Thompson } 324441428dfSJeremy L Thompson return CEED_ERROR_SUCCESS; 325441428dfSJeremy L Thompson } 326441428dfSJeremy L Thompson 327441428dfSJeremy L Thompson /** 328441428dfSJeremy L Thompson @brief Restore context data of a CeedQFunction 329441428dfSJeremy L Thompson 330441428dfSJeremy L Thompson @param qf CeedQFunction 331441428dfSJeremy L Thompson @param data Data to restore 332441428dfSJeremy L Thompson 333441428dfSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 334441428dfSJeremy L Thompson 335441428dfSJeremy L Thompson @ref Backend 336441428dfSJeremy L Thompson **/ 337441428dfSJeremy L Thompson int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) { 338441428dfSJeremy L Thompson int ierr; 339441428dfSJeremy L Thompson bool is_writable; 340441428dfSJeremy L Thompson CeedQFunctionContext ctx; 341441428dfSJeremy L Thompson 342441428dfSJeremy L Thompson ierr = CeedQFunctionGetContext(qf, &ctx); CeedChk(ierr); 343441428dfSJeremy L Thompson if (ctx) { 344441428dfSJeremy L Thompson ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr); 345441428dfSJeremy L Thompson if (is_writable) { 346441428dfSJeremy L Thompson ierr = CeedQFunctionContextRestoreData(ctx, data); CeedChk(ierr); 347441428dfSJeremy L Thompson } else { 348441428dfSJeremy L Thompson ierr = CeedQFunctionContextRestoreDataRead(ctx, data); CeedChk(ierr); 349441428dfSJeremy L Thompson } 350441428dfSJeremy L Thompson } 351441428dfSJeremy L Thompson return CEED_ERROR_SUCCESS; 352441428dfSJeremy L Thompson } 353441428dfSJeremy L Thompson 354441428dfSJeremy L Thompson /** 3557a982d89SJeremy L. Thompson @brief Get true user context for a CeedQFunction 356777ff853SJeremy L Thompson Note: For all QFunctions this function will return the user 357777ff853SJeremy L Thompson CeedQFunctionContext and not interface context 358777ff853SJeremy L Thompson CeedQFunctionContext, if any such object exists. 3597a982d89SJeremy L. Thompson 3607a982d89SJeremy L. Thompson @param qf CeedQFunction 361777ff853SJeremy L Thompson @param[out] ctx Variable to store CeedQFunctionContext 3627a982d89SJeremy L. Thompson 3637a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3647a982d89SJeremy L. Thompson @ref Backend 3657a982d89SJeremy L. Thompson **/ 366777ff853SJeremy L Thompson int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) { 367777ff853SJeremy L Thompson int ierr; 368f04ea552SJeremy L Thompson if (qf->is_fortran) { 369d1d35e2fSjeremylt CeedFortranContext fortran_ctx = NULL; 370d1d35e2fSjeremylt ierr = CeedQFunctionContextGetData(qf->ctx, CEED_MEM_HOST, &fortran_ctx); 371777ff853SJeremy L Thompson CeedChk(ierr); 3728cb0412aSJeremy L Thompson *ctx = fortran_ctx->inner_ctx; 373d1d35e2fSjeremylt ierr = CeedQFunctionContextRestoreData(qf->ctx, (void *)&fortran_ctx); 374d1d35e2fSjeremylt CeedChk(ierr); 3757a982d89SJeremy L. Thompson } else { 3767a982d89SJeremy L. Thompson *ctx = qf->ctx; 3777a982d89SJeremy L. Thompson } 378e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3797a982d89SJeremy L. Thompson } 3807a982d89SJeremy L. Thompson 3817a982d89SJeremy L. Thompson /** 382441428dfSJeremy L Thompson @brief Get inner context data of a CeedQFunction 383441428dfSJeremy L Thompson 384441428dfSJeremy L Thompson @param qf CeedQFunction 385441428dfSJeremy L Thompson @param mem_type Memory type on which to access the data. If the backend 386441428dfSJeremy L Thompson uses a different memory type, this will perform a copy. 387441428dfSJeremy L Thompson @param[out] data Data on memory type mem_type 388441428dfSJeremy L Thompson 389441428dfSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 390441428dfSJeremy L Thompson 391441428dfSJeremy L Thompson @ref Backend 392441428dfSJeremy L Thompson **/ 393441428dfSJeremy L Thompson int CeedQFunctionGetInnerContextData(CeedQFunction qf, CeedMemType mem_type, 394441428dfSJeremy L Thompson void *data) { 395441428dfSJeremy L Thompson int ierr; 396441428dfSJeremy L Thompson bool is_writable; 397441428dfSJeremy L Thompson CeedQFunctionContext ctx; 398441428dfSJeremy L Thompson 399441428dfSJeremy L Thompson ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr); 400441428dfSJeremy L Thompson if (ctx) { 401441428dfSJeremy L Thompson ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr); 402441428dfSJeremy L Thompson if (is_writable) { 403441428dfSJeremy L Thompson ierr = CeedQFunctionContextGetData(ctx, mem_type, data); CeedChk(ierr); 404441428dfSJeremy L Thompson } else { 405441428dfSJeremy L Thompson ierr = CeedQFunctionContextGetDataRead(ctx, mem_type, data); CeedChk(ierr); 406441428dfSJeremy L Thompson } 407441428dfSJeremy L Thompson } else { 408441428dfSJeremy L Thompson *(void **)data = NULL; 409441428dfSJeremy L Thompson } 410441428dfSJeremy L Thompson return CEED_ERROR_SUCCESS; 411441428dfSJeremy L Thompson } 412441428dfSJeremy L Thompson 413441428dfSJeremy L Thompson /** 414441428dfSJeremy L Thompson @brief Restore inner context data of a CeedQFunction 415441428dfSJeremy L Thompson 416441428dfSJeremy L Thompson @param qf CeedQFunction 417441428dfSJeremy L Thompson @param data Data to restore 418441428dfSJeremy L Thompson 419441428dfSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 420441428dfSJeremy L Thompson 421441428dfSJeremy L Thompson @ref Backend 422441428dfSJeremy L Thompson **/ 423441428dfSJeremy L Thompson int CeedQFunctionRestoreInnerContextData(CeedQFunction qf, void *data) { 424441428dfSJeremy L Thompson int ierr; 425441428dfSJeremy L Thompson bool is_writable; 426441428dfSJeremy L Thompson CeedQFunctionContext ctx; 427441428dfSJeremy L Thompson 428441428dfSJeremy L Thompson ierr = CeedQFunctionGetInnerContext(qf, &ctx); CeedChk(ierr); 429441428dfSJeremy L Thompson if (ctx) { 430441428dfSJeremy L Thompson ierr = CeedQFunctionIsContextWritable(qf, &is_writable); CeedChk(ierr); 431441428dfSJeremy L Thompson if (is_writable) { 432441428dfSJeremy L Thompson ierr = CeedQFunctionContextRestoreData(ctx, data); CeedChk(ierr); 433441428dfSJeremy L Thompson } else { 434441428dfSJeremy L Thompson ierr = CeedQFunctionContextRestoreDataRead(ctx, data); CeedChk(ierr); 435441428dfSJeremy L Thompson } 436441428dfSJeremy L Thompson } 437441428dfSJeremy L Thompson return CEED_ERROR_SUCCESS; 438441428dfSJeremy L Thompson } 439441428dfSJeremy L Thompson 440441428dfSJeremy L Thompson /** 4417a982d89SJeremy L. Thompson @brief Determine if QFunction is identity 4427a982d89SJeremy L. Thompson 4437a982d89SJeremy L. Thompson @param qf CeedQFunction 444d1d35e2fSjeremylt @param[out] is_identity Variable to store identity status 4457a982d89SJeremy L. Thompson 4467a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4477a982d89SJeremy L. Thompson 4487a982d89SJeremy L. Thompson @ref Backend 4497a982d89SJeremy L. Thompson **/ 450d1d35e2fSjeremylt int CeedQFunctionIsIdentity(CeedQFunction qf, bool *is_identity) { 451f04ea552SJeremy L Thompson *is_identity = qf->is_identity; 452e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4537a982d89SJeremy L. Thompson } 4547a982d89SJeremy L. Thompson 4557a982d89SJeremy L. Thompson /** 456441428dfSJeremy L Thompson @brief Determine if QFunctionContext is writable 457441428dfSJeremy L Thompson 458441428dfSJeremy L Thompson @param qf CeedQFunction 459441428dfSJeremy L Thompson @param[out] is_writable Variable to store context writeable staus 460441428dfSJeremy L Thompson 461441428dfSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 462441428dfSJeremy L Thompson 463441428dfSJeremy L Thompson @ref Backend 464441428dfSJeremy L Thompson **/ 465441428dfSJeremy L Thompson int CeedQFunctionIsContextWritable(CeedQFunction qf, bool *is_writable) { 466441428dfSJeremy L Thompson *is_writable = qf->is_context_writable; 467441428dfSJeremy L Thompson return CEED_ERROR_SUCCESS; 468441428dfSJeremy L Thompson } 469441428dfSJeremy L Thompson 470441428dfSJeremy L Thompson /** 4717a982d89SJeremy L. Thompson @brief Get backend data of a CeedQFunction 4727a982d89SJeremy L. Thompson 4737a982d89SJeremy L. Thompson @param qf CeedQFunction 4747a982d89SJeremy L. Thompson @param[out] data Variable to store data 4757a982d89SJeremy L. Thompson 4767a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4777a982d89SJeremy L. Thompson 4787a982d89SJeremy L. Thompson @ref Backend 4797a982d89SJeremy L. Thompson **/ 480777ff853SJeremy L Thompson int CeedQFunctionGetData(CeedQFunction qf, void *data) { 481777ff853SJeremy L Thompson *(void **)data = qf->data; 482e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4837a982d89SJeremy L. Thompson } 4847a982d89SJeremy L. Thompson 4857a982d89SJeremy L. Thompson /** 4867a982d89SJeremy L. Thompson @brief Set backend data of a CeedQFunction 4877a982d89SJeremy L. Thompson 4887a982d89SJeremy L. Thompson @param[out] qf CeedQFunction 4897a982d89SJeremy L. Thompson @param data Data to set 4907a982d89SJeremy L. Thompson 4917a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4927a982d89SJeremy L. Thompson 4937a982d89SJeremy L. Thompson @ref Backend 4947a982d89SJeremy L. Thompson **/ 495777ff853SJeremy L Thompson int CeedQFunctionSetData(CeedQFunction qf, void *data) { 496777ff853SJeremy L Thompson qf->data = data; 497e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4987a982d89SJeremy L. Thompson } 4997a982d89SJeremy L. Thompson 5007a982d89SJeremy L. Thompson /** 50134359f16Sjeremylt @brief Increment the reference counter for a CeedQFunction 50234359f16Sjeremylt 50334359f16Sjeremylt @param qf CeedQFunction to increment the reference counter 50434359f16Sjeremylt 50534359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 50634359f16Sjeremylt 50734359f16Sjeremylt @ref Backend 50834359f16Sjeremylt **/ 5099560d06aSjeremylt int CeedQFunctionReference(CeedQFunction qf) { 51034359f16Sjeremylt qf->ref_count++; 51134359f16Sjeremylt return CEED_ERROR_SUCCESS; 51234359f16Sjeremylt } 51334359f16Sjeremylt 5147a982d89SJeremy L. Thompson /// @} 5157a982d89SJeremy L. Thompson 5167a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 5177a982d89SJeremy L. Thompson /// CeedQFunction Public API 5187a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 5197a982d89SJeremy L. Thompson /// @addtogroup CeedQFunctionUser 5207a982d89SJeremy L. Thompson /// @{ 5217a982d89SJeremy L. Thompson 5227a982d89SJeremy L. Thompson /** 5237a982d89SJeremy L. Thompson @brief Create a CeedQFunction for evaluating interior (volumetric) terms. 5247a982d89SJeremy L. Thompson 5257a982d89SJeremy L. Thompson @param ceed A Ceed object where the CeedQFunction will be created 526d1d35e2fSjeremylt @param vec_length Vector length. Caller must ensure that number of quadrature 527d1d35e2fSjeremylt points is a multiple of vec_length. 5287a982d89SJeremy L. Thompson @param f Function pointer to evaluate action at quadrature points. 5297a982d89SJeremy L. Thompson See \ref CeedQFunctionUser. 5307a982d89SJeremy L. Thompson @param source Absolute path to source of QFunction, 531c8d5b03cSJeremy L Thompson "\abs_path\file.h:function_name". 532c8d5b03cSJeremy L Thompson For support across all backends, this source must only 533c8d5b03cSJeremy L Thompson contain constructs supported by C99, C++11, and CUDA. 5347a982d89SJeremy L. Thompson @param[out] qf Address of the variable where the newly created 5357a982d89SJeremy L. Thompson CeedQFunction will be stored 5367a982d89SJeremy L. Thompson 5377a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 5387a982d89SJeremy L. Thompson 5397a982d89SJeremy L. Thompson See \ref CeedQFunctionUser for details on the call-back function @a f's 5407a982d89SJeremy L. Thompson arguments. 5417a982d89SJeremy L. Thompson 5427a982d89SJeremy L. Thompson @ref User 5437a982d89SJeremy L. Thompson **/ 544d1d35e2fSjeremylt int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, 545d1d35e2fSjeremylt CeedQFunctionUser f, 5467a982d89SJeremy L. Thompson const char *source, CeedQFunction *qf) { 5477a982d89SJeremy L. Thompson int ierr; 54843e1b16fSJeremy L Thompson char *source_copy, *kernel_name_copy; 5497a982d89SJeremy L. Thompson 5507a982d89SJeremy L. Thompson if (!ceed->QFunctionCreate) { 5517a982d89SJeremy L. Thompson Ceed delegate; 5527a982d89SJeremy L. Thompson ierr = CeedGetObjectDelegate(ceed, &delegate, "QFunction"); CeedChk(ierr); 5537a982d89SJeremy L. Thompson 5547a982d89SJeremy L. Thompson if (!delegate) 5557a982d89SJeremy L. Thompson // LCOV_EXCL_START 556e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 557e15f9bd0SJeremy L Thompson "Backend does not support QFunctionCreate"); 5587a982d89SJeremy L. Thompson // LCOV_EXCL_STOP 5597a982d89SJeremy L. Thompson 560d1d35e2fSjeremylt ierr = CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf); 5617a982d89SJeremy L. Thompson CeedChk(ierr); 562e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5637a982d89SJeremy L. Thompson } 5647a982d89SJeremy L. Thompson 565edfb5f23SJeremy L Thompson if (strlen(source) && !strrchr(source, ':')) 56643e1b16fSJeremy L Thompson // LCOV_EXCL_START 56743e1b16fSJeremy L Thompson return CeedError(ceed, CEED_ERROR_INCOMPLETE, 56843e1b16fSJeremy L Thompson "Provided path to source does not include function name. " 56943e1b16fSJeremy L Thompson "Provided: \"%s\"\nRequired: \"\\abs_path\\file.h:function_name\"", 57043e1b16fSJeremy L Thompson source); 57143e1b16fSJeremy L Thompson // LCOV_EXCL_STOP 57243e1b16fSJeremy L Thompson 5737a982d89SJeremy L. Thompson ierr = CeedCalloc(1, qf); CeedChk(ierr); 5747a982d89SJeremy L. Thompson (*qf)->ceed = ceed; 5759560d06aSjeremylt ierr = CeedReference(ceed); CeedChk(ierr); 576d1d35e2fSjeremylt (*qf)->ref_count = 1; 577d1d35e2fSjeremylt (*qf)->vec_length = vec_length; 578f04ea552SJeremy L Thompson (*qf)->is_identity = false; 579441428dfSJeremy L Thompson (*qf)->is_context_writable = true; 5807a982d89SJeremy L. Thompson (*qf)->function = f; 58143e1b16fSJeremy L Thompson if (strlen(source)) { 58243e1b16fSJeremy L Thompson const char *kernel_name = strrchr(source, ':') + 1; 58343e1b16fSJeremy L Thompson size_t kernel_name_len = strlen(kernel_name); 58443e1b16fSJeremy L Thompson ierr = CeedCalloc(kernel_name_len + 1, &kernel_name_copy); CeedChk(ierr); 58543e1b16fSJeremy L Thompson strncpy(kernel_name_copy, kernel_name, kernel_name_len); 58643e1b16fSJeremy L Thompson (*qf)->kernel_name = kernel_name_copy; 58743e1b16fSJeremy L Thompson 58843e1b16fSJeremy L Thompson size_t source_len = strlen(source) - kernel_name_len - 1; 58943e1b16fSJeremy L Thompson ierr = CeedCalloc(source_len + 1, &source_copy); CeedChk(ierr); 59043e1b16fSJeremy L Thompson strncpy(source_copy, source, source_len); 591d1d35e2fSjeremylt (*qf)->source_path = source_copy; 59243e1b16fSJeremy L Thompson } 593bf4cb664SJeremy L Thompson ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->input_fields); CeedChk(ierr); 594bf4cb664SJeremy L Thompson ierr = CeedCalloc(CEED_FIELD_MAX, &(*qf)->output_fields); CeedChk(ierr); 5957a982d89SJeremy L. Thompson ierr = ceed->QFunctionCreate(*qf); CeedChk(ierr); 596e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5977a982d89SJeremy L. Thompson } 5987a982d89SJeremy L. Thompson 5997a982d89SJeremy L. Thompson /** 600288c0443SJeremy L Thompson @brief Create a CeedQFunction for evaluating interior (volumetric) terms by name. 601288c0443SJeremy L Thompson 602288c0443SJeremy L Thompson @param ceed A Ceed object where the CeedQFunction will be created 603288c0443SJeremy L Thompson @param name Name of QFunction to use from gallery 604288c0443SJeremy L Thompson @param[out] qf Address of the variable where the newly created 605288c0443SJeremy L Thompson CeedQFunction will be stored 606288c0443SJeremy L Thompson 607288c0443SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 608288c0443SJeremy L Thompson 6097a982d89SJeremy L. Thompson @ref User 610288c0443SJeremy L Thompson **/ 611288c0443SJeremy L Thompson int CeedQFunctionCreateInteriorByName(Ceed ceed, const char *name, 612288c0443SJeremy L Thompson CeedQFunction *qf) { 613288c0443SJeremy L Thompson int ierr; 614f7e22acaSJeremy L Thompson size_t match_len = 0, match_index = UINT_MAX; 615288c0443SJeremy L Thompson 6161d013790SJed Brown ierr = CeedQFunctionRegisterAll(); CeedChk(ierr); 617288c0443SJeremy L Thompson // Find matching backend 618e15f9bd0SJeremy L Thompson if (!name) return CeedError(ceed, CEED_ERROR_INCOMPLETE, 619e15f9bd0SJeremy L Thompson "No QFunction name provided"); 620288c0443SJeremy L Thompson for (size_t i=0; i<num_qfunctions; i++) { 621288c0443SJeremy L Thompson size_t n; 622d1d35e2fSjeremylt const char *curr_name = gallery_qfunctions[i].name; 623d1d35e2fSjeremylt for (n = 0; curr_name[n] && curr_name[n] == name[n]; n++) {} 624d1d35e2fSjeremylt if (n > match_len) { 625d1d35e2fSjeremylt match_len = n; 626f7e22acaSJeremy L Thompson match_index = i; 627288c0443SJeremy L Thompson } 628288c0443SJeremy L Thompson } 629d1d35e2fSjeremylt if (!match_len) 630138d4072Sjeremylt // LCOV_EXCL_START 631e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "No suitable gallery QFunction"); 632138d4072Sjeremylt // LCOV_EXCL_STOP 633288c0443SJeremy L Thompson 634288c0443SJeremy L Thompson // Create QFunction 635d1d35e2fSjeremylt ierr = CeedQFunctionCreateInterior(ceed, 636f7e22acaSJeremy L Thompson gallery_qfunctions[match_index].vec_length, 637f7e22acaSJeremy L Thompson gallery_qfunctions[match_index].f, 638f7e22acaSJeremy L Thompson gallery_qfunctions[match_index].source, qf); 639288c0443SJeremy L Thompson CeedChk(ierr); 640288c0443SJeremy L Thompson 641288c0443SJeremy L Thompson // QFunction specific setup 642f7e22acaSJeremy L Thompson ierr = gallery_qfunctions[match_index].init(ceed, name, *qf); CeedChk(ierr); 643288c0443SJeremy L Thompson 64475affc3bSjeremylt // Copy name 645f7e22acaSJeremy L Thompson ierr = CeedStringAllocCopy(name, (char **)&(*qf)->gallery_name); CeedChk(ierr); 64643e1b16fSJeremy L Thompson (*qf)->is_gallery = true; 647e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 648288c0443SJeremy L Thompson } 649288c0443SJeremy L Thompson 650288c0443SJeremy L Thompson /** 6510219ea01SJeremy L Thompson @brief Create an identity CeedQFunction. Inputs are written into outputs in 6520219ea01SJeremy L Thompson the order given. This is useful for CeedOperators that can be 6530219ea01SJeremy L Thompson represented with only the action of a CeedRestriction and CeedBasis, 6540219ea01SJeremy L Thompson such as restriction and prolongation operators for p-multigrid. 6550219ea01SJeremy L Thompson Backends may optimize CeedOperators with this CeedQFunction to avoid 6560219ea01SJeremy L Thompson the copy of input data to output fields by using the same memory 6570219ea01SJeremy L Thompson location for both. 6580219ea01SJeremy L Thompson 6590219ea01SJeremy L Thompson @param ceed A Ceed object where the CeedQFunction will be created 660d1d35e2fSjeremylt @param[in] size Size of the QFunction fields 661d1d35e2fSjeremylt @param[in] in_mode CeedEvalMode for input to CeedQFunction 662d1d35e2fSjeremylt @param[in] out_mode CeedEvalMode for output to CeedQFunction 6630219ea01SJeremy L Thompson @param[out] qf Address of the variable where the newly created 6640219ea01SJeremy L Thompson CeedQFunction will be stored 6650219ea01SJeremy L Thompson 6660219ea01SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 6670219ea01SJeremy L Thompson 6687a982d89SJeremy L. Thompson @ref User 6690219ea01SJeremy L Thompson **/ 670d1d35e2fSjeremylt int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, 671d1d35e2fSjeremylt CeedEvalMode out_mode, CeedQFunction *qf) { 6720219ea01SJeremy L Thompson int ierr; 6730219ea01SJeremy L Thompson 6740219ea01SJeremy L Thompson ierr = CeedQFunctionCreateInteriorByName(ceed, "Identity", qf); CeedChk(ierr); 675d1d35e2fSjeremylt ierr = CeedQFunctionAddInput(*qf, "input", size, in_mode); CeedChk(ierr); 676d1d35e2fSjeremylt ierr = CeedQFunctionAddOutput(*qf, "output", size, out_mode); CeedChk(ierr); 6770219ea01SJeremy L Thompson 678f04ea552SJeremy L Thompson (*qf)->is_identity = true; 679547dbd6fSJeremy L Thompson 680777ff853SJeremy L Thompson CeedQFunctionContext ctx; 6813668ca4bSJeremy L Thompson CeedContextFieldLabel size_label; 682547dbd6fSJeremy L Thompson ierr = CeedQFunctionGetContext(*qf, &ctx); CeedChk(ierr); 6833668ca4bSJeremy L Thompson ierr = CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label); 6843668ca4bSJeremy L Thompson CeedChk(ierr); 6857bfe0f0eSJeremy L Thompson ierr = CeedQFunctionContextSetInt32(ctx, size_label, &size); CeedChk(ierr); 686547dbd6fSJeremy L Thompson 687e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6880219ea01SJeremy L Thompson } 6890219ea01SJeremy L Thompson 6900219ea01SJeremy L Thompson /** 6919560d06aSjeremylt @brief Copy the pointer to a CeedQFunction. Both pointers should 6929560d06aSjeremylt be destroyed with `CeedQFunctionDestroy()`; 6939560d06aSjeremylt Note: If `*qf_copy` is non-NULL, then it is assumed that 6949560d06aSjeremylt `*qf_copy` is a pointer to a CeedQFunction. This 6959560d06aSjeremylt CeedQFunction will be destroyed if `*qf_copy` is the only 6969560d06aSjeremylt reference to this CeedQFunction. 6979560d06aSjeremylt 6989560d06aSjeremylt @param qf CeedQFunction to copy reference to 6999560d06aSjeremylt @param[out] qf_copy Variable to store copied reference 7009560d06aSjeremylt 7019560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 7029560d06aSjeremylt 7039560d06aSjeremylt @ref User 7049560d06aSjeremylt **/ 7059560d06aSjeremylt int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) { 7069560d06aSjeremylt int ierr; 7079560d06aSjeremylt 7089560d06aSjeremylt ierr = CeedQFunctionReference(qf); CeedChk(ierr); 7099560d06aSjeremylt ierr = CeedQFunctionDestroy(qf_copy); CeedChk(ierr); 7109560d06aSjeremylt *qf_copy = qf; 7119560d06aSjeremylt return CEED_ERROR_SUCCESS; 7129560d06aSjeremylt } 7139560d06aSjeremylt 7149560d06aSjeremylt /** 715a0a97fcfSJed Brown @brief Add a CeedQFunction input 716b11c1e72Sjeremylt 717b11c1e72Sjeremylt @param qf CeedQFunction 718d1d35e2fSjeremylt @param field_name Name of QFunction field 719d1d35e2fSjeremylt @param size Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or 720d1d35e2fSjeremylt (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP 721d1d35e2fSjeremylt @param eval_mode \ref CEED_EVAL_NONE to use values directly, 722b11c1e72Sjeremylt \ref CEED_EVAL_INTERP to use interpolated values, 723b11c1e72Sjeremylt \ref CEED_EVAL_GRAD to use gradients. 724b11c1e72Sjeremylt 725b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 726dfdf5a53Sjeremylt 7277a982d89SJeremy L. Thompson @ref User 728b11c1e72Sjeremylt **/ 729d1d35e2fSjeremylt int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, 730d1d35e2fSjeremylt CeedInt size, 731d1d35e2fSjeremylt CeedEvalMode eval_mode) { 732f04ea552SJeremy L Thompson if (qf->is_immutable) 733e15f9bd0SJeremy L Thompson // LCOV_EXCL_START 734e15f9bd0SJeremy L Thompson return CeedError(qf->ceed, CEED_ERROR_MAJOR, 735f04ea552SJeremy L Thompson "QFunction cannot be changed after set as immutable"); 736e15f9bd0SJeremy L Thompson // LCOV_EXCL_STOP 737d1d35e2fSjeremylt if ((eval_mode == CEED_EVAL_WEIGHT) && (size != 1)) 738e15f9bd0SJeremy L Thompson // LCOV_EXCL_START 739e15f9bd0SJeremy L Thompson return CeedError(qf->ceed, CEED_ERROR_DIMENSION, 740e15f9bd0SJeremy L Thompson "CEED_EVAL_WEIGHT should have size 1"); 741e15f9bd0SJeremy L Thompson // LCOV_EXCL_STOP 742d1d35e2fSjeremylt int ierr = CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], 743d1d35e2fSjeremylt field_name, size, eval_mode); 744fe2413ffSjeremylt CeedChk(ierr); 745d1d35e2fSjeremylt qf->num_input_fields++; 746e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 747d7b241e6Sjeremylt } 748d7b241e6Sjeremylt 749b11c1e72Sjeremylt /** 750a0a97fcfSJed Brown @brief Add a CeedQFunction output 751b11c1e72Sjeremylt 752b11c1e72Sjeremylt @param qf CeedQFunction 753d1d35e2fSjeremylt @param field_name Name of QFunction field 754d1d35e2fSjeremylt @param size Size of QFunction field, (num_comp * dim) for @ref CEED_EVAL_GRAD or 755d1d35e2fSjeremylt (num_comp * 1) for @ref CEED_EVAL_NONE and @ref CEED_EVAL_INTERP 756d1d35e2fSjeremylt @param eval_mode \ref CEED_EVAL_NONE to use values directly, 757b11c1e72Sjeremylt \ref CEED_EVAL_INTERP to use interpolated values, 758b11c1e72Sjeremylt \ref CEED_EVAL_GRAD to use gradients. 759b11c1e72Sjeremylt 760b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 761dfdf5a53Sjeremylt 7627a982d89SJeremy L. Thompson @ref User 763b11c1e72Sjeremylt **/ 764d1d35e2fSjeremylt int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, 765d1d35e2fSjeremylt CeedInt size, CeedEvalMode eval_mode) { 766f04ea552SJeremy L Thompson if (qf->is_immutable) 767e15f9bd0SJeremy L Thompson // LCOV_EXCL_START 768e15f9bd0SJeremy L Thompson return CeedError(qf->ceed, CEED_ERROR_MAJOR, 769f04ea552SJeremy L Thompson "QFunction cannot be changed after set as immutable"); 770e15f9bd0SJeremy L Thompson // LCOV_EXCL_STOP 771d1d35e2fSjeremylt if (eval_mode == CEED_EVAL_WEIGHT) 772c042f62fSJeremy L Thompson // LCOV_EXCL_START 773e15f9bd0SJeremy L Thompson return CeedError(qf->ceed, CEED_ERROR_DIMENSION, 774e15f9bd0SJeremy L Thompson "Cannot create QFunction output with " 7751d102b48SJeremy L Thompson "CEED_EVAL_WEIGHT"); 776c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 777d1d35e2fSjeremylt int ierr = CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], 778d1d35e2fSjeremylt field_name, size, eval_mode); 779fe2413ffSjeremylt CeedChk(ierr); 780d1d35e2fSjeremylt qf->num_output_fields++; 781e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 782d7b241e6Sjeremylt } 783d7b241e6Sjeremylt 784dfdf5a53Sjeremylt /** 78543bbe138SJeremy L Thompson @brief Get the CeedQFunctionFields of a CeedQFunction 78643bbe138SJeremy L Thompson 787f04ea552SJeremy L Thompson Note: Calling this function asserts that setup is complete 788f04ea552SJeremy L Thompson and sets the CeedQFunction as immutable. 789f04ea552SJeremy L Thompson 79043bbe138SJeremy L Thompson @param qf CeedQFunction 791f74ec584SJeremy L Thompson @param[out] num_input_fields Variable to store number of input fields 792f74ec584SJeremy L Thompson @param[out] input_fields Variable to store input fields 793f74ec584SJeremy L Thompson @param[out] num_output_fields Variable to store number of output fields 794f74ec584SJeremy L Thompson @param[out] output_fields Variable to store output fields 79543bbe138SJeremy L Thompson 79643bbe138SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 79743bbe138SJeremy L Thompson 798e9b533fbSJeremy L Thompson @ref Advanced 79943bbe138SJeremy L Thompson **/ 80043bbe138SJeremy L Thompson int CeedQFunctionGetFields(CeedQFunction qf, CeedInt *num_input_fields, 80143bbe138SJeremy L Thompson CeedQFunctionField **input_fields, 80243bbe138SJeremy L Thompson CeedInt *num_output_fields, 80343bbe138SJeremy L Thompson CeedQFunctionField **output_fields) { 804f04ea552SJeremy L Thompson qf->is_immutable = true; 80543bbe138SJeremy L Thompson if (num_input_fields) *num_input_fields = qf->num_input_fields; 80643bbe138SJeremy L Thompson if (input_fields) *input_fields = qf->input_fields; 80743bbe138SJeremy L Thompson if (num_output_fields) *num_output_fields = qf->num_output_fields; 80843bbe138SJeremy L Thompson if (output_fields) *output_fields = qf->output_fields; 80943bbe138SJeremy L Thompson return CEED_ERROR_SUCCESS; 81043bbe138SJeremy L Thompson } 81143bbe138SJeremy L Thompson 81243bbe138SJeremy L Thompson /** 81343bbe138SJeremy L Thompson @brief Get the name of a CeedQFunctionField 81443bbe138SJeremy L Thompson 81543bbe138SJeremy L Thompson @param qf_field CeedQFunctionField 81643bbe138SJeremy L Thompson @param[out] field_name Variable to store the field name 81743bbe138SJeremy L Thompson 81843bbe138SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 81943bbe138SJeremy L Thompson 820e9b533fbSJeremy L Thompson @ref Advanced 82143bbe138SJeremy L Thompson **/ 82243bbe138SJeremy L Thompson int CeedQFunctionFieldGetName(CeedQFunctionField qf_field, char **field_name) { 82343bbe138SJeremy L Thompson *field_name = (char *)qf_field->field_name; 82443bbe138SJeremy L Thompson return CEED_ERROR_SUCCESS; 82543bbe138SJeremy L Thompson } 82643bbe138SJeremy L Thompson 82743bbe138SJeremy L Thompson /** 82843bbe138SJeremy L Thompson @brief Get the number of components of a CeedQFunctionField 82943bbe138SJeremy L Thompson 83043bbe138SJeremy L Thompson @param qf_field CeedQFunctionField 83143bbe138SJeremy L Thompson @param[out] size Variable to store the size of the field 83243bbe138SJeremy L Thompson 83343bbe138SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 83443bbe138SJeremy L Thompson 835e9b533fbSJeremy L Thompson @ref Advanced 83643bbe138SJeremy L Thompson **/ 83743bbe138SJeremy L Thompson int CeedQFunctionFieldGetSize(CeedQFunctionField qf_field, CeedInt *size) { 83843bbe138SJeremy L Thompson *size = qf_field->size; 83943bbe138SJeremy L Thompson return CEED_ERROR_SUCCESS; 84043bbe138SJeremy L Thompson } 84143bbe138SJeremy L Thompson 84243bbe138SJeremy L Thompson /** 84343bbe138SJeremy L Thompson @brief Get the CeedEvalMode of a CeedQFunctionField 84443bbe138SJeremy L Thompson 84543bbe138SJeremy L Thompson @param qf_field CeedQFunctionField 84643bbe138SJeremy L Thompson @param[out] eval_mode Variable to store the field evaluation mode 84743bbe138SJeremy L Thompson 84843bbe138SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 84943bbe138SJeremy L Thompson 850e9b533fbSJeremy L Thompson @ref Advanced 85143bbe138SJeremy L Thompson **/ 85243bbe138SJeremy L Thompson int CeedQFunctionFieldGetEvalMode(CeedQFunctionField qf_field, 85343bbe138SJeremy L Thompson CeedEvalMode *eval_mode) { 85443bbe138SJeremy L Thompson *eval_mode = qf_field->eval_mode; 85543bbe138SJeremy L Thompson return CEED_ERROR_SUCCESS; 85643bbe138SJeremy L Thompson } 85743bbe138SJeremy L Thompson 85843bbe138SJeremy L Thompson /** 8594ce2993fSjeremylt @brief Set global context for a CeedQFunction 860b11c1e72Sjeremylt 861b11c1e72Sjeremylt @param qf CeedQFunction 862b11c1e72Sjeremylt @param ctx Context data to set 863b11c1e72Sjeremylt 864b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 865dfdf5a53Sjeremylt 8667a982d89SJeremy L. Thompson @ref User 867b11c1e72Sjeremylt **/ 868777ff853SJeremy L Thompson int CeedQFunctionSetContext(CeedQFunction qf, CeedQFunctionContext ctx) { 86934359f16Sjeremylt int ierr; 8701389e7feSJeremy L Thompson ierr = CeedQFunctionContextDestroy(&qf->ctx); CeedChk(ierr); 871d7b241e6Sjeremylt qf->ctx = ctx; 8729560d06aSjeremylt ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr); 873e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 874d7b241e6Sjeremylt } 875d7b241e6Sjeremylt 876b11c1e72Sjeremylt /** 877441428dfSJeremy L Thompson @brief Set writability of CeedQFunctionContext when calling the `CeedQFunctionUser`. 878441428dfSJeremy L Thompson The default value is 'is_writable == true'. 879441428dfSJeremy L Thompson 880441428dfSJeremy L Thompson Setting `is_writable == true` indicates the `CeedQFunctionUser` writes 881441428dfSJeremy L Thompson into the CeedQFunctionContextData and requires memory syncronization 882441428dfSJeremy L Thompson after calling `CeedQFunctionApply()`. 883441428dfSJeremy L Thompson 884441428dfSJeremy L Thompson Setting 'is_writable == false' asserts that `CeedQFunctionUser` does not 885441428dfSJeremy L Thompson modify the CeedQFunctionContextData. Violating this assertion may lead 886441428dfSJeremy L Thompson to inconsistent data. 887441428dfSJeremy L Thompson 888441428dfSJeremy L Thompson Setting `is_writable == false` may offer a performance improvement on GPU backends. 889441428dfSJeremy L Thompson 890441428dfSJeremy L Thompson @param qf CeedQFunction 891441428dfSJeremy L Thompson @param is_writable Writability status 892441428dfSJeremy L Thompson 893441428dfSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 894441428dfSJeremy L Thompson 895441428dfSJeremy L Thompson @ref User 896441428dfSJeremy L Thompson **/ 897441428dfSJeremy L Thompson int CeedQFunctionSetContextWritable(CeedQFunction qf, bool is_writable) { 898441428dfSJeremy L Thompson qf->is_context_writable = is_writable; 899441428dfSJeremy L Thompson return CEED_ERROR_SUCCESS; 900441428dfSJeremy L Thompson } 901441428dfSJeremy L Thompson 902441428dfSJeremy L Thompson /** 90375affc3bSjeremylt @brief View a CeedQFunction 90475affc3bSjeremylt 90575affc3bSjeremylt @param[in] qf CeedQFunction to view 90675affc3bSjeremylt @param[in] stream Stream to write; typically stdout/stderr or a file 90775affc3bSjeremylt 90875affc3bSjeremylt @return Error code: 0 - success, otherwise - failure 90975affc3bSjeremylt 9107a982d89SJeremy L. Thompson @ref User 91175affc3bSjeremylt **/ 91275affc3bSjeremylt int CeedQFunctionView(CeedQFunction qf, FILE *stream) { 91375affc3bSjeremylt int ierr; 91475affc3bSjeremylt 91575affc3bSjeremylt fprintf(stream, "%sCeedQFunction %s\n", 91643e1b16fSJeremy L Thompson qf->is_gallery ? "Gallery " : "User ", 91743e1b16fSJeremy L Thompson qf->is_gallery ? qf->gallery_name : qf->kernel_name); 91875affc3bSjeremylt 919d1d35e2fSjeremylt fprintf(stream, " %d Input Field%s:\n", qf->num_input_fields, 920d1d35e2fSjeremylt qf->num_input_fields>1 ? "s" : ""); 921d1d35e2fSjeremylt for (CeedInt i=0; i<qf->num_input_fields; i++) { 922d1d35e2fSjeremylt ierr = CeedQFunctionFieldView(qf->input_fields[i], i, 1, stream); 9232da88da5Sjeremylt CeedChk(ierr); 92475affc3bSjeremylt } 92575affc3bSjeremylt 926d1d35e2fSjeremylt fprintf(stream, " %d Output Field%s:\n", qf->num_output_fields, 927d1d35e2fSjeremylt qf->num_output_fields>1 ? "s" : ""); 928d1d35e2fSjeremylt for (CeedInt i=0; i<qf->num_output_fields; i++) { 929d1d35e2fSjeremylt ierr = CeedQFunctionFieldView(qf->output_fields[i], i, 0, stream); 93075affc3bSjeremylt CeedChk(ierr); 93175affc3bSjeremylt } 932e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 93375affc3bSjeremylt } 93475affc3bSjeremylt 93575affc3bSjeremylt /** 936b7c9bbdaSJeremy L Thompson @brief Get the Ceed associated with a CeedQFunction 937b7c9bbdaSJeremy L Thompson 938b7c9bbdaSJeremy L Thompson @param qf CeedQFunction 939b7c9bbdaSJeremy L Thompson @param[out] ceed Variable to store Ceed 940b7c9bbdaSJeremy L Thompson 941b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 942b7c9bbdaSJeremy L Thompson 943b7c9bbdaSJeremy L Thompson @ref Advanced 944b7c9bbdaSJeremy L Thompson **/ 945b7c9bbdaSJeremy L Thompson int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) { 946b7c9bbdaSJeremy L Thompson *ceed = qf->ceed; 947b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 948b7c9bbdaSJeremy L Thompson } 949b7c9bbdaSJeremy L Thompson 950b7c9bbdaSJeremy L Thompson /** 951b11c1e72Sjeremylt @brief Apply the action of a CeedQFunction 952b11c1e72Sjeremylt 953f04ea552SJeremy L Thompson Note: Calling this function asserts that setup is complete 954f04ea552SJeremy L Thompson and sets the CeedQFunction as immutable. 955f04ea552SJeremy L Thompson 956b11c1e72Sjeremylt @param qf CeedQFunction 957b11c1e72Sjeremylt @param Q Number of quadrature points 95834138859Sjeremylt @param[in] u Array of input CeedVectors 95934138859Sjeremylt @param[out] v Array of output CeedVectors 960b11c1e72Sjeremylt 961b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 962dfdf5a53Sjeremylt 9637a982d89SJeremy L. Thompson @ref User 964b11c1e72Sjeremylt **/ 965d7b241e6Sjeremylt int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, 966aedaa0e5Sjeremylt CeedVector *u, CeedVector *v) { 967d7b241e6Sjeremylt int ierr; 968d7b241e6Sjeremylt if (!qf->Apply) 969c042f62fSJeremy L Thompson // LCOV_EXCL_START 970e15f9bd0SJeremy L Thompson return CeedError(qf->ceed, CEED_ERROR_UNSUPPORTED, 971e15f9bd0SJeremy L Thompson "Backend does not support QFunctionApply"); 972c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 973d1d35e2fSjeremylt if (Q % qf->vec_length) 974c042f62fSJeremy L Thompson // LCOV_EXCL_START 975e15f9bd0SJeremy L Thompson return CeedError(qf->ceed, CEED_ERROR_DIMENSION, 976e15f9bd0SJeremy L Thompson "Number of quadrature points %d must be a " 977d1d35e2fSjeremylt "multiple of %d", Q, qf->vec_length); 978c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 979f04ea552SJeremy L Thompson qf->is_immutable = true; 980d7b241e6Sjeremylt ierr = qf->Apply(qf, Q, u, v); CeedChk(ierr); 981e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 982d7b241e6Sjeremylt } 983d7b241e6Sjeremylt 984b11c1e72Sjeremylt /** 985b11c1e72Sjeremylt @brief Destroy a CeedQFunction 986b11c1e72Sjeremylt 987b11c1e72Sjeremylt @param qf CeedQFunction to destroy 988b11c1e72Sjeremylt 989b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 990dfdf5a53Sjeremylt 9917a982d89SJeremy L. Thompson @ref User 992b11c1e72Sjeremylt **/ 993d7b241e6Sjeremylt int CeedQFunctionDestroy(CeedQFunction *qf) { 994d7b241e6Sjeremylt int ierr; 995d7b241e6Sjeremylt 996d1d35e2fSjeremylt if (!*qf || --(*qf)->ref_count > 0) return CEED_ERROR_SUCCESS; 997fe2413ffSjeremylt // Backend destroy 998d7b241e6Sjeremylt if ((*qf)->Destroy) { 999d7b241e6Sjeremylt ierr = (*qf)->Destroy(*qf); CeedChk(ierr); 1000d7b241e6Sjeremylt } 1001fe2413ffSjeremylt // Free fields 1002d1d35e2fSjeremylt for (int i=0; i<(*qf)->num_input_fields; i++) { 1003d1d35e2fSjeremylt ierr = CeedFree(&(*(*qf)->input_fields[i]).field_name); CeedChk(ierr); 1004d1d35e2fSjeremylt ierr = CeedFree(&(*qf)->input_fields[i]); CeedChk(ierr); 1005fe2413ffSjeremylt } 1006d1d35e2fSjeremylt for (int i=0; i<(*qf)->num_output_fields; i++) { 1007d1d35e2fSjeremylt ierr = CeedFree(&(*(*qf)->output_fields[i]).field_name); CeedChk(ierr); 1008d1d35e2fSjeremylt ierr = CeedFree(&(*qf)->output_fields[i]); CeedChk(ierr); 1009fe2413ffSjeremylt } 1010d1d35e2fSjeremylt ierr = CeedFree(&(*qf)->input_fields); CeedChk(ierr); 1011d1d35e2fSjeremylt ierr = CeedFree(&(*qf)->output_fields); CeedChk(ierr); 1012777ff853SJeremy L Thompson 1013777ff853SJeremy L Thompson // User context data object 1014777ff853SJeremy L Thompson ierr = CeedQFunctionContextDestroy(&(*qf)->ctx); CeedChk(ierr); 1015fe2413ffSjeremylt 1016d1d35e2fSjeremylt ierr = CeedFree(&(*qf)->source_path); CeedChk(ierr); 101743e1b16fSJeremy L Thompson ierr = CeedFree(&(*qf)->gallery_name); CeedChk(ierr); 101843e1b16fSJeremy L Thompson ierr = CeedFree(&(*qf)->kernel_name); CeedChk(ierr); 1019d7b241e6Sjeremylt ierr = CeedDestroy(&(*qf)->ceed); CeedChk(ierr); 1020d7b241e6Sjeremylt ierr = CeedFree(qf); CeedChk(ierr); 1021e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1022d7b241e6Sjeremylt } 1023d7b241e6Sjeremylt 1024d7b241e6Sjeremylt /// @} 1025