From 51407b9fd6dabed5ae399217cc65bc4c95da4ad0 Mon Sep 17 00:00:00 2001 From: lrennels Date: Wed, 9 Sep 2020 20:58:33 -0700 Subject: [PATCH 1/6] Add a TODO comment --- src/core/defcomp.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/core/defcomp.jl b/src/core/defcomp.jl index 2b527d0fe..c41984e55 100644 --- a/src/core/defcomp.jl +++ b/src/core/defcomp.jl @@ -268,6 +268,12 @@ macro defcomp(comp_name, ex) end end + # TODO: We can discuss the problem with Distributions on Fri. We might just have slightly + # rewrite the expression that gets passed to the Main.eval call, so that in the expression + # tree we don't use a symbol Distributions (which it will then try to resolve in the Main + # module, where Distributions isn't loaded), but instead just put a ref to the Distributions + # module itself into the AST that we pass to eval. I think it should then work without a name + # resolution. datum_type = (datum_type === nothing ? Number : Main.eval(datum_type)) addexpr(_generate_var_or_param(elt_type, name, datum_type, dimensions, dflt, desc, unit)) From 8d3b44b345c8d0ddcaed5a29be5cf24989b264b8 Mon Sep 17 00:00:00 2001 From: lrennels Date: Wed, 9 Sep 2020 22:16:18 -0700 Subject: [PATCH 2/6] Try @__MODULE__ macro; add wip file --- src/core/defcomp.jl | 12 +++++------- wip/defcomp_bug.jl | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 wip/defcomp_bug.jl diff --git a/src/core/defcomp.jl b/src/core/defcomp.jl index c41984e55..404a5d336 100644 --- a/src/core/defcomp.jl +++ b/src/core/defcomp.jl @@ -268,13 +268,11 @@ macro defcomp(comp_name, ex) end end - # TODO: We can discuss the problem with Distributions on Fri. We might just have slightly - # rewrite the expression that gets passed to the Main.eval call, so that in the expression - # tree we don't use a symbol Distributions (which it will then try to resolve in the Main - # module, where Distributions isn't loaded), but instead just put a ref to the Distributions - # module itself into the AST that we pass to eval. I think it should then work without a name - # resolution. - datum_type = (datum_type === nothing ? Number : Main.eval(datum_type)) + @debug "before: datum type is $datum_type, which is of type $(typeof(datum_type))" + # datum_type = (datum_type === nothing ? Number : Main.eval(datum_type)) + datum_type = (datum_type === nothing ? Number : Base.eval(@__MODULE__, datum_type)) + @debug "after: datum type is $datum_type, which is of type $(typeof(datum_type))" + addexpr(_generate_var_or_param(elt_type, name, datum_type, dimensions, dflt, desc, unit)) else diff --git a/wip/defcomp_bug.jl b/wip/defcomp_bug.jl new file mode 100644 index 000000000..6bc4ea63c --- /dev/null +++ b/wip/defcomp_bug.jl @@ -0,0 +1,24 @@ +module brick + +using Mimi +using Distributions + +@defcomp component1 begin + + savingsrate = Parameter{Distributions.Normal{Float64}}() + pickrate = Variable(index=[time]) + function run_timestep(p, v, d, t) + v.pickrate[t] = rand(p.savingsrate) + end + +end + +function get_model() + m = Model() + set_dimension!(m, :time, collect(2015:5:2110)) + add_comp!(m, component1) + set_param!(m, :component1, :savingsrate, Distributions.Normal(1.0)) + return m +end + +end # module \ No newline at end of file From e3e1dafbd564ab5cdcc4d9816d58ea8881bbb047 Mon Sep 17 00:00:00 2001 From: lrennels Date: Wed, 9 Sep 2020 22:19:10 -0700 Subject: [PATCH 3/6] Edit wip file --- wip/defcomp_bug.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wip/defcomp_bug.jl b/wip/defcomp_bug.jl index 6bc4ea63c..6e48c73cd 100644 --- a/wip/defcomp_bug.jl +++ b/wip/defcomp_bug.jl @@ -21,4 +21,6 @@ function get_model() return m end -end # module \ No newline at end of file +end # module + +m = brick.get_model() From 7c6e95ec100f76770738e3fbd60d34ea4562422f Mon Sep 17 00:00:00 2001 From: Lisa Rennels <31779240+lrennels@users.noreply.github.com> Date: Wed, 9 Sep 2020 22:32:57 -0700 Subject: [PATCH 4/6] Revert change --- src/core/defcomp.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/defcomp.jl b/src/core/defcomp.jl index 404a5d336..6625020ac 100644 --- a/src/core/defcomp.jl +++ b/src/core/defcomp.jl @@ -269,8 +269,7 @@ macro defcomp(comp_name, ex) end @debug "before: datum type is $datum_type, which is of type $(typeof(datum_type))" - # datum_type = (datum_type === nothing ? Number : Main.eval(datum_type)) - datum_type = (datum_type === nothing ? Number : Base.eval(@__MODULE__, datum_type)) + datum_type = (datum_type === nothing ? Number : Main.eval(datum_type)) @debug "after: datum type is $datum_type, which is of type $(typeof(datum_type))" addexpr(_generate_var_or_param(elt_type, name, datum_type, dimensions, dflt, desc, unit)) From 64f1ea1b7bc00e4f23a6621711e264ef612eb035 Mon Sep 17 00:00:00 2001 From: lrennels Date: Fri, 11 Sep 2020 14:03:50 -0700 Subject: [PATCH 5/6] Change module for @defcomp eval --- src/core/defcomp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/defcomp.jl b/src/core/defcomp.jl index 6625020ac..985cfca11 100644 --- a/src/core/defcomp.jl +++ b/src/core/defcomp.jl @@ -269,7 +269,7 @@ macro defcomp(comp_name, ex) end @debug "before: datum type is $datum_type, which is of type $(typeof(datum_type))" - datum_type = (datum_type === nothing ? Number : Main.eval(datum_type)) + datum_type = (datum_type === nothing ? Number : (@__MODULE__).eval(datum_type)) @debug "after: datum type is $datum_type, which is of type $(typeof(datum_type))" addexpr(_generate_var_or_param(elt_type, name, datum_type, dimensions, dflt, desc, unit)) From 435763f6b49b8716cab4584e980e7212dc9b5b02 Mon Sep 17 00:00:00 2001 From: lrennels Date: Fri, 11 Sep 2020 14:27:56 -0700 Subject: [PATCH 6/6] Pass symbol instead of eval in defcomp --- src/core/defcomp.jl | 2 +- wip/defcomp_bug.jl | 26 -------------------------- 2 files changed, 1 insertion(+), 27 deletions(-) delete mode 100644 wip/defcomp_bug.jl diff --git a/src/core/defcomp.jl b/src/core/defcomp.jl index 985cfca11..39f5c52a1 100644 --- a/src/core/defcomp.jl +++ b/src/core/defcomp.jl @@ -269,7 +269,7 @@ macro defcomp(comp_name, ex) end @debug "before: datum type is $datum_type, which is of type $(typeof(datum_type))" - datum_type = (datum_type === nothing ? Number : (@__MODULE__).eval(datum_type)) + datum_type = (datum_type === nothing ? :Number : datum_type) @debug "after: datum type is $datum_type, which is of type $(typeof(datum_type))" addexpr(_generate_var_or_param(elt_type, name, datum_type, dimensions, dflt, desc, unit)) diff --git a/wip/defcomp_bug.jl b/wip/defcomp_bug.jl deleted file mode 100644 index 6e48c73cd..000000000 --- a/wip/defcomp_bug.jl +++ /dev/null @@ -1,26 +0,0 @@ -module brick - -using Mimi -using Distributions - -@defcomp component1 begin - - savingsrate = Parameter{Distributions.Normal{Float64}}() - pickrate = Variable(index=[time]) - function run_timestep(p, v, d, t) - v.pickrate[t] = rand(p.savingsrate) - end - -end - -function get_model() - m = Model() - set_dimension!(m, :time, collect(2015:5:2110)) - add_comp!(m, component1) - set_param!(m, :component1, :savingsrate, Distributions.Normal(1.0)) - return m -end - -end # module - -m = brick.get_model()