# 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("当前网格未激活。")

bc = s.setup.boundary_conditions
print("CURRENT_WALL_ZONES", bc.wall.get_object_names())
print("CURRENT_ITERATIONS", s.solution.run_calculation.iter_count.get_state())
print("CURRENT_MODELS", s.setup.models.viscous.get_state())

s.file.write_case(
    file_name=task_directory + r"\before_front_wing_wall_separation.cas.h5"
)

modify = s.mesh.modify_zones
wall_names = bc.wall.get_object_names()
if wall_names == ["walls"]:
    print(
        "REGION_SEPARATION",
        modify.sep_face_zone_region(
            face_zone_name="walls",
            move_faces=True,
        ),
    )
    region_names = bc.wall.get_object_names()
    print("CONNECTED_WALL_REGIONS", region_names)
    for name in region_names:
        print(
            "ANGLE_SEPARATION",
            name,
            modify.sep_face_zone_angle(
                face_zone_name=name,
                angle=45.0,
                move_faces=True,
            ),
        )
else:
    print("EXISTING_PARTITIONS_PRESERVED", wall_names)

print("SEPARATED_WALL_ZONES", bc.wall.get_object_names())
s.mesh.check()
s.file.write_case(
    file_name=task_directory + r"\front_wing_separated_walls.cas.h5"
)

s.setup.general.solver.type = "pressure-based"
s.setup.models.viscous.model = "k-omega"
s.setup.models.viscous.k_omega_model = "sst"
s.setup.models.energy.enabled = False
s.setup.general.operating_conditions.gravity.enable = False

materials = s.setup.materials
if "air" not in materials.fluid.get_object_names():
    materials.database.copy_by_name(type="fluid", name="air")
air = materials.fluid["air"]
for name, value in [
    ("density", 1.225),
    ("viscosity", 1.7894e-5),
]:
    getattr(air, name).set_state({"option": "constant", "value": value})

fluid_zones = s.setup.cell_zone_conditions.fluid
for name in fluid_zones.get_object_names():
    fluid_zones[name].general.material = "air"

inlet = bc.velocity_inlet["inlet"]
inlet.momentum.velocity.set_state({"option": "value", "value": 30.0})
outlet = bc.pressure_outlet["outlet"]
outlet.momentum.gauge_pressure.set_state({"option": "value", "value": 0.0})
print("INLET_TURBULENCE", inlet.turbulence.get_state())
print("INLET_TURBULENCE_FIELDS", getattr(inlet.turbulence, "child_names", []))
print("OUTLET_TURBULENCE", outlet.turbulence.get_state())

surface_reports = s.solution.report_definitions.surface
report_name = "front_wing_coordinate_interface"
if report_name not in surface_reports.get_object_names():
    surface_reports[report_name] = {}
report = surface_reports[report_name]
print("COORDINATE_REPORT_STATE", report.get_state())
print("COORDINATE_REPORT_FIELDS", getattr(report, "child_names", []))
for name in getattr(report, "child_names", []):
    child = getattr(report, name)
    if child.is_active() and hasattr(child, "allowed_values"):
        if name in ["report_type", "surface_names", "zones"]:
            print("COORDINATE_REPORT_OPTIONS", name, child.allowed_values())

wall_names = bc.wall.get_object_names()
if wall_names:
    momentum = bc.wall[wall_names[0]].momentum
    print("WALL_MOTION_OPTIONS", momentum.wall_motion.allowed_values())
    print("WALL_MOMENTUM_FIELDS", getattr(momentum, "child_names", []))

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