Skip to content

Energy Sub-Module

t3co.energy_models.energy

Energy dataclass

Source code in src/t3co/energy_models/energy.py
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
@dataclass
class Energy:
    mpgge: float = None
    primary_fuel_range_mi: float = None
    zero_to_sixty_loaded: float = None
    zero_to_thirty_loaded: float = None
    grade_6_mph_ach: float = None
    grade_1_25_mph_ach: float = None

    def __new__(cls, *args, **kwargs):
        """
        Creates a new instance of the Energy class.
        """
        instance = super(Energy, cls).__new__(cls)
        return instance

    def __init__(self, mpgge: float = None, primary_fuel_range_mi: float = None):
        """
        Initializes the Energy instance.

        Args:
            mpgge (float, optional): Miles per gallon gasoline equivalent. Defaults to None.
            primary_fuel_range_mi (float, optional): Primary fuel range in miles. Defaults to None.
        """
        if mpgge is not None and primary_fuel_range_mi is not None:
            self.mpgge = mpgge
            self.primary_fuel_range_mi = primary_fuel_range_mi

    def run_fastsim_model(
        self,
        veh_no: int,
        scenario: Scenario,
        t3co_vehicle: Vehicle = None,
        vehicle_df: pd.DataFrame = None,
        vehicle_file: Union[str, Path] = gl.RESOURCES_FOLDERPATH
        / "inputs"
        / "Demo_FY22_vehicle_model_assumptions.csv",
    ) -> None:
        """
        Runs the FASTSim model to calculate mpgge and primary fuel range.

        Args:
            veh_no (int): Vehicle selection number.
            scenario (Scenario): Scenario instance containing configuration data.
            vehicle_file (Union[str, Path], optional): Vehicle model assumptions input CSV file path. Defaults to gl.RESOURCES_FOLDERPATH / "inputs" / "Demo_FY22_vehicle_model_assumptions.csv".
        """
        if RunFASTSim is None:
            raise ImportError(
                "FASTSim is not installed or could not be imported. Cannot run FASTSim model."
            )

        fastsim_run = RunFASTSim(
            veh_no=veh_no,
            scenario=scenario,
            t3co_vehicle=t3co_vehicle,
            vehicle_df=vehicle_df,
            veh_input_path=vehicle_file,
        )
        self.mpgge = fastsim_run.mpgge
        self.primary_fuel_range_mi = fastsim_run.range_mi

    def run_range_test(self, vehicle: Vehicle, scenario: Scenario) -> None:
        """
        Runs the range test and updates the energy object with results.
        """
        if self.primary_fuel_range_mi is None:
            self.run_fastsim_model(
                veh_no=vehicle.selection,
                scenario=scenario,
                t3co_vehicle=vehicle,
            )

    def run_acceleration_test(
        self,
        vehicle: Vehicle,
        scenario: Scenario,
        set_weight_to_max_kg: bool = True,
        verbose: bool = False,
    ) -> None:
        """
        Runs the acceleration test and updates the energy object with results.
        """
        if RunFASTSim is None:
            raise ImportError(
                "FASTSim is not installed or could not be imported. Cannot run acceleration test."
            )

        if set_weight_to_max_kg:
            vehicle.veh_kg = scenario.gvwr_kg + scenario.gvwr_credit_kg

        accel_cycle = RunFASTSim.get_accel_cycle()

        fastsim_run = RunFASTSim(
            scenario=scenario,
            t3co_vehicle=vehicle,
            veh_no=vehicle.selection,
            cycle=accel_cycle,
        )

        simdrive = fastsim_run.simdrives

        # Calculate 0-60 and 0-30
        # Logic from accel.py
        def get_time_to_speed(target_mph):
            if (np.array(simdrive.mph_ach) >= target_mph).any():
                return np.interp(
                    x=target_mph,
                    xp=np.array(simdrive.mph_ach),
                    fp=accel_cycle.time_s,
                )
            else:
                return -simdrive.mph_ach[-1]

        self.zero_to_sixty_loaded = get_time_to_speed(60)
        self.zero_to_thirty_loaded = get_time_to_speed(30)

    def run_gradeability_test(
        self,
        vehicle: Vehicle,
        scenario: Scenario,
        set_weight_to_max_kg: bool = True,
        verbose: bool = False,
    ) -> None:
        """
        Runs the gradeability test and updates the energy object with results.
        """
        if RunFASTSim is None:
            raise ImportError(
                "FASTSim is not installed or could not be imported. Cannot run gradeability test."
            )

        if set_weight_to_max_kg:
            vehicle.veh_kg = scenario.gvwr_kg + scenario.gvwr_credit_kg

        # 6% Grade
        grade_6_cycle = RunFASTSim.get_grade_cycle(0.06, scenario)
        fastsim_run_6 = RunFASTSim(
            scenario=scenario,
            t3co_vehicle=vehicle,
            veh_no=vehicle.selection,
            cycle=grade_6_cycle,
        )
        self.grade_6_mph_ach = fastsim_run_6.simdrives.mph_ach[-1]

        # 1.25% Grade
        grade_125_cycle = RunFASTSim.get_grade_cycle(0.0125, scenario)
        fastsim_run_125 = RunFASTSim(
            scenario=scenario,
            t3co_vehicle=vehicle,
            veh_no=vehicle.selection,
            cycle=grade_125_cycle,
        )
        self.grade_1_25_mph_ach = fastsim_run_125.simdrives.mph_ach[-1]

__new__(*args, **kwargs)

Creates a new instance of the Energy class.

Source code in src/t3co/energy_models/energy.py
34
35
36
37
38
39
def __new__(cls, *args, **kwargs):
    """
    Creates a new instance of the Energy class.
    """
    instance = super(Energy, cls).__new__(cls)
    return instance

__init__(mpgge=None, primary_fuel_range_mi=None)

Initializes the Energy instance.

Parameters:

Name Type Description Default
mpgge float

Miles per gallon gasoline equivalent. Defaults to None.

None
primary_fuel_range_mi float

Primary fuel range in miles. Defaults to None.

None
Source code in src/t3co/energy_models/energy.py
41
42
43
44
45
46
47
48
49
50
51
def __init__(self, mpgge: float = None, primary_fuel_range_mi: float = None):
    """
    Initializes the Energy instance.

    Args:
        mpgge (float, optional): Miles per gallon gasoline equivalent. Defaults to None.
        primary_fuel_range_mi (float, optional): Primary fuel range in miles. Defaults to None.
    """
    if mpgge is not None and primary_fuel_range_mi is not None:
        self.mpgge = mpgge
        self.primary_fuel_range_mi = primary_fuel_range_mi

run_fastsim_model(veh_no, scenario, t3co_vehicle=None, vehicle_df=None, vehicle_file=gl.RESOURCES_FOLDERPATH / 'inputs' / 'Demo_FY22_vehicle_model_assumptions.csv')

Runs the FASTSim model to calculate mpgge and primary fuel range.

Parameters:

Name Type Description Default
veh_no int

Vehicle selection number.

required
scenario Scenario

Scenario instance containing configuration data.

required
vehicle_file Union[str, Path]

Vehicle model assumptions input CSV file path. Defaults to gl.RESOURCES_FOLDERPATH / "inputs" / "Demo_FY22_vehicle_model_assumptions.csv".

RESOURCES_FOLDERPATH / 'inputs' / 'Demo_FY22_vehicle_model_assumptions.csv'
Source code in src/t3co/energy_models/energy.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def run_fastsim_model(
    self,
    veh_no: int,
    scenario: Scenario,
    t3co_vehicle: Vehicle = None,
    vehicle_df: pd.DataFrame = None,
    vehicle_file: Union[str, Path] = gl.RESOURCES_FOLDERPATH
    / "inputs"
    / "Demo_FY22_vehicle_model_assumptions.csv",
) -> None:
    """
    Runs the FASTSim model to calculate mpgge and primary fuel range.

    Args:
        veh_no (int): Vehicle selection number.
        scenario (Scenario): Scenario instance containing configuration data.
        vehicle_file (Union[str, Path], optional): Vehicle model assumptions input CSV file path. Defaults to gl.RESOURCES_FOLDERPATH / "inputs" / "Demo_FY22_vehicle_model_assumptions.csv".
    """
    if RunFASTSim is None:
        raise ImportError(
            "FASTSim is not installed or could not be imported. Cannot run FASTSim model."
        )

    fastsim_run = RunFASTSim(
        veh_no=veh_no,
        scenario=scenario,
        t3co_vehicle=t3co_vehicle,
        vehicle_df=vehicle_df,
        veh_input_path=vehicle_file,
    )
    self.mpgge = fastsim_run.mpgge
    self.primary_fuel_range_mi = fastsim_run.range_mi

run_range_test(vehicle, scenario)

Runs the range test and updates the energy object with results.

Source code in src/t3co/energy_models/energy.py
86
87
88
89
90
91
92
93
94
95
def run_range_test(self, vehicle: Vehicle, scenario: Scenario) -> None:
    """
    Runs the range test and updates the energy object with results.
    """
    if self.primary_fuel_range_mi is None:
        self.run_fastsim_model(
            veh_no=vehicle.selection,
            scenario=scenario,
            t3co_vehicle=vehicle,
        )

run_acceleration_test(vehicle, scenario, set_weight_to_max_kg=True, verbose=False)

Runs the acceleration test and updates the energy object with results.

Source code in src/t3co/energy_models/energy.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def run_acceleration_test(
    self,
    vehicle: Vehicle,
    scenario: Scenario,
    set_weight_to_max_kg: bool = True,
    verbose: bool = False,
) -> None:
    """
    Runs the acceleration test and updates the energy object with results.
    """
    if RunFASTSim is None:
        raise ImportError(
            "FASTSim is not installed or could not be imported. Cannot run acceleration test."
        )

    if set_weight_to_max_kg:
        vehicle.veh_kg = scenario.gvwr_kg + scenario.gvwr_credit_kg

    accel_cycle = RunFASTSim.get_accel_cycle()

    fastsim_run = RunFASTSim(
        scenario=scenario,
        t3co_vehicle=vehicle,
        veh_no=vehicle.selection,
        cycle=accel_cycle,
    )

    simdrive = fastsim_run.simdrives

    # Calculate 0-60 and 0-30
    # Logic from accel.py
    def get_time_to_speed(target_mph):
        if (np.array(simdrive.mph_ach) >= target_mph).any():
            return np.interp(
                x=target_mph,
                xp=np.array(simdrive.mph_ach),
                fp=accel_cycle.time_s,
            )
        else:
            return -simdrive.mph_ach[-1]

    self.zero_to_sixty_loaded = get_time_to_speed(60)
    self.zero_to_thirty_loaded = get_time_to_speed(30)

run_gradeability_test(vehicle, scenario, set_weight_to_max_kg=True, verbose=False)

Runs the gradeability test and updates the energy object with results.

Source code in src/t3co/energy_models/energy.py
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def run_gradeability_test(
    self,
    vehicle: Vehicle,
    scenario: Scenario,
    set_weight_to_max_kg: bool = True,
    verbose: bool = False,
) -> None:
    """
    Runs the gradeability test and updates the energy object with results.
    """
    if RunFASTSim is None:
        raise ImportError(
            "FASTSim is not installed or could not be imported. Cannot run gradeability test."
        )

    if set_weight_to_max_kg:
        vehicle.veh_kg = scenario.gvwr_kg + scenario.gvwr_credit_kg

    # 6% Grade
    grade_6_cycle = RunFASTSim.get_grade_cycle(0.06, scenario)
    fastsim_run_6 = RunFASTSim(
        scenario=scenario,
        t3co_vehicle=vehicle,
        veh_no=vehicle.selection,
        cycle=grade_6_cycle,
    )
    self.grade_6_mph_ach = fastsim_run_6.simdrives.mph_ach[-1]

    # 1.25% Grade
    grade_125_cycle = RunFASTSim.get_grade_cycle(0.0125, scenario)
    fastsim_run_125 = RunFASTSim(
        scenario=scenario,
        t3co_vehicle=vehicle,
        veh_no=vehicle.selection,
        cycle=grade_125_cycle,
    )
    self.grade_1_25_mph_ach = fastsim_run_125.simdrives.mph_ach[-1]