New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
modupdate.F in trunk/AGRIF/AGRIF_FILES – NEMO

source: trunk/AGRIF/AGRIF_FILES/modupdate.F @ 898

Last change on this file since 898 was 898, checked in by rblod, 16 years ago

Correct some bugs in agrif optimization and add MPP optimization, see #42

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 94.5 KB
Line 
1!
2! $Id$
3!
4C     AGRIF (Adaptive Grid Refinement In Fortran)
5C
6C     Copyright (C) 2003 Laurent Debreu (Laurent.Debreu@imag.fr)
7C                        Christophe Vouland (Christophe.Vouland@imag.fr)   
8C
9C     This program is free software; you can redistribute it and/or modify
10C     it under the terms of the GNU General Public License as published by
11C     the Free Software Foundation; either version 2 of the License, or
12C     (at your option) any later version.
13C
14C     This program is distributed in the hope that it will be useful,
15C     but WITHOUT ANY WARRANTY; without even the implied warranty of
16C     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17C     GNU General Public License for more details.
18C
19C     You should have received a copy of the GNU General Public License
20C     along with this program; if not, write to the Free Software
21C     Foundation, Inc., 59 Temple Place- Suite 330, Boston, MA 02111-1307, USA.
22C
23C
24C
25CCC   Module Agrif_Update
26C
27      Module Agrif_Update
28C
29CCC   Description:
30CCC   Module to update a parent grid from its child grids
31C
32C     Modules used:
33C   
34      Use Agrif_Updatebasic
35c      Use Agrif_Boundary
36      Use Agrif_Arrays
37      Use Agrif_CurgridFunctions
38      Use Agrif_Mask
39#ifdef AGRIF_MPI
40      Use Agrif_mpp
41#endif
42C
43      IMPLICIT NONE
44      logical, private :: precomputedone(7) = .FALSE.     
45C     
46      CONTAINS
47C     Define procedures contained in this module
48C
49C
50C
51C     **************************************************************************
52CCC   Subroutine Agrif_Update_1d
53C     **************************************************************************
54C 
55      Subroutine Agrif_Update_1d(TypeUpdate,parent,child,tab,deb,fin,
56     &                           procname)
57C
58CCC   Description:
59CCC   Subroutine to update a 1D grid variable on the parent grid.
60C
61C     Declarations:
62C
63     
64C
65C     Arguments     
66      INTEGER, DIMENSION(6) :: TypeUpdate                  ! TYPE of update (copy or average)
67      TYPE(AGRIF_PVariable) :: parent        ! Variable on the parent grid
68      TYPE(AGRIF_PVariable) :: child         ! Variable on the child grid
69      TYPE(AGRIF_PVariable) :: childtemp     ! Temporary variable on the child
70      INTEGER :: deb,fin                      ! Positions where interpolations
71                                              ! are done on the fine grid       
72      External :: procname
73      Optional ::  procname     
74      REAL, DIMENSION(lbound(child%var%array1,1):
75     &                ubound(child%var%array1,1)), Target :: tab  ! Results
76C
77C
78C     Definition of a temporary AGRIF_PVariable data TYPE 
79      allocate(childtemp % var)
80C
81C     Pointer on the root variable
82      childtemp % var % root_var => child % var %root_var
83C     
84C     Number of dimensions of the grid variable
85      childtemp % var % nbdim = 1 
86C     
87C     Values on the current grid used for the update
88      childtemp % var % array1 => tab     
89     
90C      childtemp % var % list_update => child%var%list_update
91             
92C
93     
94      IF (present(procname)) THEN
95      CALL Agrif_UpdateVariable
96     &     (TypeUpdate,parent,child,deb,fin,procname)
97      ELSE
98      CALL Agrif_UpdateVariable
99     &     (TypeUpdate,parent,child,deb,fin)
100      ENDIF     
101C     
102C      child % var % list_update => childtemp%var%list_update
103     
104      deallocate(childtemp % var)
105C
106C       
107      End Subroutine Agrif_Update_1D
108C
109C
110C
111C     **************************************************************************
112CCC   Subroutine Agrif_Update_2d
113C     **************************************************************************
114C 
115
116      Subroutine Agrif_Update_2d(TypeUpdate,parent,child,tab,deb,fin,
117     &                           procname)
118C
119CCC   Description:
120CCC   Subroutine to update a 2D grid variable on the parent grid.
121C
122C     Declarations:
123C
124     
125C
126C     Arguments     
127      INTEGER, DIMENSION(6) :: TypeUpdate                  ! TYPE of update (copy or average)
128      TYPE(AGRIF_PVariable) :: parent        ! Variable on the parent grid
129      TYPE(AGRIF_PVariable) :: child         ! Variable on the child grid
130      TYPE(AGRIF_PVariable) :: childtemp     ! Temporary variable on the child
131      INTEGER :: deb,fin                      ! Positions where interpolations
132                                              ! are done on the fine grid
133                                             
134      External :: procname
135      Optional ::  procname
136                                                         
137      REAL, DIMENSION(
138     &      lbound(child%var%array2,1):ubound(child%var%array2,1),
139     &      lbound(child%var%array2,2):ubound(child%var%array2,2)),
140     &      Target :: tab  ! Results
141C
142C
143C     Definition of a temporary AGRIF_PVariable data TYPE
144      allocate(childtemp % var)
145C
146C     Pointer on the root variable
147      childtemp % var % root_var => child % var %root_var
148C     
149C     Number of dimensions of the grid variable
150      childtemp % var % nbdim = 2 
151C     
152C     Values on the current grid used for the update
153      childtemp % var % array2 => tab     
154     
155C      childtemp % var % list_update => child%var%list_update     
156C
157      IF (present(procname)) THEN
158      CALL Agrif_UpdateVariable
159     &     (TypeUpdate,parent,child,deb,fin,procname)
160      ELSE
161      CALL Agrif_UpdateVariable
162     &     (TypeUpdate,parent,child,deb,fin)
163      ENDIF
164C     
165C      child % var % list_update => childtemp%var%list_update
166     
167      deallocate(childtemp % var)
168C
169C       
170      End Subroutine Agrif_Update_2D
171C
172C
173C
174C     **************************************************************************
175CCC   Subroutine Agrif_Update_3d
176C     **************************************************************************
177C 
178      Subroutine Agrif_Update_3d(TypeUpdate,parent,child,tab,deb,fin,
179     &                           procname)
180C
181CCC   Description:
182CCC   Subroutine to update a 3D grid variable on the parent grid.
183C
184C     Declarations:
185C
186     
187C
188C     Arguments     
189      INTEGER, DIMENSION(6) :: TypeUpdate                  ! TYPE of update (copy or average)
190      TYPE(AGRIF_PVariable) :: parent        ! Variable on the parent grid
191      TYPE(AGRIF_PVariable) :: child         ! Variable on the child grid
192      TYPE(AGRIF_PVariable) :: childtemp     ! Temporary variable on the child
193      INTEGER :: deb,fin                      ! Positions where interpolations
194                                              ! are done on the fine grid   
195      External :: procname
196      Optional ::  procname
197                                                       
198      REAL, DIMENSION(
199     &      lbound(child%var%array3,1):ubound(child%var%array3,1),
200     &      lbound(child%var%array3,2):ubound(child%var%array3,2),
201     &      lbound(child%var%array3,3):ubound(child%var%array3,3)),
202     &      Target :: tab  ! Results   
203C
204C
205C     Definition of a temporary AGRIF_PVariable data TYPE 
206      allocate(childtemp % var)
207C
208C     Pointer on the root variable
209      childtemp % var % root_var => child % var %root_var
210C     
211C     Number of dimensions of the grid variable
212      childtemp % var % nbdim = 3 
213C     
214C     Values on the current grid used for the update
215      childtemp % var % array3 => tab     
216     
217C      childtemp % var % list_update => child%var%list_update     
218C
219      IF (present(procname)) THEN
220      CALL Agrif_UpdateVariable
221     &     (TypeUpdate,parent,child,deb,fin,procname)
222      ELSE
223      CALL Agrif_UpdateVariable
224     &     (TypeUpdate,parent,child,deb,fin)
225      ENDIF
226C     
227C      child % var % list_update => childtemp%var%list_update
228     
229      DEALLOCATE(childtemp % var)
230C
231C       
232      End Subroutine Agrif_Update_3D
233C
234C
235C
236C     **************************************************************************
237CCC   Subroutine Agrif_Update_4d
238C     **************************************************************************
239C 
240      Subroutine Agrif_Update_4d(TypeUpdate,parent,child,tab,deb,fin,
241     &                           procname)
242C
243CCC   Description:
244CCC   Subroutine to update a 4D grid variable on the parent grid.
245C
246C     Declarations:
247C
248     
249C
250C     Arguments     
251      INTEGER, DIMENSION(6) :: TypeUpdate                  ! TYPE of update (copy or average)
252      TYPE(AGRIF_PVariable) :: parent        ! Variable on the parent grid
253      TYPE(AGRIF_PVariable) :: child         ! Variable on the child grid
254      TYPE(AGRIF_PVariable) :: childtemp     ! Temporary variable on the child
255      INTEGER :: deb,fin                      ! Positions where interpolations
256                                              ! are done on the fine grid     
257      External :: procname
258      Optional ::  procname       
259      REAL, DIMENSION(
260     &      lbound(child%var%array4,1):ubound(child%var%array4,1),
261     &      lbound(child%var%array4,2):ubound(child%var%array4,2),
262     &      lbound(child%var%array4,3):ubound(child%var%array4,3),
263     &      lbound(child%var%array4,4):ubound(child%var%array4,4)),
264     &      Target :: tab  ! Results
265C
266C
267C     Definition of a temporary AGRIF_PVariable data TYPE 
268      allocate(childtemp % var)
269C
270C     Pointer on the root variable
271      childtemp % var % root_var => child % var %root_var
272C     
273C     Number of dimensions of the grid variable
274      childtemp % var % nbdim = 4 
275C     
276C     Values on the current grid used for the update
277      childtemp % var % array4 => tab     
278     
279C      childtemp % var % list_update => child%var%list_update
280           
281C
282      IF (present(procname)) THEN
283      CALL Agrif_UpdateVariable
284     &     (TypeUpdate,parent,child,deb,fin,procname)
285      ELSE
286      CALL Agrif_UpdateVariable
287     &     (TypeUpdate,parent,child,deb,fin)
288      ENDIF
289
290C      child % var % list_update => childtemp%var%list_update     
291C
292      deallocate(childtemp % var)
293C
294C       
295      End Subroutine Agrif_Update_4D
296C
297C
298C
299C     **************************************************************************
300CCC   Subroutine Agrif_Update_5d
301C     **************************************************************************
302C 
303      Subroutine Agrif_Update_5d(TypeUpdate,parent,child,tab,deb,fin,
304     &                           procname)
305C
306CCC   Description:
307CCC   Subroutine to update a 5D grid variable on the parent grid.
308C
309C     Declarations:
310C
311     
312C
313C     Arguments     
314      INTEGER, DIMENSION(6) :: TypeUpdate                  ! TYPE of update (copy or average)
315      TYPE(AGRIF_PVariable) :: parent        ! Variable on the parent grid
316      TYPE(AGRIF_PVariable) :: child         ! Variable on the child grid
317      TYPE(AGRIF_PVariable) :: childtemp     ! Temporary variable on the child
318      INTEGER :: deb,fin                      ! Positions where interpolations
319                                              ! are done on the fine grid     
320      External :: procname
321      Optional ::  procname
322             
323      REAL, DIMENSION(
324     &      lbound(child%var%array5,1):ubound(child%var%array5,1),
325     &      lbound(child%var%array5,2):ubound(child%var%array5,2),
326     &      lbound(child%var%array5,3):ubound(child%var%array5,3),
327     &      lbound(child%var%array5,4):ubound(child%var%array5,4),
328     &      lbound(child%var%array5,5):ubound(child%var%array5,5)),
329     &      Target :: tab  ! Results
330C
331C
332C     Definition of a temporary AGRIF_PVariable data TYPE 
333      allocate(childtemp % var)
334C
335C     Pointer on the root variable
336      childtemp % var % root_var => child % var %root_var
337C
338C     Number of dimensions of the grid variable
339      childtemp % var % nbdim = 5 
340C     
341C     Values on the current grid used for the update
342      childtemp % var % array5 => tab     
343     
344C      childtemp % var % list_update => child%var%list_update     
345C
346      IF (present(procname)) THEN
347      CALL Agrif_UpdateVariable
348     &     (TypeUpdate,parent,child,deb,fin,procname)
349      ELSE
350      CALL Agrif_UpdateVariable
351     &     (TypeUpdate,parent,child,deb,fin)
352      ENDIF
353     
354C      child % var % list_update => childtemp%var%list_update
355           
356C     
357      deallocate(childtemp % var)
358C
359C       
360      End Subroutine Agrif_Update_5D
361C
362C
363C
364C
365C     **************************************************************************
366CCC   Subroutine Agrif_Update_6d
367C     **************************************************************************
368C 
369      Subroutine Agrif_Update_6d(TypeUpdate,parent,child,tab,deb,fin)
370C
371CCC   Description:
372CCC   Subroutine to update a 6D grid variable on the parent grid.
373C
374C     Declarations:
375C
376     
377C
378C     Arguments     
379      INTEGER, DIMENSION(6) :: TypeUpdate                  ! TYPE of update (copy or average)
380      TYPE(AGRIF_PVariable) :: parent        ! Variable on the parent grid
381      TYPE(AGRIF_PVariable) :: child         ! Variable on the child grid
382      TYPE(AGRIF_PVariable) :: childtemp     ! Temporary variable on the child
383      INTEGER :: deb,fin                      ! Positions where interpolations
384                                              ! are done on the fine grid       
385      REAL, DIMENSION(
386     &      lbound(child%var%array6,1):ubound(child%var%array6,1),
387     &      lbound(child%var%array6,2):ubound(child%var%array6,2),
388     &      lbound(child%var%array6,3):ubound(child%var%array6,3),
389     &      lbound(child%var%array6,4):ubound(child%var%array6,4),
390     &      lbound(child%var%array6,5):ubound(child%var%array6,5),
391     &      lbound(child%var%array6,6):ubound(child%var%array6,6)),
392     &      Target :: tab  ! Results
393C
394C
395C     Definition of a temporary AGRIF_PVariable data TYPE 
396      allocate(childtemp % var)
397C
398C     Pointer on the root variable
399      childtemp % var % root_var => child % var %root_var
400C     
401C     Number of dimensions of the grid variable
402      childtemp % var % nbdim = 6 
403C     
404C     Values on the current grid used for the update
405      childtemp % var % array6 => tab     
406C      childtemp % var % list_update => child%var%list_update
407C
408      Call Agrif_UpdateVariable
409     &     (TypeUpdate,parent,child,deb,fin)
410     
411C      child % var % list_update => childtemp%var%list_update
412           
413C     
414      deallocate(childtemp % var)
415C
416C       
417      End Subroutine Agrif_Update_6D
418C
419C
420C
421C     ************************************************************************** 
422C     Subroutine Agrif_UpdateVariable   
423C     ************************************************************************** 
424C   
425      Subroutine Agrif_UpdateVariable(TypeUpdate,parent,child,deb,fin,
426     &                   procname)   
427C
428CCC   Description:
429CCC   Subroutine to set arguments of Agrif_UpdatenD, n being the number of
430C         dimensions of the grid variable.
431C
432CC    Declarations:
433C     
434c     
435C     
436C     Scalar argument
437      INTEGER, DIMENSION(6) :: TypeUpdate                  ! TYPE of update (copy or average)
438C     Data TYPE arguments
439      TYPE(AGRIF_PVariable) :: parent   ! Variable on the parent grid
440      TYPE(AGRIF_PVariable) :: child    ! Variable on the child grid
441      INTEGER               :: deb,fin  ! Positions where boundary conditions
442                                        !    are calculated     
443      External :: procname
444      Optional ::  procname
445     
446C
447C     Local scalars     
448      INTEGER :: nbdim                  ! Number of dimensions of the current
449                                        !    grid
450      INTEGER ,DIMENSION(6) :: pttab_child 
451      INTEGER ,DIMENSION(6) :: petab_child     
452      INTEGER ,DIMENSION(6) :: pttab_parent 
453      REAL    ,DIMENSION(6) :: s_child,s_parent
454      REAL    ,DIMENSION(6) :: ds_child,ds_parent
455      INTEGER,DIMENSION(6)          :: loctab_Child ! Indicates if the child
456                                        !    grid has a common border with
457                                        !    the root grid           
458      TYPE(AGRIF_Variable), Pointer :: root               ! Variable on the root grid
459      INTEGER,DIMENSION(6)          :: posvartab_Child    ! Position of the
460                                        !    variable on the cell
461      INTEGER,DIMENSION(6)          :: nbtab_Child        ! Number of the cells   
462      INTEGER :: n             
463      LOGICAL :: wholeupdate
464C
465C 
466
467      loctab_child(:) = 0
468C
469      root => child % var % root_var 
470      nbdim = root % nbdim
471C
472      do n = 1,nbdim
473        posvartab_child(n) = root % posvar(n)
474      enddo
475C     
476     
477      Call PreProcessToInterpOrUpdate(parent,child,
478     &             petab_Child(1:nbdim),
479     &             pttab_Child(1:nbdim),pttab_Parent(1:nbdim),
480     &             s_Child(1:nbdim),s_Parent(1:nbdim),
481     &             ds_Child(1:nbdim),ds_Parent(1:nbdim),
482     &             nbdim)
483C
484C
485      do n = 1,nbdim
486C
487        Select case(root % interptab(n))
488C
489          case('x') ! x DIMENSION
490C
491            nbtab_Child(n) = Agrif_Curgrid % nb(1)
492C
493          case('y') ! y DIMENSION     
494C
495            nbtab_Child(n) = Agrif_Curgrid % nb(2)
496C
497          case('z') ! z DIMENSION
498C
499            nbtab_Child(n) = Agrif_Curgrid % nb(3)
500C
501          case('N') ! No space DIMENSION     
502C
503            select case (nbdim) 
504C     
505              case(1)
506                nbtab_Child(n) = SIZE(child % var % array1,n) - 1
507              case(2)
508                nbtab_Child(n) = SIZE(child % var % array2,n) - 1
509              case(3)
510                nbtab_Child(n) = SIZE(child % var % array3,n) - 1
511              case(4)
512                nbtab_Child(n) = SIZE(child % var % array4,n) - 1
513              case(5)
514                nbtab_Child(n) = SIZE(child % var % array5,n) - 1 
515              case(6)
516                nbtab_Child(n) = SIZE(child % var % array6,n) - 1 
517C
518            end select
519C
520C           No interpolation but only a copy of the values of the grid variable     
521C     
522            posvartab_child(n) = 1
523           
524            loctab_child(n) = -3
525C
526        End select
527C
528      enddo
529     
530C     Call to a procedure of update according to the number of dimensions of
531C     the grid variable
532
533      wholeupdate = .FALSE.
534
535      IF ((deb == -99) .AND. (deb == fin)) THEN
536       wholeupdate = .TRUE.
537      ENDIF
538
539      IF ((deb > fin)) THEN
540       wholeupdate = .TRUE.
541      ENDIF
542     
543       IF (present(procname)) THEN
544
545          IF (wholeupdate) THEN
546
547          Call AGRIF_UpdateWhole
548     &         (TypeUpdate,parent,child,deb,fin,
549     &          pttab_Child(1:nbdim),pttab_Parent(1:nbdim),
550     &          nbtab_Child(1:nbdim),posvartab_Child(1:nbdim),
551     &          loctab_Child(1:nbdim),
552     &          s_Child(1:nbdim),s_Parent(1:nbdim),
553     &          ds_Child(1:nbdim),ds_Parent(1:nbdim),nbdim,procname) 
554         ELSE
555          Call AGRIF_UpdateBcnD
556     &         (TypeUpdate,parent,child,deb,fin,
557     &          pttab_Child(1:nbdim),pttab_Parent(1:nbdim),
558     &          nbtab_Child(1:nbdim),posvartab_Child(1:nbdim),
559     &          loctab_Child(1:nbdim),
560     &          s_Child(1:nbdim),s_Parent(1:nbdim),
561     &          ds_Child(1:nbdim),ds_Parent(1:nbdim),nbdim,procname) 
562         ENDIF
563       ELSE
564         IF (wholeupdate) THEN
565          Call AGRIF_UpdateWhole
566     &         (TypeUpdate,parent,child,deb,fin,
567     &          pttab_Child(1:nbdim),pttab_Parent(1:nbdim),
568     &          nbtab_Child(1:nbdim),posvartab_Child(1:nbdim),
569     &          loctab_Child(1:nbdim),
570     &          s_Child(1:nbdim),s_Parent(1:nbdim),
571     &          ds_Child(1:nbdim),ds_Parent(1:nbdim),nbdim)
572         ELSE
573          Call AGRIF_UpdateBcnD
574     &         (TypeUpdate,parent,child,deb,fin,
575     &          pttab_Child(1:nbdim),pttab_Parent(1:nbdim),
576     &          nbtab_Child(1:nbdim),posvartab_Child(1:nbdim),
577     &          loctab_Child(1:nbdim),
578     &          s_Child(1:nbdim),s_Parent(1:nbdim),
579     &          ds_Child(1:nbdim),ds_Parent(1:nbdim),nbdim)
580         ENDIF
581       ENDIF
582C
583      Return
584C
585C
586      End subroutine Agrif_UpdateVariable
587C
588C     **************************************************************************
589CCC   Subroutine Agrif_UpdateWhole
590C     **************************************************************************               
591C
592      Subroutine AGRIF_UpdateWhole(TypeUpdate,parent,child,deb,fin,
593     &                           pttab_child,pttab_Parent,
594     &                           nbtab_Child,posvartab_Child,
595     &                           loctab_Child,
596     &                           s_Child,s_Parent,
597     &                           ds_Child,ds_Parent,nbdim,procname)
598C
599CCC   Description:
600CCC   Subroutine to calculate the boundary conditions for a nD grid variable on 
601CCC   a fine grid by using a space and time interpolations; it is called by the 
602CCC   Agrif_CorrectVariable procedure.
603C
604C
605C     Declarations:
606C
607     
608C
609#ifdef AGRIF_MPI
610C
611#include "mpif.h"
612C
613#endif
614C
615C     Arguments
616      INTEGER, DIMENSION(6) :: TypeUpdate            ! TYPE of update (copy or
617                                                     !    average)
618      TYPE(AGRIF_PVariable)    :: parent             ! Variable on the parent
619                                                     !    grid
620      TYPE(AGRIF_PVariable)    :: child              ! Variable on the child
621                                                     !    grid
622      INTEGER :: deb, fin
623      INTEGER                  :: nbdim              ! Number of dimensions of
624                                                     !    the grid variable
625      INTEGER,DIMENSION(nbdim) :: pttab_child        ! Index of the first point
626                                                     !    inside the domain for
627                                                     !    the parent grid
628                                                     !    variable
629      INTEGER,DIMENSION(nbdim) :: pttab_Parent       ! Index of the first point
630                                                     !    inside the domain for
631                                                     !    the child grid
632                                                     !    variable
633      INTEGER,DIMENSION(nbdim) :: nbtab_Child        ! Number of cells of the
634                                                     !    child grid
635      INTEGER,DIMENSION(nbdim) :: posvartab_Child    ! Position of the grid
636                                                     !    variable (1 or 2)
637      INTEGER,DIMENSION(nbdim) :: loctab_Child       ! Indicates if the child
638                                                     !    grid has a common
639                                                     !    border with the root
640                                                     !    grid
641      REAL   ,DIMENSION(nbdim) :: s_Child,s_Parent   ! Positions of the parent
642                                                     !    and child grids
643      REAL   ,DIMENSION(nbdim) :: ds_Child,ds_Parent ! Space steps of the parent
644                                                     !    and child grids
645      External :: procname
646      Optional ::  procname     
647C
648C     Local variables     
649      INTEGER,DIMENSION(nbdim,2)   :: lubglob
650      INTEGER                      :: i                 
651      INTEGER,DIMENSION(nbdim,2,2) :: indtab         ! Arrays indicating the
652                                                     !    limits of the child
653      INTEGER,DIMENSION(nbdim,2,2) :: indtruetab     ! grid variable where
654                                                     !   boundary conditions are
655      integer :: coeffraf
656      INTEGER :: debloc, finloc
657C
658#ifdef AGRIF_MPI
659C
660      INTEGER,DIMENSION(nbdim)   :: lb,ub
661      INTEGER,DIMENSION(nbdim,2) :: iminmaxg
662      INTEGER                    :: code
663C
664#endif
665C     
666C
667C indtab contains the limits for the fine grid points that will be used
668C in the update scheme
669
670      DO i = 1, nbdim
671        coeffraf = nint(ds_Parent(i)/ds_Child(i))
672        debloc = 0
673        finloc = nbtab_Child(i)/coeffraf - 1
674
675        IF (posvartab_child(i) == 1) THEN
676           finloc = finloc - 1
677        ENDIF
678
679        IF (deb > fin) THEN
680          debloc = deb
681          finloc = finloc - deb
682        ENDIF
683
684        indtab(i,1,1) = pttab_child(i) + (debloc + 1) * coeffraf
685        indtab(i,1,2) = pttab_child(i) + (finloc + 1) * coeffraf
686
687        IF (posvartab_child(i) == 1) THEN
688          IF (TypeUpdate(i) .EQ. Agrif_Update_Full_Weighting) THEN
689            indtab(i,1,1) = indtab(i,1,1) - (coeffraf - 1)
690            indtab(i,1,2) = indtab(i,1,2) + (coeffraf - 1)       
691          ELSE IF (TypeUpdate(i) .NE. Agrif_Update_Copy) THEN
692            indtab(i,1,1) = indtab(i,1,1) - coeffraf / 2
693            indtab(i,1,2) = indtab(i,1,2) + coeffraf / 2
694          ENDIF
695        ELSE
696          indtab(i,1,1) = indtab(i,1,1) - coeffraf
697          indtab(i,1,2) = indtab(i,1,2) - 1
698          IF ((TypeUpdate(i) .EQ. Agrif_Update_Full_Weighting)
699     &                            .AND.(mod(coeffraf,2) == 1)) THEN
700            indtab(i,1,1) = indtab(i,1,1) - 1
701            indtab(i,1,2) = indtab(i,1,2) + 1
702          ENDIF
703        ENDIF
704        IF (loctab_child(i) == -3) THEN
705           indtab(i,1,1) = pttab_child(i)
706C
707               if (posvartab_child(i) == 1) then
708C
709               indtab(i,1,2) = pttab_child(i) + nbtab_child(i) 
710C
711               else
712C
713               indtab(i,1,2) = pttab_child(i) + nbtab_child(i) - 1
714               ENDIF
715        ENDIF
716      ENDDO
717
718C lubglob contains the global lbound and ubound of the child array
719C lubglob(:,1) : global lbound for each dimension
720C lubglob(:,2) : global lbound for each dimension
721
722#if !defined AGRIF_MPI
723        Call Agrif_nbdim_Get_bound_dimension(child % var,lubglob(:,1),
724     &               lubglob(:,2),nbdim)
725C
726#else
727C     
728        Call Agrif_nbdim_Get_bound_dimension(child % var,lb,ub,nbdim)
729      DO i = 1,nbdim
730C
731        Call Agrif_Invloc(lb(i),Agrif_Procrank,i,iminmaxg(i,1))
732        Call Agrif_Invloc(ub(i),Agrif_Procrank,i,iminmaxg(i,2))
733C
734      ENDDO
735C
736      iminmaxg(1:nbdim,2) = - iminmaxg(1:nbdim,2)
737     
738      CALL MPI_ALLREDUCE(iminmaxg,lubglob,2*nbdim,MPI_INTEGER,MPI_MIN,
739     &                     MPI_COMM_WORLD,code)
740
741      lubglob(1:nbdim,2) = - lubglob(1:nbdim,2)     
742C
743#endif
744C
745
746      indtruetab(1:nbdim,1,1) = max(indtab(1:nbdim,1,1),
747     &     lubglob(1:nbdim,1))
748      indtruetab(1:nbdim,1,2) = min(indtab(1:nbdim,1,2),
749     &     lubglob(1:nbdim,2))
750
751C
752C
753
754           IF (present(procname)) THEN
755              Call Agrif_UpdatenD             
756     &             (TypeUpdate,parent,child,
757     &              indtruetab(1:nbdim,1,1),indtruetab(1:nbdim,1,2),
758     &              pttab_child(1:nbdim),pttab_Parent(1:nbdim),
759     &              s_Child(1:nbdim),s_Parent(1:nbdim),
760     &              ds_Child(1:nbdim),ds_Parent(1:nbdim),
761     &              posvartab_child,loctab_Child,
762     &              nbdim,procname)
763           ELSE
764              Call Agrif_UpdatenD             
765     &             (TypeUpdate,parent,child,
766     &              indtruetab(1:nbdim,1,1),indtruetab(1:nbdim,1,2),
767     &              pttab_child(1:nbdim),pttab_Parent(1:nbdim),
768     &              s_Child(1:nbdim),s_Parent(1:nbdim),
769     &              ds_Child(1:nbdim),ds_Parent(1:nbdim),
770     &              posvartab_child,loctab_Child,
771     &              nbdim)           
772           ENDIF
773C
774C     
775C 
776      End Subroutine Agrif_UpdateWhole
777C
778C     **************************************************************************
779CCC   Subroutine Agrif_UpdateBcnd
780C     **************************************************************************               
781C
782      Subroutine AGRIF_UpdateBcnd(TypeUpdate,parent,child,deb,fin,
783     &                           pttab_child,pttab_Parent,
784     &                           nbtab_Child,posvartab_Child,
785     &                           loctab_Child,
786     &                           s_Child,s_Parent,
787     &                           ds_Child,ds_Parent,nbdim,procname)
788C
789CCC   Description:
790CCC   Subroutine to calculate the boundary conditions for a nD grid variable on
791CCC   a fine grid by using a space and time interpolations; it is called by the 
792CCC   Agrif_CorrectVariable procedure.
793C
794C
795C     Declarations:
796C
797     
798C
799#ifdef AGRIF_MPI
800C
801#include "mpif.h"
802C
803#endif
804C
805C     Arguments
806      INTEGER, DIMENSION(6) :: TypeUpdate            ! TYPE of update
807                                                     !   (copy or average)
808      TYPE(AGRIF_PVariable)    :: parent             ! Variable on the parent
809                                                     !   grid
810      TYPE(AGRIF_PVariable)    :: child              ! Variable on the child
811                                                     !   grid
812      INTEGER                  :: deb,fin            ! Positions where
813                                                     !   interpolations are done
814      INTEGER                  :: nbdim              ! Number of dimensions of
815                                                     !   the grid variable
816      INTEGER,DIMENSION(nbdim) :: pttab_child        ! Index of the first point
817                                                     !   inside the domain for
818                                                     !   the parent grid
819                                                     !   variable
820      INTEGER,DIMENSION(nbdim) :: pttab_Parent       ! Index of the first point
821                                                     !   inside the domain for
822                                                     !   the child grid variable
823      INTEGER,DIMENSION(nbdim) :: nbtab_Child        ! Number of cells of the
824                                                     !   child grid
825      INTEGER,DIMENSION(nbdim) :: posvartab_Child    ! Position of the grid
826                                                     !   variable (1 or 2)
827      INTEGER,DIMENSION(nbdim) :: loctab_Child       ! Indicates if the child
828                                                     !   grid has a common
829                                                     !   border with the root
830                                                     !   grid
831      REAL   ,DIMENSION(nbdim) :: s_Child,s_Parent   ! Positions of the parent
832                                                     !   and child grids
833      REAL   ,DIMENSION(nbdim) :: ds_Child,ds_Parent ! Space steps of the parent
834                                                     !   and child grids
835      External :: procname
836      Optional ::  procname     
837C
838C     Local variables
839      INTEGER,DIMENSION(nbdim,2)   :: lubglob
840      INTEGER                      :: i                 
841      INTEGER,DIMENSION(nbdim,2,2) :: indtab         ! Arrays indicating the
842                                                     !   limits of the child
843      INTEGER,DIMENSION(nbdim,2,2) :: indtruetab     ! grid variable where
844                                                     !  boundary conditions are
845      INTEGER,DIMENSION(nbdim,2,2,nbdim)   :: ptres      ! calculated
846      INTEGER                      :: nb,ndir,n
847      integer :: coeffraf
848C
849#ifdef AGRIF_MPI
850C
851      INTEGER,DIMENSION(nbdim)   :: lb,ub
852      INTEGER,DIMENSION(nbdim,2) :: iminmaxg
853      INTEGER                    :: code
854C
855#endif
856C     
857C
858
859      DO i = 1, nbdim
860        coeffraf = nint(ds_Parent(i)/ds_Child(i))
861        indtab(i,1,1) = pttab_child(i) + (deb + 1) * coeffraf
862        indtab(i,1,2) = pttab_child(i) + (fin + 1) * coeffraf
863
864        indtab(i,2,1) = pttab_child(i) + nbtab_child(i)
865     &    - (fin + 1) *  coeffraf
866        indtab(i,2,2) = pttab_child(i) + nbtab_child(i)
867     &    - (deb + 1) *  coeffraf
868
869        IF (posvartab_child(i) == 1) THEN
870          IF (TypeUpdate(i) .EQ. Agrif_Update_Full_Weighting) THEN
871            indtab(i,:,1) = indtab(i,:,1) - (coeffraf - 1)
872            indtab(i,:,2) = indtab(i,:,2) + (coeffraf - 1)       
873          ELSE IF (TypeUpdate(i) .NE. Agrif_Update_Copy) THEN
874            indtab(i,:,1) = indtab(i,:,1) - coeffraf / 2
875            indtab(i,:,2) = indtab(i,:,2) + coeffraf / 2
876          ENDIF
877        ELSE
878          indtab(i,1,1) = indtab(i,1,1) - coeffraf
879          indtab(i,1,2) = indtab(i,1,2) - 1
880          indtab(i,2,2) = indtab(i,2,2) + coeffraf - 1
881          IF (TypeUpdate(i) .EQ. Agrif_Update_Full_Weighting) THEN
882            indtab(i,1,1) = indtab(i,1,1) - 1
883            indtab(i,1,2) = indtab(i,1,2) + 1
884            indtab(i,2,1) = indtab(i,2,1) - 1
885            indtab(i,2,2) = indtab(i,2,2) + 1
886          ENDIF         
887        ENDIF
888      ENDDO
889
890#if !defined AGRIF_MPI     
891        Call Agrif_nbdim_Get_bound_dimension(child % var,lubglob(:,1),
892     &               lubglob(:,2),nbdim)
893
894C
895#else
896C     
897        Call Agrif_nbdim_Get_bound_dimension(child % var,lb,ub,nbdim)
898      DO i = 1,nbdim
899C
900        Call Agrif_Invloc(lb(i),Agrif_Procrank,i,iminmaxg(i,1))
901        Call Agrif_Invloc(ub(i),Agrif_Procrank,i,iminmaxg(i,2))
902C
903      ENDDO
904C
905      iminmaxg(1:nbdim,2) = - iminmaxg(1:nbdim,2)
906     
907      CALL MPI_ALLREDUCE(iminmaxg,lubglob,2*nbdim,MPI_INTEGER,MPI_MIN,
908     &                     MPI_COMM_WORLD,code) 
909     
910      lubglob(1:nbdim,2) = - lubglob(1:nbdim,2)     
911C
912#endif
913C     
914      indtruetab(1:nbdim,1,1) = max(indtab(1:nbdim,1,1),
915     &     lubglob(1:nbdim,1))
916      indtruetab(1:nbdim,1,2) = max(indtab(1:nbdim,1,2),
917     &     lubglob(1:nbdim,1))
918      indtruetab(1:nbdim,2,1) = min(indtab(1:nbdim,2,1),
919     &     lubglob(1:nbdim,2))
920      indtruetab(1:nbdim,2,2) = min(indtab(1:nbdim,2,2),
921     &     lubglob(1:nbdim,2))
922                       
923C 
924C
925      do nb = 1,nbdim
926C
927        do ndir = 1,2
928C
929          if (loctab_child(nb) /= -3) then
930C           
931              do n = 1,2
932C
933                ptres(nb,n,ndir,nb) = indtruetab(nb,ndir,n)
934C
935              enddo             
936C
937              do i = 1,nbdim
938C     
939                if (i .NE. nb) then     
940C
941                    if (loctab_child(i) == -3) then
942C
943                        ptres(i,1,ndir,nb) = pttab_child(i)
944C
945                      else
946C
947                        ptres(i,1,ndir,nb) = indtruetab(i,1,1)
948C
949                    endif
950C
951                    if (loctab_child(i) == -3) then
952C
953                        if (posvartab_child(i) == 1) then
954C
955                            ptres(i,2,ndir,nb) = pttab_child(i) 
956     &                                + nbtab_child(i)
957C
958                          else
959C
960                            ptres(i,2,ndir,nb) = pttab_child(i) 
961     &                             + nbtab_child(i) - 1
962C
963                        endif                             
964C
965                      else
966C
967                        ptres(i,2,ndir,nb) = indtruetab(i,2,2)
968C
969                    endif                       
970C     
971                endif
972C     
973              enddo
974     
975C
976           
977        endif
978     
979        enddo
980       enddo
981C
982
983C
984
985      do nb = 1,nbdim
986C
987        do ndir = 1,2               
988C
989          if (loctab_child(nb) /= -3) then
990C
991           IF (present(procname)) THEN
992              Call Agrif_UpdatenD             
993     &             (TypeUpdate,parent,child,
994     &              ptres(1:nbdim,1,ndir,nb),ptres(1:nbdim,2,ndir,nb),
995     &              pttab_child(1:nbdim),pttab_Parent(1:nbdim),
996     &              s_Child(1:nbdim),s_Parent(1:nbdim),
997     &              ds_Child(1:nbdim),ds_Parent(1:nbdim),
998     &              posvartab_Child,loctab_Child,
999     &              nbdim,procname,nb,ndir)
1000           ELSE
1001              Call Agrif_UpdatenD             
1002     &             (TypeUpdate,parent,child,
1003     &              ptres(1:nbdim,1,ndir,nb),ptres(1:nbdim,2,ndir,nb),
1004     &              pttab_child(1:nbdim),pttab_Parent(1:nbdim),
1005     &              s_Child(1:nbdim),s_Parent(1:nbdim),
1006     &              ds_Child(1:nbdim),ds_Parent(1:nbdim),
1007     &              posvartab_Child,loctab_Child,
1008     &              nbdim)           
1009           ENDIF
1010C
1011          endif
1012         
1013C
1014        enddo       
1015C
1016      enddo
1017C
1018C     
1019C 
1020      End Subroutine Agrif_UpdateBcnd
1021C
1022C     ************************************************************************** 
1023CCC   Subroutine Agrif_UpdatenD 
1024C     ************************************************************************** 
1025C 
1026      Subroutine Agrif_UpdatenD(TypeUpdate,parent,child,
1027     &                          pttab,petab,
1028     &                          pttab_Child,pttab_Parent,
1029     &                          s_Child,s_Parent,
1030     &                          ds_Child,ds_Parent,
1031     &                          posvartab_Child,loctab_Child,
1032     &                          nbdim,procname,nb,ndir) 
1033C
1034C     Description:
1035C     Subroutine to update a 2D grid variable on the parent grid of 
1036C        the current grid. 
1037C
1038C     Declarations:
1039C
1040     
1041C
1042#ifdef AGRIF_MPI
1043C
1044#include "mpif.h"
1045C
1046#endif
1047C
1048C     Arguments
1049      INTEGER                    :: nbdim
1050      INTEGER, DIMENSION(6) :: TypeUpdate              ! TYPE of update
1051                                                       !  (copy or average)
1052      TYPE(AGRIF_PVARIABLE)      :: parent             ! Variable of the parent
1053                                                       !   grid   
1054      TYPE(AGRIF_PVARIABLE)      :: child              ! Variable of the child
1055                                                       !   grid
1056      INTEGER,DIMENSION(nbdim)   :: pttab              ! Index of the first
1057                                                       !   point inside the
1058                                                       !   domain
1059      INTEGER,DIMENSION(nbdim)   :: petab              ! Index of the first
1060                                                       !   point inside the
1061                                                       !   domain
1062      INTEGER,DIMENSION(nbdim)   :: pttab_Child        ! Index of the first
1063                                                       !   point inside the
1064                                                       !   domain for the child
1065                                                       !   grid variable
1066      INTEGER,DIMENSION(nbdim)   :: pttab_Parent       ! Index of the first
1067                                                       !   point inside the
1068                                                       !   domain for the parent
1069                                                       !   grid variable
1070      REAL,DIMENSION(nbdim)      :: s_Child,s_Parent   ! Positions of the parent
1071                                                       !   and child grids
1072      REAL,DIMENSION(nbdim)      :: ds_Child,ds_Parent ! Space steps of the
1073                                                       !   parent and child
1074                                                       !   grids
1075      External :: procname
1076      Optional ::  procname
1077      Integer :: nb,ndir
1078      Optional :: nb,ndir
1079     
1080C
1081C     Local pointers
1082      TYPE(AGRIF_PVARIABLE), SAVE      :: tempP      ! Temporary parent grid variable
1083      TYPE(AGRIF_PVARIABLE), SAVE      :: tempC      ! Temporary child grid variable
1084C
1085C     Local scalars
1086      INTEGER,DIMENSION(nbdim)    :: pttruetab,cetruetab
1087      INTEGER,DIMENSION(nbdim)    :: posvartab_Child,loctab_Child
1088      INTEGER,DIMENSION(nbdim)    :: indmin,indmax
1089      INTEGER,DIMENSION(nbdim)    :: indminglob,indmaxglob
1090      REAL   ,DIMENSION(nbdim)    :: s_Child_temp,s_Parent_temp
1091cccccccc      LOGICAL,DIMENSION(nbdim)    :: noraftab
1092      INTEGER,DIMENSION(nbdim)    :: lowerbound,upperbound
1093      LOGICAL :: memberin, member
1094      INTEGER,DIMENSION(nbdim)    :: pttruetabwhole,cetruetabwhole
1095      INTEGER,DIMENSION(nbdim,2,2) :: childarray
1096      INTEGER,DIMENSION(nbdim,2,2) :: parentarray
1097      TYPE(AGRIF_PVARIABLE), SAVE      :: tempCextend,tempPextend ! Temporary child
1098      INTEGER :: nbin, ndirin
1099C
1100#ifdef AGRIF_MPI
1101C
1102      INTEGER,DIMENSION(nbdim)    :: indminglob2,indmaxglob2
1103      LOGICAL, DIMENSION(0:Agrif_Nbprocs-1) :: sendtoproc1,recvfromproc1
1104      LOGICAL, DIMENSION(0:Agrif_Nbprocs-1) :: sendtoproc2,recvfromproc2     
1105      INTEGER                              :: code
1106      INTEGER                              :: i,j,k
1107      INTEGER,DIMENSION(nbdim,4)           :: tab3
1108      INTEGER,DIMENSION(nbdim,4,0:Agrif_Nbprocs-1) :: tab4
1109      INTEGER,DIMENSION(nbdim,0:Agrif_Nbprocs-1,8) :: tab4t
1110      INTEGER,DIMENSION(nbdim,0:Agrif_Nbprocs-1,8) :: tab5t     
1111      LOGICAL :: find_list_update
1112      LOGICAL, DIMENSION(0:Agrif_Nbprocs-1) :: memberinall, memberinall2
1113      LOGICAL, DIMENSION(1) :: memberin1     
1114C
1115#endif
1116C
1117 
1118C
1119C     local lbound and ubound of the child array
1120
1121      Call Agrif_nbdim_Get_bound_dimension(child%var,
1122     &                              lowerbound,upperbound,nbdim)
1123
1124C here pttab and petab corresponds to the (global) indices of the points needed
1125C in the update
1126C pttruetab and cetruetab contains only indices that are present
1127C on the local processor
1128
1129      Call Agrif_Childbounds(nbdim,
1130     &                       lowerbound,upperbound,
1131     &                       pttab,petab,
1132     &                       pttruetab,cetruetab,memberin)
1133
1134       Call Agrif_Prtbounds(nbdim,indminglob,indmaxglob,s_Parent_temp,
1135     &                     s_Child_temp,s_Child,ds_Child,
1136     &                     s_Parent,ds_Parent,
1137     &                     pttab,petab,pttab_Child,
1138     &                     pttab_Parent,
1139     &                     posvartab_Child,TypeUpdate,loctab_Child
1140#ifdef AGRIF_MPI
1141     &                 ,pttruetabwhole,cetruetabwhole
1142#endif
1143     &     )
1144
1145#ifdef AGRIF_MPI
1146      IF (memberin) THEN
1147       Call Agrif_GlobtoLocInd2(childarray,
1148     &                     lowerbound,upperbound,
1149     &                     pttruetab,cetruetab,
1150     &                     nbdim,Agrif_Procrank,
1151     &                     member)
1152
1153      ENDIF
1154
1155
1156         Call Agrif_Prtbounds(nbdim,indmin,indmax,s_Parent_temp,
1157     &                     s_Child_temp,s_Child,ds_Child,
1158     &                     s_Parent,ds_Parent,
1159     &                     pttruetab,cetruetab,pttab_Child,
1160     &                     pttab_Parent,
1161     &                     posvartab_Child,TypeUpdate,loctab_Child
1162     &                 ,pttruetabwhole,cetruetabwhole
1163     &     )
1164
1165#else
1166       indmin = indminglob
1167       indmax = indmaxglob
1168       pttruetabwhole = pttruetab
1169       cetruetabwhole = cetruetab
1170       childarray(:,1,2) = pttruetab
1171       childarray(:,2,2) = cetruetab
1172#endif
1173
1174       IF (present(procname)) THEN
1175       IF (.Not.present(nb)) THEN
1176       nbin=0
1177       ndirin=0
1178       ELSE
1179       nbin = nb
1180       ndirin = ndir
1181       ENDIF
1182       ENDIF
1183       
1184      IF (memberin) THEN
1185      IF (.not.associated(tempC%var)) allocate(tempC%var)
1186
1187C
1188      Call Agrif_nbdim_allocation(tempC%var,
1189     &                 pttruetab,cetruetab,nbdim)
1190
1191      Call Agrif_nbdim_Full_VarEQreal(tempC%var,0.,nbdim)
1192
1193
1194
1195      IF (present(procname)) THEN
1196        SELECT CASE (nbdim)
1197        CASE(1)
1198          CALL procname(tempC%var%array1,
1199     &                          childarray(1,1,2),childarray(1,2,2),
1200     &                                   .TRUE.,nbin,ndirin)
1201        CASE(2)
1202          CALL procname(tempC%var%array2,
1203     &                          childarray(1,1,2),childarray(1,2,2),
1204     &                          childarray(2,1,2),childarray(2,2,2),
1205     &                                   .TRUE.,nbin,ndirin)
1206        CASE(3)
1207          CALL procname(tempC%var%array3,
1208     &                          childarray(1,1,2),childarray(1,2,2),
1209     &                          childarray(2,1,2),childarray(2,2,2),
1210     &                          childarray(3,1,2),childarray(3,2,2),
1211     &                                   .TRUE.,nbin,ndirin)
1212        CASE(4)
1213          CALL procname(tempC%var%array4,
1214     &                          childarray(1,1,2),childarray(1,2,2),
1215     &                          childarray(2,1,2),childarray(2,2,2),
1216     &                          childarray(3,1,2),childarray(3,2,2),
1217     &                          childarray(4,1,2),childarray(4,2,2),
1218     &                                   .TRUE.,nbin,ndirin)
1219        CASE(5)
1220          CALL procname(tempC%var%array5,
1221     &                          childarray(1,1,2),childarray(1,2,2),
1222     &                          childarray(2,1,2),childarray(2,2,2),
1223     &                          childarray(3,1,2),childarray(3,2,2),
1224     &                          childarray(4,1,2),childarray(4,2,2),
1225     &                          childarray(5,1,2),childarray(5,2,2),
1226     &                                   .TRUE.,nbin,ndirin)
1227        CASE(6)
1228          CALL procname(tempC%var%array6,
1229     &                          childarray(1,1,2),childarray(1,2,2),
1230     &                          childarray(2,1,2),childarray(2,2,2),
1231     &                          childarray(3,1,2),childarray(3,2,2),
1232     &                          childarray(4,1,2),childarray(4,2,2),
1233     &                          childarray(5,1,2),childarray(5,2,2),
1234     &                          childarray(6,1,2),childarray(6,2,2),
1235     &                                   .TRUE.,nbin,ndirin)
1236        END SELECT
1237      ELSE
1238      Call Agrif_nbdim_VarEQvar(tempC%var,pttruetab,cetruetab,
1239     &          child%var,childarray(:,1,2),childarray(:,2,2),
1240     &                          nbdim)
1241      ENDIF
1242
1243      ENDIF
1244
1245
1246
1247C
1248C
1249#ifdef AGRIF_MPI
1250C
1251C     tab2 contains the necessary limits of the parent grid for each processor
1252
1253      IF (Associated(child%var%list_update)) THEN
1254      Call Agrif_Find_list_update(child%var%list_update,pttab,petab,
1255     &                          pttab_Child,pttab_Parent,nbdim,
1256     &       find_list_update,tab4t,tab5t,memberinall,memberinall2,
1257     &     sendtoproc1,recvfromproc1,sendtoproc2,recvfromproc2)
1258      ELSE
1259      find_list_update = .FALSE.
1260      ENDIF
1261     
1262      if (.not.find_list_update) then     
1263      tab3(:,1) = pttruetab(:)
1264      tab3(:,2) = cetruetab(:)
1265      tab3(:,3) = pttruetabwhole(:)
1266      tab3(:,4) = cetruetabwhole(:)
1267C
1268C
1269      Call MPI_ALLGATHER(tab3,4*nbdim,MPI_INTEGER,tab4,4*nbdim,
1270     &                   MPI_INTEGER,MPI_COMM_WORLD,code)
1271
1272      IF (.not.associated(tempCextend%var)) Allocate(tempCextend%var)
1273      DO k=0,Agrif_Nbprocs-1
1274       do j=1,4
1275         do i=1,nbdim
1276         tab4t(i,k,j) = tab4(i,j,k)
1277         enddo
1278       enddo
1279      enddo
1280     
1281      memberin1(1) = memberin
1282      CALL MPI_ALLGATHER(memberin1,1,MPI_LOGICAL,memberinall,
1283     &                  1,MPI_LOGICAL,MPI_COMM_WORLD,code)
1284 
1285       Call Get_External_Data_first(tab4t(:,:,1),
1286     &            tab4t(:,:,2),
1287     &            tab4t(:,:,3),tab4t(:,:,4),nbdim,memberin,memberin,
1288     &            memberinall,sendtoproc1,recvfromproc1,tab4t(:,:,5),
1289     & tab4t(:,:,6),tab4t(:,:,7),tab4t(:,:,8))
1290               
1291      endif
1292
1293      Call ExchangeSameLevel2(sendtoproc1,recvfromproc1,nbdim,
1294     &            tab4t(:,:,3),tab4t(:,:,4),tab4t(:,:,5),tab4t(:,:,6),
1295     &            tab4t(:,:,7),tab4t(:,:,8),memberin,tempC,
1296     &            tempCextend) 
1297           
1298!      Call Get_External_Data(tempC,tempCextend,tab4t(:,:,1),
1299!     &            tab4t(:,:,2),
1300!     &            tab4t(:,:,3),tab4t(:,:,4),nbdim,memberin,memberin,
1301!     &            memberinall)
1302
1303#else
1304      tempCextend%var => tempC%var
1305#endif
1306
1307C
1308C
1309C     Update of the parent grid (tempP) from the child grid (tempC)
1310
1311
1312      IF (memberin) THEN
1313
1314      IF (.not.associated(tempP%var)) allocate(tempP%var)
1315      Call Agrif_nbdim_allocation(tempP%var,
1316     &                 indmin,indmax,nbdim)
1317
1318      if ( nbdim .EQ. 1 ) then
1319         tempP%var%array1 = 0.
1320         Call Agrif_Update_1D_recursive(TypeUpdate,
1321     &           tempP%var%array1,tempCextend%var%array1,
1322     &           indmin,indmax,
1323     &           pttruetabwhole,cetruetabwhole,
1324     &           s_Child_temp,s_Parent_temp,
1325     &           ds_Child,ds_Parent,nbdim)
1326      endif
1327      if ( nbdim .EQ. 2 ) then
1328         Call Agrif_Update_2D_recursive(TypeUpdate,
1329     &           tempP%var%array2,tempCextend%var%array2,
1330     &           indmin,indmax,
1331     &           pttruetabwhole,cetruetabwhole,
1332     &           s_Child_temp,s_Parent_temp,
1333     &           ds_Child,ds_Parent,nbdim)
1334      endif
1335
1336      if ( nbdim .EQ. 3 ) then
1337         Call Agrif_Update_3D_recursive(TypeUpdate,
1338     &           tempP%var%array3,tempCextend%var%array3,
1339     &           indmin,indmax,
1340     &           pttruetabwhole,cetruetabwhole,
1341     &           s_Child_temp,s_Parent_temp,
1342     &           ds_Child,ds_Parent,nbdim)
1343      endif
1344      if ( nbdim .EQ. 4 ) then
1345         Call Agrif_Update_4D_recursive(TypeUpdate,
1346     &           tempP%var%array4,tempCextend%var%array4,
1347     &           indmin,indmax,
1348     &           pttruetabwhole,cetruetabwhole,
1349     &           s_Child_temp,s_Parent_temp,
1350     &           ds_Child,ds_Parent,nbdim)
1351      endif
1352      if ( nbdim .EQ. 5 ) then
1353         Call Agrif_Update_5D_recursive(TypeUpdate,
1354     &           tempP%var%array5,tempCextend%var%array5,
1355     &           indmin,indmax,
1356     &           pttruetabwhole,cetruetabwhole,
1357     &           s_Child_temp,s_Parent_temp,
1358     &           ds_Child,ds_Parent,nbdim)
1359      endif
1360      if ( nbdim .EQ. 6 ) then
1361         Call Agrif_Update_6D_recursive(TypeUpdate,
1362     &           tempP%var%array6,tempCextend%var%array6,
1363     &           indmin,indmax,
1364     &           pttruetabwhole,cetruetabwhole,
1365     &           s_Child_temp,s_Parent_temp,
1366     &           ds_Child,ds_Parent,nbdim)
1367      endif
1368
1369      Call Agrif_nbdim_deallocation(tempCextend%var,nbdim)
1370C      Deallocate(tempCextend%var)
1371
1372      ENDIF
1373
1374#ifdef AGRIF_MPI
1375      Call Agrif_nbdim_Get_bound_dimension(parent%var,
1376     &                              lowerbound,upperbound,nbdim)
1377
1378      Call Agrif_ChildGrid_to_ParentGrid()
1379C
1380      Call Agrif_Childbounds(nbdim,
1381     &                       lowerbound,upperbound,
1382     &                       indminglob,indmaxglob,
1383     &                       indminglob2,indmaxglob2,member)
1384C
1385      IF (member) THEN
1386      Call Agrif_GlobtoLocInd2(parentarray,
1387     &                     lowerbound,upperbound,
1388     &                     indminglob2,indmaxglob2,
1389     &                     nbdim,Agrif_Procrank,
1390     &                     member)
1391      ENDIF
1392
1393      Call Agrif_ParentGrid_to_ChildGrid()
1394
1395      if (.not.find_list_update) then
1396      tab3(:,1) = indmin(:)
1397      tab3(:,2) = indmax(:)
1398      tab3(:,3) = indminglob2(:)
1399      tab3(:,4) = indmaxglob2(:)
1400C
1401      Call MPI_ALLGATHER(tab3,4*nbdim,MPI_INTEGER,tab4,4*nbdim,
1402     &                   MPI_INTEGER,MPI_COMM_WORLD,code)
1403
1404      IF (.not.associated(tempPextend%var)) Allocate(tempPextend%var)
1405      DO k=0,Agrif_Nbprocs-1
1406       do j=1,4
1407         do i=1,nbdim
1408         tab5t(i,k,j) = tab4(i,j,k)
1409         enddo
1410       enddo
1411      enddo
1412 
1413      memberin1(1) = member
1414      CALL MPI_ALLGATHER(memberin1,1,MPI_LOGICAL,memberinall2,
1415     &                  1,MPI_LOGICAL,MPI_COMM_WORLD,code)
1416 
1417      Call Get_External_Data_first(tab5t(:,:,1),
1418     &            tab5t(:,:,2),
1419     &            tab5t(:,:,3),tab5t(:,:,4),nbdim,memberin,member,
1420     &            memberinall2,sendtoproc2,recvfromproc2,tab5t(:,:,5),
1421     & tab5t(:,:,6),tab5t(:,:,7),tab5t(:,:,8))
1422         
1423      Call Agrif_Addto_list_update(child%var%list_update,pttab,petab,
1424     &                          pttab_Child,pttab_Parent,nbdim
1425     &   ,tab4t,tab5t,memberinall,memberinall2,
1426     &   sendtoproc1,recvfromproc1,sendtoproc2,recvfromproc2) 
1427         
1428      endif
1429     
1430c      Call Get_External_Data(tempP,tempPextend,tab5t(:,:,1),
1431c     &            tab5t(:,:,2),
1432c     &            tab5t(:,:,3),tab5t(:,:,4),nbdim,memberin,member,
1433c     &            memberinall2)
1434     
1435      Call ExchangeSameLevel2(sendtoproc2,recvfromproc2,nbdim,
1436     &            tab5t(:,:,3),tab5t(:,:,4),tab5t(:,:,5),tab5t(:,:,6),
1437     &            tab5t(:,:,7),tab5t(:,:,8),member,tempP,
1438     &            tempPextend)     
1439
1440#else
1441      tempPextend%var => tempP%var
1442      parentarray(:,1,1) = indmin
1443      parentarray(:,2,1) = indmax
1444      parentarray(:,1,2) = indmin
1445      parentarray(:,2,2) = indmax
1446      member = .TRUE.
1447#endif
1448
1449C
1450C
1451C
1452C     Special values on the child grid
1453      if (Agrif_UseSpecialValueFineGrid) then
1454C
1455ccc         noraftab(1:nbdim) =
1456ccc     &    child % var % root_var % interptab(1:nbdim) .EQ. 'N'
1457C
1458#ifdef AGRIF_MPI
1459C
1460c          Allocate(childvalues% var)
1461C
1462c          Call Agrif_nbdim_allocation(childvalues%var,
1463c     &                      pttruetab,cetruetab,nbdim)
1464c          Call Agrif_nbdim_Full_VarEQvar(childvalues% var,
1465c     &                                tempC% var,
1466c     &                                nbdim)
1467c          Call Agrif_CheckMasknD(tempC,childvalues,
1468c     &                           pttruetab(1:nbdim),cetruetab(1:nbdim),
1469c     &                           pttruetab(1:nbdim),cetruetab(1:nbdim),
1470c     &                           noraftab(1:nbdim),nbdim)
1471c          Call Agrif_nbdim_deallocation(childvalues% var,nbdim)
1472c         Deallocate(childvalues % var)
1473C
1474#else
1475C
1476c          Call Agrif_nbdim_Get_bound_dimension(child%var,
1477c     &                              lowerbound,upperbound,nbdim)
1478c          Call Agrif_CheckMasknD(tempC,child,
1479c     &                           pttruetab(1:nbdim),cetruetab(1:nbdim),
1480c     &                           lowerbound,
1481c     &                           upperbound,
1482c     &                           noraftab(1:nbdim),nbdim)
1483C
1484#endif
1485C
1486      endif
1487
1488
1489C
1490C
1491C
1492C
1493C     Special values on the parent grid
1494      if (Agrif_UseSpecialValue) then
1495C
1496#ifdef AGRIF_MPI
1497C
1498c          Call GiveAgrif_SpecialValueToTab_mpi(parent%var,tempP%var,
1499c     &                 parentarray,
1500c     &                 indmin,indmax,
1501c     &                 Agrif_SpecialValue,nbdim)
1502C
1503C
1504#else
1505C
1506c          Call GiveAgrif_SpecialValueToTab(parent%var,tempP%var,
1507c     &                  indmin,indmax,
1508c     &                  Agrif_SpecialValue,nbdim)
1509C
1510#endif
1511C
1512C
1513      endif   
1514C
1515C
1516        IF (member) THEN
1517
1518          IF (present(procname)) THEN
1519            CALL Agrif_ChildGrid_to_ParentGrid()
1520            SELECT CASE(nbdim)
1521            CASE(1)
1522            CALL procname(
1523     &      tempPextend%var%array1(
1524     &                      parentarray(1,1,1):parentarray(1,2,1)),
1525     &                      parentarray(1,1,2),parentarray(1,2,2),
1526     &                                   .FALSE.,nbin,ndirin
1527     &                      )
1528            CASE(2)
1529            CALL procname(
1530     &      tempPextend%var%array2(
1531     &                      parentarray(1,1,1):parentarray(1,2,1),
1532     &                      parentarray(2,1,1):parentarray(2,2,1)),
1533     &                      parentarray(1,1,2),parentarray(1,2,2),
1534     &                      parentarray(2,1,2),parentarray(2,2,2),
1535     &                                   .FALSE.,nbin,ndirin
1536     &                      )
1537            CASE(3)
1538            CALL procname(
1539     &      tempPextend%var%array3(
1540     &                      parentarray(1,1,1):parentarray(1,2,1),
1541     &                      parentarray(2,1,1):parentarray(2,2,1),
1542     &                      parentarray(3,1,1):parentarray(3,2,1)),
1543     &                      parentarray(1,1,2),parentarray(1,2,2),
1544     &                      parentarray(2,1,2),parentarray(2,2,2),
1545     &                      parentarray(3,1,2),parentarray(3,2,2),
1546     &                                   .FALSE.,nbin,ndirin
1547     &                      )
1548            CASE(4)
1549            CALL procname(
1550     &      tempPextend%var%array4(
1551     &                      parentarray(1,1,1):parentarray(1,2,1),
1552     &                      parentarray(2,1,1):parentarray(2,2,1),
1553     &                      parentarray(3,1,1):parentarray(3,2,1),
1554     &                      parentarray(4,1,1):parentarray(4,2,1)),
1555     &                      parentarray(1,1,2),parentarray(1,2,2),
1556     &                      parentarray(2,1,2),parentarray(2,2,2),
1557     &                      parentarray(3,1,2),parentarray(3,2,2),
1558     &                      parentarray(4,1,2),parentarray(4,2,2),
1559     &                                   .FALSE.,nbin,ndirin
1560     &                      )
1561            CASE(5)
1562            CALL procname(
1563     &      tempPextend%var%array5(
1564     &                      parentarray(1,1,1):parentarray(1,2,1),
1565     &                      parentarray(2,1,1):parentarray(2,2,1),
1566     &                      parentarray(3,1,1):parentarray(3,2,1),
1567     &                      parentarray(4,1,1):parentarray(4,2,1),
1568     &                      parentarray(5,1,1):parentarray(5,2,1)),
1569     &                      parentarray(1,1,2),parentarray(1,2,2),
1570     &                      parentarray(2,1,2),parentarray(2,2,2),
1571     &                      parentarray(3,1,2),parentarray(3,2,2),
1572     &                      parentarray(4,1,2),parentarray(4,2,2),
1573     &                      parentarray(5,1,2),parentarray(5,2,2),
1574     &                                   .FALSE.,nbin,ndirin
1575     &                      )
1576            CASE(6)
1577            CALL procname(
1578     &      tempPextend%var%array6(
1579     &                      parentarray(1,1,1):parentarray(1,2,1),
1580     &                      parentarray(2,1,1):parentarray(2,2,1),
1581     &                      parentarray(3,1,1):parentarray(3,2,1),
1582     &                      parentarray(4,1,1):parentarray(4,2,1),
1583     &                      parentarray(5,1,1):parentarray(5,2,1),
1584     &                      parentarray(6,1,1):parentarray(6,2,1)),
1585     &                      parentarray(1,1,2),parentarray(1,2,2),
1586     &                      parentarray(2,1,2),parentarray(2,2,2),
1587     &                      parentarray(3,1,2),parentarray(3,2,2),
1588     &                      parentarray(4,1,2),parentarray(4,2,2),
1589     &                      parentarray(5,1,2),parentarray(5,2,2),
1590     &                      parentarray(6,1,2),parentarray(6,2,2),
1591     &                                   .FALSE.,nbin,ndirin
1592     &                      )
1593            END SELECT
1594            CALL Agrif_ParentGrid_to_ChildGrid()
1595          ELSE
1596            SELECT CASE(nbdim)
1597            CASE(1)
1598            parent%var%array1(parentarray(1,1,2):parentarray(1,2,2)) =
1599     &      tempPextend%var%array1(
1600     &                      parentarray(1,1,1):parentarray(1,2,1))
1601            CASE(2)
1602            parent%var%array2(parentarray(1,1,2):parentarray(1,2,2),
1603     &                      parentarray(2,1,2):parentarray(2,2,2)) =
1604     &      tempPextend%var%array2(
1605     &                      parentarray(1,1,1):parentarray(1,2,1),
1606     &                      parentarray(2,1,1):parentarray(2,2,1))
1607            CASE(3)
1608            parent%var%array3(parentarray(1,1,2):parentarray(1,2,2),
1609     &                      parentarray(2,1,2):parentarray(2,2,2),
1610     &                      parentarray(3,1,2):parentarray(3,2,2)) =
1611     &      tempPextend%var%array3(
1612     &                      parentarray(1,1,1):parentarray(1,2,1),
1613     &                      parentarray(2,1,1):parentarray(2,2,1),
1614     &                      parentarray(3,1,1):parentarray(3,2,1))
1615            CASE(4)
1616            parent%var%array4(parentarray(1,1,2):parentarray(1,2,2),
1617     &                      parentarray(2,1,2):parentarray(2,2,2),
1618     &                      parentarray(3,1,2):parentarray(3,2,2),
1619     &                      parentarray(4,1,2):parentarray(4,2,2)) =
1620     &      tempPextend%var%array4(
1621     &                      parentarray(1,1,1):parentarray(1,2,1),
1622     &                      parentarray(2,1,1):parentarray(2,2,1),
1623     &                      parentarray(3,1,1):parentarray(3,2,1),
1624     &                      parentarray(4,1,1):parentarray(4,2,1))
1625            CASE(5)
1626            parent%var%array5(parentarray(1,1,2):parentarray(1,2,2),
1627     &                      parentarray(2,1,2):parentarray(2,2,2),
1628     &                      parentarray(3,1,2):parentarray(3,2,2),
1629     &                      parentarray(4,1,2):parentarray(4,2,2),
1630     &                      parentarray(5,1,2):parentarray(5,2,2)) =
1631     &      tempPextend%var%array5(
1632     &                      parentarray(1,1,1):parentarray(1,2,1),
1633     &                      parentarray(2,1,1):parentarray(2,2,1),
1634     &                      parentarray(3,1,1):parentarray(3,2,1),
1635     &                      parentarray(4,1,1):parentarray(4,2,1),
1636     &                      parentarray(5,1,1):parentarray(5,2,1))
1637            CASE(6)
1638            parent%var%array6(parentarray(1,1,2):parentarray(1,2,2),
1639     &                      parentarray(2,1,2):parentarray(2,2,2),
1640     &                      parentarray(3,1,2):parentarray(3,2,2),
1641     &                      parentarray(4,1,2):parentarray(4,2,2),
1642     &                      parentarray(5,1,2):parentarray(5,2,2),
1643     &                      parentarray(6,1,2):parentarray(6,2,2)) =
1644     &      tempPextend%var%array6(
1645     &                      parentarray(1,1,1):parentarray(1,2,1),
1646     &                      parentarray(2,1,1):parentarray(2,2,1),
1647     &                      parentarray(3,1,1):parentarray(3,2,1),
1648     &                      parentarray(4,1,1):parentarray(4,2,1),
1649     &                      parentarray(5,1,1):parentarray(5,2,1),
1650     &                      parentarray(6,1,1):parentarray(6,2,1))
1651            END SELECT
1652          ENDIF
1653
1654        Call Agrif_nbdim_deallocation(tempPextend%var,nbdim)
1655       ENDIF
1656C
1657C
1658C     Deallocations
1659
1660      IF (memberin) THEN
1661#ifdef AGRIF_MPI
1662      Call Agrif_nbdim_deallocation(tempP%var,nbdim)
1663      Call Agrif_nbdim_deallocation(tempC%var,nbdim)
1664!      Deallocate(tempC % var)
1665#endif
1666!      Deallocate(tempP % var)
1667      ENDIF
1668#ifdef AGRIF_MPI
1669!      Deallocate(tempPextend%var)
1670!      IF (.Not.memberin) Deallocate(tempCextend%var)
1671#endif
1672
1673C
1674C
1675      End Subroutine Agrif_UpdatenD
1676C
1677C
1678C     **************************************************************************
1679CCC   Subroutine Agrif_Prtbounds
1680C     **************************************************************************
1681C
1682      Subroutine Agrif_Prtbounds(nbdim,indmin,indmax,s_Parent_temp,
1683     &                              s_Child_temp,s_Child,ds_Child,
1684     &                              s_Parent,ds_Parent,
1685     &                              pttruetab,cetruetab,pttab_Child,
1686     &                              pttab_Parent,
1687     &                              posvartab_child,TypeUpdate,
1688     &                              loctab_Child
1689#ifdef AGRIF_MPI
1690     &                 ,pttruetabwhole,cetruetabwhole
1691#endif
1692     &                 )
1693C
1694CCC   Description:
1695CCC   Subroutine calculating the bounds of the parent grid to be updated
1696CCC   by the child grid     
1697C
1698C
1699C     Declarations:
1700C
1701     
1702C
1703#ifdef AGRIF_MPI
1704cccccccccccccccccccccccccc#include "mpif.h"
1705#endif
1706C
1707C     Arguments
1708      INTEGER :: nbdim
1709      INTEGER,DIMENSION(nbdim) :: indmin,indmax
1710      REAL,DIMENSION(nbdim) :: s_Parent_temp,s_child_temp
1711      REAL,DIMENSION(nbdim) :: s_Child,ds_child
1712      REAL,DIMENSION(nbdim) :: s_Parent,ds_Parent
1713      INTEGER,DIMENSION(nbdim) :: pttruetab,cetruetab
1714      INTEGER,DIMENSION(nbdim) :: posvartab_child,TypeUpdate
1715      INTEGER,DIMENSION(nbdim) :: loctab_Child
1716      INTEGER,DIMENSION(nbdim) :: pttab_Child,pttab_Parent
1717C
1718C     Local variables
1719      INTEGER :: i
1720      REAL,DIMENSION(nbdim) :: dim_newmin,dim_newmax     
1721#ifdef AGRIF_MPI
1722      INTEGER,DIMENSION(nbdim)    :: pttruetabwhole,cetruetabwhole
1723      REAL :: positionmin,positionmax
1724      INTEGER :: imin,imax
1725#endif
1726C
1727C
1728      do i = 1,nbdim
1729C
1730        dim_newmin(i) = s_Child(i) + (pttruetab(i) -
1731     &                                pttab_Child(i)) * ds_Child(i)
1732C
1733        dim_newmax(i) = s_Child(i) + (cetruetab(i) - 
1734     &                                pttab_Child(i)) * ds_Child(i)
1735C     
1736        indmin(i) = pttab_Parent(i) +
1737     &        agrif_ceiling((dim_newmin(i)-s_Parent(i))/ds_Parent(i))
1738C
1739        indmax(i) = pttab_Parent(i) +
1740     &        agrif_int((dim_newmax(i)-s_Parent(i))/ds_Parent(i))
1741C
1742#ifdef AGRIF_MPI
1743        positionmin = s_Parent(i) + (indmin(i)-
1744     &                          pttab_Parent(i))*ds_Parent(i)
1745        IF (loctab_Child(i) .NE. -3) THEN
1746        IF (posvartab_child(i) == 1) THEN
1747          IF (TypeUpdate(i) .EQ. Agrif_Update_Average) THEN
1748          positionmin = positionmin - ds_Parent(i)/2.
1749          ELSE IF (TypeUpdate(i) .EQ. Agrif_Update_Full_Weighting) THEN
1750          positionmin = positionmin - (ds_Parent(i)-ds_Child(i))
1751          ENDIF
1752        ELSE
1753        positionmin = positionmin - ds_Parent(i)/2.
1754        IF (TypeUpdate(i) .EQ. Agrif_Update_Full_Weighting) THEN
1755          positionmin = positionmin - ds_Child(i)
1756        ENDIF
1757        ENDIF
1758        ENDIF
1759        imin = pttab_Child(i) +
1760     &        agrif_ceiling((positionmin-s_Child(i))/ds_Child(i))
1761
1762       positionmin = s_Child(i) + (imin -
1763     &                                pttab_Child(i)) * ds_Child(i)
1764
1765        pttruetabwhole(i) = imin
1766
1767        positionmax = s_Parent(i) + (indmax(i)-
1768     &                          pttab_Parent(i))*ds_Parent(i)
1769        IF (loctab_Child(i) .NE. -3) THEN
1770        IF (posvartab_child(i) == 1) THEN
1771          IF (TypeUpdate(i) .EQ. Agrif_Update_Average) THEN
1772        positionmax = positionmax  + ds_Parent(i)/2.
1773          ELSE IF (TypeUpdate(i) .EQ. Agrif_Update_Full_Weighting) THEN
1774          positionmax = positionmax  + (ds_Parent(i)-ds_Child(i))
1775          ENDIF
1776        ELSE
1777        positionmax = positionmax  + ds_Parent(i)/2.
1778        IF (TypeUpdate(i) .EQ. Agrif_Update_Full_Weighting) THEN
1779          positionmax = positionmax + ds_Child(i)
1780        ENDIF       
1781        ENDIF
1782        ENDIF
1783        imax = pttab_Child(i) +
1784     &        agrif_int((positionmax-s_Child(i))/ds_Child(i))
1785
1786        positionmax = s_Child(i) + (imax -
1787     &                                pttab_Child(i)) * ds_Child(i)
1788
1789        cetruetabwhole(i) = imax
1790
1791#endif
1792C
1793        s_Parent_temp(i) = s_Parent(i) + 
1794     &                     (indmin(i) - pttab_Parent(i)) * 
1795     &                      ds_Parent(i) 
1796C     
1797        s_Child_temp(i) = dim_newmin(i)
1798
1799#ifdef AGRIF_MPI
1800        s_Child_temp(i) = positionmin
1801#endif
1802C
1803      enddo
1804C
1805      Return
1806C
1807C
1808      End Subroutine Agrif_Prtbounds
1809C
1810C
1811C
1812C
1813
1814C
1815C
1816C
1817C     ************************************************************************** 
1818CCC   Subroutine Agrif_Update_2D_Recursive 
1819C     **************************************************************************
1820C
1821      Subroutine Agrif_Update_2D_recursive(TypeUpdate,tempP,tempC,
1822     &                                     indmin,indmax,
1823     &                                     pttab_child,petab_child,
1824     &                                     s_child,s_parent,
1825     &                                     ds_child,ds_parent,nbdim)
1826C
1827CCC   Description:
1828CCC   Subroutine to update a 2D grid variable on the parent grid. 
1829CCC   It calls Agrif_Update_1D_Recursive and Agrif_UpdateBase.   
1830C
1831CC    Method:
1832C
1833C     Declarations:
1834C
1835     
1836C     
1837      INTEGER                   :: nbdim
1838      INTEGER, DIMENSION(nbdim) :: TypeUpdate            ! TYPE of update (copy or average)
1839      INTEGER, DIMENSION(nbdim) :: indmin,indmax
1840      INTEGER, DIMENSION(nbdim) :: pttab_child,petab_child
1841      REAL, DIMENSION(nbdim)    :: s_child,s_parent
1842      REAL, DIMENSION(nbdim)    :: ds_child,ds_parent
1843      REAL, DIMENSION(indmin(1):indmax(1),
1844     &                indmin(2):indmax(2))           :: tempP
1845C      REAL, DIMENSION(pttab_child(1):petab_child(1),
1846C     &                pttab_child(2):petab_child(2)) :: tempC
1847     
1848      REAL, DIMENSION(:,:) :: tempC     
1849C
1850C     Local variables     
1851      REAL, DIMENSION(indmin(1):indmax(1),
1852     &                 pttab_child(2):petab_child(2)) :: tabtemp
1853      REAL, DIMENSION(indmin(2):indmax(2),
1854     &                indmin(1):indmax(1))           :: tempP_trsp
1855      REAL, DIMENSION(pttab_child(2):petab_child(2),
1856     &                 indmin(1):indmax(1)) :: tabtemp_trsp
1857      INTEGER :: i,j
1858      INTEGER :: coeffraf,locind_child_left
1859C
1860      tabtemp = 0.
1861
1862
1863      coeffraf = nint ( ds_parent(1) / ds_child(1) ) 
1864      IF((TypeUpdate(1) == AGRIF_Update_average) 
1865     &                 .AND. (coeffraf /= 1 ))THEN
1866!---CDIR NEXPAND
1867         IF(.NOT. precomputedone(1) ) Call average1Dprecompute2D
1868     &       (petab_child(2)-pttab_child(2)+1,
1869     &        indmax(1)-indmin(1)+1,
1870     &        petab_child(1)-pttab_child(1)+1,
1871     &        s_parent(1),s_child(1),ds_parent(1),ds_child(1),1)
1872!---CDIR NEXPAND
1873          Call average1Daftercompute
1874     &       (  tabtemp, tempC,
1875     &          size(tabtemp), size(tempC),
1876     &          s_parent(1),s_child(1),ds_parent(1),ds_child(1),1)
1877 
1878      ELSEIF((TypeUpdate(1) == AGRIF_Update_copy)
1879     &                 .AND. (coeffraf /= 1 ))THEN
1880!---CDIR NEXPAND
1881         IF(.NOT. precomputedone(1) ) Call copy1Dprecompute2D
1882     &       (petab_child(2)-pttab_child(2)+1,
1883     &        indmax(1)-indmin(1)+1,
1884     &        petab_child(1)-pttab_child(1)+1,
1885     &        s_parent(1),s_child(1),ds_parent(1),ds_child(1),1)
1886!---CDIR NEXPAND
1887          Call copy1Daftercompute
1888     &       (  tabtemp, tempC,
1889     &          size(tabtemp), size(tempC),
1890     &          s_parent(1),s_child(1),ds_parent(1),ds_child(1),1)
1891
1892      ELSE
1893      do j = pttab_child(nbdim),petab_child(nbdim)
1894C
1895!---CDIR NEXPAND
1896        Call Agrif_Update_1D_recursive(TypeUpdate(1:nbdim-1),   
1897     &         tabtemp(:,j),
1898     &         tempC(:,j-pttab_child(nbdim)+1),
1899     &         indmin(1:nbdim-1),indmax(1:nbdim-1),
1900     &         pttab_child(1:nbdim-1),petab_child(1:nbdim-1),
1901     &         s_child(1:nbdim-1),s_parent(1:nbdim-1),
1902     &         ds_child(1:nbdim-1),ds_parent(1:nbdim-1),nbdim-1)
1903C
1904      enddo
1905      ENDIF
1906      tabtemp_trsp = TRANSPOSE(tabtemp) 
1907      coeffraf = nint(ds_parent(nbdim)/ds_child(nbdim))
1908   
1909!---CDIR NEXPAND
1910      Call Agrif_Compute_nbdim_update(s_parent(nbdim),s_child(nbdim),
1911     &  ds_parent(nbdim),ds_child(nbdim),coeffraf,locind_child_left)
1912C
1913     
1914      tempP_trsp = 0. 
1915
1916      IF((TypeUpdate(2) == AGRIF_Update_average)
1917     &                .AND. (coeffraf /= 1 ))THEN
1918!---CDIR NEXPAND
1919         IF(.NOT. precomputedone(2) ) Call average1Dprecompute2D
1920     &      ( indmax(1)-indmin(1)+1,
1921     &        indmax(2)-indmin(2)+1,
1922     &        petab_child(2)-pttab_child(2)+1,
1923     &        s_parent(2),s_child(2),ds_parent(2),ds_child(2),2)
1924!---CDIR NEXPAND
1925          Call average1Daftercompute
1926     &       (  tempP_trsp, tabtemp_trsp,
1927     &          size(tempP_trsp), size(tabtemp_trsp),
1928     &          s_parent(2),s_child(2),ds_parent(2),ds_child(2),2)
1929
1930      ELSEIF((TypeUpdate(2) == AGRIF_Update_copy)
1931     &                .AND. (coeffraf /= 1 ))THEN
1932!---CDIR NEXPAND
1933         IF(.NOT. precomputedone(2) ) Call copy1Dprecompute2D
1934     &      ( indmax(1)-indmin(1)+1,
1935     &        indmax(2)-indmin(2)+1,
1936     &        petab_child(2)-pttab_child(2)+1,
1937     &        s_parent(2),s_child(2),ds_parent(2),ds_child(2),2)
1938!---CDIR NEXPAND
1939          Call copy1Daftercompute
1940     &       (  tempP_trsp, tabtemp_trsp,
1941     &          size(tempP_trsp), size(tabtemp_trsp),
1942     &          s_parent(2),s_child(2),ds_parent(2),ds_child(2),2)
1943
1944      ELSE
1945
1946      do i = indmin(1),indmax(1)
1947C
1948!---CDIR NEXPAND
1949        Call Agrif_UpdateBase(TypeUpdate(2),
1950     &           tempP_trsp(indmin(nbdim):indmax(nbdim),i),
1951     &          tabtemp_trsp(pttab_child(nbdim):petab_child(nbdim),i),
1952     &           indmin(nbdim),indmax(nbdim),
1953     &           pttab_child(nbdim),petab_child(nbdim),
1954     &           s_parent(nbdim),s_child(nbdim),
1955     &           ds_parent(nbdim),ds_child(nbdim),
1956     &                  coeffraf,locind_child_left)
1957C       
1958      enddo
1959     
1960      ENDIF
1961
1962      tempP = TRANSPOSE(tempP_trsp)
1963C
1964      Return
1965C
1966C
1967      End Subroutine Agrif_Update_2D_recursive
1968C
1969C
1970C
1971C     ************************************************************************** 
1972CCC   Subroutine Agrif_Update_3D_Recursive 
1973C     **************************************************************************
1974C
1975      Subroutine Agrif_Update_3D_recursive(TypeUpdate,tempP,tempC,
1976     &                                     indmin,indmax,   
1977     &                                     pttab_child,petab_child,
1978     &                                     s_child,s_parent,
1979     &                                     ds_child,ds_parent,nbdim)
1980C
1981CCC   Description:
1982CCC   Subroutine to update a 3D grid variable on the parent grid. 
1983CCC   It calls Agrif_Update_2D_Recursive and Agrif_UpdateBase.   
1984C
1985CC    Method:
1986C
1987C     Declarations:
1988C
1989     
1990C     
1991      INTEGER                   :: nbdim
1992      INTEGER, DIMENSION(nbdim) :: TypeUpdate            ! TYPE of update (copy or average)
1993      INTEGER, DIMENSION(nbdim) :: indmin,indmax
1994      INTEGER, DIMENSION(nbdim) :: pttab_child,petab_child
1995      REAL, DIMENSION(nbdim)    :: s_child,s_parent
1996      REAL, DIMENSION(nbdim)    :: ds_child,ds_parent
1997      REAL, DIMENSION(indmin(1):indmax(1),
1998     &                indmin(2):indmax(2),
1999     &                indmin(3):indmax(3))           :: tempP
2000      REAL, DIMENSION(pttab_child(1):petab_child(1),
2001     &                pttab_child(2):petab_child(2),
2002     &                pttab_child(3):petab_child(3)) :: tempC
2003C
2004C     Local variables     
2005      REAL, DIMENSION(indmin(1):indmax(1),
2006     &                 indmin(2):indmax(2), 
2007     &                 pttab_child(3):petab_child(3)) :: tabtemp
2008      INTEGER :: i,j,k
2009      INTEGER :: coeffraf,locind_child_left
2010      INTEGER :: kdeb
2011C
2012C
2013      coeffraf = nint ( ds_parent(1) / ds_child(1) )
2014      IF((TypeUpdate(1) == AGRIF_Update_average)
2015     &                 .AND. (coeffraf /= 1 ))THEN
2016!---CDIR NEXPAND
2017         Call average1Dprecompute2D
2018     &       (petab_child(2)-pttab_child(2)+1,
2019     &        indmax(1)-indmin(1)+1,
2020     &        petab_child(1)-pttab_child(1)+1,
2021     &        s_parent(1),s_child(1),ds_parent(1),ds_child(1),1)
2022      precomputedone(1) = .TRUE.
2023      ELSEIF((TypeUpdate(1) == AGRIF_Update_copy)
2024     &                 .AND. (coeffraf /= 1 ))THEN
2025!---CDIR NEXPAND
2026         Call copy1Dprecompute2D
2027     &       (petab_child(2)-pttab_child(2)+1,
2028     &        indmax(1)-indmin(1)+1,
2029     &        petab_child(1)-pttab_child(1)+1,
2030     &        s_parent(1),s_child(1),ds_parent(1),ds_child(1),1)
2031      precomputedone(1) = .TRUE.
2032      ENDIF
2033
2034      coeffraf = nint ( ds_parent(2) / ds_child(2) )
2035      IF((TypeUpdate(2) == AGRIF_Update_average)
2036     &                .AND. (coeffraf /= 1 ))THEN
2037!---CDIR NEXPAND
2038         Call average1Dprecompute2D
2039     &      ( indmax(1)-indmin(1)+1,
2040     &        indmax(2)-indmin(2)+1,
2041     &        petab_child(2)-pttab_child(2)+1,
2042     &        s_parent(2),s_child(2),ds_parent(2),ds_child(2),2)
2043      precomputedone(2) = .TRUE.
2044      ELSEIF((TypeUpdate(2) == AGRIF_Update_copy)
2045     &                .AND. (coeffraf /= 1 ))THEN
2046!---CDIR NEXPAND
2047         Call copy1Dprecompute2D
2048     &      ( indmax(1)-indmin(1)+1,
2049     &        indmax(2)-indmin(2)+1,
2050     &        petab_child(2)-pttab_child(2)+1,
2051     &        s_parent(2),s_child(2),ds_parent(2),ds_child(2),2)
2052      precomputedone(2) = .TRUE.
2053      ENDIF
2054
2055
2056      do k = pttab_child(nbdim),petab_child(nbdim)
2057C
2058        Call Agrif_Update_2D_recursive(TypeUpdate(1:nbdim-1),   
2059     &         tabtemp(:,:,k),
2060     &         tempC(:,:,k),
2061     &         indmin(1:nbdim-1),indmax(1:nbdim-1),
2062     &         pttab_child(1:nbdim-1),petab_child(1:nbdim-1),
2063     &         s_child(1:nbdim-1),s_parent(1:nbdim-1),
2064     &         ds_child(1:nbdim-1),ds_parent(1:nbdim-1),nbdim-1)
2065C
2066      enddo
2067C
2068      precomputedone(1) = .FALSE.
2069      precomputedone(2) = .FALSE.
2070      coeffraf = nint ( ds_parent(3) / ds_child(3) )
2071
2072      Call Agrif_Compute_nbdim_update(s_parent(nbdim),s_child(nbdim),
2073     &  ds_parent(nbdim),ds_child(nbdim),coeffraf,locind_child_left)
2074     
2075      IF (coeffraf == 1) THEN
2076     
2077      kdeb = pttab_child(3)+locind_child_left-2
2078      do k=indmin(3),indmax(3)
2079      kdeb = kdeb + 1
2080      do j = indmin(2),indmax(2)
2081        do i = indmin(1),indmax(1)
2082        tempP(i,j,k) = tabtemp(i,j,kdeb)
2083      enddo
2084      enddo
2085      enddo
2086             
2087      ELSE
2088      tempP = 0.
2089C
2090      do j = indmin(2),indmax(2)
2091C
2092        do i = indmin(1),indmax(1)
2093C
2094          Call Agrif_UpdateBase(TypeUpdate(3),
2095     &           tempP(i,j,:),
2096     &          tabtemp(i,j,:),
2097     &           indmin(nbdim),indmax(nbdim),
2098     &           pttab_child(nbdim),petab_child(nbdim),
2099     &           s_parent(nbdim),s_child(nbdim),
2100     &           ds_parent(nbdim),ds_child(nbdim),
2101     &                  coeffraf,locind_child_left)
2102C
2103        enddo 
2104C       
2105      enddo
2106      ENDIF
2107C
2108      Return
2109C
2110C
2111      End Subroutine Agrif_Update_3D_recursive
2112C
2113C
2114C
2115C     ************************************************************************** 
2116CCC   Subroutine Agrif_Update_4D_Recursive 
2117C     **************************************************************************
2118C
2119      Subroutine Agrif_Update_4D_recursive(TypeUpdate,tempP,tempC,
2120     &                                     indmin,indmax,   
2121     &                                     pttab_child,petab_child,
2122     &                                     s_child,s_parent,
2123     &                                     ds_child,ds_parent,nbdim)
2124C
2125CCC   Description:
2126CCC   Subroutine to update a 4D grid variable on the parent grid. 
2127CCC   It calls Agrif_Update_3D_Recursive and Agrif_UpdateBase.   
2128C
2129CC    Method:
2130C
2131C     Declarations:
2132C
2133     
2134C     
2135      INTEGER                   :: nbdim
2136      INTEGER, DIMENSION(nbdim) :: TypeUpdate            ! TYPE of update (copy or average)
2137      INTEGER, DIMENSION(nbdim) :: indmin,indmax
2138      INTEGER, DIMENSION(nbdim) :: pttab_child,petab_child
2139      REAL, DIMENSION(nbdim)    :: s_child,s_parent
2140      REAL, DIMENSION(nbdim)    :: ds_child,ds_parent
2141      REAL, DIMENSION(indmin(1):indmax(1),
2142     &                indmin(2):indmax(2),
2143     &                indmin(3):indmax(3),
2144     &                indmin(4):indmax(4))           :: tempP
2145      REAL, DIMENSION(pttab_child(1):petab_child(1),
2146     &                pttab_child(2):petab_child(2),
2147     &                pttab_child(3):petab_child(3),
2148     &                pttab_child(4):petab_child(4)) :: tempC
2149C
2150C     Local variables     
2151      REAL, DIMENSION(:,:,:,:), Allocatable :: tabtemp
2152      INTEGER :: i,j,k,l
2153      INTEGER :: coeffraf,locind_child_left
2154C
2155C
2156      Allocate(tabtemp(indmin(1):indmax(1),
2157     &                 indmin(2):indmax(2),
2158     &                 indmin(3):indmax(3), 
2159     &                 pttab_child(4):petab_child(4)))
2160C
2161      do l = pttab_child(nbdim),petab_child(nbdim)
2162C
2163        Call Agrif_Update_3D_recursive(TypeUpdate(1:nbdim-1),   
2164     &         tabtemp(indmin(nbdim-3):indmax(nbdim-3),
2165     &                 indmin(nbdim-2):indmax(nbdim-2),
2166     &                 indmin(nbdim-1):indmax(nbdim-1),l),
2167     &         tempC(pttab_child(nbdim-3):petab_child(nbdim-3),
2168     &               pttab_child(nbdim-2):petab_child(nbdim-2),
2169     &               pttab_child(nbdim-1):petab_child(nbdim-1),l),
2170     &         indmin(1:nbdim-1),indmax(1:nbdim-1),
2171     &         pttab_child(1:nbdim-1),petab_child(1:nbdim-1),
2172     &         s_child(1:nbdim-1),s_parent(1:nbdim-1),
2173     &         ds_child(1:nbdim-1),ds_parent(1:nbdim-1),nbdim-1)
2174C
2175      enddo
2176     
2177      Call Agrif_Compute_nbdim_update(s_parent(nbdim),s_child(nbdim),
2178     &  ds_parent(nbdim),ds_child(nbdim),coeffraf,locind_child_left)
2179C
2180      tempP = 0.
2181
2182      do k = indmin(3),indmax(3)
2183C
2184        do j = indmin(2),indmax(2)
2185C
2186          do i = indmin(1),indmax(1)
2187C
2188            Call Agrif_UpdateBase(TypeUpdate(4),
2189     &           tempP(i,j,k,indmin(nbdim):indmax(nbdim)),
2190     &          tabtemp(i,j,k,pttab_child(nbdim):petab_child(nbdim)),
2191     &           indmin(nbdim),indmax(nbdim),
2192     &           pttab_child(nbdim),petab_child(nbdim),
2193     &           s_parent(nbdim),s_child(nbdim),
2194     &           ds_parent(nbdim),ds_child(nbdim),
2195     &                  coeffraf,locind_child_left)
2196C
2197          enddo 
2198C
2199        enddo 
2200C       
2201      enddo
2202C
2203      Deallocate(tabtemp)
2204C
2205      Return
2206C
2207C
2208      End Subroutine Agrif_Update_4D_recursive
2209C
2210C
2211C
2212C     ************************************************************************** 
2213CCC   Subroutine Agrif_Update_5D_Recursive 
2214C     **************************************************************************
2215C
2216      Subroutine Agrif_Update_5D_recursive(TypeUpdate,tempP,tempC,
2217     &                                     indmin,indmax,   
2218     &                                     pttab_child,petab_child,
2219     &                                     s_child,s_parent,
2220     &                                     ds_child,ds_parent,nbdim)
2221C
2222CCC   Description:
2223CCC   Subroutine to update a 5D grid variable on the parent grid. 
2224CCC   It calls Agrif_Update_4D_Recursive and Agrif_UpdateBase.   
2225C
2226CC    Method:
2227C
2228C     Declarations:
2229C
2230     
2231C     
2232      INTEGER                   :: nbdim
2233      INTEGER, DIMENSION(nbdim) :: TypeUpdate            ! TYPE of update (copy or average)
2234      INTEGER, DIMENSION(nbdim) :: indmin,indmax
2235      INTEGER, DIMENSION(nbdim) :: pttab_child,petab_child
2236      REAL, DIMENSION(nbdim)    :: s_child,s_parent
2237      REAL, DIMENSION(nbdim)    :: ds_child,ds_parent
2238      REAL, DIMENSION(indmin(1):indmax(1),
2239     &                indmin(2):indmax(2),
2240     &                indmin(3):indmax(3),
2241     &                indmin(4):indmax(4),
2242     &                indmin(5):indmax(5))           :: tempP
2243      REAL, DIMENSION(pttab_child(1):petab_child(1),
2244     &                pttab_child(2):petab_child(2),
2245     &                pttab_child(3):petab_child(3),
2246     &                pttab_child(4):petab_child(4),
2247     &                pttab_child(5):petab_child(5)) :: tempC
2248C
2249C     Local variables     
2250      REAL, DIMENSION(:,:,:,:,:), Allocatable :: tabtemp
2251      INTEGER :: i,j,k,l,m
2252      INTEGER :: coeffraf,locind_child_left
2253C
2254C
2255      Allocate(tabtemp(indmin(1):indmax(1),
2256     &                 indmin(2):indmax(2),
2257     &                 indmin(3):indmax(3),
2258     &                 indmin(4):indmax(4),   
2259     &                 pttab_child(5):petab_child(5)))
2260C
2261      do m = pttab_child(nbdim),petab_child(nbdim)
2262C
2263        Call Agrif_Update_4D_recursive(TypeUpdate(1:nbdim-1),   
2264     &         tabtemp(indmin(nbdim-4):indmax(nbdim-4),
2265     &                 indmin(nbdim-3):indmax(nbdim-3),
2266     &                 indmin(nbdim-2):indmax(nbdim-2),
2267     &                 indmin(nbdim-1):indmax(nbdim-1),m),
2268     &         tempC(pttab_child(nbdim-4):petab_child(nbdim-4),
2269     &               pttab_child(nbdim-3):petab_child(nbdim-3),
2270     &               pttab_child(nbdim-2):petab_child(nbdim-2),
2271     &               pttab_child(nbdim-1):petab_child(nbdim-1),m),
2272     &         indmin(1:nbdim-1),indmax(1:nbdim-1),
2273     &         pttab_child(1:nbdim-1),petab_child(1:nbdim-1),
2274     &         s_child(1:nbdim-1),s_parent(1:nbdim-1),
2275     &         ds_child(1:nbdim-1),ds_parent(1:nbdim-1),nbdim-1)
2276C
2277      enddo
2278     
2279      Call Agrif_Compute_nbdim_update(s_parent(nbdim),s_child(nbdim),
2280     &  ds_parent(nbdim),ds_child(nbdim),coeffraf,locind_child_left)
2281      tempP = 0.
2282C
2283      do l = indmin(4),indmax(4)
2284C
2285        do k = indmin(3),indmax(3)
2286C
2287          do j = indmin(2),indmax(2)
2288C
2289            do i = indmin(1),indmax(1)
2290C
2291              Call Agrif_UpdateBase(TypeUpdate(5),
2292     &           tempP(i,j,k,l,indmin(nbdim):indmax(nbdim)),
2293     &          tabtemp(i,j,k,l,
2294     &                   pttab_child(nbdim):petab_child(nbdim)),
2295     &           indmin(nbdim),indmax(nbdim),
2296     &           pttab_child(nbdim),petab_child(nbdim),
2297     &           s_parent(nbdim),s_child(nbdim),
2298     &           ds_parent(nbdim),ds_child(nbdim),
2299     &                  coeffraf,locind_child_left)
2300C
2301            enddo
2302C
2303          enddo 
2304C
2305        enddo 
2306C       
2307      enddo
2308C
2309      Deallocate(tabtemp)
2310C
2311      Return
2312C
2313C
2314      End Subroutine Agrif_Update_5D_recursive
2315C
2316C
2317C
2318C
2319C     ************************************************************************** 
2320CCC   Subroutine Agrif_Update_6D_Recursive 
2321C     **************************************************************************
2322C
2323      Subroutine Agrif_Update_6D_recursive(TypeUpdate,tempP,tempC,
2324     &                                     indmin,indmax,   
2325     &                                     pttab_child,petab_child,
2326     &                                     s_child,s_parent,
2327     &                                     ds_child,ds_parent,nbdim)
2328C
2329CCC   Description:
2330CCC   Subroutine to update a 6D grid variable on the parent grid. 
2331CCC   It calls Agrif_Update_5D_Recursive and Agrif_UpdateBase.   
2332C
2333CC    Method:
2334C
2335C     Declarations:
2336C
2337     
2338C     
2339      INTEGER                   :: nbdim
2340      INTEGER, DIMENSION(nbdim) :: TypeUpdate            ! TYPE of update (copy or average)
2341      INTEGER, DIMENSION(nbdim) :: indmin,indmax
2342      INTEGER, DIMENSION(nbdim) :: pttab_child,petab_child
2343      REAL, DIMENSION(nbdim)    :: s_child,s_parent
2344      REAL, DIMENSION(nbdim)    :: ds_child,ds_parent
2345      REAL, DIMENSION(indmin(1):indmax(1),
2346     &                indmin(2):indmax(2),
2347     &                indmin(3):indmax(3),
2348     &                indmin(4):indmax(4),
2349     &                indmin(5):indmax(5),
2350     &                indmin(6):indmax(6))           :: tempP
2351      REAL, DIMENSION(pttab_child(1):petab_child(1),
2352     &                pttab_child(2):petab_child(2),
2353     &                pttab_child(3):petab_child(3),
2354     &                pttab_child(4):petab_child(4),
2355     &                pttab_child(5):petab_child(5),
2356     &                pttab_child(6):petab_child(6)) :: tempC
2357C
2358C     Local variables     
2359      REAL, DIMENSION(:,:,:,:,:,:), Allocatable :: tabtemp
2360      INTEGER :: i,j,k,l,m,n
2361      INTEGER :: coeffraf,locind_child_left
2362C
2363C
2364      Allocate(tabtemp(indmin(1):indmax(1),
2365     &                 indmin(2):indmax(2),
2366     &                 indmin(3):indmax(3),
2367     &                 indmin(4):indmax(4),   
2368     &                 indmin(5):indmax(5),   
2369     &                 pttab_child(6):petab_child(6)))
2370C
2371      do n = pttab_child(nbdim),petab_child(nbdim)
2372C
2373        Call Agrif_Update_5D_recursive(TypeUpdate(1:nbdim-1),   
2374     &         tabtemp(indmin(nbdim-5):indmax(nbdim-5),
2375     &                 indmin(nbdim-4):indmax(nbdim-4),
2376     &                 indmin(nbdim-3):indmax(nbdim-3),
2377     &                 indmin(nbdim-2):indmax(nbdim-2),
2378     &                 indmin(nbdim-1):indmax(nbdim-1),n),
2379     &         tempC(pttab_child(nbdim-5):petab_child(nbdim-5),
2380     &               pttab_child(nbdim-4):petab_child(nbdim-4),
2381     &               pttab_child(nbdim-3):petab_child(nbdim-3),
2382     &               pttab_child(nbdim-2):petab_child(nbdim-2),
2383     &               pttab_child(nbdim-1):petab_child(nbdim-1),n),
2384     &         indmin(1:nbdim-1),indmax(1:nbdim-1),
2385     &         pttab_child(1:nbdim-1),petab_child(1:nbdim-1),
2386     &         s_child(1:nbdim-1),s_parent(1:nbdim-1),
2387     &         ds_child(1:nbdim-1),ds_parent(1:nbdim-1),nbdim-1)
2388C
2389      enddo
2390     
2391      Call Agrif_Compute_nbdim_update(s_parent(nbdim),s_child(nbdim),
2392     &  ds_parent(nbdim),ds_child(nbdim),coeffraf,locind_child_left)
2393C
2394      tempP = 0.
2395
2396      do m = indmin(5),indmax(5)
2397      do l = indmin(4),indmax(4)
2398C
2399        do k = indmin(3),indmax(3)
2400C
2401          do j = indmin(2),indmax(2)
2402C
2403            do i = indmin(1),indmax(1)
2404C
2405              Call Agrif_UpdateBase(TypeUpdate(6),
2406     &           tempP(i,j,k,l,m,indmin(nbdim):indmax(nbdim)),
2407     &          tabtemp(i,j,k,l,m,
2408     &                   pttab_child(nbdim):petab_child(nbdim)),
2409     &           indmin(nbdim),indmax(nbdim),
2410     &           pttab_child(nbdim),petab_child(nbdim),
2411     &           s_parent(nbdim),s_child(nbdim),
2412     &           ds_parent(nbdim),ds_child(nbdim),
2413     &                  coeffraf,locind_child_left)
2414C
2415            enddo
2416C
2417          enddo 
2418C
2419        enddo 
2420C       
2421      enddo
2422      enddo
2423C
2424      Deallocate(tabtemp)
2425C
2426      Return
2427C
2428C
2429      End Subroutine Agrif_Update_6D_recursive
2430C
2431C
2432C
2433C     ************************************************************************** 
2434CCC   Subroutine Agrif_UpdateBase 
2435C     ************************************************************************** 
2436C 
2437      Subroutine Agrif_UpdateBase(TypeUpdate,
2438     &                            parenttab,childtab,
2439     &                            indmin,indmax,pttab_child,petab_child,
2440     &                            s_parent,s_child,ds_parent,ds_child,
2441     &                            coeffraf,locind_child_left)
2442C
2443CCC   Description:
2444CCC   Subroutine calling the updating method chosen by the user (copy, average
2445CCC   or full-weighting).   
2446C
2447CC    Method:
2448C
2449C     Declarations:
2450C
2451     
2452C
2453      INTEGER :: TypeUpdate
2454      INTEGER :: indmin,indmax
2455      INTEGER :: pttab_child,petab_child
2456      REAL,DIMENSION(indmin:indmax)           :: parenttab       
2457      REAL,DIMENSION(pttab_child:petab_child) :: childtab     
2458      REAL    :: s_parent,s_child
2459      REAL    :: ds_parent,ds_child     
2460      INTEGER :: coeffraf,locind_child_left
2461C
2462C
2463      if (TypeUpdate == AGRIF_Update_copy) then
2464C             
2465          Call agrif_copy1D
2466     &       (parenttab,childtab,
2467     &          indmax-indmin+1,petab_child-pttab_child+1,
2468     &          s_parent,s_child,ds_parent,ds_child)     
2469C
2470        elseif (TypeUpdate == AGRIF_Update_average) then
2471C           
2472          Call average1D
2473     &       (parenttab,childtab,
2474     &          indmax-indmin+1,petab_child-pttab_child+1,
2475     &          s_parent,s_child,ds_parent,ds_child) 
2476C
2477        elseif (TypeUpdate == AGRIF_Update_full_weighting) then
2478C             
2479          Call full_weighting1D
2480     &       (parenttab,childtab,
2481     &          indmax-indmin+1,petab_child-pttab_child+1,
2482     &          s_parent,s_child,ds_parent,ds_child,
2483     &          coeffraf,locind_child_left)
2484C
2485      endif
2486C
2487      Return               
2488C
2489C     
2490      End Subroutine Agrif_UpdateBase
2491C
2492C
2493
2494      Subroutine Agrif_Compute_nbdim_update(s_parent,s_child,
2495     &  ds_parent,ds_child,coeffraf,locind_child_left)
2496      real :: s_parent,s_child,ds_parent,ds_child
2497      integer :: coeffraf,locind_child_left
2498     
2499      coeffraf = nint(ds_parent/ds_child)
2500      locind_child_left = 1 + agrif_int((s_parent-s_child)/ds_child)
2501     
2502      End Subroutine Agrif_Compute_nbdim_update
2503 
2504#if defined AGRIF_MPI     
2505      Subroutine Agrif_Find_list_update(list_update,pttab,petab,
2506     &                          pttab_Child,pttab_Parent,nbdim,
2507     &     find_list_update,tab4t,tab5t,memberinall,memberinall2,
2508     &     sendtoproc1,recvfromproc1,sendtoproc2,recvfromproc2)     
2509      TYPE(Agrif_List_Interp_Loc), Pointer :: list_update
2510      INTEGER :: nbdim
2511      INTEGER,DIMENSION(nbdim)   :: pttab,petab,pttab_Child,pttab_Parent
2512      LOGICAL :: find_list_update
2513      Type(Agrif_List_Interp_loc), Pointer :: parcours
2514      INTEGER :: i
2515C
2516      INTEGER,DIMENSION(nbdim,0:Agrif_Nbprocs-1,8) :: tab4t
2517      INTEGER,DIMENSION(nbdim,0:Agrif_Nbprocs-1,8) :: tab5t     
2518      LOGICAL, DIMENSION(0:Agrif_Nbprocs-1) :: memberinall,memberinall2
2519      LOGICAL, DIMENSION(0:Agrif_Nbprocs-1) :: sendtoproc1,recvfromproc1     
2520      LOGICAL, DIMENSION(0:Agrif_Nbprocs-1) :: sendtoproc2,recvfromproc2     
2521                   
2522      find_list_update = .FALSE.
2523     
2524      parcours => list_update
2525     
2526      Find_loop :   Do While (associated(parcours))
2527        Do i=1,nbdim
2528          IF ((pttab(i) /= parcours%interp_loc%pttab(i)).OR.
2529     &        (petab(i) /= parcours%interp_loc%petab(i)).OR.
2530     &        (pttab_child(i) /= parcours%interp_loc%pttab_child(i)).OR.
2531     &        (pttab_parent(i) /= parcours%interp_loc%pttab_parent(i)))
2532     &               THEN
2533            parcours=>parcours%suiv
2534            Cycle Find_loop
2535          ENDIF
2536        EndDo
2537       
2538        tab4t = parcours%interp_loc%tab4t(1:nbdim,0:Agrif_Nbprocs-1,1:8)
2539        memberinall = parcours%interp_loc%memberinall(0:Agrif_Nbprocs-1)
2540       
2541        tab5t = parcours%interp_loc%tab5t(1:nbdim,0:Agrif_Nbprocs-1,1:8)
2542        memberinall2 = 
2543     &       parcours%interp_loc%memberinall2(0:Agrif_Nbprocs-1)
2544        sendtoproc1 = 
2545     &       parcours%interp_loc%sendtoproc1(0:Agrif_Nbprocs-1)   
2546        recvfromproc1 = 
2547     &       parcours%interp_loc%recvfromproc1(0:Agrif_Nbprocs-1)     
2548        sendtoproc2 = 
2549     &       parcours%interp_loc%sendtoproc2(0:Agrif_Nbprocs-1)   
2550        recvfromproc2 = 
2551     &       parcours%interp_loc%recvfromproc2(0:Agrif_Nbprocs-1)         
2552       
2553        find_list_update = .TRUE.   
2554        Exit Find_loop
2555      End Do Find_loop 
2556                             
2557      End Subroutine Agrif_Find_list_update 
2558     
2559      Subroutine Agrif_AddTo_list_update(list_update,pttab,petab,
2560     &                          pttab_Child,pttab_Parent,nbdim
2561     &      ,tab4t,tab5t,memberinall,memberinall2,
2562     &      sendtoproc1,recvfromproc1,sendtoproc2,recvfromproc2)
2563         
2564      TYPE(Agrif_List_Interp_Loc), Pointer :: list_update
2565      INTEGER :: nbdim
2566      INTEGER,DIMENSION(nbdim)   :: pttab,petab,pttab_Child,pttab_Parent
2567      INTEGER,DIMENSION(nbdim,0:Agrif_Nbprocs-1,8) :: tab4t
2568      INTEGER,DIMENSION(nbdim,0:Agrif_Nbprocs-1,8) :: tab5t     
2569      LOGICAL,DIMENSION(0:Agrif_Nbprocs-1) :: memberinall, memberinall2
2570      LOGICAL,DIMENSION(0:Agrif_Nbprocs-1) :: sendtoproc1, recvfromproc1     
2571      LOGICAL,DIMENSION(0:Agrif_Nbprocs-1) :: sendtoproc2, recvfromproc2
2572
2573      Type(Agrif_List_Interp_loc), Pointer :: parcours
2574           
2575       Allocate(parcours)
2576      Allocate(parcours%interp_loc)
2577     
2578      parcours%interp_loc%pttab(1:nbdim) = pttab(1:nbdim)
2579      parcours%interp_loc%petab(1:nbdim) = petab(1:nbdim)
2580      parcours%interp_loc%pttab_child(1:nbdim) = pttab_child(1:nbdim)
2581      parcours%interp_loc%pttab_parent(1:nbdim) = pttab_parent(1:nbdim)
2582      Allocate(parcours%interp_loc%tab4t(nbdim,0:Agrif_Nbprocs-1,8))
2583      Allocate(parcours%interp_loc%memberinall(0:Agrif_Nbprocs-1))
2584     
2585      Allocate(parcours%interp_loc%tab5t(nbdim,0:Agrif_Nbprocs-1,8))
2586      Allocate(parcours%interp_loc%memberinall2(0:Agrif_Nbprocs-1))
2587      Allocate(parcours%interp_loc%sendtoproc1(0:Agrif_Nbprocs-1))
2588      Allocate(parcours%interp_loc%recvfromproc1(0:Agrif_Nbprocs-1))     
2589      Allocate(parcours%interp_loc%sendtoproc2(0:Agrif_Nbprocs-1))
2590      Allocate(parcours%interp_loc%recvfromproc2(0:Agrif_Nbprocs-1))           
2591           
2592      parcours%interp_loc%tab4t=tab4t   
2593      parcours%interp_loc%memberinall=memberinall
2594 
2595      parcours%interp_loc%tab5t=tab5t   
2596      parcours%interp_loc%memberinall2=memberinall2
2597      parcours%interp_loc%sendtoproc1=sendtoproc1   
2598      parcours%interp_loc%recvfromproc1=recvfromproc1     
2599      parcours%interp_loc%sendtoproc2=sendtoproc2       
2600      parcours%interp_loc%recvfromproc2=recvfromproc2     
2601           
2602      parcours%suiv => list_update
2603     
2604      list_update => parcours
2605     
2606      End Subroutine Agrif_Addto_list_update
2607#endif
2608           
2609
2610C     **************************************************************************
2611CCC   Subroutine Agrif_Update_1D_Recursive
2612C     **************************************************************************
2613C
2614      Subroutine Agrif_Update_1D_recursive(TypeUpdate,tempP,tempC,
2615     &                                     indmin,indmax,
2616     &                                     pttab_child,petab_child,
2617     &                                     s_child,s_parent,
2618     &                                     ds_child,ds_parent,nbdim)
2619C
2620CCC   Description:
2621CCC   Subroutine to update a 1D grid variable on the parent grid.
2622C
2623CC    Method:
2624C
2625C     Declarations:
2626C
2627     
2628C
2629C     Arguments
2630      INTEGER                   :: nbdim
2631      INTEGER, DIMENSION(nbdim) :: TypeUpdate            ! TYPE of update (copy or average)
2632      INTEGER, DIMENSION(nbdim) :: indmin,indmax
2633      INTEGER, DIMENSION(nbdim) :: pttab_child,petab_child
2634      REAL, DIMENSION(nbdim)    :: s_child,s_parent
2635      REAL, DIMENSION(nbdim)    :: ds_child,ds_parent
2636      REAL, DIMENSION(indmin(nbdim):indmax(nbdim))           :: tempP
2637      REAL, DIMENSION(pttab_child(nbdim):petab_child(nbdim)) :: tempC
2638      INTEGER :: coeffraf,locind_child_left
2639C
2640C
2641      Call Agrif_Compute_nbdim_update(s_parent(nbdim),s_child(nbdim),
2642     &  ds_parent(nbdim),ds_child(nbdim),coeffraf,locind_child_left)
2643     
2644      Call Agrif_UpdateBase(TypeUpdate(1),
2645     &                  tempP(indmin(nbdim):indmax(nbdim)),
2646     &                  tempC(pttab_child(nbdim):petab_child(nbdim)),
2647     &                  indmin(nbdim),indmax(nbdim),           
2648     &                  pttab_child(nbdim),petab_child(nbdim),
2649     &                  s_parent(nbdim),s_child(nbdim),
2650     &                  ds_parent(nbdim),ds_child(nbdim),
2651     &                  coeffraf,locind_child_left)
2652
2653C
2654      Return
2655C
2656C
2657      End Subroutine Agrif_Update_1D_recursive     
2658
2659      End Module Agrif_Update
Note: See TracBrowser for help on using the repository browser.