bug in tangent linear and adjoint NPZD iron code

Bug reports, work arounds and fixes

Moderators: arango, robertson

Post Reply
Message
Author
jpm
Posts: 21
Joined: Thu Aug 27, 2009 4:37 pm
Location: UCSC

bug in tangent linear and adjoint NPZD iron code

#1 Unread post by jpm »

I am currently working on a project generating tangent linear and adjoint models without the need for tangent linear or adjoint code. When comparing my results with the "reference" results based on the code in the repository, a large discrepancy between the results led me to discover a bug in repository code. In both tl_npzd_iron.h and ad_npzd_iron.h, the wrong nonlinear state is used in a few lines.

In tl_npzd_iron.h, this is affecting line 1680 and following:

Code: Select all

!>            Bio(i,k,iPhyt)=Bio(i,k,iPhyt)/(1.0_r8+cff)
!>
              tl_Bio(i,k,iPhyt)=(tl_Bio(i,k,iPhyt)-                     &
     &                           tl_cff*Bio(i,k,iPhyt))/                &
     &                          (1.0_r8+cff)
!>            Bio(i,k,iZoop)=Bio(i,k,iZoop)+                            &
!>   &                       Bio(i,k,iPhyt)*cff2*cff
!>
              tl_Bio(i,k,iZoop)=tl_Bio(i,k,iZoop)+                      &
     &                          cff2*(tl_Bio(i,k,iPhyt)*cff+            &
     &                                Bio(i,k,iPhyt)*tl_cff)
!>            Bio(i,k,iNO3_)=Bio(i,k,iNO3_)+                            &
!>   &                       Bio(i,k,iPhyt)*ZooEEN(ng)*cff
!>
              tl_Bio(i,k,iNO3_)=tl_Bio(i,k,iNO3_)+                      &
     &                          ZooEEN(ng)*(tl_Bio(i,k,iPhyt)*cff+      &
     &                                      Bio(i,k,iPhyt)*tl_cff)
!>            Bio(i,k,iSDet)=Bio(i,k,iSDet)+                            &
!>   &                       Bio(i,k,iPhyt)*ZooEED(ng)*cff
!>
              tl_Bio(i,k,iSDet)=tl_Bio(i,k,iSDet)+                      &
     &                          ZooEED(ng)*(tl_Bio(i,k,iPhyt)*cff+      &
     &                                      Bio(i,k,iPhyt)*tl_cff)
In all instances, Bio(i,k,iPhyt) is not at the correct value.

FIX:
The fix is straightforward:
(1) save the nonlinear state at the correct instance by inserting a Bio2(i,k,iPhyt)=Bio(i,k,iPhyt) statement just below line 1405:

Code: Select all

                Bio(i,k,iPhyt)=Bio(i,k,iPhyt)/(1.0_r8+cff)
                Bio2(i,k,iPhyt)=Bio(i,k,iPhyt)
(2) use the saved state in the generation of the tangent linear result (4 modifications, line 1680 and following):

Code: Select all

!>            Bio(i,k,iPhyt)=Bio(i,k,iPhyt)/(1.0_r8+cff)
!>
              tl_Bio(i,k,iPhyt)=(tl_Bio(i,k,iPhyt)-                     &
     &                           tl_cff*Bio2(i,k,iPhyt))/               &
     &                          (1.0_r8+cff)
!>            Bio(i,k,iZoop)=Bio(i,k,iZoop)+                            &
!>   &                       Bio(i,k,iPhyt)*cff2*cff
!>
              tl_Bio(i,k,iZoop)=tl_Bio(i,k,iZoop)+                      &
     &                          cff2*(tl_Bio(i,k,iPhyt)*cff+            &
     &                                Bio2(i,k,iPhyt)*tl_cff)
!>            Bio(i,k,iNO3_)=Bio(i,k,iNO3_)+                            &
!>   &                       Bio(i,k,iPhyt)*ZooEEN(ng)*cff
!>
              tl_Bio(i,k,iNO3_)=tl_Bio(i,k,iNO3_)+                      &
     &                          ZooEEN(ng)*(tl_Bio(i,k,iPhyt)*cff+      &
     &                                      Bio2(i,k,iPhyt)*tl_cff)
!>            Bio(i,k,iSDet)=Bio(i,k,iSDet)+                            &
!>   &                       Bio(i,k,iPhyt)*ZooEED(ng)*cff
!>
              tl_Bio(i,k,iSDet)=tl_Bio(i,k,iSDet)+                      &
     &                          ZooEED(ng)*(tl_Bio(i,k,iPhyt)*cff+      &
     &                                      Bio2(i,k,iPhyt)*tl_cff)
(3) make the corresponding changes in the adjoint code:

ad_npzd_iron.h, line 2404:

Code: Select all

                Bio(i,k,iPhyt)=Bio(i,k,iPhyt)/(1.0_r8+cff)
                Bio2(i,k,iPhyt)=Bio(i,k,iPhyt)
ad_npzd_iron.h, line 2766 and following (8 modifications, including comments):

Code: Select all

!>            tl_Bio(i,k,iSDet)=tl_Bio(i,k,iSDet)+                      &
!>   &                          ZooEED(ng)*(tl_Bio(i,k,iPhyt)*cff+      &
!>   &                                      Bio2(i,k,iPhyt)*tl_cff)
!>
              ad_cff=ad_cff+ZooEED(ng)*Bio2(i,k,iPhyt)*ad_Bio(i,k,iSDet)
              ad_Bio(i,k,iPhyt)=ad_Bio(i,k,iPhyt)+                      &
     &                          ZooEED(ng)*cff*ad_Bio(i,k,iSDet)
!>            tl_Bio(i,k,iNO3_)=tl_Bio(i,k,iNO3_)+                      &
!>   &                          ZooEEN(ng)*(tl_Bio(i,k,iPhyt)*cff+      &
!>   &                                      Bio2(i,k,iPhyt)*tl_cff)
!>
              ad_cff=ad_cff+                                            &
     &               ZooEEN(ng)*Bio2(i,k,iPhyt)*ad_Bio(i,k,iNO3_)
              ad_Bio(i,k,iPhyt)=ad_Bio(i,k,iPhyt)+                      &
     &                          ZooEEN(ng)*cff*ad_Bio(i,k,iNO3_)
!>            tl_Bio(i,k,iZoop)=tl_Bio(i,k,iZoop)+                      &
!>   &                          cff2*(tl_Bio(i,k,iPhyt)*cff+            &
!>   &                                Bio2(i,k,iPhyt)*tl_cff)
!>
              ad_cff=ad_cff+                                            &
     &               cff2*Bio2(i,k,iPhyt)*ad_Bio(i,k,iZoop)
              ad_Bio(i,k,iPhyt)=ad_Bio(i,k,iPhyt)+                      &
     &                          cff2*cff*ad_Bio(i,k,iZoop)
!>            tl_Bio(i,k,iPhyt)=(tl_Bio(i,k,iPhyt)-                     &
!>   &                           tl_cff*Bio2(i,k,iPhyt))/               &
!>   &                          (1.0_r8+cff)
!>
              adfac=ad_Bio(i,k,iPhyt)/(1.0_r8+cff)
              ad_cff=ad_cff-Bio2(i,k,iPhyt)*adfac
              ad_Bio(i,k,iPhyt)=adfac
Jann Paul Mattern, Ocean Sciences Department, UCSC

Post Reply