Beispiel #1
0
 def gas_types(self, g_types):
     self._gas_types = tuple_with_length(g_types, self._gas_count, str,
                                         'gas mixture gas_types')
     self._gas_types = tuple(gas.title() for gas in self._gas_types)
     for gas in self._gas_types:
         assert gas in self.GASES, 'Invalid input "{}" for gas type.' \
             '\nGas type must be one of the following:{}'.format(gas, self.GASES)
Beispiel #2
0
    def average_schedules(name, schedules, weights=None, timestep_resolution=1):
        """Create a ScheduleDay that is a weighted average between other ScheduleDays.

        Args:
            name: A name for the new averaged ScheduleDay.
            schedules: A list of ScheduleDay objects that will be averaged together
                to make a new ScheduleDay.
            weights: An optional list of fractioanl numbers with the same length
                as the input schedules that sum to 1. These will be used to weight
                each of the ScheduleDay objects in the resulting average schedule.
                If None, the individual schedules will be weighted equally.
            timestep_resolution: An optional integer for the timestep resolution
                at which the schedules will be averaged. Any schedule details
                smaller than this timestep will be lost in the averaging process.
                Default: 1.
        """
        # check the inputs
        assert isinstance(schedules, (list, tuple)), 'Expected a list of ScheduleDay ' \
            'objects for average_schedules. Got {}.'.format(type(schedules))
        if weights is None:
            weight = 1 / len(schedules)
            weights = [weight for i in schedules]
        else:
            weights = tuple_with_length(weights, len(schedules), float,
                                        'average schedules weights')
            assert sum(weights) == 1, 'Average schedule weights must sum to 1. ' \
                'Got {}.'.format(sum(weights))

        # create a weighted average list of values
        all_values = [sch.values_at_timestep(timestep_resolution) for sch in schedules]
        sch_vals = [sum([val * weights[i] for i, val in enumerate(values)])
                    for values in zip(*all_values)]

        # return the final list
        return ScheduleDay.from_values_at_timestep(name, sch_vals, timestep_resolution)
Beispiel #3
0
    def _check_avg_weights(load_objects, weights, obj_name):
        """Check input weights of an average calculation and generate them if None."""
        if weights is None:
            weights = unity_weights = [1 / len(load_objects)] * len(load_objects) if \
                len(load_objects) > 0 else []
        else:
            weights = tuple_with_length(weights, len(load_objects), float,
                                        'average {} weights'.format(obj_name))
            total_weight = sum(weights)
            assert total_weight <= 1 + 1e-9, 'Average {} weights must be less than ' \
                'or equal to 1. Got {}.'.format(obj_name, sum(weights))
            unity_weights = [w / total_weight for w in weights]

        return weights, unity_weights
Beispiel #4
0
 def dir(self, value):
     self._dir = typing.tuple_with_length(value) if value is not None else (
         0, 0, 1)
Beispiel #5
0
 def pos(self, value):
     self._pos = typing.tuple_with_length(value) if value is not None else (
         0, 0, 0)
    def average(identifier,
                program_types,
                weights=None,
                timestep_resolution=1):
        """Get a ProgramType object that's a weighted average between other objects.

        Args:
            identifier: A unique ID text string for the new averaged ProgramType.
                Must be < 100 characters and not contain any EnergyPlus special
                characters. This will be used to identify the object across a model
                and in the exported IDF.
            program_types: A list of ProgramType objects that will be averaged
                together to make a new ProgramType.
            weights: An optional list of fractional numbers with the same length
                as the input program_types that sum to 1. These will be used to weight
                each of the ProgramType objects in the resulting average. If None, the
                individual objects will be weighted equally. Default: None.
            timestep_resolution: An optional integer for the timestep resolution
                at which the schedules will be averaged. Any schedule details
                smaller than this timestep will be lost in the averaging process.
                Default: 1.
        """
        # check the weights input
        if weights is None:
            weights = [1 / len(program_types)] * len(program_types) if \
                len(program_types) > 0 else []
        else:
            weights = tuple_with_length(weights, len(program_types), float,
                                        'average ProgramType weights')
        assert abs(sum(weights) - 1.0) <= 1e-9, 'Average ProgramType weights ' \
            'must be equal to 1. Got {}.'.format(sum(weights))

        # gather all of the load objects across all of the programs
        people_mtx = [[pr.people, w] for pr, w in zip(program_types, weights)
                      if pr.people is not None]
        lighting_mtx = [[pr.lighting, w]
                        for pr, w in zip(program_types, weights)
                        if pr.lighting is not None]
        e_equip_mtx = [[pr.electric_equipment, w]
                       for pr, w in zip(program_types, weights)
                       if pr.electric_equipment is not None]
        g_equip_mtx = [[pr.gas_equipment, w]
                       for pr, w in zip(program_types, weights)
                       if pr.gas_equipment is not None]
        inf_mtx = [[pr.infiltration, w]
                   for pr, w in zip(program_types, weights)
                   if pr.infiltration is not None]
        vent_mtx = [[pr.ventilation, w]
                    for pr, w in zip(program_types, weights)
                    if pr.ventilation is not None]
        setp_mtx = [[pr.setpoint, w] for pr, w in zip(program_types, weights)
                    if pr.setpoint is not None]

        # compute the average loads
        people = None
        if len(people_mtx) != 0:
            t_people_mtx = tuple(zip(*people_mtx))
            people = People.average('{}_People'.format(identifier),
                                    t_people_mtx[0], t_people_mtx[1],
                                    timestep_resolution)
        lighting = None
        if len(lighting_mtx) != 0:
            t_lighting_mtx = tuple(zip(*lighting_mtx))
            lighting = Lighting.average('{}_Lighting'.format(identifier),
                                        t_lighting_mtx[0], t_lighting_mtx[1],
                                        timestep_resolution)
        electric_equipment = None
        if len(e_equip_mtx) != 0:
            t_e_equip_mtx = tuple(zip(*e_equip_mtx))
            electric_equipment = ElectricEquipment.average(
                '{}_Electric Equipment'.format(identifier), t_e_equip_mtx[0],
                t_e_equip_mtx[1], timestep_resolution)
        gas_equipment = None
        if len(g_equip_mtx) != 0:
            t_g_equip_mtx = tuple(zip(*g_equip_mtx))
            gas_equipment = GasEquipment.average(
                '{}_Gas Equipment'.format(identifier), t_g_equip_mtx[0],
                t_g_equip_mtx[1], timestep_resolution)
        infiltration = None
        if len(inf_mtx) != 0:
            t_inf_mtx = tuple(zip(*inf_mtx))
            infiltration = Infiltration.average(
                '{}_Infiltration'.format(identifier), t_inf_mtx[0],
                t_inf_mtx[1], timestep_resolution)
        ventilation = None
        if len(vent_mtx) != 0:
            t_vent_mtx = tuple(zip(*vent_mtx))
            ventilation = Ventilation.average(
                '{}_Ventilation'.format(identifier), t_vent_mtx[0],
                t_vent_mtx[1], timestep_resolution)
        setpoint = None
        if len(setp_mtx) != 0:
            t_setp_mtx = tuple(zip(*setp_mtx))
            setpoint = Setpoint.average('{}_Setpoint'.format(identifier),
                                        t_setp_mtx[0], t_setp_mtx[1],
                                        timestep_resolution)

        # return the averaged object
        return ProgramType(identifier, people, lighting, electric_equipment,
                           gas_equipment, infiltration, ventilation, setpoint)
Beispiel #7
0
    def average_schedules(identifier, schedules, weights=None):
        """Get a ScheduleFixedInterval that's a weighted average between other schedules.

        Args:
            identifier: A unique ID text string for the new unique ScheduleFixedInterval.
                Must be < 100 characters and not contain any EnergyPlus special
                characters. This will be used to identify the object across a
                model and in the exported IDF.
            schedules: A list of ScheduleFixedInterval objects that will be averaged
                together to make a new ScheduleFixedInterval. This list may also contain
                ScheduleRulesets but it is recommend there be at least one
                ScheduleFixedInterval. Otherwise, the ScheduleRuleset.average_schedules
                method should be used.
            weights: An optional list of fractional numbers with the same length
                as the input schedules that sum to 1. These will be used to weight
                each of the ScheduleFixedInterval objects in the resulting average
                schedule. If None, the individual schedules will be weighted equally.
        """
        # check the inputs
        assert isinstance(schedules, (list, tuple)), 'Expected a list of ScheduleDay ' \
            'objects for average_schedules. Got {}.'.format(type(schedules))
        if weights is None:
            weight = 1 / len(schedules)
            weights = [weight for i in schedules]
        else:
            weights = tuple_with_length(weights, len(schedules), float,
                                        'average schedules weights')
            assert abs(sum(weights) - 1.0) <= 1e-9, 'Average schedule weights must ' \
                'sum to 1.  Got {}.'.format(sum(weights))

        # determine the max timestep and leap year for the resulting schedule
        t_steps = [1]
        lp_yrs = []
        for sched in schedules:
            try:
                t_steps.append(sched.timestep)
                lp_yrs.append(sched.is_leap_year)
            except AttributeError:
                pass  # ScheduleRuleset
        timestep = max(t_steps)
        lp_yr = lp_yrs[0] if len(lp_yrs) != 0 else False
        for lp in lp_yrs:
            assert lp is lp_yr, \
                'All is_leap_year properties must match to make an average schedule.'

        # collect all of the values at the timestep
        all_values = []
        for sched in schedules:
            if isinstance(sched, ScheduleFixedInterval):
                all_values.append(sched.values_at_timestep(timestep))
            else:
                try:
                    all_values.append(sched.values(timestep, leap_year=lp_yr))
                except AttributeError:
                    raise TypeError(
                        '"{}" is not an acceptable input type for '
                        'ScheduleFixedInterval.average_schedules.'.format(
                            type(sched)))

        sch_vals = [
            sum([val * weights[i] for i, val in enumerate(values)])
            for values in zip(*all_values)
        ]

        # return the final schedule
        return ScheduleFixedInterval(identifier,
                                     sch_vals,
                                     schedules[0].schedule_type_limit,
                                     timestep,
                                     start_date=Date(1, 1, lp_yr))
Beispiel #8
0
 def gas_fractions(self, g_fracs):
     self._gas_fractions = tuple_with_length(g_fracs, self._gas_count,
                                             float,
                                             'gas mixture gas_fractions')
     assert sum(self._gas_fractions) == 1, 'Gas fractions must sum to 1. ' \
         'Got {}.'.format(sum(self._gas_fractions))
Beispiel #9
0
    def diffuse_transmittance(self, value):
        if value is not None:
            value = typing.tuple_with_length(value, 3)

        self._diffuse_transmittance = value
Beispiel #10
0
    def back_diffuse_reflectance(self, value):
        if value is not None:
            value = typing.tuple_with_length(value, 3)

        self._back_diffuse_reflectance = value
Beispiel #11
0
def test_tuple_with_length():
    """Test the tuple_with_length method."""
    assert isinstance(tuple_with_length((1, 2, 3), 3, float, 'test tuple'),
                      tuple)
    assert isinstance(tuple_with_length([1, 2, 3], 3, float, 'test tuple'),
                      tuple)
    assert isinstance(tuple_with_length(range(3), 3, float, 'test tuple'),
                      tuple)
    assert isinstance(
        tuple_with_length((1.0, 2.0, 3.0), 3, float, 'test tuple'), tuple)
    assert isinstance(
        tuple_with_length(('1', '2', '3'), 3, float, 'test tuple'), tuple)

    with pytest.raises(AssertionError):
        tuple_with_length((1, 2, 3), 4, float, 'test tuple')
    with pytest.raises(TypeError):
        tuple_with_length(('one', 'two', 'three'), 3, float, 'test tuple')

    try:
        tuple_with_length((1, 2, 3), 4, float, 'test tuple')
    except AssertionError as e:
        assert 'test tuple' in str(e)