def test_expression_6(self): function = Function('Tensor')(Symbol('g')) tensor = Tensor(function, dimension=3, weight=2) self.assertEqual( Parser._generate_liedrv(tensor, function, 'beta'), r'\mathcal{L}_\text{beta} g = \text{beta}^a \partial_a g + (2)(\partial_a \text{beta}^a) g' ) function = Function('Tensor')(Symbol('gUU'), Symbol('i'), Symbol('j')) tensor = Tensor(function, 3) self.assertEqual( Parser._generate_liedrv(tensor, function, 'beta'), r'\mathcal{L}_\text{beta} g^{i j} = \text{beta}^a \partial_a g^{i j} - (\partial_a \text{beta}^i) g^{a j} - (\partial_a \text{beta}^j) g^{i a}' ) function = Function('Tensor')(Symbol('gUD'), Symbol('i'), Symbol('j')) tensor = Tensor(function, 3) self.assertEqual( Parser._generate_liedrv(tensor, function, 'beta'), r'\mathcal{L}_\text{beta} g^i_j = \text{beta}^a \partial_a g^i_j - (\partial_a \text{beta}^i) g^a_j + (\partial_j \text{beta}^a) g^i_a' ) function = Function('Tensor')(Symbol('gDD'), Symbol('i'), Symbol('j')) tensor = Tensor(function, 3) self.assertEqual( Parser._generate_liedrv(tensor, function, 'beta'), r'\mathcal{L}_\text{beta} g_{i j} = \text{beta}^a \partial_a g_{i j} + (\partial_i \text{beta}^a) g_{a j} + (\partial_j \text{beta}^a) g_{i a}' )
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_example_6(self): Parser.continue_parsing(True) 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_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_expression_4(self): function = Function('Tensor')(Symbol('T')) tensor = Tensor(function, 4) self.assertEqual( Parser._generate_covdrv(tensor, function, [('beta', 'D')]), r'\nabla_\beta T = \partial_\beta (T)') function = Function('Tensor')(Symbol('TUU'), Symbol('mu'), Symbol('nu')) tensor = Tensor(function, 4) self.assertEqual( Parser._generate_covdrv(tensor, function, [('beta', 'D')]), r'\nabla_\beta T^{\mu \nu} = \partial_\beta (T^{\mu \nu}) + \Gamma^\mu_{a \beta} (T^{a \nu}) + \Gamma^\nu_{a \beta} (T^{\mu a})' ) function = Function('Tensor')(Symbol('TUD'), Symbol('mu'), Symbol('nu')) tensor = Tensor(function, 4) self.assertEqual( Parser._generate_covdrv(tensor, function, [('beta', 'D')]), r'\nabla_\beta T^\mu_\nu = \partial_\beta (T^\mu_\nu) + \Gamma^\mu_{a \beta} (T^a_\nu) - \Gamma^a_{\nu \beta} (T^\mu_a)' ) function = Function('Tensor')(Symbol('TDD'), Symbol('mu'), Symbol('nu')) tensor = Tensor(function, 4) self.assertEqual( Parser._generate_covdrv(tensor, function, [('beta', 'D')]), r'\nabla_\beta T_{\mu \nu} = \partial_\beta (T_{\mu \nu}) - \Gamma^a_{\mu \beta} (T_{a \nu}) - \Gamma^a_{\nu \beta} (T_{\mu a})' )
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_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_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_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_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_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_assignment_2(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % define vU (2D), const w % assign numeric vU % define index [a-z] (2D) T^a_c = \partial_c (v^a w) """)), {'vU', 'vU_dD', 'TUD'}) self.assertEqual(str(TUD), '[[vU_dD00*w, vU_dD01*w], [vU_dD10*w, vU_dD11*w]]')
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]')
def test_assignment_2(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % vardef -const 'w' % vardef -numeric 'vU' (2D) % keydef index [a-z] (2D) T^a_c = \partial_c (v^a w) """)), {'vU', 'vU_dD', 'TUD'}) self.assertEqual(str(TUD), '[[vU_dD00*w, vU_dD01*w], [vU_dD10*w, vU_dD11*w]]')
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_4_2(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % vardef -anti01 'FUU' (4D) % vardef -metric 'ghatDD' (4D) % vardef -const 'k' J^\mu = (4\pi k)^{-1} \vphantom{numeric} \hat{\nabla}_\nu F^{\mu\nu} """)), { 'FUU', 'ghatUU', 'ghatdet', 'epsilonUUUU', 'ghatDD', 'FUU_dD', 'ghatDD_dD', 'GammahatUDD', 'FUU_cdhatD', 'JU' })
def test_assignment_4(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % define basis [x, y], deriv _d; % define vD (2D); v_0 = x^{{2}} + 2x; v_1 = y\sqrt{x}; T_{\mu\nu} = (\partial_\nu v_\mu)\,\partial^2_x v_0 """)), {'vD', 'x', 'y', 'vD_dD', 'TDD'}) self.assertEqual(str(TDD), '[[2*vD_dD00, 2*vD_dD01], [2*vD_dD10, 2*vD_dD11]]')
def test_example_2(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % vardef -metric 'gUU' (3D) % vardef 'vD' (3D) v^\mu = g^{\mu\nu} v_\nu """)), {'gDD', 'gdet', 'epsilonDDD', '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_assignment_3(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % vardef -numeric -metric 'gDD' (4D) % vardef -numeric 'vU' (4D) % keydef index [a-z] (4D) T^{ab} = \nabla^b v^a """)), { 'gUU', 'gdet', 'epsilonUUUU', 'gDD', 'vU', 'vU_dD', 'gDD_dD', 'GammaUDD', 'vU_cdD', 'vU_cdU', 'TUU' })
def test_assignment_3(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % define metric gDD (4D), vU (4D) % assign numeric gDD, vU % define index [a-z] (4D) T^{ab} = \nabla^b v^a """)), { 'gUU', 'gDD', 'vU', 'vU_dD', 'gDD_dD', 'GammaUDD', 'vU_cdD', 'vU_cdU', 'TUU' })
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_assignment_6(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % define vD (2D), uD (2D), wD (2D) % define index [a-z] (2D) T_{abc} = \vphantom{numeric} ((v_a + u_a)_{,b} - w_{a,b})_{,c} """)), { 'vD', 'uD', 'wD', 'TDDD', 'uD_dD', 'vD_dD', 'wD_dD', 'wD_dDD', 'uD_dDD', 'vD_dDD' }) self.assertEqual(str(TDDD[0][0][0]), 'uD_dDD000 + vD_dDD000 - wD_dDD000')
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_expression_5(self): tensor = Tensor(Function('Tensor')(Symbol('vU'), Symbol('mu')), 4) self.assertEqual( Parser._generate_covdrv(tensor, [('a', 'U'), ('b', 'D')], 'symbolic'), r'\nabla_a \nabla_b v^\mu = \partial_a (\partial_b (v^\mu) + \Gamma^\mu_{c b} (v^c)) + \Gamma^\mu_{c a} (\partial_b (v^c) + \Gamma^c_{d b} (v^d))' )
def test_expression_5(self): function = Function('Tensor')(Symbol('vU'), Symbol('mu')) tensor = Tensor(function, 4) self.assertEqual( Parser._generate_covdrv(tensor, function, [('a', 'D'), ('b', 'D')]), r'\nabla_a \nabla_b v^\mu = \partial_a (\partial_b (v^\mu) + \Gamma^\mu_{c b} (v^c)) + \Gamma^\mu_{c a} (\partial_b (v^c) + \Gamma^c_{d b} (v^d)) - \Gamma^c_{b a} (\partial_c (v^\mu) + \Gamma^\mu_{b c} (v^b))' )
def test_assignment_4(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % keydef basis [x, y] % vardef 'uD' (2D), 'wD' (2D) % keydef index [a-z] (2D) u_0 = x^{{2}} + 2x \\ u_1 = y\sqrt{x} \\ v_a = u_a + w_a \\ % assign -numeric 'wD', 'vD' T_{ab} = \partial^2_x v_0 (\partial_b v_a) """)), {'uD', 'wD', 'vD', 'vD_dD', 'wD_dD', 'TDD'}) self.assertEqual( str(TDD), '[[2*wD_dD00 + 4*x + 4, 2*wD_dD01], [2*wD_dD10 + y/sqrt(x), 2*wD_dD11 + 2*sqrt(x)]]' )
def test_assignment_5(self): Parser.clear_namespace() self.assertEqual( set( parse(r""" % define basis [x, y] % define uD (2D), wD (2D) % assign symbolic <H> uD % define index [a-z] (2D) u_0 = x^{{2}} + 2x \\ u_1 = y\sqrt{x} \\ v_a = u_a + w_a \\ T_{ab} = \partial^2_x v_0 (\vphantom{numeric} \partial_b v_a) """)), {'uD', 'wD', 'vD', 'vD_dD', 'wD_dD', 'TDD'}) self.assertEqual( str(TDD), '[[2*wD_dD00 + 4*x + 4, 2*wD_dD01], [2*wD_dD10 + y/sqrt(x), 2*wD_dD11 + 2*sqrt(x)]]' )