In this example, using the support files, you will insert a Magnetostatic analysis object into an undefined Mechanical session and execute a sequence of python journal commands that will define and solve the analysis.
This example begins in the Mechanical application. It requires you to download the following files.
Magnetostatic_001.agdb
Magnetostatic_001.py
Magnetostatic_001_Mat1.xml
Magnetostatic_001_Mat2.xml
These files are available here.
Procedure
Open Workbench and insert a system into the Project Schematic.
Double-click the Engineering Data cell to open the workspace.
Select > , navigate to the proper folder location and select Magnetostatic_001_Mat1.xml. Repeat this action for the Magnetostatic_001_Mat2.xml file.
Return to the Project tab.
Right-click the Geometry cell and select > and then navigate to the proper folder location and select Magnetostatic_001.agdb.
Open Mechanical: right-click the Model cell and select .
Select the Automation tab and select the Scripting option to open the Mechanical Scripting pane.
Select the Open Script option (
) from the Editor toolbar. Navigate to the
proper folder location and select
Magnetostatic_001.py.Select the Run Script option (
) from the Editor toolbar.
Scripts Illustrated
In this example, the python file automatically performs the following actions:
# Scenario1 - Set up Unit System: Set up the Unit Systems to Metric (m, kg, N, s, V, A),
ExtAPI.Application.ActiveUnitSystem = MechanicalUnitSystem.StandardMKS
# Scenario 2 - Set up the Tree Object Items: Define the parts, connections, mesh, Magnetostatic system, the analysis settings, and the solution navigating through the tree items.
GEOMETRY = Model.Geometry
BODY1 = [x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == 'air_body'][0]
BODY2 = [x for x in ExtAPI.DataModel.Tree.AllObjects if x.Name == 'conductor'][0]
MESH = Model.Mesh
MAGNETOSTATIC = DataModel.AnalysisByName("Magnetostatic")
ANSLYSIS_SETTINGS = MAGNETOSTATIC.AnalysisSettings
SOLUTION = MAGNETOSTATIC.Solution
# Scenario 3 - Define Named Selections: Define the Named selections that will be used to define the Mesh Controls, the loads and the results.
NS_VOLUME1 = DataModel.GetObjectsByName("NS_Volume1")[0]
NS_VOLUME2 = DataModel.GetObjectsByName("NS_Volume2")[0]
NS_VOLUME = DataModel.GetObjectsByName("NS_Volume")[0]
NS_FACES5 = DataModel.GetObjectsByName("Faces5")[0]
NS_FACE1 = DataModel.GetObjectsByName("Face1")[0]
NS_FACE2 = DataModel.GetObjectsByName("Face2")[0]
# Scenario 4 Create Coordinate System: Create Coordinate System that will be used to define the results.
COORDINATE_SYSTEMS = Model.CoordinateSystems
GCS = COORDINATE_SYSTEMS.Children[0]
LCS = COORDINATE_SYSTEMS.AddCoordinateSystem()
LCS.OriginX = Quantity('0 [m]')
LCS.PrimaryAxis = CoordinateSystemAxisType.PositiveYAxis
LCS.AddTransformation(TransformationType.Rotation,CoordinateSystemAxisType.PositiveZAxis)
LCS.SetTransformationValue(1,-120)
# Scenario 5 Assign materials: Assign materials to bodies.
BODY1.Material = 'Emag_Air'
BODY2.Material = 'Emag_Copper_Alloy'
# Scenario 6 - Define Mesh Settings: Assign Body Sizing with element size of 0.5 m.
BODY_SIZING = MESH.AddSizing()
BODY_SIZING.Location = NS_VOLUME
BODY_SIZING.ElementSize = Quantity('0.5 [m]')
MESH.GenerateMesh()
# Scenario 7 - Insert Magnetostatic loads: Insert and Magnetic Flux Parallel, Source Conductor with conductor type as Solid with 1 turn. Insert Source Conductor Voltage and Current loads.
MAGNETIC_FLUX_PARALLEL = MAGNETOSTATIC.AddMagneticFluxParallel()
MAGNETIC_FLUX_PARALLEL.Location = NS_FACES5
SOURCE_CONDUCTOR = MAGNETOSTATIC.AddSourceConductor()
SOURCE_CONDUCTOR.Location = NS_VOLUME2
SOURCE_CONDUCTOR.ConductorType = SourceConductorType.Solid
SOURCE_CONDUCTOR.NumberOfTurns = 1
SOURCE_CONDUCTOR_CURRENT = SOURCE_CONDUCTOR.AddCurrent()
SOURCE_CONDUCTOR_CURRENT.Location = NS_FACE1
SOURCE_CONDUCTOR_CURRENT.Magnitude.Output.DiscreteValues=[Quantity('0 [A]'), Quantity('-120 [A]')]
SOURCE_CONDUCTOR_VOLTAGE = SOURCE_CONDUCTOR.AddVoltage()
SOURCE_CONDUCTOR_VOLTAGE.Location = NS_FACE2
# Scenario 8 - Insert Results: Insert Directional Magnetic Field Intensity, Directional Magnetic Flux Density, Magnetic Directional Forces, Force Summation Probe and Torque Probe
DIRECTIONAL_MAGNETIC_FIELD_INTENSITY = SOLUTION.AddDirectionalMagneticFieldIntensity()
DIRECTIONAL_MAGNETIC_FIELD_INTENSITY.Location = NS_VOLUME1
DIRECTIONAL_MAGNETIC_FIELD_INTENSITY.NormalOrientation = NormalOrientationType.YAxis
DIRECTIONAL_MAGNETIC_FLUX_DENSITY = SOLUTION.AddDirectionalMagneticFluxDensity()
DIRECTIONAL_MAGNETIC_FLUX_DENSITY.Location = NS_VOLUME1
DIRECTIONAL_MAGNETIC_FLUX_DENSITY.NormalOrientation = NormalOrientationType.YAxis
DIRECTIONAL_FORCE01 = SOLUTION.AddMagneticDirectionalForces()
DIRECTIONAL_FORCE01.Location = NS_VOLUME2
DIRECTIONAL_FORCE02 = SOLUTION.AddMagneticDirectionalForces()
DIRECTIONAL_FORCE02.Location = NS_VOLUME2
DIRECTIONAL_FORCE02.NormalOrientation = NormalOrientationType.YAxis
DIRECTIONAL_FORCE03 = SOLUTION.AddMagneticDirectionalForces()
DIRECTIONAL_FORCE03.Location = NS_VOLUME2
DIRECTIONAL_FORCE03.NormalOrientation = NormalOrientationType.ZAxis
DIRECTIONAL_FORCE04 = SOLUTION.AddMagneticDirectionalForces()
DIRECTIONAL_FORCE04.Location = NS_VOLUME2
DIRECTIONAL_FORCE04.NormalOrientation = NormalOrientationType.YAxis
DIRECTIONAL_FORCE04.CoordinateSystem = LCS
DIRECTIONAL_FORCE05 = SOLUTION.AddMagneticDirectionalForces()
DIRECTIONAL_FORCE05.Location = NS_VOLUME2
DIRECTIONAL_FORCE05.NormalOrientation = NormalOrientationType.ZAxis
DIRECTIONAL_FORCE05.CoordinateSystem = LCS
FORCE_SUMMATION01 = SOLUTION.AddForceSummationProbe()
FORCE_SUMMATION01.GeometryLocation = NS_VOLUME2
FORCE_SUMMATION02 = SOLUTION.AddForceSummationProbe()
FORCE_SUMMATION02.GeometryLocation = NS_VOLUME2
FORCE_SUMMATION02.ResultSelection = ProbeDisplayFilter.YAxis
FORCE_SUMMATION03 = SOLUTION.AddForceSummationProbe()
FORCE_SUMMATION03.GeometryLocation = NS_VOLUME2
FORCE_SUMMATION03.ResultSelection = ProbeDisplayFilter.ZAxis
FORCE_SUMMATION04 = SOLUTION.AddForceSummationProbe()
FORCE_SUMMATION04.GeometryLocation = NS_VOLUME2
FORCE_SUMMATION04.ResultSelection = ProbeDisplayFilter.YAxis
FORCE_SUMMATION04.Orientation = LCS
FORCE_SUMMATION05 = SOLUTION.AddForceSummationProbe()
FORCE_SUMMATION05.GeometryLocation = NS_VOLUME2
FORCE_SUMMATION05.ResultSelection = ProbeDisplayFilter.ZAxis
FORCE_SUMMATION05.Orientation = LCS
TORQUE01 = SOLUTION.AddTorqueProbe()
TORQUE01.GeometryLocation = NS_VOLUME2
TORQUE02 = SOLUTION.AddTorqueProbe()
TORQUE02.GeometryLocation = NS_VOLUME2
TORQUE02.ResultSelection = ProbeDisplayFilter.YAxis
TORQUE03 = SOLUTION.AddTorqueProbe()
TORQUE03.GeometryLocation = NS_VOLUME2
TORQUE03.ResultSelection = ProbeDisplayFilter.ZAxis
TORQUE04 = SOLUTION.AddTorqueProbe()
TORQUE04.GeometryLocation = NS_VOLUME2
TORQUE04.ResultSelection = ProbeDisplayFilter.YAxis
TORQUE04.Orientation = LCS
TORQUE05 = SOLUTION.AddTorqueProbe()
TORQUE05.GeometryLocation = NS_VOLUME2
TORQUE05.ResultSelection = ProbeDisplayFilter.ZAxis
TORQUE05.Orientation = LCS
# Scenario 9 - Solve and Define the Results: Solve the system and set the results variables. Note the Scripting window returns the value of any of the variables stored.
SOLUTION.Solve(True)
DIRECTIONAL_MAGNETIC_FIELD_INTENSITY_MAX = DIRECTIONAL_MAGNETIC_FIELD_INTENSITY.Maximum.Value
DIRECTIONAL_MAGNETIC_FLUX_DENSITY_MAX = DIRECTIONAL_MAGNETIC_FLUX_DENSITY.Maximum.Value
FORCE_SUMMATION01_X_AXIS = FORCE_SUMMATION01.XAxis.Value
FORCE_SUMMATION02_Y_AXIS = FORCE_SUMMATION02.YAxis.Value
FORCE_SUMMATION03_Z_AXIS = FORCE_SUMMATION03.ZAxis.Value
FORCE_SUMMATION04_Y_AXIS = FORCE_SUMMATION04.YAxis.Value
FORCE_SUMMATION05_Z_AXIS = FORCE_SUMMATION05.ZAxis.Value
TORQUE01_X_AXIS = TORQUE01.XAxis.Value
TORQUE02_Y_AXIS = TORQUE02.YAxis.Value
TORQUE03_Z_AXIS = TORQUE03.ZAxis.Value
TORQUE04_Y_AXIS = TORQUE04.YAxis.Value
TORQUE05_Z_AXIS = TORQUE05.ZAxis.ValueSummary
This example demonstrates how scripting in Mechanical can be used to automate your actions.