-
Notifications
You must be signed in to change notification settings - Fork 35
Explorer for Composite Components #711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using Mimi | ||
|
||
# components | ||
@defcomp Comp1 begin | ||
par_1_1 = Parameter(index=[time]) # external input | ||
var_1_1 = Variable(index=[time]) # computed | ||
foo = Parameter() | ||
|
||
function run_timestep(p, v, d, t) | ||
v.var_1_1[t] = p.par_1_1[t] | ||
end | ||
end | ||
|
||
@defcomp Comp2 begin | ||
par_2_1 = Parameter(index=[time]) # connected to Comp1.var_1_1 | ||
par_2_2 = Parameter(index=[time]) # external input | ||
var_2_1 = Variable(index=[time]) # computed | ||
foo = Parameter() | ||
|
||
function run_timestep(p, v, d, t) | ||
v.var_2_1[t] = p.par_2_1[t] + p.foo * p.par_2_2[t] | ||
end | ||
end | ||
|
||
@defcomp Comp3 begin | ||
par_3_1 = Parameter(index=[time]) # connected to Comp2.var_2_1 | ||
var_3_1 = Variable(index=[time]) # external output | ||
foo = Parameter(default=30) | ||
|
||
function run_timestep(p, v, d, t) | ||
# @info "Comp3 run_timestep" | ||
v.var_3_1[t] = p.par_3_1[t] * 2 | ||
end | ||
end | ||
|
||
@defcomp Comp4 begin | ||
par_4_1 = Parameter(index=[time]) # connected to Comp2.var_2_1 | ||
var_4_1 = Variable(index=[time]) # external output | ||
foo = Parameter(default=300) | ||
|
||
function run_timestep(p, v, d, t) | ||
# @info "Comp4 run_timestep" | ||
v.var_4_1[t] = p.par_4_1[t] * 2 | ||
end | ||
end | ||
|
||
@defcomposite A begin | ||
Component(Comp1) | ||
Component(Comp2) | ||
|
||
foo1 = Parameter(Comp1.foo) | ||
foo2 = Parameter(Comp2.foo) | ||
|
||
var_2_1 = Variable(Comp2.var_2_1) | ||
|
||
connect(Comp2.par_2_1, Comp1.var_1_1) | ||
connect(Comp2.par_2_2, Comp1.var_1_1) | ||
end | ||
|
||
@defcomposite B begin | ||
Component(Comp3) | ||
Component(Comp4) | ||
|
||
foo3 = Parameter(Comp3.foo) | ||
foo4 = Parameter(Comp4.foo) | ||
|
||
var_3_1 = Variable(Comp3.var_3_1) | ||
end | ||
|
||
@defcomposite top begin | ||
Component(A) | ||
|
||
fooA1 = Parameter(A.foo1) | ||
fooA2 = Parameter(A.foo2) | ||
|
||
# TBD: component B isn't getting added to mi | ||
Component(B) | ||
foo3 = Parameter(B.foo3) | ||
foo4 = Parameter(B.foo4) | ||
|
||
var_3_1 = Variable(B.var_3_1) | ||
|
||
connect(B.par_3_1, A.var_2_1) | ||
connect(B.par_4_1, B.var_3_1) | ||
end | ||
|
||
# model | ||
m = Model() | ||
md = m.md | ||
set_dimension!(m, :time, 2005:2020) | ||
add_comp!(m, top, nameof(top)) | ||
|
||
set_param!(m, :fooA1, 1) | ||
set_param!(m, :fooA2, 2) | ||
set_param!(m, :foo3, 10) | ||
set_param!(m, :foo4, 20) | ||
set_param!(m, :par_1_1, collect(1:length(time_labels(md)))) | ||
run(m) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using Mimi | ||
using Test | ||
using DataFrames | ||
using VegaLite | ||
using Electron | ||
|
||
import Mimi: | ||
dataframe_or_scalar, _spec_for_item, menu_item_list, getdataframe, dim_names, time_labels | ||
|
||
# Helper function returns true if VegaLite is verison 3 or above, and false otherwise | ||
function _is_VegaLite_v3() | ||
return isdefined(VegaLite, :vlplot) ? true : false | ||
end | ||
|
||
include("../examples/compositecomp-model.jl") # constructs and runs model | ||
# m's structure is as follows: | ||
# | ||
# top | ||
# / \ | ||
# A B | ||
# / \ / \ | ||
# 1 2 3 4 | ||
|
||
|
||
# 1. dataframe helper functions | ||
@test typeof(dataframe_or_scalar(m, :top, :fooA1)) == Float64 # same for fooA2, foo3, foo4 | ||
@test typeof(dataframe_or_scalar(m, :top, :var_3_1)) == DataFrame | ||
@test typeof(dataframe_or_scalar(m, :top, :par_1_1)) == DataFrame | ||
|
||
#2. Specs and menu | ||
items = [:fooA1, :fooA2, :foo3, :foo4, :var_3_1, :par_1_1] | ||
for item in items | ||
static_spec = _spec_for_item(m, :top, item; interactive = false) | ||
interactive_spec = _spec_for_item(m, :top, item) | ||
if length(dim_names(m, :top, item)) == 0 | ||
name = string(:top, " : ", item, " = ", m[:top, item]) | ||
else | ||
name = string(:top, " : ", item) | ||
end | ||
@test static_spec["name"] == interactive_spec["name"] == name | ||
end | ||
|
||
s = menu_item_list(m) | ||
@test typeof(s) == Array{Any, 1} | ||
@test length(s) == 6 | ||
|
||
#3. explore(m::Model, title = "Electron") | ||
w = explore(m, title = "Testing Window") | ||
@test typeof(w) == Electron.Window | ||
close(w) | ||
|
||
#4. Mim.plot(m::Model, comp_name::Symbol, datum_name::Symbol; | ||
# dim_name::Union{Nothing, Symbol} = nothing) | ||
items = [:fooA1, :fooA2, :foo3, :foo4, :var_3_1, :par_1_1] | ||
for item in items | ||
p = Mimi.plot(m, :top, item) | ||
p_type = _is_VegaLite_v3() ? VegaLite.VLSpec : VegaLite.VLSpec{:plot} | ||
@test typeof(p) == p_type | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ckingdon95 do you see a downside to doing it this way (generalizing to one signature for all
AbstractComponentDef
types instead of making a separate method for aCompositeComponentDef
? All tests pass, but you have a better sense of the theory behind this so I want to make sure I'm not breaking something.