Hello everyone,
I am desperately looking for the mistake in my code. I am implementing a CFD code and i don't understand why the method can't be run, when called. I assume it has either sth to do with
-solving for tempF[][i+1]=… OR
-looping only up to i<numTotSteps even though the Array has tempF[][numTotSteps+1] elements. But obviously, since I am solving for tempF[][i+1] I don't want to loop up to numTotSteps+1.
The dimensions of the Array are tempF[numCells][numTotSteps+1] whereas at tempF[][0] the initial values are stored.
I am super happy for feedback. The error I get is "Status -1073741819". I found one post on it where it had sth to do with the scope of looping, but I don't understand why my implementation wouldn't work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
for (int i=0;i<numTotSteps;i++){
switch(state[i]){…} //vector has numTotSteps elements, and depending on i, the velocity is set differently
for (int j=0;j<numCells;j++){
if(j==0 && velocity>0){
tempF[j][i+1] = tempF[j][i]-velocity*deltaTime/deltaDistance*(tempF[j][i]-tempCharge)
+constF*deltaTime/pow(deltaDistance,2)*(tempF[j+1][i]-tempF[j][i]);
break;
} elseif(j==(numCells-1) && velocity<0){
tempF[j][i+1] = tempF[j][i]-velocity*deltaTime/deltaDistance*(tempDischarge-tempF[j][i])
+constF*deltaTime/pow(deltaDistance,2)*(tempF[j-1][i]-tempF[j][i]);
break;
} else {
tempF[j][i+1] = tempF[j][i]-velocity*deltaTime/deltaDistance*(tempF[j][i]-tempF[j-1][i])
+constF*deltaTime/pow(deltaDistance,2)*(tempF[j+1][i]-2*tempF[j][i]+tempF[j-1][i]);
}
}
}
Your program crashed, is basically what that means. Usually, this means that you attempted to access memory out of bounds, if you're dealing with arrays.
What is the size of your tempF array? is it [numCells] by [numTotSteps]? The other way around?
I bet that your [i+1] or [j+1] part of your code is what's going out of bounds.
Also, you're looping i on the outside and then j on the inside, but then accessing temp[j][i]. If you're access pattern is [j][i], then loop j on the outside and i on the inside. This last point isn't a matter of accuracy/correctness, but of performance. Prefer row-major access. (But it might also be pointing to a logic error)
For example how was tempF defined (what is the sizes of the array), what are the values of numTotSteps and numCells?
Also I wonder if you don't have the order of i and j wrong when using tempF. Looking at your loops I would expect tempF[i][j] not tempF[j][i]. And be careful with with that i + 1, it might be possible that you're trying to access the array out of bounds on the last increment.
I have just updated my post - the dimenstions are tempF[numCells][numTotSteps+1]. Values are integers (numCells=10, numTotSteps+1=41, but can be any integer).
I will change the order of looping then, but from what you say this shouldn't cause the crash but only decrease the performance.
I only loop up to i<numTotSteps exactely not to exceed the bounds for tempF[...][i+1]. Where do I not see my error?
Thank you so much for the fast Reply!
***One more update: the tempF Array is initialized with the same value for all Elements when passed into the loop
You may well be outside your array bounds in j. If velocity has a different sign to what you hoped on lines 4 and 8, then you may well get to lines 13 and 14 with j either 0 or numcells-1, which will then go outside array bounds on line 14. In fact since velocity can't be both positive and negative you are bound to fail at one end of the array.
If you decide to change the order of looping then you don't understand timestepping. But it's extremely odd to store more than two or three time levels at a time anyway: you are only doing first-order explicit timestepping on an advection-diffusion problem. You are simply going from the current time level i to the next time level i+1. You just need two time-level arrays, not a 2-d array. You also have a basic assumption in the last two terms of line 13 that your velocity is positive.
The Problem was indeed the looping. I have reinitialized/redefined the Arrays and adapted the code. Now it works.
Can it be, that the performance got so bad from the "wrong order" that it would lead to crashing?
You are bound to go outside the array bounds in j, irrespective of the order of looping.
Your time loop (here, i) has to be the outer loop.
Your space loop (here, j) has to be the inner loop.
Nothing else would make physical sense.
I repeat: you don't need an array in the time (i) direction - you only simultaneously handle two time levels, each of which can have a 1-d space array.
Your break; statement on line 7 is also wrong.
The fact that it may now compile and "run" doesn't make it right.
> you don't need an array in the time (i) direction
sure, if you only care about the state at the end.
You dump any time-level data you really want to file as and when. It's unlikely you would need every time level stored as that method (explicit forward differencing) for that equation (time-dependent advection-diffusion) will need a very small timestep. You don't need more than two time levels at any time to do the calculations.