Example #1
0
    # Check to see if the spheres are colliding
    for ij in hitlist:
        s1, s2 = divmod(ij, Nsp)  # decode the spheres pair (s1,s2) colliding
        hitlist.remove(s2 * Nsp +
                       s1)  # remove symmetric (s2,s1) pair from list
        R12 = Pos[s2] - Pos[s1]
        nR12 = np.linalg.norm(R12)
        d12 = Radius[s1] + Radius[s2] - nR12
        tau = R12 / nR12
        DR0 = d12 * tau
        x1 = Mass[s1] / (Mass[s1] + Mass[s2])
        x2 = 1 - x1  # x2 = Mass[s2]/(Mass[s1]+Mass[s2])
        Pos[s1] -= x2 * DR0
        Pos[s2] += x1 * DR0
        DV0 = 2 * dot(Vel[s2] - Vel[s1], tau) * tau
        Vel[s1] += x2 * DV0
        Vel[s2] -= x1 * DV0

    # Update the location of the spheres
    for s in range(Nsp):
        Spheres[s].pos([Pos[s][0], Pos[s][1], 0])

    if not int(i) % 10:  # every ten steps:
        rsp = [Pos[0][0], Pos[0][1], 0]
        rsv = [Vel[0][0], Vel[0][1], 0]
        vp += Point(rsp, c="r", r=5, alpha=0.1)  # leave a point trace
        vp.show()  # render scene
    pb.print()

vp.show(interactive=1)
Example #2
0
"""Use iminuit to find the minimum of a 3D scalar field"""
from vedo import show, Point, Line, printc
from iminuit import Minuit
# pip install iminuit  # https://github.com/scikit-hep/iminuit
import numpy as np


def fcn(x, y, z):
    f = (x - 4)**4 + (y - 3)**4 + (z - 2)**2
    if not vals or f < vals[-1]:
        path.append([x, y, z])
        vals.append(f)
    return f


paths = []
for x, y, z in np.random.rand(200, 3) * 3:
    path, vals = [], []
    m = Minuit(fcn, x=x, y=y, z=z)
    m.errordef = m.LEAST_SQUARES

    # m.simplex()  # run simplex optimiser
    m.migrad()  # run migrad optimiser

    line = Line(path).cmap('jet_r', vals).lw(2).alpha(0.25)
    paths.append(line)

printc('Last optimization output:', c='green7', invert=1)
printc(m, c='green7', italic=1)
show(paths, Point([4, 3, 2]), __doc__, axes=1)
Example #3
0
"""
Example usage of addTrail().
Add a trailing line to a moving object.
"""
print(__doc__)
from vedo import Plotter, sin, Sphere, Point

vp = Plotter(axes=6, interactive=False)

s = Sphere().c("green").bc("tomato")
s.cutWithPlane([-0.9, 0, 0])  # cut left part of sphere

p = Point([1, 1, 1], r=12, c="black")

# add a trail to point p with max length 0.5 and 50 segments
p.addTrail(lw=3, maxlength=0.5, n=50)

# add meshes to Plotter list
vp += [s, p]

for i in range(200):
    p.pos(-2 + i / 100.0, sin(i / 5.0) / 15, 0)
    vp.camera.Azimuth(-0.2)
    vp.show()

vp.show(interactive=True)
Example #4
0
    y_eu, v_eu = euler(y_eu, v_eu, t, dt)
    y_rk, v_rk = rk4(y_rk, v_rk, t, dt)
    t += dt
    positions_eu.append(y_eu)  # store result of integration
    positions_rk.append(y_rk)
    pb.print("Integrate: RK-4 and Euler")

####################################################
# Visualize the result
####################################################
vp = Plotter(interactive=0, axes=2)  # choose axes type nr.2
vp.ytitle = "u(x,t)"
vp.ztitle = ""  # will not draw z axis

for i in x:
    vp += Point([i, 0, 0], c="green", r=6)
pts_actors_eu = vp.actors  # save a copy of the actors list
pts_actors_eu[0].legend = "Euler method"

vp.actors = []  # clean up the list

for i in x:
    vp += Point([i, 0, 0], c="red", r=6)
pts_actors_rk = vp.actors  # save a copy of the actors list
pts_actors_rk[0].legend = "Runge-Kutta4"

# merge the two lists and set it as the current actors
vp.actors = pts_actors_eu + pts_actors_rk

# let's also add a fancy background image from wikipedia
vp.load(datadir + "images/wave_wiki.png",
Example #5
0
    y_rk, v_rk = rk4(y_rk, v_rk, t, dt)
    t += dt
    positions_eu.append(y_eu)  # store result of integration
    positions_rk.append(y_rk)
    pb.print("Integrate: RK-4 and Euler")

####################################################
# Visualize the result
####################################################
settings.useDepthPeeling = False
plt = Plotter(interactive=0, axes=2)  # choose axes type nr.2
plt.ytitle = "u(x,t)"
plt.ztitle = ""  # will not draw z axis

for i in x:
    plt += Point([i, 0, 0], c="green", r=6)
pts_actors_eu = plt.actors  # save a copy of the actors list
pts_actors_eu[0].legend = "Euler method"

plt.actors = []  # clean up the list

for i in x:
    plt += Point([i, 0, 0], c="red", r=6)
pts_actors_rk = plt.actors  # save a copy of the actors list
pts_actors_rk[0].legend = "Runge-Kutta4"

# merge the two lists and set it as the current actors
plt.actors = pts_actors_eu + pts_actors_rk

# let's also add a fancy background image from wikipedia
plt.load(dataurl + "images/wave_wiki.png").alpha(0.8).scale(0.4).pos(
Example #6
0
allLatIdx, allLatCoord, allLatVal = utils.mapSamps(mapIdx, mapCoord,
                                                   OrigLatCoords, OrigLatVals)

M = len(allLatIdx)

# For colorbar ranges
MINLAT = math.floor(min(allLatVal) / 10) * 10
MAXLAT = math.ceil(max(allLatVal) / 10) * 10

allPoints = Points(allLatCoord, r=10).cmap('gist_rainbow',
                                           allLatVal,
                                           vmin=MINLAT,
                                           vmax=MAXLAT).addScalarBar(c='white')

vplt = Plotter(N=1, axes=0, interactive=True)
for i in range(M):
    idx = allLatIdx[i]
    coord = allLatCoord[i]
    val = allLatVal[i]

    # plot current point as larger than the others
    testPoint = Point(coord, r=20).cmap('gist_rainbow', [val],
                                        vmin=MINLAT,
                                        vmax=MAXLAT).addScalarBar(c='white')
    vplt.show(mesh,
              allPoints,
              testPoint,
              title='i={:g}/{:g}'.format(i, M),
              bg='black')

vplt.close()
Example #7
0
File: lights.py Project: zhDai/vedo
"""Set custom lights to a 3D scene"""
from vedo import Plotter, load, dataurl, Point, Light, show

man = load(dataurl + 'man.vtk').c('white').lighting('glossy')

p1 = Point([1, 0, 1], c='y')
p2 = Point([0, 0, 2], c='r')
p3 = Point([-1, -0.5, -1], c='b')
p4 = Point([0, 1, 0], c='k')

# Add light sources at the given positions
l1 = Light(p1, c='y')  # p1 can simply be [1,0,1]
l2 = Light(p2, c='r')
l3 = Light(p3, c='b')
l4 = Light(p4, c='w', intensity=0.5)

show(man, l1, l2, l3, l4, p1, p2, p3, p4, __doc__, axes=1, viewup='z')

#####################################################
##### Equivalent code using a Plotter instance: #####
#####################################################
# plt = Plotter(axes=1)
# plt += [man, p1, p2, p3, p4, l1, l2, l3, l4]
# plt.show(viewup='z')
#####################################################