-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc_functions.py
704 lines (515 loc) · 20 KB
/
misc_functions.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
#from potential import *
###############################################################################
"""
List of various functions needed when converting a (polynomial) potential
function given in string format to a form suitable to CUDA (i.e. C).
"""
###############################################################################
def replace_all(text, dic):
"Use the replacement rules from dictionary dic to text string"
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
def rep(f,n):
"""Write a (string) function 'f**n' into form 'f*f*...*f'
i.e. write the multiplication open for CUDA."""
tmp = f
for i in xrange(n-1):
tmp = f + '*' + tmp
return tmp
def format_to_cuda(V,var_list,C_list,D_list,n):
"""Format a polynomial function V into a suitable form for CUDA.
n = the degree of V."""
tmp = V
for f in var_list:
for i in reversed(xrange(1,n+1)):
tmp = tmp.replace(f+'**'+str(float(i)),rep(f,i))
tmp = tmp.replace(f+'**'+str(i),rep(f,i))
for C in C_list:
for i in reversed(xrange(1,n+1)):
tmp = tmp.replace(C+'**'+str(float(i)),rep(f,i))
tmp = tmp.replace(C+'**'+str(i),rep(f,i))
for D in D_list:
for i in reversed(xrange(1,n+1)):
tmp = tmp.replace(D+'**'+str(float(i)),rep(f,i))
tmp = tmp.replace(D+'**'+str(i),rep(f,i))
return tmp
def new_poly(V_s, fields, n_coeffs):
"""Make a new polynomial function that has the same powers as V_s function
but with coefficients C1, C2..."""
from sympy import Poly
from sympy import diff, Symbol, var, simplify, sympify, S
from sympy.core.sympify import SympifyError
P = Poly(V_s,*fields)
d = P.as_dict()
e = {}
for key in d.iterkeys():
#print d[key], str(d[key])
for i in xrange(1, n_coeffs+1):
if 'C' + str(i) in str(d[key]):
e[key] = sympify('C'+str(i))
P2 = Poly(e,*fields)
return str(P2.as_basic())
def V_calc(V_string, n, field_list, field_i, power_list,
C_list, D_list, kernel_type, deriv_n=0,
multiplier=1.0, tmpQ = False, tmp_list = None):
"""Apply the necessary calculations (i.e. derivation and/or multiply
with a constant) to the potential function V_string:"""
from sympy import diff, Symbol, var, simplify, sympify, S, collect
from sympy.core.sympify import SympifyError
n_coeffs = len(C_list)
n2_coeffs = len(D_list)
try:
V = sympify(V_string)
m = sympify(multiplier)
except SympifyError:
print "Could not parse expression."
tmp = simplify(((m*diff(V, field_list[field_i-1], deriv_n)).expand()))
tmp = collect(tmp,field_list)
"""Replace integer coefficients with float coefficients by
first separating the different terms of the function:"""
terms = tmp.as_coeff_factors()[1]
"""The nominator and denominator of the SymPy function have to be
evaluated separately. Otherwise Sympy will use conjugate functions that
will not function in CUDA code:"""
new_terms = sympify('0')
for term in terms:
nom, denom = term.as_numer_denom()
if denom == 1:
new_terms += nom.evalf()
else:
new_terms += nom.evalf()/denom.evalf()
tmp = (format_to_cuda(str(new_terms),power_list,C_list,D_list,n)
.replace('(1 ','(1.0 '))
if kernel_type=='H3':
const_name='f_coeff'
i0 = 4
const2_name='d_coeff'
i1 = 0
elif kernel_type=='rp':
const_name='g_coeff'
i0 = 3
const2_name='d_coeff'
i1 = 0
elif kernel_type=='pd':
const_name='p_coeff'
i0 = 2
const2_name='d_coeff'
i1 = 0
"Replace C_is and D_is with the appropiate strings:"
r_cuda_V = {}
for i in xrange(n_coeffs):
r_cuda_V.update({'C'+str(i+1):const_name+'['+str(i0+i)+']'})
for i in xrange(n2_coeffs):
r_cuda_V.update({'D'+str(i+1):const2_name+'['+str(i1+i)+']'})
r_cuda_dV = {}
for i in xrange(n_coeffs):
r_cuda_dV.update({'C'+str(i+1):const_name+'['+str(i0+i)+']'})
for i in xrange(n2_coeffs):
r_cuda_dV.update({'D'+str(i+1):const2_name+'['+str(i1+i)+']'})
r_cuda_d2V = {}
for i in xrange(n_coeffs):
r_cuda_d2V.update({'C'+str(i+1):const_name+'['+str(i0+i)+']'})
for i in xrange(n2_coeffs):
r_cuda_d2V.update({'D'+str(i+1):const2_name+'['+str(i1+i)+']'})
if deriv_n == 0:
tmp2 = format_to_cuda(tmp, power_list, C_list, D_list, n)
res = replace_all(tmp2, r_cuda_V)
elif deriv_n == 1:
tmp2 = format_to_cuda(tmp, power_list, C_list, D_list, n)
res = replace_all(tmp2, r_cuda_dV)
elif deriv_n == 2:
tmp2 = format_to_cuda(tmp, power_list, C_list, D_list, n)
res = replace_all(tmp2, r_cuda_d2V)
if tmpQ:
tmp_rules = {}
for i in xrange(len(tmp_list)):
tmp_rules.update({tmp_list[i]:'tmp'+str(i+1)})
res2 = replace_all(res, tmp_rules)
else:
res2 = res
return res2
def V_calc_lin(V_string, n, field_list, field_i, power_list,
C_list, D_list, C_vals, D_vals,
deriv_n=0, multiplier=1.0):
"""Apply the necessary calculations (i.e. derivation and/or multiply with a constant)
to the potential function V_string:"""
from sympy import diff, Symbol, var, simplify, sympify, S
from sympy.core.sympify import SympifyError
n_coeffs = len(C_list)
n2_coeffs = len(D_list)
try:
V = sympify(V_string)
m = sympify(multiplier)
except SympifyError:
print "Could not parse expression."
#tmp = ((m*diff(V, field_list[field_i-1], deriv_n)).expand()).evalf()
tmp = (m*diff(V, field_list[field_i-1], deriv_n)).expand()
"Replace C_i's and D_i's with the appropiate numerical values:"
r_V_back = {}
for i in xrange(n_coeffs):
r_V_back.update({'C'+str(i+1):C_vals[i]})
for i in xrange(n2_coeffs):
r_V_back.update({'D'+str(i+1):D_vals[i]})
r_dV_back = {}
for i in xrange(n_coeffs):
r_dV_back.update({'C'+str(i+1):C_vals[i]})
for i in xrange(n2_coeffs):
r_dV_back.update({'D'+str(i+1):D_vals[i]})
r_d2V_back = {}
for i in xrange(n_coeffs):
r_d2V_back.update({'C'+str(i+1):C_vals[i]})
for i in xrange(n2_coeffs):
r_d2V_back.update({'D'+str(i+1):D_vals[i]})
f_repl = {}
for i in xrange(len(field_list)):
f_repl.update({'f'+str(i+1):'f0'+str(i+1)+'[0]'})
if deriv_n == 0:
#tmp = (tmp.subs(r_V_back)).evalf()
tmp = tmp.subs(r_V_back)
tmp2 = format_to_cuda(str(tmp), power_list, C_list, D_list, n)
res = replace_all(tmp2,f_repl)
elif deriv_n == 1:
#tmp = (tmp.subs(r_dV_back)).evalf()
tmp = tmp.subs(r_dV_back)
tmp2 = format_to_cuda(str(tmp), power_list, C_list, D_list, n)
res = replace_all(tmp2,f_repl)
elif deriv_n == 2:
#tmp = (tmp.subs(r_dV_back)).evalf()
tmp = tmp.subs(r_dV_back)
tmp2 = format_to_cuda(str(tmp), power_list, C_list, D_list, n)
res = replace_all(tmp2,f_repl)
#print 'res', res
#print 'deriv_n', deriv_n, 'res', res
return res
def dV_coeffs(lat, V_s, field_list, C_list, deriv_n):
"""Read the different numerical coefficients
from dV/df or d^2V/df^2 terms."""
from sympy import diff, Symbol, var, simplify, sympify, S
from sympy.core.sympify import SympifyError
import numpy as np
repl = {}
for field in field_list:
repl[field] = '1'
dF = [diff(sympify(V_s),f, deriv_n) for f in field_list]
"Make substitutions f_{i} = 1:"
dF1 = [df.subs(repl) for df in dF]
"""Calculate the coefficients of C_{i} terms by differentiating
with respect to them:"""
dV_mult = []
for Cf in dF1:
tmp = []
for Ci in C_list:
tmp.append(diff(Cf,Ci))
dV_mult.append(np.array(tmp, dtype=lat.prec_real))
return dV_mult
###########################################################################################
def rho_init(V, fields0, pis0):
"Calculate the energy density of the homogeneous field values:"
from sympy import diff, Symbol, var, simplify, sympify, S, evalf
from sympy.core.sympify import SympifyError
C_coeff = V.C_coeff
D_coeff = V.D_coeff
V_string = V.V
try:
V = sympify(V_string)
except SympifyError:
print "Could not parse expression."
rep_list = {}
for i in xrange(len(C_coeff)):
rep_list.update({'C'+str(i+1):C_coeff[i]})
for i in xrange(len(D_coeff)):
rep_list.update({'D'+str(i+1):D_coeff[i]})
for i in xrange(len(fields0)):
rep_list.update({'f'+str(i+1):fields0[i]})
rho0 = 0.
for i in xrange(len(fields0)):
rho0 += 0.5*pis0[i]**2.0
"Initial value of the potential function:"
V0 = V.subs(rep_list)
#print 'V0', float(V0)
rho0 += V0
return float(rho0)
def mass_eff(V, field_list, fields0, H0, deSitter=False):
"Calculate the initial effective masses of the fields:"
from sympy import diff, Symbol, var, simplify, sympify, S, evalf
from sympy.core.sympify import SympifyError
C_coeff = V.C_coeff
D_coeff = V.D_coeff
V_string = V.V
try:
d2V = [diff(sympify(V_string),f,2) for f in field_list]
except SympifyError:
print "Could not parse expression."
"Replacement list f(t)->f(t0), C_i->C_coeff[i], D_i->D_coeff[i]:"
rep_list = {}
for i in xrange(len(C_coeff)):
rep_list.update({'C'+str(i+1):C_coeff[i]})
for i in xrange(len(D_coeff)):
rep_list.update({'D'+str(i+1):D_coeff[i]})
for i in xrange(len(fields0)):
rep_list.update({'f'+str(i+1):fields0[i]})
"If a deSitter space include also a''/a term:"
if deSitter:
C = 1
else:
C = 0
"Initial value of the potential function:"
m2eff = [(float(x.subs(rep_list)) - C*9./4.*H0**2.0 ) for x in d2V]
for mass in m2eff:
if mass <0:
import sys
print 'Mass squared negative!'
sys.exit()
return m2eff
def V_func(lat, V):
"Calculate V:"
field_list = lat.field_list
from sympy import diff, Symbol, var, simplify, sympify, S, evalf
from sympy.utilities import lambdify
from sympy.core.sympify import SympifyError
C_coeff = V.C_coeff
D_coeff = V.D_coeff
V_string = V.V
try:
V = sympify(V_string)
except SympifyError:
print "Could not parse expression."
"Replacement list C_i->C_coeff[i], D_i->D_coeff[i]:"
rep_list = {}
for i in xrange(len(C_coeff)):
rep_list.update({'C'+str(i+1):C_coeff[i]})
for i in xrange(len(D_coeff)):
rep_list.update({'D'+str(i+1):D_coeff[i]})
V_func = lambdify(field_list,V.subs(rep_list))
return V_func
def dV_func(lat, V, field_var):
"Calculate dV/df:"
field_list = lat.field_list
from sympy import diff, Symbol, var, simplify, sympify, S, evalf
from sympy.utilities import lambdify
from sympy.core.sympify import SympifyError
C_coeff = V.C_coeff
D_coeff = V.D_coeff
V_string = V.V
try:
dV = diff(sympify(V_string),field_var)
except SympifyError:
print "Could not parse expression."
"Replacement list C_i->C_coeff[i], D_i->D_coeff[i]:"
rep_list = {}
for i in xrange(len(C_coeff)):
rep_list.update({'C'+str(i+1):C_coeff[i]})
for i in xrange(len(D_coeff)):
rep_list.update({'D'+str(i+1):D_coeff[i]})
dV_func = lambdify(field_list,dV.subs(rep_list))
return dV_func
def d2V_func(lat, V, field_var):
"Calculate the masses of the fields in terms of the field values:"
field_list = lat.field_list
from sympy import diff, Symbol, var, simplify, sympify, S, evalf
from sympy.utilities import lambdify
from sympy.core.sympify import SympifyError
C_coeff = V.C_coeff
D_coeff = V.D_coeff
V_string = V.V
try:
d2V = diff(sympify(V_string),field_var,2)
except SympifyError:
print "Could not parse expression."
"Replacement list C_i->C_coeff[i], D_i->D_coeff[i]:"
rep_list = {}
for i in xrange(len(C_coeff)):
rep_list.update({'C'+str(i+1):C_coeff[i]})
for i in xrange(len(D_coeff)):
rep_list.update({'D'+str(i+1):D_coeff[i]})
d2V_func = lambdify(field_list,d2V.subs(rep_list))
return d2V_func
###############################################################################
# Misc functions used when writing data to file
###############################################################################
def sort_func(string):
"Sort file names with this function:"
start, i_value, end = string.split('.')
return int(i_value)
def make_dir(model, lat, V, sim, path = None):
"Create a new folder for the simulation data:"
import os
import datetime
if path == None:
path = os.getcwd()
repl = {':':'_','.':'_','-':'_','T':'_T_'}
time_now = datetime.datetime.now().isoformat()
time_form = replace_all(time_now, repl)
time_text = time_now.replace('T',' ')
if model.superfolderQ:
data_path = path + '/data/' + model.superfolder + '/' + time_form
else:
data_path = path + '/data/' + time_form
os.makedirs(data_path)
f = open(data_path + '/info.txt','w')
f.write(create_info_file(model, lat, V, sim, time_text))
f.close()
return data_path
def make_subdir(path, method= None, sim_number=None):
"Create a new subfolder for the simulation data:"
import os
if method == 'homog':
data_path = path + '/homog/'
os.makedirs(data_path)
else:
data_path = path + '/sim_' + str(sim_number) + '/'
os.makedirs(data_path)
return data_path
def create_info_file(model, lat, V, sim, time):
"Create an info file of the simulation:"
fields0 = [field.f0_list[0] for field in sim.fields]
pis0 = [field.pi0_list[0] for field in sim.fields]
text = 'PyCOOL Simulation info file\n\n'
text += 'Simulation start time: ' + time + '\n\n'
text += 'Simulation model: ' + V.model_name + '\n\n'
text += 'mass m: ' + str(lat.m) + '\n'
text += 'Reduced Planck mass: ' + str(lat.mpl) + '\n\n'
text += 'Simulation initial time: ' + str(model.t_in*lat.m) + '/m \n'
text += 'Simulation final time: ' + str(model.t_fin*lat.m) + '/m \n'
text += 'Simulation initial scale parameter: ' + str(model.a_in) + '\n'
text += ('Simulation initial radiation density: ' +
str(sim.rho_r0/lat.m**2) + '*m**2 \n')
text += ('Simulation initial matter density: ' +
str(sim.rho_m0/lat.m**2) + '*m**2 \n\n')
text += 'Is the spacetime deSitter? : ' + str(sim.deSitter) + '\n\n'
text += 'Is debugging mode on? : ' + str(lat.test) + '\n'
text += 'Discretization method : ' + str(lat.discQ) + '\n'
text += 'Are gravitational waves solved? : ' + str(lat.gws) + '\n\n'
text += 'Lattice size (n): ' + str(lat.n) + '\n'
text += 'Lattice side length: ' + str(lat.m*lat.L) + '/m \n'
text += 'Lattice spacing dx: ' + str(lat.m*lat.dx) + '/m \n'
text += 'Time step d\eta: ' + str(lat.m*lat.dtau) + '/m \n\n'
text += ('Initial field values: ' +
', '.join([str(x) for x in fields0])
+ '\n')
text += ('Initial field derivative values: ' +
', '.join([str(x) for x in pis0])
+ '\n\n')
if V.v_l==None:
V_term = 'None'
else:
V_term = ', '.join(V.v_l)
if V.v_int==None:
V_int_term = 'None'
else:
V_int_term = ', '.join(V.v_int)
text += ('Potential functions of the fields: ' + V_term
+ '\n')
text += ('Interaction terms: ' + V_int_term
+ '\n')
text += ('Numerical coefficients C_i: ' +
', '.join([str(x) for x in V.C_coeff])
+ '\n')
text += ('Numerical coefficients D_i: ' +
', '.join([str(x) for x in V.D_coeff])
+ '\n\n')
return text
def sim_time(time_sim, per_stp, steps, data_path):
"Write simulation time into info.txt file:"
import time, datetime
time_str = str(datetime.timedelta(seconds=time_sim))
per_stp_str = str(per_stp)
sim_time = ('\nSimulation finished. Time used: ' + time_str + ' Per step: ' +
per_stp_str + '\n')
steps = ('Number of steps taken: ' + str(steps) + '\n')
print sim_time
f = open(data_path + '/info.txt','a')
f.write(sim_time)
f.write(steps)
f.close()
def data_folders(path=None):
"Give a list of possible data folders:"
import os
if path == None:
path = os.getcwd() + '/data'
folders_l = []
for dirname, dirnames, filenames in os.walk(path):
tmp = []
for subdirname in dirnames:
#folders_l.append(os.path.join(dirname, subdirname))
tmp.append(os.path.join(dirname, subdirname))
folders_l.append(tmp)
return folders_l
#for item in os.listdir(data_path):
# if os.path.isdir(os.path.join(data_path, item)):
# print item
def sub_folders(path=None):
"Give a list of possible data folders:"
import os
if path == None:
path = os.getcwd() + '/data'
folders_l = []
for dirname, dirnames, filenames in os.walk(path):
for subdirname in dirnames:
folders_l.append(os.path.join(dirname, subdirname))
return folders_l
def files_in_folder(path=None, filetype='silo', sort = True):
"Give a list of sorted filenames in a data folder:"
import os
if path == None:
path = os.getcwd() + '/data'
#files = [path + filename for filename in os.listdir(path)]
files = []
for filename in os.listdir(path):
if filetype in filename:
files.append(path + '/' + filename)
if sort:
files.sort(key=sort_func)
return files
def write_csv(lat, data_path, mode = 'non-lin', source = 'silo'):
"This reads curves from a silo file and writes the data to a csv file:"
import os
import pyvisfile.silo as silo
import csv
if source == 'silo':
files = files_in_folder(path=data_path, filetype='silo')
os.makedirs(data_path + '/csv')
print 'Writing ' + str(len(files)) + ' cvs files.'
i = 0
for x in files:
f = silo.SiloFile(x, create=False, mode=silo.DB_READ)
curves = f.get_toc().curve_names
if lat.spect and mode == 'non-lin':
k_val = f.get_curve('field1'+'_S_k').x
if lat.gws and mode == 'non-lin':
k_val2 = f.get_curve('gw_spectrum').x
if mode == 'non-lin':
t_val = f.get_curve('a').x
elif mode == 'homog':
t_val = f.get_curve('a_hom').x
f_name = data_path + '/csv/' + x.split('.')[-2] + '.csv'
csv_file = open(f_name,'w')
writer = csv.writer(csv_file)
if lat.gws:
writer.writerow(['t_val','k_val','k_val2'] + curves)
else:
writer.writerow(['t_val','k_val'] + curves)
writer.writerow(t_val)
if lat.spect and mode == 'non-lin':
writer.writerow(k_val)
if lat.gws and mode == 'non-lin':
writer.writerow(k_val2)
for curve in curves:
writer.writerow(f.get_curve(curve).y)
csv_file.close()
f.close()
i += 1
###############################################################################
# Misc CUDA functions
###############################################################################
def show_GPU_mem():
import pycuda.driver as cuda
mem_free = float(cuda.mem_get_info()[0])
mem_free_per = mem_free/float(cuda.mem_get_info()[1])
mem_used = float(cuda.mem_get_info()[1] - cuda.mem_get_info()[0])
mem_used_per = mem_used/float(cuda.mem_get_info()[1])
print '\nGPU memory available {0} Mbytes, {1} % of total \n'.format(
mem_free/1024**2, 100*mem_free_per)
print 'GPU memory used {0} Mbytes, {1} % of total \n'.format(
mem_used/1024**2, 100*mem_used_per)