示例#1
0
def main():
    filename = ''
    flag_line = 0
    step = -1
    flag_com = 0
    x0 = 0.0
    y0 = 0.0
    z0 = 0.0
    i = 1
    while i < len(sys.argv):
        if sys.argv[i] == '-f' or sys.argv[i] == '--file':
            filename = sys.argv[i+1]
            i += 2
        elif sys.argv[i] == '-line':
            flag_line = 1
            i += 1
        elif sys.argv[i] == '-step':
            step = int(sys.argv[i+1])
            step -= 1
            i += 2
        elif sys.argv[i] == '-com':
            flag_com = 1
            x0 = float(sys.argv[i+1])
            y0 = float(sys.argv[i+2])
            z0 = float(sys.argv[i+3])
            i += 4
        else:
            usage()
    if filename == '': usage()

    nc = stokes.stokes_nc_open (filename)

    # pos[] : center of particles
    pos = stokes.darray(nc.np  * nc.nvec)

    # q[] : quaternion
    if nc.flag_q != 0:
        q  = stokes.darray(nc.np  * nc.nquat)
    else:
        q = []

    # lattice
    lattice = stokes.darray(3)
    stokes.stokes_nc_get_array1d (nc, 'l', lattice)

    # extract the config at the step
    if step >= 0:
        if step > nc.ntime:
            print 'out of the range %d <= %d'%(step, nc.ntime)
            sys.exit(1)

        # read the config at the step
        t = stokes.stokes_nc_get_time_step (nc, step)
        stokes.stokes_nc_get_data (nc, "x", step, pos)
        if nc.flag_q != 0:
            stokes.stokes_nc_get_data (nc, "q", step, q)

        comx = 0.0
        comy = 0.0
        comz = 0.0
        if flag_com != 0:
            for i in range(nc.np):
                comx += pos[i*3]
                comy += pos[i*3+1]
                comz += pos[i*3+2]
            comx /= float(nc.np)
            comy /= float(nc.np)
            comz /= float(nc.np)

        # print the config
        # print arguments as a comment
        print '; config at %d step of %s, generated by'%(step+1, filename)
        print ';  ',
        for i in range(len(sys.argv)):
            print '%s'%(sys.argv[i]),
        print ''
        print '; at time %f'%(t)
        print '(define x #('
        for i in range(nc.np):
            print '  %f %f %f ; %d'%(pos[i*3]   - comx + x0,
                                     pos[i*3+1] - comy + y0,
                                     pos[i*3+2] - comz + z0,
                                     i)
        print '))'

        if nc.flag_q != 0:
            stokes.stokes_nc_get_data (nc, "q", step, q)
            print '(define q #('
            for i in range(nc.np):
                print '  %f %f %f %f ; %d'\
                      %(q[i*4],q[i*4+1],q[i*4+2],q[i*4+3],i)
            print '))'
        sys.exit(1)
        # done!

    if flag_line == 0:
        # print some general informations
        stokes.stokes_nc_print_actives(nc, stokes.get_stdout())
        print ''
        print 'ntime = %d'%(nc.ntime)
        print ''

        # imposed flows
        if nc.flag_ui0 == 1:
            ui0 = stokes.darray(3)
            stokes.stokes_nc_get_array1d (nc, "Ui0", ui0)
            print 'ui0 = %f %f %f'%(ui0[0], ui0[1], ui0[2])
        if nc.flag_oi0 == 1:
            oi0 = stokes.darray(3)
            stokes.stokes_nc_get_array1d (nc, "Oi0", oi0)
            print 'oi0 = %f %f %f'%(oi0[0], oi0[1], oi0[2])
        if nc.flag_ei0 == 1:
            ei0 = stokes.darray(5)
            stokes.stokes_nc_get_array1d (nc, "Ei0", ei0)
            print 'ei0 = %f %f %f %f %f'\
                  %(ei0[0], ei0[1], ei0[2], ei0[3], ei0[4])
        print ''

        print 'lattice %f %f %f\n'%(lattice[0], lattice[1], lattice[2])

    # print the infos about the output format
    if flag_line == 0:
        if nc.flag_q != 0:
            print '# t, i, x, y, z, q1, q2, q3, q4'
        else:
            print '# t, i, x, y, z'
    else:
        if nc.flag_q != 0:
            print '# t, x, y, z, q1, q2, q3, q4 (for particle 0),'\
                  ' ... upto particle %d'%(nc.np)
        else:
            print '# t, x, y, z (for particle 0), x, y, z (for 1),'\
                  ' ... upto particle %d'%(nc.np)

    for i in range(nc.ntime):
        t = stokes.stokes_nc_get_time_step (nc, i)

        stokes.stokes_nc_get_data (nc, "x", i, pos)
        if nc.flag_q != 0:
            stokes.stokes_nc_get_data (nc, "q", i, q)

        if flag_line == 0:
            for j in range(nc.np):
                x = pos[j*3]
                y = pos[j*3+1]
                z = pos[j*3+2]
                if nc.flag_q != 0:
                    print '%f %d %f %f %f %f %f %f %f'\
                          %(t, j, x, y, z, q[0], q[1], q[2], q[3])
                else:
                    print '%f %d %f %f %f'%(t, j, x, y, z)
        else:
            print t,
            for j in range(nc.np):
                x = pos[j*3]
                y = pos[j*3+1]
                z = pos[j*3+2]
                print x, y, z,
                if nc.flag_q != 0:
                    print q[0], q[1], q[2], q[3],
            print ''
示例#2
0
def main():
    filename = ''
    nstep = 1
    flag_bond = 0
    flag_step = 0
    flag_bottom = 0
    flag_pic = 0
    flag_cylinder = 0
    flag_sphere = 0
    flag_dumbbell = 0
    flag_hex2d = 0
    i = 1
    while i < len(sys.argv):
        if sys.argv[i] == '-f' or sys.argv[i] == '--file':
            filename = sys.argv[i+1]
            i += 2
        elif sys.argv[i] == '-step':
            nstep = int(sys.argv[i+1])
            i += 2
        elif sys.argv[i] == '-b' or sys.argv[i] == '--bond':
            flag_bond = 1
            i += 1
        elif sys.argv[i] == '-s':
            flag_step = 1
            i += 1
        elif sys.argv[i] == '-ts':
            flag_step = 2
            i += 1
        elif sys.argv[i] == '-bottom':
            flag_bottom = 1
            i += 1
        elif sys.argv[i] == '-p' or sys.argv[i] == '--pic':
            flag_pic = 1
            i += 1
        elif sys.argv[i] == '-c' or sys.argv[i] == '--cylinder':
            flag_cylinder = 1
            cylinder_radius = float(sys.argv[i+1])
            i += 2
        elif sys.argv[i] == '--sphere':
            flag_sphere = 1
            cavity_radius = float(sys.argv[i+1])
            i += 2
        elif sys.argv[i] == '-d' or sys.argv[i] == '--dumbbell':
            flag_dumbbell = 1
            cavity1_radius = float(sys.argv[i+1])
            cavity2_radius = float(sys.argv[i+2])
            cylinder_length = float(sys.argv[i+3])
            cylinder_radius = float(sys.argv[i+4])
            i += 5
        elif sys.argv[i] == '--hex2d':
            flag_hex2d = 1
            cavity_radius = float(sys.argv[i+1])
            cylinder_radius = float(sys.argv[i+2])
            lattice_spacing = float(sys.argv[i+3])
            i += 4
        else:
            usage()
    if filename == '' : usage()

    nc = stokes.stokes_nc_open (filename)
    #stokes.stokes_nc_print_actives(nc, stokes.get_stdout())
    print 'ntime = ', nc.ntime
    lattice = stokes.darray(3)
    stokes.stokes_nc_get_array1d (nc, 'l', lattice)
    print 'lattice = ', lattice[0], lattice[1], lattice[2]

    # x[] : center of particles
    x  = stokes.darray(nc.np  * nc.nvec)
    # q[] : quaternion
    if nc.flag_q != 0:
        q  = stokes.darray(nc.np  * nc.nquat)
    else:
        q = []
    # a[] : radius of mobile particles
    if nc.flag_a != 0:
        a = stokes.darray(nc.np)
        stokes.stokes_nc_get_array1d (nc, "a", a)
    else:
        a = []
    # af[] : radius of fixed particles
    if nc.flag_af != 0:
        af = stokes.darray(nc.npf)
        stokes.stokes_nc_get_array1d (nc, "af", af)
    else:
        af = []
    
    # fixed particles
    if nc.npf > 0:
        xf0  = stokes.darray(nc.npf * nc.nvec)
        stokes.stokes_nc_get_data0 (nc, "xf0", xf0)

        if lattice[0] == 0.0 and lattice[1] == 0.0 and lattice[2] == 0.0:
            # non-periodic boundary
            pfData = ryuon_vtk.make_pData(nc.npf, xf0, af)
            (pfActor,pfGlyph) = ryuon_vtk.make_pActor ((1,0,0), 1, 20)
        else:
            # periodic boundary
            pfData = ryuon_vtk.make_pData_periodic(nc.npf, xf0, af,
                                               lattice)
            (pfActor,pfGlyph) = ryuon_vtk.make_pActor_periodic ((1,0,0),
                                                                1, 20,
                                                                lattice)
        pfGlyph.SetInput(pfData)

    # then, make Actor
    if nc.flag_q != 0:
        # with quaternion
        pActors = []
        for i in range (nc.np):
            pActors.append(ryuon_vtk.make_pActor_with_quaternion())
    else:
        # no quaternion in the result
        (pActor,pGlyph) = ryuon_vtk.make_pActor((1,1,1), 1, 8)
        # pGlyph.SetInput(pData)

    # bond Actor
    if flag_bond != 0:
        (bondActor,bond) = ryuon_vtk.make_bondActor (khaki)


    ## prepare renderer
    ren = vtk.vtkRenderer()
    win = vtk.vtkRenderWindow()
    win.AddRenderer(ren)

    ren.SetBackground (0.1, 0.2, 0.4)
    win.SetSize(480,360)
    # iren = vtk.vtkRenderWindowInteractor ()
    # iren.SetRenderWindow(win)

    if nc.flag_q != 0:
        for i in range(nc.np):
            ren.AddActor(pActors[i])
    else:
        ren.AddActor(pActor)
    if nc.npf > 0:
        ren.AddActor(pfActor)
    if flag_bond != 0:
        ren.AddActor(bondActor)

    # periodic box
    if lattice[0] != 0.0 or lattice[1] != 0.0 or lattice[2] != 0.0:
        CubeEdges = ryuon_vtk.make_cubeActor (lattice)
        ren.AddActor(CubeEdges)

    # axes
    #axesActor = ryuon_vtk.make_axesActor()
    #ren.AddActor(axesActor)

    # text
    textActor = ryuon_vtk.make_textActor()
    ren.AddActor(textActor)

    # cylinder
    if flag_cylinder != 0:
        cActor = make_cylinderActor (r=cylinder_radius,
                                     x0=(0.0, 0.0, 0.0),
                                     x1=(200.0, 0.0, 0.0),
                                     rgb=(0.0, 0.5, 0.0),
                                     opacity=0.3)
        ren.AddActor(cActor)

    # sphere
    if flag_sphere != 0:
        cavActor = make_sphereActor (x=(0.0, 0.0, 0.0),
                                      r=cavity_radius,
                                      rgb=(0.0, 0.5, 0.0),
                                      opacity=0.3)
        ren.AddActor(cavActor)

    # dumbbell
    if flag_dumbbell != 0:
        cActor = make_cylinderActor (r=cylinder_radius,
                                     x0=(-0.5 * cylinder_length, 0.0, 0.0),
                                     x1=(+0.5 * cylinder_length, 0.0, 0.0),
                                     rgb=(0.0, 0.5, 0.0),
                                     opacity=0.3)
        ren.AddActor(cActor)

        theta1 = math.asin(cylinder_radius / cavity1_radius)
        cav1Actor = make_sphereActor (x=(-0.5 * cylinder_length\
                                         -cavity1_radius * math.cos(theta1),
                                         0.0, 0.0),
                                      r=cavity1_radius,
                                      rgb=(0.0, 0.5, 0.0),
                                      opacity=0.3)
        ren.AddActor(cav1Actor)
        
        theta2 = math.asin(cylinder_radius / cavity2_radius)
        cav2Actor = make_sphereActor (x=(+0.5 * cylinder_length\
                                         +cavity2_radius * math.cos(theta2),
                                         0.0, 0.0),
                                      r=cavity2_radius,
                                      rgb=(0.0, 0.5, 0.0),
                                      opacity=0.3)
        ren.AddActor(cav2Actor)
        
    # hex2d
    if flag_hex2d != 0:
        cav1Actor = make_sphereActor (x=(0.0, 0.0, 0.0),
                                      r=cavity_radius,
                                      rgb=(0.0, 0.5, 0.0),
                                      opacity=0.3)
        ren.AddActor(cav1Actor)
        
        cav2Actor = make_sphereActor (x=(lattice_spacing, 0.0, 0.0),
                                      r=cavity_radius,
                                      rgb=(0.0, 0.5, 0.0),
                                      opacity=0.3)
        ren.AddActor(cav2Actor)
        
        cav3Actor = make_sphereActor (x=(0.5*lattice_spacing, math.sqrt(3.0) * 0.5 *lattice_spacing, 0.0),
                                      r=cavity_radius,
                                      rgb=(0.0, 0.5, 0.0),
                                      opacity=0.3)
        ren.AddActor(cav3Actor)
        
    aCamera = vtk.vtkCamera()
    ren.SetActiveCamera (aCamera)

    if lattice[0] != 0.0 or lattice[1] != 0.0 or lattice[2] != 0.0:
        # periodic boundary
        if lattice[0] > lattice[2]:
            l = lattice[0]
        else:                  
            l = lattice[2]

        aCamera.SetFocalPoint (0.5*lattice[0], 0,      0.4*lattice[2])
        aCamera.SetPosition   (0.5*lattice[0], -1.6*l, 0.5*lattice[2])

    # loop 
    while 1:
        i = 0
        while i < nc.ntime:
            t = stokes.stokes_nc_get_time_step (nc, i)
            stokes.stokes_nc_get_data (nc, "x", i, x)
            if nc.flag_q != 0:
                # with quaternion
                for j in range (nc.np):
                    stokes.stokes_nc_get_data (nc, "q", i, q)
                    m = ryuon_vtk.Q2M (q[j*4+0],q[j*4+1],q[j*4+2],q[j*4+3])
                    trans = vtk.vtkTransform()
                    trans.SetMatrix((m[0],m[3],m[6], x[j*3+0],
                                     m[1],m[4],m[7], x[j*3+1],
                                     m[2],m[5],m[8], x[j*3+2],
                                     0.0, 0.0, 0.0,  1.0));
                    pActors[j].SetUserTransform(trans)
            else:
                # no quaternion in the result
                pData = ryuon_vtk.make_pData(nc.np, x, a)
                pGlyph.SetInput(pData)
    
            # update bond Actor
            if flag_bond != 0:
                bData = ryuon_vtk.make_bData(nc.np, x)
                bond.SetInput(bData)

            if lattice[0] == 0.0 and lattice[1] == 0.0 and lattice[2] == 0.0:
                # non-periodic boundary
                #(cx,cy,cz, lx,ly,lz) = bounding_box (nc.np, x)
                (cx,cy,cz, lx0,ly0,lz0, lx1, ly1, lz1)\
                           = ryuon_vtk.bounding_box_ (nc.np, x)
                lx = lx1 - lx0
                ly = ly1 - ly0
                lz = lz1 - lz0
                # centered by COM
                if lx > lz:
                    l = lx
                else:                  
                    l = lz
                # prevent to go far away
                #if l > 50: l = 50
                if l > 100: l = 100
    
                if flag_bottom == 0:
                    aCamera.SetFocalPoint (cx, cy,       cz)
                    aCamera.SetPosition   (cx, cy-3.0*l, cz+0.1*l)
                else:
                    # bottom align
                    aCamera.SetFocalPoint (cx, cy,       lz0)
                    aCamera.SetPosition   (cx, cy-3.0*l, lz0+0.1*l)

            if flag_step == 0:
                textActor.SetInput ('time: %f'%(t))
            elif flag_step == 1:
                textActor.SetInput ('step: %d'%(i))
            else:
                textActor.SetInput ('time: %f (%d step)'%(t,i))
            win.Render()

            if flag_pic == 1:
                w2if = vtk.vtkWindowToImageFilter()
                w2if.SetInput(win)
                wr = vtk.vtkPNGWriter()
                wr.SetInput(w2if.GetOutput())
                wr.SetFileName('%s-%05d.png'%(filename,i))
                wr.Write()

            i += nstep
示例#3
0
def render_one_step(str_argv, outfile, i,
                    nc, pos, a, q, xf0, af, lattice,
                    flag_ball, flag_bonds, bond_radius, bond_color,
                    flag_lookat, lk_arg, camera_dist, camera_dir,
                    sphere_radius, cylinder_radius, flag_bb):
    file = '%s-%05d.pov'%(outfile, i)
    try:
        f = open(file, 'w')
    except IOError:
        print 'cannot open', file
        sys.exit()

    # write argv[0]
    f.write('/* generated for %d step by\n'%(i))
    f.write(' * %s\n'%(str_argv))
    f.write(' */\n')
    
    stokes.stokes_nc_get_data (nc, "x", i, pos)

    # set camera direction
    (cx,cy,cz, lx,ly,lz) = bounding_box (nc.np, pos)
    if flag_lookat == 0:
        if cylinder_radius > 0.0:
            # only x is adjustable
            lk = [cx, 0, 0]
        else:
            lk = [cx, cy, cz]
    else:
        lk = [lk_arg[0], lk_arg[1], lk_arg[2]]
    
    if camera_dir == 'top':
        # top view
        if camera_dist == 0.0:
            if lx > ly:
                l = lx
            else:                  
                l = ly
            # prevent to go too far away
            if l > 50: l = 50

            camera = [lk[0], lk[1], lk[2]+2*l]
        else:
            camera = [lk[0], lk[1], lk[2]+camera_dist]
    elif camera_dir == 'bottom':
        # bottom view
        if camera_dist == 0.0:
            if lx > ly:
                l = lx
            else:                  
                l = ly
            # prevent to go too far away
            if l > 50: l = 50
    
            camera = [lk[0], lk[1], lk[2]-2*l]
        else:
            camera = [lk[0], lk[1], lk[2]-camera_dist]
    else:
        # side view
        if camera_dist == 0.0:
            if lx > lz:
                l = lx
            else:                  
                l = lz
            # prevent to go too far away
            if l > 50: l = 50
    
            camera = [lk[0], lk[1]-2*l, lk[2]]
        else:
            camera = [lk[0], lk[1]-camera_dist, lk[2]]

    # write header part
    if lattice[0] == 0.0 and lattice[1] == 0.0 and lattice[2] == 0.0:
        # non-periodic boundary
        write_pov_header_open (f, lattice, camera, lk,
                               flag_ball, flag_bonds, bond_color)
    else:
        # periodic boundary
        #move_camera (camera, lk)
        write_pov_header (f, lattice, camera, lk,
                          flag_ball, flag_bonds, bond_color)

    if flag_bb != 0:
        # write bounding box for periodic system
        if lattice[0] != 0.0 or lattice[1] != 0.0 or lattice[2] != 0.0:
            f.write('box {\n')
            f.write('  <0, 0, 0>,\n')
            f.write('  <%f, %f, %f>\n'\
                    %(lattice[0]/100.0, lattice[2]/100.0, lattice[1]/100.0))
            f.write('  pigment {\n')
            f.write('    rgbf <.9,1,.9, .95>\n')
            f.write('  }\n')
            f.write('  finish {\n')
            f.write('    ambient .2\n')
            f.write('    diffuse .6\n')
            f.write('  }\n')
            f.write('}\n')

    # write confinement
    if sphere_radius > 0.0:
        # draw sphere
        f.write('sphere {\n')
        f.write('  <0, 0, 0>, %f\n'%(sphere_radius/100.0)) # scale factor 100
        f.write('  pigment {\n')
        f.write('    rgbf <.9,1,.9, .95>\n')
        f.write('  }\n')
        f.write('  finish {\n')
        f.write('    ambient .2\n')
        f.write('    diffuse .6\n')
        f.write('  }\n')
        f.write('}\n')

    if cylinder_radius > 0.0:
        # draw cylinder
        f.write('cylinder {\n')
        f.write('  <%f, 0, 0>,\n'%((cx-lx)*0.01)) # scale factor 0.01
        f.write('  <%f, 0, 0>,\n'%((cx+lx)*0.01)) # scale factor 0.01
        f.write('  %f\n'%(cylinder_radius*0.01)) # scale factor 0.01
        f.write('  pigment {\n')
        f.write('    rgbf <.9,1,.9, .95>\n')
        f.write('  }\n')
        f.write('  finish {\n')
        f.write('    ambient .2\n')
        f.write('    diffuse .6\n')
        f.write('  }\n')
        f.write('}\n')

    # write mobile particles
    if flag_bonds == 0:
        # no bond
        if nc.flag_q != 0:
            # with quaternion
            stokes.stokes_nc_get_data (nc, "q", i, q)
            for j in range(nc.np):
                x = pos[j*3]
                y = pos[j*3+1]
                z = pos[j*3+2]
                if a != []:
                    rad = a[j]
                else:
                    rad = 1.0
    
                if flag_ball == 0:
                    write_pov_particle_Q (f, x, y, z, rad,\
                                          [q[j*4+0],q[j*4+1],\
                                           q[j*4+2],q[j*4+3]])
                else:
                    write_pov_particle_Balls_Q (f, x, y, z, rad,\
                                                [q[j*4+0],q[j*4+1],\
                                                 q[j*4+2],q[j*4+3]],\
                                                j)
        else:
            # no quaternion
            for j in range(nc.np):
                x = pos[j*3]
                y = pos[j*3+1]
                z = pos[j*3+2]
                if a != []:
                    write_pov_particle (f, x, y, z, a[j])
                else:
                    write_pov_particle (f, x, y, z, 1.0)
    else:
        # bond
        write_pov_particles_with_bonds (f, nc.np, pos, a, bond_radius)

    # write fixed particles
    for j in range(nc.npf):
        x = xf0[j*3]
        y = xf0[j*3+1]
        z = xf0[j*3+2]
        if af != []:
            write_pov_particle_fixed (f, x, y, z, af[j])
        else:
            write_pov_particle_fixed (f, x, y, z, 1.0)

    # done
    f.close()