SmokeSystem
FluxRender.entities.SmokeSystem
SmokeSystem(vec_function, dissipation_factor: float = 0.9995, grid_downscale: int = 4, solid_color: Sequence[float] = (1.0, 1.0, 1.0, 1.0), color_mapper: ColorMapper = None, color_property: Property = Property.VELOCITY, color_clipping_percentiles: Sequence[float] = (5.0, 95.0), emission_edge: Sequence[EmissionEdge] = (EmissionEdge.LEFT, EmissionEdge.RIGHT, EmissionEdge.TOP, EmissionEdge.BOTTOM), smoke_pattern: SmokePattern = SmokePattern.SMOOTH, base_angle_vector=None, custom_color_function=None)
Bases: VectorEntity
A visual entity that simulates and renders fluid advection using a grid-based smoke system.
The system uses a decoupled architecture to maintain performance: physical velocity field evaluation is performed on a downscaled grid via CPU (NumPy), while semi-Lagrangian advection, emission, and bilinear interpolation are executed at full screen resolution on the GPU (Taichi).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec_function
|
Callable or VectorMathEngine
|
The mathematical function driving the fluid flow, returning vector components (dx, dy). |
required |
dissipation_factor
|
float
|
The rate at which smoke density fades per frame. Values closer to 1.0 make the smoke last longer. (Default: 0.9995) |
0.9995
|
grid_downscale
|
int
|
The factor by which the physical evaluation grid is reduced relative to the screen resolution to save CPU cycles. The closer the value is to 1, the more accurate the behavior and coloring of the smoke. (Default: 4) |
4
|
solid_color
|
Sequence[float]
|
A static RGBA color applied to the emitted smoke. If None, the system defaults to dynamic pattern colors. (Default: (1.0, 1.0, 1.0, 1.0)) |
(1.0, 1.0, 1.0, 1.0)
|
color_mapper
|
ColorMapper
|
Maps specific field properties (e.g., velocity, curl) to colors. Overrides solid_color and dynamic patterns if provided. |
None
|
color_property
|
Property
|
The physical property used by the color_mapper. Only relevant if color_mapper is not None. (Default: Property.VELOCITY) |
VELOCITY
|
color_clipping_percentiles
|
Sequence[float]
|
Lower and upper percentiles used to clip extreme values before applying the color_mapper. (Default: (5.0, 95.0)) |
(5.0, 95.0)
|
emission_edge
|
Sequence[EmissionEdge]
|
The screen boundaries from which smoke is continuously emitted. |
(LEFT, RIGHT, TOP, BOTTOM)
|
smoke_pattern
|
SmokePattern
|
The mathematical structural pattern applied to the smoke density at the emission edges. (Default: SmokePattern.SMOOTH) |
SMOOTH
|
base_angle_vector
|
tuple, list, or Callable
|
The reference vector used for calculating angles when color_property is set to ANGLE. |
None
|
custom_color_function
|
Callable
|
A user-defined mathematical function replacing standard property evaluation when color_property is set to CUSTOM. |
None
|
Example
import FluxRender as fr
scene = fr.create_workspace()
# 1. Configure boundary conditions
inflow_boundary = fr.BoundaryConfiguration(fr.BoundaryType.INFLOW, inflow_velocity_x=0.05)
outflow_boundary = fr.BoundaryConfiguration(fr.BoundaryType.OPEN_OUTFLOW)
# 2. Initialize the fluid sandbox
with fr.FluidSandbox(
domain_x_range=(-6, 6),
domain_y_range=(-6, 6),
left_boundary=inflow_boundary,
right_boundary=outflow_boundary,
) as sandbox:
# 3. Define a solid obstacle
fr.EquationCollider(equation_function=lambda x, y: (abs(x) ** (2/3) + abs(y) ** (2/3)) <= 1)
# 4. Create Smoke
fr.SmokeSystem(sandbox, solid_color=(0, 1, 1, 1)) # You can set solid_color to None if you want colored smoke
scene.run()
Smoke colored by Property
Source code in FluxRender/entities.py
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 | |
evaluate_angle_vector
Evaluates the base reference angle vector at the specified spatial coordinates.
This method determines whether the reference angle vector is a static coordinate pair or a dynamically evaluated mathematical function. If it is a callable function, it safely executes it, automatically injecting the current time if the function signature requires it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float or ndarray
|
The x-coordinate(s) in the mathematical world space. |
required |
y
|
float or ndarray
|
The y-coordinate(s) in the mathematical world space. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple
|
A tuple (component_x, component_y) representing the evaluated reference vector components. |
Notes
- Broadcasting: This method fully supports NumPy broadcasting. You can pass single
float values for pinpoint evaluation, or large multidimensional arrays (like those
generated by
numpy.meshgrid) to evaluate the entire mathematical space simultaneously.
Example
Evaluating a dynamically rotating reference angle at the origin:
import FluxRender as fr
import numpy as np
# [Initializing the scene and coordinate system]
def rotating_reference(x, y, t):
direction_x = np.cos(t)
direction_y = np.sin(t)
return direction_x, direction_y
field = fr.VectorField(
vec_function = lambda x, y: (y, -x),
color_property = fr.Property.ANGLE,
base_angle_vector = rotating_reference
)
# The engine automatically handles the underlying time injection
angle_vector_x, angle_vector_y = field.evaluate_angle_vector(0.0, 0.0)
print(f"Reference angle vector at the origin: ({angle_vector_x}, {angle_vector_y})")
Source code in FluxRender/entities.py
evaluate_field_and_property
evaluate_field_and_property(property_type: Property | None, spatial_coordinate_x: float, spatial_coordinate_y: float)
Evaluates the primary vector function and the specified property at the given spatial coordinates.
This method behaves exactly like evaluate_vector_field, but additionally calculates a scalar value given by property_type (e.g. divergence, rotation, velocity).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
property_type
|
Property | None
|
The specific property to calculate based on the evaluated vector field. If set to None, the method will only evaluate the primary vector function and bypass any property calculations for maximum performance when only vector components are needed. |
required |
spatial_coordinate_x
|
float / ndarray
|
The x-coordinate(s) in the mathematical world space. |
required |
spatial_coordinate_y
|
float / ndarray
|
The y-coordinate(s) in the mathematical world space. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
A tuple (vector_x, vector_y, property_value) where: - vector_x (float / ndarray): The x-component(s) of the evaluated vector field. - vector_y (float / ndarray): The y-component(s) of the evaluated vector field. - property_value (float / ndarray or None): The calculated property value based on the specified property_type. This will be None if property_type is set to None, indicating that no property calculation was performed. |
Notes
- Performance Optimization This method is optimized for performance. If the caller only requires the vector components without any derived properties, they can set property_type to None to skip the property evaluation step entirely, which can significantly reduce computation time, especially for complex properties that require additional function evaluations.
- Time Injection If the primary vector function or the property evaluator function is time-dependent, this method will automatically inject the current simulation time during their evaluation, allowing for dynamic, time-evolving fields without requiring the user to manage time parameters manually.
Source code in FluxRender/entities.py
evaluate_scalar_function
Evaluates a user-provided mathematical scalar function, automatically handling time injection and vectorization.
This method serves as a robust adapter for custom user logic that maps spatial coordinates (and potentially
vector components) to a single scalar value. It analyzes the signature of the provided function to dynamically
inject the simulation time if required. It attempts to execute the function using native NumPy vectorization
for maximum performance, automatically falling back to numpy.vectorize if strictly scalar Python operations
are detected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_defined_function
|
Callable
|
The custom scalar function or lambda to evaluate. |
required |
*spatial_arguments
|
The base spatial and/or vector arrays to pass into the function (e.g., evaluated_vector_x, evaluated_vector_y, world_x, world_y). |
()
|
Returns:
| Type | Description |
|---|---|
ndarray
|
numpy.ndarray: A single NumPy array containing the computed scalar values, properly sanitized and ready for rendering or further mathematical processing. |
Notes
- Time Injection: If
user_defined_functionaccepts exactly one parameter more than the number of provided*spatial_arguments, the current scene time is automatically injected during execution. - Vectorization Fallback: You do not need to write strictly vectorized NumPy code. Regular Python scalar operations will be caught and vectorized automatically, though writing native NumPy code is highly recommended for optimal rendering performance.
- Scalar Output: Unlike its vector counterpart, this method strictly expects the user's function to return a single value (or a single array) per spatial coordinate, not a tuple.
Example
Calculating and printing a custom physical metric (like kinetic energy) at specific points, while the field continues to render its default colors visually:
import FluxRender as fr
import numpy as np
# [Initializing the scene and coordinate system]
field = fr.VectorField(
vec_function = lambda x, y: (y, -x),
)
# Custom function that internally queries the field for vector data and calculates a scalar property (e.g., kinetic energy = 0.5 * (vx^2 + vy^2))
def calculate_kinetic_energy(x, y):
vector_dx, vector_dy = field.evaluate_vector_field(x, y)
kinetic_energy = 0.5 * (vector_dx**2 + vector_dy**2)
return kinetic_energy
# Define the exact spatial points we want to analyze
target_coordinates_x = np.array([0.0, 1.0, 2.0])
target_coordinates_y = np.array([0.0, 1.0, 2.0])
# Evaluate the custom metric across all points simultaneously
energy_results = field.evaluate_scalar_function(
calculate_kinetic_energy,
target_coordinates_x,
target_coordinates_y
)
print(f"Kinetic energy at points (0,0), (1,1) and (2,2): {energy_results}")
Source code in FluxRender/entities.py
evaluate_vector_field
Evaluates the primary vector field function at the specified spatial coordinates.
This method acts as a safe execution wrapper for the user-defined vector function. It delegates the execution to the internal evaluation handler, which manages potential numpy broadcasting issues, scalar fallbacks, and automatic time-parameter injection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float or ndarray
|
The x-coordinate(s) in the mathematical world space. |
required |
y
|
float or ndarray
|
The y-coordinate(s) in the mathematical world space. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple
|
A tuple (vector_x, vector_y) representing the evaluated vector field components. |
Notes
- Broadcasting: This method fully supports NumPy broadcasting. You can pass single
float values for pinpoint evaluation, or large multidimensional arrays (like those
generated by
numpy.meshgrid) to evaluate the entire mathematical space simultaneously.
Example
Evaluating the field at a single focal point:
import FluxRender as fr
# [Initializing the scene and coordinate system]
field = fr.VectorField(
vec_function = lambda x, y: (y, -x),
color_property = fr.Property.VELOCITY
)
vector_component_x, vector_component_y = field.evaluate_vector_field(1.0, 0.0)
print(f"Vector field at (1.0, 0.0): ({vector_component_x}, {vector_component_y})")
# Result: (0.0, -1.0)
Evaluating the field at multiple points simultaneously using numpy arrays:
import numpy as np
# Define the exact spatial points we want to analyze
target_coordinates_x = np.array([0.0, 1.0, 2.0])
target_coordinates_y = np.array([0.0, 1.0, 2.0])
x_vectors, y_vectors = vec_field.evaluate_vector_field(
target_coordinates_x,
target_coordinates_y
)
print(f"Vector field at points (0,0), (1,1) and (2,2): [{x_vectors[0]}, {y_vectors[0]}] | [{x_vectors[1]}, {y_vectors[1]}] | [{x_vectors[2]}, {y_vectors[2]}]")
Source code in FluxRender/entities.py
evaluate_vector_function
Evaluates a user-provided mathematical function, automatically handling time injection and vectorization.
This method serves as a robust adapter for custom user logic. It analyzes the signature of the
provided function to dynamically inject the simulation time if required. Furthermore, it attempts
to execute the function using native NumPy vectorization for maximum performance. If the function
is strictly scalar (e.g., uses standard Python math modules instead of numpy), it automatically
falls back to numpy.vectorize to ensure compatibility across large coordinate grids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_defined_function
|
Callable
|
The custom function or lambda to evaluate. |
required |
*spatial_arguments
|
The base spatial arrays to pass into the function (typically world_x, world_y). |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple
|
A tuple (evaluated_vector_x, evaluated_vector_y) containing the computed field components as sanitized NumPy arrays. |
Notes
- Time Injection: If
user_defined_functionaccepts exactly one parameter more than the number of provided*spatial_arguments(e.g., taking x, y, and t), the current scene time is automatically injected during execution. - Vectorization Fallback: You do not need to write strictly vectorized NumPy code. Regular Python scalar operations will be caught and vectorized automatically, though writing native NumPy code is highly recommended for optimal rendering performance.
Examples:
Evaluating a custom mathematical perturbation with automatic time injection:
import FluxRender as fr
import numpy as np
# [Initializing the scene and coordinate system]
field = fr.VectorField(
vec_function = lambda x, y: (y, -x),
)
# Notice the third parameter 't'. The engine detects this and injects it.
def custom_wind_perturbation(x, y, t):
perturbation_x = np.sin(x + t)
perturbation_y = np.cos(y + t)
return perturbation_x, perturbation_y
spatial_coordinates_x = np.array([0.0, 1.0, 2.0])
spatial_coordinates_y = np.array([0.0, 1.0, 2.0])
result_vectors_x, result_vectors_y = field.evaluate_vector_function(
custom_wind_perturbation,
spatial_coordinates_x,
spatial_coordinates_y
)