Skip to content

About

C++ implementation of Walker's Aliasing algorithm

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

36 Commits

Folders and files

Repository files navigation

Walker Aliasing

This project provides a C++ implementation of Walker's alias method, an algorithm for drawing samples from an arbitrary discrete probability distribution in constant time after an initial preprocessing step. The preprocessing constructs an alias table and a corresponding cutoff table from the supplied bin weights.

The implementation reads a CSV file whose first column contains bin edges. A selected subsequent column contains a density for each bin; the final row provides the upper edge of the last bin. Bin widths may be nonuniform, including logarithmic spacing. The densities do not need to be normalized. Samples are drawn from the resulting distribution and uniformly within the selected bin.

Origin and attribution

This implementation is adapted from the Walker-aliasing routine used by the PENELOPE radiation-transport code. PENELOPE was originally implemented in FORTRAN 77; the relevant algorithm has been transferred and adapted to C++ in this repository.

The original PENELOPE Fortran source is not included in the public version of this repository. For the PENELOPE algorithm and radiation-transport code, see Baró et al., PENELOPE: An algorithm for Monte Carlo simulation of the penetration and energy loss of electrons and positrons in matter (DOI: 10.1016/0168-583X(95)00349-5).

Development environment

Tests and examples are built by default. The test suite requires Catch2 3, which is not downloaded or built by this project. Choose one of the following ways to provide it with Conda.

Option 1: dedicated environment

Create and activate the complete development environment supplied with the repository:

conda env create -f environment.yml
conda activate walker-aliasing

Option 2: install only Catch2

Install Catch2 into an existing Conda environment:

conda install -c conda-forge "catch2>=3,<4"

CMake automatically searches the active Conda environment through CONDA_PREFIX.

Build and test

With either environment option active:

cmake -S . -B build
cmake --build build
ctest --test-dir build --output-on-failure

Examples are built by default when this is the top-level project. They can be disabled independently of the tests:

cmake -S . -B build \
    -DBUILD_TESTING=OFF \
    -DWALKER_ALIASING_BUILD_EXAMPLES=OFF

Examples

Basic sampling

The basic example loads a small logarithmically binned distribution and prints ten samples. It has no additional runtime dependencies:

cd examples/basic
cmake -S . -B build
cmake --build build
./build/walker_aliasing_basic_example data/distribution.csv

Its source, input, and additional instructions are under examples/basic/.

Spectrum demonstration

The spectrum example draws a requested number of samples, bins them, and scales the generated spectrum to the input spectrum's total fluence. Run it from its directory:

cd examples/spectrum
cmake -S . -B build
cmake --build build
mkdir -p output
./build/walker_aliasing_spectrum_example \
    data/ge_N1e9_R50nm/output_ge_N1e9_dPhidE.csv \
    1 \
    100000

The second argument is the zero-based CSV column containing the desired spectrum (0 is reserved for bin edges), so the same example can sample photon or electron columns. Its output preserves the input file's linear or logarithmic bin edges. The filename is derived from the input; this example writes output/output_ge_N1e9_dPhidE-generated.csv.

An optional plotting script compares the generated spectrum with the input. It requires NumPy, pandas, and Matplotlib:

python plot_spectra.py \
    --input data/ge_N1e9_R50nm/output_ge_N1e9_dPhidE.csv \
    --generated output/output_ge_N1e9_dPhidE-generated.csv \
    --column 1

By default, the PNG uses the generated spectrum's basename with a .png suffix. Pass --output PATH.png to override it.

For logarithmic x-axes, the plot shows E dφ/dE; linear plots show dφ/dE. The plotting script does not normalize either spectrum.

More details are available in examples/spectrum/README.md.

Using the C++ library

When WalkerAliasing is included directly in another CMake project, link to its namespaced target:

add_subdirectory(path/to/walker-aliasing)

target_link_libraries(
    my_application
    PRIVATE WalkerAliasing::WalkerAliasing
)

The same target works when the repository is obtained with CMake's FetchContent.

The library can also be installed and discovered with find_package:

cmake -S . -B build \
    -DBUILD_TESTING=OFF \
    -DWALKER_ALIASING_BUILD_EXAMPLES=OFF \
    -DCMAKE_INSTALL_PREFIX=/path/to/prefix
cmake --build build
cmake --install build

A consuming project can then use:

find_package(WalkerAliasing CONFIG REQUIRED)
target_link_libraries(
    my_application
    PRIVATE WalkerAliasing::WalkerAliasing
)

If the installation prefix is not searched automatically, pass it to the consumer as -DCMAKE_PREFIX_PATH=/path/to/prefix.

Tests

The regression test records the alias and cutoff tables produced by the current implementation for a fixed, nontrivial distribution. These tables define the required behavior: future refactorings or alternative implementations must reproduce them and pass the same test.

Project layout

  • include/ and src/ contain the walker_aliasing library.
  • examples/basic/ contains the minimal example, its input, and instructions.
  • examples/spectrum/ contains the more elaborate spectrum demonstration, including its plotting utility and example data.
  • tests/ contains the C++ regression test and its input distribution.
  • cmake/ contains the package-configuration template used by the installation rules. It enables downstream projects to find an installed copy with find_package(WalkerAliasing).
  • environment.yml defines the optional Conda development environment.

References

About

C++ implementation of Walker's Aliasing algorithm

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages