def runjgsw(mesh_path, make_bool, projector): """ RUNJGSW: main call to JIGSAW to build the triangulation. MESH-PATH should point to a user-defined mesh directory, containing the COMPOSE.py template. Firstly, MESH-PATH/COMPOSE.py is called to build user-defined geometry, initial conditions, mesh spacing and mesh configuration information. The boolean flags MAKE-BOOL control whether mesh information is built from scratch, or if an exitsing an existing file is to be used. For example, setting MAKE-BOOL.SPAC = FALSE relies on an existing spacing pattern be available in MESH-PATH/tmp/. This information is written to MESH-PATH/tmp/ to be accessed by subsequent calls to JIGSAW. Finally, JIGSAW is run to build the triangular mesh, calling either the multi-level (TETRIS) or single-level (JIGSAW) algorithms. Cells are assigned ID-tags via the polygon/regions defined in GEOM.BOUNDS. Returns full-dimensional and 2d-projected msh_t objects. """ # Authors: Darren Engwirda mesh = jigsawpy.jigsaw_msh_t() mprj = jigsawpy.jigsaw_msh_t() gprj = jigsawpy.jigsaw_msh_t() #------------------------------------ setup via user COMPOSE base = \ mesh_path.replace(os.path.sep, ".") if (make_bool.geom): geom = getattr(import_module( base + ".compose"), "setgeom")() if (make_bool.spac): spac = getattr(import_module( base + ".compose"), "setspac")() if (make_bool.init): init = getattr(import_module( base + ".compose"), "setinit")() if (make_bool.opts): opts = getattr(import_module( base + ".compose"), "setopts")() #------------------------------------ setup files for JIGSAW opts.geom_file = os.path.join( mesh_path, "tmp", "geom.msh") opts.jcfg_file = os.path.join( mesh_path, "tmp", "opts.jig") opts.init_file = os.path.join( mesh_path, "tmp", "init.msh") opts.hfun_file = os.path.join( mesh_path, "tmp", "spac.msh") opts.mesh_file = os.path.join( mesh_path, "tmp", "mesh.msh") opts.hfun_tags = "precision = 9" # less float prec. jigsawpy.savemsh(opts.geom_file, geom, opts.geom_tags) jigsawpy.savemsh(opts.hfun_file, spac, opts.hfun_tags) jigsawpy.savemsh(opts.init_file, init, opts.init_tags) #------------------------------------ make mesh using JIGSAW if (not hasattr(opts, "bisection")): opts.bisection = +0 if (opts.bisection < +0): # bisect heuristic rbar = np.mean(geom.radii) hbar = np.mean(spac.value) nlev = round(math.log2( rbar / math.sin(.4 * math.pi) / hbar) ) nlev = nlev - 1 ttic = time.time() jigsawpy.cmd.tetris(opts, nlev - 0, mesh) ttoc = time.time() print("CPUSEC =", (ttoc - ttic)) print("BISECT =", +nlev) elif (opts.bisection > +0): # bisect specified nlev = opts.bisection ttic = time.time() jigsawpy.cmd.tetris(opts, nlev - 0, mesh) ttoc = time.time() print("CPUSEC =", (ttoc - ttic)) print("BISECT =", +nlev) else: # do non-recursive ttic = time.time() jigsawpy.cmd.jigsaw(opts, mesh) ttoc = time.time() print("CPUSEC =", (ttoc - ttic)) #------------------------------------ form local projections gprj = copy.deepcopy(geom) # local 2d objects mprj = copy.deepcopy(mesh) if (mesh.vert3.size > +0): project(geom, mesh, gprj, mprj, projector) #------------------------------------ assign IDtag's to cell if (geom.bound is not None and geom.bound.size > +0): # tags per polygon imin = np.amin(geom.bound["IDtag"]) imax = np.amax(geom.bound["IDtag"]) for itag in range( imin + 0, imax + 1): tagcell(geom, mesh, gprj, mprj, itag) #------------------------------------ check mesh for quality 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.savevtk(os.path.join( mesh_path, "out", "geom.vtk"), geom) jigsawpy.savevtk(os.path.join( mesh_path, "out", "spac.vtk"), spac) jigsawpy.savevtk(os.path.join( mesh_path, "out", "init.vtk"), init) jigsawpy.savevtk(os.path.join( mesh_path, "out", "mesh.vtk"), mesh) jigsawpy.savevtk(os.path.join( mesh_path, "out", "geom_prj.vtk"), gprj) jigsawpy.savevtk(os.path.join( mesh_path, "out", "mesh_prj.vtk"), mprj) return geom, gprj, mesh, mprj
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_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 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(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 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 # opts.optm_kern = "cvt+dqdx" rbar = np.mean(geom.radii) # bisect heuristic hbar = np.mean(hmat.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 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 case_1_(src_path, dst_path): # DEMO-1: generate a uniform resolution (150KM) global grid. opts = jigsawpy.jigsaw_jig_t() topo = jigsawpy.jigsaw_msh_t() geom = 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") #------------------------------------ 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) #------------------------------------ make mesh using JIGSAW opts.hfun_scal = "absolute" opts.hfun_hmax = +150. # uniform at 150km 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 nlev = round(math.log2(rbar / math.sin(.4 * math.pi) / opts.hfun_hmax)) 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_1a.vtk") jigsawpy.savevtk(os.path.join(dst_path, "case_1a.vtk"), mesh) return