def write_vtk(self, name='trilinear', gpts=None): """ Write data into vtk file with given number of grid points. Parameters: gpts - number of grid points to all directions. Default is the number of DFT grid points. """ if gpts == None: gpts = self.n R = [np.linspace(0, self.grids[i][-1], gpts[i]) for i in range(3)] nx, ny, nz = (gpts[0], gpts[1], gpts[2]) of = open('%s.vtk' % name, 'w') print("# vtk DataFile Version 2.0", file=of) print("Trilinear interpolation", file=of) print("ASCII", file=of) print("DATASET RECTILINEAR_GRID", file=of) print("DIMENSIONS %i %i %i" % (nx, ny, nz), file=of) print("X_COORDINATES %i double" % nx, file=of) print(mix.a2s(R[0]), file=of) print("Y_COORDINATES %i double" % ny, file=of) print(mix.a2s(R[1]), file=of) print("Z_COORDINATES %i double" % nz, file=of) print(mix.a2s(R[2]), file=of) print("POINT_DATA %i" % (nx * ny * nz), file=of) print("SCALARS data double", file=of) print("LOOKUP_TABLE default", file=of) for k in range(nz): for j in range(ny): for i in range(nx): r = vec([R[0][i], R[1][j], R[2][k]]) print(self(r), file=of) of.close()
def atoms_vtk(atoms, scalars={}, vectors={}, filename=None): ''' vtk output of atoms @param filename: vtk output file name @param atoms: atoms object @param scalars: dictionary of atoms' scalar properties @param vectors: dictionary of atoms' vector properties ''' if filename == None: filename = atoms.get_name() + '.vtk' N = len(atoms) f = open(filename, 'w') f.write('# vtk DataFile Version 2.0 \nAtoms %s\n' % atoms.get_name()) f.write('ASCII \nDATASET UNSTRUCTURED_GRID\n') f.write('POINTS %i double \n ' % N) fmt = '%20.14f' #output format for floats # Point data (atom coordinates) and cell data (bonds) for r in atoms.get_positions(): f.write('%s\n' % mix.a2s(r, fmt=fmt)) # First the data related to atoms f.write('POINT_DATA %i\n' % N) for scalar in scalars: print('SCALARS %s double 1\nLOOKUP_TABLE default' % scalar, file=f) for value in scalars[scalar]: print('%12.6f' % (value * 1.0), file=f) for vector in vectors: print('VECTORS %s double\n' % vector, file=f) for value in properties: print(mix.a2s(value, fmt=fmt), file=f) f.close()
def rectilinear_vtk(grid, data, fname): """ Write data in rectilinear grid into .vtk file. parameters: ----------- grid: grid[:,{0,1,2}] x-, y-, and z-grids data: data on this regular grid. fname: output file name """ f = open(fname, 'w') nx, ny, nz = len(grid[0][:]), len(grid[1][:]), len(grid[2][:]) print("# vtk DataFile Version 2.0", file=f) print("...some rectilinear grid data.", file=f) print("ASCII", file=f) print("DATASET RECTILINEAR_GRID", file=f) print("DIMENSIONS %i %i %i" % (nx, ny, nz), file=f) print("X_COORDINATES %i double" % nx, file=f) print(mix.a2s(grid[0][:]), file=f) print("Y_COORDINATES %i double" % ny, file=f) print(mix.a2s(grid[1][:]), file=f) print("Z_COORDINATES %i double" % nz, file=f) print(mix.a2s(grid[2][:]), file=f) print("POINT_DATA %i" % (nx * ny * nz), file=f) print("SCALARS some_data double", file=f) print("LOOKUP_TABLE default", file=f) for k in range(nz): for j in range(ny): for i in range(nx): print(data[i, j, k], file=f) print('min ... max=', min(data.flatten()), '...', max(data.flatten())) f.close()
def atoms_vtk(atoms,scalars={},vectors={},filename=None): ''' vtk output of atoms @param filename: vtk output file name @param atoms: atoms object @param scalars: dictionary of atoms' scalar properties @param vectors: dictionary of atoms' vector properties ''' if filename==None: filename=atoms.get_name()+'.vtk' N=len(atoms) f=open(filename,'w') f.write('# vtk DataFile Version 2.0 \nAtoms %s\n' %atoms.get_name()) f.write('ASCII \nDATASET UNSTRUCTURED_GRID\n') f.write('POINTS %i double \n ' %N) fmt='%20.14f' #output format for floats # Point data (atom coordinates) and cell data (bonds) for r in atoms.get_positions(): f.write('%s\n' %mix.a2s(r,fmt=fmt)) # First the data related to atoms f.write('POINT_DATA %i\n' %N) for scalar in scalars: print>>f, 'SCALARS %s double 1\nLOOKUP_TABLE default' %scalar for value in scalars[scalar]: print>>f, '%12.6f' %(value*1.0) for vector in vectors: print>>f, 'VECTORS %s double\n' %vector for value in properties: print>>f, mix.a2s(value,fmt=fmt) f.close()
def rectilinear_vtk(grid,data,fname): """ Write data in rectilinear grid into .vtk file. parameters: ----------- grid: grid[:,{0,1,2}] x-, y-, and z-grids data: data on this regular grid. fname: output file name """ f=open(fname,'w') nx, ny, nz=len(grid[0][:]), len(grid[1][:]), len(grid[2][:]) print>>f,"# vtk DataFile Version 2.0" print>>f,"...some rectilinear grid data." print>>f,"ASCII" print>>f,"DATASET RECTILINEAR_GRID" print>>f,"DIMENSIONS %i %i %i" %(nx,ny,nz) print>>f,"X_COORDINATES %i double" %nx print>>f,mix.a2s(grid[0][:]) print>>f,"Y_COORDINATES %i double" %ny print>>f,mix.a2s(grid[1][:]) print>>f,"Z_COORDINATES %i double" %nz print>>f,mix.a2s(grid[2][:]) print>>f,"POINT_DATA %i" %(nx*ny*nz) print>>f,"SCALARS some_data double" print>>f,"LOOKUP_TABLE default" for k in range(nz): for j in range(ny): for i in range(nx): print>>f, data[i,j,k] print 'min ... max=',min(data.flatten()),'...',max(data.flatten()) f.close()
def write_vtk(self, file=None): """ vtk output of atoms (all scalar and vector properties) """ if file == None: file = self.get_name() + '.vtk' N = len(self) f = open(file, 'w') f.write('# vtk DataFile Version 2.0 \nAtoms %s\n' % self.get_name()) f.write('ASCII \nDATASET UNSTRUCTURED_GRID\n') f.write('POINTS %i double \n ' % N) fmt = '%20.14f' #output format for floats # Point data (atom coordinates) and cell data (bonds) if self.get('nbonds') == None: self.construct_bonds() nb = self.get('nbonds') bonds = self.get('bonds') for r in self.get_positions(): f.write('%s\n' % mix.a2s(r, fmt=fmt)) f.write('CELLS %i %i\n' % (nb, 3 * nb)) for bond in bonds: f.write('2 %i %i\n' % (bond['i'], bond['j'])) f.write('CELL_TYPES %i\n' % nb) for bond in bonds: f.write('3\n') # First the data related to atoms f.write('POINT_DATA %i\n' % N) self.set('velocities', self.get_velocities()) for property in self.list_properties(): properties = self.get(property) try: tp = type(properties[0]) except: continue if tp == type(vec([])) or tp == type([]): if not isinstance(properties[0], (int, float)): continue print('VECTORS %s double\n' % property, file=f) for value in properties: print(mix.a2s(value, fmt=fmt), file=f) else: try: x = float(properties[0]) except: continue print('SCALARS %s double 1\nLOOKUP_TABLE default' % property, file=f) for value in properties: print('%12.6f' % (value * 1.0), file=f) # Then data related to bonds print('CELL_DATA %i' % nb, file=f) print('SCALARS bond_length double 1\nLOOKUP_TABLE default', file=f) for bond in bonds: f.write('%f\n' % bond['length']) f.close()
def write_vtk(self,file=None): """ vtk output of atoms (all scalar and vector properties) """ if file==None: file=self.get_name()+'.vtk' N=len(self) f=open(file,'w') f.write('# vtk DataFile Version 2.0 \nAtoms %s\n' %self.get_name()) f.write('ASCII \nDATASET UNSTRUCTURED_GRID\n') f.write('POINTS %i double \n ' %N) fmt='%20.14f' #output format for floats # Point data (atom coordinates) and cell data (bonds) if self.get('nbonds')==None: self.construct_bonds() nb=self.get('nbonds') bonds=self.get('bonds') for r in self.get_positions(): f.write('%s\n' %mix.a2s(r,fmt=fmt)) f.write('CELLS %i %i\n' %(nb,3*nb)) for bond in bonds: f.write( '2 %i %i\n' %(bond['i'],bond['j']) ) f.write('CELL_TYPES %i\n' %nb) for bond in bonds: f.write('3\n') # First the data related to atoms f.write('POINT_DATA %i\n' %N) self.set('velocities',self.get_velocities()) for property in self.list_properties(): properties=self.get(property) try: tp=type(properties[0]) except: continue if tp==type(vec([])) or tp==type([]): if not isinstance(properties[0],(int,float)): continue print>>f, 'VECTORS %s double\n' %property for value in properties: print>>f, mix.a2s(value,fmt=fmt) else: try: x=float(properties[0]) except: continue print>>f, 'SCALARS %s double 1\nLOOKUP_TABLE default' %property for value in properties: print>>f, '%12.6f' %(value*1.0) # Then data related to bonds print>>f, 'CELL_DATA %i' %nb print>>f, 'SCALARS bond_length double 1\nLOOKUP_TABLE default' for bond in bonds: f.write( '%f\n' %bond['length'] ) f.close()
def write_vtk(self,name='trilinear',gpts=None): """ Write data into vtk file with given number of grid points. Parameters: gpts - number of grid points to all directions. Default is the number of DFT grid points. """ if gpts==None: gpts=self.n R=[np.linspace(0,self.grids[i][-1],gpts[i]) for i in range(3)] nx,ny,nz=(gpts[0],gpts[1],gpts[2]) of=open('%s.vtk' %name,'w') print>>of,"# vtk DataFile Version 2.0" print>>of,"Trilinear interpolation" print>>of,"ASCII" print>>of,"DATASET RECTILINEAR_GRID" print>>of,"DIMENSIONS %i %i %i" %(nx,ny,nz) print>>of,"X_COORDINATES %i double" %nx print>>of,mix.a2s(R[0]) print>>of,"Y_COORDINATES %i double" %ny print>>of,mix.a2s(R[1]) print>>of,"Z_COORDINATES %i double" %nz print>>of,mix.a2s(R[2]) print>>of,"POINT_DATA %i" %(nx*ny*nz) print>>of,"SCALARS data double" print>>of,"LOOKUP_TABLE default" for k in range(nz): for j in range(ny): for i in range(nx): r=vec([R[0][i],R[1][j],R[2][k]]) print>>of, self(r) of.close()