CHE 120: Computer Literacy and Programming for Chemical Engineers

Estimated study time: 8 minutes

Table of contents

Sources and References

Primary texts — Downey, A., Think Python: How to Think Like a Computer Scientist, 3rd ed., O’Reilly, 2024 (open-access); Chapra, S.C., Applied Numerical Methods with Python for Engineers and Scientists, 5th ed., McGraw-Hill, 2022.

Supplementary texts — Kiusalaas, J., Numerical Methods in Engineering with Python 3, Cambridge, 2013; Hill, C., Learning Scientific Programming with Python, 2nd ed., Cambridge, 2020.

Online resources — MIT OCW 6.0001 “Introduction to Computer Science and Programming in Python”; Software Carpentry lessons (open-access); Python Software Foundation docs; NumPy, SciPy, and Matplotlib project documentation.


Chapter 1: The Computer as an Engineering Tool

1.1 Architecture in One Page

A modern computer pairs a central processing unit (CPU) that executes instructions with a hierarchy of memory: registers (tens of bytes, sub-nanosecond access), L1–L3 caches (kilobytes to megabytes), main memory (gigabytes, tens of nanoseconds), and secondary storage (solid-state drives, hundreds of gigabytes to terabytes, microseconds to milliseconds). Moving data up and down this hierarchy dominates real program runtimes far more than arithmetic operations.

An operating system manages processes, memory, filesystems, and I/O. Engineers meet the OS mostly through the filesystem, through process management, and through the network stack.

1.2 Networking Essentials

Networks are layered: physical (copper, fiber, radio), link (Ethernet, Wi-Fi), network (IP), transport (TCP, UDP), and application (HTTP, SSH, FTP). IP addresses (IPv4 32-bit, IPv6 128-bit) identify hosts; DNS resolves human-readable names. TCP guarantees ordered, error-checked delivery; UDP is lighter and datagram-oriented. Engineering practice meets networking in version control (git over SSH/HTTPS), remote computation (SSH to Linux clusters), and web APIs for data access.

1.3 Shells and Command-Line Interfaces

A Unix-style shell (bash, zsh) accepts commands, invokes programs, and composes them through pipes. Key primitives:

  • File and directory navigation: ls, cd, pwd, mkdir, rm.
  • Text streaming: cat, head, tail, grep, sed, awk.
  • Redirection and pipelines: >, >>, <, |, &&, ||.
  • Process control: ps, top, kill, &, jobs.
  • Scripting: shebang lines, variables, loops, conditionals, functions.

Command-line fluency pays dividends when a GUI would require hundreds of clicks. Reproducibility—the scientific hallmark—is easier to achieve with scripts than with mouse-driven workflows.

1.4 GUIs and Productivity Software

Word processors, spreadsheets, and presentation tools remain standard. Spreadsheets deserve particular respect in engineering: named ranges, absolute/relative references, structured tables, and auditable formulas distinguish professional practice from error-prone copy-paste worksheets. Solver and Goal Seek handle small optimization problems. Every engineer should internalize the hazards of floating-point arithmetic, the limits of iterative solvers, and the importance of input validation.

Chapter 2: Programming Fundamentals

2.1 Variables, Types, and Expressions

A variable names a memory location (in Python, a reference to an object). Core types include integers (unbounded in Python), floating-point numbers (IEEE 754 double precision, \( \sim 16 \) decimal digits), Booleans, and strings. An expression evaluates to a value; a statement may change program state.

Arithmetic operators in Python follow mathematical precedence with floor division //, true division /, and exponent **. Comparison operators return Booleans; logical operators and, or, not use short-circuit evaluation.

2.2 Control Flow

Programs execute sequentially unless directed otherwise by conditionals or loops.

if Re < 2100:
    regime = "laminar"
elif Re < 4000:
    regime = "transitional"
else:
    regime = "turbulent"

for loops iterate over iterables (ranges, lists, file lines); while loops repeat until a condition fails. Break and continue manage loop termination and skipping.

2.3 Functions

A function abstracts a computation:

def reynolds(rho, v, D, mu):
    return rho * v * D / mu

Functions enable decomposition, reuse, and testing. Document with docstrings; default parameter values and keyword arguments enhance flexibility. Recursion—a function calling itself—expresses problems such as tree traversal naturally but can exhaust the call stack if depth is unbounded.

2.4 Strings and I/O

Strings are immutable sequences of Unicode code points. Slicing, concatenation, formatting (f"{x:.3f}"), and methods (.split(), .strip(), .replace()) handle text manipulation. File I/O uses context managers:

with open("data.csv") as f:
    for line in f:
        parse(line)

CSV and JSON modules parse structured text; binary formats (HDF5, Parquet) handle large numerical datasets efficiently.

Chapter 3: Collections and Data Structures

3.1 Lists and Tuples

A list is an ordered, mutable sequence. Indexing (lst[0], lst[-1]), slicing (lst[1:4]), and append/extend/insert operations. A tuple is an immutable list, suitable for records.

Filtering flow rates. Extract only turbulent streams from a list.
streams = [(1, 2100), (2, 5000), (3, 12000)]
turbulent = [s for s in streams if s[1] > 4000]

List comprehensions combine filtering, mapping, and nesting into concise expressions.

3.2 Dictionaries and Sets

A dictionary maps keys to values with amortized \( O(1) \) lookup. A set is an unordered collection of unique elements; membership tests are \( O(1) \). Both use hashing, so keys must be immutable.

3.3 NumPy Arrays

numpy.ndarray represents a contiguous block of typed memory with shape and stride. Element-wise operations are vectorized:

import numpy as np
T = np.array([300, 350, 400])
Cp = 29.1 + 0.005 * T

Broadcasting extends shape-compatible operations without explicit loops. Performance is 10-100x faster than Python loops because the inner work runs in compiled C.

Chapter 4: Numerical Computing

4.1 Floating-Point Arithmetic

IEEE 754 double precision stores 52 mantissa bits, giving \( \epsilon_{machine} \approx 2.22 \times 10^{-16} \). Consequences:

  • 0.1 + 0.2 != 0.3 in finite precision.
  • Subtracting nearly equal numbers loses significance (catastrophic cancellation).
  • Very large or very small intermediate values can overflow or underflow.

Good practice: use relative tolerances (abs(a - b) < rtol * abs(b)), reformulate to avoid cancellation (the quadratic formula is the classic example), and trust numpy.allclose rather than ==.

4.2 Linear Algebra with NumPy

numpy.linalg.solve solves \( Ax = b \) by LU decomposition; numpy.linalg.lstsq handles overdetermined systems. Conditioning matters: the condition number \( \kappa(A) = \|A\|\|A^{-1}\| \) bounds error amplification; an ill-conditioned system requires care (regularization, higher precision, or a reformulation).

4.3 Root Finding and Nonlinear Equations

Bisection is robust but slow (linear convergence). Newton’s method

\[ x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} \]

converges quadratically near a simple root but can diverge. scipy.optimize.brentq combines robustness and speed for scalar problems; scipy.optimize.fsolve handles systems using Powell hybrid methods.

4.4 Numerical Integration and ODEs

scipy.integrate.quad integrates scalar functions adaptively. Ordinary differential equations \( dy/dt = f(t, y) \) with scipy.integrate.solve_ivp offer Runge-Kutta (RK45), implicit (BDF) methods for stiff systems, and error control via atol/rtol.

Chapter 5: Plotting and File I/O

5.1 Matplotlib Basics

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(T, Cp, label="heat capacity")
ax.set_xlabel("T [K]"); ax.set_ylabel("Cp [J/mol K]")
ax.legend(); fig.savefig("cp.png", dpi=300)

The object-oriented interface (figure, axes) is clearer than the state-based plt.plot(...) pattern. Log axes, dual axes, subplots, and annotations round out the plotting vocabulary.

5.2 Structured Data Files

CSV for tabular data; JSON for nested structures; HDF5 for large numerical arrays; NetCDF for self-describing scientific datasets. Pandas reads and writes most formats with one-line calls, and adds indexing, grouping, and merging operations.

5.3 Plotting Design

Axes labels with units, legible fonts, colorblind-friendly palettes (e.g., viridis), and minimal chartjunk separate professional plots from amateur ones. Every plot should stand alone: what is shown, for what conditions, with what axes.

Chapter 6: Engineering Problem Workflow

6.1 Decomposition and Debugging

Top-down design breaks a problem into subproblems; bottom-up testing verifies each piece. When a program fails, isolate: minimal failing input, print or log intermediate values, check types, compare against hand calculation on a toy case. Version control (git) records every change and enables rollback.

6.2 Reproducible Analysis

A reproducible project has: raw data preserved read-only; scripts that transform raw to processed; outputs that regenerate from scripts; a README documenting environment (Python version, package versions pinned in requirements.txt or environment.yml); and version-controlled history. Jupyter notebooks combine code, prose, and figures but require discipline to stay reproducible.

6.3 Professional Habits

Write code others (including your future self) can read: meaningful names, short functions, comments that explain why not what. Test on simple cases before solving real ones. Read error messages—they usually tell you what went wrong. Learn to search documentation and StackOverflow, but verify answers rather than paste blindly. Engineering software is engineering: correctness, maintainability, and clarity trump cleverness every time.

Back to top