Skip to content

Commit

Permalink
cluster config
Browse files Browse the repository at this point in the history
  • Loading branch information
Ombrini committed Jun 28, 2023
1 parent cd4a507 commit a74c044
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 54 deletions.
106 changes: 61 additions & 45 deletions bin/Automatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ def run_params_mpet(config_file, material_file, system_properties, material_prop
cfg_sys.optionxform = str
cfg_sys.read(config_file)
combinations_system = list(itertools.product(*val_system))
num_sys = len(combinations_system)

keys_mat, val_mat = ensemble_definitions(material_properties)
cfg_mat = configparser.ConfigParser()
cfg_mat.optionxform = str
cfg_mat.read(material_file)
combinations_material = list(itertools.product(*val_mat))

num_mat = len(combinations_material)
ind = 0
for comb_mat in combinations_material:
param_mat = dict(zip(keys_mat, comb_mat))
new_mat = cfg_mat
Expand All @@ -51,6 +53,7 @@ def run_params_mpet(config_file, material_file, system_properties, material_prop
nicename_mat.append(key[1] + "=" + val)
with open(material_file, "w") as f:
new_mat.write(f)

for combin_sys in combinations_system:
params_sys = dict(zip(keys_system, combin_sys))
new_sys = cfg_sys
Expand Down Expand Up @@ -83,61 +86,74 @@ def run_params_mpet(config_file, material_file, system_properties, material_prop
os.rename(last_folder, os.path.join(cwd, output_folder, new_folder_name))
os.chdir(cwd)
os.chdir(r".\configs")
ind += 1
print("Simulation " + str(ind) + " of " + str(num_mat*num_sys) + " completed")

# NMC study
# system_properties = [
# [("Conductivity", "sigma_s_c"), ["0.06","0.05", "0.04"]],
# [("Sim Params","Crate"), ["-0.206"]],
# [("Electrodes", "Rfilm_foil"), ["0"]],
# [("Electrodes", "k0_foil"), ["4","3","2"]],
# ]

# material_properties = [
# [("Reactions", 'k0'), ["25","20","15"]],
# ]

# output_folder = "NMC_thick_conv_8032522"
# config_file = 'params_system_NMC_Mark.cfg'
# material_file = 'params_NMC_Chen2020.cfg'

# LFP study
# diffusion more or less 1e-15
# k0 between 40 and 70
# sigma_c at least 0.05, small influence
# system_properties = [
# [("Sim Params","segments"), ["[(-1,60)]","[(-3,20)]"]],
# [("Conductivity","sigma_s_c"), ["0.1","0.075", "0.05"]],
# # [("Conductivity","G_mean_c"), ["0.5e-10"]],
# # [("Conductivity","sigma_s_c"), ["1"]],
# # [("Electrodes", "k0_foil"), ["20"]],
# # [("Sim Params","Crate"), ["-5","-3"]],
# # [("Particles","mean_c"), ["20e-9","50e-9","100e-9","300e-9"]],
# # [("Particles","stddev_c"), ["35e-9"]],
# # [("Geometry","BruggExp_c"), ["-1.85"]],
# [("Sim Params","segments"), [
# # "[(-0.2,135), (0,60), (-3,10)]",
# "[(-1,27), (0,60), (-3,10)]",
# # "[(-3,9), (0,60), (-3,10)]",
# # "[(-5,5.4), (0,60), (-3,10)]",
# ]],
# ]

# material_properties = [
# [("Reactions", 'k0'), ["40", "50", "70"]],
# [("Material", 'D'), ["2e-15","0.5e-15", "1e-15", "3e-15"]],
# # [("Reactions", 'k0'), ["30"]],
# # [("Material", 'dgammadc'), ["0e-29"]],
# # [("Material", 'kappa'), ["2.5148e-10"]],
# # [("Material", 'D'), ["3.49e-15"]],
# # [("Material", 'muRfunc'), ["LiFePO4_meta"]],
# # [("Material", 'noise'), ["false"]],
# # [("Material", 'noise_prefac'), ["1e-3"]],
# # [("Material", 'size_dep_D'), ["true"]],
# [("Material", 'B'), ["0.001e9"]],
# ]

# output_folder = "LFP_test_more_precise"
# config_file = 'params_system_LFP_test.cfg'
# material_file = 'params_LFP_CHR.cfg'
# k0_foil is 14
# k0 is 50
# sigma_s_c = 0.1
# polarization curve


# output_folder = "LFP_memory_best3C_1C"
# config_file = 'params_system_LFP_memory.cfg'
# material_file = 'params_LFP_CHR_memory.cfg'

system_properties = [
# [("Conductivity","sigma_s_c"), ["0.1"]],
[("Electrodes", "k0_foil"), ["15"]],
[("Sim Params","segments"), ["[(-1,60)]"]],
[("Conductivity","G_mean_c"), ["10e-12"]],
# [("Conductivity","G_stddev_c", ["2e-13"]],
# [("Conductivity","sigma_s_c"), ["2"]],
# [("Electrodes", "k0_foil"), ["20"]],
# [("Sim Params","Crate"), ["-3"]],
# [("Particles","mean_c"), ["40e-9"]],
# [("Particles","stddev_c"), ["50e-9"]],
[("Geometry","BruggExp_c"), ["-2.5"]],
[("Sim Params","segments"), [
"[(-5,5.4),(0,60),(-3,10)]",
# "[(-3,9),(0,60),(-3,10)]",
# "[(-0.2,135),(0,60),(-3,10)]",
]],
]

material_properties = [
# [("Reactions", 'k0'), ["25"]],
# [("Material", 'kappa'), ["20.0148e-10", "5.0148e-10"]],
[("Material", 'D'), ["5e-14"]],
[("Reactions", 'k0'), ["5"]],
# [("Material", 'dgammadc'), ["0e-29"]],
# [("Material", 'kappa'), ["5e-10"]],
[("Material", 'D_surf'), ["1e-18"]],
# [("Material", 'muRfunc'), ["LiFePO4_meta"]],
# [("Material", 'noise'), ["false"]],
# [("Material", 'noise_prefac'), ["1e-3"]],
# [("Material", 'size_dep_D'), ["true"]],
[("Material", 'B'), ["0.01e9"]],
[("Particles", 'thickness'), ["50e-9"]],
]

output_folder = "LFP_memory10"
config_file = 'params_system_LFP.cfg'
material_file = 'params_LFP_CHR.cfg'
output_folder = "LFP_ACR_noisek09_mem15"
config_file = 'params_system_LFP_Galuppini.cfg'
material_file = 'params_LFP_Galuppini.cfg'



Expand Down
126 changes: 126 additions & 0 deletions bin/run_jobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/usr/bin/env python3

import argparse
import os
import shutil
import mpet.main as main

from dask_jobqueue import SLURMCluster, PBSCluster
from dask.distributed import Client, LocalCluster


def create_slurm_cluster(time, nproc, mem, queue, dashboard_port):
"""Create a SLURM cluster for use with dask"""
cluster = SLURMCluster(cores=nproc,
processes=nproc,
memory=mem,
queue=queue,
walltime=time,
scheduler_options={'dashboard_address': dashboard_port})
return cluster


def create_pbs_cluster(time, nproc, mem, queue, dashboard_port):
"""Create a PBS cluster for use with dask"""
cluster = PBSCluster(cores=nproc,
processes=nproc,
memory=mem,
resource_spec=f'nodes=1:ppn={nproc}',
queue=queue,
walltime=time,
scheduler_options={'dashboard_address': dashboard_port})
return cluster


def create_local_cluster(mem, dashboard_port):
"""Create a local cluster for use with dask"""
cluster = LocalCluster(memory_limit=mem,
dashboard_address=dashboard_port)
return cluster


def run_mpet(client, output_folder, mpet_configs):
"""Run MPET on each config file present in the mpet_configs folder"""
# In order to copy or move simulation output to current directory, remove old existing folder
tmpDir = os.path.join(os.getcwd(), "sim_output")
shutil.rmtree(tmpDir, ignore_errors=True)

with open(mpet_configs, 'r') as fp:
config_files = fp.readlines()

folder = os.path.dirname(mpet_configs)
files = [f'{os.path.join(folder, fname.strip())}' for fname in config_files]
print('Running mpet for these config files:', files)

# function to run MPET in directory given as input to run_mpet
def run_mpet_instance(*args, **kwargs):
print(f"Running in {output_folder} with {args=} and {kwargs=}")
os.chdir(output_folder)
try:
main.main(*args, **kwargs)
except Exception as e:
print(f'MPET crashed with error: {e}')

futures = client.map(run_mpet_instance, files, keepFullRun=True)
print('Waiting for MPET to finish')
client.gather(futures)
client.close()
print('Done')

return


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Runs several instances of MPET on a SLURM or'
' PBS cluster, or local cluster')
# cluster settings
parser.add_argument('--time', '-t', required=True,
help='Maximum walltime per job (hh:mm:ss format)')
parser.add_argument('--nproc', '-n', type=int, required=True,
help='Number of CPU cores per job')
parser.add_argument('--mem', '-m', required=True,
help=('Max memory usage per job. When using a '
'local cluster it sets the memory limit per worker process.'))
parser.add_argument('--queue', '-q', default='default',
help='Queue to use (default: %(default)s)')
parser.add_argument('--dashboard_port', '-d', type=int, default=4096,
help='Port for dask dashboard (default: %(default)s)')

# script settings
parser.add_argument('--scheduler', '-s', default='slurm', choices=('slurm', 'pbs', 'local'),
help='Scheduling system to use (default: %(default)s)')
parser.add_argument('--min_jobs', type=int, default=1,
help='Minimum number of jobs to launch (default: %(default)s)')
parser.add_argument('--max_jobs', type=int, default=1,
help='Maximum number of jobs to launch (default: %(default)s)')
parser.add_argument('mpet_configs',
help='Text file containg the path to each MPET config file to run')

args = parser.parse_args()
# split cluster settings from the rest
main_settings = {}
for arg in ['scheduler', 'mpet_configs', 'min_jobs', 'max_jobs']:
main_settings[arg] = getattr(args, arg)
delattr(args, arg)
cluster_settings = vars(args)

# create cluster
if main_settings['scheduler'] == 'slurm':
cluster = create_slurm_cluster(**cluster_settings)
elif main_settings['scheduler'] == 'pbs':
cluster = create_pbs_cluster(**cluster_settings)
elif main_settings['scheduler'] == 'local':
cluster = create_local_cluster(args.mem, args.dashboard_port)

# Scale Dask cluster automatically based on scheduler activity (only if not local cluster)
if main_settings['scheduler'] != 'local':
cluster.adapt(minimum_jobs=main_settings['min_jobs'],
maximum_jobs=main_settings['max_jobs'])
client = Client(cluster)

# Store output in folder this script was called from
output_folder = os.getcwd()

run_mpet(client, output_folder, os.path.abspath(main_settings['mpet_configs']))

client.shutdown()
13 changes: 5 additions & 8 deletions configs/params_LFP_CHR.cfg
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# Default parameters for simulating LFP in 1D using the ACR model.
# See params_electrodes.cfg for parameter explanations.

[Particles]
type = CHR
discretization = 1e-9
discretization = 2.5e-9
shape = sphere
thickness = 20e-9

Expand All @@ -13,19 +10,19 @@ noise = false
noise_prefac = 1e-6
numnoise = 200
Omega_a = 1.8560e-20
kappa = 5.0148e-10
kappa = 20.0148e-10
B = 0.01e9
rho_s = 1.3793e28
D = 5e-14
D = 2e-15
Dfunc = lattice
dgammadc = 0e-30
cwet = 0.98

[Reactions]
rxnType = CIET
k0 = 60
k0 = 50
E_A = 13000
alpha = 0.5
# Fraggedakis et al. 2020, lambda = 8.3kBT
lambda = 3.4113e-20
Rfilm = 0e-0

28 changes: 28 additions & 0 deletions configs/params_LFP_CHR_memory.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[Particles]
type = CHR
discretization = 0.5e-9
shape = sphere
thickness = 20e-9

[Material]
muRfunc = LiFePO4
noise = false
noise_prefac = 1e-5
numnoise = 200
Omega_a = 1.8560e-20
kappa = 2.0148e-10
B = 0.001e9
rho_s = 1.3793e28
D = 3.5e-15
Dfunc = lattice
dgammadc = 0e-30
cwet = 0.98

[Reactions]
rxnType = CIET
k0 = 30
E_A = 13000
alpha = 0.5
lambda = 3.4113e-20
Rfilm = 0e-0

3 changes: 2 additions & 1 deletion mpet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def run_simulation(config, outdir):

# Solve at time=0 (initialization)
# Increase the number of Newton iterations for more robust initialization
dae.daeGetConfig().SetString("daetools.IDAS.MaxNumItersIC","100")
dae.daeGetConfig().SetString("daetools.IDAS.MaxNumItersIC","500")
dae.daeGetConfig().SetString("daetools.IDAS.MaxNumSteps","100000")
simulation.SolveInitial()

# Run
Expand Down

0 comments on commit a74c044

Please sign in to comment.