import numpy as np import matplotlib.pyplot as plt
time = np.arange(0, 24, 1)
solar_generation = np.maximum(0, 10 * np.sin(np.pi * (time - 6) / 12))
load_demand = 5 + 3 * np.where((time >= 9) & (time <= 17), 1, 0)
battery_capacity = 50 # in kWh battery_charge = 20 # initial charge in kWh battery_charge_rate = 5 # kW (max charge/discharge rate per hour)
battery_storage = np.zeros_like(time) grid_import = np.zeros_like(time) grid_export = np.zeros_like(time)
for t in range(len(time)): # Calculate excess or shortfall of energy excess_energy = solar_generation[t] - load_demand[t]
# Update battery storage
if excess_energy > 0: # excess energy, try to charge battery
charge_amount = min(excess_energy, battery_charge_rate, battery_capacity - battery_charge)
battery_charge += charge_amount
excess_energy -= charge_amount
grid_export[t] = excess_energy # export excess to the grid
else: # shortfall of energy, try to discharge battery
discharge_amount = min(-excess_energy, battery_charge_rate, battery_charge)
battery_charge -= discharge_amount
grid_import[t] = -excess_energy - discharge_amount # import shortfall from the grid
# Store battery state
battery_storage[t] = battery_charge
plt.figure(figsize=(12, 8))
plt.subplot(4, 1, 1) plt.plot(time, solar_generation, label='Solar Generation (kW)', color='orange') plt.ylabel('kW') plt.legend()
plt.subplot(4, 1, 2) plt.plot(time, load_demand, label='Load Demand (kW)', color='red') plt.ylabel('kW') plt.legend()
plt.subplot(4, 1, 3) plt.plot(time, battery_storage, label='Battery Storage (kWh)', color='blue') plt.ylabel('kWh') plt.legend()
plt.subplot(4, 1, 4) plt.plot(time, grid_import, label='Grid Import (kW)', color='green') plt.plot(time, grid_export, label='Grid Export (kW)', color='purple') plt.ylabel('kW') plt.xlabel('Time (hours)') plt.legend()
plt.tight_layout() plt.show()
import numpy as np import matplotlib.pyplot as plt
time = np.arange(0, 24, 1)
solar_generation = np.maximum(0, 10 * np.sin(np.pi * (time - 6) / 12))
wind_generation = 3 + 2 * np.sin(np.pi * (time) / 24 + np.pi/4) + np.random.normal(0, 0.5, len(time))
base_load = np.full_like(time, 4)
variable_load = 2 * np.where((time >= 8) & (time <= 18), np.sin(np.pi * (time - 8) / 10), 0)
peak_load = np.where((time >= 18) & (time <= 21), 3, 0)
load_demand = base_load + variable_load + peak_load
battery_capacity = 50 # in kWh .......................................... .............................................
plt.figure(figsize=(12, 10))
plt.subplot(5, 1, 1) plt.plot(time, solar_generation, label='Solar Generation (kW)', color='orange') plt.ylabel('kW') plt.legend()
plt.subplot(5, 1, 2) plt.plot(time, wind_generation, label='Wind Generation (kW)', color='cyan') plt.ylabel('kW') plt.legend()
plt.subplot(5, 1, 3) plt.plot(time, load_demand, label='Load Demand (kW)', color='red') plt.ylabel('kW') plt.legend()
plt.subplot(5, 1, 4) plt.plot(time, battery_storage, label='Battery Storage (kWh)', color='blue') plt.ylabel('kWh') plt.legend()
plt.subplot(5, 1, 5) plt.plot(time, grid_import, label='Grid Import (kW)', color='green') plt.plot(time, grid_export, label='Grid Export (kW)', color='purple') plt.ylabel('kW') plt.xlabel('Time (hours)') plt.legend()
plt.tight_layout() plt.show() import numpy as np import matplotlib.pyplot as plt
def simulate_microgrid(time, solar_gen, wind_gen, load_demand, battery_capacity, battery_charge_rate): battery_charge = 20 # initial charge in kWh battery_storage = np.zeros_like(time) grid_import = np.zeros_like(time) grid_export = np.zeros_like(time) ............................................ .............................................. ..................................................
plot_results(time, battery_storage, grid_import, grid_export, 'Test Case 1: Normal Operation') plot_results(time, battery_storage_high_renewable, grid_import_high_renewable, grid_export_high_renewable, 'Test Case 2: High Renewable Generation') plot_results(time, battery_storage_low_renewable, grid_import_low_renewable, grid_export_low_renewable, 'Test Case 3: Low Renewable Generation') plot_results(time, battery_storage_peak_load, grid_import_peak_load, grid_export_peak_load, 'Test Case 4: Peak Load Demand') plot_results(time, battery_storage_failure, grid_import_failure, grid_export_failure, 'Test Case 5: Battery Failure') plot_results(time, battery_storage_increased_activity, grid_import_increased_activity, grid_export_increased_activity, 'Test Case 6: Increased Industrial Activity')
plt.figure(figsize=(12, 8)) plt.subplot(3, 1, 1) plt.plot(extended_time, battery_storage_extended, label='Battery Storage (kWh)', color='blue') plt.ylabel('kWh') plt.legend() plt.title('Test Case 7: Extended Cloudy Days')
plt.subplot(3, 1, 2) plt.plot(extended_time, grid_import_extended, label='Grid Import (kW)', color='green') plt.ylabel('kW') plt.legend()
plt.subplot(3, 1, 3) plt.plot(extended_time, grid_export_extended, label='Grid Export (kW)', color='purple') plt.ylabel('kW') plt.xlabel('Time (hours)') plt.legend()
plt.tight_layout() plt.show()
*Optimization and Analysys *