
import flow360 as fl
from flow360.examples import Cube

Cube.get_files()

project = fl.Project.from_geometry(Cube.geometry, name="SRF with Cube", length_unit="m")

geo = project.geometry
geo.show_available_groupings(verbose_mode=True)
geo.group_bodies_by_tag("groupByFile")
geo.group_faces_by_tag("faceId")

with fl.SI_unit_system:
    fluid = fl.SeedpointVolume(
        name="fluid", point_in_mesh=[(10.1435, 0, 0)], center=(0, 0, 0), axis=(1, 0, 0)
    )
    fluid_zone = fl.CustomZones(name="fluid", entities=[fluid])

    meshing_params = fl.ModularMeshingWorkflow(
        surface_meshing=fl.snappy.SurfaceMeshingParams(
            defaults=fl.snappy.SurfaceMeshingDefaults(
                min_spacing=0.5 * fl.u.m, max_spacing=2 * fl.u.m, gap_resolution=1 * fl.u.mm
            ),
            refinements=[
                fl.snappy.RegionRefinement(
                    min_spacing=50 * fl.u.mm, max_spacing=100 * fl.u.mm, regions=[geo["cube"]]
                ),
                fl.snappy.SurfaceEdgeRefinement(
                    spacing=[50 * fl.u.mm], distances=[2 * fl.u.mm], entities=[geo["cube"]]
                ),
            ],
        ),
        volume_meshing=fl.VolumeMeshingParams(
            defaults=fl.VolumeMeshingDefaults(boundary_layer_first_layer_thickness=1 * fl.u.mm),
            refinements=[
                fl.PassiveSpacing(faces=[geo["farfield"]], type="unchanged"),
                fl.UniformRefinement(
                    entities=fl.Box(name="box", center=(0.5, 0, 0), size=(3, 2, 2)),
                    spacing=50 * fl.u.mm,
                ),
            ],
        ),
        zones=[fluid_zone],
    )

with fl.SI_unit_system:
    rotating_farfield = fl.Rotation(
        name="rotating_fluid", volumes=[fluid], spec=fl.AngularVelocity(50 * fl.u.rpm)
    )

with fl.SI_unit_system:
    params = fl.SimulationParams(
        meshing=meshing_params,  # Previously defined meshing parameters
        operating_condition=fl.AerospaceCondition(velocity_magnitude=10 * fl.u.m / fl.u.s),
        time_stepping=fl.Steady(),
        models=[
            fl.Wall(surfaces=[geo["cube"]]),
            fl.Freestream(surfaces=[geo["farfield"]]),
            rotating_farfield,
        ],
        reference_geometry=fl.ReferenceGeometry(),
        outputs=[
            fl.SurfaceOutput(surfaces=[geo["cube"]], output_fields=["CfVec", "Cf", "yPlus", "Cp"])
        ],
    )

case = project.run_case(params=params, name="Steady SRF", use_beta_mesher=True)
