Skip to content

How can I run the following python function (which is based on subprocess.run) on multiple processors? #1310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
amirrezaheidari opened this issue Aug 2, 2021 · 1 comment

Comments

@amirrezaheidari
Copy link

I am using the following function that is developed for running TRNSYS software from Python. I was I need to run this function thousands of times in each simulation, and it takes 22 hours for me to run one simulation. I am now trying to run this function in parallel but did not have any success so far. I am not very familiar with parallel computation. Can anybody show me on the code how would you run this function to use multiple cores and speed up running?

The code is first replacing the desired inputs into the input file, then uses subprocess.run to run the TRNSYS model with desired inputs, and then saves the outputs into a text file. I guess (not sure) I just need to somehow convert this "subprocess.run" into a parallel one...I hope someone can show how I can do it. If there is no easy way to run it in parallel, any suggestions how to speed it up?

def run(trnsys_inputs):
          
     #################################################################################################################    
 
     label_no=0
     
     demand = trnsys_inputs.demand #Liters
     Tamb = trnsys_inputs.Tamb #C
     signal_valve = trnsys_inputs.signal_valve 
     Ttank_dhw_start = trnsys_inputs.Ttank_dhw_start #C
     Ttank_sh_start=trnsys_inputs.Ttank_sh_start
     building_area = trnsys_inputs.building_area #m2 "pyt_building_area"  
     floor_area=trnsys_inputs.floor_area #m2
     building_Tinitial = trnsys_inputs.building_Tinitial #C
     signal_hp = trnsys_inputs.signal_hp 
     Twater_network = trnsys_inputs.Twater_network #C
     beam_for_surface = trnsys_inputs.beam_for_surface #kJ/hr.m2
     sky_for_surface = trnsys_inputs.sky_for_surface #kJ/hr.m2
     ground_for_surface = trnsys_inputs.ground_for_surface #kJ/hr.m2
     angle_of_incidence = trnsys_inputs.angle_of_incidence #kJ/hr.m2
     building_volume = building_area*2.4 #m3
     signal_sh_pump=trnsys_inputs.signal_sh_pump
     
     
     #################################inputs calculated based on building area
     
     system_sizes=size_system(building_area, floor_area)
     
     hp_rated_heating = system_sizes["hp_rated_heating"] #kJ/h
     hp_compressor_power = system_sizes["hp_compressor_power"] #kJ/h
     hp_sensible_cooling = system_sizes["hp_sensible_cooling"] #kJ/h
     hp_total_cooling = system_sizes["hp_total_cooling"] #kJ/h
     hp_airflow = system_sizes["hp_airflow"] #l/s
     dhw_tank_size = system_sizes["dhw_tank_size"] #m3
     radiator_power = system_sizes["radiator_power"] #kJ/h
     pv_area = int(system_sizes["pv_area"]) #m2
     
     ######################################################################################################################
     
     # 1) Assigning parameter values as input in the TRNSYS (.dck) file
 
     #  - opening dublicated template .dck file (parameter values changed to pyt tags via NotePad for the first time i.e. pyt_t_on in this template z_pyt_TEMPLATE.dck)
     with open(r"C:\Base control\Base_control - Copy.dck", 'r') as file_in:
         filedata = file_in.read()
         
     #  - changing/replacing the pyt tags to parameter values in the .dck text
     
     filedata = filedata.replace("pyt_demand", str(demand)) 
     filedata = filedata.replace("pyt_Tamb" , str(Tamb))
     filedata = filedata.replace("pyt_signal_valve" , str(signal_valve))
     filedata = filedata.replace("pyt_signal_sh_pump" , str(signal_sh_pump))
     filedata = filedata.replace("pyt_Ttank_dhw_start" , str(Ttank_dhw_start))
     filedata = filedata.replace("pyt_Ttank_sh_start" , str(Ttank_sh_start))
     filedata = filedata.replace("pyt_hp_total_cooling", str(hp_total_cooling))
     filedata = filedata.replace("pyt_hp_sensible_cooling", str(hp_sensible_cooling))
     filedata = filedata.replace("pyt_hp_compressor_power", str(hp_compressor_power))
     filedata = filedata.replace("pyt_hp_rated_heating", str(hp_rated_heating))
     filedata = filedata.replace("pyt_hp_airflow", str(hp_airflow))
     filedata = filedata.replace("pyt_dhw_tank_size", str(dhw_tank_size))
     filedata = filedata.replace("pyt_radiator_power", str(radiator_power))
     filedata = filedata.replace("pyt_building_area", str(building_area))
     filedata = filedata.replace("pyt_pv_area", str(pv_area))  
     filedata = filedata.replace("pyt_building_volume", str(building_volume))
     filedata = filedata.replace("pyt_building_Tinitial", str(building_Tinitial))
     filedata = filedata.replace("pyt_signal_hp", str(signal_hp))
     filedata = filedata.replace("pyt_Twater_network", str(Twater_network))
     filedata = filedata.replace("pyt_beam_for_surface", str(beam_for_surface))
     filedata = filedata.replace("pyt_sky_for_surface", str(sky_for_surface))
     filedata = filedata.replace("pyt_ground_for_surface", str(ground_for_surface))
     filedata = filedata.replace("pyt_angle_of_incidence", str(angle_of_incidence))
  
     
     #  - (over)writing the modified template .dck file to the original .dck file (to be run by TRNSYS) 
     with open(r"C:\Base control\Base_control.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:\Base control\Base_control.dck","/h"])
     elapsed_time = time.time() - start_time # Measuring time (end point)
     
     # 3) Generating the output .txt file name for each of the simulation results (i.e. first one as 001.txt)
     label_no+=1
     t=str(label_no)
     filename_out=t.rjust(3, '0')+'.txt'
     
     shutil.copy("C:\Base control\Outputs.txt", filename_out)
     
     results=pd.read_csv("C:\Base control\Outputs.txt", sep="\t")
     
     outputs={
     
     "hp_power":results["hp_power                 "][1]/3600 ,
     "hp_cop":results["hp_cop                   "][1],
     "hp_tout":results["hp_tout                  "][1],
     "hp_heattowater":results["hp_heattowater           "][1]/3600 ,
     "valve_mout1":results["valve_mout1              "][1],
     "valve_tout1":results["valve_tout1              "][1],
     "valve_mout2":results["valve_mout2              "][1],
     "valve_tout2":results["valve_tout2              "][1],
     "radiator_heat":results["radiator_heat            "][1]/3600 ,
     "radiator_tout":results["radiator_tout            "][1],
     "building_temp":results["building_temp            "][1],
     "building_humidity":results["building_humidity        "][1],
     "tank_dhw_tavg":results["tank_dhw_tavg            "][1],
     "tank_dhw_tout":results["tank_dhw_tout            "][1],
     "tank_dhw_touthx":results["tank_dhw_touthx          "][1],
     "tank_sh_tavg":results["tank_sh_tavg             "][1],
     "tank_sh_tout":results["tank_sh_tout             "][1],
     "tank_sh_touthx":results["tank_sh_touthx           "][1],
     "mixer_tout":results["mixer_tout               "][1],
     "mixer_mout":results["mixer_mout               "][1],
     "pump_sh_tout":results["pump_sh_tout             "][1],
     "pump_sh_mout":results["pump_sh_mout             "][1],
     "pump_sh_power":results["pump_sh_power            "][1]/3600,
     "pv_volt":results["pv_volt                  "][1],
     "pv_current":results["pv_current               "][1],
     "pv_power":results["pv_power                 "][1]/1000 ,
     "pv_efficiency":results["pv_efficiency            "][1]  
         
     }
 
     
     #outputs units: power (kW), Ttank (C)
     
     return outputs 
@Byron
Copy link
Member

Byron commented Aug 2, 2021

Closing this issue as it appears unrelated to GitPython.

@Byron Byron closed this as completed Aug 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants