The simple tangent linear model

From Interactive System for Ice sheet Simulation
Jump to: navigation, search

The driver routine for running the tangent linear model

program driver
 
real(8), dimension(2) :: xvec, g_xvec
real(8) :: fc, g_fc
real(8) :: alpha, beta
 
! initialize	
        fc = 0.
        xvec(1)    = 10.
	xvec(2)    = 30.
        alpha = 5.
        beta = 2.
 
	print *, '-------------------------'
	print *, 'directional derivative x1'
	g_fc  = 0.
	g_xvec(1)  = 1.
	g_xvec(2)  = 0.
 
! call tangent linear model
	call g_simple(alpha,beta,xvec,g_xvec,fc,g_fc)
 
! write output
	print *, 'alpha, beta = ', alpha, beta
	print *, 'x1, x2 = ', xvec(1), xvec(2)
	print *, 'fc, g_fc = ', fc, g_fc
	print *, 'g_x1, g_x2 = ', g_xvec(1), g_xvec(2)
 
	print *, '-------------------------'
	print *, 'directional derivative x2'
	g_fc=0.
	g_xvec(1) = 0.
	g_xvec(2) = 1.
 
! call tangent linear model
	call g_simple(alpha,beta,xvec,g_xvec,fc,g_fc)
 
! write output
	print *, 'alpha, beta = ', alpha, beta
	print *, 'x1, x2 = ', xvec(1), xvec(2)
	print *, 'fc, g_fc = ', fc, g_fc
	print *, 'g_x1, g_x2 = ', g_xvec(1), g_xvec(2)
 
	end

Basic command to invoke AD tool in forward mode

Invoking the AD tool (here TAF) needs to specify

  • what are the independent / control variables (here \mathbf{x} = \texttt{xvec})
  • what are the dependent variables or cost function (here J_0 = \texttt{fc})
  • what is the top-level routines where the actual model starts (different from program driver
  • list of routines to be differentiated
taf     -forward \
	-toplevel simple \
	-input 'xvec' \
	-output 'fc' \
	simple.f90

The tangent linear code, generated via the AD tool TAF

!                           DISCLAIMER
!
!   This file was generated by TAF version 1.9.54
!
module     g_simple_variables
!******************************************************************
!******************************************************************
!** This routine was generated by Automatic differentiation.     **
!** FastOpt: Transformation of Algorithm in Fortran, TAF 1.9.54  **
!******************************************************************
!******************************************************************
!==============================================
! referencing used modules
!==============================================
use simple_variables
 
!==============================================
! all entries are defined explicitly
!==============================================
implicit none
 
!==============================================
! declare local variables
!==============================================
real(kind=8) g_yvec(2)
 
end module     g_simple_variables
 
 
module     g_simple_functions
!******************************************************************
!******************************************************************
!** This routine was generated by Automatic differentiation.     **
!** FastOpt: Transformation of Algorithm in Fortran, TAF 1.9.54  **
!******************************************************************
!******************************************************************
!==============================================
! referencing used modules
!==============================================
use simple_functions
 
!==============================================
! all entries are defined explicitly
!==============================================
implicit none
 
contains
subroutine g_hcostfunction( costfunction, g_costfunction, yvec, g_yvec )
!******************************************************************
!******************************************************************
!** This routine was generated by Automatic differentiation.     **
!** FastOpt: Transformation of Algorithm in Fortran, TAF 1.9.54  **
!******************************************************************
!******************************************************************
!==============================================
! all entries are defined explicitly
!==============================================
implicit none
 
!==============================================
! declare parameters
!==============================================
real(kind=8) d1
parameter ( d1 = 55. )
real(kind=8) d2
parameter ( d2 = 65. )
real(kind=8) var1
parameter ( var1 = 1 )
 
!==============================================
! declare arguments
!==============================================
real(kind=8), intent(out) :: costfunction
real(kind=8), intent(out) :: g_costfunction
real(kind=8) g_yvec(2)
real(kind=8) yvec(2)
 
!----------------------------------------------
! TANGENT LINEAR AND FUNCTION STATEMENTS
!----------------------------------------------
g_costfunction = 2*g_yvec(2)*1.d0/var1*(yvec(2)-d2)+2*g_yvec(1)*1.d0/var1*(yvec(1)-d1)
costfunction = 1.d0/var1*(yvec(1)-d1)**2+1.d0/var1*(yvec(2)-d2)**2
 
end subroutine g_hcostfunction
 
 
end module     g_simple_functions
 
 
subroutine g_simple( alpha, beta, xvec, g_xvec, fc, g_fc )
!******************************************************************
!******************************************************************
!** This routine was generated by Automatic differentiation.     **
!** FastOpt: Transformation of Algorithm in Fortran, TAF 1.9.54  **
!******************************************************************
!******************************************************************
!==============================================
! referencing used modules
!==============================================
use simple_variables
use g_simple_variables
use simple_functions
use g_simple_functions
 
!==============================================
! all entries are defined explicitly
!==============================================
implicit none
 
!==============================================
! declare arguments
!==============================================
real(kind=8), intent(in) :: alpha
real(kind=8), intent(in) :: beta
real(kind=8), intent(out) :: fc
real(kind=8), intent(out) :: g_fc
real(kind=8), intent(in) :: g_xvec(2)
real(kind=8), intent(in) :: xvec(2)
 
!----------------------------------------------
! TANGENT LINEAR AND FUNCTION STATEMENTS
!----------------------------------------------
g_yvec(1) = g_xvec(1)*alpha
yvec(1) = alpha*xvec(1)
g_yvec(2) = -(g_xvec(2)*beta)
yvec(2) = -(beta*xvec(2))
call g_hcostfunction( fc,g_fc,yvec,g_yvec )
 
end subroutine g_simple