{"id":17227711,"url":"https://github.com/tshort/openmodelica-javascript","last_synced_at":"2025-04-14T01:23:46.705Z","repository":{"id":10743819,"uuid":"13001431","full_name":"tshort/openmodelica-javascript","owner":"tshort","description":"Code to compile OpenModelica models to JavaScript","archived":false,"fork":false,"pushed_at":"2014-02-21T19:28:20.000Z","size":25642,"stargazers_count":66,"open_issues_count":3,"forks_count":15,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-03-27T15:21:48.836Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tshort.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-09-21T19:59:58.000Z","updated_at":"2025-03-05T18:56:52.000Z","dependencies_parsed_at":"2022-08-28T17:02:22.161Z","dependency_job_id":null,"html_url":"https://github.com/tshort/openmodelica-javascript","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tshort%2Fopenmodelica-javascript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tshort%2Fopenmodelica-javascript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tshort%2Fopenmodelica-javascript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tshort%2Fopenmodelica-javascript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tshort","download_url":"https://codeload.github.com/tshort/openmodelica-javascript/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248805595,"owners_count":21164357,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T04:20:15.986Z","updated_at":"2025-04-14T01:23:46.679Z","avatar_url":"https://github.com/tshort.png","language":"C","funding_links":[],"categories":["C"],"sub_categories":[],"readme":"# OpenModelica models in JavaScript\n\n[OpenModelica](http://openmodelica.org) is an open-source compiler for\nthe [Modelica](http://modelica.org) language. Modelica is a language for\nsimulating systems (electrical, mechanical, and many more).\n\nThe files in this repository include some files to help OpenModelica\ncompile models to JavaScript using\n[Emscripten](http://emscripten.org/).\n\nThanks to Martin Sjölund (a core OpenModelica developer), OpenModelica\nhas a backend that supports compilation with Emscripten. \n\n## Examples\n\nOverall, the user experience works relatively well. All the\ncalculations are done on the client. Here are several examples:\n\n- [Modelica.Blocks.Examples.PID_Controller](http://tshort.github.io/mdpad/mdpad.html?Modelica.Blocks.Examples.PID_Controller.md)\n- [Modelica.Electrical.Analog.Examples.ChuaCircuit](http://tshort.github.io/mdpad/mdpad.html?Modelica.Electrical.Analog.Examples.ChuaCircuit.md)\n- [Modelica.Electrical.Analog.Examples.Rectifier](http://tshort.github.io/mdpad/mdpad.html?Modelica.Electrical.Analog.Examples.Rectifier.md)\n- [Modelica.Mechanics.MultiBody.Examples.Systems.RobotR3.fullRobot](http://tshort.github.io/mdpad/mdpad.html?Modelica.Mechanics.MultiBody.Examples.Systems.RobotR3.fullRobot.md)\n\nSee [here](https://github.com/tshort/mdpad/tree/gh-pages/) for the\nsource code for these examples. Here are examples from my area of\nwork:\n\n- [Ferroresonance](http://distributionhandbook.com/calculators/mdpad.html?ferro.md)\n- [Switching transients on a network](http://distributionhandbook.com/calculators/mdpad.html?UrbanPrimary.md)\n\nYou wouldn't want to run complex Monte Carlo analysis with this, but\nmany models should run sufficiently fast for use on the web. With\nadvances in Emscripten and a few tweaks to OpenModelica, models in\nFirefox run within about 1.5X of native, and in the most recent\nChrome, they run within 2X of native. The key to the speed is that\nEmscripten compiles to the asm.js format which can be optimized very\nwell. The first run takes more time because the files need to be\ndownloaded and compiled. \n\nThe JavaScript file generated by Emscripten is almost 2 MB (about 0.5\nMB gzipped) for models with complexity similar to examples in the\nModelica Standard Library. The most complex model I've tried is the\nMultiBody.Examples.Loops.EngineV6 that was about 8 MB. Some options to\ntry to reduce the page size are:\n\n- Look for more stuff to strip out (one could strip out all solvers but\n  DASSL for example).\n- Replace some of the system code with something already built in. For\n  example, it might be possible to replace `expat` with JavaScript's\n  own XML processing functionality.\n\nThe gui for these pages was done using\n[mdpad](http://tshort.github.io/mdpad/), another web technology I'm\nexperimenting with. You can of course write your own JavaScript gui\nthat interfaces with the simulation code.\n\n## Setting up Compilation\n\nTo set up to compile your own model, here are the steps needed:\n\n- Install [Emscripten](http://emscripten.org/).\n- Make sure Emscripten works and paths to `emcc` are set up right.\n- Install OpenModelica with at least SVN revision 18828.\n- Copy all of the object files (*.so) and pre.js into the build\n  directory of OpenModelica at this location (you may need to create\n  the directory):\n  - build/lib/omc/emcc/\n\nIf you want to compile the Modelica libraries (as OpenModelica gets\nupdated), you can run the following at the shell from your\nOpenModelica source location:\n\n    make -j4 -C SimulationRuntime/c emcc\n    make -j4 omc\n\nThis will update the following files:\n  - build/lib/omc/emcc/libf2c.so\n  - build/lib/omc/emcc/libSimulationRuntimeC.so\n\nThis repository also contains the Emscripten compiled code for\nLAPACK/BLAS and expat.\n\nNote that I have only tested Emscripten compilation with Linux. I\ndon't know if it will work out-of-the-box under Windows.\n\n## Compiling your model\n\nTo compile your own model, create a modelica script (*.mos) file\nsomething like:\n    \n    loadModel(Modelica);\n    setCommandLineOptions(\"+simCodeTarget=JavaScript\");\n    buildModel(Modelica.Electrical.Analog.Examples.ChuaCircuit);\n\nRun this in a shell with something like:\n\n    omc chua.mos\n\nThis will compile the model to JavaScript. This will generate many\nother files, too. The most important files are:\n\n- Modelica.Electrical.Analog.Examples.ChuaCircuit.js\n- Modelica.Electrical.Analog.Examples.ChuaCircuit_init.xml\n- Modelica.Electrical.Analog.Examples.ChuaCircuit.md\n\nThe `js` file is the JavaScript code. The `xml` file is the\ninitialization file. You will likely want to modify some of the\ndefaults. The `md` file is a Markdown file that describes a user\ninterface using [mdpad](http://tshort.github.io/mdpad/).\n\n## Set up the Model with a Web Interface\n\nTo run your compiled JavaScript model in a browser, clone the mdpad\nfiles from here:\n\n- https://github.com/tshort/mdpad/tree/gh-pages/\n\nCopy the three files (js, xml, and md) created by the call to omc to\nyour newly cloned mdpad directory.\n\nLaunch a web server from within your mdpad directory. I use `python -m\nSimpleHTTPServer` which starts a webserver on port 8000.\n\nBrowse to your model, something like\n`http://localhost:8000/mdpad_local.html?Modelica.Electrical.Analog.Examples.ChuaCircuit.md`.\n\n## Fixing up the Model\n\nThere are a few tweaks you will normally want to do. First, you can\nedit the Markdown file to better describe your model. You can also\nchange the inputs and outputs in this file. Using the Chua circuit as\nan example, the following Markdown code defines the form inputs on the\npage. This is YAML code that describes the form elements. In this\nexample, `L`, `C1`, and `C2` are inputs to the model that are specific\nto this simulation, so modify these are add similar entries for your\nmodel.\n\n    ```yaml jquery=dform\n    class : form-horizontal\n    col1class : col-sm-7\n    col2class : col-sm-5\n    html: \n      - name: stopTime\n        type: number\n        bs3caption: Stop time, sec\n        value: 10000.0\n      - name: intervals\n        type: number\n        bs3caption: Output intervals\n        value: 500\n      - name: tolerance\n        type: number\n        bs3caption: Tolerance\n        value: 0.0001\n      - name: L\n        type: number\n        bs3caption: L, henries\n        value: 18.0\n      - name: C1\n        type: number\n        bs3caption: C1, farads\n        value: 10.0\n      - name: C2\n        type: number\n        bs3caption: C2, farads\n        value: 100.0\n    ```\n\nNext, you need to tie these input elements to model parameters in the\nxml file. Do this by changing a JavaScript block within the Markdown\nfile. Here is an example for the three variables set in the Chua\nexample:\n\n    // Set some model parameters\n    $xml.find(\"ScalarVariable[name = 'L.L']\").find(\"Real\").attr(\"start\", L)\n    $xml.find(\"ScalarVariable[name = 'C1.C']\").find(\"Real\").attr(\"start\", C1)\n    $xml.find(\"ScalarVariable[name = 'C2.C']\").find(\"Real\").attr(\"start\", C2)\n\nJavaScript uses the CSV outputFormat, and this can be quite slow in\nEmscripten-compiled code. So, simulations run much faster if you\nreduce the amount of output from the simulation. You can do that by\nchanging the `variableFilter` input in the `_init.xml` file. Here is\nan example for the Chua circuit to just show two voltages and two\ncurrents:\n\n    variableFilter = \"C1.v|C2.v|L.i|Nr.i\" /\u003e\n\nYou could also change this in the `md` file by adding a line like the\nfollowing:\n\n    defex = $xml.find(\"DefaultExperiment\")\n    defex.attr(\"variableFilter\", \"C1.v|C2.v|L.i|Nr.i\")\n\nLastly, this interface shows an image of the model. You need to create\nthis image file as follows. In OMEdit, open your model. Right click in\nthe area with your model visible. Select \"Export as an image\". Save as\nthe full model name with an `svg` extension in the mdpad directory.\nNow, your model should be visible when you reload the model in the\nbrowser.\n\n## Status\n\nEverything here is experimental at this point. I've tried this with\nseveral models. I've left off some system libraries like Sundials, so\nsome solvers and functionality might not work.\n\nAll this code is free and open source. The OpenModelica code is under\nvarious licenses. The files that I made are granted to the public\ndomain (or alternatively under the MIT license).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftshort%2Fopenmodelica-javascript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftshort%2Fopenmodelica-javascript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftshort%2Fopenmodelica-javascript/lists"}