[S0027] Register a user-defined response value

 

As a means of comparing and evaluating the results of multiple cases, there are response values ​​that are defined using a calculation method appropriate to the purpose.
JMAG provides a variety of calculation results output and response value calculation methods, but it's also possible to register user-defined response values ​​using scripts and use them in optimization, etc.
If this user-defined response values need to be referred during an optimization, register them in the post-calculation script.
This script registers the rotation angle at which the torque is maximized as the user-defined response value.

Preconditions

  • One or more study has been created.
    This script runs on the active magnetic field transient response analysis study in the project tree.
  • User-defined response values ​​can be used regardless of the type of study, but this example targets magnetic field transient response analysis with rotational motion in order to obtain the rotation angle at which the torque is maximized.

Script Function

  • Get the object of the torque table results.
  • Specify the type name "Torque" for the torque: nodal force condition, retrieve the title of the first assigned torque: nodal force condition, and retrieve the column number in the table result to be referred.
  • Compare the torque values ​​in each row and retrieve the row number with the maximum value.
  • Get the rotation angle at the retrieved row number and register it as a user-defined response value.
    If the study already has a response value name, it will be registered as the value for the specified case; otherwise, a response value will be created and registered.
# 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 getConditionsSpecifiedType(targetStudy, typeName):
    """Get specified type conditions applied to the target study"""
    matches = []
    numConds = targetStudy.NumConditions()

    for i in range(numConds):
        condition = targetStudy.GetCondition(i)
        CondType = condition.GetScriptTypeName()
        if CondType == typeName:
            matches.append(condition)
    return matches

def findTargetColumnByConditionName(targetStudy, targetResultTableData, referenceCondTypeName):
    """Retrieve the desired column number from the table format result where column names are determined by the tilte of the analysis condition"""
    ret = -1
    conditions = getConditionsSpecifiedType(targetStudy, referenceCondTypeName)
    if len(conditions) <= 0:
        print(u"Reference condition is not found.")
    else:
        # Search for the column using the title of the first condition found
        nameToFind = conditions[0].GetName()
        for col in range(0, targetResultTableData.GetCols()):
            if targetResultTableData.GetColName(col) == nameToFind:
                ret = col
                break
    return ret

def setOriginalResponseValue(varName, targetStudy, targetCaseNo):
    """Register the rotation angle at the maximum torque as a user-defined response value."""
    torqueData = targetStudy.GetResultTable().GetData(u"Torque")
    if not torqueData.IsValid():
        print(u"Torque result doesn't exist.")
        return
    # Search for the column number to refer to by the tile of the Torque:Nodal Force condition
    targetColNo = findTargetColumnByConditionName(targetStudy, torqueData, u"Torque")
    if targetColNo <= -1:
        print(u"Column is not found.")
        return
    maxTorque = torqueData.GetValue(0, targetColNo)
    maxRow = 0
    # Find the row number where the torque is maximum
    for row in range(1, torqueData.GetRows()):
        if maxTorque < torqueData.GetValue(row, targetColNo):
            maxTorque = torqueData.GetValue(row, targetColNo)
            maxRow = row
    # Get the rotation angle of the obtained row number
    angleAtMaxTorque = torqueData.GetAngle(maxRow)
    print(varName, angleAtMaxTorque)

    targetStudy.SetUserResponseVariable(varName, targetCaseNo, angleAtMaxTorque)

app = designer.GetApplication()
responseVarName = u"Angle_atMaxTorque"
study = app.GetCurrentStudy()
caseNo = study.GetCurrentCase()
setOriginalResponseValue(responseVarName, study, caseNo)

Download Python source code

How to use script file

Use the JMAG Script Library after reading and agreeing to the following terms of use.

Search Filter

  • All Categories

An engineer's diary
JMAG-Express Online