Error message when trying to create a module in fortran 90 - fortran90

I am trying to create a module for a fortran 90 program. The file is called epath.f90. When I try to create the file epath.mod by running an object-only compile on the file by way of the commad f95 -c epath.f90 it gives me the following error message:
epath.f90:1:
MODULE euler-path
1
Error: Unclassifiable statement at (1)
epath.f90:8.3:
END MODULE euler-path
1
Error: Expecting END PROGRAM statement at (1)
Error: Unexpected end of file in 'epath.f90'
The code for epath.f90 is:
MODULE euler-path
INTEGER, PARAMETER :: NSTEPS=10
REAL, PARAMETER :: A=0.0, B=1.0, YSTART=0.0
REAL, DIMENSION(0:NSTEPS) :: x,y
END MODULE euler-path
I took the same steps for another module and it worked fine. Any help is appreciated.

In Fortran, names - module names, variable names, etc - have to start with a letter and contain only letters, digits, or underscores. (Fortran in particular forbids using special characters like operators, eg, -/+/*/(/) in names because it's historically taken a very cavalier approach to the use of spaces, or for that matter explicitly defined variable names, which would make it very difficult to distinguish between a-b as a name and the expression a - b.) See, eg, section 3.2.2 ("Names") of the recent Fortran standard.
So euler_path is ok, euler_path123 is ok, but euler-path isn't.

Related

How does the following quine work?

According to wikipedia :
A quine is a non-empty computer program which takes no input and produces a copy of its own source code as its only output
I saw this piece of perl code and am not able to figure out how it works.
Save the following line in file /tmp/p and run the file as perl /tmp/p:
Illegal division by zero at /tmp/p line 1.
The output of perl /tmp/p is:
Illegal division by zero at /tmp/p line 1.
How is the code working?
First, try to run it with warnings turned on:
$ perl -w p
Unquoted string "at" may clash with future reserved word at p line 1.
Unquoted string "tmp" may clash with future reserved word at p line 1.
Argument "tmp" isn't numeric in division (/) at p line 1.
Argument "at" isn't numeric in division (/) at p line 1.
The first two warnings are from the compile phase.
Let's look at the Deparse output:
$ perl -MO=Deparse p
'division'->Illegal('zero'->by('at' / 'tmp' / 'line'->p(1)));
p syntax OK
In essence, the value of at divided by tmp divided by the return value of another method invocationp is passed as an argument to the method by invoked on the class 'zero'. at and tmp are considered to be strings, and their numeric values are zero. Therefore, at/tmp results in the illegal division by zero error.
You will get the same error if you change the file's contents to
Stackoverflow hacker news one at /tmp/p line 1.
If you are wondering how Illegal division becomes 'division'->Illegal, see indirect object syntax, and avoid using it.
I would prefer to see you concentrating on improving the quality of your Perl code, rather than investigating obscure corners
But the answer is that the line is parsed as
'division'->Illegal('zero'->by('at' / 'tmp' / 'line'->p(1)));
and Perl uses zero for 'at' and 'tmp' because they are not valid numeric strings, so the first action is to evaluate 0 / 0 which throws the error

How to execute multi-line Maxima code from Octave / Matlab

I can execute Maxima code from Octave like this and it works:
mm=maxima("diff(a*x^3-b*x^2+x+d,x,1)")
but how can I execute multi line commands?
Example code below that works in Maxima
kill(all)$
numer:true$
ratprint:false$
angle_in_bits:3779$
total_fs:18136$
s:solve(angle_deg=(angle_in_bits/total_fs*360),angle_deg)$
round(s);
[round(angle_deg)=75]
When I try the code below in Octave I get syntax errors
mm=maxima("kill(all)$
numer:true$
ratprint:false$
angle_in_bits:3779$
total_fs:18136$
s:solve(angle_deg=(angle_in_bits/total_fs*360),angle_deg)$
round(s);")
Errors that I get:
>>> mm=maxima("kill(all)$
numer:true$
ratprint:false$
angle_in_bits:3779$
total_fs:18136$
s:solve(angle_deg=(angle_in_bits/total_fs*360),angle_deg)$
round(s);")
error: unterminated character string constant
parse error:
syntax error
>>> mm=maxima("kill(all)$
^
>>> _ide_reload_variables_list( whos() );
error: 'numer' undefined near line 1 column 1
error: invalid base value in colon expression
error: 'ratprint' undefined near line 1 column 1
error: invalid base value in colon expression
parse error:
syntax error
>>> angle_in_bits:3779$
^
parse error:
syntax error
>>> total_fs:18136$
^
parse error:
syntax error
>>> s:solve(angle_deg=(angle_in_bits/total_fs*360),angle_deg)$
^
error: unterminated character string constant
parse error:
syntax error
>>> round(s);")
^
Thanks to Fred Senese and rayryeng for the assist.
I know someone may need this so here's some example code. This bit of code allows you to directly access maxima's symbolic solver from octave (allows you to execute multiple lines of maxima's commands). Since octave doesn't have a good symbolic solver yet this will come in handy for another person down the line.
mm=maxima("(kill(all), numer:true, ratprint:false, angle_in_bits:3779, total_fs:18136, s:solve(angle_deg=(angle_in_bits/total_fs*360),angle_deg),(s))")
%mm = '[angle_deg = 75.01323334803705]';
[si ei xt mt] = regexp(mm, '(\d)*(\.)?(\d)*');
number = str2num(mt{1})
>>>number = 75.013
I will suppose here that you are using QtOctave which I am guessing from googling your error message "_ide_reload_variables_list( whos() );"
If this is not so, none of the following may apply to your question.
typing help maxima at the prompt points me to a file /usr/share/qtoctave/scripts_octave/maxima.m
with this contents:
function result=maxima(command)
in="";
in=sprintf("echo \"string(%s);\"|maxima --very-quiet", command);
[status,result]=system(in);
%if(status!=0) result=""; endif;
result = deblank ( strjust ( strrep (result, "%", "") ,"left") );
endfunction
Which tells me that maxima is called via octave's function system in a very special way that is not allowing for multiple commands in maxima.
modifying the assignment of in in the way below would allow you to call the function maxima now with a cell array of commands maxima({command_1,command_2}) where command_i are strings.
in=['echo ', sprintf('\"%s;\" ',command{:}), '| maxima --very-quiet'];
Please note that the function system still returns only one output, the one that is sent to standard out by maxima.
This may also be of interest for you as it describes methods of octave's interaction with subprocesses.
I am not sure if this helping much as I think the modification provided by me is only of very superficial use, but maybe it helps you to understand better what octave is doing if you tell it maxima(something). It helped me.
Last but not least as far as I know there is no real interface between octave (or matlab) and maxima. I hope someone will correct me if I am wrong about that.
I have Octave and Maxima in my Linux laptop (Ubuntu). There exist system -function in Octave, which could be used to run terminal -commands.
In terminal it is possible to call maxima functions by using pipe
(add quit(); to the end of maxima command) :
$ echo "factor(12345); quit();" | maxima
Maxima 5.41.0 http://maxima.sourceforge.net
using Lisp GNU Common Lisp (GCL) GCL 2.6.12
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) (%o1) 3 5 823
$
In Octave' system -commad use double "" inside "-marks to get " :
[status,output]=system("echo ""factor(565);quit();""|maxima")
status = 0
output =
Maxima 5.41.0 http://maxima.sourceforge.net
using Lisp GNU Common Lisp (GCL) GCL 2.6.12
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) (%o1) 5 113
Extra txt could be cutted out from the output -string in Octave. Use Maxima's properties to run it's commands from a script file, and the script could be created in Octave.
Br. Juha (juhap.karjalainen#mail.suomi.net)

Read integer statement

I am quite new with fortran and have a question. I need to read 2 integers from the following line:
K=234, L=241, I=0
I am not interested in the last value. Just need the integers 234 and 241. I tried it with
read(20,'(3X,I3,3X,I3)')a,b
It compiles, but when I run the program I always get the error message:
At line 27 of file test.f90 (unit = 20, file = 'int_p2.dat')
Fortran runtime error: Bad value during integer read
Don't know what I am doing wrong. Can someone give me some advice?
You have strings in your line, so your READ statement ought to account for it. You should replace it with
READ(20, '(3(a2,i3,2x))') dumChar, k, dumChar, l, dumInt, dumChar
where dumChar is a character of length 2 and dumInt is an integer.
I don't see a problem in your code. (off course, your format is wrong, but should not give runtime error). Also, you are escaping chars (K,L) rather then reading them. compiler should not complain. but 3X will eat up 1 integer for K=234
Program se
Implicit None
integer :: K,L,I,a,b
open(20, file="se.in",status='old')
read(20,'(3X,I3,3X,I3)')a,b
close(20)
write(*,*)a,b
End Program se
$ cat se.in
K=234, L=241, I=0
$gfortran se.f90
$ ./a.out
34 241
If you still getting the problem, and if this are one single line you are trying to read, do
remove any space before K= in the file.
I think this is the error, as the code is reading non-integer.

How to use command line arguments in Fortran?

GCC version 4.6
The Problem: To find a way to feed in parameters to the executable, say a.out, from the command line - more specifically feed in an array of double precision numbers.
Attempt: Using the READ(*,*) command, which is older in the standard:
Program test.f -
PROGRAM MAIN
REAL(8) :: A,B
READ(*,*) A,B
PRINT*, A+B, COMMAND_ARGUMENT_COUNT()
END PROGRAM MAIN
The execution -
$ gfortran test.f
$ ./a.out 3.D0 1.D0
This did not work. On a bit of soul-searching, found that
$./a.out
3.d0,1.d0
4.0000000000000000 0
does work, but the second line is an input prompt, and the objective of getting this done in one-line is not achieved. Also the COMMAND_ARGUMENT_COUNT() shows that the numbers fed into the input prompt don't really count as 'command line arguments', unlike PERL.
If you want to get the arguments fed to your program on the command line, use the (since Fortran 2003) standard intrinsic subroutine GET_COMMAND_ARGUMENT. Something like this might work
PROGRAM MAIN
REAL(8) :: A,B
integer :: num_args, ix
character(len=12), dimension(:), allocatable :: args
num_args = command_argument_count()
allocate(args(num_args)) ! I've omitted checking the return status of the allocation
do ix = 1, num_args
call get_command_argument(ix,args(ix))
! now parse the argument as you wish
end do
PRINT*, A+B, COMMAND_ARGUMENT_COUNT()
END PROGRAM MAIN
Note:
The second argument to the subroutine get_command_argument is a character variable which you'll have to parse to turn into a real (or whatever). Note also that I've allowed only 12 characters in each element of the args array, you may want to fiddle around with that.
As you've already figured out read isn't used for reading command line arguments in Fortran programs.
Since you want to read an array of real numbers, you might be better off using the approach you've already figured out, that is reading them from the terminal after the program has started, it's up to you.
The easiest way is to use a library. There is FLAP or f90getopt available. Both are open source and licensed under free licenses.
The latter is written by Mark Gates and me, just one module and can be learned in minutes but contains all what is needed to parse GNU- and POSIX-like command-line options. The first is more sophisticated and can be used even in closed-source projects. Check them out.
Furthermore libraries at https://fortranwiki.org/fortran/show/Command-line+arguments
What READ (*,*) does is that it reads from the standard input. For example, the characters entered using the keyboard.
As the question shows COMMAND_ARGUMENT_COUNT() can be used to get the number of the command line arguments.
The accepted answer by High Performance Mark show how to retrieve the individual command line arguments separated by blanks as individual character strings using GET_COMMAND_ARGUMENT(). One can also get the whole command line using GET_COMMAND(). One then has to somehow parse that character-based information into the data in your program.
I very simple cases you just need the program requires, for example, two numbers, so you read one number from arg 1 and another form arg 2. That is simple. Or you can read a triplet of numbers from a single argument if they are comma-separated like 1,2,3 using a simple read(arg,*) nums(1:3).
For general complicated command line parsing one uses libraries such as those mentioned in the answer by Hani. You have set them up so that the library knows the expected syntax of the command line arguments and the data it should fill with the values.
There is a middle ground, that is still relatively simple, but one already have multiple arguments, that correspond to Fortran variables in the program, that may or may not be present. In that case one can use the namelist for the syntax and for the parsing.
Here is an example, the man point is the namelist /cmd/ name, point, flag:
implicit none
real :: point(3)
logical :: flag
character(256) :: name
character(1024) :: command_line
call read_command_line
call parse_command_line
print *, point
print *, "'",trim(name),"'"
print *, flag
contains
subroutine read_command_line
integer :: exenamelength
integer :: io, io2
command_line = ""
call get_command(command = command_line,status = io)
if (io==0) then
call get_command_argument(0,length = exenamelength,status = io2)
if (io2==0) then
command_line = "&cmd "//adjustl(trim(command_line(exenamelength+1:)))//" /"
else
command_line = "&cmd "//adjustl(trim(command_line))//" /"
end if
else
write(*,*) io,"Error getting command line."
end if
end subroutine
subroutine parse_command_line
character(256) :: msg
namelist /cmd/ name, point, flag
integer :: io
if (len_trim(command_line)>0) then
msg = ''
read(command_line,nml = cmd,iostat = io,iomsg = msg)
if (io/=0) then
error stop "Error parsing the command line or cmd.conf " // msg
end if
end if
end subroutine
end
Usage in bash:
> ./command flag=T name=\"data.txt\" point=1.0,2.0,3.0
1.00000000 2.00000000 3.00000000
'data.txt'
T
or
> ./command flag=T name='"data.txt"' point=1.0,2.0,3.0
1.00000000 2.00000000 3.00000000
'data.txt'
T
Escaping the quotes for the string is unfortunately necessary, because bash eats the first quotes.

perl error in svn_load_dirs.pl

I want to manage vendor branches in svn using the svn_load_dirs.pl script.
When I execute this script on my Mac OS X 10.5, perl v5.10.1 (*) built for darwin-multi-2level
I get he following error message:
Argument "2.07_02" isn't numeric in subroutine entry at /usr/local/bin/svn_load_dirs line 22.
What does this mean and is this harmful?
To understand Perl messages, you can look them up in perldoc perldiag:
Argument "%s" isn't numeric%s
(W numeric) The indicated string was
fed as an argument to an operator that
expected a numeric value instead.
Since this message is classified as a Warning, it may not be harmful. But, it is worth investigating. Since I do not have access to the svn_load_dirs.pl script, could you please update your Question with the 1st 22 lines of the script?