fortran,fortran90,multiple-files , How to open and read multiple files in Fortran 90
How to open and read multiple files in Fortran 90
I have some issues about opening and reading multiple files. I have to write a code which reads two columns in n files formatted in the same way (they are different only for the values...). Before this, I open another input file and an output file in which I will write my results. I read other questions in this forum (such as this one) and tried to do the same thing, but I receive these errors:
fileLoop : do i = 50,52
Error: Variable 'i' at (1) cannot be redefined inside loop beginning at (2)
Error: Invalid character in name at (1)
My files are numbered from 1 to n and are named 'lin*27-n.dat' (where n is the index starts from 1) and the code is:
character(len=15) :: filein,fileout,fileinp
integer :: row,i,h
real :: usv,usf,tsv,tsf,diff
write(*,'(2x,''Input file .......''/)')
open(unit = 30,File=filein)
open(unit = 20,File=fileout)
fileLoop : do i = 50,52
fileinp = 'lin*27-'
open(unit = i,File=fileinp)
do row = 1,24
diff = usf - usv
end program deviation
How can I solve it? I am not pro in Fortran, so please don't use difficult language, thanks.
The troublesome line is
You surely mean to do a
write (as in the example linked): this
read statement attempts to read from the variable
fileinp rather than writing to it.
That said, simply replacing with
write is probably not what you need either. This will ignore the previous line
fileinp = 'lin*27-'
merely setting to, in turn, "1", "2", "3" (with leading blanks). Something like (assuming you intend that
* to be there)
write(fileinp, '("lin*27-",I1)') i-49
Note also the use of
I1 in the format, rather than
I5: one may want to avoid blanks in the filename. [This is suitable when there is exactly one digit; look up
I0 when generalizing.]
Suppose that I have a C function with the following API : int c_function(int **a); How should I go about declaring a Fortran array/pointer to array, and passing it to the function, assuming that the C function is responsible for memory allocation ?...
In my parallel program, there was a big matrix. Each process computed and stored a part of it. Then the program wrote the matrix to a file by letting each process wrote its own part of the matrix in the correct order. The output file is in "unformatted" form. But...
I think my code below it's not exactly give me the same random distribution. subroutine trig_random_value() implicit none integer :: t, z, y, x real(real64) :: theta, r real(real64), parameter :: PI=4.D0*DATAN(1.D0) integer, dimension(12) :: date_time integer, dimension(12) :: seed call date_and_time(values=date_time) call random_seed seed = date_time(6) * date_time(7) +...
I'm using f2py to offload performance critical sections of my python code to fortran. I have two fortran compilers, gfortran and ifort. Since the code I run will be on intel processors, ifort is usually faster. But for the most critical part of my code, I observe the opposite, with...
I have created a derived type to access multi-dimensional arrays. With each array I associate a name in the array nm. My problem consists how to fill the array values once I have allocated the memory. An initial idea has been to use a multi-dimensional array as input. However I...
I installed gfortran on my Ubuntu 15.04. While compiling in Fortran, be it .f95 or .f90 type, the DO loop asks to take integer parameters only but not real values or variables. That includes the loop variable and the step expression. Why can't it take real values too? The following...
The various LDx arguments to BLAS ?gemm functions are there to make it possible to operate on slices of larger arrays. For example, this small C program does a matrix multiplication of the top left and top right (100,100) submatrices of a (200,200) matrix, and stores the result in the...
This question has been covered somewhat in previous SO questions. However, previous discussions seem somewhat incomplete. Fortran has several I/O statements. There is READ(*,*) and WRITE(*,*), etc. The first asterisk (*) is the standard asterisk designating an input or output from the keyboard to/from the screen. My question is about...
In my code: DO i=1,numJog,1 IF(val(i) .EQV. .TRUE.)THEN DO j=1,contVenc,1 result(j) = i END DO END IF END DO Where val is a logical array, and result is a integer array. For example, if val is: F T F T Then, i=2 and i=4. But the result array just write...
We are developing a scientific application which has the interface in python 2.7 and the computation routines written in Intel Visual Fortran. Reading the source files is done using python, then only the required data for computations has to be passed to standalone Fortran algorithms. Once the computations done, the...
My understanding of programming is very limited so I hope I am making sense. I made a change to a fixed variable in a program (the program is called NAFnoise; I was using the .exe but it came with the source code and I made the change there). The program...
Not sure if the title of my question makes sense, so bear with me. I'd like to find a system for representing single digit numbers with as few bits as possible. There is a method called "Densely packed decimal" (https://en.wikipedia.org/wiki/Densely_packed_decimal) which would be my ideal solution, but I wouldn't even...
I've searched for a long time before I ask: I need to output a lot of unformatted files in Fortran to Ensight. I want to name them with geo.000000, geo.000001 ... geo.0001000. Here is how I deal with wild card: character(54) :: filename, temp character(80) :: buffer write(temp,'(i6.6)') step filename...
I have written a derived type that stores a multidimensional array. The array will be read from a file to an array b. I then want to transfer b to the derived type Space using member a. The advantage for mvalloc is not having to allocate two large arrays which...
Given the minimal working example provided below, do you know why the memory allocation error does not occur at memory allocation step? As I checked, when I use valgrind to run the code, or add parameter source=0.0 to memory allocation statement, then I have, as expected, the memory allocation error....
In some legacy code, I found the following line size(k)=N What (if anything) does this do? As far as I know, it does not make sense to set size(thing) to a value in Fortran. Furthermore, k is implicitly defined as a scalar integer (i.e. there it is not declared in...
I am coding in Fortran 95. I must extract the two floating values from this line in an ascii file: circle(4097.0438,4111.337) Of course the READ statement is read(unit=11, fmt="(tr7,f9.4,tr1,f8.3)") x, y The problem is that I must do that for hundreds of ascii files, but there are variations in the...
I am pretty new to to fortran and I don't really know why am I getting this error. integrand(i)=inte(x(i),beta,r2,r1) 1 Error: Unclassifiable statement at (1) calka11.f95:97.6: I have made all the variables into a module file and then call them using use and when I am getting those variables into...
So I am doing 2 modules which are linking to the main program. The first one has all the variables defined in it and the second one is with the functions. Module1: module zmienne implicit none integer, parameter :: ngauss = 8 integer, parameter :: out_unit=1000 integer, parameter :: out_unit1=1001...
Is there any means to accessing (reading, writing to) files that are opened in some other source code by just passing the unit number?
I have written a simple program in Fortran90 to calculate the area of a triangle. The user enters the three sides of the triangle, and then the program outputs the area. Simple enough. MODULE Triangle_Operations IMPLICIT NONE CONTAINS FUNCTION Area(x,y,z) REAL :: Area ! function type REAL, INTENT( IN )...
[UPDATE] the code and a few sentences were changed to reflect a realization explained in my second comment. The code should compile with the line below, however, I have an older gfortran and may not be seeing some errors that you might. gfortran BLU_implementation_copy.f90 -o BLU_implementation_copy.x I'm getting an incredibly...
I would like to ask about the error occurred with my Fortran code. Since I'm new to fortran I can't handle this after 2 days, I also searched around but still don't know how to fix it. PROGRAM SUBDEM IMPLICIT REAL*8(A-H,O-Z) REAL*8 NTET,NPTK INTEGER*4 NA,NC,NE,NBAND,NTMAX,IDEF C CALL SETK08(NA,NC, A,C, PTK,NPTK,...
I am using a calculation to locate a base pixel. Seems to me that the two calculations for i and j are equivalent (i.e. i==j is always true) when I tried a few examples. Is this always true? i = (int ((x - xmin) / a)) + 1 j =...
I need to use gfortran to compile a library that is dependent on ieee_arithmetic. However, it is found that gfortran can not identify this module. For example with the code a.f90 program test use,intrinsic :: ieee_arithmetic real :: x read *, x if (ieee_is_nan(x)) then print *, "Nan" else print...
If I have a Fortran subroutine which takes a Fortran IO Unit as one of its parameters (for printing debug information to), and this function is compiled into a shared library, how do I correctly call this function from C? ! An example subroutine that I want to call from...
I need to create a new directory from my code to be able to write a data file to it. I am using Force Fortran 2.0 from Windows 8 and I am also wondering if this syntax is going to vary from one operating system to the other due to...
I am using a make.bat file to compile my Fortran code. To compile using the ifort command, I first need to initialize the compiler by executing ifortvarsbat intel64 command where I had to setup the path variable. This works fine, however, the path keeps increasing in size and says command...
I have a fortran main program called solidsolver.f90, and a module called read_mesh.f90. The module contains two subroutines and is used in the main program. I can compile them manually but not with a makefile. My makefile is named makefile.makefile, and it gives me an error: make: *** No targets...
I want to check all rows of an array for a logical condition. I used function ALL as described in GNU GCC guide https://gcc.gnu.org/onlinedocs/gfortran/ALL.html Here is a sample code: program test3 implicit none real, allocatable, dimension (:,:) :: mat1 integer :: i,j,k,r logical :: lg r=3 allocate(mat1(r,r)) mat1=transpose( reshape( (/-1,-2,-3,-4,-5,-6,-7,-8,-9/),...
This question already has an answer here: Fortran derived types: Overloaded assignment operator not working with 'PARAMETER' attribute 2 answers I'd really like to be able to define some parameters in a Fortran module, so so that they are accessible for the main program and all its subroutines. The...
I am a novice using the LAPACK routines, so I don't deeply know them, and I want to use them in parallelized loops (openmp). I use Ubuntu 14.04LTS and have LAPACK installed using my package manager. The version installed is: liblapack3 3.5.0-2ubuntu1 Library of linear algebra routines 3 - shared...
I have the question of the title, but If not, how could I get away with using only 4 bits to represent an integer? EDIT really my question is how. I am aware that there are 1 byte data structures in a language like c, but how could I use...
This question already has an answer here: How to use LDFLAGS in makefile 2 answers I wish to compile Fortran source code which uses functions from LAPACK and BLAS. When I compile a single source code file e.g. gfortran -g -framework accelerate test.f it works. However, I have many...
I am using VS2012 and Intel Visual Fortran 2015. According to https://software.intel.com/en-us/forums/topic/269585, it is now allowed to use allocatable and assumed-size arrays with namelist read and write; however, I am still getting the error "A namelist-group-object must not be an assumed-size array". example code: subroutine writeGrid(fname, grid) character*(*) :: fname...
It might be useful to have both C/C++ programmers and Fortran programmers compare and contrast pointers in these two languages. In trying to explain to C/C++ programmers how Fortran pointers differ, I usually say pointers to functions or subroutines do not exist. I then try to make the argument that...
I got troubles with this common: COMMON /REDCOM/ DPREC,NITMA,INDIC,NBERR,NCAR,KMOTLU, & REDVAR,MOCDER(2) COMMON /REDCO1/ CTEXT C type(double_st) :: DPREC INTEGER :: NITMA,INDIC,NBERR,NCAR,KMOTLU,REDVAR,MOCDER CHARACTER(72) :: CTEXT CHARACTER(4) :: CTEXT4 C EQUIVALENCE (CTEXT,CTEXT4) The double_st derived type is: type double_st sequence real(kind(0.d0)) :: x,y,z integer :: acc = -1 end type double_st Trying...
I'm using the g77 compiler (on terminal I just type g77 filename.for ) on a Mac. It can't read my fortran program and I can't modify the program (very old program, not mine, and I really just need the output from it). Any suggestion for another compiler to read the...
I use Visual Studio (2010 SP1) with Fortran IMSL (2011) and I can't get the right precision for my reals: program prova use, intrinsic :: iso_fortran_env implicit none integer, parameter :: ikind=selected_real_kind(p=8, r=99) real(kind=ikind) :: a=0.79 real(real64) :: b=0.79 real(kind=16) :: c=0.79 real(8) :: d=0.79 print *, a print *,...
I'm trying to get quad precision to work with my FORTRAN code. I have to replace some intrinsic functions by those in libquadmath, i.e. the dabs function by fabsq. Unfortunately, if I compile the following test function program test integer dp parameter (dp=10) real(kind=dp) a a= -5.0_dp a=fabsq(a) write(*,*) "abs(a)",...
I receive the runtime error "bad integer for item 11 in list input" referencing the following line of code. read(2,*)a,b,c,d,e,f,g,h,theta1,phi1,k,l,m,n,o, $ p,theta2,phi2,s,theta3,phi3, $ r1,x1,y1,r2,x2,y2,r3,x3,y3,z1,z2,z3 The line its reading from is 1 255.11211 0.2876 165.11404 90 4 8 0.19173 90 165.11404 0.09587 90 345.11404 4 4 0.0764 89.99915 -64.51149 0.11131 90.0015...
Scenario: I pass e.g. a 2D matrix as argument in a function and I would like to be able to read and change all the elements, BUT I would like to prevent any accidental writting in let's say the last column. So I would like somehow to tell the compiler...