Gains:
- Ability to automate engineering calculation, unit management and data processing with AI-powered Python code
- Ability to validate AI code with unit checking, known result testing, and edge cases
- Ability to gain the habit of producing repeatable, traceable and version-controlled account documents
In mechanical engineering, the same calculation is done over and over again: stresses of a family of parts, pump power for a range of operating points, property tables at different temperatures. Doing these manually is both slow and error-prone. Python (an easy-to-learn programming language with rich libraries for engineering) automates these iterations; It makes the account repeatable, traceable and version controlled. Artificial intelligence (AI) Python is incredibly fast at generating code: converting a formula into a function, adding unit management, reading data, plotting graphs. But there is a dangerous misconception here: just because the code works without errors does not mean that it calculates correctly. The AI code may silently return incorrect results due to incorrect unit conversion, incorrect formula, or in edge cases, and the program will continue to run without any errors. That's why every engineering code produced with AI; Test inputs with known results are unreliable without verification by unit (size) checking and edge case trials. In this unit, you will learn how to safely set up Python account automation with AI.
Why Code Account? Traceability and Reproducibility
A manual calculation is a one-off; When an input changes, it is done from scratch and intermediate steps are lost. Calculation done in code is like a document: inputs, formulas and outputs are clearly written; you change an input and get a new result within seconds; With version control (like git), "what I calculated with which value on which date" can be tracked. This is invaluable in terms of control and accountability. But this power depends on the correctness of the code; A wrong code produces the wrong result, also reproducibly and quickly.
Tip: Write a test for each compute function with a known true result next to it (assert in Python). For example, your stress function should give 28.1 MPa in a known sample. This test immediately warns you if you break something when you change the code in the future. Engineering code that is not tested is an unverified account.
Volume Management: Most Common Source of Error
In the engineering code, most of the errors come from units: N with kN, m with mm, Pa with MPa, which can be confused by a factor of 1000 or 1,000,000. There are two defenses. The first is discipline: choosing a single unit system from the beginning (e.g. N, mm, MPa) and converting all inputs to it and adding units to the variable names (length_mm, force_N). The second is the tool: a library like pint carries the units within the code and catches the inconsistent operation as an error.
Approach
How does it work
Advantage
Naming discipline
like force_N, length_mm
Simple, no dependencies
single unit system
All converted to N-mm-MPa
Simplicity, speed
pint library
Moves unit by variable
Automatically catches inconsistency
Known result test
reference with assert
Catches formula/unit error
Caution: A unit conversion may be missing or incorrect in the AI-generated code and the code will still "work". For example, if the diameter is mm and the area is expected to be m², the result will deviate by 1,000,000 times, but the program will not give an error. Before running the code, comment out the units of the inputs and output; then provide the result with a known example.
Step by Step: AI Verifiable Account Code
- Clarify the problem and unit system. Inputs, outputs, units.
- Generate the function. Single responsible, interpretive, united.
- Add known results test. Assert with reference example.
- Try edge cases. Zero, negative, very large/small input.
- Do a unit check. Does the unit of output match what is expected?
- Document and version. Assumptions, source, date; traceability with git.
Prompt that generates functions and tests
Role: Experienced Python developer writing engineering calculations. Task: Write a function that calculates the maximum bending stress in a rectangular cross-section cantilever beam. Input: F (N), L (mm), b (mm), h (mm). Output: sigma (MPa).Convention: Unit system N-mm-MPa; comment the unit of each entry.Rule: use I = b*h^3/12 and sigma = M*c/I; comment the steps.Rule: Add a test with KNOWN RESULT: sigma ~28.1 MPa for F=500,L=300,b=20,h=40; Check with assert (small tolerance).
Edge status prompt
Add edge case checks to the above function:- If b, h or L are zero or negative, give a significant error (raise ValueError).- Comment if there is an overflow/precision problem with very large/small inputs. Also add 3 more different test inputs and write the expected result; explain the results in a way that I can verify them manually.
Unit security (pint) prompt
Unit-safe the same account with the 'pint' library. Let inputs be defined in units (e.g. 500 * ureg.newton). Convert the output to MPa and print it. Add a small example showing how pint fails when given an input with the wrong unit.
Code check prompt
Critique my engineering calculation code below from a code review perspective, don't agree with me. In particular: is the unit conversion correct, is the formula correct, have edge cases (zero, negative) been considered, are the tests truly confirmatory? For each finding, write how to fix it.[code]
Weak Prompt / Strong Prompt
Weak prompt:
Write Python code for stress calculation.
No choice of units, formulas, input definitions and tests; AI generates code that works but is unverified and whose unit is unknown.
Powerful prompt:
Write the bending stress function in the cantilever beam. Input F(N), L(mm), b(mm),h(mm); output sigma(MPa). N-mm-MPa system, specify each unit in the comment. Add a test with known results (F=500, L=300, b=20, h=40 → ~28.1 MPa, assert). Give an error on zero/negative input. Interpret edge cases.
The second prompt requires the unit system, formula, inputs, test, and edge cases; It makes the code verifiable.
Three Mini Cases (By Numbers)
Case 1 - Silent volume error. The area calculation produced by AI takes the diameter in mm and gives mm² with pi*d**2/4, but the next line puts it into a formula that expects m²; The code works without errors and gives the stress 1,000,000 times lower. When the engineer runs the test with a known result (assert abs(sigma-28.1)<0.5), the test explodes and the error is caught. If there was no test, the wrong result would have entered the report unnoticed. Lesson: working code ≠ correct code.
Case 2 - Edge state crash. In the code that loops for a part family, thickness h=0 is entered in one line; When I = b*h**3/12 = 0, sigma = M*c/I gives a division by zero error. Thanks to the if h<=0: raise ValueError control added by AI, the code stops with a meaningful message and does not silently produce inf. Lesson: handle edge cases beforehand.
Case 3 - Repeatability gain. It took an engineer half a day to manually calculate the pump power for 40 different operating points. Written in AI, the script reads the CSV, calculates the power for each line, and verifies a known point with assert, reducing the work to ~2 minutes and writing the results to a traceable file. When an entry changes, the entire table is updated every second. Lesson: verified automation is both fast and reliable.
Common mistakes
- "Worked = correct" fallacy: Thinking that code that works without errors is correct.
- Not writing tests: Relying on code without a reference test with a known result.
- Unit ambiguity: Leaving input/output units uninterpreted, skipping conversion.
- Ignoring edge cases: Silent error or crash on zero/negative input.
- Not documenting the source/assumption: Not writing down the source and assumption of the formula used.
- Not versioning: Leaving the account as a one-time file without making it traceable (git).
In summary
- Python makes engineering computation repeatable, traceable, and version controlled.
- AI is very fast at generating code; But the fact that the code works without errors does not mean that it calculates correctly.
- Every code should be validated through known-result testing, unit checking, and edge cases.
- Unit errors are the most frequent and most insidious source of error; Defend by single unit system, nomenclature or pint.
- Validated automation saves time and gives confidence; Unverified code is dangerous.
Application task
Choose a recurring engineering calculation (such as stress, pump power, heat load). Write a Python function to the AI that does this calculation; Comment out the unit of each input and output and add an assert test with a known result. Run the test and see if it passes. Then do two more verifications: try an edge case (zero or negative input) to check that the code returns a significant error, and manually provide the unit of the output in an example. If possible, also have a unit-safe version produced in pint. Finally, add the calculation's assumptions, formula source, and date to the code as a short title and write why this code still requires engineer approval.
checklist
- [ ] Input and output units are clearly documented in the code; single unit system was chosen.
- [ ] A test (assert) with a known result was added and passed.
- [ ] At least one edge case (zero/negative) was attempted; The code gave a significant error.
- [ ] The unit of output was provided by a manual example (not assuming "worked = correct").
- [ ] Formula source, assumptions and date noted in the code.
- [ ] Account kept trackable/tracked; final approval was left to the engineer.