VasiliBaranov / packing-generation

Hard-sphere packing generation in C++ with the Lubachevsky–Stillinger, Jodrey–Tory, and force-biased algorithms and packing post-processing.
MIT License
106 stars 43 forks source link

How to target a single `generation.conf` file without copying `PackingGeneration.exe` to unique folders #34

Closed sgbaird closed 1 year ago

sgbaird commented 1 year ago

i.e. I want to write a function:

def run_simulation(...):
   ...

that uses subprocess's run command, but the filenames generation.conf, diameters.txt, etc. are hardcoded. Is there a built-in way to specify the path of a configuration file and bypass the current behavior of running for any and all generation.conf-s contained in the current directory and subdirectories?

This is important to me from an optimization standpoint, where a single worker needs to be able to run a specific suggested configuration.

VasiliBaranov commented 1 year ago

Hi @sgbaird ,

i think the easiest way is (i.e. without changing the code) is to use OS capabilities. You can specify the "working directory" for a current run of the program. I.e. your script can be located in a folder A, the packing_generation.exe can be located in a folder B, but the script can ask the OS to run the program in a folder C.

This is the relevant stackoverflow answer: https://stackoverflow.com/a/34539434

And one more SO link: https://stackoverflow.com/questions/38862496/running-executable-in-a-different-directory-in-python

(i googled "python call exe with arguments another working directory")

Python documentation seems to be a bit cryptic here: https://docs.python.org/3/library/subprocess.html Maybe there is a better page, though.

Hope this helps!

Best Regards, Vasili

sgbaird commented 1 year ago

Thanks! the cwd kwarg within subprocess looks like the right option. I think I use subprocess.run in one example, and it seems like there may be a cwd kwarg for that.