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.
toamr.c in vendors/AGRIF/dev/LIB – NEMO

source: vendors/AGRIF/dev/LIB/toamr.c @ 12420

Last change on this file since 12420 was 12420, checked in by smueller, 4 years ago

Reintegration of the AGRIF development branch associated with NEMO development branch 2019/dev_r11613_ENHANCE-04_namelists_as_internalfiles (/vendors/AGRIF/dev_r11615_ENHANCE-04_namelists_as_internalfiles_agrif) into /vendors/AGRIF/dev

  • Property svn:keywords set to Id
File size: 45.0 KB
Line 
1/******************************************************************************/
2/*                                                                            */
3/*     CONV (converter) for Agrif (Adaptive Grid Refinement In Fortran)       */
4/*                                                                            */
5/* Copyright or   or Copr. Laurent Debreu (Laurent.Debreu@imag.fr)            */
6/*                        Cyril Mazauric (Cyril_Mazauric@yahoo.fr)            */
7/* This software is governed by the CeCILL-C license under French law and     */
8/* abiding by the rules of distribution of free software.  You can  use,      */
9/* modify and/ or redistribute the software under the terms of the CeCILL-C   */
10/* license as circulated by CEA, CNRS and INRIA at the following URL          */
11/* "http://www.cecill.info".                                                  */
12/*                                                                            */
13/* As a counterpart to the access to the source code and  rights to copy,     */
14/* modify and redistribute granted by the license, users are provided only    */
15/* with a limited warranty  and the software's author,  the holder of the     */
16/* economic rights,  and the successive licensors  have only  limited         */
17/* liability.                                                                 */
18/*                                                                            */
19/* In this respect, the user's attention is drawn to the risks associated     */
20/* with loading,  using,  modifying and/or developing or reproducing the      */
21/* software by the user in light of its specific status of free software,     */
22/* that may mean  that it is complicated to manipulate,  and  that  also      */
23/* therefore means  that it is reserved for developers  and  experienced      */
24/* professionals having in-depth computer knowledge. Users are therefore      */
25/* encouraged to load and test the software's suitability as regards their    */
26/* requirements in conditions enabling the security of their systems and/or   */
27/* data to be ensured and,  more generally, to use and operate it in the      */
28/* same conditions as regards security.                                       */
29/*                                                                            */
30/* The fact that you are presently reading this means that you have had       */
31/* knowledge of the CeCILL-C license and that you accept its terms.           */
32/******************************************************************************/
33/* version 1.7                                                                */
34/******************************************************************************/
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
38#include "decl.h"
39
40const char * tabvarsname(const variable *var)
41{
42    static char * tname[5] = {
43        "tabvars",      // v_catvar == 0
44        "tabvars_c",    // v_catvar == 1
45        "tabvars_r",    // v_catvar == 2
46        "tabvars_l",    // v_catvar == 3
47        "tabvars_i"     // v_catvar == 4
48    };
49    return tname[var->v_catvar];    // v_catvar should never be ouside the range [0:4].
50}
51
52/******************************************************************************/
53/*                        variablecurgridtabvars                              */
54/******************************************************************************/
55/* This subroutine is used to create the string                               */
56/******************************************************************************/
57/*                                                                            */
58/*  ----------->  Agrif_Curgrid % tabvars (i)                                 */
59/*                                                                            */
60/******************************************************************************/
61const char * variablecurgridtabvars(int which_grid)
62{
63    static char * varname[4] = {
64        " Agrif_%s(%d)",                // which_grid == 0
65        " Agrif_%s(%d) %% parent_var",  // which_grid == 1
66        " Agrif_Mygrid %% %s(%d)",      // which_grid == 2
67        " Agrif_Curgrid %% %s(%d)",     // which_grid == 3
68    };
69
70    return varname[which_grid];
71}
72
73void WARNING_CharSize(const variable *var)
74{
75    if ( var->v_nbdim == 0 )
76    {
77        if ( convert2int(var->v_dimchar) > 2400 )
78        {
79            printf("WARNING : The dimension of the character  %s   \n", var->v_nomvar);
80            printf("   is upper than 2400. You must change         \n");
81            printf("   the dimension of carray0                    \n");
82            printf("   in the file AGRIF/AGRIF_FILES/modtypes.F90  \n");
83            printf("   line 161. Replace 2400 with %d.              \n", convert2int(var->v_dimchar)+100);
84        }
85        Save_Length_int(convert2int(var->v_dimchar),1);
86    }
87    else if ( var->v_nbdim == 1 )
88    {
89        if ( convert2int(var->v_dimchar) > 200 )
90        {
91            printf("WARNING : The dimension of the character  %s   \n", var->v_nomvar);
92            printf("   is upper than 200. You must change          \n");
93            printf("   the dimension of carray1                    \n");
94            printf("   in the file AGRIF/AGRIF_FILES/modtypes.F90  \n");
95            printf("   line 162. Replace 200 with %d.              \n", convert2int(var->v_dimchar)+100);
96        }
97        Save_Length_int(convert2int(var->v_dimchar),2);
98    }
99    else if ( var->v_nbdim == 2 )
100    {
101        if ( convert2int(var->v_dimchar) > 200 )
102        {
103            printf("WARNING : The dimension of the character  %s   \n", var->v_nomvar);
104            printf("   is upper than 200. You must change          \n");
105            printf("   the dimension of carray2                    \n");
106            printf("   in the file AGRIF/AGRIF_FILES/modtypes.F90  \n");
107            printf("   line 163. Replace 200 with %d.              \n", convert2int(var->v_dimchar)+100);
108        }
109        Save_Length_int(convert2int(var->v_dimchar),3);
110    }
111    else if ( var->v_nbdim == 3 )
112    {
113        if ( convert2int(var->v_dimchar) > 200 )
114        {
115            printf("WARNING : The dimension of the character  %s   \n", var->v_nomvar);
116            printf("   is upper than 200. You must change          \n");
117            printf("   the dimension of carray3                    \n");
118            printf("   in the file AGRIF/AGRIF_FILES/modtypes.F90  \n");
119            printf("   line 164. Replace 200 with %d.              \n", convert2int(var->v_dimchar)+100);
120        }
121        Save_Length_int(convert2int(var->v_dimchar),4);
122    }
123}
124/******************************************************************************/
125/*                           vargridnametabvars                               */
126/******************************************************************************/
127/* This subroutine is used to create the string                               */
128/******************************************************************************/
129/*                                                                            */
130/*  if iorindice == 0 ----------->  Agrif_Gr % tabvars (i) % array1           */
131/*                                                                            */
132/*  if iorindice == 1 ----------->  Agrif_Gr % tabvars (12) % array1          */
133/*                                                                            */
134/******************************************************************************/
135const char *vargridnametabvars (const variable * var, int iorindice)
136{
137    static char tname_1[LONG_C];
138    static char tname_2[LONG_C];
139
140    if ( iorindice == 0 ) sprintf(tname_1, "Agrif_Gr %% %s(%d)", tabvarsname(var), var->v_indicetabvars);
141    else                  sprintf(tname_1, "Agrif_Gr %% %s(i)",  tabvarsname(var));
142
143    if (!strcasecmp(var->v_typevar, "REAL"))
144    {
145        if      ( !strcasecmp(var->v_nameinttypename,"8") ) sprintf(tname_2, "%% darray%d", var->v_nbdim);
146        else if ( !strcasecmp(var->v_nameinttypename,"4") ) sprintf(tname_2, "%% sarray%d", var->v_nbdim);
147        else                                                sprintf(tname_2, "%% array%d",  var->v_nbdim);
148    }
149    else if (!strcasecmp(var->v_typevar, "integer"))
150    {
151        sprintf(tname_2, "%% iarray%d", var->v_nbdim);
152    }
153    else if (!strcasecmp(var->v_typevar, "logical"))
154    {
155        sprintf(tname_2, "%% larray%d", var->v_nbdim);
156    }
157    else if (!strcasecmp(var->v_typevar, "character"))
158    {
159        WARNING_CharSize(var);
160        if (!strcasecmp(var->v_dimchar  ,":") && var->v_nbdim == 0 )
161        {
162        sprintf (tname_2, "%% carrayu");
163        } else {
164        sprintf (tname_2, "%% carray%d", var->v_nbdim);
165        }
166    }
167
168    strcat(tname_1, tname_2);
169    Save_Length(tname_1, 46);
170
171    return tname_1;
172}
173
174/******************************************************************************/
175/*                           vargridcurgridtabvars                            */
176/******************************************************************************/
177/* This subroutine is used to create the string                               */
178/******************************************************************************/
179/*                                                                            */
180/* if which_grid == 0 -->  Agrif_Curgrid % tabvars (i) % array1               */
181/*                                                                            */
182/* if which_grid == 1 -->  Agrif_tabvars (i) % parent_var % array1            */
183/*                                                                            */
184/* if which_grid == 2 -->  Agrif_Gr % tabvars (i) % array1                    */
185/*                                                                            */
186/******************************************************************************/
187const char *vargridcurgridtabvars(const variable *var, int which_grid)
188{
189    static char tname_1[LONG_C];
190    static char tname_2[LONG_C];
191
192    if (!strcasecmp(var->v_typevar,"type"))
193    {
194        sprintf(tname_1, "Agrif_%s_var(Agrif_Curgrid%%fixedrank)%%%s", var->v_modulename, var->v_nomvar);
195    }
196    else
197    {
198        sprintf(tname_1, variablecurgridtabvars(which_grid), tabvarsname(var), var->v_indicetabvars);
199
200        if (!strcasecmp(var->v_typevar, "REAL"))
201        {
202            if      ( !strcasecmp(var->v_nameinttypename,"8") ) sprintf(tname_2, "%% darray%d", var->v_nbdim);
203            else if ( !strcasecmp(var->v_nameinttypename,"4") ) sprintf(tname_2, "%% sarray%d", var->v_nbdim);
204            else                                                sprintf(tname_2, "%% array%d", var->v_nbdim);
205        }
206        else if (!strcasecmp(var->v_typevar, "INTEGER"))
207        {
208            sprintf(tname_2, "%% iarray%d", var->v_nbdim);
209        }
210        else if (!strcasecmp(var->v_typevar, "LOGICAL"))
211        {
212            sprintf(tname_2, "%% larray%d", var->v_nbdim);
213        }
214        else if (!strcasecmp(var->v_typevar, "CHARACTER"))
215        {
216            WARNING_CharSize(var);
217            if (!strcasecmp(var->v_dimchar  ,":") && var->v_nbdim == 0 )
218            {
219            sprintf (tname_2, "%% carrayu");
220            } else {
221            sprintf (tname_2, "%% carray%d", var->v_nbdim);
222            }
223        }
224        strcat(tname_1, tname_2);
225    }
226    Save_Length(tname_1, 46);
227
228    return tname_1;
229}
230
231/******************************************************************************/
232/*                  vargridcurgridtabvarswithoutAgrif_Gr                      */
233/******************************************************************************/
234/* This subroutine is used to create the string                               */
235/******************************************************************************/
236/*                                                                            */
237/******************************************************************************/
238const char *vargridcurgridtabvarswithoutAgrif_Gr(const variable *var)
239{
240    static char tname_1[LONG_C];
241    static char tname_2[LONG_C];
242
243    sprintf(tname_1, "(%d)", var->v_indicetabvars);
244
245    if (!strcasecmp (var->v_typevar, "REAL"))
246    {
247        if      ( !strcasecmp(var->v_nameinttypename,"8") ) sprintf(tname_2, "%% darray%d", var->v_nbdim);
248        else if ( !strcasecmp(var->v_nameinttypename,"4") ) sprintf(tname_2, "%% sarray%d", var->v_nbdim);
249        else                                                sprintf(tname_2, "%% array%d", var->v_nbdim);
250    }
251    else if (!strcasecmp(var->v_typevar, "INTEGER"))
252    {
253        sprintf(tname_2, "%% iarray%d", var->v_nbdim);
254    }
255    else if (!strcasecmp(var->v_typevar, "LOGICAL"))
256    {
257        sprintf(tname_2, "%% larray%d", var->v_nbdim);
258    }
259    else if (!strcasecmp(var->v_typevar, "CHARACTER"))
260    {
261        WARNING_CharSize(var);
262        sprintf(tname_2, "%% carray%d", var->v_nbdim);
263    }
264
265    strcat(tname_1, tname_2);
266    Save_Length(tname_1, 46);
267
268    return tname_1;
269}
270
271/******************************************************************************/
272/*                               vargridparam                                 */
273/******************************************************************************/
274/* This subroutine is used to create the string which contains                */
275/* dimension list                                                             */
276/******************************************************************************/
277/*                                                                            */
278/*  DIMENSION(jpi,0:jpj) ----------->"1:jpi,0:jpj"                            */
279/*                                                                            */
280/******************************************************************************/
281const char * vargridparam(const variable *var)
282{
283    typedim dim;
284    listdim *newdim;
285    char newname[LONG_M];
286
287    newdim = var->v_dimension;
288    if (!newdim) return "";
289
290    strcpy (tmpvargridname, "(");
291    while (newdim)
292    {
293        dim = newdim->dim;
294        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(dim.first,List_Global_Var));
295        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(newname,List_Common_Var));
296        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(newname,List_ModuleUsed_Var));
297        strcat(tmpvargridname, newname);
298        strcat(tmpvargridname, " : ");
299        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(dim.last,List_Global_Var));
300        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(newname, List_Common_Var));
301        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(newname, List_ModuleUsed_Var));
302        strcat(tmpvargridname, newname);
303        newdim = newdim->suiv;
304        if (newdim) strcat(tmpvargridname, ",");
305    }
306    strcat(tmpvargridname, ")\0");
307    Save_Length(tmpvargridname,40);
308    return tmpvargridname;
309}
310
311/******************************************************************************/
312/*                        write_probdimagrif_file                             */
313/******************************************************************************/
314/* This subroutine is used to create the file probdim_agrif.h                 */
315/******************************************************************************/
316/*                                                                            */
317/*               probdim_agrif.h                                              */
318/*                                                                            */
319/*               Agrif_probdim = <number>                                     */
320/*                                                                            */
321/******************************************************************************/
322void write_probdimagrif_file()
323{
324  FILE *probdim;
325  char ligne[LONG_M];
326
327  probdim = open_for_write("probdim_agrif.h");
328  sprintf (ligne, "Agrif_Probdim = %d", dimprob);
329  tofich (probdim, ligne,1);
330  fclose (probdim);
331}
332
333/******************************************************************************/
334/*                             write_keysagrif_file                           */
335/******************************************************************************/
336/* This subroutine is used to create the file keys_agrif.h                    */
337/******************************************************************************/
338/*                                                                            */
339/*               keys_agrif.h                                                 */
340/*                                                                            */
341/*               AGRIF_USE_FIXED_GRIDS = 0                                    */
342/*               AGRIF_USE_ONLY_FIXED_GRIDS = 0                               */
343/*               AGRIF_USE_(ONLY)_FIXED_GRIDS = 1                             */
344/*                                                                            */
345/******************************************************************************/
346void write_keysagrif_file()
347{
348  FILE *keys;
349
350  keys = open_for_write("keys_agrif.h");
351  fprintf(keys,"      AGRIF_USE_FIXED_GRIDS = %d\n", fixedgrids);
352  fprintf(keys,"      AGRIF_USE_ONLY_FIXED_GRIDS = %d\n", onlyfixedgrids);
353  fclose(keys);
354}
355
356/******************************************************************************/
357/*                      write_modtypeagrif_file                               */
358/******************************************************************************/
359/* This subroutine is used to create the file typedata                        */
360/******************************************************************************/
361/*                                                                            */
362/*               modtype_agrif.h                                              */
363/*                                                                            */
364/*               Agrif_NbVariables =                                          */
365/*                                                                            */
366/******************************************************************************/
367void write_modtypeagrif_file()
368{
369  char ligne[LONG_M];
370  FILE *typedata;
371  int i;
372
373  typedata = open_for_write("modtype_agrif.h");
374  /* AGRIF_NbVariables : number of variables                                  */
375  for (i=0;i<NB_CAT_VARIABLES;i++)
376   {
377    sprintf (ligne, "Agrif_NbVariables(%d) = %d",i,indicemaxtabvars[i]);
378    tofich(typedata,ligne,1);
379   }
380  fclose (typedata);
381}
382
383/******************************************************************************/
384/*                   write_createvarnameagrif_file                            */
385/******************************************************************************/
386/* This subroutine is used to create the file  createvarname                  */
387/******************************************************************************/
388/*                                                                            */
389/*    Agrif_Gr % tabvars (i) % namevar = "variable"                           */
390/*                                                                            */
391/******************************************************************************/
392void write_createvarnameagrif_file(variable *v,FILE *createvarname, int *InitEmpty)
393{
394    char ligne[LONG_M];
395
396    *InitEmpty = 0 ;
397    sprintf(ligne, "Agrif_Gr %% %s(%d) %% namevar = \"%s\"",tabvarsname(v),v->v_indicetabvars,v->v_nomvar);
398    tofich(createvarname,ligne,1);
399}
400
401/******************************************************************************/
402/*                        write_Setnumberofcells_file                         */
403/******************************************************************************/
404/* This subroutine is used to create the file  setnumberofcells               */
405/******************************************************************************/
406/*                                                                            */
407/*              Agrif_Gr % n(i) = nbmailles                                   */
408/*                                                                            */
409/******************************************************************************/
410void write_Setnumberofcells_file()
411{
412    char ligne[LONG_VNAME];
413    char cformat[LONG_VNAME];
414    FILE *setnumberofcells;
415
416    if ( IndicenbmaillesX == 0 )  return;
417
418    setnumberofcells = open_for_write("SetNumberofcells.h");
419
420    if ( onlyfixedgrids == 1 )
421        strcpy(cformat, "Agrif_Gr %% nb(%d) = Agrif_Curgrid %% tabvars_i(%d) %% iarray0");
422    else
423        strcpy(cformat, "Agrif_Gr %% nb(%d) = Agrif_Gr %% tabvars_i(%d) %% iarray0");
424
425    sprintf(ligne, cformat, 1, IndicenbmaillesX);
426    tofich(setnumberofcells, ligne, 1);
427
428    if ( dimprob > 1 )
429    {
430        sprintf(ligne, cformat, 2, IndicenbmaillesY);
431        tofich(setnumberofcells, ligne, 1);
432    }
433    if ( dimprob > 2 )
434    {
435        sprintf(ligne, cformat, 3, IndicenbmaillesZ);
436        tofich(setnumberofcells, ligne, 1);
437    }
438    fclose(setnumberofcells);
439}
440
441/******************************************************************************/
442/*                       write_Getnumberofcells_file                          */
443/******************************************************************************/
444/* This subroutine is used to create the file  getnumberofcells               */
445/******************************************************************************/
446/*                                                                            */
447/*              nbmailles = Agrif_Gr % n(i)                                   */
448/*                                                                            */
449/******************************************************************************/
450void write_Getnumberofcells_file()
451{
452    char ligne[LONG_VNAME];
453    char cformat[LONG_VNAME];
454    FILE *getnumberofcells;
455
456    if ( IndicenbmaillesX == 0 )    return;
457
458    strcpy(cformat, "Agrif_Curgrid %% tabvars_i(%d) %% iarray0 = Agrif_Gr %% nb(%d)");
459
460    getnumberofcells = open_for_write("GetNumberofcells.h");
461
462    sprintf(ligne, cformat, IndicenbmaillesX, 1);
463    tofich(getnumberofcells, ligne, 1);
464
465    if (dimprob > 1)
466    {
467        sprintf(ligne, cformat, IndicenbmaillesY, 2);
468        tofich(getnumberofcells, ligne,1);
469    }
470    if (dimprob > 2)
471    {
472        sprintf(ligne, cformat, IndicenbmaillesZ, 3);
473        tofich(getnumberofcells, ligne,1);
474    }
475    fclose(getnumberofcells);
476}
477
478
479/******************************************************************************/
480/*                      write_initialisationsagrif_file                       */
481/******************************************************************************/
482/* This subroutine is used to create the file initproc                        */
483/******************************************************************************/
484/*                                                                            */
485/*              ! variable                                                    */
486/*              Agrif_Gr % tabvars(i) % nbdim = 1                             */
487/*                                                                            */
488/******************************************************************************/
489void write_initialisationsagrif_file(variable *v,FILE *initproc,int *VarnameEmpty)
490{
491    char ligne[LONG_M];
492
493    if ( v->v_nbdim != 0 )
494    {
495        *VarnameEmpty = 0 ;
496        sprintf(ligne,"Agrif_Mygrid %% %s(%d) %% nbdim = %d", tabvarsname(v), v->v_indicetabvars, v->v_nbdim);
497        tofich (initproc, ligne,1);
498    }
499}
500
501
502void Write_Alloc_Agrif_Files()
503{
504   listnom *parcours;
505   FILE *alloccalls;
506   FILE *AllocUSE;
507
508   AllocUSE= open_for_write("include_use_Alloc_agrif.h");
509   alloccalls = open_for_write("allocations_calls_agrif.h");
510
511   parcours = List_Subroutine_For_Alloc;
512   while ( parcours )
513   {
514      fprintf(AllocUSE,"      use %s, only: Alloc_agrif_%s\n", parcours -> o_nom, parcours -> o_nom );
515      fprintf (alloccalls,"      call Alloc_agrif_%s(Agrif_Gr)\n", parcours -> o_nom );
516      parcours = parcours -> suiv;
517   }
518
519   fclose (AllocUSE);
520   fclose (alloccalls);
521}
522
523int IndiceInlist(int indic, listindice *listin)
524{
525   listindice *parcoursindic;
526   int out;
527
528   out = 0 ;
529
530   parcoursindic = listin;
531   while ( parcoursindic && out == 0 )
532   {
533      if ( parcoursindic->i_indice == indic ) out = 1;
534      else parcoursindic = parcoursindic -> suiv;
535   }
536
537   return out;
538}
539
540void write_allocation_Common_0()
541{
542    listnom *parcours_nom;
543    listnom *neededparameter;
544    listvar *parcours;
545    listvar *parcoursprec;
546    listvar *parcours1;
547    FILE *allocationagrif;
548    FILE *paramtoamr;
549    char ligne[LONG_M];
550    char ligne2[LONG_M];
551    variable *v;
552    int IndiceMax;
553    int IndiceMin;
554    int compteur;
555    int out;
556    int indiceprec;
557    int ValeurMax;
558    char initialvalue[LONG_M];
559    listindice **list_indic;
560    listindice *parcoursindic;
561    int i;
562
563    parcoursprec = (listvar *) NULL;
564    parcours_nom = List_NameOfCommon;
565    ValeurMax = 2;
566    while ( parcours_nom  )
567    {
568        if ( parcours_nom->o_val == 1 )
569        {
570            /* Open the file to create the Alloc_agrif subroutine                */
571            sprintf(ligne,"alloc_agrif_%s.h",parcours_nom->o_nom);
572            allocationagrif = open_for_write(ligne);
573            fprintf(allocationagrif,"#include \"Param_toamr_%s.h\" \n", parcours_nom->o_nom);
574
575            sprintf(ligne,"Param_toamr_%s.h",parcours_nom->o_nom);
576            paramtoamr = open_for_write(ligne);
577            neededparameter = (listnom *) NULL;
578            list_indic = (listindice **) calloc(NB_CAT_VARIABLES,sizeof(listindice *));
579
580//             shouldincludempif = 1 ;
581            parcours = List_Common_Var;
582            while ( parcours )
583            {
584                if ( !strcasecmp(parcours->var->v_commonname,parcours_nom->o_nom) &&
585                    IndiceInlist(parcours->var->v_indicetabvars,list_indic[parcours->var->v_catvar]) == 0 )
586                {
587                    v = parcours->var;
588                    IndiceMax = 0;
589                    IndiceMin = indicemaxtabvars[v->v_catvar];
590                    /* body of the file */
591                    if ( !strcasecmp(v->v_commoninfile,cur_filename) )
592                    {
593                        if ( (onlyfixedgrids != 1) && (v->v_nbdim != 0) )
594                        {
595                            sprintf(ligne,"if (.not. allocated(%s)) then", vargridnametabvars(v,0));
596                            tofich(allocationagrif,ligne,1);
597                        }
598                        if ( (v->v_allocatable != 1) && (v->v_dimsempty != 1) )
599                        {
600                            /*                ALLOCATION                                          */
601                            if ( v->v_dimension != 0 )
602                            {
603                                if ( v->v_indicetabvars < IndiceMin || v->v_indicetabvars > IndiceMax )
604                                {
605                                    parcours1 = parcours;
606                                    compteur = -1;
607                                    out = 0;
608                                    indiceprec = parcours->var->v_indicetabvars -1 ;
609                                    while ( parcours1 && out == 0
610                                        && !strcasecmp(parcours->var->v_readedlistdimension,parcours1->var->v_readedlistdimension)
611                                        && !strcasecmp(parcours->var->v_typevar,            parcours1->var->v_typevar)
612                                        && (parcours1->var->v_indicetabvars == indiceprec+1) )
613                                    {
614                                        if ( !strcasecmp(parcours1->var->v_modulename,parcours_nom->o_nom) ||
615                                             !strcasecmp(parcours1->var->v_commonname,parcours_nom->o_nom) )
616                                        {
617                                            compteur = compteur +1 ;
618                                            indiceprec = parcours1->var->v_indicetabvars;
619                                            parcoursprec = parcours1;
620                                            parcours1 = parcours1->suiv;
621                                        }
622                                        else out = 1;
623                                    }
624                                    sprintf(ligne,"!! ALLOCATION OF VARIABLE : %s",v->v_nomvar);
625                                    tofich(allocationagrif,ligne,1);
626                                    if ( compteur > ValeurMax )
627                                    {
628                                        sprintf(ligne,"do i = %d,%d", parcours->var->v_indicetabvars,
629                                                                      parcours->var->v_indicetabvars+compteur);
630                                        tofich(allocationagrif,ligne,1);
631                                        IndiceMin = parcours->var->v_indicetabvars;
632                                        IndiceMax = parcours->var->v_indicetabvars+compteur;
633                                        sprintf(ligne,"    allocate(%s", vargridnametabvars(v,1));
634                                        sprintf(ligne2,"%s)", vargridparam(v));
635                                        strcat(ligne,ligne2);
636                                        tofich(allocationagrif,ligne,1);
637                                        tofich(allocationagrif,"enddo",1);
638                                        i = parcours->var->v_indicetabvars;
639                                        do
640                                        {
641                                            parcoursindic =  (listindice *)calloc(1,sizeof(listindice));
642                                            parcoursindic -> i_indice = i;
643                                            parcoursindic -> suiv = list_indic[parcours->var->v_catvar];
644                                            list_indic[parcours->var->v_catvar] = parcoursindic;
645                                            i = i + 1;
646                                        } while ( i <= parcours->var->v_indicetabvars+compteur );
647                                        parcours = parcoursprec;
648                                    }
649                                    else
650                                    {
651                                        sprintf(ligne,"allocate(%s", vargridnametabvars(v,0));
652                                        sprintf(ligne2,"%s)", vargridparam(v));
653                                        strcat(ligne,ligne2);
654                                        tofich(allocationagrif,ligne,1);
655                                        parcoursindic =  (listindice *) calloc(1,sizeof(listindice));
656                                        parcoursindic -> i_indice = parcours->var->v_indicetabvars;
657                                        parcoursindic -> suiv = list_indic[parcours->var->v_catvar];
658                                        list_indic[parcours->var->v_catvar] = parcoursindic;
659                                    }
660                                    neededparameter = writedeclarationintoamr(List_Parameter_Var,
661                                                        paramtoamr,v,parcours_nom->o_nom,neededparameter,v->v_commonname);
662                                }
663                            } /* end of the allocation part                                       */
664                            /*                INITIALISATION                                      */
665                            if ( strcasecmp(v->v_initialvalue,"") )
666                            {
667                                strcpy(ligne, vargridnametabvars(v,0));
668                                /* We should modify the initialvalue in the case of variable has been defined with others variables */
669                                strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(v->v_initialvalue,List_Global_Var));
670                                if ( !strcasecmp(initialvalue,v->v_initialvalue) )
671                                {
672                                    strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(v->v_initialvalue,List_Common_Var));
673                                }
674                                if ( !strcasecmp(initialvalue,v->v_initialvalue) )
675                                {
676                                    strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(v->v_initialvalue,List_ModuleUsed_Var));
677                                }
678                                strcat (ligne," = ");
679
680                                if (v->v_nbdim == 0)
681                                {
682                                    strcpy(ligne2,initialvalue);
683                                }
684                                else
685                                {
686                                    sprintf(ligne2,"reshape(%s,shape(%s))",initialvalue,vargridnametabvars(v,0));
687                                }
688                                strcat(ligne,ligne2);
689                                tofich(allocationagrif,ligne,1);
690                            }
691                        }
692                        if ( (onlyfixedgrids != 1) && (v->v_nbdim != 0) )
693                        {
694                            tofich(allocationagrif,"endif",1);
695                        }
696                    }
697                }
698                parcours = parcours -> suiv;
699            }
700            /* Close the file Alloc_agrif                                        */
701            fclose(allocationagrif);
702            fclose(paramtoamr);
703        }
704        parcours_nom = parcours_nom -> suiv;
705    }
706}
707
708void write_allocation_Global_0()
709{
710    listnom *parcours_nom;
711    listvar *parcours;
712    listvar *parcoursprec;
713    listvar *parcours1;
714    FILE *allocationagrif;
715    char ligne[LONG_M];
716    char ligne2[LONG_M];
717    variable *v;
718    int IndiceMax;
719    int IndiceMin;
720    int compteur;
721    int out;
722    int indiceprec;
723    int ValeurMax;
724    char initialvalue[LONG_M];
725    int typeiswritten ;
726
727    parcoursprec = (listvar *) NULL;
728    parcours_nom = List_NameOfModule;
729    ValeurMax = 2;
730
731    while ( parcours_nom  )
732    {
733        if ( parcours_nom->o_val == 1 )
734        {
735            IndiceMax = 0;
736            IndiceMin = indicemaxtabvars[0];
737            /* Open the file to create the Alloc_agrif subroutine                */
738            sprintf(ligne,"alloc_agrif_%s.h",parcours_nom->o_nom);
739            allocationagrif = open_for_write(ligne);
740
741//             if ( ModuleIsDefineInInputFile(parcours_nom->o_nom) == 1 )
742//             {
743//                 /* add the call to initworkspace         */
744//                 tofich(allocationagrif,"if (.not. Agrif_Root() ) then",1);
745//                 tofich(allocationagrif,"#include \"GetNumberofcells.h\"\n",0);
746//                 tofich(allocationagrif,"else",1);
747//                 tofich(allocationagrif,"#include \"SetNumberofcells.h\"\n",0);
748//                 tofich(allocationagrif,"endif",1);
749//                 tofich(allocationagrif,"call Agrif_InitWorkspace",1);
750//             }
751
752            typeiswritten = 0;
753            parcours = List_Global_Var;
754            while ( parcours )
755            {
756                if ( !strcasecmp(parcours->var->v_modulename,parcours_nom->o_nom) &&
757                       parcours->var->v_VariableIsParameter == 0                  &&
758                       parcours->var->v_notgrid == 0  )
759                {
760                    v = parcours->var;
761                    IndiceMax = 0;
762                    IndiceMin = indicemaxtabvars[v->v_catvar];
763                    /* body of the file */
764                    if ( !strcasecmp(v->v_commoninfile,cur_filename) )
765                    {
766                        if ( (onlyfixedgrids != 1) && (v->v_nbdim != 0) )
767                        {
768                            sprintf(ligne,"if (.not. allocated(%s)) then", vargridnametabvars(v,0));
769                            tofich(allocationagrif,ligne,1);
770                        }
771                        if ( (v->v_allocatable != 1) && (v->v_dimsempty != 1) )
772                        {
773                            /*                ALLOCATION                                          */
774                            if ( v->v_dimension != 0 )
775                            {
776                                if ( v->v_indicetabvars < IndiceMin || v->v_indicetabvars > IndiceMax )
777                                {
778                                    parcours1 = parcours;
779                                    compteur = -1;
780                                    out = 0;
781                                    indiceprec = parcours->var->v_indicetabvars -1 ;
782                                    while ( parcours1 && out == 0
783                                        && !strcasecmp(parcours->var->v_readedlistdimension,parcours1->var->v_readedlistdimension)
784                                        && !strcasecmp(parcours->var->v_typevar,            parcours1->var->v_typevar)
785                                        && (parcours1->var->v_indicetabvars == indiceprec+1) )
786                                    {
787                                        if ( !strcasecmp(parcours1->var->v_modulename, parcours_nom->o_nom) ||
788                                             !strcasecmp(parcours1->var->v_commonname, parcours_nom->o_nom) )
789                                        {
790                                            compteur = compteur +1 ;
791                                            indiceprec = parcours1->var->v_indicetabvars;
792                                            parcoursprec = parcours1;
793                                            parcours1 = parcours1->suiv;
794                                        }
795                                        else out = 1;
796                                    }
797                                    sprintf(ligne,"!! ALLOCATION OF VARIABLE : %s",v->v_nomvar);
798                                    tofich(allocationagrif,ligne,1);
799                                    if ( compteur > ValeurMax )
800                                    {
801                                        sprintf(ligne,"do i = %d,%d", parcours->var->v_indicetabvars,
802                                                                      parcours->var->v_indicetabvars+compteur);
803                                        tofich(allocationagrif,ligne,1);
804                                        IndiceMin = parcours->var->v_indicetabvars;
805                                        IndiceMax = parcours->var->v_indicetabvars+compteur;
806                                        sprintf(ligne,"    allocate(%s", vargridnametabvars(v,1));
807                                        sprintf(ligne2,"%s)", vargridparam(v));
808                                        strcat(ligne,ligne2);
809                                        tofich(allocationagrif,ligne,1);
810                                        tofich(allocationagrif,"enddo",1);
811                                        parcours = parcoursprec;
812                                    }
813                                    else
814                                    {
815                                        sprintf(ligne,"allocate(%s", vargridnametabvars(v,0));
816                                        sprintf(ligne2,"%s)", vargridparam(v));
817                                        strcat(ligne,ligne2);
818                                        tofich(allocationagrif,ligne,1);
819                                    }
820                                }
821                            } /* end of the allocation part                                       */
822                            /*                INITIALISATION                                      */
823                            if ( strcasecmp(v->v_initialvalue,"") )
824                            {
825                                strcpy(ligne, vargridnametabvars(v,0));
826                                /* We should modify the initialvalue in the case of variable has been defined with others variables */
827                                strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(v->v_initialvalue,List_Global_Var));
828                                if ( !strcasecmp(initialvalue,v->v_initialvalue) )
829                                {
830                                    strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(v->v_initialvalue,List_Common_Var));
831                                }
832                                if ( !strcasecmp(initialvalue,v->v_initialvalue) )
833                                {
834                                    strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(v->v_initialvalue,List_ModuleUsed_Var));
835                                }
836                                strcat (ligne," = ");
837                                strcat (ligne,initialvalue);
838                                Save_Length(ligne,48);
839                                tofich(allocationagrif,ligne,1);
840                            }
841                        }
842                        /* Case of structure types */
843                        if ( (typeiswritten == 0) && !strcasecmp(v->v_typevar,"type") )
844                        {
845                            sprintf(ligne,"if (.not. allocated(Agrif_%s_var)) then",v->v_modulename);
846                            tofich(allocationagrif, ligne, 1);
847                            sprintf(ligne,"    allocate(Agrif_%s_var(0:Agrif_NbMaxGrids))",v->v_modulename);
848                            tofich(allocationagrif, ligne, 1);
849                            tofich(allocationagrif, "endif", 1);
850                            typeiswritten = 1;
851                        }
852                        if ( (onlyfixedgrids != 1) && (v->v_nbdim != 0) )
853                        {
854                            tofich(allocationagrif,"endif",1);
855                        }
856                    }
857                }
858                parcours = parcours -> suiv;
859            }
860            if ( ModuleIsDefineInInputFile(parcours_nom->o_nom) == 1 )
861            {
862                fprintf(allocationagrif, "      if ( .not.Agrif_Root() ) then\n");
863                fprintf(allocationagrif, "#include \"GetNumberofcells.h\"\n");
864                fprintf(allocationagrif, "      else\n");
865                fprintf(allocationagrif, "#include \"SetNumberofcells.h\"\n");
866                fprintf(allocationagrif, "      endif\n");
867                fprintf(allocationagrif, "      call Agrif_InitWorkspace\n");
868            }
869            fclose(allocationagrif);
870        }
871        parcours_nom = parcours_nom -> suiv;
872    }
873}
874
875/******************************************************************************/
876/*                           creefichieramr                                   */
877/******************************************************************************/
878/* This subroutine is the main one to create AGRIF_INC files                  */
879/******************************************************************************/
880/*                                                                            */
881/******************************************************************************/
882void creefichieramr ()
883{
884    listvar *newvar;
885    variable *v;
886    int erreur;
887    char filefich[LONG_M];
888
889    int InitEmpty;
890    int VarnameEmpty;
891    int donotwrite;
892
893    FILE *initproc;
894    FILE *initglobal;
895    FILE *createvarname;
896    FILE *createvarnameglobal;
897
898    if ( todebug == 1 ) printf("Enter in creefichieramr\n");
899
900    sprintf(filefich, "cd %s", include_dir);
901    erreur = system (filefich);
902    if (erreur)
903    {
904        sprintf(filefich, "mkdir -p %s", include_dir);
905        system(filefich);
906        printf("%s: Directory created\n", include_dir);
907    }
908
909/******************************************************************************/
910/******************** Creation of AGRIF_INC files *****************************/
911/******************************************************************************/
912
913    if ( todebug == 1 )
914    {
915        const char *NameTampon = "toto";
916        sprintf(filefich,"initialisations_agrif_%s.h", NameTampon);
917        initproc = open_for_write(filefich);
918
919        sprintf(filefich,"createvarname_agrif_%s.h", NameTampon);
920        createvarname = open_for_write(filefich);
921
922        InitEmpty = 1 ;
923        VarnameEmpty = 1 ;
924
925        newvar = List_Global_Var;
926        while ( newvar )
927        {
928            donotwrite = 0;
929            v = newvar->var;
930
931            if ( ( v->v_common == 1 || v->v_module == 1 ) && donotwrite == 0 )
932            {
933                write_createvarnameagrif_file(v,createvarname,&VarnameEmpty);
934                write_initialisationsagrif_file(v,initproc,&InitEmpty);
935            }
936            newvar = newvar->suiv;
937        }
938        fclose (createvarname);
939        fclose (initproc);
940
941        if ( is_dependfile_created(curmodulename) == 0 )
942        {
943            if ( InitEmpty != 1  )
944            {
945                initglobal = open_for_append("initialisations_agrif.h");
946                fprintf(initglobal,"#include \"initialisations_agrif_%s.h\"\n", NameTampon);
947                fclose(initglobal);
948            }
949            if ( VarnameEmpty != 1 )
950            {
951                createvarnameglobal= open_for_append("createvarname_agrif.h");
952                fprintf(createvarnameglobal,"#include \"createvarname_agrif_%s.h\"\n", NameTampon);
953                fclose(createvarnameglobal);
954            }
955        }
956    }
957    write_allocation_Common_0();
958    write_allocation_Global_0();
959
960    Write_Alloc_Agrif_Files();
961    write_probdimagrif_file();
962    write_keysagrif_file();
963    write_modtypeagrif_file();
964
965    if ( NbMailleXDefined == 1 )
966    {
967        write_Setnumberofcells_file();
968        write_Getnumberofcells_file();
969    }
970
971    if ( todebug == 1 ) printf("Out of creefichieramr\n");
972}
Note: See TracBrowser for help on using the repository browser.