Skip to content
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

ZMT zurich med tech

  1. Home
  2. Sim4Life
  3. Python API
  4. RuntimeError: Could not allocate memory for numpy array

RuntimeError: Could not allocate memory for numpy array

Scheduled Pinned Locked Moved Python API
pythonnumpyallocation
3 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SylvainS Offline
    SylvainS Offline
    Sylvain
    ZMT
    wrote on last edited by
    #1

    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)
    
    1 Reply Last reply
    0
    • G Offline
      G Offline
      guoran188
      wrote on last edited by
      #2

      Thanks. But how to release the memory after using? like, clear the E_field and release the memory it occupies. I tried to open a new project from s4l_v1.document, but it does not work.

      1 Reply Last reply
      0
      • SylvainS Offline
        SylvainS Offline
        Sylvain
        ZMT
        wrote on last edited by
        #3

        have you tried overwriting the E_field and raw_data variables?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Search