Skip to content

Commit e53d876

Browse files
fix: allow GET DIRECT CONSTANTS to use 2 subscripts in the same family (#144)
Fixes #143
1 parent 44d326e commit e53d876

4 files changed

Lines changed: 36 additions & 9 deletions

File tree

models/directconst/data/g.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
g,C1,C2
2+
C1,11,12
3+
C2,21,22

models/directconst/directconst.dat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ f[C2,A3]
5656
0 23
5757
FINAL TIME
5858
0 1
59+
g[C1,C1]
60+
0 11
61+
g[C1,C2]
62+
0 12
63+
g[C2,C1]
64+
0 21
65+
g[C2,C2]
66+
0 22
5967
INITIAL TIME
6068
0 0
6169
SAVEPER

models/directconst/directconst.mdl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ DimA: A1, A2, A3 ~~|
33
SubA: A2, A3 ~~|
44
DimB: B1, B2, B3 ~~|
55
DimC: C1, C2 ~~|
6+
From DimC: DimC ~~|
7+
To DimC: DimC ~~|
68
DimD: D1, D2 ~~|
79

810
a =
@@ -50,6 +52,13 @@ f[DimC, SubA] =
5052
f[DimC, DimA] :EXCEPT: [DimC, SubA] = 0
5153
~~~:SUPPLEMENTARY|
5254

55+
g[From DimC, To DimC] =
56+
GET DIRECT CONSTANTS(
57+
'data/g.csv',
58+
',',
59+
'B2'
60+
) ~~~:SUPPLEMENTARY|
61+
5362
********************************************************
5463
.Control
5564
********************************************************~

src/EquationGen.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)