Guest User

Untitled

a guest
Jun 8th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ! Nicholas Mai
  2. ! PHYS 401 - Computational Physics
  3. ! Homework 1 - Population Growth
  4.  
  5. ! Linear ODE Version
  6. ! This program numerically computes the solution to a first-order, linear
  7. ! ordinary differential equation. The method used is the basic Euler explicit
  8. ! scheme.
  9.  
  10. PROGRAM lin_popgrowth
  11.     IMPLICIT NONE
  12.  
  13.     ! Declaration of arrays
  14.     REAL, DIMENSION(:), ALLOCATABLE:: N,NA,R,RA,t
  15.     REAL:: gamma_,dt,N0
  16.     INTEGER::t_f, num_it, i
  17.     CHARACTER (LEN=20):: n_out
  18.     CHARACTER (LEN=20):: na_out
  19.     CHARACTER (LEN=20):: r_out
  20.     CHARACTER (LEN=20):: ra_out
  21.    
  22.     ! Ask for input for variables
  23.     WRITE(*,*) 'Enter the final time (t_f): '
  24.         READ(*,*) t_f
  25.     WRITE(*,*) 'Enter the time step (dt): '
  26.         READ(*,*) dt
  27.     WRITE(*,*) 'Enter the gamma factor (gamma): '
  28.         READ(*,*) gamma_
  29.     WRITE(*,*) 'Enter the computed N(t) file name (n_out): '
  30.         READ(*,*) n_out
  31.     WRITE(*,*) 'Enter the computed R(t) file name (r_out): '
  32.         READ(*,*) r_out
  33.     WRITE(*,*) 'Enter the analytical N(t) file name (na_out): '
  34.         READ(*,*) na_out
  35.     WRITE(*,*) 'Enter the analytical N(t) file name (ra_out): '
  36.         READ(*,*) ra_out
  37.  
  38.     ! Input / Output parameters
  39.     OPEN(7,FILE=n_out)
  40.     OPEN(8,FILE=r_out)
  41.     OPEN(9,FILE=na_out)
  42.     OPEN(10,FILE=ra_out)
  43.  
  44.     ! Number of iterates
  45.     num_it = INT(t_f/dt)
  46.     WRITE(*,*) 'Number of iterations: '
  47.     WRITE(*,*) num_it
  48.    
  49.     ! Allocate and initialize the main variables
  50.     ALLOCATE(N(0:(num_it-1)))
  51.     ALLOCATE(R(0:(num_it-1)))
  52.     ALLOCATE(t(0:(num_it-1)))
  53.     WRITE (*,*) 'Enter the initial value: '
  54.         READ(*,*) N0
  55.     N(0) = N0
  56.     R(0) = N0 * gamma_
  57.     t(0) = 0
  58.     NA(0) = N0
  59.     RA(0) = N0 * gamma_
  60.  
  61.     ! Calculate the solution via the explicit Euler's method
  62.     DO i = 0, num_it, 1
  63.         N(i+1) = N(i) +  N(i) * gamma_ * dt;
  64.         R(i+1) = N(i) * gamma_;
  65.         NA(i) = N0 * exp(gamma_ * t(i));
  66.         RA(i) = gamma_ * NA(i);
  67.         t(i+1) = t(i) + dt;
  68.         WRITE(7,*)t(i),N(i+1)
  69.         WRITE(8,*)t(i),R(i+1)
  70.         WRITE(9,*)t(i),NA(i)
  71.         WRITE(10,*)t(i),RA(i)
  72.     END DO
  73.  
  74.     ! Closing all input / output connections
  75.     CLOSE(7)
  76.     CLOSE(8)
  77.     CLOSE(9)
  78.     CLOSE(10)
  79. STOP
  80. END
Add Comment
Please, Sign In to add comment