From ea7668f515f3c4026c72f06be8ff569710e02d72 Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Thu, 30 May 2024 14:01:30 +0200
Subject: [PATCH] Philippe 30/05/2024: fix: IO_BOX_PREPARE: set correct
 dimensions in case of grid-nesting

---
 .../SURCOUCHE/src/mode_io_manage_struct.f90   | 80 ++++++++++---------
 1 file changed, 42 insertions(+), 38 deletions(-)

diff --git a/src/LIB/SURCOUCHE/src/mode_io_manage_struct.f90 b/src/LIB/SURCOUCHE/src/mode_io_manage_struct.f90
index 32f51fe5f..0cde722be 100644
--- a/src/LIB/SURCOUCHE/src/mode_io_manage_struct.f90
+++ b/src/LIB/SURCOUCHE/src/mode_io_manage_struct.f90
@@ -499,7 +499,7 @@ DO IMI = 1, NMODEL
     CALL IO_OUT_FIELDLIST_FILL( COUT_VAR(IMI,:), .TRUE., OUT_MODEL(IMI)%NOUT_FIELDLIST )
 
     ! Treat the boxes (sub-domains) for output files
-    CALL IO_BOX_PREPARE( IMI )
+    CALL IO_BOX_PREPARE( )
   END IF
   !
   IF ( IP == 1 ) THEN
@@ -788,15 +788,13 @@ SUBROUTINE IO_OUT_FIELDLIST_FILL( HVARLIST, OMAINBOX, KFIELDLIST )
 END SUBROUTINE IO_OUT_FIELDLIST_FILL
 
 !#########################################################################
-SUBROUTINE IO_BOX_PREPARE( KMI )
+SUBROUTINE IO_BOX_PREPARE( )
 !#########################################################################
 
-  USE MODD_DIM_n, ONLY: NIMAX_ll, NJMAX_ll, NKMAX
-  USE MODD_DYN_n, ONLY: NALBAS, NALBOT
+  USE MODD_DIM_n, ONLY: DIM_MODEL
+  USE MODD_DYN_n, ONLY: DYN_MODEL
   USE MODD_OUT_n, ONLY: CMAINDOMAINNAME
 
-  INTEGER, INTENT(IN) :: KMI
-
   INTEGER                            :: IFIELD
   INTEGER                            :: IVAR
   INTEGER                            :: JIDX1, JIDX2
@@ -845,32 +843,32 @@ SUBROUTINE IO_BOX_PREPARE( KMI )
 
   IF ( OUT_MODEL(IMI)%LOUT_HOR_BORDER_REMOVE ) THEN
     TOUT_BOXES(0)%NIINF = 1
-    TOUT_BOXES(0)%NISUP = NIMAX_ll
+    TOUT_BOXES(0)%NISUP = DIM_MODEL(IMI)%NIMAX_ll
     TOUT_BOXES(0)%NJINF = 1
-    TOUT_BOXES(0)%NJSUP = NJMAX_ll
+    TOUT_BOXES(0)%NJSUP = DIM_MODEL(IMI)%NJMAX_ll
   ELSE
     ! Set boundaries in physical domain coordinates, but must cover all the domain with non-physical values
     TOUT_BOXES(0)%NIINF = 1 - JPHEXT
-    TOUT_BOXES(0)%NISUP = NIMAX_ll + JPHEXT
+    TOUT_BOXES(0)%NISUP = DIM_MODEL(IMI)%NIMAX_ll + JPHEXT
     TOUT_BOXES(0)%NJINF = 1 - JPHEXT
-    TOUT_BOXES(0)%NJSUP = NJMAX_ll + JPHEXT
+    TOUT_BOXES(0)%NJSUP = DIM_MODEL(IMI)%NJMAX_ll + JPHEXT
   END IF
   IF ( OUT_MODEL(IMI)%LOUT_VER_BORDER_REMOVE ) THEN
     TOUT_BOXES(0)%NKINF = 1
-    TOUT_BOXES(0)%NKSUP = NKMAX
+    TOUT_BOXES(0)%NKSUP = DIM_MODEL(IMI)%NKMAX
   ELSE
     TOUT_BOXES(0)%NKINF = 1 - JPVEXT
-    TOUT_BOXES(0)%NKSUP = NKMAX + JPVEXT
+    TOUT_BOXES(0)%NKSUP = DIM_MODEL(IMI)%NKMAX + JPVEXT
   END IF
 
   IF ( OUT_MODEL(IMI)%LOUT_BAL_REMOVE ) THEN
     ! Max to manage case when the BAL is in the unphysical domain (should not happen)
-    TOUT_BOXES(0)%NKINF = MAX( TOUT_BOXES(0)%NKINF, NALBAS - JPVEXT )
+    TOUT_BOXES(0)%NKINF = MAX( TOUT_BOXES(0)%NKINF, DYN_MODEL(IMI)%NALBAS - JPVEXT )
   END IF
 
   IF ( OUT_MODEL(IMI)%LOUT_TAL_REMOVE ) THEN
     ! Min to manage case when the TAL is in the unphysical domain (should not happen)
-    TOUT_BOXES(0)%NKSUP = MIN( TOUT_BOXES(0)%NKSUP, NALBOT - JPVEXT )
+    TOUT_BOXES(0)%NKSUP = MIN( TOUT_BOXES(0)%NKSUP, DYN_MODEL(IMI)%NALBOT - JPVEXT )
   END IF
 
   ! Treat boxes
@@ -904,45 +902,51 @@ SUBROUTINE IO_BOX_PREPARE( KMI )
 
     !Set default values to physical domain boundaries
     IF ( TOUT_BOXES(JI)%NIINF == NNEGUNDEF ) TOUT_BOXES(JI)%NIINF = 1
-    IF ( TOUT_BOXES(JI)%NISUP == NNEGUNDEF ) TOUT_BOXES(JI)%NISUP = NIMAX_ll
+    IF ( TOUT_BOXES(JI)%NISUP == NNEGUNDEF ) TOUT_BOXES(JI)%NISUP = DIM_MODEL(IMI)%NIMAX_ll
     IF ( TOUT_BOXES(JI)%NJINF == NNEGUNDEF ) TOUT_BOXES(JI)%NJINF = 1
-    IF ( TOUT_BOXES(JI)%NJSUP == NNEGUNDEF ) TOUT_BOXES(JI)%NJSUP = NJMAX_ll
+    IF ( TOUT_BOXES(JI)%NJSUP == NNEGUNDEF ) TOUT_BOXES(JI)%NJSUP = DIM_MODEL(IMI)%NJMAX_ll
     IF ( TOUT_BOXES(JI)%NKINF == NNEGUNDEF ) TOUT_BOXES(JI)%NKINF = 1
-    IF ( TOUT_BOXES(JI)%NKSUP == NNEGUNDEF ) TOUT_BOXES(JI)%NKSUP = NKMAX
+    IF ( TOUT_BOXES(JI)%NKSUP == NNEGUNDEF ) TOUT_BOXES(JI)%NKSUP = DIM_MODEL(IMI)%NKMAX
 
     IF ( OUT_MODEL(IMI)%LOUT_BAL_REMOVE ) THEN
-      IF ( GKINF_PROVIDED .AND. TOUT_BOXES(JI)%NKINF < ( NALBAS - JPVEXT ) ) &
+      IF ( GKINF_PROVIDED .AND. TOUT_BOXES(JI)%NKINF < ( DYN_MODEL(IMI)%NALBAS - JPVEXT ) ) &
         CALL Print_msg( NVERB_WARNING, 'GEN', 'IO_BOX_PREPARE', 'provided NKINF increazed after removal of Bottom Absorbing Layer' )
-      TOUT_BOXES(JI)%NKINF = MAX( TOUT_BOXES(JI)%NKINF, NALBAS - JPVEXT )
+      TOUT_BOXES(JI)%NKINF = MAX( TOUT_BOXES(JI)%NKINF, DYN_MODEL(IMI)%NALBAS - JPVEXT )
     END IF
 
     IF ( OUT_MODEL(IMI)%LOUT_TAL_REMOVE ) THEN
-      IF ( GKSUP_PROVIDED .AND. TOUT_BOXES(JI)%NKSUP > ( NALBOT - JPVEXT ) ) &
+      IF ( GKSUP_PROVIDED .AND. TOUT_BOXES(JI)%NKSUP > ( DYN_MODEL(IMI)%NALBOT - JPVEXT ) ) &
         CALL Print_msg( NVERB_WARNING, 'GEN', 'IO_BOX_PREPARE', 'provided NKSUP reduced after removal of Top Absorbing Layer' )
-      TOUT_BOXES(JI)%NKSUP = MIN( TOUT_BOXES(JI)%NKSUP, NALBOT - JPVEXT )
+      TOUT_BOXES(JI)%NKSUP = MIN( TOUT_BOXES(JI)%NKSUP, DYN_MODEL(IMI)%NALBOT - JPVEXT )
     END IF
 
     !Check that selected indices are in physical domain
-    IF ( TOUT_BOXES(JI)%NIINF < 1 )        CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_IINF too small (<1)' )
-    IF ( TOUT_BOXES(JI)%NIINF > NIMAX_ll ) CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_IINF too large (>NIMAX)')
-    IF ( TOUT_BOXES(JI)%NISUP < 1 )        CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_ISUP too small (<1)' )
-    IF ( TOUT_BOXES(JI)%NISUP > NIMAX_ll ) CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_ISUP too large (>NIMAX)')
+    IF ( TOUT_BOXES(JI)%NIINF < 1 ) CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_IINF too small (<1)' )
+    IF ( TOUT_BOXES(JI)%NIINF > DIM_MODEL(IMI)%NIMAX_ll ) &
+                                    CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_IINF too large (>NIMAX)')
+    IF ( TOUT_BOXES(JI)%NISUP < 1 ) CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_ISUP too small (<1)' )
+    IF ( TOUT_BOXES(JI)%NISUP > DIM_MODEL(IMI)%NIMAX_ll ) &
+                                    CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_ISUP too large (>NIMAX)')
     IF ( TOUT_BOXES(JI)%NISUP < TOUT_BOXES(JI)%NIINF ) &
-                                           CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_ISUP < NOUT_BOX_IINF' )
-
-    IF ( TOUT_BOXES(JI)%NJINF < 1 )        CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_JINF too small (<1)' )
-    IF ( TOUT_BOXES(JI)%NJINF > NJMAX_ll ) CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_JINF too large (>NJMAX)')
-    IF ( TOUT_BOXES(JI)%NJSUP < 1 )        CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_JSUP too small (<1)' )
-    IF ( TOUT_BOXES(JI)%NJSUP > NJMAX_ll ) CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_JSUP too large (>NJMAX)')
+                                    CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_ISUP < NOUT_BOX_IINF' )
+
+    IF ( TOUT_BOXES(JI)%NJINF < 1 ) CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_JINF too small (<1)' )
+    IF ( TOUT_BOXES(JI)%NJINF > DIM_MODEL(IMI)%NJMAX_ll ) &
+                                    CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_JINF too large (>NJMAX)')
+    IF ( TOUT_BOXES(JI)%NJSUP < 1 ) CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_JSUP too small (<1)' )
+    IF ( TOUT_BOXES(JI)%NJSUP > DIM_MODEL(IMI)%NJMAX_ll ) &
+                                    CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_JSUP too large (>NJMAX)')
     IF ( TOUT_BOXES(JI)%NJSUP < TOUT_BOXES(JI)%NJINF ) &
-                                           CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_JSUP < NOUT_BOX_JINF' )
-
-    IF ( TOUT_BOXES(JI)%NKINF < 1 )     CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_KINF too small (<1)' )
-    IF ( TOUT_BOXES(JI)%NKINF > NKMAX ) CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_KINF too large (>NKMAX)' )
-    IF ( TOUT_BOXES(JI)%NKSUP < 1 )     CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_KSUP too small (<1)' )
-    IF ( TOUT_BOXES(JI)%NKSUP > NKMAX ) CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_KSUP too large (>NKMAX)' )
+                                    CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_JSUP < NOUT_BOX_JINF' )
+
+    IF ( TOUT_BOXES(JI)%NKINF < 1 ) CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_KINF too small (<1)' )
+    IF ( TOUT_BOXES(JI)%NKINF > DIM_MODEL(IMI)%NKMAX ) &
+                                    CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_KINF too large (>NKMAX)' )
+    IF ( TOUT_BOXES(JI)%NKSUP < 1 ) CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_KSUP too small (<1)' )
+    IF ( TOUT_BOXES(JI)%NKSUP > DIM_MODEL(IMI)%NKMAX ) &
+                                    CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_KSUP too large (>NKMAX)' )
     IF ( TOUT_BOXES(JI)%NKSUP < TOUT_BOXES(JI)%NKINF ) &
-                                           CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_KSUP < NOUT_BOX_KINF' )
+                                    CALL Print_msg( NVERB_ERROR, 'GEN', 'IO_BOX_PREPARE', 'NOUT_BOX_KSUP < NOUT_BOX_KINF' )
 
     ! Field the list of variables to write for each box (in addition to the NOUTFIELDLIST which is common to all the boxes)
     CALL IO_OUT_FIELDLIST_FILL( COUT_BOX_VAR_SUPP(IMI,JI,:), .FALSE., TOUT_BOXES(JI)%NFIELDLIST_SUPP )
-- 
GitLab