Skip to content

Commit 2940b55

Browse files
benegeesloede
andauthored
Make t8code available in libtrixi (#97)
* first steps towards accessing Trixi data * the real changes * copy of t8code hello world * make cmake find t8code * link against t8code * project_dir is meanwhile not used anymore * add libelexir using T8codeMesh * example for retrieving T8codeMesh via libtrixi * get examples right * add t8code forest getter * demonstrate also data access * fix merge accidents * cleanup * adapt example to current version in Trixi.jl * rename trixi_get_t8code_mesh to trixi_get_t8ode_forest * adapt example, include is required to prevent conflicting declaration * adapt example [skip ci] * just to test the CI [no ci] * check if disabling checks still works * update cmake calls [no ci] * correct t8code path? * aha... * add trixi_get_t8code_cmesh * remove t8code include from trixi.h provide forward declaration of t8_cmesh_t and t8_forest_t, which may be overwritten by the real t8code definitions * add t8code example * make t8code optional * safeguard declaration of t8code types * add t8code C-test * add t8code Julia test * increase coverage of example * Update CMakeLists.txt * Update examples/CMakeLists.txt * Update test/c/interface_c.cpp * Update CMakeLists.txt Co-authored-by: Michael Schlottke-Lakemper <michael@sloede.com> * Update LibTrixi.jl/test/test_t8code.jl Co-authored-by: Michael Schlottke-Lakemper <michael@sloede.com> * Update src/trixi.h Co-authored-by: Michael Schlottke-Lakemper <michael@sloede.com> * fix indentation * remove trixi_get_t8code_cmesh --------- Co-authored-by: Michael Schlottke-Lakemper <michael@sloede.com>
1 parent a4b45b2 commit 2940b55

15 files changed

Lines changed: 358 additions & 2 deletions

File tree

.github/workflows/ci.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ jobs:
9797
cd build
9898
cmake .. -DCMAKE_INSTALL_PREFIX=../install \
9999
-DCMAKE_BUILD_TYPE=Release \
100+
-DT8CODE_PREFIX=$PWD/../t8code-local/prefix \
100101
-DENABLE_TESTING=ON -DJULIA_PROJECT_PATH=../libtrixi-julia
101102
102103
- name: Configure (test_type == 'coverage')
@@ -106,6 +107,7 @@ jobs:
106107
cd build
107108
cmake .. -DCMAKE_INSTALL_PREFIX=../install \
108109
-DCMAKE_BUILD_TYPE=Debug \
110+
-DT8CODE_PREFIX=$PWD/../t8code-local/prefix \
109111
-DCMAKE_C_FLAGS="-cpp --coverage -O0" \
110112
-DCMAKE_Fortran_FLAGS="-cpp --coverage -O0" \
111113
-DCMAKE_EXE_LINKER_FLAGS="--coverage" \
@@ -145,6 +147,7 @@ jobs:
145147
--force
146148
cp ../install/share/libtrixi/LibTrixi.jl/examples/libelixir_tree1d_dgsem_advection_basic.jl .
147149
cp ../install/share/libtrixi/LibTrixi.jl/examples/libelixir_p4est2d_dgsem_euler_sedov.jl .
150+
cp ../install/share/libtrixi/LibTrixi.jl/examples/libelixir_t8code_2d_dgsem_advection_basic.jl .
148151
149152
- name: Prepare coverage reporting
150153
if: ${{ matrix.test_type == 'coverage' }}
@@ -171,6 +174,7 @@ jobs:
171174
../build/examples/simple_trixi_controller_f . libelixir_p4est2d_dgsem_euler_sedov.jl
172175
../build/examples/trixi_controller_data_c . libelixir_p4est2d_dgsem_euler_sedov.jl
173176
../build/examples/trixi_controller_data_f . libelixir_p4est2d_dgsem_euler_sedov.jl
177+
../build/examples/simple_t8code_c . libelixir_t8code_2d_dgsem_advection_basic.jl
174178
env:
175179
LIBTRIXI_DEBUG: all
176180

@@ -185,7 +189,9 @@ jobs:
185189
"../build/examples/trixi_controller_data_c" \
186190
"../build/examples/trixi_controller_data_c ." \
187191
"../build/examples/trixi_controller_data_f" \
188-
"../build/examples/trixi_controller_data_f ."
192+
"../build/examples/trixi_controller_data_f ." \
193+
"../build/examples/simple_t8code_c" \
194+
"../build/examples/simple_t8code_c ."
189195
do
190196
$command
191197
if [ $? -ne 2 ]; then

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ list ( APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/" )
3434
# Find Julia
3535
find_package( Julia REQUIRED )
3636

37+
# Find t8code
38+
find_package( T8code )
39+
if ( NOT T8CODE_FOUND )
40+
message( NOTICE "t8code not found: t8code examples will NOT be built")
41+
endif()
42+
3743
# Find MPI
3844
find_package( MPI REQUIRED )
3945

@@ -83,6 +89,9 @@ set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR})
8389

8490
# Include directories
8591
target_include_directories( ${PROJECT_NAME} PRIVATE src ${JULIA_INCLUDE_DIRS} )
92+
if ( T8CODE_FOUND )
93+
target_include_directories( ${PROJECT_NAME} PRIVATE ${T8CODE_INCLUDE_DIR} )
94+
endif()
8695

8796
# Libraries to link
8897
target_link_libraries( ${PROJECT_NAME} PRIVATE ${JULIA_LIBRARY} MPI::MPI_C )
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using LibTrixi
2+
using OrdinaryDiffEq
3+
using Trixi
4+
5+
# The function to create the simulation state needs to be named `init_simstate`
6+
function init_simstate()
7+
8+
###############################################################################
9+
# semidiscretization of the linear advection equation
10+
11+
advection_velocity = (0.2, -0.7)
12+
equations = LinearScalarAdvectionEquation2D(advection_velocity)
13+
14+
# Create DG solver with polynomial degree = 3 and (local) Lax-Friedrichs/Rusanov flux as surface flux
15+
solver = DGSEM(polydeg=3, surface_flux=flux_lax_friedrichs)
16+
17+
coordinates_min = (-1.0, -1.0) # minimum coordinates (min(x), min(y))
18+
coordinates_max = ( 1.0, 1.0) # maximum coordinates (max(x), max(y))
19+
20+
mapping = Trixi.coordinates2mapping(coordinates_min, coordinates_max)
21+
22+
trees_per_dimension = (4, 4)
23+
24+
mesh = T8codeMesh(trees_per_dimension, polydeg=3,
25+
mapping=mapping,
26+
initial_refinement_level=1)
27+
28+
# A semidiscretization collects data structures and functions for the spatial discretization
29+
semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition_convergence_test, solver)
30+
31+
32+
###############################################################################
33+
# ODE solvers, callbacks etc.
34+
35+
# Create ODE problem with time span from 0.0 to 1.0
36+
ode = semidiscretize(semi, (0.0, 1.0));
37+
38+
# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup
39+
# and resets the timers
40+
summary_callback = SummaryCallback()
41+
42+
# The AnalysisCallback allows to analyse the solution in regular intervals and prints the results
43+
analysis_interval = 100
44+
analysis_callback = AnalysisCallback(semi, interval=analysis_interval)
45+
46+
alive_callback = AliveCallback(analysis_interval=analysis_interval)
47+
48+
# The StepsizeCallback handles the re-calculation of the maximum Δt after each time step
49+
stepsize_callback = StepsizeCallback(cfl=0.5)
50+
51+
# Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver
52+
callbacks = CallbackSet(summary_callback,
53+
analysis_callback,
54+
alive_callback,
55+
stepsize_callback)
56+
57+
58+
###############################################################################
59+
# create the time integrator
60+
61+
# OrdinaryDiffEq's `integrator`
62+
integrator = init(ode, CarpenterKennedy2N54(williamson_condition=false),
63+
dt=1.0, # solve needs some value here but it will be overwritten by the stepsize_callback
64+
save_everystep=false, callback=callbacks);
65+
66+
###############################################################################
67+
# Create simulation state
68+
69+
simstate = SimulationState(semi, integrator)
70+
71+
return simstate
72+
end

LibTrixi.jl/src/LibTrixi.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ export trixi_version_julia,
5555
export trixi_version_julia_extended,
5656
trixi_version_julia_extended_cfptr,
5757
trixi_version_julia_extended_jl
58+
export trixi_get_t8code_forest,
59+
trixi_get_t8code_forest_cfptr,
60+
trixi_get_t8code_forest_jl
5861

5962
export SimulationState, store_simstate, load_simstate, delete_simstate!
6063

LibTrixi.jl/src/api_c.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,23 @@ end
354354

355355
trixi_load_cell_averages_cfptr() =
356356
@cfunction(trixi_load_cell_averages, Cvoid, (Ptr{Cdouble}, Cint,))
357+
358+
359+
"""
360+
trixi_get_t8code_forest(simstate_handle::Cint)::::Ptr{Trixi.t8_forest}
361+
362+
Return t8code forest of the current T8codeMesh.
363+
364+
!!! warning "Experimental"
365+
The interface to t8code is experimental and implementation details may change at any
366+
time without warning.
367+
"""
368+
function trixi_get_t8code_forest end
369+
370+
Base.@ccallable function trixi_get_t8code_forest(simstate_handle::Cint)::Ptr{Trixi.t8_forest}
371+
simstate = load_simstate(simstate_handle)
372+
return trixi_get_t8code_forest_jl(simstate)
373+
end
374+
375+
trixi_get_t8code_forest_cfptr() =
376+
@cfunction(trixi_get_t8code_forest, Ptr{Trixi.t8_forest}, (Cint,))

LibTrixi.jl/src/api_jl.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,9 @@ function trixi_load_cell_averages_jl(data, simstate)
129129

130130
return nothing
131131
end
132+
133+
134+
function trixi_get_t8code_forest_jl(simstate)
135+
mesh, _, _, _ = Trixi.mesh_equations_solver_cache(simstate.semi)
136+
return mesh.forest
137+
end

LibTrixi.jl/test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ using Test
44
include("test_auxiliary.jl")
55
include("test_function_pointers.jl")
66
include("test_interface.jl")
7+
include("test_t8code.jl")
78
end

LibTrixi.jl/test/test_t8code.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module TestT8code
2+
3+
using Test
4+
using LibTrixi
5+
using Trixi
6+
7+
8+
libelixir = joinpath(dirname(pathof(LibTrixi)),
9+
"../examples/libelixir_t8code_2d_dgsem_advection_basic.jl")
10+
11+
# initialize a simulation via API, receive a handle
12+
handle = trixi_initialize_simulation(libelixir)
13+
14+
# initialize the same simulation directly from julia, get a simstate object
15+
simstate_jl = trixi_initialize_simulation_jl(libelixir)
16+
17+
18+
@testset verbose=true showtiming=true "T8code mesh" begin
19+
# compare t8code forest
20+
forest_c = trixi_get_t8code_forest(handle)
21+
@test forest_c isa Ptr{Trixi.t8_forest}
22+
@test forest_c != C_NULL
23+
end
24+
25+
26+
# finalize simulation from julia
27+
trixi_finalize_simulation_jl(simstate_jl)
28+
29+
# finalize simulation from API
30+
trixi_finalize_simulation(handle)
31+
32+
end # module

cmake/FindT8code.cmake

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#
2+
# Stop if already found
3+
#
4+
if ( T8CODE_FOUND )
5+
return()
6+
endif()
7+
8+
9+
10+
#
11+
# Look for include, libs, executables
12+
#
13+
find_path ( T8CODE_INCLUDE_DIR t8.h PATHS ${T8CODE_PREFIX}/include )
14+
find_program ( T8CODE_VERSION_EXE t8_version PATHS ${T8CODE_PREFIX}/bin )
15+
find_library ( T8CODE_LIBRARY t8 PATHS ${T8CODE_PREFIX}/lib )
16+
find_library ( P4EST_LIBRARY p4est PATHS ${T8CODE_PREFIX}/lib )
17+
find_library ( SC_LIBRARY sc PATHS ${T8CODE_PREFIX}/lib )
18+
19+
20+
21+
#
22+
# Extract version
23+
#
24+
execute_process(
25+
COMMAND ${T8CODE_VERSION_EXE}
26+
OUTPUT_VARIABLE T8CODE_VERSION_STRING
27+
RESULT_VARIABLE RESULT
28+
)
29+
30+
if( RESULT EQUAL 0 )
31+
string(REGEX REPLACE "\\[t8\\] ([0-9]+\\.[0-9]+\\.[0-9]+).*" "\\1" T8CODE_VERSION_STRING ${T8CODE_VERSION_STRING} )
32+
endif ()
33+
34+
35+
36+
#
37+
# Finalize
38+
#
39+
include ( FindPackageHandleStandardArgs )
40+
find_package_handle_standard_args(
41+
T8code
42+
REQUIRED_VARS T8CODE_LIBRARY SC_LIBRARY P4EST_LIBRARY T8CODE_INCLUDE_DIR
43+
VERSION_VAR T8CODE_VERSION_STRING
44+
)
45+
46+
set ( T8CODE_LIBRARIES ${T8CODE_LIBRARY} ${P4EST_LIBRARY} ${SC_LIBRARY})

examples/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
set ( EXAMPLES
22
simple_trixi_controller.c
33
simple_trixi_controller.f90
4+
simple_t8code.c
45
trixi_controller_data.c
56
trixi_controller_data.f90 )
67

8+
if ( NOT T8CODE_FOUND )
9+
list( FILTER EXAMPLES EXCLUDE REGEX ".*(t|T)8(c|C)(o|O)(d|D)(e|E).*" )
10+
endif()
711

812
foreach ( EXAMPLE ${EXAMPLES} )
913

@@ -29,12 +33,18 @@ foreach ( EXAMPLE ${EXAMPLES} )
2933
${TARGET_NAME}
3034
PRIVATE MPI::MPI_${EXAMPLE_LANG} ${PROJECT_NAME} ${PROJECT_NAME}_tls
3135
)
36+
if ( T8CODE_FOUND )
37+
target_link_libraries( ${TARGET_NAME} PRIVATE ${T8CODE_LIBRARIES} )
38+
endif()
3239

3340
# set include directories
3441
target_include_directories(
3542
${TARGET_NAME}
3643
PRIVATE ${CMAKE_SOURCE_DIR}/src
3744
)
45+
if ( T8CODE_FOUND )
46+
target_include_directories( ${TARGET_NAME} PRIVATE ${T8CODE_INCLUDE_DIR} )
47+
endif()
3848

3949
# set runtime path for installed binaries
4050
set_target_properties(

0 commit comments

Comments
 (0)