What is problem?..

17 views
Skip to first unread message

yark

unread,
Dec 5, 2016, 8:30:58 PM12/5/16
to VPython-users
# -*- coding: cp949 -*-
#작은 전류 요소에 의한 자기장
#저자: 
#날짜: 2016-12-01

from visual import *
scene.height = scene.width = 800
L = 5.e3
scene.range = L

#상수
m_const = 1.e-7 #자기 상수

#작은 전류 요소

source_current = 1.   #전류(암페어)
source_axis = vector(0, 0.01*L, 0) #dl 벡터
source_pos = vector(0,-10*L,0) #위치
curve(pos=[(0, -10*L, 0),(0, 10*L, 0)], color=color.green)#dl 방향 연장선


#관찰 위치
obs_radius = 0.4*L #관찰 위치 반지름
d_theta = pi/3.
length = 2*L #자기장을 그릴 전체 거리
obs_y = -10*L #최초 관찰 위치 y 좌표
dy = L/10 #관찰 위치 증가분
obs = [vector(0,-10*L,0)] #관찰 위치 리스트
obs1 = vector(0,-10*L,0)
while obs1.y < 10*L:
    rate(100)
    obs1.y += L/10
    obs.append(obs1)
print obs


#자기장  
scale_factor_m = 2.e-10 #자기장 화살표 스케일
m_field_origin = vector(0,0,0)
i = 0
for hello in obs:
    r = hello - vector(0.05*obs_radius,0,0) #전류에서 보는 관찰 점의 상대 위치
    r_hat = r/mag(r) #상대위치의 단위 벡터
    if i==len(obs)-1:
        i = i-1
    m_field = m_const*source_current*cross(obs[i+1]-obs[i],r_hat)/mag(r)**2#자기장
    i = i+1
    source_pos.y += dy
    m_field_origin += m_field
print "유한한 길이의 도선에 의한 자기장= ", mag(m_field_origin)
m_inf_field = 4.*pi*10**-7*source_current/(2.*pi*obs_radius)
print "무한한 길이의 도선에 의한 자기장= ",m_inf_field

print "Done!!!"

I made this program. I want to increase obs1.y as much as 500 in while formal, but if I print list 'obs' , list 'obs'  is [vector(0, -50000, 0), vector(0, 50000, 0), vector(0, 50000, 0), vector(0, 50000, 0), vector(0, 50000, 0), vector(0, 50000, 0), vector(0, 50000, 0), ...., vector(0,50000,0)]. This is not result that I want. What is problem??

Bruce Sherwood

unread,
Dec 5, 2016, 8:41:08 PM12/5/16
to VPython-users
This is a subtle Python issue associated with "mutable" objects such as lists and vectors. When you change the y component of obs1, you do not create a new copy of obs1, you simply modify the vector object obs1. In your while loop you construct a list that looks like this:

obs is [vector(0,-10*L,0), obs1, obs1, obs1, obs1, .....]

where obs1 has the last value assigned to it, namely vector(0, 50000, 0).

One way to get what you want is with this: obs.append( vector(0,obs1.y,0) )

Minor point: You do not need a rate statement in that while loop. A rate statement is needed only if you want changes to visual objects to be seen during the loop.
Reply all
Reply to author
Forward
0 new messages