예제 #1
0
            /// <summary>
            /// Tries to construct a power for <paramref name="resistor"/>, returns true on success
            /// </summary>
            /// <param name="element"></param>
            /// <returns></returns>
            private bool TryConstructPower(IResistor resistor)
            {
                // Try to get voltage drop across the element
                if (_VoltageDrops.TryGet(resistor, out var voltageDrop))
                {
                    //// If successful, create a new power signal based on it, cache it
                    //CachePower(resistor,
                    //	IoC.Resolve<IPhasorDomainSignal>(
                    //	Math.Pow(voltageDrop.DC, 2) / resistor.Resistance,
                    //	voltageDrop.Phasors.
                    //	Select((v) => new KeyValuePair<double, Complex>(v.Key, Complex.Pow(v.Value, 2) / resistor.Resistance))));

                    CachePower(resistor, IoC.Resolve <IPhasorDomainSignal>(
                                   voltageDrop.Phasors.
                                   Select((x) => new KeyValuePair <ISourceDescription, double>(
                                              x.Key, x.Key.FrequencyCategory == FrequencyCategory.DC ? x.Value.Real : x.Value.Magnitude / Math.Sqrt(2))).
                                   Select((x) => new KeyValuePair <ISourceDescription, Complex>(x.Key, Math.Pow(x.Value, 2) / resistor.Resistance))));


                    // And return success
                    return(true);
                }
                else
                {
                    // Return failure
                    return(false);
                }
            }
예제 #2
0
        public List <IResistor> CreateResistorsOnDevice(IConfiguratedDeviceSchemeTable configuratedDeviceSchemeTable, IRuntimeDevice runtimeDevice)
        {
            List <IResistor> resistors = new List <IResistor>();

            if (runtimeDevice.StartersOnDevice.Count < 1)
            {
                return(resistors);
            }
            IEnumerable <IDeviceResistorRow> resistorRowsInDevice = configuratedDeviceSchemeTable.GetResistorEnumerable();

            foreach (var resistorRow in resistorRowsInDevice)
            {
                IResistor resistor = _container.Resolve <IResistor>();
                resistor.ModuleNumber   = resistorRow.ResistorModul;
                resistor.ResistorNumber = resistorRow.ResistorDiskret;
                if (resistorRow.ParentStarterId != null)
                {
                    resistor.ParentStarter =
                        runtimeDevice.StartersOnDevice.First((starter =>
                                                              starter.StarterNumber == resistorRow.ParentStarterId));
                }
                resistors.Add(resistor);
            }
            return(resistors);
        }
예제 #3
0
        public static void AddResistor(this List <IResistor> list, IResistor resistor, double targetResistance, Action <List <IResistor>, IResistor> callback)
        {
            if (list.Contains(resistor))
            {
                return;
            }
            int i = 0;

            while (i < list.Count && Math.Abs(list[i].Value - targetResistance) < Math.Abs(resistor.Value - targetResistance))
            {
                i++;
            }
            list.Insert(i, resistor);
            if (callback != null && (resistor is SingleResistor || resistor.Resistors.Count > 0))
            {
                callback(list, resistor);
            }
        }
예제 #4
0
 public static void Cleanup(ref IResistor resistor)
 {
     if (!(resistor is SingleResistor))
     {
         if (resistor.Resistors.Count == 0)
         {
             throw new Exception("Invalid resistor");
         }
         else if (resistor.Resistors.Count == 1)
         {
             IResistor internalResistor = resistor.Resistors[0];
             Cleanup(ref internalResistor);
             resistor = internalResistor;
         }
         else
         {
             resistor.Resistors.ForEach(x => Cleanup(ref x));
             resistor.Resistors = resistor.Resistors.OrderBy(x => x.Value).ToList();
         }
     }
 }
예제 #5
0
 /// <summary>
 /// Returns true if power for <paramref name="resistor"/> can be obtained from <see cref="_Cache"/>
 /// </summary>
 /// <param name="resistor"></param>
 /// <returns></returns>
 private bool TryEnablePower(IResistor resistor) =>
 // Check if the cache already contains an entry, otherwise try to construct it
 _Cache.ContainsKey(resistor) || TryConstructPower(resistor);
예제 #6
0
 /// <summary>
 /// Gets information about power dissipated on an <see cref="IResistor"/>
 /// </summary>
 /// <param name="voltageDrop"></param>
 /// <param name="resistor"></param>
 /// <returns></returns>
 public ISignalInformation Get(IResistor resistor, bool voltageBA) =>
 // Check if power can be enabled and if it can be fetched, if so return it, otherwise return null
 TryEnablePower(resistor) && _Cache.TryGetValue(resistor, out var power) ? power : null;
예제 #7
0
 /// <summary>
 /// Returns null
 /// </summary>
 /// <param name="resistor"></param>
 /// <param name="voltageBA">If true, voltage used to calculate the current is taken from <see cref="ITwoTerminal.TerminalA"/>
 /// (reference node) to <see cref="ITwoTerminal.TerminalB"/>, if false the direction is reversed</param>
 /// <returns></returns>
 public ISignalInformation Get(IResistor resistor, bool voltageBA) => null;
예제 #8
0
 /// <summary>
 /// Gets information about power dissipated on an <see cref="IResistor"/>
 /// </summary>
 /// <param name="voltageDrop"></param>
 /// <param name="resistor"></param>
 /// <returns></returns>
 public ISignalInformation Get(IResistor resistor, bool voltageBA) =>
 // Check if power can be enabled and if it can be fetched, if so return it, otherwise return null
 TryEnablePower(resistor, voltageBA) &&
 _Cache.TryGetValue(Tuple.Create <IBaseComponent, bool>(resistor, voltageBA), out var power) ? power.Item2 : null;
예제 #9
0
 /// <summary>
 /// Constructor injection
 /// </summary>
 public ResistorService(IResistor resistor)
 {
     this.resistor = resistor;
 }
예제 #10
0
        public IOutgoingLinesViewModel CreateOutgoingLinesViewModel(object runtimeDeviceObj)
        {
            IRuntimeDevice runtimeDevice = runtimeDeviceObj as IRuntimeDevice;
            var            fiders        = runtimeDevice.DeviceMomento.State.CustomDeviceSchemeTable.FidersTable.GetEnumeratorObjects();

            if (fiders.Any())
            {
                FidersOutgoingLinesViewModel fidersOutgoingLinesViewModel =
                    _container.Resolve <FidersOutgoingLinesViewModel>();

                foreach (var fider in fiders)
                {
                    IFiderViewModel fiderViewModel = _container.Resolve <IFiderViewModel>();
                    bool            isFiderEnabled = false;


                    if (fider.IsEnabledResistor1)
                    {
                        if (runtimeDevice.ResistorsOnDevice.Any((resistor =>
                                                                 resistor.ModuleNumber == fider.ModuleResistor1 &&
                                                                 resistor.ResistorNumber == fider.NumberResistor1 - 1)))
                        {
                            IResistor resistor1 = runtimeDevice.ResistorsOnDevice.First((resistor =>
                                                                                         resistor.ModuleNumber == fider.ModuleResistor1 &&
                                                                                         resistor.ResistorNumber == fider.NumberResistor1 - 1));

                            fiderViewModel.ResistorViewModels.Add(
                                _resistorViewModelFactory.CreateResistorViewModel(resistor1, true));
                            isFiderEnabled = true;
                        }
                        else
                        {
                            fiderViewModel.ResistorViewModels.Add(
                                _resistorViewModelFactory.CreateResistorViewModel(null, false));
                        }
                    }
                    else
                    {
                        fiderViewModel.ResistorViewModels.Add(
                            _resistorViewModelFactory.CreateResistorViewModel(null, false));
                    }


                    if (fider.IsEnabledResistor2)
                    {
                        if (runtimeDevice.ResistorsOnDevice.Any((resistor =>
                                                                 resistor.ModuleNumber == fider.ModuleResistor2 &&
                                                                 resistor.ResistorNumber == fider.NumberResistor2 - 1)))
                        {
                            IResistor resistor2 = runtimeDevice.ResistorsOnDevice.First((resistor =>
                                                                                         resistor.ModuleNumber == fider.ModuleResistor2 &&
                                                                                         resistor.ResistorNumber == fider.NumberResistor2 - 1));

                            fiderViewModel.ResistorViewModels.Add(
                                _resistorViewModelFactory.CreateResistorViewModel(resistor2, true));
                            isFiderEnabled = true;
                        }
                        else
                        {
                            fiderViewModel.ResistorViewModels.Add(
                                _resistorViewModelFactory.CreateResistorViewModel(null, false));
                        }
                    }
                    else
                    {
                        fiderViewModel.ResistorViewModels.Add(
                            _resistorViewModelFactory.CreateResistorViewModel(null, false));
                    }

                    if (fider.IsEnabledResistor3)
                    {
                        if (runtimeDevice.ResistorsOnDevice.Any((resistor =>
                                                                 resistor.ModuleNumber == fider.ModuleResistor3 &&
                                                                 resistor.ResistorNumber == fider.NumberResistor3 - 1)))
                        {
                            IResistor resistor3 = runtimeDevice.ResistorsOnDevice.First((resistor =>
                                                                                         resistor.ModuleNumber == fider.ModuleResistor3 &&
                                                                                         resistor.ResistorNumber == fider.NumberResistor3 - 1));

                            fiderViewModel.ResistorViewModels.Add(
                                _resistorViewModelFactory.CreateResistorViewModel(resistor3, true));
                            isFiderEnabled = true;
                        }
                        else
                        {
                            fiderViewModel.ResistorViewModels.Add(
                                _resistorViewModelFactory.CreateResistorViewModel(null, false));
                        }
                    }
                    else
                    {
                        fiderViewModel.ResistorViewModels.Add(
                            _resistorViewModelFactory.CreateResistorViewModel(null, false));
                    }
                    if (isFiderEnabled)
                    {
                        fiderViewModel.FiderSignature = fider.Tag;
                        fidersOutgoingLinesViewModel.FiderViewModels.Add(fiderViewModel);
                    }
                }

                return(fidersOutgoingLinesViewModel);
            }
            else
            {
                ResistorOutgoingLinesViewModel resistorOutgoingLinesViewModel =
                    _container.Resolve <ResistorOutgoingLinesViewModel>();
                List <IResistorViewModel> resistorViewModels = new List <IResistorViewModel>();
                foreach (var resistor in runtimeDevice.ResistorsOnDevice)
                {
                    resistorViewModels.Add(
                        _resistorViewModelFactory.CreateResistorViewModel(resistor, true));
                }
                var moduleOrdered = resistorViewModels.OrderBy((model => model.ResistorNumber));
                var resOrdered    = moduleOrdered.OrderBy((model => model.ModuleNumber));
                resistorOutgoingLinesViewModel.ResistorViewModels.AddRange(resOrdered);
                return(resistorOutgoingLinesViewModel);
            }
            throw new ArgumentException();
        }
예제 #11
0
        private static List <IResistor> BuildResistor(double targetResistance, IResistor prev, List <IResistor> all, ref int k, List <double> resistors, Action <List <IResistor>, IResistor> callback = null)
        {
            if (all == null)
            {
                all = new List <IResistor>();
            }
            if (prev == null)
            {
                prev = new SerialResistor();
            }

            if (prev.Count >= k)
            {
                if (prev.Count == k)
                {
                    all.AddResistor(prev, targetResistance, callback);
                }
                return(all);
            }

            IResistor resistor = (IResistor)prev.Clone();

            double bestResult = Math.Min(all.Count == 0 ? double.PositiveInfinity
                                                        : all.Min(x => Math.Abs(x.Value - targetResistance)), Math.Abs(prev.Value - targetResistance));

            List <IResistor> content = new List <IResistor>();

            void RecurseResistors(IResistor res)
            {
                if (!(res is SingleResistor))
                {
                    content.Add(res);
                    res.Resistors.ForEach(RecurseResistors);
                }
            }

            RecurseResistors(resistor);
            content = content.Distinct().ToList();

            double RecurseOption(IResistor subResistor, IResistor newResistor, ref int remainingK)
            {
                subResistor.Resistors.Add(newResistor);
                Cleanup(ref newResistor);
                double diff = Math.Abs(resistor.Value - targetResistance);

                if (!all.Contains(newResistor))
                {
                    if (diff == 0)
                    {
                        all.AddResistor((IResistor)resistor.Clone(), targetResistance, callback);
                        remainingK = Math.Min(resistor.Count, remainingK);
                    }
                    else if (diff < bestResult)
                    {
                        List <IResistor> results = BuildResistor(targetResistance, resistor, all, ref remainingK, new List <double>(resistors), callback);

                        if (results.Count == 0)
                        {
                            all.AddResistor((IResistor)resistor.Clone(), targetResistance, callback);
                        }
                        else
                        {
                            all.AddResistors(results, targetResistance, callback);
                        }

                        all = all.Distinct().ToList();
                    }
                }
                subResistor.Resistors.RemoveAt(subResistor.Resistors.Count - 1);
                return(diff);
            }

            foreach (IResistor subResistor in content)
            {
                for (int i = 0; i < resistors.Count; i++)
                {
                    double resistance0 = resistors[i];
                    resistors.RemoveAt(i);

                    if (RecurseOption(subResistor, new SingleResistor(resistance0), ref k) == 0)
                    {
                        goto Exit;
                    }

                    for (int j = 0; j < resistors.Count; j++)
                    {
                        double resistance1 = resistors[j];
                        resistors.RemoveAt(j);

                        if (RecurseOption(subResistor, new SerialResistor(resistance0, resistance1), ref k) == 0)
                        {
                            goto Exit;
                        }
                        if (RecurseOption(subResistor, new ParallelResistor(resistance0, resistance1), ref k) == 0)
                        {
                            goto Exit;
                        }

                        resistors.Add(resistance1);
                    }

                    resistors.Add(resistance0);
                }
            }

Exit:

            return(all);
        }