Skip to main content

A package for Electromagnetic applications using MTFLibrary.

Project description

em-simulation-platform

Python CI PyPI version License: MIT Documentation Status

EM simulation tools for electromagnetic field analysis, visualization, and benchmarking.

Features

  • Modular solvers for EM field calculations (Python, C, and Optimized C++ backends)
  • Source modeling (dipoles, wires, loops/RingCoil, solenoids)
  • Advanced plotting and visualization
  • Demo scripts for validation and exploration
  • Benchmarking utilities
  • Extensible architecture for research and teaching

Installation

This project uses pyproject.toml to manage dependencies. For development, it is recommended to install the package in "editable" mode along with the development extras.

# Clone the repository
git clone https://github.com/shashi-manikonda/em-simulation-platform.git
cd em-simulation-platform

# (Recommended) Create a virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install the package in editable mode with all development dependencies
pip install -e .[dev,benchmark]

The [dev] extra includes dependencies for running tests and building the documentation. The [benchmark] extra includes dependencies for running the benchmark scripts.

Usage

Run all demos

python scripts/run_all_demos.py

Run a specific demo

To run a specific demo, you can execute the script directly. For notebooks, you can use a tool like jupytext to run it as a script:

jupytext --execute demos/em/01_validation_demo.ipynb

Run tests

pytest

Building the Documentation

This project uses Sphinx to generate API documentation from the source code. The necessary dependencies are included in the [dev] extra.

Build Script

A helper script is provided to simplify the build process. To build the documentation, run the following command from the project root:

./docs/build_docs.sh

The script will clean the previous build and generate the HTML documentation in the docs/_build/html directory.

Viewing the Documentation

To view the documentation, open the docs/_build/html/index.html file in your web browser.

Example: Calculate and Plot the Magnetic Field of a Ring Coil

This example demonstrates how to define a current source, calculate its magnetic field on a grid, and visualize the results.

import numpy as np
import matplotlib.pyplot as plt
from em_app.sources import RingCoil
from em_app.solvers import calculate_b_field, Backend
from mtflib import mtf

# Initialize the MTF library (Optional - defaults to Order 4, Dim 3 if omitted)
# mtf.initialize_mtf(max_order=1, max_dimension=4)

# --- 1. Setup the Coil Geometry ---
coil = RingCoil(
    current=1.0,
    radius=0.5,
    num_segments=20,
    center_point=np.array([0, 0, 0]),
    axis_direction=np.array([0, 0, 1]),
)

# --- 2. Define the Field Points for Calculation ---
grid_size = 1.0
num_points = 15
x_points = np.linspace(-grid_size, grid_size, num_points)
z_points = np.linspace(-grid_size, grid_size, num_points)
X, Z = np.meshgrid(x_points, z_points)
field_points = np.vstack([X.ravel(), np.zeros_like(X.ravel()), Z.ravel()]).T

# --- 3. Calculate the Magnetic Field ---
# --- 3. Calculate the Magnetic Field ---
# You can specify the backend explicitly using the Backend Enum
b_field = calculate_b_field(coil, field_points, backend=Backend.PYTHON)
b_vectors = np.array([b.to_numpy_array() for b in b_field._vectors_mtf])

# --- 4. Plot the Results ---
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection="3d")

# Plot the coil geometry
coil.plot(ax, color="b", wire_thickness=0.02)

# Plot the magnetic field vectors
ax.quiver(
    field_points[:, 0],
    field_points[:, 1],
    field_points[:, 2],
    b_vectors[:, 0],
    b_vectors[:, 1],
    b_vectors[:, 2],
    length=0.2,
    normalize=True,
    color="gray",
)

# --- 5. Customize and Show the Plot ---
ax.set_title("Magnetic Field of a Ring Coil")
ax.set_xlabel("X (m)")
ax.set_ylabel("Y (m)")
ax.set_zlabel("Z (m)")
ax.view_init(elev=20.0, azim=-60)
plt.show()

Live Demos

For more detailed examples, see the demo scripts in the demos/em directory. These scripts cover topics such as solver validation, dipole approximation, and advanced plotting.

You can run all demos at once using the following command:

python scripts/run_all_demos.py

This will generate output files and plots in the runoutput directory.

Project Structure

  • src/em_app/ - Core library modules
  • demos/em/ - Demo scripts
  • benchmarks/ - Performance and accuracy benchmarks
  • tests/ - Unit tests

License

MIT

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

em_app-0.2.7.tar.gz (31.3 kB view details)

Uploaded Source

Built Distribution

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

em_app-0.2.7-py3-none-any.whl (27.4 kB view details)

Uploaded Python 3

File details

Details for the file em_app-0.2.7.tar.gz.

File metadata

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

File hashes

Hashes for em_app-0.2.7.tar.gz
Algorithm Hash digest
SHA256 e95cad0703e40690374b86962d0f16e0cb26583944f7567971afe9b1d66b4a98
MD5 02cb1df56b6c0baae3e44b8926ed9840
BLAKE2b-256 de620619a53a9ff6f4a8ab7c77c926e5eddb31609ae92329961f776a12e2d932

See more details on using hashes here.

File details

Details for the file em_app-0.2.7-py3-none-any.whl.

File metadata

  • Download URL: em_app-0.2.7-py3-none-any.whl
  • Upload date:
  • Size: 27.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for em_app-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 2862a1f80225c11fb07afaa8a937b23c3f689970c67db2e329c216a39d5de1f8
MD5 219e972ee8e0e8aeeb36902021baa22a
BLAKE2b-256 546cf9b61f284f50c042d0b8892aabe3cff5b998745eb0691a0ae14f3d315417

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