Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter sweep #23

Closed
phigas opened this issue Feb 22, 2024 · 1 comment
Closed

Parameter sweep #23

phigas opened this issue Feb 22, 2024 · 1 comment

Comments

@phigas
Copy link
Contributor

phigas commented Feb 22, 2024

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
Copy link
Owner

anufrievroman commented Feb 25, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants