def test_zero_div(self): g = lbnz.div(lbnz.curl(lbnz.thetaphir.phi)) self.assertAlmostEqualWithMagnitude(0, g, 6) g = lbnz.div(lbnz.curl(lbnz.thetaphir.theta)) self.assertAlmostEqualWithMagnitude(0, g, 6) g = lbnz.div(lbnz.curl(lbnz.thetaphir.r)) self.assertAlmostEqualWithMagnitude(0, g, 6) phx, phy, phz = lbnz.thetaphir.phi thx, thy, thz = lbnz.thetaphir.theta rx, ry, rz = lbnz.thetaphir.r g = lbnz.div(lbnz.curl((rx * rx + phx * thx, ry * ry + phy * thy, rz * rz + phz * thz))) self.assertAlmostEqualWithMagnitude(0, g, 6)
def test_lapacian(self): # f = x * y * y + z * z * z # expect that grad_f = y * y, 2 * x * y, 3 * z * z fld = lbnz.x * lbnz.y * lbnz.y + lbnz.z * lbnz.z * lbnz.z e0, e1, e2 = lbnz.y * lbnz.y, 2 * lbnz.x * lbnz.y, 3 * lbnz.z * lbnz.z g0, g1, g2 = lbnz.grad(fld) self.assertAlmostEqualWithMagExceptBoundary(e0, g0, 3) self.assertAlmostEqualWithMagExceptBoundary(e1, g1, 6) self.assertAlmostEqualWithMagExceptBoundary(e2, g2, 4) # F = y * y, 2 * x * y, 3 * z * z = grad(f) # expect that div_F = 2 * x + 6 * z expected = 2 * lbnz.x + 6 * lbnz.z test = lbnz.div((g0, g1, g2)) self.assertAlmostEqualWithMagExceptBoundary(expected, test, 2) # f = x * y * y + z * z * z # expect that lapacian_f = 2 * x + 6 * z fld = lbnz.x * lbnz.y * lbnz.y + lbnz.z * lbnz.z * lbnz.z expected = 2 * lbnz.x + 6 * lbnz.z test = lbnz.laplacian(fld) self.assertAlmostEqualWithMagExceptBoundary(expected, test, 2)
def test_div3(self): # F = y * y, 2 * x * y, 3 * z * z # expect that div_F = 2 * x + 6 * z fld = lbnz.y * lbnz.y, 2 * lbnz.x * lbnz.y, 3 * lbnz.z * lbnz.z expected = 2 * lbnz.x + 6 * lbnz.z test = lbnz.div(fld) self.assertAlmostEqualWithMagExceptBoundary(expected, test, 6)
def test_div2(self): # F = (-y, x * y, z); # expect that div_F = x + 1 fld = -lbnz.y, lbnz.x * lbnz.y, lbnz.z expected = lbnz.x + 1 test = lbnz.div(fld) self.assertAlmostEqualWithMagExceptBoundary(expected, test, 6)
def test_div1(self): # F = (0, phi * theta, 0); # expect that div_F = theta * (-z * y / (r ** 2 * sqrt(r ** 2 - z ** 2))) + phi * x / (x ** 2 + y ** 2) fld = lbnz.zero, lbnz.phi * lbnz.theta, lbnz.zero expected = lbnz.theta * (-lbnz.z * lbnz.y / (lbnz.r ** 2 * th.sqrt(lbnz.r ** 2 - lbnz.z ** 2))) + lbnz.phi * lbnz.x / \ (lbnz.x ** 2 + lbnz.y ** 2) test = lbnz.div(fld) self.assertAlmostEqualWithMagExceptBoundary(expected, test, 2)
def test_div1(self): # F = (-theta, phi * theta, r); # expect that div_F = theta * tan(phi) / r + phi / (r * cos(phi)) + 3 fld = - lbnz.theta, lbnz.phi * lbnz.theta, lbnz.r expected = lbnz.theta * th.tan(lbnz.phi) / lbnz.r + lbnz.phi / (lbnz.r * th.cos(lbnz.phi)) + 3 test = lbnz.div(fld) self.assertAlmostEqualWithMagnitude(expected, test, 3)
def test_div_sensibility(self): # F = y * y, 2 * x * y, 3 * z * z fld = lbnz.y * lbnz.y, 2 * lbnz.x * lbnz.y, 3 * lbnz.z * lbnz.z noise = th.randn_like(fld[0]) * th.std(fld[0]) * 1e-6, \ th.randn_like(fld[1]) * th.std(fld[1]) * 1e-6, \ th.randn_like(fld[2]) * th.std(fld[2]) * 1e-6 fld_ = fld[0] + noise[0], fld[1] + noise[1], fld[2] + noise[2] self.assertAlmostEqualWithMagExceptBoundary(fld[0], fld_[0], 5) self.assertAlmostEqualWithMagExceptBoundary(fld[1], fld_[1], 5) self.assertAlmostEqualWithMagExceptBoundary(fld[2], fld_[2], 5) div1 = lbnz.div(fld) div2 = lbnz.div(fld_) self.assertAlmostEqualWithMagExceptBoundary(div1, div2, 4) diff_div1_0, diff_div1_1, diff_div1_2 = lbnz.diff(div1) diff_div2_0, diff_div2_1, diff_div2_2 = lbnz.diff(div2) self.assertAlmostEqualWithMagExceptBoundary(diff_div1_0, diff_div2_0, 2) self.assertAlmostEqualWithMagExceptBoundary(0.0, diff_div2_1, 2) self.assertAlmostEqualWithMagExceptBoundary(diff_div1_2, diff_div2_2, 2)
def test_div2(self): # phi0 = phi[:, :, W//2, L//2, H//2], theta0 = theta[:, :, W//2, L//2, H//2], r0 = r[:, :, W//2, L//2, H//2] # F = (-(theta - theta0), (phi - phi0) * (theta-theta0), (r-r0)); # expect that div_F = (theta - theta0) * tan(phi) / r + (phi - phi0) / (r * cos(phi)) + 3 - 2 * r0 / r phi0 = lbnz.phi[:, :, lbnz.W // 2, lbnz.L // 2, lbnz.H // 2] theta0 = lbnz.theta[:, :, lbnz.W // 2, lbnz.L // 2, lbnz.H // 2] r0 = lbnz.r[:, :, lbnz.W // 2, lbnz.L // 2, lbnz.H // 2] Fx = -(lbnz.theta - theta0) Fy = (lbnz.phi - phi0) * (lbnz.theta - theta0) Fz = (lbnz.r - r0) test = lbnz.div((Fx, Fy, Fz)) expected = (lbnz.theta - theta0) * th.tan(lbnz.phi) / lbnz.r +\ (lbnz.phi - phi0) / (lbnz.r * th.cos(lbnz.phi)) + 3 - 2 * r0 / lbnz.r self.assertAlmostEqualWithMagnitude(expected, test, 3)