Skip to content
  • 1 Votes
    2 Posts
    53 Views
    brynB
    This is a simplified example (partly generated using the "To Python" action in the context menu). The example does the following creates an EM simulation as heat source creates a thermal simulation creates a "continue" simulation using the thermal distribution from the first simulation extracts the temperature field from pathlib import Path import numpy as np import s4l_v1.document as document import s4l_v1.materials.database as database import s4l_v1.model as model import s4l_v1.simulation.emfdtd as emfdtd import s4l_v1.simulation.thermal as thermal import s4l_v1.units as units from s4l_v1 import Unit def create_emfdtd_simulation(): simulation = emfdtd.Simulation() entity__lines1 = model.AllEntities()["Edge Source"] entity__sphere1 = model.AllEntities()["Sphere 1"] material_settings = simulation.AddMaterialSettings([entity__sphere1]) simulation.LinkMaterialWithDatabase(material_settings, database["IT'IS LF 5.0"]["Liver"]) edge_source_settings = emfdtd.EdgeSourceSettings() simulation.Add(edge_source_settings, [entity__lines1]) edge_sensor_settings = emfdtd.EdgeSensorSettings() simulation.Add(edge_sensor_settings, [entity__lines1]) automatic_grid_settings = [x for x in simulation.AllSettings if isinstance(x, emfdtd.AutomaticGridSettings) and x.Name == "Automatic"][0] simulation.Add(automatic_grid_settings, [entity__lines1, entity__sphere1]) automatic_voxeler_settings = [x for x in simulation.AllSettings if isinstance(x, emfdtd.AutomaticVoxelerSettings) and x.Name == "Automatic Voxeler Settings"][0] simulation.Add(automatic_voxeler_settings, [entity__lines1, entity__sphere1]) simulation.UpdateAllMaterials() simulation.UpdateGrid() return simulation def create_thermal_transient(em_overall_field): simulation = thermal.TransientSimulation() entity__sphere1 = model.AllEntities()["Sphere 1"] material_settings = simulation.AddMaterialSettings([entity__sphere1]) simulation.LinkMaterialWithDatabase(material_settings, database["IT'IS LF 5.0"]["Liver"]) initial_condition_settings = thermal.InitialConditionSettings() initial_condition_settings.InitialTemperature = 37.0, Unit("C") simulation.Add(initial_condition_settings, [entity__sphere1]) transient_heat_source_settings = simulation.AddHeatSourceSettings([em_overall_field]) automatic_grid_settings = [x for x in simulation.AllSettings if isinstance(x, thermal.AutomaticGridSettings) and x.Name == "Automatic"][0] simulation.Add(automatic_grid_settings, [entity__sphere1]) automatic_voxeler_settings = [x for x in simulation.AllSettings if isinstance(x, thermal.AutomaticVoxelerSettings) and x.Name == "Automatic Voxeler Settings"][0] simulation.Add(automatic_voxeler_settings, [entity__sphere1]) simulation.UpdateAllMaterials() simulation.UpdateGrid() return simulation if __name__ == "__main__": # create dummy model line = model.CreatePolyLine([model.Vec3(0,0,20), model.Vec3(5,0,20)]) line.Name = "Edge Source" sphere = model.CreateSolidSphere(model.Vec3(0,0,0), radius=15) sphere.Name = "Sphere 1" sphere.MaterialName = "Liver" # create heat source em_sim = create_emfdtd_simulation() em_sim.Name = "EM Heat Source" document.AllSimulations.Add( em_sim ) # setup initial thermal simulation th_sim = create_thermal_transient(em_sim.AllComponents["Overall Field"]) th_sim.Name = "T Initial Simulation" document.AllSimulations.Add( th_sim ) # setup simulation using result from previous simulation th_continue = th_sim.CloneWithDiscretization() th_continue.Name = "T Continue Simulation" init_cond_settings = th_continue.GlobalInitialConditionSettings init_cond_settings.InitializationOptions = init_cond_settings.InitializationOptions.enum.ContinueSimulation init_cond_settings.InputSensor = "Overall Field" init_cond_settings.InputSimulation = th_sim.Name document.AllSimulations.Add( th_continue ) # run simulations project_file_path = Path.home() / "t_continue.smash" em_sim.CreateVoxels(project_file_path) em_sim.RunSimulation() th_sim.CreateVoxels(project_file_path) th_sim.RunSimulation() th_continue.CreateVoxels(project_file_path) th_continue.RunSimulation() # extract some results th_continue_extractor = th_continue.Results() th_sensor_extractor = th_continue_extractor["Overall Field"] th_sensor_extractor.Outputs["T(x,y,z,t)"].Update() field = th_sensor_extractor.Outputs["T(x,y,z,t)"].Data print(field)
  • 0 Votes
    3 Posts
    2k Views
    brynB
    Here is a simple implementation to write an iSEG tissue list file, given a dictionary mapping the label index to a name: https://github.com/dyollb/s4l-scripts/blob/df8e2241f87ca91d71138e9d2b3d4336dadb82dc/src/anisotropic_conductivity/load_labels.py#L44
  • Installing Additional Python Packages in Sim4Life Environment

    Solved Python API python pip
    9
    1 Votes
    9 Posts
    3k Views
    G
    @Sylvain Yes thanks finally I succeed!
  • Clear or Reset Geometry Workspace in Python?

    Python API python geometry
    4
    0 Votes
    4 Posts
    2k Views
    brynB
    I typically do the following (which also clears the history and frees memory): import XCoreModeling as xcm xcm.GetActiveModel().Clear()
  • Geometry Modeling - Snapping to Endpoints in Python API ?

    Solved Python API python
    3
    0 Votes
    3 Posts
    2k 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
  • Export spline points

    CAD Modeling python
    3
    0 Votes
    3 Posts
    1k Views
    L
    That worked- thanks a lot!
  • Evaluate distance along a (curved) line

    CAD Modeling python
    1
    1 Votes
    1 Posts
    734 Views
    No one has replied
  • How to Change Material Properties with Python?

    Solved Python API emfdtd python material python api
    5
    1 Votes
    5 Posts
    2k Views
    M
    Mesh_material = sim.AddMaterialSettings(themesh(fromstl)) Mesh_material.Name = yourname Mesh_material.ElectricProps.Conductivity = v # S/m
  • 2 Votes
    1 Posts
    641 Views
    No one has replied
  • 1 Votes
    3 Posts
    2k Views
    L
    Perfect thanks !!
  • VSCode extension

    Python API python vscode
    5
    1 Votes
    5 Posts
    2k Views
    L
    I am not sure to be honest why that was an issue for me with that specific version only, I installed the new version and it was there so problem solved!
  • SetUp Field Crop Filter

    Analysis & Postprocessing python
    2
    1 Votes
    2 Posts
    843 Views
    SylvainS
    In general, an easy way to create a postprocessing script is to first do it in the GUI and then use the "To-Python" function (available via right-click on the algorithm in the Explorer window). Here is what the auto-generated script looks like for a simple pipeline with a Crop Filter: # Creating the analysis pipeline # Adding a new SimulationExtractor simulation = document.AllSimulations["EM"] simulation_extractor = simulation.Results() # Adding a new EmSensorExtractor em_sensor_extractor = simulation_extractor["Overall Field"] em_sensor_extractor.FrequencySettings.ExtractedFrequency = u"All" em_sensor_extractor.Normalization.Normalize = True em_sensor_extractor.SurfaceCurrent.SurfaceResolution = 0.001, units.Meters document.AllAlgorithms.Add(em_sensor_extractor) # Adding a new FieldCropFilter inputs = [em_sensor_extractor.Outputs["EM E(x,y,z,f0)"]] field_crop_filter = analysis.core.FieldCropFilter(inputs=inputs) field_crop_filter.LowerExtent = numpy.array([1, 3, 2]) field_crop_filter.UpperExtent = numpy.array([21, 21, 21]) field_crop_filter.UpdateAttributes() document.AllAlgorithms.Add(field_crop_filter) Note fyi that you don't actually need to pass a Numpy array to UpperExtent or LowerExtent, it also works if you pass any iterable, like a list or a tuple.
  • 0 Votes
    2 Posts
    2k Views
    M
    This is common. Once you import a script, it becomes cached so even if you make changes to it and save it, python won't see the changes. You need to reload it as you said if you make changes to the imported script after running your main script.
  • 0 Votes
    2 Posts
    2k Views
    SylvainS
    This looks quite good, thank you for providing your scripts. Note that, for the second method, you could create your cylinder directly at the desired position: cylinder2 = model.CreateSolidCylinder(Vec3(0,1,-15),Vec3(0,1,-15),0.5) There is also the XCoredModeling.CoverWires() function that allows you to make a surface (i.e. face) out of a circle entity. Last, but not least, note that the line model_to_grid_filter.MaximumEdgeLength is ultimately what determines the tradeoff between accuracy and computational cost of the interpolation (since it defines the resolution of the triangulated mesh on which the interpolation is done).
  • 3 Votes
    1 Posts
    1k Views
    No one has replied
  • 0 Votes
    1 Posts
    1k Views
    No one has replied
  • 1 Votes
    1 Posts
    1k Views
    No one has replied
  • 0 Votes
    3 Posts
    2k Views
    SylvainS
    have you tried overwriting the E_field and raw_data variables?
  • 1 Votes
    2 Posts
    2k Views
    PeterStijnmanP
    Works perfectly, thanks! Edit, I think you could also get away with: import s4l_v1.model as model import XCoreModeling def get_spline_length(spline): wire = XCoreModeling.GetWires(spline)[0] return wire.GetLength()