5.13.2-dev0
mHM
The mesoscale Hydrological Model
Loading...
Searching...
No Matches
mo_coupling_type.f90
Go to the documentation of this file.
1!> \dir coupling
2!> \brief \copybrief f_coupling
3!> \details \copydetails f_coupling
4
5!> \defgroup f_coupling coupling - Fortran modules
6!> \brief Core modules to couple mHM.
7!> \details These modules provide the core components to couple mHM.
8
9!> \file mo_coupling_type.f90
10!> \brief \copybrief mo_coupling_type
11!> \details \copydetails mo_coupling_type
12
13!> \brief Types to specify the coupling configuration of mHM.
14!> \version 0.1
15!> \authors Sebastian Mueller
16!> \date Apr 2023
17!> \copyright Copyright 2005-\today, the mHM Developers, Luis Samaniego, Sabine Attinger: All rights reserved.
18!! mHM is released under the LGPLv3+ license \license_note
19!> \ingroup f_coupling
21
22 use mo_kind, only: i4, dp
23
24 implicit none
25
26 private
27
28 !> \class couple_cfg_type
29 !> \brief This is a container to hold all coupling configurations for mHM.
30 type, public :: couple_cfg_type
31 character(256) :: nml_name = 'coupling' !< namelist name in mhm namelist
32 logical :: read_nml = .true. !< whether to read the namelist (or set via routine)
33 integer(i4) :: case !< coupling case (0: no coupling, 1: single domain coupling)
34 integer(i4) :: meteo_timestep !< timestep for meteo-data from coupling
35 logical :: meteo_time_ref_endpoint !< expect meteo has time reference point at end of associated time interval
36 logical :: meteo_expect_pre !< expect meteo from coupling: [mm] Precipitation
37 logical :: meteo_expect_temp !< expect meteo from coupling: [degC] Air temperature
38 logical :: meteo_expect_pet !< expect meteo from coupling: [mm TS-1] Potential evapotranspiration
39 logical :: meteo_expect_tmin !< expect meteo from coupling: [degC] minimum daily air temperature
40 logical :: meteo_expect_tmax !< expect meteo from coupling: [degC] maximum daily air temperature
41 logical :: meteo_expect_netrad !< expect meteo from coupling: [W m2] net radiation
42 logical :: meteo_expect_absvappress !< expect meteo from coupling: [Pa] absolute vapour pressure
43 logical :: meteo_expect_windspeed !< expect meteo from coupling: [m s-1] windspeed
44 ! riv-temp related
45 logical :: meteo_expect_ssrd !< expect meteo from coupling: [W m2] short wave radiation
46 logical :: meteo_expect_strd !< expect meteo from coupling: [W m2] long wave radiation
47 logical :: meteo_expect_tann !< expect meteo from coupling: [degC] annual mean air temperature
48
49 contains
50 !> \copydoc mo_coupling_type::read_config
51 procedure :: read_config !< \see mo_coupling_type::read_config
52 !> \copydoc mo_coupling_type::set_config
53 procedure :: set_config !< \see mo_coupling_type::set_config
54 !> \copydoc mo_coupling_type::any_meteo_expected
55 procedure :: any_meteo_expected !< \see mo_coupling_type::any_meteo_expected
56 !> \copydoc mo_coupling_type::clean_up
57 procedure :: clean_up !< \see mo_coupling_type::clean_up
58 !> \copydoc mo_coupling_type::check
59 procedure :: check !< \see mo_coupling_type::check
60 !> \copydoc mo_coupling_type::active
61 procedure :: active !< \see mo_coupling_type::active
62 end type couple_cfg_type
63
64contains
65
66 !> \brief clean up
67 subroutine clean_up(self)
68 implicit none
69
70 class(couple_cfg_type), intent(inout) :: self
71
72 ! restore defaults
73 call self%set_config(read_nml=.true.)
74
75 end subroutine clean_up
76
77 !> \brief read configuration for the \ref couple_cfg_type class from the mhm namelist
78 subroutine read_config(self, file_namelist, unamelist)
79 use mo_nml, only : close_nml, open_nml, position_nml
80 implicit none
81
82 class(couple_cfg_type), intent(inout) :: self
83 character(*), intent(in) :: file_namelist !< mhm namelist file
84 integer, intent(in) :: unamelist !< unit to open namelist file
85
86 integer(i4) :: case ! coupling case
87 integer(i4) :: meteo_timestep ! timestep for meteo-data from coupling
88 logical :: meteo_time_ref_endpoint ! expect meteo has time reference point at end of associated time interval
89 logical :: meteo_expect_pre ! expect meteo from coupling: [mm] Precipitation
90 logical :: meteo_expect_temp ! expect meteo from coupling: [degC] Air temperature
91 logical :: meteo_expect_pet ! expect meteo from coupling: [mm TS-1] Potential evapotranspiration
92 logical :: meteo_expect_tmin ! expect meteo from coupling: [degC] minimum daily air temperature
93 logical :: meteo_expect_tmax ! expect meteo from coupling: [degC] maximum daily air temperature
94 logical :: meteo_expect_netrad ! expect meteo from coupling: [W m2] net radiation
95 logical :: meteo_expect_absvappress ! expect meteo from coupling: [Pa] absolute vapour pressure
96 logical :: meteo_expect_windspeed ! expect meteo from coupling: [m s-1] windspeed
97 logical :: meteo_expect_ssrd ! expect meteo from coupling: [W m2] short wave radiation
98 logical :: meteo_expect_strd ! expect meteo from coupling: [W m2] long wave radiation
99 logical :: meteo_expect_tann ! expect meteo from coupling: [degC] annual mean air temperature
100
101 integer(i4) :: status ! nml status
102
103 ! namelist coupling
104 namelist /coupling/ &
105 case, &
106 meteo_timestep, &
107 meteo_time_ref_endpoint, &
108 meteo_expect_pre, &
109 meteo_expect_temp, &
110 meteo_expect_pet, &
111 meteo_expect_tmin, &
112 meteo_expect_tmax, &
113 meteo_expect_netrad, &
114 meteo_expect_absvappress, &
115 meteo_expect_windspeed, &
116 meteo_expect_ssrd, &
117 meteo_expect_strd, &
118 meteo_expect_tann
119
120 ! set defaults of namelist should be read
121 if (self%read_nml) then
122 call self%set_config(read_nml=.true.)
123 else
124 return ! config already set via set_config
125 end if
126 ! get defaults for local variables
127 case = self%case
128 meteo_timestep = self%meteo_timestep
129 meteo_time_ref_endpoint = self%meteo_time_ref_endpoint
130 meteo_expect_pre = self%meteo_expect_pre
131 meteo_expect_temp = self%meteo_expect_temp
132 meteo_expect_pet = self%meteo_expect_pet
133 meteo_expect_tmin = self%meteo_expect_tmin
134 meteo_expect_tmax = self%meteo_expect_tmax
135 meteo_expect_netrad = self%meteo_expect_netrad
136 meteo_expect_absvappress = self%meteo_expect_absvappress
137 meteo_expect_windspeed = self%meteo_expect_windspeed
138 meteo_expect_ssrd = self%meteo_expect_ssrd
139 meteo_expect_strd = self%meteo_expect_strd
140 meteo_expect_tann = self%meteo_expect_tann
141
142 ! open the namelist file
143 call open_nml(file_namelist, unamelist, quiet=.true.)
144 call position_nml(self%nml_name, unamelist, status=status)
145
146 ! coupling namelist can be missing
147 if (status == 0_i4) then
148 ! read namelist if present
149 read(unamelist, nml=coupling)
150 end if
151
152 ! closing the namelist file
153 call close_nml(unamelist)
154
155 self%case = case
156 self%meteo_timestep = meteo_timestep
157 self%meteo_time_ref_endpoint = meteo_time_ref_endpoint
158 self%meteo_expect_pre = meteo_expect_pre
159 self%meteo_expect_temp = meteo_expect_temp
160 self%meteo_expect_pet = meteo_expect_pet
161 self%meteo_expect_tmin = meteo_expect_tmin
162 self%meteo_expect_tmax = meteo_expect_tmax
163 self%meteo_expect_netrad = meteo_expect_netrad
164 self%meteo_expect_absvappress = meteo_expect_absvappress
165 self%meteo_expect_windspeed = meteo_expect_windspeed
166 self%meteo_expect_ssrd = meteo_expect_ssrd
167 self%meteo_expect_strd = meteo_expect_strd
168 self%meteo_expect_tann = meteo_expect_tann
169
170 end subroutine read_config
171
172 !> \brief set configuration for the \ref couple_cfg_type class
173 subroutine set_config( &
174 self, &
175 case, &
176 meteo_timestep, &
177 meteo_time_ref_endpoint, &
178 meteo_expect_pre, &
179 meteo_expect_temp, &
180 meteo_expect_pet, &
181 meteo_expect_tmin, &
182 meteo_expect_tmax, &
183 meteo_expect_netrad, &
184 meteo_expect_absvappress, &
185 meteo_expect_windspeed, &
186 meteo_expect_ssrd, &
187 meteo_expect_strd, &
188 meteo_expect_tann, &
189 read_nml &
190 )
191 implicit none
192
193 class(couple_cfg_type), intent(inout) :: self
194 integer(i4), intent(in), optional :: case !< coupling case
195 integer(i4), intent(in), optional :: meteo_timestep !< timestep for meteo-data from coupling
196 logical, intent(in), optional :: meteo_time_ref_endpoint !< expect meteo has time reference point at end of time interval
197 logical, intent(in), optional :: meteo_expect_pre !< expect meteo from coupling: [mm] Precipitation
198 logical, intent(in), optional :: meteo_expect_temp !< expect meteo from coupling: [degC] Air temperature
199 logical, intent(in), optional :: meteo_expect_pet !< expect meteo from coupling: [mm TS-1] Potential evapotranspiration
200 logical, intent(in), optional :: meteo_expect_tmin !< expect meteo from coupling: [degC] minimum daily air temperature
201 logical, intent(in), optional :: meteo_expect_tmax !< expect meteo from coupling: [degC] maximum daily air temperature
202 logical, intent(in), optional :: meteo_expect_netrad !< expect meteo from coupling: [W m2] net radiation
203 logical, intent(in), optional :: meteo_expect_absvappress !< expect meteo from coupling: [Pa] absolute vapour pressure
204 logical, intent(in), optional :: meteo_expect_windspeed !< expect meteo from coupling: [m s-1] windspeed
205 logical, intent(in), optional :: meteo_expect_ssrd !< expect meteo from coupling: [W m2] short wave radiation
206 logical, intent(in), optional :: meteo_expect_strd !< expect meteo from coupling: [W m2] long wave radiation
207 logical, intent(in), optional :: meteo_expect_tann !< expect meteo from coupling: [degC] annual mean air temperature
208 logical, intent(in), optional :: read_nml !< whether to read the namelist (or set via routine)
209
210 ! defaults
211 self%case = 0_i4 ! no coupling by default
212 self%meteo_timestep = 0_i4 ! only valid if no meteo expected
213 self%meteo_time_ref_endpoint = .false. ! meteo data usually given at begin of time interval (i.e. 00:00 for current day)
214 self%meteo_expect_pre = .false.
215 self%meteo_expect_temp = .false.
216 self%meteo_expect_pet = .false.
217 self%meteo_expect_tmin = .false.
218 self%meteo_expect_tmax = .false.
219 self%meteo_expect_netrad = .false.
220 self%meteo_expect_absvappress = .false.
221 self%meteo_expect_windspeed = .false.
222 self%meteo_expect_ssrd = .false.
223 self%meteo_expect_strd = .false.
224 self%meteo_expect_tann = .false.
225 ! don't read namelist if configured via this routine (by default)
226 ! if set_config is only used to set defaults, set this to true
227 self%read_nml = .false.
228
229 if (present(case)) self%case = case
230 if (present(meteo_timestep)) self%meteo_timestep = meteo_timestep
231 if (present(meteo_time_ref_endpoint)) self%meteo_time_ref_endpoint = meteo_time_ref_endpoint
232 if (present(meteo_expect_pre)) self%meteo_expect_pre = meteo_expect_pre
233 if (present(meteo_expect_temp)) self%meteo_expect_temp = meteo_expect_temp
234 if (present(meteo_expect_pet)) self%meteo_expect_pet = meteo_expect_pet
235 if (present(meteo_expect_tmin)) self%meteo_expect_tmin = meteo_expect_tmin
236 if (present(meteo_expect_tmax)) self%meteo_expect_tmax = meteo_expect_tmax
237 if (present(meteo_expect_netrad)) self%meteo_expect_netrad = meteo_expect_netrad
238 if (present(meteo_expect_absvappress)) self%meteo_expect_absvappress = meteo_expect_absvappress
239 if (present(meteo_expect_windspeed)) self%meteo_expect_windspeed = meteo_expect_windspeed
240 if (present(meteo_expect_ssrd)) self%meteo_expect_ssrd = meteo_expect_ssrd
241 if (present(meteo_expect_strd)) self%meteo_expect_strd = meteo_expect_strd
242 if (present(meteo_expect_tann)) self%meteo_expect_tann = meteo_expect_tann
243 if (present(read_nml)) self%read_nml = read_nml
244
245 end subroutine set_config
246
247 !> \brief whether any meteo data is expected
248 !> \return True if any meteo data expected, else False
249 logical function any_meteo_expected(self)
250 implicit none
251
252 class(couple_cfg_type), intent(in) :: self
253
255 self%meteo_expect_pre .or. &
256 self%meteo_expect_temp .or. &
257 self%meteo_expect_pet .or. &
258 self%meteo_expect_tmin .or. &
259 self%meteo_expect_tmax .or. &
260 self%meteo_expect_netrad .or. &
261 self%meteo_expect_absvappress .or. &
262 self%meteo_expect_windspeed .or. &
263 self%meteo_expect_ssrd .or. &
264 self%meteo_expect_strd .or. &
265 self%meteo_expect_tann
266
267 end function any_meteo_expected
268
269 !> \brief check configuration
270 subroutine check(self, domainMeta, optimize)
271 use mo_message, only : error_message
272 use mo_string_utils, only : num2str
273 use mo_common_types, only : domain_meta
274 implicit none
275
276 class(couple_cfg_type), intent(inout) :: self
277 type(domain_meta), intent(in) :: domainMeta !< domain general description
278 logical, intent(in) :: optimize !< Optimization (.true. ) or Evaluation run (.false.)
279
280 if (.not. any(self%case == [0, 1])) &
281 call error_message("Coupling: case needs to be 0 or 1. Got: ", num2str(self%case))
282
283 ! coupling case 1 for single domain coupling
284 if (self%case == 1 .and. domainmeta%nDomains > 1) &
285 call error_message("Coupling: Only one domain allowed when coupling.")
286
287 ! coupling only without internal optimization
288 if (self%case /= 0 .and. optimize) &
289 call error_message("Coupling: no internal optimization allowed when coupling.")
290
291 ! if meteo data is expected, an associated time-step (1 or 24) needs to be given
292 if (self%case /= 0 .and. self%any_meteo_expected() .and. .not. any(self%meteo_timestep == [1, 24])) &
293 call error_message("Coupling: meteo data expected but no valid time-step (1 or 24) given for it.")
294
295 end subroutine check
296
297 !> \brief whether coupling is actived
298 !> \return True if any case > 0, else False
299 logical function active(self)
300 implicit none
301 class(couple_cfg_type), intent(in) :: self
302 active = self%case > 0_i4
303 end function active
304
305end module mo_coupling_type
Provides common types needed by mHM, mRM and/or mpr.
Types to specify the coupling configuration of mHM.
subroutine set_config(self, case, meteo_timestep, meteo_time_ref_endpoint, meteo_expect_pre, meteo_expect_temp, meteo_expect_pet, meteo_expect_tmin, meteo_expect_tmax, meteo_expect_netrad, meteo_expect_absvappress, meteo_expect_windspeed, meteo_expect_ssrd, meteo_expect_strd, meteo_expect_tann, read_nml)
set configuration for the couple_cfg_type class
logical function active(self)
whether coupling is actived
logical function any_meteo_expected(self)
whether any meteo data is expected
subroutine read_config(self, file_namelist, unamelist)
read configuration for the couple_cfg_type class from the mhm namelist
subroutine clean_up(self)
clean up
subroutine check(self, domainmeta, optimize)
check configuration
DOMAIN general description.
This is a container to hold all coupling configurations for mHM.