Skip to content

Commit

Permalink
code adjustments to internal sim.clearAll(), added sim.close() (#832)
Browse files Browse the repository at this point in the history
note, there is new #TODO to check references in sim.clearAll
calling sim.close() will run pc.barrier() then terminate with sys.exit()
  • Loading branch information
jchen6727 authored Sep 11, 2024
1 parent 2934100 commit e52e780
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
1 change: 1 addition & 0 deletions netpyne/sim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
_init_stim_randomizer,
unique,
checkMemory,
close,
)

# import utils functions to manipulate objects
Expand Down
97 changes: 53 additions & 44 deletions netpyne/sim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from time import time
import hashlib
import array
import sys
from numbers import Number
from collections import OrderedDict
from neuron import h # Import NEURON
Expand Down Expand Up @@ -914,7 +915,8 @@ def clearAll():

# clean up
sim.pc.barrier()
sim.pc.gid_clear() # clear previous gid settings
#TODO are the references within containers cleared before the containers are?


# clean cells and simData in all nodes
if hasattr(sim, 'net'):
Expand Down Expand Up @@ -963,54 +965,61 @@ def clearAll():
matplotlib.pyplot.clf()
matplotlib.pyplot.close('all')

# clean rxd components
if hasattr(sim.net, 'rxd'):

sim.clearObj(sim.net.rxd)

if 'rxd' not in globals():
try:
from neuron import crxd as rxd
except:
pass
# try:
for r in rxd.rxd._all_reactions[:]:
if r():
rxd.rxd._unregister_reaction(r)

for s in rxd.species._all_species:
if s():
s().__del__()

rxd.region._all_regions = []
rxd.region._region_count = 0
rxd.region._c_region_lookup = None
rxd.species._species_counts = 0
rxd.section1d._purge_cptrs()
rxd.initializer.has_initialized = False
rxd.rxd.free_conc_ptrs()
rxd.rxd.free_curr_ptrs()
rxd.rxd.rxd_include_node_flux1D(0, None, None, None)
rxd.species._has_1d = False
rxd.species._has_3d = False
rxd.rxd._zero_volume_indices = np.ndarray(0, dtype=np.int_)
rxd.set_solve_type(dimension=1)
# clear reactions in case next sim does not use rxd
rxd.rxd.clear_rates()

for obj in rxd.__dict__:
sim.clearObj(obj)

# except:
# pass

if hasattr(sim, 'net'):
if hasattr(sim.net, 'rxd'): # check that 'net' exists before checking sim.net.rxd
sim.clearObj(sim.net.rxd)
# clean rxd components
if 'rxd' not in globals():
try:
from neuron import crxd as rxd
except:
pass
# try:
for r in rxd.rxd._all_reactions[:]:
if r():
rxd.rxd._unregister_reaction(r)

for s in rxd.species._all_species:
if s():
s().__del__()

rxd.region._all_regions = []
rxd.region._region_count = 0
rxd.region._c_region_lookup = None
rxd.species._species_counts = 0
rxd.section1d._purge_cptrs()
rxd.initializer.has_initialized = False
rxd.rxd.free_conc_ptrs()
rxd.rxd.free_curr_ptrs()
rxd.rxd.rxd_include_node_flux1D(0, None, None, None)
rxd.species._has_1d = False
rxd.species._has_3d = False
rxd.rxd._zero_volume_indices = np.ndarray(0, dtype=np.int_)
rxd.set_solve_type(dimension=1)
# clear reactions in case next sim does not use rxd
rxd.rxd.clear_rates()

for obj in rxd.__dict__:
sim.clearObj(obj)
del sim.net

import gc

gc.collect()

sim.pc.barrier()
sim.pc.gid_clear() # clear previous gid settings

def close(clear=True):
"""
Function to close simulation
"""
from .. import sim
if clear:
clearAll()
else:
sim.pc.barrier()
sys.exit()


def checkConditions(conditions, against, cellGid=None):

Expand Down

0 comments on commit e52e780

Please sign in to comment.