# Use the existing connected PyFluent variable solver.
task_directory = r"C:\Users\13977\AppData\Local\FluentAGI\data\runs\20261018-142547-90d0a4"
s = solver.settings

if not s.setup.models.is_active():
    raise RuntimeError("当前网格未激活，停止操作以保留已有状态。")

print("SOLVER_STATE", s.setup.general.solver.get_state())

bc = s.setup.boundary_conditions
for label, container in [
    ("WALL_ZONES", bc.wall),
    ("INLET_ZONES", bc.velocity_inlet),
    ("OUTLET_ZONES", bc.pressure_outlet),
]:
    print(label, container.get_object_names())

def inspect_commands(label, obj):
    children = getattr(obj, "child_names", [])
    commands = getattr(obj, "command_names", [])
    queries = getattr(obj, "query_names", [])
    print(label + "_CHILDREN", children)
    print(label + "_COMMANDS", commands)
    print(label + "_QUERIES", queries)
    for name in commands + queries:
        if any(word in name for word in [
            "separate", "split", "zone", "list", "rename", "type", "merge"
        ]):
            item = getattr(obj, name)
            print(label + "_ARGUMENTS", name, getattr(item, "argument_names", []))

queue = [("MESH", s.mesh, 0)]
while queue:
    label, obj, depth = queue.pop(0)
    inspect_commands(label, obj)
    if depth < 3:
        for name in getattr(obj, "child_names", []):
            if any(word in name for word in [
                "zone", "modify", "separate", "split", "manage"
            ]):
                queue.append((label + "_" + name, getattr(obj, name), depth + 1))

for name in bc.wall.get_object_names():
    momentum = bc.wall[name].momentum
    print("WALL_MOMENTUM_STATE", name, momentum.get_state())
    for child_name in getattr(momentum, "child_names", []):
        if any(word in child_name for word in [
            "motion", "velocity", "speed", "direction", "shear"
        ]):
            child = getattr(momentum, child_name)
            print("WALL_FIELD_ACTIVE", name, child_name, child.is_active())
            if child.is_active():
                print("WALL_FIELD_STATE", name, child_name, child.get_state())
                print(
                    "WALL_FIELD_CHILDREN",
                    child_name,
                    getattr(child, "child_names", []),
                )
                if hasattr(child, "allowed_values"):
                    print("WALL_FIELD_OPTIONS", child_name, child.allowed_values())
    break

reports = s.solution.report_definitions
force_names = [
    "front_wing_drag_interface",
    "front_wing_lift_interface",
]
for name in force_names:
    if name not in reports.force.get_object_names():
        reports.force[name] = {}
    report = reports.force[name]
    print("FORCE_REPORT_STATE", name, report.get_state())
    print("FORCE_REPORT_CHILDREN", name, getattr(report, "child_names", []))
    for child_name in getattr(report, "child_names", []):
        child = getattr(report, child_name)
        if child.is_active() and hasattr(child, "allowed_values"):
            if any(word in child_name for word in ["zone", "type", "force"]):
                print("FORCE_REPORT_OPTIONS", child_name, child.allowed_values())

viscous = s.setup.models.viscous
print("VISCOUS_STATE", viscous.get_state())
print("VISCOUS_MODEL_OPTIONS", viscous.model.allowed_values())
print("VISCOUS_CHILDREN", getattr(viscous, "child_names", []))
print("RUN_CALCULATION_STATE", s.solution.run_calculation.get_state())

s.file.write_case(
    file_name=task_directory + r"\front_wing_boundary_force_interface.cas.h5"
)
print("STAGE", "boundary_separation_and_force_field_readback")