CBD.rst.txt 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. CBD Formalism
  2. =============
  3. Causal Block Diagrams allow for finding solutions to mathematical problems.
  4. Setting up Drawio
  5. ------------------
  6. In drawio, select :menuselection:`File --> Open Library` and load :file:`CBDLibrary.xml`
  7. (which can be found :download:`here <../../formalisms/CBD/CBDLibrary.xml>`).
  8. This provides a set of primitive CBD blocks that can be used in your models.
  9. Once included, it can be found at the top of the shapes library.
  10. .. rst-class:: table-center
  11. +------------------------------------------+
  12. | .. figure:: ../_figures/library.png |
  13. | |
  14. | CBD Library in the Sidebar |
  15. +------------------------------------------+
  16. .. _block-props:
  17. Block Properties
  18. ----------------
  19. All blocks in the library have some block-specific properties that can be set by the user.
  20. Hovering over a component shows all non-empty properties that were set on this shape.
  21. There are several ways of accessing/modifying these properties:
  22. * Double Click with the left mouse button on a shape.
  23. * Click with right mouse button to open a shape's context menu.
  24. Near the bottom of the list, select :menuselection:`Edit Data`.
  25. * Select the shape (left mouse button) and in the top of the right panel,
  26. go to :menuselection:`Arrange --> Edit Data`.
  27. * Select the shape and press :kbd:`CTRL + M` or :kbd:`CMND + M`.
  28. .. warning:: Do not change the :code:`class_name` as the converter relies on this property.
  29. .. rst-class:: table-center
  30. +------------------------------------------+
  31. | .. figure:: ../_figures/properties.png |
  32. | |
  33. | Example of the Property Window (for a |
  34. | ConstantBlock) |
  35. +------------------------------------------+
  36. .. _library:
  37. Working with Library Blocks
  38. ---------------------------
  39. To guarantee a consistent visual appearance of CBD models,
  40. no blocks in the library, except for the Custom Block, can be resized.
  41. Every block has zero or more input and output ports. Input ports are represented by
  42. the :code:`InputPortBlock` (black triangle) and output ports by the :code:`OutputPortBlock`
  43. (white triangle). When selecting (left mouse button) a port, a small, movable,
  44. yellow/orange diamond-shaped anchor will appear in front of the port's name.
  45. The position of this label can be changed by dragging the anchor.
  46. The port name can be altered in the :menuselection:`Edit Data` window.
  47. .. For each of a CBD model's top-level :code:`OutputPortBlock` shapes, a :code:`signal` property
  48. may be added (see :ref:`block-props`).
  49. The :code:`drawio2cbd` will then generate code to produce a plot, using https://bokeh.org/.
  50. Note that bokeh produces an HTML document which is rendered in a browser
  51. and allows some user interaction with the plot.
  52. The value of the :code:`signal` property on a top-level :code:`OutputPortBlock` should be the title
  53. to be put on the plot. Multiple signals may be rendered onto the same plot.
  54. For more complex CBD simulation result analysis and visualization, code should be added
  55. at the bottom of the generated file.
  56. Each CBD block that is *not* a port may have the following properties:
  57. * :code:`ID` or :code:`id`: The drawio unique ID for this block. Note that drawio automatically
  58. overwrites a property with the key :code:`id` by the value shown by :code:`ID` (upon saving the diagram).
  59. * :code:`block_name`: The name of the block. It is required that
  60. all blocks have a unique name within a class. When omitted, the :code:`ID` is used.
  61. * :code:`class_name`: The type of block.
  62. .. warning:: Do not change the :code:`class_name` as the converter relies on this property.
  63. Changing it makes it so the block type changes as well in the simulation.
  64. * :code:`symbol`: The block symbol that must be rendered by MathJax/LaTeX. This is a purely
  65. graphical attribute and is ignored by the script.
  66. Optionally, additional class parameters can be provided by adding more properties.
  67. Their values will be passed on as default values for the corresponding property as an argument, e.g., to
  68. use strings, enclose the value in "..." or '...', for :code:`None`, leave it empty or litterally type
  69. ":code:`None`"...
  70. .. warning:: Make sure these values are expressions/rvalues! If not, the generated Python file will crash,
  71. or cause some unexpected behavior. Furthermore, newlines may not be used in these values.
  72. Class parameters that cannot be used are: :code:`label` (prevented by drawio), :code:`id` (overwritten by
  73. drawio), :code:`placeholders` (prevented by drawio), :code:`symbol` (ignored by the converter)
  74. and :code:`__docstring__` (used for other purposes, see later).
  75. .. note:: Blocks with a variable number of inputs (i.e., the :code:`OrBlock` and the
  76. :code:`AndBlock`) may still need additional ports to be placed/removed.
  77. .. _custom-block:
  78. Creating Custom CBD Blocks
  79. -------------------------------
  80. A custom hierarchical CBD block is constructed as a network of already provided blocks
  81. (which in their own right may be custom hierarchical blocks)
  82. using the :code:`Custom Block` element from :file:`CBDLibrary.xml`.
  83. This is a collapsible component in which a block diagram may be drawn.
  84. It has two important properties:
  85. * :code:`class_name`: The new class name for the custom block. Note that all class names must be unique!
  86. * :code:`block_name`: The name of an instance of the CBD model if it's the top-level CBD.
  87. Additional properties can be added to create custom class parameters.
  88. The same set of parameters that was discussed above cannot be set,
  89. with the exception of :code:`__docstring__`, which now allows adding documentation to custom blocks.
  90. The empty rectangle is a container for the custom hierachical block.
  91. Blocks can be dragged into the rectangular area and connected.
  92. :code:`InputPortBlock` and :code:`OutputPortBlock` are used to add inputs and outputs with
  93. their :code:`name` property set to the port's name.
  94. .. hint:: The :code:`Custom Block` component can, in constrast with all other blocks in the
  95. library, be resized. This allows for larger hierarchical models to be created, as the
  96. basic blocks cannot be resized.
  97. .. rst-class:: table-center
  98. +------------------------------------------+
  99. | .. figure:: ../_figures/custom-block.png |
  100. | |
  101. | Custom Sine Generator Block with no |
  102. | Input Ports, a single Output Port and |
  103. | a Custom Graphical Representation |
  104. +------------------------------------------+
  105. Next, create a graphical representation of the block (i.e., what an instance
  106. block should look like when used in a block diagram), add the corresponding
  107. ports and set the property :code:`class_name` to the same class as that set in the
  108. :code:`Custom Block`. Make sure not to forget to add any class parameters that were
  109. added to the :code:`Custom Block`.
  110. .. hint:: This can be done easily by adding a predefined block and changing its
  111. ports and properties to match the new custom block.
  112. .. note:: Only the :code:`Custom Block` components will be read by the converter.
  113. Anything else is implicitly ignored. Furthermore, the script is page-independent,
  114. meaning multiple pages may be used inside a single drawio document to maintain a
  115. clean overview of the created models.
  116. Conversion
  117. ----------
  118. To generate code from the created drawio files, execute the script with the :code:`-F CBD` flag.
  119. This tells :code:`DrawioConvert` to use the CBD formalism. Two files will be created:
  120. * :code:`model.py`: The model file, containing the code for the modelled blocks.
  121. * :code:`experiment.py`: The "protected" experiment file. You can feely add/remove parts of this file
  122. without fear of it being overwritten next execution. Use the :code:`-f`/:code:`--force` flag to
  123. force overwrite the file.
  124. It is possible to use continuous models, where they are discretized to a certain delta. This is
  125. customizeable using the :code:`-E`/:code:`--environment` flag. I.e. by setting :code:`-E delta=0.1`,
  126. it is possible to execute in steps of 0.1 time-units. Note how :code:`-t`/:code:`--time` identifies
  127. the total simulation time, **not** the amount of steps taken.