Skip read with implied do-loops in Fortran without dummy variable - fortran

I would like to skip n number of lines. It could be done as:
do i = i,n
read(file,*)
enddo
However, I would like to do that by implied do-loop without dummy variable, like
read(file,*)(,i=1,n)

READ (file, '(' // REPEAT('/',n-1) // ')')
but promise you won't do this...

Related

Meaning of this code (probably in FORTRAN 90)

I found following code in one of the frameworks we are using:
Rem = max (50.0, gm*diamj(i)/vism)
I am looking for the result of Rem. The gm, diamj, vism should be other formula or alphanumeric name.
It's fairly straightforward: it just calculates Rem from a simple formula, and then uses max to limit the result to a value of at least 50, i.e.
Rem = gm*diamj(i)/vism
if (Rem < 50.0)
Rem = 50.0
There is an answer which gives a plausible idea of what the line in the question means. However, an important thing to understand is that it isn't generally possible to isolate the individual lines of code without further context.
That is, from just this single line you cannot tell what will happen.
While there is an intrinsic function max which returns the maximum value of the two arguments we do not know that this function is being used.
Consider the case
function max(a,b)
intrinsic min
max = min(a,b)
end function max
external max
Rem = max(1., 2.)
print*, Rem
end
Here an external function called max is used, not the intrinsic.
There's also
type max
real a,b
end type
type(max) Rem
Rem = max(1., 2.)
print*, Rem
end
Here the default structure constructor for a derived type called max is used. max may even be a generic, or feature other such excitement.
Now, even if max is the intrinsic function, we don't understand the second argument in the question: diamj could be an array or a function. See
function diamj(i)
error stop
end function
Rem = max(1.,diamj(2))
print*, Rem
end
What we can safely say is that max isn't an array. However, there's
integer :: max(2,2)=-1
Rem = max(1,2)
print*, Rem
end
which is very different from
Rem = max(1,2)
print*, Rem
end
Arguably you shouldn't do any of these things but you also shouldn't assume other people haven't.

Read signed exponential

I am having trouble reading exponential from a text file using Fortran.
The entry in the text file looks like the following
0.02547163e+06-0.04601176e+01 0.02500000e+02 0.00000000e+00 0.00000000e+00 3
And the code that I am using looks like the following
read(iunit,'(ES20.8,ES20.8,ES20.8,ES20.8,ES20.8,I2)') dummy1, dummy2, Thermo_DB_Coeffs_LowT(iS,1:3),temp
The error I am getting is
Fortran runtime error: Bad value during floating point read
How can I read these values?
Well here is what I usually do when it is too painful to hand edit the file...
CHARACTER(LEN=256) :: Line
INTEGER, PARAMETER :: Start = 1
INTEGER :: Fin, Trailing_Int, I
DOUBLE, DIMENSION(6) :: Element
...
Ingest_All_Rows: DO WHILE(.TRUE.)
READ(...) Line ! Into a character-string
<if we get to the end of the file, then 'EXIT Ingest_All_Rows'>
Start =1
Single_Row_Ingest: DO I = 1, 6
Fin = SCAN(Line,'eE')+3 !or maybe it is 4?
IF(I ==6) Fin = LEN_TRIM(Line)
READ(Line(Start:Fin),*) Element(I) !fron the string(len-string) to the double.
Line = Line((Fin+1):)
IF(I ==6) Trailing_Int = Element(6)
ENDDO Single_Row_Ingest
<Here we shove the row's 5 elements into some array, and the trailing int somewhere>
ENDDO Ingest_All_Rows
You will have to fill in the blanks, but I find that SCAN and LEN_TRIM can be useful in these cases

Converting character string to integer

This is a follow up to my get_command_argument() question.
I'm reading a command line argument (arg) into a Fortran program. Then I want to store the value of arg as an integer. ichar() doesn't do the job.
This seems kind of basic, so clearly I'm doing something wrong. Any hints?
program test_get_command_argument
integer :: i,j
character(len=32) :: arg
i = 0
do
call get_command_argument(i,arg)
if (LEN_TRIM(arg) == 0) EXIT
write (*,*) trim(arg)
i = i + 1
end do
j = ichar(arg)
end program
You want to use the "internal files" capability.
You should have a statement like read(arg,*) j.
This will read the character variable arg as if it were a file
and store the result into j.
This isn't an answer but an extended comment:
That's a bizarre way to loop over the command line arguments. What's wrong with the straightforward and obvious
do i = 1, command_argument_count()
call get_command_argument(i,arg)
! do funky stuff
end do

How to read string like a file in Fortran

Suppose I have a string like this :
character(20) :: str="&
&1.2 &
&2. &
&3.32 &
&4.223 &
"
How can I read the string like a file?
For example, I tried
program main
implicit none
character(20) :: str="&
&1.2 &
&2. &
&3.32 &
&4.223 &
"
integer i
real a
do i=1,4
read(str,*) a
print*, a
end do
end program main
but I get only the first line every time
1.200000
1.200000
1.200000
1.200000
You have four reals in your string, but you read it into one real variable (four times). Since every time you start with the same string, you always get the same result.
Maybe you should read the string into an array directly (without a loop):
real a(4)
read(str,*) a(1:4)
(You can simple write read(str,*) a, the range is just given to indicate that a is an array here...)

Why doesn't this fortran code work?

Hey, I wrote this (fortran) with the aim of finding the minimum spanning tree of a bunch of points (syscount of them). I know for a fact that this approach works, since i wrote it in javascript earlier today. js is slow though, and i wanted to see how much faster fortran would be!!
only problem is it's not working, i'm getting an annoying error;
prims.f95:72.43:
if((check == 1) .and. (path(nodesin(j))(k) < minpath)) then
1
Error: Expected a right parenthesis in expression at (1)
What the hell is that about?! the 43rd character on the line is the "h" of "path"
nodesin(1) = 1
do i = 1,syscount-1
pathstart = -1
pathend = -1
minpath = 2000
do j = 1,i
do k = 1, syscount
check = 1
do l = 1, i
if(nodesin(l) == k) then
check = 0
end if
end do
if((check == 1) .and. (path(nodesin(j))(k) < minpath)) then
minpath = path(nodesin(j))(k)
pathstart = nodesin(j)
pathend = k
end if
end do
end do
nodesin(i+1) = pathend
minpaths(i)(1) = pathstart
minpaths(i)(2) = pathend
end do
Also, i'm fairly new to fortran, so i have a few other questions;
can i use && instead of .and. ?
is there a versions of the for(object in list){} loop found in many other languages?
is there a verion of the php function in_array ? i.e. bool in_array(needle,haystack), and if there is, is there a better way of doing it than:
check = false
Asize = size(array)
do i = 1, Asize
if(array(i) == needle) then
check = true
end if
end do
then to using the check variable to see if it's there?
(I haven't posted anything on stackoverflow before. please don't get angry if i've broken loads of etiquette things!)
It looks like you have defined path and minpaths as two-dimensional arrays. Multi-dimensional arrays are accessed differently in Fortran when compared to C-like languages. In Fortran you separate the indices by commas within one set of parentheses.
I'm guessing by the use of these variables they are integer arrays. Here is how you access elements of those arrays (since you didn't share your variable declarations I am making up the shape of these arrays):
integer :: path(n1, n2)
integer :: minpaths(n3, 2)
your if statement should be:
if((check == 1) .and. (path(nodesin(j), k) < minpath)) then
your access to minpaths should be:
minpaths(i, 1) = pathstart
minpaths(i, 2) = pathend
Also, if you are not using IMPLICIT NONE I recommend you consider it. Not using it is dangerous, and you are using variable names that are close to each other (minpath and minpaths). You could save hours of hair pulling debugging by using IMPLICIT NONE.
While .EQ. can be replaced with ==, there is still only .AND.
For your code block to check whether a "variable is there", you can use "where" and have much shorter code!
In Fortran >= 90 statements and functions can operate on arrays so that explicit loops don't have to be used as frequently.
There is no for (object in list), but using the where statement can do something very similar.
Many of the intrinsic functions that act on arrays also take masks as optional arguments to selectively operate.
I suggest reading a book to learn about these features. I like the one by Metcalf, Reid and Cohen. In the meantime, the second Wikipedia article may help: http://en.wikipedia.org/wiki/Fortran_95_language_features