FORTRAN90 - Update of arrays in a loop
Hi everybody, I'm struggling to find a way out hope you can help me, or at least tell me that is possible to do it. Anyway any suggestion is appreciated.
I'm trying to update a code written by someone else. In this portion of the code I will obtain one final array which is updated with values obtained from a series of calculations nested in a do loop. The question is: is it possible to obtain the separate individual values obtained from the calculations for each step of the loop instead of having the final array, which is actually the result of sum of the individual calculations after every cycle of the loop? Otherwise, is it possible to create different separate arrays with every cycle of a loop? Sorry for being unclear, and thank you in advance for every suggestion/advice. |
The general form for doing something like this is, if I understand the question, to use an index value to see an individual element of an array (single- or multidimensional).
So, if you had an array of 1,000 elements and wanted to substitute or view the 565th element you wound refer to it as Code:
x = array (565) Code:
array(565) = x In a loop you'd do the same thing, just looping until you hit the right element. If you're working with a multidimensional array, you'd want to look at (or replace) the 565th row and second column Code:
array (i, j) = x Code:
x = array (i, j) Keep in mind that FORTRAN arrays start at 1 versus C arrays that start at 0. That what you're trying to do? Hope this helps some. |
Thanks for your reply tronayne but is not what I'm trying to do. I'll try to be more clear.
I have an array (f_array) which is updated at every cycle of a loop, briefly the code looks like this: Code:
... But, I'd like to be able to separate and look at each single contribution for each cycle of the loop. Is it possible to do that, for example, creating every time a separate array (different from f_array, let's say from 1 to 20) for each cycle of the loop? Thanks again |
Sure. Just create another array, same size, same type. Call it, say, s_array.
After the end of your existing loop, add another loop Code:
do i = 1, 20 There's probably some function to do an array copy but I haven't done FORTRAN for... well, a long time and I don't recall but it's simple enough to just copy. Hope this helps some. |
Yes thank you I was thinking about something like that.
Cheers |
Fortran supports array slicing, so if you have n values for this iteration in s_array, you can add them to corresponding values in f_array using a single statement:
Code:
f_array(1:n) = f_array(1:n) + s_array(1:n) Code:
f_array(1:n) = 0.0 Code:
FORALL (i = 1:n) |
All times are GMT -5. The time now is 12:23 PM. |