Breakthrough-Energy / SwitchWrapper

Wrapper for Switch
MIT License
1 stars 2 forks source link

feat: add call script #84

Closed danielolsen closed 3 years ago

danielolsen commented 3 years ago

Pull Request doc

Purpose

Add a script which can be used to launch Switch on a directory, with the appropriate default settings and input validation. Closes #52.

What the code is doing

In the launch_switch function, input parameters are translated to a list of commands to launch in the appropriate folder via subprocess.run(), and the current working directory is restored afterwards no matter what. There is an if __name__ == "__main__" block to allow command-line invocation.

Testing

Tested manually. Note: this relies on the executable named switch, installed as part of switch-model, to be findable with the Path variable in the environment you are running in.

Usage Example/Visuals

From the command line:

python3 call.py ~/switchwrapper_test/

If we try with a folder that does't have an inputs subdirectory:

python3 switchwrapper/call.py ~
Traceback (most recent call last):
  File "switchwrapper/call.py", line 55, in <module>
    launch_switch(sys.argv[1])
  File "switchwrapper/call.py", line 28, in launch_switch
    raise ValueError("input_folder must contain a subdirectory named 'inputs'")
ValueError: input_folder must contain a subdirectory named 'inputs'

From python:

from switchwrapper.call import launch_switch
launch_switch("~/switchwrapper_test")

Time estimate

5 minutes to understand the current code. @YifanLi86 & @danlivengood, if this is not the workflow you would prefer to use to launch simulations for now, please let me know. Eventually we will probably want to manage inputs/output files for a series of runs, and this will evolve, but that will be a subsequent evolution, not part of this PR.

danielolsen commented 3 years ago

Per conversations with @BainanXia and @rouille, I've refactored this so that we remove the os.chdir call and pass all directories explicitly to Switch. I also added a validation step to make sure that the required modules.txt file exists in the input directory. The call signature to our call.py scripts remains the same for now, but eventually if we have non-default directories for the input/output dirs, we can add these as inputs to call.py and we're more set up for passing these to the command-line call to Switch.

Tested successfully on the server.