python Copy code
import tkinter as tk
from tkinter import ttk, messagebox
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import networkx as nx
import matplotlib.animation as animation
import os
class BatteryStorageBlockDiagram:
def __init__(self, root):
self.root = root
self.root.title("Battery Storage System Process Diagram")
self.root.geometry("1200x800")
# Create the main frames
self.control_frame = ttk.Frame(root, padding="10")
self.control_frame.pack(side=tk.TOP, fill=tk.X)
self.diagram_frame = ttk.Frame(root)
self.diagram_frame.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
# Initialize the graph
self.fig = plt.figure(figsize=(12, 8))
self.ax = self.fig.add_subplot(111)
# Create the diagram canvas
self.canvas = FigureCanvasTkAgg(self.fig, self.diagram_frame)
self.canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True)
# Create controls
self.setup_controls()
# Initialize the graph components
self.G = nx.DiGraph()
self.setup_blocks()
self.current_step = 0
self.animation = None
# Draw the initial state
self.draw_graph()
def setup_controls(self):
# Phase selection
ttk.Label(self.control_frame, text="Select Phase:").pack(side=tk.LEFT, padx=5)
self.phase_var = tk.StringVar(value="Planning")
phases = ["Planning", "Building", "Operation"]
phase_menu = ttk.Combobox(self.control_frame, textvariable=self.phase_var, values=phases)
phase_menu.pack(side=tk.LEFT, padx=5)
phase_menu.bind("<<ComboboxSelected>>", self.change_phase)
# Animation controls
ttk.Button(self.control_frame, text="Start Animation", command=self.start_animation).pack(side=tk.LEFT, padx=5)
ttk.Button(self.control_frame, text="Stop Animation", command=self.stop_animation).pack(side=tk.LEFT, padx=5)
ttk.Button(self.control_frame, text="Reset", command=self.reset_animation).pack(side=tk.LEFT, padx=5)
# Step controls
ttk.Button(self.control_frame, text="Next Step", command=self.next_step).pack(side=tk.LEFT, padx=5)
ttk.Button(self.control_frame, text="Previous Step", command=self.prev_step).pack(side=tk.LEFT, padx=5)
# Save button
ttk.Button(self.control_frame, text="Save Diagram", command=self.save_diagram).pack(side=tk.RIGHT, padx=5)
# Description area
self.description_frame = ttk.LabelFrame(self.root, text="Step Description", padding="10")
self.description_frame.pack(side=tk.TOP, fill=tk.X, padx=10, pady=5)
self.description_text = tk.Text(self.description_frame, height=4, wrap=tk.WORD)
self.description_text.pack(fill=tk.X, expand=True)
self.description_text.config(state=tk.DISABLED)
def setup_blocks(self):
# Clear any existing graph
self.G.clear()
# Add nodes for each entity with positions
entities = {
"BMWK": {"pos": (0, 0), "color": "lightblue", "size": 1500},
"Bundesnetzagentur": {"pos": (1, 0), "color": "lightgreen", "size": 2000},
"Lokale Behörden": {"pos": (2, 0), "color": "lightgreen", "size": 1500},
"Finanzierung": {"pos": (0, -1), "color": "yellow", "size": 1500},
"EPC": {"pos": (1, -1), "color": "orange", "size": 1500},
"Optimierer": {"pos": (2, -1), "color": "pink", "size": 1500},
"Batterie-Betreiber": {"pos": (0, -2), "color": "lightgray", "size": 1500},
"Stromnetz": {"pos": (1, -2), "color": "skyblue", "size": 1500},
"Marktplatz": {"pos": (2, -2), "color": "lightyellow", "size": 1500},
}
# Add nodes to the graph
for entity, attrs in entities.items():
self.G.add_node(entity, pos=attrs["pos"], color=attrs["color"], size=attrs["size"])
# Define the phases and steps
self.phases = {
"Planning": [
{
"description": "BMWK sets energy storage policy and incentives",
"edges": [("BMWK", "Bundesnetzagentur")],
"active_nodes": ["BMWK"]
},
{
"description": "Bundesnetzagentur provides regulatory framework",
"edges": [("Bundesnetzagentur", "Lokale Behörden"), ("Bundesnetzagentur", "Batterie-Betreiber")],
"active_nodes": ["BMWK", "Bundesnetzagentur"]
},
{
"description": "Batterie-Betreiber applies for permits from local authorities",
"edges": [("Batterie-Betreiber", "Lokale Behörden")],
"active_nodes": ["BMWK", "Bundesnetzagentur", "Batterie-Betreiber", "Lokale Behörden"]
},
{
"description": "Financing is secured for the project",
"edges": [("Finanzierung", "Batterie-Betreiber")],
"active_nodes": ["BMWK", "Bundesnetzagentur", "Batterie-Betreiber", "Lokale Behörden", "Finanzierung"]
}
],
"Building": [
{
"description": "Batterie-Betreiber contracts EPC company",
"edges": [("Batterie-Betreiber", "EPC")],
"active_nodes": ["Batterie-Betreiber", "EPC"]
},
{
"description": "EPC begins construction and system integration",
"edges": [("EPC", "Stromnetz")],
"active_nodes": ["Batterie-Betreiber", "EPC", "Stromnetz"]
},
{
"description": "Optimierer system is implemented for energy management",
"edges": [("EPC", "Optimierer"), ("Optimierer", "Batterie-Betreiber")],
"active_nodes": ["Batterie-Betreiber", "EPC", "Stromnetz", "Optimierer"]
},
{
"description": "Final grid connection and system testing",
"edges": [("Stromnetz", "Bundesnetzagentur"), ("Optimierer", "Stromnetz")],
"active_nodes": ["Batterie-Betreiber", "EPC", "Stromnetz", "Optimierer", "Bundesnetzagentur"]
}
],
"Operation": [
{
"description": "System begins standard operation with grid connection",
"edges": [("Batterie-Betreiber", "Stromnetz"), ("Stromnetz", "Batterie-Betreiber")],
"active_nodes": ["Batterie-Betreiber", "Stromnetz", "Optimierer"]
},
{
"description": "Optimierer manages energy trading on the market",
"edges": [("Optimierer", "Marktplatz"), ("Marktplatz", "Optimierer")],
"active_nodes": ["Batterie-Betreiber", "Stromnetz", "Optimierer", "Marktplatz"]
},
{
"description": "Grid services and balancing power are provided",
"edges": [("Batterie-Betreiber", "Bundesnetzagentur"), ("Optimierer", "Stromnetz")],
"active_nodes": ["Batterie-Betreiber", "Stromnetz", "Optimierer", "Marktplatz", "Bundesnetzagentur"]
},
{
"description": "Regulatory compliance reporting and monitoring",
"edges": [("Batterie-Betreiber", "BMWK"), ("Bundesnetzagentur", "BMWK")],
"active_nodes": ["Batterie-Betreiber", "Stromnetz", "Optimierer", "Marktplatz", "Bundesnetzagentur", "BMWK"]
}
]
}
self.current_phase = "Planning"
self.current_step = 0
def draw_graph(self):
self.ax.clear()
# Get current phase and step
phase_data = self.phases[self.current_phase]
current_step_data = phase_data[self.current_step]
# Get node positions
pos = nx.get_node_attributes(self.G, 'pos')
# Draw all nodes with lower alpha if not active
node_colors = []
node_sizes = []
active_nodes = current_step_data["active_nodes"]
for node in self.G.nodes():
if node in active_nodes:
node_colors.append(self.G.nodes[node]["color"])
node_sizes.append(self.G.nodes[node]["size"])
else:
# Make inactive nodes more transparent and smaller
node_colors.append(self.G.nodes[node]["color"])
node_sizes.append(self.G.nodes[node]["size"] * 0.7)
# Draw nodes
nx.draw_networkx_nodes(
self.G, pos,
node_color=node_colors,
node_size=node_sizes,
alpha=[1 if node in active_nodes else 0.3 for node in self.G.nodes()]
)
# Draw node labels
nx.draw_networkx_labels(
self.G, pos,
font_size=10,
font_weight="bold",
alpha=[1 if node in active_nodes else 0.3 for node in self.G.nodes()]
)
# Draw edges for the current step
edges = current_step_data["edges"]
nx.draw_networkx_edges(
self.G, pos,
edgelist=edges,
width=2,
alpha=0.7,
edge_color="blue",
arrows=True,
arrowsize=20,
connectionstyle="arc3,rad=0.2"
)
# Update description
self.description_text.config(state=tk.NORMAL)
self.description_text.delete(1.0, tk.END)
self.description_text.insert(tk.END, f"Phase: {self.current_phase}\nStep {self.current_step + 1}/{len(phase_data)}: {current_step_data['description']}")
self.description_text.config(state=tk.DISABLED)
# Set title and adjust layout
self.ax.set_title(f"Battery Storage System: {self.current_phase} Phase - Step {self.current_step + 1}")
self.ax.axis('off')
self.canvas.draw()
def change_phase(self, event=None):
self.current_phase = self.phase_var.get()
self.current_step = 0
self.draw_graph()
def next_step(self):
phase_data = self.phases[self.current_phase]
if self.current_step < len(phase_data) - 1:
self.current_step += 1
self.draw_graph()
else:
messagebox.showinfo("End of Phase", "This is the last step of the current phase.")
def prev_step(self):
if self.current_step > 0:
self.current_step -= 1
self.draw_graph()
else:
messagebox.showinfo("Start of Phase", "This is the first step of the current phase.")
def update_animation(self, frame):
phase_data = self.phases[self.current_phase]
self.current_step = frame % len(phase_data)
self.draw_graph()
return []
def start_animation(self):
if self.animation is None:
phase_data = self.phases[self.current_phase]
self.animation = animation.FuncAnimation(
self.fig, self.update_animation, frames=len(phase_data),
interval=2000, blit=True, repeat=True
)
self.canvas.draw()
def stop_animation(self):
if self.animation is not None:
self.animation.event_source.stop()
def reset_animation(self):
if self.animation is not None:
self.animation.event_source.stop()
self.animation = None
self.current_step = 0
self.draw_graph()
def save_diagram(self):
filename = f"battery_storage_{self.current_phase}_step{self.current_step+1}.png"
self.fig.savefig(filename, dpi=300, bbox_inches='tight')
messagebox.showinfo("Save Successful", f"Diagram saved as {filename}")
def main():
root = tk.Tk()
app = BatteryStorageBlockDiagram(root)
root.mainloop()
if __name__ == "__main__":
main()
To turn this into an executable file for Windows, you'll need to install PyInstaller. Here's how to do it:
This program creates an interactive block diagram showing the relationships between entities involved in planning, building, and operating a large battery storage system in Germany. You can switch between phases, step through the process manually, or run an animation to see how the interactions evolve.