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