Skip to content

Commit

Permalink
Merge pull request #60 from mnlevy1981/feature/more_efficient_tidal
Browse files Browse the repository at this point in the history
Refactor cvmix_tidal to only compute vert_dep once
  • Loading branch information
mnlevy1981 committed Jan 20, 2016
2 parents dc5038a + 7edab82 commit e7d5e80
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 83 deletions.
2 changes: 1 addition & 1 deletion reg_tests/tidal-Simmons/Simmons-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ build
run

# (2) Look at output
ncdump -v Tdiff single_col.nc
ncdump -v Mdiff,Tdiff single_col.nc
29 changes: 24 additions & 5 deletions src/drivers/cvmix_tidal_Simmons.F90
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Subroutine cvmix_tidal_driver()
cvmix_global_params_type
use cvmix_tidal, only : cvmix_init_tidal, &
cvmix_coeffs_tidal, &
cvmix_compute_Simmons_invariant, &
cvmix_tidal_params_type, &
cvmix_get_tidal_str, &
cvmix_get_tidal_real
Expand All @@ -42,7 +43,7 @@ Subroutine cvmix_tidal_driver()
type(cvmix_global_params_type) :: CVmix_params
type(cvmix_tidal_params_type) :: CVmix_Simmons_params

real(cvmix_r8), dimension(:,:,:), allocatable, target :: Tdiff
real(cvmix_r8), dimension(:,:,:), allocatable, target :: Mdiff, Tdiff

! file index
integer :: fid
Expand Down Expand Up @@ -118,9 +119,11 @@ Subroutine cvmix_tidal_driver()
buoy(:,:,1) = cvmix_zero

! Allocate memory to store diffusivity values
allocate(Mdiff(nlon, nlat, max_nlev+1))
allocate(Tdiff(nlon, nlat, max_nlev+1))
! Set diffusivity to _FillValue
FillVal = 1e5_cvmix_r8
Mdiff = FillVal
Tdiff = FillVal

! Read in global data from grid file, physics file, and energy flux file
Expand Down Expand Up @@ -181,13 +184,18 @@ Subroutine cvmix_tidal_driver()
call cvmix_put(CVmix_vars(i,j), 'lat', lat(i,j))
call cvmix_put(CVmix_vars(i,j), 'lon', lon(i,j))

call cvmix_put(CVmix_params, 'max_nlev', max_nlev)
call cvmix_put(CVmix_params, 'max_nlev', max_nlev)
call cvmix_put(CVmix_params, 'Prandtl', 10._cvmix_r8)
call cvmix_put(CVmix_params, 'fw_rho', 1e3_cvmix_r8)
call cvmix_compute_Simmons_invariant(CVmix_vars(i,j), CVmix_params, &
energy_flux(i,j), &
CVmix_Simmons_params)
! Point CVmix_vars values to memory allocated above
CVmix_vars(i,j)%Mdiff_iface => Mdiff(i,j,1:nlev+1)
CVmix_vars(i,j)%Tdiff_iface => Tdiff(i,j,1:nlev+1)

call cvmix_coeffs_tidal(CVmix_vars(i,j), CVmix_params, &
energy_flux(i,j), CVmix_Simmons_params)
CVmix_Simmons_params)

end if
end do
Expand All @@ -205,7 +213,7 @@ Subroutine cvmix_tidal_driver()
this_lat = CVmix_vars(lon_out, lat_out)%lat
call cvmix_io_open(fid, "single_col.nc", "nc")
call cvmix_output_write(fid, CVmix_vars(lon_out, lat_out), &
(/"zw_iface", "Tdiff "/))
(/"zw_iface", "Mdiff ", "Tdiff "/))
if (this_lon.ge.0) then
write(lonstr,"(F6.2,1X,A)") this_lon, "E"
else
Expand All @@ -221,6 +229,9 @@ Subroutine cvmix_tidal_driver()
call cvmix_output_write_att(fid, "column_lat", latstr)

! Variable Attributes
call cvmix_output_write_att(fid, "long_name", "momentum diffusivity", &
var_name="Mdiff")
call cvmix_output_write_att(fid, "units", "m^2/s", var_name="Mdiff")
call cvmix_output_write_att(fid, "long_name", "tracer diffusivity", &
var_name="Tdiff")
call cvmix_output_write_att(fid, "units", "m^2/s", var_name="Tdiff")
Expand All @@ -235,7 +246,15 @@ Subroutine cvmix_tidal_driver()
end if

! Write diffusivity field to netcdf
call cvmix_io_open(fid, "diff.nc", "nc")
call cvmix_io_open(fid, "Mdiff.nc", "nc")
call cvmix_output_write(fid, "Mdiff", (/"nlon ", "nlat ", "niface"/), &
Mdiff, FillVal=FillVal)
call cvmix_output_write_att(fid, "long_name", "momentum diffusivity", &
var_name="Mdiff")
call cvmix_output_write_att(fid, "units", "m^2/s", var_name="Mdiff")
call cvmix_io_close(fid)

call cvmix_io_open(fid, "Tdiff.nc", "nc")
call cvmix_output_write(fid, "Tdiff", (/"nlon ", "nlat ", "niface"/), &
Tdiff, FillVal=FillVal)
call cvmix_output_write_att(fid, "long_name", "tracer diffusivity", &
Expand Down
7 changes: 6 additions & 1 deletion src/shared/cvmix_kinds_and_types.F90
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ module cvmix_kinds_and_types
! Surface Stokes drift magnitude
real(cvmix_r8) :: SurfaceStokesDrift
! units: m/s
! A time-invariant coefficient needed for Simmons, et al. tidal mixing
real(cvmix_r8) :: SimmonsCoeff

! Values on interfaces (dimsize = nlev+1)
! --------------------
Expand All @@ -125,9 +127,12 @@ module cvmix_kinds_and_types
real(cvmix_r8), dimension(:), pointer :: ShearRichardson_iface => NULL()
! units: unitless

! For tidal mixing, we need the squared buoyancy frequency
! For tidal mixing, we need the squared buoyancy frequency and vertical
! deposition function
real(cvmix_r8), dimension(:), pointer :: SqrBuoyancyFreq_iface => NULL()
! units: s^-2
real(cvmix_r8), dimension(:), pointer :: VertDep_iface => NULL()
! units: unitless

! For KPP, need to store non-local transport term
real(cvmix_r8), dimension(:), pointer :: kpp_Tnonlocal_iface => NULL()
Expand Down
91 changes: 51 additions & 40 deletions src/shared/cvmix_put_get.F90
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module cvmix_put_get
!\newpage
! !MODULE: cvmix_put_get
!
! !AUTHOR:
! !AUTHOR:
! Michael N. Levy, NCAR (mlevy@ucar.edu)
!
! !DESCRIPTION:
Expand Down Expand Up @@ -57,7 +57,7 @@ subroutine cvmix_put_int(CVmix_vars, varname, val, nlev_in)
!\\
! !USES:
! Only those used by entire module.
! Only those used by entire module.

! !INPUT PARAMETERS:
character(len=*), intent(in) :: varname
Expand All @@ -72,7 +72,7 @@ subroutine cvmix_put_int(CVmix_vars, varname, val, nlev_in)

! Local variables
integer :: nlev

if (present(nlev_in)) then
nlev = nlev_in
else
Expand All @@ -85,7 +85,7 @@ subroutine cvmix_put_int(CVmix_vars, varname, val, nlev_in)
print*, "You tried to set ", trim(varname)
stop 1
end if

select case (trim(cvmix_att_name(varname)))
case ('nlev')
CVmix_vars%nlev = val
Expand Down Expand Up @@ -118,7 +118,7 @@ subroutine cvmix_put_real(CVmix_vars, varname, val, nlev_in)
!\\
! !USES:
! Only those used by entire module.
! Only those used by entire module.

! !INPUT PARAMETERS:
character(len=*), intent(in) :: varname
Expand All @@ -132,7 +132,7 @@ subroutine cvmix_put_real(CVmix_vars, varname, val, nlev_in)

! Local variables
integer :: nlev

if (present(nlev_in)) then
nlev = nlev_in
else
Expand All @@ -145,7 +145,7 @@ subroutine cvmix_put_real(CVmix_vars, varname, val, nlev_in)
print*, "You tried to set ", trim(varname)
stop 1
end if

select case (trim(cvmix_att_name(varname)))
case ('OceanDepth')
CVmix_vars%OceanDepth = val
Expand All @@ -170,10 +170,12 @@ subroutine cvmix_put_real(CVmix_vars, varname, val, nlev_in)
CVmix_vars%LangmuirEnhancementFactor = val
case ("SurfaceStokesDrift")
CVmix_vars%SurfaceStokesDrift = val
case ('SimmonsCoeff')
CVmix_vars%SimmonsCoeff = val

case ("dzw")
print*, "WARNING: you are setting the cell midpoint to midpoint", &
"distance in all levels to a constant value"
! print*, "WARNING: you are setting the cell midpoint to midpoint ", &
! "distance in all levels to a constant value"
if (.not.associated(CVmix_vars%dzw)) then
allocate(CVmix_vars%dzw(nlev+1))
end if
Expand All @@ -194,15 +196,15 @@ subroutine cvmix_put_real(CVmix_vars, varname, val, nlev_in)
end if
CVmix_vars%Sdiff_iface(:) = val
case ("ShearRichardson_iface")
print*, "WARNING: you are setting the Richardson number in all", &
"levels to a constant value"
! print*, "WARNING: you are setting the Richardson number in all ", &
! "levels to a constant value"
if (.not.associated(CVmix_vars%ShearRichardson_iface)) then
allocate(CVmix_vars%ShearRichardson_iface(nlev+1))
end if
CVmix_vars%ShearRichardson_iface(:) = val
case ("SqrBuoyancyFreq_iface")
print*, "WARNING: you are setting the buoyancy in all levels to a", &
"constant value"
! print*, "WARNING: you are setting the buoyancy in all levels to a ", &
! "constant value"
if (.not.associated(CVmix_vars%SqrBuoyancyFreq_iface)) then
allocate(CVmix_vars%SqrBuoyancyFreq_iface(nlev+1))
end if
Expand All @@ -228,52 +230,57 @@ subroutine cvmix_put_real(CVmix_vars, varname, val, nlev_in)
CVmix_vars%kpp_Snonlocal_iface(:) = val

case ("dzt")
print*, "WARNING: you are setting the cell thickness in all levels", &
"to a constant value"
! print*, "WARNING: you are setting the cell thickness in all levels ", &
! "to a constant value"
if (.not.associated(CVmix_vars%dzt)) then
allocate(CVmix_vars%dzt(nlev))
end if
CVmix_vars%dzt(:) = val
case ("WaterDensity_cntr")
print*, "WARNING: you are setting the density in all levels to a", &
"constant value"
! print*, "WARNING: you are setting the density in all levels to a ", &
! "constant value"
if (.not.associated(CVmix_vars%WaterDensity_cntr)) then
allocate(CVmix_vars%WaterDensity_cntr(nlev))
end if
CVmix_vars%WaterDensity_cntr(:) = val
case ("AdiabWaterDensity_cntr")
print*, "WARNING: you are setting the adiabatic density in all", &
"levels to a constant value"
! print*, "WARNING: you are setting the adiabatic density in all ", &
! "levels to a constant value"
if (.not.associated(CVmix_vars%AdiabWaterDensity_cntr)) then
allocate(CVmix_vars%AdiabWaterDensity_cntr(nlev))
end if
CVmix_vars%AdiabWaterDensity_cntr(:) = val
case ("BulkRichardson_cntr")
print*, "WARNING: you are setting the bulk Richardson number in all", &
"levels to a constant value"
! print*, "WARNING: you are setting the bulk Richardson number in all", &
! " levels to a constant value"
if (.not.associated(CVmix_vars%BulkRichardson_cntr)) then
allocate(CVmix_vars%BulkRichardson_cntr(nlev))
end if
CVmix_vars%BulkRichardson_cntr(:) = val
case ('strat_param_num')
print*, "WARNING: you are setting the numerator of the", &
"stratification parameter in all levels to a constant value"
! print*, "WARNING: you are setting the numerator of the ", &
! "stratification parameter in all levels to a constant value"
if (.not.associated(CVmix_vars%strat_param_num)) then
allocate(CVmix_vars%strat_param_num(nlev))
end if
CVmix_vars%strat_param_num(:) = val
case ('strat_param_denom')
print*, "WARNING: you are setting the denominator of the", &
"stratification parameter in all levels to a constant value"
! print*, "WARNING: you are setting the denominator of the ", &
! "stratification parameter in all levels to a constant value"
if (.not.associated(CVmix_vars%strat_param_denom)) then
allocate(CVmix_vars%strat_param_denom(nlev))
end if
CVmix_vars%strat_param_denom(:) = val
case ("VertDep_iface")
if (.not.associated(CVmix_vars%VertDep_iface)) then
allocate(CVmix_vars%VertDep_iface(nlev+1))
end if
CVmix_vars%VertDep_iface(:) = val

case default
print*, "ERROR: ", trim(varname), " not a valid choice!"
print*, "ERROR: ", trim(varname), " not a valid choice for cvmix_put!"
stop 1

end select
!EOC

Expand All @@ -292,7 +299,7 @@ subroutine cvmix_put_real_1D(CVmix_vars, varname, val, nlev_in)
!\\
! !USES:
! Only those used by entire module.
! Only those used by entire module.

! !INPUT PARAMETERS:
character(len=*), intent(in) :: varname
Expand All @@ -306,7 +313,7 @@ subroutine cvmix_put_real_1D(CVmix_vars, varname, val, nlev_in)

! Local variables
integer :: nlev

if (present(nlev_in)) then
nlev = nlev_in
else
Expand All @@ -319,7 +326,7 @@ subroutine cvmix_put_real_1D(CVmix_vars, varname, val, nlev_in)
print*, "You tried to set ", trim(varname)
stop 1
end if

select case (trim(cvmix_att_name(varname)))
case ("zw_iface")
if (.not.associated(CVmix_vars%zw_iface)) then
Expand Down Expand Up @@ -375,7 +382,11 @@ subroutine cvmix_put_real_1D(CVmix_vars, varname, val, nlev_in)
allocate(CVmix_vars%kpp_Snonlocal_iface(nlev+1))
end if
CVmix_vars%kpp_Snonlocal_iface(:) = val

case ("VertDep_iface")
if (.not.associated(CVmix_vars%VertDep_iface)) then
allocate(CVmix_vars%VertDep_iface(nlev+1))
end if
CVmix_vars%VertDep_iface(:) = val
case ("zt_cntr")
if (.not.associated(CVmix_vars%zt_cntr)) then
allocate(CVmix_vars%zt_cntr(nlev))
Expand Down Expand Up @@ -423,9 +434,9 @@ subroutine cvmix_put_real_1D(CVmix_vars, varname, val, nlev_in)
CVmix_vars%Vy_cntr(:) = val

case default
print*, "ERROR: ", trim(varname), " not a valid choice!"
print*, "ERROR: ", trim(varname), " not a valid choice for cvmix_put!"
stop 1

end select

!EOC
Expand All @@ -445,7 +456,7 @@ subroutine cvmix_put_global_params_int(CVmix_params, varname, val)
!\\
! !USES:
! Only those used by entire module.
! Only those used by entire module.

! !INPUT PARAMETERS:
character(len=*), intent(in) :: varname
Expand All @@ -459,11 +470,11 @@ subroutine cvmix_put_global_params_int(CVmix_params, varname, val)
select case (trim(varname))
case ('max_nlev')
CVmix_params%max_nlev = val

case default
print*, "ERROR: ", trim(varname), " not a valid choice!"
stop 1

end select
!EOC

Expand All @@ -482,7 +493,7 @@ subroutine cvmix_put_global_params_real(CVmix_params, varname, val)
!\\
! !USES:
! Only those used by entire module.
! Only those used by entire module.

! !INPUT PARAMETERS:
character(len=*), intent(in) :: varname
Expand All @@ -494,17 +505,17 @@ subroutine cvmix_put_global_params_real(CVmix_params, varname, val)
!BOC

select case (trim(varname))
case ('prandtl')
case ('prandtl','Prandtl')
CVmix_params%prandtl = val
case ('fw_rho','FreshWaterDensity')
CVmix_params%FreshWaterDensity = val
case ('sw_rho','SaltWaterDensity')
CVmix_params%SaltWaterDensity= val

case default
print*, "ERROR: ", trim(varname), " not a valid choice!"
stop 1

end select
!EOC

Expand Down
Loading

0 comments on commit e7d5e80

Please sign in to comment.