From 1e2e51145142763ca2348b9172c637e01864e63d Mon Sep 17 00:00:00 2001 From: Todd Fincannon Date: Tue, 12 Oct 2021 21:23:03 -0700 Subject: [PATCH] generate correct references for the ALLOCATE AVAILABLE priority profile --- src/EquationReader.js | 17 +++++++++++++++-- src/Model.js | 1 + src/c/vensim.c | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/EquationReader.js b/src/EquationReader.js index bcb6af99..dc3ded6a 100644 --- a/src/EquationReader.js +++ b/src/EquationReader.js @@ -10,6 +10,7 @@ import { indexNamesForSubscript, normalizeSubscripts, separatedVariableIndex, + sub, isDimension } from './Subscript.js' import { @@ -67,7 +68,7 @@ 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) } } @@ -75,7 +76,7 @@ export default class EquationReader extends ModelReader { if (R.isEmpty(this.expandedRefIds)) { add(this.refId) } else { - R.forEach(refId => add(refId), this.expandedRefIds) + this.expandedRefIds.forEach(refId => add(refId)) } } // @@ -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 { diff --git a/src/Model.js b/src/Model.js index e0999f1e..df170807 100644 --- a/src/Model.js +++ b/src/Model.js @@ -1042,6 +1042,7 @@ export default { read, refIdForVar, refIdsWithName, + splitRefId, variables, varNames, varsWithName, diff --git a/src/c/vensim.c b/src/c/vensim.c index 7396149d..ecbe8831 100644 --- a/src/c/vensim.c +++ b/src/c/vensim.c @@ -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;