1 | /****************************************************************************** |
---|
2 | |
---|
3 | KPP - The Kinetic PreProcessor |
---|
4 | Builds simulation code for chemical kinetic systems |
---|
5 | |
---|
6 | Copyright (C) 1995-1996 Valeriu Damian and Adrian Sandu |
---|
7 | Copyright (C) 1997-2005 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 <unistd.h> |
---|
36 | #include <string.h> |
---|
37 | |
---|
38 | #define MAX_LINE 120 |
---|
39 | |
---|
40 | void FatalError( int status, char *fmt, char *buf ); |
---|
41 | |
---|
42 | char *MATLAB_types[] = { "", /* VOID */ |
---|
43 | "INTEGER", /* INT */ |
---|
44 | "REAL", /* FLOAT */ |
---|
45 | /*"REAL(dp)", */ /* DOUBLE */ |
---|
46 | "DOUBLE PRECISION", /* DOUBLE */ |
---|
47 | "CHARACTER(LEN=12)", /* STRING */ |
---|
48 | "CHARACTER(LEN=100)" /* DOUBLESTRING */ |
---|
49 | }; |
---|
50 | |
---|
51 | /*************************************************************************************************/ |
---|
52 | void MATLAB_WriteElm( NODE * n ) |
---|
53 | { |
---|
54 | ELEMENT *elm; |
---|
55 | char * name; |
---|
56 | char maxi[20]; |
---|
57 | char maxj[20]; |
---|
58 | |
---|
59 | elm = n->elm; |
---|
60 | name = varTable[ elm->var ]->name; |
---|
61 | |
---|
62 | switch( n->type ) { |
---|
63 | case CONST: bprintf("%g", elm->val.cnst); |
---|
64 | break; |
---|
65 | case ELM: bprintf("%s", name); |
---|
66 | break; |
---|
67 | case VELM: if( elm->val.idx.i >= 0 ) sprintf( maxi, "%d", elm->val.idx.i+1 ); |
---|
68 | else sprintf( maxi, "%s", varTable[ -elm->val.idx.i ]->name ); |
---|
69 | bprintf("%s(%s)", name, maxi ); |
---|
70 | break; |
---|
71 | case MELM: if( elm->val.idx.i >= 0 ) sprintf( maxi, "%d", elm->val.idx.i+1 ); |
---|
72 | else sprintf( maxi, "%s", varTable[ -elm->val.idx.i ]->name ); |
---|
73 | if( elm->val.idx.j >= 0 ) sprintf( maxj, "%d", elm->val.idx.j+1 ); |
---|
74 | else sprintf( maxj, "%s", varTable[ -elm->val.idx.j ]->name ); |
---|
75 | bprintf("%s(%s,%s)", name, maxi, maxj ); |
---|
76 | break; |
---|
77 | case EELM: bprintf("(%s)", elm->val.expr ); |
---|
78 | break; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | /*************************************************************************************************/ |
---|
83 | void MATLAB_WriteSymbol( int op ) |
---|
84 | { |
---|
85 | switch( op ) { |
---|
86 | case ADD: bprintf("+"); |
---|
87 | AllowBreak(); |
---|
88 | break; |
---|
89 | case SUB: bprintf("-"); |
---|
90 | AllowBreak(); |
---|
91 | break; |
---|
92 | case MUL: bprintf("*"); |
---|
93 | AllowBreak(); |
---|
94 | break; |
---|
95 | case DIV: bprintf("/"); |
---|
96 | AllowBreak(); |
---|
97 | break; |
---|
98 | case POW: bprintf("^"); |
---|
99 | break; |
---|
100 | case O_PAREN: bprintf("("); |
---|
101 | AllowBreak(); |
---|
102 | break; |
---|
103 | case C_PAREN: bprintf(")"); |
---|
104 | break; |
---|
105 | case NONE: |
---|
106 | break; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | /*************************************************************************************************/ |
---|
111 | void MATLAB_WriteAssign( char *ls, char *rs ) |
---|
112 | { |
---|
113 | int start; |
---|
114 | int linelg; |
---|
115 | int i, j; |
---|
116 | int ifound, jfound; |
---|
117 | char c; |
---|
118 | int first; |
---|
119 | int crtident; |
---|
120 | |
---|
121 | /* Max no of continuation lines in F95 standard is 39 */ |
---|
122 | int number_of_lines = 1, MAX_NO_OF_LINES = 36; |
---|
123 | |
---|
124 | /* Operator Mapping: 0xaa = '*' | 0xab = '+' | 0xac = ',' |
---|
125 | 0xad = '-' | 0xae ='.' | 0xaf = '/' */ |
---|
126 | /* char op_mult=0xaa, op_plus=0xab, op_minus=0xad, op_dot=0xae, op_div=0xaf; */ |
---|
127 | char op_plus='+', op_minus='-'; /* op_mult='*', op_dot='.', op_div='/'; */ |
---|
128 | |
---|
129 | crtident = 3 + ident * 2; |
---|
130 | bprintf("%*s%s = ", crtident, "", ls); |
---|
131 | start = strlen( ls ) + 2; |
---|
132 | linelg = 70 - crtident - start - 1; |
---|
133 | |
---|
134 | first = 1; |
---|
135 | while( strlen(rs) > linelg ) { |
---|
136 | ifound = 0; jfound = 0; |
---|
137 | if ( number_of_lines >= MAX_NO_OF_LINES ) { |
---|
138 | /* If a new line needs to be started. |
---|
139 | Note: the approach below will create erroneous code if the +/- is within a subexpression, e.g. for |
---|
140 | A*(B+C) one cannot start a new continuation line by splitting at the + sign */ |
---|
141 | for( j=linelg; j>5; j-- ) /* split row here if +, -, or comma */ |
---|
142 | if ( ( rs[j] == op_plus )||( rs[j] == op_minus )||( rs[j]==',' ) ) { |
---|
143 | jfound = 1; i=j; break; |
---|
144 | } |
---|
145 | } |
---|
146 | if ( ( number_of_lines < MAX_NO_OF_LINES )||( !jfound ) ) { |
---|
147 | for( i=linelg; i>10; i-- ) /* split row here if operator or comma */ |
---|
148 | if ( ( rs[i] & 0x80 )||( rs[i]==',' ) ) { |
---|
149 | ifound = 1; break; |
---|
150 | } |
---|
151 | if( i <= 10 ) { |
---|
152 | printf("\n Warning: possible error in continuation lines for %s = ...",ls); |
---|
153 | i = linelg; |
---|
154 | } |
---|
155 | } |
---|
156 | while ( rs[i-1] & 0x80 ) i--; /* put all operators on the next row */ |
---|
157 | while ( rs[i] == ',' ) i++; /* put commas on the current row */ |
---|
158 | |
---|
159 | c = rs[i]; |
---|
160 | rs[i] = 0; |
---|
161 | |
---|
162 | if ( first ) { /* first line in a split row */ |
---|
163 | bprintf("%s", rs ); |
---|
164 | linelg++; |
---|
165 | first = 0; |
---|
166 | } else {/* continuation line in a split row - but not last line*/ |
---|
167 | bprintf(" ...\n %*s%s", start, "", rs ); |
---|
168 | if ( jfound ) { |
---|
169 | bprintf(" ;\n%*s%s = %s", crtident, "", ls, ls); |
---|
170 | number_of_lines = 1; |
---|
171 | } |
---|
172 | } |
---|
173 | rs[i] = c; |
---|
174 | rs += i; /* jump to the first not-yet-written character */ |
---|
175 | number_of_lines++; |
---|
176 | } |
---|
177 | |
---|
178 | if ( number_of_lines > MAX_NO_OF_LINES ) { |
---|
179 | printf("\n Warning: %d continuation lines for %s = ...",number_of_lines,ls); |
---|
180 | } |
---|
181 | |
---|
182 | if ( first ) bprintf("%s ;\n", rs ); /* non-split row */ |
---|
183 | else bprintf(" ...\n %*s%s;\n", start, "", rs ); /* last line in a split row */ |
---|
184 | |
---|
185 | |
---|
186 | FlushBuf(); |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | /*************************************************************************************************/ |
---|
191 | void MATLAB_WriteComment( char *fmt, ... ) |
---|
192 | { |
---|
193 | Va_list args; |
---|
194 | char buf[ MAX_LINE ]; |
---|
195 | |
---|
196 | va_start( args, fmt ); |
---|
197 | vsprintf( buf, fmt, args ); |
---|
198 | va_end( args ); |
---|
199 | |
---|
200 | fprintf( currentFile, "%c ", '%' ); |
---|
201 | bprintf( "%-65s\n", buf ); |
---|
202 | |
---|
203 | FlushBuf(); |
---|
204 | } |
---|
205 | |
---|
206 | /*************************************************************************************************/ |
---|
207 | char * MATLAB_Decl( int v ) |
---|
208 | { |
---|
209 | static char buf[120]; |
---|
210 | /* VARIABLE *var; |
---|
211 | char *baseType; |
---|
212 | char maxi[20]; |
---|
213 | char maxj[20]; */ |
---|
214 | |
---|
215 | buf[0] = 0; return buf; /* Nothing to declare in matlab */ |
---|
216 | /* var = varTable[ v ]; |
---|
217 | baseType = MATLAB_types[ var->baseType ]; |
---|
218 | |
---|
219 | *buf = 0; |
---|
220 | |
---|
221 | switch( var->type ) { |
---|
222 | case ELM: sprintf( buf, "%s :: %s", |
---|
223 | baseType, var->name ); |
---|
224 | break; |
---|
225 | case VELM: |
---|
226 | if( var->maxi > 0 ) sprintf( maxi, "%d", var->maxi ); |
---|
227 | else sprintf( maxi, "%s", varTable[ -var->maxi ]->name ); |
---|
228 | if( (var->maxi == 0) || |
---|
229 | ((var->maxi < 0) && (varTable[ -var->maxi ]->maxi == 0)) ) |
---|
230 | strcat( maxi, "+1"); |
---|
231 | sprintf( buf, "%s, DIMENSION(%s) :: %s", |
---|
232 | baseType, maxi, var->name ); |
---|
233 | break; |
---|
234 | case MELM: if( var->maxi > 0 ) sprintf( maxi, "%d", var->maxi ); |
---|
235 | else sprintf( maxi, "%s", varTable[ -var->maxi ]->name ); |
---|
236 | if( (var->maxi == 0) || |
---|
237 | ((var->maxi < 0) && (varTable[ -var->maxi ]->maxi == 0)) ) |
---|
238 | strcat( maxi, "+1"); |
---|
239 | if( var->maxj > 0 ) sprintf( maxj, "%d", var->maxj ); |
---|
240 | else sprintf( maxj, "%s", varTable[ -var->maxj ]->name ); |
---|
241 | if( (var->maxj == 0) || |
---|
242 | ((var->maxj < 0 ) && (varTable[ -var->maxj ]->maxi == 0)) ) |
---|
243 | strcat( maxj, "+1"); |
---|
244 | sprintf( buf, "%s, DIMENSION(%s,%s) :: %s", |
---|
245 | baseType, maxi, maxj,var->name ); |
---|
246 | break; |
---|
247 | default: |
---|
248 | printf( "Can not declare type %d\n", var->type ); |
---|
249 | break; |
---|
250 | } |
---|
251 | return buf; |
---|
252 | */ |
---|
253 | } |
---|
254 | |
---|
255 | /*************************************************************************************************/ |
---|
256 | char * MATLAB_DeclareData( int v, void * values, int n) |
---|
257 | { |
---|
258 | int i; |
---|
259 | int nlines, nmax; |
---|
260 | int split; |
---|
261 | static char buf[120]; |
---|
262 | VARIABLE *var; |
---|
263 | int * ival; |
---|
264 | double * dval; |
---|
265 | char ** cval; |
---|
266 | char *baseType; |
---|
267 | char maxi[20]; |
---|
268 | char maxj[20]; |
---|
269 | int maxCols = MAX_COLS; |
---|
270 | char dsbuf[55]; |
---|
271 | |
---|
272 | int splitsize; |
---|
273 | int maxi_mod; |
---|
274 | int maxi_div; |
---|
275 | |
---|
276 | char mynumber[30]; |
---|
277 | |
---|
278 | var = varTable[ v ]; |
---|
279 | ival = (int*) values; |
---|
280 | dval = (double *) values; |
---|
281 | cval = (char **) values; |
---|
282 | |
---|
283 | nlines = 1; |
---|
284 | nmax = 1; |
---|
285 | split = 0; |
---|
286 | var -> maxi = max( n, 1 ); |
---|
287 | |
---|
288 | baseType = MATLAB_types[ var->baseType ]; |
---|
289 | |
---|
290 | *buf = 0; |
---|
291 | |
---|
292 | switch( var->type ) { /* changed here */ |
---|
293 | case ELM: |
---|
294 | /* bprintf( " %s :: %s = ", baseType, var->name ); |
---|
295 | switch ( var->baseType ) { |
---|
296 | case INT: bprintf( "%d", *ival ); break; |
---|
297 | case DOUBLE: bprintf( "%f", *dval); break; |
---|
298 | case REAL: bprintf( "%lg", *dval ); break; |
---|
299 | case STRING: bprintf( "'%3s'", *cval ); break; |
---|
300 | } */ |
---|
301 | break; |
---|
302 | case VELM: |
---|
303 | splitsize = 36; /*elements*/ |
---|
304 | maxi_mod = var->maxi % splitsize; |
---|
305 | maxi_div = var->maxi / splitsize; |
---|
306 | |
---|
307 | if( var->maxi > 0 ) sprintf( maxi, "%d", var->maxi ); |
---|
308 | else sprintf( maxi, "%s", varTable[ -var->maxi ]->name ); |
---|
309 | if( (var->maxi == 0) || |
---|
310 | ((var->maxi < 0) && (varTable[ -var->maxi ]->maxi == 0)) ) |
---|
311 | strcat( maxi, "+1"); |
---|
312 | if( var->maxj > 0 ) sprintf( maxj, "%d", var->maxj ); |
---|
313 | else sprintf( maxj, "%s", varTable[ -var->maxj ]->name ); |
---|
314 | if( (var->maxj == 0) || |
---|
315 | ((var->maxj < 0 ) && (varTable[ -var->maxj ]->maxi == 0)) ) |
---|
316 | strcat( maxj, "+1"); |
---|
317 | /* now list values */ |
---|
318 | /* if ( (var->baseType==STRING)||(var->baseType==DOUBLESTRING) ) { |
---|
319 | bprintf( "%s(1:%s,:) = [ ... \n", var->name, maxi) ; |
---|
320 | } else { |
---|
321 | bprintf( "%s(1:%s) = [ ... \n", var->name, maxi) ; |
---|
322 | }*/ |
---|
323 | if ( (var->baseType==STRING)||(var->baseType==DOUBLESTRING) ) { |
---|
324 | bprintf( "%s = [ ... \n", var->name, maxi) ; |
---|
325 | } else { |
---|
326 | bprintf( "%s = [ ... \n", var->name, maxi) ; |
---|
327 | } |
---|
328 | |
---|
329 | /* if the array is defined in one piece, then the for loop will |
---|
330 | go from 0 to n. Otherwise, there will be partial arrays from |
---|
331 | i_from to i_to which are of size splitsize except for the |
---|
332 | last one which is usually smaller and contains the rest */ |
---|
333 | for ( i=0; i < n; i++ ) { |
---|
334 | switch( var -> baseType ) { |
---|
335 | case INT: |
---|
336 | bprintf( "%4d", ival[i] ); maxCols =12; break; |
---|
337 | case DOUBLE: |
---|
338 | sprintf(mynumber, "%12.6e",dval[i]); |
---|
339 | bprintf( " %s", mynumber ); maxCols = 5; break; |
---|
340 | case REAL: |
---|
341 | bprintf( "%12.6e", dval[i] ); maxCols = 5; break; |
---|
342 | case STRING: |
---|
343 | bprintf( "'%12s'", cval[i] ); maxCols = 3; break; |
---|
344 | case DOUBLESTRING: |
---|
345 | strncpy( dsbuf, cval[i], 54 ); dsbuf[54]='\0'; |
---|
346 | bprintf( "'%48s'", dsbuf ); maxCols=1; break; |
---|
347 | } |
---|
348 | if( i < n-1 ) { |
---|
349 | bprintf( ";" ); |
---|
350 | if( (i+1) % maxCols == 0 ) { |
---|
351 | bprintf( " ... \n" ); |
---|
352 | nlines++; |
---|
353 | } |
---|
354 | } |
---|
355 | } |
---|
356 | bprintf( " ];\n" ); |
---|
357 | break; |
---|
358 | |
---|
359 | case MELM: if( var->maxi > 0 ) sprintf( maxi, "%d", var->maxi ); |
---|
360 | else sprintf( maxi, "%s", varTable[ -var->maxi ]->name ); |
---|
361 | if( (var->maxi == 0) || |
---|
362 | ((var->maxi < 0) && (varTable[ -var->maxi ]->maxi == 0)) ) |
---|
363 | strcat( maxi, "+1"); |
---|
364 | if( var->maxj > 0 ) sprintf( maxj, "%d", var->maxj ); |
---|
365 | else sprintf( maxj, "%s", varTable[ -var->maxj ]->name ); |
---|
366 | if( (var->maxj == 0) || |
---|
367 | ((var->maxj < 0 ) && (varTable[ -var->maxj ]->maxi == 0)) ) |
---|
368 | strcat( maxj, "+1"); |
---|
369 | sprintf( buf, "%s, DIMENSION(%s,%s) :: %s\n", /* changed here */ |
---|
370 | baseType, maxi, maxj,var->name ); |
---|
371 | break; |
---|
372 | default: |
---|
373 | printf( "Can not declare type %d", var->type ); |
---|
374 | break; |
---|
375 | } |
---|
376 | return buf; |
---|
377 | } |
---|
378 | |
---|
379 | /*************************************************************************************************/ |
---|
380 | void MATLAB_Declare( int v ) |
---|
381 | { |
---|
382 | if( varTable[ v ]->comment ) { |
---|
383 | MATLAB_WriteComment( "%s - %s", |
---|
384 | varTable[ v ]->name, varTable[ v ]->comment ); |
---|
385 | } |
---|
386 | FlushBuf(); |
---|
387 | bprintf(" %s\n", MATLAB_Decl(v) ); |
---|
388 | |
---|
389 | FlushBuf(); |
---|
390 | } |
---|
391 | |
---|
392 | /*************************************************************************************************/ |
---|
393 | void MATLAB_ExternDeclare( int v ) |
---|
394 | { |
---|
395 | if( varTable[ v ]->comment ) { |
---|
396 | MATLAB_WriteComment( "%s - %s", |
---|
397 | varTable[ v ]->name, varTable[ v ]->comment ); |
---|
398 | } |
---|
399 | FlushBuf(); |
---|
400 | bprintf(" global %s;\n", varTable[ v ]->name ); |
---|
401 | FlushBuf(); |
---|
402 | } |
---|
403 | |
---|
404 | |
---|
405 | /*************************************************************************************************/ |
---|
406 | void MATLAB_GlobalDeclare( int v ) |
---|
407 | { |
---|
408 | } |
---|
409 | |
---|
410 | |
---|
411 | /*************************************************************************************************/ |
---|
412 | void MATLAB_DeclareConstant( int v, char *val ) |
---|
413 | { |
---|
414 | VARIABLE *var; |
---|
415 | int ival; |
---|
416 | char dummy_val[100]; /* used just to avoid strange behaviour of |
---|
417 | sscanf when compiled with gcc */ |
---|
418 | |
---|
419 | strcpy(dummy_val,val);val = dummy_val; |
---|
420 | |
---|
421 | var = varTable[ v ]; |
---|
422 | |
---|
423 | if( sscanf(val, "%d", &ival) == 1 ) |
---|
424 | if( ival == 0 ) var->maxi = 0; |
---|
425 | else var->maxi = 1; |
---|
426 | else |
---|
427 | var->maxi = -1; |
---|
428 | |
---|
429 | if( var->comment ) |
---|
430 | MATLAB_WriteComment( "%s - %s", var->name, var->comment ); |
---|
431 | |
---|
432 | switch( var->type ) { |
---|
433 | case CONST: bprintf(" global %s;",var->name, val ); |
---|
434 | bprintf(" %s = %s; \n", var->name, val ); |
---|
435 | break; |
---|
436 | default: |
---|
437 | printf( "Invalid constant %d", var->type ); |
---|
438 | break; |
---|
439 | } |
---|
440 | |
---|
441 | FlushBuf(); |
---|
442 | } |
---|
443 | |
---|
444 | |
---|
445 | /*************************************************************************************************/ |
---|
446 | void MATLAB_WriteVecData( VARIABLE * var, int min, int max, int split ) |
---|
447 | { |
---|
448 | char buf[80]; |
---|
449 | |
---|
450 | if( split ) |
---|
451 | sprintf( buf, "%6sdata( %s(i), i = %d, %d ) / &\n%5s", |
---|
452 | " ", var->name, min, max, " " ); |
---|
453 | else |
---|
454 | sprintf( buf, "%6sdata %s / &\n%5s", |
---|
455 | " ", var->name, " " ); |
---|
456 | |
---|
457 | FlushThisBuf( buf ); |
---|
458 | bprintf( " / \n\n" ); |
---|
459 | FlushBuf(); |
---|
460 | } |
---|
461 | |
---|
462 | /*************************************************************************************************/ |
---|
463 | void MATLAB_DeclareDataOld( int v, int * values, int n ) |
---|
464 | { |
---|
465 | int i; |
---|
466 | int nlines, min, max; |
---|
467 | int split; |
---|
468 | VARIABLE *var; |
---|
469 | int * ival; |
---|
470 | double * dval; |
---|
471 | char **cval; |
---|
472 | int maxCols = MAX_COLS; |
---|
473 | char dsbuf[55]; |
---|
474 | |
---|
475 | var = varTable[ v ]; |
---|
476 | ival = (int*) values; |
---|
477 | dval = (double*) values; |
---|
478 | cval = (char**) values; |
---|
479 | |
---|
480 | nlines = 1; |
---|
481 | min = max = 1; |
---|
482 | split = 0; |
---|
483 | |
---|
484 | switch( var->type ) { |
---|
485 | case VELM: if( n <= 0 ) break; |
---|
486 | for( i = 0; i < n; i++ ) { |
---|
487 | switch( var->baseType ) { |
---|
488 | case INT: bprintf( "%3d", ival[i] ); maxCols=12; break; |
---|
489 | case DOUBLE: |
---|
490 | case REAL:bprintf( "%5lg", dval[i] ); maxCols=8; break; |
---|
491 | case STRING:bprintf( "'%s'", cval[i] ); maxCols=5; break; |
---|
492 | case DOUBLESTRING: |
---|
493 | strncpy( dsbuf, cval[i], 54 ); dsbuf[54]='\0'; |
---|
494 | bprintf( "'%48s'", dsbuf ); maxCols=1; break; |
---|
495 | } |
---|
496 | if( ( (i+1) % 12 == 0 ) && ( nlines > MAX_LINES ) ) { |
---|
497 | split = 1; nlines = 1; |
---|
498 | MATLAB_WriteVecData( var, min, max, split ); |
---|
499 | min = max + 1; |
---|
500 | } |
---|
501 | else { |
---|
502 | if( i < n-1 ) bprintf( "," ); |
---|
503 | if( (i+1) % maxCols == 0 ) { |
---|
504 | bprintf( "\n%5s", " " ); |
---|
505 | nlines++; |
---|
506 | } |
---|
507 | } |
---|
508 | max ++; |
---|
509 | } |
---|
510 | MATLAB_WriteVecData( var, min, max-1, split ); |
---|
511 | break; |
---|
512 | |
---|
513 | case ELM: bprintf( "%6sdata %s / ", " ", var->name ); |
---|
514 | switch( var->baseType ) { |
---|
515 | case INT: bprintf( "%d", *ival ); break; |
---|
516 | case DOUBLE: |
---|
517 | case REAL:bprintf( "%lg", *dval ); break; |
---|
518 | case STRING:bprintf( "'%s'", *cval ); break; |
---|
519 | case DOUBLESTRING: |
---|
520 | strncpy( dsbuf, *cval, 54 ); dsbuf[54]='\0'; |
---|
521 | bprintf( "'%s'", dsbuf ); maxCols=1; break; |
---|
522 | /* bprintf( "'%50s'", *cval ); break; */ |
---|
523 | } |
---|
524 | bprintf( " / \n" ); |
---|
525 | FlushBuf(); |
---|
526 | break; |
---|
527 | default: |
---|
528 | printf( "\n Function not defined !\n" ); |
---|
529 | break; |
---|
530 | } |
---|
531 | } |
---|
532 | |
---|
533 | /*************************************************************************************************/ |
---|
534 | void MATLAB_InitDeclare( int v, int n, void * values ) |
---|
535 | { |
---|
536 | VARIABLE * var; |
---|
537 | |
---|
538 | var = varTable[ v ]; |
---|
539 | var->maxi = max( n, 1 ); |
---|
540 | |
---|
541 | NewLines(1); |
---|
542 | MATLAB_DeclareData( v, values, n ); |
---|
543 | } |
---|
544 | |
---|
545 | /*************************************************************************************************/ |
---|
546 | void MATLAB_FunctionStart( int f, int *vars ) |
---|
547 | { |
---|
548 | int i; |
---|
549 | int v; |
---|
550 | char * name; |
---|
551 | int narg; |
---|
552 | |
---|
553 | name = varTable[ f ]->name; |
---|
554 | narg = varTable[ f ]->maxi; |
---|
555 | |
---|
556 | bprintf("function " ); |
---|
557 | if( narg >= 1 ) { |
---|
558 | v = vars[ narg-1 ]; |
---|
559 | bprintf("[ %s ] = ", varTable[ v ]->name ); |
---|
560 | } |
---|
561 | bprintf(" %s_%s ( ", rootFileName, name ); |
---|
562 | for( i = 0; i < narg-1; i++ ) { |
---|
563 | v = vars[ i ]; |
---|
564 | bprintf("%s ", varTable[ v ]->name ); |
---|
565 | if (i<narg-2) bprintf(", "); |
---|
566 | } |
---|
567 | bprintf(")\n"); |
---|
568 | |
---|
569 | FlushBuf(); |
---|
570 | } |
---|
571 | |
---|
572 | /*************************************************************************************************/ |
---|
573 | void MATLAB_FunctionPrototipe( int f, ... ) |
---|
574 | { |
---|
575 | char * name; |
---|
576 | int narg; |
---|
577 | |
---|
578 | name = varTable[ f ]->name; |
---|
579 | narg = varTable[ f ]->maxi; |
---|
580 | |
---|
581 | bprintf(" EXTERNAL %s\n", name ); |
---|
582 | |
---|
583 | FlushBuf(); |
---|
584 | } |
---|
585 | |
---|
586 | /*************************************************************************************************/ |
---|
587 | void MATLAB_FunctionBegin( int f, ... ) |
---|
588 | { |
---|
589 | Va_list args; |
---|
590 | int i; |
---|
591 | int vars[20]; |
---|
592 | char * name; |
---|
593 | int narg; |
---|
594 | char buf[200]; |
---|
595 | time_t t; |
---|
596 | |
---|
597 | name = varTable[ f ]->name; |
---|
598 | narg = varTable[ f ]->maxi; |
---|
599 | |
---|
600 | /*Adi - each Matlab functin requires a separate file*/ |
---|
601 | sprintf( buf, "%s_%s.m", rootFileName, varTable[ f ]->name ); |
---|
602 | mex_funFile = fopen(buf, "w"); |
---|
603 | if( mex_funFile == 0 ) { |
---|
604 | FatalError(3,"%s: Can't create file", buf ); |
---|
605 | } |
---|
606 | UseFile( mex_funFile ); |
---|
607 | /*Adi*/ |
---|
608 | |
---|
609 | |
---|
610 | Va_start( args, f ); |
---|
611 | for( i = 0; i < narg; i++ ) |
---|
612 | vars[ i ] = va_arg( args, int ); |
---|
613 | va_end( args ); |
---|
614 | |
---|
615 | CommentFncBegin( f, vars ); |
---|
616 | |
---|
617 | WriteDelim(); |
---|
618 | WriteComment(""); |
---|
619 | WriteComment("Generated by KPP - symbolic chemistry Kinetics PreProcessor" ); |
---|
620 | WriteComment(" KPP is developed at CGRER labs University of Iowa by" ); |
---|
621 | WriteComment(" Valeriu Damian & Adrian Sandu" ); |
---|
622 | WriteComment(""); |
---|
623 | WriteComment("%-20s : %s", "File", buf ); |
---|
624 | strcpy( buf, (char*)ctime( &t ) ); |
---|
625 | buf[ (int)strlen(buf) - 1 ] = 0; |
---|
626 | WriteComment("%-20s : %s", "Time", buf ); |
---|
627 | WriteComment("%-20s : %s", "Working directory", getcwd(buf, 200) ); |
---|
628 | WriteComment("%-20s : %s", "Equation file", eqFileName ); |
---|
629 | WriteComment("%-20s : %s", "Output root filename", rootFileName ); |
---|
630 | WriteComment(""); |
---|
631 | WriteDelim(); |
---|
632 | NewLines(1); |
---|
633 | |
---|
634 | MATLAB_FunctionStart( f, vars ); |
---|
635 | NewLines(1); |
---|
636 | |
---|
637 | FlushBuf(); |
---|
638 | |
---|
639 | MapFunctionComment( f, vars ); |
---|
640 | } |
---|
641 | |
---|
642 | /*************************************************************************************************/ |
---|
643 | void MATLAB_FunctionEnd( int f ) |
---|
644 | { |
---|
645 | bprintf(" \nreturn\n\n"); |
---|
646 | |
---|
647 | FlushBuf(); |
---|
648 | |
---|
649 | CommentFunctionEnd( f ); |
---|
650 | |
---|
651 | /*Adi*/ |
---|
652 | fclose(mex_funFile); |
---|
653 | |
---|
654 | |
---|
655 | } |
---|
656 | |
---|
657 | /*************************************************************************************************/ |
---|
658 | void MATLAB_Inline( char *fmt, ... ) |
---|
659 | { |
---|
660 | Va_list args; |
---|
661 | char buf[ 1000 ]; |
---|
662 | |
---|
663 | if( useLang != MATLAB_LANG ) return; |
---|
664 | |
---|
665 | Va_start( args, fmt ); |
---|
666 | vsprintf( buf, fmt, args ); |
---|
667 | va_end( args ); |
---|
668 | bprintf( "%s\n", buf ); |
---|
669 | |
---|
670 | FlushBuf(); |
---|
671 | } |
---|
672 | |
---|
673 | /*************************************************************************************************/ |
---|
674 | void Use_MATLAB() |
---|
675 | { |
---|
676 | WriteElm = MATLAB_WriteElm; |
---|
677 | WriteSymbol = MATLAB_WriteSymbol; |
---|
678 | WriteAssign = MATLAB_WriteAssign; |
---|
679 | WriteComment = MATLAB_WriteComment; |
---|
680 | DeclareConstant = MATLAB_DeclareConstant; |
---|
681 | Declare = MATLAB_Declare; |
---|
682 | ExternDeclare = MATLAB_ExternDeclare; |
---|
683 | GlobalDeclare = MATLAB_GlobalDeclare; |
---|
684 | InitDeclare = MATLAB_InitDeclare; |
---|
685 | |
---|
686 | FunctionStart = MATLAB_FunctionStart; |
---|
687 | FunctionPrototipe = MATLAB_FunctionPrototipe; |
---|
688 | FunctionBegin = MATLAB_FunctionBegin; |
---|
689 | FunctionEnd = MATLAB_FunctionEnd; |
---|
690 | |
---|
691 | OpenFile( ¶m_headerFile, rootFileName, "_Parameters.m","Parameter Definition File" ); |
---|
692 | OpenFile( &driverFile, rootFileName, "_Main.m", "Main Program File" ); |
---|
693 | OpenFile( &rateFile, rootFileName, "_Rates.m", |
---|
694 | "The Reaction Rates File" ); |
---|
695 | if ( useStoicmat ) { |
---|
696 | OpenFile( &stoichiomFile, rootFileName, "_Stoichiom.m", |
---|
697 | "The Stoichiometric Chemical Model File" ); |
---|
698 | OpenFile( &sparse_stoicmFile, rootFileName, "_StoichiomSP.m", |
---|
699 | "Sparse Stoichiometric Data Structures File" ); |
---|
700 | } |
---|
701 | OpenFile( &utilFile, rootFileName, "_Util.m", |
---|
702 | "Auxiliary Routines File" ); |
---|
703 | OpenFile( &sparse_dataFile, rootFileName, "_Sparse.m", |
---|
704 | "Sparse Data Definition File" ); |
---|
705 | OpenFile( &global_dataFile, rootFileName, "_Global_defs.m", "Global Data Definition File" ); |
---|
706 | if ( useJacSparse ) { |
---|
707 | OpenFile( &sparse_jacFile, rootFileName, "_JacobianSP.m", |
---|
708 | "Sparse Jacobian Data Structures File" ); |
---|
709 | } |
---|
710 | if ( useHessian ) { |
---|
711 | OpenFile( &sparse_hessFile, rootFileName, "_HessianSP.m", |
---|
712 | "Sparse Hessian Data Structures File" ); |
---|
713 | } |
---|
714 | OpenFile( &mapFile, rootFileName, ".map", |
---|
715 | "Map File with Human-Readable Information" ); |
---|
716 | OpenFile( &monitorFile, rootFileName, "_Monitor.m", |
---|
717 | "Utility Data Definition File" ); |
---|
718 | } |
---|