Example #1
0
def test_rchload():
    nlay = 2
    nrow = 3
    ncol = 4
    nper = 2

    # create model 1
    m1 = flopy.modflow.Modflow("rchload1", model_ws=newpth)
    dis1 = flopy.modflow.ModflowDis(m1,
                                    nlay=nlay,
                                    nrow=nrow,
                                    ncol=ncol,
                                    nper=nper)
    a = np.random.random((nrow, ncol))
    rech1 = Util2d(m1, (nrow, ncol),
                   np.float32,
                   a,
                   "rech",
                   cnstnt=1.0,
                   how="openclose")
    rch1 = flopy.modflow.ModflowRch(m1, rech={0: rech1})
    m1.write_input()

    # load model 1
    os.chdir(newpth)
    m1l = flopy.modflow.Modflow.load("rchload1.nam")
    a1 = rech1.array
    a2 = m1l.rch.rech[0].array
    assert np.allclose(a1, a2)
    a2 = m1l.rch.rech[1].array
    assert np.allclose(a1, a2)
    os.chdir(startpth)

    m2 = flopy.modflow.Modflow("rchload2", model_ws=newpth)
    dis2 = flopy.modflow.ModflowDis(m2,
                                    nlay=nlay,
                                    nrow=nrow,
                                    ncol=ncol,
                                    nper=nper)
    a = np.random.random((nrow, ncol))
    rech2 = Util2d(m2, (nrow, ncol),
                   np.float32,
                   a,
                   "rech",
                   cnstnt=2.0,
                   how="openclose")
    rch2 = flopy.modflow.ModflowRch(m2, rech={0: rech2})
    m2.write_input()

    # load model 2
    os.chdir(newpth)
    m2l = flopy.modflow.Modflow.load("rchload2.nam")
    a1 = rech2.array
    a2 = m2l.rch.rech[0].array
    assert np.allclose(a1, a2)
    a2 = m2l.rch.rech[1].array
    assert np.allclose(a1, a2)
    os.chdir(startpth)
Example #2
0
def test_new_get_file_entry():
    ml = flopy.modflow.Modflow(model_ws=out_dir)
    u2d = Util2d(ml, (5, 2), np.float32, np.ones((5, 2)), 'test', locat=99)
    print(u2d.get_file_entry(how="internal"))
    print(u2d.get_file_entry(how="constant"))
    print(u2d.get_file_entry(how="external"))
    u2d.format.binary = True
    print(u2d.get_file_entry(how="external"))
    u2d.format.binary = False
    print(u2d.get_file_entry(how="openclose"))
    u2d.format.binary = True
    print(u2d.get_file_entry(how="openclose"))

    ml.array_free_format = False
    u2d = Util2d(ml, (5, 2), np.float32, np.ones((5, 2)), 'test', locat=99)
    print(u2d.get_file_entry(how="internal"))
    print(u2d.get_file_entry(how="constant"))
    print(u2d.get_file_entry(how="external"))
    u2d.format.binary = True
    print(u2d.get_file_entry(how="external"))
Example #3
0
def test_arrayformat():
    ml = flopy.modflow.Modflow(model_ws=out_dir)
    u2d = Util2d(ml, (15, 2), np.float32, np.ones((15, 2)), 'test')

    fmt_fort = u2d.format.fortran
    cr = u2d.get_internal_cr()
    parsed = Util2d.parse_control_record(cr)
    print(fmt_fort, parsed["fmtin"])
    assert fmt_fort.upper() == parsed["fmtin"].upper()

    u2d.format.npl = 1
    fmt_fort = u2d.format.fortran
    cr = u2d.get_internal_cr()
    parsed = Util2d.parse_control_record(cr)
    print(fmt_fort, parsed["fmtin"])
    assert fmt_fort.upper() == parsed["fmtin"].upper()

    u2d.format.npl = 2
    u2d.format.width = 8
    fmt_fort = u2d.format.fortran
    cr = u2d.get_internal_cr()
    parsed = Util2d.parse_control_record(cr)
    print(fmt_fort, parsed["fmtin"])
    assert fmt_fort.upper() == parsed["fmtin"].upper()

    u2d.format.free = True
    u2d.format.width = 8
    fmt_fort = u2d.format.fortran
    cr = u2d.get_internal_cr()
    parsed = Util2d.parse_control_record(cr)
    print(fmt_fort, parsed["fmtin"])
    assert fmt_fort.upper() == parsed["fmtin"].upper()

    u2d.format.free = False
    fmt_fort = u2d.format.fortran
    cr = u2d.get_internal_cr()
    parsed = Util2d.parse_control_record(cr)
    print(fmt_fort, parsed["fmtin"])
    assert fmt_fort.upper() == parsed["fmtin"].upper()

    u2d.fmtin = "(10G15.6)"
    fmt_fort = u2d.format.fortran
    cr = u2d.get_internal_cr()
    parsed = Util2d.parse_control_record(cr)
    print(fmt_fort, parsed["fmtin"])
    assert fmt_fort.upper() == parsed["fmtin"].upper()

    u2d.format.binary = True
    fmt_fort = u2d.format.fortran
    cr = u2d.get_internal_cr()
    parsed = Util2d.parse_control_record(cr)
    print(fmt_fort, parsed["fmtin"])
    assert fmt_fort.upper() == parsed["fmtin"].upper()
Example #4
0
def test_util2d():
    ml = flopy.modflow.Modflow()
    u2d = Util2d(ml, (10, 10), np.float32, 10., "test")
    a1 = u2d.array
    a2 = np.ones((10, 10), dtype=np.float32) * 10.
    assert np.array_equal(a1, a2)

    # test external filenames - ascii and binary
    fname_ascii = os.path.join(out_dir, 'test_a.dat')
    fname_bin = os.path.join(out_dir, 'test_b.dat')
    np.savetxt(fname_ascii, a1, fmt="%15.6E")
    u2d.write_bin(a1.shape, fname_bin, a1, bintype="head")
    dis = flopy.modflow.ModflowDis(ml, 2, 10, 10)
    lpf = flopy.modflow.ModflowLpf(ml, hk=[fname_ascii, fname_bin])
    ml.lpf.hk[1].fmtin = "(BINARY)"
    assert np.array_equal(lpf.hk[0].array, a1)
    assert np.array_equal(lpf.hk[1].array, a1)

    # test external filenames - ascii and binary with model_ws and external_path
    ml = flopy.modflow.Modflow(model_ws=out_dir,
                               external_path=os.path.join(out_dir, "ref"))
    u2d = Util2d(ml, (10, 10), np.float32, 10., "test")
    fname_ascii = os.path.join(out_dir, 'test_a.dat')
    fname_bin = os.path.join(out_dir, 'test_b.dat')
    np.savetxt(fname_ascii, a1, fmt="%15.6E")
    u2d.write_bin(a1.shape, fname_bin, a1, bintype="head")
    dis = flopy.modflow.ModflowDis(ml, 2, 10, 10)
    lpf = flopy.modflow.ModflowLpf(ml, hk=[fname_ascii, fname_bin])
    ml.lpf.hk[1].fmtin = "(BINARY)"
    assert np.array_equal(lpf.hk[0].array, a1)
    assert np.array_equal(lpf.hk[1].array, a1)

    # bin read write test
    fname = os.path.join(out_dir, 'test.bin')
    u2d.write_bin((10, 10), fname, u2d.array)
    a3 = u2d.load_bin((10, 10), fname, u2d.dtype)[1]
    assert np.array_equal(a3, a1)
    # ascii read write test
    fname = os.path.join(out_dir, 'text.dat')
    u2d.write_txt((10, 10), fname, u2d.array)
    a4 = u2d.load_txt((10, 10), fname, u2d.dtype, "(FREE)")
    assert np.array_equal(a1, a4)

    # fixed format read/write with touching numbers - yuck!
    data = np.arange(100).reshape(10, 10)
    u2d_arange = Util2d(ml, (10, 10), np.float32, data, "test")
    u2d_arange.write_txt((10, 10),
                         fname,
                         u2d_arange.array,
                         python_format=[7, "{0:10.4E}"])
    a4a = u2d.load_txt((10, 10), fname, np.float32, "(7E10.6)")
    assert np.array_equal(u2d_arange.array, a4a)

    # test view vs copy with .array
    a5 = u2d.array
    a5 += 1
    assert not np.array_equal(a5, u2d.array)

    # Util2d.__mul__() overload
    new_2d = u2d * 2
    assert np.array_equal(new_2d.array, u2d.array * 2)

    # test the cnstnt application
    u2d.cnstnt = 2.0
    a6 = u2d.array
    assert not np.array_equal(a1, a6)
    u2d.write_txt((10, 10), fname, u2d.array)
    a7 = u2d.load_txt((10, 10), fname, u2d.dtype, "(FREE)")
    assert np.array_equal(u2d.array, a7)

    # test binary integer file
    fname = os.path.join(out_dir, 'test.int')
    e8 = np.arange(3 * 4).reshape((3, 4)) - 1
    with open(fname, 'wb') as fp:
        fp.write(e8.tobytes())
    h8, a8 = Util2d.load_bin((3, 4), fname, np.int)
    assert h8 is None  # no header_dtype
    np.testing.assert_equal(a8, e8)

    return