Skip to content

Python API

Scripting interface for Sim4Life

121 Topics 342 Posts
  • Extract the bounding box of a solid

    Pinned
    2
    0 Votes
    2 Posts
    721 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.

  • Location of Simulation Outputs and Voxel Coordinates?

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

    4
    0 Votes
    4 Posts
    18 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
    34 Views
    No one has replied
  • Accessing specific entities of my model from the python API

    2
    1 Votes
    2 Posts
    32 Views
    L

    found my own answer:

    image.png

  • Installing Additional Python Packages in Sim4Life Environment

    Solved
    9
    1 Votes
    9 Posts
    251 Views
    G

    @Sylvain Yes thanks finally I succeed!

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

    4
    0 Votes
    4 Posts
    430 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
    246 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
    364 Views
    J

    Would like to know too in 2024 🙂

  • Is it possible to use Python API without opening Sim4Life?

    2
    0 Votes
    2 Posts
    348 Views
    brynB

    short answer: you can run most of the Python API without opening the Sim4Life user interface.

    you have different options

    you can call Sim4Life.exe --run your_script.py you can write scripts and run them using the python.exe in the Sim4Life installation you can create a virtual environment with the Sim4Life packages, e.g. "C:\Program Files\Sim4Life_6.2.2.6592\Python\python.exe" -m venv .venv --system-site-packages and then use the python from this venv to run your script

    The first option opens Sim4Life, runs the script, and closes Sim4Life again. It probably is the easiest option to work robustly.

    The latter two options run without the UI and by default without an application. In most cases, you will need an application though (e.g. the Application initializes the active model, which is needed for modeling). For Python scripting without the UI you can create a console application. We do this e.g., to run Python tests without opening the UI (which is slower). To create a console application you can usually call:

    import XCore XCore.GetOrCreateConsoleApp() # now you can do some stuff import s4l_v1 as s4l sphere = s4l.model.CreateSolidSphere(s4l.model.Vec3(0.0), 1.0)
  • Change parameters in Simulation Combiner

    2
    0 Votes
    2 Posts
    266 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
    353 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
    370 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?

  • Refresh Matlab Expoter

    2
    0 Votes
    2 Posts
    317 Views
    brynB

    Like all entities in the Analysis pipeline, the Matlab exporter has an Update method to execute.

    to find more information on a specific class you can

    export the pipeline from the GUI as a python script use the help() function to get more information on a specific class, e.g. import s4l_v1 as s4l help(s4l.analysis.exporters.MatlabExporter)
  • Interaction with Neuron results

    2
    0 Votes
    2 Posts
    350 Views
    AntoninoMCA

    @Hüfer yes, you can access all the simulation results as well as the parameters of you EM-neuronal simulations using the Python interface. For example, you can access titration data, or the transmembrane voltage profiles stored on a point or line sensor data, etc. or you can dynamically change the stimulation pulse shape parameters (phase duration, amplitude, etc.).

    Sim4Life provides you already with some Python tutorials. There are at least 3 tutorials provided that show how to use Python to setup a T-Neuron simulation, how to run it and how to access the simulation results. Please have a look at these tutorials to learn Sim4Life programming (if you are a Python expert, learning will take you a day!).

    To access these tutorials, open the 'Scripter' panel in Sim4Life, and load one of the scripts in the folder opened when selecting "Open Example Script" clicking on the open folder button.

  • multiport sensor combiner freq normalization

    1
    0 Votes
    1 Posts
    201 Views
    No one has replied