Skip to content

Commit

Permalink
add quadtree IO tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roeldegoede committed Dec 19, 2024
1 parent cd14730 commit 2446903
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 6 deletions.
2 changes: 2 additions & 0 deletions hydromt_sfincs/sfincs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3426,6 +3426,8 @@ def read_results(
chunks={"time": chunksize},
)

# set coords
dsu = dsu.set_coords(["mesh2d_node_x","mesh2d_node_y"])
# get crs variable, drop it and set it correctly
crs = dsu["crs"].values
dsu.drop_vars("crs")
Expand Down
9 changes: 9 additions & 0 deletions tests/data/sfincs_test_quadtree/gis/bnd.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "FeatureCollection",
"name": "bnd",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::32633" } },
"features": [
{ "type": "Feature", "properties": { "stations": 1 }, "geometry": { "type": "Point", "coordinates": [ 319526.0, 5041108.0 ] } },
{ "type": "Feature", "properties": { "stations": 2 }, "geometry": { "type": "Point", "coordinates": [ 329195.0, 5046243.0 ] } }
]
}
8 changes: 8 additions & 0 deletions tests/data/sfincs_test_quadtree/gis/region.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "FeatureCollection",
"name": "region",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::32633" } },
"features": [
{ "type": "Feature", "properties": { "value": 1.0 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 324990.358609079034068, 5044296.783995999023318 ], [ 324967.659084092068952, 5044341.334322208538651 ], [ 324878.558431673212908, 5044295.935272234492004 ], [ 324855.858906686247792, 5044340.485598444007337 ], [ 324588.55694942973787, 5044204.288448522798717 ], [ 324565.857424442772754, 5044248.83877473231405 ], [ 324075.803836139151827, 5043999.14399987552315 ], [ 324053.104311152128503, 5044043.694326085038483 ], [ 323919.453332523873542, 5043975.595751123502851 ], [ 323942.152857510896865, 5043931.045424913987517 ], [ 323585.750247835589107, 5043749.44922501873225 ], [ 323608.449772822554223, 5043704.898898809216917 ], [ 323430.248467984842137, 5043614.100798861123621 ], [ 323407.54894299787702, 5043658.651125070638955 ], [ 323318.448290579079185, 5043613.252075096592307 ], [ 323295.748765592055861, 5043657.80240130610764 ], [ 323162.0977869638009, 5043589.70382634550333 ], [ 323139.398261976835784, 5043634.254152555018663 ], [ 322872.096304720325861, 5043498.057002632878721 ], [ 322894.795829707290977, 5043453.506676423363388 ], [ 322805.695177288434934, 5043408.10762644931674 ], [ 322782.995652301469818, 5043452.657952658832073 ], [ 322693.894999882613774, 5043407.258902684785426 ], [ 322671.195474895648658, 5043451.809228893369436 ], [ 322582.094822476850823, 5043406.410178919322789 ], [ 322559.395297489885706, 5043450.960505128838122 ], [ 322069.341709186264779, 5043201.265730272047222 ], [ 322046.642184199299663, 5043245.816056481562555 ], [ 321957.54153178044362, 5043200.41700650844723 ], [ 321934.842006793420296, 5043244.967332717962563 ], [ 321845.74135437462246, 5043199.568282743915915 ], [ 321823.041829387657344, 5043244.118608953431249 ], [ 321778.491503178200219, 5043221.419083965942264 ], [ 321801.191028165165335, 5043176.86875775642693 ], [ 321622.989723327511456, 5043086.070657808333635 ], [ 321645.68924831453478, 5043041.520331598818302 ], [ 321556.588595895678736, 5042996.121281625702977 ], [ 321579.288120882643852, 5042951.570955416187644 ], [ 321222.885511207336094, 5042769.974755520001054 ], [ 321200.185986220370978, 5042814.525081729516387 ], [ 321066.535007592116017, 5042746.426506768912077 ], [ 321089.234532579081133, 5042701.876180559396744 ], [ 320599.180944275460206, 5042452.181405702605844 ], [ 320576.48141928849509, 5042496.731731912121177 ], [ 320398.280114450841211, 5042405.933631964027882 ], [ 320375.580589463817887, 5042450.483958173543215 ], [ 320108.278632207307965, 5042314.286808251403272 ], [ 320085.579107220342848, 5042358.837134460918605 ], [ 319996.478454801486805, 5042313.438084486871958 ], [ 319973.778929814521689, 5042357.988410696387291 ], [ 319840.127951186266728, 5042289.889835735782981 ], [ 319817.428426199301612, 5042334.440161945298314 ], [ 319683.77744757104665, 5042266.341586984694004 ], [ 319706.476972558011767, 5042221.791260775178671 ], [ 319305.524036673246883, 5042017.495535891503096 ], [ 319328.223561660211999, 5041972.945209681987762 ], [ 318927.270625775447115, 5041768.64948479924351 ], [ 318949.970150762412231, 5041724.099158589728177 ], [ 318860.869498343556188, 5041678.700108616612852 ], [ 318838.169973356591072, 5041723.250434826128185 ], [ 318749.069320937793236, 5041677.851384852081537 ], [ 318726.36979595082812, 5041722.40171106159687 ], [ 318637.269143531972077, 5041677.002661087550223 ], [ 318614.569618545006961, 5041721.552987297065556 ], [ 318213.616682660242077, 5041517.257262414321303 ], [ 318236.316207647207193, 5041472.70693620480597 ], [ 317969.014250390697271, 5041336.509786282666028 ], [ 316221.150826393452007, 5044766.884904407896101 ], [ 327358.732378748012707, 5050441.766151151619852 ], [ 329106.59580274525797, 5047011.391033026389778 ], [ 328928.394497907604091, 5046920.592933079227805 ], [ 328951.094022894569207, 5046876.042606869712472 ], [ 328772.892718056915328, 5046785.244506921619177 ], [ 328795.592243043880444, 5046740.694180712103844 ], [ 328751.041916834423319, 5046717.994655724614859 ], [ 328728.342391847458202, 5046762.544981934130192 ], [ 328639.241739428660367, 5046717.145931961014867 ], [ 328616.542214441695251, 5046761.6962581705302 ], [ 328527.441562022839207, 5046716.297208196483552 ], [ 328504.742037035874091, 5046760.847534405998886 ], [ 328192.889753569965251, 5046601.950859496369958 ], [ 328215.589278556930367, 5046557.400533286854625 ], [ 327992.837647509819362, 5046443.902908352203667 ], [ 328015.537172496784478, 5046399.352582142688334 ], [ 327926.436520077928435, 5046353.953532168641686 ], [ 327949.136045064893551, 5046309.403205959126353 ], [ 327904.585718855494633, 5046286.703680972568691 ], [ 327927.285243842517957, 5046242.153354763053358 ], [ 327838.184591423661914, 5046196.75430478900671 ], [ 327860.88411641062703, 5046152.203978579491377 ], [ 327549.03183294471819, 5045993.307303670793772 ], [ 327571.731357931683306, 5045948.756977461278439 ], [ 327438.080379303428344, 5045880.658402499742806 ], [ 327460.779904290393461, 5045836.108076291158795 ], [ 326837.075337358517572, 5045518.314726473763585 ], [ 326814.375812371552456, 5045562.865052682347596 ], [ 326769.825486162153538, 5045540.165527695789933 ], [ 326792.525011149118654, 5045495.615201487205923 ], [ 326614.323706311464775, 5045404.817101539112628 ], [ 326637.023231298429891, 5045360.266775329597294 ], [ 326503.37225267017493, 5045292.168200368992984 ], [ 326526.071777657198254, 5045247.617874159477651 ], [ 326392.420799028943293, 5045179.519299197942019 ], [ 326415.120324015908409, 5045134.968972988426685 ], [ 326058.717714340542443, 5044953.372773093171418 ], [ 326081.417239327507559, 5044908.822446883656085 ], [ 325680.464303442742676, 5044704.526722000911832 ], [ 325657.764778455777559, 5044749.077048210427165 ], [ 325390.462821199267637, 5044612.879898288287222 ], [ 325413.162346186232753, 5044568.329572078771889 ], [ 325234.961041348578874, 5044477.531472130678594 ], [ 325257.66056633554399, 5044432.981145921163261 ], [ 324990.358609079034068, 5044296.783995999023318 ] ] ] } }
]
}
2 changes: 2 additions & 0 deletions tests/data/sfincs_test_quadtree/sfincs.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
319526.0 5041108.0
329195.0 5046243.0
3 changes: 3 additions & 0 deletions tests/data/sfincs_test_quadtree/sfincs.bzs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0.0 0.00 0.25
43200.0 0.75 1.00
86400.0 0.00 0.25
39 changes: 39 additions & 0 deletions tests/data/sfincs_test_quadtree/sfincs.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
epsg = 32633
latitude = 0.0
tref = 20100201 000000
tstart = 20100201 000000
tstop = 20100202 000000
tspinup = 3600
dtout = 3600.0
dthisout = 600.0
dtrstout = 0.0
dtmaxout = 86400
trstout = -999.0
dtwnd = 1800.0
alpha = 0.5
theta = 1.0
huthresh = 0.01
manning_land = 0.04
manning_sea = 0.02
rgh_lev_land = 0.0
zsini = 0.0
rhoa = 1.25
rhow = 1024.0
dtmax = 60.0
advection = 1
baro = 1
pavbnd = 0
gapres = 101200.0
stopdepth = 100.0
crsgeo = 0
btfilter = 60.0
viscosity = 1
bndfile = sfincs.bnd
bzsfile = sfincs.bzs
sbgfile = sfincs_subgrid.nc
qtrfile = sfincs.nc
inputformat = bin
outputformat = net
cdnrb = 3
cdwnd = 0.0 28.0 50.0
cdval = 0.001 0.0025 0.0015
Binary file added tests/data/sfincs_test_quadtree/sfincs.nc
Binary file not shown.
102 changes: 102 additions & 0 deletions tests/data/sfincs_test_quadtree/sfincs_log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

----------- Welcome to SFINCS -----------

@@@@@ @@@@@@@ @@ @@ @@ @@@@ @@@@@
@@@ @@@ @@@@@@@ @@ @@@ @@ @@@@@@@ @@@ @@@
@@@ @@ @@ @@@ @@ @@ @@ @@@
@@@@@ @@@@@@ @@ @@@@@@ @@ @@@@@
@@@ @@ @@ @@ @@@ @@ @@ @@@
@@@ @@@ @@ @@ @@ @@ @@@@@@ @@@ @@@
@@@@@ @@ @@ @@ @ @@@@ @@@@@

..............
......:@@@@@@@@:......
..::::..@@........@@.:::::..
..:::::..@@..::..::..@@.::::::..
.::::::..@@............@@.:::::::.
.::::::..@@..............@@.:::::::.
.::::::::..@@............@@..::::::::.
.:::::::::...@@.@..@@..@.@@..::::::::::.
.:::::::::...:@@@..@@..@@@:..:::::::::..
............@@.@@..@@..@@.@@............
^^^~~^^~~^^@@..............@@^^^~^^^~~^^
.::::::::::@@..............@@.:::::::::.
.......:.@@.....@.....@....@@.:.......
.::....@@......@.@@@.@....@@.....::.
.:::~@@.:...:.@@...@@.:.:.@@~::::.
.::~@@@@@@@@@@.....@@@@@@@@@~::.
..:~~~~~~~:.......:~~~~~~~:..
......................
..............

-----------------------------------------

Build-Revision: $Rev: v2.1.2-Dollerup
Build-Date: $Date: 2024-11-22

Reading input file ...
Info : Running SFINCS in subgrid mode ...
Turning on process: Viscosity
Advection scheme : First-order upwind
Reading meteo data ...
Info : Preparing SFINCS grid on quadtree mesh ...
Reading QuadTree netCDF file ...
Quadtree grid info - nr_levels : 3
Quadtree grid info - x0 : 318219.0
Quadtree grid info - y0 : 5040846.
Quadtree grid info - dx : 200.0000
Quadtree grid info - dy : 200.0000
Quadtree grid info - mmax : 64
Quadtree grid info - nmax : 24
Quadtree grid info - rotation : 27.00200
Number of refinement transitions : 379
Number of active z points : 4226
Number of active u/v points : 8526
Number of cells in level 1 : 899
Number of cells in level 2 : 1749
Number of cells in level 3 : 1804
Viscosity - nuvisc = [ 0.5000000 - 2.000000 ]
Reading sub-grid netCDF file ...
Number of subgrid levels : 10
Reading water level boundaries ...

Initializing output ...

---------- Starting simulation ----------
---- Using 8 of 8 available threads -----

0% complete, - s remaining ...
5% complete, 3.9 s remaining ...
10% complete, 2.7 s remaining ...
15% complete, 2.1 s remaining ...
20% complete, 1.8 s remaining ...
25% complete, 1.8 s remaining ...
30% complete, 1.8 s remaining ...
35% complete, 1.7 s remaining ...
40% complete, 1.5 s remaining ...
45% complete, 1.3 s remaining ...
50% complete, 1.2 s remaining ...
55% complete, 1.0 s remaining ...
60% complete, 0.9 s remaining ...
65% complete, 0.8 s remaining ...
70% complete, 0.7 s remaining ...
75% complete, 0.6 s remaining ...
80% complete, 0.5 s remaining ...
85% complete, 0.4 s remaining ...
90% complete, 0.2 s remaining ...
95% complete, 0.1 s remaining ...
100% complete, 0.0 s remaining ...

---------- Simulation finished ----------

Total time : 2.503
Total simulation time : 2.433
Time in input : 0.070
Time in boundaries : 0.058 ( 2.4%)
Time in momentum : 1.781 ( 73.2%)
Time in continuity : 0.551 ( 22.6%)
Time in output : 0.023 ( 0.9%)

Average time step (s) : 3.733

---------- Closing off SFINCS -----------
Binary file added tests/data/sfincs_test_quadtree/sfincs_map.nc
Binary file not shown.
Binary file added tests/data/sfincs_test_quadtree/sfincs_subgrid.nc
Binary file not shown.
18 changes: 12 additions & 6 deletions tests/test_1model_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ def test_model_class(case):
mod.read()
# run test_model_api() method
non_compliant_list = mod._test_model_api()
# drop non-compliant variables with "results" and "mesh" in name
non_compliant_list = [
v for v in non_compliant_list if "results" not in v and "mesh" not in v
]
assert len(non_compliant_list) == 0
# pass


def test_states(mod):
Expand Down Expand Up @@ -398,20 +401,23 @@ def test_forcing_io(tmpdir):
)


def test_read_results():
root = TESTMODELDIR
@pytest.mark.parametrize("case", list(_cases.keys()))
def test_read_results(case):
root = join(TESTDATADIR, _cases[case]["example"])
mod = SfincsModel(root=root, mode="r")
assert all([v in mod.results for v in ["zs", "zsmax", "inp"]])


def test_plots(mod):
@pytest.mark.parametrize("case", list(_cases.keys()))
def test_plots(case):
root = join(TESTDATADIR, _cases[case]["example"])
mod = SfincsModel(root=root, mode="r")
mod.plot_forcing(fn_out="forcing.png")
assert isfile(join(mod.root, "figs", "forcing.png"))
mod.plot_basemap(fn_out="basemap.png")
assert isfile(join(mod.root, "figs", "basemap.png"))


@pytest.mark.parametrize("case", list(_cases.keys()))
@pytest.mark.parametrize("case", list(_cases.keys())[:1])
def test_model_build(tmpdir, case):
# compare results with model from examples folder
root = str(tmpdir.join(case))
Expand Down
40 changes: 40 additions & 0 deletions tests/test_quadtree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from os.path import join, dirname, abspath
import numpy as np
from pyproj import CRS

from hydromt_sfincs.quadtree import QuadtreeGrid

TESTDATADIR = join(dirname(abspath(__file__)), "data")

def test_quadtree_io(tmpdir):
# Initialize a QuadtreeGrid object
qtr = QuadtreeGrid()
# Read a quadtree netcdf file
qtr.read(join(TESTDATADIR, "sfincs_test_quadtree", "sfincs.nc"))
# Check the face coordinates
face_coordinates = qtr.face_coordinates
assert len(face_coordinates[0] == 4452)
# Check the msk variable
msk = qtr.data["msk"]
assert np.sum(msk.values) == 4298
# Check the crs
crs = qtr.crs
assert crs == CRS.from_epsg(32633)

# now write the quadtree to a new file
fn = tmpdir.join("sfincs_out.nc")
qtr.write(fn)

# read the new file and check the msk variable
qtr2 = QuadtreeGrid()
qtr2.read(fn)
# assert the crs is the same
assert qtr2.crs == qtr.crs
# assert the msk variable is the same
assert np.sum(qtr2.data["msk"].values) == 4298
# assert the dep variable is the same
assert np.sum(qtr.data["dep"].values) == np.sum(qtr2.data["dep"].values)




0 comments on commit 2446903

Please sign in to comment.