def test_haverkamp_theta_u(self): mesh = discretize.TensorMesh([50]) hav = richards.empirical.Haverkamp_theta(mesh) passed = checkDerivative(lambda u: (hav(u), hav.derivU(u)), np.random.randn(50), plotIt=False) self.assertTrue(passed, True)
def test_vangenuchten_theta_m(self): mesh = discretize.TensorMesh([50]) idnmap = maps.IdentityMap(nP=mesh.nC) seeds = { "theta_r": np.random.rand(mesh.nC), "theta_s": np.random.rand(mesh.nC), "n": np.random.rand(mesh.nC) + 1, "alpha": np.random.rand(mesh.nC), } opts = [ ("theta_r", dict(theta_rMap=idnmap), 1), ("theta_s", dict(theta_sMap=idnmap), 1), ("n", dict(nMap=idnmap), 1), ("alpha", dict(alphaMap=idnmap), 1), ] u = np.random.randn(mesh.nC) for name, opt, nM in opts: van = richards.empirical.Vangenuchten_theta(mesh, **opt) x0 = np.concatenate([seeds[n] for n in name.split("-")]) def fun(m): van.model = m return van(u), van.derivM(u) print("Vangenuchten_theta test m deriv: ", name) passed = checkDerivative(fun, x0, plotIt=False) self.assertTrue(passed, True)
def test_vangenuchten_theta_u(self): mesh = discretize.TensorMesh([50]) van = richards.empirical.Vangenuchten_theta(mesh) passed = checkDerivative(lambda u: (van(u), van.derivU(u)), np.random.randn(50), plotIt=False) self.assertTrue(passed, True)
def _dotest_sensitivity_full(self): print("Testing Richards Derivative FULL dim={}".format(self.mesh.dim)) J = self.prob.Jfull(self.mtrue) passed = checkDerivative( lambda m: [self.prob.dpred(m), J], self.mtrue, num=4, plotIt=False ) self.assertTrue(passed, True)
def test_simpleFunction(self): def simpleFunction(x): return np.sin(x), lambda xi: sdiag(np.cos(x)) * xi passed = checkDerivative(simpleFunction, np.random.randn(5), plotIt=False) self.assertTrue(passed, True)
def _dotest_sensitivity(self): print("Testing Richards Derivative dim={}".format(self.mesh.dim)) passed = checkDerivative( lambda m: [self.prob.dpred(m), lambda v: self.prob.Jvec(m, v)], self.mtrue, num=3, plotIt=False, ) self.assertTrue(passed, True)
def test_haverkamp_k_u(self): mesh = discretize.TensorMesh([5]) hav = richards.empirical.Haverkamp_k(mesh) print("Haverkamp_k test u deriv") passed = checkDerivative(lambda u: (hav(u), hav.derivU(u)), np.random.randn(mesh.nC), plotIt=False) self.assertTrue(passed, True)
def _test_deriv(self, x=None, num=4, plotIt=False, **kwargs): print("Testing {0!s} Deriv".format(self.__class__.__name__)) if x is None: if self.nP == "*": x = np.random.randn(np.random.randint(1e2, high=1e3)) else: x = np.random.randn(self.nP) return checkDerivative( lambda m: [self(m), self.deriv(m)], x, num=num, plotIt=plotIt, **kwargs )
def test_EdgeInnerProductIsotropicDeriv(self): def fun(x): MeSig = self.mesh.getEdgeInnerProduct(x) MeSigDeriv = self.mesh.getEdgeInnerProductDeriv(self.x0) return MeSig * self.edge_vec, MeSigDeriv(self.edge_vec) print("Testing EdgeInnerProduct Isotropic") return self.assertTrue( tests.checkDerivative(fun, self.x0, num=7, tolerance=TOLD, plotIt=False))
def test_haverkamp_k_m(self): mesh = discretize.TensorMesh([5]) expmap = maps.IdentityMap(nP=mesh.nC) wires2 = maps.Wires(("one", mesh.nC), ("two", mesh.nC)) wires3 = maps.Wires(("one", mesh.nC), ("two", mesh.nC), ("three", mesh.nC)) opts = [ ("Ks", dict(KsMap=expmap), 1), ("A", dict(AMap=expmap), 1), ("gamma", dict(gammaMap=expmap), 1), ("Ks-A", dict(KsMap=expmap * wires2.one, AMap=expmap * wires2.two), 2), ( "Ks-gamma", dict(KsMap=expmap * wires2.one, gammaMap=expmap * wires2.two), 2, ), ( "A-gamma", dict(AMap=expmap * wires2.one, gammaMap=expmap * wires2.two), 2, ), ( "Ks-A-gamma", dict( KsMap=expmap * wires3.one, AMap=expmap * wires3.two, gammaMap=expmap * wires3.three, ), 3, ), ] u = np.random.randn(mesh.nC) for name, opt, nM in opts: np.random.seed(2) hav = richards.empirical.Haverkamp_k(mesh, **opt) def fun(m): hav.model = m return hav(u), hav.derivM(u) print("Haverkamp_k test m deriv: ", name) passed = checkDerivative(fun, np.random.randn(mesh.nC * nM), plotIt=False) self.assertTrue(passed, True)
def test_FaceInnerProductIsotropicDerivInvMat(self): def fun(x): MfSig = self.mesh.getFaceInnerProduct(x, invMat=True) MfSigDeriv = self.mesh.getFaceInnerProductDeriv(self.x0, invMat=True) return MfSig * self.face_vec, MfSigDeriv(self.face_vec) print("Testing FaceInnerProduct Isotropic InvMat") return self.assertTrue( tests.checkDerivative(fun, self.x0, num=7, tolerance=TOLD, plotIt=False))
def _test_deriv2(self, x=None, num=4, plotIt=False, **kwargs): print("Testing {0!s} Deriv2".format(self.__class__.__name__)) if x is None: if self.nP == "*": x = np.random.randn(np.random.randint(1e2, high=1e3)) else: x = np.random.randn(self.nP) v = x + 0.1 * np.random.rand(len(x)) return checkDerivative( lambda m: [self.deriv(m).dot(v), self.deriv2(m, v=v)], x, num=num, expectedOrder=1, plotIt=plotIt, **kwargs )
def _dotest_getResidual(self, newton): print("Testing richards get residual newton={}, dim={}".format( newton, self.mesh.dim)) self.prob.do_newton = newton passed = checkDerivative( lambda hn1: self.prob.getResidual( self.mtrue, self.h0, hn1, self.prob.time_steps[0], self.prob.boundary_conditions, ), self.h0, expectedOrder=2 if newton else 1, plotIt=False, ) self.assertTrue(passed, True)
def test_EdgeInnerProductAnisotropicDerivInvMat(self): def fun(x): x = np.repeat(np.atleast_2d(x), 3, axis=0).T x0 = np.repeat(self.x0, 3, axis=0).T zero = sp.csr_matrix((self.mesh.nC, self.mesh.nC)) eye = sp.eye(self.mesh.nC) P = sp.vstack([sp.hstack([zero, eye, zero])]) MeSig = self.mesh.getEdgeInnerProduct(x, invMat=True) MeSigDeriv = self.mesh.getEdgeInnerProductDeriv(x0, invMat=True) return MeSig * self.edge_vec, MeSigDeriv(self.edge_vec) * P.T print("Testing EdgeInnerProduct Anisotropic InvMat") return self.assertTrue( tests.checkDerivative(fun, self.x0, num=7, tolerance=TOLD, plotIt=False))
def test_vangenuchten_k_m(self): mesh = discretize.TensorMesh([50]) expmap = maps.ExpMap(nP=mesh.nC) idnmap = maps.IdentityMap(nP=mesh.nC) seeds = { "Ks": np.random.triangular(np.log(1e-7), np.log(1e-6), np.log(1e-5), mesh.nC), "I": np.random.rand(mesh.nC), "n": np.random.rand(mesh.nC) + 1, "alpha": np.random.rand(mesh.nC), } opts = [ ("Ks", dict(KsMap=expmap), 1), ("I", dict(IMap=idnmap), 1), ("n", dict(nMap=idnmap), 1), ("alpha", dict(alphaMap=idnmap), 1), ] u = np.random.randn(mesh.nC) for name, opt, nM in opts: van = richards.empirical.Vangenuchten_k(mesh, **opt) x0 = np.concatenate([seeds[n] for n in name.split("-")]) def fun(m): van.model = m return van(u), van.derivM(u) print("Vangenuchten_k test m deriv: ", name) passed = checkDerivative(fun, x0, plotIt=False) self.assertTrue(passed, True)
def test_FaceInnerProductAnisotropicDeriv(self): def fun(x): # fake anisotropy (tests anistropic implementation with isotropic # vector). First order behavior expected for fully anisotropic x = np.repeat(np.atleast_2d(x), 3, axis=0).T x0 = np.repeat(self.x0, 3, axis=0).T zero = sp.csr_matrix((self.mesh.nC, self.mesh.nC)) eye = sp.eye(self.mesh.nC) P = sp.vstack([sp.hstack([eye, zero, eye])]) MfSig = self.mesh.getFaceInnerProduct(x) MfSigDeriv = self.mesh.getFaceInnerProductDeriv(x0) return MfSig * self.face_vec, MfSigDeriv(self.face_vec) * P.T print("Testing FaceInnerProduct Anisotropic") return self.assertTrue( tests.checkDerivative(fun, self.x0, num=7, tolerance=TOLD, plotIt=False))
def test_simpleFail(self): def simpleFail(x): return np.sin(x), -sdiag(np.cos(x)) passed = checkDerivative(simpleFail, np.random.randn(5), plotIt=False) self.assertTrue(not passed, True)