13 predici_command_list=[
'gettemp',
'getmass',
'getvol',
'getco',
'cpol',
'getmmlow']
14 predici_command_arg_list=[0,0,0,2,2,2]
15 predici_command_type_list=[
'double',
'double',
'double',
'double',
'double',
'double']
16 predici_command_replace_list=[
'my_temp',
'my_cell_mass',
'my_cell_volume',
'my_conc[MY_NEW_INDEX]',
'my_conc[MY_NEW_INDEX]',
'my_molma[MY_NEW_INDEX]']
17 fluent_command_replace_list=[
'C_T(c,t)',
'C_VOLUME(c,t)*C_R(c,t)',
'C_VOLUME(c,t)',
'my_conc[MY_NEW_INDEX]',
'C_UDSI(c,t,MY_NEW_INDEX)',
'my_molma[MY_NEW_INDEX]']
18 fluent_command_array_size_list=[
'0',
'0',
'0',
'40',
'40',
'40']
19 target_list=[
'Openfoam',
'Fluent']
23 func_type_list=[
'void',
'bool',
'int',
'long',
'double',
'float',
'char']
29 def firststring(stringname,string_list):
32 for index
in range(len(string_list)):
33 index_sstr = stringname.find(string_list[index])
37 myname = string_list[index]
39 if myindex > index_sstr:
41 myname = string_list[index]
42 return myname, myindex
55 def check_func_name(str1,sub_str1,i1):
57 strb = str1[i1+len(sub_str1):]
59 stra = stra.replace(
'\n',
' ')
60 stra = stra.replace(
'\t',
' ')
61 strb = strb.replace(
'\t',
' ')
62 strb = strb.replace(
'\n',
' ')
67 elif stra[len(stra)-1:len(stra)] ==
' ':
74 elif strb[0:1] ==
'(':
79 if (icheck1 == 1)
and (icheck2 == 1):
94 def corresponding_bracket_index(str1,str2,i1):
96 if str2
not in [
'(',
'{',
'[']:
97 print 'Error in function corresponding_bracket_index. str2 is not an opening bracket' 108 while index < len(str1)
and count_bracket > 0:
109 while str1[index:].find(str2)>=0
or str1[index:].find(str3)>=0:
110 index_b1 = str1[index:].find(str2)
111 index_b2 = str1[index:].find(str3)
112 if (index_b1 == -1)
and (index_b2 >=0):
114 index = index+index_b2+1
115 if count_bracket <= 0:
118 print 'Unbalanced bracket' 120 if (index_b2 == -1)
and (index_b1 >=0):
121 print 'Unbalanced bracket' 123 if (index_b1 < index_b2):
125 index = index+index_b1+1
126 elif (index_b2 < index_b1):
128 index = index+index_b2+1
129 if count_bracket <= 0:
131 print 'Error in function corresponding_bracket_index. Wrong syntax of interpreted source code' 138 def func_2_voidfunc(code_string,func_type):
139 itype = len(func_type)
140 while code_string.find(func_type) >= 0:
141 ibool = code_string.find(func_type)
142 temp_string1 = code_string[:ibool+itype]
143 temp_string1 = temp_string1.replace(func_type,
'void')
144 temp_string2 = code_string[ibool+itype:]
145 first_string, first_index = firststring(temp_string2,[
'(',
';',
'='])
146 if first_string ==
'(':
148 temp_string1 += temp_string2[:first_index]
149 temp_string2 = temp_string2[first_index:]
150 sc_index = temp_string2.find(
'{')
151 ec_index = corresponding_bracket_index(temp_string2,
'{',sc_index)
152 temp_string1 += temp_string2[:sc_index]
153 temp_string3 = temp_string2[ec_index:]
154 temp_string2 = temp_string2[sc_index:ec_index]
155 while temp_string2.find(
'return') >= 0:
156 index1 = temp_string2.find(
'return')
157 temp_string1 += temp_string2[:index1+6]
158 temp_string2 = temp_string2[index1+6:]
159 if temp_string2[0] ==
' ':
160 index2 = temp_string2.find(
';')
161 temp_string1 += temp_string2[index2]
162 temp_string2 = temp_string2[index2+1:]
163 temp_string1 += temp_string2+temp_string3
166 code_string = temp_string1+temp_string2
183 def my_find_func( str1, str2 ):
184 index4 = str1.find(str2)
189 bindex = str1.find(
'(',index4+len(str2))
193 str3 = str1[index4+len(str2):bindex]
200 pchar = str1[index4-1]
201 if pchar
not in [
' ',
'=',
'>',
'<',
'*',
'+',
'-',
'/',
'(',
',']:
205 index5 = str1.find(
')',index4,)
206 while str1.find(
'(',index4+1) > -1
and str1.find(
'(',index4+1) < index5:
207 index5 = str1.find(
')',index5+1)
208 index4_ = str1.find(
'(',index4+1)
211 return index_start, index_end
230 def get_arg_list(str,i1,i2):
231 indexb = str.find(
'(',i1,i2)
233 arg_string = str[indexb:i2]
251 def get_list_entry(str1,str2,i1):
252 arg_list = str1.split(str2)
253 arg_string = arg_list[i1-1]
271 def delete_func(code_string):
272 my_delete_list =[
'exp',
'power']
275 while len(code_string) > 0:
276 functype,itype = firststring(code_string,func_type_list)
277 funcname,ifunc = firststring(code_string,my_delete_list)
278 if itype < 0
or ifunc < 0:
279 new_code += code_string
283 new_code += code_string[:itype]
284 code_string = code_string[itype:]
286 test_string = code_string[itype+len(functype):ifunc]
287 test_string = test_string.strip()
288 if test_string ==
'':
289 istart,iend = my_find_func(code_string,funcname)
290 new_code += code_string[:itype]
291 code_string = code_string[iend:]
292 mystring,myindex=firststring(code_string,[
';',
'{'])
294 code_string = code_string[myindex:]
296 iend = corresponding_bracket_index(code_string,
'{',myindex)
297 code_string = code_string[iend+1:]
299 new_code += code_string[:itype+len(functype)]
300 code_string = code_string[itype+len(functype):]
317 while index < len(str1):
318 index = str1[index:].find(
'for')+index
320 str2 = str1[index+3:]
325 elif str1[index-1:index]
in [
' ',
';',
'}',
'{']:
349 def remove_loop_int_declaration(str1,i1):
350 index=i1+str1[i1:].find(
'(')
351 str2 = str1[index+1:]
353 if str2.find(
'int ') == 0:
354 index_start = str1[index:].find(
'int ')+index
355 index_end = index_start+4
356 str2=str2[4:].lstrip()
357 str2=str2[:str2.find(
'=')]
359 str3 = str1[:i1]+
'int '+str2+
';'+str1[i1:index_start]+str1[index_end:]
376 def remove_all_loop_int_dec(str1):
379 iloop = find_loop(str1)
380 str2 = remove_loop_int_declaration(str1,iloop)
381 new_code += str2[:iloop+3]
382 str1 = str2[iloop+3:]
394 def remove_semicolon(str1):
395 my_list_f1 = [
'{',
'}']
401 index = str1.find(
';')
406 temp_string = str1[index+1:].replace(
' ',
'')
407 if temp_string[0]
in my_list_b1:
408 str3, delindex = firststring(str1[index+1:],my_list_b1)
409 str1 = str1[:index+1]+str1[index+1+delindex+1:]
411 temp_string=str1[:index+1].replace(
' ',
'')
412 if temp_string[len(temp_string)-2]
in my_list_f1:
413 if temp_string[len(temp_string)-2]
not in my_list_f2:
414 temp_string = str1[index+1:].replace(
' ',
'')
415 if temp_string[0]
in my_list_b2:
416 str1 = str1[:index]+str1[index+1:]
418 str2 += str1[:index+1]
419 str1 = str1[index+1:]
421 str1 = str1[:index]+str1[index+1:]
423 temp_string=str1[:index+1].replace(
' ',
'')
424 i1 = len(temp_string)-5
425 i2 = len(temp_string)-1
426 temp_string=temp_string[i1:i2]
427 if temp_string ==
'else':
428 str1 = str1[:index]+str1[index+1:]
430 str2 += str1[:index+1]
431 str1 = str1[index+1:]
444 def pass_by_reference_2_c(str1):
446 searchstring=
'double& result1, double& result2' 447 replacestring =
'double* results' 448 while index < len(str1):
449 deltai = str1[index:].find(searchstring)
454 icurli = index+str1[index:].find(
'{')
455 isemi = index+str1[index:].find(
';')
456 if icurli >= 0
and (icurli < isemi
or isemi < 0):
458 ibend = index+corresponding_bracket_index(sub0,
'{',icurli-index)
460 sub2 = str1[icurli:ibend+1]
461 sub3 = str1[ibend+1:]
462 sub2 = sub2.replace(
'result1',
'results[0]')
463 sub2 = sub2.replace(
'result2',
'results[1]')
464 str1 = sub1+sub2+sub3
465 index = len(sub1)+len(sub2)
467 index +=len(searchstring)
469 str1 = str1.replace(searchstring,replacestring)
471 index = str1.find(
'k1, k2, k1, k2')
476 str2 += str1[:index-1]
477 str1 = str1[index-1:]
478 isemi = str1.find(
';')
479 str1 =
'k1, k2, results); k1 = results[0]; k2 = results[1];'+str1[isemi+1:]
481 index = str2.find(
'void F')
482 icurli = index + str2[index:].find(
'{')
483 sub1 = str2[:icurli+1]
484 sub2 =
'double results[2];' 485 sub3 = str2[icurli+1:]
486 str2 = sub1+sub2+sub3
487 index = str2.find(
'void A')
488 icurli = index + str2[index:].find(
'{')
489 sub1 = str2[:icurli+1]
490 sub2 =
'double results[2];' 491 sub3 = str2[icurli+1:]
492 str2 = sub1+sub2+sub3
499 def reformat_code(lines):
501 for index
in range(len(lines)):
502 new_string = lines[index]
503 icom = new_string.find(
'//')
505 lines[index]=new_string[0:icom]
507 for index
in range(len(lines)):
508 new_string = lines[index]
509 new_string = new_string.strip(
' \t\r\n')
510 new_string = new_string.replace(
'\t',
' ')
511 while new_string.find(
' ') >= 0:
512 new_string = new_string.replace(
' ',
' ')
513 lines[index] = new_string
517 for index
in range(len(lines)):
518 code_string+=lines[index]
525 def add_newlines(code_string):
526 code_string = code_string.replace (
';',
';\n')
527 code_string = code_string.replace (
'{',
'\n{\n')
528 code_string = code_string.replace (
'}',
'}\n')
535 def add_call_FandA(mystring):
536 mystring+=
'void FandA(double* x, double t, double* fall){int i=0; double fx[DIM]; double dfx[DIM]; F(x,t,fx); A(x,t,dfx); for (i=0; i< DIM; i++){fall[i]=fx[i];fall[i+DIM]=dfx[i];} return;}' 541 def create_args(kinetics_name):
543 fin = open(kinetics_name,
'r') 544 lines=fin.readlines() 551 code_string = reformat_code(lines)
556 code_string = func_2_voidfunc(code_string,
'bool')
558 code_string = delete_func(code_string)
564 code_string = remove_all_loop_int_dec(code_string)
569 index = code_string.find(
'void F')
571 index1 = index+code_string[index:].find(
'{')
572 initcode =
'if (my_global_init == -1) {SetGlobalIniParam();my_global_init=1;}' 573 code_string = code_string[:index1+1]+initcode+code_string[index1+1:]
578 code_string = remove_semicolon(code_string)
583 code_string = pass_by_reference_2_c(code_string)
585 code_string = add_newlines(code_string)
587 new_lines = code_string.split(
'\n')
592 new_lines.insert(0,
'#include <math.h>\n')
593 new_lines.insert(1,
'int my_global_init = -1;\n')
596 for index
in range(len(new_lines)):
597 str3+=new_lines[index]
601 predicicode = add_call_FandA(str3)
603 predicicode = add_newlines(predicicode)
607 headerfilelist=[
'modena.h',
'math.h']
611 for index
in range(len(headerfilelist)):
612 codestring +=
'#include "'+headerfilelist[index]+
'"\n' 616 void FandA(double* x, double t, double* fall); 619 void predici_kinetics 621 const modena_model_t* model, 622 const double* inputs, 626 double time = inputs[0]; 627 double * inputParams = (double *) (&(inputs[1])); 628 FandA(inputParams, time, outputs); 632 codestring += predicicode
636 index = str3.find(
'XNAMES')
637 strnames=str3[index:len(str3)]
638 istart=strnames.find(
'{')
639 iend=strnames.find(
'}')
640 strnames=strnames[istart+1:iend]
641 namelist = strnames.split(
',')
642 for index
in range(len(namelist)):
643 namelist[index] = namelist[index].replace(
'"',
"'")
645 namelist.insert(0,
"'kineticTime'");
647 returnDict[
'Ccode'] = codestring
648 returnDict[
'inputs'] = {}
649 returnDict[
'outputs'] = {}
650 returnDict[
'parameters'] = {}
652 for index
in range(len(namelist)):
653 returnDict[
'inputs'][namelist[index]] = \
654 {
'min': -10,
'max': 9e99,
'argPos': index}
659 index = str3.find(
'XTYPES')
660 strnames=str3[index:len(str3)]
661 istart=strnames.find(
'{')
662 iend=strnames.find(
'}')
663 strnames=strnames[istart+1:iend]
664 typelist = strnames.split(
',')
668 for index
in range(len(namelist)):
669 source_name=
'source_'+namelist[index].strip(
"'")
670 returnDict[
'outputs'][source_name] = \
671 {
'min': -9e99,
'max': 9e99,
'argPos': index_out}
674 for index
in range(len(namelist)):
675 dsource_name=
'dsource_'+namelist[index].strip(
"'")
676 returnDict[
'outputs'][dsource_name] = \
677 {
'min': -9e99,
'max': 9e99,
'argPos': index_out}