Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Performing calculation with a variable

Status
Not open for further replies.

underthewood

Programmer
Nov 8, 2010
4
Hi, im new to fortran, and im using it for a project to perform some simple calculations. Ive already created a prgram that ALMOST does what I need it to, but im having a problem getting the final product.

Basically, I have a table of data, with about 2000 rows and 7 columns.
I have to perform the same calculation on every row, using 2 values from 2 of the columns, 2 already defined constants, and another number that I choose (variable).
Once this calculation is performed on every row, I must sum up all the results, ie I perform the calculation 2000 times then sum together the 2000 results.

Now I have figured out how to do this by choosing a single number for the variable. What I would like to do is do this entire process, but with different values of this variable between a certain range, at certain intervals.

Here is my code:

program clean
integer :: a, N, T
double precision :: g, c2, Z0, J, E, Z
open(10,file="file.txt")


!This is the file that has the 2000 rows of data, 7 columns

N=0
Z=0
Do
Read (10,*,iostat=a)v123,J,Ka,Kc,E,NT,Grade


! These are the headings assigned to each column

if(a==-1)exit
N=N+1

g=0.25
c2=1.4387752
T=1000

! Some definitions. I want to vary T.


Z0=g*((2*J)+1)*(exp((-E*c2)/T))

! This is the equation, where J and E are taken from each row, g and c2 are constants, and T is the value I would like to vary.

Z=Z+Z0
print *, Z

! The summing part of the program.

End Do
open(11,file="Z0s.txt")
write(11,*)T, Z
close(11)

! This bit i want as an output file, writing the final value of Z and the corresponding value of T. Of course, at the moment I only have one value for T so I only get one value for Z, and hence a single row with two columns

print *, 'The number of rows evaluated is:', N

print *, 'The value of Z is:', Z

! This eventually prints the final result for the value of T =1000, and vefifies the number of rows evaluated

End program

So to summarise: I want to read out from a large number of rows, take numeric values from certain columns in these rows, insert them into an equation (and do the equation for every row), then add together the results, but do this for various values of a certain variables within the equation.

How should I modify the code?

Cheers
Dan
 

You should use arrays to store the needed columns of the file "file.txt".

Example :

Code:
program clean

implicit none

integer :: n,i,k
double precision :: g, c2, Z0, Z, T, any
double precision,allocatable :: J(:),E(:)


open(10,file="file.txt")

! computing the size of the file

n=0
do
  read(10,*,end=100)
  n=n+1
enddo

100 continue

! allocating the needed arrays

allocate(J(n),E(n))

! reading the arrays

rewind(10)

do i=1,n
   read(10,*) any,J(i),any,any,E(i)
enddo

close(10)

! computing the function for several values of T

g=0.25
c2=1.4387752

open(11,file="Z0s.txt")

do i=1,20
   T=1000+i*5
   z=0
   do k=1,n
      z=z+g*(2*J(k)+1)*exp(-E(k)*c2/T)
   enddo
   write(11,*) t,z
enddo

end program

With the following "file.txt" :

Code:
0. 10. 0. 0. 2. 0. 0.
0. 20. 0. 0. 3. 0. 0.
0. 30. 0. 0. 1. 0. 0.
0. 40. 0. 0. 5. 0. 0.

the output file Z0s.txt is :

Code:
   1005.00000000000        50.7748114730551     
   1010.00000000000        50.7759230772770     
   1015.00000000000        50.7770237609448     
   1020.00000000000        50.7781136841991     
   1025.00000000000        50.7791930040647     
   1030.00000000000        50.7802618745258     
   1035.00000000000        50.7813204465997     
   1040.00000000000        50.7823688684071     
   1045.00000000000        50.7834072852422     
   1050.00000000000        50.7844358396391     
   1055.00000000000        50.7854546714373     
   1060.00000000000        50.7864639178450     
   1065.00000000000        50.7874637135007     
   1070.00000000000        50.7884541905331     
   1075.00000000000        50.7894354786195     
   1080.00000000000        50.7904077050420     
   1085.00000000000        50.7913709947429     
   1090.00000000000        50.7923254703778     
   1095.00000000000        50.7932712523684     
   1100.00000000000        50.7942084589522




François Jacq
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top