-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanoFoam.py
executable file
·550 lines (426 loc) · 21.4 KB
/
nanoFoam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
"""
@author: Giorgio La Civita, UNIBO DIN
"""
# SimNANODOME Engine
import multiprocessing, os, numpy as np, matplotlib.pyplot as plt, csv
from osp.wrappers.simcfd.cfdsession import CFDSession
from osp.wrappers.simnanodome.nanosession import NanoDOMESession
from osp.wrappers.simelenbaas.elenbaassession import ElenbaasSession
from osp.wrappers.simcoupledreactor.coupledreactorsession import CoupledReactorSession
from osp.core.namespaces import nanofoam as onto
from osp.core.utils import pretty_print
# Engine settings
#####################################################################
# modes supported: elenbaas (elenbaas only), nanodome (nanodome only),
# cfd (openfoam and elenbaas), linked (nanofoam), coupled (nanocouplefoam)
mode = 'coupled'
# Create the computational mesh, boundary conditions and properties
#####################################################################
# Set the accuracy level
# accuracy_level = onto.LowAccuracyLevel()
accuracy_level = onto.MediumAccuracyLevel()
# accuracy_level = onto.HighAccuracyLevel()
# Create precursor's species
prec = onto.SolidPrecursor()
prec_feedrate = onto.FeedRate(value = 125/1000/3600, unit = 'kg/s', name = 'Feed Rate')
prec_type = onto.Type(name = 'Fe')
prec.add(prec_feedrate, prec_type, rel = onto.hasProperty)
# Create plasma's source operative conditions
source = onto.PlasmaSource()
ipower = onto.InputPower(value = 30e3, unit = 'W', name = 'Input Power')
flow_rate = onto.FlowRate(value = 40., unit = 'slpm', name = 'Flow Rate')
source.add(ipower, flow_rate, rel = onto.hasProperty)
source.add(prec, rel = onto.hasPart)
# Create plasma's composition
comp = onto.GasComposition()
arf = onto.MolarFraction(value = 1., name = 'Ar', unit = '~')
h2f = onto.MolarFraction(value = 0., name = 'H2', unit = '~')
n2f = onto.MolarFraction(value = 0., name = 'N2', unit = '~')
o2f = onto.MolarFraction(value = 0., name = 'O2', unit = '~')
comp.add(arf, h2f, n2f, o2f, rel = onto.hasPart)
# Set reactor's dimensions
reactor_geom = onto.CylindricalReactorDimensions()
diameter = onto.Diameter(value=0.250, unit='m', name = 'Diameter')
length = onto.Length(value=0.875, unit='m', name = 'Length')
inlet_diameter = onto.InletDiameter(value=13e-3, unit='m', name = 'Inlet Diameter')
reactor_geom.add(diameter, length, inlet_diameter, rel = onto.hasPart)
# Process' CUDS
reactor = onto.nanoReactor()
reactor.add(reactor_geom, rel = onto.hasPart)
# Plot utility
def plot_distributions(dists, time = 0., file_name = None, savefig = False):
def plot_dist(bins,counts,name, file_name, savefig):
plt.hist(bins,density=True,weights=counts)
if 'fractal' in name:
plt.xlabel('Fractal dimension [#]')
else:
plt.xlabel('Diamater [nm]')
plt.ylabel(name)
if savefig is True:
plt.savefig(os.path.join(os.getcwd(),"PSDs", str(file_name)+".png"))
else:
plt.show()
plt.clf()
if savefig is True:
path = os.path.join(os.getcwd(),"PSDs")
if not os.path.exists(path):
os.makedirs(path)
for dd in dists:
bins = []
counts = []
fracs = []
for bin in dd.get():
diam = bin.get(oclass=onto.ParticleDiameter)[0].value
numb = bin.get(oclass=onto.ParticleNumberDensity)[0].value
bins.append(diam)
counts.append(numb)
try:
frac = bin.get(oclass=onto.ParticleFractalDimension)[0].value
fracs.append(frac)
except:
pass
# if len(fracs) > 0:
# plot_dist(fracs,counts,'Particles fractal dimension probability density [#]')
if savefig is True:
plot_dist(bins,counts,dd.name + ' diameter probability density [#]', str(time) + "_" + file_name, savefig)
else:
plot_dist(bins,counts,dd.name + ' diameter probability density [#]', file_name, savefig)
# Parallel linked execution utility
def nano_link(del_files,source,reactor,tcond,stream,conn):
with NanoDOMESession(delete_simulation_files = del_files) as nano:
nanowrapper = onto.NanoFOAMWrapper(session=nano)
stream.name = str(nanowrapper.uid)
tcond.add(stream,rel=onto.hasProperty)
primaries = onto.NanoParticleSizeDistribution(name = 'Primaries')
particles = onto.NanoParticleSizeDistribution(name = 'Particles')
reactor.add(primaries, particles, rel = onto.hasPart)
nanowrapper.add(source, accuracy_level)
nano.run()
primaries = nanowrapper.get(source.uid).get(reactor.uid).get(primaries.uid)
particles = nanowrapper.get(source.uid).get(reactor.uid).get(particles.uid)
prims = []
for idx,bins in enumerate(primaries.get()):
data_bin = []
for data in bins.get():
data_bin.append([data.value, str(data.oclass)])
prims.append(data_bin)
parts = []
for idx,bins in enumerate(particles.get()):
data_bin = []
for data in bins.get():
data_bin.append([data.value, str(data.oclass)])
parts.append(data_bin)
conn.send([prims,parts])
conn.close()
# Run the wrappers based on input
#########################################################
if mode == 'cfd':
# Set thermodynamic conditions
tcond = onto.ThermoCond()
pressure = onto.Pressure(value=101325, unit='m^2/s^2', name = 'Pressure')
tcond.add(pressure, rel = onto.hasPart)
reactor.add(tcond, comp, rel = onto.hasPart)
source.add(reactor, rel = onto.hasProperty)
with ElenbaasSession(delete_simulation_files=True) as elen:
# Run elenbaas
elenwrapper = onto.NanoFOAMWrapper(session=elen)
elenwrapper.add(source)
elen.run()
# Get the plasma properties calculated from Elenbaas
plasma = elenwrapper.get(source.uid).get(oclass=onto.Plasma)[0]
source.add(plasma,rel=onto.hasPart)
with CFDSession(delete_simulation_files=True) as cfd:
# Run cfd
cfdwrapper = onto.NanoFOAMWrapper(session=cfd)
cfdwrapper.add(source)
cfd.run()
streams = cfdwrapper.get(source.uid).get(reactor.uid).get(tcond.uid) \
.get(oclass=onto.TemperatureStreamline)
for idx,stream in enumerate(streams,start=1):
x = []
y = []
with open(stream.path,'r') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
for row in plots:
x.append(float(row[0]))
y.append(float(row[1]))
plt.plot(x,y)
plt.xlabel('Time (s)')
plt.ylabel('Temperature (K)')
plt.title('Streamline number '+str(idx))
plt.legend()
plt.show()
elif mode == 'elenbaas':
# Set thermodynamic conditions
reactor.add(comp, rel = onto.hasPart)
source.add(reactor, rel = onto.hasProperty)
with ElenbaasSession(delete_simulation_files=True) as elen:
elenwrapper = onto.NanoFOAMWrapper(session=elen)
elenwrapper.add(source, accuracy_level)
elen.run()
plasma = elenwrapper.get(source.uid).get(oclass=onto.Plasma)[0]
for idx,prop in enumerate(plasma.get(),start=1):
if not prop.name == "Reference density":
x = []
y = []
with open(prop.path,'r') as file:
plots = csv.reader(file, delimiter=',')
for row in plots:
try:
x.append(float(row[0]))
y.append(float(row[1]))
except:
pass
file.close()
plt.plot(x,y)
if 'profile' in prop.name:
plt.xlabel('Radial distance (m)')
else:
plt.xlabel('Temperature (K)')
plt.ylabel(prop.name + ' ' + prop.unit)
plt.title(prop.name)
plt.show()
elif mode == 'nanodome':
# Set thermodynamic conditions
tcond = onto.ThermoCond()
pressure = onto.Pressure(value=101325, unit='m^2/s^2', name = 'Pressure')
temp_grad = onto.TemperatureGradient(value = -1e+7, unit = 'K/s', name = 'Temporal Temperature Gradient')
temp = onto.Temperature(value = 2000, unit = 'K', name = 'Temperature')
tcond.add(pressure, temp, temp_grad, rel = onto.hasPart)
reactor.add(tcond, comp, rel = onto.hasPart)
source.add(reactor, rel = onto.hasProperty)
with NanoDOMESession(delete_simulation_files=True) as nano:
nanowrapper = onto.NanoFOAMWrapper(session=nano)
# Results CUDS
particles = onto.NanoParticleSizeDistribution(name = 'Particles')
primaries = onto.NanoParticleSizeDistribution(name = 'Primaries')
reactor.add(particles, primaries, rel = onto.hasPart)
nanowrapper.add(source, accuracy_level)
# Run the session
##########################################################
nano.run()
# Get the results
res = nanowrapper.get(source.uid).get(reactor.uid)
particles = res.get(particles.uid)
primaries = res.get(primaries.uid)
# Histogram plotting examples for Particles and Primaries
# for Medium and High Accuracy Levels
if accuracy_level.is_a(onto.LowAccuracyLevel):
pretty_print(particles)
elif (accuracy_level.is_a(onto.MediumAccuracyLevel) or \
accuracy_level.is_a(onto.HighAccuracyLevel)):
plot_distributions([primaries,particles])
elif mode == 'linked':
# Set thermodynamic conditions
tcond = onto.ThermoCond()
pressure = onto.Pressure(value=101325, unit='m^2/s^2', name = 'Pressure')
tcond.add(pressure, rel = onto.hasPart)
reactor.add(tcond, comp, rel = onto.hasPart)
source.add(reactor, rel = onto.hasProperty)
with ElenbaasSession(delete_simulation_files=True) as elen:
# Run elenbaas
##########################################################
elenwrapper = onto.NanoFOAMWrapper(session=elen)
elenwrapper.add(source)
elen.run()
# Get the plasma properties calculated from Elenbaas
plasma = elenwrapper.get(source.uid).get(oclass=onto.Plasma)[0]
source.add(plasma,rel=onto.hasPart)
with CFDSession(delete_simulation_files=True) as cfd:
# Run cfd
##########################################################
cfdwrapper = onto.NanoFOAMWrapper(session=cfd)
cfdwrapper.add(source)
cfd.run()
# Run nanodome processes
##########################################################
# Get the TemperatureStreamline CUDS from the cfd session
streams = cfdwrapper.get(source.uid).get(reactor.uid).get(tcond.uid) \
.get(oclass=onto.TemperatureStreamline)
procs = []
for idx in range(len(streams)):
parent_conn, child_conn = multiprocessing.Pipe()
p = multiprocessing.Process(target=nano_link, \
args=(True,source,reactor,tcond,streams[idx],child_conn,))
procs.append([p,parent_conn,child_conn])
p.start()
res = []
for p,pa,ch in procs:
res.append(pa.recv())
p.join()
prims = []
parts = []
for pr,pa in res:
prims.append(pr)
parts.append(pa)
if accuracy_level.is_a(onto.LowAccuracyLevel):
diams = []
numbs = []
vols = []
for part in parts:
for data in part[0]:
if data[1] == str(onto.ParticleDiameter):
diams.append(data[0])
elif data[1] == str(onto.ParticleNumberDensity):
numbs.append(data[0])
elif data[1] == str(onto.ParticleVolumePercentage):
vols.append(data[0])
mean_diam = np.average(np.asarray(diams))
numb_dens = np.average(np.asarray(numbs))
vol_frac = np.average(np.asarray(vols))
mean_prim_size = onto.ParticleDiameter(
value=mean_diam, unit='nm', name='Mean particles diameter')
prim_numb_dens = onto.ParticleNumberDensity(
value=numb_dens, unit='#/m3', name='Mean particles number density')
prim_vol_perc = onto.ParticleVolumePercentage(
value=vol_frac, unit='m3/m3', name='Mean particles volume percentage')
result = onto.Bin(name="PSD bin")
result.add(mean_prim_size, prim_numb_dens, prim_vol_perc,
rel=onto.hasProperty)
particles = onto.NanoParticleSizeDistribution(name = 'Particles')
particles.add(result, rel=onto.hasPart)
reactor.add(particles)
else:
part_diams = []
part_numbs = []
prim_diams = []
prim_numbs = []
frac_dims = []
for prim in prims:
for data_bin in prim:
for data in data_bin:
if data[1] == str(onto.ParticleDiameter):
prim_diams.append(data[0])
elif data[1] == str(onto.ParticleNumberDensity):
prim_numbs.append(data[0])
for part in parts:
for data_bin in part:
for data in data_bin:
if data[1] == str(onto.ParticleDiameter):
part_diams.append(data[0])
elif data[1] == str(onto.ParticleNumberDensity):
part_numbs.append(data[0])
elif data[1] == str(onto.ParticleFractalDimension):
frac_dims.append(data[0])
# Create and fill bins then add them to the NanoParticleSizeDistribution CUDS
pr_counts, pr_bins = np.histogram(prim_diams,range=(np.amin(prim_diams), \
np.amax(prim_diams)),weights=prim_numbs)
pa_counts, pa_bins = np.histogram(part_diams,range=(np.amin(part_diams), \
np.amax(part_diams)),weights=part_numbs)
fd_counts, fd_bins = np.histogram(frac_dims,range=(np.amin(frac_dims), \
np.amax(frac_dims)),weights=part_numbs)
particles = onto.NanoParticleSizeDistribution(name = 'Particles')
for idx,numb in enumerate(pa_counts):
result = onto.Bin(name="PSD bin")
size_dist = onto.ParticleNumberDensity(value=numb,
unit="#/m3",name="Size distribution")
size_class = onto.ParticleDiameter(value=pa_bins[idx],
unit="nm",name="Size class")
fract_dim = onto.ParticleFractalDimension(value=fd_bins[idx],
unit="~",name="Mean fractal dimension")
result.add(size_dist, size_class, fract_dim, rel=onto.hasProperty)
particles.add(result, rel=onto.hasPart)
reactor.add(particles)
primaries = onto.NanoParticleSizeDistribution(name = 'Primaries')
for idx,numb in enumerate(pr_counts):
result = onto.Bin(name="PSD bin")
size_dist = onto.ParticleNumberDensity(value=numb,
unit="#/m3",name="Size distribution")
size_class = onto.ParticleDiameter(value=pr_bins[idx],
unit="nm",name="Size class")
result.add(size_dist, size_class, rel=onto.hasProperty)
primaries.add(result, rel=onto.hasPart)
reactor.add(primaries)
# Histogram plotting examples for Particles and Primaries
# for Medium and High Accuracy Levels
if accuracy_level.is_a(onto.LowAccuracyLevel):
pretty_print(particles)
elif (accuracy_level.is_a(onto.MediumAccuracyLevel) or \
accuracy_level.is_a(onto.HighAccuracyLevel)):
plot_distributions([primaries,particles])
elif mode == 'coupled':
if not accuracy_level.is_a(onto.MediumAccuracyLevel):
raise ValueError("This method only works with Medium accuracy level.")
cells = []
cells_number = 5
for idx in range(cells_number):
cell = onto.reactorCell(name = str(idx))
comp = onto.GasComposition()
prec_mol = onto.MolarFraction(value = 0. , name = prec_type.name , unit = '~')
arf = onto.MolarFraction(value = 1., name = 'Ar', unit = '~')
h2f = onto.MolarFraction(value = 0., name = 'H2', unit = '~')
n2f = onto.MolarFraction(value = 0., name = 'N2', unit = '~')
o2f = onto.MolarFraction(value = 0., name = 'O2', unit = '~')
comp.add(prec_mol, arf, h2f, n2f, o2f, rel = onto.hasPart)
primaries = onto.NanoParticleSizeDistribution(name = 'Primaries')
particles = onto.NanoParticleSizeDistribution(name = 'Particles')
cell.add(primaries,particles, rel=onto.hasPart)
cell.add(onto.Velocity(value = 0., name = str(idx), unit = 'm/s'))
# Set thermodynamic conditions
tcond = onto.ThermoCond()
pressure = onto.Pressure(value=101325, unit='m^2/s^2', name = 'Pressure')
temp = onto.Temperature(value = 500, unit = 'K', name = 'Temperature')
tcond.add(pressure, temp, rel = onto.hasPart)
cell.add(tcond,comp,rel=onto.hasPart)
cells.append(cell)
reactor.add(cell)
time = onto.Time(value = 0., unit="s", name="Simulation Time")
dt = onto.Time(value = 1e-8, unit="s", name="Current Timestep")
reactor.add(time,dt, rel = onto.hasPart)
source.add(reactor, rel = onto.hasProperty)
save_time = 0.
save_step = 5e-5
with CoupledReactorSession(delete_simulation_files=True) as coupled, \
NanoDOMESession(delete_simulation_files=True) as nano:
coupledwrapper = onto.NanoFOAMWrapper(session=coupled)
coupledwrapper.add(source, accuracy_level)
nanowrapper = onto.NanoFOAMWrapper(session=nano)
nanowrapper.add(source, accuracy_level)
tf = 1.1e-6
while time.value < tf:
print("Time: ",time.value)
print("")
coupledwrapper.update(source)
coupled.run()
source.update(coupledwrapper.get(source.uid).get(reactor.uid))
save_time += dt.value
if save_time > save_step:
print("Post coupled")
for cl in cells:
t_fracs = cl.get(oclass=onto.GasComposition)[0].get(oclass=onto.MolarFraction)
for mol in t_fracs:
if mol.name is prec_type.name:
print(mol.value)
print("")
nanowrapper.update(source)
nano.run()
source.update(nanowrapper.get(source.uid).get(reactor.uid))
if save_time > save_step:
print("Post nano")
for cl in cells:
t_fracs = cl.get(oclass=onto.GasComposition)[0].get(oclass=onto.MolarFraction)
for mol in t_fracs:
if mol.name is prec_type.name:
print(mol.value)
print("")
# Get the PSD
results = nanowrapper.get(source.uid).get(reactor.uid).get(oclass=onto.reactorCell)
for idx, res in enumerate(results):
for dist in res.get(oclass=onto.NanoParticleSizeDistribution):
if dist.name == "Particles":
part_uid = dist.uid
elif dist.name == "Primaries":
prim_uid = dist.uid
particles = res.get(part_uid)
primaries = res.get(prim_uid)
if (particles.get(oclass=onto.Bin)) or \
(primaries.get(oclass=onto.Bin)) :
# Histogram plotting examples for Particles and Primaries
# for Medium and High Accuracy Levels
plot_distributions([primaries,particles], time.value, "cell_" + str(res.name), savefig = True)
save_time = 0.
time.value += dt.value
print("Final time: ", time.value)
print("")
else:
raise SystemExit('No valid mode selected. Available modes are: elenbaas, cfd, nanodome, linked and coupled')