3

I am trying to write an add-in for a finite element geology piece of software.

I have three arrays which are essentially a coordinate system. I want to assign a value to a variable depending on its position in the grid system. Basically I want to say if my node is within an x range and a y range, then my aquifer thickness at this node is this value. So far I have this.

//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 how the nested for loops will loop through 110 times, but i don't know how to assign my aquifer thickness from my array to each loop.

I open to any way of solving this problem as I'm very new new to programming and am still not sure which is the best way to achieve things.

8
  • Can you give more data? What does nodeX and aquiferThickness hold? Commented Jul 29, 2015 at 11:29
  • 8
    We can't answer this question directly, as we don't know the relation of your x and y coordinates to your double[110] (why isn't it a double[121] btw?). You should consider to use a two-dimensional array for your thickness, instead of the one-dimensional one, so you could just access it via aquiferThickness[x][y], otherwise, your access would probably be something similar to aquiferThickness[10 * x + y] = .... Commented Jul 29, 2015 at 11:30
  • Yes the nodeX and nodeY value are passed from the software and are defined by the mesh properties, so can be any x or y value. The aquiferThickness can be any value i choose. Commented Jul 29, 2015 at 11:45
  • Do you need the thickness at the closest known point or an interpolated value between defined grid coordinates? (Please add this information to your question) Commented Jul 29, 2015 at 11:46
  • 1
    I agree with @LInsoDeTeh. Most likely the best solution is going to be a complete rewrite of how you're attempting to do this, but you don't give nearly enough information for us to accomplish that. Unfortunately, if you did, the question would then be out of scope for the site. Commented Jul 29, 2015 at 11:46

2 Answers 2

1

Just use i * xcoord.Length + j insted of ?

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 take 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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Houssein Narimani Rad. Yea the [i * xcoord.Length + j] does the trick perfect. I knew it was something simple. Thanks a lot for the help.
@GeorgeFrench glad to be helpful
1

You want to use a two-dimensional array for your 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
}; 

You can then address the data using the two coordinates:

aquiferThickness = aquiferThicknessPoints[j, i];

(or i, j, it's not obvious how your data is organized)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.