5.13.2-dev0
mHM
The mesoscale Hydrological Model
Loading...
Searching...
No Matches
mo_common_mHM_mRM_read_config.f90
Go to the documentation of this file.
1!> \file mo_common_mHM_mRM_read_config.f90
2!> \brief \copybrief mo_common_mhm_mrm_read_config
3!> \details \copydetails mo_common_mhm_mrm_read_config
4
5!> \brief Reading of main model configurations.
6!> \details This routine reads the configurations of common program parts
7!> \authors Matthias Zink
8!> \date Dec 2012
9!> \copyright Copyright 2005-\today, the mHM Developers, Luis Samaniego, Sabine Attinger: All rights reserved.
10!! mHM is released under the LGPLv3+ license \license_note
11!> \ingroup f_common
13
14 use mo_kind, only : i4, dp
15 use mo_message, only : message, error_message
16
17 IMPLICIT NONE
18
19 PRIVATE
20
22
23CONTAINS
24
25
26 !> \brief Read main configurations for common parts
27 !> \changelog
28 !! - Robert Schweppe Dec 2017
29 !! - based on mhm_read_config
30 !! - Stephan Thober Jan 2022
31 !! - added nTStepForcingDay
32 !> \authors Matthias Zink
33 !> \date Dec 2012
34 subroutine common_mhm_mrm_read_config(file_namelist, unamelist)
35
45 use mo_common_types, only: period
47 use mo_julian, only : caldat, julday
48 use mo_nml, only : close_nml, open_nml, position_nml
49 use mo_string_utils, only : num2str
50
51 implicit none
52
53 character(*), intent(in) :: file_namelist !< namelist file name
54 integer, intent(in) :: unamelist !< unit to open namelist file
55
56 integer(i4) :: jday
57
58 integer(i4) :: domainid, idomain
59
60 integer(i4), dimension(maxNoDomains) :: warming_days
61
62 type(period), dimension(maxNoDomains) :: eval_per
63
64 real(dp), dimension(maxNoDomains) :: resolution_routing
65
66 character(256), dimension(maxNoDomains) :: mhm_file_restartin
67 character(256), dimension(maxNoDomains) :: mrm_file_restartin
68
69
70 ! namelist spatial & temporal resolution, otmization information
71 namelist /mainconfig_mhm_mrm/ timestep, resolution_routing, optimize, &
74 mhm_file_restartin, mrm_file_restartin
75 ! namelist for optimization settings
76 namelist /optimization/ niterations, seed, dds_r, sa_temp, sce_ngs, &
78 ! namelist for time settings
79 namelist /time_periods/ warming_days, eval_per
80
81 ! set default values for optional arguments
85
86 !===============================================================
87 ! Read namelist main directories
88 !===============================================================
89 call open_nml(file_namelist, unamelist, quiet = .true.)
90
91 !===============================================================
92 ! Read namelist specifying the model configuration
93 !===============================================================
94 call position_nml('mainconfig_mhm_mrm', unamelist)
95 read(unamelist, nml = mainconfig_mhm_mrm)
96 ! consistency between read_restart and mrm_read_river_network
97 if (read_restart) then
98 if (.not. mrm_read_river_network) then
99 call message('***WARNING: mrm_read_river_network is set to .true. because read_restart is .true.')
100 end if
102 end if
103
104 allocate(resolutionrouting(domainmeta%nDomains))
105 allocate(mhmfilerestartin(domainmeta%nDomains))
106 allocate(mrmfilerestartin(domainmeta%nDomains))
107 do idomain = 1, domainmeta%nDomains
108 domainid = domainmeta%indices(idomain)
109 mhmfilerestartin(idomain) = mhm_file_restartin(domainid)
110 mrmfilerestartin(idomain) = mrm_file_restartin(domainid)
111 resolutionrouting(idomain) = resolution_routing(domainid)
112 end do
113
114 ! check for optimize and read restart
115 if ((read_restart) .and. (optimize)) then
116 call message()
117 call error_message('***ERROR: cannot read states from restart file when optimizing')
118 end if
119
120 do idomain = 1, domainmeta%nDomains
121 domainid = domainmeta%indices(idomain)
122 if (processmatrix(8, 1) > 0 .and. domainmeta%optidata(idomain) > 1 .and. optimize) then
123 domainmeta%doRouting(idomain) = .false.
124 call message('Warning: although defined in namelist, routing is switched off for domain', trim(num2str(domainid)))
125 call message(' since the calibration of Q is not possible with the chosen opti input')
126 end if
127 end do
128
129 !===============================================================
130 ! INIT !!! (merged from mo_startup and mo_mrm_read_config)
131 !===============================================================
132 ! transformation of time units & constants
133 if (mod(24, timestep) > 0) then
134 call error_message('mo_startup: timeStep must be a divisor of 24: ', num2str(timestep))
135 end if
136 ntstepday = 24_i4 / timestep ! # of time steps per day
137
138 ! allocate time periods
139 allocate(simper(domainmeta%nDomains))
140 allocate(evalper(domainmeta%nDomains))
141 allocate(warmingdays(domainmeta%nDomains))
142 allocate(warmper(domainmeta%nDomains))
143
144 !===============================================================
145 ! read simulation time periods incl. warming days
146 !===============================================================
147 call position_nml('time_periods', unamelist)
148 read(unamelist, nml = time_periods)
149 do idomain = 1, domainmeta%nDomains
150 domainid = domainmeta%indices(idomain)
151 warmingdays(idomain) = warming_days(domainid)
152 ! this will be a procedure subroutine
153 ! therefore inout first, in second
154 call period_copy_period_data(evalper(idomain), eval_per(domainid))
155 end do
156 ! evalPer = eval_Per(1 : domainMeta%nDomains)
157
158 !===============================================================
159 ! determine simulation time period incl. warming days for each
160 ! domain
161 !===============================================================
162 do idomain = 1, domainmeta%nDomains
163 ! julian days for evaluation period
164 jday = julday(dd = evalper(idomain)%dStart, mm = evalper(idomain)%mStart, yy = evalper(idomain)%yStart)
165 evalper(idomain)%julStart = jday
166
167 jday = julday(dd = evalper(idomain)%dEnd, mm = evalper(idomain)%mEnd, yy = evalper(idomain)%yEnd)
168 evalper(idomain)%julEnd = jday
169
170 ! determine warming period
171 warmper(idomain)%julStart = evalper(idomain)%julStart - warmingdays(idomain)
172 warmper(idomain)%julEnd = evalper(idomain)%julStart - 1
173
174 call caldat(warmper(idomain)%julStart, dd = warmper(idomain)%dStart, mm = warmper(idomain)%mStart, &
175 yy = warmper(idomain)%yStart)
176 call caldat(warmper(idomain)%julEnd, dd = warmper(idomain)%dEnd, mm = warmper(idomain)%mEnd, &
177 yy = warmper(idomain)%yEnd)
178
179 ! simulation Period = warming Period + evaluation Period
180 simper(idomain)%dStart = warmper(idomain)%dStart
181 simper(idomain)%mStart = warmper(idomain)%mStart
182 simper(idomain)%yStart = warmper(idomain)%yStart
183 simper(idomain)%julStart = warmper(idomain)%julStart
184 simper(idomain)%dEnd = evalper(idomain)%dEnd
185 simper(idomain)%mEnd = evalper(idomain)%mEnd
186 simper(idomain)%yEnd = evalper(idomain)%yEnd
187 simper(idomain)%julEnd = evalper(idomain)%julEnd
188 end do
189
191
192 !===============================================================
193 ! Settings for Optimization
194 !===============================================================
195 ! namelist for Optimization settings
196 call position_nml('Optimization', unamelist)
197 read(unamelist, nml = optimization)
198 ! checking of settings and default value initialization moved to new subroutine
199 ! because global_parameters need to be set, which is not the case right now
200 call close_nml(unamelist)
201
202 end subroutine common_mhm_mrm_read_config
203
204
205 !> \brief check optimization settings
206 !> \authors Robert Schweppe
207 !> \date Jun 2018
209
212
213 implicit none
214
215 integer(i4) :: n_true_pars
216
217
218 ! check and set default values
219 if (niterations .le. 0_i4) then
220 call error_message('Number of iterations for Optimization (nIterations) must be greater than zero')
221 end if
222 if (dds_r .lt. 0.0_dp .or. dds_r .gt. 1.0_dp) then
223 call error_message('dds_r must be between 0.0 and 1.0')
224 end if
225 if (sce_ngs .lt. 1_i4) then
226 call error_message('number of complexes in SCE (sce_ngs) must be at least 1')
227 end if
228 ! number of points in each complex: default = 2n+1
229 if (sce_npg .lt. 0_i4) then
230 n_true_pars = count(nint(global_parameters(:, 4)) .eq. 1)
231 sce_npg = 2 * n_true_pars + 1_i4
232 end if
233 ! number of points in each sub-complex: default = n+1
234 if (sce_nps .lt. 0_i4) then
235 n_true_pars = count(nint(global_parameters(:, 4)) .eq. 1)
236 sce_nps = n_true_pars + 1_i4
237 end if
238 if (sce_npg .lt. sce_nps) then
239 call error_message('number of points per complex (sce_npg) must be greater or', raise=.false.)
240 call error_message('equal number of points per sub-complex (sce_nps)')
241 end if
242
243 end subroutine check_optimization_settings
244
245
246 !> \brief check resolution
247 !> \authors Robert Schweppe
248 !> \date Jun 2018
249 subroutine common_check_resolution(do_message, allow_subgrid_routing)
250
253 use mo_string_utils, only : num2str
254
255 implicit none
256
257 logical, intent(in) :: do_message !< flag to print messages
258 logical, intent(in) :: allow_subgrid_routing !< flag to allow subgrid routing
259
260 integer(i4) :: idomain, domainid
261
262 ! conversion factor L11 to L1
263 real(dp) :: cellfactorrbyh
264
265
266 !===============================================================
267 ! check matching of resolutions: hydrology, forcing and routing
268 !===============================================================
269 do idomain = 1, domainmeta%nDomains
270 domainid = domainmeta%indices(idomain)
271 cellfactorrbyh = resolutionrouting(idomain) / resolutionhydrology(idomain)
272 if (do_message) then
273 call message()
274 call message('domain ', trim(adjustl(num2str(domainid))), ': ')
275 call message('resolution Hydrology (domain ', trim(adjustl(num2str(domainid))), ') = ', &
276 trim(adjustl(num2str(resolutionhydrology(idomain)))))
277 call message('resolution Routing (domain ', trim(adjustl(num2str(domainid))), ') = ', &
278 trim(adjustl(num2str(resolutionrouting(idomain)))))
279 end if
280 !
281 if(nint(cellfactorrbyh * 100.0_dp) .eq. 100) then
282 if (do_message) then
283 call message()
284 call message('Resolution of routing and hydrological modeling are equal!')
285 end if
286
287 else if ((nint(cellfactorrbyh * 100.0_dp) .gt. 100) .and. .not.allow_subgrid_routing) then
288 if(nint(mod(cellfactorrbyh, 2.0_dp) * 100.0_dp) .ne. 0) then
289 call error_message('***ERROR: Resolution of routing is not a multiple of hydrological model resolution!', raise=.false.)
290 call error_message(' FILE: mhm.nml, namelist: mainconfig, variable: resolutionRouting')
291 end if
292 !
293 if (do_message) then
294 call message()
295 call message('Resolution of routing is bigger than hydrological model resolution by ', &
296 trim(adjustl(num2str(nint(cellfactorrbyh)))), ' times !')
297 end if
298 end if
299 !
300 end do
301
302 end subroutine common_check_resolution
303
304
305 ! ToDo: make this a procedure of period
306 !> \brief copy period data
307 subroutine period_copy_period_data(toPeriod, fromPeriod)
308 use mo_common_types, only: period
309 type(period), intent(inout) :: toPeriod !< copy to this period
310 type(period), intent(in) :: fromPeriod !< copy from this period
311
312 toperiod%dStart = fromperiod%dStart ! first day
313 toperiod%mStart = fromperiod%mStart ! first month
314 toperiod%yStart = fromperiod%yStart ! first year
315 toperiod%dEnd = fromperiod%dEnd ! last day
316 toperiod%mEnd = fromperiod%mEnd ! last month
317 toperiod%yEnd = fromperiod%yEnd ! last year
318 toperiod%julStart = 0 ! first julian day
319 toperiod%julEnd = 0 ! last julian day
320 toperiod%nObs = 0 ! total number of observations
321
322 end subroutine period_copy_period_data
323
Provides constants commonly used by mHM, mRM and MPR.
integer(i4), parameter, public maxnodomains
integer(i4), parameter, public nodata_i4
Reading of main model configurations.
subroutine period_copy_period_data(toperiod, fromperiod)
copy period data
subroutine, public common_check_resolution(do_message, allow_subgrid_routing)
check resolution
subroutine, public common_mhm_mrm_read_config(file_namelist, unamelist)
Read main configurations for common parts.
subroutine, public check_optimization_settings
check optimization settings
Provides structures needed by mHM, mRM and/or mpr.
character(256), dimension(:), allocatable, public mrmfilerestartin
logical, public restart_reset_fluxes_states
flag to reset fluxes and states read from restart to default values
type(period), dimension(:), allocatable, public warmper
character(256), dimension(:), allocatable, public mhmfilerestartin
real(dp), dimension(:), allocatable, public resolutionrouting
real(dp), dimension(nerror_model), public mcmc_error_params
integer(i4), dimension(:), allocatable, public warmingdays
integer(i4), dimension(:, :), allocatable, public lcyearid
type(period), dimension(:), allocatable, public simper
type(period), dimension(:), allocatable, public evalper
Reading of main model configurations.
subroutine, public set_land_cover_scenes_id(sim_per, lcyear_id)
Set land cover scenes IDs.
Provides common types needed by mHM, mRM and/or mpr.
Provides structures needed by mHM, mRM and/or mpr.
real(dp), dimension(:), allocatable, public resolutionhydrology
real(dp), dimension(:, :), allocatable, target, public global_parameters
character(256), dimension(:), allocatable, public lcfilename
type(domain_meta), public domainmeta
integer(i4), dimension(nprocesses, 3), public processmatrix