This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.py
244 lines (211 loc) · 9.6 KB
/
simulation.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
import json
from pathlib import Path
from typing import Optional
import yaml
class Simulation:
def __init__(self):
self.runid = None # type:int
self.vcode = None
self.alphacode = None
self.mcode = None
self.gammacode = None
self.wtcode = None
self.wpcode = None
self.type = None # simulation set
self.v = None # v/v_esc
self.alpha = None # impact angle
self.total_mass = None # m
self.projectile_mass = None # mp
self.target_mass = None # mt
self.projectile_water_fraction = None # wp
self.projectile_core_fraction = None
self.target_water_fraction = None # wt
self.target_core_fraction = None
self.largest_aggregate_mass = None # mS1
self.largest_aggregate_water_fraction = None # wmfS1
self.largest_aggregate_core_fraction = None
self.second_largest_aggregate_mass = None # mS2
self.second_largest_aggregate_water_fraction = None # wmfS2
self.second_largest_aggregate_core_fraction = None
self.rel_velocity = None # vrel
self.rel_velocity_per_esc_velocity = None # vrel_over_vesc
self.desired_N = None
self.actual_N = None
self.relaxation_time = None
self.miluphcuda_time = None
self.setup_time = None
@classmethod
def from_dict(cls, data: dict):
sim = cls()
for key in data:
setattr(sim, key, data[key])
return sim
@property
def gamma(self) -> float:
return self.projectile_mass / self.target_mass
@property
def relative_projectile_mass(self) -> float:
return self.projectile_mass / self.total_mass
@property
def relative_target_mass(self) -> float:
return self.target_mass / self.total_mass
@property
def largest_aggregate_relative_mass(self) -> float:
"""
p['mS1_over_mt'] = p['mS1'] / p['mt']
"""
return self.largest_aggregate_mass / self.target_mass
@property
def largest_aggregate_mantle_fraction(self) -> float:
return 1 - self.largest_aggregate_core_fraction - self.largest_aggregate_water_fraction
@property
def second_largest_aggregate_relative_mass(self) -> float:
"""
p['mS2_over_mp'] = p['mS2'] / p['mp']
"""
return self.second_largest_aggregate_mass / self.projectile_mass
@property
def second_largest_aggregate_mantle_fraction(self) -> float:
return 1 - self.second_largest_aggregate_core_fraction - self.second_largest_aggregate_water_fraction
@property
def projectile_mantle_fraction(self):
return 1 - self.projectile_water_fraction - self.projectile_core_fraction
@property
def target_mantle_fraction(self):
return 1 - self.target_water_fraction - self.target_core_fraction
@property
def initial_water_mass(self) -> float:
return self.projectile_mass * self.projectile_water_fraction + self.target_mass * self.target_water_fraction
@property
def initial_core_mass(self) -> float:
return self.projectile_mass * self.projectile_core_fraction + self.target_mass * self.target_core_fraction
@property
def initial_mantle_mass(self) -> float:
return self.projectile_mass * self.projectile_mantle_fraction + self.target_mass * self.target_mantle_fraction
@property
def water_retention_both(self) -> float:
"""
p['wretentionB'] = (p['mS1'] * p['wmfS1'] + p['mS2'] * p['wmfS2']) / (p['mp'] * p['wp'] + p['mt'] * p['wt'])
"""
return (
self.largest_aggregate_mass * self.largest_aggregate_water_fraction
+ self.second_largest_aggregate_mass * self.second_largest_aggregate_water_fraction
) / self.initial_water_mass
@property
def core_retention_both(self) -> float:
return (
self.largest_aggregate_mass * self.largest_aggregate_core_fraction
+ self.second_largest_aggregate_mass * self.largest_aggregate_core_fraction
) / self.initial_core_mass
@property
def mantle_retention_both(self) -> float:
return (
self.largest_aggregate_mass * self.largest_aggregate_mantle_fraction
+ self.second_largest_aggregate_mass * self.largest_aggregate_mantle_fraction
) / self.initial_mantle_mass
@property
def water_retention_main(self) -> float:
"""
p['wretention1'] = p['mS1'] * p['wmfS1'] / (p['mp'] * p['wp'] + p['mt'] * p['wt'])
"""
return self.largest_aggregate_mass * self.largest_aggregate_water_fraction / self.initial_water_mass
@property
def output_mass_fraction(self) -> Optional[float]:
if not self.largest_aggregate_mass:
return 0 # FIXME
massive_size_relation=self.largest_aggregate_mass/self.target_mass
less_massive_size_relation=self.second_largest_aggregate_mass/self.projectile_mass
print("mr",massive_size_relation, less_massive_size_relation)
return less_massive_size_relation / massive_size_relation
@property
def original_simulation(self) -> bool:
return self.type == "original"
@property
def testcase(self) -> bool:
return self.type == "cloud" and 529 <= self.runid <= 631
@property
def simulation_key(self):
return "id{:04d}_v{:.1f}_a{:.0f}_m{:.0f}_g{:.1f}_wt{:.1f}_wp{:.1f}".format(
self.runid, self.vcode, self.alphacode, self.mcode, self.gammacode, self.wtcode, self.wpcode
)
def __repr__(self):
return f"<Simulation '{vars(self)}'>"
def load_params_from_dirname(self, dirname: str) -> None:
params = dirname.split("_")
self.runid = int(params[0][2:])
self.vcode = float(params[1][1:])
self.alphacode = float(params[2][1:])
self.mcode = float(params[3][1:])
self.gammacode = float(params[4][1:])
self.wtcode = float(params[5][2:])
self.wpcode = float(params[6][2:])
assert dirname == self.simulation_key
def load_params_from_json(self, paramfile: str) -> None:
with open(paramfile) as f:
data = json.load(f)
self.runid = int(data["run_id"])
self.vcode = data["v_code"]
self.alphacode = data["alpha_code"]
self.mcode = data["m_code"]
self.gammacode = data["gamma_code"]
self.wtcode = data["wt_code"]
self.wpcode = data["wp_code"]
def load_params_from_setup_txt(self, file: Path) -> None:
with file.open() as f:
data = yaml.safe_load(f)
# self.runid=data["ID"]
# self.vcode=data["vel_vesc_touching_ball"]
# self.alphacode=data["impact_angle_touching_ball"]
# self.mcode=data["M_tot"]
# self.gammacode=data["gamma"]
# TODO: maybe more needed?
def load_params_from_spheres_ini_log(self, filename: Path) -> None:
with filename.open() as f:
lines = [line.rstrip("\n") for line in f]
for i in range(len(lines)):
line = lines[i]
if "Geometry:" in line:
self.v = float(lines[i + 2].split(" = ")[-1])
print(lines[i + 3])
# self.alpha = float(lines[i + 3].split(" = ")[-1][:-1]) #for old format
self.alpha = float(lines[i + 3].split(" = ")[-1].split(" ")[0])
if "Masses:" in line:
self.total_mass = float(lines[i + 2].split()[3])
self.projectile_mass = float(lines[i + 4].split()[3])
self.target_mass = float(lines[i + 6].split()[3])
if "Mantle/shell mass fractions:" in line:
self.projectile_water_fraction = float(lines[i + 1].split()[7])
self.target_water_fraction = float(lines[i + 3].split()[7])
if "Particle numbers" in line:
self.desired_N = int(lines[i + 1].split()[4])
self.actual_N = int(lines[i + 1].split()[-1])
def load_params_from_aggregates_txt(self, filename: Path) -> None:
with filename.open() as f:
lines = [line.rstrip("\n") for line in f]
for i in range(len(lines)):
line = lines[i]
if "# largest aggregate" in line:
cols = lines[i + 2].split()
self.largest_aggregate_mass = float(cols[0])
self.largest_aggregate_core_fraction = float(cols[1])
self.largest_aggregate_water_fraction = 1 - float(cols[2]) - self.largest_aggregate_core_fraction
if "# 2nd-largest aggregate:" in line:
cols = lines[i + 2].split()
self.second_largest_aggregate_mass = float(cols[0])
self.second_largest_aggregate_core_fraction = float(cols[1])
self.second_largest_aggregate_water_fraction = 1 - float(
cols[2]) - self.second_largest_aggregate_core_fraction
if "# distance" in line: # TODO: not sure if correct anymore
self.distance = float(lines[i + 1].split()[0])
self.rel_velocity = float(lines[i + 1].split()[1])
self.rel_velocity_per_esc_velocity = float(lines[i + 1].split()[2])
def load_params_from_pythontiming_json(self, filename: str) -> None:
with open(filename) as f:
data = json.load(f)
self.miluphcuda_time = data["miluphcuda"]
self.relaxation_time = data["relaxation"]
self.setup_time = data["setup"]
def assert_all_loaded(self) -> None:
for key, value in self.__dict__.items():
print(key, value)
assert value is not None