Measures/m_12_example_12_variable_refrigerant_flow_system_override/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)
    outdoor_vrf_units_to_modify = runner.getOptionalWorkspaceObjectChoiceValue('outdoor_vrf_units_to_modify', user_arguments, model) 
    indoor_vrf_unit_fixed_plr_value = runner.getDoubleArgumentValue("indoor_vrf_unit_fixed_plr_value",user_arguments)
    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")
    if indoor_vrf_unit_fixed_plr_value <= 0.0
      runner.registerError("The value of #{indoor_vrf_unit_fixed_plr_value} entered for the PLR value must be greater than zero.")
    end
    if indoor_vrf_unit_fixed_plr_value > 1.0
      runner.registerError("The value of #{indoor_vrf_unit_fixed_plr_value} entered for the PLR value must be less then 1.0.")
    end
    apply_to_all_vrf_outdoor_units = false
    selected_vrf_outdoor_units = nil
    if outdoor_vrf_units_to_modify.empty?
      handle = runner.getStringArgumentValue('outdoor_vrf_units_to_modify', user_arguments)
      if handle.empty?
        runner.registerError('No VRF Outdoor Unit object was chosen.')
        return false
      else
        runner.registerError("The selected VRF outdoor unit object with handle '#{handle}' was not found in the model. It may have been removed by another measure.")
        return false
      end
    else
      if !outdoor_vrf_units_to_modify.get.to_AirConditionerVariableRefrigerantFlow.empty?
        selected_vrf_outdoor_units = outdoor_vrf_units_to_modify.get.to_AirConditionerVariableRefrigerantFlow.get
      elsif !outdoor_vrf_units_to_modify.get.to_Building.empty?
        apply_to_all_vrf_outdoor_units = true
      else
        runner.registerError('Script Error - argument not showing up as an outdoor VRF unit.')
        return false
      end
    end
    selected_outdoor_vrf_units = []
    if apply_to_all_vrf_outdoor_units == true
      model.getAirConditionerVariableRefrigerantFlows.each do |each_vrf_outdoor_unit|
        selected_outdoor_vrf_units << each_vrf_outdoor_unit
      end
    else 
      selected_outdoor_vrf_units << selected_vrf_outdoor_units
    end
    if selected_outdoor_vrf_units.length == 0
      runner.registerAsNotApplicable("Model contains no 'qualified' VRF outdoor unit objects for this measure to modify.") 
      return true
    end
    vrf_outdoor_unit_counter = 0
    vrf_indoor_unit_counter = 0
    ems_vrf_control_prgm = nil
    ems_prgm_calling_mngr = nil
    ems_initialize_vrf_control_modes_prgm = OpenStudio::Model::EnergyManagementSystemProgram.new(model)
    ems_initialize_vrf_control_modes_prgm.setName("InitializeVRFControlModes")
    if verbose_info_statements == true
      runner.registerInfo("EMS Program object named '#{ems_initialize_vrf_control_modes_prgm.name} was added to initialize the control mode of specified VRF outdoor unit(s).")
    end
    ems_prgm_calling_mngr = OpenStudio::Model::EnergyManagementSystemProgramCallingManager.new(model)
    ems_prgm_calling_mngr.setName("Init VRF Control Mode Constants")
    ems_prgm_calling_mngr.setCallingPoint("BeginNewEnvironment")
    ems_prgm_calling_mngr.addProgram(ems_initialize_vrf_control_modes_prgm)  
    if verbose_info_statements == true
      runner.registerInfo("EMS Program Calling Manager object named '#{ems_prgm_calling_mngr.name}' added to call #{ems_initialize_vrf_control_modes_prgm.name} EMS program.") 
    end
    ems_vrf_control_prgm = OpenStudio::Model::EnergyManagementSystemProgram.new(model)
    ems_vrf_control_prgm.setName("VRFControl")
    if verbose_info_statements == true
      runner.registerInfo("EMS Program object named '#{ems_vrf_control_prgm.name} was added to override the specified thermostat control logic of the VRF outdor unit.")
    end
    ems_prgm_calling_mngr = OpenStudio::Model::EnergyManagementSystemProgramCallingManager.new(model)
    ems_prgm_calling_mngr.setName("VRF OnOff Management")
    ems_prgm_calling_mngr.setCallingPoint("InsideHVACSystemIterationLoop")
    ems_prgm_calling_mngr.addProgram(ems_vrf_control_prgm)  
    if verbose_info_statements == true
      runner.registerInfo("EMS Program Calling Manager object named '#{ems_prgm_calling_mngr.name}' added to call #{ems_vrf_control_prgm.name} EMS program.") 
    end
    selected_outdoor_vrf_units.each do |outdoor_vrf_unit|
      vrf_outdoor_unit_counter += 1
      ems_vrf_status_off_gv = OpenStudio::Model::EnergyManagementSystemGlobalVariable.new(model, "vrf_status_off_#{vrf_outdoor_unit_counter}")
      ems_vrf_status_cooling_gv = OpenStudio::Model::EnergyManagementSystemGlobalVariable.new(model, "vrf_status_cooling_#{vrf_outdoor_unit_counter}")
      ems_vrf_status_heating_gv = OpenStudio::Model::EnergyManagementSystemGlobalVariable.new(model, "vrf_status_heating_#{vrf_outdoor_unit_counter}")
      ems_initialize_vrf_control_modes_prgm.addLine("SET #{ems_vrf_status_off_gv.name} = 0.0")
      ems_initialize_vrf_control_modes_prgm.addLine("SET #{ems_vrf_status_cooling_gv.name} = 1.0")
      ems_initialize_vrf_control_modes_prgm.addLine("SET #{ems_vrf_status_heating_gv.name} = 2.0")
      ems_outdoor_vrf_unit_status_actuator = OpenStudio::Model::EnergyManagementSystemActuator.new(outdoor_vrf_unit,"Variable Refrigerant Flow Heat Pump","Operating Mode")
      ems_outdoor_vrf_unit_status_actuator.setName("VRF_Actuator_OnOff#{vrf_outdoor_unit_counter}".gsub("-","_"))
      if verbose_info_statements == true
        runner.registerInfo("An EMS Actuator object named '#{ems_outdoor_vrf_unit_status_actuator.name}' representing the Operating Mode of 'Variable Refrigerant Flow Heat Pump' object named #{outdoor_vrf_unit.name} was added to the model.") 
      end
      indoor_unit_terminals_vector = outdoor_vrf_unit.terminals
      if indoor_unit_terminals_vector.count == 0
        runner.registerInfo("VRF Outdoor unit object named #{outdoor_vrf_unit.name} does not appear to have any connected indoor VF terminal unit objects.")
      end
      indoor_unit_terminals_vector.each do |indoor_unit_terminal|
        vrf_indoor_unit_counter += 1
        ems_indoor_vrf_unit_PLR_actuator = OpenStudio::Model::EnergyManagementSystemActuator.new(indoor_unit_terminal,"Variable Refrigerant Flow Terminal Unit","Part Load Ratio")
        ems_indoor_vrf_unit_PLR_actuator.setName("#{indoor_unit_terminal.name}_VRF#{vrf_outdoor_unit_counter}".gsub("-","_"))
        if verbose_info_statements == true
          runner.registerInfo("An EMS Actuator object named '#{ems_indoor_vrf_unit_PLR_actuator.name}' representing the PLR value associated with the 'Variable Refrigerant Flow Indoor Terminal Unit' object named #{indoor_unit_terminal.name} was added to the model.") 
        end
        ems_vrf_control_prgm.addLine("SET #{ems_outdoor_vrf_unit_status_actuator.name} = #{ems_vrf_status_heating_gv.name}")
        ems_vrf_control_prgm.addLine("SET #{ems_indoor_vrf_unit_PLR_actuator.name} = #{indoor_vrf_unit_fixed_plr_value}")
      end 
      vrf_outdoor_unit_control_status_value = OpenStudio::Model::EnergyManagementSystemOutputVariable.new(model, ems_outdoor_vrf_unit_status_actuator)
      vrf_outdoor_unit_control_status_value.setName("Erl VRF Control Status#{vrf_outdoor_unit_counter}") 
      vrf_outdoor_unit_control_status_value.setEMSVariableName("#{ems_outdoor_vrf_unit_status_actuator.name}")
      vrf_outdoor_unit_control_status_value.setTypeOfDataInVariable("Averaged")
	  vrf_outdoor_unit_control_status_value.setUpdateFrequency("SystemTimeStep")    
    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 ) outdoor_vrf_units_to_modify = runner . getOptionalWorkspaceObjectChoiceValue ( 'outdoor_vrf_units_to_modify' , user_arguments , model ) indoor_vrf_unit_fixed_plr_value = runner . getDoubleArgumentValue ( "indoor_vrf_unit_fixed_plr_value" , user_arguments ) 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" ) if indoor_vrf_unit_fixed_plr_value <= 0.0 runner . registerError ( "The value of #{indoor_vrf_unit_fixed_plr_value} entered for the PLR value must be greater than zero." ) end if indoor_vrf_unit_fixed_plr_value > 1.0 runner . registerError ( "The value of #{indoor_vrf_unit_fixed_plr_value} entered for the PLR value must be less then 1.0." ) end apply_to_all_vrf_outdoor_units = false selected_vrf_outdoor_units = nil if outdoor_vrf_units_to_modify . empty? handle = runner . getStringArgumentValue ( 'outdoor_vrf_units_to_modify' , user_arguments ) if handle . empty? runner . registerError ( 'No VRF Outdoor Unit object was chosen.' ) return false else runner . registerError ( "The selected VRF outdoor unit object with handle '#{handle}' was not found in the model. It may have been removed by another measure." ) return false end else if ! outdoor_vrf_units_to_modify . get . to_AirConditionerVariableRefrigerantFlow . empty? selected_vrf_outdoor_units = outdoor_vrf_units_to_modify . get . to_AirConditionerVariableRefrigerantFlow . get elsif ! outdoor_vrf_units_to_modify . get . to_Building . empty? apply_to_all_vrf_outdoor_units = true else runner . registerError ( 'Script Error - argument not showing up as an outdoor VRF unit.' ) return false end end selected_outdoor_vrf_units = [ ] if apply_to_all_vrf_outdoor_units == true model . getAirConditionerVariableRefrigerantFlows . each do | each_vrf_outdoor_unit | selected_outdoor_vrf_units << each_vrf_outdoor_unit end else selected_outdoor_vrf_units << selected_vrf_outdoor_units end if selected_outdoor_vrf_units . length == 0 runner . registerAsNotApplicable ( "Model contains no 'qualified' VRF outdoor unit objects for this measure to modify." ) return true end vrf_outdoor_unit_counter = 0 vrf_indoor_unit_counter = 0 ems_vrf_control_prgm = nil ems_prgm_calling_mngr = nil ems_initialize_vrf_control_modes_prgm = OpenStudio :: Model :: EnergyManagementSystemProgram . new ( model ) ems_initialize_vrf_control_modes_prgm . setName ( "InitializeVRFControlModes" ) if verbose_info_statements == true runner . registerInfo ( "EMS Program object named '#{ems_initialize_vrf_control_modes_prgm.name} was added to initialize the control mode of specified VRF outdoor unit(s)." ) end ems_prgm_calling_mngr = OpenStudio :: Model :: EnergyManagementSystemProgramCallingManager . new ( model ) ems_prgm_calling_mngr . setName ( "Init VRF Control Mode Constants" ) ems_prgm_calling_mngr . setCallingPoint ( "BeginNewEnvironment" ) ems_prgm_calling_mngr . addProgram ( ems_initialize_vrf_control_modes_prgm ) if verbose_info_statements == true runner . registerInfo ( "EMS Program Calling Manager object named '#{ems_prgm_calling_mngr.name}' added to call #{ems_initialize_vrf_control_modes_prgm.name} EMS program." ) end ems_vrf_control_prgm = OpenStudio :: Model :: EnergyManagementSystemProgram . new ( model ) ems_vrf_control_prgm . setName ( "VRFControl" ) if verbose_info_statements == true runner . registerInfo ( "EMS Program object named '#{ems_vrf_control_prgm.name} was added to override the specified thermostat control logic of the VRF outdor unit." ) end ems_prgm_calling_mngr = OpenStudio :: Model :: EnergyManagementSystemProgramCallingManager . new ( model ) ems_prgm_calling_mngr . setName ( "VRF OnOff Management" ) ems_prgm_calling_mngr . setCallingPoint ( "InsideHVACSystemIterationLoop" ) ems_prgm_calling_mngr . addProgram ( ems_vrf_control_prgm ) if verbose_info_statements == true runner . registerInfo ( "EMS Program Calling Manager object named '#{ems_prgm_calling_mngr.name}' added to call #{ems_vrf_control_prgm.name} EMS program." ) end selected_outdoor_vrf_units . each do | outdoor_vrf_unit | vrf_outdoor_unit_counter += 1 ems_vrf_status_off_gv = OpenStudio :: Model :: EnergyManagementSystemGlobalVariable . new ( model , "vrf_status_off_#{vrf_outdoor_unit_counter}" ) ems_vrf_status_cooling_gv = OpenStudio :: Model :: EnergyManagementSystemGlobalVariable . new ( model , "vrf_status_cooling_#{vrf_outdoor_unit_counter}" ) ems_vrf_status_heating_gv = OpenStudio :: Model :: EnergyManagementSystemGlobalVariable . new ( model , "vrf_status_heating_#{vrf_outdoor_unit_counter}" ) ems_initialize_vrf_control_modes_prgm . addLine ( "SET #{ems_vrf_status_off_gv.name} = 0.0" ) ems_initialize_vrf_control_modes_prgm . addLine ( "SET #{ems_vrf_status_cooling_gv.name} = 1.0" ) ems_initialize_vrf_control_modes_prgm . addLine ( "SET #{ems_vrf_status_heating_gv.name} = 2.0" ) ems_outdoor_vrf_unit_status_actuator = OpenStudio :: Model :: EnergyManagementSystemActuator . new ( outdoor_vrf_unit , "Variable Refrigerant Flow Heat Pump" , "Operating Mode" ) ems_outdoor_vrf_unit_status_actuator . setName ( "VRF_Actuator_OnOff#{vrf_outdoor_unit_counter}" . gsub ( "-" , "_" ) ) if verbose_info_statements == true runner . registerInfo ( "An EMS Actuator object named '#{ems_outdoor_vrf_unit_status_actuator.name}' representing the Operating Mode of 'Variable Refrigerant Flow Heat Pump' object named #{outdoor_vrf_unit.name} was added to the model." ) end indoor_unit_terminals_vector = outdoor_vrf_unit . terminals if indoor_unit_terminals_vector . count == 0 runner . registerInfo ( "VRF Outdoor unit object named #{outdoor_vrf_unit.name} does not appear to have any connected indoor VF terminal unit objects." ) end indoor_unit_terminals_vector . each do | indoor_unit_terminal | vrf_indoor_unit_counter += 1 ems_indoor_vrf_unit_PLR_actuator = OpenStudio :: Model :: EnergyManagementSystemActuator . new ( indoor_unit_terminal , "Variable Refrigerant Flow Terminal Unit" , "Part Load Ratio" ) ems_indoor_vrf_unit_PLR_actuator . setName ( "#{indoor_unit_terminal.name}_VRF#{vrf_outdoor_unit_counter}" . gsub ( "-" , "_" ) ) if verbose_info_statements == true runner . registerInfo ( "An EMS Actuator object named '#{ems_indoor_vrf_unit_PLR_actuator.name}' representing the PLR value associated with the 'Variable Refrigerant Flow Indoor Terminal Unit' object named #{indoor_unit_terminal.name} was added to the model." ) end ems_vrf_control_prgm . addLine ( "SET #{ems_outdoor_vrf_unit_status_actuator.name} = #{ems_vrf_status_heating_gv.name}" ) ems_vrf_control_prgm . addLine ( "SET #{ems_indoor_vrf_unit_PLR_actuator.name} = #{indoor_vrf_unit_fixed_plr_value}" ) end vrf_outdoor_unit_control_status_value = OpenStudio :: Model :: EnergyManagementSystemOutputVariable . new ( model , ems_outdoor_vrf_unit_status_actuator ) vrf_outdoor_unit_control_status_value . setName ( "Erl VRF Control Status#{vrf_outdoor_unit_counter}" ) vrf_outdoor_unit_control_status_value . setEMSVariableName ( "#{ems_outdoor_vrf_unit_status_actuator.name}" ) vrf_outdoor_unit_control_status_value . setTypeOfDataInVariable ( "Averaged" ) vrf_outdoor_unit_control_status_value . setUpdateFrequency ( "SystemTimeStep" ) 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 # check for reasonableness of plr value # check the VRF outdoor unit for existence in model # define selected outdoor vrf unit objects, depending on user input, add selected vrf unit(s) to an array # declare variables for proper scope # Create a stub for the EnergyManagementSystem:Program object for initializing the VRF control modes # Create a new EnergyManagementSystem:ProgramCallingManager object configured to call the EMS program "Init VRF Control Mode Constants" # Create a stub for the EnergyManagementSystem:Program object for actuation of the VRF outdoor unit # Create a second new EnergyManagementSystem:ProgramCallingManager object configured to call the EMS program "VRF OnOff Management" #Loop through selected vrf outdoor unit objects # create new EMS Global Variable objects representing the operating status of the VRF outdoor unit  # Append logic statements to the EMS program as we iterate through the selected outdoor vrf unit objects # create a new VRF outdoor unit EMS actuator object representing the operating status of the VRF outdoor unit  # place attached indoor vrf indoor terminal unit objects into an array # loop through the indoor terminal units to create an actuator associated with the part load ratio attribute of the indoor unit object # Append logic statements to the EMS program as we iterate through the attached indoor vrf unit objects # end loop throught associated indoor VRF units # create new EnergyManagementSystem:OutputVariable object and configure it to hold the VRF outdoor unit control status  # end loop through VRF outdoor units   
model
<not_specific>
runner
<not_specific>
user_arguments
<not_specific>