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
  Load data from One oracle Table to Multiple tables amy83 1 36 4 hours ago
Last Post: Larz60+
  Multi set string inputs/outputs kwmcgreal 2 254 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  Why this code not getting desired output ? MDRI 2 622 Sep-18-2020, 02:11 AM
Last Post: MDRI
  Python Cut/Copy paste file from folder to another folder rdDrp 4 452 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  data from multiple Entry widgets beevee 13 736 Aug-16-2020, 12:31 PM
Last Post: deanhystad
  showing only desired part of a plot grknkilicaslan 1 248 Jul-10-2020, 03:51 PM
Last Post: Marbelous
  How to read multiple csv files and merge data rajeshE 0 384 Mar-28-2020, 04:01 PM
Last Post: rajeshE
  [Beginner] Code is not producing desired result fakej171 2 554 Mar-21-2020, 10:26 AM
Last Post: buran
  Outputs missing SamAnw 4 464 Feb-12-2020, 04:32 PM
Last Post: adetheheat
  Not Getting the Desired value using json.dumps saurabh210 0 431 Feb-03-2020, 06:48 PM
Last Post: saurabh210

Forum Jump:

User Panel Messages

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