LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   FORTRAN90 - Update of arrays in a loop (https://www.linuxquestions.org/questions/programming-9/fortran90-update-of-arrays-in-a-loop-4175412462/)

bldcerealkiller 06-20-2012 08:19 AM

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.

tronayne 06-20-2012 08:50 AM

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)
or
Code:

array(565) = x
(No, FORTRAN does not have to be upper case.)

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
where i = 565 and j = 2, and
Code:

x = array (i, j)
to use the value.

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.

bldcerealkiller 06-20-2012 10:10 AM

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:

...

do i =1,20

[...operations to obtain "number1", "number2" etc.]

f_array(i)        = f_array(i)    + number1
f_array(i+x)      = f_array(i+x)  + number2
f_array(i+2*x)    = f_array(i+2*x) + number3

end do

So as you can see, in the end i'll get f_array which includes the result of all of the iterations of the loop.
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

tronayne 06-21-2012 08:28 AM

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
    s_array(i) = f_array(i)
end do

Then print it, write it to a file or something so you can look at it.

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.

bldcerealkiller 06-24-2012 06:01 AM

Yes thank you I was thinking about something like that.
Cheers

Nominal Animal 06-24-2012 04:07 PM

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)
Note that you can use a slicing to initialize an array, too:
Code:

f_array(1:n) = 0.0
If your calculation is done using only PURE functions, for example using mycalculation(), then you can use
Code:

FORALL (i = 1:n)
    s_array(i) = mycalculation(arguments...)
END FORALL

f_array(1:n) = f_array(1:n) + s_array(1:n)

which turns out to compile to quite efficient code.


All times are GMT -5. The time now is 12:23 PM.