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

Document and unify C and Fortran examples #133

Merged
merged 23 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
32 changes: 16 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ jobs:
--force
cp ../install/share/libtrixi/LibTrixi.jl/examples/libelixir_tree1d_dgsem_advection_basic.jl .
cp ../install/share/libtrixi/LibTrixi.jl/examples/libelixir_p4est2d_dgsem_euler_sedov.jl .
cp ../install/share/libtrixi/LibTrixi.jl/examples/libelixir_t8code_2d_dgsem_advection_basic.jl .
cp ../install/share/libtrixi/LibTrixi.jl/examples/libelixir_t8code_2d_dgsem_advection_amr.jl .

- name: Initialize project directory
if: ${{ matrix.test_type == 'package-compiler' }}
Expand Down Expand Up @@ -194,7 +194,7 @@ jobs:
run: |
cd examples/external
./build.sh
mpirun -n 2 ./build/simple_trixi_controller_c \
mpirun -n 2 ./build/trixi_controller_simple_c \
../../libtrixi-julia \
../../LibTrixi.jl/examples/libelixir_p4est2d_dgsem_euler_sedov.jl
env:
Expand All @@ -221,21 +221,21 @@ jobs:
if: ${{ matrix.test_type == 'regular' || matrix.test_type == 'coverage' }}
run: |
cd libtrixi-julia
../build/examples/simple_trixi_controller_c . libelixir_tree1d_dgsem_advection_basic.jl
../build/examples/simple_trixi_controller_f . libelixir_tree1d_dgsem_advection_basic.jl
../build/examples/simple_trixi_controller_c . libelixir_p4est2d_dgsem_euler_sedov.jl
../build/examples/simple_trixi_controller_f . libelixir_p4est2d_dgsem_euler_sedov.jl
../build/examples/trixi_controller_data_c . libelixir_p4est2d_dgsem_euler_sedov.jl
../build/examples/trixi_controller_data_f . libelixir_p4est2d_dgsem_euler_sedov.jl
../build/examples/simple_t8code_c . libelixir_t8code_2d_dgsem_advection_basic.jl
../build/examples/trixi_controller_simple_c . libelixir_tree1d_dgsem_advection_basic.jl
../build/examples/trixi_controller_simple_f . libelixir_tree1d_dgsem_advection_basic.jl
../build/examples/trixi_controller_simple_c . libelixir_p4est2d_dgsem_euler_sedov.jl
../build/examples/trixi_controller_simple_f . libelixir_p4est2d_dgsem_euler_sedov.jl
../build/examples/trixi_controller_data_c . libelixir_t8code_2d_dgsem_advection_amr.jl
../build/examples/trixi_controller_data_f . libelixir_t8code_2d_dgsem_advection_amr.jl
../build/examples/trixi_controller_t8code_c . libelixir_t8code_2d_dgsem_advection_amr.jl
env:
LIBTRIXI_DEBUG: all

- name: Run examples
if: ${{ matrix.test_type == 'package-compiler' }}
run: |
cd build/examples
mpirun -n 2 simple_trixi_controller_c \
mpirun -n 2 trixi_controller_simple_c \
../../libtrixi-julia \
../../LibTrixi.jl/examples/libelixir_p4est2d_dgsem_euler_sedov.jl
env:
Expand All @@ -246,16 +246,16 @@ jobs:
run: |
cd libtrixi-julia
set +e # disable early exit on non-zero exit code
for command in "../build/examples/simple_trixi_controller_c" \
"../build/examples/simple_trixi_controller_c ." \
"../build/examples/simple_trixi_controller_f" \
"../build/examples/simple_trixi_controller_f ." \
for command in "../build/examples/trixi_controller_simple_c" \
"../build/examples/trixi_controller_simple_c ." \
"../build/examples/trixi_controller_simple_f" \
"../build/examples/trixi_controller_simple_f ." \
"../build/examples/trixi_controller_data_c" \
"../build/examples/trixi_controller_data_c ." \
"../build/examples/trixi_controller_data_f" \
"../build/examples/trixi_controller_data_f ." \
"../build/examples/simple_t8code_c" \
"../build/examples/simple_t8code_c ."
"../build/examples/trixi_controller_t8code_c" \
"../build/examples/trixi_controller_t8code_c ."
do
$command
if [ $? -ne 2 ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function init_simstate()

mapping = Trixi.coordinates2mapping(coordinates_min, coordinates_max)

trees_per_dimension = (4, 4)
trees_per_dimension = (2, 2)

mesh = T8codeMesh(trees_per_dimension, polydeg=3,
mapping=mapping,
Expand All @@ -32,8 +32,8 @@ function init_simstate()
###############################################################################
# ODE solvers, callbacks etc.

# Create ODE problem with time span from 0.0 to 1.0
ode = semidiscretize(semi, (0.0, 1.0));
# Create ODE problem with time span from 0.0 to 0.2
ode = semidiscretize(semi, (0.0, 0.2));

# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup
# and resets the timers
Expand All @@ -48,10 +48,21 @@ function init_simstate()
# The StepsizeCallback handles the re-calculation of the maximum Δt after each time step
stepsize_callback = StepsizeCallback(cfl=0.5)

# The AMRCallback triggers adaptive mesh refinement
amr_controller = ControllerThreeLevel(semi, IndicatorMax(semi, variable=first),
base_level=2,
med_level=3, med_threshold=0.1,
max_level=4, max_threshold=0.6)
amr_callback = AMRCallback(semi, amr_controller,
interval=10,
adapt_initial_condition=true,
adapt_initial_condition_only_refine=true)

# Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver
callbacks = CallbackSet(summary_callback,
analysis_callback,
alive_callback,
amr_callback,
stepsize_callback)


Expand Down
2 changes: 1 addition & 1 deletion LibTrixi.jl/test/test_t8code.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using Trixi


libelixir = joinpath(dirname(pathof(LibTrixi)),
"../examples/libelixir_t8code_2d_dgsem_advection_basic.jl")
"../examples/libelixir_t8code_2d_dgsem_advection_amr.jl")

# initialize a simulation via API, receive a handle
handle = trixi_initialize_simulation(libelixir)
Expand Down
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Go to some directory from where you want to run a Trixi simulation.

```shell
LIBTRIXI_DEBUG=all \
<install_directory>/bin/simple_trixi_controller_c \
<install_directory>/bin/trixi_controller_simple_c \
<libtrixi-julia_directory> \
<install_directory>/share/libtrixi/LibTrixi.jl/examples/libelixir_tree1d_dgsem_advection_basic.jl
```
Expand Down Expand Up @@ -207,20 +207,25 @@ Current time step length: 0.050000
────────────────────────────────────────────────────────────────────────────────────
```

If you change the executable name from `simple_trixi_controller_c` to
`simple_trixi_controller_f`, you will get a near identical output. The corresponding source
files `simple_trixi_controller.c` and `simple_trixi_controller.f90` give you an idea on how
to use the C and Fortran APIs of libtrixi, and can be found in the
[`examples/`](examples/) folder.
If you change the executable name from `trixi_controller_simple_c` to
`trixi_controller_simple_f`, you will get a near identical output. The corresponding source
files can be found in the [`examples/`](examples/) folder. The examples demonstrate different
aspects on how to use the C and Fortran APIs of libtrixi:

- `trixi_controller_simple.(c|f90)`: basic usage
- `trixi_controller_mpi.(c|f90)`: usage in the presence of MPI
- `trixi_controller_data.(c|f90)`: simulation data access
- `trixi_controller_t8code.c`: interacting with t8code
benegee marked this conversation as resolved.
Show resolved Hide resolved
(there is no Fortran example yet as the Fortran interface of t8code is still under development)

If you just want to test the Julia part of libtrixi, i.e., LibTrixi.jl, you can also run
everything from Julia.
`trixi_controller_simple.jl` from Julia.

```shell
JULIA_DEPOT_PATH=<julia-depot_directory> \
LIBTRIXI_DEBUG=all \
julia --project=<libtrixi-julia_directory>
<install_directory>/share/libtrixi/examples/simple_trixi_controller.jl
<install_directory>/share/libtrixi/examples/trixi_controller_simple.jl
<install_directory>/share/libtrixi/LibTrixi.jl/examples/libelixir_tree1d_dgsem_advection_basic.jl
```

Expand All @@ -247,7 +252,7 @@ the `examples/` directory as
```shell
make -f MakefileExternal LIBTRIXI_PREFIX=path/to/libtrixi/prefix
```
to build `simple_trixi_controller_f`.
to build `trixi_controller_simple_f`.

#### CMake

Expand Down Expand Up @@ -289,12 +294,12 @@ To try this out, perform the following steps:
make
```
- Go to the `examples` folder in the repository root and compile
`simple_trixi_controller_c`:
`trixi_controller_simple_c`:
```shell
cd examples
make -f MakefileCompiled LIBTRIXI_PREFIX=$PWD/../LibTrixi.jl/lib/build
```
This will create a `simple_trixi_controller_c` file.
This will create a `trixi_controller_simple_c` file.

*using cmake*
- Add
Expand All @@ -305,7 +310,7 @@ To try this out, perform the following steps:
3. From inside the `examples` folder you should be able to run the example (in parallel)
with the following command:
```shell
mpirun -n 2 simple_trixi_controller_c \
mpirun -n 2 trixi_controller_simple_c \
../libtrixi-julia \
../LibTrixi.jl/examples/libelixir_p4est2d_dgsem_euler_sedov.jl
```
Expand Down
35 changes: 22 additions & 13 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Go to some directory from where you want to run a Trixi simulation.

```shell
LIBTRIXI_DEBUG=all \
<install_directory>/bin/simple_trixi_controller_c \
<install_directory>/bin/trixi_controller_simple_c \
<libtrixi-julia_directory> \
<install_directory>/share/libtrixi/LibTrixi.jl/examples/libelixir_tree1d_dgsem_advection_basic.jl
```
Expand Down Expand Up @@ -209,18 +209,27 @@ Current time step length: 0.050000
────────────────────────────────────────────────────────────────────────────────────
```

If you change the executable name from `simple_trixi_controller_c` to
`simple_trixi_controller_f`, you will get a near identical output. The corresponding source
files `simple_trixi_controller.c` and `simple_trixi_controller.f90` give you an idea on how
to use the C and Fortran APIs of libtrixi, and can be found in the
[`examples/`](https://github.com/trixi-framework/libtrixi/tree/main/examples/) folder.
If you change the executable name from `trixi_controller_simple_c` to
`trixi_controller_simple_f`, you will get a near identical output. The corresponding source
files can be found in the
[`examples/`](https://github.com/trixi-framework/libtrixi/tree/main/examples/) folder. The
examples demonstrate different aspects on how to use the C and Fortran APIs of libtrixi:

- `trixi_controller_simple.(c|f90)`: basic usage
- `trixi_controller_mpi.(c|f90)`: usage in the presence of MPI
- `trixi_controller_data.(c|f90)`: simulation data access
- `trixi_controller_t8code.c`: interacting with t8code
(there is no Fortran example yet as the Fortran interface of t8code is still under development)

If you just want to test the Julia part of libtrixi, i.e., LibTrixi.jl, you can also run
everything from Julia. From the repository root, execute
`trixi_controller_simple.jl` from Julia.

```shell
JULIA_DEPOT_PATH=$PWD/libtrixi-julia/julia-depot \
JULIA_DEPOT_PATH=<julia-depot_directory> \
LIBTRIXI_DEBUG=all \
julia --project=<libtrixi-julia_directory>
<install_directory>/share/libtrixi/LibTrixi.jl/examples/simple_trixi_controller.jl
<install_directory>/share/libtrixi/examples/trixi_controller_simple.jl
<install_directory>/share/libtrixi/LibTrixi.jl/examples/libelixir_tree1d_dgsem_advection_basic.jl
```

Note: Most auxiliary output is hidden unless the environment variable `LIBTRIXI_DEBUG` is
Expand All @@ -246,7 +255,7 @@ which can be invoked from inside the `examples/` directory as
```shell
make -f MakefileExternal LIBTRIXI_PREFIX=path/to/libtrixi/prefix
```
to build `simple_trixi_controller_f`.
to build `trixi_controller_simple_f`.

#### CMake

Expand Down Expand Up @@ -289,12 +298,12 @@ To try this out, perform the following steps:
make
```
- Go to the `examples` folder in the repository root and compile
`simple_trixi_controller_c`:
`trixi_controller_simple_c`:
```shell
cd examples
make -f MakefileCompiled LIBTRIXI_PREFIX=$PWD/../LibTrixi.jl/lib/build
```
This will create a `simple_trixi_controller_c` file.
This will create a `trixi_controller_simple_c` file.

*using cmake*
- Add
Expand All @@ -305,7 +314,7 @@ To try this out, perform the following steps:
3. From inside the `examples` folder you should be able to run the example (in parallel)
with the following command:
```shell
mpirun -n 2 simple_trixi_controller_c \
mpirun -n 2 trixi_controller_simple_c \
../libtrixi-julia \
../LibTrixi.jl/examples/libelixir_p4est2d_dgsem_euler_sedov.jl
```
Expand Down
10 changes: 8 additions & 2 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
simple_trixi_controller_c
simple_trixi_controller_f
trixi_controller_simple_c
trixi_controller_simple_f
benegee marked this conversation as resolved.
Show resolved Hide resolved
trixi_controller_mpi_c
trixi_controller_mpi_f
trixi_controller_data_c
trixi_controller_data_f
trixi_controller_t8code_c
trixi_controller_t8code_f
12 changes: 7 additions & 5 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
set ( EXAMPLES
simple_trixi_controller.c
simple_trixi_controller.f90
simple_t8code.c
trixi_controller_simple.c
trixi_controller_simple.f90
trixi_controller_mpi.c
trixi_controller_mpi.f90
trixi_controller_data.c
trixi_controller_data.f90 )
trixi_controller_data.f90
trixi_controller_t8code.c )

if ( NOT T8CODE_FOUND )
list( FILTER EXAMPLES EXCLUDE REGEX ".*(t|T)8(c|C)(o|O)(d|D)(e|E).*" )
Expand Down Expand Up @@ -67,4 +69,4 @@ foreach ( EXAMPLE ${EXAMPLES} )
endforeach()

# install the julia controller example as well
install( FILES simple_trixi_controller.jl DESTINATION share/libtrixi/examples/ )
install( FILES trixi_controller_simple.jl DESTINATION share/libtrixi/examples/ )
2 changes: 1 addition & 1 deletion examples/MakefileCompiled
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ CFLAGS ?= -fPIC
LDFLAGS ?= -L$(libdir) -Wl,-rpath,$(libdir) -L$(MPI_PREFIX)/lib -Wl,-rpath,$(MPI_PREFIX)/lib
LIBS ?= -ltrixi -lmpi

simple_trixi_controller_c: simple_trixi_controller.c
trixi_controller_simple_c: trixi_controller_simple.c
$(CC) $< -o $@ $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LIBS)
2 changes: 1 addition & 1 deletion examples/MakefileExternal
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ FC ?= f95
libdir := $(LIBTRIXI_PREFIX)/lib
incdir := $(LIBTRIXI_PREFIX)/include

simple_trixi_controller_f: simple_trixi_controller.f90
trixi_controller_simple_f: trixi_controller_simple.f90
$(FC) $< -o $@ -fPIC -I$(incdir) -L$(libdir) -Wl,-rpath,$(libdir) -ltrixi $(libdir)/libtrixi_tls.o
4 changes: 2 additions & 2 deletions examples/external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ find_package( MPI REQUIRED )
find_package( LibTrixi REQUIRED )

# Set sources
set ( TARGET_NAME simple_trixi_controller_c )
add_executable ( ${TARGET_NAME} ${CMAKE_SOURCE_DIR}/../simple_trixi_controller.c )
set ( TARGET_NAME trixi_controller_simple_c )
add_executable ( ${TARGET_NAME} ${CMAKE_SOURCE_DIR}/../trixi_controller_simple.c )

# Set libraries to link
target_link_libraries(
Expand Down
38 changes: 23 additions & 15 deletions examples/trixi_controller_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,38 @@ int main ( int argc, char *argv[] ) {
printf("\n*** Trixi controller *** Set up Trixi simulation\n");
int handle = trixi_initialize_simulation( argv[2] );

// Get number of variables
int nvariables = trixi_nvariables( handle );
printf("\n*** Trixi controller *** nvariables %d\n", nvariables);

// Main loop
int steps = 0;
int nelements = 0;
double* data = NULL;

printf("\n*** Trixi controller *** Entering main loop\n");
while ( !trixi_is_finished( handle ) ) {

trixi_step( handle );
}
steps++;

// get number of elements
int nelements = trixi_nelements( handle );
printf("\n*** Trixi controller *** nelements %d\n", nelements);
if (steps % 10 == 0) {

// get number of variables
int nvariables = trixi_nvariables( handle );
printf("\n*** Trixi controller *** nvariables %d\n", nvariables);

// allocate memory
double* data = malloc( sizeof(double) * nelements * nvariables );
// Get number of elements
nelements = trixi_nelements( handle );
printf("\n*** Trixi controller *** nelements %d\n", nelements);

// get averaged cell values for each variable
trixi_load_cell_averages(data, handle);
// Allocate memory
data = realloc( data, sizeof(double) * nelements * nvariables );
benegee marked this conversation as resolved.
Show resolved Hide resolved

// compute temperature
const double gas_constant = 0.287;
// Get averaged cell values for each variable
trixi_load_cell_averages(data, handle);
}
}

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

// Finalize Trixi simulation
Expand All @@ -60,5 +66,7 @@ int main ( int argc, char *argv[] ) {
printf("\n*** Trixi controller *** Finalize Trixi\n");
trixi_finalize();

free(data);

return 0;
}
Loading