def project(geom, mesh, gprj, mprj, pmid): """ PROJECT: projection of GEOM/MESH objects to a 2d. plane. Modifies GPRJ, MPRJ objects "inplace". """ # Authors: Darren Engwirda proj = jigsawpy.jigsaw_prj_t() mprj.point = np.full( mesh.point.size, +0, dtype=mprj.VERT2_t) mprj.point["coord"] = \ jigsawpy.R3toS2( geom.radii, mesh.point["coord"]) proj.prjID = "stereographic" proj.radii = np.mean(geom.radii) proj.xbase = pmid[0] * np.pi / +180. proj.ybase = pmid[1] * np.pi / +180. jigsawpy.project(gprj, proj, "fwd") jigsawpy.project(mprj, proj, "fwd") return
def setgeom(): geom = jigsawpy.jigsaw_msh_t() poly = jigsawpy.jigsaw_msh_t() #------------------------------------ define JIGSAW geometry geom.mshID = "euclidean-mesh" #------------------------------------ set watershed boundary print("BUILDING MESH GEOM.") filename = os.path.join( "data", "NHD_H_0204_HU4_Shape", "Shape", "WBDHU8.shp") loadshp(filename, poly, filt_WDBHU8) poly.point["coord"] *= np.pi / +180. addpoly(geom, poly, +1) #------------------------------------ approx. stream network filename = os.path.join( "data", "namerica_rivers", "narivs.shp") loadshp(filename, poly, filt_narivs) poly.point["coord"] *= np.pi / +180. itag = innerto(poly.vert2["coord"], geom) keep = np.logical_and.reduce(( itag[poly.edge2["index"][:, 0]] > +0, itag[poly.edge2["index"][:, 1]] > +0 )) poly.edge2 = poly.edge2[keep] addline(geom, poly, +1) #------------------------------------ proj. to local 2-plane proj = jigsawpy.jigsaw_prj_t() proj.prjID = "stereographic" proj.radii = FULL_SPHERE_RADIUS proj.xbase = PROJ_CENTRE[0] * np.pi / +180. proj.ybase = PROJ_CENTRE[1] * np.pi / +180. jigsawpy.project(geom, proj, "fwd") GEOM[0] = geom # save a "pointer" return geom
def case_6_(src_path, dst_path): # DEMO-6: generate a 2-dim. grid for the Australian coastal # region, using scaled ocean-depth as a mesh-resolution # heuristic. A local stereographic projection is employed. opts = jigsawpy.jigsaw_jig_t() topo = jigsawpy.jigsaw_msh_t() geom = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() hmat = jigsawpy.jigsaw_msh_t() proj = jigsawpy.jigsaw_prj_t() #------------------------------------ setup files for JIGSAW opts.geom_file = \ os.path.join(dst_path, "geom.msh") opts.jcfg_file = \ os.path.join(dst_path, "aust.jig") opts.mesh_file = \ os.path.join(dst_path, "mesh.msh") opts.hfun_file = \ os.path.join(dst_path, "spac.msh") #------------------------------------ define JIGSAW geometry jigsawpy.loadmsh(os.path.join(src_path, "aust.msh"), geom) jigsawpy.loadmsh(os.path.join(src_path, "topo.msh"), topo) xmin = np.min(geom.point["coord"][:, 0]) ymin = np.min(geom.point["coord"][:, 1]) xmax = np.max(geom.point["coord"][:, 0]) ymax = np.max(geom.point["coord"][:, 1]) zlev = topo.value xmsk = np.logical_and(topo.xgrid > xmin, topo.xgrid < xmax) ymsk = np.logical_and(topo.ygrid > ymin, topo.ygrid < ymax) zlev = zlev[:, xmsk] zlev = zlev[ymsk, :] #------------------------------------ define spacing pattern hmat.mshID = "ellipsoid-grid" hmat.radii = np.full(+3, +6371.0, dtype=jigsawpy.jigsaw_msh_t.REALS_t) hmat.xgrid = \ topo.xgrid[xmsk] * np.pi / 180. hmat.ygrid = \ topo.ygrid[ymsk] * np.pi / 180. hmin = +1.0E+01 hmax = +1.0E+02 hmat.value = \ np.sqrt(np.maximum(-zlev, 0.)) / 0.5 hmat.value = \ np.maximum(hmat.value, hmin) hmat.value = \ np.minimum(hmat.value, hmax) hmat.slope = np.full(hmat.value.shape, +0.1500, dtype=jigsawpy.jigsaw_msh_t.REALS_t) #------------------------------------ do stereographic proj. geom.point["coord"][:, :] *= np.pi / 180. proj.prjID = 'stereographic' proj.radii = +6.371E+003 proj.xbase = \ +0.500 * (xmin + xmax) * np.pi / 180. proj.ybase = \ +0.500 * (ymin + ymax) * np.pi / 180. jigsawpy.project(geom, proj, "fwd") jigsawpy.project(hmat, proj, "fwd") jigsawpy.savemsh(opts.geom_file, geom) jigsawpy.savemsh(opts.hfun_file, hmat) #------------------------------------ set HFUN grad.-limiter jigsawpy.cmd.marche(opts, hmat) #------------------------------------ make mesh using JIGSAW opts.hfun_scal = "absolute" opts.hfun_hmax = float("inf") # null HFUN limits opts.hfun_hmin = float(+0.00) opts.mesh_dims = +2 # 2-dim. simplexes opts.mesh_eps1 = +1. ttic = time.time() jigsawpy.cmd.jigsaw(opts, mesh) ttoc = time.time() print("CPUSEC =", (ttoc - ttic)) cost = jigsawpy.triscr2( # quality metrics! mesh.point["coord"], mesh.tria3["index"]) print("TRISCR =", np.min(cost), np.mean(cost)) cost = jigsawpy.pwrscr2(mesh.point["coord"], mesh.power, mesh.tria3["index"]) print("PWRSCR =", np.min(cost), np.mean(cost)) tbad = jigsawpy.centre2(mesh.point["coord"], mesh.power, mesh.tria3["index"]) print("OBTUSE =", +np.count_nonzero(np.logical_not(tbad))) #------------------------------------ save mesh for Paraview print("Saving to ../cache/case_6a.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_6a.vtk"), mesh) print("Saving to ../cache/case_6b.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_6b.vtk"), hmat) return
def case_6_(src_path, dst_path): # DEMO-6: generate a multi-part mesh of the (contiguous) USA # using state boundaries to partition the mesh. A local # stereographic projection is employed. The domain is meshed # using uniform resolution. opts = jigsawpy.jigsaw_jig_t() geom = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() proj = jigsawpy.jigsaw_prj_t() #------------------------------------ setup files for JIGSAW opts.geom_file = \ os.path.join(src_path, "proj.msh") opts.jcfg_file = \ os.path.join(dst_path, "us48.jig") opts.mesh_file = \ os.path.join(dst_path, "mesh.msh") #------------------------------------ define JIGSAW geometry jigsawpy.loadmsh(os.path.join(src_path, "us48.msh"), geom) xmin = np.min(geom.point["coord"][:, 0]) ymin = np.min(geom.point["coord"][:, 1]) xmax = np.max(geom.point["coord"][:, 0]) ymax = np.max(geom.point["coord"][:, 1]) geom.point["coord"][:, :] *= np.pi / 180. proj.prjID = 'stereographic' proj.radii = +6.371E+003 proj.xbase = \ +0.500 * (xmin + xmax) * np.pi / 180. proj.ybase = \ +0.500 * (ymin + ymax) * np.pi / 180. jigsawpy.project(geom, proj, "fwd") jigsawpy.savemsh(opts.geom_file, geom) #------------------------------------ make mesh using JIGSAW opts.hfun_hmax = .005 opts.mesh_dims = +2 # 2-dim. simplexes opts.mesh_eps1 = +1 / 6. jigsawpy.cmd.jigsaw(opts, mesh) #------------------------------------ save mesh for Paraview print("Saving to ../cache/case_6a.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_6a.vtk"), mesh) return
def case_7_(src_path, dst_path): # DEMO-7: generate a multi-part mesh of the (contiguous) USA # using state boundaries to partition the mesh. A local # stereographic projection is employed. The domain is meshed # using uniform resolution. opts = jigsawpy.jigsaw_jig_t() geom = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() proj = jigsawpy.jigsaw_prj_t() #------------------------------------ setup files for JIGSAW opts.geom_file = \ os.path.join(dst_path, "geom.msh") opts.jcfg_file = \ os.path.join(dst_path, "us48.jig") opts.mesh_file = \ os.path.join(dst_path, "mesh.msh") #------------------------------------ define JIGSAW geometry jigsawpy.loadmsh(os.path.join(src_path, "us48.msh"), geom) xmin = np.min(geom.point["coord"][:, 0]) ymin = np.min(geom.point["coord"][:, 1]) xmax = np.max(geom.point["coord"][:, 0]) ymax = np.max(geom.point["coord"][:, 1]) geom.point["coord"][:, :] *= np.pi / 180. proj.prjID = 'stereographic' proj.radii = +6.371E+003 proj.xbase = \ +0.500 * (xmin + xmax) * np.pi / 180. proj.ybase = \ +0.500 * (ymin + ymax) * np.pi / 180. jigsawpy.project(geom, proj, "fwd") jigsawpy.savemsh(opts.geom_file, geom) #------------------------------------ make mesh using JIGSAW opts.hfun_hmax = .005 opts.mesh_dims = +2 # 2-dim. simplexes opts.mesh_eps1 = +1 / 6. ttic = time.time() jigsawpy.cmd.jigsaw(opts, mesh) ttoc = time.time() print("CPUSEC =", (ttoc - ttic)) cost = jigsawpy.triscr2( # quality metrics! mesh.point["coord"], mesh.tria3["index"]) print("TRISCR =", np.min(cost), np.mean(cost)) cost = jigsawpy.pwrscr2(mesh.point["coord"], mesh.power, mesh.tria3["index"]) print("PWRSCR =", np.min(cost), np.mean(cost)) tbad = jigsawpy.centre2(mesh.point["coord"], mesh.power, mesh.tria3["index"]) print("OBTUSE =", +np.count_nonzero(np.logical_not(tbad))) #------------------------------------ save mesh for Paraview print("Saving to ../cache/case_7a.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_7a.vtk"), mesh) return
def ex_9(): # DEMO-9: generate a 2-dim. grid for the Australian coastal # region, using scaled ocean-depth as a mesh-resolution # heuristic. A local stereographic projection is employed. dst_path = \ os.path.abspath( os.path.dirname(__file__)) src_path = \ os.path.join(dst_path, "..", "files") dst_path = \ os.path.join(dst_path, "..", "cache") opts = jigsawpy.jigsaw_jig_t() topo = jigsawpy.jigsaw_msh_t() geom = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() msph = jigsawpy.jigsaw_msh_t() hmat = jigsawpy.jigsaw_msh_t() proj = jigsawpy.jigsaw_prj_t() #------------------------------------ setup files for JIGSAW opts.geom_file = \ os.path.join(dst_path, "proj.msh") opts.jcfg_file = \ os.path.join(dst_path, "aust.jig") opts.mesh_file = \ os.path.join(dst_path, "mesh.msh") opts.hfun_file = \ os.path.join(dst_path, "spac.msh") #------------------------------------ define JIGSAW geometry jigsawpy.loadmsh(os.path.join(src_path, "aust.msh"), geom) jigsawpy.loadmsh(os.path.join(src_path, "topo.msh"), topo) xmin = np.min(geom.point["coord"][:, 0]) ymin = np.min(geom.point["coord"][:, 1]) xmax = np.max(geom.point["coord"][:, 0]) ymax = np.max(geom.point["coord"][:, 1]) zlev = topo.value xmsk = np.logical_and(topo.xgrid > xmin, topo.xgrid < xmax) ymsk = np.logical_and(topo.ygrid > ymin, topo.ygrid < ymax) zlev = zlev[:, xmsk] zlev = zlev[ymsk, :] #------------------------------------ define spacing pattern hmat.mshID = "ellipsoid-grid" hmat.radii = np.full(+3, +6371.0, dtype=jigsawpy.jigsaw_msh_t.REALS_t) hmat.xgrid = \ topo.xgrid[xmsk] * np.pi / 180. hmat.ygrid = \ topo.ygrid[ymsk] * np.pi / 180. hmin = +1.0E+01 hmax = +1.0E+02 hmat.value = \ np.sqrt(np.maximum(-zlev, 0.)) / 0.5 hmat.value = \ np.maximum(hmat.value, hmin) hmat.value = \ np.minimum(hmat.value, hmax) hmat.slope = np.full(hmat.value.shape, +0.1500, dtype=jigsawpy.jigsaw_msh_t.REALS_t) #------------------------------------ do stereographic proj. geom.point["coord"][:, :] *= np.pi / 180. proj.prjID = 'stereographic' proj.radii = +6.371E+003 proj.xbase = \ +0.500 * (xmin + xmax) * np.pi / 180. proj.ybase = \ +0.500 * (ymin + ymax) * np.pi / 180. jigsawpy.project(geom, proj, "fwd") jigsawpy.project(hmat, proj, "fwd") jigsawpy.savemsh(opts.geom_file, geom) jigsawpy.savemsh(opts.hfun_file, hmat) #------------------------------------ set HFUN grad.-limiter jigsawpy.cmd.marche(opts, hmat) #------------------------------------ make mesh using JIGSAW opts.hfun_scal = "absolute" opts.hfun_hmax = float("inf") # null HFUN limits opts.hfun_hmin = float(+0.00) opts.mesh_dims = +2 # 2-dim. simplexes opts.mesh_eps1 = +1.0E+00 # relax edge error opts.optm_iter = +32 opts.optm_qtol = +1.0E-05 jigsawpy.cmd.jigsaw(opts, mesh) #------------------------------------ do stereographic proj. msph = copy.deepcopy(mesh) jigsawpy.project(msph, proj, "inv") radii = np.full(+3, proj.radii, dtype=jigsawpy.jigsaw_msh_t.REALS_t) msph.vert3 = np.zeros((msph.vert2.size), dtype=jigsawpy.jigsaw_msh_t.VERT3_t) msph.vert3["coord"] = \ jigsawpy.S2toR3( radii, msph.point["coord"]) msph.vert2 = None #------------------------------------ save mesh for Paraview print("Saving to ../cache/case_9a.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_9a.vtk"), mesh) print("Saving to ../cache/case_9b.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_9b.vtk"), hmat) print("Saving to ../cache/case_9c.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_9c.vtk"), msph) return
def setspac(): spac_wbd = 2.5 # watershed km spac_pfz = 1. # PFZ km elev_pfz = 25. # PFZ elev. thresh dhdx_lim = 0.0625 # |dH/dx| thresh bbox = [-77.5, 37.5, -72.5, 42.5] spac = jigsawpy.jigsaw_msh_t() opts = jigsawpy.jigsaw_jig_t() poly = jigsawpy.jigsaw_msh_t() geom = GEOM[0] opts.jcfg_file = os.path.join(TDIR, "opts.jig") opts.hfun_file = os.path.join(TDIR, "spac.msh") #------------------------------------ define spacing pattern print("BUILDING MESH SPAC.") print("Loading elevation assets...") data = nc.Dataset(os.path.join( "data", "etopo_gebco", "etopo_gebco_tiled.nc"), "r") zlev = np.array(data.variables["z"]) spac.mshID = "ellipsoid-grid" # use elev. grid spac.radii = np.full( +3, FULL_SPHERE_RADIUS, dtype=spac.REALS_t) spac.xgrid = np.array( data.variables["x"][:], dtype=spac.REALS_t) spac.ygrid = np.array( data.variables["y"][:], dtype=spac.REALS_t) xmsk = np.logical_and.reduce(( spac.xgrid >= bbox[0] - 1., spac.xgrid <= bbox[2] + 1. )) ymsk = np.logical_and.reduce(( spac.ygrid >= bbox[1] - 1., spac.ygrid <= bbox[3] + 1. )) spac.xgrid = spac.xgrid[xmsk] # only bbox part spac.ygrid = spac.ygrid[ymsk] zlev = zlev[:, xmsk] zlev = zlev[ymsk, :] spac.xgrid *= np.pi / +180. spac.ygrid *= np.pi / +180. xgrd, ygrd = np.meshgrid(spac.xgrid, spac.ygrid) spac.value = +10. * np.ones( (spac.ygrid.size, spac.xgrid.size)) grid = np.concatenate(( # to [x, y] list xgrd.reshape((xgrd.size, +1)), ygrd.reshape((ygrd.size, +1))), axis=+1) #------------------------------------ push watershed spacing print("Compute watersheds h(x)...") shed = np.full( (grid.shape[0]), False, dtype=bool) filename = os.path.join( "data", "NHD_H_0204_HU4_Shape", "Shape", "WBDHU4.shp") loadshp(filename, poly) poly.point["coord"] *= np.pi / +180. mask, _ = inpoly2( grid, poly.point["coord"], poly.edge2["index"]) shed[mask] = True shed = np.reshape(shed, spac.value.shape) spac.value[shed] = \ np.minimum(spac_wbd, spac.value[shed]) #------------------------------------ partially flooded zone mask = np.logical_and(shed, zlev < elev_pfz) spac.value[mask] = \ np.minimum(spac_pfz, spac.value[mask]) #------------------------------------ push |DH/DX| threshold print("Impose |DH/DX| threshold...") spac.slope = np.full( spac.value.shape, dhdx_lim, dtype=spac.REALS_t) jigsawpy.savemsh(opts.hfun_file, spac, "precision = 9") jigsawpy.cmd.marche(opts, spac) spac.slope = \ np.empty((+0), dtype=spac.REALS_t) #------------------------------------ proj. to local 2-plane proj = jigsawpy.jigsaw_prj_t() proj.prjID = "stereographic" proj.radii = FULL_SPHERE_RADIUS proj.xbase = PROJ_CENTRE[0] * np.pi / +180. proj.ybase = PROJ_CENTRE[1] * np.pi / +180. jigsawpy.project(spac, proj, "fwd") SPAC[0] = spac # save a "pointer" return spac
def ex_8(): # DEMO-8: build regional meshes via stereographic projection dst_path = \ os.path.abspath( os.path.dirname(__file__)) src_path = \ os.path.join(dst_path, "..", "files") dst_path = \ os.path.join(dst_path, "..", "cache") opts = jigsawpy.jigsaw_jig_t() topo = jigsawpy.jigsaw_msh_t() geom = jigsawpy.jigsaw_msh_t() hfun = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() msph = jigsawpy.jigsaw_msh_t() proj = jigsawpy.jigsaw_prj_t() #------------------------------------ setup files for JIGSAW opts.geom_file = \ os.path.join(dst_path, "proj.msh") opts.jcfg_file = \ os.path.join(dst_path, "paci.jig") opts.mesh_file = \ os.path.join(dst_path, "mesh.msh") opts.hfun_file = \ os.path.join(dst_path, "spac.msh") #------------------------------------ define JIGSAW geometry jigsawpy.loadmsh(os.path.join( src_path, "paci.msh"), geom) jigsawpy.loadmsh(os.path.join( src_path, "topo.msh"), topo) xmin = np.min(geom.point["coord"][:, 0]) xmax = np.max(geom.point["coord"][:, 0]) xlon = np.linspace(xmin, xmax, 100) ymin = np.min(geom.point["coord"][:, 1]) ymax = np.max(geom.point["coord"][:, 1]) ylat = np.linspace(ymin, ymax, 100) #------------------------------------ define spacing pattern hfun.mshID = "ellipsoid-grid" hfun.radii = np.full( +3, 6.371E+003, dtype=jigsawpy.jigsaw_msh_t.REALS_t) hfun.xgrid = xlon * np.pi / 180. hfun.ygrid = ylat * np.pi / 180. hfun.value = np.full( (ylat.size, xlon.size), 200., dtype=jigsawpy.jigsaw_msh_t.REALS_t) #------------------------------------ do stereographic proj. geom.point["coord"][:, :] *= np.pi / 180. proj.prjID = 'stereographic' proj.radii = +6.371E+003 proj.xbase = \ +0.500 * (xmin + xmax) * np.pi / 180. proj.ybase = \ +0.500 * (ymin + ymax) * np.pi / 180. jigsawpy.project(geom, proj, "fwd") jigsawpy.project(hfun, proj, "fwd") jigsawpy.savemsh(opts.geom_file, geom) jigsawpy.savemsh(opts.hfun_file, hfun) #------------------------------------ make mesh using JIGSAW opts.hfun_scal = "absolute" opts.hfun_hmax = float("inf") # null HFUN limits opts.hfun_hmin = float(+0.00) opts.mesh_dims = +2 # 2-dim. simplexes opts.mesh_eps1 = +1.0E+00 # relax edge error opts.optm_iter = +32 opts.optm_qtol = +1.0E-05 jigsawpy.cmd.jigsaw(opts, mesh) #------------------------------------ do stereographic proj. msph = copy.deepcopy(mesh) jigsawpy.project(msph, proj, "inv") radii = np.full( +3, proj.radii, dtype=jigsawpy.jigsaw_msh_t.REALS_t) msph.vert3 = np.zeros( (msph.vert2.size), dtype=jigsawpy.jigsaw_msh_t.VERT3_t) msph.vert3["coord"] = \ jigsawpy.S2toR3( radii, msph.point["coord"]) msph.vert2 = None #------------------------------------ save mesh for Paraview print("Saving to ../cache/case_8a.vtk") jigsawpy.savevtk(os.path.join( dst_path, "case_8a.vtk"), mesh) print("Saving to ../cache/case_8b.vtk") jigsawpy.savevtk(os.path.join( dst_path, "case_8b.vtk"), hfun) print("Saving to ../cache/case_8c.vtk") jigsawpy.savevtk(os.path.join( dst_path, "case_8c.vtk"), msph) return