anufrievroman / freepaths

Monte Carlo simulator of phonon and heat transport in nanostructures
https://anufrievroman.gitbook.io/freepaths
Other
16 stars 4 forks source link

Parameter sweep #23

Open phigas opened 9 months ago

phigas commented 9 months ago

Do do a parameter sweep currently it is necessary to have a default config file and to customize it with some code and then run it automatically. Because the config file is copied into the results folder it is necessary to store it somewhere until the simulation ends. It could be useful to have a more elegant solution. Maybe something like this:

# define the parameters and values as a dict
PARAMETER_SWEEP_VALUES = {
    'T': (4, 50, 300),
    }

# Option between grid search and specified combinations like in comsol
PARAMETER_SWEEP_TYPE = 'grid search'

# check that the name contains the parameters to sweep for .format
OUTPUT_FOLDER_NAME = 'Simulation at T: {T}'

If a parameter sweep is specified the program would call a special script that would execute the different simulations

anufrievroman commented 9 months ago

To me, this shifts the burden of scripting from the user to the software. I also don't see a short way of implementing this, and imagine it to be not so widely used. So I'd rather just make a tutorial on how to make user scripts to run parametric sweeps than to implement something like this into the code. For example, this works for me:

"""Example script to run several simulations in which temperature is changed"""

import subprocess

FILENAME = "point_line.py"

def update_config_file(config_file, temperature):
    with open(config_file, 'r') as f:
        lines = f.readlines()

    for i, line in enumerate(lines):
        if line.startswith("OUTPUT_FOLDER_NAME"):
            lines[i] = f"OUTPUT_FOLDER_NAME = {temperature}\n"
        if line.startswith("T "):
            lines[i] = f"T = {temperature}\n"

    with open(config_file, 'w') as f:
        f.writelines(lines)

def main():

    # Define your list of temperatures here:
    temperatures = [100, 200, 300]

    for temp in temperatures:
        update_config_file(FILENAME, temp)
        subprocess.run(['python', '-m', 'freepaths', FILENAME])

if __name__ == "__main__":
    main()

p.s. the tutorial.