MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
mf6bmi.f90
Go to the documentation of this file.
1!> @brief This module contains the MODFLOW 6 BMI
2!!
3!! This BMI interface matches the CSDMS standard, with a few modifications:
4!!
5!! - This interface will build into a shared library that can be called from other
6!! executables and scripts, not necessarily written in Fortran. Therefore we have
7!! omitted the type-boundness of the routines, since they cannot have the
8!! bind(C,"...") attribute.
9!! - MODFLOW has internal data arrays with rank > 1 that we would like to expose. An
10!! example would be access to data in the BOUND array of GWF boundary packages (BndType).
11!! The get_value_ptr calls below support this, returning a C-style pointer to the arrays
12!! and methods have been added to query the variable's rank and shape.
13!!
14!! Note on style: BMI apparently uses underscores, we use underscores in some
15!! places but camelcase in other. Since this is a dedicated BMI interface module,
16!! we'll use underscores here as well.
17!<
18module mf6bmi
19 use mf6bmiutil
20 use mf6bmierror
22 use tdismodule, only: kper, kstp
23 use iso_c_binding, only: c_int, c_char, c_double, c_null_char, c_loc, c_ptr, &
24 c_f_pointer
25 use kindmodule, only: dp, i4b, lgp
35 use inputoutputmodule, only: getunit
36 implicit none
37
38 integer(c_int), bind(C, name="ISTDOUTTOFILE") :: istdout_to_file = 1 !< output control: =0 to screen, >0 to file
39 !DIR$ ATTRIBUTES DLLEXPORT :: istdout_to_file
40
41contains
42
43 function bmi_get_component_name(name) result(bmi_status) &
44 bind(C, name="get_component_name")
45 !DIR$ ATTRIBUTES DLLEXPORT :: bmi_get_component_name
46 ! -- dummy variables
47 character(kind=c_char), intent(inout) :: name(bmi_lencomponentname)
48 integer(kind=c_int) :: bmi_status !< BMI status code
49 ! -- local variables
50 character(len=*), parameter :: component_name = 'MODFLOW 6'
51
52 name(1:len(component_name) + 1) = &
53 string_to_char_array(component_name, len(component_name))
54 bmi_status = bmi_success
55
56 end function bmi_get_component_name
57
58 !> @brief Initialize the computational core
59 !!
60 !! It is required to have the MODFLOW 6 configuration file 'mfsim.nam'
61 !! available in the current working directory when calling this routine.
62 !!
63 !! NOTE: initialization should always be matched with a call to
64 !! bmi_finalize(). However, we currently do not support the reinitialization
65 !! of a model in the same memory space... You would have to create a new
66 !! process for that.
67 !<
68 function bmi_initialize() result(bmi_status) bind(C, name="initialize")
69 !DIR$ ATTRIBUTES DLLEXPORT :: bmi_initialize
70 ! -- dummy variables
71 integer(kind=c_int) :: bmi_status !< BMI status code
72 ! -- local variables
73
74 if (istdout_to_file > 0) then
75 ! -- open stdout file mfsim.stdout
77 !
78 ! -- set STDOUT to a physical file unit
79 open (unit=istdout, file=simstdout)
80 end if
81 !
82 ! -- initialize MODFLOW 6
83 call mf6initialize()
84
85 bmi_status = bmi_success
86
87 end function bmi_initialize
88
89 !> @brief Perform a computational time step
90 !!
91 !! It will prepare the timestep, call the calculation routine
92 !! on all the solution groups in the simulation, and finalize
93 !! the timestep by printing out diagnostics and writing output.
94 !! It can be called in succession to perform multiple steps up
95 !! to the simulation's end time is reached.
96 !<
97 function bmi_update() result(bmi_status) bind(C, name="update")
98 !DIR$ ATTRIBUTES DLLEXPORT :: bmi_update
99 ! -- dummy variables
100 integer(kind=c_int) :: bmi_status !< BMI status code
101 ! -- local variables
102 logical :: hasconverged
103
104 hasconverged = mf6update()
105
106 bmi_status = bmi_success
107 end function bmi_update
108
109 !> @brief Perform time steps until the current time reaches @p time
110 !!
111 !! Time steps are not split, so the current time can pass @p time. A time
112 !! before the current time or after the end time is an error.
113 !<
114 function bmi_update_until(time) result(bmi_status) &
115 bind(C, name="update_until")
116 !DIR$ ATTRIBUTES DLLEXPORT :: bmi_update_until
117 ! -- modules
118 use constantsmodule, only: done, dem10
120 ! -- dummy variables
121 real(kind=c_double), value, intent(in) :: time !< time to advance the simulation to
122 integer(kind=c_int) :: bmi_status !< BMI status code
123 ! -- local variables
124 real(dp) :: tol
125 logical :: hasconverged
126
127 bmi_status = bmi_failure
128
129 ! -- times within a relative 1e-10 are the same time, so that roundoff
130 ! accumulated in the simulation time does not add a time step
131 tol = dem10 * max(done, abs(time))
132 if (time < totim - tol) then
133 write (bmi_last_error, fmt_invalid_time) time, &
134 'which is before the current time'
136 return
137 end if
138 if (time > totalsimtime + tol) then
139 write (bmi_last_error, fmt_invalid_time) time, &
140 'which is after the end time'
142 return
143 end if
144
145 do while (totim < time - tol .and. .not. endofsimulation)
146 hasconverged = mf6update()
147 end do
148
149 bmi_status = bmi_success
150 end function bmi_update_until
151
152 !> @brief Clean up the initialized simulation
153 !!
154 !! Performs teardown tasks for the initialized simulation, this
155 !! call should match the call to bmi_initialize()
156 !<
157 function bmi_finalize() result(bmi_status) bind(C, name="finalize")
158 !DIR$ ATTRIBUTES DLLEXPORT :: bmi_finalize
159 ! -- modules
161 ! -- dummy variables
162 integer(kind=c_int) :: bmi_status !< BMI status code
163
164 ! we don't want a full stop() here, this disables it:
165 iforcestop = 0
166 call mf6finalize()
167
168 bmi_status = bmi_success
169
170 end function bmi_finalize
171
172 !> @brief Get the start time of the simulation
173 !!
174 !! As MODFLOW currently does not have internal time, this will be
175 !! returning 0.0 for now. New version...
176 !<
177 function get_start_time(start_time) result(bmi_status) &
178 bind(C, name="get_start_time")
179 !DIR$ ATTRIBUTES DLLEXPORT :: get_start_time
180 ! -- dummy variables
181 real(kind=c_double), intent(out) :: start_time !< start time
182 integer(kind=c_int) :: bmi_status !< BMI status code
183
184 start_time = 0.0_dp
185 bmi_status = bmi_success
186
187 end function get_start_time
188
189 !> @brief Get the end time of the simulation
190 !!
191 !! As MODFLOW does currently does not have internal time, this will be
192 !! equal to the total runtime.
193 !<
194 function get_end_time(end_time) result(bmi_status) bind(C, name="get_end_time")
195 !DIR$ ATTRIBUTES DLLEXPORT :: get_end_time
196 ! -- modules
197 use tdismodule, only: totalsimtime
198 ! -- dummy variables
199 real(kind=c_double), intent(out) :: end_time !< end time
200 integer(kind=c_int) :: bmi_status !< BMI status code
201
202 end_time = totalsimtime
203 bmi_status = bmi_success
204
205 end function get_end_time
206
207 !> @brief Get the current time of the simulation
208 !!
209 !! As MODFLOW currently does not have internal time, this will be
210 !! equal to the time passed w.r.t. the start time of the simulation.
211 !<
212 function get_current_time(current_time) result(bmi_status) &
213 bind(C, name="get_current_time")
214 !DIR$ ATTRIBUTES DLLEXPORT :: get_current_time
215 ! -- modules
216 use tdismodule, only: totim
217 ! -- dummy variables
218 real(kind=c_double), intent(out) :: current_time !< current time
219 integer(kind=c_int) :: bmi_status !< BMI status code
220
221 current_time = totim
222 bmi_status = bmi_success
223
224 end function get_current_time
225
226 !> @brief Get the time step for the simulation
227 !!
228 !! Note that the returned value may vary between and within stress periods,
229 !! depending on your time discretization settings in the TDIS package.
230 !<
231 function get_time_step(time_step) result(bmi_status) &
232 bind(C, name="get_time_step")
233 !DIR$ ATTRIBUTES DLLEXPORT :: get_time_step
234 ! -- modules
235 use tdismodule, only: delt
236 ! -- dummy variables
237 real(kind=c_double), intent(out) :: time_step !< current time step
238 integer(kind=c_int) :: bmi_status !< BMI status code
239
240 time_step = delt
241 bmi_status = bmi_success
242
243 end function get_time_step
244
245 !> @brief Get the time units of the simulation
246 !!
247 !! The units are those of TIME_UNITS in the TDIS Package, as UDUNITS
248 !! symbols, or 'unknown' when TIME_UNITS is not given.
249 !<
250 function get_time_units(c_time_units) result(bmi_status) &
251 bind(C, name="get_time_units")
252 !DIR$ ATTRIBUTES DLLEXPORT :: get_time_units
253 ! -- modules
254 use tdismodule, only: itmuni
255 ! -- dummy variables
256 character(kind=c_char), intent(out) :: c_time_units(bmi_lenunits) !< time units as a string
257 integer(kind=c_int) :: bmi_status !< BMI status code
258 ! -- local variables
259 character(len=LENUNITS) :: time_units
260
261 select case (itmuni)
262 case (1)
263 time_units = 's'
264 case (2)
265 time_units = 'min'
266 case (3)
267 time_units = 'h'
268 case (4)
269 time_units = 'd'
270 case (5)
271 time_units = 'year'
272 case default
273 time_units = 'unknown'
274 end select
275 c_time_units(1:len_trim(time_units) + 1) = &
276 string_to_char_array(trim(time_units), len_trim(time_units))
277 bmi_status = bmi_success
278
279 end function get_time_units
280
281 !> @brief Get the number of input variables in the simulation
282 !!
283 !! This concerns all variables stored in the memory manager
284 !<
285 function get_input_item_count(count) result(bmi_status) &
286 bind(C, name="get_input_item_count")
287 !DIR$ ATTRIBUTES DLLEXPORT :: get_input_item_count
288 ! -- dummy variables
289 integer(kind=c_int), intent(out) :: count !< the number of input variables
290 integer(kind=c_int) :: bmi_status !< BMI status code
291
292 count = memorystore%count()
293
294 bmi_status = bmi_success
295
296 end function get_input_item_count
297
298 !> @brief Get the number of output variables in the simulation
299 !!
300 !! This concerns all variables stored in the memory manager
301 !<
302 function get_output_item_count(count) result(bmi_status) &
303 bind(C, name="get_output_item_count")
304 !DIR$ ATTRIBUTES DLLEXPORT :: get_output_item_count
305 ! -- dummy variables
306 integer(kind=c_int), intent(out) :: count !< the number of output variables
307 integer(kind=c_int) :: bmi_status !< BMI status code
308
309 count = memorystore%count()
310
311 bmi_status = bmi_success
312
313 end function get_output_item_count
314
315 !> @brief Returns all input variables in the simulation
316 !!
317 !! This functions returns the full address for all variables in the
318 !! memory manager
319 !!
320 !! The array @p c_names should be pre-allocated of proper size:
321 !!
322 !! size = BMI_LENVARADDRESS * get_input_item_count()
323 !!
324 !! The strings will be written contiguously with stride equal to
325 !! BMI_LENVARADDRESS and nul-terminated where the trailing spaces start:
326 !!
327 !! c_names = 'variable_address_1\\0 ... variable_address_2\\0 ... ' etc.
328 !<
329 function get_input_var_names(c_names) result(bmi_status) &
330 bind(C, name="get_input_var_names")
331 !DIR$ ATTRIBUTES DLLEXPORT :: get_input_var_names
332 ! -- dummy variables
333 character(kind=c_char, len=1), intent(inout) :: c_names(*) !< array with memory paths for input variables
334 integer(kind=c_int) :: bmi_status !< BMI status code
335 ! -- local variables
336 integer(I4B) :: start, i
337 type(memorycontaineriteratortype), allocatable :: itr
338 type(memorytype), pointer :: mt => null()
339 character(len=LENMEMADDRESS) :: var_address
340
341 start = 1
342 itr = memorystore%iterator()
343 do while (itr%has_next())
344 call itr%next()
345 mt => itr%value()
346 var_address = create_mem_address(mt%path, mt%name)
347 do i = 1, len(trim(var_address))
348 c_names(start + i - 1) = var_address(i:i)
349 end do
350 c_names(start + i) = c_null_char
351 start = start + bmi_lenvaraddress
352 end do
353
354 bmi_status = bmi_success
355
356 end function get_input_var_names
357
358 !> @brief Returns all output variables in the simulation
359 !!
360 !! This function works analogously to get_input_var_names(),
361 !! and currently returns the same set of memory variables,
362 !! which is all of them!
363 !<
364 function get_output_var_names(c_names) result(bmi_status) &
365 bind(C, name="get_output_var_names")
366 !DIR$ ATTRIBUTES DLLEXPORT :: get_output_var_names
367 ! -- dummy variables
368 character(kind=c_char, len=1), intent(inout) :: c_names(*) !< array with memory paths for output variables
369 integer(kind=c_int) :: bmi_status !< BMI status code
370 ! -- local variables
371 integer(I4B) :: start, i
372 type(memorycontaineriteratortype), allocatable :: itr
373 type(memorytype), pointer :: mt => null()
374 character(len=LENMEMADDRESS) :: var_address
375
376 start = 1
377 itr = memorystore%iterator()
378 do while (itr%has_next())
379 call itr%next()
380 mt => itr%value()
381 var_address = create_mem_address(mt%path, mt%name)
382 do i = 1, len(trim(var_address))
383 c_names(start + i - 1) = var_address(i:i)
384 end do
385 c_names(start + i) = c_null_char
386 start = start + bmi_lenvaraddress
387 end do
388
389 bmi_status = bmi_success
390
391 end function get_output_var_names
392
393 !> @brief Get the size (in bytes) of a single element of a variable
394 !<
395 function get_var_itemsize(c_var_address, var_size) result(bmi_status) &
396 bind(C, name="get_var_itemsize")
397 !DIR$ ATTRIBUTES DLLEXPORT :: get_var_itemsize
398 ! -- dummy variables
399 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
400 integer(kind=c_int), intent(out) :: var_size !< size of the element in bytes
401 integer(kind=c_int) :: bmi_status !< BMI status code
402 ! -- local variables
403 character(len=LENMEMPATH) :: mem_path
404 character(len=LENVARNAME) :: var_name
405 logical(LGP) :: valid
406
407 bmi_status = bmi_success
408
409 call split_address(c_var_address, mem_path, var_name, valid)
410 if (.not. valid) then
411 bmi_status = bmi_failure
412 return
413 end if
414
415 call get_mem_elem_size(var_name, mem_path, var_size)
416 if (var_size == -1) bmi_status = bmi_failure
417
418 end function get_var_itemsize
419
420 !> @brief Get size of the variable, in bytes
421 !<
422 function get_var_nbytes(c_var_address, var_nbytes) result(bmi_status) &
423 bind(C, name="get_var_nbytes")
424 !DIR$ ATTRIBUTES DLLEXPORT :: get_var_nbytes
425 ! -- dummy variables
426 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
427 integer(kind=c_int), intent(out) :: var_nbytes !< size in bytes
428 integer(kind=c_int) :: bmi_status !< BMI status code
429 ! -- local variables
430 integer(I4B) :: var_size, isize
431 character(len=LENMEMPATH) :: mem_path
432 character(len=LENVARNAME) :: var_name
433 logical(LGP) :: valid
434
435 bmi_status = bmi_success
436
437 call split_address(c_var_address, mem_path, var_name, valid)
438 if (.not. valid) then
439 bmi_status = bmi_failure
440 return
441 end if
442
443 call get_mem_elem_size(var_name, mem_path, var_size)
444 if (var_size == -1) bmi_status = bmi_failure
445 call get_isize(var_name, mem_path, isize)
446 if (isize == -1) bmi_status = bmi_failure
447
448 var_nbytes = var_size * isize
449
450 end function get_var_nbytes
451
452 !> @brief Copy the double precision values of a variable into the array
453 !!
454 !! The copied variable us located at @p c_var_address. The caller should
455 !! provide @p c_arr_ptr pointing to an array of the proper shape (the
456 !! BMI function get_var_shape() can be used to create it). Multi-dimensional
457 !! arrays are supported.
458 !<
459 function get_value_double(c_var_address, c_arr_ptr) result(bmi_status) &
460 bind(C, name="get_value_double")
461 !DIR$ ATTRIBUTES DLLEXPORT :: get_value_double
462 ! -- modules
464 ! -- dummy variables
465 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
466 type(c_ptr), intent(in) :: c_arr_ptr !< pointer to the double precision array
467 integer(kind=c_int) :: bmi_status !< BMI status code
468 ! -- local variables
469 character(len=LENMEMPATH) :: mem_path
470 character(len=LENVARNAME) :: var_name
471 logical(LGP) :: valid
472 integer(I4B) :: rank
473 real(dp), pointer :: src_ptr, tgt_ptr
474 real(dp), dimension(:), pointer, contiguous :: src1d_ptr, tgt1d_ptr
475 real(dp), dimension(:, :), pointer, contiguous :: src2d_ptr, tgt2d_ptr
476 real(dp), dimension(:, :, :), pointer, contiguous :: src3d_ptr, tgt3d_ptr
477 integer(I4B) :: i, j, k
478
479 bmi_status = bmi_success
480
481 call split_address(c_var_address, mem_path, var_name, valid)
482 if (.not. valid) then
483 bmi_status = bmi_failure
484 return
485 end if
486
487 ! convert pointer and copy data from memory manager into
488 ! the passed array, using loops to avoid stack overflow
489 rank = -1
490 call get_mem_rank(var_name, mem_path, rank)
491
492 if (rank == 0) then
493 call mem_setptr(src_ptr, var_name, mem_path)
494 call c_f_pointer(c_arr_ptr, tgt_ptr)
495 tgt_ptr = src_ptr
496 else if (rank == 1) then
497 call mem_setptr(src1d_ptr, var_name, mem_path)
498 call c_f_pointer(c_arr_ptr, tgt1d_ptr, shape(src1d_ptr))
499 do i = 1, size(tgt1d_ptr)
500 tgt1d_ptr(i) = src1d_ptr(i)
501 end do
502 else if (rank == 2) then
503 call mem_setptr(src2d_ptr, var_name, mem_path)
504 call c_f_pointer(c_arr_ptr, tgt2d_ptr, shape(src2d_ptr))
505 do j = 1, size(tgt2d_ptr, 2)
506 do i = 1, size(tgt2d_ptr, 1)
507 tgt2d_ptr(i, j) = src2d_ptr(i, j)
508 end do
509 end do
510 else if (rank == 3) then
511 call mem_setptr(src3d_ptr, var_name, mem_path)
512 call c_f_pointer(c_arr_ptr, tgt3d_ptr, shape(src3d_ptr))
513 do k = 1, size(tgt3d_ptr, 3)
514 do j = 1, size(tgt3d_ptr, 2)
515 do i = 1, size(tgt3d_ptr, 1)
516 tgt3d_ptr(i, j, k) = src3d_ptr(i, j, k)
517 end do
518 end do
519 end do
520 else
521 write (bmi_last_error, fmt_unsupported_rank) trim(var_name)
523 bmi_status = bmi_failure
524 return
525 end if
526
527 end function get_value_double
528
529 !> @brief Copy the integer values of a variable into the array
530 !!
531 !! The copied variable is located at @p c_var_address. The caller should
532 !! provide @p c_arr_ptr pointing to an array of the proper shape (the
533 !! BMI function get_var_shape() can be used to create it). Multi-dimensional
534 !! arrays are supported.
535 !<
536 function get_value_int(c_var_address, c_arr_ptr) result(bmi_status) &
537 bind(C, name="get_value_int")
538 !DIR$ ATTRIBUTES DLLEXPORT :: get_value_int
539 ! -- modules
541 ! -- dummy variables
542 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
543 type(c_ptr), intent(in) :: c_arr_ptr !< pointer to the integer array
544 integer(kind=c_int) :: bmi_status !< BMI status code
545 ! -- local variables
546 character(len=LENMEMPATH) :: mem_path
547 character(len=LENVARNAME) :: var_name
548 logical(LGP) :: valid
549 integer(I4B) :: rank
550 integer(I4B), pointer :: src_ptr, tgt_ptr
551 integer(I4B), dimension(:), pointer, contiguous :: src1d_ptr, tgt1d_ptr
552 integer(I4B), dimension(:, :), pointer, contiguous :: src2d_ptr, tgt2d_ptr
553 integer(I4B), dimension(:, :, :), pointer, contiguous :: src3d_ptr, tgt3d_ptr
554 integer(I4B) :: i, j, k
555
556 bmi_status = bmi_success
557
558 call split_address(c_var_address, mem_path, var_name, valid)
559 if (.not. valid) then
560 bmi_status = bmi_failure
561 return
562 end if
563
564 ! convert pointer and copy data from memory manager into
565 ! the passed array, using loops to avoid stack overflow
566 rank = -1
567 call get_mem_rank(var_name, mem_path, rank)
568
569 if (rank == 0) then
570 call mem_setptr(src_ptr, var_name, mem_path)
571 call c_f_pointer(c_arr_ptr, tgt_ptr)
572 tgt_ptr = src_ptr
573 else if (rank == 1) then
574 call mem_setptr(src1d_ptr, var_name, mem_path)
575 call c_f_pointer(c_arr_ptr, tgt1d_ptr, shape(src1d_ptr))
576 do i = 1, size(tgt1d_ptr)
577 tgt1d_ptr(i) = src1d_ptr(i)
578 end do
579 else if (rank == 2) then
580 call mem_setptr(src2d_ptr, var_name, mem_path)
581 call c_f_pointer(c_arr_ptr, tgt2d_ptr, shape(src2d_ptr))
582 do j = 1, size(tgt2d_ptr, 2)
583 do i = 1, size(tgt2d_ptr, 1)
584 tgt2d_ptr(i, j) = src2d_ptr(i, j)
585 end do
586 end do
587 else if (rank == 3) then
588 call mem_setptr(src3d_ptr, var_name, mem_path)
589 call c_f_pointer(c_arr_ptr, tgt3d_ptr, shape(src3d_ptr))
590 do k = 1, size(tgt3d_ptr, 3)
591 do j = 1, size(tgt3d_ptr, 2)
592 do i = 1, size(tgt3d_ptr, 1)
593 tgt3d_ptr(i, j, k) = src3d_ptr(i, j, k)
594 end do
595 end do
596 end do
597 else
598 write (bmi_last_error, fmt_unsupported_rank) trim(var_name)
600 bmi_status = bmi_failure
601 return
602 end if
603
604 end function get_value_int
605
606 !> @brief Copy the logical scalar value into the array
607 !!
608 !! The copied variable us located at @p c_var_address. The caller should
609 !! provide @p c_arr_ptr pointing to a scalar array with rank=0.
610 !<
611 function get_value_bool(c_var_address, c_arr_ptr) result(bmi_status) &
612 bind(C, name="get_value_bool")
613 !DIR$ ATTRIBUTES DLLEXPORT :: get_value_bool
614 ! -- modules
616 ! -- dummy variables
617 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
618 type(c_ptr), intent(in) :: c_arr_ptr !< pointer to the logical array
619 integer(kind=c_int) :: bmi_status !< BMI status code
620 ! -- local variables
621 character(len=LENMEMPATH) :: mem_path
622 character(len=LENVARNAME) :: var_name
623 logical(LGP) :: valid
624 integer(I4B) :: rank
625 logical(LGP), pointer :: src_ptr, tgt_ptr
626
627 bmi_status = bmi_success
628
629 call split_address(c_var_address, mem_path, var_name, valid)
630 if (.not. valid) then
631 bmi_status = bmi_failure
632 return
633 end if
634
635 rank = -1
636 call get_mem_rank(var_name, mem_path, rank)
637
638 ! convert pointer
639 if (rank == 0) then
640 call mem_setptr(src_ptr, var_name, mem_path)
641 call c_f_pointer(c_arr_ptr, tgt_ptr)
642 tgt_ptr = src_ptr
643 else
644 write (bmi_last_error, fmt_unsupported_rank) trim(var_name)
646 bmi_status = bmi_failure
647 return
648 end if
649
650 end function get_value_bool
651
652 !> @brief Copy the string(s) of a variable into the array
653 !!
654 !! The copied variable is located at @p c_var_address. The caller should
655 !! provide @p c_arr_ptr pointing to an array of the proper shape (the
656 !! BMI function get_var_shape() can be used to create it). For strings
657 !! currently scalars and 1d arrays (of CharacterStringType) are
658 !< supported
659 function get_value_string(c_var_address, c_arr_ptr) result(bmi_status) &
660 bind(C, name="get_value_string")
661 !DIR$ ATTRIBUTES DLLEXPORT :: get_value_string
662 ! -- modules
663 ! -- dummy variables
664 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
665 type(c_ptr), intent(in) :: c_arr_ptr !< pointer to the string array
666 integer(kind=c_int) :: bmi_status !< BMI status code
667 ! -- local variables
668 character(len=LENMEMPATH) :: mem_path
669 character(len=LENVARNAME) :: var_name
670 logical(LGP) :: valid
671 integer(I4B) :: rank
672 character(len=:), pointer :: srcstr
673 character(kind=c_char), pointer :: tgtstr(:)
674 type(characterstringtype), dimension(:), pointer, contiguous :: srccharstr1d
675 character(kind=c_char), pointer :: tgtstr1d(:, :)
676 character(:), allocatable :: tempstr
677 integer(I4B) :: i, ilen, isize
678
679 bmi_status = bmi_success
680
681 call split_address(c_var_address, mem_path, var_name, valid)
682 if (.not. valid) then
683 bmi_status = bmi_failure
684 return
685 end if
686
687 ! single string, or array of strings (CharacterStringType)
688 rank = -1
689 call get_mem_rank(var_name, mem_path, rank)
690
691 if (rank == 0) then
692 ! a string scalar
693 call mem_setptr(srcstr, var_name, mem_path)
694 call get_mem_elem_size(var_name, mem_path, ilen)
695 call c_f_pointer(c_arr_ptr, tgtstr, shape=[ilen + 1])
696
697 tgtstr(1:len(srcstr) + 1) = string_to_char_array(srcstr, len(srcstr))
698
699 else if (rank == 1) then
700 ! an array of strings
701 call mem_setptr(srccharstr1d, var_name, mem_path)
702 if (.not. associated(srccharstr1d)) then
703 write (bmi_last_error, fmt_general_err) 'string type not supported in API'
705 bmi_status = bmi_failure
706 return
707 end if
708
709 ! create fortran pointer to C data array
710 call get_isize(var_name, mem_path, isize)
711 call get_mem_elem_size(var_name, mem_path, ilen)
712 call c_f_pointer(c_arr_ptr, tgtstr1d, shape=[ilen + 1, isize])
713
714 ! allocate work array to handle CharacterStringType,
715 ! and copy the strings
716 allocate (character(ilen) :: tempstr)
717 do i = 1, isize
718 tempstr = srccharstr1d(i)
719 tgtstr1d(1:ilen + 1, i) = string_to_char_array(tempstr, ilen)
720 end do
721 deallocate (tempstr)
722 else
723 write (bmi_last_error, fmt_unsupported_rank) trim(var_name)
725 bmi_status = bmi_failure
726 return
727 end if
728
729 end function get_value_string
730
731 !> @brief Copy the value of a variable into the array
732 !!
733 !! The copied variable is located at @p c_var_address. The caller should
734 !! provide @p c_arr_ptr pointing to an array of the proper shape (the
735 !! BMI function get_var_shape() can be used to create it). Multi-dimensional
736 !! arrays are supported.
737 !<
738 function get_value(c_var_address, c_arr_ptr) result(bmi_status) &
739 bind(C, name="get_value")
740 !DIR$ ATTRIBUTES DLLEXPORT :: get_value
741 ! -- modules
742 use constantsmodule, only: lenmemtype
743 ! -- dummy variables
744 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
745 type(c_ptr), intent(inout) :: c_arr_ptr !< pointer to the array
746 integer(kind=c_int) :: bmi_status !< BMI status code
747 ! -- local variables
748 character(len=LENMEMPATH) :: mem_path
749 character(len=LENMEMTYPE) :: mem_type
750 character(len=LENVARNAME) :: var_name
751 logical(LGP) :: valid
752
753 bmi_status = bmi_success
754
755 call split_address(c_var_address, mem_path, var_name, valid)
756 if (.not. valid) then
757 bmi_status = bmi_failure
758 return
759 end if
760
761 call get_mem_type(var_name, mem_path, mem_type)
762
763 if (index(mem_type, "DOUBLE") /= 0) then
764 bmi_status = get_value_double(c_var_address, c_arr_ptr)
765 else if (index(mem_type, "INTEGER") /= 0) then
766 bmi_status = get_value_int(c_var_address, c_arr_ptr)
767 else if (index(mem_type, "LOGICAL") /= 0) then
768 bmi_status = get_value_bool(c_var_address, c_arr_ptr)
769 else if (index(mem_type, "STRING") /= 0) then
770 bmi_status = get_value_string(c_var_address, c_arr_ptr)
771 else
772 write (bmi_last_error, fmt_unsupported_type) trim(var_name)
774 bmi_status = bmi_failure
775 return
776 end if
777
778 end function get_value
779
780 !> @brief Get a pointer to an array
781 !!
782 !! The array is located at @p c_var_address. There is no copying of data involved.
783 !! Multi-dimensional arrays are supported and the get_var_rank() function
784 !! can be used to get the variable's dimensionality, and get_var_shape() for
785 !! its shape.
786 !<
787 function get_value_ptr(c_var_address, c_arr_ptr) result(bmi_status) &
788 bind(C, name="get_value_ptr")
789 !DIR$ ATTRIBUTES DLLEXPORT :: get_value_ptr
790 ! -- modules
791 use constantsmodule, only: lenmemtype
792 ! -- dummy variables
793 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
794 type(c_ptr), intent(inout) :: c_arr_ptr !< pointer to the array
795 integer(kind=c_int) :: bmi_status !< BMI status code
796 ! -- local variables
797 character(len=LENMEMPATH) :: mem_path
798 character(len=LENMEMTYPE) :: mem_type
799 character(len=LENVARNAME) :: var_name
800 logical(LGP) :: valid
801
802 bmi_status = bmi_success
803
804 call split_address(c_var_address, mem_path, var_name, valid)
805 if (.not. valid) then
806 bmi_status = bmi_failure
807 return
808 end if
809
810 call get_mem_type(var_name, mem_path, mem_type)
811
812 if (index(mem_type, "DOUBLE") /= 0) then
813 bmi_status = get_value_ptr_double(c_var_address, c_arr_ptr)
814 else if (index(mem_type, "INTEGER") /= 0) then
815 bmi_status = get_value_ptr_int(c_var_address, c_arr_ptr)
816 else if (index(mem_type, "LOGICAL") /= 0) then
817 bmi_status = get_value_ptr_bool(c_var_address, c_arr_ptr)
818 else
819 write (bmi_last_error, fmt_unsupported_type) trim(var_name)
821 bmi_status = bmi_failure
822 return
823 end if
824
825 end function get_value_ptr
826
827 !> @brief Get a pointer to the array of double precision numbers
828 !!
829 !! The array is located at @p c_var_address. There is no copying of data involved.
830 !! Multi-dimensional arrays are supported and the get_var_rank() function
831 !! can be used to get the variable's dimensionality, and get_var_shape() for
832 !! its shape.
833 !<
834 function get_value_ptr_double(c_var_address, c_arr_ptr) result(bmi_status) &
835 bind(C, name="get_value_ptr_double")
836 !DIR$ ATTRIBUTES DLLEXPORT :: get_value_ptr_double
837 ! -- dummy variables
838 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
839 type(c_ptr), intent(inout) :: c_arr_ptr !< pointer to the array
840 integer(kind=c_int) :: bmi_status !< BMI status code
841 ! -- local variables
842 character(len=LENMEMPATH) :: mem_path
843 character(len=LENVARNAME) :: var_name
844 logical(LGP) :: valid
845 real(dp), pointer :: scalar_ptr
846 real(dp), dimension(:), pointer, contiguous :: array_ptr
847 real(dp), dimension(:, :), pointer, contiguous :: array2d_ptr
848 real(dp), dimension(:, :, :), pointer, contiguous :: array3d_ptr
849 integer(I4B) :: rank
850
851 bmi_status = bmi_success
852
853 call split_address(c_var_address, mem_path, var_name, valid)
854 if (.not. valid) then
855 bmi_status = bmi_failure
856 return
857 end if
858
859 rank = -1
860 call get_mem_rank(var_name, mem_path, rank)
861 if (rank == 0) then
862 call mem_setptr(scalar_ptr, var_name, mem_path)
863 c_arr_ptr = c_loc(scalar_ptr)
864 else if (rank == 1) then
865 call mem_setptr(array_ptr, var_name, mem_path)
866 c_arr_ptr = c_loc(array_ptr)
867 else if (rank == 2) then
868 call mem_setptr(array2d_ptr, var_name, mem_path)
869 c_arr_ptr = c_loc(array2d_ptr)
870 else if (rank == 3) then
871 call mem_setptr(array3d_ptr, var_name, mem_path)
872 c_arr_ptr = c_loc(array3d_ptr)
873 else
874 write (bmi_last_error, fmt_unsupported_rank) trim(var_name)
876 bmi_status = bmi_failure
877 return
878 end if
879
880 end function get_value_ptr_double
881
882 !> @brief Get a pointer to the array of integer numbers
883 !!
884 !! The array is located at @p c_var_address. There is no copying of data involved.
885 !! Multi-dimensional arrays are supported and the get_var_rank() function
886 !! can be used to get the variable's dimensionality.
887 !<
888 function get_value_ptr_int(c_var_address, c_arr_ptr) result(bmi_status) &
889 bind(C, name="get_value_ptr_int")
890 !DIR$ ATTRIBUTES DLLEXPORT :: get_value_ptr_int
891 ! -- dummy variables
892 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
893 type(c_ptr), intent(inout) :: c_arr_ptr !< pointer to the array
894 integer(kind=c_int) :: bmi_status !< BMI status code
895 ! -- local variables
896 character(len=LENMEMPATH) :: mem_path
897 character(len=LENVARNAME) :: var_name
898 logical(LGP) :: valid
899 integer(I4B) :: rank
900 integer(I4B), pointer :: scalar_ptr
901 integer(I4B), dimension(:), pointer, contiguous :: array_ptr
902 integer(I4B), dimension(:, :), pointer, contiguous :: array2d_ptr
903 integer(I4B), dimension(:, :, :), pointer, contiguous :: array3d_ptr
904
905 bmi_status = bmi_success
906
907 call split_address(c_var_address, mem_path, var_name, valid)
908 if (.not. valid) then
909 bmi_status = bmi_failure
910 return
911 end if
912
913 rank = -1
914 call get_mem_rank(var_name, mem_path, rank)
915
916 if (rank == 0) then
917 call mem_setptr(scalar_ptr, var_name, mem_path)
918 c_arr_ptr = c_loc(scalar_ptr)
919 else if (rank == 1) then
920 call mem_setptr(array_ptr, var_name, mem_path)
921 c_arr_ptr = c_loc(array_ptr)
922 else if (rank == 2) then
923 call mem_setptr(array2d_ptr, var_name, mem_path)
924 c_arr_ptr = c_loc(array2d_ptr)
925 else if (rank == 3) then
926 call mem_setptr(array3d_ptr, var_name, mem_path)
927 c_arr_ptr = c_loc(array3d_ptr)
928 else
929 write (bmi_last_error, fmt_unsupported_rank) trim(var_name)
931 bmi_status = bmi_failure
932 return
933 end if
934
935 end function get_value_ptr_int
936
937 !> @brief Get a pointer to the logical scalar value
938 !!
939 !! Only scalar values (with rank=0) are supported.
940 !<
941 function get_value_ptr_bool(c_var_address, c_arr_ptr) result(bmi_status) &
942 bind(C, name="get_value_ptr_bool")
943 !DIR$ ATTRIBUTES DLLEXPORT :: get_value_ptr_bool
944 ! -- dummy variables
945 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
946 type(c_ptr), intent(inout) :: c_arr_ptr !< pointer to the array
947 integer(kind=c_int) :: bmi_status !< BMI status code
948 ! -- local variables
949 character(len=LENMEMPATH) :: mem_path
950 character(len=LENVARNAME) :: var_name
951 logical(LGP) :: valid
952 logical(LGP), pointer :: scalar_ptr
953 integer(I4B) :: rank
954
955 bmi_status = bmi_success
956
957 call split_address(c_var_address, mem_path, var_name, valid)
958 if (.not. valid) then
959 bmi_status = bmi_failure
960 return
961 end if
962
963 rank = -1
964 call get_mem_rank(var_name, mem_path, rank)
965 if (rank == 0) then
966 call mem_setptr(scalar_ptr, var_name, mem_path)
967 c_arr_ptr = c_loc(scalar_ptr)
968 else
969 write (bmi_last_error, fmt_unsupported_rank) trim(var_name)
971 bmi_status = bmi_failure
972 return
973 end if
974
975 end function get_value_ptr_bool
976
977 !> @brief Set new values for a given variable
978 !!
979 !! The array pointed to by @p c_arr_ptr can have rank equal to 0, 1, or 2
980 !! and should have a C-style layout, which is particularly important for
981 !! rank > 1.
982 !<
983 function set_value(c_var_address, c_arr_ptr) result(bmi_status) &
984 bind(C, name="set_value")
985 !DIR$ ATTRIBUTES DLLEXPORT :: set_value
986 ! -- modules
987 use constantsmodule, only: lenmemtype
988 ! -- dummy variables
989 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
990 type(c_ptr), intent(inout) :: c_arr_ptr !< pointer to the array
991 integer(kind=c_int) :: bmi_status !< BMI status code
992 ! -- local variables
993 character(len=LENMEMPATH) :: mem_path
994 character(len=LENMEMTYPE) :: mem_type
995 character(len=LENVARNAME) :: var_name
996 logical(LGP) :: valid
997
998 bmi_status = bmi_success
999
1000 call split_address(c_var_address, mem_path, var_name, valid)
1001 if (.not. valid) then
1002 bmi_status = bmi_failure
1003 return
1004 end if
1005
1006 call get_mem_type(var_name, mem_path, mem_type)
1007
1008 if (index(mem_type, "DOUBLE") /= 0) then
1009 bmi_status = set_value_double(c_var_address, c_arr_ptr)
1010 else if (index(mem_type, "INTEGER") /= 0) then
1011 bmi_status = set_value_int(c_var_address, c_arr_ptr)
1012 else if (index(mem_type, "LOGICAL") /= 0) then
1013 bmi_status = set_value_bool(c_var_address, c_arr_ptr)
1014 else
1015 write (bmi_last_error, fmt_unsupported_type) trim(var_name)
1017 bmi_status = bmi_failure
1018 return
1019 end if
1020
1021 end function set_value
1022
1023 !> @brief Set new values for a variable of type double
1024 !!
1025 !! The array pointed to by @p c_arr_ptr can have rank equal to 0, 1, or 2
1026 !! and should have a C-style layout, which is particularly important for
1027 !! rank > 1.
1028 !<
1029 function set_value_double(c_var_address, c_arr_ptr) result(bmi_status) &
1030 bind(C, name="set_value_double")
1031 !DIR$ ATTRIBUTES DLLEXPORT :: set_value_double
1032 ! -- modules
1034 ! -- dummy variables
1035 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
1036 type(c_ptr), intent(in) :: c_arr_ptr !< pointer to the double precision array
1037 integer(kind=c_int) :: bmi_status !< BMI status code
1038 ! -- local variables
1039 character(len=LENMEMPATH) :: mem_path
1040 character(len=LENVARNAME) :: var_name
1041 logical(LGP) :: valid
1042 integer(I4B) :: rank
1043 real(dp), pointer :: src_ptr, tgt_ptr
1044 real(dp), dimension(:), pointer, contiguous :: src1d_ptr, tgt1d_ptr
1045 real(dp), dimension(:, :), pointer, contiguous :: src2d_ptr, tgt2d_ptr
1046 integer(I4B) :: i, j
1047 integer(I4B) :: status
1048
1049 bmi_status = bmi_success
1050
1051 call split_address(c_var_address, mem_path, var_name, valid)
1052 if (.not. valid) then
1053 bmi_status = bmi_failure
1054 return
1055 end if
1056
1057 ! convert pointer and copy, using loops to avoid stack overflow
1058 rank = -1
1059 call get_mem_rank(var_name, mem_path, rank)
1060
1061 if (rank == 0) then
1062 call mem_setptr(tgt_ptr, var_name, mem_path)
1063 call c_f_pointer(c_arr_ptr, src_ptr)
1064 tgt_ptr = src_ptr
1065 else if (rank == 1) then
1066 call mem_setptr(tgt1d_ptr, var_name, mem_path)
1067 call c_f_pointer(c_arr_ptr, src1d_ptr, shape(tgt1d_ptr))
1068 do i = 1, size(tgt1d_ptr)
1069 tgt1d_ptr(i) = src1d_ptr(i)
1070 end do
1071 else if (rank == 2) then
1072 call mem_setptr(tgt2d_ptr, var_name, mem_path)
1073 call c_f_pointer(c_arr_ptr, src2d_ptr, shape(tgt2d_ptr))
1074 do j = 1, size(tgt2d_ptr, 2)
1075 do i = 1, size(tgt2d_ptr, 1)
1076 tgt2d_ptr(i, j) = src2d_ptr(i, j)
1077 end do
1078 end do
1079 else
1080 write (bmi_last_error, fmt_unsupported_rank) trim(var_name)
1082 bmi_status = bmi_failure
1083 return
1084 end if
1085
1086 ! trigger event:
1087 call on_memory_set(var_name, mem_path, status)
1088 if (status /= 0) then
1089 ! something went terribly wrong here, aborting
1090 write (bmi_last_error, fmt_invalid_mem_access) trim(var_name)
1092 bmi_status = bmi_failure
1093 return
1094 end if
1095
1096 end function set_value_double
1097
1098 !> @brief Set new values for a variable of type integer
1099 !!
1100 !! The array pointed to by @p c_arr_ptr can have rank equal to 0, 1, or 2.
1101 !<
1102 function set_value_int(c_var_address, c_arr_ptr) result(bmi_status) &
1103 bind(C, name="set_value_int")
1104 !DIR$ ATTRIBUTES DLLEXPORT :: set_value_int
1105 ! -- modules
1107 ! -- dummy variables
1108 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
1109 type(c_ptr), intent(in) :: c_arr_ptr !< pointer to the integer array
1110 integer(kind=c_int) :: bmi_status !< BMI status code
1111 ! -- local variables
1112 character(len=LENMEMPATH) :: mem_path
1113 character(len=LENVARNAME) :: var_name
1114 logical(LGP) :: valid
1115 integer(I4B) :: rank
1116 integer(I4B), pointer :: src_ptr, tgt_ptr
1117 integer(I4B), dimension(:), pointer, contiguous :: src1d_ptr, tgt1d_ptr
1118 integer(I4B), dimension(:, :), pointer, contiguous :: src2d_ptr, tgt2d_ptr
1119 integer(I4B) :: i, j
1120 integer(I4B) :: status
1121
1122 bmi_status = bmi_success
1123
1124 call split_address(c_var_address, mem_path, var_name, valid)
1125 if (.not. valid) then
1126 bmi_status = bmi_failure
1127 return
1128 end if
1129
1130 ! convert pointer and copy, using loops to avoid stack overflow
1131 rank = -1
1132 call get_mem_rank(var_name, mem_path, rank)
1133
1134 if (rank == 0) then
1135 call mem_setptr(tgt_ptr, var_name, mem_path)
1136 call c_f_pointer(c_arr_ptr, src_ptr)
1137 tgt_ptr = src_ptr
1138 else if (rank == 1) then
1139 call mem_setptr(tgt1d_ptr, var_name, mem_path)
1140 call c_f_pointer(c_arr_ptr, src1d_ptr, shape(tgt1d_ptr))
1141 do i = 1, size(tgt1d_ptr)
1142 tgt1d_ptr(i) = src1d_ptr(i)
1143 end do
1144 else if (rank == 2) then
1145 call mem_setptr(tgt2d_ptr, var_name, mem_path)
1146 call c_f_pointer(c_arr_ptr, src2d_ptr, shape(tgt2d_ptr))
1147 do j = 1, size(tgt2d_ptr, 2)
1148 do i = 1, size(tgt2d_ptr, 1)
1149 tgt2d_ptr(i, j) = src2d_ptr(i, j)
1150 end do
1151 end do
1152 else
1153 write (bmi_last_error, fmt_unsupported_rank) trim(var_name)
1155 bmi_status = bmi_failure
1156 return
1157 end if
1158
1159 ! trigger event:
1160 call on_memory_set(var_name, mem_path, status)
1161 if (status /= 0) then
1162 ! something went terribly wrong here, aborting
1163 write (bmi_last_error, fmt_invalid_mem_access) trim(var_name)
1165 bmi_status = bmi_failure
1166 return
1167 end if
1168
1169 end function set_value_int
1170
1171 !> @brief Set new value for a logical scalar variable
1172 !!
1173 !! The array pointed to by @p c_arr_ptr must have a rank equal to 0.
1174 !<
1175 function set_value_bool(c_var_address, c_arr_ptr) result(bmi_status) &
1176 bind(C, name="set_value_bool")
1177 !DIR$ ATTRIBUTES DLLEXPORT :: set_value_bool
1178 ! -- modules
1180 ! -- dummy variables
1181 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
1182 type(c_ptr), intent(in) :: c_arr_ptr !< pointer to the logical array
1183 integer(kind=c_int) :: bmi_status !< BMI status code
1184 ! -- local variables
1185 character(len=LENMEMPATH) :: mem_path
1186 character(len=LENVARNAME) :: var_name
1187 logical(LGP) :: valid
1188 integer(I4B) :: rank
1189 logical(LGP), pointer :: src_ptr, tgt_ptr
1190 integer(I4B) :: status
1191
1192 bmi_status = bmi_success
1193
1194 call split_address(c_var_address, mem_path, var_name, valid)
1195 if (.not. valid) then
1196 bmi_status = bmi_failure
1197 return
1198 end if
1199
1200 rank = -1
1201 call get_mem_rank(var_name, mem_path, rank)
1202
1203 ! convert pointer
1204 if (rank == 0) then
1205 call mem_setptr(tgt_ptr, var_name, mem_path)
1206 call c_f_pointer(c_arr_ptr, src_ptr)
1207 tgt_ptr = src_ptr
1208 else
1209 write (bmi_last_error, fmt_unsupported_rank) trim(var_name)
1211 bmi_status = bmi_failure
1212 return
1213 end if
1214
1215 ! trigger event:
1216 call on_memory_set(var_name, mem_path, status)
1217 if (status /= 0) then
1218 ! something went terribly wrong here, aborting
1219 write (bmi_last_error, fmt_invalid_mem_access) trim(var_name)
1221 bmi_status = bmi_failure
1222 return
1223 end if
1224
1225 end function set_value_bool
1226
1227 !> @brief Copy the values of a variable at the given indices into the array
1228 !!
1229 !! The indices are zero-based positions in the variable's values in C-style
1230 !! (row-major) order of the shape returned by get_var_shape(), so a variable
1231 !! of rank > 1 is indexed as if flattened. The caller should provide @p c_arr_ptr pointing to an array of size
1232 !! @p count. Double precision and integer variables are supported.
1233 !<
1234 function get_value_at_indices(c_var_address, c_arr_ptr, c_inds, count) &
1235 result(bmi_status) bind(C, name="get_value_at_indices")
1236 !DIR$ ATTRIBUTES DLLEXPORT :: get_value_at_indices
1237 ! -- modules
1238 use constantsmodule, only: lenmemtype
1239 ! -- dummy variables
1240 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
1241 type(c_ptr), intent(in) :: c_arr_ptr !< pointer to the array to copy into
1242 integer(kind=c_int), intent(in) :: c_inds(*) !< zero-based indices of the values
1243 integer(kind=c_int), intent(in) :: count !< number of indices
1244 integer(kind=c_int) :: bmi_status !< BMI status code
1245 ! -- local variables
1246 character(len=LENMEMPATH) :: mem_path
1247 character(len=LENMEMTYPE) :: mem_type
1248 character(len=LENVARNAME) :: var_name
1249 logical(LGP) :: valid
1250 integer(I4B) :: i, nvalues
1251 type(c_ptr) :: var_ptr
1252 real(dp), dimension(:), pointer, contiguous :: src_dbl, tgt_dbl
1253 integer(I4B), dimension(:), pointer, contiguous :: src_int, tgt_int
1254
1255 bmi_status = bmi_failure
1256
1257 call split_address(c_var_address, mem_path, var_name, valid)
1258 if (.not. valid) return
1259 if (.not. valid_indices(mem_path, var_name, c_inds, count, nvalues)) return
1260
1261 ! -- the variable's storage, viewed as a flat array in memory order
1262 call get_mem_type(var_name, mem_path, mem_type)
1263 if (index(mem_type, "DOUBLE") /= 0) then
1264 if (get_value_ptr_double(c_var_address, var_ptr) /= bmi_success) return
1265 call c_f_pointer(var_ptr, src_dbl, [nvalues])
1266 call c_f_pointer(c_arr_ptr, tgt_dbl, [count])
1267 do i = 1, count
1268 tgt_dbl(i) = src_dbl(c_inds(i) + 1)
1269 end do
1270 else if (index(mem_type, "INTEGER") /= 0) then
1271 if (get_value_ptr_int(c_var_address, var_ptr) /= bmi_success) return
1272 call c_f_pointer(var_ptr, src_int, [nvalues])
1273 call c_f_pointer(c_arr_ptr, tgt_int, [count])
1274 do i = 1, count
1275 tgt_int(i) = src_int(c_inds(i) + 1)
1276 end do
1277 else
1278 write (bmi_last_error, fmt_unsupported_type) trim(var_name)
1280 return
1281 end if
1282
1283 bmi_status = bmi_success
1284
1285 end function get_value_at_indices
1286
1287 !> @brief Set the values of a variable at the given indices
1288 !!
1289 !! The indices are zero-based positions in the variable's values in C-style
1290 !! (row-major) order of the shape returned by get_var_shape(), as in
1291 !! get_value_at_indices(). The array pointed to by
1292 !! @p c_arr_ptr holds @p count values. Double precision and integer
1293 !! variables are supported. A new hydraulic conductivity does not change
1294 !! the saturated conductance until NPF recalculates it, as it does for TVK.
1295 !<
1296 function set_value_at_indices(c_var_address, c_inds, count, c_arr_ptr) &
1297 result(bmi_status) bind(C, name="set_value_at_indices")
1298 !DIR$ ATTRIBUTES DLLEXPORT :: set_value_at_indices
1299 ! -- modules
1300 use constantsmodule, only: lenmemtype
1302 ! -- dummy variables
1303 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
1304 integer(kind=c_int), intent(in) :: c_inds(*) !< zero-based indices of the values
1305 integer(kind=c_int), intent(in) :: count !< number of indices
1306 type(c_ptr), intent(in) :: c_arr_ptr !< pointer to the array with the new values
1307 integer(kind=c_int) :: bmi_status !< BMI status code
1308 ! -- local variables
1309 character(len=LENMEMPATH) :: mem_path
1310 character(len=LENMEMTYPE) :: mem_type
1311 character(len=LENVARNAME) :: var_name
1312 logical(LGP) :: valid
1313 integer(I4B) :: i, nvalues, status
1314 type(c_ptr) :: var_ptr
1315 real(dp), dimension(:), pointer, contiguous :: src_dbl, tgt_dbl
1316 integer(I4B), dimension(:), pointer, contiguous :: src_int, tgt_int
1317
1318 bmi_status = bmi_failure
1319
1320 call split_address(c_var_address, mem_path, var_name, valid)
1321 if (.not. valid) return
1322 if (.not. valid_indices(mem_path, var_name, c_inds, count, nvalues)) return
1323
1324 ! -- the variable's storage, viewed as a flat array in memory order
1325 call get_mem_type(var_name, mem_path, mem_type)
1326 if (index(mem_type, "DOUBLE") /= 0) then
1327 if (get_value_ptr_double(c_var_address, var_ptr) /= bmi_success) return
1328 call c_f_pointer(var_ptr, tgt_dbl, [nvalues])
1329 call c_f_pointer(c_arr_ptr, src_dbl, [count])
1330 do i = 1, count
1331 tgt_dbl(c_inds(i) + 1) = src_dbl(i)
1332 end do
1333 else if (index(mem_type, "INTEGER") /= 0) then
1334 if (get_value_ptr_int(c_var_address, var_ptr) /= bmi_success) return
1335 call c_f_pointer(var_ptr, tgt_int, [nvalues])
1336 call c_f_pointer(c_arr_ptr, src_int, [count])
1337 do i = 1, count
1338 tgt_int(c_inds(i) + 1) = src_int(i)
1339 end do
1340 else
1341 write (bmi_last_error, fmt_unsupported_type) trim(var_name)
1343 return
1344 end if
1345
1346 ! trigger event:
1347 call on_memory_set(var_name, mem_path, status)
1348 if (status /= 0) then
1349 write (bmi_last_error, fmt_invalid_mem_access) trim(var_name)
1351 return
1352 end if
1353
1354 bmi_status = bmi_success
1355
1356 end function set_value_at_indices
1357
1358 !> @brief Check that every index is within the variable, and count its values
1359 !<
1360 function valid_indices(mem_path, var_name, c_inds, count, nvalues) &
1361 result(valid)
1362 ! -- dummy variables
1363 character(len=*), intent(in) :: mem_path !< memory path of the variable
1364 character(len=*), intent(in) :: var_name !< name of the variable
1365 integer(kind=c_int), intent(in) :: c_inds(*) !< zero-based indices
1366 integer(kind=c_int), intent(in) :: count !< number of indices
1367 integer(I4B), intent(out) :: nvalues !< number of values in the variable
1368 logical(LGP) :: valid !< true when all indices are valid
1369 ! -- local variables
1370 integer(I4B) :: i, rank
1371
1372 valid = .false.
1373 if (count < 0) then
1374 write (bmi_last_error, fmt_invalid_count) count, trim(var_name)
1376 return
1377 end if
1378 rank = -1
1379 call get_mem_rank(var_name, mem_path, rank)
1380 if (rank < 0 .or. rank > 3) then
1381 write (bmi_last_error, fmt_unsupported_rank) trim(var_name)
1383 return
1384 end if
1385
1386 call get_isize(var_name, mem_path, nvalues)
1387 do i = 1, count
1388 if (c_inds(i) < 0 .or. c_inds(i) >= nvalues) then
1389 write (bmi_last_error, fmt_invalid_index) c_inds(i), trim(var_name)
1391 return
1392 end if
1393 end do
1394 valid = .true.
1395
1396 end function valid_indices
1397
1398 !> @brief Get the variable type as a string
1399 !!
1400 !! The type returned is that of a single element.
1401 !! When the variable cannot be found, the string
1402 !< 'UNKNOWN' is assigned.
1403 function get_var_type(c_var_address, c_var_type) result(bmi_status) &
1404 bind(C, name="get_var_type")
1405 !DIR$ ATTRIBUTES DLLEXPORT :: get_var_type
1406 ! -- modules
1407 use constantsmodule, only: lenmemtype
1408 ! -- dummy variables
1409 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
1410 character(kind=c_char), intent(out) :: c_var_type(bmi_lenvartype) !< variable type as a string
1411 integer(kind=c_int) :: bmi_status !< BMI status code
1412 ! -- local variables
1413 character(len=LENMEMPATH) :: mem_path
1414 character(len=LENVARNAME) :: var_name
1415 character(len=LENMEMTYPE) :: mem_type
1416 logical(LGP) :: valid
1417
1418 bmi_status = bmi_success
1419
1420 call split_address(c_var_address, mem_path, var_name, valid)
1421 if (.not. valid) then
1422 bmi_status = bmi_failure
1423 return
1424 end if
1425
1426 call get_mem_type(var_name, mem_path, mem_type)
1427 c_var_type(1:len(trim(mem_type)) + 1) = &
1428 string_to_char_array(trim(mem_type), len(trim(mem_type)))
1429
1430 if (mem_type == 'UNKNOWN') then
1431 write (bmi_last_error, fmt_general_err) 'unknown memory type'
1433 bmi_status = bmi_failure
1434 end if
1435
1436 end function get_var_type
1437
1438 !> @brief Get the units of a variable
1439 !!
1440 !! The memory manager does not store units, so 'unknown' is returned for
1441 !! every variable.
1442 !<
1443 function get_var_units(c_var_address, c_var_units) result(bmi_status) &
1444 bind(C, name="get_var_units")
1445 !DIR$ ATTRIBUTES DLLEXPORT :: get_var_units
1446 ! -- dummy variables
1447 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
1448 character(kind=c_char), intent(out) :: c_var_units(bmi_lenunits) !< variable units as a string
1449 integer(kind=c_int) :: bmi_status !< BMI status code
1450 ! -- local variables
1451 character(len=LENMEMPATH) :: mem_path
1452 character(len=LENVARNAME) :: var_name
1453 character(len=*), parameter :: var_units = 'unknown'
1454 logical(LGP) :: valid
1455
1456 bmi_status = bmi_failure
1457
1458 call split_address(c_var_address, mem_path, var_name, valid)
1459 if (.not. valid) return
1460
1461 c_var_units(1:len(var_units) + 1) = &
1462 string_to_char_array(var_units, len(var_units))
1463 bmi_status = bmi_success
1464
1465 end function get_var_units
1466
1467 !> @brief Get the grid location of a variable
1468 !!
1469 !! Every variable is reported at the grid nodes.
1470 !<
1471 function get_var_location(c_var_address, c_var_location) result(bmi_status) &
1472 bind(C, name="get_var_location")
1473 !DIR$ ATTRIBUTES DLLEXPORT :: get_var_location
1474 ! -- dummy variables
1475 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
1476 character(kind=c_char), intent(out) :: c_var_location(bmi_lenvarlocation) !< variable location as a string
1477 integer(kind=c_int) :: bmi_status !< BMI status code
1478 ! -- local variables
1479 character(len=LENMEMPATH) :: mem_path
1480 character(len=LENVARNAME) :: var_name
1481 character(len=*), parameter :: var_location = 'node'
1482 logical(LGP) :: valid
1483
1484 bmi_status = bmi_failure
1485
1486 call split_address(c_var_address, mem_path, var_name, valid)
1487 if (.not. valid) return
1488
1489 c_var_location(1:len(var_location) + 1) = &
1490 string_to_char_array(var_location, len(var_location))
1491 bmi_status = bmi_success
1492
1493 end function get_var_location
1494
1495 !> @brief Get the variable rank (non-BMI)
1496 !!
1497 !! In order to support multi-dimensional arrays, this function gives
1498 !! access to the rank of the array.
1499 !<
1500 function get_var_rank(c_var_address, c_var_rank) result(bmi_status) &
1501 bind(C, name="get_var_rank")
1502 !DIR$ ATTRIBUTES DLLEXPORT :: get_var_rank
1503 ! -- dummy variables
1504 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
1505 integer(kind=c_int), intent(out) :: c_var_rank !< variable rank
1506 integer(kind=c_int) :: bmi_status !< BMI status code
1507 ! -- local variables
1508 character(len=LENMEMPATH) :: mem_path
1509 character(len=LENVARNAME) :: var_name
1510 logical(LGP) :: valid
1511
1512 bmi_status = bmi_success
1513
1514 call split_address(c_var_address, mem_path, var_name, valid)
1515 if (.not. valid) then
1516 bmi_status = bmi_failure
1517 return
1518 end if
1519
1520 call get_mem_rank(var_name, mem_path, c_var_rank)
1521 if (c_var_rank == -1) then
1522 bmi_status = bmi_failure
1523 return
1524 end if
1525
1526 end function get_var_rank
1527
1528 !> @brief Get the shape of the array for the variable (non-BMI)
1529 !!
1530 !! The shape is an integer array with size equal to the rank of
1531 !! the variable (see get_var_rank()) and values that give the
1532 !! length of the array in each dimension. The target shape array
1533 !! @p c_var_shape should be allocated before calling this routine.
1534 !!
1535 !! Note that the returned shape representation will has been converted
1536 !! to C-style.
1537 !<
1538 function get_var_shape(c_var_address, c_var_shape) result(bmi_status) &
1539 bind(C, name="get_var_shape")
1540 !DIR$ ATTRIBUTES DLLEXPORT :: get_var_shape
1541 ! -- modules
1542 use constantsmodule, only: maxmemrank
1543 ! -- dummy variables
1544 character(kind=c_char), intent(in) :: c_var_address(*) !< memory address string of the variable
1545 integer(c_int), intent(inout) :: c_var_shape(*) !< 1D array with the variable's shape
1546 integer(kind=c_int) :: bmi_status !< BMI status code
1547 ! -- local variables
1548 integer(I4B), dimension(MAXMEMRANK) :: var_shape
1549 integer(I4B) :: var_rank
1550 character(len=LENMEMPATH) :: mem_path
1551 character(len=LENVARNAME) :: var_name
1552 logical(LGP) :: valid
1553
1554 bmi_status = bmi_success
1555
1556 call split_address(c_var_address, mem_path, var_name, valid)
1557 if (.not. valid) then
1558 bmi_status = bmi_failure
1559 return
1560 end if
1561
1562 var_shape = 0
1563 var_rank = 0
1564 call get_mem_rank(var_name, mem_path, var_rank)
1565 call get_mem_shape(var_name, mem_path, var_shape)
1566 if (var_shape(1) == -1 .or. var_rank == -1) then
1567 bmi_status = bmi_failure
1568 return
1569 end if
1570
1571 ! External calls to this BMI are assumed C style, so if the internal shape
1572 ! is (100,1) we get (100,1,undef) from the call get_mem_shape
1573 ! This we need to convert to C-style which should be (1,100).
1574 ! Hence, we reverse the array and drop undef:
1575 c_var_shape(1:var_rank) = var_shape(var_rank:1:-1)
1576
1577 end function get_var_shape
1578
1579end module mf6bmi
This module contains simulation constants.
Definition Constants.f90:9
real(dp), parameter dem10
real constant 1e-10
integer(i4b), parameter lenvarname
maximum length of a variable name
Definition Constants.f90:17
integer(i4b), parameter, public maxmemrank
maximum memory manager length (up to 3-dimensional arrays)
Definition Constants.f90:61
integer(i4b), parameter, public lenmemtype
maximum length of a memory manager type
Definition Constants.f90:62
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
integer(i4b) function, public getunit()
Get a free unit number.
This module defines variable data types.
Definition kind.f90:8
character(len=lenmemaddress) function create_mem_address(mem_path, var_name)
returns the address string of the memory object
subroutine, public get_mem_type(name, mem_path, var_type)
Get the variable memory type.
subroutine, public get_mem_shape(name, mem_path, mem_shape)
Get the variable memory shape.
type(memorystoretype), public memorystore
subroutine, public get_isize(name, mem_path, isize)
Get the number of elements for this variable.
subroutine, public get_mem_rank(name, mem_path, rank)
Get the variable rank.
subroutine, public get_mem_elem_size(name, mem_path, size)
Get the memory size of a single element of the stored variable.
subroutine, public on_memory_set(var_name, mem_path, status)
Triggers the calling of the side effect handler for this variable.
This module contains the MODFLOW 6 BMI.
Definition mf6bmi.f90:18
integer(kind=c_int) function get_end_time(end_time)
Get the end time of the simulation.
Definition mf6bmi.f90:195
integer(kind=c_int) function get_input_var_names(c_names)
Returns all input variables in the simulation.
Definition mf6bmi.f90:331
integer(kind=c_int) function get_input_item_count(count)
Get the number of input variables in the simulation.
Definition mf6bmi.f90:287
logical(lgp) function valid_indices(mem_path, var_name, c_inds, count, nvalues)
Check that every index is within the variable, and count its values.
Definition mf6bmi.f90:1362
integer(kind=c_int) function get_value_ptr_double(c_var_address, c_arr_ptr)
Get a pointer to the array of double precision numbers.
Definition mf6bmi.f90:836
integer(kind=c_int) function bmi_update_until(time)
Perform time steps until the current time reaches time.
Definition mf6bmi.f90:116
integer(kind=c_int) function get_value_ptr_int(c_var_address, c_arr_ptr)
Get a pointer to the array of integer numbers.
Definition mf6bmi.f90:890
integer(kind=c_int) function get_current_time(current_time)
Get the current time of the simulation.
Definition mf6bmi.f90:214
integer(kind=c_int) function get_var_rank(c_var_address, c_var_rank)
Get the variable rank (non-BMI).
Definition mf6bmi.f90:1502
integer(kind=c_int) function get_output_item_count(count)
Get the number of output variables in the simulation.
Definition mf6bmi.f90:304
integer(kind=c_int) function get_value_ptr_bool(c_var_address, c_arr_ptr)
Get a pointer to the logical scalar value.
Definition mf6bmi.f90:943
integer(kind=c_int) function set_value(c_var_address, c_arr_ptr)
Set new values for a given variable.
Definition mf6bmi.f90:985
integer(kind=c_int) function get_time_step(time_step)
Get the time step for the simulation.
Definition mf6bmi.f90:233
integer(kind=c_int) function get_var_nbytes(c_var_address, var_nbytes)
Get size of the variable, in bytes.
Definition mf6bmi.f90:424
integer(kind=c_int) function set_value_double(c_var_address, c_arr_ptr)
Set new values for a variable of type double.
Definition mf6bmi.f90:1031
integer(kind=c_int) function get_start_time(start_time)
Get the start time of the simulation.
Definition mf6bmi.f90:179
integer(kind=c_int) function set_value_int(c_var_address, c_arr_ptr)
Set new values for a variable of type integer.
Definition mf6bmi.f90:1104
integer(kind=c_int) function get_var_itemsize(c_var_address, var_size)
Get the size (in bytes) of a single element of a variable.
Definition mf6bmi.f90:397
integer(kind=c_int) function get_time_units(c_time_units)
Get the time units of the simulation.
Definition mf6bmi.f90:252
integer(kind=c_int) function bmi_get_component_name(name)
Definition mf6bmi.f90:45
integer(kind=c_int) function bmi_update()
Perform a computational time step.
Definition mf6bmi.f90:98
integer(kind=c_int) function get_value_ptr(c_var_address, c_arr_ptr)
Get a pointer to an array.
Definition mf6bmi.f90:789
integer(kind=c_int) function get_var_shape(c_var_address, c_var_shape)
Get the shape of the array for the variable (non-BMI).
Definition mf6bmi.f90:1540
integer(kind=c_int) function bmi_initialize()
Initialize the computational core.
Definition mf6bmi.f90:69
integer(kind=c_int) function get_value_at_indices(c_var_address, c_arr_ptr, c_inds, count)
Copy the values of a variable at the given indices into the array.
Definition mf6bmi.f90:1236
integer(kind=c_int) function set_value_at_indices(c_var_address, c_inds, count, c_arr_ptr)
Set the values of a variable at the given indices.
Definition mf6bmi.f90:1298
integer(kind=c_int) function bmi_finalize()
Clean up the initialized simulation.
Definition mf6bmi.f90:158
integer(kind=c_int) function get_value_int(c_var_address, c_arr_ptr)
Copy the integer values of a variable into the array.
Definition mf6bmi.f90:538
integer(kind=c_int) function get_output_var_names(c_names)
Returns all output variables in the simulation.
Definition mf6bmi.f90:366
integer(kind=c_int) function get_var_type(c_var_address, c_var_type)
Get the variable type as a string.
Definition mf6bmi.f90:1405
integer(kind=c_int) function get_var_units(c_var_address, c_var_units)
Get the units of a variable.
Definition mf6bmi.f90:1445
integer(kind=c_int) function get_value_double(c_var_address, c_arr_ptr)
Copy the double precision values of a variable into the array.
Definition mf6bmi.f90:461
integer(kind=c_int) function set_value_bool(c_var_address, c_arr_ptr)
Set new value for a logical scalar variable.
Definition mf6bmi.f90:1177
integer(kind=c_int) function get_value(c_var_address, c_arr_ptr)
Copy the value of a variable into the array.
Definition mf6bmi.f90:740
integer(kind=c_int) function get_var_location(c_var_address, c_var_location)
Get the grid location of a variable.
Definition mf6bmi.f90:1473
integer(kind=c_int) function get_value_string(c_var_address, c_arr_ptr)
Copy the string(s) of a variable into the array.
Definition mf6bmi.f90:661
integer(kind=c_int) function get_value_bool(c_var_address, c_arr_ptr)
Copy the logical scalar value into the array.
Definition mf6bmi.f90:613
integer(c_int), bind(C, name="ISTDOUTTOFILE") istdout_to_file
output control: =0 to screen, >0 to file
Definition mf6bmi.f90:38
Detailed error information for the BMI.
character(len= *), parameter fmt_general_err
integer, parameter bmi_failure
BMI status code for failure (taken from bmi.f90, CSDMS).
character(len= *), parameter fmt_unsupported_type
character(len= *), parameter fmt_invalid_time
character(len= *), parameter fmt_invalid_count
character(len=lenerrmessage) bmi_last_error
module variable containing the last error as a Fortran string
character(len= *), parameter fmt_invalid_index
subroutine report_bmi_error(err_msg)
Sets the last BMI error message and copies it to an exported C-string.
character(len= *), parameter fmt_invalid_mem_access
character(len= *), parameter fmt_unsupported_rank
integer, parameter bmi_success
BMI status code for success (taken from bmi.f90, CSDMS).
This module contains helper routines and parameters for the MODFLOW 6 BMI.
Definition mf6bmiUtil.f90:4
integer(c_int), bind(C, name="BMI_LENVARTYPE") bmi_lenvartype
max. length for variable type C-strings
integer(c_int), bind(C, name="BMI_LENCOMPONENTNAME") bmi_lencomponentname
component name length, i.e. 'MODFLOW 6'
subroutine split_address(c_var_address, mem_path, var_name, success)
Split the variable address string.
pure character(kind=c_char, len=1) function, dimension(length+1) string_to_char_array(string, length)
Convert Fortran string to C-style character string.
integer(c_int), bind(C, name="BMI_LENUNITS") bmi_lenunits
max. length for units C-strings
integer(c_int), bind(C, name="BMI_LENVARLOCATION") bmi_lenvarlocation
max. length for variable location C-strings
integer(c_int), bind(C, name="BMI_LENVARADDRESS") bmi_lenvaraddress
max. length for the variable's address C-string
Core MODFLOW 6 module.
Definition mf6core.f90:8
logical(lgp) function mf6update()
Run a time step.
Definition mf6core.f90:120
subroutine mf6initialize()
Initialize a simulation.
Definition mf6core.f90:71
subroutine mf6finalize()
Finalize the simulation.
Definition mf6core.f90:146
This module contains simulation variables.
integer(i4b) iforcestop
forced stop flag (1) forces a call to ustop(..) when the simulation has ended, (0) doesn't
character(len=linelength) simstdout
name of standard out file if screen output is piped to a file
integer(i4b) istdout
unit number for stdout
logical(lgp), pointer, public endofsimulation
flag indicating end of simulation
Definition tdis.f90:31
integer(i4b), pointer, public itmuni
flag indicating time units
Definition tdis.f90:25
real(dp), pointer, public totim
time relative to start of simulation
Definition tdis.f90:35
real(dp), pointer, public totalsimtime
time at end of simulation
Definition tdis.f90:40
integer(i4b), pointer, public kstp
current time step number
Definition tdis.f90:27
integer(i4b), pointer, public kper
current stress period number
Definition tdis.f90:26
real(dp), pointer, public delt
length of the current time step
Definition tdis.f90:32
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.