rudnichek

Untitled

Feb 27th, 2020
2,123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module const ! declarations of system parameters
  2.   implicit none
  3.   public
  4.   real*16, parameter :: &
  5.        m1 = 1.0d0, m2 = 1.25d0, &
  6.        l1 = 1.0d0, l2 = 1.0d0, &
  7.        g = 9.8d0,&
  8.        h = 0.02d0
  9.   integer, parameter :: &
  10.        n = 10e3, m = 4
  11.   integer :: i, j
  12.   real*16 :: &
  13.        ! parameters of decart representation
  14.        x1 = 0.0, y1 = 0.0, x2 = 0.0, y2 = 0.0
  15. end module const
  16. subroutine incep (q1, q2, q3, q4)
  17.   implicit none
  18.   real*16 :: q1, q2, q3, q4
  19.   q1 = 1.0
  20.   q2 = 3.0
  21.   q3 = 0.0
  22.   q4 = 0.0
  23. end subroutine incep
  24. !====================================================
  25.        
  26. function F1(phi1, phi2, w1, w2)
  27.   use const
  28.   implicit none
  29.   real*16 :: F1, phi1, phi2, w1, w2
  30.   F1 = w1
  31. end function F1
  32.  
  33. function F2(phi1, phi2, w1, w2)
  34.   use const
  35.   implicit none
  36.   real*16 :: F2, phi1, phi2, w1, w2
  37.   F2=w2
  38. end function F2
  39.  
  40. function F3(phi1, phi2, w1, w2)
  41.   use const
  42.   implicit none
  43.   real*16 :: F3, phi1, phi2, w1, w2, add1
  44.   add1 = l1*(2*m1+m2-m2*cos(2*phi2-2*phi1))
  45.   F3 = (-g*(2*m1+m2)*sin(phi1)+m2*g*sin(phi1-2*phi2)-2*sin(phi1-phi2)*m2*(w2*w2*l2+w1*w1*l1*cos(phi1-phi2)))/add1
  46. end function F3
  47.  
  48. function F4(phi1, phi2, w1, w2)
  49.   use const
  50.   implicit none
  51.   real*16 :: F4, phi1, phi2, w1, w2, add2
  52.   add2 = (l2*(2*m1+m2-m2*cos(2*phi2-2*phi1)))
  53.   F4 = (2*sin(phi1-phi2)*(w1*w1*l1*(m1+m2)+g*(m1+m2)*cos(phi1)+w2*w2*l2*m2*cos(phi1-phi2)))/add2
  54. end function F4
  55.  
  56. subroutine runge_kutt
  57.   use const
  58.   implicit none
  59.   real*16, external :: F1, F2, F3, F4
  60.   real*16 :: q1, q2, q3, q4
  61.   ! asign matrix with coefficiants
  62.   real*16, allocatable :: k1(:), k2(:), k3(:), k4(:)
  63.   ! matrix (n * m) with solution
  64.   real*16, allocatable :: y(:,:)
  65.  
  66.   allocate(k1(m), k2(m), k3(m), k4(m))
  67.   if (m==0) stop 'Allocation error, m = 0'
  68.  
  69.   allocate(y(n,m))
  70.   if (n==0) stop 'Allocation error, n = 0'
  71.   call incep (q1, q2, q3, q4)
  72.  
  73.    y(1,1) = q1 !  alpha1(0) = angle1 in rad
  74.    y(1,2) = q2 !  alpha2(0) = angle2 in rad
  75.    y(1,3) = q3 !  w1(0) = 0
  76.    y(1,4) = q4 !  w2(0) = 0
  77.  
  78.   open(unit = 1, file = 'xypendulum.txt')
  79.   open(unit = 2, file = 'angle.txt')
  80.   ! Algorithm Runge-Kutta
  81.   do i = 1, n-1, 1      
  82.      ! K_{ij} = (k11 & k12 & k13 & k14\\ ...)
  83.      k1(1) = F1(y(i,1), y(i,2), y(i,3), y(i,4))*h
  84.      k1(2) = F2(y(i,1), y(i,2), y(i,3), y(i,4))*h
  85.      k1(3) = F3(y(i,1), y(i,2), y(i,3), y(i,4))*h
  86.      k1(4) = F4(y(i,1), y(i,2), y(i,3), y(i,4))*h
  87.      
  88.      k2(1) = F1(y(i,1) + k1(1)/2, y(i,2) + k1(2)/2, y(i,3) + k1(3)/2, y(i,4) + k1(4)/2)*h
  89.      k2(2) = F2(y(i,1) + k1(1)/2, y(i,2) + k1(2)/2, y(i,3) + k1(3)/2, y(i,4) + k1(4)/2)*h
  90.      k2(3) = F3(y(i,1) + k1(1)/2, y(i,2) + k1(2)/2, y(i,3) + k1(3)/2, y(i,4) + k1(4)/2)*h
  91.      k2(4) = F4(y(i,1) + k1(1)/2, y(i,2) + k1(2)/2, y(i,3) + k1(3)/2, y(i,4) + k1(4)/2)*h
  92.      
  93.      k3(1) = F1(y(i,1) + k2(1)/2, y(i,2) + k2(2)/2, y(i,3) + k2(3)/2, y(i,4) + k2(4)/2)*h
  94.      k3(2) = F2(y(i,1) + k2(1)/2, y(i,2) + k2(2)/2, y(i,3) + k2(3)/2, y(i,4) + k2(4)/2)*h
  95.      k3(3) = F3(y(i,1) + k2(1)/2, y(i,2) + k2(2)/2, y(i,3) + k2(3)/2, y(i,4) + k2(4)/2)*h
  96.      k3(4) = F4(y(i,1) + k2(1)/2, y(i,2) + k2(2)/2, y(i,3) + k2(3)/2, y(i,4) + k2(4)/2)*h
  97.      
  98.      k4(1) = F1(y(i,1) + k3(1), y(i,2) + k3(2), y(i,3) + k3(3), y(i,4) + k3(4))*h
  99.      k4(2) = F2(y(i,1) + k3(1), y(i,2) + k3(2), y(i,3) + k3(3), y(i,4) + k3(4))*h
  100.      k4(3) = F3(y(i,1) + k3(1), y(i,2) + k3(2), y(i,3) + k3(3), y(i,4) + k3(4))*h
  101.      k4(4) = F4(y(i,1) + k3(1), y(i,2) + k3(2), y(i,3) + k3(3), y(i,4) + k3(4))*h
  102.      do j = 1, m
  103.         y(i + 1,j) = y(i,j) + (k1(j) + 2*k2(j) + 2*k3(j) + k4(j))/6
  104.      end do
  105.   end do
  106.   do i = 1, n
  107.      x1 = sin(y(i,1))
  108.      y1 = -cos(y(i,1))
  109.      x2 = x1 + sin(y(i,2))
  110.      y2 = y1 - cos(y(i,2))
  111.      write(1,'(1x, 4e20.12)') x2, y2, x1, y1 ! output
  112.      write(2,'(1x, 3e20.12)') (i-1)*h, y(i,2), y(i,1)
  113.   end do
  114.   deallocate(k1, k2, k3, k4, y)
  115.   close(1)
  116.   close(2)
  117. end subroutine runge_kutt
  118.  
  119. subroutine inter()
  120.   use const
  121.   implicit none
  122.   write(*,101)
  123. 101 format(1x,'System of differential equation of first order is solved'/&
  124.               ' due runge-kutta method of 4-th order')
  125.   write(*,'(a36, i8)') 'Number of rows (sampling rate) is  ', n
  126.   write(*,100) h
  127. 100 format(1x,'Step:  ', e10.4)
  128.   write(*,10)
  129. 10 format(1x,'Solution:  '/&
  130.          ' +----------------+-----------------+'/&
  131.          ' |    analitic    |  interpolation  |'/&
  132.          ' +----------------+-----------------+'/&
  133.          ' |    not exist   |  xypendulum.txt |'/&
  134.          ' +----------------+-----------------+'/&
  135.          ' |    ---------   |    angle.txt    |'/&
  136.          ' +----------------+-----------------+'/)
  137. end subroutine inter
  138.  
  139. program pendulum
  140.   implicit none
  141.   call runge_kutt
  142.   call inter()
  143. end program pendulum
Advertisement
Add Comment
Please, Sign In to add comment