Nugroho's blog.

Friday, May 6, 2016

Piston Free Expansion Oscillation.

The result of daydreaming, :)

#code
from visual import *
from random import uniform,random
from visual.controls import *

display(center=(0,0,0),background=(1,1,1), autoscale=False, range=(5,7,7),
width=600, height=600, forward=(0.4,-0.3,-1)) #arah kamera

distant_light(direction=(1,1,1), color=color.red)
cylinder(pos=(-4,0,0), axis=(7,0,0), radius=1, color=color.green, opacity=.1)
piston = cylinder(pos=(-2,0,0), axis=(1,0,0), radius=.7, color=color.red)
piston1 = cylinder(pos=(-2,0,0), axis=(5,0,0), radius=.1, color=color.red)

p0 = 1.
A = 1.
x0 = 5. #panjang piston
c = p0*A*x0
x = 3
dx = x0-x #
m = 1.
v = 0.
dt = 1./8.
def proses():
global x,v,dx
p = c/x
dp = p-p0
print x,dp
f = -dp
a = f/m
v += a*dt
dx += v*dt
x -= v*dt

piston.x = dx
piston1.x = dx
while 1:
rate (19)
proses()






.



Ball Oscillation Inside a Sphere

 with Visual Python.

 I use nonlinear solution, :)


#code
from visual import *
from random import uniform,random
from visual.controls import *

display(center=(0,0,0),background=(1,1,1), #autoscale=False,
width=600, height=600, forward=(-0.4,-0.3,-1)) #arah kamera

distant_light(direction=(1,1,1), color=color.red)

dl = .01
l = 1.1

box(color=color.white, pos=(0,0,0),length=2*l,height=dl, width=dl, opacity=.3)
box(color=color.white, pos=(0,0,0),length=dl,height=2*l, width=dl, opacity=.3)
box(color=color.white, pos=(0,0,0),length=dl,height=dl, width=2*l, opacity=.3)


bola1 = sphere (pos=(0,0,0), radius=1.1, color=color.green, opacity=.3)
bola = sphere (pos=(1,0,0), radius=.1, color=color.red)

r = 1.
x = 1.
m = 1.
g = 1.
vx = 0.
dt = 1./16.
def proses():
global x,vx
f = -m*g*x/r
a = f/m
vx += a*dt
x += vx*dt
r2 = r*r
x2 = x*x
if x2>r2:
x2 = r2
y = r-sqrt(r2-x2)
bola.x = x
print x
bola.y = y-r

while 1:
rate (19)
proses()







.




Monday, May 2, 2016

Thursday, April 28, 2016

3D Ball Collision in Python with Visual Module.


I only compute the collision between bed and blue one. They're bola[1] and bola[2] respectively


from visual import *
from random import uniform,random
from visual.controls import *

def change():
global jalan
if b.value:
jalan = True
for ball in(bola):
ball.vx = uniform(-7,7)
ball.vy = uniform(-7,7)
ball.vz = uniform(-7,7)
else:
jalan = False

c = controls(title='Tempat Tombol',x=800, y=0, width=300, height=300, range=50)
b = toggle( pos=(0,0), width=20, height=20, text='Click me', action=lambda: change() )
display(center=(0,0,0),background=(1,1,1), #autoscale=False,
width=600, height=600, forward=(-0.4,-0.3,-1)) #arah kamera
g = -1.
dt = .1
e = 1.
b.value = True
jalan = True
l = 17.
dl = .01
n = 11

distant_light(direction=(1,1,1), color=color.red)
lantai = box(color=color.white, pos=(0,0,0),length=l,height=dl, width=l, opacity=.3)
dindingKiri = box(color=color.white, pos=(-l/2,l/2,0),length=dl,height=l, width=l, opacity=.3)
dindingKanan= box(color=color.white, pos=(l/2,l/2,0),length=dl,height=l, width=l, opacity=.3)
dBelakang = box(color=color.white, pos=(0,l/2,-l/2),length=l,height=l, width=dl, opacity=.3)
atap = box(color=color.white, pos=(0,l,0),length=l,height=dl, width=l, opacity=.3)
bola = []
for i in arange(n):
ball = sphere (pos=(uniform(1,7),0,uniform(-7,7)), radius=.3, color=color.green)
ball.v = vector(uniform(-7,7),uniform(-7,7),uniform(-7,7))
bola.append(ball)


bola[1].radius = 2.5
bola[2].radius = 2.5

bola[1].color = color.red
bola[2].color = color.blue
def proses():
for ball in (bola):
a = g
ball.v[1] += a*dt
ball.pos+= ball.v*dt
tumbukan()
tumbukanBola()

def tumbukan():
for ball in(bola):
if ball.y<0:
ball.y = 0.01
ball.v[1] *=-1.*e
elif ball.y>l:
ball.y = l-.01
ball.v[1] *= -1
if ball.x<-l/2:
ball.x=-l/2+.01
ball.v[0] *= -1*e
if ball.x>l/2:
ball.x=l/2-.01
ball.v[0] *= -1*e
if ball.z<-l/2:
ball.z=-l/2+.01
ball.v[2] *= -1*e
if ball.z>l/2:
ball.z=l/2-.01
ball.v[2] *= -1*e

def tumbukanBola():
#pass
jarak = mag(bola[2].pos-bola[1].pos)
if jarak<(bola[1].radius+bola[2].radius):
arah = norm(bola[2].pos-bola[1].pos)
v1 = dot(bola[1].v,arah)
v2 = dot(bola[2].v,arah)
dv = v2-v1
bola[1].v += dv*arah
bola[2].v -= dv*arah



while 1:
rate (51)
if jalan:
proses()







.





Alright, Now Do It in Python, with style, :)


Python version of this flash action script of electron under Lorentz force, :)

from visual import *
from random import uniform

display(center=(0,0,0),background=(1,1,1), autoscale=False,
width=600, height=600,
#forward=(-0.4,-0.3,-1)
)

distant_light(direction=(1,1,1), color=color.red)


l = 11
dt = 1./8.
medan = box(color=color.white,
pos=(l/2,0,0),length=l,height=l,
width=l, opacity=.3)

ball = sphere (pos=(-7,0,0), radius=.3, color=(uniform(0,1),uniform(0,1),uniform(0,1)))

def awal():
global q,m,B,v
q = 1.
m = 1.
B = vector(0.,0.,1.)
v = vector(3.,0.,0.)


def proses():
global v,B
if ball.x > 0:
B = vector(0.,0.,1.)
else:
B = vector(0.,0.,0.)
print B
F = q*(cross(v,B))
a = F/m
v += a*dt

ball.pos += v*dt
print ball.x

awal()
while 1:
rate (37)
proses()



.





Menyapa Senjata Lama.


Sudah lama tidak otak-atik Macromedia Flash.

Yup, masih yang Macromedia, bukan Adobe, :) .

Membuat animasi bola bermuatan (atau elektron) yang bergerak lurus dengan kecepatan konstan tiba-tiba mencapai daerah dengan  medan magnet. Sesuai hukum Lorentz maka bola/elektron akan bergerak melengkung.

Action script hanya ditulis pada action di frame pertama layer background.






px0 = bola._x;
py0 = bola._y;
awal();
_root.onEnterFrame = function() {
if (jalan == true) {
proses();
}
//trace(jalan)
};
function awal() {
jalan = false;
bola._x = px0;
bola._y = py0;
q = 1;
B = 1;
tB.text = B;
//massa
m = 1;
tm.text = m;
//kecepatan
vx = 50;
vy = -10;
tvx.text = vx;
tvy.text = vy;
//percepatan
ax = 0;
ay = 0;
//posisi
px = 0;
py = 0;
dt = 1/8;
}
function bacaInput() {
vx = Number(tvx.text);
vy = Number(tvy.text);
m = Number(tm.text);
B = Number(tB.text);
}
function updateNilai() {
tB.text = B;
tm.text = m;
tvx.text = vx;
tvy.text = vy;
}
function proses() {
v = Math.sqrt(vx*vx+vy*vy);
if (bola._x>200) {
F = B*q*v;
} else {
F = 0;
}
a = F/m;
//arah vektor normal v
nvx = vx/v;
nvy = vy/v;
/*arah vektor percepatan
karena vektor yang tegak lurus a=(ax,ay) adalah at=(-ay,ax)
*/
nax = -nvy;
nay = nvx;
ax = a*nax;
ay = a*nay;
//hitung kecepatan baru
vx += ax*dt;
vy += ay*dt;
px += vx*dt;
py -= vy*dt;
trace(F);
//update posisi bola
bola._x = px0+px;
bola._y = py0+py;
updateNilai();
}
//tombol-tombol
//tombol tbJalan
tbJalan.onRelease = function() {
bacaInput();
jalan = true;
};
tbStop.onRelease = function() {
jalan = false;
};
tbReset.onRelease = function() {
awal();
};


.







Wednesday, April 27, 2016

Iterasi di Python


Jika kita punya sebuah list bernama bola yang didefinisikan sebagai:

bola = []

kemudian kita membuat obyek bernama ball

ball = sphere (pos=(uniform(1,7),0,uniform(-7,7)), radius=.3, color (uniform(0,1),uniform(0,1),uniform(0,1)))
ball.vx  = uniform(-7,7)
ball.vy  = uniform(-7,7)
ball.vz  = uniform(-7,7)
 
Obyek ini kita masukkan ke dalam bola dengan perintah

bola.append(ball)

Kita dapat melakukannya berkali-kali sehingga pada list bola terdapat beberapa obyek bernama ball

Jika kita ingin mengakses obyek tersebut, kita dapat menggunakan perintah semacam

bola[0].vx = 1

jika kita ingin mengakses vx di semua obyek bola, kita dapat menggunakan iterasi

for i in arange (n):
  bola[i].vx = 1


Namun di python ada cara lain yang juga mudah

for ball in (bola):
  ball.vx = 1

Berikut contoh kode yang menggunakan iterasi seperti itu

from visual import *
from random import uniform,random
from visual.controls import *

def change():
global jalan,vx,vy,vz
if b.value:
jalan = True
for ball in(bola):
ball.vx = uniform(-7,7)
ball.vy = uniform(-7,7)
ball.vz = uniform(-7,7)
else:
jalan = False

c = controls(title='Tempat Tombol',x=800, y=0, width=300, height=300, range=50)
b = toggle( pos=(0,0), width=20, height=20, text='Click me', action=lambda: change() )
display(center=(0,0,0),background=(1,1,1), #autoscale=False,
width=600, height=600, forward=(-0.4,-0.3,-1)) #arah kamera
g = -1.
dt = .1
e = 1.
b.value = True
jalan = True
l = 17.
dl = .01
n = 11

distant_light(direction=(1,1,1), color=color.red)
lantai = box(color=color.white, pos=(0,0,0),length=l,height=dl, width=l, opacity=.3)
dindingKiri = box(color=color.white, pos=(-l/2,l/2,0),length=dl,height=l, width=l, opacity=.3)
dindingKanan= box(color=color.white, pos=(l/2,l/2,0),length=dl,height=l, width=l, opacity=.3)
dBelakang = box(color=color.white, pos=(0,l/2,-l/2),length=l,height=l, width=dl, opacity=.3)
atap = box(color=color.white, pos=(0,l,0),length=l,height=dl, width=l, opacity=.3)
bola = []
for i in arange(n):
ball = sphere (pos=(uniform(1,7),0,uniform(-7,7)), radius=.3, color=(uniform(0,1),uniform(0,1),uniform(0,1)))
ball.vx = uniform(-7,7)
ball.vy = uniform(-7,7)
ball.vz = uniform(-7,7)
bola.append(ball)

def proses():
global vx,vy,vz
for ball in (bola):
a = g
ball.vy += a*dt
ball.pos+= vector(ball.vx*dt,ball.vy*dt,ball.vz*dt)
tumbukan()

def tumbukan():
global vx,vy,vz
for ball in(bola):
if ball.y<0:
ball.y = 0.01
ball.vy *=-1.*e
elif ball.y>l:
ball.y = l-.01
ball.vy *= -1
if ball.x<-l/2:
ball.x=-l/2+.01
ball.vx *= -1*e
if ball.x>l/2:
ball.x=l/2-.01
ball.vx *= -1*e
if ball.z<-l/2:
ball.z=-l/2+.01
ball.vz *= -1*e
if ball.z>l/2:
ball.z=l/2-.01
ball.vz *= -1*e

while 1:
rate (100)
if jalan:
proses()


.




323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)