diff --git a/models/directconst/data/g.csv b/models/directconst/data/g.csv new file mode 100644 index 00000000..8a8ee268 --- /dev/null +++ b/models/directconst/data/g.csv @@ -0,0 +1,3 @@ +g,C1,C2 +C1,11,12 +C2,21,22 \ No newline at end of file diff --git a/models/directconst/directconst.dat b/models/directconst/directconst.dat index de9b7f99..dcda9002 100644 --- a/models/directconst/directconst.dat +++ b/models/directconst/directconst.dat @@ -56,6 +56,14 @@ f[C2,A3] 0 23 FINAL TIME 0 1 +g[C1,C1] +0 11 +g[C1,C2] +0 12 +g[C2,C1] +0 21 +g[C2,C2] +0 22 INITIAL TIME 0 0 SAVEPER diff --git a/models/directconst/directconst.mdl b/models/directconst/directconst.mdl index 9b6ef1a4..1cc7d4d2 100644 --- a/models/directconst/directconst.mdl +++ b/models/directconst/directconst.mdl @@ -3,6 +3,8 @@ DimA: A1, A2, A3 ~~| SubA: A2, A3 ~~| DimB: B1, B2, B3 ~~| DimC: C1, C2 ~~| +From DimC: DimC ~~| +To DimC: DimC ~~| DimD: D1, D2 ~~| a = @@ -50,6 +52,13 @@ f[DimC, SubA] = f[DimC, DimA] :EXCEPT: [DimC, SubA] = 0 ~~~:SUPPLEMENTARY| +g[From DimC, To DimC] = + GET DIRECT CONSTANTS( + 'data/g.csv', + ',', + 'B2' + ) ~~~:SUPPLEMENTARY| + ******************************************************** .Control ********************************************************~ diff --git a/src/EquationGen.js b/src/EquationGen.js index 814c1a33..23894036 100644 --- a/src/EquationGen.js +++ b/src/EquationGen.js @@ -466,7 +466,7 @@ export default class EquationGen extends ModelReader { let lhsIndexSubscripts = cartesianProductOf(cSubscripts) // Find the table cell offset for each LHS index tuple. for (let indexSubscripts of lhsIndexSubscripts) { - let entry = [0, 0] + let entry = [null, null] for (let i = 0; i < this.var.subscripts.length; i++) { // LHS dimensions or indices in a separated dimension map to table cells. let lhsSubscript = this.var.subscripts[i] @@ -476,18 +476,25 @@ export default class EquationGen extends ModelReader { let ind = sub(indexSubscript) // Find the model subscript position corresponding to the LHS index subscript. for (let iModelDim = 0; iModelDim < modelDimNames.length; iModelDim++) { - let modelDim = sub(modelDimNames[iModelDim]) - if (modelDim.family === ind.family) { - // Set the numeric index for the model dimension in the cell offset entry. - // Use the position within the dimension to map subdimensions onto cell offsets. - let pos = modelDim.value.indexOf(indexSubscript) - let entryRowOrCol = modelDimNames.length > 1 ? iModelDim : 1 - entry[entryRowOrCol] = pos - break + // Only fill an entry position once. + if (entry[iModelDim] === null) { + let modelDim = sub(modelDimNames[iModelDim]) + if (modelDim.family === ind.family) { + // Set the numeric index for the model dimension in the cell offset entry. + // Use the position within the dimension to map subdimensions onto cell offsets. + let pos = modelDim.value.indexOf(indexSubscript) + // Vectors use a 2D cell offset that maps to columns in the first row. + // Tables use a 2D cell offset with the row or column matching the model dimension. + let entryRowOrCol = modelDimNames.length > 1 ? iModelDim : 1 + entry[entryRowOrCol] = pos + break + } } } } } + // Replace unfilled entry positions with zero. + entry = entry.map(x => (x === null ? 0 : x)) // Read values by column first when the start cell ends with an asterisk. // Ref: https://www.vensim.com/documentation/fn_get_direct_constants.html if (startCell.endsWith('*')) {