System.IndexOutOfRangeException

0

Witam,
Podczas pisania prostego programu dotyczącego metody elementów skończonych natknąłem się na problem.
Chcąc wygenerować macierz globalną, będącą sumą macierzy lokalnych poszczególnych elementów wyrzuca mi System.IndexOutOfRangeException mimo, iż wydaje mi się, że nie przekraczam zakresu. Męczę się już z tym kilka godzin i nie mogę znaleźć rozwiązania. Problem dotyczy funkcji generateGlobalMatrix, kiedy z wygenerowanych macierzy lokalnych klasy Element próbuję utworzyć macierz globalną będącą sumą macierzy lokalnych poszczególnych elementów. Proszę o pomoc :)

Klasa Element:

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MES_1
{
    class Element
    {
        //PARAMETERS:

        public double k; //współczynnik przewodzenia ciepła
        public double l; //długość elementu
        public double s; //pole powierzchni
        public int np1; // węzel 1
        public int np2; // węzeł 2
        public int bc; //warunki brzegowe strumień - 0, konwekcja - 1, brak - (-1)
        public double[,] hl = new double[2, 2];
        public double[] pl = new double[2];

        //METHODS:

        public Element(double k, double l, double s, int np1, int np2, int bc)
        {
            this.k = k;
            this.l = l;
            this.s = s;
            this.np1 = np1;
            this.np2 = np2;
            this.bc = bc;
        }
    
        public void showElement()
        {
            Console.WriteLine("k = " + k);
            Console.WriteLine("l = " + l);
            Console.WriteLine("s = " + s);
            Console.WriteLine("np1 = " + np1);
            Console.WriteLine("np2 = " + np2);
            Console.WriteLine("hl:");
            this.showHl();
            Console.WriteLine("pl:");
            this.showPl();
            Console.WriteLine("*************");
        }
        public void prepareElements(double alfa, double q,double tn)
        {
            prepareHl(alfa);
            preparePl(alfa, q, tn);
        }
        private void prepareHl(double alfa)
        {
            double c = (this.s * this.k) / this.l;
            for(int i = 0; i < 2; i++)
            {
                for(int j = 0; j < 2; j++)
                {
                    if(i!=j) // make the -
                        c *= -1;
                    this.hl[i, j] = c;
                    if (i != j) // back to +
                        c *= -1;
                    if (this.bc == 1 && i==1 && j==1) this.hl[i, j] += alfa * this.s; // set the alfa * s  condition
                }
            }
        }

        public void showHl()
        {
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    Console.Write(this.hl[i, j]+"   ");
                }
                Console.WriteLine();
            }
        }

        private void preparePl(double q, double alfa, double tn)
        {
            if(this.bc!=-1)
            {
                if (bc == 0)
                    this.pl[0] = q * this.s;
                else
                    this.pl[1] = -q * this.s * tn;
            }
        }
        public void showPl()
        {
            for (int i = 0; i < 2; i++)
                Console.WriteLine(this.pl[i]);
        }
    }
}

Program:

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MES_1
{
    class Program
    {
        static void Main(string[] args)
        {
            Element[] elements = new Element[3];
            elements[0] = new Element(1, 1, 1, 1, 2, 0);
            elements[1] = new Element(1, 1, 1, 2, 3, 1);
            int ne = elements.Length;
            double[,] globalH = new double[ne, ne];
            double[] globalP = new double[ne];
            for (int i = 0; i < elements.Length-1; i++ )
            {
                elements[i].prepareElements(2,3,20); // arguments: alfa,q,tn
                Console.Write("Element ");
                Console.WriteLine(i + 1);
                elements[i].showElement();
            }
            generateGlobalMatrix(ne, elements, globalH, globalP);
            Console.WriteLine("Global H matrix: ");
            showGlobalH(ne, globalH);
            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }
        public static void generateGlobalMatrix(int ne,Element[] elements, double[,] globalH, double[] globalP)
        {
            for (int i = 0; i < ne; i++)
            {
                globalH[elements[i].np1, elements[i].np1] += elements[i].hl[0, 0];
                globalH[elements[i].np1, elements[i].np2] += elements[i].hl[0, 1]; // problem występuje tutaj  dla i = 1 i dotyczy hl (tak mi się wydaje)
                globalH[elements[i].np2, elements[i].np1] += elements[i].hl[1, 0];
                globalH[elements[i].np2, elements[i].np2] += elements[i].hl[1, 1];

                globalP[elements[i].np1] += elements[i].pl[0];
                globalP[elements[i].np2] += elements[i].pl[1];

            }
        }

        public static void showGlobalH(int ne,double[,] globalH)
        {
             for(int i = 0 ; i < ne ; i++)
             {
                 for (int j = 0; j < ne ; j++)
                     Console.Write(globalH[i, j] + "   ");
                 Console.WriteLine();
             }
        }
    }
}

1

Okej tutaj masz problem:

globalH[elements[i].np1, elements[i].np2] += elements[i].hl[0, 1];

Podstawiasz globalH[2,3] a możesz maksymalnie globalH[2,2], więc wychodzisz poza zakres.

Poza tym ostatni element w elements jest nullem więc późnij i tak Ci się to znowu wywali tym razem będziesz miał NullReferenceException

1 użytkowników online, w tym zalogowanych: 0, gości: 1