4 #include <sys/resource.h>
10 char rxt_tbl
[5][1000][128];
11 char chm_scheme
[5][128];
14 void strip_blanks( char *instring
, char *outstring
)
19 slen
= strlen( instring
);
20 strcpy( outstring
,instring
);
22 for( i
=0,j
=0; i
< slen
; i
++ )
24 if( strncmp( c
, " ", 1 ) )
25 strncpy( (outstring
+(j
++)), c
, 1 );
28 c
= index( outstring
,'\n' );
32 int AppendReg( char *chem_opt
, int ndx
)
37 char *stradj
= "abcdef";
43 char inln
[1024],winln
[1024],s
[1024];
45 char rxtstr_tbl
[1000][128];
48 FILE *fp_eqn
, *fp_reg
;
50 strcpy( fname
,chem_opt
);
51 slen
= sprintf( path
, "chem/KPP/mechanisms/%s/%s.eqn",fname
,fname
);
52 fprintf(stderr
,"Using file:\n");
53 fprintf(stderr
,"%s\n", path
);
56 // open input *eqn file
58 if( (fp_eqn
= fopen( path
, "r" )) == NULL
)
60 fprintf(stderr
,"Can not open %s for reading\n", path
);
65 // open output registry file
67 if( access( "Registry/registry.irr_diag",F_OK
) )
68 fp_reg
= fopen( "Registry/registry.irr_diag", "w" );
70 fp_reg
= fopen( "Registry/registry.irr_diag", "a" );
72 if( fp_reg
== NULL
) {
73 fprintf(stderr
,"Can not open registry.irr_diag for writing\n");
76 strcpy( buffer
,"\"Integrated Reaction Rate\" \"\"");
77 fprintf(fp_reg
,"state real - ikjf irr_diag_%s - - - - %s\n",fname
,buffer
);
81 // loop over input lines
83 while( fgets( inln
, 1024, fp_eqn
) != NULL
)
85 if( strncmp( inln
, "//", 2 ) && strncmp( inln
, "#", 1 ) )
88 // strip blanks from reaction string
90 strip_blanks( inln
, inln
);
92 // concatentate lines?
94 if( rindex( inln
,';' ) == NULL
) {
96 fgets( winln
, 1024, fp_eqn
);
97 strip_blanks( winln
, winln
);
99 } while( rindex( inln
,';' ) == NULL
);
101 strt
= strchr( inln
,'{' );
102 while( strt
!= NULL
)
104 end
= strchr( strt
,'}' );
110 for( i
= 0; i
< slen
; i
++ )
111 if( !strncmp( strt
+i
,"=",1 ) ) *(strt
+i
) = '$';
114 strt
= strchr( strt
,'{' );
117 strt
= strchr( inln
, '}' );
118 if( strt
== NULL
) continue;
120 end
= rindex( strt
, '=' );
124 slen
= strlen( strt
);
127 // string to upper case
129 slen
= strlen( wstrg1
);
130 for( i
=0; i
<= slen
; i
++ )
131 wstrg1
[i
] = toupper( wstrg1
[i
] );
133 // remove all text between {} pair including delimiters
135 strt
= strchr( wstrg1
,'{' );
138 end
= index( wstrg1
,'}' );
150 strcpy( rxtstr_tbl
[Nrxt
],wstrg1
);
152 // check for unique reaction string
157 for( i
= 0; i
< Nrxt
; i
++)
158 if( !strcmp( wstrg1
,rxtstr_tbl
[i
] ) )
164 strcat( wstrg1
,"_" );
165 strncat( wstrg1
,stradj
+Nmatch
,1 );
169 strcpy( rxtstr
,wstrg1
);
170 strcpy( rxtsym
,wstrg1
);
171 strcpy( rxt_tbl
[ndx
][Nrxt
],wstrg1
);
175 for( i
=0; i
< slen
; i
++ )
177 if( ! strncmp( rxtsym
+i
, "+", 1 ) )
178 strncpy( rxtsym
+i
, "_", 1 );
180 strcat( rxtsym
,"_IRR" );
184 // fprintf(fp_reg,"state real %s ikjf irr_diag_%s 1 - rh9 \"%s\" \"%s Integrated Reaction Rate\" \"molecules/cm^3/s\"\n",rxtsym,fname,rxtstr,rxtstr);
185 fprintf(fp_reg
,"state real %s ikjf irr_diag_%s 1 - rh9 \"%s\" \"%s Integrated Reaction Rate\" \"ppmv\"\n",rxtsym
,fname
,rxtsym
,rxtstr
);
193 strcpy( chm_scheme
[ndx
],chem_opt
);
201 int irr_diag_scalar_indices( char *dirname
)
205 int first
, flush
, s1
;
213 // open output inc file
215 sprintf( fname
, "inc/scalar_indices_irr_diag_decls.inc");
216 fp_inc
= fopen( fname
, "w" );
218 if( fp_inc
== NULL
) {
219 fprintf(stderr
,"Can not open %s for writing\n",fname
);
222 fprintf( fp_inc
," \n");
223 fprintf( fp_inc
," INTEGER, PARAMETER :: nchm_opts = %d\n",nChmOpts
);
224 fprintf( fp_inc
," INTEGER :: chm_opt_ndx\n");
225 fprintf( fp_inc
," \n");
226 fprintf( fp_inc
," INTEGER :: chm_opts_cnt(nchm_opts)\n");
227 fprintf( fp_inc
," INTEGER :: chm_opts_ndx(nchm_opts)\n");
228 fprintf( fp_inc
," CHARACTER(len=32) :: chm_opts_name(nchm_opts)\n");
229 for( i
= 0,j
= 0; i
< nChmOpts
; i
++ ) {
233 fprintf( fp_inc
," CHARACTER(len=64), TARGET :: rxtsym(%d,nchm_opts)\n",j
);
234 fprintf( fp_inc
," \n");
237 // open output inc file
239 sprintf( fname
, "inc/scalar_indices_irr_diag.inc");
240 fp_inc
= fopen( fname
, "w" );
242 if( fp_inc
== NULL
) {
243 fprintf(stderr
,"Can not open %s for writing\n",fname
);
247 fprintf( fp_inc
," \n");
248 sprintf( line
," chm_opts_cnt(:nchm_opts) = (/ ");
249 for( i
= 0; i
< nChmOpts
; i
++ ) {
251 sprintf( piece
," %d",rxt_cnt
[i
]);
253 sprintf( piece
," ,%d",rxt_cnt
[i
]);
254 strcat( line
,piece
);
256 strcat( line
," /)\n" );
257 fprintf( fp_inc
,line
);
258 fprintf( fp_inc
," \n");
260 for( i
= 0; i
< nChmOpts
; i
++ ) {
261 sprintf( line
," chm_opts_name(%d) = '%s'\n",i
+1,chm_scheme
[i
]);
262 fprintf( fp_inc
,line
);
264 fprintf( fp_inc
," \n");
266 sprintf( line
," chm_opts_ndx(:nchm_opts) = (/ ");
267 for( i
= 0; i
< nChmOpts
; i
++ ) {
269 sprintf( piece
,"%s_kpp",chm_scheme
[i
]);
271 sprintf( piece
," ,%s_kpp",chm_scheme
[i
]);
272 strcat( line
,piece
);
274 strcat( line
," /)\n" );
275 fprintf( fp_inc
,line
);
276 fprintf( fp_inc
," \n");
278 for( i
= 0; i
< nChmOpts
,rxt_cnt
[i
] > 0; i
++ ) {
279 for( j
= 0; j
< rxt_cnt
[i
]; j
++ ) {
280 sprintf( line
," rxtsym(%d,%d) = '%s'\n",j
+1,i
+1,rxt_tbl
[i
][j
]);
281 fprintf( fp_inc
,"%s",line
);
283 fprintf( fp_inc
," \n");
286 fprintf( fp_inc," IF( model_config_rec%%irr_opt(idomain) == 1 ) THEN\n");
287 fprintf( fp_inc," CALL nl_get_chem_opt( idomain,chem_opt )\n");
288 fprintf( fp_inc," DO chm_opt_ndx = 1,nchm_opts\n");
289 fprintf( fp_inc," IF( chem_opt == chm_opts_ndx(chm_opt_ndx) ) THEN\n");
290 fprintf( fp_inc," EXIT\n");
291 fprintf( fp_inc," ENDIF\n");
292 fprintf( fp_inc," ENDDO\n");
293 fprintf( fp_inc," IF( chm_opt_ndx > nchm_opts ) THEN\n");
294 fprintf( fp_inc," write(err_mes,*) 'IRR not supported for chem option ',chem_opt\n");
295 fprintf( fp_inc," CALL wrf_error_fatal( trim(err_mes) )\n");
296 fprintf( fp_inc," ENDIF\n");
297 fprintf( fp_inc," ENDIF\n");