| 1 |
|
| 2 |
module active_module |
| 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 |
double precision :: 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 |
double precision :: d=0.0 |
| 26 |
! double precision :: 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_p2a_scalar_impl |
| 47 |
end interface |
| 48 |
interface convert_a2p_scalar |
| 49 |
module procedure convert_a2p_scalar_impl |
| 50 |
end interface |
| 51 |
|
| 52 |
interface convert_p2a_vector |
| 53 |
module procedure convert_sp2a_vector_impl |
| 54 |
end interface |
| 55 |
interface convert_a2p_vector |
| 56 |
module procedure convert_a2sp_vector_impl |
| 57 |
end interface |
| 58 |
|
| 59 |
interface convert_p2a_matrix |
| 60 |
module procedure convert_sp2a_matrix_impl |
| 61 |
end interface |
| 62 |
interface convert_a2p_matrix |
| 63 |
module procedure convert_a2sp_matrix_impl |
| 64 |
end interface |
| 65 |
|
| 66 |
interface convert_p2a_three_tensor |
| 67 |
module procedure convert_sp2a_three_tensor_impl |
| 68 |
end interface |
| 69 |
interface convert_a2p_three_tensor |
| 70 |
module procedure convert_a2sp_three_tensor_impl |
| 71 |
end interface |
| 72 |
|
| 73 |
interface convert_p2a_four_tensor |
| 74 |
module procedure convert_sp2a_four_tensor_impl |
| 75 |
end interface |
| 76 |
interface convert_a2p_four_tensor |
| 77 |
module procedure convert_a2sp_four_tensor_impl |
| 78 |
end interface |
| 79 |
|
| 80 |
interface convert_p2a_five_tensor |
| 81 |
module procedure convert_sp2a_five_tensor_impl |
| 82 |
end interface |
| 83 |
interface convert_a2p_five_tensor |
| 84 |
module procedure convert_a2sp_five_tensor_impl |
| 85 |
end interface |
| 86 |
|
| 87 |
contains |
| 88 |
|
| 89 |
! |
| 90 |
! chain rule saxpy to be used in forward and reverse modes |
| 91 |
! |
| 92 |
|
| 93 |
subroutine saxpy_a_a(a,x,y) |
| 94 |
double precision, intent(in) :: a |
| 95 |
type(active), intent(in) :: x |
| 96 |
type(active), intent(inout) :: y |
| 97 |
|
| 98 |
y%d=y%d+x%d*a |
| 99 |
end subroutine saxpy_a_a |
| 100 |
|
| 101 |
! |
| 102 |
! chain rule saxpy to be used in forward and reverse modes |
| 103 |
! derivative component of y is equal to zero initially |
| 104 |
! note: y needs to be inout as otherwise value component gets |
| 105 |
! zeroed out |
| 106 |
! |
| 107 |
|
| 108 |
subroutine sax_d_a_a(a,x,y) |
| 109 |
double precision, intent(in) :: a |
| 110 |
type(active), intent(in) :: x |
| 111 |
type(active), intent(inout) :: y |
| 112 |
|
| 113 |
y%d=x%d*a |
| 114 |
end subroutine sax_d_a_a |
| 115 |
|
| 116 |
subroutine sax_i_a_a(a,x,y) |
| 117 |
integer(kind=w2f__i8), intent(in) :: a |
| 118 |
type(active), intent(in) :: x |
| 119 |
type(active), intent(inout) :: y |
| 120 |
|
| 121 |
y%d=x%d*a |
| 122 |
end subroutine sax_i_a_a |
| 123 |
|
| 124 |
! |
| 125 |
! set derivative of y to be equal to derivative of x |
| 126 |
! note: making y inout allows for already existing active |
| 127 |
! variables to become the target of a derivative assignment |
| 128 |
! |
| 129 |
|
| 130 |
subroutine setderiv_a_a(y,x) |
| 131 |
type(active), intent(inout) :: y |
| 132 |
type(active), intent(in) :: x |
| 133 |
|
| 134 |
y%d=x%d |
| 135 |
end subroutine setderiv_a_a |
| 136 |
|
| 137 |
! |
| 138 |
! set derivative components to 0.0 |
| 139 |
! |
| 140 |
subroutine zero_deriv_a(x) |
| 141 |
type(active), intent(out) :: x |
| 142 |
|
| 143 |
x%d=0.0d0 |
| 144 |
end subroutine zero_deriv_a |
| 145 |
|
| 146 |
subroutine convert_a2p_scalar_impl(convertTo, convertFrom) |
| 147 |
double precision, intent(out) :: convertTo |
| 148 |
type(active), intent(in) :: convertFrom |
| 149 |
convertTo=convertFrom%v |
| 150 |
end subroutine |
| 151 |
|
| 152 |
subroutine convert_p2a_scalar_impl(convertTo, convertFrom) |
| 153 |
double precision, intent(in) :: convertFrom |
| 154 |
type(active), intent(inout) :: convertTo |
| 155 |
convertTo%v=convertFrom |
| 156 |
end subroutine |
| 157 |
|
| 158 |
subroutine convert_a2sp_vector_impl(convertTo, convertFrom) |
| 159 |
type(active), dimension(:), intent(in) :: convertFrom |
| 160 |
real, dimension(:), intent(out) :: convertTo |
| 161 |
integer i |
| 162 |
do i=lbound(convertFrom,1),ubound(convertFrom,1) |
| 163 |
convertTo(i)=convertFrom(i)%v |
| 164 |
end do |
| 165 |
end subroutine |
| 166 |
|
| 167 |
subroutine convert_sp2a_vector_impl(convertTo, convertFrom) |
| 168 |
real, dimension(:), intent(in) :: convertFrom |
| 169 |
type(active), dimension(:), intent(inout) :: convertTo |
| 170 |
integer i |
| 171 |
do i=lbound(convertFrom,1),ubound(convertFrom,1) |
| 172 |
convertTo(i)%v=convertFrom(i) |
| 173 |
end do |
| 174 |
end subroutine |
| 175 |
|
| 176 |
subroutine convert_a2sp_matrix_impl(convertTo, convertFrom) |
| 177 |
type(active), dimension(:,:), intent(in) :: convertFrom |
| 178 |
real, dimension(:,:), intent(out) :: convertTo |
| 179 |
integer i,j |
| 180 |
do i=lbound(convertFrom,1),ubound(convertFrom,1) |
| 181 |
do j=lbound(convertFrom,2),ubound(convertFrom,2) |
| 182 |
convertTo(i,j)=convertFrom(i,j)%v |
| 183 |
end do |
| 184 |
end do |
| 185 |
end subroutine |
| 186 |
|
| 187 |
subroutine convert_sp2a_matrix_impl(convertTo, convertFrom) |
| 188 |
real, dimension(:,:), intent(in) :: convertFrom |
| 189 |
type(active), dimension(:,:), intent(inout) :: convertTo |
| 190 |
integer i,j |
| 191 |
do i=lbound(convertFrom,1),ubound(convertFrom,1) |
| 192 |
do j=lbound(convertFrom,2),ubound(convertFrom,2) |
| 193 |
convertTo(i,j)%v=convertFrom(i,j) |
| 194 |
end do |
| 195 |
end do |
| 196 |
end subroutine |
| 197 |
|
| 198 |
subroutine convert_a2sp_three_tensor_impl(convertTo, convertFrom) |
| 199 |
type(active), dimension(:,:,:), intent(in) :: convertFrom |
| 200 |
real, dimension(:,:,:), intent(out) :: convertTo |
| 201 |
integer i,j,k |
| 202 |
do i=lbound(convertFrom,1),ubound(convertFrom,1) |
| 203 |
do j=lbound(convertFrom,2),ubound(convertFrom,2) |
| 204 |
do k=lbound(convertFrom,3),ubound(convertFrom,3) |
| 205 |
convertTo(i,j,k)=convertFrom(i,j,k)%v |
| 206 |
end do |
| 207 |
end do |
| 208 |
end do |
| 209 |
end subroutine |
| 210 |
|
| 211 |
subroutine convert_sp2a_three_tensor_impl(convertTo, convertFrom) |
| 212 |
real, dimension(:,:,:), intent(in) :: convertFrom |
| 213 |
type(active), dimension(:,:,:), intent(inout) :: convertTo |
| 214 |
integer i,j,k |
| 215 |
do i=lbound(convertFrom,1),ubound(convertFrom,1) |
| 216 |
do j=lbound(convertFrom,2),ubound(convertFrom,2) |
| 217 |
do k=lbound(convertFrom,3),ubound(convertFrom,3) |
| 218 |
convertTo(i,j,k)%v=convertFrom(i,j,k) |
| 219 |
end do |
| 220 |
end do |
| 221 |
end do |
| 222 |
end subroutine |
| 223 |
|
| 224 |
subroutine convert_a2sp_four_tensor_impl(convertTo, convertFrom) |
| 225 |
type(active), dimension(:,:,:,:), intent(in) :: convertFrom |
| 226 |
real, dimension(:,:,:,:), intent(out) :: convertTo |
| 227 |
integer i,j,k,l |
| 228 |
do i=lbound(convertFrom,1),ubound(convertFrom,1) |
| 229 |
do j=lbound(convertFrom,2),ubound(convertFrom,2) |
| 230 |
do k=lbound(convertFrom,3),ubound(convertFrom,3) |
| 231 |
do l=lbound(convertFrom,4),ubound(convertFrom,4) |
| 232 |
convertTo(i,j,k,l)=convertFrom(i,j,k,l)%v |
| 233 |
end do |
| 234 |
end do |
| 235 |
end do |
| 236 |
end do |
| 237 |
end subroutine |
| 238 |
|
| 239 |
subroutine convert_sp2a_four_tensor_impl(convertTo, convertFrom) |
| 240 |
real, dimension(:,:,:,:), intent(in) :: convertFrom |
| 241 |
type(active), dimension(:,:,:,:), intent(inout) :: convertTo |
| 242 |
integer i,j,k,l |
| 243 |
do i=lbound(convertFrom,1),ubound(convertFrom,1) |
| 244 |
do j=lbound(convertFrom,2),ubound(convertFrom,2) |
| 245 |
do k=lbound(convertFrom,3),ubound(convertFrom,3) |
| 246 |
do l=lbound(convertFrom,4),ubound(convertFrom,4) |
| 247 |
convertTo(i,j,k,l)%v=convertFrom(i,j,k,l) |
| 248 |
end do |
| 249 |
end do |
| 250 |
end do |
| 251 |
end do |
| 252 |
end subroutine |
| 253 |
|
| 254 |
subroutine convert_a2sp_five_tensor_impl(convertTo, convertFrom) |
| 255 |
type(active), dimension(:,:,:,:,:), intent(in) :: convertFrom |
| 256 |
real, dimension(:,:,:,:,:), intent(out) :: convertTo |
| 257 |
integer i,j,k,l,m |
| 258 |
do i=lbound(convertFrom,1),ubound(convertFrom,1) |
| 259 |
do j=lbound(convertFrom,2),ubound(convertFrom,2) |
| 260 |
do k=lbound(convertFrom,3),ubound(convertFrom,3) |
| 261 |
do l=lbound(convertFrom,4),ubound(convertFrom,4) |
| 262 |
do m=lbound(convertFrom,5),ubound(convertFrom,5) |
| 263 |
convertTo(i,j,k,l,m)=convertFrom(i,j,k,l,m)%v |
| 264 |
end do |
| 265 |
end do |
| 266 |
end do |
| 267 |
end do |
| 268 |
end do |
| 269 |
end subroutine |
| 270 |
|
| 271 |
subroutine convert_sp2a_five_tensor_impl(convertTo, convertFrom) |
| 272 |
real, dimension(:,:,:,:,:), intent(in) :: convertFrom |
| 273 |
type(active), dimension(:,:,:,:,:), intent(inout) :: convertTo |
| 274 |
integer i,j,k,l,m |
| 275 |
do i=lbound(convertFrom,1),ubound(convertFrom,1) |
| 276 |
do j=lbound(convertFrom,2),ubound(convertFrom,2) |
| 277 |
do k=lbound(convertFrom,3),ubound(convertFrom,3) |
| 278 |
do l=lbound(convertFrom,4),ubound(convertFrom,4) |
| 279 |
do m=lbound(convertFrom,5),ubound(convertFrom,5) |
| 280 |
convertTo(i,j,k,l,m)%v=convertFrom(i,j,k,l,m) |
| 281 |
end do |
| 282 |
end do |
| 283 |
end do |
| 284 |
end do |
| 285 |
end do |
| 286 |
end subroutine |
| 287 |
|
| 288 |
end module |
| 289 |
|
| 290 |
|