def generate_stubs(self): nx, ny = 11, 21 ratio = 3 # grid to be used as a mask self.grid = [numpy.random.random(nx), numpy.random.random(ny)] # create fields self.fine = Field(x=numpy.ones(nx * ratio**2), y=numpy.ones(ny * ratio**2), values=numpy.random.rand(ny * ratio**2, nx * ratio**2), label='fine') self.fine.x[::ratio**2] = self.grid[0][:] self.fine.y[::ratio**2] = self.grid[1][:] self.medium = Field(x=numpy.ones(nx * ratio), y=numpy.ones(ny * ratio), values=self.fine.values[::ratio, ::ratio] + 1.0, label='medium') self.medium.x[::ratio] = self.grid[0][:] self.medium.y[::ratio] = self.grid[1][:] self.coarse = Field(x=self.grid[0], y=self.grid[1], values=(self.fine.values[::ratio**2, ::ratio**2] + (1.0 + ratio)), label='coarse') self.coarse2 = Field( x=self.grid[0], y=self.grid[1], values=numpy.random.rand(*self.coarse.values.shape), label='coarse2') self.ratio = ratio
def __init__(self): x, y = numpy.linspace(0.0, 10.0, 9*4), numpy.linspace(-1.0, 1.0, 9*5) self.field = Field(x=x, y=y, time_step=0, values=numpy.random.rand(y.size, x.size), label='test') self.restriction() self.get_difference() self.subtract()
def get_validation_data(path, Re): """Gets the validation data. Parameters ---------- path: string Path of the file containing the validation data. Re: float Reynolds number of the simulation. Returns ------- d: 2-tuple of Field objects Contains stations and velocity values along center-lines (vertical and horizontal). """ Re = str(int(round(Re))) # column indices in file with experimental results cols = { '100': { 'u': 1, 'v': 7 }, '1000': { 'u': 2, 'v': 8 }, '3200': { 'u': 3, 'v': 9 }, '5000': { 'u': 4, 'v': 10 }, '10000': { 'u': 5, 'v': 11 } } with open(path, 'r') as infile: y, u, x, v = numpy.loadtxt(infile, dtype=float, usecols=(0, cols[Re]['u'], 6, cols[Re]['v']), unpack=True) return (Field(y=y, values=u, label='x-velocity'), Field(x=x, values=v, label='y-velocity'))
def test_three_grids(nx=11, ny=11, ratio=3, offset=0): """Computes the observed order of convergence using the solution on three consecutive grids with constant refinement ratio. The solution on the finest grid is random. The solution of the medium grid is the finest solution restricted and incremented by 1. The solution of the coarsest grid is the finest solution restricted and incremented by 1+ratio. Therefore, no matter the norm used, we expect an order of convergence of 1. Parameters ---------- nx, ny: integers, optional Number of grid-points along each direction on the grid used a mask to project the three solutions; default: 11, 11. ratio: integer, optional Grid refinement ratio; default: 3. offset: integer, optional Position of the coarsest grid relatively to the mask grid; default: 0 (the coarsest solution is defined on the mask grid) """ # grid used as mask grid = [numpy.random.random(nx), numpy.random.random(ny)] # create fields fine = Field(values=numpy.random.rand(ny*ratio**(offset+2), nx*ratio**(offset+2)), label='fine') medium = Field(values=fine.values[::ratio, ::ratio]+1.0, label='medium') coarse = Field(values=fine.values[::ratio**2, ::ratio**2]+(1.0+ratio), label='coarse') # fill nodal stations coarse.x, coarse.y = numpy.ones(nx*ratio**offset), numpy.ones(ny*ratio**offset) coarse.x[::ratio**offset], coarse.y[::ratio**offset] = grid[0][:], grid[1][:] medium.x, medium.y = numpy.ones(nx*ratio**(offset+1)), numpy.ones(ny*ratio**(offset+1)) medium.x[::ratio**(offset+1)], medium.y[::ratio**(offset+1)] = grid[0][:], grid[1][:] fine.x, fine.y = numpy.ones(nx*ratio**(offset+2)), numpy.ones(ny*ratio**(offset+2)) fine.x[::ratio**(offset+2)], fine.y[::ratio**(offset+2)] = grid[0][:], grid[1][:] # compute observed order of convergence p = convergence.get_observed_order(coarse, medium, fine, ratio, grid) assert p == 1.0 p = convergence.get_observed_order(coarse, medium, fine, ratio, grid, order=numpy.inf) assert p == 1.0
def test_same_grid(): """Computes the observed order of convergence using three solutions on the same grid. The first and last fields are a zero-solution; the middle solution is random. Therefore, no matter the norm used, we expect an order of convergence of 0. """ # create grid x, y = numpy.linspace(0.0, 1.0, 11), numpy.linspace(0.0, 10.0, 51) # create three fields on the same grid field1 = Field(x=x, y=y, time_step=0, label='field1', values=numpy.zeros((y.size, x.size))) field2 = Field(x=x, y=y, time_step=0, label='field2', values=numpy.random.rand(y.size, x.size)) field3 = Field(x=x, y=y, time_step=0, label='field3', values=numpy.zeros((y.size, x.size))) # compute observed order of convergence in L2-norm p = convergence.get_observed_order(field1, field2, field3, 3, [field1.x, field1.y]) assert p == 0.0 # compute observed order of convergence in Linf-norm p = convergence.get_observed_order(field1, field2, field3, 3, [field1.x, field1.y], order=numpy.inf) assert p == 0.0
class FieldTest(object): def __init__(self): x, y = numpy.linspace(0.0, 10.0, 9*4), numpy.linspace(-1.0, 1.0, 9*5) self.field = Field(x=x, y=y, time_step=0, values=numpy.random.rand(y.size, x.size), label='test') self.restriction() self.get_difference() self.subtract() def restriction(self): print('\nField.restriction() ...'), field1 = self.field.restriction([self.field.x, self.field.y]) field2 = self.field.restriction([self.field.x[::3], self.field.y[::3]]) field3 = self.field.restriction([self.field.x[::9], self.field.y[::9]]) assert numpy.allclose(field1.x, self.field.x, atol=1.0E-06) assert numpy.allclose(field1.y, self.field.y, atol=1.0E-06) assert numpy.allclose(field1.values, self.field.values, atol=1.0E-06) assert numpy.allclose(field2.x, self.field.x[::3], atol=1.0E-06) assert numpy.allclose(field2.y, self.field.y[::3], atol=1.0E-06) assert numpy.allclose(field2.values, self.field.values[::3, ::3], atol=1.0E-06) assert numpy.allclose(field3.x, self.field.x[::9], atol=1.0E-06) assert numpy.allclose(field3.y, self.field.y[::9], atol=1.0E-06) assert numpy.allclose(field3.values, self.field.values[::9, ::9], atol=1.0E-06) print('ok') def get_difference(self): print('\nField.get_difference() ...'), assert self.field.get_difference(self.field, self.field, norm='L2') == 0.0 assert self.field.get_difference(self.field, self.field, norm='Linf') == 0.0 print('ok') def subtract(self): print('\nField.subtract() ...'), self.field.subtract(self.field) assert numpy.allclose(self.field.values, numpy.zeros_like(self.field.values), atol=1.0E-06) print('ok')
def test_three_grids(nx=11, ny=11, ratio=3, offset=0): """Computes the observed order of convergence using the solution on three consecutive grids with constant refinement ratio. The solution on the finest grid is random. The solution of the medium grid is the finest solution restricted and incremented by 1. The solution of the coarsest grid is the finest solution restricted and incremented by 1+ratio. Therefore, no matter the norm used, we expect an order of convergence of 1. Parameters ---------- nx, ny: integers, optional Number of grid-points along each direction on the grid used a mask to project the three solutions; default: 11, 11. ratio: integer, optional Grid refinement ratio; default: 3. offset: integer, optional Position of the coarsest grid relatively to the mask grid; default: 0 (the coarsest solution is defined on the mask grid) """ # grid used as mask grid = [numpy.random.random(nx), numpy.random.random(ny)] # create fields fine = Field(values=numpy.random.rand(ny * ratio**(offset + 2), nx * ratio**(offset + 2)), label='fine') medium = Field(values=fine.values[::ratio, ::ratio] + 1.0, label='medium') coarse = Field(values=fine.values[::ratio**2, ::ratio**2] + (1.0 + ratio), label='coarse') # fill nodal stations coarse.x, coarse.y = numpy.ones(nx * ratio**offset), numpy.ones( ny * ratio**offset) coarse.x[::ratio**offset], coarse.y[::ratio**offset] = grid[0][:], grid[ 1][:] medium.x, medium.y = numpy.ones(nx * ratio**(offset + 1)), numpy.ones( ny * ratio**(offset + 1)) medium.x[::ratio**(offset + 1)], medium.y[::ratio**(offset + 1)] = grid[0][:], grid[1][:] fine.x, fine.y = numpy.ones(nx * ratio**(offset + 2)), numpy.ones( ny * ratio**(offset + 2)) fine.x[::ratio**(offset + 2)], fine.y[::ratio**(offset + 2)] = grid[0][:], grid[1][:] # compute observed order of convergence p = convergence.get_observed_order(coarse, medium, fine, ratio, grid) assert p == 1.0 p = convergence.get_observed_order(coarse, medium, fine, ratio, grid, order=numpy.inf) assert p == 1.0