FAQ Database Discussion Community


How to make a random function in fortran to generate the same random distribution into array?

random,fortran,fortran90
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) +...

Do sum with alternating sign in argument

fortran,fortran90
I'm doing some numerical exercises in Fortran 90. When I was trying to do a sum with alternating sign in argument I noticed that (in the manner that I did it) Fortran don't know how to do that. For example I want to do a sum over k from 1...

unformatted output with wild card in Fortran

fortran,wildcard,fortran90
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...

OpenMP over Summation

parallel-processing,fortran,openmp,fortran90,gfortran
I have been trying to apply OpenMP on a simple summation operation inside two nested loop, but it produced incorrect result so far. I have been looking around in here and here, also in here. All suggest to use reduction clause, but it does not work for my case by...

Simple read function error

fortran,fortran90
I imagine this is something silly I've missed but I've asked my whole class and noone can seem to work it out. Making a simple program calling in a subroutine and I'm having trouble with the do loop reading in the entries of the matrix. program Householder_Program use QR_Factorisation use...

Fortran multidimensional array in C++

c++,arrays,multidimensional-array,fortran,fortran90
I am trying to be able to pass a multidimensional Fortran array to a C++ program, in a C++ Fortran interoperating program. I have a basic idea of how passing the arrays from Fortran to C++ works; you pass a location of the array from Fortran to C++. Then C++...

LOC and %LOC not working in subroutines on Windows

windows,fortran,fortran90,intel-fortran
I have a Fortran 90 project which makes extensive use of the loc function to obtain the address of arrays (part of an API for interopability with Matlab). This code compiles and works on Mac and Linux with both Intel and gfortran, and has for some time (across a couple...

The name of a subroutine can be a variable in fortran? [duplicate]

fortran,fortran90,subroutine
This question already has an answer here: How to alias a function name in Fortran 2 answers I was wondering if there is something similar to this in FORTRAN. Of course this example does not compile but I think you can get the idea, program test character(1):: sub sub='A'...

Errors in very simple fortran program

fortran,fortran90,gfortran
I am a fortran beginner and writing a very simple program, compiler GNU fortran. program t1 implicit none real :: a, b a=35.4 CHARACTER (LEN=*) :: str = 'This is a test program' REAL, PARAMETER :: pi = 3.14 WRITE (*,*) a WRITE (*,*) str WRITE (*,*) pi end program...

Matrix from column

fortran,fortran90
The following data is given in a file: 1 7 5 4 11 0 1 -13 -7 I want to form a 3x3 matrix from these columns. I have tried advance=no but receive a runtime error. program form_matrix implicit none integer:: col,A(9) open(unit=12,file='q10.txt') do col=1,9 read(12,*,advance='no')(A(col)) end do 7 format(3i4)...

How to open and read a file

fortran,fortran90,fortran77,fortran95
I have this Fortran code that reads two columns of data from an external file: PRINT*, ' Q1 Q2 ' DO 2 J = 1, NPTS READ(20,*) Q1(J),Q2(J) WRITE(*,98) Q1(J),Q2(J) So the file 20 is used, but it doesn't seem to like any names I use in the file, so...

GFortran: Not passing arrays between f03 and f90?

arrays,fortran,fft,fortran90,gfortran
I am a novice in Fortran programming but I have started using code::blocks with Fortran plugin. I am trying to pass variables between my code (f03) and fftsg.f (code from http://www.kurims.kyoto-u.ac.jp/~ooura/fft.html) to calculate fft. fftsg.f contains a subroutine called rdft (real discrete Fourier transform) that I try to use to...

Fortran DO loop, warning to use integer only

fortran,fortran90,gfortran,fortran95
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...

Fortran: makefile error

makefile,fortran,fortran90
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...

how to stop a fortran program abnormally

fortran,fortran90
When an exception occurs I would like to terminate abnormally my program. Right now, when an exception happens a write statement with an explanatory sentence is called, and then a stop statement is called. I am debugging the program with idb (intel debugger), when the exception happens I get the...

Ambiguous reference to variable

gcc,fortran,fortran90,gfortran,intel-fortran
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...

Fortran Program Crashes when running

crash,fortran,fortran90,gfortran
I'm a starter in Fortran, using it to solve mathematical models in economics. I wrote a program (below) that is crashing when running. Windows show that message of error and nothing seems to happen. I haven't got a clue of what may be wrong, I already debugged it and it...

recursively take in input file for operation

fortran,fortran90,gfortran,fortran95
I have several input data files with the name angleFile1.dat, angleFile2.dat, angleFile3.dat and so on. (I have more than 100 files) Each file contain 45000 data of angles. I want to group these angles to get a distribution within 0 to 360 degrees. I have written a Fortran code to...

Assigning values of an array in a loop

arrays,loops,fortran,fortran90,gfortran
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...

C Wrapper Calling Fortran Functions

c,fortran,fortran90,gfortran,fortran-iso-c-binding
I'm trying to write a C wrapper to call a set of functions in a Fortran module. I'm starting with something basic but I'm missing something important. I've tried appending/prepending different numbers of underscores. I've also tried linking with gcc instead of gfortran. What I show below gives the simplest...

Understanding Fortran pointers

fortran,fortran90,fortran77,intel-fortran
In Fortran one can allocate memory to a pointer, or one cannot: real(kind=jp), target :: bt(100,100) real(kind=jp), pointer :: pt(:,:) But then you can allocate memory to the pointer pt: allocate(pt(100,100)) My question is: what are the pros and cons? For all I can see, allocating memory to the pointer...

How do I make all the calculations in double precision in fortran?

fortran,fortran90,gfortran
In the Fortran code given below, I have made all numbers involving calculation of PI as double precision but the value of PI I get is just a real number with a large number of zero or 9 at the end. How do I make the program give PI in...

Proper use of the PURE keyword Fortran

fortran,keyword,fortran90,fortran95
I'm currently delving into Fortran and I've come across the pure keyword specifying functions/subroutines that have no side effects. I have a book, Fortran 90/95 by S Chapman which introduces the pure keyword but strangely provides no "good coding practice" uses. I'm wondering how liberally one should use this keyword...

Fortran 90 Character array error

arrays,fortran,fortran90,gfortran
I have to calculate commissions for different interval of sales on different days and form a table "Day", "Sales", "Commissions". So far I've done this: real::a(5),b(5) character(10)::c(5)=(/Sunday,Monday,Tuesday,Wednesday,Thursday/) open(1,file='in_1.txt') open(2,file='out_1.txt') read(1,*)(a(i),i=1,5) do i=1,5 if(0.<a(i).and.a(i)<99.)then b(i)=a(i)*.02 elseif(100.<a(i).and.a(i)<299.)then b(i)=a(i)*.05 elseif(300.<a(i).and.a(i)<499.)then b(i)=a(i)*.075 else b(i)=500.*.1+(a(i)-500.)*.125 endif enddo write(2,*)' Day ','...

IF statement in F90 errors

if-statement,fortran,fortran90,gfortran
I have the code: if i < n then x = topsep(1) y = topsep(2) realvor(n,1) = x + dx realvor(n,2) = x + dy imvor(n,1) = (realvor(n,1)*(a**2))/((realvor(n,1))**2+(realvor(n,2))**2) imvor(n,2) = (realvor(n,2)*(a**2))/((realvor(n,1))**2+(realvor(n,2))**2) tf = .TRUE. else x = botsep(1) y = botsep(2) realvor(n,1) = x + dx realvor(n,2) = y -...

Compling Fortran 77 subroutines with Fortran 90 code

fortran,fortran90,gfortran,fortran77
I'm having problems fitting LAPack subroutines, written in f77, into an f90 program. Gfortran is the compiler. I know that f77 is supposed to be a subset of f90, but for example * is not recognized by the compiler as the start of a comment. It recognizes much of the...

direct indexing on function return value in fortran

fortran,fortran90,fortran2003
Is there posibility to use indexing directly on a function's return value? Something like this: readStr()(2:5) where readStr() is a function which returns a character string. In many other languages it is quite possible, but what about Fortran? The syntax in my example of course does not compile. Is there...

Unclassifiable statement at (1) in Fortran

gcc,fortran,fortran90,gfortran
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...

Fortran derived type in common: initialization?

fortran,fortran90,static-initialization
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...

How do I write a makefile for multiple variants of the same library/executable?

makefile,target,fortran90
I am using Intel's ifort and GNU make to build my project on a linux machine. The lib depends on the source files a.f90, b.f90 and c.f90 in the source directory. I am invoking make via make FLAG where these FLAGS are defined in my makefile and correspond to different...

How to write a large amount of columns in a formatted file

formatting,fortran,fortran90
I'm trying to write a code in fortran90 that allows me to create lets say 20 columns (f10.6) with one space between them in to the "output file". When I try to create file just with the 4 columns it works, but when I try to format a file with...

Better way to mask a Fortran array?

arrays,fortran,fortran90,masking
I am wanting to mask a Fortran array. Here's the way I am currently doing it... where (my_array <=15.0) mask_array = 1 elsewhere mask_array = 0 end where So then I get my masked array with: masked = my_array * mask_array Is there a more concise way to do this?...

F2PY cannot see module-scope variables

python,numpy,fortran,fortran90,f2py
Sorry about being new to both Fortran 90 and f2py. I am using Windows 64 bit, Python 3.4 64 bit, gfortran. Numpy version is 1.9.1, and I commented the "raise NotImplementedError("Only MS compiler supported with gfortran on win64")" in the gnu.py, as instructed on this link: http://scientificcomputingco.blogspot.com.au/2013/02/f2py-on-64bit-windows-python27.html I have a...

Error in nested IF: Expected a right parenthesis in expression at (1)

fortran,fortran90,gfortran
I have a nested IF constructed like this: if(tt-t(ind)<=t(ind+1)-tt) then t(ind)=tt y(:,ind)=yy else if((y:,ind) == 0) then y(:,ind)=y(:,ind-1) ! If a certain sample point is skipped ! then assign previous sample value to it endif ind=ind+1 t(ind)=tt y(:,ind)=yy endif I am getting an error saying if((y:,ind) == 0) then 1...

Finding the intersect of two arrays in Fortran

arrays,fortran,fortran90,fortran95,set-intersection
I'm trying to generate the intersect of two 1-D arrays in Fortran. The intent is to use this intersection as a mask in maxloc so that I can pull the max few elements from one array into another (behavior analogous to repeated root removal in a max heap). Currently I...

Generate and sum prime numbers

algorithm,fortran,fortran90
I am attempting to sum up all the prime numbers below 2 million. I've gone over my code for hours but I cannot find what is causing it to print out the faulty number. logical function isprime(n) result(response) implicit none integer :: i integer, intent(in) :: n integer :: upto...

Write data to file in columns with commented names

fortran,fortran90
I am doing some exercises programming in Fortran90 and I have to write data to file in columns with each name of columns commented and I struggled while doing this because my data come from a do while like this do while (a<b) k = 2*a - b a =...

Error in Derived type declaration: Variable at (1) in this context must be constant

fortran,fortran90,derived-types
I have a derived type declared in a module like this: MODULE dmotifs TYPE :: PRM INTEGER, PRIVATE :: nsp=4,nrx=8,maxprx=4 REAL, PRIVATE :: cref=1e-6,tref=1 REAL, DIMENSION(nrx,maxprx) :: k REAL :: input END TYPE PRM CONTAINS SUBROUTINE unreg(y,param,r,s) TYPE(PRM), INTENT(IN) :: param REAL, DIMENSION(param%nsp), INTENT(IN) :: y INTEGER, DIMENSION(param%nsp,param%nrx), INTENT(OUT) ::...

How to orchestrate members in a cluster to read new input from a single file once the current job is done?

file-io,fortran,mpi,fortran90
I am working on a global optimization using brutal force. I am wondering if it is possible to complete the following task with Fortran MPI file I/O: I have three nodes, A, B, C. I want these nodes to search for the optima over six sets of parameter inputs, which...