From 90ed0580638e7e3dbad3d8836d52cf691a88add2 Mon Sep 17 00:00:00 2001 From: cjayross Date: Wed, 3 Jul 2019 19:46:10 -0500 Subject: [PATCH 01/18] added source files --- docs/Makefile | 177 ++++++++++++++++++++++ src/riccipy/__init__.py | 1 - src/test_symbolic/ipy_test_environment.py | 30 ++++ src/test_symbolic/test_metric.py | 100 ++++++++++++ src/test_symbolic/test_partial.py | 34 +++++ src/test_symbolic/test_tensor.py | 149 ++++++++++++++++++ 6 files changed, 490 insertions(+), 1 deletion(-) create mode 100644 docs/Makefile create mode 100644 src/test_symbolic/ipy_test_environment.py create mode 100644 src/test_symbolic/test_metric.py create mode 100644 src/test_symbolic/test_partial.py create mode 100644 src/test_symbolic/test_tensor.py diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..0f83864 --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,177 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = build + +# User-friendly check for sphinx-build +ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) +$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) +endif + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source + +.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext + +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " xml to make Docutils-native XML files" + @echo " pseudoxml to make pseudoxml-XML files for display purposes" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + +clean: + rm -rf $(BUILDDIR)/* + +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/einsteinpy.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/einsteinpy.qhc" + +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/einsteinpy" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/einsteinpy" + @echo "# devhelp" + +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +latexpdfja: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through platex and dvipdfmx..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +texinfo: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +info: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +gettext: + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." + +xml: + $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml + @echo + @echo "Build finished. The XML files are in $(BUILDDIR)/xml." + +pseudoxml: + $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml + @echo + @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/src/riccipy/__init__.py b/src/riccipy/__init__.py index 589f1e6..19b1913 100644 --- a/src/riccipy/__init__.py +++ b/src/riccipy/__init__.py @@ -1,5 +1,4 @@ """ -======= RicciPy ======= diff --git a/src/test_symbolic/ipy_test_environment.py b/src/test_symbolic/ipy_test_environment.py new file mode 100644 index 0000000..445354a --- /dev/null +++ b/src/test_symbolic/ipy_test_environment.py @@ -0,0 +1,30 @@ +from sympy import * + +from riccipy.metric import * +from riccipy.partial import * +from riccipy.tensor import * + +init_printing() + +t, r, th, ph, x, y, z = symbols("t r theta phi x y z", real=True) +momentum = symbols("E p_1:4", positive=True) +E, p1, p2, p3 = momentum +# schw = diag(1-1/r, -1/(1-1/r), -r**2, -r**2*sin(th)**2) +alpha, beta = symbols("alpha beta", cls=Function) +schw = diag(-exp(2 * alpha(r)), exp(2 * beta(r)), r ** 2, r ** 2 * sin(th) ** 2) +g = Metric("g", [t, r, th, ph], schw) +x = Tensor("x", [t, r, th, ph], g) +p = Tensor("p", momentum, g) +mu, nu, si, rh, la = indices("mu nu sigma rho lambda", g) +d = g.partial +Gamma = g.christoffel +R = g.riemann +G = g.einstein +C = g.weyl + +# mink = diag(1, -1, -1, -1) +# eta = Metric('eta', [t, x, y, z], mink) +# al, be, ga = indices('alpha beta gamma', eta) +# E1, E2, E3, B1, B2, B3 = symbols('E_1:4 B_1:4', real=True) +# matrix = [[0, -E1, -E2, -E3], [E1, 0, -B3, B2], [E2, B3, 0, -B1], [E3, -B2, B1, 0]] +# F = Tensor('F', matrix, eta, symmetry=[[2]]) diff --git a/src/test_symbolic/test_metric.py b/src/test_symbolic/test_metric.py new file mode 100644 index 0000000..93ba727 --- /dev/null +++ b/src/test_symbolic/test_metric.py @@ -0,0 +1,100 @@ +from sympy import Expr, diag, sin, symbols, zeros + +from riccipy.metric import * +from riccipy.partial import * +from riccipy.tensor import * + + +def _generate_schwarzschild(): + coords = symbols("t r theta phi", real=True) + t, r, th, ph = coords + schw = diag(1 - 1 / r, -1 / (1 - 1 / r), -r ** 2, -r ** 2 * sin(th) ** 2) + g = SpacetimeMetric("g", coords, schw, timelike=True) + mu, nu = indices("mu nu", g) + return (coords, t, r, th, ph, schw, g, mu, nu) + + +def test_Metric(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + assert isinstance(g, AbstractTensor) + assert isinstance(g, TensorIndexType) + assert isinstance(g(mu, nu), IndexedTensor) + assert isinstance(g.metric, Tensor) + assert isinstance(g.partial, PartialDerivative) + assert g.metric.covar == (-1, -1) + + +def test_SpacetimeMetric(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + array1 = g.as_array() + assert g.signature == (1, -1, -1, -1) + rev_sig = g.reverse_signature() + array2 = g.as_array() + assert rev_sig == (-1, 1, 1, 1) + assert array1 == -1 * array2 + + +def test_Metric_christoffel(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + gamma = g.christoffel.simplify() + assert ( + str(gamma) + == "[[[0, 0.5/(r*(r - 1)), 0, 0], [0.5/(r*(r - 1)), 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0.5*(r - 1)/r**3, 0, 0, 0], [0, -0.5/(r*(r - 1)), 0, 0], [0, 0, 1.0 - 1.0*r, 0], [0, 0, 0, (1.0 - 1.0*r)*sin(theta)**2]], [[0, 0, 0, 0], [0, 0, 1.0/r, 0], [0, 1.0/r, 0, 0], [0, 0, 0, -0.5*sin(2*theta)]], [[0, 0, 0, 0], [0, 0, 0, 1.0/r], [0, 0, 0, 1.0/tan(theta)], [0, 1.0/r, 1.0/tan(theta), 0]]]" + ) + + +def test_Metric_riemann(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + rh, si = indices("rho sigma", g) + R = g.riemann + + def is_zero(arr): + for comp in arr: + yield comp.equals(0) + + expr = R(-rh, -si, -mu, -nu) + R(-si, -rh, -mu, -nu) + assert all(is_zero(expand_tensor(expr))) + expr = R(-rh, -si, -mu, -nu) + R(-rh, -si, -nu, -mu) + assert all(is_zero(expand_tensor(expr))) + expr = R(-rh, -si, -mu, -nu) - R(-mu, -nu, -rh, -si) + assert all(is_zero(expand_tensor(expr))) + expr = R(-rh, -si, -mu, -nu) + R(-rh, -mu, -nu, -si) + R(-rh, -nu, -si, -mu) + assert all(is_zero(expand_tensor(expr))) + + +def test_Metric_ricci_tensor(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + R = g.ricci_tensor + assert zeros(4).equals(R.as_array()) + + +def test_Metric_ricci_scalar(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + R = g.ricci_scalar + assert isinstance(R, Expr) + assert R.equals(0) + + +def test_Metric_einstein(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + G = g.einstein + assert zeros(4).equals(G.as_array()) + + +def test_Metric_weyl(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + rh, si = indices("rho sigma", g) + C = g.weyl + + def is_zero(arr): + for comp in arr: + yield comp.equals(0) + + expr = C(-rh, -si, -mu, -nu) + C(-si, -rh, -mu, -nu) + assert all(is_zero(expand_tensor(expr))) + expr = C(-rh, -si, -mu, -nu) + C(-rh, -si, -nu, -mu) + assert all(is_zero(expand_tensor(expr))) + expr = C(-rh, -si, -mu, -nu) - C(-mu, -nu, -rh, -si) + assert all(is_zero(expand_tensor(expr))) + expr = C(-rh, -si, -mu, -nu) + C(-rh, -mu, -nu, -si) + C(-rh, -nu, -si, -mu) + assert all(is_zero(expand_tensor(expr))) diff --git a/src/test_symbolic/test_partial.py b/src/test_symbolic/test_partial.py new file mode 100644 index 0000000..5b8ee4d --- /dev/null +++ b/src/test_symbolic/test_partial.py @@ -0,0 +1,34 @@ +from sympy import diag, sin, symbols + +from riccipy.metric import * +from riccipy.partial import * +from riccipy.tensor import * + + +def _generate_schwarzschild(): + coords = symbols("t r theta phi", real=True) + t, r, th, ph = coords + schw = diag(1 - 1 / r, -1 / (1 - 1 / r), -r ** 2, -r ** 2 * sin(th) ** 2) + g = SpacetimeMetric("g", coords, schw, timelike=True) + mu, nu = indices("mu nu", g) + return (coords, t, r, th, ph, schw, g, mu, nu) + + +def test_DiffOperator(): + r = Symbol("r") + dr = DiffOperator(r) + expr = dr * (1 - 1 / r) + assert expr == 1 / r ** 2 + expr = (1 - 1 / r) * dr + assert isinstance(expr, DiffOperator) + assert (expr * (r ** 3 / 3)).equals(r * (r - 1)) + + +def test_PartialDerivative(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + d = g.partial + assert isinstance(d, PartialDerivative) + assert isinstance(d, Tensor) + assert d.commutes_with(d) == 0 + assert d.covar == (-1,) + assert all([isinstance(dx, DiffOperator) for dx in d.as_array()]) diff --git a/src/test_symbolic/test_tensor.py b/src/test_symbolic/test_tensor.py new file mode 100644 index 0000000..ab68c7e --- /dev/null +++ b/src/test_symbolic/test_tensor.py @@ -0,0 +1,149 @@ +from sympy import Array, diag, eye, simplify, sin, symbols +from sympy.tensor.tensor import TensExpr, TensMul + +from riccipy.metric import * +from riccipy.tensor import * + + +def _generate_simple(): + coords = symbols("x y z", real=True) + metric = Metric("metric", coords, eye(3)) + return (coords, metric) + + +def _generate_schwarzschild(): + coords = symbols("t r theta phi", real=True) + t, r, th, ph = coords + schw = diag(1 - 1 / r, -1 / (1 - 1 / r), -r ** 2, -r ** 2 * sin(th) ** 2) + g = Metric("g", coords, schw) + mu, nu = indices("mu nu", g) + return (coords, t, r, th, ph, schw, g, mu, nu) + + +def _generate_minkowski(): + coords = symbols("t x y z", real=True) + t, x, y, z = coords + mink = diag(1, -1, -1, -1) + eta = Metric("eta", coords, mink) + mu, nu = indices("mu nu", eta) + return (coords, t, x, y, z, mink, eta, mu, nu) + + +def test_Index(): + (coords, metric) = _generate_simple() + mu = Index("mu", metric) + assert isinstance(mu, TensorIndex) + assert mu.is_up + assert not (-mu).is_up + + +def test_indices(): + (coords, metric) = _generate_simple() + idxs = indices("i0:4", metric) + assert len(idxs) == 4 + assert all([isinstance(i, Index) for i in idxs]) + + +def test_Tensor(): + from sympy.tensor.tensor import tensorsymmetry + + (coords, metric) = _generate_simple() + T = Tensor("T", coords, metric) + assert isinstance(T, TensorHead) + assert T.as_array() == Array(coords) + assert T.covar == (1,) + assert T.symmetry == tensorsymmetry([1]) + + +def test_Tensor_comm(): + (coords, metric) = _generate_simple() + T = Tensor("T", coords, metric) + assert T.commutes_with(T) == 0 + assert T.commutes_with(metric) == 0 + assert T.commutes_with(metric.partial) is None + + +def test_Tensor_simplify(): + (coords, metric) = _generate_simple() + x, y, z = coords + expr = -(1 - x) ** 2 / (x - 1) + expr_simplified = 1 - x + T = Tensor("T", 3 * [expr], metric) + assert str(T.simplify()[0]) == str(expr_simplified) + + +def test_Tensor_covariance_transform(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + E, p1, p2, p3 = symbols("E p_1:4", positive=True) + p = Tensor("p", [E, p1, p2, p3], g) + res = p.covariance_transform(-mu) + expect = Array( + [E * (1 - 1 / r), -p1 / (1 - 1 / r), -p2 * r ** 2, -p3 * r ** 2 * sin(th) ** 2] + ) + assert res[0].equals(expect[0]) + assert res[1].equals(expect[1]) + assert res[2].equals(expect[2]) + assert res[3].equals(expect[3]) + + +def test_AbstractTensor(): + (coords, metric) = _generate_simple() + T = Tensor("T", coords, metric) + assert isinstance(T, AbstractTensor) + assert isinstance(T.as_array(), Array) + assert T.is_Tensor + assert not T.is_Metric + assert not T.is_Spacetime + + +def test_IndexedTensor(): + (coords, metric) = _generate_simple() + T = Tensor("T", coords, metric) + mu = Index("mu", metric) + assert isinstance(T(mu), IndexedTensor) + + +def test_ReplacementManager(): + pass + + +def test_expand_tensor(): + (coords, t, r, th, ph, mink, eta, mu, nu) = _generate_minkowski() + E1, E2, E3, B1, B2, B3 = symbols("E_1:4 B_1:4", real=True) + matrix = [[0, -E1, -E2, -E3], [E1, 0, -B3, B2], [E2, B3, 0, -B1], [E3, -B2, B1, 0]] + F = Tensor("F", matrix, eta, symmetry=[[2]]) + assert expand_tensor(eta(mu, nu) * eta(-mu, -nu)) == 4 + assert expand_tensor(F(mu, -mu)) == 0 + assert ( + expand_tensor(F(mu, nu) * F(-mu, -nu)) + == 2 * B1 ** 2 + + 2 * B2 ** 2 + + 2 * B3 ** 2 + - 2 * E1 ** 2 + - 2 * E2 ** 2 + - 2 * E3 ** 2 + ) + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + x = Tensor("x", [t, r, th, ph], g) + res = expand_tensor(g(mu, nu)) + assert schw.inv().equals(res) + res1 = expand_tensor(g(mu, nu) * g(-mu, -nu)) + res2 = expand_tensor(g(-mu, -nu) * g(mu, nu)) + assert res1 == res2 + assert simplify(res1) == 4 + res1 = expand_tensor(g(-mu, -nu) * x(nu)) + res2 = expand_tensor(x(-mu)) + res3 = x.covariance_transform(-mu) + assert res1 == res2 + assert res2 == res3 + res1 = expand_tensor(g(mu, nu) * x(-nu)) + res2 = expand_tensor(x(mu)) + assert simplify(res1) == res2 + res = expand_tensor(x(mu) * x(-mu)) + assert ( + res + == t ** 2 * (1 - 1 / r) + - r ** 2 / (1 - 1 / r) + - th ** 2 * r ** 2 + - ph ** 2 * r ** 2 * sin(th) ** 2 + ) From f22376a7cba69438d029068cdbd153ed25c558bf Mon Sep 17 00:00:00 2001 From: cjayross Date: Wed, 3 Jul 2019 20:09:54 -0500 Subject: [PATCH 02/18] added .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index a9f548a..1224cdc 100644 --- a/.gitignore +++ b/.gitignore @@ -66,6 +66,9 @@ pip-wheel-metadata # VS Code .vscode +# Emacs - projectile +TAGS + # default pytest cache directory */.pytest_cache .pytest_cache/ From 3dcca53593450e002665c17dbac1b9c6a633ffa6 Mon Sep 17 00:00:00 2001 From: cjayross Date: Wed, 3 Jul 2019 20:12:28 -0500 Subject: [PATCH 03/18] added myself as an author --- AUTHORS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/AUTHORS b/AUTHORS index 8412821..0c81213 100644 --- a/AUTHORS +++ b/AUTHORS @@ -9,3 +9,6 @@ * Priyanshu Khandelwal Primary Author + +* Calvin Jay Ross + Primary Author From e495709132b3d2db68ef7587e5ed3829d0eff1fe Mon Sep 17 00:00:00 2001 From: cjayross Date: Wed, 3 Jul 2019 20:14:14 -0500 Subject: [PATCH 04/18] reorganized test files --- .../tests}/ipy_test_environment.py | 0 src/test_symbolic/test_metric.py | 100 ------------ src/test_symbolic/test_partial.py | 34 ---- src/test_symbolic/test_tensor.py | 149 ------------------ 4 files changed, 283 deletions(-) rename src/{test_symbolic => riccipy/tests}/ipy_test_environment.py (100%) delete mode 100644 src/test_symbolic/test_metric.py delete mode 100644 src/test_symbolic/test_partial.py delete mode 100644 src/test_symbolic/test_tensor.py diff --git a/src/test_symbolic/ipy_test_environment.py b/src/riccipy/tests/ipy_test_environment.py similarity index 100% rename from src/test_symbolic/ipy_test_environment.py rename to src/riccipy/tests/ipy_test_environment.py diff --git a/src/test_symbolic/test_metric.py b/src/test_symbolic/test_metric.py deleted file mode 100644 index 93ba727..0000000 --- a/src/test_symbolic/test_metric.py +++ /dev/null @@ -1,100 +0,0 @@ -from sympy import Expr, diag, sin, symbols, zeros - -from riccipy.metric import * -from riccipy.partial import * -from riccipy.tensor import * - - -def _generate_schwarzschild(): - coords = symbols("t r theta phi", real=True) - t, r, th, ph = coords - schw = diag(1 - 1 / r, -1 / (1 - 1 / r), -r ** 2, -r ** 2 * sin(th) ** 2) - g = SpacetimeMetric("g", coords, schw, timelike=True) - mu, nu = indices("mu nu", g) - return (coords, t, r, th, ph, schw, g, mu, nu) - - -def test_Metric(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - assert isinstance(g, AbstractTensor) - assert isinstance(g, TensorIndexType) - assert isinstance(g(mu, nu), IndexedTensor) - assert isinstance(g.metric, Tensor) - assert isinstance(g.partial, PartialDerivative) - assert g.metric.covar == (-1, -1) - - -def test_SpacetimeMetric(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - array1 = g.as_array() - assert g.signature == (1, -1, -1, -1) - rev_sig = g.reverse_signature() - array2 = g.as_array() - assert rev_sig == (-1, 1, 1, 1) - assert array1 == -1 * array2 - - -def test_Metric_christoffel(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - gamma = g.christoffel.simplify() - assert ( - str(gamma) - == "[[[0, 0.5/(r*(r - 1)), 0, 0], [0.5/(r*(r - 1)), 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0.5*(r - 1)/r**3, 0, 0, 0], [0, -0.5/(r*(r - 1)), 0, 0], [0, 0, 1.0 - 1.0*r, 0], [0, 0, 0, (1.0 - 1.0*r)*sin(theta)**2]], [[0, 0, 0, 0], [0, 0, 1.0/r, 0], [0, 1.0/r, 0, 0], [0, 0, 0, -0.5*sin(2*theta)]], [[0, 0, 0, 0], [0, 0, 0, 1.0/r], [0, 0, 0, 1.0/tan(theta)], [0, 1.0/r, 1.0/tan(theta), 0]]]" - ) - - -def test_Metric_riemann(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - rh, si = indices("rho sigma", g) - R = g.riemann - - def is_zero(arr): - for comp in arr: - yield comp.equals(0) - - expr = R(-rh, -si, -mu, -nu) + R(-si, -rh, -mu, -nu) - assert all(is_zero(expand_tensor(expr))) - expr = R(-rh, -si, -mu, -nu) + R(-rh, -si, -nu, -mu) - assert all(is_zero(expand_tensor(expr))) - expr = R(-rh, -si, -mu, -nu) - R(-mu, -nu, -rh, -si) - assert all(is_zero(expand_tensor(expr))) - expr = R(-rh, -si, -mu, -nu) + R(-rh, -mu, -nu, -si) + R(-rh, -nu, -si, -mu) - assert all(is_zero(expand_tensor(expr))) - - -def test_Metric_ricci_tensor(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - R = g.ricci_tensor - assert zeros(4).equals(R.as_array()) - - -def test_Metric_ricci_scalar(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - R = g.ricci_scalar - assert isinstance(R, Expr) - assert R.equals(0) - - -def test_Metric_einstein(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - G = g.einstein - assert zeros(4).equals(G.as_array()) - - -def test_Metric_weyl(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - rh, si = indices("rho sigma", g) - C = g.weyl - - def is_zero(arr): - for comp in arr: - yield comp.equals(0) - - expr = C(-rh, -si, -mu, -nu) + C(-si, -rh, -mu, -nu) - assert all(is_zero(expand_tensor(expr))) - expr = C(-rh, -si, -mu, -nu) + C(-rh, -si, -nu, -mu) - assert all(is_zero(expand_tensor(expr))) - expr = C(-rh, -si, -mu, -nu) - C(-mu, -nu, -rh, -si) - assert all(is_zero(expand_tensor(expr))) - expr = C(-rh, -si, -mu, -nu) + C(-rh, -mu, -nu, -si) + C(-rh, -nu, -si, -mu) - assert all(is_zero(expand_tensor(expr))) diff --git a/src/test_symbolic/test_partial.py b/src/test_symbolic/test_partial.py deleted file mode 100644 index 5b8ee4d..0000000 --- a/src/test_symbolic/test_partial.py +++ /dev/null @@ -1,34 +0,0 @@ -from sympy import diag, sin, symbols - -from riccipy.metric import * -from riccipy.partial import * -from riccipy.tensor import * - - -def _generate_schwarzschild(): - coords = symbols("t r theta phi", real=True) - t, r, th, ph = coords - schw = diag(1 - 1 / r, -1 / (1 - 1 / r), -r ** 2, -r ** 2 * sin(th) ** 2) - g = SpacetimeMetric("g", coords, schw, timelike=True) - mu, nu = indices("mu nu", g) - return (coords, t, r, th, ph, schw, g, mu, nu) - - -def test_DiffOperator(): - r = Symbol("r") - dr = DiffOperator(r) - expr = dr * (1 - 1 / r) - assert expr == 1 / r ** 2 - expr = (1 - 1 / r) * dr - assert isinstance(expr, DiffOperator) - assert (expr * (r ** 3 / 3)).equals(r * (r - 1)) - - -def test_PartialDerivative(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - d = g.partial - assert isinstance(d, PartialDerivative) - assert isinstance(d, Tensor) - assert d.commutes_with(d) == 0 - assert d.covar == (-1,) - assert all([isinstance(dx, DiffOperator) for dx in d.as_array()]) diff --git a/src/test_symbolic/test_tensor.py b/src/test_symbolic/test_tensor.py deleted file mode 100644 index ab68c7e..0000000 --- a/src/test_symbolic/test_tensor.py +++ /dev/null @@ -1,149 +0,0 @@ -from sympy import Array, diag, eye, simplify, sin, symbols -from sympy.tensor.tensor import TensExpr, TensMul - -from riccipy.metric import * -from riccipy.tensor import * - - -def _generate_simple(): - coords = symbols("x y z", real=True) - metric = Metric("metric", coords, eye(3)) - return (coords, metric) - - -def _generate_schwarzschild(): - coords = symbols("t r theta phi", real=True) - t, r, th, ph = coords - schw = diag(1 - 1 / r, -1 / (1 - 1 / r), -r ** 2, -r ** 2 * sin(th) ** 2) - g = Metric("g", coords, schw) - mu, nu = indices("mu nu", g) - return (coords, t, r, th, ph, schw, g, mu, nu) - - -def _generate_minkowski(): - coords = symbols("t x y z", real=True) - t, x, y, z = coords - mink = diag(1, -1, -1, -1) - eta = Metric("eta", coords, mink) - mu, nu = indices("mu nu", eta) - return (coords, t, x, y, z, mink, eta, mu, nu) - - -def test_Index(): - (coords, metric) = _generate_simple() - mu = Index("mu", metric) - assert isinstance(mu, TensorIndex) - assert mu.is_up - assert not (-mu).is_up - - -def test_indices(): - (coords, metric) = _generate_simple() - idxs = indices("i0:4", metric) - assert len(idxs) == 4 - assert all([isinstance(i, Index) for i in idxs]) - - -def test_Tensor(): - from sympy.tensor.tensor import tensorsymmetry - - (coords, metric) = _generate_simple() - T = Tensor("T", coords, metric) - assert isinstance(T, TensorHead) - assert T.as_array() == Array(coords) - assert T.covar == (1,) - assert T.symmetry == tensorsymmetry([1]) - - -def test_Tensor_comm(): - (coords, metric) = _generate_simple() - T = Tensor("T", coords, metric) - assert T.commutes_with(T) == 0 - assert T.commutes_with(metric) == 0 - assert T.commutes_with(metric.partial) is None - - -def test_Tensor_simplify(): - (coords, metric) = _generate_simple() - x, y, z = coords - expr = -(1 - x) ** 2 / (x - 1) - expr_simplified = 1 - x - T = Tensor("T", 3 * [expr], metric) - assert str(T.simplify()[0]) == str(expr_simplified) - - -def test_Tensor_covariance_transform(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - E, p1, p2, p3 = symbols("E p_1:4", positive=True) - p = Tensor("p", [E, p1, p2, p3], g) - res = p.covariance_transform(-mu) - expect = Array( - [E * (1 - 1 / r), -p1 / (1 - 1 / r), -p2 * r ** 2, -p3 * r ** 2 * sin(th) ** 2] - ) - assert res[0].equals(expect[0]) - assert res[1].equals(expect[1]) - assert res[2].equals(expect[2]) - assert res[3].equals(expect[3]) - - -def test_AbstractTensor(): - (coords, metric) = _generate_simple() - T = Tensor("T", coords, metric) - assert isinstance(T, AbstractTensor) - assert isinstance(T.as_array(), Array) - assert T.is_Tensor - assert not T.is_Metric - assert not T.is_Spacetime - - -def test_IndexedTensor(): - (coords, metric) = _generate_simple() - T = Tensor("T", coords, metric) - mu = Index("mu", metric) - assert isinstance(T(mu), IndexedTensor) - - -def test_ReplacementManager(): - pass - - -def test_expand_tensor(): - (coords, t, r, th, ph, mink, eta, mu, nu) = _generate_minkowski() - E1, E2, E3, B1, B2, B3 = symbols("E_1:4 B_1:4", real=True) - matrix = [[0, -E1, -E2, -E3], [E1, 0, -B3, B2], [E2, B3, 0, -B1], [E3, -B2, B1, 0]] - F = Tensor("F", matrix, eta, symmetry=[[2]]) - assert expand_tensor(eta(mu, nu) * eta(-mu, -nu)) == 4 - assert expand_tensor(F(mu, -mu)) == 0 - assert ( - expand_tensor(F(mu, nu) * F(-mu, -nu)) - == 2 * B1 ** 2 - + 2 * B2 ** 2 - + 2 * B3 ** 2 - - 2 * E1 ** 2 - - 2 * E2 ** 2 - - 2 * E3 ** 2 - ) - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - x = Tensor("x", [t, r, th, ph], g) - res = expand_tensor(g(mu, nu)) - assert schw.inv().equals(res) - res1 = expand_tensor(g(mu, nu) * g(-mu, -nu)) - res2 = expand_tensor(g(-mu, -nu) * g(mu, nu)) - assert res1 == res2 - assert simplify(res1) == 4 - res1 = expand_tensor(g(-mu, -nu) * x(nu)) - res2 = expand_tensor(x(-mu)) - res3 = x.covariance_transform(-mu) - assert res1 == res2 - assert res2 == res3 - res1 = expand_tensor(g(mu, nu) * x(-nu)) - res2 = expand_tensor(x(mu)) - assert simplify(res1) == res2 - res = expand_tensor(x(mu) * x(-mu)) - assert ( - res - == t ** 2 * (1 - 1 / r) - - r ** 2 / (1 - 1 / r) - - th ** 2 * r ** 2 - - ph ** 2 * r ** 2 * sin(th) ** 2 - ) From 4030936896eb886df66bb8cc533bd4f35f87bc4a Mon Sep 17 00:00:00 2001 From: cjayross Date: Wed, 3 Jul 2019 21:54:27 -0500 Subject: [PATCH 05/18] removed metric commutation group, added tox.ini, reformatted --- src/riccipy/tensor.py | 4 +- src/riccipy/tests/ipy_test_environment.py | 3 +- src/riccipy/tests/test_metric.py | 1 + src/riccipy/tests/test_partial.py | 3 +- src/riccipy/tests/test_tensor.py | 5 +- tox.ini | 73 +++++++++++++++++++++++ 6 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 tox.ini diff --git a/src/riccipy/tensor.py b/src/riccipy/tensor.py index 53a4473..5df7294 100644 --- a/src/riccipy/tensor.py +++ b/src/riccipy/tensor.py @@ -3,9 +3,9 @@ from sympy import Array, simplify, symbols from sympy.core.compatibility import string_types from sympy.tensor.array import permutedims, tensorcontraction, tensorproduct +from sympy.tensor.tensor import TensMul +from sympy.tensor.tensor import Tensor as SympyTensor from sympy.tensor.tensor import ( - TensMul, - Tensor as SympyTensor, TensorHead, TensorIndex, TensorManager, diff --git a/src/riccipy/tests/ipy_test_environment.py b/src/riccipy/tests/ipy_test_environment.py index 445354a..57d4936 100644 --- a/src/riccipy/tests/ipy_test_environment.py +++ b/src/riccipy/tests/ipy_test_environment.py @@ -1,8 +1,7 @@ -from sympy import * - from riccipy.metric import * from riccipy.partial import * from riccipy.tensor import * +from sympy import * init_printing() diff --git a/src/riccipy/tests/test_metric.py b/src/riccipy/tests/test_metric.py index 0e243bd..c67a754 100644 --- a/src/riccipy/tests/test_metric.py +++ b/src/riccipy/tests/test_metric.py @@ -3,6 +3,7 @@ from riccipy.metric import * from riccipy.partial import * from riccipy.tensor import * +from sympy import Expr, diag, sin, symbols, zeros def _generate_schwarzschild(): diff --git a/src/riccipy/tests/test_partial.py b/src/riccipy/tests/test_partial.py index 6b1402b..31b2149 100644 --- a/src/riccipy/tests/test_partial.py +++ b/src/riccipy/tests/test_partial.py @@ -1,8 +1,7 @@ -from sympy import diag, sin, symbols - from riccipy.metric import * from riccipy.partial import * from riccipy.tensor import * +from sympy import diag, sin, symbols def _generate_schwarzschild(): diff --git a/src/riccipy/tests/test_tensor.py b/src/riccipy/tests/test_tensor.py index ab68c7e..27623e4 100644 --- a/src/riccipy/tests/test_tensor.py +++ b/src/riccipy/tests/test_tensor.py @@ -1,8 +1,7 @@ -from sympy import Array, diag, eye, simplify, sin, symbols -from sympy.tensor.tensor import TensExpr, TensMul - from riccipy.metric import * from riccipy.tensor import * +from sympy import Array, diag, eye, simplify, sin, symbols +from sympy.tensor.tensor import TensExpr, TensMul def _generate_simple(): diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..43fc3f0 --- /dev/null +++ b/tox.ini @@ -0,0 +1,73 @@ +[tox] +description = list of environments againts tox runs the tests +envlist = + clean, + check, + docs, + py35, + py36 + py37 + pypy, + pypy3 + +[testenv] +basepython = + pypy: {env:PYTHON:pypy} + pypy3: {env:PYTHON:pypy3} + py35: {env:PYTHON:python3.5} + {py36,docs}: {env:PYTHON:python3.6} + py37: {env:PYTHON:python3.7} + {clean,check,reformat}: {env:PYTHON:python3} +setenv = + PYTHONPATH={toxinidir}/tests + PYTHONUNBUFFERED=yes +passenv = + * +usedevelop = false +deps = + pytest-cov + ipywidgets +skip_install = false +commands = + {posargs:pytest --cov --cov-report=term-missing -vv src/einsteinpy/tests} + + +[testenv:check] +description = this environments checks for flake8, black, isort and einsteinpy code style +deps = + black + docutils + isort + flake8 + mypy + pygments +skip_install = true +commands = + python setup.py check --strict --metadata --restructuredtext + flake8 src setup.py + isort --check-only --diff --recursive --project einsteinpy --section-default THIRDPARTY src setup.py + black --check src setup.py + mypy --ignore-missing-imports --check-untyped-defs --no-strict-optional src + + +[testenv:reformat] +description = reformats the code using black and isort +deps = + black + isort +skip_install = true +commands = + isort --recursive --project einsteinpy --section-default THIRDPARTY src setup.py + black src setup.py + + +[testenv:docs] +description = invoke sphinx-build to build the HTML docs +extras = docs +whitelist_externals = sphinx-build +commands = sphinx-build -d "{toxworkdir}/docs_doctree" docs/source "{toxworkdir}/docs_out" --color -W -bhtml + +[testenv:clean] +commands = coverage erase +skip_install = true +deps = coverage From d2bf9b1b2f133e9a8886aa30bda496f88f5c64ca Mon Sep 17 00:00:00 2001 From: cjayross Date: Wed, 3 Jul 2019 21:56:03 -0500 Subject: [PATCH 06/18] Revert "added myself as an author" This reverts commit 014c284369d69d04c7626ac213339d6a69878969. --- AUTHORS | 3 --- 1 file changed, 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index 0c81213..8412821 100644 --- a/AUTHORS +++ b/AUTHORS @@ -9,6 +9,3 @@ * Priyanshu Khandelwal Primary Author - -* Calvin Jay Ross - Primary Author From 84efb1cf18ccdc3f0e30c4a849fe3353ae9ec353 Mon Sep 17 00:00:00 2001 From: cjayross Date: Thu, 4 Jul 2019 05:04:48 -0500 Subject: [PATCH 07/18] removed ipy_test_environment and added more strict checks in ReplacementManager --- src/riccipy/tensor.py | 2 +- src/riccipy/tests/ipy_test_environment.py | 29 ----------------------- 2 files changed, 1 insertion(+), 30 deletions(-) delete mode 100644 src/riccipy/tests/ipy_test_environment.py diff --git a/src/riccipy/tensor.py b/src/riccipy/tensor.py index 5df7294..41a9ef9 100644 --- a/src/riccipy/tensor.py +++ b/src/riccipy/tensor.py @@ -29,7 +29,7 @@ class _ReplacementManager(dict): def get_key(self, tensor): item = tensor if (tensor.is_TensorHead or tensor.is_Metric) else tensor.args[0] for key in self.keys(): - if (key == item) or (key.args[0] == item): + if (key is item) or (key.args[0] is item): return key return None diff --git a/src/riccipy/tests/ipy_test_environment.py b/src/riccipy/tests/ipy_test_environment.py deleted file mode 100644 index 57d4936..0000000 --- a/src/riccipy/tests/ipy_test_environment.py +++ /dev/null @@ -1,29 +0,0 @@ -from riccipy.metric import * -from riccipy.partial import * -from riccipy.tensor import * -from sympy import * - -init_printing() - -t, r, th, ph, x, y, z = symbols("t r theta phi x y z", real=True) -momentum = symbols("E p_1:4", positive=True) -E, p1, p2, p3 = momentum -# schw = diag(1-1/r, -1/(1-1/r), -r**2, -r**2*sin(th)**2) -alpha, beta = symbols("alpha beta", cls=Function) -schw = diag(-exp(2 * alpha(r)), exp(2 * beta(r)), r ** 2, r ** 2 * sin(th) ** 2) -g = Metric("g", [t, r, th, ph], schw) -x = Tensor("x", [t, r, th, ph], g) -p = Tensor("p", momentum, g) -mu, nu, si, rh, la = indices("mu nu sigma rho lambda", g) -d = g.partial -Gamma = g.christoffel -R = g.riemann -G = g.einstein -C = g.weyl - -# mink = diag(1, -1, -1, -1) -# eta = Metric('eta', [t, x, y, z], mink) -# al, be, ga = indices('alpha beta gamma', eta) -# E1, E2, E3, B1, B2, B3 = symbols('E_1:4 B_1:4', real=True) -# matrix = [[0, -E1, -E2, -E3], [E1, 0, -B3, B2], [E2, B3, 0, -B1], [E3, -B2, B1, 0]] -# F = Tensor('F', matrix, eta, symmetry=[[2]]) From a3bd7d9433886f6d7d31c25415208a1ad01be243 Mon Sep 17 00:00:00 2001 From: cjayross Date: Fri, 5 Jul 2019 05:39:11 -0500 Subject: [PATCH 08/18] fixed test cases, loosened comparisons in ReplacementManager --- src/riccipy/tensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/riccipy/tensor.py b/src/riccipy/tensor.py index 41a9ef9..5df7294 100644 --- a/src/riccipy/tensor.py +++ b/src/riccipy/tensor.py @@ -29,7 +29,7 @@ class _ReplacementManager(dict): def get_key(self, tensor): item = tensor if (tensor.is_TensorHead or tensor.is_Metric) else tensor.args[0] for key in self.keys(): - if (key is item) or (key.args[0] is item): + if (key == item) or (key.args[0] == item): return key return None From 5f342a053c0614d5b099eafe7374ea5ecdf59d6e Mon Sep 17 00:00:00 2001 From: cjayross Date: Sat, 6 Jul 2019 19:36:31 -0500 Subject: [PATCH 09/18] reformatted --- src/riccipy/tensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/riccipy/tensor.py b/src/riccipy/tensor.py index 5df7294..53a4473 100644 --- a/src/riccipy/tensor.py +++ b/src/riccipy/tensor.py @@ -3,9 +3,9 @@ from sympy import Array, simplify, symbols from sympy.core.compatibility import string_types from sympy.tensor.array import permutedims, tensorcontraction, tensorproduct -from sympy.tensor.tensor import TensMul -from sympy.tensor.tensor import Tensor as SympyTensor from sympy.tensor.tensor import ( + TensMul, + Tensor as SympyTensor, TensorHead, TensorIndex, TensorManager, From 9a77b33359b602c4c6387d5fb1637e0fcc9a6f88 Mon Sep 17 00:00:00 2001 From: cjayross Date: Sat, 6 Jul 2019 19:38:32 -0500 Subject: [PATCH 10/18] ran isort --- src/riccipy/tests/test_metric.py | 1 - src/riccipy/tests/test_partial.py | 3 ++- src/riccipy/tests/test_tensor.py | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/riccipy/tests/test_metric.py b/src/riccipy/tests/test_metric.py index c67a754..0e243bd 100644 --- a/src/riccipy/tests/test_metric.py +++ b/src/riccipy/tests/test_metric.py @@ -3,7 +3,6 @@ from riccipy.metric import * from riccipy.partial import * from riccipy.tensor import * -from sympy import Expr, diag, sin, symbols, zeros def _generate_schwarzschild(): diff --git a/src/riccipy/tests/test_partial.py b/src/riccipy/tests/test_partial.py index 31b2149..6b1402b 100644 --- a/src/riccipy/tests/test_partial.py +++ b/src/riccipy/tests/test_partial.py @@ -1,7 +1,8 @@ +from sympy import diag, sin, symbols + from riccipy.metric import * from riccipy.partial import * from riccipy.tensor import * -from sympy import diag, sin, symbols def _generate_schwarzschild(): diff --git a/src/riccipy/tests/test_tensor.py b/src/riccipy/tests/test_tensor.py index 27623e4..ab68c7e 100644 --- a/src/riccipy/tests/test_tensor.py +++ b/src/riccipy/tests/test_tensor.py @@ -1,8 +1,9 @@ -from riccipy.metric import * -from riccipy.tensor import * from sympy import Array, diag, eye, simplify, sin, symbols from sympy.tensor.tensor import TensExpr, TensMul +from riccipy.metric import * +from riccipy.tensor import * + def _generate_simple(): coords = symbols("x y z", real=True) From df29ec85d8f1952d40e9413d1abee83d3a57ac9f Mon Sep 17 00:00:00 2001 From: cjayross Date: Wed, 3 Jul 2019 19:46:10 -0500 Subject: [PATCH 11/18] added source files --- src/test_symbolic/ipy_test_environment.py | 30 +++++ src/test_symbolic/test_metric.py | 100 +++++++++++++++ src/test_symbolic/test_partial.py | 34 +++++ src/test_symbolic/test_tensor.py | 149 ++++++++++++++++++++++ 4 files changed, 313 insertions(+) create mode 100644 src/test_symbolic/ipy_test_environment.py create mode 100644 src/test_symbolic/test_metric.py create mode 100644 src/test_symbolic/test_partial.py create mode 100644 src/test_symbolic/test_tensor.py diff --git a/src/test_symbolic/ipy_test_environment.py b/src/test_symbolic/ipy_test_environment.py new file mode 100644 index 0000000..445354a --- /dev/null +++ b/src/test_symbolic/ipy_test_environment.py @@ -0,0 +1,30 @@ +from sympy import * + +from riccipy.metric import * +from riccipy.partial import * +from riccipy.tensor import * + +init_printing() + +t, r, th, ph, x, y, z = symbols("t r theta phi x y z", real=True) +momentum = symbols("E p_1:4", positive=True) +E, p1, p2, p3 = momentum +# schw = diag(1-1/r, -1/(1-1/r), -r**2, -r**2*sin(th)**2) +alpha, beta = symbols("alpha beta", cls=Function) +schw = diag(-exp(2 * alpha(r)), exp(2 * beta(r)), r ** 2, r ** 2 * sin(th) ** 2) +g = Metric("g", [t, r, th, ph], schw) +x = Tensor("x", [t, r, th, ph], g) +p = Tensor("p", momentum, g) +mu, nu, si, rh, la = indices("mu nu sigma rho lambda", g) +d = g.partial +Gamma = g.christoffel +R = g.riemann +G = g.einstein +C = g.weyl + +# mink = diag(1, -1, -1, -1) +# eta = Metric('eta', [t, x, y, z], mink) +# al, be, ga = indices('alpha beta gamma', eta) +# E1, E2, E3, B1, B2, B3 = symbols('E_1:4 B_1:4', real=True) +# matrix = [[0, -E1, -E2, -E3], [E1, 0, -B3, B2], [E2, B3, 0, -B1], [E3, -B2, B1, 0]] +# F = Tensor('F', matrix, eta, symmetry=[[2]]) diff --git a/src/test_symbolic/test_metric.py b/src/test_symbolic/test_metric.py new file mode 100644 index 0000000..93ba727 --- /dev/null +++ b/src/test_symbolic/test_metric.py @@ -0,0 +1,100 @@ +from sympy import Expr, diag, sin, symbols, zeros + +from riccipy.metric import * +from riccipy.partial import * +from riccipy.tensor import * + + +def _generate_schwarzschild(): + coords = symbols("t r theta phi", real=True) + t, r, th, ph = coords + schw = diag(1 - 1 / r, -1 / (1 - 1 / r), -r ** 2, -r ** 2 * sin(th) ** 2) + g = SpacetimeMetric("g", coords, schw, timelike=True) + mu, nu = indices("mu nu", g) + return (coords, t, r, th, ph, schw, g, mu, nu) + + +def test_Metric(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + assert isinstance(g, AbstractTensor) + assert isinstance(g, TensorIndexType) + assert isinstance(g(mu, nu), IndexedTensor) + assert isinstance(g.metric, Tensor) + assert isinstance(g.partial, PartialDerivative) + assert g.metric.covar == (-1, -1) + + +def test_SpacetimeMetric(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + array1 = g.as_array() + assert g.signature == (1, -1, -1, -1) + rev_sig = g.reverse_signature() + array2 = g.as_array() + assert rev_sig == (-1, 1, 1, 1) + assert array1 == -1 * array2 + + +def test_Metric_christoffel(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + gamma = g.christoffel.simplify() + assert ( + str(gamma) + == "[[[0, 0.5/(r*(r - 1)), 0, 0], [0.5/(r*(r - 1)), 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0.5*(r - 1)/r**3, 0, 0, 0], [0, -0.5/(r*(r - 1)), 0, 0], [0, 0, 1.0 - 1.0*r, 0], [0, 0, 0, (1.0 - 1.0*r)*sin(theta)**2]], [[0, 0, 0, 0], [0, 0, 1.0/r, 0], [0, 1.0/r, 0, 0], [0, 0, 0, -0.5*sin(2*theta)]], [[0, 0, 0, 0], [0, 0, 0, 1.0/r], [0, 0, 0, 1.0/tan(theta)], [0, 1.0/r, 1.0/tan(theta), 0]]]" + ) + + +def test_Metric_riemann(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + rh, si = indices("rho sigma", g) + R = g.riemann + + def is_zero(arr): + for comp in arr: + yield comp.equals(0) + + expr = R(-rh, -si, -mu, -nu) + R(-si, -rh, -mu, -nu) + assert all(is_zero(expand_tensor(expr))) + expr = R(-rh, -si, -mu, -nu) + R(-rh, -si, -nu, -mu) + assert all(is_zero(expand_tensor(expr))) + expr = R(-rh, -si, -mu, -nu) - R(-mu, -nu, -rh, -si) + assert all(is_zero(expand_tensor(expr))) + expr = R(-rh, -si, -mu, -nu) + R(-rh, -mu, -nu, -si) + R(-rh, -nu, -si, -mu) + assert all(is_zero(expand_tensor(expr))) + + +def test_Metric_ricci_tensor(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + R = g.ricci_tensor + assert zeros(4).equals(R.as_array()) + + +def test_Metric_ricci_scalar(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + R = g.ricci_scalar + assert isinstance(R, Expr) + assert R.equals(0) + + +def test_Metric_einstein(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + G = g.einstein + assert zeros(4).equals(G.as_array()) + + +def test_Metric_weyl(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + rh, si = indices("rho sigma", g) + C = g.weyl + + def is_zero(arr): + for comp in arr: + yield comp.equals(0) + + expr = C(-rh, -si, -mu, -nu) + C(-si, -rh, -mu, -nu) + assert all(is_zero(expand_tensor(expr))) + expr = C(-rh, -si, -mu, -nu) + C(-rh, -si, -nu, -mu) + assert all(is_zero(expand_tensor(expr))) + expr = C(-rh, -si, -mu, -nu) - C(-mu, -nu, -rh, -si) + assert all(is_zero(expand_tensor(expr))) + expr = C(-rh, -si, -mu, -nu) + C(-rh, -mu, -nu, -si) + C(-rh, -nu, -si, -mu) + assert all(is_zero(expand_tensor(expr))) diff --git a/src/test_symbolic/test_partial.py b/src/test_symbolic/test_partial.py new file mode 100644 index 0000000..5b8ee4d --- /dev/null +++ b/src/test_symbolic/test_partial.py @@ -0,0 +1,34 @@ +from sympy import diag, sin, symbols + +from riccipy.metric import * +from riccipy.partial import * +from riccipy.tensor import * + + +def _generate_schwarzschild(): + coords = symbols("t r theta phi", real=True) + t, r, th, ph = coords + schw = diag(1 - 1 / r, -1 / (1 - 1 / r), -r ** 2, -r ** 2 * sin(th) ** 2) + g = SpacetimeMetric("g", coords, schw, timelike=True) + mu, nu = indices("mu nu", g) + return (coords, t, r, th, ph, schw, g, mu, nu) + + +def test_DiffOperator(): + r = Symbol("r") + dr = DiffOperator(r) + expr = dr * (1 - 1 / r) + assert expr == 1 / r ** 2 + expr = (1 - 1 / r) * dr + assert isinstance(expr, DiffOperator) + assert (expr * (r ** 3 / 3)).equals(r * (r - 1)) + + +def test_PartialDerivative(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + d = g.partial + assert isinstance(d, PartialDerivative) + assert isinstance(d, Tensor) + assert d.commutes_with(d) == 0 + assert d.covar == (-1,) + assert all([isinstance(dx, DiffOperator) for dx in d.as_array()]) diff --git a/src/test_symbolic/test_tensor.py b/src/test_symbolic/test_tensor.py new file mode 100644 index 0000000..ab68c7e --- /dev/null +++ b/src/test_symbolic/test_tensor.py @@ -0,0 +1,149 @@ +from sympy import Array, diag, eye, simplify, sin, symbols +from sympy.tensor.tensor import TensExpr, TensMul + +from riccipy.metric import * +from riccipy.tensor import * + + +def _generate_simple(): + coords = symbols("x y z", real=True) + metric = Metric("metric", coords, eye(3)) + return (coords, metric) + + +def _generate_schwarzschild(): + coords = symbols("t r theta phi", real=True) + t, r, th, ph = coords + schw = diag(1 - 1 / r, -1 / (1 - 1 / r), -r ** 2, -r ** 2 * sin(th) ** 2) + g = Metric("g", coords, schw) + mu, nu = indices("mu nu", g) + return (coords, t, r, th, ph, schw, g, mu, nu) + + +def _generate_minkowski(): + coords = symbols("t x y z", real=True) + t, x, y, z = coords + mink = diag(1, -1, -1, -1) + eta = Metric("eta", coords, mink) + mu, nu = indices("mu nu", eta) + return (coords, t, x, y, z, mink, eta, mu, nu) + + +def test_Index(): + (coords, metric) = _generate_simple() + mu = Index("mu", metric) + assert isinstance(mu, TensorIndex) + assert mu.is_up + assert not (-mu).is_up + + +def test_indices(): + (coords, metric) = _generate_simple() + idxs = indices("i0:4", metric) + assert len(idxs) == 4 + assert all([isinstance(i, Index) for i in idxs]) + + +def test_Tensor(): + from sympy.tensor.tensor import tensorsymmetry + + (coords, metric) = _generate_simple() + T = Tensor("T", coords, metric) + assert isinstance(T, TensorHead) + assert T.as_array() == Array(coords) + assert T.covar == (1,) + assert T.symmetry == tensorsymmetry([1]) + + +def test_Tensor_comm(): + (coords, metric) = _generate_simple() + T = Tensor("T", coords, metric) + assert T.commutes_with(T) == 0 + assert T.commutes_with(metric) == 0 + assert T.commutes_with(metric.partial) is None + + +def test_Tensor_simplify(): + (coords, metric) = _generate_simple() + x, y, z = coords + expr = -(1 - x) ** 2 / (x - 1) + expr_simplified = 1 - x + T = Tensor("T", 3 * [expr], metric) + assert str(T.simplify()[0]) == str(expr_simplified) + + +def test_Tensor_covariance_transform(): + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + E, p1, p2, p3 = symbols("E p_1:4", positive=True) + p = Tensor("p", [E, p1, p2, p3], g) + res = p.covariance_transform(-mu) + expect = Array( + [E * (1 - 1 / r), -p1 / (1 - 1 / r), -p2 * r ** 2, -p3 * r ** 2 * sin(th) ** 2] + ) + assert res[0].equals(expect[0]) + assert res[1].equals(expect[1]) + assert res[2].equals(expect[2]) + assert res[3].equals(expect[3]) + + +def test_AbstractTensor(): + (coords, metric) = _generate_simple() + T = Tensor("T", coords, metric) + assert isinstance(T, AbstractTensor) + assert isinstance(T.as_array(), Array) + assert T.is_Tensor + assert not T.is_Metric + assert not T.is_Spacetime + + +def test_IndexedTensor(): + (coords, metric) = _generate_simple() + T = Tensor("T", coords, metric) + mu = Index("mu", metric) + assert isinstance(T(mu), IndexedTensor) + + +def test_ReplacementManager(): + pass + + +def test_expand_tensor(): + (coords, t, r, th, ph, mink, eta, mu, nu) = _generate_minkowski() + E1, E2, E3, B1, B2, B3 = symbols("E_1:4 B_1:4", real=True) + matrix = [[0, -E1, -E2, -E3], [E1, 0, -B3, B2], [E2, B3, 0, -B1], [E3, -B2, B1, 0]] + F = Tensor("F", matrix, eta, symmetry=[[2]]) + assert expand_tensor(eta(mu, nu) * eta(-mu, -nu)) == 4 + assert expand_tensor(F(mu, -mu)) == 0 + assert ( + expand_tensor(F(mu, nu) * F(-mu, -nu)) + == 2 * B1 ** 2 + + 2 * B2 ** 2 + + 2 * B3 ** 2 + - 2 * E1 ** 2 + - 2 * E2 ** 2 + - 2 * E3 ** 2 + ) + (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() + x = Tensor("x", [t, r, th, ph], g) + res = expand_tensor(g(mu, nu)) + assert schw.inv().equals(res) + res1 = expand_tensor(g(mu, nu) * g(-mu, -nu)) + res2 = expand_tensor(g(-mu, -nu) * g(mu, nu)) + assert res1 == res2 + assert simplify(res1) == 4 + res1 = expand_tensor(g(-mu, -nu) * x(nu)) + res2 = expand_tensor(x(-mu)) + res3 = x.covariance_transform(-mu) + assert res1 == res2 + assert res2 == res3 + res1 = expand_tensor(g(mu, nu) * x(-nu)) + res2 = expand_tensor(x(mu)) + assert simplify(res1) == res2 + res = expand_tensor(x(mu) * x(-mu)) + assert ( + res + == t ** 2 * (1 - 1 / r) + - r ** 2 / (1 - 1 / r) + - th ** 2 * r ** 2 + - ph ** 2 * r ** 2 * sin(th) ** 2 + ) From 662781a6c61d2b913f3ab00dec70de006bc51578 Mon Sep 17 00:00:00 2001 From: cjayross Date: Wed, 3 Jul 2019 20:14:14 -0500 Subject: [PATCH 12/18] reorganized test files --- .../tests}/ipy_test_environment.py | 0 src/test_symbolic/test_metric.py | 100 ------------ src/test_symbolic/test_partial.py | 34 ---- src/test_symbolic/test_tensor.py | 149 ------------------ 4 files changed, 283 deletions(-) rename src/{test_symbolic => riccipy/tests}/ipy_test_environment.py (100%) delete mode 100644 src/test_symbolic/test_metric.py delete mode 100644 src/test_symbolic/test_partial.py delete mode 100644 src/test_symbolic/test_tensor.py diff --git a/src/test_symbolic/ipy_test_environment.py b/src/riccipy/tests/ipy_test_environment.py similarity index 100% rename from src/test_symbolic/ipy_test_environment.py rename to src/riccipy/tests/ipy_test_environment.py diff --git a/src/test_symbolic/test_metric.py b/src/test_symbolic/test_metric.py deleted file mode 100644 index 93ba727..0000000 --- a/src/test_symbolic/test_metric.py +++ /dev/null @@ -1,100 +0,0 @@ -from sympy import Expr, diag, sin, symbols, zeros - -from riccipy.metric import * -from riccipy.partial import * -from riccipy.tensor import * - - -def _generate_schwarzschild(): - coords = symbols("t r theta phi", real=True) - t, r, th, ph = coords - schw = diag(1 - 1 / r, -1 / (1 - 1 / r), -r ** 2, -r ** 2 * sin(th) ** 2) - g = SpacetimeMetric("g", coords, schw, timelike=True) - mu, nu = indices("mu nu", g) - return (coords, t, r, th, ph, schw, g, mu, nu) - - -def test_Metric(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - assert isinstance(g, AbstractTensor) - assert isinstance(g, TensorIndexType) - assert isinstance(g(mu, nu), IndexedTensor) - assert isinstance(g.metric, Tensor) - assert isinstance(g.partial, PartialDerivative) - assert g.metric.covar == (-1, -1) - - -def test_SpacetimeMetric(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - array1 = g.as_array() - assert g.signature == (1, -1, -1, -1) - rev_sig = g.reverse_signature() - array2 = g.as_array() - assert rev_sig == (-1, 1, 1, 1) - assert array1 == -1 * array2 - - -def test_Metric_christoffel(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - gamma = g.christoffel.simplify() - assert ( - str(gamma) - == "[[[0, 0.5/(r*(r - 1)), 0, 0], [0.5/(r*(r - 1)), 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0.5*(r - 1)/r**3, 0, 0, 0], [0, -0.5/(r*(r - 1)), 0, 0], [0, 0, 1.0 - 1.0*r, 0], [0, 0, 0, (1.0 - 1.0*r)*sin(theta)**2]], [[0, 0, 0, 0], [0, 0, 1.0/r, 0], [0, 1.0/r, 0, 0], [0, 0, 0, -0.5*sin(2*theta)]], [[0, 0, 0, 0], [0, 0, 0, 1.0/r], [0, 0, 0, 1.0/tan(theta)], [0, 1.0/r, 1.0/tan(theta), 0]]]" - ) - - -def test_Metric_riemann(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - rh, si = indices("rho sigma", g) - R = g.riemann - - def is_zero(arr): - for comp in arr: - yield comp.equals(0) - - expr = R(-rh, -si, -mu, -nu) + R(-si, -rh, -mu, -nu) - assert all(is_zero(expand_tensor(expr))) - expr = R(-rh, -si, -mu, -nu) + R(-rh, -si, -nu, -mu) - assert all(is_zero(expand_tensor(expr))) - expr = R(-rh, -si, -mu, -nu) - R(-mu, -nu, -rh, -si) - assert all(is_zero(expand_tensor(expr))) - expr = R(-rh, -si, -mu, -nu) + R(-rh, -mu, -nu, -si) + R(-rh, -nu, -si, -mu) - assert all(is_zero(expand_tensor(expr))) - - -def test_Metric_ricci_tensor(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - R = g.ricci_tensor - assert zeros(4).equals(R.as_array()) - - -def test_Metric_ricci_scalar(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - R = g.ricci_scalar - assert isinstance(R, Expr) - assert R.equals(0) - - -def test_Metric_einstein(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - G = g.einstein - assert zeros(4).equals(G.as_array()) - - -def test_Metric_weyl(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - rh, si = indices("rho sigma", g) - C = g.weyl - - def is_zero(arr): - for comp in arr: - yield comp.equals(0) - - expr = C(-rh, -si, -mu, -nu) + C(-si, -rh, -mu, -nu) - assert all(is_zero(expand_tensor(expr))) - expr = C(-rh, -si, -mu, -nu) + C(-rh, -si, -nu, -mu) - assert all(is_zero(expand_tensor(expr))) - expr = C(-rh, -si, -mu, -nu) - C(-mu, -nu, -rh, -si) - assert all(is_zero(expand_tensor(expr))) - expr = C(-rh, -si, -mu, -nu) + C(-rh, -mu, -nu, -si) + C(-rh, -nu, -si, -mu) - assert all(is_zero(expand_tensor(expr))) diff --git a/src/test_symbolic/test_partial.py b/src/test_symbolic/test_partial.py deleted file mode 100644 index 5b8ee4d..0000000 --- a/src/test_symbolic/test_partial.py +++ /dev/null @@ -1,34 +0,0 @@ -from sympy import diag, sin, symbols - -from riccipy.metric import * -from riccipy.partial import * -from riccipy.tensor import * - - -def _generate_schwarzschild(): - coords = symbols("t r theta phi", real=True) - t, r, th, ph = coords - schw = diag(1 - 1 / r, -1 / (1 - 1 / r), -r ** 2, -r ** 2 * sin(th) ** 2) - g = SpacetimeMetric("g", coords, schw, timelike=True) - mu, nu = indices("mu nu", g) - return (coords, t, r, th, ph, schw, g, mu, nu) - - -def test_DiffOperator(): - r = Symbol("r") - dr = DiffOperator(r) - expr = dr * (1 - 1 / r) - assert expr == 1 / r ** 2 - expr = (1 - 1 / r) * dr - assert isinstance(expr, DiffOperator) - assert (expr * (r ** 3 / 3)).equals(r * (r - 1)) - - -def test_PartialDerivative(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - d = g.partial - assert isinstance(d, PartialDerivative) - assert isinstance(d, Tensor) - assert d.commutes_with(d) == 0 - assert d.covar == (-1,) - assert all([isinstance(dx, DiffOperator) for dx in d.as_array()]) diff --git a/src/test_symbolic/test_tensor.py b/src/test_symbolic/test_tensor.py deleted file mode 100644 index ab68c7e..0000000 --- a/src/test_symbolic/test_tensor.py +++ /dev/null @@ -1,149 +0,0 @@ -from sympy import Array, diag, eye, simplify, sin, symbols -from sympy.tensor.tensor import TensExpr, TensMul - -from riccipy.metric import * -from riccipy.tensor import * - - -def _generate_simple(): - coords = symbols("x y z", real=True) - metric = Metric("metric", coords, eye(3)) - return (coords, metric) - - -def _generate_schwarzschild(): - coords = symbols("t r theta phi", real=True) - t, r, th, ph = coords - schw = diag(1 - 1 / r, -1 / (1 - 1 / r), -r ** 2, -r ** 2 * sin(th) ** 2) - g = Metric("g", coords, schw) - mu, nu = indices("mu nu", g) - return (coords, t, r, th, ph, schw, g, mu, nu) - - -def _generate_minkowski(): - coords = symbols("t x y z", real=True) - t, x, y, z = coords - mink = diag(1, -1, -1, -1) - eta = Metric("eta", coords, mink) - mu, nu = indices("mu nu", eta) - return (coords, t, x, y, z, mink, eta, mu, nu) - - -def test_Index(): - (coords, metric) = _generate_simple() - mu = Index("mu", metric) - assert isinstance(mu, TensorIndex) - assert mu.is_up - assert not (-mu).is_up - - -def test_indices(): - (coords, metric) = _generate_simple() - idxs = indices("i0:4", metric) - assert len(idxs) == 4 - assert all([isinstance(i, Index) for i in idxs]) - - -def test_Tensor(): - from sympy.tensor.tensor import tensorsymmetry - - (coords, metric) = _generate_simple() - T = Tensor("T", coords, metric) - assert isinstance(T, TensorHead) - assert T.as_array() == Array(coords) - assert T.covar == (1,) - assert T.symmetry == tensorsymmetry([1]) - - -def test_Tensor_comm(): - (coords, metric) = _generate_simple() - T = Tensor("T", coords, metric) - assert T.commutes_with(T) == 0 - assert T.commutes_with(metric) == 0 - assert T.commutes_with(metric.partial) is None - - -def test_Tensor_simplify(): - (coords, metric) = _generate_simple() - x, y, z = coords - expr = -(1 - x) ** 2 / (x - 1) - expr_simplified = 1 - x - T = Tensor("T", 3 * [expr], metric) - assert str(T.simplify()[0]) == str(expr_simplified) - - -def test_Tensor_covariance_transform(): - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - E, p1, p2, p3 = symbols("E p_1:4", positive=True) - p = Tensor("p", [E, p1, p2, p3], g) - res = p.covariance_transform(-mu) - expect = Array( - [E * (1 - 1 / r), -p1 / (1 - 1 / r), -p2 * r ** 2, -p3 * r ** 2 * sin(th) ** 2] - ) - assert res[0].equals(expect[0]) - assert res[1].equals(expect[1]) - assert res[2].equals(expect[2]) - assert res[3].equals(expect[3]) - - -def test_AbstractTensor(): - (coords, metric) = _generate_simple() - T = Tensor("T", coords, metric) - assert isinstance(T, AbstractTensor) - assert isinstance(T.as_array(), Array) - assert T.is_Tensor - assert not T.is_Metric - assert not T.is_Spacetime - - -def test_IndexedTensor(): - (coords, metric) = _generate_simple() - T = Tensor("T", coords, metric) - mu = Index("mu", metric) - assert isinstance(T(mu), IndexedTensor) - - -def test_ReplacementManager(): - pass - - -def test_expand_tensor(): - (coords, t, r, th, ph, mink, eta, mu, nu) = _generate_minkowski() - E1, E2, E3, B1, B2, B3 = symbols("E_1:4 B_1:4", real=True) - matrix = [[0, -E1, -E2, -E3], [E1, 0, -B3, B2], [E2, B3, 0, -B1], [E3, -B2, B1, 0]] - F = Tensor("F", matrix, eta, symmetry=[[2]]) - assert expand_tensor(eta(mu, nu) * eta(-mu, -nu)) == 4 - assert expand_tensor(F(mu, -mu)) == 0 - assert ( - expand_tensor(F(mu, nu) * F(-mu, -nu)) - == 2 * B1 ** 2 - + 2 * B2 ** 2 - + 2 * B3 ** 2 - - 2 * E1 ** 2 - - 2 * E2 ** 2 - - 2 * E3 ** 2 - ) - (coords, t, r, th, ph, schw, g, mu, nu) = _generate_schwarzschild() - x = Tensor("x", [t, r, th, ph], g) - res = expand_tensor(g(mu, nu)) - assert schw.inv().equals(res) - res1 = expand_tensor(g(mu, nu) * g(-mu, -nu)) - res2 = expand_tensor(g(-mu, -nu) * g(mu, nu)) - assert res1 == res2 - assert simplify(res1) == 4 - res1 = expand_tensor(g(-mu, -nu) * x(nu)) - res2 = expand_tensor(x(-mu)) - res3 = x.covariance_transform(-mu) - assert res1 == res2 - assert res2 == res3 - res1 = expand_tensor(g(mu, nu) * x(-nu)) - res2 = expand_tensor(x(mu)) - assert simplify(res1) == res2 - res = expand_tensor(x(mu) * x(-mu)) - assert ( - res - == t ** 2 * (1 - 1 / r) - - r ** 2 / (1 - 1 / r) - - th ** 2 * r ** 2 - - ph ** 2 * r ** 2 * sin(th) ** 2 - ) From fd40e0e24fa47298db719e47d962314b624703d8 Mon Sep 17 00:00:00 2001 From: cjayross Date: Wed, 3 Jul 2019 21:54:27 -0500 Subject: [PATCH 13/18] removed metric commutation group, added tox.ini, reformatted --- src/riccipy/tensor.py | 4 ++-- src/riccipy/tests/ipy_test_environment.py | 3 +-- src/riccipy/tests/test_metric.py | 1 + src/riccipy/tests/test_partial.py | 3 +-- src/riccipy/tests/test_tensor.py | 5 ++--- 5 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/riccipy/tensor.py b/src/riccipy/tensor.py index 53a4473..5df7294 100644 --- a/src/riccipy/tensor.py +++ b/src/riccipy/tensor.py @@ -3,9 +3,9 @@ from sympy import Array, simplify, symbols from sympy.core.compatibility import string_types from sympy.tensor.array import permutedims, tensorcontraction, tensorproduct +from sympy.tensor.tensor import TensMul +from sympy.tensor.tensor import Tensor as SympyTensor from sympy.tensor.tensor import ( - TensMul, - Tensor as SympyTensor, TensorHead, TensorIndex, TensorManager, diff --git a/src/riccipy/tests/ipy_test_environment.py b/src/riccipy/tests/ipy_test_environment.py index 445354a..57d4936 100644 --- a/src/riccipy/tests/ipy_test_environment.py +++ b/src/riccipy/tests/ipy_test_environment.py @@ -1,8 +1,7 @@ -from sympy import * - from riccipy.metric import * from riccipy.partial import * from riccipy.tensor import * +from sympy import * init_printing() diff --git a/src/riccipy/tests/test_metric.py b/src/riccipy/tests/test_metric.py index 0e243bd..c67a754 100644 --- a/src/riccipy/tests/test_metric.py +++ b/src/riccipy/tests/test_metric.py @@ -3,6 +3,7 @@ from riccipy.metric import * from riccipy.partial import * from riccipy.tensor import * +from sympy import Expr, diag, sin, symbols, zeros def _generate_schwarzschild(): diff --git a/src/riccipy/tests/test_partial.py b/src/riccipy/tests/test_partial.py index 6b1402b..31b2149 100644 --- a/src/riccipy/tests/test_partial.py +++ b/src/riccipy/tests/test_partial.py @@ -1,8 +1,7 @@ -from sympy import diag, sin, symbols - from riccipy.metric import * from riccipy.partial import * from riccipy.tensor import * +from sympy import diag, sin, symbols def _generate_schwarzschild(): diff --git a/src/riccipy/tests/test_tensor.py b/src/riccipy/tests/test_tensor.py index ab68c7e..27623e4 100644 --- a/src/riccipy/tests/test_tensor.py +++ b/src/riccipy/tests/test_tensor.py @@ -1,8 +1,7 @@ -from sympy import Array, diag, eye, simplify, sin, symbols -from sympy.tensor.tensor import TensExpr, TensMul - from riccipy.metric import * from riccipy.tensor import * +from sympy import Array, diag, eye, simplify, sin, symbols +from sympy.tensor.tensor import TensExpr, TensMul def _generate_simple(): From d4df75374735c8ac63a976c61d97b6d316a513d1 Mon Sep 17 00:00:00 2001 From: cjayross Date: Sat, 6 Jul 2019 19:36:31 -0500 Subject: [PATCH 14/18] reformatted --- src/riccipy/tensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/riccipy/tensor.py b/src/riccipy/tensor.py index 5df7294..53a4473 100644 --- a/src/riccipy/tensor.py +++ b/src/riccipy/tensor.py @@ -3,9 +3,9 @@ from sympy import Array, simplify, symbols from sympy.core.compatibility import string_types from sympy.tensor.array import permutedims, tensorcontraction, tensorproduct -from sympy.tensor.tensor import TensMul -from sympy.tensor.tensor import Tensor as SympyTensor from sympy.tensor.tensor import ( + TensMul, + Tensor as SympyTensor, TensorHead, TensorIndex, TensorManager, From eebf7a48fe3bf6b0a16f80eea9656625a93498aa Mon Sep 17 00:00:00 2001 From: cjayross Date: Sat, 6 Jul 2019 19:38:32 -0500 Subject: [PATCH 15/18] ran isort --- src/riccipy/tests/test_metric.py | 1 - src/riccipy/tests/test_partial.py | 3 ++- src/riccipy/tests/test_tensor.py | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/riccipy/tests/test_metric.py b/src/riccipy/tests/test_metric.py index c67a754..0e243bd 100644 --- a/src/riccipy/tests/test_metric.py +++ b/src/riccipy/tests/test_metric.py @@ -3,7 +3,6 @@ from riccipy.metric import * from riccipy.partial import * from riccipy.tensor import * -from sympy import Expr, diag, sin, symbols, zeros def _generate_schwarzschild(): diff --git a/src/riccipy/tests/test_partial.py b/src/riccipy/tests/test_partial.py index 31b2149..6b1402b 100644 --- a/src/riccipy/tests/test_partial.py +++ b/src/riccipy/tests/test_partial.py @@ -1,7 +1,8 @@ +from sympy import diag, sin, symbols + from riccipy.metric import * from riccipy.partial import * from riccipy.tensor import * -from sympy import diag, sin, symbols def _generate_schwarzschild(): diff --git a/src/riccipy/tests/test_tensor.py b/src/riccipy/tests/test_tensor.py index 27623e4..ab68c7e 100644 --- a/src/riccipy/tests/test_tensor.py +++ b/src/riccipy/tests/test_tensor.py @@ -1,8 +1,9 @@ -from riccipy.metric import * -from riccipy.tensor import * from sympy import Array, diag, eye, simplify, sin, symbols from sympy.tensor.tensor import TensExpr, TensMul +from riccipy.metric import * +from riccipy.tensor import * + def _generate_simple(): coords = symbols("x y z", real=True) From 3890bfabacfad58343f58879ca47deb8a8f3459d Mon Sep 17 00:00:00 2001 From: cjayross Date: Sun, 7 Jul 2019 20:51:00 -0500 Subject: [PATCH 16/18] fixed calculation of Ricci scalar --- src/riccipy/metric.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/riccipy/metric.py b/src/riccipy/metric.py index 904bae6..ad24a51 100644 --- a/src/riccipy/metric.py +++ b/src/riccipy/metric.py @@ -159,8 +159,9 @@ def ricci_scalar(self): """ if self._ricci_scalar is None: mu, nu = indices("mu nu", self) + g = self.metric RR = self.ricci_tensor - res = expand_tensor(RR(mu, -mu)) + res = expand_tensor(g(-mu, -nu) * RR(mu, nu)) self._ricci_scalar = res return self._ricci_scalar From 9606c210654e4eed38ff64e2f3042bd4b6c7b191 Mon Sep 17 00:00:00 2001 From: cjayross Date: Sun, 7 Jul 2019 20:53:28 -0500 Subject: [PATCH 17/18] reformatted --- src/riccipy/tests/test_metric.py | 3 +-- src/riccipy/tests/test_partial.py | 3 +-- src/riccipy/tests/test_tensor.py | 5 ++--- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/riccipy/tests/test_metric.py b/src/riccipy/tests/test_metric.py index 0e243bd..f87675c 100644 --- a/src/riccipy/tests/test_metric.py +++ b/src/riccipy/tests/test_metric.py @@ -1,8 +1,7 @@ -from sympy import Expr, diag, eye, sin, symbols, tensorproduct, zeros - from riccipy.metric import * from riccipy.partial import * from riccipy.tensor import * +from sympy import Expr, diag, eye, sin, symbols, tensorproduct, zeros def _generate_schwarzschild(): diff --git a/src/riccipy/tests/test_partial.py b/src/riccipy/tests/test_partial.py index 6b1402b..31b2149 100644 --- a/src/riccipy/tests/test_partial.py +++ b/src/riccipy/tests/test_partial.py @@ -1,8 +1,7 @@ -from sympy import diag, sin, symbols - from riccipy.metric import * from riccipy.partial import * from riccipy.tensor import * +from sympy import diag, sin, symbols def _generate_schwarzschild(): diff --git a/src/riccipy/tests/test_tensor.py b/src/riccipy/tests/test_tensor.py index ab68c7e..27623e4 100644 --- a/src/riccipy/tests/test_tensor.py +++ b/src/riccipy/tests/test_tensor.py @@ -1,8 +1,7 @@ -from sympy import Array, diag, eye, simplify, sin, symbols -from sympy.tensor.tensor import TensExpr, TensMul - from riccipy.metric import * from riccipy.tensor import * +from sympy import Array, diag, eye, simplify, sin, symbols +from sympy.tensor.tensor import TensExpr, TensMul def _generate_simple(): From 04f6ad26a0993a74b31e59877f5c1a20e47cc0be Mon Sep 17 00:00:00 2001 From: cjayross Date: Tue, 9 Jul 2019 02:08:05 -0500 Subject: [PATCH 18/18] removed ipy_test_environment --- src/riccipy/tests/ipy_test_environment.py | 29 ----------------------- 1 file changed, 29 deletions(-) delete mode 100644 src/riccipy/tests/ipy_test_environment.py diff --git a/src/riccipy/tests/ipy_test_environment.py b/src/riccipy/tests/ipy_test_environment.py deleted file mode 100644 index 57d4936..0000000 --- a/src/riccipy/tests/ipy_test_environment.py +++ /dev/null @@ -1,29 +0,0 @@ -from riccipy.metric import * -from riccipy.partial import * -from riccipy.tensor import * -from sympy import * - -init_printing() - -t, r, th, ph, x, y, z = symbols("t r theta phi x y z", real=True) -momentum = symbols("E p_1:4", positive=True) -E, p1, p2, p3 = momentum -# schw = diag(1-1/r, -1/(1-1/r), -r**2, -r**2*sin(th)**2) -alpha, beta = symbols("alpha beta", cls=Function) -schw = diag(-exp(2 * alpha(r)), exp(2 * beta(r)), r ** 2, r ** 2 * sin(th) ** 2) -g = Metric("g", [t, r, th, ph], schw) -x = Tensor("x", [t, r, th, ph], g) -p = Tensor("p", momentum, g) -mu, nu, si, rh, la = indices("mu nu sigma rho lambda", g) -d = g.partial -Gamma = g.christoffel -R = g.riemann -G = g.einstein -C = g.weyl - -# mink = diag(1, -1, -1, -1) -# eta = Metric('eta', [t, x, y, z], mink) -# al, be, ga = indices('alpha beta gamma', eta) -# E1, E2, E3, B1, B2, B3 = symbols('E_1:4 B_1:4', real=True) -# matrix = [[0, -E1, -E2, -E3], [E1, 0, -B3, B2], [E2, B3, 0, -B1], [E3, -B2, B1, 0]] -# F = Tensor('F', matrix, eta, symmetry=[[2]])