def test_example_5_2(self): parse(r""" % define basis [t, r, \theta, \phi] \begin{align} R^\alpha{}_{\beta\mu\nu} &= \partial_\mu \Gamma^\alpha_{\beta\nu} - \partial_\nu \Gamma^\alpha_{\beta\mu} + \Gamma^\alpha_{\mu\gamma}\Gamma^\gamma_{\beta\nu} - \Gamma^\alpha_{\nu\sigma}\Gamma^\sigma_{\beta\mu} \\ R^{\alpha\beta\mu\nu} &= g^{\beta a} g^{\mu b} g^{\nu c} R^\alpha_{a b c} \\ R_{\alpha\beta\mu\nu} &= g_{\alpha a} R^a_{\beta\mu\nu} \\ K &= R^{\alpha\beta\mu\nu} R_{\alpha\beta\mu\nu} \\ R_{\beta\nu} &= R^\alpha_{\beta\alpha\nu} \\ R &= g^{\beta\nu} R_{\beta\nu} \\ G_{\beta\nu} &= R_{\beta\nu} - \frac{1}{2}g_{\beta\nu}R \end{align} """) self.assertEqual(0, simplify(GammaUDD[0][0][1] - GammaUDD[0][1][0])) self.assertEqual(str(GammaUDD[0][0][1]), '-G*M/(r**2*(2*G*M/r - 1))') self.assertEqual(str(GammaUDD[1][0][0]), 'G*M*(-2*G*M/r + 1)/r**2') self.assertEqual(str(GammaUDD[1][1][1]), '-G*M/(r**2*(-2*G*M/r + 1))') self.assertEqual(str(GammaUDD[1][3][3]), '-r*(-2*G*M/r + 1)*sin(theta)**2') self.assertEqual(0, simplify(GammaUDD[2][1][2] - GammaUDD[2][2][1])) self.assertEqual(str(GammaUDD[2][1][2]), '1/r') self.assertEqual(str(GammaUDD[2][3][3]), '-sin(theta)*cos(theta)') self.assertEqual(0, simplify(GammaUDD[2][1][3] - GammaUDD[2][3][1])) self.assertEqual(str(GammaUDD[3][1][3]), '1/r') self.assertEqual(0, simplify(GammaUDD[3][2][3] - GammaUDD[3][3][2])) self.assertEqual(str(GammaUDD[3][2][3]), 'cos(theta)/sin(theta)') self.assertEqual(str(simplify(K)), '48*G**2*M**2/r**6') self.assertEqual(simplify(R), 0) self.assertEqual( True, all(component == 0 for component in (row for row in simplify(Matrix(GDD)))))
def test_assignment_8(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % vardef -metric 'gDD' (4D) \gamma_{ij} = g_{ij} """)), {'gUU', 'gdet', 'epsilonUUUU', 'gDD', 'gammaDD'}) self.assertEqual( str(gammaDD), '[[gDD11, gDD12, gDD13], [gDD12, gDD22, gDD23], [gDD13, gDD23, gDD33]]' ) Parser.clear_namespace() self.assertEqual( set( parse(r""" % vardef -nosym 'TUU' (3D) % vardef 'vD' (2D) % keydef index i (2D) w^\mu = T^{\mu i} v_i """)), {'TUU', 'vD', 'wU'}) self.assertEqual( str(wU), '[TUU01*vD0 + TUU02*vD1, TUU11*vD0 + TUU12*vD1, TUU21*vD0 + TUU22*vD1]' )
def test_assignment_7(self): Parser.clear_namespace() parse(r""" % define basis [\theta, \phi] % define const r, deltaDD (2D) % define index [a-z] (2D) % parse g_{\mu\nu} = \delta_{\mu\nu} \begin{align*} g_{0 0} &= r^{{2}} \\ g_{1 1} &= r^{{2}} \sin^2(\theta) \end{align*} % assign metric gDD \begin{align*} R^\alpha_{\beta\mu\nu} &= \partial_\mu \Gamma^\alpha_{\beta\nu} - \partial_\nu \Gamma^\alpha_{\beta\mu} + \Gamma^\alpha_{\mu\gamma}\Gamma^\gamma_{\beta\nu} - \Gamma^\alpha_{\nu\sigma}\Gamma^\sigma_{\beta\mu} \\ R_{\alpha\beta\mu\nu} &= g_{\alpha a} R^a_{\beta\mu\nu} \\ R_{\beta\nu} &= R^\alpha_{\beta\alpha\nu} \\ R &= g^{\beta\nu} R_{\beta\nu} \end{align*} """) self.assertEqual(str(GammaUDD[0][1][1]), '-sin(theta)*cos(theta)') self.assertEqual(0, simplify(GammaUDD[1][0][1] - GammaUDD[1][1][0])) self.assertEqual(str(GammaUDD[1][0][1]), 'cos(theta)/sin(theta)') self.assertEqual( 0, simplify(RDDDD[0][1][0][1] - (-RDDDD[0][1][1][0]) + (-RDDDD[1][0][0][1]) - RDDDD[1][0][1][0])) self.assertEqual(str(RDDDD[0][1][0][1]), 'r**2*sin(theta)**2') self.assertEqual(RDD[0][0], 1) self.assertEqual(str(RDD[1][1]), 'sin(theta)**2') self.assertEqual(0, simplify(RDD[0][1] - RDD[1][0])) self.assertEqual(RDD[0][1], 0) self.assertEqual(str(R), '2/r**2')
def test_metric_inverse(self): # TODO: simplify -> assert_equal for DIM in range(2, 5): Parser.clear_namespace() parse(r""" % vardef -metric 'gDD' ({DIM}D) \delta^a_c = g^{{ab}} g_{{bc}} """.format(DIM=DIM)) # for i in range(DIM): # for j in range(DIM): # if i == j: # assert_equal(deltaUD[i][j], 1) # else: # assert_equal(deltaUD[i][j], 0) kronecker = simplify(Matrix(deltaUD)) self.assertEqual( True, all((simplify(kronecker[i, j]) == 1 if i == j else simplify(kronecker[i, j]) == 0 for j in range(DIM)) for i in range(DIM))) for DIM in range(2, 5): Parser.clear_namespace() parse(r""" % vardef -metric 'gUU' ({DIM}D) \delta^a_c = g^{{ab}} g_{{bc}} """.format(DIM=DIM)) kronecker = simplify(Matrix(deltaUD)) self.assertEqual( True, all((simplify(kronecker[i, j]) == 1 if i == j else simplify(kronecker[i, j]) == 0 for j in range(DIM)) for i in range(DIM)))
def test_example_BSSN(): import NRPy_param_funcs as par, reference_metric as rfm, BSSN.BSSN_RHSs as Brhs Parser.clear_namespace() parse(r""" % define deltaDD (3D), sym01 hDD (3D), sym01 aDD (3D), vetU (3D) % parse \hat{\gamma}_{ij} = \delta_{ij} % assign symbolic <H> gammahatDD % parse \bar{\gamma}_{ij} = h_{ij} + \hat{\gamma}_{ij} % assign numeric hDD, aDD, vetU, gammabarDD % assign metric gammahatDD, gammabarDD % srepl "\bar{A}" -> "a", "\beta" -> "\text{vet}" % parse \bar{A}^i_j = \bar{\gamma}^{ik} \bar{A}_{kj} \begin{align} % define basis [x, y, z] %% parse \partial_k \bar{\gamma}_{ij} = \vphantom{upwind} \partial_k h_{ij} + \partial_k \hat{\gamma}_{ij} % srepl "\text{vet}^<1> \partial_<1>" -> "\text{vet}^<1> \vphantom{upwind} \partial_<1>" %% (inside Lie derivative expansion) % srepl "\bar{D}_k \text{vet}^k" -> "(\partial_k \text{vet}^k + \frac{\text{vet}^k \vphantom{symbolic} \partial_k \text{gammabardet}}{2 \text{gammabardet}})" % srepl "\partial_t \bar{\gamma}" -> "\text{h_rhs}" \partial_t \bar{\gamma}_{ij} &= \mathcal{L}_\beta \bar{\gamma}_{ij} + \frac{2}{3} \bar{\gamma}_{ij} \left(\alpha \bar{A}^k{}_k - \bar{D}_k \beta^k\right) - 2 \alpha \bar{A}_{ij} % srepl "K" -> "\text{trK}", "\phi" -> "\text{cf}", "\partial_t \text{cf}" -> "\text{cf_rhs}" \partial_t \phi &= \mathcal{L}_\beta \phi - \frac{1}{3} \phi \left(\bar{D}_k \beta^k - \alpha K \right) \end{align} """) par.set_parval_from_str('reference_metric::CoordSystem', 'Cartesian') rfm.reference_metric() Brhs.BSSN_RHSs() assert_equal({ 'h_rhsDD': h_rhsDD, 'cf_rhs': cf_rhs }, { 'h_rhsDD': Brhs.h_rhsDD, 'cf_rhs': Brhs.cf_rhs })
def test_assignment_4(self): parse(r""" % define basis [x, y], deriv _d; % define vD (2); v_0 = x^{{2}} + 2x; v_1 = y\sqrt{x}; T_{\mu\nu} = (\partial_\nu v_\mu)\,\partial^2_x (y\sqrt{x}) """) self.assertEqual( str(TDD), '[[-vD_dD00*y/(4*x**(3/2)), -vD_dD01*y/(4*x**(3/2))], [-vD_dD10*y/(4*x**(3/2)), -vD_dD11*y/(4*x**(3/2))]]' )
def test_expression_5(self): Parser.clear_namespace() parse(r""" % vardef -numeric -metric 'gDD' (4D) % vardef -numeric 'vU' (4D) T^\mu_b = \nabla_b v^\mu """) tensor = Parser._namespace['vU_cdD'] function = Parser._namespace['vU_cdD'].equation[0] self.assertEqual( Parser._generate_covdrv(tensor, function, 'a'), r'\nabla_a \nabla_b v^\mu = \partial_a (\nabla_b v^\mu) + \text{Gamma}^\mu_{c a} (\nabla_b v^c) - \text{Gamma}^c_{b a} (\nabla_c v^\mu)' )
def test_example_6_2(self): parse(r""" \begin{align} K^{ij} &= \gamma^{ik} \gamma^{jl} K_{kl} \\ R_{ij} &= \partial_k \Gamma^k_{ij} - \partial_j \Gamma^k_{ik} + \Gamma^k_{ij}\Gamma^l_{kl} - \Gamma^l_{ik}\Gamma^k_{lj} \\ R &= \gamma^{ij} R_{ij} \\ E &= \frac{1}{16\pi}\left(R + K^{{2}} - K_{ij}K^{ij}\right) \\ p_i &= \frac{1}{8\pi}\left(D_j \gamma^{jk} K_{ki} - D_i K\right) \end{align} """) self.assertEqual(simplify(E), 0) self.assertEqual(True, all(component == 0 for component in pD))
def test_example_5(self): parse(r""" % define kronecker deltaDD (4); % define const G, const M; % parse g_{\mu\nu} = \delta_{\mu\nu}; \begin{align} g_{0 0} &= -\left(1 - \frac{2GM}{r}\right) \\ g_{1 1} &= \left(1 - \frac{2GM}{r}\right)^{-1} \\ g_{2 2} &= r^{{2}} \\ g_{3 3} &= r^{{2}} \sin^2\theta \end{align} % assign metric gDD """) self.assertEqual(str(gdet), 'r**4*(2*G*M/r - 1)*sin(theta)**2/(-2*G*M/r + 1)')
def test_assignment_3(self): self.assertEqual( parse(r""" % define vU (4); T^{\mu\nu} = \vphantom{_d} \nabla^\nu v^\mu """), ('vU', 'gDD', 'gdet', 'gUU', 'vU_dD', 'gDD_dD', 'GammaUDD', 'vU_cdD', 'vU_cdU', 'TUU'))
def test_assignment_1(self): self.assertEqual( parse(r""" % define vU (2), wU (2); T^{ab}_c = \vphantom{_d} \partial_c (v^a w^b) """), ('vU', 'wU', 'vU_dD', 'wU_dD', 'TUUD')) self.assertEqual(str(TUUD[0][0][0]), 'vU0*wU_dD00 + vU_dD00*wU0')
def test_example_4_2(self): self.assertEqual( parse(r""" % define anti01 FUU (4), const k; J^\mu = (4\pi k)^{-1} \vphantom{_d} \hat{\nabla}_\nu F^{\mu\nu} """), ('FUU', 'k', 'FUU_dD', 'ghatDD', 'ghatdet', 'ghatUU', 'ghatDD_dD', 'GammahatUDD', 'FUU_cdhatD', 'JU'))
def test_example_4_1(self): self.assertEqual( parse(r""" % define anti01 FUU (4), const k; J^\mu = (4\pi k)^{-1} \vphantom{_d} \nabla_\nu F^{\mu\nu} """), ('FUU', 'k', 'FUU_dD', 'gDD', 'gdet', 'gUU', 'gDD_dD', 'GammaUDD', 'FUU_cdD', 'JU'))
def test_example_6_1(self): parse(r""" % define basis [r, \theta, \phi] \begin{align} \gamma_{ij} &= g_{ij} \\ % assign metric gammaDD \beta_i &= g_{0 i} \\ \alpha &= \sqrt{\gamma^{ij}\beta_i\beta_j - g_{0 0}} \\ K_{ij} &= \frac{1}{2\alpha}\left(\nabla_i \beta_j + \nabla_j \beta_i\right) \\ K &= \gamma^{ij} K_{ij} \end{align} """) self.assertEqual( True, all(component == 0 for component in (row for row in simplify(Matrix(KDD)))))
def test_srepl_macro(self): Parser.clear_namespace() parse(r""" % srepl "<1>'" -> "\text{<1>prime}" % srepl "\text{<1..>}_<2>" -> "\text{(<1..>)<2>}" % srepl "<1>_{<2>}" -> "<1>_<2>", "<1>_<2>" -> "\text{<1>_<2>}" % srepl "\text{(<1..>)<2>}" -> "\text{<1..>_<2>}" % srepl "<1>^{<2>}" -> "<1>^<2>", "<1>^<2>" -> "<1>^{{<2>}}" """) expr = r"x_n^4 + x'_n \exp(x_n y_n^2)" self.assertEqual(str(parse_expr(expr)), "x_n**4 + xprime_n*exp(x_n*y_n**2)") Parser.clear_namespace() parse(r""" % srepl "<1>'^{<2..>}" -> "\text{<1>prime}" """) expr = r"v'^{label}" self.assertEqual(str(parse_expr(expr)), "vprime")
def test_example_1(self): self.assertEqual( parse(r""" % define nosym hUD (4); h = h^\mu{}_\mu """), ('hUD', 'h')) self.assertEqual(str(h), 'hUD00 + hUD11 + hUD22 + hUD33')
def test_assignment_2(self): self.assertEqual( parse(r""" % define vU (2), const w; T^a_c = \vphantom{_d} \partial_c (v^a w) """), ('vU', 'w', 'vU_dD', 'TUD')) self.assertEqual(str(TUD), '[[vU_dD00*w, vU_dD01*w], [vU_dD10*w, vU_dD11*w]]')
def test_example_1(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % define nosym hUD (4D) h = h^\mu{}_\mu """)), {'hUD', 'h'}) self.assertEqual(str(h), 'hUD00 + hUD11 + hUD22 + hUD33')
def test_assignment_1(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % define vU (2D), wU (2D); T^{ab}_c = \vphantom{_d} \partial_c (v^a w^b) """)), {'vU', 'wU', 'vU_dD', 'wU_dD', 'TUUD'}) self.assertEqual(str(TUUD[0][0][0]), 'vU0*wU_dD00 + vU_dD00*wU0')
def test_example_2(self): self.assertEqual( parse(r""" % define metric gUU (3), vD (3); v^\mu = g^{\mu\nu} v_\nu """), ('gDD', 'gdet', 'gUU', 'vD', 'vU')) self.assertEqual( str(vU), '[gUU00*vD0 + gUU01*vD1 + gUU02*vD2, gUU01*vD0 + gUU11*vD1 + gUU12*vD2, gUU02*vD0 + gUU12*vD1 + gUU22*vD2]' )
def test_example_3(self): self.assertEqual( parse(r""" % define permutation epsilonDDD (3); % define vU (3), wU (3); u_i = \epsilon_{ijk} v^j w^k """), ('epsilonDDD', 'vU', 'wU', 'uD')) self.assertEqual( str(uD), '[vU1*wU2 - vU2*wU1, -vU0*wU2 + vU2*wU0, vU0*wU1 - vU1*wU0]')
def test_assignment_2(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % define vU (2D), const w; T^a_c = \vphantom{_d} \partial_c (v^a w) """)), {'vU', 'w', 'vU_dD', 'TUD'}) self.assertEqual(str(TUD), '[[vU_dD00*w, vU_dD01*w], [vU_dD10*w, vU_dD11*w]]')
def test_assignment_1(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % vardef -numeric 'vU' (2D), 'wU' (2D) % keydef index [a-z] (2D) T^{ab}_c = \partial_c (v^a w^b) """)), {'vU', 'wU', 'vU_dD', 'wU_dD', 'TUUD'}) self.assertEqual(str(TUUD[0][0][0]), 'vU0*wU_dD00 + vU_dD00*wU0')
def test_example_4_1(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % define anti01 FUU (4D), metric gDD (4D), const k; J^\mu = (4\pi k)^{-1} \vphantom{_d} \nabla_\nu F^{\mu\nu} """)), { 'FUU', 'gUU', 'gdet', 'gDD', 'k', 'FUU_dD', 'gDD_dD', 'GammaUDD', 'FUU_cdD', 'JU' })
def test_assignment_6(self): self.assertEqual( parse(r""" % define metric gDD (4); % define index [i-j] = 1:3; \gamma_{ij} = g_{ij} """), ('gUU', 'gdet', 'gDD', 'gammaDD')) self.assertEqual( str(gammaDD), '[[gDD11, gDD12, gDD13], [gDD12, gDD22, gDD23], [gDD13, gDD23, gDD33]]' )
def test_assignment_3(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % define metric gDD (4D), vU (4D); T^{\mu\nu} = \vphantom{_d} \nabla^\nu v^\mu """)), { 'gUU', 'gdet', 'gDD', 'vU', 'vU_dD', 'gDD_dD', 'GammaUDD', 'vU_cdD', 'vU_cdU', 'TUU' })
def test_example_5_1(self): Parser.clear_namespace() parse(r""" % define deltaDD (4D) % define const G, const M % parse g_{\mu\nu} = \delta_{\mu\nu} \begin{align} g_{0 0} &= -\left(1 - \frac{2GM}{r}\right) \\ g_{1 1} &= \left(1 - \frac{2GM}{r}\right)^{-1} \\ g_{2 2} &= r^{{2}} \\ g_{3 3} &= r^{{2}} \sin^2\theta \end{align} % assign metric gDD """) self.assertEqual(str(gDD[0][0]), '2*G*M/r - 1') self.assertEqual(str(gDD[1][1]), '1/(-2*G*M/r + 1)') self.assertEqual(str(gDD[2][2]), 'r**2') self.assertEqual(str(gDD[3][3]), 'r**2*sin(theta)**2') self.assertEqual(str(gdet), 'r**4*(2*G*M/r - 1)*sin(theta)**2/(-2*G*M/r + 1)')
def test_example_4_2(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % define anti01 FUU (4D), metric ghatDD (4D), const k J^\mu = (4\pi k)^{-1} \vphantom{numeric} \hat{\nabla}_\nu F^{\mu\nu} """)), { 'FUU', 'ghatUU', 'ghatDD', 'FUU_dD', 'ghatDD_dD', 'GammahatUDD', 'FUU_cdhatD', 'JU' })
def test_example_2(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % define metric gUU (3D), vD (3D) v^\mu = g^{\mu\nu} v_\nu """)), {'gDD', 'gUU', 'vD', 'vU'}) self.assertEqual( str(vU), '[gUU00*vD0 + gUU01*vD1 + gUU02*vD2, gUU01*vD0 + gUU11*vD1 + gUU12*vD2, gUU02*vD0 + gUU12*vD1 + gUU22*vD2]' )
def test_example_3(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % define epsilonDDD (3D) % define vU (3D), wU (3D) u_i = \epsilon_{ijk} v^j w^k """)), {'epsilonDDD', 'vU', 'wU', 'uD'}) self.assertEqual( str(uD), '[vU1*wU2 - vU2*wU1, -vU0*wU2 + vU2*wU0, vU0*wU1 - vU1*wU0]')