Running from Python#

Note

Running from Python has been officially supported since 2025.05.

pyemsol requires a Python environment. Set up the Python environment yourself.

The installer differs by Python version.

Version 2025.05 supports Python 3.11. Create and use a compatible environment.

Use the dedicated pyemsol module to run EMSolution from Python.

Installation#

Prepare the installer (with the whl extension) and run the following command from a prompt where Python is available, such as Anaconda Prompt.

Installation Example at a Command Prompt#
C:\> pip install pyemsol-(version)-(environment)-(win/linux).whl

pyemsol is installed as a Python package.

Additional Dependencies#

pyemsol requires additional dependency packages. After installing the package, also install the following packages. In an Anaconda or conda environment, run the following command.

Installing Additional Packages with Conda#
C:\> conda install -c conda-forge pybind11 pybind11-abi
C:\> conda install -c conda-forge mkl mkl-include

Alternatively, install them with pip using the following command. Take care with dependencies on other packages.

Installing Additional Packages with pip#
C:\> pip install pybind11 mkl mkl-include

After installation, import and use pyemsol as shown below. Use pyemsol.version() to check that it was loaded correctly.

import pyemsol
print(pyemsol.version())

Running an Analysis#

There are two ways to run an analysis from Python using pyemsol.

  1. As with EMSolution, call pyemsol.run with input.json data as an argument.

  2. Initialize with pyemsol.initialize, call pyemsol.solve multiple times, and end with pyemsol.finalize.

1. Single Run#

Use pyemsol.run for a conventional single run.

pyemsol.run is approximately equivalent to running the Windows batch program EMSolBatch.exe from a script. Use it as follows.

Python code#
import pyemsol
# input.json のあるディレクトリ
current_directory = os.getcwd()
# or
# current_directory = "C:/EMSolution/pyemsol_test/"
# input.json のファイル名
input_json = "input.json"
with open(input_json, 'r') as file:
    json_data = json.load(file)

# 実行。リターン値として output.json が返される
output = pyemsol.run(json_data, current_directory)

2. Initialize and Run Multiple Times#

Initialize with pyemsol.initialize, then call pyemsol.solve multiple times. End with pyemsol.finalize.

Calling pyemsol.solve multiple times lets you modify calculation conditions between runs. For example, use it to run a motor analysis repeatedly while changing current amplitude or phase.

Use it as follows.

Python code#
import pyemsol
# input.json のあるディレクトリ
current_directory = os.getcwd()
# or
# current_directory = "C:/EMSolution/pyemsol_test/"
# input.json のファイル名
input_json = "input.json"
with open(input_json, 'r') as file:
    json_data = json.load(file)

# 初期化
pyemsol.initialize(json_data, current_directory)
# 計算条件の更新
for i in range(num_case):
    # 計算条件の更新
    # update Time_Function
    # json_data["18_Time_Function"] = new_time_function
    pyemsol.update_Time_Function(json_data)
    # 計算実行, 計算結果の取得
    output = pyemsol.solve()

# 計算終了
pyemsol.finalize()

There are two ways to change calculation conditions.

  • pyemsol.update_properties_in_design_domain: updates design-domain properties.

  • pyemsol.update_Time_Function: updates Time Function amplitude or phase.

For detailed usage, see Examples and Showcase.