Skip to main content

MagnetiCalc calculates the magnetic flux density, vector potential, energy, self-inductance and magnetic dipole moment of arbitrary coils. Inside a VisPy / OpenGL-accelerated PyQt5 GUI, the static magnetic flux density (B-field) or the magnetic vector potential (A-field) is displayed in interactive 3D, using multiple metrics for highlighting this field's properties.

Project description

MagnetiCalc

License: ISC Donate API Documentation PyPI version

What does MagnetiCalc do?

MagnetiCalc calculates the static magnetic flux density, vector potential, energy, self-inductance and magnetic dipole moment of arbitrary coils. Inside a VisPy / OpenGL-accelerated PyQt5 GUI, the magnetic flux density (B-field, in units of Tesla) or the magnetic vector potential (A-field, in units of Tesla-meter) is displayed in interactive 3D, using multiple metrics for highlighting the field properties.

Experimental feature: To calculate the energy and self-inductance of permeable (i.e. ferrous) materials, different core media can be modeled as regions of variable relative permeability; however, core saturation is currently not modeled, leading to excessive flux density values.

Who needs MagnetiCalc?

MagnetiCalc does its job for hobbyists, students, engineers and researchers of magnetic phenomena. I designed MagnetiCalc from scratch, because I didn't want to mess around with expensive and/or overly complex simulation software whenever I needed to solve a magnetostatic problem.

How does it work?

The B-field calculation is implemented using the Biot-Savart law [1], employing multiprocessing techniques; MagnetiCalc uses just-in-time compilation (JIT) and, if available, GPU-acceleration (CUDA) to achieve high-performance calculations. Additionally, the use of easily constrainable "sampling volumes" allows for selective calculation over grids of arbitrary shape and arbitrary relative permeabilities µ_r(x) (experimental).

The shape of any wire is modeled as a 3D piecewise linear curve. Arbitrary loops of wire are sliced into differential current elements (l), each of which contributes to the total resulting field (A, B) at some fixed 3D grid point (x), summing over the positions of all current elements (x'):



At each grid point, the field magnitude (or field angle in some plane) is displayed using colored arrows and/or dots; field color and alpha transparency are individually mapped using one of the various available metrics.

The coil's energy E [2] and self-inductance L [3] are calculated by summing the squared B-field over the entire sampling volume; ensure that the sampling volume encloses a large, non-singular portion of the field:



Additionally, the scalar magnetic dipole moment m [4] is calculated by summing over all current elements:


References

[1]: Jackson, Klassische Elektrodynamik, 5. Auflage, S. 204, (5.4).
[2]: Kraus, Electromagnetics, 4th Edition, p. 269, 6-9-1.
[3]: Jackson, Klassische Elektrodynamik, 5. Auflage, S. 252, (5.157).
[4]: Jackson, Klassische Elektrodynamik, 5. Auflage, S. 216, (5.54).

Screenshot

Screenshot

(Screenshot taken from the latest GitHub release.)

Installation

If you have trouble installing MagnetiCalc, make sure to file an issue so I can help you get it up and running!

Tested with:

  • Python 3.8 in Ubuntu 20.04
  • Python 3.7 in Linux Mint 19.3
  • Python 3.8 in Windows 10

Prerequisites

Linux

The following dependency packages must be installed first (Ubuntu 20.04):

sudo apt install python3-dev
sudo apt install libxcb-xinerama0 --reinstall

Windows

On some systems, it may be necessary to install Visual C++ Redistributable Packages for Visual Studio 2017 first.

All platforms

On some systems, it may be necessary to upgrade pip first:

python3 -m pip install pip --upgrade

Option A: Automatic install via pip

Linux

Install (or upgrade) MagnetiCalc to the user site-packages directory and start it from there:

python3 -m pip install magneticalc --upgrade
python3 -m magneticalc

This will automatically install all dependency packages.

Windows

Install (or upgrade) MagnetiCalc to the user site-packages directory and start it from there:

python -m pip install --upgrade scipy
python -m install --upgrade magneticaclc
python -m magneticalc

This will automatically install SciPy and all dependency packages.

Note: Although installation would succeed without installing SciPy first, MagnetiCalc would crash upon calling the np.dot function due to missing dependencies.

Note: Installation will fail for Python >= 3.9 because there is no official wheel for llvmlite available currently (it might be possible to find an unofficial wheel, but this is not recommended).

Juptyer Notebook

From within a Jupyter Notebook, MagnetiCalc must be installed and run like this (Ubuntu 20.04):

import sys
!{sys.executable} -m pip install magneticalc --upgrade
!{sys.executable} -m magneticalc

Option B: Manual download

First, manually install all dependency packages, upgrading each to the latest version (Ubuntu 20.04):

python3 -m pip install numpy numba PyQt5 vispy qtawesome colorit si-prefix h5py --upgrade

Clone the latest version of MagnetiCalc from GitHub and start it directly:

git clone https://github.com/shredEngineer/MagnetiCalc
cd MagnetiCalc
python3 -m magneticalc

For debugging, you may now also install (uninstall) the package in a virtual environment:

python3 -m pip install .
…
python3 -m pip uninstall magneticalc -y

Enabling CUDA Support

Tested in Ubuntu 20.04, using the NVIDIA CUDA 10.1 driver and NVIDIA GeForce GTX 1650 GPU.

Please refer to the Numba Installation Guide which includes the steps necessary to get CUDA up and running.

Data Import/Export and Python API 🆕

GUI

MagnetiCalc allows the following data to be imported/exported using the GUI:

  • Import/export wire points from/to TXT file.
  • Export A- / B-fields, wire points and wire current to an HDF5 container for use in post-processing.

API

The API.py class provides basic functions for importing/exporting data programmatically: API class documentation.

  • Generate a wire shape using NumPy and export it to a TXT file:

    from magneticalc import API
    import numpy as np
    
    wire = [
        (np.cos(a), np.sin(a), np.sin(16 * a))
        for a in np.linspace(0, 2 * np.pi, 200)
    ]
    
    API.export_wire("MyWire.txt", wire)
    
  • Import an HDF5 file containing an A-field (which needs to be generated using the GUI first) and plot it using Matplotlib:

    from magneticalc import API
    import matplotlib.pyplot as plt
    
    data = API.import_hdf5("MagnetiCalc_Export_A.hdf5")
    fields = data["fields"]
    x, y, z = fields["x"], fields["y"], fields["z"]
    A_x, A_y, A_z = fields["A_x"], fields["A_y"], fields["A_z"]
    
    ax = plt.figure(figsize=(10, 10), dpi=150).add_subplot(projection="3d")
    ax.quiver(x, y, z, A_x, A_y, A_z, length=5e5, normalize=False, linewidth=2)
    plt.show()
    

    Data is always exported as 1D raveled arrays, which is the native format of MagnetiCalc. If required, a list of 3D points (tuples) can be obtained like this:

    points = list(zip(*[data["fields"][i] for i in ["x", "y", "z"]]))
    

    However, for visualising a slice of the magnitude of a field or a field component in a plane, and for integrating over the axes, it may be preferable to have minimal 1D representations of the axes and the field components arranged in 3D arrays, with Axis0x, Axis1y, and Axis2z. The reshaped data can be obtained like this:

    data_reshape = API.reshape_fields(data)
    

License

Copyright © 2020–2021, Paul Wilhelm, M. Sc. <anfrage@paulwilhelm.de>

ISC License

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Contribute

You are invited to contribute to MagnetiCalc in any way you like! :)

If this software has been helpful to you in some way or another, please let me and others know!

Donate

ToDo

General

  • Ensure consistent PyQt5 look and feel in Windows and Linux.
  • Move from INI format to HDF5 format for storing project data; make auto-generated MagnetiCalc.ini a global settings file instead. (Retain option to import old MagnetiCalc.ini files.)
  • Add a global settings dialog for some selection of options currently hard-coded in various classes.
  • Add "Overwrite Existing File?" dialogs for "Save As" and "Export" actions.

Functional

  • Add an overlay for vector metrics, like gradient or curvature (derived from the fundamental A- and B-fields).
  • Add a list of objects, for wires and permeability classes (constraints), with a transformation pipeline for each object; move the Wire widget to a dedicated dialog window instead. (Add support for multiple wires, study mutual induction.)
  • Highlight permeability classes with in the 3D view.
  • Add support for multiple current values and animate the resulting fields.
  • Add support for modeling of core material saturation and hysteresis effects (Landau–Lifshitz–Gilbert equation).
  • Provide a means to emulate permanent magnets.

API

  • Increase the level of abstraction and add more functionality.

Usability

  • Add more example projects to examples/.
  • Move variations of each wire preset (e.g. the number of turns) into an individual sub-menu; alternatively, provide a dialog for parametric generation.
  • Add stationary coordinate system and ruler in the bottom left corner.
  • Add support for selective display over a portion of the metric range, enabling a kind of iso-contour display.

Known Bugs

  • Fix issue where the points of a sampling volume with fractional resolution are not always spaced equidistantly for some sampling volume dimensions.
  • Fix calculation of divergence right at the sampling volume boundary.
  • Fix delayed GUI start-up when loading "complex" files.
  • Fix missing scaling of VisPy markers when zooming.
  • Fix unnecessary shading of VisPy markers.

Code Quality

  • Add debug output where it is missing.
  • Add type hints where they are missing.
  • Use my QtWidgets2 wrapper everywhere.
  • Use the @property decorator for accessing data where applicable.
  • Merge sparse *_Types.py modules with higher-level classes if possible.

Design

  • Replace plain QMessageBox dialogs with nice-looking custom dialogs where possible.

Video

A very short demo of MagnetiCalc in action:

Magnetic Field Calculation with Python (MagnetiCalc)

Links

If you want to comment on the project or see additional info, please visit my personal website: https://paulwilhelm.de/magneticalc/

Appendix: Metrics

Metric Symbol Description
Magnitude Magnitude in space
Magnitude X Magnitude in X-direction
Magnitude Y Magnitude in Y-direction
Magnitude Z Magnitude in Z-direction
Magnitude XY Magnitude in XY-plane
Magnitude XZ Magnitude in XZ-plane
Magnitude YZ Magnitude in YZ-plane
Divergence Divergence
Divergence + Positive Divergence
Divergence – Negative Divergence
Log Magnitude Logarithmic Magnitude in space
Log Magnitude X Logarithmic Magnitude in X-direction
Log Magnitude Y Logarithmic Magnitude in Y-direction
Log Magnitude Z Logarithmic Magnitude in Z-direction
Log Magnitude XY Logarithmic Magnitude in XY-plane
Log Magnitude XZ Logarithmic Magnitude in XZ-plane
Log Magnitude YZ Logarithmic Magnitude in YZ-plane
Log Divergence Logarithmic Divergence
Log Divergence + Positive Logarithmic Divergence
Log Divergence – Negative Logarithmic Divergence
Angle XY Field angle in XY-plane
Angle XZ Field angle in XZ-plane
Angle YZ Field angle in YZ-plane

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

MagnetiCalc-1.11.1.tar.gz (77.3 kB view hashes)

Uploaded Source

Built Distribution

MagnetiCalc-1.11.1-py3-none-any.whl (120.9 kB view hashes)

Uploaded Python 3

Supported by

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