Explanation of the Flow:
Initialization: The simulation starts by setting up the environment, loading necessary code modules, configurations (mission, aircraft aerodynamics), finding a network port, synchronizing settings with the JavaScript front-end, launching the browser, and finally starting the WebSocket server to listen for client connections.
WebSocket Server Loop: Once the server is running, it waits for a client (the browser front-end) to connect. Upon connection, it resets the simulation time and prepares for data recording. It then enters a loop, waiting for messages from the client.
Update Aircraft State (Per Message): When a message arrives (containing the client's current understanding of the aircraft state, user control inputs, and the time elapsed since the last update, deltaTime), the server processes it:
It parses the incoming data.
It updates the internal simulation time (sim_time).
It calls the core Runge_Kutta_4_integrator function to calculate the aircraft's state after deltaTime.
It checks if the current sim_time falls within the user-defined recording window. If so, it appends the calculated state and telemetry data to a DataFrame in memory.
If the simulation time has passed the recording end time and the data hasn't been saved yet, it writes the collected DataFrame to a CSV file.
It adds the current sim_time (as server_time) to the results dictionary.
Finally, it sends this updated state dictionary back to the client.
Runge-Kutta 4 Integrator: This is the numerical integration heart.
It first calculates the actual control surface deflections and thrust achievable in deltaTime, considering actuator speeds and engine spool times.
It calculates the initial aerodynamic conditions (like Angle of Attack, Sideslip, Mach number) based on the current state vector.
It then performs the four stages (k1, k2, k3, k4) of the RK4 method. Each stage involves calling the compute_6DOF_equations_of_motion function with slightly different input states.
It combines the results of the four stages to calculate the final state vector after deltaTime.
It averages the forces and detailed telemetry calculated during the RK4 stages.
It performs a simple ground collision check.
It packages all the results (new state, forces, telemetry) into a dictionary to be returned.
6DOF Equations of Motion: This function calculates the instantaneous forces and moments acting on the aircraft and determines the resulting linear and angular accelerations.
It calculates propulsive forces.
It calculates aerodynamic force coefficients (Lift, Drag, Sideforce) based on the current flight conditions and control inputs, likely using lookups or interpolations from the loaded aero data.
It converts these forces into the body frame.
It sums forces (propulsion, aerodynamics, weight) to find the net force and thus linear acceleration.
It calculates aerodynamic moment coefficients (rolling, pitching, yawing) due to forces, control deflections, inherent stability (stiffness), and rotation rates (damping).
It sums these moments.
Using the aircraft's inertia tensor, it calculates the angular accelerations.
It returns the calculated derivatives (accelerations) of the state vector, along with detailed force and telemetry data.
Calculate Flight Conditions: This helper function takes the current state vector and computes essential aerodynamic parameters needed for the 6DOF calculations, such as True Airspeed (TAS), Angle of Attack (alpha), Sideslip angle (beta), Mach number, dynamic pressure, and atmospheric properties at the current altitude.
This cycle repeats for every message received from the client, advancing the simulation step by step.