CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Main CFD Forum (https://www.cfd-online.com/Forums/main/)
-   -   Fortran, does anyone know how.. (https://www.cfd-online.com/Forums/main/2314-fortran-does-anyone-know-how.html)

Frank Muldoon June 30, 2000 04:52

Fortran, does anyone know how..
 
if it is possible to pass an array to a subroutine without passing it's bounds and have the subroutine get the correct bounds? The following runs but gives bounds from 1 to 9 for a in the subroutine.

Thanks, Frank

real, dimension(2:10) :: a

a=1.2 a(2)=2.2

call sub(a) end

subroutine sub(a) real, dimension(:) :: a write(*,*)lbound(a,1),ubound(a,1) return end subroutine sub

Nishikawa June 30, 2000 13:05

Re: Fortran, does anyone know how..
 
One way to do something like that would be to use MODULE. Define the following module.

MODULE ARRAY_a

real, dimension(2:10) :: a

END MODULE ARRAY_a

The array "a" is then accessible to anywhere by just typing "USE ARRAY_a". Well, it's like a COMMON in Fortran77.


Shigunov July 2, 2000 04:20

Re: Fortran, does anyone know how..
 
It is possible to do so:

subroutine sub(a) real, dimension(2:) :: a write(*,*)lbound(a,1),ubound(a,1) return end

subroutine sub

Then the bounds of a will be correct

With best regards

John C. Chien July 2, 2000 11:21

Re: Fortran, does anyone know how..
 
(1). I have been studying C/C++, and I can say that you are not using C/C++. (2).I don't think you are using f77 either, because "::" is the scope operator in C++, such as the member function of a class. (3). I can only say that you are using a new language, a version of Fortran or something like that. (4). If the language is not clearly defined, then it is not a good language. (5). If you have to use it (the only compiler available to you), then try to use only the standard features which are backward compatible to f77. (6). Trying to use new features of a language can only make the code less portable and very hard to read later on. (7). This is just a comment, and it does not solve your question right away.

Nishikawa July 2, 2000 21:34

Re: Fortran, does anyone know how..
 
Yes! That's it. But, just make sure that you have the interface:

INTERFACE

SUBROUTINE SUB(a)

real, dimension(:) :: a

END SUBROUTINE

END INTERFACE

in the main program. Without this, it won't work.

(If the subprogram is contained in a module, the interface is not necessary, of course.)

BTW, I'm assuming that you're using Fortran 90.

Frank Muldoon July 3, 2000 14:55

Re: Fortran, does anyone know how..
 
This will work for the case of a lower bound = 2. However I need to be able to call the subroutine with varying upper and lower bounds without passing in the bounds with the call. This is because of multigrid. It would seem like there should be away to do this. In answer to John C. Chien this is Fortran 90/95.

Thanks Frank

Nishikawa July 3, 2000 15:23

Re: Fortran, does anyone know how..
 
Well, how about using the data module as I said in my first message? This way, you can use "a" just as you defined in the module anywhere in the entire program. No need to pass the bounds as well as "a".

Frank Muldoon July 3, 2000 17:34

Re: Fortran, does anyone know how..
 
That is actually what I am currently doing. However that means that I have to have a separate subroutine to say compute the convective terms for each grid level in multigrid. This is because I have to have a separate module for each grid level in multigrid which will be accessed by a use statement in the respective subroutine. In general what I want is to have all dimension statements in one location (the module).

Thanks, Frank

John C. Chien July 3, 2000 20:28

Re: Fortran, does anyone know how..,Thank your .
 
(1). Thank you for letting me know that it is Fortran 90/95. (2). I did a brief Internet search and found a tutorial site. (3). In the tutorial, there is a simple sample written in f77 and Fortran 90. (4). All I can say is, I don't like the new language. (5). The f77 is:DIMENSION B(3) (6). DATA B/1.,2.,3./ (7). The f90 is: Real, DIMENSION(1:3) :: B=(/1.,2.,3./) (9). The main reason is: /1.,2.,3./ is the f77 approach. In C/C++, it is : {1.,2.,3.}. Now we have this double standard, that is : (/1.,2.,3./). (10). Also the scope definition (::) in C/C++ is not the same as (::) in Fortran 90/95. (11). The use of Real, DIMENSION(1:3) require the use of a comma (,) It becomes very hard to read the whole statement. (12). I think, the new language is not user-friendly. Well, that's my first impression. Now we need to learn three ways to initialize an array, /1,2,3/ , {1.2,3}, (/1.2,3/). And this is definitely not a user-friendly trend.

Nishikawa July 3, 2000 21:15

Re: Fortran, does anyone know how..
 
I guess I don't understand what you want to do.

If you store all the information in a single array, you'll need some pointers for each grid level (where the data start on a particular grid level. And in this case you must pass the bounds to subroutines to let them know to which grid it should access).

If you store a set of information separately for each grid, you can simplify your operations, but still you'll need to tell the code to which grid it should access, and what you're doing is one way. (But I don't think you need a subroutine for each data set(grid) to do the same thing. Just use the function SIZE, and the subroutine understands on which grid "a" is defined, provided the subroutine knows the size of each grid.)

Do you want to do without such pointers? I think you need something like that however you write a multigrid code. Or, am I missing something?


Frank Muldoon July 4, 2000 03:40

Re: Fortran, does anyone know how..
 
In general what I want is to have all dimension statements that contain bounds in one location (the module) of which there will be one for each grid. I would like to do the following:

!-----------------------------------------

module def

contains

subroutine sub1(a)

real, dimension(:) :: a

write(*,*)'sub1',lbound(a),ubound(a)

return

end subroutine sub1

end module def

program main

use def

real,allocatable, dimension(:) :: a

allocate(a(2:12))

a=1.0

call sub1(a)

end program main

!-----------------------------------------

However this returns bounds from 1 to 11 in the subroutine.

Another way of expressing what I want to do is below. It does not compile on a Alpha but does on an IBM SP2 and then gives bounds from 1 to 804400217

!-----------------------------------------

program main

real, dimension(2:12) :: a

a=1.0

call sub1(a)

end program main

subroutine sub1(a)

real, dimension(:) :: a

write(*,*)'sub1',lbound(a),ubound(a)

return

end subroutine sub1

!-----------------------------------------

Thanks,

Frank

Chidu July 28, 2000 14:08

Re: Fortran, does anyone know how..
 
Hi Frank,

Did you find out a way to do that? Infact I encountered a similar problem but in this case my array lower bound was fixed to 0 instead of the normal 1 and if I just defined this array as a(:,:) then I was unable to access the zeroth element inside the subroutine. I had to finally define it as a(0:,:) and then it worked!

I think there is a problem in F90 in this regard. Let me know if you find something.

Thanks. chidu...


All times are GMT -4. The time now is 17:25.