Need to read a file and assign its content to a function - fortran

I am a novice to Fortran 90 and stack overflow, and I have a simple question (I think).
I have a file, named Eq1, containing the following Fortran mathematical expression:
x**2
The objective is to input this expression from the file, and use it in a function inside fortran. I have tried the following
REAL FUNCTION f(x)
IMPLICIT NONE
REAL (kind =8), INTENT(IN) :: x
open(1,file = 'Eq1',status='old')
read(1,*) f
close(1)
END FUNCTION f
and when I do
print *, f(0.1d+0)
inside the main program, I get the following error message:
Fortran runtime error: Bad real number in item 1 of list input
Thanks for the help!
Cheers

Fortran does not have a string evaluation functionality as some script languages, e.g. Python's eval().
You have to write your own string to expression parser, which is not a trivial task.
One simple workaround is to call Python's eval() with SYSTEM and use it's output:
REAL FUNCTION f(x)
IMPLICIT NONE
REAL (kind =8), INTENT(IN) :: x
CHARACTER(LEN=200) EXP
CHARACTER(LEN=400) CMD
open(1,file = 'Eq1',status='old')
read(1,*) exp
close(1)
WRITE(CMD, "(A,f14.7, A, A, A)") 'python -c "x=',x,';print ',trim(exp), '" > out.txt'
CALL system(CMD)
open(2,file = 'out.txt',status='old')
read(2,*) f
close(2)
END FUNCTION f
program calc
implicit none
real f
print *, f(0.1d+0)
print *, f(2.0d+0)
print *, f(3.0d+0)
print *, f(4.0d+0)
end program calc
Output:
9.9999998E-03
4.000000
9.000000
16.00000

Related

When passing function name as argument, the function does not take array as input [duplicate]

This question already has answers here:
How to pass array to a procedure which is passed as an argument to another procedure using Fortran
(2 answers)
Closed 1 year ago.
I'm writing a subroutine that can take function names are argument.
In my example, it is call call_test(ga), where ga is a function ga(x).
My old practice works fine if x is a scalar.
The problem is the program failed if x is an array.
The minimal sample that can reproduce the problem is the code below:
module fun
implicit none
private
public :: ga, call_test
contains
subroutine call_test(fn)
double precision,external::fn
double precision::f
double precision,dimension(0:2)::x0
x0 = 0.111d0
print*,'Input x0=',x0
print*,'sze x0:',size(x0)
f = fn(x0)
print*,'fn(x0)=',f
end subroutine call_test
function ga(x) result(f)
double precision,dimension(0:) :: x
double precision::f
print*,'size x inside ga:',size(x)
print*,'x in ga is:=',x
f = sum(x)
end function ga
end module
program main
use fun
call call_test(ga)
end program main
Using latest ifort, my execution result is:
Input x0= 0.111000000000000 0.111000000000000 0.111000000000000
sze x0: 3
size x inside ga: 140732712329264
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image PC Routine Line Source
a.out 000000010C6EC584 Unknown Unknown Unknown
libsystem_platfor 00007FFF20610D7D Unknown Unknown Unknown
a.out 000000010C6C62B2 _MAIN__ 32 main.f90
a.out 000000010C6C5FEE Unknown Unknown Unknown
Using gfortran the result is
Input x0= 0.11100000000000000 0.11100000000000000 0.11100000000000000
sze x0: 3
size x inside ga: 0
x in ga is:=
fn(x0)= 0.0000000000000000
My question is why is this, and how to make it work. A functional code solution based on my code is highly appreciated.
#IanBush is right in his comment. You need an explicit interface as the function argument takes an assumed-shape dummy argument. Since you have some other deprecated features in your code, here is a modern reimplementation of it in the hope of improving Fortran community coding practice,
module fun
use iso_fortran_env, only: RK => real64
implicit none
private
public :: ga, call_test
abstract interface
function fn_proc(x) result(f)
import RK
real(RK), intent(in) :: x(0:)
real(RK) :: f
end function fn_proc
end interface
contains
subroutine call_test(fn)
implicit none
procedure(fn_proc) :: fn
real(RK) :: f
real(RK) :: x0(0:2)
x0 = 0.111_RK
write(*,"(*(g0,:,' '))") 'Input x0 =',x0
write(*,"(*(g0,:,' '))") 'sze x0:',size(x0)
f = fn(x0)
write(*,"(*(g0,:,' '))") 'fn(x0) =', f
end subroutine call_test
function ga(x) result(f)
real(RK), intent(in) :: x(0:)
real(RK) :: f
write(*,"(*(g0,:,' '))") 'size x inside ga:',size(x)
write(*,"(*(g0,:,' '))") 'x in ga is:',x
f = sum(x)
end function ga
end module
program main
use fun
call call_test(ga)
end program main
Note the use of the abstract interface. The import statement merely imports the symbol RK for usage within the abstract. Without it, you will have to use iso_fortran_env to declare RK as real64. Do not use double precision, it is deprecated and does not necessarily mean 64bit real number. Also, note the conversion of 0.111d0 to 0.111_RK. Also, I strongly recommend not using single-letter variable names. They are ugly, error-prone, and non-informative. Modern Fortran allows variable names up to 63 characters long. There is no harm in having long but descriptive variable names. Here is the code output,
$main
Input x0 = 0.11100000000000000 0.11100000000000000 0.11100000000000000
sze x0: 3
size x inside ga: 3
x in ga is: 0.11100000000000000 0.11100000000000000 0.11100000000000000
fn(x0) = 0.33300000000000002

Invalid character in name `1t`

Here is my code:
!lab 4(a) solution by James Ney
program lab4_a
implicit none
integer :: n
real :: L,R
interface
function testFun (x)
real :: testFun
real, intent (in) :: x
end function testFun
end interface
print *, "lab 4(a) solution by James Ney"
print *, "Enter left and right ends of interval and number of subintervals"
read *, L,R,n
call MeshCalcs(testFun,L,R,n)
contains
subroutine MeshCalcs(F,a,b,n)
implicit none
integer, intent(in) :: n
real, intent(in) :: a,b
real :: del,fVal,xVal
integer :: 1t=0,gr=0,i
real ::F,sum=0,average
del=(b-a)/real(n)
do i=0,n
xVal=a+(i*del)
fVal=F(xVal)
sum=sum+fVal
end do
Average=sum/(n+1.0)
print "('Average is: ',f10.2)",average
do i=0,n
xVal=a+(i*del)
fVal=F(xVal)
if (fVal>average) then
gr=gr+1
else if(fVal<average) then
1t=1t+1
end if
end do
print "('number of function values greater than average =',i4)",gr
print "('number of function values less than average =',i4)",1t
end subroutine MeshCalcs
end Program Lab4_a
real function testFun(x)
real, intent (in) :: x
testFun=-(x-4.0)**2+9.0
end function testFun
and the errors I get when I try to compile with gfortran are:
lab4_2a.f90:27.20:
integer :: 1t=0,gr=0,i
1
Error: Invalid character in name at (1)
lab4_2a.f90:43.5:
1t=1t+1
1
Error: Non-numeric character in statement label at (1)
lab4_2a.f90:43.6:
1t=1t+1
1
Error: Invalid character in name at (1)
lab4_2a.f90:47.62:
print "('number of function values less than average =',i4)",1t
1
Error: Syntax error in PRINT statement at (1)
lab4_2a.f90:41.5:
gr=gr+1
1
Error: Symbol 'gr' at (1) has no IMPLICIT type
lab4_2a.f90:30.12:
do i=0,n
1
Error: Symbol 'i' at (1) has no IMPLICIT type
The first error message is quite clear (well, clear to those who already know this stuff). In this line
integer :: 1t=0,gr=0,i
the first variable declared has a name beginning with the digit 1. Fortran's rules require that all names begin with a letter or an underscore. I believe that this is common in other programming languages too. So the compiler barfs on 1t and the rest of the errors shown are probably direct consequences. Rename that variable and see what happens.

Check values of local variables in subroutines from outside

How do you check values in Fortran like in Matlab? For example in the little program under, why does it show c=0 in main when it is c=36 in the subroutine testing? How do you make it so c=36 in the main program?
Can you call on the value c in some sort of way? I understand that in the main program the variable c is either undefined or has value 0, but is there a way to save the value of c in the subroutine so you can use it again in other subroutines, without calculating it again?
When the program is quite large it is handy to check values as you go.
program main
use test
implicit none
integer :: a,b,c
call testing(a,b)
write(*,*)'Test of c in main program',c
end program main
module test
implicit none
contains
subroutine testing(a,b)
integer :: a,b,c
a=2
b=3
c=(a*b)**a
write(*,*)'Value of c in subroutine',c
end subroutine testing
end module test
It is important to learn about scope in programming languages.
Every name (identifier) has only a limited range of validity.
If you declare some variable inside a subroutine
subroutine s
integer :: i
end subroutine
than the i is valid only in that subroutine.
If you declare a variable in a module
module m
integer :: i
contains
subroutines and functions
end module
then the i is valid inside all those subroutines and functions and also in all program units that use that module.
However, that does not mean that you should declare the variable in the module and just share it. That would be more or less a global variable. This is reserved only for certain cases, where that is necessary, but not for getting results out of your subprograms.
If your subroutine just computes something and you want to get the result of that computation you have two possibilities:
1. pass it as an additional argument, which will be defined by the subroutine
subroutine testing(a, b, c)
integer, intent(in) :: a, b
integer, intent(out) :: c
c = (a*b) ** a
end subroutine
you then call it as
call testing(2, 3, c)
print *, "result is:" c
2. Make it a function
function testing(a, b) result(c)
integer, intent(in) :: a, b
integer :: c
c = (a*b) ** a
end function
and then you can use directly
c = testing(2, 3)
print *, "result is:", c
or just
print *, "result is:", testing(2, 3)
This is the desired behaviour. Basically the calling routine should know nothing about the subroutine except for how to call it and what to get back. This is called encapsulation.
You can make variables accessible to the calling routine by declaring it in the module itself, and not in the subroutine:
module test
implicit none
integer :: c
contains
subroutine testing(a, b)
implicit none
integer :: a, b
a = 2
b = 3
c = (a*b) ** a
write(*, *) "Value of c in the subroutine: ", c
end subroutine testing
end module test
program main
use test
implicit none
integer :: a, b
call testing(a, b)
write(*, *) "Test of c in the main program: ", c
end program main
Note that you must not declare c in the main program, as it will get the variable from the module.
You could also use a COMMON block, but modules are far superior.

Fortran compiling error Two main programs

I've been trying to write this program for fortran where the pressure of a gas is calculated using the Van der Waals equation and everytime I've scrapped the entire code and started over because everything I do comes back with an error, even when I try using answers from other questions. I'm new to fortran and I know my syntax is probably all over, if someone could just please tell me where I'm going wrong it would be greatly appreciated.
!Named program EquationOfState
program EquationOfState
implicit none
real :: pressure
write(*,*) 'pressure of Ideal Gas = '
write(*,*) 'pressure calculated with Van der Waals equation = '
end program EquationOfState
!Prompt user to input values for density == d, and temperature == t
write(*,*) "Enter density of gas"
read(*,*) d
write(*,*) "Enter temperature conditions"
read(*,*) t
write(*,*) "The pressure of the gas is", pressure
function pressure(d, t)
real(kind=4) :: pressure_IdealGas
real, intent(in) :: d, t
real, parameter :: R = 286.9_4
pressure = d * R * t
end function pressure
with this error
program EquationOfState
1
HWL_4.f90:14.36:
write(*,*) "Enter density of gas"
2
Error: Two main PROGRAMs at (1) and (2)
Your lines:
!Prompt user to input values for density == d, and temperature == t
write(*,*) "Enter density of gas"
read(*,*) d
write(*,*) "Enter temperature conditions"
read(*,*) t
write(*,*) "The pressure of the gas is", pressure
are outside of any program or function. You should at least move the end program statement behind these lines.
After you will do that you will realize there is no way to call your function from the main program as the variable pressure shadows the external function pressure(). You will have to rename one of them.
Also, your function is ill-formed, it is named pressure, but you declare something called pressure_IdealGas inside.
So you probably wanted:
function pressure_IdealGas(d, t)
implicit none
real :: pressure_IdealGas
real, intent(in) :: d, t
real, parameter :: R = 286.9_4
pressure_IdealGas = d * R * t
end function pressure_IdealGas
and in the main program you wanted to call this function:
pressure = pressure_IdealGas(d, t)
write(*,*) "The pressure of the gas is", pressure
Notice the implicit none in the function. It is important to use it there, because the function is external and the implicit none in the program does not affect it.
The previous should make it work, but it is better not to use external functions and external subroutines. In very simple program, you can make the function internal by placing it between the keyword contains and end program but in serious programs you want to place it into a module:
module gas_functions
implicit none
contains
function pressure_IdealGas(d, t)
real :: pressure_IdealGas
real, intent(in) :: d, t
real, parameter :: R = 286.9_4
pressure_IdealGas = d * R * t
end function pressure_IdealGas
end module
program EquationOfState
use gas functions
implicit none
...
Note that one implicit none per program is enough.
Read Correct use of modules, subroutines and functions in fortran for more.
Finally, there is no reason for the real(kind=4) in the function declaration. For compatibility, if you use just real everywhere, stick with it. The hardcoded number 4 is not a good idea for other reasons, see Fortran 90 kind parameter

I cannot work out the error in the script

I am very new to programming and I cannot debug my program. Whenever I run it it gives the same error:
return type mismatch of function f at (1)
My code is:
real function F(x)
implicit none
real:: x
F=exp(-x)-x
end function
program easycod
implicit none
real::xl,xu,xr,fu,test,xrold,fl,fr
integer ::i
do i=1,50
xr=xrold
xr=(xl+xu)/2.0
fr =F(xr)
fl =F(xl)
test=fl*fr
IF (test>0.0) then
xl=xr
fr=fl
else if (test<0.0) then
xu=xr
end if
if (test==0.0) exit
print*,xr
end do
end program
There are a few things wrong with your code.
First, you are getting a compiler error because you haven't declared your function F in your program:
program easycode
implicit none
real :: xl, xu, xr, fu, test, xrold, fl, fr
real :: F ! <----------------- Add this line
integer :: i
Then you are assigning xr twice, which makes the first one unnecessary. Finally, xold, xl and xr are not initialized and can therefore be given any value the compiler would like.