MODFLOW 6  version 6.7.0.dev3
USGS Modular Hydrologic Model
MemoryManagerExt.f90
Go to the documentation of this file.
2 
3  use kindmodule, only: dp, lgp, i4b, i8b
4  use simmodule, only: store_error
8 
9  implicit none
10  private
11  public :: mem_set_value
12  public :: memorystore_remove
13 
14  interface mem_set_value
15  module procedure mem_set_value_logical, mem_set_value_int, &
23  end interface mem_set_value
24 
25 contains
26 
27  subroutine memorystore_remove(component, subcomponent, context)
29  use constantsmodule, only: lenmempath
30  character(len=*), intent(in) :: component !< name of the solution, model, or exchange
31  character(len=*), intent(in), optional :: subcomponent !< name of the package (optional)
32  character(len=*), intent(in), optional :: context !< name of the context (optional)
33  character(len=LENMEMPATH) :: memory_path !< the memory path
34  type(memorytype), pointer :: mt
35  type(memorycontaineriteratortype), allocatable :: itr
36  logical(LGP) :: removed
37 
38  memory_path = create_mem_path(component, subcomponent, context)
39  removed = .true. !< initialize the loop
40 
41  do while (removed)
42  removed = .false.
43  itr = memorystore%iterator()
44  do while (itr%has_next())
45  call itr%next()
46  mt => itr%value()
47  if (mt%path == memory_path .and. mt%mt_associated()) then
48  call mt%mt_deallocate()
49  removed = .true.
50  deallocate (itr)
51  exit
52  end if
53  end do
54  end do
55  end subroutine memorystore_remove
56 
57  !> @brief Set pointer to value of memory list logical variable
58  !<
59  subroutine mem_set_value_logical(p_mem, varname, memory_path, found)
60  logical(LGP), pointer, intent(inout) :: p_mem !< pointer to logical scalar
61  character(len=*), intent(in) :: varname !< variable name
62  character(len=*), intent(in) :: memory_path !< path where variable is stored
63  logical(LGP), intent(inout) :: found
64  type(memorytype), pointer :: mt
65  logical(LGP) :: checkfail = .false.
66 
67  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
68  if (.not. found) return
69  if (mt%memtype(1:index(mt%memtype, ' ')) == 'INTEGER') then
70  if (mt%intsclr == 0) then
71  p_mem = .false.
72  else
73  p_mem = .true.
74  end if
75  end if
76  end subroutine mem_set_value_logical
77 
78  !> @brief Set pointer to value of memory list int variable
79  !<
80  subroutine mem_set_value_int(p_mem, varname, memory_path, found)
81  integer(I4B), pointer, intent(inout) :: p_mem !< pointer to int scalar
82  character(len=*), intent(in) :: varname !< variable name
83  character(len=*), intent(in) :: memory_path !< path where variable is stored
84  logical(LGP), intent(inout) :: found
85  type(memorytype), pointer :: mt
86  logical(LGP) :: checkfail = .false.
87 
88  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
89  if (.not. found) return
90  if (mt%memtype(1:index(mt%memtype, ' ')) == 'INTEGER') then
91  p_mem = mt%intsclr
92  end if
93  end subroutine mem_set_value_int
94 
95  subroutine mem_set_value_int_setval(p_mem, varname, memory_path, setval, found)
96  integer(I4B), pointer, intent(inout) :: p_mem !< pointer to int scalar
97  character(len=*), intent(in) :: varname !< variable name
98  character(len=*), intent(in) :: memory_path !< path where variable is stored
99  integer(I4B), intent(in) :: setval !< set p_mem to setval if varname found
100  logical(LGP), intent(inout) :: found
101  type(memorytype), pointer :: mt
102  logical(LGP) :: checkfail = .false.
103 
104  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
105  if (.not. found) return
106 
107  p_mem = setval
108 
109  end subroutine mem_set_value_int_setval
110 
111  subroutine mem_set_value_str_mapped_int(p_mem, varname, memory_path, str_list, &
112  found)
113  integer(I4B), pointer, intent(inout) :: p_mem !< pointer to int scalar
114  character(len=*), intent(in) :: varname !< variable name
115  character(len=*), intent(in) :: memory_path !< path where variable is stored
116  character(len=*), dimension(:), intent(in) :: str_list
117  logical(LGP), intent(inout) :: found
118  type(memorytype), pointer :: mt
119  logical(LGP) :: checkfail = .false.
120  integer(I4B) :: i
121 
122  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
123  if (.not. found) return
124  if (mt%memtype(1:index(mt%memtype, ' ')) == 'STRING') then
125  do i = 1, size(str_list)
126  if (mt%strsclr == str_list(i)) then
127  p_mem = i
128  end if
129  end do
130  end if
131  end subroutine mem_set_value_str_mapped_int
132 
133  !> @brief Set pointer to value of memory list 1d logical array variable
134  !<
135  subroutine mem_set_value_logical1d(p_mem, varname, memory_path, found)
136  logical(LGP), dimension(:), pointer, contiguous, intent(inout) :: p_mem !< pointer to 1d logical array
137  character(len=*), intent(in) :: varname !< variable name
138  character(len=*), intent(in) :: memory_path !< path where variable is stored
139  logical(LGP), intent(inout) :: found
140  type(memorytype), pointer :: mt
141  logical(LGP) :: checkfail = .false.
142  integer(I4B) :: n
143 
144  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
145  if (.not. found) return
146  if (mt%memtype(1:index(mt%memtype, ' ')) == 'LOGICAL') then
147  if (size(mt%alogical1d) /= size(p_mem)) then
148  call store_error('mem_set_value() size mismatch logical1d, varname='//&
149  &trim(varname), terminate=.true.)
150  end if
151  do n = 1, size(mt%alogical1d)
152  p_mem(n) = mt%alogical1d(n)
153  end do
154  end if
155  end subroutine mem_set_value_logical1d
156 
157  !> @brief Set pointer to value of memory list 1d logical array variable with mapping
158  !<
159  subroutine mem_set_value_logical1d_mapped(p_mem, varname, memory_path, map, &
160  found)
161  logical(LGP), dimension(:), pointer, contiguous, intent(inout) :: p_mem !< pointer to 1d logical array
162  character(len=*), intent(in) :: varname !< variable name
163  character(len=*), intent(in) :: memory_path !< path where variable is stored
164  integer(I4B), dimension(:), pointer, contiguous, intent(in) :: map !< pointer to 1d int mapping array
165  logical(LGP), intent(inout) :: found
166  type(memorytype), pointer :: mt
167  logical(LGP) :: checkfail = .false.
168  integer(I4B) :: n
169 
170  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
171  if (.not. found) return
172  if (mt%memtype(1:index(mt%memtype, ' ')) == 'LOGICAL') then
173  if (associated(map)) then
174  do n = 1, size(p_mem)
175  p_mem(n) = mt%alogical1d(map(n))
176  end do
177  else
178  if (size(mt%alogical1d) /= size(p_mem)) then
179  call store_error('mem_set_value() size mismatch logical1d, varname='//&
180  &trim(varname), terminate=.true.)
181  end if
182  do n = 1, size(mt%alogical1d)
183  p_mem(n) = mt%alogical1d(n)
184  end do
185  end if
186  end if
187  end subroutine mem_set_value_logical1d_mapped
188 
189  !> @brief Set pointer to value of memory list 1d int array variable
190  !<
191  subroutine mem_set_value_int1d(p_mem, varname, memory_path, found)
192  integer(I4B), dimension(:), pointer, contiguous, intent(inout) :: p_mem !< pointer to 1d int array
193  character(len=*), intent(in) :: varname !< variable name
194  character(len=*), intent(in) :: memory_path !< path where variable is stored
195  logical(LGP), intent(inout) :: found
196  type(memorytype), pointer :: mt
197  logical(LGP) :: checkfail = .false.
198  integer(I4B) :: n
199 
200  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
201  if (.not. found) return
202  if (mt%memtype(1:index(mt%memtype, ' ')) == 'INTEGER') then
203  if (size(mt%aint1d) /= size(p_mem)) then
204  call store_error('mem_set_value() size mismatch int1d, varname='//&
205  &trim(varname), terminate=.true.)
206  end if
207  do n = 1, size(mt%aint1d)
208  p_mem(n) = mt%aint1d(n)
209  end do
210  end if
211  end subroutine mem_set_value_int1d
212 
213  !> @brief Set pointer to value of memory list 1d int array variable with mapping
214  !<
215  subroutine mem_set_value_int1d_mapped(p_mem, varname, memory_path, map, &
216  found)
217  integer(I4B), dimension(:), pointer, contiguous, intent(inout) :: p_mem !< pointer to 1d int array
218  character(len=*), intent(in) :: varname !< variable name
219  character(len=*), intent(in) :: memory_path !< path where variable is stored
220  integer(I4B), dimension(:), pointer, contiguous, intent(in) :: map !< pointer to 1d int mapping array
221  logical(LGP), intent(inout) :: found
222  type(memorytype), pointer :: mt
223  logical(LGP) :: checkfail = .false.
224  integer(I4B) :: n
225 
226  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
227  if (.not. found) return
228  if (mt%memtype(1:index(mt%memtype, ' ')) == 'INTEGER') then
229  if (associated(map)) then
230  do n = 1, size(p_mem)
231  p_mem(n) = mt%aint1d(map(n))
232  end do
233  else
234  if (size(mt%aint1d) /= size(p_mem)) then
235  call store_error('mem_set_value() size mismatch int1d, varname='//&
236  &trim(varname), terminate=.true.)
237  end if
238  do n = 1, size(mt%aint1d)
239  p_mem(n) = mt%aint1d(n)
240  end do
241  end if
242  end if
243  end subroutine mem_set_value_int1d_mapped
244 
245  !> @brief Set pointer to value of memory list 2d int array variable
246  !<
247  subroutine mem_set_value_int2d(p_mem, varname, memory_path, found)
248  integer(I4B), dimension(:, :), pointer, contiguous, intent(inout) :: p_mem !< pointer to 2d int array
249  character(len=*), intent(in) :: varname !< variable name
250  character(len=*), intent(in) :: memory_path !< path where variable is stored
251  logical(LGP), intent(inout) :: found
252  type(memorytype), pointer :: mt
253  logical(LGP) :: checkfail = .false.
254  integer(I4B) :: i, j
255 
256  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
257  if (.not. found) return
258  if (mt%memtype(1:index(mt%memtype, ' ')) == 'INTEGER') then
259  if (size(mt%aint2d, dim=1) /= size(p_mem, dim=1) .or. &
260  size(mt%aint2d, dim=2) /= size(p_mem, dim=2)) then
261  call store_error('mem_set_value() size mismatch int2d, varname='//&
262  &trim(varname), terminate=.true.)
263  end if
264  do j = 1, size(mt%aint2d, dim=2)
265  do i = 1, size(mt%aint2d, dim=1)
266  p_mem(i, j) = mt%aint2d(i, j)
267  end do
268  end do
269  end if
270  end subroutine mem_set_value_int2d
271 
272  !> @brief Set pointer to value of memory list 3d int array variable
273  !<
274  subroutine mem_set_value_int3d(p_mem, varname, memory_path, found)
275  integer(I4B), dimension(:, :, :), pointer, contiguous, intent(inout) :: p_mem !< pointer to 3d int array
276  character(len=*), intent(in) :: varname !< variable name
277  character(len=*), intent(in) :: memory_path !< path where variable is stored
278  logical(LGP), intent(inout) :: found
279  type(memorytype), pointer :: mt
280  logical(LGP) :: checkfail = .false.
281  integer(I4B) :: i, j, k
282 
283  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
284  if (.not. found) return
285  if (mt%memtype(1:index(mt%memtype, ' ')) == 'INTEGER') then
286  if (size(mt%aint3d, dim=1) /= size(p_mem, dim=1) .or. &
287  size(mt%aint3d, dim=2) /= size(p_mem, dim=2) .or. &
288  size(mt%aint3d, dim=3) /= size(p_mem, dim=3)) then
289  call store_error('mem_set_value() size mismatch int3d, varname='//&
290  &trim(varname), terminate=.true.)
291  end if
292  do k = 1, size(mt%aint3d, dim=3)
293  do j = 1, size(mt%aint3d, dim=2)
294  do i = 1, size(mt%aint3d, dim=1)
295  p_mem(i, j, k) = mt%aint3d(i, j, k)
296  end do
297  end do
298  end do
299  end if
300  end subroutine mem_set_value_int3d
301 
302  !> @brief Set pointer to value of memory list double variable
303  !<
304  subroutine mem_set_value_dbl(p_mem, varname, memory_path, found)
305  real(DP), pointer, intent(inout) :: p_mem !< pointer to dbl scalar
306  character(len=*), intent(in) :: varname !< variable name
307  character(len=*), intent(in) :: memory_path !< path where variable is stored
308  logical(LGP), intent(inout) :: found
309  type(memorytype), pointer :: mt
310  logical(LGP) :: checkfail = .false.
311 
312  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
313  if (.not. found) return
314  if (mt%memtype(1:index(mt%memtype, ' ')) == 'DOUBLE') then
315  p_mem = mt%dblsclr
316  end if
317  end subroutine mem_set_value_dbl
318 
319  !> @brief Set pointer to value of memory list 1d dbl array variable
320  !<
321  subroutine mem_set_value_dbl1d(p_mem, varname, memory_path, found)
322  real(DP), dimension(:), pointer, contiguous, intent(inout) :: p_mem !< pointer to 1d dbl array
323  character(len=*), intent(in) :: varname !< variable name
324  character(len=*), intent(in) :: memory_path !< path where variable is stored
325  logical(LGP), intent(inout) :: found
326  type(memorytype), pointer :: mt
327  logical(LGP) :: checkfail = .false.
328  integer(I4B) :: n
329 
330  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
331  if (.not. found) return
332  if (mt%memtype(1:index(mt%memtype, ' ')) == 'DOUBLE') then
333  if (size(mt%adbl1d) /= size(p_mem)) then
334  call store_error('mem_set_value() size mismatch dbl1d, varname='//&
335  &trim(varname), terminate=.true.)
336  end if
337  do n = 1, size(mt%adbl1d)
338  p_mem(n) = mt%adbl1d(n)
339  end do
340  end if
341  end subroutine mem_set_value_dbl1d
342 
343  !> @brief Set pointer to value of memory list 1d dbl array variable with mapping
344  !<
345  subroutine mem_set_value_dbl1d_mapped(p_mem, varname, memory_path, map, &
346  found)
347  real(DP), dimension(:), pointer, contiguous, intent(inout) :: p_mem !< pointer to 1d dbl array
348  character(len=*), intent(in) :: varname !< variable name
349  character(len=*), intent(in) :: memory_path !< path where variable is stored
350  integer(I4B), dimension(:), pointer, contiguous, intent(in) :: map !< pointer to 1d int mapping array
351  logical(LGP), intent(inout) :: found
352  type(memorytype), pointer :: mt
353  logical(LGP) :: checkfail = .false.
354  integer(I4B) :: n
355 
356  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
357  if (.not. found) return
358  if (mt%memtype(1:index(mt%memtype, ' ')) == 'DOUBLE') then
359  if (associated(map)) then
360  do n = 1, size(p_mem)
361  p_mem(n) = mt%adbl1d(map(n))
362  end do
363  else
364  if (size(mt%adbl1d) /= size(p_mem)) then
365  call store_error('mem_set_value() size mismatch dbl1d, varname='//&
366  &trim(varname), terminate=.true.)
367  end if
368  do n = 1, size(mt%adbl1d)
369  p_mem(n) = mt%adbl1d(n)
370  end do
371  end if
372  end if
373  end subroutine mem_set_value_dbl1d_mapped
374 
375  !> @brief Set pointer to value of memory list 2d dbl array variable
376  !<
377  subroutine mem_set_value_dbl2d(p_mem, varname, memory_path, found)
378  real(DP), dimension(:, :), pointer, contiguous, intent(inout) :: p_mem !< pointer to 2d dbl array
379  character(len=*), intent(in) :: varname !< variable name
380  character(len=*), intent(in) :: memory_path !< path where variable is stored
381  logical(LGP), intent(inout) :: found
382  type(memorytype), pointer :: mt
383  logical(LGP) :: checkfail = .false.
384  integer(I4B) :: i, j
385 
386  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
387  if (.not. found) return
388  if (mt%memtype(1:index(mt%memtype, ' ')) == 'DOUBLE') then
389  if (size(mt%adbl2d, dim=1) /= size(p_mem, dim=1) .or. &
390  size(mt%adbl2d, dim=2) /= size(p_mem, dim=2)) then
391  call store_error('mem_set_value() size mismatch dbl2d, varname='//&
392  &trim(varname), terminate=.true.)
393  end if
394  do j = 1, size(mt%adbl2d, dim=2)
395  do i = 1, size(mt%adbl2d, dim=1)
396  p_mem(i, j) = mt%adbl2d(i, j)
397  end do
398  end do
399  end if
400  end subroutine mem_set_value_dbl2d
401 
402  !> @brief Set pointer to value of memory list 3d dbl array variable
403  !<
404  subroutine mem_set_value_dbl3d(p_mem, varname, memory_path, found)
405  real(DP), dimension(:, :, :), pointer, contiguous, intent(inout) :: p_mem !< pointer to 3d dbl array
406  character(len=*), intent(in) :: varname !< variable name
407  character(len=*), intent(in) :: memory_path !< path where variable is stored
408  logical(LGP), intent(inout) :: found
409  type(memorytype), pointer :: mt
410  logical(LGP) :: checkfail = .false.
411  integer(I4B) :: i, j, k
412 
413  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
414  if (.not. found) return
415  if (mt%memtype(1:index(mt%memtype, ' ')) == 'DOUBLE') then
416  if (size(mt%adbl3d, dim=1) /= size(p_mem, dim=1) .or. &
417  size(mt%adbl3d, dim=2) /= size(p_mem, dim=2) .or. &
418  size(mt%adbl3d, dim=3) /= size(p_mem, dim=3)) then
419  call store_error('mem_set_value() size mismatch dbl3d, varname='//&
420  &trim(varname), terminate=.true.)
421  end if
422  do k = 1, size(mt%adbl3d, dim=3)
423  do j = 1, size(mt%adbl3d, dim=2)
424  do i = 1, size(mt%adbl3d, dim=1)
425  p_mem(i, j, k) = mt%adbl3d(i, j, k)
426  end do
427  end do
428  end do
429  end if
430  end subroutine mem_set_value_dbl3d
431 
432  subroutine mem_set_value_str(p_mem, varname, memory_path, found)
433  character(len=*), intent(inout) :: p_mem !< pointer to str scalar
434  character(len=*), intent(in) :: varname !< variable name
435  character(len=*), intent(in) :: memory_path !< path where variable is stored
436  logical(LGP), intent(inout) :: found
437  type(memorytype), pointer :: mt
438  logical(LGP) :: checkfail = .false.
439 
440  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
441  if (.not. found) return
442  if (mt%memtype(1:index(mt%memtype, ' ')) == 'STRING') then
443  p_mem = mt%strsclr
444  end if
445  end subroutine mem_set_value_str
446 
447  subroutine mem_set_value_charstr1d(p_mem, varname, memory_path, found)
449  type(characterstringtype), dimension(:), &
450  pointer, contiguous, intent(inout) :: p_mem !< pointer to charstr 1d array
451  character(len=*), intent(in) :: varname !< variable name
452  character(len=*), intent(in) :: memory_path !< path where variable is stored
453  logical(LGP), intent(inout) :: found
454  type(memorytype), pointer :: mt
455  logical(LGP) :: checkfail = .false.
456  integer(I4B) :: n
457 
458  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
459  if (.not. found) return
460  if (mt%memtype(1:index(mt%memtype, ' ')) == 'STRING') then
461  do n = 1, size(mt%acharstr1d)
462  p_mem(n) = mt%acharstr1d(n)
463  end do
464  end if
465  end subroutine mem_set_value_charstr1d
466 
467 end module memorymanagerextmodule
This module contains simulation constants.
Definition: Constants.f90:9
integer(i4b), parameter lenmempath
maximum length of the memory path
Definition: Constants.f90:27
This module defines variable data types.
Definition: kind.f90:8
character(len=lenmempath) function create_mem_path(component, subcomponent, context)
returns the path to the memory object
subroutine mem_set_value_logical(p_mem, varname, memory_path, found)
Set pointer to value of memory list logical variable.
subroutine, public memorystore_remove(component, subcomponent, context)
subroutine mem_set_value_dbl3d(p_mem, varname, memory_path, found)
Set pointer to value of memory list 3d dbl array variable.
subroutine mem_set_value_dbl(p_mem, varname, memory_path, found)
Set pointer to value of memory list double variable.
subroutine mem_set_value_int(p_mem, varname, memory_path, found)
Set pointer to value of memory list int variable.
subroutine mem_set_value_charstr1d(p_mem, varname, memory_path, found)
subroutine mem_set_value_str_mapped_int(p_mem, varname, memory_path, str_list, found)
subroutine mem_set_value_int_setval(p_mem, varname, memory_path, setval, found)
subroutine mem_set_value_int2d(p_mem, varname, memory_path, found)
Set pointer to value of memory list 2d int array variable.
subroutine mem_set_value_int3d(p_mem, varname, memory_path, found)
Set pointer to value of memory list 3d int array variable.
subroutine mem_set_value_str(p_mem, varname, memory_path, found)
subroutine mem_set_value_int1d_mapped(p_mem, varname, memory_path, map, found)
Set pointer to value of memory list 1d int array variable with mapping.
subroutine mem_set_value_dbl2d(p_mem, varname, memory_path, found)
Set pointer to value of memory list 2d dbl array variable.
subroutine mem_set_value_dbl1d(p_mem, varname, memory_path, found)
Set pointer to value of memory list 1d dbl array variable.
subroutine mem_set_value_logical1d_mapped(p_mem, varname, memory_path, map, found)
Set pointer to value of memory list 1d logical array variable with mapping.
subroutine mem_set_value_logical1d(p_mem, varname, memory_path, found)
Set pointer to value of memory list 1d logical array variable.
subroutine mem_set_value_int1d(p_mem, varname, memory_path, found)
Set pointer to value of memory list 1d int array variable.
subroutine mem_set_value_dbl1d_mapped(p_mem, varname, memory_path, map, found)
Set pointer to value of memory list 1d dbl array variable with mapping.
type(memorystoretype), public memorystore
subroutine, public get_from_memorystore(name, mem_path, mt, found, check)
@ brief Get a memory type entry from the memory list
This module contains simulation methods.
Definition: Sim.f90:10
subroutine, public store_error(msg, terminate)
Store an error message.
Definition: Sim.f90:92
This class is used to store a single deferred-length character string. It was designed to work in an ...
Definition: CharString.f90:23
An iterator used to iterate through a MemoryContainer.