{"id":17111486,"url":"https://github.com/jkrt/hif_processes_modelica","last_synced_at":"2026-01-05T06:22:39.523Z","repository":{"id":165910846,"uuid":"637941959","full_name":"JKRT/HIF_Processes_Modelica","owner":"JKRT","description":"Initial proof of concept of handling Zimmer theta operator in Modelica","archived":false,"fork":false,"pushed_at":"2023-05-16T11:54:32.000Z","size":55,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-29T05:32:35.164Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Julia","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JKRT.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-08T18:36:56.000Z","updated_at":"2023-05-16T07:25:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"c290760e-8730-4339-800b-db04da4f799d","html_url":"https://github.com/JKRT/HIF_Processes_Modelica","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/JKRT%2FHIF_Processes_Modelica","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JKRT%2FHIF_Processes_Modelica/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JKRT%2FHIF_Processes_Modelica/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JKRT%2FHIF_Processes_Modelica/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JKRT","download_url":"https://codeload.github.com/JKRT/HIF_Processes_Modelica/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245176402,"owners_count":20572944,"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-14T16:53:58.378Z","updated_at":"2026-01-05T06:22:39.491Z","avatar_url":"https://github.com/JKRT.png","language":"Julia","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HIF_Processes_Modelica\nInitial proof of concept of handling Zimmer theta operator in Modelica.\n\nThere are two instructions provided here, the first is the long install option.\nThese instructions, might require some familiarity with the Julia Language and its ecosystem.\n\nTo test models generated using the new code generator, without generating them on your own machine locally see the examples folder\n\n## How to Install\n\n### Using the new operator for the entire compiler\nNote that this need to be done in order to generate files using this new operator.\nHowever, since this is currently on an experimental branch it might be a bit cumbersome.\n\n- Install OpenModelica.jl by following the instructions here:\n  - https://github.com/JKRT/OM.jl\n- Switch to the follwowing branch: https://github.com/JKRT/OMBackend.jl/tree/when-eq-improvements\n  -It should be noted that this branch is currently under development and things might change, please email me if there are any issues.\n\n## Additional dependencies\n- DifferentialEquations.jl\n- MetaGraphs.jl\n- ModelingToolkit.jl\n- Plots.jl\n- CSV.jl\n- DataFrames.jl\n- JuliaFormatter.jl\n- Graphs.jl\n+ All assorted depencies described at https://github.com/JKRT/OM.jl\n\nOnce this is complete the file *hifProcessTest.jl* may be included.\n\nTo test the various models decribed below under *models*\nSee\n\n```julia\nfunction dynamicTest(;modelName = \"TestThetaMethod.NonlinearCircuit.Test.Circuit1Dynamic\")\n  global file = \"./Models/TestThetaMethod2.mo\"\n  simulateDynamic(;solver = :(FBDF()))\n  sysInfo = experimentWithTeta(;modelName = modelName)\n  global LATEST_SYS_INFO = sysInfo\n  testRun(sysInfo; outputFileName = outputFileNameHelper(modelName))\n#  simulate()\nend\n\n\"\"\"\n  Runs codegen were theta is used in a component\n\"\"\"\nfunction dynamicTestTheta1(;\n                          modelName = \"TestThetaMethod.NonlinearCircuit.Test.ThetaCircuit1Dynamic\")\n  global file = \"./Models/TestThetaMethod2.mo\"\n  simulateDynamicTheta(;solver = :(FBDF()))\n  sysInfo = experimentWithTeta(;modelName = modelName)\n  global LATEST_SYS_INFO = sysInfo\n  testRun(sysInfo; outputFileName = outputFileNameHelper(modelName))\nend\n\n\"\"\"\n  Runs codegen were theta is used at the top level\n\"\"\"\nfunction dynamicTestTheta2(;\n                           modelName = \"TestThetaMethod.NonlinearCircuit.Test.ThetaCircuit2Dynamic\")\n  global file = \"./Models/TestThetaMethod2.mo\"\n  simulateDynamicTheta2(;solver = :(FBDF()))\n  sysInfo = experimentWithTeta(;modelName = modelName)\n  global LATEST_SYS_INFO = sysInfo\n  testRun(sysInfo; outputFileName = outputFileNameHelper(modelName))\nend\n```\n\nIt should be noted that running this code evaluates and generate code just-in-time in the currently Julia environment.\nHence, if they are run twice they will overwrite eachother.\n\nIf you use the system this way the file name of the resulting models will be:\n```\nlowered_sys\u003cqualified-modelica-path\u003e.jl\n```\n\nThe files should be included in your local directory.\nOne such file is included as a starter:\n\n```\nloweredSysTestThetaMethod__NonlinearCircuit__Test__ThetaCircuit2Dynamic.jl\n```\n\nIt should be the same as the files under the Examples directory.\n\n\n### Testing the model using the new operator\nTwo example models have been generated, along with a Modelica program to simulate the same system using OpenModelica.\nThey can be run and examined directely without running the steps above.\n\nTo run the OpenModelica reference file. Navigate to the folder and execute\n\n```\nomc buildDynamic.mos\n````\n\nThe two generated models are called\n\n- sysWithProcess.jl\n- sysWithoutProcess.jl\n\nThese can be executed using the following set of commands, each in its separate terminal since they overwrite each others symbols:\n\n```\n\u003e julia-1.9\ninclude(\"sysWithProcess.jl\")\nusing Plots\nsol = simulateRK4()\nplot(sol, idxs = (1))\n```\n\n\n```\n\u003e julia-1.9\ninclude(\"sysWithoutProcess.jl\")\nusing Plots\nsol = simulate()\nplot(sol, idxs = (1))\n```\n\nTo compare timing between the two variants, one using theta and one without.\n```julia\nusing BenchmarkTools\ninclude(\"sysWithProcess.jl\")\n@benchmark simulateTsit5()\ninclude(\"sysWithoutProcess.jl\")\n@benchmark simulate()\n```\n\nTo experiment with each of these models using different solvers see the following functions:\n\n```julia\nfunction simulateRodas5(; tspan = (0.0, 1.0))\n    prob = problemDef(; tspan = tspan)\n    sol = solve(prob, Rodas5(autodiff = false); reltol = 1e-6, abstol = 1e-6)\n    return sol\nend\n\nfunction simulateTsit5(; tspan = (0.0, 1.0))\n    prob = problemDef(; tspan = tspan)\n    sol = solve(prob, Tsit5(); reltol = 1e-6, abstol = 1e-6);\n    return sol\nend\n\nfunction simulateRK4(; tspan = (0.0, 1.0))\n    prob = problemDef(; tspan = tspan)\n    sol = solve(prob, RK4(); reltol = 1e-6, abstol = 1e-6)\n    return sol\nend\n```\n\nPlease note that the system that is not using theta, that is the system defined by sysWithoutProcess.jl explicit solvers such as RK-4 and Tsit5 will not work.\nHowever, the process that uses Θ will run successfully for both RK4 and for TSIT5.\n\n\n## The Modelica example\nThe example model in Modelica is available under `Models`.\nThe model used to produce the two examples is TestThetaMethod2.mo.\n\nThe two relevant models are:\n\n```modelica\nmodel ThetaCircuit1Dynamic\n  extends Circuit1Static;\nTestThetaMethod.ThetaCapacitator Cnl(C (displayUnit = \"F\")= 1e-12) annotation(\n    Placement(visible = true, transformation(origin = {60, -14}, extent = {{-10, -10}, {10, 10}}, rotation = -90)));\n  equation\n    connect(Cnl.n, ground.p) annotation(\n    Line(points = {{60, -24}, {60, -30}, {0, -30}}, color = {0, 0, 255}));\n    connect(diode.n, Cnl.p) annotation(\n    Line(points = {{60, 10}, {60, -4}}, color = {0, 0, 255}));\n  annotation(\n    Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}})),\n__OpenModelica_simulationFlags(lv = \"LOG_DASSL,LOG_SIMULATION,LOG_STATS\", s = \"dassl\", cpu = \"()\"),\n__OpenModelica_commandLineOptions = \"--matchingAlgorithm=PFPlusExt --indexReductionMethod=dynamicStateSelection -d=initialization,NLSanalyticJacobian -d=infoXmlOperations \");\nend ThetaCircuit1Dynamic;\n```\n\nand\n\n```modelica\nmodel ThetaCircuit2Dynamic\n  parameter Real THETA = 1.0;\n  extends Circuit1Static;\n Modelica.Electrical.Analog.Basic.Capacitor Cp(C (displayUnit = \"F\")= 1e-12 * THETA) annotation(\n    Placement(visible = true, transformation(origin = {60, -14}, extent = {{-10, -10}, {10, 10}}, rotation = -90)));\n  equation\n    connect(Cp.n, ground.p) annotation(\n    Line(points = {{60, -24}, {60, -30}, {0, -30}}, color = {0, 0, 255}));\n    connect(diode.n, Cp.p) annotation(\n    Line(points = {{60, 10}, {60, -4}}, color = {0, 0, 255}));\n  annotation(\n    Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}})),\n__OpenModelica_simulationFlags(lv = \"LOG_DASSL,LOG_SIMULATION,LOG_STATS\", s = \"dassl\", cpu = \"()\"),\n__OpenModelica_commandLineOptions = \"--matchingAlgorithm=PFPlusExt --indexReductionMethod=dynamicStateSelection -d=initialization,NLSanalyticJacobian -d=infoXmlOperations \");\nend ThetaCircuit2Dynamic;\n```\n\nThe first uses theta as a part of a component while the second use it in the parameter modification.\n\n\n## Translation notes\nThe\nModelica.Electrical.Analog.Semiconductors.exlin is not very well documented.\nThe implementation of that function is:\n\n```\nfunction exlin \"Exponential function linearly continued for x \u003e Maxexp\"\n  extends Modelica.Icons.Function;\n  input Real x;\n  input Real Maxexp;\n  output Real z;\nalgorithm\n  z := if x \u003e Maxexp then exp(Maxexp)*(1 + x - Maxexp) else exp(x);\nend exlin;\n```\nThis function is inlined in the frontend.\n\n### The file ThetaExperiment.jl\nThe file `ThetaExperiment.jl` is a temporary file showing MTK structure before\ngeneration of the more low level representation.\n\n## Algorithm high level overview\n- Index reduction performed as before\n- The Θ affected part of the system is extracted using dataflow analysis\n- Construction of code for continuation solvers, those are to be available in G_z\n\n### G_z\nG_z consists of each separate infinitely fast process.\nIt is constructed by means of extracting the relevant structure using static analysis.\n\n### Substeps\n1) We treat Θ as a normal parameter and perform index reduction\n2) The system should be examined post index reduction (This is currently not done)\n3) Θ identify, the variables belonging to the infinitely fast processes and extract this code for continuation solvers\n\n### Error handling and reporting\nExamine the resulting system post index reduction and see if Θ has been applied in the correct way\n\n- Θ is not allowed as an argument to functions\n- Θ can be expressed as a factor of Θ to the power of {0, -1}, ensuring the use of Θ as a time constant\n\nEither a normal time deriviative Θ raised to the power of {0} or infinitely fast, power of {-1}\n\n### Extracting code for continuation solvers from the index-0 system\nThis step describes how the relevant structure is extracted from the system.\nWe extract it by looking at the dependencies of the original system, extracting those parts affected by Θ.\n\n\n## Open questions based on Zimmers paper with artificial states\nHow should the step-size control of the main simulation loop be controlled w.r.t to the\nconvergence speed of the continuation solver?\nCurrently this is left as future work\n\n\n## Ongoing work\nThe previous bullet is currently ongoing work\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjkrt%2Fhif_processes_modelica","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjkrt%2Fhif_processes_modelica","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjkrt%2Fhif_processes_modelica/lists"}