Skip to content

Commit

Permalink
clean up depth mapping logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jornbr committed Mar 26, 2024
1 parent eb3a99c commit 8e88637
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 45 deletions.
66 changes: 21 additions & 45 deletions src/builtin/depth_mapping.F90
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ module fabm_builtin_depth_mapping

private

public type_vertical_distribution, type_vertical_depth_range
public set_vertical_distribution, register_depth_explicit_dependency, register_depth_explicit_state_dependency
public type_vertical_depth_range
public register_depth_explicit_dependency, register_depth_explicit_state_dependency
public type_depth_integrated_particle
public type_depth_integrated_particle_override

type, extends(type_base_model) :: type_vertical_distribution
type (type_diagnostic_variable_id) :: id_w
end type

type, extends(type_vertical_distribution) :: type_vertical_depth_range
type (type_dependency_id) :: id_z, id_h
type, extends(type_base_model) :: type_vertical_depth_range
type (type_diagnostic_variable_id) :: id_w ! weights specifying the vertical distribution
type (type_dependency_id) :: id_z ! depth of layer center (m)
type (type_dependency_id) :: id_h ! layer thickness (m)
real(rk) :: minimum_depth = 0.0_rk
real(rk) :: maximum_depth = 1e5_rk
contains
Expand Down Expand Up @@ -171,42 +169,6 @@ subroutine vertical_depth_range_do(self, _ARGUMENTS_DO_)
_LOOP_END_
end subroutine

subroutine set_vertical_distribution(self, w_calculator, w_int_link)
class (type_base_model), intent(inout) :: self
class (type_vertical_distribution), optional, intent(inout) :: w_calculator
type (type_link), pointer, optional :: w_int_link

class (type_depth_integral), pointer :: w_int_calculator
type (type_link), pointer :: w_int_link_

! Register a dependency on the vertical distribution weights
! We use add_interior_variable instead of register_dependency because we do not
! need read access to the weights ourselves (they are only for child models)
call self%add_interior_variable('w', '1', 'vertical distribution weights')

if (present(w_calculator)) then
call self%add_child(w_calculator, 'w_calculator')
call self%request_coupling('w', 'w_calculator/w')
end if

! Create a child model that calculates the depth integral of the weights
! This is used to calculate depth averages form depth integrals, among others.
allocate(w_int_calculator)
call self%add_child(w_int_calculator, 'w_int_calculator')
call w_int_calculator%request_coupling('source', '../w')

! Make the depth integrated available as a local variable
if (present(w_int_link)) then
! A variable has already been registered to hold the depth-integrated weights
w_int_link_ => w_int_link
else
! Create a local alias for the depth-integrated weights
w_int_link_ => null()
call self%add_horizontal_variable('w_int', 'm', 'integral of vertical distribution weights', link=w_int_link_)
end if
call self%request_coupling(w_int_link_, 'w_int_calculator/result')
end subroutine

subroutine register_depth_explicit_dependency(self, id, name, units, long_name, average, link)
class (type_base_model), intent(inout) :: self
class (type_horizontal_dependency_id), intent(inout) :: id
Expand Down Expand Up @@ -484,8 +446,22 @@ subroutine depth_integrated_particle_initialize(self, configunit)
class (type_depth_integrated_particle), intent(inout), target :: self
integer, intent(in) :: configunit

class (type_depth_integral), pointer :: w_int_calculator

! Register a dependency on the vertical distribution weights
! We use add_interior_variable instead of register_dependency because we do not
! need read access to the weights ourselves (they are only for child models)
call self%add_interior_variable('w', '1', 'vertical distribution weights', presence=presence_external_required)

! Create a child model that calculates the depth integral of the weights
! This is used to calculate depth averages form depth integrals, among others.
allocate(w_int_calculator)
call self%add_child(w_int_calculator, 'w_int_calculator')
call w_int_calculator%request_coupling('source', '../w')

! Make the depth integrated available as a local variable
call self%register_dependency(self%id_w_int, 'w_int', 'm', 'depth-integrated vertical distribution weights')
call set_vertical_distribution(self, w_int_link=self%id_w_int%link)
call self%request_coupling(self%id_w_int, 'w_int_calculator/result')
end subroutine

subroutine depth_integrated_particle_request_mapped_coupling1(self, id, standard_variable, average)
Expand Down
3 changes: 3 additions & 0 deletions src/models/examples/particle/depth_integrated_predator.F90
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ subroutine initialize(self, configunit)
! For now, specify a prescribed depth distribution
allocate(depth_distribution)
call self%add_child(depth_distribution, 'habitat')

! Link our own weights that specify the vertical habitat (variable "w", inherited
! from type_depth_integrated_particle) to those computed by the "habitat" child model
call self%request_coupling('w', 'habitat/w')

! Predator biomass and its contribution to different elemental pools
Expand Down

0 comments on commit 8e88637

Please sign in to comment.