Mercurial > repos > public > sbplib_julia
comparison src/SbpOperators/readoperator.jl @ 832:00f6bbdcd73a operator_storage_array_of_table
Review: Include latest changes
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 12 Jan 2022 15:54:21 +0100 |
parents | 760c11e81fd4 |
children | fc2ac236dd73 |
comparison
equal
deleted
inserted
replaced
813:cdc2b5ebf7cb | 832:00f6bbdcd73a |
---|---|
2 | 2 |
3 export read_stencil_set | 3 export read_stencil_set |
4 export get_stencil_set | 4 export get_stencil_set |
5 | 5 |
6 export parse_stencil | 6 export parse_stencil |
7 export parse_rational | 7 export parse_scalar |
8 export parse_tuple | |
8 | 9 |
9 export sbp_operators_path | 10 export sbp_operators_path |
10 | 11 |
11 # The read_stencil_set and get_stencil_set functions return the freshly parsed | 12 # The read_stencil_set and get_stencil_set functions return the freshly parsed |
12 # toml. The generic code in these functions can't be expected to know anyhting | 13 # toml. The generic code in these functions can't be expected to know anyhting |
30 | 31 |
31 # TODO: Docs for readoperator.jl | 32 # TODO: Docs for readoperator.jl |
32 # Parsing as rationals is intentional, allows preserving exactness, which can be lowered using converts or promotions later. | 33 # Parsing as rationals is intentional, allows preserving exactness, which can be lowered using converts or promotions later. |
33 # TODO: readoperator.jl file name? | 34 # TODO: readoperator.jl file name? |
34 # TODO: Remove references to toml for dict-input arguments | 35 # TODO: Remove references to toml for dict-input arguments |
36 # TODO: Documetning the format: Allows representing rationals as strings | |
35 | 37 |
36 """ | 38 """ |
37 read_stencil_set(fn; filters) | 39 read_stencil_set(fn; filters) |
38 | 40 |
39 Picks out a stencil set from the given toml file based on some filters. | 41 Picks out a stencil set from the given toml file based on some filters. |
83 | 85 |
84 weights = parse_rational.(toml["s"]) | 86 weights = parse_rational.(toml["s"]) |
85 return Stencil(weights..., center = toml["c"]) | 87 return Stencil(weights..., center = toml["c"]) |
86 end | 88 end |
87 | 89 |
90 parse_stencil(T, toml) = Stencil{T}(parse_stencil(toml)) | |
91 | |
88 function check_stencil_toml(toml) | 92 function check_stencil_toml(toml) |
89 if !(toml isa Dict || toml isa Vector{String}) | 93 if !(toml isa Dict || toml isa Vector{String}) |
90 throw(ArgumentError("the TOML for a stencil must be a vector of strings or a table.")) | 94 throw(ArgumentError("the TOML for a stencil must be a vector of strings or a table.")) |
91 end | 95 end |
92 | 96 |
105 if !(toml["c"] isa Int) | 109 if !(toml["c"] isa Int) |
106 throw(ArgumentError("the center of a stencil must be specified as an integer.")) | 110 throw(ArgumentError("the center of a stencil must be specified as an integer.")) |
107 end | 111 end |
108 end | 112 end |
109 | 113 |
110 function parse_rational(str) | 114 |
111 expr = Meta.parse(replace(str, "/"=>"//")) | 115 function parse_scalar(toml) |
112 return eval(:(Rational($expr))) | 116 try |
117 return parse_rational(toml) | |
118 catch e | |
119 throw(ArgumentError("must be a number or a string representing a number.")) | |
120 end | |
121 end | |
122 | |
123 function parse_tuple(toml) | |
124 if !(toml isa Array) | |
125 throw(ArgumentError("argument must be an array")) | |
126 end | |
127 return Tuple(parse_scalar.(toml)) | |
128 end | |
129 | |
130 function parse_rational(toml) | |
131 if toml isa String | |
132 expr = Meta.parse(replace(toml, "/"=>"//")) | |
133 return eval(:(Rational($expr))) | |
134 else | |
135 return Rational(toml) | |
136 end | |
113 end | 137 end |
114 | 138 |
115 sbp_operators_path() = (@__DIR__) * "/operators/" | 139 sbp_operators_path() = (@__DIR__) * "/operators/" |