forked from kammerje/P-pop-second-version
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemGenerator.py
More file actions
executable file
·501 lines (434 loc) · 16.6 KB
/
SystemGenerator.py
File metadata and controls
executable file
·501 lines (434 loc) · 16.6 KB
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
"""
# =============================================================================
# P-POP
# A Monte-Carlo tool to simulate exoplanet populations
# =============================================================================
"""
# =============================================================================
# IMPORTS
# =============================================================================
import os
import sys
import time
import Star, System
# =============================================================================
# SYSTEMGENERATOR
# =============================================================================
class SystemGenerator():
def __init__(self,
StarCatalog,
StypeToModel,
ScalingModel,
MassModel,
EccentricityModel,
StabilityModel,
OrbitModel,
AlbedoModel,
ExozodiModel,
Stypes,
Dist_range, # pc
Dec_range, # deg
Scenario,
SummaryPlots,
Ntest,
FigDir,
block):
"""
Parameters
----------
StarCatalog: module
Module of type StarCatalog.
StypeToModel: dict
Dictionary mapping the spectral types to planet distribution
modules.
ScalingModel: module
Module of type ScalingModel.
MassModel: module
Module of type MassModel.
EccentricityModel: module
Module of type EccentricityModel.
StabilityModel: None, module
Module of type StabilityModel.
OrbitModel: module
Module of type OrbitModel.
AlbedoModel: module
Module of type AlbedoModel.
ExozodiModel: module
Module of type ExozodiModel.
Stypes: list
Spectral types to be included.
Dist_range: list
Distance range (pc) to be included.
Dec_range: list
Declination range (deg) to be included.
Scenario: 'baseline', 'pessimistic', 'optimistic'
Scenario for planet occurrence rates.
SummaryPlots: bool
If True, makes summary plots after importing a module.
Ntest: int
Number of test draws for summary plot.
FigDir: str, None
Directory to which summary plots are saved.
block: bool
If True, blocks plots when showing.
"""
# Create directory to which summary plots are saved
if (FigDir is not None and not os.path.exists(FigDir)):
os.makedirs(FigDir)
# Flag indicating whether the output planet population table has
# already been created.
self.TableFlag = False
# Get star catalog.
self.StarCatalog = self.getStarCatalog(StarCatalog,
Stypes,
Dist_range,
Dec_range)
if (SummaryPlots == True):
self.StarCatalog.SummaryPlot(FigDir=FigDir,
block=block)
# Get planet distributions.
self.PlanetDistributions = self.getPlanetDistributions(StypeToModel,
Scenario)
if (SummaryPlots == True):
temp = sorted(set(val for val in self.PlanetDistributions.values()))
for val in temp:
val.SummaryPlot(Ntest=Ntest,
FigDir=FigDir,
block=block)
# Get scaling model.
self.ScalingModel = self.getScalingModel(ScalingModel)
if (SummaryPlots == True and self.ScalingModel is not None):
self.ScalingModel.SummaryPlot(Ntest=Ntest,
FigDir=FigDir,
block=block)
# Get mass model.
self.MassModel = self.getMassModel(MassModel)
if (SummaryPlots == True):
self.MassModel.SummaryPlot(Ntest=Ntest,
FigDir=FigDir,
block=block)
# Get eccentricity model.
self.EccentricityModel = self.getEccentricityModel(EccentricityModel)
if (SummaryPlots == True):
self.EccentricityModel.SummaryPlot(Ntest=Ntest,
FigDir=FigDir,
block=block)
# Get stability model.
self.StabilityModel = self.getStabilityModel(StabilityModel)
if (SummaryPlots == True and self.StabilityModel is not None):
try:
self.StabilityModel.SummaryPlot(self.PlanetDistributions['G'],
self.MassModel,
self.EccentricityModel,
Ntest=Ntest,
FigDir=FigDir,
block=block)
except:
self.StabilityModel.SummaryPlot(self.PlanetDistributions[sorted(self.PlanetDistributions.keys())[0]],
self.MassModel,
self.EccentricityModel,
Ntest=Ntest,
FigDir=FigDir,
block=block)
# Get orbit model.
self.OrbitModel = self.getOrbitModel(OrbitModel)
if (SummaryPlots == True):
self.OrbitModel.SummaryPlot(self.EccentricityModel,
Ntest=Ntest,
FigDir=FigDir,
block=block)
# Get albedo model.
self.AlbedoModel = self.getAlbedoModel(AlbedoModel)
if (SummaryPlots == True):
self.AlbedoModel.SummaryPlot(Ntest=Ntest,
FigDir=FigDir,
block=block)
# Get exozodiacal dust model.
self.ExozodiModel = self.getExozodiModel(ExozodiModel,
Scenario)
if (SummaryPlots == True):
self.ExozodiModel.SummaryPlot(Ntest=Ntest,
FigDir=FigDir,
block=block)
pass
def getStarCatalog(self,
StarCatalog,
Stypes,
Dist_range, # pc
Dec_range): # deg
return StarCatalog.StarCatalog(Stypes,
Dist_range,
Dec_range)
def getPlanetDistributions(self,
StypeToModel,
Scenario):
"""
Parameters
----------
StypeToModel: dict
Dictionary mapping the spectral types to planet distribution
modules.
Scenario: 'baseline', 'pessimistic', 'optimistic'
Scenario for planet occurrence rates.
Returns
-------
PlanetDistributions: dict
Dictionary mapping the spectral types to planet distribution
instances.
"""
# Get one planet distribution instance for each spectral type. If
# multiple spectral types have the same planet distribution don't
# create a new instance for each of them, but simply map it to the
# already existing one.
PlanetDistributions = {}
for i, key_i in enumerate(StypeToModel):
if (i == 0):
PlanetDistributions[key_i] = StypeToModel[key_i].PlanetDistribution(Scenario)
else:
isnew = True
for j, key_j in enumerate(StypeToModel):
if (j < i and StypeToModel[key_i] == StypeToModel[key_j]):
PlanetDistributions[key_i] = PlanetDistributions[key_j]
isnew = False
if (isnew == True):
PlanetDistributions[key_i] = StypeToModel[key_i].PlanetDistribution(Scenario)
return PlanetDistributions
def getScalingModel(self,
ScalingModel):
"""
Parameters
----------
ScalingModel: None, module
Module of type ScalingModel.
Returns
-------
ScalingModel: None, instance
Instance of class ScalingModel.
"""
if (ScalingModel is None):
return None
else:
return ScalingModel.ScalingModel()
def getMassModel(self,
MassModel):
"""
Parameters
----------
MassModel: module
Module of type MassModel.
Returns
-------
MassModel: instance
Instance of class MassModel.
"""
return MassModel.MassModel()
def getEccentricityModel(self,
EccentricityModel):
"""
Parameters
----------
EccentricityModel: module
Module of type EccentricityModel.
Returns
-------
EccentricityModel: instance
Instance of class EccentricityModel.
"""
return EccentricityModel.EccentricityModel()
def getStabilityModel(self,
StabilityModel):
"""
Parameters
----------
StabilityModel: None, module
Module of type StabilityModel.
Returns
-------
StabilityModel: None, instance
Instance of class StabilityModel.
"""
if (StabilityModel is None):
return None
else:
return StabilityModel.StabilityModel()
def getOrbitModel(self,
OrbitModel):
"""
Parameters
----------
OrbitModel: module
Module of type OrbitModel.
Returns
-------
OrbitModel: instance
Instance of class OrbitModel.
"""
return OrbitModel.OrbitModel()
def getAlbedoModel(self,
AlbedoModel):
"""
Parameters
----------
AlbedoModel: module
Module of type AlbedoModel.
Returns
-------
AlbedoModel: instance
Instance of class AlbedoModel.
"""
return AlbedoModel.AlbedoModel()
def getExozodiModel(self,
ExozodiModel,
Scenario):
"""
Parameters
----------
ExozodiModel: module
Module of type ExozodiModel.
Scenario: 'baseline', 'pessimistic', 'optimistic'
Scenario for exozodi level.
Returns
-------
ExozodiModel: instance
Instance of class ExozodiModel.
"""
return ExozodiModel.ExozodiModel(Scenario)
def SimulateUniverses(self,
Name,
Nuniverses=1):
"""
Parameters
----------
Name: str
Name of the output planet table.
Nuniverses: int
Number of universes to be simulated.
"""
# Print.
print('Simulating %.0f universe(s)' % Nuniverses)
t0 = time.time()
# Go through the star catalog.
Nstars = len(self.StarCatalog.SC)
for i in range(Nstars):
# Get star.
self.Star = self.getStar(i)
# Apply scaling for the planet occurrence rates.
if (self.ScalingModel is None):
Scale = 1.
else:
Scale = self.ScalingModel.getScale(self.Star)
# Go through the number of universes to be simulated.
for j in range(Nuniverses):
# Get system.
self.System = self.getSystem(i,
j,
Scale)
# If there is a planet distribution for the star's spectral
# type (i.e. if the system is not None), create a new
# planet population table (if it hasn't already been created)
# and write the simulated planets to it.
if (self.System is not None):
if (self.TableFlag == False):
self.System.write(Name)
self.TableFlag = True
else:
self.System.append(Name)
sys.stdout.write('\r--> Star %.0f of %.0f, scaling = %.1f' % ((i+1), Nstars, Scale))
sys.stdout.flush()
print('')
t1 = time.time()
# Print.
if (self.StabilityModel is None):
print('--> Finished after %.0f s' % (t1-t0))
else:
print('--> Finished after %.0f s, drawing stable system failed %.0f times' % (t1-t0, self.StabilityModel.Nfails))
pass
def getStar(self,
index):
"""
Parameters
----------
index: int
Index of requested star in the star catalog.
Returns
-------
Star: instance
Instance of class Star.
"""
try:
tempVmag = self.StarCatalog.SC[index]['Vmag']
except:
tempVmag = None
try:
tempJmag = self.StarCatalog.SC[index]['Jmag']
except:
tempJmag = None
try:
tempHmag = self.StarCatalog.SC[index]['Hmag']
except:
tempHmag = None
try:
tempWDSsep = self.StarCatalog.SC[index]['WDSsep']
except:
tempWDSsep = None
try:
tempWDSdmag = self.StarCatalog.SC[index]['WDSdmag']
except:
tempWDSdmag = None
try:
templGal = self.StarCatalog.SC[index]['lGal']
except:
templGal = None
try:
tempbGal = self.StarCatalog.SC[index]['bGal']
except:
tempbGal = None
return Star.Star(self.StarCatalog.SC[index]['Name'],
self.StarCatalog.SC[index]['Dist'],
self.StarCatalog.SC[index]['Stype'],
self.StarCatalog.SC[index]['Rad'],
self.StarCatalog.SC[index]['Teff'],
self.StarCatalog.SC[index]['Mass'],
self.StarCatalog.SC[index]['RA'],
self.StarCatalog.SC[index]['Dec'],
tempVmag,
tempJmag,
tempHmag,
tempWDSsep,
tempWDSdmag,
templGal,
tempbGal)
def getSystem(self,
Nstar,
Nuniverse,
Scale):
"""
Parameters
----------
Nstar: int
Number of the host star to which the system belongs to.
Nuniverse: int
Number of the universe to which the system belongs to.
Scale: float
Scaling factor for the planet occurrence rates.
Returns
-------
System: None, instance
Instance of class System.
"""
# If there is no planet distribution for the star's spectral type,
# return None.
if (self.Star.Stype in self.PlanetDistributions.keys()):
return System.System(self.Star,
self.PlanetDistributions[self.Star.Stype],
self.MassModel,
self.EccentricityModel,
self.OrbitModel,
self.AlbedoModel,
self.ExozodiModel,
self.StabilityModel,
Nstar,
Nuniverse,
Scale)
else:
return None