RuntimeError: Could not allocate memory for numpy array
-
A relatively common error happen when trying to access the raw data at postprocessing, for example the script:
import numpy import s4l_v1.analysis as analysis import s4l_v1.document as document import s4l_v1.units as units # Creating the analysis pipeline # Adding a new SimulationExtractor simulation = document.AllSimulations[0] 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.SurfaceCurrent.SurfaceResolution = 0.001, units.Meters document.AllAlgorithms.Add(em_sensor_extractor) # Adding a new SliceFieldViewer E_field = em_sensor_extractor.Outputs["EM E(x,y,z,f0)"] raw_data = E_field.Data.Field(0)
produces the following error:
Traceback (most recent call last):
File "C:\Users\user\Documents\Sim4Life\4.2\Scripts\temp\temp-10.py", line 22, in <module>
raw_data = E_field.Data.Field(0)
RuntimeError: Could not allocate memory for numpy array.The reason for the error is that at the last line of the script, Sim4Life has not yet evaluated the analysis pipeline, so the data is not loaded and no memory has been allocated for it.
The fix is quite simple: simply force the analysis pipeline to update before trying to access the data, using
E_field.Update()
The full script now looks like this:
# Creating the analysis pipeline # Adding a new SimulationExtractor simulation = document.AllSimulations["Dipole (Broadband)"] 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.SurfaceCurrent.SurfaceResolution = 0.001, units.Meters document.AllAlgorithms.Add(em_sensor_extractor) # Adding a new SliceFieldViewer E_field = em_sensor_extractor.Outputs["EM E(x,y,z,f0)"] E_field.Update() raw_data = E_field.Data.Field(0)