Reading a two-dimensional array in namelist, f90

  • Thread starter Thread starter solarblast
  • Start date Start date
  • Tags Tags
    Array Reading
Click For Summary

Discussion Overview

The discussion revolves around reading two-dimensional arrays in Fortran 90 using namelists. Participants explore methods for inputting array data, share code examples, and discuss the necessity and practicality of using namelists in their applications.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Meta-discussion

Main Points Raised

  • One participant presents a code snippet for reading a 2D array using namelists and questions if there is an easier way to format the input data.
  • Several participants confirm that the language in question is Fortran, with one suggesting that inputting the array as a simple list of numbers might be possible.
  • A code example is provided that illustrates how to write and read a 2D array using namelists, demonstrating the output format.
  • One participant expresses frustration over the lack of comprehensive manuals for Gfortran under MinGW, sharing their experience of returning to Fortran after many years.
  • Another participant questions the necessity of using namelists, stating they have managed without them and suggesting that there are online resources available for learning Fortran.
  • A later reply emphasizes the need for namelists in the context of a specific application, indicating that they are required for input to a computation program.
  • One participant raises a concern about the dependency on variable names in namelists, suggesting that it may be more efficient to read data without them.

Areas of Agreement / Disagreement

Participants express differing views on the necessity and practicality of using namelists, with some advocating for their use while others suggest alternative methods. The discussion remains unresolved regarding the best approach to reading data in this context.

Contextual Notes

Participants mention limitations in available resources and manuals for Fortran, as well as the potential challenges of keeping variable names in sync between program source and input files when using namelists.

solarblast
Messages
146
Reaction score
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
What language is this?
 
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...
 
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 ",
/
 
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. :-)
 
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.
 
Absolutely need them for my application. They are read as input to a computation program.
 
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 33 ·
2
Replies
33
Views
6K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
12K