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
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 466 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  format json outputs ! evilcode1 3 1,692 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  Formatting outputs created with .join command klairel 2 594 Aug-23-2023, 08:52 AM
Last Post: perfringo
  Json filter is not capturing desired key/element mrapple2020 1 1,073 Nov-24-2022, 09:22 AM
Last Post: ibreeden
  Load multiple Jason data in one Data Frame vijays3 6 1,500 Aug-12-2022, 05:17 PM
Last Post: vijays3
  I have written a program that outputs data based on GPS signal kalle 1 1,134 Jul-22-2022, 12:10 AM
Last Post: mcmxl22
  Why does absence of print command outputs quotes in function? Mark17 2 1,342 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,528 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  how can I display only desired items? 3lnyn0 5 1,975 Dec-25-2021, 06:49 PM
Last Post: menator01
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,395 Dec-18-2021, 09:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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