Simulation outputs
-
Hello everyone!
I'm currently running an electrical simulation using Sim4Life. So far, I've been using the analysis options provided by S4L, but I'm curious if there's a way to directly access and manipulate the output.
The electric field generated is of type <class 's4l_v1._api.analysiswrappers.AlgorithmOutput'>. According to the API, this represents "the output port of an algorithm, which is passed to the input connection of another algorithm. This class is the intermediate object used in a connection between two algorithms."
Is there a way to access the field matrix without exporting it to MATLAB?
Thanks in advance for your help!
-
You can directly access the data of any algorithm. Here is an example to get you started:
import numpy as np simulation = document.AllSimulations[0] simulation_extractor = simulation.Results() em_sensor_extractor = simulation_extractor["Overall Field"] efield_sensor = em_sensor_extractor['EM E(x,y,z,f0)'] efield_sensor.Update() # THIS IS NECESSARY, otherwise the algorithm output will not contain any data and there will be an allocation error when trying to access it... field_data= efield_sensor.Data grid = field_data.Grid print(grid.XAxis) # for example... e_field = field_data.Field(0) # 0 is the first "snapshot", typically this is the recorded frequency print(e_field.shape) # should be the dimensions of the numpy array num_components = e_field.shape[1] # 3 for vectors nx, ny, nz = grid.XAxis.size, grid.YAxis.size, grid.ZAxis.size if field_data.ValueLocation == s4l.analysis.core.eValueLocation.kCellCenter: nx, ny, nz = nx - 1, ny - 1, nz - 1 e_field_reshaped= np.reshape(e_field, (nz, ny, nx, -1))
-
@Sylvain Hello!
Thank you so much!
So as far as I understand the coordinates are located on the grid. Which I guess depends on the meshes for the simulation.On the other hand, the e_field is store in .Field(0) which holds a vector for each point.
And the reshape np.reshape(e_field, (nz, ny, nx, -1)). Its because the first coordinate with respect to the e_field is in the z axis?
-
it is because the data is ordered using x-fastest indexing. This means (for a scalar field)
- if you increase the index by 1, you move one position in x direction (unless you are at the boundary).
- if you move by nx, you move by one position in y direction (the stride is nx)
- if you move by nx * nz, you move by one position in z direction (the stride is nx * ny)