Example #1
0
 def test_multiple_vector_valued(self):
     """
     For a function that returns a vector rather than a scalar, make
     sure Dofs.f(), Dofs.jac(), and Dofs.fd_jac() behave correctly.
     """
     for nparams1 in range(1, 5):
         for nvals1 in range(1, 5):
             nparams2 = np.random.randint(1, 6)
             nparams3 = np.random.randint(1, 6)
             nvals2 = np.random.randint(1, 6)
             nvals3 = np.random.randint(1, 6)
             o1 = Affine(nparams=nparams1, nvals=nvals1)
             o2 = Affine(nparams=nparams2, nvals=nvals2)
             o3 = Affine(nparams=nparams3, nvals=nvals3)
             dofs = Dofs([o1, o2, o3], diff_method="centered")
             dofs.set(
                 (np.random.rand(nparams1 + nparams2 + nparams3) - 0.5) * 4)
             f1 = np.matmul(o1.A, o1.x) + o1.B
             f2 = np.matmul(o2.A, o2.x) + o2.B
             f3 = np.matmul(o3.A, o3.x) + o3.B
             np.testing.assert_allclose(dofs.f(), np.concatenate((f1, f2, f3)), \
                                        rtol=1e-13, atol=1e-13)
             true_jac = np.zeros(
                 (nvals1 + nvals2 + nvals3, nparams1 + nparams2 + nparams3))
             true_jac[0:nvals1, 0:nparams1] = o1.A
             true_jac[nvals1:nvals1 + nvals2,
                      nparams1:nparams1 + nparams2] = o2.A
             true_jac[nvals1 + nvals2:nvals1 + nvals2 + nvals3, \
                      nparams1 + nparams2:nparams1 + nparams2 + nparams3] = o3.A
             np.testing.assert_allclose(dofs.jac(),
                                        true_jac,
                                        rtol=1e-13,
                                        atol=1e-13)
             np.testing.assert_allclose(dofs.fd_jac(), \
                                        true_jac, rtol=1e-7, atol=1e-7)
Example #2
0
 def test_mixed_vector_valued(self):
     """
     For a mixture of functions that return a scalar vs return a
     vector, make sure Dofs.f(), Dofs.jac(), and Dofs.fd_jac()
     behave correctly.
     """
     for nparams1 in range(1, 5):
         for nvals1 in range(1, 5):
             nparams2 = np.random.randint(1, 6)
             nparams3 = np.random.randint(1, 6)
             nvals2 = np.random.randint(1, 6)
             nvals3 = np.random.randint(1, 6)
             o1 = Affine(nparams=nparams1, nvals=nvals1)
             o2 = Affine(nparams=nparams2, nvals=nvals2)
             o3 = Affine(nparams=nparams3, nvals=nvals3)
             a1 = Adder(n=2)
             a2 = Adder(n=3)
             dofs = Dofs([o1, o2, a1, o3, a2], diff_method="centered")
             dofs.set(
                 (np.random.rand(nparams1 + nparams2 + nparams3 + 5) - 0.5)
                 * 4)
             f1 = np.matmul(o1.A, o1.x) + o1.B
             f2 = np.matmul(o2.A, o2.x) + o2.B
             f3 = np.array([a1.f])
             f4 = np.matmul(o3.A, o3.x) + o3.B
             f5 = np.array([a2.f])
             np.testing.assert_allclose(dofs.f(), np.concatenate((f1, f2, f3, f4, f5)), \
                                        rtol=1e-13, atol=1e-13)
             true_jac = np.zeros((nvals1 + nvals2 + nvals3 + 2,
                                  nparams1 + nparams2 + nparams3 + 5))
             true_jac[0:nvals1, 0:nparams1] = o1.A
             true_jac[nvals1:nvals1 + nvals2,
                      nparams1:nparams1 + nparams2] = o2.A
             true_jac[nvals1 + nvals2:nvals1 + nvals2 + 1, \
                      nparams1 + nparams2:nparams1 + nparams2 + 2] = np.ones(2)
             true_jac[nvals1 + nvals2 + 1:nvals1 + nvals2 + 1 + nvals3, \
                      nparams1 + nparams2 + 2:nparams1 + nparams2 + 2 + nparams3] = o3.A
             true_jac[nvals1 + nvals2 + 1 + nvals3:nvals1 + nvals2 + nvals3 + 2, \
                      nparams1 + nparams2 + nparams3 + 2:nparams1 + nparams2 + nparams3 + 5] = np.ones(3)
             np.testing.assert_allclose(dofs.jac(),
                                        true_jac,
                                        rtol=1e-13,
                                        atol=1e-13)
             np.testing.assert_allclose(dofs.fd_jac(), \
                                        true_jac, rtol=1e-7, atol=1e-7)
Example #3
0
 def test_vector_valued(self):
     """
     For a function that returns a vector rather than a scalar, make
     sure Dofs.f(), Dofs.jac(), and Dofs.fd_jac() behave correctly.
     """
     for nparams in range(1, 5):
         for nvals in range(1, 5):
             o = Affine(nparams=nparams, nvals=nvals)
             o.set_dofs((np.random.rand(nparams) - 0.5) * 4)
             dofs = Dofs([o], diff_method="centered")
             np.testing.assert_allclose(dofs.f(), np.matmul(o.A, o.x) + o.B, \
                                        rtol=1e-13, atol=1e-13)
             np.testing.assert_allclose(dofs.jac(),
                                        o.A,
                                        rtol=1e-13,
                                        atol=1e-13)
             np.testing.assert_allclose(dofs.fd_jac(), \
                                        o.A, rtol=1e-7, atol=1e-7)
Example #4
0
    def test_derivatives(self):
        """
        Check the automatic differentiation for area and volume.
        """
        for mpol in range(1, 3):
            for ntor in range(2):
                for nfp in range(1, 4):
                    s = SurfaceRZFourier(nfp=nfp, mpol=mpol, ntor=ntor)
                    x0 = s.get_dofs()
                    x = np.random.rand(len(x0)) - 0.5
                    x[0] = np.random.rand() + 2
                    # This surface will probably self-intersect, but I
                    # don't think this actually matters here.
                    s.set_dofs(x)

                    dofs = Dofs([s.area, s.volume])
                    jac = dofs.jac()
                    fd_jac = dofs.fd_jac()
                    print('difference for surface test_derivatives:',
                          jac - fd_jac)
                    np.testing.assert_allclose(jac,
                                               fd_jac,
                                               rtol=1e-4,
                                               atol=1e-4)
Example #5
0
    def test_Jacobian(self):
        for n in range(1, 20):
            v1 = np.random.rand() * 4 - 2
            v2 = np.random.rand() * 4 - 2
            o = TestObject2(v1, v2)
            o.adder.set_dofs(np.random.rand(2) * 4 - 2)
            o.t.set_dofs([np.random.rand() * 4 - 2])
            o.t.adder1.set_dofs(np.random.rand(3) * 4 - 2)
            o.t.adder2.set_dofs(np.random.rand(2) * 4 - 2)
            r = Rosenbrock(b=3.0)
            r.set_dofs(np.random.rand(2) * 3 - 1.5)
            a = Affine(nparams=3, nvals=3)

            # Randomly fix some of the degrees of freedom
            o.fixed = np.random.rand(2) > 0.5
            o.adder.fixed = np.random.rand(2) > 0.5
            o.t.adder1.fixed = np.random.rand(3) > 0.5
            o.t.adder2.fixed = np.random.rand(2) > 0.5
            r.fixed = np.random.rand(2) > 0.5
            a.fixed = np.random.rand(3) > 0.5

            rtol = 1e-6
            atol = 1e-6

            for j in range(4):
                # Try different sets of the objects:
                if j == 0:
                    dofs = Dofs([o.J, r.terms, o.t.J])
                    nvals = 4
                    nvals_per_func = [1, 2, 1]
                elif j == 1:
                    dofs = Dofs([r.term2, r.terms])
                    nvals = 3
                    nvals_per_func = [1, 2]
                elif j == 2:
                    dofs = Dofs(
                        [r.term2,
                         Target(o.t, 'f'), r.term1,
                         Target(o, 'f')])
                    nvals = 4
                    nvals_per_func = [1, 1, 1, 1]
                elif j == 3:
                    dofs = Dofs([a, o])
                    nvals = 4
                    nvals_per_func = [3, 1]

                jac = dofs.jac()
                dofs.diff_method = "forward"
                fd_jac = dofs.fd_jac()
                dofs.diff_method = "centered"
                fd_jac_centered = dofs.fd_jac()
                #print('j=', j, '  Diff in Jacobians:', jac - fd_jac)
                #print('jac: ', jac)
                #print('fd_jac: ', fd_jac)
                #print('fd_jac_centered: ', fd_jac_centered)
                #print('shapes: jac=', jac.shape, '  fd_jac=', fd_jac.shape, '  fd_jac_centered=', fd_jac_centered.shape)
                np.testing.assert_allclose(jac, fd_jac, rtol=rtol, atol=atol)
                np.testing.assert_allclose(fd_jac,
                                           fd_jac_centered,
                                           rtol=rtol,
                                           atol=atol)
                self.assertEqual(dofs.nvals, nvals)
                self.assertEqual(list(dofs.nvals_per_func), nvals_per_func)