Skip to content

Basis Evaluation Struct #260

@dtoshniwal

Description

@dtoshniwal

Hi @apalha @J15525 @dccabanas:

I started wrapping the evaluations of basis functions in a struct. The code is attached below. Since the earlier, explicit indexing was not obvious/was hard to remember, the getters here should help clarify what you are asking for. However, are these all the getters that we want? Is this enough functionality or would we like to access things in a different way?

"""
    EvaluatedBasis

A struct to store the evaluations of finite element basis functions and their derivatives.

The raw data is stored in a nested vector format, but should be accessed through the provided
getter methods for clarity.

# Fields
- `data::Vector{Vector{Vector{Matrix{Float64}}}}`: The core data store where:
    - `data[i][j][k]` corresponds to a matrix of evaluations where
     - `i`: derivative order `i-1`
     - `j`: `j`-th mixed derivative of order `i-1` (indexing specified by get_derivative_idx)
     - `k`: index for the component of a multicomponent function.
"""
struct EvaluatedBasis{manifold_dim}
    data::Vector{Vector{Vector{Matrix{Float64}}}}

    function EvaluatedBasis(manifold_dim::Int, data::Vector{Vector{Vector{Matrix{Float64}}}})
        return new{manifold_dim}(data)
    end
end

"""
    get_values(evals::EvaluatedBasis, component_id::Int=1)

Retrieves the evaluation matrix for the function values (0-th order derivatives).

# Arguments
- `evals::EvaluatedBasis`: The `EvaluatedBasis` object.
- `component_id::Int=1`: The component index for vector-valued elements. Defaults to 1 for scalar cases.

# Returns
- `Matrix{Float64}`: A matrix where `M[a, b]` is the value of the `b`-th basis function at the `a`-th evaluation point.
"""
function get_values(evals::EvaluatedBasis, component_id::Int=1)
    # Values are order 0, which corresponds to the first index `i=1`.
    # The derivative type index `j` is also 1.
    return @view evals.data[1][1][component_id]
end

"""
    get_partial_derivatives(evals::EvaluatedBasis, type::Vector{Int}, component_id::Int=1)

Retrieves the evaluation matrix for a specific derivative.

# Arguments
- `evals::EvaluatedBasis`: The `EvaluatedBasis` object.
- `type::Vector{Int}`: The type of mixed derivative. E.g.:
    -- `[1,0]` for ∂/∂x, `[0,1]` for ∂/∂y, `[1,1]` for ∂²/∂x∂y in 2D
    -- `[1,0,0]` for ∂/∂x, `[0,1,0]` for ∂/∂y, `[0,0,1]` for ∂/∂z,
       `[1,1,0]` for ∂²/∂x∂y, etc. in 3D.
- `component_id::Int=1`: (Optional) The component index. Defaults to 1.

# Returns
- `Matrix{Float64}`: A matrix where `M[a, b]` is the value of the specified derivative of the `b`-th basis function at the `a`-th evaluation point.
"""
function get_partial_derivatives(
    evals::EvaluatedBasis{manifold_dim},
    type::Vector{Int},
    component_id::Int=1
) where {manifold_dim}
    # The data index `i` is `order + 1`.
    order = sum(type)
    if order == 0
        return get_values(evals, component_id)
    end
    return @view evals.data[order+1][get_derivative_idx(type)][component_id]
end

Metadata

Metadata

Assignees

Labels

refactor 🦋Updates or rewrites of existing code to use new interfaces/setups. Neither broken nor truly new.

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions