diff-grok 1.0.8 → 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/.github/workflows/test.yaml +19 -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/jest.config.js +12 -0
- package/package.json +8 -3
- 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 -2
- package/src/examples/scripting.js.map +1 -1
- package/src/examples/scripting.ts +2 -3
- 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/tests/correctness.test.js +13 -0
- package/src/tests/correctness.test.js.map +1 -0
- package/src/tests/correctness.test.ts +14 -0
- package/src/tests/performance.test.js +14 -0
- package/src/tests/performance.test.js.map +1 -0
- package/src/tests/performance.test.ts +15 -0
- package/src/tests/pipeline.test.js +10 -0
- package/src/tests/pipeline.test.js.map +1 -0
- package/src/tests/pipeline.test.ts +11 -0
- package/src/tests/test-defs.js +194 -0
- package/src/tests/test-defs.js.map +1 -0
- package/src/tests/test-defs.ts +211 -0
- package/src/tests/test-utils.js +28 -0
- package/src/tests/test-utils.js.map +1 -0
- package/src/tests/test-utils.ts +37 -0
- 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/tsconfig.json +1 -1
- package/typedoc.json +8 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {ODEs, CorrProblem, getIVP, getIvp2WebWorker, getInputVector, getPipelineCreator,
|
|
2
|
+
applyPipeline} from '../../index';
|
|
3
|
+
|
|
4
|
+
/** Return numerical solution error: maximum absolute deviation between approximate & exact solutions */
|
|
5
|
+
export function getError(method: (odes: ODEs) => Float64Array[], corProb: CorrProblem): number {
|
|
6
|
+
let error = 0;
|
|
7
|
+
|
|
8
|
+
// Get numerical solution
|
|
9
|
+
const approxSolution = method(corProb.odes);
|
|
10
|
+
|
|
11
|
+
const exact = corProb.exact;
|
|
12
|
+
|
|
13
|
+
const arg = approxSolution[0];
|
|
14
|
+
|
|
15
|
+
const pointsCount = arg.length;
|
|
16
|
+
const funcsCount = approxSolution.length - 1;
|
|
17
|
+
|
|
18
|
+
// Compute error
|
|
19
|
+
for (let i = 0; i < pointsCount; ++i) {
|
|
20
|
+
const exactSolution = exact(arg[i]);
|
|
21
|
+
|
|
22
|
+
for (let j = 0; j < funcsCount; ++j)
|
|
23
|
+
error = Math.max(error, Math.abs(exactSolution[j] - approxSolution[j + 1][i]));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return error;
|
|
27
|
+
} // getError
|
|
28
|
+
|
|
29
|
+
/** Evaluate model */
|
|
30
|
+
export function evalModel(model: string, inputs: Record<string, number>): Float64Array[] {
|
|
31
|
+
const ivp = getIVP(model);
|
|
32
|
+
const ivpWW = getIvp2WebWorker(ivp);
|
|
33
|
+
const inputVector = getInputVector(inputs, ivp);
|
|
34
|
+
const creator = getPipelineCreator(ivp);
|
|
35
|
+
const pipeline = creator.getPipeline(inputVector);
|
|
36
|
+
return applyPipeline(pipeline, ivpWW, inputVector);
|
|
37
|
+
} // evalModel
|
|
@@ -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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,CAAC,CAAC,CAAC;IAEzD,MAAM,KAAK,GAAkB;QAC3B,SAAS,EAAE,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC;QAChC,UAAU,EAAE,aAAa,CAAC,GAAG,CAAC,cAAc,CAAC;QAC7C,YAAY,EAAE,eAAe,CAAC,GAAG,CAAC;QAClC,UAAU,EAAE,UAAU;QACtB,SAAS,EAAE,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM;KACzC,CAAC;IAEF,OAAO,KAAK,CAAC;AACf,CAAC;AAED,uFAAuF;AACvF,MAAM,UAAU,OAAO,CAAC,GAAkB,EAAE,SAAuB;IACjE,MAAM,KAAK,GAAa,EAAE,CAAC;IAE3B,GAAG,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,IAAI,MAAM,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;IAExF,OAAO,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,YAAY,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,KAAK,SAAS;YACZ,OAAO,OAAO,CAAC;QAEjB;YACE,OAAO,QAAQ,CAAC;IAClB,CAAC;AACH,CAAC;AAED,gDAAgD;AAChD,SAAS,WAAW,CAAC,GAAkB,EAAE,MAAoB;IAC3D,MAAM,QAAQ,GAAG,aAAa,GAAG,GAAG,CAAC,SAAS,GAAG,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC;IAEvE,IAAI,QAAQ,GAAG,MAAM,CAAC,MAAM;QAC1B,MAAM,IAAI,KAAK,CAAC,qCAAqC,QAAQ,cAAc,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;AAChG,CAAC;AAED
|
|
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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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;
|
package/tsconfig.json
CHANGED
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
|
49
49
|
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
|
50
50
|
// "typeRoots": [], /* List of folders to include type definitions from. */
|
|
51
|
-
|
|
51
|
+
"types": ["jest", "node"], /* Type declaration files to be included in compilation. */
|
|
52
52
|
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
|
53
53
|
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
|
54
54
|
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|