Skip to content

Replace lens-based variable lookup system by dictionaries constructed at model initialization #903

@SouthEndMusic

Description

@SouthEndMusic

I'm not a huge fan of the lenses used to lookup vectors in the model structure; some downsides I can think of are:

  • The lenses have to be updated every time something in the model data structure is changed
  • Lookups with these lenses are type unstable

Suggestion

Make a struct that is a new field of the Model struct, like

struct DataLookup{N}
   data_float::Dict{String, Vector{Float64}} = Dict()
   data_int::Dict{String, Vector{Int}} = Dict()
   data_svector::Dict{String, Vector{SVector{N, Float64}}} = Dict()
   ...
end

where the categorization by type makes it type stable. Then at the construction of the model these dictionaries are populated, with a convenience function:

Base.setindex!(data_lookup::DataLookup, standard_name::String, data::Vector{Float64}) = (data_lookup.data_float[standard_name] = data)
Base.setindex!(data_lookup::DataLookup, standard_name::String, data::Vector{Int}) = (data_lookup.data_int[intstandard_name] = data)
Base.setindex!(data_lookup::DataLookup{N}, standard_name::String, data::Vector{SVector{N, Float64}}) where {N} = (data_lookup.data_svector[standard_name] = data)
...

Another advantage of this is that it is very cheaply checked whether the data corresponding to some standard name is part of the model.

Metadata

Metadata

Assignees

No one assigned

    Labels

    developer-experienceDevelopment quality of life improvements

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions