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/CMEMS_2020/LIB – NEMO

source: vendors/AGRIF/CMEMS_2020/LIB/toamr.c @ 10088

Last change on this file since 10088 was 10088, checked in by rblod, 6 years ago

update conv

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