forked from trixi-framework/Trixi2Img.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.jl
99 lines (84 loc) · 2.89 KB
/
io.jl
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
using EllipsisNotation
# Use data file to extract mesh filename from attributes
function extract_mesh_filename(filename::String)
# Open file for reading
h5open(filename, "r") do file
# Extract filename relative to data file
mesh_file = read(attrs(file)["mesh_file"])
return joinpath(dirname(filename), mesh_file)
end
end
# Read in mesh file and return relevant data
function read_meshfile(filename::String)
# Open file for reading
h5open(filename, "r") do file
# Extract basic information
if exists(attrs(file), "ndims")
ndims = read(attrs(file)["ndims"])
else
ndims = read(attrs(file)["ndim"]) # FIXME once Trixi's 3D branch is merged & released
end
n_children_per_cell = 2^ndims
n_cells = read(attrs(file)["n_cells"])
n_leaf_cells = read(attrs(file)["n_leaf_cells"])
center_level_0 = read(attrs(file)["center_level_0"])
length_level_0 = read(attrs(file)["length_level_0"])
# Extract coordinates, levels, child cells
coordinates = Array{Float64}(undef, ndims, n_cells)
coordinates .= read(file["coordinates"])
levels = Array{Int}(undef, n_cells)
levels .= read(file["levels"])
child_ids = Array{Int}(undef, n_children_per_cell, n_cells)
child_ids .= read(file["child_ids"])
# Extract leaf cells (= cells to be plotted) and contract all other arrays accordingly
leaf_cells = similar(levels)
n_cells = 0
for cell_id in 1:length(levels)
if sum(child_ids[:, cell_id]) > 0
continue
end
n_cells += 1
leaf_cells[n_cells] = cell_id
end
leaf_cells = leaf_cells[1:n_cells]
coordinates = coordinates[:, leaf_cells]
levels = levels[leaf_cells]
return center_level_0, length_level_0, leaf_cells, coordinates, levels, ndims
end
end
function read_datafile(filename::String)
# Open file for reading
h5open(filename, "r") do file
ndims = read(attrs(file)["ndims"])
# Extract basic information
if exists(attrs(file), "polydeg")
polydeg = read(attrs(file)["polydeg"])
else
polydeg = read(attrs(file)["N"])
end
n_elements = read(attrs(file)["n_elements"])
n_variables = read(attrs(file)["n_vars"])
time = read(attrs(file)["time"])
# Extract labels for legend
labels = Array{String}(undef, 1, n_variables)
for v = 1:n_variables
labels[1, v] = read(attrs(file["variables_$v"])["name"])
end
# Extract data arrays
n_nodes = polydeg + 1
if ndims == 3
# Read 3d data
data = Array{Float64}(undef, n_nodes, n_nodes, n_nodes, n_elements, n_variables)
elseif ndims == 2
# Read 2d data
data = Array{Float64}(undef, n_nodes, n_nodes, n_elements, n_variables)
else
error("unsupported number of dimensions: $ndims")
end
for v = 1:n_variables
vardata = read(file["variables_$v"])
@views data[.., v][:] .= vardata
end
return labels, data, n_nodes, time
end
end