#loops #isl #image-processing #stencil

rs_isl

2-dimensional generic Iterative Stencil Loops

3 unstable releases

0.2.0 Oct 29, 2024
0.1.2 Oct 4, 2024
0.1.1 Oct 4, 2024
0.1.0 Oct 4, 2024

#84 in Simulation

MIT/Apache

22KB
422 lines

rs_isl

Crates.io Version docs.rs page Crates.io Downloads Crates.io License CI

rs_isl is an implementation of Iterative Stencil Loops in Rust. ISLs can be used in a variety of scenarios such as image processing, fluid simulation and the calculation of PDEs. For more information see Wikipedia.

Example

This animation was created with Paraview from the data created by the example two_waves.

Output

rs_isl writes .vtk output files to a specified path. Their contents may be defined by the user.

To create those files rs_isl uses the vtkio crate.

Usage

General

use core::f64;
use std::{cmp::max, path::PathBuf};
use rs_isl::*;

fn main() {
    // create a domain with a size of 200 by 100
    let dim = (200, 100)
    // we only access the left neighbour of every cell
    let neighbours = vec![(-1, 0)];
    // take neighbours value, if there is no neighbour decrease by 3
    let op = |num: &f32, nb: Vec<Option<&f32>>| {
        if nb.first().unwrap().is_some() {
            let f = *nb[0].unwrap();
            return f;
        }
        return max(*num as i32 - 3, 0) as f32;
    };
    // creates a sine shape at the left boundary of the domain
    let init = |x: usize, _y: usize| {
        if x < DIM.0 / 10 {
            let fac = x as f64 / (DIM.0 / 10) as f64 * f64::consts::FRAC_PI_2;
            return (250.0 - 250.0 * fac.sin()) as f32;
        }
        0.0
    };
    // create the simulation parameters
    let params = IslParams::new(
        dim,
        op,
        // number of threads for simulation, the domain size must be divisible by this number
        10,
        init,
        // number of simulation steps
        200,
        // number of output steps
        100,
        neighbours,
        // path for writing vtk files
        PathBuf::from("raw"),
    );
    // run the simulation
    run_isl(params).unwrap();
}

Setting output values for a custom data type

struct Point {
    x: u32,
    y: u32,
}

impl VtkOutput for Point {
    fn value_names() -> Vec<String> {
        // define the names of the DataArrays
        vec!["x_coord".into(), "y_coord".into()]
    }
    fn cellvalue(&self) -> Vec<f32> {
        // write the values for every cell
        vec![self.x as f32, self.y as f32]
    }
}

Dependencies

~4–8.5MB
~87K SLoC