Mimi Framework

Error in Coupling Different Timestep Length Components

I have a case where I run a climate model using historic emissions, and then in 2015 it switches on to use emissions calculated by an IAM component. When I couple these together (so the shorter IAM emissions component provides inputs to the longer climate components starting in 2015), I get the following error: ERROR: Cannot add two components of the same name (ConnectorComp1). As far as I can tell, there are no components with the same name.

I attached a toy model that reproduces the error below. If you replace one of the connect_param!(...) lines with set_param(...) and just provide some exogenous data, it will run fine (i.e. it only errors when both connections are created).

using Mimi

# A longer CH4 cycle component that runs for 20 periods.
@defcomp ch4_cycle begin
	ch4_emiss = Parameter(index=[time])
	ch4_conc  = Variable(index=[time])

	function run_timestep(p, v, d, t)
    	if is_first(t)
    		v.ch4_conc[t] = 720.0
    		v.ch4_conc[t] = v.ch4_conc[t-1] + 0.5 * p.ch4_emiss[t]

# A longer CO2 cycle component that runs for 20 periods.
@defcomp co2_cycle begin
	co2_emiss = Parameter(index=[time])
	co2_conc  = Variable(index=[time])

	function run_timestep(p, v, d, t)
    	if is_first(t)
    		v.co2_conc[t] = 278.0
    		v.co2_conc[t] = v.co2_conc[t-1] + 0.1 * p.co2_emiss[t]

# A shorter emissions component that switches on in period 15.
@defcomp emissions begin
	gdp	      = Parameter(index=[time])
	co2_emiss = Variable(index=[time])
	ch4_emiss  = Variable(index=[time])

	function run_timestep(p, v, d, t)
		v.co2_emiss[t] = 0.25 * p.gdp[t]
		v.ch4_emiss[t] = 0.1 * p.gdp[t]

# Create the model for 20 time steps.
m = Model()
set_dimension!(m, :time, 20)

# Add components (emissions turn on in period 15).
add_comp!(m, emissions; first=15)
add_comp!(m, co2_cycle)
add_comp!(m, ch4_cycle)

# Set exogenous gdp scenario for periods 15-20 in emissions component.
set_param!(m, :emissions, :gdp, ones(6) .* 100)

# Create component connections (with backup CO2 and CH4 emissions data for periods 1-15)
backup_ch4_data = ones(20) .* 15
backup_co2_data = ones(20) .* 5

connect_param!(m, :ch4_cycle => :ch4_emiss, :emissions => :ch4_emiss, backup_ch4_data)
connect_param!(m, :co2_cycle => :co2_emiss, :emissions => :co2_emiss, backup_co2_data)


@FrankErrickson can you tell me what version of Mimi you’re using just so I make sure I’m replicating and looking into things correctly? You can find it with

pkg> st

and just copy the entire result in this thread.

@lrennels [e4e893b0] Mimi v0.9.4

1 Like

Great I’ll look into this ASAP. I think it’s an internal bug that needs to be fixed so I’m glad you caught it!

This definitely seems like a bug in Mimi to me! ConnectorComps get added under the hood by Mimi when you connect a longer component to a shorter component and provide backup data. I thought it was supposed to append unique numbers (1,2,etc.) to the “name” of each ConnectorComp that gets added, but it looks like that got broken at some point! I will look into this.

@ckingdon95 we have a function

connector_comp_name(i::Int) = Symbol("ConnectorComp$I")

that then often gets implemented in a loop containing a line like

conn_comp = compdef(comp_def, connector_comp_name(i))

but obviously this isn’t going quite right because we are ending up in a situation with two of the same name. If you search connector_comp_name in the code base you’ll be able to narrow things down a bit. I’m looking into it now but just an FYI.

@ckingdon95 I’ve got it, let me check on Mimi how we want to handle patching v0.9.4 given we disabled this on master and I’ll check back here after.

@lrennels Great, thanks for finding the bug so quickly! Do you have a sense of when this will be fixed (and if it will be a while, is there a workaround I could do on my end)? I was doing these runs for a paper that we were going to submit soon. But if the timestep thing isn’t working I’ll have to hack up the models to force them to all start at the same time (which I was hoping to avoid). Thanks again for all the help!

@FrankErrickson I’m just asking @davidanthoff how he wants to handle patching this old version and will let you know asap. Basically we may redesign this functionality a bit so were going to wait so as to keep things simple for you, but if this is for work that needs to happen quickly I’d suggest I write you a quick patch for now.

@lrennels Ok thanks! And yes, I would be needing this pretty quickly. So if a quick patch is not a lot of effort on your part, that’d be great. But if it’s a pain, or a more involved Mimi design issue then don’t worry about it.

Hi @FrankErrickson I added a patch to v0.9.4 for you that I think will work, why don’t you try out the conncomps branch with the following line in your terminal.

add Mimi#conncomps

1 Like

@lrennels Yup, that fixed it. Thanks so much for all the help!

@FrankErrickson good! We’re going to make v0.9.5 release for this so you can go back to just using the latest tagged version instead of the branch, I’ll let you know when that comes out.

@FrankErrickson ok we released v0.9.5 so you can do

pkg> free Mimi
pkg> up Mimi