6 Module N_FLUIDS
gullradriel edited this page 2026-10-29 20:54:40 +01:00

FLUIDS: fluid simulation example

Structures

Name
struct N_FLUID_THREAD_PARAMS
structure passed to a threaded fluid process
struct N_FLUID
structure of a fluid

Functions

Name
N_FLUID * new_n_fluid(double density, double gravity, size_t numIters, double dt, double overRelaxation, size_t sx, size_t sy)
create a new fluid simulation
int n_fluid_solveIncompressibility(N_FLUID * fluid)
solve incompressibility constraint
int n_fluid_simulate_threaded(N_FLUID * fluid, THREAD_POOL * thread_pool)
run one simulation step using thread pool
int n_fluid_simulate(N_FLUID * fluid)
run one simulation step
int n_fluid_setObstacle(N_FLUID * fluid, double x, double y, double vx, double vy, double r)
set an obstacle in the fluid
double n_fluid_sampleField(N_FLUID * fluid, double x, double y, uint32_t field)
sample a field value at given coordinates
int n_fluid_resetObstacles(N_FLUID * fluid)
reset all obstacles
int n_fluid_integrate(N_FLUID * fluid)
integrate fluid velocities
ALLEGRO_COLOR n_fluid_getSciColor(const N_FLUID * fluid, double val, double minVal, double maxVal)
get a scientific color for a value
int n_fluid_extrapolate(N_FLUID * fluid)
extrapolate fluid boundaries
int n_fluid_draw(N_FLUID * fluid)
draw the fluid simulation
double n_fluid_avgV(const N_FLUID * fluid, size_t i, size_t j)
compute average V velocity at cell
double n_fluid_avgU(const N_FLUID * fluid, size_t i, size_t j)
compute average U velocity at cell
int n_fluid_advectVel(N_FLUID * fluid)
advect velocity field
int n_fluid_advectSmoke(N_FLUID * fluid)
advect smoke density field
int destroy_n_fluid(N_FLUID ** fluid)
destroy a fluid simulation

Defines

Name
_zd(__fluid, __value)
test if value is near zero, according to fluid's precision
_z(__fluid, __component)
test if component is near zero, according to fluid's precision

Functions Documentation

function new_n_fluid

N_FLUID * new_n_fluid(
    double density,
    double gravity,
    size_t numIters,
    double dt,
    double overRelaxation,
    size_t sx,
    size_t sy
)

create a new fluid simulation

Parameters:

  • density density of the fluid
  • gravity gravity applied (Y axe)
  • numIters predefined number of iterations for each frames
  • dt preconfigured delta time
  • overRelaxation over predefined over relaxation value
  • sx number of cells on x axis
  • sy number of cells on y axis

Return: a newly allocated N_FLUID structure

return a newly allocated fluid

create a new fluid simulation

function n_fluid_solveIncompressibility

int n_fluid_solveIncompressibility(
    N_FLUID * fluid
)

solve incompressibility constraint

Parameters:

Return: TRUE or FALSE

non threaded version of incompressibility solving function

solve incompressibility constraint

function n_fluid_simulate_threaded

int n_fluid_simulate_threaded(
    N_FLUID * fluid,
    THREAD_POOL * thread_pool
)

run one simulation step using thread pool

Parameters:

Return: TRUE or FALSE

a threaded version of N_FLUID global processing function

run one simulation step using thread pool

function n_fluid_simulate

int n_fluid_simulate(
    N_FLUID * fluid
)

run one simulation step

Parameters:

Return: TRUE or FALSE

non threaded version of N_FLUID global processing function

run one simulation step

function n_fluid_setObstacle

int n_fluid_setObstacle(
    N_FLUID * fluid,
    double x,
    double y,
    double vx,
    double vy,
    double r
)

set an obstacle in the fluid

Parameters:

  • fluid targeted N_FLUID
  • x X position in fluid grid
  • y y position in fluid grid
  • vx X velocity for the point
  • vy Y velocity for the point
  • r radius

Return: TRUE or FALSE

set an obstacle in the fluid grid

set an obstacle in the fluid

function n_fluid_sampleField

double n_fluid_sampleField(
    N_FLUID * fluid,
    double x,
    double y,
    uint32_t field
)

sample a field value at given coordinates

Parameters:

  • fluid target N_FLUID
  • x X position in the fluid grid
  • y Y position in the fluid grid
  • field type of field wanted, one of N_FLUID_U_FIELD, N_FLUID_V_FIELD, N_FLUID_S_FIELD

Return: the computed value

compute a sample value at a field position

sample a field value at given coordinates

function n_fluid_resetObstacles

int n_fluid_resetObstacles(
    N_FLUID * fluid
)

reset all obstacles

Parameters:

Return: TRUE or FALSE

reset the obstacles set in a N_FLUID

reset all obstacles

function n_fluid_integrate

int n_fluid_integrate(
    N_FLUID * fluid
)

integrate fluid velocities

Parameters:

  • fluid a N_FLUID to integrate_chunk_list

Return: TRUE or FALSE

non threaded version of integration function

integrate fluid velocities

function n_fluid_getSciColor

ALLEGRO_COLOR n_fluid_getSciColor(
    const N_FLUID * fluid,
    double val,
    double minVal,
    double maxVal
)

get a scientific color for a value

Parameters:

  • fluid targeted N_FLUID
  • val value mixer
  • minVal minimum output value
  • maxVal maximum output value

Return: the generated ALLEGRO_COLOR

get funky colors for the fluid

get a scientific color for a value

function n_fluid_extrapolate

int n_fluid_extrapolate(
    N_FLUID * fluid
)

extrapolate fluid boundaries

Parameters:

Return: TRUE or FALSE

non threaded extrapolation function

extrapolate fluid boundaries

function n_fluid_draw

int n_fluid_draw(
    N_FLUID * fluid
)

draw the fluid simulation

Parameters:

Return: TRUE or FALSE

draw a N_FLUID on screen / targert bitmap

draw the fluid simulation

function n_fluid_avgV

double n_fluid_avgV(
    const N_FLUID * fluid,
    size_t i,
    size_t j
)

compute average V velocity at cell

Parameters:

  • fluid targeted N_FLUID
  • i X position in the fluid grid
  • j Y position in the fluid grid

Return: the average computed value

compute the average V value at a fluid position using it's surrounding

compute average V velocity at cell

function n_fluid_avgU

double n_fluid_avgU(
    const N_FLUID * fluid,
    size_t i,
    size_t j
)

compute average U velocity at cell

Parameters:

  • fluid targeted N_FLUID
  • i X position in the fluid grid
  • j Y position in the fluid grid

Return: the average computed value

compute the average U value at a fluid position using it's surrounding

compute average U velocity at cell

function n_fluid_advectVel

int n_fluid_advectVel(
    N_FLUID * fluid
)

advect velocity field

Parameters:

Return: TRUE or FALSE

non threaded version of add velocities function

advect velocity field

function n_fluid_advectSmoke

int n_fluid_advectSmoke(
    N_FLUID * fluid
)

advect smoke density field

Parameters:

Return: TRUE or FALSE

non threaded version of add smoke function

advect smoke density field

function destroy_n_fluid

int destroy_n_fluid(
    N_FLUID ** fluid
)

destroy a fluid simulation

Parameters:

  • fluid pointer to the structure to destroy

Return: TRUE or FALSE

destroy a fluid structure

destroy a fluid simulation

Macros Documentation

define _zd

#define _zd(
    __fluid,
    __value
)
((__value) > (__fluid->negative_float_tolerance) && (__value) < (__fluid->positive_float_tolerance))

test if value is near zero, according to fluid's precision

define _z

#define _z(
    __fluid,
    __component
)
((__fluid->__component) > (__fluid->negative_float_tolerance) && (__fluid->__component) < (__fluid->positive_float_tolerance))

test if component is near zero, according to fluid's precision