MODFLOW 6  version 6.6.0.dev0
USGS Modular Hydrologic Model
MappedMemory.f90
Go to the documentation of this file.
2  use kindmodule, only: i4b, lgp
6 
7  implicit none
8  private
9 
10  public :: castasmappeddata
11  public :: mappedmemorytype
12 
14  integer(I4B) :: controller_id
15  integer(I4B) :: sync_stage
16  character(len=LENVARNAME) :: src_name
17  character(len=LENMEMPATH) :: src_path
18  type(memorytype), pointer :: src !< cached memory item
19  character(len=LENVARNAME) :: tgt_name
20  character(len=LENMEMPATH) :: tgt_path
21  type(memorytype), pointer :: tgt !< cached memory item
22  logical(LGP) :: copy_all !< when true: copy all elements
23  integer(I4B), dimension(:), pointer :: src_idx !< source indexes to copy from
24  integer(I4B), dimension(:), pointer :: tgt_idx !< target indexes to copy to
25  integer(I4B), dimension(:), pointer :: sign !< optional sign (or null) to negate copied value
26  integer(I4B), dimension(:), pointer :: lut !< optional lookup table (can be null()), converts
27  !! src_idx(i) to actual (local) idx when copying
28 
29  contains
30  procedure :: sync
31  procedure :: skip_sync !< possibility to skip synchronization, e.g. when src variable not allocated and should remain at default
32  ! private stuff
33  procedure, private :: sync_int1d
34  procedure, private :: apply_sgn_int1d
35  procedure, private :: sync_dbl1d
36  procedure, private :: apply_sgn_dbl1d
37  procedure, private :: sync_dbl2d
38  procedure, private :: apply_sgn_dbl2d
39 
40  end type mappedmemorytype
41 
42 contains
43 
44  subroutine sync(this)
45  class(mappedmemorytype) :: this
46  ! local
47  logical(LGP) :: found
48 
49  if (.not. associated(this%src)) then
50  ! cache
51  call get_from_memorystore(this%src_name, this%src_path, this%src, found)
52  call get_from_memorystore(this%tgt_name, this%tgt_path, this%tgt, found)
53  end if
54 
55  if (this%skip_sync()) return
56 
57  if (associated(this%tgt%aint1d)) call this%sync_int1d()
58  if (associated(this%tgt%adbl1d)) call this%sync_dbl1d()
59  if (associated(this%tgt%adbl2d)) call this%sync_dbl2d()
60 
61  if (associated(this%sign)) then
62  if (associated(this%tgt%aint1d)) call this%apply_sgn_int1d()
63  if (associated(this%tgt%adbl1d)) call this%apply_sgn_dbl1d()
64  if (associated(this%tgt%adbl2d)) call this%apply_sgn_dbl2d()
65  end if
66 
67  end subroutine sync
68 
69  function skip_sync(this) result(skip)
70  class(mappedmemorytype) :: this
71  logical(LGP) :: skip
72 
73  skip = (this%src%isize == 0)
74 
75  end function skip_sync
76 
77  !> @brief Copy 1d integer array with map
78  subroutine sync_int1d(this)
79  class(mappedmemorytype) :: this
80  ! local
81  integer(I4B) :: i
82 
83  if (this%copy_all) then
84  do i = 1, this%tgt%isize
85  this%tgt%aint1d(i) = this%src%aint1d(i)
86  end do
87  else if (associated(this%lut)) then
88  do i = 1, size(this%tgt_idx)
89  this%tgt%aint1d(this%tgt_idx(i)) = &
90  this%src%aint1d(this%lut(this%src_idx(i)))
91  end do
92  else
93  do i = 1, size(this%tgt_idx)
94  this%tgt%aint1d(this%tgt_idx(i)) = this%src%aint1d(this%src_idx(i))
95  end do
96  end if
97 
98  end subroutine sync_int1d
99 
100  subroutine apply_sgn_int1d(this)
101  class(mappedmemorytype) :: this
102  ! local
103  integer(I4B) :: i
104 
105  if (this%copy_all) then
106  do i = 1, this%tgt%isize
107  this%tgt%aint1d(i) = this%tgt%aint1d(i) * this%sign(i)
108  end do
109  else
110  do i = 1, size(this%tgt_idx)
111  this%tgt%aint1d(this%tgt_idx(i)) = this%tgt%aint1d(this%tgt_idx(i)) * &
112  this%sign(i)
113  end do
114  end if
115 
116  end subroutine apply_sgn_int1d
117 
118  !> @brief Copy 1d double array with map.
119  !<
120  subroutine sync_dbl1d(this)
121  class(mappedmemorytype) :: this
122  ! local
123  integer(I4B) :: i
124 
125  if (this%copy_all) then
126  do i = 1, this%tgt%isize
127  this%tgt%adbl1d(i) = this%src%adbl1d(i)
128  end do
129  else if (associated(this%lut)) then
130  do i = 1, size(this%tgt_idx)
131  this%tgt%adbl1d(this%tgt_idx(i)) = &
132  this%src%adbl1d(this%lut(this%src_idx(i)))
133  end do
134  else
135  do i = 1, size(this%tgt_idx)
136  this%tgt%adbl1d(this%tgt_idx(i)) = this%src%adbl1d(this%src_idx(i))
137  end do
138  end if
139 
140  end subroutine sync_dbl1d
141 
142  subroutine apply_sgn_dbl1d(this)
143  class(mappedmemorytype) :: this
144  ! local
145  integer(I4B) :: i
146 
147  if (this%copy_all) then
148  do i = 1, this%tgt%isize
149  this%tgt%adbl1d(i) = this%tgt%adbl1d(i) * this%sign(i)
150  end do
151  else
152  do i = 1, size(this%tgt_idx)
153  this%tgt%adbl1d(this%tgt_idx(i)) = this%tgt%adbl1d(this%tgt_idx(i)) * &
154  this%sign(i)
155  end do
156  end if
157 
158  end subroutine apply_sgn_dbl1d
159 
160  !> @brief Copy 2d double array with map.
161  !< NB: only dim=2 is mapped.
162  subroutine sync_dbl2d(this)
163  class(mappedmemorytype) :: this
164  ! local
165  integer(I4B) :: i, k
166 
167  if (this%copy_all) then
168  do i = 1, this%tgt%isize
169  do k = 1, size(this%src%adbl2d, dim=1)
170  this%tgt%adbl2d(k, i) = this%src%adbl2d(k, i)
171  end do
172  end do
173  else if (associated(this%lut)) then
174  do i = 1, size(this%tgt_idx)
175  do k = 1, size(this%src%adbl2d, dim=1)
176  this%tgt%adbl2d(k, this%tgt_idx(i)) = &
177  this%src%adbl2d(k, this%lut(this%src_idx(i)))
178  end do
179  end do
180  else
181  do i = 1, size(this%tgt_idx)
182  do k = 1, size(this%src%adbl2d, dim=1)
183  this%tgt%adbl2d(k, this%tgt_idx(i)) = &
184  this%src%adbl2d(k, this%src_idx(i))
185  end do
186  end do
187  end if
188 
189  end subroutine sync_dbl2d
190 
191  subroutine apply_sgn_dbl2d(this)
192  class(mappedmemorytype) :: this
193  ! local
194  integer(I4B) :: i, k
195 
196  if (this%copy_all) then
197  do i = 1, this%tgt%isize
198  do k = 1, size(this%src%adbl2d, dim=1)
199  this%tgt%adbl2d(k, i) = this%tgt%adbl2d(k, i) * this%sign(i)
200  end do
201  end do
202  else
203  do i = 1, size(this%tgt_idx)
204  do k = 1, size(this%src%adbl2d, dim=1)
205  this%tgt%adbl2d(k, this%tgt_idx(i)) = &
206  this%tgt%adbl2d(k, this%tgt_idx(i)) * this%sign(i)
207  end do
208  end do
209  end if
210 
211  end subroutine apply_sgn_dbl2d
212 
213  function castasmappeddata(obj) result(res)
214  implicit none
215  class(*), pointer, intent(inout) :: obj
216  class(mappedmemorytype), pointer :: res
217 
218  res => null()
219 
220  select type (obj)
221  class is (mappedmemorytype)
222  res => obj
223  end select
224 
225  end function castasmappeddata
226 
227 end module mappedmemorymodule
This module contains simulation constants.
Definition: Constants.f90:9
integer(i4b), parameter lenvarname
maximum length of a variable name
Definition: Constants.f90:17
integer(i4b), parameter lenmempath
maximum length of the memory path
Definition: Constants.f90:27
This module defines variable data types.
Definition: kind.f90:8
logical(lgp) function skip_sync(this)
subroutine apply_sgn_int1d(this)
subroutine sync_dbl1d(this)
Copy 1d double array with map.
subroutine apply_sgn_dbl2d(this)
subroutine apply_sgn_dbl1d(this)
subroutine sync_int1d(this)
Copy 1d integer array with map.
subroutine sync_dbl2d(this)
Copy 2d double array with map.
class(mappedmemorytype) function, pointer, public castasmappeddata(obj)
subroutine sync(this)
subroutine, public get_from_memorystore(name, mem_path, mt, found, check)
@ brief Get a memory type entry from the memory list