文档章节

RK4

ludlows
 ludlows
发布于 2014/10/07 22:55
字数 475
阅读 14
收藏 0
MODULE Global_data
  !Symbolic names for kind types of single- and double-precision reals: 
  INTEGER, PARAMETER :: SP = KIND(1.0) 
  INTEGER, PARAMETER :: DP = KIND(1.0D0) 
  !Frequently used mathematical constants (with precision to spare): 
  REAL(DP), PARAMETER :: Pi=3.141592653589793238462643383279502884197_dp 
  ! order of the problem
  INTEGER, PARAMETER :: no_of_equations=2
  ! parameters for the equation
  REAL(DP) :: y_initial(no_of_equations)
  REAL(DP) :: x_start,x_finish,h,kappa
END MODULE Global_data

MODULE RungeKutta_method
! here we just need dp, kappa, h and no_of_equations
! if we set wp=>sp we can change to single precision numbers
  USE global_data, ONLY : wp=>dp,h,kappa,no_of_equations
  IMPLICIT NONE

CONTAINS

  FUNCTION func(x,y)
!    we assign y to have 'no_of_equations's elements throughout to keep the program consistent
    REAL(wp) , INTENT(IN) :: x,y(no_of_equations)
    REAL(wp) :: func(no_of_equations)
!   The ODE here is y'' + kappa y' + x y = 0
!   so that the set of first ODEs is
!    y_1' = y_2
!    y_2' = - kappa y_2 - x y_1
! where we write y_1 = y and y_2 = y'
    func(1) = y(2)
    func(2) = -kappa*y(2) - x*y(1)

  END FUNCTION func

  FUNCTION rungekutta4(x,y)

    REAL(wp) , INTENT(IN) :: x,y(no_of_equations)
    REAL(wp),DIMENSION(no_of_equations) :: rungekutta4,k1,k2,k3,k4
    ! Note here that k1,k2,... etc are two element arrays.
    k1 = h*func(x,y)
    k2 = h*func(x+0.5_wp*h,y+0.5_wp*k1)
    k3 = h*func(x+0.5_wp*h,y+0.5_wp*k2)
    k4 = h*func(x+h,y+k3)
    rungekutta4 = y + (k1+2._wp*(k2+k3) + k4)/6._wp

  END FUNCTION rungekutta4

END MODULE RungeKutta_method

PROGRAM euler
  ! we need all the variables from global data
  USE global_data, ONLY : wp=>dp,kappa,h,x_start,x_finish,y_initial,no_of_equations
  ! RungeKutta_method gives us our rungekutta method function
  USE RungeKutta_method
  IMPLICIT NONE
  
  INTEGER :: i,no_of_steps
  REAL(wp) :: x,y(no_of_equations)
  
  ! Setup initial conditions
  ! set x_0 = 0
  x_start = 0._wp
  ! set x_n = 1
  x_finish = 1._wp
  ! Set y(x=0) = 0
  y_initial(1)=0._wp  
  ! Set y'(x=0) = 1
  y_initial(2)=1._wp
  ! Set the number of steps
  no_of_steps = 10
  ! Set the parameter kappa from the ODE
  kappa = 5._wp
  ! Set the step size
  h = (x_finish-x_start)/dble(no_of_steps)

  ! Open a file to write to and output some info to screen
  WRITE(6,*)' Run with ::',no_of_steps,' steps.'
  OPEN(unit=10,file="runge_kutta.dat")

  ! Set the start values of x and y
  x = x_start
  y = y_initial
  WRITE(6,'(3(E15.8,1X))')x,y
  WRITE(10,'(3(E15.8,1X))')x,y
    
  ! Loop over the required no. of steps to reach x_finish
  DO i=1,no_of_steps
	! update the values of x and y
     y = rungekutta4(x,y)
     x = x + h

     ! pick out only 10 value to print at.
     if(mod(i,no_of_steps/10)==0)THEN
     	! print to screen and file
         WRITE(6,'(3(E15.8,1X))')x,y
         WRITE(10,'(3(E15.8,1X))')x,y
     END if
        
  END DO

  CLOSE(unit=10)
  
END PROGRAM euler


© 著作权归作者所有

共有 人打赏支持
ludlows
粉丝 0
博文 15
码字总数 4195
作品 0
海淀
程序员
加载中

评论(2)

ludlows
ludlows

引用来自“明天以后”的评论

. 什么语言 没看懂...
fortran 这语言太老了啊
明天以后
明天以后
. 什么语言 没看懂...
利用RK4更新四元數並轉換成歐拉角及 OpenGL 的 Viewer

開發平台 : 硬體規格 Odroid-U2 作業系統 Ubuntu 13.10 Codename: saucy 下面都是一些基本的程式可示需求修改, 之後我會把算法跟GUI整再一起開發. 四元數更新 : Windows : dev-c++ 專案 : qu...

cctsao1008
2014/11/28
0
2

没有更多内容

加载失败,请刷新页面

加载更多
记Structured Streaming 2.3.1的OOM排查过程

记Structured Streaming 2.3.1的OOM排查过程 缘起 最近在使用Structured Streaming开发一套自助配置SQL的来生成流式作业的平台,在测试的过程中发现有些作业长时间运行后会有Executor端的OOM...

纳兰清风
7分钟前
0
0
通过 websocket 连接 openfire

通过 websocket 连接 openfire 一、简介 老版本是需要安装websocket插件的。之前版本就不说了。 二、 引入XML与JSON互转的js <script src="lib/XML/ObjTree.js"></script><script src="lib......

lwenhao
11分钟前
0
0
MicropPython的学习,如何从0到1?

是Java还是C++ ? 是Go还是Python? 你为你最爱或者最熟悉的编程语言打Call了吗? Python在国外是一门非常热门的编程语言,近些年在国内也被发掘与追捧, Python的开放、简洁、黏合正符合了现发...

bodasisiter
12分钟前
0
0
Django框架17: Celery的使用

Celery介绍 1.什么是Celery Celery是一个python模块,它在官网的定义:Celery is asynchronous task queue/job based on distributed message passing. It is focused on real-time operatio......

代码打碟手
18分钟前
0
0
poi的excel解析工具类

import org.apache.commons.lang.StringUtils;import org.apache.commons.lang.time.DateFormatUtils;import org.apache.commons.lang.time.DateUtils;import org.apache.poi.hssf.use......

Sheav
22分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部