sbmlsim-core 0.5.0 → 0.6.0

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/README.md CHANGED
@@ -20,7 +20,7 @@ npm install sbmlsim-core
20
20
  or import the modules from a CDN, which serves them with the headers every origin may read:
21
21
 
22
22
  ```js
23
- import { createPanelEndpoint } from "https://cdn.jsdelivr.net/npm/sbmlsim-core@0.4.0/panels.mjs";
23
+ import { createPanelEndpoint } from "https://cdn.jsdelivr.net/npm/sbmlsim-core@0.6.0/panels.mjs";
24
24
  ```
25
25
 
26
26
  A pool started from a module of another origin starts its workers through a module of the page's own origin, so the CDN works for the workers as well. The engine needs fixed-width SIMD of WebAssembly: Chrome 91, Firefox 89, Safari 16.4 or newer, Node 20 or newer.
@@ -86,7 +86,7 @@ pool.close();
86
86
  <!-- the IIFE build of sbmlsim-web, a release with registerEndpoint -->
87
87
  <script src="sbmlsim-web.iife.js"></script>
88
88
  <script type="module">
89
- import { createPanelEndpoint } from "https://cdn.jsdelivr.net/npm/sbmlsim-core@0.4.0/panels.mjs";
89
+ import { createPanelEndpoint } from "https://cdn.jsdelivr.net/npm/sbmlsim-core@0.6.0/panels.mjs";
90
90
 
91
91
  sbmlsimWeb.registerEndpoint(
92
92
  "local:glimepiride",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sbmlsim-core",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Simulate SBML models compiled to WebAssembly in browsers and Node: the JavaScript host of sbmlsim-core with its engine (CVODE, KLU), a pool of workers and the panel endpoint of sbmlsim",
5
5
  "keywords": [
6
6
  "sbml",
package/panels.mjs CHANGED
@@ -58,7 +58,7 @@ import { EngineError } from "./loader.mjs";
58
58
  import { createPool, defaultWorkers } from "./pool.mjs";
59
59
 
60
60
  /** The version of sbmlsim-core (`js/package.json`), which the provenance of a run names. */
61
- export const CORE_VERSION = "0.5.0";
61
+ export const CORE_VERSION = "0.6.0";
62
62
 
63
63
  /** The engine a provenance names. */
64
64
  export const ENGINE = `sbmlsim-core ${CORE_VERSION}`;
@@ -460,6 +460,60 @@ class Runs {
460
460
  }
461
461
  }
462
462
 
463
+ /** The most doses of a dosing protocol after its first one, `MAX_DOSES` of sbmlsim. */
464
+ export const MAX_DOSES = 1000;
465
+
466
+ /**
467
+ * The times of the doses of a dosing protocol after its first one, as `dose_times` of
468
+ * `sbmlsim.panel`: `k * interval` for `k = 1, 2, ...` while before the duration (the end without)
469
+ * and the end, at most `MAX_DOSES`; none for an interval which is not positive.
470
+ */
471
+ export function doseTimes(interval, duration, end) {
472
+ if (!(interval > 0)) return [];
473
+ const until = duration === null || duration === undefined ? end : Math.min(duration, end);
474
+ const times = [];
475
+ for (let k = 1; k <= MAX_DOSES; k += 1) {
476
+ const time = k * interval;
477
+ if (!(time < until)) break;
478
+ times.push(time);
479
+ }
480
+ return times;
481
+ }
482
+
483
+ /**
484
+ * The changes of the doses of the dosing protocols of a run at a state of its knobs: per dose
485
+ * the increment of its target by the dose of its knob in the unit of the target, in the order of
486
+ * the times (the changes of one time are one change of the engine).
487
+ */
488
+ function doseChanges(run, state) {
489
+ const changes = [];
490
+ for (const dosing of run.dosing ?? []) {
491
+ const dose = state[dosing.dose] * run.knobs[dosing.dose].factor;
492
+ const interval = state[dosing.interval] * dosing.interval_factor;
493
+ const duration = dosing.duration === null || dosing.duration === undefined ? null : state[dosing.duration] * dosing.duration_factor;
494
+ for (const time of doseTimes(interval, duration, run.end)) {
495
+ changes.push({ time, values: [], increments: [[dosing.target, dose]] });
496
+ }
497
+ }
498
+ return changes.sort((a, b) => a.time - b.time);
499
+ }
500
+
501
+ /**
502
+ * The rows of a result which are its output times: of two rows of one time (a change at an
503
+ * output time, the state before it and after it) the one after it.
504
+ */
505
+ function outputRows(result) {
506
+ if (result.columns[0] !== "time") throw new Error(`a result has the first column ${result.columns[0]}, not time`);
507
+ const width = result.columns.length;
508
+ const time = (r) => result.values[r * width];
509
+ const rows = [];
510
+ for (let r = 0; r < result.nRows; r += 1) {
511
+ if (r + 1 < result.nRows && time(r + 1) === time(r)) continue;
512
+ rows.push(r);
513
+ }
514
+ return rows;
515
+ }
516
+
463
517
  /**
464
518
  * Creates the endpoint of a bundle: a function `(url, init) => Promise<Response>` with the
465
519
  * signature of `fetch`, see the module comment. Options:
@@ -645,20 +699,23 @@ export function createPanelEndpoint(options) {
645
699
  if (spec.target !== null && spec.target !== undefined) preinit.set(spec.target, state[knob.id] * spec.factor);
646
700
  if (spec.parameter !== null && spec.parameter !== undefined) preinit.set(spec.parameter, state[knob.id]);
647
701
  }
648
- return [...preinit].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
702
+ return {
703
+ preinit: [...preinit].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)),
704
+ changes: doseChanges(run, state),
705
+ };
649
706
  });
650
707
 
651
708
  const runKey = JSON.stringify([run.model, run.start, run.end, run.points, points]);
652
709
  const results = await runs.get(
653
710
  runKey,
654
711
  async (runSignal) => {
655
- const requests = points.map((preinit) => ({
712
+ const requests = points.map(({ preinit, changes }) => ({
656
713
  contract: "simulation/1",
657
714
  start: run.start,
658
715
  end: run.end,
659
716
  output: { uniform: { steps: run.points } },
660
717
  preinit,
661
- changes: [],
718
+ changes,
662
719
  steady_state: null,
663
720
  selections: info.selections,
664
721
  settings: info.settings,
@@ -681,8 +738,11 @@ export function createPanelEndpoint(options) {
681
738
  for (const curve of declaration.curves ?? []) curves[curve.id] = [];
682
739
  results.forEach((result, index) => {
683
740
  if (result === null) failed.push(index);
684
- if (result !== null && result.nRows !== run.x.length) {
685
- throw new Error(`the run of the panel ${panel} has ${result.nRows} rows, its x ${run.x.length}`);
741
+ // a dose at an output time is two rows of that time, the state before it and after it;
742
+ // the curve is the state after it, as the service interpolates onto its times
743
+ const rows = result === null ? [] : outputRows(result);
744
+ if (result !== null && rows.length !== run.x.length) {
745
+ throw new Error(`the run of the panel ${panel} has ${rows.length} rows, its x ${run.x.length}`);
686
746
  }
687
747
  for (const curve of declaration.curves ?? []) {
688
748
  const spec = run.curves?.[curve.id];
@@ -694,11 +754,11 @@ export function createPanelEndpoint(options) {
694
754
  const width = result.columns.length;
695
755
  const column = result.columns.indexOf(spec.selection);
696
756
  if (column < 0) throw new Error(`the model ${run.model} does not select ${spec.selection}`);
697
- const row = new Array(result.nRows);
698
- for (let r = 0; r < result.nRows; r += 1) {
757
+ const row = new Array(rows.length);
758
+ rows.forEach((r, i) => {
699
759
  const value = result.values[r * width + column] * spec.factor;
700
- row[r] = Number.isFinite(value) ? value : null;
701
- }
760
+ row[i] = Number.isFinite(value) ? value : null;
761
+ });
702
762
  curves[curve.id].push(row);
703
763
  }
704
764
  });
Binary file
Binary file