Math Engine
FluxRender.math_engine
VectorMathEngine
VectorMathEngine(scene: Scene, primary_vector_function: callable, base_angle_vector=[1.0, 0.0], custom_function: callable = None)
Bases: MathEngine
The core mathematical processor for evaluating 2D vector fields.
This engine acts as the computational "brain" for spatial entities such as VectorField or ParticleSystem. It parses user-defined mathematical functions, evaluates them across the simulation space, and calculates derived physical properties (e.g., velocity magnitude, directional angle, or divergence) to prepare the raw data for GPU execution.
Initializes the VectorMathEngine with necessary functions and spatial references.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scene
|
Scene
|
The main simulation scene. Provides essential global context for the mathematical engine, such as the current simulation time or coordinate system boundaries required during evaluation. |
required |
primary_vector_function
|
callable
|
The main mathematical function defining the
vector field. It must accept spatial coordinates (and optionally time, e.g.,
|
required |
base_angle_vector
|
Sequence or callable
|
Defines the reference baseline for calculating directional angles (used with Property.ANGLE). It can be a static 2D sequence (e.g., [1.0, 0.0]) for a global zero-degree reference. Alternatively, it can be a callable function evaluating to a 2D vector (with or without a time parameter), establishing a dynamic, spatially varying reference frame. Defaults to [1.0, 0.0]. |
[1.0, 0.0]
|
custom_function
|
callable
|
A user-defined mapping function evaluated
across the field when the color property is set to Property.CUSTOM. The callable
must accept exactly four or five arguments representing the local vector components,
the spatial coordinates, and optionally the time parameter (e.g.,
|
None
|
Source code in FluxRender/math_engine.py
calculate_angle
Calculates the directional angle of the vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world_x
|
float / ndarray
|
X coordinates in world space. |
required |
world_y
|
float / ndarray
|
Y coordinates in world space. |
required |
vec_dx
|
float / ndarray
|
Evaluated vector X components. |
required |
vec_dy
|
float / ndarray
|
Evaluated vector Y components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
angle |
ndarray
|
The scalar angle field in radians. |
Source code in FluxRender/math_engine.py
calculate_convective_acceleration
Calculates the convective acceleration term of the fluid flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world_x
|
float / ndarray
|
X coordinates in world space. |
required |
world_y
|
float / ndarray
|
Y coordinates in world space. |
required |
vec_dx
|
float / ndarray
|
Evaluated vector X components. |
required |
vec_dy
|
float / ndarray
|
Evaluated vector Y components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
convective_acceleration |
ndarray
|
The scalar convective acceleration field, representing the acceleration experienced by a fluid particle due to the spatial variation of the velocity field. |
Source code in FluxRender/math_engine.py
calculate_curl
Calculates the 2D curl.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world_x
|
float / ndarray
|
X coordinates in world space. |
required |
world_y
|
float / ndarray
|
Y coordinates in world space. |
required |
vec_dx
|
float / ndarray
|
Evaluated vector X components. |
required |
vec_dy
|
float / ndarray
|
Evaluated vector Y components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
curl |
ndarray
|
The scalar curl field representing local rotation. |
Source code in FluxRender/math_engine.py
calculate_custom
Evaluates a user-defined custom property function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world_x
|
float / ndarray
|
X coordinates in world space. |
required |
world_y
|
float / ndarray
|
Y coordinates in world space. |
required |
vec_dx
|
float / ndarray
|
Evaluated vector X components. |
required |
vec_dy
|
float / ndarray
|
Evaluated vector Y components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
custom_property |
ndarray
|
The results computed by the assigned custom function. |
Source code in FluxRender/math_engine.py
calculate_divergence
Calculates the 2D divergence field (dv_x/dx + dv_y/dy).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world_x
|
float / ndarray
|
X coordinates in world space. |
required |
world_y
|
float / ndarray
|
Y coordinates in world space. |
required |
vec_dx
|
float / ndarray
|
Evaluated vector X components. |
required |
vec_dy
|
float / ndarray
|
Evaluated vector Y components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
divergence |
ndarray
|
The scalar divergence field representing sources and sinks. |
Source code in FluxRender/math_engine.py
calculate_jacobian
Calculates the Jacobian tensor components of the vector field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world_x
|
float / ndarray
|
X coordinates in world space. |
required |
world_y
|
float / ndarray
|
Y coordinates in world space. |
required |
vec_dx
|
float / ndarray
|
Evaluated vector X components. |
required |
vec_dy
|
float / ndarray
|
Evaluated vector Y components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
jacobian |
ndarray
|
A scalar field representing the determinant of the Jacobian matrix, which indicates local expansion or contraction of the vector field. Positive values indicate local expansion, negative values indicate local contraction, and zero indicates a critical point where the flow is neither expanding nor contracting. |
Source code in FluxRender/math_engine.py
calculate_okubo_weiss
Calculates the Okubo-Weiss criterion for topological vortex identification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world_x
|
float / ndarray
|
X coordinates in world space. |
required |
world_y
|
float / ndarray
|
Y coordinates in world space. |
required |
vec_dx
|
float / ndarray
|
Evaluated vector X components. |
required |
vec_dy
|
float / ndarray
|
Evaluated vector Y components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
okubo_weiss |
ndarray
|
The scalar Okubo-Weiss parameter field. |
Source code in FluxRender/math_engine.py
calculate_velocity
Calculates the velocity magnitude (Euclidean norm) of the vector field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world_x
|
float / ndarray
|
X coordinates in world space. (Note: It won't be used in this specific calculation, but is included in the signature for consistency and potential future use in more complex properties.) |
required |
world_y
|
float / ndarray
|
Y coordinates in world space. (Note: It won't be used in this specific calculation, but is included in the signature for consistency and potential future use in more complex properties.) |
required |
vec_dx
|
float / ndarray
|
Evaluated vector X components. |
required |
vec_dy
|
float / ndarray
|
Evaluated vector Y components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
velocity |
ndarray
|
The scalar velocity magnitude field. |
Source code in FluxRender/math_engine.py
clone
Creates a deep copy of the current MathEngine instance, allowing selective overrides of specific attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primary_vector_function
|
callable
|
If provided, this function will replace the primary vector function in the cloned instance. Must follow the same signature requirements as the original. |
None
|
base_angle_vector
|
Sequence or callable
|
If provided, this will replace the base angle vector in the cloned instance. Can be a static 2D sequence or a callable function, following the same validation rules as the original. |
None
|
custom_function
|
callable
|
If provided, this function will replace the custom function in the cloned instance. Must follow the same signature requirements as the original. |
None
|
Source code in FluxRender/math_engine.py
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 |
|---|---|---|---|
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 |
|---|---|---|
vector |
tuple
|
A tuple (component_x, component_y) representing the evaluated reference vector components. |
Source code in FluxRender/math_engine.py
evaluate_field_and_property
evaluate_field_and_property(property_type: Property, spatial_coordinate_x: float, spatial_coordinate_y: float) -> tuple
Evaluates the primary vector function and the specified property at the given spatial coordinates.
This method behaves exactly like evaluate_primary_vector_function, but additionally calculates a scalar value given by property_type (e.g. divergence, rotation, velocity).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
property_type
|
Property
|
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 |
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.
Example
Evaluating the vector field and its velocity property at a single point:
import FluxRender as fr
# [Initializing the scene and coordinate system]
math_engine = fr.VectorMathEngine(scene, primary_vector_function=lambda x, y: (y, -x))
vector_x, vector_y, velocity = math_engine.evaluate_field_and_property(fr.Property.VELOCITY, 1.0, 0.0)
print(f"At (1.0, 0.0) -> Vector: ({vector_x}, {vector_y}), Velocity: {velocity}")
Source code in FluxRender/math_engine.py
evaluate_primary_vector_function
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 |
|---|---|---|---|
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 |
|---|---|---|
vector |
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]
math_engine = fr.VectorMathEngine(scene, primary_vector_function=lambda x, y: (y, -x))
vector_component_x, vector_component_y = math_engine.evaluate_primary_vector_function(1.0, 0.0)
print(f"Vector field at (1.0, 0.0): ({vector_component_x}, {vector_component_y})")
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 = math_engine.evaluate_primary_vector_function(
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]}]")