def test_add_sub(self):
        container1 = mm.Dynamics(terms=[self.precession, self.zhangli])
        container2 = mm.Dynamics(terms=[self.damping])
        container3 = mm.Dynamics(terms=[self.precession, self.damping])

        assert container1 != container2

        res = container1 + container2
        check_container(res)
        assert len(res) == 3
        assert container1 + container2 == container2 + container1

        res = container3 - container2
        check_container(res)
        assert len(res) == 1
        assert self.damping not in res
        with pytest.raises(ValueError):
            res -= self.damping

        assert res + self.damping == self.damping + res

        with pytest.raises(TypeError):
            res = container1 + 5

        with pytest.raises(TypeError):
            res = container1 - 5
    def test_init_invalid_args(self):
        container = mm.Dynamics()
        for term in self.invalid_terms:
            with pytest.raises(TypeError):
                container += term

        check_container(container)
        assert len(container) == 0
Esempio n. 3
0
 def dynamics(self, value):
     empty_container = mm.Dynamics()
     if value == 0:
         self._dynamics = empty_container
     elif isinstance(value, (mm.DynamicsTerm, mm.Dynamics)):
         self._dynamics = empty_container + value  # checks by + operator
     else:
         msg = f'Cannot set dynamics equation with {type(value)}.'
         raise TypeError(msg)
    def test_repr(self):
        container = mm.Dynamics(terms=self.terms)
        check_container(container)

        assert isinstance(repr(container), str)
        assert 'Precession' in repr(container)

        container -= container.precession

        assert 'Precession' not in repr(container)
Esempio n. 5
0
    def test_add_terms(self):
        dynamics = mm.Dynamics()
        for term in self.terms:
            dynamics._add(term)
            assert isinstance(dynamics, mm.Dynamics)
            assert isinstance(dynamics.terms, list)
            assert dynamics.terms[-1] == term
            assert dynamics.terms[-1].name == term.name

        assert len(dynamics.terms) == 3
    def test_init(self):
        # Init with terms list.
        container = mm.Dynamics(terms=self.terms)
        check_container(container)
        assert len(container) == 3

        # Empty terms list.
        container = mm.Dynamics()
        check_container(container)
        assert len(container) == 0

        # Add terms one by one.
        for i, term in enumerate(self.terms):
            container += term
            check_container(container)
            assert len(container) == i + 1
            assert isinstance(container, mm.Dynamics)

        # Create container as a sum of terms.
        container = (self.precession + self.damping)
        check_container(container)
        assert len(container) == 2
    def test_getattr(self):
        container = mm.Dynamics(terms=self.terms)
        check_container(container)

        assert isinstance(container.precession, mm.Precession)
        assert hasattr(container.precession, 'gamma0')
        assert isinstance(container.damping, mm.Damping)
        assert hasattr(container.damping, 'alpha')
        assert isinstance(container.zhangli, mm.ZhangLi)
        assert hasattr(container.zhangli, 'u')
        assert hasattr(container.zhangli, 'beta')

        # Try to get non-existing attribute.
        container -= self.damping
        check_container(container)
        with pytest.raises(AttributeError):
            damping = container.damping
Esempio n. 8
0
    def test_repr_latex(self):
        dynamics = mm.Dynamics()
        latex = dynamics._repr_latex_()
        assert latex[0] == latex[-1] == '$'
        assert latex.count('$') == 2
        assert '\\frac' in latex
        assert latex[-2] == '0'

        for term in self.terms:
            dynamics._add(term)

        latex = dynamics._repr_latex_()

        assert latex[0] == latex[-1] == '$'
        assert latex.count('$') == 2
        assert r'-\gamma_{0}^{*}' in latex
        assert r'\mathbf{m}' in latex
        assert r'\mathbf{H}_\text{eff}' in latex
        assert r'\times' in latex
        assert r'\alpha' in latex
        assert latex.count('-') == 2
        assert latex.count('+') == 2
        assert latex.count('=') == 1
        assert latex.count(r'\partial') == 4
 def test_repr_latex(self):
     container = mm.Dynamics()
     check_container(container)
     latexstr = container._repr_latex_()
     assert latexstr == '$0$'
Esempio n. 10
0
 def test_add_exception(self):
     dynamics = mm.Dynamics()
     for term in self.invalid_terms:
         with pytest.raises(TypeError):
             dynamics += term
Esempio n. 11
0
    def test_add_dynamics(self):
        term_sum = self.precession + self.damping + self.stt
        dynamics = mm.Dynamics()
        dynamics += term_sum

        assert len(dynamics.terms) == 3
Esempio n. 12
0
 def test_script(self):
     dynamics = mm.Dynamics()
     with pytest.raises(NotImplementedError):
         script = dynamics._script