A user subroutine is a program created by users according to a specified format.
By calling it during analysis, it is possible to customize the behavior of JMAG.
Furthermore, values for variables defined in the user subroutine can be set from the study properties and perform parametric studies on these variables.
This script sets the electric potential of electric potential source to be applied by user subroutine and sets user variable values.
Preconditions
- One or more magnetic field analysis study has been created.
- The user subroutine has been placed in its default location and its name is known.
Script Function
- Set the DLL for the user subroutine to be used to "default".
- Set the user variable names and values for the subroutine parameters.
- Set the user variable names to "paramInt1", "paramInt2" and "paramDouble1", and the values to "5", "12" and "ELPotentialVal".
"ELPotentialVal" is a variable name, it needs to be defined as an equation. - Create and place the electric potential source (1 terminal) component in the circuit. Check if the circuit has already been created, and create it if it hasn't.
- Set the X-axis type to "User Subroutine (usrvsrc)" and the ID to "-1".
# Copyright (c) 2026 JSOL CORPORATION
#
# This script is released under the MIT License.
# See the full license text at:
# https://www.jmag-international.com/scriptlibrary/jmag_script_library_mit/
def setUserSubAndSubroutineParameter(study):
"""Set user subroutines and subroutine parameters"""
study.GetStudyProperties().SetValue(u"UsrsubName", u"default")
study.AddSubroutineParameter(u"paramInt1", u"5")
study.AddSubroutineParameter(u"paramInt2", u"12")
study.AddSubroutineParameter(u"ParamDouble1", u"ELPotentialVal")
def getCircuitInStudy(study):
"""Retrieve the Circuit object from the study.if it does not exist, create a new circuit"""
if study.HasCircuit():
circuit = study.GetCircuit()
else:
circuit = study.CreateCircuit()
return circuit
def setUserSubroutineInCircuit(circuit, targetCompName):
"""Place the Electric Potential source (1 terminal) element in the circuit and configure the user subroutine"""
VSComp = circuit.CreateComponent(u"VoltageSource", targetCompName)
circuit.CreateComponentInstance(VSComp, -9, 7)
VSComp.SetValue(u"XType", u"UserSub")
VSComp.SetValue(u"userid", -1)
app = designer.GetApplication()
study = app.GetCurrentStudy()
setUserSubAndSubroutineParameter(study)
circuit = getCircuitInStudy(study)
targetCompName = u"VS"
setUserSubroutineInCircuit(circuit, targetCompName)


