source: palm/trunk/UTIL/chemistry/gasphase_preproc/kpp/src/code_c.c @ 4901

Last change on this file since 4901 was 4843, checked in by raasch, 3 years ago

local namelist parameter added to switch off the module although the respective module namelist appears in the namelist file, further copyright updates

File size: 15.2 KB
Line 
1/******************************************************************************
2
3  KPP - The Kinetic PreProcessor
4        Builds simulation code for chemical kinetic systems
5
6  Copyright (C) -2021 996 Valeriu Damian and Adrian Sandu
7  Copyright (C) -2021 005 Adrian Sandu
8
9  KPP is free software; you can redistribute it and/or modify it under the
10  terms of the GNU General Public License as published by the Free Software
11  Foundation (http://www.gnu.org/copyleft/gpl.html); either version 2 of the
12  License, or (at your option) any later version.
13
14  KPP is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  details.
18
19  You should have received a copy of the GNU General Public License along
20  with this program; if not, consult http://www.gnu.org/copyleft/gpl.html or
21  write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  Boston, MA  02111-1307,  USA.
23
24  Adrian Sandu
25  Computer Science Department
26  Virginia Polytechnic Institute and State University
27  Blacksburg, VA 24060
28  E-mail: sandu@cs.vt.edu
29
30******************************************************************************/
31
32
33#include "gdata.h"
34#include "code.h"
35#include <string.h>
36
37#define MAX_LINE  120
38#define LINE_LENGTH 70
39
40int fncPrototipe = 0;
41
42char *C_types[] = { "void",     /* VOID */ 
43                    "int",    /* INT */
44                    "float",  /* FLOAT */
45                    "double", /* DOUBLE */
46                    "char *", /* STRING */
47                    "char *"  /* DOUBLESTRING */
48                  }; 
49
50void C_WriteElm( NODE * n )
51{
52ELEMENT *elm;
53char * name;
54char maxi[20];
55char maxj[20];
56
57  elm = n->elm;
58  name = varTable[ elm->var ]->name;
59
60  switch( n->type ) {
61    case CONST: bprintf("%g", elm->val.cnst);
62                break;
63    case ELM:   bprintf("%s", name);
64                break;
65    case VELM:  if( elm->val.idx.i >= 0 ) sprintf( maxi, "%d", elm->val.idx.i );
66                  else sprintf( maxi, "%s", varTable[ -elm->val.idx.i ]->name );
67                bprintf("%s[%s]", name, maxi );
68                break;
69    case MELM:  if( elm->val.idx.i >= 0 ) sprintf( maxi, "%d", elm->val.idx.i );
70                  else sprintf( maxi, "%s", varTable[ -elm->val.idx.i ]->name );
71                if( elm->val.idx.j >= 0 ) sprintf( maxj, "%d", elm->val.idx.j );
72                  else sprintf( maxj, "%s", varTable[ -elm->val.idx.j ]->name );
73                bprintf("%s[%s][%s]", name, maxi, maxj );
74                break;
75    case EELM:  bprintf("(%s)", elm->val.expr );
76                break;
77  }
78}
79
80void C_WriteSymbol( int op )
81{
82  switch( op ) {
83    case ADD:   bprintf("+");
84                AllowBreak(); 
85                break;
86    case SUB:   bprintf("-"); 
87                AllowBreak(); 
88                break;
89    case MUL:   bprintf("*"); 
90                AllowBreak(); 
91                break;
92    case DIV:   bprintf("/"); 
93                AllowBreak(); 
94                break;
95    case POW:   bprintf("power");
96                break;         
97    case O_PAREN: bprintf("(");
98                AllowBreak(); 
99                break;           
100    case C_PAREN: bprintf(")");
101                break;           
102    case NONE:
103                break;           
104  }
105}
106
107void C_WriteAssign( char *ls, char *rs )
108{
109int start;
110int crtident;
111int linelg;
112int i,j;
113char c;
114int first;
115int number_of_lines = 1, MAX_NO_OF_LINES = 99;
116int ifound, jfound;
117
118/*  Operator Mapping: 0xaa = '*' | 0xab = '+' | 0xac = ','
119                      0xad = '-' | 0xae ='.' | 0xaf = '/' */                 
120/* char op_mult=0xaa, op_plus=0xab, op_minus=0xad, op_dot=0xae, op_div=0xaf; */               
121char op_mult='*', op_plus='+', op_minus='-', op_dot='.', op_div='/';                 
122
123  crtident = 2 + ident * 2;
124  bprintf("%*s%s = ", crtident, "", ls);
125  start = strlen( ls ) + crtident + 2;
126  linelg = LINE_LENGTH - start;
127
128  first = 1;
129  while( strlen(rs) > linelg ) {
130    ifound = 0; jfound = 0;
131    if ( number_of_lines >= MAX_NO_OF_LINES ) {/* if a new line needs to be started */
132     for( j=linelg; j>5; j-- ) /* split row here if +, -, or comma */
133       if ( ( rs[j] == op_plus )||( rs[j] == op_minus )||( rs[j]==',' ) ) { 
134        jfound = 1; i=j; break;
135        }
136    }
137    if ( ( number_of_lines < MAX_NO_OF_LINES )||( !jfound ) ) {
138     for( i=linelg; i>10; i-- ) /* split row here if operator or comma */
139       if ( ( rs[i] & 0x80 )||( rs[i]==',' ) ) {
140        ifound = 1; break;
141        }
142     if( i <= 10 ) {
143      printf("\n Warning: possible error in continuation lines for %s = ...",ls);
144      i = linelg;
145     }
146    } 
147    while ( rs[i-1] & 0x80 ) i--; /* put all operators on the next row */
148    while ( rs[i] == ',' ) i++; /* put commas on the current row */
149   
150    /*for( i=linelg; i>10; i-- )
151      if( ( rs[i] & 0x80 ) || ( rs[i] == ',' ) )
152        break;
153    if( i < 10 ) {
154      printf("\nPossible error when cutting lines");
155      i = linelg;
156    }*/
157   
158    c = rs[i]; 
159    rs[i] = 0;
160    if ( first ) { 
161      bprintf("%s", rs );
162      linelg++;
163      first = 0;
164    } else {
165      bprintf("\n%*s%s", start, "", rs );     
166      if ( jfound ) {
167         bprintf(";\n%*s%s = %s", crtident, "", ls, ls);
168         number_of_lines = 1;
169         }
170    } 
171    rs[i] = c;
172    rs += i;
173    number_of_lines++;
174  }
175
176  if ( number_of_lines > MAX_NO_OF_LINES )
177     printf("\n Warning: many continuation lines (%d) for %s = ...",number_of_lines,ls);
178 
179  if ( first ) bprintf("%s;\n", rs );
180          else bprintf("\n%*s%s;\n", start, "", rs );
181
182  FlushBuf();
183}
184
185void C_WriteComment( char *fmt, ... )
186{
187Va_list args;
188char buf[ MAX_LINE ];
189
190  Va_start( args, fmt );
191  vsprintf( buf, fmt, args );
192  va_end( args );
193
194  bprintf( "/* %-*s */\n", LINE_LENGTH - 6, buf );
195
196  FlushBuf();
197}
198
199
200char * C_Decl( int v )
201{ 
202static char buf[120];
203VARIABLE *var;
204char *baseType;
205char maxi[20];
206char maxj[20];
207
208  var = varTable[ v ];
209  baseType = C_types[ var->baseType ];
210 
211  *buf = 0;
212 
213  switch( var->type ) {
214    case ELM:   
215              sprintf( buf, "%s %s", baseType, var->name );
216                break;
217    case VELM: 
218              if( var->maxi > 0 ) sprintf( maxi, "%d", var->maxi );
219              if( var->maxi == 0 ) sprintf( maxi, "%d", 1 );
220                /*  else sprintf( maxi, "%s", varTable[ -var->maxi ]->name);  */
221              if ( var->maxi < 0 ) {
222                    if (varTable[ -var->maxi ]->value < 0) 
223                      sprintf( maxi, "%s", varTable[ -var->maxi ]->name );
224                    else 
225                      sprintf( maxi, "%d", (varTable[-var->maxi]->value)==0?
226                           1:varTable[-var->maxi]->value );
227              } 
228                /*if( (var->maxi == 0) ||
229                    ((var->maxi < 0) && (varTable[ -var->maxi ]->maxi == 0)) )
230                  sprintf( maxi, "%s+1", maxi );*/
231              if( fncPrototipe ) 
232                  sprintf( buf, "%s %s[]", baseType, var->name );
233              else 
234                  sprintf( buf, "%s %s[%s]", baseType, var->name, maxi );
235              break;
236    case MELM: 
237              if( var->maxi > 0 ) sprintf( maxi, "%d", var->maxi );
238              else { 
239                 if (varTable[ -var->maxi ]->value < 0)
240                    sprintf( maxi, "%s", varTable[ -var->maxi ]->name );
241                 else 
242                    sprintf( maxi, "%d", (varTable[-var->maxi]->value)==0?
243                           1:varTable[-var->maxi]->value );
244              } 
245                /* if( (var->maxi == 0) ||
246                    ((var->maxi < 0) && (varTable[ -var->maxi ]->maxi == 0)) )
247                  strcat( maxi, "+1"); */
248              if( var->maxj > 0 ) sprintf( maxj, "%d", var->maxj );
249              else {
250                  if (varTable[ -var->maxj ]->value < 0)
251                     sprintf( maxj, "%s", varTable[ -var->maxj ]->name );
252                  else 
253                     sprintf( maxj, "%d", (varTable[-var->maxj]->value)==0?
254                           1:varTable[-var->maxj]->value );
255              }
256              if( fncPrototipe ) 
257                  sprintf( buf, "%s %s[][]", baseType, var->name );
258              else 
259                  sprintf( buf, "%s %s[%s][%s]",
260                        baseType, var->name, maxi, maxj ); 
261                break;
262    default:
263                Message( "Can not declare type %d", var->type );
264                Message( "v = %d", v );
265                break;
266  }
267  return buf;
268}
269
270void C_Declare( int v )
271{
272  bprintf("%-40s", strcat( C_Decl(v), ";" ) ); 
273  if( varTable[ v ]->comment )
274    bprintf(" /* %s */\n", varTable[ v ]->comment );
275  else 
276    bprintf("\n");   
277
278  FlushBuf();
279}
280
281void C_ExternDeclare( int v )
282{
283  bprintf("extern %-40s", strcat( C_Decl(v), ";" ) ); 
284  if( varTable[ v ]->comment )
285    bprintf(" /* %s */\n", varTable[ v ]->comment );
286  else 
287    bprintf("\n");   
288
289  FlushBuf();
290}
291
292void C_GlobalDeclare( int v )
293{
294  C_Declare( v );
295}
296
297void C_InitDeclare( int v, int n, void * values )
298{
299int i;
300VARIABLE *var;
301int * ival;
302double * dval;
303char ** cval;
304int maxCols = MAX_COLS;
305
306  var = varTable[ v ];
307  ival = (int*) values;
308  dval = (double*) values;
309  cval = (char**) values;
310 
311  if( var->comment )
312      bprintf(" /* %s */\n\n", var->comment );
313       
314  switch( var->type ) {
315    case VELM: bprintf( "  %s  %s[] = {\n%5s", C_types[var->baseType], var->name, " " );
316               for( i = 0; i < n; i++ ) {
317                 switch( var->baseType ) {
318                   case INT: bprintf( "%3d",  ival[i] ); maxCols=12; break;
319                   case DOUBLE:
320                   case REAL:bprintf( "%5lg", dval[i] ); maxCols=8; break;
321                   case STRING:bprintf( "\"%s\"", cval[i] ); maxCols=8; break;
322                   case DOUBLESTRING:bprintf( "\"%s\"", cval[i] ); maxCols=1; break;
323                 }
324                 if( i < n-1 ) bprintf( "," );
325                 if( (i+1) % maxCols == 0 ) bprintf( "\n%5s", " " );
326               } 
327               if( n == 0 ) bprintf( "0" );
328               bprintf( " }; \n\n" );
329               break;
330               
331    case ELM:  bprintf( "  %s %s = ", C_types[var->baseType], var->name );
332               switch( var->baseType ) {
333                 case INT: bprintf( "%d",  *ival ); break;
334                 case DOUBLE:
335                 case REAL:bprintf( "%lg", *dval ); break;
336                 case STRING:bprintf( "\"%s\"", *cval ); break;
337                 case DOUBLESTRING:bprintf( "\"%s\"", *cval ); break;
338               }
339               bprintf( ";\n\n" );
340               break;
341               
342    default:   printf( "\n Function not defined !\n" );
343               break;
344  }
345
346  FlushBuf();
347}
348
349void C_DeclareConstant( int v, char *val ) 
350{
351VARIABLE *var;
352int ival;
353char dummy_val[100];           /* used just to avoid strange behaviour of
354                                  sscanf when compiled with gcc */
355                                 
356  strcpy(dummy_val,val);val = dummy_val;
357 
358  var = varTable[ v ];
359
360  if( sscanf(val, "%d", &ival) == 1 ) 
361    if( ival == 0 ) var->maxi = 0;
362               else var->maxi = 1;
363  else
364    var->maxi = -1;       
365
366  switch( var->type ) {
367    case CONST: bprintf("#define %-20s %-10s ", var->name, val );
368                break;       
369    default:
370                printf( "Invalid constant", var->type );
371                break;
372  }
373  if( varTable[ v ]->comment )
374    bprintf(" /* %s */\n", varTable[ v ]->comment );
375  else 
376    bprintf("\n");   
377
378  FlushBuf();
379}
380
381void C_FunctionStart( int f, int *vars )
382{
383int i;
384int v;
385char * name;
386int narg;
387
388  name = varTable[ f ]->name;
389  narg = varTable[ f ]->maxi;
390
391  fncPrototipe = 1;
392
393  bprintf("void %s( \n", name );
394  for( i = 0; i < narg-1; i++ ) {
395    v = vars[ i ];
396    bprintf("  %-38s", strcat( C_Decl(v), "," ) ); 
397    if( varTable[ v ]->comment )
398      bprintf(" /* %s */\n", varTable[ v ]->comment );
399    else 
400      bprintf("\n");   
401  }
402  if( narg >= 1 ) {
403    v = vars[ i ];
404    bprintf("  %-38s", C_Decl(v) );
405    if( varTable[ v ]->comment )
406      bprintf(" /* %s */\n", varTable[ v ]->comment );
407    else 
408      bprintf("\n");   
409  }
410  bprintf(")");
411
412  fncPrototipe = 0;
413 
414  FlushBuf();
415}
416
417void C_FunctionPrototipe( int f, ... )
418{
419Va_list args;
420int i;
421int vars[20];
422char * name;
423int narg;
424
425  name = varTable[ f ]->name;
426  narg = varTable[ f ]->maxi;
427
428  Va_start( args, f ); 
429  for( i = 0; i < narg; i++ ) 
430    vars[i] = va_arg( args, int );
431  va_end( args );
432  C_FunctionStart( f, vars );
433  bprintf(";\n");
434
435  FlushBuf();
436}
437
438void C_FunctionBegin( int f, ... )
439{
440Va_list args;
441int i;
442int vars[20];
443char * name;
444int narg;
445
446  name = varTable[ f ]->name;
447  narg = varTable[ f ]->maxi;
448
449  Va_start( args, f ); 
450  for( i = 0; i < narg; i++ ) 
451    vars[i] = va_arg( args, int );
452  va_end( args );
453 
454  CommentFncBegin( f, vars );
455  C_FunctionStart( f, vars );
456  bprintf("\n");
457  bprintf("{\n");
458 
459  FlushBuf();
460
461  MapFunctionComment( f, vars );
462}
463
464void C_FunctionEnd( int f )
465{
466  bprintf("}\n\n");
467
468  FlushBuf();
469
470  CommentFunctionEnd( f );
471}
472
473void C_Inline( char *fmt, ... )
474{
475Va_list args;
476char buf[ 1000 ];
477
478  if( useLang != C_LANG ) return;
479 
480  Va_start( args, fmt );
481  vsprintf( buf, fmt, args );
482  va_end( args );
483  bprintf( "%s\n",buf );
484 
485  FlushBuf();
486}
487
488void Use_C()
489{ 
490  WriteElm          = C_WriteElm;
491  WriteSymbol       = C_WriteSymbol; 
492  WriteAssign       = C_WriteAssign;
493  WriteComment      = C_WriteComment;
494  DeclareConstant   = C_DeclareConstant;
495  Declare           = C_Declare;
496  ExternDeclare     = C_ExternDeclare;
497  GlobalDeclare     = C_GlobalDeclare;
498  InitDeclare       = C_InitDeclare;
499 
500  FunctionStart     = C_FunctionStart;
501  FunctionPrototipe = C_FunctionPrototipe;
502  FunctionBegin     = C_FunctionBegin;
503  FunctionEnd       = C_FunctionEnd;
504
505  OpenFile( &param_headerFile,   rootFileName, "_Parameters.h", "Parameter Header File" );
506  OpenFile( &initFile, rootFileName, "_Initialize.c", "Initialization File" );
507  OpenFile( &driverFile, rootFileName, "_Main.c", "Main Program File" );
508  OpenFile( &integratorFile, rootFileName, "_Integrator.c", 
509                   "Numerical Integrator (Time-Stepping) File" );
510  OpenFile( &linalgFile, rootFileName, "_LinearAlgebra.c", 
511                   "Linear Algebra Data and Routines File" );
512  OpenFile( &functionFile, rootFileName, "_Function.c", 
513                   "The ODE Function of Chemical Model File" );
514  OpenFile( &jacobianFile, rootFileName, "_Jacobian.c", 
515                   "The ODE Jacobian of Chemical Model File" );
516  OpenFile( &rateFile, rootFileName, "_Rates.c", 
517                   "The Reaction Rates File" );
518  if ( useStochastic )
519    OpenFile( &stochasticFile, rootFileName, "_Stochastic.c", 
520                   "The Stochastic Chemical Model File" );
521  if ( useStoicmat ) {
522    OpenFile( &stoichiomFile, rootFileName, "_Stoichiom.c", 
523                   "The Stoichiometric Chemical Model File" );
524    OpenFile( &sparse_stoicmFile, rootFileName, "_StoichiomSP.c", 
525                   "Sparse Stoichiometric Data Structures File" );
526  }               
527  OpenFile( &utilFile, rootFileName, "_Util.c", 
528                   "Auxiliary Routines File" );
529  OpenFile( &sparse_dataFile, rootFileName, "_Sparse.h", "Sparse Data Header File" );
530  OpenFile( &global_dataFile, rootFileName, "_Global.h", "Global Data Header File" );
531  if ( useJacSparse ) {
532     OpenFile( &sparse_jacFile, rootFileName, "_JacobianSP.c",
533        "Sparse Jacobian Data Structures File" ); 
534  }
535  if ( useHessian ) {
536     OpenFile( &hessianFile, rootFileName, "_Hessian.c", "Hessian File" );
537     OpenFile( &sparse_hessFile, rootFileName, "_HessianSP.c",
538         "Sparse Hessian Data Structures File" );
539  }   
540  OpenFile( &mapFile, rootFileName, ".map", 
541                   "Map File with Human-Readable Information" );
542  OpenFile( &monitorFile, rootFileName, "_Monitor.c", 
543                   "Utility Data Initialization" );
544} 
Note: See TracBrowser for help on using the repository browser.