Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generate correct references for the ALLOCATE AVAILABLE priority profile #136

Merged
merged 2 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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