Measures/m_8_example_8_system_sizing_with_discrete_package_sizes/measure.rb
# define what happens when the measure is run
define what happens when the measure is run
define what happens when the measure is run
def run(model, runner, user_arguments)
    super(model, runner, user_arguments)
    if !runner.validateUserArguments(arguments(model), user_arguments)
      return false
    end
    verbose_info_statements = runner.getBoolArgumentValue("verbose_info_statements",user_arguments)
    air_loop_objects = runner.getOptionalWorkspaceObjectChoiceValue('air_loop_objects', user_arguments, model) 
    internal_variable_availability_dictionary_reporting = runner.getStringArgumentValue('internal_variable_availability_dictionary_reporting', user_arguments)
    ems_runtime_language_debug_output_level = runner.getStringArgumentValue('ems_runtime_language_debug_output_level', user_arguments)
    actuator_availability_dictionary_reporting = runner.getStringArgumentValue('actuator_availability_dictionary_reporting', user_arguments)
    runner.registerInitialCondition("Measure began with #{model.getEnergyManagementSystemSensors.count} EMS sensors, #{model.getEnergyManagementSystemActuators.count} EMS Actuators, #{model.getEnergyManagementSystemPrograms.count} EMS Programs, #{model.getEnergyManagementSystemSubroutines.count} EMS Subroutines, #{model.getEnergyManagementSystemProgramCallingManagers.count} EMS Program Calling Manager objects")
    counter = 0
    apply_to_all_airloops = false
    selected_airloop = nil
    if air_loop_objects.empty?
      handle = runner.getStringArgumentValue('air_loop_objects', user_arguments)
      if handle.empty?
        runner.registerError('No air loop was chosen.')
        return false
      else
        runner.registerError("The selected air loop with handle '#{handle}' was not found in the model. It may have been removed by another measure.")
        return false
      end
    else
      if !air_loop_objects.get.to_AirLoopHVAC.empty?
        selected_airloop = air_loop_objects.get.to_AirLoopHVAC.get
      elsif !air_loop_objects.get.to_Building.empty?
        apply_to_all_airloops = true
      else
        runner.registerError('Script Error - argument not showing up as an air loop.')
        return false
      end
    end
    selected_airloops = []
    show_loop = false
    if apply_to_all_airloops == true
      model.getAirLoopHVACs.each do |each_airloop|
        if each_airloop.designSupplyAirFlowRate.empty?
          show_loop = true
        end  
        if show_loop == true
           selected_airloops << each_airloop
        end
      end 
    else 
      selected_airloops << selected_airloop
    end
    selected_airloops.each do |selected_airloop|
      keep_loop = false
      supply_comps = selected_airloop.supplyComponents
      supply_comps.each do |supply_comp|
        if supply_comp.to_CoilCoolingDXSingleSpeed.is_initialized || supply_comp.to_CoilCoolingDXTwoSpeed.is_initialized || supply_comp.to_CoilCoolingDXVariableSpeed.is_initialized
          keep_loop = true
        end      
      end 
      if keep_loop == false
        if verbose_info_statements == true
          runner.registerInfo("Autosized Airloop named #{selected_airloop.name} did not contain a DX cooling coil and the airloop will not be modified by this measure.")
          selected_airloops.remove(selected_airloop)
        end
      else
        if verbose_info_statements == true
          runner.registerInfo("keeping airloop #{selected_airloop.name}.")
        end
      end      
    end
    if selected_airloops.length == 0
      runner.registerAsNotApplicable("Model contains no 'qualified' AirLoops for this measure to modify.") 
      return true
    end
    ems_subroutine_prg = nil
    ems_resize_psz_based_on_clg_airflow_prgm = nil
    ems_prgm_calling_mngr = nil
    ems_resize_psz_based_on_clg_airflow_prgm = OpenStudio::Model::EnergyManagementSystemProgram.new(model)
    ems_resize_psz_based_on_clg_airflow_prgm.setName("Resize PSZ To Match Product Availability")
    if verbose_info_statements == true
      runner.registerInfo("EMS Program object named '#{ems_resize_psz_based_on_clg_airflow_prgm.name}' added to resize airloop equipment based on autosized High Speed Airflow of Two Speed DX Clg Coil.") 
    end
    ems_prgm_calling_mngr = OpenStudio::Model::EnergyManagementSystemProgramCallingManager.new(model)
    ems_prgm_calling_mngr.setName("Apply Discrete Package Sizes to Air System Sizing")
    ems_prgm_calling_mngr.setCallingPoint("EndOfSystemSizing")
    ems_prgm_calling_mngr.addProgram(ems_resize_psz_based_on_clg_airflow_prgm)
    if verbose_info_statements == true
      runner.registerInfo("EMS Program Calling Manager object named '#{ems_prgm_calling_mngr.name}' added to call #{ems_resize_psz_based_on_clg_airflow_prgm.name} EMS program.") 
    end
    ems_arg_discrete_main_v_dot_gv = OpenStudio::Model::EnergyManagementSystemGlobalVariable.new(model, "argDiscreteMainVdot")
    if verbose_info_statements == true
      runner.registerInfo("EMS Global Variable named '#{ems_arg_discrete_main_v_dot_gv.name}' added to hold a discrete airflow value for equipment sizing added to model.") 
    end
    ems_arg_main_v_dot_gv = OpenStudio::Model::EnergyManagementSystemGlobalVariable.new(model, "argMainVdot")
    if verbose_info_statements == true
      runner.registerInfo("EMS Global Variable named '#{ems_arg_main_v_dot_gv.name}' added to hold a calculated airflow value for equipment sizing added to model.") 
    end
    ems_subroutine_prg = OpenStudio::Model::EnergyManagementSystemSubroutine.new(model)
    ems_subroutine_prg.setName("Select_Discrete_Nominal_Air_Flow")
    ems_subroutine_prg.addLine("IF (#{ems_arg_main_v_dot_gv.name} <= 0.56628)")
    ems_subroutine_prg.addLine("SET #{ems_arg_discrete_main_v_dot_gv.name} = 0.56628")
    ems_subroutine_prg.addLine("ELSEIF (#{ems_arg_main_v_dot_gv.name} > 0.56628) && (#{ems_arg_main_v_dot_gv.name} <= 0.75504)")
    ems_subroutine_prg.addLine("SET #{ems_arg_discrete_main_v_dot_gv.name} = 0.75504")
    ems_subroutine_prg.addLine("ELSEIF (#{ems_arg_main_v_dot_gv.name} > 0.75504) && (#{ems_arg_main_v_dot_gv.name} <= 0.9438 )")
    ems_subroutine_prg.addLine("SET #{ems_arg_discrete_main_v_dot_gv.name} = 0.9438")
    ems_subroutine_prg.addLine("ELSEIF (#{ems_arg_main_v_dot_gv.name} > 0.9438) && (#{ems_arg_main_v_dot_gv.name} <= 1.13256 )")
    ems_subroutine_prg.addLine("SET #{ems_arg_discrete_main_v_dot_gv.name} = 1.13256")
    ems_subroutine_prg.addLine("ELSEIF (#{ems_arg_main_v_dot_gv.name} > 1.13256) && (#{ems_arg_main_v_dot_gv.name} <= 1.4157 )")
    ems_subroutine_prg.addLine("SET #{ems_arg_discrete_main_v_dot_gv.name} = 1.4157")
    ems_subroutine_prg.addLine("ELSEIF (#{ems_arg_main_v_dot_gv.name} > 1.4157) && (#{ems_arg_main_v_dot_gv.name} <= 1.60446 )")
    ems_subroutine_prg.addLine("SET #{ems_arg_discrete_main_v_dot_gv.name} = 1.60446")
    ems_subroutine_prg.addLine("ELSEIF (#{ems_arg_main_v_dot_gv.name} > 1.60446) && (#{ems_arg_main_v_dot_gv.name} <= 1.8879 )")
    ems_subroutine_prg.addLine("SET #{ems_arg_discrete_main_v_dot_gv.name} = 1.8879")
    ems_subroutine_prg.addLine("ELSEIF (#{ems_arg_main_v_dot_gv.name} > 1.8879)")
    ems_subroutine_prg.addLine("SET dummy = @SevereWarnEP 666.0")
    ems_subroutine_prg.addLine("ENDIF")
    if verbose_info_statements == true
      runner.registerInfo("EMS Subroutine Program object named '#{ems_subroutine_prg.name}' added to reference main EMS program.") 
    end
    selected_airloops.each do |airloop|
      ems_internal_variable = OpenStudio::Model::EnergyManagementSystemInternalVariable.new(model, "Intermediate Air System Main Supply Volume Flow Rate")
      ems_internal_variable.setName("#{airloop.name}_CalcMainSupVdot".gsub("-","_"))
      ems_internal_variable.setInternalDataIndexKeyName("#{airloop.name}")  
      if verbose_info_statements == true
        runner.registerInfo("EMS Program Internal Variable object named '#{ems_internal_variable.name}' added to model.") 
      end
      ems_actuator = OpenStudio::Model::EnergyManagementSystemActuator.new(airloop,"Sizing:System","Main Supply Volume Flow Rate")
      ems_actuator.setName("#{airloop.name}MainSupVdotSet".gsub("-","_"))
      if verbose_info_statements == true
        runner.registerInfo("EMS Actuator Object named #{ems_actuator.name} representing the Main Supply Volume Flow Rate of the AirLoop named '#{airloop.name}. added to the model.") 
      end
      ems_resize_psz_based_on_clg_airflow_prgm.addLine("SET #{ems_arg_main_v_dot_gv.name} = #{ems_internal_variable.name}")
      ems_resize_psz_based_on_clg_airflow_prgm.addLine("RUN #{ems_subroutine_prg.name}")
      ems_resize_psz_based_on_clg_airflow_prgm.addLine("SET #{ems_actuator.name} = #{ems_arg_discrete_main_v_dot_gv.name}")
      if verbose_info_statements == true
        runner.registerInfo("Logic appended to the EMS Actuator program Object named '#{ems_resize_psz_based_on_clg_airflow_prgm.name}' calling the EMS subroutine program for the AirLoop object named '#{airloop.name}'.") 
      end
    end 
    output_EMS = model.getOutputEnergyManagementSystem
    output_EMS.setInternalVariableAvailabilityDictionaryReporting('internal_variable_availability_dictionary_reporting')
    output_EMS.setEMSRuntimeLanguageDebugOutputLevel('ems_runtime_language_debug_output_level')
    output_EMS.setActuatorAvailabilityDictionaryReporting('actuator_availability_dictionary_reporting')
    if verbose_info_statements == true
      runner.registerInfo("Output EMS Program Object configured for model.") 
    end
    runner.registerFinalCondition("Measure finished with #{model.getEnergyManagementSystemSensors.count} EMS sensors, #{model.getEnergyManagementSystemActuators.count} EMS Actuators, #{model.getEnergyManagementSystemPrograms.count} EMS Programs, #{model.getEnergyManagementSystemSubroutines.count} EMS Subroutines, #{model.getEnergyManagementSystemProgramCallingManagers.count} EMS Program Calling Manager objects")
  end
def run ( model , runner , user_arguments ) super ( model , runner , user_arguments ) if ! runner . validateUserArguments ( arguments ( model ) , user_arguments ) return false end verbose_info_statements = runner . getBoolArgumentValue ( "verbose_info_statements" , user_arguments ) air_loop_objects = runner . getOptionalWorkspaceObjectChoiceValue ( 'air_loop_objects' , user_arguments , model ) internal_variable_availability_dictionary_reporting = runner . getStringArgumentValue ( 'internal_variable_availability_dictionary_reporting' , user_arguments ) ems_runtime_language_debug_output_level = runner . getStringArgumentValue ( 'ems_runtime_language_debug_output_level' , user_arguments ) actuator_availability_dictionary_reporting = runner . getStringArgumentValue ( 'actuator_availability_dictionary_reporting' , user_arguments ) runner . registerInitialCondition ( "Measure began with #{model.getEnergyManagementSystemSensors.count} EMS sensors, #{model.getEnergyManagementSystemActuators.count} EMS Actuators, #{model.getEnergyManagementSystemPrograms.count} EMS Programs, #{model.getEnergyManagementSystemSubroutines.count} EMS Subroutines, #{model.getEnergyManagementSystemProgramCallingManagers.count} EMS Program Calling Manager objects" ) counter = 0 apply_to_all_airloops = false selected_airloop = nil if air_loop_objects . empty? handle = runner . getStringArgumentValue ( 'air_loop_objects' , user_arguments ) if handle . empty? runner . registerError ( 'No air loop was chosen.' ) return false else runner . registerError ( "The selected air loop with handle '#{handle}' was not found in the model. It may have been removed by another measure." ) return false end else if ! air_loop_objects . get . to_AirLoopHVAC . empty? selected_airloop = air_loop_objects . get . to_AirLoopHVAC . get elsif ! air_loop_objects . get . to_Building . empty? apply_to_all_airloops = true else runner . registerError ( 'Script Error - argument not showing up as an air loop.' ) return false end end selected_airloops = [ ] show_loop = false if apply_to_all_airloops == true model . getAirLoopHVACs . each do | each_airloop | if each_airloop . designSupplyAirFlowRate . empty? show_loop = true end if show_loop == true selected_airloops << each_airloop end end else selected_airloops << selected_airloop end selected_airloops . each do | selected_airloop | keep_loop = false supply_comps = selected_airloop . supplyComponents supply_comps . each do | supply_comp | if supply_comp . to_CoilCoolingDXSingleSpeed . is_initialized || supply_comp . to_CoilCoolingDXTwoSpeed . is_initialized || supply_comp . to_CoilCoolingDXVariableSpeed . is_initialized keep_loop = true end end if keep_loop == false if verbose_info_statements == true runner . registerInfo ( "Autosized Airloop named #{selected_airloop.name} did not contain a DX cooling coil and the airloop will not be modified by this measure." ) selected_airloops . remove ( selected_airloop ) end else if verbose_info_statements == true runner . registerInfo ( "keeping airloop #{selected_airloop.name}." ) end end end if selected_airloops . length == 0 runner . registerAsNotApplicable ( "Model contains no 'qualified' AirLoops for this measure to modify." ) return true end ems_subroutine_prg = nil ems_resize_psz_based_on_clg_airflow_prgm = nil ems_prgm_calling_mngr = nil ems_resize_psz_based_on_clg_airflow_prgm = OpenStudio :: Model :: EnergyManagementSystemProgram . new ( model ) ems_resize_psz_based_on_clg_airflow_prgm . setName ( "Resize PSZ To Match Product Availability" ) if verbose_info_statements == true runner . registerInfo ( "EMS Program object named '#{ems_resize_psz_based_on_clg_airflow_prgm.name}' added to resize airloop equipment based on autosized High Speed Airflow of Two Speed DX Clg Coil." ) end ems_prgm_calling_mngr = OpenStudio :: Model :: EnergyManagementSystemProgramCallingManager . new ( model ) ems_prgm_calling_mngr . setName ( "Apply Discrete Package Sizes to Air System Sizing" ) ems_prgm_calling_mngr . setCallingPoint ( "EndOfSystemSizing" ) ems_prgm_calling_mngr . addProgram ( ems_resize_psz_based_on_clg_airflow_prgm ) if verbose_info_statements == true runner . registerInfo ( "EMS Program Calling Manager object named '#{ems_prgm_calling_mngr.name}' added to call #{ems_resize_psz_based_on_clg_airflow_prgm.name} EMS program." ) end ems_arg_discrete_main_v_dot_gv = OpenStudio :: Model :: EnergyManagementSystemGlobalVariable . new ( model , "argDiscreteMainVdot" ) if verbose_info_statements == true runner . registerInfo ( "EMS Global Variable named '#{ems_arg_discrete_main_v_dot_gv.name}' added to hold a discrete airflow value for equipment sizing added to model." ) end ems_arg_main_v_dot_gv = OpenStudio :: Model :: EnergyManagementSystemGlobalVariable . new ( model , "argMainVdot" ) if verbose_info_statements == true runner . registerInfo ( "EMS Global Variable named '#{ems_arg_main_v_dot_gv.name}' added to hold a calculated airflow value for equipment sizing added to model." ) end ems_subroutine_prg = OpenStudio :: Model :: EnergyManagementSystemSubroutine . new ( model ) ems_subroutine_prg . setName ( "Select_Discrete_Nominal_Air_Flow" ) ems_subroutine_prg . addLine ( "IF (#{ems_arg_main_v_dot_gv.name} <= 0.56628)" ) ems_subroutine_prg . addLine ( "SET #{ems_arg_discrete_main_v_dot_gv.name} = 0.56628" ) ems_subroutine_prg . addLine ( "ELSEIF (#{ems_arg_main_v_dot_gv.name} > 0.56628) && (#{ems_arg_main_v_dot_gv.name} <= 0.75504)" ) ems_subroutine_prg . addLine ( "SET #{ems_arg_discrete_main_v_dot_gv.name} = 0.75504" ) ems_subroutine_prg . addLine ( "ELSEIF (#{ems_arg_main_v_dot_gv.name} > 0.75504) && (#{ems_arg_main_v_dot_gv.name} <= 0.9438 )" ) ems_subroutine_prg . addLine ( "SET #{ems_arg_discrete_main_v_dot_gv.name} = 0.9438" ) ems_subroutine_prg . addLine ( "ELSEIF (#{ems_arg_main_v_dot_gv.name} > 0.9438) && (#{ems_arg_main_v_dot_gv.name} <= 1.13256 )" ) ems_subroutine_prg . addLine ( "SET #{ems_arg_discrete_main_v_dot_gv.name} = 1.13256" ) ems_subroutine_prg . addLine ( "ELSEIF (#{ems_arg_main_v_dot_gv.name} > 1.13256) && (#{ems_arg_main_v_dot_gv.name} <= 1.4157 )" ) ems_subroutine_prg . addLine ( "SET #{ems_arg_discrete_main_v_dot_gv.name} = 1.4157" ) ems_subroutine_prg . addLine ( "ELSEIF (#{ems_arg_main_v_dot_gv.name} > 1.4157) && (#{ems_arg_main_v_dot_gv.name} <= 1.60446 )" ) ems_subroutine_prg . addLine ( "SET #{ems_arg_discrete_main_v_dot_gv.name} = 1.60446" ) ems_subroutine_prg . addLine ( "ELSEIF (#{ems_arg_main_v_dot_gv.name} > 1.60446) && (#{ems_arg_main_v_dot_gv.name} <= 1.8879 )" ) ems_subroutine_prg . addLine ( "SET #{ems_arg_discrete_main_v_dot_gv.name} = 1.8879" ) ems_subroutine_prg . addLine ( "ELSEIF (#{ems_arg_main_v_dot_gv.name} > 1.8879)" ) ems_subroutine_prg . addLine ( "SET dummy = @SevereWarnEP 666.0" ) ems_subroutine_prg . addLine ( "ENDIF" ) if verbose_info_statements == true runner . registerInfo ( "EMS Subroutine Program object named '#{ems_subroutine_prg.name}' added to reference main EMS program." ) end selected_airloops . each do | airloop | ems_internal_variable = OpenStudio :: Model :: EnergyManagementSystemInternalVariable . new ( model , "Intermediate Air System Main Supply Volume Flow Rate" ) ems_internal_variable . setName ( "#{airloop.name}_CalcMainSupVdot" . gsub ( "-" , "_" ) ) ems_internal_variable . setInternalDataIndexKeyName ( "#{airloop.name}" ) if verbose_info_statements == true runner . registerInfo ( "EMS Program Internal Variable object named '#{ems_internal_variable.name}' added to model." ) end ems_actuator = OpenStudio :: Model :: EnergyManagementSystemActuator . new ( airloop , "Sizing:System" , "Main Supply Volume Flow Rate" ) ems_actuator . setName ( "#{airloop.name}MainSupVdotSet" . gsub ( "-" , "_" ) ) if verbose_info_statements == true runner . registerInfo ( "EMS Actuator Object named #{ems_actuator.name} representing the Main Supply Volume Flow Rate of the AirLoop named '#{airloop.name}. added to the model." ) end ems_resize_psz_based_on_clg_airflow_prgm . addLine ( "SET #{ems_arg_main_v_dot_gv.name} = #{ems_internal_variable.name}" ) ems_resize_psz_based_on_clg_airflow_prgm . addLine ( "RUN #{ems_subroutine_prg.name}" ) ems_resize_psz_based_on_clg_airflow_prgm . addLine ( "SET #{ems_actuator.name} = #{ems_arg_discrete_main_v_dot_gv.name}" ) if verbose_info_statements == true runner . registerInfo ( "Logic appended to the EMS Actuator program Object named '#{ems_resize_psz_based_on_clg_airflow_prgm.name}' calling the EMS subroutine program for the AirLoop object named '#{airloop.name}'." ) end end output_EMS = model . getOutputEnergyManagementSystem output_EMS . setInternalVariableAvailabilityDictionaryReporting ( 'internal_variable_availability_dictionary_reporting' ) output_EMS . setEMSRuntimeLanguageDebugOutputLevel ( 'ems_runtime_language_debug_output_level' ) output_EMS . setActuatorAvailabilityDictionaryReporting ( 'actuator_availability_dictionary_reporting' ) if verbose_info_statements == true runner . registerInfo ( "Output EMS Program Object configured for model." ) end runner . registerFinalCondition ( "Measure finished with #{model.getEnergyManagementSystemSensors.count} EMS sensors, #{model.getEnergyManagementSystemActuators.count} EMS Actuators, #{model.getEnergyManagementSystemPrograms.count} EMS Programs, #{model.getEnergyManagementSystemSubroutines.count} EMS Subroutines, #{model.getEnergyManagementSystemProgramCallingManagers.count} EMS Program Calling Manager objects" ) end
define what happens when the measure is run
define what happens when the measure is run
# use the built-in error checking # assign the user inputs to variables # model is passed in because of argument type # initialize variables # check the airloop for existence in model # define selected airloop(s), depending on user input, add selected airloops to an array # end loop through airloops        # Further refine selected airloops to only have DX Cooling coils (1, 2 or variable speed) # end loop through supply components  # declare variables for proper scope # Create a 'stub' object for the EnergyManagementSystem:Program object for resizing a PSZ airloop # create a new EnergyManagementSystem:ProgramCallingManager object configured to call the EMS programs # Create new EnergyManagementSystem:GlobalVariable object and configure to hold the discrete value for MainVDot # Create new EnergyManagementSystem:GlobalVariable object and configure to hold the discrete value for MainVDot # Create new EnergyManagementSystem:Subroutine object for selecting equipment based on discrete  # airflows available from Manufacturer # Loop through selected AirLoops, hard size equipment airflow  # create a new EnergyManagementSystem:InternalVariable object for holding Intermediate Air System Main Supply Volume Flow Rate # Create new EMS Actuator Object representing airloop system sizing "Main Supply Volume Flow Rate" output variable # Append logic to the EMS program as we iterate through the selected airloop # end loop through selected airloop objects # create OutputEnergyManagementSystem object (a 'unique' object) and configure to allow EMS reporting
model
<not_specific>
runner
<not_specific>
user_arguments
<not_specific>