MOM6
coupler_util.F90
Go to the documentation of this file.
1 module coupler_util
2 !***********************************************************************
3 !* GNU General Public License *
4 !* This file is a part of MOM. *
5 !* *
6 !* MOM is free software; you can redistribute it and/or modify it and *
7 !* are expected to follow the terms of the GNU General Public License *
8 !* as published by the Free Software Foundation; either version 2 of *
9 !* the License, or (at your option) any later version. *
10 !* *
11 !* MOM is distributed in the hope that it will be useful, but WITHOUT *
12 !* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
13 !* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public *
14 !* License for more details. *
15 !* *
16 !* For the full text of the GNU General Public License, *
17 !* write to: Free Software Foundation, Inc., *
18 !* 675 Mass Ave, Cambridge, MA 02139, USA. *
19 !* or see: http://www.gnu.org/licenses/gpl.html *
20 !***********************************************************************
21 
22 ! This code provides a couple of interfaces to allow more transparent and
23 ! robust extraction of the various fields in the coupler types.
24 use mom_error_handler, only : mom_error, fatal, warning
26 use coupler_types_mod, only : ind_csurf
27 
28 implicit none ; private
29 
31 public :: ind_flux, ind_alpha, ind_csurf
32 
33 contains
34 
35 subroutine extract_coupler_values(BC_struc, BC_index, BC_element, array_out, &
36  is, ie, js, je, conversion)
37  type(coupler_2d_bc_type), intent(in) :: BC_struc
38  integer, intent(in) :: BC_index, BC_element
39  real, dimension(:,:), intent(out) :: array_out
40  integer, optional, intent(in) :: is, ie, js, je
41  real, optional, intent(in) :: conversion
42 ! Arguments: BC_struc - The type from which the data is being extracted.
43 ! (in) BC_index - The boundary condition number being extracted.
44 ! (in) BC_element - The element of the boundary condition being extracted.
45 ! This could be ind_csurf, ind_alpha, ind_flux or ind_deposition.
46 ! (out) array_out - The array being filled with the input values.
47 ! (in, opt) is, ie, js, je - The i- and j- limits of array_out to be filled.
48 ! These must match the size of the corresponding value array or an
49 ! error message is issued.
50 ! (in, opt) conversion - A number that every element is multiplied by, to
51 ! permit sign convention or unit conversion.
52 
53  real, pointer, dimension(:,:) :: Array_in
54  real :: conv
55  integer :: i, j, is0, ie0, js0, je0, i_offset, j_offset
56 
57  if ((bc_element /= ind_flux) .and. (bc_element /= ind_alpha) .and. &
58  (bc_element /= ind_csurf)) then
59  call mom_error(fatal,"extract_coupler_values: Unrecognized BC_element.")
60  endif
61 
62  ! These error messages should be made more explicit.
63 ! if (.not.associated(BC_struc%bc(BC_index))) &
64  if (.not.associated(bc_struc%bc)) &
65  call mom_error(fatal,"extract_coupler_values: " // &
66  "The requested boundary condition is not associated.")
67 ! if (.not.associated(BC_struc%bc(BC_index)%field(BC_element))) &
68  if (.not.associated(bc_struc%bc(bc_index)%field)) &
69  call mom_error(fatal,"extract_coupler_values: " // &
70  "The requested boundary condition element is not associated.")
71  if (.not.associated(bc_struc%bc(bc_index)%field(bc_element)%values)) &
72  call mom_error(fatal,"extract_coupler_values: " // &
73  "The requested boundary condition value array is not associated.")
74 
75  array_in => bc_struc%bc(bc_index)%field(bc_element)%values
76 
77  if (present(is)) then ; is0 = is ; else ; is0 = lbound(array_out,1) ; endif
78  if (present(ie)) then ; ie0 = ie ; else ; ie0 = ubound(array_out,1) ; endif
79  if (present(js)) then ; js0 = js ; else ; js0 = lbound(array_out,2) ; endif
80  if (present(je)) then ; je0 = je ; else ; je0 = ubound(array_out,2) ; endif
81 
82  conv = 1.0 ; if (present(conversion)) conv = conversion
83 
84  if (size(array_in,1) /= ie0 - is0 + 1) &
85  call mom_error(fatal,"extract_coupler_values: Mismatch in i-size " // &
86  "between BC array and output array or computational domain.")
87  if (size(array_in,2) /= je0 - js0 + 1) &
88  call mom_error(fatal,"extract_coupler_values: Mismatch in i-size " // &
89  "between BC array and output array or computational domain.")
90  i_offset = lbound(array_in,1) - is0
91  j_offset = lbound(array_in,2) - js0
92  do j=js0,je0 ; do i=is0,ie0
93  array_out(i,j) = conv * array_in(i+i_offset,j+j_offset)
94  enddo ; enddo
95 
96 end subroutine extract_coupler_values
97 
98 subroutine set_coupler_values(array_in, BC_struc, BC_index, BC_element, &
99  is, ie, js, je, conversion)
100  real, dimension(:,:), intent(in) :: array_in
101  type(coupler_2d_bc_type), intent(inout) :: BC_struc
102  integer, intent(in) :: BC_index, BC_element
103  integer, optional, intent(in) :: is, ie, js, je
104  real, optional, intent(in) :: conversion
105 ! Arguments: array_in - The array containing the values to load into the BC.
106 ! (out) BC_struc - The type into which the data is being loaded.
107 ! (in) BC_index - The boundary condition number being extracted.
108 ! (in) BC_element - The element of the boundary condition being extracted.
109 ! This could be ind_csurf, ind_alpha, ind_flux or ind_deposition.
110 ! (in, opt) is, ie, js, je - The i- and j- limits of array_out to be filled.
111 ! These must match the size of the corresponding value array or an
112 ! error message is issued.
113 ! (in, opt) conversion - A number that every element is multiplied by, to
114 ! permit sign convention or unit conversion.
115 
116  real, pointer, dimension(:,:) :: Array_out
117  real :: conv
118  integer :: i, j, is0, ie0, js0, je0, i_offset, j_offset
119 
120  if ((bc_element /= ind_flux) .and. (bc_element /= ind_alpha) .and. &
121  (bc_element /= ind_csurf)) then
122  call mom_error(fatal,"extract_coupler_values: Unrecognized BC_element.")
123  endif
124 
125  ! These error messages should be made more explicit.
126 ! if (.not.associated(BC_struc%bc(BC_index))) &
127  if (.not.associated(bc_struc%bc)) &
128  call mom_error(fatal,"set_coupler_values: " // &
129  "The requested boundary condition is not associated.")
130 ! if (.not.associated(BC_struc%bc(BC_index)%field(BC_element))) &
131  if (.not.associated(bc_struc%bc(bc_index)%field)) &
132  call mom_error(fatal,"set_coupler_values: " // &
133  "The requested boundary condition element is not associated.")
134  if (.not.associated(bc_struc%bc(bc_index)%field(bc_element)%values)) &
135  call mom_error(fatal,"set_coupler_values: " // &
136  "The requested boundary condition value array is not associated.")
137 
138  array_out => bc_struc%bc(bc_index)%field(bc_element)%values
139 
140  if (present(is)) then ; is0 = is ; else ; is0 = lbound(array_in,1) ; endif
141  if (present(ie)) then ; ie0 = ie ; else ; ie0 = ubound(array_in,1) ; endif
142  if (present(js)) then ; js0 = js ; else ; js0 = lbound(array_in,2) ; endif
143  if (present(je)) then ; je0 = je ; else ; je0 = ubound(array_in,2) ; endif
144 
145  conv = 1.0 ; if (present(conversion)) conv = conversion
146 
147  if (size(array_out,1) /= ie0 - is0 + 1) &
148  call mom_error(fatal,"extract_coupler_values: Mismatch in i-size " // &
149  "between BC array and input array or computational domain.")
150  if (size(array_out,2) /= je0 - js0 + 1) &
151  call mom_error(fatal,"extract_coupler_values: Mismatch in i-size " // &
152  "between BC array and input array or computational domain.")
153  i_offset = lbound(array_out,1) - is0
154  j_offset = lbound(array_out,2) - js0
155  do j=js0,je0 ; do i=is0,ie0
156  array_out(i+i_offset,j+j_offset) = conv * array_in(i,j)
157  enddo ; enddo
158 
159 end subroutine set_coupler_values
160 
161 end module coupler_util
integer, public ind_csurf
subroutine, public extract_coupler_values(BC_struc, BC_index, BC_element, array_out, is, ie, js, je, conversion)
integer, public ind_alpha
subroutine, public set_coupler_values(array_in, BC_struc, BC_index, BC_element, is, ie, js, je, conversion)
subroutine, public mom_error(level, message, all_print)
integer, public ind_flux