Jiangsu Yawei Transformer Co., Ltd.

How to implement Compact Transformer in Python?

Nov 05, 2025Leave a message

Compact transformers have emerged as a revolutionary solution in the field of electrical power systems, offering high efficiency, reduced footprint, and excellent performance. As a leading Compact Transformer supplier, I am excited to share with you how to implement a Compact Transformer in Python. This guide will cover the theoretical background, the practical implementation steps, and some tips for optimizing your implementation.

Theoretical Background of Compact Transformers

Before diving into the implementation, it's essential to understand what Compact Transformers are. Compact transformers, such as the Compact Substation Transformer, are designed to provide a high - power density solution. They are commonly used in various applications, including industrial, commercial, and renewable energy sectors.

The core principle of a transformer is based on electromagnetic induction. A Compact Transformer typically consists of a primary winding, a secondary winding, and a magnetic core. When an alternating current (AC) flows through the primary winding, it creates a changing magnetic field in the core. This changing magnetic field then induces an electromotive force (EMF) in the secondary winding, resulting in the transfer of electrical energy from the primary to the secondary side.

Python Libraries for Implementing Compact Transformers

To implement a Compact Transformer in Python, we will rely on several key libraries:

  1. NumPy: A fundamental library for scientific computing in Python. It provides support for multi - dimensional arrays and a large collection of mathematical functions.
  2. SciPy: A library that builds on NumPy and offers additional functionality for scientific and technical computing, including signal processing, optimization, and integration.
  3. Matplotlib: A plotting library used to visualize the results of our simulations.

You can install these libraries using pip:

pip install numpy scipy matplotlib

Step - by - Step Implementation

Step 1: Define the Transformer Parameters

The first step is to define the parameters of the Compact Transformer. These parameters include the number of turns in the primary and secondary windings, the magnetic permeability of the core, the cross - sectional area of the core, and the frequency of the input voltage.

import numpy as np

# Transformer parameters
N1 = 100  # Number of turns in the primary winding
N2 = 50   # Number of turns in the secondary winding
mu = 1.25663706212e - 6  # Magnetic permeability of free space (core assumed to be air - core for simplicity)
A = 0.01  # Cross - sectional area of the core (m^2)
l = 0.1   # Mean length of the magnetic path (m)
f = 50    # Frequency of the input voltage (Hz)
V1 = 220  # Input voltage (V)

Step 2: Calculate the Inductance

The inductance of the primary and secondary windings can be calculated using the formula for the inductance of a solenoid:

[L=\frac{\mu N^{2}A}{l}]

New Energy Integrated Photovoltaic Prefabricated Cabin MV&HV Transformers Cutting-Edge Distribution EquipmentNew Energy Integrated Photovoltaic Prefabricated Cabin MV&HV Transformers Cutting-Edge Distribution Equipment

# Calculate inductance of primary and secondary windings
L1 = (mu * N1**2 * A) / l
L2 = (mu * N2**2 * A) / l

# Calculate the mutual inductance
M = (mu * N1 * N2 * A) / l

Step 3: Generate the Input Voltage Signal

We will generate a sinusoidal input voltage signal using NumPy.

import matplotlib.pyplot as plt

# Generate time vector
t = np.linspace(0, 0.1, 1000)

# Generate input voltage signal
v1 = V1 * np.sin(2 * np.pi * f * t)

Step 4: Calculate the Currents and Voltages in the Windings

We can use the equations for a transformer to calculate the currents and voltages in the primary and secondary windings.

# Calculate the impedance of the primary and secondary windings
omega = 2 * np.pi * f
Z1 = 1j * omega * L1
Z2 = 1j * omega * L2
Zm = 1j * omega * M

# Assume a load impedance on the secondary side
Z_load = 10 + 0j

# Calculate the secondary current
I2 = v1 / (Z2 + Z_load - (Zm**2 / Z1))

# Calculate the primary current
I1 = (v1 - Zm * I2) / Z1

# Calculate the secondary voltage
V2 = Z_load * I2

Step 5: Visualize the Results

We can use Matplotlib to visualize the input voltage, primary current, and secondary voltage.

# Plot the results
plt.figure(figsize=(12, 8))

plt.subplot(3, 1, 1)
plt.plot(t, v1, label='Input Voltage (V1)')
plt.title('Transformer Simulation')
plt.ylabel('Voltage (V)')
plt.legend()

plt.subplot(3, 1, 2)
plt.plot(t, np.real(I1), label='Primary Current (I1)')
plt.ylabel('Current (A)')
plt.legend()

plt.subplot(3, 1, 3)
plt.plot(t, np.real(V2), label='Secondary Voltage (V2)')
plt.xlabel('Time (s)')
plt.ylabel('Voltage (V)')
plt.legend()

plt.show()

Optimization and Advanced Considerations

The above implementation is a simplified model of a Compact Transformer. In a real - world scenario, there are several factors that need to be considered for optimization:

  1. Core Losses: The magnetic core of a transformer experiences hysteresis and eddy current losses. These losses can be modeled using more complex equations and incorporated into the simulation.
  2. Leakage Inductance: In practice, not all of the magnetic flux generated by the primary winding links with the secondary winding. This results in leakage inductance, which can affect the performance of the transformer.
  3. Non - linearity: The magnetic properties of the core material may exhibit non - linear behavior, especially at high magnetic fields. This non - linearity can be modeled using techniques such as the Preisach model.

Contact for Purchase and Further Information

If you are interested in our Compact Transformers or our New Energy Integrated Photovoltaic Prefabricated Cabin MV&HV Transformers Cutting - Edge Distribution Equipment, we welcome you to contact us for procurement discussions. Our team of experts is ready to assist you in selecting the right Compact Transformer for your specific needs. Whether you are in the industrial, commercial, or renewable energy sector, we have the solutions to meet your requirements.

References

  1. Chapman, S. J. (2012). Electric Machinery Fundamentals. McGraw - Hill.
  2. Hayt, W. H., & Kemmerly, J. E. (2001). Engineering Circuit Analysis. McGraw - Hill.