source: LMDZ6/branches/cirrus/libf/dyn3d/check_isotopes.F90 @ 5324

Last change on this file since 5324 was 5203, checked in by Laurent Fairhead, 8 weeks ago

Merge with trunk revision 5202 before reintegration to trunk

File size: 8.2 KB
Line 
1SUBROUTINE check_isotopes_seq(q, ip1jmp1, err_msg)
2   USE strings_mod, ONLY: maxlen, msg, strIdx, strStack, int2str, real2str
3   USE infotrac,    ONLY: nqtot, niso, nphas, isotope, isoCheck, iqIsoPha, isoSelect, &
4                          ntiso, iH2O, nzone, tracers, isoName,  itZonIso, getKey
5#ifdef CPP_IOIPSL
6   USE ioipsl,          ONLY: getin
7#else
8   USE ioipsl_getincom, ONLY: getin
9#endif
10   IMPLICIT NONE
11   include "dimensions.h"
12   REAL,             INTENT(INOUT) :: q(ip1jmp1,llm,nqtot)
13   INTEGER,          INTENT(IN)    :: ip1jmp1
14   CHARACTER(LEN=*), INTENT(IN)    :: err_msg    !--- Error message to display
15   CHARACTER(LEN=maxlen) :: modname, msg1, nm(2)
16   INTEGER :: ixt, ipha, k, i, iq, iiso, izon, ieau, iqeau, iqpar
17   INTEGER, ALLOCATABLE ::   ix(:)
18   REAL,    ALLOCATABLE, SAVE :: tnat(:)
19   REAL    :: xtractot, xiiso, deltaD, q1, q2
20   REAL, PARAMETER :: borne     = 1e19,  &
21                      errmax    = 1e-8,  &       !--- Max. absolute error
22                      errmaxrel = 1e-3,  &       !--- Max. relative error
23                      qmin      = 1e-11, &
24                      deltaDmax =1000.0, &
25                      deltaDmin =-999.0, &
26                      ridicule  = 1e-12
27   INTEGER, SAVE :: iso_eau, iso_O17, iso_O18, iso_HDO, iso_HTO
28   LOGICAL, SAVE :: ltnat1, first=.TRUE.
29
30   modname='check_isotopes'
31   IF(.NOT.isoCheck)    RETURN                   !--- No need to check => finished
32   IF(isoSelect('H2O')) RETURN                   !--- No H2O isotopes group found
33   IF(niso == 0)        RETURN                   !--- No isotopes => finished
34   IF(first) THEN
35      ltnat1 = .TRUE.; CALL getin('tnateq1', ltnat1)
36      ALLOCATE(tnat(niso))
37      iso_eau = strIdx(isoName,'H216O')
38      iso_O17 = strIdx(isoName,'H217O')
39      iso_O18 = strIdx(isoName,'H218O')
40      iso_HDO = strIdx(isoName,'HDO')
41      iso_HTO = strIdx(isoName,'HTO')
42      IF(ltnat1) THEN
43         tnat(:)=1.0
44      ELSE
45         IF(getKey('tnat', tnat)) CALL abort_gcm(modname, 'missing isotopic parameter', 1)
46      END IF
47      first = .FALSE.
48   END IF
49   CALL msg('31: err_msg='//TRIM(err_msg), modname)
50
51   !--- CHECK FOR NaNs (FOR ALL THE ISOTOPES, INCLUDING GEOGRAPHIC TAGGING TRACERS)
52   modname = 'check_isotopes:iso_verif_noNaN'
53   DO ixt = 1, ntiso
54      DO ipha = 1, nphas
55         iq = iqIsoPha(ixt,ipha)
56         DO k = 1, llm
57            DO i = 1, ip1jmp1
58               IF(ABS(q(i,k,iq)) <= borne) CYCLE
59               WRITE(msg1,'(s,"(",i0,",",i0,",",i0,") = ",ES12.4)')TRIM(isoName(ixt)),i,k,iq,q(i,k,iq)
60               CALL msg(msg1, modname)
61               CALL abort_gcm(modname, 'Error with isotopes: '//TRIM(err_msg), 1)
62            END DO
63         END DO
64      END DO
65   END DO
66
67   !--- CHECK CONSERVATION (MAIN ISOTOPE AND PARENT CONCENTRATIONS MUST BE EQUAL)
68   modname = 'check_isotopes:iso_verif_egalite'
69   ixt = iso_eau
70   IF(ixt /= 0) THEN
71      DO ipha = 1, nphas
72         iq = iqIsoPha(ixt,ipha)
73         iqpar = tracers(iq)%iqParent
74         DO k = 1, llm
75            DO i = 1, ip1jmp1
76               q1 = q(i,k,iqpar)
77               q2 = q(i,k,iq)
78!--- IMPROVEMENT in case at least one isotope is not negligible compared to the main isotopic form.
79!    This would be probably required to sum from smallest to highest concentrations ; the corresponding
80!    indices vector can be computed once only (in the initializations stage), using mean concentrations.
81!              q2 = SUM(q(i,k,tracers(iqPar)%iqDesc), DIM=3)
82               IF(ABS(q1-q2) <= errmax .OR. ABS(q1-q2)/MAX(MAX(ABS(q1),ABS(q2)),1e-18) <= errmaxrel) THEN
83                  q(i,k,iq) = q1                 !--- Bidouille pour convergence
84!                 q(i,k,tracers(iqPar)%iqDesc) = q(i,k,tracers(iqPar)%iqDesc) * q1 / q2
85                  CYCLE
86               END IF
87               CALL msg('ixt, iq = '//TRIM(strStack(int2str([ixt,iq]))), modname)
88               msg1 = '('//TRIM(strStack(int2str([i,k])))//')'
89               CALL msg(TRIM(tracers(iqpar)%name)//TRIM(msg1)//' = '//TRIM(real2str(q1)), modname)
90               CALL msg(TRIM(tracers(iq   )%name)//TRIM(msg1)//' = '//TRIM(real2str(q2)), modname)
91               CALL abort_gcm(modname, 'Error with isotopes: '//TRIM(err_msg), 1)
92            END DO
93         END DO
94      END DO
95   END IF
96
97   !--- CHECK DELTA ANOMALIES
98   modname = 'check_isotopes:iso_verif_aberrant'
99   ix = [ iso_HDO  ,   iso_O18 ]
100   nm = ['deltaD  ', 'deltaO18']
101   DO iiso = 1, SIZE(ix)
102      ixt = ix(iiso)
103      IF(ixt  == 0) CYCLE
104      DO ipha = 1, nphas
105         iq = iqIsoPha(ixt,ipha)
106         iqpar = tracers(iq)%iqParent
107         DO k = 1, llm
108            DO i = 1, ip1jmp1
109               q1 = q(i,k,iqpar)
110               q2 = q(i,k,iq)
111!--- IMPROVEMENT in case at least one isotope is not negligible compared to the main isotopic form.
112!    This would be probably required to sum from smallest to highest concentrations ; the corresponding
113!    indices vector can be computed once only (in the initializations stage), using mean concentrations.
114!              q2 = SUM(q(i,k,tracers(iqPar)%iqDesc), DIM=3)
115               IF(q2 <= qmin) CYCLE
116               deltaD = (q2/q1/tnat(ixt)-1.)*1000.
117               IF(deltaD <= deltaDmax .AND. deltaD >= deltaDmin) CYCLE
118               CALL msg('ixt, iq = '//TRIM(strStack(int2str([ixt,iq]))), modname)
119               msg1 = '('//TRIM(strStack(int2str([i,k])))//')'
120               CALL msg(TRIM(tracers(iqpar)%name)//TRIM(msg1)//' = '//TRIM(real2str(q1)), modname)
121               CALL msg(TRIM(tracers(iq   )%name)//TRIM(msg1)//' = '//TRIM(real2str(q2)), modname)
122               CALL msg(TRIM(nm(iiso))//TRIM(real2str(deltaD)), modname)
123               CALL abort_gcm(modname, 'Error with isotopes: '//TRIM(err_msg), 1)
124            END DO
125         END DO
126      END DO
127   END DO
128
129   IF(nzone == 0) RETURN
130
131   !--- CHECK FOR TAGGING TRACERS DELTAD ANOMALIES
132   modname = 'check_isotopes:iso_verif_aberrant'
133   IF(iso_eau /= 0 .AND. iso_HDO /= 0) THEN
134      DO izon = 1, nzone
135         ixt  = itZonIso(izon, iso_HDO)
136         ieau = itZonIso(izon, iso_eau)
137         DO ipha = 1, nphas
138            iq    = iqIsoPha(ixt,  ipha)
139            iqeau = iqIsoPha(ieau, ipha)
140            DO k = 1, llm
141               DO i = 1, ip1jmp1
142                  q1 = q(i,k,iqeau)
143                  q2 = q(i,k,iq)
144                  IF(q2<=qmin) CYCLE
145                  deltaD = (q2/q1/tnat(iso_HDO)-1.)*1000.
146                  IF(deltaD <= deltaDmax .AND. deltaD >= deltaDmin) CYCLE
147                  CALL msg('izon, ipha = '//TRIM(strStack(int2str([izon, ipha]))), modname)
148                  CALL msg( 'ixt, ieau = '//TRIM(strStack(int2str([ ixt, ieau]))), modname)
149                  msg1 = '('//TRIM(strStack(int2str([i,k])))//')'
150                  CALL msg(TRIM(tracers(iqeau)%name)//TRIM(msg1)//' = '//TRIM(real2str(q1)), modname)
151                  CALL msg(TRIM(tracers(iq   )%name)//TRIM(msg1)//' = '//TRIM(real2str(q2)), modname)
152                  CALL msg('deltaD = '//TRIM(real2str(deltaD)), modname)
153                  CALL abort_gcm(modname, 'Error with isotopes: '//TRIM(err_msg), 1)
154               END DO
155            END DO
156         END DO
157      END DO
158   END IF
159
160   !--- CHECK FOR TAGGING TRACERS CONSERVATION (PARENT AND TAGGING TRACERS SUM OVER ALL REGIONS MUST BE EQUAL)
161   DO iiso = 1, niso
162      DO ipha = 1, nphas
163         iq = iqIsoPha(iiso, ipha)
164         DO k = 1, llm
165            DO i = 1, ip1jmp1
166               xiiso = q(i,k,iq)
167               xtractot = SUM(q(i, k, iqIsoPha(itZonIso(1:nzone,iiso), ipha)))
168               IF(ABS(xtractot-xiiso) > errmax .AND. ABS(xtractot-xiiso)/MAX(MAX(ABS(xtractot),ABS(xiiso)),1e-18) > errmaxrel) THEN
169                  CALL msg('Error in iso_verif_aberrant trac: '//TRIM(err_msg))
170                  CALL msg('iiso, ipha = '//TRIM(strStack(int2str([iiso, ipha]))), modname)
171                  CALL msg('q('//TRIM(strStack(int2str([i,k])))//',:) = '//TRIM(strStack(real2str(q(i,k,:)))), modname)
172                  CALL abort_gcm(modname, 'Error with isotopes: '//TRIM(err_msg), 1)
173               END IF
174               IF(ABS(xtractot) <= ridicule) CYCLE
175               DO izon = 1, nzone
176                  q(i,k,iq) = q(i,k,iq) / xtractot * xiiso !--- Bidouille pour convergence
177               END DO
178            END DO
179         END DO
180      END DO
181   END DO
182
183END SUBROUTINE check_isotopes_seq
184
Note: See TracBrowser for help on using the repository browser.