Python automation platform for FEMM finite element simulations.
py2femm generates Lua scripts from a Python API and executes them in FEMM via a REST server. It supports magnetics, electrostatics, heat flow, and current flow problems in 2D planar and axisymmetric geometries.
pyFEMM uses ActiveX and only works on Windows. py2femm takes a different approach:
- File-based Lua generation -- works with Wine on Linux, in Docker, or natively on Windows
- Geometry separated from physics -- describe a shape once, reuse it for magnetic, thermal, or electrostatic analysis
- REST server -- submit jobs from any client (Python, notebook, CI) to a Windows machine running FEMM
- Parametric workflows -- sweep dimensions, materials, or BCs from Python and collect results programmatically
┌──────────────────┐ HTTP/REST ┌──────────────────┐
│ Python client │ ──────────────────► │ py2femm_server │
│ (any platform) │ ◄────────────────── │ (Windows+FEMM) │
│ │ JSON results │ │
│ FemmProblem API │ │ FastAPI + FEMM │
│ FemmClient │ │ subprocess │
└──────────────────┘ └──────────────────┘
The client library (py2femm/) runs anywhere: it builds a FemmProblem, serializes it to a Lua script, and hands it to FemmClient. The server (py2femm_server/) runs on Windows where FEMM is installed. It receives Lua scripts over HTTP, launches FEMM as a subprocess, and returns CSV results.
FemmClient auto-detects the best transport: shared filesystem (WSL/local), remote REST, or environment variable override.
- Python >= 3.10
- FEMM 4.2 installed on Windows (default:
C:\femm42\)
# Core library (script generation + client)
pip install -e .
# With REST server dependencies (FastAPI, uvicorn)
pip install -e ".[server]"
# Everything including dev tools
pip install -e ".[server,dev]"Or run the interactive setup on Windows:
setup_femm.batstart_femm_server.batOr manually:
python -m py2femm_server --host 0.0.0.0 --port 8082To keep FEMM visible for debugging:
python -m py2femm_server --host 0.0.0.0 --port 8082 --show-femmWith the server running:
python examples/heatflow/heatsink/heatsink.pyfrom py2femm.femm_problem import FemmProblem
from py2femm.general import LengthUnit
from py2femm.geometry import Geometry, Node, Line
from py2femm.heatflow import HeatFlowMaterial, HeatFlowConvection
problem = FemmProblem(out_file="results.csv")
problem.heat_problem(units=LengthUnit.MILLIMETERS, type="planar",
precision=1e-8, depth=100, minangle=30)
# Add geometry, materials, BCs...
# Then write the Lua script:
problem.write("my_simulation.lua")from py2femm.client import FemmClient
client = FemmClient(mode="remote", url="http://localhost:8082")
result = client.run(lua_script, timeout=120)
print(result.csv_data)# Run a single Lua script
py2femm run my_simulation.lua --output results.csv
# Run all .lua files in a directory
py2femm run-batch simulations/ --output-dir results/
# Check server status
py2femm statusThe examples/ directory contains worked examples across all four physics domains.
| Example | Description |
|---|---|
heatflow/heatsink/heatsink_baseline.ipynb |
FEMM Tutorial #7 reproduction -- 5-fin aluminum heat sink with fixed BCs and FEMM bitmap capture |
heatflow/heatsink/heatsink_parametric.ipynb |
360-config factorial sweep using square-wave fin parametrization (10 base widths x 3 pitch ratios x 3 duty cycles x 4 base ratios), sensitivity analysis, 2D heatmaps, scaling plots, and contact mode comparison |
01_simple_thermal.py |
Minimal heat flow script |
| Example | Description |
|---|---|
02_e2e_capacitor_with_plot.py |
End-to-end capacitor simulation with result plotting |
electrostatics/capacitance/ |
Planar capacitor analysis |
electrostatics/double_l_shape/ |
Double L-shaped domain |
| Example | Description |
|---|---|
magnetics/solenoid/ |
Solenoid field analysis |
magnetics/PMDC_motor/ |
Permanent magnet DC motor |
magnetics/ISPMSM/ |
Interior surface-mount PMSM -- torque, cogging, NSGA-II optimization |
magnetics/FI-PMASynRM/ |
Flux-intensifying PM-assisted SynRM |
magnetics/reluctance_machine/ |
Switched reluctance machine studies |
magnetics/toyota_prius/ |
Toyota Prius motor model |
The heat sink example is split into two notebooks:
Baseline (heatsink_baseline.ipynb) reproduces FEMM Tutorial #7:
- Build a 5-fin aluminum heat sink geometry and plot it
- Define material (aluminum k=200) and boundary conditions (heat flux + convection)
- Generate the Lua script and submit it to FEMM
- Parse results and display FEMM temperature contour bitmap
- Summary: T_avg, thermal resistance R_th
Parametric study (heatsink_parametric.ipynb) explores the design space:
- Define the parameter grid (360 full-factorial combinations, ~176 valid after manufacturability filtering)
- Run the sweep via FEMM (~10 min runtime)
- 1D sensitivity plots (R_th and cross-section area vs each parameter)
- 2D heatmaps of R_th over pitch ratio and duty cycle
- Scaling analysis: best R_th per base width
- Contact mode comparison (centered, single-fin aligned, between-fins)
- Top configurations with geometry overlay
| Field | Problem Types | Prefix | Key Outputs |
|---|---|---|---|
| Magnetics | Magnetostatic, time-harmonic | mi_/mo_ |
Flux density, force, torque, inductance |
| Electrostatics | Static electric fields | ei_/eo_ |
Voltage, energy, capacitance |
| Heat Flow | Steady-state thermal | hi_/ho_ |
Temperature, heat flux, thermal resistance |
| Current Flow | DC/AC conduction | ci_/co_ |
Current density, resistance, power loss |
py2femm/
├── py2femm/ # Core library
│ ├── femm_problem.py # FemmProblem — Lua script generator
│ ├── geometry.py # Node, Line, CircleArc, Geometry
│ ├── magnetics.py # Magnetic materials & BCs
│ ├── electrostatics.py # Electrostatic materials & BCs
│ ├── heatflow.py # Heat flow materials & BCs
│ ├── current_flow.py # Current flow materials & BCs
│ ├── general.py # FemmFields, LengthUnit, base classes
│ ├── cli.py # CLI (run, status, run-batch)
│ └── client/ # FemmClient (auto, local, remote)
│
├── py2femm_server/ # REST server (Windows)
│ ├── server.py # FastAPI endpoints
│ ├── executor.py # FEMM subprocess runner
│ ├── job_store.py # In-memory job state
│ ├── health.py # FEMM installation detection
│ └── watcher.py # Filesystem watcher mode
│
├── examples/
│ ├── heatflow/heatsink/ # Heat sink tutorials + parametric study
│ ├── electrostatics/ # Capacitor and field examples
│ └── magnetics/ # Motor, solenoid, and machine examples
│
├── docs/superpowers/ # Design specs and implementation plans
├── tests/ # Test suite
├── start_femm_server.bat # Launch server on Windows
└── setup_femm.bat # One-time environment setup
py2femm reads configuration from config/default.yml (generated by setup_femm.bat):
python:
env_type: conda # or venv
env_name: my_env
conda_root: C:\ProgramData\Anaconda3
femm:
exe: C:\femm42\bin\femm.exeClient auto-detection order:
- Explicit
mode/urlarguments /mnt/c/exists -- local shared-filesystem mode (WSL)PYFEMM_AGENT_URLenvironment variable -- remote mode~/.py2femm/config.yml-- remote mode
Design documents and implementation plans live in docs/superpowers/. To browse the documentation site locally:
pip install mkdocs-material
mkdocs serveThen open http://localhost:8000 in your browser.
python -m pytest tests/ -vTests that require a live FEMM server are marked with pytest.mark.skip or guarded by server health checks. The unit tests (geometry, config, client mocking) run without FEMM.
Contributions are welcome. Please:
- Install dev dependencies:
pip install -e ".[server,dev]" - Run
ruff check .andruff format --check .before submitting - Add tests for new features
- Follow existing code style: type hints, dataclasses,
from __future__ import annotations
- FEMM by David Meeker -- the finite element engine that makes this all possible
- pyFEMM -- the original Python-FEMM bridge via ActiveX, which inspired the py2femm approach
- The FEMM community and tutorial authors
Copyright (c) Riccardo Tinivella (tinix84@gmail.com)