10.014 CTD
  • Overview
  • Schedule
  • Administrative
    • Accessing Rhino remotely
    • Rhino for Mac
  • ASSIGNMENTS
    • Dates and rubrics
    • Generative design
      • Generative design
    • Parametric design
      • Parametric design
    • Simulated design
      • Simulated design
      • Simulated design
  • SESSION 1B
    • Computer Aided Design
    • Ranges and expressions 1
      • Ranges and expressions 2
      • Ranges and expressions 3
      • Ranges and expressions 4
      • Ranges and expressions 5
      • Ranges and expressions 6
  • SESSION 2A
    • Visual programming 1
      • Visual programming 2
      • Visual programming 3
      • Visual programming 4
    • Associative modelling 1
      • Associative modelling 2
      • Associative modelling 3
  • SESSION 2B
    • Logical Patterns 1
      • Logical patterns 2
      • Logical patterns 3
  • SESSION 3A
    • Spatial geometry 1
      • Spatial geometry 2
      • Spatial geometry 3
      • Spatial geometry 4
      • Spatial geometry 5
      • Spatial geometry 6
      • Spatial geometry 7
    • Curve geometry 1
      • Curve geometry 2
      • Curve geometry 3
      • Curve geometry 4
  • SESSION 3B
    • Surface geometry
    • Parametric modelling 1
      • Parametric modelling 2
      • Parametric modelling 3
      • Parametric modelling 4
  • SESSION 4A
    • Information nesting 1
      • Information nesting 2
      • Information nesting 3
    • Data landscapes 1
      • Data landscapes 2
      • Data Landscapes 3
      • Data landscapes 4
  • SESSION 4B
    • Mesh geometry 1
      • Mesh geometry 2
      • Mesh geometry 3
  • SESSION 5A
    • Space and time 1
      • Space and time 2
    • Modelling entities 1
      • Modelling entities 2
      • Modelling entities 3
  • SESSION 5B
    • Multibody dynamics 1
      • Multibody dynamics 2
    • Material elasticity 1
      • Material elasticity 2
      • Material elasticity 3
  • SESSION 6A
    • Form-finding 1
      • Form-finding 2
      • Form-finding 3
      • Form-finding 4
  • SESSION 6B
    • AI Image generation 1
      • AI Image generation 2
      • AI Image generation 3
  • APPENDIX
    • Spirograph 1
      • Spirograph 2
    • Curves
    • Swarm Intelligence 1
      • Swarm Intelligence 2
    • Hybrid programming 1
      • Hybrid programming 2
Powered by GitBook
On this page
  • Control Flow
  • Conclusions
  1. SESSION 2B
  2. Logical Patterns 1

Logical patterns 3

PreviousLogical patterns 2NextSpatial geometry 1

Last updated 8 months ago

Control Flow

We discussed the concept of generating conditional outputs using “if” expressions. In this section we refine this idea to general execution control flow of information using conditionals.

Let’s assume that given a number “value” we wish to compute its inverse “1 / value” or “0”, if the number is zero ie. to avoid division by zero. Using conditionals with the “if” function in the expression editor has the following outcome: The component just blatantly fails; but why?

if( value = 0, 0, 1 / value )

The reason for this is based on the sequence of events that take place when we evaluate expressions. In order for the “if” function to be executed, all its supplied arguments must be evaluated beforehand. At first the conditional sub-expression “value = 0” is evaluated, then the constant expression “0”, and finally the numeric division is performed. This means that in every scenario, whether the “value” is zero or not, the division will be performed!

Branching Logic

What we need is a construct that captures the idea of only one outcome expression being evaluated, based on a condition, but not both. We call this behaviour branching because only one path of logic is ever taken. We can achieve this using the approach seen below. Notice how parts of the graph turns orange which signifies that they become inactive based on the value of the slider.

There are two new components used here that allow us to achieve this conditional execution behaviour: “stream gate” and “stream filter”.

The “stream gate” component redirects the input data on either output based on the boolean value supplied at the gate. It behaves like a valve for fluids, hence the stream analogy perhaps. Nevertheless, we need a symmetric opposite of this to merge back the flow of execution so we use the “stream filter”.

The “stream filter” component selects either of the two stream inputs based on the boolean value supplied at the gate. Notice that the output name changes based on which stream is selected.

Practice

  • The “stream gate” component by itself might not appear very useful but you can create a “warning” system as means of trapping invalid user input

  • Instead of the “stream filter” you may use a “list item” component to select the first or second item from a list or even “addition” to merge results. Experiment with various merging techniques.

Conditional Statements

Using gates in visual programming sense feels a bit verbose and crufty. The graph below shows the equivalent of the “expression” editor using the “python” component instead. Python has no problem with our number inversion logic because “if” is not a function, which forces the evaluation of its arguments. It is a different construct altogether created exactly for the purposes of controlling the flow of data.

B = 0 if( A == 0 ) else 1 / A

Conclusions

We investigated the most common uses of conditional expressions, namely to filter data and to control computational flow. Filtering is a critical tool for data processing operations. Semantically, it allow us to select or discard values and introduce abrupt discontinuities, in the otherwise smooth flow of sequences. It is the foundation for expressing patterns that capture logic.

We derived the need for control flow to avoid disaster i.e. dividing numbers by zero. Instead of filtering for data values, we filtered i.e. allowed of disallowed computing instructions from being performed. Conditional execution is almost synonymous with computing; it is a prerequisite in the sense without it, we would have been able to perform only complex calculations. With the ability to express decision making, we can alter the flow of events without even always resulting in a predetermined result.

Boolean operations are typically applied to true/false data values. However, if we represent integers as sequences of on/off bits, we can extend the use of boolean algebra to groups of bits. Those are called bitwise operations and as they are not essential, additional information is offered as bonus materials [>].

18KB
5_ControlFlow.gh
Drawing
Drawing