source: LMDZ6/trunk/libf/dyn3dmem/check_isotopes_loc.f90 @ 5451

Last change on this file since 5451 was 5271, checked in by abarral, 2 months ago

Move dimensions.h into a module
Nb: doesn't compile yet

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