Click or drag to resize

Launching A Basic Simulation

This example will go over the basics of launching an EMA3D simulation from within SpaceClaim's built-in IronPython environment. This example assumes that the user has already done the bulk of their model's configuration using the GUI and wants to automate the running of the simulation perhaps to perform a variable sweep or some sort of optimization.

In general, there are 3 main steps required to start an EMA3D simulation.

  1. Meshing - This step may or may not be required depending on the type of sweep/modification that is being performed. For example, a sweep that progressively adjusts some geometry or perhaps changes the model step size in a mesh refinement study would require either a full or partial re-mesh of the model. Whereas, a sweep that adjusts the resistance of a material would not require the model to be re-meshed between iterations.
  2. Export - In this step, the model's simulation files are compiled and exported to a directory to be used by the solver.
  3. Execute - In this step, the solver is called to run on the exported simulation files.

In its most basic form, these 3 steps can be executed in only a few lines of code:

Basic Run Script
import clr
apipath = "ema3d.Api.V25.dll"
clr.AddReferenceToFileAndPath(apipath)

from ema3d.Api.V25.Core.Execution import E3DWriter
from ema3d.Api.V25.Core.Execution import E3DSimulation
from ema3d.Api.V25.Core.Meshing import FDMeshEngine

document = Window.ActiveWindow.Document

# Step 1 - Mesh the geometry in the current document (Optional).
FDMeshEngine.Create(document).Mesh()

# Step 2 - Export the current simulation files.
path = E3DWriter.Write(document, E3DWriter.GetSim())

# Step 3 - Execute the simulation and block until the simulation completes.
E3DSimulation.Launch(path).WaitForCompletion()

Additional runtime messages can be returned from the API by attaching to WarningThrown and MessageReceived events:

Run Script With Output
import warnings
import clr

# load the api referenced dll's into the Python script
import clr
apipath = "ema3d.Api.V25.dll"
clr.AddReferenceToFileAndPath(apipath)

# import the EMA3D API and the classes we will need into the namespace
from ema3d.Api.V25.Core.Execution import E3DWriter
from ema3d.Api.V25.Core.Execution import E3DSimulation
from ema3d.Api.V25.Core.Execution import PythonCommand
from ema3d.Api.V25.Core.Meshing import FDMeshEngine

from ema3d.Api.V25.Results import ResultFileCollection, XYDataFrame



# Callback function for warning events.
def OnWarningThrown(sender, args):
    warnings.warn(args.Message)

# Callback function for message events.
def OnMessageReceived(sender, args):
    print(args.Message)

document = Window.ActiveWindow.Document;

# Step 1 - Mesh the geometry in the current document (Optional).
meshEngine = FDMeshEngine.Create(document);
meshEngine.MessageReceived += OnMessageReceived
meshEngine.WarningThrown += OnWarningThrown
meshEngine.Mesh();

# Step 2 - Export the current simulation files.
writer = E3DWriter.Create(document, E3DWriter.GetSim())
writer.WarningThrown += OnWarningThrown
path = writer.Write();

# Step 3 - Execute the simulation and block until the simulation completes.
simulation = E3DSimulation.Launch(path)
simulation.MessageReceived += OnMessageReceived
simulation.WaitForCompletion()


# Step 4 - Read data in simulation
results = ResultFileCollection.CreateE3D(path)

# get the results for the probe that we created
resultFile = results["Field Probe"]

# call an external python script to process the data
command = PythonCommand.Launch(PythonCommand.Scripts.FieldStatistics, "\"" + resultFile.FilePath + "\" " + str(0.05) + "\" " + resultFile.FilePath.replace(".dat","_estat.dat"))
command.MessageReceived += OnMessageReceived
command.WaitForCompletion()

 """
# Iterate over the data in IronPython
for resultFile in ResultFileCollection.CreateE3D(path):
    print("Reading File: " + resultFile.DisplayName + ": " + resultFile.FilePath)

    # Iterate over data in IronPython API
    with XYDataFrame.Read(resultFile) as frame:
        # iterate rowwise over data
        for row in frame.Rows:
            print(", ".join([str((value.X, value.Y)) for value in row]))

        # iterate columnwise over data
        for column in frame.Columns:
            print("Reading column: " + column.DisplayName)
            for value in column:
                print(str((value.X, value.Y)))
"""