Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,20 @@ cd libtrixi-julia
--p4est-library <p4est_install_directory>/lib/libp4est.so
<install_directory>
```

Use `libtrixi-init-julia -h` to get help.
When running a program that uses libtrixi, make sure to set up the `JULIA_DEPOT_PATH`
environment variable to point to the `<julia-depot>` folder reported. In your code, pass
the path to the `libtrixi-julia` directory to `trixi_initialize`, see the code of the
examples.

In your code, pass the path to the `libtrixi-julia` directory to `trixi_initialize`,
see the code of the examples. If you did not modify the default value for the Julia depot
when calling `libtrixi-init-julia`, libtrixi will find it automatically.
Otherwise, when running a program that uses libtrixi, you need to make sure to set the
`JULIA_DEPOT_PATH` environment variable to point to the `<julia-depot>` folder reported.

### Testing

Go to some directory from where you want to run a Trixi simulation.

```shell
LIBTRIXI_DEBUG=all \
JULIA_DEPOT_PATH=<julia-depot> \
<install_directory>/bin/simple_trixi_controller_c \
<libtrixi-julia_directory> \
<install_directory>/share/libtrixi/LibTrixi.jl/examples/libelixir_tree1d_dgsem_advection_basic.jl
Expand Down
12 changes: 6 additions & 6 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,20 @@ cd libtrixi-julia
--p4est-library <p4est_install_directory>/lib/libp4est.so
<install_directory>
```

Use `libtrixi-init-julia -h` to get help.
When running a program that uses libtrixi, make sure to set up the `JULIA_DEPOT_PATH`
environment variable to point to the `<julia-depot>` folder reported. In your code, pass
the path to the `libtrixi-julia` directory to `trixi_initialize`, see the code of the
examples.

In your code, pass the path to the `libtrixi-julia` directory to `trixi_initialize`,
see the code of the examples. If you did not modify the default value for the Julia depot
when calling `libtrixi-init-julia`, libtrixi will find it automatically.
Otherwise, when running a program that uses libtrixi, you need to make sure to set the
`JULIA_DEPOT_PATH` environment variable to point to the `<julia-depot>` folder reported.

### Testing

Go to some directory from where you want to run a Trixi simulation.

```shell
LIBTRIXI_DEBUG=all \
JULIA_DEPOT_PATH=<julia-depot> \
<install_directory>/bin/simple_trixi_controller_c \
<libtrixi-julia_directory> \
<install_directory>/share/libtrixi/LibTrixi.jl/examples/libelixir_tree1d_dgsem_advection_basic.jl
Expand Down
3 changes: 2 additions & 1 deletion examples/simple_trixi_controller.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

#include <trixi.h>
Expand Down Expand Up @@ -51,7 +52,7 @@ int main ( int argc, char *argv[] ) {

// Initialize Trixi
printf("\n*** Trixi controller *** Initialize Trixi\n");
trixi_initialize( argv[1] );
trixi_initialize( argv[1], NULL );

// Set up the Trixi simulation
// We get a handle to use subsequently
Expand Down
65 changes: 59 additions & 6 deletions src/trixi.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -16,6 +17,9 @@ static jl_value_t* checked_eval_string(const char* code, const char* func, const
// Auxiliary function to determine debug level
static int show_debug_output();

// Auxiliary function to update JULIA_DEPOT_PATH environment variable
static void update_depot_path(const char * project_directory, const char * depot_path);

// Store function pointers to avoid overhead of `jl_eval_string`
enum {
TRIXI_FTPR_INITIALIZE_SIMULATION,
Expand All @@ -39,13 +43,23 @@ static const char* trixi_function_pointer_names[] = {
[TRIXI_FTPR_FINALIZE_SIMULATION] = "trixi_finalize_simulation_cfptr",
};

// Default depot path *relative* to the project directory
// OBS! If you change the value here, you should also update the default value of
// `LIBTRIXI_JULIA_DEPOT` in `utils/libtrixi-init-julia` accordingly
static const char* default_depot_path = "julia-depot";


/** Initialize Julia runtime environment
*
* \todo Path is still hard-coded
* Initialize Julia and activate the project at `project_directory`. If `depot_path` is not
* a null pointer, forcefully set the environment variable `JULIA_DEPOT_PATH` to the value
* of `depot_path`. If `depot_path` *is* null, then proceed as follows:
* If `JULIA_DEPOT_PATH` is already set, do not touch it. Otherwise, set `JULIA_DEPOT_PATH`
* to `project_directory` + `default_depot_path`
*/
void trixi_initialize(const char * project_directory) {
void trixi_initialize(const char * project_directory, const char * depot_path) {
// Update JULIA_DEPOT_PATH environment variable before initializing Julia
update_depot_path(project_directory, depot_path);

// Init Julia
jl_init();
Expand All @@ -58,8 +72,7 @@ void trixi_initialize(const char * project_directory) {
"Pkg.status();\n";
const char * activate = show_debug_output() ? activate_debug : activate_regular;
if ( strlen(activate) + strlen(project_directory) + 1 > 1024 ) {
fprintf(stderr, "error: buffer size not sufficient for activation command\n");
exit(1);
print_and_die("buffer size not sufficient for activation command", LOC);
}
char buffer[1024];
snprintf(buffer, 1024, activate, project_directory);
Expand Down Expand Up @@ -197,6 +210,46 @@ void julia_eval_string(const char * code) {
};


/** Set JULIA_DEPOT_PATH environment variable appropriately
*
*/
void update_depot_path(const char * project_directory, const char * depot_path) {
// Set/modify Julia's depot path if desired
if (depot_path != NULL) {
// If depot path is provided as an argument, set environment variable JULIA_DEPOT_PATH
// to it
setenv("JULIA_DEPOT_PATH", depot_path, 1);
if (show_debug_output()) {
printf("JULIA_DEPOT_PATH set to \"%s\"\n", depot_path);
}
} else if (getenv("JULIA_DEPOT_PATH") == NULL) {
// Otherwise, if environment variable is *not* already set, set it to
// `project_directory` + `default_depot_path`

// Verify that buffer size is large enough (+2 for '/' and trailing null)
char path[1024];
if ( strlen(project_directory) + strlen(default_depot_path) + 2 > 1024 ) {
print_and_die("buffer size not sufficient for depot path construction", LOC);
}

// Construct complete path
strcpy(path, project_directory);
strcat(path, "/");
strcat(path, default_depot_path);

// Construct absolute path
char absolute_path[PATH_MAX];
realpath(path, absolute_path);

// Set environment variable
setenv("JULIA_DEPOT_PATH", absolute_path, 1);
if (show_debug_output()) {
printf("JULIA_DEPOT_PATH set to \"%s\"\n", absolute_path);
}
}
}


/* Run Julia command and check for errors
*
* Adapted from the Julia repository.
Expand Down Expand Up @@ -227,9 +280,9 @@ void print_and_die(const char* message, const char* func, const char* file, int
exit(1);
}

static int show_debug_output() {
int show_debug_output() {
const char * env = getenv("LIBTRIXI_DEBUG");
if (!env) {
if (env == NULL) {
return 0;
}

Expand Down
15 changes: 11 additions & 4 deletions src/trixi.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module LibTrixi
implicit none

interface
subroutine trixi_initialize_c(project_directory) bind(c, name='trixi_initialize')
subroutine trixi_initialize_c(project_directory, depot_path) bind(c, name='trixi_initialize')
use, intrinsic :: iso_c_binding, only: c_char
character(kind=c_char), dimension(*), intent(in) :: project_directory
character(kind=c_char), dimension(*), intent(in), optional :: depot_path
end subroutine

integer(c_int) function trixi_initialize_simulation_c(libelixir) bind(c, name='trixi_initialize_simulation')
Expand Down Expand Up @@ -50,11 +51,17 @@ logical function trixi_is_finished(handle)
trixi_is_finished = trixi_is_finished_c(handle) == 1
end function

subroutine trixi_initialize(project_directory)
subroutine trixi_initialize(project_directory, depot_path)
use, intrinsic :: iso_c_binding, only: c_null_char
character(len=*), intent(in) :: project_directory

call trixi_initialize_c(trim(adjustl(project_directory)) // c_null_char)
character(len=*), intent(in), optional :: depot_path

if (present(depot_path)) then
call trixi_initialize_c(trim(adjustl(project_directory)) // c_null_char, &
trim(adjustl(depot_path)) // c_null_char)
else
call trixi_initialize_c(trim(adjustl(project_directory)) // c_null_char)
end if
end subroutine

integer(c_int) function trixi_initialize_simulation(libelixir)
Expand Down
2 changes: 1 addition & 1 deletion src/trixi.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef TRIXI_H_
#define TRIXI_H_

void trixi_initialize(const char * project_directory);
void trixi_initialize(const char * project_directory, const char * depot_path);
int trixi_initialize_simulation(const char * libelixir);
double trixi_calculate_dt(int handle);
int trixi_is_finished(int handle);
Expand Down
2 changes: 2 additions & 0 deletions utils/libtrixi-init-julia
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ if [ -z "$LIBTRIXI_JULIA_EXEC" ]; then
LIBTRIXI_JULIA_EXEC=julia
fi
if [ -z "$LIBTRIXI_JULIA_DEPOT" ]; then
# OBS! If you change this value here, you should also update the default value for
# `default_depot_path` in `src/trixi.c` accordingly
LIBTRIXI_JULIA_DEPOT=julia-depot
fi
while [[ $# -gt 0 ]]; do
Expand Down