Sep-21-2020, 09:17 AM
(Sep-20-2020, 03:00 AM)yaythomas Wrote:(Sep-19-2020, 12:21 PM)3SG14 Wrote: My code in pyhthon right now has five for loops nested and its making me very difficult to store and manage output files according to number of simulation.
Can pypyr help?
Hi @3SG14,
Yes, pypyr can certainly help. I saw your other post where you describe your issue in greater detail. I don't know exactly what you're trying to achieve, but the sample code you gave would look something like this in a pypyr pipeline:
# trnsys-simulate.yaml steps: - name: pypyr.steps.default comment: set parameters to be evaluated in: defaults: label: 1 # Volume of seasonal storage tank volume_iss: ['1', '2', '3'] # Height of seasonal storage tank height_iss: ['4', '5', '6'] # Volume of DH tank volume_dhw: ['6', '8'] # Height of DH tank height_dhw: ['9'] # Area of flat plate solar collector area_sc: ['10', '11', '12'] - name: pypyr.steps.py comment: create the cartesian product of all input iterables save result to parameter_matrix, which the next step will loop through. in: pycode: | from itertools import product context['parameter_matrix'] = list(product( context['volume_iss'], context['height_iss'], context['volume_dhw'], context['height_dhw'], context['area_sc'])) - name: pypyr.steps.call comment: loop through the input combinations and run simulation for each foreach: '{parameter_matrix}' in: call: simulate simulate: - name: pypyr.steps.contextsetf comment: create unique input file name for this iteration in: contextSetf: out_dir: "./out/{label:04d}" input_file: "{out_dir}/IRLPHv00.dck" label: !py label + 1 - name: pypyr.steps.filereplace comment: create input file from template, by replacing tags w parameter values i is the iterator from the foreach in: fileReplace: in: z_PH_TEMPLATE.dck out: '{input_file}' replacePairs: PH_VolumeISS: '{i[0]}' PH_HeightISS: '{i[1]}' PH_VolumeDHW: '{i[2]}' PH_HeightDHW: '{i[3]}' PH_AreaSC: '{i[4]}' - name: pypyr.steps.cmd comment: run trnsys cmd with the input file created by previous step in: cmd: C:/TRNSYS18/Exe\TrnEXE64.exe "{input_file}" /h - name: pypyr.steps.cmd comment: move or copy outfiles to outdir using your preferred console cmd in: cmd: cp *.out {out_dir}/If you save this file as trnsys-simulate.yaml, you will run it from the same directory like this:
This will create in the out_dir directories like this:
Output:pypyr trnsys-simulate
out/0001/
out/0002/
out/0003/
It will also copy the input file (the dck file for that particular combination of parameters) so you can double-check everything as expected.
WARNING: I don't have trnsys on my system, nor do I know exactly what you're trying to achieve, and also I am not on a windows machine: I did test the above on linux/macOS, but I'm pretty sure you're going to have to fiddle with the out_dir, input_file and so on to make it appropriate for windows. So please don't expect it just to work first time after a copy/paste!
![Angel Angel](https://python-forum.io/images/smilies/angel.png)
![Angel Angel](https://python-forum.io/images/smilies/angel.png)
Thank you so much for such a detailed solution... I am giving the last try to my approach now. If I am not able to find what I want I will try to copy your code and modify according to windows if required (will let you how it worked if I use it). Thanks for such a prompt reply. Appreciate it