Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
37e6014
fix #74
nbassler Mar 31, 2026
584e176
Refactor dedx_load_config to return an int for success/failure
nbassler Mar 31, 2026
7e5b477
Refactor symbol naming guidelines in CONTRIBUTING.md and improve memo…
nbassler Mar 31, 2026
0d94d07
cleanup dedx_validate
nbassler Mar 31, 2026
6ff80e0
cleanup and fix bad return value
nbassler Mar 31, 2026
89ed109
Refactor to replace _DEDX_MAXELEMENTS with DEDX_MAX_ELEMENTS and incl…
nbassler Mar 31, 2026
b42b68c
Refactor split functions to improve naming consistency and memory man…
nbassler Mar 31, 2026
d330e31
Refactor spline functions to improve naming consistency and enhance c…
nbassler Mar 31, 2026
69592b9
Refactor header files to improve structure and organization by moving…
nbassler Mar 31, 2026
b30c33b
Refactor to replace deprecated functions with updated internal counte…
nbassler Mar 31, 2026
83ea700
fix also a bug in compound state handling in mstar
nbassler Mar 31, 2026
5cf0f2e
Refactor includes and remove unused workspace header to improve code …
nbassler Mar 31, 2026
49b8e02
Refactor dedx_file functions to use const char* return type and updat…
nbassler Mar 31, 2026
3611f3f
Refactor to replace deprecated functions with internal counterparts f…
nbassler Mar 31, 2026
21aa9c4
fix typo in titanium def
nbassler Mar 31, 2026
7b0470c
fix bethe and header guards
nbassler Mar 31, 2026
37de83c
Add unit tests for dedx validation and evaluation functions
nbassler Mar 31, 2026
b8b7826
fix codecov icon
nbassler Mar 31, 2026
4f4f5d7
Refactor code to improve error handling and memory management; update…
nbassler Mar 31, 2026
700179a
Add test suite for dedx functions with error handling and validation
nbassler Mar 31, 2026
42569d7
Add comprehensive test suite for dedx functionality and error handling
nbassler Mar 31, 2026
2148e3e
Add tests for getdedx functionality and reorder includes in test_core…
nbassler Mar 31, 2026
55d532f
Refactor error handling in check_mode_equivalence function for improv…
nbassler Mar 31, 2026
0d39c9c
be realistic with codecov
nbassler Mar 31, 2026
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
9 changes: 9 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
coverage:
status:
patch:
default:
target: 70%
threshold: 2%
project:
default:
target: auto
4 changes: 2 additions & 2 deletions .github/workflows/clang-tidy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ jobs:
run: |
run-clang-tidy-19 \
-p build \
-header-filter='(libdedx|tests|examples|buildbins)/' \
'(libdedx|tests|examples|buildbins)/.*\.c$'
-header-filter='(libdedx|tests|examples)/' \
'(libdedx|tests|examples)/.*\.c$'
36 changes: 31 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@

## C coding style

### Symbol naming and linkage

Use symbol names to make visibility obvious:

- Public API functions and types use the `dedx_*` prefix.
- Shared internal helpers used across multiple `.c` files use the `dedx_internal_*` prefix.
- File-local helpers should be declared `static` and use short unprefixed names.

Examples:

```c
/* public API */
float dedx_get_stp(dedx_workspace *ws, dedx_config *config, float energy, int *err);

/* shared internal helper */
float dedx_internal_get_atom_mass(int id, int *err);

/* file-local helper */
static int check_energy_bounds(_dedx_lookup_data *data, float energy);
```

Do not introduce new `_dedx_*` identifiers. Existing ones should be migrated to
the scheme above as code is touched.

### Variable declarations

Declare all variables at the top of their enclosing block, before any statements.
Expand Down Expand Up @@ -36,10 +60,12 @@ fine when the variable is genuinely local to that scope.
## Thread safety

libdedx is currently **not thread-safe**. There is no synchronization around
the static path cache in `_dedx_get_data_path()`, nor around workspace mutation
in `dedx_load_config()` / `_dedx_load_data()`. Do not share a `dedx_workspace`
the static path cache in the file-local data-path helper in
[`libdedx/dedx_file_access.c`](libdedx/dedx_file_access.c),
nor around workspace mutation in `dedx_load_config()` / the internal dataset
loading helpers. Do not share a `dedx_workspace`
across threads without external locking.

The intended fix is to replace the `done` flag in `_dedx_get_data_path()` with
C11 `call_once()`, and audit the rest of the library for shared mutable state.
This is tracked as a known issue.
The intended fix is to replace the cached-path `done` flag with C11
`call_once()`, and audit the rest of the library for shared mutable state. This
is tracked as a known issue.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# libdedx

[![CI](https://github.com/APTG/libdedx/actions/workflows/ci.yml/badge.svg)](https://github.com/APTG/libdedx/actions/workflows/ci.yml)
[![Coverage](https://codecov.io/gh/APTG/libdedx/graph/badge.svg?branch=main)](https://codecov.io/gh/APTG/libdedx)[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
[![Coverage](https://codecov.io/gh/APTG/libdedx/branch/main/graph/badge.svg)](https://app.codecov.io/gh/APTG/libdedx/tree/main)[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)

A C library for stopping power calculations (dE/dx) — the energy loss of a charged particle per unit length of material.

Expand All @@ -20,6 +20,11 @@ Full documentation: https://aptg.github.io/libdedx/
| `DEDX_BETHE_EXT00` | Bethe formula with extensions | All ions |
| `DEDX_ICRU` | Auto-selects ICRU49 or ICRU73 by ion type | p, He, heavy |

Note:
For compound targets, `libdedx` currently extends the native coverage of some original programs by falling back to Bragg/stoichiometric weighting when the upstream database does not provide that compound directly. This applies in particular to programs such as `DEDX_PSTAR`, `DEDX_ASTAR`, and `DEDX_MSTAR`, whose original target coverage is more limited than the full set of ICRU/ESTAR-style materials exposed by `libdedx`.

This behavior is useful, but it means that a result returned under a given program label is not always a direct value from the original upstream program. Future releases may separate native program coverage more explicitly from these weighted extension modes.

## Quick start

One-call API for a single stopping power value:
Expand Down
5 changes: 4 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ foreach(example dedx_example dedx_bethe dedx_list dedx_use_wrappers dedx_get_sam
add_test(NAME ${example} COMMAND ${example})
endforeach()

install (TARGETS getdedx DESTINATION bin)
add_test(NAME getdedx_lookup COMMAND getdedx 2 1 276 100)
add_test(NAME getdedx_list_programs COMMAND getdedx -1 1 276 100)

install (TARGETS getdedx DESTINATION bin)
38 changes: 26 additions & 12 deletions examples/dedx_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

int main(int argc, char *argv[]) {
int err = 0;
int exit_code = 0;
int prog = -1;
int target = DEDX_WATER;
int z = -1;
Expand All @@ -16,10 +17,17 @@ int main(int argc, char *argv[]) {
int i = 0;
float energy = -1.0;
float stp = 0;
dedx_workspace *ws;
dedx_workspace *ws = NULL;
dedx_config *cfg = (dedx_config *) calloc(1, sizeof(dedx_config));
char *str = (char *) malloc(100);

if (cfg == NULL || str == NULL) {
free(cfg);
free(str);
fprintf(stderr, "Allocation failure.\n");
return 1;
}

if (argc != 5) {
dedx_get_version(&vmaj, &vmin, &vpatch);
printf("\n This is getdedx using libdEdx version %i.%i.%i", vmaj, vmin, vpatch);
Expand All @@ -35,7 +43,7 @@ int main(int argc, char *argv[]) {
printf(" %s MSTAR CARBON WATER 100\n", argv[0]);
printf(" %s PSTAR HYDROGEN -1 100\n", argv[0]);
printf("\n");
return 0;
goto cleanup;
}

if (isdigit(*argv[1]))
Expand Down Expand Up @@ -98,7 +106,7 @@ int main(int argc, char *argv[]) {
i++;
}
printf("\n");
return 0;
goto cleanup;
}

if (z == -1) {
Expand All @@ -110,7 +118,7 @@ int main(int argc, char *argv[]) {
i++;
}
printf("\n");
return 0;
goto cleanup;
}

if (target == -1) {
Expand All @@ -124,7 +132,7 @@ int main(int argc, char *argv[]) {
printf("\nAdditional materials are possible by Braggs additivity rule,\n");
printf("as long as the constituent elements are availble in the list above.");
printf("\n");
return 0;
goto cleanup;
}

if (energy == -1) {
Expand All @@ -133,7 +141,7 @@ int main(int argc, char *argv[]) {
dedx_get_ion_name(z),
dedx_get_min_energy(prog, z),
dedx_get_max_energy(prog, z));
return 0;
goto cleanup;
}

if ((energy < dedx_get_min_energy(prog, z)) || (energy > dedx_get_max_energy(prog, z))) {
Expand All @@ -142,7 +150,8 @@ int main(int argc, char *argv[]) {
dedx_get_ion_name(z),
dedx_get_min_energy(prog, z),
dedx_get_max_energy(prog, z));
exit(-1);
exit_code = 1;
goto cleanup;
}

if (prog == DEDX_ESTAR) {
Expand All @@ -166,7 +175,8 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "dedx_initialize, error %i:", err);
dedx_get_error_code(str, err);
fprintf(stderr, " %s\n", str);
exit(1);
exit_code = 1;
goto cleanup;
}

/* load configuration */
Expand All @@ -179,7 +189,8 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "dedx_load_config, error %i:", err);
dedx_get_error_code(str, err);
fprintf(stderr, " %s\n", str);
exit(1);
exit_code = 1;
goto cleanup;
}

/* get stopping power */
Expand All @@ -188,7 +199,8 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "dedx_read_energy, error %i:", err);
dedx_get_error_code(str, err);
fprintf(stderr, " %s\n", str);
exit(1);
exit_code = 1;
goto cleanup;
}

/* check if braggs additivity rule was used */
Expand All @@ -198,9 +210,11 @@ int main(int argc, char *argv[]) {
}
printf("1/rho dE/dx = %6.3E MeV cm2/g\n", stp);

dedx_free_workspace(ws, &err);
cleanup:
if (ws != NULL)
dedx_free_workspace(ws, &err);
dedx_free_config(cfg, &err);
free(str);

return 0;
return exit_code;
}
5 changes: 5 additions & 0 deletions examples/dedx_get_sample_csv.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <dedx.h>
#include <stdio.h>
#include <stdlib.h>

#include "dedx_wrappers.h"

Expand All @@ -10,6 +11,10 @@ int main() {
const int ions[] = {DEDX_HYDROGEN, DEDX_HELIUM, DEDX_OXYGEN, DEDX_CARBON, DEDX_URANIUM, DEDX_IRON, -1};

FILE *file = fopen("result.txt", "a");
if (file == NULL) {
fprintf(stderr, "Unable to open result.txt for writing.\n");
return 1;
}
fprintf(file, "Program, Material, Ion, Energy, Stopping Power \n");
for (int i = 0; programs[i] != -1; i++) {
const char *prog_name = dedx_get_program_name(programs[i]);
Expand Down
1 change: 1 addition & 0 deletions examples/dedx_use_wrappers.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <dedx.h>
#include <stdio.h>
#include <stdlib.h>

#include "dedx_wrappers.h"

Expand Down
6 changes: 3 additions & 3 deletions libdedx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ESTAR removed from installer
set(dedx_data data/ASTAR.bin data/astarEng.dat data/mstarEng.dat data/pstarEng.bin data/ASTAR.dat data/compos.txt data/MSTAR.dat data/gas_states.dat data/pstarEng.dat data/astarEng.bin data/effective_charge.dat data/MSTAR.bin data/mstarEng.bin data/PSTAR.bin data/PSTAR.dat data/short_names data/ICRU73.dat data/ICRU73.bin data/ICRU_PSTAR.dat data/ICRU_PSTAR.bin data/ICRU_ASTAR.dat data/ICRU_ASTAR.bin data/icru73Eng.dat data/icru73Eng.bin data/ICRU73_NEW.dat data/ICRU73_NEW.bin data/icru73_newEng.dat data/icru73_newEng.bin data/composition data/betheEng.dat data/betheEng.bin data/icru_pstarEng.dat data/icru_pstarEng.bin data/icru_astarEng.dat data/icru_astarEng.bin data/atima_compos)
set(dedx_data data/ASTAR.bin data/astarEng.dat data/mstarEng.dat data/pstarEng.bin data/ASTAR.dat data/compos.txt data/MSTAR.dat data/gas_states.dat data/pstarEng.dat data/astarEng.bin data/effective_charge.dat data/MSTAR.bin data/mstarEng.bin data/PSTAR.bin data/PSTAR.dat data/short_names data/ICRU73.dat data/ICRU73.bin data/ICRU_PSTAR.dat data/ICRU_PSTAR.bin data/ICRU_ASTAR.dat data/ICRU_ASTAR.bin data/icru73Eng.dat data/icru73Eng.bin data/ICRU73_NEW.dat data/ICRU73_NEW.bin data/icru73_newEng.dat data/icru73_newEng.bin data/composition data/betheEng.dat data/betheEng.bin data/icru_pstarEng.dat data/icru_pstarEng.bin data/icru_astarEng.dat data/icru_astarEng.bin)

set(dedx_data_bin data/ASTAR.bin data/astarEng.bin data/ICRU73.bin data/icru73Eng.bin data/ICRU73_NEW.bin data/icru73_newEng.bin data/MSTAR.bin data/mstarEng.bin data/PSTAR.bin data/ICRU_PSTAR.bin data/ICRU_ASTAR.bin data/pstarEng.bin data/betheEng.bin data/icru_astarEng.bin data/icru_pstarEng.bin data/compos.txt data/effective_charge.dat data/short_names data/composition data/gas_states.dat data/atima_compos)
set(dedx_data_bin data/ASTAR.bin data/astarEng.bin data/ICRU73.bin data/icru73Eng.bin data/ICRU73_NEW.bin data/icru73_newEng.bin data/MSTAR.bin data/mstarEng.bin data/PSTAR.bin data/ICRU_PSTAR.bin data/ICRU_ASTAR.bin data/pstarEng.bin data/betheEng.bin data/icru_astarEng.bin data/icru_pstarEng.bin data/compos.txt data/effective_charge.dat data/short_names data/composition data/gas_states.dat)

# Object library: compiled once with -fPIC so the objects feed both the
# static and the shared library without a second compilation pass.
Expand Down Expand Up @@ -47,7 +47,7 @@ if(NOT WIN32)
target_link_libraries(dedx_shared PUBLIC m)
endif()

install(FILES dedx.h dedx_error.h dedx_tools.h dedx_wrappers.h DESTINATION include)
install(FILES dedx.h dedx_elements.h dedx_error.h dedx_tools.h dedx_wrappers.h DESTINATION include)

install(TARGETS dedx dedx_shared
RUNTIME DESTINATION bin
Expand Down
92 changes: 0 additions & 92 deletions libdedx/data/atima_compos

This file was deleted.

Loading
Loading