MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
MemoryManager.f90
Go to the documentation of this file.
2
3 use kindmodule, only: dp, lgp, i4b, i8b
4 use constantsmodule, only: dzero, done, &
5 dem3, dem6, dem9, dep3, dep6, dep9, &
12 use simvariablesmodule, only: errmsg
21
22 implicit none
23 private
24 public :: mem_allocate
25 public :: mem_checkin
26 public :: mem_reallocate
27 public :: mem_setptr
28 public :: mem_copyptr
29 public :: mem_reassignptr
30 public :: mem_deallocate
31 public :: mem_write_usage
32 public :: mem_da
33 public :: mem_set_print_option
34 public :: get_from_memorystore
35
36 public :: get_mem_type
37 public :: get_mem_rank
38 public :: get_mem_elem_size
39 public :: get_mem_shape
40 public :: get_isize
41 public :: copy_dbl1d
42
43 public :: memorystore
44 public :: mem_print_detailed
45 public :: mem_release
46
48 type(tabletype), pointer :: memtab => null()
49 integer(I8B) :: nvalues_alogical = 0
50 integer(I8B) :: nvalues_astr = 0
51 integer(I8B) :: nvalues_aint = 0
52 integer(I8B) :: nvalues_adbl = 0
53 integer(I4B) :: iprmem = 0
54
55 interface mem_allocate
56 module procedure &
70 end interface mem_allocate
71
72 interface mem_checkin
73 module procedure &
80 end interface mem_checkin
81
83 module procedure &
91 end interface mem_reallocate
92
93 interface mem_setptr
94 module procedure &
97 setptr_int, &
100 setptr_int3d, &
101 setptr_dbl, &
102 setptr_dbl1d, &
103 setptr_dbl2d, &
104 setptr_dbl3d, &
105 setptr_str, &
106 setptr_str1d, &
108 end interface mem_setptr
109
110 interface mem_copyptr
111 module procedure &
117 end interface mem_copyptr
118
120 module procedure &
127 end interface mem_reassignptr
128
130 module procedure &
144 end interface mem_deallocate
145
146contains
147
148 !> @brief Get the variable memory type
149 !!
150 !! Returns any of 'LOGICAL', 'INTEGER', 'DOUBLE', 'STRING'.
151 !! returns 'UNKNOWN' when the variable is not found.
152 !<
153 subroutine get_mem_type(name, mem_path, var_type)
154 character(len=*), intent(in) :: name !< variable name
155 character(len=*), intent(in) :: mem_path !< path where the variable is stored
156 character(len=LENMEMTYPE), intent(out) :: var_type !< memory type
157 ! -- local
158 type(memorytype), pointer :: mt
159 logical(LGP) :: found
160 ! -- code
161 mt => null()
162 var_type = 'UNKNOWN'
163 call get_from_memorystore(name, mem_path, mt, found)
164 if (found) then
165 var_type = mt%memtype
166 end if
167 end subroutine get_mem_type
168
169 !> @brief Get the variable rank
170 !!
171 !! Returns rank = -1 when not found.
172 !<
173 subroutine get_mem_rank(name, mem_path, rank)
174 character(len=*), intent(in) :: name !< variable name
175 character(len=*), intent(in) :: mem_path !< mem_path
176 integer(I4B), intent(out) :: rank !< rank
177 ! -- local
178 type(memorytype), pointer :: mt => null()
179 logical(LGP) :: found
180 ! -- code
181 !
182 ! -- initialize rank to a value to communicate failure
183 rank = -1
184 !
185 ! -- get the entry from the memory manager; check=.false. so a missing
186 ! variable returns rank=-1 instead of terminating
187 call get_from_memorystore(name, mem_path, mt, found, check=.false.)
188 !
189 ! -- set rank
190 if (found) then
191 if (associated(mt%logicalsclr)) rank = 0
192 if (associated(mt%intsclr)) rank = 0
193 if (associated(mt%dblsclr)) rank = 0
194 if (associated(mt%alogical1d)) rank = 1
195 if (associated(mt%aint1d)) rank = 1
196 if (associated(mt%aint2d)) rank = 2
197 if (associated(mt%aint3d)) rank = 3
198 if (associated(mt%adbl1d)) rank = 1
199 if (associated(mt%adbl2d)) rank = 2
200 if (associated(mt%adbl3d)) rank = 3
201 if (associated(mt%strsclr)) rank = 0
202 if (associated(mt%astr1d)) rank = 1
203 if (associated(mt%acharstr1d)) rank = 1
204 end if
205 end subroutine get_mem_rank
206
207 !> @brief Get the memory size of a single element of the stored variable
208 !!
209 !! Memory size in bytes, returns size = -1 when not found. This is
210 !< also string length.
211 subroutine get_mem_elem_size(name, mem_path, size)
212 character(len=*), intent(in) :: name !< variable name
213 character(len=*), intent(in) :: mem_path !< path where the variable is stored
214 integer(I4B), intent(out) :: size !< size of the variable in bytes
215 ! -- local
216 type(memorytype), pointer :: mt => null()
217 logical(LGP) :: found
218 ! -- code
219 !
220 ! -- initialize size to a value to communicate failure
221 size = -1
222 !
223 ! -- get the entry from the memory manager
224 call get_from_memorystore(name, mem_path, mt, found)
225 !
226 ! -- set memory size
227 if (found) then
228 size = mt%element_size
229 end if
230 end subroutine get_mem_elem_size
231
232 !> @brief Get the variable memory shape
233 !!
234 !! Returns an integer array with the shape (Fortran ordering),
235 !! and set shape(1) = -1 when not found.
236 !<
237 subroutine get_mem_shape(name, mem_path, mem_shape)
238 character(len=*), intent(in) :: name !< variable name
239 character(len=*), intent(in) :: mem_path !< path where the variable is stored
240 integer(I4B), dimension(:), intent(out) :: mem_shape !< shape of the variable
241 ! -- local
242 type(memorytype), pointer :: mt => null()
243 logical(LGP) :: found
244 ! -- code
245 !
246 ! -- get the entry from the memory manager
247 call get_from_memorystore(name, mem_path, mt, found)
248 !
249 ! -- set shape
250 if (found) then
251 if (associated(mt%logicalsclr)) mem_shape = shape(mt%logicalsclr)
252 if (associated(mt%intsclr)) mem_shape = shape(mt%logicalsclr)
253 if (associated(mt%dblsclr)) mem_shape = shape(mt%dblsclr)
254 if (associated(mt%alogical1d)) mem_shape = shape(mt%alogical1d)
255 if (associated(mt%aint1d)) mem_shape = shape(mt%aint1d)
256 if (associated(mt%aint2d)) mem_shape = shape(mt%aint2d)
257 if (associated(mt%aint3d)) mem_shape = shape(mt%aint3d)
258 if (associated(mt%adbl1d)) mem_shape = shape(mt%adbl1d)
259 if (associated(mt%adbl2d)) mem_shape = shape(mt%adbl2d)
260 if (associated(mt%adbl3d)) mem_shape = shape(mt%adbl3d)
261 if (associated(mt%strsclr)) mem_shape = shape(mt%strsclr)
262 if (associated(mt%astr1d)) mem_shape = shape(mt%astr1d)
263 if (associated(mt%acharstr1d)) mem_shape = shape(mt%acharstr1d)
264 ! -- to communicate failure
265 else
266 mem_shape(1) = -1
267 end if
268 end subroutine get_mem_shape
269
270 !> @brief Get the number of elements for this variable
271 !!
272 !! Returns with isize = -1 when not found.
273 !! Return 1 for scalars.
274 !! @hidecallergraph
275 !<
276 subroutine get_isize(name, mem_path, isize)
277 character(len=*), intent(in) :: name !< variable name
278 character(len=*), intent(in) :: mem_path !< path where the variable is stored
279 integer(I4B), intent(out) :: isize !< number of elements (flattened)
280 ! -- local
281 type(memorytype), pointer :: mt => null()
282 logical(LGP) :: found
283 logical(LGP) :: terminate
284 ! -- code
285 !
286 ! -- initialize isize to a value to communicate failure
287 isize = -1
288 !
289 ! -- don't exit program if variable not found
290 terminate = .false.
291 !
292 ! -- get the entry from the memory manager
293 call get_from_memorystore(name, mem_path, mt, found, terminate)
294 !
295 ! -- set isize
296 if (found) then
297 isize = mt%isize
298 end if
299 end subroutine get_isize
300
301 !> @brief Get a memory type entry from the memory list
302 !!
303 !! Default value for @par check is .true. which means that this
304 !! routine will kill the program when the memory entry cannot be found.
305 !! @hidecallergraph
306 !<
307 subroutine get_from_memorystore(name, mem_path, mt, found, check)
308 character(len=*), intent(in) :: name !< variable name
309 character(len=*), intent(in) :: mem_path !< path where the variable is stored
310 type(memorytype), pointer, intent(inout) :: mt !< memory type entry
311 logical(LGP), intent(out) :: found !< set to .true. when found
312 logical(LGP), intent(in), optional :: check !< to suppress aborting the program when not found,
313 !! set check = .false.
314 ! -- local
315 logical(LGP) check_opt
316 ! -- code
317 mt => memorystore%get(name, mem_path)
318 found = associated(mt)
319
320 check_opt = .true.
321 if (present(check)) then
322 check_opt = check
323 end if
324 if (check_opt) then
325 if (.not. found) then
326 errmsg = "Programming error in memory manager. Variable '"// &
327 trim(name)//"' in '"//trim(mem_path)//"' cannot be "// &
328 "assigned because it does not exist in memory manager."
329 call store_error(errmsg, terminate=.true.)
330 end if
331 end if
332 end subroutine get_from_memorystore
333
334 !> @brief Issue allocation error message and stop program execution
335 !<
336 subroutine allocate_error(varname, mem_path, istat, isize)
337 character(len=*), intent(in) :: varname !< variable name
338 character(len=*), intent(in) :: mem_path !< path where the variable is stored
339 integer(I4B), intent(in) :: istat !< status code
340 integer(I4B), intent(in) :: isize !< size of allocation
341 ! -- local
342 character(len=20) :: csize
343 character(len=20) :: cstat
344 ! -- code
345 !
346 ! -- initialize character variables
347 write (csize, '(i0)') isize
348 write (cstat, '(i0)') istat
349 !
350 ! -- create error message
351 errmsg = "Error trying to allocate memory. Path '"//trim(mem_path)// &
352 "' variable name '"//trim(varname)//"' size '"//trim(csize)// &
353 "'. Error message is '"//trim(adjustl(errmsg))// &
354 "'. Status code is "//trim(cstat)//'.'
355 !
356 ! -- store error and stop program execution
357 call store_error(errmsg, terminate=.true.)
358 end subroutine allocate_error
359
360 !> @brief Allocate a logical scalar
361 !<
362 subroutine allocate_logical(sclr, name, mem_path)
363 logical(LGP), pointer, intent(inout) :: sclr !< variable for allocation
364 character(len=*), intent(in) :: name !< variable name
365 character(len=*), intent(in) :: mem_path !< path where the variable is stored
366 ! -- local
367 integer(I4B) :: istat
368 type(memorytype), pointer :: mt
369 ! -- code
370 !
371 ! -- check variable name length
372 call mem_check_length(name, lenvarname, "variable")
373 !
374 ! -- allocate the logical scalar
375 allocate (sclr, stat=istat, errmsg=errmsg)
376 if (istat /= 0) then
377 call allocate_error(name, mem_path, istat, 1)
378 end if
379 !
380 ! -- update counter
382 !
383 ! -- allocate memory type
384 allocate (mt)
385 !
386 ! -- set memory type
387 mt%logicalsclr => sclr
388 mt%element_size = lgp
389 mt%isize = 1
390 mt%name = name
391 mt%path = mem_path
392 write (mt%memtype, "(a)") 'LOGICAL'
393 !
394 ! -- add memory type to the memory list
395 call memorystore%add(mt)
396 end subroutine allocate_logical
397
398 !> @brief Allocate a 1-dimensional logical array
399 !<
400 subroutine allocate_logical1d(alog, nrow, name, mem_path)
401 logical(LGP), dimension(:), pointer, contiguous, intent(inout) :: alog !< variable for allocation
402 integer(I4B), intent(in) :: nrow !< number of rows
403 character(len=*), intent(in) :: name !< variable name
404 character(len=*), intent(in) :: mem_path !< path where variable is stored
405 ! --local
406 type(memorytype), pointer :: mt
407 integer(I4B) :: istat
408 integer(I4B) :: isize
409 ! -- code
410 !
411 ! -- check variable name length
412 call mem_check_length(name, lenvarname, "variable")
413 !
414 ! -- set isize
415 isize = nrow
416 !
417 ! -- allocate logical array
418 allocate (alog(nrow), stat=istat, errmsg=errmsg)
419 if (istat /= 0) then
420 call allocate_error(name, mem_path, istat, isize)
421 end if
422 !
423 ! -- update counter
425 !
426 ! -- allocate memory type
427 allocate (mt)
428 !
429 ! -- set memory type
430 mt%alogical1d => alog
431 mt%element_size = lgp
432 mt%isize = isize
433 mt%name = name
434 mt%path = mem_path
435 write (mt%memtype, "(a,' (',i0,')')") 'LOGICAL', isize
436 !
437 ! -- add memory type to the memory list
438 call memorystore%add(mt)
439 end subroutine allocate_logical1d
440
441 !> @brief Allocate a character string
442 !<
443 subroutine allocate_str(sclr, ilen, name, mem_path)
444 integer(I4B), intent(in) :: ilen !< string length
445 character(len=ilen), pointer, intent(inout) :: sclr !< variable for allocation
446 character(len=*), intent(in) :: name !< variable name
447 character(len=*), intent(in) :: mem_path !< path where the variable is stored
448 ! -- local
449 integer(I4B) :: istat
450 type(memorytype), pointer :: mt
451 ! -- format
452 ! -- code
453 !
454 ! -- make sure ilen is greater than 0
455 if (ilen < 1) then
456 errmsg = 'Programming error in allocate_str. ILEN must be greater than 0.'
457 call store_error(errmsg, terminate=.true.)
458 end if
459 !
460 ! -- check variable name length
461 call mem_check_length(name, lenvarname, "variable")
462 !
463 ! -- allocate string
464 allocate (character(len=ilen) :: sclr, stat=istat, errmsg=errmsg)
465 if (istat /= 0) then
466 call allocate_error(name, mem_path, istat, 1)
467 end if
468 !
469 ! -- set sclr to a empty string
470 sclr = ' '
471 !
472 ! -- update counter
474 !
475 ! -- allocate memory type
476 allocate (mt)
477 !
478 ! -- set memory type
479 mt%strsclr => sclr
480 mt%element_size = ilen
481 mt%isize = 1
482 mt%name = name
483 mt%path = mem_path
484 write (mt%memtype, "(a,' LEN=',i0)") 'STRING', ilen
485 !
486 ! -- add defined length string to the memory manager list
487 call memorystore%add(mt)
488 end subroutine allocate_str
489
490 !> @brief Allocate a 1-dimensional defined length string array
491 !<
492 subroutine allocate_str1d(astr1d, ilen, nrow, name, mem_path)
493 integer(I4B), intent(in) :: ilen !< string length
494 character(len=ilen), dimension(:), &
495 pointer, contiguous, intent(inout) :: astr1d !< variable for allocation
496 integer(I4B), intent(in) :: nrow !< number of strings in array
497 character(len=*), intent(in) :: name !< variable name
498 character(len=*), intent(in) :: mem_path !< path where the variable is stored
499 ! -- local variables
500 type(memorytype), pointer :: mt
501 character(len=ilen) :: string
502 integer(I4B) :: n
503 integer(I4B) :: istat
504 integer(I4B) :: isize
505 ! -- code
506 !
507 ! -- initialize string
508 string = ''
509 !
510 ! -- make sure ilen is greater than 0
511 if (ilen < 1) then
512 errmsg = 'Programming error in allocate_str1d. '// &
513 'ILEN must be greater than 0.'
514 call store_error(errmsg, terminate=.true.)
515 end if
516 !
517 ! -- check variable name length
518 call mem_check_length(name, lenvarname, "variable")
519 !
520 ! -- calculate isize
521 isize = nrow
522 !
523 ! -- allocate defined length string array
524 allocate (character(len=ilen) :: astr1d(nrow), stat=istat, errmsg=errmsg)
525 !
526 ! -- check for error condition
527 if (istat /= 0) then
528 call allocate_error(name, mem_path, istat, isize)
529 end if
530 !
531 ! -- fill deferred length string with empty string
532 do n = 1, nrow
533 astr1d(n) = string
534 end do
535 !
536 ! -- update counter
537 nvalues_astr = nvalues_astr + isize
538 !
539 ! -- allocate memory type
540 allocate (mt)
541 !
542 ! -- set memory type
543 mt%astr1d => astr1d
544 mt%element_size = ilen
545 mt%isize = isize
546 mt%name = name
547 mt%path = mem_path
548 write (mt%memtype, "(a,' LEN=',i0,' (',i0,')')") 'STRING', ilen, nrow
549 !
550 ! -- add deferred length character array to the memory manager list
551 call memorystore%add(mt)
552 end subroutine allocate_str1d
553
554 !> @brief Allocate a 1-dimensional array of deferred-length CharacterStringType
555 !<
556 subroutine allocate_charstr1d(acharstr1d, ilen, nrow, name, mem_path)
557 type(characterstringtype), dimension(:), &
558 pointer, contiguous, intent(inout) :: acharstr1d !< variable for allocation
559 integer(I4B), intent(in) :: ilen !< string length
560 integer(I4B), intent(in) :: nrow !< number of strings in array
561 character(len=*), intent(in) :: name !< variable name
562 character(len=*), intent(in) :: mem_path !< path where the variable is stored
563 ! -- local variables
564 character(len=ilen) :: string
565 type(memorytype), pointer :: mt
566 integer(I4B) :: n
567 integer(I4B) :: istat
568 integer(I4B) :: isize
569 ! -- code
570 !
571 ! -- initialize string
572 string = ''
573 !
574 ! -- check variable name length
575 call mem_check_length(name, lenvarname, "variable")
576 !
577 ! -- calculate isize
578 isize = nrow
579 !
580 ! -- allocate deferred length string array
581 allocate (acharstr1d(nrow), stat=istat, errmsg=errmsg)
582 !
583 ! -- check for error condition
584 if (istat /= 0) then
585 call allocate_error(name, mem_path, istat, isize)
586 end if
587 !
588 ! -- fill deferred length string with empty string
589 do n = 1, nrow
590 acharstr1d(n) = string
591 end do
592 !
593 ! -- update counter
594 nvalues_astr = nvalues_astr + isize
595 !
596 ! -- allocate memory type
597 allocate (mt)
598 !
599 ! -- set memory type
600 mt%acharstr1d => acharstr1d
601 mt%element_size = ilen
602 mt%isize = isize
603 mt%name = name
604 mt%path = mem_path
605 write (mt%memtype, "(a,' LEN=',i0,' (',i0,')')") 'STRING', ilen, nrow
606 !
607 ! -- add deferred length character array to the memory manager list
608 call memorystore%add(mt)
609 end subroutine allocate_charstr1d
610
611 !> @brief Allocate a integer scalar
612 !<
613 subroutine allocate_int(sclr, name, mem_path)
614 integer(I4B), pointer, intent(inout) :: sclr !< variable for allocation
615 character(len=*), intent(in) :: name !< variable name
616 character(len=*), intent(in) :: mem_path !< path where the variable is stored
617 ! -- local
618 type(memorytype), pointer :: mt
619 integer(I4B) :: istat
620 ! -- code
621 !
622 ! -- check variable name length
623 call mem_check_length(name, lenvarname, "variable")
624 !
625 ! -- allocate integer scalar
626 allocate (sclr, stat=istat, errmsg=errmsg)
627 if (istat /= 0) then
628 call allocate_error(name, mem_path, istat, 1)
629 end if
630 !
631 ! -- update counter
633 !
634 ! -- allocate memory type
635 allocate (mt)
636 !
637 ! -- set memory type
638 mt%intsclr => sclr
639 mt%element_size = i4b
640 mt%isize = 1
641 mt%name = name
642 mt%path = mem_path
643 write (mt%memtype, "(a)") 'INTEGER'
644 !
645 ! -- add memory type to the memory list
646 call memorystore%add(mt)
647 end subroutine allocate_int
648
649 !> @brief Allocate a 1-dimensional integer array
650 !<
651 subroutine allocate_int1d(aint, nrow, name, mem_path)
652 integer(I4B), dimension(:), pointer, contiguous, intent(inout) :: aint !< variable for allocation
653 integer(I4B), intent(in) :: nrow !< integer array number of rows
654 character(len=*), intent(in) :: name !< variable name
655 character(len=*), intent(in) :: mem_path !< path where variable is stored
656 ! --local
657 type(memorytype), pointer :: mt
658 integer(I4B) :: istat
659 integer(I4B) :: isize
660 ! -- code
661 !
662 ! -- check variable name length
663 call mem_check_length(name, lenvarname, "variable")
664 !
665 ! -- set isize
666 isize = nrow
667 !
668 ! -- allocate integer array
669 allocate (aint(nrow), stat=istat, errmsg=errmsg)
670 if (istat /= 0) then
671 call allocate_error(name, mem_path, istat, isize)
672 end if
673 !
674 ! -- update counter
675 nvalues_aint = nvalues_aint + isize
676 !
677 ! -- allocate memory type
678 allocate (mt)
679 !
680 ! -- set memory type
681 mt%aint1d => aint
682 mt%element_size = i4b
683 mt%isize = isize
684 mt%name = name
685 mt%path = mem_path
686 write (mt%memtype, "(a,' (',i0,')')") 'INTEGER', isize
687 !
688 ! -- add memory type to the memory list
689 call memorystore%add(mt)
690 end subroutine allocate_int1d
691
692 !> @brief Allocate a 2-dimensional integer array
693 !<
694 subroutine allocate_int2d(aint, ncol, nrow, name, mem_path)
695 integer(I4B), dimension(:, :), pointer, contiguous, intent(inout) :: aint !< variable for allocation
696 integer(I4B), intent(in) :: ncol !< number of columns
697 integer(I4B), intent(in) :: nrow !< number of rows
698 character(len=*), intent(in) :: name !< variable name
699 character(len=*), intent(in) :: mem_path !< path where variable is stored
700 ! -- local
701 type(memorytype), pointer :: mt
702 integer(I4B) :: istat
703 integer(I4B) :: isize
704 ! -- code
705 !
706 ! -- check the variable name length
707 call mem_check_length(name, lenvarname, "variable")
708 !
709 ! -- set isize
710 isize = ncol * nrow
711 !
712 ! -- allocate the integer array
713 allocate (aint(ncol, nrow), stat=istat, errmsg=errmsg)
714 if (istat /= 0) then
715 call allocate_error(name, mem_path, istat, isize)
716 end if
717 !
718 ! -- update the counter
719 nvalues_aint = nvalues_aint + isize
720 !
721 ! -- allocate memory type
722 allocate (mt)
723 !
724 ! -- set memory type
725 mt%aint2d => aint
726 mt%element_size = i4b
727 mt%isize = isize
728 mt%name = name
729 mt%path = mem_path
730 write (mt%memtype, "(a,' (',i0,',',i0,')')") 'INTEGER', ncol, nrow
731 !
732 ! -- add memory type to the memory list
733 call memorystore%add(mt)
734 end subroutine allocate_int2d
735
736 !> @brief Allocate a 3-dimensional integer array
737 !<
738 subroutine allocate_int3d(aint, ncol, nrow, nlay, name, mem_path)
739 integer(I4B), dimension(:, :, :), pointer, contiguous, intent(inout) :: aint !< variable for allocation
740 integer(I4B), intent(in) :: ncol !< number of columns
741 integer(I4B), intent(in) :: nrow !< number of rows
742 integer(I4B), intent(in) :: nlay !< number of layers
743 character(len=*), intent(in) :: name !< variable name
744 character(len=*), intent(in) :: mem_path !< path where variable is stored
745 ! -- local
746 type(memorytype), pointer :: mt
747 integer(I4B) :: istat
748 integer(I4B) :: isize
749 ! -- code
750 !
751 ! -- check variable name length
752 call mem_check_length(name, lenvarname, "variable")
753 !
754 ! -- set isize
755 isize = ncol * nrow * nlay
756 !
757 ! -- allocate integer array
758 allocate (aint(ncol, nrow, nlay), stat=istat, errmsg=errmsg)
759 if (istat /= 0) then
760 call allocate_error(name, mem_path, istat, isize)
761 end if
762 !
763 ! -- update counter
764 nvalues_aint = nvalues_aint + isize
765 !
766 ! -- allocate memory type
767 allocate (mt)
768 !
769 ! -- set memory type
770 mt%aint3d => aint
771 mt%element_size = i4b
772 mt%isize = isize
773 mt%name = name
774 mt%path = mem_path
775 write (mt%memtype, "(a,' (',i0,',',i0,',',i0,')')") 'INTEGER', ncol, &
776 nrow, nlay
777 !
778 ! -- add memory type to the memory list
779 call memorystore%add(mt)
780 end subroutine allocate_int3d
781
782 !> @brief Allocate a real scalar
783 !<
784 subroutine allocate_dbl(sclr, name, mem_path)
785 real(DP), pointer, intent(inout) :: sclr !< variable for allocation
786 character(len=*), intent(in) :: name !< variable name
787 character(len=*), intent(in) :: mem_path !< path where variable is stored
788 ! -- local
789 type(memorytype), pointer :: mt
790 integer(I4B) :: istat
791 ! -- code
792 !
793 ! -- check variable name length
794 call mem_check_length(name, lenvarname, "variable")
795 !
796 ! -- allocate real scalar
797 allocate (sclr, stat=istat, errmsg=errmsg)
798 if (istat /= 0) then
799 call allocate_error(name, mem_path, istat, 1)
800 end if
801 !
802 ! -- update counter
804 !
805 ! -- allocate memory type
806 allocate (mt)
807 !
808 ! -- set memory type
809 mt%dblsclr => sclr
810 mt%element_size = dp
811 mt%isize = 1
812 mt%name = name
813 mt%path = mem_path
814 write (mt%memtype, "(a)") 'DOUBLE'
815 !
816 ! -- add memory type to the memory list
817 call memorystore%add(mt)
818 end subroutine allocate_dbl
819
820 !> @brief Allocate a 1-dimensional real array
821 !<
822 subroutine allocate_dbl1d(adbl, nrow, name, mem_path)
823 real(DP), dimension(:), pointer, contiguous, intent(inout) :: adbl !< variable for allocation
824 integer(I4B), intent(in) :: nrow !< number of rows
825 character(len=*), intent(in) :: name !< variable name
826 character(len=*), intent(in) :: mem_path !< path where variable is stored
827 ! -- local
828 type(memorytype), pointer :: mt
829 integer(I4B) :: istat
830 integer(I4B) :: isize
831 ! -- code
832 !
833 ! -- check the variable name length
834 call mem_check_length(name, lenvarname, "variable")
835 !
836 ! -- set isize
837 isize = nrow
838 !
839 ! -- allocate the real array
840 allocate (adbl(nrow), stat=istat, errmsg=errmsg)
841 if (istat /= 0) then
842 call allocate_error(name, mem_path, istat, isize)
843 end if
844 !
845 ! -- update counter
846 nvalues_adbl = nvalues_adbl + isize
847 !
848 ! -- allocate memory type
849 allocate (mt)
850 !
851 ! -- set memory type
852 mt%adbl1d => adbl
853 mt%element_size = dp
854 mt%isize = isize
855 mt%name = name
856 mt%path = mem_path
857 write (mt%memtype, "(a,' (',i0,')')") 'DOUBLE', isize
858 !
859 ! -- add memory type to the memory list
860 call memorystore%add(mt)
861 end subroutine allocate_dbl1d
862
863 !> @brief Allocate a 2-dimensional real array
864 !<
865 subroutine allocate_dbl2d(adbl, ncol, nrow, name, mem_path)
866 real(DP), dimension(:, :), pointer, contiguous, intent(inout) :: adbl !< variable for allocation
867 integer(I4B), intent(in) :: ncol !< number of columns
868 integer(I4B), intent(in) :: nrow !< number of rows
869 character(len=*), intent(in) :: name !< variable name
870 character(len=*), intent(in) :: mem_path !< path where variable is stored
871 ! -- local
872 type(memorytype), pointer :: mt
873 integer(I4B) :: istat
874 integer(I4B) :: isize
875 ! -- code
876 !
877 ! -- check the variable name length
878 call mem_check_length(name, lenvarname, "variable")
879 !
880 ! -- set isize
881 isize = ncol * nrow
882 !
883 ! -- allocate the real array
884 allocate (adbl(ncol, nrow), stat=istat, errmsg=errmsg)
885 if (istat /= 0) then
886 call allocate_error(name, mem_path, istat, isize)
887 end if
888 !
889 ! -- update counter
890 nvalues_adbl = nvalues_adbl + isize
891 !
892 ! -- allocate memory type
893 allocate (mt)
894 !
895 ! -- set memory type
896 mt%adbl2d => adbl
897 mt%element_size = dp
898 mt%isize = isize
899 mt%name = name
900 mt%path = mem_path
901 write (mt%memtype, "(a,' (',i0,',',i0,')')") 'DOUBLE', ncol, nrow
902 !
903 ! -- add memory type to the memory list
904 call memorystore%add(mt)
905 end subroutine allocate_dbl2d
906
907 !> @brief Allocate a 3-dimensional real array
908 !<
909 subroutine allocate_dbl3d(adbl, ncol, nrow, nlay, name, mem_path)
910 real(DP), dimension(:, :, :), pointer, contiguous, intent(inout) :: adbl !< variable for allocation
911 integer(I4B), intent(in) :: ncol !< number of columns
912 integer(I4B), intent(in) :: nrow !< number of rows
913 integer(I4B), intent(in) :: nlay !< number of layers
914 character(len=*), intent(in) :: name !< variable name
915 character(len=*), intent(in) :: mem_path !< path where variable is stored
916 ! -- local
917 type(memorytype), pointer :: mt
918 integer(I4B) :: istat
919 integer(I4B) :: isize
920 ! -- code
921 !
922 ! -- check the variable name length
923 call mem_check_length(name, lenvarname, "variable")
924 !
925 ! -- set isize
926 isize = ncol * nrow * nlay
927 !
928 ! -- allocate the real array
929 allocate (adbl(ncol, nrow, nlay), stat=istat, errmsg=errmsg)
930 if (istat /= 0) then
931 call allocate_error(name, mem_path, istat, isize)
932 end if
933 !
934 ! -- update the counter
935 nvalues_adbl = nvalues_adbl + isize
936 !
937 ! -- allocate memory type
938 allocate (mt)
939 !
940 ! -- set memory type
941 mt%adbl3d => adbl
942 mt%element_size = dp
943 mt%isize = isize
944 mt%name = name
945 mt%path = mem_path
946 write (mt%memtype, "(a,' (',i0,',',i0,',',i0,')')") 'DOUBLE', ncol, &
947 nrow, nlay
948 !
949 ! -- add memory type to the memory list
950 call memorystore%add(mt)
951 end subroutine allocate_dbl3d
952
953 !> @brief Check in an existing 1d logical array with a new address (name + path)
954 !<
955 subroutine checkin_logical1d(alog, name, mem_path, name2, mem_path2)
956 logical(LGP), dimension(:), pointer, contiguous, intent(in) :: alog !< the existing array
957 character(len=*), intent(in) :: name !< new variable name
958 character(len=*), intent(in) :: mem_path !< new path where variable is stored
959 character(len=*), intent(in) :: name2 !< existing variable name
960 character(len=*), intent(in) :: mem_path2 !< existing path where variable is stored
961 ! --local
962 type(memorytype), pointer :: mt
963 integer(I4B) :: isize
964 ! -- code
965 !
966 ! -- check variable name length
967 call mem_check_length(name, lenvarname, "variable")
968 !
969 ! -- set isize
970 isize = size(alog)
971 !
972 ! -- allocate memory type
973 allocate (mt)
974 !
975 ! -- set memory type
976 mt%alogical1d => alog
977 mt%element_size = lgp
978 mt%isize = isize
979 mt%name = name
980 mt%path = mem_path
981 write (mt%memtype, "(a,' (',i0,')')") 'LOGICAL', isize
982 !
983 ! -- set master information
984 mt%master = .false.
985 mt%mastername = name2
986 mt%masterPath = mem_path2
987 !
988 ! -- add memory type to the memory list
989 call memorystore%add(mt)
990 end subroutine checkin_logical1d
991
992 !> @brief Check in an existing 1d integer array with a new address (name + path)
993 !<
994 subroutine checkin_int1d(aint, name, mem_path, name2, mem_path2)
995 integer(I4B), dimension(:), pointer, contiguous, intent(in) :: aint !< the existing array
996 character(len=*), intent(in) :: name !< new variable name
997 character(len=*), intent(in) :: mem_path !< new path where variable is stored
998 character(len=*), intent(in) :: name2 !< existing variable name
999 character(len=*), intent(in) :: mem_path2 !< existing path where variable is stored
1000 ! --local
1001 type(memorytype), pointer :: mt
1002 integer(I4B) :: isize
1003 ! -- code
1004 !
1005 ! -- check variable name length
1006 call mem_check_length(name, lenvarname, "variable")
1007 !
1008 ! -- set isize
1009 isize = size(aint)
1010 !
1011 ! -- allocate memory type
1012 allocate (mt)
1013 !
1014 ! -- set memory type
1015 mt%aint1d => aint
1016 mt%element_size = i4b
1017 mt%isize = isize
1018 mt%name = name
1019 mt%path = mem_path
1020 write (mt%memtype, "(a,' (',i0,')')") 'INTEGER', isize
1021 !
1022 ! -- set master information
1023 mt%master = .false.
1024 mt%mastername = name2
1025 mt%masterPath = mem_path2
1026 !
1027 ! -- add memory type to the memory list
1028 call memorystore%add(mt)
1029 end subroutine checkin_int1d
1030
1031 !> @brief Check in an existing 2d integer array with a new address (name + path)
1032 !<
1033 subroutine checkin_int2d(aint2d, name, mem_path, name2, mem_path2)
1034 integer(I4B), dimension(:, :), pointer, contiguous, intent(inout) :: aint2d !< the existing 2d array
1035 character(len=*), intent(in) :: name !< new variable name
1036 character(len=*), intent(in) :: mem_path !< new path where variable is stored
1037 character(len=*), intent(in) :: name2 !< existing variable name
1038 character(len=*), intent(in) :: mem_path2 !< existing path where variable is stored
1039 ! -- local
1040 type(memorytype), pointer :: mt
1041 integer(I4B) :: ncol, nrow, isize
1042 ! -- code
1043 !
1044 ! -- check the variable name length
1045 call mem_check_length(name, lenvarname, "variable")
1046 !
1047 ! -- set isize
1048 ncol = size(aint2d, dim=1)
1049 nrow = size(aint2d, dim=2)
1050 isize = ncol * nrow
1051 !
1052 ! -- allocate memory type
1053 allocate (mt)
1054 !
1055 ! -- set memory type
1056 mt%aint2d => aint2d
1057 mt%isize = isize
1058 mt%name = name
1059 mt%path = mem_path
1060 write (mt%memtype, "(a,' (',i0,',',i0,')')") 'INTEGER', ncol, nrow
1061 !
1062 ! -- set master information
1063 mt%master = .false.
1064 mt%mastername = name2
1065 mt%masterPath = mem_path2
1066 !
1067 ! -- add memory type to the memory list
1068 call memorystore%add(mt)
1069 end subroutine checkin_int2d
1070
1071 !> @brief Check in an existing 1d double precision array with a new address (name + path)
1072 !<
1073 subroutine checkin_dbl1d(adbl, name, mem_path, name2, mem_path2)
1074 real(DP), dimension(:), pointer, contiguous, intent(inout) :: adbl !< the existing array
1075 character(len=*), intent(in) :: name !< new variable name
1076 character(len=*), intent(in) :: mem_path !< new path where variable is stored
1077 character(len=*), intent(in) :: name2 !< existing variable name
1078 character(len=*), intent(in) :: mem_path2 !< existing path where variable is stored
1079 ! -- local
1080 type(memorytype), pointer :: mt
1081 integer(I4B) :: isize
1082 ! -- code
1083 !
1084 ! -- check the variable name length
1085 call mem_check_length(name, lenvarname, "variable")
1086 !
1087 ! -- set isize
1088 isize = size(adbl)
1089 !
1090 ! -- allocate memory type
1091 allocate (mt)
1092 !
1093 ! -- set memory type
1094 mt%adbl1d => adbl
1095 mt%element_size = dp
1096 mt%isize = isize
1097 mt%name = name
1098 mt%path = mem_path
1099 write (mt%memtype, "(a,' (',i0,')')") 'DOUBLE', isize
1100 !
1101 ! -- set master information
1102 mt%master = .false.
1103 mt%mastername = name2
1104 mt%masterPath = mem_path2
1105 !
1106 ! -- add memory type to the memory list
1107 call memorystore%add(mt)
1108 end subroutine checkin_dbl1d
1109
1110 !> @brief Check in an existing 2d double precision array with a new address (name + path)
1111 !<
1112 subroutine checkin_dbl2d(adbl2d, name, mem_path, name2, mem_path2)
1113 real(DP), dimension(:, :), pointer, contiguous, intent(inout) :: adbl2d !< the existing 2d array
1114 character(len=*), intent(in) :: name !< new variable name
1115 character(len=*), intent(in) :: mem_path !< new path where variable is stored
1116 character(len=*), intent(in) :: name2 !< existing variable name
1117 character(len=*), intent(in) :: mem_path2 !< existing path where variable is stored
1118 ! -- local
1119 type(memorytype), pointer :: mt
1120 integer(I4B) :: ncol, nrow, isize
1121 ! -- code
1122 !
1123 ! -- check the variable name length
1124 call mem_check_length(name, lenvarname, "variable")
1125 !
1126 ! -- set isize
1127 ncol = size(adbl2d, dim=1)
1128 nrow = size(adbl2d, dim=2)
1129 isize = ncol * nrow
1130 !
1131 ! -- allocate memory type
1132 allocate (mt)
1133 !
1134 ! -- set memory type
1135 mt%adbl2d => adbl2d
1136 mt%isize = isize
1137 mt%name = name
1138 mt%path = mem_path
1139 write (mt%memtype, "(a,' (',i0,',',i0,')')") 'DOUBLE', ncol, nrow
1140 !
1141 ! -- set master information
1142 mt%master = .false.
1143 mt%mastername = name2
1144 mt%masterPath = mem_path2
1145 !
1146 ! -- add memory type to the memory list
1147 call memorystore%add(mt)
1148 end subroutine checkin_dbl2d
1149
1150 !> @brief Check in an existing 1d CharacterStringType array with a new address (name + path)
1151 !<
1152 subroutine checkin_charstr1d(acharstr1d, ilen, name, mem_path, name2, mem_path2)
1153 type(characterstringtype), dimension(:), &
1154 pointer, contiguous, intent(inout) :: acharstr1d !< the existing array
1155 integer(I4B), intent(in) :: ilen
1156 character(len=*), intent(in) :: name !< new variable name
1157 character(len=*), intent(in) :: mem_path !< new path where variable is stored
1158 character(len=*), intent(in) :: name2 !< existing variable name
1159 character(len=*), intent(in) :: mem_path2 !< existing path where variable is stored
1160 ! --local
1161 type(memorytype), pointer :: mt
1162 integer(I4B) :: isize
1163 ! -- code
1164 !
1165 ! -- check variable name length
1166 call mem_check_length(name, lenvarname, "variable")
1167 !
1168 ! -- set isize
1169 isize = size(acharstr1d)
1170 !
1171 ! -- allocate memory type
1172 allocate (mt)
1173 !
1174 ! -- set memory type
1175 mt%acharstr1d => acharstr1d
1176 mt%element_size = ilen
1177 mt%isize = isize
1178 mt%name = name
1179 mt%path = mem_path
1180 write (mt%memtype, "(a,' LEN=',i0,' (',i0,')')") 'STRING', ilen, isize
1181 !
1182 ! -- set master information
1183 mt%master = .false.
1184 mt%mastername = name2
1185 mt%masterPath = mem_path2
1186 !
1187 ! -- add memory type to the memory list
1188 call memorystore%add(mt)
1189 end subroutine checkin_charstr1d
1190
1191 !> @brief Reallocate a 1-dimensional defined length string array
1192 !<
1193 subroutine reallocate_str1d(astr, ilen, nrow, name, mem_path)
1194 integer(I4B), intent(in) :: ilen !< string length
1195 integer(I4B), intent(in) :: nrow !< number of rows
1196 character(len=ilen), dimension(:), pointer, contiguous, intent(inout) :: astr !< the reallocated string array
1197 character(len=*), intent(in) :: name !< variable name
1198 character(len=*), intent(in) :: mem_path !< path where variable is stored
1199 ! -- local
1200 type(memorytype), pointer :: mt
1201 logical(LGP) :: found
1202 character(len=ilen), dimension(:), allocatable :: astrtemp
1203 integer(I4B) :: istat
1204 integer(I4B) :: isize
1205 integer(I4B) :: isize_old
1206 integer(I4B) :: nrow_old
1207 integer(I4B) :: n
1208 !
1209 ! -- Find and assign mt
1210 call get_from_memorystore(name, mem_path, mt, found)
1211 !
1212 ! -- reallocate astr1d
1213 if (found) then
1214 isize_old = mt%isize
1215 if (isize_old > 0) then
1216 nrow_old = size(astr)
1217 else
1218 nrow_old = 0
1219 end if
1220 !
1221 ! -- calculate isize
1222 isize = nrow
1223 !
1224 ! -- allocate astrtemp
1225 allocate (astrtemp(nrow), stat=istat, errmsg=errmsg)
1226 if (istat /= 0) then
1227 call allocate_error(name, mem_path, istat, isize)
1228 end if
1229 !
1230 ! -- copy existing values (only up to the new size to handle shrinking)
1231 do n = 1, min(nrow_old, nrow)
1232 astrtemp(n) = astr(n)
1233 end do
1234 !
1235 ! -- fill new values with missing values
1236 do n = nrow_old + 1, nrow
1237 astrtemp(n) = ''
1238 end do
1239 !
1240 ! -- deallocate mt pointer, repoint, recalculate isize
1241 deallocate (astr)
1242 !
1243 ! -- allocate astr1d
1244 allocate (astr(nrow), stat=istat, errmsg=errmsg)
1245 if (istat /= 0) then
1246 call allocate_error(name, mem_path, istat, isize)
1247 end if
1248 !
1249 ! -- fill the reallocate character array
1250 do n = 1, nrow
1251 astr(n) = astrtemp(n)
1252 end do
1253 !
1254 ! -- deallocate temporary storage
1255 deallocate (astrtemp)
1256 !
1257 ! -- reset memory manager values
1258 mt%astr1d => astr
1259 mt%element_size = ilen
1260 mt%isize = isize
1261 mt%nrealloc = mt%nrealloc + 1
1262 mt%master = .true.
1263 nvalues_astr = nvalues_astr + isize - isize_old
1264 write (mt%memtype, "(a,' LEN=',i0,' (',i0,')')") 'STRING', ilen, nrow
1265 else
1266 errmsg = "Programming error, variable '"//trim(name)//"' from '"// &
1267 trim(mem_path)//"' is not defined in the memory manager. Use "// &
1268 "mem_allocate instead."
1269 call store_error(errmsg, terminate=.true.)
1270 end if
1271 end subroutine reallocate_str1d
1272
1273 !> @brief Reallocate a 1-dimensional deferred length string array
1274 !<
1275 subroutine reallocate_charstr1d(acharstr1d, ilen, nrow, name, mem_path)
1276 type(characterstringtype), dimension(:), pointer, contiguous, &
1277 intent(inout) :: acharstr1d !< the reallocated charstring array
1278 integer(I4B), intent(in) :: ilen !< string length
1279 integer(I4B), intent(in) :: nrow !< number of rows
1280 character(len=*), intent(in) :: name !< variable name
1281 character(len=*), intent(in) :: mem_path !< path where variable is stored
1282 ! -- local
1283 type(memorytype), pointer :: mt
1284 logical(LGP) :: found
1285 type(characterstringtype), dimension(:), allocatable :: astrtemp
1286 character(len=ilen) :: string
1287 integer(I4B) :: istat
1288 integer(I4B) :: isize
1289 integer(I4B) :: isize_old
1290 integer(I4B) :: nrow_old
1291 integer(I4B) :: n
1292 !
1293 ! -- Initialize string
1294 string = ''
1295 !
1296 ! -- Find and assign mt
1297 call get_from_memorystore(name, mem_path, mt, found)
1298 !
1299 ! -- reallocate astr1d
1300 if (found) then
1301 isize_old = mt%isize
1302 if (isize_old > 0) then
1303 nrow_old = size(acharstr1d)
1304 else
1305 nrow_old = 0
1306 end if
1307 !
1308 ! -- calculate isize
1309 isize = nrow
1310 !
1311 ! -- allocate astrtemp
1312 allocate (astrtemp(nrow), stat=istat, errmsg=errmsg)
1313 if (istat /= 0) then
1314 call allocate_error(name, mem_path, istat, isize)
1315 end if
1316 !
1317 ! -- copy existing values (only up to the new size to handle shrinking)
1318 do n = 1, min(nrow_old, nrow)
1319 astrtemp(n) = acharstr1d(n)
1320 call acharstr1d(n)%destroy()
1321 end do
1322 !
1323 ! -- destroy remaining elements if shrinking
1324 do n = nrow + 1, nrow_old
1325 call acharstr1d(n)%destroy()
1326 end do
1327 !
1328 ! -- fill new values with missing values
1329 do n = nrow_old + 1, nrow
1330 astrtemp(n) = string
1331 end do
1332 !
1333 ! -- deallocate mt pointer, repoint, recalculate isize
1334 deallocate (acharstr1d)
1335 !
1336 ! -- allocate astr1d
1337 allocate (acharstr1d(nrow), stat=istat, errmsg=errmsg)
1338 if (istat /= 0) then
1339 call allocate_error(name, mem_path, istat, isize)
1340 end if
1341 !
1342 ! -- fill the reallocated character array
1343 do n = 1, nrow
1344 acharstr1d(n) = astrtemp(n)
1345 call astrtemp(n)%destroy()
1346 end do
1347 !
1348 ! -- deallocate temporary storage
1349 deallocate (astrtemp)
1350 !
1351 ! -- reset memory manager values
1352 mt%acharstr1d => acharstr1d
1353 mt%element_size = ilen
1354 mt%isize = isize
1355 mt%nrealloc = mt%nrealloc + 1
1356 mt%master = .true.
1357 nvalues_astr = nvalues_astr + isize - isize_old
1358 write (mt%memtype, "(a,' LEN=',i0,' (',i0,')')") 'STRING', ilen, nrow
1359 else
1360 errmsg = "Programming error, variable '"//trim(name)//"' from '"// &
1361 trim(mem_path)//"' is not defined in the memory manager. Use "// &
1362 "mem_allocate instead."
1363 call store_error(errmsg, terminate=.true.)
1364 end if
1365 end subroutine reallocate_charstr1d
1366
1367 !> @brief Reallocate a 1-dimensional logical array
1368 !<
1369 subroutine reallocate_logical1d(alog, nrow, name, mem_path)
1370 logical(LGP), dimension(:), pointer, contiguous, intent(inout) :: alog !< the reallocated logical array
1371 integer(I4B), intent(in) :: nrow !< number of rows
1372 character(len=*), intent(in) :: name !< variable name
1373 character(len=*), intent(in) :: mem_path !< path where variable is stored
1374 ! -- local
1375 type(memorytype), pointer :: mt
1376 logical(LGP) :: found
1377 integer(I4B) :: istat
1378 integer(I4B) :: isize
1379 integer(I4B) :: i
1380 integer(I4B) :: isizeold
1381 integer(I4B) :: ifill
1382 ! -- code
1383 !
1384 ! -- Find and assign mt
1385 call get_from_memorystore(name, mem_path, mt, found)
1386 !
1387 ! -- Allocate aint and then refill
1388 isize = nrow
1389 isizeold = size(mt%alogical1d)
1390 ifill = min(isizeold, isize)
1391 allocate (alog(nrow), stat=istat, errmsg=errmsg)
1392 if (istat /= 0) then
1393 call allocate_error(name, mem_path, istat, isize)
1394 end if
1395 do i = 1, ifill
1396 alog(i) = mt%alogical1d(i)
1397 end do
1398 !
1399 ! -- deallocate mt pointer, repoint, recalculate isize
1400 deallocate (mt%alogical1d)
1401 mt%alogical1d => alog
1402 mt%element_size = lgp
1403 mt%isize = isize
1404 mt%nrealloc = mt%nrealloc + 1
1405 mt%master = .true.
1406 nvalues_alogical = nvalues_alogical + isize - isizeold
1407 end subroutine reallocate_logical1d
1408
1409 !> @brief Reallocate a 1-dimensional integer array
1410 !<
1411 subroutine reallocate_int1d(aint, nrow, name, mem_path)
1412 integer(I4B), dimension(:), pointer, contiguous, intent(inout) :: aint !< the reallocated integer array
1413 integer(I4B), intent(in) :: nrow !< number of rows
1414 character(len=*), intent(in) :: name !< variable name
1415 character(len=*), intent(in) :: mem_path !< path where variable is stored
1416 ! -- local
1417 type(memorytype), pointer :: mt
1418 logical(LGP) :: found
1419 integer(I4B) :: istat
1420 integer(I4B) :: isize
1421 integer(I4B) :: i
1422 integer(I4B) :: isizeold
1423 integer(I4B) :: ifill
1424 ! -- code
1425 !
1426 ! -- Find and assign mt
1427 call get_from_memorystore(name, mem_path, mt, found)
1428 !
1429 ! -- Allocate aint and then refill
1430 isize = nrow
1431 isizeold = size(mt%aint1d)
1432 ifill = min(isizeold, isize)
1433 allocate (aint(nrow), stat=istat, errmsg=errmsg)
1434 if (istat /= 0) then
1435 call allocate_error(name, mem_path, istat, isize)
1436 end if
1437 do i = 1, ifill
1438 aint(i) = mt%aint1d(i)
1439 end do
1440 !
1441 ! -- deallocate mt pointer, repoint, recalculate isize
1442 deallocate (mt%aint1d)
1443 mt%aint1d => aint
1444 mt%element_size = i4b
1445 mt%isize = isize
1446 mt%nrealloc = mt%nrealloc + 1
1447 mt%master = .true.
1448 nvalues_aint = nvalues_aint + isize - isizeold
1449 end subroutine reallocate_int1d
1450
1451 !> @brief Reallocate a 2-dimensional integer array
1452 !<
1453 subroutine reallocate_int2d(aint, ncol, nrow, name, mem_path)
1454 integer(I4B), dimension(:, :), pointer, contiguous, intent(inout) :: aint !< the reallocated 2d integer array
1455 integer(I4B), intent(in) :: ncol !< number of columns
1456 integer(I4B), intent(in) :: nrow !< number of rows
1457 character(len=*), intent(in) :: name !< variable name
1458 character(len=*), intent(in) :: mem_path !< path where variable is stored
1459 ! -- local
1460 type(memorytype), pointer :: mt
1461 logical(LGP) :: found
1462 integer(I4B) :: istat
1463 integer(I4B), dimension(2) :: ishape
1464 integer(I4B) :: i
1465 integer(I4B) :: j
1466 integer(I4B) :: isize
1467 integer(I4B) :: isizeold
1468 ! -- code
1469 !
1470 ! -- Find and assign mt
1471 call get_from_memorystore(name, mem_path, mt, found)
1472 !
1473 ! -- Allocate aint and then refill
1474 ishape = shape(mt%aint2d)
1475 isize = nrow * ncol
1476 isizeold = ishape(1) * ishape(2)
1477 allocate (aint(ncol, nrow), stat=istat, errmsg=errmsg)
1478 if (istat /= 0) then
1479 call allocate_error(name, mem_path, istat, isize)
1480 end if
1481 ! Copy only what fits in the new array to handle shrinking
1482 do i = 1, min(ishape(2), nrow)
1483 do j = 1, min(ishape(1), ncol)
1484 aint(j, i) = mt%aint2d(j, i)
1485 end do
1486 end do
1487 !
1488 ! -- deallocate mt pointer, repoint, recalculate isize
1489 deallocate (mt%aint2d)
1490 mt%aint2d => aint
1491 mt%element_size = i4b
1492 mt%isize = isize
1493 mt%nrealloc = mt%nrealloc + 1
1494 mt%master = .true.
1495 nvalues_aint = nvalues_aint + isize - isizeold
1496 write (mt%memtype, "(a,' (',i0,',',i0,')')") 'INTEGER', ncol, nrow
1497 end subroutine reallocate_int2d
1498
1499 !> @brief Reallocate a 1-dimensional real array
1500 !<
1501 subroutine reallocate_dbl1d(adbl, nrow, name, mem_path)
1502 real(DP), dimension(:), pointer, contiguous, intent(inout) :: adbl !< the reallocated 1d real array
1503 integer(I4B), intent(in) :: nrow !< number of rows
1504 character(len=*), intent(in) :: name !< variable name
1505 character(len=*), intent(in) :: mem_path !< path where variable is stored
1506 ! -- local
1507 type(memorytype), pointer :: mt
1508 integer(I4B) :: istat
1509 integer(I4B) :: isize
1510 integer(I4B) :: i
1511 integer(I4B) :: isizeold
1512 integer(I4B) :: ifill
1513 logical(LGP) :: found
1514 ! -- code
1515 !
1516 ! -- Find and assign mt
1517 call get_from_memorystore(name, mem_path, mt, found)
1518 !
1519 ! -- Allocate adbl and then refill
1520 isize = nrow
1521 isizeold = size(mt%adbl1d)
1522 ifill = min(isizeold, isize)
1523 allocate (adbl(nrow), stat=istat, errmsg=errmsg)
1524 if (istat /= 0) then
1525 call allocate_error(name, mem_path, istat, isize)
1526 end if
1527 do i = 1, ifill
1528 adbl(i) = mt%adbl1d(i)
1529 end do
1530 !
1531 ! -- deallocate mt pointer, repoint, recalculate isize
1532 deallocate (mt%adbl1d)
1533 mt%adbl1d => adbl
1534 mt%element_size = dp
1535 mt%isize = isize
1536 mt%nrealloc = mt%nrealloc + 1
1537 mt%master = .true.
1538 nvalues_adbl = nvalues_adbl + isize - isizeold
1539 write (mt%memtype, "(a,' (',i0,')')") 'DOUBLE', isize
1540 end subroutine reallocate_dbl1d
1541
1542 !> @brief Reallocate a 2-dimensional real array
1543 !<
1544 subroutine reallocate_dbl2d(adbl, ncol, nrow, name, mem_path)
1545 real(DP), dimension(:, :), pointer, contiguous, intent(inout) :: adbl !< the reallocated 2d real array
1546 integer(I4B), intent(in) :: ncol !< number of columns
1547 integer(I4B), intent(in) :: nrow !< number of rows
1548 character(len=*), intent(in) :: name !< variable name
1549 character(len=*), intent(in) :: mem_path !< path where variable is stored
1550 ! -- local
1551 type(memorytype), pointer :: mt
1552 logical(LGP) :: found
1553 integer(I4B) :: istat
1554 integer(I4B), dimension(2) :: ishape
1555 integer(I4B) :: i
1556 integer(I4B) :: j
1557 integer(I4B) :: isize
1558 integer(I4B) :: isizeold
1559 ! -- code
1560 !
1561 ! -- Find and assign mt
1562 call get_from_memorystore(name, mem_path, mt, found)
1563 !
1564 ! -- Allocate adbl and then refill
1565 ishape = shape(mt%adbl2d)
1566 isize = nrow * ncol
1567 isizeold = ishape(1) * ishape(2)
1568 allocate (adbl(ncol, nrow), stat=istat, errmsg=errmsg)
1569 if (istat /= 0) then
1570 call allocate_error(name, mem_path, istat, isize)
1571 end if
1572 ! Copy only what fits in the new array to handle shrinking
1573 do i = 1, min(ishape(2), nrow)
1574 do j = 1, min(ishape(1), ncol)
1575 adbl(j, i) = mt%adbl2d(j, i)
1576 end do
1577 end do
1578 !
1579 ! -- deallocate mt pointer, repoint, recalculate isize
1580 deallocate (mt%adbl2d)
1581 mt%adbl2d => adbl
1582 mt%element_size = dp
1583 mt%isize = isize
1584 mt%nrealloc = mt%nrealloc + 1
1585 mt%master = .true.
1586 nvalues_adbl = nvalues_adbl + isize - isizeold
1587 write (mt%memtype, "(a,' (',i0,',',i0,')')") 'DOUBLE', ncol, nrow
1588 end subroutine reallocate_dbl2d
1589
1590 !> @brief Set pointer to a logical scalar
1591 !<
1592 subroutine setptr_logical(sclr, name, mem_path)
1593 logical(LGP), pointer, intent(inout) :: sclr !< pointer to logical scalar
1594 character(len=*), intent(in) :: name !< variable name
1595 character(len=*), intent(in) :: mem_path !< path where variable is stored
1596 ! -- local
1597 type(memorytype), pointer :: mt
1598 logical(LGP) :: found
1599 ! -- code
1600 call get_from_memorystore(name, mem_path, mt, found)
1601 sclr => mt%logicalsclr
1602 end subroutine setptr_logical
1603
1604 !> @brief Set pointer to integer scalar
1605 !<
1606 subroutine setptr_int(sclr, name, mem_path)
1607 integer(I4B), pointer, intent(inout) :: sclr !< pointer to integer scalar
1608 character(len=*), intent(in) :: name !< variable name
1609 character(len=*), intent(in) :: mem_path !< path where variable is stored
1610 ! -- local
1611 type(memorytype), pointer :: mt
1612 logical(LGP) :: found
1613 ! -- code
1614 call get_from_memorystore(name, mem_path, mt, found)
1615 sclr => mt%intsclr
1616 end subroutine setptr_int
1617
1618 !> @brief Set pointer to 1d logical array
1619 !<
1620 subroutine setptr_logical1d(alog, name, mem_path)
1621 logical(LGP), dimension(:), pointer, contiguous, intent(inout) :: alog !< pointer to 1d logical array
1622 character(len=*), intent(in) :: name !< variable name
1623 character(len=*), intent(in) :: mem_path !< path where variable is stored
1624 ! -- local
1625 type(memorytype), pointer :: mt
1626 logical(LGP) :: found
1627 ! -- code
1628 call get_from_memorystore(name, mem_path, mt, found)
1629 alog => mt%alogical1d
1630 end subroutine setptr_logical1d
1631
1632 !> @brief Set pointer to 1d integer array
1633 !<
1634 subroutine setptr_int1d(aint, name, mem_path)
1635 integer(I4B), dimension(:), pointer, contiguous, intent(inout) :: aint !< pointer to 1d integer array
1636 character(len=*), intent(in) :: name !< variable name
1637 character(len=*), intent(in) :: mem_path !< path where variable is stored
1638 ! -- local
1639 type(memorytype), pointer :: mt
1640 logical(LGP) :: found
1641 ! -- code
1642 call get_from_memorystore(name, mem_path, mt, found)
1643 aint => mt%aint1d
1644 end subroutine setptr_int1d
1645
1646 !> @brief Set pointer to 2d integer array
1647 !<
1648 subroutine setptr_int2d(aint, name, mem_path)
1649 integer(I4B), dimension(:, :), pointer, contiguous, intent(inout) :: aint !< pointer to 2d integer array
1650 character(len=*), intent(in) :: name !< variable name
1651 character(len=*), intent(in) :: mem_path !< path where variable is stored
1652 ! -- local
1653 type(memorytype), pointer :: mt
1654 logical(LGP) :: found
1655 ! -- code
1656 call get_from_memorystore(name, mem_path, mt, found)
1657 aint => mt%aint2d
1658 end subroutine setptr_int2d
1659
1660 !> @brief Set pointer to 3d integer array
1661 !<
1662 subroutine setptr_int3d(aint, name, mem_path)
1663 integer(I4B), dimension(:, :, :), pointer, contiguous, intent(inout) :: aint !< pointer to 3d integer array
1664 character(len=*), intent(in) :: name !< variable name
1665 character(len=*), intent(in) :: mem_path !< path where variable is stored
1666 ! -- local
1667 type(memorytype), pointer :: mt
1668 logical(LGP) :: found
1669 ! -- code
1670 call get_from_memorystore(name, mem_path, mt, found)
1671 aint => mt%aint3d
1672 end subroutine setptr_int3d
1673
1674 !> @brief Set pointer to a real scalar
1675 !<
1676 subroutine setptr_dbl(sclr, name, mem_path)
1677 real(DP), pointer, intent(inout) :: sclr !< pointer to a real scalar
1678 character(len=*), intent(in) :: name !< variable name
1679 character(len=*), intent(in) :: mem_path !< path where variable is stored
1680 ! -- local
1681 type(memorytype), pointer :: mt
1682 logical(LGP) :: found
1683 ! -- code
1684 call get_from_memorystore(name, mem_path, mt, found)
1685 sclr => mt%dblsclr
1686 end subroutine setptr_dbl
1687
1688 !> @brief Set pointer to a 1d real array
1689 !<
1690 subroutine setptr_dbl1d(adbl, name, mem_path)
1691 real(DP), dimension(:), pointer, contiguous, intent(inout) :: adbl !< pointer to 1d real array
1692 character(len=*), intent(in) :: name !< variable name
1693 character(len=*), intent(in) :: mem_path !< path where variable is stored
1694 ! -- local
1695 type(memorytype), pointer :: mt
1696 logical(LGP) :: found
1697 ! -- code
1698 call get_from_memorystore(name, mem_path, mt, found)
1699 adbl => mt%adbl1d
1700 end subroutine setptr_dbl1d
1701
1702 !> @brief Set pointer to a 2d real array
1703 !<
1704 subroutine setptr_dbl2d(adbl, name, mem_path)
1705 real(DP), dimension(:, :), pointer, contiguous, intent(inout) :: adbl !< pointer to 2d real array
1706 character(len=*), intent(in) :: name !< variable name
1707 character(len=*), intent(in) :: mem_path !< path where variable is stored
1708 ! -- local
1709 type(memorytype), pointer :: mt
1710 logical(LGP) :: found
1711 ! -- code
1712 call get_from_memorystore(name, mem_path, mt, found)
1713 adbl => mt%adbl2d
1714 end subroutine setptr_dbl2d
1715
1716 !> @brief Set pointer to a 3d real array
1717 !<
1718 subroutine setptr_dbl3d(adbl, name, mem_path)
1719 real(DP), dimension(:, :, :), pointer, contiguous, intent(inout) :: adbl !< pointer to 3d real array
1720 character(len=*), intent(in) :: name !< variable name
1721 character(len=*), intent(in) :: mem_path !< path where variable is stored
1722 ! -- local
1723 type(memorytype), pointer :: mt
1724 logical(LGP) :: found
1725 ! -- code
1726 call get_from_memorystore(name, mem_path, mt, found)
1727 adbl => mt%adbl3d
1728 end subroutine setptr_dbl3d
1729
1730 !> @brief Set pointer to a string (scalar)
1731 !<
1732 subroutine setptr_str(asrt, name, mem_path)
1733 character(len=:), pointer :: asrt !< pointer to the character string
1734 character(len=*), intent(in) :: name !< variable name
1735 character(len=*), intent(in) :: mem_path !< path where variable is stored
1736 ! -- local
1737 type(memorytype), pointer :: mt
1738 logical(LGP) :: found
1739 ! -- code
1740 call get_from_memorystore(name, mem_path, mt, found)
1741 asrt => mt%strsclr
1742 end subroutine setptr_str
1743
1744 !> @brief Set pointer to a fixed-length string array
1745 !<
1746 subroutine setptr_str1d(astr1d, name, mem_path)
1747 character(len=:), dimension(:), &
1748 pointer, contiguous, intent(inout) :: astr1d !< pointer to the string array
1749 character(len=*), intent(in) :: name !< variable name
1750 character(len=*), intent(in) :: mem_path !< path where variable is stored
1751 ! -- local
1752 type(memorytype), pointer :: mt
1753 logical(LGP) :: found
1754 ! -- code
1755 call get_from_memorystore(name, mem_path, mt, found)
1756 select type (item => mt%astr1d)
1757 type is (character(*))
1758 astr1d => item
1759 class default
1760 astr1d => null()
1761 end select
1762 end subroutine setptr_str1d
1763
1764 !> @brief Set pointer to an array of CharacterStringType
1765 !<
1766 subroutine setptr_charstr1d(acharstr1d, name, mem_path)
1767 type(characterstringtype), dimension(:), pointer, contiguous, &
1768 intent(inout) :: acharstr1d !< the reallocated charstring array
1769 character(len=*), intent(in) :: name !< variable name
1770 character(len=*), intent(in) :: mem_path !< path where variable is stored
1771 ! -- local
1772 type(memorytype), pointer :: mt
1773 logical(LGP) :: found
1774 ! -- code
1775 call get_from_memorystore(name, mem_path, mt, found)
1776 acharstr1d => mt%acharstr1d
1777 end subroutine setptr_charstr1d
1778
1779 !> @brief Make a copy of a 1-dimensional logical array
1780 !<
1781 subroutine copyptr_logical1d(alog, name, mem_path, mem_path_copy)
1782 logical(LGP), dimension(:), pointer, contiguous, intent(inout) :: alog !< returned copy of 1d logical array
1783 character(len=*), intent(in) :: name !< variable name
1784 character(len=*), intent(in) :: mem_path !< path where variable is stored
1785 character(len=*), intent(in), optional :: mem_path_copy !< optional path where the copy will be stored,
1786 !! if passed then the copy is added to the
1787 !! memory manager
1788 ! -- local
1789 type(memorytype), pointer :: mt
1790 logical(LGP) :: found
1791 integer(I4B) :: n
1792 ! -- code
1793 call get_from_memorystore(name, mem_path, mt, found)
1794 alog => null()
1795 ! -- check the copy into the memory manager
1796 if (present(mem_path_copy)) then
1797 call allocate_logical1d(alog, size(mt%alogical1d), mt%name, mem_path_copy)
1798 ! -- create a local copy
1799 else
1800 allocate (alog(size(mt%alogical1d)))
1801 end if
1802 do n = 1, size(mt%alogical1d)
1803 alog(n) = mt%alogical1d(n)
1804 end do
1805 end subroutine copyptr_logical1d
1806
1807 !> @brief Make a copy of a 1-dimensional integer array
1808 !<
1809 subroutine copyptr_int1d(aint, name, mem_path, mem_path_copy)
1810 integer(I4B), dimension(:), pointer, contiguous, intent(inout) :: aint !< returned copy of 1d integer array
1811 character(len=*), intent(in) :: name !< variable name
1812 character(len=*), intent(in) :: mem_path !< path where variable is stored
1813 character(len=*), intent(in), optional :: mem_path_copy !< optional path where the copy will be stored,
1814 !! if passed then the copy is added to the
1815 !! memory manager
1816 ! -- local
1817 type(memorytype), pointer :: mt
1818 logical(LGP) :: found
1819 integer(I4B) :: n
1820 ! -- code
1821 call get_from_memorystore(name, mem_path, mt, found)
1822 aint => null()
1823 ! -- check the copy into the memory manager
1824 if (present(mem_path_copy)) then
1825 call allocate_int1d(aint, size(mt%aint1d), mt%name, mem_path_copy)
1826 ! -- create a local copy
1827 else
1828 allocate (aint(size(mt%aint1d)))
1829 end if
1830 do n = 1, size(mt%aint1d)
1831 aint(n) = mt%aint1d(n)
1832 end do
1833 end subroutine copyptr_int1d
1834
1835 !> @brief Make a copy of a 2-dimensional integer array
1836 !<
1837 subroutine copyptr_int2d(aint, name, mem_path, mem_path_copy)
1838 integer(I4B), dimension(:, :), pointer, contiguous, intent(inout) :: aint !< returned copy of 2d integer array
1839 character(len=*), intent(in) :: name !< variable name
1840 character(len=*), intent(in) :: mem_path !< path where variable is stored
1841 character(len=*), intent(in), optional :: mem_path_copy !< optional path where the copy will be stored,
1842 !! if passed then the copy is added to the
1843 !! memory manager
1844 ! -- local
1845 type(memorytype), pointer :: mt
1846 logical(LGP) :: found
1847 integer(I4B) :: i
1848 integer(I4B) :: j
1849 integer(I4B) :: ncol
1850 integer(I4B) :: nrow
1851 ! -- code
1852 call get_from_memorystore(name, mem_path, mt, found)
1853 aint => null()
1854 ncol = size(mt%aint2d, dim=1)
1855 nrow = size(mt%aint2d, dim=2)
1856 ! -- check the copy into the memory manager
1857 if (present(mem_path_copy)) then
1858 call allocate_int2d(aint, ncol, nrow, mt%name, mem_path_copy)
1859 ! -- create a local copy
1860 else
1861 allocate (aint(ncol, nrow))
1862 end if
1863 do i = 1, nrow
1864 do j = 1, ncol
1865 aint(j, i) = mt%aint2d(j, i)
1866 end do
1867 end do
1868 end subroutine copyptr_int2d
1869
1870 !> @brief Make a copy of a 1-dimensional real array
1871 !<
1872 subroutine copyptr_dbl1d(adbl, name, mem_path, mem_path_copy)
1873 real(DP), dimension(:), pointer, contiguous, intent(inout) :: adbl !< returned copy of 1d real array
1874 character(len=*), intent(in) :: name !< variable name
1875 character(len=*), intent(in) :: mem_path !< path where variable is stored
1876 character(len=*), intent(in), optional :: mem_path_copy !< optional path where the copy will be stored,
1877 !! if passed then the copy is added to the
1878 !! memory manager
1879 ! -- local
1880 type(memorytype), pointer :: mt
1881 logical(LGP) :: found
1882 integer(I4B) :: n
1883 ! -- code
1884 call get_from_memorystore(name, mem_path, mt, found)
1885 adbl => null()
1886 ! -- check the copy into the memory manager
1887 if (present(mem_path_copy)) then
1888 call allocate_dbl1d(adbl, size(mt%adbl1d), mt%name, mem_path_copy)
1889 ! -- create a local copy
1890 else
1891 allocate (adbl(size(mt%adbl1d)))
1892 end if
1893 do n = 1, size(mt%adbl1d)
1894 adbl(n) = mt%adbl1d(n)
1895 end do
1896 end subroutine copyptr_dbl1d
1897
1898 !> @brief Make a copy of a 2-dimensional real array
1899 !<
1900 subroutine copyptr_dbl2d(adbl, name, mem_path, mem_path_copy)
1901 real(DP), dimension(:, :), pointer, contiguous, intent(inout) :: adbl !< returned copy of 2d real array
1902 character(len=*), intent(in) :: name !< variable name
1903 character(len=*), intent(in) :: mem_path !< path where variable is stored
1904 character(len=*), intent(in), optional :: mem_path_copy !< optional path where the copy will be stored,
1905 !! if passed then the copy is added to the
1906 !! memory manager
1907 ! -- local
1908 type(memorytype), pointer :: mt
1909 logical(LGP) :: found
1910 integer(I4B) :: i
1911 integer(I4B) :: j
1912 integer(I4B) :: ncol
1913 integer(I4B) :: nrow
1914 ! -- code
1915 call get_from_memorystore(name, mem_path, mt, found)
1916 adbl => null()
1917 ncol = size(mt%adbl2d, dim=1)
1918 nrow = size(mt%adbl2d, dim=2)
1919 ! -- check the copy into the memory manager
1920 if (present(mem_path_copy)) then
1921 call allocate_dbl2d(adbl, ncol, nrow, mt%name, mem_path_copy)
1922 ! -- create a local copy
1923 else
1924 allocate (adbl(ncol, nrow))
1925 end if
1926 do i = 1, nrow
1927 do j = 1, ncol
1928 adbl(j, i) = mt%adbl2d(j, i)
1929 end do
1930 end do
1931 end subroutine copyptr_dbl2d
1932
1933 !> @brief Copy values from a 1-dimensional real array in the memory
1934 !< manager to a passed 1-dimensional real array
1935 subroutine copy_dbl1d(adbl, name, mem_path)
1936 real(dp), dimension(:), intent(inout) :: adbl !< target array
1937 character(len=*), intent(in) :: name !< variable name
1938 character(len=*), intent(in) :: mem_path !< path where variable is stored
1939 ! -- local
1940 type(memorytype), pointer :: mt
1941 logical(LGP) :: found
1942 integer(I4B) :: n
1943 ! -- code
1944 call get_from_memorystore(name, mem_path, mt, found)
1945 do n = 1, size(mt%adbl1d)
1946 adbl(n) = mt%adbl1d(n)
1947 end do
1948 end subroutine copy_dbl1d
1949
1950 !> @brief Set the pointer for an integer scalar to
1951 !< a target array already stored in the memory manager
1952 subroutine reassignptr_int(sclr, name, mem_path, name_target, mem_path_target)
1953 integer(I4B), pointer, intent(inout) :: sclr !< pointer to integer scalar
1954 character(len=*), intent(in) :: name !< variable name
1955 character(len=*), intent(in) :: mem_path !< path where variable is stored
1956 character(len=*), intent(in) :: name_target !< name of target variable
1957 character(len=*), intent(in) :: mem_path_target !< path where target variable is stored
1958 ! -- local
1959 type(memorytype), pointer :: mt
1960 type(memorytype), pointer :: mt2
1961 logical(LGP) :: found
1962 ! -- code
1963 call get_from_memorystore(name, mem_path, mt, found)
1964 call get_from_memorystore(name_target, mem_path_target, mt2, found)
1965 if (associated(sclr)) then
1967 deallocate (sclr)
1968 end if
1969 sclr => mt2%intsclr
1970 mt%intsclr => sclr
1971 mt%element_size = i4b
1972 mt%isize = 1
1973 write (mt%memtype, "(a,' (',i0,')')") 'INTEGER', mt%isize
1974 !
1975 ! -- set master information
1976 mt%master = .false.
1977 mt%mastername = name_target
1978 mt%masterPath = mem_path_target
1979 end subroutine reassignptr_int
1980
1981 !> @brief Set the pointer for a 1-dimensional logical array to
1982 !< a target array already stored in the memory manager
1983 subroutine reassignptr_logical1d(alog, name, mem_path, name_target, &
1984 mem_path_target)
1985 logical(LGP), dimension(:), pointer, contiguous, intent(inout) :: alog !< array pointer
1986 character(len=*), intent(in) :: name !< variable name
1987 character(len=*), intent(in) :: mem_path !< path where variable is stored
1988 character(len=*), intent(in) :: name_target !< name of target variable
1989 character(len=*), intent(in) :: mem_path_target !< path where target variable is stored
1990 ! -- local
1991 type(memorytype), pointer :: mt
1992 type(memorytype), pointer :: mt2
1993 logical(LGP) :: found
1994 ! -- code
1995 call get_from_memorystore(name, mem_path, mt, found)
1996 call get_from_memorystore(name_target, mem_path_target, mt2, found)
1997 if (size(alog) > 0) then
1998 nvalues_alogical = nvalues_alogical - size(alog)
1999 deallocate (alog)
2000 end if
2001 alog => mt2%alogical1d
2002 mt%alogical1d => alog
2003 mt%element_size = lgp
2004 mt%isize = size(alog)
2005 write (mt%memtype, "(a,' (',i0,')')") 'LOGICAL', mt%isize
2006 !
2007 ! -- set master information
2008 mt%master = .false.
2009 mt%mastername = name_target
2010 mt%masterPath = mem_path_target
2011 end subroutine reassignptr_logical1d
2012
2013 !> @brief Set the pointer for a 1-dimensional integer array to
2014 !< a target array already stored in the memory manager
2015 subroutine reassignptr_int1d(aint, name, mem_path, name_target, mem_path_target)
2016 integer(I4B), dimension(:), pointer, contiguous, intent(inout) :: aint !< pointer to 1d integer array
2017 character(len=*), intent(in) :: name !< variable name
2018 character(len=*), intent(in) :: mem_path !< path where variable is stored
2019 character(len=*), intent(in) :: name_target !< name of target variable
2020 character(len=*), intent(in) :: mem_path_target !< path where target variable is stored
2021 ! -- local
2022 type(memorytype), pointer :: mt
2023 type(memorytype), pointer :: mt2
2024 logical(LGP) :: found
2025 ! -- code
2026 call get_from_memorystore(name, mem_path, mt, found)
2027 call get_from_memorystore(name_target, mem_path_target, mt2, found)
2028 if (size(aint) > 0) then
2029 nvalues_aint = nvalues_aint - size(aint)
2030 deallocate (aint)
2031 end if
2032 aint => mt2%aint1d
2033 mt%aint1d => aint
2034 mt%element_size = i4b
2035 mt%isize = size(aint)
2036 write (mt%memtype, "(a,' (',i0,')')") 'INTEGER', mt%isize
2037 !
2038 ! -- set master information
2039 mt%master = .false.
2040 mt%mastername = name_target
2041 mt%masterPath = mem_path_target
2042 end subroutine reassignptr_int1d
2043
2044 !> @brief Set the pointer for a 2-dimensional integer array to
2045 !< a target array already stored in the memory manager
2046 subroutine reassignptr_int2d(aint, name, mem_path, name_target, mem_path_target)
2047 integer(I4B), dimension(:, :), pointer, contiguous, intent(inout) :: aint !< pointer to 2d integer array
2048 character(len=*), intent(in) :: name !< variable name
2049 character(len=*), intent(in) :: mem_path !< path where variable is stored
2050 character(len=*), intent(in) :: name_target !< name of target variable
2051 character(len=*), intent(in) :: mem_path_target !< path where target variable is stored
2052 ! -- local
2053 type(memorytype), pointer :: mt
2054 type(memorytype), pointer :: mt2
2055 logical(LGP) :: found
2056 integer(I4B) :: ncol
2057 integer(I4B) :: nrow
2058 ! -- code
2059 call get_from_memorystore(name, mem_path, mt, found)
2060 call get_from_memorystore(name_target, mem_path_target, mt2, found)
2061 if (size(aint) > 0) then
2062 nvalues_aint = nvalues_aint - size(aint)
2063 deallocate (aint)
2064 end if
2065 aint => mt2%aint2d
2066 mt%aint2d => aint
2067 mt%element_size = i4b
2068 mt%isize = size(aint)
2069 ncol = size(aint, dim=1)
2070 nrow = size(aint, dim=2)
2071 write (mt%memtype, "(a,' (',i0,',',i0,')')") 'INTEGER', ncol, nrow
2072 !
2073 ! -- set master information
2074 mt%master = .false.
2075 mt%mastername = name_target
2076 mt%masterPath = mem_path_target
2077 end subroutine reassignptr_int2d
2078
2079 !> @brief Set the pointer for a 1-dimensional real array to
2080 !< a target array already stored in the memory manager
2081 subroutine reassignptr_dbl1d(adbl, name, mem_path, name_target, mem_path_target)
2082 real(DP), dimension(:), pointer, contiguous, intent(inout) :: adbl !< pointer to 1d real array
2083 character(len=*), intent(in) :: name !< variable name
2084 character(len=*), intent(in) :: mem_path !< path where variable is stored
2085 character(len=*), intent(in) :: name_target !< name of target variable
2086 character(len=*), intent(in) :: mem_path_target !< path where target variable is stored
2087 ! -- local
2088 type(memorytype), pointer :: mt
2089 type(memorytype), pointer :: mt2
2090 logical(LGP) :: found
2091 ! -- code
2092 call get_from_memorystore(name, mem_path, mt, found)
2093 call get_from_memorystore(name_target, mem_path_target, mt2, found)
2094 if (size(adbl) > 0) then
2095 nvalues_adbl = nvalues_adbl - size(adbl)
2096 deallocate (adbl)
2097 end if
2098 adbl => mt2%adbl1d
2099 mt%adbl1d => adbl
2100 mt%element_size = dp
2101 mt%isize = size(adbl)
2102 write (mt%memtype, "(a,' (',i0,')')") 'DOUBLE', mt%isize
2103 !
2104 ! -- set master information
2105 mt%master = .false.
2106 mt%mastername = name_target
2107 mt%masterPath = mem_path_target
2108 end subroutine reassignptr_dbl1d
2109
2110 !> @brief Set the pointer for a 2-dimensional real array to
2111 !< a target array already stored in the memory manager
2112 subroutine reassignptr_dbl2d(adbl, name, mem_path, name_target, mem_path_target)
2113 real(DP), dimension(:, :), pointer, contiguous, intent(inout) :: adbl !< pointer to 2d real array
2114 character(len=*), intent(in) :: name !< variable name
2115 character(len=*), intent(in) :: mem_path !< path where variable is stored
2116 character(len=*), intent(in) :: name_target !< name of target variable
2117 character(len=*), intent(in) :: mem_path_target !< path where target variable is stored
2118 ! -- local
2119 type(memorytype), pointer :: mt
2120 type(memorytype), pointer :: mt2
2121 logical(LGP) :: found
2122 integer(I4B) :: ncol
2123 integer(I4b) :: nrow
2124 ! -- code
2125 call get_from_memorystore(name, mem_path, mt, found)
2126 call get_from_memorystore(name_target, mem_path_target, mt2, found)
2127 if (size(adbl) > 0) then
2128 nvalues_adbl = nvalues_adbl - size(adbl)
2129 deallocate (adbl)
2130 end if
2131 adbl => mt2%adbl2d
2132 mt%adbl2d => adbl
2133 mt%element_size = dp
2134 mt%isize = size(adbl)
2135 ncol = size(adbl, dim=1)
2136 nrow = size(adbl, dim=2)
2137 write (mt%memtype, "(a,' (',i0,',',i0,')')") 'DOUBLE', ncol, nrow
2138 !
2139 ! -- set master information
2140 mt%master = .false.
2141 mt%mastername = name_target
2142 mt%masterPath = mem_path_target
2143 end subroutine reassignptr_dbl2d
2144
2145 !> @brief DEPRECATED. The memory manager will handle the deallocation of the pointer.
2146 !<
2147 subroutine deallocate_str(sclr, name, mem_path)
2148 character(len=*), pointer, intent(inout) :: sclr !< pointer to string
2149 character(len=*), intent(in), optional :: name !< variable name
2150 character(len=*), intent(in), optional :: mem_path !< path where variable is stored
2151 ! -- code
2152 return
2153 end subroutine deallocate_str
2154
2155 !> @brief Deallocate an array of defined-length character strings
2156 !!
2157 !<
2158 subroutine deallocate_str1d(astr1d, name, mem_path)
2159 character(len=*), dimension(:), pointer, contiguous, intent(inout) :: astr1d !< array of strings
2160 character(len=*), optional, intent(in) :: name !< variable name
2161 character(len=*), optional, intent(in) :: mem_path !< path where variable is stored
2162 ! -- code
2163 return
2164
2165 end subroutine deallocate_str1d
2166
2167 !> @brief DEPRECATED. The memory manager will handle the deallocation of the pointer.
2168 !!
2169 !<
2170 subroutine deallocate_charstr1d(astr1d, name, mem_path)
2171 type(characterstringtype), dimension(:), pointer, contiguous, &
2172 intent(inout) :: astr1d !< array of strings
2173 character(len=*), optional, intent(in) :: name !< variable name
2174 character(len=*), optional, intent(in) :: mem_path !< path where variable is stored
2175 ! -- code
2176 return
2177 end subroutine deallocate_charstr1d
2178
2179 !> @brief DEPRECATED. The memory manager will handle the deallocation of the pointer.
2180 !<
2181 subroutine deallocate_logical(sclr)
2182 logical(LGP), pointer, intent(inout) :: sclr !< logical scalar to deallocate
2183 ! -- code
2184 return
2185 end subroutine deallocate_logical
2186
2187 !> @brief DEPRECATED. The memory manager will handle the deallocation of the pointer.
2188 !<
2189 subroutine deallocate_int(sclr)
2190 integer(I4B), pointer, intent(inout) :: sclr !< integer variable to deallocate
2191 ! -- code
2192 return
2193 end subroutine deallocate_int
2194
2195 !> @brief DEPRECATED. The memory manager will handle the deallocation of the pointer.
2196 !<
2197 subroutine deallocate_dbl(sclr)
2198 real(DP), pointer, intent(inout) :: sclr !< real variable to deallocate
2199 ! -- code
2200 return
2201 end subroutine deallocate_dbl
2202
2203 !> @brief DEPRECATED. The memory manager will handle the deallocation of the pointer.
2204 !<
2205 subroutine deallocate_logical1d(alog, name, mem_path)
2206 logical(LGP), dimension(:), pointer, contiguous, intent(inout) :: alog !< 1d logical array to deallocate
2207 character(len=*), optional :: name !< variable name
2208 character(len=*), optional :: mem_path !< path where variable is stored
2209 ! -- code
2210 return
2211 end subroutine deallocate_logical1d
2212
2213 !> @brief DEPRECATED. The memory manager will handle the deallocation of the pointer.
2214 !<
2215 subroutine deallocate_int1d(aint, name, mem_path)
2216 integer(I4B), dimension(:), pointer, contiguous, intent(inout) :: aint !< 1d integer array to deallocate
2217 character(len=*), optional :: name !< variable name
2218 character(len=*), optional :: mem_path !< path where variable is stored
2219 ! -- code
2220 return
2221 end subroutine deallocate_int1d
2222
2223 !> @brief DEPRECATED. The memory manager will handle the deallocation of the pointer.
2224 !<
2225 subroutine deallocate_int2d(aint, name, mem_path)
2226 integer(I4B), dimension(:, :), pointer, contiguous, intent(inout) :: aint !< 2d integer array to deallocate
2227 character(len=*), optional :: name !< variable name
2228 character(len=*), optional :: mem_path !< path where variable is stored
2229 ! -- code
2230 return
2231 end subroutine deallocate_int2d
2232
2233 !> @brief DEPRECATED. The memory manager will handle the deallocation of the pointer.
2234 !<
2235 subroutine deallocate_int3d(aint, name, mem_path)
2236 integer(I4B), dimension(:, :, :), pointer, contiguous, intent(inout) :: aint !< 3d integer array to deallocate
2237 character(len=*), optional :: name !< variable name
2238 character(len=*), optional :: mem_path !< path where variable is stored
2239 ! -- code
2240 return
2241 end subroutine deallocate_int3d
2242
2243 !> @brief DEPRECATED. The memory manager will handle the deallocation of the pointer.
2244 !<
2245 subroutine deallocate_dbl1d(adbl, name, mem_path)
2246 real(DP), dimension(:), pointer, contiguous, intent(inout) :: adbl !< 1d real array to deallocate
2247 character(len=*), optional :: name !< variable name
2248 character(len=*), optional :: mem_path !< path where variable is stored
2249 ! -- code
2250 return
2251 end subroutine deallocate_dbl1d
2252
2253 !> @brief DEPRECATED. The memory manager will handle the deallocation of the pointer.
2254 !<
2255 subroutine deallocate_dbl2d(adbl, name, mem_path)
2256 real(DP), dimension(:, :), pointer, contiguous, intent(inout) :: adbl !< 2d real array to deallocate
2257 character(len=*), optional :: name !< variable name
2258 character(len=*), optional :: mem_path !< path where variable is stored
2259 ! -- code
2260 return
2261 end subroutine deallocate_dbl2d
2262
2263 !> @brief DEPRECATED. The memory manager will handle the deallocation of the pointer.
2264 !<
2265 subroutine deallocate_dbl3d(adbl, name, mem_path)
2266 real(DP), dimension(:, :, :), pointer, contiguous, intent(inout) :: adbl !< 3d real array to deallocate
2267 character(len=*), optional :: name !< variable name
2268 character(len=*), optional :: mem_path !< path where variable is stored
2269 ! -- code
2270 return
2271 end subroutine deallocate_dbl3d
2272
2273 !> @brief Set the memory print option
2274 !<
2275 subroutine mem_set_print_option(iout, keyword, error_msg)
2276 integer(I4B), intent(in) :: iout !< unit number for mfsim.lst
2277 character(len=*), intent(in) :: keyword !< memory print option
2278 character(len=*), intent(inout) :: error_msg !< returned error message if keyword is not valid option
2279 ! -- local
2280 ! -- format
2281 ! -- code
2282 select case (keyword)
2283 case ('NONE')
2284 iprmem = 0
2285 write (iout, '(4x, a)') &
2286 'LIMITED MEMORY INFORMATION WILL BE WRITTEN.'
2287 case ('SUMMARY')
2288 iprmem = 1
2289 write (iout, '(4x, a)') &
2290 'A SUMMARY OF SIMULATION MEMORY INFORMATION WILL BE WRITTEN.'
2291 case ('ALL')
2292 iprmem = 2
2293 write (iout, '(4x, a)') &
2294 'ALL SIMULATION MEMORY INFORMATION WILL BE WRITTEN.'
2295 case default
2296 error_msg = "Unknown memory print option '"//trim(keyword)//"."
2297 end select
2298 end subroutine mem_set_print_option
2299
2300 !> @brief Create a table if memory_print_option is 'SUMMARY'
2301 !<
2302 subroutine mem_summary_table(iout, nrows, cunits)
2303 integer(I4B), intent(in) :: iout !< unit number for mfsim.lst
2304 integer(I4B), intent(in) :: nrows !< number of table rows
2305 character(len=*), intent(in) :: cunits !< memory units (bytes, kilobytes, megabytes, or gigabytes)
2306 ! -- local
2307 character(len=LINELENGTH) :: title
2308 character(len=LINELENGTH) :: text
2309 integer(I4B) :: nterms
2310 ! -- formats
2311 ! -- code
2312 nterms = 6
2313 !
2314 ! -- set up table title
2315 title = 'SUMMARY INFORMATION ON VARIABLES STORED IN THE MEMORY MANAGER, '// &
2316 'IN '//trim(cunits)
2317 !
2318 ! -- set up stage tableobj
2319 call table_cr(memtab, 'MEM SUM', title)
2320 call memtab%table_df(nrows, nterms, iout)
2321 !
2322 ! -- data type
2323 text = 'COMPONENT'
2324 call memtab%initialize_column(text, 20, alignment=tableft)
2325 !
2326 ! -- memory allocated for characters
2327 text = 'CHARACTER'
2328 call memtab%initialize_column(text, 15, alignment=tabcenter)
2329 !
2330 ! -- memory allocated for logical
2331 text = 'LOGICAL'
2332 call memtab%initialize_column(text, 15, alignment=tabcenter)
2333 !
2334 ! -- memory allocated for integers
2335 text = 'INTEGER'
2336 call memtab%initialize_column(text, 15, alignment=tabcenter)
2337 !
2338 ! -- memory allocated for reals
2339 text = 'REAL'
2340 call memtab%initialize_column(text, 15, alignment=tabcenter)
2341 !
2342 ! -- total memory allocated
2343 text = 'TOTAL'
2344 call memtab%initialize_column(text, 15, alignment=tabcenter)
2345 end subroutine mem_summary_table
2346
2347 !> @brief Create a table if memory_print_option is 'ALL'
2348 !<
2349 subroutine mem_detailed_table(iout, nrows)
2350 integer(I4B), intent(in) :: iout !< unit number for mfsim.lst
2351 integer(I4B), intent(in) :: nrows !< number of table rows
2352 ! -- local
2353 character(len=LINELENGTH) :: title
2354 character(len=LINELENGTH) :: text
2355 integer(I4B) :: nterms
2356 ! -- formats
2357 ! -- code
2358 nterms = 5
2359 !
2360 ! -- set up table title
2361 title = 'DETAILED INFORMATION ON VARIABLES STORED IN THE MEMORY MANAGER'
2362 !
2363 ! -- set up stage tableobj
2364 call table_cr(memtab, 'MEM DET', title)
2365 call memtab%table_df(nrows, nterms, iout)
2366 !
2367 ! -- origin
2368 text = 'ORIGIN'
2369 call memtab%initialize_column(text, lenmempath, alignment=tableft)
2370 !
2371 ! -- variable
2372 text = 'VARIABLE NAME'
2373 call memtab%initialize_column(text, lenvarname, alignment=tableft)
2374 !
2375 ! -- data type
2376 text = 'DATA TYPE'
2377 call memtab%initialize_column(text, 16, alignment=tableft)
2378 !
2379 ! -- size
2380 text = 'NUMBER OF ITEMS'
2381 call memtab%initialize_column(text, 20, alignment=tabright)
2382 !
2383 ! -- is it a pointer
2384 text = 'ASSOCIATED VARIABLE'
2385 call memtab%initialize_column(text, lenmemaddress, alignment=tableft)
2386 end subroutine mem_detailed_table
2387
2388 !> @brief Write a row for the memory_print_option 'SUMMARY' table
2389 !<
2390 subroutine mem_summary_line(component, rchars, rlog, rint, rreal, bytes)
2391 character(len=*), intent(in) :: component !< character defining the program component (e.g. solution)
2392 real(DP), intent(in) :: rchars !< allocated size of characters (in common units)
2393 real(DP), intent(in) :: rlog !< allocated size of logical (in common units)
2394 real(DP), intent(in) :: rint !< allocated size of integer variables (in common units)
2395 real(DP), intent(in) :: rreal !< allocated size of real variables (in common units)
2396 real(DP), intent(in) :: bytes !< total allocated memory in memory manager (in common units)
2397 ! -- formats
2398 ! -- code
2399 !
2400 ! -- write line
2401 call memtab%add_term(component)
2402 call memtab%add_term(rchars)
2403 call memtab%add_term(rlog)
2404 call memtab%add_term(rint)
2405 call memtab%add_term(rreal)
2406 call memtab%add_term(bytes)
2407 end subroutine mem_summary_line
2408
2409 !> @brief Determine appropriate memory unit and conversion factor
2410 !<
2411 subroutine mem_units(bytes, fact, cunits)
2412 ! -- dummy
2413 real(DP), intent(in) :: bytes !< total nr. of bytes
2414 real(DP), intent(inout) :: fact !< conversion factor
2415 character(len=*), intent(inout) :: cunits !< string with memory unit
2416 ! -- local
2417 ! -- formats
2418 ! -- code
2419 !
2420 ! -- initialize factor and unit string
2421 cunits = 'UNKNOWN'
2422 fact = done
2423 !
2424 ! -- set factor
2425 if (bytes < dep3) then
2426 fact = done
2427 cunits = 'BYTES'
2428 else if (bytes < dep6) then
2429 fact = dem3
2430 cunits = 'KILOBYTES'
2431 else if (bytes < dep9) then
2432 fact = dem6
2433 cunits = 'MEGABYTES'
2434 else
2435 fact = dem9
2436 cunits = 'GIGABYTES'
2437 end if
2438 end subroutine mem_units
2439
2440 !> @brief Create and fill a table with the total allocated memory
2441 !< in the memory manager
2442 subroutine mem_summary_total(iout, bytes)
2443 integer(I4B), intent(in) :: iout !< unit number for mfsim.lst
2444 real(DP), intent(in) :: bytes !< total number of bytes allocated in the memory manager
2445 ! -- local
2446 character(len=LINELENGTH) :: title
2447 character(len=LINELENGTH) :: text
2448 character(LEN=10) :: cunits
2449 integer(I4B) :: nterms
2450 integer(I4B) :: nrows
2451 real(DP) :: fact
2452 real(DP) :: smb
2453 ! -- formats
2454 ! -- code
2455 !
2456 ! -- calculate factor and memory units
2457 call mem_units(bytes, fact, cunits)
2458 !
2459 ! -- set table terms
2460 nterms = 2
2461 nrows = 6
2462 !
2463 ! -- set up table title
2464 title = 'MEMORY MANAGER TOTAL STORAGE BY DATA TYPE, IN '//trim(cunits)
2465 !
2466 ! -- set up stage tableobj
2467 call table_cr(memtab, 'MEM TOT', title)
2468 call memtab%table_df(nrows, nterms, iout)
2469 !
2470 ! -- data type
2471 text = 'DATA TYPE'
2472 call memtab%initialize_column(text, 15, alignment=tableft)
2473 !
2474 ! -- number of values
2475 text = 'ALLOCATED MEMORY'
2476 call memtab%initialize_column(text, 15, alignment=tabcenter)
2477 !
2478 ! -- write data
2479 !
2480 ! -- characters
2481 smb = real(nvalues_astr, dp) * fact
2482 call memtab%add_term('Character')
2483 call memtab%add_term(smb)
2484 !
2485 ! -- logicals
2486 smb = real(nvalues_alogical * lgp, dp) * fact
2487 call memtab%add_term('Logical')
2488 call memtab%add_term(smb)
2489 !
2490 ! -- integers
2491 smb = real(nvalues_aint * i4b, dp) * fact
2492 call memtab%add_term('Integer')
2493 call memtab%add_term(smb)
2494 !
2495 ! -- reals
2496 smb = real(nvalues_adbl * dp, dp) * fact
2497 call memtab%add_term('Real')
2498 call memtab%add_term(smb)
2499 !
2500 ! -- total memory usage
2501 call memtab%print_separator()
2502 smb = bytes * fact
2503 call memtab%add_term('Total')
2504 call memtab%add_term(smb)
2505 !
2506 ! -- Virtual memory
2507 smb = calc_virtual_mem() * fact
2508 call memtab%add_term('Virtual')
2509 call memtab%add_term(smb)
2510 !
2511 ! -- deallocate table
2512 call mem_cleanup_table()
2513 end subroutine mem_summary_total
2514
2515 !> @brief Generic function to clean a memory manager table
2516 !<
2518 ! -- local
2519 ! -- formats
2520 ! -- code
2521 call memtab%table_da()
2522 deallocate (memtab)
2523 nullify (memtab)
2524 end subroutine mem_cleanup_table
2525
2526 !> @brief Write memory manager memory usage based on the
2527 !! user-specified memory_print_option
2528 !!
2529 !! The total memory usage by data types (int, real, etc.)
2530 !! is written for every simulation.
2531 !<
2532 subroutine mem_write_usage(iout)
2533 integer(I4B), intent(in) :: iout !< unit number for mfsim.lst
2534 ! -- local
2535 class(memorytype), pointer :: mt
2536 character(len=LENMEMADDRESS), allocatable, dimension(:) :: cunique
2537 ! character(len=LENMEMPATH) :: mem_path
2538 character(len=LENMEMPATH) :: context
2539 character(len=LENCOMPONENTNAME) :: component
2540 character(len=LENCOMPONENTNAME) :: subcomponent
2541 character(len=LENMEMADDRESS) :: context_component
2542 character(LEN=10) :: cunits
2543 type(memorycontaineriteratortype), allocatable :: itr
2544 integer(I4B) :: icomp
2545 integer(I4B) :: ilen
2546 integer(I8B) :: nchars
2547 integer(I8B) :: nlog
2548 integer(I8B) :: nint
2549 integer(I8B) :: nreal
2550 real(dp) :: simbytes
2551 real(dp) :: fact
2552 real(dp) :: rchars
2553 real(dp) :: rlog
2554 real(dp) :: rint
2555 real(dp) :: rreal
2556 real(dp) :: bytes
2557 ! -- formats
2558 ! -- code
2559 !
2560 ! -- Calculate simulation memory allocation
2561 simbytes = (nvalues_astr + &
2562 nvalues_alogical * lgp + &
2563 nvalues_aint * i4b + &
2564 nvalues_adbl * dp)
2565 simbytes = real(simbytes, dp)
2566 !
2567 ! -- calculate factor and memory units
2568 call mem_units(simbytes, fact, cunits)
2569 !
2570 ! -- Write summary table for simulation components
2571 if (iprmem == 1) then
2572 !
2573 ! -- Find unique names of simulation components
2574 call mem_unique_origins(cunique)
2575 call mem_summary_table(iout, size(cunique), cunits)
2576 do icomp = 1, size(cunique)
2577 nchars = 0
2578 nlog = 0
2579 nint = 0
2580 nreal = 0
2581 bytes = dzero
2582 ilen = len_trim(cunique(icomp))
2583 itr = memorystore%iterator()
2584 do while (itr%has_next())
2585 call itr%next()
2586 mt => itr%value()
2587 call split_mem_path(mt%path, component, subcomponent)
2588 context = get_mem_path_context(mt%path)
2589 context_component = trim(context)//component
2590 if (cunique(icomp) /= context_component(1:ilen)) cycle
2591 if (.not. mt%master) cycle
2592 if (mt%memtype(1:6) == 'STRING') then
2593 nchars = nchars + mt%isize * mt%element_size
2594 else if (mt%memtype(1:7) == 'LOGICAL') then
2595 nlog = nlog + mt%isize
2596 else if (mt%memtype(1:7) == 'INTEGER') then
2597 nint = nint + mt%isize
2598 else if (mt%memtype(1:6) == 'DOUBLE') then
2599 nreal = nreal + mt%isize
2600 end if
2601 end do
2602 !
2603 ! -- calculate size of each data type in bytes
2604 rchars = real(nchars, dp) * fact
2605 rlog = real(nlog * lgp, dp) * fact
2606 rint = real(nint * i4b, dp) * fact
2607 rreal = real(nreal * dp, dp) * fact
2608 !
2609 ! -- calculate total storage in bytes
2610 bytes = rchars + rlog + rint + rreal
2611 !
2612 ! -- write data
2613 call mem_summary_line(cunique(icomp), rchars, rlog, rint, rreal, bytes)
2614 end do
2615 call mem_cleanup_table()
2616 end if
2617 !
2618 ! -- Write table with all variables for iprmem == 2
2619 if (iprmem == 2) then
2621 end if
2622 !
2623 ! -- Write total memory allocation
2624 call mem_summary_total(iout, simbytes)
2625 end subroutine mem_write_usage
2626
2627 subroutine mem_print_detailed(iout)
2628 integer(I4B) :: iout
2629 ! local
2630 class(memorytype), pointer :: mt
2631 type(memorycontaineriteratortype), allocatable :: itr
2632
2633 call mem_detailed_table(iout, memorystore%count())
2634 itr = memorystore%iterator()
2635 do while (itr%has_next())
2636 call itr%next()
2637 mt => itr%value()
2638 call mt%table_entry(memtab)
2639 end do
2640 call mem_cleanup_table()
2641
2642 end subroutine mem_print_detailed
2643
2644 !> @brief Sum up virtual memory, i.e. memory
2645 !< that is owned by other processes
2646 function calc_virtual_mem() result(vmem_size)
2647 real(dp) :: vmem_size
2648 ! local
2649 type(memorycontaineriteratortype), allocatable :: itr
2650 type(memorytype), pointer :: mt
2651
2652 vmem_size = dzero
2653 itr = memorystore%iterator()
2654 do while (itr%has_next())
2655 call itr%next()
2656 mt => itr%value()
2657 if (index(mt%path, "__P") == 1) then
2658 vmem_size = mt%element_size * mt%isize + vmem_size
2659 end if
2660 end do
2661
2662 end function calc_virtual_mem
2663
2664 !> @brief Release a memory store entry: deallocate data and update counters
2665 !!
2666 !! Deallocates the data held by mt, zeroes mt%isize, and decrements the
2667 !! appropriate nvalues_* counter for master entries. no-op when data has
2668 !! already been released. Primarily intended to support the release of
2669 !! input context memory prior to simulation runtime.
2670 !<
2671 subroutine mem_release(mt)
2672 type(memorytype), pointer, intent(inout) :: mt
2673
2674 if (.not. mt%mt_associated()) return
2675
2676 if (mt%master) then
2677 if (mt%memtype(1:6) == 'STRING') then
2678 ! nvalues_astr increments differ: scalar adds element_size (ilen),
2679 ! arrays (str1d, charstr1d) add isize only. For IDM release the
2680 ! variables are arrays, so decrement by isize to match.
2681 if (mt%isize == 1) then
2682 nvalues_astr = nvalues_astr - mt%element_size
2683 else
2684 nvalues_astr = nvalues_astr - mt%isize
2685 end if
2686 else if (mt%memtype(1:7) == 'LOGICAL') then
2688 else if (mt%memtype(1:7) == 'INTEGER') then
2689 nvalues_aint = nvalues_aint - mt%isize
2690 else if (mt%memtype(1:6) == 'DOUBLE') then
2691 nvalues_adbl = nvalues_adbl - mt%isize
2692 end if
2693 end if
2694
2695 call mt%mt_deallocate()
2696 mt%isize = 0
2697 end subroutine mem_release
2698
2699 !> @brief Deallocate memory in the memory manager
2700 !<
2701 subroutine mem_da()
2702 ! -- modules
2703 use versionmodule, only: idevelopmode
2704 ! -- local
2705 class(memorytype), pointer :: mt
2706 type(memorycontaineriteratortype), allocatable :: itr
2707 ! -- code
2708 itr = memorystore%iterator()
2709 do while (itr%has_next())
2710 call itr%next()
2711 mt => itr%value()
2712 call mt%mt_deallocate()
2713 if (idevelopmode == 1) call mem_da_check(mt)
2714 deallocate (mt)
2715 end do
2716
2717 call memorystore%clear()
2718 if (count_errors() > 0) then
2719 call store_error('Could not clear memory list.', terminate=.true.)
2720 end if
2721 end subroutine mem_da
2722
2723 subroutine mem_da_check(mt)
2724 ! -- modules
2725 use inputoutputmodule, only: upcase
2726 ! -- dummy
2727 class(memorytype), pointer :: mt
2728 ! -- local
2729 character(len=LINELENGTH) :: error_msg
2730 character(len=LENVARNAME) :: ucname
2731 !
2732 ! -- check if memory has been deallocated
2733 if (mt%mt_associated() .and. mt%element_size == -1) then
2734 error_msg = trim(adjustl(mt%path))//' '// &
2735 trim(adjustl(mt%name))//' has invalid element size'
2736 call store_error(trim(error_msg))
2737 end if
2738 !
2739 ! -- check if memory has been deallocated
2740 if (mt%mt_associated() .and. mt%isize > 0) then
2741 error_msg = trim(adjustl(mt%path))//' '// &
2742 trim(adjustl(mt%name))//' not deallocated'
2743 call store_error(trim(error_msg))
2744 end if
2745 !
2746 ! -- check case of varname
2747 ucname = mt%name
2748 call upcase(ucname)
2749 if (mt%name /= ucname) then
2750 error_msg = trim(adjustl(mt%path))//' '// &
2751 trim(adjustl(mt%name))//' not upper case'
2752 call store_error(trim(error_msg))
2753 end if
2754 end subroutine mem_da_check
2755
2756 !> @brief Create a array with unique first components from all memory paths.
2757 !! Only the first component of the memory path is evaluated.
2758 !<
2759 subroutine mem_unique_origins(cunique)
2760 ! -- modules
2762 ! -- dummy
2763 character(len=LENMEMADDRESS), allocatable, dimension(:), intent(inout) :: &
2764 cunique !< array with unique first components
2765 ! -- local
2766 class(memorytype), pointer :: mt
2767 character(len=LENMEMPATH) :: context
2768 character(len=LENCOMPONENTNAME) :: component
2769 character(len=LENCOMPONENTNAME) :: subcomponent
2770 character(len=LENMEMADDRESS) :: context_component
2771 type(memorycontaineriteratortype), allocatable :: itr
2772 integer(I4B) :: ipa
2773 ! -- code
2774 !
2775 ! -- initialize cunique
2776 allocate (cunique(0))
2777 !
2778 ! -- find unique origins
2779 itr = memorystore%iterator()
2780 do while (itr%has_next())
2781 call itr%next()
2782 mt => itr%value()
2783 call split_mem_path(mt%path, component, subcomponent)
2784 context = get_mem_path_context(mt%path)
2785 context_component = trim(context)//component
2786 ipa = ifind(cunique, context_component)
2787 if (ipa < 1) then
2788 call expandarray(cunique, 1)
2789 cunique(size(cunique)) = context_component
2790 end if
2791 end do
2792 end subroutine mem_unique_origins
2793
2794end module memorymanagermodule
This module contains simulation constants.
Definition Constants.f90:9
integer(i4b), parameter linelength
maximum length of a standard line
Definition Constants.f90:45
integer(i4b), parameter lencomponentname
maximum length of a component name
Definition Constants.f90:18
@ tabcenter
centered table column
@ tabright
right justified table column
@ tableft
left justified table column
integer(i4b), parameter lenmemaddress
maximum length of the full memory address, including variable name
Definition Constants.f90:31
real(dp), parameter dep3
real constant 1000
Definition Constants.f90:88
@ tabucstring
upper case string table data
@ tabstring
string table data
@ tabreal
real table data
@ tabinteger
integer table data
real(dp), parameter dep6
real constant 1000000
Definition Constants.f90:89
integer(i4b), parameter lenmemseparator
maximum length of the memory path separator used, currently a '/'
Definition Constants.f90:26
real(dp), parameter dep9
real constant 1e9
Definition Constants.f90:90
integer(i4b), parameter lenvarname
maximum length of a variable name
Definition Constants.f90:17
integer(i4b), parameter, public lenmemtype
maximum length of a memory manager type
Definition Constants.f90:62
real(dp), parameter dem3
real constant 1e-3
real(dp), parameter dem6
real constant 1e-6
real(dp), parameter dzero
real constant zero
Definition Constants.f90:65
real(dp), parameter dem9
real constant 1e-9
integer(i4b), parameter lenmempath
maximum length of the memory path
Definition Constants.f90:27
real(dp), parameter done
real constant 1
Definition Constants.f90:76
subroutine, public upcase(word)
Convert to upper case.
This module defines variable data types.
Definition kind.f90:8
character(len=lenmempath) function get_mem_path_context(mem_path)
Return the context from the memory path.
subroutine mem_check_length(name, max_length, description)
Generic routine to check the length of (parts of) the memory address.
subroutine strip_context_mem_path(mem_path, mem_path_no_context)
Remove the context from the memory path.
subroutine split_mem_path(mem_path, component, subcomponent)
Split the memory path into component(s).
subroutine reallocate_int2d(aint, ncol, nrow, name, mem_path)
Reallocate a 2-dimensional integer array.
subroutine allocate_logical(sclr, name, mem_path)
Allocate a logical scalar.
subroutine allocate_int2d(aint, ncol, nrow, name, mem_path)
Allocate a 2-dimensional integer array.
subroutine mem_summary_line(component, rchars, rlog, rint, rreal, bytes)
Write a row for the memory_print_option 'SUMMARY' table.
subroutine allocate_dbl3d(adbl, ncol, nrow, nlay, name, mem_path)
Allocate a 3-dimensional real array.
integer(i8b) nvalues_adbl
subroutine deallocate_str1d(astr1d, name, mem_path)
Deallocate an array of defined-length character strings.
subroutine deallocate_int(sclr)
DEPRECATED. The memory manager will handle the deallocation of the pointer.
subroutine deallocate_logical1d(alog, name, mem_path)
DEPRECATED. The memory manager will handle the deallocation of the pointer.
type(tabletype), pointer memtab
subroutine setptr_int3d(aint, name, mem_path)
Set pointer to 3d integer array.
subroutine mem_detailed_table(iout, nrows)
Create a table if memory_print_option is 'ALL'.
subroutine deallocate_charstr1d(astr1d, name, mem_path)
DEPRECATED. The memory manager will handle the deallocation of the pointer.
subroutine allocate_charstr1d(acharstr1d, ilen, nrow, name, mem_path)
Allocate a 1-dimensional array of deferred-length CharacterStringType.
subroutine reallocate_charstr1d(acharstr1d, ilen, nrow, name, mem_path)
Reallocate a 1-dimensional deferred length string array.
subroutine, public mem_write_usage(iout)
Write memory manager memory usage based on the user-specified memory_print_option.
subroutine reallocate_dbl2d(adbl, ncol, nrow, name, mem_path)
Reallocate a 2-dimensional real array.
integer(i8b) nvalues_aint
subroutine setptr_int(sclr, name, mem_path)
Set pointer to integer scalar.
subroutine checkin_charstr1d(acharstr1d, ilen, name, mem_path, name2, mem_path2)
Check in an existing 1d CharacterStringType array with a new address (name + path).
subroutine mem_cleanup_table()
Generic function to clean a memory manager table.
subroutine copyptr_int1d(aint, name, mem_path, mem_path_copy)
Make a copy of a 1-dimensional integer array.
subroutine deallocate_dbl1d(adbl, name, mem_path)
DEPRECATED. The memory manager will handle the deallocation of the pointer.
subroutine allocate_str1d(astr1d, ilen, nrow, name, mem_path)
Allocate a 1-dimensional defined length string array.
subroutine allocate_str(sclr, ilen, name, mem_path)
Allocate a character string.
subroutine, public get_mem_type(name, mem_path, var_type)
Get the variable memory type.
subroutine setptr_int2d(aint, name, mem_path)
Set pointer to 2d integer array.
subroutine, public mem_da()
Deallocate memory in the memory manager.
subroutine deallocate_int1d(aint, name, mem_path)
DEPRECATED. The memory manager will handle the deallocation of the pointer.
integer(i8b) nvalues_astr
subroutine deallocate_dbl(sclr)
DEPRECATED. The memory manager will handle the deallocation of the pointer.
subroutine copyptr_int2d(aint, name, mem_path, mem_path_copy)
Make a copy of a 2-dimensional integer array.
subroutine mem_da_check(mt)
subroutine allocate_dbl(sclr, name, mem_path)
Allocate a real scalar.
real(dp) function calc_virtual_mem()
Sum up virtual memory, i.e. memory.
subroutine copyptr_dbl2d(adbl, name, mem_path, mem_path_copy)
Make a copy of a 2-dimensional real array.
subroutine reassignptr_int2d(aint, name, mem_path, name_target, mem_path_target)
Set the pointer for a 2-dimensional integer array to.
subroutine reallocate_logical1d(alog, nrow, name, mem_path)
Reallocate a 1-dimensional logical array.
subroutine setptr_dbl(sclr, name, mem_path)
Set pointer to a real scalar.
subroutine deallocate_int3d(aint, name, mem_path)
DEPRECATED. The memory manager will handle the deallocation of the pointer.
subroutine deallocate_int2d(aint, name, mem_path)
DEPRECATED. The memory manager will handle the deallocation of the pointer.
subroutine reassignptr_dbl1d(adbl, name, mem_path, name_target, mem_path_target)
Set the pointer for a 1-dimensional real array to.
subroutine, public mem_release(mt)
Release a memory store entry: deallocate data and update counters.
subroutine allocate_int3d(aint, ncol, nrow, nlay, name, mem_path)
Allocate a 3-dimensional integer array.
subroutine, public get_mem_shape(name, mem_path, mem_shape)
Get the variable memory shape.
subroutine deallocate_dbl2d(adbl, name, mem_path)
DEPRECATED. The memory manager will handle the deallocation of the pointer.
subroutine mem_units(bytes, fact, cunits)
Determine appropriate memory unit and conversion factor.
subroutine setptr_int1d(aint, name, mem_path)
Set pointer to 1d integer array.
subroutine setptr_str1d(astr1d, name, mem_path)
Set pointer to a fixed-length string array.
subroutine checkin_int2d(aint2d, name, mem_path, name2, mem_path2)
Check in an existing 2d integer array with a new address (name + path).
type(memorystoretype), public memorystore
subroutine reallocate_dbl1d(adbl, nrow, name, mem_path)
Reallocate a 1-dimensional real array.
subroutine setptr_dbl2d(adbl, name, mem_path)
Set pointer to a 2d real array.
subroutine deallocate_logical(sclr)
DEPRECATED. The memory manager will handle the deallocation of the pointer.
subroutine setptr_logical1d(alog, name, mem_path)
Set pointer to 1d logical array.
subroutine checkin_dbl2d(adbl2d, name, mem_path, name2, mem_path2)
Check in an existing 2d double precision array with a new address (name + path).
subroutine deallocate_dbl3d(adbl, name, mem_path)
DEPRECATED. The memory manager will handle the deallocation of the pointer.
subroutine reallocate_int1d(aint, nrow, name, mem_path)
Reallocate a 1-dimensional integer array.
subroutine checkin_logical1d(alog, name, mem_path, name2, mem_path2)
Check in an existing 1d logical array with a new address (name + path).
subroutine, public get_from_memorystore(name, mem_path, mt, found, check)
Get a memory type entry from the memory list.
subroutine, public mem_print_detailed(iout)
subroutine reallocate_str1d(astr, ilen, nrow, name, mem_path)
Reallocate a 1-dimensional defined length string array.
subroutine copyptr_dbl1d(adbl, name, mem_path, mem_path_copy)
Make a copy of a 1-dimensional real array.
subroutine allocate_logical1d(alog, nrow, name, mem_path)
Allocate a 1-dimensional logical array.
subroutine reassignptr_dbl2d(adbl, name, mem_path, name_target, mem_path_target)
Set the pointer for a 2-dimensional real array to.
subroutine copyptr_logical1d(alog, name, mem_path, mem_path_copy)
Make a copy of a 1-dimensional logical array.
subroutine allocate_int1d(aint, nrow, name, mem_path)
Allocate a 1-dimensional integer array.
subroutine allocate_int(sclr, name, mem_path)
Allocate a integer scalar.
subroutine reassignptr_int1d(aint, name, mem_path, name_target, mem_path_target)
Set the pointer for a 1-dimensional integer array to.
subroutine mem_summary_total(iout, bytes)
Create and fill a table with the total allocated memory.
subroutine, public get_isize(name, mem_path, isize)
Get the number of elements for this variable.
subroutine allocate_dbl2d(adbl, ncol, nrow, name, mem_path)
Allocate a 2-dimensional real array.
subroutine deallocate_str(sclr, name, mem_path)
DEPRECATED. The memory manager will handle the deallocation of the pointer.
subroutine, public get_mem_rank(name, mem_path, rank)
Get the variable rank.
subroutine checkin_dbl1d(adbl, name, mem_path, name2, mem_path2)
Check in an existing 1d double precision array with a new address (name + path).
subroutine checkin_int1d(aint, name, mem_path, name2, mem_path2)
Check in an existing 1d integer array with a new address (name + path).
subroutine reassignptr_logical1d(alog, name, mem_path, name_target, mem_path_target)
Set the pointer for a 1-dimensional logical array to.
subroutine reassignptr_int(sclr, name, mem_path, name_target, mem_path_target)
Set the pointer for an integer scalar to.
subroutine, public copy_dbl1d(adbl, name, mem_path)
Copy values from a 1-dimensional real array in the memory.
subroutine setptr_dbl3d(adbl, name, mem_path)
Set pointer to a 3d real array.
subroutine, public get_mem_elem_size(name, mem_path, size)
Get the memory size of a single element of the stored variable.
subroutine setptr_dbl1d(adbl, name, mem_path)
Set pointer to a 1d real array.
subroutine setptr_str(asrt, name, mem_path)
Set pointer to a string (scalar).
integer(i8b) nvalues_alogical
subroutine setptr_charstr1d(acharstr1d, name, mem_path)
Set pointer to an array of CharacterStringType.
subroutine mem_unique_origins(cunique)
Create a array with unique first components from all memory paths. Only the first component of the me...
subroutine mem_summary_table(iout, nrows, cunits)
Create a table if memory_print_option is 'SUMMARY'.
subroutine setptr_logical(sclr, name, mem_path)
Set pointer to a logical scalar.
subroutine, public mem_set_print_option(iout, keyword, error_msg)
Set the memory print option.
subroutine allocate_dbl1d(adbl, nrow, name, mem_path)
Allocate a 1-dimensional real array.
subroutine allocate_error(varname, mem_path, istat, isize)
Issue allocation error message and stop program execution.
This module contains simulation methods.
Definition Sim.f90:10
subroutine, public store_error(msg, terminate)
Store an error message.
Definition Sim.f90:94
integer(i4b) function, public count_errors()
Return number of errors.
Definition Sim.f90:60
This module contains simulation variables.
character(len=maxcharlen) errmsg
error message string
integer(i4b) iout
file unit number for simulation output
subroutine, public table_cr(this, name, title)
Definition Table.f90:87
This module contains version information.
Definition version.f90:7
integer(i4b), parameter idevelopmode
Definition version.f90:19
This class is used to store a single deferred-length character string. It was designed to work in an ...
An iterator used to iterate through a MemoryContainer.