MODFLOW 6  version 6.6.0.dev0
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, &
22  end interface mem_set_value
23 
24 contains
25 
26  subroutine memorystore_remove(component, subcomponent, context)
28  use constantsmodule, only: lenmempath
29  character(len=*), intent(in) :: component !< name of the solution, model, or exchange
30  character(len=*), intent(in), optional :: subcomponent !< name of the package (optional)
31  character(len=*), intent(in), optional :: context !< name of the context (optional)
32  character(len=LENMEMPATH) :: memory_path !< the memory path
33  type(memorytype), pointer :: mt
34  type(memorycontaineriteratortype), allocatable :: itr
35  logical(LGP) :: removed
36 
37  memory_path = create_mem_path(component, subcomponent, context)
38  removed = .true. !< initialize the loop
39 
40  do while (removed)
41  removed = .false.
42  itr = memorystore%iterator()
43  do while (itr%has_next())
44  call itr%next()
45  mt => itr%value()
46  if (mt%path == memory_path .and. mt%mt_associated()) then
47  call mt%mt_deallocate()
48  removed = .true.
49  deallocate (itr)
50  exit
51  end if
52  end do
53  end do
54  end subroutine memorystore_remove
55 
56  !> @brief Set pointer to value of memory list logical variable
57  !<
58  subroutine mem_set_value_logical(p_mem, varname, memory_path, found)
59  logical(LGP), pointer, intent(inout) :: p_mem !< pointer to logical scalar
60  character(len=*), intent(in) :: varname !< variable name
61  character(len=*), intent(in) :: memory_path !< path where variable is stored
62  logical(LGP), intent(inout) :: found
63  type(memorytype), pointer :: mt
64  logical(LGP) :: checkfail = .false.
65 
66  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
67  if (.not. found) return
68  if (mt%memtype(1:index(mt%memtype, ' ')) == 'INTEGER') then
69  if (mt%intsclr == 0) then
70  p_mem = .false.
71  else
72  p_mem = .true.
73  end if
74  end if
75  end subroutine mem_set_value_logical
76 
77  !> @brief Set pointer to value of memory list int variable
78  !<
79  subroutine mem_set_value_int(p_mem, varname, memory_path, found)
80  integer(I4B), pointer, intent(inout) :: p_mem !< pointer to int scalar
81  character(len=*), intent(in) :: varname !< variable name
82  character(len=*), intent(in) :: memory_path !< path where variable is stored
83  logical(LGP), intent(inout) :: found
84  type(memorytype), pointer :: mt
85  logical(LGP) :: checkfail = .false.
86 
87  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
88  if (.not. found) return
89  if (mt%memtype(1:index(mt%memtype, ' ')) == 'INTEGER') then
90  p_mem = mt%intsclr
91  end if
92  end subroutine mem_set_value_int
93 
94  subroutine mem_set_value_int_setval(p_mem, varname, memory_path, setval, found)
95  integer(I4B), pointer, intent(inout) :: p_mem !< pointer to int scalar
96  character(len=*), intent(in) :: varname !< variable name
97  character(len=*), intent(in) :: memory_path !< path where variable is stored
98  integer(I4B), intent(in) :: setval !< set p_mem to setval if varname found
99  logical(LGP), intent(inout) :: found
100  type(memorytype), pointer :: mt
101  logical(LGP) :: checkfail = .false.
102 
103  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
104  if (.not. found) return
105 
106  p_mem = setval
107 
108  end subroutine mem_set_value_int_setval
109 
110  subroutine mem_set_value_str_mapped_int(p_mem, varname, memory_path, str_list, &
111  found)
112  integer(I4B), pointer, intent(inout) :: p_mem !< pointer to int scalar
113  character(len=*), intent(in) :: varname !< variable name
114  character(len=*), intent(in) :: memory_path !< path where variable is stored
115  character(len=*), dimension(:), intent(in) :: str_list
116  logical(LGP), intent(inout) :: found
117  type(memorytype), pointer :: mt
118  logical(LGP) :: checkfail = .false.
119  integer(I4B) :: i
120 
121  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
122  if (.not. found) return
123  if (mt%memtype(1:index(mt%memtype, ' ')) == 'STRING') then
124  do i = 1, size(str_list)
125  if (mt%strsclr == str_list(i)) then
126  p_mem = i
127  end if
128  end do
129  end if
130  end subroutine mem_set_value_str_mapped_int
131 
132  !> @brief Set pointer to value of memory list 1d int array variable
133  !<
134  subroutine mem_set_value_int1d(p_mem, varname, memory_path, found)
135  integer(I4B), dimension(:), pointer, contiguous, intent(inout) :: p_mem !< pointer to 1d int array
136  character(len=*), intent(in) :: varname !< variable name
137  character(len=*), intent(in) :: memory_path !< path where variable is stored
138  logical(LGP), intent(inout) :: found
139  type(memorytype), pointer :: mt
140  logical(LGP) :: checkfail = .false.
141  integer(I4B) :: n
142 
143  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
144  if (.not. found) return
145  if (mt%memtype(1:index(mt%memtype, ' ')) == 'INTEGER') then
146  if (size(mt%aint1d) /= size(p_mem)) then
147  call store_error('mem_set_value() size mismatch int1d, varname='//&
148  &trim(varname), terminate=.true.)
149  end if
150  do n = 1, size(mt%aint1d)
151  p_mem(n) = mt%aint1d(n)
152  end do
153  end if
154  end subroutine mem_set_value_int1d
155 
156  !> @brief Set pointer to value of memory list 1d int array variable with mapping
157  !<
158  subroutine mem_set_value_int1d_mapped(p_mem, varname, memory_path, map, &
159  found)
160  integer(I4B), dimension(:), pointer, contiguous, intent(inout) :: p_mem !< pointer to 1d int array
161  character(len=*), intent(in) :: varname !< variable name
162  character(len=*), intent(in) :: memory_path !< path where variable is stored
163  integer(I4B), dimension(:), pointer, contiguous, intent(in) :: map !< pointer to 1d int mapping array
164  logical(LGP), intent(inout) :: found
165  type(memorytype), pointer :: mt
166  logical(LGP) :: checkfail = .false.
167  integer(I4B) :: n
168 
169  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
170  if (.not. found) return
171  if (mt%memtype(1:index(mt%memtype, ' ')) == 'INTEGER') then
172  if (associated(map)) then
173  do n = 1, size(p_mem)
174  p_mem(n) = mt%aint1d(map(n))
175  end do
176  else
177  if (size(mt%aint1d) /= size(p_mem)) then
178  call store_error('mem_set_value() size mismatch int1d, varname='//&
179  &trim(varname), terminate=.true.)
180  end if
181  do n = 1, size(mt%aint1d)
182  p_mem(n) = mt%aint1d(n)
183  end do
184  end if
185  end if
186  end subroutine mem_set_value_int1d_mapped
187 
188  !> @brief Set pointer to value of memory list 2d int array variable
189  !<
190  subroutine mem_set_value_int2d(p_mem, varname, memory_path, found)
191  integer(I4B), dimension(:, :), pointer, contiguous, intent(inout) :: p_mem !< pointer to 2d int array
192  character(len=*), intent(in) :: varname !< variable name
193  character(len=*), intent(in) :: memory_path !< path where variable is stored
194  logical(LGP), intent(inout) :: found
195  type(memorytype), pointer :: mt
196  logical(LGP) :: checkfail = .false.
197  integer(I4B) :: i, j
198 
199  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
200  if (.not. found) return
201  if (mt%memtype(1:index(mt%memtype, ' ')) == 'INTEGER') then
202  if (size(mt%aint2d, dim=1) /= size(p_mem, dim=1) .or. &
203  size(mt%aint2d, dim=2) /= size(p_mem, dim=2)) then
204  call store_error('mem_set_value() size mismatch int2d, varname='//&
205  &trim(varname), terminate=.true.)
206  end if
207  do j = 1, size(mt%aint2d, dim=2)
208  do i = 1, size(mt%aint2d, dim=1)
209  p_mem(i, j) = mt%aint2d(i, j)
210  end do
211  end do
212  end if
213  end subroutine mem_set_value_int2d
214 
215  !> @brief Set pointer to value of memory list 3d int array variable
216  !<
217  subroutine mem_set_value_int3d(p_mem, varname, memory_path, found)
218  integer(I4B), dimension(:, :, :), pointer, contiguous, intent(inout) :: p_mem !< pointer to 3d int array
219  character(len=*), intent(in) :: varname !< variable name
220  character(len=*), intent(in) :: memory_path !< path where variable is stored
221  logical(LGP), intent(inout) :: found
222  type(memorytype), pointer :: mt
223  logical(LGP) :: checkfail = .false.
224  integer(I4B) :: i, j, k
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 (size(mt%aint3d, dim=1) /= size(p_mem, dim=1) .or. &
230  size(mt%aint3d, dim=2) /= size(p_mem, dim=2) .or. &
231  size(mt%aint3d, dim=3) /= size(p_mem, dim=3)) then
232  call store_error('mem_set_value() size mismatch int3d, varname='//&
233  &trim(varname), terminate=.true.)
234  end if
235  do k = 1, size(mt%aint3d, dim=3)
236  do j = 1, size(mt%aint3d, dim=2)
237  do i = 1, size(mt%aint3d, dim=1)
238  p_mem(i, j, k) = mt%aint3d(i, j, k)
239  end do
240  end do
241  end do
242  end if
243  end subroutine mem_set_value_int3d
244 
245  !> @brief Set pointer to value of memory list double variable
246  !<
247  subroutine mem_set_value_dbl(p_mem, varname, memory_path, found)
248  real(DP), pointer, intent(inout) :: p_mem !< pointer to dbl scalar
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 
255  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
256  if (.not. found) return
257  if (mt%memtype(1:index(mt%memtype, ' ')) == 'DOUBLE') then
258  p_mem = mt%dblsclr
259  end if
260  end subroutine mem_set_value_dbl
261 
262  !> @brief Set pointer to value of memory list 1d dbl array variable
263  !<
264  subroutine mem_set_value_dbl1d(p_mem, varname, memory_path, found)
265  real(DP), dimension(:), pointer, contiguous, intent(inout) :: p_mem !< pointer to 1d dbl array
266  character(len=*), intent(in) :: varname !< variable name
267  character(len=*), intent(in) :: memory_path !< path where variable is stored
268  logical(LGP), intent(inout) :: found
269  type(memorytype), pointer :: mt
270  logical(LGP) :: checkfail = .false.
271  integer(I4B) :: n
272 
273  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
274  if (.not. found) return
275  if (mt%memtype(1:index(mt%memtype, ' ')) == 'DOUBLE') then
276  if (size(mt%adbl1d) /= size(p_mem)) then
277  call store_error('mem_set_value() size mismatch dbl1d, varname='//&
278  &trim(varname), terminate=.true.)
279  end if
280  do n = 1, size(mt%adbl1d)
281  p_mem(n) = mt%adbl1d(n)
282  end do
283  end if
284  end subroutine mem_set_value_dbl1d
285 
286  !> @brief Set pointer to value of memory list 1d dbl array variable with mapping
287  !<
288  subroutine mem_set_value_dbl1d_mapped(p_mem, varname, memory_path, map, &
289  found)
290  real(DP), dimension(:), pointer, contiguous, intent(inout) :: p_mem !< pointer to 1d dbl array
291  character(len=*), intent(in) :: varname !< variable name
292  character(len=*), intent(in) :: memory_path !< path where variable is stored
293  integer(I4B), dimension(:), pointer, contiguous, intent(in) :: map !< pointer to 1d int mapping array
294  logical(LGP), intent(inout) :: found
295  type(memorytype), pointer :: mt
296  logical(LGP) :: checkfail = .false.
297  integer(I4B) :: n
298 
299  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
300  if (.not. found) return
301  if (mt%memtype(1:index(mt%memtype, ' ')) == 'DOUBLE') then
302  if (associated(map)) then
303  do n = 1, size(p_mem)
304  p_mem(n) = mt%adbl1d(map(n))
305  end do
306  else
307  if (size(mt%adbl1d) /= size(p_mem)) then
308  call store_error('mem_set_value() size mismatch dbl1d, varname='//&
309  &trim(varname), terminate=.true.)
310  end if
311  do n = 1, size(mt%adbl1d)
312  p_mem(n) = mt%adbl1d(n)
313  end do
314  end if
315  end if
316  end subroutine mem_set_value_dbl1d_mapped
317 
318  !> @brief Set pointer to value of memory list 2d dbl array variable
319  !<
320  subroutine mem_set_value_dbl2d(p_mem, varname, memory_path, found)
321  real(DP), dimension(:, :), pointer, contiguous, intent(inout) :: p_mem !< pointer to 2d dbl array
322  character(len=*), intent(in) :: varname !< variable name
323  character(len=*), intent(in) :: memory_path !< path where variable is stored
324  logical(LGP), intent(inout) :: found
325  type(memorytype), pointer :: mt
326  logical(LGP) :: checkfail = .false.
327  integer(I4B) :: i, j
328 
329  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
330  if (.not. found) return
331  if (mt%memtype(1:index(mt%memtype, ' ')) == 'DOUBLE') then
332  if (size(mt%adbl2d, dim=1) /= size(p_mem, dim=1) .or. &
333  size(mt%adbl2d, dim=2) /= size(p_mem, dim=2)) then
334  call store_error('mem_set_value() size mismatch dbl2d, varname='//&
335  &trim(varname), terminate=.true.)
336  end if
337  do j = 1, size(mt%adbl2d, dim=2)
338  do i = 1, size(mt%adbl2d, dim=1)
339  p_mem(i, j) = mt%adbl2d(i, j)
340  end do
341  end do
342  end if
343  end subroutine mem_set_value_dbl2d
344 
345  !> @brief Set pointer to value of memory list 3d dbl array variable
346  !<
347  subroutine mem_set_value_dbl3d(p_mem, varname, memory_path, found)
348  real(DP), dimension(:, :, :), pointer, contiguous, intent(inout) :: p_mem !< pointer to 3d dbl array
349  character(len=*), intent(in) :: varname !< variable name
350  character(len=*), intent(in) :: memory_path !< path where variable is stored
351  logical(LGP), intent(inout) :: found
352  type(memorytype), pointer :: mt
353  logical(LGP) :: checkfail = .false.
354  integer(I4B) :: i, j, k
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 (size(mt%adbl3d, dim=1) /= size(p_mem, dim=1) .or. &
360  size(mt%adbl3d, dim=2) /= size(p_mem, dim=2) .or. &
361  size(mt%adbl3d, dim=3) /= size(p_mem, dim=3)) then
362  call store_error('mem_set_value() size mismatch dbl3d, varname='//&
363  &trim(varname), terminate=.true.)
364  end if
365  do k = 1, size(mt%adbl3d, dim=3)
366  do j = 1, size(mt%adbl3d, dim=2)
367  do i = 1, size(mt%adbl3d, dim=1)
368  p_mem(i, j, k) = mt%adbl3d(i, j, k)
369  end do
370  end do
371  end do
372  end if
373  end subroutine mem_set_value_dbl3d
374 
375  subroutine mem_set_value_str(p_mem, varname, memory_path, found)
376  character(len=*), intent(inout) :: p_mem !< pointer to str scalar
377  character(len=*), intent(in) :: varname !< variable name
378  character(len=*), intent(in) :: memory_path !< path where variable is stored
379  logical(LGP), intent(inout) :: found
380  type(memorytype), pointer :: mt
381  logical(LGP) :: checkfail = .false.
382 
383  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
384  if (.not. found) return
385  if (mt%memtype(1:index(mt%memtype, ' ')) == 'STRING') then
386  p_mem = mt%strsclr
387  end if
388  end subroutine mem_set_value_str
389 
390  subroutine mem_set_value_charstr1d(p_mem, varname, memory_path, found)
392  type(characterstringtype), dimension(:), &
393  pointer, contiguous, intent(inout) :: p_mem !< pointer to charstr 1d array
394  character(len=*), intent(in) :: varname !< variable name
395  character(len=*), intent(in) :: memory_path !< path where variable is stored
396  logical(LGP), intent(inout) :: found
397  type(memorytype), pointer :: mt
398  logical(LGP) :: checkfail = .false.
399  integer(I4B) :: n
400 
401  call get_from_memorystore(varname, memory_path, mt, found, checkfail)
402  if (.not. found) return
403  if (mt%memtype(1:index(mt%memtype, ' ')) == 'STRING') then
404  do n = 1, size(mt%acharstr1d)
405  p_mem(n) = mt%acharstr1d(n)
406  end do
407  end if
408  end subroutine mem_set_value_charstr1d
409 
410 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_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.