def test_vangenuchten_theta_u(self): mesh = Mesh.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 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 test_vangenuchten_theta_m(self): mesh = Mesh.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_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_haverkamp_theta_u(self): mesh = Mesh.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 _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.survey.dpred(m), J], self.mtrue, num=4, plotIt=False) self.assertTrue(passed, True)
def _dotest_sensitivity(self): print('Testing Richards Derivative dim={}'.format(self.mesh.dim)) passed = checkDerivative( lambda m: [self.survey.dpred(m), lambda v: self.prob.Jvec(m, v)], self.mtrue, num=4, plotIt=False) self.assertTrue(passed, True)
def test_vangenuchten_theta_u(self): mesh = Mesh.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 test_haverkamp_theta_u(self): mesh = Mesh.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_k_u(self): mesh = discretize.TensorMesh([50]) van = richards.empirical.Vangenuchten_k(mesh) print("Vangenuchten_k test u deriv") passed = checkDerivative(lambda u: (van(u), van.derivU(u)), np.random.randn(mesh.nC), plotIt=False) self.assertTrue(passed, True)
def test_haverkamp_k_u(self): mesh = Mesh.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 _dotest_sensitivity(self): print('Testing Richards Derivative dim={}'.format( self.mesh.dim )) passed = checkDerivative( lambda m: [self.survey.dpred(m), lambda v: self.prob.Jvec(m, v)], self.mtrue, num=4, 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 _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.timeSteps[0], self.prob. boundary_conditions), self.h0, expectedOrder=2 if newton else 1, 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_haverkamp_k_u(self): mesh = Mesh.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 _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.survey.dpred(m), J], self.mtrue, num=4, plotIt=False ) self.assertTrue(passed, True)
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 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_haverkamp_k_m(self): mesh = Mesh.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_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 test_vangenuchten_k_m(self): mesh = Mesh.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 _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.timeSteps[0], self.prob.boundary_conditions ), self.h0, expectedOrder=2 if newton else 1, plotIt=False ) self.assertTrue(passed, True)
def test_vangenuchten_theta_m(self): mesh = Mesh.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_haverkamp_k_m(self): mesh = Mesh.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_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_vangenuchten_k_m(self): mesh = Mesh.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_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)