Getting the name of an array from a string
Getting the name of an array from a string
(OP)
Hello everybody,
I have a question related to fortran 90. I have a subroutine named for instance f1 that needs an array 'a' (defined as double precision) and gives some output. That is ----> f1(a,output).
Now, imagine that I would like to call the subroutine f1, but giving the name of the array 'a' from a string. For example, imagine that I have three arrays named 'array1', 'array2' and 'array_test' and at a given stage of the program I create a string named 'array1' and I try that the subroutine gets it as the double percision array (array1). I think there should be some way to do it, but as I am sure that someone, a part from me, should have thought on this question, I would appreciate if you could give me a posible solution.
Thank you very much in advance
I have a question related to fortran 90. I have a subroutine named for instance f1 that needs an array 'a' (defined as double precision) and gives some output. That is ----> f1(a,output).
Now, imagine that I would like to call the subroutine f1, but giving the name of the array 'a' from a string. For example, imagine that I have three arrays named 'array1', 'array2' and 'array_test' and at a given stage of the program I create a string named 'array1' and I try that the subroutine gets it as the double percision array (array1). I think there should be some way to do it, but as I am sure that someone, a part from me, should have thought on this question, I would appreciate if you could give me a posible solution.
Thank you very much in advance
RE: Getting the name of an array from a string
CODE
CODE
RE: Getting the name of an array from a string
RE: Getting the name of an array from a string
Many thanks in advance
RE: Getting the name of an array from a string
Of course you can create the string and pass it to the procedure but you will still need the IF logic in the procedure, for example you can declare a subroutine which argument ist the string:
CODE
and then in your main program create the string and then call the subroutine like this
CODE
RE: Getting the name of an array from a string
Thanks you very much for your fast answers. I like to enter this forum to share things with programmers
RE: Getting the name of an array from a string
By the way, if you think that an "if" evaluation is time consuming, you should check how long the string concatenation operation takes.
Another thing I would suggest doing is simply to create a short array of pointers and have them point to the beginning of each of the arrays:
arrptr(1) => array1(1)
arrptr(2) => array2(1)
arrptr(3) => array3(1)
arrptr(4) => array4(1)
then, getting back to your loop, you don't need an 'if' statement and you can simply use the loop variable:
do i = 1,4
do_with_array( arrptr(i) )
end do
RE: Getting the name of an array from a string
RE: Getting the name of an array from a string
Thanks
RE: Getting the name of an array from a string
CODE
Output:
CODE
RE: Getting the name of an array from a string
In any case, I'll be honest...I think I spoke too soon, as I am yet to use pointers in Fortran. I was just thinking back to my C days where you could get a pointer point to a spot in memory and pass that address around.
A little googling and, apparently, that is not the case in Fortran; in Fortran, for some reason, the pointer needs to be the same rank as the target.
So, I tried to do something similar to what mikrom shows and while I was able to have the matrices co-exist and display them one a time with separate calls to the subroutine, I started to get an error as soon as I tried to display them with the same, single subroutine call within a DO-loop...things got fixed as soon as I added an interface to the subroutine...go figure.
Here is the approach:
- Declare (target) 2D matrices to process
- Define a derived type containing a single 2D-matrix pointer
- Declare 1D-array variable of the derived type
- Define an interface to the processing subroutine
Here is the source code:CODE
CODE
Needless to say, my data blocks initialize the matrices column-wise as Fortran internal storage scheme is column-major.
RE: Getting the name of an array from a string
CODE
Output:
CODE
RE: Getting the name of an array from a string
While I am using Fortran 90 features with the pointer and the explicit interface, let's go ahead and truly do it the proper Fortran 90 style of using a module as mikrom does. The thing is that modules do a bit more than simply grouping stuff, as shown by the two programs above...mine without module needs an explicit interface, mikrom's with a module does not need an explicit interface...it is provided by the module, behind the scenes, as a free service.
RE: Getting the name of an array from a string
Although I do not know detailed technical differences between using of modules and not using them, but I have learned that using them can save me a lot of troubles
RE: Getting the name of an array from a string
CODE
CODE
RE: Getting the name of an array from a string
Thanks
RE: Getting the name of an array from a string
RE: Getting the name of an array from a string
As you can see, mikrom is already showing that the same kind of pointer can be use to point to matrices if different sizes...for as long as all your matrices are 2D, you should be o.k. If, one of your matrices a different number of dimensions, like 3, 4, or 5 or whatever, than, you can not use the same array of pointers.