6 n__fluids_8h
gullradriel edited this page 2026-10-09 00:55:18 +02:00

include/nilorea/n_fluids.h

Fluid management port from "How to write an Eulerian fluid simulator with 200 lines of code", by Ten Minute Physics ( https://www.youtube.com/watch?v=iKAVRgIrUOU ). More...

Structures

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

Detailed Description

Fluid management port from "How to write an Eulerian fluid simulator with 200 lines of code", by Ten Minute Physics ( https://www.youtube.com/watch?v=iKAVRgIrUOU ).

Author: Castagnier Mickael aka Gull Ra Driel

Version: 1.0

Date: 31/12/2022

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#ifndef __N_FLUID_HEADER
#define __N_FLUID_HEADER

#ifdef __cplusplus
extern "C" {
#endif


#include "allegro5/allegro.h"
#include <allegro5/allegro_primitives.h>
#include "nilorea/n_list.h"
#include "nilorea/n_thread_pool.h"

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

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

typedef struct N_FLUID_THREAD_PARAMS {
    void* ptr;
    size_t x_start;
    size_t x_end;
    size_t y_start;
    size_t y_end;
} N_FLUID_THREAD_PARAMS;

typedef struct N_FLUID {
    size_t numX;
    size_t numY;
    size_t numZ;
    size_t numCells;
    size_t numIters;
    double density;
    double dt;
    double h;
    double gravity;
    double overRelaxation;
    bool showSmoke;
    bool showPaint;
    bool showPressure;

    double negative_float_tolerance;
    double positive_float_tolerance;

    double fluid_production_percentage;
    double cScale;

    double* u;
    double* newU;

    double* v;
    double* newV;

    double* p;
    double* s;

    double* m;
    double* newM;

    LIST* integrate_chunk_list;
    LIST* solveIncompressibility_chunk_list;
    LIST* advectVel_chunk_list;
    LIST* advectSmoke_chunk_list;

} N_FLUID;

int destroy_n_fluid(N_FLUID** fluid);
N_FLUID* new_n_fluid(double density, double gravity, size_t numIters, double dt, double overRelaxation, size_t sx, size_t sy);

int n_fluid_integrate(N_FLUID* fluid);
int n_fluid_solveIncompressibility(N_FLUID* fluid);
int n_fluid_extrapolate(N_FLUID* fluid);

double n_fluid_sampleField(N_FLUID* fluid, double x, double y, uint32_t field);
double n_fluid_avgU(const N_FLUID* fluid, size_t i, size_t j);
double n_fluid_avgV(const N_FLUID* fluid, size_t i, size_t j);

int n_fluid_advectVel(N_FLUID* fluid);
int n_fluid_advectSmoke(N_FLUID* fluid);

int n_fluid_simulate(N_FLUID* fluid);
int n_fluid_simulate_threaded(N_FLUID* fluid, THREAD_POOL* thread_pool);

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

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


#ifdef __cplusplus
}
#endif

#endif