| 1 |
|
| 2 |
module OADactive |
| 3 |
use w2f__types |
| 4 |
implicit none |
| 5 |
private |
| 6 |
public :: active, saxpy, sax, setderiv, zero_deriv, & |
| 7 |
convert_p2a_scalar, convert_a2p_scalar, & |
| 8 |
convert_p2a_vector, convert_a2p_vector, & |
| 9 |
convert_p2a_matrix, convert_a2p_matrix, & |
| 10 |
convert_p2a_three_tensor, convert_a2p_three_tensor, & |
| 11 |
convert_p2a_four_tensor, convert_a2p_four_tensor, & |
| 12 |
convert_p2a_five_tensor, convert_a2p_five_tensor |
| 13 |
|
| 14 |
|
| 15 |
! |
| 16 |
! active needs to be a sequence type |
| 17 |
! with no initialization |
| 18 |
! |
| 19 |
type active |
| 20 |
sequence |
| 21 |
real(w2f__8) :: v |
| 22 |
! initialization does not work for active variables |
| 23 |
! inside of common block, such as in boxmodel |
| 24 |
! initialization is required for correct adjoint |
| 25 |
real(w2f__8) :: d=0.0 |
| 26 |
! real(w2f__8) :: d |
| 27 |
end type active |
| 28 |
|
| 29 |
interface saxpy |
| 30 |
module procedure saxpy_a_a |
| 31 |
end interface |
| 32 |
|
| 33 |
interface setderiv |
| 34 |
module procedure setderiv_a_a |
| 35 |
end interface |
| 36 |
|
| 37 |
interface zero_deriv |
| 38 |
module procedure zero_deriv_a |
| 39 |
end interface |
| 40 |
|
| 41 |
interface sax |
| 42 |
module procedure sax_d_a_a, sax_i_a_a |
| 43 |
end interface |
| 44 |
|
| 45 |
interface convert_p2a_scalar |
| 46 |
module procedure convert_sp2a_scalar_impl |
| 47 |
module procedure convert_p2a_scalar_impl |
| 48 |
end interface |
| 49 |
interface convert_a2p_scalar |
| 50 |
module procedure convert_a2sp_scalar_impl |
| 51 |
module procedure convert_a2p_scalar_impl |
| 52 |
end interface |
| 53 |
|
| 54 |
interface convert_p2a_vector |
| 55 |
module procedure convert_sp2a_vector_impl |
| 56 |
module procedure convert_p2a_vector_impl |
| 57 |
end interface |
| 58 |
interface convert_a2p_vector |
| 59 |
module procedure convert_a2sp_vector_impl |
| 60 |
module procedure convert_a2p_vector_impl |
| 61 |
end interface |
| 62 |
|
| 63 |
interface convert_p2a_matrix |
| 64 |
module procedure convert_sp2a_matrix_impl |
| 65 |
module procedure convert_p2a_matrix_impl |
| 66 |
end interface |
| 67 |
interface convert_a2p_matrix |
| 68 |
module procedure convert_a2sp_matrix_impl |
| 69 |
module procedure convert_a2p_matrix_impl |
| 70 |
end interface |
| 71 |
|
| 72 |
interface convert_p2a_three_tensor |
| 73 |
module procedure convert_sp2a_three_tensor_impl |
| 74 |
module procedure convert_p2a_three_tensor_impl |
| 75 |
end interface |
| 76 |
interface convert_a2p_three_tensor |
| 77 |
module procedure convert_a2sp_three_tensor_impl |
| 78 |
module procedure convert_a2p_three_tensor_impl |
| 79 |
end interface |
| 80 |
|
| 81 |
interface convert_p2a_four_tensor |
| 82 |
module procedure convert_sp2a_four_tensor_impl |
| 83 |
module procedure convert_p2a_four_tensor_impl |
| 84 |
end interface |
| 85 |
interface convert_a2p_four_tensor |
| 86 |
module procedure convert_a2sp_four_tensor_impl |
| 87 |
module procedure convert_a2p_four_tensor_impl |
| 88 |
end interface |
| 89 |
|
| 90 |
interface convert_p2a_five_tensor |
| 91 |
module procedure convert_sp2a_five_tensor_impl |
| 92 |
module procedure convert_p2a_five_tensor_impl |
| 93 |
end interface |
| 94 |
interface convert_a2p_five_tensor |
| 95 |
module procedure convert_a2sp_five_tensor_impl |
| 96 |
module procedure convert_a2p_five_tensor_impl |
| 97 |
end interface |
| 98 |
|
| 99 |
contains |
| 100 |
|
| 101 |
! |
| 102 |
! chain rule saxpy to be used in forward and reverse modes |
| 103 |
! |
| 104 |
|
| 105 |
subroutine saxpy_a_a(a,x,y) |
| 106 |
real(w2f__8), intent(in) :: a |
| 107 |
type(active), intent(in) :: x |
| 108 |
type(active), intent(inout) :: y |
| 109 |
y%d=y%d+x%d*a |
| 110 |
end subroutine saxpy_a_a |
| 111 |
|
| 112 |
! |
| 113 |
! chain rule saxpy to be used in forward and reverse modes |
| 114 |
! derivative component of y is equal to zero initially |
| 115 |
! note: y needs to be inout as otherwise value component gets |
| 116 |
! zeroed out |
| 117 |
! |
| 118 |
|
| 119 |
subroutine sax_d_a_a(a,x,y) |
| 120 |
real(w2f__8), intent(in) :: a |
| 121 |
type(active), intent(in) :: x |
| 122 |
type(active), intent(inout) :: y |
| 123 |
y%d=x%d*a |
| 124 |
end subroutine sax_d_a_a |
| 125 |
|
| 126 |
subroutine sax_i_a_a(a,x,y) |
| 127 |
integer(kind=w2f__i8), intent(in) :: a |
| 128 |
type(active), intent(in) :: x |
| 129 |
type(active), intent(inout) :: y |
| 130 |
y%d=x%d*a |
| 131 |
end subroutine sax_i_a_a |
| 132 |
|
| 133 |
! |
| 134 |
! set derivative of y to be equal to derivative of x |
| 135 |
! note: making y inout allows for already existing active |
| 136 |
! variables to become the target of a derivative assignment |
| 137 |
! |
| 138 |
|
| 139 |
subroutine setderiv_a_a(y,x) |
| 140 |
type(active), intent(inout) :: y |
| 141 |
type(active), intent(in) :: x |
| 142 |
y%d=x%d |
| 143 |
end subroutine setderiv_a_a |
| 144 |
|
| 145 |
! |
| 146 |
! set derivative components to 0.0 |
| 147 |
! |
| 148 |
subroutine zero_deriv_a(x) |
| 149 |
type(active), intent(inout) :: x |
| 150 |
x%d=0.0d0 |
| 151 |
end subroutine zero_deriv_a |
| 152 |
|
| 153 |
! |
| 154 |
! active/passive conversions |
| 155 |
! |
| 156 |
subroutine convert_a2sp_scalar_impl(convertTo, convertFrom) |
| 157 |
real(w2f__4), intent(out) :: convertTo |
| 158 |
type(active), intent(in) :: convertFrom |
| 159 |
convertTo=convertFrom%v |
| 160 |
end subroutine |
| 161 |
|
| 162 |
subroutine convert_a2p_scalar_impl(convertTo, convertFrom) |
| 163 |
real(w2f__8), intent(out) :: convertTo |
| 164 |
type(active), intent(in) :: convertFrom |
| 165 |
convertTo=convertFrom%v |
| 166 |
end subroutine |
| 167 |
|
| 168 |
subroutine convert_sp2a_scalar_impl(convertTo, convertFrom) |
| 169 |
real(w2f__4), intent(in) :: convertFrom |
| 170 |
type(active), intent(inout) :: convertTo |
| 171 |
convertTo%v=convertFrom |
| 172 |
end subroutine |
| 173 |
|
| 174 |
subroutine convert_p2a_scalar_impl(convertTo, convertFrom) |
| 175 |
real(w2f__8), intent(in) :: convertFrom |
| 176 |
type(active), intent(inout) :: convertTo |
| 177 |
convertTo%v=convertFrom |
| 178 |
end subroutine |
| 179 |
|
| 180 |
subroutine convert_a2sp_vector_impl(convertTo, convertFrom) |
| 181 |
type(active), dimension(:), intent(in) :: convertFrom |
| 182 |
real(w2f__4), dimension(:), intent(out) :: convertTo |
| 183 |
convertTo=convertFrom%v |
| 184 |
end subroutine |
| 185 |
|
| 186 |
subroutine convert_a2p_vector_impl(convertTo, convertFrom) |
| 187 |
type(active), dimension(:), intent(in) :: convertFrom |
| 188 |
real(w2f__8), dimension(:), intent(out) :: convertTo |
| 189 |
convertTo=convertFrom%v |
| 190 |
end subroutine |
| 191 |
|
| 192 |
subroutine convert_sp2a_vector_impl(convertTo, convertFrom) |
| 193 |
real(w2f__4), dimension(:), intent(in) :: convertFrom |
| 194 |
type(active), dimension(:), intent(inout) :: convertTo |
| 195 |
convertTo%v=convertFrom |
| 196 |
end subroutine |
| 197 |
|
| 198 |
subroutine convert_p2a_vector_impl(convertTo, convertFrom) |
| 199 |
real(w2f__8), dimension(:), intent(in) :: convertFrom |
| 200 |
type(active), dimension(:), intent(inout) :: convertTo |
| 201 |
convertTo%v=convertFrom |
| 202 |
end subroutine |
| 203 |
|
| 204 |
subroutine convert_a2sp_matrix_impl(convertTo, convertFrom) |
| 205 |
type(active), dimension(:,:), intent(in) :: convertFrom |
| 206 |
real(w2f__4), dimension(:,:), intent(out) :: convertTo |
| 207 |
convertTo=convertFrom%v |
| 208 |
end subroutine |
| 209 |
|
| 210 |
subroutine convert_sp2a_matrix_impl(convertTo, convertFrom) |
| 211 |
real(w2f__4), dimension(:,:), intent(in) :: convertFrom |
| 212 |
type(active), dimension(:,:), intent(inout) :: convertTo |
| 213 |
convertTo%v=convertFrom |
| 214 |
end subroutine |
| 215 |
|
| 216 |
subroutine convert_a2p_matrix_impl(convertTo, convertFrom) |
| 217 |
type(active), dimension(:,:), intent(in) :: convertFrom |
| 218 |
real(w2f__8), dimension(:,:), intent(out) :: convertTo |
| 219 |
convertTo=convertFrom%v |
| 220 |
end subroutine |
| 221 |
|
| 222 |
subroutine convert_p2a_matrix_impl(convertTo, convertFrom) |
| 223 |
real(w2f__8), dimension(:,:), intent(in) :: convertFrom |
| 224 |
type(active), dimension(:,:), intent(inout) :: convertTo |
| 225 |
convertTo%v=convertFrom |
| 226 |
end subroutine |
| 227 |
|
| 228 |
subroutine convert_a2sp_three_tensor_impl(convertTo, convertFrom) |
| 229 |
type(active), dimension(:,:,:), intent(in) :: convertFrom |
| 230 |
real(w2f__4), dimension(:,:,:), intent(out) :: convertTo |
| 231 |
convertTo=convertFrom%v |
| 232 |
end subroutine |
| 233 |
|
| 234 |
subroutine convert_a2p_three_tensor_impl(convertTo, convertFrom) |
| 235 |
type(active), dimension(:,:,:), intent(in) :: convertFrom |
| 236 |
real(w2f__8), dimension(:,:,:), intent(out) :: convertTo |
| 237 |
convertTo=convertFrom%v |
| 238 |
end subroutine |
| 239 |
|
| 240 |
subroutine convert_sp2a_three_tensor_impl(convertTo, convertFrom) |
| 241 |
real(w2f__4), dimension(:,:,:), intent(in) :: convertFrom |
| 242 |
type(active), dimension(:,:,:), intent(inout) :: convertTo |
| 243 |
convertTo%v=convertFrom |
| 244 |
end subroutine |
| 245 |
|
| 246 |
subroutine convert_p2a_three_tensor_impl(convertTo, convertFrom) |
| 247 |
real(w2f__8), dimension(:,:,:), intent(in) :: convertFrom |
| 248 |
type(active), dimension(:,:,:), intent(inout) :: convertTo |
| 249 |
convertTo%v=convertFrom |
| 250 |
end subroutine |
| 251 |
|
| 252 |
subroutine convert_a2sp_four_tensor_impl(convertTo, convertFrom) |
| 253 |
type(active), dimension(:,:,:,:), intent(in) :: convertFrom |
| 254 |
real(w2f__4), dimension(:,:,:,:), intent(out) :: convertTo |
| 255 |
convertTo=convertFrom%v |
| 256 |
end subroutine |
| 257 |
|
| 258 |
subroutine convert_a2p_four_tensor_impl(convertTo, convertFrom) |
| 259 |
type(active), dimension(:,:,:,:), intent(in) :: convertFrom |
| 260 |
real(w2f__8), dimension(:,:,:,:), intent(out) :: convertTo |
| 261 |
convertTo=convertFrom%v |
| 262 |
end subroutine |
| 263 |
|
| 264 |
subroutine convert_sp2a_four_tensor_impl(convertTo, convertFrom) |
| 265 |
real(w2f__4), dimension(:,:,:,:), intent(in) :: convertFrom |
| 266 |
type(active), dimension(:,:,:,:), intent(inout) :: convertTo |
| 267 |
convertTo%v=convertFrom |
| 268 |
end subroutine |
| 269 |
|
| 270 |
subroutine convert_p2a_four_tensor_impl(convertTo, convertFrom) |
| 271 |
real(w2f__8), dimension(:,:,:,:), intent(in) :: convertFrom |
| 272 |
type(active), dimension(:,:,:,:), intent(inout) :: convertTo |
| 273 |
convertTo%v=convertFrom |
| 274 |
end subroutine |
| 275 |
|
| 276 |
subroutine convert_a2sp_five_tensor_impl(convertTo, convertFrom) |
| 277 |
type(active), dimension(:,:,:,:,:), intent(in) :: convertFrom |
| 278 |
real(w2f__4), dimension(:,:,:,:,:), intent(out) :: convertTo |
| 279 |
convertTo=convertFrom%v |
| 280 |
end subroutine |
| 281 |
|
| 282 |
subroutine convert_a2p_five_tensor_impl(convertTo, convertFrom) |
| 283 |
type(active), dimension(:,:,:,:,:), intent(in) :: convertFrom |
| 284 |
real(w2f__8), dimension(:,:,:,:,:), intent(out) :: convertTo |
| 285 |
convertTo=convertFrom%v |
| 286 |
end subroutine |
| 287 |
|
| 288 |
subroutine convert_sp2a_five_tensor_impl(convertTo, convertFrom) |
| 289 |
real(w2f__4), dimension(:,:,:,:,:), intent(in) :: convertFrom |
| 290 |
type(active), dimension(:,:,:,:,:), intent(inout) :: convertTo |
| 291 |
convertTo%v=convertFrom |
| 292 |
end subroutine |
| 293 |
|
| 294 |
subroutine convert_p2a_five_tensor_impl(convertTo, convertFrom) |
| 295 |
real(w2f__8), dimension(:,:,:,:,:), intent(in) :: convertFrom |
| 296 |
type(active), dimension(:,:,:,:,:), intent(inout) :: convertTo |
| 297 |
convertTo%v=convertFrom |
| 298 |
end subroutine |
| 299 |
|
| 300 |
end module |
| 301 |
|
| 302 |
|