PyFluent provides an event-driven mechanism to monitor Fluent activities such as solver iterations, case loading, or meshing events. Each session object has an events attribute of type EventsManager. You can use the events.register_callback() method to attach a Python function that executes whenever a specific event occurs.
Callback Signature:
The callback function must follow the signature: cb(session, event_info, <additional arguments>).
session: The current session instance.event_info: An instance containing metadata about the event (e.g., iteration index, file names).<additional arguments>: Optional positional or keyword arguments passed during registration.
Supported Events:
Events are categorized into two main classes:
SolverEvent: For solver-related activities (e.g., ITERATION_ENDED, CASE_LOADED, SOLUTION_INITIALIZED).MeshingEvent: For meshing-related activities.
Best Practices:
- Keep callbacks lightweight: Long-running or CPU-heavy logic in a callback can block event processing and interfere with gRPC communication with the Fluent server.
- Thread Safety: Event callbacks may run on a worker thread. If you are performing UI updates (e.g., refreshing PyVista or Matplotlib windows), you must schedule that work onto your application's active event loop thread using a thread-safe mechanism like
asyncio.call_soon_threadsafe.
from ansys.fluent.core import SolverEvent, IterationEndedEventInfo
# Define the callback
def on_iteration_ended(session, event_info: IterationEndedEventInfo):
print("Iteration ended. Index = ", event_info.index)
# Register the callback
callback_id = solver_session.events.register_callback(SolverEvent.ITERATION_ENDED, on_iteration_ended)