-
Notifications
You must be signed in to change notification settings - Fork 22
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
Incorrect code generated when multiple subscripts resolve to same family #278
Comments
This bug is fixed by the new implementation of the I added the following test model in Prior to the fix, this test would fail on the lines marked "BUG". After the fix, the correct refIds are generated and the test passes. describe('when LHS is NON-apply-to-all (3D)', () => {
// ...
// This test is based on the example from #278
it('should work when RHS variable is NON-apply-to-all (2D) and is accessed with 2 different dimensions from LHS that map to the same family', () => {
const vars = readInlineModel(`
Scenario: S1, S2 ~~|
Sector: A1, A2, A3 ~~|
Supplying Sector: A1, A2 -> Producing Sector ~~|
Producing Sector: A1, A2 -> Supplying Sector ~~|
x[A1,A1] = 101 ~~|
x[A1,A2] = 102 ~~|
x[A1,A3] = 103 ~~|
x[A2,A1] = 201 ~~|
x[A2,A2] = 202 ~~|
x[A2,A3] = 203 ~~|
x[A3,A1] = 301 ~~|
x[A3,A2] = 302 ~~|
x[A3,A3] = 303 ~~|
y[S1] = 1000 ~~|
y[S2] = 2000 ~~|
z[Scenario, Supplying Sector, Producing Sector] =
y[Scenario] + x[Supplying Sector, Producing Sector]
~~|
`)
expect(vars).toEqual([
// refIdsForRhsVarRef(_z[_scenario,_a1,_a1], '_x', ['_supplying_sector', '_producing_sector'])
// -> ['_x[_a1,_a1]']
v('z[Scenario,Supplying Sector,Producing Sector]', 'y[Scenario]+x[Supplying Sector,Producing Sector]', {
refId: '_z[_scenario,_a1,_a1]',
subscripts: ['_scenario', '_a1', '_a1'],
separationDims: ['_supplying_sector', '_producing_sector'],
references: ['_y[_s1]', '_y[_s2]', '_x[_a1,_a1]'],
varType: 'aux'
}),
// refIdsForRhsVarRef(_z[_scenario,_a1,_a2], '_x', ['_supplying_sector', '_producing_sector'])
// -> ['_x[_a1,_a2]']
v('z[Scenario,Supplying Sector,Producing Sector]', 'y[Scenario]+x[Supplying Sector,Producing Sector]', {
refId: '_z[_scenario,_a1,_a2]',
subscripts: ['_scenario', '_a1', '_a2'],
separationDims: ['_supplying_sector', '_producing_sector'],
references: ['_y[_s1]', '_y[_s2]', '_x[_a1,_a2]'], // BUG: Actual value for last element is '_x[a1,_a1]'
varType: 'aux'
}),
// refIdsForRhsVarRef(_z[_scenario,_a2,_a1], '_x', ['_supplying_sector', '_producing_sector'])
// -> ['_x[_a2,_a1]']
v('z[Scenario,Supplying Sector,Producing Sector]', 'y[Scenario]+x[Supplying Sector,Producing Sector]', {
refId: '_z[_scenario,_a2,_a1]',
subscripts: ['_scenario', '_a2', '_a1'],
separationDims: ['_supplying_sector', '_producing_sector'],
references: ['_y[_s1]', '_y[_s2]', '_x[_a2,_a1]'], // BUG: Actual value for last element is '_x[a2,_a2]'
varType: 'aux'
}),
// refIdsForRhsVarRef(_z[_scenario,_a2,_a2], '_x', ['_supplying_sector', '_producing_sector'])
// -> ['_x[_a2,_a2]']
v('z[Scenario,Supplying Sector,Producing Sector]', 'y[Scenario]+x[Supplying Sector,Producing Sector]', {
refId: '_z[_scenario,_a2,_a2]',
subscripts: ['_scenario', '_a2', '_a2'],
separationDims: ['_supplying_sector', '_producing_sector'],
references: ['_y[_s1]', '_y[_s2]', '_x[_a2,_a2]'],
varType: 'aux'
})
]) |
The fixes in However, there is still a bug on the code gen side (related to #179) that causes the newly added test to fail in Until we fix that issue, the following test will fail on the lines marked "BUG". After the fix, the correct code should be generated (with correct array indices) and the test should pass. describe('when LHS is NON-apply-to-all (2D)', () => {
// ...
// This test is based on the example from #278
it('should work when RHS variable is NON-apply-to-all (2D) and is accessed with 2 different dimensions from LHS that map to the same family', () => {
const vars = readInlineModel(`
Scenario: S1, S2 ~~|
Sector: A1, A2, A3 ~~|
Supplying Sector: A1, A2 -> Producing Sector ~~|
Producing Sector: A1, A2 -> Supplying Sector ~~|
x[A1,A1] = 101 ~~|
x[A1,A2] = 102 ~~|
x[A1,A3] = 103 ~~|
x[A2,A1] = 201 ~~|
x[A2,A2] = 202 ~~|
x[A2,A3] = 203 ~~|
x[A3,A1] = 301 ~~|
x[A3,A2] = 302 ~~|
x[A3,A3] = 303 ~~|
y[S1] = 1000 ~~|
y[S2] = 2000 ~~|
z[Scenario, Supplying Sector, Producing Sector] =
y[Scenario] + x[Supplying Sector, Producing Sector]
~~|
`)
expect(vars.size).toBe(15)
// ...
expect(genC(vars.get('_z[_scenario,_a1,_a1]'))).toEqual([
'for (size_t i = 0; i < 2; i++) {',
'_z[i][0][0] = _y[i] + _x[0][0];',
'}'
])
expect(genC(vars.get('_z[_scenario,_a1,_a2]'))).toEqual([
'for (size_t i = 0; i < 2; i++) {',
'_z[i][0][1] = _y[i] + _x[0][1];', // BUG: RHS is generated as `_x[0][0]`
'}'
])
expect(genC(vars.get('_z[_scenario,_a2,_a1]'))).toEqual([
'for (size_t i = 0; i < 2; i++) {',
'_z[i][1][0] = _y[i] + _x[1][0];', // BUG: RHS is generated as `_x[1][1]`
'}'
])
expect(genC(vars.get('_z[_scenario,_a2,_a2]'))).toEqual([
'for (size_t i = 0; i < 2; i++) {',
'_z[i][1][1] = _y[i] + _x[1][1];',
'}'
])
})
}) |
In addition to the new unit tests described above, I added a new sample model under This issue is effectively resolved by the same fixes that are described in my evaluation for #179. I will submit a PR for this branch shortly and will mark both issues as being "fixed" by that PR. |
We have a model that has two dimensions with different names that resolve to the same subscript family. Currently SDE produces incorrect results for equations that involve these dimensions when there are 3 subscripts involved on the LHS.
SDE has existing code to deal with similar cases involving 2 subscripts, see
compile/src/_shared/subscript.js
, but that code as written only handles the case where the LHS has 2 subscripts and the RHS has 2 subscripts. We need to generalize that code so that it works with >= 2 subscripts on either LHS or RHS.Here's an isolated test case that demonstrates the issue. I will push this to a branch under
models/subfamilies
.This produces the following differences (compounded by the confusion that occurs when comparing variables that have multiple instances of the same subscript/dimension, as noted in the part about
normalizeSubscripts
in the evaluation of #179):This is similar to #179, but as far as I can tell, there are different underlying issues. (My proposed fix for this issue doesn't seem to help with #179.)
The text was updated successfully, but these errors were encountered: