diff-grok 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +45 -0
- package/.github/scripts/bump-js-api-version.sh +106 -0
- package/.github/scripts/check-output.sh +20 -0
- package/.github/scripts/iac-cfn-deploy.sh +90 -0
- package/.github/scripts/slack_api_request.sh +65 -0
- package/.github/scripts/test_package.sh +270 -0
- package/.github/workflows/libraries.yaml +305 -0
- package/LICENSE +21 -0
- package/README.MD +396 -0
- package/eslintrc.json +45 -0
- package/images/acid-prod.gif +0 -0
- package/index.js +6 -0
- package/index.js.map +1 -0
- package/index.ts +15 -0
- package/package.json +45 -0
- package/src/examples/basic-use.js +40 -0
- package/src/examples/basic-use.js.map +1 -0
- package/src/examples/basic-use.ts +44 -0
- package/src/examples/check-methods.js +45 -0
- package/src/examples/check-methods.js.map +1 -0
- package/src/examples/check-methods.ts +62 -0
- package/src/examples/corr-probs.js +130 -0
- package/src/examples/corr-probs.js.map +1 -0
- package/src/examples/corr-probs.ts +151 -0
- package/src/examples/cyclic-model.js +100 -0
- package/src/examples/cyclic-model.js.map +1 -0
- package/src/examples/cyclic-model.ts +110 -0
- package/src/examples/e5.js +30 -0
- package/src/examples/e5.js.map +1 -0
- package/src/examples/e5.ts +29 -0
- package/src/examples/hires.js +29 -0
- package/src/examples/hires.js.map +1 -0
- package/src/examples/hires.ts +29 -0
- package/src/examples/index.js +17 -0
- package/src/examples/index.js.map +1 -0
- package/src/examples/index.ts +18 -0
- package/src/examples/model-updates.js +112 -0
- package/src/examples/model-updates.js.map +1 -0
- package/src/examples/model-updates.ts +122 -0
- package/src/examples/orego.js +19 -0
- package/src/examples/orego.js.map +1 -0
- package/src/examples/orego.ts +19 -0
- package/src/examples/pipeline-use.js +87 -0
- package/src/examples/pipeline-use.js.map +1 -0
- package/src/examples/pipeline-use.ts +98 -0
- package/src/examples/pollution.js +110 -0
- package/src/examples/pollution.js.map +1 -0
- package/src/examples/pollution.ts +110 -0
- package/src/examples/robertson.js +14 -0
- package/src/examples/robertson.js.map +1 -0
- package/src/examples/robertson.ts +13 -0
- package/src/examples/scripting.js +31 -0
- package/src/examples/scripting.js.map +1 -0
- package/src/examples/scripting.ts +36 -0
- package/src/examples/vdpol.js +13 -0
- package/src/examples/vdpol.js.map +1 -0
- package/src/examples/vdpol.ts +12 -0
- package/src/pipeline/basic-pipeline-creator.js +21 -0
- package/src/pipeline/basic-pipeline-creator.js.map +1 -0
- package/src/pipeline/basic-pipeline-creator.ts +24 -0
- package/src/pipeline/constants.js +21 -0
- package/src/pipeline/constants.js.map +1 -0
- package/src/pipeline/constants.ts +21 -0
- package/src/pipeline/index.js +8 -0
- package/src/pipeline/index.js.map +1 -0
- package/src/pipeline/index.ts +7 -0
- package/src/pipeline/input.js +45 -0
- package/src/pipeline/input.js.map +1 -0
- package/src/pipeline/input.ts +55 -0
- package/src/pipeline/loops-pipeline-creator.js +107 -0
- package/src/pipeline/loops-pipeline-creator.js.map +1 -0
- package/src/pipeline/loops-pipeline-creator.ts +138 -0
- package/src/pipeline/output.js +146 -0
- package/src/pipeline/output.js.map +1 -0
- package/src/pipeline/output.ts +196 -0
- package/src/pipeline/pipeline-creator.js +10 -0
- package/src/pipeline/pipeline-creator.js.map +1 -0
- package/src/pipeline/pipeline-creator.ts +16 -0
- package/src/pipeline/pipeline.js +71 -0
- package/src/pipeline/pipeline.js.map +1 -0
- package/src/pipeline/pipeline.ts +104 -0
- package/src/pipeline/updates-pipeline-creator.js +98 -0
- package/src/pipeline/updates-pipeline-creator.js.map +1 -0
- package/src/pipeline/updates-pipeline-creator.ts +125 -0
- package/src/scripting-tools/constants.js +53 -0
- package/src/scripting-tools/constants.js.map +1 -0
- package/src/scripting-tools/constants.ts +51 -0
- package/src/scripting-tools/index.js +4 -0
- package/src/scripting-tools/index.js.map +1 -0
- package/src/scripting-tools/index.ts +7 -0
- package/src/scripting-tools/model-error.js +17 -0
- package/src/scripting-tools/model-error.js.map +1 -0
- package/src/scripting-tools/model-error.ts +19 -0
- package/src/scripting-tools/scripting-tools.js +1032 -0
- package/src/scripting-tools/scripting-tools.js.map +1 -0
- package/src/scripting-tools/scripting-tools.ts +1375 -0
- package/src/solver-tools/callbacks/callback-base.js +10 -0
- package/src/solver-tools/callbacks/callback-base.js.map +1 -0
- package/src/solver-tools/callbacks/callback-base.ts +8 -0
- package/src/solver-tools/callbacks/callback-tools.js +14 -0
- package/src/solver-tools/callbacks/callback-tools.js.map +1 -0
- package/src/solver-tools/callbacks/callback-tools.ts +20 -0
- package/src/solver-tools/callbacks/iter-checker-callback.js +16 -0
- package/src/solver-tools/callbacks/iter-checker-callback.js.map +1 -0
- package/src/solver-tools/callbacks/iter-checker-callback.ts +21 -0
- package/src/solver-tools/callbacks/time-checker-callback.js +17 -0
- package/src/solver-tools/callbacks/time-checker-callback.js.map +1 -0
- package/src/solver-tools/callbacks/time-checker-callback.ts +19 -0
- package/src/solver-tools/index.js +6 -0
- package/src/solver-tools/index.js.map +1 -0
- package/src/solver-tools/index.ts +6 -0
- package/src/solver-tools/lin-alg-tools.js +56 -0
- package/src/solver-tools/lin-alg-tools.js.map +1 -0
- package/src/solver-tools/lin-alg-tools.ts +75 -0
- package/src/solver-tools/mrt-method.js +210 -0
- package/src/solver-tools/mrt-method.js.map +1 -0
- package/src/solver-tools/mrt-method.ts +269 -0
- package/src/solver-tools/ros34prw-method.js +257 -0
- package/src/solver-tools/ros34prw-method.js.map +1 -0
- package/src/solver-tools/ros34prw-method.ts +335 -0
- package/src/solver-tools/ros3prw-method.js +233 -0
- package/src/solver-tools/ros3prw-method.js.map +1 -0
- package/src/solver-tools/ros3prw-method.ts +304 -0
- package/src/solver-tools/solver-defs.js +58 -0
- package/src/solver-tools/solver-defs.js.map +1 -0
- package/src/solver-tools/solver-defs.ts +100 -0
- package/src/worker-tools/index.js +3 -0
- package/src/worker-tools/index.js.map +1 -0
- package/src/worker-tools/index.ts +2 -0
- package/src/worker-tools/scripting.js +68 -0
- package/src/worker-tools/scripting.js.map +1 -0
- package/src/worker-tools/scripting.ts +95 -0
- package/src/worker-tools/solving.js +48 -0
- package/src/worker-tools/solving.js.map +1 -0
- package/src/worker-tools/solving.ts +60 -0
- package/tsconfig.json +71 -0
|
@@ -0,0 +1,1032 @@
|
|
|
1
|
+
/* eslint-disable max-len */
|
|
2
|
+
/* Scripting tools for the Initial Value Problem (IVP) solver:
|
|
3
|
+
- parser of formulas defining IVP;
|
|
4
|
+
- JS-script generator.
|
|
5
|
+
|
|
6
|
+
The parser processes IVP formulas given in the declarative form (see README.md).
|
|
7
|
+
|
|
8
|
+
JS-script generator creates DATAGROK JavaScript script: annotation & code.
|
|
9
|
+
*/
|
|
10
|
+
import { CONTROL_TAG, CONTROL_TAG_LEN, CONTROL_EXPR, LOOP, UPDATE, MAX_LINE_CHART, SOLVER_OPTIONS_RANGES, TINY, STEP_RATIO } from './constants';
|
|
11
|
+
import { ModelError } from './model-error';
|
|
12
|
+
// Scripting specific constants
|
|
13
|
+
export const CONTROL_SEP = ':';
|
|
14
|
+
const COMMA = ',';
|
|
15
|
+
const EQUAL_SIGN = '=';
|
|
16
|
+
const DIV_SIGN = '/';
|
|
17
|
+
const SERVICE = '_';
|
|
18
|
+
export const BRACE_OPEN = '{';
|
|
19
|
+
export const BRACE_CLOSE = '}';
|
|
20
|
+
export const BRACKET_OPEN = '[';
|
|
21
|
+
export const BRACKET_CLOSE = ']';
|
|
22
|
+
export const ANNOT_SEPAR = ';';
|
|
23
|
+
const DEFAULT_TOL = '0.00005';
|
|
24
|
+
export const DEFAULT_SOLVER_SETTINGS = '{}';
|
|
25
|
+
const COMMENT_SEQ = '//';
|
|
26
|
+
export const STAGE_COL_NAME = `_Stage`;
|
|
27
|
+
const INCEPTION = 'Inception';
|
|
28
|
+
/** Elementary math tools */
|
|
29
|
+
export const MATH_FUNCS = ['pow', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'sqrt', 'exp', 'log', 'sinh', 'cosh', 'tanh'];
|
|
30
|
+
export const POW_IDX = MATH_FUNCS.indexOf('pow');
|
|
31
|
+
export const MATH_CONSTS = ['PI', 'E'];
|
|
32
|
+
/** Default meta */
|
|
33
|
+
const defaultMetas = `//meta.runOnOpen: true
|
|
34
|
+
//meta.runOnInput: true
|
|
35
|
+
//meta.features: {"sens-analysis": true, "fitting": true}`;
|
|
36
|
+
/** Input keys of Arg */
|
|
37
|
+
export const ARG_INPUT_KEYS = ['initial', 'final', 'step'];
|
|
38
|
+
/** Scripting specific constants */
|
|
39
|
+
export var SCRIPTING;
|
|
40
|
+
(function (SCRIPTING) {
|
|
41
|
+
SCRIPTING["ARG_NAME"] = "name";
|
|
42
|
+
SCRIPTING["COUNT"] = "count";
|
|
43
|
+
SCRIPTING["DURATION"] = "duration";
|
|
44
|
+
})(SCRIPTING || (SCRIPTING = {}));
|
|
45
|
+
;
|
|
46
|
+
/** Help links for model errors */
|
|
47
|
+
var ERROR_LINK;
|
|
48
|
+
(function (ERROR_LINK) {
|
|
49
|
+
ERROR_LINK["MAIN_DOCS"] = "/help/compute/diff-studio";
|
|
50
|
+
ERROR_LINK["CORE_BLOCKS"] = "/help/compute/diff-studio#core-blocks";
|
|
51
|
+
ERROR_LINK["COMPS_SYNTAX"] = "/help/compute/diff-studio#model-components-and-syntax";
|
|
52
|
+
ERROR_LINK["LOOP"] = "/help/compute/diff-studio#cyclic-processes";
|
|
53
|
+
ERROR_LINK["UPDATE"] = "/help/compute/diff-studio#multistage-processes";
|
|
54
|
+
ERROR_LINK["UI_OPTS"] = "/help/compute/diff-studio#user-interface-options";
|
|
55
|
+
ERROR_LINK["LOOP_VS_UPDATE"] = "/help/compute/diff-studio#advanced-features";
|
|
56
|
+
ERROR_LINK["SOLVER_CONFIG"] = "/help/compute/diff-studio#solver-configuration";
|
|
57
|
+
ERROR_LINK["MODEL_PARAMS"] = "/help/compute/diff-studio#model-parameters";
|
|
58
|
+
})(ERROR_LINK || (ERROR_LINK = {}));
|
|
59
|
+
;
|
|
60
|
+
/** Specific error messages */
|
|
61
|
+
var ERROR_MSG;
|
|
62
|
+
(function (ERROR_MSG) {
|
|
63
|
+
ERROR_MSG["CTRL_EXPR"] = "Unsupported control expression with the tag **\"#\"**";
|
|
64
|
+
ERROR_MSG["ARG"] = "'The **#argument** block must consist of 3 lines specifying initial and final time, and solution grid step.";
|
|
65
|
+
ERROR_MSG["LOOP"] = "The **#loop** block must contain at least one line.";
|
|
66
|
+
ERROR_MSG["COUNT"] = "Incorrect loop count";
|
|
67
|
+
ERROR_MSG["LOOP_VS_UPDATE"] = "The **#loop** and **'#update'** blocks cannot be used simultaneously.";
|
|
68
|
+
ERROR_MSG["UPDATE_LINES_COUNT"] = "The **'#update'** block must contain at least one line.";
|
|
69
|
+
ERROR_MSG["DURATION"] = "Incorrect update duration";
|
|
70
|
+
ERROR_MSG["BRACES"] = " Missing one of the braces (**{**, **}**).";
|
|
71
|
+
ERROR_MSG["COLON"] = "Incorrect position of **\":\"**.";
|
|
72
|
+
ERROR_MSG["CASE_INSENS"] = "Non-unique name (case-insensitive): use different caption for ";
|
|
73
|
+
ERROR_MSG["MISSING_INIT"] = "Correct the **#inits** block.";
|
|
74
|
+
ERROR_MSG["UNDEF_NAME"] = "Model name missing. Specify the model name in the **#name** block.";
|
|
75
|
+
ERROR_MSG["UNDEF_DEQS"] = "Differential equation(s) are required for this model. Add equation(s) under the **#equations** block.";
|
|
76
|
+
ERROR_MSG["UNDEF_INITS"] = "Initial conditions are required for this model. Add initial conditions under the **#inits** block.";
|
|
77
|
+
ERROR_MSG["UNDEF_ARG"] = "Argument specification is required for this model. Specify an argument, its range, and a grid step in the **#argument** block.";
|
|
78
|
+
ERROR_MSG["CORRECT_ARG_LIM"] = "Correct limits in the **#argument** block.";
|
|
79
|
+
ERROR_MSG["INTERVAL"] = "Incorrect range for";
|
|
80
|
+
ERROR_MSG["NEGATIVE_STEP"] = "Solution grid step must be positive. Correct the **#argument** block.";
|
|
81
|
+
ERROR_MSG["INCOR_STEP"] = "Grid step must less than the length of solution interval. Correct the **#argument** block.";
|
|
82
|
+
ERROR_MSG["MISS_COLON"] = "Missing **\":\"**";
|
|
83
|
+
ERROR_MSG["NAN"] = "is not a valid number. Correct the line";
|
|
84
|
+
ERROR_MSG["SERVICE_START"] = "Variable names must not begin with **\"_\"**.";
|
|
85
|
+
ERROR_MSG["REUSE_NAME"] = "Variable reuse (case-insensitive): rename ";
|
|
86
|
+
ERROR_MSG["SOLVER"] = "Incorrect solver options. Correct the **#meta.solver** line.";
|
|
87
|
+
})(ERROR_MSG || (ERROR_MSG = {})); // ERROR_MSG
|
|
88
|
+
/** Datagrok annotations */
|
|
89
|
+
var ANNOT;
|
|
90
|
+
(function (ANNOT) {
|
|
91
|
+
ANNOT["NAME"] = "//name:";
|
|
92
|
+
ANNOT["DESCR"] = "//description:";
|
|
93
|
+
ANNOT["TAGS"] = "//tags:";
|
|
94
|
+
ANNOT["LANG"] = "//language: javascript";
|
|
95
|
+
ANNOT["DOUBLE_INPUT"] = "//input: double";
|
|
96
|
+
ANNOT["INT_INPUT"] = "//input: int";
|
|
97
|
+
ANNOT["OUTPUT"] = "//output: dataframe df";
|
|
98
|
+
ANNOT["EDITOR"] = "//editor: Compute:RichFunctionViewEditor";
|
|
99
|
+
ANNOT["SIDEBAR"] = "//sidebar: @compute";
|
|
100
|
+
ANNOT["CAPTION"] = "caption:";
|
|
101
|
+
ANNOT["ARG_INIT"] = "{caption: Initial; category: Argument}";
|
|
102
|
+
ANNOT["ARG_FIN"] = "{caption: Final; category: Argument}";
|
|
103
|
+
ANNOT["ARG_STEP"] = "{caption: Step; category: Argument}";
|
|
104
|
+
ANNOT["INITS"] = "category: Initial values";
|
|
105
|
+
ANNOT["PARAMS"] = "category: Parameters";
|
|
106
|
+
})(ANNOT || (ANNOT = {}));
|
|
107
|
+
/** JS-scripting components */
|
|
108
|
+
var SCRIPT;
|
|
109
|
+
(function (SCRIPT) {
|
|
110
|
+
SCRIPT["CONSTS"] = "// constants";
|
|
111
|
+
SCRIPT["ODE_COM"] = "// the problem definition";
|
|
112
|
+
SCRIPT["ODE"] = "let odes = {";
|
|
113
|
+
SCRIPT["SOLVER_COM"] = "// solve the problem";
|
|
114
|
+
SCRIPT["SOLVER"] = "const solver = await grok.functions.eval('DiffStudio:solveEquations');";
|
|
115
|
+
SCRIPT["PREPARE"] = "let call = solver.prepare({problem: odes, options: opts});";
|
|
116
|
+
SCRIPT["CALL"] = "await call.call();";
|
|
117
|
+
SCRIPT["OUTPUT"] = "let df = call.getParamValue('df');";
|
|
118
|
+
SCRIPT["SPACE2"] = " ";
|
|
119
|
+
SCRIPT["SPACE4"] = " ";
|
|
120
|
+
SCRIPT["SPACE6"] = " ";
|
|
121
|
+
SCRIPT["SPACE8"] = " ";
|
|
122
|
+
SCRIPT["FUNC_VALS"] = "// extract function values";
|
|
123
|
+
SCRIPT["EVAL_EXPR"] = "// evaluate expressions";
|
|
124
|
+
SCRIPT["COMP_OUT"] = "// compute output";
|
|
125
|
+
SCRIPT["MATH_FUNC_COM"] = "// used Math-functions";
|
|
126
|
+
SCRIPT["MATH_CONST_COM"] = "// used Math-constants";
|
|
127
|
+
SCRIPT["ONE_STAGE_COM"] = "\n// one stage solution";
|
|
128
|
+
SCRIPT["ONE_STAGE_BEGIN"] = "let _oneStage = async (";
|
|
129
|
+
SCRIPT["ONE_STAGE_END"] = ") => {";
|
|
130
|
+
SCRIPT["ASYNC_OUTPUT"] = "let df = await _oneStage(";
|
|
131
|
+
SCRIPT["RETURN_OUTPUT"] = "return call.getParamValue('df');";
|
|
132
|
+
SCRIPT["EMPTY_OUTPUT"] = "let df = DG.DataFrame.create();";
|
|
133
|
+
SCRIPT["APPEND"] = "df.append(";
|
|
134
|
+
SCRIPT["SOLUTION_DF_COM"] = "// solution dataframe";
|
|
135
|
+
SCRIPT["LOOP_INTERVAL_COM"] = "// loop interval";
|
|
136
|
+
SCRIPT["LOOP_INTERVAL"] = "_interval";
|
|
137
|
+
SCRIPT["LAST_IDX"] = "_lastIdx";
|
|
138
|
+
SCRIPT["UPDATE_COM"] = "// update ";
|
|
139
|
+
SCRIPT["CUSTOM_OUTPUT_COM"] = "// create custom output";
|
|
140
|
+
SCRIPT["CUSTOM_COLUMNS"] = "let _columns = [";
|
|
141
|
+
SCRIPT["ONE_STAGE"] = "await _oneStage(";
|
|
142
|
+
SCRIPT["SEGMENT_COM"] = "// add segment category";
|
|
143
|
+
})(SCRIPT || (SCRIPT = {}));
|
|
144
|
+
/** Get start of the problem skipping note-lines */
|
|
145
|
+
function getStartOfProblemDef(lines) {
|
|
146
|
+
const linesCount = lines.length;
|
|
147
|
+
let idx = 0;
|
|
148
|
+
while (!lines[idx].startsWith(CONTROL_TAG)) {
|
|
149
|
+
++idx;
|
|
150
|
+
if (idx === linesCount)
|
|
151
|
+
throw new ModelError(ERROR_MSG.UNDEF_NAME, ERROR_LINK.CORE_BLOCKS);
|
|
152
|
+
}
|
|
153
|
+
return idx;
|
|
154
|
+
}
|
|
155
|
+
/** Get limits of blocks specifying IVP */
|
|
156
|
+
function getBlocks(lines, start) {
|
|
157
|
+
const linesCount = lines.length;
|
|
158
|
+
let beg = start;
|
|
159
|
+
let idx = start + 1;
|
|
160
|
+
const blocks = [];
|
|
161
|
+
while (true) {
|
|
162
|
+
if (idx === linesCount) {
|
|
163
|
+
blocks.push({ begin: beg, end: idx });
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
if (lines[idx].startsWith(CONTROL_TAG)) {
|
|
167
|
+
blocks.push({ begin: beg, end: idx });
|
|
168
|
+
beg = idx;
|
|
169
|
+
}
|
|
170
|
+
++idx;
|
|
171
|
+
}
|
|
172
|
+
return blocks;
|
|
173
|
+
}
|
|
174
|
+
/** Concatenate multi-line formulas & expressions */
|
|
175
|
+
function concatMultilineFormulas(source) {
|
|
176
|
+
const res = [source[0]];
|
|
177
|
+
let idxRes = 0;
|
|
178
|
+
let idxSource = 1;
|
|
179
|
+
const size = source.length;
|
|
180
|
+
while (idxSource < size) {
|
|
181
|
+
const str = source[idxSource];
|
|
182
|
+
if (!str.includes(EQUAL_SIGN))
|
|
183
|
+
res[idxRes] += str;
|
|
184
|
+
else {
|
|
185
|
+
res.push(str);
|
|
186
|
+
++idxRes;
|
|
187
|
+
}
|
|
188
|
+
++idxSource;
|
|
189
|
+
}
|
|
190
|
+
return res;
|
|
191
|
+
}
|
|
192
|
+
/** Get indeces of math functions used in the current text */
|
|
193
|
+
function getUsedMathIds(text, mathIds) {
|
|
194
|
+
const res = [];
|
|
195
|
+
const size = mathIds.length;
|
|
196
|
+
for (let i = 0; i < size; ++i) {
|
|
197
|
+
if (text.includes(`${mathIds[i]}`))
|
|
198
|
+
res.push(i);
|
|
199
|
+
}
|
|
200
|
+
return res;
|
|
201
|
+
}
|
|
202
|
+
/** Get differential equations */
|
|
203
|
+
function getDifEquations(lines) {
|
|
204
|
+
const deqs = new Map();
|
|
205
|
+
const names = [];
|
|
206
|
+
let divIdx = 0;
|
|
207
|
+
let eqIdx = 0;
|
|
208
|
+
for (const line of lines) {
|
|
209
|
+
if (line === undefined)
|
|
210
|
+
throw new ModelError(ERROR_MSG.UNDEF_DEQS, ERROR_LINK.CORE_BLOCKS, CONTROL_EXPR.DIF_EQ);
|
|
211
|
+
divIdx = line.indexOf(DIV_SIGN);
|
|
212
|
+
eqIdx = line.indexOf(EQUAL_SIGN);
|
|
213
|
+
const name = line.slice(line.indexOf('d') + 1, divIdx).replace(/[() ]/g, '');
|
|
214
|
+
names.push(name);
|
|
215
|
+
deqs.set(name, line.slice(eqIdx + 1).trim());
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
equations: deqs,
|
|
219
|
+
solutionNames: names,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
/** Get expressions of IVP */
|
|
223
|
+
function getExpressions(lines) {
|
|
224
|
+
const exprs = new Map();
|
|
225
|
+
let eqIdx = 0;
|
|
226
|
+
for (const line of lines) {
|
|
227
|
+
if (line === undefined)
|
|
228
|
+
break;
|
|
229
|
+
eqIdx = line.indexOf(EQUAL_SIGN);
|
|
230
|
+
exprs.set(line.slice(0, eqIdx).replaceAll(' ', ''), line.slice(eqIdx + 1).trim());
|
|
231
|
+
}
|
|
232
|
+
return exprs;
|
|
233
|
+
}
|
|
234
|
+
/** Get input specification */
|
|
235
|
+
function getInput(line) {
|
|
236
|
+
const str = line.slice(line.indexOf(EQUAL_SIGN) + 1).trim();
|
|
237
|
+
const braceIdx = str.indexOf(BRACE_OPEN);
|
|
238
|
+
let val;
|
|
239
|
+
if (braceIdx === -1) { // no annotation
|
|
240
|
+
val = Number(str);
|
|
241
|
+
// Check right-hand side
|
|
242
|
+
if (isNaN(val))
|
|
243
|
+
throw new ModelError(`**${str}** ${ERROR_MSG.NAN}\n **${line}**.`, ERROR_LINK.COMPS_SYNTAX, line);
|
|
244
|
+
return {
|
|
245
|
+
value: val,
|
|
246
|
+
annot: null,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
// There is an annotation
|
|
250
|
+
val = Number(str.slice(0, braceIdx));
|
|
251
|
+
// Check right-hand side
|
|
252
|
+
if (isNaN(val))
|
|
253
|
+
throw new ModelError(`**${str.slice(0, braceIdx)}** ${ERROR_MSG.NAN}: **${line}**`, ERROR_LINK.COMPS_SYNTAX, line);
|
|
254
|
+
return {
|
|
255
|
+
value: val,
|
|
256
|
+
annot: str.slice(braceIdx),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
/** Get argument (independent variable) of IVP */
|
|
260
|
+
function getArg(lines) {
|
|
261
|
+
if (lines.length !== 4)
|
|
262
|
+
throw new ModelError(ERROR_MSG.ARG, ERROR_LINK.CORE_BLOCKS, CONTROL_EXPR.ARG);
|
|
263
|
+
const sepIdx = lines[0].indexOf(CONTROL_SEP);
|
|
264
|
+
if (sepIdx === -1)
|
|
265
|
+
throw new ModelError(`${ERROR_MSG.MISS_COLON}. Correct the line **${lines[0]}**`, ERROR_LINK.CORE_BLOCKS, lines[0]);
|
|
266
|
+
const commaIdx = lines[0].indexOf(COMMA);
|
|
267
|
+
return {
|
|
268
|
+
name: ((commaIdx > -1) ? lines[0].slice(sepIdx + 1, commaIdx) : lines[0].slice(sepIdx + 1)).trim(),
|
|
269
|
+
initial: getInput(lines[1]),
|
|
270
|
+
final: getInput(lines[2]),
|
|
271
|
+
step: getInput(lines[3]),
|
|
272
|
+
updateName: (commaIdx > -1) ? lines[0].slice(commaIdx + 1).trim() : undefined,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
/** Get equalities specification */
|
|
276
|
+
function getEqualities(lines, begin, end) {
|
|
277
|
+
const source = concatMultilineFormulas(lines.slice(begin, end));
|
|
278
|
+
const eqs = new Map();
|
|
279
|
+
let eqIdx = 0;
|
|
280
|
+
for (const line of source) {
|
|
281
|
+
if (line === undefined)
|
|
282
|
+
break;
|
|
283
|
+
eqIdx = line.indexOf(EQUAL_SIGN);
|
|
284
|
+
eqs.set(line.slice(0, eqIdx).replaceAll(' ', ''), getInput(line));
|
|
285
|
+
}
|
|
286
|
+
return eqs;
|
|
287
|
+
}
|
|
288
|
+
/** Get loop specification */
|
|
289
|
+
function getLoop(lines, begin, end) {
|
|
290
|
+
if (begin >= end)
|
|
291
|
+
throw new ModelError(ERROR_MSG.LOOP, ERROR_LINK.LOOP, CONTROL_EXPR.LOOP);
|
|
292
|
+
const source = concatMultilineFormulas(lines.slice(begin, end));
|
|
293
|
+
const size = source.length;
|
|
294
|
+
if (size < LOOP.MIN_LINES_COUNT)
|
|
295
|
+
throw new ModelError(ERROR_MSG.LOOP, ERROR_LINK.LOOP, CONTROL_EXPR.LOOP);
|
|
296
|
+
const count = getInput(source[LOOP.COUNT_IDX]);
|
|
297
|
+
if (count.value < LOOP.MIN_COUNT)
|
|
298
|
+
throw new ModelError(`${ERROR_MSG.COUNT}: **${count.value}**.`, ERROR_LINK.LOOP, source[LOOP.COUNT_IDX]);
|
|
299
|
+
return { count: count, updates: source.slice(LOOP.COUNT_IDX + 1) };
|
|
300
|
+
}
|
|
301
|
+
/** Get update specification */
|
|
302
|
+
function getUpdate(lines, begin, end) {
|
|
303
|
+
const colonIdx = lines[begin].indexOf(CONTROL_SEP);
|
|
304
|
+
if (colonIdx === -1) {
|
|
305
|
+
throw new ModelError(`${ERROR_MSG.MISS_COLON}. Correct the line: **${lines[begin]}**`, ERROR_LINK.UPDATE, lines[begin]);
|
|
306
|
+
}
|
|
307
|
+
const source = concatMultilineFormulas(lines.slice(begin + 1, end));
|
|
308
|
+
const size = source.length;
|
|
309
|
+
if (size < UPDATE.MIN_LINES_COUNT)
|
|
310
|
+
throw new ModelError(ERROR_MSG.UPDATE_LINES_COUNT, ERROR_LINK.UPDATE, CONTROL_EXPR.UPDATE);
|
|
311
|
+
if (source[UPDATE.DURATION_IDX] == undefined)
|
|
312
|
+
throw new ModelError(ERROR_MSG.UPDATE_LINES_COUNT, ERROR_LINK.UPDATE, CONTROL_EXPR.UPDATE);
|
|
313
|
+
const eqIdx = source[UPDATE.DURATION_IDX].indexOf(EQUAL_SIGN);
|
|
314
|
+
if (eqIdx === -1) {
|
|
315
|
+
throw new ModelError(`Missing **${EQUAL_SIGN}**. Correct the line: **${source[UPDATE.DURATION_IDX]}**`, ERROR_LINK.UPDATE, source[UPDATE.DURATION_IDX]);
|
|
316
|
+
}
|
|
317
|
+
return {
|
|
318
|
+
name: lines[begin].slice(colonIdx + 1),
|
|
319
|
+
durationFormula: source[UPDATE.DURATION_IDX].slice(eqIdx + 1).trim(),
|
|
320
|
+
updates: source.slice(UPDATE.DURATION_IDX + 1),
|
|
321
|
+
};
|
|
322
|
+
} // getUpdate
|
|
323
|
+
/** Get output specification */
|
|
324
|
+
function getOutput(lines, begin, end) {
|
|
325
|
+
const res = new Map();
|
|
326
|
+
let line;
|
|
327
|
+
let token;
|
|
328
|
+
let eqIdx;
|
|
329
|
+
let openIdx;
|
|
330
|
+
let closeIdx;
|
|
331
|
+
let colonIdx;
|
|
332
|
+
for (let i = begin; i < end; ++i) {
|
|
333
|
+
line = lines[i].trim();
|
|
334
|
+
if (line.length < 1)
|
|
335
|
+
continue;
|
|
336
|
+
eqIdx = line.indexOf(EQUAL_SIGN);
|
|
337
|
+
openIdx = line.indexOf(BRACE_OPEN);
|
|
338
|
+
closeIdx = line.indexOf(BRACE_CLOSE);
|
|
339
|
+
colonIdx = line.indexOf(CONTROL_SEP);
|
|
340
|
+
// check braces
|
|
341
|
+
if (openIdx * closeIdx <= 0) {
|
|
342
|
+
throw new ModelError(`${ERROR_MSG.BRACES} Correct the line **${line}** in the **${CONTROL_EXPR.OUTPUT}** block.`, ERROR_LINK.UI_OPTS, line);
|
|
343
|
+
}
|
|
344
|
+
// check ":"
|
|
345
|
+
if (openIdx * colonIdx <= 0) {
|
|
346
|
+
throw new ModelError(`${ERROR_MSG.COLON} Correct the line **${line}** in the **#output** block.`, ERROR_LINK.UI_OPTS, line);
|
|
347
|
+
}
|
|
348
|
+
if (eqIdx === -1) { // no formula
|
|
349
|
+
if (openIdx === -1) { // no annotation
|
|
350
|
+
token = line.trim();
|
|
351
|
+
res.set(token, {
|
|
352
|
+
caption: token,
|
|
353
|
+
formula: null,
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
else { // there is an annotation
|
|
357
|
+
token = line.slice(0, openIdx).trim();
|
|
358
|
+
res.set(token, {
|
|
359
|
+
caption: line.slice(colonIdx + 1, closeIdx).trim(),
|
|
360
|
+
formula: null,
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
else // there is a formula
|
|
365
|
+
if (openIdx === -1) { // no annotation
|
|
366
|
+
token = line.slice(0, eqIdx).trim();
|
|
367
|
+
res.set(token, {
|
|
368
|
+
caption: token,
|
|
369
|
+
formula: line.slice(eqIdx + 1),
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
else { // there is an annotation
|
|
373
|
+
token = line.slice(0, eqIdx).trim();
|
|
374
|
+
res.set(token, {
|
|
375
|
+
caption: line.slice(colonIdx + 1, closeIdx),
|
|
376
|
+
formula: line.slice(eqIdx + 1, openIdx),
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
} // for i
|
|
380
|
+
return res;
|
|
381
|
+
} // getOutput
|
|
382
|
+
/** Get initial value problem specification given in the text */
|
|
383
|
+
export function getIVP(text) {
|
|
384
|
+
// The current Initial Value Problem (IVP) specification
|
|
385
|
+
let name;
|
|
386
|
+
let tags = null;
|
|
387
|
+
let descr = null;
|
|
388
|
+
let deqs;
|
|
389
|
+
let exprs = null;
|
|
390
|
+
let arg;
|
|
391
|
+
let inits;
|
|
392
|
+
let consts = null;
|
|
393
|
+
let params = null;
|
|
394
|
+
let tolerance = DEFAULT_TOL;
|
|
395
|
+
let loop = null;
|
|
396
|
+
const updates = [];
|
|
397
|
+
const metas = [];
|
|
398
|
+
let outputs = null;
|
|
399
|
+
let solverSettings = DEFAULT_SOLVER_SETTINGS;
|
|
400
|
+
let inputsLookup = null;
|
|
401
|
+
// 0. Split text into lines & remove comments
|
|
402
|
+
const lines = text.replaceAll('\t', ' ').split('\n')
|
|
403
|
+
.map((s) => {
|
|
404
|
+
const idx = s.indexOf(COMMENT_SEQ);
|
|
405
|
+
return s.slice(0, (idx >= 0) ? idx : undefined).trimStart();
|
|
406
|
+
})
|
|
407
|
+
.filter((s) => s !== '');
|
|
408
|
+
// 1. Skip first lines without the control tag
|
|
409
|
+
const start = getStartOfProblemDef(lines);
|
|
410
|
+
// 2. Get blocks limits
|
|
411
|
+
const blocks = getBlocks(lines, start);
|
|
412
|
+
// 3. Process blocks
|
|
413
|
+
for (const block of blocks) {
|
|
414
|
+
const firstLine = lines[block.begin];
|
|
415
|
+
if (firstLine.startsWith(CONTROL_EXPR.NAME)) { // the 'name' block
|
|
416
|
+
name = firstLine.slice(firstLine.indexOf(CONTROL_SEP) + 1).trim();
|
|
417
|
+
}
|
|
418
|
+
else if (firstLine.startsWith(CONTROL_EXPR.TAGS)) { // the 'tags' block
|
|
419
|
+
tags = firstLine.slice(firstLine.indexOf(CONTROL_SEP) + 1).trim();
|
|
420
|
+
}
|
|
421
|
+
else if (firstLine.startsWith(CONTROL_EXPR.DESCR)) { // the 'description' block
|
|
422
|
+
descr = firstLine.slice(firstLine.indexOf(CONTROL_SEP) + 1).trim();
|
|
423
|
+
}
|
|
424
|
+
else if (firstLine.startsWith(CONTROL_EXPR.DIF_EQ)) { // the 'differential equations' block
|
|
425
|
+
deqs = getDifEquations(concatMultilineFormulas(lines.slice(block.begin + 1, block.end)));
|
|
426
|
+
}
|
|
427
|
+
else if (firstLine.startsWith(CONTROL_EXPR.EXPR)) { // the 'expressions' block
|
|
428
|
+
exprs = getExpressions(concatMultilineFormulas(lines.slice(block.begin + 1, block.end)));
|
|
429
|
+
}
|
|
430
|
+
else if (firstLine.startsWith(CONTROL_EXPR.ARG)) { // the 'argument' block
|
|
431
|
+
arg = getArg(concatMultilineFormulas(lines.slice(block.begin, block.end)));
|
|
432
|
+
}
|
|
433
|
+
else if (firstLine.startsWith(CONTROL_EXPR.INITS)) { // the 'initial values' block
|
|
434
|
+
inits = getEqualities(lines, block.begin + 1, block.end);
|
|
435
|
+
}
|
|
436
|
+
else if (firstLine.startsWith(CONTROL_EXPR.CONSTS)) { // the 'constants' block
|
|
437
|
+
consts = getEqualities(lines, block.begin + 1, block.end);
|
|
438
|
+
}
|
|
439
|
+
else if (firstLine.startsWith(CONTROL_EXPR.PARAMS)) { // the 'parameters' block
|
|
440
|
+
params = getEqualities(lines, block.begin + 1, block.end);
|
|
441
|
+
}
|
|
442
|
+
else if (firstLine.startsWith(CONTROL_EXPR.TOL)) { // the 'tolerance' block
|
|
443
|
+
tolerance = firstLine.slice(firstLine.indexOf(CONTROL_SEP) + 1).trim();
|
|
444
|
+
}
|
|
445
|
+
else if (firstLine.startsWith(CONTROL_EXPR.SOLVER)) { // the 'solver settings' block
|
|
446
|
+
solverSettings = firstLine.slice(firstLine.indexOf(CONTROL_SEP) + 1).trim();
|
|
447
|
+
}
|
|
448
|
+
else if (firstLine.startsWith(CONTROL_EXPR.LOOP)) { // the 'loop' block
|
|
449
|
+
loop = getLoop(lines, block.begin + 1, block.end);
|
|
450
|
+
}
|
|
451
|
+
else if (firstLine.startsWith(CONTROL_EXPR.UPDATE)) { // the 'update' block
|
|
452
|
+
updates.push(getUpdate(lines, block.begin, block.end));
|
|
453
|
+
}
|
|
454
|
+
else if (firstLine.startsWith(CONTROL_EXPR.OUTPUT)) { // the 'output' block
|
|
455
|
+
outputs = getOutput(lines, block.begin + 1, block.end);
|
|
456
|
+
}
|
|
457
|
+
else if (firstLine.startsWith(CONTROL_EXPR.INPUTS)) { // the 'inputs' block
|
|
458
|
+
inputsLookup = firstLine.slice(firstLine.indexOf(CONTROL_SEP) + 1).trim();
|
|
459
|
+
}
|
|
460
|
+
else if (firstLine.startsWith(CONTROL_EXPR.COMMENT)) { // the 'comment' block
|
|
461
|
+
// just skip it
|
|
462
|
+
}
|
|
463
|
+
else
|
|
464
|
+
metas.push(firstLine.slice(CONTROL_TAG_LEN));
|
|
465
|
+
} // for
|
|
466
|
+
// check loop- & update-features: just one is supported
|
|
467
|
+
if ((loop !== null) && (updates.length > 0))
|
|
468
|
+
throw new ModelError(ERROR_MSG.LOOP_VS_UPDATE, ERROR_LINK.LOOP_VS_UPDATE, CONTROL_EXPR.LOOP);
|
|
469
|
+
// obtained model
|
|
470
|
+
const ivp = {
|
|
471
|
+
name: name,
|
|
472
|
+
tags: tags,
|
|
473
|
+
descr: descr,
|
|
474
|
+
deqs: deqs,
|
|
475
|
+
exprs: exprs,
|
|
476
|
+
arg: arg,
|
|
477
|
+
inits: inits,
|
|
478
|
+
consts: consts,
|
|
479
|
+
params: params,
|
|
480
|
+
tolerance: tolerance,
|
|
481
|
+
usedMathFuncs: getUsedMathIds(text, MATH_FUNCS),
|
|
482
|
+
usedMathConsts: getUsedMathIds(text, MATH_CONSTS),
|
|
483
|
+
loop: loop,
|
|
484
|
+
updates: (updates.length === 0) ? null : updates,
|
|
485
|
+
metas: metas,
|
|
486
|
+
outputs: outputs,
|
|
487
|
+
solverSettings: solverSettings,
|
|
488
|
+
inputsLookup: inputsLookup,
|
|
489
|
+
};
|
|
490
|
+
checkCorrectness(ivp);
|
|
491
|
+
return ivp;
|
|
492
|
+
} // getIVP
|
|
493
|
+
/** Return input specification, required for annotation generating */
|
|
494
|
+
function getInputSpec(inp) {
|
|
495
|
+
if (inp.annot)
|
|
496
|
+
return `${inp.value} ${inp.annot}`;
|
|
497
|
+
return `${inp.value}`;
|
|
498
|
+
}
|
|
499
|
+
/** Return annotation line specifying viewers */
|
|
500
|
+
function getViewersLine(ivp) {
|
|
501
|
+
const outputColsCount = (ivp.outputs) ? ivp.outputs.size : ivp.inits.size;
|
|
502
|
+
const multiAxis = (outputColsCount > MAX_LINE_CHART - 1) ? 'true' : 'false';
|
|
503
|
+
const segments = (ivp.updates) ? ` segmentColumnName: "${STAGE_COL_NAME}",` : '';
|
|
504
|
+
// eslint-disable-next-line max-len
|
|
505
|
+
return `viewer: Line chart(block: 100, multiAxis: "${multiAxis}",${segments} multiAxisLegendPosition: "RightCenter", autoLayout: "false", showAggrSelectors: "false") | Grid(block: 100)`;
|
|
506
|
+
}
|
|
507
|
+
/** Generate annotation lines */
|
|
508
|
+
function getAnnot(ivp, toAddViewers = true, toAddEditor = false) {
|
|
509
|
+
const res = [];
|
|
510
|
+
// the 'name' line
|
|
511
|
+
res.push(`${ANNOT.NAME} ${ivp.name}`);
|
|
512
|
+
// the 'tags' line
|
|
513
|
+
if (ivp.tags)
|
|
514
|
+
res.push(`${ANNOT.TAGS} ${ivp.tags}`);
|
|
515
|
+
// the 'description' line
|
|
516
|
+
if (ivp.descr)
|
|
517
|
+
res.push(`${ANNOT.DESCR} ${ivp.descr}`);
|
|
518
|
+
// the 'language' line
|
|
519
|
+
res.push(ANNOT.LANG);
|
|
520
|
+
// the 'loop' lines
|
|
521
|
+
if (ivp.loop)
|
|
522
|
+
res.push(`${ANNOT.INT_INPUT} ${LOOP.COUNT_NAME} = ${getInputSpec(ivp.loop.count)}`);
|
|
523
|
+
// argument lines
|
|
524
|
+
const arg = ivp.arg;
|
|
525
|
+
const t0 = `_${arg.name}0`;
|
|
526
|
+
const t1 = `_${arg.name}1`;
|
|
527
|
+
const h = `_h`;
|
|
528
|
+
res.push(`${ANNOT.DOUBLE_INPUT} ${t0} = ${getInputSpec(arg.initial)}`);
|
|
529
|
+
res.push(`${ANNOT.DOUBLE_INPUT} ${t1} = ${getInputSpec(arg.final)}`);
|
|
530
|
+
res.push(`${ANNOT.DOUBLE_INPUT} ${h} = ${getInputSpec(arg.step)}`);
|
|
531
|
+
// initial values lines
|
|
532
|
+
ivp.inits.forEach((val, key) => res.push(`${ANNOT.DOUBLE_INPUT} ${key} = ${getInputSpec(val)}`));
|
|
533
|
+
// parameters lines
|
|
534
|
+
if (ivp.params !== null)
|
|
535
|
+
ivp.params.forEach((val, key) => res.push(`${ANNOT.DOUBLE_INPUT} ${key} = ${getInputSpec(val)}`));
|
|
536
|
+
// the 'output' line
|
|
537
|
+
if (toAddViewers)
|
|
538
|
+
res.push(`${ANNOT.OUTPUT} {${ANNOT.CAPTION} ${ivp.name}; ${getViewersLine(ivp)}}`);
|
|
539
|
+
else
|
|
540
|
+
res.push(`${ANNOT.OUTPUT} {${ANNOT.CAPTION} ${ivp.name}`);
|
|
541
|
+
// the 'editor' line
|
|
542
|
+
if (toAddEditor) {
|
|
543
|
+
res.push(ANNOT.EDITOR);
|
|
544
|
+
res.push(ANNOT.SIDEBAR);
|
|
545
|
+
}
|
|
546
|
+
if (ivp.metas.length > 0)
|
|
547
|
+
ivp.metas.forEach((line) => res.push(`//${line}`));
|
|
548
|
+
else
|
|
549
|
+
res.push(defaultMetas);
|
|
550
|
+
return res;
|
|
551
|
+
} // getAnnot
|
|
552
|
+
/** Returns math functions arguments string */
|
|
553
|
+
function getMathArg(funcIdx) {
|
|
554
|
+
return (funcIdx > POW_IDX) ? '(x)' : '(x, y)';
|
|
555
|
+
}
|
|
556
|
+
/** Return custom output lines: no expressions */
|
|
557
|
+
function getCustomOutputLinesNoExpressions(name, outputs, toAddUpdateCol) {
|
|
558
|
+
const res = [''];
|
|
559
|
+
res.push(SCRIPT.CUSTOM_OUTPUT_COM);
|
|
560
|
+
res.push(SCRIPT.CUSTOM_COLUMNS);
|
|
561
|
+
outputs.forEach((val, key) => {
|
|
562
|
+
if (!val.formula)
|
|
563
|
+
res.push(`${SCRIPT.SPACE2}df.col('${key}'),`);
|
|
564
|
+
});
|
|
565
|
+
if (toAddUpdateCol)
|
|
566
|
+
res.push(`${SCRIPT.SPACE2}df.col('${STAGE_COL_NAME}'),`);
|
|
567
|
+
res.push('];');
|
|
568
|
+
outputs.forEach((val, key) => {
|
|
569
|
+
if (!val.formula)
|
|
570
|
+
res.push(`df.col('${key}').name = '${val.caption}';`);
|
|
571
|
+
});
|
|
572
|
+
res.push(`df = DG.DataFrame.fromColumns(_columns);`);
|
|
573
|
+
res.push(`df.name = '${name}';`);
|
|
574
|
+
return res;
|
|
575
|
+
} // getCustomOutputLinesNoExpressions
|
|
576
|
+
/** Return custom output lines: with expressions */
|
|
577
|
+
function getCustomOutputLinesWithExpressions(ivp) {
|
|
578
|
+
const res = [''];
|
|
579
|
+
res.push(SCRIPT.CUSTOM_OUTPUT_COM);
|
|
580
|
+
// 1. Solution raw data
|
|
581
|
+
res.push(`const ${ivp.arg.name}RawData = df.col('${ivp.arg.name}').getRawData();`);
|
|
582
|
+
res.push(`let ${ivp.arg.name};`);
|
|
583
|
+
res.push(`const len = df.rowCount;\n`);
|
|
584
|
+
ivp.inits.forEach((val, key) => {
|
|
585
|
+
res.push(`const ${key}RawData = df.col('${key}').getRawData();`);
|
|
586
|
+
});
|
|
587
|
+
res.push('');
|
|
588
|
+
// 2. Expressions raw data & variables
|
|
589
|
+
ivp.exprs.forEach((val, key) => {
|
|
590
|
+
if (ivp.outputs.has(key))
|
|
591
|
+
res.push(`const ${key}RawData = new Float64Array(len);`);
|
|
592
|
+
res.push(`let ${key};`);
|
|
593
|
+
});
|
|
594
|
+
res.push('');
|
|
595
|
+
// 3. Computations
|
|
596
|
+
res.push('for (let i = 0; i < len; ++i) {');
|
|
597
|
+
res.push(`${SCRIPT.SPACE2}${ivp.arg.name} = ${ivp.arg.name}RawData[i];`);
|
|
598
|
+
ivp.inits.forEach((val, key) => {
|
|
599
|
+
res.push(`${SCRIPT.SPACE2}${key} = ${key}RawData[i];`);
|
|
600
|
+
});
|
|
601
|
+
ivp.exprs.forEach((val, key) => {
|
|
602
|
+
res.push(`${SCRIPT.SPACE2}${key} = ${val};`);
|
|
603
|
+
if (ivp.outputs.has(key))
|
|
604
|
+
res.push(`${SCRIPT.SPACE2}${key}RawData[i] = ${key};`);
|
|
605
|
+
});
|
|
606
|
+
res.push('}\n');
|
|
607
|
+
// 4. Form output
|
|
608
|
+
res.push(`df = DG.DataFrame.fromColumns([`);
|
|
609
|
+
ivp.outputs.forEach((val, key) => {
|
|
610
|
+
if (!val.formula)
|
|
611
|
+
res.push(`${SCRIPT.SPACE2}DG.Column.fromFloat64Array('${val.caption}', ${key}RawData.slice(0, len)),`);
|
|
612
|
+
});
|
|
613
|
+
if (ivp.updates !== null)
|
|
614
|
+
res.push(`${SCRIPT.SPACE2}df.col('${STAGE_COL_NAME}'),`);
|
|
615
|
+
res.push(']);');
|
|
616
|
+
res.push(`df.name = '${ivp.name}';`);
|
|
617
|
+
return res;
|
|
618
|
+
} // getCustomOutputLinesWithExpressions
|
|
619
|
+
/** Return custom output lines */
|
|
620
|
+
function getCustomOutputLines(ivp) {
|
|
621
|
+
var _a;
|
|
622
|
+
if (ivp.exprs !== null) {
|
|
623
|
+
for (const key of ivp.exprs.keys()) {
|
|
624
|
+
if ((_a = ivp.outputs) === null || _a === void 0 ? void 0 : _a.has(key))
|
|
625
|
+
return getCustomOutputLinesWithExpressions(ivp);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
return getCustomOutputLinesNoExpressions(ivp.name, ivp.outputs, (ivp.updates !== null));
|
|
629
|
+
} // getCustomOutputLinesWithExpressions
|
|
630
|
+
/** Return main body of JS-script: basic variant */
|
|
631
|
+
function getScriptMainBodyBasic(ivp) {
|
|
632
|
+
const res = [];
|
|
633
|
+
// 1. Constants lines
|
|
634
|
+
if (ivp.consts !== null) {
|
|
635
|
+
res.push('');
|
|
636
|
+
res.push(SCRIPT.CONSTS);
|
|
637
|
+
ivp.consts.forEach((val, key) => res.push(`const ${key} = ${val.value};`));
|
|
638
|
+
}
|
|
639
|
+
// 2. The problem definition lines
|
|
640
|
+
res.push('');
|
|
641
|
+
res.push(SCRIPT.ODE_COM);
|
|
642
|
+
res.push(SCRIPT.ODE);
|
|
643
|
+
res.push(`${SCRIPT.SPACE4}name: '${ivp.name}',`);
|
|
644
|
+
// 2.1) argument
|
|
645
|
+
const t = ivp.arg.name;
|
|
646
|
+
const t0 = `_${t}0`;
|
|
647
|
+
const t1 = `_${t}1`;
|
|
648
|
+
const h = `_h`;
|
|
649
|
+
res.push(`${SCRIPT.SPACE4}arg: {name: '${t}', start: ${t0}, finish: ${t1}, step: ${h}},`);
|
|
650
|
+
const names = ivp.deqs.solutionNames;
|
|
651
|
+
// 2.2) initial values
|
|
652
|
+
res.push(`${SCRIPT.SPACE4}initial: [${names.join(', ')}],`);
|
|
653
|
+
// 2.3) the right-hand side of the problem
|
|
654
|
+
res.push(`${SCRIPT.SPACE4}func: (${t}, _y, _output) => {`);
|
|
655
|
+
res.push(`${SCRIPT.SPACE6}${SCRIPT.FUNC_VALS}`);
|
|
656
|
+
names.forEach((name, idx) => res.push(`${SCRIPT.SPACE6}const ${name} = _y[${idx}];`));
|
|
657
|
+
if (ivp.exprs !== null) {
|
|
658
|
+
res.push(`\n${SCRIPT.SPACE6}${SCRIPT.EVAL_EXPR}`);
|
|
659
|
+
ivp.exprs.forEach((val, key) => res.push(`${SCRIPT.SPACE6}const ${key} = ${val};`));
|
|
660
|
+
}
|
|
661
|
+
res.push(`\n${SCRIPT.SPACE6}${SCRIPT.COMP_OUT}`);
|
|
662
|
+
names.forEach((name, idx) => res.push(`${SCRIPT.SPACE6}_output[${idx}] = ${ivp.deqs.equations.get(name)};`));
|
|
663
|
+
res.push(`${SCRIPT.SPACE4}},`);
|
|
664
|
+
// 2.4) final lines of the problem specification
|
|
665
|
+
res.push(`${SCRIPT.SPACE4}tolerance: ${ivp.tolerance},`);
|
|
666
|
+
//res.push(`${SCRIPT.SPACE6}solverOptions: ${ivp.solverSettings.replaceAll(ANNOT_SEPAR, COMMA)},`);
|
|
667
|
+
res.push(`${SCRIPT.SPACE4}solutionColNames: [${names.map((key) => `'${key}'`).join(', ')}]`);
|
|
668
|
+
res.push('};');
|
|
669
|
+
// 2.5) solver options
|
|
670
|
+
res.push('');
|
|
671
|
+
res.push(`let opts = ${ivp.solverSettings.replaceAll(';', ',')};`);
|
|
672
|
+
// 3. Math functions
|
|
673
|
+
if (ivp.usedMathFuncs.length > 0) {
|
|
674
|
+
res.push('');
|
|
675
|
+
res.push(SCRIPT.MATH_FUNC_COM);
|
|
676
|
+
ivp.usedMathFuncs.forEach((i) => res.push(`const ${MATH_FUNCS[i]} = ${getMathArg(i)} => Math.${MATH_FUNCS[i]}${getMathArg(i)};`));
|
|
677
|
+
}
|
|
678
|
+
// 4. Math constants
|
|
679
|
+
if (ivp.usedMathConsts.length > 0) {
|
|
680
|
+
res.push('');
|
|
681
|
+
res.push(SCRIPT.MATH_CONST_COM);
|
|
682
|
+
ivp.usedMathConsts.forEach((i) => res.push(`const ${MATH_CONSTS[i]} = Math.${MATH_CONSTS[i]};`));
|
|
683
|
+
}
|
|
684
|
+
// 5. The 'call solver' lines
|
|
685
|
+
res.push('');
|
|
686
|
+
res.push(SCRIPT.SOLVER_COM);
|
|
687
|
+
res.push(SCRIPT.SOLVER);
|
|
688
|
+
res.push(SCRIPT.PREPARE);
|
|
689
|
+
res.push(SCRIPT.CALL);
|
|
690
|
+
res.push(SCRIPT.OUTPUT);
|
|
691
|
+
return res;
|
|
692
|
+
} // getScriptMainBodyBasic
|
|
693
|
+
/** Return function for JS-script */
|
|
694
|
+
function getScriptFunc(ivp, funcParamsNames) {
|
|
695
|
+
const res = [];
|
|
696
|
+
// 0. Function declaration
|
|
697
|
+
res.push(SCRIPT.ONE_STAGE_COM);
|
|
698
|
+
res.push(`${SCRIPT.ONE_STAGE_BEGIN}${funcParamsNames}${SCRIPT.ONE_STAGE_END}`);
|
|
699
|
+
// 1. Constants lines
|
|
700
|
+
if (ivp.consts !== null) {
|
|
701
|
+
res.push('');
|
|
702
|
+
res.push(SCRIPT.SPACE2 + SCRIPT.CONSTS);
|
|
703
|
+
ivp.consts.forEach((val, key) => res.push(`${SCRIPT.SPACE2}const ${key} = ${val.value};`));
|
|
704
|
+
}
|
|
705
|
+
// 2. The problem definition lines
|
|
706
|
+
res.push(SCRIPT.SPACE2 + SCRIPT.ODE_COM);
|
|
707
|
+
res.push(SCRIPT.SPACE2 + SCRIPT.ODE);
|
|
708
|
+
res.push(`${SCRIPT.SPACE6}name: '${ivp.name}',`);
|
|
709
|
+
// 2.1) argument
|
|
710
|
+
const t = ivp.arg.name;
|
|
711
|
+
const t0 = `_${t}0`;
|
|
712
|
+
const t1 = `_${t}1`;
|
|
713
|
+
const h = `_h`;
|
|
714
|
+
res.push(`${SCRIPT.SPACE6}arg: {name: '${t}', start: ${t0}, finish: ${t1}, step: ${h}},`);
|
|
715
|
+
const names = ivp.deqs.solutionNames;
|
|
716
|
+
// 2.2) initial values
|
|
717
|
+
res.push(`${SCRIPT.SPACE6}initial: [${names.join(', ')}],`);
|
|
718
|
+
// 2.3) the right-hand side of the problem
|
|
719
|
+
res.push(`${SCRIPT.SPACE6}func: (${t}, _y, _output) => {`);
|
|
720
|
+
res.push(`${SCRIPT.SPACE8}${SCRIPT.FUNC_VALS}`);
|
|
721
|
+
names.forEach((name, idx) => res.push(`${SCRIPT.SPACE8}const ${name} = _y[${idx}];`));
|
|
722
|
+
if (ivp.exprs !== null) {
|
|
723
|
+
res.push(`\n${SCRIPT.SPACE8}${SCRIPT.EVAL_EXPR}`);
|
|
724
|
+
ivp.exprs.forEach((val, key) => res.push(`${SCRIPT.SPACE8}const ${key} = ${val};`));
|
|
725
|
+
}
|
|
726
|
+
res.push(`\n${SCRIPT.SPACE8}${SCRIPT.COMP_OUT}`);
|
|
727
|
+
names.forEach((name, idx) => res.push(`${SCRIPT.SPACE8}_output[${idx}] = ${ivp.deqs.equations.get(name)};`));
|
|
728
|
+
res.push(`${SCRIPT.SPACE6}},`);
|
|
729
|
+
// 2.4) final lines of the problem specification
|
|
730
|
+
res.push(`${SCRIPT.SPACE6}tolerance: ${ivp.tolerance},`);
|
|
731
|
+
//res.push(`${SCRIPT.SPACE6}solverOptions: ${ivp.solverSettings.replaceAll(ANNOT_SEPAR, COMMA)},`);
|
|
732
|
+
res.push(`${SCRIPT.SPACE6}solutionColNames: [${names.map((key) => `'${key}'`).join(', ')}]`);
|
|
733
|
+
res.push(`${SCRIPT.SPACE2}};`);
|
|
734
|
+
// 2.5) solver options
|
|
735
|
+
res.push('');
|
|
736
|
+
res.push(`${SCRIPT.SPACE2}let opts = ${ivp.solverSettings.replaceAll(';', ',')};`);
|
|
737
|
+
// 3. Math functions
|
|
738
|
+
if (ivp.usedMathFuncs.length > 0) {
|
|
739
|
+
res.push('');
|
|
740
|
+
res.push(SCRIPT.SPACE2 + SCRIPT.MATH_FUNC_COM);
|
|
741
|
+
// eslint-disable-next-line max-len
|
|
742
|
+
ivp.usedMathFuncs.forEach((i) => res.push(`${SCRIPT.SPACE2}const ${MATH_FUNCS[i]} = ${getMathArg(i)} => Math.${MATH_FUNCS[i]}${getMathArg(i)};`));
|
|
743
|
+
}
|
|
744
|
+
// 4. Math constants
|
|
745
|
+
if (ivp.usedMathConsts.length > 0) {
|
|
746
|
+
res.push('');
|
|
747
|
+
res.push(SCRIPT.SPACE2 + SCRIPT.MATH_CONST_COM);
|
|
748
|
+
ivp.usedMathConsts.forEach((i) => res.push(`${SCRIPT.SPACE2}const ${MATH_CONSTS[i]} = Math.${MATH_CONSTS[i]};`));
|
|
749
|
+
}
|
|
750
|
+
// 5. The 'call solver' lines
|
|
751
|
+
res.push('');
|
|
752
|
+
res.push(SCRIPT.SPACE2 + SCRIPT.SOLVER_COM);
|
|
753
|
+
res.push(SCRIPT.SPACE2 + SCRIPT.SOLVER);
|
|
754
|
+
res.push(SCRIPT.SPACE2 + SCRIPT.PREPARE);
|
|
755
|
+
res.push(SCRIPT.SPACE2 + SCRIPT.CALL);
|
|
756
|
+
res.push(SCRIPT.SPACE2 + SCRIPT.RETURN_OUTPUT);
|
|
757
|
+
// 6. Close the function
|
|
758
|
+
res.push('};');
|
|
759
|
+
return res;
|
|
760
|
+
} // getScriptFunc
|
|
761
|
+
/** Return main body of JS-script: loop case */
|
|
762
|
+
function getScriptMainBodyLoopCase(ivp) {
|
|
763
|
+
const funcParamsNames = getFuncParamsNames(ivp);
|
|
764
|
+
const res = getScriptFunc(ivp, funcParamsNames);
|
|
765
|
+
res.push('');
|
|
766
|
+
//res.push(`${SCRIPT.ASYNC_OUTPUT}${funcParamsNames});`);
|
|
767
|
+
res.push(SCRIPT.SOLUTION_DF_COM);
|
|
768
|
+
const dfNames = getSolutionDfColsNames(ivp);
|
|
769
|
+
res.push(`let df = DG.DataFrame.fromColumns([`);
|
|
770
|
+
dfNames.forEach((name) => res.push(`${SCRIPT.SPACE2}DG.Column.fromFloat64Array('${name}', []),`));
|
|
771
|
+
res.push(`]);`);
|
|
772
|
+
res.push(`df.name = '${ivp.name}';`);
|
|
773
|
+
res.push('');
|
|
774
|
+
res.push(SCRIPT.LOOP_INTERVAL_COM);
|
|
775
|
+
res.push(`const ${SCRIPT.LOOP_INTERVAL} = _${ivp.arg.name}1 - _${ivp.arg.name}0;`);
|
|
776
|
+
res.push('');
|
|
777
|
+
res.push(`let ${SCRIPT.LAST_IDX} = 0;\n`);
|
|
778
|
+
res.push(SCRIPT.SOLVER_COM);
|
|
779
|
+
res.push(`for (let _idx = 0; _idx < ${LOOP.COUNT_NAME}; ++_idx) {`);
|
|
780
|
+
ivp.loop.updates.forEach((upd) => res.push(`${SCRIPT.SPACE2}${upd};`));
|
|
781
|
+
res.push(`${SCRIPT.SPACE2}${SCRIPT.APPEND}${SCRIPT.ONE_STAGE}${funcParamsNames}), true);`);
|
|
782
|
+
res.push(`${SCRIPT.SPACE2}_${ivp.arg.name}0 = _${ivp.arg.name}1;`);
|
|
783
|
+
res.push(`${SCRIPT.SPACE2}_${ivp.arg.name}1 += ${SCRIPT.LOOP_INTERVAL};`);
|
|
784
|
+
res.push(`${SCRIPT.SPACE2}${SCRIPT.LAST_IDX} = df.rowCount - 1;`);
|
|
785
|
+
dfNames.forEach((name, idx) => {
|
|
786
|
+
if (idx !== 0)
|
|
787
|
+
res.push(`${SCRIPT.SPACE2}${name} = df.get('${name}', ${SCRIPT.LAST_IDX});`);
|
|
788
|
+
});
|
|
789
|
+
// eslint-disable-next-line max-len
|
|
790
|
+
res.push(`${SCRIPT.SPACE2}df.set('${ivp.arg.name}', ${SCRIPT.LAST_IDX}, _${ivp.arg.name}0 - Math.min(_h * ${STEP_RATIO}, ${TINY}));`);
|
|
791
|
+
res.push('};');
|
|
792
|
+
return res;
|
|
793
|
+
} // getScriptMainBodyLoopCase
|
|
794
|
+
/** Return main body of JS-script: update case */
|
|
795
|
+
function getScriptMainBodyUpdateCase(ivp) {
|
|
796
|
+
var _a;
|
|
797
|
+
const funcParamsNames = getFuncParamsNames(ivp);
|
|
798
|
+
const res = getScriptFunc(ivp, funcParamsNames);
|
|
799
|
+
res.push('');
|
|
800
|
+
res.push(SCRIPT.SOLUTION_DF_COM);
|
|
801
|
+
const dfNames = getSolutionDfColsNames(ivp);
|
|
802
|
+
res.push(`${SCRIPT.ASYNC_OUTPUT}${funcParamsNames});`);
|
|
803
|
+
res.push('');
|
|
804
|
+
res.push(SCRIPT.SEGMENT_COM);
|
|
805
|
+
// eslint-disable-next-line max-len
|
|
806
|
+
res.push(`df.columns.add(DG.Column.fromList('string', '${STAGE_COL_NAME}', new Array(df.rowCount).fill('${(_a = ivp.arg.updateName) !== null && _a !== void 0 ? _a : INCEPTION}')));`);
|
|
807
|
+
res.push('');
|
|
808
|
+
res.push(`let ${SCRIPT.LAST_IDX} = 0;`);
|
|
809
|
+
ivp.updates.forEach((upd, idx) => {
|
|
810
|
+
res.push('');
|
|
811
|
+
res.push(`${SCRIPT.UPDATE_COM} ${idx + 1}`);
|
|
812
|
+
res.push(`const ${UPDATE.DURATION}${idx + 1} = ${upd.durationFormula};`);
|
|
813
|
+
res.push(`${SCRIPT.LAST_IDX} = df.rowCount - 1;`);
|
|
814
|
+
// eslint-disable-next-line max-len
|
|
815
|
+
res.push(`df.set('${ivp.arg.name}', ${SCRIPT.LAST_IDX}, _${ivp.arg.name}1 - Math.min(_h * ${STEP_RATIO}, ${TINY}));`);
|
|
816
|
+
dfNames.forEach((name, idx) => {
|
|
817
|
+
if (idx !== 0)
|
|
818
|
+
res.push(`${name} = df.get('${name}', ${SCRIPT.LAST_IDX});`);
|
|
819
|
+
});
|
|
820
|
+
upd.updates.forEach((upd) => res.push(`${upd};`));
|
|
821
|
+
res.push(`_${ivp.arg.name}0 = _${ivp.arg.name}1;`);
|
|
822
|
+
res.push(`_${ivp.arg.name}1 += ${UPDATE.DURATION}${idx + 1};`);
|
|
823
|
+
res.push(`const _DF${idx + 1} = ${SCRIPT.ONE_STAGE}${funcParamsNames});`);
|
|
824
|
+
// eslint-disable-next-line max-len
|
|
825
|
+
res.push(`_DF${idx + 1}.columns.add(DG.Column.fromList('string', '${STAGE_COL_NAME}', new Array(_DF${idx + 1}.rowCount).fill('${upd.name}')));`);
|
|
826
|
+
res.push(`${SCRIPT.APPEND}_DF${idx + 1}, true);`);
|
|
827
|
+
});
|
|
828
|
+
return res;
|
|
829
|
+
} // getScriptMainBodyUpdateCase
|
|
830
|
+
/** Return main body of JS-script */
|
|
831
|
+
function getScriptMainBody(ivp) {
|
|
832
|
+
if (ivp.loop)
|
|
833
|
+
return getScriptMainBodyLoopCase(ivp);
|
|
834
|
+
if (ivp.updates)
|
|
835
|
+
return getScriptMainBodyUpdateCase(ivp);
|
|
836
|
+
return getScriptMainBodyBasic(ivp);
|
|
837
|
+
}
|
|
838
|
+
/** Return JS-script lines */
|
|
839
|
+
export function getScriptLines(ivp, toAddViewers = true, toAddEditor = false) {
|
|
840
|
+
const res = getAnnot(ivp, toAddViewers, toAddEditor).concat(getScriptMainBody(ivp));
|
|
841
|
+
if (ivp.outputs)
|
|
842
|
+
return res.concat(getCustomOutputLines(ivp));
|
|
843
|
+
return res;
|
|
844
|
+
}
|
|
845
|
+
/** Return parameters of JS-script */
|
|
846
|
+
export function getScriptParams(ivp) {
|
|
847
|
+
const res = {};
|
|
848
|
+
if (ivp.loop)
|
|
849
|
+
res[LOOP.COUNT_NAME] = ivp.loop.count.value;
|
|
850
|
+
const arg = ivp.arg;
|
|
851
|
+
res[`_${arg.name}0`] = arg.initial.value;
|
|
852
|
+
res[`_${arg.name}1`] = arg.final.value;
|
|
853
|
+
res[`_h`] = arg.step.value;
|
|
854
|
+
ivp.inits.forEach((val, key) => res[key] = val.value);
|
|
855
|
+
if (ivp.params)
|
|
856
|
+
ivp.params.forEach((val, key) => res[key] = val.value);
|
|
857
|
+
return res;
|
|
858
|
+
}
|
|
859
|
+
/** Return func parameters names string */
|
|
860
|
+
function getFuncParamsNames(ivp) {
|
|
861
|
+
const names = [];
|
|
862
|
+
const arg = ivp.arg.name;
|
|
863
|
+
names.push(`_${arg}0`);
|
|
864
|
+
names.push(`_${arg}1`);
|
|
865
|
+
names.push(`_h`);
|
|
866
|
+
ivp.inits.forEach((val, key) => names.push(key));
|
|
867
|
+
if (ivp.params)
|
|
868
|
+
ivp.params.forEach((val, key) => names.push(key));
|
|
869
|
+
return names.join(', ');
|
|
870
|
+
}
|
|
871
|
+
/** Return solution dataframe columns names*/
|
|
872
|
+
function getSolutionDfColsNames(ivp) {
|
|
873
|
+
const res = [];
|
|
874
|
+
res.push(ivp.arg.name);
|
|
875
|
+
ivp.inits.forEach((val, key) => res.push(key));
|
|
876
|
+
return res;
|
|
877
|
+
}
|
|
878
|
+
/** Check solver settings */
|
|
879
|
+
function checkSolverSettings(line) {
|
|
880
|
+
const settings = new Map();
|
|
881
|
+
const openBraceIdx = line.indexOf(BRACE_OPEN);
|
|
882
|
+
const closeBraceIdx = line.indexOf(BRACE_CLOSE);
|
|
883
|
+
let sepIdx;
|
|
884
|
+
if ((openBraceIdx < 0) || (closeBraceIdx < 0))
|
|
885
|
+
throw new ModelError(`${ERROR_MSG.BRACES}. Correct the line **${line}**.`, ERROR_LINK.SOLVER_CONFIG, line);
|
|
886
|
+
for (const item of line.slice(openBraceIdx + 1, closeBraceIdx).split(ANNOT_SEPAR)) {
|
|
887
|
+
sepIdx = item.indexOf(CONTROL_SEP);
|
|
888
|
+
if (sepIdx > 1)
|
|
889
|
+
settings.set(item.slice(0, sepIdx).trim(), item.slice(sepIdx + 1).trim());
|
|
890
|
+
}
|
|
891
|
+
SOLVER_OPTIONS_RANGES.forEach((range, opt) => {
|
|
892
|
+
if (settings.has(opt)) {
|
|
893
|
+
const val = Number(settings.get(opt));
|
|
894
|
+
if ((val < range.min) || (val > range.max)) {
|
|
895
|
+
throw new ModelError(`${ERROR_MSG.SOLVER}: **${opt}** must be in the range **${range.min}..${range.max}**.`, ERROR_LINK.SOLVER_CONFIG, line);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
});
|
|
899
|
+
} // checkSolverSettings
|
|
900
|
+
/** Check IVP correctness */
|
|
901
|
+
function checkCorrectness(ivp) {
|
|
902
|
+
// 0. Check basic elements
|
|
903
|
+
if (ivp.name === undefined)
|
|
904
|
+
throw new ModelError(ERROR_MSG.UNDEF_NAME, ERROR_LINK.CORE_BLOCKS);
|
|
905
|
+
if ((ivp.deqs === undefined) || (ivp.deqs.equations.size === 0))
|
|
906
|
+
throw new ModelError(ERROR_MSG.UNDEF_DEQS, ERROR_LINK.CORE_BLOCKS);
|
|
907
|
+
if ((ivp.inits === undefined) || (ivp.inits.size === 0))
|
|
908
|
+
throw new ModelError(ERROR_MSG.UNDEF_INITS, ERROR_LINK.CORE_BLOCKS);
|
|
909
|
+
if (ivp.arg === undefined)
|
|
910
|
+
throw new ModelError(ERROR_MSG.UNDEF_ARG, ERROR_LINK.CORE_BLOCKS);
|
|
911
|
+
// 1. Check initial values
|
|
912
|
+
ivp.deqs.equations.forEach((ignore, name) => {
|
|
913
|
+
if (!ivp.inits.has(name)) {
|
|
914
|
+
throw new ModelError(`Initial value for **${name}** is missing. ${ERROR_MSG.MISSING_INIT}`, ERROR_LINK.CORE_BLOCKS, CONTROL_EXPR.INITS);
|
|
915
|
+
}
|
|
916
|
+
});
|
|
917
|
+
// 2. Check names of output columns
|
|
918
|
+
const usedNames = [];
|
|
919
|
+
let lowCase;
|
|
920
|
+
if (ivp.outputs !== null) {
|
|
921
|
+
ivp.outputs.forEach((val) => {
|
|
922
|
+
lowCase = val.caption.toLowerCase();
|
|
923
|
+
if (usedNames.includes(lowCase))
|
|
924
|
+
throw new ModelError(`${ERROR_MSG.CASE_INSENS}**${val.caption}**.`, ERROR_LINK.UI_OPTS, CONTROL_EXPR.OUTPUT);
|
|
925
|
+
else
|
|
926
|
+
usedNames.push(lowCase);
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
else {
|
|
930
|
+
const usedNames = [ivp.arg.name];
|
|
931
|
+
ivp.deqs.solutionNames.forEach((name) => {
|
|
932
|
+
lowCase = name.toLowerCase();
|
|
933
|
+
if (usedNames.includes(lowCase))
|
|
934
|
+
throw new ModelError(`${ERROR_MSG.CASE_INSENS}**${name}**.`, ERROR_LINK.UI_OPTS);
|
|
935
|
+
else
|
|
936
|
+
usedNames.push(lowCase);
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
// 3. Check argument
|
|
940
|
+
if (ivp.arg.initial.value >= ivp.arg.final.value) {
|
|
941
|
+
throw new ModelError(`${ERROR_MSG.INTERVAL} **${ivp.arg.name}**. ${ERROR_MSG.CORRECT_ARG_LIM}`, ERROR_LINK.CORE_BLOCKS, CONTROL_EXPR.ARG);
|
|
942
|
+
}
|
|
943
|
+
if (ivp.arg.step.value <= 0) {
|
|
944
|
+
throw new ModelError(`${ERROR_MSG.NEGATIVE_STEP}`, ERROR_LINK.CORE_BLOCKS);
|
|
945
|
+
}
|
|
946
|
+
if (ivp.arg.step.value > ivp.arg.final.value - ivp.arg.initial.value)
|
|
947
|
+
throw new ModelError(ERROR_MSG.INCOR_STEP, ERROR_LINK.CORE_BLOCKS);
|
|
948
|
+
// 4. Check script inputs, due to https://reddata.atlassian.net/browse/GROK-15152
|
|
949
|
+
const scriptInputs = [];
|
|
950
|
+
let current;
|
|
951
|
+
// 4.1) initial values
|
|
952
|
+
ivp.inits.forEach((_, key) => {
|
|
953
|
+
if (key[0] === SERVICE) {
|
|
954
|
+
throw new ModelError(`${ERROR_MSG.SERVICE_START} Correct **"${key}"** in the **#inits** block.`, ERROR_LINK.CORE_BLOCKS);
|
|
955
|
+
}
|
|
956
|
+
current = key.toLocaleLowerCase();
|
|
957
|
+
if (scriptInputs.includes(current)) {
|
|
958
|
+
throw new ModelError(`${ERROR_MSG.REUSE_NAME} **"${key}"** in the **#inits** block.`, ERROR_LINK.CORE_BLOCKS);
|
|
959
|
+
}
|
|
960
|
+
scriptInputs.push(current);
|
|
961
|
+
});
|
|
962
|
+
// 4.2) parameters
|
|
963
|
+
if (ivp.params !== null) {
|
|
964
|
+
ivp.params.forEach((_, key) => {
|
|
965
|
+
if (key[0] === SERVICE) {
|
|
966
|
+
throw new ModelError(`${ERROR_MSG.SERVICE_START}: correct **"${key}"** in the **${CONTROL_EXPR.PARAMS}** block.`, ERROR_LINK.MODEL_PARAMS);
|
|
967
|
+
}
|
|
968
|
+
current = key.toLocaleLowerCase();
|
|
969
|
+
if (scriptInputs.includes(current)) {
|
|
970
|
+
throw new ModelError(`${ERROR_MSG.REUSE_NAME} **"${key}"** in the **${CONTROL_EXPR.PARAMS}** block.`, ERROR_LINK.MODEL_PARAMS);
|
|
971
|
+
}
|
|
972
|
+
scriptInputs.push(current);
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
// 5. Check solver settings
|
|
976
|
+
checkSolverSettings(ivp.solverSettings);
|
|
977
|
+
} // checkCorrectness
|
|
978
|
+
/** */
|
|
979
|
+
export function getJScode(ivp) {
|
|
980
|
+
const res = [];
|
|
981
|
+
// 1. Math functions
|
|
982
|
+
if (ivp.usedMathFuncs.length > 0) {
|
|
983
|
+
res.push(SCRIPT.MATH_FUNC_COM);
|
|
984
|
+
ivp.usedMathFuncs.forEach((i) => res.push(`const ${MATH_FUNCS[i]} = ${getMathArg(i)} => Math.${MATH_FUNCS[i]}${getMathArg(i)};`));
|
|
985
|
+
}
|
|
986
|
+
// 2. Math constants
|
|
987
|
+
if (ivp.usedMathConsts.length > 0) {
|
|
988
|
+
res.push('');
|
|
989
|
+
res.push(SCRIPT.MATH_CONST_COM);
|
|
990
|
+
ivp.usedMathConsts.forEach((i) => res.push(`const ${MATH_CONSTS[i]} = Math.${MATH_CONSTS[i]};`));
|
|
991
|
+
}
|
|
992
|
+
// 1. Constants lines
|
|
993
|
+
if (ivp.consts !== null) {
|
|
994
|
+
res.push('');
|
|
995
|
+
res.push(SCRIPT.CONSTS);
|
|
996
|
+
ivp.consts.forEach((val, key) => res.push(`const ${key} = ${val.value};`));
|
|
997
|
+
}
|
|
998
|
+
// 2. The problem definition lines
|
|
999
|
+
res.push('');
|
|
1000
|
+
res.push(SCRIPT.ODE_COM);
|
|
1001
|
+
res.push(`const odes = {`);
|
|
1002
|
+
res.push(`${SCRIPT.SPACE4}name: '${ivp.name}',`);
|
|
1003
|
+
// 2.1) argument
|
|
1004
|
+
const t = ivp.arg.name;
|
|
1005
|
+
const t0 = `_${t}0`;
|
|
1006
|
+
const t1 = `_${t}1`;
|
|
1007
|
+
const h = `_h`;
|
|
1008
|
+
res.push(`${SCRIPT.SPACE4}arg: {name: '${t}', start: ${t0}, finish: ${t1}, step: ${h}},`);
|
|
1009
|
+
const names = ivp.deqs.solutionNames;
|
|
1010
|
+
// 2.2) initial values
|
|
1011
|
+
res.push(`${SCRIPT.SPACE4}initial: [${names.join(', ')}],`);
|
|
1012
|
+
// 2.3) the right-hand side of the problem
|
|
1013
|
+
res.push(`${SCRIPT.SPACE4}func: (${t}, _y, _output) => {`);
|
|
1014
|
+
res.push(`${SCRIPT.SPACE6}${SCRIPT.FUNC_VALS}`);
|
|
1015
|
+
names.forEach((name, idx) => res.push(`${SCRIPT.SPACE6}const ${name} = _y[${idx}];`));
|
|
1016
|
+
if (ivp.exprs !== null) {
|
|
1017
|
+
res.push(`\n${SCRIPT.SPACE6}${SCRIPT.EVAL_EXPR}`);
|
|
1018
|
+
ivp.exprs.forEach((val, key) => res.push(`${SCRIPT.SPACE6}const ${key} = ${val};`));
|
|
1019
|
+
}
|
|
1020
|
+
res.push(`\n${SCRIPT.SPACE6}${SCRIPT.COMP_OUT}`);
|
|
1021
|
+
names.forEach((name, idx) => res.push(`${SCRIPT.SPACE6}_output[${idx}] = ${ivp.deqs.equations.get(name)};`));
|
|
1022
|
+
res.push(`${SCRIPT.SPACE4}},`);
|
|
1023
|
+
// 2.4) final lines of the problem specification
|
|
1024
|
+
res.push(`${SCRIPT.SPACE4}tolerance: ${ivp.tolerance},`);
|
|
1025
|
+
//res.push(`${SCRIPT.SPACE6}solverOptions: ${ivp.solverSettings.replaceAll(ANNOT_SEPAR, COMMA)},`);
|
|
1026
|
+
res.push(`${SCRIPT.SPACE4}solutionColNames: [${names.map((key) => `'${key}'`).join(', ')}]`);
|
|
1027
|
+
res.push('};');
|
|
1028
|
+
return res;
|
|
1029
|
+
} // getJScode
|
|
1030
|
+
/** */
|
|
1031
|
+
export function getFunc4worker(ivp) { }
|
|
1032
|
+
//# sourceMappingURL=scripting-tools.js.map
|