Skip to content

Commit 88750b8

Browse files
committed
Add ArgParse & ConfParser; read params from cfg.ini
1 parent f157fc5 commit 88750b8

File tree

3 files changed

+104
-10
lines changed

3 files changed

+104
-10
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ authors = ["Simon Byrne <[email protected]>"]
44
version = "0.1.0"
55

66
[deps]
7+
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
8+
ConfParser = "88353bc9-fd38-507d-a820-d3b43837d6b9"
79
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
810
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
911
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

examples/L96m/cfg.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Integration]
2+
SOLVER = Tsit5
3+
4+
T = 4.0
5+
T_conv = 100.0
6+
7+
dtmax = 1e-3
8+
reltol = 1e-3
9+
abstol = 1e-6
10+

examples/L96m/main.jl

Lines changed: 92 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,117 @@
22

33
import DifferentialEquations
44
import PyPlot
5+
import ConfParser
6+
import ArgParse
7+
58
const DE = DifferentialEquations
69
const plt = PyPlot
10+
const CP = ConfParser
11+
const AP = ArgParse
712
include("L96m.jl")
813

14+
################################################################################
15+
# function section #############################################################
16+
################################################################################
17+
function parse_cli_args()
18+
ap_settings = AP.ArgParseSettings()
19+
AP.@add_arg_table ap_settings begin
20+
"--cfg", "-c"
21+
help = "name of the ini-file to be used as config"
22+
arg_type = String
23+
default = "cfg.ini"
24+
end
25+
26+
return AP.parse_args(ap_settings)
27+
end
28+
29+
function get_cfg_value(config::CP.ConfParse, sect::String, key::String, default)
30+
return try
31+
val = CP.retrieve(config, sect, key)
32+
if isa(val, String) && String != typeof(default)
33+
parse(typeof(default), val)
34+
else
35+
val
36+
end
37+
catch
38+
default
39+
end
40+
end
41+
42+
function print_var(var_name::String; offset::Int = 0)
43+
println(
44+
" "^offset,
45+
rpad(var_name, RPAD_VAR),
46+
lpad(eval(Symbol(var_name)), LPAD_VAR)
47+
)
48+
end
49+
50+
function print_var(var_names::Array{String})
51+
println("# Parameters")
52+
for var_name in var_names
53+
print_var(var_name, offset = 2)
54+
end
55+
end
956

1057
################################################################################
11-
# constants section ############################################################
58+
# config section ###############################################################
59+
################################################################################
60+
args_dict = parse_cli_args()
61+
config = try
62+
CP.ConfParse(args_dict["cfg"])
63+
catch
64+
CP.ConfParse("cfg.ini")
65+
end
66+
67+
CP.parse_conf!(config)
68+
69+
################################################################################
70+
# parameters section ###########################################################
1271
################################################################################
1372
const RPAD = 42
73+
const RPAD_VAR = 21
1474
const LPAD_INTEGER = 7
1575
const LPAD_FLOAT = 13
16-
const SOLVER = DE.Tsit5()
17-
18-
const T = 4 # integration time
19-
const T_compile = 1e-10 # force JIT compilation
20-
const T_conv = 100 # converging integration time
76+
const LPAD_VAR = 29
77+
78+
parameters = String[]
79+
# integration parameters #######################################################
80+
# T integration time
81+
# T_conv converging integration time
82+
# T_compile force JIT compilation
83+
push!(parameters, "T", "T_conv", "T_compile")
84+
85+
const T = get_cfg_value(config, "integration", "T", 4.0)
86+
const T_conv = get_cfg_value(config, "integration", "T_conv", 100.0)
87+
const T_compile = 1e-10
2188
#const T_learn = 15 # time to gather training data for GP
2289
#const T_hist = 10000 # time to gather histogram statistics
2390

24-
# integrator parameters
25-
const dtmax = 1e-3 # maximum step size
91+
# time-stepper parameters ######################################################
92+
# SOLVER time-stepper itself
93+
# dtmax maximum step size
94+
# reltol relative tolerance
95+
# abstol absolute tolerance
96+
push!(parameters, "SOLVER", "dtmax", "reltol", "abstol")
97+
98+
const SOLVER_STR = get_cfg_value(config, "integration", "SOLVER", "Tsit5")
99+
const SOLVER = getfield(DE, Symbol(SOLVER_STR))()
100+
const dtmax = get_cfg_value(config, "integration", "dtmax", 1e-3)
26101
#const tau = 1e-3 # maximum step size for histogram statistics
27102
#const dt_conv = 0.01 # maximum step size for converging to attractor
28-
const reltol = 1e-3 # relative tolerance
29-
const abstol = 1e-6 # absolute tolerance
103+
const reltol = get_cfg_value(config, "integration", "reltol", 1e-3)
104+
const abstol = get_cfg_value(config, "integration", "abstol", 1e-6)
30105

106+
# save/plot parameters #########################################################
31107
const k = 1 # index of the slow variable to save etc.
32108
const j = 1 # index of the fast variable to save/plot etc.
109+
push!(parameters, "k", "j")
33110

111+
# L96 parameters ###############################################################
34112
const hx = -0.8
113+
push!(parameters, "hx")
114+
115+
print_var(parameters)
35116

36117
################################################################################
37118
# IC section ###################################################################
@@ -50,6 +131,7 @@ end
50131
################################################################################
51132
# main section #################################################################
52133
################################################################################
134+
println("# Main")
53135

54136
# force compilation of functions used in numerical integration
55137
print(rpad("(JIT compilation)", RPAD))

0 commit comments

Comments
 (0)