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

Last change on this file was 14107, checked in by nicolasmartin, 3 years ago

Reintegration of dev_r12970_AGRIF_CMEMS to AGRIF/dev

  • Property svn:keywords set to Id
File size: 53.4 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
50    return tname[var->v_catvar];    // v_catvar should never be ouside the range [0:4].
51}
52
53/******************************************************************************/
54/*                        variablecurgridtabvars                              */
55/******************************************************************************/
56/* This subroutine is used to create the string                               */
57/******************************************************************************/
58/*                                                                            */
59/*  ----------->  Agrif_Curgrid % tabvars (i)                                 */
60/*                                                                            */
61/******************************************************************************/
62const char * variablecurgridtabvars(int which_grid)
63{
64    static char * varname[4] = {
65        " Agrif_%s(%d)",                // which_grid == 0
66        " Agrif_%s(%d) %% parent_var",  // which_grid == 1
67        " Agrif_Mygrid %% %s(%d)",      // which_grid == 2
68        " Agrif_Curgrid %% %s(%d)",     // which_grid == 3
69    };
70
71    return varname[which_grid];
72}
73
74void WARNING_CharSize(const variable *var)
75{
76    if ( var->v_nbdim == 0 )
77    {
78        if ( convert2int(var->v_dimchar) > 2400 )
79        {
80            printf("WARNING : The dimension of the character  %s   \n", var->v_nomvar);
81            printf("   is upper than 2400. You must change         \n");
82            printf("   the dimension of carray0                    \n");
83            printf("   in the file AGRIF/AGRIF_FILES/modtypes.F90  \n");
84            printf("   line 161. Replace 2400 with %d.              \n", convert2int(var->v_dimchar)+100);
85        }
86        Save_Length_int(convert2int(var->v_dimchar),1);
87    }
88    else if ( var->v_nbdim == 1 )
89    {
90        if ( convert2int(var->v_dimchar) > 200 )
91        {
92            printf("WARNING : The dimension of the character  %s   \n", var->v_nomvar);
93            printf("   is upper than 200. You must change          \n");
94            printf("   the dimension of carray1                    \n");
95            printf("   in the file AGRIF/AGRIF_FILES/modtypes.F90  \n");
96            printf("   line 162. Replace 200 with %d.              \n", convert2int(var->v_dimchar)+100);
97        }
98        Save_Length_int(convert2int(var->v_dimchar),2);
99    }
100    else if ( var->v_nbdim == 2 )
101    {
102        if ( convert2int(var->v_dimchar) > 200 )
103        {
104            printf("WARNING : The dimension of the character  %s   \n", var->v_nomvar);
105            printf("   is upper than 200. You must change          \n");
106            printf("   the dimension of carray2                    \n");
107            printf("   in the file AGRIF/AGRIF_FILES/modtypes.F90  \n");
108            printf("   line 163. Replace 200 with %d.              \n", convert2int(var->v_dimchar)+100);
109        }
110        Save_Length_int(convert2int(var->v_dimchar),3);
111    }
112    else if ( var->v_nbdim == 3 )
113    {
114        if ( convert2int(var->v_dimchar) > 200 )
115        {
116            printf("WARNING : The dimension of the character  %s   \n", var->v_nomvar);
117            printf("   is upper than 200. You must change          \n");
118            printf("   the dimension of carray3                    \n");
119            printf("   in the file AGRIF/AGRIF_FILES/modtypes.F90  \n");
120            printf("   line 164. Replace 200 with %d.              \n", convert2int(var->v_dimchar)+100);
121        }
122        Save_Length_int(convert2int(var->v_dimchar),4);
123    }
124}
125/******************************************************************************/
126/*                           vargridnametabvars                               */
127/******************************************************************************/
128/* This subroutine is used to create the string                               */
129/******************************************************************************/
130/*                                                                            */
131/*  if iorindice == 0 ----------->  Agrif_Gr % tabvars (i) % array1           */
132/*                                                                            */
133/*  if iorindice == 1 ----------->  Agrif_Gr % tabvars (12) % array1          */
134/*                                                                            */
135/******************************************************************************/
136const char *vargridnametabvars (const variable * var, int iorindice)
137{
138    static char tname_1[LONG_C];
139    static char tname_2[LONG_C];
140   
141    if ( iorindice == 0 ) sprintf(tname_1, "Agrif_Gr %% %s(%d)", tabvarsname(var), var->v_indicetabvars);
142    else                  sprintf(tname_1, "Agrif_Gr %% %s(i)",  tabvarsname(var));
143
144    if (!strcasecmp(var->v_typevar, "real"))
145    {
146        if      ( !strcasecmp(var->v_nameinttypename,"8") ) sprintf(tname_2, "%% darray%d", var->v_nbdim);
147        else if ( !strcasecmp(var->v_nameinttypename,"4") ) sprintf(tname_2, "%% sarray%d", var->v_nbdim);
148        else if ( !strcasecmp(var->v_precision,"8") ) sprintf(tname_2, "%% darray%d", var->v_nbdim);
149        else if ( !strcasecmp(var->v_precision,"4") ) sprintf(tname_2, "%% sarray%d", var->v_nbdim);       
150        else 
151          {
152          sprintf(tname_2, "%% array%d",  var->v_nbdim);
153          }
154    }
155    else if (!strcasecmp(var->v_typevar, "integer"))
156    {
157        sprintf(tname_2, "%% iarray%d", var->v_nbdim);
158    }
159    else if (!strcasecmp(var->v_typevar, "logical"))
160    {
161        sprintf(tname_2, "%% larray%d", var->v_nbdim);
162    }
163    else if (!strcasecmp(var->v_typevar, "character"))
164    {
165        WARNING_CharSize(var);
166        if (!strcasecmp(var->v_dimchar  ,":") && var->v_nbdim == 0 )
167        {
168        sprintf (tname_2, "%% carrayu");
169        } else {
170        sprintf (tname_2, "%% carray%d", var->v_nbdim);
171        }
172    }
173
174    strcat(tname_1, tname_2);
175    Save_Length(tname_1, 46);
176
177    return tname_1;
178}
179
180/******************************************************************************/
181/*                           vargridcurgridtabvars                            */
182/******************************************************************************/
183/* This subroutine is used to create the string                               */
184/******************************************************************************/
185/*                                                                            */
186/* if which_grid == 0 -->  Agrif_Curgrid % tabvars (i) % array1               */
187/*                                                                            */
188/* if which_grid == 1 -->  Agrif_tabvars (i) % parent_var % array1            */
189/*                                                                            */
190/* if which_grid == 2 -->  Agrif_Gr % tabvars (i) % array1                    */
191/*                                                                            */
192/******************************************************************************/
193const char *vargridcurgridtabvars(const variable *var, int which_grid)
194{
195    static char tname_1[LONG_C];
196    static char tname_2[LONG_C];
197
198    if (!strcasecmp(var->v_typevar,"type"))
199    {
200        sprintf(tname_1, "Agrif_%s_var(Agrif_Curgrid%%fixedrank)%%%s", var->v_modulename, var->v_nomvar);
201    }
202    else
203    {
204        sprintf(tname_1, variablecurgridtabvars(which_grid), tabvarsname(var), var->v_indicetabvars);
205
206        if (!strcasecmp(var->v_typevar, "REAL"))
207        {
208            if      ( !strcasecmp(var->v_nameinttypename,"8") ) sprintf(tname_2, "darray%d", var->v_nbdim);
209            else if ( !strcasecmp(var->v_nameinttypename,"4") ) sprintf(tname_2, "sarray%d", var->v_nbdim);
210            else if ( !strcasecmp(var->v_precision,"8") ) sprintf(tname_2, "darray%d", var->v_nbdim);
211            else if ( !strcasecmp(var->v_precision,"4") ) sprintf(tname_2, "sarray%d", var->v_nbdim);
212            else sprintf(tname_2, "array%d", var->v_nbdim);
213        }
214        else if (!strcasecmp(var->v_typevar, "INTEGER"))
215        {
216            sprintf(tname_2, "iarray%d", var->v_nbdim);
217        }
218        else if (!strcasecmp(var->v_typevar, "LOGICAL"))
219        {
220            sprintf(tname_2, "larray%d", var->v_nbdim);
221        }
222        else if (!strcasecmp(var->v_typevar, "CHARACTER"))
223        {
224            WARNING_CharSize(var);
225            sprintf(tname_2, "carray%d", var->v_nbdim);
226     
227
228            if (!strcasecmp(var->v_dimchar  ,":") && var->v_nbdim == 0 )
229            {
230            sprintf (tname_2, "carrayu");
231            } else {
232            sprintf (tname_2, "carray%d", var->v_nbdim);
233            }
234
235        }
236
237
238
239        if (var->v_pointerdeclare)
240        {
241                strcat(tname_1,"%p");
242                strcat(tname_1, tname_2);
243        }
244        else
245        {
246                strcat(tname_1,"%");
247                strcat(tname_1, tname_2);
248        }
249    }
250    Save_Length(tname_1, 46);
251
252    return tname_1;
253}
254
255/******************************************************************************/
256/*                  vargridcurgridtabvarswithoutAgrif_Gr                      */
257/******************************************************************************/
258/* This subroutine is used to create the string                               */
259/******************************************************************************/
260/*                                                                            */
261/******************************************************************************/
262const char *vargridcurgridtabvarswithoutAgrif_Gr(const variable *var)
263{
264    static char tname_1[LONG_C];
265    static char tname_2[LONG_C];
266
267    sprintf(tname_1, "(%d)", var->v_indicetabvars);
268   
269        if (!strcasecmp(var->v_typevar, "REAL"))
270        {
271            if      ( !strcasecmp(var->v_nameinttypename,"8") ) sprintf(tname_2, "darray%d", var->v_nbdim);
272            else if ( !strcasecmp(var->v_nameinttypename,"4") ) sprintf(tname_2, "sarray%d", var->v_nbdim);
273            else if ( !strcasecmp(var->v_precision,"8") ) sprintf(tname_2, "darray%d", var->v_nbdim);
274            else if ( !strcasecmp(var->v_precision,"4") ) sprintf(tname_2, "sarray%d", var->v_nbdim);
275            else sprintf(tname_2, "array%d", var->v_nbdim);
276        }
277        else if (!strcasecmp(var->v_typevar, "INTEGER"))
278        {
279            sprintf(tname_2, "iarray%d", var->v_nbdim);
280        }
281        else if (!strcasecmp(var->v_typevar, "LOGICAL"))
282        {
283            sprintf(tname_2, "larray%d", var->v_nbdim);
284        }
285        else if (!strcasecmp(var->v_typevar, "CHARACTER"))
286        {
287            WARNING_CharSize(var);
288            sprintf(tname_2, "carray%d", var->v_nbdim);
289        }
290        if (var->v_pointerdeclare)
291        {
292                strcat(tname_1,"%p");
293                strcat(tname_1, tname_2);
294        }
295        else
296        {
297                strcat(tname_1,"%");
298                strcat(tname_1, tname_2);
299        }
300
301    Save_Length(tname_1, 46);
302
303    return tname_1;
304}
305
306/******************************************************************************/
307/*                               vargridparam                                 */
308/******************************************************************************/
309/* This subroutine is used to create the string which contains                */
310/* dimension list                                                             */
311/******************************************************************************/
312/*                                                                            */
313/*  DIMENSION(jpi,0:jpj) ----------->"1:jpi,0:jpj"                            */
314/*                                                                            */
315/******************************************************************************/
316const char * vargridparam(const variable *var)
317{
318    typedim dim;
319    listdim *newdim;
320    char newname[LONG_M];
321
322    newdim = var->v_dimension;
323    if (!newdim) return "";
324
325    strcpy (tmpvargridname, "(");
326    while (newdim)
327    {
328        dim = newdim->dim;
329        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(dim.first,List_Global_Var));
330        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(newname,List_Common_Var));
331        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(newname,List_ModuleUsed_Var));
332        strcat(tmpvargridname, newname);
333        strcat(tmpvargridname, " : ");
334        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(dim.last,List_Global_Var));
335        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(newname, List_Common_Var));
336        strcpy(newname,ChangeTheInitalvaluebyTabvarsName(newname, List_ModuleUsed_Var));
337        strcat(tmpvargridname, newname);
338        newdim = newdim->suiv;
339        if (newdim) strcat(tmpvargridname, ",");
340    }
341    strcat(tmpvargridname, ")\0");
342    Save_Length(tmpvargridname,40);
343    return tmpvargridname;
344}
345
346/******************************************************************************/
347/*                        write_probdimagrif_file                             */
348/******************************************************************************/
349/* This subroutine is used to create the file probdim_agrif.h                 */
350/******************************************************************************/
351/*                                                                            */
352/*               probdim_agrif.h                                              */
353/*                                                                            */
354/*               Agrif_probdim = <number>                                     */
355/*                                                                            */
356/******************************************************************************/
357void write_probdimagrif_file()
358{
359  FILE *probdim;
360  char ligne[LONG_M];
361
362  probdim = open_for_write("probdim_agrif.h");
363  sprintf (ligne, "Agrif_Probdim = %d", dimprob);
364  tofich (probdim, ligne,1);
365  fclose (probdim);
366}
367
368/******************************************************************************/
369/*                             write_keysagrif_file                           */
370/******************************************************************************/
371/* This subroutine is used to create the file keys_agrif.h                    */
372/******************************************************************************/
373/*                                                                            */
374/*               keys_agrif.h                                                 */
375/*                                                                            */
376/*               AGRIF_USE_FIXED_GRIDS = 0                                    */
377/*               AGRIF_USE_ONLY_FIXED_GRIDS = 0                               */
378/*               AGRIF_USE_(ONLY)_FIXED_GRIDS = 1                             */
379/*                                                                            */
380/******************************************************************************/
381void write_keysagrif_file()
382{
383  FILE *keys;
384
385  keys = open_for_write("keys_agrif.h");
386  fprintf(keys,"      AGRIF_USE_FIXED_GRIDS = %d\n", fixedgrids);
387  fprintf(keys,"      AGRIF_USE_ONLY_FIXED_GRIDS = %d\n", onlyfixedgrids);
388  fclose(keys);
389}
390
391/******************************************************************************/
392/*                      write_modtypeagrif_file                               */
393/******************************************************************************/
394/* This subroutine is used to create the file typedata                        */
395/******************************************************************************/
396/*                                                                            */
397/*               modtype_agrif.h                                              */
398/*                                                                            */
399/*               Agrif_NbVariables =                                          */
400/*                                                                            */
401/******************************************************************************/
402void write_modtypeagrif_file()
403{
404  char ligne[LONG_M];
405  FILE *typedata;
406  int i;
407
408  typedata = open_for_write("modtype_agrif.h");
409  /* AGRIF_NbVariables : number of variables                                  */
410  for (i=0;i<NB_CAT_VARIABLES;i++)
411   {
412    sprintf (ligne, "Agrif_NbVariables(%d) = %d",i,indicemaxtabvars[i]);
413    tofich(typedata,ligne,1);
414   }
415  fclose (typedata);
416}
417
418/******************************************************************************/
419/*                   write_createvarnameagrif_file                            */
420/******************************************************************************/
421/* This subroutine is used to create the file  createvarname                  */
422/******************************************************************************/
423/*                                                                            */
424/*    Agrif_Gr % tabvars (i) % namevar = "variable"                           */
425/*                                                                            */
426/******************************************************************************/
427void write_createvarnameagrif_file(variable *v,FILE *createvarname, int *InitEmpty)
428{
429    char ligne[LONG_M];
430
431    *InitEmpty = 0 ;
432    sprintf(ligne, "Agrif_Gr %% %s(%d) %% namevar = \"%s\"",tabvarsname(v),v->v_indicetabvars,v->v_nomvar);
433    tofich(createvarname,ligne,1);
434}
435
436/******************************************************************************/
437/*                        write_Setnumberofcells_file                         */
438/******************************************************************************/
439/* This subroutine is used to create the file  setnumberofcells               */
440/******************************************************************************/
441/*                                                                            */
442/*              Agrif_Gr % n(i) = nbmailles                                   */
443/*                                                                            */
444/******************************************************************************/
445void write_Setnumberofcells_file()
446{
447    char ligne[LONG_VNAME];
448    char cformat[LONG_VNAME];
449    FILE *setnumberofcells;
450
451    if ( IndicenbmaillesX == 0 )  return;
452
453    setnumberofcells = open_for_write("SetNumberofcells.h");
454
455    if ( onlyfixedgrids == 1 )
456        strcpy(cformat, "Agrif_Gr %% nb(%d) = Agrif_Curgrid %% tabvars_i(%d) %% iarray0");
457    else
458        strcpy(cformat, "Agrif_Gr %% nb(%d) = Agrif_Gr %% tabvars_i(%d) %% iarray0");
459
460    sprintf(ligne, cformat, 1, IndicenbmaillesX);
461    tofich(setnumberofcells, ligne, 1);
462
463    if ( dimprob > 1 )
464    {
465        sprintf(ligne, cformat, 2, IndicenbmaillesY);
466        tofich(setnumberofcells, ligne, 1);
467    }
468    if ( dimprob > 2 )
469    {
470        sprintf(ligne, cformat, 3, IndicenbmaillesZ);
471        tofich(setnumberofcells, ligne, 1);
472    }
473    fclose(setnumberofcells);
474}
475
476/******************************************************************************/
477/*                       write_Getnumberofcells_file                          */
478/******************************************************************************/
479/* This subroutine is used to create the file  getnumberofcells               */
480/******************************************************************************/
481/*                                                                            */
482/*              nbmailles = Agrif_Gr % n(i)                                   */
483/*                                                                            */
484/******************************************************************************/
485void write_Getnumberofcells_file()
486{
487    char ligne[LONG_VNAME];
488    char cformat[LONG_VNAME];
489    FILE *getnumberofcells;
490
491    if ( IndicenbmaillesX == 0 )    return;
492
493    strcpy(cformat, "Agrif_Curgrid %% tabvars_i(%d) %% iarray0 = Agrif_Gr %% nb(%d)");
494
495    getnumberofcells = open_for_write("GetNumberofcells.h");
496
497    sprintf(ligne, cformat, IndicenbmaillesX, 1);
498    tofich(getnumberofcells, ligne, 1);
499
500    if (dimprob > 1)
501    {
502        sprintf(ligne, cformat, IndicenbmaillesY, 2);
503        tofich(getnumberofcells, ligne,1);
504    }
505    if (dimprob > 2)
506    {
507        sprintf(ligne, cformat, IndicenbmaillesZ, 3);
508        tofich(getnumberofcells, ligne,1);
509    }
510    fclose(getnumberofcells);
511}
512
513
514/******************************************************************************/
515/*                      write_initialisationsagrif_file                       */
516/******************************************************************************/
517/* This subroutine is used to create the file initproc                        */
518/******************************************************************************/
519/*                                                                            */
520/*              ! variable                                                    */
521/*              Agrif_Gr % tabvars(i) % nbdim = 1                             */
522/*                                                                            */
523/******************************************************************************/
524void write_initialisationsagrif_file(variable *v,FILE *initproc,int *VarnameEmpty)
525{
526    char ligne[LONG_M];
527
528    if ( v->v_nbdim != 0 )
529    {
530        *VarnameEmpty = 0 ;
531        sprintf(ligne,"Agrif_Mygrid %% %s(%d) %% nbdim = %d", tabvarsname(v), v->v_indicetabvars, v->v_nbdim);
532        tofich (initproc, ligne,1);
533    }
534}
535
536
537void Write_Alloc_Agrif_Files()
538{
539   listnom *parcours;
540   FILE *alloccalls;
541   FILE *AllocUSE;
542
543   AllocUSE= open_for_write("include_use_Alloc_agrif.h");
544   alloccalls = open_for_write("allocations_calls_agrif.h");
545
546   parcours = List_Subroutine_For_Alloc;
547   while ( parcours )
548   {
549      fprintf(AllocUSE,"      use %s, only: Alloc_agrif_%s\n", parcours -> o_nom, parcours -> o_nom );
550      fprintf (alloccalls,"      call Alloc_agrif_%s(Agrif_Gr)\n", parcours -> o_nom );
551      parcours = parcours -> suiv;
552   }
553
554   fclose (AllocUSE);
555   fclose (alloccalls);
556}
557
558int IndiceInlist(int indic, listindice *listin)
559{
560   listindice *parcoursindic;
561   int out;
562
563   out = 0 ;
564
565   parcoursindic = listin;
566   while ( parcoursindic && out == 0 )
567   {
568      if ( parcoursindic->i_indice == indic ) out = 1;
569      else parcoursindic = parcoursindic -> suiv;
570   }
571
572   return out;
573}
574
575void write_allocation_Common_0()
576{
577    listnom *parcours_nom;
578    listnom *neededparameter;
579    listvar *parcours;
580    listvar *parcoursprec;
581    listvar *parcours1;
582    listname *parcours_name;
583    listname *parcours_name_array;
584    listdoloop *parcours_loop;
585    FILE *allocationagrif;
586    FILE *paramtoamr;
587    char ligne[LONG_M];
588    char ligne2[LONG_M];
589    char ligne3[LONG_M];
590    variable *v;
591    int IndiceMax;
592    int IndiceMin;
593    int compteur;
594    int out;
595    int indiceprec;
596    int ValeurMax;
597    char initialvalue[LONG_M];
598    listindice **list_indic;
599    listindice *parcoursindic;
600    int i;
601    int nb_initial;
602    int is_parameter_local;
603    int global_check;
604
605    parcoursprec = (listvar *) NULL;
606    parcours_nom = List_NameOfCommon;
607    ValeurMax = 2;
608    while ( parcours_nom  )
609    {
610        if ( parcours_nom->o_val == 1 )
611        {
612            /* Open the file to create the Alloc_agrif subroutine                */
613            sprintf(ligne,"alloc_agrif_%s.h",parcours_nom->o_nom);
614            allocationagrif = open_for_write(ligne);
615
616            fprintf(allocationagrif,"#include \"Param_toamr_%s.h\" \n", parcours_nom->o_nom);
617            sprintf(ligne,"Param_toamr_%s.h",parcours_nom->o_nom);
618            paramtoamr = open_for_write(ligne);
619            neededparameter = (listnom *) NULL;
620            list_indic = (listindice **) calloc(NB_CAT_VARIABLES,sizeof(listindice *));
621
622             shouldincludempif = 1 ;
623            parcours = List_Common_Var;
624            while ( parcours )
625            {
626                if ( !strcasecmp(parcours->var->v_commonname,parcours_nom->o_nom) &&
627                    IndiceInlist(parcours->var->v_indicetabvars,list_indic[parcours->var->v_catvar]) == 0 )
628                {
629                    v = parcours->var;
630                    IndiceMax = 0;
631                    IndiceMin = indicemaxtabvars[v->v_catvar];
632                    /* body of the file */
633                    if ( !strcasecmp(v->v_commoninfile,cur_filename) )
634                    {
635                        if ( (onlyfixedgrids != 1) && (v->v_nbdim != 0) )
636                        {
637                            sprintf(ligne,"if (.not. allocated(%s)) then", vargridnametabvars(v,0));
638                            tofich(allocationagrif,ligne,1);
639                        }
640                        if ( (v->v_allocatable != 1) && (v->v_dimsempty != 1) )
641                        {
642                            /*                ALLOCATION                                          */
643                            if ( v->v_dimension != 0 )
644                            {
645                                if ( v->v_indicetabvars < IndiceMin || v->v_indicetabvars > IndiceMax )
646                                {
647                                    parcours1 = parcours;
648                                    compteur = -1;
649                                    out = 0;
650                                    indiceprec = parcours->var->v_indicetabvars -1 ;
651                                    while ( parcours1 && out == 0
652                                        && !strcasecmp(parcours->var->v_readedlistdimension,parcours1->var->v_readedlistdimension)
653                                        && !strcasecmp(parcours->var->v_typevar,            parcours1->var->v_typevar)
654                                        && (parcours1->var->v_indicetabvars == indiceprec+1) )
655                                    {
656                                        if ( !strcasecmp(parcours1->var->v_modulename,parcours_nom->o_nom) ||
657                                             !strcasecmp(parcours1->var->v_commonname,parcours_nom->o_nom) )
658                                        {
659                                            compteur = compteur +1 ;
660                                            indiceprec = parcours1->var->v_indicetabvars;
661                                            parcoursprec = parcours1;
662                                            parcours1 = parcours1->suiv;
663                                        }
664                                        else out = 1;
665                                    }
666                                    sprintf(ligne,"!! ALLOCATION OF VARIABLE : %s",v->v_nomvar);
667                                    tofich(allocationagrif,ligne,1);
668                                    if ( compteur > ValeurMax )
669                                    {
670                                        sprintf(ligne,"do i = %d,%d", parcours->var->v_indicetabvars,
671                                                                      parcours->var->v_indicetabvars+compteur);
672                                        tofich(allocationagrif,ligne,1);
673                                        IndiceMin = parcours->var->v_indicetabvars;
674                                        IndiceMax = parcours->var->v_indicetabvars+compteur;
675                                        sprintf(ligne,"    if (.not. allocated(%s)) allocate(%s", vargridnametabvars(v,1), vargridnametabvars(v,1));
676                                        sprintf(ligne2,"%s)", vargridparam(v));
677                                        strcat(ligne,ligne2);
678                                        tofich(allocationagrif,ligne,1);
679                                        tofich(allocationagrif,"enddo",1);
680                                        i = parcours->var->v_indicetabvars;
681                                        do
682                                        {
683                                            parcoursindic =  (listindice *)calloc(1,sizeof(listindice));
684                                            parcoursindic -> i_indice = i;
685                                            parcoursindic -> suiv = list_indic[parcours->var->v_catvar];
686                                            list_indic[parcours->var->v_catvar] = parcoursindic;
687                                            i = i + 1;
688                                        } while ( i <= parcours->var->v_indicetabvars+compteur );
689                                        parcours = parcoursprec;
690                                    }
691                                    else
692                                    {
693                                        sprintf(ligne,"if (.not. allocated(%s)) allocate(%s", vargridnametabvars(v,0), vargridnametabvars(v,0));
694                                        sprintf(ligne2,"%s)", vargridparam(v));
695                                        strcat(ligne,ligne2);
696                                        tofich(allocationagrif,ligne,1);
697                                        parcoursindic =  (listindice *) calloc(1,sizeof(listindice));
698                                        parcoursindic -> i_indice = parcours->var->v_indicetabvars;
699                                        parcoursindic -> suiv = list_indic[parcours->var->v_catvar];
700                                        list_indic[parcours->var->v_catvar] = parcoursindic;
701                                    }
702
703                                    global_check = 0;
704                                    is_parameter_local = writedeclarationintoamr(List_Parameter_Var,
705                                                        paramtoamr,v,parcours_nom->o_nom,&neededparameter,v->v_commonname,global_check);
706                                    if (is_parameter_local == 0)
707                                    {
708                                    global_check = 1;
709                                    is_parameter_local = writedeclarationintoamr(List_GlobalParameter_Var,
710                                                        paramtoamr,v,parcours_nom->o_nom,&neededparameter,v->v_commonname,global_check);
711                                    }
712                                }
713                            } /* end of the allocation part                                       */
714                            /*                INITIALISATION                                      */
715                            if ( v->v_initialvalue )
716                            {
717                            parcours_name = v->v_initialvalue;
718                            parcours_name_array = v->v_initialvalue_array;
719                            if (parcours_name_array)
720                            {
721                            while (parcours_name)
722                            {
723                                strcpy(ligne, vargridnametabvars(v,0));
724                                if (parcours_name_array)
725                                {
726                                if (strcasecmp(parcours_name_array->n_name,"") )
727                                {
728                                sprintf(ligne2,"(%s)",parcours_name_array->n_name);
729                                strcat(ligne,ligne2);
730                                }
731                                }
732                                /* We should modify the initialvalue in the case of variable has been defined with others variables */
733                                strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(parcours_name->n_name,List_Global_Var));
734                                if ( !strcasecmp(initialvalue,parcours_name->n_name) )
735                                {
736                                    strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(parcours_name->n_name,List_Common_Var));
737                                }
738                                if ( !strcasecmp(initialvalue,parcours_name->n_name) )
739                                {
740                                    strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(parcours_name->n_name,List_ModuleUsed_Var));
741                                }
742                                strcat (ligne," = ");
743
744                                if (v->v_nbdim >= 0)
745                                {
746                                    strcpy(ligne2,initialvalue);
747                                }
748                                else
749                                {
750                                    sprintf(ligne2,"reshape(%s,shape(%s))",initialvalue,vargridnametabvars(v,0));
751                                }
752                                strcat(ligne,ligne2);
753                                tofich(allocationagrif,ligne,1);
754                             
755                             parcours_name = parcours_name->suiv;
756                             if (parcours_name_array) parcours_name_array = parcours_name_array->suiv;
757                            }
758                            }
759                            else
760                            {
761                            strcpy(ligne, vargridnametabvars(v,0));
762                            strcat (ligne," = ");
763                            strcpy(ligne2,"");
764                            nb_initial = 0;
765                           
766                            while (parcours_name)
767                            {
768                            nb_initial = nb_initial + 1;
769                                /* We should modify the initialvalue in the case of variable has been defined with others variables */
770                                strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(parcours_name->n_name,List_Global_Var));
771                                if ( !strcasecmp(initialvalue,parcours_name->n_name) )
772                                {
773                                    strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(parcours_name->n_name,List_Common_Var));
774                                }
775                                if ( !strcasecmp(initialvalue,parcours_name->n_name) )
776                                {
777                                    strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(parcours_name->n_name,List_ModuleUsed_Var));
778                                }
779
780                                strcat(ligne2,initialvalue);
781                             if (parcours_name->suiv)
782                             {
783                             strcat(ligne2,",");
784                             }
785                             
786                             parcours_name = parcours_name->suiv;
787                            }
788                            if (nb_initial > 1)
789                            {
790                            sprintf(ligne3,"reshape((/%s/),shape(%s))",ligne2,vargridnametabvars(v,0));
791                            }
792                            else
793                            {
794                            strcpy(ligne3,ligne2);
795                            }
796                            strcat(ligne,ligne3);
797                            tofich(allocationagrif,ligne,1);
798                            }
799                            }
800                        }
801                        if ( (onlyfixedgrids != 1) && (v->v_nbdim != 0) )
802                        {
803                            tofich(allocationagrif,"endif",1);
804                        }
805                    }
806                }
807                parcours = parcours -> suiv;
808            }
809            /* Close the file Alloc_agrif                                        */
810            fclose(allocationagrif);
811            fclose(paramtoamr);
812        }
813        parcours_nom = parcours_nom -> suiv;
814    }
815}
816
817void write_allocation_Global_0()
818{
819    listnom *parcours_nom;
820    listvar *parcours;
821    listvar *parcoursprec;
822    listvar *parcours1;
823    FILE *allocationagrif;
824    char ligne[LONG_M];
825    char ligne2[LONG_M];
826    char ligne3[LONG_M];
827    listname *parcours_name;
828    listname *parcours_name_array;
829    variable *v;
830    int IndiceMax;
831    int IndiceMin;
832    int compteur;
833    int out;
834    int indiceprec;
835    int ValeurMax;
836    char initialvalue[LONG_M];
837    int typeiswritten ;
838    int nb_initial;
839
840    parcoursprec = (listvar *) NULL;
841    parcours_nom = List_NameOfModule;
842    ValeurMax = 2;
843
844    while ( parcours_nom  )
845    {
846        if ( parcours_nom->o_val == 1 )
847        {
848            IndiceMax = 0;
849            IndiceMin = indicemaxtabvars[0];
850            /* Open the file to create the Alloc_agrif subroutine                */
851            sprintf(ligne,"alloc_agrif_%s.h",parcours_nom->o_nom);
852            allocationagrif = open_for_write(ligne);
853
854//             if ( ModuleIsDefineInInputFile(parcours_nom->o_nom) == 1 )
855//             {
856//                 /* add the call to initworkspace         */
857//                 tofich(allocationagrif,"if (.not. Agrif_Root() ) then",1);
858//                 tofich(allocationagrif,"#include \"GetNumberofcells.h\"\n",0);
859//                 tofich(allocationagrif,"else",1);
860//                 tofich(allocationagrif,"#include \"SetNumberofcells.h\"\n",0);
861//                 tofich(allocationagrif,"endif",1);
862//                 tofich(allocationagrif,"call Agrif_InitWorkspace",1);
863//             }
864
865            typeiswritten = 0;
866            parcours = List_Global_Var;
867            while ( parcours )
868            {
869                if ( !strcasecmp(parcours->var->v_modulename,parcours_nom->o_nom) &&
870                       parcours->var->v_VariableIsParameter == 0                  &&
871                       parcours->var->v_notgrid == 0  )
872                {
873                    v = parcours->var;
874                    IndiceMax = 0;
875                    IndiceMin = indicemaxtabvars[v->v_catvar];
876                    /* body of the file */
877                    if ( !strcasecmp(v->v_commoninfile,cur_filename) )
878                    {
879                        if ( (onlyfixedgrids != 1) && (v->v_nbdim != 0) )
880                        {
881                            sprintf(ligne,"if (.not. allocated(%s)) then", vargridnametabvars(v,0));
882                            tofich(allocationagrif,ligne,1);
883                        }
884                        if ( (v->v_allocatable != 1) && (v->v_dimsempty != 1) )
885                        {
886                            /*                ALLOCATION                                          */
887                            if ( v->v_dimension != 0 )
888                            {
889                                if ( v->v_indicetabvars < IndiceMin || v->v_indicetabvars > IndiceMax )
890                                {
891                                    parcours1 = parcours;
892                                    compteur = -1;
893                                    out = 0;
894                                    indiceprec = parcours->var->v_indicetabvars -1 ;
895                                    while ( parcours1 && out == 0
896                                        && !strcasecmp(parcours->var->v_readedlistdimension,parcours1->var->v_readedlistdimension)
897                                        && !strcasecmp(parcours->var->v_typevar,            parcours1->var->v_typevar)
898                                        && (parcours1->var->v_indicetabvars == indiceprec+1) )
899                                    {
900                                        if ( !strcasecmp(parcours1->var->v_modulename, parcours_nom->o_nom) ||
901                                             !strcasecmp(parcours1->var->v_commonname, parcours_nom->o_nom) )
902                                        {
903                                            compteur = compteur +1 ;
904                                            indiceprec = parcours1->var->v_indicetabvars;
905                                            parcoursprec = parcours1;
906                                            parcours1 = parcours1->suiv;
907                                        }
908                                        else out = 1;
909                                    }
910                                    sprintf(ligne,"!! ALLOCATION OF VARIABLE : %s",v->v_nomvar);
911                                    tofich(allocationagrif,ligne,1);
912                                    if ( compteur > ValeurMax )
913                                    {
914                                        sprintf(ligne,"do i = %d,%d", parcours->var->v_indicetabvars,
915                                                                      parcours->var->v_indicetabvars+compteur);
916                                        tofich(allocationagrif,ligne,1);
917                                        IndiceMin = parcours->var->v_indicetabvars;
918                                        IndiceMax = parcours->var->v_indicetabvars+compteur;
919                                        sprintf(ligne,"    if (.not. allocated(%s)) allocate(%s", vargridnametabvars(v,1), vargridnametabvars(v,1));
920                                        sprintf(ligne2,"%s)", vargridparam(v));
921                                        strcat(ligne,ligne2);
922                                        tofich(allocationagrif,ligne,1);
923                                        tofich(allocationagrif,"enddo",1);
924                                        parcours = parcoursprec;
925                                    }
926                                    else
927                                    {
928                                        sprintf(ligne,"if (.not. allocated(%s)) allocate(%s", vargridnametabvars(v,0), vargridnametabvars(v,0));
929                                        sprintf(ligne2,"%s)", vargridparam(v));
930                                        strcat(ligne,ligne2);
931                                        tofich(allocationagrif,ligne,1);
932                                    }
933                                }
934                            } /* end of the allocation part                                       */
935                            /*                INITIALISATION                                      */
936
937               if ( v->v_initialvalue )
938                            {
939                            parcours_name = v->v_initialvalue;
940                            parcours_name_array = v->v_initialvalue_array;
941                            if (parcours_name_array)
942                            {
943                            while (parcours_name)
944                            {
945                                strcpy(ligne, vargridnametabvars(v,0));
946                                if (parcours_name_array)
947                                {
948                                if (strcasecmp(parcours_name_array->n_name,"") )
949                                {
950                                sprintf(ligne2,"(%s)",parcours_name_array->n_name);
951                                strcat(ligne,ligne2);
952                                }
953                                }
954                                /* We should modify the initialvalue in the case of variable has been defined with others variables */
955                                strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(parcours_name->n_name,List_Global_Var));
956                                if ( !strcasecmp(initialvalue,parcours_name->n_name) )
957                                {
958                                    strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(parcours_name->n_name,List_Common_Var));
959                                }
960                                if ( !strcasecmp(initialvalue,parcours_name->n_name) )
961                                {
962                                    strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(parcours_name->n_name,List_ModuleUsed_Var));
963                                }
964                                strcat (ligne," = ");
965
966                                if (v->v_nbdim >= 0)
967                                {
968                                    strcpy(ligne2,initialvalue);
969                                }
970                                else
971                                {
972                                    sprintf(ligne2,"reshape(%s,shape(%s))",initialvalue,vargridnametabvars(v,0));
973                                }
974                                strcat(ligne,ligne2);
975                                tofich(allocationagrif,ligne,1);
976                             
977                             parcours_name = parcours_name->suiv;
978                             if (parcours_name_array) parcours_name_array = parcours_name_array->suiv;
979                            }
980                            }
981                            else
982                            {
983                            strcpy(ligne, vargridnametabvars(v,0));
984                            strcat (ligne," = ");
985                            strcpy(ligne2,"");
986                            nb_initial = 0;
987                           
988                            while (parcours_name)
989                            {
990                            nb_initial = nb_initial + 1;
991                                /* We should modify the initialvalue in the case of variable has been defined with others variables */
992                                strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(parcours_name->n_name,List_Global_Var));
993                                if ( !strcasecmp(initialvalue,parcours_name->n_name) )
994                                {
995                                    strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(parcours_name->n_name,List_Common_Var));
996                                }
997                                if ( !strcasecmp(initialvalue,parcours_name->n_name) )
998                                {
999                                    strcpy(initialvalue,ChangeTheInitalvaluebyTabvarsName(parcours_name->n_name,List_ModuleUsed_Var));
1000                                }
1001
1002                                strcat(ligne2,initialvalue);
1003                             if (parcours_name->suiv)
1004                             {
1005                             strcat(ligne2,",");
1006                             }
1007                             
1008                             parcours_name = parcours_name->suiv;
1009                            }
1010                            if (nb_initial > 1)
1011                            {
1012                            sprintf(ligne3,"reshape((/%s/),shape(%s))",ligne2,vargridnametabvars(v,0));
1013                            }
1014                            else
1015                            {
1016                            strcpy(ligne3,ligne2);
1017                            }
1018                            strcat(ligne,ligne3);
1019                            tofich(allocationagrif,ligne,1);
1020                            }
1021                            }
1022                        }
1023                        /* Case of structure types */
1024                        if ( (typeiswritten == 0) && !strcasecmp(v->v_typevar,"type") )
1025                        {
1026                            sprintf(ligne,"if (.not. allocated(Agrif_%s_var)) then",v->v_modulename);
1027                            tofich(allocationagrif, ligne, 1);
1028                            sprintf(ligne,"    allocate(Agrif_%s_var(0:Agrif_NbMaxGrids))",v->v_modulename);
1029                            tofich(allocationagrif, ligne, 1);
1030                            tofich(allocationagrif, "endif", 1);
1031                            typeiswritten = 1;
1032                        }
1033                        if ( (onlyfixedgrids != 1) && (v->v_nbdim != 0) )
1034                        {
1035                            tofich(allocationagrif,"endif",1);
1036                        }
1037                    }
1038                }
1039                parcours = parcours -> suiv;
1040            }
1041            if ( ModuleIsDefineInInputFile(parcours_nom->o_nom) == 1 )
1042            {
1043                fprintf(allocationagrif, "      if ( .not.Agrif_Root() ) then\n");
1044                fprintf(allocationagrif, "#include \"GetNumberofcells.h\"\n");
1045                fprintf(allocationagrif, "      else\n");
1046                fprintf(allocationagrif, "#include \"SetNumberofcells.h\"\n");
1047                fprintf(allocationagrif, "      endif\n");
1048                fprintf(allocationagrif, "      call Agrif_InitWorkspace\n");
1049            }
1050            fclose(allocationagrif);
1051        }
1052        parcours_nom = parcours_nom -> suiv;
1053    }
1054}
1055
1056/******************************************************************************/
1057/*                           creefichieramr                                   */
1058/******************************************************************************/
1059/* This subroutine is the main one to create AGRIF_INC files                  */
1060/******************************************************************************/
1061/*                                                                            */
1062/******************************************************************************/
1063void creefichieramr ()
1064{
1065    listvar *newvar;
1066    variable *v;
1067    int erreur;
1068    char filefich[LONG_M];
1069
1070    int InitEmpty;
1071    int VarnameEmpty;
1072    int donotwrite;
1073
1074    FILE *initproc;
1075    FILE *initglobal;
1076    FILE *createvarname;
1077    FILE *createvarnameglobal;
1078
1079    if ( todebug == 1 ) printf("Enter in creefichieramr\n");
1080
1081    sprintf(filefich, "cd %s", include_dir);
1082    erreur = system (filefich);
1083    if (erreur)
1084    {
1085        sprintf(filefich, "mkdir -p %s", include_dir);
1086        system(filefich);
1087        printf("%s: Directory created\n", include_dir);
1088    }
1089
1090/******************************************************************************/
1091/******************** Creation of AGRIF_INC files *****************************/
1092/******************************************************************************/
1093
1094    if ( todebug == 1 )
1095    {
1096        const char *NameTampon = "toto";
1097        sprintf(filefich,"initialisations_agrif_%s.h", NameTampon);
1098        initproc = open_for_write(filefich);
1099
1100        sprintf(filefich,"createvarname_agrif_%s.h", NameTampon);
1101        createvarname = open_for_write(filefich);
1102
1103        InitEmpty = 1 ;
1104        VarnameEmpty = 1 ;
1105
1106        newvar = List_Global_Var;
1107        while ( newvar )
1108        {
1109            donotwrite = 0;
1110            v = newvar->var;
1111
1112            if ( ( v->v_common == 1 || v->v_module == 1 ) && donotwrite == 0 )
1113            {
1114                write_createvarnameagrif_file(v,createvarname,&VarnameEmpty);
1115                write_initialisationsagrif_file(v,initproc,&InitEmpty);
1116            }
1117            newvar = newvar->suiv;
1118        }
1119        fclose (createvarname);
1120        fclose (initproc);
1121
1122        if ( is_dependfile_created(curmodulename) == 0 )
1123        {
1124            if ( InitEmpty != 1  )
1125            {
1126                initglobal = open_for_append("initialisations_agrif.h");
1127                fprintf(initglobal,"#include \"initialisations_agrif_%s.h\"\n", NameTampon);
1128                fclose(initglobal);
1129            }
1130            if ( VarnameEmpty != 1 )
1131            {
1132                createvarnameglobal= open_for_append("createvarname_agrif.h");
1133                fprintf(createvarnameglobal,"#include \"createvarname_agrif_%s.h\"\n", NameTampon);
1134                fclose(createvarnameglobal);
1135            }
1136        }
1137    }
1138    write_allocation_Common_0();
1139    write_allocation_Global_0();
1140
1141    Write_Alloc_Agrif_Files();
1142    write_probdimagrif_file();
1143    write_keysagrif_file();
1144    write_modtypeagrif_file();
1145
1146    if ( NbMailleXDefined == 1 )
1147    {
1148        write_Setnumberofcells_file();
1149        write_Getnumberofcells_file();
1150    }
1151
1152    if ( todebug == 1 ) printf("Out of creefichieramr\n");
1153}
Note: See TracBrowser for help on using the repository browser.