diff-grok 1.0.9 → 1.0.10
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/.github/ISSUE_TEMPLATE/bug_report.md +29 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +22 -0
- package/.github/workflows/deploy-docs.yml +15 -0
- package/CONTRIBUTING.md +74 -0
- package/README.MD +183 -5
- package/docs/.nojekyll +1 -0
- package/docs/assets/hierarchy.js +1 -0
- package/docs/assets/highlight.css +99 -0
- package/docs/assets/icons.js +18 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +60 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1633 -0
- package/docs/classes/BasicModelPipelineCreator.html +10 -0
- package/docs/classes/Callback.html +7 -0
- package/docs/classes/CyclicModelPipelineCreator.html +10 -0
- package/docs/classes/IterCheckerCallback.html +8 -0
- package/docs/classes/PipelineCreator.html +6 -0
- package/docs/classes/TimeCheckerCallback.html +8 -0
- package/docs/classes/UpdatesModelPipelineCreator.html +10 -0
- package/docs/functions/applyPipeline.html +9 -0
- package/docs/functions/getCallback.html +3 -0
- package/docs/functions/getIVP.html +6 -0
- package/docs/functions/getInputVector.html +2 -0
- package/docs/functions/getIvp2WebWorker.html +6 -0
- package/docs/functions/getJScode.html +6 -0
- package/docs/functions/getOutputNames.html +4 -0
- package/docs/functions/getPipelineCreator.html +7 -0
- package/docs/functions/mrt.html +11 -0
- package/docs/functions/printE5.html +76 -0
- package/docs/functions/printHires.html +76 -0
- package/docs/functions/printOrego.html +66 -0
- package/docs/functions/printPollution.html +163 -0
- package/docs/functions/printRobertson.html +60 -0
- package/docs/functions/printVdpol.html +66 -0
- package/docs/functions/ros34prw.html +8 -0
- package/docs/functions/ros3prw.html +8 -0
- package/docs/functions/solveIvp.html +5 -0
- package/docs/hierarchy.html +1 -0
- package/docs/index.html +683 -0
- package/docs/media/CONTRIBUTING.md +74 -0
- package/docs/media/POLL.png +0 -0
- package/docs/media/VDPOL.png +0 -0
- package/docs/media/acid-prod.gif +0 -0
- package/docs/media/basic-use.js +40 -0
- package/docs/media/basic-use.js.map +1 -0
- package/docs/media/basic-use.ts +44 -0
- package/docs/media/check-methods.js +59 -0
- package/docs/media/check-methods.js.map +1 -0
- package/docs/media/check-methods.ts +81 -0
- package/docs/media/corr-probs.js +132 -0
- package/docs/media/corr-probs.js.map +1 -0
- package/docs/media/corr-probs.ts +155 -0
- package/docs/media/cyclic-model.js +100 -0
- package/docs/media/cyclic-model.js.map +1 -0
- package/docs/media/cyclic-model.ts +110 -0
- package/docs/media/e5.js +36 -0
- package/docs/media/e5.js.map +1 -0
- package/docs/media/e5.ts +36 -0
- package/docs/media/hires.js +39 -0
- package/docs/media/hires.js.map +1 -0
- package/docs/media/hires.ts +40 -0
- package/docs/media/model-updates.js +112 -0
- package/docs/media/model-updates.js.map +1 -0
- package/docs/media/model-updates.ts +122 -0
- package/docs/media/orego.js +24 -0
- package/docs/media/orego.js.map +1 -0
- package/docs/media/orego.ts +25 -0
- package/docs/media/pipeline-use.js +87 -0
- package/docs/media/pipeline-use.js.map +1 -0
- package/docs/media/pipeline-use.ts +98 -0
- package/docs/media/pollution.js +132 -0
- package/docs/media/pollution.js.map +1 -0
- package/docs/media/pollution.ts +133 -0
- package/docs/media/print-benchmark.js +581 -0
- package/docs/media/print-benchmark.js.map +1 -0
- package/docs/media/print-benchmark.ts +594 -0
- package/docs/media/robertson.js +19 -0
- package/docs/media/robertson.js.map +1 -0
- package/docs/media/robertson.ts +19 -0
- package/docs/media/scripting.js +30 -0
- package/docs/media/scripting.js.map +1 -0
- package/docs/media/scripting.ts +35 -0
- package/docs/media/vdpol.js +17 -0
- package/docs/media/vdpol.js.map +1 -0
- package/docs/media/vdpol.ts +17 -0
- package/docs/modules.html +1 -0
- package/docs/types/Arg.html +12 -0
- package/docs/types/DifEqs.html +8 -0
- package/docs/types/Func.html +7 -0
- package/docs/types/IVP.html +40 -0
- package/docs/types/IVP2WebWorker.html +12 -0
- package/docs/types/Input.html +8 -0
- package/docs/types/Loop.html +7 -0
- package/docs/types/ODEs.html +31 -0
- package/docs/types/Output.html +6 -0
- package/docs/types/Pipeline.html +6 -0
- package/docs/types/SolverOptions.html +8 -0
- package/docs/types/Update.html +9 -0
- package/docs/types/Wrapper.html +8 -0
- package/images/POLL.png +0 -0
- package/images/VDPOL.png +0 -0
- package/index.js +2 -2
- package/index.js.map +1 -1
- package/index.ts +6 -5
- package/package.json +2 -1
- package/src/examples/check-methods.js +23 -9
- package/src/examples/check-methods.js.map +1 -1
- package/src/examples/check-methods.ts +28 -9
- package/src/examples/corr-probs.js +3 -1
- package/src/examples/corr-probs.js.map +1 -1
- package/src/examples/corr-probs.ts +7 -3
- package/src/examples/e5.js +6 -0
- package/src/examples/e5.js.map +1 -1
- package/src/examples/e5.ts +7 -0
- package/src/examples/hires.js +10 -0
- package/src/examples/hires.js.map +1 -1
- package/src/examples/hires.ts +11 -0
- package/src/examples/index.js +21 -7
- package/src/examples/index.js.map +1 -1
- package/src/examples/index.ts +23 -7
- package/src/examples/orego.js +5 -0
- package/src/examples/orego.js.map +1 -1
- package/src/examples/orego.ts +6 -0
- package/src/examples/pollution.js +22 -0
- package/src/examples/pollution.js.map +1 -1
- package/src/examples/pollution.ts +23 -0
- package/src/examples/print-benchmark.js +581 -0
- package/src/examples/print-benchmark.js.map +1 -0
- package/src/examples/print-benchmark.ts +594 -0
- package/src/examples/robertson.js +6 -1
- package/src/examples/robertson.js.map +1 -1
- package/src/examples/robertson.ts +7 -1
- package/src/examples/scripting.js +1 -1
- package/src/examples/scripting.js.map +1 -1
- package/src/examples/scripting.ts +1 -1
- package/src/examples/vdpol.js +4 -0
- package/src/examples/vdpol.js.map +1 -1
- package/src/examples/vdpol.ts +5 -0
- package/src/pipeline/basic-pipeline-creator.js +96 -1
- package/src/pipeline/basic-pipeline-creator.js.map +1 -1
- package/src/pipeline/basic-pipeline-creator.ts +97 -1
- package/src/pipeline/loops-pipeline-creator.js +115 -2
- package/src/pipeline/loops-pipeline-creator.js.map +1 -1
- package/src/pipeline/loops-pipeline-creator.ts +116 -2
- package/src/pipeline/output.js +7 -2
- package/src/pipeline/output.js.map +1 -1
- package/src/pipeline/output.ts +7 -2
- package/src/pipeline/pipeline-creator.js.map +1 -1
- package/src/pipeline/pipeline-creator.ts +4 -0
- package/src/pipeline/pipeline.js +193 -2
- package/src/pipeline/pipeline.js.map +1 -1
- package/src/pipeline/pipeline.ts +206 -4
- package/src/pipeline/updates-pipeline-creator.js +130 -2
- package/src/pipeline/updates-pipeline-creator.js.map +1 -1
- package/src/pipeline/updates-pipeline-creator.ts +131 -2
- package/src/scripting-tools/constants.js +18 -4
- package/src/scripting-tools/constants.js.map +1 -1
- package/src/scripting-tools/constants.ts +22 -4
- package/src/scripting-tools/index.js.map +1 -1
- package/src/scripting-tools/index.ts +3 -2
- package/src/scripting-tools/model-error.js +3 -1
- package/src/scripting-tools/model-error.js.map +1 -1
- package/src/scripting-tools/model-error.ts +3 -1
- package/src/scripting-tools/scripting-tools.js +139 -9
- package/src/scripting-tools/scripting-tools.js.map +1 -1
- package/src/scripting-tools/scripting-tools.ts +228 -21
- package/src/solver-tools/callbacks/callback-base.js +2 -0
- package/src/solver-tools/callbacks/callback-base.js.map +1 -1
- package/src/solver-tools/callbacks/callback-base.ts +4 -0
- package/src/solver-tools/callbacks/callback-tools.js +3 -1
- package/src/solver-tools/callbacks/callback-tools.js.map +1 -1
- package/src/solver-tools/callbacks/callback-tools.ts +3 -1
- package/src/solver-tools/callbacks/iter-checker-callback.js +4 -0
- package/src/solver-tools/callbacks/iter-checker-callback.js.map +1 -1
- package/src/solver-tools/callbacks/iter-checker-callback.ts +4 -0
- package/src/solver-tools/callbacks/time-checker-callback.js +4 -0
- package/src/solver-tools/callbacks/time-checker-callback.js.map +1 -1
- package/src/solver-tools/callbacks/time-checker-callback.ts +4 -0
- package/src/solver-tools/index.js +3 -0
- package/src/solver-tools/index.js.map +1 -1
- package/src/solver-tools/index.ts +3 -0
- package/src/solver-tools/mrt-method.js +40 -1
- package/src/solver-tools/mrt-method.js.map +1 -1
- package/src/solver-tools/mrt-method.ts +40 -1
- package/src/solver-tools/ros34prw-method.js +39 -1
- package/src/solver-tools/ros34prw-method.js.map +1 -1
- package/src/solver-tools/ros34prw-method.ts +39 -1
- package/src/solver-tools/ros3prw-method.js +39 -1
- package/src/solver-tools/ros3prw-method.js.map +1 -1
- package/src/solver-tools/ros3prw-method.ts +39 -1
- package/src/solver-tools/solver-defs.js +9 -3
- package/src/solver-tools/solver-defs.js.map +1 -1
- package/src/solver-tools/solver-defs.ts +85 -7
- package/src/worker-tools/scripting.js +95 -1
- package/src/worker-tools/scripting.js.map +1 -1
- package/src/worker-tools/scripting.ts +104 -2
- package/src/worker-tools/solving.js +5 -1
- package/src/worker-tools/solving.js.map +1 -1
- package/src/worker-tools/solving.ts +5 -1
- package/typedoc.json +8 -0
|
@@ -2,17 +2,87 @@
|
|
|
2
2
|
|
|
3
3
|
import {Callback} from './callbacks/callback-base';
|
|
4
4
|
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* Represents the right-hand side function **F(t, y)** of an ODE system.
|
|
7
|
+
*
|
|
8
|
+
* @param t - The independent variable.
|
|
9
|
+
* @param y - The current values of the system's state variables.
|
|
10
|
+
* @param output - A preallocated array where the computed derivatives
|
|
11
|
+
* should be written. This approach avoids unnecessary memory allocations
|
|
12
|
+
* and improves performance.
|
|
13
|
+
*/
|
|
6
14
|
export type Func = (t: number, y: Float64Array, output: Float64Array) => void;
|
|
7
15
|
|
|
8
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Optional configuration settings of the numerical solver of an initial value problem (ODE).
|
|
18
|
+
*
|
|
19
|
+
* @property maxIterations - The maximum number of iterations allowed.
|
|
20
|
+
* @property maxTimeMs - The maximum time in milliseconds allowed for the solver.
|
|
21
|
+
* @property method - The name of the numerical method to use.
|
|
22
|
+
*/
|
|
9
23
|
export type SolverOptions = {
|
|
10
24
|
maxIterations: number,
|
|
11
25
|
maxTimeMs: number,
|
|
12
26
|
method: string,
|
|
13
27
|
};
|
|
14
28
|
|
|
15
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* Specifies a single-stage ODE model.
|
|
31
|
+
*
|
|
32
|
+
* @property name - The name of the model.
|
|
33
|
+
* @property initial - The initial values of the state variables. Can be
|
|
34
|
+
* an array or typed array.
|
|
35
|
+
* @property solutionColNames - Names of the columns in the model's output.
|
|
36
|
+
* @property tolerance - Tolerance for the numerical solver.
|
|
37
|
+
* @property func - The function representing the right-hand side F(t, y)
|
|
38
|
+
* of the ODE system.
|
|
39
|
+
* @property arg - Specification of the independent variable:
|
|
40
|
+
* - name: the variable's name
|
|
41
|
+
* - start: starting value
|
|
42
|
+
* - finish: ending value
|
|
43
|
+
* - step: step size for the grid
|
|
44
|
+
* * Example: Solve a simple initial value problem (IVP)
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* // Example. Solve the following initial value problem:
|
|
48
|
+
* // dx/dt = x + y - t
|
|
49
|
+
* // dy/dt = x * y + t
|
|
50
|
+
* // x(0) = 1
|
|
51
|
+
* // y(0) = -1
|
|
52
|
+
* // on [0, 2] with the step 0.01.
|
|
53
|
+
* import { ODEs, mrt } from 'diff-grok';
|
|
54
|
+
*
|
|
55
|
+
* const task: ODEs = {
|
|
56
|
+
* name: 'Example', // name of your model
|
|
57
|
+
* arg: {
|
|
58
|
+
* name: 't', // name of the argument
|
|
59
|
+
* start: 0, // initial value of the argument
|
|
60
|
+
* finish: 2, // final value of the argument
|
|
61
|
+
* step: 0.01, // solution grid step
|
|
62
|
+
* },
|
|
63
|
+
* initial: [1, -1], // initial values
|
|
64
|
+
* func: (t: number, y: Float64Array, output: Float64Array) => { // right-hand side of the system
|
|
65
|
+
* output[0] = y[0] + y[1] - t; // 1-st equation
|
|
66
|
+
* output[1] = y[0] * y[1] + t; // 2-nd equation
|
|
67
|
+
* },
|
|
68
|
+
* tolerance: 1e-7, // tolerance
|
|
69
|
+
* solutionColNames: ['x', 'y'], // names of solution functions
|
|
70
|
+
* };
|
|
71
|
+
*
|
|
72
|
+
* try {
|
|
73
|
+
* // Solve the problem using the MRT method
|
|
74
|
+
* const solution = mrt(task);
|
|
75
|
+
*
|
|
76
|
+
* // Print a table with the results
|
|
77
|
+
* console.log(task.arg.name, task.solutionColNames[0], task.solutionColNames[1]);
|
|
78
|
+
* const length = solution[0].length;
|
|
79
|
+
* for (let i = 0; i < length; ++i)
|
|
80
|
+
* console.log(solution[0][i], solution[1][i], solution[2][i]);
|
|
81
|
+
* } catch (err) {
|
|
82
|
+
* console.log('Solver failed: ', err instanceof Error ? err.message : 'Unknown problem!');
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
16
86
|
export type ODEs = {
|
|
17
87
|
name: string,
|
|
18
88
|
arg: {
|
|
@@ -76,25 +146,33 @@ export function jacobian(t: number, y: Float64Array, f: Func, eps: number,
|
|
|
76
146
|
}
|
|
77
147
|
}
|
|
78
148
|
|
|
79
|
-
/** Error messeges
|
|
149
|
+
/** Error messeges
|
|
150
|
+
* @internal
|
|
151
|
+
*/
|
|
80
152
|
export enum ERROR_MSG {
|
|
81
153
|
MRT_FAILS = 'The modified Rosenbrock triple method fails',
|
|
82
154
|
ROS3PRW_FAILS = 'The ROS3PRw method fails',
|
|
83
155
|
ROS34PRW_FAILS = 'The ROS34PRw method fails',
|
|
84
156
|
};
|
|
85
157
|
|
|
86
|
-
/** Callback action
|
|
158
|
+
/** Callback action
|
|
159
|
+
* @internal
|
|
160
|
+
*/
|
|
87
161
|
export class CallbackAction extends Error {
|
|
88
162
|
constructor(msg: string) {
|
|
89
163
|
super(msg);
|
|
90
164
|
}
|
|
91
165
|
}
|
|
92
166
|
|
|
93
|
-
/** Default options of the solver
|
|
167
|
+
/** Default options of the solver
|
|
168
|
+
* @internal
|
|
169
|
+
*/
|
|
94
170
|
export enum DEFAULT_OPTIONS {
|
|
95
171
|
SCRIPTING = '{maxIterations: 1}',
|
|
96
172
|
NO_CHECKS = '{ }',
|
|
97
173
|
}
|
|
98
174
|
|
|
99
|
-
/** IVP solving method
|
|
175
|
+
/** IVP solving method
|
|
176
|
+
* @internal
|
|
177
|
+
*/
|
|
100
178
|
export type SolverMethod = (odes: ODEs, callback?: Callback) => Float64Array[];
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable max-len */
|
|
1
2
|
// Scripting tools for solving IVPs in web-workers
|
|
2
3
|
import { MATH_CONSTS, POW_IDX, MATH_FUNCS } from '../scripting-tools/scripting-tools';
|
|
3
4
|
const DEFAULT_METHOD = 'ros34prw';
|
|
@@ -45,7 +46,100 @@ function getFuncMainBody(ivp) {
|
|
|
45
46
|
ivp.deqs.solutionNames.forEach((name, idx) => lines.push(`arguments[2][${idx}] = ${ivp.deqs.equations.get(name)};`));
|
|
46
47
|
return lines.join('\n');
|
|
47
48
|
}
|
|
48
|
-
/** Return IVP-structure to be passed to web-worker
|
|
49
|
+
/** Return IVP-structure to be passed to web-worker
|
|
50
|
+
* @param ivp - initial value problem to be solved
|
|
51
|
+
* @example
|
|
52
|
+
* // Here, we consider modeling queues: https://en.wikipedia.org/wiki/M/M/c_queue
|
|
53
|
+
* import * as DGL from 'diff-grok';
|
|
54
|
+
*
|
|
55
|
+
* // 1. Model specification: the M|M|2|2 model
|
|
56
|
+
* const model = `#name: M|M|2|2
|
|
57
|
+
* #description: Modelling the system with two servers & two waiting places (EXPLAINED)
|
|
58
|
+
*
|
|
59
|
+
* #equations:
|
|
60
|
+
* dp0/dt = -la * p0 + mu * p1
|
|
61
|
+
* dp1/dt = la * p0 - (la + mu) * p1 + 2 * mu * p2
|
|
62
|
+
* dp2/dt = la * p1 - (la + 2 * mu) * p2 + 2 * mu * p3
|
|
63
|
+
* dp3/dt = la * p2 - (la + 2 * mu) * p3 + 2 * mu * p4
|
|
64
|
+
*
|
|
65
|
+
* #expressions:
|
|
66
|
+
* la = 1 / arrival
|
|
67
|
+
* mu = 1 / service
|
|
68
|
+
* p4 = 1 - p0 - p1 - p2 - p3
|
|
69
|
+
* busy = 1 - p0 - p1
|
|
70
|
+
* queue = p3 + 2 * p4
|
|
71
|
+
*
|
|
72
|
+
* #argument: t
|
|
73
|
+
* _t0 = 0 {min: 0; max: 10; caption: start; category: Time; units: min} [Initial time of simulation]
|
|
74
|
+
* _t1 = 60 {min: 20; max: 100; caption: finish; category: Time; units: min} [Final time of simulation]
|
|
75
|
+
* _h = 1 {min: 0.01; max: 0.1; caption: step; category: Time; units: min} [Time step of simulation]
|
|
76
|
+
*
|
|
77
|
+
* #inits:
|
|
78
|
+
* p0 = 1 {min: 0; max: 1; category: Initial state; caption: empty} [Probability that initially there are NO customers]
|
|
79
|
+
* p1 = 0 {min: 0; max: 1; category: Initial state; caption: single} [Probability that initially there is ONE customer]
|
|
80
|
+
* p2 = 0 {min: 0; max: 1; category: Initial state; caption: two} [Probability that initially there are TWO customers]
|
|
81
|
+
* p3 = 0 {min: 0; max: 1; category: Initial state; caption: three} [Probability that initially there are THREE customers]
|
|
82
|
+
*
|
|
83
|
+
* #output:
|
|
84
|
+
* t {caption: Time, min}
|
|
85
|
+
* p0 {caption: P(Empty)}
|
|
86
|
+
* busy {caption: P(Full load)}
|
|
87
|
+
* queue {caption: E(Queue)}
|
|
88
|
+
*
|
|
89
|
+
* #parameters:
|
|
90
|
+
* arrival = 10 {min: 1; max: 100; category: Means; caption: arrival; units: min} [Mean arrival time]
|
|
91
|
+
* service = 100 {min: 1; max: 100; category: Means; caption: service; units: min} [Mean service time]`;
|
|
92
|
+
*
|
|
93
|
+
* // 2. Generate IVP-objects: for the main thread & for computations in webworkers
|
|
94
|
+
* const ivp = DGL.getIVP(model);
|
|
95
|
+
* const ivpWW = DGL.getIvp2WebWorker(ivp);
|
|
96
|
+
*
|
|
97
|
+
* // 3. Perform computations
|
|
98
|
+
* try {
|
|
99
|
+
* // 3.1) Extract names of outputs
|
|
100
|
+
* const outputNames = DGL.getOutputNames(ivp);
|
|
101
|
+
* const outSize = outputNames.length;
|
|
102
|
+
*
|
|
103
|
+
* // 3.2) Set model inputs
|
|
104
|
+
* const inputs = {
|
|
105
|
+
* _t0: 0, // Initial time of simulation
|
|
106
|
+
* _t1: 60, // Final time of simulation
|
|
107
|
+
* _h: 1, // Time step of simulation
|
|
108
|
+
* p0: 1, // Probability that initially there are NO customers
|
|
109
|
+
* p1: 0, // Probability that initially there is ONE customer
|
|
110
|
+
* p2: 0, // Probability that initially there are TWO customers
|
|
111
|
+
* p3: 0, // Probability that initially there are THREE customers
|
|
112
|
+
* arrival: 10, // Mean arrival time
|
|
113
|
+
* service: 100, // Mean service time
|
|
114
|
+
* };
|
|
115
|
+
* const inputVector = DGL.getInputVector(inputs, ivp);
|
|
116
|
+
*
|
|
117
|
+
* // 3.3) Create a pipeline
|
|
118
|
+
* const creator = DGL.getPipelineCreator(ivp);
|
|
119
|
+
* const pipeline = creator.getPipeline(inputVector);
|
|
120
|
+
*
|
|
121
|
+
* // 3.4) Apply pipeline to perform computations
|
|
122
|
+
* const solution = DGL.applyPipeline(pipeline, ivpWW, inputVector);
|
|
123
|
+
* // 3.5) Print results
|
|
124
|
+
*
|
|
125
|
+
* // 3.5.1) Table header
|
|
126
|
+
* let line = '';
|
|
127
|
+
* outputNames.forEach((name) => line += name + ' ');
|
|
128
|
+
* console.log(line);
|
|
129
|
+
*
|
|
130
|
+
* // 3.5.2) Table with solution
|
|
131
|
+
* const length = solution[0].length;
|
|
132
|
+
* for (let i = 0; i < length; ++i) {
|
|
133
|
+
* line = '';
|
|
134
|
+
*
|
|
135
|
+
* for (let j = 0; j < outSize; ++j)
|
|
136
|
+
* line += solution[j][i].toFixed(8) + ' ';
|
|
137
|
+
* console.log(line);
|
|
138
|
+
* }
|
|
139
|
+
* } catch (err) {
|
|
140
|
+
* console.log('Simulation failed: ', err instanceof Error ? err.message : 'Unknown problem!');
|
|
141
|
+
* }
|
|
142
|
+
*/
|
|
49
143
|
export function getIvp2WebWorker(ivp) {
|
|
50
144
|
const paramNames = [];
|
|
51
145
|
if (ivp.params !== null)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scripting.js","sourceRoot":"","sources":["scripting.ts"],"names":[],"mappings":"AAAA,kDAAkD;AAElD,OAAO,EAAM,WAAW,EAAE,OAAO,EAAE,UAAU,EAAC,MAAM,oCAAoC,CAAC;
|
|
1
|
+
{"version":3,"file":"scripting.js","sourceRoot":"","sources":["scripting.ts"],"names":[],"mappings":"AAAA,4BAA4B;AAC5B,kDAAkD;AAElD,OAAO,EAAM,WAAW,EAAE,OAAO,EAAE,UAAU,EAAC,MAAM,oCAAoC,CAAC;AAoBzF,MAAM,cAAc,GAAG,UAAU,CAAC;AAClC,MAAM,mBAAmB,GAAG,KAAK,CAAC;AAElC,4BAA4B;AAC5B,SAAS,aAAa,CAAC,IAAY;IACjC,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI;aACzB,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC;aACpB,OAAO,CAAC,QAAQ,EAAE,UAAU,CAAC;aAC7B,OAAO,CAAC,WAAW,EAAE,aAAa,CAAC;aACnC,OAAO,CAAC,eAAe,EAAE,iBAAiB,CAAC;aAC3C,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CACnB,CAAC;QAEF,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO;YACL,MAAM,EAAE,cAAc;YACtB,SAAS,EAAE,mBAAmB;SAC/B,CAAC;IACJ,CAAC;AACH,CAAC;AAED,4DAA4D;AAC5D,SAAS,eAAe,CAAC,GAAQ;IAC/B,MAAM,KAAK,GAAa,EAAE,CAAC;IAE3B,kBAAkB;IAClB,GAAG,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,GAAG,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AAY,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACnD,CAAC"}
|
|
@@ -1,9 +1,18 @@
|
|
|
1
|
+
/* eslint-disable max-len */
|
|
1
2
|
// Scripting tools for solving IVPs in web-workers
|
|
2
3
|
|
|
3
4
|
import {IVP, MATH_CONSTS, POW_IDX, MATH_FUNCS} from '../scripting-tools/scripting-tools';
|
|
4
5
|
import {SolverOptions} from '../solver-tools/solver-defs';
|
|
5
6
|
|
|
6
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Represents a fully parsed simulation prepared for execution in Web Workers.
|
|
9
|
+
*
|
|
10
|
+
* @property deqsCount - The number of differential equations in the system.
|
|
11
|
+
* @property funcMainBody - The main body of the function implementing the ODE system.
|
|
12
|
+
* @property paramNames - Names of the model parameters.
|
|
13
|
+
* @property solverOpts - Configuration options for the numerical solver (partial).
|
|
14
|
+
* @property tolerance - Tolerance for the numerical solver.
|
|
15
|
+
*/
|
|
7
16
|
export type IVP2WebWorker = {
|
|
8
17
|
tolerance: number,
|
|
9
18
|
solverOpts: Partial<SolverOptions>,
|
|
@@ -68,7 +77,100 @@ function getFuncMainBody(ivp: IVP): string {
|
|
|
68
77
|
return lines.join('\n');
|
|
69
78
|
}
|
|
70
79
|
|
|
71
|
-
/** Return IVP-structure to be passed to web-worker
|
|
80
|
+
/** Return IVP-structure to be passed to web-worker
|
|
81
|
+
* @param ivp - initial value problem to be solved
|
|
82
|
+
* @example
|
|
83
|
+
* // Here, we consider modeling queues: https://en.wikipedia.org/wiki/M/M/c_queue
|
|
84
|
+
* import * as DGL from 'diff-grok';
|
|
85
|
+
*
|
|
86
|
+
* // 1. Model specification: the M|M|2|2 model
|
|
87
|
+
* const model = `#name: M|M|2|2
|
|
88
|
+
* #description: Modelling the system with two servers & two waiting places (EXPLAINED)
|
|
89
|
+
*
|
|
90
|
+
* #equations:
|
|
91
|
+
* dp0/dt = -la * p0 + mu * p1
|
|
92
|
+
* dp1/dt = la * p0 - (la + mu) * p1 + 2 * mu * p2
|
|
93
|
+
* dp2/dt = la * p1 - (la + 2 * mu) * p2 + 2 * mu * p3
|
|
94
|
+
* dp3/dt = la * p2 - (la + 2 * mu) * p3 + 2 * mu * p4
|
|
95
|
+
*
|
|
96
|
+
* #expressions:
|
|
97
|
+
* la = 1 / arrival
|
|
98
|
+
* mu = 1 / service
|
|
99
|
+
* p4 = 1 - p0 - p1 - p2 - p3
|
|
100
|
+
* busy = 1 - p0 - p1
|
|
101
|
+
* queue = p3 + 2 * p4
|
|
102
|
+
*
|
|
103
|
+
* #argument: t
|
|
104
|
+
* _t0 = 0 {min: 0; max: 10; caption: start; category: Time; units: min} [Initial time of simulation]
|
|
105
|
+
* _t1 = 60 {min: 20; max: 100; caption: finish; category: Time; units: min} [Final time of simulation]
|
|
106
|
+
* _h = 1 {min: 0.01; max: 0.1; caption: step; category: Time; units: min} [Time step of simulation]
|
|
107
|
+
*
|
|
108
|
+
* #inits:
|
|
109
|
+
* p0 = 1 {min: 0; max: 1; category: Initial state; caption: empty} [Probability that initially there are NO customers]
|
|
110
|
+
* p1 = 0 {min: 0; max: 1; category: Initial state; caption: single} [Probability that initially there is ONE customer]
|
|
111
|
+
* p2 = 0 {min: 0; max: 1; category: Initial state; caption: two} [Probability that initially there are TWO customers]
|
|
112
|
+
* p3 = 0 {min: 0; max: 1; category: Initial state; caption: three} [Probability that initially there are THREE customers]
|
|
113
|
+
*
|
|
114
|
+
* #output:
|
|
115
|
+
* t {caption: Time, min}
|
|
116
|
+
* p0 {caption: P(Empty)}
|
|
117
|
+
* busy {caption: P(Full load)}
|
|
118
|
+
* queue {caption: E(Queue)}
|
|
119
|
+
*
|
|
120
|
+
* #parameters:
|
|
121
|
+
* arrival = 10 {min: 1; max: 100; category: Means; caption: arrival; units: min} [Mean arrival time]
|
|
122
|
+
* service = 100 {min: 1; max: 100; category: Means; caption: service; units: min} [Mean service time]`;
|
|
123
|
+
*
|
|
124
|
+
* // 2. Generate IVP-objects: for the main thread & for computations in webworkers
|
|
125
|
+
* const ivp = DGL.getIVP(model);
|
|
126
|
+
* const ivpWW = DGL.getIvp2WebWorker(ivp);
|
|
127
|
+
*
|
|
128
|
+
* // 3. Perform computations
|
|
129
|
+
* try {
|
|
130
|
+
* // 3.1) Extract names of outputs
|
|
131
|
+
* const outputNames = DGL.getOutputNames(ivp);
|
|
132
|
+
* const outSize = outputNames.length;
|
|
133
|
+
*
|
|
134
|
+
* // 3.2) Set model inputs
|
|
135
|
+
* const inputs = {
|
|
136
|
+
* _t0: 0, // Initial time of simulation
|
|
137
|
+
* _t1: 60, // Final time of simulation
|
|
138
|
+
* _h: 1, // Time step of simulation
|
|
139
|
+
* p0: 1, // Probability that initially there are NO customers
|
|
140
|
+
* p1: 0, // Probability that initially there is ONE customer
|
|
141
|
+
* p2: 0, // Probability that initially there are TWO customers
|
|
142
|
+
* p3: 0, // Probability that initially there are THREE customers
|
|
143
|
+
* arrival: 10, // Mean arrival time
|
|
144
|
+
* service: 100, // Mean service time
|
|
145
|
+
* };
|
|
146
|
+
* const inputVector = DGL.getInputVector(inputs, ivp);
|
|
147
|
+
*
|
|
148
|
+
* // 3.3) Create a pipeline
|
|
149
|
+
* const creator = DGL.getPipelineCreator(ivp);
|
|
150
|
+
* const pipeline = creator.getPipeline(inputVector);
|
|
151
|
+
*
|
|
152
|
+
* // 3.4) Apply pipeline to perform computations
|
|
153
|
+
* const solution = DGL.applyPipeline(pipeline, ivpWW, inputVector);
|
|
154
|
+
* // 3.5) Print results
|
|
155
|
+
*
|
|
156
|
+
* // 3.5.1) Table header
|
|
157
|
+
* let line = '';
|
|
158
|
+
* outputNames.forEach((name) => line += name + ' ');
|
|
159
|
+
* console.log(line);
|
|
160
|
+
*
|
|
161
|
+
* // 3.5.2) Table with solution
|
|
162
|
+
* const length = solution[0].length;
|
|
163
|
+
* for (let i = 0; i < length; ++i) {
|
|
164
|
+
* line = '';
|
|
165
|
+
*
|
|
166
|
+
* for (let j = 0; j < outSize; ++j)
|
|
167
|
+
* line += solution[j][i].toFixed(8) + ' ';
|
|
168
|
+
* console.log(line);
|
|
169
|
+
* }
|
|
170
|
+
* } catch (err) {
|
|
171
|
+
* console.log('Simulation failed: ', err instanceof Error ? err.message : 'Unknown problem!');
|
|
172
|
+
* }
|
|
173
|
+
*/
|
|
72
174
|
export function getIvp2WebWorker(ivp: IVP): IVP2WebWorker {
|
|
73
175
|
const paramNames: string[] = [];
|
|
74
176
|
if (ivp.params !== null)
|
|
@@ -22,7 +22,11 @@ function checkInputs(ivp, inputs) {
|
|
|
22
22
|
if (expected > inputs.length)
|
|
23
23
|
throw new Error(`Incorrect inputs count, expected: ${expected}, current: ${inputs.length}`);
|
|
24
24
|
}
|
|
25
|
-
/** Return solution of IVP with a specified inputs
|
|
25
|
+
/** Return solution of IVP with a specified inputs
|
|
26
|
+
* @param ivp - initial value problem to be solved
|
|
27
|
+
* @param inputs - vector of input values
|
|
28
|
+
* @returns solution of the given initial value problem
|
|
29
|
+
*/
|
|
26
30
|
export function solveIvp(ivp, inputs) {
|
|
27
31
|
checkInputs(ivp, inputs);
|
|
28
32
|
const nonParamInputsCount = inputs.length - ivp.paramNames.length;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"solving.js","sourceRoot":"","sources":["solving.ts"],"names":[],"mappings":"AAAA,wCAAwC;AAExC,OAAO,EAAe,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAQ,WAAW,EAAC,MAAM,iBAAiB,CAAC;AAExF,OAAO,EAAgB,OAAO,EAAC,MAAM,aAAa,CAAC;AAEnD,MAAM,QAAQ,GAAG,CAAC,CAAC;AACnB,MAAM,SAAS,GAAG,CAAC,CAAC;AACpB,MAAM,QAAQ,GAAG,CAAC,CAAC;AACnB,MAAM,aAAa,GAAG,CAAC,CAAC;AAExB,oCAAoC;AACpC,SAAS,SAAS,CAAC,IAAwB;IACzC,QAAQ,IAAI,EAAE,CAAC;QACf,KAAK,KAAK;YACR,OAAO,GAAG,CAAC;QAEb,K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AAM,CAAC,MAAM,EAAE,CAAC,CAAC;AAChG,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAkB,EAAE,MAAoB;IAC/D,WAAW,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IACzB,MAAM,mBAAmB,GAAG,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC;IAElE,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAC,CAAC;IACjE,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,QAAQ,CAAS,CAAC;IAE5C,MAAM,IAAI,GAAS;QACjB,IAAI,EAAE,EAAE;QACR,GAAG,EAAE;YACH,IAAI,EAAE,EAAE;YACR,KAAK,EAAE,MAAM,CAAC,QAAQ,CAAC;YACvB,MAAM,EAAE,MAAM,CAAC,SAAS,CAAC;YACzB,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC;SACvB;QACD,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,QAAQ,GAAG,CAAC,EAAE,mBAAmB,CAAC;QACxD,IAAI,EAAE,IAAI;QACV,SAAS,EAAE,GAAG,CAAC,SAAS;QACxB,gBAAgB,EAAE,EAAE;KACrB,CAAC;IAEF,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IAChD,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAE7C,OAAO,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;AAChC,CAAC"}
|
|
@@ -31,7 +31,11 @@ function checkInputs(ivp: IVP2WebWorker, inputs: Float64Array): void {
|
|
|
31
31
|
throw new Error(`Incorrect inputs count, expected: ${expected}, current: ${inputs.length}`);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
/** Return solution of IVP with a specified inputs
|
|
34
|
+
/** Return solution of IVP with a specified inputs
|
|
35
|
+
* @param ivp - initial value problem to be solved
|
|
36
|
+
* @param inputs - vector of input values
|
|
37
|
+
* @returns solution of the given initial value problem
|
|
38
|
+
*/
|
|
35
39
|
export function solveIvp(ivp: IVP2WebWorker, inputs: Float64Array): Float64Array[] {
|
|
36
40
|
checkInputs(ivp, inputs);
|
|
37
41
|
const nonParamInputsCount = inputs.length - ivp.paramNames.length;
|