Skip to content

Commit 68dfa18

Browse files
committed
Add docstrings and rearrange code for clarity
1 parent 4bfd247 commit 68dfa18

File tree

14 files changed

+532
-54
lines changed

14 files changed

+532
-54
lines changed

src/components/adder.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ using Mimi
1313
v.output[t] = @allow_missing(p.input[t]) + p.add[t]
1414
end
1515
end
16-

src/core/build.jl

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
connector_comp_name(i::Int) = Symbol("ConnectorComp$i")
1+
"""
2+
_substitute_views!(vals::Array{T, N}, comp_def) where {T, N}
23
4+
For each value in `vals`, if the value is a `TimestepArray` swap in a new
5+
TimestepArray with the same type parameterization but with its `data` field
6+
holding a view of the original value's `data` defined by the first and last
7+
indices of Component `comp_def`.
8+
"""
39
# helper function to substitute views for data
410
function _substitute_views!(vals::Array{T, N}, comp_def) where {T, N}
511
times = [keys(comp_def.dim_dict[:time])...]
@@ -12,6 +18,13 @@ function _substitute_views!(vals::Array{T, N}, comp_def) where {T, N}
1218
end
1319
end
1420

21+
"""
22+
_get_view(val::TimestepArray{T_TS, T, N, ti, S}, first_idx, last_idx) where {T_TS, T, N, ti, S}
23+
24+
Return a TimestepArray with the same type parameterization as the `val` TimestepArray,
25+
but with its `data` field holding a view of the `val.data` based on the entered
26+
`first-idx` and `last_idx`.
27+
"""
1528
function _get_view(val::TimestepArray{T_TS, T, N, ti, S}, first_idx, last_idx) where {T_TS, T, N, ti, S}
1629

1730
idxs = Array{Any}(fill(:, N))
@@ -21,7 +34,12 @@ function _get_view(val::TimestepArray{T_TS, T, N, ti, S}, first_idx, last_idx) w
2134
return TimestepArray{T_TS, T, N, ti}(val.data isa SubArray ? view(val.data.parent, idxs...) : view(val.data, idxs...))
2235
end
2336

24-
# Return the datatype to use for instance variables/parameters
37+
"""
38+
_instance_datatype(md::ModelDef, def::AbstractDatumDef)
39+
40+
Return the datatype of the AbstractDataumDef `def` in ModelDef `md`, which will
41+
be used to create ModelInstance instance variables and parameters.
42+
"""
2543
function _instance_datatype(md::ModelDef, def::AbstractDatumDef)
2644
dtype = def.datatype == Number ? number_type(md) : def.datatype
2745
dims = dim_names(def)
@@ -51,6 +69,13 @@ function _instance_datatype(md::ModelDef, def::AbstractDatumDef)
5169
return T
5270
end
5371

72+
"""
73+
_instantiate_datum(md::ModelDef, def::AbstractDatumDef)
74+
75+
Return the parameterized datum, broadly either Scalar or Array, pertaining to
76+
AbstractDatumDef `def` in the Model Def `md`, that will support instantiate of parameters
77+
and variables.
78+
"""
5479
# Create the Ref or Array that will hold the value(s) for a Parameter or Variable
5580
function _instantiate_datum(md::ModelDef, def::AbstractDatumDef)
5681
dtype = _instance_datatype(md, def)
@@ -106,7 +131,12 @@ function _instantiate_component_vars(md::ModelDef, comp_def::ComponentDef)
106131
return ComponentInstanceVariables(names, types, values, paths)
107132
end
108133

109-
# Creates the top-level vars for the model
134+
"""
135+
function _instantiate_vars(md::ModelDef)
136+
137+
Create the top-level variables for the Model Def `md` and return the dictionary
138+
of the resulting ComponentInstanceVariables.
139+
"""
110140
function _instantiate_vars(md::ModelDef)
111141
vdict = Dict{ComponentPath, Any}()
112142
recurse(md, cd -> vdict[cd.comp_path] = _instantiate_component_vars(md, cd); leaf_only=true)
@@ -207,7 +237,16 @@ function _get_leaf_level_epcs(md::ModelDef, epc::ExternalParameterConnection)
207237
return leaf_epcs
208238
end
209239

210-
# Collect all parameters with connections to allocated variable storage
240+
# generic helper function to get connector component name
241+
connector_comp_name(i::Int) = Symbol("ConnectorComp$i")
242+
243+
"""
244+
_collect_params(md::ModelDef, var_dict::Dict{ComponentPath, Any})
245+
246+
Collect all parameters in ModelDef `md` with connections to allocated variable
247+
storage in `var_dict` and return a dictionary of (comp_path, par_name) => ModelParameter
248+
elements.
249+
"""
211250
function _collect_params(md::ModelDef, var_dict::Dict{ComponentPath, Any})
212251

213252
# @info "Collecting params for $(comp_def.comp_id)"
@@ -251,6 +290,12 @@ function _collect_params(md::ModelDef, var_dict::Dict{ComponentPath, Any})
251290
return pdict
252291
end
253292

293+
"""
294+
_instantiate_params(comp_def::ComponentDef, par_dict::Dict{Tuple{ComponentPath, Symbol}, Any})
295+
296+
Create the top-level parameters for the Model Def `md` using the parameter dictionary
297+
`par_dict` and return the resulting ComponentInstanceParameters.
298+
"""
254299
function _instantiate_params(comp_def::ComponentDef, par_dict::Dict{Tuple{ComponentPath, Symbol}, Any})
255300
# @info "Instantiating params for $(comp_def.comp_path)"
256301
comp_path = comp_def.comp_path
@@ -263,7 +308,16 @@ function _instantiate_params(comp_def::ComponentDef, par_dict::Dict{Tuple{Compon
263308
return ComponentInstanceParameters(names, types, vals, paths)
264309
end
265310

266-
# Return a built leaf or composite LeafComponentInstance
311+
"""
312+
_build(comp_def::ComponentDef,
313+
var_dict::Dict{ComponentPath, Any},
314+
par_dict::Dict{Tuple{ComponentPath, Symbol}, Any},
315+
time_bounds::Tuple{Int, Int})
316+
317+
Return a built leaf or composite LeafComponentInstance created using ComponentDef
318+
`comp_def`, variables and parameters from `var_dict` and `par_dict` and the time
319+
bounds set by `time_bounds`.
320+
"""
267321
function _build(comp_def::ComponentDef,
268322
var_dict::Dict{ComponentPath, Any},
269323
par_dict::Dict{Tuple{ComponentPath, Symbol}, Any},
@@ -278,6 +332,16 @@ function _build(comp_def::ComponentDef,
278332
return LeafComponentInstance(comp_def, vars, pars, time_bounds)
279333
end
280334

335+
"""
336+
_build(comp_def::AbstractCompositeComponentDef,
337+
var_dict::Dict{ComponentPath, Any},
338+
par_dict::Dict{Tuple{ComponentPath, Symbol}, Any},
339+
time_bounds::Tuple{Int, Int})
340+
341+
Return a built CompositeComponentInstance created using AbstractCompositeComponentDef
342+
`comp_def`, variables and parameters from `var_dict` and `par_dict` and the time
343+
bounds set by `time_bounds`.
344+
"""
281345
function _build(comp_def::AbstractCompositeComponentDef,
282346
var_dict::Dict{ComponentPath, Any},
283347
par_dict::Dict{Tuple{ComponentPath, Symbol}, Any},
@@ -318,6 +382,11 @@ function _get_parameters(comp_def::AbstractCompositeComponentDef)
318382
return parameters
319383
end
320384

385+
"""
386+
_build(md::ModelDef)
387+
388+
Build ModelDef `md` (lowest build function called by `build(md::ModelDef)`) and return the ModelInstance..
389+
"""
321390
function _build(md::ModelDef)
322391

323392
# @info "_build(md)"
@@ -347,13 +416,23 @@ function _build(md::ModelDef)
347416
return mi
348417
end
349418

419+
"""
420+
build(m::Model)
421+
422+
Build Model `m` and return the ModelInstance.
423+
"""
350424
function build(m::Model)
351425
# Reference a copy in the ModelInstance to avoid changes underfoot
352426
md = deepcopy(m.md)
353427
mi = _build(md)
354428
return mi
355429
end
356430

431+
"""
432+
build!(m::Model)
433+
434+
Build Model `m` for and dirty `m`'s ModelDef.
435+
"""
357436
function build!(m::Model)
358437
m.mi = build(m)
359438
m.md.dirty = false
@@ -384,6 +463,11 @@ function Base.run(mm::MarginalModel; ntimesteps::Int=typemax(Int))
384463
run(mm.modified, ntimesteps=ntimesteps)
385464
end
386465

466+
"""
467+
build!(mm::MarginalModel)
468+
469+
Build MarginalModel `mm` by building both its `base` and `modified models`.
470+
"""
387471
function build!(mm::MarginalModel)
388472
build!(mm.base)
389473
build!(mm.modified)

src/core/connections.jl

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ end
4949
# Default string, string unit check function
5050
verify_units(unit1::AbstractString, unit2::AbstractString) = (unit1 == unit2)
5151

52+
"""
53+
_check_labels(obj::AbstractCompositeComponentDef,
54+
comp_def::AbstractComponentDef, param_name::Symbol,
55+
mod_param::ArrayModelParameter)
56+
57+
Check that the labels of the ArrayModelParameter `mod_param` match the labels
58+
of the model parameter `param_name` in component `comp_def` of object `obj`,
59+
including datatype and dimensions.
60+
"""
5261
function _check_labels(obj::AbstractCompositeComponentDef,
5362
comp_def::AbstractComponentDef, param_name::Symbol, mod_param::ArrayModelParameter)
5463
param_def = parameter(comp_def, param_name)
@@ -100,6 +109,13 @@ function connect_param!(obj::AbstractCompositeComponentDef, comp_name::Symbol,
100109
connect_param!(obj, comp_def, param_name, model_param_name, check_labels=check_labels)
101110
end
102111

112+
"""
113+
connect_param!(obj::AbstractCompositeComponentDef, comp_def::AbstractComponentDef,
114+
param_name::Symbol, model_param_name::Symbol; check_labels::Bool=true)
115+
116+
Connect a parameter `param_name` in the component `comp_def` of composite `obj` to
117+
the model parameter `model_param_name`.
118+
"""
103119
function connect_param!(obj::AbstractCompositeComponentDef, comp_def::AbstractComponentDef,
104120
param_name::Symbol, model_param_name::Symbol; check_labels::Bool=true)
105121
mod_param = model_param(obj, model_param_name)
@@ -232,6 +248,23 @@ Try calling:
232248
return nothing
233249
end
234250

251+
"""
252+
connect_param!(obj::AbstractCompositeComponentDef,
253+
dst_comp_name::Symbol, dst_par_name::Symbol,
254+
src_comp_name::Symbol, src_var_name::Symbol,
255+
backup::Union{Nothing, Array}=nothing; ignoreunits::Bool=false,
256+
backup_offset::Union{Nothing, Int} = nothing)
257+
258+
Bind the parameter `dst_par_name` of one component `dst_comp_name` of composite `obj` to a
259+
variable `src_var_name` in another component `src_comp_name` of the same model using
260+
`backup` to provide default values and the `ignoreunits` flag to indicate the need to
261+
check match units between the two. The `backup_offset` argument, which is only valid
262+
when `backup` data has been set, indicates that the backup data should be used for
263+
a specified number of timesteps after the source component begins. ie. the value would be
264+
`1` if the destination component parameter should only use the source component
265+
data for the second timestep and beyond.
266+
"""
267+
235268
function connect_param!(obj::AbstractCompositeComponentDef,
236269
dst_comp_name::Symbol, dst_par_name::Symbol,
237270
src_comp_name::Symbol, src_var_name::Symbol,
@@ -368,9 +401,9 @@ function unconnected_params(obj::AbstractCompositeComponentDef)
368401
end
369402

370403
"""
371-
set_leftover_params!(m::Model, parameters::Dict)
404+
set_leftover_params!(md::ModelDef, parameters::Dict{T, Any})
372405
373-
Set all of the parameters in model `m` that don't have a value and are not connected
406+
Set all of the parameters in `ModelDef` `md` that don't have a value and are not connected
374407
to some other component to a value from a dictionary `parameters`. This method assumes
375408
the dictionary keys are strings that match the names of unset parameters in the model,
376409
and all resulting new model parameters will be shared parameters.
@@ -644,8 +677,12 @@ function update_param!(md::ModelDef, comp_name::Symbol, param_name::Symbol, valu
644677

645678
end
646679

647-
function _update_param!(obj::AbstractCompositeComponentDef,
648-
name::Symbol, value)
680+
"""
681+
_update_param!(obj::AbstractCompositeComponentDef, name::Symbol, value)
682+
683+
Update the `value` of the model parameter `name` in Model Def `md`.
684+
"""
685+
function _update_param!(obj::AbstractCompositeComponentDef, name::Symbol, value)
649686
param = model_param(obj, name, missing_ok=true)
650687
if param === nothing
651688
error("Cannot update parameter; $name not found in composite's model parameters.")
@@ -660,6 +697,11 @@ function _update_param!(obj::AbstractCompositeComponentDef,
660697
dirty!(obj)
661698
end
662699

700+
"""
701+
_update_scalar_param!(param::ScalarModelParameter, name, value)
702+
703+
Update the `value` of the scalar model parameter `param`.
704+
"""
663705
function _update_scalar_param!(param::ScalarModelParameter, name, value)
664706
if ! (value isa typeof(param.value))
665707
try
@@ -672,6 +714,11 @@ function _update_scalar_param!(param::ScalarModelParameter, name, value)
672714
nothing
673715
end
674716

717+
"""
718+
_update_array_param!(obj::AbstractCompositeComponentDef, name, value)
719+
720+
Update the `value` of the array model parameter `name` in object `obj`.
721+
"""
675722
function _update_array_param!(obj::AbstractCompositeComponentDef, name, value)
676723

677724
# Get original parameter
@@ -735,6 +782,11 @@ function update_params!(obj::AbstractCompositeComponentDef, parameters::Dict; up
735782
nothing
736783
end
737784

785+
"""
786+
add_connector_comps!(obj::AbstractCompositeComponentDef)
787+
788+
Add all the needed Mimi connector components to object `obj`.
789+
"""
738790
function add_connector_comps!(obj::AbstractCompositeComponentDef)
739791
conns = internal_param_conns(obj)
740792
i = 1 # counter to track the number of connector comps added

src/core/defcomp.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ macro defcomp(comp_name, ex)
200200
continue
201201
end
202202

203-
# DEPRECATION - EVENTUALLY REMOVE
203+
# DEPRECATION
204204
if @capture(elt, name_::datum_type_ = elt_type_(args__))
205205
error("The following syntax has been deprecated in @defcomp: \"$name::$datum_type = $elt_type(...)\". Use curly bracket syntax instead: \"$name = $elt_type{$datum_type}(...)\"")
206206
elseif ! @capture(elt, name_ = (elt_type_{datum_type_}(args__) | elt_type_(args__)))

0 commit comments

Comments
 (0)