Ejemplo n.º 1
0
 public void CalculateOptimalLines(Conditions conditions, float exitSpeed, float exitAlt, float initialSpeed, float initialAlt)
 => EnvelopeLine.CalculateOptimalLines(conditions, exitSpeed, exitAlt, initialSpeed, initialAlt, envelopePoints, cancellationTokenSource, graphables);
Ejemplo n.º 2
0
        private IEnumerator Processing(Conditions conditions, EnvelopePoint[,] prelimData)
        {
            CancellationTokenSource closureCancellationTokenSource = this.cancellationTokenSource;

            primaryProgress = new EnvelopePoint[conditions.Resolution];
            int cachedCount = 0;

            stopwatch.Reset();
            stopwatch.Start();

            task = Task.Factory.StartNew <EnvelopePoint[, ]>(
                () =>
            {
                float[,] AoAs_guess = null, maxAs_guess = null, pitchIs_guess = null;
                AoAs_guess          = prelimData.SelectToArray(pt => pt.AoA_level);
                maxAs_guess         = prelimData.SelectToArray(pt => pt.AoA_max);
                pitchIs_guess       = prelimData.SelectToArray(pt => pt.pitchInput);

                try
                {
                    //OrderablePartitioner<EnvelopePoint> partitioner = Partitioner.Create(primaryProgress, true);
                    Parallel.For <AeroPredictor>(0, primaryProgress.Length, new ParallelOptions()
                    {
                        CancellationToken = closureCancellationTokenSource.Token
                    },
                                                 WindTunnelWindow.Instance.GetAeroPredictor,
                                                 (index, state, predictor) =>
                    {
                        int x             = index % conditions.XResolution, y = index / conditions.XResolution;
                        SurfCoords coords = new SurfCoords(x * conditions.stepSpeed + conditions.lowerBoundSpeed,
                                                           y * conditions.stepAltitude + conditions.lowerBoundAltitude);

                        EnvelopePoint result;
                        if (!cache.TryGetValue(coords, out result))
                        {
                            result        = new EnvelopePoint(predictor, conditions.body, y * conditions.stepAltitude + conditions.lowerBoundAltitude, x * conditions.stepSpeed + conditions.lowerBoundSpeed);
                            cache[coords] = result;
                        }
                        else
                        {
                            Interlocked.Increment(ref cachedCount);
                        }
                        primaryProgress[index] = result;
                        return(predictor);
                    }, (predictor) => (predictor as VesselCache.IReleasable)?.Release());

                    closureCancellationTokenSource.Token.ThrowIfCancellationRequested();
                    Debug.Log("KWT Data run finished. " + cachedCount + " of " + primaryProgress.Length + " retreived from cache. (" + (float)cachedCount / primaryProgress.Length * 100 + "%)");

                    return(primaryProgress.To2Dimension(conditions.XResolution));
                }
                catch (AggregateException aggregateException)
                {
                    foreach (var ex in aggregateException.Flatten().InnerExceptions)
                    {
                        Debug.LogException(ex);
                    }
                    throw aggregateException;
                }
            },
                closureCancellationTokenSource.Token);

            //if (task.Wait(25))
            //Debug.Log("KWT: Waiting actually did something!");

            while (task.Status < TaskStatus.RanToCompletion)
            {
                //Debug.Log(manager.PercentComplete + "% done calculating...");
                yield return(0);
            }
            //timer.Stop();
            //Debug.Log("Time taken: " + timer.ElapsedMilliseconds / 1000f);

            if (task.Status > TaskStatus.RanToCompletion)
            {
                if (task.Status == TaskStatus.Faulted)
                {
                    Debug.LogError("Wind tunnel task faulted (Envelope)");
                    Debug.LogException(task.Exception);
                }
                else if (task.Status == TaskStatus.Canceled)
                {
                    Debug.Log("Wind tunnel task was canceled. (Envelope)");
                }
                yield break;
            }

            if (!closureCancellationTokenSource.IsCancellationRequested)
            {
                envelopePoints    = ((Task <EnvelopePoint[, ]>)task).Result;
                currentConditions = conditions;
                UpdateGraphs();
                EnvelopeLine.CalculateOptimalLines(conditions, WindTunnelWindow.Instance.TargetSpeed, WindTunnelWindow.Instance.TargetAltitude, 0, 0, envelopePoints, closureCancellationTokenSource, graphables);
                valuesSet = true;
            }

            if (cachedCount < primaryProgress.Length)
            {
                yield return(0);
            }

            if (!closureCancellationTokenSource.IsCancellationRequested)
            {
                Conditions newConditions;
                for (int i = 1; i <= resolution.GetUpperBound(0); i++)
                {
                    if (resolution[i, 0] + 1 > conditions.XResolution || resolution[i, 1] + 1 > conditions.YResolution)
                    {
                        newConditions = conditions.Modify(
                            stepSpeed: Math.Min((conditions.upperBoundSpeed - conditions.lowerBoundSpeed) / resolution[i, 0], conditions.stepSpeed),
                            stepAltitude: Math.Min((conditions.upperBoundAltitude - conditions.lowerBoundAltitude) / resolution[i, 1], conditions.stepAltitude));
                        Debug.Log("Wind Tunnel graphing higher res at:" + newConditions.XResolution + " by " + newConditions.YResolution);
                        WindTunnel.Instance.StartCoroutine(Processing(newConditions, envelopePoints));
                        yield break;
                    }
                }

                Debug.Log("Wind Tunnel Graph reached maximum resolution");
            }
        }