@sdeverywhere/runtime 0.2.6 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { Result } from 'neverthrow';
2
2
 
3
+ /** The name of a data source for external/static datasets, e.g., 'Ref', 'Constants'. */
4
+ type SourceName = string;
3
5
  /** A variable name, as used in the modeling tool. */
4
6
  type VarName = string;
5
7
  /** A variable identifier, as used in model code generated by SDEverywhere. */
@@ -86,6 +88,10 @@ declare function createInputValue(varId: InputVarId, defaultValue: number, initi
86
88
 
87
89
  /** Indicates the type of error encountered when parsing an outputs buffer. */
88
90
  type ParseError = 'invalid-point-count';
91
+ /** Type alias for a map that holds a `Series` instance for each output (or static) variable ID. */
92
+ type SeriesMap = Map<OutputVarId, Series>;
93
+ /** Type alias for a map that holds data for a given source name. */
94
+ type DataMap = Map<SourceName, SeriesMap>;
89
95
  /**
90
96
  * A time series of data points for an output variable.
91
97
  */
@@ -173,6 +179,24 @@ declare class Outputs {
173
179
  getSeriesForVar(varId: OutputVarId): Series | undefined;
174
180
  }
175
181
 
182
+ /**
183
+ * Specifies the constant value that will be used to override a constant in a
184
+ * generated model.
185
+ */
186
+ interface ConstantDef {
187
+ /** The reference that identifies the constant variable to be modified. */
188
+ varRef: VarRef;
189
+ /** The new constant value. */
190
+ value: number;
191
+ }
192
+ /**
193
+ * Create a `ConstantDef` instance.
194
+ *
195
+ * @param varRef The reference to the constant variable to be modified.
196
+ * @param value The new constant value.
197
+ */
198
+ declare function createConstantDef(varRef: VarRef, value: number): ConstantDef;
199
+
176
200
  /**
177
201
  * Specifies the data that will be used to set or override a lookup definition.
178
202
  */
@@ -210,6 +234,41 @@ declare function getEncodedVarIndicesLength(varSpecs: VarSpec[]): number;
210
234
  * @param varSpecs The `VarSpec` instances to encode.
211
235
  */
212
236
  declare function encodeVarIndices(varSpecs: VarSpec[], indicesArray: Int32Array): void;
237
+ /**
238
+ * Return the lengths of the arrays that are required to store the constant values
239
+ * and indices for the given `ConstantDef` instances.
240
+ *
241
+ * @hidden This is not part of the public API; it is exposed here for use by
242
+ * the synchronous and asynchronous model runner implementations.
243
+ *
244
+ * @param constantDefs The `ConstantDef` instances to encode.
245
+ */
246
+ declare function getEncodedConstantBufferLengths(constantDefs: ConstantDef[]): {
247
+ constantIndicesLength: number;
248
+ constantsLength: number;
249
+ };
250
+ /**
251
+ * Encode constant values and indices to the given arrays.
252
+ *
253
+ * @hidden This is not part of the public API; it is exposed here for use by
254
+ * the synchronous and asynchronous model runner implementations.
255
+ *
256
+ * @param constantDefs The `ConstantDef` instances to encode.
257
+ * @param constantIndicesArray The view on the constant indices buffer.
258
+ * @param constantsArray The view on the constant values buffer.
259
+ */
260
+ declare function encodeConstants(constantDefs: ConstantDef[], constantIndicesArray: Int32Array, constantsArray: Float64Array): void;
261
+ /**
262
+ * Decode constant values and indices from the given buffer views and return the
263
+ * reconstructed `ConstantDef` instances.
264
+ *
265
+ * @hidden This is not part of the public API; it is exposed here for use by
266
+ * the synchronous and asynchronous model runner implementations.
267
+ *
268
+ * @param constantIndicesArray The view on the constant indices buffer.
269
+ * @param constantsArray The view on the constant values buffer.
270
+ */
271
+ declare function decodeConstants(constantIndicesArray: Int32Array, constantsArray: Float64Array): ConstantDef[];
213
272
  /**
214
273
  * Return the lengths of the arrays that are required to store the lookup data
215
274
  * and indices for the given `LookupDef` instances.
@@ -299,6 +358,17 @@ declare class ModelListing {
299
358
  * Additional options that can be passed to a `runModel` call to influence the model run.
300
359
  */
301
360
  interface RunModelOptions {
361
+ /**
362
+ * If defined, override the values for the specified constant variables.
363
+ *
364
+ * Note that constant overrides do not persist after the `runModel` call. Because
365
+ * `initConstants` is called at the beginning of each `runModel` call, all constants
366
+ * are reset to their default values before each model run. If you want to override
367
+ * constants, you must provide them in the options for each `runModel` call. To
368
+ * reset constants to their original values, simply stop passing them in the options
369
+ * (or pass an empty array).
370
+ */
371
+ constants?: ConstantDef[];
302
372
  /**
303
373
  * If defined, override the data for the specified lookups and/or data variables.
304
374
  *
@@ -377,6 +447,11 @@ interface RunModelParams {
377
447
  * @param array The array that contains the output values.
378
448
  */
379
449
  storeOutputs(array: Float64Array): void;
450
+ /**
451
+ * Return an array containing constant overrides, or undefined if no constants were passed
452
+ * to the latest `runModel` call.
453
+ */
454
+ getConstants(): ConstantDef[] | undefined;
380
455
  /**
381
456
  * Return an array containing lookup overrides, or undefined if no lookups were passed to
382
457
  * the latest `runModel` call.
@@ -413,6 +488,8 @@ declare class BufferedRunModelParams implements RunModelParams {
413
488
  * inputs
414
489
  * outputs
415
490
  * outputIndices
491
+ * constants (values)
492
+ * constantIndices
416
493
  * lookups (data)
417
494
  * lookupIndices
418
495
  */
@@ -430,6 +507,10 @@ declare class BufferedRunModelParams implements RunModelParams {
430
507
  private readonly outputs;
431
508
  /** The output indices section of the `encoded` buffer. */
432
509
  private readonly outputIndices;
510
+ /** The constant values section of the `encoded` buffer. */
511
+ private readonly constants;
512
+ /** The constant indices section of the `encoded` buffer. */
513
+ private readonly constantIndices;
433
514
  /** The lookup data section of the `encoded` buffer. */
434
515
  private readonly lookups;
435
516
  /** The lookup indices section of the `encoded` buffer. */
@@ -453,6 +534,7 @@ declare class BufferedRunModelParams implements RunModelParams {
453
534
  getOutputs(): Float64Array | undefined;
454
535
  getOutputsObject(): Outputs | undefined;
455
536
  storeOutputs(array: Float64Array): void;
537
+ getConstants(): ConstantDef[] | undefined;
456
538
  getLookups(): LookupDef[] | undefined;
457
539
  getElapsedTime(): number;
458
540
  storeElapsedTime(elapsed: number): void;
@@ -495,6 +577,7 @@ declare class ReferencedRunModelParams implements RunModelParams {
495
577
  private outputs;
496
578
  private outputsLengthInElements;
497
579
  private outputIndicesLengthInElements;
580
+ private constants;
498
581
  private lookups;
499
582
  /**
500
583
  * @param listing The model listing that is used to locate a variable that is referenced by
@@ -511,6 +594,7 @@ declare class ReferencedRunModelParams implements RunModelParams {
511
594
  getOutputs(): Float64Array | undefined;
512
595
  getOutputsObject(): Outputs | undefined;
513
596
  storeOutputs(array: Float64Array): void;
597
+ getConstants(): ConstantDef[] | undefined;
514
598
  getLookups(): LookupDef[] | undefined;
515
599
  getElapsedTime(): number;
516
600
  storeElapsedTime(elapsed: number): void;
@@ -758,6 +842,8 @@ interface JsModel {
758
842
  /** @hidden */
759
843
  setInputs(inputValue: (index: number) => number): void;
760
844
  /** @hidden */
845
+ setConstant(varSpec: VarSpec, value: number): void;
846
+ /** @hidden */
761
847
  setLookup(varSpec: VarSpec, points: Float64Array | undefined): void;
762
848
  /** @hidden */
763
849
  storeOutputs(storeValue: (value: number) => void): void;
@@ -796,7 +882,7 @@ declare function execJsModel(jsModel: JsModel): void;
796
882
  * @hidden This type is not part of the public API; it is exposed only for use in
797
883
  * tests in the runtime-async package.
798
884
  */
799
- type OnEvalAux = (vars: Map<VarId, number>, lookups: Map<VarId, JsModelLookup>) => void;
885
+ type OnEvalAux = (vars: Map<VarId, number>, constants: Map<VarId, number> | undefined, lookups: Map<VarId, JsModelLookup>) => void;
800
886
  /**
801
887
  * @hidden This type is not part of the public API; it is exposed only for use in
802
888
  * tests in the runtime-async package.
@@ -810,6 +896,7 @@ declare class MockJsModel implements JsModel {
810
896
  private readonly initialTime;
811
897
  private readonly finalTime;
812
898
  private readonly vars;
899
+ private readonly constants;
813
900
  private readonly lookups;
814
901
  private fns;
815
902
  readonly onEvalAux: OnEvalAux;
@@ -829,6 +916,7 @@ declare class MockJsModel implements JsModel {
829
916
  setModelFunctions(fns: JsModelFunctions): void;
830
917
  setTime(time: number): void;
831
918
  setInputs(): void;
919
+ setConstant(varSpec: VarSpec, value: number): void;
832
920
  setLookup(varSpec: VarSpec, points: Float64Array | undefined): void;
833
921
  storeOutputs(storeValue: (value: number) => void): void;
834
922
  storeOutput(varSpec: VarSpec, storeValue: (value: number) => void): void;
@@ -874,7 +962,7 @@ declare function initWasmModel(wasmModule: WasmModule): RunnableModel;
874
962
  * @hidden This type is not part of the public API; it is exposed only for use in
875
963
  * tests in the runtime-async package.
876
964
  */
877
- type OnRunModel = (inputs: Float64Array, outputs: Float64Array, lookups: Map<VarId, JsModelLookup>, outputIndices?: Int32Array) => void;
965
+ type OnRunModel = (inputs: Float64Array, outputs: Float64Array, constants: Map<VarId, number> | undefined, lookups: Map<VarId, JsModelLookup>, outputIndices?: Int32Array) => void;
878
966
  /**
879
967
  * @hidden This type is not part of the public API; it is exposed only for use in
880
968
  * tests in the runtime-async package.
@@ -892,6 +980,7 @@ declare class MockWasmModule implements WasmModule {
892
980
  private mallocOffset;
893
981
  private readonly allocs;
894
982
  private readonly lookups;
983
+ private readonly constants;
895
984
  readonly onRunModel: OnRunModel;
896
985
  constructor(options: {
897
986
  initialTime: number;
@@ -901,14 +990,14 @@ declare class MockWasmModule implements WasmModule {
901
990
  onRunModel: OnRunModel;
902
991
  });
903
992
  varIdForSpec(varSpec: VarSpec): VarId;
904
- cwrap(fname: string): (varIndex: number, _subIndicesAddress: number, pointsAddress: number, numPoints: number) => void;
993
+ cwrap(fname: string): (inputsAddress: number, _inputIndicesAddress: number, outputsAddress: number, outputIndicesAddress: number, constantValuesAddress: number, constantIndicesAddress: number) => void;
905
994
  _malloc(lengthInBytes: number): number;
906
995
  _free(): void;
907
996
  private getHeapView;
908
997
  }
909
998
 
910
999
  /**
911
- * Abstraction that allows for running the wasm model on the JS thread
1000
+ * Abstraction that allows for running a generated model on the JS thread
912
1001
  * or asynchronously (e.g. in a Web Worker), depending on the implementation.
913
1002
  */
914
1003
  interface ModelRunner {
@@ -965,7 +1054,7 @@ declare function createRunnableModel(generatedModel: GeneratedModel): RunnableMo
965
1054
  declare function createSynchronousModelRunner(generatedModel: GeneratedModel): ModelRunner;
966
1055
 
967
1056
  /**
968
- * A high-level interface that schedules running of the underlying `WasmModel`.
1057
+ * A high-level interface that schedules the underlying `ModelRunner`.
969
1058
  *
970
1059
  * When one or more input values are changed, this class will schedule a model
971
1060
  * run to be completed as soon as possible. When the model run has completed,
@@ -994,14 +1083,127 @@ declare class ModelScheduler {
994
1083
  */
995
1084
  constructor(runner: ModelRunner, userInputs: InputValue[], outputs: Outputs);
996
1085
  /**
997
- * Schedule a wasm model run (if not already pending). When the run is
1086
+ * Schedule a model run (if not already pending). When the run is
1087
+ * complete, save the outputs and call the `onOutputsChanged` callback.
1088
+ */
1089
+ private runModelIfNeeded;
1090
+ /**
1091
+ * Run the model asynchronously using the current set of input values.
1092
+ */
1093
+ private runModelNow;
1094
+ }
1095
+
1096
+ /**
1097
+ * Defines a context that holds a distinct set of model inputs and outputs.
1098
+ * These inputs and outputs are kept separate from those in other contexts,
1099
+ * which allows an application to use the same underlying model instance
1100
+ * with multiple sets of inputs and outputs.
1101
+ */
1102
+ interface ModelContext {
1103
+ /**
1104
+ * Called when the outputs have been updated after a model run.
1105
+ */
1106
+ onOutputsChanged?: () => void;
1107
+ /**
1108
+ * Return the series data for the given model output variable or external
1109
+ * dataset.
1110
+ *
1111
+ * @param varId The ID of the output variable associated with the data.
1112
+ * @param sourceName The external data source name (e.g. "Ref"), or
1113
+ * undefined to use the latest model output data from this context.
1114
+ */
1115
+ getSeriesForVar(varId: OutputVarId, sourceName?: SourceName): Series | undefined;
1116
+ }
1117
+ /**
1118
+ * A high-level interface that schedules running of the underlying `ModelRunner`.
1119
+ *
1120
+ * This class is similar to the (single context) `ModelScheduler` class, except
1121
+ * this one supports multiple contexts, each with its own distinct set of
1122
+ * inputs and outputs. This is useful for running the same underlying model
1123
+ * instance with different sets of inputs and outputs. For example, you can
1124
+ * use this to show the outputs for multiple scenarios in a single graph, or
1125
+ * multiple scenarios across different graphs.
1126
+ *
1127
+ * When input values are changed in one or more contexts, this class will schedule
1128
+ * a model run for each changed context to be completed as soon as possible.
1129
+ * When the model run has completed, the context's `onOutputsChanged` function
1130
+ * is called to notify that new output data is available for that context.
1131
+ *
1132
+ * The `ModelRunner` is pluggable to allow for running the model synchronously
1133
+ * (on the main JavaScript thread) or asynchronously (in a Web Worker or Node.js
1134
+ * worker thread).
1135
+ */
1136
+ declare class MultiContextModelScheduler {
1137
+ private readonly runner;
1138
+ /**
1139
+ * An optional `Outputs` instance that will be reused for the initial context. This will
1140
+ * be set to undefined after it is used for the first context.
1141
+ */
1142
+ private initialOutputs?;
1143
+ /** The second array that holds a stable copy of the user inputs. */
1144
+ private currentInputs;
1145
+ /** The contexts that hold distinct sets of inputs and outputs. */
1146
+ private readonly contexts;
1147
+ /** Whether a model run has been scheduled. */
1148
+ private runNeeded;
1149
+ /** Whether a model run is in progress. */
1150
+ private runInProgress;
1151
+ /**
1152
+ * @param runner The model runner.
1153
+ * @param options Additional options for the scheduler.
1154
+ * @param options.initialOutputs An optional `Outputs` instance that will be reused
1155
+ * for the initial context. This is useful for saving memory when an `Outputs`
1156
+ * instance was already created for, e.g., a initial baseline/reference run.
1157
+ */
1158
+ constructor(runner: ModelRunner, options?: {
1159
+ initialOutputs?: Outputs;
1160
+ });
1161
+ /**
1162
+ * Return true if the scheduler has started any model runs.
1163
+ */
1164
+ isStarted(): boolean;
1165
+ /**
1166
+ * Add a new context that holds a distinct set of model inputs and outputs.
1167
+ * These inputs and outputs are kept separate from those in other contexts,
1168
+ * which allows an application to use the same underlying model to run with
1169
+ * multiple I/O contexts.
1170
+ *
1171
+ * Note that the contexts created before the first scheduled model run
1172
+ * will inherit the data from `initialOutputs` passed to the constructor,
1173
+ * but contexts created after that will initially have output values set
1174
+ * to zero.
1175
+ *
1176
+ * @param inputs The input values, in the same order as in the spec file passed to `sde`.
1177
+ * @param options Additional options for the context.
1178
+ * @param options.externalData Additional data that is external to the model outputs.
1179
+ * For example, this can contain data that was captured from an initial reference
1180
+ * run, or other static data that is displayed in graphs alongside the model
1181
+ * output data in graphs.
1182
+ */
1183
+ addContext(inputs: InputValue[], options?: {
1184
+ externalData?: DataMap;
1185
+ }): ModelContext;
1186
+ /**
1187
+ * Remove the given context from the set of contexts managed by the scheduler.
1188
+ *
1189
+ * @param context The context to remove.
1190
+ */
1191
+ removeContext(context: ModelContext): void;
1192
+ /**
1193
+ * Schedule a model run (if not already pending). When the run is
998
1194
  * complete, save the outputs and call the `onOutputsChanged` callback.
999
1195
  */
1000
- private runWasmModelIfNeeded;
1196
+ private runModelIfNeeded;
1001
1197
  /**
1002
- * Run the wasm model asynchronously using the current set of input values.
1198
+ * Run the model asynchronously for all relevant contexts.
1199
+ */
1200
+ private runModelNow;
1201
+ /**
1202
+ * Run the model asynchronously using the current set of input values in the given context.
1203
+ *
1204
+ * @param context The context to use for the model run.
1003
1205
  */
1004
- private runWasmModelNow;
1206
+ private runModelNowForContext;
1005
1207
  }
1006
1208
 
1007
1209
  /**
@@ -1018,4 +1220,4 @@ declare function perfNow(): unknown;
1018
1220
  */
1019
1221
  declare function perfElapsed(t0: unknown): number;
1020
1222
 
1021
- export { BufferedRunModelParams, type GeneratedModel, type InputCallbacks, type InputValue, type InputVarId, type JsModel, type JsModelFunctionContext, type JsModelFunctions, type LookupDef, MockJsModel, MockWasmModule, ModelListing, type ModelListingSpecs, type ModelRunner, ModelScheduler, type OnEvalAux, type OnRunModel, type OutputVarId, Outputs, type ParseError, type Point, ReferencedRunModelParams, type RunModelOptions, type RunModelParams, type RunnableModel, Series, type VarId, type VarName, type VarRef, type VarSpec, type WasmModule, createInputValue, createLookupDef, createRunnableModel, createSynchronousModelRunner, decodeLookups, encodeLookups, encodeVarIndices, execJsModel, getEncodedLookupBufferLengths, getEncodedVarIndicesLength, getJsModelFunctions, initJsModel, initWasmModel, perfElapsed, perfNow };
1223
+ export { BufferedRunModelParams, type ConstantDef, type DataMap, type GeneratedModel, type InputCallbacks, type InputValue, type InputVarId, type JsModel, type JsModelFunctionContext, type JsModelFunctions, type LookupDef, MockJsModel, MockWasmModule, type ModelContext, ModelListing, type ModelListingSpecs, type ModelRunner, ModelScheduler, MultiContextModelScheduler, type OnEvalAux, type OnRunModel, type OutputVarId, Outputs, type ParseError, type Point, ReferencedRunModelParams, type RunModelOptions, type RunModelParams, type RunnableModel, Series, type SeriesMap, type SourceName, type VarId, type VarName, type VarRef, type VarSpec, type WasmModule, createConstantDef, createInputValue, createLookupDef, createRunnableModel, createSynchronousModelRunner, decodeConstants, decodeLookups, encodeConstants, encodeLookups, encodeVarIndices, execJsModel, getEncodedConstantBufferLengths, getEncodedLookupBufferLengths, getEncodedVarIndicesLength, getJsModelFunctions, initJsModel, initWasmModel, perfElapsed, perfNow };