|
1 | 1 | # ModelingToolkitDesigner.jl |
2 | 2 |
|
3 | | -ModelingToolkitDesigner.jl |
| 3 | +The ModelingToolkitDesigner.jl package is a helper tool for visualizing and editing ModelingToolkit.jl system connections. An example system visualization generated from this package is shown below |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +# Tutorial |
| 9 | + |
| 10 | +Let's start with a simple hydraulic system with no connections defined yet... |
| 11 | + |
| 12 | +```julia |
| 13 | +using ModelingToolkit |
| 14 | +using ModelingToolkitDesigner |
| 15 | +using GLMakie |
| 16 | + |
| 17 | +import ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible as IC |
| 18 | +import ModelingToolkitStandardLibrary.Blocks as B |
| 19 | + |
| 20 | + |
| 21 | +@parameters t |
| 22 | + |
| 23 | +@component function system(; name) |
| 24 | + |
| 25 | + pars = [] |
| 26 | + |
| 27 | + systems = @named begin |
| 28 | + stp = B.Step(;height = 10e5, start_time = 0.005) |
| 29 | + src = IC.InputSource(;p_int=0) |
| 30 | + vol = IC.FixedVolume(;p_int=0, vol=10.0) |
| 31 | + res = IC.Pipe(5; p_int=0, area=0.01, length=500.0) |
| 32 | + end |
| 33 | + |
| 34 | + eqs = Equation[] |
| 35 | + |
| 36 | + ODESystem(eqs, t, [], pars; name, systems) |
| 37 | +end |
| 38 | + |
| 39 | +@named sys = system() |
| 40 | +``` |
| 41 | + |
| 42 | +Then we can visualize the system using `ODESystemDesign()` and the `view()` functions |
| 43 | + |
| 44 | +```julia |
| 45 | +path = "./design" # folder where visualization info is saved and retrieved |
| 46 | +design = ODESystemDesign(sys, path); |
| 47 | +ModelingToolkitDesigner.view(design) |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +Components can then be positioned in several ways: |
| 53 | +- keyboard: select component and then use up, down, left, right keys |
| 54 | +- mouse: click and drag (currently only 1 component at a time is supported) |
| 55 | +- alignment: select and align horrizontal or vertial with respective buttons |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +Nodes/Connectors can be positioned by selecting with the mouse and using the __move node__ button. *Note: Components and Nodes can be de-selected by right clicking anywhere or using the button* __clear selection__ |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +Connections can then be made by clicking 2 nodes and using the __connect__ button. One can then click the __save__ button which will store the visualization information in the `path` location in a `.toml` format as well as the connection code in `.jl` format. |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +The connection code can also be obtained with the `connection_code()` function |
| 68 | + |
| 69 | +```julia |
| 70 | +julia> connection_code(design) |
| 71 | +connect(stp.output, src.input) |
| 72 | +connect(src.port, res.port_a) |
| 73 | +connect(vol.port, res.port_b) |
| 74 | +``` |
| 75 | + |
| 76 | +After the original `system()` component function is updated, the connections will be re-drawn automatically when using `ModelingToolkitDesigner.view()`. *Note: CairoMakie vector based images can also be generated by passing `false` to the 2nd argument of `view()` (i.e. the `interactive` variable). |
| 77 | + |
| 78 | +# Hierarchy |
| 79 | +If a component has a double lined box then it is possible to "look under the hood". Simply select the component and click __open__. |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +Edits made to sub-components can be saved and loaded directly or indirectly. |
| 84 | + |
| 85 | +# Icons |
| 86 | +ModelingToolkitDesigner.jl comes with icons for the ModelingToolkitStandardLibrary.jl pre-loaded. For custom components, icons are loaded from the `path` variable supplied to `ODESystemDesign()`. To find the path ModelingToolkitDesign.jl is searching, pass the system of interest to `ODESystemDesign()` and replace the `.toml` with `.png`. For example if we want to make an icon for the `sys.vol` component, we can find the path by running... |
| 87 | + |
| 88 | +```julia |
| 89 | +julia> ODESystemDesign(sys.vol, path).file |
| 90 | +"./design\\ModelingToolkitStandardLibrary\\Hydraulic\\IsothermalCompressible\\FixedVolume.toml" |
| 91 | +``` |
| 92 | + |
| 93 | +Placing a "FixedVolume.png" file in this location will load that icon. |
| 94 | + |
| 95 | +# Colors |
| 96 | +ModelingToolkitDesigner.jl colors the connections based on `ModelingToolkitDesigner.design_colors`. Colors for the ModelingToolkitStandardLibrary.jl are already loaded. To add a custom connector color, simply use `add_color(system::ODESystem, color::Symbol)` where `system` is a reference to the connector (e.g. `sys.vol.port`) and `color` is a named color from [Colors.jl](https://juliagraphics.github.io/Colors.jl/stable/namedcolors/). |
0 commit comments