Ejemplo n.º 1
0
        /// <summary>
        /// ヤコビ行列を返します。
        /// </summary>
        /// <returns></returns>
        public DoubleSparseMatrix GetA()
        {
            var       sizeOfBlock   = _fem.NumberOfNodes;
            var       capacity      = _fem.MaxNodesPerRow;
            const int numberOfBlock = 2;
            var       J             = new DoubleSparseMatrix(numberOfBlock * sizeOfBlock, numberOfBlock * sizeOfBlock, numberOfBlock * capacity);
            var       electron      = _manager.GetParticle(Species.Electron);
            var       positive      = _manager.GetParticle(Species.Positive);
            var       elements      = _fem.Elements;

            // J11...
            var J11 = Assembler(electron);
            var rhs = _manager.GetRHSPoissonEqn();

            J.Insert(J11, 0 * sizeOfBlock, 0 * sizeOfBlock);
            // J12
            var J12r = new DoubleSparseMatrix(sizeOfBlock, sizeOfBlock, capacity);
            var J12s = new DoubleSparseMatrix(sizeOfBlock, sizeOfBlock, capacity);

            AssembleBoundaryIntegralSecondaryEmission(J12r, d => GetVelocity(positive, d));
            J.Insert(J12s - J12r, 0 * sizeOfBlock, 1 * sizeOfBlock);
            // J21
            var J21s = new DoubleSparseMatrix(sizeOfBlock, capacity);

            AssembleSourceMatrix(J21s, d => GetVelocity(positive, d), positive.GetDiffusion(), positive.GetSourceDerivative(Species.Electron));
            var J21r = new DoubleSparseMatrix(sizeOfBlock, capacity);

            J.Insert(J21s - J21r, 1 * sizeOfBlock, 0 * sizeOfBlock);
            // J22
            var J22 = Assembler(positive);

            J.Insert(J22, 1 * sizeOfBlock, 1 * sizeOfBlock);
            return(J);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// ドリフト拡散反応方程式ソルバの基底クラスを初期化します。
 /// </summary>
 protected DriftDiffusionBase(int n, int capacity)
 {
     _A = new DoubleSparseMatrix(n, n, capacity);
     _K = new DoubleSparseMatrix(n, n, capacity);
     _S = new DoubleSparseMatrix(n, n, capacity);
     _D = new DoubleSparseMatrix(n, n, capacity);
     _M = new DoubleSparseMatrix(n, n, capacity);
     _N = new DoubleSparseMatrix(n, n, capacity);
 }
        public void Product()
        {
            var values = new double[2, 3] {
                { 1, 2, 3 }, { 4, 5, 6 }
            };
            var sparseMatrix = new DoubleSparseMatrix(2, 3, values);

            var vector = new double[] { 2, 2, 2 };

            var result = sparseMatrix.Product(new ScalarSet(vector));

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual(12, result.GetScalar(0));
            Assert.AreEqual(30, result.GetScalar(1));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 質量行列を返します。
        /// </summary>
        public DoubleSparseMatrix GetM()
        {
            var n  = _fem.NumberOfNodes;
            var m  = _fem.MaxNodesPerRow;
            var M  = new DoubleSparseMatrix(2 * n, 2 * m);
            var Mg = new DoubleSparseMatrix(n, m);
            var e  = _manager.GetParticle(Species.Electron);

            AssembleMassMatrix(Mg, d => GetVelocity(e, d), e.GetDiffusion());
            M.Insert(Mg, 0 * n, 0 * n);
            Mg.Clear();
            var p = p_manager.GetParticle(Species.Positive);

            AssembleMassMatrix(Mg, d => GetVelocity(p, d), p.GetDiffusion());
            M.Insert(Mg, 1 * n, 1 * n);
            return(M);
        }
        public void CopyDenseToSparse()
        {
            var denseMatrix = new double[2, 2] {
                { 1, 2 }, { 3, 4 }
            };

            var sparseMatrix = new DoubleSparseMatrix(2, 2);

            sparseMatrix[0, 0] = denseMatrix[0, 0];
            sparseMatrix[0, 1] = denseMatrix[0, 1];
            sparseMatrix[1, 0] = denseMatrix[1, 0];
            sparseMatrix[1, 1] = denseMatrix[1, 1];

            Assert.AreEqual(1, sparseMatrix[0, 0]);
            Assert.AreEqual(2, sparseMatrix[0, 1]);
            Assert.AreEqual(3, sparseMatrix[1, 0]);
            Assert.AreEqual(4, sparseMatrix[1, 1]);
        }
Ejemplo n.º 6
0
        public void CheckElementMapper()
        {
            ElementSet regularGrid = CreateRegularGridElmtSet(320000.0, 380000.0, 10000, 6120000.0, 6225000.0, 10000);
            //ElementSet regularGrid = CreateRegularGridElmtSet(320000.0, 380000.0, 5000, 6120000.0, 6225000.0, 5000);
            //ElementSet regularGrid = CreateRegularGridElmtSet(320000.0, 380000.0, 2500, 6120000.0, 6225000.0, 2500);
            //ElementSet regularGrid = CreateRegularGridElmtSet(320000.0, 380000.0, 1250, 6120000.0, 6225000.0, 1250);
            ElementSet mesh = ReadMesh(@"..\..\Spatial\Data\oresund.mesh");

            double y0 = 6120000.0;
            double x0 = 320000.0;

            // If this is not done,
            //MoveSystemOrigo(mesh, x0, y0);
            //MoveSystemOrigo(regularGrid, x0, y0);

            ElementMapper mapper1 = new ElementMapper();

            mapper1.UseSearchTree = false;
            Stopwatch timer1 = new Stopwatch();

            timer1.Start();
            // Weighted sum
            IIdentifiable[] methods = SpatialAdaptedOutputFactory.GetAvailableMethods(ElementType.Polygon, ElementType.Polygon);
            mapper1.Initialise(methods[1], regularGrid, mesh);
            timer1.Stop();


            Console.Out.WriteLine("time 1 : " + timer1.Elapsed.TotalSeconds);

            DoubleSparseMatrix matrix1 = (DoubleSparseMatrix)mapper1.MappingMatrix;

            Assert.AreEqual(66, matrix1.ColumnCount);
            Assert.AreEqual(3636, matrix1.RowCount);
            Assert.AreEqual(4430, matrix1.Values.Count);

            // TODO: Pick out some test values
            foreach (KeyValuePair <DoubleSparseMatrix.Index, double> pair in matrix1.Values)
            {
                int row = pair.Key.Row;
                int col = pair.Key.Column;
//                Assert.AreEqual(pair.Value, string.Format("({0},{1})", row, col));
            }
        }
Ejemplo n.º 7
0
        public void Solve()
        {
            if (_solverJ == null)
            {
                var A = _assembler.GetStiffnessMatrix(_epsilonR);
                foreach (var boundary in _potentials)
                {
                    _boundary.ImposeFirstKindBoundaryCondition(A, boundary.Key);
                }
                _solverJ.SetMatrix(A);
            }
            if (_solverM == null)
            {
                _M = _assembler.GetMassMatrix();
                _solverM.SetMatrix(_M);
            }
            var rhsVector = _M * _rhs;

            foreach (var boundary in _potentials)
            {
                _boundary.ImposeFirstKindBoundaryCondition(rhsVector, boundary.Key, boundary.Value);
            }
            _phi = _solverJ.Solve(rhsVector);
        }
        public void DefaultValue()
        {
            var sparseMatrix = new DoubleSparseMatrix(2, 2);

            Assert.AreEqual(default(double), sparseMatrix[0, 0]);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// 質量行列を返します。
 /// </summary>
 /// <param name="M"></param>
 protected abstract void AssembleMassMatrix(DoubleSparseMatrix M, Func <Axis, DoubleDenseMatrix> vhandle, DoubleDenseMatrix diffusion);
Ejemplo n.º 10
0
 /// <summary>
 /// 生成行列を返します。
 /// </summary>
 protected abstract void AssembleSourceMatrix(DoubleSparseMatrix S, Func <Axis, DoubleDenseMatrix> vhandle, DoubleDenseMatrix diffusion, DoubleDenseMatrix source);
Ejemplo n.º 11
0
 /// <summary>
 /// 拡散行列を返します。
 /// </summary>
 protected abstract void AssembleDiffusionMatrix(DoubleSparseMatrix D, Func <Axis, DoubleDenseMatrix> vhandle, DoubleDenseMatrix diffusivity);
Ejemplo n.º 12
0
 /// <summary>
 /// 二次電子放出に関する境界積分マトリクスを構築します。
 /// </summary>
 protected abstract void AssembleBoundaryIntegralSecondaryEmission(DoubleSparseMatrix K2, Func <Axis, DoubleDenseMatrix> vhandle);
Ejemplo n.º 13
0
 /// <summary>
 /// フラックスに関する境界条件を埋め込みます。
 /// </summary>
 protected abstract void BoundaryMatrixHelper(DoubleSparseMatrix K, Func <Axis, DoubleDenseMatrix> vhandle);
Ejemplo n.º 14
0
 protected void AssembleBoundaryIntegralMatrix(DoubleSparseMatrix K, Func <Axis, DoubleDenseMatrix> vhandle)
 {
     BoundaryMatrixHelper(K, vhandle);
 }
Ejemplo n.º 15
0
        public DoubleSparseMatrix GetStiffnessMatrix(DoubleDenseMatrix diffusivity)
        {
            const int capacitySecondOrder = 5;
            var       yield = new DoubleSparseMatrix(_nx * _ny, _nx * _ny, capacitySecondOrder);

            {
                var i = 0;
                // grad = 0.
                {
                    var j     = 0;
                    var index = i * _ny + j;
                    yield[index, index + 1]   = 1.0;
                    yield[index, index + _ny] = 1.0;
                    yield[index, index]       = -2.0;
                }
                for (var j = 1; j < _ny - 1; j++)
                {
                    var index = i * _ny + j;
                    yield[index, index - _ny] = 1.0;
                    yield[index, index + 1]   = 1.0;
                    yield[index, index + _ny] = 1.0;
                    yield[index, index]       = -3.0;
                }
                {
                    var j     = _ny - 1;
                    var index = i * _ny + j;
                    yield[index, index + 1]   = 1.0;
                    yield[index, index - _ny] = 1.0;
                    yield[index, index]       = -2.0;
                }
            }
            for (var i = 1; i < _nx - 1; i++)
            {
                {
                    var j = 0;
                    // grad = 0.
                    var index = i * _ny + j;
                    yield[index, index - 1]   = 1.0;
                    yield[index, index + 1]   = 1.0;
                    yield[index, index + _ny] = 1.0;
                    yield[index, index]       = -3.0;
                }
                for (var j = 1; j < _ny - 1; j++)
                {
                    var phaseC = _phase[i, j];
                    var phaseE = _phase[i - 1, j];
                    var phaseW = _phase[i + 1, j];
                    var phaseS = _phase[i, j - 1];
                    var phaseN = _phase[i, j + 1];
                    var index  = i * _ny + j;
                    yield[index, index - _ny] = phaseS;
                    yield[index, index - 1]   = phaseE;
                    yield[index, index + 1]   = phaseW;
                    yield[index, index + _ny] = phaseN;
                    var coefS = phaseS == 1 ? 1.0 : 2.0;
                    var coefE = phaseE == 1 ? 1.0 : 2.0;
                    var coefW = phaseW == 1 ? 1.0 : 2.0;
                    var coefN = phaseN == 1 ? 1.0 : 2.0;
                    yield[index, index] = -(coefS + coefE + coefW + coefN);
                }
                {
                    var j = _ny - 1;
                    // grad = 0.
                    var index = i * _ny + j;
                    yield[index, index - 1]   = 1.0;
                    yield[index, index + 1]   = 1.0;
                    yield[index, index - _ny] = 1.0;
                    yield[index, index]       = -3.0;
                }
            }
            {
                var i = _nx - 1;
                // grad = 0.
                {
                    var j     = 0;
                    var index = i * _ny + j;
                    yield[index, index - 1]   = 1.0;
                    yield[index, index + _ny] = 1.0;
                    yield[index, index]       = -2.0;
                }
                for (var j = 1; j < _ny - 1; j++)
                {
                    var index = i * _ny + j;
                    yield[index, index - _ny] = 1.0;
                    yield[index, index - 1]   = 1.0;
                    yield[index, index + _ny] = 1.0;
                    yield[index, index]       = -3.0;
                }
                {
                    var j     = _ny - 1;
                    var index = i * _ny + j;
                    yield[index, index - 1]   = 1.0;
                    yield[index, index - _ny] = 1.0;
                    yield[index, index]       = -2.0;
                }
            }
            return(yield * (1.0 / (_delta * _delta)));
        }
Ejemplo n.º 16
0
        public virtual void UpdateMappingMatrix(string methodDescription, IElementSet fromElements,
                                                IElementSet toElements, bool useSparseMatrix)
        {
            try
            {
                ElementSetChecker.CheckElementSet(fromElements);
                ElementSetChecker.CheckElementSet(toElements);

                CurrentMethod = GetMethod(methodDescription, fromElements.ElementType, toElements.ElementType);

                if(useSparseMatrix)
                {
                    MappingMatrix = new DoubleSparseMatrix(RowsCount, ColumnsCount);
                    intersectedLengthMatrix = new DoubleSparseMatrix(RowsCount, ColumnsCount);
                }
                else
                {
                    MappingMatrix = new DoubleMatrix(RowsCount, ColumnsCount);
                    intersectedLengthMatrix = new DoubleMatrix(RowsCount, ColumnsCount);
                }

                if (fromElements.ElementType == ElementType.XYPoint && toElements.ElementType == ElementType.XYPoint)
                    // Point to Point
                {
                    #region

                    try
                    {
                        for (int i = 0; i < RowsCount; i++)
                        {
                            XYPoint ToPoint = CreateXYPoint(toElements, i);
                            for (int j = 0; j < ColumnsCount; j++)
                            {
                                XYPoint FromPoint = CreateXYPoint(fromElements, j);
                                MappingMatrix[i, j] = XYGeometryTools.CalculatePointToPointDistance(ToPoint, FromPoint);
                            }
                        }

                        if (CurrentMethod.ID.Equals((int) DefaultMethodType.PointToPoint.Nearest))
                        {
                            for (int i = 0; i < RowsCount; i++)
                            {
                                double MinDist = MappingMatrix[i, 0];
                                for (int j = 1; j < ColumnsCount; j++)
                                {
                                    if (MappingMatrix[i, j] < MinDist)
                                    {
                                        MinDist = MappingMatrix[i, j];
                                    }
                                }
                                int Denominator = 0;
                                for (int j = 0; j < ColumnsCount; j++)
                                {
                                    if (MappingMatrix[i, j] == MinDist)
                                    {
                                        MappingMatrix[i, j] = 1;
                                        Denominator++;
                                    }
                                    else
                                    {
                                        MappingMatrix[i, j] = 0;
                                    }
                                }
                                for (int j = 0; j < ColumnsCount; j++)
                                {
                                    MappingMatrix[i, j] = MappingMatrix[i, j]/Denominator;
                                }
                            }
                        } // if (currentMethodId.Equals((int) DefaultMethodType.PointToPoint.Nearest))
                        else if (CurrentMethod.ID.Equals((int) DefaultMethodType.PointToPoint.Inverse))
                        {
                            for (int i = 0; i < RowsCount; i++)
                            {
                                double MinDist = MappingMatrix[i, 0];
                                for (int j = 1; j < ColumnsCount; j++)
                                {
                                    if (MappingMatrix[i, j] < MinDist)
                                    {
                                        MinDist = MappingMatrix[i, j];
                                    }
                                }
                                if (MinDist == 0)
                                {
                                    int Denominator = 0;
                                    for (int j = 0; j < ColumnsCount; j++)
                                    {
                                        if (MappingMatrix[i, j] == MinDist)
                                        {
                                            MappingMatrix[i, j] = 1;
                                            Denominator++;
                                        }
                                        else
                                        {
                                            MappingMatrix[i, j] = 0;
                                        }
                                    }
                                    for (int j = 0; j < ColumnsCount; j++)
                                    {
                                        MappingMatrix[i, j] = MappingMatrix[i, j]/Denominator;
                                    }
                                }
                                else
                                {
                                    double Denominator = 0;
                                    for (int j = 0; j < ColumnsCount; j++)
                                    {
                                        MappingMatrix[i, j] = 1/MappingMatrix[i, j];
                                        Denominator = Denominator + MappingMatrix[i, j];
                                    }
                                    for (int j = 0; j < ColumnsCount; j++)
                                    {
                                        MappingMatrix[i, j] = MappingMatrix[i, j]/Denominator;
                                    }
                                }
                            }
                        } // else if (currentMethodId.Equals((int) DefaultMethodType.PointToPoint.Inverse))
                        else
                        {
                            throw new Exception("methodDescription unknown for point point mapping");
                        }
                        // else if (currentMethodId.Equals((int) DefaultMethodType.PointToPoint.Nearest)) and else if (currentMethodId.Equals((int) DefaultMethodType.PointToPoint.Inverse))
                    }
                    catch (Exception e) // Catch for all of the Point to Point part
                    {
                        throw new Exception("Point to point mapping failed", e);
                    }

                    #endregion
                }
                else if (fromElements.ElementType == ElementType.XYPoint &&
                         ((toElements.ElementType == ElementType.XYPolyLine) ||
                          (toElements.ElementType == ElementType.XYLine)))
                    // Point to PolyLine/Line
                {
                    #region

                    try
                    {
                        for (int i = 0; i < RowsCount; i++)
                        {
                            XYPolyline toPolyLine = CreateXYPolyline(toElements, i);
                            for (int j = 0; j < ColumnsCount; j++)
                            {
                                XYPoint fromPoint = CreateXYPoint(fromElements, j);
                                MappingMatrix[i, j] = XYGeometryTools.CalculatePolylineToPointDistance(toPolyLine,
                                                                                                       fromPoint);
                            }
                        }

                        if (CurrentMethod.ID.Equals((int) DefaultMethodType.PointToPolyline.Nearest) ||
                            CurrentMethod.ID.Equals((int) DefaultMethodType.PointToLine.Nearest))
                        {
                            for (int i = 0; i < RowsCount; i++)
                            {
                                double MinDist = MappingMatrix[i, 0];
                                for (int j = 1; j < ColumnsCount; j++)
                                {
                                    if (MappingMatrix[i, j] < MinDist)
                                    {
                                        MinDist = MappingMatrix[i, j];
                                    }
                                }
                                int denominator = 0;
                                for (int j = 0; j < ColumnsCount; j++)
                                {
                                    if (MappingMatrix[i, j] == MinDist)
                                    {
                                        MappingMatrix[i, j] = 1;
                                        denominator++;
                                    }
                                    else
                                    {
                                        MappingMatrix[i, j] = 0;
                                    }
                                }
                                for (int j = 0; j < ColumnsCount; j++)
                                {
                                    MappingMatrix[i, j] = MappingMatrix[i, j]/denominator;
                                }
                            }
                        } // if (currentMethodId.Equals((int) DefaultMethodType.PointToPolyline.Nearest))
                        else if ((CurrentMethod.ID.Equals((int) DefaultMethodType.PointToPolyline.Inverse)) ||
                                 (CurrentMethod.ID.Equals((int) DefaultMethodType.PointToLine.Inverse)))
                        {
                            for (int i = 0; i < RowsCount; i++)
                            {
                                double minDist = MappingMatrix[i, 0];
                                for (int j = 1; j < ColumnsCount; j++)
                                {
                                    if (MappingMatrix[i, j] < minDist)
                                    {
                                        minDist = MappingMatrix[i, j];
                                    }
                                }
                                if (minDist == 0)
                                {
                                    int denominator = 0;
                                    for (int j = 0; j < ColumnsCount; j++)
                                    {
                                        if (MappingMatrix[i, j] == minDist)
                                        {
                                            MappingMatrix[i, j] = 1;
                                            denominator++;
                                        }
                                        else
                                        {
                                            MappingMatrix[i, j] = 0;
                                        }
                                    }
                                    for (int j = 0; j < ColumnsCount; j++)
                                    {
                                        MappingMatrix[i, j] = MappingMatrix[i, j]/denominator;
                                    }
                                }
                                else
                                {
                                    double denominator = 0;
                                    for (int j = 0; j < ColumnsCount; j++)
                                    {
                                        MappingMatrix[i, j] = 1/MappingMatrix[i, j];
                                        denominator = denominator + MappingMatrix[i, j];
                                    }
                                    for (int j = 0; j < ColumnsCount; j++)
                                    {
                                        MappingMatrix[i, j] = MappingMatrix[i, j]/denominator;
                                    }
                                }
                            }
                        } // else if (currentMethodId.Equals((int) DefaultMethodType.PointToPolyline.Inverse))
                        else // if currentMethodId != Nearest and Inverse
                        {
                            throw new Exception("methodDescription unknown for point to polyline mapping");
                        }
                    }
                    catch (Exception e) // Catch for all of the Point to Polyline part
                    {
                        throw new Exception("Point to polyline mapping failed", e);
                    }

                    #endregion
                }
                else if (fromElements.ElementType == ElementType.XYPoint &&
                         toElements.ElementType == ElementType.XYPolygon)
                    // Point to Polygon
                {
                    #region

                    try
                    {
                        XYPolygon polygon;
                        XYPoint point;
                        int count;
                        for (int i = 0; i < RowsCount; i++)
                        {
                            polygon = toXYPolygons[i];
                            count = 0;
                            for (int n = 0; n < ColumnsCount; n++)
                            {
                                point = CreateXYPoint(fromElements, n);
                                if (XYGeometryTools.IsPointInPolygon(point, polygon))
                                {
                                    if (CurrentMethod.ID.Equals((int) DefaultMethodType.PointToPolygon.Mean))
                                    {
                                        count = count + 1;
                                    }
                                    else if (CurrentMethod.ID.Equals((int) DefaultMethodType.PointToPolygon.Sum))
                                    {
                                        count = 1;
                                    }
                                    else
                                    {
                                        throw new Exception(
                                            "methodDescription unknown for point to polygon mapping");
                                    }
                                }
                            }
                            for (int n = 0; n < ColumnsCount; n++)
                            {
                                point = CreateXYPoint(fromElements, n);

                                if (XYGeometryTools.IsPointInPolygon(point, polygon))
                                {
                                    MappingMatrix[i, n] = 1.0/count;
                                }
                            }
                        }
                    }
                    catch (Exception e) // Catch for all of the Point to Polyline part
                    {
                        throw new Exception("Point to polygon mapping failed", e);
                    }

                    #endregion
                }
                else if (((fromElements.ElementType == ElementType.XYPolyLine) ||
                          (fromElements.ElementType == ElementType.XYLine)) &&
                         toElements.ElementType == ElementType.XYPoint)
                    // Polyline/Line to Point
                {
                    #region

                    try
                    {
                        for (int i = 0; i < RowsCount; i++)
                        {
                            XYPoint toPoint = CreateXYPoint(toElements, i);
                            for (int j = 0; j < ColumnsCount; j++)
                            {
                                XYPolyline fromPolyLine = CreateXYPolyline(fromElements, j);
                                MappingMatrix[i, j] =
                                    XYGeometryTools.CalculatePolylineToPointDistance(fromPolyLine, toPoint);
                            }
                        }

                        if (CurrentMethod.ID.Equals((int) DefaultMethodType.PolylineToPoint.Nearest) ||
                            CurrentMethod.ID.Equals((int) DefaultMethodType.LineToPoint.Nearest))
                        {
                            for (int i = 0; i < RowsCount; i++)
                            {
                                double minDist = MappingMatrix[i, 0];
                                for (int j = 1; j < ColumnsCount; j++)
                                {
                                    if (MappingMatrix[i, j] < minDist)
                                    {
                                        minDist = MappingMatrix[i, j];
                                    }
                                }
                                int denominator = 0;
                                for (int j = 0; j < ColumnsCount; j++)
                                {
                                    if (MappingMatrix[i, j] == minDist)
                                    {
                                        MappingMatrix[i, j] = 1;
                                        denominator++;
                                    }
                                    else
                                    {
                                        MappingMatrix[i, j] = 0;
                                    }
                                }
                                for (int j = 0; j < ColumnsCount; j++)
                                {
                                    MappingMatrix[i, j] = MappingMatrix[i, j]/denominator;
                                }
                            }
                        } // if (currentMethodId.Equals((int) DefaultMethodType.PolylineToPoint.Nearest))
                        else if (CurrentMethod.ID.Equals((int) DefaultMethodType.PolylineToPoint.Inverse) ||
                                 CurrentMethod.ID.Equals((int) DefaultMethodType.LineToPoint.Inverse))
                        {
                            for (int i = 0; i < RowsCount; i++)
                            {
                                double minDist = MappingMatrix[i, 0];
                                for (int j = 1; j < ColumnsCount; j++)
                                {
                                    if (MappingMatrix[i, j] < minDist)
                                    {
                                        minDist = MappingMatrix[i, j];
                                    }
                                }
                                if (minDist == 0)
                                {
                                    int denominator = 0;
                                    for (int j = 0; j < ColumnsCount; j++)
                                    {
                                        if (MappingMatrix[i, j] == minDist)
                                        {
                                            MappingMatrix[i, j] = 1;
                                            denominator++;
                                        }
                                        else
                                        {
                                            MappingMatrix[i, j] = 0;
                                        }
                                    }
                                    for (int j = 0; j < ColumnsCount; j++)
                                    {
                                        MappingMatrix[i, j] = MappingMatrix[i, j]/denominator;
                                    }
                                }
                                else
                                {
                                    double denominator = 0;
                                    for (int j = 0; j < ColumnsCount; j++)
                                    {
                                        MappingMatrix[i, j] = 1/MappingMatrix[i, j];
                                        denominator = denominator + MappingMatrix[i, j];
                                    }
                                    for (int j = 0; j < ColumnsCount; j++)
                                    {
                                        MappingMatrix[i, j] = MappingMatrix[i, j]/denominator;
                                    }
                                }
                            }
                        } // if (currentMethodId.Equals((int) DefaultMethodType.PolylineToPoint.Inverse))
                        else // MethodID != Nearest and Inverse
                        {
                            throw new Exception(
                                "methodDescription unknown for polyline to point mapping");
                        }
                    }
                    catch (Exception e) // Catch for all of the Point to Polyline part
                    {
                        throw new Exception("Polyline to point mapping failed", e);
                    }

                    #endregion
                }
                else if (((fromElements.ElementType == ElementType.XYPolyLine) ||
                          (fromElements.ElementType == ElementType.XYLine)) &&
                         toElements.ElementType == ElementType.XYPolygon)
                    // PolyLine to Polygon
                {
                    #region

                    try
                    {
                        for (int i = 0; i < RowsCount; i++)
                        {
                            XYPolygon polygon = toXYPolygons[i];

                            if (
                                CurrentMethod.ID.Equals(
                                    (int) DefaultMethodType.PolylineToPolygon.WeightedMean) ||
                                CurrentMethod.ID.Equals((int) DefaultMethodType.LineToPolygon.WeightedMean))
                            {
                                double totalLineLengthInPolygon = 0;
                                for (int n = 0; n < ColumnsCount; n++)
                                {
                                    XYPolyline polyline = CreateXYPolyline(fromElements, n);
                                    MappingMatrix[i, n] =
                                        XYGeometryTools.CalculateLengthOfPolylineInsidePolygon(
                                            polyline, polygon);

                                    IntersectedLengthMatrix[i, n] = MappingMatrix[i, n];

                                    totalLineLengthInPolygon += MappingMatrix[i, n];
                                }
                                if (totalLineLengthInPolygon > 0)
                                {
                                    for (int n = 0; n < ColumnsCount; n++)
                                    {
                                        MappingMatrix[i, n] = MappingMatrix[i, n]/
                                                              totalLineLengthInPolygon;
                                    }
                                }
                            }
                                // if (currentMethodId.Equals((int) DefaultMethodType.PolylineToPolygon.WeightedMean))
                            else if (
                                CurrentMethod.ID.Equals(
                                    (int) DefaultMethodType.PolylineToPolygon.WeightedSum) ||
                                CurrentMethod.ID.Equals(
                                    (int) DefaultMethodType.LineToPolygon.WeightedSum))
                            {
                                for (int n = 0; n < ColumnsCount; n++)
                                {
                                    XYPolyline polyline = CreateXYPolyline(fromElements, n);
                                    MappingMatrix[i, n] =
                                        XYGeometryTools.CalculateLengthOfPolylineInsidePolygon(
                                            polyline, polygon)/polyline.GetLength();

                                    IntersectedLengthMatrix[i, n] = MappingMatrix[i, n];
                                } // for (int n = 0; n < ColumnsCount; n++)
                            }
                                // else if (currentMethodId.Equals((int) DefaultMethodType.PolylineToPolygon.WeightedSum))
                            else // if MethodID != WeightedMean and WeigthedSum
                            {
                                throw new Exception(
                                    "methodDescription unknown for polyline to polygon mapping");
                            }
                        } // for (int i = 0; i < RowsCount; i++)
                    }
                    catch (Exception e) // Catch for all of polyLine to polygon
                    {
                        throw new Exception("Polyline to polygon mapping failed", e);
                    }

                    #endregion
                }
                else if (fromElements.ElementType == ElementType.XYPolygon &&
                         toElements.ElementType == ElementType.XYPoint)
                    // Polygon to Point
                {
                    #region

                    try
                    {
                        for (int n = 0; n < RowsCount; n++)
                        {
                            XYPoint point = CreateXYPoint(toElements, n);
                            for (int i = 0; i < ColumnsCount; i++)
                            {
                                XYPolygon polygon = fromXYPolygons[i];
                                if (XYGeometryTools.IsPointInPolygon(point, polygon))
                                {
                                    if (
                                        CurrentMethod.ID.Equals(
                                            (int) DefaultMethodType.PolygonToPoint.Value))
                                    {
                                        MappingMatrix[n, i] = 1.0;
                                    }
                                    else // if currentMethodId != Value
                                    {
                                        throw new Exception(
                                            "methodDescription unknown for polygon to point mapping");
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception e) // catch for all of Polygon to Point
                    {
                        throw new Exception("Polygon to point mapping failed", e);
                    }

                    #endregion
                }
                else if (fromElements.ElementType == ElementType.XYPolygon &&
                         ((toElements.ElementType == ElementType.XYPolyLine) ||
                          (toElements.ElementType == ElementType.XYLine)))
                    // Polygon to PolyLine
                {
                    #region

                    try
                    {
                        for (int i = 0; i < RowsCount; i++)
                        {
                            XYPolyline polyline = CreateXYPolyline(toElements, i);
                            if (
                                CurrentMethod.ID.Equals(
                                    (int) DefaultMethodType.PolygonToPolyline.WeightedMean) ||
                                CurrentMethod.ID.Equals(
                                    (int) DefaultMethodType.PolygonToLine.WeightedMean))
                            {
                                for (int n = 0; n < ColumnsCount; n++)
                                {
                                    XYPolygon polygon = fromXYPolygons[n];
                                    var intersectedLength = XYGeometryTools.CalculateLengthOfPolylineInsidePolygon(polyline, polygon);
                                    MappingMatrix[i, n] = intersectedLength/polyline.GetLength();

                                    IntersectedLengthMatrix[i, n] = intersectedLength;
                                }
                                double sum = 0;
                                for (int n = 0; n < ColumnsCount; n++)
                                {
                                    sum += MappingMatrix[i, n];
                                }
                                for (int n = 0; n < ColumnsCount; n++)
                                {
                                    MappingMatrix[i, n] = MappingMatrix[i, n]/sum;
                                }
                            }
                                // if (currentMethodId.Equals((int) DefaultMethodType.PolygonToPolyline.WeightedMean))
                            else if (
                                CurrentMethod.ID.Equals((int) DefaultMethodType.PolygonToPolyline.WeightedSum) ||
                                CurrentMethod.ID.Equals((int) DefaultMethodType.PolygonToLine.WeightedSum))
                            {
                                for (int n = 0; n < ColumnsCount; n++)
                                {
                                    XYPolygon polygon = fromXYPolygons[n];
                                    var intersectedLength = XYGeometryTools.CalculateLengthOfPolylineInsidePolygon(polyline, polygon);
                                    MappingMatrix[i, n] = intersectedLength/polyline.GetLength();
                                    IntersectedLengthMatrix[i, n] = intersectedLength;
                                }
                            }
                                // else if (currentMethodId.Equals((int) DefaultMethodType.PolygonToPolyline.WeightedSum))
                            else // currentMethodId != WeightedMean and WeightedSum
                            {
                                throw new Exception(
                                    "methodDescription unknown for polygon to polyline mapping");
                            }
                        }
                    }
                    catch (Exception e) // catch for all of Polygon to PolyLine
                    {
                        throw new Exception("Polygon to polyline mapping failed", e);
                    }

                    #endregion
                }
                else if (fromElements.ElementType == ElementType.XYPolygon &&
                         toElements.ElementType == ElementType.XYPolygon)
                    // Polygon to Polygon
                {
                    #region

                    try
                    {
                        for (int i = 0; i < RowsCount; i++)
                        {
                            XYPolygon toPolygon = toXYPolygons[i];
                            for (int j = 0; j < ColumnsCount; j++)
                            {
                                XYPolygon fromPolygon = fromXYPolygons[j];
                                MappingMatrix[i, j] =
                                    XYGeometryTools.CalculateSharedArea(toPolygon,
                                                                        fromPolygon);
                            }
                            if (
                                CurrentMethod.ID.Equals(
                                    (int) DefaultMethodType.PolygonToPolygon.WeightedMean))
                            {
                                double denominator = 0;
                                for (int j = 0; j < ColumnsCount; j++)
                                {
                                    denominator = denominator + MappingMatrix[i, j];
                                }
                                for (int j = 0; j < ColumnsCount; j++)
                                {
                                    if (denominator != 0)
                                    {
                                        MappingMatrix[i, j] = MappingMatrix[i, j]/
                                                              denominator;
                                    }
                                }
                            }
                                // if (currentMethodId.Equals((int) DefaultMethodType.PolygonToPolygon.WeightedMean))
                            else if (
                                CurrentMethod.ID.Equals(
                                    (int)
                                    DefaultMethodType.PolygonToPolygon.WeightedSum))
                            {
                                for (int j = 0; j < ColumnsCount; j++)
                                {
                                    MappingMatrix[i, j] = MappingMatrix[i, j]/
                                                          toPolygon.GetArea();
                                }
                            }
                                // else if (currentMethodId.Equals((int) DefaultMethodType.PolygonToPolygon.WeightedSum))
                            else // currentMethodId != WeightedMean and WeightedSum
                            {
                                throw new Exception(
                                    "methodDescription unknown for polygon to polygon mapping");
                            }
                        }
                    }
                    catch (Exception e) // catch for all of Polygon to Polygon
                    {
                        throw new Exception("Polygon to polygon mapping failed", e);
                    }

                    #endregion
                }
                else if (fromElements.ElementType == ElementType.XYPolyLine &&
                    toElements.ElementType == ElementType.XYPolyLine)
                // PolyLine to PolyLine
                {
                    #region

                    if (toElements.Equals(fromElements) && CurrentMethod.ID.Equals((int)DefaultMethodType.PolylineToPolyline.Value))
                    {
                        int nElements = fromElements.ElementCount;

                        if (useSparseMatrix)
                        {
                            MappingMatrix = new DoubleSparseMatrix(nElements, nElements);
                        }
                        else
                        {
                            MappingMatrix = new DoubleMatrix(nElements, nElements);
                        }

                        for (int i = 0; i < nElements; i++)
                        {
                            MappingMatrix[i, i] = 1;
                        }
                    }
                    else
                    {
                        throw new Exception("Polyline to Polyline mapping failed");
                    }
                    #endregion
                }
                else // if the fromElementType, toElementType combination is no implemented
                {
                    throw new Exception(
                        "Mapping of specified ElementTypes not included in ElementMapper");
                }
            }
            catch (Exception e)
            {
                throw new Exception("UpdateMappingMatrix failed to update mapping matrix", e);
            }
        }