Skip to content

Commit

Permalink
Refactor diesel tractor
Browse files Browse the repository at this point in the history
I am aware that this is nitpicking and doesn't solve any fundamental
issue.
  • Loading branch information
Erikvv committed Feb 3, 2025
1 parent 48b04ed commit 7f12147
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 59 deletions.
3 changes: 1 addition & 2 deletions _alp/Agents/GridConnection/Code/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ else if (j_ea instanceof J_EAConversionCurtailer || j_ea instanceof J_EAConversi
c_consumptionAssets.forEach(c -> c.f_updateAllFlows(0));
c_productionAssets.forEach(p -> p.f_updateAllFlows(0));
c_profileAssets.forEach(p -> p.f_updateAllFlows(energyModel.t_h));
c_tractorAssets.forEach(p -> p.f_updateAllFlows(energyModel.t_h));
/*ALCODEEND*/}

double f_resetStates()
Expand Down Expand Up @@ -1243,7 +1242,7 @@ else if (j_ea.energyAssetType == OL_EnergyAssetType.CHP) {
traceln( "Unrecognized profile type!");
}
} else if (j_ea instanceof J_EADieselTractor) {
c_tractorAssets.add(j_ea);
c_profileAssets.add(j_ea);
} else {
traceln("Unrecognized energy asset %s in gridconnection %s", j_ea, this);
}
Expand Down
20 changes: 1 addition & 19 deletions _alp/Agents/GridConnection/Variables.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5875,7 +5875,7 @@
<ShowLabel>true</ShowLabel>
<Properties SaveInSnapshot="true" AccessType="public" StaticVariable="false">
<CollectionClass>ArrayList</CollectionClass>
<ElementClass>J_EAProfile</ElementClass>
<ElementClass>J_EA</ElementClass>
<ValueElementClass>String</ValueElementClass>
</Properties>
</Variable>
Expand Down Expand Up @@ -6137,22 +6137,4 @@
<ValueElementClass>String</ValueElementClass>
</Properties>
</Variable>
<Variable Class="CollectionVariable">
<Id>1737642450668</Id>
<Name><![CDATA[c_tractorAssets]]></Name>
<X>600</X>
<Y>250</Y>
<Label>
<X>10</X>
<Y>0</Y>
</Label>
<PublicFlag>false</PublicFlag>
<PresentationFlag>true</PresentationFlag>
<ShowLabel>true</ShowLabel>
<Properties SaveInSnapshot="true" AccessType="public" StaticVariable="false">
<CollectionClass>ArrayList</CollectionClass>
<ElementClass>J_EA</ElementClass>
<ValueElementClass>String</ValueElementClass>
</Properties>
</Variable>
</Variables>
119 changes: 81 additions & 38 deletions _alp/Classes/Class.J_EADieselTractor.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
import java.util.*;

/**
* J_EADieselTractor
*/
public class J_EADieselTractor extends J_EA implements Serializable {
Double[] dieselConsumptionPerWeek;
Double yearlyDieselConsumption_l;

/**
* Default constructor
*/
public J_EADieselTractor() {
}

final static double DIESEL_ENERGY_DENSITY_KWH_PER_L = 9.7;

final double[] dieselConsumptionPerWeek_L;
final double workDayStart_h = 6;
final double workDayEnd_h = 17;

/**
* Constructor initializing the fields
* @param parentAgent
* @param yearlyDieselConsumption_l diesel consumption of a single tractor for a whole year
* @param dieselConsumptionPerWeek profile of a year of diesel consumption.
* Usually expressed in L per ha per week for a specific crop or mix of crops.
* For our purpose the unit doesn't matter.
* @param timeStep_h
*/
public J_EADieselTractor(Agent parentAgent, Double yearlyDieselConsumption_l, Double[] dieselConsumptionPerWeek, double timeStep_h) {
public J_EADieselTractor(Agent parentAgent, double yearlyDieselConsumption_L, double[] dieselConsumptionPerWeek, double timeStep_h) {
if (parentAgent == null) {
throw new RuntimeException("Diesel tractor missing parent agent");
}

if (yearlyDieselConsumption_L <= 100.0) {
throw new RuntimeException(
String.format("Diesel tractor fuel usage conspicuously low: %d L", yearlyDieselConsumption_L)
);
}

if (dieselConsumptionPerWeek == null) {
throw new RuntimeException("Tractor diesel consumption profile is null");
}

if (dieselConsumptionPerWeek.length != 52) {
throw new RuntimeException(
String.format("Tractor diesel consumption profile has %d weeks instead of 52", dieselConsumptionPerWeek.length)
);
}

if (timeStep_h <= 0.0) {
throw new RuntimeException("Tractor timestep is off");
}

this.parentAgent = parentAgent;
this.yearlyDieselConsumption_l = yearlyDieselConsumption_l;
this.dieselConsumptionPerWeek = dieselConsumptionPerWeek;
this.dieselConsumptionPerWeek_L = calculateDieselConsumptionPerWeek_L(yearlyDieselConsumption_L, dieselConsumptionPerWeek);
this.timestep_h = timeStep_h;

this.activeConsumptionEnergyCarriers.add(OL_EnergyCarriers.DIESEL);
Expand All @@ -39,39 +61,60 @@ public void f_updateAllFlows(double t_h) {

@Override
public void operate(double t_h) {
double timeOfDay = t_h % 24;
if (timeOfDay < 6 || timeOfDay > 17) {
if (!shouldWork(t_h)) {
this.flowsMap.clear();
return;
}
if (parentAgent instanceof GridConnection) {
if (!((GridConnection)parentAgent).energyModel.b_isWeekday) {
this.flowsMap.clear();
return;
}
}
// TODO: Extract this calculation from operate and only do this once a week and store dieselPerTimeStep
int week = (int)(t_h / 168);
double weeklyDieselConsumption_l = this.dieselConsumptionPerWeek[week] / Arrays.stream(dieselConsumptionPerWeek).mapToDouble(f -> f.doubleValue()).sum() * this.yearlyDieselConsumption_l;
double weeklyDieselConsumption_kWh = weeklyDieselConsumption_l * 9.7;
int totalWorkTimeSteps = roundToInt(5 * (17 - 6) / this.timestep_h);
double dieselPerTimeStep_kW = weeklyDieselConsumption_l / totalWorkTimeSteps;

this.flowsMap.put(OL_EnergyCarriers.DIESEL, dieselPerTimeStep_kW);
this.energyUse_kW = dieselPerTimeStep_kW;
this.energyUsed_kWh += this.energyUse_kW * this.timestep_h;
double currentPower_kW = currentPower_kW(t_h);

this.flowsMap.put(OL_EnergyCarriers.DIESEL, currentPower_kW);
this.energyUse_kW = currentPower_kW;
this.energyUsed_kWh += currentPower_kW * timestep_h;
}

private static double[] calculateDieselConsumptionPerWeek_L(double yearlyDieselConsumption_l, double[] weekProfile) {
var profileSum = Arrays.stream(weekProfile).sum();

return Arrays.stream(weekProfile)
.map(weekValue -> yearlyDieselConsumption_l * weekValue / profileSum)
.toArray();
}

@Override
public String toString() {
return super.toString();
private boolean shouldWork(double currentStep_h) {
return isWorkTime(currentStep_h) && isWorkDay();
}

private boolean isWorkTime(double currentStep_h) {
double timeOfDay = currentStep_h % 24;

return timeOfDay >= workDayStart_h && timeOfDay < workDayEnd_h;
}

private boolean isWorkDay() {
return ((GridConnection)parentAgent).energyModel.b_isWeekday;
}

private double workHoursPerWeek() {
return 5 * (workDayEnd_h - workDayStart_h);
}

private int workTimeStepsPerWeek() {
return roundToInt(workHoursPerWeek() / this.timestep_h);
}

private double currentPower_kW(double currentStep_h) {
int week = (int) Math.round(currentStep_h / (7 * 24));

double thisWeekDieselConsumption_L = this.dieselConsumptionPerWeek_L[week];
double thisWeekDieselConsumption_kWh = thisWeekDieselConsumption_L * DIESEL_ENERGY_DENSITY_KWH_PER_L;
double power_kW = thisWeekDieselConsumption_kWh / workHoursPerWeek();
return power_kW;
}

/**
* This number is here for model snapshot storing purpose<br>
* It needs to be changed when this class gets changed
*/
private static final long serialVersionUID = 1L;

}
}

0 comments on commit 7f12147

Please sign in to comment.