Mercurial > repos > public > sbplib_julia
comparison src/SbpOperators/readoperator.jl @ 989:7bf3121c6864 feature/stencil_set_type
Add type StencilSet
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Thu, 17 Mar 2022 21:31:20 +0100 |
parents | 568058183791 |
children | b6238afd3bb0 |
comparison
equal
deleted
inserted
replaced
988:83046af6143a | 989:7bf3121c6864 |
---|---|
1 using TOML | 1 using TOML |
2 | 2 |
3 export read_stencil_set | 3 export StencilSet |
4 export get_stencil_set | 4 export get_stencil_set |
5 | 5 |
6 export parse_stencil | 6 export parse_stencil |
7 export parse_scalar | 7 export parse_scalar |
8 export parse_tuple | 8 export parse_tuple |
9 | 9 |
10 export sbp_operators_path | 10 export sbp_operators_path |
11 | 11 |
12 """ | |
13 StencilSet | |
14 | |
15 A `StencilSet` contains a set of associated stencils. The stencils | |
16 are are stored in a table, and can be accesed by indexing into the `StencilSet`. | |
17 """ | |
18 struct StencilSet | |
19 table | |
20 end | |
12 | 21 |
13 """ | 22 """ |
14 read_stencil_set(filename; filters) | 23 StencilSet(filename; filters) |
15 | 24 |
16 Picks out a stencil set from a TOML file based on some key-value | 25 Creates a `StencilSet` from a TOML file based on some key-value |
17 filters. If more than one set matches the filters an error is raised. The | 26 filters. If more than one set matches the filters an error is raised. The |
18 returned stencil set contains parsed TOML intended for functions like | 27 table of the `StencilSet` is a parsed TOML intended for functions like |
19 `parse_scalar` and `parse_stencil`. | 28 `parse_scalar` and `parse_stencil`. |
20 | 29 |
21 The stencil set is not parsed beyond the inital TOML parse. To get usable | 30 The `StencilSet` table is not parsed beyond the inital TOML parse. To get usable |
22 stencils use the `parse_stencil` functions on the fields of the stencil set. | 31 stencils use the `parse_stencil` functions on the fields of the stencil set. |
23 | 32 |
24 The reason for this is that since stencil sets are intended to be very | 33 The reason for this is that since stencil sets are intended to be very |
25 general, and currently do not include any way to specify how to parse a given | 34 general, and currently do not include any way to specify how to parse a given |
26 section, the exact parsing is left to the user. | 35 section, the exact parsing is left to the user. |
27 | 36 |
28 For more information see [Operator file format](@ref) in the documentation. | 37 For more information see [Operator file format](@ref) in the documentation. |
29 | 38 |
30 See also [`sbp_operators_path`](@ref), [`get_stencil_set`](@ref), [`parse_stencil`](@ref), [`parse_scalar`](@ref), [`parse_tuple`](@ref),. | 39 See also [`sbp_operators_path`](@ref), [`get_stencil_set`](@ref), [`parse_stencil`](@ref), [`parse_scalar`](@ref), [`parse_tuple`](@ref),. |
31 """ | 40 """ |
32 read_stencil_set(filename; filters...) = get_stencil_set(TOML.parsefile(filename); filters...) | 41 StencilSet(filename; filters...) = StencilSet(get_stencil_set(TOML.parsefile(filename); filters...)) |
42 Base.getindex(set::StencilSet,I...) = set.table[I...] | |
33 | 43 |
34 """ | 44 """ |
35 get_stencil_set(parsed_toml; filters...) | 45 get_stencil_set(parsed_toml; filters...) |
36 | 46 |
37 Picks out a stencil set from an already parsed TOML based on some key-value | 47 Picks out a stencil set from an already parsed TOML based on some key-value |
38 filters. | 48 filters. |
39 | 49 |
40 See also [`read_stencil_set`](@ref). | 50 See also [`StencilSet`](@ref). |
41 """ | 51 """ |
42 function get_stencil_set(parsed_toml; filters...) | 52 function get_stencil_set(parsed_toml; filters...) |
43 matches = findall(parsed_toml["stencil_set"]) do set | 53 matches = findall(parsed_toml["stencil_set"]) do set |
44 for (key, val) ∈ filters | 54 for (key, val) ∈ filters |
45 if set[string(key)] != val | 55 if set[string(key)] != val |
61 """ | 71 """ |
62 parse_stencil(parsed_toml) | 72 parse_stencil(parsed_toml) |
63 | 73 |
64 Accepts parsed TOML and reads it as a stencil. | 74 Accepts parsed TOML and reads it as a stencil. |
65 | 75 |
66 See also [`read_stencil_set`](@ref), [`parse_scalar`](@ref), [`parse_tuple`](@ref). | 76 See also [`StencilSet`](@ref), [`parse_scalar`](@ref), [`parse_tuple`](@ref). |
67 """ | 77 """ |
68 function parse_stencil(parsed_toml) | 78 function parse_stencil(parsed_toml) |
69 check_stencil_toml(parsed_toml) | 79 check_stencil_toml(parsed_toml) |
70 | 80 |
71 if parsed_toml isa Array | 81 if parsed_toml isa Array |
109 """ | 119 """ |
110 parse_scalar(parsed_toml) | 120 parse_scalar(parsed_toml) |
111 | 121 |
112 Parse a scalar, represented as a string or a number in the TOML, and return it as a `Rational` | 122 Parse a scalar, represented as a string or a number in the TOML, and return it as a `Rational` |
113 | 123 |
114 See also [`read_stencil_set`](@ref), [`parse_stencil`](@ref) [`parse_tuple`](@ref). | 124 See also [`StencilSet`](@ref), [`parse_stencil`](@ref) [`parse_tuple`](@ref). |
115 """ | 125 """ |
116 function parse_scalar(parsed_toml) | 126 function parse_scalar(parsed_toml) |
117 try | 127 try |
118 return parse_rational(parsed_toml) | 128 return parse_rational(parsed_toml) |
119 catch e | 129 catch e |
124 """ | 134 """ |
125 parse_tuple(parsed_toml) | 135 parse_tuple(parsed_toml) |
126 | 136 |
127 Parse an array as a tuple of scalars. | 137 Parse an array as a tuple of scalars. |
128 | 138 |
129 See also [`read_stencil_set`](@ref), [`parse_stencil`](@ref), [`parse_scalar`](@ref). | 139 See also [`StencilSet`](@ref), [`parse_stencil`](@ref), [`parse_scalar`](@ref). |
130 """ | 140 """ |
131 function parse_tuple(parsed_toml) | 141 function parse_tuple(parsed_toml) |
132 if !(parsed_toml isa Array) | 142 if !(parsed_toml isa Array) |
133 throw(ArgumentError("argument must be an array")) | 143 throw(ArgumentError("argument must be an array")) |
134 end | 144 end |
153 """ | 163 """ |
154 sbp_operators_path() | 164 sbp_operators_path() |
155 | 165 |
156 Calculate the path for the operators folder with included stencil sets. | 166 Calculate the path for the operators folder with included stencil sets. |
157 | 167 |
158 See also [`read_stencil_set`](@ref) | 168 See also [`StencilSet`](@ref) |
159 """ | 169 """ |
160 sbp_operators_path() = (@__DIR__) * "/operators/" | 170 sbp_operators_path() = (@__DIR__) * "/operators/" |