Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix add_connector_comps! bug #646

Closed
lrennels opened this issue Jan 24, 2020 · 1 comment
Closed

Fix add_connector_comps! bug #646

lrennels opened this issue Jan 24, 2020 · 1 comment
Assignees
Labels

Comments

@lrennels
Copy link
Collaborator

lrennels commented Jan 24, 2020

There is a bug in add_connector_comps such that if there are needed connector components in more than one of the comp_def in compdefs(obj), we will try to add ConnectorComp1 twice and trigger an Error. We need to track how many total have been added outside of the loop over all compdefs. A toy example to show this problem (on v0.9.4) is below from https://forum.mimiframework.org/t/error-in-coupling-different-timestep-length-components/111

This all relates to the bigger issue of adding first and last keywords #573

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
    	else
    		v.ch4_conc[t] = v.ch4_conc[t-1] + 0.5 * p.ch4_emiss[t]
    	end
    end
end

# 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
    	else
    		v.co2_conc[t] = v.co2_conc[t-1] + 0.1 * p.co2_emiss[t]
    	end
    end
end

# 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]
    end
end


# 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)

run(m)
@lrennels lrennels self-assigned this Jan 24, 2020
@lrennels lrennels added the bug label Jan 24, 2020
@lrennels lrennels modified the milestones: Triage, v1.0.1 Jan 24, 2020
@corakingdon
Copy link
Collaborator

@lrennels we can close this right?

@lrennels lrennels closed this as completed Feb 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants