Index errors for custom dimensions

Hi all,

I’m integrating a new set of damage functions into a Monte Carlo run of MimiGIVE, and have run into an error when I define a new dimension for a Parameter (a “gcms” dimensions for outputs from different climate models, alongside the typical “time” and “country”, for example).

When I try to index into a Parameter (e.g., beta) that has this dimension (e.g., p.beta[model, c] for a single model and country), I get an “ArgumentError: invalid index: 1.0 of type Float64.” This when the dimension has just been defined as a list of numbers from 1 to 20, and the index I’m trying to use is just the number 1. I’ve made sure to add the new index as an Index() call at the top of the component I’m defining.

The question arising from this is: Are there restrictions on or problems with defining new dimensions for Parameters? When doing so, are there right or wrong ways to set these dimensions and index into them?

Thank you!

Hi @christophercallahan I assume you are also setting the dimension with set_dimension!(m, :model, 1:20) or something like that? So to recap you have

(1) Added the new index as Index() at the top of the new component (this is optional but a good idea).
(2) Looping over that index within the run_timestep function ie.

for model in d.models
      for c in d.country
            ...
            beta[model, c] = ...
            ...
      end
end

(3) Set the model dimension ie. something like set_dimension!(m, :model, 1:20).

Taking a look at the documentation about the dimension names and set_dimension! here my guess is that you are inadvertently setting the dimension keys to be Float64s instead of Integers, and we do not allow those labels to Float64s (now it’s possible I could add that in without a problem but there might be complications with indexing that are behind why we don’t allow it).

So this works for me:

@defcomp foo begin 

    model = Index()

    x = Parameter(index=[model]) 
    y = Variable(index=[time, model])

    function run_timestep(p,v,d,t)
        for model in d.model
            v.y[t, model] = p.x[model]
        end
    end
end 

m = Model()
set_dimension!(m, :time, 2000:2010)
set_dimension!(m, :model, 1:20)

add_comp!(m, foo)
update_param!(m, :foo, :x, 1:5:100)
run(m)

Now if you try to do

set_dimension!(m, :model, 1.:20.)

which uses Floats, you immediately get an error, so I’m not sure how you’re getting to yours? I"m sure we can sort it though!

Thank you! I had thought I’d accounted for the float vs. int issue when setting the dimension, but I guess not – setting the dimension the way you’ve done it in your example fixes it.

Fixing this issue just revealed others, of course (about to post a separate topic…), but thank you again for your help!