Skip to content

Commit

Permalink
interpolate a numeric range inside a subscript list (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
ToddFincannonEI committed Oct 16, 2023
1 parent f63ef09 commit eabe432
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 19 deletions.
10 changes: 10 additions & 0 deletions models/subscript/subscript.dat
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,13 @@ t[C5]
0 1
TIME STEP
0 1
u[C1]
0 1
u[C2]
0 2
u[C3]
0 3
u[C4]
0 4
u[C5]
0 5
6 changes: 6 additions & 0 deletions models/subscript/subscript.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ t[DimC] = 1
~:SUPPLEMENTARY
|

u[C1] = 1 ~~~:SUPPLEMENTARY|
u[C2] = 2 ~~~:SUPPLEMENTARY|
u[C3] = 3 ~~~:SUPPLEMENTARY|
u[C4] = 4 ~~~:SUPPLEMENTARY|
u[C5] = 5 ~~~:SUPPLEMENTARY|

********************************************************
.Control
********************************************************~
Expand Down
69 changes: 69 additions & 0 deletions models/subscript/subscript_subs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
_dima:
{
modelName: 'DimA',
modelValue: [ 'A1', 'A2', 'A3' ],
modelMappings: [],
name: '_dima',
value: [ '_a1', '_a2', '_a3' ],
size: 3,
family: '_dima',
mappings: {}
}

_dimb:
{
modelName: 'DimB',
modelValue: [ 'B1', 'B2', 'B3' ],
modelMappings: [ { toDim: 'DimA', value: [] } ],
name: '_dimb',
value: [ '_b1', '_b2', '_b3' ],
size: 3,
family: '_dimb',
mappings: { _dima: [ '_b1', '_b2', '_b3' ] }
}

_dimc:
{
modelName: 'DimC',
modelValue: [ 'C1', 'C2', 'C3', 'C4', 'C5' ],
modelMappings: [],
name: '_dimc',
value: [ '_c1', '_c2', '_c3', '_c4', '_c5' ],
size: 5,
family: '_dimc',
mappings: {}
}

_a1:
{ name: '_a1', value: 0, size: 1, family: '_dima', mappings: {} }

_a2:
{ name: '_a2', value: 1, size: 1, family: '_dima', mappings: {} }

_a3:
{ name: '_a3', value: 2, size: 1, family: '_dima', mappings: {} }

_b1:
{ name: '_b1', value: 0, size: 1, family: '_dimb', mappings: {} }

_b2:
{ name: '_b2', value: 1, size: 1, family: '_dimb', mappings: {} }

_b3:
{ name: '_b3', value: 2, size: 1, family: '_dimb', mappings: {} }

_c1:
{ name: '_c1', value: 0, size: 1, family: '_dimc', mappings: {} }

_c2:
{ name: '_c2', value: 1, size: 1, family: '_dimc', mappings: {} }

_c3:
{ name: '_c3', value: 2, size: 1, family: '_dimc', mappings: {} }

_c4:
{ name: '_c4', value: 3, size: 1, family: '_dimc', mappings: {} }

_c5:
{ name: '_c5', value: 4, size: 1, family: '_dimc', mappings: {} }

123 changes: 109 additions & 14 deletions models/subscript/subscript_vars.txt
Original file line number Diff line number Diff line change
@@ -1,110 +1,205 @@
a[DimA]: aux
= b[DimB]
refId(_a)
families(_dima)
subscripts(_dima)
hasInitValue(false)
refs(_b[_b1], _b[_b2], _b[_b3])

b[DimB]: const (non-apply-to-all)
= 1,2,3
refId(_b[_b1])
families(_dimb)
subscripts(_b1)
separationDims(_dimb)
hasInitValue(false)

b[DimB]: const (non-apply-to-all)
= 1,2,3
refId(_b[_b2])
families(_dimb)
subscripts(_b2)
separationDims(_dimb)
hasInitValue(false)

b[DimB]: const (non-apply-to-all)
= 1,2,3
refId(_b[_b3])
families(_dimb)
subscripts(_b3)

a[DimA]: aux
= b[DimB]
refId(_a)
families(_dima)
subscripts(_dima)
separationDims(_dimb)
hasInitValue(false)

c[DimB]: aux
= b[DimB]
refId(_c)
families(_dimb)
subscripts(_dimb)
hasInitValue(false)
refs(_b[_b1], _b[_b2], _b[_b3])

d[A1]: aux
= b[B1]
refId(_d)
families(_dima)
subscripts(_a1)
hasInitValue(false)
refs(_b[_b1])

e[B1]: aux
= b[B1]
refId(_e)
families(_dimb)
subscripts(_b1)
hasInitValue(false)
refs(_b[_b1])

f[DimA,B1]: const (non-apply-to-all)
= 1
refId(_f[_dima,_b1])
families(_dima, _dimb)
subscripts(_dima, _b1)
hasInitValue(false)

f[DimA,B2]: const (non-apply-to-all)
= 2
refId(_f[_dima,_b2])
families(_dima, _dimb)
subscripts(_dima, _b2)
hasInitValue(false)

f[DimA,B3]: const (non-apply-to-all)
= 3
refId(_f[_dima,_b3])
families(_dima, _dimb)
subscripts(_dima, _b3)
hasInitValue(false)

FINAL TIME: const
= 1
refId(_final_time)
hasInitValue(false)

g[B1,DimA]: aux (non-apply-to-all)
= f[DimA,B1]
refId(_g[_dima,_b1])
families(_dima, _dimb)
subscripts(_dima, _b1)
hasInitValue(false)
refs(_f[_dima,_b1])

g[B2,DimA]: aux (non-apply-to-all)
= f[DimA,B2]
refId(_g[_dima,_b2])
families(_dima, _dimb)
subscripts(_dima, _b2)
hasInitValue(false)
refs(_f[_dima,_b2])

g[B3,DimA]: aux (non-apply-to-all)
= f[DimA,B3]
refId(_g[_dima,_b3])
families(_dima, _dimb)
subscripts(_dima, _b3)
hasInitValue(false)
refs(_f[_dima,_b3])

INITIAL TIME: const
= 0
refId(_initial_time)
hasInitValue(false)

o[DimA,DimB]: aux
= f[DimA,DimB]
refId(_o)
families(_dima, _dimb)
subscripts(_dima, _dimb)
hasInitValue(false)
refs(_f[_dima,_b1], _f[_dima,_b2], _f[_dima,_b3])

p[DimB,DimA]: aux
= f[DimA,DimB]
refId(_p)
families(_dima, _dimb)
subscripts(_dima, _dimb)
hasInitValue(false)
refs(_f[_dima,_b1], _f[_dima,_b2], _f[_dima,_b3])

FINAL TIME: const
= 1
refId(_final_time)
r[DimA]: aux
= IF THEN ELSE(DimA=Selected A,1,0)
refId(_r)
families(_dima)
subscripts(_dima)
hasInitValue(false)
refs(_selected_a)

INITIAL TIME: const
= 0
refId(_initial_time)
s[DimA]: aux
= DimB
refId(_s)
families(_dima)
subscripts(_dima)
hasInitValue(false)

SAVEPER: aux
= TIME STEP
refId(_saveper)
hasInitValue(false)
refs(_time_step)

TIME STEP: const
Selected A: const
= 2
refId(_selected_a)
hasInitValue(false)

t[DimC]: const
= 1
refId(_time_step)
refId(_t)
families(_dimc)
subscripts(_dimc)
hasInitValue(false)

Time: const
=
refId(_time)
hasInitValue(false)

TIME STEP: const
= 1
refId(_time_step)
hasInitValue(false)

u[C1]: const (non-apply-to-all)
= 1
refId(_u[_c1])
families(_dimc)
subscripts(_c1)
hasInitValue(false)

u[C2]: const (non-apply-to-all)
= 2
refId(_u[_c2])
families(_dimc)
subscripts(_c2)
hasInitValue(false)

u[C3]: const (non-apply-to-all)
= 3
refId(_u[_c3])
families(_dimc)
subscripts(_c3)
hasInitValue(false)

u[C4]: const (non-apply-to-all)
= 4
refId(_u[_c4])
families(_dimc)
subscripts(_c4)
hasInitValue(false)

u[C5]: const (non-apply-to-all)
= 5
refId(_u[_c5])
families(_dimc)
subscripts(_c5)
hasInitValue(false)

18 changes: 13 additions & 5 deletions packages/compile/src/model/subscript-range-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,20 @@ export default class SubscriptRangeReader extends ModelReader {
}
}
visitSubscriptList(ctx) {
// Get the subscripts from each element of the list, which can include both
// individual subscript indices and numeric ranges.
// A subscript list can appear in either a subscript range or mapping.
let subscripts = R.map(id => id.getText(), ctx.Id())
let subscripts = []
for (let child of ctx.children) {
if (child?.symbol?.type === ModelParser.Id) {
subscripts.push(child.getText())
} else if (child?.ruleIndex === ModelParser.RULE_subscriptSequence) {
this.visitSubscriptSequence(child, subscripts)
}
}
if (ctx.parentCtx.ruleIndex === ModelParser.RULE_subscriptRange) {
this.indNames = subscripts
}
if (ctx.parentCtx.ruleIndex === ModelParser.RULE_subscriptMapping) {
} else if (ctx.parentCtx.ruleIndex === ModelParser.RULE_subscriptMapping) {
this.mappingValue = subscripts
}
}
Expand All @@ -64,7 +72,7 @@ export default class SubscriptRangeReader extends ModelReader {
super.visitSubscriptMapping(ctx)
this.modelMappings.push({ toDim, value: this.mappingValue })
}
visitSubscriptSequence(ctx) {
visitSubscriptSequence(ctx, subscripts) {
// Construct index names from the sequence start and end indices.
// This assumes the indices begin with the same string and end with numbers.
let r = /^(.*?)(\d+)$/
Expand All @@ -75,7 +83,7 @@ export default class SubscriptRangeReader extends ModelReader {
let start = parseInt(matches[0][2])
let end = parseInt(matches[1][2])
for (let i = start; i <= end; i++) {
this.indNames.push(prefix + i)
subscripts.push(prefix + i)
}
}
}
Expand Down

0 comments on commit eabe432

Please sign in to comment.