Skip to main content

Python wrapper for OpenMagnetics

Project description

PyOpenMagnetics - Python Wrapper for OpenMagnetics

Python License: MIT

🤖 For AI Assistants: If you're an AI assistant helping with this library, please read AGENTS.md FIRST before continuing with this README. It contains essential information about:

  • Correct import procedures (this package requires special handling)
  • ⚠️ Using API methods instead of manual calculations (CRITICAL)
  • Required field specifications for converter topologies
  • Troubleshooting common API errors
  • Complete working examples

⚠️ AVOID MANUAL MODE: Always use process_flyback(), calculate_advised_magnetics(), and other API methods rather than doing manual calculations. The MKF engine handles complex magnetic effects that manual calculations cannot.

PyOpenMagnetics is a Python wrapper for MKF (Magnetics Knowledge Foundation), the simulation engine of OpenMagnetics, providing a comprehensive toolkit for designing and analyzing magnetic components such as transformers and inductors.

Features

  • 🧲 Core Database: Access to extensive database of core shapes, materials, and manufacturers
  • 🔌 Winding Design: Automatic winding calculations with support for various wire types (round, litz, rectangular, planar)
  • 📊 Loss Calculations: Core losses (Steinmetz), winding losses (DC, skin effect, proximity effect)
  • 🎯 Design Adviser: Automated recommendations for optimal magnetic designs
  • 📈 Signal Processing: Harmonic analysis, waveform processing
  • 🖼️ Visualization: SVG plotting of cores, windings, magnetic fields
  • 🔧 SPICE Export: Export magnetic components as SPICE subcircuits

Installation

From PyPI (recommended)

pip install PyOpenMagnetics

From Source

git clone https://github.com/OpenMagnetics/PyOpenMagnetics.git
cd PyOpenMagnetics
pip install .

⚠️ Import Instructions

Important: The compiled extension module may require special import handling:

import importlib.util

# Option 1: Direct loading (recommended)
so_path = '/path/to/PyOpenMagnetics.cpython-311-x86_64-linux-gnu.so'
spec = importlib.util.spec_from_file_location('PyOpenMagnetics', so_path)
PyOpenMagnetics = importlib.util.module_from_spec(spec)
spec.loader.exec_module(PyOpenMagnetics)

# Option 2: Create __init__.py (see AGENTS.md for details)

# Verify installation
PyOpenMagnetics.load_databases({})
print(f"✓ Loaded {len(PyOpenMagnetics.get_core_materials())} materials")
print(f"✓ Loaded {len(PyOpenMagnetics.get_core_shapes())} shapes")

See AGENTS.md for complete import instructions and troubleshooting.

Quick Start

Basic Example: Creating a Core

import PyOpenMagnetics

# Find a core shape by name
shape = PyOpenMagnetics.find_core_shape_by_name("E 42/21/15")

# Find a core material by name
material = PyOpenMagnetics.find_core_material_by_name("3C95")

# Create a core with gapping
core_data = {
    "functionalDescription": {
        "shape": shape,
        "material": material,
        "gapping": [{"type": "subtractive", "length": 0.001}],  # 1mm gap
        "numberStacks": 1
    }
}

# Calculate complete core data
core = PyOpenMagnetics.calculate_core_data(core_data, False)
print(f"Effective area: {core['processedDescription']['effectiveParameters']['effectiveArea']} m²")

Design Adviser: Get Magnetic Recommendations

import PyOpenMagnetics

# Define design requirements
inputs = {
    "designRequirements": {
        "magnetizingInductance": {
            "minimum": 100e-6,  # 100 µH minimum
            "nominal": 110e-6   # 110 µH nominal
        },
        "turnsRatios": [{"nominal": 5.0}]  # 5:1 turns ratio
    },
    "operatingPoints": [
        {
            "name": "Nominal",
            "conditions": {"ambientTemperature": 25},
            "excitationsPerWinding": [
                {
                    "name": "Primary",
                    "frequency": 100000,  # 100 kHz
                    "current": {
                        "waveform": {
                            "data": [0, 1.0, 0],
                            "time": [0, 5e-6, 10e-6]
                        }
                    },
                    "voltage": {
                        "waveform": {
                            "data": [50, 50, -50, -50],
                            "time": [0, 5e-6, 5e-6, 10e-6]
                        }
                    }
                }
            ]
        }
    ]
}

# Process inputs (adds harmonics and validation)
processed_inputs = PyOpenMagnetics.process_inputs(inputs)

# Get magnetic recommendations
# core_mode: "available cores" (stock cores) or "standard cores" (all standard shapes)
result = PyOpenMagnetics.calculate_advised_magnetics(processed_inputs, 5, "standard cores")

# Result format: {"data": [{"mas": {...}, "scoring": float, "scoringPerFilter": {...}}, ...]}
for i, item in enumerate(result["data"]):
    mag = item["mas"]["magnetic"]
    core = mag["core"]["functionalDescription"]
    print(f"{i+1}. {core['shape']['name']} - {core['material']['name']} (score: {item['scoring']:.3f})")

Calculate Core Losses

import PyOpenMagnetics

# Define core and operating point
core_data = {...}  # Your core definition
operating_point = {
    "name": "Nominal",
    "conditions": {"ambientTemperature": 25},
    "excitationsPerWinding": [
        {
            "frequency": 100000,
            "magneticFluxDensity": {
                "processed": {
                    "peakToPeak": 0.2,  # 200 mT peak-to-peak
                    "offset": 0
                }
            }
        }
    ]
}

losses = PyOpenMagnetics.calculate_core_losses(core_data, operating_point, "IGSE")
print(f"Core losses: {losses['coreLosses']} W")

Winding a Coil

import PyOpenMagnetics

# Define coil requirements
coil_functional_description = [
    {
        "name": "Primary",
        "numberTurns": 50,
        "numberParallels": 1,
        "wire": "Round 0.5 - Grade 1"
    },
    {
        "name": "Secondary",
        "numberTurns": 10,
        "numberParallels": 3,
        "wire": "Round 1.0 - Grade 1"
    }
]

# Wind the coil on the core
result = PyOpenMagnetics.wind(core_data, coil_functional_description, bobbin_data, [1, 1], [])
print(f"Winding successful: {result.get('windingResult', 'unknown')}")

Flyback Converter Wizard

PyOpenMagnetics includes a complete flyback converter design wizard. See flyback.py for a full example:

from flyback import design_flyback, create_mas_inputs, get_advised_magnetics

# Define flyback specifications
specs = {
    "input_voltage_min": 90,
    "input_voltage_max": 375,
    "outputs": [{"voltage": 12, "current": 2, "diode_drop": 0.5}],
    "switching_frequency": 100000,
    "max_duty_cycle": 0.45,
    "efficiency": 0.85,
    "current_ripple_ratio": 0.4,
    "force_dcm": False,
    "safety_margin": 0.85,
    "ambient_temperature": 40,
    "max_drain_source_voltage": None,
}

# Calculate magnetic requirements
design = design_flyback(specs)
print(f"Required inductance: {design['min_inductance']*1e6:.1f} µH")
print(f"Turns ratio: {design['turns_ratios'][0]:.2f}")

# Create inputs for PyOpenMagnetics
inputs = create_mas_inputs(specs, design)

# Get recommended magnetics
magnetics = get_advised_magnetics(inputs, max_results=5)

API Reference

Database Access

Function Description
get_core_materials() Get all available core materials
get_core_shapes() Get all available core shapes
get_wires() Get all available wires
get_bobbins() Get all available bobbins
find_core_material_by_name(name) Find core material by name
find_core_shape_by_name(name) Find core shape by name
find_wire_by_name(name) Find wire by name

Core Calculations

Function Description
calculate_core_data(core, process) Calculate complete core data
calculate_core_gapping(core, gapping) Calculate gapping configuration
calculate_inductance_from_number_turns_and_gapping(...) Calculate inductance
calculate_core_losses(core, operating_point, model) Calculate core losses

Winding Functions

Function Description
wind(core, coil, bobbin, pattern, layers) Wind coils on a core
calculate_winding_losses(...) Calculate total winding losses
calculate_ohmic_losses(...) Calculate DC losses
calculate_skin_effect_losses(...) Calculate skin effect losses
calculate_proximity_effect_losses(...) Calculate proximity effect losses

Design Adviser

Function Description
calculate_advised_cores(inputs, max_results) Get recommended cores
calculate_advised_magnetics(inputs, max, mode) Get complete designs
process_inputs(inputs) Process and validate inputs

Visualization

Function Description
plot_core(core, ...) Generate SVG of core
plot_sections(magnetic, ...) Plot winding sections
plot_layers(magnetic, ...) Plot winding layers
plot_turns(magnetic, ...) Plot individual turns
plot_field(magnetic, ...) Plot magnetic field

Settings

Function Description
get_settings() Get current settings
set_settings(settings) Configure settings
reset_settings() Reset to defaults

SPICE Export

Function Description
export_magnetic_as_subcircuit(magnetic, ...) Export as SPICE model

Core Materials

PyOpenMagnetics includes materials from major manufacturers:

  • TDK/EPCOS: N27, N49, N87, N95, N97, etc.
  • Ferroxcube: 3C90, 3C94, 3C95, 3F3, 3F4, etc.
  • Fair-Rite: Various ferrite materials
  • Magnetics Inc.: Powder cores (MPP, High Flux, Kool Mu)
  • Micrometals: Iron powder cores

Core Shapes

Supported shape families include:

  • E cores: E, EI, EFD, EQ, ER
  • ETD/EC cores: ETD, EC
  • PQ/PM cores: PQ, PM
  • RM cores: RM, RM/ILP
  • Toroidal: Various sizes
  • Pot cores: P, PT
  • U/UI cores: U, UI, UR
  • Planar: E-LP, EQ-LP, etc.

Wire Types

  • Round enamelled wire: Various AWG and IEC sizes
  • Litz wire: Multiple strand configurations
  • Rectangular wire: For high-current applications
  • Foil: For planar magnetics
  • Planar PCB: For integrated designs

Configuration

Use set_settings() to configure:

settings = PyOpenMagnetics.get_settings()
settings["coilAllowMarginTape"] = True
settings["coilWindEvenIfNotFit"] = False
settings["painterNumberPointsX"] = 50
PyOpenMagnetics.set_settings(settings)

Contributing

Contributions are welcome! Please see the OpenMagnetics organization for contribution guidelines.

Documentation

Quick Start

  • llms.txt - Comprehensive API reference optimized for AI assistants and quick lookup
  • examples/ - Practical example scripts for common design workflows
  • PyOpenMagnetics.pyi - Type stubs for IDE autocompletion

Tutorials

Reference

Validation

License

This project is licensed under the MIT License - see the LICENSE file for details.

Related Projects

References

  • Maniktala, S. "Switching Power Supplies A-Z", 2nd Edition
  • Basso, C. "Switch-Mode Power Supplies", 2nd Edition
  • McLyman, C. "Transformer and Inductor Design Handbook"

Support

For questions and support:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyopenmagnetics-1.3.6.tar.gz (643.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyopenmagnetics-1.3.6-cp313-cp313-win_amd64.whl (53.1 MB view details)

Uploaded CPython 3.13Windows x86-64

pyopenmagnetics-1.3.6-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (6.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

pyopenmagnetics-1.3.6-cp313-cp313-macosx_15_0_arm64.whl (52.6 MB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

pyopenmagnetics-1.3.6-cp312-cp312-win_amd64.whl (26.6 MB view details)

Uploaded CPython 3.12Windows x86-64

pyopenmagnetics-1.3.6-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (6.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

pyopenmagnetics-1.3.6-cp312-cp312-macosx_15_0_arm64.whl (26.4 MB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

pyopenmagnetics-1.3.6-cp311-cp311-win_amd64.whl (13.4 MB view details)

Uploaded CPython 3.11Windows x86-64

pyopenmagnetics-1.3.6-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (6.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

pyopenmagnetics-1.3.6-cp311-cp311-macosx_15_0_arm64.whl (13.2 MB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

pyopenmagnetics-1.3.6-cp310-cp310-win_amd64.whl (6.7 MB view details)

Uploaded CPython 3.10Windows x86-64

pyopenmagnetics-1.3.6-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (6.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

pyopenmagnetics-1.3.6-cp310-cp310-macosx_15_0_arm64.whl (6.7 MB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

Details for the file pyopenmagnetics-1.3.6.tar.gz.

File metadata

  • Download URL: pyopenmagnetics-1.3.6.tar.gz
  • Upload date:
  • Size: 643.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyopenmagnetics-1.3.6.tar.gz
Algorithm Hash digest
SHA256 8d920ea711cfc158c649eeaf01d337176eb485951315dcf99f486f79f3509366
MD5 55e5e04b30e07d99a8b80d6fada2de22
BLAKE2b-256 6bc59f6a5b1c5f3479fdf526edddc0c72283c30f9435246c54f9168587def791

See more details on using hashes here.

File details

Details for the file pyopenmagnetics-1.3.6-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for pyopenmagnetics-1.3.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f83c9c9065bac7a86bd238485e8bd1e52167d33d1f3c588dba0726ab7d37ae15
MD5 e6ce8999a21c7227dd72fb63aa370fe8
BLAKE2b-256 537f7d85cbf79c76daf2be8cee4d0fe7a8099b05e5a96923e34a117293487c70

See more details on using hashes here.

File details

Details for the file pyopenmagnetics-1.3.6-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyopenmagnetics-1.3.6-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 484837f2aca5ca65a39aa549eb26f5597d335c0e597a2b9dd1b83aacfe75e08e
MD5 22beb836a027b78dad234ae4ebe336a7
BLAKE2b-256 deb73e16ae8af95ed12fbbc3fb9d52be01565182fe052f8c1ad3d0883d2ee223

See more details on using hashes here.

File details

Details for the file pyopenmagnetics-1.3.6-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pyopenmagnetics-1.3.6-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 549c14293fba8497b519b56a15921d298d440134ad0fa4b04c5bb9dfccf57b1f
MD5 3ccb82071821fffee3a0e8ec770bac92
BLAKE2b-256 9e4afaa7752c05b1150f9c873d426942d50af729d995c4ecd4fc3525ad8f074f

See more details on using hashes here.

File details

Details for the file pyopenmagnetics-1.3.6-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for pyopenmagnetics-1.3.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 64f8d48951da7774869183499aa8e1b65508b5b651e44006479fb6a6dc6690a7
MD5 08de9cb391f0aaffc51f81b0a3476c75
BLAKE2b-256 f1649f054e5ddac81670e8d09656bbb4bbfc563df5bb9de54dff2f9c1c32fd7e

See more details on using hashes here.

File details

Details for the file pyopenmagnetics-1.3.6-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyopenmagnetics-1.3.6-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 85897e1dad05cc1fbc3e8b4416e114340c260d084b1050d5e3db511c03e18f7a
MD5 abc18a93179720aca659f9b6ab822d41
BLAKE2b-256 1d3ae1ed97074fa9a7aa1fd2b45acfa10a575ab9353c4f1e60f68a23a913b445

See more details on using hashes here.

File details

Details for the file pyopenmagnetics-1.3.6-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pyopenmagnetics-1.3.6-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 d9b2e00fe25518339572b1ecd700664cf6a3b59530dd3555e340326217de27e9
MD5 bdf8d8da28328c91729b597989cd082d
BLAKE2b-256 4c2ef8afdc3e7c3776e48c50ea7d4f3214f11fc898322b24e0789f655e0e86a6

See more details on using hashes here.

File details

Details for the file pyopenmagnetics-1.3.6-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for pyopenmagnetics-1.3.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b85c4759c67a08061f09d6ae866b94c9c021da18c34bc32dc48533b6601e8dd7
MD5 196a3d5c6de72a682f17bf0b2b2d26c5
BLAKE2b-256 44199dc1ac97841bab97a386b5e3153745b3c218d062745c2b63b4f0b2179c69

See more details on using hashes here.

File details

Details for the file pyopenmagnetics-1.3.6-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyopenmagnetics-1.3.6-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 50129cb9244d028be68d975ec2b38a5602ef8d61af317d10b92a0d70e02423ce
MD5 8a24d0be461f81c8f78d58378ad2c47d
BLAKE2b-256 004be717d5443ecd70a07ff3ac211b0e4dfd8353428f0c6bb5ccbf5c3a0dc9fb

See more details on using hashes here.

File details

Details for the file pyopenmagnetics-1.3.6-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pyopenmagnetics-1.3.6-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 d6cdc2ef3a03fb7f78aefe3da5092ae05b16240c48da23b6dff786be4138e2b5
MD5 a230c4d5210c1b08e8ad658d6c5998ca
BLAKE2b-256 506e5fb95958eae54a55b7ab893111a160de82a4376e325aeb402bb43e498ab3

See more details on using hashes here.

File details

Details for the file pyopenmagnetics-1.3.6-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for pyopenmagnetics-1.3.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5f80603f9ca79e65979eaf0275b21685336218f37cd657e2d42facc246bf828e
MD5 0a6a35f12be0332b531783ffb66d6126
BLAKE2b-256 db193444a122c6cad277fe594cadf659c865d6abfd31903fb70b52085fbfa792

See more details on using hashes here.

File details

Details for the file pyopenmagnetics-1.3.6-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyopenmagnetics-1.3.6-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2e27b45d313bfe2bed71082acba1c0a944872dff3ce21f145d33f42bf1b6b85b
MD5 8942f359ba09895937828b4b73295009
BLAKE2b-256 1c54f4d9e03462262653408aed2fa590d164ba161aeb0ee62da70200c7611690

See more details on using hashes here.

File details

Details for the file pyopenmagnetics-1.3.6-cp310-cp310-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pyopenmagnetics-1.3.6-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 c13fc0ff36a2b396cbbffd95407ced9fd43f55429b9f85eaebba35a1deb2e98a
MD5 a7463e283bcde3d31169a2decd06a0cf
BLAKE2b-256 0773a0101e2df72784b91fccdcd8de009e7693ba414a7e314abcf267f62ea481

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page