-
Notifications
You must be signed in to change notification settings - Fork 2
/
diagnostics.py
230 lines (210 loc) · 7.2 KB
/
diagnostics.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Diagnostic routines
"""
import logging
import argparse
import numpy
from amuse.datamodel import ParticlesSuperset, Particles
from amuse.units import units, nbody_system
from amuse.io import read_set_from_file, write_set_to_file
from amuse.io.base import IoException
from amuse.community.hop.interface import Hop
# from amuse.ext.LagrangianRadii import LagrangianRadii
def identify_subgroups(
unit_converter,
particles,
saddle_density_threshold=None,
outer_density_threshold=None,
peak_density_threshold="auto",
logger=None,
):
"Identify groups of particles by particle densities"
# print(peak_density_threshold)
# exit()
logger = logger or logging.getLogger(__name__)
hop = Hop(unit_converter)
hop.particles.add_particles(particles)
logger.info("particles added to Hop")
hop.calculate_densities()
logger.info("densities calculated")
try:
mean_density = hop.particles.density.mean()
except:
print("error")
# if peak_density_threshold == "auto":
# peak_density_threshold = mean_density
hop.parameters.peak_density_threshold = peak_density_threshold
logger.info(
"peak density threshold set to %s", peak_density_threshold,
)
print(peak_density_threshold/mean_density)
saddle_density_threshold = (
0.9*peak_density_threshold
if saddle_density_threshold is None
else saddle_density_threshold
)
hop.parameters.saddle_density_threshold = saddle_density_threshold
logger.info(
"saddle density threshold set to %s", saddle_density_threshold,
)
outer_density_threshold = (
0.01*peak_density_threshold
if outer_density_threshold is None
else outer_density_threshold
)
hop.parameters.outer_density_threshold = outer_density_threshold
logger.info(
"outer density threshold set to %s", saddle_density_threshold,
)
hop.do_hop()
logger.info("doing hop")
result = [x.get_intersecting_subset_in(particles) for x in hop.groups()]
hop.stop()
print("hop done")
logger.info("stopping hop")
return result
def run_diagnostics(
model,
logger=None,
length_unit=units.pc,
mass_unit=units.MSun,
time_unit=units.Myr,
):
"""
Run diagnostics on model
"""
logger = logger or logging.getLogger(__name__)
stars = model.star_particles
sinks = model.sink_particles
gas = model.gas_particles
converter = model.star_converter
if not sinks.is_empty():
non_collisional_bodies = Particles()
non_collisional_bodies.add_particles(stars)
non_collisional_bodies.add_particles(sinks)
else:
non_collisional_bodies = stars
groups = identify_subgroups(
converter,
non_collisional_bodies,
peak_density_threshold=1e-16 | units.g * units.cm**-3,
)
n_groups = len(groups)
if hasattr(stars, 'group_id'):
group_id_offset = 1 + max(stars.group_id)
else:
group_id_offset = 1 # a group id of 0 would mean "no group found"
logger.info("Found %i groups", n_groups)
for i, group in enumerate(groups):
group_id = i + group_id_offset
if hasattr(group, 'group_id'):
group.previous_group_id = group.group_id
else:
group.previous_group_id = 0
group.group_id = group_id
stars_in_group = len(group)
if (stars_in_group > 100):
mass_in_group = group.total_mass().in_(mass_unit)
mass_fraction = [0.01, 0.02, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 1.0]
radii, new_mass_fraction = group.LagrangianRadii(
unit_converter=converter, mf=mass_fraction, cm=group.center_of_mass(),
)
assert(new_mass_fraction == mass_fraction)
radii = radii.value_in(length_unit)
x, y, z = group.center_of_mass().value_in(length_unit)
median_previous_group_id = numpy.median(group.previous_group_id)
logger.info(
"step %i group %i nstars %i mass %s xyz %f %f %f %s origin %i "
"LR %f %f %f %f %f %f %f %f %f %s",
model.step, group_id,
stars_in_group, mass_in_group,
x, y, z, length_unit,
median_previous_group_id,
radii[0], radii[1],
radii[2], radii[3],
radii[4], radii[5],
radii[6], radii[7],
radii[8],
length_unit,
)
groups = ParticlesSuperset(groups)
group_identifiers = Particles(keys=groups.key)
group_identifiers.group_id = groups.group_id
group_identifiers.previous_group_id = groups.previous_group_id
return group_identifiers
class BasicEksterModel:
def __init__(self):
self.gas_particles = Particles()
self.star_particles = Particles()
self.sink_particles = Particles()
self.star_converter = nbody_system.nbody_to_si(
0.25 | units.pc,
0.01 | units.Myr,
)
self.step = 0
def run_diagnostics(self):
self.groups = run_diagnostics(self)
def new_argument_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
'-i', dest="step", type=int, default=None,
help="snapshot number",
)
# parser.add_argument(
# '-g', dest="gas", type=str, default=None,
# help="gas file",
# )
# parser.add_argument(
# '-s', dest="stars", type=str, default=None,
# help="stars file",
# )
# parser.add_argument(
# '-i', dest="sinks", type=str, default=None,
# help="sinks file",
# )
return parser.parse_args()
def main():
logger = logging.getLogger(__name__)
logging.basicConfig(
filename="diagnostics.log",
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s: %(message)s',
datefmt='%Y%m%d %H:%M:%S'
)
args = new_argument_parser()
model = BasicEksterModel()
model.step = args.step
try:
gasfile = "gas-%04i.hdf5" % model.step
gas = read_set_from_file(gasfile, "amuse")
model.gas_particles.add_particles(gas)
except IoException:
gas = Particles()
try:
starsfile = "stars-%04i.hdf5" % model.step
stars = read_set_from_file(starsfile, "amuse")
if not hasattr(stars, "group_id"):
try:
groupsfile = "groups-%04i.hdf5" % (model.step-1)
groups = read_set_from_file(groupsfile, "amuse")
groups_to_stars = groups.new_channel_to(stars)
groups_to_stars.copy_attributes(["group_id"])
except IoException:
stars.group_id = 0
model.star_particles.add_particles(stars)
except IoException:
stars = Particles()
try:
sinksfile = "sinks-%04i.hdf5" % model.step
sinks = read_set_from_file(sinksfile, "amuse")
model.sink_particles.add_particles(sinks)
except IoException:
sinks = Particles()
model.run_diagnostics()
write_set_to_file(
model.groups, "groups-%04i.hdf5" % model.step, "amuse"
)
if __name__ == "__main__":
main()