On Sunday, November 7, 2021 at 8:43:25 AM UTC-8, majm...@umn.edu
> I am trying to perform a 4th order Runge-Kutta integration of two coupled complex-valued differential equations in Fortran 90 using the gcc compiler. Following are what I think the relevant snippets of my code:
> !!!!! RK4 integration using functions fb and fe which are defined later. The increment is over an index k. The increment is from k to k+2 because of the way the system is constructed and is not important for this question.
> !!!!! The external functions 1are given below. array1 and array2 are globally defined arrays. constant is a globally defined real number.
There aren't global variables in Fortran. There is COMMON, which is sometimes called global, but needs to be declared.
For internal procedures, you get access to host variables. I presume this is what you mean, but the details aren't shown.
> complex(8) function fb(f1)
> complex(8) :: ij = (0,1)
> complex(8),intent(in) :: f1
> fb = (ij*constant-array1)*f1/(array2)**2
> end function fb
Assuming array1 and array2 are arrays, this doesn't make sense.
> The given code compiles but doesn't increment the functions of interest (f1 and f2).
> The first value itself (k=1) is nonsensical and thereafter I get NaNs. The initial values given to them are sensible.
You have to initialize the k=1 elements with the initial value. (That is, this is an initial value problem.)
If they are being changed, then something is very wrong.
> I suspect this is because f1 and f2 are arrays and so they need to be defined as such inside the external functions fe and fb. However, when I do so, gcc gives me an incompatible rank error between fe and f2, since I defined both fe and fb as complex scalars.
When I write these, I use scalar variables for the k's. They are not needed between iterations,
though for debugging purposes, it is sometimes nice. Then I usually just print them out
inside the loop, but could also be assigned to array elements, but only for debugging.
(Partly because it might be faster as scalars.)
> But when I define fe and fb as complex arrays, gcc shoots out an error which says that "array index must be of type integer" inside the various k functions, which makes sense.
You can write functions that return arrays. That isn't usual, but could be used for
solving mutliple equations at the same time. In this case, you are solving two equations
(the one with b and the one with e), which could be done with two element arrays.
As they are independent, I would probably give them separate loops (it sometimes
helps the optimization), but otherwise it works both ways.
> So I need help in marrying these two issues: to get fe and fb to read
> f1 and f2 as arrays without being arrays themselves.
It isn't so obvious what should depend on what.
Can you write the differential equations in math form, so it is more
obvious what is variable and what isn't?