# Best way to get dimension keys within run\_timestep

**URL:** <https://forum.mimiframework.org/t/best-way-to-get-dimension-keys-within-run-timestep/270>\
**Category:** Uncategorized\
**Created:** [December 19, 2023, 7:44pm UTC](https://forum.mimiframework.org/t/best-way-to-get-dimension-keys-within-run-timestep/270 "2023-12-19T19:44:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrising](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/jrising/32/7_2.png) [@jrising](https://forum.mimiframework.org/u/jrising)\
**Post date:** [December 19, 2023, 7:44pm UTC](https://forum.mimiframework.org/t/best-way-to-get-dimension-keys-within-run-timestep/270/1 "2023-12-19T19:44:55Z")

</div>

I would like to get information from the `Model` within `run_timestep` and `init`. In particular, I want to call `dim_keys`. Is this information available in the `d` object? Or what is the accepted way to get model information within these functions?

In the past, I have been set up things up outside of these functions, but I now have a case where the relevant work needs to be done differently for each Monte Carlo. I like to avoid storing the relevant information in a parameter (`regionnames = Parameter{String}()`) since these functions should know what model they are being called on.

---

<div class="post-metadata">

**Author:** ![lrennels](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/lrennels/32/6_2.png) [@lrennels](https://forum.mimiframework.org/u/lrennels)\
**Post date:** [January 31, 2024, 12:23am UTC](https://forum.mimiframework.org/t/best-way-to-get-dimension-keys-within-run-timestep/270/2 "2024-01-31T00:23:34Z")

</div>

Hi @jrising sorry for the long delay on this! This is something that I have been frustrated with as well with the current implementation, in models like MimiGIVE and MimiSSPs we did as you said and stored relevant information in a parameter, but as you suggest this is a bit fragile since if one changes the other roan’t automatically change, necessitating extra checks.

I opened this issue a while ago focused on exactly this ([Add access to time information in the `init` function; Add Dimensions keys to `d` · Issue #948 · mimiframework/Mimi.jl · GitHub](https://github.com/mimiframework/Mimi.jl/issues/948)) and will put this at the top of the todo stack! @davidanthoff let us know if you have any thoughts on this though.

---

<div class="post-metadata">

**Author:** ![lrennels](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/lrennels/32/6_2.png) [@lrennels](https://forum.mimiframework.org/u/lrennels)\
**Post date:** [August 23, 2024, 8:01pm UTC](https://forum.mimiframework.org/t/best-way-to-get-dimension-keys-within-run-timestep/270/3 "2024-08-23T20:01:07Z")

</div>

@jrising I’m going to start working on this now. My goals are to first allow access to dimension information, and second (longer term) to allow indexing directly with the dim keys instead of integers, though that might have some performance issues.

Issue is here: [Add access to time information in the `init` function; Add Dimensions keys to `d` · Issue #948 · mimiframework/Mimi.jl · GitHub](https://github.com/mimiframework/Mimi.jl/issues/948) and will open a PR soon

---

<div class="post-metadata">

**Author:** ![jrising](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/jrising/32/7_2.png) [@jrising](https://forum.mimiframework.org/u/jrising)\
**Post date:** [August 25, 2024, 1:31am UTC](https://forum.mimiframework.org/t/best-way-to-get-dimension-keys-within-run-timestep/270/4 "2024-08-25T01:31:58Z")

</div>

That’s great to hear. An alternative would be to just provide a function like `thismodel()` that can be called in both `init` and `run_timestep`, and returns the model that a given component is being run within. That’s effectively my current solution.

---

<div class="post-metadata">

**Author:** ![lrennels](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/lrennels/32/6_2.png) [@lrennels](https://forum.mimiframework.org/u/lrennels)\
**Post date:** [August 25, 2024, 1:54am UTC](https://forum.mimiframework.org/t/best-way-to-get-dimension-keys-within-run-timestep/270/5 "2024-08-25T01:54:49Z")

</div>

Ah interesting, I like that idea too, gives you access to all the information you could need. Since the `ModelInstance` is being built as you run, I assume it’s really the `ModelDefinition` that you are accessing? I’m curious how you do that, either passing a function from outside or taking advantage of the fact that the component “knows” what model it is running in? If you have some code I can look at I will, since collaborating on solutions is always good.

I don’t want to break any compatibility, so I can’t modify `p` of `(p,v,d,t)` too much but was considering adding keys to the `p` `NamedTuple` like `country_labels` so you’d have a `_labels` version of each dimension — but wasn’t loving that approach.

---

<div class="post-metadata">

**Author:** ![jrising](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/jrising/32/7_2.png) [@jrising](https://forum.mimiframework.org/u/jrising)\
**Post date:** [August 25, 2024, 8:41pm UTC](https://forum.mimiframework.org/t/best-way-to-get-dimension-keys-within-run-timestep/270/6 "2024-08-25T20:41:22Z")

</div>

My current solution is to set the model as a parameter on the component. I use that in a bunch of components of the national-level version of MimiPAGE, since I often need to match up the region and national dimensions. e.g., here:

> <https://github.com/openmodels/MimiPAGE2020.jl/blob/national/src/components/GDP.jl>

A simple option for you would be to have `model` be a preset parameter. It wouldn’t break any code as long as any explicitly defined `model` parameter overrides this implicit one. The `thismodel()` function is the same idea. It’s not a real function and gets translated in the macro parser into the model variable.

---

<div class="post-metadata">

**Author:** ![lrennels](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/lrennels/32/6_2.png) [@lrennels](https://forum.mimiframework.org/u/lrennels)\
**Post date:** [August 26, 2024, 4:58pm UTC](https://forum.mimiframework.org/t/best-way-to-get-dimension-keys-within-run-timestep/270/7 "2024-08-26T16:58:43Z")

</div>

@jrising wonderful thank you! Yes, I like the `thismodel()` idea in the macro, what I realized though is that at the time of parsing `@defcomp` you don’t necessarily have a model yet – in fact you certainly don’t – but I like the idea of a default `thismodel()` function along those lines and we could add `thismodel` as a field in the `ComponentInstance` struct and fill it in upon `build!`ing the model, or even adding the component to a model. So we could have something like `thismodel()` that returns your `ComponentInstance`’s my model` field.

---

<div class="post-metadata">

**Author:** ![jrising](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.mimiframework.org/jrising/32/7_2.png) [@jrising](https://forum.mimiframework.org/u/jrising)\
**Post date:** [August 27, 2024, 6:24pm UTC](https://forum.mimiframework.org/t/best-way-to-get-dimension-keys-within-run-timestep/270/8 "2024-08-27T18:24:52Z")

</div>

That makes sense. There’s probably a broader design question about how to provide this kind of model-scope information within the component-scope, but I don’t have any useful input on that.
