module m_case_wind_turbine !! Case for wind-turbine simulations (the `two_turbines` setup): !! uniform inflow and convective outflow in x; y and z each either periodic !! or bounded by free-slip walls (BC = 'neumann', 'neumann', Incompact3d !! ncl = 1). Incompact3d's two_turbines example has walls in y and z. !! !! The case owns a polymorphic turbine forcing model (turbine_model_t), set up !! by m_turbine_setup from the `iturbine` switch. It drives the model from its !! forcings()/postprocess() hooks. !! !! The inflow/outflow boundary treatment mirrors m_case_cylinder. use m_mpi, only: MPI_COMM_WORLD, MPI_IN_PLACE, MPI_SUM, & MPI_Allreduce use m_allocator, only: allocator_t use m_base_backend, only: base_backend_t use m_base_case, only: base_case_t use m_common, only: dp, MPI_X3D2_DP, get_argument, DIR_C, DIR_X, & VERT, X_FACE, Y_FACE, Z_FACE, BC_DIRICHLET, & BC_NEUMANN, BC_PERIODIC use m_config, only: wind_turbine_config_t use m_field, only: field_t use m_mesh, only: mesh_t use m_solver, only: init use m_turbine_model, only: turbine_model_t use m_turbine_setup, only: init_turbine_model, finalise_turbine_model use m_logger, only: log_info, abort_run, str implicit none type, extends(base_case_t) :: case_wind_turbine_t type(wind_turbine_config_t) :: wt_cfg class(turbine_model_t), pointer :: turbine => null() !> Free-slip walls in y and in z logical :: y_walls = .false., z_walls = .false. !> Weights of the local y and z points in the outlet flow rate, see !> wall_weights real(dp), allocatable :: flow_rate_y_weights(:), flow_rate_z_weights(:) contains procedure :: define_BC => define_BC_wind_turbine procedure :: initial_conditions => initial_conditions_wind_turbine procedure :: forcings => forcings_wind_turbine procedure :: apply_BC => apply_BC_wind_turbine procedure :: postprocess => postprocess_wind_turbine procedure :: post_step => post_step_wind_turbine procedure :: finalise_case_specific => finalise_wind_turbine end type case_wind_turbine_t interface case_wind_turbine_t module procedure case_wind_turbine_init end interface case_wind_turbine_t contains function case_wind_turbine_init(backend, mesh, host_allocator) & result(flow_case) implicit none class(base_backend_t), target, intent(inout) :: backend type(mesh_t), target, intent(inout) :: mesh type(allocator_t), target, intent(inout) :: host_allocator type(case_wind_turbine_t) :: flow_case ! The inflow, outflow and mass-correction BCs act on the x = 1 and x = nx ! planes and assume every rank holds both, so x must not be decomposed. if (mesh%par%nproc_dir(1) /= 1) & call abort_run('wind_turbine: x must not be decomposed (nproc_dir(1) = & &1).', collective=.true.) ! apply_BC overwrites both x planes as Dirichlet inflow and outflow, so the ! mesh must agree for the derivatives and the pressure solve. if (any(mesh%grid%BCs_global(1, :) /= BC_DIRICHLET)) & call abort_run('wind_turbine requires BC_x = ''dirichlet'', & &''dirichlet''.', collective=.true.) ! Free-slip walls (Incompact3d ncl = 1) or periodic, in y and in z flow_case%y_walls = walls_or_periodic(mesh, 2, 'BC_y') flow_case%z_walls = walls_or_periodic(mesh, 3, 'BC_z') ! field_set_face stamps the local y planes, so the y walls must be the ! planes of every rank if (flow_case%y_walls .and. mesh%par%nproc_dir(2) /= 1) & call abort_run('wind_turbine: y walls need y not decomposed & &(nproc_dir(2) = 1).', collective=.true.) flow_case%flow_rate_y_weights = wall_weights(mesh, 2, flow_case%y_walls) flow_case%flow_rate_z_weights = wall_weights(mesh, 3, flow_case%z_walls) call flow_case%wt_cfg%read(nml_file=get_argument(1)) call flow_case%case_init(backend, mesh, host_allocator) ! Seed the RNG deterministically, offset by rank, so inlet-noise runs are ! reproducible for a fixed decomposition while ranks stay decorrelated. ! The RNG state is not checkpointed, so a restart also offsets the seed by ! the restart iteration rather than replaying the noise from step 0. call seed_rng(flow_case%solver%mesh%par%nrank, & flow_case%solver%mesh%par%nproc, & flow_case%solver%current_iter) call init_turbine_model(flow_case%turbine, flow_case%wt_cfg, & flow_case%solver, flow_case%io_mgr) end function case_wind_turbine_init logical function walls_or_periodic(mesh, dir, name) result(walls) !! Whether direction dir has free-slip walls ('neumann' at both ends), !! stopping the run unless it is that or periodic at both ends. type(mesh_t), intent(in) :: mesh integer, intent(in) :: dir character(len=*), intent(in) :: name walls = all(mesh%grid%BCs_global(dir, :) == BC_NEUMANN) if (.not. (walls .or. all(mesh%grid%BCs_global(dir, :) == BC_PERIODIC))) & call abort_run('wind_turbine requires '//name//' = ''periodic'', & &''periodic'' or ''neumann'', ''neumann''.', & collective=.true.) end function walls_or_periodic function wall_weights(mesh, dir, walls) result(w) !! Weights of the local vertices of direction dir in the outlet flow !! rate, as the discrete divergence sees it: its interpolation along a !! non-periodic direction gives the global boundary planes half the !! weight of the others (trapezoid). u is not stamped on a free-slip !! wall, so the wall planes keep their 1/2. type(mesh_t), intent(in) :: mesh integer, intent(in) :: dir logical, intent(in) :: walls real(dp), allocatable :: w(:) integer :: dims(3) dims = mesh%get_dims(VERT) allocate (w(dims(dir)), source=1._dp) if (walls) then if (mesh%par%nrank_dir(dir) == 0) w(1) = 0.5_dp if (mesh%par%nrank_dir(dir) == mesh%par%nproc_dir(dir) - 1) & w(dims(dir)) = 0.5_dp end if end function wall_weights subroutine seed_rng(nrank, nproc, iter) !! Deterministic seeding of the intrinsic RNG, distinct for every !! (rank, start iteration) pair. A fresh run starts at iter = 0. integer, intent(in) :: nrank, nproc, iter ! Odd multiplier that spreads the seed array over distinct values. integer, parameter :: seed_stride = 37 integer :: seed_size, s integer, allocatable :: seed(:) call random_seed(size=seed_size) allocate (seed(seed_size)) seed = [(seed_stride*s + 1, s=1, seed_size)] + nrank + nproc*iter call random_seed(put=seed) end subroutine seed_rng ! Initial Conditions: uniform inflow with localised noise subroutine initial_conditions_wind_turbine(self) implicit none class(case_wind_turbine_t) :: self class(field_t), pointer :: u_init, v_init, w_init integer :: i, j, k, dims(3) real(dp) :: noise(3) dims = self%solver%mesh%get_dims(VERT) u_init => self%solver%host_allocator%get_block(DIR_C) v_init => self%solver%host_allocator%get_block(DIR_C) w_init => self%solver%host_allocator%get_block(DIR_C) call random_number(u_init%data(1:dims(1), 1:dims(2), 1:dims(3))) call random_number(v_init%data(1:dims(1), 1:dims(2), 1:dims(3))) call random_number(w_init%data(1:dims(1), 1:dims(2), 1:dims(3))) noise = self%wt_cfg%init_noise do k = 1, dims(3) do j = 1, dims(2) do i = 1, dims(1) u_init%data(i, j, k) = self%wt_cfg%bc_start_u & + noise(1)*(2*u_init%data(i, j, k) - 1._dp) v_init%data(i, j, k) = self%wt_cfg%bc_start_v & + noise(2)*(2*v_init%data(i, j, k) - 1._dp) w_init%data(i, j, k) = self%wt_cfg%bc_start_w & + noise(3)*(2*w_init%data(i, j, k) - 1._dp) end do end do end do call self%solver%backend%set_field_data(self%solver%u, u_init%data) call self%solver%backend%set_field_data(self%solver%v, v_init%data) call self%solver%backend%set_field_data(self%solver%w, w_init%data) call self%solver%host_allocator%release_block(u_init) call self%solver%host_allocator%release_block(v_init) call self%solver%host_allocator%release_block(w_init) call self%solver%u%set_data_loc(VERT) call self%solver%v%set_data_loc(VERT) call self%solver%w%set_data_loc(VERT) end subroutine initial_conditions_wind_turbine subroutine define_BC_wind_turbine(self) !! Boundary Conditions hook (called per substep before transeq). !! Builds the inlet Dirichlet profile only; outflow parameters are !! computed in apply_BC from the post-integration velocity. !! u = bc_start_u + inlet_noise(1) * um * (2r - 1) !! v = bc_start_v + inlet_noise(2) * um * (2r - 1) !! w = bc_start_w + inlet_noise(3) * um * (2r - 1) implicit none class(case_wind_turbine_t) :: self real(dp), allocatable :: pu(:, :), pv(:, :), pw(:, :) integer :: dims(3) real(dp) :: noise(3), um logical :: first_call dims = self%solver%mesh%get_dims(VERT) noise = self%wt_cfg%inlet_noise um = abs(self%wt_cfg%bc_start_u) ! Allocate persistent device BC fields on first call first_call = .not. associated(self%bc_start_u_x) if (first_call) then self%bc_start_u_x => self%solver%backend%allocator%get_block(DIR_X, VERT) self%bc_start_v_x => self%solver%backend%allocator%get_block(DIR_X, VERT) self%bc_start_w_x => self%solver%backend%allocator%get_block(DIR_X, VERT) call self%bc_start_u_x%set_data_loc(VERT) call self%bc_start_v_x%set_data_loc(VERT) call self%bc_start_w_x%set_data_loc(VERT) end if ! A noise-free inlet is constant in time: build it once and reuse. Only a ! noisy inlet needs refreshing every substep. if (.not. first_call .and. all(noise == 0._dp)) return ! Only the inlet plane is used by apply_BC, so build just that (ny, nz) ! plane on the host and upload it; a full 3D upload and reorder per ! substep dominated the step time on GPU. allocate (pu(dims(2), dims(3)), pv(dims(2), dims(3)), pw(dims(2), dims(3))) ! Fill the inlet plane with random numbers in [0, 1) and map them onto ! noise in [-1, 1). All three are drawn even when a component has no ! noise, so the random sequence does not depend on which are active. call random_number(pu) call random_number(pv) call random_number(pw) pu = self%wt_cfg%bc_start_u + noise(1)*um*(2._dp*pu - 1._dp) pv = self%wt_cfg%bc_start_v + noise(2)*um*(2._dp*pv - 1._dp) pw = self%wt_cfg%bc_start_w + noise(3)*um*(2._dp*pw - 1._dp) ! Noise-free components are constant, so they need uploading only once if (first_call .or. noise(1) /= 0._dp) & call self%solver%backend%field_set_x_inlet_plane(self%bc_start_u_x, pu) if (first_call .or. noise(2) /= 0._dp) & call self%solver%backend%field_set_x_inlet_plane(self%bc_start_v_x, pv) if (first_call .or. noise(3) /= 0._dp) & call self%solver%backend%field_set_x_inlet_plane(self%bc_start_w_x, pw) end subroutine define_BC_wind_turbine subroutine apply_BC_wind_turbine(self, u, v, w) !! Pre-correction (called per substep after the integrator step), !! following Incompact3d's pre_correc: !! 1. cx = uxmax*gdt/dx from the post-integration field. !! 2. Dirichlet inflow at x=1 and convective outflow at x=nx for u,v,w: !! u(nx) = u(nx) - cx*(u(nx) - u(nx-1)), with a uniform shift of u on !! the outlet that makes the flow rate out equal the flow rate in. !! Incompact3d balances the plain means over the planes; here the !! planes are weighted as the discrete divergence sees them (see !! wall_weights), the same thing for periodic y and z, and exact with !! walls, where the plain mean leaves a uniform divergence the !! Neumann-x projection cannot remove. !! 3. Free-slip walls: v = 0 on the y walls and w = 0 on the z walls. !! The tangential components take the even-symmetry closure, and the !! Neumann pressure BC gives no correction on the wall planes. implicit none class(case_wind_turbine_t) :: self class(field_t), intent(inout) :: u, v, w real(dp) :: out_vel, flow_rate_diff ! Convective speed from the already-integrated velocity field call self%compute_outflow_params(out_vel) flow_rate_diff = self%outflow_flow_rate_correction( & u, out_vel, self%flow_rate_y_weights, & self%flow_rate_z_weights) call self%solver%backend%field_set_face_from_field( & u, self%bc_start_u_x, out_vel, X_FACE, & bc_start=BC_DIRICHLET, bc_end=BC_DIRICHLET, & flow_rate_diff=flow_rate_diff) ! The flow-rate correction balances the streamwise flux, so only u ! takes it call self%solver%backend%field_set_face_from_field( & v, self%bc_start_v_x, out_vel, X_FACE, & bc_start=BC_DIRICHLET, bc_end=BC_DIRICHLET) call self%solver%backend%field_set_face_from_field( & w, self%bc_start_w_x, out_vel, X_FACE, & bc_start=BC_DIRICHLET, bc_end=BC_DIRICHLET) ! Impermeable walls, stamped last so they win on the edges where they ! meet the x faces, as in Incompact3d if (self%y_walls) & call self%solver%backend%field_set_face(v, 0._dp, 0._dp, Y_FACE) if (self%z_walls) & call self%solver%backend%field_set_face(w, 0._dp, 0._dp, Z_FACE) end subroutine apply_BC_wind_turbine ! Forcings: advance the turbine model and accumulate its momentum source. subroutine forcings_wind_turbine(self, du, dv, dw, iter) implicit none class(case_wind_turbine_t) :: self class(field_t), intent(inout) :: du, dv, dw integer, intent(in) :: iter real(dp) :: t t = real(iter, dp)*self%solver%dt call self%turbine%update(t, self%solver%dt) call self%turbine%project_forces(du, dv, dw, & self%solver%u, self%solver%v, & self%solver%w) end subroutine forcings_wind_turbine ! Turbine diagnostics every iturboutput steps, independent of n_output. subroutine post_step_wind_turbine(self, iter, t) implicit none class(case_wind_turbine_t) :: self integer, intent(in) :: iter real(dp), intent(in) :: t if (self%wt_cfg%iturboutput > 0) then if (mod(iter, self%wt_cfg%iturboutput) == 0) then call self%turbine%write_output(iter, self%solver%mesh%par%is_root()) end if end if end subroutine post_step_wind_turbine ! Post-processing: flow diagnostics. subroutine postprocess_wind_turbine(self, iter, t) implicit none class(case_wind_turbine_t) :: self integer, intent(in) :: iter real(dp), intent(in) :: t call log_info('time = '//str(t)//' iteration = '//str(iter)) call self%monitoring%write_step( & self%solver, t, self%solver%u, self%solver%v, self%solver%w) end subroutine postprocess_wind_turbine subroutine finalise_wind_turbine(self) class(case_wind_turbine_t) :: self call finalise_turbine_model(self%turbine, self%io_mgr) end subroutine finalise_wind_turbine end module m_case_wind_turbine