Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Understanding Python Programs

  • 06-09-2011 10:52pm
    #1
    Registered Users, Registered Users 2 Posts: 578 ✭✭✭


    I have some Python programs for showing some physics simulations, I haven't looked at python in over a year and then only spent a very short time on it. I was wondering if someone with knowledge of python could help me with understanding them. They are ran in vIdle for vPython.

    Bouncing Projectile Code :


    from visual import *

    floor = box(length=4, height=0.5, width=4, color=color.blue)

    ball = sphere(pos=(0,4,0), color=color.red, radius=1)
    ball.velocity = vector(0,-1,0)

    dt = 0.01
    while 1:
    rate(100)
    ball.pos = ball.pos + ball.velocity*dt
    if ball.y < 1:
    ball.velocity.y = -ball.velocity.y
    else:
    ball.velocity.y = ball.velocity.y - 9.8*dt


    Circular Motion Code:


    from visual import *

    ball = sphere(pos=(0,10,0), color=color.magenta, radius=0.5)
    string = cylinder(pos=(0,0,0), axis=(0,10,0), color=color.white, radius=0.05)
    PI = 3.1415926535
    radius = 10
    t=0
    scene.autoscale = 0
    while 1:
    rate(100)
    t = t + 0.01
    ball.pos = (radius * sin (PI*t - 0.5 * sin(PI*t)),radius * cos (PI*t - 0.5 * sin(PI*t)),0)
    string.axis = (ball.pos.x,ball.pos.y,0)
    sphere(pos=(10 * sin (PI*t - 0.5 * sin(PI*t)),10 * cos (PI*t - 0.5 * sin(PI*t)),0),color=color.blue, radius=0.1)

    Multiple Balls Code:


    from visual import *
    from random import uniform

    thk = 0.3
    side = 4.0
    s2 = 2*side - thk
    s3 = 2*side + thk

    wallR = box (pos=vector( side, 0, 0), length=thk, height=s2, width=s3, color = color.green)
    wallL = box (pos=vector(-side, 0, 0), length=thk, height=s2, width=s3, color = color.green)
    wallB = box (pos=vector(0, -side, 0), length=s3, height=thk, width=s3, color = color.green)
    wallT = box (pos=vector(0, side, 0), length=s3, height=thk, width=s3, color = color.green)
    wallBK = box(pos=vector(0, 0, -side), length=s2, height=s2, width=thk, color = color.white)

    no_particles=10
    ball_radius= 0.5
    maxpos=side-.5*thk-ball_radius
    maxv=1.0

    ball_list=[]
    for i in arange(no_particles):
    ball=sphere(color=color.yellow,radius=ball_radius)
    ball.pos=maxpos*vector(uniform(-1,1),uniform(-1,1),uniform(-1,1))
    ball.velocity=maxv*vector(uniform(-1,1),uniform(-1,1),uniform(-1,1))
    ball_list.append(ball)

    timestep = 0.05

    while (1==1):
    rate(100)
    for ball in ball_list:
    ball.pos = ball.pos + ball.velocity*timestep
    if ball.x > maxpos:
    ball.velocity.x = -ball.velocity.x
    ball.x=2*maxpos-ball.x
    if ball.x < -maxpos:
    ball.velocity.x = -ball.velocity.x
    ball.x=-2*maxpos-ball.x
    if ball.y > maxpos:
    ball.velocity.y = -ball.velocity.y
    ball.y=2*maxpos-ball.y
    if ball.y < -maxpos:
    ball.velocity.y = -ball.velocity.y
    ball.y=-2*maxpos-ball.y
    if ball.z > maxpos:
    ball.velocity.z = -ball.velocity.z
    ball.z=2*maxpos-ball.z
    if ball.z < -maxpos:
    ball.velocity.z = -ball.velocity.z
    ball.z=-2*maxpos-ball.z

    for i in range(no_particles):
    for j in range(i+1,no_particles):
    distance=mag(ball_list.pos-ball_list[j].pos)
    if distance<(ball_list.radius+ball_list[j].radius):
    direction=norm(ball_list[j].pos-ball_list.pos)
    vi=dot(ball_list.velocity,direction)
    vj=dot(ball_list[j].velocity,direction)
    exchange=vj-vi
    ball_list.velocity=ball_list.velocity + exchange*direction
    ball_list[j].velocity=ball_list[j].velocity - exchange*direction
    overlap=2*ball_radius-distance
    ball_list.pos=ball_list.pos - overlap*direction
    ball_list[j].pos=ball_list[j].pos + overlap*direction


    Thanks in advance for help.


Comments

  • Closed Accounts Posts: 9,139 ✭✭✭-Trek-


    Interesting add on, never knew existed, could be very useful :), there is documentation for visual here http://www.vpython.org/index.html

    They explain the first program in your post in detail also if it helps....
    Here is a complete VPython program that produces a 3D animation of a red ball bouncing on a blue floor as shown above. Note that in the "while" loop there are no graphics commands, just computations to update the position of the ball and check whether it hits the floor. The 3D animation is a side effect of these computations.
    from visual import *
    
    floor = box (pos=(0,0,0), length=4, height=0.5, width=4, color=color.blue)
    ball = sphere (pos=(0,4,0), radius=1, color=color.red)
    ball.velocity = vector(0,-1,0)
    dt = 0.01
    
    while 1:
        rate (100)
        ball.pos = ball.pos + ball.velocity*dt
        if ball.y < ball.radius:
            ball.velocity.y = abs(ball.velocity.y)
        else:
            ball.velocity.y = ball.velocity.y - 9.8*dt
    
    The program starts by importing the module "visual" which enables 3D graphics.

    A box and a sphere are created and given names "floor" and "ball" so one can refer to these objects.

    The ball is given a vector velocity.

    In the while loop, "rate(100" has the effect "no more than 100 loops per second on fast computers."

    The ball's velocity is used to update its position, in a single vector statement that updates x, y, and z.

    Visual periodically use the current values of the objects' attributes, including ball.pos, to paint a 3D picture.

    The program checks for the ball touching the floor and if necessary reverses the y-component of velocity, otherwise the velocity is updated due to the gravitational force.


Advertisement