Skip to content

Commit bb6d005

Browse files
committed
Many bugs for long simulations and isolated simulations corrected
1 parent ed1fe25 commit bb6d005

File tree

9 files changed

+85
-70
lines changed

9 files changed

+85
-70
lines changed

src/analysis/compute_quantities/aggregators.jl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -503,16 +503,17 @@ function integrateQty(
503503

504504
if present_idx == 1
505505

506-
integrated_qty = 0.0u"Msun*yr^-1"
506+
integrated_qty = 0.0u"Msun * yr^-1"
507507

508508
else
509509

510510
# Get the physical times
511-
times = data_dict[:sim_data].snapshot_table[:, 5]
511+
times = data_dict[:sim_data].snapshot_table[!, :physical_times]
512+
512513
# Compute the time between snapshots
513514
Δt = times[present_idx] - times[present_idx - 1]
514515

515-
integrated_qty = sum(computeSFR(data_dict; age_resol=Δt); init=0.0u"Msun*yr^-1")
516+
integrated_qty = sum(computeSFR(data_dict; age_resol=Δt); init=0.0u"Msun * yr^-1")
516517

517518
end
518519

@@ -531,7 +532,7 @@ function integrateQty(
531532
else
532533

533534
# Get the physical times
534-
times = data_dict[:sim_data].snapshot_table[:, 5]
535+
times = data_dict[:sim_data].snapshot_table[!, :physical_times]
535536
# Compute the time between snapshots
536537
Δt = times[present_idx] - times[present_idx - 1]
537538

@@ -666,19 +667,19 @@ function integrateQty(
666667

667668
elseif quantity == :scale_factor
668669

669-
integrated_qty = data_dict[:sim_data].snapshot_table[data_dict[:snap_data].global_index, 3]
670+
integrated_qty = data_dict[:sim_data].snapshot_table[data_dict[:snap_data].global_index, :scale_factors]
670671

671672
elseif quantity == :redshift
672673

673-
integrated_qty = data_dict[:sim_data].snapshot_table[data_dict[:snap_data].global_index, 4]
674+
integrated_qty = data_dict[:sim_data].snapshot_table[data_dict[:snap_data].global_index, :redshifts]
674675

675676
elseif quantity == :physical_time
676677

677-
integrated_qty = data_dict[:sim_data].snapshot_table[data_dict[:snap_data].global_index, 5]
678+
integrated_qty = data_dict[:sim_data].snapshot_table[data_dict[:snap_data].global_index, :physical_times]
678679

679680
elseif quantity == :lookback_time
680681

681-
integrated_qty = data_dict[:sim_data].snapshot_table[data_dict[:snap_data].global_index, 6]
682+
integrated_qty = data_dict[:sim_data].snapshot_table[data_dict[:snap_data].global_index, :lookback_times]
682683

683684
elseif quantity == :ode_gas_it
684685

@@ -1328,7 +1329,7 @@ function scatterQty(data_dict::Dict, quantity::Symbol)::Vector{<:Number}
13281329
else
13291330

13301331
# Get the physical times
1331-
times = data_dict[:sim_data].snapshot_table[:, 5]
1332+
times = data_dict[:sim_data].snapshot_table[!, :physical_times]
13321333
# Compute the time between snapshots
13331334
Δt = times[present_idx] - times[present_idx - 1]
13341335

@@ -1351,7 +1352,7 @@ function scatterQty(data_dict::Dict, quantity::Symbol)::Vector{<:Number}
13511352
else
13521353

13531354
# Get the physical times
1354-
times = data_dict[:sim_data].snapshot_table[:, 5]
1355+
times = data_dict[:sim_data].snapshot_table[!, :physical_times]
13551356
# Compute the time between snapshots
13561357
Δt = times[present_idx] - times[present_idx - 1]
13571358

src/analysis/compute_quantities/other.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function computeTimeTicks(
114114
scale_factors = [readTime(path) for path in snapshot_paths]
115115
redshifts = @. (1.0 / scale_factors) - 1.0
116116
physical_times = computeTime(scale_factors, readSnapHeader(first_snapshot))
117-
lookback_times = last(physical_times) .- physical_times
117+
lookback_times = maximum(physical_times) .- physical_times
118118

119119
else
120120

@@ -127,7 +127,7 @@ function computeTimeTicks(
127127
redshifts = zeros(length(snapshot_paths))
128128
# For non-cosmological simulations, the time in the snapshot is the physical time
129129
physical_times = [readTime(path) * u_time for path in snapshot_paths]
130-
lookback_times = last(physical_times) .- physical_times
130+
lookback_times = maximum(physical_times) .- physical_times
131131

132132
end
133133

@@ -260,7 +260,7 @@ function computeSFR(
260260
!isempty(ages) || return Unitful.MassFlow[]
261261

262262
# Allocate memory
263-
sfr = zeros(typeof(1.0u"Msun*yr^-1"), length(ages))
263+
sfr = zeros(typeof(1.0u"Msun * yr^-1"), length(ages))
264264

265265
# Find the stellar particles younger than `age_resol`
266266
idxs = map(x -> x <= age_resol, ages)

src/analysis/data_acquisition.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,11 +1257,13 @@ function makeSimulationTable(simulation_path::String)::DataFrame
12571257
corresponding snapshot"))
12581258
)
12591259

1260-
paths = [snapshot_paths, groupcat_paths]
1261-
labels = [:snapshot_paths, :groupcat_paths]
1262-
rows = [[1:length(snapshot_paths);], [1:length(groupcat_paths);]]
1260+
# Number of rows
1261+
n = length(snapshot_paths)
12631262

1264-
source_table = unstack(flatten(DataFrame(l=labels, p=paths, ids=rows), [:p, :ids]), :l, :p)
1263+
source_table = DataFrame(
1264+
snapshot_paths = snapshot_paths,
1265+
groupcat_paths = [groupcat_paths; fill(missing, n - length(groupcat_paths))],
1266+
)
12651267

12661268
# Add the file name number column
12671269
numbers = snap_source[:numbers]
@@ -1282,7 +1284,13 @@ function makeSimulationTable(simulation_path::String)::DataFrame
12821284
# Add the lookback time column
12831285
insertcols!(source_table, 6, :lookback_times => lookback_times; copycols=false)
12841286

1285-
return identity.(sort!(DataFrame(source_table), [:physical_times]))
1287+
# Sort the table by physical time
1288+
sort!(source_table, :physical_times)
1289+
1290+
# Add the row indices
1291+
insertcols!(source_table, 1, :row_id => 1:nrow(source_table))
1292+
1293+
return identity.(DataFrame(source_table))
12861294

12871295
end
12881296

@@ -1938,9 +1946,9 @@ function internalUnits(quantity::String, path::String)::Union{Unitful.Quantity,U
19381946

19391947
elseif dimensions == Unitful.𝐓
19401948

1941-
# From internal units to Myr, for non-cosmological simulations,
1949+
# From internal units to Myr for non-cosmological simulations,
19421950
# and to a dimensionless quantity for cosmological simulations
1943-
return cosmological ? Unitful.NoUnits : IU.t_cosmo
1951+
return cosmological ? Unitful.NoUnits : IU.t_newton
19441952

19451953
elseif dimensions == Unitful.𝐌 * Unitful.𝐋^-3
19461954

src/analysis/data_analysis.jl

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4397,13 +4397,13 @@ function daEvolution(
43974397

43984398
for (slice_index, sim_table_data) in pairs(iterator)
43994399

4400-
global_index = sim_table_data[1]
4401-
scale_factor = sim_table_data[3]
4402-
redshift = sim_table_data[4]
4403-
physical_time = sim_table_data[5]
4404-
lookback_time = sim_table_data[6]
4405-
snapshot_path = sim_table_data[7]
4406-
groupcat_path = sim_table_data[8]
4400+
global_index = sim_table_data[:row_id]
4401+
scale_factor = sim_table_data[:scale_factors]
4402+
redshift = sim_table_data[:redshifts]
4403+
physical_time = sim_table_data[:physical_times]
4404+
lookback_time = sim_table_data[:lookback_times]
4405+
snapshot_path = sim_table_data[:snapshot_paths]
4406+
groupcat_path = sim_table_data[:groupcat_paths]
44074407

44084408
# Skip missing snapshots
44094409
!ismissing(snapshot_path) || continue
@@ -4586,12 +4586,12 @@ function daVirialAccretion(
45864586
:sim_data => sim_data,
45874587
:snap_data => Snapshot(
45884588
snapshot_path,
4589-
sim_table_data[1],
4589+
sim_table_data[:row_id],
45904590
1,
4591-
sim_table_data[5],
4592-
sim_table_data[6],
4593-
sim_table_data[3],
4594-
sim_table_data[4],
4591+
sim_table_data[:physical_times],
4592+
sim_table_data[:lookback_times],
4593+
sim_table_data[:scale_factors],
4594+
sim_table_data[:redshifts],
45954595
snapshot_header,
45964596
),
45974597
:gc_data => GroupCatalog(groupcat_path, groupcat_header),
@@ -4620,13 +4620,13 @@ function daVirialAccretion(
46204620

46214621
for (slice_index, sim_table_data) in pairs(iterator[2:end])
46224622

4623-
global_index = sim_table_data[1]
4624-
scale_factor = sim_table_data[3]
4625-
redshift = sim_table_data[4]
4626-
physical_time = sim_table_data[5]
4627-
lookback_time = sim_table_data[6]
4628-
snapshot_path = sim_table_data[7]
4629-
groupcat_path = sim_table_data[8]
4623+
global_index = sim_table_data[:row_id]
4624+
scale_factor = sim_table_data[:scale_factors]
4625+
redshift = sim_table_data[:redshifts]
4626+
physical_time = sim_table_data[:physical_times]
4627+
lookback_time = sim_table_data[:lookback_times]
4628+
snapshot_path = sim_table_data[:snapshot_paths]
4629+
groupcat_path = sim_table_data[:groupcat_paths]
46304630

46314631
# Get the snapshot header
46324632
snapshot_header = readSnapHeader(snapshot_path)
@@ -4810,12 +4810,12 @@ function daDiscAccretion(
48104810
:sim_data => sim_data,
48114811
:snap_data => Snapshot(
48124812
snapshot_path,
4813-
sim_table_data[1],
4813+
sim_table_data[:row_id],
48144814
1,
4815-
sim_table_data[5],
4816-
sim_table_data[6],
4817-
sim_table_data[3],
4818-
sim_table_data[4],
4815+
sim_table_data[:physical_times],
4816+
sim_table_data[:lookback_times],
4817+
sim_table_data[:scale_factors],
4818+
sim_table_data[:redshifts],
48194819
snapshot_header,
48204820
),
48214821
:gc_data => GroupCatalog(groupcat_path, groupcat_header),
@@ -4846,13 +4846,13 @@ function daDiscAccretion(
48464846

48474847
for (slice_index, sim_table_data) in pairs(iterator[2:end])
48484848

4849-
global_index = sim_table_data[1]
4850-
scale_factor = sim_table_data[3]
4851-
redshift = sim_table_data[4]
4852-
physical_time = sim_table_data[5]
4853-
lookback_time = sim_table_data[6]
4854-
snapshot_path = sim_table_data[7]
4855-
groupcat_path = sim_table_data[8]
4849+
global_index = sim_table_data[:row_id]
4850+
scale_factor = sim_table_data[:scale_factors]
4851+
redshift = sim_table_data[:redshifts]
4852+
physical_time = sim_table_data[:physical_times]
4853+
lookback_time = sim_table_data[:lookback_times]
4854+
snapshot_path = sim_table_data[:snapshot_paths]
4855+
groupcat_path = sim_table_data[:groupcat_paths]
48564856

48574857
# Get the snapshot header
48584858
snapshot_header = readSnapHeader(snapshot_path)
@@ -4951,7 +4951,7 @@ function daSFRtxt(
49514951
smooth::Int=0,
49524952
)::NTuple{2,Vector{<:Number}}
49534953

4954-
snapshot_paths = filter(!ismissing, sim_data.snapshot_table[!, 7])
4954+
snapshot_paths = filter(!ismissing, sim_data.snapshot_table[!, :snapshot_paths])
49554955

49564956
(
49574957
!isempty(snapshot_paths) ||
@@ -5094,7 +5094,7 @@ function daCPUtxt(
50945094
smooth::Int=0,
50955095
)::NTuple{2,Vector{<:Number}}
50965096

5097-
snapshot_paths = filter(!ismissing, sim_data.snapshot_table[!, 7])
5097+
snapshot_paths = filter(!ismissing, sim_data.snapshot_table[!, :snapshot_paths])
50985098

50995099
(
51005100
!isempty(snapshot_paths) ||

src/analysis/filters.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ function filterOldStars(data_dict::Dict)::Dict{Symbol,IndexType}
11141114
end
11151115

11161116
# Get the physical times
1117-
times = data_dict[:sim_data].snapshot_table[:, 5]
1117+
times = data_dict[:sim_data].snapshot_table[!, :physical_times]
11181118

11191119
new_stars_idxs = map(t -> t > times[present_idx - 1], birth_times)
11201120

src/auxiliary_functions/histograms.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function histogram1D(
136136
width = (p_max - p_min) / n_bins
137137

138138
# Allocate memory
139-
histogram = zeros(eltype(values), n_bins)
139+
histogram = zeros(typeof(first(values)), n_bins)
140140
counts = zeros(Int, n_bins)
141141

142142
# Compute the histogram, ignoring NaNs and positions outside the grid range
@@ -227,7 +227,7 @@ function histogram1D(
227227
p_max = last(edges)
228228

229229
# Allocate memory
230-
histogram = zeros(eltype(values), n_bins)
230+
histogram = zeros(typeof(first(values)), n_bins)
231231
counts = zeros(Int, n_bins)
232232

233233
# Compute the histogram, ignoring NaNs and positions outside of range
@@ -420,7 +420,7 @@ function histogram2D(
420420
y_borders = (grid.y_bins[1] - h_bin_width, grid.y_bins[end] + h_bin_width)
421421

422422
# Allocate memory
423-
histogram = zeros(eltype(values), size(grid.grid))
423+
histogram = zeros(typeof(first(values)), size(grid.grid))
424424
counts = zeros(Int, size(grid.grid))
425425

426426
# Compute the histogram, ignoring NaNs and positions outside of range
@@ -529,7 +529,7 @@ function histogram2D(
529529
y_borders = (first(y_edges), last(y_edges))
530530

531531
# Allocate memory
532-
histogram = zeros(eltype(values), (n_x_bins, n_y_bins))
532+
histogram = zeros(typeof(first(values)), (n_x_bins, n_y_bins))
533533
counts = zeros(Int, (n_x_bins, n_y_bins))
534534

535535
# Compute the histogram, ignoring NaNs and positions outside of range
@@ -773,7 +773,7 @@ function histogram3D(
773773
z_borders = (grid.z_bins[1] - h_bin_width, grid.z_bins[end] + h_bin_width)
774774

775775
# Allocate memory
776-
histogram = zeros(eltype(values), size(grid.grid))
776+
histogram = zeros(typeof(first(values)), size(grid.grid))
777777
counts = zeros(Int, size(grid.grid))
778778

779779
# Compute the histogram, ignoring NaNs and positions outside of range

src/constants/globals.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ Unit conversion factors.
660660
- `m_cosmo::Unitful.Mass`: From internal units of mass to ``\\mathrm{M_\\odot}``.
661661
- `t_cgs::Unitful.Time`: From internal units of time to ``\\mathrm{s}``.
662662
- `t_cosmo::Unitful.Time`: From internal units of time to ``\\mathrm{Myr}``.
663+
- `t_newton::Unitful.Time`: From internal units (non-cosmological simulations) of time to ``\\mathrm{Myr}``.
663664
- `U_cgs::Unitful.Energy`: From internal units of specific energy to ``\\mathrm{erg \\, g^{-1}}``.
664665
- `rho_cgs::Unitful.Density`: From internal units of density to ``\\mathrm{g \\, cm^{-3}}``.
665666
- `P_Pa::Unitful.Pressure`: From internal units of pressure to ``\\mathrm{Pa}``.
@@ -678,6 +679,7 @@ struct InternalUnits
678679

679680
t_cgs::Unitful.Time # From internal units of time to s
680681
t_cosmo::Unitful.Time # From internal units of time to Myr
682+
t_newton::Unitful.Time # From internal units (non-cosmological simulations) of time to Myr
681683

682684
U_cgs::SpecificEnergy # From internal units of specific energy to erg * g^-1
683685

@@ -732,6 +734,7 @@ struct InternalUnits
732734
# Time conversion factors
733735
t_cgs = x_cgs / v_cgs
734736
t_cosmo = t_cgs |> u"Myr"
737+
t_newton = 1.0u"Gyr" |> u"Myr"
735738

736739
# Specific energy conversion factor
737740
U_cgs = v_unit^2 |> u"erg * g^-1"
@@ -753,6 +756,7 @@ struct InternalUnits
753756
m_cosmo,
754757
t_cgs,
755758
t_cosmo,
759+
t_newton,
756760
U_cgs,
757761
rho_cgs,
758762
P_Pa,

src/plotting/convenience.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,14 @@ function snapshotReport(
8787
$(slice_n), the contents of $(simulation_path) are: \n$(simulation_table)"))
8888
)
8989

90+
# Select the ordinal index of the target snapshot
91+
o_idx = findfirst(==(lpad(snap_number, 3, "0")), simulation_table.numbers)
92+
9093
# Find the target row
91-
snapshot_row = filter(:numbers => ==(lpad(snap_number, 3, "0")), simulation_table)
94+
snapshot_row = simulation_table[o_idx, :]
9295

9396
# Read the path to the target snapshot
94-
snapshot_path = snapshot_row[1, :snapshot_paths]
97+
snapshot_path = snapshot_row[:snapshot_paths]
9598

9699
# Check that the snapshot path is not missing
97100
snapshot_filename = "\"$(SNAP_BASENAME)_$(lpad(snap_number, 3, "0"))\""
@@ -102,16 +105,13 @@ function snapshotReport(
102105
)
103106

104107
# Read the path to the target group catalog file
105-
groupcat_path = snapshot_row[1, :groupcat_paths]
108+
groupcat_path = snapshot_row[:groupcat_paths]
106109

107110
# Check if the simulation is cosmological
108111
cosmological = isSnapCosmological(snapshot_path)
109112

110113
# Read the physical time since the Big Bang
111-
physical_time = round(ustrip(u"Gyr", snapshot_row[1, :physical_times]), digits=2)
112-
113-
# Select the ordinal index of the target snapshot
114-
o_idx = snapshot_row[1, :ids]
114+
physical_time = round(ustrip(u"Gyr", snapshot_row[:physical_times]), digits=2)
115115

116116
# Compute the number of snapshots in the folder
117117
snapshot_length = count(!ismissing, simulation_table[!, :snapshot_paths])
@@ -136,8 +136,8 @@ function snapshotReport(
136136

137137
if cosmological
138138
# For cosmological simulations print the scale factor and the redshift
139-
scale_factor = round(snapshot_row[1, :scale_factors], digits=3)
140-
redshift = round(snapshot_row[1, :redshifts], digits=3)
139+
scale_factor = round(snapshot_row[:scale_factors], digits=3)
140+
redshift = round(snapshot_row[:redshifts], digits=3)
141141

142142
println(file, "Scale factor: $(scale_factor)")
143143
println(file, "Redshift: $(redshift)")

0 commit comments

Comments
 (0)