Second-Order Adjoint Code by Overloading

The arithmetic on both components of compad_type from the adjoint mode module needs to be overloaded in tangent-linear mode. This includes, in particular, the interpreter. A special module is used for this purpose. Parts of it are shown below.

module compad_module
implicit none

private

public :: compad_type, tls_type
public :: assignment(=), operator(*) ...
public :: sin ...

type tls_type
  double precision :: v=0
  double precision :: d=0
end type tls_type

type compad_type
  type(tls_type) val
  type(tls_type) drv
end type compad_type

interface assignment(=)
  module procedure tls_assign_tls
end interface assignment(=)

interface operator(*)
  module procedure tls_times_tls
end interface operator(*)

interface sin
  module procedure sin_tls
end interface sin

...

contains

elemental subroutine tls_assign_tls(y,x)
  type(tls_type), intent(out) :: y
  type(tls_type), intent(in) :: x
  y%v=x%v; y%d=x%d
end subroutine tls_assign_tls

elemental function tls_times_tls(x1,x2) result(y)
  type(tls_type), intent(in) :: x1,x2
  type(tls_type) :: y
  y%v=x1%v*x2%v; y%d=x1%d*x2%v+x2%d*x1%v
end function tls_times_tls

elemental function sin_tls(x) result(y)
  type(tls_type), intent(in) :: x
  type(tls_type) :: y
  y%v=sin(x%v); y%d=cos(x%v)*x%d
end function sin_tls

...

end module compad_module

-- UweNaumann - 16 Aug 2007

Topic revision: r3 - 2009-04-30 - 06:07:38 - Main.uwe
 
Header STCE
Header RWTH RWTH Informatik
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.