GFortran and CodeBlocks issue with Modules and Multiple Files - fortran

I am working with GFortran and CodeBlocks but I'm having an issue about Modules and Multiple files.
i keep getting this error:
Fatal Error: Can't open module file 'mesh.mod' for reading at (1): No such file or directory
For some reason, GFortran is not building the 'mesh.mod' file.
This problem does not occur when I put all the code in a single .f90 file.
Bellow is an example of code that this error happens.
main.f90
MODULE MESH
IMPLICIT NONE
INTEGER :: IMAX,JMAX,NMAX
REAL(8), ALLOCATABLE :: XD(:),YD(:),FX(:,:),FY(:,:)
REAL(8) :: PI,E,DX,DY,H,L,RHO,MU
PARAMETER (PI = ACOS(-1.D0))
PARAMETER (E = 2.718)
END MODULE MESH
!**************************************************************
program Cavity
Use Mesh
implicit none
Real(8), Allocatable :: func(:)
Real(8) :: Der,DfDx
integer :: i
IMAX=10
DX=1./10
Allocate(xd(IMAX),func(IMAX))
Do i=1,IMAX
xd(i)=i*DX
End Do
Do i=1,IMAX
func(i) = xd(i)**2
End Do
Der=Dfdx(func,2)
Write(*,*) Der
End program Cavity
Derivatives.f90
Real(8) Function DfDx(f,i)
Use Mesh
implicit none
Real(8) :: f(1:Imax)
integer :: i
DfDx=(f(i+1)-f(i-1))/(2d0*dx)
return
end function DfDx
When I use console command line compilation instead of CodeBlocks interface I already solved this problem (Compiling Multiple Files with modules) but I'm still getting this problem with CodeBlocks.
Does anyone know how to solve this issue?

Assuming what you have written is how your code is, then it appears that the problem is that the module mesh is inside the main program and not a separate file. You should have three files: Mesh.f90, Derivatives.f90 and Main.f90.
Mesh.f90 is exactly as you have it,
module Mesh
implicit none
integer :: IMAX,JMAX,NMAX
real(8), allocatable :: XD(:),YD(:),FX(:,:),FY(:,:)
real(8) :: PI,E,DX,DY,H,L,RHO,MU
parameter (PI = ACOS(-1.D0))
parameter (E = 2.718)
end module Mesh
Derivatives.f90 should be written as another module, using contains:
module Derivatives
use mesh
contains
real(8) function dfdx(f,i)
real(8) :: f(i:imax)
integer :: i
DfDx=(f(i+1)-f(i-1))/(2d0*dx)
end function dfdx
end module Derivatives
and the Main.f90 will then use both modules. Note that I had to eliminate the variable DfDx; this is because it conflicts with the function DfDx in module Derivatives
program Cavity
Use Mesh
use Derivatives
implicit none
Real(8), Allocatable :: func(:)
Real(8) :: Der
integer :: i
IMAX=10
DX=1./10
Allocate(xd(IMAX),func(IMAX))
Do i=1,IMAX
xd(i)=i*DX
End Do
Do i=1,IMAX
func(i) = xd(i)**2
End Do
Der=Dfdx(func,2)
Write(*,*) Der
End program Cavity
I do not know how CodeBlocks works, but I would presume it lets you choose the compilation order. If that is the case, you should compile Mesh.f90 first, then Derivatives.f90, then compile Main.f90 before linking them to an executable.
When I compiled & linked them, I got an answer of 0.200000002980232; hopefully that links up to what you have as well.

On codeblock, you may go to Project properties > Build targets
Then select the file you want to build first (say mod.f90).
In the "Selected file properties" go to "Build"
Here,change the priority weight. Lower weight implies the file will be built first.

The problem is that in CodeBlocks "projects are built in the order of appearence, from top to bottom" (CodeBlocks Wiki), in other words, the files are compiled alphabetically.
Which means that in my case, Derivatives.f90 was being compiled before than Main.f90 causing the error.
A way to circumvent the problem is to set only the Main.f90 file as build target in CodeBlocks:
Menu Project/Properties...
In Build Target Files at the tab Build targets check only Main.f90
And use the command Include 'File_Name.f90' inside the Main.f90 code to include the other f90 files for compilation in the right order.

Related

Why does the "stop" statement in Fortran prevent my program from outputting anything?

I have a very basic Fortran program to try to learn MPI. I'm compiling this script using Visual Studio 2019, MPICH2 and the Intel oneAPI Toolkit. The program is the following
program hello
implicit none
include 'mpif.h'
integer :: rank, size, ierror, tag, status(MPI_STATUS_SIZE)
call MPI_INIT(ierror)
call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierror)
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierror)
print *, "node", rank, "says Hello World!"
call MPI_BARRIER(MPI_COMM_WORLD, ierror)
call MPI_FINALIZE(ierror)
! here
end program hello
This program compiles and runs as expected, producing the output
PS C:\Users\gf715\Documents\VS_testing\test\Debug> mpiexec -n 4 test.exe
node 1 says Hello World
node 2 says Hello World
node 3 says Hello World
node 0 says Hello World
However, if I add a stop statement before ending the program (where I have written the "here" comment), the code will still compile, but now produces no output when I run it:
PS C:\Users\gf715\Documents\VS_testing\test\Debug> mpiexec -n 4 test.exe
PS C:\Users\gf715\Documents\VS_testing\test\Debug>
I don't remember having this problem before I used the Intel oneAPI toolkit (I used to use the same setup, but with Parallel Studio XE Cluster Edition, I was forced to change when my license expired).
Why does the stop prevent any output?
Answering my own question. This is due to having multiple versions of the Intel Compiler on a computer (see here)
I fixed this by uninstalling the old version and updating the new compilers, but there seem to be easier ways.

Executing UMAT subroutine in .f90 form through ABAQUS?

I am using 'Abaqus standard 6.14-4' with 'Microsoft Visual Studio Ultimate 2012' and 'Intel composer XE 2013 for windows'. I have written a UMAT subroutine in free form .f90 format. Now when I try to provide this UMAT subroutine in ABAQUS, it gives me an error message, 'User subroutine file name must have a .for or .obj extension'.
I have tried to change the following lines in 'abaqus_v6.env' file:
compile_fortran=['ifort', '/c','/DABQ_WIN86_64', '/extend-source', '/fpp', '/iface:cref', '/recursive', '/Qauto-scalar', '/QxSSE3', '/QaxAVX', '/heap-arrays:1', # '/Od', '/Ob0', # <-- Optimization Debugging # '/Zi', # <-- Debugging '/include:%I']
But, may be I had made some mistake or some thing, I don't know... my problem still persist. Then, I have tried to add the following line at the top of UMAT subroutine: !DIR$ FREEFORM and tried to feed this to ABAQUS. But, again failed as the same error message displayed again.
Please suggest me a solution so that I can feed my .f90 subroutine to ABAQUS job.

Can't compile with module and main program in same file

I am trying to make use of a module that is in the same file as my main program. However, I cannot get it to work. Does Fortran allow a module to be contained in the same file as the main program or must it be in a separate file? Here is a simple version of my code:
main program
use my_module
call my_subroutine()
end program main
module my_module
contains
subroutine my_subroutine()
print *, "Hello World!"
end subroutine my_subroutine
end module my_module
When I try to compile this file I get:
Fatal Error: Can't open module file 'my_module.mod' for reading at (1): No such file or directory
Yes, Fortran does allow modules to be contained in the same file as the main program. However, modules must be written before the main program:
module my_module
contains
subroutine my_subroutine()
print *, "Hello World!"
end subroutine my_subroutine
end module my_module
program main
use my_module
call my_subroutine()
end program main

Fortran running error

I have a Fortran code that I have to run but unfortunately I don't have any experience with Fortran. I tried to run the code using different Fortran version and nothing works.
Here is the link for the code: http://cpc.cs.qub.ac.uk/summaries/adpw.
It would be great if someone could tell me which Fortran version should I use.
Here are the details:
When I try to run with gfortran:
gfortran numcbas.f < numcbas_c.data
Segmentation fault: 11
and when I run with g77:
g77 numcbas.f < numcbas_c.data
ld: warning: -macosx_version_min not specified, assuming 10.10
ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not allowed in code signed PIE, but used in __start from /usr/lib/crt1.o.
To fix this warning, don't compile with -mdynamic-no-pic or link with -Wl,-no_pie
And here is start of the code:
program NUMCBAS
IMPLICIT DOUBLE PRECISION (A-H,O-Z)
MAIN DRIVING ROUTINE
CHARACTER*120 TITLE
DIMENSION IBUG(3),HRXS(10),IRXS(10)
COMMON /BASCON/ HRX(10),IRX(10),NIX,IRA
DATA TITLE /' '/
DATA HRXS/1.D-02,2.D-02,2.605D-02,7*0.D0/
DATA IRXS/30,120,500,7*0/,IBUG/3*0/
INTEGER :: NFTA=6,LUNUMB=13, LVAL=0
DOUBLE PRECISION :: BTOL=0.2D0, TINY=1.D-11
DOUBLE PRECISION :: ECMAX=10.D0, RLIM=10.D0, CHARGE=0.D0
NAMELIST /INPUT/ TITLE,LUNUMB,NIX,IRX,HRX,lval,IBUG,BTOL,
* TINY,ECMAX,RLIM,CHARGE
WRITE (6,1000)
and the input file:
&INPUT
TITLE='IONIC TARGET',
lval=0, ECMAX = 5.00D0,
RLIM = 12.0D0, CHARGE=1.0D0,/
It seems to me you are completely misunderstanding tho processes of compilation and running.
These lines are suspicious:
gfortran numcbas.f < numcbas_c.data
g77 numcbas.f < numcbas_c.data
There is no reason to redirect a data file to the compiler command. The compiler first has to create an executable program which you then can run with your data. Normally, a file ./a.out is created and you then run it
./a.out < some_data_to_stdin
It is very strange that you get a Segmentation fault from running gfortran without any other error message. Are you sure the commands you show above are exactly what you are running?

In Mac gfortran 4.7.2, isatty(6) does not work

In Mac 10.8.2, use gfortran 4.7.2 to compile the following code:
program test
write(*, *) isatty(6)
end program test
The program just not response and not return. Any idea?
I'm not really sure why you get no output, it works fine for me with gfortran 4.7.2 on Linux. You could try whether writing your own interface to C's isatty makes any difference:
program test
implicit none
interface
function my_isatty(fd) bind(C, name = 'isatty')
use, intrinsic :: iso_c_binding, only: c_int
integer(c_int) :: my_isatty
integer(c_int), value :: fd
end function
end interface
! Standard output should be at 1 in C:
print*, my_isatty(1)
end program test
I get the following output:
$ ./a.out
1
$ ./a.out > b && cat b
0