Skip to content

Commit

Permalink
adding destructor
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasCouppey committed Jan 12, 2024
1 parent 2140092 commit 52ffcbb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
11 changes: 8 additions & 3 deletions nrv/backend/NRV_Class.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,26 @@ def is_NRV_dict_dict(x):
return True
return False


######################################
# numpy compatibility #
######################################


def is_empty_iterable(x):
"""
"""
""" """
if not np.iterable(x):
return False
if len(x) == 0:
return True
return False


######################################
# NRV Class #
######################################


class NRV_class(metaclass=ABCMeta):
"""
Instanciate a basic NRV class
Expand All @@ -114,6 +116,9 @@ def __del__(self):
Destructor for NRV class
"""
pass_debug_info(self.nrv_type, " deleted")
keys = list(self.__dict__.keys())
for key in keys:
del self.__dict__[key]

def save(self, save=False, fname="nrv_save.json", blacklist=[], **kwargs):
"""
Expand Down
20 changes: 16 additions & 4 deletions nrv/fmod/FEM/COMSOL_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ def __init__(self, fname, Ncore=None, handle_server=False):
t0 = time.time()
super().__init__(Ncore=Ncore)
self.type = "COMSOL"

self.model_path = fname
f_in_librairy = rmv_ext(str(fname)) + ".mph"
self.__has_client = False
self.__has_server = False

if f_in_librairy in material_library:
self.fname = dir_path + "/comsol_templates/" + f_in_librairy
else:
Expand All @@ -71,9 +73,11 @@ def __init__(self, fname, Ncore=None, handle_server=False):
pass_info("Starting COMSOL server/client, this may take few seconds")
if self.handle_server:
self.server = mph.Server(cores=self.Ncore)
self.__has_server = True
else:
self.server = None
self.client = mph.start(cores=self.Ncore)
self.__has_client = True
self.client.caching(True)
pass_info("... loading the COMSOL model")
self.model = self.client.load(self.fname)
Expand Down Expand Up @@ -105,11 +109,19 @@ def close(self):
"""
Close the FEM simulation and the COMSOL link
"""
# self.client.disconnect()
del self.client
if self.handle_server:
if self.__has_client:
self.client.disconnect()
del self.client
self.__has_client = False
print(self.handle_server or self.__has_server)
if self.handle_server or self.__has_server:
self.server.stop()
del self.server
self.__has_server = False

def __del__(self):
self.close()
super().__del__()

#############################
## Access model parameters ##
Expand Down
8 changes: 1 addition & 7 deletions nrv/fmod/extracellular.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,16 +432,10 @@ def __init__(
"Slave process abort as axon is supposed to used a COMSOL FEM and COMSOL turned off",
abort=True,
)
self.model = None
else:
self.model = FENICS_model(Ncore=self.Ncore)

def __del__(self):
if (
MCH.do_master_only_work() and COMSOL_Status and self.comsol
): # added for safe del in case of COMSOL status turned OFF
self.model.close()
del self.model

def set_Ncore(self, N):
"""
Set the number of cores to use for the FEM
Expand Down

0 comments on commit 52ffcbb

Please sign in to comment.