Skip to content

Commit 26bdb37

Browse files
committed
null and capacity check added to all battery management to prevent null pointers
1 parent 61a05c8 commit 26bdb37

7 files changed

Lines changed: 79 additions & 67 deletions

_alp/Classes/Class.J_BatteryManagementExternalSetpoint.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ public double getChargeSetpoint_kW() {
4444
}
4545

4646
public void manageBattery(J_TimeVariables timeVariables) {
47-
//Manage the battery with the set charge setpoint
48-
gc.f_updateFlexAssetFlows(gc.p_batteryAsset, this.currentChargeSetpoint_kW / gc.p_batteryAsset.getCapacityElectric_kW(), timeVariables);
47+
if(gc.p_batteryAsset != null && gc.p_batteryAsset.getStorageCapacity_kWh() > 0) {
48+
//Manage the battery with the set charge setpoint
49+
gc.f_updateFlexAssetFlows(gc.p_batteryAsset, this.currentChargeSetpoint_kW / gc.p_batteryAsset.getCapacityElectric_kW(), timeVariables);
4950

50-
//Reset the value again.
51-
this.currentChargeSetpoint_kW = 0;
51+
//Reset the value again.
52+
this.currentChargeSetpoint_kW = 0;
53+
}
5254
}
5355

5456

_alp/Classes/Class.J_BatteryManagementOff.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public J_BatteryManagementOff( GridConnection gc, J_TimeParameters timeParameter
3131
}
3232

3333
public void manageBattery(J_TimeVariables timeVariables) {
34-
gc.f_updateFlexAssetFlows(gc.p_batteryAsset, 0.0, timeVariables);
34+
if(gc.p_batteryAsset != null && gc.p_batteryAsset.getStorageCapacity_kWh() > 0) {
35+
gc.f_updateFlexAssetFlows(gc.p_batteryAsset, 0.0, timeVariables);
36+
}
3537
}
3638

3739

_alp/Classes/Class.J_BatteryManagementPeakShaving.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,25 @@ public J_BatteryManagementPeakShaving( GridConnection gc, double SOC_setpoint_fr
5454
* so that it can take the connection capacity of the GC into account and prevent any peaks when they occur.
5555
*/
5656
public void manageBattery(J_TimeVariables timeVariables) {
57-
if (this.target == null) {
58-
gc.f_updateFlexAssetFlows(gc.p_batteryAsset, 0.0, timeVariables);
59-
return;
60-
}
61-
double feedbackGain_kWpSOC = feedbackGain_fr * gc.p_batteryAsset.getCapacityElectric_kW();
62-
double chargeSetpoint_kW = (SOC_setpoint_fr - gc.p_batteryAsset.getCurrentStateOfCharge_fr()) * feedbackGain_kWpSOC;
63-
64-
// Try to stay within the target connection capacity
65-
double v_allowedDeliveryCapacity_kW = getDeliveryCapacity_kW();
66-
double v_allowedFeedinCapacity_kW = getFeedinCapacity_kW();
67-
double balanceElectricity_kW = getBalanceElectricity_kW();
68-
double availableChargePower_kW = v_allowedDeliveryCapacity_kW - balanceElectricity_kW; // Max battery charging power within safety margins
69-
double availableDischargePower_kW = v_allowedFeedinCapacity_kW + balanceElectricity_kW; // Max discharging power within safety margins
70-
71-
chargeSetpoint_kW = min(max(chargeSetpoint_kW, -availableDischargePower_kW),availableChargePower_kW); // Don't allow too much (dis)charging!
72-
73-
gc.f_updateFlexAssetFlows(gc.p_batteryAsset, chargeSetpoint_kW / gc.p_batteryAsset.getCapacityElectric_kW(), timeVariables);
74-
57+
if(gc.p_batteryAsset != null && gc.p_batteryAsset.getStorageCapacity_kWh() > 0) {
58+
if (this.target == null) {
59+
gc.f_updateFlexAssetFlows(gc.p_batteryAsset, 0.0, timeVariables);
60+
return;
61+
}
62+
double feedbackGain_kWpSOC = feedbackGain_fr * gc.p_batteryAsset.getCapacityElectric_kW();
63+
double chargeSetpoint_kW = (SOC_setpoint_fr - gc.p_batteryAsset.getCurrentStateOfCharge_fr()) * feedbackGain_kWpSOC;
64+
65+
// Try to stay within the target connection capacity
66+
double v_allowedDeliveryCapacity_kW = getDeliveryCapacity_kW();
67+
double v_allowedFeedinCapacity_kW = getFeedinCapacity_kW();
68+
double balanceElectricity_kW = getBalanceElectricity_kW();
69+
double availableChargePower_kW = v_allowedDeliveryCapacity_kW - balanceElectricity_kW; // Max battery charging power within safety margins
70+
double availableDischargePower_kW = v_allowedFeedinCapacity_kW + balanceElectricity_kW; // Max discharging power within safety margins
71+
72+
chargeSetpoint_kW = min(max(chargeSetpoint_kW, -availableDischargePower_kW),availableChargePower_kW); // Don't allow too much (dis)charging!
73+
74+
gc.f_updateFlexAssetFlows(gc.p_batteryAsset, chargeSetpoint_kW / gc.p_batteryAsset.getCapacityElectric_kW(), timeVariables);
75+
}
7576
}
7677

7778
public void setTarget( Agent agent ) {

_alp/Classes/Class.J_BatteryManagementPeakShavingForecast.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,17 @@ public J_BatteryManagementPeakShavingForecast( GridConnection parentGC, J_TimePa
4444

4545

4646
public void manageBattery(J_TimeVariables timeVariables) {
47-
if (this.target == null) {
48-
parentGC.f_updateFlexAssetFlows(parentGC.p_batteryAsset, 0.0, timeVariables);
49-
return;
47+
if(parentGC.p_batteryAsset != null && parentGC.p_batteryAsset.getStorageCapacity_kWh() > 0) {
48+
if (this.target == null) {
49+
parentGC.f_updateFlexAssetFlows(parentGC.p_batteryAsset, 0.0, timeVariables);
50+
return;
51+
}
52+
int index = roundToInt(timeVariables.getTimeOfDay_h()/timeParameters.getTimeStep_h());
53+
if(index == 0){
54+
this.batteryChargingSchedule_kW = this.calculateBatteryChargingSchedule(timeVariables);
55+
}
56+
parentGC.f_updateFlexAssetFlows(parentGC.p_batteryAsset, this.batteryChargingSchedule_kW[index] / parentGC.p_batteryAsset.getCapacityElectric_kW(), timeVariables);
5057
}
51-
int index = roundToInt(timeVariables.getTimeOfDay_h()/timeParameters.getTimeStep_h());
52-
if(index == 0){
53-
this.batteryChargingSchedule_kW = this.calculateBatteryChargingSchedule(timeVariables);
54-
}
55-
parentGC.f_updateFlexAssetFlows(parentGC.p_batteryAsset, this.batteryChargingSchedule_kW[index] / parentGC.p_batteryAsset.getCapacityElectric_kW(), timeVariables);
5658
}
5759

5860

_alp/Classes/Class.J_BatteryManagementPrice.java

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,37 +56,38 @@ public J_BatteryManagementPrice( GridConnection gc, J_TimeParameters timeParamet
5656
* It has a boolean flag wether or not to take the GC's connection capacity into account.
5757
*/
5858
public void manageBattery(J_TimeVariables timeVariables) {
59-
// Get the national EPEX price
60-
double currentElectricityPriceCharge_eurpkWh = gc.energyModel.nationalEnergyMarket.f_getNationalElectricityPrice_eurpMWh()/1000;
61-
62-
// Base the WTP on a moving average price and the SOC
63-
electricityPriceLowPassed_eurpkWh += lowPassFactor_fr * ( currentElectricityPriceCharge_eurpkWh - electricityPriceLowPassed_eurpkWh );
64-
65-
double SOC_setpoint_fr = 0.5;
66-
double SOC_deficit_fr = SOC_setpoint_fr - gc.p_batteryAsset.getCurrentStateOfCharge_fr(); // How far away from desired SOC? SOC too LOW is a POSITIVE deficit
67-
68-
// Define WTP price for charging and discharging!
69-
double WTP_charge_eurpkWh = electricityPriceLowPassed_eurpkWh - chargeDischarge_offset_eurpkWh + SOC_deficit_fr * WTPfeedbackGain_eurpSOC;
70-
double WTP_discharge_eurpkWh = electricityPriceLowPassed_eurpkWh + chargeDischarge_offset_eurpkWh + SOC_deficit_fr * WTPfeedbackGain_eurpSOC;
71-
72-
// Choose charging power based on prices and desired SOC level
73-
double chargeSetpoint_kW = 0;
74-
if ( WTP_charge_eurpkWh > currentElectricityPriceCharge_eurpkWh ) {
75-
chargeSetpoint_kW = gc.p_batteryAsset.getCapacityElectric_kW()*(WTP_charge_eurpkWh - currentElectricityPriceCharge_eurpkWh)*priceGain_kWhpeur;
76-
}
77-
else if (WTP_discharge_eurpkWh < currentElectricityPriceCharge_eurpkWh) {
78-
chargeSetpoint_kW = -gc.p_batteryAsset.getCapacityElectric_kW()*(currentElectricityPriceCharge_eurpkWh - WTP_discharge_eurpkWh)*priceGain_kWhpeur;
79-
}
80-
81-
// limit charging power to available connection capacity
82-
if( stayWithinConnectionLimits ) {
83-
double availableChargePower_kW = gc.v_liveConnectionMetaData.contractedDeliveryCapacity_kW - gc.fm_currentBalanceFlows_kW.get(OL_EnergyCarriers.ELECTRICITY); // Max battery charging power within grid capacity
84-
double availableDischargePower_kW = gc.v_liveConnectionMetaData.contractedFeedinCapacity_kW + gc.fm_currentBalanceFlows_kW.get(OL_EnergyCarriers.ELECTRICITY); // Max discharging power within grid capacity
85-
chargeSetpoint_kW = min(max(chargeSetpoint_kW, -availableDischargePower_kW),availableChargePower_kW); // Don't allow too much (dis)charging!
86-
}
87-
88-
gc.f_updateFlexAssetFlows(gc.p_batteryAsset, chargeSetpoint_kW / gc.p_batteryAsset.getCapacityElectric_kW(), timeVariables);
89-
59+
if(gc.p_batteryAsset != null && gc.p_batteryAsset.getStorageCapacity_kWh() > 0) {
60+
// Get the national EPEX price
61+
double currentElectricityPriceCharge_eurpkWh = gc.energyModel.nationalEnergyMarket.f_getNationalElectricityPrice_eurpMWh()/1000;
62+
63+
// Base the WTP on a moving average price and the SOC
64+
electricityPriceLowPassed_eurpkWh += lowPassFactor_fr * ( currentElectricityPriceCharge_eurpkWh - electricityPriceLowPassed_eurpkWh );
65+
66+
double SOC_setpoint_fr = 0.5;
67+
double SOC_deficit_fr = SOC_setpoint_fr - gc.p_batteryAsset.getCurrentStateOfCharge_fr(); // How far away from desired SOC? SOC too LOW is a POSITIVE deficit
68+
69+
// Define WTP price for charging and discharging!
70+
double WTP_charge_eurpkWh = electricityPriceLowPassed_eurpkWh - chargeDischarge_offset_eurpkWh + SOC_deficit_fr * WTPfeedbackGain_eurpSOC;
71+
double WTP_discharge_eurpkWh = electricityPriceLowPassed_eurpkWh + chargeDischarge_offset_eurpkWh + SOC_deficit_fr * WTPfeedbackGain_eurpSOC;
72+
73+
// Choose charging power based on prices and desired SOC level
74+
double chargeSetpoint_kW = 0;
75+
if ( WTP_charge_eurpkWh > currentElectricityPriceCharge_eurpkWh ) {
76+
chargeSetpoint_kW = gc.p_batteryAsset.getCapacityElectric_kW()*(WTP_charge_eurpkWh - currentElectricityPriceCharge_eurpkWh)*priceGain_kWhpeur;
77+
}
78+
else if (WTP_discharge_eurpkWh < currentElectricityPriceCharge_eurpkWh) {
79+
chargeSetpoint_kW = -gc.p_batteryAsset.getCapacityElectric_kW()*(currentElectricityPriceCharge_eurpkWh - WTP_discharge_eurpkWh)*priceGain_kWhpeur;
80+
}
81+
82+
// limit charging power to available connection capacity
83+
if( stayWithinConnectionLimits ) {
84+
double availableChargePower_kW = gc.v_liveConnectionMetaData.contractedDeliveryCapacity_kW - gc.fm_currentBalanceFlows_kW.get(OL_EnergyCarriers.ELECTRICITY); // Max battery charging power within grid capacity
85+
double availableDischargePower_kW = gc.v_liveConnectionMetaData.contractedFeedinCapacity_kW + gc.fm_currentBalanceFlows_kW.get(OL_EnergyCarriers.ELECTRICITY); // Max discharging power within grid capacity
86+
chargeSetpoint_kW = min(max(chargeSetpoint_kW, -availableDischargePower_kW),availableChargePower_kW); // Don't allow too much (dis)charging!
87+
}
88+
89+
gc.f_updateFlexAssetFlows(gc.p_batteryAsset, chargeSetpoint_kW / gc.p_batteryAsset.getCapacityElectric_kW(), timeVariables);
90+
}
9091
}
9192

9293

_alp/Classes/Class.J_BatteryManagementSelfConsumption.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public J_BatteryManagementSelfConsumption( GridConnection gc, J_TimeParameters t
3737
* If there is more consumption than production it will discharge the battery to make up for the difference untill the battery is empty.
3838
*/
3939
public void manageBattery(J_TimeVariables timeVariables) {
40-
gc.f_updateFlexAssetFlows(gc.p_batteryAsset, -gc.fm_currentBalanceFlows_kW.get(OL_EnergyCarriers.ELECTRICITY) / gc.p_batteryAsset.getCapacityElectric_kW(), timeVariables);
40+
if(gc.p_batteryAsset != null && gc.p_batteryAsset.getStorageCapacity_kWh() > 0) {
41+
gc.f_updateFlexAssetFlows(gc.p_batteryAsset, -gc.fm_currentBalanceFlows_kW.get(OL_EnergyCarriers.ELECTRICITY) / gc.p_batteryAsset.getCapacityElectric_kW(), timeVariables);
42+
}
4143
}
4244

4345

_alp/Classes/Class.J_BatteryManagementSelfConsumptionGridNode.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ public J_BatteryManagementSelfConsumptionGridNode( GridConnection gc, J_TimePara
3737
* If there is more consumption than production it will discharge the battery to make up for the difference untill the battery is empty.
3838
*/
3939
public void manageBattery(J_TimeVariables timeVariables) {
40-
double nodePreviousLoad_kW = gc.p_parentNodeElectric.v_currentLoad_kW;
41-
double chargeSetpoint_kW = -(nodePreviousLoad_kW - gc.p_batteryAsset.getLastFlows().get(OL_EnergyCarriers.ELECTRICITY));
42-
43-
gc.f_updateFlexAssetFlows(gc.p_batteryAsset, chargeSetpoint_kW / gc.p_batteryAsset.getCapacityElectric_kW(), timeVariables);
40+
if(gc.p_batteryAsset != null && gc.p_batteryAsset.getStorageCapacity_kWh() > 0) {
41+
double nodePreviousLoad_kW = gc.p_parentNodeElectric.v_currentLoad_kW;
42+
double chargeSetpoint_kW = -(nodePreviousLoad_kW - gc.p_batteryAsset.getLastFlows().get(OL_EnergyCarriers.ELECTRICITY));
43+
44+
gc.f_updateFlexAssetFlows(gc.p_batteryAsset, chargeSetpoint_kW / gc.p_batteryAsset.getCapacityElectric_kW(), timeVariables);
45+
}
4446
}
4547

4648

0 commit comments

Comments
 (0)