package com.njcn.advance.utils.assess;

/**
 * Description:
 * Date: 2025/05/30  下午 3:32【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
public class VoltageFluctuationUtil {

    /** 
    * @Description: calculateWindFarmVoltageFluctuation
    * @Param: 
     * @param powerFactor 用户真功率因数，默认取0.95
     * @param installedCapacity 评估用户参数表风电场装机容量
     * @param pccResistance 评估用户参数表PCC点电阻分量
     * @param pccReactance 评估用户参数表PCC点电抗分量
     * @param voltageLevel 评估用户参数表母线电压等级
     * @param isPV 是否是风电光伏
    * @return: double 
    * @Author: clam
    * @Date: 2025/06/06 
    */ 
    public static double calculateWindFarmVoltageFluctuation(double powerFactor,
            double installedCapacity,
            double pccResistance, double pccReactance,double voltageLevel,
             boolean isPV) {

        // 1. Calculate active power change
        double deltaP;
        if (isPV) {
            // For PV station, take 10% of rated capacity as power increment
            deltaP = installedCapacity * 0.1;
        } else {
            // For wind farm, get deltaP based on installed capacity
            if (installedCapacity < 30) {
                deltaP = 10;
            } else if (installedCapacity <= 150) {
                deltaP = installedCapacity / 3;
            } else {
                deltaP = 50;
            }
        }

        // 2. Calculate reactive voltage change rate
        double deltaQ = (deltaP / powerFactor) * Math.sqrt(1 - Math.pow(powerFactor, 2));

        // 3. Calculate voltage fluctuation
        return (pccResistance * deltaP + pccReactance * deltaQ) / Math.pow(voltageLevel, 2) * 100;
    }

    // Calculate voltage fluctuation for non-wind farm loads
    public static double calculateImpactLoadVoltageFluctuation(
            double impactLoadCapacity, double startCapacityMultiple,
            double pccShortCircuitCapacity) {

        // 1. Calculate maximum power change
        double deltaSmax = impactLoadCapacity * startCapacityMultiple;

        // 2. Calculate voltage fluctuation
        return (deltaSmax / pccShortCircuitCapacity) * 100;
    }

    // Get voltage fluctuation limit based on voltage level and frequency
    public static double getVoltageFluctuationLimit(
            double voltageLevel, String frequencyCategory) {

        if (voltageLevel <= 35) {
            // For 1kV < UN ≤ 35kV
            switch (frequencyCategory) {
                case "01": return 6.0; // 5%~6%
                case "02": return 4.0;
                case "03": return 3.0;
                default: return 3.0; // For irregular fluctuations like arc furnaces
            }
        } else {
            // For 35kV < UN ≤ 750kV
            switch (frequencyCategory) {
                case "01": return 5.0; // 3%~5%
                case "02": return 3.0;
                case "03": return 2.5;
                default: return 2.5; // For irregular fluctuations like arc furnaces
            }
        }
    }
}
