From 73897d5fc29aea83055d09a3a3bfaa53267cd676 Mon Sep 17 00:00:00 2001 From: Chris Campbell Date: Tue, 13 Jul 2021 13:12:02 -0400 Subject: [PATCH 1/2] fix: correct initialization of 2D arrays to allow dimensions with matching subscript names --- models/arrays_cname/arrays_cname.dat | 18 ++++++++++++ models/arrays_cname/arrays_cname.mdl | 9 ++++++ models/arrays_cname/arrays_cname_spec.json | 32 +++++++++++++++++++++- src/EquationGen.js | 23 ++++++++-------- 4 files changed, 69 insertions(+), 13 deletions(-) diff --git a/models/arrays_cname/arrays_cname.dat b/models/arrays_cname/arrays_cname.dat index aac7ccf6..8aa91ace 100644 --- a/models/arrays_cname/arrays_cname.dat +++ b/models/arrays_cname/arrays_cname.dat @@ -341,6 +341,24 @@ s3iii[A1,B1,C1] SAVEPER 0 1 1 1 +sc[C1,C1] +0 11 +sc[C1,C2] +0 12 +sc[C1,C3] +0 13 +sc[C2,C1] +0 21 +sc[C2,C2] +0 22 +sc[C2,C3] +0 23 +sc[C3,C1] +0 31 +sc[C3,C2] +0 32 +sc[C3,C3] +0 33 t[A2] 0 1 t[A3] diff --git a/models/arrays_cname/arrays_cname.mdl b/models/arrays_cname/arrays_cname.mdl index f387533b..5427444e 100755 --- a/models/arrays_cname/arrays_cname.mdl +++ b/models/arrays_cname/arrays_cname.mdl @@ -2,6 +2,7 @@ DimA: A1, A2, A3 -> DimB ~~| DimB: B1, B2, B3 ~~| DimC: C1, C2, C3 ~~| +DimC': DimC ~~| DimD: D1, D2, D3, D4 ~~| SubA: A2, A3 ~~| DimX: SubA, A1 ~~| @@ -93,6 +94,14 @@ s[DimA,DimD]= ~ 2D constant array | +sc[DimC,DimC']= + 11,12,13; + 21,22,23; + 31,32,33; + ~ + ~ 2D constant array where dimensions have matching subscript names + | + s1d[DimA] = 1 ~~| s1i[A1] = 1 ~~| diff --git a/models/arrays_cname/arrays_cname_spec.json b/models/arrays_cname/arrays_cname_spec.json index 3d9f08a9..6270cc30 100644 --- a/models/arrays_cname/arrays_cname_spec.json +++ b/models/arrays_cname/arrays_cname_spec.json @@ -1,5 +1,35 @@ { "name": "Arrays test model (where the spec file contains C names with numeric subscript indices)", "inputVars": ["_inputa[0]", "_inputa[1]", "_inputa[2]"], - "outputVars": ["_time", "_a[0]", "_a[1]", "_a[2]", "_f[0][0]", "_f[0][1]", "_f[0][2]", "_h"] + "outputVars": [ + "_time", + "_a[0]", + "_a[1]", + "_a[2]", + "_f[0][0]", + "_f[0][1]", + "_f[0][2]", + "_h", + "_s[0][0]", + "_s[0][1]", + "_s[0][2]", + "_s[0][3]", + "_s[1][0]", + "_s[1][1]", + "_s[1][2]", + "_s[1][3]", + "_s[2][0]", + "_s[2][1]", + "_s[2][2]", + "_s[2][3]", + "_sc[0][0]", + "_sc[0][1]", + "_sc[0][2]", + "_sc[1][0]", + "_sc[1][1]", + "_sc[1][2]", + "_sc[2][0]", + "_sc[2][1]", + "_sc[2][2]" + ] } diff --git a/src/EquationGen.js b/src/EquationGen.js index 12040cf5..b9090311 100644 --- a/src/EquationGen.js +++ b/src/EquationGen.js @@ -869,18 +869,17 @@ module.exports = class EquationGen extends ModelReader { modelLHSReader.read(this.var.modelLHS) let cNames = modelLHSReader.names().map(Model.cName) // Visit dims in normal order. Find the ind in the dim. Compose the C array expression with numeric indices. - for (let dim of this.var.separationDims) { - let sepDim = sub(dim) - for (let ind of this.var.subscripts) { - let i = sepDim.value.indexOf(ind) - if (i >= 0) { - let indexNum = sub(ind).value - if (!cVarName) { - cVarName = `${this.var.varName}[${indexNum}]` - } else { - cVarName += `[${indexNum}]` - } - break + for (let i = 0; i < this.var.separationDims.length; i++) { + const dim = this.var.separationDims[i] + const sepDim = sub(dim) + const ind = this.var.subscripts[i] + let j = sepDim.value.indexOf(ind) + if (j >= 0) { + let indexNum = sub(ind).value + if (!cVarName) { + cVarName = `${this.var.varName}[${indexNum}]` + } else { + cVarName += `[${indexNum}]` } } } From 3e850ddd6200f9a4e4c1c2aaaff25194d05740e3 Mon Sep 17 00:00:00 2001 From: Chris Campbell Date: Wed, 4 Aug 2021 15:29:54 -0700 Subject: [PATCH 2/2] fix: change let to const to be consistent with the rest of the block --- src/EquationGen.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EquationGen.js b/src/EquationGen.js index d7beed14..c10c26e1 100644 --- a/src/EquationGen.js +++ b/src/EquationGen.js @@ -962,9 +962,9 @@ export default class EquationGen extends ModelReader { const dim = this.var.separationDims[i] const sepDim = sub(dim) const ind = this.var.subscripts[i] - let j = sepDim.value.indexOf(ind) + const j = sepDim.value.indexOf(ind) if (j >= 0) { - let indexNum = sub(ind).value + const indexNum = sub(ind).value if (!cVarName) { cVarName = `${this.var.varName}[${indexNum}]` } else {