def __createDomain(self): """ Creates and returns an escript domain that spans the entire area of available data plus a padding zone. This method is called only once the first time `getDomain()` is invoked. :return: The escript domain :rtype: `esys.escript.Domain` """ X0, NX, DX = self.__getTotalExtentsWithPadding() # number of domain elements NE = NX + [self._v_num_cells] # origin of domain origin = X0 + [-self._v_depth * self.__v_scale] if self.getReferenceSystem().isCartesian(): # rounding will give us about meter-accuracy with UTM coordinates self._dom_origin = [np.floor(oi) for oi in origin] else: # this should give us about meter-accuracy with lat/lon coords self._dom_origin = [1e-5 * np.floor(oi * 1e5) for oi in origin] # cell size / point spacing spacing = DX + [ self.__v_scale * np.floor( (self._v_depth + self._v_air_layer) / self._v_num_cells) ] #self._spacing = [float(np.floor(si)) for si in spacing] self._spacing = spacing lo = [(self._dom_origin[i], self._dom_origin[i] + NE[i] * self._spacing[i]) for i in range(self.__dim)] if self.__dim == 3: dom = Brick(*NE, l0=lo[0], l1=lo[1], l2=lo[2]) else: dom = Rectangle(*NE, l0=lo[0], l1=lo[1]) # ripley may internally adjust NE and length, so recompute self._dom_len = [ esu.sup(dom.getX()[i]) - esu.inf(dom.getX()[i]) for i in range(self.__dim) ] self._dom_NE = [ int(self._dom_len[i] / self._spacing[i]) for i in range(self.__dim) ] self.logger.debug("Domain size: " + str(self._dom_NE)) self.logger.debug(" length: " + str(self._dom_len)) self.logger.debug(" origin: " + str(self._dom_origin)) return dom
def __createDomain(self): """ Creates and returns an escript domain that spans the entire area of available data plus a padding zone. This method is called only once the first time `getDomain()` is invoked. :return: The escript domain :rtype: `esys.escript.Domain` """ X0, NX, DX = self.__getTotalExtentsWithPadding() # number of domain elements NE = NX + [self._v_num_cells] # origin of domain origin = X0 + [-self._v_depth*self.__v_scale] if self.getReferenceSystem().isCartesian(): # rounding will give us about meter-accuracy with UTM coordinates self._dom_origin = [np.floor(oi) for oi in origin] else: # this should give us about meter-accuracy with lat/lon coords self._dom_origin = [1e-5*np.floor(oi*1e5) for oi in origin] # cell size / point spacing spacing = DX + [self.__v_scale*np.floor((self._v_depth+self._v_air_layer)/self._v_num_cells)] #self._spacing = [float(np.floor(si)) for si in spacing] self._spacing = spacing lo=[(self._dom_origin[i], self._dom_origin[i]+NE[i]*self._spacing[i]) for i in range(self.__dim)] if self.__dim==3: dom=Brick(*NE, l0=lo[0], l1=lo[1], l2=lo[2]) else: dom=Rectangle(*NE, l0=lo[0], l1=lo[1]) # ripley may internally adjust NE and length, so recompute self._dom_len=[sup(dom.getX()[i])-inf(dom.getX()[i]) for i in range(self.__dim)] self._dom_NE=[int(self._dom_len[i]/self._spacing[i]) for i in range(self.__dim)] self.logger.debug("Domain size: "+str(self._dom_NE)) self.logger.debug(" length: "+str(self._dom_len)) self.logger.debug(" origin: "+str(self._dom_origin)) return dom
class TestGravityApp(unittest.TestCase): xdim = 1000. ydim = 1000. zdim = 1000. NEX = 100 NEZ = 100 TESTTOL = 1e-3 def setUp(self): self.domain = Brick(self.NEX, self.NEX, self.NEZ, l0=self.xdim, l1=self.ydim, l2=self.zdim) def tearDown(self): del self.domain def test_pde_answer(self): model = GravityModel(self.domain, fixBase=True) x = self.domain.getX() xmin = inf(x[0]) xmax = sup(x[0]) ymin = inf(x[1]) ymax = sup(x[1]) zmin = inf(x[2]) zmax = sup(x[2]) xp = 2. * np.pi / (xmax - xmin) yp = 2. * np.pi / (ymax - ymin) zp = 2. * np.pi / (zmax - zmin) Dens = (xp**2 + yp**2 + zp**2) * cos(xp * x[0]) * cos(yp * x[1]) * sin( zp * x[2]) / (4.0 * np.pi * U.Gravitational_Constant) model.setDensity(Dens) outdens = model.getDensity() densdiff = abs(Dens - outdens) self.assertLessEqual(sup(densdiff), self.TESTTOL) actualanswer = -cos(xp * x[0]) * cos(yp * x[1]) * sin(zp * x[2]) abserror = abs(actualanswer - model.getGravityPotential()) biggesterror = sup(abserror) self.assertLessEqual(biggesterror, self.TESTTOL) gz = model.getzGravity() actualgz = -zp * cos(xp * x[0]) * cos(yp * x[1]) * cos(zp * x[2]) errorgz = abs(gz - actualgz) self.assertLessEqual(sup(errorgz), self.TESTTOL * 100.)
class TestMagneticApp3D(unittest.TestCase): xdim = 1000. ydim = 1000. zdim = 1000. NEX = 100 NEZ = 100 TESTTOL = 1e-3 def setUp(self): self.domain = Brick(self.NEX, self.NEX, self.NEZ, l0=self.xdim, l1=self.ydim, l2=self.zdim) def tearDown(self): del self.domain def test_pde_answer(self): model = MagneticModel3D(self.domain, fixVert=True) x = self.domain.getX() xmin = inf(x[0]) xmax = sup(x[0]) ymin = inf(x[1]) ymax = sup(x[1]) zmin = inf(x[2]) zmax = sup(x[2]) xp = 2. * np.pi / (xmax - xmin) yp = 2. * np.pi / (ymax - ymin) zp = 2. * np.pi / (zmax - zmin) Bh = [1., 0., 0.] k = ((xp**2 + yp**2 + zp**2) / xp) * cos(xp * x[0]) * sin( yp * x[1]) * cos(zp * x[2]) model.setSusceptibility(k) outk = model.getSusceptibility() kdiff = abs(k - outk) self.assertLessEqual(sup(kdiff), self.TESTTOL) model.setBackgroundMagneticField(Bh) actualanswer = sin(xp * x[0]) * sin(yp * x[1]) * cos(zp * x[2]) abserror = abs(actualanswer - model.getAnomalyPotential()) biggesterror = sup(abserror) self.assertLessEqual(biggesterror, self.TESTTOL)
try: from esys.ripley import Brick HAVE_RIPLEY = True except ImportError: HAVE_RIPLEY = False print("Ripley module not available") if HAVE_RIPLEY: from esys.escript import * from esys.escript.linearPDEs import Poisson from esys.weipa import saveSilo, saveVoxet import tempfile dom = Brick(l0=1., l1=1., n0=9, n1=9, n2=9) x = dom.getX() gammaD = whereZero(x[0]) + whereZero(x[1]) pde = Poisson(dom) q = gammaD pde.setValue(f=1, q=q) u = pde.getSolution() u = interpolate(u + dom.getX()[2], ReducedFunction(dom)) print(u) handle, filename = tempfile.mkstemp(suffix='.vo', prefix='poisson') saveVoxet(filename, u=u) print("-------") dom = Brick(l0=1., l1=1., l2=4., n0=18, n1=18, n2=36) v = readVoxet(dom, filename, 'u', fillValue=0.5) print(v) os.remove(filename) #saveSilo('/tmp/poisson', v=v)
elif (NY+1)%(d1*f) == 0: d1 = d1 * f d2 = d2 / f else: if (NY+1)%(d1*f) == 0: d1 = d1 * f d2 = d2 / f elif (NX+1)%(d0*f) == 0: d0 = d0 * f d2 = d2 / f # create domain print("Domain subdivisions: %d x %d x %d"%(d0,d1,d2)) dom = Brick(NX, NY, n_cells_v, l0, l1, l2, d0, d1, d2) dom_len = [sup(dom.getX()[i])-inf(dom.getX()[i]) for i in range(dom.getDim())] # report domain setup print("Domain size: "+str([NX, NY, n_cells_v])) print(" length: "+str(dom_len)) print(" origin: "+str(dom_origin)) DIM = dom.getDim() # = 3 datacoords = ReducedFunction(dom).getX() # create the output directory if not existing already try: os.mkdir(OUTPUTDIR) except: pass
if __name__ == "__main__": try: from esys.ripley import Brick HAVE_RIPLEY = True except ImportError: HAVE_RIPLEY = False print("Ripley module not available") if HAVE_RIPLEY: from esys.escript import * from esys.escript.linearPDEs import Poisson from esys.weipa import saveSilo, saveVoxet dom = Brick(l0=1.,l1=1.,n0=9, n1=9, n2=9) x = dom.getX() gammaD = whereZero(x[0])+whereZero(x[1]) pde = Poisson(dom) q = gammaD pde.setValue(f=1, q=q) u = pde.getSolution() u=interpolate(u+dom.getX()[2], ReducedFunction(dom)) print(u) saveVoxet('/tmp/poisson.vo', u=u) print("-------") dom = Brick(l0=1.,l1=1.,l2=4.,n0=18, n1=18, n2=36) v=readVoxet(dom, '/tmp/poisson.vo', 'u', fillValue=0.5) print(v) #saveSilo('/tmp/poisson', v=v)
class TestDCResistivityApp(unittest.TestCase): dx = 10 # grid line spacing in [m] NEx = 50 # number of nodes in the x direction NEy = 50 # number of nodes in the y direction NEz = 50 # number of nodes in the z direction H0 = 600 # height [m] of transect above bottom of domain (will be locked to grid) sig_p = 1. # primary conductivity sig_2 = 100. # conductivity in ball xdim = 1000. TESTTOL = 1e-3 POSx = 100 POSy = 70 NEGx = 100 NEGy = 110 POS_node = (POSx * dx, POSy * dx, NEz * dx) NEG_node = (NEGx * dx, NEGy * dx, NEz * dx) Z0 = 100 # vertical position of circle below transect [m] Lx = dx * NEx Ly = dx * NEy Lz = dx * NEz c = [Lx / 2., Ly / 2., H0 - Z0] # circle center R = 50. # radius def setUp(self): self.domain = Brick(n0=self.NEx, n1=self.NEy, n2=self.NEz, l0=self.Lx, l1=self.Ly, l2=self.Lz, diracPoints=[self.POS_node, self.NEG_node], diracTags=['e0', 'e1']) def tearDown(self): del self.domain def test_pde_Primary(self): model1 = DCResistivityModel(self.domain, sigma0=self.sig_p, useFastSolver=True) model1.setPrimaryPotentialForHalfSpace( sources=[self.POS_node, self.NEG_node], charges=[1.0, -1.0]) analyticprimary = model1.setPrimaryPotentialForHalfSpace( sources=[self.POS_node, self.NEG_node], charges=[1.0, -1.0]) primaryans1 = model1.getPrimaryPotential() model2 = DCResistivityModel(self.domain, sigma0=self.sig_p, useFastSolver=True) src = Scalar(0., DiracDeltaFunctions(self.domain)) src.setTaggedValue('e0', 1.0) src.setTaggedValue('e1', -1.0) model2.setPrimaryPotential(source=src) primaryans2 = model2.getPrimaryPotential() abserror = abs(primaryans1 - primaryans2) self.assertLessEqual(sup(abserror), self.TESTTOL * 10., "primaries") sigma = Scalar(self.sig_p, ContinuousFunction(self.domain)) x = self.domain.getX() d = length(x - self.c) sphereCond = sigma + (self.sig_2 - self.sig_p) * whereNegative( d - self.R) # 0 for d>R and 1 for d<R model1.setConductivity(sphereCond) model2.setConductivity(sphereCond) us1 = model1.getSecondaryPotential() us2 = model1.getSecondaryPotential() abserror = abs(us1 - us2) self.assertLessEqual(sup(abserror), self.TESTTOL * 1., "secondaries") ut1 = model1.getPotential() ut2 = model2.getPotential() model3 = DCResistivityModelNoPrimary(self.domain, source=src, sigma=sphereCond, useFastSolver=True) ut3 = model3.getPotential() abserror = abs(ut1 - ut2) self.assertLessEqual(sup(abserror), self.TESTTOL * 10., "total with primaries") abserror = abs(ut1 - ut3) self.assertLessEqual(sup(abserror), self.TESTTOL * 10., "total with analytic primary") abserror = abs(ut2 - ut3) self.assertLessEqual(sup(abserror), self.TESTTOL * 10., "total with FE primary")