Skip to content

Commit

Permalink
fix: generate correct references for the ALLOCATE AVAILABLE priority …
Browse files Browse the repository at this point in the history
…profile (#136)

Fixes #135
  • Loading branch information
ToddFincannon authored Oct 14, 2021
1 parent 4c66470 commit b1d8ae2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/EquationReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
indexNamesForSubscript,
normalizeSubscripts,
separatedVariableIndex,
sub,
isDimension
} from './Subscript.js'
import {
Expand Down Expand Up @@ -67,15 +68,15 @@ export default class EquationReader extends ModelReader {
// In Vensim a variable can refer to its current value in the state.
// Do not add self-references to the lists of references.
// Do not duplicate references.
if (refId !== this.var.refId && !R.contains(refId, list)) {
if (refId !== this.var.refId && !list.includes(refId)) {
list.push(refId)
}
}
// Add expanded reference ids if they exist, otherwise, add the single reference id.
if (R.isEmpty(this.expandedRefIds)) {
add(this.refId)
} else {
R.forEach(refId => add(refId), this.expandedRefIds)
this.expandedRefIds.forEach(refId => add(refId))
}
}
//
Expand Down Expand Up @@ -250,6 +251,18 @@ export default class EquationReader extends ModelReader {
this.addReferencesToList(this.var.initReferences)
} else if (this.argIndexForFunctionName('_SAMPLE_IF_TRUE') === 2) {
this.addReferencesToList(this.var.initReferences)
} else if (this.argIndexForFunctionName('_ALLOCATE_AVAILABLE') === 1) {
// Reference the second and third elements of the priority profile argument instead of the first one
// that Vensim requires for ALLOCATE AVAILABLE. This is required to get correct dependencies.
let ptypeRefId = this.expandedRefIds[0]
let { subscripts } = Model.splitRefId(ptypeRefId)
let ptypeIndexName = subscripts[1]
let profileElementsDimName = sub(ptypeIndexName).family
let profileElementsDim = sub(profileElementsDimName)
let priorityRefId = ptypeRefId.replace(ptypeIndexName, profileElementsDim.value[1])
let widthRefId = ptypeRefId.replace(ptypeIndexName, profileElementsDim.value[2])
this.expandedRefIds = [priorityRefId, widthRefId]
this.addReferencesToList(this.var.references)
} else if (this.var.isInitial()) {
this.addReferencesToList(this.var.initReferences)
} else {
Expand Down
1 change: 1 addition & 0 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,7 @@ export default {
read,
refIdForVar,
refIdsWithName,
splitRefId,
variables,
varNames,
varsWithName,
Expand Down
2 changes: 1 addition & 1 deletion src/c/vensim.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ double* _ALLOCATE_AVAILABLE(
}
// Start the search in the midpoint of the means, with a big first jump.
double total_allocations = 0.0;
double x = (max_mean - min_mean) / 2.0;
double x = (max_mean + min_mean) / 2.0;
double delta = normal_curve_tail;
size_t num_steps = 0;
double last_delta_sign = 1.0;
Expand Down

0 comments on commit b1d8ae2

Please sign in to comment.