# Project Context: Wind Farm Layout Optimization

**Last Updated:** 2025-12-30
**Project Path:** `D:\code\windfarm`
**Current Goal:** Optimize offshore wind farm cable layout using MST and K-means algorithms, with realistic constraints and CAD export.

## 1. System Overview
The system simulates and designs the collection system (inter-array cables) for an offshore wind farm.
It compares two main algorithms:
1.  **MST (Minimum Spanning Tree)**: Global optimization of cable length. (Note: Often creates overloaded branches in large farms).
2.  **Sector Clustering (K-means)**: Angular clustering to divide turbines into radial "strings" or "loops" feeding the substation. This is the preferred method for large offshore farms to ensure cable capacity constraints are met.

## 2. Key Implementations

### A. Data Handling
- **Generation**: Can generate random or grid layouts.
- **Import**: Supports reading coordinates from `coordinates.xlsx` (Columns: Type, ID, X, Y, Power).
- **Units**: 
    - Power in **MW** (input).
    - Coordinates in **meters**.
    - Voltage: **66 kV** (Code constant `VOLTAGE_LEVEL`).

### B. Algorithms
- **Angular K-means**: 
    - Uses `(cosθ, sinθ)` of the angle relative to substation for clustering.
    - Eliminates cable crossings between sectors.
- **Dynamic Cluster Sizing**: 
    - Automatically calculates the required number of clusters (feeders) based on: `Total_Power / Max_Cable_Capacity`.
    - Ensures no string exceeds the thermal limit of the largest available cable.

### C. Electrical Modeling
- **Cable Sizing**: Selects from standard cross-sections (35mm² to 400mm²).
- **Constraint**: Max cable capacity (400mm²) is approx. **50.4 MW** at 66kV/0.95PF.
- **Loss Calc**: $I^2 R$ losses.

### D. Visualization & Export
- **Matplotlib**: Shows layout with color-coded cables (Green=Thin -> Red=Thick).
- **DXF Export**: Uses `ezdxf` to generate `.dxf` files compatible with CAD.
    - Layers: `Substation`, `Turbines`, `Cable_XXmm`.
    - entities: Circles (Turbines), Polylines (Substation), Lines (Cables).

## 3. Critical Logic & Constants
- **Voltage**: 66,000 V
- **Power Factor**: 0.95
- **Max Current (400mm²)**: 580 A * 0.8 (derating) = 464 A.
- **Unit Conversion**: Critical fix applied to convert MW to Watts for current calculation (`power * 1e6`).

## 4. Current State & file Structure
- `main.py`: Core logic.
- `coordinates.xlsx`: Input data (if present).
- `wind_farm_design_imported.png`: Latest visualization.
- `wind_farm_design.dxf`: Latest CAD export.

## 5. Known Behaviors
- **MST Method**: Will report extremely high costs/losses for large farms because it creates a single tree structure that massively overloads the root cables. This is expected behavior (physically invalid but mathematically correct for unconstrained MST).
- **K-means Method**: Produces realistic, valid designs with appropriate cable tapering (e.g., 400mm² at root, 35mm² at leaves).

## 6. Future Improvements (Optional)
- **Obstacle Avoidance**: Currently assumes open ocean.
- **Loop Topology**: Current design is radial strings. Reliability could be improved with loop/ring structures.
- **Substation Placement Optimization**: Currently fixed or calculated as centroid.
