4 #include <sys/resource.h>
11 char rxt_tbl
[5][1000][128];
12 char chm_scheme
[5][128];
15 void strip_blanks( char *instring
, char *outstring
)
20 slen
= strlen( instring
);
21 strcpy( outstring
,instring
);
23 for( i
=0,j
=0; i
< slen
; i
++ )
25 if( strncmp( c
, " ", 1 ) )
26 strncpy( (outstring
+(j
++)), c
, 1 );
29 c
= index( outstring
,'\n' );
33 int AppendReg( char *chem_opt
, int ndx
)
38 char *stradj
= "abcdef";
44 char inln
[1024],winln
[1024],s
[1024];
46 char rxtstr_tbl
[1000][128];
49 FILE *fp_eqn
, *fp_reg
;
51 strcpy( fname
,chem_opt
);
52 slen
= sprintf( path
, "chem/KPP/mechanisms/%s/%s.eqn",fname
,fname
);
53 fprintf(stderr
,"Using file:\n");
54 fprintf(stderr
,"%s\n", path
);
57 // open input *eqn file
59 if( (fp_eqn
= fopen( path
, "r" )) == NULL
)
61 fprintf(stderr
,"Can not open %s for reading\n", path
);
66 // open output registry file
68 if( access( "Registry/registry.irr_diag",F_OK
) )
69 fp_reg
= fopen( "Registry/registry.irr_diag", "w" );
71 fp_reg
= fopen( "Registry/registry.irr_diag", "a" );
73 if( fp_reg
== NULL
) {
74 fprintf(stderr
,"Can not open registry.irr_diag for writing\n");
77 strcpy( buffer
,"\"Integrated Reaction Rate\" \"\"");
78 fprintf(fp_reg
,"state real - ikjf irr_diag_%s - - - - %s\n",fname
,buffer
);
82 // loop over input lines
84 while( fgets( inln
, 1024, fp_eqn
) != NULL
)
86 if( strncmp( inln
, "//", 2 ) && strncmp( inln
, "#", 1 ) )
89 // strip blanks from reaction string
91 strip_blanks( inln
, inln
);
93 // concatentate lines?
95 if( rindex( inln
,';' ) == NULL
) {
97 fgets( winln
, 1024, fp_eqn
);
98 strip_blanks( winln
, winln
);
100 } while( rindex( inln
,';' ) == NULL
);
102 strt
= strchr( inln
,'{' );
103 while( strt
!= NULL
)
105 end
= strchr( strt
,'}' );
111 for( i
= 0; i
< slen
; i
++ )
112 if( !strncmp( strt
+i
,"=",1 ) ) *(strt
+i
) = '$';
115 strt
= strchr( strt
,'{' );
118 strt
= strchr( inln
, '}' );
119 if( strt
== NULL
) continue;
121 end
= rindex( strt
, '=' );
125 slen
= strlen( strt
);
128 // string to upper case
130 slen
= strlen( wstrg1
);
131 for( i
=0; i
<= slen
; i
++ )
132 wstrg1
[i
] = toupper( wstrg1
[i
] );
134 // remove all text between {} pair including delimiters
136 strt
= strchr( wstrg1
,'{' );
139 end
= index( wstrg1
,'}' );
151 strcpy( rxtstr_tbl
[Nrxt
],wstrg1
);
153 // check for unique reaction string
158 for( i
= 0; i
< Nrxt
; i
++)
159 if( !strcmp( wstrg1
,rxtstr_tbl
[i
] ) )
165 strcat( wstrg1
,"_" );
166 strncat( wstrg1
,stradj
+Nmatch
,1 );
170 strcpy( rxtstr
,wstrg1
);
171 strcpy( rxtsym
,wstrg1
);
172 strcpy( rxt_tbl
[ndx
][Nrxt
],wstrg1
);
176 for( i
=0; i
< slen
; i
++ )
178 if( ! strncmp( rxtsym
+i
, "+", 1 ) )
179 strncpy( rxtsym
+i
, "_", 1 );
181 strcat( rxtsym
,"_IRR" );
185 // 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);
186 fprintf(fp_reg
,"state real %s ikjf irr_diag_%s 1 - rh9 \"%s\" \"%s Integrated Reaction Rate\" \"ppmv\"\n",rxtsym
,fname
,rxtsym
,rxtstr
);
194 strcpy( chm_scheme
[ndx
],chem_opt
);
202 int irr_diag_scalar_indices( char *dirname
)
206 int first
, flush
, s1
;
214 // open output inc file
216 sprintf( fname
, "inc/scalar_indices_irr_diag_decls.inc");
217 fp_inc
= fopen( fname
, "w" );
219 if( fp_inc
== NULL
) {
220 fprintf(stderr
,"Can not open %s for writing\n",fname
);
223 fprintf( fp_inc
," \n");
224 fprintf( fp_inc
," INTEGER, PARAMETER :: nchm_opts = %d\n",nChmOpts
);
225 fprintf( fp_inc
," INTEGER :: chm_opt_ndx\n");
226 fprintf( fp_inc
," \n");
227 fprintf( fp_inc
," INTEGER :: chm_opts_cnt(nchm_opts)\n");
228 fprintf( fp_inc
," INTEGER :: chm_opts_ndx(nchm_opts)\n");
229 fprintf( fp_inc
," CHARACTER(len=32) :: chm_opts_name(nchm_opts)\n");
230 for( i
= 0,j
= 0; i
< nChmOpts
; i
++ ) {
234 fprintf( fp_inc
," CHARACTER(len=64), TARGET :: rxtsym(%d,nchm_opts)\n",j
);
235 fprintf( fp_inc
," \n");
238 // open output inc file
240 sprintf( fname
, "inc/scalar_indices_irr_diag.inc");
241 fp_inc
= fopen( fname
, "w" );
243 if( fp_inc
== NULL
) {
244 fprintf(stderr
,"Can not open %s for writing\n",fname
);
248 fprintf( fp_inc
," \n");
249 sprintf( line
," chm_opts_cnt(:nchm_opts) = (/ ");
250 for( i
= 0; i
< nChmOpts
; i
++ ) {
252 sprintf( piece
," %d",rxt_cnt
[i
]);
254 sprintf( piece
," ,%d",rxt_cnt
[i
]);
255 strcat( line
,piece
);
257 strcat( line
," /)\n" );
258 fprintf( fp_inc
,"%s",line
);
259 fprintf( fp_inc
," \n");
261 for( i
= 0; i
< nChmOpts
; i
++ ) {
262 sprintf( line
," chm_opts_name(%d) = '%s'\n",i
+1,chm_scheme
[i
]);
263 fprintf( fp_inc
,"%s",line
);
265 fprintf( fp_inc
," \n");
267 sprintf( line
," chm_opts_ndx(:nchm_opts) = (/ ");
268 for( i
= 0; i
< nChmOpts
; i
++ ) {
270 sprintf( piece
,"%s_kpp",chm_scheme
[i
]);
272 sprintf( piece
," ,%s_kpp",chm_scheme
[i
]);
273 strcat( line
,piece
);
275 strcat( line
," /)\n" );
276 fprintf( fp_inc
,"%s",line
);
277 fprintf( fp_inc
," \n");
279 for( i
= 0; i
< nChmOpts
&& rxt_cnt
[i
] > 0; i
++ ) {
280 for( j
= 0; j
< rxt_cnt
[i
]; j
++ ) {
281 sprintf( line
," rxtsym(%d,%d) = '%s'\n",j
+1,i
+1,rxt_tbl
[i
][j
]);
282 fprintf( fp_inc
,"%s",line
);
284 fprintf( fp_inc
," \n");
287 fprintf( fp_inc," IF( model_config_rec%%irr_opt(idomain) == 1 ) THEN\n");
288 fprintf( fp_inc," CALL nl_get_chem_opt( idomain,chem_opt )\n");
289 fprintf( fp_inc," DO chm_opt_ndx = 1,nchm_opts\n");
290 fprintf( fp_inc," IF( chem_opt == chm_opts_ndx(chm_opt_ndx) ) THEN\n");
291 fprintf( fp_inc," EXIT\n");
292 fprintf( fp_inc," ENDIF\n");
293 fprintf( fp_inc," ENDDO\n");
294 fprintf( fp_inc," IF( chm_opt_ndx > nchm_opts ) THEN\n");
295 fprintf( fp_inc," write(err_mes,*) 'IRR not supported for chem option ',chem_opt\n");
296 fprintf( fp_inc," CALL wrf_error_fatal( trim(err_mes) )\n");
297 fprintf( fp_inc," ENDIF\n");
298 fprintf( fp_inc," ENDIF\n");