MOM6
MOM_unit_scaling.F90
1 !> Provides a transparent unit rescaling type to facilitate dimensional consistency testing
3 
4 ! This file is part of MOM6. See LICENSE.md for the license.
5 
6 use mom_error_handler, only : mom_error, mom_mesg, fatal
8 
9 implicit none ; private
10 
11 public unit_scaling_init, unit_scaling_end, fix_restart_unit_scaling
12 
13 !> Describes various unit conversion factors
14 type, public :: unit_scale_type
15  real :: m_to_z !< A constant that translates distances in meters to the units of depth.
16  real :: z_to_m !< A constant that translates distances in the units of depth to meters.
17  real :: m_to_l !< A constant that translates lengths in meters to the units of horizontal lengths.
18  real :: l_to_m !< A constant that translates lengths in the units of horizontal lengths to meters.
19  real :: s_to_t !< A constant that time intervals in seconds to the units of time.
20  real :: t_to_s !< A constant that the units of time to seconds.
21 
22  ! These are useful combinations of the fundamental scale conversion factors above.
23  real :: z_to_l !< Convert vertical distances to lateral lengths
24  real :: l_to_z !< Convert vertical distances to lateral lengths
25  real :: l_t_to_m_s !< Convert lateral velocities from L T-1 to m s-1.
26  real :: m_s_to_l_t !< Convert lateral velocities from m s-1 to L T-1.
27  real :: l_t2_to_m_s2 !< Convert lateral accelerations from L T-2 to m s-2.
28  real :: z2_t_to_m2_s !< Convert vertical diffusivities from Z2 T-1 to m2 s-1.
29  real :: m2_s_to_z2_t !< Convert vertical diffusivities from m2 s-1 to Z2 T-1.
30 
31  ! These are used for changing scaling across restarts.
32  real :: m_to_z_restart = 0.0 !< A copy of the m_to_Z that is used in restart files.
33  real :: m_to_l_restart = 0.0 !< A copy of the m_to_L that is used in restart files.
34  real :: s_to_t_restart = 0.0 !< A copy of the s_to_T that is used in restart files.
35 end type unit_scale_type
36 
37 contains
38 
39 !> Allocates and initializes the ocean model unit scaling type
40 subroutine unit_scaling_init( param_file, US )
41  type(param_file_type), intent(in) :: param_file !< Parameter file handle/type
42  type(unit_scale_type), pointer :: us !< A dimensional unit scaling type
43 
44  ! This routine initializes a unit_scale_type structure (US).
45 
46  ! Local variables
47  integer :: z_power, l_power, t_power
48  real :: z_rescale_factor, l_rescale_factor, t_rescale_factor
49  ! This include declares and sets the variable "version".
50 # include "version_variable.h"
51  character(len=16) :: mdl = "MOM_unit_scaling"
52 
53  if (associated(us)) call mom_error(fatal, &
54  'unit_scaling_init: called with an associated US pointer.')
55  allocate(us)
56 
57  ! Read all relevant parameters and write them to the model log.
58  call log_version(param_file, mdl, version, &
59  "Parameters for doing unit scaling of variables.")
60  call get_param(param_file, mdl, "Z_RESCALE_POWER", z_power, &
61  "An integer power of 2 that is used to rescale the model's "//&
62  "intenal units of depths and heights. Valid values range from -300 to 300.", &
63  units="nondim", default=0, debuggingparam=.true.)
64  call get_param(param_file, mdl, "L_RESCALE_POWER", l_power, &
65  "An integer power of 2 that is used to rescale the model's "//&
66  "intenal units of lateral distances. Valid values range from -300 to 300.", &
67  units="nondim", default=0, debuggingparam=.true.)
68  call get_param(param_file, mdl, "T_RESCALE_POWER", t_power, &
69  "An integer power of 2 that is used to rescale the model's "//&
70  "intenal units of time. Valid values range from -300 to 300.", &
71  units="nondim", default=0, debuggingparam=.true.)
72  if (abs(z_power) > 300) call mom_error(fatal, "unit_scaling_init: "//&
73  "Z_RESCALE_POWER is outside of the valid range of -300 to 300.")
74  if (abs(l_power) > 300) call mom_error(fatal, "unit_scaling_init: "//&
75  "L_RESCALE_POWER is outside of the valid range of -300 to 300.")
76  if (abs(t_power) > 300) call mom_error(fatal, "unit_scaling_init: "//&
77  "T_RESCALE_POWER is outside of the valid range of -300 to 300.")
78 
79  z_rescale_factor = 1.0
80  if (z_power /= 0) z_rescale_factor = 2.0**z_power
81  us%Z_to_m = 1.0 * z_rescale_factor
82  us%m_to_Z = 1.0 / z_rescale_factor
83 
84  l_rescale_factor = 1.0
85  if (l_power /= 0) l_rescale_factor = 2.0**l_power
86  us%L_to_m = 1.0 * l_rescale_factor
87  us%m_to_L = 1.0 / l_rescale_factor
88 
89  t_rescale_factor = 1.0
90  if (t_power /= 0) t_rescale_factor = 2.0**t_power
91  us%T_to_s = 1.0 * t_rescale_factor
92  us%s_to_T = 1.0 / t_rescale_factor
93 
94  ! These are useful combinations of the fundamental scale conversion factors set above.
95  us%Z_to_L = us%Z_to_m * us%m_to_L
96  us%L_to_Z = us%L_to_m * us%m_to_Z
97  us%L_T_to_m_s = us%L_to_m * us%s_to_T
98  us%m_s_to_L_T = us%m_to_L * us%T_to_s
99  us%L_T2_to_m_s2 = us%L_to_m * us%s_to_T**2
100  ! It does not look like US%m_s2_to_L_T2 would be used, so it does not exist.
101  us%Z2_T_to_m2_s = us%Z_to_m**2 * us%s_to_T
102  us%m2_s_to_Z2_T = us%m_to_Z**2 * us%T_to_s
103 
104 end subroutine unit_scaling_init
105 
106 !> Set the unit scaling factors for output to restart files to the unit scaling
107 !! factors for this run.
108 subroutine fix_restart_unit_scaling(US)
109  type(unit_scale_type), intent(inout) :: us !< A dimensional unit scaling type
110 
111  us%m_to_Z_restart = us%m_to_Z
112  us%m_to_L_restart = us%m_to_L
113  us%s_to_T_restart = us%s_to_T
114 
115 end subroutine fix_restart_unit_scaling
116 
117 !> Deallocates a unit scaling structure.
118 subroutine unit_scaling_end( US )
119  type(unit_scale_type), pointer :: us !< A dimensional unit scaling type
120 
121  deallocate( us )
122 
123 end subroutine unit_scaling_end
124 
125 end module mom_unit_scaling
mom_file_parser::log_version
An overloaded interface to log version information about modules.
Definition: MOM_file_parser.F90:109
mom_file_parser::param_file_type
A structure that can be parsed to read and document run-time parameters.
Definition: MOM_file_parser.F90:54
mom_file_parser::get_param
An overloaded interface to read and log the values of various types of parameters.
Definition: MOM_file_parser.F90:102
mom_unit_scaling::unit_scale_type
Describes various unit conversion factors.
Definition: MOM_unit_scaling.F90:14
mom_file_parser
The MOM6 facility to parse input files for runtime parameters.
Definition: MOM_file_parser.F90:2
mom_unit_scaling
Provides a transparent unit rescaling type to facilitate dimensional consistency testing.
Definition: MOM_unit_scaling.F90:2
mom_file_parser::log_param
An overloaded interface to log the values of various types of parameters.
Definition: MOM_file_parser.F90:96
mom_error_handler
Routines for error handling and I/O management.
Definition: MOM_error_handler.F90:2