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.
SubLoopCreation.c in trunk/AGRIF/LIB – NEMO

source: trunk/AGRIF/LIB/SubLoopCreation.c @ 774

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

Update Agrif, see ticket:#39

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.6 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 <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include "decl.h"
39
40
41/******************************************************************************/
42/*     preparation and write of the argument list of a subroutine             */
43/******************************************************************************/
44
45
46/******************************************************************************/
47/*                             writeheadnewsub_0                              */
48/******************************************************************************/
49/* Firstpass 0                                                                */
50/* We should write the head of the subroutine sub_loop_<subroutinename>       */
51/******************************************************************************/
52/*                                                                            */
53/******************************************************************************/
54void writeheadnewsub_0()
55{
56   char ligne[LONG_C];
57
58   if ( firstpass == 0 && IsTabvarsUseInArgument_0() == 1 )
59   {
60      if ( todebug == 1 ) printf("Enter in writeheadnewsub_0\n");
61      /* we should add the use agrif_uti l if it is necessary                 */
62      WriteHeadofSubroutineLoop();
63      WriteUsemoduleDeclaration();
64      if ( ImplicitNoneInSubroutine() == 1 ) fprintf(fortranout,
65                                                       "      IMPLICIT NONE\n");
66      WriteIncludeDeclaration();
67      /*                                                                      */
68      /* We should write once the declaration of tables (extract              */
69      /*    from pointer) in the new subroutine                               */
70      if ( mark == 1 ) fprintf(fortranout,"!!! 000000000000000 \n");
71      if ( SubInList_ContainsSubroutine() == 0 ) WriteLocalParamDeclaration();
72      if ( mark == 1 ) fprintf(fortranout,"!!! 111111111111111 \n");
73
74      sprintf(ligne,"\n#include \"ParamFile%s.h\" \n",subroutinename);
75      tofich(fortranout,ligne,1);
76
77      WriteArgumentDeclaration_Sort();
78
79      if ( mark == 1 ) fprintf(fortranout,"!!! 222222222222222 \n");
80      writesub_loopdeclaration_tab(List_UsedInSubroutine_Var,fortranout);
81      if ( mark == 1 ) fprintf(fortranout,"!!! 333333333333333 \n");
82      writesub_loopdeclaration_scalar(List_UsedInSubroutine_Var,paramout);
83      if ( mark == 1 ) fprintf(fortranout,"!!! 444444444444444 \n");
84      /* now we should write the function declaration                         */
85      /*    case if it is the                                                 */
86      WriteFunctionDeclaration();
87      if ( mark == 1 ) fprintf(fortranout,"!!! 555555555555555 \n");
88      if ( SubInList_ContainsSubroutine() == 0 ) WriteSubroutineDeclaration(1);
89      if ( mark == 1 ) fprintf(fortranout,"!!! 666666666666666 \n");
90      if ( todebug == 1 ) printf("Out of writeheadnewsub_0\n");
91   }
92   else if ( firstpass == 0 )
93   {
94      AddUseAgrifUtil_0(fortranout);
95      WriteUsemoduleDeclaration();
96      WriteIncludeDeclaration();
97      if ( ImplicitNoneInSubroutine() == 1 ) fprintf(fortranout,
98                                                       "      IMPLICIT NONE\n");
99      if ( mark == 1 ) fprintf(fortranout,"!!! aaaaaaaaaaaaaaa \n");
100      WriteLocalParamDeclaration();
101      if ( mark == 1 ) fprintf(fortranout,"!!! bbbbbbbbbbbbbbb \n");
102      if ( functiondeclarationisdone == 0 ) WriteFunctionDeclaration();
103      WriteArgumentDeclaration_beforecall();
104/*      writesub_loopdeclaration_scalar(List_SubroutineArgument_Var,fortranout);
105      writesub_loopdeclaration_tab(List_SubroutineArgument_Var,fortranout);*/
106      if ( mark == 1 ) fprintf(fortranout,"!!! ccccccccccccccc \n");
107      if ( mark == 1 ) fprintf(fortranout,"!!! ddddddddddddddd \n");
108      WriteSubroutineDeclaration(1);
109      if ( mark == 1 ) fprintf(fortranout,"!!! eeeeeeeeeeeeeee \n");
110   }
111}
112
113
114/******************************************************************************/
115/*                    WriteVariablelist_subloop                               */
116/******************************************************************************/
117/* This subroutine is used to write the list of the variable which            */
118/* should be called by the sub_loop_<name> subroutine                         */
119/* The first part is composed by the list of the local variables              */
120/******************************************************************************/
121/*                                                                            */
122/*    List_SubroutineDeclaration_Var    a,b,c,  &                             */
123/*                                      d,e,f,  &                             */
124/*     a,b,c,d,e,f,g,h     ========>    g,h                                   */
125/*                                                                            */
126/******************************************************************************/
127void WriteVariablelist_subloop(FILE *outputfile)
128{
129   listvar *parcours;
130   char ligne[LONG_C];
131   int compteur;
132
133   if ( todebug == 1 ) printf("Enter in WriteVariablelist_subloop\n");
134   parcours = List_SubroutineArgument_Var;
135   didvariableadded = 0;
136   compteur = 0 ;
137
138   while ( parcours )
139   {
140      /* if the readed variable is a variable of the subroutine               */
141      /*    subroutinename we should write the name of this variable          */
142      /*    in the output file                                                */
143      if ( !strcasecmp(parcours->var->v_subroutinename,subroutinename) )
144      {
145         if ( didvariableadded == 0 )
146         {
147            strcpy(ligne,"");
148         }
149         else
150         {
151            if ( compteur == 0 ) strcpy(ligne,"");
152            strcat(ligne,",");
153         }
154         strcat(ligne,parcours->var->v_nomvar);
155         didvariableadded = 1;
156         compteur = compteur + 1;
157         if ( compteur == 3 )
158         {
159            if ( retour77 == 0 )
160            {
161               strcat(ligne," &");
162               fprintf(outputfile,"\n      %s",ligne);
163            }
164            else fprintf(outputfile,"\n     & %s",ligne);
165            compteur = 0;
166         }
167      }
168      parcours = parcours -> suiv;
169   }
170   parcours = List_FunctionType_Var;
171   while ( parcours )
172   {
173      if ( !strcasecmp(parcours->var->v_subroutinename,subroutinename) )
174      {
175         if ( didvariableadded == 0 )
176         {
177            strcpy(ligne,"");
178         }
179         else
180         {
181            if ( compteur == 0 ) strcpy(ligne,"");
182            strcat(ligne,",");
183         }
184         strcat(ligne,parcours->var->v_nomvar);
185         didvariableadded = 1;
186         compteur = compteur + 1;
187         if ( compteur == 3 )
188         {
189            if ( retour77 == 0 )
190            {
191               strcat(ligne," &");
192               fprintf(outputfile,"\n      %s",ligne);
193            }
194            else fprintf(outputfile,"\n     & %s",ligne);
195            compteur = 0;
196         }
197      }
198      parcours = parcours -> suiv;
199   }
200   if ( compteur != 3 && compteur != 0 )
201   {
202      if ( retour77 == 0 ) fprintf(outputfile,"\n      %s &",ligne);
203      else fprintf(outputfile,"\n     & %s ",ligne);
204   }
205   if ( todebug == 1 ) printf("Out of WriteVariablelist_subloop\n");
206}
207
208
209/******************************************************************************/
210/*                     WriteVariablelist_subloop_Call                         */
211/******************************************************************************/
212/* This subroutine is used to write the list of the variable which            */
213/* should be called by the sub_loop_<name> subroutine into the called         */
214/* The second part is composed by the list of the global table                */
215/******************************************************************************/
216/*                                                                            */
217/*   List_UsedInSubroutine_Var SubloopScalar = 0 | SubloopScalar = 1          */
218/*                                a,b,c,  &      |  a,b(1,1),c,      &        */
219/*     a,b,c,d,e,f,g,h  =====>    d,e,f,  &      |  d(1),e(1,1,1),f, &        */
220/*                                g,h            |  g,h(1,1)                  */
221/*                                                                            */
222/******************************************************************************/
223void WriteVariablelist_subloop_Call(FILE *outputfile)
224{
225   listvar *parcours;
226   char ligne[LONG_40M];
227   char ligne2[10];
228   int i;
229   int compteur ;
230
231   if ( todebug == 1 ) printf("Enter in WriteVariablelist_subloop_Call\n");
232   parcours = List_UsedInSubroutine_Var;
233   compteur = 0 ;
234   while ( parcours )
235   {
236      /* if the readed variable is a variable of the subroutine               */
237      /*    subroutinename we should write the name of this variable          */
238      /*    in the output file                                                */
239      if ( !strcasecmp(parcours->var->v_subroutinename,subroutinename)  &&
240           parcours->var->v_allocatable == 0                            &&
241           parcours->var->v_pointerdeclare == 0
242         )
243      {
244         if ( didvariableadded == 0 )
245         {
246            if ( retour77 == 1 ) strcpy(ligne,"\n     & ");
247            else strcpy(ligne,"\n      ");
248         }
249         else
250         {
251            if ( compteur == 0 )
252            {
253               if ( retour77 == 1 ) strcpy(ligne,"\n     & ");
254               else strcpy(ligne,"\n      ");
255            }
256            strcat(ligne," , ");
257         }
258         strcat(ligne,vargridcurgridtabvars(parcours->var,0));
259         /* if it is asked in the call of the conv we should give             */
260         /* scalar in argument, so we should put (1,1,1) after the            */
261         /* the name of the variable                                          */
262         if (  SubloopScalar != 0 &&
263               (IsVarAllocatable_0(parcours->var->v_nomvar) == 0 &&
264               parcours->var->v_pointerdeclare == 0 ) &&
265               parcours->var->v_nbdim != 0 )
266         {
267             i = 1;
268             while ( i <=  parcours->var->v_nbdim )
269             {
270                if ( i == 1 ) strcat(ligne,"( ");
271                if ( SubloopScalar == 2 )
272                {
273                   strcat(ligne,":");
274                   if ( i != parcours->var->v_nbdim ) strcat(ligne,",");
275                }
276                else
277                {
278                   strcat(ligne," lbound( ");
279                   strcat(ligne,vargridcurgridtabvars(parcours->var,0));
280                   strcat(ligne,",");
281                   strcpy(ligne2,"");
282                   sprintf(ligne2,"%d",i);
283                   strcat(ligne,ligne2);
284                   if ( i != parcours->var->v_nbdim ) strcat(ligne,"),");
285                }
286                if ( i == parcours->var->v_nbdim ) strcat(ligne,"))");
287                i++;
288             }
289         }
290         didvariableadded = 1;
291         compteur = compteur +1 ;
292         /*if ( retour77 == 0 )
293         {
294            strcat(ligne," &");
295            fprintf(outputfile,"\n");
296         }
297         else fprintf(outputfile,"\n     & ");*/
298         /*tofich(outputfile,ligne,0);*/
299      }
300      parcours = parcours -> suiv;
301   }
302   Save_Length(ligne,41);
303   tofich(outputfile,ligne,0);
304   /* Now we should replace the last ", &" by " &"                            */
305/*   if ( didvariableadded != 0 && retour77 == 0 ) fseek(outputfile,-1,SEEK_CUR);
306   if ( didvariableadded == 0 ) fseek(outputfile,-1,SEEK_CUR);*/
307   if ( todebug == 1 ) printf("Out of WriteVariablelist_subloop_Call\n");
308}
309
310
311/******************************************************************************/
312/*                       WriteVariablelist_subloop_Def                        */
313/******************************************************************************/
314/* This subroutine is used to write the list of the variable which            */
315/* should be called by the sub_loop_<name> subroutine into the def            */
316/* The second part is composed by the list of the global table                */
317/* <name>_tmp                                                                 */
318/******************************************************************************/
319/*                                                                            */
320/*       List_UsedInSubroutine_Var                                            */
321/*                                a-tmp,b-tmp,c_tmp, &                        */
322/*     a,b,c,d,e,f,g,h  =====>    d_tmp,e_tmp,f_tmp, &                        */
323/*                                g_tmp,h_tmp                                 */
324/*                                                                            */
325/******************************************************************************/
326void WriteVariablelist_subloop_Def(FILE *outputfile)
327{
328   listvar *parcours;
329   char ligne[LONG_40M];
330   int compteur;
331
332   if ( todebug == 1 ) printf("Enter in WriteVariablelist_subloop_Def\n");
333   parcours = List_UsedInSubroutine_Var;
334   compteur = 0 ;
335   while ( parcours )
336   {
337      /* if the readed variable is a variable of the subroutine               */
338      /*    subrotinename we should write the name of this variable           */
339      /*    in the output file                                                */
340      if ( !strcasecmp(parcours->var->v_subroutinename,subroutinename)  &&
341           parcours->var->v_allocatable == 0                            &&
342           parcours->var->v_pointerdeclare == 0
343         )
344      {
345         if ( didvariableadded == 0 )
346         {
347            if ( retour77 == 1 ) strcpy(ligne,"\n     &");
348            else strcpy(ligne,"\n      ");
349         }
350         else
351         {
352            if ( compteur == 0 )
353            {
354               if ( retour77 == 1 ) strcpy(ligne,"\n     & ");
355               else strcpy(ligne,"\n      ");
356            }
357            strcat(ligne,",");
358         }
359         strcat(ligne,parcours->var->v_nomvar);
360         compteur = compteur + 1;
361         didvariableadded = 1;
362/*         if ( compteur == 3 )
363         {
364            if ( retour77 == 0 )
365            {
366               strcat(ligne," &");
367               fprintf(outputfile,"\n      %s",ligne);
368            }
369            else fprintf(outputfile,"\n     & %s",ligne);
370            compteur = 0;
371         }*/
372      }
373      parcours = parcours -> suiv;
374   }
375 /*  if ( compteur != 3 && compteur != 0 )
376   {
377      if ( retour77 == 0 ) fprintf(outputfile,"\n      %s &",ligne);
378      else fprintf(outputfile,"\n     & %s",ligne);
379   }*/
380   Save_Length(ligne,41);
381   tofich(outputfile,ligne,0);
382
383   /* Now we should replace the last ", &" by " &"                            */
384  /* if ( didvariableadded != 0 && retour77 == 0 ) fseek(outputfile,-1,SEEK_CUR);
385   if ( didvariableadded == 0 ) fseek(outputfile,-1,SEEK_CUR);*/
386   if ( todebug == 1 ) printf("Out of WriteVariablelist_subloop_Def\n");
387}
388
389
390
391/******************************************************************************/
392/*                      WriteHeadofSubroutineLoop                             */
393/******************************************************************************/
394/* This subroutine is used to write the head of the subroutine                */
395/* Sub_Loop_<name>                                                            */
396/******************************************************************************/
397/*                 Sub_loop_subroutine.h                                      */
398/*                                                                            */
399/*                 subroutine Sub_Loop_subroutine ( &                         */
400/*                 a,b,c, &                                                   */
401/* SubLoopScalar   d,e(1,1),f(1,1,1), &                                       */
402/*                 g,h  &                                                     */
403/*                 )                                                          */
404/******************************************************************************/
405void WriteHeadofSubroutineLoop()
406{
407   char ligne[LONG_C];
408   FILE * subloop;
409
410   if ( todebug == 1 ) printf("Enter in WriteHeadofSubroutineLoop\n");
411   tofich(fortranout,"\n",1);
412   /* Open this newfile                                                       */
413   sprintf(ligne,"Sub_Loop_%s.h",subroutinename);
414   subloop = associate(ligne);
415   /*                                                                         */
416   if ( retour77 == 0 ) sprintf(ligne,"      subroutine Sub_Loop_%s( &"
417                                                               ,subroutinename);
418   else sprintf(ligne,"      subroutine Sub_Loop_%s( ",subroutinename);
419   fprintf(subloop,ligne);
420   /*                                                                         */
421   WriteVariablelist_subloop(subloop);
422   WriteVariablelist_subloop_Def(subloop);
423   /*                                                                         */
424   sprintf(ligne,")");
425   fprintf(subloop,ligne);
426   /* if USE agrif_Uti l should be add                                        */
427   AddUseAgrifUtil_0(subloop);
428   /*                                                                         */
429   oldfortranout = fortranout;
430   fortranout = subloop;
431   if ( todebug == 1 ) printf("Out of WriteHeadofSubroutineLoop\n");
432}
433
434/******************************************************************************/
435/*                closeandcallsubloopandincludeit_0                           */
436/******************************************************************************/
437/* Firstpass 0                                                                */
438/* We should close the sub_loop subroutine, call it and close the             */
439/* function (suborfun = 0)                                                    */
440/* subroutine (suborfun = 1)                                                  */
441/* end (suborfun = 2)                                                         */
442/* end program (suborfun = 3)                                                 */
443/* and include the sub_loop subroutine after                                  */
444/******************************************************************************/
445/*                                                                            */
446/******************************************************************************/
447void closeandcallsubloopandincludeit_0(int suborfun)
448{
449   char ligne[LONG_C];
450
451   if ( firstpass == 0 )
452   {
453   if ( todebug == 1 ) printf("Enter in closeandcallsubloopandincludeit_0\n");
454   if ( IsTabvarsUseInArgument_0() == 1 )
455   {
456      /* We should remove the key word end subroutine                         */
457      RemoveWordCUR_0(fortranout,(long)(-(pos_cur-pos_endsubroutine)),
458                                          pos_cur-pos_endsubroutine);
459      /* We should close the loop subroutine                                  */
460      sprintf(ligne,"\n      end subroutine Sub_Loop_%s",subroutinename);
461      tofich(fortranout,ligne,1);
462      fclose(fortranout);
463      fortranout = oldfortranout;
464      AddUseAgrifUtilBeforeCall_0(fortranout);
465      if ( functiondeclarationisdone == 0 ) WriteFunctionDeclaration();
466      WriteArgumentDeclaration_beforecall();
467      if ( !strcasecmp(subofagrifinitgrids,subroutinename) )
468                     fprintf(oldfortranout,"      Call Agrif_Init_Grids () \n");
469      /* Now we add the call af the new subroutine                            */
470      if ( retour77 == 0 ) sprintf(ligne,"\n      Call Sub_Loop_%s( &"
471                                                               ,subroutinename);
472      else sprintf(ligne,"\n      Call Sub_Loop_%s( ",subroutinename);
473      fprintf(fortranout,ligne);
474      /* Write the list of the local variables used in this new subroutine    */
475      WriteVariablelist_subloop(fortranout);
476      /* Write the list of the global tables used in this new subroutine      */
477      /*    in doloop                                                         */
478      WriteVariablelist_subloop_Call(fortranout);
479      /* Close the parenthesis of the new subroutine called                   */
480      sprintf(ligne,")");
481      fprintf(fortranout,ligne);
482      /* We should close the original subroutine                              */
483      if ( suborfun == 3 ) sprintf(ligne,"\n      end program %s"
484                                                               ,subroutinename);
485      if ( suborfun == 2 ) sprintf(ligne,"\n      end");
486      if ( suborfun == 1 ) sprintf(ligne,"\n      end subroutine %s"
487                                                               ,subroutinename);
488      if ( suborfun == 0 ) sprintf(ligne,"\n      end function %s"
489                                                               ,subroutinename);
490      tofich(fortranout,ligne,1);
491      /* we should include the above file in the original code                */
492      sprintf(ligne,"\n#include \"Sub_Loop_%s.h\" \n",subroutinename);
493      tofich(fortranout,ligne,1);
494      }
495   if ( todebug == 1 ) printf("Out of closeandcallsubloopandincludeit_0\n");
496   }
497}
498
499
500
501
502void closeandcallsubloop_contains_0()
503{
504   char ligne[LONG_C];
505
506   if ( firstpass == 0 )
507   {
508   if ( todebug == 1 ) printf("Enter in closeandcallsubloopandincludeit_0\n");
509   if ( IsTabvarsUseInArgument_0() == 1 )
510   {
511      Remove_Word_Contains_0();
512      sprintf(ligne,"\n      end subroutine Sub_Loop_%s",subroutinename);
513      tofich(fortranout,ligne,1);
514      fclose(fortranout);
515      fortranout = oldfortranout;
516      AddUseAgrifUtilBeforeCall_0(fortranout);
517      if ( ImplicitNoneInSubroutine() == 1 ) fprintf(fortranout,
518                                                       "      IMPLICIT NONE\n");
519      WriteLocalParamDeclaration();
520      if ( functiondeclarationisdone == 0 ) WriteFunctionDeclaration();
521      WriteArgumentDeclaration_beforecall();
522      WriteSubroutineDeclaration(0);
523      if ( !strcasecmp(subofagrifinitgrids,subroutinename) )
524                     fprintf(oldfortranout,"      Call Agrif_Init_Grids () \n");
525      /* Now we add the call af the new subroutine                            */
526      if ( retour77 == 0 ) sprintf(ligne,"\n      Call Sub_Loop_%s( &"
527                                                               ,subroutinename);
528      else sprintf(ligne,"\n      Call Sub_Loop_%s( ",subroutinename);
529      fprintf(fortranout,ligne);
530      /* Write the list of the local variables used in this new subroutine    */
531      WriteVariablelist_subloop(fortranout);
532      /* Write the list of the global tables used in this new subroutine      */
533      /*    in doloop                                                         */
534      WriteVariablelist_subloop_Call(fortranout);
535      /* Close the parenthesis of the new subroutine called                   */
536      sprintf(ligne,")");
537      tofich(fortranout,ligne,1);
538      /* We should close the original subroutine                              */
539      sprintf(ligne,"\n      contains");
540      tofich(fortranout,ligne,1);
541      /* we should include the above file in the original code                */
542      sprintf(ligne,"\n#include \"Sub_Loop_%s.h\" \n",subroutinename);
543      tofich(fortranout,ligne,1);
544      }
545   if ( todebug == 1 ) printf("Out of closeandcallsubloopandincludeit_0\n");
546   }
547}
Note: See TracBrowser for help on using the repository browser.