Advertisement
Guest User

Untitled

a guest
Dec 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. !------------------------------------------------------------------------------
  2. !PHY2063 Assignment: Modified Euler Method Approximator for coupled ODEs
  3. !URN 6484960
  4. !October 9 2018       Numerical Physics Lab Group B (TUESDAY 1000-1300)
  5. !------------------------------------------------------------------------------
  6. PROGRAM assignment1 !https://www.desmos.com/calculator/4lrp1jh2b7
  7.     IMPLICIT NONE
  8.  
  9.     INTEGER, PARAMETER                       :: dp = KIND(1.d0)
  10.     ! Helps with setting variables as double-precision reals
  11.  
  12.     REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: x, y, vx, vy, t
  13.     ! There are 3 one-dimensional arrays: used to store values of x(t),
  14.     ! v(t) & t
  15.  
  16.     REAL(KIND=dp), PARAMETER                 :: h = 0.005
  17.     ! The distance between each consecutive value of t at which x(t) and
  18.     ! v(t) will be evaluated
  19.  
  20.     REAL(KIND=dp), PARAMETER                 :: finalt = 50.0
  21.     ! Tells the program the maximum t-value to reach
  22.  
  23.     REAL(KIND=dp) :: dxdt, dydt, dvxdt, dvydt, xmid, ymid, vxmid, vymid,&
  24.                      dxdtmid, dvxdtmid, dydtmid, dvydtmid
  25.     ! Declares re-usable variables for time, dx/dt and dv/dt, as well as
  26.     ! the midpoints between two consecutive x and v points, and their
  27.     ! time derivatives
  28.  
  29.     INTEGER                                  :: i
  30.     !A simple index to use in loops
  31.  
  32.     WRITE(6,*) "This program will solve the Assignment 1 coupled ODEs, "
  33.     WRITE(6,*) "using the modified Euler method, and a t-step of 5e-3"
  34.  
  35.     ALLOCATE( x(0:(NINT(finalt/h))))
  36.     ALLOCATE( y(0:(NINT(finalt/h))))
  37.     ALLOCATE(vx(0:(NINT(finalt/h))))
  38.     ALLOCATE(vy(0:(NINT(finalt/h))))
  39.     ALLOCATE( t(0:(NINT(finalt/h))))
  40.     ! Looks at the finalt and h parameters and initializes all 1D arrays
  41.     ! to the correct size
  42.  
  43.      x(0) = -2.5
  44.      y(0) =  0.0
  45.     vx(0) =  2.0
  46.     vy(0) =  2.0
  47.      t(0) =  0.0
  48.     !Sets the boundary conditions as given
  49.  
  50.     OPEN (15, FILE='data.dat')
  51.  
  52.     WRITE(15,*) " #  t                         x(t)                      y(t)  &
  53.                         &                    vx(t)                     vy(t)"
  54.     WRITE(15,*) t(0), x(0), y(0), vx(0), vy(0)
  55.     !WRITE(15,'(1x, f6.3, 3x, es12.5E2, 3x, es12.5E2)') t(0),x(0),v(0)
  56.     !Prints the column headings as well as the values of x(t) and v(t)
  57.  
  58.     DO i = 1, (NINT(finalt/h))
  59.     !Main loop
  60.  
  61.         t(i)     = t(i-1) + h
  62.         ! Saves the current value for time to the appropriate index in
  63.         ! the array
  64.  
  65.         dxdt     = vx(i-1)
  66.         dvxdt    = -5*x(i-1) + (0.5*COS(2*t(i-1)))
  67.  
  68.         dydt     = vy(i-1)
  69.         dvydt    = -5*y(i-1) + (0.5*COS(2*t(i-1)))
  70.         ! As dictated by the problem sheet
  71.  
  72.         xmid     = x(i-1) + 0.5*h*dxdt
  73.         vxmid    = vx(i-1) + 0.5*h*dvxdt
  74.  
  75.         ymid     = y(i-1) + 0.5*h*dxdt
  76.         vymid    = vy(i-1) + 0.5*h*dvydt
  77.         ! As we must do whenever we use the modified Euler method
  78.  
  79.         dxdtmid    = vxmid
  80.         dvxdtmid   = -5*xmid + 0.5*COS(2*(t(i-1)+(0.5*h)))
  81.  
  82.         dydtmid    = vymid
  83.         dvydtmid   = -5*ymid + 0.5*COS(2*(t(i-1)+(0.5*h)))
  84.         ! Similar to what we did for dx and dv
  85.  
  86.         x(i)     = x(i-1)  + h*dxdtmid
  87.         y(i)     = y(i-1)  + h*dydtmid
  88.         vx(i)    = vx(i-1) + h*dvxdtmid
  89.         vy(i)    = vy(i-1) + h*dvydtmid
  90.         ! Saves the final values calculated for x and v into their
  91.         ! respective arrays
  92.  
  93.         WRITE(15,*) t(i), x(i), y(i), vx(i), vy(i)
  94.         ! WRITE(15,'(1x,f6.3,3x,es12.5E2,3x,es12.5E2)') t(i),x(i),v(i)
  95.         ! Prints the final values that were just calculated, along with
  96.         ! the time
  97.     END DO
  98.     CLOSE(15)
  99.     DEALLOCATE(x,y,vx,vy,t)
  100.     ! The loop has ended, the arrays are removed from memory, and the
  101.     ! file will no longer be written to
  102.  
  103.     WRITE(6,*) ''
  104.     WRITE(6,*) 'The results can be found in data.dat in the same folder&
  105.                & as this program.'
  106.     WRITE(6,*) 'You may plot them by running gnuplot and then using the&
  107.                & command:'
  108.     WRITE(6,*) "plot 'data.dat' u 1:2 w l, 'data.dat' u 1:3 w l"
  109.  
  110. END PROGRAM assignment1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement