Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend data access API #169

Merged
merged 30 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d5ecc49
add getter for number of dofs
benegee Feb 22, 2024
72709e5
change trixi_load_cell_average
benegee Feb 22, 2024
71ba4bf
fix: ensure Int32
benegee Feb 23, 2024
9c83b05
adapt reference value
benegee Feb 23, 2024
5b3960b
add getter for all dofs values
benegee Feb 23, 2024
3e31f4a
adapt next value
benegee Feb 23, 2024
92d817a
add trixi_ndofs_element
benegee Feb 26, 2024
530f5cc
add tests
benegee Feb 26, 2024
c7d38d4
add missing parts in tests
benegee Feb 26, 2024
ba00c6f
fix tests
benegee Feb 26, 2024
cdf6e5e
deallocate first
benegee Feb 26, 2024
aa6d8c7
add trixi_load_prim to Fortran API
benegee Feb 26, 2024
cf43795
reference value
benegee Feb 26, 2024
dda491a
get doxygen right
benegee Feb 26, 2024
35513ac
Merge branch 'main' into more-data-access
sloede Feb 26, 2024
94cc1c7
Merge branch 'more-data-access' of github.com:trixi-framework/libtrix…
benegee Mar 7, 2024
7dbb2c6
make everything more consistent!
benegee Mar 7, 2024
ff6ac54
Merge branch 'main' into more-data-access
benegee Mar 11, 2024
dfea9e4
Merge branch 'main' into more-data-access
sloede Apr 18, 2024
f534175
Merge branch 'main' into more-data-access
benegee May 13, 2024
37b88a1
Merge branch 'main' into more-data-access
benegee Jun 25, 2024
b40a2a1
add functions to get quadrature information
benegee Jul 24, 2024
e4d6901
update CI badge URL
benegee Jul 24, 2024
66ee664
Merge branch 'main' into more-data-access
benegee Jul 24, 2024
6627b97
add get_t8code_forest to Fortran interface
benegee Jul 26, 2024
2550872
format
benegee Jul 26, 2024
0176461
Merge branch 'main' into more-data-access
benegee Nov 18, 2024
6f1c864
remove duplicate
benegee Nov 18, 2024
3b5bb17
make gcc 14 happy
benegee Nov 18, 2024
aa45b69
Apply suggestions from code review
benegee Nov 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions LibTrixi.jl/src/LibTrixi.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module LibTrixi

using OrdinaryDiffEq: OrdinaryDiffEq, step!, check_error, DiscreteCallback
using Trixi: Trixi, summary_callback, mesh_equations_solver_cache, nelements,
nelementsglobal, nvariables, nnodes, wrap_array, eachelement, cons2prim,
get_node_vars, eachnode
using Trixi: Trixi, summary_callback, mesh_equations_solver_cache, ndims, nelements,
nelementsglobal, ndofs, ndofsglobal, nvariables, nnodes, wrap_array,
eachelement, cons2prim, get_node_vars, eachnode
using MPI: MPI, run_init_hooks, set_default_error_handler_return
using Pkg

Expand All @@ -28,15 +28,27 @@ export trixi_ndims,
export trixi_nelements,
trixi_nelements_cfptr,
trixi_nelements_jl
export trixi_nelements_global,
trixi_nelements_global_cfptr,
trixi_nelements_global_jl
export trixi_nelementsglobal,
trixi_nelementsglobal_cfptr,
trixi_nelementsglobal_jl
export trixi_ndofs,
trixi_ndofs_cfptr,
trixi_ndofs_jl
export trixi_ndofsglobal,
trixi_ndofsglobal_cfptr,
trixi_ndofsglobal_jl
export trixi_ndofselement,
trixi_ndofselement_cfptr,
trixi_ndofselement_jl
export trixi_nvariables,
trixi_nvariables_cfptr,
trixi_nvariables_jl
export trixi_load_cell_averages,
trixi_load_cell_averages_cfptr,
trixi_load_cell_averages_jl
export trixi_load_primitive_vars,
trixi_load_primitive_vars_cfptr,
trixi_load_primitive_vars_jl
export trixi_load_element_averaged_primitive_vars,
trixi_load_element_averaged_primitive_vars_cfptr,
trixi_load_element_averaged_primitive_vars_jl
benegee marked this conversation as resolved.
Show resolved Hide resolved
export trixi_version_library,
trixi_version_library_cfptr,
trixi_version_library_jl
Expand Down
115 changes: 95 additions & 20 deletions LibTrixi.jl/src/api_c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ trixi_ndims_cfptr() = @cfunction(trixi_ndims, Cint, (Cint,))
"""
trixi_nelements(simstate_handle::Cint)::Cint

Return number of local elements (cells).
Return number of local elements.
benegee marked this conversation as resolved.
Show resolved Hide resolved
"""
function trixi_nelements end

Expand All @@ -298,18 +298,63 @@ trixi_nelements_cfptr() = @cfunction(trixi_nelements, Cint, (Cint,))


"""
trixi_nelements_global(simstate_handle::Cint)::Cint
trixi_nelementsglobal(simstate_handle::Cint)::Cint

Return number of global elements (cells).
Return global number of elements.
benegee marked this conversation as resolved.
Show resolved Hide resolved
"""
function trixi_nelements_global end
function trixi_nelementsglobal end

Base.@ccallable function trixi_nelements_global(simstate_handle::Cint)::Cint
Base.@ccallable function trixi_nelementsglobal(simstate_handle::Cint)::Cint
simstate = load_simstate(simstate_handle)
return trixi_nelements_global_jl(simstate)
return trixi_nelementsglobal_jl(simstate)
end

trixi_nelements_global_cfptr() = @cfunction(trixi_nelements_global, Cint, (Cint,))
trixi_nelementsglobal_cfptr() = @cfunction(trixi_nelementsglobal, Cint, (Cint,))


"""
trixi_ndofs(simstate_handle::Cint)::Cint

Return number of degrees of freedom (all quadrature points on all elements of current rank).
"""
function trixi_ndofs end

Base.@ccallable function trixi_ndofs(simstate_handle::Cint)::Cint
simstate = load_simstate(simstate_handle)
return trixi_ndofs_jl(simstate)
end

trixi_ndofs_cfptr() = @cfunction(trixi_ndofs, Cint, (Cint,))


"""
trixi_ndofsglobal(simstate_handle::Cint)::Cint

Return global number of degrees of freedom (all quadrature points on all elements).
benegee marked this conversation as resolved.
Show resolved Hide resolved
"""
function trixi_ndofsglobal end

Base.@ccallable function trixi_ndofsglobal(simstate_handle::Cint)::Cint
simstate = load_simstate(simstate_handle)
return trixi_ndofsglobal_jl(simstate)
end

trixi_ndofsglobal_cfptr() = @cfunction(trixi_ndofsglobal, Cint, (Cint,))


"""
trixi_ndofselement(simstate_handle::Cint)::Cint

Return number of degrees of freedom per element.
"""
function trixi_ndofselement end

Base.@ccallable function trixi_ndofselement(simstate_handle::Cint)::Cint
simstate = load_simstate(simstate_handle)
return trixi_ndofselement_jl(simstate)
end

trixi_ndofselement_cfptr() = @cfunction(trixi_ndofselement, Cint, (Cint,))


"""
Expand All @@ -328,32 +373,62 @@ trixi_nvariables_cfptr() = @cfunction(trixi_nvariables, Cint, (Cint,))


"""
trixi_load_cell_averages(data::Ptr{Cdouble}, simstate_handle::Cint)::Cvoid
trixi_load_primitive_vars(simstate_handle::Cint, variable_id::Cint,
data::Ptr{Cdouble})::Cvoid

Load primitive variable.

The values for the primitive variable at position `variable_id` at every degree of freedom
for are stored in the given array `data`.

The given array has to be of correct size (ndofs) and memory has to be allocated beforehand.
"""
function trixi_load_primitive_vars end

Base.@ccallable function trixi_load_primitive_vars(simstate_handle::Cint, variable_id::Cint,
data::Ptr{Cdouble})::Cvoid
simstate = load_simstate(simstate_handle)

# convert C to Julia array
size = trixi_ndofs_jl(simstate)
data_jl = unsafe_wrap(Array, data, size)

trixi_load_primitive_vars_jl(simstate, variable_id, data_jl)
return nothing
end

trixi_load_primitive_vars_cfptr() =
@cfunction(trixi_load_primitive_vars, Cvoid, (Cint, Cint, Ptr{Cdouble}))


"""
trixi_load_element_averaged_primitive_vars(simstate_handle::Cint, variable_id::Cint,
data::Ptr{Cdouble})::Cvoid

Return cell averaged solution state.
Load element averages for primitive variable.

Cell averaged values for each cell and each primitive variable are stored in a contiguous
array, where cell values for the first variable appear first and values for the other
variables subsequently (structure-of-arrays layout).
Element averaged values for the primitive variable at position `variable_id` for each
element are stored in the given array `data`.

The given array has to be of correct size and memory has to be allocated beforehand.
The given array has to be of correct size (nelements) and memory has to be allocated
beforehand.
"""
function trixi_load_cell_averages end
function trixi_load_element_averaged_primitive_vars end

Base.@ccallable function trixi_load_cell_averages(data::Ptr{Cdouble},
simstate_handle::Cint)::Cvoid
Base.@ccallable function trixi_load_element_averaged_primitive_vars(simstate_handle::Cint,
variable_id::Cint, data::Ptr{Cdouble})::Cvoid
simstate = load_simstate(simstate_handle)

# convert C to Julia array
size = trixi_nvariables_jl(simstate) * trixi_nelements_jl(simstate)
size = trixi_nelements_jl(simstate)
data_jl = unsafe_wrap(Array, data, size)

trixi_load_cell_averages_jl(data_jl, simstate)
trixi_load_element_averaged_primitive_vars_jl(simstate, variable_id, data_jl)
return nothing
end

trixi_load_cell_averages_cfptr() =
@cfunction(trixi_load_cell_averages, Cvoid, (Ptr{Cdouble}, Cint,))
trixi_load_element_averaged_primitive_vars_cfptr() =
@cfunction(trixi_load_element_averaged_primitive_vars, Cvoid, (Cint, Cint, Ptr{Cdouble}))


"""
Expand Down
64 changes: 50 additions & 14 deletions LibTrixi.jl/src/api_jl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,63 @@ function trixi_nelements_jl(simstate)
end


function trixi_nelements_global_jl(simstate)
function trixi_nelementsglobal_jl(simstate)
_, _, solver, cache = mesh_equations_solver_cache(simstate.semi)
return nelementsglobal(solver, cache)
end


function trixi_ndofs_jl(simstate)
mesh, _, solver, cache = mesh_equations_solver_cache(simstate.semi)
return ndofs(mesh, solver, cache)
end


function trixi_ndofsglobal_jl(simstate)
mesh, _, solver, cache = mesh_equations_solver_cache(simstate.semi)
return ndofsglobal(mesh, solver, cache)
end


function trixi_ndofselement_jl(simstate)
mesh, _, solver, _ = mesh_equations_solver_cache(simstate.semi)
return nnodes(solver)^ndims(mesh)
end


function trixi_nvariables_jl(simstate)
_, equations, _, _ = mesh_equations_solver_cache(simstate.semi)
return nvariables(equations)
end


function trixi_load_cell_averages_jl(data, simstate)
function trixi_load_primitive_vars_jl(simstate, variable_id, data)
mesh, equations, solver, cache = mesh_equations_solver_cache(simstate.semi)
n_nodes_per_dim = nnodes(solver)
n_dims = ndims(mesh)
n_nodes = n_nodes_per_dim^n_dims

u_ode = simstate.integrator.u
u = wrap_array(u_ode, mesh, equations, solver, cache)

# all permutations of nodes indices for arbitrary dimension
node_cis = CartesianIndices(ntuple(i -> n_nodes_per_dim, n_dims))
node_lis = LinearIndices(node_cis)

for element in eachelement(solver, cache)
for node_ci in node_cis
node_vars = get_node_vars(u, equations, solver, node_ci, element)
node_index = (element-1) * n_nodes + node_lis[node_ci]
data[node_index] = cons2prim(node_vars, equations)[variable_id]
end
end

return nothing
end


function trixi_load_element_averaged_primitive_vars_jl(simstate, variable_id, data)
mesh, equations, solver, cache = mesh_equations_solver_cache(simstate.semi)
n_elements = nelements(solver, cache)
n_variables = nvariables(equations)
n_nodes = nnodes(solver)
n_dims = ndims(mesh)

Expand All @@ -101,15 +142,13 @@ function trixi_load_cell_averages_jl(data, simstate)
# all permutations of nodes indices for arbitrary dimension
node_cis = CartesianIndices(ntuple(i -> n_nodes, n_dims))

# temporary storage for mean value on current element for all variables
u_mean = get_node_vars(u, equations, solver, node_cis[1], 1)

for element in eachelement(solver, cache)

# compute mean value using nodal dg values and quadrature
u_mean = zero(u_mean)
u_mean = zero(eltype(u))
for node_ci in node_cis
u_node_prim = cons2prim(get_node_vars(u, equations, solver, node_ci, element), equations)
u_node_prim = cons2prim(get_node_vars(u, equations, solver, node_ci, element),
equations)[variable_id]
weight = 1.
for node_index in Tuple(node_ci)
weight *= solver.basis.weights[node_index]
Expand All @@ -120,11 +159,8 @@ function trixi_load_cell_averages_jl(data, simstate)
# normalize to unit element
u_mean = u_mean / 2^n_dims

# copy to provided array
# all element values for first variable, then for second, ...
for ivar = 0:n_variables-1
data[element + ivar * n_elements] = u_mean[ivar+1]
end
# write to provided array
data[element] = u_mean
end

return nothing
Expand Down
36 changes: 28 additions & 8 deletions LibTrixi.jl/test/test_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,40 @@ end
nelements_jl = trixi_nelements_jl(simstate_jl)
@test nelements_c == nelements_jl

nelements_global_c = trixi_nelements_global(handle)
nelements_global_jl = trixi_nelements_global_jl(simstate_jl)
@test nelements_global_c == nelements_global_jl
nelementsglobal_c = trixi_nelementsglobal(handle)
nelementsglobal_jl = trixi_nelementsglobal_jl(simstate_jl)
@test nelementsglobal_c == nelementsglobal_jl

# compare number of dofs
ndofs_c = trixi_ndofs(handle)
ndofs_jl = trixi_ndofs_jl(simstate_jl)
@test ndofs_c == ndofs_jl

ndofsglobal_c = trixi_ndofsglobal(handle)
ndofsglobal_jl = trixi_ndofsglobal_jl(simstate_jl)
@test ndofsglobal_c == ndofsglobal_jl

ndofselement_c = trixi_ndofselement(handle)
ndofselement_jl = trixi_ndofselement_jl(simstate_jl)
@test ndofselement_c == ndofselement_jl

# compare number of variables
nvariables_c = trixi_nvariables(handle)
nvariables_jl = trixi_nvariables_jl(simstate_jl)
@test nvariables_c == nvariables_jl

# compare cell averaged values
data_c = zeros(nvariables_c * nelements_c)
trixi_load_cell_averages(pointer(data_c), handle)
data_jl = zeros(nvariables_jl * nelements_jl)
trixi_load_cell_averages_jl(data_jl, simstate_jl)
# compare element averaged values
data_c = zeros(nelements_c)
trixi_load_element_averaged_primitive_vars(handle, Int32(1), pointer(data_c))
data_jl = zeros(nelements_jl)
trixi_load_element_averaged_primitive_vars_jl(simstate_jl, 1, data_jl)
@test data_c == data_jl

# compare primitive variable values on all dofs
data_c = zeros(ndofs_c)
trixi_load_primitive_vars(handle, Int32(1), pointer(data_c))
data_jl = zeros(ndofs_jl)
trixi_load_primitive_vars_jl(simstate_jl, 1, data_jl)
@test data_c == data_jl
end

Expand Down
8 changes: 4 additions & 4 deletions examples/trixi_controller_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ int main ( int argc, char *argv[] ) {
printf("\n*** Trixi controller *** nelements %d\n", nelements);

// Allocate memory
data = realloc( data, sizeof(double) * nelements * nvariables );
data = realloc( data, sizeof(double) * nelements );

// Get averaged cell values for each variable
trixi_load_cell_averages(data, handle);
// Get element averaged values for first variable
trixi_load_element_averaged_primitive_vars(handle, 1, data);
}
}

// Print first variable
for (int i = 0; i < nelements; ++i) {
printf("u[cell %3d] = %f\n", i, data[i]);
printf("u[element %3d] = %f\n", i, data[i]);
}

// Finalize Trixi simulation
Expand Down
8 changes: 4 additions & 4 deletions examples/trixi_controller_data.f90
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ program trixi_controller_data_f

! allocate memory
if ( associated(data) ) deallocate(data)
allocate( data(nelements*nvariables) )
allocate( data(nelements) )

! get averaged cell values for each variable
call trixi_load_cell_averages(data, handle)
! get element averaged values for first variable
call trixi_load_element_averaged_primitive_vars(handle, 1, data)
end if
end do

! print first variable
do i = 1,nelements
print "('u[cell ', i4, '] = ', e14.8)", i, data(i)
print "('u[element ', i4, '] = ', e14.8)", i, data(i)
end do

! Finalize Trixi simulation
Expand Down
Loading
Loading