@sdeverywhere/runtime 0.2.5 → 0.2.7

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
  */
@@ -180,15 +186,16 @@ interface LookupDef {
180
186
  /** The reference that identifies the lookup or data variable to be modified. */
181
187
  varRef: VarRef;
182
188
  /** The lookup data as a flat array of (x,y) pairs. */
183
- points: Float64Array;
189
+ points?: Float64Array;
184
190
  }
185
191
  /**
186
192
  * Create a `LookupDef` instance from the given array of `Point` objects.
187
193
  *
188
194
  * @param varRef The reference to the lookup or data variable to be modified.
189
- * @param points The lookup data as an array of `Point` objects.
195
+ * @param points The lookup data as an array of `Point` objects. This can be
196
+ * undefined, in which case the lookup data will be reset to the original data.
190
197
  */
191
- declare function createLookupDef(varRef: VarRef, points: Point[]): LookupDef;
198
+ declare function createLookupDef(varRef: VarRef, points: Point[] | undefined): LookupDef;
192
199
 
193
200
  /**
194
201
  * Return the length of the array that is required to store the variable
@@ -567,19 +574,60 @@ type JsModelLookupMode = 'interpolate' | 'forward' | 'backward';
567
574
  * @hidden This is not yet part of the public API; for internal use only.
568
575
  */
569
576
  declare class JsModelLookup {
570
- private readonly n;
571
- private readonly data;
577
+ /** The original data passed to the constructor. */
578
+ private readonly originalData;
579
+ /** The size (i.e., number of pairs) of the original data. */
580
+ private readonly originalSize;
581
+ /**
582
+ * The dynamic data array. This will be undefined initially, and the array
583
+ * will be allocated (or grown) by `setData`.
584
+ */
585
+ private dynamicData;
586
+ /** The size (i.e., number of pairs) of the dynamic data. */
587
+ private dynamicSize;
588
+ /**
589
+ * The active data array. This will be the same as either `originalData`
590
+ * or `dynamicData`, depending on whether the lookup data is overridden
591
+ * at runtime using `setData`.
592
+ */
593
+ private activeData;
594
+ /** The size (i.e., number of pairs) of the active data. */
595
+ private activeSize;
596
+ /**
597
+ * The inverted version of the active data array. This is allocated on demand
598
+ * only in the case of `LOOKUP INVERT` function calls.
599
+ */
572
600
  private invertedData?;
601
+ /**
602
+ * The input value for the last hit. This is cached for performance so that we
603
+ * can reduce the amount of linear searching in the common case where `LOOKUP`
604
+ * input values are monotonically increasing.
605
+ */
573
606
  private lastInput;
607
+ /** The index for the last hit (see `lastInput`). */
574
608
  private lastHitIndex;
575
609
  /**
576
- * @param n The number of (x,y) pairs in the lookup.
610
+ * @param size The number of (x,y) pairs in the lookup.
577
611
  * @param data The lookup data, as (x,y) pairs. The length of the array must be
578
612
  * >= 2*n. Note that the data will be stored by reference, so if there is a chance
579
613
  * that the array will be reused or modified by other code, be sure to pass in a
580
614
  * copy of the array.
581
615
  */
582
- constructor(n: number, data: number[] | Float64Array);
616
+ constructor(size: number, data: number[] | Float64Array | undefined);
617
+ /**
618
+ * Set new data for this lookup instance, or restore the original data.
619
+ *
620
+ * If `data` is undefined, the original data that was supplied to the constructor will
621
+ * be restored as the "active" data. Otherwise, `data` will be copied to an internal
622
+ * data buffer, which will be the "active" data. If `size` is greater than the size
623
+ * passed to previous calls, the internal data buffer will be grown as needed.
624
+ *
625
+ * @param size The number of (x,y) pairs in the lookup.
626
+ * @param data The lookup data, as (x,y) pairs. The length of the array must be
627
+ * >= 2*n. Note that the data will be copied into an internal data buffer, so it
628
+ * is not necessary to defensively copy data before calling this method.
629
+ */
630
+ setData(size: number, data: Float64Array | undefined): void;
583
631
  getValueForX(x: number, mode: JsModelLookupMode): number;
584
632
  getValueForY(y: number): number;
585
633
  /**
@@ -716,7 +764,7 @@ interface JsModel {
716
764
  /** @hidden */
717
765
  setInputs(inputValue: (index: number) => number): void;
718
766
  /** @hidden */
719
- setLookup(varSpec: VarSpec, points: Float64Array): void;
767
+ setLookup(varSpec: VarSpec, points: Float64Array | undefined): void;
720
768
  /** @hidden */
721
769
  storeOutputs(storeValue: (value: number) => void): void;
722
770
  /** @hidden */
@@ -787,7 +835,7 @@ declare class MockJsModel implements JsModel {
787
835
  setModelFunctions(fns: JsModelFunctions): void;
788
836
  setTime(time: number): void;
789
837
  setInputs(): void;
790
- setLookup(varSpec: VarSpec, points: Float64Array): void;
838
+ setLookup(varSpec: VarSpec, points: Float64Array | undefined): void;
791
839
  storeOutputs(storeValue: (value: number) => void): void;
792
840
  storeOutput(varSpec: VarSpec, storeValue: (value: number) => void): void;
793
841
  initConstants(): void;
@@ -866,7 +914,7 @@ declare class MockWasmModule implements WasmModule {
866
914
  }
867
915
 
868
916
  /**
869
- * Abstraction that allows for running the wasm model on the JS thread
917
+ * Abstraction that allows for running a generated model on the JS thread
870
918
  * or asynchronously (e.g. in a Web Worker), depending on the implementation.
871
919
  */
872
920
  interface ModelRunner {
@@ -923,7 +971,7 @@ declare function createRunnableModel(generatedModel: GeneratedModel): RunnableMo
923
971
  declare function createSynchronousModelRunner(generatedModel: GeneratedModel): ModelRunner;
924
972
 
925
973
  /**
926
- * A high-level interface that schedules running of the underlying `WasmModel`.
974
+ * A high-level interface that schedules the underlying `ModelRunner`.
927
975
  *
928
976
  * When one or more input values are changed, this class will schedule a model
929
977
  * run to be completed as soon as possible. When the model run has completed,
@@ -952,14 +1000,127 @@ declare class ModelScheduler {
952
1000
  */
953
1001
  constructor(runner: ModelRunner, userInputs: InputValue[], outputs: Outputs);
954
1002
  /**
955
- * Schedule a wasm model run (if not already pending). When the run is
1003
+ * Schedule a model run (if not already pending). When the run is
1004
+ * complete, save the outputs and call the `onOutputsChanged` callback.
1005
+ */
1006
+ private runModelIfNeeded;
1007
+ /**
1008
+ * Run the model asynchronously using the current set of input values.
1009
+ */
1010
+ private runModelNow;
1011
+ }
1012
+
1013
+ /**
1014
+ * Defines a context that holds a distinct set of model inputs and outputs.
1015
+ * These inputs and outputs are kept separate from those in other contexts,
1016
+ * which allows an application to use the same underlying model instance
1017
+ * with multiple sets of inputs and outputs.
1018
+ */
1019
+ interface ModelContext {
1020
+ /**
1021
+ * Called when the outputs have been updated after a model run.
1022
+ */
1023
+ onOutputsChanged?: () => void;
1024
+ /**
1025
+ * Return the series data for the given model output variable or external
1026
+ * dataset.
1027
+ *
1028
+ * @param varId The ID of the output variable associated with the data.
1029
+ * @param sourceName The external data source name (e.g. "Ref"), or
1030
+ * undefined to use the latest model output data from this context.
1031
+ */
1032
+ getSeriesForVar(varId: OutputVarId, sourceName?: SourceName): Series | undefined;
1033
+ }
1034
+ /**
1035
+ * A high-level interface that schedules running of the underlying `ModelRunner`.
1036
+ *
1037
+ * This class is similar to the (single context) `ModelScheduler` class, except
1038
+ * this one supports multiple contexts, each with its own distinct set of
1039
+ * inputs and outputs. This is useful for running the same underlying model
1040
+ * instance with different sets of inputs and outputs. For example, you can
1041
+ * use this to show the outputs for multiple scenarios in a single graph, or
1042
+ * multiple scenarios across different graphs.
1043
+ *
1044
+ * When input values are changed in one or more contexts, this class will schedule
1045
+ * a model run for each changed context to be completed as soon as possible.
1046
+ * When the model run has completed, the context's `onOutputsChanged` function
1047
+ * is called to notify that new output data is available for that context.
1048
+ *
1049
+ * The `ModelRunner` is pluggable to allow for running the model synchronously
1050
+ * (on the main JavaScript thread) or asynchronously (in a Web Worker or Node.js
1051
+ * worker thread).
1052
+ */
1053
+ declare class MultiContextModelScheduler {
1054
+ private readonly runner;
1055
+ /**
1056
+ * An optional `Outputs` instance that will be reused for the initial context. This will
1057
+ * be set to undefined after it is used for the first context.
1058
+ */
1059
+ private initialOutputs?;
1060
+ /** The second array that holds a stable copy of the user inputs. */
1061
+ private currentInputs;
1062
+ /** The contexts that hold distinct sets of inputs and outputs. */
1063
+ private readonly contexts;
1064
+ /** Whether a model run has been scheduled. */
1065
+ private runNeeded;
1066
+ /** Whether a model run is in progress. */
1067
+ private runInProgress;
1068
+ /**
1069
+ * @param runner The model runner.
1070
+ * @param options Additional options for the scheduler.
1071
+ * @param options.initialOutputs An optional `Outputs` instance that will be reused
1072
+ * for the initial context. This is useful for saving memory when an `Outputs`
1073
+ * instance was already created for, e.g., a initial baseline/reference run.
1074
+ */
1075
+ constructor(runner: ModelRunner, options?: {
1076
+ initialOutputs?: Outputs;
1077
+ });
1078
+ /**
1079
+ * Return true if the scheduler has started any model runs.
1080
+ */
1081
+ isStarted(): boolean;
1082
+ /**
1083
+ * Add a new context that holds a distinct set of model inputs and outputs.
1084
+ * These inputs and outputs are kept separate from those in other contexts,
1085
+ * which allows an application to use the same underlying model to run with
1086
+ * multiple I/O contexts.
1087
+ *
1088
+ * Note that the contexts created before the first scheduled model run
1089
+ * will inherit the data from `initialOutputs` passed to the constructor,
1090
+ * but contexts created after that will initially have output values set
1091
+ * to zero.
1092
+ *
1093
+ * @param inputs The input values, in the same order as in the spec file passed to `sde`.
1094
+ * @param options Additional options for the context.
1095
+ * @param options.externalData Additional data that is external to the model outputs.
1096
+ * For example, this can contain data that was captured from an initial reference
1097
+ * run, or other static data that is displayed in graphs alongside the model
1098
+ * output data in graphs.
1099
+ */
1100
+ addContext(inputs: InputValue[], options?: {
1101
+ externalData?: DataMap;
1102
+ }): ModelContext;
1103
+ /**
1104
+ * Remove the given context from the set of contexts managed by the scheduler.
1105
+ *
1106
+ * @param context The context to remove.
1107
+ */
1108
+ removeContext(context: ModelContext): void;
1109
+ /**
1110
+ * Schedule a model run (if not already pending). When the run is
956
1111
  * complete, save the outputs and call the `onOutputsChanged` callback.
957
1112
  */
958
- private runWasmModelIfNeeded;
1113
+ private runModelIfNeeded;
959
1114
  /**
960
- * Run the wasm model asynchronously using the current set of input values.
1115
+ * Run the model asynchronously for all relevant contexts.
1116
+ */
1117
+ private runModelNow;
1118
+ /**
1119
+ * Run the model asynchronously using the current set of input values in the given context.
1120
+ *
1121
+ * @param context The context to use for the model run.
961
1122
  */
962
- private runWasmModelNow;
1123
+ private runModelNowForContext;
963
1124
  }
964
1125
 
965
1126
  /**
@@ -976,4 +1137,4 @@ declare function perfNow(): unknown;
976
1137
  */
977
1138
  declare function perfElapsed(t0: unknown): number;
978
1139
 
979
- 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 };
1140
+ export { BufferedRunModelParams, 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, createInputValue, createLookupDef, createRunnableModel, createSynchronousModelRunner, decodeLookups, encodeLookups, encodeVarIndices, execJsModel, getEncodedLookupBufferLengths, getEncodedVarIndicesLength, getJsModelFunctions, initJsModel, initWasmModel, perfElapsed, perfNow };
package/dist/index.d.ts 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
  */
@@ -180,15 +186,16 @@ interface LookupDef {
180
186
  /** The reference that identifies the lookup or data variable to be modified. */
181
187
  varRef: VarRef;
182
188
  /** The lookup data as a flat array of (x,y) pairs. */
183
- points: Float64Array;
189
+ points?: Float64Array;
184
190
  }
185
191
  /**
186
192
  * Create a `LookupDef` instance from the given array of `Point` objects.
187
193
  *
188
194
  * @param varRef The reference to the lookup or data variable to be modified.
189
- * @param points The lookup data as an array of `Point` objects.
195
+ * @param points The lookup data as an array of `Point` objects. This can be
196
+ * undefined, in which case the lookup data will be reset to the original data.
190
197
  */
191
- declare function createLookupDef(varRef: VarRef, points: Point[]): LookupDef;
198
+ declare function createLookupDef(varRef: VarRef, points: Point[] | undefined): LookupDef;
192
199
 
193
200
  /**
194
201
  * Return the length of the array that is required to store the variable
@@ -567,19 +574,60 @@ type JsModelLookupMode = 'interpolate' | 'forward' | 'backward';
567
574
  * @hidden This is not yet part of the public API; for internal use only.
568
575
  */
569
576
  declare class JsModelLookup {
570
- private readonly n;
571
- private readonly data;
577
+ /** The original data passed to the constructor. */
578
+ private readonly originalData;
579
+ /** The size (i.e., number of pairs) of the original data. */
580
+ private readonly originalSize;
581
+ /**
582
+ * The dynamic data array. This will be undefined initially, and the array
583
+ * will be allocated (or grown) by `setData`.
584
+ */
585
+ private dynamicData;
586
+ /** The size (i.e., number of pairs) of the dynamic data. */
587
+ private dynamicSize;
588
+ /**
589
+ * The active data array. This will be the same as either `originalData`
590
+ * or `dynamicData`, depending on whether the lookup data is overridden
591
+ * at runtime using `setData`.
592
+ */
593
+ private activeData;
594
+ /** The size (i.e., number of pairs) of the active data. */
595
+ private activeSize;
596
+ /**
597
+ * The inverted version of the active data array. This is allocated on demand
598
+ * only in the case of `LOOKUP INVERT` function calls.
599
+ */
572
600
  private invertedData?;
601
+ /**
602
+ * The input value for the last hit. This is cached for performance so that we
603
+ * can reduce the amount of linear searching in the common case where `LOOKUP`
604
+ * input values are monotonically increasing.
605
+ */
573
606
  private lastInput;
607
+ /** The index for the last hit (see `lastInput`). */
574
608
  private lastHitIndex;
575
609
  /**
576
- * @param n The number of (x,y) pairs in the lookup.
610
+ * @param size The number of (x,y) pairs in the lookup.
577
611
  * @param data The lookup data, as (x,y) pairs. The length of the array must be
578
612
  * >= 2*n. Note that the data will be stored by reference, so if there is a chance
579
613
  * that the array will be reused or modified by other code, be sure to pass in a
580
614
  * copy of the array.
581
615
  */
582
- constructor(n: number, data: number[] | Float64Array);
616
+ constructor(size: number, data: number[] | Float64Array | undefined);
617
+ /**
618
+ * Set new data for this lookup instance, or restore the original data.
619
+ *
620
+ * If `data` is undefined, the original data that was supplied to the constructor will
621
+ * be restored as the "active" data. Otherwise, `data` will be copied to an internal
622
+ * data buffer, which will be the "active" data. If `size` is greater than the size
623
+ * passed to previous calls, the internal data buffer will be grown as needed.
624
+ *
625
+ * @param size The number of (x,y) pairs in the lookup.
626
+ * @param data The lookup data, as (x,y) pairs. The length of the array must be
627
+ * >= 2*n. Note that the data will be copied into an internal data buffer, so it
628
+ * is not necessary to defensively copy data before calling this method.
629
+ */
630
+ setData(size: number, data: Float64Array | undefined): void;
583
631
  getValueForX(x: number, mode: JsModelLookupMode): number;
584
632
  getValueForY(y: number): number;
585
633
  /**
@@ -716,7 +764,7 @@ interface JsModel {
716
764
  /** @hidden */
717
765
  setInputs(inputValue: (index: number) => number): void;
718
766
  /** @hidden */
719
- setLookup(varSpec: VarSpec, points: Float64Array): void;
767
+ setLookup(varSpec: VarSpec, points: Float64Array | undefined): void;
720
768
  /** @hidden */
721
769
  storeOutputs(storeValue: (value: number) => void): void;
722
770
  /** @hidden */
@@ -787,7 +835,7 @@ declare class MockJsModel implements JsModel {
787
835
  setModelFunctions(fns: JsModelFunctions): void;
788
836
  setTime(time: number): void;
789
837
  setInputs(): void;
790
- setLookup(varSpec: VarSpec, points: Float64Array): void;
838
+ setLookup(varSpec: VarSpec, points: Float64Array | undefined): void;
791
839
  storeOutputs(storeValue: (value: number) => void): void;
792
840
  storeOutput(varSpec: VarSpec, storeValue: (value: number) => void): void;
793
841
  initConstants(): void;
@@ -866,7 +914,7 @@ declare class MockWasmModule implements WasmModule {
866
914
  }
867
915
 
868
916
  /**
869
- * Abstraction that allows for running the wasm model on the JS thread
917
+ * Abstraction that allows for running a generated model on the JS thread
870
918
  * or asynchronously (e.g. in a Web Worker), depending on the implementation.
871
919
  */
872
920
  interface ModelRunner {
@@ -923,7 +971,7 @@ declare function createRunnableModel(generatedModel: GeneratedModel): RunnableMo
923
971
  declare function createSynchronousModelRunner(generatedModel: GeneratedModel): ModelRunner;
924
972
 
925
973
  /**
926
- * A high-level interface that schedules running of the underlying `WasmModel`.
974
+ * A high-level interface that schedules the underlying `ModelRunner`.
927
975
  *
928
976
  * When one or more input values are changed, this class will schedule a model
929
977
  * run to be completed as soon as possible. When the model run has completed,
@@ -952,14 +1000,127 @@ declare class ModelScheduler {
952
1000
  */
953
1001
  constructor(runner: ModelRunner, userInputs: InputValue[], outputs: Outputs);
954
1002
  /**
955
- * Schedule a wasm model run (if not already pending). When the run is
1003
+ * Schedule a model run (if not already pending). When the run is
1004
+ * complete, save the outputs and call the `onOutputsChanged` callback.
1005
+ */
1006
+ private runModelIfNeeded;
1007
+ /**
1008
+ * Run the model asynchronously using the current set of input values.
1009
+ */
1010
+ private runModelNow;
1011
+ }
1012
+
1013
+ /**
1014
+ * Defines a context that holds a distinct set of model inputs and outputs.
1015
+ * These inputs and outputs are kept separate from those in other contexts,
1016
+ * which allows an application to use the same underlying model instance
1017
+ * with multiple sets of inputs and outputs.
1018
+ */
1019
+ interface ModelContext {
1020
+ /**
1021
+ * Called when the outputs have been updated after a model run.
1022
+ */
1023
+ onOutputsChanged?: () => void;
1024
+ /**
1025
+ * Return the series data for the given model output variable or external
1026
+ * dataset.
1027
+ *
1028
+ * @param varId The ID of the output variable associated with the data.
1029
+ * @param sourceName The external data source name (e.g. "Ref"), or
1030
+ * undefined to use the latest model output data from this context.
1031
+ */
1032
+ getSeriesForVar(varId: OutputVarId, sourceName?: SourceName): Series | undefined;
1033
+ }
1034
+ /**
1035
+ * A high-level interface that schedules running of the underlying `ModelRunner`.
1036
+ *
1037
+ * This class is similar to the (single context) `ModelScheduler` class, except
1038
+ * this one supports multiple contexts, each with its own distinct set of
1039
+ * inputs and outputs. This is useful for running the same underlying model
1040
+ * instance with different sets of inputs and outputs. For example, you can
1041
+ * use this to show the outputs for multiple scenarios in a single graph, or
1042
+ * multiple scenarios across different graphs.
1043
+ *
1044
+ * When input values are changed in one or more contexts, this class will schedule
1045
+ * a model run for each changed context to be completed as soon as possible.
1046
+ * When the model run has completed, the context's `onOutputsChanged` function
1047
+ * is called to notify that new output data is available for that context.
1048
+ *
1049
+ * The `ModelRunner` is pluggable to allow for running the model synchronously
1050
+ * (on the main JavaScript thread) or asynchronously (in a Web Worker or Node.js
1051
+ * worker thread).
1052
+ */
1053
+ declare class MultiContextModelScheduler {
1054
+ private readonly runner;
1055
+ /**
1056
+ * An optional `Outputs` instance that will be reused for the initial context. This will
1057
+ * be set to undefined after it is used for the first context.
1058
+ */
1059
+ private initialOutputs?;
1060
+ /** The second array that holds a stable copy of the user inputs. */
1061
+ private currentInputs;
1062
+ /** The contexts that hold distinct sets of inputs and outputs. */
1063
+ private readonly contexts;
1064
+ /** Whether a model run has been scheduled. */
1065
+ private runNeeded;
1066
+ /** Whether a model run is in progress. */
1067
+ private runInProgress;
1068
+ /**
1069
+ * @param runner The model runner.
1070
+ * @param options Additional options for the scheduler.
1071
+ * @param options.initialOutputs An optional `Outputs` instance that will be reused
1072
+ * for the initial context. This is useful for saving memory when an `Outputs`
1073
+ * instance was already created for, e.g., a initial baseline/reference run.
1074
+ */
1075
+ constructor(runner: ModelRunner, options?: {
1076
+ initialOutputs?: Outputs;
1077
+ });
1078
+ /**
1079
+ * Return true if the scheduler has started any model runs.
1080
+ */
1081
+ isStarted(): boolean;
1082
+ /**
1083
+ * Add a new context that holds a distinct set of model inputs and outputs.
1084
+ * These inputs and outputs are kept separate from those in other contexts,
1085
+ * which allows an application to use the same underlying model to run with
1086
+ * multiple I/O contexts.
1087
+ *
1088
+ * Note that the contexts created before the first scheduled model run
1089
+ * will inherit the data from `initialOutputs` passed to the constructor,
1090
+ * but contexts created after that will initially have output values set
1091
+ * to zero.
1092
+ *
1093
+ * @param inputs The input values, in the same order as in the spec file passed to `sde`.
1094
+ * @param options Additional options for the context.
1095
+ * @param options.externalData Additional data that is external to the model outputs.
1096
+ * For example, this can contain data that was captured from an initial reference
1097
+ * run, or other static data that is displayed in graphs alongside the model
1098
+ * output data in graphs.
1099
+ */
1100
+ addContext(inputs: InputValue[], options?: {
1101
+ externalData?: DataMap;
1102
+ }): ModelContext;
1103
+ /**
1104
+ * Remove the given context from the set of contexts managed by the scheduler.
1105
+ *
1106
+ * @param context The context to remove.
1107
+ */
1108
+ removeContext(context: ModelContext): void;
1109
+ /**
1110
+ * Schedule a model run (if not already pending). When the run is
956
1111
  * complete, save the outputs and call the `onOutputsChanged` callback.
957
1112
  */
958
- private runWasmModelIfNeeded;
1113
+ private runModelIfNeeded;
959
1114
  /**
960
- * Run the wasm model asynchronously using the current set of input values.
1115
+ * Run the model asynchronously for all relevant contexts.
1116
+ */
1117
+ private runModelNow;
1118
+ /**
1119
+ * Run the model asynchronously using the current set of input values in the given context.
1120
+ *
1121
+ * @param context The context to use for the model run.
961
1122
  */
962
- private runWasmModelNow;
1123
+ private runModelNowForContext;
963
1124
  }
964
1125
 
965
1126
  /**
@@ -976,4 +1137,4 @@ declare function perfNow(): unknown;
976
1137
  */
977
1138
  declare function perfElapsed(t0: unknown): number;
978
1139
 
979
- 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 };
1140
+ export { BufferedRunModelParams, 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, createInputValue, createLookupDef, createRunnableModel, createSynchronousModelRunner, decodeLookups, encodeLookups, encodeVarIndices, execJsModel, getEncodedLookupBufferLengths, getEncodedVarIndicesLength, getJsModelFunctions, initJsModel, initWasmModel, perfElapsed, perfNow };