Skip to content

Commit

Permalink
Use eachindex instead of 1:length, compare against nothing with…
Browse files Browse the repository at this point in the history
… `===` (#1899)

* Use `eachindex` instead of `1:length` and compare against nothing with `===`

* revert some places

* revert for solvers

* revert

* revert

* revert

* fmt

* fmt

* revert
  • Loading branch information
DanielDoehring authored Apr 9, 2024
1 parent c025873 commit c6fc9c5
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion docs/literate/src/files/non_periodic_boundaries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ sol = solve(ode, CarpenterKennedy2N54(williamson_condition=false),
save_everystep=false, saveat=visnodes, callback=callbacks);

using Plots
@gif for step in 1:length(sol.u)
@gif for step in eachindex(sol.u)
plot(sol.u[step], semi, ylim=(-1.5, 1.5), legend=true, label="approximation", title="time t=$(round(sol.t[step], digits=5))")
scatter!([0.0], [sum(boundary_condition(SVector(0.0), sol.t[step], equations))], label="boundary condition")
end
Expand Down
4 changes: 2 additions & 2 deletions docs/literate/src/files/scalar_linear_advection_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ integral = sum(nodes.^3 .* weights)
x = Matrix{Float64}(undef, length(nodes), n_elements)
for element in 1:n_elements
x_l = coordinates_min + (element - 1) * dx + dx/2
for i in 1:length(nodes)
for i in eachindex(nodes)
ξ = nodes[i] # nodes in [-1, 1]
x[i, element] = x_l + dx/2 * ξ
end
Expand Down Expand Up @@ -417,7 +417,7 @@ dx = (coordinates_max - coordinates_min) / n_elements # length of one element
x = Matrix{Float64}(undef, length(nodes), n_elements)
for element in 1:n_elements
x_l = -1 + (element - 1) * dx + dx/2
for i in 1:length(nodes) # basis points in [-1, 1]
for i in eachindex(nodes) # basis points in [-1, 1]
ξ = nodes[i]
x[i, element] = x_l + dx/2 * ξ
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function (indicator::IndicatorSolutionIndependent)(u::AbstractArray{<:Any, 4},
outer_distance = 1.85

#Iterate over all elements
for element in 1:length(alpha)
for element in eachindex(alpha)
# Calculate periodic distance between cell and center.
# This requires an uncurved mesh!
coordinates = SVector(0.5 * (cache.elements.node_coordinates[1, 1, 1, element] +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function (indicator::IndicatorSolutionIndependent)(u::AbstractArray{<:Any, 4},
outer_distance = 1.85

# Iterate over all elements.
for element in 1:length(alpha)
for element in eachindex(alpha)
# Calculate periodic distance between cell and center.
# This requires an uncurved mesh!
coordinates = SVector(0.5 * (cache.elements.node_coordinates[1, 1, 1, element] +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function (indicator::IndicatorSolutionIndependent)(u::AbstractArray{<:Any, 4},
outer_distance = 1.85

#Iterate over all elements
for element in 1:length(alpha)
for element in eachindex(alpha)
#Calculate periodic distance between cell and center.
cell_id = cache.elements.cell_ids[element]
coordinates = mesh.tree.coordinates[1:2, cell_id]
Expand Down
8 changes: 4 additions & 4 deletions src/callbacks_step/amr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ function (amr_callback::AMRCallback)(u_ode::AbstractVector, mesh::TreeMesh,
@unpack to_refine, to_coarsen = amr_callback.amr_cache
empty!(to_refine)
empty!(to_coarsen)
for element in 1:length(lambda)
for element in eachindex(lambda)
controller_value = lambda[element]
if controller_value > 0
push!(to_refine, leaf_cell_ids[element])
Expand Down Expand Up @@ -307,7 +307,7 @@ function (amr_callback::AMRCallback)(u_ode::AbstractVector, mesh::TreeMesh,
end

# Extract only those parent cells for which all children should be coarsened
to_coarsen = collect(1:length(parents_to_coarsen))[parents_to_coarsen .== 2^ndims(mesh)]
to_coarsen = collect(eachindex(parents_to_coarsen))[parents_to_coarsen .== 2^ndims(mesh)]

# Finally, coarsen mesh
coarsened_original_cells = @trixi_timeit timer() "mesh" coarsen!(mesh.tree,
Expand Down Expand Up @@ -395,7 +395,7 @@ function (amr_callback::AMRCallback)(u_ode::AbstractVector, mesh::TreeMesh,
@unpack to_refine, to_coarsen = amr_callback.amr_cache
empty!(to_refine)
empty!(to_coarsen)
for element in 1:length(lambda)
for element in eachindex(lambda)
controller_value = lambda[element]
if controller_value > 0
push!(to_refine, leaf_cell_ids[element])
Expand Down Expand Up @@ -456,7 +456,7 @@ function (amr_callback::AMRCallback)(u_ode::AbstractVector, mesh::TreeMesh,
end

# Extract only those parent cells for which all children should be coarsened
to_coarsen = collect(1:length(parents_to_coarsen))[parents_to_coarsen .== 2^ndims(mesh)]
to_coarsen = collect(eachindex(parents_to_coarsen))[parents_to_coarsen .== 2^ndims(mesh)]

# Finally, coarsen mesh
coarsened_original_cells = @trixi_timeit timer() "mesh" coarsen!(mesh.tree,
Expand Down
2 changes: 1 addition & 1 deletion src/callbacks_step/euler_acoustics_coupling_dg2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function calc_acoustic_sources!(acoustic_source_terms, u_euler, u_acoustics,
dg::DGSEM, cache)
acoustic_source_terms .= zero(eltype(acoustic_source_terms))

@threaded for k in 1:length(coupled_element_ids)
@threaded for k in eachindex(coupled_element_ids)
element = coupled_element_ids[k]

for j in eachnode(dg), i in eachnode(dg)
Expand Down
16 changes: 8 additions & 8 deletions src/callbacks_step/time_series_dg_tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function get_elements_by_coordinates!(element_ids, coordinates, mesh::TreeMesh,
cell_id = cell_ids[element]

# Iterate over coordinates
for index in 1:length(element_ids)
for index in eachindex(element_ids)
# Skip coordinates for which an element has already been found
if element_ids[index] > 0
continue
Expand Down Expand Up @@ -63,7 +63,7 @@ function calc_interpolating_polynomials!(interpolating_polynomials, coordinates,

wbary = barycentric_weights(nodes)

for index in 1:length(element_ids)
for index in eachindex(element_ids)
# Construct point
x = SVector(ntuple(i -> coordinates[i, index], ndims(mesh)))

Expand Down Expand Up @@ -94,7 +94,7 @@ function record_state_at_points!(point_data, u, solution_variables,
new_length = old_length + n_solution_variables

# Loop over all points/elements that should be recorded
for index in 1:length(element_ids)
for index in eachindex(element_ids)
# Extract data array and element id
data = point_data[index]
element_id = element_ids[index]
Expand All @@ -108,7 +108,7 @@ function record_state_at_points!(point_data, u, solution_variables,
u_node = solution_variables(get_node_vars(u, equations, dg, i,
element_id), equations)

for v in 1:length(u_node)
for v in eachindex(u_node)
data[old_length + v] += (u_node[v] *
interpolating_polynomials[i, 1, index])
end
Expand All @@ -126,7 +126,7 @@ function record_state_at_points!(point_data, u, solution_variables,
new_length = old_length + n_solution_variables

# Loop over all points/elements that should be recorded
for index in 1:length(element_ids)
for index in eachindex(element_ids)
# Extract data array and element id
data = point_data[index]
element_id = element_ids[index]
Expand All @@ -140,7 +140,7 @@ function record_state_at_points!(point_data, u, solution_variables,
u_node = solution_variables(get_node_vars(u, equations, dg, i, j,
element_id), equations)

for v in 1:length(u_node)
for v in eachindex(u_node)
data[old_length + v] += (u_node[v]
* interpolating_polynomials[i, 1, index]
* interpolating_polynomials[j, 2, index])
Expand All @@ -159,7 +159,7 @@ function record_state_at_points!(point_data, u, solution_variables,
new_length = old_length + n_solution_variables

# Loop over all points/elements that should be recorded
for index in 1:length(element_ids)
for index in eachindex(element_ids)
# Extract data array and element id
data = point_data[index]
element_id = element_ids[index]
Expand All @@ -173,7 +173,7 @@ function record_state_at_points!(point_data, u, solution_variables,
u_node = solution_variables(get_node_vars(u, equations, dg, i, j, k,
element_id), equations)

for v in 1:length(u_node)
for v in eachindex(u_node)
data[old_length + v] += (u_node[v]
* interpolating_polynomials[i, 1, index]
* interpolating_polynomials[j, 2, index]
Expand Down
10 changes: 5 additions & 5 deletions src/callbacks_step/time_series_dg_unstructured.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function get_elements_by_coordinates!(element_ids, coordinates,
# Iterate over coordinates
distances = zeros(eltype(mesh.corners), mesh.n_elements)
indices = zeros(Int, mesh.n_elements, 2)
for index in 1:length(element_ids)
for index in eachindex(element_ids)
# Grab the current point for which the element needs found
point = SVector(coordinates[1, index],
coordinates[2, index])
Expand Down Expand Up @@ -77,7 +77,7 @@ function get_elements_by_coordinates!(element_ids, coordinates,
# Loop through all the element candidates until we find a vector from the barycenter
# to the surface that points in the same direction as the current `point` vector.
# This then gives us the correct element.
for element in 1:length(candidates)
for element in eachindex(candidates)
bary_center = SVector(bary_centers[1, candidates[element]],
bary_centers[2, candidates[element]])
# Vector pointing from the barycenter toward the minimal `surface_point`
Expand Down Expand Up @@ -153,7 +153,7 @@ function calc_interpolating_polynomials!(interpolating_polynomials, coordinates,
# Helper array for a straight-sided quadrilateral element
corners = zeros(eltype(mesh.corners), 4, 2)

for index in 1:length(element_ids)
for index in eachindex(element_ids)
# Construct point
x = SVector(ntuple(i -> coordinates[i, index], ndims(mesh)))

Expand Down Expand Up @@ -280,7 +280,7 @@ function record_state_at_points!(point_data, u, solution_variables,
new_length = old_length + n_solution_variables

# Loop over all points/elements that should be recorded
for index in 1:length(element_ids)
for index in eachindex(element_ids)
# Extract data array and element id
data = point_data[index]
element_id = element_ids[index]
Expand All @@ -294,7 +294,7 @@ function record_state_at_points!(point_data, u, solution_variables,
u_node = solution_variables(get_node_vars(u, equations, dg, i, j,
element_id), equations)

for v in 1:length(u_node)
for v in eachindex(u_node)
data[old_length + v] += (u_node[v]
* interpolating_polynomials[i, 1, index]
* interpolating_polynomials[j, 2, index])
Expand Down
4 changes: 2 additions & 2 deletions src/semidiscretization/semidiscretization_euler_acoustics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function precompute_weights(source_region, weights, coupled_element_ids, equatio
(nnodes(dg), nnodes(dg),
length(coupled_element_ids)))

@threaded for k in 1:length(coupled_element_ids)
@threaded for k in eachindex(coupled_element_ids)
element = coupled_element_ids[k]
for j in eachnode(dg), i in eachnode(dg)
x = get_node_coords(cache.elements.node_coordinates, equations, dg, i, j,
Expand Down Expand Up @@ -197,7 +197,7 @@ function add_acoustic_source_terms!(du_acoustics, acoustic_source_terms, source_
coupled_element_ids, mesh::TreeMesh{2}, equations,
dg::DGSEM,
cache)
@threaded for k in 1:length(coupled_element_ids)
@threaded for k in eachindex(coupled_element_ids)
element = coupled_element_ids[k]

for j in eachnode(dg), i in eachnode(dg)
Expand Down
4 changes: 2 additions & 2 deletions src/visualization/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ function cell2node(cell_centered_data)
resolution_in, _ = size(first(cell_centered_data))
resolution_out = resolution_in + 1
node_centered_data = [Matrix{Float64}(undef, resolution_out, resolution_out)
for _ in 1:length(cell_centered_data)]
for _ in eachindex(cell_centered_data)]

for (cell_data, node_data) in zip(cell_centered_data, node_centered_data)
# Fill center with original data
Expand Down Expand Up @@ -1545,7 +1545,7 @@ end

# Create an axis.
function axis_curve(nodes_x, nodes_y, nodes_z, slice, point, n_points)
if n_points == nothing
if n_points === nothing
n_points = 64
end
dimensions = length(point)
Expand Down
2 changes: 1 addition & 1 deletion test/test_unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ end
function MyContainer(data, capacity)
c = MyContainer(Vector{Int}(undef, capacity + 1), capacity, length(data),
capacity + 1)
c.data[1:length(data)] .= data
c.data[eachindex(data)] .= data
return c
end
MyContainer(data::AbstractArray) = MyContainer(data, length(data))
Expand Down
8 changes: 4 additions & 4 deletions utils/trixi2txt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function trixi2txt(filename::AbstractString...;
"maximum supported level $max_supported_level")
end
max_available_nodes_per_finest_element = 2^(max_supported_level - max_level)
if nvisnodes == nothing
if nvisnodes === nothing
max_nvisnodes = 2 * n_nodes
elseif nvisnodes == 0
max_nvisnodes = n_nodes
Expand Down Expand Up @@ -137,9 +137,9 @@ function trixi2txt(filename::AbstractString...;
println(io)

# Data
for idx in 1:length(xs)
for idx in eachindex(xs)
@printf(io, "%+10.8e", xs[idx])
for variable_id in 1:length(variables)
for variable_id in eachindex(variables)
@printf(io, " %+10.8e ", node_centered_data[idx, variable_id])
end
println(io)
Expand Down Expand Up @@ -199,7 +199,7 @@ function read_meshfile(filename::String)
# 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)
for cell_id in eachindex(levels)
if sum(child_ids[:, cell_id]) > 0
continue
end
Expand Down

0 comments on commit c6fc9c5

Please sign in to comment.