查看: 2196|回复: 0

[原创发布] 用 Ruby 做数值模拟和可视化

[复制链接]
梦石
0
星屑
1944
在线时间
404 小时
注册时间
2015-8-30
回帖
322
发表于 2020-4-26 13:11:09 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 有丘直方 于 2020-4-27 13:34 编辑

昨天下午的产物:
  1. require 'matrix'
  2. require 'bigdecimal'
  3. require 'openrgss'

  4. RGSS.title = 'Runge-Kutta'
  5. RGSS.show_fps = true

  6. module NumberRefinements
  7.         Float.prepend self
  8.         Integer.prepend self
  9.         BigDecimal.prepend self
  10.         def + other
  11.                 zero? && [Vector, Matrix].any? { other.is_a? _1 } ? other : super
  12.         end
  13. end

  14. class Vector
  15.         def self.build(...)
  16.                 new Array.new(...)
  17.         end
  18.         def to_ary
  19.                 to_a
  20.         end
  21. end

  22. class Array
  23.         def inner other
  24.                 zip(other).sum { _1 * _2 }
  25.         end
  26. end

  27. METHODS = [
  28. FORWARD_EULER = [[],[1]],
  29. EXPLICIT_MIDPOINT = [[],[1/2r],[0,1]],
  30. HEUN = [[],[1],[1/2r,1/2r]],
  31. RALSTON = [[],[2/3r],[1/4r,3/4r]],
  32. KUTTA_3RD = [[],[1/2r],[-1,2],[1/6r,2/3r,1/6r]],
  33. HEUN_3RD = [[],[1/3r],[0,2/3r],[1/4r,0,3/4r]],
  34. RALSTON_3RD = [[],[1/2r],[0,3/4r],[2/9r,1/3r,4/9r]],
  35. SSPRK3 = [[],[1],[1/4r,1/4r],[1/6r,1/6r,2/3r]],
  36. CLASSIC_4TH = [[],[1/2r],[0,1/2r],[0,0,1],[1/6r,1/3r,1/3r,1/6r]],
  37. RALSTON_4TH = [[],[0.4],[0.29697761,0.15875964],[0.21810040,-3.05096516,3.83286476],[0.17476028, -0.55148066, 1.20553560, 0.17118478]],
  38. THREE_EIGHTH_4TH = [[],[1/3r],[-1/3r,1],[1,-1,1],[1/8r,3/8r,3/8r,1/8r]]
  39. ]

  40. class Canvas
  41.         attr_accessor :bitmap, :color
  42.         def initialize
  43.                 @bitmap = Bitmap.new 1024, 768
  44.                 @color = Color.new 255, 255, 255
  45.         end
  46.         def trace t, ret
  47.                 block = ->y { @bitmap.set_pixel t*128, 200*(y+2), @color }
  48.                 ret.is_a?(Enumerable) ? ret.each(&block) : block.(ret)
  49.         end
  50. end

  51. $canvas = Canvas.new

  52. def runge_kutta initial, max_t, dt, (*pyramid, coefs), func
  53.         (0..max_t).step(dt).reduce initial do |ret, t|
  54.                 $canvas&.trace t, ret
  55.                 coefs.zip(pyramid).each_with_object([]).sum do |(coef, row), ary|
  56.                         coef * ary.push(func.(t, row.inner(ary) * dt + ret)).last
  57.                 end * dt + ret#p(ret)
  58.         end
  59. end

  60. def div x0, dx, f
  61.         f0 = f.(x0)
  62.         n = x0.size
  63.         Vector.build n do |i|
  64.                 (f.(x0 + Vector.basis(size: n, index: i) * dx) - f0) / dx
  65.         end
  66. end

  67. def solve_hamiltonian n, x0, t, dt, hamiltonian
  68.         runge_kutta Vector[*x0], t, dt, CLASSIC_4TH, ->t, x do
  69.                 dxdt = div x, 1e-5, ->qp { hamiltonian.(t, qp) }
  70.                 Vector[*dxdt[n...n*2], *dxdt[0...n].map(&:-@)]
  71.         end
  72. end

  73. BigDecimal.limit 25

  74. include RGSS

  75. Graphics.resize_screen 1024, 768
  76. rgss_main do
  77.         Thread.start do
  78.                 Sprite.new.bitmap = $canvas.bitmap
  79.                 rgss_stop
  80.         end
  81. end

  82. alias ` BigDecimal

  83. #METHODS.each do |method|
  84. #        p runge_kutta `1`, `1`, `1e-4`, method, ->t,x { x*t }
  85. #end

  86. #p runge_kutta Vector[`1`,`0`], `4`, `1e-4`, CLASSIC_4TH, ->t,v { v.cross }
  87. p solve_hamiltonian 1, Vector[`0`,`1`], `8`, `1e-4`, ->t,(x,p) { x**2+0.3*x**3 + p**2 }
  88. gets
复制代码

这个 openrgss 是我魔改过的:https://github.com/UlyssesZh/openrgss
这里模拟了个非简谐振动,Hamiltonian 是 H=x^2+0.3x^3+p^2
看下效果:
Snipaste_2020-04-26_12-51-30.png
这里有个 RGSS 版的:
RungeKutta.rar (1.25 MB, 下载次数: 14)


把 RGSS 版的稍微修改了一下。这里做了个双摆的模拟。
Animation.gif
RungeKutta.rar (1.25 MB, 下载次数: 27)

评分

参与人数 2赞 +2 收起 理由
Mono_kyrin + 1 精品文章
小吵闹的革命 + 1 精品文章

查看全部评分

小仙女一枚~
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部