def run(self): # self.display.string += 'File' + '\n' f = DataFile() DataFile.open(f,self.path+self.filename) varlist = f.list() self.display.string += 'Variables in file :' + '\t' + 'No. of Dims.' + '\n' for i in varlist: self.display.string += str(i)+ '\t' + str(f.ndims(i))+ '\n' f.close()
import numpy as np from boututils import DataFile from boutdata import collect # Load in the datafile as a BOUT DataFile object # path links to the directory containing the data # file1 is the first file in this subdirectory (NB requires / at start) path = '/hwdisks/home/nrw504/BOUT/blobsims/3DMASTmodel/RUNS/RUN13' file1 = '/BOUT.dmp.0.nc' f = DataFile() print 'Opening file: ' + path + file1 DataFile.open(f, path + file1) # Calculates the ranges of time, x, y and z in the dataset # See BOUT manual p.27 t = [int(np.min(f.read('t_array'))), int(np.max(f.read('t_array')))] nx = f.read('NXPE') * f.read('MXSUB') + 2*f.read('MXG') ny = f.read('NYPE') * f.read('MYSUB') + 2*f.read('MYG') nz = f.read('MZ') # Function query_yes_no released under MIT license # http://code.activestate.com/recipes/577058-query-yesno/ Accessed 6/8/2012 # Returns 'yes' if user responds 'y', 'no' if 'n', &c. def query_yes_no(question, default=None): """Ask a yes/no question via raw_input() and return their answer. "question" is a string that is presented to the user. "default" is the presumed answer if the user just hits <Enter>. It must be "yes" (the default), "no" or None (meaning
from past.utils import old_div from boututils import DataFile # Wrapper around NetCDF4 libraries from math import pow from sys import argv length = 80. # Length of the domain in m nx = 5 # Minimum is 5: 2 boundary, one evolved if len(argv)>1: ny = int(argv[1]) # Minimum 5. Should be divisible by number of processors (so powers of 2 nice) else: ny = 256 # Minimum 5. Should be divisible by number of processors (so powers of 2 nice) #dy = [[1.]*ny]*nx # distance between points in y, in m/g22/lengthunit g22 = [[pow(old_div(float(ny-1),length),2)]*ny]*nx g_22 = [[pow(old_div(length,float(ny-1)),2)]*ny]*nx ixseps1 = -1 ixseps2 = 0 f = DataFile() f.open("conduct_grid.nc", create=True) f.write("nx", nx) f.write("ny", ny) #f.write("dy", dy) f.write("g22",g22) f.write("g_22", g_22) f.write("ixseps1", ixseps1) f.write("ixseps2", ixseps2) f.close()
# Only one region yup_xsplit = [nx] ydown_xsplit = [nx] yup_xin = [0] yup_xout = [-1] ydown_xin = [0] ydown_xout = [-1] nrad = [nx] npol = [ny] ###################################################### print "Writing grid to file "+output of = DataFile() of.open(output, create=True) of.write("nx", nx) of.write("ny", ny) # Topology for original scheme of.write("ixseps1", ixseps1) of.write("ixseps2", ixseps2) of.write("jyseps1_1", jyseps1_1) of.write("jyseps1_2", jyseps1_2) of.write("jyseps2_1", jyseps2_1) of.write("jyseps2_2", jyseps2_2) of.write("ny_inner", ny_inner) # Grid spacing of.write("dx", dx)
# Only one region yup_xsplit = [nx] ydown_xsplit = [nx] yup_xin = [0] yup_xout = [-1] ydown_xin = [0] ydown_xout = [-1] nrad = [nx] npol = [ny] ###################################################### print("Writing grid to file "+output) of = DataFile() of.open(output, create=True) of.write("nx", nx) of.write("ny", ny) # Topology for original scheme of.write("ixseps1", ixseps1) of.write("ixseps2", ixseps2) of.write("jyseps1_1", jyseps1_1) of.write("jyseps1_2", jyseps1_2) of.write("jyseps2_1", jyseps2_1) of.write("jyseps2_2", jyseps2_2) of.write("ny_inner", ny_inner) # Grid spacing of.write("dx", dx)
def generate(nx, ny, R = 2.0, r=0.2, # Major & minor radius dr=0.05, # Radial width of domain Bt=1.0, # Toroidal magnetic field q=5.0, # Safety factor mxg=2, file="circle.nc" ): # q = rBt / RBp Bp = r*Bt / (R*q) # Minor radius as function of x. Choose so boundary # is half-way between grid points h = dr / (nx - 2.*mxg) # Grid spacing in r rminor = linspace(r - 0.5*dr - (mxg-0.5)*h, r + 0.5*dr + (mxg-0.5)*h, nx) # mesh spacing in x and y dx = ndarray([nx,ny]) dx[:,:] = r*Bt*h # NOTE: dx is toroidal flux dy = ndarray([nx,ny]) dy[:,:] = 2.*pi / ny # LogB = log(1/(1+r/R cos(theta))) =(approx) -(r/R)*cos(theta) logB = zeros([nx, ny, 3]) # (constant, n=1 real, n=1 imag) # At y = 0, Rmaj = R + r*cos(theta) logB[:,0,1] = -(rminor/R) # Moving in y, phase shift by (toroidal angle) / q for y in range(1,ny): dtheta = y * 2.*pi / ny / q # Change in poloidal angle logB[:,y,1] = -(rminor/R)*cos(dtheta) logB[:,y,2] = -(rminor/R)*sin(dtheta) # Shift angle from one end of y to the other ShiftAngle = ndarray([nx]) ShiftAngle[:] = 2.*pi / q Rxy = ndarray([nx,ny]) Rxy[:,:] = r # NOTE : opposite to standard BOUT convention Btxy = ndarray([nx,ny]) Btxy[:,:] = Bp Bpxy = ndarray([nx,ny]) Bpxy[:,:] = Bt Bxy = ndarray([nx,ny]) Bxy[:,:] = sqrt(Bt**2 + Bp**2) hthe = ndarray([nx,ny]) hthe[:,:] = R print("Writing to file '"+file+"'") f = DataFile() f.open(file, create=True) # Mesh size f.write("nx", nx) f.write("ny", ny) # Mesh spacing f.write("dx", dx) f.write("dy", dy) # Metric components f.write("Rxy", Rxy) f.write("Btxy", Btxy) f.write("Bpxy", Bpxy) f.write("Bxy", Bxy) f.write("hthe", hthe) # Shift f.write("ShiftAngle", ShiftAngle); # Curvature f.write("logB", logB) # Input parameters f.write("R", R) f.write("r", r) f.write("dr", dr) f.write("Bt", Bt) f.write("q", q) f.write("mxg", mxg) f.close()
from math import pow from sys import argv length = 80. # Length of the domain in m nx = 5 # Minimum is 5: 2 boundary, one evolved if len(argv) > 1: ny = int( argv[1] ) # Minimum 5. Should be divisible by number of processors (so powers of 2 nice) else: ny = 256 # Minimum 5. Should be divisible by number of processors (so powers of 2 nice) #dy = [[1.]*ny]*nx # distance between points in y, in m/g22/lengthunit g22 = [[pow(old_div(float(ny - 1), length), 2)] * ny] * nx g_22 = [[pow(old_div(length, float(ny - 1)), 2)] * ny] * nx ixseps1 = -1 ixseps2 = 0 f = DataFile() f.open("conduct_grid.nc", create=True) f.write("nx", nx) f.write("ny", ny) #f.write("dy", dy) f.write("g22", g22) f.write("g_22", g_22) f.write("ixseps1", ixseps1) f.write("ixseps2", ixseps2) f.close()
def generate( nx, ny, R=2.0, r=0.2, # Major & minor radius dr=0.05, # Radial width of domain Bt=1.0, # Toroidal magnetic field q=5.0, # Safety factor mxg=2, file="circle.nc"): # q = rBt / RBp Bp = r * Bt / (R * q) # Minor radius as function of x. Choose so boundary # is half-way between grid points h = dr / (nx - 2. * mxg) # Grid spacing in r rminor = linspace(r - 0.5 * dr - (mxg - 0.5) * h, r + 0.5 * dr + (mxg - 0.5) * h, nx) # mesh spacing in x and y dx = ndarray([nx, ny]) dx[:, :] = r * Bt * h # NOTE: dx is toroidal flux dy = ndarray([nx, ny]) dy[:, :] = 2. * pi / ny # LogB = log(1/(1+r/R cos(theta))) =(approx) -(r/R)*cos(theta) logB = zeros([nx, ny, 3]) # (constant, n=1 real, n=1 imag) # At y = 0, Rmaj = R + r*cos(theta) logB[:, 0, 1] = -(rminor / R) # Moving in y, phase shift by (toroidal angle) / q for y in range(1, ny): dtheta = y * 2. * pi / ny / q # Change in poloidal angle logB[:, y, 1] = -(rminor / R) * cos(dtheta) logB[:, y, 2] = -(rminor / R) * sin(dtheta) # Shift angle from one end of y to the other ShiftAngle = ndarray([nx]) ShiftAngle[:] = 2. * pi / q Rxy = ndarray([nx, ny]) Rxy[:, :] = r # NOTE : opposite to standard BOUT convention Btxy = ndarray([nx, ny]) Btxy[:, :] = Bp Bpxy = ndarray([nx, ny]) Bpxy[:, :] = Bt Bxy = ndarray([nx, ny]) Bxy[:, :] = sqrt(Bt**2 + Bp**2) hthe = ndarray([nx, ny]) hthe[:, :] = R print("Writing to file '" + file + "'") f = DataFile() f.open(file, create=True) # Mesh size f.write("nx", nx) f.write("ny", ny) # Mesh spacing f.write("dx", dx) f.write("dy", dy) # Metric components f.write("Rxy", Rxy) f.write("Btxy", Btxy) f.write("Bpxy", Bpxy) f.write("Bxy", Bxy) f.write("hthe", hthe) # Shift f.write("ShiftAngle", ShiftAngle) # Curvature f.write("logB", logB) # Input parameters f.write("R", R) f.write("r", r) f.write("dr", dr) f.write("Bt", Bt) f.write("q", q) f.write("mxg", mxg) f.close()
#!/usr/bin/env python # # Generate an input mesh # from boututils import DataFile # Wrapper around NetCDF4 libraries nx = 5 # Minimum is 5: 2 boundary, one evolved ny = 32 # Minimum 5. Should be divisible by number of processors (so powers of 2 nice) dy = 1. # distance between points in y, in m/g22/lengthunit ixseps1 = -1 ixseps2 = -1 f = DataFile() f.open("test-staggered.nc", create=True) f.write("nx", nx) f.write("ny", ny) f.write("dy", dy) f.write("ixseps1", ixseps1) f.write("ixseps2", ixseps2) f.close()
for ix in range(nx): for jy in range(ny): a[ix, jy] = -1.0 # === Print normalization information === # print '==============================' print ' n0 = ', n0 print ' v0 = ', v0 print '==============================' # ============== Write to grid file ============ # f=DataFile() f.open("slab.grd.nc",create=True) f.write("nx" ,nx) #write the value of nx to the element of name "nx" in f f.write("ny", ny) # == auxiliary quantities == # f.write("dx", dx) f.write("a", a) # == normalization quantities == # f.write("n0", n0) f.write("v0", v0)