Skip to content

Commit

Permalink
feat: implement GET DIRECT LOOKUPS including a test model (#99)
Browse files Browse the repository at this point in the history
Fixes #98
  • Loading branch information
ToddFincannon authored Jul 29, 2021
1 parent 3988e5c commit 47f6fe5
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 4 deletions.
134 changes: 134 additions & 0 deletions models/directlookups/directlookups.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
b[A1]
2020 0
2021 0.033333
2022 0.066667
2023 0.1
2024 0.133333
2025 0.166667
2026 0.2
2027 0.233333
2028 0.266667
2029 0.3
2030 0.333333
2031 0.366667
2032 0.4
2033 0.433333
2034 0.466667
2035 0.5
2036 0.533333
2037 0.566667
2038 0.6
2039 0.633333
2040 0.666667
2041 0.7
2042 0.733333
2043 0.766667
2044 0.8
2045 0.833333
2046 0.866667
2047 0.9
2048 0.933333
2049 0.966667
2050 1
b[A2]
2020 0
2021 0
2022 0.034483
2023 0.068966
2024 0.103448
2025 0.137931
2026 0.172414
2027 0.206897
2028 0.241379
2029 0.275862
2030 0.310345
2031 0.344828
2032 0.37931
2033 0.413793
2034 0.448276
2035 0.482759
2036 0.517241
2037 0.551724
2038 0.586207
2039 0.62069
2040 0.655172
2041 0.689655
2042 0.724138
2043 0.758621
2044 0.793103
2045 0.827586
2046 0.862069
2047 0.896552
2048 0.931034
2049 0.965517
2050 1
b[A3]
2020 0
2021 0.008492
2022 0.011317
2023 0.015069
2024 0.020039
2025 0.026606
2026 0.03525
2027 0.04657
2028 0.0613
2029 0.080307
2030 0.104569
2031 0.135111
2032 0.172899
2033 0.218658
2034 0.272653
2035 0.33445
2036 0.402756
2037 0.475409
2038 0.549591
2039 0.622244
2040 0.69055
2041 0.752347
2042 0.806342
2043 0.852101
2044 0.889889
2045 0.920431
2046 0.944693
2047 0.9637
2048 0.97843
2049 0.98975
2050 0.998394
FINAL TIME
2020 2050
INITIAL TIME
2020 2020
SAVEPER
2020 1
2021 1
2022 1
2023 1
2024 1
2025 1
2026 1
2027 1
2028 1
2029 1
2030 1
2031 1
2032 1
2033 1
2034 1
2035 1
2036 1
2037 1
2038 1
2039 1
2040 1
2041 1
2042 1
2043 1
2044 1
2045 1
2046 1
2047 1
2048 1
2049 1
2050 1
TIME STEP
2020 1
40 changes: 40 additions & 0 deletions models/directlookups/directlookups.mdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{UTF-8}
DimA: A1, A2, A3 ~~|

a[DimA] = GET DIRECT LOOKUPS('lookup_data.csv', ',', '1', 'E2') ~~|
b[DimA] = a[DimA](Time) ~~~:SUPPLEMENTARY|

********************************************************
.Control
********************************************************~
Simulation Control Parameters
|

INITIAL TIME = 2020 ~~|
FINAL TIME = 2050 ~~|
TIME STEP = 1 ~~|
SAVEPER = TIME STEP ~~|

\\\---/// Sketch information - do not modify anything except names
V300 Do not put anything below this section - it will be ignored
*View 1
$0-0-0,0,|0||0-0-0|0-0-0|0-0-0|0-0-0|0-0-0|0,0,100,0
///---\\\
:L<%^E!@
9:directlookups
15:0,0,0,0,0,0
19:100,0
27:2,
34:0,
5:FINAL TIME
35:Date
36:YYYY-MM-DD
37:2000
38:1
39:1
40:2
41:0
42:1
24:0
25:0
26:0
4 changes: 4 additions & 0 deletions models/directlookups/lookup_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Variable,Subscript 1,Subscript 2,Subscript 3,2020,2021,2022,2023,2024,2025,2026,2027,2028,2029,2030,2031,2032,2033,2034,2035,2036,2037,2038,2039,2040,2041,2042,2043,2044,2045,2046,2047,2048,2049,2050
a,A1,,,0,0.033333,0.066667,0.1,0.133333,0.166667,0.2,0.233333,0.266667,0.3,0.333333,0.366667,0.4,0.433333,0.466667,0.5,0.533333,0.566667,0.6,0.633333,0.666667,0.7,0.733333,0.766667,0.8,0.833333,0.866667,0.9,0.933333,0.966667,1
a,A2,,,0,0,0.034483,0.068966,0.103448,0.137931,0.172414,0.206897,0.241379,0.275862,0.310345,0.344828,0.37931,0.413793,0.448276,0.482759,0.517241,0.551724,0.586207,0.62069,0.655172,0.689655,0.724138,0.758621,0.793103,0.827586,0.862069,0.896552,0.931034,0.965517,1
a,A3,,,0,0.008492,0.011317,0.015069,0.020039,0.026606,0.03525,0.04657,0.0613,0.080307,0.104569,0.135111,0.172899,0.218658,0.272653,0.33445,0.402756,0.475409,0.549591,0.622244,0.69055,0.752347,0.806342,0.852101,0.889889,0.920431,0.944693,0.9637,0.97843,0.98975,0.998394
2 changes: 1 addition & 1 deletion src/EquationGen.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export default class EquationGen extends ModelReader {
let csvPathname = path.resolve(this.modelDirname, file)
let data = readCsv(csvPathname, tab)
if (data) {
getCellValue = (c, r) => (data[r] != null ? cdbl(data[r][c]) : null)
getCellValue = (c, r) => (data[r] != null && data[r][c] != null ? cdbl(data[r][c]) : null)
}
}
// If the data was found, convert it to a lookup.
Expand Down
4 changes: 2 additions & 2 deletions src/EquationReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default class EquationReader extends ModelReader {
this.var.hasInitValue = true
} else if (fn === '_ACTIVE_INITIAL' || fn === '_SAMPLE_IF_TRUE') {
this.var.hasInitValue = true
} else if (fn === '_GET_DIRECT_DATA') {
} else if (fn === '_GET_DIRECT_DATA' || fn === '_GET_DIRECT_LOOKUPS') {
this.var.varType = 'data'
} else if (fn === '_GET_DIRECT_CONSTANTS') {
this.var.varType = 'const'
Expand Down Expand Up @@ -143,7 +143,7 @@ export default class EquationReader extends ModelReader {
// Generate a level var to expand the DELAY* call.
let args = R.map(expr => expr.getText(), ctx.expr())
this.expandDelayFunction(fn, args)
} else if (fn === '_GET_DIRECT_DATA') {
} else if (fn === '_GET_DIRECT_DATA' || fn === '_GET_DIRECT_LOOKUPS') {
// Extract string constant arguments into an object used in code generation.
// For Excel files, the file argument names an indirect "?" file tag from the model settings.
// For CSV files, it gives a relative pathname in the model directory.
Expand Down
5 changes: 4 additions & 1 deletion src/VariableReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export default class VariableReader extends ModelReader {
}
if (!expand) {
// Direct data vars with subscripts are separated because we generate a lookup for each index.
if (isDimension(subscript) && this.var.modelFormula.includes('GET DIRECT DATA')) {
if (
isDimension(subscript) &&
(this.var.modelFormula.includes('GET DIRECT DATA') || this.var.modelFormula.includes('GET DIRECT LOOKUPS'))
) {
expand = true
}
}
Expand Down

0 comments on commit 47f6fe5

Please sign in to comment.