brython-dev / brython-in-the-classroom

MIT License
14 stars 9 forks source link

Problem getting morphed binary stars code to work #26

Open rbarve opened 9 years ago

rbarve commented 9 years ago

This program below is an attempt to implement the one here : http://www.glowscript.org/#/user/GlowScriptDemos/folder/Examples/program/BinaryStar-VPython/edit but it doesnt work.. This was on Chrome beta 42.0 on Ubuntu 14.04 Notes: (a) I replaced the while True with the recursive version as in Pyschool's bounce (a) There seem to be some associative rule issues : the expression F = G * giant.mass * dwarf.mass * norm(r) / mag2(r) doesnt work even with r.norm() and r.mag2() so that I had to use : coeff = G * giant.mass * dwarf.mass/r.mag2() F = r.norm() * coeff to make it work.. (b) Initially I had the make_trail params to the spheres left as in the original vPython. But the program seemed to be executing with canvas/scene completely blank. Since I recall reading somewhere that make_trail had issues in brython-glow, I removed the make_trail params but still the canvas/scene is blank..

from glow import *

glow('pydiv') scene = canvas()

scene.forward = vec(0,-.3,-1).to_glowscript()

G = 6.7e-11

giant = sphere(pos=vec(-1e11,0,0), radius=2e10, color=color.red) giant.mass = 2e30 giant.p = vec(0, 0, -1e4) * giant.mass

dwarf = sphere(pos=vec(1.5e11,0,0), radius=1e10, color=color.yellow) dwarf.mass = 1e30 dwarf.p = vec(0,0,0)-giant.p r = dwarf.pos - giant.pos coeff = G * giant.mass * dwarf.mass/r.mag2() q = r.norm()*coeff print("q",q)

dt = 1e5 def move(): r = dwarf.pos - giant.pos

F = G * giant.mass * dwarf.mass * norm(r) / mag2(r)

coeff = G \* giant.mass \* dwarf.mass/r.mag2()
F = r.norm() \* coeff
giant.p = giant.p + F_dt
dwarf.p = dwarf.p - F_dt
giant.pos = giant.pos + (giant.p/giant.mass) \* dt
dwarf.pos = dwarf.pos + (dwarf.p/dwarf.mass) \* dt
rate(200,move)

print("start exec") move()

rbarve commented 9 years ago

Update -- when I replaced the radius arg with a size vector arg for the spheres the code started working However the associative rule problem of mult and div above remains. And the attach trail issue also remains .. below code works as expected without the trails

from glow import *

glow('pydiv') scene = canvas()

scene.forward = vec(0,-.3,-1).to_glowscript()

G = 6.7e-11

giant = sphere(pos=vec(-1e11,0,0), size=vec(2e10,2e10,2e10), color=color.red,make_trail=True, trail_type='points', interval=10, retain=50) giant.mass = 2e30 giant.p = vec(0, 0, -1e4) * giant.mass

dwarf = sphere(pos=vec(1.5e11,0,0), size=vec(1e10,1e10,1e10), color=color.yellow,make_trail=True,trail_type='points', interval=10, retain=40) dwarf.mass = 1e30 dwarf.p = vec(0,0,0)-giant.p r = dwarf.pos - giant.pos coeff = G * giant.mass * dwarf.mass/r.mag2() q = r.norm()*coeff print("q",q)

dt = 1e5 def move(): r = dwarf.pos - giant.pos

F = G * giant.mass * dwarf.mass * norm(r) / mag2(r)

coeff = G * giant.mass * dwarf.mass/r.mag2() F = r.norm() * coeff giant.p = giant.p + F_dt dwarf.p = dwarf.p - F_dt giant.pos = giant.pos + (giant.p/giant.mass) * dt dwarf.pos = dwarf.pos + (dwarf.p/dwarf.mass) * dt rate(200,move)

move()

rbarve commented 9 years ago

Actually one of the issues above -- the (a) issue -- is because you cant do Number*vec():

from glow import * glow('pydiv')

a=vec(1,2,1) b=a_2 print('b',b) c=2_a print('c',c)

ghost commented 9 years ago

Thanks for reporting this issue, and figuring out most of the issues. I'll look into adding/fixing the issue dealing with multiplying a vector by a number.