XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
namespace XFEEDToolBox.Core.Navigation;

/// <summary>Native energy units and simulation seconds. HeatPercent is HUD percent; cooling thresholds are internal fractions.</summary>
public sealed record ScoThermalInput
{
    public bool Verified{get;init;}
    public double HeatPercent{get;init;}
    public double HeatCapacity{get;init;}
    public double HeatDisplayScale{get;init;}
    public double ScoInputUpper{get;init;}
    public double OtherInputUpper{get;init;}
    public double CoolingPassive{get;init;}
    public double CoolingMaximum{get;init;}
    public double CoolingScale{get;init;}
    public double CoolingHeatLower{get;init;}
    public double CoolingHeatUpper{get;init;}
    public double SourceAgeSeconds{get;init;}
    public double HeatAgeSeconds{get;init;}
    public double LeaseRemainingSeconds{get;init;}
    public double ResponseDelaySeconds{get;init;}
    public double AfterStopSeconds{get;init;}
    public double HeatUncertaintyPercent{get;init;}
}

public sealed record ScoThermalForecast
{
    public bool Known{get;init;}
    public double ScoOnNetRisePercentPerSecond{get;init;}=double.NaN;
    public double ScoOffNetRisePercentPerSecond{get;init;}=double.NaN;
    public double? SecondsTo100LowerBound{get;init;}
    public double ContinueUntilDeadlinePeakPercent{get;init;}=double.NaN;
    public double DeadlineStopPeakPercent{get;init;}=double.NaN;
    public double ImmediateStopPeakPercent{get;init;}=double.NaN;
    public double ImmediateStopEndPercent{get;init;}=double.NaN;
    public double DeadlineStopEndPercent{get;init;}=double.NaN;
    public double StopHorizonSeconds{get;init;}
    public bool ShouldStop{get;init;}=true;
    public string Reason{get;init;}="unverified";
}

/// <summary>Shared-heat-pool forecast. Removing SCO removes its future input, never the stored energy or other sources.</summary>
public static class ScoThermalPredictor
{
    /// <summary>Bounds the next actual native simulation step before any later radiator tick is credited.</summary>
    public static bool TryBoundCurrentSimulationStep(ScoThermalInput value,double simulationDt,out double peakPercent)
    {
        peakPercent=double.NaN;
        if(!value.Verified||!double.IsFinite(simulationDt)||simulationDt<=0||simulationDt>.25
            ||!double.IsFinite(value.HeatPercent)||value.HeatPercent<0||!double.IsFinite(value.HeatUncertaintyPercent)||value.HeatUncertaintyPercent<0
            ||!double.IsFinite(value.HeatCapacity)||value.HeatCapacity<=0||!double.IsFinite(value.HeatDisplayScale)||value.HeatDisplayScale<=0
            ||!double.IsFinite(value.ScoInputUpper)||value.ScoInputUpper<0||!double.IsFinite(value.OtherInputUpper)||value.OtherInputUpper<0
            ||!double.IsFinite(value.SourceAgeSeconds)||value.SourceAgeSeconds<0||value.SourceAgeSeconds>.2)return false;
        // Sources are energy/simulation-second. No UTC duration or future cooling is substituted for this exact dt.
        peakPercent=value.HeatPercent+value.HeatUncertaintyPercent
            +(value.ScoInputUpper+value.OtherInputUpper)*simulationDt*100/(value.HeatCapacity*value.HeatDisplayScale);
        return double.IsFinite(peakPercent);
    }
    public static ScoThermalForecast Predict(ScoThermalInput value)
    {
        static ScoThermalForecast Unknown(string why)=>new(){Reason=why};
        if(!value.Verified)return Unknown("unverified-sources-or-radiator");
        double[] numbers=[value.HeatPercent,value.HeatCapacity,value.HeatDisplayScale,value.ScoInputUpper,value.OtherInputUpper,
            value.CoolingPassive,value.CoolingMaximum,value.CoolingScale,value.CoolingHeatLower,value.CoolingHeatUpper,
            value.SourceAgeSeconds,value.HeatAgeSeconds,value.LeaseRemainingSeconds,value.ResponseDelaySeconds,value.AfterStopSeconds,value.HeatUncertaintyPercent];
        if(numbers.Any(number=>!double.IsFinite(number)))return Unknown("non-finite-model");
        if(value.HeatPercent<0||value.HeatPercent>1000||value.HeatCapacity<=0||value.HeatDisplayScale<=0
            ||value.ScoInputUpper<0||value.OtherInputUpper<0||value.CoolingPassive<0||value.CoolingMaximum<value.CoolingPassive
            ||value.CoolingScale<0||value.CoolingHeatLower<0||value.CoolingHeatUpper<=value.CoolingHeatLower||value.HeatUncertaintyPercent<0)
            return Unknown("invalid-physical-model");
        if(value.SourceAgeSeconds<0||value.SourceAgeSeconds>.2||value.HeatAgeSeconds<0||value.HeatAgeSeconds>.2)
            return Unknown("stale-source-or-heat");
        if(value.LeaseRemainingSeconds<=0||value.LeaseRemainingSeconds>.25||value.ResponseDelaySeconds<0||value.ResponseDelaySeconds>.25
            ||value.AfterStopSeconds<0||value.AfterStopSeconds>2)return Unknown("invalid-control-horizon");
        var conversion=100/(value.HeatCapacity*value.HeatDisplayScale);
        var on=value.OtherInputUpper+value.ScoInputUpper;
        double Cooling(double hud)
        {
            var internalFraction=hud*value.HeatDisplayScale*.01;
            var fraction=Math.Clamp((internalFraction-value.CoolingHeatLower)/(value.CoolingHeatUpper-value.CoolingHeatLower),0,1);
            return value.CoolingScale*(value.CoolingPassive+(value.CoolingMaximum-value.CoolingPassive)*fraction*fraction);
        }
        double Slope(double heat,double input)=>(input-Cooling(heat))*conversion;
        var derivative=2*value.CoolingScale*(value.CoolingMaximum-value.CoolingPassive)
            /(value.HeatCapacity*(value.CoolingHeatUpper-value.CoolingHeatLower));
        var step=Math.Min(.01,derivative>0?.5/derivative:.01);
        if(!double.IsFinite(conversion)||!double.IsFinite(derivative)||step<.00001)return Unknown("unsupported-physical-scale");
        double Advance(double heat,double input,double seconds,ref double peak)
        {
            for(var remaining=seconds;remaining>1e-10;)
            {
                var duration=Math.Min(step,remaining);var slope=Slope(heat,input);
                if(slope>=0)heat+=slope*duration; // Monotone cooling makes a heating trajectory concave: explicit Euler is an upper bound.
                else
                {
                    // A cooling trajectory is convex. Implicit Euler stays above it and still cools;
                    // retaining the heating slope after SCO stops would invent a continuing heat source.
                    var high=heat;var low=Math.Max(0,heat+slope*duration);
                    if(-heat-Slope(0,input)*duration>=0)high=0;
                    else for(var iteration=0;iteration<40;iteration++)
                    {var middle=(low+high)*.5;if(middle-heat-Slope(middle,input)*duration>=0)high=middle;else low=middle;}
                    heat=high;
                }
                peak=Math.Max(peak,heat);remaining-=duration;
            }
            return heat;
        }
        var initial=value.HeatPercent+value.HeatUncertaintyPercent;var initialPeak=initial;
        initial=Advance(initial,on,value.HeatAgeSeconds,ref initialPeak);
        var continuingPeak=initialPeak;var continuing=Advance(initial,on,value.LeaseRemainingSeconds+value.ResponseDelaySeconds,ref continuingPeak);
        var stoppedAtDeadlinePeak=continuingPeak;
        var deadlineEnd=Advance(continuing,value.OtherInputUpper,value.AfterStopSeconds,ref stoppedAtDeadlinePeak);
        var stoppedPeak=initialPeak;var stopped=Advance(initial,on,value.ResponseDelaySeconds,ref stoppedPeak);
        stopped=Advance(stopped,value.OtherInputUpper,value.LeaseRemainingSeconds+value.AfterStopSeconds,ref stoppedPeak);
        double? to100=null;
        if(initial>=100)to100=0;
        else if(Slope(100,on)>0)
        {
            // Left rectangles underestimate the increasing integral 1/net-rise.
            // This is a lower time bound under the supplied input-upper/cooling-lower envelope, not a promise of future module activity.
            const int bands=128;var width=(100-initial)/bands;var seconds=0d;
            for(var index=0;index<bands;index++)seconds+=width/Slope(initial+index*width,on);
            if(double.IsFinite(seconds)&&seconds>=0)to100=seconds;
        }
        if(new[]{continuingPeak,stoppedPeak,stopped,deadlineEnd}.Any(number=>!double.IsFinite(number)))return Unknown("forecast-overflow");
        var stop=stoppedAtDeadlinePeak>=100;
        return new(){Known=true,ScoOnNetRisePercentPerSecond=Slope(value.HeatPercent,on),ScoOffNetRisePercentPerSecond=Slope(value.HeatPercent,value.OtherInputUpper),
            SecondsTo100LowerBound=to100,ContinueUntilDeadlinePeakPercent=continuingPeak,DeadlineStopPeakPercent=stoppedAtDeadlinePeak,ImmediateStopPeakPercent=stoppedPeak,
            ImmediateStopEndPercent=stopped,DeadlineStopEndPercent=deadlineEnd,StopHorizonSeconds=value.HeatAgeSeconds+value.LeaseRemainingSeconds+value.ResponseDelaySeconds,
            ShouldStop=stop,Reason=stop?(continuingPeak>=100?"reaches-100-before-original-stop":"other-source-overheat-after-deadline"):"within-current-source-envelope"};
    }
}