Learn the Python calculations in notebook cells

Learn the Python calculations in notebook cells

I develop the Python calculations in explained cells, with checks and plots. Start at the notebook collection or download the python-notebook-lessons.zip collection. The notebooks under notebooks/ embed their code and do not need companion scripts. I retain the complete command-line programs below.

Run the notebook cells in order in Jupyter or Colab, using the listed dependencies. The native VPython viewers need local Jupyter. The 256³ generation and full-volume morphology are optional and need substantial memory. The default tests use small exact masks; they do not replace the original 256³ simulation. Each lesson states which cells were executed in the published check.

MS5033 worked examples — Julia and NumPy

These examples are based on my MS5033 teaching codes and notebooks. I wrote the original material to explain random walks, diffusion, phase separation and elastic effects. The website versions focus on one calculation at a time. All quantities are dimensionless. These are simplified educational models, not calibrated materials predictions.

Run

Python: install NumPy with python -m pip install numpy. Julia: install FFTW with julia -e 'using Pkg; Pkg.add("FFTW")' (not needed for walkers).

LessonPythonJulia
Random walkspython walkers.py walkers_resultsjulia walkers.jl walkers_results
Fourier diffusionpython diffusion.py diffusion_resultsjulia diffusion.jl diffusion_results
Chemical spinodalpython phase_field.py chemical chemical_resultsjulia phase_field.jl chemical chemical_results
Elastic spinodalpython phase_field.py elastic elastic_resultsjulia phase_field.jl elastic elastic_results

Each run writes CSV snapshots and diagnostics. Use different output directories for different runs. Scripts do not require plotting libraries. Website viewers display results generated from these NumPy scripts. Field CSV row index is x, column index is y; transpose for common plotting conventions.

My teaching sources and changes in these examples

  • walkers.*: based on my teaching code demo2_many_walkers_diffusion.jl. Updates every walker once per sweep on an unbounded lattice rather than asynchronous moves in a periodic box. MSD uses unwrapped coordinates. Julia and NumPy use different random-number generators; compare ensemble statistics, not identical trajectories.
  • diffusion.*: a worked example based on my teaching notebook Fourier_Julia.ipynb, with two Fourier modes and an exact analytic benchmark.
  • phase_field.*: based on the chemical and elastochemical sections of my teaching notebook PhaseField_Examples_Spectral.ipynb. Chemical free energy is c²(1−c)². FFT-based semi-implicit integration, constant mobility and gradient coefficient, periodic boundaries. Explicit nonlinear term; not unconditionally stable, not dealiased. No clipping of composition.
  • Elastic example: homogeneous 2D square-symmetric stiffness, linear dilatational composition eigenstrain, zero mean stress. Uses the acoustic-tensor contraction Q_ik=C_ijkl n_j n_l. Q11=C11 nx²+C44 ny² and Q22=C44 nx²+C11 ny². My original notebook used C12 in place of C44 in these diagonal terms; this version corrects them. This is not a full 3D plane-strain eigenstrain reduction.
  • The elastic default uses n=256, with conjugate-pair symmetrization of the discrete kernel for a real self-adjoint operator on even grids. To compare chemical and elastic evolution, use n=256 for BOTH runs and the same initial condition/time. Default chemical n=64 and elastic n=256 galleries are separate demonstrations. Increasing n at fixed grid spacing increases the domain; it is not fixed-domain refinement. Snapshots are saved every 25 steps.
  • Deterministic broadband perturbations enable cross-language checks, but math libraries can give small floating-point differences.

Validation performed

Both languages were executed (Julia 1.12.6, NumPy 2.3.5). Fourier diffusion maximum analytic error was 4.45e-16 or less. Original small-grid chemical/elastic diagnostic arrays agreed within 5e-13 across languages; the new 256-square elastic run also agrees within 1e-9. Mean composition drift was below 9e-14. Sampled energies decreased in both default examples; this does not establish unconditional stability or spatial convergence. The isotropic elastic kernel is direction independent to floating-point precision and equals 0.12 for C11=4, C12=2, C44=1, eigenstrain=0.2. Walker MSD at 400 steps: NumPy 399.31192, Julia 399.95048, theory 400.

Before quantitative use, assess time-step and spatial convergence at fixed physical domain, including FFT wavevector scaling; verify the elastic constitutive assumptions for the intended problem. The defaults are teaching demonstrations.

Editing parameters

Edit the source or import/include it and call the functions. Python: run('elastic', 'results', n=256, dt=0.1, steps=4000). Julia: run("elastic", "results"; n=256, dt=0.1, steps=4000). Both reach time 400. For chemical evolution call the same function with chemical. The elastic constants and eigenstrain are keyword defaults in kernel; change them there. Zero eigenstrain should recover the chemical evolution on the same grid.

Ink-drop defaults now use 100,000 walkers and 256-square density bins in [-128,128], with frames every 10 steps. The plotting window is not a physical boundary. MC examples are in monte_carlo.jl and monte_carlo.py: Ising Metropolis flips and the course Potts neighbor-copy heuristic. Julia movies: 256-square, 200 Ising sweeps and 2,000 Potts sweeps; NumPy smoke runs: 16-square, 5 sweeps. The latter do not validate long-run equilibrium. Potts starts with one distinct orientation label per pixel (a shuffled permutation of 1 through n²), with frames every 25 sweeps. Colors are fixed categorical labels, not crystallographic angles. Four-neighbor, equal-energy boundaries retain lattice anisotropy.

Using my code

See licence and dependency terms.

Portrait fonts

I use Pillow 10.1 or later for the portrait scripts: python -m pip install "Pillow>=10.1" numpy. I try Helvetica on macOS, then DejaVu Sans, then Pillow’s default font at the requested size. Font availability changes the labels, not the calculated fields or diagnostics.