Python Forum
How to use subprocess to get multiple data outputs in desired folder? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to use subprocess to get multiple data outputs in desired folder? (/thread-29778.html)



How to use subprocess to get multiple data outputs in desired folder? - 3SG14 - Sep-19-2020

HI,

I am using subprocess to automate my Trnsys simulation for multiple inputs.

The problem I am having is each run should produce multiple results which its not producing? it moves to next run after giving me the first output for one objective (leaves others without showing any error and moves on).

Another issue: I have is that I want to store the results in folders(being created by python)for each and numbered for that run. So that I can avoid sorting them at the end of the process (having to avoid doing it manually).

my code is like this:
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 18 21:49:09 2020

@author: 
"""

## This Python script runs automated TRNSYS simulation
#   prepared by Sachin Gangwar (PhD student, URV) from the references of Hakan İbrahim Tol and Len Rijvers

#  Libraries Imported
import subprocess           # to run the TRNSYS simulation
import shutil               # to duplicate the output txt file
import time                 # to measure the computation time
import numpy as np          # to measure the computation time
import os                   # to create directories for storing results
import sys

# List of Parameters to be Evaluated
list_VolumeISS =[1]         # Volume of seasonal storage tank
list_HeightISS =[1]                   # Height of seasonal storage tank
list_VolumeDHW =[0.5]  # Volume of DH tank
list_HeightDHW =[1]                   # Height of DH tank
list_AreaSC    =[1]         # Area of flat plate solar collector

label_no=0

#  Looping through Each of Combinations (List of Parameters)
for VolumeISS in list_VolumeISS:
    for HeightISS in list_HeightISS:
        for VolumeDHW in list_VolumeDHW:
            for HeightDHW in list_HeightDHW:
                for AreaSC in list_AreaSC:
                    # 1) Assigning parameter values as input in the TRNSYS (.dck) file
                    
                    
                    with open(r'C:\Users\Desktop\v0.0\z_PH_TEMPLATE.dck', 'r') as file_in:
                        filedata = file_in.read()
                        
                        #  - changing/replacing the py tags to parameter values in the .dck text
                        filedata = filedata.replace('PH_VolumeISS', str(VolumeISS))
                        filedata = filedata.replace('PH_HeightISS', str(HeightISS))
                        filedata = filedata.replace('PH_VolumeDHW', str(VolumeDHW))
                        filedata = filedata.replace('PH_HeightDHW', str(HeightDHW))
                        filedata = filedata.replace('PH_AreaSC', str(AreaSC))
                        
                        #  - (over)writing the modified template .dck file to the original .dck file (to be run by TRNSYS)
                        with open(r'C:\Users\Desktop\v0.0\IRLPHv00.dck', 'w') as dckfile_out:
                            dckfile_out.write(filedata)
                            
                            # 2) Running TRNSYS simulation
                            
                            start_time=time.time()                  # Measuring time (start point)
                            
                            subprocess.run([r"C:/TRNSYS18/Exe\TrnEXE64.exe",r"C:\Users\Desktop\v0.0\IRLPHv00.dck","/h"])
                            elapsed_time = time.time() - start_time # Measuring time (end point)
                            print(elapsed_time)

                            # 3) Generating the output .out file name for each of the simulation results (i.e. first one as 001.out)
                
                            # creating the path where the folder needs to be created and result to be stored
                            label_no+=1
                            t=str(label_no)
                            os.chdir(r'C:\Users\Downloads')
                            os.makedirs(results,float,1)                
                            Newfolder = t
                            filename_out=t.rjust(4,'0')+'.out'                # if .out doesnt work change to .txt
                                                                                  
                            shutil.copy('1economic.out',  filename_out)
                            shutil.copy(r'2environmental.out', filename_out)
                            shutil.copy(r'3Wsum.out', filename_out)
                            shutil.copy(r'4energyoutput.out', filename_out)
                            shutil.copy(r'5economcheck.out', filename_out)
                            shutil.copy(r'6environcheck.out', filename_out)
                            shutil.copy(r'7tempcheck.out', filename_out)



RE: How to use subprocess to get multiple data outputs in desired folder? - bowlofred - Sep-19-2020

You don't really need to keep the modified deck open. You just need to write it and then complete the writes so that other processes can read it. So doing all that work in the with on line 48 seems wrong. You should exit that context immediately.

I have no idea what is meant by the multiple results. Can you have it just try to do one and then debug if it's giving you the correct results? If it is not doing so, provide more details and we can suggest what might be wrong.