Code:how-much-energy-storage.c

From the change wiki
Revision as of 17:41, 30 December 2024 by Elie (talk | contribs) (Created page with "{{mbox|This code is currently just a '''draft'''. It hasn't been tested. It hasn't even been run once. It doesn't do anything yet. It's just a prototype of the analysis we're going to need for research page: How much seasonal energy storage?.}} <syntaxhighlight lang="c"> #define N 8760 typedef double num; // number // input parameters to tweak num supply_demand_ratio = 2; num solar_wind_ratio = 1; num battery_capacity = 1; // TODO: change this default val...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This code is currently just a draft. It hasn't been tested. It hasn't even been run once. It doesn't do anything yet. It's just a prototype of the analysis we're going to need for research page: How much seasonal energy storage?.

#define N 8760
typedef double num; // number

// input parameters to tweak
num supply_demand_ratio = 2;
num solar_wind_ratio    = 1;
num battery_capacity        = 1; // TODO: change this default value to something meaningful
num charge_efficiency       = 0.85;
num electrolysis_efficiency = 0.79;

// input data
num wind[N];
num solar[N];
num base_demand[N];
num heat_demand[N];

// output data
num battery_charge[N];
num hydrogen_stock[N];
num non_renewable_heating[N]    = {0};
num non_renewable_electricity[N]= {0};

int main() {
 // TODO: load data from files into arrays
 // ...
 // init variables
 num charge = 0;
 num hydrogen = 0;
 // precalculate variables
 num sum_wind=0; for(int i=0;i<N;i++) sum_wind += wind[i];
 num sum_solar=0;for(int i=0;i<N;i++) sum_solar+= solar[i];
 num sum_base=0; for(int i=0;i<N;i++) sum_base += base_demand[i];
 num sum_heat=0; for(int i=0;i<N;i++) sum_heat += heat_demand[i];
 num wind_scale = (sum_base + sum_heat) / sum_wind  * supply_demand_ratio / (1 + solar_wind_ratio);
 num solar_scale= (sum_base + sum_heat) / sum_solar * supply_demand_ratio / (1 + solar_wind_ratio) * solar_wind_ratio;
 num undo_charge_efficiency = 1/charge_efficiency;
 // main loop
 for (int i=0; i<N; i++) {
  num heating = heat_demand[i];
  num electricity_supply = solar[i]*solar_scale + wind[i]*wind_scale;
  // 1st priority: meet base demand
  num diff = electricity_supply - base_demand[i];
  if (diff > 0) {
   // 2nd priority: charge battery
   charge += diff * charge_efficiency;
   if (charge > battery_capacity) {
    diff = (charge - battery_capacity) * undo_charge_efficiency;
    charge = battery_capacity;
    // 3rd priority: use surplus electricity for heating
    heating -= diff;
    if (heating < 0) {
     hydrogen += -heating * electrolysis_efficiency; // 4th priority: produce hydrogen gas
     heating = 0;
    }
   }
  }
  else {  // diff <= 0:
   // take from battery: meet the rest of base demand
   charge += diff;
   if (charge < 0) {
    non_renewable_electricity[i] = -charge; // renewables failed, battery dead
    charge = 0;
   }
  }
  // burn hydrogen for heating if needed
  hydrogen -= heating;
  if (hydrogen < 0) {
   non_renewable_heating[i] = -hydrogen; // renewables failed, ran out of hydrogen
   hydrogen = 0;
  }
  // save the result
  battery_charge[i] = charge;
  hydrogen_stock[i] = hydrogen;
 }
 // TODO: output the results
 // ...
 return 0;
}