Reading a two-dimensional array in namelist, f90

In summary, the programmer wants to read two-dim arrays in f90, but does not know how to do so. They generate a namelist of the array values and then write the namelist to a file. They then open the file and read the values.
  • #1
solarblast
152
2
I'd like to create some test data to read 2-dim arrays in f90 with namelists. Suppose I have this in the main program:

integer, dimension(2,3) array_in, array_out
namelist /WW_ARRAY/ array_in, array_out
...
Open(unit=WW_ARRAY_infile, file="WW_ARRAY_in.dat", status = "OLD")
...

ww_array_in.dat might look like this:

&WW_ARRAY
array_in(1:1) = 1,
array_in(1:2) = 2,
array_in(2:1) = 3,
array_in(2:2) = 4

but maybe there's any easier way to write this?
 
Technology news on Phys.org
  • #2
What language is this?
 
  • #3
Whovian said:
What language is this?

If you don't know, you are not a http://en.wikipedia.org/wiki/Real_Programmer :smile:

I can't answer the OP's question, but I would expect you can input the array column by column just as a list of numbers, without all the "array_in(1:1)" stuff. It's "read the manual" time...
 
  • #4
This does a better job of illustrating what happens by looking what a program can output for a 2-d array.

! write/read array data
integer :: n=1
real :: delta = 0.12

real, dimension(1:5, 1:2) :: astro_data
namelist /astro_nml/ astro_data, a, b, c
real :: a, b
character*12 :: c
integer :: astro_file = 10

! generate astro_nml values
a=1.5
b=-3.5
c="just c"

do j = 1,5
do k = 1,2
astro_data(j,k) = j*1.0 + (k-1)*delta
end do
end do
write(*, ("(10x, a20)")) "ARRAY EXAMPLE"
Write(*, nml=astro_nml)

Open(unit=astro_file, file="z-astro_file.dat", status = "REPLACE")
Write(unit=astro_file, nml=astro_nml)
Close(unit=astro_file)
end

produces:
ARRAY EXAMPLE
&ASTRO_NML
ASTRO_DATA= 1.0000000 , 2.0000000 , 3.0000000 , 4.0000000 , 5.0000000 ,
1.1200000 , 2.1199999 , 3.1199999 , 4.1199999 , 5.1199999 ,
A= 1.5000000 ,
B= -3.5000000 ,
C="just c ",
/
 
  • #5
As far as I know, there is no manual for Gfortran f90 under MinGW. I find myself writing in fortran several decades after I did it long ago out of necessity. I've picked up a few pdf files on it via Google, but they are somewhat incomplete, about a 100 pages or fewer. A few books I've found on Amazon about it cost $100+. No thanks on that. Fortunately, my task is confined to taking apart a few data files created with formats, and convert them into namelists. Lucky me. :-)
 
  • #6
Do you really NEED to convert them to namelists? I have never use namelists, I am doing fine.

By the way, there are quite a few on-line resources for Fortran; no need to buy a book.
 
  • #7
Absolutely need them for my application. They are read as input to a computation program.
 
  • #8
So, you do no have the source to such "computation program" and that program has already been compiled, then.

Otherwise, if you have the source to the program, nothing prevents you from simply reading values the typical way.

Anyway, I guess is a matter of preference; it's just that namelists never seem to come up to the point that I thought nobody used them.

Just like the local variables of third party library subroutines or functions, you do not need to know the name of the variables in them, you just need to pass values...this allows the programmer to change variable names to something more meaningful if so desired.

...with namelists, you need to keep variable names in program source and input files in sync.

my 2 cents

should you decide to do away from namelists, I am sure we can show you very efficient ways to read data.
 

1. How do I declare and initialize a two-dimensional array in namelist?

To declare and initialize a two-dimensional array in namelist, you can use the following syntax:
array_name(1:n, 1:m) = (/value1, value2, ..., valueN/)
where n and m are the size of the array in the first and second dimension, respectively. The values inside the parentheses should be separated by commas and enclosed in forward slashes.

2. How do I access and modify elements in a two-dimensional array in namelist?

To access a specific element in a two-dimensional array, you can use the following syntax:
array_name(i, j)
where i and j represent the indices of the element you want to access. To modify an element, you can assign a new value to it using the same syntax.

3. Can I use a namelist to read a two-dimensional array from a file?

Yes, you can use namelist to read a two-dimensional array from a file. You can specify the input file in the namelist statement and use the read keyword to read the values into the array. Make sure the file contains the correct number of values and is formatted properly.

4. How do I print out the values of a two-dimensional array in namelist?

To print out the values of a two-dimensional array, you can use a do loop to iterate through each element and use the print keyword to display the values. Alternatively, you can use the write keyword to write the values to a file.

5. Can I pass a two-dimensional array as an argument to a subroutine in namelist?

Yes, you can pass a two-dimensional array as an argument to a subroutine in namelist. You can declare the array in the subroutine's argument list and use the same syntax as in the main program to access and modify the elements of the array. Any changes made to the array within the subroutine will also be reflected in the main program.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
741
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
3K
Back
Top