Python code for outputting analysis results
-
Excuse me for butting in.
Nice to meet you. I’m currently a graduate student conducting research on temperature distribution in liver tumors with respiratory motion using Sim4Life.
Could you please share some code that uses Python scripts to output the analysis results of temperature distributions after running the simulation? -
Hello,
If you would just like to export the temperature distributions computed by the solver, you can use one of the built in Exporter tools (e.g. VTK, MATLAB, Text files), which produce formats that can be read externally.
I would recommend that you do this manually first by using the GUI. Once you are satisfied with the configuration, you can right-click on your analysis tree and select 'To Python' to automatically generate the code required to reproduce the analysis.
For example:
import s4l_v1.analysis as analysis import s4l_v1.document as document # Incident EM parent simulation simulation = document.AllSimulations["EM Simulation Name"] simulation_extractor = simulation.Results() # Thermal simulation simulation = document.AllSimulations["Thermal Simulation Name"] simulation_extractor_2 = simulation.Results() # Choose a sensor to extract thermo_sensor_extractor = simulation_extractor_2["Overall Field"] document.AllAlgorithms.Add(thermo_sensor_extractor) # Adding a new MatlabExporter inputs = [thermo_sensor_extractor.Outputs["T(x,y,z,t)"]] matlab_exporter = analysis.exporters.MatlabExporter(inputs=inputs) matlab_exporter.FileName = u"C:\\Users\\Test\\Downloads\\myfile.mat" matlab_exporter.UpdateAttributes() document.AllAlgorithms.Add(matlab_exporter) # Call Update() to trigger writing the file matlab_exporter.Update()This will produce a .mat file that contains separate arrays for every 'Snapshot' in your thermal simulation. For transient thermal simulations, a snapshot represents the field distribution at some point in time. It is dependent on the 'Maximum No. Samples' option in the Sensor settings of the simulation configuration.