build_cmake/OMCompiler/Compiler/generated-mo/Template/CodegenJS.mo
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | encapsulated package CodegenJS | ||
| 2 | " | ||
| 3 | file: CodegenJS.mo | ||
| 4 | package: CodegenJS | ||
| 5 | description: Generated by Susan. | ||
| 6 | " | ||
| 7 | |||
| 8 | public import Tpl; | ||
| 9 | |||
| 10 | public import SimCodeVar; | ||
| 11 | public import HashTableCrefSimVar; | ||
| 12 | public import SimCode; | ||
| 13 | public import SimCodeFunction; | ||
| 14 | public import SimCodeCodegenUtil; | ||
| 15 | public import SimCodeFunctionUtil; | ||
| 16 | public import BackendDAE; | ||
| 17 | public import System; | ||
| 18 | public import Autoconf; | ||
| 19 | protected import Absyn; | ||
| 20 | protected import AbsynUtil; | ||
| 21 | public import MMath; | ||
| 22 | public import DAE; | ||
| 23 | public import ClassInf; | ||
| 24 | public import ClassInfUtil; | ||
| 25 | protected import SCode; | ||
| 26 | protected import SCodeDump; | ||
| 27 | public import StringUtil; | ||
| 28 | public import Util; | ||
| 29 | public import List; | ||
| 30 | public import ComponentReferenceBasics; | ||
| 31 | public import ComponentReference; | ||
| 32 | public import Expression; | ||
| 33 | protected import ExpressionDump; | ||
| 34 | public import ExpressionBasics; | ||
| 35 | public import Config; | ||
| 36 | public import Testsuite; | ||
| 37 | public import Flags; | ||
| 38 | public import FlagsUtil; | ||
| 39 | public import Settings; | ||
| 40 | public import Patternm; | ||
| 41 | public import Error; | ||
| 42 | public import Values; | ||
| 43 | public import ValuesUtil; | ||
| 44 | public import DAEDump; | ||
| 45 | public import Algorithm; | ||
| 46 | protected import ElementSource; | ||
| 47 | public import DAEUtil; | ||
| 48 | public import TypesDump; | ||
| 49 | public import Types; | ||
| 50 | public import HashTableCrIListArray; | ||
| 51 | public import CodegenUtil; | ||
| 52 | |||
| 53 | public function markdownFile | ||
| 54 | input Tpl.Text in_txt; | ||
| 55 | input SimCode.SimCode in_a_simCode; | ||
| 56 | |||
| 57 | output Tpl.Text out_txt; | ||
| 58 | algorithm | ||
| 59 | out_txt := | ||
| 60 | match(in_txt, in_a_simCode) | ||
| 61 | local | ||
| 62 | Tpl.Text txt; | ||
| 63 | String i_fileNamePrefix; | ||
| 64 | SimCode.SimCode i_simCode; | ||
| 65 | Tpl.Text txt_3; | ||
| 66 | Tpl.Text txt_2; | ||
| 67 | Tpl.Text txt_1; | ||
| 68 | Tpl.Text txt_0; | ||
| 69 | |||
| 70 | case ( txt, | ||
| 71 | (i_simCode as SimCode.SIMCODE(fileNamePrefix = i_fileNamePrefix)) ) | ||
| 72 | algorithm | ||
| 73 | ✗ | txt_0 := markdownContents(Tpl.emptyTxt, i_simCode); | |
| 74 | ✗ | txt_1 := Tpl.writeStr(Tpl.emptyTxt, i_fileNamePrefix); | |
| 75 | ✗ | txt_1 := Tpl.writeTok(txt_1, Tpl.ST_STRING(".md")); | |
| 76 | ✗ | Tpl.textFile(txt_0, Tpl.textString(txt_1)); | |
| 77 | ✗ | txt_2 := nodeJSDriver(Tpl.emptyTxt, i_simCode); | |
| 78 | ✗ | txt_3 := Tpl.writeStr(Tpl.emptyTxt, i_fileNamePrefix); | |
| 79 | ✗ | txt_3 := Tpl.writeTok(txt_3, Tpl.ST_STRING("_node.js")); | |
| 80 | ✗ | Tpl.textFile(txt_2, Tpl.textString(txt_3)); | |
| 81 | then txt; | ||
| 82 | |||
| 83 | case ( txt, | ||
| 84 | _ ) | ||
| 85 | then txt; | ||
| 86 | end match; | ||
| 87 | end markdownFile; | ||
| 88 | |||
| 89 | public function nodeJSDriver | ||
| 90 | input Tpl.Text in_txt; | ||
| 91 | input SimCode.SimCode in_a_simCode; | ||
| 92 | |||
| 93 | output Tpl.Text out_txt; | ||
| 94 | algorithm | ||
| 95 | out_txt := | ||
| 96 | match(in_txt, in_a_simCode) | ||
| 97 | local | ||
| 98 | Tpl.Text txt; | ||
| 99 | String i_s_outputFormat; | ||
| 100 | String i_fileNamePrefix; | ||
| 101 | |||
| 102 | case ( txt, | ||
| 103 | SimCode.SIMCODE(simulationSettingsOpt = SOME(SimCode.SIMULATION_SETTINGS(outputFormat = i_s_outputFormat)), fileNamePrefix = i_fileNamePrefix) ) | ||
| 104 | algorithm | ||
| 105 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING_LIST({ | |
| 106 | "#!/usr/bin/nodejs --max-old-space-size=8192\n", | ||
| 107 | "var fs = require(\'fs\');\n", | ||
| 108 | "\n", | ||
| 109 | "var initXML = fs.readFileSync(\'./" | ||
| 110 | }, false)); | ||
| 111 | ✗ | txt := Tpl.writeStr(txt, i_fileNamePrefix); | |
| 112 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING_LIST({ | |
| 113 | "_init.xml\');\n", | ||
| 114 | "\n", | ||
| 115 | "var mod = require(\'./" | ||
| 116 | }, false)); | ||
| 117 | ✗ | txt := Tpl.writeStr(txt, i_fileNamePrefix); | |
| 118 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING_LIST({ | |
| 119 | ".js\');\n", | ||
| 120 | "mod.FS_createDataFile(\"/\", \'" | ||
| 121 | }, false)); | ||
| 122 | ✗ | txt := Tpl.writeStr(txt, i_fileNamePrefix); | |
| 123 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING_LIST({ | |
| 124 | "_init.xml\', initXML, true, false);\n", | ||
| 125 | "mod.FS_createLazyFile(\"/\", \'" | ||
| 126 | }, false)); | ||
| 127 | ✗ | txt := Tpl.writeStr(txt, i_fileNamePrefix); | |
| 128 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING("_info.xml\', \'")); | |
| 129 | ✗ | txt := Tpl.writeStr(txt, i_fileNamePrefix); | |
| 130 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING_LIST({ | |
| 131 | "_info.xml\', true, false);\n", | ||
| 132 | "mod.callMain(process.argv.slice(2));\n", | ||
| 133 | "\n", | ||
| 134 | "var fname = \'" | ||
| 135 | }, false)); | ||
| 136 | ✗ | txt := Tpl.writeStr(txt, i_fileNamePrefix); | |
| 137 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING("_res.")); | |
| 138 | ✗ | txt := Tpl.writeStr(txt, i_s_outputFormat); | |
| 139 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING_LIST({ | |
| 140 | "\';\n", | ||
| 141 | "var content = mod.OpenModelica_readFile(fname);\n", | ||
| 142 | "fs.writeFileSync(fname,content);" | ||
| 143 | }, false)); | ||
| 144 | then txt; | ||
| 145 | |||
| 146 | case ( txt, | ||
| 147 | _ ) | ||
| 148 | then txt; | ||
| 149 | end match; | ||
| 150 | end nodeJSDriver; | ||
| 151 | |||
| 152 | public function markdownContents | ||
| 153 | input Tpl.Text in_txt; | ||
| 154 | input SimCode.SimCode in_a_simCode; | ||
| 155 | |||
| 156 | output Tpl.Text out_txt; | ||
| 157 | algorithm | ||
| 158 | out_txt := | ||
| 159 | match(in_txt, in_a_simCode) | ||
| 160 | local | ||
| 161 | Tpl.Text txt; | ||
| 162 | String i_fileNamePrefix; | ||
| 163 | Real i_s_tolerance; | ||
| 164 | Integer i_s_numberOfIntervals; | ||
| 165 | Real i_s_stopTime; | ||
| 166 | Absyn.Path i_modelInfo_name; | ||
| 167 | String ret_1; | ||
| 168 | Tpl.Text txt_0; | ||
| 169 | |||
| 170 | case ( txt, | ||
| 171 | SimCode.SIMCODE(modelInfo = SimCode.MODELINFO(functions = _, varInfo = SimCode.VARINFO(numZeroCrossings = _), vars = SimCodeVar.SIMVARS(stateVars = _), name = i_modelInfo_name), simulationSettingsOpt = SOME(SimCode.SIMULATION_SETTINGS(stopTime = i_s_stopTime, numberOfIntervals = i_s_numberOfIntervals, tolerance = i_s_tolerance)), makefileParams = SimCodeFunction.MAKEFILE_PARAMS(ccompiler = _), fileNamePrefix = i_fileNamePrefix) ) | ||
| 172 | algorithm | ||
| 173 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING_LIST({ | |
| 174 | "# OpenModelica simulation example\n", | ||
| 175 | "## " | ||
| 176 | }, false)); | ||
| 177 | ✗ | txt_0 := CodegenUtil.dotPath(Tpl.emptyTxt, i_modelInfo_name); | |
| 178 | ✗ | ret_1 := Util.escapeModelicaStringToXmlString(Tpl.textString(txt_0)); | |
| 179 | ✗ | txt := Tpl.writeStr(txt, ret_1); | |
| 180 | ✗ | txt := Tpl.softNewLine(txt); | |
| 181 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING_LIST({ | |
| 182 | "\n", | ||
| 183 | "```yaml script=scriptloader\n", | ||
| 184 | "- lib/tinytimer.js\n", | ||
| 185 | "```\n", | ||
| 186 | "\n", | ||
| 187 | "<style media=\"screen\" type=\"text/css\">\n", | ||
| 188 | "label {font-weight:normal; size: 0.9em}\n", | ||
| 189 | "</style>\n", | ||
| 190 | "\n", | ||
| 191 | "<br/>\n", | ||
| 192 | "<br/>\n", | ||
| 193 | "\n", | ||
| 194 | "<div id=\"status\" style=\"text-align:center\"><span id=\"statustext\">\n", | ||
| 195 | "Simulation loading</span>.   Time: <span id=\"statustimer\"> </span></div>\n", | ||
| 196 | "\n", | ||
| 197 | "<br/>\n", | ||
| 198 | "\n", | ||
| 199 | "<div class = \"row\">\n", | ||
| 200 | "<div class = \"col-md-4\">\n", | ||
| 201 | "\n", | ||
| 202 | "<br/>\n", | ||
| 203 | "<br/>\n", | ||
| 204 | "<br/>\n", | ||
| 205 | "<br/>\n", | ||
| 206 | "\n", | ||
| 207 | "```yaml jquery=dform\n", | ||
| 208 | "class : form-horizontal\n", | ||
| 209 | "col1class : col-sm-7\n", | ||
| 210 | "col2class : col-sm-5\n", | ||
| 211 | "html:\n", | ||
| 212 | " - name: stopTime\n", | ||
| 213 | " type: number\n", | ||
| 214 | " bs3caption: Stop time, sec\n" | ||
| 215 | }, true)); | ||
| 216 | ✗ | txt := Tpl.pushBlock(txt, Tpl.BT_INDENT(4)); | |
| 217 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING("value: ")); | |
| 218 | ✗ | txt := Tpl.writeStr(txt, realString(i_s_stopTime)); | |
| 219 | ✗ | txt := Tpl.softNewLine(txt); | |
| 220 | ✗ | txt := Tpl.popBlock(txt); | |
| 221 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING_LIST({ | |
| 222 | " - name: intervals\n", | ||
| 223 | " type: number\n", | ||
| 224 | " bs3caption: Output intervals\n" | ||
| 225 | }, true)); | ||
| 226 | ✗ | txt := Tpl.pushBlock(txt, Tpl.BT_INDENT(4)); | |
| 227 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING("value: ")); | |
| 228 | ✗ | txt := Tpl.writeStr(txt, intString(i_s_numberOfIntervals)); | |
| 229 | ✗ | txt := Tpl.softNewLine(txt); | |
| 230 | ✗ | txt := Tpl.popBlock(txt); | |
| 231 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING_LIST({ | |
| 232 | " - name: tolerance\n", | ||
| 233 | " type: number\n", | ||
| 234 | " bs3caption: Tolerance\n" | ||
| 235 | }, true)); | ||
| 236 | ✗ | txt := Tpl.pushBlock(txt, Tpl.BT_INDENT(4)); | |
| 237 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING("value: ")); | |
| 238 | ✗ | txt := Tpl.writeStr(txt, realString(i_s_tolerance)); | |
| 239 | ✗ | txt := Tpl.softNewLine(txt); | |
| 240 | ✗ | txt := Tpl.popBlock(txt); | |
| 241 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING_LIST({ | |
| 242 | "```\n", | ||
| 243 | "\n", | ||
| 244 | "```js\n", | ||
| 245 | "if (typeof(isRunning) == \"undefined\") isRunning = false\n", | ||
| 246 | "\n", | ||
| 247 | "if (typeof(timer) != \"undefined\") {clearInterval(timer.interval); timer = null};\n", | ||
| 248 | "\n", | ||
| 249 | "$(\"#statustext\").html(\'Simulation running\')\n", | ||
| 250 | "$(\"#statustimer\").html(\"\");\n", | ||
| 251 | "$(\'#statustimer\').tinyTimer({ from: Date.now() });\n", | ||
| 252 | "\n", | ||
| 253 | "timer = $(\"#statustimer\").data(\"tinyTimer\")\n", | ||
| 254 | "\n", | ||
| 255 | "// Start the simulation!\n", | ||
| 256 | "basename = \"" | ||
| 257 | }, false)); | ||
| 258 | ✗ | txt := Tpl.writeStr(txt, i_fileNamePrefix); | |
| 259 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING_LIST({ | |
| 260 | "\"\n", | ||
| 261 | "\n", | ||
| 262 | "if (typeof(wworker) != \"undefined\" && isRunning) wworker.terminate();\n", | ||
| 263 | "if (typeof(wworker) == \"undefined\" || isRunning) {\n", | ||
| 264 | " wworker = new Worker(basename + \".js\");\n", | ||
| 265 | " isRunning = true\n", | ||
| 266 | " wworker.addEventListener(\'error\', function(event) {\n", | ||
| 267 | " });\n", | ||
| 268 | " // read the csv file with the simulation results\n", | ||
| 269 | " wworker.addEventListener(\"message\", function(e) {\n", | ||
| 270 | " var data = e.data;\n", | ||
| 271 | " if (data.preloaded) {\n", | ||
| 272 | " preloadComplete = true;\n", | ||
| 273 | " wworker.postMessage({basename: basename, override: {stopTime: stopTime, tolerance: tolerance, stepSize: +stopTime / intervals}});\n", | ||
| 274 | " return;\n", | ||
| 275 | " }\n", | ||
| 276 | " $(\"#statustext\").html(e.data.status)\n", | ||
| 277 | " timer.stop();\n", | ||
| 278 | " isRunning = false\n", | ||
| 279 | " x = $.csv.toArrays(e.data.csv, {onParseValue: $.csv.hooks.castToScalar})\n", | ||
| 280 | "\n", | ||
| 281 | " // `header` has the column names. The first is the time, and the rest\n", | ||
| 282 | " // of the columns are the variables.\n", | ||
| 283 | " header = x.slice(0,1)[0]\n", | ||
| 284 | "\n", | ||
| 285 | " // Select graph variables with a select box based on the header values\n", | ||
| 286 | " if (typeof(graphvar) == \"undefined\") graphvar = header[1];\n", | ||
| 287 | " if (typeof(graphvarX) == \"undefined\") graphvarX = header[0];\n", | ||
| 288 | "\n", | ||
| 289 | " var jsonform = {\n", | ||
| 290 | " html: {\n", | ||
| 291 | " type: \"select\",\n", | ||
| 292 | " bs3caption: \"Plot variable\",\n", | ||
| 293 | " name: \"graphvar\",\n", | ||
| 294 | " selectvalue: graphvar,\n", | ||
| 295 | " choices: header\n", | ||
| 296 | " }};\n", | ||
| 297 | " var jsonformX = {\n", | ||
| 298 | " html: {\n", | ||
| 299 | " type: \"select\",\n", | ||
| 300 | " bs3caption: \"\",\n", | ||
| 301 | " name: \"graphvarX\",\n", | ||
| 302 | " selectvalue: graphvarX,\n", | ||
| 303 | " choices: header\n", | ||
| 304 | " }};\n", | ||
| 305 | " updatefun = function (evt) {\n", | ||
| 306 | " calculate_forms();\n", | ||
| 307 | " $(\"#plotdiv\").calculate();\n", | ||
| 308 | " }\n", | ||
| 309 | "\n", | ||
| 310 | "\n", | ||
| 311 | " $(\"#yaxisform\").html(\"\");\n", | ||
| 312 | " $(\"#yaxisform\").dform(jsonform);\n", | ||
| 313 | " $(\"#yaxisform\").change(updatefun);\n", | ||
| 314 | " $(\"#xaxisform\").html(\"\");\n", | ||
| 315 | " $(\"#xaxisform\").dform(jsonformX);\n", | ||
| 316 | " $(\"#xaxisform\").change(updatefun);\n", | ||
| 317 | " $(\"#plotdiv\").calculate();\n", | ||
| 318 | "\n", | ||
| 319 | "}, false);\n", | ||
| 320 | "}\n", | ||
| 321 | "wworker.postMessage({basename: basename, preload: true})\n", | ||
| 322 | "```\n", | ||
| 323 | "\n", | ||
| 324 | "</div>\n", | ||
| 325 | "\n", | ||
| 326 | "<div class = \"col-md-1\">\n", | ||
| 327 | "</div>\n", | ||
| 328 | "\n", | ||
| 329 | "\n", | ||
| 330 | "<div class = \"col-md-7\">\n", | ||
| 331 | "\n", | ||
| 332 | "<!-- Nav tabs -->\n", | ||
| 333 | "<ul class=\"nav nav-tabs\" id=\"mytab\">\n", | ||
| 334 | " <li class=\"active\"><a href=\"#model\" data-toggle=\"tab\">Model</a></li>\n", | ||
| 335 | " <li><a href=\"#results\" data-toggle=\"tab\">Results</a></li>\n", | ||
| 336 | "</ul>\n", | ||
| 337 | "\n", | ||
| 338 | "<!-- Tab panes -->\n", | ||
| 339 | "<div class=\"tab-content\">\n", | ||
| 340 | " <!-- Model pane -->\n", | ||
| 341 | " <div class=\"tab-pane active\" id=\"model\">\n", | ||
| 342 | "\n", | ||
| 343 | "<img src=\"" | ||
| 344 | }, false)); | ||
| 345 | ✗ | txt := Tpl.writeStr(txt, i_fileNamePrefix); | |
| 346 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING_LIST({ | |
| 347 | ".svg\" style=\"width:100%; background-color:#ffffff; border:2px solid gray\" />\n", | ||
| 348 | "\n", | ||
| 349 | " </div>\n", | ||
| 350 | "\n", | ||
| 351 | " <!-- Results pane -->\n", | ||
| 352 | " <div class=\"tab-pane\" id=\"results\">\n", | ||
| 353 | "\n", | ||
| 354 | "</br>\n", | ||
| 355 | "\n", | ||
| 356 | "<div id=\"yaxisform\" style=\"width:15em; position:relative\"> </div>\n", | ||
| 357 | "\n", | ||
| 358 | "```js id=plotdiv\n", | ||
| 359 | "if (typeof(header) != \"undefined\") {\n", | ||
| 360 | " $(\"#mytab a:last\").tab(\"show\"); // Select last tab\n", | ||
| 361 | " yidx = header.indexOf(graphvar);\n", | ||
| 362 | " xidx = header.indexOf(graphvarX);\n", | ||
| 363 | " // pick out the column to plot\n", | ||
| 364 | " series = x.slice(1).map(function(x) {return [x[xidx], x[yidx]];});\n", | ||
| 365 | " plot([series]);\n", | ||
| 366 | "}\n", | ||
| 367 | "```\n", | ||
| 368 | "\n", | ||
| 369 | "<div id=\"xaxisform\" class=\"center-block\" style=\"text-align:center; width:15em; position:relative\"> </div>\n", | ||
| 370 | "\n", | ||
| 371 | "\n", | ||
| 372 | " </div>\n", | ||
| 373 | "</div>\n", | ||
| 374 | "\n", | ||
| 375 | "</div>\n", | ||
| 376 | "</div>\n", | ||
| 377 | "\n", | ||
| 378 | "\n", | ||
| 379 | "## Comments\n", | ||
| 380 | "\n", | ||
| 381 | "This simulation model is from a [Modelica](http://modelica.org) model.\n", | ||
| 382 | "Modelica is a language for simulating electrical, thermal, and\n", | ||
| 383 | "mechanical, systems. [OpenModelica](http://openmodelica.org) was used\n", | ||
| 384 | "to compile this model to C. Then, [Emscripten](http://emscripten.org/)\n", | ||
| 385 | "was used to compile the C code to JavaScript.\n", | ||
| 386 | "\n", | ||
| 387 | "For more information on compiling OpenModelica to JavaScript, see\n", | ||
| 388 | "[here](https://github.com/tshort/openmodelica-javascript).\n", | ||
| 389 | "\n", | ||
| 390 | "The user interface was created in\n", | ||
| 391 | "[mdpad](http://tshort.github.io/mdpad/). See\n", | ||
| 392 | "[" | ||
| 393 | }, false)); | ||
| 394 | ✗ | txt := Tpl.writeStr(txt, i_fileNamePrefix); | |
| 395 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING(".md](")); | |
| 396 | ✗ | txt := Tpl.writeStr(txt, i_fileNamePrefix); | |
| 397 | ✗ | txt := Tpl.writeTok(txt, Tpl.ST_STRING_LIST({ | |
| 398 | ".md) for the Markdown code\n", | ||
| 399 | "for this page." | ||
| 400 | }, false)); | ||
| 401 | then txt; | ||
| 402 | |||
| 403 | case ( txt, | ||
| 404 | _ ) | ||
| 405 | then txt; | ||
| 406 | end match; | ||
| 407 | end markdownContents; | ||
| 408 | |||
| 409 | annotation(__OpenModelica_Interface="codegen"); | ||
| 410 | end CodegenJS; | ||
| 411 |