Skip to content

Commit a032779

Browse files
fix: correct handling of lookups defined by GET DIRECT LOOKUPS when used as function call arguments (#379)
Fixes #378
1 parent 9d557e0 commit a032779

3 files changed

Lines changed: 224 additions & 78 deletions

File tree

models/directlookups/directlookups.dat

Lines changed: 161 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
b[A1]
1+
b
22
2020 0
33
2021 0.033333
44
2022 0.066667
@@ -30,72 +30,168 @@ b[A1]
3030
2048 0.933333
3131
2049 0.966667
3232
2050 1
33-
b[A2]
34-
2020 0
35-
2021 0
36-
2022 0.034483
37-
2023 0.068966
38-
2024 0.103448
39-
2025 0.137931
40-
2026 0.172414
41-
2027 0.206897
42-
2028 0.241379
43-
2029 0.275862
44-
2030 0.310345
45-
2031 0.344828
46-
2032 0.37931
47-
2033 0.413793
48-
2034 0.448276
49-
2035 0.482759
50-
2036 0.517241
51-
2037 0.551724
52-
2038 0.586207
53-
2039 0.62069
54-
2040 0.655172
55-
2041 0.689655
56-
2042 0.724138
57-
2043 0.758621
58-
2044 0.793103
59-
2045 0.827586
60-
2046 0.862069
61-
2047 0.896552
62-
2048 0.931034
63-
2049 0.965517
64-
2050 1
65-
b[A3]
66-
2020 0
67-
2021 0.008492
68-
2022 0.011317
69-
2023 0.015069
70-
2024 0.020039
71-
2025 0.026606
72-
2026 0.03525
73-
2027 0.04657
74-
2028 0.0613
75-
2029 0.080307
76-
2030 0.104569
77-
2031 0.135111
78-
2032 0.172899
79-
2033 0.218658
80-
2034 0.272653
81-
2035 0.33445
82-
2036 0.402756
83-
2037 0.475409
84-
2038 0.549591
85-
2039 0.622244
86-
2040 0.69055
87-
2041 0.752347
88-
2042 0.806342
89-
2043 0.852101
90-
2044 0.889889
91-
2045 0.920431
92-
2046 0.944693
93-
2047 0.9637
94-
2048 0.97843
95-
2049 0.98975
96-
2050 0.998394
33+
c
34+
2020 2035
35+
2021 2035
36+
2022 2035
37+
2023 2035
38+
2024 2035
39+
2025 2035
40+
2026 2035
41+
2027 2035
42+
2028 2035
43+
2029 2035
44+
2030 2035
45+
2031 2035
46+
2032 2035
47+
2033 2035
48+
2034 2035
49+
2035 2035
50+
2036 2035
51+
2037 2035
52+
2038 2035
53+
2039 2035
54+
2040 2035
55+
2041 2035
56+
2042 2035
57+
2043 2035
58+
2044 2035
59+
2045 2035
60+
2046 2035
61+
2047 2035
62+
2048 2035
63+
2049 2035
64+
2050 2035
65+
d
66+
2020 0.3
67+
2021 0.3
68+
2022 0.3
69+
2023 0.3
70+
2024 0.3
71+
2025 0.3
72+
2026 0.3
73+
2027 0.3
74+
2028 0.3
75+
2029 0.3
76+
2030 0.3
77+
2031 0.3
78+
2032 0.3
79+
2033 0.3
80+
2034 0.3
81+
2035 0.3
82+
2036 0.3
83+
2037 0.3
84+
2038 0.3
85+
2039 0.3
86+
2040 0.3
87+
2041 0.3
88+
2042 0.3
89+
2043 0.3
90+
2044 0.3
91+
2045 0.3
92+
2046 0.3
93+
2047 0.3
94+
2048 0.3
95+
2049 0.3
96+
2050 0.3
97+
e
98+
2020 0.266667
99+
2021 0.266667
100+
2022 0.266667
101+
2023 0.266667
102+
2024 0.266667
103+
2025 0.266667
104+
2026 0.266667
105+
2027 0.266667
106+
2028 0.266667
107+
2029 0.266667
108+
2030 0.266667
109+
2031 0.266667
110+
2032 0.266667
111+
2033 0.266667
112+
2034 0.266667
113+
2035 0.266667
114+
2036 0.266667
115+
2037 0.266667
116+
2038 0.266667
117+
2039 0.266667
118+
2040 0.266667
119+
2041 0.266667
120+
2042 0.266667
121+
2043 0.266667
122+
2044 0.266667
123+
2045 0.266667
124+
2046 0.266667
125+
2047 0.266667
126+
2048 0.266667
127+
2049 0.266667
128+
2050 0.266667
129+
f
130+
2020 0.269999
131+
2021 0.269999
132+
2022 0.269999
133+
2023 0.269999
134+
2024 0.269999
135+
2025 0.269999
136+
2026 0.269999
137+
2027 0.269999
138+
2028 0.269999
139+
2029 0.269999
140+
2030 0.269999
141+
2031 0.269999
142+
2032 0.269999
143+
2033 0.269999
144+
2034 0.269999
145+
2035 0.269999
146+
2036 0.269999
147+
2037 0.269999
148+
2038 0.269999
149+
2039 0.269999
150+
2040 0.269999
151+
2041 0.269999
152+
2042 0.269999
153+
2043 0.269999
154+
2044 0.269999
155+
2045 0.269999
156+
2046 0.269999
157+
2047 0.269999
158+
2048 0.269999
159+
2049 0.269999
160+
2050 0.269999
97161
FINAL TIME
98162
2020 2050
163+
h
164+
2020 1
165+
2021 1
166+
2022 1
167+
2023 1
168+
2024 1
169+
2025 1
170+
2026 1
171+
2027 1
172+
2028 1
173+
2029 1
174+
2030 1
175+
2031 1
176+
2032 1
177+
2033 1
178+
2034 1
179+
2035 1
180+
2036 1
181+
2037 1
182+
2038 1
183+
2039 1
184+
2040 1
185+
2041 1
186+
2042 1
187+
2043 1
188+
2044 1
189+
2045 1
190+
2046 1
191+
2047 1
192+
2048 1
193+
2049 1
194+
2050 1
99195
INITIAL TIME
100196
2020 2020
101197
SAVEPER

models/directlookups/directlookups.mdl

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,45 @@
11
{UTF-8}
22
DimA: A1, A2, A3 ~~|
33

4-
a[DimA] = GET DIRECT LOOKUPS('lookup_data.csv', ',', '1', 'E2') ~~|
5-
b[DimA] = a[DimA](Time) ~~~:SUPPLEMENTARY|
4+
a[DimA] = GET DIRECT LOOKUPS('lookup_data.csv', ',', '1', 'E2')
5+
~
6+
~ subscript currently required for GET DIRECT LOOKUPS
7+
|
8+
9+
b = a[A1](Time)
10+
~
11+
~ access the lookup
12+
|
13+
14+
c = LOOKUP INVERT(a[A1], 0.5)
15+
~
16+
~ look up y for an x value (returns 2035)
17+
|
18+
19+
d = LOOKUP FORWARD(a[A1], 2028.1)
20+
~
21+
~ look up using the next x without interpolation (returns 0.3)
22+
|
23+
24+
e = LOOKUP FORWARD(a[A1], 2028)
25+
~
26+
~ no effect when the x value exists in the lookup (returns 0.266667)
27+
|
28+
29+
f = a[A1](2028.1)
30+
~
31+
~ regular lookups extrapolate by default
32+
|
33+
34+
g((0,0),(1,1),(2,2))
35+
~
36+
~ lookup definition
37+
|
38+
39+
h = LOOKUP FORWARD(g, 1)
40+
~
41+
~ non-subscripted look up
42+
|
643

744
********************************************************
845
.Control

packages/compile/src/generate/equation-gen.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -983,8 +983,9 @@ export default class EquationGen extends ModelReader {
983983
// Push the var name on the stack and then emit it.
984984
let id = ctx.Id().getText()
985985
let varName = canonicalName(id)
986+
let functionName = this.currentFunctionName()
986987
if (isDimension(varName)) {
987-
if (this.currentFunctionName() === '_ELMCOUNT') {
988+
if (functionName === '_ELMCOUNT') {
988989
// Emit the size of the dimension in place of the dimension name.
989990
this.emit(`${sub(varName).size}`)
990991
} else {
@@ -997,8 +998,8 @@ export default class EquationGen extends ModelReader {
997998
}
998999
} else {
9991000
this.varNames.push(varName)
1000-
if (this.currentFunctionName() === '_VECTOR_SELECT') {
1001-
let argIndex = this.argIndexForFunctionName('_VECTOR_SELECT')
1001+
if (functionName === '_VECTOR_SELECT') {
1002+
let argIndex = this.argIndexForFunctionName(functionName)
10021003
if (argIndex === 0) {
10031004
this.vsSelectionArray = this.currentVarName()
10041005
super.visitVar(ctx)
@@ -1007,30 +1008,42 @@ export default class EquationGen extends ModelReader {
10071008
} else {
10081009
super.visitVar(ctx)
10091010
}
1010-
} else if (this.currentFunctionName() === '_VECTOR_ELM_MAP') {
1011-
if (this.argIndexForFunctionName('_VECTOR_ELM_MAP') === 1) {
1011+
} else if (functionName === '_VECTOR_ELM_MAP') {
1012+
if (this.argIndexForFunctionName(functionName) === 1) {
10121013
this.vemOffset = this.currentVarName()
10131014
}
10141015
super.visitVar(ctx)
1015-
} else if (this.currentFunctionName() === '_VECTOR_SORT_ORDER') {
1016-
if (this.argIndexForFunctionName('_VECTOR_SORT_ORDER') === 0) {
1016+
} else if (functionName === '_VECTOR_SORT_ORDER') {
1017+
if (this.argIndexForFunctionName(functionName) === 0) {
10171018
this.vsoVarName = this.currentVarName()
10181019
this.vsoTmpName = newTmpVarName()
10191020
this.emit(this.vsoTmpName)
10201021
}
10211022
super.visitVar(ctx)
1022-
} else if (this.currentFunctionName() === '_ALLOCATE_AVAILABLE') {
1023-
if (this.argIndexForFunctionName('_ALLOCATE_AVAILABLE') === 0) {
1023+
} else if (functionName === '_ALLOCATE_AVAILABLE') {
1024+
if (this.argIndexForFunctionName(functionName) === 0) {
10241025
this.aaRequestArray = this.currentVarName()
10251026
this.aaTmpName = newTmpVarName()
10261027
this.emit(this.aaTmpName)
1027-
} else if (this.argIndexForFunctionName('_ALLOCATE_AVAILABLE') === 1) {
1028+
} else if (this.argIndexForFunctionName(functionName) === 1) {
10281029
this.aaPriorityArray = this.currentVarName()
10291030
}
10301031
super.visitVar(ctx)
1031-
} else if (this.currentFunctionName() === '_GET_DATA_BETWEEN_TIMES') {
1032+
} else if (functionName === '_GET_DATA_BETWEEN_TIMES') {
10321033
this.emit(this.currentVarName())
10331034
super.visitVar(ctx)
1035+
} else if (
1036+
functionName === '_LOOKUP_FORWARD' ||
1037+
functionName === '_LOOKUP_BACKWARD' ||
1038+
functionName === '_LOOKUP_INVERT'
1039+
) {
1040+
let argIndex = this.argIndexForFunctionName(functionName)
1041+
if (argIndex === 0) {
1042+
this.emit(this.currentVarName())
1043+
super.visitVar(ctx)
1044+
} else {
1045+
emitVar()
1046+
}
10341047
} else {
10351048
emitVar()
10361049
}

0 commit comments

Comments
 (0)