Mercurial > repos > public > sbplib_julia
comparison src/SbpOperators/readoperator.jl @ 836:91a63b04b1c9 operator_storage_array_of_table
Attempt to make variable names for parse functions more descriptive
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 13 Jan 2022 13:09:10 +0100 |
parents | fc2ac236dd73 |
children | 510f744d0876 |
comparison
equal
deleted
inserted
replaced
835:fc2ac236dd73 | 836:91a63b04b1c9 |
---|---|
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 # TODO: Think about naming and terminology around freshly parsed TOML. answer: toml_dict/parsed_toml? | |
13 | 12 |
14 # TODO: Docs for readoperator.jl | 13 # TODO: Docs for readoperator.jl |
15 # Parsing as rationals is intentional, allows preserving exactness, which can be lowered using converts or promotions later. | 14 # Parsing as rationals is intentional, allows preserving exactness, which can be lowered using converts or promotions later. |
16 # Documetning the format: Allows representing rationals as strings | 15 # Documetning the format: Allows representing rationals as strings |
17 # TODO: Remove references to toml for dict-input arguments | |
18 | 16 |
19 """ | 17 """ |
20 read_stencil_set(fn; filters) | 18 read_stencil_set(fn; filters) |
21 | 19 |
22 Picks out a stencil set from the given toml file based on some key-value filters. | 20 Picks out a stencil set from the given toml file based on some key-value |
23 If more than one set matches the filters an error is raised. | 21 filters. If more than one set matches the filters an error is raised. The |
22 stencil set contains parsed toml intended for functions like `parse_scalar` | |
23 and `parse_stencil`. | |
24 | 24 |
25 The stencil set is not parsed beyond the inital toml parse. To get usable | 25 The stencil set is not parsed beyond the inital toml parse. To get usable |
26 stencils use the `parse_stencil` functions on the fields of the stencil set. | 26 stencils use the `parse_stencil` functions on the fields of the stencil set. |
27 | 27 |
28 The reason for this is that since stencil sets are intended to be very | 28 The reason for this is that since stencil sets are intended to be very |
58 i = matches[1] | 58 i = matches[1] |
59 return parsed_toml["stencil_set"][i] | 59 return parsed_toml["stencil_set"][i] |
60 end | 60 end |
61 | 61 |
62 """ | 62 """ |
63 parse_stencil(toml) | 63 parse_stencil(parsed_toml) |
64 | 64 |
65 Accepts parsed toml and reads it as a stencil | 65 Accepts parsed parsed_toml and reads it as a stencil |
66 | 66 |
67 See also [`read_stencil_set`](@ref), [`parse_scalar`](@ref), [`parse_tuple`](@ref). | 67 See also [`read_stencil_set`](@ref), [`parse_scalar`](@ref), [`parse_tuple`](@ref). |
68 """ | 68 """ |
69 function parse_stencil(toml) | 69 function parse_stencil(parsed_toml) |
70 check_stencil_toml(toml) | 70 check_stencil_toml(parsed_toml) |
71 | 71 |
72 if toml isa Array | 72 if parsed_toml isa Array |
73 weights = parse_rational.(toml) | 73 weights = parse_rational.(parsed_toml) |
74 return CenteredStencil(weights...) | 74 return CenteredStencil(weights...) |
75 end | 75 end |
76 | 76 |
77 weights = parse_rational.(toml["s"]) | 77 weights = parse_rational.(parsed_toml["s"]) |
78 return Stencil(weights..., center = toml["c"]) | 78 return Stencil(weights..., center = parsed_toml["c"]) |
79 end | 79 end |
80 | 80 |
81 """ | 81 """ |
82 parse_stencil(T, toml) | 82 parse_stencil(T, parsed_toml) |
83 | 83 |
84 Parses the stencil with element type `T` | 84 Parses the stencil with element type `T` |
85 """ | 85 """ |
86 parse_stencil(T, toml) = Stencil{T}(parse_stencil(toml)) | 86 parse_stencil(T, parsed_toml) = Stencil{T}(parse_stencil(parsed_toml)) |
87 | 87 |
88 function check_stencil_toml(toml) | 88 function check_stencil_toml(parsed_toml) |
89 if !(toml isa Dict || toml isa Vector{String}) | 89 if !(parsed_toml isa Dict || parsed_toml isa Vector{String}) |
90 throw(ArgumentError("the TOML for a stencil must be a vector of strings or a table.")) | 90 throw(ArgumentError("the TOML for a stencil must be a vector of strings or a table.")) |
91 end | 91 end |
92 | 92 |
93 if toml isa Vector{String} | 93 if parsed_toml isa Vector{String} |
94 return | 94 return |
95 end | 95 end |
96 | 96 |
97 if !(haskey(toml, "s") && haskey(toml, "c")) | 97 if !(haskey(parsed_toml, "s") && haskey(parsed_toml, "c")) |
98 throw(ArgumentError("the table form of a stencil must have fields `s` and `c`.")) | 98 throw(ArgumentError("the table form of a stencil must have fields `s` and `c`.")) |
99 end | 99 end |
100 | 100 |
101 if !(toml["s"] isa Vector{String}) | 101 if !(parsed_toml["s"] isa Vector{String}) |
102 throw(ArgumentError("a stencil must be specified as a vector of strings.")) | 102 throw(ArgumentError("a stencil must be specified as a vector of strings.")) |
103 end | 103 end |
104 | 104 |
105 if !(toml["c"] isa Int) | 105 if !(parsed_toml["c"] isa Int) |
106 throw(ArgumentError("the center of a stencil must be specified as an integer.")) | 106 throw(ArgumentError("the center of a stencil must be specified as an integer.")) |
107 end | 107 end |
108 end | 108 end |
109 | 109 |
110 """ | 110 """ |
111 parse_scalar(toml) | 111 parse_scalar(parsed_toml) |
112 | 112 |
113 Parse a scalar, represented as a string or a number in the TOML, and return it as a `Rational` | 113 Parse a scalar, represented as a string or a number in the TOML, and return it as a `Rational` |
114 | 114 |
115 See also [`read_stencil_set`](@ref), [`parse_stencil`](@ref) [`parse_tuple`](@ref). | 115 See also [`read_stencil_set`](@ref), [`parse_stencil`](@ref) [`parse_tuple`](@ref). |
116 """ | 116 """ |
117 function parse_scalar(toml) | 117 function parse_scalar(parsed_toml) |
118 try | 118 try |
119 return parse_rational(toml) | 119 return parse_rational(parsed_toml) |
120 catch e | 120 catch e |
121 throw(ArgumentError("must be a number or a string representing a number.")) | 121 throw(ArgumentError("must be a number or a string representing a number.")) |
122 end | 122 end |
123 end | 123 end |
124 | 124 |
125 """ | 125 """ |
126 parse_tuple(toml) | 126 parse_tuple(parsed_toml) |
127 | 127 |
128 Parse `toml` as a tuple of scalars. | 128 Parse `parsed_toml` as a tuple of scalars. |
129 | 129 |
130 See also [`read_stencil_set`](@ref), [`parse_stencil`](@ref), [`parse_scalar`](@ref). | 130 See also [`read_stencil_set`](@ref), [`parse_stencil`](@ref), [`parse_scalar`](@ref). |
131 """ | 131 """ |
132 function parse_tuple(toml) | 132 function parse_tuple(parsed_toml) |
133 if !(toml isa Array) | 133 if !(parsed_toml isa Array) |
134 throw(ArgumentError("argument must be an array")) | 134 throw(ArgumentError("argument must be an array")) |
135 end | 135 end |
136 return Tuple(parse_scalar.(toml)) | 136 return Tuple(parse_scalar.(parsed_toml)) |
137 end | 137 end |
138 | 138 |
139 | 139 |
140 """ | 140 """ |
141 parse_rational(toml) | 141 parse_rational(parsed_toml) |
142 | 142 |
143 Parse a string or a number as a rational. | 143 Parse a string or a number as a rational. |
144 """ | 144 """ |
145 function parse_rational(toml) | 145 function parse_rational(parsed_toml) |
146 if toml isa String | 146 if parsed_toml isa String |
147 expr = Meta.parse(replace(toml, "/"=>"//")) | 147 expr = Meta.parse(replace(parsed_toml, "/"=>"//")) |
148 return eval(:(Rational($expr))) | 148 return eval(:(Rational($expr))) |
149 else | 149 else |
150 return Rational(toml) | 150 return Rational(parsed_toml) |
151 end | 151 end |
152 end | 152 end |
153 | 153 |
154 """ | 154 """ |
155 sbp_operators_path() | 155 sbp_operators_path() |