Python Forum
How to use subprocess to get multiple data outputs in desired folder?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use subprocess to get multiple data outputs in desired folder?
#1
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)
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas: how to split one row of data to multiple rows and columns in Python GerardMoussendo 4 279 Feb-22-2021, 06:51 PM
Last Post: eddywinch82
  How to filter out Column data From Multiple rows data? firaki12345 10 499 Feb-06-2021, 04:54 AM
Last Post: buran
  ONE input => THREE outputs Tricia279 6 283 Jan-14-2021, 08:52 AM
Last Post: perfringo
  Fetching data from multiple tables in a single request. swaroop 0 152 Jan-09-2021, 04:23 PM
Last Post: swaroop
  How to fill parameter with data from multiple columns CSV file greenpine 1 176 Dec-21-2020, 06:50 PM
Last Post: Larz60+
  Load data from One oracle Table to Multiple tables amy83 1 216 Dec-02-2020, 01:57 AM
Last Post: Larz60+
  Multi set string inputs/outputs kwmcgreal 2 363 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  Why this code not getting desired output ? MDRI 2 743 Sep-18-2020, 02:11 AM
Last Post: MDRI
  Python Cut/Copy paste file from folder to another folder rdDrp 4 702 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  data from multiple Entry widgets beevee 13 925 Aug-16-2020, 12:31 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020