Beispiel #1
0
def merge_subdomains(base, digits, it, save=True):
    fn_subdomains = io.subdomains_filename(base)
    with open(fn_subdomains, 'rb') as f:
        subdomains = pickle.load(f)
    bb = get_bounding_box(subdomains)

    data = np.load(io.filename(base, digits, subdomains[0].id, it))
    dtype = data['v'].dtype
    dim = data['v'].shape[0]

    out = {}
    for field in data.files:
        if len(data[field].shape) == dim:
            shape = bb
        else:
            shape = list(data[field].shape)
            shape[-dim:] = bb

        out[field] = np.zeros(shape, dtype=dtype)
        out[field][:] = np.nan
        # np.ma.masked_all(shape, dtype=dtype)

    for s in subdomains:
        fn = io.filename(base, digits, s.id, it)
        data = np.load(fn)
        for field in data.files:
            selector = [slice(None)] * (len(data[field].shape) - dim)
            selector.extend([slice(i0, i1) for i0, i1 in reversed(list(zip(s.location, s.end_location)))])
            out[field][selector] = data[field]

    if save:
        np.savez(io.merged_filename(base, digits, it), **out)
    return out
Beispiel #2
0
def merge_subdomains(base, digits, it, save=True):
    fn_subdomains = io.subdomains_filename(base)
    subdomains = pickle.load(open(fn_subdomains, 'r'))
    bb = get_bounding_box(subdomains)

    data = np.load(io.filename(base, digits, subdomains[0].id, it))
    dtype = data['v'].dtype
    dim = data['v'].shape[0]

    out = {}
    for field in data.files:
        if len(data[field].shape) == dim:
            shape = bb
        else:
            shape = list(data[field].shape)
            shape[-dim:] = bb

        out[field] = np.zeros(shape, dtype=dtype)
        out[field][:] = np.nan
        # np.ma.masked_all(shape, dtype=dtype)

    for s in subdomains:
        fn = io.filename(base, digits, s.id, it)
        data = np.load(fn)
        for field in data.files:
            selector = [slice(None)] * (len(data[field].shape) - dim)
            selector.extend([
                slice(i0, i1)
                for i0, i1 in reversed(zip(s.location, s.end_location))
            ])
            out[field][selector] = data[field]

    if save:
        np.savez(io.merged_filename(base, digits, it), **out)
    return out
Beispiel #3
0
    def test_shift_3d(self):
        global output
        output = os.path.join(tmpdir, 'baseline')
        LBSimulationController(SCTestSim3D, LBGeometry3D).run(ignore_cmdline=True)
        digits = io.filename_iter_digits(MAX_ITERS_3D)
        ref = np.load(io.filename(output, digits, 0, MAX_ITERS_3D))

        output = os.path.join(tmpdir, 'shifted')
        SCTestDomain3D.shift = True
        LBSimulationController(SCTestSim3D, LBGeometry3D).run(ignore_cmdline=True)
        shifted = np.load(io.filename(output, digits, 0, MAX_ITERS_3D))
        self._verify(ref, shifted)
Beispiel #4
0
    def test_shift_3d(self):
        global output
        output = os.path.join(tmpdir, 'baseline')
        LBSimulationController(SCTestSim3D,
                               LBGeometry3D).run(ignore_cmdline=True)
        digits = io.filename_iter_digits(MAX_ITERS_3D)
        ref = np.load(io.filename(output, digits, 0, MAX_ITERS_3D))

        output = os.path.join(tmpdir, 'shifted')
        SCTestDomain3D.shift = True
        LBSimulationController(SCTestSim3D,
                               LBGeometry3D).run(ignore_cmdline=True)
        shifted = np.load(io.filename(output, digits, 0, MAX_ITERS_3D))
        self._verify(ref, shifted)
Beispiel #5
0
 def setUpClass(cls):
     global blocks, output
     output = os.path.join(tmpdir, 'ref')
     blocks = 1
     LBSimulationController(SimulationTest, LDCGeometry).run(ignore_cmdline=True)
     cls.digits = io.filename_iter_digits(MAX_ITERS)
     cls.ref = np.load(io.filename(output, cls.digits, 0, MAX_ITERS))
Beispiel #6
0
 def setUpClass(cls):
     global blocks, output
     output = os.path.join(tmpdir, 'ref')
     blocks = 1
     LBSimulationController(SimulationTest, LDCGeometry).run(ignore_cmdline=True)
     cls.digits = io.filename_iter_digits(MAX_ITERS)
     cls.ref = np.load(io.filename(output, cls.digits, 0, MAX_ITERS))
Beispiel #7
0
def run_test(name, precision):
    minvec = []
    maxvec = []
    ordvec = []
    basepath = os.path.join('regtest/results', name)

    if not os.path.exists(basepath):
        os.makedirs(basepath)

    xvec = np.linspace(3, 5.5, num=POINTS)
    f = open(os.path.join(basepath, '%s.dat' % precision), 'w')
    output = os.path.join(tmpdir, 'phase_sep')

    max_iters = 50000
    for g in xvec:
        print(' {0}'.format(g), end=' ')
        defaults = {
                'quiet': True,
                'verbose': False,
                'every': 1000,
                'seed': 2348,
                'max_iters': max_iters,
                'output': output,
                'G': -g,
            }

        LBSimulationController(SCSim, LBGeometry2D,
                default_config=defaults).run(ignore_cmdline=True)

        digits = io.filename_iter_digits(max_iters)
        fname = io.filename(output, digits, 0, max_iters)

        data = np.load(fname)
        rho = data['rho']
        avg = np.average(rho)
        order = np.sqrt(np.average(np.square(rho - avg))) / avg

        minvec.append(np.min(rho))
        maxvec.append(np.max(rho))
        ordvec.append(order)
        print(g, minvec[-1], maxvec[-1], ordvec[-1], file=f)

    print()

    f.close()

    plt.clf()
    plt.cla()

    plt.plot(xvec, minvec, 'b.-', label='min rho')
    plt.plot(xvec, maxvec, 'r.-', label='max rho')
    plt.plot(xvec, ordvec, 'k.-', label='order parameter')
    plt.gca().yaxis.grid(True)
    plt.gca().yaxis.grid(True, which='minor')
    plt.gca().xaxis.grid(True)
    plt.gca().xaxis.grid(True, which='minor')
    plt.ylabel('rho / order parameter')
    plt.xlabel('g')
    plt.legend(loc='upper left')
    plt.savefig(os.path.join(basepath, '%s.pdf' % precision), format='pdf')
Beispiel #8
0
    def test_shift_3d(self):
        global output
        output = os.path.join(tmpdir, 'baseline')
        LBSimulationController(FETestSim3D, LBGeometry3D).run(ignore_cmdline=True)
        digits = io.filename_iter_digits(MAX_ITERS_3D)
        ref = np.load(io.filename(output, digits, 0, MAX_ITERS_3D))

        output = os.path.join(tmpdir, 'shifted')
        FETestDomain3D.shift = True
        LBSimulationController(FETestSim3D, LBGeometry3D).run(ignore_cmdline=True)
        shifted = np.load(io.filename(output, digits, 0, MAX_ITERS_3D))
        match_fields(ref['rho'], shifted['rho'])
        match_fields(ref['phi'], shifted['phi'])
        self.assertFalse(np.any(np.isnan(ref['rho'])))
        self.assertFalse(np.any(np.isnan(ref['phi'])))
        self.assertFalse(np.any(np.isnan(shifted['rho'])))
        self.assertFalse(np.any(np.isnan(shifted['phi'])))
Beispiel #9
0
    def setUpClass(cls):
        global blocks, vertical, output
        output = os.path.join(tmpdir, 'href')
        blocks = 1
        LBSimulationController(SimulationTest, EqualSubdomainsGeometry2D).run(ignore_cmdline=True)
        cls.digits = io.filename_iter_digits(MAX_ITERS)
        cls.href = np.load(io.filename(output, cls.digits, 0, MAX_ITERS))
        cls.hrho = cls.href['rho']
        cls.hvx  = cls.href['v'][0]
        cls.hvy  = cls.href['v'][1]

        output = os.path.join(tmpdir, 'vref')
        vertical = True
        LBSimulationController(SimulationTest, EqualSubdomainsGeometry2D).run(ignore_cmdline=True)
        cls.vref = np.load(io.filename(output, cls.digits, 0, MAX_ITERS))
        cls.vrho = cls.vref['rho']
        cls.vvx  = cls.vref['v'][0]
        cls.vvy  = cls.vref['v'][1]
Beispiel #10
0
    def test_shift_3d(self):
        global output
        output = os.path.join(tmpdir, 'baseline')
        LBSimulationController(FETestSim3D,
                               LBGeometry3D).run(ignore_cmdline=True)
        digits = io.filename_iter_digits(MAX_ITERS_3D)
        ref = np.load(io.filename(output, digits, 0, MAX_ITERS_3D))

        output = os.path.join(tmpdir, 'shifted')
        FETestDomain3D.shift = True
        LBSimulationController(FETestSim3D,
                               LBGeometry3D).run(ignore_cmdline=True)
        shifted = np.load(io.filename(output, digits, 0, MAX_ITERS_3D))
        match_fields(ref['rho'], shifted['rho'])
        match_fields(ref['phi'], shifted['phi'])
        self.assertFalse(np.any(np.isnan(ref['rho'])))
        self.assertFalse(np.any(np.isnan(ref['phi'])))
        self.assertFalse(np.any(np.isnan(shifted['rho'])))
        self.assertFalse(np.any(np.isnan(shifted['phi'])))
Beispiel #11
0
    def setUpClass(cls):
        global blocks, vertical, output
        output = os.path.join(tmpdir, 'href')
        blocks = 1
        LBSimulationController(
            SimulationTest, EqualSubdomainsGeometry2D).run(ignore_cmdline=True)
        cls.digits = io.filename_iter_digits(MAX_ITERS)
        cls.href = np.load(io.filename(output, cls.digits, 0, MAX_ITERS))
        cls.hrho = cls.href['rho']
        cls.hvx = cls.href['v'][0]
        cls.hvy = cls.href['v'][1]

        output = os.path.join(tmpdir, 'vref')
        vertical = True
        LBSimulationController(
            SimulationTest, EqualSubdomainsGeometry2D).run(ignore_cmdline=True)
        cls.vref = np.load(io.filename(output, cls.digits, 0, MAX_ITERS))
        cls.vrho = cls.vref['rho']
        cls.vvx = cls.vref['v'][0]
        cls.vvy = cls.vref['v'][1]
Beispiel #12
0
def run_test(name, precision):
    minvec = []
    maxvec = []
    ordvec = []
    basepath = os.path.join('regtest/results', name)

    if not os.path.exists(basepath):
        os.makedirs(basepath)

    xvec = np.linspace(3, 5.5, num=POINTS)
    f = open(os.path.join(basepath, '%s.dat' % precision), 'w')
    output = os.path.join(tmpdir, 'phase_sep')

    max_iters = 50000
    for g in xvec:
        print ' {0}'.format(g),
        defaults = {
            'quiet': True,
            'verbose': False,
            'every': 1000,
            'seed': 2348,
            'max_iters': max_iters,
            'output': output,
            'G': -g,
        }

        LBSimulationController(
            SCSim, LBGeometry2D,
            default_config=defaults).run(ignore_cmdline=True)

        digits = io.filename_iter_digits(max_iters)
        fname = io.filename(output, digits, 0, max_iters)

        data = np.load(fname)
        rho = data['rho']
        avg = np.average(rho)
        order = np.sqrt(np.average(np.square(rho - avg))) / avg

        minvec.append(np.min(rho))
        maxvec.append(np.max(rho))
        ordvec.append(order)
        print >> f, g, minvec[-1], maxvec[-1], ordvec[-1]

    print

    f.close()

    plt.clf()
    plt.cla()

    plt.plot(xvec, minvec, 'b.-', label='min rho')
    plt.plot(xvec, maxvec, 'r.-', label='max rho')
    plt.plot(xvec, ordvec, 'k.-', label='order parameter')
    plt.gca().yaxis.grid(True)
    plt.gca().yaxis.grid(True, which='minor')
    plt.gca().xaxis.grid(True)
    plt.gca().xaxis.grid(True, which='minor')
    plt.ylabel('rho / order parameter')
    plt.xlabel('g')
    plt.legend(loc='upper left')
    plt.savefig(os.path.join(basepath, '%s.pdf' % precision), format='pdf')
Beispiel #13
0
def run_test_2d():
    xvec = np.logspace(-3, -1, num=POINTS)
    yvec = np.zeros(POINTS, dtype=np.float64)
    tmpdir = tempfile.mkdtemp()

    summary_path = 'regtest/results/poiseuille/summary.png'
    profile_path = 'regtest/results/poiseuille'

    for i, visc in enumerate(xvec):
        print '%f ' % visc,

        max_iters = int(100 / visc)
        base_path = os.path.join(tmpdir, 'visc{0}'.format(i))

        defaults = {}
        defaults['visc'] = visc
        defaults['max_iters'] = max_iters
        defaults['output'] = base_path

        ctrl = LBSimulationController(TestPoiseuille2D, default_config=defaults)
        ctrl.run()

        digits = io.filename_iter_digits(max_iters)

        final_iter = 100 * (max_iters / 100)
        fname = io.filename(base_path, digits, 0, final_iter)
        res = np.load(fname)

        vx = res['v'][0]
        nyw = vx.shape[1] / 2
        hy = np.mgrid[0:vx.shape[0]]

        profile_sim = vx[:,nyw]
        profile_th = TestPoiseuille2D.subdomain.velocity_profile(ctrl.config, hy)

        yvec[i] = np.nanmax(profile_sim) / np.nanmax(profile_th) - 1.0

        plt.gca().yaxis.grid(True)
        plt.gca().xaxis.grid(True)
        plt.gca().xaxis.grid(True, which='minor')
        plt.gca().yaxis.grid(True, which='minor')
        plt.plot((profile_th[1:-1] - profile_sim[1:-1]) / np.nanmax(profile_th), 'b.-')
        plt.gca().set_xbound(0, len(profile_sim)-2)
        plt.title('(theoretical - simulation) / theo; visc = %f' % visc)
        plt.savefig(os.path.join(profile_path, 'profile{0:02d}.png'.format(i)),
                format='png')

        plt.clf()
        plt.cla()

    plt.clf()
    plt.cla()

    plt.semilogx(xvec, yvec * 100, 'b.-')
    plt.gca().yaxis.grid(True)
    plt.gca().yaxis.grid(True, which='minor')
    plt.gca().xaxis.grid(True)
    plt.gca().xaxis.grid(True, which='minor')
    plt.ylabel('max velocity / theoretical max velocity - 1 [%]')
    plt.xlabel('viscosity')
    plt.savefig(summary_path, format='png')

    shutil.rmtree(tmpdir)
Beispiel #14
0
def run_test_2d():
    xvec = np.logspace(-3, -1, num=POINTS)
    yvec = np.zeros(POINTS, dtype=np.float64)
    tmpdir = tempfile.mkdtemp()

    summary_path = 'regtest/results/poiseuille/summary.png'
    profile_path = 'regtest/results/poiseuille'

    for i, visc in enumerate(xvec):
        print '%f ' % visc,

        max_iters = int(100 / visc)
        base_path = os.path.join(tmpdir, 'visc{0}'.format(i))

        defaults = {}
        defaults['visc'] = visc
        defaults['max_iters'] = max_iters
        defaults['output'] = base_path

        ctrl = LBSimulationController(TestPoiseuille2D,
                                      default_config=defaults)
        ctrl.run()

        digits = io.filename_iter_digits(max_iters)

        final_iter = 100 * (max_iters / 100)
        fname = io.filename(base_path, digits, 0, final_iter)
        res = np.load(fname)

        vx = res['v'][0]
        nyw = vx.shape[1] / 2
        hy = np.mgrid[0:vx.shape[0]]

        profile_sim = vx[:, nyw]
        profile_th = TestPoiseuille2D.subdomain.velocity_profile(
            ctrl.config, hy)

        yvec[i] = np.nanmax(profile_sim) / np.nanmax(profile_th) - 1.0

        plt.gca().yaxis.grid(True)
        plt.gca().xaxis.grid(True)
        plt.gca().xaxis.grid(True, which='minor')
        plt.gca().yaxis.grid(True, which='minor')
        plt.plot(
            (profile_th[1:-1] - profile_sim[1:-1]) / np.nanmax(profile_th),
            'b.-')
        plt.gca().set_xbound(0, len(profile_sim) - 2)
        plt.title('(theoretical - simulation) / theo; visc = %f' % visc)
        plt.savefig(os.path.join(profile_path, 'profile{0:02d}.png'.format(i)),
                    format='png')

        plt.clf()
        plt.cla()

    plt.clf()
    plt.cla()

    plt.semilogx(xvec, yvec * 100, 'b.-')
    plt.gca().yaxis.grid(True)
    plt.gca().yaxis.grid(True, which='minor')
    plt.gca().xaxis.grid(True)
    plt.gca().xaxis.grid(True, which='minor')
    plt.ylabel('max velocity / theoretical max velocity - 1 [%]')
    plt.xlabel('viscosity')
    plt.savefig(summary_path, format='png')

    shutil.rmtree(tmpdir)