Posted to tcl by hypnotoad at Thu Jun 19 17:09:28 GMT 2014view raw

  1. source [file normalize [file dirname [info script]]]/../../modules/tao-physics/index.tcl
  2. package require Tk
  3. package require Plotchart
  4. source ~/odie/sandbox/tklib/modules/plotchart/xyplot.tcl
  5. tao::class rocket {
  6. superclass physics::base
  7.  
  8. property physics mass {
  9. default 200
  10. units kg
  11. }
  12.  
  13. property physics fuel {
  14. default 10
  15. units kg
  16. }
  17.  
  18. property physics fuel_burn_rate {
  19. default 1
  20. units kg/s
  21. }
  22.  
  23. property physics fuel_energy_release {
  24. default 10000
  25. units N/kg
  26. }
  27.  
  28. option world {
  29. default {}
  30. }
  31.  
  32. constructor {objects args} {
  33. foreach {item obj} $objects {
  34. my graft $item $obj
  35. }
  36. my configurelist [::tao::args_to_options {*}$args]
  37. my initialize
  38. }
  39.  
  40. }
  41.  
  42. ::physics::world create world
  43.  
  44. set n [rocket new {world ::world}]
  45. $n physics heading [::vector::scale {0 0 45} [::odiemath::m_pi_180]]
  46. #
  47. #exit
  48. set pscript {
  49. set g [my <world> physics property gravity]
  50. set thrust {0 0 0}
  51. if {$fuel > 0} {
  52. set dfuel [expr {$::dT * $fuel_burn_rate}]
  53. if {$dfuel > $fuel} {
  54. set dfuel $fuel
  55. set fuel 0
  56. } else {
  57. set fuel [expr {$fuel-$dfuel}]
  58. }
  59. if { $dfuel <= 0} {
  60. set thrust_mag 0.0
  61. set fuel 0.0
  62. } else {
  63. set mass [expr {$mass-$dfuel}]
  64. set thrust [::vector::scale $direction [expr {$dfuel * $fuel_energy_release/$mass}] 0]
  65. }
  66. }
  67. #puts [list f: $fuel $fuel_burn_rate $thrust]
  68. set acceleration [vector::add [vector::scale $g $::dT] $thrust]
  69. set velocity [vector::add $velocity $acceleration]
  70. set position [vector::add $position [::vector::scale $velocity $::dT]]
  71. }
  72.  
  73. set ::dT 0.1
  74. for {set step 0} {$step < 2000} {incr step} {
  75. set state [$n physics with $pscript]
  76. dict with state {}
  77. set t [expr {$step*$::dT}]
  78. #puts [list [expr {$t/10.0}] x $position v $velocity a $acceleration m $mass m/f $fuel t $thrust $direction]
  79. set plot_position($t) $position
  80. if {[lindex $position 1] < 0} break
  81. }
  82.  
  83. set minx 1e10
  84. set maxx -1e10
  85. set miny 1e10
  86. set maxy -1e10
  87.  
  88. foreach {t pos} [array get plot_position] {
  89. foreach {x y z} $pos {}
  90. if { $x > $maxx } { set maxx $x }
  91. if { $x < $minx } { set minx $x }
  92. if { $y > $maxy } { set maxy $y }
  93. if { $y < $miny } { set miny $x }
  94. }
  95.  
  96. canvas .plot -width 500 -height 500
  97. pack .plot
  98.  
  99.  
  100. set p [::Plotchart::createXYPlot .plot [list $minx $maxx 10] [list $miny $maxy 10]]
  101. foreach t [lsort -real [array names plot_position]] {
  102. # If you want to crash tcl use:
  103. #lappend plotlist {*}[lrange $plot_position($t) 0 1]
  104.  
  105. lappend plotlist $t {*}[set lrange lrange; $lrange $plot_position($t) 0 1]
  106. #lappend plotlist {*}[lrange [::vector::to_list $plot_position($t)] 0 1]
  107. #lappend plotlist {*}[::vector::index $plot_position($t) 0 1]
  108. }
  109.  
  110. # HERE Prints
  111. puts HERE
  112.  
  113. foreach {t x y} $plotlist {
  114. puts [list $p $t plot main [expr {int($x)}] [expr int($y)]]
  115. $p plot main [expr {int($x)}] [expr int($y)]
  116. puts [list /$p plot main [expr {int($x)}] [expr int($y)]]
  117.  
  118. }
  119. puts done
  120.  
  121. if 0 {
  122.  
  123. foreach {lx ly lx} $plot_position(0.0) {}
  124.  
  125. set scalex [expr {500/($maxx-$minx)}]
  126. set scaley [expr {500/($maxy-$miny)}]
  127. if { $scalex > $scaley } {
  128. set scale $scaley
  129. } else {
  130. set scale $scalex
  131. }
  132.  
  133.  
  134.  
  135. foreach t [lsort -real [array names plot_position]] {
  136. puts $t
  137. puts [list $t $plot_position($t)]
  138. foreach {x y z} $plot_position($t) {}
  139.  
  140. .plot create oval [expr {($x-$minx)*$scale-1}] [expr {($maxy-$y)*$scale-1}] [expr {($x-$minx)*$scale+1}] [expr {($maxy-$y)*$scale+1}]
  141. }
  142. }