def case_8_(src_path, dst_path): opts = jigsawpy.jigsaw_jig_t() geom = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() #------------------------------------ make mesh using JIGSAW print("Call libJIGSAW: case 8a.") jigsawpy.loadmsh(os.path.join(src_path, "eight.msh"), geom) opts.hfun_hmax = 0.04 # set HFUN limits opts.mesh_kern = "delfront" # DELFRONT kernel opts.mesh_dims = +3 jigsawpy.lib.jigsaw(opts, geom, mesh) print("Saving case_8a.vtk file.") jigsawpy.savevtk(os.path.join(dst_path, "case_8a.vtk"), geom) print("Saving case_8b.vtk file.") jigsawpy.savevtk(os.path.join(dst_path, "case_8b.vtk"), mesh) return
def case_1a(src_path, dst_path): opts = jigsawpy.jigsaw_jig_t() geom = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() #------------------------------------ define JIGSAW geometry geom.mshID = "euclidean-mesh" geom.ndims = +3 geom.vert3 = np.array([ # list of xy "node" coordinates ((0, 0, 0), 0), ((3, 0, 0), 0), ((3, 3, 0), 0), ((0, 3, 0), 0), ((0, 0, 3), 0), ((3, 0, 3), 0), ((3, 3, 3), 0), ((0, 3, 3), 0)], dtype=geom.VERT3_t) geom.tria3 = np.array([ # list of "tria" between points ((0, 1, 2), 0), ((0, 2, 3), 0), ((4, 5, 6), 0), ((4, 6, 7), 0), ((0, 1, 5), 0), ((0, 5, 4), 0), ((1, 2, 6), 0), ((1, 6, 5), 0), ((2, 3, 7), 0), ((2, 7, 6), 0), ((3, 7, 4), 0), ((3, 4, 0), 0)], dtype=geom.TRIA3_t) #------------------------------------ build mesh via JIGSAW! print("Call libJIGSAW: case 1a.") opts.hfun_hmax = 0.08 # push HFUN limits opts.mesh_dims = +3 # 3-dim. simplexes opts.mesh_top1 = True # for sharp feat's opts.geom_feat = True jigsawpy.lib.jigsaw(opts, geom, mesh) scr3 = jigsawpy.triscr3( # "quality" metric mesh.point["coord"], mesh.tria4["index"]) print("Saving case_1a.vtk file.") jigsawpy.savevtk(os.path.join( dst_path, "case_1a.vtk"), mesh) return
def case_7_(src_path, dst_path): opts = jigsawpy.jigsaw_jig_t() geom = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() #------------------------------------ make mesh using JIGSAW print("Call libJIGSAW: case 7a.") jigsawpy.loadmsh(os.path.join(src_path, "wheel.msh"), geom) opts.hfun_hmax = 0.03 # set HFUN limits opts.mesh_kern = "delfront" # DELFRONT kernel opts.mesh_dims = +2 opts.geom_feat = True opts.mesh_top1 = True opts.mesh_top2 = True jigsawpy.lib.jigsaw(opts, geom, mesh) print("Saving case_7a.vtk file.") jigsawpy.savevtk(os.path.join(dst_path, "case_7a.vtk"), geom) print("Saving case_7b.vtk file.") jigsawpy.savevtk(os.path.join(dst_path, "case_7b.vtk"), mesh) return
def make_mesh(): opts = jigsawpy.jigsaw_jig_t() # jigsaw data structures geom = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() opts.geom_file = "tmp/geom.msh" # setup jigsaw files opts.jcfg_file = "tmp/opts.jig" opts.mesh_file = "out/mesh.msh" geom.mshID = "ellipsoid-mesh" # a simple "unit" sphere geom.radii = np.ones(+3) jigsawpy.savemsh(opts.geom_file, geom) opts.verbosity = +1 # setup user-defined opt opts.optm_iter = +512 opts.optm_qtol = +1.E-08 opts.optm_kern = "odt+dqdx" # opts.optm_kern = "cvt+dqdx" ttic = time.time() jigsawpy.cmd.icosahedron(opts, 6, mesh) # mesh with n bisections # keep = mesh.tria3["IDtag"] == +1 # only keep single face? # mesh.tria3 = mesh.tria3[keep] ttoc = time.time() print("CPUSEC =", (ttoc - ttic)) jigsawpy.savevtk("mesh.vtk", mesh) # to open in paraview... return
def case_0a(src_path, dst_path): opts = jigsawpy.jigsaw_jig_t() geom = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() #------------------------------------ define JIGSAW geometry geom.mshID = "euclidean-mesh" geom.ndims = +2 geom.vert2 = np.array([ # list of xy "node" coordinate ((0, 0), 0), # outer square ((9, 0), 0), ((9, 9), 0), ((0, 9), 0), ((4, 4), 0), # inner square ((5, 4), 0), ((5, 5), 0), ((4, 5), 0)], dtype=geom.VERT2_t) geom.edge2 = np.array([ # list of "edges" between vert ((0, 1), 0), # outer square ((1, 2), 0), ((2, 3), 0), ((3, 0), 0), ((4, 5), 0), # inner square ((5, 6), 0), ((6, 7), 0), ((7, 4), 0)], dtype=geom.EDGE2_t) #------------------------------------ build mesh via JIGSAW! print("Call libJIGSAW: case 0a.") opts.hfun_hmax = 0.05 # push HFUN limits opts.mesh_dims = +2 # 2-dim. simplexes opts.optm_qlim = +.95 opts.mesh_top1 = True # for sharp feat's opts.geom_feat = True jigsawpy.lib.jigsaw(opts, geom, mesh) scr2 = jigsawpy.triscr2( # "quality" metric mesh.point["coord"], mesh.tria3["index"]) print("Saving case_0a.vtk file.") jigsawpy.savevtk(os.path.join( dst_path, "case_0a.vtk"), mesh) return
def _opts(self): try: return self.__opts except AttributeError: self.__opts = jigsawpy.jigsaw_jig_t() self.__opts.mesh_dims = self.Geom._ndim if self.Hfun is not None: self.__opts.hfun_scal = self.Hfun._hfun_scal return self.__opts
def __get__(self, obj, val): opts = obj.__dict__.get('opts') if opts is None: opts = jigsaw_jig_t() opts.mesh_dims = +2 opts.optm_tria = True opts.hfun_scal = 'absolute' obj.__dict__['opts'] = opts return opts
def case_6_(src_path, dst_path): opts = jigsawpy.jigsaw_jig_t() geom = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() #------------------------------------ setup files for JIGSAW opts.geom_file = \ os.path.join(src_path, "piece.msh") opts.jcfg_file = \ os.path.join(dst_path, "piece.jig") opts.mesh_file = \ os.path.join(dst_path, "piece.msh") #------------------------------------ make mesh using JIGSAW jigsawpy.loadmsh(opts.geom_file, geom) print("Saving case_6a.vtk file.") jigsawpy.savevtk(os.path.join(dst_path, "case_6a.vtk"), geom) opts.hfun_hmax = 0.03 # set HFUN limits opts.mesh_kern = "delfront" # DELFRONT kernel opts.mesh_dims = +2 jigsawpy.cmd.jigsaw(opts, mesh) print("Saving case_6b.vtk file.") jigsawpy.savevtk(os.path.join(dst_path, "case_6b.vtk"), mesh) #------------------------------------ make mesh using JIGSAW opts.hfun_hmax = 0.03 # set HFUN limits opts.mesh_kern = "delfront" # DELFRONT kernel opts.mesh_dims = +2 opts.geom_feat = True opts.mesh_top1 = True jigsawpy.cmd.jigsaw(opts, mesh) print("Saving case_6c.vtk file.") jigsawpy.savevtk(os.path.join(dst_path, "case_6c.vtk"), mesh) return
def case_2_(src_path, dst_path): opts = jigsawpy.jigsaw_jig_t() mesh = jigsawpy.jigsaw_msh_t() #------------------------------------ setup files for JIGSAW opts.geom_file = \ os.path.join(src_path, "lakes.msh") opts.jcfg_file = \ os.path.join(dst_path, "lakes.jig") opts.mesh_file = \ os.path.join(dst_path, "lakes.msh") #------------------------------------ make mesh using JIGSAW opts.mesh_kern = "delaunay" # DELAUNAY kernel opts.mesh_dims = +2 opts.mesh_top1 = True # mesh sharp feat. opts.geom_feat = True opts.optm_iter = +0 opts.hfun_hmax = 0.02 jigsawpy.cmd.jigsaw(opts, mesh) print("Saving case_2a.vtk file.") jigsawpy.savevtk(os.path.join(dst_path, "case_2a.vtk"), mesh) #------------------------------------ make mesh using JIGSAW opts.mesh_kern = "delfront" # DELFRONT kernel opts.mesh_dims = +2 opts.mesh_top1 = True # mesh sharp feat. opts.geom_feat = True opts.optm_iter = +0 opts.hfun_hmax = 0.02 jigsawpy.cmd.jigsaw(opts, mesh) print("Saving case_2b.vtk file.") jigsawpy.savevtk(os.path.join(dst_path, "case_2b.vtk"), mesh) return
def _run_cmdsaw(self): msg = f'_run_cmdsaw()' self.logger.debug(msg) # init tmpfiles self.logger.debug(f'init tmpfiles') mesh_file = tempfile.NamedTemporaryFile(prefix=tmpdir, suffix='.msh') hmat_file = tempfile.NamedTemporaryFile(prefix=tmpdir, suffix='.msh') geom_file = tempfile.NamedTemporaryFile(prefix=tmpdir, suffix='.msh') jcfg_file = tempfile.NamedTemporaryFile(prefix=tmpdir, suffix='.jig') # dump data to tempfiles jigsawpy.savemsh(hmat_file.name, self.hfun) jigsawpy.savemsh(geom_file.name, self.geom) # init opts opts = jigsaw_jig_t() opts.mesh_file = mesh_file.name opts.hfun_file = hmat_file.name opts.geom_file = geom_file.name opts.jcfg_file = jcfg_file.name # additional configuration options opts.verbosity = self.verbosity opts.mesh_dims = +2 # NOTE: Hardcoded value opts.hfun_scal = 'absolute' opts.optm_tria = True # NOTE: Hardcoded value opts.optm_qlim = self.optm_qlim if self.hmin_is_absolute_limit: opts.hfun_hmin = self.hmin else: opts.hfun_hmin = np.min(self.hfun.value) if self.hmax_is_absolute_limit: opts.hfun_hmax = self.hmax else: opts.hfun_hmax = np.max(self.hfun.value) # init outputmesh mesh = jigsaw_msh_t() # call jigsaw self.logger.debug('call cmdsaw') jigsawpy.cmd.jigsaw(opts, mesh) # cleanup temporary files for tmpfile in (mesh_file, hmat_file, geom_file, jcfg_file): del (tmpfile) self.__output_mesh = mesh
def _jigsaw_hmat_worker(path, window, hmin, hmax, geom): geom = None raster = Raster(path) x = raster.get_x(window) y = raster.get_y(window) _y = np.repeat(np.min(y), len(x)) _x = np.repeat(np.min(x), len(y)) _tx = utm.from_latlon(_y, x)[0] _ty = np.flip(utm.from_latlon(y, _x)[1]) hmat = jigsaw_msh_t() hmat.mshID = "euclidean-grid" hmat.ndims = +2 hmat.xgrid = _tx.astype(jigsaw_msh_t.REALS_t) hmat.ygrid = _ty.astype(jigsaw_msh_t.REALS_t) hmat.value = np.flipud( raster.get_values(band=1, window=window) ).astype(jigsaw_msh_t.REALS_t) # init opts opts = jigsaw_jig_t() # additional configuration options opts.verbosity = 1 opts.mesh_dims = 2 opts.hfun_scal = 'absolute' opts.optm_tria = True if hmin is not None: opts.hfun_hmin = hmin else: opts.hfun_hmin = np.min(hmat.value) if hmax is not None: opts.hfun_hmax = hmax else: opts.hfun_hmax = np.max(hmat.value) # output mesh mesh = jigsaw_msh_t() # call jigsaw to create local mesh libsaw.jigsaw( opts, geom, mesh, hfun=hmat ) breakpoint() return mesh
def jigsaw_driver(cellWidth, lon, lat): ''' A function for building a jigsaw mesh Parameters ---------- cellWidth : ndarray The size of each cell in the resulting mesh as a function of space lon, lat : ndarray The lon. and lat. of each point in the cellWidth array ''' # Authors # ------- # Mark Petersen, Phillip Wolfram, Xylar Asay-Davis # setup files for JIGSAW opts = jigsawpy.jigsaw_jig_t() opts.geom_file = 'mesh.msh' opts.jcfg_file = 'mesh.jig' opts.mesh_file = 'mesh-MESH.msh' opts.hfun_file = 'mesh-HFUN.msh' # save HFUN data to file hmat = jigsawpy.jigsaw_msh_t() hmat.mshID = 'ELLIPSOID-GRID' hmat.xgrid = numpy.radians(lon) hmat.ygrid = numpy.radians(lat) hmat.value = cellWidth jigsawpy.savemsh(opts.hfun_file, hmat) # define JIGSAW geometry geom = jigsawpy.jigsaw_msh_t() geom.mshID = 'ELLIPSOID-MESH' geom.radii = 6371.*numpy.ones(3, float) jigsawpy.savemsh(opts.geom_file, geom) # build mesh via JIGSAW! mesh = jigsawpy.jigsaw_msh_t() opts.hfun_scal = 'absolute' opts.hfun_hmax = float("inf") opts.hfun_hmin = 0.0 opts.mesh_dims = +2 # 2-dim. simplexes opts.optm_qlim = 0.9375 opts.verbosity = +1 jigsawpy.cmd.jigsaw(opts)
def setopts(): opts = jigsawpy.jigsaw_jig_t() #------------------------------------ define JIGSAW useropts opts.hfun_scal = "absolute" opts.hfun_hmax = float("inf") # null spacing lim opts.hfun_hmin = float(+0.00) opts.mesh_dims = +2 # 2-dim. simplexes opts.bisection = -1 # call heutristic! opts.optm_qlim = +9.5E-01 # tighter opt. tol opts.optm_iter = +32 opts.optm_qtol = +1.0E-05 return opts
def setopts(): opts = jigsawpy.jigsaw_jig_t() #------------------------------------ define JIGSAW useropts opts.hfun_scal = "absolute" opts.hfun_hmax = float("inf") # null spacing lim opts.hfun_hmin = float(+0.00) opts.mesh_dims = +2 # 2-dim. simplexes opts.bisection = +0 # just single-lev. opts.mesh_rad2 = +1.20 opts.mesh_eps1 = +0.50 # opts.optm_kern = "cvt+dqdx" opts.optm_qlim = +9.5E-01 # tighter opt. tol opts.optm_iter = +64 opts.optm_qtol = +1.0E-05 return opts
def jigsaw_gen_icos_grid(basename="mesh", level=4): # setup files for JIGSAW opts = jig.jigsaw_jig_t() icos = jig.jigsaw_msh_t() geom = jig.jigsaw_msh_t() opts.geom_file = basename + '.msh' opts.jcfg_file = basename + '.jig' opts.mesh_file = basename + '-MESH.msh' geom.mshID = "ellipsoid-mesh" geom.radii = np.full(3, 1.000E+000, dtype=geom.REALS_t) jig.savemsh(opts.geom_file, geom) opts.hfun_hmax = +1. opts.mesh_dims = +2 # 2-dim. simplexes opts.optm_iter = +512 opts.optm_qtol = +1.0E-06 jig.cmd.icosahedron(opts, level, icos) return opts.mesh_file
def jigsaw_driver(cellWidth, x, y, on_sphere=True, earth_radius=6371.0e3, geom_points=None, geom_edges=None, logger=None): """ A function for building a jigsaw mesh Parameters ---------- cellWidth : ndarray The size of each cell in the resulting mesh as a function of space x, y : ndarray The x and y coordinates of each point in the cellWidth array (lon and lat for spherical mesh) on_sphere : logical, optional Whether this mesh is spherical or planar earth_radius : float, optional Earth radius in meters geom_points : ndarray, optional list of point coordinates for bounding polygon for planar mesh geom_edges : ndarray, optional list of edges between points in geom_points that define the bounding polygon logger : logging.Logger, optional A logger for the output if not stdout """ # Authors # ------- # Mark Petersen, Phillip Wolfram, Xylar Asay-Davis # setup files for JIGSAW opts = jigsawpy.jigsaw_jig_t() opts.geom_file = 'mesh.msh' opts.jcfg_file = 'mesh.jig' opts.mesh_file = 'mesh-MESH.msh' opts.hfun_file = 'mesh-HFUN.msh' # save HFUN data to file hmat = jigsawpy.jigsaw_msh_t() if on_sphere: hmat.mshID = 'ELLIPSOID-GRID' hmat.xgrid = numpy.radians(x) hmat.ygrid = numpy.radians(y) else: hmat.mshID = 'EUCLIDEAN-GRID' hmat.xgrid = x hmat.ygrid = y hmat.value = cellWidth jigsawpy.savemsh(opts.hfun_file, hmat) # define JIGSAW geometry geom = jigsawpy.jigsaw_msh_t() if on_sphere: geom.mshID = 'ELLIPSOID-MESH' geom.radii = earth_radius * 1e-3 * numpy.ones(3, float) else: geom.mshID = 'EUCLIDEAN-MESH' geom.vert2 = geom_points geom.edge2 = geom_edges jigsawpy.savemsh(opts.geom_file, geom) # build mesh via JIGSAW! opts.hfun_scal = 'absolute' opts.hfun_hmax = float("inf") opts.hfun_hmin = 0.0 opts.mesh_dims = +2 # 2-dim. simplexes opts.optm_qlim = 0.9375 opts.verbosity = +1 savejig(opts.jcfg_file, opts) check_call(['jigsaw', opts.jcfg_file], logger=logger)
def case_2_(src_path, dst_path): # DEMO-2: generate a regionally-refined global grid with a # high-resolution 37.5km patch embedded in a uniform 150km # background grid. 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() #------------------------------------ setup files for JIGSAW opts.geom_file = \ os.path.join(src_path, "eSPH.msh") opts.jcfg_file = \ os.path.join(dst_path, "eSPH.jig") opts.mesh_file = \ os.path.join(dst_path, "mesh.msh") opts.hfun_file = \ os.path.join(dst_path, "spac.msh") #------------------------------------ define JIGSAW geometry geom.mshID = "ellipsoid-mesh" geom.radii = np.full(3, 6.371E+003, dtype=geom.REALS_t) jigsawpy.savemsh(opts.geom_file, geom) #------------------------------------ define spacing pattern hfun.mshID = "ellipsoid-grid" hfun.radii = geom.radii hfun.xgrid = np.linspace(-1. * np.pi, +1. * np.pi, 360) hfun.ygrid = np.linspace(-.5 * np.pi, +.5 * np.pi, 180) xmat, ymat = \ np.meshgrid(hfun.xgrid, hfun.ygrid) hfun.value = +150. - 112.5 * np.exp(-(+1.5 * (xmat + 1.0)**2 + +1.5 * (ymat - 0.5)**2)**4) 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.optm_qlim = +9.5E-01 # tighter opt. tol opts.optm_iter = +32 opts.optm_qtol = +1.0E-05 jigsawpy.cmd.tetris(opts, 3, mesh) scr2 = jigsawpy.triscr2( # "quality" metric mesh.point["coord"], mesh.tria3["index"]) #------------------------------------ save mesh for Paraview jigsawpy.loadmsh(os.path.join(src_path, "topo.msh"), topo) #------------------------------------ a very rough land mask apos = jigsawpy.R3toS2(geom.radii, mesh.point["coord"][:]) apos = apos * 180. / np.pi zfun = interpolate.RectBivariateSpline(topo.ygrid, topo.xgrid, topo.value) mesh.value = zfun(apos[:, 1], apos[:, 0], grid=False) cell = mesh.tria3["index"] zmsk = \ mesh.value[cell[:, 0]] + \ mesh.value[cell[:, 1]] + \ mesh.value[cell[:, 2]] zmsk = zmsk / +3.0 mesh.tria3 = mesh.tria3[zmsk < +0.] print("Saving to ../cache/case_2a.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_2a.vtk"), mesh) print("Saving to ../cache/case_2b.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_2b.vtk"), hfun) return
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_2_(src_path, dst_path): # DEMO-2: generate a regionally-refined global grid with a # high-resolution 37.5km patch embedded in a uniform 150km # background grid. 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() #------------------------------------ setup files for JIGSAW opts.geom_file = \ os.path.join(dst_path, "geom.msh") opts.jcfg_file = \ os.path.join(dst_path, "opts.jig") opts.mesh_file = \ os.path.join(dst_path, "mesh.msh") opts.hfun_file = \ os.path.join(dst_path, "spac.msh") #------------------------------------ define JIGSAW geometry geom.mshID = "ellipsoid-mesh" geom.radii = np.full(3, 6.371E+003, dtype=geom.REALS_t) jigsawpy.savemsh(opts.geom_file, geom) #------------------------------------ define spacing pattern hfun.mshID = "ellipsoid-grid" hfun.radii = geom.radii hfun.xgrid = np.linspace(-1. * np.pi, +1. * np.pi, 360) hfun.ygrid = np.linspace(-.5 * np.pi, +.5 * np.pi, 180) xmat, ymat = \ np.meshgrid(hfun.xgrid, hfun.ygrid) hfun.value = +150. - 112.5 * np.exp(-(+1.5 * (xmat + 1.0)**2 + +1.5 * (ymat - 0.5)**2)**4) 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.optm_qlim = +9.5E-01 # tighter opt. tol opts.optm_iter = +32 opts.optm_qtol = +1.0E-05 # opts.optm_kern = "cvt+dqdx" rbar = np.mean(geom.radii) # bisect heuristic hbar = np.mean(hfun.value) nlev = round(math.log2(rbar / math.sin(.4 * math.pi) / hbar)) ttic = time.time() jigsawpy.cmd.tetris(opts, nlev - 1, mesh) ttoc = time.time() print("CPUSEC =", (ttoc - ttic)) print("BISECT =", +nlev) 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))) ndeg = jigsawpy.trideg2(mesh.point["coord"], mesh.tria3["index"]) print("TOPOL. =", +np.count_nonzero(ndeg == +6) / ndeg.size) #------------------------------------ save mesh for Paraview jigsawpy.loadmsh(os.path.join(src_path, "topo.msh"), topo) #------------------------------------ a very rough land mask apos = jigsawpy.R3toS2(geom.radii, mesh.point["coord"][:]) apos = apos * 180. / np.pi zfun = interpolate.RectBivariateSpline(topo.ygrid, topo.xgrid, topo.value) mesh.value = zfun(apos[:, 1], apos[:, 0], grid=False) cell = mesh.tria3["index"] zmsk = \ mesh.value[cell[:, 0]] + \ mesh.value[cell[:, 1]] + \ mesh.value[cell[:, 2]] zmsk = zmsk / +3.0 mesh.tria3 = mesh.tria3[zmsk < +0.] print("Saving to ../cache/case_2a.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_2a.vtk"), mesh) print("Saving to ../cache/case_2b.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_2b.vtk"), hfun) return
def case_3_(src_path, dst_path): opts = jigsawpy.jigsaw_jig_t() geom = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() #------------------------------------ setup files for JIGSAW opts.geom_file = \ os.path.join(src_path, "bunny.msh") opts.jcfg_file = \ os.path.join(dst_path, "bunny.jig") opts.mesh_file = \ os.path.join(dst_path, "bunny.msh") #------------------------------------ make mesh using JIGSAW jigsawpy.loadmsh( opts.geom_file, geom) print("Saving case_3a.vtk file.") jigsawpy.savevtk(os.path.join( dst_path, "case_3a.vtk"), geom) opts.mesh_kern = "delaunay" # DELAUNAY kernel opts.mesh_dims = +2 opts.optm_iter = +0 opts.hfun_hmax = 0.03 jigsawpy.cmd.jigsaw(opts, mesh) scr2 = jigsawpy.triscr2( # "quality" metric mesh.point["coord"], mesh.tria3["index"]) print("Saving case_3b.vtk file.") jigsawpy.savevtk(os.path.join( dst_path, "case_3b.vtk"), mesh) #------------------------------------ make mesh using JIGSAW opts.mesh_kern = "delfront" # DELFRONT kernel opts.mesh_dims = +2 opts.optm_iter = +0 opts.hfun_hmax = 0.03 jigsawpy.cmd.jigsaw(opts, mesh) scr2 = jigsawpy.triscr2( # "quality" metric mesh.point["coord"], mesh.tria3["index"]) print("Saving case_3c.vtk file.") jigsawpy.savevtk(os.path.join( dst_path, "case_3c.vtk"), mesh) 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_1b(src_path, dst_path): opts = jigsawpy.jigsaw_jig_t() geom = jigsawpy.jigsaw_msh_t() hmat = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() #------------------------------------ define JIGSAW geometry geom.mshID = "euclidean-mesh" geom.ndims = +3 geom.vert3 = np.array([ # list of xy "node" coordinates ((0, 0, 0), 0), ((3, 0, 0), 0), ((3, 3, 0), 0), ((0, 3, 0), 0), ((0, 0, 3), 0), ((3, 0, 3), 0), ((3, 3, 3), 0), ((0, 3, 3), 0)], dtype=geom.VERT3_t) geom.tria3 = np.array([ # list of "tria" between points ((0, 1, 2), 0), ((0, 2, 3), 0), ((4, 5, 6), 0), ((4, 6, 7), 0), ((0, 1, 5), 0), ((0, 5, 4), 0), ((1, 2, 6), 0), ((1, 6, 5), 0), ((2, 3, 7), 0), ((2, 7, 6), 0), ((3, 7, 4), 0), ((3, 4, 0), 0)], dtype=geom.TRIA3_t) #------------------------------------ compute HFUN over GEOM xgeo = geom.vert3["coord"][:, 0] ygeo = geom.vert3["coord"][:, 1] zgeo = geom.vert3["coord"][:, 2] xpos = np.linspace( xgeo.min(), xgeo.max(), 32) ypos = np.linspace( ygeo.min(), ygeo.max(), 16) zpos = np.linspace( zgeo.min(), zgeo.max(), 64) xmat, ymat, zmat = \ np.meshgrid(xpos, ypos, zpos) hfun = 0.4 - 0.3 * np.exp( - 2.0 * (xmat - 1.50) ** 2 - 2.0 * (ymat - 1.50) ** 2 - 2.0 * (zmat - 1.50) ** 2) hmat.mshID = "euclidean-grid" hmat.ndims = +3 hmat.xgrid = np.array( xpos, dtype=hmat.REALS_t) hmat.ygrid = np.array( ypos, dtype=hmat.REALS_t) hmat.zgrid = np.array( zpos, dtype=hmat.REALS_t) hmat.value = np.array( hfun, dtype=hmat.REALS_t) #------------------------------------ build mesh via JIGSAW! print("Call libJIGSAW: case 1b.") opts.hfun_scal = "absolute" opts.hfun_hmax = float("inf") # null HFUN limits opts.hfun_hmin = float(+0.00) opts.mesh_dims = +3 # 3-dim. simplexes opts.mesh_top1 = True # for sharp feat's opts.geom_feat = True jigsawpy.lib.jigsaw(opts, geom, mesh, None, hmat) print("Saving case_1b.vtk file.") jigsawpy.savevtk(os.path.join( dst_path, "case_1b.vtk"), mesh) return
def case_4_(src_path, dst_path): # DEMO-4: generate a multi-resolution mesh, via local refin- # ement along coastlines and shallow ridges. Global grid # resolution is 150KM, background resolution is 67KM and the # min. adaptive resolution is 33KM. 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() #------------------------------------ setup files for JIGSAW opts.geom_file = \ os.path.join(src_path, "eSPH.msh") opts.jcfg_file = \ os.path.join(dst_path, "eSPH.jig") opts.mesh_file = \ os.path.join(dst_path, "mesh.msh") opts.hfun_file = \ os.path.join(dst_path, "spac.msh") #------------------------------------ define JIGSAW geometry geom.mshID = "ellipsoid-mesh" geom.radii = np.full(3, 6.371E+003, dtype=geom.REALS_t) jigsawpy.savemsh(opts.geom_file, geom) #------------------------------------ define spacing pattern jigsawpy.loadmsh(os.path.join(src_path, "topo.msh"), topo) hmat.mshID = "ellipsoid-grid" hmat.radii = geom.radii hmat.xgrid = topo.xgrid * np.pi / 180. hmat.ygrid = topo.ygrid * np.pi / 180. hfn0 = +150. # global spacing hfn2 = +33. # adapt. spacing hfn3 = +67. # arctic spacing hmat.value = np.sqrt(np.maximum(-topo.value, 0.0)) hmat.value = \ np.maximum(hmat.value, hfn2) hmat.value = \ np.minimum(hmat.value, hfn3) mask = hmat.ygrid < 40. * np.pi / 180. hmat.value[mask] = hfn0 #------------------------------------ set HFUN grad.-limiter hmat.slope = np.full( # |dH/dx| limits topo.value.shape, +0.050, dtype=hmat.REALS_t) jigsawpy.savemsh(opts.hfun_file, hmat) 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.optm_qlim = +9.5E-01 # tighter opt. tol opts.optm_iter = +32 opts.optm_qtol = +1.0E-05 jigsawpy.cmd.tetris(opts, 3, mesh) scr2 = jigsawpy.triscr2( # "quality" metric mesh.point["coord"], mesh.tria3["index"]) #------------------------------------ save mesh for Paraview apos = jigsawpy.R3toS2(geom.radii, mesh.point["coord"][:]) apos = apos * 180. / np.pi zfun = interpolate.RectBivariateSpline(topo.ygrid, topo.xgrid, topo.value) mesh.value = zfun(apos[:, 1], apos[:, 0], grid=False) cell = mesh.tria3["index"] zmsk = \ mesh.value[cell[:, 0]] + \ mesh.value[cell[:, 1]] + \ mesh.value[cell[:, 2]] zmsk = zmsk / +3.0 mesh.tria3 = mesh.tria3[zmsk < +0.] print("Saving to ../cache/case_4a.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_4a.vtk"), mesh) print("Saving to ../cache/case_4b.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_4b.vtk"), hmat) return
def ex_3(): # DEMO-3: generate a grid based on the "HR" spacing pattern, # developed by the FESOM team at AWI. 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() #------------------------------------ setup files for JIGSAW opts.geom_file = \ os.path.join(src_path, "eSPH.msh") opts.jcfg_file = \ os.path.join(dst_path, "eSPH.jig") opts.mesh_file = \ os.path.join(dst_path, "mesh.msh") opts.hfun_file = \ os.path.join(dst_path, "spac.msh") #------------------------------------ define JIGSAW geometry geom.mshID = "ellipsoid-mesh" geom.radii = np.full( 3, 6.371E+003, dtype=geom.REALS_t) jigsawpy.savemsh(opts.geom_file, geom) #------------------------------------ define spacing pattern jigsawpy.loadmsh(os.path.join( src_path, "f_hr.msh"), hfun) hfun.value = +3. * hfun.value # for fast example 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.optm_qlim = +9.5E-01 # tighter opt. tol opts.optm_iter = +32 opts.optm_qtol = +1.0E-05 jigsawpy.cmd.jigsaw(opts, mesh) #------------------------------------ save mesh for Paraview jigsawpy.loadmsh(os.path.join( src_path, "topo.msh"), topo) #------------------------------------ a very rough land mask apos = jigsawpy.R3toS2( geom.radii, mesh.point["coord"][:]) apos = apos * 180. / np.pi zfun = interpolate.RectBivariateSpline( topo.ygrid, topo.xgrid, topo.value) mesh.value = zfun( apos[:, 1], apos[:, 0], grid=False) cell = mesh.tria3["index"] zmsk = \ mesh.value[cell[:, 0]] + \ mesh.value[cell[:, 1]] + \ mesh.value[cell[:, 2]] zmsk = zmsk / +3.0 mesh.tria3 = mesh.tria3[zmsk < +0.] print("Saving to ../cache/case_3a.vtk") jigsawpy.savevtk(os.path.join( dst_path, "case_3a.vtk"), mesh) print("Saving to ../cache/case_3b.vtk") jigsawpy.savevtk(os.path.join( dst_path, "case_3b.vtk"), hfun) return
def case_3_(src_path, dst_path): # DEMO-3: generate multi-resolution spacing, via local refi- # nement along coastlines and shallow ridges. Global grid # resolution is 150KM, background resolution is 67KM and the # min. adaptive resolution is 33KM. opts = jigsawpy.jigsaw_jig_t() topo = jigsawpy.jigsaw_msh_t() geom = jigsawpy.jigsaw_msh_t() hraw = jigsawpy.jigsaw_msh_t() hlim = jigsawpy.jigsaw_msh_t() #------------------------------------ setup files for JIGSAW opts.geom_file = \ os.path.join(src_path, "eSPH.msh") opts.jcfg_file = \ os.path.join(dst_path, "eSPH.jig") opts.hfun_file = \ os.path.join(dst_path, "spac.msh") #------------------------------------ define JIGSAW geometry geom.mshID = "ellipsoid-mesh" geom.radii = np.full(3, 6.371E+003, dtype=geom.REALS_t) jigsawpy.savemsh(opts.geom_file, geom) #------------------------------------ define spacing pattern jigsawpy.loadmsh(os.path.join(src_path, "topo.msh"), topo) hraw.mshID = "ellipsoid-grid" hraw.radii = geom.radii hraw.xgrid = topo.xgrid * np.pi / 180. hraw.ygrid = topo.ygrid * np.pi / 180. hfn0 = +150. # global spacing hfn2 = +33. # adapt. spacing hfn3 = +67. # arctic spacing hraw.value = np.sqrt(np.maximum(-topo.value, 0.0)) hraw.value = \ np.maximum(hraw.value, hfn2) hraw.value = \ np.minimum(hraw.value, hfn3) mask = hraw.ygrid < 40. * np.pi / 180. hraw.value[mask] = hfn0 #------------------------------------ set HFUN grad.-limiter hlim = copy.copy(hraw) hlim.slope = np.full( # |dH/dx| limits topo.value.shape, +0.050, dtype=hlim.REALS_t) jigsawpy.savemsh(opts.hfun_file, hlim) jigsawpy.cmd.marche(opts, hlim) #------------------------------------ save mesh for Paraview print("Saving to ../cache/case_3a.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_3a.vtk"), hraw) print("Saving to ../cache/case_3b.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_3b.vtk"), hlim) return
def _opts(self): return jigsaw_jig_t()
def getInitialMesh(topofile, meshfile, spacefile, outfile, dst_path, hfn): t0 = process_time() 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() jigsawpy.loadmsh(topofile, topo) print("Load topography grid (%0.02f seconds)" % (process_time() - t0)) t0 = process_time() opts.geom_file = os.path.join(dst_path, "topology.msh") opts.jcfg_file = os.path.join(dst_path, "config.jig") opts.mesh_file = meshfile opts.hfun_file = spacefile geom.mshID = "ellipsoid-mesh" geom.radii = np.full(3, 6.371e003, dtype=geom.REALS_t) jigsawpy.savemsh(opts.geom_file, geom) hmat.mshID = "ellipsoid-grid" hmat.radii = geom.radii hmat.xgrid = topo.xgrid * np.pi / 180.0 hmat.ygrid = topo.ygrid * np.pi / 180.0 # Set HFUN gradient-limiter hmat.value = np.full(topo.value.shape, hfn[0], dtype=hmat.REALS_t) hmat.value[topo.value > -1000] = hfn[1] hmat.value[topo.value > 0] = hfn[2] hmat.slope = np.full(topo.value.shape, +0.050, dtype=hmat.REALS_t) jigsawpy.savemsh(opts.hfun_file, hmat) jigsawpy.cmd.marche(opts, hmat) print("Build space function (%0.02f seconds)" % (process_time() - t0)) t0 = process_time() 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.optm_qlim = +9.5e-01 # tighter opt. tol opts.optm_iter = +32 opts.optm_qtol = +1.0e-05 jigsawpy.cmd.tetris(opts, 3, mesh) print("Perform triangulation (%0.02f seconds)" % (process_time() - t0)) t0 = process_time() apos = jigsawpy.R3toS2(geom.radii, mesh.point["coord"][:]) apos = apos * 180.0 / np.pi zfun = interpolate.RectBivariateSpline(topo.ygrid, topo.xgrid, topo.value) mesh.value = zfun(apos[:, 1], apos[:, 0], grid=False) jigsawpy.savevtk(outfile, mesh) jigsawpy.savemsh(opts.mesh_file, mesh) print("Get unstructured mesh (%0.02f seconds)" % (process_time() - t0)) return
def run(self, args): # Get inputs mesh_file = args.mesh mesh_crs = args.mesh_crs shape_path = args.shape refine_upstream = args.upstream refine_factor = args.factor refine_cutoff = args.cutoff contours = args.contours patches = args.patches constants = args.constants sieve = args.sieve interp = args.interpolate out_path = args.output out_format = args.output_format nprocs = args.nprocs # Process inputs contour_defns = [] for contour in contours: if len(contour) > 3: raise ValueError( "Invalid format for contour specification." " It should be level [expansion target-size].") level, expansion_rate, target_size = [ *contour, *[None] * (3 - len(contour)) ] contour_defns.append((level, expansion_rate, target_size)) patch_defns = [] for lower_bound, target_size in patches: patch_defns.append((lower_bound, expansion_rate, target_size)) constant_defns = [] for lower_bound, target_size in constants: constant_defns.append((lower_bound, target_size)) interp_rast_list = [] for dem in interp: interp_rast_list.append(Raster(dem)) mesh = Mesh.open(mesh_file, crs=mesh_crs) geom = Geom(deepcopy(mesh)) geom_jig = geom.msh_t() initial_hfun = Hfun(deepcopy(mesh)) initial_hfun.size_from_mesh() # DO NOT DEEPCOPY initial_hfun_jig = initial_hfun.msh_t() ref_crs = initial_hfun_jig.crs utils.reproject(geom_jig, ref_crs) init_jig = None # If there's an input shape, refine in the shape, otherwise # refine the whole domain by the factor if shape_path or refine_cutoff is not None: mesh_poly = mesh.hull.multipolygon() gdf_mesh_poly = gpd.GeoDataFrame(geometry=gpd.GeoSeries(mesh_poly), crs=mesh.crs) if shape_path: gdf_shape = gpd.read_file(shape_path) gdf_shape = gdf_shape.to_crs(mesh.crs) gdf_to_refine = gpd.overlay(gdf_mesh_poly, gdf_shape, how='intersection') gdf_diff = gpd.overlay(gdf_mesh_poly, gdf_shape, how='difference') diff_polys = [] for geom in gdf_diff.geometry: if isinstance(geom, Polygon): diff_polys.append(geom) elif isinstance(geom, MultiPolygon): diff_polys.extend(geom) if refine_upstream: # TODO: Check for multipolygon and single polygon in multi assumption area_ref = 0.05 * np.sum( [i.area for i in gdf_to_refine.geometry]) upstream_polys = [] for ipoly in diff_polys: if ipoly.area < area_ref: upstream_polys.append(ipoly) if upstream_polys: gdf_upstream = gpd.GeoDataFrame( geometry=gpd.GeoSeries(upstream_polys), crs=gdf_diff.crs) gdf_to_refine = gpd.overlay(gdf_upstream, gdf_to_refine, how='union') else: gdf_to_refine = gdf_mesh_poly gdf_to_refine = gdf_to_refine.to_crs(ref_crs) if refine_cutoff is not None: cutoff_mp = mesh.get_multipolygon(zmin=refine_cutoff) cutoff_gdf = gpd.GeoDataFrame( geometry=gpd.GeoSeries(cutoff_mp), crs=mesh.crs) cutoff_gdf = cutoff_gdf.to_crs(ref_crs) gdf_to_refine = gpd.overlay(gdf_to_refine, cutoff_gdf, how='intersection') refine_polys = gdf_to_refine.unary_union # Initial mesh for the refinement (all except refinement area) init_jig = deepcopy(mesh.msh_t) utils.reproject(init_jig, ref_crs) utils.clip_mesh_by_shape(init_jig, refine_polys, fit_inside=True, inverse=True, in_place=True) # Fix elements in the inital mesh that are NOT clipped by refine # polygon init_jig.vert2['IDtag'][:] = -1 # Preparing refinement size function vert_in = utils.get_verts_in_shape(initial_hfun_jig, refine_polys) # Reduce hfun by factor in refinement area; modifying in-place refine_hfun_jig = utils.clip_mesh_by_shape(initial_hfun_jig, refine_polys, fit_inside=False) utils.clip_mesh_by_shape(initial_hfun_jig, refine_polys, fit_inside=True, inverse=True, in_place=True) else: # Refine the whole domain by factor refine_hfun_jig = deepcopy(initial_hfun_jig) # Prepare refinement size function with additional criteria refine_hfun_jig.value[:] = refine_hfun_jig.value / refine_factor hfun_refine = Hfun(Mesh(deepcopy(refine_hfun_jig))) transformer = Transformer.from_crs(mesh.crs, ref_crs, always_xy=True) for level, expansion_rate, target_size in contour_defns: if expansion_rate is None: expansion_rate = 0.1 if target_size is None: target_size = np.min(refine_hfun_jig.value) refine_ctr = mesh.get_contour(level=level) refine_ctr = transform(transformer.transform, refine_ctr) hfun_refine.add_feature(refine_ctr, expansion_rate, target_size, nprocs=nprocs) for lower_bound, expansion_rate, target_size in patch_defns: refine_mp = mesh.get_multipolygon(zmin=lower_bound) refine_mp = transform(transformer.transform, refine_mp) hfun_refine.add_patch(refine_mp, expansion_rate, target_size, nprocs) for lower_bound, target_size in constant_defns: refine_mp = mesh.get_multipolygon(zmin=lower_bound) refine_mp = transform(transformer.transform, refine_mp) hfun_refine.add_patch(refine_mp, None, target_size, nprocs) refine_hfun_jig = hfun_refine.msh_t() utils.reproject(refine_hfun_jig, ref_crs) final_hfun_jig = utils.merge_msh_t(initial_hfun_jig, refine_hfun_jig, out_crs=ref_crs, drop_by_bbox=False) if not (geom_jig.crs == ref_crs and (init_jig and init_jig.crs == ref_crs)): raise ValueError( "CRS for geometry, hfun and init mesh is not the same") opts = jigsawpy.jigsaw_jig_t() opts.hfun_scal = "absolute" opts.hfun_hmin = np.min(final_hfun_jig.value) opts.hfun_hmax = np.max(final_hfun_jig.value) opts.mesh_dims = +2 remesh_jig = jigsawpy.jigsaw_msh_t() remesh_jig.mshID = 'euclidean-mesh' remesh_jig.ndims = 2 remesh_jig.crs = init_jig.crs jigsawpy.lib.jigsaw(opts, geom_jig, remesh_jig, init=init_jig, hfun=final_hfun_jig) utils.finalize_mesh(remesh_jig, sieve) # Interpolate from inpu mesh and DEM if any utils.interpolate_euclidean_mesh_to_euclidean_mesh( mesh.msh_t, remesh_jig) final_mesh = Mesh(remesh_jig) if interp_rast_list: final_mesh.interpolate(interp_rast_list, nprocs=nprocs) # Write to disk final_mesh.write(str(out_path), format=out_format, overwrite=True)
def case_5_(src_path, dst_path): opts = jigsawpy.jigsaw_jig_t() geom = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() hmat = jigsawpy.jigsaw_msh_t() #------------------------------------ setup files for JIGSAW opts.geom_file = \ os.path.join(src_path, "airfoil.msh") opts.jcfg_file = \ os.path.join(dst_path, "airfoil.jig") opts.mesh_file = \ os.path.join(dst_path, "airfoil.msh") opts.hfun_file = \ os.path.join(dst_path, "spacing.msh") #------------------------------------ compute HFUN over GEOM jigsawpy.loadmsh(opts.geom_file, geom) xgeo = geom.vert2["coord"][:, 0] ygeo = geom.vert2["coord"][:, 1] xpos = np.linspace(xgeo.min(), xgeo.max(), 80) ypos = np.linspace(ygeo.min(), ygeo.max(), 40) xmat, ymat = np.meshgrid(xpos, ypos) fun1 = \ +0.1 * (xmat - .40) ** 2 + \ +2.0 * (ymat - .55) ** 2 fun2 = \ +0.7 * (xmat - .75) ** 2 + \ +0.7 * (ymat - .45) ** 2 hfun = np.minimum(fun1, fun2) hmin = 0.01 hmax = 0.10 hfun = 0.4 * np.maximum(np.minimum(hfun, hmax), hmin) hmat.mshID = "euclidean-grid" hmat.ndims = +2 hmat.xgrid = np.array(xpos, dtype=hmat.REALS_t) hmat.ygrid = np.array(ypos, dtype=hmat.REALS_t) hmat.value = np.array(hfun, dtype=hmat.REALS_t) jigsawpy.savemsh(opts.hfun_file, 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_kern = "delfront" # DELFRONT kernel opts.mesh_dims = +2 opts.geom_feat = True opts.mesh_top1 = True jigsawpy.cmd.jigsaw(opts, mesh) print("Saving case_5a.vtk file.") jigsawpy.savevtk(os.path.join(dst_path, "case_5a.vtk"), hmat) print("Saving case_5b.vtk file.") jigsawpy.savevtk(os.path.join(dst_path, "case_5b.vtk"), mesh) return
def waves_mesh(cfg): pwd = os.getcwd() src_path = pwd dst_path = pwd opts = jigsawpy.jigsaw_jig_t() hfun = jigsawpy.jigsaw_msh_t() mesh = jigsawpy.jigsaw_msh_t() init = jigsawpy.jigsaw_msh_t() #------------------------------------ setup files for JIGSAW opts.geom_file = \ str(Path(dst_path)/"geom.msh") # GEOM file opts.jcfg_file = \ str(Path(dst_path)/"jcfg.jig") # JCFG file opts.hfun_file = \ str(Path(dst_path)/"hfun.msh") # HFUN file opts.mesh_file = \ str(Path(dst_path)/"mesh.msh") # MESH file opts.init_file = \ str(Path(dst_path)/"init.msh") # INIT file #------------------------------------ define JIGSAW geometry if cfg['coastlines']: print('Defining coastline geometry') create_coastline_geometry(cfg['shpfiles'], opts.geom_file, cfg['sphere_radius']) else: geom = jigsawpy.jigsaw_msh_t() geom.mshID = 'ELLIPSOID-MESH' geom.radii = cfg['sphere_radius'] * np.ones(3, float) jigsawpy.savemsh(opts.geom_file, geom) #------------------------------------ define mesh size function print('Defining mesh size function') lon_min = -180.0 lon_max = 180.0 dlon = cfg['hfun_grid_spacing'] nlon = int((lon_max - lon_min) / dlon) + 1 lat_min = -90.0 lat_max = 90.0 nlat = int((lat_max - lat_min) / dlon) + 1 dlat = cfg['hfun_grid_spacing'] xlon = np.linspace(lon_min, lon_max, nlon) ylat = np.linspace(lat_min, lat_max, nlat) print(' hfun grid dimensions: {}, {}'.format(xlon.size, ylat.size)) define_hfunction.depth_threshold_refined = cfg['depth_threshold_refined'] define_hfunction.distance_threshold_refined = cfg[ 'distance_threshold_refined'] define_hfunction.depth_threshold_global = cfg['depth_threshold_global'] define_hfunction.distance_threshold_global = cfg[ 'distance_threshold_global'] define_hfunction.refined_res = cfg['refined_res'] define_hfunction.maxres = cfg['maxres'] hfunction = define_hfunction.cell_widthVsLatLon(xlon, ylat, cfg['shpfiles'], cfg['sphere_radius'], cfg['ocean_mesh']) hfunction = hfunction / km hfun.mshID = "ellipsoid-grid" hfun.radii = np.full(3, cfg['sphere_radius'], dtype=jigsawpy.jigsaw_msh_t.REALS_t) hfun.xgrid = np.radians(xlon) hfun.ygrid = np.radians(ylat) hfun.value = hfunction.astype(jigsawpy.jigsaw_msh_t.REALS_t) #------------------------------------ specify JIGSAW initial conditions print('Specifying initial fixed coordinate locations') create_initial_points(cfg['ocean_mesh'], xlon, ylat, hfunction, cfg['sphere_radius'], opts.init_file) jigsawpy.loadmsh(opts.init_file, init) jigsawpy.savevtk(str(Path(dst_path) / "init.vtk"), init) #------------------------------------ set HFUN grad.-limiter print('Limiting mesh size function gradients') hfun.slope = np.full( # |dH/dx| limits hfun.value.shape, cfg['hfun_slope_lim'], dtype=hfun.REALS_t) jigsawpy.savemsh(opts.hfun_file, hfun) jigsawpy.cmd.marche(opts, 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 = +.67 # relax edge error opts.mesh_rad2 = +1.2 opts.optm_qlim = +9.5E-01 # tighter opt. tol opts.optm_iter = +32 opts.optm_qtol = +1.0E-05 opts.verbosity = 2 #jigsawpy.cmd.tetris(opts, 3, mesh) jigsawpy.cmd.jigsaw(opts, mesh) if cfg['coastlines']: segment.segment(mesh) #------------------------------------ save mesh for Paraview print("Writing ex_h.vtk file.") jigsawpy.savevtk(str(Path(dst_path) / "_hfun.vtk"), hfun) jigsawpy.savevtk(str(Path(dst_path) / "waves_mesh.vtk"), mesh) jigsawpy.savemsh(str(Path(dst_path) / "waves_mesh.msh"), mesh) #------------------------------------ convert mesh to MPAS format jigsaw_to_netcdf(msh_filename='waves_mesh.msh', output_name='waves_mesh_triangles.nc', on_sphere=True, sphere_radius=cfg['sphere_radius']) write_netcdf( convert(xarray.open_dataset('waves_mesh_triangles.nc'), dir='./', logger=None), 'waves_mesh.nc') #------------------------------------ cull mesh to ocean bounary if os.path.exists('./cull_waves_mesh'): f = open('cull_waves_mesh.nml', 'w') f.write('&inputs\n') f.write(" waves_mesh_file = 'waves_mesh.nc'\n") f.write(" ocean_mesh_file = '" + cfg['ocean_mesh'] + "'\n") f.write("/\n") f.write('&output\n') f.write(" waves_mesh_culled_vtk = 'waves_mesh_culled.vtk'\n") f.write(" waves_mesh_culled_gmsh = 'waves_mesh_culled.msh'\n") f.write("/\n") f.close() subprocess.call('./cull_waves_mesh', shell=True) #------------------------------------ output ocean mesh polygons subprocess.call('paraview_vtk_field_extractor.py -f ' + cfg['ocean_mesh'] + ' --ignore_time -v areaCell -o ' + cfg['ocean_mesh'] + '.vtk', shell=True) return