Skip to content

推进与记录 ​

编译后的模型提供两个求值函数:discrete_fn 推进一步,continuous_fn 只求当前点的导数。这一章给出两者的调用约定、输出分组与落盘回读方式。

最小可运行 ​

python
y = None
for step in range(100):
    x, xd, y, effects = model.discrete_fn(step * 0.01, x, xd, u, p)

discrete_fn(t, x, xd, u, p) 返回新的连续状态、离散状态、本步输出与 effects,不修改传入的数组,因此每步都可以自行决定是否保留历史。完整脚本见 models/fr_f16/examples/simulate.py。

只关心导数或做一次快速评估时用 continuous_fn:

python
xdot, _, y, _ = model.continuous_fn(0.0, x, model.xd.build(), u, model.p.default())

F-16 模型没有离散状态(model.n_xd 为 0),model.xd.build() 给的是空向量,但签名要求照传。

输出分组 ​

无地面时共 35 条路径、133 个数值:

分组路径
姿态与运动Root::pos_ned、vel_body、vel_ned、quat、omega_body、euler
气动与大气Root::coefficients、mach、qbar_pa、ps_pa、tas、alpha、alpha_deg、beta、beta_deg、altitude
力与力矩Root::force_aero、thrust_force、force_gravity、force_total、moment_aero、moment_total、specific_force_body
质量与惯量Root::mass、inertia
风Root::wind_uvw、wind_omega、wind_steady、wind_gust
执行机构与前缘襟翼Root::control_actual、control_actual_deg、d_lef、d_lef_deg
观测Root::clean_y、y

F16Config.default(enable_ground=True) 额外导出 Root::ground_force_body、ground_moment_body、ground_penetrations、ground_normal_forces、ground_longitudinal_forces、ground_lateral_forces、ground_contact_count 七条路径。

按路径取值:

python
final = model.y.read(y, by="path")
print(final["Root::mach"], final["Root::qbar_pa"])

记录与回读 ​

python
from pathlib import Path

from fly_ruler_std.runtime.record import Loader, Recorder

output_dir = Path("results") / "f16_nominal"
recorder = Recorder(model, output_dir)
for step in range(100):
    x, xd, y, effects = model.discrete_fn(step * 0.01, x, xd, u, p)
    recorder.record(
        step,
        (step + 1) * 0.01,
        x=x,
        xd=xd,
        y=y,
        u=u,
        p=p,
        effect=effects,
    )
recorder._save("nominal")

loader = Loader(output_dir, filename="nominal", model=model)
pos_ned = loader.get_y("Root::pos_ned")

Recorder 按编译后的布局解释数组并落盘成 Parquet 与元数据,列名是「类别:完整路径[分量]」;Loader 与 model.y.read 都按完整路径取值。批量回读与时间区间筛选见记录与回读。

示例脚本 ​

命令在 models/fr_f16/ 下执行:

脚本内容命令
models/fr_f16/examples/inspect_model.py打印产物路径与 x、u、y 的完整路径清单just example-inspect
models/fr_f16/examples/simulate.py100 步标称仿真,记录后回读姿态、气动力矩与马赫数、动压just example-simulate
models/fr_f16/examples/disturbances.py同一初值下比较标称、稳态风、Dryden、传感器噪声与不确定度just example-disturbances
models/fr_f16/examples/trim_linearize.pyCasADi 上配平并求 A、B、C、Djust example-trim
models/fr_f16/examples/backend_compare.pyNumPy、CasADi 与可选 JAX 的编译耗时与偏差just example-backends
models/fr_f16/examples/ground_operations.py起飞滑跑或接地,支持四个后端just example-ground --scenario takeoff
models/fr_f16/examples/fly_stick_f16.py手柄实时驾驶并推送 Proto 状态just example-fly-stick

边界与常见坑 ​

情况说明
discrete_fn 与 continuous_fn 的返回前者四位分别是新状态、新离散状态、输出与 effects;后者第一位是导数,不是新状态
时间参数第一个参数是当前时间,记录时的第二个参数才是步末时间,两者差一个 dt
记录前转换类型Recorder 接受 DM、JAX 数组和 Tensor,在记录边界转换为主机 NumPy;求导时保留原生 Tensor
回读用同一个模型Loader 需要编译后的 model 才能把列名映射回路径
忘记传 xd即使模型没有离散状态,也要把 model.xd.build() 的结果按签名传入

这一章用到的 API ​

相关页面 ​