# Set parameters to be studied
# Set pressure, inlet temperature, and equivalence ratio ranges
p_list = range(1, 2) # pressure [atm]
phi_list = [x * 0.1 for x in range(10,11)]
T_list = [y * 100 for y in range(3,4)]
res_time_list = [z * 1e-6 for z in range(1,10000)]
for p in p_list:
for phi in phi_list:
reactants = ics.format(phi)
for Tin in T_list:
for res_time in res_time_list:
outfile.write('{}, {}, {}, {},'.format(p, Tin, phi, res_time))
gas_inlet.TPX = Tin, p*ct.one_atm, reactants
psr(gas, gas_inlet, reactants, Tin, p, res_time, outfile, integrator)list_1 = [1,2,3,4]
list_2 = [5,6,7,8]
for i, j in zip(list_1, list_2):
print(i, j)
1, 5
2, 6
3, 7
4, 8from itertools import product
for i, j in product(list_1, list_2):
print(i, j)
1, 5
1, 6
1, 7
1, 8
2, 5
etc.from multiprocessing import Pool
from itertools import product
def run_psr_cases(res_time, reactants, p, Tin, whatever other arguments):
gas = ct.Solution('gri30.xml')
gas.TPX = Tin, p, reactants
# etc.
# return whatever results you want to collect
return reac.T, reac.P, netw.time, reac.X
# Set your parameter lists
...
n_processes = 5
with Pool(n_processes) as p:
results = p.starmap(run_psr_cases, product(all your parameters))
from itertools import product
from multiprocessing import Pool
def my_pow(x, y):
return x, y, "{} to the power of {} equals {}".format(x, y, x**y)
if __name__ == '__main__':
list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
print(list(zip(list_1, list_2)))
print(list(product(list_1, list_2)))
with Pool(5) as p:
a = p.starmap(pow, zip(list_1, list_2))
print(a)
with Pool(5) as p:
b = p.starmap(pow, product(list_1, list_2))
print(b)
print(my_pow(3, 2))
with Pool(5) as p:
c = p.starmap(my_pow, zip(list_1, list_2))
print(c)
with Pool(5) as p:
d = p.starmap(my_pow, product(list_1, list_2))
print(d)
print(range(1, 2))
<range object at.... >
print(list(range(1, 2)))
[1, 2]...
result = np.array(pool.starmap(run_case,send))...
# ... code here
return [val_1, val_2, val_3]C:\Work>python PSR_min.py
Traceback (most recent call last):
File "PSR_min.py", line 39, in <module>
gas = ct.Solution('grimech30.cti')
File "cantera\base.pyx", line 26, in cantera._cantera._SolutionBase.__cinit__
(interfaces\cython\cantera\_cantera.cpp:6785)
File "cantera\base.pyx", line 47, in cantera._cantera._SolutionBase._init_cti_
xml (interfaces\cython\cantera\_cantera.cpp:7155)
RuntimeError:
***********************************************************************
CanteraError thrown by get_modified_time:
Couldn't open file:./grimech30.ctiimport time
while True:
try:
gas = ct.Solution('gri30.cti')
break
except Exception:
print('...')
time.sleep(np.random.random())
import cantera as ct
gas = ct.Solution('gri30.xml')
def psr(Tin):
gas.TP = Tin, None
if __name__ == '__main__':
print(gas.T)
psr(600)
print(gas.T)
import cantera as ct
def psr(Tin):
gas.TP = Tin, None
if __name__ == '__main__':
gas = ct.Solution('gri30.xml')
print(gas.T)
psr(600)
print(gas.T)
C:\Work>python PSR_min.py
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\Python35\lib\multiprocessing\spawn.py", line 106, in sp
awn_main
exitcode = _main(fd)
File "C:\Program Files\Python35\lib\multiprocessing\spawn.py", line 115, in _m
ain
prepare(preparation_data)
File "C:\Program Files\Python35\lib\multiprocessing\spawn.py", line 226, in pr
epare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Program Files\Python35\lib\multiprocessing\spawn.py", line 278, in _f
ixup_main_from_path
run_name="__mp_main__")
File "C:\Program Files\Python35\lib\runpy.py", line 240, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Program Files\Python35\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Program Files\Python35\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Work\PSR_min.py", line 8, in <module>
gas = ct.Solution('grimech30.cti')
File "interfaces\cython\cantera\base.pyx", line 26, in cantera._cantera._Solut
ionBase.__cinit__ (interfaces\cython\cantera\_cantera.cpp:6934)
File "interfaces\cython\cantera\base.pyx", line 47, in cantera._cantera._Solut
ionBase._init_cti_xml (interfaces\cython\cantera\_cantera.cpp:7323)
RuntimeError:
***********************************************************************
CanteraError thrown by findInputFile:
Input file grimech30.cti not found in directories
'C:\Program Files\Python35\lib\site-packages\cantera\data',
'.',
'C:\Program Files\Cantera\data',
'C:\Program Files\Cantera\data'
To fix this problem, either:
a) move the missing files into the local directory;
b) define environment variable CANTERA_DATA to
point to the directory containing the file.
*********************************************************************** res_time_list = [z for z in np.logspace(-6, -2, 2000)]******************************************************
CanteraError thrown by Phase::setDensity():
density must be positive
******************************************************gas.TPX = Tinit, p*ct.one_atm, reactants
gas.equilibrate('hp')
gas.TPX = gas.T-300, p*ct.one_atm, reactantsgas.TPX = Tin, p*ct.one_atm, reactants
gas.equilibrate('hp')
if i == timestep-1:
return p, phi, Tin, res_time, reactor.T, gas.P with Pool(nProcs) as p:
vit_result = np.array(p.starmap(vitiator, product(p_list, Tvit_list, Tvit_target_list, T_tol)), dtype=object)
result = np.array(p.starmap(psr, [vit_result[0,5], product(T_rise_list, Eff_list, T_tol), vit_result[0,3], vit_result[0,6]]))>>> f = lambda x,y: (x,y)
>>> list(itertools.starmap(f, itertools.product(range(4), [99])))
[(0, 99), (1, 99), (2, 99), (3, 99)]return p, phi, Tvit_in, Tpsr_in, XO2, Ppsr_in, XALLresult = np.array(p.starmap(psr, product(Ppsr_in, T_rise_list, Eff_list, T_tol, Tpsr_in, [XALL])), dtype=object)