MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
MemoryHelper.f90
Go to the documentation of this file.
2 use kindmodule, only: i4b, lgp
5 use simmodule, only: store_error
7
8 implicit none
9
10 character(len=LENMEMSEPARATOR), parameter :: mempathseparator = '/' !< used to build up the memory address for the stored variables
11
12contains
13
14 !> @brief returns the path to the memory object
15 !!
16 !! Returns the path to the location in the memory manager where
17 !! the variables for this (sub)component are stored, the 'memoryPath'
18 !!
19 !! NB: no need to trim the input parameters
20 !! @hidecallergraph
21 !<
22 function create_mem_path(component, subcomponent, context) result(memory_path)
23 character(len=*), intent(in) :: component !< name of the solution, model, or exchange
24 character(len=*), intent(in), optional :: subcomponent !< name of the package (optional)
25 character(len=*), intent(in), optional :: context !< name of the context (optional)
26 character(len=LENMEMPATH) :: memory_path !< the memory path
27
28 call mem_check_length(component, lencomponentname, "solution/model/exchange")
29
30 if (present(subcomponent)) then
31 call mem_check_length(subcomponent, lencomponentname, "package")
32 end if
33
34 if (present(context)) then
35 call mem_check_length(context, lencontextname, "context")
36 end if
37
38 memory_path = trim(component)
39
40 if (present(subcomponent)) then
41 memory_path = trim(memory_path)//mempathseparator//trim(subcomponent)
42 end if
43
44 if (present(context)) then
45 memory_path = trim(context)//mempathseparator//trim(memory_path)
46 end if
47
48 end function create_mem_path
49
50 !> @brief returns the address string of the memory object
51 !!
52 !! Returns the memory address, i.e. the full path plus name of the stored variable
53 !!
54 !! NB: no need to trim the input parameters
55 !<
56 function create_mem_address(mem_path, var_name) result(mem_address)
57 character(len=*), intent(in) :: mem_path !< path to the memory object
58 character(len=*), intent(in) :: var_name !< name of the stored variable
59 character(len=LENMEMADDRESS) :: mem_address !< full address string to the memory object
60
61 call mem_check_length(mem_path, lenmempath, "memory path")
62 call mem_check_length(var_name, lenvarname, "variable")
63
64 mem_address = trim(mem_path)//mempathseparator//trim(var_name)
65
66 end function create_mem_address
67
68 !> @brief Split a memory address string into memory path and variable name
69 !<
70 subroutine split_mem_address(mem_address, mem_path, var_name, success)
71 character(len=*), intent(in) :: mem_address !< the full memory address string
72 character(len=LENMEMPATH), intent(out) :: mem_path !< the memory path
73 character(len=LENVARNAME), intent(out) :: var_name !< the variable name
74 logical(LGP), intent(out) :: success !< true when successful
75 ! local
76 integer(I4B) :: idx
77
78 idx = index(mem_address, mempathseparator, back=.true.)
79
80 ! if no separator, or it's at the end of the string,
81 ! the memory address is not valid:
82 if (idx < 1 .or. idx == len(mem_address)) then
83 success = .false.
84 mem_path = ''
85 var_name = ''
86 else
87 success = .true.
88 mem_path = mem_address(:idx - 1)
89 var_name = mem_address(idx + 1:)
90 end if
91
92 ! remove context specifier if prepended to mempath
93 !if (success) then
94 ! idx = index(mem_path, memPathSeparator, back=.true.)
95 ! if (idx > 0 .and. mem_path(1:2) == '__') then
96 ! mem_path = mem_path(idx + 1:)
97 ! end if
98 !end if
99
100 end subroutine split_mem_address
101
102 !> @brief Split the memory path into component(s)
103 !!
104 !! NB: when there is no subcomponent in the path, the
105 !! value for @par subcomponent is set to an empty string.
106 !<
107 subroutine split_mem_path(mem_path, component, subcomponent)
108 character(len=*), intent(in) :: mem_path !< path to the memory object
109 character(len=LENCOMPONENTNAME), intent(out) :: component !< name of the component (solution, model, exchange)
110 character(len=LENCOMPONENTNAME), intent(out) :: subcomponent !< name of the subcomponent (package)
111 ! local
112 character(len=LENMEMPATH) :: local_mem_path
113 integer(I4B) :: idx
114
115 call strip_context_mem_path(mem_path, local_mem_path)
116
117 idx = index(local_mem_path, mempathseparator, back=.true.)
118 ! if the separator is found at the end of the string,
119 ! the path is invalid:
120 if (idx == len_trim(local_mem_path)) then
121 write (errmsg, '(*(G0))') &
122 'Fatal error in Memory Manager, cannot split invalid memory path: ', &
123 mem_path
124
125 ! -- store error and stop program execution
126 call store_error(errmsg, terminate=.true.)
127 end if
128
129 if (idx > 0) then
130 ! when found:
131 component = local_mem_path(:idx - 1)
132 subcomponent = local_mem_path(idx + 1:)
133 else
134 ! when not found, there apparently is no subcomponent:
135 component = local_mem_path(:lencomponentname)
136 subcomponent = ''
137 end if
138
139 end subroutine split_mem_path
140
141 !> @brief Return the context from the memory path
142 !!
143 !! NB: when there is no context in the memory path, a
144 !! empty character string is returned.
145 !<
146 function get_mem_path_context(mem_path) result(res)
147 character(len=*), intent(in) :: mem_path !< path to the memory object
148 character(len=LENMEMPATH) :: res !< memory path context
149 ! local
150 integer(I4B) :: idx
151
152 ! initialize the memory path context
153 res = ' '
154
155 if (mem_path(1:2) == '__') then
156 idx = index(mem_path, mempathseparator)
157 if (idx > 0) then
158 res = mem_path(:idx)
159 end if
160 end if
161 end function get_mem_path_context
162
163 !> @brief Remove the context from the memory path
164 !!
165 !! NB: when there is no context in the memory path, the
166 !! original memory path is returned.
167 !<
168 subroutine strip_context_mem_path(mem_path, mem_path_no_context)
169 character(len=*), intent(in) :: mem_path !< path to the memory object
170 character(len=LENMEMPATH), intent(inout) :: mem_path_no_context !< path to the memory object without the context
171 ! local
172 integer(I4B) :: idx
173 character(len=LENMEMPATH) :: context
174
175 ! initialize the local mem_path
176 mem_path_no_context = mem_path
177
178 context = get_mem_path_context(mem_path)
179
180 if (len_trim(context) > 0) then
181 idx = len_trim(context)
182 mem_path_no_context = mem_path(idx + 1:)
183 end if
184
185 end subroutine strip_context_mem_path
186
187 !> @brief Generic routine to check the length of (parts of) the memory address
188 !!
189 !! The string will be trimmed before the measurement.
190 !!
191 !! @warning{if the length exceeds the maximum, a message is recorded
192 !! and the program will be stopped}
193 !!
194 !! The description should describe the part of the address that is checked
195 !! (variable, package, model, solution, exchange name) or the full memory path
196 !! itself
197 !<
198 subroutine mem_check_length(name, max_length, description)
199 character(len=*), intent(in) :: name !< string to be checked
200 integer(I4B), intent(in) :: max_length !< maximum length
201 character(len=*), intent(in) :: description !< a descriptive string
202
203 if (len(trim(name)) > max_length) then
204 write (errmsg, '(*(G0))') &
205 'Fatal error in Memory Manager, length of ', description, ' must be ', &
206 max_length, ' characters or less: ', name, '(len=', len(trim(name)), ')'
207
208 ! -- store error and stop program execution
209 call store_error(errmsg, terminate=.true.)
210 end if
211
212 end subroutine mem_check_length
213end module memoryhelpermodule
This module contains simulation constants.
Definition Constants.f90:9
integer(i4b), parameter lencomponentname
maximum length of a component name
Definition Constants.f90:18
integer(i4b), parameter lenmemaddress
maximum length of the full memory address, including variable name
Definition Constants.f90:31
integer(i4b), parameter lenmemseparator
maximum length of the memory path separator used, currently a '/'
Definition Constants.f90:26
integer(i4b), parameter lenvarname
maximum length of a variable name
Definition Constants.f90:17
integer(i4b), parameter lencontextname
maximum length of a memory manager context
Definition Constants.f90:19
integer(i4b), parameter lenmempath
maximum length of the memory path
Definition Constants.f90:27
This module defines variable data types.
Definition kind.f90:8
character(len=lenmemseparator), parameter mempathseparator
used to build up the memory address for the stored variables
character(len=lenmempath) function get_mem_path_context(mem_path)
Return the context from the memory path.
subroutine split_mem_address(mem_address, mem_path, var_name, success)
Split a memory address string into memory path and variable name.
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.
character(len=lenmemaddress) function create_mem_address(mem_path, var_name)
returns the address string of the memory object
character(len=lenmempath) function create_mem_path(component, subcomponent, context)
returns the path to the memory object
subroutine split_mem_path(mem_path, component, subcomponent)
Split the memory path into component(s).
This module contains simulation methods.
Definition Sim.f90:10
subroutine, public store_error(msg, terminate)
Store an error message.
Definition Sim.f90:94
This module contains simulation variables.
character(len=maxcharlen) errmsg
error message string