How to assign a variable using 3 arrays in C #

I am trying to write an add-on for a finite element software snippet.

I have three arrays which are essentially a coordinate system. I want to assign a value to a variable based on its position in the grid. Basically what I want to say is that if my node is within the x range and the y range, then my aquifer thickness at that node is equal to that value. As long as I have it.

//create an array of xcoords of data points:
double[] xcoord = new double[11] {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50};

//create an array of ycoords of data points:
double[] ycoord = new double[11] {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50};

//create an array of aquifer thickness

double[] aquiferThicknessPoints = new double[121]
        {
        10, 10, 12, 13, 12, 15, 14, 15, 14, 13, 13,
        10, 10, 13, 15, 16, 14, 13, 15, 16, 12, 13,
        12, 14, 15, 18, 19, 17, 14, 15, 18, 14, 14,
        13, 14, 15, 18, 20, 17, 15, 17, 18, 15, 15,
        14, 15, 17, 18, 21, 17, 18, 18, 19, 17, 16,
        15, 15, 17, 17, 20, 21, 21, 19, 19, 18, 18,
        15, 15, 17, 20, 20, 21, 22, 21, 19, 19, 19,
        16, 17, 19, 20, 22, 23, 22, 21, 20, 20, 20,
        17, 18, 20, 22, 23, 24, 24, 23, 22, 20, 21,
        18, 19, 21, 22, 24, 25, 24, 23, 22, 22, 22,
        19, 19, 22, 22, 24, 25, 25, 23, 23, 22, 23,
        }; 

dataPointSpacingHalf = dataPointSpacing / 2;



for (int i = 0; i < xcoord.Length; i++)
{
    for (int j = 0; j < ycoord.Length; j++)
    {
        if (nodeX >= (xcoord[i] - dataPointSpacingHalf) && (nodeX < (xcoord[i] + dataPointSpacingHalf)) && (nodeY >= (ycoord[j] - dataPointSpacingHalf) && (nodeY < (ycoord[j] + dataPointSpacingHalf))))
        {
            aquiferThickness = aquiferThicknessPoints[?];
        }
    }
}

      

I can see the loops of the nested loops going through 110 times, but I have no idea how to assign the thickness of the aquifer from my array to each loop.

I am discovering any way to solve this problem as I am very new to programming and I am still not sure if this is the best way to achieve something.

+3


source to share


2 answers


Just use i * xcoord.Length + j

insted?

Here is the code:

for (int i = 0; i < xcoord.Length; i++)
{
    for (int j = 0; j < ycoord.Length; j++)
    {
                                                      //Here is the magic!
        //without considering coordinates
        //aquiferThickness[i, j] = aquiferThicknessPoints[i * xcoord.Length + j];

        //considering coordinates
        aquiferThickness[i, j] = 
            aquiferThicknessPoints[
                CoordToIndex(xNode,indexedCoords) * xcoord.Length + 
                CoordToIndex(yNode,indexedCoords)];

    }
}

      



Also to consider the xNode, yNode coordinate you can use this approach

Dictionary<int, double> indexedCoords = new Dictionary<int, double> { { 0, 0 }, { 1, 5 }, { 2, 10 }, .... };

int CoordToIndex(double node, Dictionary<int, double> indexedCoords)
{
    return indexedCoords.First(i => i.Value > node).Key;
}

      

+1


source


You want to use a two dimensional array for aquiferThicknessPoints

:

double[,] aquiferThicknessPoints = new double[,]
{
  {10, 10, 12, 13, 12, 15, 14, 15, 14, 13, 13},
  {10, 10, 13, 15, 16, 14, 13, 15, 16, 12, 13},
  {12, 14, 15, 18, 19, 17, 14, 15, 18, 14, 14},
  {13, 14, 15, 18, 20, 17, 15, 17, 18, 15, 15},
  // the rest
}; 

      

Then you can address the data using two coordinates:



aquiferThickness = aquiferThicknessPoints[j, i];

      

(or i, j

, it's not clear how your data is organized)

+1


source







All Articles