-
Notifications
You must be signed in to change notification settings - Fork 3
/
ICgen_settings.py
501 lines (369 loc) · 13.8 KB
/
ICgen_settings.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
# -*- coding: utf-8 -*-
"""
Defines the settings class for ICgen
USAGE:
Load settings from file:
import ICgen_settings
settings = ICgen_settings.settings('filename')
Load settings from defaults (Defined in ICgen_settings.py):
import ICgen_settings
settings = ICgen_settings.settings()
AFTER LOADING:
Access settings:
# Return number of particles:
settings.pos_gen.nParticles
# Etc...
Echo settings:
# Echo ALL settings
settings()
# Echo filenames
settings.filenames()
# Etc...
READ/WRITE
Write (save) settings to disk:
settings.write('filename')
Load (read) settings from disk:
settings.load('filename')
"""
__version__ = "$Revision: 3 $"
# $Source$
__iversion__ = int(filter(str.isdigit,__version__))
from ICgen_utils import checkversion
import pynbody
SimArray = pynbody.array.SimArray
import cPickle as pickle
import numpy as np
""" **************************************************
SETTINGS
ALL of these variable names MUST be set!
************************************************** """
class filenames:
"""
Filenames
To echo settings:
filenames()
To access settings:
filenames.settingname
"""
def __init__(self):
# Initial conditions filename
self.IC_file_name = 'IC.p'
# Filename to save tipsy snapshot to
self.snapshotName = 'snapshot.std'
# Filename to save ChaNGa .param file to. If None, no file saved
# To edit settings, you can change default.param or the output .param file
self.paramName = 'snapshot.param'
# Default .director filename
self.directorName = 'snapshot.director'
def __call__(self):
print '--------------------'
print 'Filenames:'
print ''
for key,val in self.__dict__.iteritems():
if pynbody.units.has_units(val):
print '{0} : {1} {2}'.format(key,val,val.units)
else:
print '{0} : {1}'.format(key,val)
class physical:
"""
Defines default physical parameters
To echo settings:
physical()
To access settings:
physical.settingname
"""
def __init__(self, kind=None, binsys=None, starMode = 'single'):
#editted by dflemin3 07/10/2015
# Molecular mass of the gass. If m = None, Assumed to be H2, m = 2.00132 m_p
self.m = SimArray(2.00132,'m_p')
# Mass of the star (or total mass of binary system). If M = None, Assumed to be 0.33 Msol
self.M = SimArray(0.33 , 'Msol')
# CONSTANTS FOR CALCULATING TEMPERATURE. T(r) = T0(r/r0)^Tpower.
# See calc_temp.py. If None, defaults to settings in calc_temp.py
self.T0 = SimArray(332.406,'K')
self.r0 = SimArray(0.5,'au')
self.Tpower = -0.59
self.Tmin = SimArray(0, 'K') # Minimum temperature cut-off
self.Tmax = SimArray(np.inf, 'K')
# Equation of state parameters
self.eos = 'isothermal' # isothermal or adiabatic
self.gamma = 1.4
# Binary parameters
self.starMode = starMode #single star or binary?
#Binary Orbital Parameters...store in Binary class from binary.py
self.binsys = binsys
if kind is None:
kind = 'powerlaw'
self.kind = kind # Type of temperature profile. See calc_temp.py
def __call__(self):
print '--------------------'
print 'General physical parameters:'
print ''
for key,val in self.__dict__.iteritems():
if pynbody.units.has_units(val):
print '{0} : {1} {2}'.format(key,val,val.units)
else:
print '{0} : {1}'.format(key,val)
class sigma:
"""
Settings for generating a surface density profile
To echo:
sigma()
To access settings:
sigma.settingname
"""
def __init__(self, kind='powerlaw'):
# Kind of surface density profile to use. Options:
# 'powerlaw'
# 'MQWS'
# Setting self.kind automatically initializes the default settings
# for that kind
self.kind = kind
def _set_defaults(self):
# Set default attributes:
# First delete all attributes but kind
kind = self.kind
for key in self.__dict__.keys():
if key != 'kind':
self.__dict__.pop(key, None)
# Now set attributes
if kind == 'powerlaw':
self.Rd = SimArray(1.0,'au')
self.rin = 0.5
self.rmax = 2.3
self.cutlength = 0.3
self.Qmin = 1.5
self.n_points = 1000
self.power = -1 #Power on powerlaw of form sigma ~ r^(power)
if (kind == 'mqws') | (kind == 'MQWS'):
self.rin = 4.0
self.rout = 20.0
self.rmax = None
self.power = -1
self.m_disk = SimArray(0.1, 'Msol')
self.n_points = 1000
self.Qmin = 1.5
if kind == 'viscous':
self.Rd = SimArray(1.0, 'au')
self.rin = 0.1
self.rmax = 2.0
self.power = -1
self.m_disk = SimArray(0.1, 'Msol')
self.n_points = 1500
self.gamma = 0.9
# innercut and outercut determine where to apply a hard cut to the
# surface density (if anywhere). for example, to set sigma=0 for
# R > 2 au, do: outercut = SimArray(2,'au')
self.innercut = None
self.outercut = None
def __setattr__(self, attr, value):
"""
Override the default __setattr__ so that changing self.kind causes
the defaults to be set for that kind
"""
self.__dict__[attr] = value
if attr == 'kind':
self._set_defaults()
def __call__(self):
print '--------------------'
print 'Sigma profile parameters:'
print ''
for key,val in self.__dict__.iteritems():
if pynbody.units.has_units(val):
print '{0} : {1} {2}'.format(key,val,val.units)
else:
print '{0} : {1}'.format(key,val)
class rho_calc:
"""
Settings for calculating rho(z,r)
To echo settings:
rho_calc()
To access settings:
rho_calc.settingname
"""
def __init__(self):
# The number of radial data points to calculate rho(z,r) at
self.nr = 1000
# The number of vertical points to calculate rho(z,r) at
self.nz = 1000
# The maximum z (assumed to be au) to calculate rho(z,r) at. Outside zmax,
# rho = 0. If None, zmax is twice the scale height at rmax
self.zmax = None
# Numerical parameter. During the calculation of rho(z) for a given r. See
# the doc-string for calc_rho.py
self.rho_tol = 1.001
def __call__(self):
print '--------------------'
print 'Rho calculation settings:'
print ''
for key,val in self.__dict__.iteritems():
if pynbody.units.has_units(val):
print '{0} : {1} {2}'.format(key,val,val.units)
else:
print '{0} : {1}'.format(key,val)
class pos_gen:
"""
Settings for generating random positions [STEP 2]
To echo settings:
pos_gen()
To access settings:
pos_gen.settingname
"""
def __init__(self):
# Number of random positions to generate:
self.nParticles = 40411
# The method for generating positions
self.method = 'grid'
def __call__(self):
print '--------------------'
print 'Position generator settings:'
print ''
for key,val in self.__dict__.iteritems():
if pynbody.units.has_units(val):
print '{0} : {1} {2}'.format(key,val,val.units)
else:
print '{0} : {1}'.format(key,val)
class snapshot:
"""
Settings for generating tipsy files (includes particle mass, temp, and vel.)
[STEP 3]
"""
def __init__(self, nParticles=0):
# Factor by which to scale the disc mass before time evolving it up to
# a final mass of Mdisc (see above). Should be between 0 and 1
# Default: 1.0 (no scaling done). Final particle masses are scaled
# by this factor
self.mScale = 1.0
# Other defaults that shouldn't need to be changed
self.metals = 1.0
def __call__(self):
print '--------------------'
print 'Tipsy snapshot generator settings:'
print ''
for key,val in self.__dict__.iteritems():
if pynbody.units.has_units(val):
print '{0} : {1} {2}'.format(key,val,val.units)
else:
print '{0} : {1}'.format(key,val)
class changa_run:
"""
Settings for running ChaNGa (needed when calculating velocities, eps, so on)
"""
def __init__(self, preset = 'local'):
# Run configuration to use. See ICgen_utils.changa_command for options
self.preset = preset
# Additional arguments for all ChaNGa calls
self.changa_args = ''
# Additional arguments for all runner (mpirun, charmrun, ...) calls
self.runner_args = ''
# Display ChaNGa output
self.verbose = True
# Save to log file
self.logfile_name = None
def __call__(self):
print '--------------------'
print 'ChaNGa run options:'
print ''
for key,val in self.__dict__.iteritems():
if pynbody.units.has_units(val):
print '{0} : {1} {2}'.format(key,val,val.units)
else:
print '{0} : {1}'.format(key,val)
class settings:
"""
settings for ICgen
USAGE:
Load settings from file:
import ICgen_settings
settings = ICgen_settings.settings('filename')
Load settings from defaults (Defined in ICgen_settings.py):
import ICgen_settings
settings = ICgen_settings.settings()
Load settings for a given surface density profile
import ICgen_settings
settings = ICgen_settings.settings('powerlaw')
or try:
settings = ICgen_settings.settings('MQWS')
AFTER LOADING:
Access settings:
# Return sigmaFileName:
settings.filenames.sigmaFileName
# Return number of particles:
settings.pos_gen.nParticles
# Etc...
Echo settings:
# Echo ALL settings
settings()
# Echo filenames
settings.filenames()
# Etc...
READ/WRITE
Write (save) settings to disk:
settings.write('filename')
Load (read) settings from disk:
settings.load('filename')
"""
def __init__(self, settings_filename=None, kind=None):
self.__version__ = __iversion__
if settings_filename is None:
# Load the defaults
self.filenames = filenames()
self.sigma = sigma(kind)
self.rho_calc = rho_calc()
self.pos_gen = pos_gen()
self.snapshot = snapshot(self.pos_gen.nParticles)
self.physical = physical(kind)
self.changa_run = changa_run()
else:
self.load(settings_filename)
def __call__(self):
self.filenames()
print ''
# Check if sigma is a setting (needed for compatibility)
if hasattr(self,'sigma'):
self.sigma()
print ''
self.rho_calc()
print ''
self.pos_gen()
print ''
self.snapshot()
print ''
self.physical()
print ''
self.changa_run()
def save(self,settings_filename = None):
"""
Save settings to settings_filename. If settings_filename is None
(or is not set), settings_filename will be taken as the filename of
the most recent save/load
"""
if settings_filename is None:
# Try filename stored in settings object (self)
if not hasattr(self,'settings_filename'):
raise RuntimeError,'No settings_filename defined. Cannot save'
else:
settings_filename = self.settings_filename
f = open(settings_filename,'wb')
pickle.dump(self.__dict__,f,2)
f.close()
print 'Settings saved to {0}'.format(settings_filename)
# Update the settings_filename
self.settings_filename = settings_filename
def load(self,settings_filename):
"""
Load settings from settings_filename.
"""
f = open(settings_filename,'rb')
tmp_dict = pickle.load(f)
f.close()
self.__dict__.update(tmp_dict)
self.settings_filename = settings_filename
version = checkversion(self)
# Version checking
if version < 3:
# Might be missing EOS and binary parameters. Get those setup
self.physical = physical()
self.physical.__dict__.update(tmp_dict['physical'].__dict__)
# update the version
self.__version__ = 3