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

RFC: source terms via global vector storage #173

Merged
merged 61 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 57 commits
Commits
Show all changes
61 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
2d45c62
add trixi_store_in_database
benegee Feb 26, 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
bf0b732
Merge branch 'main' into bg/store-in-database
benegee Feb 28, 2024
b864850
add trixi_get_time, trixi_load_node_coordinates
benegee Feb 29, 2024
9430f2d
implement source terms controller
benegee Feb 29, 2024
5956a78
libelixir with source terms via database
benegee Feb 29, 2024
ccd23fc
change Array to Vector
benegee Feb 29, 2024
1be73d9
spelling
benegee Feb 29, 2024
6170610
Merge branch 'more-data-access' into bg/store-in-database
benegee Mar 7, 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
7906080
use const double *
benegee Mar 8, 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
017e166
Merge branch 'bg/store-in-database' of github.com:trixi-framework/lib…
benegee May 13, 2024
f534175
Merge branch 'main' into more-data-access
benegee May 13, 2024
8b6a81f
Merge branch 'more-data-access' into bg/store-in-database
benegee May 13, 2024
f96436b
missed merge conflict
benegee May 13, 2024
b76ca70
transpose calloc args
benegee May 13, 2024
37b88a1
Merge branch 'main' into more-data-access
benegee Jun 25, 2024
95ba0cc
Merge branch 'more-data-access' into bg/store-in-database
benegee Jun 28, 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
3f665f0
Merge branch 'more-data-access' into bg/store-in-database
benegee Jul 24, 2024
3a0387b
remove load_node_coordinates
benegee Jul 24, 2024
6627b97
add get_t8code_forest to Fortran interface
benegee Jul 26, 2024
2550872
format
benegee Jul 26, 2024
211de01
Merge branch 'more-data-access' into bg/store-in-database
benegee Jul 26, 2024
08daf22
add trixi_store_in_database to Fortran interface
benegee Jul 26, 2024
312e979
Merge branch 'main' into bg/store-in-database
benegee Nov 18, 2024
a175a04
adapt min version according to main CMakeLists.txt
benegee Nov 18, 2024
d5fd61f
missed while merging
benegee Nov 18, 2024
4449a82
Merge branch 'main' into bg/store-in-database
benegee Nov 19, 2024
0a359b1
change default argument for DataBase parameter
benegee Nov 19, 2024
7b654a4
remove deprecated export
benegee Nov 19, 2024
41ab02a
test for get_time
benegee Nov 19, 2024
6bda669
add tests for trixi_store_in_database
benegee Nov 19, 2024
a8c573e
need Int32
benegee Nov 19, 2024
5a33b95
cannot compare Refs
benegee Nov 19, 2024
378dc90
check based on address
benegee Nov 19, 2024
8c36455
relax error tolerance
benegee Nov 19, 2024
f28d802
remove deprecated example
benegee Nov 19, 2024
fdfb80d
rename get_time to get_simulation_time
benegee Nov 25, 2024
9b2508d
rename database to registry
benegee Nov 25, 2024
e21ba07
spurious Ref access
benegee Nov 25, 2024
223866d
Merge branch 'main' into bg/store-in-database
sloede Nov 26, 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
4 changes: 3 additions & 1 deletion LibTrixi.jl/examples/libelixir_p4est2d_dgsem_euler_sedov.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ function init_simstate()
###############################################################################
# Create simulation state

simstate = SimulationState(semi, integrator)
# database only used for tests
database = LibTrixiDataBaseType(undef, 1)
simstate = SimulationState(semi, integrator, database)

return simstate
end
8 changes: 8 additions & 0 deletions LibTrixi.jl/src/LibTrixi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export trixi_load_primitive_vars,
export trixi_load_element_averaged_primitive_vars,
trixi_load_element_averaged_primitive_vars_cfptr,
trixi_load_element_averaged_primitive_vars_jl
export trixi_store_in_database,
trixi_store_in_database_cfptr,
trixi_store_in_database_jl
export trixi_version_library,
trixi_version_library_cfptr,
trixi_version_library_jl
Expand All @@ -82,8 +85,13 @@ export trixi_get_t8code_forest,
export trixi_eval_julia,
trixi_eval_julia_cfptr,
trixi_eval_julia_jl
export trixi_get_time,
trixi_get_time_cfptr,
trixi_get_time_jl


export SimulationState, store_simstate, load_simstate, delete_simstate!
export LibTrixiDataBaseType


# global storage of name and version information of loaded packages
Expand Down
47 changes: 47 additions & 0 deletions LibTrixi.jl/src/api_c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,53 @@ trixi_load_primitive_vars_cfptr() =
@cfunction(trixi_load_primitive_vars, Cvoid, (Cint, Cint, Ptr{Cdouble}))


"""
trixi_store_in_database(data::Ptr{Cdouble}, size::Cint, index::Cint,
simstate_handle::Cint)::Cvoid
sloede marked this conversation as resolved.
Show resolved Hide resolved
sloede marked this conversation as resolved.
Show resolved Hide resolved

Store data vector in current simulation's database.

A reference to the passed data array `data` will be stored in the database of the simulation
given by `simstate_handle` at given `index`. The database object has to be created in
`init_simstate()` of the running libelixir and can be used throughout the simulation.

The database object has to exist, has to be of type `LibTrixiDataBaseType`, and has to hold
enough data references such that access at `index` is valid.

The size of `data` has to match `size`.
sloede marked this conversation as resolved.
Show resolved Hide resolved
"""
function trixi_store_in_database end

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

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

trixi_store_in_database_jl(simstate, index, data_jl)
return nothing
end

trixi_store_in_database_cfptr() =
@cfunction(trixi_store_in_database, Cvoid, (Cint, Cint, Cint, Ptr{Cdouble},))


"""
trixi_get_time(simstate_handle::Cint)::Cdouble

Return current physical time.
benegee marked this conversation as resolved.
Show resolved Hide resolved
"""
function trixi_get_time end

Base.@ccallable function trixi_get_time(simstate_handle::Cint)::Cdouble
simstate = load_simstate(simstate_handle)
return trixi_get_time_jl(simstate)
end

trixi_get_time_cfptr() = @cfunction(trixi_get_time, Cdouble, (Cint,))


"""
trixi_load_element_averaged_primitive_vars(simstate_handle::Cint, variable_id::Cint,
data::Ptr{Cdouble})::Cvoid
Expand Down
14 changes: 14 additions & 0 deletions LibTrixi.jl/src/api_jl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ function trixi_load_element_averaged_primitive_vars_jl(simstate, variable_id, da
end


function trixi_store_in_database_jl(simstate, index, data)
simstate.data[index] = Ref(data)
sloede marked this conversation as resolved.
Show resolved Hide resolved
if show_debug_output()
println("New data vector stored at index ", index)
end
return nothing
end


function trixi_get_time_jl(simstate)
return simstate.integrator.t
end


function trixi_get_t8code_forest_jl(simstate)
mesh, _, _, _ = mesh_equations_solver_cache(simstate.semi)
return mesh.forest
Expand Down
13 changes: 11 additions & 2 deletions LibTrixi.jl/src/simulationstate.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
const LibTrixiDataBaseType = Vector{Ref{Vector{Float64}}}
sloede marked this conversation as resolved.
Show resolved Hide resolved

"""
SimulationState

Data structure to store a simulation state that consists of a semidiscretization
plus the time integrator.
Data structure to store a simulation state consisting of
- a semidiscretization
- the time integrator
- an optional array of data vectors
"""
mutable struct SimulationState{SemiType, IntegratorType}
semi::SemiType
integrator::IntegratorType
data::LibTrixiDataBaseType

function SimulationState(semi, integrator, data = LibTrixiDataBaseType())
return new{typeof(semi), typeof(integrator)}(semi, integrator, data)
end
end

# Global variables to store different simulation states
Expand Down
15 changes: 15 additions & 0 deletions LibTrixi.jl/test/test_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,24 @@ end
dt_jl = trixi_calculate_dt_jl(simstate_jl)
@test dt_c == dt_jl

# compare time
time_c = trixi_get_time(handle)
time_jl = trixi_get_time_jl(simstate_jl)
@test time_c == time_jl

# compare finished status
@test trixi_is_finished(handle) == 0
@test !trixi_is_finished_jl(simstate_jl)

# manually increase databases (for testing only!)
push!(simstate_jl.data, Vector{Float64}())
push!(LibTrixi.simstates[handle].data, Vector{Float64}())
# store a vector
test_data = [1.0, 2.0, 3.0]
trixi_store_in_database(handle, Int32(1), Int32(3), pointer(test_data))
trixi_store_in_database_jl(simstate_jl, 1, test_data)
# check that the same memory is referenced
@test pointer(simstate_jl.data[1][]) == pointer(LibTrixi.simstates[handle].data[1][])
end


Expand Down
2 changes: 1 addition & 1 deletion examples/external/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Specify the minimum version
cmake_minimum_required ( VERSION 3.9 )
cmake_minimum_required ( VERSION 3.12 )

# Specify a project name
project(ExternalLibTrixi)
Expand Down
57 changes: 56 additions & 1 deletion src/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ enum {
TRIXI_FPTR_LOAD_NODE_WEIGHTS,
TRIXI_FTPR_LOAD_PRIMITIVE_VARS,
TRIXI_FTPR_LOAD_ELEMENT_AVERAGED_PRIMITIVE_VARS,
TRIXI_FTPR_STORE_IN_DATABASE,
TRIXI_FTPR_VERSION_LIBRARY,
TRIXI_FTPR_VERSION_LIBRARY_MAJOR,
TRIXI_FTPR_VERSION_LIBRARY_MINOR,
Expand All @@ -32,6 +33,7 @@ enum {
TRIXI_FTPR_VERSION_JULIA_EXTENDED,
TRIXI_FTPR_EVAL_JULIA,
TRIXI_FTPR_GET_T8CODE_FOREST,
TRIXI_FPTR_GET_TIME,

// The last one is for the array size
TRIXI_NUM_FPTRS
Expand Down Expand Up @@ -60,14 +62,16 @@ static const char* trixi_function_pointer_names[] = {
[TRIXI_FPTR_LOAD_NODE_WEIGHTS] = "trixi_load_node_weights_cfptr",
[TRIXI_FTPR_LOAD_PRIMITIVE_VARS] = "trixi_load_primitive_vars_cfptr",
[TRIXI_FTPR_LOAD_ELEMENT_AVERAGED_PRIMITIVE_VARS] = "trixi_load_element_averaged_primitive_vars_cfptr",
[TRIXI_FTPR_STORE_IN_DATABASE] = "trixi_store_in_database_cfptr",
[TRIXI_FTPR_VERSION_LIBRARY] = "trixi_version_library_cfptr",
[TRIXI_FTPR_VERSION_LIBRARY_MAJOR] = "trixi_version_library_major_cfptr",
[TRIXI_FTPR_VERSION_LIBRARY_MINOR] = "trixi_version_library_minor_cfptr",
[TRIXI_FTPR_VERSION_LIBRARY_PATCH] = "trixi_version_library_patch_cfptr",
[TRIXI_FTPR_VERSION_JULIA] = "trixi_version_julia_cfptr",
[TRIXI_FTPR_VERSION_JULIA_EXTENDED] = "trixi_version_julia_extended_cfptr",
[TRIXI_FTPR_EVAL_JULIA] = "trixi_eval_julia_cfptr",
[TRIXI_FTPR_GET_T8CODE_FOREST] = "trixi_get_t8code_forest_cfptr"
[TRIXI_FTPR_GET_T8CODE_FOREST] = "trixi_get_t8code_forest_cfptr",
[TRIXI_FPTR_GET_TIME] = "trixi_get_time_cfptr"
};

// Track initialization/finalization status to prevent unhelpful errors
Expand Down Expand Up @@ -691,6 +695,57 @@ void trixi_load_element_averaged_primitive_vars(int handle, int variable_id, dou
}


/**
* @anchor trixi_store_in_database_api_c
*
* @brief Store data vector in current simulation's database
*
* A reference to the passed data array data will be stored in the database of the
* simulation given by simstate_handle at given index. The database object has to be
* created in init_simstate() of the running libelixir and can be used throughout the
* simulation.
*
* The database object has to exist, has to be of type `LibTrixiDataBaseType`, and has to
* hold enough data references such that access at `index` is valid.
*
* The size of data has to match size.
*
* @param[in] handle simulation handle
* @param[in] index index in database where data vector will be stored
* @param[in] size size of given data vector
* @param[in] data data vector to store
*/
void trixi_store_in_database(int handle, int index, int size, const double * data) {

// Get function pointer
void (*store_in_database)(int, int, int, const double *) =
trixi_function_pointers[TRIXI_FTPR_STORE_IN_DATABASE];

// Call function
store_in_database(handle, index, size, data);
}


/**
* @anchor trixi_get_time_api_c
*
* @brief Return current physical time.
*
* @param[in] handle simulation handle
*
* @return physical time
*/
double trixi_get_time(int handle) {

// Get function pointer
double (*get_time)(int) = trixi_function_pointers[TRIXI_FPTR_GET_TIME];

// Call function
return get_time(handle);
}



/******************************************************************************************/
/* T8code */
/******************************************************************************************/
Expand Down
36 changes: 35 additions & 1 deletion src/api.f90
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,24 @@ subroutine trixi_load_primitive_vars(handle, variable_id, data) bind(c)
use, intrinsic :: iso_c_binding, only: c_int, c_double
integer(c_int), value, intent(in) :: handle
integer(c_int), value, intent(in) :: variable_id
real(c_double), dimension(*), intent(in) :: data
real(c_double), dimension(*), intent(out) :: data
end subroutine

!>
!! @fn LibTrixi::trixi_get_time::trixi_get_time(handle)
!!
!! @brief Return current physical time.
!!
!! @param[in] handle simulation handle
!!
!! @return physical time
!!
!! @see @ref trixi_get_time_api_c "trixi_get_time (C API)"
real(c_double) function trixi_get_time(handle) bind(c)
use, intrinsic :: iso_c_binding, only: c_int, c_double
integer(c_int), value, intent(in) :: handle
end function

!>
!! @fn LibTrixi::trixi_load_element_averaged_primitive_vars::trixi_load_element_averaged_primitive_vars(handle, variable_id, data)
!!
Expand All @@ -405,6 +420,25 @@ subroutine trixi_load_element_averaged_primitive_vars(handle, variable_id, data)
use, intrinsic :: iso_c_binding, only: c_int, c_double
integer(c_int), value, intent(in) :: handle
integer(c_int), value, intent(in) :: variable_id
real(c_double), dimension(*), intent(out) :: data
end subroutine

!>
!! @fn LibTrixi::trixi_store_in_database::trixi_store_in_database(handle, variable_id, data)
!!
!! @brief Store data vector in current simulation's database
!!
!! @param[in] handle simulation handle
!! @param[in] index index in database where data vector will be stored
!! @param[in] size size of given data vector
!! @param[in] data data vector to store
!!
!! @see @ref trixi_store_in_database_api_c "trixi_store_in_database (C API)"
subroutine trixi_store_in_database(handle, index, size, data) bind(c)
use, intrinsic :: iso_c_binding, only: c_int, c_double
integer(c_int), value, intent(in) :: handle
integer(c_int), value, intent(in) :: index
integer(c_int), value, intent(in) :: size
real(c_double), dimension(*), intent(in) :: data
end subroutine

Expand Down
2 changes: 2 additions & 0 deletions src/trixi.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ int trixi_ndofselement(int handle);
int trixi_nvariables(int handle);
int trixi_nnodes(int handle);
double trixi_calculate_dt(int handle);
double trixi_get_time(int handle);
void trixi_load_node_reference_coordinates(int handle, double* node_coords);
void trixi_load_node_weights(int handle, double* node_weights);
void trixi_load_primitive_vars(int handle, int variable_id, double * data);
void trixi_load_element_averaged_primitive_vars(int handle, int variable_id, double * data);
void trixi_store_in_database(int handle, int index, int size, const double * data);

// T8code
#if !defined(T8_H) && !defined(T8_FOREST_GENERAL_H)
Expand Down
5 changes: 0 additions & 5 deletions test/c/interface_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ extern "C" {
// Julia project path defined via cmake
const char * julia_project_path = JULIA_PROJECT_PATH;

// Example libexlixir
const char * libelixir_path =
"../../../LibTrixi.jl/examples/libelixir_p4est2d_dgsem_euler_sedov.jl";


TEST(CInterfaceTest, JuliaProject) {

// be evil
Expand Down
18 changes: 14 additions & 4 deletions test/c/simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ TEST(CInterfaceTest, SimulationRun) {
EXPECT_DEATH(trixi_is_finished(42),
"the provided handle was not found in the stored simulation states: 42");

// Store a vector in database
std::vector<double> test_data(3);
trixi_store_in_database(handle, 1, 3, test_data.data());
EXPECT_DEATH(trixi_store_in_database(handle, 2, 3, test_data.data()),
"BoundsError");

// Do 10 simulation steps
for (int i = 0; i < 10; ++i) {
trixi_step(handle);
Expand All @@ -48,6 +54,10 @@ TEST(CInterfaceTest, SimulationRun) {
// Check time step length
double dt = trixi_calculate_dt(handle);
EXPECT_NEAR(dt, 0.0028566952356658794, 1e-17);

// Check time
double time = trixi_get_time(handle);
EXPECT_NEAR(time, 0.0304927240859461, 1e-16);

// Check finished status
int finished_status = trixi_is_finished(handle);
Expand Down Expand Up @@ -95,7 +105,7 @@ TEST(CInterfaceTest, SimulationRun) {
std::vector<double> energy(ndofs);
trixi_load_primitive_vars(handle, 1, rho.data());
trixi_load_primitive_vars(handle, 4, energy.data());
// check memory boarders
// check memory borders
EXPECT_DOUBLE_EQ(rho[0], 1.0);
EXPECT_DOUBLE_EQ(rho[ndofs-1], 1.0);
EXPECT_DOUBLE_EQ(energy[0], 1.0e-5);
Expand All @@ -111,7 +121,7 @@ TEST(CInterfaceTest, SimulationRun) {
trixi_load_element_averaged_primitive_vars(handle, 3, v2_averages.data());
trixi_load_element_averaged_primitive_vars(handle, 4, e_averages.data());
if (nranks == 1) {
// check memory boarders (densities at the beginning, energies at the end)
// check memory borders (densities at the beginning, energies at the end)
EXPECT_DOUBLE_EQ(rho_averages[0], 1.0);
EXPECT_DOUBLE_EQ(e_averages[nelements-1], 1.0e-5);
// check values somewhere near the center (expect symmetries)
Expand All @@ -128,7 +138,7 @@ TEST(CInterfaceTest, SimulationRun) {
}
else if (nranks == 2) {
if (rank == 0) {
// check memory boarders (densities at the beginning, energies at the end)
// check memory borders (densities at the beginning, energies at the end)
EXPECT_DOUBLE_EQ(rho_averages[0], 1.0);
EXPECT_DOUBLE_EQ(e_averages[nelements-1], 1.0e-5);
// check values somewhere near the center (expect symmetries)
Expand All @@ -140,7 +150,7 @@ TEST(CInterfaceTest, SimulationRun) {
EXPECT_NEAR(v2_averages[94], -0.14037267400591, 1e-14);
}
else {
// check memory boarders (densities at the beginning, energies at the end)
// check memory borders (densities at the beginning, energies at the end)
EXPECT_DOUBLE_EQ(rho_averages[0], 1.0);
EXPECT_DOUBLE_EQ(e_averages[nelements-1], 1.0e-5);
// check values somewhere near the center (expect symmetries)
Expand Down
Loading
Loading