The Chartership Forum

Computational Engineering / Programming and automation for engineers

The vocabulary, and what each idea buys

Algorithm, variable, condition, loop, function, data structure — the six ideas an engineer needs, and what each one is for.

Lesson review: expert verification remains open

This lesson has received a full-context editorial review and a check of its technical explanations. Selected arithmetic has been checked separately. This is not independent engineering sign-off.

Checks needed for project use

  • A computational engineering reviewer must check algorithms, convergence claims, statistical assumptions and numerical examples beyond the cases explicitly recorded.

These are educational models and exercises. Check their assumptions before using the results. The full technical content and every diagram have not yet been independently verified.

Concrete and steel calculations retain their stated first-generation Eurocode teaching basis. Use the standards and amendments specified for your project; do not mix generations without an appropriate design basis.

Read the lesson register and outstanding review notes

Why this matters

Basic programming knowledge helps an engineer automate repetitive checks, read a colleague's script and assess a tool before relying on it. This lesson introduces the terms used in the examples and the checks needed around them.

The six

Algorithm. A sequence of unambiguous steps that turns an input into an output. The difficulty is never the sequence; it is the unambiguity. 'Check the beam' is not an algorithm. 'For each load combination, compute the moment; find the section whose moment resistance exceeds it and whose deflection under the serviceability combination is below span/360; return the lightest' is.

Variable and parameter. A named value. Naming it is the point: span rather than 7.2 means the number appears once, and changing it changes every use. Typing the same number in eleven places makes inconsistent updates easy to miss.

Condition. A branch: if the utilisation exceeds one, then take the next section. Conditions are where the engineering judgement gets encoded, and they are where the untested edge cases live. What happens exactly at 1.0? What if the list runs out?

Loop. Repetition. Loops are what make automation worth doing and they are also how one mistake becomes two hundred. A loop over load cases applies your logic — and your error — to every case identically.

Function. A named, reusable piece of behaviour with declared inputs and outputs. This is the unit of reuse, of testing, and of understanding. A function that computes a moment resistance can be tested against a worked example independently of everything around it, and that is what makes the whole assembly trustworthy.

Data structure. How information is organised. A list of load cases, a table of sections, a nested description of a frame. Choosing it badly makes everything downstream awkward; choosing it well makes the code read like the engineering.

Error handling and testing

Two more that are not optional in engineering software.

Error handling. What happens when the input is nonsense — a negative span, an empty section list, a load case with no loads. The right behaviour is nearly always to stop and say so. A tool that returns a plausible number for impossible input is more dangerous than one that crashes, because the crash gets investigated.

Testing. A test is a worked example the computer re-checks every time the code changes. Its value is not that it proves the code right; it is that it catches the day you change something and break something else. Untested code is a hypothesis.

Everything in this course's calculation library is tested this way, and the tests have caught real errors while it was being written — including a beam check that was 12.5 % wrong and would otherwise have been quoted in a lesson.

Try it

Sequence, condition, loop

A member-sizing routine, assembled from its steps. Reorder it or break a condition, and read what the routine would then return.

Break a condition

Each of these is a one-character change in real code.

Use the up and down buttons beside each step to reorder the routine. Everything here works from the keyboard; nothing requires dragging.

  1. 1Read the inputsSpan, load, deflection limit, catalogue.
  2. 2Compute the design momentM = wL²/8 for this case.
  3. 3Sort the catalogue by massLightest first, so the first pass is the lightest that works.
  4. 4Loop through the catalogueOne section at a time.
  5. 5Check strengthIs the moment resistance at least the design moment?
  6. 6Check deflectionIs the deflection under the limit?
  7. 7Return the first section that passes bothAnd stop.
  8. 8If none passes, say soDo not return the last one tried.

The routine is in a working order with sound conditions. It returns the lightest section that satisfies both checks, and says so when none does.

What this shows: A broken routine does not usually crash. It returns a plausible number, which is why a test with a known answer is the only real check.

Check yourself

A colleague's script sizes beams by looping through a catalogue and returning the first section that passes. It has no tests. What is the most useful thing to do before relying on it?

Worked example

Reading a loop that runs one time too few

Given

  • A script places nodes along a beam: for i in range(n): x = i * L / n
  • It is asked for 5 nodes on a 10 m beam

Find

What it produces, and whether the error is visible

    Worked example

    A condition using the wrong scale

    Given

    • A script flags members whose utilisation exceeds 1.0: if utilisation > 1.0: flag(member)
    • Utilisation is computed as a percentage elsewhere in the script

    Find

    What the script reports, and how it would be noticed

      Practice

      A loop for i in range(n) with x = i * L / n is asked for 5 nodes on a 10 m beam. What is the coordinate of the last node it produces, in metres?

      Check yourself

      How can a wrong condition hide an error even when the reported calculations are correct?

      Check yourself

      What should a script that generates a model always report back?

      Summary

      • The hard part of an algorithm is making the steps unambiguous
      • Naming a value once is the difference between one change and eleven
      • Conditions carry the judgement and hide the edge cases
      • A function is the unit of reuse, of testing and of understanding
      • A tool that returns a plausible number for impossible input is worse than one that stops