Beispiel #1
0
        /// <summary>
        /// Find a voltage driver that closes a voltage drive loop.
        /// </summary>
        /// <returns>
        /// The component that closes the loop.
        /// </returns>
        private Component FindVoltageDriveLoop()
        {
            // Remove the ground node and make a map for reducing the matrix complexity
            var index = 1;
            var map   = new Dictionary <int, int> {
                { 0, 0 }
            };

            foreach (var vd in _voltageDriven)
            {
                if (vd.Node1 != 0)
                {
                    if (!map.ContainsKey(vd.Node1))
                    {
                        map.Add(vd.Node1, index++);
                    }
                }
                if (vd.Node2 != 0)
                {
                    if (!map.ContainsKey(vd.Node2))
                    {
                        map.Add(vd.Node2, index++);
                    }
                }
            }

            // Determine the rank of the matrix
            var solver = new RealSolver(Math.Max(_voltageDriven.Count, map.Count));

            for (var i = 0; i < _voltageDriven.Count; i++)
            {
                var pins = _voltageDriven[i];
                solver.GetMatrixElement(i + 1, map[pins.Node1]).Value += 1.0;
                solver.GetMatrixElement(i + 1, map[pins.Node2]).Value += 1.0;
            }
            try
            {
                // Try refactoring the matrix
                solver.OrderAndFactor();
            }
            catch (SingularException exception)
            {
                /*
                 * If the rank of the matrix is lower than the number of driven nodes, then
                 * the matrix is not solvable for those nodes. This means that there are
                 * voltage sources driving nodes in such a way that they cannot be solved.
                 */
                if (exception.Index <= _voltageDriven.Count)
                {
                    var indices = new LinearSystemIndices(exception.Index);
                    solver.InternalToExternal(indices);
                    return(_voltageDriven[indices.Row - 1].Source);
                }
            }
            return(null);
        }
Beispiel #2
0
        public void When_Factoring_Expect_Reference()
        {
            double[][] matrixElements =
            {
                new[] { 1.0, 1.0, 1.0 },
                new[] { 2.0, 3.0, 5.0 },
                new[] { 4.0, 6.0, 8.0 }
            };
            double[][] expected =
            {
                new[] { 1.0, 1.0,  1.0 },
                new[] { 2.0, 1.0,  3.0 },
                new[] { 4.0, 2.0, -0.5 }
            };

            // Create matrix
            var solver = new RealSolver();

            for (var r = 0; r < matrixElements.Length; r++)
            {
                for (var c = 0; c < matrixElements[r].Length; c++)
                {
                    solver.GetMatrixElement(r + 1, c + 1).Value = matrixElements[r][c];
                }
            }

            // Factor
            solver.Factor();

            // compare
            for (var r = 0; r < matrixElements.Length; r++)
            {
                for (var c = 0; c < matrixElements[r].Length; c++)
                {
                    Assert.AreEqual(expected[r][c], solver.GetMatrixElement(r + 1, c + 1).Value, 1e-12);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Read a .MTX file
        /// </summary>
        /// <param name="filename">Filename</param>
        /// <returns></returns>
        protected Solver <double> ReadMtxFile(string filename)
        {
            Solver <double> result;

            using (StreamReader sr = new StreamReader(filename))
            {
                // The first line is a comment
                sr.ReadLine();

                // The second line tells us the dimensions
                string line  = sr.ReadLine() ?? throw new Exception("Invalid Mtx file");
                var    match = Regex.Match(line, @"^(?<rows>\d+)\s+(?<columns>\d+)\s+(\d+)");
                int    size  = int.Parse(match.Groups["rows"].Value);
                if (int.Parse(match.Groups["columns"].Value) != size)
                {
                    throw new Exception("Matrix is not square");
                }

                result = new RealSolver(size);

                // All subsequent lines are of the format [row] [column] [value]
                while (!sr.EndOfStream)
                {
                    // Read the next line
                    line = sr.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    match = Regex.Match(line, @"^(?<row>\d+)\s+(?<column>\d+)\s+(?<value>.*)\s*$");
                    if (!match.Success)
                    {
                        throw new Exception("Could not recognize file");
                    }
                    int    row    = int.Parse(match.Groups["row"].Value);
                    int    column = int.Parse(match.Groups["column"].Value);
                    double value  = double.Parse(match.Groups["value"].Value, System.Globalization.CultureInfo.InvariantCulture);

                    // Set the value in the matrix
                    result.GetMatrixElement(row, column).Value = value;
                }
            }

            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Reads a matrix file generated by Spice 3f5.
        /// </summary>
        /// <param name="matFilename">The matrix filename.</param>
        /// <param name="vecFilename">The vector filename.</param>
        /// <returns></returns>
        protected Solver <double> ReadSpice3f5File(string matFilename, string vecFilename)
        {
            var solver = new RealSolver();

            // Read the spice file
            string line;

            using (var reader = new StreamReader(matFilename))
            {
                // The file is organized using (row) (column) (value) (imag value)
                while (!reader.EndOfStream && (line = reader.ReadLine()) != null)
                {
                    if (line == "first")
                    {
                        continue;
                    }
                    var match = Regex.Match(line, @"^(?<size>\d+)\s+(complex|real)$");

                    // Try to read an element
                    match = Regex.Match(line, @"^(?<row>\d+)\s+(?<col>\d+)\s+(?<value>[^\s]+)(\s+[^\s]+)?$");
                    if (match.Success)
                    {
                        int row   = int.Parse(match.Groups["row"].Value);
                        int col   = int.Parse(match.Groups["col"].Value);
                        var value = double.Parse(match.Groups["value"].Value, CultureInfo.InvariantCulture);
                        solver.GetMatrixElement(row, col).Value = value;
                    }
                }
            }

            // Read the vector file
            using (var reader = new StreamReader(vecFilename))
            {
                var index = 1;
                while (!reader.EndOfStream && (line = reader.ReadLine()) != null)
                {
                    var value = double.Parse(line, CultureInfo.InvariantCulture);
                    solver.GetRhsElement(index).Value = value;
                    index++;
                }
            }

            return(solver);
        }
        public void When_QuickDiagonalPivoting_Expect_NoException()
        {
            // Build the solver with only the quick diagonal pivoting
            var solver   = new RealSolver();
            var strategy = (Markowitz <double>)solver.Strategy;

            strategy.Strategies.Clear();
            strategy.Strategies.Add(new MarkowitzQuickDiagonal <double>());

            // Build the matrix that should be solvable using only the singleton pivoting strategy
            double[][] matrix =
            {
                new[] {    1, 0.5,     0,   0 },
                new[] { -0.5,   5,     4,   0 },
                new[] {    0,   3,     2, 0.1 },
                new[] {    0,   0, -0.01,   3 }
            };
            double[] rhs = { 0, 0, 0, 0 };
            for (var r = 0; r < matrix.Length; r++)
            {
                for (var c = 0; c < matrix[r].Length; c++)
                {
                    if (!matrix[r][c].Equals(0.0))
                    {
                        solver.GetMatrixElement(r + 1, c + 1).Value = matrix[r][c];
                    }
                }
                if (!rhs[r].Equals(0.0))
                {
                    solver.GetRhsElement(r + 1).Value = rhs[r];
                }
            }

            // This should run without throwing an exception
            solver.OrderAndFactor();
        }
Beispiel #6
0
        public void When_OrderAndFactoring2_Expect_Reference()
        {
            var solver = new RealSolver(5);

            solver.GetMatrixElement(1, 1).Value = 1.0;
            solver.GetMatrixElement(2, 1).Value = 0.0;
            solver.GetMatrixElement(2, 2).Value = 1.0;
            solver.GetMatrixElement(2, 5).Value = 0.0;
            solver.GetMatrixElement(3, 3).Value = 1.0;
            solver.GetMatrixElement(3, 4).Value = 1e-4;
            solver.GetMatrixElement(3, 5).Value = -1e-4;
            solver.GetMatrixElement(4, 4).Value = 1.0;
            solver.GetMatrixElement(5, 1).Value = 5.38e-23;
            solver.GetMatrixElement(5, 4).Value = -1e-4;
            solver.GetMatrixElement(5, 5).Value = 1e-4;

            solver.OrderAndFactor();

            AssertInternal(solver, 1, 1, 1.0);
            AssertInternal(solver, 2, 1, 0.0);
            AssertInternal(solver, 2, 2, 1.0);
            AssertInternal(solver, 2, 5, 0.0);
            AssertInternal(solver, 3, 3, 1.0);
            AssertInternal(solver, 3, 4, 1e-4);
            AssertInternal(solver, 3, 5, -1e-4);
            AssertInternal(solver, 4, 4, 1.0);
            AssertInternal(solver, 5, 1, 5.38e-23);
            AssertInternal(solver, 5, 4, -1e-4);
            AssertInternal(solver, 5, 5, 10000);
        }
Beispiel #7
0
        public void When_OrderAndFactoring_Expect_Reference()
        {
            var solver = new RealSolver();

            solver.GetMatrixElement(1, 1).Value = 0.0001;
            solver.GetMatrixElement(1, 4).Value = -0.0001;
            solver.GetMatrixElement(1, 5).Value = 0.0;
            solver.GetMatrixElement(2, 1).Value = 0.0;
            solver.GetMatrixElement(2, 2).Value = 1.0;
            solver.GetMatrixElement(2, 5).Value = 0.0;
            solver.GetMatrixElement(3, 1).Value = -0.0001;
            solver.GetMatrixElement(3, 3).Value = 1.0;
            solver.GetMatrixElement(3, 4).Value = 0.0001;
            solver.GetMatrixElement(4, 4).Value = 1.0;
            solver.GetMatrixElement(5, 5).Value = 1.0;

            // Order and factor
            solver.OrderAndFactor();

            // Compare
            Assert.AreEqual(solver.GetMatrixElement(1, 1).Value, 1.0e4);
            Assert.AreEqual(solver.GetMatrixElement(1, 4).Value, -0.0001);
            Assert.AreEqual(solver.GetMatrixElement(1, 5).Value, 0.0);
            Assert.AreEqual(solver.GetMatrixElement(2, 1).Value, 0.0);
            Assert.AreEqual(solver.GetMatrixElement(2, 2).Value, 1.0);
            Assert.AreEqual(solver.GetMatrixElement(2, 5).Value, 0.0);
            Assert.AreEqual(solver.GetMatrixElement(3, 1).Value, -0.0001);
            Assert.AreEqual(solver.GetMatrixElement(3, 3).Value, 1.0);
            Assert.AreEqual(solver.GetMatrixElement(3, 4).Value, 0.0001);
            Assert.AreEqual(solver.GetMatrixElement(4, 4).Value, 1.0);
            Assert.AreEqual(solver.GetMatrixElement(5, 5).Value, 1.0);
        }
Beispiel #8
0
        public void When_Preorder_Expect_Reference()
        {
            var solver = new RealSolver(5);

            solver.GetMatrixElement(1, 1).Value = 1e-4;
            solver.GetMatrixElement(1, 2).Value = 0.0;
            solver.GetMatrixElement(1, 3).Value = -1e-4;
            solver.GetMatrixElement(2, 1).Value = 0.0;
            solver.GetMatrixElement(2, 2).Value = 0.0;
            solver.GetMatrixElement(2, 5).Value = 1.0;
            solver.GetMatrixElement(3, 1).Value = -1e-4;
            solver.GetMatrixElement(3, 3).Value = 1e-4;
            solver.GetMatrixElement(3, 4).Value = 1.0;
            solver.GetMatrixElement(4, 3).Value = 1.0;
            solver.GetMatrixElement(5, 2).Value = 1.0;

            SpiceSharp.Simulations.ModifiedNodalAnalysisHelper.PreorderModifiedNodalAnalysis(solver, Math.Abs);

            AssertInternal(solver, 1, 1, 1e-4);
            AssertInternal(solver, 1, 4, -1e-4);
            AssertInternal(solver, 1, 5, 0.0);
            AssertInternal(solver, 2, 1, 0.0);
            AssertInternal(solver, 2, 2, 1.0);
            AssertInternal(solver, 2, 5, 0.0);
            AssertInternal(solver, 3, 1, -1e-4);
            AssertInternal(solver, 3, 3, 1.0);
            AssertInternal(solver, 3, 4, 1e-4);
            AssertInternal(solver, 4, 4, 1.0);
            AssertInternal(solver, 5, 5, 1.0);
        }