Measures/m_1_example_1_whole_building_average_zone_air_temperature/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)
    internal_VariableAvailabilityDictionaryReporting = runner.getOptionalWorkspaceObjectChoiceValue('internal_VariableAvailabilityDictionaryReporting', user_arguments, model) 
    ems_RuntimeLanguageDebugOutputLevel = runner.getOptionalWorkspaceObjectChoiceValue('ems_RuntimeLanguageDebugOutputLevel', user_arguments, model) 
    actuator_AvailabilityDictionaryReporting = runner.getOptionalWorkspaceObjectChoiceValue('actuator_AvailabilityDictionaryReporting', user_arguments, model) 
    conditioned_thermal_zone_count = 0
    zone_mean_air_temp_sensor_name_array = []
    zone_volume_name_array = []
    sum_numerator = 0 
    sum_denominator = 0 
    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")
    model.getThermalZones.each do |thermal_zone|
      if thermal_zone.thermostatSetpointDualSetpoint.is_initialized
        t_stat = thermal_zone.thermostatSetpointDualSetpoint.get
        if t_stat.getHeatingSchedule.is_initialized && t_stat.getCoolingSchedule.is_initialized
          ems_zone_mean_air_temp_sensor = OpenStudio::Model::EnergyManagementSystemSensor.new(model, "Zone Mean Air Temperature")
          ems_zone_mean_air_temp_sensor.setName("T#{conditioned_thermal_zone_count}")
          ems_zone_mean_air_temp_sensor.setKeyName(thermal_zone.name.to_s)
          zone_mean_air_temp_sensor_name_array << ems_zone_mean_air_temp_sensor.name.to_s
          if verbose_info_statements == true
            runner.registerInfo("EMS Sensor named #{ems_zone_mean_air_temp_sensor.name} measuring the Mean Air Temp of the Thermal Zone named #{thermal_zone.name} added to the model.")
          end
          ems_zone_volume_internal_variable = OpenStudio::Model::EnergyManagementSystemInternalVariable.new(model, "#{thermal_zone.name} Zone Air Volume")
          ems_zone_volume_internal_variable.setName("Zn#{conditioned_thermal_zone_count}vol")
          ems_zone_volume_internal_variable.setInternalDataIndexKeyName(thermal_zone.name.to_s)
          ems_zone_volume_internal_variable.setInternalDataType("Zone Air Volume")
          zone_volume_name_array << ems_zone_volume_internal_variable.name.to_s
          conditioned_thermal_zone_count += 1
          if verbose_info_statements == true
            runner.registerInfo("EMS Internal Variable named #{ems_zone_volume_internal_variable.name} representing the volume of the Thermal Zone named #{thermal_zone.name} added.")
          end
          if conditioned_thermal_zone_count == 0 
            runner.registerAsNotApplicable("EMS Measure not applicable because model has zero conditioned thermal zones!")
            return true
          end
        end
      end
    end 
    pairs = zone_mean_air_temp_sensor_name_array.zip(zone_volume_name_array)
    products = pairs.map{ |a| a[0] + "*" + a[1]}
    sum_numerator = products.join(" + ")
    sum_denominator = zone_volume_name_array.join(" + ")
    ems_average_zone_temps = OpenStudio::Model::EnergyManagementSystemProgram.new(model)
    ems_average_zone_temps.setName("AverageZoneTemps")
    ems_average_zone_temps.addLine("Set SumNumerator = #{sum_numerator}")
    ems_average_zone_temps.addLine("Set SumDenominator = #{sum_denominator}")
    ems_average_zone_temps.addLine("Set AverageBuildingTemp = SumNumerator / SumDenominator")
    if verbose_info_statements == true
      runner.registerInfo("A new EnergyManagementSystemProgram object named '#{ems_average_zone_temps.name}' was added to the model.")
    end
    outputEMS = model.getOutputEnergyManagementSystem
    outputEMS.setInternalVariableAvailabilityDictionaryReporting("internal_VariableAvailabilityDictionaryReporting")
    outputEMS.setEMSRuntimeLanguageDebugOutputLevel("ems_RuntimeLanguageDebugOutputLevel")
    outputEMS.setActuatorAvailabilityDictionaryReporting("actuator_AvailabilityDictionaryReporting")
    if verbose_info_statements == true
      runner.registerInfo("An OutputEnergyManagementSystem object was configured for EMS program reporting per the user arguments.")
    end
    output_variable = OpenStudio::Model::OutputVariable.new("Weighted Average Building Zone Air Temperature",model)
    output_variable.setKeyValue("*")
    output_variable.setReportingFrequency("Timestep") 
    output_variable.setVariableName("Weighted Average Building Zone Air Temperature")
    if verbose_info_statements == true
      runner.registerInfo("A new OutputVariable object named '#{output_variable.name}' representing the Weighted Average Building Zone Air Temperature was added to the model.")
    end
    ems_program_calling_manager = OpenStudio::Model::EnergyManagementSystemProgramCallingManager.new(model)
    ems_program_calling_manager.setName("Average Building Temperature")
    ems_program_calling_manager.setCallingPoint("EndOfZoneTimestepBeforeZoneReporting")
    ems_program_calling_manager.addProgram(ems_average_zone_temps)
    if verbose_info_statements == true
      runner.registerInfo("An EMS Program Calling Manager object named #{ems_program_calling_manager.name} was added to the model.")
    end
    average_building_temp = OpenStudio::Model::EnergyManagementSystemGlobalVariable.new(model, "AverageBuildingTemp")
    if verbose_info_statements == true
      runner.registerInfo("An EMS Global Variable object named #{average_building_temp.name} representing the Average Building Temperature was added to the model.")
    end
    ems_average_bldg_temp_ov = OpenStudio::Model::EnergyManagementSystemOutputVariable.new(model, average_building_temp)
    ems_average_bldg_temp_ov.setName("Weighted Average Building Zone Air Temperature") 
    ems_average_bldg_temp_ov.setEMSVariableName("#{average_building_temp.name}")
    ems_average_bldg_temp_ov.setUnits("C")
    ems_average_bldg_temp_ov.setTypeOfDataInVariable("Averaged")
	ems_average_bldg_temp_ov.setUpdateFrequency("ZoneTimeStep")    
    if verbose_info_statements == true
      runner.registerInfo("An EMS Output Variable object named #{ems_average_bldg_temp_ov.name} representing the Weighted Average Building Zone Air Temperature was added to the 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")
    return true
  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 ) internal_VariableAvailabilityDictionaryReporting = runner . getOptionalWorkspaceObjectChoiceValue ( 'internal_VariableAvailabilityDictionaryReporting' , user_arguments , model ) ems_RuntimeLanguageDebugOutputLevel = runner . getOptionalWorkspaceObjectChoiceValue ( 'ems_RuntimeLanguageDebugOutputLevel' , user_arguments , model ) actuator_AvailabilityDictionaryReporting = runner . getOptionalWorkspaceObjectChoiceValue ( 'actuator_AvailabilityDictionaryReporting' , user_arguments , model ) conditioned_thermal_zone_count = 0 zone_mean_air_temp_sensor_name_array = [ ] zone_volume_name_array = [ ] sum_numerator = 0 sum_denominator = 0 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" ) model . getThermalZones . each do | thermal_zone | if thermal_zone . thermostatSetpointDualSetpoint . is_initialized t_stat = thermal_zone . thermostatSetpointDualSetpoint . get if t_stat . getHeatingSchedule . is_initialized && t_stat . getCoolingSchedule . is_initialized ems_zone_mean_air_temp_sensor = OpenStudio :: Model :: EnergyManagementSystemSensor . new ( model , "Zone Mean Air Temperature" ) ems_zone_mean_air_temp_sensor . setName ( "T#{conditioned_thermal_zone_count}" ) ems_zone_mean_air_temp_sensor . setKeyName ( thermal_zone . name . to_s ) zone_mean_air_temp_sensor_name_array << ems_zone_mean_air_temp_sensor . name . to_s if verbose_info_statements == true runner . registerInfo ( "EMS Sensor named #{ems_zone_mean_air_temp_sensor.name} measuring the Mean Air Temp of the Thermal Zone named #{thermal_zone.name} added to the model." ) end ems_zone_volume_internal_variable = OpenStudio :: Model :: EnergyManagementSystemInternalVariable . new ( model , "#{thermal_zone.name} Zone Air Volume" ) ems_zone_volume_internal_variable . setName ( "Zn#{conditioned_thermal_zone_count}vol" ) ems_zone_volume_internal_variable . setInternalDataIndexKeyName ( thermal_zone . name . to_s ) ems_zone_volume_internal_variable . setInternalDataType ( "Zone Air Volume" ) zone_volume_name_array << ems_zone_volume_internal_variable . name . to_s conditioned_thermal_zone_count += 1 if verbose_info_statements == true runner . registerInfo ( "EMS Internal Variable named #{ems_zone_volume_internal_variable.name} representing the volume of the Thermal Zone named #{thermal_zone.name} added." ) end if conditioned_thermal_zone_count == 0 runner . registerAsNotApplicable ( "EMS Measure not applicable because model has zero conditioned thermal zones!" ) return true end end end end pairs = zone_mean_air_temp_sensor_name_array . zip ( zone_volume_name_array ) products = pairs . map { | a | a [ 0 ] + "*" + a [ 1 ] } sum_numerator = products . join ( " + " ) sum_denominator = zone_volume_name_array . join ( " + " ) ems_average_zone_temps = OpenStudio :: Model :: EnergyManagementSystemProgram . new ( model ) ems_average_zone_temps . setName ( "AverageZoneTemps" ) ems_average_zone_temps . addLine ( "Set SumNumerator = #{sum_numerator}" ) ems_average_zone_temps . addLine ( "Set SumDenominator = #{sum_denominator}" ) ems_average_zone_temps . addLine ( "Set AverageBuildingTemp = SumNumerator / SumDenominator" ) if verbose_info_statements == true runner . registerInfo ( "A new EnergyManagementSystemProgram object named '#{ems_average_zone_temps.name}' was added to the model." ) end outputEMS = model . getOutputEnergyManagementSystem outputEMS . setInternalVariableAvailabilityDictionaryReporting ( "internal_VariableAvailabilityDictionaryReporting" ) outputEMS . setEMSRuntimeLanguageDebugOutputLevel ( "ems_RuntimeLanguageDebugOutputLevel" ) outputEMS . setActuatorAvailabilityDictionaryReporting ( "actuator_AvailabilityDictionaryReporting" ) if verbose_info_statements == true runner . registerInfo ( "An OutputEnergyManagementSystem object was configured for EMS program reporting per the user arguments." ) end output_variable = OpenStudio :: Model :: OutputVariable . new ( "Weighted Average Building Zone Air Temperature" , model ) output_variable . setKeyValue ( "*" ) output_variable . setReportingFrequency ( "Timestep" ) output_variable . setVariableName ( "Weighted Average Building Zone Air Temperature" ) if verbose_info_statements == true runner . registerInfo ( "A new OutputVariable object named '#{output_variable.name}' representing the Weighted Average Building Zone Air Temperature was added to the model." ) end ems_program_calling_manager = OpenStudio :: Model :: EnergyManagementSystemProgramCallingManager . new ( model ) ems_program_calling_manager . setName ( "Average Building Temperature" ) ems_program_calling_manager . setCallingPoint ( "EndOfZoneTimestepBeforeZoneReporting" ) ems_program_calling_manager . addProgram ( ems_average_zone_temps ) if verbose_info_statements == true runner . registerInfo ( "An EMS Program Calling Manager object named #{ems_program_calling_manager.name} was added to the model." ) end average_building_temp = OpenStudio :: Model :: EnergyManagementSystemGlobalVariable . new ( model , "AverageBuildingTemp" ) if verbose_info_statements == true runner . registerInfo ( "An EMS Global Variable object named #{average_building_temp.name} representing the Average Building Temperature was added to the model." ) end ems_average_bldg_temp_ov = OpenStudio :: Model :: EnergyManagementSystemOutputVariable . new ( model , average_building_temp ) ems_average_bldg_temp_ov . setName ( "Weighted Average Building Zone Air Temperature" ) ems_average_bldg_temp_ov . setEMSVariableName ( "#{average_building_temp.name}" ) ems_average_bldg_temp_ov . setUnits ( "C" ) ems_average_bldg_temp_ov . setTypeOfDataInVariable ( "Averaged" ) ems_average_bldg_temp_ov . setUpdateFrequency ( "ZoneTimeStep" ) if verbose_info_statements == true runner . registerInfo ( "An EMS Output Variable object named #{ems_average_bldg_temp_ov.name} representing the Weighted Average Building Zone Air Temperature was added to the 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" ) return true 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 # model is passed in because of argument type # model is passed in because of argument type # initialize variables for proper scope # loop through all conditioned thermal zones to determine if they qualify for the average temperature calculation # Create new EnergyManagementSystem:Sensor object representing zone mean air temperature   # store sensor name in array for later post-processing # Create new EnergyManagementSystem:InternalVariable object representing the zone volume   # store internal variable name in an array for later post-processing # check to make sure model has at least one conditioned thermal zone else trigger N/A message # end loop through thermal zones # manipulate string of array of sensor names and internal variables to form string for  # EMS program logic statement creating SumNumerator # manipulate string of array of internal variable names to form necessary string for  # EMS program logic statement creating SumDenominator # create new EnergyManagementSystem:Program object describing the zone temp averaging algorithm # create unique object for OutputEnergyManagementSystem object and configure to allow EMS reporting # create new OutputVariable object describing the requested reporting frequency # create new EnergyManagementSystem:ProgramCallingManager object and configure to call ems_average_zone_temps EMS Program # create new EnergyManagementSystem:GlobalVariable object and configure to hold AverageBuildingTemp calculated variable # create new global EnergyManagementSystem:OutputVariable object and configure it to hold the "averaged" value of  # the AverageBuildingTemp Global Variable
model
<not_specific>
runner
<not_specific>
user_arguments
<not_specific>