Mercurial > repos > public > sbplib_julia
comparison EquidistantGrid.jl @ 129:1aaeb46ba5f4 cell_based_test
Improve efficiency of apply by the following:
- Remove divisions in interior loop by storing and multiplying by the reciprocal of grid spacing instead.
- Add @inline to apply(::Laplace
- Remove initialization of w = 0 in apply(::Stencil) by manually unrolling first iteration of the loop.
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Thu, 14 Feb 2019 16:25:22 +0100 |
parents | 631eb9b35d72 |
children | 155bbecf18bb |
comparison
equal
deleted
inserted
replaced
128:7c0b9bb7ab4d | 129:1aaeb46ba5f4 |
---|---|
6 | 6 |
7 struct EquidistantGrid{Dim,T<:Real} <: AbstractGrid | 7 struct EquidistantGrid{Dim,T<:Real} <: AbstractGrid |
8 size::NTuple{Dim, Int} # First coordinate direction stored first | 8 size::NTuple{Dim, Int} # First coordinate direction stored first |
9 limit_lower::NTuple{Dim, T} | 9 limit_lower::NTuple{Dim, T} |
10 limit_upper::NTuple{Dim, T} | 10 limit_upper::NTuple{Dim, T} |
11 spacing::NTuple{Dim, T} | 11 inverse_spacing::NTuple{Dim, T} # The reciprocal of the grid spacing |
12 | 12 |
13 # General constructor | 13 # General constructor |
14 function EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where Dim where T | 14 function EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where Dim where T |
15 @assert all(size.>0) | 15 @assert all(size.>0) |
16 @assert all(limit_upper.-limit_lower .!= 0) | 16 @assert all(limit_upper.-limit_lower .!= 0) |
17 spacing = abs.(limit_upper.-limit_lower)./(size.-1) | 17 inverse_spacing = (size.-1)./abs.(limit_upper.-limit_lower) |
18 return new{Dim,T}(size, limit_lower, limit_upper, spacing) | 18 return new{Dim,T}(size, limit_lower, limit_upper, inverse_spacing) |
19 end | 19 end |
20 end | |
21 | |
22 function Base.eachindex(grid::EquidistantGrid) | |
23 CartesianIndices(grid.size) | |
20 end | 24 end |
21 | 25 |
22 # Returns the number of dimensions of an EquidistantGrid. | 26 # Returns the number of dimensions of an EquidistantGrid. |
23 # | 27 # |
24 # @Input: grid - an EquidistantGrid | 28 # @Input: grid - an EquidistantGrid |
25 # @Return: dimension - The dimension of the grid | 29 # @Return: dimension - The dimension of the grid |
26 function dimension(grid::EquidistantGrid) | 30 function dimension(grid::EquidistantGrid) |
27 return length(grid.size) | 31 return length(grid.size) |
28 end | 32 end |
29 | 33 |
30 function Base.eachindex(grid::EquidistantGrid) | 34 # Returns the spacing of the grid |
31 CartesianIndices(grid.size) | 35 # |
36 function spacing(grid::EquidistantGrid) | |
37 return 1.0./grid.inverse_spacing | |
32 end | 38 end |
33 | 39 |
34 # Computes the points of an EquidistantGrid as a vector of tuples. The vector is ordered | 40 # Computes the points of an EquidistantGrid as a vector of tuples. The vector is ordered |
35 # such that points in the first coordinate direction varies first, then the second | 41 # such that points in the first coordinate direction varies first, then the second |
36 # and lastely the third (if applicable) | 42 # and lastely the third (if applicable) |