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 @ 530

Last change on this file since 530 was 530, checked in by opalod, 18 years ago

RB: update of the conv for IOM and NEC MPI library

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.0 KB
Line 
1/******************************************************************************/
2/*                                                                            */
3/*     CONV (converter) for Agrif (Adaptive Grid Refinement In Fortran)       */
4/*                                                                            */
5/* Copyright or © or Copr. Laurent Debreu (Laurent.Debreu@imag.fr)            */
6/*                        Cyril Mazauric (Cyril.Mazauric@imag.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.0                                                                */
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/* suborfunc = 1 from subroutine                                              */
52/* suborfunc = 2 from function                                                */
53/******************************************************************************/
54/*                                                                            */
55/******************************************************************************/
56void writeheadnewsub_0(int suborfunc)
57{
58   int out;
59   listusemodule *newmodule;
60
61   if ( firstpass == 0 && IsTabvarsUseInArgument_0() == 1 )
62   {
63      /* we should add the use agrif_util if it is necessary                  */
64      newmodule = listofmodulebysubroutine;
65      out = 0 ;
66      if ( adduseagrifutil != 1 && inmodulemeet == 0 )
67      {
68         while ( newmodule && out == 0 )
69         {
70             if ( !strcasecmp(newmodule->cursubroutine,subroutinename) ||
71                  !strcasecmp(newmodule->cursubroutine," ")
72                 )
73             {
74                if ( !strcasecmp(newmodule->charusemodule,"Agrif_Util") ) 
75                                                                        out = 1;
76             }
77             newmodule = newmodule ->suiv; 
78         }
79         if ( out == 0 ) tofich(fortranout,
80                             "\n      USE Agrif_Types, ONLY : Agrif_tabvars",1);
81      }
82      /* we should modify the name of the variable in the                     */
83      /* listvarindoloop which has been declared by the way of the            */
84      /* USE fortran  function : USE MOD, U => V                              */
85      if ( suborfunc == 1 ) ModifyThelistvarindoloop();
86      WriteHeadofSubroutineLoop();   
87      if ( IsAllocateInThisSubroutine_0() == 1 && inmodulemeet == 0 )
88           tofich(fortranout,"\n      USE Agrif_Types, ONLY : Agrif_tabvars",1);
89      else if ( out == 1 && fortran77 == 1 ) tofich(fortranout,
90                                                    "\n      USE Agrif_Util",1);
91      WriteUsemoduleDeclaration();
92      WriteIncludeDeclaration();
93      if ( ImplicitNoneInSubroutine() == 1 ) fprintf(fortranout,
94                                                       "      IMPLICIT NONE\n");
95      /* We should write once the declaration of tables (extract              */
96      /*    from pointer) in the new subroutine                               */
97      tmpdeclaration_everdone = 1;
98      if ( todebug == 1 ) fprintf(fortranout,"!!! 111111111111111 \n");
99      if ( fortran77 == 1 ) writesub_loopdeclaration
100                                                     (parameterlist,fortranout);
101      if ( todebug == 1 ) fprintf(fortranout,"!!! 222222222222222 \n");
102      writesub_loopdeclaration(listvarindoloop,fortranout);
103      if ( todebug == 1 ) fprintf(fortranout,"!!! 333333333333333 \n");
104      if ( fortran77 == 1 ) writesub_loopdeclaration
105                                                     (varsubroutine,fortranout);
106      if ( todebug == 1 ) fprintf(fortranout,"!!! 444444444444444 \n");
107      if ( fortran77 == 1 ) writesub_loopdeclaration
108                                              (varofsubroutineliste,fortranout);
109      if ( todebug == 1 ) fprintf(fortranout,"!!! 555555555555555 \n");
110      /* now we should write the function declaration                         */
111      /*    case if it is the                                                 */
112      writedeclaration (functionlistvar, fortranout,varofsubroutineliste);
113      if ( todebug == 1 ) fprintf(fortranout,"!!! 666666666666666 \n");
114   }
115   else if ( firstpass == 0 ) 
116   {
117      WriteUsemoduleDeclaration();
118      WriteIncludeDeclaration();
119   }
120}
121
122
123/******************************************************************************/
124/*                    writesubroutinedeclaration_0                            */
125/******************************************************************************/
126/* Firstpass 0                                                                */
127/* We should write the declaration of the subroutine in order to              */
128/* create the new sub_loop subroutine                                         */
129/******************************************************************************/
130/*                                                                            */
131/******************************************************************************/
132void writesubroutinedeclaration_0(listvar *listtomodify)
133{
134   if ( VariableIsParameter == 0 && SaveDeclare == 0)
135   {
136      if (firstpass == 0 && IsTabvarsUseInArgument_0() == 1 )
137      {
138         /* We should write this declaration into the original                */
139         /*    subroutine too                                                 */
140        if ( fortran77 == 1                 && 
141             paramdeclaration_everdone == 0 && 
142             varofsubroutineliste
143           )
144        {
145           paramdeclaration_everdone = 1;
146           writedeclarationsubroutinedeclaration
147                             (parameterlist,oldfortranout,varofsubroutineliste);
148        }
149        writedeclaration (listtomodify, oldfortranout,varofsubroutineliste);
150      }
151   }
152}
153
154/******************************************************************************/
155/*                    WriteVariablelist_subloop                               */
156/******************************************************************************/
157/* This subroutine is used to write the list of the variable which            */
158/* should be called by the sub_loop_<name> subroutine                         */
159/* The first part is composed by the list of the local variables              */
160/******************************************************************************/
161/*                                                                            */
162/*    varofsubroutineliste              a,b,c,  &                             */
163/*                                      d,e,f,  &                             */
164/*     a,b,c,d,e,f,g,h     ========>    g,h                                   */
165/*                                                                            */
166/******************************************************************************/
167void WriteVariablelist_subloop(FILE *outputfile)
168{
169   listvar *parcours;   
170   char ligne[LONGNOM];
171   int compteur;
172   
173   parcours = varofsubroutineliste;
174   didvariableadded = 0;
175   compteur = 0 ;
176   
177   while ( parcours )   
178   {
179      /* if the readed variable is a variable of the subroutine               */
180      /*    subroutinename we should write the name of this variable          */
181      /*    in the output file                                                */
182      if ( !strcasecmp(parcours->var->modulename,subroutinename) )
183      {     
184         if ( didvariableadded == 0 )
185         {
186            strcpy(ligne,"");
187         }
188         else
189         {
190            if ( compteur == 0 ) strcpy(ligne,"");
191            strcat(ligne,",");
192         }
193         strcat(ligne,parcours->var->nomvar);
194         didvariableadded = 1;
195         compteur = compteur + 1;
196         if ( compteur == 3 ) 
197         {
198            if ( fortran77 == 0 ) strcat(ligne," &");
199            if ( fortran77 == 0 ) fprintf(outputfile,"\n      %s",ligne);
200            else fprintf(outputfile,"\n     & %s",ligne);
201            compteur = 0;
202         }
203      }
204      parcours = parcours -> suiv;   
205   }
206   if ( compteur != 3 && compteur != 0 ) 
207   {
208      if ( fortran77 == 0 ) fprintf(outputfile,"\n      %s &",ligne);
209      else fprintf(outputfile,"\n     & %s ",ligne);
210   }
211}
212
213
214/******************************************************************************/
215/*                     WriteVariablelist_subloop_Call                         */
216/******************************************************************************/
217/* This subroutine is used to write the list of the variable which            */
218/* should be called by the sub_loop_<name> subroutine into the called         */
219/* The second part is composed by the list of the global table                */
220/******************************************************************************/
221/*                                                                            */
222/*      listvarindoloop        SubloopScalar = 0 | SubloopScalar = 1          */
223/*                                a,b,c,  &      |  a,b(1,1),c,      &        */
224/*     a,b,c,d,e,f,g,h  =====>    d,e,f,  &      |  d(1),e(1,1,1),f, &        */
225/*                                g,h            |  g,h(1,1)                  */
226/*                                                                            */
227/******************************************************************************/
228void  WriteVariablelist_subloop_Call(FILE *outputfile)
229{
230   listvar *parcours;   
231   char ligne[LONGNOM*100];
232   char ligne2[10];
233   int i;
234   
235   parcours = listvarindoloop;
236   while ( parcours )   
237   {
238      /* if the readed variable is a variable of the subroutine               */
239      /*    subroutinename we should write the name of this variable          */
240      /*    in the output file                                                */
241      if ( !strcasecmp(parcours->var->modulename,subroutinename) )
242      {
243         if ( didvariableadded == 0 )
244         {
245            strcpy(ligne,"");
246         }
247         else
248         {
249            strcpy(ligne,"");
250            strcat(ligne,",");
251         }
252         strcat(ligne,vargridcurgridtabvars(parcours->var,0));
253         /* if it is asked in the call of the conv we should give             */
254         /* scalar in argument, so we should put (1,1,1) after the            */
255         /* the name of the variable                                          */
256         if (  SubloopScalar != 0 && 
257               (IsVarAllocatable_0(parcours->var->nomvar) == 0 && 
258          parcours->var->pointerdeclare == 0 ) &&
259               parcours->var->nbdim != 0 )
260         {
261             i = 1;
262             while ( i <=  parcours->var->nbdim )
263             {
264                if ( i == 1 ) strcat(ligne,"( ");
265                if ( SubloopScalar == 2 )
266                {
267                   strcat(ligne,":");
268                   if ( i != parcours->var->nbdim ) strcat(ligne,",");
269                }
270                else
271                {
272                   strcat(ligne," lbound( ");
273                   strcat(ligne,vargridcurgridtabvars(parcours->var,0));
274                   strcat(ligne,",");
275                   strcpy(ligne2,"");
276                   sprintf(ligne2,"%d",i);
277                   strcat(ligne,ligne2);
278                   if ( i != parcours->var->nbdim ) strcat(ligne,"),");
279                }
280                if ( i == parcours->var->nbdim ) strcat(ligne,"))");
281                i++;
282             }
283         }
284         didvariableadded = 1;
285         if ( fortran77 == 0 ) strcat(ligne," &");
286         if ( fortran77 == 0 ) fprintf(outputfile,"\n");
287         else fprintf(outputfile,"\n     & ");
288         tofich(outputfile,ligne,0);
289      }
290      parcours = parcours -> suiv;   
291   }
292   /* Now we should replace the last ", &" by " &"                            */
293   if ( didvariableadded != 0 && fortran77 == 0 ) fseek(outputfile,-1,SEEK_CUR);
294   if ( didvariableadded == 0 ) fseek(outputfile,-2,SEEK_CUR);
295}
296
297
298/******************************************************************************/
299/*                       WriteVariablelist_subloop_Def                        */
300/******************************************************************************/
301/* This subroutine is used to write the list of the variable which            */
302/* should be called by the sub_loop_<name> subroutine into the def            */
303/* The second part is composed by the list of the global table                */
304/* <name>_tmp                                                                 */
305/******************************************************************************/
306/*                                                                            */
307/*       listvarindoloop                                                      */
308/*                                a-tmp,b-tmp,c_tmp, &                        */
309/*     a,b,c,d,e,f,g,h  =====>    d_tmp,e_tmp,f_tmp, &                        */
310/*                                g_tmp,h_tmp                                 */
311/*                                                                            */
312/******************************************************************************/
313void  WriteVariablelist_subloop_Def(FILE *outputfile)
314{
315   listvar *parcours;   
316   char ligne[LONGNOM];
317   int compteur;
318
319   parcours = listvarindoloop;
320   compteur = 0 ;
321   while ( parcours )   
322   {
323      /* if the readed variable is a variable of the subroutine               */
324      /*    subrotinename we should write the name of this variable           */
325      /*    in the output file                                                */
326      if ( !strcasecmp(parcours->var->modulename,subroutinename) )
327      {
328         if ( didvariableadded == 0 )
329         {
330            strcpy(ligne,"");
331         }
332         else
333         {
334            if ( compteur == 0 ) strcpy(ligne,"");
335            strcat(ligne,",");
336         }
337         strcat(ligne,parcours->var->nomvar);
338         compteur = compteur + 1;
339         didvariableadded = 1;
340         if ( compteur == 3 ) 
341         {
342            if ( fortran77 == 0 ) strcat(ligne," &");
343            if ( fortran77 == 0 ) fprintf(outputfile,"\n      %s",ligne);
344            else fprintf(outputfile,"\n     & %s",ligne);
345            compteur = 0;
346         }
347      }
348      parcours = parcours -> suiv;   
349   }   
350   if ( compteur != 3 && compteur != 0 )
351   {
352      if ( fortran77 == 0 ) fprintf(outputfile,"\n      %s &",ligne);
353      else fprintf(outputfile,"\n     & %s",ligne);
354   }
355
356   /* Now we should replace the last ", &" by " &"                            */
357   if ( didvariableadded != 0 && fortran77 == 0 ) fseek(outputfile,-1,SEEK_CUR);
358   if ( didvariableadded == 0 ) fseek(outputfile,-1,SEEK_CUR);
359}
360
361
362/******************************************************************************/
363/*                      WriteHeadofSubroutineLoop                             */
364/******************************************************************************/
365/* This subroutine is used to write the head of the subroutine                */
366/* Sub_Loop_<name>                                                            */
367/******************************************************************************/
368/*                 Sub_loop_subroutine.h                                      */
369/*                                                                            */
370/*                 subroutine Sub_Loop_subroutine ( &                         */
371/*                 a,b,c, &                                                   */
372/* SubLoopScalar   d,e(1,1),f(1,1,1), &                                       */
373/*                 g,h  &                                                     */
374/*                 )                                                          */
375/* adduseagrifutil USE Agrif_Util                                             */
376/******************************************************************************/
377void  WriteHeadofSubroutineLoop()
378{
379   char ligne[LONGNOM];
380   FILE * subloop;
381
382
383   tofich(fortranout,"\n",1);
384   /* Open this newfile                                                       */
385   sprintf(ligne,"Sub_Loop_%s.h",subroutinename);
386   subloop = associate(ligne);
387   /*                                                                         */
388   if ( fortran77 == 0 ) sprintf(ligne,"      subroutine Sub_Loop_%s( &"
389                                                               ,subroutinename);
390   else sprintf(ligne,"      subroutine Sub_Loop_%s( ",subroutinename);
391   fprintf(subloop,ligne);
392   /*                                                                         */
393   WriteVariablelist_subloop(subloop);
394   WriteVariablelist_subloop_Def(subloop);
395   /*                                                                         */
396   sprintf(ligne,")");
397   tofich(subloop,ligne,1);   
398   /* if USE agrif_Util should be add                                         */
399   if ( adduseagrifutil == 1 ) fprintf(subloop,"\n      USE Agrif_Util\n");
400   /*                                                                         */
401   oldfortranout = fortranout;
402   fortranout = subloop;
403}
404
405/******************************************************************************/
406/*                closeandcallsubloopandincludeit_0                           */
407/******************************************************************************/
408/* Firstpass 0                                                                */
409/* We should close the sub_loop subroutine, call it and close the             */
410/* function (suborfun = 0)                                                    */
411/* subroutine (suborfun = 1)                                                  */
412/* end (suborfun = 2)                                                         */
413/* end program (suborfun = 3)                                                 */
414/* and include the sub_loop subroutine after                                  */
415/******************************************************************************/
416/*                                                                            */
417/******************************************************************************/
418void closeandcallsubloopandincludeit_0(int suborfun, char endsub[LONGNOM],
419                                                          char optname[LONGNOM])
420{
421   char ligne[LONGNOM];
422
423   if ( firstpass == 0 ) 
424   {
425   if ( IsTabvarsUseInArgument_0() == 1 )
426   {
427      /* We should remove the key word end subroutine                         */
428      RemoveWordCUR_0(fortranout,(long)(-strlen(optname)-strlen(endsub)-1),
429                                       strlen(optname)+strlen(endsub)+1);
430      /* We should close the loop subroutine                                  */
431      sprintf(ligne,"\n      end subroutine Sub_Loop_%s",subroutinename);
432      tofich(fortranout,ligne,1);
433      fclose(fortranout); 
434      fortranout = oldfortranout;
435      /* Now we add the call af the new subroutine                            */
436      if ( fortran77 == 0 ) sprintf(ligne,"\n      Call Sub_Loop_%s( &"
437                                                               ,subroutinename);
438      else sprintf(ligne,"\n      Call Sub_Loop_%s( ",subroutinename);
439      fprintf(fortranout,ligne);
440      /* Write the list of the local variables used in this new subroutine    */
441      WriteVariablelist_subloop(fortranout);
442      /* Write the list of the global tables used in this new subroutine      */
443      /*    in doloop                                                         */
444      WriteVariablelist_subloop_Call(fortranout); 
445      /* Close the parenthesis of the new subroutine called                   */
446      sprintf(ligne,")");
447      tofich(fortranout,ligne,1); 
448      /* We should close the original subroutine                              */
449      if ( !strcasecmp(subofagrifinitgrids,subroutinename) )
450      {
451/*         fprintf(fortranout,"      CALL Agrif_Deallocation\n");*/
452      }
453      if ( suborfun == 3 ) sprintf(ligne,"\n      end program %s"
454                                                               ,subroutinename);
455      if ( suborfun == 2 ) sprintf(ligne,"\n      end");
456      if ( suborfun == 1 ) sprintf(ligne,"\n      end subroutine %s"
457                                                               ,subroutinename);
458      if ( suborfun == 0 ) sprintf(ligne,"\n      end function %s"
459                                                               ,subroutinename);
460      tofich(fortranout,ligne,1);
461      /* we should include the above file in the original code                */
462      sprintf(ligne,"\n#include \"Sub_Loop_%s.h\" \n",subroutinename);
463      tofich(fortranout,ligne,1);
464      }
465   }
466}
Note: See TracBrowser for help on using the repository browser.