/// <summary> /// Adds the <paramref name="other"/> state to this instance. /// </summary> /// <param name="other">Object to add to this instance, internal collections' keys have to match, otherwise an exception is thrown</param> /// <param name="invalidateSource">If true, <see cref="GenericState{T}.SourceDescription"/> is invalidated (set to null)</param> public void AddState(InstantenousState other, bool invalidateSource = true) { // Check if keys of the other instance match internal keys, if not throw an exception if (!(Potentials.Keys.IsSequenceEqual(other.Potentials.Keys) && Currents.Keys.IsSequenceEqual(other.Currents.Keys))) { throw new ArgumentException(nameof(other) + " has incompatible internal collections (keys don't match)"); } // Add potentials from the other instance foreach (var key in Potentials.Keys.ToList()) { Potentials[key] += other.Potentials[key]; } // Add currents from the other instance foreach (var key in Currents.Keys.ToList()) { Currents[key] += other.Currents[key]; } if (invalidateSource) { SourceDescription = null; } }
/// <summary> /// Creates a DC instantenous state based on phasors in this instance. Throws an exception if <see cref="GenericState{T}.SourceDescription"/> /// is a description of an AC source. The DC version is created by taking real parts of each phasor (for DC phasors are real numbers). /// </summary> /// <returns></returns> public InstantenousState ToDC() { // Check if this state can be converted to DC if (SourceDescription.SourceType == SourceType.ACVoltageSource) { throw new Exception("Can't convert phasor produced by AC source to DC representation"); } // Create state for result var result = new InstantenousState(Potentials.Keys, Currents.Keys, SourceDescription); // Assign potentials - take real parts - phasors for DC are purely real foreach (var potential in Potentials) { result.Potentials[potential.Key] = potential.Value.Real; } // And currents - take real parts - phasors for DC are purely real foreach (var current in Currents) { result.Currents[current.Key] = current.Value.Real; } return(result); }
/// <summary> /// Creates an instantenous state for some time moment based on phasors in this instance. The time moment is defined as /// <paramref name="pointIndex"/> * <paramref name="timeStep"/>. /// </summary> /// <returns></returns> /// <param name="pointIndex">Index of the point for which the instantenous values will be calculated, indexing starts from 0</param> /// <param name="timeStep">Time step between 2 subsequent points</param> public InstantenousState ToInstantenousValues(int pointIndex, double timeStep) { // Create state for result var result = new InstantenousState(Potentials.Keys, Currents.Keys, SourceDescription); // Depending on source type switch (SourceDescription.SourceType) { // For AC sources case SourceType.ACVoltageSource: { // For each potential foreach (var potential in Potentials) { // Assign to result an instantenous value of a sine wave based on the phasor result.Potentials[potential.Key] = WaveformBuilder.SineWaveInstantenousValue(potential.Value.Magnitude, SourceDescription.Frequency, potential.Value.Phase, pointIndex, timeStep); } // Similarly for each current foreach (var current in Currents) { result.Currents[current.Key] = WaveformBuilder.SineWaveInstantenousValue(current.Value.Magnitude, SourceDescription.Frequency, current.Value.Phase, pointIndex, timeStep); } } break; // Phasors for DC sources are purely real - simply assign the real part of the phasor - time moment does not influence the value. case SourceType.DCVoltageSource: case SourceType.DCCurrentSource: { // For each potential foreach (var potential in Potentials) { result.Potentials[potential.Key] = potential.Value.Real; } // For each current foreach (var current in Currents) { result.Currents[current.Key] = current.Value.Real; } } break; } return(result); }