@@ -466,7 +466,7 @@ export default class EquationGen extends ModelReader {
466466 let lhsIndexSubscripts = cartesianProductOf ( cSubscripts )
467467 // Find the table cell offset for each LHS index tuple.
468468 for ( let indexSubscripts of lhsIndexSubscripts ) {
469- let entry = [ 0 , 0 ]
469+ let entry = [ null , null ]
470470 for ( let i = 0 ; i < this . var . subscripts . length ; i ++ ) {
471471 // LHS dimensions or indices in a separated dimension map to table cells.
472472 let lhsSubscript = this . var . subscripts [ i ]
@@ -476,18 +476,25 @@ export default class EquationGen extends ModelReader {
476476 let ind = sub ( indexSubscript )
477477 // Find the model subscript position corresponding to the LHS index subscript.
478478 for ( let iModelDim = 0 ; iModelDim < modelDimNames . length ; iModelDim ++ ) {
479- let modelDim = sub ( modelDimNames [ iModelDim ] )
480- if ( modelDim . family === ind . family ) {
481- // Set the numeric index for the model dimension in the cell offset entry.
482- // Use the position within the dimension to map subdimensions onto cell offsets.
483- let pos = modelDim . value . indexOf ( indexSubscript )
484- let entryRowOrCol = modelDimNames . length > 1 ? iModelDim : 1
485- entry [ entryRowOrCol ] = pos
486- break
479+ // Only fill an entry position once.
480+ if ( entry [ iModelDim ] === null ) {
481+ let modelDim = sub ( modelDimNames [ iModelDim ] )
482+ if ( modelDim . family === ind . family ) {
483+ // Set the numeric index for the model dimension in the cell offset entry.
484+ // Use the position within the dimension to map subdimensions onto cell offsets.
485+ let pos = modelDim . value . indexOf ( indexSubscript )
486+ // Vectors use a 2D cell offset that maps to columns in the first row.
487+ // Tables use a 2D cell offset with the row or column matching the model dimension.
488+ let entryRowOrCol = modelDimNames . length > 1 ? iModelDim : 1
489+ entry [ entryRowOrCol ] = pos
490+ break
491+ }
487492 }
488493 }
489494 }
490495 }
496+ // Replace unfilled entry positions with zero.
497+ entry = entry . map ( x => ( x === null ? 0 : x ) )
491498 // Read values by column first when the start cell ends with an asterisk.
492499 // Ref: https://www.vensim.com/documentation/fn_get_direct_constants.html
493500 if ( startCell . endsWith ( '*' ) ) {
0 commit comments