OMCompiler/Compiler/Template/CodegenJS.tpl
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of OpenModelica. | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), | ||
| 5 | * c/o Linköpings universitet, Department of Computer and Information Science, | ||
| 6 | * SE-58183 Linköping, Sweden. | ||
| 7 | * | ||
| 8 | * All rights reserved. | ||
| 9 | * | ||
| 10 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR | ||
| 11 | * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. | ||
| 12 | * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES | ||
| 13 | * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL | ||
| 14 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 15 | * | ||
| 16 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) | ||
| 17 | * Public License (OSMC-PL) are obtained from OSMC, either from the above | ||
| 18 | * address, from the URLs: | ||
| 19 | * http://www.openmodelica.org or | ||
| 20 | * https://github.com/OpenModelica/ or | ||
| 21 | * http://www.ida.liu.se/projects/OpenModelica, | ||
| 22 | * and in the OpenModelica distribution. | ||
| 23 | * | ||
| 24 | * GNU AGPL version 3 is obtained from: | ||
| 25 | * https://www.gnu.org/licenses/licenses.html#GPL | ||
| 26 | * | ||
| 27 | * This program is distributed WITHOUT ANY WARRANTY; without | ||
| 28 | * even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 29 | * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH | ||
| 30 | * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. | ||
| 31 | * | ||
| 32 | * See the full OSMC Public License conditions for more details. | ||
| 33 | * | ||
| 34 | */ | ||
| 35 | |||
| 36 | // Code to generate control files for JavaScript | ||
| 37 | |||
| 38 | package CodegenJS | ||
| 39 | |||
| 40 | import interface SimCodeTV; | ||
| 41 | import CodegenUtil.*; | ||
| 42 | |||
| 43 | ✗ | template markdownFile(SimCode simCode) | |
| 44 | ::= | ||
| 45 | match simCode case SIMCODE(__) then | ||
| 46 | let () = textFile(markdownContents(simCode), '<%fileNamePrefix%>.md') | ||
| 47 | let () = textFile(nodeJSDriver(simCode), '<%fileNamePrefix%>_node.js') | ||
| 48 | "" | ||
| 49 | end markdownFile; | ||
| 50 | |||
| 51 | ✗ | template nodeJSDriver(SimCode simCode) | |
| 52 | ::= | ||
| 53 | match simCode | ||
| 54 | case SIMCODE(simulationSettingsOpt = SOME(s as SIMULATION_SETTINGS(__))) | ||
| 55 | then | ||
| 56 | << | ||
| 57 | #!/usr/bin/nodejs --max-old-space-size=8192 | ||
| 58 | var fs = require('fs'); | ||
| 59 | |||
| 60 | var initXML = fs.readFileSync('./<%fileNamePrefix%>_init.xml'); | ||
| 61 | |||
| 62 | var mod = require('./<%fileNamePrefix%>.js'); | ||
| 63 | mod.FS_createDataFile("/", '<%fileNamePrefix%>_init.xml', initXML, true, false); | ||
| 64 | mod.FS_createLazyFile("/", '<%fileNamePrefix%>_info.xml', '<%fileNamePrefix%>_info.xml', true, false); | ||
| 65 | mod.callMain(process.argv.slice(2)); | ||
| 66 | |||
| 67 | var fname = '<%fileNamePrefix%>_res.<%s.outputFormat%>'; | ||
| 68 | var content = mod.OpenModelica_readFile(fname); | ||
| 69 | fs.writeFileSync(fname,content); | ||
| 70 | >> | ||
| 71 | end nodeJSDriver; | ||
| 72 | |||
| 73 | ✗ | template markdownContents(SimCode simCode) | |
| 74 | ::= | ||
| 75 | match simCode | ||
| 76 | case SIMCODE(modelInfo = MODELINFO(functions = functions, varInfo = vi as VARINFO(__), vars = vars as SIMVARS(__)), | ||
| 77 | simulationSettingsOpt = SOME(s as SIMULATION_SETTINGS(__)), makefileParams = makefileParams as MAKEFILE_PARAMS(__)) | ||
| 78 | then | ||
| 79 | << | ||
| 80 | # OpenModelica simulation example | ||
| 81 | ## <%Util.escapeModelicaStringToXmlString(dotPath(modelInfo.name))%> | ||
| 82 | |||
| 83 | ```yaml script=scriptloader | ||
| 84 | - lib/tinytimer.js | ||
| 85 | ``` | ||
| 86 | |||
| 87 | <style media="screen" type="text/css"> | ||
| 88 | label {font-weight:normal; size: 0.9em} | ||
| 89 | </style> | ||
| 90 | |||
| 91 | <br/> | ||
| 92 | <br/> | ||
| 93 | |||
| 94 | <div id="status" style="text-align:center"><span id="statustext"> | ||
| 95 | Simulation loading</span>.   Time: <span id="statustimer"> </span></div> | ||
| 96 | |||
| 97 | <br/> | ||
| 98 | |||
| 99 | <div class = "row"> | ||
| 100 | <div class = "col-md-4"> | ||
| 101 | |||
| 102 | <br/> | ||
| 103 | <br/> | ||
| 104 | <br/> | ||
| 105 | <br/> | ||
| 106 | |||
| 107 | ```yaml jquery=dform | ||
| 108 | class : form-horizontal | ||
| 109 | col1class : col-sm-7 | ||
| 110 | col2class : col-sm-5 | ||
| 111 | html: | ||
| 112 | - name: stopTime | ||
| 113 | type: number | ||
| 114 | bs3caption: Stop time, sec | ||
| 115 | value: <%s.stopTime%> | ||
| 116 | - name: intervals | ||
| 117 | type: number | ||
| 118 | bs3caption: Output intervals | ||
| 119 | value: <%s.numberOfIntervals%> | ||
| 120 | - name: tolerance | ||
| 121 | type: number | ||
| 122 | bs3caption: Tolerance | ||
| 123 | value: <%s.tolerance%> | ||
| 124 | ``` | ||
| 125 | |||
| 126 | ```js | ||
| 127 | if (typeof(isRunning) == "undefined") isRunning = false | ||
| 128 | |||
| 129 | if (typeof(timer) != "undefined") {clearInterval(timer.interval); timer = null}; | ||
| 130 | |||
| 131 | $("#statustext").html('Simulation running') | ||
| 132 | $("#statustimer").html(""); | ||
| 133 | $('#statustimer').tinyTimer({ from: Date.now() }); | ||
| 134 | |||
| 135 | timer = $("#statustimer").data("tinyTimer") | ||
| 136 | |||
| 137 | // Start the simulation! | ||
| 138 | basename = "<%fileNamePrefix%>" | ||
| 139 | |||
| 140 | if (typeof(wworker) != "undefined" && isRunning) wworker.terminate(); | ||
| 141 | if (typeof(wworker) == "undefined" || isRunning) { | ||
| 142 | wworker = new Worker(basename + ".js"); | ||
| 143 | isRunning = true | ||
| 144 | wworker.addEventListener('error', function(event) { | ||
| 145 | }); | ||
| 146 | // read the csv file with the simulation results | ||
| 147 | wworker.addEventListener("message", function(e) { | ||
| 148 | var data = e.data; | ||
| 149 | if (data.preloaded) { | ||
| 150 | preloadComplete = true; | ||
| 151 | wworker.postMessage({basename: basename, override: {stopTime: stopTime, tolerance: tolerance, stepSize: +stopTime / intervals}}); | ||
| 152 | return; | ||
| 153 | } | ||
| 154 | $("#statustext").html(e.data.status) | ||
| 155 | timer.stop(); | ||
| 156 | isRunning = false | ||
| 157 | x = $.csv.toArrays(e.data.csv, {onParseValue: $.csv.hooks.castToScalar}) | ||
| 158 | |||
| 159 | // `header` has the column names. The first is the time, and the rest | ||
| 160 | // of the columns are the variables. | ||
| 161 | header = x.slice(0,1)[0] | ||
| 162 | |||
| 163 | // Select graph variables with a select box based on the header values | ||
| 164 | if (typeof(graphvar) == "undefined") graphvar = header[1]; | ||
| 165 | if (typeof(graphvarX) == "undefined") graphvarX = header[0]; | ||
| 166 | |||
| 167 | var jsonform = { | ||
| 168 | html: { | ||
| 169 | type: "select", | ||
| 170 | bs3caption: "Plot variable", | ||
| 171 | name: "graphvar", | ||
| 172 | selectvalue: graphvar, | ||
| 173 | choices: header | ||
| 174 | }}; | ||
| 175 | var jsonformX = { | ||
| 176 | html: { | ||
| 177 | type: "select", | ||
| 178 | bs3caption: "", | ||
| 179 | name: "graphvarX", | ||
| 180 | selectvalue: graphvarX, | ||
| 181 | choices: header | ||
| 182 | }}; | ||
| 183 | updatefun = function (evt) { | ||
| 184 | calculate_forms(); | ||
| 185 | $("#plotdiv").calculate(); | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | $("#yaxisform").html(""); | ||
| 190 | $("#yaxisform").dform(jsonform); | ||
| 191 | $("#yaxisform").change(updatefun); | ||
| 192 | $("#xaxisform").html(""); | ||
| 193 | $("#xaxisform").dform(jsonformX); | ||
| 194 | $("#xaxisform").change(updatefun); | ||
| 195 | $("#plotdiv").calculate(); | ||
| 196 | |||
| 197 | }, false); | ||
| 198 | } | ||
| 199 | wworker.postMessage({basename: basename, preload: true}) | ||
| 200 | ``` | ||
| 201 | |||
| 202 | </div> | ||
| 203 | |||
| 204 | <div class = "col-md-1"> | ||
| 205 | </div> | ||
| 206 | |||
| 207 | |||
| 208 | <div class = "col-md-7"> | ||
| 209 | |||
| 210 | <!-- Nav tabs --> | ||
| 211 | <ul class="nav nav-tabs" id="mytab"> | ||
| 212 | <li class="active"><a href="#model" data-toggle="tab">Model</a></li> | ||
| 213 | <li><a href="#results" data-toggle="tab">Results</a></li> | ||
| 214 | </ul> | ||
| 215 | |||
| 216 | <!-- Tab panes --> | ||
| 217 | <div class="tab-content"> | ||
| 218 | <!-- Model pane --> | ||
| 219 | <div class="tab-pane active" id="model"> | ||
| 220 | |||
| 221 | <img src="<%fileNamePrefix%>.svg" style="width:100%; background-color:#ffffff; border:2px solid gray" /> | ||
| 222 | |||
| 223 | </div> | ||
| 224 | |||
| 225 | <!-- Results pane --> | ||
| 226 | <div class="tab-pane" id="results"> | ||
| 227 | |||
| 228 | </br> | ||
| 229 | |||
| 230 | <div id="yaxisform" style="width:15em; position:relative"> </div> | ||
| 231 | |||
| 232 | ```js id=plotdiv | ||
| 233 | if (typeof(header) != "undefined") { | ||
| 234 | $("#mytab a:last").tab("show"); // Select last tab | ||
| 235 | yidx = header.indexOf(graphvar); | ||
| 236 | xidx = header.indexOf(graphvarX); | ||
| 237 | // pick out the column to plot | ||
| 238 | series = x.slice(1).map(function(x) {return [x[xidx], x[yidx]];}); | ||
| 239 | plot([series]); | ||
| 240 | } | ||
| 241 | ``` | ||
| 242 | |||
| 243 | <div id="xaxisform" class="center-block" style="text-align:center; width:15em; position:relative"> </div> | ||
| 244 | |||
| 245 | |||
| 246 | </div> | ||
| 247 | </div> | ||
| 248 | |||
| 249 | </div> | ||
| 250 | </div> | ||
| 251 | |||
| 252 | |||
| 253 | ## Comments | ||
| 254 | |||
| 255 | This simulation model is from a [Modelica](http://modelica.org) model. | ||
| 256 | Modelica is a language for simulating electrical, thermal, and | ||
| 257 | mechanical, systems. [OpenModelica](http://openmodelica.org) was used | ||
| 258 | to compile this model to C. Then, [Emscripten](http://emscripten.org/) | ||
| 259 | was used to compile the C code to JavaScript. | ||
| 260 | |||
| 261 | For more information on compiling OpenModelica to JavaScript, see | ||
| 262 | [here](https://github.com/tshort/openmodelica-javascript). | ||
| 263 | |||
| 264 | The user interface was created in | ||
| 265 | [mdpad](http://tshort.github.io/mdpad/). See | ||
| 266 | [<%fileNamePrefix%>.md](<%fileNamePrefix%>.md) for the Markdown code | ||
| 267 | for this page. | ||
| 268 | >> | ||
| 269 | end markdownContents; | ||
| 270 | |||
| 271 | annotation(__OpenModelica_Interface="codegen"); | ||
| 272 | end CodegenJS; | ||
| 273 | |||
| 274 | // vim: filetype=susan sw=2 sts=2 | ||
| 275 |