Skip to content

Python API

Scripting interface for Sim4Life

124 Topics 357 Posts
  • Extract the bounding box of a solid

    Pinned
    2
    0 Votes
    2 Posts
    734 Views
    SylvainS

    There is a function that returns the bounding box of an entity.
    Let us assume you have an entity called 'object 1'. The following code should return the coordinate vectors of two diagonally opposite corners of the bounding box:

    import s4l_v1.model as model import XCoreModeling entity = model.AllEntities()['object 1'] bb_lower, bb_upper = XCoreModeling.GetBoundingBox([entity])

    You can then use the model.CreateWireBlock() function to create an object which you can use in your simulation settings.

    For more detail:

    help(XCoreModeling.GetBoundingBox) help(model.CreateWireBlock)
  • Moving from SEMCAD 14.8 to Sim4Life python API

    Pinned
    3
    2 Votes
    3 Posts
    1k Views
    pcrespoP

    @pcrespo said in Moving from SEMCAD 14.8 to Sim4Life python API:

    How can we synchronize the entities in a model with a simulation (say, s4l_v1.simulation.emfdtd.Simulation)? Previously, the SolidRegions in a simulation seemed to be synchronized with model entities automatically, but it seems that SolidRegions are not available in simulations anymore.

    Yes, the philosophy there changed a bit. The idea is that every context adds a new layer of information on top of the previous one. When the user starts modeling, it is expected to define the geometry of the problem. For instance, what is the shape, position and size of a given entity.

    The simulation context adds the necessary information to the geometry of the problem so that we can perform a numerical simulation. These new settings could be related with the physics of the problem (e.g. electric properties of a given geometry ) or with the numerical method (e.g. fdtd grid details for the discretization of a given geometry). Creating a simulation in the UI reveals what type of settings each simulation type adds on the associated modeling entities.

    Another big different with respect to SEMCAD is that every simulation uses a selection of entities instead of using by default all and having to unselect/ignore them. It has the same result but the producer is reversed.

    Then, coming back to the API, assume we want to simulate a dipole antenna.. To start I modeled two arms and a line to define the feed. I can retrieve them by name as follows:

    import s4l_v1.model as model entities = model.AllEntities() arm1 = entities['Arm 1'] arm2 = entities['Arm 2'] line = entities['SourceLine']

    Now i want to create a simulation that uses this geometry. In order to assign a material to these entities, I need to
    a) associate the entities to the simulation (they become simulation components)
    b) create material settings and
    c) assign these settings to the associated entities.
    The API provides a shortcut for these three operations simulation.Add*Settings . This directly returns the settings object assigned to the entity within this simulation. Then we can set the values of the different properties:

    simulation = fdtd.Simulation() # ... some general settings ... # Materials: dipole_material = simulation.AddMaterialSettings([arm1, arm2]) dipole_material.Name = 'Dipole Arms' dipole_material.MaterialType = dipole_material.MaterialType.enum.PEC

    and analogously with the line entity:

    edgesrc_settings = sim.AddEdgeSourceSettings([line,]) options = edgesrc_settings.ExcitationType.enum edgesrc_settings.ExcitationType = options.Gaussian edgesrc_settings.CenterFrequency = 300., units.MHz edgesrc_settings.Bandwidth = 300., units.MHz

    The model entity in a simulation and with all the extra layers is now referred as a simulation component. In other words, a simulation component is a geometry (defined by model entity) with several layers of settings corresponding to the physics and the numerical methods.

  • API Documentation - searching for functions

    Pinned
    3
    1 Votes
    3 Posts
    1k Views
    SylvainS

    "Experimental" in that context means that the API may change in the future and that backward compatibility may break.

  • Algorithm "Max Modulation" was not yet exposed in the Python API

    11
    0 Votes
    11 Posts
    14 Views
    C

    @halder thank you for the explanation and laying out the steps for scaling the field. I'm also trying to normalize my current to 1mA in my simulations. However, I'm running an older version of sim4life, v6.2. Is this "Current Extractor" tool only available in newer versions of the software? Are there any concerns with backwards compatibility of previous models if I were to update to the current version of sim4life?

    I was following the steps on an old post and using the "Total Flux" to evaluate my currents. Would an equivalent method to the "Current Extractor" tool be to evaluate "Total Flux" for the Absolute Value of J field on the surface of my boundary condition? Thanks in advance

  • How to create voxels using a script?

    1
    0 Votes
    1 Posts
    8 Views
    No one has replied
  • Is it possible to use Python API without opening Sim4Life?

    4
    0 Votes
    4 Posts
    390 Views
    brynB

    You can open a smash file using

    import s4l_v1 as s4l s4l.document.Open(smash_file_path)

    You can save it using

    s4l.document.Save(smash_file_path)

    You can find a simulation using (assuming you have unique simulation names

    sim = [sim for sim in s4l.document.AllSimulations if sim.Name == "the name of your simulation"][0]

    If you want to load different models (from .sab file or .smash), but don't need to load e.g. simulations (in .smash file), you can import the file

    entities = s4l.model.Import(file_path)

    if you need to reset the document or model because you are accumulating too many entities and memory use is getting too high, you can use e.g.

    # reset the model sl4.model.Clear() # or keep entities, but discard model history import XCoreModeling as xcm xcm.GetActiveModel().ClearHistory() # or reset entire document (reset model, simulations, analysis) s4l.document.New()
  • How to suppress messages in the Python console?

    1
    0 Votes
    1 Posts
    20 Views
    No one has replied
  • Location of Simulation Outputs and Voxel Coordinates?

    1
    0 Votes
    1 Posts
    24 Views
    No one has replied
  • About TI Simulation

    4
    0 Votes
    4 Posts
    37 Views
    L

    I want to see the impact of different grid sizes on the simulation, so I cloned a pair of simulations and modified the maximum step size of the grid. However, I found that if I create a new pair of simulations using the same settings, the grid sizes, simulation durations, and results of the new simulations and the cloned simulations are all different. Why does this happen?

  • Iso Surfaces - Creating volumes with a threshold

    1
    0 Votes
    1 Posts
    43 Views
    No one has replied
  • Accessing specific entities of my model from the python API

    2
    1 Votes
    2 Posts
    42 Views
    L

    found my own answer:

    image.png

  • Installing Additional Python Packages in Sim4Life Environment

    Solved
    9
    1 Votes
    9 Posts
    272 Views
    G

    @Sylvain Yes thanks finally I succeed!

  • 1 Votes
    1 Posts
    53 Views
    No one has replied
  • Clear or Reset Geometry Workspace in Python?

    4
    0 Votes
    4 Posts
    446 Views
    brynB

    I typically do the following (which also clears the history and frees memory):

    import XCoreModeling as xcm xcm.GetActiveModel().Clear()
  • RuntimeError: Could not allocate memory for numpy array

    3
    0 Votes
    3 Posts
    1k Views
    SylvainS

    have you tried overwriting the E_field and raw_data variables?

  • Extract the EM E field along the Spline

    3
    0 Votes
    3 Posts
    255 Views
    A

    Thank you so much for hour help with the technical issue!
    Your assistance made a big difference, and I truly appreciate your expertise and prompt support!

  • Max modulation tool in API

    2
    0 Votes
    2 Posts
    369 Views
    J

    Would like to know too in 2024 🙂

  • Change parameters in Simulation Combiner

    2
    0 Votes
    2 Posts
    276 Views
    C

    The following example for a 2-port simulation combiner should help.

    i = 0 for channel in em_multi_port_simulation_combiner.GetChannelWeights(): power = [1.0, 2.0] phase = [45, 90] em_multi_port_simulation_combiner.SetChannelWeight(channel, power[i], phase[i]) i += 1 em_multi_port_simulation_combiner.UpdateAttributes() em_multi_port_simulation_combiner.Update() document.AllAlgorithms.Add(em_multi_port_simulation_combiner)
  • Geometry Modeling - Snapping to Endpoints in Python API ?

    Solved
    3
    0 Votes
    3 Posts
    363 Views
    brynB

    Hi @dbsim4
    I think it would help a lot if you could post an image depicting what you are trying to achieve.

    Answering the question from the subject line: No, there is no snapping in Python (not sure how that API could look like), but there are functions to get the distance between entities (and corresponding closest points), which may help.

    brick1 = XCoreModeling.CreateSolidBlock(Vec3(0), Vec3(1)) brick2 = XCoreModeling.CreateSolidBlock(Vec3(2), Vec3(3)) res = XCoreModeling.GetEntityEntityDistance(brick1, brick2) print(f"Distance brick1-brick2: {res[0].Distance}") print(f"Closest point on brick1: {res[0].ClosestPosition}") print(f"Closest point on brick2: {res[1].ClosestPosition}")

    or distance to a point:

    brick = XCoreModeling.CreateSolidBlock(Vec3(0), Vec3(1)) res = XCoreModeling.GetEntityEntityDistance(brick, Vec3(3)) print(f"Distance brick-point: {res.Distance}") print(f"Closest point on brick: {res.ClosestPosition}")

    For geometry that has end-points or corners, you could extract the vertices and again use distance wrt some other point as a way to write a script.

    edge = XCoreModeling.CreateEdge(Vec3(0), Vec3(1)) vertices = XCoreModeling.GetVertices(edge) assert len(vertices) == 2 for v in vertices: print(v.Position) brick = XCoreModeling.CreateSolidBlock(Vec3(0), Vec3(1)) vertices = XCoreModeling.GetVertices(edge) assert len(vertices) == 8
  • How to create solid circle ie. with surface ?

    Solved
    4
    0 Votes
    4 Posts
    376 Views
    brynB

    @dbsim4 regarding your "Update", I am not sure I understand the context. Are you trying to assign the circle loop as an edge source?