
import flow360 as fl
from flow360.examples import Cube
Cube.get_files()

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

geo = project.geometry
geo.group_faces_for_snappy()

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.BodyRefinement(
                    min_spacing=50 * fl.u.mm, max_spacing=100 * fl.u.mm,
                    bodies=geo.snappy_bodies["cube"]
                ),
                fl.snappy.SurfaceEdgeRefinement(
                    spacing=[50* fl.u.mm],
                    distances=[2*fl.u.mm],
                    entities=geo.snappy_bodies["cube"]
                )
            ]
        ),
        volume_meshing=fl.VolumeMeshingParams(
            defaults=fl.VolumeMeshingDefaults(boundary_layer_first_layer_thickness=1 * fl.u.mm),
            refinements=[
                fl.PassiveSpacing(
                    faces=geo.snappy_bodies["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.snappy_bodies["cube"]["*"]]
            ),
            fl.Freestream(
                surfaces=[geo.snappy_bodies["farfield"]["*"]]
            ),
            rotating_farfield,
        ],
        reference_geometry=fl.ReferenceGeometry(),
        outputs=[
            fl.SurfaceOutput(
                surfaces=[geo.snappy_bodies["cube"]["*"]],
                output_fields=['CfVec','Cf','yPlus','Cp']
            )
        ]
    )

case = project.run_case(params=params, name="SRF cube from Python", use_beta_mesher=True)
